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