Skip to content

Commit 6bda976

Browse files
authored
Merge pull request #4082 from hdiethelm/rtapi_get_clocks_removal_v2
Remove rtapi_get_clocks()
2 parents 4761ed7 + 217cfb8 commit 6bda976

17 files changed

Lines changed: 34 additions & 177 deletions

File tree

docs/src/config/core-components.adoc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,8 @@ change or removal at any time.
170170
* 'motion.debug-float-3' - (float, RO) This is used for debugging purposes.
171171
* 'motion.debug-s32-0' - (s32, RO) This is used for debugging purposes.
172172
* 'motion.debug-s32-1' - (s32, RO) This is used for debugging purposes.
173-
* 'motion.servo.last-period' - (u32, RO) The number of CPU cycles between invocations of the servo thread.
174-
Typically, this number divided by the CPU speed gives the time in seconds,
175-
and can be used to determine whether the realtime motion controller is meeting its timing constraints
176-
* 'motion.servo.last-period-ns' - (float, RO)
173+
* 'motion.servo.last-period' - (u32, RO) The time in ns between invocations of the servo thread.
174+
This number can be used to determine whether the realtime motion controller is meeting its timing constraints
177175

178176
=== Functions
179177

docs/src/hal/basic-hal.adoc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,12 +373,13 @@ If you used the Stepper Config Wizard to generate your config you will have up t
373373
== HAL Parameter
374374

375375
(((HAL Parameters)))
376-
Two parameters are automatically added to each HAL component when it is created.
377-
These parameters allow you to scope the execution time of a component.
376+
One pin and two parameters are automatically added to each HAL component when it is created.
377+
These allow you to scope the execution time of a component.
378378

379379
[horizontal]
380-
`.time`(((HAL time))):: Time is the number of CPU cycles it took to execute the function.
381-
`.tmax`(((HAL tmax))):: Tmax is the maximum number of CPU cycles it took to execute the function.
380+
`.time`(((HAL time))):: Pin time shows in ns how long it took to execute the function.
381+
`.tmax`(((HAL tmax))):: Parameter tmax is the maximum time in ns it took to execute the function.
382+
`.tmax-increased`(((HAL tmax-increased))):: This parameter is set to true for one cycle if tmax increased.
382383

383384
`tmax` is a read/write parameter so the user can set it to 0 to get rid of the first time initialization on the function's execution time.
384385

docs/src/hal/components.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,6 @@ rtapi_clock_set_period.3
511511
rtapi_delay.3
512512
rtapi_delay_max.3
513513
rtapi_exit.3
514-
rtapi_get_clocks.3
515514
rtapi_get_msg_level.3
516515
rtapi_get_time.3
517516
rtapi_inb.3

docs/src/man/man3/rtapi_get_time.3.adoc

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
== NAME
66

7-
rtapi_get_time, rtapi_get_clocks - get the current time
7+
rtapi_get_time - get the current time in ns
88

99
== SYNTAX
1010

1111
[source,c]
1212
----
1313
long long rtapi_get_time();
14-
long long rtapi_get_clocks();
1514
----
1615

1716
== DESCRIPTION
@@ -25,55 +24,19 @@ resolution of the returned value may be as good as one nano-second, or
2524
as poor as several microseconds. May be called from init/cleanup code,
2625
and from within realtime tasks.
2726

28-
*rtapi_get_clocks* returns the current time in CPU clocks. It is fast,
29-
since it just reads the TSC in the CPU instead of calling a kernel or
30-
RTOS function. Of course, times measured in CPU clocks are not as
31-
convenient, but for relative measurements this works fine. Its absolute
32-
value means nothing, but it is monotonically increasing and can be used
33-
to schedule future events, or to time the duration of some activity. (On
34-
SMP machines, the two TSC's may get out of sync, so if a task reads the
35-
TSC, gets swapped to the other CPU, and reads again, the value may
36-
decrease. RTAPI tries to force all RT tasks to run on one CPU.) Returns
37-
a 64 bit value. The resolution of the returned value is one CPU clock,
38-
which is usually a few nanoseconds to a fraction of a nanosecond. Note
39-
that _long long_ math may be poorly supported on some platforms,
40-
especially in kernel space. Also note that rtapi_print() will NOT print
41-
__long long__s. Most time measurements are relative, and should be done
42-
like this:
43-
4427
[source,c]
4528
----
4629
deltat = (long int)(end_time - start_time);
4730
----
4831

