![]() |
![]() |
![]() |
Get the number of clock cycles
#include <sys/neutrino.h> #include <inttypes.h> uint64_t ClockCycles( void );
libc
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.
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 sychronized between the CPUs. So if you do a ClockCycles(), and then the thread is migrated to another CPU and another ClockCycles() is done, 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, ...)
This call doesn't block.
#include <sys/neutrino.h> #include <inttypes.h> #include <stdio.h> #include <stdlib.h> #include <sys/syspage.h> int main( void ) { uint64_t cps, cycle1, cycle2; float sec, ncycles; /* snap the time */ cycle1=ClockCycles( ); /* do something */ printf("poo\n"); /* snap the time again */ cycle2=ClockCycles( ); ncycles=cycle2-cycle1; printf("%d cycles elapsed \n", ncycles); /* find out how many cycles per second */ cps = SYSPAGE_ENTRY(qtime)->cycles_per_sec; printf( "This system has %lld cycles/sec.\n",cps ); sec=ncycles/cps; printf("The cycles in seconds is %f \n",sec); return EXIT_SUCCESS; }
Safety: | |
---|---|
Cancellation point | No |
Interrupt handler | Yes |
Signal handler | Yes |
Thread | Yes |
![]() |
![]() |
![]() |