[PATCH] Easy process shared semaphores
[fio.git] / init.c
diff --git a/init.c b/init.c
index d2122e3fd3c7f17361b0f1d7ced0dab4fcabc510..8c6b2e1e336c7a572902f346336969157da3231b 100644 (file)
--- a/init.c
+++ b/init.c
@@ -55,55 +55,6 @@ int write_bw_log = 0;
 int exitall_on_terminate = 0;
 unsigned long long mlock_size = 0;
 
-static int setup_rate(struct thread_data *td)
-{
-       int nr_reads_per_sec;
-
-       if (!td->rate)
-               return 0;
-
-       if (td->rate < td->ratemin) {
-               fprintf(stderr, "min rate larger than nominal rate\n");
-               return -1;
-       }
-
-       nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
-       td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
-       td->rate_pending_usleep = 0;
-       return 0;
-}
-
-static void setup_log(struct io_log **log)
-{
-       struct io_log *l = malloc(sizeof(*l));
-
-       l->nr_samples = 0;
-       l->max_samples = 1024;
-       l->log = malloc(l->max_samples * sizeof(struct io_sample));
-       *log = l;
-}
-
-void finish_log(struct thread_data *td, struct io_log *log, const char *name)
-{
-       char file_name[256];
-       FILE *f;
-       unsigned int i;
-
-       snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
-       f = fopen(file_name, "w");
-       if (!f) {
-               perror("fopen log");
-               return;
-       }
-
-       for (i = 0; i < log->nr_samples; i++)
-               fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
-
-       fclose(f);
-       free(log->log);
-       free(log);
-}
-
 static struct thread_data *get_new_job(int global, struct thread_data *parent)
 {
        struct thread_data *td;
@@ -223,7 +174,7 @@ static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
        } else
                strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
 
-       sem_init(&td->mutex, 0, 0);
+       fio_sem_init(&td->mutex, 0);
 
        td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
        td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
@@ -251,10 +202,13 @@ static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
        if (write_bw_log)
                setup_log(&td->bw_log);
 
+       if (td->name[0] == '\0')
+               snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
+
        ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
 
        if (!job_add_num)
-               printf("Client%d (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->thread_number, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
+               printf("%s: (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
        else if (job_add_num == 1)
                printf("...\n");
 
@@ -302,9 +256,9 @@ int init_random_state(struct thread_data *td)
 
        close(fd);
 
-       srand48_r(seeds[0], &td->bsrange_state);
-       srand48_r(seeds[1], &td->verify_state);
-       srand48_r(seeds[2], &td->rwmix_state);
+       os_random_seed(seeds[0], &td->bsrange_state);
+       os_random_seed(seeds[1], &td->verify_state);
+       os_random_seed(seeds[2], &td->rwmix_state);
 
        if (td->sequential)
                return 0;
@@ -318,7 +272,7 @@ int init_random_state(struct thread_data *td)
        td->num_maps = num_maps;
        memset(td->file_map, 0, num_maps * sizeof(long));
 
-       srand48_r(seeds[3], &td->random_state);
+       os_random_seed(seeds[3], &td->random_state);
        return 0;
 }
 
@@ -575,7 +529,7 @@ static int str_rw_cb(struct thread_data *td, char *mem)
                return 0;
        }
 
-       fprintf(stderr, "bad data direction: %s\n", mem);
+       fprintf(stderr, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
        return 1;
 }
 
@@ -592,7 +546,7 @@ static int str_verify_cb(struct thread_data *td, char *mem)
                return 0;
        }
 
-       fprintf(stderr, "bad verify type: %s\n", mem);
+       fprintf(stderr, "fio: verify types: md5, crc32\n");
        return 1;
 }
 
@@ -609,7 +563,7 @@ static int str_mem_cb(struct thread_data *td, char *mem)
                return 0;
        }
 
-       fprintf(stderr, "bad mem type: %s\n", mem);
+       fprintf(stderr, "fio: mem type: malloc, shm, mmap\n");
        return 1;
 }
 
@@ -642,34 +596,13 @@ static int str_ioengine_cb(struct thread_data *td, char *str)
                return 0;
        }
 
-       fprintf(stderr, "bad ioengine type: %s\n", str);
+       fprintf(stderr, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
        return 1;
 }
 
