Note process vs thread in output
[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 *);
33
ee738499
JA
34#define __stringify_1(x) #x
35#define __stringify(x) __stringify_1(x)
36
e1f36503
JA
37/*
38 * Map of job/command line options
39 */
40static struct fio_option options[] = {
61697c37
JA
41 {
42 .name = "description",
43 .type = FIO_OPT_STR_STORE,
44 .off1 = td_var_offset(description),
45 .help = "Text job description",
46 },
e1f36503
JA
47 {
48 .name = "name",
49 .type = FIO_OPT_STR_STORE,
50 .off1 = td_var_offset(name),
fd28ca49 51 .help = "Name of this job",
e1f36503
JA
52 },
53 {
54 .name = "directory",
55 .type = FIO_OPT_STR_STORE,
56 .off1 = td_var_offset(directory),
fd28ca49 57 .help = "Directory to store files in",
e1f36503
JA
58 },
59 {
60 .name = "filename",
61 .type = FIO_OPT_STR_STORE,
62 .off1 = td_var_offset(filename),
fd28ca49 63 .help = "Force the use of a specific file",
e1f36503
JA
64 },
65 {
66 .name = "rw",
67 .type = FIO_OPT_STR,
b1ec1da6 68 .off1 = td_var_offset(td_ddir),
15f7918f 69 .help = "IO direction",
ee738499 70 .def = "read",
b1ec1da6
JA
71 .posval = {
72 { .ival = "read", .oval = TD_DDIR_READ },
73 { .ival = "write", .oval = TD_DDIR_WRITE },
74 { .ival = "randread", .oval = TD_DDIR_RANDREAD },
75 { .ival = "randwrite", .oval = TD_DDIR_RANDWRITE },
76 { .ival = "rw", .oval = TD_DDIR_RW },
77 { .ival = "randrw", .oval = TD_DDIR_RANDRW },
78 },
e1f36503
JA
79 },
80 {
81 .name = "ioengine",
09629a90
JA
82 .type = FIO_OPT_STR_STORE,
83 .off1 = td_var_offset(ioengine),
15f7918f 84 .help = "IO engine to use",
ee738499 85 .def = "sync",
b1ec1da6 86 .posval = {
10cad1aa
JA
87 { .ival = "sync", },
88#ifdef FIO_HAVE_LIBAIO
89 { .ival = "libaio", },
90#endif
91#ifdef FIO_HAVE_POSIXAIO
92 { .ival = "posixaio", },
93#endif
94 { .ival = "mmap", },
95#ifdef FIO_HAVE_SPLICE
96 { .ival = "splice", },
97#endif
98#ifdef FIO_HAVE_SGIO
99 { .ival = "sg", },
100#endif
b1ec1da6 101 { .ival = "null", }, { .ival = "net", },
10cad1aa 102#ifdef FIO_HAVE_SYSLET
b1ec1da6 103 { .ival = "syslet-rw", },
10cad1aa 104#endif
ba0fbe10 105 { .ival = "cpuio", },
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),
ba0fbe10
JA
478 .help = "Length of the CPU burn cycles (usecs)",
479 .def = "50000",
e1f36503 480 },
e1f36503
JA
481#ifdef FIO_HAVE_CPU_AFFINITY
482 {
483 .name = "cpumask",
484 .type = FIO_OPT_INT,
485 .cb = str_cpumask_cb,
fd28ca49 486 .help = "CPU affinity mask",
e1f36503
JA
487 },
488#endif
489 {
490 .name = "end_fsync",
13335ddb 491 .type = FIO_OPT_BOOL,
1304923a 492 .off1 = td_var_offset(end_fsync),
fd28ca49 493 .help = "Include fsync at the end of job",
ee738499 494 .def = "0",
e1f36503
JA
495 },
496 {
497 .name = "unlink",
13335ddb 498 .type = FIO_OPT_BOOL,
e1f36503 499 .off1 = td_var_offset(unlink),
ee738499 500 .help = "Unlink created files after job has completed",
e545a6ce 501 .def = "0",
e1f36503
JA
502 },
503 {
504 .name = "exitall",
505 .type = FIO_OPT_STR_SET,
506 .cb = str_exitall_cb,
fd28ca49 507 .help = "Terminate all jobs when one exits",
e1f36503
JA
508 },
509 {
510 .name = "stonewall",
511 .type = FIO_OPT_STR_SET,
512 .off1 = td_var_offset(stonewall),
fd28ca49 513 .help = "Insert a hard barrier between this job and previous",
e1f36503
JA
514 },
515 {
516 .name = "thread",
517 .type = FIO_OPT_STR_SET,
d9bb3b80 518 .off1 = td_var_offset(use_thread),
fd28ca49 519 .help = "Use threads instead of forks",
e1f36503
JA
520 },
521 {
522 .name = "write_bw_log",
523 .type = FIO_OPT_STR_SET,
524 .off1 = td_var_offset(write_bw_log),
fd28ca49 525 .help = "Write log of bandwidth during run",
e1f36503
JA
526 },
527 {
528 .name = "write_lat_log",
529 .type = FIO_OPT_STR_SET,
530 .off1 = td_var_offset(write_lat_log),
fd28ca49 531 .help = "Write log of latency during run",
e1f36503 532 },
56bb17f2
JA
533 {
534 .name = "hugepage-size",
535 .type = FIO_OPT_STR_VAL,
536 .off1 = td_var_offset(hugepage_size),
fd28ca49 537 .help = "When using hugepages, specify size of each page",
ee738499 538 .def = __stringify(FIO_HUGE_PAGE),
56bb17f2 539 },
b2560f3c
JA
540 {
541 .name = "group_reporting",
542 .type = FIO_OPT_STR_SET,
543 .off1 = td_var_offset(group_reporting),
544 .help = "Do reporting on a per-group basis",
545 },
e1f36503
JA
546 {
547 .name = NULL,
548 },
549};
550
b4692828
JA
551#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
552#define FIO_CMD_OPTS (16)
553#define FIO_GETOPT_JOB (0x89988998)
554
555/*
556 * Command line options. These will contain the above, plus a few
557 * extra that only pertain to fio itself and not jobs.
558 */
559static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
560 {
561 .name = "output",
562 .has_arg = required_argument,
563 .val = 'o',
564 },
565 {
566 .name = "timeout",
567 .has_arg = required_argument,
568 .val = 't',
569 },
570 {
571 .name = "latency-log",
572 .has_arg = required_argument,
573 .val = 'l',
574 },
575 {
576 .name = "bandwidth-log",
577 .has_arg = required_argument,
578 .val = 'b',
579 },
580 {
581 .name = "minimal",
582 .has_arg = optional_argument,
583 .val = 'm',
584 },
585 {
586 .name = "version",
587 .has_arg = no_argument,
588 .val = 'v',
589 },
fd28ca49
JA
590 {
591 .name = "help",
592 .has_arg = no_argument,
593 .val = 'h',
594 },
595 {
596 .name = "cmdhelp",
320beefe 597 .has_arg = optional_argument,
fd28ca49
JA
598 .val = 'c',
599 },
b4692828
JA
600 {
601 .name = NULL,
602 },
603};
604
ee738499 605static int def_timeout = 0;
972cfd25 606
fdf3de27 607static char fio_version_string[] = "fio 1.13";
ebac4655 608
972cfd25 609static char **ini_file;
ebac4655
JA
610static int max_jobs = MAX_JOBS;
611
612struct thread_data def_thread;
613struct thread_data *threads = NULL;
614
ebac4655 615int exitall_on_terminate = 0;
c6ae0a5b 616int terse_output = 0;
c04f7ec3 617unsigned long long mlock_size = 0;
eb8bbf48
JA
618FILE *f_out = NULL;
619FILE *f_err = NULL;
ebac4655 620
ee738499 621static int write_lat_log = 0;
bb3884d8 622int write_bw_log = 0;
ec94ec56 623
9728ce37
JB
624FILE *get_f_out()
625{
626 return f_out;
627}
628
629FILE *get_f_err()
630{
631 return f_err;
632}
633
906c8d75
JA
634/*
635 * Return a free job structure.
636 */
ebac4655
JA
637static struct thread_data *get_new_job(int global, struct thread_data *parent)
638{
639 struct thread_data *td;
640
641 if (global)
642 return &def_thread;
643 if (thread_number >= max_jobs)
644 return NULL;
645
646 td = &threads[thread_number++];
ddaeaa5a 647 *td = *parent;
ebac4655 648
ebac4655 649 td->thread_number = thread_number;
ebac4655
JA
650 return td;
651}
652
653static void put_job(struct thread_data *td)
654{
549577a7
JA
655 if (td == &def_thread)
656 return;
657
16edf25d
JA
658 if (td->error)
659 fprintf(f_out, "fio: %s\n", td->verror);
660
ebac4655
JA
661 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
662 thread_number--;
663}
664
dad915e3
JA
665/*
666 * Lazy way of fixing up options that depend on each other. We could also
667 * define option callback handlers, but this is easier.
668 */
e1f36503
JA
669static void fixup_options(struct thread_data *td)
670{
e1f36503
JA
671 if (!td->rwmixread && td->rwmixwrite)
672 td->rwmixread = 100 - td->rwmixwrite;
dad915e3 673
076efc7c
JA
674 if (td->write_iolog_file && td->read_iolog_file) {
675 log_err("fio: read iolog overrides write_iolog\n");
676 free(td->write_iolog_file);
677 td->write_iolog_file = NULL;
678 }
16b462ae
JA
679
680 if (td->io_ops->flags & FIO_SYNCIO)
681 td->iodepth = 1;
682 else {
683 if (!td->iodepth)
b5af8293 684 td->iodepth = td->open_files;
16b462ae
JA
685 }
686
687 /*
688 * only really works for sequential io for now, and with 1 file
689 */
b5af8293 690 if (td->zone_size && td_random(td) && td->open_files == 1)
16b462ae
JA
691 td->zone_size = 0;
692
693 /*
694 * Reads can do overwrites, we always need to pre-create the file
695 */
696 if (td_read(td) || td_rw(td))
697 td->overwrite = 1;
698
a00735e6
JA
699 if (!td->min_bs[DDIR_READ])
700 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
701 if (!td->max_bs[DDIR_READ])
702 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
703 if (!td->min_bs[DDIR_WRITE])
75e6f36f 704 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
a00735e6 705 if (!td->max_bs[DDIR_WRITE])
75e6f36f 706 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
a00735e6
JA
707
708 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
709
16b462ae
JA
710 if (td_read(td) && !td_rw(td))
711 td->verify = 0;
bb8895e0
JA
712
713 if (td->norandommap && td->verify != VERIFY_NONE) {
714 log_err("fio: norandommap given, verify disabled\n");
715 td->verify = VERIFY_NONE;
716 }
690adba3
JA
717 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
718 log_err("fio: bs_unaligned may not work with raw io\n");
e0a22335
JA
719
720 /*
721 * O_DIRECT and char doesn't mix, clear that flag if necessary.
722 */
723 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
724 td->odirect = 0;
48097d5c
JA
725
726 /*
727 * thinktime_spin must be less than thinktime
728 */
729 if (td->thinktime_spin > td->thinktime)
730 td->thinktime_spin = td->thinktime;
e916b390
JA
731
732 /*
733 * The low water mark cannot be bigger than the iodepth
734 */
9467b77c
JA
735 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
736 /*
737 * syslet work around - if the workload is sequential,
738 * we want to let the queue drain all the way down to
739 * avoid seeking between async threads
740 */
741 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
742 td->iodepth_low = 1;
743 else
744 td->iodepth_low = td->iodepth;
745 }
cb5ab512
JA
746
747 /*
748 * If batch number isn't set, default to the same as iodepth
749 */
750 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
751 td->iodepth_batch = td->iodepth;
b5af8293
JA
752
753 if (td->open_files > td->nr_files || !td->open_files)
754 td->open_files = td->nr_files;
e1f36503
JA
755}
756
f8977ee6
JA
757/*
758 * This function leaks the buffer
759 */
760static char *to_kmg(unsigned int val)
761{
762 char *buf = malloc(32);
f3502ba2 763 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
f8977ee6
JA
764 char *p = post;
765
245142ff 766 do {
f8977ee6
JA
767 if (val & 1023)
768 break;
769
770 val >>= 10;
771 p++;
245142ff 772 } while (*p);
f8977ee6
JA
773
774 snprintf(buf, 31, "%u%c", val, *p);
775 return buf;
776}
777
09629a90
JA
778/* External engines are specified by "external:name.o") */
779static const char *get_engine_name(const char *str)
780{
781 char *p = strstr(str, ":");
782
783 if (!p)
784 return str;
785
786 p++;
787 strip_blank_front(&p);
788 strip_blank_end(p);
789 return p;
790}
791
906c8d75
JA
792/*
793 * Adds a job to the list of things todo. Sanitizes the various options
794 * to make sure we don't have conflicts, and initializes various
795 * members of td.
796 */
75154845 797static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
ebac4655 798{
413dd459
JA
799 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
800 "randread", "randwrite", "randrw" };
ebac4655 801 struct stat sb;
413dd459 802 int numjobs, i;
53cdc686 803 struct fio_file *f;
09629a90 804 const char *engine;
ebac4655 805
ebac4655
JA
806 /*
807 * the def_thread is just for options, it's not a real job
808 */
809 if (td == &def_thread)
810 return 0;
811
09629a90
JA
812 engine = get_engine_name(td->ioengine);
813 td->io_ops = load_ioengine(td, engine);
814 if (!td->io_ops) {
815 log_err("fio: failed to load engine %s\n", engine);
816 return 1;
817 }
df64119d 818
9cedf167
JA
819 if (td->use_thread)
820 nr_thread++;
821 else
822 nr_process++;
823
690adba3
JA
824 if (td->odirect)
825 td->io_ops->flags |= FIO_RAWIO;
826
80be24f4
JA
827 if (!td->filename)
828 td->filename = strdup(jobname);
829
ebac4655 830 td->filetype = FIO_TYPE_FILE;
80be24f4 831 if (!lstat(td->filename, &sb)) {
0af7b542
JA
832 if (S_ISBLK(sb.st_mode))
833 td->filetype = FIO_TYPE_BD;
834 else if (S_ISCHR(sb.st_mode))
835 td->filetype = FIO_TYPE_CHAR;
836 }
ebac4655 837
e0a22335
JA
838 fixup_options(td);
839
13f8e2d2
JA
840 if (td->filename)
841 td->nr_uniq_files = 1;
842 else
0f14fef3 843 td->nr_uniq_files = td->open_files;
13f8e2d2
JA
844
845 if (td->filetype == FIO_TYPE_FILE || td->filename) {
e9c047a0 846 char tmp[PATH_MAX];
53cdc686 847 int len = 0;
e9c047a0 848
16edf25d
JA
849 if (td->directory && td->directory[0] != '\0') {
850 if (lstat(td->directory, &sb) < 0) {
851 log_err("fio: %s is not a directory\n", td->directory);
e1161c32 852 td_verror(td, errno, "lstat");
16edf25d
JA
853 return 1;
854 }
855 if (!S_ISDIR(sb.st_mode)) {
856 log_err("fio: %s is not a directory\n", td->directory);
857 return 1;
858 }
8aeebd55 859 len = sprintf(tmp, "%s/", td->directory);
16edf25d 860 }
ebac4655 861
b5af8293 862 td->files = malloc(sizeof(struct fio_file) * td->open_files);
53cdc686
JA
863
864 for_each_file(td, f, i) {
865 memset(f, 0, sizeof(*f));
866 f->fd = -1;
867
13f8e2d2
JA
868 if (td->filename)
869 sprintf(tmp + len, "%s", td->filename);
870 else
871 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
53cdc686
JA
872 f->file_name = strdup(tmp);
873 }
874 } else {
b5af8293 875 td->open_files = td->nr_files = 1;
53cdc686
JA
876 td->files = malloc(sizeof(struct fio_file));
877 f = &td->files[0];
878
879 memset(f, 0, sizeof(*f));
880 f->fd = -1;
881 f->file_name = strdup(jobname);
882 }
883
884 for_each_file(td, f, i) {
885 f->file_size = td->total_file_size / td->nr_files;
886 f->file_offset = td->start_offset;
887 }
888
07739b57 889 td->mutex = fio_sem_init(0);
ebac4655 890
756867bd
JA
891 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
892 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
893 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
ebac4655 894
ebac4655
JA
895 if (td->stonewall && td->thread_number > 1)
896 groupid++;
897
898 td->groupid = groupid;
899
900 if (setup_rate(td))
901 goto err;
902
ec94ec56 903 if (td->write_lat_log) {
756867bd
JA
904 setup_log(&td->ts.slat_log);
905 setup_log(&td->ts.clat_log);
ebac4655 906 }
ec94ec56 907 if (td->write_bw_log)
756867bd 908 setup_log(&td->ts.bw_log);
ebac4655 909
b4692828
JA
910 if (!td->name)
911 td->name = strdup(jobname);
01452055 912
c6ae0a5b 913 if (!terse_output) {
b990b5c0 914 if (!job_add_num) {
ba0fbe10 915 if (!strcmp(td->io_ops->name, "cpuio"))
b990b5c0 916 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
f8977ee6
JA
917 else {
918 char *c1, *c2, *c3, *c4;
919
920 c1 = to_kmg(td->min_bs[DDIR_READ]);
921 c2 = to_kmg(td->max_bs[DDIR_READ]);
922 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
923 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
924
413dd459 925 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
926
927 free(c1);
928 free(c2);
929 free(c3);
930 free(c4);
931 }
b990b5c0 932 } else if (job_add_num == 1)
c6ae0a5b
JA
933 fprintf(f_out, "...\n");
934 }
ebac4655
JA
935
936 /*
937 * recurse add identical jobs, clear numjobs and stonewall options
938 * as they don't apply to sub-jobs
939 */
940 numjobs = td->numjobs;
941 while (--numjobs) {
942 struct thread_data *td_new = get_new_job(0, td);
943
944 if (!td_new)
945 goto err;
946
947 td_new->numjobs = 1;
948 td_new->stonewall = 0;
75154845 949 job_add_num = numjobs - 1;
ebac4655 950
75154845 951 if (add_job(td_new, jobname, job_add_num))
ebac4655
JA
952 goto err;
953 }
954 return 0;
955err:
956 put_job(td);
957 return -1;
958}
959
906c8d75
JA
960/*
961 * Initialize the various random states we need (random io, block size ranges,
962 * read/write mix, etc).
963 */
ebac4655
JA
964int init_random_state(struct thread_data *td)
965{
0aabe160 966 unsigned long seeds[5];
53cdc686 967 int fd, num_maps, blocks, i;
0ab8db89 968 struct fio_file *f;
ebac4655 969
ba0fbe10 970 if (td->io_ops->flags & FIO_DISKLESSIO)
f48b467c
JA
971 return 0;
972
1ac267bb 973 fd = open("/dev/urandom", O_RDONLY);
ebac4655 974 if (fd == -1) {
e1161c32 975 td_verror(td, errno, "open");
ebac4655
JA
976 return 1;
977 }
978
a6ccc7be 979 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
e1161c32 980 td_verror(td, EIO, "read");
ebac4655
JA
981 close(fd);
982 return 1;
983 }
984
985 close(fd);
986
6dfd46b9
JA
987 os_random_seed(seeds[0], &td->bsrange_state);
988 os_random_seed(seeds[1], &td->verify_state);
989 os_random_seed(seeds[2], &td->rwmix_state);
ebac4655 990
413dd459
JA
991 if (td->file_service_type == FIO_FSERVICE_RANDOM)
992 os_random_seed(seeds[3], &td->next_file_state);
993
994 if (!td_random(td))
ebac4655
JA
995 return 0;
996
9ebc27e1 997 if (td->rand_repeatable)
0aabe160 998 seeds[4] = FIO_RANDSEED * td->thread_number;
ebac4655 999
bb8895e0
JA
1000 if (!td->norandommap) {
1001 for_each_file(td, f, i) {
a4a81712 1002 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
c7c280ed 1003 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
bb8895e0 1004 f->file_map = malloc(num_maps * sizeof(long));
bda4fd9e
JA
1005 if (!f->file_map) {
1006 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
1007 return 1;
1008 }
bb8895e0
JA
1009 f->num_maps = num_maps;
1010 memset(f->file_map, 0, num_maps * sizeof(long));
1011 }
53cdc686 1012 }
ebac4655 1013
0aabe160 1014 os_random_seed(seeds[4], &td->random_state);
ebac4655
JA
1015 return 0;
1016}
1017
1018static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1019{
1020#ifdef FIO_HAVE_CPU_AFFINITY
1021 unsigned int i;
1022
1023 CPU_ZERO(&cpumask);
1024
1025 for (i = 0; i < sizeof(int) * 8; i++) {
1026 if ((1 << i) & cpu)
1027 CPU_SET(i, &cpumask);
1028 }
1029#endif
1030}
1031
ebac4655
JA
1032static int is_empty_or_comment(char *line)
1033{
1034 unsigned int i;
1035
1036 for (i = 0; i < strlen(line); i++) {
1037 if (line[i] == ';')
1038 return 1;
5cc2da30
IM
1039 if (line[i] == '#')
1040 return 1;
ebac4655
JA
1041 if (!isspace(line[i]) && !iscntrl(line[i]))
1042 return 0;
1043 }
1044
1045 return 1;
1046}
1047
313cb206
JA
1048/*
1049 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1050 */
1051static char *get_mmap_file(const char *str)
1052{
1053 char *p = strstr(str, ":");
1054
1055 if (!p)
1056 return NULL;
1057
1058 p++;
1059 strip_blank_front(&p);
1060 strip_blank_end(p);
1061 return strdup(p);
1062}
1063
b4692828 1064static int str_mem_cb(void *data, const char *mem)
ebac4655 1065{
cb2c86fd
JA
1066 struct thread_data *td = data;
1067
b1ec1da6 1068 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
313cb206 1069 td->mmapfile = get_mmap_file(mem);
b1ec1da6 1070 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
d0bdaf49
JA
1071 log_err("fio: mmaphuge:/path/to/file\n");
1072 return 1;
1073 }
ebac4655
JA
1074 }
1075
b1ec1da6 1076 return 0;
ebac4655
JA
1077}
1078
e1f36503
JA
1079static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1080{
1081 mlock_size = *val;
1082 return 0;
1083}
1084
34cfcdaf 1085#ifdef FIO_HAVE_IOPRIO
e1f36503
JA
1086static int str_prioclass_cb(void *data, unsigned int *val)
1087{
1088 struct thread_data *td = data;
1089
1090 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1091 return 0;
1092}
1093
1094static int str_prio_cb(void *data, unsigned int *val)
1095{
1096 struct thread_data *td = data;
1097
1098 td->ioprio |= *val;
1099 return 0;
1100}
34cfcdaf 1101#endif
e1f36503
JA
1102
1103static int str_exitall_cb(void)
1104{
1105 exitall_on_terminate = 1;
1106 return 0;
1107}
1108
1109static int str_cpumask_cb(void *data, unsigned int *val)
1110{
1111 struct thread_data *td = data;
1112
1113 fill_cpu_mask(td->cpumask, *val);
1114 return 0;
1115}
1116
07261983
JA
1117/*
1118 * This is our [ini] type file parser.
1119 */
1e97cce9 1120static int parse_jobs_ini(char *file, int stonewall_flag)
ebac4655 1121{
e1f36503 1122 unsigned int global;
ebac4655 1123 struct thread_data *td;
fee3bb48 1124 char *string, *name;
ebac4655
JA
1125 fpos_t off;
1126 FILE *f;
1127 char *p;
0c7e37a0 1128 int ret = 0, stonewall;
ebac4655
JA
1129
1130 f = fopen(file, "r");
1131 if (!f) {
aea47d44 1132 perror("fopen job file");
ebac4655
JA
1133 return 1;
1134 }
1135
1136 string = malloc(4096);
1137 name = malloc(256);
fee3bb48 1138 memset(name, 0, 256);
ebac4655 1139
0c7e37a0 1140 stonewall = stonewall_flag;
7c124ac1
JA
1141 do {
1142 p = fgets(string, 4095, f);
1143 if (!p)
45410acb 1144 break;
ebac4655
JA
1145 if (is_empty_or_comment(p))
1146 continue;
fee3bb48 1147 if (sscanf(p, "[%255s]", name) != 1)
ebac4655
JA
1148 continue;
1149
1150 global = !strncmp(name, "global", 6);
1151
1152 name[strlen(name) - 1] = '\0';
1153
1154 td = get_new_job(global, &def_thread);
45410acb
JA
1155 if (!td) {
1156 ret = 1;
1157 break;
1158 }
ebac4655 1159
972cfd25
JA
1160 /*
1161 * Seperate multiple job files by a stonewall
1162 */
f9481919 1163 if (!global && stonewall) {
972cfd25
JA
1164 td->stonewall = stonewall;
1165 stonewall = 0;
1166 }
1167
ebac4655
JA
1168 fgetpos(f, &off);
1169 while ((p = fgets(string, 4096, f)) != NULL) {
1170 if (is_empty_or_comment(p))
1171 continue;
e1f36503 1172
b6754f9d 1173 strip_blank_front(&p);
7c124ac1
JA
1174
1175 if (p[0] == '[')
1176 break;
1177
4ae3f763 1178 strip_blank_end(p);
aea47d44 1179
e1f36503 1180 fgetpos(f, &off);
ebac4655 1181
45410acb
JA
1182 /*
1183 * Don't break here, continue parsing options so we
1184 * dump all the bad ones. Makes trial/error fixups
1185 * easier on the user.
1186 */
7c124ac1 1187 ret |= parse_option(p, options, td);
ebac4655 1188 }
ebac4655 1189
45410acb
JA
1190 if (!ret) {
1191 fsetpos(f, &off);
1192 ret = add_job(td, name, 0);
b1508cf9
JA
1193 } else {
1194 log_err("fio: job %s dropped\n", name);
1195 put_job(td);
45410acb 1196 }
7c124ac1 1197 } while (!ret);
ebac4655
JA
1198
1199 free(string);
1200 free(name);
1201 fclose(f);
45410acb 1202 return ret;
ebac4655
JA
1203}
1204
1205static int fill_def_thread(void)
1206{
1207 memset(&def_thread, 0, sizeof(def_thread));
1208
1209 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1210 perror("sched_getaffinity");
1211 return 1;
1212 }
1213
1214 /*
ee738499 1215 * fill default options
ebac4655 1216 */
ee738499
JA
1217 fill_default_options(&def_thread, options);
1218
972cfd25 1219 def_thread.timeout = def_timeout;
ec94ec56
JA
1220 def_thread.write_bw_log = write_bw_log;
1221 def_thread.write_lat_log = write_lat_log;
ee738499 1222
ebac4655
JA
1223#ifdef FIO_HAVE_DISK_UTIL
1224 def_thread.do_disk_util = 1;
1225#endif
1226
1227 return 0;
1228}
1229
0ab8db89 1230static void usage(void)
4785f995
JA
1231{
1232 printf("%s\n", fio_version_string);
b4692828
JA
1233 printf("\t--output\tWrite output to file\n");
1234 printf("\t--timeout\tRuntime in seconds\n");
1235 printf("\t--latency-log\tGenerate per-job latency logs\n");
1236 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1237 printf("\t--minimal\tMinimal (terse) output\n");
1238 printf("\t--version\tPrint version info and exit\n");
fd28ca49
JA
1239 printf("\t--help\t\tPrint this page\n");
1240 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
4785f995
JA
1241}
1242
972cfd25 1243static int parse_cmd_line(int argc, char *argv[])
ebac4655 1244{
b4692828 1245 struct thread_data *td = NULL;
b1ec1da6 1246 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
ebac4655 1247
0be06ea2 1248 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
ebac4655 1249 switch (c) {
b4692828
JA
1250 case 't':
1251 def_timeout = atoi(optarg);
1252 break;
1253 case 'l':
1254 write_lat_log = 1;
1255 break;
1256 case 'w':
1257 write_bw_log = 1;
1258 break;
1259 case 'o':
1260 f_out = fopen(optarg, "w+");
1261 if (!f_out) {
1262 perror("fopen output");
1263 exit(1);
1264 }
1265 f_err = f_out;
1266 break;
1267 case 'm':
1268 terse_output = 1;
1269 break;
1270 case 'h':
1271 usage();
1272 exit(0);
fd28ca49 1273 case 'c':
29fc6afe
JA
1274 ret = show_cmd_help(options, optarg);
1275 exit(ret);
b4692828
JA
1276 case 'v':
1277 printf("%s\n", fio_version_string);
1278 exit(0);
1279 case FIO_GETOPT_JOB: {
1280 const char *opt = long_options[lidx].name;
1281 char *val = optarg;
1282
c2b1e753
JA
1283 if (!strncmp(opt, "name", 4) && td) {
1284 ret = add_job(td, td->name ?: "fio", 0);
1285 if (ret) {
1286 put_job(td);
1287 return 0;
1288 }
1289 td = NULL;
1290 }
b4692828 1291 if (!td) {
38d0adb0 1292 int global = !strncmp(val, "global", 6);
c2b1e753
JA
1293
1294 td = get_new_job(global, &def_thread);
b4692828
JA
1295 if (!td)
1296 return 0;
1297 }
38d0adb0 1298
b1508cf9 1299 ret = parse_cmd_option(opt, val, options, td);
78217dfa 1300 if (ret)
b1ec1da6 1301 dont_add_job = 1;
b4692828
JA
1302 break;
1303 }
1304 default:
b4692828 1305 break;
ebac4655
JA
1306 }
1307 }
c9fad893 1308
b4692828 1309 if (td) {
b1ec1da6 1310 if (dont_add_job)
b4692828 1311 put_job(td);
b1ec1da6
JA
1312 else {
1313 ret = add_job(td, td->name ?: "fio", 0);
1314 if (ret)
1315 put_job(td);
1316 }
972cfd25 1317 }
774a6177 1318
b4692828
JA
1319 while (optind < argc) {
1320 ini_idx++;
1321 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1322 ini_file[ini_idx - 1] = strdup(argv[optind]);
1323 optind++;
eb8bbf48 1324 }
972cfd25
JA
1325
1326 return ini_idx;
ebac4655
JA
1327}
1328
1329static void free_shm(void)
1330{
1331 struct shmid_ds sbuf;
1332
1333 if (threads) {
2c0ecd28 1334 shmdt((void *) threads);
ebac4655
JA
1335 threads = NULL;
1336 shmctl(shm_id, IPC_RMID, &sbuf);
1337 }
1338}
1339
906c8d75
JA
1340/*
1341 * The thread area is shared between the main process and the job
1342 * threads/processes. So setup a shared memory segment that will hold
1343 * all the job info.
1344 */
ebac4655
JA
1345static int setup_thread_area(void)
1346{
1347 /*
1348 * 1024 is too much on some machines, scale max_jobs if
1349 * we get a failure that looks like too large a shm segment
1350 */
1351 do {
906c8d75 1352 size_t size = max_jobs * sizeof(struct thread_data);
ebac4655 1353
906c8d75 1354 shm_id = shmget(0, size, IPC_CREAT | 0600);
ebac4655
JA
1355 if (shm_id != -1)
1356 break;
1357 if (errno != EINVAL) {
1358 perror("shmget");
1359 break;
1360 }
1361
1362 max_jobs >>= 1;
1363 } while (max_jobs);
1364
1365 if (shm_id == -1)
1366 return 1;
1367
1368 threads = shmat(shm_id, NULL, 0);
1369 if (threads == (void *) -1) {
1370 perror("shmat");
1371 return 1;
1372 }
1373
1374 atexit(free_shm);
1375 return 0;
1376}
1377
b4692828
JA
1378/*
1379 * Copy the fio options into the long options map, so we mirror
1380 * job and cmd line options.
1381 */
1382static void dupe_job_options(void)
1383{
1384 struct fio_option *o;
1385 unsigned int i;
1386
1387 i = 0;
1388 while (long_options[i].name)
1389 i++;
1390
1391 o = &options[0];
1392 while (o->name) {
1393 long_options[i].name = o->name;
1394 long_options[i].val = FIO_GETOPT_JOB;
1395 if (o->type == FIO_OPT_STR_SET)
1396 long_options[i].has_arg = no_argument;
1397 else
1398 long_options[i].has_arg = required_argument;
1399
1400 i++;
1401 o++;
1402 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1403 }
1404}
1405
ebac4655
JA
1406int parse_options(int argc, char *argv[])
1407{
972cfd25
JA
1408 int job_files, i;
1409
b4692828
JA
1410 f_out = stdout;
1411 f_err = stderr;
1412
13335ddb
JA
1413 options_init(options);
1414
b4692828
JA
1415 dupe_job_options();
1416
ebac4655
JA
1417 if (setup_thread_area())
1418 return 1;
1419 if (fill_def_thread())
1420 return 1;
1421
972cfd25 1422 job_files = parse_cmd_line(argc, argv);
ebac4655 1423
972cfd25
JA
1424 for (i = 0; i < job_files; i++) {
1425 if (fill_def_thread())
1426 return 1;
0c7e37a0 1427 if (parse_jobs_ini(ini_file[i], i))
972cfd25 1428 return 1;
88c6ed80 1429 free(ini_file[i]);
972cfd25 1430 }
ebac4655 1431
88c6ed80 1432 free(ini_file);
b4692828
JA
1433
1434 if (!thread_number) {
1435 log_err("No jobs defined(s)\n");
b4692828
JA
1436 return 1;
1437 }
1438
ebac4655
JA
1439 return 0;
1440}