4932
where end_time and start_time are longlong values returned from
5033
rtapi_get_time, and deltat is an ordinary long int (32 bits). This will
51-
work for times up to a second or so, depending on the CPU clock
52-
frequency. It is best used for millisecond and microsecond scale
34+
work for times up to 2.1s. It is best used for millisecond and microsecond scale
5335
measurements though.
5436

5537
== RETURN VALUE
5638

57-
Returns the current time in nanoseconds or CPU clocks.
58-
59-
== NOTES
60-
61-
Certain versions of the Linux kernel provide a global variable *cpu_khz*. Computing
62-
63-
[source,c]
64-
----
65-
deltat = (end_clocks - start_clocks) / cpu_khz:
66-
----
67-
68-
gives the duration measured in milliseconds. Computing
69-
70-
[source,c]
71-
----
72-
deltat = (end_clocks - start_clocks) * 1000000 / cpu_khz:
73-
----
74-
75-
gives the duration measured in nanoseconds for deltas less than about 9
76-
trillion clocks (e.g., 3000 seconds at 3 GHz).
39+
Returns the current time in nanoseconds.
7740

7841
== REALTIME CONSIDERATIONS
7942

docs/src/man/man9/encoder.9.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ The *encoder*.__N__. format is shown in the following descriptions.
139139
for counting the output of a single channel (non-quadrature) sensor.
140140
When false (the default), it counts in quadrature mode.
141141
**encoder**.__N__.**capture-position.tmax** s32 rw::
142-
Maximum number of CPU cycles it took to execute this function.
142+
Maximum time in ns it took to execute this function.
143143

144144
== PARAMETERS
145145

docs/src/man/man9/motion.9.adoc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ digital pins and two analog pins.
106106
== MOTION PINS
107107

108108
*motion-command-handler.time* OUT S32::
109-
Time (in CPU clocks) for the motion module motion-command-handler
109+
Time (in ns) for the motion module motion-command-handler
110110
*motion-controller.time* OUT S32::
111-
Time (in CPU clocks) for the motion module motion-controller
111+
Time (in ns) for the motion module motion-controller
112112
*motion.adaptive-feed* IN FLOAT::
113113
When adaptive feed is enabled with M52 P1, the commanded velocity is
114114
multiplied by this value. This effect is multiplicative with the
@@ -246,7 +246,7 @@ Note: feed-inhibit applies to G-code commands -- not jogs.
246246
possibly reduced to accommodate machine velocity and acceleration limits.
247247
The value on this pin does not reflect the feed override or any other adjustments.
248248
*motion.servo.last-period* OUT U32::
249-
The number of CPU clocks between invocations of the servo thread.
249+
Time (in ns) between invocations of the servo thread.
250250
Typically, this number divided by the CPU speed gives the time in seconds,
251251
and can be used to determine whether the realtime motion
252252
controller is meeting its timing constraints
@@ -544,12 +544,12 @@ the M19 command fails with an error message.
544544
Many of the parameters serve as debugging aids, and are subject to change or removal at any time.
545545

546546
*motion-command-handler.tmax* RW S32::
547-
Show information about the execution time of these HAL functions in CPU clocks.
547+
Show information about the execution time of these HAL functions in ns.
548548

549549
*motion-command-handler.tmax-increased* RO S32::
550550

551551
*motion-controller.tmax* RW S32::
552-
Show information about the execution time of these HAL functions in CPU clocks.
552+
Show information about the execution time of these HAL functions in ns.
553553

554554
*motion-controller.tmax-increased* RO BIT::
555555
+

src/emc/motion/control.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,9 @@ void emcmotController(void *arg, long period)
225225

226226
static long long int last = 0;
227227

228-
long long int now = rtapi_get_clocks();
228+
long long int now = rtapi_get_time();
229229
long int this_run = (long int)(now - last);
230230
*(emcmot_hal_data->last_period) = this_run;
231-
#ifdef HAVE_CPU_KHZ
232-
*(emcmot_hal_data->last_period_ns) = this_run * 1e6 / cpu_khz;
233-
#endif
234231

