c++profilingrtems

Time Short Functions with cpu time using RTEMS operating system


I am looking to profile some code in a real time operating system, RTEMS. Essentially, rtems has a bunch of functions to read the time, the most useful of which is rtems_clock_get_ticks_since_boot.

The problem here is that for whatever reason the clock ticks reported are synchronized with our state machine loop rate, 5kHz whereas the processor is running at around 200MHz (embedded system). I know this because i recorded the clock time, waited 1 sec and only 5000 ticks had gone by.

So the question is:

How can I get the actual CPU ticks from RTEMS?

PS. clock() from GNU C (has the same problem)

There is a guide that i have been looking into here, but I get impossible constraint in asm which indicates that i would need to use some different assembler keywords. Maybe someone can point me to something similar?


Context

I want to profile some code, so essentially:

start = cpu_clock_ticks()
    //Some code
time = cpu_clock_ticks() - start;

The code runs in less than 0.125ms so the 8khz counter that clock() and other rtems functions get, wont cut it.


Solution

  • So a solution to this is to use the following function:

    inline unsigned long timer_now()  {
        unsigned int time; 
    
        // The internal timer is accessed as special purpose register #268
        // (@24.576 MHz => 1tick=4.069010416E-8 sec,~.04µs 
        asm volatile ("mfspr %0,268; sync" : "=r" (time));
        return time;
    }
    

    timer_now will return tics that are still not at the processor speed, but much faster than 8kHz, the time taken can then be calculated as tics * 0.04µs.

    NOTE This may only work for the powerPC MPC5200 BSP for rtems, since it uses an assembler routine.