Add --readonly option
[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
20static char fio_version_string[] = "fio 1.17.1";
21
22#define FIO_RANDSEED (0xb1899bedUL)
23
24static char **ini_file;
25static int max_jobs = MAX_JOBS;
26static int dump_cmdline;
27static int read_only;
28
29struct thread_data def_thread;
30struct thread_data *threads = NULL;
31
32int exitall_on_terminate = 0;
33int terse_output = 0;
34unsigned long long mlock_size = 0;
35FILE *f_out = NULL;
36FILE *f_err = NULL;
37
38int write_bw_log = 0;
39
40static int def_timeout = 0;
41static int write_lat_log = 0;
42
43static int prev_group_jobs;
44
45/*
46 * Command line options. These will contain the above, plus a few
47 * extra that only pertain to fio itself and not jobs.
48 */
49static struct option long_options[FIO_NR_OPTIONS] = {
50 {
51 .name = "output",
52 .has_arg = required_argument,
53 .val = 'o',
54 },
55 {
56 .name = "timeout",
57 .has_arg = required_argument,
58 .val = 't',
59 },
60 {
61 .name = "latency-log",
62 .has_arg = required_argument,
63 .val = 'l',
64 },
65 {
66 .name = "bandwidth-log",
67 .has_arg = required_argument,
68 .val = 'b',
69 },
70 {
71 .name = "minimal",
72 .has_arg = optional_argument,
73 .val = 'm',
74 },
75 {
76 .name = "version",
77 .has_arg = no_argument,
78 .val = 'v',
79 },
80 {
81 .name = "help",
82 .has_arg = no_argument,
83 .val = 'h',
84 },
85 {
86 .name = "cmdhelp",
87 .has_arg = optional_argument,
88 .val = 'c',
89 },
90 {
91 .name = "showcmd",
92 .has_arg = no_argument,
93 .val = 's',
94 },
95 {
96 .name = "readonly",
97 .has_arg = no_argument,
98 .val = 'r',
99 },
100 {
101 .name = NULL,
102 },
103};
104
105FILE *get_f_out()
106{
107 return f_out;
108}
109
110FILE *get_f_err()
111{
112 return f_err;
113}
114
115/*
116 * Return a free job structure.
117 */
118static struct thread_data *get_new_job(int global, struct thread_data *parent)
119{
120 struct thread_data *td;
121
122 if (global)
123 return &def_thread;
124 if (thread_number >= max_jobs)
125 return NULL;
126
127 td = &threads[thread_number++];
128 *td = *parent;
129
130 dup_files(td, parent);
131 options_mem_dupe(td);
132
133 td->thread_number = thread_number;
134 return td;
135}
136
137static void put_job(struct thread_data *td)
138{
139 if (td == &def_thread)
140 return;
141
142 if (td->error)
143 log_info("fio: %s\n", td->verror);
144
145 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
146 thread_number--;
147}
148
149static int setup_rate(struct thread_data *td)
150{
151 unsigned long nr_reads_per_msec;
152 unsigned long long rate;
153 unsigned int bs;
154
155 if (!td->o.rate && !td->o.rate_iops)
156 return 0;
157
158 if (td_rw(td))
159 bs = td->o.rw_min_bs;
160 else if (td_read(td))
161 bs = td->o.min_bs[DDIR_READ];
162 else
163 bs = td->o.min_bs[DDIR_WRITE];
164
165 if (td->o.rate) {
166 rate = td->o.rate;
167 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
168 } else
169 nr_reads_per_msec = td->o.rate_iops * 1000UL;
170
171 if (!nr_reads_per_msec) {
172 log_err("rate lower than supported\n");
173 return -1;
174 }
175
176 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
177 td->rate_pending_usleep = 0;
178 return 0;
179}
180
181/*
182 * Lazy way of fixing up options that depend on each other. We could also
183 * define option callback handlers, but this is easier.
184 */
185static int fixup_options(struct thread_data *td)
186{
187 struct thread_options *o = &td->o;
188
189 if (read_only && td_write(td)) {
190 log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name);
191 return 1;
192 }
193
194 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
195 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
196
197 if (o->write_iolog_file && o->read_iolog_file) {
198 log_err("fio: read iolog overrides write_iolog\n");
199 free(o->write_iolog_file);
200 o->write_iolog_file = NULL;
201 }
202
203 if (td->io_ops->flags & FIO_SYNCIO)
204 o->iodepth = 1;
205 else {
206 if (!o->iodepth)
207 o->iodepth = o->open_files;
208 }
209
210 /*
211 * only really works for sequential io for now, and with 1 file
212 */
213 if (o->zone_size && td_random(td) && o->open_files == 1)
214 o->zone_size = 0;
215
216 /*
217 * Reads can do overwrites, we always need to pre-create the file
218 */
219 if (td_read(td) || td_rw(td))
220 o->overwrite = 1;
221
222 if (!o->min_bs[DDIR_READ])
223 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
224 if (!o->max_bs[DDIR_READ])
225 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
226 if (!o->min_bs[DDIR_WRITE])
227 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
228 if (!o->max_bs[DDIR_WRITE])
229 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
230
231 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
232
233 if (!o->file_size_high)
234 o->file_size_high = o->file_size_low;
235
236 if (o->norandommap && o->verify != VERIFY_NONE) {
237 log_err("fio: norandommap given, verify disabled\n");
238 o->verify = VERIFY_NONE;
239 }
240 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
241 log_err("fio: bs_unaligned may not work with raw io\n");
242
243 /*
244 * thinktime_spin must be less than thinktime
245 */
246 if (o->thinktime_spin > o->thinktime)
247 o->thinktime_spin = o->thinktime;
248
249 /*
250 * The low water mark cannot be bigger than the iodepth
251 */
252 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
253 /*
254 * syslet work around - if the workload is sequential,
255 * we want to let the queue drain all the way down to
256 * avoid seeking between async threads
257 */
258 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
259 o->iodepth_low = 1;
260 else
261 o->iodepth_low = o->iodepth;
262 }
263
264 /*
265 * If batch number isn't set, default to the same as iodepth
266 */
267 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
268 o->iodepth_batch = o->iodepth;
269
270 if (o->nr_files > td->files_index)
271 o->nr_files = td->files_index;
272
273 if (o->open_files > o->nr_files || !o->open_files)
274 o->open_files = o->nr_files;
275
276 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
277 log_err("fio: rate and rate_iops are mutually exclusive\n");
278 return 1;
279 }
280 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
281 log_err("fio: minimum rate exceeds rate\n");
282 return 1;
283 }
284
285 if (!o->timeout && o->time_based) {
286 log_err("fio: time_based requires a runtime/timeout setting\n");
287 o->time_based = 0;
288 }
289
290 return 0;
291}
292
293/*
294 * This function leaks the buffer
295 */
296static char *to_kmg(unsigned int val)
297{
298 char *buf = malloc(32);
299 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
300 char *p = post;
301
302 do {
303 if (val & 1023)
304 break;
305
306 val >>= 10;
307 p++;
308 } while (*p);
309
310 snprintf(buf, 31, "%u%c", val, *p);
311 return buf;
312}
313
314/* External engines are specified by "external:name.o") */
315static const char *get_engine_name(const char *str)
316{
317 char *p = strstr(str, ":");
318
319 if (!p)
320 return str;
321
322 p++;
323 strip_blank_front(&p);
324 strip_blank_end(p);
325 return p;
326}
327
328static int exists_and_not_file(const char *filename)
329{
330 struct stat sb;
331
332 if (lstat(filename, &sb) == -1)
333 return 0;
334
335 if (S_ISREG(sb.st_mode))
336 return 0;
337
338 return 1;
339}
340
341/*
342 * Initialize the various random states we need (random io, block size ranges,
343 * read/write mix, etc).
344 */
345static int init_random_state(struct thread_data *td)
346{
347 unsigned long seeds[6];
348 int fd;
349
350 fd = open("/dev/urandom", O_RDONLY);
351 if (fd == -1) {
352 td_verror(td, errno, "open");
353 return 1;
354 }
355
356 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
357 td_verror(td, EIO, "read");
358 close(fd);
359 return 1;
360 }
361
362 close(fd);
363
364 os_random_seed(seeds[0], &td->bsrange_state);
365 os_random_seed(seeds[1], &td->verify_state);
366 os_random_seed(seeds[2], &td->rwmix_state);
367
368 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
369 os_random_seed(seeds[3], &td->next_file_state);
370
371 os_random_seed(seeds[5], &td->file_size_state);
372
373 if (!td_random(td))
374 return 0;
375
376 if (td->o.rand_repeatable)
377 seeds[4] = FIO_RANDSEED * td->thread_number;
378
379 os_random_seed(seeds[4], &td->random_state);
380 return 0;
381}
382
383/*
384 * Adds a job to the list of things todo. Sanitizes the various options
385 * to make sure we don't have conflicts, and initializes various
386 * members of td.
387 */
388static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
389{
390 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
391 "randread", "randwrite", "randrw" };
392 unsigned int i;
393 const char *engine;
394 char fname[PATH_MAX];
395 int numjobs, file_alloced;
396
397 /*
398 * the def_thread is just for options, it's not a real job
399 */
400 if (td == &def_thread)
401 return 0;
402
403 /*
404 * if we are just dumping the output command line, don't add the job
405 */
406 if (dump_cmdline) {
407 put_job(td);
408 return 0;
409 }
410
411 engine = get_engine_name(td->o.ioengine);
412 td->io_ops = load_ioengine(td, engine);
413 if (!td->io_ops) {
414 log_err("fio: failed to load engine %s\n", engine);
415 goto err;
416 }
417
418 if (td->o.use_thread)
419 nr_thread++;
420 else
421 nr_process++;
422
423 if (td->o.odirect)
424 td->io_ops->flags |= FIO_RAWIO;
425
426 file_alloced = 0;
427 if (!td->o.filename && !td->files_index) {
428 file_alloced = 1;
429
430 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
431 add_file(td, jobname);
432 else {
433 for (i = 0; i < td->o.nr_files; i++) {
434 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
435 add_file(td, fname);
436 }
437 }
438 }
439
440 if (fixup_options(td))
441 goto err;
442
443 if (td->io_ops->flags & FIO_DISKLESSIO) {
444 struct fio_file *f;
445
446 for_each_file(td, f, i)
447 f->real_file_size = -1ULL;
448 }
449
450 td->mutex = fio_sem_init(0);
451
452 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
453 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
454 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
455 td->ddir_nr = td->o.ddir_nr;
456
457 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
458 && prev_group_jobs) {
459 prev_group_jobs = 0;
460 groupid++;
461 }
462
463 td->groupid = groupid;
464 prev_group_jobs++;
465
466 if (init_random_state(td))
467 goto err;
468
469 if (setup_rate(td))
470 goto err;
471
472 if (td->o.write_lat_log) {
473 setup_log(&td->ts.slat_log);
474 setup_log(&td->ts.clat_log);
475 }
476 if (td->o.write_bw_log)
477 setup_log(&td->ts.bw_log);
478
479 if (!td->o.name)
480 td->o.name = strdup(jobname);
481
482 if (!terse_output) {
483 if (!job_add_num) {
484 if (!strcmp(td->io_ops->name, "cpuio"))
485 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle);
486 else {
487 char *c1, *c2, *c3, *c4;
488
489 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
490 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
491 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
492 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
493
494 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->o.name, td->groupid, ddir_str[td->o.td_ddir], c1, c2, c3, c4, td->io_ops->name, td->o.iodepth);
495
496 free(c1);
497 free(c2);
498 free(c3);
499 free(c4);
500 }
501 } else if (job_add_num == 1)
502 log_info("...\n");
503 }
504
505 /*
506 * recurse add identical jobs, clear numjobs and stonewall options
507 * as they don't apply to sub-jobs
508 */
509 numjobs = td->o.numjobs;
510 while (--numjobs) {
511 struct thread_data *td_new = get_new_job(0, td);
512
513 if (!td_new)
514 goto err;
515
516 td_new->o.numjobs = 1;
517 td_new->o.stonewall = 0;
518 td_new->o.new_group = 0;
519
520 if (file_alloced) {
521 td_new->o.filename = NULL;
522 td_new->files_index = 0;
523 td_new->files = NULL;
524 }
525
526 job_add_num = numjobs - 1;
527
528 if (add_job(td_new, jobname, job_add_num))
529 goto err;
530 }
531
532 return 0;
533err:
534 put_job(td);
535 return -1;
536}
537
538static int is_empty_or_comment(char *line)
539{
540 unsigned int i;
541
542 for (i = 0; i < strlen(line); i++) {
543 if (line[i] == ';')
544 return 1;
545 if (line[i] == '#')
546 return 1;
547 if (!isspace(line[i]) && !iscntrl(line[i]))
548 return 0;
549 }
550
551 return 1;
552}
553
554/*
555 * This is our [ini] type file parser.
556 */
557static int parse_jobs_ini(char *file, int stonewall_flag)
558{
559 unsigned int global;
560 struct thread_data *td;
561 char *string, *name;
562 fpos_t off;
563 FILE *f;
564 char *p;
565 int ret = 0, stonewall;
566 int first_sect = 1;
567
568 f = fopen(file, "r");
569 if (!f) {
570 perror("fopen job file");
571 return 1;
572 }
573
574 string = malloc(4096);
575
576 /*
577 * it's really 256 + small bit, 280 should suffice
578 */
579 name = malloc(280);
580 memset(name, 0, 280);
581
582 stonewall = stonewall_flag;
583 do {
584 p = fgets(string, 4095, f);
585 if (!p)
586 break;
587
588 strip_blank_front(&p);
589 strip_blank_end(p);
590
591 if (is_empty_or_comment(p))
592 continue;
593 if (sscanf(p, "[%255s]", name) != 1) {
594 log_err("fio: option <%s> outside of [] job section\n", p);
595 break;
596 }
597
598 global = !strncmp(name, "global", 6);
599
600 name[strlen(name) - 1] = '\0';
601
602 if (dump_cmdline) {
603 if (first_sect)
604 log_info("fio ");
605 if (!global)
606 log_info("--name=%s ", name);
607 first_sect = 0;
608 }
609
610 td = get_new_job(global, &def_thread);
611 if (!td) {
612 ret = 1;
613 break;
614 }
615
616 /*
617 * Seperate multiple job files by a stonewall
618 */
619 if (!global && stonewall) {
620 td->o.stonewall = stonewall;
621 stonewall = 0;
622 }
623
624 fgetpos(f, &off);
625 while ((p = fgets(string, 4096, f)) != NULL) {
626 if (is_empty_or_comment(p))
627 continue;
628
629 strip_blank_front(&p);
630
631 if (p[0] == '[')
632 break;
633
634 strip_blank_end(p);
635
636 fgetpos(f, &off);
637
638 /*
639 * Don't break here, continue parsing options so we
640 * dump all the bad ones. Makes trial/error fixups
641 * easier on the user.
642 */
643 ret |= fio_option_parse(td, p);
644 if (!ret && dump_cmdline)
645 log_info("--%s ", p);
646 }
647
648 if (!ret) {
649 fsetpos(f, &off);
650 ret = add_job(td, name, 0);
651 } else {
652 log_err("fio: job %s dropped\n", name);
653 put_job(td);
654 }
655 } while (!ret);
656
657 if (dump_cmdline)
658 log_info("\n");
659
660 free(string);
661 free(name);
662 fclose(f);
663 return ret;
664}
665
666static int fill_def_thread(void)
667{
668 memset(&def_thread, 0, sizeof(def_thread));
669
670 fio_getaffinity(getpid(), &def_thread.o.cpumask);
671
672 /*
673 * fill default options
674 */
675 fio_fill_default_options(&def_thread);
676
677 def_thread.o.timeout = def_timeout;
678 def_thread.o.write_bw_log = write_bw_log;
679 def_thread.o.write_lat_log = write_lat_log;
680
681 return 0;
682}
683
684static void free_shm(void)
685{
686 struct shmid_ds sbuf;
687
688 if (threads) {
689 shmdt((void *) threads);
690 threads = NULL;
691 shmctl(shm_id, IPC_RMID, &sbuf);
692 }
693}
694
695/*
696 * The thread area is shared between the main process and the job
697 * threads/processes. So setup a shared memory segment that will hold
698 * all the job info.
699 */
700static int setup_thread_area(void)
701{
702 /*
703 * 1024 is too much on some machines, scale max_jobs if
704 * we get a failure that looks like too large a shm segment
705 */
706 do {
707 size_t size = max_jobs * sizeof(struct thread_data);
708
709 shm_id = shmget(0, size, IPC_CREAT | 0600);
710 if (shm_id != -1)
711 break;
712 if (errno != EINVAL) {
713 perror("shmget");
714 break;
715 }
716
717 max_jobs >>= 1;
718 } while (max_jobs);
719
720 if (shm_id == -1)
721 return 1;
722
723 threads = shmat(shm_id, NULL, 0);
724 if (threads == (void *) -1) {
725 perror("shmat");
726 return 1;
727 }
728
729 atexit(free_shm);
730 return 0;
731}
732
733static void usage(void)
734{
735 printf("%s\n", fio_version_string);
736 printf("\t--output\tWrite output to file\n");
737 printf("\t--timeout\tRuntime in seconds\n");
738 printf("\t--latency-log\tGenerate per-job latency logs\n");
739 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
740 printf("\t--minimal\tMinimal (terse) output\n");
741 printf("\t--version\tPrint version info and exit\n");
742 printf("\t--help\t\tPrint this page\n");
743 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
744 printf("\t--showcmd\tTurn a job file into command line options\n");
745}
746
747static int parse_cmd_line(int argc, char *argv[])
748{
749 struct thread_data *td = NULL;
750 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
751
752 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
753 switch (c) {
754 case 't':
755 def_timeout = atoi(optarg);
756 break;
757 case 'l':
758 write_lat_log = 1;
759 break;
760 case 'w':
761 write_bw_log = 1;
762 break;
763 case 'o':
764 f_out = fopen(optarg, "w+");
765 if (!f_out) {
766 perror("fopen output");
767 exit(1);
768 }
769 f_err = f_out;
770 break;
771 case 'm':
772 terse_output = 1;
773 break;
774 case 'h':
775 usage();
776 exit(0);
777 case 'c':
778 exit(fio_show_option_help(optarg));
779 case 's':
780 dump_cmdline = 1;
781 break;
782 case 'r':
783 read_only = 1;
784 break;
785 case 'v':
786 printf("%s\n", fio_version_string);
787 exit(0);
788 case FIO_GETOPT_JOB: {
789 const char *opt = long_options[lidx].name;
790 char *val = optarg;
791
792 if (!strncmp(opt, "name", 4) && td) {
793 ret = add_job(td, td->o.name ?: "fio", 0);
794 if (ret) {
795 put_job(td);
796 return 0;
797 }
798 td = NULL;
799 }
800 if (!td) {
801 int global = 0;
802
803 if (strncmp(opt, "name", 4) ||
804 !strncmp(val, "global", 6))
805 global = 1;
806
807 td = get_new_job(global, &def_thread);
808 if (!td)
809 return 0;
810 }
811
812 ret = fio_cmd_option_parse(td, opt, val);
813 if (ret)
814 dont_add_job = 1;
815 break;
816 }
817 default:
818 break;
819 }
820 }
821
822 if (td) {
823 if (dont_add_job)
824 put_job(td);
825 else {
826 ret = add_job(td, td->o.name ?: "fio", 0);
827 if (ret)
828 put_job(td);
829 }
830 }
831
832 while (optind < argc) {
833 ini_idx++;
834 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
835 ini_file[ini_idx - 1] = strdup(argv[optind]);
836 optind++;
837 }
838
839 return ini_idx;
840}
841
842
843int parse_options(int argc, char *argv[])
844{
845 int job_files, i;
846
847 f_out = stdout;
848 f_err = stderr;
849
850 fio_options_dup_and_init(long_options);
851
852 if (setup_thread_area())
853 return 1;
854 if (fill_def_thread())
855 return 1;
856
857 job_files = parse_cmd_line(argc, argv);
858
859 for (i = 0; i < job_files; i++) {
860 if (fill_def_thread())
861 return 1;
862 if (parse_jobs_ini(ini_file[i], i))
863 return 1;
864 free(ini_file[i]);
865 }
866
867 free(ini_file);
868 options_mem_free(&def_thread);
869
870 if (!thread_number) {
871 if (dump_cmdline)
872 return 0;
873
874 log_err("No jobs defined(s)\n");
875 return 1;
876 }
877
878 return 0;
879}