time: fix overflow in timeval_add_msec()
authorChris Taylor <ctaylor@pivot3.com>
Sun, 2 Apr 2017 21:54:02 +0000 (15:54 -0600)
committerJens Axboe <axboe@fb.com>
Sun, 2 Apr 2017 21:54:02 +0000 (15:54 -0600)
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 <axboe@fb.com>
time.c

diff --git a/time.c b/time.c
index f5dc04969f3d47020fb4f61fa3ddf5dcfcdf0e09..f637afb51487f291f7413cae596e969b8491e084 100644 (file)
--- 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++;
        }