[PATCH] fio: cleanup adding of jobs
authorJens Axboe <axboe@suse.de>
Wed, 19 Oct 2005 08:47:36 +0000 (10:47 +0200)
committerJens Axboe <axboe@suse.de>
Wed, 19 Oct 2005 08:47:36 +0000 (10:47 +0200)
fio.c

diff --git a/fio.c b/fio.c
index b7df04a2aa2a364a3799008e20e83ca7ed207e2c..51163adc0cdfef950b3dba6d7946dd5303cf441e 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -110,12 +110,12 @@ struct thread_data {
        int stat_fd;
        pid_t pid;
        int terminate;
-       int ddir;
-       int ioprio;
-       int sequential;
-       int bs;
-       int odirect;
-       int delay_sleep;
+       unsigned int ddir;
+       unsigned int ioprio;
+       unsigned int sequential;
+       unsigned int bs;
+       unsigned int odirect;
+       unsigned int delay_sleep;
        cpu_set_t cpumask;
 
        unsigned int rate;
@@ -372,7 +372,7 @@ void do_thread_io(struct thread_data *td)
                else
                        ret = write(td->fd, buffer, td->bs);
 
-               if (ret < td->bs) {
+               if (ret < (int) td->bs) {
                        if (ret == -1)
                                td->error = errno;
                        break;
@@ -465,7 +465,7 @@ void *thread_main(int shm_id, int offset, char *argv[])
        } else
                td->blocks = 1024 * 1024 * 1024 / td->bs;
 
-       if (td->ioprio != -1) {
+       if (td->ioprio) {
                if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
                        td->error = errno;
                        goto out;
@@ -514,41 +514,49 @@ void usage(char *progname)
        printf("%s: <-s 0/1> <-b kb> <-t sec> <-w 0/1> <-c r,w,r...> file0... fileN\n", progname);
 }
 
-void setup_rate(struct thread_data *td, int rate)
+void setup_rate(struct thread_data *td)
 {
-       int nr_reads_per_sec = rate * 1024 / td->bs;
+       int nr_reads_per_sec = td->rate * 1024 / td->bs;
 
-       td->rate = rate;
        td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
        td->rate_pending_usleep = 0;
 }
 
-void add_job(const char *filename, int rw, int bs, int direct, int prio, int random, int delay, int rate, cpu_set_t cpumask)
+struct thread_data *get_new_job(void)
 {
        struct thread_data *td = &threads[thread_number++];
 
-       strcpy(td->file_name, filename);
        td->thread_number = thread_number;
+       td->ddir = DDIR_READ;
+       td->bs = global_bs;
+       td->odirect = 1;
+       td->delay_sleep = 0;
+       td->rate = 0;
+       td->sequential = sequential;
+       td->ioprio = 0;
+       memcpy(&td->cpumask, &def_cpumask, sizeof(td->cpumask));
+
+       return td;
+}
+
+void add_job(struct thread_data *td, const char *filename, int prioclass,
+            int prio)
+{
+       strcpy(td->file_name, filename);
        td->stat_fd = -1;
        sem_init(&td->mutex, 1, 1);
        td->min_latency = 10000000;
-       td->ddir = rw;
-       td->ioprio = prio;
-       td->sequential = !random;
-       td->odirect = direct;
-       td->bs = bs;
-       td->delay_sleep = delay;
-       memcpy(&td->cpumask, &cpumask, sizeof(cpumask));
-
-       if (rate)
-               setup_rate(td, rate);
-
-       printf("Client%d: file=%s, rw=%d, prio=%d, seq=%d, odir=%d, bs=%d, rate=%d\n", thread_number, filename, rw, prio, !random, direct, bs, rate);
+       td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio;
+
+       if (td->rate)
+               setup_rate(td);
+
+       printf("Client%d: file=%s, rw=%d, prio=%d, seq=%d, odir=%d, bs=%d, rate=%d\n", td->thread_number, filename, td->ddir, td->ioprio, td->sequential, td->odirect, td->bs, td->rate);
 }
 
 static void fill_cpu_mask(cpu_set_t cpumask, int cpu)
 {
-       int i;
+       unsigned int i;
 
        CPU_ZERO(&cpumask);
 
@@ -581,9 +589,9 @@ void fill_option(const char *input, char *output)
  */
 int parse_jobs_cmd(int argc, char *argv[], int index)
 {
-       int rw, bs, direct, prio, random, prioclass, delay, rate, cpu;
+       struct thread_data *td;
+       unsigned int prio, prioclass, cpu;
        char *string, *filename, *p, *c;
-       cpu_set_t cpumask;
        int i;
 
        string = malloc(256);
@@ -597,23 +605,19 @@ int parse_jobs_cmd(int argc, char *argv[], int index)
                        break;
 
                filename[0] = 0;
-               rw = DDIR_READ;
-               bs = global_bs;
-               direct = 1;
-               prio = 4;
-               random = !sequential;
+               td = get_new_job();
+
                prioclass = 2;
-               delay = 0;
-               rate = 0;
-               memcpy(&cpumask, &def_cpumask, sizeof(cpumask));
+               prio = 4;
+               cpu = 0;
 
                c = strstr(p, "rw=");
                if (c) {
                        c += 3;
                        if (*c == '0')
-                               rw = DDIR_READ;
+                               td->ddir = DDIR_READ;
                        else
-                               rw = DDIR_WRITE;
+                               td->ddir = DDIR_WRITE;
                }
 
                c = strstr(p, "prio=");
@@ -638,31 +642,31 @@ int parse_jobs_cmd(int argc, char *argv[], int index)
                if (c) {
                        c += 3;
                        fill_option(c, string);
-                       bs = strtoul(string, NULL, 10);
-                       bs <<= 10;
+                       td->bs = strtoul(string, NULL, 10);
+                       td->bs <<= 10;
                }
 
                c = strstr(p, "direct=");
                if (c) {
                        c += 7;
                        if (*c != '0')
-                               direct = 1;
+                               td->odirect = 1;
                        else
-                               direct = 0;
+                               td->odirect = 0;
                }
 
                c = strstr(p, "delay=");
                if (c) {
                        c += 6;
                        fill_option(c, string);
-                       delay = strtoul(string, NULL, 10);
+                       td->delay_sleep = strtoul(string, NULL, 10);
                }
 
                c = strstr(p, "rate=");
                if (c) {
                        c += 5;
                        fill_option(c, string);
-                       rate = strtoul(string, NULL, 10);
+                       td->rate = strtoul(string, NULL, 10);
                }
 
                c = strstr(p, "cpumask=");
@@ -670,18 +674,18 @@ int parse_jobs_cmd(int argc, char *argv[], int index)
                        c += 8;
                        fill_option(c, string);
                        cpu = strtoul(string, NULL, 10);
-                       fill_cpu_mask(cpumask, cpu);
+                       fill_cpu_mask(td->cpumask, cpu);
                }
 
 
                c = strstr(p, "random");
                if (c)
-                       random = 1;
+                       td->sequential = 0;
                c = strstr(p, "sequential");
                if (c)
-                       random = 0;
+                       td->sequential = 1;
 
-               add_job(filename, rw, bs, direct, (prioclass << IOPRIO_CLASS_SHIFT) | prio, random, delay, rate, cpumask);
+               add_job(td, filename, prioclass, prio);
        }
 
        free(string);
@@ -689,7 +693,7 @@ int parse_jobs_cmd(int argc, char *argv[], int index)
        return thread_number;
 }
 
-int check_int(char *p, char *name, int *val)
+int check_int(char *p, char *name, unsigned int *val)
 {
        char str[128];
 
@@ -717,8 +721,8 @@ int is_empty(char *line)
 
 int parse_jobs_ini(char *file)
 {
-       int rw, bs, direct, prio, random, prioclass, delay, rate, jobs, cpu;
-       cpu_set_t cpumask;
+       unsigned int prioclass, prio, cpu, jobs;
+       struct thread_data *td;
        char *string, *name;
        fpos_t off;
        FILE *f;
@@ -741,27 +745,22 @@ int parse_jobs_ini(char *file)
 
                name[strlen(name) - 1] = '\0';
 
-               rw = DDIR_READ;
-               bs = global_bs;
-               direct = 1;
-               prio = 4;
-               random = !sequential;
+               td = get_new_job();
+
                prioclass = 2;
-               delay = 0;
-               rate = 0;
-               memcpy(&cpumask, &def_cpumask, sizeof(cpumask));
+               prio = 4;
                cpu = 0;
 
                fgetpos(f, &off);
                while ((p = fgets(string, 4096, f)) != NULL) {
                        if (is_empty(p))
                                break;
-                       if (!check_int(p, "bs", &bs)) {
-                               bs <<= 10;
+                       if (!check_int(p, "bs", &td->bs)) {
+                               td->bs <<= 10;
                                fgetpos(f, &off);
                                continue;
                        }
-                       if (!check_int(p, "rw", &rw)) {
+                       if (!check_int(p, "rw", &td->ddir)) {
                                fgetpos(f, &off);
                                continue;
                        }
@@ -773,37 +772,37 @@ int parse_jobs_ini(char *file)
                                fgetpos(f, &off);
                                continue;
                        }
-                       if (!check_int(p, "direct", &direct)) {
+                       if (!check_int(p, "direct", &td->odirect)) {
                                fgetpos(f, &off);
                                continue;
                        }
-                       if (!check_int(p, "rate", &rate)) {
+                       if (!check_int(p, "rate", &td->rate)) {
                                fgetpos(f, &off);
                                continue;
                        }
-                       if (!check_int(p, "delay", &delay)) {
+                       if (!check_int(p, "delay", &td->delay_sleep)) {
                                fgetpos(f, &off);
                                continue;
                        }
                        if (!check_int(p, "cpumask", &cpu)) {
-                               fill_cpu_mask(cpumask, cpu);
+                               fill_cpu_mask(td->cpumask, cpu);
                                fgetpos(f, &off);
                                continue;
                        }
                        if (!strcmp(p, "sequential")) {
-                               sequential = 1;
+                               td->sequential = 1;
                                fgetpos(f, &off);
                                continue;
                        }
                        if (!strcmp(p, "random")) {
-                               sequential = 0;
+                               td->sequential = 0;
                                fgetpos(f, &off);
                                continue;
                        }
                }
                fsetpos(f, &off);
 
-               add_job(name, rw, bs, direct, (prioclass << IOPRIO_CLASS_SHIFT) | prio, random, delay, rate, cpumask);
+               add_job(td, name, prioclass, prio);
                jobs++;
        }