From: Willy Tarreau Date: Sat, 8 May 2021 05:40:52 +0000 (+0200) Subject: MINOR: stats: use tv_remain() to precisely compute the uptime X-Git-Tag: v2.4-dev19~75 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d37e26eaa61692f0217ddec23f63b22fcc55b7f9;p=thirdparty%2Fhaproxy.git MINOR: stats: use tv_remain() to precisely compute the uptime We'll have to support reporting sub-second uptimes, so let's use the appropriate function which will automatically adjust the tv_usec field. In addition to this, it will also report a more accurate uptime thanks to considering the sub-second part in the result. --- diff --git a/src/stats.c b/src/stats.c index a352a9bc22..4b58f662c6 100644 --- a/src/stats.c +++ b/src/stats.c @@ -4293,7 +4293,7 @@ static int stats_dump_typed_info_fields(struct buffer *out, */ int stats_fill_info(struct field *info, int len, uint flags) { - unsigned int up = (now.tv_sec - start_date.tv_sec); + struct timeval up; struct buffer *out = get_trash_chunk(); #ifdef USE_OPENSSL @@ -4307,6 +4307,8 @@ int stats_fill_info(struct field *info, int len, uint flags) } #endif + tv_remain(&start_date, &now, &up); + if (len < INF_TOTAL_FIELDS) return 0; @@ -4324,9 +4326,9 @@ int stats_fill_info(struct field *info, int len, uint flags) info[INF_PID] = mkf_u32(FO_STATUS, pid); info[INF_UPTIME] = mkf_str(FN_DURATION, chunk_newstr(out)); - chunk_appendf(out, "%ud %uh%02um%02us", up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60)); + chunk_appendf(out, "%ud %uh%02um%02us", (uint)up.tv_sec / 86400, ((uint)up.tv_sec % 86400) / 3600, ((uint)up.tv_sec % 3600) / 60, ((uint)up.tv_sec % 60)); - info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up); + info[INF_UPTIME_SEC] = mkf_u32(FN_DURATION, up.tv_sec); info[INF_START_TIME_SEC] = mkf_u32(FN_DURATION, start_date.tv_sec); info[INF_MEMMAX_MB] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax); info[INF_MEMMAX_BYTES] = mkf_u32(FO_CONFIG|FN_LIMIT, global.rlimit_memmax * 1048576L);