summaryrefslogtreecommitdiffstats
path: root/usr.sbin/afs/src/lwp/fasttime.c
blob: 4cefa1eb4e0c44e9c5355e6f8bd1b8ccad805839 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
****************************************************************************
*        Copyright IBM Corporation 1988, 1989 - All Rights Reserved        *
*                                                                          *
* Permission to use, copy, modify, and distribute this software and its    *
* documentation for any purpose and without fee is hereby granted,         *
* provided that the above copyright notice appear in all copies and        *
* that both that copyright notice and this permission notice appear in     *
* supporting documentation, and that the name of IBM not be used in        *
* advertising or publicity pertaining to distribution of the software      *
* without specific, written prior permission.                              *
*                                                                          *
* IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL IBM *
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY      *
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER  *
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING   *
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.    *
****************************************************************************
*/
/*
 * fasttime.c -- Get the time of day quickly by mapping the kernel's
 *	         time of day variable.
 *
 * David Nichols
 *  6 January 1986
 *
 * Modification History
 * 3/21/86:  Added FT_ApproxTime which returns the last time
 *           in seconds returned by RT_FastTime.  The intent is to give
 *           routines which aren't too concerned about the exact time
 *           fast access to the time, even on kernels without mmap.
 *           - Bob Sidebotham.
 * 4/2/86:   Fixed my previous mod and fixed FT_Init so it doesn't initialize
 *	     a second time if explicitly called after being implicitly called.
 *	     This saves a (precious) file descriptor.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
RCSID("$arla: fasttime.c,v 1.10 2002/12/20 12:54:55 lha Exp $");
#endif

#include <stdio.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/file.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif

#ifdef HAVE_NLIST_H
#include <nlist.h>
#else
#ifdef HAVE_LIBELF_NLIST_H
#include <libelf/nlist.h>
#endif
#endif 

#include "timer.h"

#define TRUE	1
#define FALSE	0

static enum InitState {
    notTried, tried, done
} initState = notTried;

/* last time returned by RT_FastTime. Used to implement FT_ApproxTime */
struct timeval FT_LastTime;


/*
 * Call this to get the memory mapped.  It will return -1 if anything went
 * wrong.  In that case, calls to FT_GetTimeOfDay will call gettimeofday
 * instead.  If printErrors is true, errors in initialization will cause
 * error messages to be printed on stderr.  If notReally is true, then
 * things are set up so that all calls to FT_GetTimeOfDay call gettimeofday.
 * You might want this if your program won't run too long and the nlist
 * call is too expensive.  Yeah, it's pretty horrible.
 */
int
FT_Init(int printErrors, int notReally)
{

    /*
     * This is in case explicit initialization occurs after automatic
     * initialization
     */
    if (initState != notTried && !notReally)
	return (initState == done ? 0 : -1);

    initState = tried;
    if (notReally)
	return 0;		       /* fake success, but leave initState
				        * wrong. */
    return (-1);
}

/*
 * Call this to get the time of day.  It will automatically initialize the
 * first time you call it.  If you want error messages when you initialize,
 * call FT_Init yourself.  If the initialization failed, this will just
 * call gettimeofday.  If you ask for the timezone info, this routine will
 * punt to gettimeofday.
 */
int
FT_GetTimeOfDay(struct timeval * tv, struct timezone * tz)
{
    int ret;

    ret = gettimeofday(tv, tz);
    if (!ret) {

	/*
	 * need to bounds check 'cause Unix can fail these checks, (esp on
	 * Suns) and time package can generate invalid (to select syscall)
	 * values for the time until the next interesting event if it
	 * encounters out of range microsecond fields
	 */
	if (tv->tv_usec < 0)
	    tv->tv_usec = 0;
	if (tv->tv_usec > 999999)
	    tv->tv_usec = 999999;
	FT_LastTime.tv_sec = tv->tv_sec;
	FT_LastTime.tv_usec = tv->tv_usec;
    }
    return ret;
}


/* For compatibility.  Should go away. */
int
TM_GetTimeOfDay(struct timeval * tv, struct timezone * tz)
{
    return FT_GetTimeOfDay(tv, tz);
}

int
FT_AGetTimeOfDay(struct timeval * tv, struct timezone * tz)
{
    if (FT_LastTime.tv_sec) {
	tv->tv_sec = FT_LastTime.tv_sec;
	tv->tv_usec = FT_LastTime.tv_usec;
	return 0;
    }
    return FT_GetTimeOfDay(tv, tz);
}

unsigned int
FT_ApproxTime(void)
{
    if (!FT_LastTime.tv_sec) {
	FT_GetTimeOfDay(&FT_LastTime, 0);
    }
    return FT_LastTime.tv_sec;
}