Note offset_increment parent option
[fio.git] / options.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <ctype.h>
5 #include <string.h>
6 #include <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         td->o.ddir_seq_add = 0;
207
208         if (!nr)
209                 return 0;
210
211         if (td_random(td))
212                 td->o.ddir_seq_nr = atoi(nr);
213         else {
214                 long long val;
215
216                 if (str_to_decimal(nr, &val, 1, td)) {
217                         log_err("fio: rw postfix parsing failed\n");
218                         free(nr);
219                         return 1;
220                 }
221
222                 td->o.ddir_seq_add = val;
223         }
224
225         free(nr);
226         return 0;
227 }
228
229 static int str_mem_cb(void *data, const char *mem)
230 {
231         struct thread_data *td = data;
232
233         if (td->o.mem_type == MEM_MMAPHUGE || td->o.mem_type == MEM_MMAP) {
234                 td->mmapfile = get_opt_postfix(mem);
235                 if (td->o.mem_type == MEM_MMAPHUGE && !td->mmapfile) {
236                         log_err("fio: mmaphuge:/path/to/file\n");
237                         return 1;
238                 }
239         }
240
241         return 0;
242 }
243
244 static int str_verify_cb(void *data, const char *mem)
245 {
246         struct thread_data *td = data;
247
248         if (td->o.verify == VERIFY_CRC32C_INTEL ||
249             td->o.verify == VERIFY_CRC32C) {
250                 crc32c_intel_probe();
251         }
252
253         return 0;
254 }
255
256 static int fio_clock_source_cb(void *data, const char *str)
257 {
258         struct thread_data *td = data;
259
260         fio_clock_source = td->o.clocksource;
261         fio_time_init();
262         return 0;
263 }
264
265 static int str_lockmem_cb(void fio_unused *data, unsigned long long *val)
266 {
267         mlock_size = *val;
268         return 0;
269 }
270
271 static int str_rwmix_read_cb(void *data, unsigned long long *val)
272 {
273         struct thread_data *td = data;
274
275         td->o.rwmix[DDIR_READ] = *val;
276         td->o.rwmix[DDIR_WRITE] = 100 - *val;
277         return 0;
278 }
279
280 static int str_rwmix_write_cb(void *data, unsigned long long *val)
281 {
282         struct thread_data *td = data;
283
284         td->o.rwmix[DDIR_WRITE] = *val;
285         td->o.rwmix[DDIR_READ] = 100 - *val;
286         return 0;
287 }
288
289 #ifdef FIO_HAVE_IOPRIO
290 static int str_prioclass_cb(void *data, unsigned long long *val)
291 {
292         struct thread_data *td = data;
293         unsigned short mask;
294
295         /*
296          * mask off old class bits, str_prio_cb() may have set a default class
297          */
298         mask = (1 << IOPRIO_CLASS_SHIFT) - 1;
299         td->ioprio &= mask;
300
301         td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
302         td->ioprio_set = 1;
303         return 0;
304 }
305
306 static int str_prio_cb(void *data, unsigned long long *val)
307 {
308         struct thread_data *td = data;
309
310         td->ioprio |= *val;
311
312         /*
313          * If no class is set, assume BE
314          */
315         if ((td->ioprio >> IOPRIO_CLASS_SHIFT) == 0)
316                 td->ioprio |= IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT;
317
318         td->ioprio_set = 1;
319         return 0;
320 }
321 #endif
322
323 static int str_exitall_cb(void)
324 {
325         exitall_on_terminate = 1;
326         return 0;
327 }
328
329 #ifdef FIO_HAVE_CPU_AFFINITY
330 static int str_cpumask_cb(void *data, unsigned long long *val)
331 {
332         struct thread_data *td = data;
333         unsigned int i;
334         long max_cpu;
335         int ret;
336
337         ret = fio_cpuset_init(&td->o.cpumask);
338         if (ret < 0) {
339                 log_err("fio: cpuset_init failed\n");
340                 td_verror(td, ret, "fio_cpuset_init");
341                 return 1;
342         }
343
344         max_cpu = cpus_online();
345
346         for (i = 0; i < sizeof(int) * 8; i++) {
347                 if ((1 << i) & *val) {
348                         if (i > max_cpu) {
349                                 log_err("fio: CPU %d too large (max=%ld)\n", i,
350                                                                 max_cpu);
351                                 return 1;
352                         }
353                         dprint(FD_PARSE, "set cpu allowed %d\n", i);
354                         fio_cpu_set(&td->o.cpumask, i);
355                 }
356         }
357
358         td->o.cpumask_set = 1;
359         return 0;
360 }
361
362 static int set_cpus_allowed(struct thread_data *td, os_cpu_mask_t *mask,
363                             const char *input)
364 {
365         char *cpu, *str, *p;
366         long max_cpu;
367         int ret = 0;
368
369         ret = fio_cpuset_init(mask);
370         if (ret < 0) {
371                 log_err("fio: cpuset_init failed\n");
372                 td_verror(td, ret, "fio_cpuset_init");
373                 return 1;
374         }
375
376         p = str = strdup(input);
377
378         strip_blank_front(&str);
379         strip_blank_end(str);
380
381         max_cpu = cpus_online();
382
383         while ((cpu = strsep(&str, ",")) != NULL) {
384                 char *str2, *cpu2;
385                 int icpu, icpu2;
386
387                 if (!strlen(cpu))
388                         break;
389
390                 str2 = cpu;
391                 icpu2 = -1;
392                 while ((cpu2 = strsep(&str2, "-")) != NULL) {
393                         if (!strlen(cpu2))
394                                 break;
395
396                         icpu2 = atoi(cpu2);
397                 }
398
399                 icpu = atoi(cpu);
400                 if (icpu2 == -1)
401                         icpu2 = icpu;
402                 while (icpu <= icpu2) {
403                         if (icpu >= FIO_MAX_CPUS) {
404                                 log_err("fio: your OS only supports up to"
405                                         " %d CPUs\n", (int) FIO_MAX_CPUS);
406                                 ret = 1;
407                                 break;
408                         }
409                         if (icpu > max_cpu) {
410                                 log_err("fio: CPU %d too large (max=%ld)\n",
411                                                         icpu, max_cpu);
412                                 ret = 1;
413                                 break;
414                         }
415
416                         dprint(FD_PARSE, "set cpu allowed %d\n", icpu);
417                         fio_cpu_set(mask, icpu);
418                         icpu++;
419                 }
420                 if (ret)
421                         break;
422         }
423
424         free(p);
425         if (!ret)
426                 td->o.cpumask_set = 1;
427         return ret;
428 }
429
430 static int str_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.cpumask, input);
436         if (!ret)
437                 td->o.cpumask_set = 1;
438
439         return ret;
440 }
441
442 static int str_verify_cpus_allowed_cb(void *data, const char *input)
443 {
444         struct thread_data *td = data;
445         int ret;
446
447         ret = set_cpus_allowed(td, &td->o.verify_cpumask, input);
448         if (!ret)
449                 td->o.verify_cpumask_set = 1;
450
451         return ret;
452 }
453 #endif
454
455 #ifdef FIO_HAVE_TRIM
456 static int str_verify_trim_cb(void *data, unsigned long long *val)
457 {
458         struct thread_data *td = data;
459
460         td->o.trim_percentage = *val;
461         return 0;
462 }
463 #endif
464
465 static int str_fst_cb(void *data, const char *str)
466 {
467         struct thread_data *td = data;
468         char *nr = get_opt_postfix(str);
469
470         td->file_service_nr = 1;
471         if (nr) {
472                 td->file_service_nr = atoi(nr);
473                 free(nr);
474         }
475
476         return 0;
477 }
478
479 #ifdef FIO_HAVE_SYNC_FILE_RANGE
480 static int str_sfr_cb(void *data, const char *str)
481 {
482         struct thread_data *td = data;
483         char *nr = get_opt_postfix(str);
484
485         td->sync_file_range_nr = 1;
486         if (nr) {
487                 td->sync_file_range_nr = atoi(nr);
488                 free(nr);
489         }
490
491         return 0;
492 }
493 #endif
494
495 static int check_dir(struct thread_data *td, char *fname)
496 {
497 #if 0
498         char file[PATH_MAX], *dir;
499         int elen = 0;
500
501         if (td->o.directory) {
502                 strcpy(file, td->o.directory);
503                 strcat(file, "/");
504                 elen = strlen(file);
505         }
506
507         sprintf(file + elen, "%s", fname);
508         dir = dirname(file);
509
510         {
511         struct stat sb;
512         /*
513          * We can't do this on FIO_DISKLESSIO engines. The engine isn't loaded
514          * yet, so we can't do this check right here...
515          */
516         if (lstat(dir, &sb) < 0) {
517                 int ret = errno;
518
519                 log_err("fio: %s is not a directory\n", dir);
520                 td_verror(td, ret, "lstat");
521                 return 1;
522         }
523
524         if (!S_ISDIR(sb.st_mode)) {
525                 log_err("fio: %s is not a directory\n", dir);
526                 return 1;
527         }
528         }
529 #endif
530
531         return 0;
532 }
533
534 /*
535  * Return next file in the string. Files are separated with ':'. If the ':'
536  * is escaped with a '\', then that ':' is part of the filename and does not
537  * indicate a new file.
538  */
539 static char *get_next_file_name(char **ptr)
540 {
541         char *str = *ptr;
542         char *p, *start;
543
544         if (!str || !strlen(str))
545                 return NULL;
546
547         start = str;
548         do {
549                 /*
550                  * No colon, we are done
551                  */
552                 p = strchr(str, ':');
553                 if (!p) {
554                         *ptr = NULL;
555                         break;
556                 }
557
558                 /*
559                  * We got a colon, but it's the first character. Skip and
560                  * continue
561                  */
562                 if (p == start) {
563                         str = ++start;
564                         continue;
565                 }
566
567                 if (*(p - 1) != '\\') {
568                         *p = '\0';
569                         *ptr = p + 1;
570                         break;
571                 }
572
573                 memmove(p - 1, p, strlen(p) + 1);
574                 str = p;
575         } while (1);
576
577         return start;
578 }
579
580 static int str_filename_cb(void *data, const char *input)
581 {
582         struct thread_data *td = data;
583         char *fname, *str, *p;
584
585         p = str = strdup(input);
586
587         strip_blank_front(&str);
588         strip_blank_end(str);
589
590         if (!td->files_index)
591                 td->o.nr_files = 0;
592
593         while ((fname = get_next_file_name(&str)) != NULL) {
594                 if (!strlen(fname))
595                         break;
596                 if (check_dir(td, fname)) {
597                         free(p);
598                         return 1;
599                 }
600                 add_file(td, fname);
601                 td->o.nr_files++;
602         }
603
604         free(p);
605         return 0;
606 }
607
608 static int str_directory_cb(void *data, const char fio_unused *str)
609 {
610         struct thread_data *td = data;
611         struct stat sb;
612
613         if (lstat(td->o.directory, &sb) < 0) {
614                 int ret = errno;
615
616                 log_err("fio: %s is not a directory\n", td->o.directory);
617                 td_verror(td, ret, "lstat");
618                 return 1;
619         }
620         if (!S_ISDIR(sb.st_mode)) {
621                 log_err("fio: %s is not a directory\n", td->o.directory);
622                 return 1;
623         }
624
625         return 0;
626 }
627
628 static int str_opendir_cb(void *data, const char fio_unused *str)
629 {
630         struct thread_data *td = data;
631
632         if (!td->files_index)
633                 td->o.nr_files = 0;
634
635         return add_dir_files(td, td->o.opendir);
636 }
637
638 static int str_verify_offset_cb(void *data, unsigned long long *off)
639 {
640         struct thread_data *td = data;
641
642         if (*off && *off < sizeof(struct verify_header)) {
643                 log_err("fio: verify_offset too small\n");
644                 return 1;
645         }
646
647         td->o.verify_offset = *off;
648         return 0;
649 }
650
651 static int str_verify_pattern_cb(void *data, const char *input)
652 {
653         struct thread_data *td = data;
654         long off;
655         int i = 0, j = 0, len, k, base = 10;
656         char* loc1, * loc2;
657
658         loc1 = strstr(input, "0x");
659         loc2 = strstr(input, "0X");
660         if (loc1 || loc2)
661                 base = 16;
662         off = strtol(input, NULL, base);
663         if (off != LONG_MAX || errno != ERANGE) {
664                 while (off) {
665                         td->o.verify_pattern[i] = off & 0xff;
666                         off >>= 8;
667                         i++;
668                 }
669         } else {
670                 len = strlen(input);
671                 k = len - 1;
672                 if (base == 16) {
673                         if (loc1)
674                                 j = loc1 - input + 2;
675                         else
676                                 j = loc2 - input + 2;
677                 } else
678                         return 1;
679                 if (len - j < MAX_PATTERN_SIZE * 2) {
680                         while (k >= j) {
681                                 off = converthexchartoint(input[k--]);
682                                 if (k >= j)
683                                         off += (converthexchartoint(input[k--])
684                                                 * 16);
685                                 td->o.verify_pattern[i++] = (char) off;
686                         }
687                 }
688         }
689
690         /*
691          * Fill the pattern all the way to the end. This greatly reduces
692          * the number of memcpy's we have to do when verifying the IO.
693          */
694         while (i > 1 && i * 2 <= MAX_PATTERN_SIZE) {
695                 memcpy(&td->o.verify_pattern[i], &td->o.verify_pattern[0], i);
696                 i *= 2;
697         }
698         if (i == 1) {
699                 /*
700                  * The code in verify_io_u_pattern assumes a single byte pattern
701                  * fills the whole verify pattern buffer.
702                  */
703                 memset(td->o.verify_pattern, td->o.verify_pattern[0],
704                        MAX_PATTERN_SIZE);
705         }
706
707         td->o.verify_pattern_bytes = i;
708
709         /*
710          * VERIFY_META could already be set
711          */
712         if (td->o.verify == VERIFY_NONE)
713                 td->o.verify = VERIFY_PATTERN;
714
715         return 0;
716 }
717
718 static int str_lockfile_cb(void *data, const char *str)
719 {
720         struct thread_data *td = data;
721         char *nr = get_opt_postfix(str);
722
723         td->o.lockfile_batch = 1;
724         if (nr) {
725                 td->o.lockfile_batch = atoi(nr);
726                 free(nr);
727         }
728
729         return 0;
730 }
731
732 static int str_write_bw_log_cb(void *data, const char *str)
733 {
734         struct thread_data *td = data;
735
736         if (str)
737                 td->o.bw_log_file = strdup(str);
738
739         td->o.write_bw_log = 1;
740         return 0;
741 }
742
743 static int str_write_lat_log_cb(void *data, const char *str)
744 {
745         struct thread_data *td = data;
746
747         if (str)
748                 td->o.lat_log_file = strdup(str);
749
750         td->o.write_lat_log = 1;
751         return 0;
752 }
753
754 static int str_write_iops_log_cb(void *data, const char *str)
755 {
756         struct thread_data *td = data;
757
758         if (str)
759                 td->o.iops_log_file = strdup(str);
760
761         td->o.write_iops_log = 1;
762         return 0;
763 }
764
765 static int str_gtod_reduce_cb(void *data, int *il)
766 {
767         struct thread_data *td = data;
768         int val = *il;
769
770         td->o.disable_lat = !!val;
771         td->o.disable_clat = !!val;
772         td->o.disable_slat = !!val;
773         td->o.disable_bw = !!val;
774         td->o.clat_percentiles = !val;
775         if (val)
776                 td->tv_cache_mask = 63;
777
778         return 0;
779 }
780
781 static int str_gtod_cpu_cb(void *data, long long *il)
782 {
783         struct thread_data *td = data;
784         int val = *il;
785
786         td->o.gtod_cpu = val;
787         td->o.gtod_offload = 1;
788         return 0;
789 }
790
791 static int str_size_cb(void *data, unsigned long long *__val)
792 {
793         struct thread_data *td = data;
794         unsigned long long v = *__val;
795
796         if (parse_is_percent(v)) {
797                 td->o.size = 0;
798                 td->o.size_percent = -1ULL - v;
799         } else
800                 td->o.size = v;
801
802         return 0;
803 }
804
805 static int rw_verify(struct fio_option *o, void *data)
806 {
807         struct thread_data *td = data;
808
809         if (read_only && td_write(td)) {
810                 log_err("fio: job <%s> has write bit set, but fio is in"
811                         " read-only mode\n", td->o.name);
812                 return 1;
813         }
814
815         return 0;
816 }
817
818 static int gtod_cpu_verify(struct fio_option *o, void *data)
819 {
820 #ifndef FIO_HAVE_CPU_AFFINITY
821         struct thread_data *td = data;
822
823         if (td->o.gtod_cpu) {
824                 log_err("fio: platform must support CPU affinity for"
825                         "gettimeofday() offloading\n");
826                 return 1;
827         }
828 #endif
829
830         return 0;
831 }
832
833 static int kb_base_verify(struct fio_option *o, void *data)
834 {
835         struct thread_data *td = data;
836
837         if (td->o.kb_base != 1024 && td->o.kb_base != 1000) {
838                 log_err("fio: kb_base set to nonsensical value: %u\n",
839                                 td->o.kb_base);
840                 return 1;
841         }
842
843         return 0;
844 }
845
846 /*
847  * Map of job/command line options
848  */
849 static struct fio_option options[FIO_MAX_OPTS] = {
850         {
851                 .name   = "description",
852                 .type   = FIO_OPT_STR_STORE,
853                 .off1   = td_var_offset(description),
854                 .help   = "Text job description",
855         },
856         {
857                 .name   = "name",
858                 .type   = FIO_OPT_STR_STORE,
859                 .off1   = td_var_offset(name),
860                 .help   = "Name of this job",
861         },
862         {
863                 .name   = "directory",
864                 .type   = FIO_OPT_STR_STORE,
865                 .off1   = td_var_offset(directory),
866                 .cb     = str_directory_cb,
867                 .help   = "Directory to store files in",
868         },
869         {
870                 .name   = "filename",
871                 .type   = FIO_OPT_STR_STORE,
872                 .off1   = td_var_offset(filename),
873                 .cb     = str_filename_cb,
874                 .prio   = -1, /* must come after "directory" */
875                 .help   = "File(s) to use for the workload",
876         },
877         {
878                 .name   = "kb_base",
879                 .type   = FIO_OPT_INT,
880                 .off1   = td_var_offset(kb_base),
881                 .verify = kb_base_verify,
882                 .prio   = 1,
883                 .def    = "1024",
884                 .help   = "How many bytes per KB for reporting (1000 or 1024)",
885         },
886         {
887                 .name   = "lockfile",
888                 .type   = FIO_OPT_STR,
889                 .cb     = str_lockfile_cb,
890                 .off1   = td_var_offset(file_lock_mode),
891                 .help   = "Lock file when doing IO to it",
892                 .parent = "filename",
893                 .def    = "none",
894                 .posval = {
895                           { .ival = "none",
896                             .oval = FILE_LOCK_NONE,
897                             .help = "No file locking",
898                           },
899                           { .ival = "exclusive",
900                             .oval = FILE_LOCK_EXCLUSIVE,
901                             .help = "Exclusive file lock",
902                           },
903                           {
904                             .ival = "readwrite",
905                             .oval = FILE_LOCK_READWRITE,
906                             .help = "Read vs write lock",
907                           },
908                 },
909         },
910         {
911                 .name   = "opendir",
912                 .type   = FIO_OPT_STR_STORE,
913                 .off1   = td_var_offset(opendir),
914                 .cb     = str_opendir_cb,
915                 .help   = "Recursively add files from this directory and down",
916         },
917         {
918                 .name   = "rw",
919                 .alias  = "readwrite",
920                 .type   = FIO_OPT_STR,
921                 .cb     = str_rw_cb,
922                 .off1   = td_var_offset(td_ddir),
923                 .help   = "IO direction",
924                 .def    = "read",
925                 .verify = rw_verify,
926                 .posval = {
927                           { .ival = "read",
928                             .oval = TD_DDIR_READ,
929                             .help = "Sequential read",
930                           },
931                           { .ival = "write",
932                             .oval = TD_DDIR_WRITE,
933                             .help = "Sequential write",
934                           },
935                           { .ival = "randread",
936                             .oval = TD_DDIR_RANDREAD,
937                             .help = "Random read",
938                           },
939                           { .ival = "randwrite",
940                             .oval = TD_DDIR_RANDWRITE,
941                             .help = "Random write",
942                           },
943                           { .ival = "rw",
944                             .oval = TD_DDIR_RW,
945                             .help = "Sequential read and write mix",
946                           },
947                           { .ival = "randrw",
948                             .oval = TD_DDIR_RANDRW,
949                             .help = "Random read and write mix"
950                           },
951                 },
952         },
953         {
954                 .name   = "rw_sequencer",
955                 .type   = FIO_OPT_STR,
956                 .off1   = td_var_offset(rw_seq),
957                 .help   = "IO offset generator modifier",
958                 .def    = "sequential",
959                 .posval = {
960                           { .ival = "sequential",
961                             .oval = RW_SEQ_SEQ,
962                             .help = "Generate sequential offsets",
963                           },
964                           { .ival = "identical",
965                             .oval = RW_SEQ_IDENT,
966                             .help = "Generate identical offsets",
967                           },
968                 },
969         },
970
971         {
972                 .name   = "ioengine",
973                 .type   = FIO_OPT_STR_STORE,
974                 .off1   = td_var_offset(ioengine),
975                 .help   = "IO engine to use",
976                 .def    = FIO_PREFERRED_ENGINE,
977                 .posval = {
978                           { .ival = "sync",
979                             .help = "Use read/write",
980                           },
981                           { .ival = "psync",
982                             .help = "Use pread/pwrite",
983                           },
984                           { .ival = "vsync",
985                             .help = "Use readv/writev",
986                           },
987 #ifdef FIO_HAVE_LIBAIO
988                           { .ival = "libaio",
989                             .help = "Linux native asynchronous IO",
990                           },
991 #endif
992 #ifdef FIO_HAVE_POSIXAIO
993                           { .ival = "posixaio",
994                             .help = "POSIX asynchronous IO",
995                           },
996 #endif
997 #ifdef FIO_HAVE_SOLARISAIO
998                           { .ival = "solarisaio",
999                             .help = "Solaris native asynchronous IO",
1000                           },
1001 #endif
1002 #ifdef FIO_HAVE_WINDOWSAIO
1003                           { .ival = "windowsaio",
1004                             .help = "Windows native asynchronous IO"
1005                           },
1006 #endif
1007                           { .ival = "mmap",
1008                             .help = "Memory mapped IO"
1009                           },
1010 #ifdef FIO_HAVE_SPLICE
1011                           { .ival = "splice",
1012                             .help = "splice/vmsplice based IO",
1013                           },
1014                           { .ival = "netsplice",
1015                             .help = "splice/vmsplice to/from the network",
1016                           },
1017 #endif
1018 #ifdef FIO_HAVE_SGIO
1019                           { .ival = "sg",
1020                             .help = "SCSI generic v3 IO",
1021                           },
1022 #endif
1023                           { .ival = "null",
1024                             .help = "Testing engine (no data transfer)",
1025                           },
1026                           { .ival = "net",
1027                             .help = "Network IO",
1028                           },
1029 #ifdef FIO_HAVE_SYSLET
1030                           { .ival = "syslet-rw",
1031                             .help = "syslet enabled async pread/pwrite IO",
1032                           },
1033 #endif
1034                           { .ival = "cpuio",
1035                             .help = "CPU cycle burner engine",
1036                           },
1037 #ifdef FIO_HAVE_GUASI
1038                           { .ival = "guasi",
1039                             .help = "GUASI IO engine",
1040                           },
1041 #endif
1042 #ifdef FIO_HAVE_BINJECT
1043                           { .ival = "binject",
1044                             .help = "binject direct inject block engine",
1045                           },
1046 #endif
1047 #ifdef FIO_HAVE_RDMA
1048                           { .ival = "rdma",
1049                             .help = "RDMA IO engine",
1050                           },
1051 #endif
1052                           { .ival = "external",
1053                             .help = "Load external engine (append name)",
1054                           },
1055                 },
1056         },
1057         {
1058                 .name   = "iodepth",
1059                 .type   = FIO_OPT_INT,
1060                 .off1   = td_var_offset(iodepth),
1061                 .help   = "Number of IO buffers to keep in flight",
1062                 .minval = 1,
1063                 .def    = "1",
1064         },
1065         {
1066                 .name   = "iodepth_batch",
1067                 .alias  = "iodepth_batch_submit",
1068                 .type   = FIO_OPT_INT,
1069                 .off1   = td_var_offset(iodepth_batch),
1070                 .help   = "Number of IO buffers to submit in one go",
1071                 .parent = "iodepth",
1072                 .minval = 1,
1073                 .def    = "1",
1074         },
1075         {
1076                 .name   = "iodepth_batch_complete",
1077                 .type   = FIO_OPT_INT,
1078                 .off1   = td_var_offset(iodepth_batch_complete),
1079                 .help   = "Number of IO buffers to retrieve in one go",
1080                 .parent = "iodepth",
1081                 .minval = 0,
1082                 .def    = "1",
1083         },
1084         {
1085                 .name   = "iodepth_low",
1086                 .type   = FIO_OPT_INT,
1087                 .off1   = td_var_offset(iodepth_low),
1088                 .help   = "Low water mark for queuing depth",
1089                 .parent = "iodepth",
1090         },
1091         {
1092                 .name   = "size",
1093                 .type   = FIO_OPT_STR_VAL,
1094                 .cb     = str_size_cb,
1095                 .help   = "Total size of device or files",
1096         },
1097         {
1098                 .name   = "fill_device",
1099                 .alias  = "fill_fs",
1100                 .type   = FIO_OPT_BOOL,
1101                 .off1   = td_var_offset(fill_device),
1102                 .help   = "Write until an ENOSPC error occurs",
1103                 .def    = "0",
1104         },
1105         {
1106                 .name   = "filesize",
1107                 .type   = FIO_OPT_STR_VAL,
1108                 .off1   = td_var_offset(file_size_low),
1109                 .off2   = td_var_offset(file_size_high),
1110                 .minval = 1,
1111                 .help   = "Size of individual files",
1112         },
1113         {
1114                 .name   = "offset",
1115                 .alias  = "fileoffset",
1116                 .type   = FIO_OPT_STR_VAL,
1117                 .off1   = td_var_offset(start_offset),
1118                 .help   = "Start IO from this offset",
1119                 .def    = "0",
1120         },
1121         {
1122                 .name   = "offset_increment",
1123                 .type   = FIO_OPT_STR_VAL,
1124                 .off1   = td_var_offset(offset_increment),
1125                 .help   = "What is the increment from one offset to the next",
1126                 .parent = "offset",
1127                 .def    = "0",
1128         },
1129         {
1130                 .name   = "bs",
1131                 .alias  = "blocksize",
1132                 .type   = FIO_OPT_INT,
1133                 .off1   = td_var_offset(bs[DDIR_READ]),
1134                 .off2   = td_var_offset(bs[DDIR_WRITE]),
1135                 .minval = 1,
1136                 .help   = "Block size unit",
1137                 .def    = "4k",
1138                 .parent = "rw",
1139         },
1140         {
1141                 .name   = "ba",
1142                 .alias  = "blockalign",
1143                 .type   = FIO_OPT_INT,
1144                 .off1   = td_var_offset(ba[DDIR_READ]),
1145                 .off2   = td_var_offset(ba[DDIR_WRITE]),
1146                 .minval = 1,
1147                 .help   = "IO block offset alignment",
1148                 .parent = "rw",
1149         },
1150         {
1151                 .name   = "bsrange",
1152                 .alias  = "blocksize_range",
1153                 .type   = FIO_OPT_RANGE,
1154                 .off1   = td_var_offset(min_bs[DDIR_READ]),
1155                 .off2   = td_var_offset(max_bs[DDIR_READ]),
1156                 .off3   = td_var_offset(min_bs[DDIR_WRITE]),
1157                 .off4   = td_var_offset(max_bs[DDIR_WRITE]),
1158                 .minval = 1,
1159                 .help   = "Set block size range (in more detail than bs)",
1160                 .parent = "rw",
1161         },
1162         {
1163                 .name   = "bssplit",
1164                 .type   = FIO_OPT_STR,
1165                 .cb     = str_bssplit_cb,
1166                 .help   = "Set a specific mix of block sizes",
1167                 .parent = "rw",
1168         },
1169         {
1170                 .name   = "bs_unaligned",
1171                 .alias  = "blocksize_unaligned",
1172                 .type   = FIO_OPT_STR_SET,
1173                 .off1   = td_var_offset(bs_unaligned),
1174                 .help   = "Don't sector align IO buffer sizes",
1175                 .parent = "rw",
1176         },
1177         {
1178                 .name   = "randrepeat",
1179                 .type   = FIO_OPT_BOOL,
1180                 .off1   = td_var_offset(rand_repeatable),
1181                 .help   = "Use repeatable random IO pattern",
1182                 .def    = "1",
1183                 .parent = "rw",
1184         },
1185         {
1186                 .name   = "use_os_rand",
1187                 .type   = FIO_OPT_BOOL,
1188                 .off1   = td_var_offset(use_os_rand),
1189                 .help   = "Set to use OS random generator",
1190                 .def    = "0",
1191                 .parent = "rw",
1192         },
1193         {
1194                 .name   = "norandommap",
1195                 .type   = FIO_OPT_STR_SET,
1196                 .off1   = td_var_offset(norandommap),
1197                 .help   = "Accept potential duplicate random blocks",
1198                 .parent = "rw",
1199         },
1200         {
1201                 .name   = "softrandommap",
1202                 .type   = FIO_OPT_BOOL,
1203                 .off1   = td_var_offset(softrandommap),
1204                 .help   = "Set norandommap if randommap allocation fails",
1205                 .parent = "norandommap",
1206                 .def    = "0",
1207         },
1208         {
1209                 .name   = "nrfiles",
1210                 .alias  = "nr_files",
1211                 .type   = FIO_OPT_INT,
1212                 .off1   = td_var_offset(nr_files),
1213                 .help   = "Split job workload between this number of files",
1214                 .def    = "1",
1215         },
1216         {
1217                 .name   = "openfiles",
1218                 .type   = FIO_OPT_INT,
1219                 .off1   = td_var_offset(open_files),
1220                 .help   = "Number of files to keep open at the same time",
1221         },
1222         {
1223                 .name   = "file_service_type",
1224                 .type   = FIO_OPT_STR,
1225                 .cb     = str_fst_cb,
1226                 .off1   = td_var_offset(file_service_type),
1227                 .help   = "How to select which file to service next",
1228                 .def    = "roundrobin",
1229                 .posval = {
1230                           { .ival = "random",
1231                             .oval = FIO_FSERVICE_RANDOM,
1232                             .help = "Choose a file at random",
1233                           },
1234                           { .ival = "roundrobin",
1235                             .oval = FIO_FSERVICE_RR,
1236                             .help = "Round robin select files",
1237                           },
1238                           { .ival = "sequential",
1239                             .oval = FIO_FSERVICE_SEQ,
1240                             .help = "Finish one file before moving to the next",
1241                           },
1242                 },
1243                 .parent = "nrfiles",
1244         },
1245 #ifdef FIO_HAVE_FALLOCATE
1246         {
1247                 .name   = "fallocate",
1248                 .type   = FIO_OPT_STR,
1249                 .off1   = td_var_offset(fallocate_mode),
1250                 .help   = "Whether pre-allocation is performed when laying out files",
1251                 .def    = "posix",
1252                 .posval = {
1253                           { .ival = "none",
1254                             .oval = FIO_FALLOCATE_NONE,
1255                             .help = "Do not pre-allocate space",
1256                           },
1257                           { .ival = "posix",
1258                             .oval = FIO_FALLOCATE_POSIX,
1259                             .help = "Use posix_fallocate()",
1260                           },
1261 #ifdef FIO_HAVE_LINUX_FALLOCATE
1262                           { .ival = "keep",
1263                             .oval = FIO_FALLOCATE_KEEP_SIZE,
1264                             .help = "Use fallocate(..., FALLOC_FL_KEEP_SIZE, ...)",
1265                           },
1266 #endif
1267                           /* Compatibility with former boolean values */
1268                           { .ival = "0",
1269                             .oval = FIO_FALLOCATE_NONE,
1270                             .help = "Alias for 'none'",
1271                           },
1272                           { .ival = "1",
1273                             .oval = FIO_FALLOCATE_POSIX,
1274                             .help = "Alias for 'posix'",
1275                           },
1276                 },
1277         },
1278 #endif  /* FIO_HAVE_FALLOCATE */
1279         {
1280                 .name   = "fadvise_hint",
1281                 .type   = FIO_OPT_BOOL,
1282                 .off1   = td_var_offset(fadvise_hint),
1283                 .help   = "Use fadvise() to advise the kernel on IO pattern",
1284                 .def    = "1",
1285         },
1286         {
1287                 .name   = "fsync",
1288                 .type   = FIO_OPT_INT,
1289                 .off1   = td_var_offset(fsync_blocks),
1290                 .help   = "Issue fsync for writes every given number of blocks",
1291                 .def    = "0",
1292         },
1293         {
1294                 .name   = "fdatasync",
1295                 .type   = FIO_OPT_INT,
1296                 .off1   = td_var_offset(fdatasync_blocks),
1297                 .help   = "Issue fdatasync for writes every given number of blocks",
1298                 .def    = "0",
1299         },
1300         {
1301                 .name   = "write_barrier",
1302                 .type   = FIO_OPT_INT,
1303                 .off1   = td_var_offset(barrier_blocks),
1304                 .help   = "Make every Nth write a barrier write",
1305                 .def    = "0",
1306         },
1307 #ifdef FIO_HAVE_SYNC_FILE_RANGE
1308         {
1309                 .name   = "sync_file_range",
1310                 .posval = {
1311                           { .ival = "wait_before",
1312                             .oval = SYNC_FILE_RANGE_WAIT_BEFORE,
1313                             .help = "SYNC_FILE_RANGE_WAIT_BEFORE",
1314                             .or   = 1,
1315                           },
1316                           { .ival = "write",
1317                             .oval = SYNC_FILE_RANGE_WRITE,
1318                             .help = "SYNC_FILE_RANGE_WRITE",
1319                             .or   = 1,
1320                           },
1321                           {
1322                             .ival = "wait_after",
1323                             .oval = SYNC_FILE_RANGE_WAIT_AFTER,
1324                             .help = "SYNC_FILE_RANGE_WAIT_AFTER",
1325                             .or   = 1,
1326                           },
1327                 },
1328                 .type   = FIO_OPT_STR_MULTI,
1329                 .cb     = str_sfr_cb,
1330                 .off1   = td_var_offset(sync_file_range),
1331                 .help   = "Use sync_file_range()",
1332         },
1333 #endif
1334         {
1335                 .name   = "direct",
1336                 .type   = FIO_OPT_BOOL,
1337                 .off1   = td_var_offset(odirect),
1338                 .help   = "Use O_DIRECT IO (negates buffered)",
1339                 .def    = "0",
1340         },
1341         {
1342                 .name   = "buffered",
1343                 .type   = FIO_OPT_BOOL,
1344                 .off1   = td_var_offset(odirect),
1345                 .neg    = 1,
1346                 .help   = "Use buffered IO (negates direct)",
1347                 .def    = "1",
1348         },
1349         {
1350                 .name   = "overwrite",
1351                 .type   = FIO_OPT_BOOL,
1352                 .off1   = td_var_offset(overwrite),
1353                 .help   = "When writing, set whether to overwrite current data",
1354                 .def    = "0",
1355         },
1356         {
1357                 .name   = "loops",
1358                 .type   = FIO_OPT_INT,
1359                 .off1   = td_var_offset(loops),
1360                 .help   = "Number of times to run the job",
1361                 .def    = "1",
1362         },
1363         {
1364                 .name   = "numjobs",
1365                 .type   = FIO_OPT_INT,
1366                 .off1   = td_var_offset(numjobs),
1367                 .help   = "Duplicate this job this many times",
1368                 .def    = "1",
1369         },
1370         {
1371                 .name   = "startdelay",
1372                 .type   = FIO_OPT_STR_VAL_TIME,
1373                 .off1   = td_var_offset(start_delay),
1374                 .help   = "Only start job when this period has passed",
1375                 .def    = "0",
1376         },
1377         {
1378                 .name   = "runtime",
1379                 .alias  = "timeout",
1380                 .type   = FIO_OPT_STR_VAL_TIME,
1381                 .off1   = td_var_offset(timeout),
1382                 .help   = "Stop workload when this amount of time has passed",
1383                 .def    = "0",
1384         },
1385         {
1386                 .name   = "time_based",
1387                 .type   = FIO_OPT_STR_SET,
1388                 .off1   = td_var_offset(time_based),
1389                 .help   = "Keep running until runtime/timeout is met",
1390         },
1391         {
1392                 .name   = "ramp_time",
1393                 .type   = FIO_OPT_STR_VAL_TIME,
1394                 .off1   = td_var_offset(ramp_time),
1395                 .help   = "Ramp up time before measuring performance",
1396         },
1397         {
1398                 .name   = "clocksource",
1399                 .type   = FIO_OPT_STR,
1400                 .cb     = fio_clock_source_cb,
1401                 .off1   = td_var_offset(clocksource),
1402                 .help   = "What type of timing source to use",
1403                 .posval = {
1404                           { .ival = "gettimeofday",
1405                             .oval = CS_GTOD,
1406                             .help = "Use gettimeofday(2) for timing",
1407                           },
1408                           { .ival = "clock_gettime",
1409                             .oval = CS_CGETTIME,
1410                             .help = "Use clock_gettime(2) for timing",
1411                           },
1412 #ifdef ARCH_HAVE_CPU_CLOCK
1413                           { .ival = "cpu",
1414                             .oval = CS_CPUCLOCK,
1415                             .help = "Use CPU private clock",
1416                           },
1417 #endif
1418                 },
1419         },
1420         {
1421                 .name   = "mem",
1422                 .alias  = "iomem",
1423                 .type   = FIO_OPT_STR,
1424                 .cb     = str_mem_cb,
1425                 .off1   = td_var_offset(mem_type),
1426                 .help   = "Backing type for IO buffers",
1427                 .def    = "malloc",
1428                 .posval = {
1429                           { .ival = "malloc",
1430                             .oval = MEM_MALLOC,
1431                             .help = "Use malloc(3) for IO buffers",
1432                           },
1433                           { .ival = "shm",
1434                             .oval = MEM_SHM,
1435                             .help = "Use shared memory segments for IO buffers",
1436                           },
1437 #ifdef FIO_HAVE_HUGETLB
1438                           { .ival = "shmhuge",
1439                             .oval = MEM_SHMHUGE,
1440                             .help = "Like shm, but use huge pages",
1441                           },
1442 #endif
1443                           { .ival = "mmap",
1444                             .oval = MEM_MMAP,
1445                             .help = "Use mmap(2) (file or anon) for IO buffers",
1446                           },
1447 #ifdef FIO_HAVE_HUGETLB
1448                           { .ival = "mmaphuge",
1449                             .oval = MEM_MMAPHUGE,
1450                             .help = "Like mmap, but use huge pages",
1451                           },
1452 #endif
1453                   },
1454         },
1455         {
1456                 .name   = "iomem_align",
1457                 .alias  = "mem_align",
1458                 .type   = FIO_OPT_INT,
1459                 .off1   = td_var_offset(mem_align),
1460                 .minval = 0,
1461                 .help   = "IO memory buffer offset alignment",
1462                 .def    = "0",
1463                 .parent = "iomem",
1464         },
1465         {
1466                 .name   = "verify",
1467                 .type   = FIO_OPT_STR,
1468                 .off1   = td_var_offset(verify),
1469                 .help   = "Verify data written",
1470                 .cb     = str_verify_cb,
1471                 .def    = "0",
1472                 .posval = {
1473                           { .ival = "0",
1474                             .oval = VERIFY_NONE,
1475                             .help = "Don't do IO verification",
1476                           },
1477                           { .ival = "md5",
1478                             .oval = VERIFY_MD5,
1479                             .help = "Use md5 checksums for verification",
1480                           },
1481                           { .ival = "crc64",
1482                             .oval = VERIFY_CRC64,
1483                             .help = "Use crc64 checksums for verification",
1484                           },
1485                           { .ival = "crc32",
1486                             .oval = VERIFY_CRC32,
1487                             .help = "Use crc32 checksums for verification",
1488                           },
1489                           { .ival = "crc32c-intel",
1490                             .oval = VERIFY_CRC32C,
1491                             .help = "Use crc32c checksums for verification (hw assisted, if available)",
1492                           },
1493                           { .ival = "crc32c",
1494                             .oval = VERIFY_CRC32C,
1495                             .help = "Use crc32c checksums for verification (hw assisted, if available)",
1496                           },
1497                           { .ival = "crc16",
1498                             .oval = VERIFY_CRC16,
1499                             .help = "Use crc16 checksums for verification",
1500                           },
1501                           { .ival = "crc7",
1502                             .oval = VERIFY_CRC7,
1503                             .help = "Use crc7 checksums for verification",
1504                           },
1505                           { .ival = "sha1",
1506                             .oval = VERIFY_SHA1,
1507                             .help = "Use sha1 checksums for verification",
1508                           },
1509                           { .ival = "sha256",
1510                             .oval = VERIFY_SHA256,
1511                             .help = "Use sha256 checksums for verification",
1512                           },
1513                           { .ival = "sha512",
1514                             .oval = VERIFY_SHA512,
1515                             .help = "Use sha512 checksums for verification",
1516                           },
1517                           { .ival = "meta",
1518                             .oval = VERIFY_META,
1519                             .help = "Use io information",
1520                           },
1521                           {
1522                             .ival = "null",
1523                             .oval = VERIFY_NULL,
1524                             .help = "Pretend to verify",
1525                           },
1526                 },
1527         },
1528         {
1529                 .name   = "do_verify",
1530                 .type   = FIO_OPT_BOOL,
1531                 .off1   = td_var_offset(do_verify),
1532                 .help   = "Run verification stage after write",
1533                 .def    = "1",
1534                 .parent = "verify",
1535         },
1536         {
1537                 .name   = "verifysort",
1538                 .type   = FIO_OPT_BOOL,
1539                 .off1   = td_var_offset(verifysort),
1540                 .help   = "Sort written verify blocks for read back",
1541                 .def    = "1",
1542                 .parent = "verify",
1543         },
1544         {
1545                 .name   = "verify_interval",
1546                 .type   = FIO_OPT_INT,
1547                 .off1   = td_var_offset(verify_interval),
1548                 .minval = 2 * sizeof(struct verify_header),
1549                 .help   = "Store verify buffer header every N bytes",
1550                 .parent = "verify",
1551         },
1552         {
1553                 .name   = "verify_offset",
1554                 .type   = FIO_OPT_INT,
1555                 .help   = "Offset verify header location by N bytes",
1556                 .def    = "0",
1557                 .cb     = str_verify_offset_cb,
1558                 .parent = "verify",
1559         },
1560         {
1561                 .name   = "verify_pattern",
1562                 .type   = FIO_OPT_STR,
1563                 .cb     = str_verify_pattern_cb,
1564                 .help   = "Fill pattern for IO buffers",
1565                 .parent = "verify",
1566         },
1567         {
1568                 .name   = "verify_fatal",
1569                 .type   = FIO_OPT_BOOL,
1570                 .off1   = td_var_offset(verify_fatal),
1571                 .def    = "0",
1572                 .help   = "Exit on a single verify failure, don't continue",
1573                 .parent = "verify",
1574         },
1575         {
1576                 .name   = "verify_dump",
1577                 .type   = FIO_OPT_BOOL,
1578                 .off1   = td_var_offset(verify_dump),
1579                 .def    = "0",
1580                 .help   = "Dump contents of good and bad blocks on failure",
1581                 .parent = "verify",
1582         },
1583         {
1584                 .name   = "verify_async",
1585                 .type   = FIO_OPT_INT,
1586                 .off1   = td_var_offset(verify_async),
1587                 .def    = "0",
1588                 .help   = "Number of async verifier threads to use",
1589                 .parent = "verify",
1590         },
1591         {
1592                 .name   = "verify_backlog",
1593                 .type   = FIO_OPT_STR_VAL,
1594                 .off1   = td_var_offset(verify_backlog),
1595                 .help   = "Verify after this number of blocks are written",
1596                 .parent = "verify",
1597         },
1598         {
1599                 .name   = "verify_backlog_batch",
1600                 .type   = FIO_OPT_INT,
1601                 .off1   = td_var_offset(verify_batch),
1602                 .help   = "Verify this number of IO blocks",
1603                 .parent = "verify",
1604         },
1605 #ifdef FIO_HAVE_CPU_AFFINITY
1606         {
1607                 .name   = "verify_async_cpus",
1608                 .type   = FIO_OPT_STR,
1609                 .cb     = str_verify_cpus_allowed_cb,
1610                 .help   = "Set CPUs allowed for async verify threads",
1611                 .parent = "verify_async",
1612         },
1613 #endif
1614 #ifdef FIO_HAVE_TRIM
1615         {
1616                 .name   = "trim_percentage",
1617                 .type   = FIO_OPT_INT,
1618                 .cb     = str_verify_trim_cb,
1619                 .maxval = 100,
1620                 .help   = "Number of verify blocks to discard/trim",
1621                 .parent = "verify",
1622                 .def    = "0",
1623         },
1624         {
1625                 .name   = "trim_verify_zero",
1626                 .type   = FIO_OPT_INT,
1627                 .help   = "Verify that trim/discarded blocks are returned as zeroes",
1628                 .off1   = td_var_offset(trim_zero),
1629                 .parent = "trim_percentage",
1630                 .def    = "1",
1631         },
1632         {
1633                 .name   = "trim_backlog",
1634                 .type   = FIO_OPT_STR_VAL,
1635                 .off1   = td_var_offset(trim_backlog),
1636                 .help   = "Trim after this number of blocks are written",
1637                 .parent = "trim_percentage",
1638         },
1639         {
1640                 .name   = "trim_backlog_batch",
1641                 .type   = FIO_OPT_INT,
1642                 .off1   = td_var_offset(trim_batch),
1643                 .help   = "Trim this number of IO blocks",
1644                 .parent = "trim_percentage",
1645         },
1646 #endif
1647         {
1648                 .name   = "write_iolog",
1649                 .type   = FIO_OPT_STR_STORE,
1650                 .off1   = td_var_offset(write_iolog_file),
1651                 .help   = "Store IO pattern to file",
1652         },
1653         {
1654                 .name   = "read_iolog",
1655                 .type   = FIO_OPT_STR_STORE,
1656                 .off1   = td_var_offset(read_iolog_file),
1657                 .help   = "Playback IO pattern from file",
1658         },
1659         {
1660                 .name   = "replay_no_stall",
1661                 .type   = FIO_OPT_INT,
1662                 .off1   = td_var_offset(no_stall),
1663                 .def    = "0",
1664                 .parent = "read_iolog",
1665                 .help   = "Playback IO pattern file as fast as possible without stalls",
1666         },
1667         {
1668                 .name   = "replay_redirect",
1669                 .type   = FIO_OPT_STR_STORE,
1670                 .off1   = td_var_offset(replay_redirect),
1671                 .parent = "read_iolog",
1672                 .help   = "Replay all I/O onto this device, regardless of trace device",
1673         },
1674         {
1675                 .name   = "exec_prerun",
1676                 .type   = FIO_OPT_STR_STORE,
1677                 .off1   = td_var_offset(exec_prerun),
1678                 .help   = "Execute this file prior to running job",
1679         },
1680         {
1681                 .name   = "exec_postrun",
1682                 .type   = FIO_OPT_STR_STORE,
1683                 .off1   = td_var_offset(exec_postrun),
1684                 .help   = "Execute this file after running job",
1685         },
1686 #ifdef FIO_HAVE_IOSCHED_SWITCH
1687         {
1688                 .name   = "ioscheduler",
1689                 .type   = FIO_OPT_STR_STORE,
1690                 .off1   = td_var_offset(ioscheduler),
1691                 .help   = "Use this IO scheduler on the backing device",
1692         },
1693 #endif
1694         {
1695                 .name   = "zonesize",
1696                 .type   = FIO_OPT_STR_VAL,
1697                 .off1   = td_var_offset(zone_size),
1698                 .help   = "Amount of data to read per zone",
1699                 .def    = "0",
1700         },
1701         {
1702                 .name   = "zonerange",
1703                 .type   = FIO_OPT_STR_VAL,
1704                 .off1   = td_var_offset(zone_range),
1705                 .help   = "Give size of an IO zone",
1706                 .def    = "0",
1707         },
1708         {
1709                 .name   = "zoneskip",
1710                 .type   = FIO_OPT_STR_VAL,
1711                 .off1   = td_var_offset(zone_skip),
1712                 .help   = "Space between IO zones",
1713                 .def    = "0",
1714         },
1715         {
1716                 .name   = "lockmem",
1717                 .type   = FIO_OPT_STR_VAL,
1718                 .cb     = str_lockmem_cb,
1719                 .help   = "Lock down this amount of memory",
1720                 .def    = "0",
1721         },
1722         {
1723                 .name   = "rwmixread",
1724                 .type   = FIO_OPT_INT,
1725                 .cb     = str_rwmix_read_cb,
1726                 .maxval = 100,
1727                 .help   = "Percentage of mixed workload that is reads",
1728                 .def    = "50",
1729         },
1730         {
1731                 .name   = "rwmixwrite",
1732                 .type   = FIO_OPT_INT,
1733                 .cb     = str_rwmix_write_cb,
1734                 .maxval = 100,
1735                 .help   = "Percentage of mixed workload that is writes",
1736                 .def    = "50",
1737         },
1738         {
1739                 .name   = "rwmixcycle",
1740                 .type   = FIO_OPT_DEPRECATED,
1741         },
1742         {
1743                 .name   = "nice",
1744                 .type   = FIO_OPT_INT,
1745                 .off1   = td_var_offset(nice),
1746                 .help   = "Set job CPU nice value",
1747                 .minval = -19,
1748                 .maxval = 20,
1749                 .def    = "0",
1750         },
1751 #ifdef FIO_HAVE_IOPRIO
1752         {
1753                 .name   = "prio",
1754                 .type   = FIO_OPT_INT,
1755                 .cb     = str_prio_cb,
1756                 .help   = "Set job IO priority value",
1757                 .minval = 0,
1758                 .maxval = 7,
1759         },
1760         {
1761                 .name   = "prioclass",
1762                 .type   = FIO_OPT_INT,
1763                 .cb     = str_prioclass_cb,
1764                 .help   = "Set job IO priority class",
1765                 .minval = 0,
1766                 .maxval = 3,
1767         },
1768 #endif
1769         {
1770                 .name   = "thinktime",
1771                 .type   = FIO_OPT_INT,
1772                 .off1   = td_var_offset(thinktime),
1773                 .help   = "Idle time between IO buffers (usec)",
1774                 .def    = "0",
1775         },
1776         {
1777                 .name   = "thinktime_spin",
1778                 .type   = FIO_OPT_INT,
1779                 .off1   = td_var_offset(thinktime_spin),
1780                 .help   = "Start think time by spinning this amount (usec)",
1781                 .def    = "0",
1782                 .parent = "thinktime",
1783         },
1784         {
1785                 .name   = "thinktime_blocks",
1786                 .type   = FIO_OPT_INT,
1787                 .off1   = td_var_offset(thinktime_blocks),
1788                 .help   = "IO buffer period between 'thinktime'",
1789                 .def    = "1",
1790                 .parent = "thinktime",
1791         },
1792         {
1793                 .name   = "rate",
1794                 .type   = FIO_OPT_INT,
1795                 .off1   = td_var_offset(rate[0]),
1796                 .off2   = td_var_offset(rate[1]),
1797                 .help   = "Set bandwidth rate",
1798         },
1799         {
1800                 .name   = "ratemin",
1801                 .type   = FIO_OPT_INT,
1802                 .off1   = td_var_offset(ratemin[0]),
1803                 .off2   = td_var_offset(ratemin[1]),
1804                 .help   = "Job must meet this rate or it will be shutdown",
1805                 .parent = "rate",
1806         },
1807         {
1808                 .name   = "rate_iops",
1809                 .type   = FIO_OPT_INT,
1810                 .off1   = td_var_offset(rate_iops[0]),
1811                 .off2   = td_var_offset(rate_iops[1]),
1812                 .help   = "Limit IO used to this number of IO operations/sec",
1813         },
1814         {
1815                 .name   = "rate_iops_min",
1816                 .type   = FIO_OPT_INT,
1817                 .off1   = td_var_offset(rate_iops_min[0]),
1818                 .off2   = td_var_offset(rate_iops_min[1]),
1819                 .help   = "Job must meet this rate or it will be shut down",
1820                 .parent = "rate_iops",
1821         },
1822         {
1823                 .name   = "ratecycle",
1824                 .type   = FIO_OPT_INT,
1825                 .off1   = td_var_offset(ratecycle),
1826                 .help   = "Window average for rate limits (msec)",
1827                 .def    = "1000",
1828                 .parent = "rate",
1829         },
1830         {
1831                 .name   = "invalidate",
1832                 .type   = FIO_OPT_BOOL,
1833                 .off1   = td_var_offset(invalidate_cache),
1834                 .help   = "Invalidate buffer/page cache prior to running job",
1835                 .def    = "1",
1836         },
1837         {
1838                 .name   = "sync",
1839                 .type   = FIO_OPT_BOOL,
1840                 .off1   = td_var_offset(sync_io),
1841                 .help   = "Use O_SYNC for buffered writes",
1842                 .def    = "0",
1843                 .parent = "buffered",
1844         },
1845         {
1846                 .name   = "bwavgtime",
1847                 .type   = FIO_OPT_INT,
1848                 .off1   = td_var_offset(bw_avg_time),
1849                 .help   = "Time window over which to calculate bandwidth"
1850                           " (msec)",
1851                 .def    = "500",
1852                 .parent = "write_bw_log",
1853         },
1854         {
1855                 .name   = "iopsavgtime",
1856                 .type   = FIO_OPT_INT,
1857                 .off1   = td_var_offset(iops_avg_time),
1858                 .help   = "Time window over which to calculate IOPS (msec)",
1859                 .def    = "500",
1860                 .parent = "write_iops_log",
1861         },
1862         {
1863                 .name   = "create_serialize",
1864                 .type   = FIO_OPT_BOOL,
1865                 .off1   = td_var_offset(create_serialize),
1866                 .help   = "Serialize creating of job files",
1867                 .def    = "1",
1868         },
1869         {
1870                 .name   = "create_fsync",
1871                 .type   = FIO_OPT_BOOL,
1872                 .off1   = td_var_offset(create_fsync),
1873                 .help   = "fsync file after creation",
1874                 .def    = "1",
1875         },
1876         {
1877                 .name   = "create_on_open",
1878                 .type   = FIO_OPT_BOOL,
1879                 .off1   = td_var_offset(create_on_open),
1880                 .help   = "Create files when they are opened for IO",
1881                 .def    = "0",
1882         },
1883         {
1884                 .name   = "pre_read",
1885                 .type   = FIO_OPT_BOOL,
1886                 .off1   = td_var_offset(pre_read),
1887                 .help   = "Pre-read files before starting official testing",
1888                 .def    = "0",
1889         },
1890         {
1891                 .name   = "cpuload",
1892                 .type   = FIO_OPT_INT,
1893                 .off1   = td_var_offset(cpuload),
1894                 .help   = "Use this percentage of CPU",
1895         },
1896         {
1897                 .name   = "cpuchunks",
1898                 .type   = FIO_OPT_INT,
1899                 .off1   = td_var_offset(cpucycle),
1900                 .help   = "Length of the CPU burn cycles (usecs)",
1901                 .def    = "50000",
1902                 .parent = "cpuload",
1903         },
1904 #ifdef FIO_HAVE_CPU_AFFINITY
1905         {
1906                 .name   = "cpumask",
1907                 .type   = FIO_OPT_INT,
1908                 .cb     = str_cpumask_cb,
1909                 .help   = "CPU affinity mask",
1910         },
1911         {
1912                 .name   = "cpus_allowed",
1913                 .type   = FIO_OPT_STR,
1914                 .cb     = str_cpus_allowed_cb,
1915                 .help   = "Set CPUs allowed",
1916         },
1917 #endif
1918         {
1919                 .name   = "end_fsync",
1920                 .type   = FIO_OPT_BOOL,
1921                 .off1   = td_var_offset(end_fsync),
1922                 .help   = "Include fsync at the end of job",
1923                 .def    = "0",
1924         },
1925         {
1926                 .name   = "fsync_on_close",
1927                 .type   = FIO_OPT_BOOL,
1928                 .off1   = td_var_offset(fsync_on_close),
1929                 .help   = "fsync files on close",
1930                 .def    = "0",
1931         },
1932         {
1933                 .name   = "unlink",
1934                 .type   = FIO_OPT_BOOL,
1935                 .off1   = td_var_offset(unlink),
1936                 .help   = "Unlink created files after job has completed",
1937                 .def    = "0",
1938         },
1939         {
1940                 .name   = "exitall",
1941                 .type   = FIO_OPT_STR_SET,
1942                 .cb     = str_exitall_cb,
1943                 .help   = "Terminate all jobs when one exits",
1944         },
1945         {
1946                 .name   = "stonewall",
1947                 .alias  = "wait_for_previous",
1948                 .type   = FIO_OPT_STR_SET,
1949                 .off1   = td_var_offset(stonewall),
1950                 .help   = "Insert a hard barrier between this job and previous",
1951         },
1952         {
1953                 .name   = "new_group",
1954                 .type   = FIO_OPT_STR_SET,
1955                 .off1   = td_var_offset(new_group),
1956                 .help   = "Mark the start of a new group (for reporting)",
1957         },
1958         {
1959                 .name   = "thread",
1960                 .type   = FIO_OPT_STR_SET,
1961                 .off1   = td_var_offset(use_thread),
1962                 .help   = "Use threads instead of forks",
1963         },
1964         {
1965                 .name   = "write_bw_log",
1966                 .type   = FIO_OPT_STR,
1967                 .off1   = td_var_offset(write_bw_log),
1968                 .cb     = str_write_bw_log_cb,
1969                 .help   = "Write log of bandwidth during run",
1970         },
1971         {
1972                 .name   = "write_lat_log",
1973                 .type   = FIO_OPT_STR,
1974                 .off1   = td_var_offset(write_lat_log),
1975                 .cb     = str_write_lat_log_cb,
1976                 .help   = "Write log of latency during run",
1977         },
1978         {
1979                 .name   = "write_iops_log",
1980                 .type   = FIO_OPT_STR,
1981                 .off1   = td_var_offset(write_iops_log),
1982                 .cb     = str_write_iops_log_cb,
1983                 .help   = "Write log of IOPS during run",
1984         },
1985         {
1986                 .name   = "log_avg_msec",
1987                 .type   = FIO_OPT_INT,
1988                 .off1   = td_var_offset(log_avg_msec),
1989                 .help   = "Average bw/iops/lat logs over this period of time",
1990                 .def    = "0",
1991         },
1992         {
1993                 .name   = "hugepage-size",
1994                 .type   = FIO_OPT_INT,
1995                 .off1   = td_var_offset(hugepage_size),
1996                 .help   = "When using hugepages, specify size of each page",
1997                 .def    = __fio_stringify(FIO_HUGE_PAGE),
1998         },
1999         {
2000                 .name   = "group_reporting",
2001                 .type   = FIO_OPT_STR_SET,
2002                 .off1   = td_var_offset(group_reporting),
2003                 .help   = "Do reporting on a per-group basis",
2004         },
2005         {
2006                 .name   = "zero_buffers",
2007                 .type   = FIO_OPT_STR_SET,
2008                 .off1   = td_var_offset(zero_buffers),
2009                 .help   = "Init IO buffers to all zeroes",
2010         },
2011         {
2012                 .name   = "refill_buffers",
2013                 .type   = FIO_OPT_STR_SET,
2014                 .off1   = td_var_offset(refill_buffers),
2015                 .help   = "Refill IO buffers on every IO submit",
2016         },
2017         {
2018                 .name   = "scramble_buffers",
2019                 .type   = FIO_OPT_BOOL,
2020                 .off1   = td_var_offset(scramble_buffers),
2021                 .help   = "Slightly scramble buffers on every IO submit",
2022                 .def    = "1",
2023         },
2024         {
2025                 .name   = "buffer_compress_percentage",
2026                 .type   = FIO_OPT_INT,
2027                 .off1   = td_var_offset(compress_percentage),
2028                 .maxval = 100,
2029                 .minval = 1,
2030                 .help   = "How compressible the buffer is (approximately)",
2031         },
2032         {
2033                 .name   = "buffer_compress_chunk",
2034                 .type   = FIO_OPT_INT,
2035                 .off1   = td_var_offset(compress_chunk),
2036                 .parent = "buffer_compress_percentage",
2037                 .help   = "Size of compressible region in buffer",
2038         },
2039         {
2040                 .name   = "clat_percentiles",
2041                 .type   = FIO_OPT_BOOL,
2042                 .off1   = td_var_offset(clat_percentiles),
2043                 .help   = "Enable the reporting of completion latency percentiles",
2044                 .def    = "1",
2045         },
2046         {
2047                 .name   = "percentile_list",
2048                 .type   = FIO_OPT_FLOAT_LIST,
2049                 .off1   = td_var_offset(percentile_list),
2050                 .off2   = td_var_offset(overwrite_plist),
2051                 .help   = "Specify a custom list of percentiles to report",
2052                 .maxlen = FIO_IO_U_LIST_MAX_LEN,
2053                 .minfp  = 0.0,
2054                 .maxfp  = 100.0,
2055         },
2056
2057 #ifdef FIO_HAVE_DISK_UTIL
2058         {
2059                 .name   = "disk_util",
2060                 .type   = FIO_OPT_BOOL,
2061                 .off1   = td_var_offset(do_disk_util),
2062                 .help   = "Log disk utilization statistics",
2063                 .def    = "1",
2064         },
2065 #endif
2066         {
2067                 .name   = "gtod_reduce",
2068                 .type   = FIO_OPT_BOOL,
2069                 .help   = "Greatly reduce number of gettimeofday() calls",
2070                 .cb     = str_gtod_reduce_cb,
2071                 .def    = "0",
2072         },
2073         {
2074                 .name   = "disable_lat",
2075                 .type   = FIO_OPT_BOOL,
2076                 .off1   = td_var_offset(disable_lat),
2077                 .help   = "Disable latency numbers",
2078                 .parent = "gtod_reduce",
2079                 .def    = "0",
2080         },
2081         {
2082                 .name   = "disable_clat",
2083                 .type   = FIO_OPT_BOOL,
2084                 .off1   = td_var_offset(disable_clat),
2085                 .help   = "Disable completion latency numbers",
2086                 .parent = "gtod_reduce",
2087                 .def    = "0",
2088         },
2089         {
2090                 .name   = "disable_slat",
2091                 .type   = FIO_OPT_BOOL,
2092                 .off1   = td_var_offset(disable_slat),
2093                 .help   = "Disable submission latency numbers",
2094                 .parent = "gtod_reduce",
2095                 .def    = "0",
2096         },
2097         {
2098                 .name   = "disable_bw_measurement",
2099                 .type   = FIO_OPT_BOOL,
2100                 .off1   = td_var_offset(disable_bw),
2101                 .help   = "Disable bandwidth logging",
2102                 .parent = "gtod_reduce",
2103                 .def    = "0",
2104         },
2105         {
2106                 .name   = "gtod_cpu",
2107                 .type   = FIO_OPT_INT,
2108                 .cb     = str_gtod_cpu_cb,
2109                 .help   = "Set up dedicated gettimeofday() thread on this CPU",
2110                 .verify = gtod_cpu_verify,
2111         },
2112         {
2113                 .name   = "continue_on_error",
2114                 .type   = FIO_OPT_STR,
2115                 .off1   = td_var_offset(continue_on_error),
2116                 .help   = "Continue on non-fatal errors during IO",
2117                 .def    = "none",
2118                 .posval = {
2119                           { .ival = "none",
2120                             .oval = ERROR_TYPE_NONE,
2121                             .help = "Exit when an error is encountered",
2122                           },
2123                           { .ival = "read",
2124                             .oval = ERROR_TYPE_READ,
2125                             .help = "Continue on read errors only",
2126                           },
2127                           { .ival = "write",
2128                             .oval = ERROR_TYPE_WRITE,
2129                             .help = "Continue on write errors only",
2130                           },
2131                           { .ival = "io",
2132                             .oval = ERROR_TYPE_READ | ERROR_TYPE_WRITE,
2133                             .help = "Continue on any IO errors",
2134                           },
2135                           { .ival = "verify",
2136                             .oval = ERROR_TYPE_VERIFY,
2137                             .help = "Continue on verify errors only",
2138                           },
2139                           { .ival = "all",
2140                             .oval = ERROR_TYPE_ANY,
2141                             .help = "Continue on all io and verify errors",
2142                           },
2143                           { .ival = "0",
2144                             .oval = ERROR_TYPE_NONE,
2145                             .help = "Alias for 'none'",
2146                           },
2147                           { .ival = "1",
2148                             .oval = ERROR_TYPE_ANY,
2149                             .help = "Alias for 'all'",
2150                           },
2151                 },
2152         },
2153         {
2154                 .name   = "profile",
2155                 .type   = FIO_OPT_STR_STORE,
2156                 .off1   = td_var_offset(profile),
2157                 .help   = "Select a specific builtin performance test",
2158         },
2159         {
2160                 .name   = "cgroup",
2161                 .type   = FIO_OPT_STR_STORE,
2162                 .off1   = td_var_offset(cgroup),
2163                 .help   = "Add job to cgroup of this name",
2164         },
2165         {
2166                 .name   = "cgroup_weight",
2167                 .type   = FIO_OPT_INT,
2168                 .off1   = td_var_offset(cgroup_weight),
2169                 .help   = "Use given weight for cgroup",
2170                 .minval = 100,
2171                 .maxval = 1000,
2172         },
2173         {
2174                 .name   = "cgroup_nodelete",
2175                 .type   = FIO_OPT_BOOL,
2176                 .off1   = td_var_offset(cgroup_nodelete),
2177                 .help   = "Do not delete cgroups after job completion",
2178                 .def    = "0",
2179         },
2180         {
2181                 .name   = "uid",
2182                 .type   = FIO_OPT_INT,
2183                 .off1   = td_var_offset(uid),
2184                 .help   = "Run job with this user ID",
2185         },
2186         {
2187                 .name   = "gid",
2188                 .type   = FIO_OPT_INT,
2189                 .off1   = td_var_offset(gid),
2190                 .help   = "Run job with this group ID",
2191         },
2192         {
2193                 .name   = "flow_id",
2194                 .type   = FIO_OPT_INT,
2195                 .off1   = td_var_offset(flow_id),
2196                 .help   = "The flow index ID to use",
2197                 .def    = "0",
2198         },
2199         {
2200                 .name   = "flow",
2201                 .type   = FIO_OPT_INT,
2202                 .off1   = td_var_offset(flow),
2203                 .help   = "Weight for flow control of this job",
2204                 .parent = "flow_id",
2205                 .def    = "0",
2206         },
2207         {
2208                 .name   = "flow_watermark",
2209                 .type   = FIO_OPT_INT,
2210                 .off1   = td_var_offset(flow_watermark),
2211                 .help   = "High watermark for flow control. This option"
2212                         " should be set to the same value for all threads"
2213                         " with non-zero flow.",
2214                 .parent = "flow_id",
2215                 .def    = "1024",
2216         },
2217         {
2218                 .name   = "flow_sleep",
2219                 .type   = FIO_OPT_INT,
2220                 .off1   = td_var_offset(flow_sleep),
2221                 .help   = "How many microseconds to sleep after being held"
2222                         " back by the flow control mechanism",
2223                 .parent = "flow_id",
2224                 .def    = "0",
2225         },
2226         {
2227                 .name = NULL,
2228         },
2229 };
2230
2231 static void add_to_lopt(struct option *lopt, struct fio_option *o,
2232                         const char *name, int val)
2233 {
2234         lopt->name = (char *) name;
2235         lopt->val = val;
2236         if (o->type == FIO_OPT_STR_SET)
2237                 lopt->has_arg = no_argument;
2238         else
2239                 lopt->has_arg = required_argument;
2240 }
2241
2242 static void options_to_lopts(struct fio_option *opts,
2243                               struct option *long_options,
2244                               int i, int option_type)
2245 {
2246         struct fio_option *o = &opts[0];
2247         while (o->name) {
2248                 add_to_lopt(&long_options[i], o, o->name, option_type);
2249                 if (o->alias) {
2250                         i++;
2251                         add_to_lopt(&long_options[i], o, o->alias, option_type);
2252                 }
2253
2254                 i++;
2255                 o++;
2256                 assert(i < FIO_NR_OPTIONS);
2257         }
2258 }
2259
2260 void fio_options_set_ioengine_opts(struct option *long_options,
2261                                    struct thread_data *td)
2262 {
2263         unsigned int i;
2264
2265         i = 0;
2266         while (long_options[i].name) {
2267                 if (long_options[i].val == FIO_GETOPT_IOENGINE) {
2268                         memset(&long_options[i], 0, sizeof(*long_options));
2269                         break;
2270                 }
2271                 i++;
2272         }
2273
2274         /*
2275          * Just clear out the prior ioengine options.
2276          */
2277         if (!td || !td->eo)
2278                 return;
2279
2280         options_to_lopts(td->io_ops->options, long_options, i,
2281                          FIO_GETOPT_IOENGINE);
2282 }
2283
2284 void fio_options_dup_and_init(struct option *long_options)
2285 {
2286         unsigned int i;
2287
2288         options_init(options);
2289
2290         i = 0;
2291         while (long_options[i].name)
2292                 i++;
2293
2294         options_to_lopts(options, long_options, i, FIO_GETOPT_JOB);
2295 }
2296
2297 struct fio_keyword {
2298         const char *word;
2299         const char *desc;
2300         char *replace;
2301 };
2302
2303 static struct fio_keyword fio_keywords[] = {
2304         {
2305                 .word   = "$pagesize",
2306                 .desc   = "Page size in the system",
2307         },
2308         {
2309                 .word   = "$mb_memory",
2310                 .desc   = "Megabytes of memory online",
2311         },
2312         {
2313                 .word   = "$ncpus",
2314                 .desc   = "Number of CPUs online in the system",
2315         },
2316         {
2317                 .word   = NULL,
2318         },
2319 };
2320
2321 void fio_keywords_init(void)
2322 {
2323         unsigned long long mb_memory;
2324         char buf[128];
2325         long l;
2326
2327         sprintf(buf, "%lu", page_size);
2328         fio_keywords[0].replace = strdup(buf);
2329
2330         mb_memory = os_phys_mem() / (1024 * 1024);
2331         sprintf(buf, "%llu", mb_memory);
2332         fio_keywords[1].replace = strdup(buf);
2333
2334         l = cpus_online();
2335         sprintf(buf, "%lu", l);
2336         fio_keywords[2].replace = strdup(buf);
2337 }
2338
2339 #define BC_APP          "bc"
2340
2341 static char *bc_calc(char *str)
2342 {
2343         char buf[128], *tmp;
2344         FILE *f;
2345         int ret;
2346
2347         /*
2348          * No math, just return string
2349          */
2350         if ((!strchr(str, '+') && !strchr(str, '-') && !strchr(str, '*') &&
2351              !strchr(str, '/')) || strchr(str, '\''))
2352                 return str;
2353
2354         /*
2355          * Split option from value, we only need to calculate the value
2356          */
2357         tmp = strchr(str, '=');
2358         if (!tmp)
2359                 return str;
2360
2361         tmp++;
2362
2363         /*
2364          * Prevent buffer overflows; such a case isn't reasonable anyway
2365          */
2366         if (strlen(str) >= 128 || strlen(tmp) > 100)
2367                 return str;
2368
2369         sprintf(buf, "which %s > /dev/null", BC_APP);
2370         if (system(buf)) {
2371                 log_err("fio: bc is needed for performing math\n");
2372                 return NULL;
2373         }
2374
2375         sprintf(buf, "echo '%s' | %s", tmp, BC_APP);
2376         f = popen(buf, "r");
2377         if (!f) {
2378                 return NULL;
2379         }
2380
2381         ret = fread(&buf[tmp - str], 1, 128 - (tmp - str), f);
2382         if (ret <= 0) {
2383                 return NULL;
2384         }
2385
2386         pclose(f);
2387         buf[(tmp - str) + ret - 1] = '\0';
2388         memcpy(buf, str, tmp - str);
2389         free(str);
2390         return strdup(buf);
2391 }
2392
2393 /*
2394  * Return a copy of the input string with substrings of the form ${VARNAME}
2395  * substituted with the value of the environment variable VARNAME.  The
2396  * substitution always occurs, even if VARNAME is empty or the corresponding
2397  * environment variable undefined.
2398  */
2399 static char *option_dup_subs(const char *opt)
2400 {
2401         char out[OPT_LEN_MAX+1];
2402         char in[OPT_LEN_MAX+1];
2403         char *outptr = out;
2404         char *inptr = in;
2405         char *ch1, *ch2, *env;
2406         ssize_t nchr = OPT_LEN_MAX;
2407         size_t envlen;
2408
2409         if (strlen(opt) + 1 > OPT_LEN_MAX) {
2410                 log_err("OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
2411                 return NULL;
2412         }
2413
2414         in[OPT_LEN_MAX] = '\0';
2415         strncpy(in, opt, OPT_LEN_MAX);
2416
2417         while (*inptr && nchr > 0) {
2418                 if (inptr[0] == '$' && inptr[1] == '{') {
2419                         ch2 = strchr(inptr, '}');
2420                         if (ch2 && inptr+1 < ch2) {
2421                                 ch1 = inptr+2;
2422                                 inptr = ch2+1;
2423                                 *ch2 = '\0';
2424
2425                                 env = getenv(ch1);
2426                                 if (env) {
2427                                         envlen = strlen(env);
2428                                         if (envlen <= nchr) {
2429                                                 memcpy(outptr, env, envlen);
2430                                                 outptr += envlen;
2431                                                 nchr -= envlen;
2432                                         }
2433                                 }
2434
2435                                 continue;
2436                         }
2437                 }
2438
2439                 *outptr++ = *inptr++;
2440                 --nchr;
2441         }
2442
2443         *outptr = '\0';
2444         return strdup(out);
2445 }
2446
2447 /*
2448  * Look for reserved variable names and replace them with real values
2449  */
2450 static char *fio_keyword_replace(char *opt)
2451 {
2452         char *s;
2453         int i;
2454         int docalc = 0;
2455
2456         for (i = 0; fio_keywords[i].word != NULL; i++) {
2457                 struct fio_keyword *kw = &fio_keywords[i];
2458
2459                 while ((s = strstr(opt, kw->word)) != NULL) {
2460                         char *new = malloc(strlen(opt) + 1);
2461                         char *o_org = opt;
2462                         int olen = s - opt;
2463                         int len;
2464
2465                         /*
2466                          * Copy part of the string before the keyword and
2467                          * sprintf() the replacement after it.
2468                          */
2469                         memcpy(new, opt, olen);
2470                         len = sprintf(new + olen, "%s", kw->replace);
2471
2472                         /*
2473                          * If there's more in the original string, copy that
2474                          * in too
2475                          */
2476                         opt += strlen(kw->word) + olen;
2477                         if (strlen(opt))
2478                                 memcpy(new + olen + len, opt, opt - o_org - 1);
2479
2480                         /*
2481                          * replace opt and free the old opt
2482                          */
2483                         opt = new;
2484                         free(o_org);
2485
2486                         docalc = 1;
2487                 }
2488         }
2489
2490         /*
2491          * Check for potential math and invoke bc, if possible
2492          */
2493         if (docalc)
2494                 opt = bc_calc(opt);
2495
2496         return opt;
2497 }
2498
2499 static char **dup_and_sub_options(char **opts, int num_opts)
2500 {
2501         int i;
2502         char **opts_copy = malloc(num_opts * sizeof(*opts));
2503         for (i = 0; i < num_opts; i++) {
2504                 opts_copy[i] = option_dup_subs(opts[i]);
2505                 if (!opts_copy[i])
2506                         continue;
2507                 opts_copy[i] = fio_keyword_replace(opts_copy[i]);
2508         }
2509         return opts_copy;
2510 }
2511
2512 int fio_options_parse(struct thread_data *td, char **opts, int num_opts)
2513 {
2514         int i, ret, unknown;
2515         char **opts_copy;
2516
2517         sort_options(opts, options, num_opts);
2518         opts_copy = dup_and_sub_options(opts, num_opts);
2519
2520         for (ret = 0, i = 0, unknown = 0; i < num_opts; i++) {
2521                 struct fio_option *o;
2522                 int newret = parse_option(opts_copy[i], opts[i], options, &o,
2523                                           td);
2524
2525                 if (opts_copy[i]) {
2526                         if (newret && !o) {
2527                                 unknown++;
2528                                 continue;
2529                         }
2530                         free(opts_copy[i]);
2531                         opts_copy[i] = NULL;
2532                 }
2533
2534                 ret |= newret;
2535         }
2536
2537         if (unknown) {
2538                 ret |= ioengine_load(td);
2539                 if (td->eo) {
2540                         sort_options(opts_copy, td->io_ops->options, num_opts);
2541                         opts = opts_copy;
2542                 }
2543                 for (i = 0; i < num_opts; i++) {
2544                         struct fio_option *o = NULL;
2545                         int newret = 1;
2546                         if (!opts_copy[i])
2547                                 continue;
2548
2549                         if (td->eo)
2550                                 newret = parse_option(opts_copy[i], opts[i],
2551                                                       td->io_ops->options, &o,
2552                                                       td->eo);
2553
2554                         ret |= newret;
2555                         if (!o)
2556                                 log_err("Bad option <%s>\n", opts[i]);
2557
2558                         free(opts_copy[i]);
2559                         opts_copy[i] = NULL;
2560                 }
2561         }
2562
2563         free(opts_copy);
2564         return ret;
2565 }
2566
2567 int fio_cmd_option_parse(struct thread_data *td, const char *opt, char *val)
2568 {
2569         return parse_cmd_option(opt, val, options, td);
2570 }
2571
2572 int fio_cmd_ioengine_option_parse(struct thread_data *td, const char *opt,
2573                                 char *val)
2574 {
2575         return parse_cmd_option(opt, val, td->io_ops->options, td);
2576 }
2577
2578 void fio_fill_default_options(struct thread_data *td)
2579 {
2580         fill_default_options(td, options);
2581 }
2582
2583 int fio_show_option_help(const char *opt)
2584 {
2585         return show_cmd_help(options, opt);
2586 }
2587
2588 void options_mem_dupe(void *data, struct fio_option *options)
2589 {
2590         struct fio_option *o;
2591         char **ptr;
2592
2593         for (o = &options[0]; o->name; o++) {
2594                 if (o->type != FIO_OPT_STR_STORE)
2595                         continue;
2596
2597                 ptr = td_var(data, o->off1);
2598                 if (*ptr)
2599                         *ptr = strdup(*ptr);
2600         }
2601 }
2602
2603 /*
2604  * dupe FIO_OPT_STR_STORE options
2605  */
2606 void fio_options_mem_dupe(struct thread_data *td)
2607 {
2608         options_mem_dupe(&td->o, options);
2609
2610         if (td->eo && td->io_ops) {
2611                 void *oldeo = td->eo;
2612
2613                 td->eo = malloc(td->io_ops->option_struct_size);
2614                 memcpy(td->eo, oldeo, td->io_ops->option_struct_size);
2615                 options_mem_dupe(td->eo, td->io_ops->options);
2616         }
2617 }
2618
2619 unsigned int fio_get_kb_base(void *data)
2620 {
2621         struct thread_data *td = data;
2622         unsigned int kb_base = 0;
2623
2624         if (td)
2625                 kb_base = td->o.kb_base;
2626         if (!kb_base)
2627                 kb_base = 1024;
2628
2629         return kb_base;
2630 }
2631
2632 int add_option(struct fio_option *o)
2633 {
2634         struct fio_option *__o;
2635         int opt_index = 0;
2636
2637         __o = options;
2638         while (__o->name) {
2639                 opt_index++;
2640                 __o++;
2641         }
2642
2643         memcpy(&options[opt_index], o, sizeof(*o));
2644         return 0;
2645 }
2646
2647 void invalidate_profile_options(const char *prof_name)
2648 {
2649         struct fio_option *o;
2650
2651         o = options;
2652         while (o->name) {
2653                 if (o->prof_name && !strcmp(o->prof_name, prof_name)) {
2654                         o->type = FIO_OPT_INVALID;
2655                         o->prof_name = NULL;
2656                 }
2657                 o++;
2658         }
2659 }
2660
2661 void add_opt_posval(const char *optname, const char *ival, const char *help)
2662 {
2663         struct fio_option *o;
2664         unsigned int i;
2665
2666         o = find_option(options, optname);
2667         if (!o)
2668                 return;
2669
2670         for (i = 0; i < PARSE_MAX_VP; i++) {
2671                 if (o->posval[i].ival)
2672                         continue;
2673
2674                 o->posval[i].ival = ival;
2675                 o->posval[i].help = help;
2676                 break;
2677         }
2678 }
2679
2680 void del_opt_posval(const char *optname, const char *ival)
2681 {
2682         struct fio_option *o;
2683         unsigned int i;
2684
2685         o = find_option(options, optname);
2686         if (!o)
2687                 return;
2688
2689         for (i = 0; i < PARSE_MAX_VP; i++) {
2690                 if (!o->posval[i].ival)
2691                         continue;
2692                 if (strcmp(o->posval[i].ival, ival))
2693                         continue;
2694
2695                 o->posval[i].ival = NULL;
2696                 o->posval[i].help = NULL;
2697         }
2698 }
2699
2700 void fio_options_free(struct thread_data *td)
2701 {
2702         options_free(options, td);
2703         if (td->eo && td->io_ops && td->io_ops->options) {
2704                 options_free(td->io_ops->options, td->eo);
2705                 free(td->eo);
2706                 td->eo = NULL;
2707         }
2708 }