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