OpenWrt Forum Archive

Topic: How to get time of Microseconds accuracy?

The content of this topic has been archived on 5 May 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Hi,all
I want to test how long my function (in kernal driver) would take . Since jiffies' accuracy is 10ms and it is not enough for me.
I have tried gettimeofday() , clock(). But the result seems unnomal and absolutely wrong!
I am running Kamikaze 8.09 on alix 3d2 . Any body know how to do it? Thanks!

(Last edited by huasion on 30 Sep 2009, 03:48)

You may need to write your own high precision timer.

On x86 architecture(which is what your geode is) you can use rdtsc in assembler which returns the number of clock cycles executed since system start. That in conjunction with the clock speed of your chip should give you the necessary tools to write a high precision timer yourself.

Most routers don't have an RTC. This is mainly your problem.

RaX wrote:

You may need to write your own high precision timer.

On x86 architecture(which is what your geode is) you can use rdtsc in assembler which returns the number of clock cycles executed since system start. That in conjunction with the clock speed of your chip should give you the necessary tools to write a high precision timer yourself.

Thanks! The result seems workable though it is not of  microsceonds accuracy. I think it returns reliable millisecond . What makes me supprise is that why there is no full accuracy since it is from clock ticks. Is that the sysytem hardware unstable ? I just cannot guess why.

(Last edited by huasion on 9 Oct 2009, 12:41)

Dogge wrote:

Most routers don't have an RTC. This is mainly your problem.

Hi?I have checked my router, it is said to have RTC according to instrument(http://www.pcengines.ch/alix3d2.htm)

And what makes the difference?

The RTC is battery backed and provides a stable frequency that is decoupled from the CPU iirc. The RTC is able to generate interrupts which can be used by software to obtain exact time measurements.

~ JoW

jow wrote:

The RTC is battery backed and provides a stable frequency that is decoupled from the CPU iirc. The RTC is able to generate interrupts which can be used by software to obtain exact time measurements.

~ JoW

Thanks. My router doesnt have a battery. Does that means it doesnt have RTC support, Which  is not as the instrument say?

I have checked my router, the reason is that I need to add one battery  on it(as well as a battery box as there is only two soldering point  ). That's pretty funny!

(Last edited by huasion on 9 Oct 2009, 12:48)

Hello huasion
You need to use the high resolution performance counter that is available in the QueryPerformanceCounter() function.  It is able to provide times that exceed 1 microsecond accuracy on most hardware. The availability of the QueryPerformanceCounter() is dependent.on the system's hardware.  To find out if it's available, and to find out its accuracy, use the QueryPerformanceFrequency()  function.

The QueryPerformanceCounter() function returns the amount of time that has elapsed since the system was booted.  Elapsed time can be found by comparing the returned values at two different points in time.
Thanks

michelbrown wrote:

Hello huasion
You need to use the high resolution performance counter that is available in the QueryPerformanceCounter() function.  It is able to provide times that exceed 1 microsecond accuracy on most hardware. The availability of the QueryPerformanceCounter() is dependent.on the system's hardware.  To find out if it's available, and to find out its accuracy, use the QueryPerformanceFrequency()  function.

The QueryPerformanceCounter() function returns the amount of time that has elapsed since the system was booted.  Elapsed time can be found by comparing the returned values at two different points in time.
Thanks

Hi michelbrown,
The function QueryPerformanceCounter() is supported by win32 but not linux.

huasion wrote:
RaX wrote:

You may need to write your own high precision timer.

On x86 architecture(which is what your geode is) you can use rdtsc in assembler which returns the number of clock cycles executed since system start. That in conjunction with the clock speed of your chip should give you the necessary tools to write a high precision timer yourself.

Thanks! The result seems workable though it is not of  microsceonds accuracy. I think it returns reliable millisecond . What makes me supprise is that why there is no full accuracy since it is from clock ticks. Is that the sysytem hardware unstable ? I just cannot guess why.

It returns the number of clock cycles executed since system start which, in conjunction with the speed of your processor, can be used to create a timer that is as precise as your processor is... which, of course, is the only way to time things on the system like you are trying to do. You can't outperform your own hardware =P

IE:: on a 600mhz processor you could time things in excess of 600million ticks per second max

And, in reply to the person that said RDTSC can't be used on routers...If I'm not mistaken the only real architectures used in networking hardware are mips, arm, powerpc, and x86 all of which have rdtsc or a compatible replacement function. I realize there are some less mainstream architectures that are used...but they also probably have a workaround to get rdtsc working, you have to have a way to time things in systems after all smile

The discussion might have continued from here.