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