From 47293a4f116e04a88b0797bafad0c98b4393b744 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Thu, 5 Nov 2026 13:52:17 +0100 Subject: Clamp every elapsed time span at zero, using a set of helper functions --- src/ot_stats.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src/ot_stats.c') diff --git a/src/ot_stats.c b/src/ot_stats.c index 2d6335b..7b5b1b9 100644 --- a/src/ot_stats.c +++ b/src/ot_stats.c @@ -373,7 +373,7 @@ void stats_top_txt(char **reply, const char *reply_end, size_t amount) { static unsigned long events_per_time(unsigned long long events, time_t t) { return events / ((unsigned int)t ? (unsigned int)t : 1); } static void stats_connections_mrtg(char **reply, const char *reply_end) { - ot_time t = time(NULL) - ot_start_time; + ot_time t = time_since_seconds(ot_start_time); unsigned long long total[OT_COUNTER_COUNT]; counters_total(total); @@ -386,7 +386,7 @@ static void stats_connections_mrtg(char **reply, const char *reply_end) { } static void stats_udpconnections_mrtg(char **reply, const char *reply_end) { - ot_time t = time(NULL) - ot_start_time; + ot_time t = time_since_seconds(ot_start_time); unsigned long long total[OT_COUNTER_COUNT]; counters_total(total); @@ -398,7 +398,7 @@ static void stats_udpconnections_mrtg(char **reply, const char *reply_end) { } static void stats_tcpconnections_mrtg(char **reply, const char *reply_end) { - time_t t = time(NULL) - ot_start_time; + time_t t = time_since_seconds(ot_start_time); unsigned long long total[OT_COUNTER_COUNT]; counters_total(total); @@ -409,7 +409,7 @@ static void stats_tcpconnections_mrtg(char **reply, const char *reply_end) { } static void stats_scrape_mrtg(char **reply, const char *reply_end) { - time_t t = time(NULL) - ot_start_time; + time_t t = time_since_seconds(ot_start_time); unsigned long long total[OT_COUNTER_COUNT]; counters_total(total); @@ -420,7 +420,7 @@ static void stats_scrape_mrtg(char **reply, const char *reply_end) { } static void stats_fullscrapes_mrtg(char **reply, const char *reply_end) { - ot_time t = time(NULL) - ot_start_time; + ot_time t = time_since_seconds(ot_start_time); unsigned long long total[OT_COUNTER_COUNT]; counters_total(total); @@ -468,7 +468,7 @@ static void stats_return_renew_bucket(char **reply, const char *reply_end) { } static void stats_return_sync_mrtg(char **reply, const char *reply_end) { - ot_time t = time(NULL) - ot_start_time; + ot_time t = time_since_seconds(ot_start_time); unsigned long long total[OT_COUNTER_COUNT]; counters_total(total); @@ -479,7 +479,7 @@ static void stats_return_sync_mrtg(char **reply, const char *reply_end) { } static void stats_return_completed_mrtg(char **reply, const char *reply_end) { - ot_time t = time(NULL) - ot_start_time; + ot_time t = time_since_seconds(ot_start_time); unsigned long long total[OT_COUNTER_COUNT]; counters_total(total); @@ -510,7 +510,7 @@ static void stats_return_everything(char **reply, const char *reply_end) { fmt_append(reply, reply_end, "\n"); fmt_append(reply, reply_end, "\n"); - fmt_append(reply, reply_end, " %llu\n", (unsigned long long)(time(NULL) - ot_start_time)); + fmt_append(reply, reply_end, " %llu\n", (unsigned long long)time_since_seconds(ot_start_time)); fmt_append(reply, reply_end, " \n"); fmt_append(reply, reply_end, " %zd\n", mutex_get_torrent_count()); fmt_append(reply, reply_end, " %llu\n", stats.torrent_count); @@ -785,7 +785,7 @@ void stats_issue_event(struct ot_workstruct *ws, ot_status_event event, PROTO_FL case EVENT_FULLSCRAPE_REQUEST: { ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ char _debug[512]; - int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(time_now_seconds() - ot_start_time) / 60); + int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)time_since_seconds(ot_start_time) / 60); off += ip_format(_debug + off, *ip); off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); (void)write(2, _debug, off); @@ -794,7 +794,7 @@ void stats_issue_event(struct ot_workstruct *ws, ot_status_event event, PROTO_FL case EVENT_FULLSCRAPE_REQUEST_GZIP: { ot_ip6 *ip = (ot_ip6 *)event_data; /* ugly hack to transfer ip to stats */ char _debug[512]; - int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)(time_now_seconds() - ot_start_time) / 60); + int off = snprintf(_debug, sizeof(_debug), "[%08d] scrp: ", (unsigned int)time_since_seconds(ot_start_time) / 60); off += ip_format(_debug + off, *ip); off += snprintf(_debug + off, sizeof(_debug) - off, " - FULL SCRAPE\n"); (void)write(2, _debug, off); @@ -804,7 +804,8 @@ void stats_issue_event(struct ot_workstruct *ws, ot_status_event event, PROTO_FL COUNT(OT_COUNTER_FAILED + event_data, 1); break; case EVENT_RENEW: - COUNT(OT_COUNTER_RENEWED + event_data, 1); + if (event_data < OT_PEER_TIMEOUT) + COUNT(OT_COUNTER_RENEWED + event_data, 1); break; case EVENT_SYNC: COUNT(OT_COUNTER_SYNCS, event_data); -- cgit v1.3