rtapi_get_time(3rtapi)

πŸ“¦
version 2.9.4 Β· 2 versions
, in debian trixie, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

okay so bestie, welcome to rtapi_get_time β€” this is page 3rtapi, living in the HAL manual, and it does exactly one iconic thing: it gets the current time. ✨


🌸 SYNTAX 🌸

long long rtapi_get_time()

long long rtapi_get_clocks()

πŸ¦‹ DESCRIPTION πŸ¦‹

rtapi_get_time hands you back the current time in nanoseconds. Depending on the RTOS, this may be time since boot, or time since the clock period was set, or some other time. Its absolute value means nothing β€” like, literally nothing, don't even look at it β€” but it is monotonically increasing, so it can be used to schedule future events, or to time the duration of some activity. Returns a 64 bit value. The resolution of the returned value may be as good as one nano-second, or as poor as several microseconds. May be called from init/cleanup code, and from within realtime tasks. πŸ’…

rtapi_get_clocks returns the current time in CPU clocks. It is fast, since it just reads the TSC in the CPU instead of calling a kernel or RTOS function. Of course, times measured in CPU clocks are not as convenient, but for relative measurements this works fine. Its absolute value means nothing, but it is monotonically increasing and can be used to schedule future events, or to time the duration of some activity. (On SMP machines, the two TSC's may get out of sync, so if a task reads the TSC, gets swapped to the other CPU, and reads again, the value may decrease. RTAPI tries to force all RT tasks to run on one CPU.) Returns a 64 bit value. The resolution of the returned value is one CPU clock, which is usually a few nanoseconds to a fraction of a nanosecond. πŸ•βœ¨

Note that long long math may be poorly supported on some platforms, especially in kernel space. Also note that rtapi_print() will NOT print long longs. 😭 (the betrayal fr fr)

Most time measurements are relative, and should be done like this:

deltat = (long int)(end_time - start_time);

where end_time and start_time are longlong values returned from rtapi_get_time, and deltat is an ordinary long int (32 bits). This will work for times up to a second or so, depending on the CPU clock frequency. It is best used for millisecond and microsecond scale measurements though. 🧚


πŸŽ€ RETURN VALUE πŸŽ€

Returns the current time in nanoseconds or CPU clocks. that's it, that's the whole thing πŸ’œ


⭐ NOTES ⭐

Certain versions of the Linux kernel provide a global variable cpu_khz. Computing

deltat = (end_clocks - start_clocks) / cpu_khz:

gives the duration measured in milliseconds. Computing

deltat = (end_clocks - start_clocks) * 1000000 / cpu_khz:

gives the duration measured in nanoseconds for deltas less than about 9 trillion clocks (e.g., 3000 seconds at 3 GHz). πŸ”₯


πŸ‘‘ REALTIME CONSIDERATIONS πŸ‘‘

May be called from init/cleanup code and from within realtime tasks. Not available in non-realtime components. 🚫


and that's the whole page, bestie 🫢 β€” this was the rtapi_get_time(3rtapi) glow-up, brought to you by the LinuxCNC Documentation girlies, dated 2006-10-12. periodt. πŸ’–πŸŒˆ