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