2 ****************************************************************************
3 * Copyright IBM Corporation 1988, 1989 - All Rights Reserved *
5 * Permission to use, copy, modify, and distribute this software and its *
6 * documentation for any purpose and without fee is hereby granted, *
7 * provided that the above copyright notice appear in all copies and *
8 * that both that copyright notice and this permission notice appear in *
9 * supporting documentation, and that the name of IBM not be used in *
10 * advertising or publicity pertaining to distribution of the software *
11 * without specific, written prior permission. *
13 * IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
14 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
15 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY *
16 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER *
17 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING *
18 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. *
19 ****************************************************************************
22 * fasttime.c -- Get the time of day quickly by mapping the kernel's
23 * time of day variable.
28 * Modification History
29 * 3/21/86: Added FT_ApproxTime which returns the last time
30 * in seconds returned by RT_FastTime. The intent is to give
31 * routines which aren't too concerned about the exact time
32 * fast access to the time, even on kernels without mmap.
34 * 4/2/86: Fixed my previous mod and fixed FT_Init so it doesn't initialize
35 * a second time if explicitly called after being implicitly called.
36 * This saves a (precious) file descriptor.
45 #include <sys/types.h>
48 #ifdef HAVE_SYS_MMAN_H
55 #ifdef HAVE_LIBELF_NLIST_H
56 #include <libelf/nlist.h>
65 static enum InitState
{
67 } initState
= notTried
;
69 /* last time returned by RT_FastTime. Used to implement FT_ApproxTime */
70 struct timeval FT_LastTime
;
74 * Call this to get the memory mapped. It will return -1 if anything went
75 * wrong. In that case, calls to FT_GetTimeOfDay will call gettimeofday
76 * instead. If printErrors is true, errors in initialization will cause
77 * error messages to be printed on stderr. If notReally is true, then
78 * things are set up so that all calls to FT_GetTimeOfDay call gettimeofday.
79 * You might want this if your program won't run too long and the nlist
80 * call is too expensive. Yeah, it's pretty horrible.
83 FT_Init(int printErrors
, int notReally
)
87 * This is in case explicit initialization occurs after automatic
90 if (initState
!= notTried
&& !notReally
)
91 return (initState
== done
? 0 : -1);
95 return 0; /* fake success, but leave initState
101 * Call this to get the time of day. It will automatically initialize the
102 * first time you call it. If you want error messages when you initialize,
103 * call FT_Init yourself. If the initialization failed, this will just
104 * call gettimeofday. If you ask for the timezone info, this routine will
105 * punt to gettimeofday.
108 FT_GetTimeOfDay(struct timeval
* tv
, struct timezone
* tz
)
112 ret
= gettimeofday(tv
, tz
);
116 * need to bounds check 'cause Unix can fail these checks, (esp on
117 * Suns) and time package can generate invalid (to select syscall)
118 * values for the time until the next interesting event if it
119 * encounters out of range microsecond fields
123 if (tv
->tv_usec
> 999999)
124 tv
->tv_usec
= 999999;
125 FT_LastTime
.tv_sec
= tv
->tv_sec
;
126 FT_LastTime
.tv_usec
= tv
->tv_usec
;
132 /* For compatibility. Should go away. */
134 TM_GetTimeOfDay(struct timeval
* tv
, struct timezone
* tz
)
136 return FT_GetTimeOfDay(tv
, tz
);
140 FT_AGetTimeOfDay(struct timeval
* tv
, struct timezone
* tz
)
142 if (FT_LastTime
.tv_sec
) {
143 tv
->tv_sec
= FT_LastTime
.tv_sec
;
144 tv
->tv_usec
= FT_LastTime
.tv_usec
;
147 return FT_GetTimeOfDay(tv
, tz
);
153 if (!FT_LastTime
.tv_sec
) {
154 FT_GetTimeOfDay(&FT_LastTime
, 0);
156 return FT_LastTime
.tv_sec
;