ClockCycles()

Updated: May 06, 2022

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() 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. 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.

We require that the hardware underlying ClockCycles() be synchronized across all processors on an SMP system. This means that you no longer 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