Don't include engines we don't have
[fio.git] / init.c
... / ...
CommitLineData
1/*
2 * This file contains job initialization and setup functions.
3 */
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <ctype.h>
9#include <string.h>
10#include <errno.h>
11#include <getopt.h>
12#include <assert.h>
13#include <sys/ipc.h>
14#include <sys/shm.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17
18#include "fio.h"
19#include "parse.h"
20
21#define FIO_RANDSEED (0xb1899bedUL)
22
23#define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
24
25static int str_ioengine_cb(void *, const char *);
26static int str_mem_cb(void *, const char *);
27static int str_lockmem_cb(void *, unsigned long *);
28#ifdef FIO_HAVE_IOPRIO
29static int str_prio_cb(void *, unsigned int *);
30static int str_prioclass_cb(void *, unsigned int *);
31#endif
32static int str_exitall_cb(void);
33static int str_cpumask_cb(void *, unsigned int *);
34
35#define __stringify_1(x) #x
36#define __stringify(x) __stringify_1(x)
37
38/*
39 * Map of job/command line options
40 */
41static struct fio_option options[] = {
42 {
43 .name = "description",
44 .type = FIO_OPT_STR_STORE,
45 .off1 = td_var_offset(description),
46 .help = "Text job description",
47 },
48 {
49 .name = "name",
50 .type = FIO_OPT_STR_STORE,
51 .off1 = td_var_offset(name),
52 .help = "Name of this job",
53 },
54 {
55 .name = "directory",
56 .type = FIO_OPT_STR_STORE,
57 .off1 = td_var_offset(directory),
58 .help = "Directory to store files in",
59 },
60 {
61 .name = "filename",
62 .type = FIO_OPT_STR_STORE,
63 .off1 = td_var_offset(filename),
64 .help = "Force the use of a specific file",
65 },
66 {
67 .name = "rw",
68 .type = FIO_OPT_STR,
69 .off1 = td_var_offset(td_ddir),
70 .help = "IO direction",
71 .def = "read",
72 .posval = {
73 { .ival = "read", .oval = TD_DDIR_READ },
74 { .ival = "write", .oval = TD_DDIR_WRITE },
75 { .ival = "randread", .oval = TD_DDIR_RANDREAD },
76 { .ival = "randwrite", .oval = TD_DDIR_RANDWRITE },
77 { .ival = "rw", .oval = TD_DDIR_RW },
78 { .ival = "randrw", .oval = TD_DDIR_RANDRW },
79 },
80 },
81 {
82 .name = "ioengine",
83 .type = FIO_OPT_STR,
84 .cb = str_ioengine_cb,
85 .help = "IO engine to use",
86 .def = "sync",
87 .posval = {
88 { .ival = "sync", },
89#ifdef FIO_HAVE_LIBAIO
90 { .ival = "libaio", },
91#endif
92#ifdef FIO_HAVE_POSIXAIO
93 { .ival = "posixaio", },
94#endif
95 { .ival = "mmap", },
96#ifdef FIO_HAVE_SPLICE
97 { .ival = "splice", },
98#endif
99#ifdef FIO_HAVE_SGIO
100 { .ival = "sg", },
101#endif
102 { .ival = "null", }, { .ival = "net", },
103#ifdef FIO_HAVE_SYSLET
104 { .ival = "syslet-rw", },
105#endif
106 },
107 },
108 {
109 .name = "iodepth",
110 .type = FIO_OPT_INT,
111 .off1 = td_var_offset(iodepth),
112 .help = "Amount of IO buffers to keep in flight",
113 .def = "1",
114 },
115 {
116 .name = "iodepth_low",
117 .type = FIO_OPT_INT,
118 .off1 = td_var_offset(iodepth_low),
119 .help = "Low water mark for queuing depth",
120 },
121 {
122 .name = "size",
123 .type = FIO_OPT_STR_VAL,
124 .off1 = td_var_offset(total_file_size),
125 .help = "Size of device or file",
126 },
127 {
128 .name = "bs",
129 .type = FIO_OPT_STR_VAL_INT,
130 .off1 = td_var_offset(bs[DDIR_READ]),
131 .off2 = td_var_offset(bs[DDIR_WRITE]),
132 .help = "Block size unit",
133 .def = "4k",
134 },
135 {
136 .name = "bsrange",
137 .type = FIO_OPT_RANGE,
138 .off1 = td_var_offset(min_bs[DDIR_READ]),
139 .off2 = td_var_offset(max_bs[DDIR_READ]),
140 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
141 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
142 .help = "Set block size range (in more detail than bs)",
143 },
144 {
145 .name = "bs_unaligned",
146 .type = FIO_OPT_STR_SET,
147 .off1 = td_var_offset(bs_unaligned),
148 .help = "Don't sector align IO buffer sizes",
149 },
150 {
151 .name = "offset",
152 .type = FIO_OPT_STR_VAL,
153 .off1 = td_var_offset(start_offset),
154 .help = "Start IO from this offset",
155 .def = "0",
156 },
157 {
158 .name = "randrepeat",
159 .type = FIO_OPT_BOOL,
160 .off1 = td_var_offset(rand_repeatable),
161 .help = "Use repeatable random IO pattern",
162 .def = "1",
163 },
164 {
165 .name = "norandommap",
166 .type = FIO_OPT_STR_SET,
167 .off1 = td_var_offset(norandommap),
168 .help = "Accept potential duplicate random blocks",
169 },
170 {
171 .name = "nrfiles",
172 .type = FIO_OPT_INT,
173 .off1 = td_var_offset(nr_files),
174 .help = "Split job workload between this number of files",
175 .def = "1",
176 },
177 {
178 .name = "file_service_type",
179 .type = FIO_OPT_STR,
180 .off1 = td_var_offset(file_service_type),
181 .help = "How to select which file to service next",
182 .def = "roundrobin",
183 .posval = {
184 { .ival = "random", .oval = FIO_FSERVICE_RANDOM },
185 { .ival = "roundrobin", .oval = FIO_FSERVICE_RR },
186 },
187 },
188 {
189 .name = "fsync",
190 .type = FIO_OPT_INT,
191 .off1 = td_var_offset(fsync_blocks),
192 .help = "Issue fsync for writes every given number of blocks",
193 .def = "0",
194 },
195 {
196 .name = "direct",
197 .type = FIO_OPT_BOOL,
198 .off1 = td_var_offset(odirect),
199 .help = "Use O_DIRECT IO (negates buffered)",
200 .def = "0",
201 },
202 {
203 .name = "buffered",
204 .type = FIO_OPT_BOOL,
205 .off1 = td_var_offset(odirect),
206 .neg = 1,
207 .help = "Use buffered IO (negates direct)",
208 .def = "1",
209 },
210 {
211 .name = "overwrite",
212 .type = FIO_OPT_BOOL,
213 .off1 = td_var_offset(overwrite),
214 .help = "When writing, set whether to overwrite current data",
215 .def = "0",
216 },
217 {
218 .name = "loops",
219 .type = FIO_OPT_INT,
220 .off1 = td_var_offset(loops),
221 .help = "Number of times to run the job",
222 .def = "1",
223 },
224 {
225 .name = "numjobs",
226 .type = FIO_OPT_INT,
227 .off1 = td_var_offset(numjobs),
228 .help = "Duplicate this job this many times",
229 .def = "1",
230 },
231 {
232 .name = "startdelay",
233 .type = FIO_OPT_INT,
234 .off1 = td_var_offset(start_delay),
235 .help = "Only start job when this period has passed",
236 .def = "0",
237 },
238 {
239 .name = "runtime",
240 .alias = "timeout",
241 .type = FIO_OPT_STR_VAL_TIME,
242 .off1 = td_var_offset(timeout),
243 .help = "Stop workload when this amount of time has passed",
244 .def = "0",
245 },
246 {
247 .name = "mem",
248 .type = FIO_OPT_STR,
249 .cb = str_mem_cb,
250 .off1 = td_var_offset(mem_type),
251 .help = "Backing type for IO buffers",
252 .def = "malloc",
253 .posval = {
254 { .ival = "malloc", .oval = MEM_MALLOC },
255 { .ival = "shm", .oval = MEM_SHM },
256#ifdef FIO_HAVE_HUGETLB
257 { .ival = "shmhuge", .oval = MEM_SHMHUGE },
258#endif
259 { .ival = "mmap", .oval = MEM_MMAP },
260#ifdef FIO_HAVE_HUGETLB
261 { .ival = "mmaphuge", .oval = MEM_MMAPHUGE },
262#endif
263 },
264 },
265 {
266 .name = "verify",
267 .type = FIO_OPT_STR,
268 .off1 = td_var_offset(verify),
269 .help = "Verify sum function",
270 .def = "0",
271 .posval = {
272 { .ival = "0", .oval = VERIFY_NONE },
273 { .ival = "crc32", .oval = VERIFY_CRC32 },
274 { .ival = "md5", .oval = VERIFY_MD5 },
275 },
276 },
277 {
278 .name = "write_iolog",
279 .type = FIO_OPT_STR_STORE,
280 .off1 = td_var_offset(write_iolog_file),
281 .help = "Store IO pattern to file",
282 },
283 {
284 .name = "read_iolog",
285 .type = FIO_OPT_STR_STORE,
286 .off1 = td_var_offset(read_iolog_file),
287 .help = "Playback IO pattern from file",
288 },
289 {
290 .name = "exec_prerun",
291 .type = FIO_OPT_STR_STORE,
292 .off1 = td_var_offset(exec_prerun),
293 .help = "Execute this file prior to running job",
294 },
295 {
296 .name = "exec_postrun",
297 .type = FIO_OPT_STR_STORE,
298 .off1 = td_var_offset(exec_postrun),
299 .help = "Execute this file after running job",
300 },
301#ifdef FIO_HAVE_IOSCHED_SWITCH
302 {
303 .name = "ioscheduler",
304 .type = FIO_OPT_STR_STORE,
305 .off1 = td_var_offset(ioscheduler),
306 .help = "Use this IO scheduler on the backing device",
307 },
308#endif
309 {
310 .name = "zonesize",
311 .type = FIO_OPT_STR_VAL,
312 .off1 = td_var_offset(zone_size),
313 .help = "Give size of an IO zone",
314 .def = "0",
315 },
316 {
317 .name = "zoneskip",
318 .type = FIO_OPT_STR_VAL,
319 .off1 = td_var_offset(zone_skip),
320 .help = "Space between IO zones",
321 .def = "0",
322 },
323 {
324 .name = "lockmem",
325 .type = FIO_OPT_STR_VAL,
326 .cb = str_lockmem_cb,
327 .help = "Lock down this amount of memory",
328 .def = "0",
329 },
330 {
331 .name = "rwmixcycle",
332 .type = FIO_OPT_INT,
333 .off1 = td_var_offset(rwmixcycle),
334 .help = "Cycle period for mixed read/write workloads (msec)",
335 .def = "500",
336 },
337 {
338 .name = "rwmixread",
339 .type = FIO_OPT_INT,
340 .off1 = td_var_offset(rwmixread),
341 .maxval = 100,
342 .help = "Percentage of mixed workload that is reads",
343 .def = "50",
344 },
345 {
346 .name = "rwmixwrite",
347 .type = FIO_OPT_INT,
348 .off1 = td_var_offset(rwmixwrite),
349 .maxval = 100,
350 .help = "Percentage of mixed workload that is writes",
351 .def = "50",
352 },
353 {
354 .name = "nice",
355 .type = FIO_OPT_INT,
356 .off1 = td_var_offset(nice),
357 .help = "Set job CPU nice value",
358 .minval = -19,
359 .maxval = 20,
360 .def = "0",
361 },
362#ifdef FIO_HAVE_IOPRIO
363 {
364 .name = "prio",
365 .type = FIO_OPT_INT,
366 .cb = str_prio_cb,
367 .help = "Set job IO priority value",
368 .minval = 0,
369 .maxval = 7,
370 },
371 {
372 .name = "prioclass",
373 .type = FIO_OPT_INT,
374 .cb = str_prioclass_cb,
375 .help = "Set job IO priority class",
376 .minval = 0,
377 .maxval = 3,
378 },
379#endif
380 {
381 .name = "thinktime",
382 .type = FIO_OPT_INT,
383 .off1 = td_var_offset(thinktime),
384 .help = "Idle time between IO buffers (usec)",
385 .def = "0",
386 },
387 {
388 .name = "thinktime_spin",
389 .type = FIO_OPT_INT,
390 .off1 = td_var_offset(thinktime_spin),
391 .help = "Start thinktime by spinning this amount (usec)",
392 .def = "0",
393 },
394 {
395 .name = "thinktime_blocks",
396 .type = FIO_OPT_INT,
397 .off1 = td_var_offset(thinktime_blocks),
398 .help = "IO buffer period between 'thinktime'",
399 .def = "1",
400 },
401 {
402 .name = "rate",
403 .type = FIO_OPT_INT,
404 .off1 = td_var_offset(rate),
405 .help = "Set bandwidth rate",
406 },
407 {
408 .name = "ratemin",
409 .type = FIO_OPT_INT,
410 .off1 = td_var_offset(ratemin),
411 .help = "The bottom limit accepted",
412 },
413 {
414 .name = "ratecycle",
415 .type = FIO_OPT_INT,
416 .off1 = td_var_offset(ratecycle),
417 .help = "Window average for rate limits (msec)",
418 .def = "1000",
419 },
420 {
421 .name = "invalidate",
422 .type = FIO_OPT_BOOL,
423 .off1 = td_var_offset(invalidate_cache),
424 .help = "Invalidate buffer/page cache prior to running job",
425 .def = "1",
426 },
427 {
428 .name = "sync",
429 .type = FIO_OPT_BOOL,
430 .off1 = td_var_offset(sync_io),
431 .help = "Use O_SYNC for buffered writes",
432 .def = "0",
433 },
434 {
435 .name = "bwavgtime",
436 .type = FIO_OPT_INT,
437 .off1 = td_var_offset(bw_avg_time),
438 .help = "Time window over which to calculate bandwidth (msec)",
439 .def = "500",
440 },
441 {
442 .name = "create_serialize",
443 .type = FIO_OPT_BOOL,
444 .off1 = td_var_offset(create_serialize),
445 .help = "Serialize creating of job files",
446 .def = "1",
447 },
448 {
449 .name = "create_fsync",
450 .type = FIO_OPT_BOOL,
451 .off1 = td_var_offset(create_fsync),
452 .help = "Fsync file after creation",
453 .def = "1",
454 },
455 {
456 .name = "cpuload",
457 .type = FIO_OPT_INT,
458 .off1 = td_var_offset(cpuload),
459 .help = "Use this percentage of CPU",
460 },
461 {
462 .name = "cpuchunks",
463 .type = FIO_OPT_INT,
464 .off1 = td_var_offset(cpucycle),
465 .help = "Length of the CPU burn cycles",
466 },
467#ifdef FIO_HAVE_CPU_AFFINITY
468 {
469 .name = "cpumask",
470 .type = FIO_OPT_INT,
471 .cb = str_cpumask_cb,
472 .help = "CPU affinity mask",
473 },
474#endif
475 {
476 .name = "end_fsync",
477 .type = FIO_OPT_BOOL,
478 .off1 = td_var_offset(end_fsync),
479 .help = "Include fsync at the end of job",
480 .def = "0",
481 },
482 {
483 .name = "unlink",
484 .type = FIO_OPT_BOOL,
485 .off1 = td_var_offset(unlink),
486 .help = "Unlink created files after job has completed",
487 .def = "0",
488 },
489 {
490 .name = "exitall",
491 .type = FIO_OPT_STR_SET,
492 .cb = str_exitall_cb,
493 .help = "Terminate all jobs when one exits",
494 },
495 {
496 .name = "stonewall",
497 .type = FIO_OPT_STR_SET,
498 .off1 = td_var_offset(stonewall),
499 .help = "Insert a hard barrier between this job and previous",
500 },
501 {
502 .name = "thread",
503 .type = FIO_OPT_STR_SET,
504 .off1 = td_var_offset(use_thread),
505 .help = "Use threads instead of forks",
506 },
507 {
508 .name = "write_bw_log",
509 .type = FIO_OPT_STR_SET,
510 .off1 = td_var_offset(write_bw_log),
511 .help = "Write log of bandwidth during run",
512 },
513 {
514 .name = "write_lat_log",
515 .type = FIO_OPT_STR_SET,
516 .off1 = td_var_offset(write_lat_log),
517 .help = "Write log of latency during run",
518 },
519 {
520 .name = "hugepage-size",
521 .type = FIO_OPT_STR_VAL,
522 .off1 = td_var_offset(hugepage_size),
523 .help = "When using hugepages, specify size of each page",
524 .def = __stringify(FIO_HUGE_PAGE),
525 },
526 {
527 .name = NULL,
528 },
529};
530
531#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
532#define FIO_CMD_OPTS (16)
533#define FIO_GETOPT_JOB (0x89988998)
534
535/*
536 * Command line options. These will contain the above, plus a few
537 * extra that only pertain to fio itself and not jobs.
538 */
539static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
540 {
541 .name = "output",
542 .has_arg = required_argument,
543 .val = 'o',
544 },
545 {
546 .name = "timeout",
547 .has_arg = required_argument,
548 .val = 't',
549 },
550 {
551 .name = "latency-log",
552 .has_arg = required_argument,
553 .val = 'l',
554 },
555 {
556 .name = "bandwidth-log",
557 .has_arg = required_argument,
558 .val = 'b',
559 },
560 {
561 .name = "minimal",
562 .has_arg = optional_argument,
563 .val = 'm',
564 },
565 {
566 .name = "version",
567 .has_arg = no_argument,
568 .val = 'v',
569 },
570 {
571 .name = "help",
572 .has_arg = no_argument,
573 .val = 'h',
574 },
575 {
576 .name = "cmdhelp",
577 .has_arg = required_argument,
578 .val = 'c',
579 },
580 {
581 .name = NULL,
582 },
583};
584
585static int def_timeout = 0;
586
587static char fio_version_string[] = "fio 1.11";
588
589static char **ini_file;
590static int max_jobs = MAX_JOBS;
591
592struct thread_data def_thread;
593struct thread_data *threads = NULL;
594
595int exitall_on_terminate = 0;
596int terse_output = 0;
597unsigned long long mlock_size = 0;
598FILE *f_out = NULL;
599FILE *f_err = NULL;
600
601static int write_lat_log = 0;
602int write_bw_log = 0;
603
604/*
605 * Return a free job structure.
606 */
607static struct thread_data *get_new_job(int global, struct thread_data *parent)
608{
609 struct thread_data *td;
610
611 if (global)
612 return &def_thread;
613 if (thread_number >= max_jobs)
614 return NULL;
615
616 td = &threads[thread_number++];
617 *td = *parent;
618
619 td->thread_number = thread_number;
620 return td;
621}
622
623static void put_job(struct thread_data *td)
624{
625 if (td == &def_thread)
626 return;
627
628 if (td->error)
629 fprintf(f_out, "fio: %s\n", td->verror);
630
631 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
632 thread_number--;
633}
634
635/*
636 * Lazy way of fixing up options that depend on each other. We could also
637 * define option callback handlers, but this is easier.
638 */
639static void fixup_options(struct thread_data *td)
640{
641 if (!td->rwmixread && td->rwmixwrite)
642 td->rwmixread = 100 - td->rwmixwrite;
643
644 if (td->write_iolog_file && td->read_iolog_file) {
645 log_err("fio: read iolog overrides write_iolog\n");
646 free(td->write_iolog_file);
647 td->write_iolog_file = NULL;
648 }
649
650 if (td->io_ops->flags & FIO_SYNCIO)
651 td->iodepth = 1;
652 else {
653 if (!td->iodepth)
654 td->iodepth = td->nr_files;
655 }
656
657 /*
658 * only really works for sequential io for now, and with 1 file
659 */
660 if (td->zone_size && td_random(td) && td->nr_files == 1)
661 td->zone_size = 0;
662
663 /*
664 * Reads can do overwrites, we always need to pre-create the file
665 */
666 if (td_read(td) || td_rw(td))
667 td->overwrite = 1;
668
669 if (!td->min_bs[DDIR_READ])
670 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
671 if (!td->max_bs[DDIR_READ])
672 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
673 if (!td->min_bs[DDIR_WRITE])
674 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
675 if (!td->max_bs[DDIR_WRITE])
676 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
677
678 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
679
680 if (td_read(td) && !td_rw(td))
681 td->verify = 0;
682
683 if (td->norandommap && td->verify != VERIFY_NONE) {
684 log_err("fio: norandommap given, verify disabled\n");
685 td->verify = VERIFY_NONE;
686 }
687 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
688 log_err("fio: bs_unaligned may not work with raw io\n");
689
690 /*
691 * O_DIRECT and char doesn't mix, clear that flag if necessary.
692 */
693 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
694 td->odirect = 0;
695
696 /*
697 * thinktime_spin must be less than thinktime
698 */
699 if (td->thinktime_spin > td->thinktime)
700 td->thinktime_spin = td->thinktime;
701
702 /*
703 * The low water mark cannot be bigger than the iodepth
704 */
705 if (td->iodepth_low > td->iodepth || !td->iodepth_low)
706 td->iodepth_low = td->iodepth;
707}
708
709/*
710 * This function leaks the buffer
711 */
712static char *to_kmg(unsigned int val)
713{
714 char *buf = malloc(32);
715 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
716 char *p = post;
717
718 do {
719 if (val & 1023)
720 break;
721
722 val >>= 10;
723 p++;
724 } while (*p);
725
726 snprintf(buf, 31, "%u%c", val, *p);
727 return buf;
728}
729
730/*
731 * Adds a job to the list of things todo. Sanitizes the various options
732 * to make sure we don't have conflicts, and initializes various
733 * members of td.
734 */
735static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
736{
737 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
738 "randread", "randwrite", "randrw" };
739 struct stat sb;
740 int numjobs, i;
741 struct fio_file *f;
742
743 /*
744 * the def_thread is just for options, it's not a real job
745 */
746 if (td == &def_thread)
747 return 0;
748
749 assert(td->io_ops);
750
751 if (td->odirect)
752 td->io_ops->flags |= FIO_RAWIO;
753
754 td->filetype = FIO_TYPE_FILE;
755 if (td->filename && !lstat(td->filename, &sb)) {
756 if (S_ISBLK(sb.st_mode))
757 td->filetype = FIO_TYPE_BD;
758 else if (S_ISCHR(sb.st_mode))
759 td->filetype = FIO_TYPE_CHAR;
760 }
761
762 fixup_options(td);
763
764 if (td->filename)
765 td->nr_uniq_files = 1;
766 else
767 td->nr_uniq_files = td->nr_files;
768
769 if (td->filetype == FIO_TYPE_FILE || td->filename) {
770 char tmp[PATH_MAX];
771 int len = 0;
772
773 if (td->directory && td->directory[0] != '\0') {
774 if (lstat(td->directory, &sb) < 0) {
775 log_err("fio: %s is not a directory\n", td->directory);
776 td_verror(td, errno, "lstat");
777 return 1;
778 }
779 if (!S_ISDIR(sb.st_mode)) {
780 log_err("fio: %s is not a directory\n", td->directory);
781 return 1;
782 }
783 len = sprintf(tmp, "%s/", td->directory);
784 }
785
786 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
787
788 for_each_file(td, f, i) {
789 memset(f, 0, sizeof(*f));
790 f->fd = -1;
791
792 if (td->filename)
793 sprintf(tmp + len, "%s", td->filename);
794 else
795 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
796 f->file_name = strdup(tmp);
797 }
798 } else {
799 td->nr_files = 1;
800 td->files = malloc(sizeof(struct fio_file));
801 f = &td->files[0];
802
803 memset(f, 0, sizeof(*f));
804 f->fd = -1;
805 f->file_name = strdup(jobname);
806 }
807
808 for_each_file(td, f, i) {
809 f->file_size = td->total_file_size / td->nr_files;
810 f->file_offset = td->start_offset;
811 }
812
813 fio_sem_init(&td->mutex, 0);
814
815 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
816 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
817 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
818
819 if (td->stonewall && td->thread_number > 1)
820 groupid++;
821
822 td->groupid = groupid;
823
824 if (setup_rate(td))
825 goto err;
826
827 if (td->write_lat_log) {
828 setup_log(&td->ts.slat_log);
829 setup_log(&td->ts.clat_log);
830 }
831 if (td->write_bw_log)
832 setup_log(&td->ts.bw_log);
833
834 if (!td->name)
835 td->name = strdup(jobname);
836
837 if (!terse_output) {
838 if (!job_add_num) {
839 if (td->io_ops->flags & FIO_CPUIO)
840 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
841 else {
842 char *c1, *c2, *c3, *c4;
843
844 c1 = to_kmg(td->min_bs[DDIR_READ]);
845 c2 = to_kmg(td->max_bs[DDIR_READ]);
846 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
847 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
848
849 fprintf(f_out, "%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[td->td_ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth);
850
851 free(c1);
852 free(c2);
853 free(c3);
854 free(c4);
855 }
856 } else if (job_add_num == 1)
857 fprintf(f_out, "...\n");
858 }
859
860 /*
861 * recurse add identical jobs, clear numjobs and stonewall options
862 * as they don't apply to sub-jobs
863 */
864 numjobs = td->numjobs;
865 while (--numjobs) {
866 struct thread_data *td_new = get_new_job(0, td);
867
868 if (!td_new)
869 goto err;
870
871 td_new->numjobs = 1;
872 td_new->stonewall = 0;
873 job_add_num = numjobs - 1;
874
875 if (add_job(td_new, jobname, job_add_num))
876 goto err;
877 }
878 return 0;
879err:
880 put_job(td);
881 return -1;
882}
883
884/*
885 * Initialize the various random states we need (random io, block size ranges,
886 * read/write mix, etc).
887 */
888int init_random_state(struct thread_data *td)
889{
890 unsigned long seeds[5];
891 int fd, num_maps, blocks, i;
892 struct fio_file *f;
893
894 if (td->io_ops->flags & FIO_CPUIO)
895 return 0;
896
897 fd = open("/dev/urandom", O_RDONLY);
898 if (fd == -1) {
899 td_verror(td, errno, "open");
900 return 1;
901 }
902
903 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
904 td_verror(td, EIO, "read");
905 close(fd);
906 return 1;
907 }
908
909 close(fd);
910
911 os_random_seed(seeds[0], &td->bsrange_state);
912 os_random_seed(seeds[1], &td->verify_state);
913 os_random_seed(seeds[2], &td->rwmix_state);
914
915 if (td->file_service_type == FIO_FSERVICE_RANDOM)
916 os_random_seed(seeds[3], &td->next_file_state);
917
918 if (!td_random(td))
919 return 0;
920
921 if (td->rand_repeatable)
922 seeds[4] = FIO_RANDSEED * td->thread_number;
923
924 if (!td->norandommap) {
925 for_each_file(td, f, i) {
926 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
927 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
928 f->file_map = malloc(num_maps * sizeof(long));
929 f->num_maps = num_maps;
930 memset(f->file_map, 0, num_maps * sizeof(long));
931 }
932 }
933
934 os_random_seed(seeds[4], &td->random_state);
935 return 0;
936}
937
938static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
939{
940#ifdef FIO_HAVE_CPU_AFFINITY
941 unsigned int i;
942
943 CPU_ZERO(&cpumask);
944
945 for (i = 0; i < sizeof(int) * 8; i++) {
946 if ((1 << i) & cpu)
947 CPU_SET(i, &cpumask);
948 }
949#endif
950}
951
952static int is_empty_or_comment(char *line)
953{
954 unsigned int i;
955
956 for (i = 0; i < strlen(line); i++) {
957 if (line[i] == ';')
958 return 1;
959 if (line[i] == '#')
960 return 1;
961 if (!isspace(line[i]) && !iscntrl(line[i]))
962 return 0;
963 }
964
965 return 1;
966}
967
968/*
969 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
970 */
971static char *get_mmap_file(const char *str)
972{
973 char *p = strstr(str, ":");
974
975 if (!p)
976 return NULL;
977
978 p++;
979 strip_blank_front(&p);
980 strip_blank_end(p);
981 return strdup(p);
982}
983
984static int str_mem_cb(void *data, const char *mem)
985{
986 struct thread_data *td = data;
987
988 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
989 td->mmapfile = get_mmap_file(mem);
990 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
991 log_err("fio: mmaphuge:/path/to/file\n");
992 return 1;
993 }
994 }
995
996 return 0;
997}
998
999static int str_ioengine_cb(void *data, const char *str)
1000{
1001 struct thread_data *td = data;
1002
1003 td->io_ops = load_ioengine(td, str);
1004 if (td->io_ops)
1005 return 0;
1006
1007 return 1;
1008}
1009
1010static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1011{
1012 mlock_size = *val;
1013 return 0;
1014}
1015
1016#ifdef FIO_HAVE_IOPRIO
1017static int str_prioclass_cb(void *data, unsigned int *val)
1018{
1019 struct thread_data *td = data;
1020
1021 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1022 return 0;
1023}
1024
1025static int str_prio_cb(void *data, unsigned int *val)
1026{
1027 struct thread_data *td = data;
1028
1029 td->ioprio |= *val;
1030 return 0;
1031}
1032#endif
1033
1034static int str_exitall_cb(void)
1035{
1036 exitall_on_terminate = 1;
1037 return 0;
1038}
1039
1040static int str_cpumask_cb(void *data, unsigned int *val)
1041{
1042 struct thread_data *td = data;
1043
1044 fill_cpu_mask(td->cpumask, *val);
1045 return 0;
1046}
1047
1048/*
1049 * This is our [ini] type file parser.
1050 */
1051static int parse_jobs_ini(char *file, int stonewall_flag)
1052{
1053 unsigned int global;
1054 struct thread_data *td;
1055 char *string, *name;
1056 fpos_t off;
1057 FILE *f;
1058 char *p;
1059 int ret = 0, stonewall;
1060
1061 f = fopen(file, "r");
1062 if (!f) {
1063 perror("fopen job file");
1064 return 1;
1065 }
1066
1067 string = malloc(4096);
1068 name = malloc(256);
1069 memset(name, 0, 256);
1070
1071 stonewall = stonewall_flag;
1072 do {
1073 p = fgets(string, 4095, f);
1074 if (!p)
1075 break;
1076 if (is_empty_or_comment(p))
1077 continue;
1078 if (sscanf(p, "[%255s]", name) != 1)
1079 continue;
1080
1081 global = !strncmp(name, "global", 6);
1082
1083 name[strlen(name) - 1] = '\0';
1084
1085 td = get_new_job(global, &def_thread);
1086 if (!td) {
1087 ret = 1;
1088 break;
1089 }
1090
1091 /*
1092 * Seperate multiple job files by a stonewall
1093 */
1094 if (!global && stonewall) {
1095 td->stonewall = stonewall;
1096 stonewall = 0;
1097 }
1098
1099 fgetpos(f, &off);
1100 while ((p = fgets(string, 4096, f)) != NULL) {
1101 if (is_empty_or_comment(p))
1102 continue;
1103
1104 strip_blank_front(&p);
1105
1106 if (p[0] == '[')
1107 break;
1108
1109 strip_blank_end(p);
1110
1111 fgetpos(f, &off);
1112
1113 /*
1114 * Don't break here, continue parsing options so we
1115 * dump all the bad ones. Makes trial/error fixups
1116 * easier on the user.
1117 */
1118 ret |= parse_option(p, options, td);
1119 }
1120
1121 if (!ret) {
1122 fsetpos(f, &off);
1123 ret = add_job(td, name, 0);
1124 } else {
1125 log_err("fio: job %s dropped\n", name);
1126 put_job(td);
1127 }
1128 } while (!ret);
1129
1130 free(string);
1131 free(name);
1132 fclose(f);
1133 return ret;
1134}
1135
1136static int fill_def_thread(void)
1137{
1138 memset(&def_thread, 0, sizeof(def_thread));
1139
1140 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1141 perror("sched_getaffinity");
1142 return 1;
1143 }
1144
1145 /*
1146 * fill default options
1147 */
1148 fill_default_options(&def_thread, options);
1149
1150 def_thread.timeout = def_timeout;
1151 def_thread.write_bw_log = write_bw_log;
1152 def_thread.write_lat_log = write_lat_log;
1153
1154#ifdef FIO_HAVE_DISK_UTIL
1155 def_thread.do_disk_util = 1;
1156#endif
1157
1158 return 0;
1159}
1160
1161static void usage(void)
1162{
1163 printf("%s\n", fio_version_string);
1164 printf("\t--output\tWrite output to file\n");
1165 printf("\t--timeout\tRuntime in seconds\n");
1166 printf("\t--latency-log\tGenerate per-job latency logs\n");
1167 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1168 printf("\t--minimal\tMinimal (terse) output\n");
1169 printf("\t--version\tPrint version info and exit\n");
1170 printf("\t--help\t\tPrint this page\n");
1171 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
1172}
1173
1174static int parse_cmd_line(int argc, char *argv[])
1175{
1176 struct thread_data *td = NULL;
1177 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
1178
1179 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
1180 switch (c) {
1181 case 't':
1182 def_timeout = atoi(optarg);
1183 break;
1184 case 'l':
1185 write_lat_log = 1;
1186 break;
1187 case 'w':
1188 write_bw_log = 1;
1189 break;
1190 case 'o':
1191 f_out = fopen(optarg, "w+");
1192 if (!f_out) {
1193 perror("fopen output");
1194 exit(1);
1195 }
1196 f_err = f_out;
1197 break;
1198 case 'm':
1199 terse_output = 1;
1200 break;
1201 case 'h':
1202 usage();
1203 exit(0);
1204 case 'c':
1205 ret = show_cmd_help(options, optarg);
1206 exit(ret);
1207 case 'v':
1208 printf("%s\n", fio_version_string);
1209 exit(0);
1210 case FIO_GETOPT_JOB: {
1211 const char *opt = long_options[lidx].name;
1212 char *val = optarg;
1213
1214 if (!strncmp(opt, "name", 4) && td) {
1215 ret = add_job(td, td->name ?: "fio", 0);
1216 if (ret) {
1217 put_job(td);
1218 return 0;
1219 }
1220 td = NULL;
1221 }
1222 if (!td) {
1223 int global = !strncmp(val, "global", 6);
1224
1225 td = get_new_job(global, &def_thread);
1226 if (!td)
1227 return 0;
1228 }
1229
1230 ret = parse_cmd_option(opt, val, options, td);
1231 if (ret)
1232 dont_add_job = 1;
1233 break;
1234 }
1235 default:
1236 break;
1237 }
1238 }
1239
1240 if (td) {
1241 if (dont_add_job)
1242 put_job(td);
1243 else {
1244 ret = add_job(td, td->name ?: "fio", 0);
1245 if (ret)
1246 put_job(td);
1247 }
1248 }
1249
1250 while (optind < argc) {
1251 ini_idx++;
1252 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1253 ini_file[ini_idx - 1] = strdup(argv[optind]);
1254 optind++;
1255 }
1256
1257 return ini_idx;
1258}
1259
1260static void free_shm(void)
1261{
1262 struct shmid_ds sbuf;
1263
1264 if (threads) {
1265 shmdt((void *) threads);
1266 threads = NULL;
1267 shmctl(shm_id, IPC_RMID, &sbuf);
1268 }
1269}
1270
1271/*
1272 * The thread area is shared between the main process and the job
1273 * threads/processes. So setup a shared memory segment that will hold
1274 * all the job info.
1275 */
1276static int setup_thread_area(void)
1277{
1278 /*
1279 * 1024 is too much on some machines, scale max_jobs if
1280 * we get a failure that looks like too large a shm segment
1281 */
1282 do {
1283 size_t size = max_jobs * sizeof(struct thread_data);
1284
1285 shm_id = shmget(0, size, IPC_CREAT | 0600);
1286 if (shm_id != -1)
1287 break;
1288 if (errno != EINVAL) {
1289 perror("shmget");
1290 break;
1291 }
1292
1293 max_jobs >>= 1;
1294 } while (max_jobs);
1295
1296 if (shm_id == -1)
1297 return 1;
1298
1299 threads = shmat(shm_id, NULL, 0);
1300 if (threads == (void *) -1) {
1301 perror("shmat");
1302 return 1;
1303 }
1304
1305 atexit(free_shm);
1306 return 0;
1307}
1308
1309/*
1310 * Copy the fio options into the long options map, so we mirror
1311 * job and cmd line options.
1312 */
1313static void dupe_job_options(void)
1314{
1315 struct fio_option *o;
1316 unsigned int i;
1317
1318 i = 0;
1319 while (long_options[i].name)
1320 i++;
1321
1322 o = &options[0];
1323 while (o->name) {
1324 long_options[i].name = o->name;
1325 long_options[i].val = FIO_GETOPT_JOB;
1326 if (o->type == FIO_OPT_STR_SET)
1327 long_options[i].has_arg = no_argument;
1328 else
1329 long_options[i].has_arg = required_argument;
1330
1331 i++;
1332 o++;
1333 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1334 }
1335}
1336
1337int parse_options(int argc, char *argv[])
1338{
1339 int job_files, i;
1340
1341 f_out = stdout;
1342 f_err = stderr;
1343
1344 options_init(options);
1345
1346 dupe_job_options();
1347
1348 if (setup_thread_area())
1349 return 1;
1350 if (fill_def_thread())
1351 return 1;
1352
1353 job_files = parse_cmd_line(argc, argv);
1354
1355 for (i = 0; i < job_files; i++) {
1356 if (fill_def_thread())
1357 return 1;
1358 if (parse_jobs_ini(ini_file[i], i))
1359 return 1;
1360 free(ini_file[i]);
1361 }
1362
1363 free(ini_file);
1364
1365 if (!thread_number) {
1366 log_err("No jobs defined(s)\n");
1367 return 1;
1368 }
1369
1370 return 0;
1371}