openafs-kill-dead-code-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 #include <stdio.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include "rx.h"
44 #include "rx_clock.h"
45 #endif
46
47 #if !defined(AFS_USE_GETTIMEOFDAY)
48 /*use this package only if gettimeofday is much much costlier than getitime */
49
50 #ifndef KERNEL
51
52 #define STARTVALUE 100000000    /* Max number of seconds setitimer allows, for some reason */
53 static int startvalue = STARTVALUE;
54
55 struct clock clock_now;         /* The last elapsed time ready by clock_GetTimer */
56
57 /* 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) */
58 int clock_haveCurrentTime;
59
60 int clock_nUpdates;             /* The actual number of clock updates */
61 static int clockInitialized = 0;
62
63 /* Initialize the clock */
64 void
65 clock_Init(void)
66 {
67     struct itimerval itimer, otimer;
68
69     if (!clockInitialized) {
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         if (setitimer(ITIMER_REAL, &itimer, &otimer) != 0) {
76             fprintf(stderr, "clock:  could not set interval timer; \
77                                 aborted(errno=%d)\n", errno);
78             fflush(stderr);
79             exit(1);
80         }
81         getitimer(ITIMER_REAL, &itimer);
82         startvalue = itimer.it_value.tv_sec;
83         if (itimer.it_value.tv_usec > 0)
84           startvalue++;
85         clockInitialized = 1;
86     }
87
88     clock_UpdateTime();
89 }
90
91 /* Make clock uninitialized. */
92 int
93 clock_UnInit(void)
94 {
95     clockInitialized = 0;
96     return 0;
97 }
98
99 /* 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 */
100 void
101 clock_UpdateTime(void)
102 {
103     struct itimerval itimer;
104     getitimer(ITIMER_REAL, &itimer);
105     clock_now.sec = startvalue - 1 - itimer.it_value.tv_sec;    /* The "-1" makes up for adding 1000000 usec, on the next line */
106     clock_now.usec = 1000000 - itimer.it_value.tv_usec;
107     if (clock_now.usec == 1000000)
108         clock_now.usec = 0, clock_now.sec++;
109     clock_haveCurrentTime = 1;
110     clock_nUpdates++;
111 }
112 #else /* KERNEL */
113 #endif /* KERNEL */
114
115 #endif /* AFS_USE_GETTIMEOFDAY */