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