Wednesday, 7 January 2015

Timing C Code Running On the XMOS xCORE Architecture

I recently had to show a customer how long a piece of code took to execute on the XMOS xCORE.

The nice thing about the xCORE is that it includes hardware timers that can be directly accessed from the application.

In order to be able to benchmark the code I knew there would be a small overhead to call the timers from the top level C code so my first task was to calculate this.

Having calculated the overhead I was now free to benchmark the actual code then remove the overhead to print the correct time.

As an aside, one of the great benefits of the XMOS architecture is how printf's are implemented. Essentially the output of a printf can be routed over the inter-processor communications link called xCONNECT, in real-time. This allows printf's to be located directly inside real-time code, unlike other microcontrollers where locating a printf inside an interrupt service routine is guaranteed to cause a run-time exception (AKA crash).

Here is the benchmarking code showing the performance of a simple piece of C code (or assembly) :

#include <stdio.h>

int main (void) 
  timer t;
  int start_time, end_time, overhead_time;

  t :> start_time;
  t :> end_time;
  overhead_time = end_time - start_time;

  t :> start_time;
  asm ("nop");
  asm ("nop");
  t :> end_time;
  printf ("Elapsed time = %d cycles\n", end_time - start_time - overhead_time);

  return (0);

And here is the output :

Elapsed time = 2 cycles

Phew, two nops takes two cycles :-)

XMOS use the incredible LLVM compiler, which I am sure will one day take over from GCC. It is almost never necessary to revert to assembly code but if you need to then you can see from the above example just how easy it is to use.
Maybe a future blog will cover how to read and write hardware (the timer, as shown above) but the instructions are essentially simple extensions to C (called XC) that are documented in the XMOS Programming Guide :

If you have found this solution useful then please do hit the Google (+1) button so that others may be able to find it as well.
Numerix-DSP Libraries :

No comments:

Post a comment