Fio 1.36-rc2
[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 11#include <getopt.h>
ebac4655
JA
12#include <sys/ipc.h>
13#include <sys/shm.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16
17#include "fio.h"
cb2c86fd 18#include "parse.h"
2e5cdb11 19#include "smalloc.h"
380065aa 20#include "filehash.h"
4f5af7b2 21#include "verify.h"
ebac4655 22
d2a1ed1a 23static char fio_version_string[] = "fio 1.36-rc2";
214e1eca 24
ee738499 25#define FIO_RANDSEED (0xb1899bedUL)
ebac4655 26
214e1eca
JA
27static char **ini_file;
28static int max_jobs = MAX_JOBS;
cca73aa7 29static int dump_cmdline;
e1f36503 30
be4ecfdf 31static struct thread_data def_thread;
214e1eca 32struct thread_data *threads = NULL;
e1f36503 33
214e1eca
JA
34int exitall_on_terminate = 0;
35int terse_output = 0;
e592a06b 36int eta_print;
214e1eca
JA
37unsigned long long mlock_size = 0;
38FILE *f_out = NULL;
39FILE *f_err = NULL;
01f06b63 40char *job_section = NULL;
ee738499 41
214e1eca 42int write_bw_log = 0;
4241ea8f 43int read_only = 0;
214e1eca 44
5ec10eaa
JA
45static int def_timeout;
46static int write_lat_log;
e1f36503 47
214e1eca 48static int prev_group_jobs;
b4692828 49
ee56ad50 50unsigned long fio_debug = 0;
5e1d306e
JA
51unsigned int fio_debug_jobno = -1;
52unsigned int *fio_debug_jobp = NULL;
ee56ad50 53
b4692828
JA
54/*
55 * Command line options. These will contain the above, plus a few
56 * extra that only pertain to fio itself and not jobs.
57 */
5ec10eaa 58static struct option l_opts[FIO_NR_OPTIONS] = {
b4692828
JA
59 {
60 .name = "output",
61 .has_arg = required_argument,
62 .val = 'o',
63 },
64 {
65 .name = "timeout",
66 .has_arg = required_argument,
67 .val = 't',
68 },
69 {
70 .name = "latency-log",
71 .has_arg = required_argument,
72 .val = 'l',
73 },
74 {
75 .name = "bandwidth-log",
76 .has_arg = required_argument,
77 .val = 'b',
78 },
79 {
80 .name = "minimal",
81 .has_arg = optional_argument,
82 .val = 'm',
83 },
84 {
85 .name = "version",
86 .has_arg = no_argument,
87 .val = 'v',
88 },
fd28ca49
JA
89 {
90 .name = "help",
91 .has_arg = no_argument,
92 .val = 'h',
93 },
94 {
95 .name = "cmdhelp",
320beefe 96 .has_arg = optional_argument,
fd28ca49
JA
97 .val = 'c',
98 },
cca73aa7
JA
99 {
100 .name = "showcmd",
101 .has_arg = no_argument,
724e4435
JA
102 .val = 's',
103 },
104 {
105 .name = "readonly",
106 .has_arg = no_argument,
107 .val = 'r',
cca73aa7 108 },
e592a06b
AC
109 {
110 .name = "eta",
111 .has_arg = required_argument,
112 .val = 'e',
113 },
ee56ad50
JA
114 {
115 .name = "debug",
116 .has_arg = required_argument,
117 .val = 'd',
118 },
01f06b63
JA
119 {
120 .name = "section",
121 .has_arg = required_argument,
122 .val = 'x',
123 },
2b386d25
JA
124 {
125 .name = "alloc-size",
126 .has_arg = required_argument,
127 .val = 'a',
128 },
9ac8a797
JA
129 {
130 .name = "profile",
131 .has_arg = required_argument,
132 .val = 'p',
133 },
b4692828
JA
134 {
135 .name = NULL,
136 },
137};
138
9ac8a797 139static const char *tiobench_opts[] = {
3f5bf577 140 "buffered=0", "size=4*1024*$mb_memory", "bs=4k", "timeout=600",
9ac8a797
JA
141 "numjobs=4", "group_reporting", "thread", "overwrite=1",
142 "filename=.fio.tio.1:.fio.tio.2:.fio.tio.3:.fio.tio.4",
143 "name=seqwrite", "rw=write", "end_fsync=1",
144 "name=randwrite", "stonewall", "rw=randwrite", "end_fsync=1",
145 "name=seqread", "stonewall", "rw=read",
146 "name=randread", "stonewall", "rw=randread", NULL,
147};
148
149static const char **fio_prof_strings[PROFILE_END] = {
150 NULL,
151 tiobench_opts,
152};
153
154static const char *profiles[PROFILE_END] = {
155 "none",
156 "tiobench",
157};
158
9728ce37
JB
159FILE *get_f_out()
160{
161 return f_out;
162}
163
164FILE *get_f_err()
165{
166 return f_err;
167}
168
906c8d75
JA
169/*
170 * Return a free job structure.
171 */
ebac4655
JA
172static struct thread_data *get_new_job(int global, struct thread_data *parent)
173{
174 struct thread_data *td;
175
176 if (global)
177 return &def_thread;
178 if (thread_number >= max_jobs)
179 return NULL;
180
181 td = &threads[thread_number++];
ddaeaa5a 182 *td = *parent;
ebac4655 183
e0b0d892
JA
184 td->o.uid = td->o.gid = -1U;
185
cade3ef4 186 dup_files(td, parent);
d23bb327 187 options_mem_dupe(td);
cade3ef4 188
ebac4655 189 td->thread_number = thread_number;
ebac4655
JA
190 return td;
191}
192
193static void put_job(struct thread_data *td)
194{
549577a7
JA
195 if (td == &def_thread)
196 return;
197
16edf25d 198 if (td->error)
6d86144d 199 log_info("fio: %s\n", td->verror);
16edf25d 200
ebac4655
JA
201 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
202 thread_number--;
203}
204
581e7141 205static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
127f6865 206{
581e7141 207 unsigned int bs = td->o.min_bs[ddir];
127f6865 208 unsigned long long rate;
581e7141 209 unsigned long ios_per_msec;
127f6865 210
581e7141
JA
211 if (td->o.rate[ddir]) {
212 rate = td->o.rate[ddir];
213 ios_per_msec = (rate * 1000LL) / bs;
127f6865 214 } else
581e7141 215 ios_per_msec = td->o.rate_iops[ddir] * 1000UL;
127f6865 216
581e7141 217 if (!ios_per_msec) {
127f6865
JA
218 log_err("rate lower than supported\n");
219 return -1;
220 }
221
581e7141
JA
222 td->rate_usec_cycle[ddir] = 1000000000ULL / ios_per_msec;
223 td->rate_pending_usleep[ddir] = 0;
127f6865
JA
224 return 0;
225}
226
581e7141
JA
227static int setup_rate(struct thread_data *td)
228{
229 int ret = 0;
230
231 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
232 ret = __setup_rate(td, DDIR_READ);
233 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
234 ret |= __setup_rate(td, DDIR_WRITE);
235
236 return ret;
237}
238
8347239a
JA
239static int fixed_block_size(struct thread_options *o)
240{
241 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
242 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
243 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
244}
245
dad915e3
JA
246/*
247 * Lazy way of fixing up options that depend on each other. We could also
248 * define option callback handlers, but this is easier.
249 */
4e991c23 250static int fixup_options(struct thread_data *td)
e1f36503 251{
2dc1bbeb 252 struct thread_options *o = &td->o;
dad915e3 253
f356d01d
JA
254#ifndef FIO_HAVE_PSHARED_MUTEX
255 if (!td->o.use_thread) {
256 log_info("fio: this platform does not support process shared"
257 " mutexes, forcing use of threads. Use the 'thread'"
258 " option to get rid of this warning.\n");
259 td->o.use_thread = 1;
260 }
261#endif
262
2dc1bbeb 263 if (o->write_iolog_file && o->read_iolog_file) {
076efc7c 264 log_err("fio: read iolog overrides write_iolog\n");
2dc1bbeb
JA
265 free(o->write_iolog_file);
266 o->write_iolog_file = NULL;
076efc7c 267 }
16b462ae 268
16b462ae
JA
269 /*
270 * only really works for sequential io for now, and with 1 file
271 */
2dc1bbeb
JA
272 if (o->zone_size && td_random(td) && o->open_files == 1)
273 o->zone_size = 0;
16b462ae
JA
274
275 /*
276 * Reads can do overwrites, we always need to pre-create the file
277 */
278 if (td_read(td) || td_rw(td))
2dc1bbeb 279 o->overwrite = 1;
16b462ae 280
2dc1bbeb 281 if (!o->min_bs[DDIR_READ])
5ec10eaa 282 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
2dc1bbeb
JA
283 if (!o->max_bs[DDIR_READ])
284 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
285 if (!o->min_bs[DDIR_WRITE])
5ec10eaa 286 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
2dc1bbeb
JA
287 if (!o->max_bs[DDIR_WRITE])
288 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
a00735e6 289
2dc1bbeb 290 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
a00735e6 291
2b7a01d0
JA
292 /*
293 * For random IO, allow blockalign offset other than min_bs.
294 */
295 if (!o->ba[DDIR_READ] || !td_random(td))
296 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
297 if (!o->ba[DDIR_WRITE] || !td_random(td))
298 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
299
300 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
301 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
302 !td->o.norandommap) {
303 log_err("fio: Any use of blockalign= turns off randommap\n");
304 td->o.norandommap = 1;
305 }
306
2dc1bbeb
JA
307 if (!o->file_size_high)
308 o->file_size_high = o->file_size_low;
9c60ce64 309
8347239a
JA
310 if (o->norandommap && o->verify != VERIFY_NONE
311 && !fixed_block_size(o)) {
312 log_err("fio: norandommap given for variable block sizes, "
313 "verify disabled\n");
2dc1bbeb 314 o->verify = VERIFY_NONE;
bb8895e0 315 }
2dc1bbeb 316 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
690adba3 317 log_err("fio: bs_unaligned may not work with raw io\n");
e0a22335 318
48097d5c
JA
319 /*
320 * thinktime_spin must be less than thinktime
321 */
2dc1bbeb
JA
322 if (o->thinktime_spin > o->thinktime)
323 o->thinktime_spin = o->thinktime;
e916b390
JA
324
325 /*
326 * The low water mark cannot be bigger than the iodepth
327 */
2dc1bbeb 328 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
9467b77c
JA
329 /*
330 * syslet work around - if the workload is sequential,
331 * we want to let the queue drain all the way down to
332 * avoid seeking between async threads
333 */
334 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
2dc1bbeb 335 o->iodepth_low = 1;
9467b77c 336 else
2dc1bbeb 337 o->iodepth_low = o->iodepth;
9467b77c 338 }
cb5ab512
JA
339
340 /*
341 * If batch number isn't set, default to the same as iodepth
342 */
2dc1bbeb
JA
343 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
344 o->iodepth_batch = o->iodepth;
b5af8293 345
2dc1bbeb
JA
346 if (o->nr_files > td->files_index)
347 o->nr_files = td->files_index;
9f9214f2 348
2dc1bbeb
JA
349 if (o->open_files > o->nr_files || !o->open_files)
350 o->open_files = o->nr_files;
4e991c23 351
581e7141
JA
352 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
353 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
354 o->rate_iops_min[1]))) {
4e991c23
JA
355 log_err("fio: rate and rate_iops are mutually exclusive\n");
356 return 1;
357 }
581e7141
JA
358 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
359 (o->rate_iops[0] < o->rate_iops_min[0]) ||
360 (o->rate_iops[1] < o->rate_iops_min[1])) {
4e991c23
JA
361 log_err("fio: minimum rate exceeds rate\n");
362 return 1;
363 }
364
cf4464ca
JA
365 if (!o->timeout && o->time_based) {
366 log_err("fio: time_based requires a runtime/timeout setting\n");
367 o->time_based = 0;
368 }
369
aa31f1f1 370 if (o->fill_device && !o->size)
5921e80c 371 o->size = -1ULL;
5ec10eaa 372
79713149
JA
373 if (td_rw(td) && td->o.verify != VERIFY_NONE)
374 log_info("fio: mixed read/write workload with verify. May not "
375 "work as expected, unless you pre-populated the file\n");
aa31f1f1 376
41ccd845
JA
377 if (td->o.verify != VERIFY_NONE)
378 td->o.refill_buffers = 1;
379
9c0d2241 380 if (td->o.pre_read) {
34f1c044 381 td->o.invalidate_cache = 0;
9c0d2241
JA
382 if (td->io_ops->flags & FIO_PIPEIO)
383 log_info("fio: cannot pre-read files with an IO engine"
384 " that isn't seekable. Pre-read disabled.\n");
385 }
34f1c044 386
4e991c23 387 return 0;
e1f36503
JA
388}
389
f8977ee6
JA
390/*
391 * This function leaks the buffer
392 */
393static char *to_kmg(unsigned int val)
394{
395 char *buf = malloc(32);
f3502ba2 396 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
f8977ee6
JA
397 char *p = post;
398
245142ff 399 do {
f8977ee6
JA
400 if (val & 1023)
401 break;
402
403 val >>= 10;
404 p++;
245142ff 405 } while (*p);
f8977ee6
JA
406
407 snprintf(buf, 31, "%u%c", val, *p);
408 return buf;
409}
410
09629a90
JA
411/* External engines are specified by "external:name.o") */
412static const char *get_engine_name(const char *str)
413{
414 char *p = strstr(str, ":");
415
416 if (!p)
417 return str;
418
419 p++;
420 strip_blank_front(&p);
421 strip_blank_end(p);
422 return p;
423}
424
e132cbae
JA
425static int exists_and_not_file(const char *filename)
426{
427 struct stat sb;
428
429 if (lstat(filename, &sb) == -1)
430 return 0;
431
432 if (S_ISREG(sb.st_mode))
433 return 0;
434
435 return 1;
436}
437
5bfc35d7
JA
438void td_fill_rand_seeds(struct thread_data *td)
439{
440 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
441 os_random_seed(td->rand_seeds[1], &td->verify_state);
442 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
443
444 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
445 os_random_seed(td->rand_seeds[3], &td->next_file_state);
446
447 os_random_seed(td->rand_seeds[5], &td->file_size_state);
448
449 if (!td_random(td))
450 return;
451
452 if (td->o.rand_repeatable)
453 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
454
455 os_random_seed(td->rand_seeds[4], &td->random_state);
456}
457
9c60ce64
JA
458/*
459 * Initialize the various random states we need (random io, block size ranges,
460 * read/write mix, etc).
461 */
462static int init_random_state(struct thread_data *td)
463{
68727076 464 int fd;
9c60ce64
JA
465
466 fd = open("/dev/urandom", O_RDONLY);
467 if (fd == -1) {
468 td_verror(td, errno, "open");
469 return 1;
470 }
471
5bfc35d7
JA
472 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
473 (int) sizeof(td->rand_seeds)) {
9c60ce64
JA
474 td_verror(td, EIO, "read");
475 close(fd);
476 return 1;
477 }
478
479 close(fd);
5bfc35d7 480 td_fill_rand_seeds(td);
9c60ce64
JA
481 return 0;
482}
483
906c8d75
JA
484/*
485 * Adds a job to the list of things todo. Sanitizes the various options
486 * to make sure we don't have conflicts, and initializes various
487 * members of td.
488 */
75154845 489static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
ebac4655 490{
413dd459
JA
491 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
492 "randread", "randwrite", "randrw" };
af52b345 493 unsigned int i;
09629a90 494 const char *engine;
af52b345 495 char fname[PATH_MAX];
e132cbae 496 int numjobs, file_alloced;
ebac4655 497
ebac4655
JA
498 /*
499 * the def_thread is just for options, it's not a real job
500 */
501 if (td == &def_thread)
502 return 0;
503
cca73aa7
JA
504 /*
505 * if we are just dumping the output command line, don't add the job
506 */
507 if (dump_cmdline) {
508 put_job(td);
509 return 0;
510 }
511
2dc1bbeb 512 engine = get_engine_name(td->o.ioengine);
09629a90
JA
513 td->io_ops = load_ioengine(td, engine);
514 if (!td->io_ops) {
515 log_err("fio: failed to load engine %s\n", engine);
205927a3 516 goto err;
09629a90 517 }
df64119d 518
2dc1bbeb 519 if (td->o.use_thread)
9cedf167
JA
520 nr_thread++;
521 else
522 nr_process++;
523
2dc1bbeb 524 if (td->o.odirect)
690adba3
JA
525 td->io_ops->flags |= FIO_RAWIO;
526
e132cbae 527 file_alloced = 0;
691c8fb0 528 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
e132cbae 529 file_alloced = 1;
80be24f4 530
2dc1bbeb 531 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
e132cbae 532 add_file(td, jobname);
7b05a215 533 else {
2dc1bbeb 534 for (i = 0; i < td->o.nr_files; i++) {
5ec10eaa
JA
535 sprintf(fname, "%s.%d.%d", jobname,
536 td->thread_number, i);
7b05a215
JA
537 add_file(td, fname);
538 }
af52b345 539 }
0af7b542 540 }
ebac4655 541
4e991c23
JA
542 if (fixup_options(td))
543 goto err;
e0a22335 544
07eb79df
JA
545 if (td->io_ops->flags & FIO_DISKLESSIO) {
546 struct fio_file *f;
547
548 for_each_file(td, f, i)
549 f->real_file_size = -1ULL;
550 }
551
cdd18ad8 552 td->mutex = fio_mutex_init(0);
ebac4655 553
756867bd
JA
554 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
555 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
556 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
1e3d53ac 557 td->ddir_nr = td->o.ddir_nr;
ebac4655 558
755c3189 559 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
3c5df6fa 560 prev_group_jobs = 0;
ebac4655 561 groupid++;
3c5df6fa 562 }
ebac4655
JA
563
564 td->groupid = groupid;
3c5df6fa 565 prev_group_jobs++;
ebac4655 566
9c60ce64
JA
567 if (init_random_state(td))
568 goto err;
569
ebac4655
JA
570 if (setup_rate(td))
571 goto err;
572
2dc1bbeb 573 if (td->o.write_lat_log) {
756867bd
JA
574 setup_log(&td->ts.slat_log);
575 setup_log(&td->ts.clat_log);
ebac4655 576 }
2dc1bbeb 577 if (td->o.write_bw_log)
756867bd 578 setup_log(&td->ts.bw_log);
ebac4655 579
2dc1bbeb
JA
580 if (!td->o.name)
581 td->o.name = strdup(jobname);
01452055 582
c6ae0a5b 583 if (!terse_output) {
b990b5c0 584 if (!job_add_num) {
5ec10eaa
JA
585 if (!strcmp(td->io_ops->name, "cpuio")) {
586 log_info("%s: ioengine=cpu, cpuload=%u,"
587 " cpucycle=%u\n", td->o.name,
588 td->o.cpuload,
589 td->o.cpucycle);
590 } else {
f8977ee6
JA
591 char *c1, *c2, *c3, *c4;
592
2dc1bbeb
JA
593 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
594 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
595 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
596 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
f8977ee6 597
5ec10eaa
JA
598 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
599 " ioengine=%s, iodepth=%u\n",
600 td->o.name, td->groupid,
601 ddir_str[td->o.td_ddir],
602 c1, c2, c3, c4,
603 td->io_ops->name,
604 td->o.iodepth);
f8977ee6
JA
605
606 free(c1);
607 free(c2);
608 free(c3);
609 free(c4);
610 }
b990b5c0 611 } else if (job_add_num == 1)
6d86144d 612 log_info("...\n");
c6ae0a5b 613 }
ebac4655
JA
614
615 /*
616 * recurse add identical jobs, clear numjobs and stonewall options
617 * as they don't apply to sub-jobs
618 */
2dc1bbeb 619 numjobs = td->o.numjobs;
ebac4655
JA
620 while (--numjobs) {
621 struct thread_data *td_new = get_new_job(0, td);
622
623 if (!td_new)
624 goto err;
625
2dc1bbeb
JA
626 td_new->o.numjobs = 1;
627 td_new->o.stonewall = 0;
92c1d41f 628 td_new->o.new_group = 0;
e132cbae
JA
629
630 if (file_alloced) {
2dc1bbeb 631 td_new->o.filename = NULL;
e132cbae 632 td_new->files_index = 0;
4e6ea2f1 633 td_new->files_size = 0;
e132cbae
JA
634 td_new->files = NULL;
635 }
636
75154845 637 job_add_num = numjobs - 1;
ebac4655 638
75154845 639 if (add_job(td_new, jobname, job_add_num))
ebac4655
JA
640 goto err;
641 }
3c5df6fa 642
ebac4655
JA
643 return 0;
644err:
645 put_job(td);
646 return -1;
647}
648
01f06b63
JA
649static int skip_this_section(const char *name)
650{
651 if (!job_section)
652 return 0;
653 if (!strncmp(name, "global", 6))
654 return 0;
655
b36beb6e 656 return strcmp(job_section, name);
01f06b63
JA
657}
658
ebac4655
JA
659static int is_empty_or_comment(char *line)
660{
661 unsigned int i;
662
663 for (i = 0; i < strlen(line); i++) {
664 if (line[i] == ';')
665 return 1;
5cc2da30
IM
666 if (line[i] == '#')
667 return 1;
ebac4655
JA
668 if (!isspace(line[i]) && !iscntrl(line[i]))
669 return 0;
670 }
671
672 return 1;
673}
674
07261983
JA
675/*
676 * This is our [ini] type file parser.
677 */
1e97cce9 678static int parse_jobs_ini(char *file, int stonewall_flag)
ebac4655 679{
e1f36503 680 unsigned int global;
ebac4655 681 struct thread_data *td;
fee3bb48 682 char *string, *name;
ebac4655
JA
683 FILE *f;
684 char *p;
0c7e37a0 685 int ret = 0, stonewall;
097b2991 686 int first_sect = 1;
ccf8f127 687 int skip_fgets = 0;
01f06b63 688 int inside_skip = 0;
3b8b7135
JA
689 char **opts;
690 int i, alloc_opts, num_opts;
ebac4655 691
5a729cbe
AC
692 if (!strcmp(file, "-"))
693 f = stdin;
694 else
695 f = fopen(file, "r");
696
ebac4655 697 if (!f) {
aea47d44 698 perror("fopen job file");
ebac4655
JA
699 return 1;
700 }
701
702 string = malloc(4096);
7f7e6e59
JA
703
704 /*
705 * it's really 256 + small bit, 280 should suffice
706 */
707 name = malloc(280);
708 memset(name, 0, 280);
ebac4655 709
3b8b7135
JA
710 alloc_opts = 8;
711 opts = malloc(sizeof(char *) * alloc_opts);
b7c6302d 712 num_opts = 0;
3b8b7135 713
0c7e37a0 714 stonewall = stonewall_flag;
7c124ac1 715 do {
ccf8f127
JA
716 /*
717 * if skip_fgets is set, we already have loaded a line we
718 * haven't handled.
719 */
720 if (!skip_fgets) {
721 p = fgets(string, 4095, f);
722 if (!p)
723 break;
724 }
cdc7f193 725
ccf8f127 726 skip_fgets = 0;
cdc7f193 727 strip_blank_front(&p);
6c7c7da1 728 strip_blank_end(p);
cdc7f193 729
ebac4655
JA
730 if (is_empty_or_comment(p))
731 continue;
6c7c7da1 732 if (sscanf(p, "[%255s]", name) != 1) {
01f06b63
JA
733 if (inside_skip)
734 continue;
5ec10eaa
JA
735 log_err("fio: option <%s> outside of [] job section\n",
736 p);
088b4207 737 break;
6c7c7da1 738 }
ebac4655 739
01f06b63
JA
740 if (skip_this_section(name)) {
741 inside_skip = 1;
742 continue;
743 } else
744 inside_skip = 0;
745
ebac4655
JA
746 global = !strncmp(name, "global", 6);
747
748 name[strlen(name) - 1] = '\0';
749
cca73aa7 750 if (dump_cmdline) {
097b2991
JA
751 if (first_sect)
752 log_info("fio ");
cca73aa7
JA
753 if (!global)
754 log_info("--name=%s ", name);
097b2991 755 first_sect = 0;
cca73aa7
JA
756 }
757
ebac4655 758 td = get_new_job(global, &def_thread);
45410acb
JA
759 if (!td) {
760 ret = 1;
761 break;
762 }
ebac4655 763
972cfd25
JA
764 /*
765 * Seperate multiple job files by a stonewall
766 */
f9481919 767 if (!global && stonewall) {
2dc1bbeb 768 td->o.stonewall = stonewall;
972cfd25
JA
769 stonewall = 0;
770 }
771
3b8b7135
JA
772 num_opts = 0;
773 memset(opts, 0, alloc_opts * sizeof(char *));
774
ebac4655
JA
775 while ((p = fgets(string, 4096, f)) != NULL) {
776 if (is_empty_or_comment(p))
777 continue;
e1f36503 778
b6754f9d 779 strip_blank_front(&p);
7c124ac1 780
ccf8f127
JA
781 /*
782 * new section, break out and make sure we don't
783 * fgets() a new line at the top.
784 */
785 if (p[0] == '[') {
786 skip_fgets = 1;
7c124ac1 787 break;
ccf8f127 788 }
7c124ac1 789
4ae3f763 790 strip_blank_end(p);
aea47d44 791
3b8b7135
JA
792 if (num_opts == alloc_opts) {
793 alloc_opts <<= 1;
794 opts = realloc(opts,
795 alloc_opts * sizeof(char *));
796 }
797
798 opts[num_opts] = strdup(p);
799 num_opts++;
ebac4655 800 }
ebac4655 801
3b8b7135
JA
802 ret = fio_options_parse(td, opts, num_opts);
803 if (!ret) {
804 if (dump_cmdline)
805 for (i = 0; i < num_opts; i++)
806 log_info("--%s ", opts[i]);
807
45410acb 808 ret = add_job(td, name, 0);
3b8b7135 809 } else {
b1508cf9
JA
810 log_err("fio: job %s dropped\n", name);
811 put_job(td);
45410acb 812 }
3b8b7135
JA
813
814 for (i = 0; i < num_opts; i++)
815 free(opts[i]);
816 num_opts = 0;
7c124ac1 817 } while (!ret);
ebac4655 818
cca73aa7
JA
819 if (dump_cmdline)
820 log_info("\n");
821
3b8b7135
JA
822 for (i = 0; i < num_opts; i++)
823 free(opts[i]);
824
ebac4655
JA
825 free(string);
826 free(name);
2577594d 827 free(opts);
5a729cbe
AC
828 if (f != stdin)
829 fclose(f);
45410acb 830 return ret;
ebac4655
JA
831}
832
833static int fill_def_thread(void)
834{
835 memset(&def_thread, 0, sizeof(def_thread));
836
375b2695 837 fio_getaffinity(getpid(), &def_thread.o.cpumask);
ebac4655
JA
838
839 /*
ee738499 840 * fill default options
ebac4655 841 */
214e1eca 842 fio_fill_default_options(&def_thread);
ee738499 843
2dc1bbeb
JA
844 def_thread.o.timeout = def_timeout;
845 def_thread.o.write_bw_log = write_bw_log;
846 def_thread.o.write_lat_log = write_lat_log;
ee738499 847
ebac4655
JA
848 return 0;
849}
850
214e1eca
JA
851static void free_shm(void)
852{
853 struct shmid_ds sbuf;
854
855 if (threads) {
5e1d306e
JA
856 void *tp = threads;
857
214e1eca 858 threads = NULL;
5e1d306e
JA
859 file_hash_exit();
860 fio_debug_jobp = NULL;
861 shmdt(tp);
214e1eca
JA
862 shmctl(shm_id, IPC_RMID, &sbuf);
863 }
2e5cdb11
JA
864
865 scleanup();
214e1eca
JA
866}
867
868/*
869 * The thread area is shared between the main process and the job
870 * threads/processes. So setup a shared memory segment that will hold
380065aa
JA
871 * all the job info. We use the end of the region for keeping track of
872 * open files across jobs, for file sharing.
214e1eca
JA
873 */
874static int setup_thread_area(void)
875{
380065aa
JA
876 void *hash;
877
214e1eca
JA
878 /*
879 * 1024 is too much on some machines, scale max_jobs if
880 * we get a failure that looks like too large a shm segment
881 */
882 do {
883 size_t size = max_jobs * sizeof(struct thread_data);
884
380065aa 885 size += file_hash_size;
5e1d306e 886 size += sizeof(unsigned int);
380065aa 887
214e1eca
JA
888 shm_id = shmget(0, size, IPC_CREAT | 0600);
889 if (shm_id != -1)
890 break;
891 if (errno != EINVAL) {
892 perror("shmget");
893 break;
894 }
895
896 max_jobs >>= 1;
897 } while (max_jobs);
898
899 if (shm_id == -1)
900 return 1;
901
902 threads = shmat(shm_id, NULL, 0);
903 if (threads == (void *) -1) {
904 perror("shmat");
905 return 1;
906 }
907
1f809d15 908 memset(threads, 0, max_jobs * sizeof(struct thread_data));
380065aa 909 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
5e1d306e
JA
910 fio_debug_jobp = (void *) hash + file_hash_size;
911 *fio_debug_jobp = -1;
380065aa 912 file_hash_init(hash);
214e1eca
JA
913 atexit(free_shm);
914 return 0;
915}
916
45378b35 917static void usage(const char *name)
4785f995
JA
918{
919 printf("%s\n", fio_version_string);
45378b35 920 printf("%s [options] [job options] <job file(s)>\n", name);
ee56ad50 921 printf("\t--debug=options\tEnable debug logging\n");
b4692828
JA
922 printf("\t--output\tWrite output to file\n");
923 printf("\t--timeout\tRuntime in seconds\n");
924 printf("\t--latency-log\tGenerate per-job latency logs\n");
925 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
926 printf("\t--minimal\tMinimal (terse) output\n");
927 printf("\t--version\tPrint version info and exit\n");
fd28ca49 928 printf("\t--help\t\tPrint this page\n");
5ec10eaa
JA
929 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
930 " them\n");
cca73aa7 931 printf("\t--showcmd\tTurn a job file into command line options\n");
e592a06b
AC
932 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
933 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
5ec10eaa
JA
934 printf("\t--readonly\tTurn on safety read-only checks, preventing"
935 " writes\n");
01f06b63 936 printf("\t--section=name\tOnly run specified section in job file\n");
2b386d25
JA
937 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
938 " (def 1024)\n");
fd4442b8 939 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>\n");
4785f995
JA
940}
941
79e48f72 942#ifdef FIO_INC_DEBUG
ee56ad50 943struct debug_level debug_levels[] = {
bd6f78b2
JA
944 { .name = "process", .shift = FD_PROCESS, },
945 { .name = "file", .shift = FD_FILE, },
946 { .name = "io", .shift = FD_IO, },
947 { .name = "mem", .shift = FD_MEM, },
948 { .name = "blktrace", .shift = FD_BLKTRACE },
949 { .name = "verify", .shift = FD_VERIFY },
84422acd 950 { .name = "random", .shift = FD_RANDOM },
a3d741fa 951 { .name = "parse", .shift = FD_PARSE },
cd991b9e 952 { .name = "diskutil", .shift = FD_DISKUTIL },
5e1d306e 953 { .name = "job", .shift = FD_JOB },
29adda3c 954 { .name = "mutex", .shift = FD_MUTEX },
02444ad1 955 { .name = NULL, },
ee56ad50
JA
956};
957
c09823ab 958static int set_debug(const char *string)
ee56ad50
JA
959{
960 struct debug_level *dl;
961 char *p = (char *) string;
962 char *opt;
963 int i;
964
965 if (!strcmp(string, "?") || !strcmp(string, "help")) {
966 int i;
967
968 log_info("fio: dumping debug options:");
969 for (i = 0; debug_levels[i].name; i++) {
970 dl = &debug_levels[i];
971 log_info("%s,", dl->name);
972 }
bd6f78b2 973 log_info("all\n");
c09823ab 974 return 1;
ee56ad50
JA
975 }
976
977 while ((opt = strsep(&p, ",")) != NULL) {
978 int found = 0;
979
5e1d306e
JA
980 if (!strncmp(opt, "all", 3)) {
981 log_info("fio: set all debug options\n");
982 fio_debug = ~0UL;
983 continue;
984 }
985
ee56ad50
JA
986 for (i = 0; debug_levels[i].name; i++) {
987 dl = &debug_levels[i];
5e1d306e
JA
988 found = !strncmp(opt, dl->name, strlen(dl->name));
989 if (!found)
990 continue;
991
992 if (dl->shift == FD_JOB) {
993 opt = strchr(opt, ':');
994 if (!opt) {
995 log_err("fio: missing job number\n");
996 break;
997 }
998 opt++;
999 fio_debug_jobno = atoi(opt);
1000 log_info("fio: set debug jobno %d\n",
1001 fio_debug_jobno);
1002 } else {
ee56ad50 1003 log_info("fio: set debug option %s\n", opt);
bd6f78b2 1004 fio_debug |= (1UL << dl->shift);
ee56ad50 1005 }
5e1d306e 1006 break;
ee56ad50
JA
1007 }
1008
1009 if (!found)
1010 log_err("fio: debug mask %s not found\n", opt);
1011 }
c09823ab 1012 return 0;
ee56ad50 1013}
79e48f72 1014#else
69b98d4c 1015static int set_debug(const char *string)
79e48f72
JA
1016{
1017 log_err("fio: debug tracing not included in build\n");
c09823ab 1018 return 1;
79e48f72
JA
1019}
1020#endif
ee56ad50 1021
9ac8a797
JA
1022static int load_profile(const char *profile)
1023{
1024 struct thread_data *td, *td_parent;
1025 const char **o;
1026 int i, in_global = 1;
1027 char jobname[32];
1028
1029 dprint(FD_PARSE, "loading profile %s\n", profile);
1030
1031 for (i = 0; i < PROFILE_END; i++) {
1032 if (!strcmp(profile, profiles[i]))
1033 break;
1034 }
1035
1036 if (i == PROFILE_END) {
1037 log_err("fio: unknown profile %s\n", profile);
1038 return 1;
1039 }
1040
1041 o = fio_prof_strings[i];
1042 if (!o)
1043 return 0;
1044
1045 i = 0;
1046 td_parent = td = NULL;
1047 while (o[i]) {
1048 if (!strncmp(o[i], "name", 4)) {
1049 in_global = 0;
1050 if (td)
1051 add_job(td, jobname, 0);
1052 td = NULL;
1053 sprintf(jobname, "%s", o[i] + 5);
1054 }
1055 if (in_global && !td_parent)
1056 td_parent = get_new_job(1, &def_thread);
1057 else if (!in_global && !td) {
1058 if (!td_parent)
1059 td_parent = &def_thread;
1060 td = get_new_job(0, td_parent);
1061 }
1062 if (in_global)
1063 fio_options_parse(td_parent, (char **) &o[i], 1);
1064 else
1065 fio_options_parse(td, (char **) &o[i], 1);
1066 i++;
1067 }
1068 if (td)
1069 add_job(td, jobname, 0);
1070 return 0;
1071}
1072
972cfd25 1073static int parse_cmd_line(int argc, char *argv[])
ebac4655 1074{
b4692828 1075 struct thread_data *td = NULL;
c09823ab 1076 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
ebac4655 1077
5ec10eaa 1078 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
ebac4655 1079 switch (c) {
2b386d25
JA
1080 case 'a':
1081 smalloc_pool_size = atoi(optarg);
1082 break;
b4692828
JA
1083 case 't':
1084 def_timeout = atoi(optarg);
1085 break;
1086 case 'l':
1087 write_lat_log = 1;
1088 break;
1089 case 'w':
1090 write_bw_log = 1;
1091 break;
1092 case 'o':
1093 f_out = fopen(optarg, "w+");
1094 if (!f_out) {
1095 perror("fopen output");
1096 exit(1);
1097 }
1098 f_err = f_out;
1099 break;
1100 case 'm':
1101 terse_output = 1;
1102 break;
1103 case 'h':
45378b35 1104 usage(argv[0]);
b4692828 1105 exit(0);
fd28ca49 1106 case 'c':
214e1eca 1107 exit(fio_show_option_help(optarg));
cca73aa7
JA
1108 case 's':
1109 dump_cmdline = 1;
1110 break;
724e4435
JA
1111 case 'r':
1112 read_only = 1;
1113 break;
b4692828
JA
1114 case 'v':
1115 printf("%s\n", fio_version_string);
1116 exit(0);
e592a06b
AC
1117 case 'e':
1118 if (!strcmp("always", optarg))
1119 eta_print = FIO_ETA_ALWAYS;
1120 else if (!strcmp("never", optarg))
1121 eta_print = FIO_ETA_NEVER;
1122 break;
ee56ad50 1123 case 'd':
c09823ab
JA
1124 if (set_debug(optarg))
1125 do_exit++;
ee56ad50 1126 break;
01f06b63
JA
1127 case 'x':
1128 if (!strcmp(optarg, "global")) {
5ec10eaa
JA
1129 log_err("fio: can't use global as only "
1130 "section\n");
c09823ab
JA
1131 do_exit++;
1132 exit_val = 1;
01f06b63
JA
1133 break;
1134 }
1135 if (job_section)
1136 free(job_section);
1137 job_section = strdup(optarg);
1138 break;
9ac8a797
JA
1139 case 'p':
1140 if (load_profile(optarg))
1141 do_exit++;
1142 break;
b4692828 1143 case FIO_GETOPT_JOB: {
5ec10eaa 1144 const char *opt = l_opts[lidx].name;
b4692828
JA
1145 char *val = optarg;
1146
c2b1e753 1147 if (!strncmp(opt, "name", 4) && td) {
2dc1bbeb 1148 ret = add_job(td, td->o.name ?: "fio", 0);
c2b1e753
JA
1149 if (ret) {
1150 put_job(td);
1151 return 0;
1152 }
1153 td = NULL;
1154 }
b4692828 1155 if (!td) {
01f06b63 1156 int is_section = !strncmp(opt, "name", 4);
3106f220
JA
1157 int global = 0;
1158
01f06b63 1159 if (!is_section || !strncmp(val, "global", 6))
3106f220 1160 global = 1;
c2b1e753 1161
01f06b63
JA
1162 if (is_section && skip_this_section(val))
1163 continue;
1164
c2b1e753 1165 td = get_new_job(global, &def_thread);
b4692828
JA
1166 if (!td)
1167 return 0;
1168 }
38d0adb0 1169
214e1eca 1170 ret = fio_cmd_option_parse(td, opt, val);
b4692828
JA
1171 break;
1172 }
1173 default:
c09823ab
JA
1174 do_exit++;
1175 exit_val = 1;
b4692828 1176 break;
ebac4655
JA
1177 }
1178 }
c9fad893 1179
c09823ab
JA
1180 if (do_exit)
1181 exit(exit_val);
536582bf 1182
b4692828 1183 if (td) {
7d6a8904 1184 if (!ret)
2dc1bbeb 1185 ret = add_job(td, td->o.name ?: "fio", 0);
7d6a8904
JA
1186 if (ret)
1187 put_job(td);
972cfd25 1188 }
774a6177 1189
b4692828
JA
1190 while (optind < argc) {
1191 ini_idx++;
1192 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1193 ini_file[ini_idx - 1] = strdup(argv[optind]);
1194 optind++;
eb8bbf48 1195 }
972cfd25
JA
1196
1197 return ini_idx;
ebac4655
JA
1198}
1199
ebac4655
JA
1200int parse_options(int argc, char *argv[])
1201{
972cfd25
JA
1202 int job_files, i;
1203
b4692828
JA
1204 f_out = stdout;
1205 f_err = stderr;
1206
5ec10eaa 1207 fio_options_dup_and_init(l_opts);
b4692828 1208
ebac4655
JA
1209 if (setup_thread_area())
1210 return 1;
1211 if (fill_def_thread())
1212 return 1;
1213
972cfd25 1214 job_files = parse_cmd_line(argc, argv);
ebac4655 1215
972cfd25
JA
1216 for (i = 0; i < job_files; i++) {
1217 if (fill_def_thread())
1218 return 1;
0c7e37a0 1219 if (parse_jobs_ini(ini_file[i], i))
972cfd25 1220 return 1;
88c6ed80 1221 free(ini_file[i]);
972cfd25 1222 }
ebac4655 1223
88c6ed80 1224 free(ini_file);
d23bb327 1225 options_mem_free(&def_thread);
b4692828
JA
1226
1227 if (!thread_number) {
cca73aa7
JA
1228 if (dump_cmdline)
1229 return 0;
1230
fd4442b8 1231 log_err("No jobs defined(s)\n\n");
45378b35 1232 usage(argv[0]);
b4692828
JA
1233 return 1;
1234 }
1235
be4ecfdf
JA
1236 if (def_thread.o.gtod_offload) {
1237 fio_gtod_init();
1238 fio_gtod_offload = 1;
1239 fio_gtod_cpu = def_thread.o.gtod_cpu;
1240 }
1241
ebac4655
JA
1242 return 0;
1243}