2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* Elapsed time package */
11 /* See rx_clock.h for calling conventions */
13 #include <afsconfig.h>
15 #include "afs/param.h"
17 #include <afs/param.h>
21 #include <sys/time_impl.h>
27 #include "rx/rx_clock.h"
31 #include "afs/sysincludes.h"
32 #include "afsincludes.h"
34 #include "rx/rx_clock.h"
48 #if !defined(AFS_USE_GETTIMEOFDAY)
49 /*use this package only if gettimeofday is much much costlier than getitime */
53 #define STARTVALUE 3600
54 static struct clock startvalue;
55 static struct clock relclock_epoch; /* The elapsed time of the last itimer reset */
57 struct clock clock_now; /* The last elapsed time ready by clock_GetTimer */
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;
62 int clock_nUpdates; /* The actual number of clock updates */
63 static int clockInitialized = 0;
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;
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",
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;
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;
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
96 getitimer(ITIMER_REAL, &itimer);
97 startvalue.sec = itimer.it_value.tv_sec;
98 startvalue.usec = itimer.it_value.tv_usec;
101 /* Initialize the clock */
105 if (!clockInitialized) {
106 relclock_epoch.sec = relclock_epoch.usec = 0;
107 startvalue.sec = startvalue.usec = 0;
109 clockInitialized = 1;
115 /* Make clock uninitialized. */
119 clockInitialized = 0;
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 */
125 clock_UpdateTime(void)
127 struct itimerval itimer;
131 getitimer(ITIMER_REAL, &itimer);
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;
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;
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)
149 clock_haveCurrentTime = 1;
155 #endif /* AFS_USE_GETTIMEOFDAY */