Don't malloc more than necessary on extending/prereading file
authorTomohiro Kusumi <tkusumi@tuxera.com>
Tue, 28 Mar 2017 20:02:52 +0000 (23:02 +0300)
committerJens Axboe <axboe@fb.com>
Tue, 28 Mar 2017 21:14:20 +0000 (15:14 -0600)
If td->o.max_bs[DDIR_XXX] were larger than file size to extend/preread,
it only needs to malloc the file size.

Given that td->o.max_bs[DDIR_XXX] are tunable parameters, it's better
not to malloc whatever size specified if needed size is smaller than
that. Users could specify as large as maximum of uint.

I dropped "bs = td->o.max_bs[DDIR_WRITE];" inside the first while loop
since now it works the same with or without this.

(This commit directly goes on top of the previous one)

Signed-off-by: Tomohiro Kusumi <tkusumi@tuxera.com>
Signed-off-by: Jens Axboe <axboe@fb.com>
filesetup.c

index ce19cf0b127d294e6d8d4c012595863552598b30..9335dcd3f8dd4b2c38548ee23dbc7e56282caeb0 100644 (file)
@@ -159,15 +159,18 @@ static int extend_file(struct thread_data *td, struct fio_file *f)
                }
        }
 
-       b = malloc(td->o.max_bs[DDIR_WRITE]);
+       left = f->real_file_size;
+       bs = td->o.max_bs[DDIR_WRITE];
+       if (bs > left)
+               bs = left;
+
+       b = malloc(bs);
        if (!b) {
                td_verror(td, errno, "malloc");
                goto err;
        }
 
-       left = f->real_file_size;
        while (left && !td->terminate) {
-               bs = td->o.max_bs[DDIR_WRITE];
                if (bs > left)
                        bs = left;
 
@@ -245,7 +248,11 @@ static int pre_read_file(struct thread_data *td, struct fio_file *f)
 
        old_runstate = td_bump_runstate(td, TD_PRE_READING);
 
+       left = f->io_size;
        bs = td->o.max_bs[DDIR_READ];
+       if (bs > left)
+               bs = left;
+
        b = malloc(bs);
        if (!b) {
                td_verror(td, errno, "malloc");
@@ -261,8 +268,6 @@ static int pre_read_file(struct thread_data *td, struct fio_file *f)
                goto error;
        }
 
-       left = f->io_size;
-
        while (left && !td->terminate) {
                if (bs > left)
                        bs = left;