options: make unit_base be posval[] based
[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   = "unit_base",
1153                 .type   = FIO_OPT_INT,
1154                 .off1   = td_var_offset(unit_base),
1155                 .prio   = 1,
1156                 .posval = {
1157                           { .ival = "0",
1158                             .oval = 0,
1159                             .help = "Auto-detect",
1160                           },
1161                           { .ival = "8",
1162                             .oval = 8,
1163                             .help = "Normal (byte based)",
1164                           },
1165                           { .ival = "1",
1166                             .oval = 1,
1167                             .help = "Bit based",
1168                           },
1169                 },
1170                 .help   = "Bit multiple of result summary data (8 for byte, 1 for bit)",
1171         },
1172         {
1173                 .name   = "lockfile",
1174                 .type   = FIO_OPT_STR,
1175                 .off1   = td_var_offset(file_lock_mode),
1176                 .help   = "Lock file when doing IO to it",
1177                 .parent = "filename",
1178                 .def    = "none",
1179                 .posval = {
1180                           { .ival = "none",
1181                             .oval = FILE_LOCK_NONE,
1182                             .help = "No file locking",
1183                           },
1184                           { .ival = "exclusive",
1185                             .oval = FILE_LOCK_EXCLUSIVE,
1186                             .help = "Exclusive file lock",
1187                           },
1188                           {
1189                             .ival = "readwrite",
1190                             .oval = FILE_LOCK_READWRITE,
1191                             .help = "Read vs write lock",
1192                           },
1193                 },
1194         },
1195         {
1196                 .name   = "opendir",
1197                 .type   = FIO_OPT_STR_STORE,
1198                 .off1   = td_var_offset(opendir),
1199                 .cb     = str_opendir_cb,
1200                 .help   = "Recursively add files from this directory and down",
1201         },
1202         {
1203                 .name   = "rw",
1204                 .alias  = "readwrite",
1205                 .type   = FIO_OPT_STR,
1206                 .cb     = str_rw_cb,
1207                 .off1   = td_var_offset(td_ddir),
1208                 .help   = "IO direction",
1209                 .def    = "read",
1210                 .verify = rw_verify,
1211                 .posval = {
1212                           { .ival = "read",
1213                             .oval = TD_DDIR_READ,
1214                             .help = "Sequential read",
1215                           },
1216                           { .ival = "write",
1217                             .oval = TD_DDIR_WRITE,
1218                             .help = "Sequential write",
1219                           },
1220                           { .ival = "trim",
1221                             .oval = TD_DDIR_TRIM,
1222                             .help = "Sequential trim",
1223                           },
1224                           { .ival = "randread",
1225                             .oval = TD_DDIR_RANDREAD,
1226                             .help = "Random read",
1227                           },
1228                           { .ival = "randwrite",
1229                             .oval = TD_DDIR_RANDWRITE,
1230                             .help = "Random write",
1231                           },
1232                           { .ival = "randtrim",
1233                             .oval = TD_DDIR_RANDTRIM,
1234                             .help = "Random trim",
1235                           },
1236                           { .ival = "rw",
1237                             .oval = TD_DDIR_RW,
1238                             .help = "Sequential read and write mix",
1239                           },
1240                           { .ival = "readwrite",
1241                             .oval = TD_DDIR_RW,
1242                             .help = "Sequential read and write mix",
1243                           },
1244                           { .ival = "randrw",
1245                             .oval = TD_DDIR_RANDRW,
1246                             .help = "Random read and write mix"
1247                           },
1248                 },
1249         },
1250         {
1251                 .name   = "rw_sequencer",
1252                 .type   = FIO_OPT_STR,
1253                 .off1   = td_var_offset(rw_seq),
1254                 .help   = "IO offset generator modifier",
1255                 .def    = "sequential",
1256                 .posval = {
1257                           { .ival = "sequential",
1258                             .oval = RW_SEQ_SEQ,
1259                             .help = "Generate sequential offsets",
1260                           },
1261                           { .ival = "identical",
1262                             .oval = RW_SEQ_IDENT,
1263                             .help = "Generate identical offsets",
1264                           },
1265                 },
1266         },
1267
1268         {
1269                 .name   = "ioengine",
1270                 .type   = FIO_OPT_STR_STORE,
1271                 .off1   = td_var_offset(ioengine),
1272                 .help   = "IO engine to use",
1273                 .def    = FIO_PREFERRED_ENGINE,
1274                 .posval = {
1275                           { .ival = "sync",
1276                             .help = "Use read/write",
1277                           },
1278                           { .ival = "psync",
1279                             .help = "Use pread/pwrite",
1280                           },
1281                           { .ival = "vsync",
1282                             .help = "Use readv/writev",
1283                           },
1284 #ifdef CONFIG_LIBAIO
1285                           { .ival = "libaio",
1286                             .help = "Linux native asynchronous IO",
1287                           },
1288 #endif
1289 #ifdef CONFIG_POSIXAIO
1290                           { .ival = "posixaio",
1291                             .help = "POSIX asynchronous IO",
1292                           },
1293 #endif
1294 #ifdef CONFIG_SOLARISAIO
1295                           { .ival = "solarisaio",
1296                             .help = "Solaris native asynchronous IO",
1297                           },
1298 #endif
1299 #ifdef CONFIG_WINDOWSAIO
1300                           { .ival = "windowsaio",
1301                             .help = "Windows native asynchronous IO"
1302                           },
1303 #endif
1304                           { .ival = "mmap",
1305                             .help = "Memory mapped IO"
1306                           },
1307 #ifdef CONFIG_LINUX_SPLICE
1308                           { .ival = "splice",
1309                             .help = "splice/vmsplice based IO",
1310                           },
1311                           { .ival = "netsplice",
1312                             .help = "splice/vmsplice to/from the network",
1313                           },
1314 #endif
1315 #ifdef FIO_HAVE_SGIO
1316                           { .ival = "sg",
1317                             .help = "SCSI generic v3 IO",
1318                           },
1319 #endif
1320                           { .ival = "null",
1321                             .help = "Testing engine (no data transfer)",
1322                           },
1323                           { .ival = "net",
1324                             .help = "Network IO",
1325                           },
1326                           { .ival = "cpuio",
1327                             .help = "CPU cycle burner engine",
1328                           },
1329 #ifdef CONFIG_GUASI
1330                           { .ival = "guasi",
1331                             .help = "GUASI IO engine",
1332                           },
1333 #endif
1334 #ifdef FIO_HAVE_BINJECT
1335                           { .ival = "binject",
1336                             .help = "binject direct inject block engine",
1337                           },
1338 #endif
1339 #ifdef CONFIG_RDMA
1340                           { .ival = "rdma",
1341                             .help = "RDMA IO engine",
1342                           },
1343 #endif
1344 #ifdef CONFIG_FUSION_AW
1345                           { .ival = "fusion-aw-sync",
1346                             .help = "Fusion-io atomic write engine",
1347                           },
1348 #endif
1349 #ifdef CONFIG_LINUX_EXT4_MOVE_EXTENT
1350                           { .ival = "e4defrag",
1351                             .help = "ext4 defrag engine",
1352                           },
1353 #endif
1354 #ifdef CONFIG_LINUX_FALLOCATE
1355                           { .ival = "falloc",
1356                             .help = "fallocate() file based engine",
1357                           },
1358 #endif
1359                           { .ival = "external",
1360                             .help = "Load external engine (append name)",
1361                           },
1362                 },
1363         },
1364         {
1365                 .name   = "iodepth",
1366                 .type   = FIO_OPT_INT,
1367                 .off1   = td_var_offset(iodepth),
1368                 .help   = "Number of IO buffers to keep in flight",
1369                 .minval = 1,
1370                 .def    = "1",
1371         },
1372         {
1373                 .name   = "iodepth_batch",
1374                 .alias  = "iodepth_batch_submit",
1375                 .type   = FIO_OPT_INT,
1376                 .off1   = td_var_offset(iodepth_batch),
1377                 .help   = "Number of IO buffers to submit in one go",
1378                 .parent = "iodepth",
1379                 .minval = 1,
1380                 .def    = "1",
1381         },
1382         {
1383                 .name   = "iodepth_batch_complete",
1384                 .type   = FIO_OPT_INT,
1385                 .off1   = td_var_offset(iodepth_batch_complete),
1386                 .help   = "Number of IO buffers to retrieve in one go",
1387                 .parent = "iodepth",
1388                 .minval = 0,
1389                 .def    = "1",
1390         },
1391         {
1392                 .name   = "iodepth_low",
1393                 .type   = FIO_OPT_INT,
1394                 .off1   = td_var_offset(iodepth_low),
1395                 .help   = "Low water mark for queuing depth",
1396                 .parent = "iodepth",
1397         },
1398         {
1399                 .name   = "size",
1400                 .type   = FIO_OPT_STR_VAL,
1401                 .cb     = str_size_cb,
1402                 .help   = "Total size of device or files",
1403         },
1404         {
1405                 .name   = "fill_device",
1406                 .alias  = "fill_fs",
1407                 .type   = FIO_OPT_BOOL,
1408                 .off1   = td_var_offset(fill_device),
1409                 .help   = "Write until an ENOSPC error occurs",
1410                 .def    = "0",
1411         },
1412         {
1413                 .name   = "filesize",
1414                 .type   = FIO_OPT_STR_VAL,
1415                 .off1   = td_var_offset(file_size_low),
1416                 .off2   = td_var_offset(file_size_high),
1417                 .minval = 1,
1418                 .help   = "Size of individual files",
1419         },
1420         {
1421                 .name   = "offset",
1422                 .alias  = "fileoffset",
1423                 .type   = FIO_OPT_STR_VAL,
1424                 .off1   = td_var_offset(start_offset),
1425                 .help   = "Start IO from this offset",
1426                 .def    = "0",
1427         },
1428         {
1429                 .name   = "offset_increment",
1430                 .type   = FIO_OPT_STR_VAL,
1431                 .off1   = td_var_offset(offset_increment),
1432                 .help   = "What is the increment from one offset to the next",
1433                 .parent = "offset",
1434                 .def    = "0",
1435         },
1436         {
1437                 .name   = "bs",
1438                 .alias  = "blocksize",
1439                 .type   = FIO_OPT_INT,
1440                 .off1   = td_var_offset(bs[DDIR_READ]),
1441                 .off2   = td_var_offset(bs[DDIR_WRITE]),
1442                 .off3   = td_var_offset(bs[DDIR_TRIM]),
1443                 .minval = 1,
1444                 .help   = "Block size unit",
1445                 .def    = "4k",
1446                 .parent = "rw",
1447         },
1448         {
1449                 .name   = "ba",
1450                 .alias  = "blockalign",
1451                 .type   = FIO_OPT_INT,
1452                 .off1   = td_var_offset(ba[DDIR_READ]),
1453                 .off2   = td_var_offset(ba[DDIR_WRITE]),
1454                 .off3   = td_var_offset(ba[DDIR_TRIM]),
1455                 .minval = 1,
1456                 .help   = "IO block offset alignment",
1457                 .parent = "rw",
1458         },
1459         {
1460                 .name   = "bsrange",
1461                 .alias  = "blocksize_range",
1462                 .type   = FIO_OPT_RANGE,
1463                 .off1   = td_var_offset(min_bs[DDIR_READ]),
1464                 .off2   = td_var_offset(max_bs[DDIR_READ]),
1465                 .off3   = td_var_offset(min_bs[DDIR_WRITE]),
1466                 .off4   = td_var_offset(max_bs[DDIR_WRITE]),
1467                 .off5   = td_var_offset(min_bs[DDIR_TRIM]),
1468                 .off6   = td_var_offset(max_bs[DDIR_TRIM]),
1469                 .minval = 1,
1470                 .help   = "Set block size range (in more detail than bs)",
1471                 .parent = "rw",
1472         },
1473         {
1474                 .name   = "bssplit",
1475                 .type   = FIO_OPT_STR,
1476                 .cb     = str_bssplit_cb,
1477                 .help   = "Set a specific mix of block sizes",
1478                 .parent = "rw",
1479         },
1480         {
1481                 .name   = "bs_unaligned",
1482                 .alias  = "blocksize_unaligned",
1483                 .type   = FIO_OPT_STR_SET,
1484                 .off1   = td_var_offset(bs_unaligned),
1485                 .help   = "Don't sector align IO buffer sizes",
1486                 .parent = "rw",
1487         },
1488         {
1489                 .name   = "randrepeat",
1490                 .type   = FIO_OPT_BOOL,
1491                 .off1   = td_var_offset(rand_repeatable),
1492                 .help   = "Use repeatable random IO pattern",
1493                 .def    = "1",
1494                 .parent = "rw",
1495         },
1496         {
1497                 .name   = "use_os_rand",
1498                 .type   = FIO_OPT_BOOL,
1499                 .off1   = td_var_offset(use_os_rand),
1500                 .help   = "Set to use OS random generator",
1501                 .def    = "0",
1502                 .parent = "rw",
1503         },
1504         {
1505                 .name   = "norandommap",
1506                 .type   = FIO_OPT_STR_SET,
1507                 .off1   = td_var_offset(norandommap),
1508                 .help   = "Accept potential duplicate random blocks",
1509                 .parent = "rw",
1510         },
1511         {
1512                 .name   = "softrandommap",
1513                 .type   = FIO_OPT_BOOL,
1514                 .off1   = td_var_offset(softrandommap),
1515                 .help   = "Set norandommap if randommap allocation fails",
1516                 .parent = "norandommap",
1517                 .def    = "0",
1518         },
1519         {
1520                 .name   = "random_generator",
1521                 .type   = FIO_OPT_STR,
1522                 .off1   = td_var_offset(random_generator),
1523                 .help   = "Type of random number generator to use",
1524                 .def    = "tausworthe",
1525                 .posval = {
1526                           { .ival = "tausworthe",
1527                             .oval = FIO_RAND_GEN_TAUSWORTHE,
1528                             .help = "Strong Tausworthe generator",
1529                           },
1530                           { .ival = "lfsr",
1531                             .oval = FIO_RAND_GEN_LFSR,
1532                             .help = "Variable length LFSR",
1533                           },
1534                 },
1535         },
1536         {
1537                 .name   = "random_distribution",
1538                 .type   = FIO_OPT_STR,
1539                 .off1   = td_var_offset(random_distribution),
1540                 .cb     = str_random_distribution_cb,
1541                 .help   = "Random offset distribution generator",
1542                 .def    = "random",
1543                 .posval = {
1544                           { .ival = "random",
1545                             .oval = FIO_RAND_DIST_RANDOM,
1546                             .help = "Completely random",
1547                           },
1548                           { .ival = "zipf",
1549                             .oval = FIO_RAND_DIST_ZIPF,
1550                             .help = "Zipf distribution",
1551                           },
1552                           { .ival = "pareto",
1553                             .oval = FIO_RAND_DIST_PARETO,
1554                             .help = "Pareto distribution",
1555                           },
1556                 },
1557         },
1558         {
1559                 .name   = "nrfiles",
1560                 .alias  = "nr_files",
1561                 .type   = FIO_OPT_INT,
1562                 .off1   = td_var_offset(nr_files),
1563                 .help   = "Split job workload between this number of files",
1564                 .def    = "1",
1565         },
1566         {
1567                 .name   = "openfiles",
1568                 .type   = FIO_OPT_INT,
1569                 .off1   = td_var_offset(open_files),
1570                 .help   = "Number of files to keep open at the same time",
1571         },
1572         {
1573                 .name   = "file_service_type",
1574                 .type   = FIO_OPT_STR,
1575                 .cb     = str_fst_cb,
1576                 .off1   = td_var_offset(file_service_type),
1577                 .help   = "How to select which file to service next",
1578                 .def    = "roundrobin",
1579                 .posval = {
1580                           { .ival = "random",
1581                             .oval = FIO_FSERVICE_RANDOM,
1582                             .help = "Choose a file at random",
1583                           },
1584                           { .ival = "roundrobin",
1585                             .oval = FIO_FSERVICE_RR,
1586                             .help = "Round robin select files",
1587                           },
1588                           { .ival = "sequential",
1589                             .oval = FIO_FSERVICE_SEQ,
1590                             .help = "Finish one file before moving to the next",
1591                           },
1592                 },
1593                 .parent = "nrfiles",
1594         },
1595 #ifdef CONFIG_POSIX_FALLOCATE
1596         {
1597                 .name   = "fallocate",
1598                 .type   = FIO_OPT_STR,
1599                 .off1   = td_var_offset(fallocate_mode),
1600                 .help   = "Whether pre-allocation is performed when laying out files",
1601                 .def    = "posix",
1602                 .posval = {
1603                           { .ival = "none",
1604                             .oval = FIO_FALLOCATE_NONE,
1605                             .help = "Do not pre-allocate space",
1606                           },
1607                           { .ival = "posix",
1608                             .oval = FIO_FALLOCATE_POSIX,
1609                             .help = "Use posix_fallocate()",
1610                           },
1611 #ifdef CONFIG_LINUX_FALLOCATE
1612                           { .ival = "keep",
1613                             .oval = FIO_FALLOCATE_KEEP_SIZE,
1614                             .help = "Use fallocate(..., FALLOC_FL_KEEP_SIZE, ...)",
1615                           },
1616 #endif
1617                           /* Compatibility with former boolean values */
1618                           { .ival = "0",
1619                             .oval = FIO_FALLOCATE_NONE,
1620                             .help = "Alias for 'none'",
1621                           },
1622                           { .ival = "1",
1623                             .oval = FIO_FALLOCATE_POSIX,
1624                             .help = "Alias for 'posix'",
1625                           },
1626                 },
1627         },
1628 #endif  /* CONFIG_POSIX_FALLOCATE */
1629         {
1630                 .name   = "fadvise_hint",
1631                 .type   = FIO_OPT_BOOL,
1632                 .off1   = td_var_offset(fadvise_hint),
1633                 .help   = "Use fadvise() to advise the kernel on IO pattern",
1634                 .def    = "1",
1635         },
1636         {
1637                 .name   = "fsync",
1638                 .type   = FIO_OPT_INT,
1639                 .off1   = td_var_offset(fsync_blocks),
1640                 .help   = "Issue fsync for writes every given number of blocks",
1641                 .def    = "0",
1642         },
1643         {
1644                 .name   = "fdatasync",
1645                 .type   = FIO_OPT_INT,
1646                 .off1   = td_var_offset(fdatasync_blocks),
1647                 .help   = "Issue fdatasync for writes every given number of blocks",
1648                 .def    = "0",
1649         },
1650         {
1651                 .name   = "write_barrier",
1652                 .type   = FIO_OPT_INT,
1653                 .off1   = td_var_offset(barrier_blocks),
1654                 .help   = "Make every Nth write a barrier write",
1655                 .def    = "0",
1656         },
1657 #ifdef CONFIG_SYNC_FILE_RANGE
1658         {
1659                 .name   = "sync_file_range",
1660                 .posval = {
1661                           { .ival = "wait_before",
1662                             .oval = SYNC_FILE_RANGE_WAIT_BEFORE,
1663                             .help = "SYNC_FILE_RANGE_WAIT_BEFORE",
1664                             .or   = 1,
1665                           },
1666                           { .ival = "write",
1667                             .oval = SYNC_FILE_RANGE_WRITE,
1668                             .help = "SYNC_FILE_RANGE_WRITE",
1669                             .or   = 1,
1670                           },
1671                           {
1672                             .ival = "wait_after",
1673                             .oval = SYNC_FILE_RANGE_WAIT_AFTER,
1674                             .help = "SYNC_FILE_RANGE_WAIT_AFTER",
1675                             .or   = 1,
1676                           },
1677                 },
1678                 .type   = FIO_OPT_STR_MULTI,
1679                 .cb     = str_sfr_cb,
1680                 .off1   = td_var_offset(sync_file_range),
1681                 .help   = "Use sync_file_range()",
1682         },
1683 #endif
1684         {
1685                 .name   = "direct",
1686                 .type   = FIO_OPT_BOOL,
1687                 .off1   = td_var_offset(odirect),
1688                 .help   = "Use O_DIRECT IO (negates buffered)",
1689                 .def    = "0",
1690         },
1691         {
1692                 .name   = "buffered",
1693                 .type   = FIO_OPT_BOOL,
1694                 .off1   = td_var_offset(odirect),
1695                 .neg    = 1,
1696                 .help   = "Use buffered IO (negates direct)",
1697                 .def    = "1",
1698         },
1699         {
1700                 .name   = "overwrite",
1701                 .type   = FIO_OPT_BOOL,
1702                 .off1   = td_var_offset(overwrite),
1703                 .help   = "When writing, set whether to overwrite current data",
1704                 .def    = "0",
1705         },
1706         {
1707                 .name   = "loops",
1708                 .type   = FIO_OPT_INT,
1709                 .off1   = td_var_offset(loops),
1710                 .help   = "Number of times to run the job",
1711                 .def    = "1",
1712         },
1713         {
1714                 .name   = "numjobs",
1715                 .type   = FIO_OPT_INT,
1716                 .off1   = td_var_offset(numjobs),
1717                 .help   = "Duplicate this job this many times",
1718                 .def    = "1",
1719         },
1720         {
1721                 .name   = "startdelay",
1722                 .type   = FIO_OPT_STR_VAL_TIME,
1723                 .off1   = td_var_offset(start_delay),
1724                 .help   = "Only start job when this period has passed",
1725                 .def    = "0",
1726         },
1727         {
1728                 .name   = "runtime",
1729                 .alias  = "timeout",
1730                 .type   = FIO_OPT_STR_VAL_TIME,
1731                 .off1   = td_var_offset(timeout),
1732                 .help   = "Stop workload when this amount of time has passed",
1733                 .def    = "0",
1734         },
1735         {
1736                 .name   = "time_based",
1737                 .type   = FIO_OPT_STR_SET,
1738                 .off1   = td_var_offset(time_based),
1739                 .help   = "Keep running until runtime/timeout is met",
1740         },
1741         {
1742                 .name   = "ramp_time",
1743                 .type   = FIO_OPT_STR_VAL_TIME,
1744                 .off1   = td_var_offset(ramp_time),
1745                 .help   = "Ramp up time before measuring performance",
1746         },
1747         {
1748                 .name   = "clocksource",
1749                 .type   = FIO_OPT_STR,
1750                 .cb     = fio_clock_source_cb,
1751                 .off1   = td_var_offset(clocksource),
1752                 .help   = "What type of timing source to use",
1753                 .posval = {
1754 #ifdef CONFIG_GETTIMEOFDAY
1755                           { .ival = "gettimeofday",
1756                             .oval = CS_GTOD,
1757                             .help = "Use gettimeofday(2) for timing",
1758                           },
1759 #endif
1760 #ifdef CONFIG_CLOCK_GETTIME
1761                           { .ival = "clock_gettime",
1762                             .oval = CS_CGETTIME,
1763                             .help = "Use clock_gettime(2) for timing",
1764                           },
1765 #endif
1766 #ifdef ARCH_HAVE_CPU_CLOCK
1767                           { .ival = "cpu",
1768                             .oval = CS_CPUCLOCK,
1769                             .help = "Use CPU private clock",
1770                           },
1771 #endif
1772                 },
1773         },
1774         {
1775                 .name   = "mem",
1776                 .alias  = "iomem",
1777                 .type   = FIO_OPT_STR,
1778                 .cb     = str_mem_cb,
1779                 .off1   = td_var_offset(mem_type),
1780                 .help   = "Backing type for IO buffers",
1781                 .def    = "malloc",
1782                 .posval = {
1783                           { .ival = "malloc",
1784                             .oval = MEM_MALLOC,
1785                             .help = "Use malloc(3) for IO buffers",
1786                           },
1787                           { .ival = "shm",
1788                             .oval = MEM_SHM,
1789                             .help = "Use shared memory segments for IO buffers",
1790                           },
1791 #ifdef FIO_HAVE_HUGETLB
1792                           { .ival = "shmhuge",
1793                             .oval = MEM_SHMHUGE,
1794                             .help = "Like shm, but use huge pages",
1795                           },
1796 #endif
1797                           { .ival = "mmap",
1798                             .oval = MEM_MMAP,
1799                             .help = "Use mmap(2) (file or anon) for IO buffers",
1800                           },
1801 #ifdef FIO_HAVE_HUGETLB
1802                           { .ival = "mmaphuge",
1803                             .oval = MEM_MMAPHUGE,
1804                             .help = "Like mmap, but use huge pages",
1805                           },
1806 #endif
1807                   },
1808         },
1809         {
1810                 .name   = "iomem_align",
1811                 .alias  = "mem_align",
1812                 .type   = FIO_OPT_INT,
1813                 .off1   = td_var_offset(mem_align),
1814                 .minval = 0,
1815                 .help   = "IO memory buffer offset alignment",
1816                 .def    = "0",
1817                 .parent = "iomem",
1818         },
1819         {
1820                 .name   = "verify",
1821                 .type   = FIO_OPT_STR,
1822                 .off1   = td_var_offset(verify),
1823                 .help   = "Verify data written",
1824                 .cb     = str_verify_cb,
1825                 .def    = "0",
1826                 .posval = {
1827                           { .ival = "0",
1828                             .oval = VERIFY_NONE,
1829                             .help = "Don't do IO verification",
1830                           },
1831                           { .ival = "md5",
1832                             .oval = VERIFY_MD5,
1833                             .help = "Use md5 checksums for verification",
1834                           },
1835                           { .ival = "crc64",
1836                             .oval = VERIFY_CRC64,
1837                             .help = "Use crc64 checksums for verification",
1838                           },
1839                           { .ival = "crc32",
1840                             .oval = VERIFY_CRC32,
1841                             .help = "Use crc32 checksums for verification",
1842                           },
1843                           { .ival = "crc32c-intel",
1844                             .oval = VERIFY_CRC32C,
1845                             .help = "Use crc32c checksums for verification (hw assisted, if available)",
1846                           },
1847                           { .ival = "crc32c",
1848                             .oval = VERIFY_CRC32C,
1849                             .help = "Use crc32c checksums for verification (hw assisted, if available)",
1850                           },
1851                           { .ival = "crc16",
1852                             .oval = VERIFY_CRC16,
1853                             .help = "Use crc16 checksums for verification",
1854                           },
1855                           { .ival = "crc7",
1856                             .oval = VERIFY_CRC7,
1857                             .help = "Use crc7 checksums for verification",
1858                           },
1859                           { .ival = "sha1",
1860                             .oval = VERIFY_SHA1,
1861                             .help = "Use sha1 checksums for verification",
1862                           },
1863                           { .ival = "sha256",
1864                             .oval = VERIFY_SHA256,
1865                             .help = "Use sha256 checksums for verification",
1866                           },
1867                           { .ival = "sha512",
1868                             .oval = VERIFY_SHA512,
1869                             .help = "Use sha512 checksums for verification",
1870                           },
1871                           { .ival = "meta",
1872                             .oval = VERIFY_META,
1873                             .help = "Use io information",
1874                           },
1875                           {
1876                             .ival = "null",
1877                             .oval = VERIFY_NULL,
1878                             .help = "Pretend to verify",
1879                           },
1880                 },
1881         },
1882         {
1883                 .name   = "do_verify",
1884                 .type   = FIO_OPT_BOOL,
1885                 .off1   = td_var_offset(do_verify),
1886                 .help   = "Run verification stage after write",
1887                 .def    = "1",
1888                 .parent = "verify",
1889         },
1890         {
1891                 .name   = "verifysort",
1892                 .type   = FIO_OPT_BOOL,
1893                 .off1   = td_var_offset(verifysort),
1894                 .help   = "Sort written verify blocks for read back",
1895                 .def    = "1",
1896                 .parent = "verify",
1897         },
1898         {
1899                 .name   = "verifysort_nr",
1900                 .type   = FIO_OPT_INT,
1901                 .off1   = td_var_offset(verifysort_nr),
1902                 .help   = "Pre-load and sort verify blocks for a read workload",
1903                 .minval = 0,
1904                 .maxval = 131072,
1905                 .def    = "1024",
1906                 .parent = "verify",
1907         },
1908         {
1909                 .name   = "verify_interval",
1910                 .type   = FIO_OPT_INT,
1911                 .off1   = td_var_offset(verify_interval),
1912                 .minval = 2 * sizeof(struct verify_header),
1913                 .help   = "Store verify buffer header every N bytes",
1914                 .parent = "verify",
1915         },
1916         {
1917                 .name   = "verify_offset",
1918                 .type   = FIO_OPT_INT,
1919                 .help   = "Offset verify header location by N bytes",
1920                 .def    = "0",
1921                 .cb     = str_verify_offset_cb,
1922                 .parent = "verify",
1923         },
1924         {
1925                 .name   = "verify_pattern",
1926                 .type   = FIO_OPT_STR,
1927                 .cb     = str_verify_pattern_cb,
1928                 .help   = "Fill pattern for IO buffers",
1929                 .parent = "verify",
1930         },
1931         {
1932                 .name   = "verify_fatal",
1933                 .type   = FIO_OPT_BOOL,
1934                 .off1   = td_var_offset(verify_fatal),
1935                 .def    = "0",
1936                 .help   = "Exit on a single verify failure, don't continue",
1937                 .parent = "verify",
1938         },
1939         {
1940                 .name   = "verify_dump",
1941                 .type   = FIO_OPT_BOOL,
1942                 .off1   = td_var_offset(verify_dump),
1943                 .def    = "0",
1944                 .help   = "Dump contents of good and bad blocks on failure",
1945                 .parent = "verify",
1946         },
1947         {
1948                 .name   = "verify_async",
1949                 .type   = FIO_OPT_INT,
1950                 .off1   = td_var_offset(verify_async),
1951                 .def    = "0",
1952                 .help   = "Number of async verifier threads to use",
1953                 .parent = "verify",
1954         },
1955         {
1956                 .name   = "verify_backlog",
1957                 .type   = FIO_OPT_STR_VAL,
1958                 .off1   = td_var_offset(verify_backlog),
1959                 .help   = "Verify after this number of blocks are written",
1960                 .parent = "verify",
1961         },
1962         {
1963                 .name   = "verify_backlog_batch",
1964                 .type   = FIO_OPT_INT,
1965                 .off1   = td_var_offset(verify_batch),
1966                 .help   = "Verify this number of IO blocks",
1967                 .parent = "verify",
1968         },
1969 #ifdef FIO_HAVE_CPU_AFFINITY
1970         {
1971                 .name   = "verify_async_cpus",
1972                 .type   = FIO_OPT_STR,
1973                 .cb     = str_verify_cpus_allowed_cb,
1974                 .help   = "Set CPUs allowed for async verify threads",
1975                 .parent = "verify_async",
1976         },
1977 #endif
1978         {
1979                 .name   = "experimental_verify",
1980                 .off1   = td_var_offset(experimental_verify),
1981                 .type   = FIO_OPT_BOOL,
1982                 .help   = "Enable experimental verification",
1983         },
1984 #ifdef FIO_HAVE_TRIM
1985         {
1986                 .name   = "trim_percentage",
1987                 .type   = FIO_OPT_INT,
1988                 .cb     = str_verify_trim_cb,
1989                 .maxval = 100,
1990                 .help   = "Number of verify blocks to discard/trim",
1991                 .parent = "verify",
1992                 .def    = "0",
1993         },
1994         {
1995                 .name   = "trim_verify_zero",
1996                 .type   = FIO_OPT_INT,
1997                 .help   = "Verify that trim/discarded blocks are returned as zeroes",
1998                 .off1   = td_var_offset(trim_zero),
1999                 .parent = "trim_percentage",
2000                 .def    = "1",
2001         },
2002         {
2003                 .name   = "trim_backlog",
2004                 .type   = FIO_OPT_STR_VAL,
2005                 .off1   = td_var_offset(trim_backlog),
2006                 .help   = "Trim after this number of blocks are written",
2007                 .parent = "trim_percentage",
2008         },
2009         {
2010                 .name   = "trim_backlog_batch",
2011                 .type   = FIO_OPT_INT,
2012                 .off1   = td_var_offset(trim_batch),
2013                 .help   = "Trim this number of IO blocks",
2014                 .parent = "trim_percentage",
2015         },
2016 #endif
2017         {
2018                 .name   = "write_iolog",
2019                 .type   = FIO_OPT_STR_STORE,
2020                 .off1   = td_var_offset(write_iolog_file),
2021                 .help   = "Store IO pattern to file",
2022         },
2023         {
2024                 .name   = "read_iolog",
2025                 .type   = FIO_OPT_STR_STORE,
2026                 .off1   = td_var_offset(read_iolog_file),
2027                 .help   = "Playback IO pattern from file",
2028         },
2029         {
2030                 .name   = "replay_no_stall",
2031                 .type   = FIO_OPT_INT,
2032                 .off1   = td_var_offset(no_stall),
2033                 .def    = "0",
2034                 .parent = "read_iolog",
2035                 .help   = "Playback IO pattern file as fast as possible without stalls",
2036         },
2037         {
2038                 .name   = "replay_redirect",
2039                 .type   = FIO_OPT_STR_STORE,
2040                 .off1   = td_var_offset(replay_redirect),
2041                 .parent = "read_iolog",
2042                 .help   = "Replay all I/O onto this device, regardless of trace device",
2043         },
2044         {
2045                 .name   = "exec_prerun",
2046                 .type   = FIO_OPT_STR_STORE,
2047                 .off1   = td_var_offset(exec_prerun),
2048                 .help   = "Execute this file prior to running job",
2049         },
2050         {
2051                 .name   = "exec_postrun",
2052                 .type   = FIO_OPT_STR_STORE,
2053                 .off1   = td_var_offset(exec_postrun),
2054                 .help   = "Execute this file after running job",
2055         },
2056 #ifdef FIO_HAVE_IOSCHED_SWITCH
2057         {
2058                 .name   = "ioscheduler",
2059                 .type   = FIO_OPT_STR_STORE,
2060                 .off1   = td_var_offset(ioscheduler),
2061                 .help   = "Use this IO scheduler on the backing device",
2062         },
2063 #endif
2064         {
2065                 .name   = "zonesize",
2066                 .type   = FIO_OPT_STR_VAL,
2067                 .off1   = td_var_offset(zone_size),
2068                 .help   = "Amount of data to read per zone",
2069                 .def    = "0",
2070         },
2071         {
2072                 .name   = "zonerange",
2073                 .type   = FIO_OPT_STR_VAL,
2074                 .off1   = td_var_offset(zone_range),
2075                 .help   = "Give size of an IO zone",
2076                 .def    = "0",
2077         },
2078         {
2079                 .name   = "zoneskip",
2080                 .type   = FIO_OPT_STR_VAL,
2081                 .off1   = td_var_offset(zone_skip),
2082                 .help   = "Space between IO zones",
2083                 .def    = "0",
2084         },
2085         {
2086                 .name   = "lockmem",
2087                 .type   = FIO_OPT_STR_VAL,
2088                 .cb     = str_lockmem_cb,
2089                 .help   = "Lock down this amount of memory",
2090                 .def    = "0",
2091         },
2092         {
2093                 .name   = "rwmixread",
2094                 .type   = FIO_OPT_INT,
2095                 .cb     = str_rwmix_read_cb,
2096                 .maxval = 100,
2097                 .help   = "Percentage of mixed workload that is reads",
2098                 .def    = "50",
2099         },
2100         {
2101                 .name   = "rwmixwrite",
2102                 .type   = FIO_OPT_INT,
2103                 .cb     = str_rwmix_write_cb,
2104                 .maxval = 100,
2105                 .help   = "Percentage of mixed workload that is writes",
2106                 .def    = "50",
2107         },
2108         {
2109                 .name   = "rwmixcycle",
2110                 .type   = FIO_OPT_DEPRECATED,
2111         },
2112         {
2113                 .name   = "nice",
2114                 .type   = FIO_OPT_INT,
2115                 .off1   = td_var_offset(nice),
2116                 .help   = "Set job CPU nice value",
2117                 .minval = -19,
2118                 .maxval = 20,
2119                 .def    = "0",
2120         },
2121 #ifdef FIO_HAVE_IOPRIO
2122         {
2123                 .name   = "prio",
2124                 .type   = FIO_OPT_INT,
2125                 .cb     = str_prio_cb,
2126                 .help   = "Set job IO priority value",
2127                 .minval = 0,
2128                 .maxval = 7,
2129         },
2130         {
2131                 .name   = "prioclass",
2132                 .type   = FIO_OPT_INT,
2133                 .cb     = str_prioclass_cb,
2134                 .help   = "Set job IO priority class",
2135                 .minval = 0,
2136                 .maxval = 3,
2137         },
2138 #endif
2139         {
2140                 .name   = "thinktime",
2141                 .type   = FIO_OPT_INT,
2142                 .off1   = td_var_offset(thinktime),
2143                 .help   = "Idle time between IO buffers (usec)",
2144                 .def    = "0",
2145         },
2146         {
2147                 .name   = "thinktime_spin",
2148                 .type   = FIO_OPT_INT,
2149                 .off1   = td_var_offset(thinktime_spin),
2150                 .help   = "Start think time by spinning this amount (usec)",
2151                 .def    = "0",
2152                 .parent = "thinktime",
2153         },
2154         {
2155                 .name   = "thinktime_blocks",
2156                 .type   = FIO_OPT_INT,
2157                 .off1   = td_var_offset(thinktime_blocks),
2158                 .help   = "IO buffer period between 'thinktime'",
2159                 .def    = "1",
2160                 .parent = "thinktime",
2161         },
2162         {
2163                 .name   = "rate",
2164                 .type   = FIO_OPT_INT,
2165                 .off1   = td_var_offset(rate[DDIR_READ]),
2166                 .off2   = td_var_offset(rate[DDIR_WRITE]),
2167                 .off3   = td_var_offset(rate[DDIR_TRIM]),
2168                 .help   = "Set bandwidth rate",
2169         },
2170         {
2171                 .name   = "ratemin",
2172                 .type   = FIO_OPT_INT,
2173                 .off1   = td_var_offset(ratemin[DDIR_READ]),
2174                 .off2   = td_var_offset(ratemin[DDIR_WRITE]),
2175                 .off3   = td_var_offset(ratemin[DDIR_TRIM]),
2176                 .help   = "Job must meet this rate or it will be shutdown",
2177                 .parent = "rate",
2178         },
2179         {
2180                 .name   = "rate_iops",
2181                 .type   = FIO_OPT_INT,
2182                 .off1   = td_var_offset(rate_iops[DDIR_READ]),
2183                 .off2   = td_var_offset(rate_iops[DDIR_WRITE]),
2184                 .off3   = td_var_offset(rate_iops[DDIR_TRIM]),
2185                 .help   = "Limit IO used to this number of IO operations/sec",
2186         },
2187         {
2188                 .name   = "rate_iops_min",
2189                 .type   = FIO_OPT_INT,
2190                 .off1   = td_var_offset(rate_iops_min[DDIR_READ]),
2191                 .off2   = td_var_offset(rate_iops_min[DDIR_WRITE]),
2192                 .off3   = td_var_offset(rate_iops_min[DDIR_TRIM]),
2193                 .help   = "Job must meet this rate or it will be shut down",
2194                 .parent = "rate_iops",
2195         },
2196         {
2197                 .name   = "ratecycle",
2198                 .type   = FIO_OPT_INT,
2199                 .off1   = td_var_offset(ratecycle),
2200                 .help   = "Window average for rate limits (msec)",
2201                 .def    = "1000",
2202                 .parent = "rate",
2203         },
2204         {
2205                 .name   = "max_latency",
2206                 .type   = FIO_OPT_INT,
2207                 .off1   = td_var_offset(max_latency),
2208                 .help   = "Maximum tolerated IO latency (usec)",
2209         },
2210         {
2211                 .name   = "invalidate",
2212                 .type   = FIO_OPT_BOOL,
2213                 .off1   = td_var_offset(invalidate_cache),
2214                 .help   = "Invalidate buffer/page cache prior to running job",
2215                 .def    = "1",
2216         },
2217         {
2218                 .name   = "sync",
2219                 .type   = FIO_OPT_BOOL,
2220                 .off1   = td_var_offset(sync_io),
2221                 .help   = "Use O_SYNC for buffered writes",
2222                 .def    = "0",
2223                 .parent = "buffered",
2224         },
2225         {
2226                 .name   = "bwavgtime",
2227                 .type   = FIO_OPT_INT,
2228                 .off1   = td_var_offset(bw_avg_time),
2229                 .help   = "Time window over which to calculate bandwidth"
2230                           " (msec)",
2231                 .def    = "500",
2232                 .parent = "write_bw_log",
2233         },
2234         {
2235                 .name   = "iopsavgtime",
2236                 .type   = FIO_OPT_INT,
2237                 .off1   = td_var_offset(iops_avg_time),
2238                 .help   = "Time window over which to calculate IOPS (msec)",
2239                 .def    = "500",
2240                 .parent = "write_iops_log",
2241         },
2242         {
2243                 .name   = "create_serialize",
2244                 .type   = FIO_OPT_BOOL,
2245                 .off1   = td_var_offset(create_serialize),
2246                 .help   = "Serialize creating of job files",
2247                 .def    = "1",
2248         },
2249         {
2250                 .name   = "create_fsync",
2251                 .type   = FIO_OPT_BOOL,
2252                 .off1   = td_var_offset(create_fsync),
2253                 .help   = "fsync file after creation",
2254                 .def    = "1",
2255         },
2256         {
2257                 .name   = "create_on_open",
2258                 .type   = FIO_OPT_BOOL,
2259                 .off1   = td_var_offset(create_on_open),
2260                 .help   = "Create files when they are opened for IO",
2261                 .def    = "0",
2262         },
2263         {
2264                 .name   = "create_only",
2265                 .type   = FIO_OPT_BOOL,
2266                 .off1   = td_var_offset(create_only),
2267                 .help   = "Only perform file creation phase",
2268                 .def    = "0",
2269         },
2270         {
2271                 .name   = "pre_read",
2272                 .type   = FIO_OPT_BOOL,
2273                 .off1   = td_var_offset(pre_read),
2274                 .help   = "Pre-read files before starting official testing",
2275                 .def    = "0",
2276         },
2277         {
2278                 .name   = "cpuload",
2279                 .type   = FIO_OPT_INT,
2280                 .off1   = td_var_offset(cpuload),
2281                 .help   = "Use this percentage of CPU",
2282         },
2283         {
2284                 .name   = "cpuchunks",
2285                 .type   = FIO_OPT_INT,
2286                 .off1   = td_var_offset(cpucycle),
2287                 .help   = "Length of the CPU burn cycles (usecs)",
2288                 .def    = "50000",
2289                 .parent = "cpuload",
2290         },
2291 #ifdef FIO_HAVE_CPU_AFFINITY
2292         {
2293                 .name   = "cpumask",
2294                 .type   = FIO_OPT_INT,
2295                 .cb     = str_cpumask_cb,
2296                 .help   = "CPU affinity mask",
2297         },
2298         {
2299                 .name   = "cpus_allowed",
2300                 .type   = FIO_OPT_STR,
2301                 .cb     = str_cpus_allowed_cb,
2302                 .help   = "Set CPUs allowed",
2303         },
2304 #endif
2305 #ifdef CONFIG_LIBNUMA
2306         {
2307                 .name   = "numa_cpu_nodes",
2308                 .type   = FIO_OPT_STR,
2309                 .cb     = str_numa_cpunodes_cb,
2310                 .help   = "NUMA CPU nodes bind",
2311         },
2312         {
2313                 .name   = "numa_mem_policy",
2314                 .type   = FIO_OPT_STR,
2315                 .cb     = str_numa_mpol_cb,
2316                 .help   = "NUMA memory policy setup",
2317         },
2318 #endif
2319         {
2320                 .name   = "end_fsync",
2321                 .type   = FIO_OPT_BOOL,
2322                 .off1   = td_var_offset(end_fsync),
2323                 .help   = "Include fsync at the end of job",
2324                 .def    = "0",
2325         },
2326         {
2327                 .name   = "fsync_on_close",
2328                 .type   = FIO_OPT_BOOL,
2329                 .off1   = td_var_offset(fsync_on_close),
2330                 .help   = "fsync files on close",
2331                 .def    = "0",
2332         },
2333         {
2334                 .name   = "unlink",
2335                 .type   = FIO_OPT_BOOL,
2336                 .off1   = td_var_offset(unlink),
2337                 .help   = "Unlink created files after job has completed",
2338                 .def    = "0",
2339         },
2340         {
2341                 .name   = "exitall",
2342                 .type   = FIO_OPT_STR_SET,
2343                 .cb     = str_exitall_cb,
2344                 .help   = "Terminate all jobs when one exits",
2345         },
2346         {
2347                 .name   = "stonewall",
2348                 .alias  = "wait_for_previous",
2349                 .type   = FIO_OPT_STR_SET,
2350                 .off1   = td_var_offset(stonewall),
2351                 .help   = "Insert a hard barrier between this job and previous",
2352         },
2353         {
2354                 .name   = "new_group",
2355                 .type   = FIO_OPT_STR_SET,
2356                 .off1   = td_var_offset(new_group),
2357                 .help   = "Mark the start of a new group (for reporting)",
2358         },
2359         {
2360                 .name   = "thread",
2361                 .type   = FIO_OPT_STR_SET,
2362                 .off1   = td_var_offset(use_thread),
2363                 .help   = "Use threads instead of forks",
2364         },
2365         {
2366                 .name   = "write_bw_log",
2367                 .type   = FIO_OPT_STR,
2368                 .off1   = td_var_offset(write_bw_log),
2369                 .cb     = str_write_bw_log_cb,
2370                 .help   = "Write log of bandwidth during run",
2371         },
2372         {
2373                 .name   = "write_lat_log",
2374                 .type   = FIO_OPT_STR,
2375                 .off1   = td_var_offset(write_lat_log),
2376                 .cb     = str_write_lat_log_cb,
2377                 .help   = "Write log of latency during run",
2378         },
2379         {
2380                 .name   = "write_iops_log",
2381                 .type   = FIO_OPT_STR,
2382                 .off1   = td_var_offset(write_iops_log),
2383                 .cb     = str_write_iops_log_cb,
2384                 .help   = "Write log of IOPS during run",
2385         },
2386         {
2387                 .name   = "log_avg_msec",
2388                 .type   = FIO_OPT_INT,
2389                 .off1   = td_var_offset(log_avg_msec),
2390                 .help   = "Average bw/iops/lat logs over this period of time",
2391                 .def    = "0",
2392         },
2393         {
2394                 .name   = "hugepage-size",
2395                 .type   = FIO_OPT_INT,
2396                 .off1   = td_var_offset(hugepage_size),
2397                 .help   = "When using hugepages, specify size of each page",
2398                 .def    = __fio_stringify(FIO_HUGE_PAGE),
2399         },
2400         {
2401                 .name   = "group_reporting",
2402                 .type   = FIO_OPT_STR_SET,
2403                 .off1   = td_var_offset(group_reporting),
2404                 .help   = "Do reporting on a per-group basis",
2405         },
2406         {
2407                 .name   = "zero_buffers",
2408                 .type   = FIO_OPT_STR_SET,
2409                 .off1   = td_var_offset(zero_buffers),
2410                 .help   = "Init IO buffers to all zeroes",
2411         },
2412         {
2413                 .name   = "refill_buffers",
2414                 .type   = FIO_OPT_STR_SET,
2415                 .off1   = td_var_offset(refill_buffers),
2416                 .help   = "Refill IO buffers on every IO submit",
2417         },
2418         {
2419                 .name   = "scramble_buffers",
2420                 .type   = FIO_OPT_BOOL,
2421                 .off1   = td_var_offset(scramble_buffers),
2422                 .help   = "Slightly scramble buffers on every IO submit",
2423                 .def    = "1",
2424         },
2425         {
2426                 .name   = "buffer_compress_percentage",
2427                 .type   = FIO_OPT_INT,
2428                 .off1   = td_var_offset(compress_percentage),
2429                 .maxval = 100,
2430                 .minval = 1,
2431                 .help   = "How compressible the buffer is (approximately)",
2432         },
2433         {
2434                 .name   = "buffer_compress_chunk",
2435                 .type   = FIO_OPT_INT,
2436                 .off1   = td_var_offset(compress_chunk),
2437                 .parent = "buffer_compress_percentage",
2438                 .help   = "Size of compressible region in buffer",
2439         },
2440         {
2441                 .name   = "clat_percentiles",
2442                 .type   = FIO_OPT_BOOL,
2443                 .off1   = td_var_offset(clat_percentiles),
2444                 .help   = "Enable the reporting of completion latency percentiles",
2445                 .def    = "1",
2446         },
2447         {
2448                 .name   = "percentile_list",
2449                 .type   = FIO_OPT_FLOAT_LIST,
2450                 .off1   = td_var_offset(percentile_list),
2451                 .off2   = td_var_offset(percentile_precision),
2452                 .help   = "Specify a custom list of percentiles to report",
2453                 .def    = "1:5:10:20:30:40:50:60:70:80:90:95:99:99.5:99.9:99.95:99.99",
2454                 .maxlen = FIO_IO_U_LIST_MAX_LEN,
2455                 .minfp  = 0.0,
2456                 .maxfp  = 100.0,
2457         },
2458
2459 #ifdef FIO_HAVE_DISK_UTIL
2460         {
2461                 .name   = "disk_util",
2462                 .type   = FIO_OPT_BOOL,
2463                 .off1   = td_var_offset(do_disk_util),
2464                 .help   = "Log disk utilization statistics",
2465                 .def    = "1",
2466         },
2467 #endif
2468         {
2469                 .name   = "gtod_reduce",
2470                 .type   = FIO_OPT_BOOL,
2471                 .help   = "Greatly reduce number of gettimeofday() calls",
2472                 .cb     = str_gtod_reduce_cb,
2473                 .def    = "0",
2474         },
2475         {
2476                 .name   = "disable_lat",
2477                 .type   = FIO_OPT_BOOL,
2478                 .off1   = td_var_offset(disable_lat),
2479                 .help   = "Disable latency numbers",
2480                 .parent = "gtod_reduce",
2481                 .def    = "0",
2482         },
2483         {
2484                 .name   = "disable_clat",
2485                 .type   = FIO_OPT_BOOL,
2486                 .off1   = td_var_offset(disable_clat),
2487                 .help   = "Disable completion latency numbers",
2488                 .parent = "gtod_reduce",
2489                 .def    = "0",
2490         },
2491         {
2492                 .name   = "disable_slat",
2493                 .type   = FIO_OPT_BOOL,
2494                 .off1   = td_var_offset(disable_slat),
2495                 .help   = "Disable submission latency numbers",
2496                 .parent = "gtod_reduce",
2497                 .def    = "0",
2498         },
2499         {
2500                 .name   = "disable_bw_measurement",
2501                 .type   = FIO_OPT_BOOL,
2502                 .off1   = td_var_offset(disable_bw),
2503                 .help   = "Disable bandwidth logging",
2504                 .parent = "gtod_reduce",
2505                 .def    = "0",
2506         },
2507         {
2508                 .name   = "gtod_cpu",
2509                 .type   = FIO_OPT_INT,
2510                 .cb     = str_gtod_cpu_cb,
2511                 .help   = "Set up dedicated gettimeofday() thread on this CPU",
2512                 .verify = gtod_cpu_verify,
2513         },
2514         {
2515                 .name   = "unified_rw_reporting",
2516                 .type   = FIO_OPT_BOOL,
2517                 .off1   = td_var_offset(unified_rw_rep),
2518                 .help   = "Unify reporting across data direction",
2519                 .def    = "0",
2520         },
2521         {
2522                 .name   = "continue_on_error",
2523                 .type   = FIO_OPT_STR,
2524                 .off1   = td_var_offset(continue_on_error),
2525                 .help   = "Continue on non-fatal errors during IO",
2526                 .def    = "none",
2527                 .posval = {
2528                           { .ival = "none",
2529                             .oval = ERROR_TYPE_NONE,
2530                             .help = "Exit when an error is encountered",
2531                           },
2532                           { .ival = "read",
2533                             .oval = ERROR_TYPE_READ,
2534                             .help = "Continue on read errors only",
2535                           },
2536                           { .ival = "write",
2537                             .oval = ERROR_TYPE_WRITE,
2538                             .help = "Continue on write errors only",
2539                           },
2540                           { .ival = "io",
2541                             .oval = ERROR_TYPE_READ | ERROR_TYPE_WRITE,
2542                             .help = "Continue on any IO errors",
2543                           },
2544                           { .ival = "verify",
2545                             .oval = ERROR_TYPE_VERIFY,
2546                             .help = "Continue on verify errors only",
2547                           },
2548                           { .ival = "all",
2549                             .oval = ERROR_TYPE_ANY,
2550                             .help = "Continue on all io and verify errors",
2551                           },
2552                           { .ival = "0",
2553                             .oval = ERROR_TYPE_NONE,
2554                             .help = "Alias for 'none'",
2555                           },
2556                           { .ival = "1",
2557                             .oval = ERROR_TYPE_ANY,
2558                             .help = "Alias for 'all'",
2559                           },
2560                 },
2561         },
2562         {
2563                 .name   = "ignore_error",
2564                 .type   = FIO_OPT_STR,
2565                 .cb     = str_ignore_error_cb,
2566                 .help   = "Set a specific list of errors to ignore",
2567                 .parent = "rw",
2568         },
2569         {
2570                 .name   = "error_dump",
2571                 .type   = FIO_OPT_BOOL,
2572                 .off1   = td_var_offset(error_dump),
2573                 .def    = "0",
2574                 .help   = "Dump info on each error",
2575         },
2576
2577         {
2578                 .name   = "profile",
2579                 .type   = FIO_OPT_STR_STORE,
2580                 .off1   = td_var_offset(profile),
2581                 .help   = "Select a specific builtin performance test",
2582         },
2583         {
2584                 .name   = "cgroup",
2585                 .type   = FIO_OPT_STR_STORE,
2586                 .off1   = td_var_offset(cgroup),
2587                 .help   = "Add job to cgroup of this name",
2588         },
2589         {
2590                 .name   = "cgroup_weight",
2591                 .type   = FIO_OPT_INT,
2592                 .off1   = td_var_offset(cgroup_weight),
2593                 .help   = "Use given weight for cgroup",
2594                 .minval = 100,
2595                 .maxval = 1000,
2596         },
2597         {
2598                 .name   = "cgroup_nodelete",
2599                 .type   = FIO_OPT_BOOL,
2600                 .off1   = td_var_offset(cgroup_nodelete),
2601                 .help   = "Do not delete cgroups after job completion",
2602                 .def    = "0",
2603         },
2604         {
2605                 .name   = "uid",
2606                 .type   = FIO_OPT_INT,
2607                 .off1   = td_var_offset(uid),
2608                 .help   = "Run job with this user ID",
2609         },
2610         {
2611                 .name   = "gid",
2612                 .type   = FIO_OPT_INT,
2613                 .off1   = td_var_offset(gid),
2614                 .help   = "Run job with this group ID",
2615         },
2616         {
2617                 .name   = "flow_id",
2618                 .type   = FIO_OPT_INT,
2619                 .off1   = td_var_offset(flow_id),
2620                 .help   = "The flow index ID to use",
2621                 .def    = "0",
2622         },
2623         {
2624                 .name   = "flow",
2625                 .type   = FIO_OPT_INT,
2626                 .off1   = td_var_offset(flow),
2627                 .help   = "Weight for flow control of this job",
2628                 .parent = "flow_id",
2629                 .def    = "0",
2630         },
2631         {
2632                 .name   = "flow_watermark",
2633                 .type   = FIO_OPT_INT,
2634                 .off1   = td_var_offset(flow_watermark),
2635                 .help   = "High watermark for flow control. This option"
2636                         " should be set to the same value for all threads"
2637                         " with non-zero flow.",
2638                 .parent = "flow_id",
2639                 .def    = "1024",
2640         },
2641         {
2642                 .name   = "flow_sleep",
2643                 .type   = FIO_OPT_INT,
2644                 .off1   = td_var_offset(flow_sleep),
2645                 .help   = "How many microseconds to sleep after being held"
2646                         " back by the flow control mechanism",
2647                 .parent = "flow_id",
2648                 .def    = "0",
2649         },
2650         {
2651                 .name = NULL,
2652         },
2653 };
2654
2655 static void add_to_lopt(struct option *lopt, struct fio_option *o,
2656                         const char *name, int val)
2657 {
2658         lopt->name = (char *) name;
2659         lopt->val = val;
2660         if (o->type == FIO_OPT_STR_SET)
2661                 lopt->has_arg = no_argument;
2662         else
2663                 lopt->has_arg = required_argument;
2664 }
2665
2666 static void options_to_lopts(struct fio_option *opts,
2667                               struct option *long_options,
2668                               int i, int option_type)
2669 {
2670         struct fio_option *o = &opts[0];
2671         while (o->name) {
2672                 add_to_lopt(&long_options[i], o, o->name, option_type);
2673                 if (o->alias) {
2674                         i++;
2675                         add_to_lopt(&long_options[i], o, o->alias, option_type);
2676                 }
2677
2678                 i++;
2679                 o++;
2680                 assert(i < FIO_NR_OPTIONS);
2681         }
2682 }
2683
2684 void fio_options_set_ioengine_opts(struct option *long_options,
2685                                    struct thread_data *td)
2686 {
2687         unsigned int i;
2688
2689         i = 0;
2690         while (long_options[i].name) {
2691                 if (long_options[i].val == FIO_GETOPT_IOENGINE) {
2692                         memset(&long_options[i], 0, sizeof(*long_options));
2693                         break;
2694                 }
2695                 i++;
2696         }
2697
2698         /*
2699          * Just clear out the prior ioengine options.
2700          */
2701         if (!td || !td->eo)
2702                 return;
2703
2704         options_to_lopts(td->io_ops->options, long_options, i,
2705                          FIO_GETOPT_IOENGINE);
2706 }
2707
2708 void fio_options_dup_and_init(struct option *long_options)
2709 {
2710         unsigned int i;
2711
2712         options_init(options);
2713
2714         i = 0;
2715         while (long_options[i].name)
2716                 i++;
2717
2718         options_to_lopts(options, long_options, i, FIO_GETOPT_JOB);
2719 }
2720
2721 struct fio_keyword {
2722         const char *word;
2723         const char *desc;
2724         char *replace;
2725 };
2726
2727 static struct fio_keyword fio_keywords[] = {
2728         {
2729                 .word   = "$pagesize",
2730                 .desc   = "Page size in the system",
2731         },
2732         {
2733                 .word   = "$mb_memory",
2734                 .desc   = "Megabytes of memory online",
2735         },
2736         {
2737                 .word   = "$ncpus",
2738                 .desc   = "Number of CPUs online in the system",
2739         },
2740         {
2741                 .word   = NULL,
2742         },
2743 };
2744
2745 void fio_keywords_init(void)
2746 {
2747         unsigned long long mb_memory;
2748         char buf[128];
2749         long l;
2750
2751         sprintf(buf, "%lu", (unsigned long) page_size);
2752         fio_keywords[0].replace = strdup(buf);
2753
2754         mb_memory = os_phys_mem() / (1024 * 1024);
2755         sprintf(buf, "%llu", mb_memory);
2756         fio_keywords[1].replace = strdup(buf);
2757
2758         l = cpus_online();
2759         sprintf(buf, "%lu", l);
2760         fio_keywords[2].replace = strdup(buf);
2761 }
2762
2763 #define BC_APP          "bc"
2764
2765 static char *bc_calc(char *str)
2766 {
2767         char buf[128], *tmp;
2768         FILE *f;
2769         int ret;
2770
2771         /*
2772          * No math, just return string
2773          */
2774         if ((!strchr(str, '+') && !strchr(str, '-') && !strchr(str, '*') &&
2775              !strchr(str, '/')) || strchr(str, '\''))
2776                 return str;
2777
2778         /*
2779          * Split option from value, we only need to calculate the value
2780          */
2781         tmp = strchr(str, '=');
2782         if (!tmp)
2783                 return str;
2784
2785         tmp++;
2786
2787         /*
2788          * Prevent buffer overflows; such a case isn't reasonable anyway
2789          */
2790         if (strlen(str) >= 128 || strlen(tmp) > 100)
2791                 return str;
2792
2793         sprintf(buf, "which %s > /dev/null", BC_APP);
2794         if (system(buf)) {
2795                 log_err("fio: bc is needed for performing math\n");
2796                 return NULL;
2797         }
2798
2799         sprintf(buf, "echo '%s' | %s", tmp, BC_APP);
2800         f = popen(buf, "r");
2801         if (!f) {
2802                 return NULL;
2803         }
2804
2805         ret = fread(&buf[tmp - str], 1, 128 - (tmp - str), f);
2806         if (ret <= 0) {
2807                 return NULL;
2808         }
2809
2810         pclose(f);
2811         buf[(tmp - str) + ret - 1] = '\0';
2812         memcpy(buf, str, tmp - str);
2813         free(str);
2814         return strdup(buf);
2815 }
2816
2817 /*
2818  * Return a copy of the input string with substrings of the form ${VARNAME}
2819  * substituted with the value of the environment variable VARNAME.  The
2820  * substitution always occurs, even if VARNAME is empty or the corresponding
2821  * environment variable undefined.
2822  */
2823 static char *option_dup_subs(const char *opt)
2824 {
2825         char out[OPT_LEN_MAX+1];
2826         char in[OPT_LEN_MAX+1];
2827         char *outptr = out;
2828         char *inptr = in;
2829         char *ch1, *ch2, *env;
2830         ssize_t nchr = OPT_LEN_MAX;
2831         size_t envlen;
2832
2833         if (strlen(opt) + 1 > OPT_LEN_MAX) {
2834                 log_err("OPT_LEN_MAX (%d) is too small\n", OPT_LEN_MAX);
2835                 return NULL;
2836         }
2837
2838         in[OPT_LEN_MAX] = '\0';
2839         strncpy(in, opt, OPT_LEN_MAX);
2840
2841         while (*inptr && nchr > 0) {
2842                 if (inptr[0] == '$' && inptr[1] == '{') {
2843                         ch2 = strchr(inptr, '}');
2844                         if (ch2 && inptr+1 < ch2) {
2845                                 ch1 = inptr+2;
2846                                 inptr = ch2+1;
2847                                 *ch2 = '\0';
2848
2849                                 env = getenv(ch1);
2850                                 if (env) {
2851                                         envlen = strlen(env);
2852                                         if (envlen <= nchr) {
2853                                                 memcpy(outptr, env, envlen);
2854                                                 outptr += envlen;
2855                                                 nchr -= envlen;
2856                                         }
2857                                 }
2858
2859                                 continue;
2860                         }
2861                 }
2862
2863                 *outptr++ = *inptr++;
2864                 --nchr;
2865         }
2866
2867         *outptr = '\0';
2868         return strdup(out);
2869 }
2870
2871 /*
2872  * Look for reserved variable names and replace them with real values
2873  */
2874 static char *fio_keyword_replace(char *opt)
2875 {
2876         char *s;
2877         int i;
2878         int docalc = 0;
2879
2880         for (i = 0; fio_keywords[i].word != NULL; i++) {
2881                 struct fio_keyword *kw = &fio_keywords[i];
2882
2883                 while ((s = strstr(opt, kw->word)) != NULL) {
2884                         char *new = malloc(strlen(opt) + 1);
2885                         char *o_org = opt;
2886                         int olen = s - opt;
2887                         int len;
2888
2889                         /*
2890                          * Copy part of the string before the keyword and
2891                          * sprintf() the replacement after it.
2892                          */
2893                         memcpy(new, opt, olen);
2894                         len = sprintf(new + olen, "%s", kw->replace);
2895
2896                         /*
2897                          * If there's more in the original string, copy that
2898                          * in too
2899                          */
2900                         opt += strlen(kw->word) + olen;
2901                         if (strlen(opt))
2902                                 memcpy(new + olen + len, opt, opt - o_org - 1);
2903
2904                         /*
2905                          * replace opt and free the old opt
2906                          */
2907                         opt = new;
2908                         free(o_org);
2909
2910                         docalc = 1;
2911                 }
2912         }
2913
2914         /*
2915          * Check for potential math and invoke bc, if possible
2916          */
2917         if (docalc)
2918                 opt = bc_calc(opt);
2919
2920         return opt;
2921 }
2922
2923 static char **dup_and_sub_options(char **opts, int num_opts)
2924 {
2925         int i;
2926         char **opts_copy = malloc(num_opts * sizeof(*opts));
2927         for (i = 0; i < num_opts; i++) {
2928                 opts_copy[i] = option_dup_subs(opts[i]);
2929                 if (!opts_copy[i])
2930                         continue;
2931                 opts_copy[i] = fio_keyword_replace(opts_copy[i]);
2932         }
2933         return opts_copy;
2934 }
2935
2936 int fio_options_parse(struct thread_data *td, char **opts, int num_opts)
2937 {
2938         int i, ret, unknown;
2939         char **opts_copy;
2940
2941         sort_options(opts, options, num_opts);
2942         opts_copy = dup_and_sub_options(opts, num_opts);
2943
2944         for (ret = 0, i = 0, unknown = 0; i < num_opts; i++) {
2945                 struct fio_option *o;
2946                 int newret = parse_option(opts_copy[i], opts[i], options, &o,
2947                                           td);
2948
2949                 if (opts_copy[i]) {
2950                         if (newret && !o) {
2951                                 unknown++;
2952                                 continue;
2953                         }
2954                         free(opts_copy[i]);
2955                         opts_copy[i] = NULL;
2956                 }
2957
2958                 ret |= newret;
2959         }
2960
2961         if (unknown) {
2962                 ret |= ioengine_load(td);
2963                 if (td->eo) {
2964                         sort_options(opts_copy, td->io_ops->options, num_opts);
2965                         opts = opts_copy;
2966                 }
2967                 for (i = 0; i < num_opts; i++) {
2968                         struct fio_option *o = NULL;
2969                         int newret = 1;
2970                         if (!opts_copy[i])
2971                                 continue;
2972
2973                         if (td->eo)
2974                                 newret = parse_option(opts_copy[i], opts[i],
2975                                                       td->io_ops->options, &o,
2976                                                       td->eo);
2977
2978                         ret |= newret;
2979                         if (!o)
2980                                 log_err("Bad option <%s>\n", opts[i]);
2981
2982                         free(opts_copy[i]);
2983                         opts_copy[i] = NULL;
2984                 }
2985         }
2986
2987         free(opts_copy);
2988         return ret;
2989 }
2990
2991 int fio_cmd_option_parse(struct thread_data *td, const char *opt, char *val)
2992 {
2993         return parse_cmd_option(opt, val, options, td);
2994 }
2995
2996 int fio_cmd_ioengine_option_parse(struct thread_data *td, const char *opt,
2997                                 char *val)
2998 {
2999         return parse_cmd_option(opt, val, td->io_ops->options, td);
3000 }
3001
3002 void fio_fill_default_options(struct thread_data *td)
3003 {
3004         fill_default_options(td, options);
3005 }
3006
3007 int fio_show_option_help(const char *opt)
3008 {
3009         return show_cmd_help(options, opt);
3010 }
3011
3012 void options_mem_dupe(void *data, struct fio_option *options)
3013 {
3014         struct fio_option *o;
3015         char **ptr;
3016
3017         for (o = &options[0]; o->name; o++) {
3018                 if (o->type != FIO_OPT_STR_STORE)
3019                         continue;
3020
3021                 ptr = td_var(data, o->off1);
3022                 if (*ptr)
3023                         *ptr = strdup(*ptr);
3024         }
3025 }
3026
3027 /*
3028  * dupe FIO_OPT_STR_STORE options
3029  */
3030 void fio_options_mem_dupe(struct thread_data *td)
3031 {
3032         options_mem_dupe(&td->o, options);
3033
3034         if (td->eo && td->io_ops) {
3035                 void *oldeo = td->eo;
3036
3037                 td->eo = malloc(td->io_ops->option_struct_size);
3038                 memcpy(td->eo, oldeo, td->io_ops->option_struct_size);
3039                 options_mem_dupe(td->eo, td->io_ops->options);
3040         }
3041 }
3042
3043 unsigned int fio_get_kb_base(void *data)
3044 {
3045         struct thread_data *td = data;
3046         unsigned int kb_base = 0;
3047
3048         if (td)
3049                 kb_base = td->o.kb_base;
3050         if (!kb_base)
3051                 kb_base = 1024;
3052
3053         return kb_base;
3054 }
3055
3056 int add_option(struct fio_option *o)
3057 {
3058         struct fio_option *__o;
3059         int opt_index = 0;
3060
3061         __o = options;
3062         while (__o->name) {
3063                 opt_index++;
3064                 __o++;
3065         }
3066
3067         memcpy(&options[opt_index], o, sizeof(*o));
3068         return 0;
3069 }
3070
3071 void invalidate_profile_options(const char *prof_name)
3072 {
3073         struct fio_option *o;
3074
3075         o = options;
3076         while (o->name) {
3077                 if (o->prof_name && !strcmp(o->prof_name, prof_name)) {
3078                         o->type = FIO_OPT_INVALID;
3079                         o->prof_name = NULL;
3080                 }
3081                 o++;
3082         }
3083 }
3084
3085 void add_opt_posval(const char *optname, const char *ival, const char *help)
3086 {
3087         struct fio_option *o;
3088         unsigned int i;
3089
3090         o = find_option(options, optname);
3091         if (!o)
3092                 return;
3093
3094         for (i = 0; i < PARSE_MAX_VP; i++) {
3095                 if (o->posval[i].ival)
3096                         continue;
3097
3098                 o->posval[i].ival = ival;
3099                 o->posval[i].help = help;
3100                 break;
3101         }
3102 }
3103
3104 void del_opt_posval(const char *optname, const char *ival)
3105 {
3106         struct fio_option *o;
3107         unsigned int i;
3108
3109         o = find_option(options, optname);
3110         if (!o)
3111                 return;
3112
3113         for (i = 0; i < PARSE_MAX_VP; i++) {
3114                 if (!o->posval[i].ival)
3115                         continue;
3116                 if (strcmp(o->posval[i].ival, ival))
3117                         continue;
3118
3119                 o->posval[i].ival = NULL;
3120                 o->posval[i].help = NULL;
3121         }
3122 }
3123
3124 void fio_options_free(struct thread_data *td)
3125 {
3126         options_free(options, td);
3127         if (td->eo && td->io_ops && td->io_ops->options) {
3128                 options_free(td->io_ops->options, td->eo);
3129                 free(td->eo);
3130                 td->eo = NULL;
3131         }
3132 }