ClockCycles()

Updated: April 19, 2023

Get the number of clock cycles

Synopsis:

#include <sys/neutrino.h>
#include <inttypes.h>

uint64_t ClockCycles( void );

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The ClockCycles() function 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. On Intel, ClockCycles() reads the Time Stamp Counter (RDTSC) and on an ARM 64-bit system, it reads the Generic Timer.

You can use the SYSPAGE_ENTRY(qtime)->cycles_per_sec field to get the number of ClockCycles() increments in one second.

CAUTION:
While running your system, you need to ensure that ClockCycles() does not roll over. Although it's uncommon, it's a good idea to check. Knowing that the cycles counter is an unsigned 64-bit integer, use the following to calculate the duration in seconds to roll over:
(~(uint64_t)0) / SYSPAGE_ENTRY(qtime)->cycles_per_sec

For example, a 6 GHz system will take 97 years for ClockCycles() to roll over.

Note: QNX Neutrino requires that the hardware underlying ClockCycles() be synchronized across all processors on an SMP system. Here, “synchronized” means the difference in time values (i.e., deltas) measured by different processors must be small enough to be not software observable. This design means that you don't have to call ThreadCtl( _NTO_TCTL_RUNMASK, ...) to prevent threads from migrating to another processor between calls to ClockCycles().
In some cases, if you don't specify the frequency for ClockCycles() via the -f option in startup, startup will try to determine the frequency at each target boot. This frequency is not exact and varies from one boot to another. To ensure consistent results, we recommend that you query your hardware for the frequency, or consult your target's documentation. Once the frequency is determined, you can either have your target configuration specify the frequency using the -f in startup, or modify startup to have the frequency hard-coded. It's important to do this for systems that:
  • Use high resolution timers
  • Have tickless operation enabled
  • Run for a long period of time
For more information, see startup in the Utilities Reference guide.

Blocking states:

This call doesn't block.

Examples:

See SYSPAGE_ENTRY().

Classification:

QNX Neutrino

Safety:  
Cancellation point No
Interrupt handler Yes
Signal handler Yes
Thread Yes