[PATCH] fio: fixup rate calculations
authorJens Axboe <axboe@suse.de>
Thu, 27 Oct 2005 10:49:27 +0000 (12:49 +0200)
committerJens Axboe <axboe@suse.de>
Thu, 27 Oct 2005 10:49:27 +0000 (12:49 +0200)
fio.c

diff --git a/fio.c b/fio.c
index 42b7b036b284fe79de4e1192438796b8375d047c..435db1985b4d7c81a0492a55729a044846844208 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -479,13 +479,18 @@ static void usec_sleep(int usec)
        } while (1);
 }
 
-static void rate_throttle(struct thread_data *td, unsigned long time_spent)
+static void rate_throttle(struct thread_data *td, unsigned long time_spent,
+                         unsigned int bytes)
 {
+       unsigned long usec_cycle;
+
        if (!td->rate)
                return;
 
-       if (time_spent < td->rate_usec_cycle) {
-               unsigned long s = td->rate_usec_cycle - time_spent;
+       usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
+
+       if (time_spent < usec_cycle) {
+               unsigned long s = usec_cycle - time_spent;
 
                td->rate_pending_usleep += s;
                if (td->rate_pending_usleep >= 100000) {
@@ -493,7 +498,7 @@ static void rate_throttle(struct thread_data *td, unsigned long time_spent)
                        td->rate_pending_usleep = 0;
                }
        } else {
-               long overtime = time_spent - td->rate_usec_cycle;
+               long overtime = time_spent - usec_cycle;
 
                td->rate_pending_usleep -= overtime;
        }
@@ -631,7 +636,7 @@ static void do_sync_io(struct thread_data *td)
 
                usec = utime_since(&io_u->start_time, &e);
 
-               rate_throttle(td, usec);
+               rate_throttle(td, usec, io_u->buflen);
 
                if (check_min_rate(td, &e)) {
                        td->error = ENODATA;
@@ -675,20 +680,21 @@ static int io_u_queue(struct thread_data *td, struct io_u *io_u)
 #define iocb_time(iocb)        ((unsigned long) (iocb)->data)
 #define ev_to_iou(ev)  (struct io_u *) ((unsigned long) (ev)->obj)
 
-static void ios_completed(struct thread_data *td, int nr)
+static int ios_completed(struct thread_data *td, int nr)
 {
        unsigned long msec;
        struct io_u *io_u;
        struct timeval e;
-       int i;
+       int i, bytes_done;
 
        gettimeofday(&e, NULL);
 
-       for (i = 0; i < nr; i++) {
+       for (i = 0, bytes_done = 0; i < nr; i++) {
                io_u = ev_to_iou(td->aio_events + i);
 
                td->io_blocks++;
                td->io_kb += io_u->buflen >> 10;
+               bytes_done += io_u->buflen;
 
                msec = mtime_since(&io_u->issue_time, &e);
 
@@ -697,6 +703,8 @@ static void ios_completed(struct thread_data *td, int nr)
 
                put_io_u(td, io_u);
        }
+
+       return bytes_done;
 }
 
 static void cleanup_pending_aio(struct thread_data *td)
@@ -741,6 +749,7 @@ static void do_async_io(struct thread_data *td)
                struct timespec *timeout;
                int ret, min_evts = 0;
                struct io_u *io_u;
+               unsigned int bytes_done;
 
                if (td->terminate)
                        break;
@@ -779,7 +788,7 @@ static void do_async_io(struct thread_data *td)
                } else if (!ret)
                        continue;
 
-               ios_completed(td, ret);
+               bytes_done = ios_completed(td, ret);
 
                if (should_fsync(td) && td->fsync_blocks &&
                    (td->io_blocks % td->fsync_blocks) == 0)
@@ -793,7 +802,7 @@ static void do_async_io(struct thread_data *td)
                gettimeofday(&e, NULL);
                usec = utime_since(&s, &e);
 
-               rate_throttle(td, usec);
+               rate_throttle(td, usec, bytes_done);
 
                if (check_min_rate(td, &e)) {
                        td->error = ENODATA;