fix utime_since overflow
authorShawn Lewis <shawnlewis@google.com>
Wed, 21 Nov 2007 08:38:13 +0000 (09:38 +0100)
committerJens Axboe <jens.axboe@oracle.com>
Wed, 21 Nov 2007 08:38:13 +0000 (09:38 +0100)
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 <jens.axboe@oracle.com>
fio.c
fio.h
time.c

diff --git a/fio.c b/fio.c
index e71537fc9d45c379d53c001d1ae160e42b6eb435..e9b27d9ccef7405583065434f60f858c09f02cea 100644 (file)
--- 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)
 {
  */
 static void *thread_main(void *data)
 {
-       unsigned long long runtime[2];
+       unsigned long long runtime[2], elapsed;
        struct thread_data *td = data;
        struct thread_data *td = data;
-       unsigned long elapsed;
        int clear_state;
 
        if (!td->o.use_thread)
        int clear_state;
 
        if (!td->o.use_thread)
diff --git a/fio.h b/fio.h
index 5ca2ad366766e6f715c88c13ac7ad5b167e0d16e..2e43473f0ab5ba34d45e35e7c45cbd17086358be 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -777,8 +777,8 @@ extern void add_agg_sample(unsigned long, enum fio_ddir);
 /*
  * Time functions
  */
 /*
  * 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 *);
 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 4fbc98bbc57f8b781d95c7a8362ce8b8ab348700..6d79ecdca3036515f89aa0df5bd9a38b4b7085f8 100644 (file)
--- a/time.c
+++ b/time.c
@@ -6,10 +6,11 @@
 static struct timeval genesis;
 static unsigned long ns_granularity;
 
 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) {
        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;
        }
 
                usec += 1000000;
        }
 
-       sec *= 1000000UL;
-       ret = sec + usec;
-
        /*
         * time warp bug on some kernels?
         */
        /*
         * 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;
 }
 
        return ret;
 }
 
-unsigned long utime_since_now(struct timeval *s)
+unsigned long long utime_since_now(struct timeval *s)
 {
        struct timeval t;
 
 {
        struct timeval t;