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