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