[PATCH] fio: factor out common code and shutdown handling
authorJens Axboe <axboe@suse.de>
Fri, 21 Oct 2005 13:51:33 +0000 (15:51 +0200)
committerJens Axboe <axboe@suse.de>
Fri, 21 Oct 2005 13:51:33 +0000 (15:51 +0200)
Also allow comments in job files

fio.c

diff --git a/fio.c b/fio.c
index e2130c26257037fcb341a7327d57765ae1ea7929..eb23a4301d144ee164d106e0fe1c9aed9484c47f 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -132,6 +132,7 @@ struct thread_data {
        int fd;
        int stat_fd;
        pid_t pid;
+       char *buf;
        volatile int terminate;
        volatile int runstate;
        unsigned int ddir;
@@ -402,17 +403,8 @@ static int check_min_rate(struct thread_data *td, struct timeval *now)
 static void do_sync_io(struct thread_data *td)
 {
        struct timeval s, e;
-       char *buffer, *ptr;
        unsigned long blocks, msec, usec;
 
-       ptr = malloc(td->bs + MASK);
-       buffer = ALIGN(ptr);
-
-       gettimeofday(&td->start, NULL);
-
-       if (td->ratemin)
-               memcpy(&td->lastrate, &td->start, sizeof(td->start));
-
        for (blocks = 0; blocks < td->blocks; blocks++) {
                off_t offset = get_next_offset(td);
                int ret;
@@ -431,9 +423,9 @@ static void do_sync_io(struct thread_data *td)
                gettimeofday(&s, NULL);
 
                if (td->ddir == DDIR_READ)
-                       ret = read(td->fd, buffer, td->bs);
+                       ret = read(td->fd, td->buf, td->bs);
                else
-                       ret = write(td->fd, buffer, td->bs);
+                       ret = write(td->fd, td->buf, td->bs);
 
                if (ret < (int) td->bs) {
                        if (ret == -1)
@@ -469,11 +461,6 @@ static void do_sync_io(struct thread_data *td)
 
        if (should_fsync(td))
                fsync(td->fd);
-
-       gettimeofday(&e, NULL);
-       td->runtime = mtime_since(&td->start, &e);
-
-       free(ptr);
 }
 
 static void aio_put_iocb(struct thread_data *td, struct iocb *iocb)
@@ -484,8 +471,7 @@ static void aio_put_iocb(struct thread_data *td, struct iocb *iocb)
        td->aio_cur_depth--;
 }
 
-static struct iocb *aio_get_iocb(struct thread_data *td, char *buffer,
-                                struct timeval *t)
+static struct iocb *aio_get_iocb(struct thread_data *td, struct timeval *t)
 {
        struct iocb *iocb = NULL;
        unsigned int i;
@@ -500,7 +486,7 @@ static struct iocb *aio_get_iocb(struct thread_data *td, char *buffer,
 
        if (iocb) {
                off_t off = get_next_offset(td);
-               char *p = buffer + i * td->bs;
+               char *p = td->buf + i * td->bs;
 
                if (td->ddir == DDIR_READ)
                        io_prep_pread(iocb, td->fd, p, td->bs, off);
@@ -538,17 +524,8 @@ static int aio_submit(struct thread_data *td, struct iocb *iocb)
 static void do_async_io(struct thread_data *td)
 {
        struct timeval s, e;
-       char *buf, *ptr;
        unsigned long blocks, msec, usec;
 
-       ptr = malloc(td->bs * td->aio_depth + MASK);
-       buf = ALIGN(ptr);
-
-       gettimeofday(&td->start, NULL);
-
-       if (td->ratemin)
-               memcpy(&td->lastrate, &td->start, sizeof(td->start));
-
        for (blocks = 0; blocks < td->blocks; blocks++) {
                struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
                struct timespec *timeout;
@@ -563,7 +540,7 @@ static void do_async_io(struct thread_data *td)
 
                gettimeofday(&s, NULL);
 
-               iocb = aio_get_iocb(td, buf, &s);
+               iocb = aio_get_iocb(td, &s);
 
                ret = aio_submit(td, iocb);
                if (ret) {
@@ -622,11 +599,6 @@ static void do_async_io(struct thread_data *td)
                        break;
                }
        }
-
-       gettimeofday(&e, NULL);
-       td->runtime = mtime_since(&td->start, &e);
-
-       free(ptr);
 }
 
 static void cleanup_pending_aio(struct thread_data *td)
@@ -695,10 +667,13 @@ static int init_aio(struct thread_data *td)
 static void *thread_main(int shm_id, int offset, char *argv[])
 {
        struct thread_data *td;
-       void *data;
+       struct timeval end;
+       void *data, *ptr = NULL;
        struct stat st;
        int ret = 1, flags;
 
+       setsid();
+
        data = shmat(shm_id, NULL, 0);
        td = data + offset * sizeof(struct thread_data);
        td->pid = getpid();
@@ -761,23 +736,39 @@ static void *thread_main(int shm_id, int offset, char *argv[])
        sem_post(&startup_sem);
        sem_wait(&td->mutex);
 
-       if (!td->use_aio)
+       gettimeofday(&td->start, NULL);
+
+       if (td->ratemin)
+               memcpy(&td->lastrate, &td->start, sizeof(td->start));
+
+       if (!td->use_aio) {
+               ptr = malloc(td->bs + MASK);
+               td->buf = ALIGN(ptr);
                do_sync_io(td);
-       else
+       } else {
+               ptr = malloc(td->bs * td->aio_depth + MASK);
+               td->buf = ALIGN(ptr);
                do_async_io(td);
+       }
+
+       gettimeofday(&end, NULL);
+       td->runtime = mtime_since(&td->start, &end);
 
        ret = 0;
 
 out:
        shutdown_stat_file(td);
 err:
-       if (td->fd != -1)
-               close(td->fd);
        if (td->use_aio)
                cleanup_aio(td);
+       if (td->fd != -1) {
+               close(td->fd);
+               td->fd = -1;
+       }
        if (ret)
                sem_post(&startup_sem);
-
+       if (ptr)
+               free(ptr);
        td->runstate = TD_EXITED;
        shmdt(data);
        return NULL;
@@ -1069,13 +1060,16 @@ static int check_int(char *p, char *name, unsigned int *val)
        return 1;
 }
 
-static int is_empty(char *line)
+static int is_empty_or_comment(char *line)
 {
        unsigned int i;
 
-       for (i = 0; i < strlen(line); i++)
+       for (i = 0; i < strlen(line); i++) {
                if (!isspace(line[i]) && !iscntrl(line[i]))
                        return 0;
+               if (line[i] == ';')
+                       return 0;
+       }
 
        return 1;
 }
@@ -1099,6 +1093,8 @@ static int parse_jobs_ini(char *file)
        name = malloc(256);
 
        while ((p = fgets(string, 4096, f)) != NULL) {
+               if (is_empty_or_comment(p))
+                       continue;
                if (sscanf(p, "[%s]", name) != 1)
                        continue;
 
@@ -1113,7 +1109,7 @@ static int parse_jobs_ini(char *file)
 
                fgetpos(f, &off);
                while ((p = fgets(string, 4096, f)) != NULL) {
-                       if (is_empty(p))
+                       if (is_empty_or_comment(p))
                                continue;
                        if (strstr(p, "["))
                                break;
@@ -1300,9 +1296,10 @@ static void run_threads(char *argv[])
        printf("Starting %d threads\n", thread_number);
        fflush(stdout);
 
+       signal(SIGINT, sig_handler);
+
        if (timeout) {
                signal(SIGALRM, sig_handler);
-               signal(SIGINT, sig_handler);
                alarm(timeout);
        }