aboutgitcodebugslistschat
path: root/util.c
diff options
context:
space:
mode:
authorStefano Brivio <sbrivio@redhat.com>2024-07-24 17:21:12 +0200
committerStefano Brivio <sbrivio@redhat.com>2024-07-26 13:43:19 +0200
commit327d9d482fd4480ca16e993fe2699c8bbc166251 (patch)
treedbfb4ddf7b70c8a490c999621aab225d67b8076b /util.c
parent2ce1d3783184ecc637d8e9be997a42e1c78a908e (diff)
downloadpasst-327d9d482fd4480ca16e993fe2699c8bbc166251.tar
passt-327d9d482fd4480ca16e993fe2699c8bbc166251.tar.gz
passt-327d9d482fd4480ca16e993fe2699c8bbc166251.tar.bz2
passt-327d9d482fd4480ca16e993fe2699c8bbc166251.tar.lz
passt-327d9d482fd4480ca16e993fe2699c8bbc166251.tar.xz
passt-327d9d482fd4480ca16e993fe2699c8bbc166251.tar.zst
passt-327d9d482fd4480ca16e993fe2699c8bbc166251.zip
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 <sbrivio@redhat.com> Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
Diffstat (limited to 'util.c')
-rw-r--r--util.c25
1 files changed, 18 insertions, 7 deletions
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;
}
/**