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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
/* OS-specific real, monotonic, and local times and timezone update. */
/* Real time. */
#if (NXT_HAVE_CLOCK_REALTIME_COARSE)
/*
* Linux clock_gettime() resides on the vDSO page. Linux 2.6.32
* clock_gettime(CLOCK_REALTIME_COARSE) uses only cached values and does
* not read TSC or HPET so it has the kernel jiffy precision (1ms by default)
* and it is several times faster than clock_gettime(CLOCK_REALTIME).
*/
void
nxt_realtime(nxt_realtime_t *now)
{
struct timespec ts;
(void) clock_gettime(CLOCK_REALTIME_COARSE, &ts);
now->sec = (nxt_time_t) ts.tv_sec;
now->nsec = ts.tv_nsec;
}
#elif (NXT_HAVE_CLOCK_REALTIME_FAST)
/*
* FreeBSD 7.0 specific clock_gettime(CLOCK_REALTIME_FAST) may be
* 5-30 times faster than clock_gettime(CLOCK_REALTIME) depending
* on kern.timecounter.hardware. The clock has a precision of 1/HZ
* seconds (HZ is 1000 on modern platforms, thus 1ms precision).
* FreeBSD 9.2 clock_gettime() resides on the vDSO page and reads
* TSC. clock_gettime(CLOCK_REALTIME_FAST) is the same as
* clock_gettime(CLOCK_REALTIME).
*/
void
nxt_realtime(nxt_realtime_t *now)
{
struct timespec ts;
(void) clock_gettime(CLOCK_REALTIME_FAST, &ts);
now->sec = (nxt_time_t) ts.tv_sec;
now->nsec = ts.tv_nsec;
}
#elif (NXT_HAVE_CLOCK_REALTIME && !(NXT_HPUX))
/*
* clock_gettime(CLOCK_REALTIME) is supported by Linux, FreeBSD 3.0,
* Solaris 8, NetBSD 1.3, and AIX. HP-UX supports it too, however,
* it is implemented through a call to gettimeofday(). Linux
* clock_gettime(CLOCK_REALTIME) resides on the vDSO page and reads
* TSC or HPET. FreeBSD 9.2 clock_gettime(CLOCK_REALTIME) resides
* on the vDSO page and reads TSC.
*/
void
nxt_realtime(nxt_realtime_t *now)
{
struct timespec ts;
(void) clock_gettime(CLOCK_REALTIME, &ts);
now->sec = (nxt_time_t) ts.tv_sec;
now->nsec = ts.tv_nsec;
}
#else
/* MacOSX, HP-UX. */
void
nxt_realtime(nxt_realtime_t *now)
{
struct timeval tv;
(void) gettimeofday(&tv, NULL);
now->sec = (nxt_time_t) tv.tv_sec;
now->nsec = tv.tv_usec * 1000;
}
#endif
/* Monotonic time. */
#if (NXT_HAVE_CLOCK_MONOTONIC_COARSE)
/*
* Linux clock_gettime() resides on the vDSO page. Linux 2.6.32
* clock_gettime(CLOCK_MONOTONIC_COARSE) uses only cached values and does
* not read TSC or HPET so it has the kernel jiffy precision (1ms by default)
* and it is several times faster than clock_gettime(CLOCK_MONOTONIC).
*/
void
nxt_monotonic_time(nxt_monotonic_time_t *now)
{
struct timespec ts;
(void) clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
now->monotonic = (nxt_nsec_t) ts.tv_sec * 1000000000 + ts.tv_nsec;
}
#elif (NXT_HAVE_CLOCK_MONOTONIC_FAST)
/*
* FreeBSD 7.0 specific clock_gettime(CLOCK_MONOTONIC_FAST) may be
* 5-30 times faster than clock_gettime(CLOCK_MONOTONIC) depending
* on kern.timecounter.hardware. The clock has a precision of 1/HZ
* seconds (HZ is 1000 on modern platforms, thus 1ms precision).
* FreeBSD 9.2 clock_gettime() resides on the vDSO page and reads
* TSC. clock_gettime(CLOCK_MONOTONIC_FAST) is the same as
* clock_gettime(CLOCK_MONOTONIC).
*/
void
nxt_monotonic_time(nxt_monotonic_time_t *now)
{
struct timespec ts;
(void) clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
now->monotonic = (nxt_nsec_t) ts.tv_sec * 1000000000 + ts.tv_nsec;
}
#elif (NXT_HAVE_HG_GETHRTIME)
/*
* HP-UX 11.31 provides fast hg_gethrtime() which uses a chunk of memory
* shared between userspace application and the kernel, and was introduced
* by Project Mercury ("HG").
*/
void
nxt_monotonic_time(nxt_monotonic_time_t *now)
{
now->monotonic = (nxt_nsec_t) hg_gethrtime();
}
#elif (NXT_SOLARIS || NXT_HPUX)
/*
* Solaris gethrtime(), clock_gettime(CLOCK_REALTIME), and gettimeofday()
* use a fast systrap whereas clock_gettime(CLOCK_MONOTONIC) and other
* clock_gettime()s use normal systrap. However, the difference is
* negligible on x86_64.
*
* HP-UX lacks clock_gettime(CLOCK_MONOTONIC) but has lightweight
* system call gethrtime().
*/
void
nxt_monotonic_time(nxt_monotonic_time_t *now)
{
now->monotonic = (nxt_nsec_t) gethrtime();
}
#elif (NXT_HAVE_CLOCK_MONOTONIC)
/*
* clock_gettime(CLOCK_MONOTONIC) is supported by Linux, FreeBSD 5.0,
* Solaris 8, NetBSD 1.6, and AIX. Linux clock_gettime(CLOCK_MONOTONIC)
* resides on the vDSO page and reads TSC or HPET. FreeBSD 9.2
* clock_gettime(CLOCK_MONOTONIC) resides on the vDSO page and reads TSC.
*/
void
nxt_monotonic_time(nxt_monotonic_time_t *now)
{
struct timespec ts;
(void) clock_gettime(CLOCK_MONOTONIC, &ts);
now->monotonic = (nxt_nsec_t) ts.tv_sec * 1000000000 + ts.tv_nsec;
}
#elif (NXT_MACOSX)
/*
* MacOSX does not support clock_gettime(), but mach_absolute_time() returns
* monotonic ticks. To get nanoseconds the ticks should be multiplied then
* divided by numerator/denominator returned by mach_timebase_info(), however
* on modern MacOSX they are 1/1. On PowerPC MacOSX these values were
* 1000000000/33333335 or 1000000000/25000000, on iOS 4+ they were 125/3,
* and on iOS 3 they were 1000000000/24000000.
*/
void
nxt_monotonic_time(nxt_monotonic_time_t *now)
{
now->monotonic = mach_absolute_time();
}
#else
void
nxt_monotonic_time(nxt_monotonic_time_t *now)
{
nxt_nsec_t current;
nxt_nsec_int_t delta;
struct timeval tv;
(void) gettimeofday(&tv, NULL);
now->realtime.sec = (nxt_time_t) tv.tv_sec;
now->realtime.nsec = tv.tv_usec * 1000;
/*
* Monotonic time emulation using gettimeofday()
* for platforms which lack monotonic time.
*/
current = (nxt_nsec_t) tv.tv_sec * 1000000000 + tv.tv_usec * 1000;
delta = current - now->previous;
now->previous = current;
if (delta > 0) {
now->monotonic += delta;
} else {
/* The time went backward. */
now->monotonic++;
}
/*
* Eliminate subsequent gettimeofday() call
* in nxt_thread_realtime_update().
*/
now->update = now->monotonic + 1;
}
#endif
/* Local time. */
#if (NXT_HAVE_LOCALTIME_R)
void
nxt_localtime(nxt_time_t s, struct tm *tm)
{
time_t _s;
_s = (time_t) s;
(void) localtime_r(&_s, tm);
}
#else
void
nxt_localtime(nxt_time_t s, struct tm *tm)
{
time_t _s;
struct tm *_tm;
_s = (time_t) s;
_tm = localtime(&_s);
*tm = *_tm;
}
#endif
/* Timezone update. */
#if (NXT_LINUX)
/*
* Linux glibc does not test /etc/localtime change
* in localtime_r(), but tests in localtime().
*/
void
nxt_timezone_update(void)
{
time_t s;
s = time(NULL);
(void) localtime(&s);
}
#elif (NXT_FREEBSD)
/*
* FreeBSD libc does not test /etc/localtime change, but it can be
* worked around by calling tzset() with TZ and then without TZ
* to update timezone. This trick should work since FreeBSD 2.1.0.
*/
void
nxt_timezone_update(void)
{
if (getenv("TZ") != NULL) {
return;
}
/* The libc uses /etc/localtime if TZ is not set. */
(void) putenv((char *) "TZ=UTC");
tzset();
(void) unsetenv("TZ");
tzset();
}
#elif (NXT_SOLARIS)
/*
* Solaris 10, patch 142909-17 introduced tzreload(8):
*
* The tzreload command notifies active (running) processes to reread
* timezone information. The timezone information is cached in each
* process, absent a tzreload command, is never reread until a process
* is restarted. In response to a tzreload command, active processes
* reread the current timezone information at the next call to ctime(3C)
* and mktime(3C). By default, the tzreload notification is sent to
* the processes within the current zone.
*/
void
nxt_timezone_update(void)
{
time_t s;
s = time(NULL);
(void) ctime(&s);
}
#else
void
nxt_timezone_update(void)
{
return;
}
#endif
|