(no commit message)
[ikiwiki.git] / presentations / nanosleep / wakeup_latency.c
1 /*
2  * Copyright (c) 2003 Paul Herman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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.
14  * 
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
25  * SUCH DAMAGE.
26  */
27
28 /*
29  * $DragonFly: site/data/docs/nanosleep/wakeup_latency.c,v 1.1 2004/01/22 21:55:58 justin Exp $
30  */
31
32 #include <sys/time.h>
33 #include <sys/resource.h>
34 #include <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38
39 #define ONE_SECOND      1000000L
40
41 int count = 200;
42 int debug = 0;
43
44 int main (int ac, char **av) {
45         long s;
46         double diff;
47         struct timeval tv1, tv2;
48
49         if (ac > 1 && av[1])
50                 count = strtol(av[1], NULL, 10);
51
52         while(count--) {
53                 gettimeofday(&tv1, NULL);
54                         /*
55                          * Calculate the number of microseconds to sleep so we
56                          * can wakeup right when the second hand hits zero.
57                          *
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.
62                          */
63                 s = ONE_SECOND - tv1.tv_usec;
64                 usleep(s);
65                 gettimeofday(&tv2, NULL);
66
67                 diff = (double)(tv2.tv_usec - (tv1.tv_usec + s))/1e6;
68                 diff += (double)(tv2.tv_sec - tv1.tv_sec);
69                 if (debug)
70                         printf("(%ld.%.6ld) ", tv2.tv_sec, tv2.tv_usec);
71                 printf("%.6f\n", diff);
72         }
73         return 0;
74 }