From: Chris Taylor Date: Sun, 2 Apr 2017 21:54:02 +0000 (-0600) Subject: time: fix overflow in timeval_add_msec() X-Git-Tag: fio-2.19~3 X-Git-Url: https://git.kernel.dk/?a=commitdiff_plain;h=d19251c4a191dd024bec82be876f57f7770e32f6;p=fio.git time: fix overflow in timeval_add_msec() There is an issue in FIO when using the detailed bandwidth and iops logging with averaging over a period of time > 1 second. It seems that usecs overflows which later causes negative time diff values resulting in skewed toward 0 results. I have attached a potential fix that should prevent usecs from going beyond 1000000. [Modified by Jens to put the adj_sec in the branch.] Signed-off-by: Jens Axboe --- diff --git a/time.c b/time.c index f5dc0496..f637afb5 100644 --- a/time.c +++ b/time.c @@ -8,8 +8,16 @@ static unsigned long ns_granularity; void timeval_add_msec(struct timeval *tv, unsigned int msec) { - tv->tv_usec += 1000 * msec; - if (tv->tv_usec >= 1000000) { + unsigned int adj_usec = 1000 * msec; + + tv->tv_usec += adj_usec; + if (adj_usec >= 1000000) { + unsigned int adj_sec = adj_usec / 1000000; + + tv->tv_usec -= adj_sec * 1000000; + tv->tv_sec += adj_sec; + } + if (tv->tv_usec >= 1000000){ tv->tv_usec -= 1000000; tv->tv_sec++; }