5bbeb34276e622cd9f1fc2f88d14664f619b8dcf
[fio.git] / options.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <getopt.h>
7 #include <assert.h>
8 #include <libgen.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12
13 #include "fio.h"
14 #include "parse.h"
15 #include "lib/fls.h"
16
17 #define td_var_offset(var)      ((size_t) &((struct thread_options *)0)->var)
18
19 /*
20  * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
21  */
22 static char *get_opt_postfix(const char *str)
23 {
24         char *p = strstr(str, ":");
25
26         if (!p)
27                 return NULL;
28
29         p++;
30         strip_blank_front(&p);
31         strip_blank_end(p);
32         return strdup(p);
33 }
34
35 static int bs_cmp(const void *p1, const void *p2)
36 {
37         const struct bssplit *bsp1 = p1;
38         const struct bssplit *bsp2 = p2;
39
40         return bsp1->perc < bsp2->perc;
41 }
42
43 static int str_bssplit_cb(void *data, const char *input)
44 {
45         struct thread_data *td = data;
46         char *fname, *str, *p;
47         unsigned int i, perc, perc_missing;
48         unsigned int max_bs, min_bs;
49         long long val;
50
51         p = str = strdup(input);
52
53         strip_blank_front(&str);
54         strip_blank_end(str);
55
56         td->o.bssplit_nr = 4;
57         td->o.bssplit = malloc(4 * sizeof(struct bssplit));
58
59         i = 0;
60         max_bs = 0;
61         min_bs = -1;
62         while ((fname = strsep(&str, ":")) != NULL) {
63                 char *perc_str;
64
65                 if (!strlen(fname))
66                         break;
67
68                 /*
69                  * grow struct buffer, if needed
70                  */
71                 if (i == td->o.bssplit_nr) {
72                         td->o.bssplit_nr <<= 1;
73                         td->o.bssplit = realloc(td->o.bssplit,
74                                                 td->o.bssplit_nr
75                                                   * sizeof(struct bssplit));
76                 }
77
78                 perc_str = strstr(fname, "/");
79                 if (perc_str) {
80                         *perc_str = '\0';
81                         perc_str++;
82                         perc = atoi(perc_str);
83                         if (perc > 100)
84                                 perc = 100;
85                         else if (!perc)
86                                 perc = -1;
87                 } else
88                         perc = -1;
89
90                 if (str_to_decimal(fname, &val, 1)) {
91                         log_err("fio: bssplit conversion failed\n");
92                         free(td->o.bssplit);
93                         return 1;
94                 }
95
96                 if (val > max_bs)
97                         max_bs = val;
98                 if (val < min_bs)
99                         min_bs = val;
100
101                 td->o.bssplit[i].bs = val;
102                 td->o.bssplit[i].perc = perc;
103                 i++;
104         }
105
106         td->o.bssplit_nr = i;
107
108         /*
109          * Now check if the percentages add up, and how much is missing
110          */
111         perc = perc_missing = 0;
112         for (i = 0; i < td->o.bssplit_nr; i++) {
113                 struct bssplit *bsp = &td->o.bssplit[i];
114
115                 if (bsp->perc == (unsigned char) -1)
116                         perc_missing++;
117                 else
118                         perc += bsp->perc;
119         }
120
121         if (perc > 100) {
122                 log_err("fio: bssplit percentages add to more than 100%%\n");
123                 free(td->o.bssplit);
124                 return 1;
125         }
126         /*
127          * If values didn't have a percentage set, divide the remains between
128          * them.
129          */
130         if (perc_missing) {
131                 for (i = 0; i < td->o.bssplit_nr; i++) {
132                         struct bssplit *bsp = &td->o.bssplit[i];
133
134                         if (bsp->perc == (unsigned char) -1)
135                                 bsp->perc = (100 - perc) / perc_missing;
136                 }
137         }
138
139         td->o.min_bs[DDIR_READ] = td->o.min_bs[DDIR_WRITE] = min_bs;
140         td->o.max_bs[DDIR_READ] = td->o.max_bs[DDIR_WRITE] = max_bs;
141
142         /*
143          * now sort based on percentages, for ease of lookup
144          */
145         qsort(td->o.bssplit, td->o.bssplit_nr, sizeof(struct bssplit), bs_cmp);
146
147         free(p);
148         return 0;
149 }
150
151 static int str_rw_cb(void *data, const char *str)
152 {
153         struct thread_data *td = data;
154         char *nr = get_opt_postfix(str);
155
156         td->o.ddir_nr = 1;
157         if (nr) {
158                 td->o.ddir_nr = atoi(nr);
159                 free(nr);
160         }
161
162         return 0;
163 }
164
165 static int str_mem_cb(void *data, const char *mem)
166 {
167         struct thread_data *td = data;
168
169         if (td->o.mem_type == MEM_MMAPHUGE || td->o.mem_type == MEM_MMAP) {
170                 td->mmapfile = get_opt_postfix(mem);
171                 if (td->o.mem_type == MEM_MMAPHUGE && !td->mmapfile) {
172                         log_err("fio: mmaphuge:/path/to/file\n");
173                         return 1;
174                 }
175         }
176
177         return 0;
178 }
179
180 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
181 {
182         mlock_size = *val;
183         return 0;
184 }
185
186 static int str_rwmix_read_cb(void *data, unsigned int *val)
187 {
188         struct thread_data *td = data;
189
190         td->o.rwmix[DDIR_READ] = *val;
191         td->o.rwmix[DDIR_WRITE] = 100 - *val;
192         return 0;
193 }
194
195 static int str_rwmix_write_cb(void *data, unsigned int *val)
196 {
197         struct thread_data *td = data;
198
199         td->o.rwmix[DDIR_WRITE] = *val;
200         td->o.rwmix[DDIR_READ] = 100 - *val;
201         return 0;
202 }
203
204 #ifdef FIO_HAVE_IOPRIO
205 static int str_prioclass_cb(void *data, unsigned int *val)
206 {
207         struct thread_data *td = data;
208         unsigned short mask;
209
210         /*
211          * mask off old class bits, str_prio_cb() may have set a default class
212          */
213         mask = (1 << IOPRIO_CLASS_SHIFT) - 1;
214         td->ioprio &= mask;
215
216         td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
217         td->ioprio_set = 1;
218         return 0;
219 }
220
221 static int str_prio_cb(void *data, unsigned int *val)
222 {
223         struct thread_data *td = data;
224
225         td->ioprio |= *val;
226
227         /*
228          * If no class is set, assume BE
229          */
230         if ((td->ioprio >> IOPRIO_CLASS_SHIFT) == 0)
231                 td->ioprio |= IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT;
232
233         td->ioprio_set = 1;
234         return 0;
235 }
236 #endif
237
238 static int str_exitall_cb(void)
239 {
240         exitall_on_terminate = 1;
241         return 0;
242 }
243
244 #ifdef FIO_HAVE_CPU_AFFINITY
245 static int str_cpumask_cb(void *data, unsigned int *val)
246 {
247         struct thread_data *td = data;
248         unsigned int i;
249
250         CPU_ZERO(&td->o.cpumask);
251
252         for (i = 0; i < sizeof(int) * 8; i++) {
253                 if ((1 << i) & *val) {
254                         dprint(FD_PARSE, "set cpu allowed %d\n", i);
255                         CPU_SET(*val, &td->o.cpumask);
256                 }
257         }
258
259         td->o.cpumask_set = 1;
260         return 0;
261 }
262
263 static int str_cpus_allowed_cb(void *data, const char *input)
264 {
265         struct thread_data *td = data;
266         char *cpu, *str, *p;
267
268         CPU_ZERO(&td->o.cpumask);
269
270         p = str = strdup(input);
271
272         strip_blank_front(&str);
273         strip_blank_end(str);
274
275         while ((cpu = strsep(&str, ",")) != NULL) {
276                 char *str2, *cpu2;
277                 int icpu, icpu2;
278
279                 if (!strlen(cpu))
280                         break;
281
282                 str2 = cpu;
283                 icpu2 = -1;
284                 while ((cpu2 = strsep(&str2, "-")) != NULL) {
285                         if (!strlen(cpu2))
286                                 break;
287
288                         icpu2 = atoi(cpu2);
289                 }
290
291                 icpu = atoi(cpu);
292                 if (icpu2 == -1)
293                         icpu2 = icpu;
294                 while (icpu <= icpu2) {
295                         dprint(FD_PARSE, "set cpu allowed %d\n", icpu);
296                         CPU_SET(atoi(cpu), &td->o.cpumask);
297                         icpu++;
298                 }
299         }
300
301         free(p);
302         td->o.cpumask_set = 1;
303         return 0;
304 }
305 #endif
306
307 static int str_fst_cb(void *data, const char *str)
308 {
309         struct thread_data *td = data;
310         char *nr = get_opt_postfix(str);
311
312         td->file_service_nr = 1;
313         if (nr) {
314                 td->file_service_nr = atoi(nr);
315                 free(nr);
316         }
317
318         return 0;
319 }
320
321 static int check_dir(struct thread_data *td, char *fname)
322 {
323         char file[PATH_MAX], *dir;
324         int elen = 0;
325
326         if (td->o.directory) {
327                 strcpy(file, td->o.directory);
328                 strcat(file, "/");
329                 elen = strlen(file);
330         }
331
332         sprintf(file + elen, "%s", fname);
333         dir = dirname(file);
334
335 #if 0
336         {
337         struct stat sb;
338         /*
339          * We can't do this on FIO_DISKLESSIO engines. The engine isn't loaded
340          * yet, so we can't do this check right here...
341          */
342         if (lstat(dir, &sb) < 0) {
343                 int ret = errno;
344
345                 log_err("fio: %s is not a directory\n", dir);
346                 td_verror(td, ret, "lstat");
347                 return 1;
348         }
349
350         if (!S_ISDIR(sb.st_mode)) {
351                 log_err("fio: %s is not a directory\n", dir);
352                 return 1;
353         }
354         }
355 #endif
356
357         return 0;
358 }
359
360 static int str_filename_cb(void *data, const char *input)
361 {
362         struct thread_data *td = data;
363         char *fname, *str, *p;
364
365         p = str = strdup(input);
366
367         strip_blank_front(&str);
368         strip_blank_end(str);
369
370         if (!td->files_index)
371                 td->o.nr_files = 0;
372
373         while ((fname = strsep(&str, ":")) != NULL) {
374                 if (!strlen(fname))
375                         break;
376                 if (check_dir(td, fname)) {
377                         free(p);
378                         return 1;
379                 }
380                 add_file(td, fname);
381                 td->o.nr_files++;
382         }
383
384         free(p);
385         return 0;
386 }
387
388 static int str_directory_cb(void *data, const char fio_unused *str)
389 {
390         struct thread_data *td = data;
391         struct stat sb;
392
393         if (lstat(td->o.directory, &sb) < 0) {
394                 int ret = errno;
395
396                 log_err("fio: %s is not a directory\n", td->o.directory);
397                 td_verror(td, ret, "lstat");
398                 return 1;
399         }
400         if (!S_ISDIR(sb.st_mode)) {
401                 log_err("fio: %s is not a directory\n", td->o.directory);
402                 return 1;
403         }
404
405         return 0;
406 }
407
408 static int str_opendir_cb(void *data, const char fio_unused *str)
409 {
410         struct thread_data *td = data;
411
412         if (!td->files_index)
413                 td->o.nr_files = 0;
414
415         return add_dir_files(td, td->o.opendir);
416 }
417
418 static int str_verify_offset_cb(void *data, unsigned int *off)
419 {
420         struct thread_data *td = data;
421
422         if (*off && *off < sizeof(struct verify_header)) {
423                 log_err("fio: verify_offset too small\n");
424                 return 1;
425         }
426
427         td->o.verify_offset = *off;
428         return 0;
429 }
430
431 static int str_verify_pattern_cb(void *data, unsigned int *off)
432 {
433         struct thread_data *td = data;
434         unsigned int msb;
435
436         msb = __fls(*off);
437         if (msb <= 8)
438                 td->o.verify_pattern_bytes = 1;
439         else if (msb <= 16)
440                 td->o.verify_pattern_bytes = 2;
441         else if (msb <= 24)
442                 td->o.verify_pattern_bytes = 3;
443         else
444                 td->o.verify_pattern_bytes = 4;
445
446         td->o.verify_pattern = *off;
447         return 0;
448 }
449
450 static int str_lockfile_cb(void *data, const char *str)
451 {
452         struct thread_data *td = data;
453         char *nr = get_opt_postfix(str);
454
455         td->o.lockfile_batch = 1;
456         if (nr) {
457                 td->o.lockfile_batch = atoi(nr);
458                 free(nr);
459         }
460
461         return 0;
462 }
463
464 static int str_write_bw_log_cb(void *data, const char *str)
465 {
466         struct thread_data *td = data;
467
468         if (str)
469                 td->o.bw_log_file = strdup(str);
470
471         td->o.write_bw_log = 1;
472         return 0;
473 }
474
475 static int str_write_lat_log_cb(void *data, const char *str)
476 {
477         struct thread_data *td = data;
478
479         if (str)
480                 td->o.lat_log_file = strdup(str);
481
482         td->o.write_lat_log = 1;
483         return 0;
484 }
485
486 static int str_gtod_reduce_cb(void *data, int *il)
487 {
488         struct thread_data *td = data;
489         int val = *il;
490
491         td->o.disable_clat = !!val;
492         td->o.disable_slat = !!val;
493         td->o.disable_bw = !!val;
494         if (val)
495                 td->tv_cache_mask = 63;
496
497         return 0;
498 }
499
500 static int str_gtod_cpu_cb(void *data, int *il)
501 {
502         struct thread_data *td = data;
503         int val = *il;
504
505         td->o.gtod_cpu = val;
506         td->o.gtod_offload = 1;
507         return 0;
508 }
509
510 #define __stringify_1(x)        #x
511 #define __stringify(x)          __stringify_1(x)
512
513 /*
514  * Map of job/command line options
515  */
516 static struct fio_option options[] = {
517         {
518                 .name   = "description",
519                 .type   = FIO_OPT_STR_STORE,
520                 .off1   = td_var_offset(description),
521                 .help   = "Text job description",
522         },
523         {
524                 .name   = "name",
525                 .type   = FIO_OPT_STR_STORE,
526                 .off1   = td_var_offset(name),
527                 .help   = "Name of this job",
528         },
529         {
530                 .name   = "directory",
531                 .type   = FIO_OPT_STR_STORE,
532                 .off1   = td_var_offset(directory),
533                 .cb     = str_directory_cb,
534                 .help   = "Directory to store files in",
535         },
536         {
537                 .name   = "filename",
538                 .type   = FIO_OPT_STR_STORE,
539                 .off1   = td_var_offset(filename),
540                 .cb     = str_filename_cb,
541                 .prio   = 1, /* must come before "directory" */
542                 .help   = "File(s) to use for the workload",
543         },
544         {
545                 .name   = "lockfile",
546                 .type   = FIO_OPT_STR,
547                 .cb     = str_lockfile_cb,
548                 .off1   = td_var_offset(file_lock_mode),
549                 .help   = "Lock file when doing IO to it",
550                 .parent = "filename",
551                 .def    = "none",
552                 .posval = {
553                           { .ival = "none",
554                             .oval = FILE_LOCK_NONE,
555                             .help = "No file locking",
556                           },
557                           { .ival = "exclusive",
558                             .oval = FILE_LOCK_EXCLUSIVE,
559                             .help = "Exclusive file lock",
560                           },
561                           {
562                             .ival = "readwrite",
563                             .oval = FILE_LOCK_READWRITE,
564                             .help = "Read vs write lock",
565                           },
566                 },
567         },
568         {
569                 .name   = "opendir",
570                 .type   = FIO_OPT_STR_STORE,
571                 .off1   = td_var_offset(opendir),
572                 .cb     = str_opendir_cb,
573                 .help   = "Recursively add files from this directory and down",
574         },
575         {
576                 .name   = "rw",
577                 .alias  = "readwrite",
578                 .type   = FIO_OPT_STR,
579                 .cb     = str_rw_cb,
580                 .off1   = td_var_offset(td_ddir),
581                 .help   = "IO direction",
582                 .def    = "read",
583                 .posval = {
584                           { .ival = "read",
585                             .oval = TD_DDIR_READ,
586                             .help = "Sequential read",
587                           },
588                           { .ival = "write",
589                             .oval = TD_DDIR_WRITE,
590                             .help = "Sequential write",
591                           },
592                           { .ival = "randread",
593                             .oval = TD_DDIR_RANDREAD,
594                             .help = "Random read",
595                           },
596                           { .ival = "randwrite",
597                             .oval = TD_DDIR_RANDWRITE,
598                             .help = "Random write",
599                           },
600                           { .ival = "rw",
601                             .oval = TD_DDIR_RW,
602                             .help = "Sequential read and write mix",
603                           },
604                           { .ival = "randrw",
605                             .oval = TD_DDIR_RANDRW,
606                             .help = "Random read and write mix"
607                           },
608                 },
609         },
610         {
611                 .name   = "ioengine",
612                 .type   = FIO_OPT_STR_STORE,
613                 .off1   = td_var_offset(ioengine),
614                 .help   = "IO engine to use",
615                 .def    = "sync",
616                 .posval = {
617                           { .ival = "sync",
618                             .help = "Use read/write",
619                           },
620                           { .ival = "psync",
621                             .help = "Use pread/pwrite",
622                           },
623                           { .ival = "vsync",
624                              .help = "Use readv/writev",
625                           },
626 #ifdef FIO_HAVE_LIBAIO
627                           { .ival = "libaio",
628                             .help = "Linux native asynchronous IO",
629                           },
630 #endif
631 #ifdef FIO_HAVE_POSIXAIO
632                           { .ival = "posixaio",
633                             .help = "POSIX asynchronous IO",
634                           },
635 #endif
636 #ifdef FIO_HAVE_SOLARISAIO
637                           { .ival = "solarisaio",
638                             .help = "Solaris native asynchronous IO",
639                           },
640 #endif
641                           { .ival = "mmap",
642                             .help = "Memory mapped IO",
643                           },
644 #ifdef FIO_HAVE_SPLICE
645                           { .ival = "splice",
646                             .help = "splice/vmsplice based IO",
647                           },
648                           { .ival = "netsplice",
649                             .help = "splice/vmsplice to/from the network",
650                           },
651 #endif
652 #ifdef FIO_HAVE_SGIO
653                           { .ival = "sg",
654                             .help = "SCSI generic v3 IO",
655                           },
656 #endif
657                           { .ival = "null",
658                             .help = "Testing engine (no data transfer)",
659                           },
660                           { .ival = "net",
661                             .help = "Network IO",
662                           },
663 #ifdef FIO_HAVE_SYSLET
664                           { .ival = "syslet-rw",
665                             .help = "syslet enabled async pread/pwrite IO",
666                           },
667 #endif
668                           { .ival = "cpuio",
669                             .help = "CPU cycler burner engine",
670                           },
671 #ifdef FIO_HAVE_GUASI
672                           { .ival = "guasi",
673                             .help = "GUASI IO engine",
674                           },
675 #endif
676                           { .ival = "external",
677                             .help = "Load external engine (append name)",
678                           },
679                 },
680         },
681         {
682                 .name   = "iodepth",
683                 .type   = FIO_OPT_INT,
684                 .off1   = td_var_offset(iodepth),
685                 .help   = "Amount of IO buffers to keep in flight",
686                 .minval = 1,
687                 .def    = "1",
688         },
689         {
690                 .name   = "iodepth_batch",
691                 .alias  = "iodepth_batch_submit",
692                 .type   = FIO_OPT_INT,
693                 .off1   = td_var_offset(iodepth_batch),
694                 .help   = "Number of IO to submit in one go",
695                 .parent = "iodepth",
696                 .minval = 1,
697                 .def    = "1",
698         },
699         {
700                 .name   = "iodepth_batch_complete",
701                 .type   = FIO_OPT_INT,
702                 .off1   = td_var_offset(iodepth_batch_complete),
703                 .help   = "Number of IO to retrieve in one go",
704                 .parent = "iodepth",
705                 .minval = 0,
706                 .def    = "1",
707         },
708         {
709                 .name   = "iodepth_low",
710                 .type   = FIO_OPT_INT,
711                 .off1   = td_var_offset(iodepth_low),
712                 .help   = "Low water mark for queuing depth",
713                 .parent = "iodepth",
714         },
715         {
716                 .name   = "size",
717                 .type   = FIO_OPT_STR_VAL,
718                 .off1   = td_var_offset(size),
719                 .minval = 1,
720                 .help   = "Total size of device or files",
721         },
722         {
723                 .name   = "fill_device",
724                 .type   = FIO_OPT_BOOL,
725                 .off1   = td_var_offset(fill_device),
726                 .help   = "Write until an ENOSPC error occurs",
727                 .def    = "0",
728         },
729         {
730                 .name   = "filesize",
731                 .type   = FIO_OPT_STR_VAL,
732                 .off1   = td_var_offset(file_size_low),
733                 .off2   = td_var_offset(file_size_high),
734                 .minval = 1,
735                 .help   = "Size of individual files",
736         },
737         {
738                 .name   = "offset",
739                 .alias  = "fileoffset",
740                 .type   = FIO_OPT_STR_VAL,
741                 .off1   = td_var_offset(start_offset),
742                 .help   = "Start IO from this offset",
743                 .def    = "0",
744         },
745         {
746                 .name   = "bs",
747                 .alias  = "blocksize",
748                 .type   = FIO_OPT_STR_VAL_INT,
749                 .off1   = td_var_offset(bs[DDIR_READ]),
750                 .off2   = td_var_offset(bs[DDIR_WRITE]),
751                 .minval = 1,
752                 .help   = "Block size unit",
753                 .def    = "4k",
754                 .parent = "rw",
755         },
756         {
757                 .name   = "bsrange",
758                 .alias  = "blocksize_range",
759                 .type   = FIO_OPT_RANGE,
760                 .off1   = td_var_offset(min_bs[DDIR_READ]),
761                 .off2   = td_var_offset(max_bs[DDIR_READ]),
762                 .off3   = td_var_offset(min_bs[DDIR_WRITE]),
763                 .off4   = td_var_offset(max_bs[DDIR_WRITE]),
764                 .minval = 1,
765                 .help   = "Set block size range (in more detail than bs)",
766                 .parent = "rw",
767         },
768         {
769                 .name   = "bssplit",
770                 .type   = FIO_OPT_STR,
771                 .cb     = str_bssplit_cb,
772                 .help   = "Set a specific mix of block sizes",
773                 .parent = "rw",
774         },
775         {
776                 .name   = "bs_unaligned",
777                 .alias  = "blocksize_unaligned",
778                 .type   = FIO_OPT_STR_SET,
779                 .off1   = td_var_offset(bs_unaligned),
780                 .help   = "Don't sector align IO buffer sizes",
781                 .parent = "rw",
782         },
783         {
784                 .name   = "randrepeat",
785                 .type   = FIO_OPT_BOOL,
786                 .off1   = td_var_offset(rand_repeatable),
787                 .help   = "Use repeatable random IO pattern",
788                 .def    = "1",
789                 .parent = "rw",
790         },
791         {
792                 .name   = "norandommap",
793                 .type   = FIO_OPT_STR_SET,
794                 .off1   = td_var_offset(norandommap),
795                 .help   = "Accept potential duplicate random blocks",
796                 .parent = "rw",
797         },
798         {
799                 .name   = "softrandommap",
800                 .type   = FIO_OPT_BOOL,
801                 .off1   = td_var_offset(softrandommap),
802                 .help   = "Set norandommap if randommap allocation fails",
803                 .parent = "norandommap",
804                 .def    = "0",
805         },
806         {
807                 .name   = "nrfiles",
808                 .type   = FIO_OPT_INT,
809                 .off1   = td_var_offset(nr_files),
810                 .help   = "Split job workload between this number of files",
811                 .def    = "1",
812         },
813         {
814                 .name   = "openfiles",
815                 .type   = FIO_OPT_INT,
816                 .off1   = td_var_offset(open_files),
817                 .help   = "Number of files to keep open at the same time",
818         },
819         {
820                 .name   = "file_service_type",
821                 .type   = FIO_OPT_STR,
822                 .cb     = str_fst_cb,
823                 .off1   = td_var_offset(file_service_type),
824                 .help   = "How to select which file to service next",
825                 .def    = "roundrobin",
826                 .posval = {
827                           { .ival = "random",
828                             .oval = FIO_FSERVICE_RANDOM,
829                             .help = "Choose a file at random",
830                           },
831                           { .ival = "roundrobin",
832                             .oval = FIO_FSERVICE_RR,
833                             .help = "Round robin select files",
834                           },
835                 },
836                 .parent = "nrfiles",
837         },
838         {
839                 .name   = "fadvise_hint",
840                 .type   = FIO_OPT_BOOL,
841                 .off1   = td_var_offset(fadvise_hint),
842                 .help   = "Use fadvise() to advise the kernel on IO pattern",
843                 .def    = "1",
844         },
845         {
846                 .name   = "fsync",
847                 .type   = FIO_OPT_INT,
848                 .off1   = td_var_offset(fsync_blocks),
849                 .help   = "Issue fsync for writes every given number of blocks",
850                 .def    = "0",
851         },
852         {
853                 .name   = "direct",
854                 .type   = FIO_OPT_BOOL,
855                 .off1   = td_var_offset(odirect),
856                 .help   = "Use O_DIRECT IO (negates buffered)",
857                 .def    = "0",
858         },
859         {
860                 .name   = "buffered",
861                 .type   = FIO_OPT_BOOL,
862                 .off1   = td_var_offset(odirect),
863                 .neg    = 1,
864                 .help   = "Use buffered IO (negates direct)",
865                 .def    = "1",
866         },
867         {
868                 .name   = "overwrite",
869                 .type   = FIO_OPT_BOOL,
870                 .off1   = td_var_offset(overwrite),
871                 .help   = "When writing, set whether to overwrite current data",
872                 .def    = "0",
873         },
874         {
875                 .name   = "loops",
876                 .type   = FIO_OPT_INT,
877                 .off1   = td_var_offset(loops),
878                 .help   = "Number of times to run the job",
879                 .def    = "1",
880         },
881         {
882                 .name   = "numjobs",
883                 .type   = FIO_OPT_INT,
884                 .off1   = td_var_offset(numjobs),
885                 .help   = "Duplicate this job this many times",
886                 .def    = "1",
887         },
888         {
889                 .name   = "startdelay",
890                 .type   = FIO_OPT_INT,
891                 .off1   = td_var_offset(start_delay),
892                 .help   = "Only start job when this period has passed",
893                 .def    = "0",
894         },
895         {
896                 .name   = "runtime",
897                 .alias  = "timeout",
898                 .type   = FIO_OPT_STR_VAL_TIME,
899                 .off1   = td_var_offset(timeout),
900                 .help   = "Stop workload when this amount of time has passed",
901                 .def    = "0",
902         },
903         {
904                 .name   = "time_based",
905                 .type   = FIO_OPT_STR_SET,
906                 .off1   = td_var_offset(time_based),
907                 .help   = "Keep running until runtime/timeout is met",
908         },
909         {
910                 .name   = "ramp_time",
911                 .type   = FIO_OPT_STR_VAL_TIME,
912                 .off1   = td_var_offset(ramp_time),
913                 .help   = "Ramp up time before measuring performance",
914         },
915         {
916                 .name   = "mem",
917                 .alias  = "iomem",
918                 .type   = FIO_OPT_STR,
919                 .cb     = str_mem_cb,
920                 .off1   = td_var_offset(mem_type),
921                 .help   = "Backing type for IO buffers",
922                 .def    = "malloc",
923                 .posval = {
924                           { .ival = "malloc",
925                             .oval = MEM_MALLOC,
926                             .help = "Use malloc(3) for IO buffers",
927                           },
928                           { .ival = "shm",
929                             .oval = MEM_SHM,
930                             .help = "Use shared memory segments for IO buffers",
931                           },
932 #ifdef FIO_HAVE_HUGETLB
933                           { .ival = "shmhuge",
934                             .oval = MEM_SHMHUGE,
935                             .help = "Like shm, but use huge pages",
936                           },
937 #endif
938                           { .ival = "mmap",
939                             .oval = MEM_MMAP,
940                             .help = "Use mmap(2) (file or anon) for IO buffers",
941                           },
942 #ifdef FIO_HAVE_HUGETLB
943                           { .ival = "mmaphuge",
944                             .oval = MEM_MMAPHUGE,
945                             .help = "Like mmap, but use huge pages",
946                           },
947 #endif
948                   },
949         },
950         {
951                 .name   = "verify",
952                 .type   = FIO_OPT_STR,
953                 .off1   = td_var_offset(verify),
954                 .help   = "Verify data written",
955                 .def    = "0",
956                 .posval = {
957                           { .ival = "0",
958                             .oval = VERIFY_NONE,
959                             .help = "Don't do IO verification",
960                           },
961                           { .ival = "md5",
962                             .oval = VERIFY_MD5,
963                             .help = "Use md5 checksums for verification",
964                           },
965                           { .ival = "crc64",
966                             .oval = VERIFY_CRC64,
967                             .help = "Use crc64 checksums for verification",
968                           },
969                           { .ival = "crc32",
970                             .oval = VERIFY_CRC32,
971                             .help = "Use crc32 checksums for verification",
972                           },
973                           { .ival = "crc32c-intel",
974                             .oval = VERIFY_CRC32C_INTEL,
975                             .help = "Use hw crc32c checksums for verification",
976                           },
977                           { .ival = "crc32c",
978                             .oval = VERIFY_CRC32C,
979                             .help = "Use crc32c checksums for verification",
980                           },
981                           { .ival = "crc16",
982                             .oval = VERIFY_CRC16,
983                             .help = "Use crc16 checksums for verification",
984                           },
985                           { .ival = "crc7",
986                             .oval = VERIFY_CRC7,
987                             .help = "Use crc7 checksums for verification",
988                           },
989                           { .ival = "sha256",
990                             .oval = VERIFY_SHA256,
991                             .help = "Use sha256 checksums for verification",
992                           },
993                           { .ival = "sha512",
994                             .oval = VERIFY_SHA512,
995                             .help = "Use sha512 checksums for verification",
996                           },
997                           { .ival = "meta",
998                             .oval = VERIFY_META,
999                             .help = "Use io information",
1000                           },
1001                           {
1002                             .ival = "null",
1003                             .oval = VERIFY_NULL,
1004                             .help = "Pretend to verify",
1005                           },
1006                 },
1007         },
1008         {
1009                 .name   = "do_verify",
1010                 .type   = FIO_OPT_BOOL,
1011                 .off1   = td_var_offset(do_verify),
1012                 .help   = "Run verification stage after write",
1013                 .def    = "1",
1014                 .parent = "verify",
1015         },
1016         {
1017                 .name   = "verifysort",
1018                 .type   = FIO_OPT_BOOL,
1019                 .off1   = td_var_offset(verifysort),
1020                 .help   = "Sort written verify blocks for read back",
1021                 .def    = "1",
1022                 .parent = "verify",
1023         },
1024         {
1025                 .name   = "verify_interval",
1026                 .type   = FIO_OPT_STR_VAL_INT,
1027                 .off1   = td_var_offset(verify_interval),
1028                 .minval = 2 * sizeof(struct verify_header),
1029                 .help   = "Store verify buffer header every N bytes",
1030                 .parent = "verify",
1031         },
1032         {
1033                 .name   = "verify_offset",
1034                 .type   = FIO_OPT_STR_VAL_INT,
1035                 .help   = "Offset verify header location by N bytes",
1036                 .def    = "0",
1037                 .cb     = str_verify_offset_cb,
1038                 .parent = "verify",
1039         },
1040         {
1041                 .name   = "verify_pattern",
1042                 .type   = FIO_OPT_INT,
1043                 .cb     = str_verify_pattern_cb,
1044                 .help   = "Fill pattern for IO buffers",
1045                 .parent = "verify",
1046         },
1047         {
1048                 .name   = "verify_fatal",
1049                 .type   = FIO_OPT_BOOL,
1050                 .off1   = td_var_offset(verify_fatal),
1051                 .def    = "0",
1052                 .help   = "Exit on a single verify failure, don't continue",
1053                 .parent = "verify",
1054         },
1055         {
1056                 .name   = "write_iolog",
1057                 .type   = FIO_OPT_STR_STORE,
1058                 .off1   = td_var_offset(write_iolog_file),
1059                 .help   = "Store IO pattern to file",
1060         },
1061         {
1062                 .name   = "read_iolog",
1063                 .type   = FIO_OPT_STR_STORE,
1064                 .off1   = td_var_offset(read_iolog_file),
1065                 .help   = "Playback IO pattern from file",
1066         },
1067         {
1068                 .name   = "exec_prerun",
1069                 .type   = FIO_OPT_STR_STORE,
1070                 .off1   = td_var_offset(exec_prerun),
1071                 .help   = "Execute this file prior to running job",
1072         },
1073         {
1074                 .name   = "exec_postrun",
1075                 .type   = FIO_OPT_STR_STORE,
1076                 .off1   = td_var_offset(exec_postrun),
1077                 .help   = "Execute this file after running job",
1078         },
1079 #ifdef FIO_HAVE_IOSCHED_SWITCH
1080         {
1081                 .name   = "ioscheduler",
1082                 .type   = FIO_OPT_STR_STORE,
1083                 .off1   = td_var_offset(ioscheduler),
1084                 .help   = "Use this IO scheduler on the backing device",
1085         },
1086 #endif
1087         {
1088                 .name   = "zonesize",
1089                 .type   = FIO_OPT_STR_VAL,
1090                 .off1   = td_var_offset(zone_size),
1091                 .help   = "Give size of an IO zone",
1092                 .def    = "0",
1093         },
1094         {
1095                 .name   = "zoneskip",
1096                 .type   = FIO_OPT_STR_VAL,
1097                 .off1   = td_var_offset(zone_skip),
1098                 .help   = "Space between IO zones",
1099                 .def    = "0",
1100         },
1101         {
1102                 .name   = "lockmem",
1103                 .type   = FIO_OPT_STR_VAL,
1104                 .cb     = str_lockmem_cb,
1105                 .help   = "Lock down this amount of memory",
1106                 .def    = "0",
1107         },
1108         {
1109                 .name   = "rwmixread",
1110                 .type   = FIO_OPT_INT,
1111                 .cb     = str_rwmix_read_cb,
1112                 .maxval = 100,
1113                 .help   = "Percentage of mixed workload that is reads",
1114                 .def    = "50",
1115         },
1116         {
1117                 .name   = "rwmixwrite",
1118                 .type   = FIO_OPT_INT,
1119                 .cb     = str_rwmix_write_cb,
1120                 .maxval = 100,
1121                 .help   = "Percentage of mixed workload that is writes",
1122                 .def    = "50",
1123         },
1124         {
1125                 .name   = "rwmixcycle",
1126                 .type   = FIO_OPT_DEPRECATED,
1127         },
1128         {
1129                 .name   = "nice",
1130                 .type   = FIO_OPT_INT,
1131                 .off1   = td_var_offset(nice),
1132                 .help   = "Set job CPU nice value",
1133                 .minval = -19,
1134                 .maxval = 20,
1135                 .def    = "0",
1136         },
1137 #ifdef FIO_HAVE_IOPRIO
1138         {
1139                 .name   = "prio",
1140                 .type   = FIO_OPT_INT,
1141                 .cb     = str_prio_cb,
1142                 .help   = "Set job IO priority value",
1143                 .minval = 0,
1144                 .maxval = 7,
1145         },
1146         {
1147                 .name   = "prioclass",
1148                 .type   = FIO_OPT_INT,
1149                 .cb     = str_prioclass_cb,
1150                 .help   = "Set job IO priority class",
1151                 .minval = 0,
1152                 .maxval = 3,
1153         },
1154 #endif
1155         {
1156                 .name   = "thinktime",
1157                 .type   = FIO_OPT_INT,
1158                 .off1   = td_var_offset(thinktime),
1159                 .help   = "Idle time between IO buffers (usec)",
1160                 .def    = "0",
1161         },
1162         {
1163                 .name   = "thinktime_spin",
1164                 .type   = FIO_OPT_INT,
1165                 .off1   = td_var_offset(thinktime_spin),
1166                 .help   = "Start think time by spinning this amount (usec)",
1167                 .def    = "0",
1168                 .parent = "thinktime",
1169         },
1170         {
1171                 .name   = "thinktime_blocks",
1172                 .type   = FIO_OPT_INT,
1173                 .off1   = td_var_offset(thinktime_blocks),
1174                 .help   = "IO buffer period between 'thinktime'",
1175                 .def    = "1",
1176                 .parent = "thinktime",
1177         },
1178         {
1179                 .name   = "rate",
1180                 .type   = FIO_OPT_INT,
1181                 .off1   = td_var_offset(rate),
1182                 .help   = "Set bandwidth rate",
1183         },
1184         {
1185                 .name   = "ratemin",
1186                 .type   = FIO_OPT_INT,
1187                 .off1   = td_var_offset(ratemin),
1188                 .help   = "Job must meet this rate or it will be shutdown",
1189                 .parent = "rate",
1190         },
1191         {
1192                 .name   = "rate_iops",
1193                 .type   = FIO_OPT_INT,
1194                 .off1   = td_var_offset(rate_iops),
1195                 .help   = "Limit IO used to this number of IO operations/sec",
1196         },
1197         {
1198                 .name   = "rate_iops_min",
1199                 .type   = FIO_OPT_INT,
1200                 .off1   = td_var_offset(rate_iops_min),
1201                 .help   = "Job must meet this rate or it will be shutdown",
1202                 .parent = "rate_iops",
1203         },
1204         {
1205                 .name   = "ratecycle",
1206                 .type   = FIO_OPT_INT,
1207                 .off1   = td_var_offset(ratecycle),
1208                 .help   = "Window average for rate limits (msec)",
1209                 .def    = "1000",
1210                 .parent = "rate",
1211         },
1212         {
1213                 .name   = "invalidate",
1214                 .type   = FIO_OPT_BOOL,
1215                 .off1   = td_var_offset(invalidate_cache),
1216                 .help   = "Invalidate buffer/page cache prior to running job",
1217                 .def    = "1",
1218         },
1219         {
1220                 .name   = "sync",
1221                 .type   = FIO_OPT_BOOL,
1222                 .off1   = td_var_offset(sync_io),
1223                 .help   = "Use O_SYNC for buffered writes",
1224                 .def    = "0",
1225                 .parent = "buffered",
1226         },
1227         {
1228                 .name   = "bwavgtime",
1229                 .type   = FIO_OPT_INT,
1230                 .off1   = td_var_offset(bw_avg_time),
1231                 .help   = "Time window over which to calculate bandwidth"
1232                           " (msec)",
1233                 .def    = "500",
1234         },
1235         {
1236                 .name   = "create_serialize",
1237                 .type   = FIO_OPT_BOOL,
1238                 .off1   = td_var_offset(create_serialize),
1239                 .help   = "Serialize creating of job files",
1240                 .def    = "1",
1241         },
1242         {
1243                 .name   = "create_fsync",
1244                 .type   = FIO_OPT_BOOL,
1245                 .off1   = td_var_offset(create_fsync),
1246                 .help   = "Fsync file after creation",
1247                 .def    = "1",
1248         },
1249         {
1250                 .name   = "cpuload",
1251                 .type   = FIO_OPT_INT,
1252                 .off1   = td_var_offset(cpuload),
1253                 .help   = "Use this percentage of CPU",
1254         },
1255         {
1256                 .name   = "cpuchunks",
1257                 .type   = FIO_OPT_INT,
1258                 .off1   = td_var_offset(cpucycle),
1259                 .help   = "Length of the CPU burn cycles (usecs)",
1260                 .def    = "50000",
1261                 .parent = "cpuload",
1262         },
1263 #ifdef FIO_HAVE_CPU_AFFINITY
1264         {
1265                 .name   = "cpumask",
1266                 .type   = FIO_OPT_INT,
1267                 .cb     = str_cpumask_cb,
1268                 .help   = "CPU affinity mask",
1269         },
1270         {
1271                 .name   = "cpus_allowed",
1272                 .type   = FIO_OPT_STR,
1273                 .cb     = str_cpus_allowed_cb,
1274                 .help   = "Set CPUs allowed",
1275         },
1276 #endif
1277         {
1278                 .name   = "end_fsync",
1279                 .type   = FIO_OPT_BOOL,
1280                 .off1   = td_var_offset(end_fsync),
1281                 .help   = "Include fsync at the end of job",
1282                 .def    = "0",
1283         },
1284         {
1285                 .name   = "fsync_on_close",
1286                 .type   = FIO_OPT_BOOL,
1287                 .off1   = td_var_offset(fsync_on_close),
1288                 .help   = "fsync files on close",
1289                 .def    = "0",
1290         },
1291         {
1292                 .name   = "unlink",
1293                 .type   = FIO_OPT_BOOL,
1294                 .off1   = td_var_offset(unlink),
1295                 .help   = "Unlink created files after job has completed",
1296                 .def    = "0",
1297         },
1298         {
1299                 .name   = "exitall",
1300                 .type   = FIO_OPT_STR_SET,
1301                 .cb     = str_exitall_cb,
1302                 .help   = "Terminate all jobs when one exits",
1303         },
1304         {
1305                 .name   = "stonewall",
1306                 .type   = FIO_OPT_STR_SET,
1307                 .off1   = td_var_offset(stonewall),
1308                 .help   = "Insert a hard barrier between this job and previous",
1309         },
1310         {
1311                 .name   = "new_group",
1312                 .type   = FIO_OPT_STR_SET,
1313                 .off1   = td_var_offset(new_group),
1314                 .help   = "Mark the start of a new group (for reporting)",
1315         },
1316         {
1317                 .name   = "thread",
1318                 .type   = FIO_OPT_STR_SET,
1319                 .off1   = td_var_offset(use_thread),
1320                 .help   = "Use threads instead of forks",
1321         },
1322         {
1323                 .name   = "write_bw_log",
1324                 .type   = FIO_OPT_STR,
1325                 .off1   = td_var_offset(write_bw_log),
1326                 .cb     = str_write_bw_log_cb,
1327                 .help   = "Write log of bandwidth during run",
1328         },
1329         {
1330                 .name   = "write_lat_log",
1331                 .type   = FIO_OPT_STR,
1332                 .off1   = td_var_offset(write_lat_log),
1333                 .cb     = str_write_lat_log_cb,
1334                 .help   = "Write log of latency during run",
1335         },
1336         {
1337                 .name   = "hugepage-size",
1338                 .type   = FIO_OPT_STR_VAL_INT,
1339                 .off1   = td_var_offset(hugepage_size),
1340                 .help   = "When using hugepages, specify size of each page",
1341                 .def    = __stringify(FIO_HUGE_PAGE),
1342         },
1343         {
1344                 .name   = "group_reporting",
1345                 .type   = FIO_OPT_STR_SET,
1346                 .off1   = td_var_offset(group_reporting),
1347                 .help   = "Do reporting on a per-group basis",
1348         },
1349         {
1350                 .name   = "zero_buffers",
1351                 .type   = FIO_OPT_STR_SET,
1352                 .off1   = td_var_offset(zero_buffers),
1353                 .help   = "Init IO buffers to all zeroes",
1354         },
1355         {
1356                 .name   = "refill_buffers",
1357                 .type   = FIO_OPT_STR_SET,
1358                 .off1   = td_var_offset(refill_buffers),
1359                 .help   = "Refill IO buffers on every IO submit",
1360         },
1361 #ifdef FIO_HAVE_DISK_UTIL
1362         {
1363                 .name   = "disk_util",
1364                 .type   = FIO_OPT_BOOL,
1365                 .off1   = td_var_offset(do_disk_util),
1366                 .help   = "Log disk utilization statistics",
1367                 .def    = "1",
1368         },
1369 #endif
1370         {
1371                 .name   = "gtod_reduce",
1372                 .type   = FIO_OPT_BOOL,
1373                 .help   = "Greatly reduce number of gettimeofday() calls",
1374                 .cb     = str_gtod_reduce_cb,
1375                 .def    = "0",
1376         },
1377         {
1378                 .name   = "disable_clat",
1379                 .type   = FIO_OPT_BOOL,
1380                 .off1   = td_var_offset(disable_clat),
1381                 .help   = "Disable completion latency numbers",
1382                 .parent = "gtod_reduce",
1383                 .def    = "0",
1384         },
1385         {
1386                 .name   = "disable_slat",
1387                 .type   = FIO_OPT_BOOL,
1388                 .off1   = td_var_offset(disable_slat),
1389                 .help   = "Disable submissionn latency numbers",
1390                 .parent = "gtod_reduce",
1391                 .def    = "0",
1392         },
1393         {
1394                 .name   = "disable_bw_measurement",
1395                 .type   = FIO_OPT_BOOL,
1396                 .off1   = td_var_offset(disable_bw),
1397                 .help   = "Disable bandwidth logging",
1398                 .parent = "gtod_reduce",
1399                 .def    = "0",
1400         },
1401         {
1402                 .name   = "gtod_cpu",
1403                 .type   = FIO_OPT_INT,
1404                 .cb     = str_gtod_cpu_cb,
1405                 .help   = "Setup dedicated gettimeofday() thread on this CPU",
1406         },
1407         {
1408                 .name = NULL,
1409         },
1410 };
1411
1412 void fio_options_dup_and_init(struct option *long_options)
1413 {
1414         struct fio_option *o;
1415         unsigned int i;
1416
1417         options_init(options);
1418
1419         i = 0;
1420         while (long_options[i].name)
1421                 i++;
1422
1423         o = &options[0];
1424         while (o->name) {
1425                 long_options[i].name = (char *) o->name;
1426                 long_options[i].val = FIO_GETOPT_JOB;
1427                 if (o->type == FIO_OPT_STR_SET)
1428                         long_options[i].has_arg = no_argument;
1429                 else
1430                         long_options[i].has_arg = required_argument;
1431
1432                 i++;
1433                 o++;
1434                 assert(i < FIO_NR_OPTIONS);
1435         }
1436 }
1437
1438 int fio_options_parse(struct thread_data *td, char **opts, int num_opts)
1439 {
1440         int i, ret;
1441
1442         sort_options(opts, options, num_opts);
1443
1444         for (ret = 0, i = 0; i < num_opts; i++)
1445                 ret |= parse_option(opts[i], options, td);
1446
1447         return ret;
1448 }
1449
1450 int fio_cmd_option_parse(struct thread_data *td, const char *opt, char *val)
1451 {
1452         return parse_cmd_option(opt, val, options, td);
1453 }
1454
1455 void fio_fill_default_options(struct thread_data *td)
1456 {
1457         fill_default_options(td, options);
1458 }
1459
1460 int fio_show_option_help(const char *opt)
1461 {
1462         return show_cmd_help(options, opt);
1463 }
1464
1465 static void __options_mem(struct thread_data *td, int alloc)
1466 {
1467         struct thread_options *o = &td->o;
1468         struct fio_option *opt;
1469         char **ptr;
1470         int i;
1471
1472         for (i = 0, opt = &options[0]; opt->name; i++, opt = &options[i]) {
1473                 if (opt->type != FIO_OPT_STR_STORE)
1474                         continue;
1475
1476                 ptr = (void *) o + opt->off1;
1477                 if (*ptr) {
1478                         if (alloc)
1479                                 *ptr = strdup(*ptr);
1480                         else {
1481                                 free(*ptr);
1482                                 *ptr = NULL;
1483                         }
1484                 }
1485         }
1486 }
1487
1488 /*
1489  * dupe FIO_OPT_STR_STORE options
1490  */
1491 void options_mem_dupe(struct thread_data *td)
1492 {
1493         __options_mem(td, 1);
1494 }
1495
1496 void options_mem_free(struct thread_data fio_unused *td)
1497 {
1498 #if 0
1499         __options_mem(td, 0);
1500 #endif
1501 }