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