Split option handling out of init.c
[fio.git] / options.c
CommitLineData
214e1eca
JA
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
9#include "fio.h"
10#include "parse.h"
11
12#define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
13
14/*
15 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
16 */
17static char *get_opt_postfix(const char *str)
18{
19 char *p = strstr(str, ":");
20
21 if (!p)
22 return NULL;
23
24 p++;
25 strip_blank_front(&p);
26 strip_blank_end(p);
27 return strdup(p);
28}
29
30static int str_mem_cb(void *data, const char *mem)
31{
32 struct thread_data *td = data;
33
34 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
35 td->mmapfile = get_opt_postfix(mem);
36 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
37 log_err("fio: mmaphuge:/path/to/file\n");
38 return 1;
39 }
40 }
41
42 return 0;
43}
44
45static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
46{
47 mlock_size = *val;
48 return 0;
49}
50
51#ifdef FIO_HAVE_IOPRIO
52static int str_prioclass_cb(void *data, unsigned int *val)
53{
54 struct thread_data *td = data;
55
56 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
57 return 0;
58}
59
60static int str_prio_cb(void *data, unsigned int *val)
61{
62 struct thread_data *td = data;
63
64 td->ioprio |= *val;
65 return 0;
66}
67#endif
68
69static int str_exitall_cb(void)
70{
71 exitall_on_terminate = 1;
72 return 0;
73}
74
75static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
76{
77#ifdef FIO_HAVE_CPU_AFFINITY
78 unsigned int i;
79
80 CPU_ZERO(&cpumask);
81
82 for (i = 0; i < sizeof(int) * 8; i++) {
83 if ((1 << i) & cpu)
84 CPU_SET(i, &cpumask);
85 }
86#endif
87}
88
89static int str_cpumask_cb(void *data, unsigned int *val)
90{
91 struct thread_data *td = data;
92
93 fill_cpu_mask(td->cpumask, *val);
94 return 0;
95}
96
97static int str_fst_cb(void *data, const char *str)
98{
99 struct thread_data *td = data;
100 char *nr = get_opt_postfix(str);
101
102 td->file_service_nr = 1;
103 if (nr)
104 td->file_service_nr = atoi(nr);
105
106 return 0;
107}
108
109static int str_filename_cb(void *data, const char *input)
110{
111 struct thread_data *td = data;
112 char *fname, *str, *p;
113
114 p = str = strdup(input);
115
116 strip_blank_front(&str);
117 strip_blank_end(str);
118
119 if (!td->files_index)
120 td->nr_files = 0;
121
122 while ((fname = strsep(&str, ":")) != NULL) {
123 if (!strlen(fname))
124 break;
125 add_file(td, fname);
126 td->nr_files++;
127 }
128
129 free(p);
130 return 0;
131}
132
133static int str_directory_cb(void *data, const char fio_unused *str)
134{
135 struct thread_data *td = data;
136 struct stat sb;
137
138 if (lstat(td->directory, &sb) < 0) {
139 log_err("fio: %s is not a directory\n", td->directory);
140 td_verror(td, errno, "lstat");
141 return 1;
142 }
143 if (!S_ISDIR(sb.st_mode)) {
144 log_err("fio: %s is not a directory\n", td->directory);
145 return 1;
146 }
147
148 return 0;
149}
150
151static int str_opendir_cb(void *data, const char fio_unused *str)
152{
153 struct thread_data *td = data;
154
155 if (!td->files_index)
156 td->nr_files = 0;
157
158 return add_dir_files(td, td->opendir);
159}
160
161
162#define __stringify_1(x) #x
163#define __stringify(x) __stringify_1(x)
164
165/*
166 * Map of job/command line options
167 */
168static struct fio_option options[] = {
169 {
170 .name = "description",
171 .type = FIO_OPT_STR_STORE,
172 .off1 = td_var_offset(description),
173 .help = "Text job description",
174 },
175 {
176 .name = "name",
177 .type = FIO_OPT_STR_STORE,
178 .off1 = td_var_offset(name),
179 .help = "Name of this job",
180 },
181 {
182 .name = "directory",
183 .type = FIO_OPT_STR_STORE,
184 .off1 = td_var_offset(directory),
185 .cb = str_directory_cb,
186 .help = "Directory to store files in",
187 },
188 {
189 .name = "filename",
190 .type = FIO_OPT_STR_STORE,
191 .off1 = td_var_offset(filename),
192 .cb = str_filename_cb,
193 .help = "File(s) to use for the workload",
194 },
195 {
196 .name = "opendir",
197 .type = FIO_OPT_STR_STORE,
198 .off1 = td_var_offset(opendir),
199 .cb = str_opendir_cb,
200 .help = "Recursively add files from this directory and down",
201 },
202 {
203 .name = "rw",
204 .type = FIO_OPT_STR,
205 .off1 = td_var_offset(td_ddir),
206 .help = "IO direction",
207 .def = "read",
208 .posval = {
209 { .ival = "read",
210 .oval = TD_DDIR_READ,
211 .help = "Sequential read",
212 },
213 { .ival = "write",
214 .oval = TD_DDIR_WRITE,
215 .help = "Sequential write",
216 },
217 { .ival = "randread",
218 .oval = TD_DDIR_RANDREAD,
219 .help = "Random read",
220 },
221 { .ival = "randwrite",
222 .oval = TD_DDIR_RANDWRITE,
223 .help = "Random write",
224 },
225 { .ival = "rw",
226 .oval = TD_DDIR_RW,
227 .help = "Sequential read and write mix",
228 },
229 { .ival = "randrw",
230 .oval = TD_DDIR_RANDRW,
231 .help = "Random read and write mix"
232 },
233 },
234 },
235 {
236 .name = "ioengine",
237 .type = FIO_OPT_STR_STORE,
238 .off1 = td_var_offset(ioengine),
239 .help = "IO engine to use",
240 .def = "sync",
241 .posval = {
242 { .ival = "sync",
243 .help = "Use read/write",
244 },
245#ifdef FIO_HAVE_LIBAIO
246 { .ival = "libaio",
247 .help = "Linux native asynchronous IO",
248 },
249#endif
250#ifdef FIO_HAVE_POSIXAIO
251 { .ival = "posixaio",
252 .help = "POSIX asynchronous IO",
253 },
254#endif
255 { .ival = "mmap",
256 .help = "Memory mapped IO",
257 },
258#ifdef FIO_HAVE_SPLICE
259 { .ival = "splice",
260 .help = "splice/vmsplice based IO",
261 },
262#endif
263#ifdef FIO_HAVE_SGIO
264 { .ival = "sg",
265 .help = "SCSI generic v3 IO",
266 },
267#endif
268 { .ival = "null",
269 .help = "Testing engine (no data transfer)",
270 },
271 { .ival = "net",
272 .help = "Network IO",
273 },
274#ifdef FIO_HAVE_SYSLET
275 { .ival = "syslet-rw",
276 .help = "syslet enabled async pread/pwrite IO",
277 },
278#endif
279 { .ival = "cpuio",
280 .help = "CPU cycler burner engine",
281 },
282 { .ival = "external",
283 .help = "Load external engine (append name)",
284 },
285 },
286 },
287 {
288 .name = "iodepth",
289 .type = FIO_OPT_INT,
290 .off1 = td_var_offset(iodepth),
291 .help = "Amount of IO buffers to keep in flight",
292 .def = "1",
293 },
294 {
295 .name = "iodepth_batch",
296 .type = FIO_OPT_INT,
297 .off1 = td_var_offset(iodepth_batch),
298 .help = "Number of IO to submit in one go",
299 },
300 {
301 .name = "iodepth_low",
302 .type = FIO_OPT_INT,
303 .off1 = td_var_offset(iodepth_low),
304 .help = "Low water mark for queuing depth",
305 },
306 {
307 .name = "size",
308 .type = FIO_OPT_STR_VAL,
309 .off1 = td_var_offset(total_file_size),
310 .help = "Total size of device or files",
311 },
312 {
313 .name = "filesize",
314 .type = FIO_OPT_STR_VAL,
315 .off1 = td_var_offset(file_size_low),
316 .off2 = td_var_offset(file_size_high),
317 .help = "Size of individual files",
318 },
319 {
320 .name = "bs",
321 .type = FIO_OPT_STR_VAL_INT,
322 .off1 = td_var_offset(bs[DDIR_READ]),
323 .off2 = td_var_offset(bs[DDIR_WRITE]),
324 .help = "Block size unit",
325 .def = "4k",
326 },
327 {
328 .name = "bsrange",
329 .type = FIO_OPT_RANGE,
330 .off1 = td_var_offset(min_bs[DDIR_READ]),
331 .off2 = td_var_offset(max_bs[DDIR_READ]),
332 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
333 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
334 .help = "Set block size range (in more detail than bs)",
335 },
336 {
337 .name = "bs_unaligned",
338 .type = FIO_OPT_STR_SET,
339 .off1 = td_var_offset(bs_unaligned),
340 .help = "Don't sector align IO buffer sizes",
341 },
342 {
343 .name = "offset",
344 .type = FIO_OPT_STR_VAL,
345 .off1 = td_var_offset(start_offset),
346 .help = "Start IO from this offset",
347 .def = "0",
348 },
349 {
350 .name = "randrepeat",
351 .type = FIO_OPT_BOOL,
352 .off1 = td_var_offset(rand_repeatable),
353 .help = "Use repeatable random IO pattern",
354 .def = "1",
355 },
356 {
357 .name = "norandommap",
358 .type = FIO_OPT_STR_SET,
359 .off1 = td_var_offset(norandommap),
360 .help = "Accept potential duplicate random blocks",
361 },
362 {
363 .name = "nrfiles",
364 .type = FIO_OPT_INT,
365 .off1 = td_var_offset(nr_files),
366 .help = "Split job workload between this number of files",
367 .def = "1",
368 },
369 {
370 .name = "openfiles",
371 .type = FIO_OPT_INT,
372 .off1 = td_var_offset(open_files),
373 .help = "Number of files to keep open at the same time",
374 },
375 {
376 .name = "file_service_type",
377 .type = FIO_OPT_STR,
378 .cb = str_fst_cb,
379 .off1 = td_var_offset(file_service_type),
380 .help = "How to select which file to service next",
381 .def = "roundrobin",
382 .posval = {
383 { .ival = "random",
384 .oval = FIO_FSERVICE_RANDOM,
385 .help = "Choose a file at random",
386 },
387 { .ival = "roundrobin",
388 .oval = FIO_FSERVICE_RR,
389 .help = "Round robin select files",
390 },
391 },
392 },
393 {
394 .name = "fsync",
395 .type = FIO_OPT_INT,
396 .off1 = td_var_offset(fsync_blocks),
397 .help = "Issue fsync for writes every given number of blocks",
398 .def = "0",
399 },
400 {
401 .name = "direct",
402 .type = FIO_OPT_BOOL,
403 .off1 = td_var_offset(odirect),
404 .help = "Use O_DIRECT IO (negates buffered)",
405 .def = "0",
406 },
407 {
408 .name = "buffered",
409 .type = FIO_OPT_BOOL,
410 .off1 = td_var_offset(odirect),
411 .neg = 1,
412 .help = "Use buffered IO (negates direct)",
413 .def = "1",
414 },
415 {
416 .name = "overwrite",
417 .type = FIO_OPT_BOOL,
418 .off1 = td_var_offset(overwrite),
419 .help = "When writing, set whether to overwrite current data",
420 .def = "0",
421 },
422 {
423 .name = "loops",
424 .type = FIO_OPT_INT,
425 .off1 = td_var_offset(loops),
426 .help = "Number of times to run the job",
427 .def = "1",
428 },
429 {
430 .name = "numjobs",
431 .type = FIO_OPT_INT,
432 .off1 = td_var_offset(numjobs),
433 .help = "Duplicate this job this many times",
434 .def = "1",
435 },
436 {
437 .name = "startdelay",
438 .type = FIO_OPT_INT,
439 .off1 = td_var_offset(start_delay),
440 .help = "Only start job when this period has passed",
441 .def = "0",
442 },
443 {
444 .name = "runtime",
445 .alias = "timeout",
446 .type = FIO_OPT_STR_VAL_TIME,
447 .off1 = td_var_offset(timeout),
448 .help = "Stop workload when this amount of time has passed",
449 .def = "0",
450 },
451 {
452 .name = "mem",
453 .type = FIO_OPT_STR,
454 .cb = str_mem_cb,
455 .off1 = td_var_offset(mem_type),
456 .help = "Backing type for IO buffers",
457 .def = "malloc",
458 .posval = {
459 { .ival = "malloc",
460 .oval = MEM_MALLOC,
461 .help = "Use malloc(3) for IO buffers",
462 },
463 { .ival = "shm",
464 .oval = MEM_SHM,
465 .help = "Use shared memory segments for IO buffers",
466 },
467#ifdef FIO_HAVE_HUGETLB
468 { .ival = "shmhuge",
469 .oval = MEM_SHMHUGE,
470 .help = "Like shm, but use huge pages",
471 },
472#endif
473 { .ival = "mmap",
474 .oval = MEM_MMAP,
475 .help = "Use mmap(2) (file or anon) for IO buffers",
476 },
477#ifdef FIO_HAVE_HUGETLB
478 { .ival = "mmaphuge",
479 .oval = MEM_MMAPHUGE,
480 .help = "Like mmap, but use huge pages",
481 },
482#endif
483 },
484 },
485 {
486 .name = "verify",
487 .type = FIO_OPT_STR,
488 .off1 = td_var_offset(verify),
489 .help = "Verify data written",
490 .def = "0",
491 .posval = {
492 { .ival = "0",
493 .oval = VERIFY_NONE,
494 .help = "Don't do IO verification",
495 },
496 { .ival = "crc32",
497 .oval = VERIFY_CRC32,
498 .help = "Use crc32 checksums for verification",
499 },
500 { .ival = "md5",
501 .oval = VERIFY_MD5,
502 .help = "Use md5 checksums for verification",
503 },
504 },
505 },
506 {
507 .name = "write_iolog",
508 .type = FIO_OPT_STR_STORE,
509 .off1 = td_var_offset(write_iolog_file),
510 .help = "Store IO pattern to file",
511 },
512 {
513 .name = "read_iolog",
514 .type = FIO_OPT_STR_STORE,
515 .off1 = td_var_offset(read_iolog_file),
516 .help = "Playback IO pattern from file",
517 },
518 {
519 .name = "exec_prerun",
520 .type = FIO_OPT_STR_STORE,
521 .off1 = td_var_offset(exec_prerun),
522 .help = "Execute this file prior to running job",
523 },
524 {
525 .name = "exec_postrun",
526 .type = FIO_OPT_STR_STORE,
527 .off1 = td_var_offset(exec_postrun),
528 .help = "Execute this file after running job",
529 },
530#ifdef FIO_HAVE_IOSCHED_SWITCH
531 {
532 .name = "ioscheduler",
533 .type = FIO_OPT_STR_STORE,
534 .off1 = td_var_offset(ioscheduler),
535 .help = "Use this IO scheduler on the backing device",
536 },
537#endif
538 {
539 .name = "zonesize",
540 .type = FIO_OPT_STR_VAL,
541 .off1 = td_var_offset(zone_size),
542 .help = "Give size of an IO zone",
543 .def = "0",
544 },
545 {
546 .name = "zoneskip",
547 .type = FIO_OPT_STR_VAL,
548 .off1 = td_var_offset(zone_skip),
549 .help = "Space between IO zones",
550 .def = "0",
551 },
552 {
553 .name = "lockmem",
554 .type = FIO_OPT_STR_VAL,
555 .cb = str_lockmem_cb,
556 .help = "Lock down this amount of memory",
557 .def = "0",
558 },
559 {
560 .name = "rwmixcycle",
561 .type = FIO_OPT_INT,
562 .off1 = td_var_offset(rwmixcycle),
563 .help = "Cycle period for mixed read/write workloads (msec)",
564 .def = "500",
565 },
566 {
567 .name = "rwmixread",
568 .type = FIO_OPT_INT,
569 .off1 = td_var_offset(rwmixread),
570 .maxval = 100,
571 .help = "Percentage of mixed workload that is reads",
572 .def = "50",
573 },
574 {
575 .name = "rwmixwrite",
576 .type = FIO_OPT_INT,
577 .off1 = td_var_offset(rwmixwrite),
578 .maxval = 100,
579 .help = "Percentage of mixed workload that is writes",
580 .def = "50",
581 },
582 {
583 .name = "nice",
584 .type = FIO_OPT_INT,
585 .off1 = td_var_offset(nice),
586 .help = "Set job CPU nice value",
587 .minval = -19,
588 .maxval = 20,
589 .def = "0",
590 },
591#ifdef FIO_HAVE_IOPRIO
592 {
593 .name = "prio",
594 .type = FIO_OPT_INT,
595 .cb = str_prio_cb,
596 .help = "Set job IO priority value",
597 .minval = 0,
598 .maxval = 7,
599 },
600 {
601 .name = "prioclass",
602 .type = FIO_OPT_INT,
603 .cb = str_prioclass_cb,
604 .help = "Set job IO priority class",
605 .minval = 0,
606 .maxval = 3,
607 },
608#endif
609 {
610 .name = "thinktime",
611 .type = FIO_OPT_INT,
612 .off1 = td_var_offset(thinktime),
613 .help = "Idle time between IO buffers (usec)",
614 .def = "0",
615 },
616 {
617 .name = "thinktime_spin",
618 .type = FIO_OPT_INT,
619 .off1 = td_var_offset(thinktime_spin),
620 .help = "Start think time by spinning this amount (usec)",
621 .def = "0",
622 },
623 {
624 .name = "thinktime_blocks",
625 .type = FIO_OPT_INT,
626 .off1 = td_var_offset(thinktime_blocks),
627 .help = "IO buffer period between 'thinktime'",
628 .def = "1",
629 },
630 {
631 .name = "rate",
632 .type = FIO_OPT_INT,
633 .off1 = td_var_offset(rate),
634 .help = "Set bandwidth rate",
635 },
636 {
637 .name = "ratemin",
638 .type = FIO_OPT_INT,
639 .off1 = td_var_offset(ratemin),
640 .help = "The bottom limit accepted",
641 },
642 {
643 .name = "ratecycle",
644 .type = FIO_OPT_INT,
645 .off1 = td_var_offset(ratecycle),
646 .help = "Window average for rate limits (msec)",
647 .def = "1000",
648 },
649 {
650 .name = "invalidate",
651 .type = FIO_OPT_BOOL,
652 .off1 = td_var_offset(invalidate_cache),
653 .help = "Invalidate buffer/page cache prior to running job",
654 .def = "1",
655 },
656 {
657 .name = "sync",
658 .type = FIO_OPT_BOOL,
659 .off1 = td_var_offset(sync_io),
660 .help = "Use O_SYNC for buffered writes",
661 .def = "0",
662 },
663 {
664 .name = "bwavgtime",
665 .type = FIO_OPT_INT,
666 .off1 = td_var_offset(bw_avg_time),
667 .help = "Time window over which to calculate bandwidth (msec)",
668 .def = "500",
669 },
670 {
671 .name = "create_serialize",
672 .type = FIO_OPT_BOOL,
673 .off1 = td_var_offset(create_serialize),
674 .help = "Serialize creating of job files",
675 .def = "1",
676 },
677 {
678 .name = "create_fsync",
679 .type = FIO_OPT_BOOL,
680 .off1 = td_var_offset(create_fsync),
681 .help = "Fsync file after creation",
682 .def = "1",
683 },
684 {
685 .name = "cpuload",
686 .type = FIO_OPT_INT,
687 .off1 = td_var_offset(cpuload),
688 .help = "Use this percentage of CPU",
689 },
690 {
691 .name = "cpuchunks",
692 .type = FIO_OPT_INT,
693 .off1 = td_var_offset(cpucycle),
694 .help = "Length of the CPU burn cycles (usecs)",
695 .def = "50000",
696 },
697#ifdef FIO_HAVE_CPU_AFFINITY
698 {
699 .name = "cpumask",
700 .type = FIO_OPT_INT,
701 .cb = str_cpumask_cb,
702 .help = "CPU affinity mask",
703 },
704#endif
705 {
706 .name = "end_fsync",
707 .type = FIO_OPT_BOOL,
708 .off1 = td_var_offset(end_fsync),
709 .help = "Include fsync at the end of job",
710 .def = "0",
711 },
712 {
713 .name = "fsync_on_close",
714 .type = FIO_OPT_BOOL,
715 .off1 = td_var_offset(fsync_on_close),
716 .help = "fsync files on close",
717 .def = "0",
718 },
719 {
720 .name = "unlink",
721 .type = FIO_OPT_BOOL,
722 .off1 = td_var_offset(unlink),
723 .help = "Unlink created files after job has completed",
724 .def = "0",
725 },
726 {
727 .name = "exitall",
728 .type = FIO_OPT_STR_SET,
729 .cb = str_exitall_cb,
730 .help = "Terminate all jobs when one exits",
731 },
732 {
733 .name = "stonewall",
734 .type = FIO_OPT_STR_SET,
735 .off1 = td_var_offset(stonewall),
736 .help = "Insert a hard barrier between this job and previous",
737 },
738 {
739 .name = "thread",
740 .type = FIO_OPT_STR_SET,
741 .off1 = td_var_offset(use_thread),
742 .help = "Use threads instead of forks",
743 },
744 {
745 .name = "write_bw_log",
746 .type = FIO_OPT_STR_SET,
747 .off1 = td_var_offset(write_bw_log),
748 .help = "Write log of bandwidth during run",
749 },
750 {
751 .name = "write_lat_log",
752 .type = FIO_OPT_STR_SET,
753 .off1 = td_var_offset(write_lat_log),
754 .help = "Write log of latency during run",
755 },
756 {
757 .name = "hugepage-size",
758 .type = FIO_OPT_STR_VAL,
759 .off1 = td_var_offset(hugepage_size),
760 .help = "When using hugepages, specify size of each page",
761 .def = __stringify(FIO_HUGE_PAGE),
762 },
763 {
764 .name = "group_reporting",
765 .type = FIO_OPT_STR_SET,
766 .off1 = td_var_offset(group_reporting),
767 .help = "Do reporting on a per-group basis",
768 },
769 {
770 .name = NULL,
771 },
772};
773
774void fio_options_dup_and_init(struct option *long_options)
775{
776 struct fio_option *o;
777 unsigned int i;
778
779 options_init(options);
780
781 i = 0;
782 while (long_options[i].name)
783 i++;
784
785 o = &options[0];
786 while (o->name) {
787 long_options[i].name = o->name;
788 long_options[i].val = FIO_GETOPT_JOB;
789 if (o->type == FIO_OPT_STR_SET)
790 long_options[i].has_arg = no_argument;
791 else
792 long_options[i].has_arg = required_argument;
793
794 i++;
795 o++;
796 assert(i < FIO_NR_OPTIONS);
797 }
798}
799
800int fio_option_parse(struct thread_data *td, const char *opt)
801{
802 return parse_option(opt, options, td);
803}
804
805int fio_cmd_option_parse(struct thread_data *td, const char *opt, char *val)
806{
807 return parse_cmd_option(opt, val, options, td);
808}
809
810void fio_fill_default_options(struct thread_data *td)
811{
812 fill_default_options(td, options);
813}
814
815int fio_show_option_help(const char *opt)
816{
817 return show_cmd_help(options, opt);
818}