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