UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Voyager
Voyager
1,227 Views
Registered: ‎06-26-2015

performance timer in linux?

whats the correct timer to use in us, for measure code performance?

clock()  or  gettimeofday?  I use both, but result are different.  This is for zc706

 

 gettimeofday(&(program_timer.perf_start),NULL);
start=clock();

 

//some process

 

diff=clock() - start ;
     usec = diff * 1000000 / CLOCKS_PER_SEC;

 

gettimeofday(&(program_timer.perf_stop),NULL);
       perf_diff=(uint32_t)((program_timer.perf_stop.tv_usec)  -( program_timer.perf_start.tv_usec));

         printf( "start=%d, stop==%d, Performance time=%d us, Time clk %d us\n", (uint32_t)program_timer.perf_start.tv_usec, (uint32_t)program_timer.perf_stop.tv_usec, perf_diff
           ,usec);

 

 

 

0 Kudos
6 Replies
Scholar hbucher
Scholar
1,211 Views
Registered: ‎03-22-2016

Re: performance timer in linux?

@s002wjhw 

So the short answer to your question is - gettimeofday() is the right solution for your purpose.

Long answer - they are radically different. clock() measures "process time", ie the amount of time the process itself netted running plus its children. It is based in kernel's "rusage" - or process book keeping. I believe the result is given in seconds, which is not really the most precise unit to measure time.

gettimeofday() is wall clock. It is usually a VDSO (ie userspace) call that (usually) does not require a kernel switch, which would make the call return very fast, thus making it more precise. However gettimeofday() can be affected by time shifts that occur from time keeping (ie NTP/PTP), especially if there is a time warp (ie when the difference between network time and local time is so big, the OS will not even attempt to adjust the clock smoothly). So from time to time you can get a completely wrong measure. 

The best way to compute time is reading the clock ticks from the CPU. On standalone you can use Xtime_GetTime() which will encapsulate these calls for you. On Linux, you have to do it in assembly - but you have to enable the PMU with a custom kernel module, which is a more involved solution.

 

vitorian.com --- We do this for fun. Always give kudos. Accept as solution if your question was answered.
I will not answer to personal messages - use the forums instead.
Voyager
Voyager
1,200 Views
Registered: ‎06-26-2015

Re: performance timer in linux?

thx, though I read online some recommend using clock() because its the CPU clk?  

as for gettimeofday()  I guess occasionally there is wrap around time which give wrong result?

0 Kudos
Scholar hbucher
Scholar
1,180 Views
Registered: ‎03-22-2016

Re: performance timer in linux?

@s002wjhw 

clock() wraps around every 72 minutes on a system where CLOCKS_PER_SEC is defined as 1,000,000 (most Linux)

gettimeofday() wraps around every 68 years - next in 2038.

That said, none are suitable to be used as a "performance" clock. You should really use processor clock ticks (ARM) or an AXI Timer.

 

vitorian.com --- We do this for fun. Always give kudos. Accept as solution if your question was answered.
I will not answer to personal messages - use the forums instead.
0 Kudos
Voyager
Voyager
1,160 Views
Registered: ‎06-26-2015

Re: performance timer in linux?

k thanks is there an example on using ARM processor ticks or guide?? don't planning add an axi timmer in fpga right now
0 Kudos
Scholar hbucher
Scholar
1,156 Views
Registered: ‎03-22-2016

Re: performance timer in linux?

@s002wjhw Use XTime_GetTime()

You have to include "xtime_l.h"

 

On Linux this module works - the code attached is for the Beaglebone but it is pretty much the same processor

 

vitorian.com --- We do this for fun. Always give kudos. Accept as solution if your question was answered.
I will not answer to personal messages - use the forums instead.
Visitor wicky_waka
Visitor
20 Views
Registered: ‎04-15-2019

Re: performance timer in linux?

Hello @hbucher,
Thanks for the code as it save me a lot of time.
Did you write this code or if you can tell me about the source of this code so that I can add an acknowledgment for it in my thesis report.
0 Kudos