235232
// we need this for next time
236233
last = now;

src/emc/motion/mot_priv.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,7 @@ typedef struct {
172172
hal_float_t tc_acc[4]; /* RPA: traj internals, for debugging */
173173

174174
// realtime overrun detection
175-
hal_u32_t *last_period; /* pin: last period in clocks */
176-
hal_float_t *last_period_ns; /* pin: last period in nanoseconds */
175+
hal_u32_t *last_period; /* pin: last period in nanoseconds */
177176

178177
hal_float_t *tooloffset_x;
179178
hal_float_t *tooloffset_y;
@@ -348,8 +347,4 @@ int joint_is_lockable(int joint_num);
348347

349348
#define SET_JOINT_FAULT_FLAG(joint,fl) if (fl) (joint)->flag |= EMCMOT_JOINT_FAULT_BIT; else (joint)->flag &= ~EMCMOT_JOINT_FAULT_BIT;
350349

351-
#if defined(__KERNEL__)
352-
#define HAVE_CPU_KHZ
353-
#endif
354-
355350
#endif /* MOT_PRIV_H */

src/emc/motion/motion.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -593,9 +593,6 @@ static int init_hal_io(void)
593593

594594
// export timing related HAL pins so they can be scoped and/or connected
595595
CALL_CHECK(hal_pin_u32_newf(HAL_OUT, &(emcmot_hal_data->last_period), mot_comp_id, "motion.servo.last-period"));
596-
#ifdef HAVE_CPU_KHZ
597-
CALL_CHECK(hal_pin_float_newf(HAL_OUT, &(emcmot_hal_data->last_period_ns), mot_comp_id, "motion.servo.last-period-ns"));
598-
#endif
599596

600597
// export timing related HAL pins so they can be scoped
601598
CALL_CHECK(hal_pin_float_newf(HAL_OUT, &(emcmot_hal_data->tooloffset_x), mot_comp_id, "motion.tooloffset.x"));

src/hal/drivers/hal_gpio.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ MODULE_AUTHOR("Andy Pugh");
5050
MODULE_DESCRIPTION("GPIO driver using gpiod / libgpiod");
5151
MODULE_LICENSE("GPL");
5252

53-
static unsigned long ns2tsc_factor;
54-
#define ns2tsc(x) (((x) * (unsigned long long)ns2tsc_factor) >> 12)
55-
5653
// There isn't really any limit except for in the MP_ARRAY macros.
5754
#define MAX_CHAN 128
5855

@@ -358,14 +355,6 @@ int rtapi_app_main(void){
358355
const char *line_name;
359356

360357
rtapi_print_msg(RTAPI_MSG_INFO, "Libgpiod is %i\n", LIBGPIOD_VER);
361-
362-
#ifdef __KERNEL__
363-
// this calculation fits in a 32-bit unsigned
364-
// as long as CPUs are under about 6GHz
365-
ns2tsc_factor = (cpu_khz << 6) / 15625ul;
366-
#else
367-
ns2tsc_factor = 1ll<<12;
368-
#endif
369358

370359
comp_id = hal_init("hal_gpio");
371360
if (comp_id < 0) {
@@ -520,7 +509,7 @@ static void hal_gpio_write(void *arg, __attribute__((unused)) long period)
520509
#endif
521510
}
522511
// store the time (in CPU clocks) for the reset function
523-
last_reset = rtapi_get_clocks();
512+
last_reset = rtapi_get_time();
524513
}
525514

526515
static void hal_gpio_reset(void *arg, long period)
@@ -537,8 +526,8 @@ static void hal_gpio_reset(void *arg, long period)
537526
}
538527
}
539528
if (*gpio->reset_ns > period/4) *gpio->reset_ns = period/4;
540-
deadline = last_reset + ns2tsc(*gpio->reset_ns);
541-
while(rtapi_get_clocks() < deadline) {} // busy-wait!
529+
deadline = last_reset + *gpio->reset_ns;
530+
while(rtapi_get_time() < deadline) {} // busy-wait!
542531
#if LIBGPIOD_VER > 200
543532
gpiod_line_request_set_values(gpio->out_chips[c].lines, gpio->out_chips[c].vals);
544533
#else

0 commit comments

Comments
 (0)