Add support for specifying UID/GID
[fio.git] / init.c
... / ...
CommitLineData
1/*
2 * This file contains job initialization and setup functions.
3 */
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 <getopt.h>
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"
18#include "parse.h"
19#include "smalloc.h"
20#include "filehash.h"
21#include "verify.h"
22
23static char fio_version_string[] = "fio 1.36-rc1";
24
25#define FIO_RANDSEED (0xb1899bedUL)
26
27static char **ini_file;
28static int max_jobs = MAX_JOBS;
29static int dump_cmdline;
30
31static struct thread_data def_thread;
32struct thread_data *threads = NULL;
33
34int exitall_on_terminate = 0;
35int terse_output = 0;
36int eta_print;
37unsigned long long mlock_size = 0;
38FILE *f_out = NULL;
39FILE *f_err = NULL;
40char *job_section = NULL;
41
42int write_bw_log = 0;
43int read_only = 0;
44
45static int def_timeout;
46static int write_lat_log;
47
48static int prev_group_jobs;
49
50unsigned long fio_debug = 0;
51unsigned int fio_debug_jobno = -1;
52unsigned int *fio_debug_jobp = NULL;
53
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 */
58static struct option l_opts[FIO_NR_OPTIONS] = {
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 },
89 {
90 .name = "help",
91 .has_arg = no_argument,
92 .val = 'h',
93 },
94 {
95 .name = "cmdhelp",
96 .has_arg = optional_argument,
97 .val = 'c',
98 },
99 {
100 .name = "showcmd",
101 .has_arg = no_argument,
102 .val = 's',
103 },
104 {
105 .name = "readonly",
106 .has_arg = no_argument,
107 .val = 'r',
108 },
109 {
110 .name = "eta",
111 .has_arg = required_argument,
112 .val = 'e',
113 },
114 {
115 .name = "debug",
116 .has_arg = required_argument,
117 .val = 'd',
118 },
119 {
120 .name = "section",
121 .has_arg = required_argument,
122 .val = 'x',
123 },
124 {
125 .name = "alloc-size",
126 .has_arg = required_argument,
127 .val = 'a',
128 },
129 {
130 .name = "profile",
131 .has_arg = required_argument,
132 .val = 'p',
133 },
134 {
135 .name = NULL,
136 },
137};
138
139static const char *tiobench_opts[] = {
140 "buffered=0", "size=4*1024*$mb_memory", "bs=4k", "timeout=600",
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
159FILE *get_f_out()
160{
161 return f_out;
162}
163
164FILE *get_f_err()
165{
166 return f_err;
167}
168
169/*
170 * Return a free job structure.
171 */
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++];
182 *td = *parent;
183
184 td->o.uid = td->o.gid = -1U;
185
186 dup_files(td, parent);
187 options_mem_dupe(td);
188
189 td->thread_number = thread_number;
190 return td;
191}
192
193static void put_job(struct thread_data *td)
194{
195 if (td == &def_thread)
196 return;
197
198 if (td->error)
199 log_info("fio: %s\n", td->verror);
200
201 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
202 thread_number--;
203}
204
205static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
206{
207 unsigned int bs = td->o.min_bs[ddir];
208 unsigned long long rate;
209 unsigned long ios_per_msec;
210
211 if (td->o.rate[ddir]) {
212 rate = td->o.rate[ddir];
213 ios_per_msec = (rate * 1000LL) / bs;
214 } else
215 ios_per_msec = td->o.rate_iops[ddir] * 1000UL;
216
217 if (!ios_per_msec) {
218 log_err("rate lower than supported\n");
219 return -1;
220 }
221
222 td->rate_usec_cycle[ddir] = 1000000000ULL / ios_per_msec;
223 td->rate_pending_usleep[ddir] = 0;
224 return 0;
225}
226
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
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
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 */
250static int fixup_options(struct thread_data *td)
251{
252 struct thread_options *o = &td->o;
253
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
263 if (o->write_iolog_file && o->read_iolog_file) {
264 log_err("fio: read iolog overrides write_iolog\n");
265 free(o->write_iolog_file);
266 o->write_iolog_file = NULL;
267 }
268
269 /*
270 * only really works for sequential io for now, and with 1 file
271 */
272 if (o->zone_size && td_random(td) && o->open_files == 1)
273 o->zone_size = 0;
274
275 /*
276 * Reads can do overwrites, we always need to pre-create the file
277 */
278 if (td_read(td) || td_rw(td))
279 o->overwrite = 1;
280
281 if (!o->min_bs[DDIR_READ])
282 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
283 if (!o->max_bs[DDIR_READ])
284 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
285 if (!o->min_bs[DDIR_WRITE])
286 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
287 if (!o->max_bs[DDIR_WRITE])
288 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
289
290 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
291
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
307 if (!o->file_size_high)
308 o->file_size_high = o->file_size_low;
309
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");
314 o->verify = VERIFY_NONE;
315 }
316 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
317 log_err("fio: bs_unaligned may not work with raw io\n");
318
319 /*
320 * thinktime_spin must be less than thinktime
321 */
322 if (o->thinktime_spin > o->thinktime)
323 o->thinktime_spin = o->thinktime;
324
325 /*
326 * The low water mark cannot be bigger than the iodepth
327 */
328 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
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))
335 o->iodepth_low = 1;
336 else
337 o->iodepth_low = o->iodepth;
338 }
339
340 /*
341 * If batch number isn't set, default to the same as iodepth
342 */
343 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
344 o->iodepth_batch = o->iodepth;
345
346 if (o->nr_files > td->files_index)
347 o->nr_files = td->files_index;
348
349 if (o->open_files > o->nr_files || !o->open_files)
350 o->open_files = o->nr_files;
351
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]))) {
355 log_err("fio: rate and rate_iops are mutually exclusive\n");
356 return 1;
357 }
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])) {
361 log_err("fio: minimum rate exceeds rate\n");
362 return 1;
363 }
364
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
370 if (o->fill_device && !o->size)
371 o->size = -1ULL;
372
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");
376
377 if (td->o.verify != VERIFY_NONE)
378 td->o.refill_buffers = 1;
379
380 if (td->o.pre_read) {
381 td->o.invalidate_cache = 0;
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 }
386
387 return 0;
388}
389
390/*
391 * This function leaks the buffer
392 */
393static char *to_kmg(unsigned int val)
394{
395 char *buf = malloc(32);
396 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
397 char *p = post;
398
399 do {
400 if (val & 1023)
401 break;
402
403 val >>= 10;
404 p++;
405 } while (*p);
406
407 snprintf(buf, 31, "%u%c", val, *p);
408 return buf;
409}
410
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
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
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
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{
464 int fd;
465
466 fd = open("/dev/urandom", O_RDONLY);
467 if (fd == -1) {
468 td_verror(td, errno, "open");
469 return 1;
470 }
471
472 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
473 (int) sizeof(td->rand_seeds)) {
474 td_verror(td, EIO, "read");
475 close(fd);
476 return 1;
477 }
478
479 close(fd);
480 td_fill_rand_seeds(td);
481 return 0;
482}
483
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 */
489static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
490{
491 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
492 "randread", "randwrite", "randrw" };
493 unsigned int i;
494 const char *engine;
495 char fname[PATH_MAX];
496 int numjobs, file_alloced;
497
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
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
512 engine = get_engine_name(td->o.ioengine);
513 td->io_ops = load_ioengine(td, engine);
514 if (!td->io_ops) {
515 log_err("fio: failed to load engine %s\n", engine);
516 goto err;
517 }
518
519 if (td->o.use_thread)
520 nr_thread++;
521 else
522 nr_process++;
523
524 if (td->o.odirect)
525 td->io_ops->flags |= FIO_RAWIO;
526
527 file_alloced = 0;
528 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
529 file_alloced = 1;
530
531 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
532 add_file(td, jobname);
533 else {
534 for (i = 0; i < td->o.nr_files; i++) {
535 sprintf(fname, "%s.%d.%d", jobname,
536 td->thread_number, i);
537 add_file(td, fname);
538 }
539 }
540 }
541
542 if (fixup_options(td))
543 goto err;
544
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
552 td->mutex = fio_mutex_init(0);
553
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;
557 td->ddir_nr = td->o.ddir_nr;
558
559 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
560 prev_group_jobs = 0;
561 groupid++;
562 }
563
564 td->groupid = groupid;
565 prev_group_jobs++;
566
567 if (init_random_state(td))
568 goto err;
569
570 if (setup_rate(td))
571 goto err;
572
573 if (td->o.write_lat_log) {
574 setup_log(&td->ts.slat_log);
575 setup_log(&td->ts.clat_log);
576 }
577 if (td->o.write_bw_log)
578 setup_log(&td->ts.bw_log);
579
580 if (!td->o.name)
581 td->o.name = strdup(jobname);
582
583 if (!terse_output) {
584 if (!job_add_num) {
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 {
591 char *c1, *c2, *c3, *c4;
592
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]);
597
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);
605
606 free(c1);
607 free(c2);
608 free(c3);
609 free(c4);
610 }
611 } else if (job_add_num == 1)
612 log_info("...\n");
613 }
614
615 /*
616 * recurse add identical jobs, clear numjobs and stonewall options
617 * as they don't apply to sub-jobs
618 */
619 numjobs = td->o.numjobs;
620 while (--numjobs) {
621 struct thread_data *td_new = get_new_job(0, td);
622
623 if (!td_new)
624 goto err;
625
626 td_new->o.numjobs = 1;
627 td_new->o.stonewall = 0;
628 td_new->o.new_group = 0;
629
630 if (file_alloced) {
631 td_new->o.filename = NULL;
632 td_new->files_index = 0;
633 td_new->files_size = 0;
634 td_new->files = NULL;
635 }
636
637 job_add_num = numjobs - 1;
638
639 if (add_job(td_new, jobname, job_add_num))
640 goto err;
641 }
642
643 return 0;
644err:
645 put_job(td);
646 return -1;
647}
648
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
656 return strcmp(job_section, name);
657}
658
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;
666 if (line[i] == '#')
667 return 1;
668 if (!isspace(line[i]) && !iscntrl(line[i]))
669 return 0;
670 }
671
672 return 1;
673}
674
675/*
676 * This is our [ini] type file parser.
677 */
678static int parse_jobs_ini(char *file, int stonewall_flag)
679{
680 unsigned int global;
681 struct thread_data *td;
682 char *string, *name;
683 FILE *f;
684 char *p;
685 int ret = 0, stonewall;
686 int first_sect = 1;
687 int skip_fgets = 0;
688 int inside_skip = 0;
689 char **opts;
690 int i, alloc_opts, num_opts;
691
692 if (!strcmp(file, "-"))
693 f = stdin;
694 else
695 f = fopen(file, "r");
696
697 if (!f) {
698 perror("fopen job file");
699 return 1;
700 }
701
702 string = malloc(4096);
703
704 /*
705 * it's really 256 + small bit, 280 should suffice
706 */
707 name = malloc(280);
708 memset(name, 0, 280);
709
710 alloc_opts = 8;
711 opts = malloc(sizeof(char *) * alloc_opts);
712 num_opts = 0;
713
714 stonewall = stonewall_flag;
715 do {
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 }
725
726 skip_fgets = 0;
727 strip_blank_front(&p);
728 strip_blank_end(p);
729
730 if (is_empty_or_comment(p))
731 continue;
732 if (sscanf(p, "[%255s]", name) != 1) {
733 if (inside_skip)
734 continue;
735 log_err("fio: option <%s> outside of [] job section\n",
736 p);
737 break;
738 }
739
740 if (skip_this_section(name)) {
741 inside_skip = 1;
742 continue;
743 } else
744 inside_skip = 0;
745
746 global = !strncmp(name, "global", 6);
747
748 name[strlen(name) - 1] = '\0';
749
750 if (dump_cmdline) {
751 if (first_sect)
752 log_info("fio ");
753 if (!global)
754 log_info("--name=%s ", name);
755 first_sect = 0;
756 }
757
758 td = get_new_job(global, &def_thread);
759 if (!td) {
760 ret = 1;
761 break;
762 }
763
764 /*
765 * Seperate multiple job files by a stonewall
766 */
767 if (!global && stonewall) {
768 td->o.stonewall = stonewall;
769 stonewall = 0;
770 }
771
772 num_opts = 0;
773 memset(opts, 0, alloc_opts * sizeof(char *));
774
775 while ((p = fgets(string, 4096, f)) != NULL) {
776 if (is_empty_or_comment(p))
777 continue;
778
779 strip_blank_front(&p);
780
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;
787 break;
788 }
789
790 strip_blank_end(p);
791
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++;
800 }
801
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
808 ret = add_job(td, name, 0);
809 } else {
810 log_err("fio: job %s dropped\n", name);
811 put_job(td);
812 }
813
814 for (i = 0; i < num_opts; i++)
815 free(opts[i]);
816 num_opts = 0;
817 } while (!ret);
818
819 if (dump_cmdline)
820 log_info("\n");
821
822 for (i = 0; i < num_opts; i++)
823 free(opts[i]);
824
825 free(string);
826 free(name);
827 free(opts);
828 if (f != stdin)
829 fclose(f);
830 return ret;
831}
832
833static int fill_def_thread(void)
834{
835 memset(&def_thread, 0, sizeof(def_thread));
836
837 fio_getaffinity(getpid(), &def_thread.o.cpumask);
838
839 /*
840 * fill default options
841 */
842 fio_fill_default_options(&def_thread);
843
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;
847
848 return 0;
849}
850
851static void free_shm(void)
852{
853 struct shmid_ds sbuf;
854
855 if (threads) {
856 void *tp = threads;
857
858 threads = NULL;
859 file_hash_exit();
860 fio_debug_jobp = NULL;
861 shmdt(tp);
862 shmctl(shm_id, IPC_RMID, &sbuf);
863 }
864
865 scleanup();
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
871 * all the job info. We use the end of the region for keeping track of
872 * open files across jobs, for file sharing.
873 */
874static int setup_thread_area(void)
875{
876 void *hash;
877
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
885 size += file_hash_size;
886 size += sizeof(unsigned int);
887
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
908 memset(threads, 0, max_jobs * sizeof(struct thread_data));
909 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
910 fio_debug_jobp = (void *) hash + file_hash_size;
911 *fio_debug_jobp = -1;
912 file_hash_init(hash);
913 atexit(free_shm);
914 return 0;
915}
916
917static void usage(const char *name)
918{
919 printf("%s\n", fio_version_string);
920 printf("%s [options] [job options] <job file(s)>\n", name);
921 printf("\t--debug=options\tEnable debug logging\n");
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");
928 printf("\t--help\t\tPrint this page\n");
929 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
930 " them\n");
931 printf("\t--showcmd\tTurn a job file into command line options\n");
932 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
933 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
934 printf("\t--readonly\tTurn on safety read-only checks, preventing"
935 " writes\n");
936 printf("\t--section=name\tOnly run specified section in job file\n");
937 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
938 " (def 1024)\n");
939 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>\n");
940}
941
942#ifdef FIO_INC_DEBUG
943struct debug_level debug_levels[] = {
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 },
950 { .name = "random", .shift = FD_RANDOM },
951 { .name = "parse", .shift = FD_PARSE },
952 { .name = "diskutil", .shift = FD_DISKUTIL },
953 { .name = "job", .shift = FD_JOB },
954 { .name = "mutex", .shift = FD_MUTEX },
955 { .name = NULL, },
956};
957
958static int set_debug(const char *string)
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 }
973 log_info("all\n");
974 return 1;
975 }
976
977 while ((opt = strsep(&p, ",")) != NULL) {
978 int found = 0;
979
980 if (!strncmp(opt, "all", 3)) {
981 log_info("fio: set all debug options\n");
982 fio_debug = ~0UL;
983 continue;
984 }
985
986 for (i = 0; debug_levels[i].name; i++) {
987 dl = &debug_levels[i];
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 {
1003 log_info("fio: set debug option %s\n", opt);
1004 fio_debug |= (1UL << dl->shift);
1005 }
1006 break;
1007 }
1008
1009 if (!found)
1010 log_err("fio: debug mask %s not found\n", opt);
1011 }
1012 return 0;
1013}
1014#else
1015static int set_debug(const char *string)
1016{
1017 log_err("fio: debug tracing not included in build\n");
1018 return 1;
1019}
1020#endif
1021
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
1073static int parse_cmd_line(int argc, char *argv[])
1074{
1075 struct thread_data *td = NULL;
1076 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1077
1078 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
1079 switch (c) {
1080 case 'a':
1081 smalloc_pool_size = atoi(optarg);
1082 break;
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':
1104 usage(argv[0]);
1105 exit(0);
1106 case 'c':
1107 exit(fio_show_option_help(optarg));
1108 case 's':
1109 dump_cmdline = 1;
1110 break;
1111 case 'r':
1112 read_only = 1;
1113 break;
1114 case 'v':
1115 printf("%s\n", fio_version_string);
1116 exit(0);
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;
1123 case 'd':
1124 if (set_debug(optarg))
1125 do_exit++;
1126 break;
1127 case 'x':
1128 if (!strcmp(optarg, "global")) {
1129 log_err("fio: can't use global as only "
1130 "section\n");
1131 do_exit++;
1132 exit_val = 1;
1133 break;
1134 }
1135 if (job_section)
1136 free(job_section);
1137 job_section = strdup(optarg);
1138 break;
1139 case 'p':
1140 if (load_profile(optarg))
1141 do_exit++;
1142 break;
1143 case FIO_GETOPT_JOB: {
1144 const char *opt = l_opts[lidx].name;
1145 char *val = optarg;
1146
1147 if (!strncmp(opt, "name", 4) && td) {
1148 ret = add_job(td, td->o.name ?: "fio", 0);
1149 if (ret) {
1150 put_job(td);
1151 return 0;
1152 }
1153 td = NULL;
1154 }
1155 if (!td) {
1156 int is_section = !strncmp(opt, "name", 4);
1157 int global = 0;
1158
1159 if (!is_section || !strncmp(val, "global", 6))
1160 global = 1;
1161
1162 if (is_section && skip_this_section(val))
1163 continue;
1164
1165 td = get_new_job(global, &def_thread);
1166 if (!td)
1167 return 0;
1168 }
1169
1170 ret = fio_cmd_option_parse(td, opt, val);
1171 break;
1172 }
1173 default:
1174 do_exit++;
1175 exit_val = 1;
1176 break;
1177 }
1178 }
1179
1180 if (do_exit)
1181 exit(exit_val);
1182
1183 if (td) {
1184 if (!ret)
1185 ret = add_job(td, td->o.name ?: "fio", 0);
1186 if (ret)
1187 put_job(td);
1188 }
1189
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++;
1195 }
1196
1197 return ini_idx;
1198}
1199
1200int parse_options(int argc, char *argv[])
1201{
1202 int job_files, i;
1203
1204 f_out = stdout;
1205 f_err = stderr;
1206
1207 fio_options_dup_and_init(l_opts);
1208
1209 if (setup_thread_area())
1210 return 1;
1211 if (fill_def_thread())
1212 return 1;
1213
1214 job_files = parse_cmd_line(argc, argv);
1215
1216 for (i = 0; i < job_files; i++) {
1217 if (fill_def_thread())
1218 return 1;
1219 if (parse_jobs_ini(ini_file[i], i))
1220 return 1;
1221 free(ini_file[i]);
1222 }
1223
1224 free(ini_file);
1225 options_mem_free(&def_thread);
1226
1227 if (!thread_number) {
1228 if (dump_cmdline)
1229 return 0;
1230
1231 log_err("No jobs defined(s)\n\n");
1232 usage(argv[0]);
1233 return 1;
1234 }
1235
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
1242 return 0;
1243}