-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathngx_times.c
509 lines (375 loc) · 13 KB
/
ngx_times.c
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// annotated by chrono since 2016
//
// * ngx_time_update
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
// 1.13.10改为使用单调时间
static ngx_msec_t ngx_monotonic_time(time_t sec, ngx_uint_t msec);
/*
* The time may be updated by signal handler or by several threads.
* The time update operations are rare and require to hold the ngx_time_lock.
* The time read operations are frequent, so they are lock-free and get time
* values and strings from the current slot. Thus thread may get the corrupted
* values only if it is preempted while copying and then it is not scheduled
* to run more than NGX_TIME_SLOTS seconds.
*/
// 最大64个时间槽
#define NGX_TIME_SLOTS 64
// 当前使用的时间槽
static ngx_uint_t slot;
// 更新时间使用的锁,给多线程用
static ngx_atomic_t ngx_time_lock;
// 当前的毫秒时间戳
// 1.13.10改为使用单调时间,不再是日历时间
volatile ngx_msec_t ngx_current_msec;
// 当前cache的时间,指向某个时间槽
volatile ngx_time_t *ngx_cached_time;
// 各种格式的时间字符串,缓存备用
volatile ngx_str_t ngx_cached_err_log_time;
volatile ngx_str_t ngx_cached_http_time;
volatile ngx_str_t ngx_cached_http_log_time;
volatile ngx_str_t ngx_cached_http_log_iso8601;
volatile ngx_str_t ngx_cached_syslog_time;
#if !(NGX_WIN32)
/*
* localtime() and localtime_r() are not Async-Signal-Safe functions, therefore,
* they must not be called by a signal handler, so we use the cached
* GMT offset value. Fortunately the value is changed only two times a year.
*/
static ngx_int_t cached_gmtoff;
#endif
// 缓存的时间值,共64个
static ngx_time_t cached_time[NGX_TIME_SLOTS];
// 利用这些nginx已经格式化好的时间日期字符串
// 可以实现$year/$month/$day等新变量
// 只要用ngx_str_t指定字符串的位置即可,几乎没有成本
// 但需要注意这些都是gmt时间,不是本地时间
// 如果要用本地时间则需要调用ngx_localtime,或者使用gmtoff
static u_char cached_err_log_time[NGX_TIME_SLOTS]
[sizeof("1970/09/28 12:00:00")];
static u_char cached_http_time[NGX_TIME_SLOTS]
[sizeof("Mon, 28 Sep 1970 06:00:00 GMT")];
static u_char cached_http_log_time[NGX_TIME_SLOTS]
[sizeof("28/Sep/1970:12:00:00 +0600")];
static u_char cached_http_log_iso8601[NGX_TIME_SLOTS]
[sizeof("1970-09-28T12:00:00+06:00")];
static u_char cached_syslog_time[NGX_TIME_SLOTS]
[sizeof("Sep 28 12:00:00")];
static char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
// main函数调用,初始化时间
// cache时间指向时间槽的0位置
void
ngx_time_init(void)
{
ngx_cached_err_log_time.len = sizeof("1970/09/28 12:00:00") - 1;
ngx_cached_http_time.len = sizeof("Mon, 28 Sep 1970 06:00:00 GMT") - 1;
ngx_cached_http_log_time.len = sizeof("28/Sep/1970:12:00:00 +0600") - 1;
ngx_cached_http_log_iso8601.len = sizeof("1970-09-28T12:00:00+06:00") - 1;
ngx_cached_syslog_time.len = sizeof("Sep 28 12:00:00") - 1;
// cache时间指向时间槽的0位置
ngx_cached_time = &cached_time[0];
ngx_time_update();
}
// 更新时间
void
ngx_time_update(void)
{
u_char *p0, *p1, *p2, *p3, *p4;
ngx_tm_t tm, gmt;
time_t sec;
ngx_uint_t msec;
ngx_time_t *tp;
struct timeval tv;
// 首先获取锁,避免并发错误
if (!ngx_trylock(&ngx_time_lock)) {
return;
}
// 系统调用,获取微秒精度的时间
ngx_gettimeofday(&tv);
// 转换为秒和毫秒
sec = tv.tv_sec;
msec = tv.tv_usec / 1000;
// 得到毫秒时间戳
// ngx_current_msec = (ngx_msec_t) sec * 1000 + msec;
// 1.13.10改为使用单调时间,不再是日历时间
ngx_current_msec = ngx_monotonic_time(sec, msec);
// 当前使用的时间槽
tp = &cached_time[slot];
// 如果秒没变那么仍然使用这个槽
// 只更新毫秒
if (tp->sec == sec) {
tp->msec = msec;
ngx_unlock(&ngx_time_lock);
return;
}
// 前进slot,超过则归0
if (slot == NGX_TIME_SLOTS - 1) {
slot = 0;
} else {
slot++;
}
// 更新时间槽里的时间
tp = &cached_time[slot];
tp->sec = sec;
tp->msec = msec;
// 各种格式的时间字符串
ngx_gmtime(sec, &gmt);
p0 = &cached_http_time[slot][0];
(void) ngx_sprintf(p0, "%s, %02d %s %4d %02d:%02d:%02d GMT",
week[gmt.ngx_tm_wday], gmt.ngx_tm_mday,
months[gmt.ngx_tm_mon - 1], gmt.ngx_tm_year,
gmt.ngx_tm_hour, gmt.ngx_tm_min, gmt.ngx_tm_sec);
#if (NGX_HAVE_GETTIMEZONE)
tp->gmtoff = ngx_gettimezone();
ngx_gmtime(sec + tp->gmtoff * 60, &tm);
#elif (NGX_HAVE_GMTOFF)
ngx_localtime(sec, &tm);
cached_gmtoff = (ngx_int_t) (tm.ngx_tm_gmtoff / 60);
tp->gmtoff = cached_gmtoff;
#else
ngx_localtime(sec, &tm);
cached_gmtoff = ngx_timezone(tm.ngx_tm_isdst);
tp->gmtoff = cached_gmtoff;
#endif
p1 = &cached_err_log_time[slot][0];
(void) ngx_sprintf(p1, "%4d/%02d/%02d %02d:%02d:%02d",
tm.ngx_tm_year, tm.ngx_tm_mon,
tm.ngx_tm_mday, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec);
p2 = &cached_http_log_time[slot][0];
(void) ngx_sprintf(p2, "%02d/%s/%d:%02d:%02d:%02d %c%02i%02i",
tm.ngx_tm_mday, months[tm.ngx_tm_mon - 1],
tm.ngx_tm_year, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec,
tp->gmtoff < 0 ? '-' : '+',
ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
p3 = &cached_http_log_iso8601[slot][0];
(void) ngx_sprintf(p3, "%4d-%02d-%02dT%02d:%02d:%02d%c%02i:%02i",
tm.ngx_tm_year, tm.ngx_tm_mon,
tm.ngx_tm_mday, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec,
tp->gmtoff < 0 ? '-' : '+',
ngx_abs(tp->gmtoff / 60), ngx_abs(tp->gmtoff % 60));
p4 = &cached_syslog_time[slot][0];
(void) ngx_sprintf(p4, "%s %2d %02d:%02d:%02d",
months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
// 原子操作相关,禁止语句顺序优化
ngx_memory_barrier();
// 时间更新完毕,更新cache指针
ngx_cached_time = tp;
ngx_cached_http_time.data = p0;
ngx_cached_err_log_time.data = p1;
ngx_cached_http_log_time.data = p2;
ngx_cached_http_log_iso8601.data = p3;
ngx_cached_syslog_time.data = p4;
// 更新完毕解锁
ngx_unlock(&ngx_time_lock);
}
// 1.13.10改为使用单调时间
// 计时起点是机器的启动时间
static ngx_msec_t
ngx_monotonic_time(time_t sec, ngx_uint_t msec)
{
// 如果支持单调时间,就调用系统函数,不使用传入的参数
#if (NGX_HAVE_CLOCK_MONOTONIC)
struct timespec ts;
#if defined(CLOCK_MONOTONIC_FAST)
clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
#else
clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
sec = ts.tv_sec;
msec = ts.tv_nsec / 1000000;
#endif
// 条件编译发现不支持单调时间,就是简单的秒*1000+毫秒
return (ngx_msec_t) sec * 1000 + msec;
}
#if !(NGX_WIN32)
void
ngx_time_sigsafe_update(void)
{
u_char *p, *p2;
ngx_tm_t tm;
time_t sec;
ngx_time_t *tp;
struct timeval tv;
if (!ngx_trylock(&ngx_time_lock)) {
return;
}
ngx_gettimeofday(&tv);
sec = tv.tv_sec;
tp = &cached_time[slot];
if (tp->sec == sec) {
ngx_unlock(&ngx_time_lock);
return;
}
if (slot == NGX_TIME_SLOTS - 1) {
slot = 0;
} else {
slot++;
}
tp = &cached_time[slot];
tp->sec = 0;
ngx_gmtime(sec + cached_gmtoff * 60, &tm);
p = &cached_err_log_time[slot][0];
(void) ngx_sprintf(p, "%4d/%02d/%02d %02d:%02d:%02d",
tm.ngx_tm_year, tm.ngx_tm_mon,
tm.ngx_tm_mday, tm.ngx_tm_hour,
tm.ngx_tm_min, tm.ngx_tm_sec);
p2 = &cached_syslog_time[slot][0];
(void) ngx_sprintf(p2, "%s %2d %02d:%02d:%02d",
months[tm.ngx_tm_mon - 1], tm.ngx_tm_mday,
tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec);
ngx_memory_barrier();
ngx_cached_err_log_time.data = p;
ngx_cached_syslog_time.data = p2;
ngx_unlock(&ngx_time_lock);
}
#endif
u_char *
ngx_http_time(u_char *buf, time_t t)
{
ngx_tm_t tm;
ngx_gmtime(t, &tm);
return ngx_sprintf(buf, "%s, %02d %s %4d %02d:%02d:%02d GMT",
week[tm.ngx_tm_wday],
tm.ngx_tm_mday,
months[tm.ngx_tm_mon - 1],
tm.ngx_tm_year,
tm.ngx_tm_hour,
tm.ngx_tm_min,
tm.ngx_tm_sec);
}
u_char *
ngx_http_cookie_time(u_char *buf, time_t t)
{
ngx_tm_t tm;
ngx_gmtime(t, &tm);
/*
* Netscape 3.x does not understand 4-digit years at all and
* 2-digit years more than "37"
*/
return ngx_sprintf(buf,
(tm.ngx_tm_year > 2037) ?
"%s, %02d-%s-%d %02d:%02d:%02d GMT":
"%s, %02d-%s-%02d %02d:%02d:%02d GMT",
week[tm.ngx_tm_wday],
tm.ngx_tm_mday,
months[tm.ngx_tm_mon - 1],
(tm.ngx_tm_year > 2037) ? tm.ngx_tm_year:
tm.ngx_tm_year % 100,
tm.ngx_tm_hour,
tm.ngx_tm_min,
tm.ngx_tm_sec);
}
void
ngx_gmtime(time_t t, ngx_tm_t *tp)
{
ngx_int_t yday;
ngx_uint_t sec, min, hour, mday, mon, year, wday, days, leap;
/* the calculation is valid for positive time_t only */
if (t < 0) {
t = 0;
}
days = t / 86400;
sec = t % 86400;
/*
* no more than 4 year digits supported,
* truncate to December 31, 9999, 23:59:59
*/
if (days > 2932896) {
days = 2932896;
sec = 86399;
}
/* January 1, 1970 was Thursday */
wday = (4 + days) % 7;
hour = sec / 3600;
sec %= 3600;
min = sec / 60;
sec %= 60;
/*
* the algorithm based on Gauss' formula,
* see src/core/ngx_parse_time.c
*/
/* days since March 1, 1 BC */
days = days - (31 + 28) + 719527;
/*
* The "days" should be adjusted to 1 only, however, some March 1st's go
* to previous year, so we adjust them to 2. This causes also shift of the
* last February days to next year, but we catch the case when "yday"
* becomes negative.
*/
year = (days + 2) * 400 / (365 * 400 + 100 - 4 + 1);
yday = days - (365 * year + year / 4 - year / 100 + year / 400);
if (yday < 0) {
leap = (year % 4 == 0) && (year % 100 || (year % 400 == 0));
yday = 365 + leap + yday;
year--;
}
/*
* The empirical formula that maps "yday" to month.
* There are at least 10 variants, some of them are:
* mon = (yday + 31) * 15 / 459
* mon = (yday + 31) * 17 / 520
* mon = (yday + 31) * 20 / 612
*/
mon = (yday + 31) * 10 / 306;
/* the Gauss' formula that evaluates days before the month */
mday = yday - (367 * mon / 12 - 30) + 1;
if (yday >= 306) {
year++;
mon -= 10;
/*
* there is no "yday" in Win32 SYSTEMTIME
*
* yday -= 306;
*/
} else {
mon += 2;
/*
* there is no "yday" in Win32 SYSTEMTIME
*
* yday += 31 + 28 + leap;
*/
}
tp->ngx_tm_sec = (ngx_tm_sec_t) sec;
tp->ngx_tm_min = (ngx_tm_min_t) min;
tp->ngx_tm_hour = (ngx_tm_hour_t) hour;
tp->ngx_tm_mday = (ngx_tm_mday_t) mday;
tp->ngx_tm_mon = (ngx_tm_mon_t) mon;
tp->ngx_tm_year = (ngx_tm_year_t) year;
tp->ngx_tm_wday = (ngx_tm_wday_t) wday;
}
time_t
ngx_next_time(time_t when)
{
time_t now, next;
struct tm tm;
now = ngx_time();
ngx_libc_localtime(now, &tm);
tm.tm_hour = (int) (when / 3600);
when %= 3600;
tm.tm_min = (int) (when / 60);
tm.tm_sec = (int) (when % 60);
next = mktime(&tm);
if (next == -1) {
return -1;
}
if (next - now > 0) {
return next;
}
tm.tm_mday++;
/* mktime() should normalize a date (Jan 32, etc) */
next = mktime(&tm);
if (next != -1) {
return next;
}
return -1;
}