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