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