comparison src/core/ngx_times.c @ 7222:81fae70d6cb8

Core: ngx_current_msec now uses monotonic time if available. When clock_gettime(CLOCK_MONOTONIC) (or faster variants, _FAST on FreeBSD, and _COARSE on Linux) is available, we now use it for ngx_current_msec. This should improve handling of timers if system time changes (ticket #189).
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 01 Mar 2018 20:25:50 +0300
parents cdbcb73239ee
children 9e7de0547f09
comparison
equal deleted inserted replaced
7221:43585e0e12a3 7222:81fae70d6cb8
5 */ 5 */
6 6
7 7
8 #include <ngx_config.h> 8 #include <ngx_config.h>
9 #include <ngx_core.h> 9 #include <ngx_core.h>
10
11
12 static ngx_msec_t ngx_monotonic_time(time_t sec, ngx_uint_t msec);
10 13
11 14
12 /* 15 /*
13 * The time may be updated by signal handler or by several threads. 16 * The time may be updated by signal handler or by several threads.
14 * The time update operations are rare and require to hold the ngx_time_lock. 17 * The time update operations are rare and require to hold the ngx_time_lock.
91 ngx_gettimeofday(&tv); 94 ngx_gettimeofday(&tv);
92 95
93 sec = tv.tv_sec; 96 sec = tv.tv_sec;
94 msec = tv.tv_usec / 1000; 97 msec = tv.tv_usec / 1000;
95 98
96 ngx_current_msec = (ngx_msec_t) sec * 1000 + msec; 99 ngx_current_msec = ngx_monotonic_time(sec, msec);
97 100
98 tp = &cached_time[slot]; 101 tp = &cached_time[slot];
99 102
100 if (tp->sec == sec) { 103 if (tp->sec == sec) {
101 tp->msec = msec; 104 tp->msec = msec;
184 ngx_cached_http_log_time.data = p2; 187 ngx_cached_http_log_time.data = p2;
185 ngx_cached_http_log_iso8601.data = p3; 188 ngx_cached_http_log_iso8601.data = p3;
186 ngx_cached_syslog_time.data = p4; 189 ngx_cached_syslog_time.data = p4;
187 190
188 ngx_unlock(&ngx_time_lock); 191 ngx_unlock(&ngx_time_lock);
192 }
193
194
195 static ngx_msec_t
196 ngx_monotonic_time(time_t sec, ngx_uint_t msec)
197 {
198 #if (NGX_HAVE_CLOCK_MONOTONIC)
199 struct timespec ts;
200
201 #if defined(CLOCK_MONOTONIC_FAST)
202 clock_gettime(CLOCK_MONOTONIC_FAST, &ts);
203
204 #elif defined(CLOCK_MONOTONIC_COARSE)
205 clock_gettime(CLOCK_MONOTONIC_COARSE, &ts);
206
207 #else
208 clock_gettime(CLOCK_MONOTONIC, &ts);
209 #endif
210
211 sec = ts.tv_sec;
212 msec = ts.tv_nsec / 1000000;
213
214 #endif
215
216 return (ngx_msec_t) sec * 1000 + msec;
189 } 217 }
190 218
191 219
192 #if !(NGX_WIN32) 220 #if !(NGX_WIN32)
193 221