Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: high accuracy timing options?




On Mar 27, 2008, at 12:04 PM, email@hidden wrote:

So I decided to do some benchmarking:

---- code ----

int
main(int argc, char *argv[])
{
   u_int32_t count = 0, fail = 0, back = 0, i;
   struct timeval sleep_until, now, last;

for (i = 0; i < 5; i ++) {
gettimeofday(&sleep_until, NULL);
sleep_until.tv_sec += 5;
gettimeofday(&last, NULL);
do {
count++;
gettimeofday(&now, NULL);
if ((now.tv_sec == last.tv_sec) && (now.tv_usec == last.tv_usec))
fail ++;
else if (timercmp(&last, &now, >))
back ++;
last.tv_sec = now.tv_sec;
last.tv_usec = now.tv_usec;
} while (timercmp(&now, &sleep_until, <));


       printf("Ran gtod() %lu times in 5 seconds.\n", count);
       printf("%f gtod()/usec\n", (float)count / 5000 / 1000);
       printf("Failed to increment %lu times\n", fail);
       printf("Back in time %lu times\n", back);
       printf("-------------\n");
       count = 0;
       fail = 0;
       back = 0;
   }
   exit(0);
}



---- code ----

gcc -O3 -o gtod gtod.c

Very interesting results:

Ran gtod() 68160730 times in 5 seconds.
13.632146 gtod()/usec
Failed to increment 63197136 times
Back in time 0 times

[...etc...]

Interesting how gettimeofday() fails to increment around 90% of the
time.  I guess technically incrementing 1/10 times while requiring
~13 times to measure a usec is good enough, but it's not giving me a
warm & fuzzy feeling.

I don't exactly know what you expect here.

% expr 68160730 - 63197136
4963594

Of the 5 million unique microsecond values to be experienced in your five second test, you saw all but about 36,000 (0.07%) of them.

Or are you dissatisfied that an iteration of your loop takes nearly 80ns to run?

May just be one of those things where a RTOS is necessary to get any improvement.

In what direction are you looking for "improvement"? It's a common mistake to think that RTOS' have better resolution; typically what you get from an RTOS is determinism, often achieved at the cost of peak performance.


 Anyone know if OS X/Darwin has the equivalent of Linux's
RT scheduler extensions?

Darwin has a fairly modest but effective set of RT-like scheduling capabilities. What is it exactly that you are trying to achieve? It's very hard to get any sort of "RT" in a system where you have any sort of competition, and a desktop Darwin system is very competitive.


Anyways, I'm going to try to dig around some more and try to find the
HPET timer API.  Theoretically, that should be more accurate then what
I'm seeing with gtod().

What do you mean by "accurate"? Your test above has demonstrated that gettimeofday is returning unique microsecond values; it has no greater resolution. If you were truly pedantic you could add roughly half another significant figure (on your test system) by counting the number of iterations since you last saw gettimeofday() wrap, but even then the question around that sort of timing resolution is - what do you expect it to tell you? Doing any sort of real work takes orders of magnitude longer than that...


If you think that the HPET will help you, I'm sorry; it's too slow, it's too far away (the connection is highly latent, you can be held off talking to it for milliseconds in pathalogical cases), and the Darwin kernel owns it entirely.

If I understood what you were actually trying to do, I might have some better ideas...

 = Mike

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Darwin-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/darwin-dev/email@hidden

This email sent to email@hidden


Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.