2 * Copyright (c) 2003 Paul Herman
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $DragonFly: site/data/docs/nanosleep/wakeup_latency.c,v 1.1 2004/01/22 21:55:58 justin Exp $
33 #include <sys/resource.h>
39 #define ONE_SECOND 1000000L
44 int main (int ac, char **av) {
47 struct timeval tv1, tv2;
50 count = strtol(av[1], NULL, 10);
53 gettimeofday(&tv1, NULL);
55 * Calculate the number of microseconds to sleep so we
56 * can wakeup right when the second hand hits zero.
58 * The latency for the following two statements is minimal.
59 * On a > 1.0GHz machine, the subtraction is done in a few
60 * nanoseconds, and the syscall to usleep/nanosleep is usualy
61 * less than 800 ns or 0.8 us.
63 s = ONE_SECOND - tv1.tv_usec;
65 gettimeofday(&tv2, NULL);
67 diff = (double)(tv2.tv_usec - (tv1.tv_usec + s))/1e6;
68 diff += (double)(tv2.tv_sec - tv1.tv_sec);
70 printf("(%ld.%.6ld) ", tv2.tv_sec, tv2.tv_usec);
71 printf("%.6f\n", diff);