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