System Administration, Reconfigurable Computing and Other Random Topics

Measuring time in a bare-metal Zynq application

If you want to measure elapsed time in a bare-metal application on the Xilinx Zynq SoC—for example to measure how long your external accelerator takes to get a result—you will soon notice that typical methods do not work. But there is a simple and precise replacement for those methods.

A method that is often recommended is to use gettimeofday() out of sys/time.h to get the current time before and after whatever time interval we want to measure and to take the difference of it. Trying this in a bare-metal application on the Zynq SoC we get the following error:

In function `_gettimeofday_r': gettimeofdayr.c:(.text+0x20): undefined reference to `_gettimeofday'

According to the man page this method is deprecated anyway, so let’s try the recommended replacement: clock_gettime() out of time.h:

undefined reference to `clock_gettime'

So, no luck either… How about just obtaining the clock count using clock() out of time.h?

In function `_times_r': timesr.c:(.text+0x4): undefined reference to `_times'

Luckily, there is a Xilinx specific replacement for the last approach. Here is a short sample program:

#include <stdio.h>
#include "platform.h"
#include "xparameters.h"
#include "xtime_l.h"

void print(char *str);

int main() {
    XTime tStart, tEnd;


    print("Hello World\n\r");

    printf("Output took %llu clock cycles.\n", 2*(tEnd - tStart));
    printf("Output took %.2f us.\n",
           1.0 * (tEnd - tStart) / (COUNTS_PER_SECOND/1000000));

    return 0;

This uses the global timer in the Zynq SoC whose counter increases every two clock cycles. So it gives pretty precise results.

Updates to this post:

  • Jul 28, 2015: Fix calculation of clock cycles in example program.

10 Responses to Measuring time in a bare-metal Zynq application

Leave a Reply to Brett Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.