death to trailing whitespace
[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
25 #ifdef KERNEL
26 #ifndef UKERNEL
27 #include "rx/rx_clock.h"
28 #include "h/types.h"
29 #include "h/time.h"
30 #else /* !UKERNEL */
31 #include "afs/sysincludes.h"
32 #include "afsincludes.h"
33 #include "rx/rx.h"
34 #include "rx/rx_clock.h"
35 #endif /* !UKERNEL */
36 #else /* KERNEL */
37 #include <sys/time.h>
38 #ifdef HAVE_SIGNAL_H
39 #include <signal.h>
40 #endif
41 #include <stdio.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <assert.h>
45 #include "rx.h"
46 #include "rx_clock.h"
47 #endif
48
49 #if !defined(AFS_USE_GETTIMEOFDAY)
50 /*use this package only if gettimeofday is much much costlier than getitime */
51
52 #ifndef KERNEL
53
54 #define STARTVALUE 3600
55 static struct clock startvalue;
56 static struct clock relclock_epoch;   /* The elapsed time of the last itimer reset */
57
58 struct clock clock_now;         /* The last elapsed time ready by clock_GetTimer */
59
60 /* 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) */
61 int clock_haveCurrentTime;
62
63 int clock_nUpdates;             /* The actual number of clock updates */
64 static int clockInitialized = 0;
65
66 static void
67 clock_Sync(void)
68 {
69     struct itimerval itimer, otimer;
70     itimer.it_value.tv_sec = STARTVALUE;
71     itimer.it_value.tv_usec = 0;
72     itimer.it_interval.tv_sec = 0;
73     itimer.it_interval.tv_usec = 0;
74
75     signal(SIGALRM, SIG_IGN);
76     if (setitimer(ITIMER_REAL, &itimer, &otimer) != 0) {
77         fprintf(stderr, "clock:  could not set interval timer; \
78                                 aborted(errno=%d)\n", errno);
79         fflush(stderr);
80         assert(0);
81     }
82     if (relclock_epoch.usec + startvalue.usec >= otimer.it_value.tv_usec) {
83         relclock_epoch.sec = relclock_epoch.sec +
84             startvalue.sec - otimer.it_value.tv_sec;
85         relclock_epoch.usec = relclock_epoch.usec +
86             startvalue.usec - otimer.it_value.tv_usec;
87     } else {
88         relclock_epoch.sec = relclock_epoch.sec +
89             startvalue.sec - 1 - otimer.it_value.tv_sec;
90         relclock_epoch.usec = relclock_epoch.usec +
91             startvalue.usec + 1000000 - otimer.it_value.tv_usec;
92     }
93     if (relclock_epoch.usec >= 1000000)
94         relclock_epoch.usec -= 1000000, relclock_epoch.sec++;
95     /* the initial value of the interval timer may not be exactly the same
96      * as the arg passed to setitimer. POSIX allows the implementation to
97      * round it up slightly, and some nonconformant implementations truncate
98      * it */
99     getitimer(ITIMER_REAL, &itimer);
100     startvalue.sec = itimer.it_value.tv_sec;
101     startvalue.usec = itimer.it_value.tv_usec;
102 }
103
104 /* Initialize the clock */
105 void
106 clock_Init(void)
107 {
108     if (!clockInitialized) {
109         relclock_epoch.sec = relclock_epoch.usec = 0;
110         startvalue.sec = startvalue.usec = 0;
111         clock_Sync();
112         clockInitialized = 1;
113     }
114
115     clock_UpdateTime();
116 }
117
118 /* Make clock uninitialized. */
119 int
120 clock_UnInit(void)
121 {
122     clockInitialized = 0;
123     return 0;
124 }
125
126 /* 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 */
127 void
128 clock_UpdateTime(void)
129 {
130     struct itimerval itimer;
131     struct clock offset;
132     struct clock new;
133
134     getitimer(ITIMER_REAL, &itimer);
135
136     if (startvalue.usec >= itimer.it_value.tv_usec) {
137         offset.sec = startvalue.sec - itimer.it_value.tv_sec;
138         offset.usec = startvalue.usec - itimer.it_value.tv_usec;
139     } else {
140         /* The "-1" makes up for adding 1000000 usec, on the next line */
141         offset.sec = startvalue.sec - 1 - itimer.it_value.tv_sec;
142         offset.usec = startvalue.usec + 1000000 - itimer.it_value.tv_usec;
143     }
144     new.sec = relclock_epoch.sec + offset.sec;
145     new.usec = relclock_epoch.usec + offset.usec;
146     if (new.usec >= 1000000)
147         new.usec -= 1000000, new.sec++;
148     clock_now.sec = new.sec;
149     clock_now.usec = new.usec;
150     if (itimer.it_value.tv_sec < startvalue.sec / 2)
151         clock_Sync();
152     clock_haveCurrentTime = 1;
153     clock_nUpdates++;
154 }
155 #else /* KERNEL */
156 #endif /* KERNEL */
157
158 #endif /* AFS_USE_GETTIMEOFDAY */