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