From 10927316a4bd0e2ceba98e62c062e352c0e312fe Mon Sep 17 00:00:00 2001 From: Shawn Lewis Date: Wed, 21 Nov 2007 09:38:13 +0100 Subject: [PATCH] fix utime_since overflow utime_since was using a long for its return which overflows when the difference between times is more than about 35 minutes. Signed-off-by: Jens Axboe --- fio.c | 3 +-- fio.h | 4 ++-- time.c | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/fio.c b/fio.c index e71537fc..e9b27d9c 100644 --- a/fio.c +++ b/fio.c @@ -787,9 +787,8 @@ static int clear_io_state(struct thread_data *td) */ static void *thread_main(void *data) { - unsigned long long runtime[2]; + unsigned long long runtime[2], elapsed; struct thread_data *td = data; - unsigned long elapsed; int clear_state; if (!td->o.use_thread) diff --git a/fio.h b/fio.h index 5ca2ad36..2e43473f 100644 --- a/fio.h +++ b/fio.h @@ -777,8 +777,8 @@ extern void add_agg_sample(unsigned long, enum fio_ddir); /* * Time functions */ -extern unsigned long utime_since(struct timeval *, struct timeval *); -extern unsigned long utime_since_now(struct timeval *); +extern unsigned long long utime_since(struct timeval *, struct timeval *); +extern unsigned long long utime_since_now(struct timeval *); extern unsigned long mtime_since(struct timeval *, struct timeval *); extern unsigned long mtime_since_now(struct timeval *); extern unsigned long time_since_now(struct timeval *); diff --git a/time.c b/time.c index 4fbc98bb..6d79ecdc 100644 --- a/time.c +++ b/time.c @@ -6,10 +6,11 @@ static struct timeval genesis; static unsigned long ns_granularity; -unsigned long utime_since(struct timeval *s, struct timeval *e) +unsigned long long utime_since(struct timeval *s, struct timeval *e) { - long sec, usec, ret; - + long sec, usec; + unsigned long long ret; + sec = e->tv_sec - s->tv_sec; usec = e->tv_usec - s->tv_usec; if (sec > 0 && usec < 0) { @@ -17,19 +18,18 @@ unsigned long utime_since(struct timeval *s, struct timeval *e) usec += 1000000; } - sec *= 1000000UL; - ret = sec + usec; - /* * time warp bug on some kernels? */ - if (ret < 0) - ret = 0; - + if (sec < 0 || (sec == 0 && usec < 0)) + return 0; + + ret = sec * 1000000ULL + usec; + return ret; } -unsigned long utime_since_now(struct timeval *s) +unsigned long long utime_since_now(struct timeval *s) { struct timeval t; -- 2.25.1