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