Re: high accuracy timing options?
site_archiver@lists.apple.com Delivered-To: darwin-dev@lists.apple.com 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(). I saw Steve's MPDelayUntil() comment, but it makes no indication what the method is, and it seems unlikely that it would be any better then this. I'm guessing it's just a wrapper around nanosleep(). The example I pointed you to: <http://developer.apple.com/documentation/Carbon/Conceptual/Multitasking_MultiproServ/appendixb/chapter_5_section_1.html> Note this code is written in Eudora, not tested.
From the example I gave you (to convert back to seconds): static float HowLong( AbsoluteTime endTime, AbsoluteTime bgnTime ) { AbsoluteTime absTime; Nanoseconds nanosec;
absTime = SubAbsoluteFromAbsolute(endTime, bgnTime); nanosec = AbsoluteToNanoseconds(absTime); return (float) UnsignedWideToUInt64( nanosec ) / 1000.0; } u_int32_t count = 0, fail = 0, back = 0, i; struct timeval sleep_until, now, last; // MPDelayUntil(&sleep_until); // would just until 5 sec from start last = UpTime(); do { count++; now = UpTime(); if (now == last) fail ++; else if (SubAbsoluteFromAbsolute(now, endTime) == 0) back ++; last = now; } 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); } Note that AbsoluteTime may be declared as a struct, but you can cast to UInt64. What are you really trying to do? -Steve _______________________________________________ Do not post admin requests to the list. They will be ignored. Darwin-dev mailing list (Darwin-dev@lists.apple.com) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/darwin-dev/site_archiver%40lists.appl... At 11:23 AM -0700 3/27/08, Aaron Turner wrote: Huh? I posted links to the documentation and sample code, which I assumed you could read. You calculate a time in the future in AbsoluteTime units and call MPDelayUntil(), which delays until that time is past. You would be wrong. It's a high-performance, high-accuracy call from the original Mac OS Driver Services library that was moved up to Multiprocessor Sevices library. It does not rely on converting unix time units to processor time units. does exactly what what you want. Note that SubAbsoluteFromAbsolute() returns 0, not a negative number if the time has passed. int main(int argc, char *argv[]) { AbsoluteTime fiveSeconds = DurationToAbsolute(5000 * kDurationMillisecond); AbsoluteTime now = UpTime(); AbsoluteTime last = now; AbsoluteTime sleep_until = now; for (i = 0; i < 5; i ++) { sleep_until = AddAbsoluteToAbsolute(UpTime(), fiveSeconds); // what you wrote // sleep_until = AddAbsoluteToAbsolute(sleep_until, fiveSeconds); // this won't drift This email sent to site_archiver@lists.apple.com
participants (1)
-
Steve Sisak