0

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.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
  • Before you suggest a profiling software, the application is time sensitive and wont work with profilers :( – Fantastic Mr Fox Mar 17 '15 at 00:30
  • Do you have any spare GPIO pins? We hook up oscilloscope probe to a GPIO pin. Initialize pin to deasserted. Assert pin at beginning of function and deassert at end of function. Measure the pulse width on o'scope. – Thomas Matthews Mar 17 '15 at 00:41
  • @ThomasMatthews There will not be any GPIO that i can get to easily. But maybe i can find a similar solution. You should add this as an answer, include some more detail, I would upvote. – Fantastic Mr Fox Mar 17 '15 at 00:42

3 Answers3

1

Accurate performance measurements can be made using an oscilloscope, provided that there is a GPIO, test point or pin that the software can write to (and the oscilloscope probe can attach to).

The method here is to send a pulse to the pin. The o'scope can be set up to trigger on the pulse. Some smarter o'scopes can perform statistics on the pulse width, such as mean time and maximum time.

On our embedded system, the H/W team was nice enough to bring out 8 test points for us to use. We initialize the pin to zero. At the start of the code to profile, we write a 1 to the pin. At the end of the profiling code, we write a 0 to the pin. This produces a pulse or square wave.

The o'scope is set up to trigger on the rising edge. The probe is connected to the pin and the program is run. Adjust the o'scope so the the entire pulse is visible on the screen. Re-run the program. When the o'scope triggers, measure the width of the pulse. This will be the actual time of the execution.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

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.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175
0

In RTEMS 4.11 or newer you can use rtems_counter_read to obtain high-precision counters that abstracts the CPU-specific assembly code. Please see: https://docs.rtems.org/doxygen/cpukit/html/group__ClassicCounter.html

RTEMS related questions like this are invariably answered more quickly and accurately when submitted to the subscribe-only users mailing list.

gedare
  • 126
  • 3
  • Cool, unfortunately we still use rtems 4.9.2. I also subscribe to the user mailing list and have asked quests there as well. I just like posting them here because they might attract some more attention and will build up a slightly larger database of rtems questions. – Fantastic Mr Fox Jun 04 '15 at 18:50