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