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