options: unify the bssplit/zone split code
authorJens Axboe <axboe@fb.com>
Mon, 7 Mar 2016 16:29:13 +0000 (09:29 -0700)
committerJens Axboe <axboe@fb.com>
Mon, 7 Mar 2016 16:29:13 +0000 (09:29 -0700)
Most of that parsing code was duplicated, apply a bit of abstraction
to it. Especially because it's not that pretty, so rather have one
copy of that nastiness.

Signed-off-by: Jens Axboe <axboe@fb.com>
options.c

index bc2d0ed..8322dda 100644 (file)
--- a/options.c
+++ b/options.c
@@ -44,7 +44,7 @@ static int bs_cmp(const void *p1, const void *p2)
        return (int) bsp1->perc - (int) bsp2->perc;
 }
 
-static int bssplit_ddir(struct thread_options *o, int ddir, char *str)
+static int bssplit_ddir(struct thread_options *o, enum fio_ddir ddir, char *str)
 {
        struct bssplit *bssplit;
        unsigned int i, perc, perc_missing;
@@ -148,57 +148,69 @@ static int bssplit_ddir(struct thread_options *o, int ddir, char *str)
        return 0;
 }
 
-static int str_bssplit_cb(void *data, const char *input)
+typedef int (split_parse_fn)(struct thread_options *, enum fio_ddir, char *);
+
+static int str_split_parse(struct thread_data *td, char *str, split_parse_fn *fn)
 {
-       struct thread_data *td = data;
-       char *str, *p, *odir, *ddir;
+       char *odir, *ddir;
        int ret = 0;
 
-       if (parse_dryrun())
-               return 0;
-
-       p = str = strdup(input);
-
-       strip_blank_front(&str);
-       strip_blank_end(str);
-
        odir = strchr(str, ',');
        if (odir) {
                ddir = strchr(odir + 1, ',');
                if (ddir) {
-                       ret = bssplit_ddir(&td->o, DDIR_TRIM, ddir + 1);
+                       ret = fn(&td->o, DDIR_TRIM, ddir + 1);
                        if (!ret)
                                *ddir = '\0';
                } else {
                        char *op;
 
                        op = strdup(odir + 1);
-                       ret = bssplit_ddir(&td->o, DDIR_TRIM, op);
+                       ret = fn(&td->o, DDIR_TRIM, op);
 
                        free(op);
                }
                if (!ret)
-                       ret = bssplit_ddir(&td->o, DDIR_WRITE, odir + 1);
+                       ret = fn(&td->o, DDIR_WRITE, odir + 1);
                if (!ret) {
                        *odir = '\0';
-                       ret = bssplit_ddir(&td->o, DDIR_READ, str);
+                       ret = fn(&td->o, DDIR_READ, str);
                }
        } else {
                char *op;
 
                op = strdup(str);
-               ret = bssplit_ddir(&td->o, DDIR_WRITE, op);
+               ret = fn(&td->o, DDIR_WRITE, op);
                free(op);
 
                if (!ret) {
                        op = strdup(str);
-                       ret = bssplit_ddir(&td->o, DDIR_TRIM, op);
+                       ret = fn(&td->o, DDIR_TRIM, op);
                        free(op);
                }
                if (!ret)
-                       ret = bssplit_ddir(&td->o, DDIR_READ, str);
+                       ret = fn(&td->o, DDIR_READ, str);
        }
 
+       return ret;
+}
+
+static int str_bssplit_cb(void *data, const char *input)
+{
+       struct thread_data *td = data;
+       char *str, *p;
+       int ret = 0;
+
+       if (parse_dryrun())
+               return 0;
+
+       p = str = strdup(input);
+
+       strip_blank_front(&str);
+       strip_blank_end(str);
+
+       ret = str_split_parse(td, str, bssplit_ddir);
+
        free(p);
        return ret;
 }
@@ -714,7 +726,8 @@ static int zone_cmp(const void *p1, const void *p2)
        return (int) zsp2->access_perc - (int) zsp1->access_perc;
 }
 
-static int zone_split_ddir(struct thread_options *o, int ddir, char *str)
+static int zone_split_ddir(struct thread_options *o, enum fio_ddir ddir,
+                          char *str)
 {
        struct zone_split *zsplit;
        unsigned int i, perc, perc_missing, sperc, sperc_missing;
@@ -869,7 +882,7 @@ static void td_zone_gen_index(struct thread_data *td)
 
 static int parse_zoned_distribution(struct thread_data *td, const char *input)
 {
-       char *str, *p, *odir, *ddir;
+       char *str, *p;
        int i, ret = 0;
 
        p = str = strdup(input);
@@ -885,42 +898,7 @@ static int parse_zoned_distribution(struct thread_data *td, const char *input)
        }
        str += strlen("zoned:");
 
-       odir = strchr(str, ',');
-       if (odir) {
-               ddir = strchr(odir + 1, ',');
-               if (ddir) {
-                       ret = zone_split_ddir(&td->o, DDIR_TRIM, ddir + 1);
-                       if (!ret)
-                               *ddir = '\0';
-               } else {
-                       char *op;
-
-                       op = strdup(odir + 1);
-                       ret = zone_split_ddir(&td->o, DDIR_TRIM, op);
-
-                       free(op);
-               }
-               if (!ret)
-                       ret = zone_split_ddir(&td->o, DDIR_WRITE, odir + 1);
-               if (!ret) {
-                       *odir = '\0';
-                       ret = zone_split_ddir(&td->o, DDIR_READ, str);
-               }
-       } else {
-               char *op;
-
-               op = strdup(str);
-               ret = zone_split_ddir(&td->o, DDIR_WRITE, op);
-               free(op);
-
-               if (!ret) {
-                       op = strdup(str);
-                       ret = zone_split_ddir(&td->o, DDIR_TRIM, op);
-                       free(op);
-               }
-               if (!ret)
-                       ret = zone_split_ddir(&td->o, DDIR_READ, str);
-       }
+       ret = str_split_parse(td, str, zone_split_ddir);
 
        free(p);