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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/* Quagga realtime and monotonic clock handling -- header
* Copyright (C) 2009 Chris Hall (GMCH), Highwayman
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2, or (at your
* option) any later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Zebra; see the file COPYING. If not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef _ZEBRA_QTIME_H
#define _ZEBRA_QTIME_H
#include "misc.h"
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include "zassert.h"
#include "config.h"
/*==============================================================================
* qtime_t -- signed 64-bit integer.
*
* The various time functions work in terms of the structures:
*
* timespec -- tv_secs seconds
* tv_nsecs nano-seconds
*
* timeval -- tv_secs seconds
* tv_usecs micro-seconds
*
* Given a 64-bit integer it is much easier to do operations on a 64 bit
* (signed) nano-second value. That gives > 34 bits for the seconds count,
* and counts from zero to > 290 years.
*/
typedef int64_t qtime_t ;
typedef qtime_t qtime_real_t ; /* qtime_t value, realtime time-base */
typedef qtime_t qtime_mono_t ; /* qtime_t value, monotonic time-base */
typedef qtime_t qtime_tod_t ; /* qtime_t value, timeofday time-base... */
/* ...just in case != CLOCK_REALTIME ! */
/* A qtime_t second 123456789 -- nano-seconds */
#define QTIME_SECOND 1000000000
#define TIMESPEC_SECOND 1000000000
#define TIMEVAL_SECOND 1000000
/* Macro to convert time in seconds to a qtime_t */
/* Note that the time to convert may be a float. */
#define QTIME(s) ((qtime_t)((s) * (qtime_t)QTIME_SECOND))
Inline qtime_t
timespec2qtime(struct timespec* p_ts) ;
Inline qtime_t
timeval2qtime(struct timeval* p_tv) ;
Inline struct timespec*
qtime2timespec(struct timespec* p_ts, qtime_t qt) ;
Inline struct timeval*
qtime2timeval(struct timeval* p_tv, qtime_t qt) ;
/*==============================================================================
* Clocks.
*
* Here is support for:
*
* * System Clock
*
* This can be read using either clock_gettime(CLOCK_REALTIME, &ts) or
* gettimeofday(&tv, NULL) -- which (are believed to) return the same clock,
* but in different units.
*
* * Monotonic Clock
*
* Using clock_gettime(CLOCK_MONOTONIC, &ts) if it is available, otherwise
* a manufactured equivalent using times() -- see qt_craft_monotonic().
*/
Inline qtime_real_t
qt_get_realtime(void) ; /* clock_gettime(CLOCK_REALTIME, ...) */
Inline qtime_mono_t
qt_add_realtime(qtime_t interval) ; /* qt_get_realtime() + interval */
Inline qtime_mono_t
qt_get_monotonic(void) ; /* clock_gettime(CLOCK_MONOTONIC, ...) */
/* OR equivalent using times() */
Inline qtime_mono_t
qt_add_monotonic(qtime_t interval) ; /* qt_get_monotonic() + interval */
Inline qtime_mono_t /* monotonic time from CLOCK_REALTIME */
qt_realtime2monotonic(qtime_real_t realtime) ;
Inline qtime_real_t /* CLOCK_REALTIME from monotonic time */
qt_monotonic2realtime(qtime_mono_t monotonic) ;
/* Function to manufacture a monotonic clock. */
extern qtime_mono_t
qt_craft_monotonic(void) ;
/* These are provided just in case gettimeofday() != CLOCK_REALTIME */
Inline qtime_tod_t
qt_get_timeofday(void) ; /* gettimeofday(&tv, NULL) */
Inline qtime_tod_t
qt_add_timeofday(qtime_t interval) ; /* qt_get_timeofday() + interval */
Inline qtime_mono_t /* monotonic time from timeofday */
qt_timeofday2monotonic(qtime_tod_t timeofday) ;
Inline qtime_tod_t /* timeofday from monotonic time */
qt_monotonic2timeofday(qtime_mono_t monotonic) ;
/*==============================================================================
* Inline conversion functions
*/
/* Convert timespec to qtime_t
*
* Returns qtime_t value.
*/
Inline qtime_t
timespec2qtime(struct timespec* p_ts)
{
return QTIME(p_ts->tv_sec) + p_ts->tv_nsec ;
confirm(QTIME_SECOND == TIMESPEC_SECOND) ;
} ;
/* Convert timeval to qtime_t
*
* Returns qtime_t value.
*/
Inline qtime_t
timeval2qtime(struct timeval* p_tv)
{
return QTIME(p_tv->tv_sec) + (p_tv->tv_usec * 1000) ;
confirm(QTIME_SECOND == TIMEVAL_SECOND * 1000) ;
} ;
/* Convert qtime_t to timespec
*
* Takes address of struct timespec and returns that address.
*/
Inline struct timespec*
qtime2timespec(struct timespec* p_ts, qtime_t qt)
{
lldiv_t imd = lldiv(qt, QTIME_SECOND) ;
confirm(sizeof(long long) >= sizeof(qtime_t)) ;
p_ts->tv_sec = imd.quot ;
p_ts->tv_nsec = imd.rem ;
confirm(TIMESPEC_SECOND == QTIME_SECOND) ;
return p_ts ;
} ;
/* Convert timespec to qtime_t
*
* Takes address of struct timespec and returns that address.
*/
Inline struct timeval*
qtime2timeval(struct timeval* p_tv, qtime_t qt)
{
lldiv_t imd = lldiv(qt, QTIME_SECOND) ;
confirm(sizeof(long long) >= sizeof(qtime_t)) ;
p_tv->tv_sec = imd.quot ;
p_tv->tv_usec = imd.rem / 1000 ;
confirm(TIMEVAL_SECOND * 1000 == QTIME_SECOND) ;
return p_tv ;
} ;
/*==============================================================================
* Inline Clock Functions.
*/
/* Read given clock & return a qtime_t value.
*
* While possibility of error is essentially theoretical, must treat it as a
* FATAL error -- cannot continue with broken time value !
*/
Inline qtime_t
qt_clock_gettime(clockid_t clock_id)
{
struct timespec ts ;
if (clock_gettime(clock_id, &ts) != 0)
zabort_errno("clock_gettime failed") ;
return timespec2qtime(&ts) ;
} ;
/* clock_gettime(CLOCK_REALTIME, ...) -- returning qtime_t value
*
* While possibility of error is essentially theoretical, must treat it as a
* FATAL error -- cannot continue with broken time value !
*/
Inline qtime_real_t
qt_get_realtime(void)
{
return qt_clock_gettime(CLOCK_REALTIME) ;
} ;
/* qt_get_realtime() + interval
*/
Inline qtime_real_t
qt_add_realtime(qtime_t interval)
{
return qt_get_realtime() + interval;
} ;
/* clock_gettime(CLOCK_MONOTONIC, ...) OR qt_craft_monotonic()
* -- returning qtime_t value
*
* While possibility of error is essentially theoretical, must treat it as a
* FATAL error -- cannot continue with broken time value !
*/
Inline qtime_mono_t
qt_get_monotonic(void)
{
#ifdef HAVE_CLOCK_MONOTONIC
return qt_clock_gettime(CLOCK_MONOTONIC) ;
#else
return qt_craft_monotonic() ;
#endif
} ;
/* qt_get_monotonic() + interval
*/
Inline qtime_mono_t
qt_add_monotonic(qtime_t interval)
{
return qt_get_monotonic() + interval;
} ;
/* gettimeofday(&tv, NULL) -- returning qtime_t value
*/
Inline qtime_tod_t
qt_get_timeofday(void)
{
struct timeval tv ;
gettimeofday(&tv, NULL) ;
return timeval2qtime(&tv) ;
}
/* qt_get_timeofday() + interval
*/
Inline qtime_tod_t
qt_add_timeofday(qtime_t interval)
{
return qt_get_timeofday() + interval;
} ;
/*==============================================================================
* Conversion between realtime/timeofday and monotonic
*/
/* Convert a CLOCK_REALTIME time to our local monotonic time. */
Inline qtime_mono_t
qt_realtime2monotonic(qtime_real_t realtime)
{
return qt_get_monotonic() + (realtime - qt_get_realtime()) ;
} ;
/* Convert a local monotonic time to CLOCK_REALTIME time. */
Inline qtime_real_t
qt_monotonic2realtime(qtime_mono_t monotonic)
{
return qt_get_realtime() + (monotonic - qt_get_monotonic()) ;
} ;
/* Convert a gettimeofday() time to our local monotonic time. */
Inline qtime_mono_t
qt_timeofday2monotonic(qtime_tod_t timeofday)
{
return qt_get_monotonic() + (timeofday - qt_get_timeofday()) ;
} ;
/* Convert a local monotonic time to gettimeofday() time. */
Inline qtime_tod_t
qt_monotonic2timeofday(qtime_mono_t monotonic)
{
return qt_get_timeofday() + (monotonic - qt_get_monotonic()) ;
} ;
#endif /* _ZEBRA_QTIME_H */
|