Add a cpuset exit function
[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 <getopt.h>
7 #include <assert.h>
8 #include <libgen.h>
9 #include <fcntl.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12
13 #include "fio.h"
14 #include "parse.h"
15 #include "lib/fls.h"
16
17 #define td_var_offset(var)      ((size_t) &((struct thread_options *)0)->var)
18
19 /*
20  * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
21  */
22 static char *get_opt_postfix(const char *str)
23 {
24         char *p = strstr(str, ":");
25
26         if (!p)
27                 return NULL;
28
29         p++;
30         strip_blank_front(&p);
31         strip_blank_end(p);
32         return strdup(p);
33 }
34
35 static int bs_cmp(const void *p1, const void *p2)
36 {
37         const struct bssplit *bsp1 = p1;
38         const struct bssplit *bsp2 = p2;
39
40         return bsp1->perc < bsp2->perc;
41 }
42
43 static int str_bssplit_cb(void *data, const char *input)
44 {
45         struct thread_data *td = data;
46         char *fname, *str, *p;
47         unsigned int i, perc, perc_missing;
48         unsigned int max_bs, min_bs;
49         long long val;
50
51         p = str = strdup(input);
52
53         strip_blank_front(&str);
54         strip_blank_end(str);
55
56         td->o.bssplit_nr = 4;
57         td->o.bssplit = malloc(4 * sizeof(struct bssplit));
58
59         i = 0;
60         max_bs = 0;
61         min_bs = -1;
62         while ((fname = strsep(&str, ":")) != NULL) {
63                 char *perc_str;
64
65                 if (!strlen(fname))
66                         break;
67
68                 /*
69                  * grow struct buffer, if needed
70                  */
71                 if (i == td->o.bssplit_nr) {
72                         td->o.bssplit_nr <<= 1;
73                         td->o.bssplit = realloc(td->o.bssplit,
74                                                 td->o.bssplit_nr
75                                                   * sizeof(struct bssplit));
76                 }
77
78                 perc_str = strstr(fname, "/");
79                 if (perc_str) {
80                         *perc_str = '\0';
81                         perc_str++;
82                         perc = atoi(perc_str);
83                         if (perc > 100)
84                                 perc = 100;
85                         else if (!perc)
86                                 perc = -1;
87                 } else
88                         perc = -1;
89
90                 if (str_to_decimal(fname, &val, 1)) {
91                         log_err("fio: bssplit conversion failed\n");
92                         free(td->o.bssplit);
93                         return 1;
94                 }
95
96                 if (val > max_bs)
97                         max_bs = val;
98                 if (val < min_bs)
99                         min_bs = val;
100
101                 td->o.bssplit[i].bs = val;
102                 td->o.bssplit[i].perc = perc;
103                 i++;
104         }
105
106         td->o.bssplit_nr = i;
107
108         /*
109          * Now check if the percentages add up, and how much is missing
110          */
111         perc = perc_missing = 0;
112         for (i = 0; i < td->o.bssplit_nr; i++) {
113                 struct bssplit *bsp = &td->o.bssplit[i];
114
115                 if (bsp->perc == (unsigned char) -1)
116                         perc_missing++;
117                 else
118                         perc += bsp->perc;
119         }
120
121         if (perc > 100) {
122                 log_err("fio: bssplit percentages add to more than 100%%\n");
123                 free(td->o.bssplit);
124                 return 1;
125         }
126         /*
127          * If values didn't have a percentage set, divide the remains between
128          * them.
129          */
130         if (perc_missing) {
131                 for (i = 0; i < td->o.bssplit_nr; i++) {
132                         struct bssplit *bsp = &td->o.bssplit[i];
133
134                         if (bsp->perc == (unsigned char) -1)
135                                 bsp->perc = (100 - perc) / perc_missing;
136                 }
137         }
138
139         td->o.min_bs[DDIR_READ] = td->o.min_bs[DDIR_WRITE] = min_bs;
140         td->o.max_bs[DDIR_READ] = td->o.max_bs[DDIR_WRITE] = max_bs;
141
142         /*
143          * now sort based on percentages, for ease of lookup
144          */
145         qsort(td->o.bssplit, td->o.bssplit_nr, sizeof(struct bssplit), bs_cmp);
146
147         free(p);
148         return 0;
149 }
150
151 static int str_rw_cb(void *data, const char *str)
152 {
153         struct thread_data *td = data;
154         char *nr = get_opt_postfix(str);
155
156         td->o.ddir_nr = 1;
157         if (nr) {
158                 td->o.ddir_nr = atoi(nr);
159                 free(nr);
160         }
161
162         return 0;
163 }
164
165 static int str_mem_cb(void *data, const char *mem)
166 {
167         struct thread_data *td = data;
168
169         if (td->o.mem_type == MEM_MMAPHUGE || td->o.mem_type == MEM_MMAP) {
170                 td->mmapfile = get_opt_postfix(mem);
171                 if (td->o.mem_type == MEM_MMAPHUGE && !td->mmapfile) {
172                         log_err("fio: mmaphuge:/path/to/file\n");
173                         return 1;
174                 }
175         }
176
177         return 0;
178 }
179
180 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
181 {
182         mlock_size = *val;
183         return 0;
184 }
185
186 static int str_rwmix_read_cb(void *data, unsigned int *val)
187 {
188         struct thread_data *td = data;
189
190         td->o.rwmix[DDIR_READ] = *val;
191         td->o.rwmix[DDIR_WRITE] = 100 - *val;
192         return 0;
193 }
194
195 static int str_rwmix_write_cb(void *data, unsigned int *val)
196 {
197         struct thread_data *td = data;
198
199         td->o.rwmix[DDIR_WRITE] = *val;
200         td->o.rwmix[DDIR_READ] = 100 - *val;
201         return 0;
202 }
203
204 #ifdef FIO_HAVE_IOPRIO
205 static int str_prioclass_cb(void *data, unsigned int *val)
206 {
207         struct thread_data *td = data;
208         unsigned short mask;
209
210         /*
211          * mask off old class bits, str_prio_cb() may have set a default class
212          */
213         mask = (1 << IOPRIO_CLASS_SHIFT) - 1;
214         td->ioprio &= mask;
215
216         td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
217         td->ioprio_set = 1;
218         return 0;
219 }
220
221 static int str_prio_cb(void *data, unsigned int *val)
222 {
223         struct thread_data *td = data;
224
225         td->ioprio |= *val;
226
227         /*
228          * If no class is set, assume BE
229          */
230         if ((td->ioprio >> IOPRIO_CLASS_SHIFT) == 0)
231                 td->ioprio |= IOPRIO_CLASS_BE << IOPRIO_CLASS_SHIFT;
232
233         td->ioprio_set = 1;
234         return 0;
235 }
236 #endif
237
238 static int str_exitall_cb(void)
239 {
240         exitall_on_terminate = 1;
241         return 0;
242 }
243
244 #ifdef FIO_HAVE_CPU_AFFINITY
245 static int str_cpumask_cb(void *data, unsigned int *val)
246 {
247         struct thread_data *td = data;
248         unsigned int i;
249         long max_cpu;
250
251         fio_cpuset_init(td);
252         max_cpu = sysconf(_SC_NPROCESSORS_ONLN);
253
254         for (i = 0; i < sizeof(int) * 8; i++) {
255                 if ((1 << i) & *val) {
256                         if (i > max_cpu) {
257                                 log_err("fio: CPU %d too large (max=%ld)\n", i,
258                                                                 max_cpu);
259                                 return 1;
260                         }
261                         dprint(FD_PARSE, "set cpu allowed %d\n", i);
262                         fio_cpu_set(&td->o.cpumask, i);
263                 }
264         }
265
266         td->o.cpumask_set = 1;
267         return 0;
268 }
269
270 static int str_cpus_allowed_cb(void *data, const char *input)
271 {
272         struct thread_data *td = data;
273         char *cpu, *str, *p;
274         long max_cpu;
275         int ret = 0;
276
277         fio_cpuset_init(td);
278
279         p = str = strdup(input);
280
281         strip_blank_front(&str);
282         strip_blank_end(str);
283
284         max_cpu = sysconf(_SC_NPROCESSORS_ONLN);
285
286         while ((cpu = strsep(&str, ",")) != NULL) {
287                 char *str2, *cpu2;
288                 int icpu, icpu2;
289
290                 if (!strlen(cpu))
291                         break;
292
293                 str2 = cpu;
294                 icpu2 = -1;
295                 while ((cpu2 = strsep(&str2, "-")) != NULL) {
296                         if (!strlen(cpu2))
297                                 break;
298
299                         icpu2 = atoi(cpu2);
300                 }
301
302                 icpu = atoi(cpu);
303                 if (icpu2 == -1)
304                         icpu2 = icpu;
305                 while (icpu <= icpu2) {
306                         if (icpu >= FIO_MAX_CPUS) {
307                                 log_err("fio: your OS only supports up to"
308                                         " %d CPUs\n", (int) FIO_MAX_CPUS);
309                                 ret = 1;
310                                 break;
311                         }
312                         if (icpu > max_cpu) {
313                                 log_err("fio: CPU %d too large (max=%ld)\n",
314                                                         icpu, max_cpu);
315                                 ret = 1;
316                                 break;
317                         }
318         
319                         dprint(FD_PARSE, "set cpu allowed %d\n", icpu);
320                         fio_cpu_set(&td->o.cpumask, icpu);
321                         icpu++;
322                 }
323                 if (ret)
324                         break;
325         }
326
327         free(p);
328         if (!ret)
329                 td->o.cpumask_set = 1;
330         return ret;
331 }
332 #endif
333
334 static int str_fst_cb(void *data, const char *str)
335 {
336         struct thread_data *td = data;
337         char *nr = get_opt_postfix(str);
338
339         td->file_service_nr = 1;
340         if (nr) {
341                 td->file_service_nr = atoi(nr);
342                 free(nr);
343         }
344
345         return 0;
346 }
347
348 static int check_dir(struct thread_data *td, char *fname)
349 {
350         char file[PATH_MAX], *dir;
351         int elen = 0;
352
353         if (td->o.directory) {
354                 strcpy(file, td->o.directory);
355                 strcat(file, "/");
356                 elen = strlen(file);
357         }
358
359         sprintf(file + elen, "%s", fname);
360         dir = dirname(file);
361
362 #if 0
363         {
364         struct stat sb;
365         /*
366          * We can't do this on FIO_DISKLESSIO engines. The engine isn't loaded
367          * yet, so we can't do this check right here...
368          */
369         if (lstat(dir, &sb) < 0) {
370                 int ret = errno;
371
372                 log_err("fio: %s is not a directory\n", dir);
373                 td_verror(td, ret, "lstat");
374                 return 1;
375         }
376
377         if (!S_ISDIR(sb.st_mode)) {
378                 log_err("fio: %s is not a directory\n", dir);
379                 return 1;
380         }
381         }
382 #endif
383
384         return 0;
385 }
386
387 static int str_filename_cb(void *data, const char *input)
388 {
389         struct thread_data *td = data;
390         char *fname, *str, *p;
391
392         p = str = strdup(input);
393
394         strip_blank_front(&str);
395         strip_blank_end(str);
396
397         if (!td->files_index)
398                 td->o.nr_files = 0;
399
400         while ((fname = strsep(&str, ":")) != NULL) {
401                 if (!strlen(fname))
402                         break;
403                 if (check_dir(td, fname)) {
404                         free(p);
405                         return 1;
406                 }
407                 add_file(td, fname);
408                 td->o.nr_files++;
409         }
410
411         free(p);
412         return 0;
413 }
414
415 static int str_directory_cb(void *data, const char fio_unused *str)
416 {
417         struct thread_data *td = data;
418         struct stat sb;
419
420         if (lstat(td->o.directory, &sb) < 0) {
421                 int ret = errno;
422
423                 log_err("fio: %s is not a directory\n", td->o.directory);
424                 td_verror(td, ret, "lstat");
425                 return 1;
426         }
427         if (!S_ISDIR(sb.st_mode)) {
428                 log_err("fio: %s is not a directory\n", td->o.directory);
429                 return 1;
430         }
431
432         return 0;
433 }
434
435 static int str_opendir_cb(void *data, const char fio_unused *str)
436 {
437         struct thread_data *td = data;
438
439         if (!td->files_index)
440                 td->o.nr_files = 0;
441
442         return add_dir_files(td, td->o.opendir);
443 }
444
445 static int str_verify_offset_cb(void *data, unsigned int *off)
446 {
447         struct thread_data *td = data;
448
449         if (*off && *off < sizeof(struct verify_header)) {
450                 log_err("fio: verify_offset too small\n");
451                 return 1;
452         }
453
454         td->o.verify_offset = *off;
455         return 0;
456 }
457
458 static int str_verify_pattern_cb(void *data, unsigned int *off)
459 {
460         struct thread_data *td = data;
461         unsigned int msb;
462
463         msb = __fls(*off);
464         if (msb <= 8)
465                 td->o.verify_pattern_bytes = 1;
466         else if (msb <= 16)
467                 td->o.verify_pattern_bytes = 2;
468         else if (msb <= 24)
469                 td->o.verify_pattern_bytes = 3;
470         else
471                 td->o.verify_pattern_bytes = 4;
472
473         td->o.verify_pattern = *off;
474         return 0;
475 }
476
477 static int str_lockfile_cb(void *data, const char *str)
478 {
479         struct thread_data *td = data;
480         char *nr = get_opt_postfix(str);
481
482         td->o.lockfile_batch = 1;
483         if (nr) {
484                 td->o.lockfile_batch = atoi(nr);
485                 free(nr);
486         }
487
488         return 0;
489 }
490
491 static int str_write_bw_log_cb(void *data, const char *str)
492 {
493         struct thread_data *td = data;
494
495         if (str)
496                 td->o.bw_log_file = strdup(str);
497
498         td->o.write_bw_log = 1;
499         return 0;
500 }
501
502 static int str_write_lat_log_cb(void *data, const char *str)
503 {
504         struct thread_data *td = data;
505
506         if (str)
507                 td->o.lat_log_file = strdup(str);
508
509         td->o.write_lat_log = 1;
510         return 0;
511 }
512
513 static int str_gtod_reduce_cb(void *data, int *il)
514 {
515         struct thread_data *td = data;
516         int val = *il;
517
518         td->o.disable_clat = !!val;
519         td->o.disable_slat = !!val;
520         td->o.disable_bw = !!val;
521         if (val)
522                 td->tv_cache_mask = 63;
523
524         return 0;
525 }
526
527 static int str_gtod_cpu_cb(void *data, int *il)
528 {
529         struct thread_data *td = data;
530         int val = *il;
531
532         td->o.gtod_cpu = val;
533         td->o.gtod_offload = 1;
534         return 0;
535 }
536
537 #define __stringify_1(x)        #x
538 #define __stringify(x)          __stringify_1(x)
539
540 /*
541  * Map of job/command line options
542  */
543 static struct fio_option options[] = {
544         {
545                 .name   = "description",
546                 .type   = FIO_OPT_STR_STORE,
547                 .off1   = td_var_offset(description),
548                 .help   = "Text job description",
549         },
550         {
551                 .name   = "name",
552                 .type   = FIO_OPT_STR_STORE,
553                 .off1   = td_var_offset(name),
554                 .help   = "Name of this job",
555         },
556         {
557                 .name   = "directory",
558                 .type   = FIO_OPT_STR_STORE,
559                 .off1   = td_var_offset(directory),
560                 .cb     = str_directory_cb,
561                 .help   = "Directory to store files in",
562         },
563         {
564                 .name   = "filename",
565                 .type   = FIO_OPT_STR_STORE,
566                 .off1   = td_var_offset(filename),
567                 .cb     = str_filename_cb,
568                 .prio   = 1, /* must come before "directory" */
569                 .help   = "File(s) to use for the workload",
570         },
571         {
572                 .name   = "lockfile",
573                 .type   = FIO_OPT_STR,
574                 .cb     = str_lockfile_cb,
575                 .off1   = td_var_offset(file_lock_mode),
576                 .help   = "Lock file when doing IO to it",
577                 .parent = "filename",
578                 .def    = "none",
579                 .posval = {
580                           { .ival = "none",
581                             .oval = FILE_LOCK_NONE,
582                             .help = "No file locking",
583                           },
584                           { .ival = "exclusive",
585                             .oval = FILE_LOCK_EXCLUSIVE,
586                             .help = "Exclusive file lock",
587                           },
588                           {
589                             .ival = "readwrite",
590                             .oval = FILE_LOCK_READWRITE,
591                             .help = "Read vs write lock",
592                           },
593                 },
594         },
595         {
596                 .name   = "opendir",
597                 .type   = FIO_OPT_STR_STORE,
598                 .off1   = td_var_offset(opendir),
599                 .cb     = str_opendir_cb,
600                 .help   = "Recursively add files from this directory and down",
601         },
602         {
603                 .name   = "rw",
604                 .alias  = "readwrite",
605                 .type   = FIO_OPT_STR,
606                 .cb     = str_rw_cb,
607                 .off1   = td_var_offset(td_ddir),
608                 .help   = "IO direction",
609                 .def    = "read",
610                 .posval = {
611                           { .ival = "read",
612                             .oval = TD_DDIR_READ,
613                             .help = "Sequential read",
614                           },
615                           { .ival = "write",
616                             .oval = TD_DDIR_WRITE,
617                             .help = "Sequential write",
618                           },
619                           { .ival = "randread",
620                             .oval = TD_DDIR_RANDREAD,
621                             .help = "Random read",
622                           },
623                           { .ival = "randwrite",
624                             .oval = TD_DDIR_RANDWRITE,
625                             .help = "Random write",
626                           },
627                           { .ival = "rw",
628                             .oval = TD_DDIR_RW,
629                             .help = "Sequential read and write mix",
630                           },
631                           { .ival = "randrw",
632                             .oval = TD_DDIR_RANDRW,
633                             .help = "Random read and write mix"
634                           },
635                 },
636         },
637         {
638                 .name   = "ioengine",
639                 .type   = FIO_OPT_STR_STORE,
640                 .off1   = td_var_offset(ioengine),
641                 .help   = "IO engine to use",
642                 .def    = "sync",
643                 .posval = {
644                           { .ival = "sync",
645                             .help = "Use read/write",
646                           },
647                           { .ival = "psync",
648                             .help = "Use pread/pwrite",
649                           },
650                           { .ival = "vsync",
651                              .help = "Use readv/writev",
652                           },
653 #ifdef FIO_HAVE_LIBAIO
654                           { .ival = "libaio",
655                             .help = "Linux native asynchronous IO",
656                           },
657 #endif
658 #ifdef FIO_HAVE_POSIXAIO
659                           { .ival = "posixaio",
660                             .help = "POSIX asynchronous IO",
661                           },
662 #endif
663 #ifdef FIO_HAVE_SOLARISAIO
664                           { .ival = "solarisaio",
665                             .help = "Solaris native asynchronous IO",
666                           },
667 #endif
668                           { .ival = "mmap",
669                             .help = "Memory mapped IO",
670                           },
671 #ifdef FIO_HAVE_SPLICE
672                           { .ival = "splice",
673                             .help = "splice/vmsplice based IO",
674                           },
675                           { .ival = "netsplice",
676                             .help = "splice/vmsplice to/from the network",
677                           },
678 #endif
679 #ifdef FIO_HAVE_SGIO
680                           { .ival = "sg",
681                             .help = "SCSI generic v3 IO",
682                           },
683 #endif
684                           { .ival = "null",
685                             .help = "Testing engine (no data transfer)",
686                           },
687                           { .ival = "net",
688                             .help = "Network IO",
689                           },
690 #ifdef FIO_HAVE_SYSLET
691                           { .ival = "syslet-rw",
692                             .help = "syslet enabled async pread/pwrite IO",
693                           },
694 #endif
695                           { .ival = "cpuio",
696                             .help = "CPU cycler burner engine",
697                           },
698 #ifdef FIO_HAVE_GUASI
699                           { .ival = "guasi",
700                             .help = "GUASI IO engine",
701                           },
702 #endif
703                           { .ival = "external",
704                             .help = "Load external engine (append name)",
705                           },
706                 },
707         },
708         {
709                 .name   = "iodepth",
710                 .type   = FIO_OPT_INT,
711                 .off1   = td_var_offset(iodepth),
712                 .help   = "Amount of IO buffers to keep in flight",
713                 .minval = 1,
714                 .def    = "1",
715         },
716         {
717                 .name   = "iodepth_batch",
718                 .alias  = "iodepth_batch_submit",
719                 .type   = FIO_OPT_INT,
720                 .off1   = td_var_offset(iodepth_batch),
721                 .help   = "Number of IO to submit in one go",
722                 .parent = "iodepth",
723                 .minval = 1,
724                 .def    = "1",
725         },
726         {
727                 .name   = "iodepth_batch_complete",
728                 .type   = FIO_OPT_INT,
729                 .off1   = td_var_offset(iodepth_batch_complete),
730                 .help   = "Number of IO to retrieve in one go",
731                 .parent = "iodepth",
732                 .minval = 0,
733                 .def    = "1",
734         },
735         {
736                 .name   = "iodepth_low",
737                 .type   = FIO_OPT_INT,
738                 .off1   = td_var_offset(iodepth_low),
739                 .help   = "Low water mark for queuing depth",
740                 .parent = "iodepth",
741         },
742         {
743                 .name   = "size",
744                 .type   = FIO_OPT_STR_VAL,
745                 .off1   = td_var_offset(size),
746                 .minval = 1,
747                 .help   = "Total size of device or files",
748         },
749         {
750                 .name   = "fill_device",
751                 .type   = FIO_OPT_BOOL,
752                 .off1   = td_var_offset(fill_device),
753                 .help   = "Write until an ENOSPC error occurs",
754                 .def    = "0",
755         },
756         {
757                 .name   = "filesize",
758                 .type   = FIO_OPT_STR_VAL,
759                 .off1   = td_var_offset(file_size_low),
760                 .off2   = td_var_offset(file_size_high),
761                 .minval = 1,
762                 .help   = "Size of individual files",
763         },
764         {
765                 .name   = "offset",
766                 .alias  = "fileoffset",
767                 .type   = FIO_OPT_STR_VAL,
768                 .off1   = td_var_offset(start_offset),
769                 .help   = "Start IO from this offset",
770                 .def    = "0",
771         },
772         {
773                 .name   = "bs",
774                 .alias  = "blocksize",
775                 .type   = FIO_OPT_STR_VAL_INT,
776                 .off1   = td_var_offset(bs[DDIR_READ]),
777                 .off2   = td_var_offset(bs[DDIR_WRITE]),
778                 .minval = 1,
779                 .help   = "Block size unit",
780                 .def    = "4k",
781                 .parent = "rw",
782         },
783         {
784                 .name   = "bsrange",
785                 .alias  = "blocksize_range",
786                 .type   = FIO_OPT_RANGE,
787                 .off1   = td_var_offset(min_bs[DDIR_READ]),
788                 .off2   = td_var_offset(max_bs[DDIR_READ]),
789                 .off3   = td_var_offset(min_bs[DDIR_WRITE]),
790                 .off4   = td_var_offset(max_bs[DDIR_WRITE]),
791                 .minval = 1,
792                 .help   = "Set block size range (in more detail than bs)",
793                 .parent = "rw",
794         },
795         {
796                 .name   = "bssplit",
797                 .type   = FIO_OPT_STR,
798                 .cb     = str_bssplit_cb,
799                 .help   = "Set a specific mix of block sizes",
800                 .parent = "rw",
801         },
802         {
803                 .name   = "bs_unaligned",
804                 .alias  = "blocksize_unaligned",
805                 .type   = FIO_OPT_STR_SET,
806                 .off1   = td_var_offset(bs_unaligned),
807                 .help   = "Don't sector align IO buffer sizes",
808                 .parent = "rw",
809         },
810         {
811                 .name   = "randrepeat",
812                 .type   = FIO_OPT_BOOL,
813                 .off1   = td_var_offset(rand_repeatable),
814                 .help   = "Use repeatable random IO pattern",
815                 .def    = "1",
816                 .parent = "rw",
817         },
818         {
819                 .name   = "norandommap",
820                 .type   = FIO_OPT_STR_SET,
821                 .off1   = td_var_offset(norandommap),
822                 .help   = "Accept potential duplicate random blocks",
823                 .parent = "rw",
824         },
825         {
826                 .name   = "softrandommap",
827                 .type   = FIO_OPT_BOOL,
828                 .off1   = td_var_offset(softrandommap),
829                 .help   = "Set norandommap if randommap allocation fails",
830                 .parent = "norandommap",
831                 .def    = "0",
832         },
833         {
834                 .name   = "nrfiles",
835                 .type   = FIO_OPT_INT,
836                 .off1   = td_var_offset(nr_files),
837                 .help   = "Split job workload between this number of files",
838                 .def    = "1",
839         },
840         {
841                 .name   = "openfiles",
842                 .type   = FIO_OPT_INT,
843                 .off1   = td_var_offset(open_files),
844                 .help   = "Number of files to keep open at the same time",
845         },
846         {
847                 .name   = "file_service_type",
848                 .type   = FIO_OPT_STR,
849                 .cb     = str_fst_cb,
850                 .off1   = td_var_offset(file_service_type),
851                 .help   = "How to select which file to service next",
852                 .def    = "roundrobin",
853                 .posval = {
854                           { .ival = "random",
855                             .oval = FIO_FSERVICE_RANDOM,
856                             .help = "Choose a file at random",
857                           },
858                           { .ival = "roundrobin",
859                             .oval = FIO_FSERVICE_RR,
860                             .help = "Round robin select files",
861                           },
862                 },
863                 .parent = "nrfiles",
864         },
865         {
866                 .name   = "fadvise_hint",
867                 .type   = FIO_OPT_BOOL,
868                 .off1   = td_var_offset(fadvise_hint),
869                 .help   = "Use fadvise() to advise the kernel on IO pattern",
870                 .def    = "1",
871         },
872         {
873                 .name   = "fsync",
874                 .type   = FIO_OPT_INT,
875                 .off1   = td_var_offset(fsync_blocks),
876                 .help   = "Issue fsync for writes every given number of blocks",
877                 .def    = "0",
878         },
879         {
880                 .name   = "direct",
881                 .type   = FIO_OPT_BOOL,
882                 .off1   = td_var_offset(odirect),
883                 .help   = "Use O_DIRECT IO (negates buffered)",
884                 .def    = "0",
885         },
886         {
887                 .name   = "buffered",
888                 .type   = FIO_OPT_BOOL,
889                 .off1   = td_var_offset(odirect),
890                 .neg    = 1,
891                 .help   = "Use buffered IO (negates direct)",
892                 .def    = "1",
893         },
894         {
895                 .name   = "overwrite",
896                 .type   = FIO_OPT_BOOL,
897                 .off1   = td_var_offset(overwrite),
898                 .help   = "When writing, set whether to overwrite current data",
899                 .def    = "0",
900         },
901         {
902                 .name   = "loops",
903                 .type   = FIO_OPT_INT,
904                 .off1   = td_var_offset(loops),
905                 .help   = "Number of times to run the job",
906                 .def    = "1",
907         },
908         {
909                 .name   = "numjobs",
910                 .type   = FIO_OPT_INT,
911                 .off1   = td_var_offset(numjobs),
912                 .help   = "Duplicate this job this many times",
913                 .def    = "1",
914         },
915         {
916                 .name   = "startdelay",
917                 .type   = FIO_OPT_INT,
918                 .off1   = td_var_offset(start_delay),
919                 .help   = "Only start job when this period has passed",
920                 .def    = "0",
921         },
922         {
923                 .name   = "runtime",
924                 .alias  = "timeout",
925                 .type   = FIO_OPT_STR_VAL_TIME,
926                 .off1   = td_var_offset(timeout),
927                 .help   = "Stop workload when this amount of time has passed",
928                 .def    = "0",
929         },
930         {
931                 .name   = "time_based",
932                 .type   = FIO_OPT_STR_SET,
933                 .off1   = td_var_offset(time_based),
934                 .help   = "Keep running until runtime/timeout is met",
935         },
936         {
937                 .name   = "ramp_time",
938                 .type   = FIO_OPT_STR_VAL_TIME,
939                 .off1   = td_var_offset(ramp_time),
940                 .help   = "Ramp up time before measuring performance",
941         },
942         {
943                 .name   = "mem",
944                 .alias  = "iomem",
945                 .type   = FIO_OPT_STR,
946                 .cb     = str_mem_cb,
947                 .off1   = td_var_offset(mem_type),
948                 .help   = "Backing type for IO buffers",
949                 .def    = "malloc",
950                 .posval = {
951                           { .ival = "malloc",
952                             .oval = MEM_MALLOC,
953                             .help = "Use malloc(3) for IO buffers",
954                           },
955                           { .ival = "shm",
956                             .oval = MEM_SHM,
957                             .help = "Use shared memory segments for IO buffers",
958                           },
959 #ifdef FIO_HAVE_HUGETLB
960                           { .ival = "shmhuge",
961                             .oval = MEM_SHMHUGE,
962                             .help = "Like shm, but use huge pages",
963                           },
964 #endif
965                           { .ival = "mmap",
966                             .oval = MEM_MMAP,
967                             .help = "Use mmap(2) (file or anon) for IO buffers",
968                           },
969 #ifdef FIO_HAVE_HUGETLB
970                           { .ival = "mmaphuge",
971                             .oval = MEM_MMAPHUGE,
972                             .help = "Like mmap, but use huge pages",
973                           },
974 #endif
975                   },
976         },
977         {
978                 .name   = "verify",
979                 .type   = FIO_OPT_STR,
980                 .off1   = td_var_offset(verify),
981                 .help   = "Verify data written",
982                 .def    = "0",
983                 .posval = {
984                           { .ival = "0",
985                             .oval = VERIFY_NONE,
986                             .help = "Don't do IO verification",
987                           },
988                           { .ival = "md5",
989                             .oval = VERIFY_MD5,
990                             .help = "Use md5 checksums for verification",
991                           },
992                           { .ival = "crc64",
993                             .oval = VERIFY_CRC64,
994                             .help = "Use crc64 checksums for verification",
995                           },
996                           { .ival = "crc32",
997                             .oval = VERIFY_CRC32,
998                             .help = "Use crc32 checksums for verification",
999                           },
1000                           { .ival = "crc32c-intel",
1001                             .oval = VERIFY_CRC32C_INTEL,
1002                             .help = "Use hw crc32c checksums for verification",
1003                           },
1004                           { .ival = "crc32c",
1005                             .oval = VERIFY_CRC32C,
1006                             .help = "Use crc32c checksums for verification",
1007                           },
1008                           { .ival = "crc16",
1009                             .oval = VERIFY_CRC16,
1010                             .help = "Use crc16 checksums for verification",
1011                           },
1012                           { .ival = "crc7",
1013                             .oval = VERIFY_CRC7,
1014                             .help = "Use crc7 checksums for verification",
1015                           },
1016                           { .ival = "sha256",
1017                             .oval = VERIFY_SHA256,
1018                             .help = "Use sha256 checksums for verification",
1019                           },
1020                           { .ival = "sha512",
1021                             .oval = VERIFY_SHA512,
1022                             .help = "Use sha512 checksums for verification",
1023                           },
1024                           { .ival = "meta",
1025                             .oval = VERIFY_META,
1026                             .help = "Use io information",
1027                           },
1028                           {
1029                             .ival = "null",
1030                             .oval = VERIFY_NULL,
1031                             .help = "Pretend to verify",
1032                           },
1033                 },
1034         },
1035         {
1036                 .name   = "do_verify",
1037                 .type   = FIO_OPT_BOOL,
1038                 .off1   = td_var_offset(do_verify),
1039                 .help   = "Run verification stage after write",
1040                 .def    = "1",
1041                 .parent = "verify",
1042         },
1043         {
1044                 .name   = "verifysort",
1045                 .type   = FIO_OPT_BOOL,
1046                 .off1   = td_var_offset(verifysort),
1047                 .help   = "Sort written verify blocks for read back",
1048                 .def    = "1",
1049                 .parent = "verify",
1050         },
1051         {
1052                 .name   = "verify_interval",
1053                 .type   = FIO_OPT_STR_VAL_INT,
1054                 .off1   = td_var_offset(verify_interval),
1055                 .minval = 2 * sizeof(struct verify_header),
1056                 .help   = "Store verify buffer header every N bytes",
1057                 .parent = "verify",
1058         },
1059         {
1060                 .name   = "verify_offset",
1061                 .type   = FIO_OPT_STR_VAL_INT,
1062                 .help   = "Offset verify header location by N bytes",
1063                 .def    = "0",
1064                 .cb     = str_verify_offset_cb,
1065                 .parent = "verify",
1066         },
1067         {
1068                 .name   = "verify_pattern",
1069                 .type   = FIO_OPT_INT,
1070                 .cb     = str_verify_pattern_cb,
1071                 .help   = "Fill pattern for IO buffers",
1072                 .parent = "verify",
1073         },
1074         {
1075                 .name   = "verify_fatal",
1076                 .type   = FIO_OPT_BOOL,
1077                 .off1   = td_var_offset(verify_fatal),
1078                 .def    = "0",
1079                 .help   = "Exit on a single verify failure, don't continue",
1080                 .parent = "verify",
1081         },
1082         {
1083                 .name   = "write_iolog",
1084                 .type   = FIO_OPT_STR_STORE,
1085                 .off1   = td_var_offset(write_iolog_file),
1086                 .help   = "Store IO pattern to file",
1087         },
1088         {
1089                 .name   = "read_iolog",
1090                 .type   = FIO_OPT_STR_STORE,
1091                 .off1   = td_var_offset(read_iolog_file),
1092                 .help   = "Playback IO pattern from file",
1093         },
1094         {
1095                 .name   = "exec_prerun",
1096                 .type   = FIO_OPT_STR_STORE,
1097                 .off1   = td_var_offset(exec_prerun),
1098                 .help   = "Execute this file prior to running job",
1099         },
1100         {
1101                 .name   = "exec_postrun",
1102                 .type   = FIO_OPT_STR_STORE,
1103                 .off1   = td_var_offset(exec_postrun),
1104                 .help   = "Execute this file after running job",
1105         },
1106 #ifdef FIO_HAVE_IOSCHED_SWITCH
1107         {
1108                 .name   = "ioscheduler",
1109                 .type   = FIO_OPT_STR_STORE,
1110                 .off1   = td_var_offset(ioscheduler),
1111                 .help   = "Use this IO scheduler on the backing device",
1112         },
1113 #endif
1114         {
1115                 .name   = "zonesize",
1116                 .type   = FIO_OPT_STR_VAL,
1117                 .off1   = td_var_offset(zone_size),
1118                 .help   = "Give size of an IO zone",
1119                 .def    = "0",
1120         },
1121         {
1122                 .name   = "zoneskip",
1123                 .type   = FIO_OPT_STR_VAL,
1124                 .off1   = td_var_offset(zone_skip),
1125                 .help   = "Space between IO zones",
1126                 .def    = "0",
1127         },
1128         {
1129                 .name   = "lockmem",
1130                 .type   = FIO_OPT_STR_VAL,
1131                 .cb     = str_lockmem_cb,
1132                 .help   = "Lock down this amount of memory",
1133                 .def    = "0",
1134         },
1135         {
1136                 .name   = "rwmixread",
1137                 .type   = FIO_OPT_INT,
1138                 .cb     = str_rwmix_read_cb,
1139                 .maxval = 100,
1140                 .help   = "Percentage of mixed workload that is reads",
1141                 .def    = "50",
1142         },
1143         {
1144                 .name   = "rwmixwrite",
1145                 .type   = FIO_OPT_INT,
1146                 .cb     = str_rwmix_write_cb,
1147                 .maxval = 100,
1148                 .help   = "Percentage of mixed workload that is writes",
1149                 .def    = "50",
1150         },
1151         {
1152                 .name   = "rwmixcycle",
1153                 .type   = FIO_OPT_DEPRECATED,
1154         },
1155         {
1156                 .name   = "nice",
1157                 .type   = FIO_OPT_INT,
1158                 .off1   = td_var_offset(nice),
1159                 .help   = "Set job CPU nice value",
1160                 .minval = -19,
1161                 .maxval = 20,
1162                 .def    = "0",
1163         },
1164 #ifdef FIO_HAVE_IOPRIO
1165         {
1166                 .name   = "prio",
1167                 .type   = FIO_OPT_INT,
1168                 .cb     = str_prio_cb,
1169                 .help   = "Set job IO priority value",
1170                 .minval = 0,
1171                 .maxval = 7,
1172         },
1173         {
1174                 .name   = "prioclass",
1175                 .type   = FIO_OPT_INT,
1176                 .cb     = str_prioclass_cb,
1177                 .help   = "Set job IO priority class",
1178                 .minval = 0,
1179                 .maxval = 3,
1180         },
1181 #endif
1182         {
1183                 .name   = "thinktime",
1184                 .type   = FIO_OPT_INT,
1185                 .off1   = td_var_offset(thinktime),
1186                 .help   = "Idle time between IO buffers (usec)",
1187                 .def    = "0",
1188         },
1189         {
1190                 .name   = "thinktime_spin",
1191                 .type   = FIO_OPT_INT,
1192                 .off1   = td_var_offset(thinktime_spin),
1193                 .help   = "Start think time by spinning this amount (usec)",
1194                 .def    = "0",
1195                 .parent = "thinktime",
1196         },
1197         {
1198                 .name   = "thinktime_blocks",
1199                 .type   = FIO_OPT_INT,
1200                 .off1   = td_var_offset(thinktime_blocks),
1201                 .help   = "IO buffer period between 'thinktime'",
1202                 .def    = "1",
1203                 .parent = "thinktime",
1204         },
1205         {
1206                 .name   = "rate",
1207                 .type   = FIO_OPT_INT,
1208                 .off1   = td_var_offset(rate),
1209                 .help   = "Set bandwidth rate",
1210         },
1211         {
1212                 .name   = "ratemin",
1213                 .type   = FIO_OPT_INT,
1214                 .off1   = td_var_offset(ratemin),
1215                 .help   = "Job must meet this rate or it will be shutdown",
1216                 .parent = "rate",
1217         },
1218         {
1219                 .name   = "rate_iops",
1220                 .type   = FIO_OPT_INT,
1221                 .off1   = td_var_offset(rate_iops),
1222                 .help   = "Limit IO used to this number of IO operations/sec",
1223         },
1224         {
1225                 .name   = "rate_iops_min",
1226                 .type   = FIO_OPT_INT,
1227                 .off1   = td_var_offset(rate_iops_min),
1228                 .help   = "Job must meet this rate or it will be shutdown",
1229                 .parent = "rate_iops",
1230         },
1231         {
1232                 .name   = "ratecycle",
1233                 .type   = FIO_OPT_INT,
1234                 .off1   = td_var_offset(ratecycle),
1235                 .help   = "Window average for rate limits (msec)",
1236                 .def    = "1000",
1237                 .parent = "rate",
1238         },
1239         {
1240                 .name   = "invalidate",
1241                 .type   = FIO_OPT_BOOL,
1242                 .off1   = td_var_offset(invalidate_cache),
1243                 .help   = "Invalidate buffer/page cache prior to running job",
1244                 .def    = "1",
1245         },
1246         {
1247                 .name   = "sync",
1248                 .type   = FIO_OPT_BOOL,
1249                 .off1   = td_var_offset(sync_io),
1250                 .help   = "Use O_SYNC for buffered writes",
1251                 .def    = "0",
1252                 .parent = "buffered",
1253         },
1254         {
1255                 .name   = "bwavgtime",
1256                 .type   = FIO_OPT_INT,
1257                 .off1   = td_var_offset(bw_avg_time),
1258                 .help   = "Time window over which to calculate bandwidth"
1259                           " (msec)",
1260                 .def    = "500",
1261         },
1262         {
1263                 .name   = "create_serialize",
1264                 .type   = FIO_OPT_BOOL,
1265                 .off1   = td_var_offset(create_serialize),
1266                 .help   = "Serialize creating of job files",
1267                 .def    = "1",
1268         },
1269         {
1270                 .name   = "create_fsync",
1271                 .type   = FIO_OPT_BOOL,
1272                 .off1   = td_var_offset(create_fsync),
1273                 .help   = "Fsync file after creation",
1274                 .def    = "1",
1275         },
1276         {
1277                 .name   = "cpuload",
1278                 .type   = FIO_OPT_INT,
1279                 .off1   = td_var_offset(cpuload),
1280                 .help   = "Use this percentage of CPU",
1281         },
1282         {
1283                 .name   = "cpuchunks",
1284                 .type   = FIO_OPT_INT,
1285                 .off1   = td_var_offset(cpucycle),
1286                 .help   = "Length of the CPU burn cycles (usecs)",
1287                 .def    = "50000",
1288                 .parent = "cpuload",
1289         },
1290 #ifdef FIO_HAVE_CPU_AFFINITY
1291         {
1292                 .name   = "cpumask",
1293                 .type   = FIO_OPT_INT,
1294                 .cb     = str_cpumask_cb,
1295                 .help   = "CPU affinity mask",
1296         },
1297         {
1298                 .name   = "cpus_allowed",
1299                 .type   = FIO_OPT_STR,
1300                 .cb     = str_cpus_allowed_cb,
1301                 .help   = "Set CPUs allowed",
1302         },
1303 #endif
1304         {
1305                 .name   = "end_fsync",
1306                 .type   = FIO_OPT_BOOL,
1307                 .off1   = td_var_offset(end_fsync),
1308                 .help   = "Include fsync at the end of job",
1309                 .def    = "0",
1310         },
1311         {
1312                 .name   = "fsync_on_close",
1313                 .type   = FIO_OPT_BOOL,
1314                 .off1   = td_var_offset(fsync_on_close),
1315                 .help   = "fsync files on close",
1316                 .def    = "0",
1317         },
1318         {
1319                 .name   = "unlink",
1320                 .type   = FIO_OPT_BOOL,
1321                 .off1   = td_var_offset(unlink),
1322                 .help   = "Unlink created files after job has completed",
1323                 .def    = "0",
1324         },
1325         {
1326                 .name   = "exitall",
1327                 .type   = FIO_OPT_STR_SET,
1328                 .cb     = str_exitall_cb,
1329                 .help   = "Terminate all jobs when one exits",
1330         },
1331         {
1332                 .name   = "stonewall",
1333                 .type   = FIO_OPT_STR_SET,
1334                 .off1   = td_var_offset(stonewall),
1335                 .help   = "Insert a hard barrier between this job and previous",
1336         },
1337         {
1338                 .name   = "new_group",
1339                 .type   = FIO_OPT_STR_SET,
1340                 .off1   = td_var_offset(new_group),
1341                 .help   = "Mark the start of a new group (for reporting)",
1342         },
1343         {
1344                 .name   = "thread",
1345                 .type   = FIO_OPT_STR_SET,
1346                 .off1   = td_var_offset(use_thread),
1347                 .help   = "Use threads instead of forks",
1348         },
1349         {
1350                 .name   = "write_bw_log",
1351                 .type   = FIO_OPT_STR,
1352                 .off1   = td_var_offset(write_bw_log),
1353                 .cb     = str_write_bw_log_cb,
1354                 .help   = "Write log of bandwidth during run",
1355         },
1356         {
1357                 .name   = "write_lat_log",
1358                 .type   = FIO_OPT_STR,
1359                 .off1   = td_var_offset(write_lat_log),
1360                 .cb     = str_write_lat_log_cb,
1361                 .help   = "Write log of latency during run",
1362         },
1363         {
1364                 .name   = "hugepage-size",
1365                 .type   = FIO_OPT_STR_VAL_INT,
1366                 .off1   = td_var_offset(hugepage_size),
1367                 .help   = "When using hugepages, specify size of each page",
1368                 .def    = __stringify(FIO_HUGE_PAGE),
1369         },
1370         {
1371                 .name   = "group_reporting",
1372                 .type   = FIO_OPT_STR_SET,
1373                 .off1   = td_var_offset(group_reporting),
1374                 .help   = "Do reporting on a per-group basis",
1375         },
1376         {
1377                 .name   = "zero_buffers",
1378                 .type   = FIO_OPT_STR_SET,
1379                 .off1   = td_var_offset(zero_buffers),
1380                 .help   = "Init IO buffers to all zeroes",
1381         },
1382         {
1383                 .name   = "refill_buffers",
1384                 .type   = FIO_OPT_STR_SET,
1385                 .off1   = td_var_offset(refill_buffers),
1386                 .help   = "Refill IO buffers on every IO submit",
1387         },
1388 #ifdef FIO_HAVE_DISK_UTIL
1389         {
1390                 .name   = "disk_util",
1391                 .type   = FIO_OPT_BOOL,
1392                 .off1   = td_var_offset(do_disk_util),
1393                 .help   = "Log disk utilization statistics",
1394                 .def    = "1",
1395         },
1396 #endif
1397         {
1398                 .name   = "gtod_reduce",
1399                 .type   = FIO_OPT_BOOL,
1400                 .help   = "Greatly reduce number of gettimeofday() calls",
1401                 .cb     = str_gtod_reduce_cb,
1402                 .def    = "0",
1403         },
1404         {
1405                 .name   = "disable_clat",
1406                 .type   = FIO_OPT_BOOL,
1407                 .off1   = td_var_offset(disable_clat),
1408                 .help   = "Disable completion latency numbers",
1409                 .parent = "gtod_reduce",
1410                 .def    = "0",
1411         },
1412         {
1413                 .name   = "disable_slat",
1414                 .type   = FIO_OPT_BOOL,
1415                 .off1   = td_var_offset(disable_slat),
1416                 .help   = "Disable submissionn latency numbers",
1417                 .parent = "gtod_reduce",
1418                 .def    = "0",
1419         },
1420         {
1421                 .name   = "disable_bw_measurement",
1422                 .type   = FIO_OPT_BOOL,
1423                 .off1   = td_var_offset(disable_bw),
1424                 .help   = "Disable bandwidth logging",
1425                 .parent = "gtod_reduce",
1426                 .def    = "0",
1427         },
1428         {
1429                 .name   = "gtod_cpu",
1430                 .type   = FIO_OPT_INT,
1431                 .cb     = str_gtod_cpu_cb,
1432                 .help   = "Setup dedicated gettimeofday() thread on this CPU",
1433         },
1434         {
1435                 .name = NULL,
1436         },
1437 };
1438
1439 void fio_options_dup_and_init(struct option *long_options)
1440 {
1441         struct fio_option *o;
1442         unsigned int i;
1443
1444         options_init(options);
1445
1446         i = 0;
1447         while (long_options[i].name)
1448                 i++;
1449
1450         o = &options[0];
1451         while (o->name) {
1452                 long_options[i].name = (char *) o->name;
1453                 long_options[i].val = FIO_GETOPT_JOB;
1454                 if (o->type == FIO_OPT_STR_SET)
1455                         long_options[i].has_arg = no_argument;
1456                 else
1457                         long_options[i].has_arg = required_argument;
1458
1459                 i++;
1460                 o++;
1461                 assert(i < FIO_NR_OPTIONS);
1462         }
1463 }
1464
1465 int fio_options_parse(struct thread_data *td, char **opts, int num_opts)
1466 {
1467         int i, ret;
1468
1469         sort_options(opts, options, num_opts);
1470
1471         for (ret = 0, i = 0; i < num_opts; i++)
1472                 ret |= parse_option(opts[i], options, td);
1473
1474         return ret;
1475 }
1476
1477 int fio_cmd_option_parse(struct thread_data *td, const char *opt, char *val)
1478 {
1479         return parse_cmd_option(opt, val, options, td);
1480 }
1481
1482 void fio_fill_default_options(struct thread_data *td)
1483 {
1484         fill_default_options(td, options);
1485 }
1486
1487 int fio_show_option_help(const char *opt)
1488 {
1489         return show_cmd_help(options, opt);
1490 }
1491
1492 static void __options_mem(struct thread_data *td, int alloc)
1493 {
1494         struct thread_options *o = &td->o;
1495         struct fio_option *opt;
1496         char **ptr;
1497         int i;
1498
1499         for (i = 0, opt = &options[0]; opt->name; i++, opt = &options[i]) {
1500                 if (opt->type != FIO_OPT_STR_STORE)
1501                         continue;
1502
1503                 ptr = (void *) o + opt->off1;
1504                 if (*ptr) {
1505                         if (alloc)
1506                                 *ptr = strdup(*ptr);
1507                         else {
1508                                 free(*ptr);
1509                                 *ptr = NULL;
1510                         }
1511                 }
1512         }
1513 }
1514
1515 /*
1516  * dupe FIO_OPT_STR_STORE options
1517  */
1518 void options_mem_dupe(struct thread_data *td)
1519 {
1520         __options_mem(td, 1);
1521 }
1522
1523 void options_mem_free(struct thread_data fio_unused *td)
1524 {
1525 #if 0
1526         __options_mem(td, 0);
1527 #endif
1528 }