rx-clock-rollover-fix-20050403
[openafs.git] / src / rx / rx_clock.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /* Elapsed time package */
11 /* See rx_clock.h for calling conventions */
12
13 #include <afsconfig.h>
14 #ifdef  KERNEL
15 #include "afs/param.h"
16 #else
17 #include <afs/param.h>
18 #endif
19
20 #ifdef AFS_SUN59_ENV
21 #include <sys/time_impl.h>
22 #endif
23
24 RCSID
25     ("$Header$");
26
27 #ifdef KERNEL
28 #ifndef UKERNEL
29 #include "rx/rx_clock.h"
30 #include "h/types.h"
31 #include "h/time.h"
32 #else /* !UKERNEL */
33 #include "afs/sysincludes.h"
34 #include "afsincludes.h"
35 #include "rx/rx.h"
36 #include "rx/rx_clock.h"
37 #endif /* !UKERNEL */
38 #else /* KERNEL */
39 #include <sys/time.h>
40 #ifdef HAVE_SIGNAL_H
41 #include <signal.h>
42 #endif
43 #include <stdio.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include "rx.h"
47 #include "rx_clock.h"
48 #endif
49
50 #if !defined(AFS_USE_GETTIMEOFDAY)
51 /*use this package only if gettimeofday is much much costlier than getitime */
52
53 #ifndef KERNEL
54
55 #define STARTVALUE 3600
56 static struct clock startvalue;
57 static struct clock relclock_epoch;   /* The elapsed time of the last itimer reset */
58
59 struct clock clock_now;         /* The last elapsed time ready by clock_GetTimer */
60
61 /* This is set to 1 whenever the time is read, and reset to 0 whenever clock_NewTime is called.  This is to allow the caller to control the frequency with which the actual time is re-evaluated (an expensive operation) */
62 int clock_haveCurrentTime;
63
64 int clock_nUpdates;             /* The actual number of clock updates */
65 static int clockInitialized = 0;
66
67 static void
68 clock_Sync(void)
69 {
70     struct itimerval itimer, otimer;
71     itimer.it_value.tv_sec = STARTVALUE;
72     itimer.it_value.tv_usec = 0;
73     itimer.it_interval.tv_sec = 0;
74     itimer.it_interval.tv_usec = 0;
75
76     signal(SIGALRM, SIG_IGN);
77     if (setitimer(ITIMER_REAL, &itimer, &otimer) != 0) {
78         fprintf(stderr, "clock:  could not set interval timer; \
79                                 aborted(errno=%d)\n", errno);
80         fflush(stderr);
81         exit(1);
82     }
83     if (relclock_epoch.usec + startvalue.usec >= otimer.it_value.tv_usec) {
84         relclock_epoch.sec = relclock_epoch.sec +
85             startvalue.sec - otimer.it_value.tv_sec;
86         relclock_epoch.usec = relclock_epoch.usec +
87             startvalue.usec - otimer.it_value.tv_usec;
88     } else {
89         relclock_epoch.sec = relclock_epoch.sec +
90             startvalue.sec - 1 - otimer.it_value.tv_sec;
91         relclock_epoch.usec = relclock_epoch.usec +
92             startvalue.usec + 1000000 - otimer.it_value.tv_usec;
93     }
94     if (relclock_epoch.usec >= 1000000)
95         relclock_epoch.usec -= 1000000, relclock_epoch.sec++;
96     /* the initial value of the interval timer may not be exactly the same
97      * as the arg passed to setitimer. POSIX allows the implementation to
98      * round it up slightly, and some nonconformant implementations truncate
99      * it */
100     getitimer(ITIMER_REAL, &itimer);
101     startvalue.sec = itimer.it_value.tv_sec;
102     startvalue.usec = itimer.it_value.tv_usec;
103 }
104
105 /* Initialize the clock */
106 void
107 clock_Init(void)
108 {
109     if (!clockInitialized) {
110         relclock_epoch.sec = relclock_epoch.usec = 0;
111         startvalue.sec = startvalue.usec = 0;
112         clock_Sync();
113         clockInitialized = 1;
114     }
115
116     clock_UpdateTime();
117 }
118
119 /* Make clock uninitialized. */
120 int
121 clock_UnInit(void)
122 {
123     clockInitialized = 0;
124     return 0;
125 }
126
127 /* Compute the current time.  The timer gets the current total elapsed time since startup, expressed in seconds and microseconds.  This call is almost 200 usec on an APC RT */
128 void
129 clock_UpdateTime(void)
130 {
131     struct itimerval itimer;
132     struct clock offset;
133     struct clock new;
134
135     getitimer(ITIMER_REAL, &itimer);
136
137     if (startvalue.usec >= itimer.it_value.tv_usec) {
138         offset.sec = startvalue.sec - itimer.it_value.tv_sec;
139         offset.usec = startvalue.usec - itimer.it_value.tv_usec;
140     } else {
141         /* The "-1" makes up for adding 1000000 usec, on the next line */
142         offset.sec = startvalue.sec - 1 - itimer.it_value.tv_sec;
143         offset.usec = startvalue.usec + 1000000 - itimer.it_value.tv_usec;
144     }
145     new.sec = relclock_epoch.sec + offset.sec;
146     new.usec = relclock_epoch.usec + offset.usec;
147     if (new.usec >= 1000000)
148         new.usec -= 1000000, new.sec++;
149     clock_now.sec = new.sec;
150     clock_now.usec = new.usec;
151     if (itimer.it_value.tv_sec < startvalue.sec / 2)
152         clock_Sync();
153     clock_haveCurrentTime = 1;
154     clock_nUpdates++;
155 }
156 #else /* KERNEL */
157 #endif /* KERNEL */
158
159 #endif /* AFS_USE_GETTIMEOFDAY */