Get the number of clock cycles
#include <sys/neutrino.h> #include <inttypes.h> uint64_t ClockCycles( void );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The ClockCycles() kernel call returns the current value of a free-running 64-bit cycle counter. This is implemented on each processor as a high-performance mechanism for timing short intervals.
Several CPU architectures have an instruction that reads such a free-running counter (e.g. x86 has the RDTSC instruction). For processors that don't implement such an instruction in hardware (e.g. a 386), the kernel emulates one. This provides a lower time resolution than if the instruction is provided (838.095345 nanoseconds on an IBM PC-compatible system).
In all cases, the SYSPAGE_ENTRY(qtime)->cycles_per_sec field gives the number of ClockCycles() increments in one second.
This function, depending on the CPU architecture, returns a value from a register that's unique to each CPU in an SMP system — for instance, the TSC (Time Stamp Counter) on an x86. These registers aren't synchronized between the CPUs. So if you call ClockCycles(), and then the thread migrates to another CPU and you call ClockCycles() again, you can't subtract the two values to get a meaningful time duration.
If you wish to use ClockCycles() on an SMP machine, you must use the following call to “lock” the thread to a single CPU:
ThreadCtl(_NTO_TCTL_RUNMASK, ...)
For more information, see the entry for ThreadCtl().
This call doesn't block.
Be careful about wrapping of the cycle counter. Use the following to calculate how many seconds before the cycle counter wraps: (~(uint64_t)0) /SYSPAGE_ENTRY(qtime) -> cycles_per_sec |
See SYSPAGE_ENTRY().
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | Yes |
Signal handler | Yes |
Thread | Yes |
Clocks, Timers, and Getting a Kick Every So Often chapter of Getting Started with QNX Neutrino