From 327d9d482fd4480ca16e993fe2699c8bbc166251 Mon Sep 17 00:00:00 2001 From: Stefano Brivio Date: Wed, 24 Jul 2024 17:21:12 +0200 Subject: log, util: Fix sub-second part in relative log time calculation For some reason, in commit 01efc71ddd25 ("log, conf: Add support for logging to file"), I added calculations for relative logging timestamps using the difference for the seconds part only, not for accounting for the fractional part. Fix that by storing the initial timestamp, log_start, as a timespec struct, and by calculating the difference from the starting time. Do this in a macro as we need the same format in a few places. To calculate the difference, turn the existing timespec_diff_ms() to microseconds, timespec_diff_us(), and rewrite timespec_diff_ms() to use that. Signed-off-by: Stefano Brivio Reviewed-by: David Gibson --- util.c | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'util.c') diff --git a/util.c b/util.c index c275b14..f2e26a7 100644 --- a/util.c +++ b/util.c @@ -239,6 +239,23 @@ void sock_probe_mem(struct ctx *c) close(s); } +/** + * timespec_diff_us() - Report difference in microseconds between two timestamps + * @a: Minuend timestamp + * @b: Subtrahend timestamp + * + * Return: difference in microseconds (wraps after 2^64 / 10^6s ~= 585k years) + */ +long long timespec_diff_us(const struct timespec *a, const struct timespec *b) +{ + if (a->tv_nsec < b->tv_nsec) { + return (b->tv_nsec - a->tv_nsec) / 1000 + + (a->tv_sec - b->tv_sec - 1) * 1000000; + } + + return (a->tv_nsec - b->tv_nsec) / 1000 + + (a->tv_sec - b->tv_sec) * 1000000; +} /** * timespec_diff_ms() - Report difference in milliseconds between two timestamps @@ -249,13 +266,7 @@ void sock_probe_mem(struct ctx *c) */ long timespec_diff_ms(const struct timespec *a, const struct timespec *b) { - if (a->tv_nsec < b->tv_nsec) { - return (b->tv_nsec - a->tv_nsec) / 1000000 + - (a->tv_sec - b->tv_sec - 1) * 1000; - } - - return (a->tv_nsec - b->tv_nsec) / 1000000 + - (a->tv_sec - b->tv_sec) * 1000; + return timespec_diff_us(a, b) / 1000; } /** -- cgit v1.2.3