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