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>
14 #include <afs/param.h>
17 #include <sys/time_impl.h>
25 # include "afs/sysincludes.h"
26 # include "afsincludes.h"
27 # endif /* !UKERNEL */
35 #if !defined(AFS_USE_GETTIMEOFDAY)
36 /*use this package only if gettimeofday is much much costlier than getitime */
40 #define STARTVALUE 3600
41 static struct clock startvalue;
42 static struct clock relclock_epoch; /* The elapsed time of the last itimer reset */
44 struct clock clock_now; /* The last elapsed time ready by clock_GetTimer */
46 /* 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) */
47 int clock_haveCurrentTime;
49 int clock_nUpdates; /* The actual number of clock updates */
50 static int clockInitialized = 0;
55 struct itimerval itimer, otimer;
56 itimer.it_value.tv_sec = STARTVALUE;
57 itimer.it_value.tv_usec = 0;
58 itimer.it_interval.tv_sec = 0;
59 itimer.it_interval.tv_usec = 0;
61 signal(SIGALRM, SIG_IGN);
62 if (setitimer(ITIMER_REAL, &itimer, &otimer) != 0) {
63 osi_Panic("clock: could not set interval timer; aborted(errno=%d)\n",
66 if (relclock_epoch.usec + startvalue.usec >= otimer.it_value.tv_usec) {
67 relclock_epoch.sec = relclock_epoch.sec +
68 startvalue.sec - otimer.it_value.tv_sec;
69 relclock_epoch.usec = relclock_epoch.usec +
70 startvalue.usec - otimer.it_value.tv_usec;
72 relclock_epoch.sec = relclock_epoch.sec +
73 startvalue.sec - 1 - otimer.it_value.tv_sec;
74 relclock_epoch.usec = relclock_epoch.usec +
75 startvalue.usec + 1000000 - otimer.it_value.tv_usec;
77 if (relclock_epoch.usec >= 1000000)
78 relclock_epoch.usec -= 1000000, relclock_epoch.sec++;
79 /* the initial value of the interval timer may not be exactly the same
80 * as the arg passed to setitimer. POSIX allows the implementation to
81 * round it up slightly, and some nonconformant implementations truncate
83 getitimer(ITIMER_REAL, &itimer);
84 startvalue.sec = itimer.it_value.tv_sec;
85 startvalue.usec = itimer.it_value.tv_usec;
88 /* Initialize the clock */
92 if (!clockInitialized) {
93 relclock_epoch.sec = relclock_epoch.usec = 0;
94 startvalue.sec = startvalue.usec = 0;
102 /* Make clock uninitialized. */
106 clockInitialized = 0;
110 /* 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 */
112 clock_UpdateTime(void)
114 struct itimerval itimer;
118 getitimer(ITIMER_REAL, &itimer);
120 if (startvalue.usec >= itimer.it_value.tv_usec) {
121 offset.sec = startvalue.sec - itimer.it_value.tv_sec;
122 offset.usec = startvalue.usec - itimer.it_value.tv_usec;
124 /* The "-1" makes up for adding 1000000 usec, on the next line */
125 offset.sec = startvalue.sec - 1 - itimer.it_value.tv_sec;
126 offset.usec = startvalue.usec + 1000000 - itimer.it_value.tv_usec;
128 new.sec = relclock_epoch.sec + offset.sec;
129 new.usec = relclock_epoch.usec + offset.usec;
130 if (new.usec >= 1000000)
131 new.usec -= 1000000, new.sec++;
132 clock_now.sec = new.sec;
133 clock_now.usec = new.usec;
134 if (itimer.it_value.tv_sec < startvalue.sec / 2)
136 clock_haveCurrentTime = 1;
142 #endif /* AFS_USE_GETTIMEOFDAY */