core/ngx_times.c源文件分析
nginx中很多地方都需要用到时间戳信息,但是如果每一次都直接调用系统函数来获取,虽然可以保证时间的精确性,但是却会严重降低系统的性能。考虑到Nginx中很多地方用到的时间戳并不需要十分精确,从系统性能方面考虑,nginx采用缓存时间戳的方法来处理。
1. 相关变量的定义
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
/*
* 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.
*/
#define NGX_TIME_SLOTS 64
static ngx_uint_t slot;
static ngx_atomic_t ngx_time_lock;
volatile ngx_msec_t ngx_current_msec;
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
static ngx_time_t cached_time[NGX_TIME_SLOTS];
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" };
这里对于每一种时间
的缓存都用一个数组来实现。之所以用数组,是因为我们在调用ngx_time_update()这一函数过程中,很有可能在中间的某个时刻别的模块正在使用我们所缓存的时间。这就可能会出现,我们更新某个时间时,可能只更新了一部分,比如更新了秒
部分,但是却还没有来得及更新毫秒
部分,这时候就被别的模块所使用,则显然是存在问题的。这里采用一个足够大的数组,确保其他模块在使用时只会用到我们更新完成的,而对于正在更新的则不会被引用到。
-
slot: 用于记录当前我们需要更新那个槽的时间
-
ngx_time_lock: 更新时间时加的互斥锁,防止同时调用ngx_time_update()函数造成混乱
-
ngx_current_msec: 缓存的当前时间的毫秒数。注意这个时间由于表示范围的关系,一般只作为程序内部的一个相对精确的
时间区间
(time diff)来使用 -
ngx_cached_time: 当前nginx缓存的最新时间
-
ngx_cached_err_log_time: 当前nginx缓存的error log格式的时间
-
ngx_cached_http_time: 当前nginx缓存的http 格式的时间
-
ngx_cached_http_log_time: 当期nginx缓存的http log格式的时间
-
ngx_cached_http_log_iso8601: 当前nginx缓存的http log iso8601格式的时间
-
ngx_cached_syslog_time: 当前nginx缓存的syslog格式的时间
-
cached_gmtoff: 用于缓存当前本地时间与GMT的时差(以
分钟
为单位)。这里之所以要缓存,是因为localtime() 与 localtime_r()函数都不是异步信号安全( Async-Signal-Safe)的,因此我们在通过信号更新时间时就不能调用这两个函数,此种情况下如果要将utc时间转换成gmt的话,就要用到本变量。
如下是为了实现上面各缓存时间的数组:
static ngx_time_t cached_time[NGX_TIME_SLOTS]; 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")];
-
week: 星期的字符串表示
-
months: 月份的字符串表示
2. 函数
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;
ngx_cached_time = &cached_time[0];
ngx_time_update();
}
本函数主要是初始化各种时间字符串表示形式的长度。接着调用ngx_time_update()更新各变量到最新时间。
3. 函数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;
tp = &cached_time[slot];
if (tp->sec == sec) {
tp->msec = msec;
ngx_unlock(&ngx_time_lock);
return;
}
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();
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);
}
此获取当前最新时间,然后更新数组中的值。下面简要分析一下函数的实现流程:
void
ngx_time_update(void)
{
//1) 获取更新时间的锁ngx_time_lock
//2) 获取当前最新时间
//3) 将当前时间的 '秒数' 与上一次缓存的最新时间的 '秒数'进行对比,如果
//秒数相同,说明上次更新与本次更新相隔时间很短,这里就不再对缓存的各变量
//再进行更新了
//4) 更新缓存数组的下一个slot的值
//5) ngx_memory_barrier(): 这里之所以用到内存屏障,是因为程序在执行过程中可能
//会出现内存的实际访问顺序与程序代码编写的访问顺序不一致。这就是内存乱序访问。
//内存乱序访问行为出现的理由是为了提升程序运行时的性能
//6) 更新各全局变量的值,由于这里都是指针操作,因此是原子性的
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;
//7) 解锁
}
4. 函数ngx_time_sigsafe_update()
#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
本函数主要是处理通过信号
来更新缓存时间。注意,此处只会更新ngx_cached_err_log_time与ngx_cached_syslog_time这两个精度要求相对较低的缓存。
5. 函数ngx_http_time()
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);
}
本函数用于将时间t
格式化成http格式
6. 函数ngx_http_cookie_time()
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);
}
本函数用于将时间t
格式化成cookie格式
7. 函数ngx_gmtime()
void
ngx_gmtime(time_t t, ngx_tm_t *tp)
{
ngx_int_t yday;
ngx_uint_t n, sec, min, hour, mday, mon, year, wday, days, leap;
/* the calculation is valid for positive time_t only */
n = (ngx_uint_t) t;
days = n / 86400;
/* January 1, 1970 was Thursday */
wday = (4 + days) % 7;
n %= 86400;
hour = n / 3600;
n %= 3600;
min = n / 60;
sec = n % 60;
/*
* the algorithm based on Gauss' formula,
* see src/http/ngx_http_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_tm_t
格式。
8. 函数ngx_next_time()
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;
}
本函数用于计算相对于当前本地时间
when秒后的时间。
[参看]