how to calculate machine cycle counts for a process

hi all

i have a codec ported on QNX IDE. i need to calculate the machine cycles consumed by the codec. i got some info in the application profiler once i implemented post moterm profiling. But i just want something that i can obtain after each frame processed, apart from the entire test case/vector run. Normally incase of some dsp applications, i used something like


                        cycles = TIMER_STOP();

to obtain the cycle count information. I need similar kind of thing in QNX IDE

please help


#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, ncycles;
float sec;

/* snap the time */
cycle1=ClockCycles( );

/* do something */

/* snap the time again */
cycle2=ClockCycles( );
printf("%lld 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 );
printf("The cycles in seconds is %f \n",sec);



Hello Mukuntan,
Consider using ClockCycles() … ycles.html


hi all

thank for ur reply. actually i tried using clockcycles() earlier. but i was getting some inconsistent results. the values obtained are not similar everytime(for example im getting different value while debugging and altogether a different value while running ). is there anything to do with the target processor. because my target is dual core processor. or i need to introduce few more inbuilt APIs before while using clockcycles().

ClockCycles gives the current time, hence if the process is performaing at different speed for whatever reason you will get different value. Dual core can be nasty because ClockCycles gets a value from a counter that could be different in each core. If the process/thread migrate from core to core the result could be bogus.

Maybe clock() is what you are looking for?

since mine is a dual core processor, i got to know that i need to create a thread and it should be set with high priority and i need to put my API there and then i need to use clockcycles(). since i was new to RTOS and QNX. could u please suggest how to proceed

If you wish to use ClockCycles() on an SMP machine, you must use the following call to “lock” the thread to a single CPU:

See … ycles.html

that’s not 100% true, many intel processor have their counter synchronised. Even better on the later model I believe the counter frequency won’t be affected by power saving throttling of the processor frequency.

Even if it locked to a single cpu, the thread still can be pre-empted. so will the value return by clockcycles() make sense?

If you want to measure CPU time used by a thread you can do it manually with pidin ti. If you want to do it programatically get the source of pidin and check out how it does it.

I am not quite sure why ClockCycles() is required here.

A possible solution:

uint64_t time_internal;
ClockTime(CLOCK_MONOTONIC, NULL, &time_internal);
return time_internal;

now= gimme_clock();