binject: ctl cleanups
[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 <assert.h>
7 #include <libgen.h>
8 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11
12 #include "fio.h"
13 #include "verify.h"
14 #include "parse.h"
15 #include "lib/fls.h"
16 #include "options.h"
17
18 #include "crc/crc32c.h"
19
20 /*
21  * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
22  */
23 static char *get_opt_postfix(const char *str)
24 {
25         char *p = strstr(str, ":");
26
27         if (!p)
28                 return NULL;
29
30         p++;
31         strip_blank_front(&p);
32         strip_blank_end(p);
33         return strdup(p);
34 }
35
36 static int converthexchartoint(char a)
37 {
38         int base;
39
40         switch(a) {
41         case '0'...'9':
42                 base = '0';
43                 break;
44         case 'A'...'F':
45                 base = 'A' - 10;
46                 break;
47         case 'a'...'f':
48                 base = 'a' - 10;
49                 break;
50         default:
51                 base = 0;
52         }
53         return (a - base);
54 }
55
56 static int bs_cmp(const void *p1, const void *p2)
57 {
58         const struct bssplit *bsp1 = p1;
59         const struct bssplit *bsp2 = p2;
60
61         return bsp1->perc < bsp2->perc;
62 }
63
64 static int bssplit_ddir(struct thread_data *td, int ddir, char *str)
65 {
66         struct bssplit *bssplit;
67         unsigned int i, perc, perc_missing;
68         unsigned int max_bs, min_bs;
69         long long val;
70         char *fname;
71
72         td->o.bssplit_nr[ddir] = 4;
73         bssplit = malloc(4 * sizeof(struct bssplit));
74
75         i = 0;
76         max_bs = 0;
77         min_bs = -1;
78         while ((fname = strsep(&str, ":")) != NULL) {
79                 char *perc_str;
80
81                 if (!strlen(fname))
82                         break;
83
84                 /*
85                  * grow struct buffer, if needed
86                  */
87                 if (i == td->o.bssplit_nr[ddir]) {
88                         td->o.bssplit_nr[ddir] <<= 1;
89                         bssplit = realloc(bssplit, td->o.bssplit_nr[ddir]
90                                                   * sizeof(struct bssplit));
91                 }
92
93                 perc_str = strstr(fname, "/");
94                 if (perc_str) {
95                         *perc_str = '\0';
96                         perc_str++;
97                         perc = atoi(perc_str);
98                         if (perc > 100)
99                                 perc = 100;
100                         else if (!perc)
101                                 perc = -1;
102                 } else
103                         perc = -1;
104
105                 if (str_to_decimal(fname, &val, 1, td)) {
106                         log_err("fio: bssplit conversion failed\n");
107                         free(td->o.bssplit);
108                         return 1;
109                 }
110
111                 if (val > max_bs)
112                         max_bs = val;
113                 if (val < min_bs)
114                         min_bs = val;
115
116                 bssplit[i].bs = val;
117                 bssplit[i].perc = perc;
118                 i++;
119         }
120
121         td->o.bssplit_nr[ddir] = i;
122
123         /*
124          * Now check if the percentages add up, and how much is missing
125          */
126         perc = perc_missing = 0;
127         for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
128                 struct bssplit *bsp = &bssplit[i];
129
130                 if (bsp->perc == (unsigned char) -1)
131                         perc_missing++;
132                 else
133                         perc += bsp->perc;
134         }
135
136         if (perc > 100) {
137                 log_err("fio: bssplit percentages add to more than 100%%\n");
138                 free(bssplit);
139                 return 1;
140         }
141         /*
142          * If values didn't have a percentage set, divide the remains between
143          * them.
144          */
145         if (perc_missing) {
146                 for (i = 0; i < td->o.bssplit_nr[ddir]; i++) {
147                         struct bssplit *bsp = &bssplit[i];
148
149                         if (bsp->perc == (unsigned char) -1)
150                                 bsp->perc = (100 - perc) / perc_missing;
151                 }
152         }
153
154         td->o.min_bs[ddir] = min_bs;
155         td->o.max_bs[ddir] = max_bs;
156
157         /*
158          * now sort based on percentages, for ease of lookup
159          */
160         qsort(bssplit, td->o.bssplit_nr[ddir], sizeof(struct bssplit), bs_cmp);
161         td->o.bssplit[ddir] = bssplit;
162         return 0;
163
164 }
165
166 static int str_bssplit_cb(void *data, const char *input)
167 {
168         struct thread_data *td = data;
169         char *str, *p, *odir;
170         int ret = 0;
171
172         p = str = strdup(input);
173
174         strip_blank_front(&str);
175         strip_blank_end(str);
176
177         odir = strchr(str, ',');
178         if (odir) {
179                 ret = bssplit_ddir(td, DDIR_WRITE, odir + 1);
180                 if (!ret) {
181                         *odir = '\0';
182                         ret = bssplit_ddir(td, DDIR_READ, str);
183                 }
184         } else {
185                 char *op;
186
187                 op = strdup(str);
188
189                 ret = bssplit_ddir(td, DDIR_READ, str);
190                 if (!ret)
191                         ret = bssplit_ddir(td, DDIR_WRITE, op);
192
193                 free(op);
194         }
195
196         free(p);
197         return ret;
198 }
199
200 static int str_rw_cb(void *data, const char *str)
201 {
202         struct thread_data *td = data;
203         char *nr = get_opt_postfix(str);
204
205         td->o.ddir_seq_nr = 1;
206         if (nr) {
207                 td->o.ddir_seq_nr = atoi(nr);
208                 free(nr);
209         }
210
211         return 0;
212 }
213
214 static int str_mem_cb(void *data, const char *mem)
215 {
216         struct thread_data *td = data;
217
218         if (td->o.mem_type == MEM_MMAPHUGE || td->o.mem_type == MEM_MMAP) {
219                 td->mmapfile = get_opt_postfix(mem);
220                 if (td->o.mem_type == MEM_MMAPHUGE && !td->mmapfile) {
221                         log_err("fio: mmaphuge:/path/to/file\n");
222                         return 1;
223                 }
224         }
225
226         return 0;
227 }
228
229 static int str_verify_cb(void *data, const char *mem)
230 {
231         struct thread_data *td = data;
232
233         if (td->o.verify != VERIFY_CRC32C_INTEL)
234                 return 0;
235
236         if (!crc32c_intel_works()) {
237                 log_info("fio: System does not support hw accelerated crc32c. Falling back to sw crc32c.\n");
238                 td->o.verify = VERIFY_CRC32C;
239         }
240
241         return 0;
242 }
243
244 static int fio_clock_source_cb(void *data, const char *str)
245 {
246         struct thread_data *td = data;
247
248         fio_clock_source = td->o.clocksource;
249         fio_time_init();
250         return 0;
251 }
252
253 static int str_lockmem_cb(void fio_unused *data, unsigned long long *val)
254 {
255         mlock_size = *val;
256         return 0;
257 }
258
259 static int str_rwmix_read_cb(void *data, unsigned long long *val)
260 {
261         struct thread_data *td = data;
262
263         td->o.rwmix[DDIR_READ] = *val;
264         td->o.rwmix[DDIR_WRITE] = 100 - *val;
265         return 0;
266 }
267
268 static int str_rwmix_write_cb(void *data, unsigned long long *val)
269 {
270         struct thread_data *td = data;
271
272         td->o.rwmix[DDIR_WRITE] = *val;
273         td->o.rwmix[DDIR_READ] = 100 - *val;
274         return 0;
275 }
276
277 #ifdef FIO_HAVE_IOPRIO
278 static int str_prioclass_cb(void *data, unsigned long long *val)
279 {
280         struct thread_data *td = data;
281         unsigned short mask;
282
283         /*
284          * mask off old class bits, str_prio_cb() may have set a default class
285          */
286         mask = (1 << IOPRIO_CLASS_SHIFT) - 1;
287         td->ioprio &= mask;
288
289         td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
290         td->ioprio_set = 1;
291         return 0;
292 }
293
294 static int str_prio_cb(void *data, unsigned long long *val)
295 {
296         struct thread_data *td = data;
297
298         td->ioprio |= *val;
299
300         /*
301          * If no class is set, assume BE
302          */
303         if ((td->ioprio >> IOPRIO_CLASS_SHIFT) == 0)
304                 td->ioprio |= IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT;
305
306         td->ioprio_set = 1;
307         return 0;
308 }
309 #endif
310
311 static int str_exitall_cb(void)
312 {
313         exitall_on_terminate = 1;
314         return 0;
315 }
316
317 #ifdef FIO_HAVE_CPU_AFFINITY
318 static int str_cpumask_cb(void *data, unsigned long long *val)
319 {
320         struct thread_data *td = data;
321         unsigned int i;
322         long max_cpu;
323         int ret;
324
325         ret = fio_cpuset_init(&td->o.cpumask);
326         if (ret < 0) {
327                 log_err("fio: cpuset_init failed\n");
328                 td_verror(td, ret, "fio_cpuset_init");
329                 return 1;
330         }
331
332         max_cpu = sysconf(_SC_NPROCESSORS_ONLN);
333
334         for (i = 0; i < sizeof(int) * 8; i++) {
335                 if ((1 << i) & *val) {
336                         if (i > max_cpu) {
337                                 log_err("fio: CPU %d too large (max=%ld)\n", i,
338                                                                 max_cpu);
339                                 return 1;
340                         }
341                         dprint(FD_PARSE, "set cpu allowed %d\n", i);
342                         fio_cpu_set(&td->o.cpumask, i);
343                 }
344         }
345
346         td->o.cpumask_set = 1;
347         return 0;
348 }
349
350 static int set_cpus_allowed(struct thread_data *td, os_cpu_mask_t *mask,
351                             const char *input)
352 {
353         char *cpu, *str, *p;
354         long max_cpu;
355         int ret = 0;
356
357         ret = fio_cpuset_init(mask);
358         if (ret < 0) {
359                 log_err("fio: cpuset_init failed\n");
360                 td_verror(td, ret, "fio_cpuset_init");
361                 return 1;
362         }
363
364         p = str = strdup(input);
365
366         strip_blank_front(&str);
367         strip_blank_end(str);
368
369         max_cpu = sysconf(_SC_NPROCESSORS_ONLN);
370
371         while ((cpu = strsep(&str, ",")) != NULL) {
372                 char *str2, *cpu2;
373                 int icpu, icpu2;
374
375                 if (!strlen(cpu))
376                         break;
377
378                 str2 = cpu;
379                 icpu2 = -1;
380                 while ((cpu2 = strsep(&str2, "-")) != NULL) {
381                         if (!strlen(cpu2))
382                                 break;
383
384                         icpu2 = atoi(cpu2);
385                 }
386
387                 icpu = atoi(cpu);
388                 if (icpu2 == -1)
389                         icpu2 = icpu;
390                 while (icpu <= icpu2) {
391                         if (icpu >= FIO_MAX_CPUS) {
392                                 log_err("fio: your OS only supports up to"
393                                         " %d CPUs\n", (int) FIO_MAX_CPUS);
394                                 ret = 1;
395                                 break;
396                         }
397                         if (icpu > max_cpu) {
398                                 log_err("fio: CPU %d too large (max=%ld)\n",
399                                                         icpu, max_cpu);
400                                 ret = 1;
401                                 break;
402                         }
403
404                         dprint(FD_PARSE, "set cpu allowed %d\n", icpu);
405                         fio_cpu_set(mask, icpu);
406                         icpu++;
407                 }
408                 if (ret)
409                         break;
410         }
411
412         free(p);
413         if (!ret)
414                 td->o.cpumask_set = 1;
415         return ret;
416 }
417
418 static int str_cpus_allowed_cb(void *data, const char *input)
419 {
420         struct thread_data *td = data;
421         int ret;
422
423         ret = set_cpus_allowed(td, &td->o.cpumask, input);
424         if (!ret)
425                 td->o.cpumask_set = 1;
426
427         return ret;
428 }
429
430 static int str_verify_cpus_allowed_cb(void *data, const char *input)
431 {
432         struct thread_data *td = data;
433         int ret;
434
435         ret = set_cpus_allowed(td, &td->o.verify_cpumask, input);
436         if (!ret)
437                 td->o.verify_cpumask_set = 1;
438
439         return ret;
440 }
441 #endif
442
443 #ifdef FIO_HAVE_TRIM
444 static int str_verify_trim_cb(void *data, unsigned long long *val)
445 {
446         struct thread_data *td = data;
447
448         td->o.trim_percentage = *val;
449         return 0;
450 }
451 #endif
452
453 static int str_fst_cb(void *data, const char *str)
454 {
455         struct thread_data *td = data;
456         char *nr = get_opt_postfix(str);
457
458         td->file_service_nr = 1;
459         if (nr) {
460                 td->file_service_nr = atoi(nr);
461                 free(nr);
462         }
463
464         return 0;
465 }
466
467 #ifdef FIO_HAVE_SYNC_FILE_RANGE
468 static int str_sfr_cb(void *data, const char *str)
469 {
470         struct thread_data *td = data;
471         char *nr = get_opt_postfix(str);
472
473         td->sync_file_range_nr = 1;
474         if (nr) {
475                 td->sync_file_range_nr = atoi(nr);
476                 free(nr);
477         }
478
479         return 0;
480 }
481 #endif
482
483 static int check_dir(struct thread_data *td, char *fname)
484 {
485         char file[PATH_MAX], *dir;
486         int elen = 0;
487
488         if (td->o.directory) {
489                 strcpy(file, td->o.directory);
490                 strcat(file, "/");
491                 elen = strlen(file);
492         }
493
494         sprintf(file + elen, "%s", fname);
495         dir = dirname(file);
496
497 #if 0
498         {
499         struct stat sb;
500         /*
501          * We can't do this on FIO_DISKLESSIO engines. The engine isn't loaded
502          * yet, so we can't do this check right here...
503          */
504         if (lstat(dir, &sb) < 0) {
505                 int ret = errno;
506
507                 log_err("fio: %s is not a directory\n", dir);
508                 td_verror(td, ret, "lstat");
509                 return 1;
510         }
511
512         if (!S_ISDIR(sb.st_mode)) {
513                 log_err("fio: %s is not a directory\n", dir);
514                 return 1;
515         }
516         }
517 #endif
518
519         return 0;
520 }
521
522 /*
523  * Return next file in the string. Files are separated with ':'. If the ':'
524  * is escaped with a '\', then that ':' is part of the filename and does not
525  * indicate a new file.
526  */
527 static char *get_next_file_name(char **ptr)
528 {
529         char *str = *ptr;
530         char *p, *start;
531
532         if (!str || !strlen(str))
533                 return NULL;
534
535         start = str;
536         do {
537                 /*
538                  * No colon, we are done
539                  */
540                 p = strchr(str, ':');
541                 if (!p) {
542                         *ptr = NULL;
543                         break;
544                 }
545
546                 /*
547                  * We got a colon, but it's the first character. Skip and
548                  * continue
549                  */
550                 if (p == start) {
551                         str = ++start;
552                         continue;
553                 }
554
555                 if (*(p - 1) != '\\') {
556                         *p = '\0';
557                         *ptr = p + 1;
558                         break;
559                 }
560
561                 memmove(p - 1, p, strlen(p) + 1);
562                 str = p;
563         } while (1);
564
565         return start;
566 }
567
568 static int str_filename_cb(void *data, const char *input)
569 {
570         struct thread_data *td = data;
571         char *fname, *str, *p;
572
573         p = str = strdup(input);
574
575         strip_blank_front(&str);
576         strip_blank_end(str);
577
578         if (!td->files_index)
579                 td->o.nr_files = 0;
580
581         while ((fname = get_next_file_name(&str)) != NULL) {
582                 if (!strlen(fname))
583                         break;
584                 if (check_dir(td, fname)) {
585                         free(p);
586                         return 1;
587                 }
588                 add_file(td, fname);
589                 td->o.nr_files++;
590         }
591
592         free(p);
593         return 0;
594 }
595
596 static int str_directory_cb(void *data, const char fio_unused *str)
597 {
598         struct thread_data *td = data;
599         struct stat sb;
600
601         if (lstat(td->o.directory, &sb) < 0) {
602                 int ret = errno;
603
604                 log_err("fio: %s is not a directory\n", td->o.directory);
605                 td_verror(td, ret, "lstat");
606                 return 1;
607         }
608         if (!S_ISDIR(sb.st_mode)) {
609                 log_err("fio: %s is not a directory\n", td->o.directory);
610                 return 1;
611         }
612
613         return 0;
614 }
615
616 static int str_opendir_cb(void *data, const char fio_unused *str)
617 {
618         struct thread_data *td = data;
619
620         if (!td->files_index)
621                 td->o.nr_files = 0;
622
623         return add_dir_files(td, td->o.opendir);
624 }
625
626 static int str_verify_offset_cb(void *data, unsigned long long *off)
627 {
628         struct thread_data *td = data;
629
630         if (*off && *off < sizeof(struct verify_header)) {
631                 log_err("fio: verify_offset too small\n");
632                 return 1;
633         }
634
635         td->o.verify_offset = *off;
636         return 0;
637 }
638
639 static int str_verify_pattern_cb(void *data, const char *input)
640 {
641         struct thread_data *td = data;
642         long off;
643         int i = 0, j = 0, len, k, base = 10;
644         char* loc1, * loc2;
645
646         loc1 = strstr(input, "0x");
647         loc2 = strstr(input, "0X");
648         if (loc1 || loc2)
649                 base = 16;
650         off = strtol(input, NULL, base);
651         if (off != LONG_MAX || errno != ERANGE) {
652                 while (off) {
653                         td->o.verify_pattern[i] = off & 0xff;
654                         off >>= 8;
655                         i++;
656                 }
657         } else {
658                 len = strlen(input);
659                 k = len - 1;
660                 if (base == 16) {
661                         if (loc1)
662                                 j = loc1 - input + 2;
663                         else
664                                 j = loc2 - input + 2;
665                 } else
666                         return 1;
667                 if (len - j < MAX_PATTERN_SIZE * 2) {
668                         while (k >= j) {
669                                 off = converthexchartoint(input[k--]);
670                                 if (k >= j)
671                                         off += (converthexchartoint(input[k--])
672                                                 * 16);
673                                 td->o.verify_pattern[i++] = (char) off;
674                         }
675                 }
676         }
677         td->o.verify_pattern_bytes = i;
678         return 0;
679 }
680
681 static int str_lockfile_cb(void *data, const char *str)
682 {
683         struct thread_data *td = data;
684         char *nr = get_opt_postfix(str);
685
686         td->o.lockfile_batch = 1;
687         if (nr) {
688                 td->o.lockfile_batch = atoi(nr);
689                 free(nr);
690         }
691
692         return 0;
693 }
694
695 static int str_write_bw_log_cb(void *data, const char *str)
696 {
697         struct thread_data *td = data;
698
699         if (str)
700                 td->o.bw_log_file = strdup(str);
701
702         td->o.write_bw_log = 1;
703         return 0;
704 }
705
706 static int str_write_lat_log_cb(void *data, const char *str)
707 {
708         struct thread_data *td = data;
709
710         if (str)
711                 td->o.lat_log_file = strdup(str);
712
713         td->o.write_lat_log = 1;
714         return 0;
715 }
716
717 static int str_gtod_reduce_cb(void *data, int *il)
718 {
719         struct thread_data *td = data;
720         int val = *il;
721
722         td->o.disable_lat = !!val;
723         td->o.disable_clat = !!val;
724         td->o.disable_slat = !!val;
725         td->o.disable_bw = !!val;
726         if (val)
727                 td->tv_cache_mask = 63;
728
729         return 0;
730 }
731
732 static int str_gtod_cpu_cb(void *data, long long *il)
733 {
734         struct thread_data *td = data;
735         int val = *il;
736
737         td->o.gtod_cpu = val;
738         td->o.gtod_offload = 1;
739         return 0;
740 }
741
742 static int rw_verify(struct fio_option *o, void *data)
743 {
744         struct thread_data *td = data;
745
746         if (read_only && td_write(td)) {
747                 log_err("fio: job <%s> has write bit set, but fio is in"
748                         " read-only mode\n", td->o.name);
749                 return 1;
750         }
751
752         return 0;
753 }
754
755 static int gtod_cpu_verify(struct fio_option *o, void *data)
756 {
757 #ifndef FIO_HAVE_CPU_AFFINITY
758         struct thread_data *td = data;
759
760         if (td->o.gtod_cpu) {
761                 log_err("fio: platform must support CPU affinity for"
762                         "gettimeofday() offloading\n");
763                 return 1;
764         }
765 #endif
766
767         return 0;
768 }
769
770 static int kb_base_verify(struct fio_option *o, void *data)
771 {
772         struct thread_data *td = data;
773
774         if (td->o.kb_base != 1024 && td->o.kb_base != 1000) {
775                 log_err("fio: kb_base set to nonsensical value: %u\n",
776                                 td->o.kb_base);
777                 return 1;
778         }
779
780         return 0;
781 }
782
783 #define __stringify_1(x)        #x
784 #define __stringify(x)          __stringify_1(x)
785
786 /*
787  * Map of job/command line options
788  */
789 static struct fio_option options[FIO_MAX_OPTS] = {
790         {
791                 .name   = "description",
792                 .type   = FIO_OPT_STR_STORE,
793                 .off1   = td_var_offset(description),
794                 .help   = "Text job description",
795         },
796         {
797                 .name   = "name",
798                 .type   = FIO_OPT_STR_STORE,
799                 .off1   = td_var_offset(name),
800                 .help   = "Name of this job",
801         },
802         {
803                 .name   = "directory",
804                 .type   = FIO_OPT_STR_STORE,
805                 .off1   = td_var_offset(directory),
806                 .cb     = str_directory_cb,
807                 .help   = "Directory to store files in",
808         },
809         {
810                 .name   = "filename",
811                 .type   = FIO_OPT_STR_STORE,
812                 .off1   = td_var_offset(filename),
813                 .cb     = str_filename_cb,
814                 .prio   = -1, /* must come after "directory" */
815                 .help   = "File(s) to use for the workload",
816         },
817         {
818                 .name   = "kb_base",
819                 .type   = FIO_OPT_INT,
820                 .off1   = td_var_offset(kb_base),
821                 .verify = kb_base_verify,
822                 .prio   = 1,
823                 .def    = "1024",
824                 .help   = "How many bytes per KB for reporting (1000 or 1024)",
825         },
826         {
827                 .name   = "lockfile",
828                 .type   = FIO_OPT_STR,
829                 .cb     = str_lockfile_cb,
830                 .off1   = td_var_offset(file_lock_mode),
831                 .help   = "Lock file when doing IO to it",
832                 .parent = "filename",
833                 .def    = "none",
834                 .posval = {
835                           { .ival = "none",
836                             .oval = FILE_LOCK_NONE,
837                             .help = "No file locking",
838                           },
839                           { .ival = "exclusive",
840                             .oval = FILE_LOCK_EXCLUSIVE,
841                             .help = "Exclusive file lock",
842                           },
843                           {
844                             .ival = "readwrite",
845                             .oval = FILE_LOCK_READWRITE,
846                             .help = "Read vs write lock",
847                           },
848                 },
849         },
850         {
851                 .name   = "opendir",
852                 .type   = FIO_OPT_STR_STORE,
853                 .off1   = td_var_offset(opendir),
854                 .cb     = str_opendir_cb,
855                 .help   = "Recursively add files from this directory and down",
856         },
857         {
858                 .name   = "rw",
859                 .alias  = "readwrite",
860                 .type   = FIO_OPT_STR,
861                 .cb     = str_rw_cb,
862                 .off1   = td_var_offset(td_ddir),
863                 .help   = "IO direction",
864                 .def    = "read",
865                 .verify = rw_verify,
866                 .posval = {
867                           { .ival = "read",
868                             .oval = TD_DDIR_READ,
869                             .help = "Sequential read",
870                           },
871                           { .ival = "write",
872                             .oval = TD_DDIR_WRITE,
873                             .help = "Sequential write",
874                           },
875                           { .ival = "randread",
876                             .oval = TD_DDIR_RANDREAD,
877                             .help = "Random read",
878                           },
879                           { .ival = "randwrite",
880                             .oval = TD_DDIR_RANDWRITE,
881                             .help = "Random write",
882                           },
883                           { .ival = "rw",
884                             .oval = TD_DDIR_RW,
885                             .help = "Sequential read and write mix",
886                           },
887                           { .ival = "randrw",
888                             .oval = TD_DDIR_RANDRW,
889                             .help = "Random read and write mix"
890                           },
891                 },
892         },
893         {
894                 .name   = "rw_sequencer",
895                 .type   = FIO_OPT_STR,
896                 .off1   = td_var_offset(rw_seq),
897                 .help   = "IO offset generator modifier",
898                 .def    = "sequential",
899                 .posval = {
900                           { .ival = "sequential",
901                             .oval = RW_SEQ_SEQ,
902                             .help = "Generate sequential offsets",
903                           },
904                           { .ival = "identical",
905                             .oval = RW_SEQ_IDENT,
906                             .help = "Generate identical offsets",
907                           },
908                 },
909         },
910
911         {
912                 .name   = "ioengine",
913                 .type   = FIO_OPT_STR_STORE,
914                 .off1   = td_var_offset(ioengine),
915                 .help   = "IO engine to use",
916                 .def    = "sync",
917                 .posval = {
918                           { .ival = "sync",
919                             .help = "Use read/write",
920                           },
921                           { .ival = "psync",
922                             .help = "Use pread/pwrite",
923                           },
924                           { .ival = "vsync",
925                              .help = "Use readv/writev",
926                           },
927 #ifdef FIO_HAVE_LIBAIO
928                           { .ival = "libaio",
929                             .help = "Linux native asynchronous IO",
930                           },
931 #endif
932 #ifdef FIO_HAVE_POSIXAIO
933                           { .ival = "posixaio",
934                             .help = "POSIX asynchronous IO",
935                           },
936 #endif
937 #ifdef FIO_HAVE_SOLARISAIO
938                           { .ival = "solarisaio",
939                             .help = "Solaris native asynchronous IO",
940                           },
941 #endif
942                           { .ival = "mmap",
943                             .help = "Memory mapped IO",
944                           },
945 #ifdef FIO_HAVE_SPLICE
946                           { .ival = "splice",
947                             .help = "splice/vmsplice based IO",
948                           },
949                           { .ival = "netsplice",
950                             .help = "splice/vmsplice to/from the network",
951                           },
952 #endif
953 #ifdef FIO_HAVE_SGIO
954                           { .ival = "sg",
955                             .help = "SCSI generic v3 IO",
956                           },
957 #endif
958                           { .ival = "null",
959                             .help = "Testing engine (no data transfer)",
960                           },
961                           { .ival = "net",
962                             .help = "Network IO",
963                           },
964 #ifdef FIO_HAVE_SYSLET
965                           { .ival = "syslet-rw",
966                             .help = "syslet enabled async pread/pwrite IO",
967                           },
968 #endif
969                           { .ival = "cpuio",
970                             .help = "CPU cycler burner engine",
971                           },
972 #ifdef FIO_HAVE_GUASI
973                           { .ival = "guasi",
974                             .help = "GUASI IO engine",
975                           },
976 #endif
977 #ifdef FIO_HAVE_BINJECT
978                           { .ival = "binject",
979                             .help = "binject direct inject block engine",
980                           },
981 #endif
982                           { .ival = "external",
983                             .help = "Load external engine (append name)",
984                           },
985                 },
986         },
987         {
988                 .name   = "iodepth",
989                 .type   = FIO_OPT_INT,
990                 .off1   = td_var_offset(iodepth),
991                 .help   = "Amount of IO buffers to keep in flight",
992                 .minval = 1,
993                 .def    = "1",
994         },
995         {
996                 .name   = "iodepth_batch",
997                 .alias  = "iodepth_batch_submit",
998                 .type   = FIO_OPT_INT,
999                 .off1   = td_var_offset(iodepth_batch),
1000                 .help   = "Number of IO buffers to submit in one go",
1001                 .parent = "iodepth",
1002                 .minval = 1,
1003                 .def    = "1",
1004         },
1005         {
1006                 .name   = "iodepth_batch_complete",
1007                 .type   = FIO_OPT_INT,
1008                 .off1   = td_var_offset(iodepth_batch_complete),
1009                 .help   = "Number of IO buffers to retrieve in one go",
1010                 .parent = "iodepth",
1011                 .minval = 0,
1012                 .def    = "1",
1013         },
1014         {
1015                 .name   = "iodepth_low",
1016                 .type   = FIO_OPT_INT,
1017                 .off1   = td_var_offset(iodepth_low),
1018                 .help   = "Low water mark for queuing depth",
1019                 .parent = "iodepth",
1020         },
1021         {
1022                 .name   = "size",
1023                 .type   = FIO_OPT_STR_VAL,
1024                 .off1   = td_var_offset(size),
1025                 .minval = 1,
1026                 .help   = "Total size of device or files",
1027         },
1028         {
1029                 .name   = "fill_device",
1030                 .type   = FIO_OPT_BOOL,
1031                 .off1   = td_var_offset(fill_device),
1032                 .help   = "Write until an ENOSPC error occurs",
1033                 .def    = "0",
1034         },
1035         {
1036                 .name   = "filesize",
1037                 .type   = FIO_OPT_STR_VAL,
1038                 .off1   = td_var_offset(file_size_low),
1039                 .off2   = td_var_offset(file_size_high),
1040                 .minval = 1,
1041                 .help   = "Size of individual files",
1042         },
1043         {
1044                 .name   = "offset",
1045                 .alias  = "fileoffset",
1046                 .type   = FIO_OPT_STR_VAL,
1047                 .off1   = td_var_offset(start_offset),
1048                 .help   = "Start IO from this offset",
1049                 .def    = "0",
1050         },
1051         {
1052                 .name   = "bs",
1053                 .alias  = "blocksize",
1054                 .type   = FIO_OPT_INT,
1055                 .off1   = td_var_offset(bs[DDIR_READ]),
1056                 .off2   = td_var_offset(bs[DDIR_WRITE]),
1057                 .minval = 1,
1058                 .help   = "Block size unit",
1059                 .def    = "4k",
1060                 .parent = "rw",
1061         },
1062         {
1063                 .name   = "ba",
1064                 .alias  = "blockalign",
1065                 .type   = FIO_OPT_INT,
1066                 .off1   = td_var_offset(ba[DDIR_READ]),
1067                 .off2   = td_var_offset(ba[DDIR_WRITE]),
1068                 .minval = 1,
1069                 .help   = "IO block offset alignment",
1070                 .parent = "rw",
1071         },
1072         {
1073                 .name   = "bsrange",
1074                 .alias  = "blocksize_range",
1075                 .type   = FIO_OPT_RANGE,
1076                 .off1   = td_var_offset(min_bs[DDIR_READ]),
1077                 .off2   = td_var_offset(max_bs[DDIR_READ]),
1078                 .off3   = td_var_offset(min_bs[DDIR_WRITE]),
1079                 .off4   = td_var_offset(max_bs[DDIR_WRITE]),
1080                 .minval = 1,
1081                 .help   = "Set block size range (in more detail than bs)",
1082                 .parent = "rw",
1083         },
1084         {
1085                 .name   = "bssplit",
1086                 .type   = FIO_OPT_STR,
1087                 .cb     = str_bssplit_cb,
1088                 .help   = "Set a specific mix of block sizes",
1089                 .parent = "rw",
1090         },
1091         {
1092                 .name   = "bs_unaligned",
1093                 .alias  = "blocksize_unaligned",
1094                 .type   = FIO_OPT_STR_SET,
1095                 .off1   = td_var_offset(bs_unaligned),
1096                 .help   = "Don't sector align IO buffer sizes",
1097                 .parent = "rw",
1098         },
1099         {
1100                 .name   = "randrepeat",
1101                 .type   = FIO_OPT_BOOL,
1102                 .off1   = td_var_offset(rand_repeatable),
1103                 .help   = "Use repeatable random IO pattern",
1104                 .def    = "1",
1105                 .parent = "rw",
1106         },
1107         {
1108                 .name   = "norandommap",
1109                 .type   = FIO_OPT_STR_SET,
1110                 .off1   = td_var_offset(norandommap),
1111                 .help   = "Accept potential duplicate random blocks",
1112                 .parent = "rw",
1113         },
1114         {
1115                 .name   = "softrandommap",
1116                 .type   = FIO_OPT_BOOL,
1117                 .off1   = td_var_offset(softrandommap),
1118                 .help   = "Set norandommap if randommap allocation fails",
1119                 .parent = "norandommap",
1120                 .def    = "0",
1121         },
1122         {
1123                 .name   = "nrfiles",
1124                 .type   = FIO_OPT_INT,
1125                 .off1   = td_var_offset(nr_files),
1126                 .help   = "Split job workload between this number of files",
1127                 .def    = "1",
1128         },
1129         {
1130                 .name   = "openfiles",
1131                 .type   = FIO_OPT_INT,
1132                 .off1   = td_var_offset(open_files),
1133                 .help   = "Number of files to keep open at the same time",
1134         },
1135         {
1136                 .name   = "file_service_type",
1137                 .type   = FIO_OPT_STR,
1138                 .cb     = str_fst_cb,
1139                 .off1   = td_var_offset(file_service_type),
1140                 .help   = "How to select which file to service next",
1141                 .def    = "roundrobin",
1142                 .posval = {
1143                           { .ival = "random",
1144                             .oval = FIO_FSERVICE_RANDOM,
1145                             .help = "Choose a file at random",
1146                           },
1147                           { .ival = "roundrobin",
1148                             .oval = FIO_FSERVICE_RR,
1149                             .help = "Round robin select files",
1150                           },
1151                           { .ival = "sequential",
1152                             .oval = FIO_FSERVICE_SEQ,
1153                             .help = "Finish one file before moving to the next",
1154                           },
1155                 },
1156                 .parent = "nrfiles",
1157         },
1158 #ifdef FIO_HAVE_FALLOCATE
1159         {
1160                 .name   = "fallocate",
1161                 .type   = FIO_OPT_BOOL,
1162                 .off1   = td_var_offset(fallocate),
1163                 .help   = "Use fallocate() when laying out files",
1164                 .def    = "1",
1165         },
1166 #endif
1167         {
1168                 .name   = "fadvise_hint",
1169                 .type   = FIO_OPT_BOOL,
1170                 .off1   = td_var_offset(fadvise_hint),
1171                 .help   = "Use fadvise() to advise the kernel on IO pattern",
1172                 .def    = "1",
1173         },
1174         {
1175                 .name   = "fsync",
1176                 .type   = FIO_OPT_INT,
1177                 .off1   = td_var_offset(fsync_blocks),
1178                 .help   = "Issue fsync for writes every given number of blocks",
1179                 .def    = "0",
1180         },
1181         {
1182                 .name   = "fdatasync",
1183                 .type   = FIO_OPT_INT,
1184                 .off1   = td_var_offset(fdatasync_blocks),
1185                 .help   = "Issue fdatasync for writes every given number of blocks",
1186                 .def    = "0",
1187         },
1188         {
1189                 .name   = "write_barrier",
1190                 .type   = FIO_OPT_INT,
1191                 .off1   = td_var_offset(barrier_blocks),
1192                 .help   = "Make every Nth write a barrier write",
1193                 .def    = "0",
1194         },
1195 #ifdef FIO_HAVE_SYNC_FILE_RANGE
1196         {
1197                 .name   = "sync_file_range",
1198                 .posval = {
1199                           { .ival = "wait_before",
1200                             .oval = SYNC_FILE_RANGE_WAIT_BEFORE,
1201                             .help = "SYNC_FILE_RANGE_WAIT_BEFORE",
1202                             .or   = 1,
1203                           },
1204                           { .ival = "write",
1205                             .oval = SYNC_FILE_RANGE_WRITE,
1206                             .help = "SYNC_FILE_RANGE_WRITE",
1207                             .or   = 1,
1208                           },
1209                           {
1210                             .ival = "wait_after",
1211                             .oval = SYNC_FILE_RANGE_WAIT_AFTER,
1212                             .help = "SYNC_FILE_RANGE_WAIT_AFTER",
1213                             .or   = 1,
1214                           },
1215                 },
1216                 .type   = FIO_OPT_STR_MULTI,
1217                 .cb     = str_sfr_cb,
1218                 .off1   = td_var_offset(sync_file_range),
1219                 .help   = "Use sync_file_range()",
1220         },
1221 #endif
1222         {
1223                 .name   = "direct",
1224                 .type   = FIO_OPT_BOOL,
1225                 .off1   = td_var_offset(odirect),
1226                 .help   = "Use O_DIRECT IO (negates buffered)",
1227                 .def    = "0",
1228         },
1229         {
1230                 .name   = "buffered",
1231                 .type   = FIO_OPT_BOOL,
1232                 .off1   = td_var_offset(odirect),
1233                 .neg    = 1,
1234                 .help   = "Use buffered IO (negates direct)",
1235                 .def    = "1",
1236         },
1237         {
1238                 .name   = "overwrite",
1239                 .type   = FIO_OPT_BOOL,
1240                 .off1   = td_var_offset(overwrite),
1241                 .help   = "When writing, set whether to overwrite current data",
1242                 .def    = "0",
1243         },
1244         {
1245                 .name   = "loops",
1246                 .type   = FIO_OPT_INT,
1247                 .off1   = td_var_offset(loops),
1248                 .help   = "Number of times to run the job",
1249                 .def    = "1",
1250         },
1251         {
1252                 .name   = "numjobs",
1253                 .type   = FIO_OPT_INT,
1254                 .off1   = td_var_offset(numjobs),
1255                 .help   = "Duplicate this job this many times",
1256                 .def    = "1",
1257         },
1258         {
1259                 .name   = "startdelay",
1260                 .type   = FIO_OPT_STR_VAL_TIME,
1261                 .off1   = td_var_offset(start_delay),
1262                 .help   = "Only start job when this period has passed",
1263                 .def    = "0",
1264         },
1265         {
1266                 .name   = "runtime",
1267                 .alias  = "timeout",
1268                 .type   = FIO_OPT_STR_VAL_TIME,
1269                 .off1   = td_var_offset(timeout),
1270                 .help   = "Stop workload when this amount of time has passed",
1271                 .def    = "0",
1272         },
1273         {
1274                 .name   = "time_based",
1275                 .type   = FIO_OPT_STR_SET,
1276                 .off1   = td_var_offset(time_based),
1277                 .help   = "Keep running until runtime/timeout is met",
1278         },
1279         {
1280                 .name   = "ramp_time",
1281                 .type   = FIO_OPT_STR_VAL_TIME,
1282                 .off1   = td_var_offset(ramp_time),
1283                 .help   = "Ramp up time before measuring performance",
1284         },
1285         {
1286                 .name   = "clocksource",
1287                 .type   = FIO_OPT_STR,
1288                 .cb     = fio_clock_source_cb,
1289                 .off1   = td_var_offset(clocksource),
1290                 .help   = "What type of timing source to use",
1291                 .posval = {
1292                           { .ival = "gettimeofday",
1293                             .oval = CS_GTOD,
1294                             .help = "Use gettimeofday(2) for timing",
1295                           },
1296                           { .ival = "clock_gettime",
1297                             .oval = CS_CGETTIME,
1298                             .help = "Use clock_gettime(2) for timing",
1299                           },
1300 #ifdef ARCH_HAVE_CPU_CLOCK
1301                           { .ival = "cpu",
1302                             .oval = CS_CPUCLOCK,
1303                             .help = "Use CPU private clock",
1304                           },
1305 #endif
1306                 },
1307         },
1308         {
1309                 .name   = "mem",
1310                 .alias  = "iomem",
1311                 .type   = FIO_OPT_STR,
1312                 .cb     = str_mem_cb,
1313                 .off1   = td_var_offset(mem_type),
1314                 .help   = "Backing type for IO buffers",
1315                 .def    = "malloc",
1316                 .posval = {
1317                           { .ival = "malloc",
1318                             .oval = MEM_MALLOC,
1319                             .help = "Use malloc(3) for IO buffers",
1320                           },
1321                           { .ival = "shm",
1322                             .oval = MEM_SHM,
1323                             .help = "Use shared memory segments for IO buffers",
1324                           },
1325 #ifdef FIO_HAVE_HUGETLB
1326                           { .ival = "shmhuge",
1327                             .oval = MEM_SHMHUGE,
1328                             .help = "Like shm, but use huge pages",
1329                           },
1330 #endif
1331                           { .ival = "mmap",
1332                             .oval = MEM_MMAP,
1333                             .help = "Use mmap(2) (file or anon) for IO buffers",
1334                           },
1335 #ifdef FIO_HAVE_HUGETLB
1336                           { .ival = "mmaphuge",
1337                             .oval = MEM_MMAPHUGE,
1338                             .help = "Like mmap, but use huge pages",
1339                           },
1340 #endif
1341                   },
1342         },
1343         {
1344                 .name   = "iomem_align",
1345                 .alias  = "mem_align",
1346                 .type   = FIO_OPT_INT,
1347                 .off1   = td_var_offset(mem_align),
1348                 .minval = 0,
1349                 .help   = "IO memory buffer offset alignment",
1350                 .def    = "0",
1351                 .parent = "iomem",
1352         },
1353         {
1354                 .name   = "verify",
1355                 .type   = FIO_OPT_STR,
1356                 .off1   = td_var_offset(verify),
1357                 .help   = "Verify data written",
1358                 .cb     = str_verify_cb,
1359                 .def    = "0",
1360                 .posval = {
1361                           { .ival = "0",
1362                             .oval = VERIFY_NONE,
1363                             .help = "Don't do IO verification",
1364                           },
1365                           { .ival = "md5",
1366                             .oval = VERIFY_MD5,
1367                             .help = "Use md5 checksums for verification",
1368                           },
1369                           { .ival = "crc64",
1370                             .oval = VERIFY_CRC64,
1371                             .help = "Use crc64 checksums for verification",
1372                           },
1373                           { .ival = "crc32",
1374                             .oval = VERIFY_CRC32,
1375                             .help = "Use crc32 checksums for verification",
1376                           },
1377                           { .ival = "crc32c-intel",
1378                             .oval = VERIFY_CRC32C_INTEL,
1379                             .help = "Use hw crc32c checksums for verification",
1380                           },
1381                           { .ival = "crc32c",
1382                             .oval = VERIFY_CRC32C,
1383                             .help = "Use crc32c checksums for verification",
1384                           },
1385                           { .ival = "crc16",
1386                             .oval = VERIFY_CRC16,
1387                             .help = "Use crc16 checksums for verification",
1388                           },
1389                           { .ival = "crc7",
1390                             .oval = VERIFY_CRC7,
1391                             .help = "Use crc7 checksums for verification",
1392                           },
1393                           { .ival = "sha1",
1394                             .oval = VERIFY_SHA1,
1395                             .help = "Use sha1 checksums for verification",
1396                           },
1397                           { .ival = "sha256",
1398                             .oval = VERIFY_SHA256,
1399                             .help = "Use sha256 checksums for verification",
1400                           },
1401                           { .ival = "sha512",
1402                             .oval = VERIFY_SHA512,
1403                             .help = "Use sha512 checksums for verification",
1404                           },
1405                           { .ival = "meta",
1406                             .oval = VERIFY_META,
1407                             .help = "Use io information",
1408                           },
1409                           {
1410                             .ival = "null",
1411                             .oval = VERIFY_NULL,
1412                             .help = "Pretend to verify",
1413                           },
1414                 },
1415         },
1416         {
1417                 .name   = "do_verify",
1418                 .type   = FIO_OPT_BOOL,
1419                 .off1   = td_var_offset(do_verify),
1420                 .help   = "Run verification stage after write",
1421                 .def    = "1",
1422                 .parent = "verify",
1423         },
1424         {
1425                 .name   = "verifysort",
1426                 .type   = FIO_OPT_BOOL,
1427                 .off1   = td_var_offset(verifysort),
1428                 .help   = "Sort written verify blocks for read back",
1429                 .def    = "1",
1430                 .parent = "verify",
1431         },
1432         {
1433                 .name   = "verify_interval",
1434                 .type   = FIO_OPT_INT,
1435                 .off1   = td_var_offset(verify_interval),
1436                 .minval = 2 * sizeof(struct verify_header),
1437                 .help   = "Store verify buffer header every N bytes",
1438                 .parent = "verify",
1439         },
1440         {
1441                 .name   = "verify_offset",
1442                 .type   = FIO_OPT_INT,
1443                 .help   = "Offset verify header location by N bytes",
1444                 .def    = "0",
1445                 .cb     = str_verify_offset_cb,
1446                 .parent = "verify",
1447         },
1448         {
1449                 .name   = "verify_pattern",
1450                 .type   = FIO_OPT_STR,
1451                 .cb     = str_verify_pattern_cb,
1452                 .help   = "Fill pattern for IO buffers",
1453                 .parent = "verify",
1454         },
1455         {
1456                 .name   = "verify_fatal",
1457                 .type   = FIO_OPT_BOOL,
1458                 .off1   = td_var_offset(verify_fatal),
1459                 .def    = "0",
1460                 .help   = "Exit on a single verify failure, don't continue",
1461                 .parent = "verify",
1462         },
1463         {
1464                 .name   = "verify_async",
1465                 .type   = FIO_OPT_INT,
1466                 .off1   = td_var_offset(verify_async),
1467                 .def    = "0",
1468                 .help   = "Number of async verifier threads to use",
1469                 .parent = "verify",
1470         },
1471         {
1472                 .name   = "verify_backlog",
1473                 .type   = FIO_OPT_STR_VAL,
1474                 .off1   = td_var_offset(verify_backlog),
1475                 .help   = "Verify after this number of blocks are written",
1476                 .parent = "verify",
1477         },
1478         {
1479                 .name   = "verify_backlog_batch",
1480                 .type   = FIO_OPT_INT,
1481                 .off1   = td_var_offset(verify_batch),
1482                 .help   = "Verify this number of IO blocks",
1483                 .parent = "verify",
1484         },
1485 #ifdef FIO_HAVE_CPU_AFFINITY
1486         {
1487                 .name   = "verify_async_cpus",
1488                 .type   = FIO_OPT_STR,
1489                 .cb     = str_verify_cpus_allowed_cb,
1490                 .help   = "Set CPUs allowed for async verify threads",
1491                 .parent = "verify_async",
1492         },
1493 #endif
1494 #ifdef FIO_HAVE_TRIM
1495         {
1496                 .name   = "trim_percentage",
1497                 .type   = FIO_OPT_INT,
1498                 .cb     = str_verify_trim_cb,
1499                 .maxval = 100,
1500                 .help   = "Number of verify blocks to discard/trim",
1501                 .parent = "verify",
1502                 .def    = "0",
1503         },
1504         {
1505                 .name   = "trim_verify_zero",
1506                 .type   = FIO_OPT_INT,
1507                 .help   = "Verify that trim/discarded blocks are returned as zeroes",
1508                 .off1   = td_var_offset(trim_zero),
1509                 .parent = "trim_percentage",
1510                 .def    = "1",
1511         },
1512         {
1513                 .name   = "trim_backlog",
1514                 .type   = FIO_OPT_STR_VAL,
1515                 .off1   = td_var_offset(trim_backlog),
1516                 .help   = "Trim after this number of blocks are written",
1517                 .parent = "trim_percentage",
1518         },
1519         {
1520                 .name   = "trim_backlog_batch",
1521                 .type   = FIO_OPT_INT,
1522                 .off1   = td_var_offset(trim_batch),
1523                 .help   = "Trim this number of IO blocks",
1524                 .parent = "trim_percentage",
1525         },
1526 #endif
1527         {
1528                 .name   = "write_iolog",
1529                 .type   = FIO_OPT_STR_STORE,
1530                 .off1   = td_var_offset(write_iolog_file),
1531                 .help   = "Store IO pattern to file",
1532         },
1533         {
1534                 .name   = "read_iolog",
1535                 .type   = FIO_OPT_STR_STORE,
1536                 .off1   = td_var_offset(read_iolog_file),
1537                 .help   = "Playback IO pattern from file",
1538         },
1539         {
1540                 .name   = "replay_no_stall",
1541                 .type   = FIO_OPT_INT,
1542                 .off1   = td_var_offset(no_stall),
1543                 .def    = "0",
1544                 .parent = "read_iolog",
1545                 .help   = "Playback IO pattern file as fast as possible without stalls",
1546         },
1547         {
1548                 .name   = "replay_redirect",
1549                 .type   = FIO_OPT_STR_STORE,
1550                 .off1   = td_var_offset(replay_redirect),
1551                 .parent = "read_iolog",
1552                 .help   = "Replay all I/O onto this device, regardless of trace device",
1553         },
1554         {
1555                 .name   = "exec_prerun",
1556                 .type   = FIO_OPT_STR_STORE,
1557                 .off1   = td_var_offset(exec_prerun),
1558                 .help   = "Execute this file prior to running job",
1559         },
1560         {
1561                 .name   = "exec_postrun",
1562                 .type   = FIO_OPT_STR_STORE,
1563                 .off1   = td_var_offset(exec_postrun),
1564                 .help   = "Execute this file after running job",
1565         },
1566 #ifdef FIO_HAVE_IOSCHED_SWITCH
1567         {
1568                 .name   = "ioscheduler",
1569                 .type   = FIO_OPT_STR_STORE,
1570                 .off1   = td_var_offset(ioscheduler),
1571                 .help   = "Use this IO scheduler on the backing device",
1572         },
1573 #endif
1574         {
1575                 .name   = "zonesize",
1576                 .type   = FIO_OPT_STR_VAL,
1577                 .off1   = td_var_offset(zone_size),
1578                 .help   = "Give size of an IO zone",
1579                 .def    = "0",
1580         },
1581         {
1582                 .name   = "zoneskip",
1583                 .type   = FIO_OPT_STR_VAL,
1584                 .off1   = td_var_offset(zone_skip),
1585                 .help   = "Space between IO zones",
1586                 .def    = "0",
1587         },
1588         {
1589                 .name   = "lockmem",
1590                 .type   = FIO_OPT_STR_VAL,
1591                 .cb     = str_lockmem_cb,
1592                 .help   = "Lock down this amount of memory",
1593                 .def    = "0",
1594         },
1595         {
1596                 .name   = "rwmixread",
1597                 .type   = FIO_OPT_INT,
1598                 .cb     = str_rwmix_read_cb,
1599                 .maxval = 100,
1600                 .help   = "Percentage of mixed workload that is reads",
1601                 .def    = "50",
1602         },
1603         {
1604                 .name   = "rwmixwrite",
1605                 .type   = FIO_OPT_INT,
1606                 .cb     = str_rwmix_write_cb,
1607                 .maxval = 100,
1608                 .help   = "Percentage of mixed workload that is writes",
1609                 .def    = "50",
1610         },
1611         {
1612                 .name   = "rwmixcycle",
1613                 .type   = FIO_OPT_DEPRECATED,
1614         },
1615         {
1616                 .name   = "nice",
1617                 .type   = FIO_OPT_INT,
1618                 .off1   = td_var_offset(nice),
1619                 .help   = "Set job CPU nice value",
1620                 .minval = -19,
1621                 .maxval = 20,
1622                 .def    = "0",
1623         },
1624 #ifdef FIO_HAVE_IOPRIO
1625         {
1626                 .name   = "prio",
1627                 .type   = FIO_OPT_INT,
1628                 .cb     = str_prio_cb,
1629                 .help   = "Set job IO priority value",
1630                 .minval = 0,
1631                 .maxval = 7,
1632         },
1633         {
1634                 .name   = "prioclass",
1635                 .type   = FIO_OPT_INT,
1636                 .cb     = str_prioclass_cb,
1637                 .help   = "Set job IO priority class",
1638                 .minval = 0,
1639                 .maxval = 3,
1640         },
1641 #endif
1642         {
1643                 .name   = "thinktime",
1644                 .type   = FIO_OPT_INT,
1645                 .off1   = td_var_offset(thinktime),
1646                 .help   = "Idle time between IO buffers (usec)",
1647                 .def    = "0",
1648         },
1649         {
1650                 .name   = "thinktime_spin",
1651                 .type   = FIO_OPT_INT,
1652                 .off1   = td_var_offset(thinktime_spin),
1653                 .help   = "Start think time by spinning this amount (usec)",
1654                 .def    = "0",
1655                 .parent = "thinktime",
1656         },
1657         {
1658                 .name   = "thinktime_blocks",
1659                 .type   = FIO_OPT_INT,
1660                 .off1   = td_var_offset(thinktime_blocks),
1661                 .help   = "IO buffer period between 'thinktime'",
1662                 .def    = "1",
1663                 .parent = "thinktime",
1664         },
1665         {
1666                 .name   = "rate",
1667                 .type   = FIO_OPT_INT,
1668                 .off1   = td_var_offset(rate[0]),
1669                 .off2   = td_var_offset(rate[1]),
1670                 .help   = "Set bandwidth rate",
1671         },
1672         {
1673                 .name   = "ratemin",
1674                 .type   = FIO_OPT_INT,
1675                 .off1   = td_var_offset(ratemin[0]),
1676                 .off2   = td_var_offset(ratemin[1]),
1677                 .help   = "Job must meet this rate or it will be shutdown",
1678                 .parent = "rate",
1679         },
1680         {
1681                 .name   = "rate_iops",
1682                 .type   = FIO_OPT_INT,
1683                 .off1   = td_var_offset(rate_iops[0]),
1684                 .off2   = td_var_offset(rate_iops[1]),
1685                 .help   = "Limit IO used to this number of IO operations/sec",
1686         },
1687         {
1688                 .name   = "rate_iops_min",
1689                 .type   = FIO_OPT_INT,
1690                 .off1   = td_var_offset(rate_iops_min[0]),
1691                 .off2   = td_var_offset(rate_iops_min[1]),
1692                 .help   = "Job must meet this rate or it will be shutdown",
1693                 .parent = "rate_iops",
1694         },
1695         {
1696                 .name   = "ratecycle",
1697                 .type   = FIO_OPT_INT,
1698                 .off1   = td_var_offset(ratecycle),
1699                 .help   = "Window average for rate limits (msec)",
1700                 .def    = "1000",
1701                 .parent = "rate",
1702         },
1703         {
1704                 .name   = "invalidate",
1705                 .type   = FIO_OPT_BOOL,
1706                 .off1   = td_var_offset(invalidate_cache),
1707                 .help   = "Invalidate buffer/page cache prior to running job",
1708                 .def    = "1",
1709         },
1710         {
1711                 .name   = "sync",
1712                 .type   = FIO_OPT_BOOL,
1713                 .off1   = td_var_offset(sync_io),
1714                 .help   = "Use O_SYNC for buffered writes",
1715                 .def    = "0",
1716                 .parent = "buffered",
1717         },
1718         {
1719                 .name   = "bwavgtime",
1720                 .type   = FIO_OPT_INT,
1721                 .off1   = td_var_offset(bw_avg_time),
1722                 .help   = "Time window over which to calculate bandwidth"
1723                           " (msec)",
1724                 .def    = "500",
1725         },
1726         {
1727                 .name   = "create_serialize",
1728                 .type   = FIO_OPT_BOOL,
1729                 .off1   = td_var_offset(create_serialize),
1730                 .help   = "Serialize creating of job files",
1731                 .def    = "1",
1732         },
1733         {
1734                 .name   = "create_fsync",
1735                 .type   = FIO_OPT_BOOL,
1736                 .off1   = td_var_offset(create_fsync),
1737                 .help   = "Fsync file after creation",
1738                 .def    = "1",
1739         },
1740         {
1741                 .name   = "create_on_open",
1742                 .type   = FIO_OPT_BOOL,
1743                 .off1   = td_var_offset(create_on_open),
1744                 .help   = "Create files when they are opened for IO",
1745                 .def    = "0",
1746         },
1747         {
1748                 .name   = "pre_read",
1749                 .type   = FIO_OPT_BOOL,
1750                 .off1   = td_var_offset(pre_read),
1751                 .help   = "Preread files before starting official testing",
1752                 .def    = "0",
1753         },
1754         {
1755                 .name   = "cpuload",
1756                 .type   = FIO_OPT_INT,
1757                 .off1   = td_var_offset(cpuload),
1758                 .help   = "Use this percentage of CPU",
1759         },
1760         {
1761                 .name   = "cpuchunks",
1762                 .type   = FIO_OPT_INT,
1763                 .off1   = td_var_offset(cpucycle),
1764                 .help   = "Length of the CPU burn cycles (usecs)",
1765                 .def    = "50000",
1766                 .parent = "cpuload",
1767         },
1768 #ifdef FIO_HAVE_CPU_AFFINITY
1769         {
1770                 .name   = "cpumask",
1771                 .type   = FIO_OPT_INT,
1772                 .cb     = str_cpumask_cb,
1773                 .help   = "CPU affinity mask",
1774         },
1775         {
1776                 .name   = "cpus_allowed",
1777                 .type   = FIO_OPT_STR,
1778                 .cb     = str_cpus_allowed_cb,
1779                 .help   = "Set CPUs allowed",
1780         },
1781 #endif
1782         {
1783                 .name   = "end_fsync",
1784                 .type   = FIO_OPT_BOOL,
1785                 .off1   = td_var_offset(end_fsync),
1786                 .help   = "Include fsync at the end of job",
1787                 .def    = "0",
1788         },
1789         {
1790                 .name   = "fsync_on_close",
1791                 .type   = FIO_OPT_BOOL,
1792                 .off1   = td_var_offset(fsync_on_close),
1793                 .help   = "fsync files on close",
1794                 .def    = "0",
1795         },
1796         {
1797                 .name   = "unlink",
1798                 .type   = FIO_OPT_BOOL,
1799                 .off1   = td_var_offset(unlink),
1800                 .help   = "Unlink created files after job has completed",
1801                 .def    = "0",
1802         },
1803         {
1804                 .name   = "exitall",
1805                 .type   = FIO_OPT_STR_SET,
1806                 .cb     = str_exitall_cb,
1807                 .help   = "Terminate all jobs when one exits",
1808         },
1809         {
1810                 .name   = "stonewall",
1811                 .type   = FIO_OPT_STR_SET,
1812                 .off1   = td_var_offset(stonewall),
1813                 .help   = "Insert a hard barrier between this job and previous",
1814         },
1815         {
1816                 .name   = "new_group",
1817                 .type   = FIO_OPT_STR_SET,
1818                 .off1   = td_var_offset(new_group),
1819                 .help   = "Mark the start of a new group (for reporting)",
1820         },
1821         {
1822                 .name   = "thread",
1823                 .type   = FIO_OPT_STR_SET,
1824                 .off1   = td_var_offset(use_thread),
1825                 .help   = "Use threads instead of forks",
1826         },
1827         {
1828                 .name   = "write_bw_log",
1829                 .type   = FIO_OPT_STR,
1830                 .off1   = td_var_offset(write_bw_log),
1831                 .cb     = str_write_bw_log_cb,
1832                 .help   = "Write log of bandwidth during run",
1833         },
1834         {
1835                 .name   = "write_lat_log",
1836                 .type   = FIO_OPT_STR,
1837                 .off1   = td_var_offset(write_lat_log),
1838                 .cb     = str_write_lat_log_cb,
1839                 .help   = "Write log of latency during run",
1840         },
1841         {
1842                 .name   = "hugepage-size",
1843                 .type   = FIO_OPT_INT,
1844                 .off1   = td_var_offset(hugepage_size),
1845                 .help   = "When using hugepages, specify size of each page",
1846                 .def    = __stringify(FIO_HUGE_PAGE),
1847         },
1848         {
1849                 .name   = "group_reporting",
1850                 .type   = FIO_OPT_STR_SET,
1851                 .off1   = td_var_offset(group_reporting),
1852                 .help   = "Do reporting on a per-group basis",
1853         },
1854         {
1855                 .name   = "zero_buffers",
1856                 .type   = FIO_OPT_STR_SET,
1857                 .off1   = td_var_offset(zero_buffers),
1858                 .help   = "Init IO buffers to all zeroes",
1859         },
1860         {
1861                 .name   = "refill_buffers",
1862                 .type   = FIO_OPT_STR_SET,
1863                 .off1   = td_var_offset(refill_buffers),
1864                 .help   = "Refill IO buffers on every IO submit",
1865         },
1866 #ifdef FIO_HAVE_DISK_UTIL
1867         {
1868                 .name   = "disk_util",
1869                 .type   = FIO_OPT_BOOL,
1870                 .off1   = td_var_offset(do_disk_util),
1871                 .help   = "Log disk utilization statistics",
1872                 .def    = "1",
1873         },
1874 #endif
1875         {
1876                 .name   = "gtod_reduce",
1877                 .type   = FIO_OPT_BOOL,
1878                 .help   = "Greatly reduce number of gettimeofday() calls",
1879                 .cb     = str_gtod_reduce_cb,
1880                 .def    = "0",
1881         },
1882         {
1883                 .name   = "disable_lat",
1884                 .type   = FIO_OPT_BOOL,
1885                 .off1   = td_var_offset(disable_lat),
1886                 .help   = "Disable latency numbers",
1887                 .parent = "gtod_reduce",
1888                 .def    = "0",
1889         },
1890         {
1891                 .name   = "disable_clat",
1892                 .type   = FIO_OPT_BOOL,
1893                 .off1   = td_var_offset(disable_clat),
1894                 .help   = "Disable completion latency numbers",
1895                 .parent = "gtod_reduce",
1896                 .def    = "0",
1897         },
1898         {
1899                 .name   = "disable_slat",
1900                 .type   = FIO_OPT_BOOL,
1901                 .off1   = td_var_offset(disable_slat),
1902                 .help   = "Disable submissionn latency numbers",
1903                 .parent = "gtod_reduce",
1904                 .def    = "0",
1905         },
1906         {
1907                 .name   = "disable_bw_measurement",
1908                 .type   = FIO_OPT_BOOL,
1909                 .off1   = td_var_offset(disable_bw),
1910                 .help   = "Disable bandwidth logging",
1911                 .parent = "gtod_reduce",
1912                 .def    = "0",
1913         },
1914         {
1915                 .name   = "gtod_cpu",
1916                 .type   = FIO_OPT_INT,
1917                 .cb     = str_gtod_cpu_cb,
1918                 .help   = "Setup dedicated gettimeofday() thread on this CPU",
1919                 .verify = gtod_cpu_verify,
1920         },
1921         {
1922                 .name   = "continue_on_error",
1923                 .type   = FIO_OPT_BOOL,
1924                 .off1   = td_var_offset(continue_on_error),
1925                 .help   = "Continue on non-fatal errors during I/O",
1926                 .def    = "0",
1927         },
1928         {
1929                 .name   = "profile",
1930                 .type   = FIO_OPT_STR_STORE,
1931                 .off1   = td_var_offset(profile),
1932                 .help   = "Select a specific builtin performance test",
1933         },
1934         {
1935                 .name   = "cgroup",
1936                 .type   = FIO_OPT_STR_STORE,
1937                 .off1   = td_var_offset(cgroup),
1938                 .help   = "Add job to cgroup of this name",
1939         },
1940         {
1941                 .name   = "cgroup_weight",
1942                 .type   = FIO_OPT_INT,
1943                 .off1   = td_var_offset(cgroup_weight),
1944                 .help   = "Use given weight for cgroup",
1945                 .minval = 100,
1946                 .maxval = 1000,
1947         },
1948         {
1949                 .name   = "cgroup_nodelete",
1950                 .type   = FIO_OPT_BOOL,
1951                 .off1   = td_var_offset(cgroup_nodelete),
1952                 .help   = "Do not delete cgroups after job completion",
1953                 .def    = "0",
1954         },
1955         {
1956                 .name   = "uid",
1957                 .type   = FIO_OPT_INT,
1958                 .off1   = td_var_offset(uid),
1959                 .help   = "Run job with this user ID",
1960         },
1961         {
1962                 .name   = "gid",
1963                 .type   = FIO_OPT_INT,
1964                 .off1   = td_var_offset(gid),
1965                 .help   = "Run job with this group ID",
1966         },
1967         {
1968                 .name = NULL,
1969         },
1970 };
1971
1972 static void add_to_lopt(struct option *lopt, struct fio_option *o,
1973                         const char *name)
1974 {
1975         lopt->name = (char *) name;
1976         lopt->val = FIO_GETOPT_JOB;
1977         if (o->type == FIO_OPT_STR_SET)
1978                 lopt->has_arg = no_argument;
1979         else
1980                 lopt->has_arg = required_argument;
1981 }
1982
1983 void fio_options_dup_and_init(struct option *long_options)
1984 {
1985         struct fio_option *o;
1986         unsigned int i;
1987
1988         options_init(options);
1989
1990         i = 0;
1991         while (long_options[i].name)
1992                 i++;
1993
1994         o = &options[0];
1995         while (o->name) {
1996                 add_to_lopt(&long_options[i], o, o->name);
1997                 if (o->alias) {
1998                         i++;
1999                         add_to_lopt(&long_options[i], o, o->alias);
2000                 }
2001
2002                 i++;
2003                 o++;
2004                 assert(i < FIO_NR_OPTIONS);
2005         }
2006 }
2007
2008 struct fio_keyword {
2009         const char *word;
2010         const char *desc;
2011         char *replace;
2012 };
2013
2014 static struct fio_keyword fio_keywords[] = {
2015         {
2016                 .word   = "$pagesize",
2017                 .desc   = "Page size in the system",
2018         },
2019         {
2020                 .word   = "$mb_memory",
2021                 .desc   = "Megabytes of memory online",
2022         },
2023         {
2024                 .word   = "$ncpus",
2025                 .desc   = "Number of CPUs online in the system",
2026         },
2027         {
2028                 .word   = NULL,
2029         },
2030 };
2031
2032 void fio_keywords_init(void)
2033 {
2034         unsigned long long mb_memory;
2035         char buf[128];
2036         long l;
2037
2038         sprintf(buf, "%lu", page_size);
2039         fio_keywords[0].replace = strdup(buf);
2040
2041         mb_memory = os_phys_mem() / page_size;
2042         sprintf(buf, "%llu", mb_memory);
2043         fio_keywords[1].replace = strdup(buf);
2044
2045         l = sysconf(_SC_NPROCESSORS_ONLN);
2046         sprintf(buf, "%lu", l);
2047         fio_keywords[2].replace = strdup(buf);
2048 }
2049
2050 #define BC_APP          "bc"
2051
2052 static char *bc_calc(char *str)
2053 {
2054         char *buf, *tmp, opt[80];
2055         FILE *f;
2056         int ret;
2057
2058         /*
2059          * No math, just return string
2060          */
2061         if (!strchr(str, '+') && !strchr(str, '-') && !strchr(str, '*') &&
2062             !strchr(str, '/'))
2063                 return str;
2064
2065         /*
2066          * Split option from value, we only need to calculate the value
2067          */
2068         tmp = strchr(str, '=');
2069         if (!tmp)
2070                 return str;
2071
2072         tmp++;
2073         memset(opt, 0, sizeof(opt));
2074         strncpy(opt, str, tmp - str);
2075
2076         buf = malloc(128);
2077
2078         sprintf(buf, "which %s > /dev/null", BC_APP);
2079         if (system(buf)) {
2080                 log_err("fio: bc is needed for performing math\n");
2081                 free(buf);
2082                 return NULL;
2083         }
2084
2085         sprintf(buf, "echo %s | %s", tmp, BC_APP);
2086         f = popen(buf, "r");
2087         if (!f) {
2088                 free(buf);
2089                 return NULL;
2090         }
2091
2092         ret = fread(buf, 1, 128, f);
2093         if (ret <= 0) {
2094                 free(buf);
2095                 return NULL;
2096         }
2097
2098         buf[ret - 1] = '\0';
2099         strcat(opt, buf);
2100         strcpy(buf, opt);
2101         pclose(f);
2102         free(str);
2103         return buf;
2104 }
2105
2106 /*
2107  * Look for reserved variable names and replace them with real values
2108  */
2109 static char *fio_keyword_replace(char *opt)
2110 {
2111         char *s;
2112         int i;
2113
2114         for (i = 0; fio_keywords[i].word != NULL; i++) {
2115                 struct fio_keyword *kw = &fio_keywords[i];
2116
2117                 while ((s = strstr(opt, kw->word)) != NULL) {
2118                         char *new = malloc(strlen(opt) + 1);
2119                         char *o_org = opt;
2120                         int olen = s - opt;
2121                         int len;
2122
2123                         /*
2124                          * Copy part of the string before the keyword and
2125                          * sprintf() the replacement after it.
2126                          */
2127                         memcpy(new, opt, olen);
2128                         len = sprintf(new + olen, "%s", kw->replace);
2129
2130                         /*
2131                          * If there's more in the original string, copy that
2132                          * in too
2133                          */
2134                         opt += strlen(kw->word) + olen;
2135                         if (strlen(opt))
2136                                 memcpy(new + olen + len, opt, opt - o_org - 1);
2137
2138                         /*
2139                          * replace opt and free the old opt
2140                          */
2141                         opt = new;
2142                         //free(o_org);
2143
2144                         /*
2145                          * Check for potential math and invoke bc, if possible
2146                          */
2147                         opt = bc_calc(opt);
2148                 }
2149         }
2150
2151         return opt;
2152 }
2153
2154 int fio_options_parse(struct thread_data *td, char **opts, int num_opts)
2155 {
2156         int i, ret;
2157
2158         sort_options(opts, options, num_opts);
2159
2160         for (ret = 0, i = 0; i < num_opts; i++) {
2161                 opts[i] = fio_keyword_replace(opts[i]);
2162                 ret |= parse_option(opts[i], options, td);
2163         }
2164
2165         return ret;
2166 }
2167
2168 int fio_cmd_option_parse(struct thread_data *td, const char *opt, char *val)
2169 {
2170         return parse_cmd_option(opt, val, options, td);
2171 }
2172
2173 void fio_fill_default_options(struct thread_data *td)
2174 {
2175         fill_default_options(td, options);
2176 }
2177
2178 int fio_show_option_help(const char *opt)
2179 {
2180         return show_cmd_help(options, opt);
2181 }
2182
2183 static void __options_mem(struct thread_data *td, int alloc)
2184 {
2185         struct thread_options *o = &td->o;
2186         struct fio_option *opt;
2187         char **ptr;
2188         int i;
2189
2190         for (i = 0, opt = &options[0]; opt->name; i++, opt = &options[i]) {
2191                 if (opt->type != FIO_OPT_STR_STORE)
2192                         continue;
2193
2194                 ptr = (void *) o + opt->off1;
2195                 if (*ptr) {
2196                         if (alloc)
2197                                 *ptr = strdup(*ptr);
2198                         else {
2199                                 free(*ptr);
2200                                 *ptr = NULL;
2201                         }
2202                 }
2203         }
2204 }
2205
2206 /*
2207  * dupe FIO_OPT_STR_STORE options
2208  */
2209 void options_mem_dupe(struct thread_data *td)
2210 {
2211         __options_mem(td, 1);
2212 }
2213
2214 void options_mem_free(struct thread_data fio_unused *td)
2215 {
2216 #if 0
2217         __options_mem(td, 0);
2218 #endif
2219 }
2220
2221 unsigned int fio_get_kb_base(void *data)
2222 {
2223         struct thread_data *td = data;
2224         unsigned int kb_base = 0;
2225
2226         if (td)
2227                 kb_base = td->o.kb_base;
2228         if (!kb_base)
2229                 kb_base = 1024;
2230
2231         return kb_base;
2232 }
2233
2234 int add_option(struct fio_option *o)
2235 {
2236         struct fio_option *__o;
2237         int opt_index = 0;
2238
2239         __o = options;
2240         while (__o->name) {
2241                 opt_index++;
2242                 __o++;
2243         }
2244
2245         memcpy(&options[opt_index], o, sizeof(*o));
2246         return 0;
2247 }
2248
2249 void invalidate_profile_options(const char *prof_name)
2250 {
2251         struct fio_option *o;
2252
2253         o = options;
2254         while (o->name) {
2255                 if (o->prof_name && !strcmp(o->prof_name, prof_name)) {
2256                         o->type = FIO_OPT_INVALID;
2257                         o->prof_name = NULL;
2258                 }
2259                 o++;
2260         }
2261 }
2262
2263 void add_opt_posval(const char *optname, const char *ival, const char *help)
2264 {
2265         struct fio_option *o;
2266         unsigned int i;
2267
2268         o = find_option(options, optname);
2269         if (!o)
2270                 return;
2271
2272         for (i = 0; i < PARSE_MAX_VP; i++) {
2273                 if (o->posval[i].ival)
2274                         continue;
2275
2276                 o->posval[i].ival = ival;
2277                 o->posval[i].help = help;
2278                 break;
2279         }
2280 }
2281
2282 void del_opt_posval(const char *optname, const char *ival)
2283 {
2284         struct fio_option *o;
2285         unsigned int i;
2286
2287         o = find_option(options, optname);
2288         if (!o)
2289                 return;
2290
2291         for (i = 0; i < PARSE_MAX_VP; i++) {
2292                 if (!o->posval[i].ival)
2293                         continue;
2294                 if (strcmp(o->posval[i].ival, ival))
2295                         continue;
2296
2297                 o->posval[i].ival = NULL;
2298                 o->posval[i].help = NULL;
2299         }
2300 }