d259c998c39215c3e0914b5aa1a57829bd464f9f
[fio.git] / init.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <ctype.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <sys/ipc.h>
9 #include <sys/shm.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12
13 #include "fio.h"
14
15 #define DEF_BS                  (4096)
16 #define DEF_TIMEOUT             (0)
17 #define DEF_RATE_CYCLE          (1000)
18 #define DEF_ODIRECT             (1)
19 #define DEF_IO_ENGINE           (FIO_SYNCIO)
20 #define DEF_IO_ENGINE_NAME      "sync"
21 #define DEF_SEQUENTIAL          (1)
22 #define DEF_RAND_REPEAT         (1)
23 #define DEF_OVERWRITE           (1)
24 #define DEF_CREATE              (1)
25 #define DEF_INVALIDATE          (1)
26 #define DEF_SYNCIO              (0)
27 #define DEF_RANDSEED            (0xb1899bedUL)
28 #define DEF_BWAVGTIME           (500)
29 #define DEF_CREATE_SER          (1)
30 #define DEF_CREATE_FSYNC        (1)
31 #define DEF_LOOPS               (1)
32 #define DEF_VERIFY              (0)
33 #define DEF_STONEWALL           (0)
34 #define DEF_NUMJOBS             (1)
35 #define DEF_USE_THREAD          (0)
36 #define DEF_FILE_SIZE           (1024 * 1024 * 1024UL)
37 #define DEF_ZONE_SIZE           (0)
38 #define DEF_ZONE_SKIP           (0)
39 #define DEF_RWMIX_CYCLE         (500)
40 #define DEF_RWMIX_READ          (50)
41 #define DEF_NICE                (0)
42
43 static char fio_version_string[] = "fio 1.4";
44
45 static int repeatable = DEF_RAND_REPEAT;
46 static char *ini_file;
47 static int max_jobs = MAX_JOBS;
48
49 struct thread_data def_thread;
50 struct thread_data *threads = NULL;
51
52 int rate_quit = 0;
53 int write_lat_log = 0;
54 int write_bw_log = 0;
55 int exitall_on_terminate = 0;
56 unsigned long long mlock_size = 0;
57
58 static int setup_rate(struct thread_data *td)
59 {
60         int nr_reads_per_sec;
61
62         if (!td->rate)
63                 return 0;
64
65         if (td->rate < td->ratemin) {
66                 fprintf(stderr, "min rate larger than nominal rate\n");
67                 return -1;
68         }
69
70         nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
71         td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
72         td->rate_pending_usleep = 0;
73         return 0;
74 }
75
76 static void setup_log(struct io_log **log)
77 {
78         struct io_log *l = malloc(sizeof(*l));
79
80         l->nr_samples = 0;
81         l->max_samples = 1024;
82         l->log = malloc(l->max_samples * sizeof(struct io_sample));
83         *log = l;
84 }
85
86 void finish_log(struct thread_data *td, struct io_log *log, const char *name)
87 {
88         char file_name[256];
89         FILE *f;
90         unsigned int i;
91
92         snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
93         f = fopen(file_name, "w");
94         if (!f) {
95                 perror("fopen log");
96                 return;
97         }
98
99         for (i = 0; i < log->nr_samples; i++)
100                 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
101
102         fclose(f);
103         free(log->log);
104         free(log);
105 }
106
107 static struct thread_data *get_new_job(int global, struct thread_data *parent)
108 {
109         struct thread_data *td;
110
111         if (global)
112                 return &def_thread;
113         if (thread_number >= max_jobs)
114                 return NULL;
115
116         td = &threads[thread_number++];
117         if (parent)
118                 *td = *parent;
119         else
120                 memset(td, 0, sizeof(*td));
121
122         td->fd = -1;
123         td->thread_number = thread_number;
124
125         td->ddir = parent->ddir;
126         td->ioprio = parent->ioprio;
127         td->sequential = parent->sequential;
128         td->bs = parent->bs;
129         td->min_bs = parent->min_bs;
130         td->max_bs = parent->max_bs;
131         td->odirect = parent->odirect;
132         td->thinktime = parent->thinktime;
133         td->fsync_blocks = parent->fsync_blocks;
134         td->start_delay = parent->start_delay;
135         td->timeout = parent->timeout;
136         td->io_engine = parent->io_engine;
137         td->create_file = parent->create_file;
138         td->overwrite = parent->overwrite;
139         td->invalidate_cache = parent->invalidate_cache;
140         td->file_size = parent->file_size;
141         td->file_offset = parent->file_offset;
142         td->zone_size = parent->zone_size;
143         td->zone_skip = parent->zone_skip;
144         td->rate = parent->rate;
145         td->ratemin = parent->ratemin;
146         td->ratecycle = parent->ratecycle;
147         td->iodepth = parent->iodepth;
148         td->sync_io = parent->sync_io;
149         td->mem_type = parent->mem_type;
150         td->bw_avg_time = parent->bw_avg_time;
151         td->create_serialize = parent->create_serialize;
152         td->create_fsync = parent->create_fsync;
153         td->loops = parent->loops;
154         td->verify = parent->verify;
155         td->stonewall = parent->stonewall;
156         td->numjobs = parent->numjobs;
157         td->use_thread = parent->use_thread;
158         td->do_disk_util = parent->do_disk_util;
159         memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
160         strcpy(td->io_engine_name, parent->io_engine_name);
161
162         return td;
163 }
164
165 static void put_job(struct thread_data *td)
166 {
167         memset(&threads[td->thread_number - 1], 0, sizeof(*td));
168         thread_number--;
169 }
170
171 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
172 {
173         char *ddir_str[] = { "read", "write", "randread", "randwrite",
174                              "rw", NULL, "randrw" };
175         struct stat sb;
176         int numjobs, ddir;
177
178 #ifndef FIO_HAVE_LIBAIO
179         if (td->io_engine == FIO_LIBAIO) {
180                 fprintf(stderr, "Linux libaio not available\n");
181                 return 1;
182         }
183 #endif
184 #ifndef FIO_HAVE_POSIXAIO
185         if (td->io_engine == FIO_POSIXAIO) {
186                 fprintf(stderr, "posix aio not available\n");
187                 return 1;
188         }
189 #endif
190
191         /*
192          * the def_thread is just for options, it's not a real job
193          */
194         if (td == &def_thread)
195                 return 0;
196
197         if (td->io_engine & FIO_SYNCIO)
198                 td->iodepth = 1;
199         else {
200                 if (!td->iodepth)
201                         td->iodepth = 1;
202         }
203
204         /*
205          * only really works for sequential io for now
206          */
207         if (td->zone_size && !td->sequential)
208                 td->zone_size = 0;
209
210         td->filetype = FIO_TYPE_FILE;
211         if (!stat(jobname, &sb)) {
212                 if (S_ISBLK(sb.st_mode))
213                         td->filetype = FIO_TYPE_BD;
214                 else if (S_ISCHR(sb.st_mode))
215                         td->filetype = FIO_TYPE_CHAR;
216         }
217
218         if (td->filetype == FIO_TYPE_FILE) {
219                 if (td->directory && td->directory[0] != '\0')
220                         sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
221                 else
222                         sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
223         } else
224                 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
225
226         sem_init(&td->mutex, 0, 0);
227
228         td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
229         td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
230         td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
231
232         if (td->min_bs == -1U)
233                 td->min_bs = td->bs;
234         if (td->max_bs == -1U)
235                 td->max_bs = td->bs;
236         if (td_read(td) && !td_rw(td))
237                 td->verify = 0;
238
239         if (td->stonewall && td->thread_number > 1)
240                 groupid++;
241
242         td->groupid = groupid;
243
244         if (setup_rate(td))
245                 goto err;
246
247         if (write_lat_log) {
248                 setup_log(&td->slat_log);
249                 setup_log(&td->clat_log);
250         }
251         if (write_bw_log)
252                 setup_log(&td->bw_log);
253
254         if (td->name[0] == '\0')
255                 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
256
257         ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
258
259         if (!job_add_num)
260                 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);
261         else if (job_add_num == 1)
262                 printf("...\n");
263
264         /*
265          * recurse add identical jobs, clear numjobs and stonewall options
266          * as they don't apply to sub-jobs
267          */
268         numjobs = td->numjobs;
269         while (--numjobs) {
270                 struct thread_data *td_new = get_new_job(0, td);
271
272                 if (!td_new)
273                         goto err;
274
275                 td_new->numjobs = 1;
276                 td_new->stonewall = 0;
277                 td_new->jobnum = numjobs;
278                 job_add_num = numjobs - 1;
279
280                 if (add_job(td_new, jobname, job_add_num))
281                         goto err;
282         }
283         return 0;
284 err:
285         put_job(td);
286         return -1;
287 }
288
289 int init_random_state(struct thread_data *td)
290 {
291         unsigned long seeds[4];
292         int fd, num_maps, blocks;
293
294         fd = open("/dev/urandom", O_RDONLY);
295         if (fd == -1) {
296                 td_verror(td, errno);
297                 return 1;
298         }
299
300         if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
301                 td_verror(td, EIO);
302                 close(fd);
303                 return 1;
304         }
305
306         close(fd);
307
308         srand48_r(seeds[0], &td->bsrange_state);
309         srand48_r(seeds[1], &td->verify_state);
310         srand48_r(seeds[2], &td->rwmix_state);
311
312         if (td->sequential)
313                 return 0;
314
315         if (repeatable)
316                 seeds[3] = DEF_RANDSEED;
317
318         blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
319         num_maps = blocks / BLOCKS_PER_MAP;
320         td->file_map = malloc(num_maps * sizeof(long));
321         td->num_maps = num_maps;
322         memset(td->file_map, 0, num_maps * sizeof(long));
323
324         srand48_r(seeds[3], &td->random_state);
325         return 0;
326 }
327
328 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
329 {
330 #ifdef FIO_HAVE_CPU_AFFINITY
331         unsigned int i;
332
333         CPU_ZERO(&cpumask);
334
335         for (i = 0; i < sizeof(int) * 8; i++) {
336                 if ((1 << i) & cpu)
337                         CPU_SET(i, &cpumask);
338         }
339 #endif
340 }
341
342 static unsigned long get_mult(char c)
343 {
344         switch (c) {
345                 case 'k':
346                 case 'K':
347                         return 1024;
348                 case 'm':
349                 case 'M':
350                         return 1024 * 1024;
351                 case 'g':
352                 case 'G':
353                         return 1024 * 1024 * 1024;
354                 default:
355                         return 1;
356         }
357 }
358
359 /*
360  * convert string after '=' into decimal value, noting any size suffix
361  */
362 static int str_cnv(char *p, unsigned long long *val)
363 {
364         char *str;
365         int len;
366
367         str = strchr(p, '=');
368         if (!str)
369                 return 1;
370
371         str++;
372         len = strlen(str);
373
374         *val = strtoul(str, NULL, 10);
375         if (*val == ULONG_MAX && errno == ERANGE)
376                 return 1;
377
378         *val *= get_mult(str[len - 1]);
379         return 0;
380 }
381
382 static int check_strcnv(char *p, char *name, unsigned long long *val)
383 {
384         if (strncmp(p, name, strlen(name) - 1))
385                 return 1;
386
387         return str_cnv(p, val);
388 }
389
390 static void strip_blank_front(char **p)
391 {
392         char *s = *p;
393
394         while (isspace(*s))
395                 s++;
396 }
397
398 static void strip_blank_end(char *p)
399 {
400         char *s = p + strlen(p) - 1;
401
402         while (isspace(*s) || iscntrl(*s))
403                 s--;
404
405         *(s + 1) = '\0';
406 }
407
408 typedef int (str_cb_fn)(struct thread_data *, char *);
409
410 static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
411 {
412         char *s;
413
414         if (strncmp(p, name, strlen(name)))
415                 return 1;
416
417         s = strstr(p, name);
418         if (!s)
419                 return 1;
420
421         s = strchr(s, '=');
422         if (!s)
423                 return 1;
424
425         s++;
426         strip_blank_front(&s);
427         return cb(td, s);
428 }
429
430 static int check_strstore(char *p, char *name, char *dest)
431 {
432         char *s;
433
434         if (strncmp(p, name, strlen(name)))
435                 return 1;
436
437         s = strstr(p, name);
438         if (!s)
439                 return 1;
440
441         s = strchr(p, '=');
442         if (!s)
443                 return 1;
444
445         s++;
446         strip_blank_front(&s);
447
448         strcpy(dest, s);
449         return 0;
450 }
451
452 static int __check_range(char *str, unsigned long *val)
453 {
454         char suffix;
455
456         if (sscanf(str, "%lu%c", val, &suffix) == 2) {
457                 *val *= get_mult(suffix);
458                 return 0;
459         }
460
461         if (sscanf(str, "%lu", val) == 1)
462                 return 0;
463
464         return 1;
465 }
466
467 static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
468 {
469         char option[128];
470         char *str, *p1, *p2;
471
472         if (strncmp(p, name, strlen(name)))
473                 return 1;
474
475         strcpy(option, p);
476         p = option;
477
478         str = strstr(p, name);
479         if (!str)
480                 return 1;
481
482         p += strlen(name);
483
484         str = strchr(p, '=');
485         if (!str)
486                 return 1;
487
488         /*
489          * 'p' now holds whatever is after the '=' sign
490          */
491         p1 = str + 1;
492
493         /*
494          * terminate p1 at the '-' sign
495          */
496         p = strchr(p1, '-');
497         if (!p)
498                 return 1;
499
500         p2 = p + 1;
501         *p = '\0';
502
503         if (!__check_range(p1, s) && !__check_range(p2, e))
504                 return 0;
505
506         return 1;
507 }
508
509 static int check_int(char *p, char *name, unsigned int *val)
510 {
511         char *str;
512
513         if (strncmp(p, name, strlen(name)))
514                 return 1;
515
516         str = strstr(p, name);
517         if (!str)
518                 return 1;
519
520         str = strchr(p, '=');
521         if (!str)
522                 return 1;
523
524         str++;
525
526         if (sscanf(str, "%u", val) == 1)
527                 return 0;
528
529         return 1;
530 }
531
532 static int check_strset(char *p, char *name)
533 {
534         return strncmp(p, name, strlen(name));
535 }
536
537 static int is_empty_or_comment(char *line)
538 {
539         unsigned int i;
540
541         for (i = 0; i < strlen(line); i++) {
542                 if (line[i] == ';')
543                         return 1;
544                 if (!isspace(line[i]) && !iscntrl(line[i]))
545                         return 0;
546         }
547
548         return 1;
549 }
550
551 static int str_rw_cb(struct thread_data *td, char *mem)
552 {
553         if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
554                 td->ddir = DDIR_READ;
555                 td->sequential = 1;
556                 return 0;
557         } else if (!strncmp(mem, "randread", 8)) {
558                 td->ddir = DDIR_READ;
559                 td->sequential = 0;
560                 return 0;
561         } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
562                 td->ddir = DDIR_WRITE;
563                 td->sequential = 1;
564                 return 0;
565         } else if (!strncmp(mem, "randwrite", 9)) {
566                 td->ddir = DDIR_WRITE;
567                 td->sequential = 0;
568                 return 0;
569         } else if (!strncmp(mem, "rw", 2)) {
570                 td->ddir = 0;
571                 td->iomix = 1;
572                 td->sequential = 1;
573                 return 0;
574         } else if (!strncmp(mem, "randrw", 6)) {
575                 td->ddir = 0;
576                 td->iomix = 1;
577                 td->sequential = 0;
578                 return 0;
579         }
580
581         fprintf(stderr, "fio: data direction: read, write, randread, randwrite, rw, randrw\n");
582         return 1;
583 }
584
585 static int str_verify_cb(struct thread_data *td, char *mem)
586 {
587         if (!strncmp(mem, "0", 1)) {
588                 td->verify = VERIFY_NONE;
589                 return 0;
590         } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
591                 td->verify = VERIFY_MD5;
592                 return 0;
593         } else if (!strncmp(mem, "crc32", 5)) {
594                 td->verify = VERIFY_CRC32;
595                 return 0;
596         }
597
598         fprintf(stderr, "fio: verify types: md5, crc32\n");
599         return 1;
600 }
601
602 static int str_mem_cb(struct thread_data *td, char *mem)
603 {
604         if (!strncmp(mem, "malloc", 6)) {
605                 td->mem_type = MEM_MALLOC;
606                 return 0;
607         } else if (!strncmp(mem, "shm", 3)) {
608                 td->mem_type = MEM_SHM;
609                 return 0;
610         } else if (!strncmp(mem, "mmap", 4)) {
611                 td->mem_type = MEM_MMAP;
612                 return 0;
613         }
614
615         fprintf(stderr, "fio: mem type: malloc, shm, mmap\n");
616         return 1;
617 }
618
619 static int str_ioengine_cb(struct thread_data *td, char *str)
620 {
621         if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
622             !strncmp(str, "libaio", 6)) {
623                 strcpy(td->io_engine_name, "libaio");
624                 td->io_engine = FIO_LIBAIO;
625                 return 0;
626         } else if (!strncmp(str, "posixaio", 8)) {
627                 strcpy(td->io_engine_name, "posixaio");
628                 td->io_engine = FIO_POSIXAIO;
629                 return 0;
630         } else if (!strncmp(str, "sync", 4)) {
631                 strcpy(td->io_engine_name, "sync");
632                 td->io_engine = FIO_SYNCIO;
633                 return 0;
634         } else if (!strncmp(str, "mmap", 4)) {
635                 strcpy(td->io_engine_name, "mmap");
636                 td->io_engine = FIO_MMAPIO;
637                 return 0;
638         } else if (!strncmp(str, "sgio", 4)) {
639                 strcpy(td->io_engine_name, "sgio");
640                 td->io_engine = FIO_SGIO;
641                 return 0;
642         } else if (!strncmp(str, "splice", 6)) {
643                 strcpy(td->io_engine_name, "splice");
644                 td->io_engine = FIO_SPLICEIO;
645                 return 0;
646         }
647
648         fprintf(stderr, "fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice\n");
649         return 1;
650 }
651
652 /*
653  * This is our [ini] type file parser.
654  */
655 int parse_jobs_ini(char *file)
656 {
657         unsigned int prioclass, prio, cpu, global, il;
658         unsigned long long ull;
659         unsigned long ul1, ul2;
660         struct thread_data *td;
661         char *string, *name, *tmpbuf;
662         fpos_t off;
663         FILE *f;
664         char *p;
665         int ret = 0;
666
667         f = fopen(file, "r");
668         if (!f) {
669                 perror("fopen job file");
670                 return 1;
671         }
672
673         string = malloc(4096);
674         name = malloc(256);
675         tmpbuf = malloc(4096);
676
677         while ((p = fgets(string, 4096, f)) != NULL) {
678                 if (ret)
679                         break;
680                 if (is_empty_or_comment(p))
681                         continue;
682                 if (sscanf(p, "[%s]", name) != 1)
683                         continue;
684
685                 global = !strncmp(name, "global", 6);
686
687                 name[strlen(name) - 1] = '\0';
688
689                 td = get_new_job(global, &def_thread);
690                 if (!td) {
691                         ret = 1;
692                         break;
693                 }
694
695                 fgetpos(f, &off);
696                 while ((p = fgets(string, 4096, f)) != NULL) {
697                         if (is_empty_or_comment(p))
698                                 continue;
699                         if (strstr(p, "["))
700                                 break;
701                         strip_blank_front(&p);
702                         strip_blank_end(p);
703
704                         if (!check_int(p, "prio", &prio)) {
705 #ifndef FIO_HAVE_IOPRIO
706                                 fprintf(stderr, "io priorities not available\n");
707                                 ret = 1;
708                                 break;
709 #endif
710                                 td->ioprio |= prio;
711                                 fgetpos(f, &off);
712                                 continue;
713                         }
714                         if (!check_int(p, "prioclass", &prioclass)) {
715 #ifndef FIO_HAVE_IOPRIO
716                                 fprintf(stderr, "io priorities not available\n");
717                                 ret = 1;
718                                 break;
719 #endif
720                                 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
721                                 fgetpos(f, &off);
722                                 continue;
723                         }
724                         if (!check_int(p, "direct", &td->odirect)) {
725                                 fgetpos(f, &off);
726                                 continue;
727                         }
728                         if (!check_int(p, "rate", &td->rate)) {
729                                 fgetpos(f, &off);
730                                 continue;
731                         }
732                         if (!check_int(p, "ratemin", &td->ratemin)) {
733                                 fgetpos(f, &off);
734                                 continue;
735                         }
736                         if (!check_int(p, "ratecycle", &td->ratecycle)) {
737                                 fgetpos(f, &off);
738                                 continue;
739                         }
740                         if (!check_int(p, "thinktime", &td->thinktime)) {
741                                 fgetpos(f, &off);
742                                 continue;
743                         }
744                         if (!check_int(p, "cpumask", &cpu)) {
745 #ifndef FIO_HAVE_CPU_AFFINITY
746                                 fprintf(stderr, "cpu affinity not available\n");
747                                 ret = 1;
748                                 break;
749 #endif
750                                 fill_cpu_mask(td->cpumask, cpu);
751                                 fgetpos(f, &off);
752                                 continue;
753                         }
754                         if (!check_int(p, "fsync", &td->fsync_blocks)) {
755                                 fgetpos(f, &off);
756                                 td->end_fsync = 1;
757                                 continue;
758                         }
759                         if (!check_int(p, "startdelay", &td->start_delay)) {
760                                 fgetpos(f, &off);
761                                 continue;
762                         }
763                         if (!check_int(p, "timeout", &td->timeout)) {
764                                 fgetpos(f, &off);
765                                 continue;
766                         }
767                         if (!check_int(p, "invalidate",&td->invalidate_cache)) {
768                                 fgetpos(f, &off);
769                                 continue;
770                         }
771                         if (!check_int(p, "iodepth", &td->iodepth)) {
772                                 fgetpos(f, &off);
773                                 continue;
774                         }
775                         if (!check_int(p, "sync", &td->sync_io)) {
776                                 fgetpos(f, &off);
777                                 continue;
778                         }
779                         if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
780                                 fgetpos(f, &off);
781                                 continue;
782                         }
783                         if (!check_int(p, "create_serialize", &td->create_serialize)) {
784                                 fgetpos(f, &off);
785                                 continue;
786                         }
787                         if (!check_int(p, "create_fsync", &td->create_fsync)) {
788                                 fgetpos(f, &off);
789                                 continue;
790                         }
791                         if (!check_int(p, "end_fsync", &td->end_fsync)) {
792                                 fgetpos(f, &off);
793                                 continue;
794                         }
795                         if (!check_int(p, "loops", &td->loops)) {
796                                 fgetpos(f, &off);
797                                 continue;
798                         }
799                         if (!check_int(p, "numjobs", &td->numjobs)) {
800                                 fgetpos(f, &off);
801                                 continue;
802                         }
803                         if (!check_int(p, "overwrite", &td->overwrite)) {
804                                 fgetpos(f, &off);
805                                 continue;
806                         }
807                         if (!check_int(p, "rwmixcycle", &td->rwmixcycle)) {
808                                 fgetpos(f, &off);
809                                 continue;
810                         }
811                         if (!check_int(p, "rwmixread", &il)) {
812                                 if (il > 100)
813                                         il = 100;
814                                 td->rwmixread = il;
815                                 fgetpos(f, &off);
816                                 continue;
817                         }
818                         if (!check_int(p, "rwmixwrite", &il)) {
819                                 if (il > 100)
820                                         il = 100;
821                                 td->rwmixread = 100 - il;
822                                 fgetpos(f, &off);
823                                 continue;
824                         }
825                         if (!check_int(p, "nice", &td->nice)) {
826                                 fgetpos(f, &off);
827                                 continue;
828                         }
829                         if (!check_range(p, "bsrange", &ul1, &ul2)) {
830                                 if (ul1 > ul2) {
831                                         td->max_bs = ul1;
832                                         td->min_bs = ul2;
833                                 } else {
834                                         td->max_bs = ul2;
835                                         td->min_bs = ul1;
836                                 }
837                                 fgetpos(f, &off);
838                                 continue;
839                         }
840                         if (!check_strcnv(p, "bs", &ull)) {
841                                 td->bs = ull;
842                                 fgetpos(f, &off);
843                                 continue;
844                         }
845                         if (!check_strcnv(p, "size", &td->file_size)) {
846                                 fgetpos(f, &off);
847                                 continue;
848                         }
849                         if (!check_strcnv(p, "offset", &td->file_offset)) {
850                                 fgetpos(f, &off);
851                                 continue;
852                         }
853                         if (!check_strcnv(p, "zonesize", &td->zone_size)) {
854                                 fgetpos(f, &off);
855                                 continue;
856                         }
857                         if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
858                                 fgetpos(f, &off);
859                                 continue;
860                         }
861                         if (!check_strcnv(p, "lockmem", &mlock_size)) {
862                                 fgetpos(f, &off);
863                                 continue;
864                         }
865                         if (!check_strstore(p, "directory", tmpbuf)) {
866                                 td->directory = strdup(tmpbuf);
867                                 fgetpos(f, &off);
868                                 continue;
869                         }
870                         if (!check_strstore(p, "name", tmpbuf)) {
871                                 snprintf(td->name, sizeof(td->name)-1, "%s%d", tmpbuf, td->thread_number);
872                                 fgetpos(f, &off);
873                                 continue;
874                         }
875                         if (!check_str(p, "mem", str_mem_cb, td)) {
876                                 fgetpos(f, &off);
877                                 continue;
878                         }
879                         if (!check_str(p, "verify", str_verify_cb, td)) {
880                                 fgetpos(f, &off);
881                                 continue;
882                         }
883                         if (!check_str(p, "rw", str_rw_cb, td)) {
884                                 fgetpos(f, &off);
885                                 continue;
886                         }
887                         if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
888                                 fgetpos(f, &off);
889                                 continue;
890                         }
891                         if (!check_strset(p, "create")) {
892                                 td->create_file = 1;
893                                 fgetpos(f, &off);
894                                 continue;
895                         }
896                         if (!check_strset(p, "exitall")) {
897                                 exitall_on_terminate = 1;
898                                 fgetpos(f, &off);
899                                 continue;
900                         }
901                         if (!check_strset(p, "stonewall")) {
902                                 td->stonewall = 1;
903                                 fgetpos(f, &off);
904                                 continue;
905                         }
906                         if (!check_strset(p, "thread")) {
907                                 td->use_thread = 1;
908                                 fgetpos(f, &off);
909                                 continue;
910                         }
911                         if (!check_strstore(p, "iolog", tmpbuf)) {
912                                 td->iolog_file = strdup(tmpbuf);
913                                 td->read_iolog = 1;
914                                 td->write_iolog = 0;
915                                 fgetpos(f, &off);
916                                 continue;
917                         }
918                         if (!td->read_iolog &&
919                             !check_strstore(p, "write_iolog", tmpbuf)) {
920                                 td->iolog_file = strdup(tmpbuf);
921                                 td->write_iolog = 1;
922                                 fgetpos(f, &off);
923                                 continue;
924                         }
925                         if (!check_strstore(p, "exec_prerun", tmpbuf)) {
926                                 td->exec_prerun = strdup(tmpbuf);
927                                 fgetpos(f, &off);
928                                 continue;
929                         }
930                         if (!check_strstore(p, "exec_postrun", tmpbuf)) {
931                                 td->exec_postrun = strdup(tmpbuf);
932                                 fgetpos(f, &off);
933                                 continue;
934                         }
935                         if (!check_strstore(p, "ioscheduler", tmpbuf)) {
936                                 td->ioscheduler = strdup(tmpbuf);
937                                 fgetpos(f, &off);
938                                 continue;
939                         }
940
941                         /*
942                          * Don't break here, continue parsing options so we
943                          * dump all the bad ones. Makes trial/error fixups
944                          * easier on the user.
945                          */
946                         printf("Client%d: bad option %s\n",td->thread_number,p);
947                         ret = 1;
948                 }
949
950                 if (!ret) {
951                         fsetpos(f, &off);
952                         ret = add_job(td, name, 0);
953                 }
954                 if (ret)
955                         break;
956         }
957
958         free(string);
959         free(name);
960         free(tmpbuf);
961         fclose(f);
962         return ret;
963 }
964
965 static int fill_def_thread(void)
966 {
967         memset(&def_thread, 0, sizeof(def_thread));
968
969         if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
970                 perror("sched_getaffinity");
971                 return 1;
972         }
973
974         /*
975          * fill globals
976          */
977         def_thread.ddir = DDIR_READ;
978         def_thread.iomix = 0;
979         def_thread.bs = DEF_BS;
980         def_thread.min_bs = -1;
981         def_thread.max_bs = -1;
982         def_thread.io_engine = DEF_IO_ENGINE;
983         strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
984         def_thread.odirect = DEF_ODIRECT;
985         def_thread.ratecycle = DEF_RATE_CYCLE;
986         def_thread.sequential = DEF_SEQUENTIAL;
987         def_thread.timeout = DEF_TIMEOUT;
988         def_thread.create_file = DEF_CREATE;
989         def_thread.overwrite = DEF_OVERWRITE;
990         def_thread.invalidate_cache = DEF_INVALIDATE;
991         def_thread.sync_io = DEF_SYNCIO;
992         def_thread.mem_type = MEM_MALLOC;
993         def_thread.bw_avg_time = DEF_BWAVGTIME;
994         def_thread.create_serialize = DEF_CREATE_SER;
995         def_thread.create_fsync = DEF_CREATE_FSYNC;
996         def_thread.loops = DEF_LOOPS;
997         def_thread.verify = DEF_VERIFY;
998         def_thread.stonewall = DEF_STONEWALL;
999         def_thread.numjobs = DEF_NUMJOBS;
1000         def_thread.use_thread = DEF_USE_THREAD;
1001         def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1002         def_thread.rwmixread = DEF_RWMIX_READ;
1003         def_thread.nice = DEF_NICE;
1004 #ifdef FIO_HAVE_DISK_UTIL
1005         def_thread.do_disk_util = 1;
1006 #endif
1007
1008         return 0;
1009 }
1010
1011 static void usage(char *name)
1012 {
1013         printf("%s\n", fio_version_string);
1014         printf("\t-s IO is sequential\n");
1015         printf("\t-b Block size in KiB for each IO\n");
1016         printf("\t-t Runtime in seconds\n");
1017         printf("\t-R Exit all threads on failure to meet rate goal\n");
1018         printf("\t-o Use O_DIRECT\n");
1019         printf("\t-l Generate per-job latency logs\n");
1020         printf("\t-w Generate per-job bandwidth logs\n");
1021         printf("\t-f Job file (Required)\n");
1022         printf("\t-v Print version info and exit\n");
1023 }
1024
1025 static void parse_cmd_line(int argc, char *argv[])
1026 {
1027         int c;
1028
1029         while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
1030                 switch (c) {
1031                         case 's':
1032                                 def_thread.sequential = !!atoi(optarg);
1033                                 break;
1034                         case 'b':
1035                                 def_thread.bs = atoi(optarg);
1036                                 def_thread.bs <<= 10;
1037                                 if (!def_thread.bs) {
1038                                         printf("bad block size\n");
1039                                         def_thread.bs = DEF_BS;
1040                                 }
1041                                 break;
1042                         case 't':
1043                                 def_thread.timeout = atoi(optarg);
1044                                 break;
1045                         case 'r':
1046                                 repeatable = !!atoi(optarg);
1047                                 break;
1048                         case 'R':
1049                                 rate_quit = !!atoi(optarg);
1050                                 break;
1051                         case 'o':
1052                                 def_thread.odirect = !!atoi(optarg);
1053                                 break;
1054                         case 'f':
1055                                 ini_file = strdup(optarg);
1056                                 break;
1057                         case 'l':
1058                                 write_lat_log = 1;
1059                                 break;
1060                         case 'w':
1061                                 write_bw_log = 1;
1062                                 break;
1063                         case 'h':
1064                                 usage(argv[0]);
1065                                 exit(0);
1066                         case 'v':
1067                                 printf("%s\n", fio_version_string);
1068                                 exit(0);
1069                 }
1070         }
1071
1072         if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
1073                 ini_file = strdup(argv[argc - 1]);
1074 }
1075
1076 static void free_shm(void)
1077 {
1078         struct shmid_ds sbuf;
1079
1080         if (threads) {
1081                 shmdt(threads);
1082                 threads = NULL;
1083                 shmctl(shm_id, IPC_RMID, &sbuf);
1084         }
1085 }
1086
1087 static int setup_thread_area(void)
1088 {
1089         /*
1090          * 1024 is too much on some machines, scale max_jobs if
1091          * we get a failure that looks like too large a shm segment
1092          */
1093         do {
1094                 int s = max_jobs * sizeof(struct thread_data);
1095
1096                 shm_id = shmget(0, s, IPC_CREAT | 0600);
1097                 if (shm_id != -1)
1098                         break;
1099                 if (errno != EINVAL) {
1100                         perror("shmget");
1101                         break;
1102                 }
1103
1104                 max_jobs >>= 1;
1105         } while (max_jobs);
1106
1107         if (shm_id == -1)
1108                 return 1;
1109
1110         threads = shmat(shm_id, NULL, 0);
1111         if (threads == (void *) -1) {
1112                 perror("shmat");
1113                 return 1;
1114         }
1115
1116         atexit(free_shm);
1117         return 0;
1118 }
1119
1120 int parse_options(int argc, char *argv[])
1121 {
1122         if (setup_thread_area())
1123                 return 1;
1124         if (fill_def_thread())
1125                 return 1;
1126
1127         parse_cmd_line(argc, argv);
1128
1129         if (!ini_file) {
1130                 printf("Need job file\n");
1131                 usage(argv[0]);
1132                 return 1;
1133         }
1134
1135         if (parse_jobs_ini(ini_file))
1136                 return 1;
1137
1138         return 0;
1139 }