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