-static int str_iolog_cb(struct thread_data *td, char *file)
-{
-       td->iolog_file = strdup(file);
-       return 0;
-}
-
-static int str_prerun_cb(struct thread_data *td, char *file)
-{
-       td->exec_prerun = strdup(file);
-       return 0;
-}
-
-static int str_postrun_cb(struct thread_data *td, char *file)
-{
-       td->exec_postrun = strdup(file);
-       return 0;
-}
-
-static int str_iosched_cb(struct thread_data *td, char *file)
-{
-       td->ioscheduler = strdup(file);
-       return 0;
-}
-
+/*
+ * This is our [ini] type file parser.
+ */
 int parse_jobs_ini(char *file)
 {
        unsigned int prioclass, prio, cpu, global, il;
@@ -680,6 +613,7 @@ int parse_jobs_ini(char *file)
        fpos_t off;
        FILE *f;
        char *p;
+       int ret = 0;
 
        f = fopen(file, "r");
        if (!f) {
@@ -692,6 +626,8 @@ int parse_jobs_ini(char *file)
        tmpbuf = malloc(4096);
 
        while ((p = fgets(string, 4096, f)) != NULL) {
+               if (ret)
+                       break;
                if (is_empty_or_comment(p))
                        continue;
                if (sscanf(p, "[%s]", name) != 1)
@@ -702,8 +638,10 @@ int parse_jobs_ini(char *file)
                name[strlen(name) - 1] = '\0';
 
                td = get_new_job(global, &def_thread);
-               if (!td)
-                       return 1;
+               if (!td) {
+                       ret = 1;
+                       break;
+               }
 
                fgetpos(f, &off);
                while ((p = fgets(string, 4096, f)) != NULL) {
@@ -717,7 +655,8 @@ int parse_jobs_ini(char *file)
                        if (!check_int(p, "prio", &prio)) {
 #ifndef FIO_HAVE_IOPRIO
                                fprintf(stderr, "io priorities not available\n");
-                               return 1;
+                               ret = 1;
+                               break;
 #endif
                                td->ioprio |= prio;
                                fgetpos(f, &off);
@@ -726,11 +665,13 @@ int parse_jobs_ini(char *file)
                        if (!check_int(p, "prioclass", &prioclass)) {
 #ifndef FIO_HAVE_IOPRIO
                                fprintf(stderr, "io priorities not available\n");
-                               return 1;
-#endif
+                               ret = 1;
+                               break;
+#else
                                td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
                                fgetpos(f, &off);
                                continue;
+#endif
                        }
                        if (!check_int(p, "direct", &td->odirect)) {
                                fgetpos(f, &off);
@@ -755,7 +696,8 @@ int parse_jobs_ini(char *file)
                        if (!check_int(p, "cpumask", &cpu)) {
 #ifndef FIO_HAVE_CPU_AFFINITY
                                fprintf(stderr, "cpu affinity not available\n");
-                               return 1;
+                               ret = 1;
+                               break;
 #endif
                                fill_cpu_mask(td->cpumask, cpu);
                                fgetpos(f, &off);
@@ -877,6 +819,11 @@ int parse_jobs_ini(char *file)
                                fgetpos(f, &off);
                                continue;
                        }
+                       if (!check_strstore(p, "name", tmpbuf)) {
+                               snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
+                               fgetpos(f, &off);
+                               continue;
+                       }
                        if (!check_str(p, "mem", str_mem_cb, td)) {
                                fgetpos(f, &off);
                                continue;
@@ -913,45 +860,64 @@ int parse_jobs_ini(char *file)
                                fgetpos(f, &off);
                                continue;
                        }
-                       if (!check_str(p, "iolog", str_iolog_cb, td)) {
+                       if (!check_strstore(p, "iolog", tmpbuf)) {
+                               td->iolog_file = strdup(tmpbuf);
                                td->read_iolog = 1;
                                td->write_iolog = 0;
                                fgetpos(f, &off);
                                continue;
                        }
                        if (!td->read_iolog &&
-                           !check_str(p, "write_iolog", str_iolog_cb, td)) {
+                           !check_strstore(p, "write_iolog", tmpbuf)) {
+                               td->iolog_file = strdup(tmpbuf);
                                td->write_iolog = 1;
                                fgetpos(f, &off);
                                continue;
                        }
-                       if (!check_str(p, "exec_prerun", str_prerun_cb, td)) {
+                       if (!check_strstore(p, "exec_prerun", tmpbuf)) {
+                               td->exec_prerun = strdup(tmpbuf);
                                fgetpos(f, &off);
                                continue;
                        }
-                       if (!check_str(p, "exec_postrun", str_postrun_cb, td)) {
+                       if (!check_strstore(p, "exec_postrun", tmpbuf)) {
+                               td->exec_postrun = strdup(tmpbuf);
                                fgetpos(f, &off);
                                continue;
                        }
-                       if (!check_str(p, "ioscheduler", str_iosched_cb, td)) {
+                       if (!check_strstore(p, "ioscheduler", tmpbuf)) {
+#ifndef FIO_HAVE_IOSCHED_SWITCH
+                               fprintf(stderr, "io scheduler switching not available\n");
+                               ret = 1;
+                               break;
+#else
+                               td->ioscheduler = strdup(tmpbuf);
                                fgetpos(f, &off);
                                continue;
+#endif
                        }
 
+                       /*
+                        * Don't break here, continue parsing options so we
+                        * dump all the bad ones. Makes trial/error fixups
+                        * easier on the user.
+                        */
                        printf("Client%d: bad option %s\n",td->thread_number,p);
-                       return 1;
+                       ret = 1;
                }
-               fsetpos(f, &off);
 
-               if (add_job(td, name, 0))
-                       return 1;
+               if (!ret) {
+                       fsetpos(f, &off);
+                       ret = add_job(td, name, 0);
+               }
+               if (ret)
+                       break;
        }
 
        free(string);
        free(name);
        free(tmpbuf);
        fclose(f);
-       return 0;
+       return ret;
 }
 
 static int fill_def_thread(void)
@@ -1124,10 +1090,8 @@ int parse_options(int argc, char *argv[])
                return 1;
        }
 
-       if (parse_jobs_ini(ini_file)) {
-               usage(argv[0]);
+       if (parse_jobs_ini(ini_file))
                return 1;
-       }
 
        return 0;
 }