[PATCH 2 of 7] Switched to ngx_current_msec for $request_time tracking
Maxim Dounin
mdounin at mdounin.ru
Wed Jun 18 12:37:34 UTC 2025
# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1750204964 -10800
# Wed Jun 18 03:02:44 2025 +0300
# Node ID 1a6f707ab976e825a00d32d8eaba6fb6402fd7a8
# Parent a63efb0d2b7576f003e5c97d18a4eccbe0600e50
Switched to ngx_current_msec for $request_time tracking.
This ensures that $request_time (and $session_time in stream) is not
affected by system time changes on platforms with monotonic time available.
Also, it is now tracked similarly to $upstream_response_time, and
there should be no unexpected differences as previously observed with
clock_gettime(CLOCK_MONOTONIC_COARSE) (see 7939:9e7de0547f09).
diff --git a/src/http/modules/ngx_http_log_module.c b/src/http/modules/ngx_http_log_module.c
--- a/src/http/modules/ngx_http_log_module.c
+++ b/src/http/modules/ngx_http_log_module.c
@@ -825,13 +825,9 @@ static u_char *
ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op)
{
- ngx_time_t *tp;
- ngx_msec_int_t ms;
+ ngx_msec_int_t ms;
- tp = ngx_timeofday();
-
- ms = (ngx_msec_int_t)
- ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
+ ms = (ngx_msec_int_t) (ngx_current_msec - r->start_time);
ms = ngx_max(ms, 0);
return ngx_sprintf(buf, "%T.%03M", (time_t) ms / 1000, ms % 1000);
diff --git a/src/http/ngx_http_core_module.c b/src/http/ngx_http_core_module.c
--- a/src/http/ngx_http_core_module.c
+++ b/src/http/ngx_http_core_module.c
@@ -2323,7 +2323,6 @@ ngx_http_subrequest(ngx_http_request_t *
ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
ngx_http_post_subrequest_t *ps, ngx_uint_t flags)
{
- ngx_time_t *tp;
ngx_connection_t *c;
ngx_http_request_t *sr;
ngx_http_core_srv_conf_t *cscf;
@@ -2470,9 +2469,7 @@ ngx_http_subrequest(ngx_http_request_t *
sr->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
sr->subrequests = r->subrequests - 1;
- tp = ngx_timeofday();
- sr->start_sec = tp->sec;
- sr->start_msec = tp->msec;
+ sr->start_time = ngx_current_msec;
r->main->count++;
diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -560,7 +560,6 @@ static ngx_http_request_t *
ngx_http_alloc_request(ngx_connection_t *c)
{
ngx_pool_t *pool;
- ngx_time_t *tp;
ngx_http_request_t *r;
ngx_http_connection_t *hc;
ngx_http_core_srv_conf_t *cscf;
@@ -635,9 +634,7 @@ ngx_http_alloc_request(ngx_connection_t
r->main = r;
r->count = 1;
- tp = ngx_timeofday();
- r->start_sec = tp->sec;
- r->start_msec = tp->msec;
+ r->start_time = ngx_current_msec;
r->method = NGX_HTTP_UNKNOWN;
r->http_version = NGX_HTTP_VERSION_10;
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -410,8 +410,7 @@ struct ngx_http_request_s {
ngx_http_request_body_t *request_body;
time_t lingering_time;
- time_t start_sec;
- ngx_msec_t start_msec;
+ ngx_msec_t start_time;
ngx_uint_t method;
ngx_uint_t http_version;
diff --git a/src/http/ngx_http_variables.c b/src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c
+++ b/src/http/ngx_http_variables.c
@@ -2216,7 +2216,6 @@ ngx_http_variable_request_time(ngx_http_
ngx_http_variable_value_t *v, uintptr_t data)
{
u_char *p;
- ngx_time_t *tp;
ngx_msec_int_t ms;
p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN + 4);
@@ -2224,10 +2223,7 @@ ngx_http_variable_request_time(ngx_http_
return NGX_ERROR;
}
- tp = ngx_timeofday();
-
- ms = (ngx_msec_int_t)
- ((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
+ ms = (ngx_msec_int_t) (ngx_current_msec - r->start_time);
ms = ngx_max(ms, 0);
v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;
diff --git a/src/stream/ngx_stream.h b/src/stream/ngx_stream.h
--- a/src/stream/ngx_stream.h
+++ b/src/stream/ngx_stream.h
@@ -202,8 +202,7 @@ struct ngx_stream_session_s {
ngx_connection_t *connection;
off_t received;
- time_t start_sec;
- ngx_msec_t start_msec;
+ ngx_msec_t start_time;
ngx_log_handler_pt log_handler;
diff --git a/src/stream/ngx_stream_handler.c b/src/stream/ngx_stream_handler.c
--- a/src/stream/ngx_stream_handler.c
+++ b/src/stream/ngx_stream_handler.c
@@ -23,7 +23,6 @@ ngx_stream_init_connection(ngx_connectio
u_char text[NGX_SOCKADDR_STRLEN];
size_t len;
ngx_uint_t i;
- ngx_time_t *tp;
ngx_event_t *rev;
struct sockaddr *sa;
ngx_stream_port_t *port;
@@ -169,9 +168,7 @@ ngx_stream_init_connection(ngx_connectio
return;
}
- tp = ngx_timeofday();
- s->start_sec = tp->sec;
- s->start_msec = tp->msec;
+ s->start_time = ngx_current_msec;
rev = c->read;
rev->handler = ngx_stream_session_handler;
diff --git a/src/stream/ngx_stream_variables.c b/src/stream/ngx_stream_variables.c
--- a/src/stream/ngx_stream_variables.c
+++ b/src/stream/ngx_stream_variables.c
@@ -753,7 +753,6 @@ ngx_stream_variable_session_time(ngx_str
ngx_stream_variable_value_t *v, uintptr_t data)
{
u_char *p;
- ngx_time_t *tp;
ngx_msec_int_t ms;
p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4);
@@ -761,10 +760,7 @@ ngx_stream_variable_session_time(ngx_str
return NGX_ERROR;
}
- tp = ngx_timeofday();
-
- ms = (ngx_msec_int_t)
- ((tp->sec - s->start_sec) * 1000 + (tp->msec - s->start_msec));
+ ms = (ngx_msec_int_t) (ngx_current_msec - s->start_time);
ms = ngx_max(ms, 0);
v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;
More information about the nginx-devel
mailing list