On Fri, Sep 06, 2024 at 12:49:39PM +0200, Stefano Brivio wrote: > If the nanoseconds of the minuend timestamp are less than the > nanoseconds of the subtrahend timestamp, we need to carry one second > in the subtraction. > > I subtracted this second from the minuend, but didn't actually carry > it in the subtraction of nanoseconds, and logged timestamps would jump > back whenever we switched to the first branch of timespec_diff_us() > from the second one. Perhaps more to the point, the subtraction was the wrong way around before. I think we're too used to math mod 2^n, where reversing the order of the arguments would be equivalent to adding an extra 2^n. > Signed-off-by: Stefano Brivio > --- > util.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/util.c b/util.c > index 6e64279..eede4e5 100644 > --- a/util.c > +++ b/util.c > @@ -249,7 +249,7 @@ void sock_probe_mem(struct ctx *c) > int64_t 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 + > + return (a->tv_nsec + 1000000000 - b->tv_nsec) / 1000 + > (a->tv_sec - b->tv_sec - 1) * 1000000; > } > -- David Gibson (he or they) | I'll have my music baroque, and my code david AT gibson.dropbear.id.au | minimalist, thank you, not the other way | around. http://www.ozlabs.org/~dgibson