Merge branch 'master' of ssh://router/data/git/fio
[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 <assert.h>
13 #include <sys/ipc.h>
14 #include <sys/shm.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17
18 #include "fio.h"
19 #include "parse.h"
20
21 /*
22  * The default options
23  */
24 #define DEF_BS                  (4096)
25 #define DEF_TIMEOUT             (0)
26 #define DEF_RATE_CYCLE          (1000)
27 #define DEF_ODIRECT             (1)
28 #define DEF_IO_ENGINE           (FIO_SYNCIO)
29 #define DEF_IO_ENGINE_NAME      "sync"
30 #define DEF_SEQUENTIAL          (1)
31 #define DEF_RAND_REPEAT         (1)
32 #define DEF_OVERWRITE           (1)
33 #define DEF_INVALIDATE          (1)
34 #define DEF_SYNCIO              (0)
35 #define DEF_RANDSEED            (0xb1899bedUL)
36 #define DEF_BWAVGTIME           (500)
37 #define DEF_CREATE_SER          (1)
38 #define DEF_CREATE_FSYNC        (1)
39 #define DEF_LOOPS               (1)
40 #define DEF_VERIFY              (0)
41 #define DEF_STONEWALL           (0)
42 #define DEF_NUMJOBS             (1)
43 #define DEF_USE_THREAD          (0)
44 #define DEF_FILE_SIZE           (1024 * 1024 * 1024UL)
45 #define DEF_ZONE_SIZE           (0)
46 #define DEF_ZONE_SKIP           (0)
47 #define DEF_RWMIX_CYCLE         (500)
48 #define DEF_RWMIX_READ          (50)
49 #define DEF_NICE                (0)
50 #define DEF_NR_FILES            (1)
51 #define DEF_UNLINK              (0)
52 #define DEF_WRITE_BW_LOG        (0)
53 #define DEF_WRITE_LAT_LOG       (0)
54 #define DEF_NO_RAND_MAP         (0)
55
56 #define td_var_offset(var)      ((size_t) &((struct thread_data *)0)->var)
57
58 static int str_rw_cb(void *, const char *);
59 static int str_ioengine_cb(void *, const char *);
60 static int str_mem_cb(void *, const char *);
61 static int str_verify_cb(void *, const char *);
62 static int str_lockmem_cb(void *, unsigned long *);
63 #ifdef FIO_HAVE_IOPRIO
64 static int str_prio_cb(void *, unsigned int *);
65 static int str_prioclass_cb(void *, unsigned int *);
66 #endif
67 static int str_exitall_cb(void);
68 static int str_cpumask_cb(void *, unsigned int *);
69
70 /*
71  * Map of job/command line options
72  */
73 static struct fio_option options[] = {
74         {
75                 .name   = "name",
76                 .type   = FIO_OPT_STR_STORE,
77                 .off1   = td_var_offset(name),
78         },
79         {
80                 .name   = "directory",
81                 .type   = FIO_OPT_STR_STORE,
82                 .off1   = td_var_offset(directory),
83         },
84         {
85                 .name   = "filename",
86                 .type   = FIO_OPT_STR_STORE,
87                 .off1   = td_var_offset(filename),
88         },
89         {
90                 .name   = "rw",
91                 .type   = FIO_OPT_STR,
92                 .cb     = str_rw_cb,
93         },
94         {
95                 .name   = "ioengine",
96                 .type   = FIO_OPT_STR,
97                 .cb     = str_ioengine_cb,
98         },
99         {
100                 .name   = "mem",
101                 .type   = FIO_OPT_STR,
102                 .cb     = str_mem_cb,
103         },
104         {
105                 .name   = "verify",
106                 .type   = FIO_OPT_STR,
107                 .cb     = str_verify_cb,
108         },
109         {
110                 .name   = "write_iolog",
111                 .type   = FIO_OPT_STR_STORE,
112                 .off1   = td_var_offset(write_iolog_file),
113         },
114         {
115                 .name   = "read_iolog",
116                 .type   = FIO_OPT_STR_STORE,
117                 .off1   = td_var_offset(read_iolog_file),
118         },
119         {
120                 .name   = "exec_prerun",
121                 .type   = FIO_OPT_STR_STORE,
122                 .off1   = td_var_offset(exec_prerun),
123         },
124         {
125                 .name   = "exec_postrun",
126                 .type   = FIO_OPT_STR_STORE,
127                 .off1   = td_var_offset(exec_postrun),
128         },
129 #ifdef FIO_HAVE_IOSCHED_SWITCH
130         {
131                 .name   = "ioscheduler",
132                 .type   = FIO_OPT_STR_STORE,
133                 .off1   = td_var_offset(ioscheduler),
134         },
135 #endif
136         {
137                 .name   = "size",
138                 .type   = FIO_OPT_STR_VAL,
139                 .off1   = td_var_offset(total_file_size),
140         },
141         {
142                 .name   = "bs",
143                 .type   = FIO_OPT_STR_VAL_INT,
144                 .off1   = td_var_offset(bs[DDIR_READ]),
145         },
146         {
147                 .name   = "read_bs",
148                 .type   = FIO_OPT_STR_VAL_INT,
149                 .off1   = td_var_offset(bs[DDIR_READ]),
150         },
151         {
152                 .name   = "write_bs",
153                 .type   = FIO_OPT_STR_VAL_INT,
154                 .off1   = td_var_offset(bs[DDIR_WRITE]),
155         },
156         {
157                 .name   = "offset",
158                 .type   = FIO_OPT_STR_VAL,
159                 .off1   = td_var_offset(start_offset),
160         },
161         {
162                 .name   = "zonesize",
163                 .type   = FIO_OPT_STR_VAL,
164                 .off1   = td_var_offset(zone_size),
165         },
166         {
167                 .name   = "zoneskip",
168                 .type   = FIO_OPT_STR_VAL,
169                 .off1   = td_var_offset(zone_skip),
170         },
171         {
172                 .name   = "lockmem",
173                 .type   = FIO_OPT_STR_VAL,
174                 .cb     = str_lockmem_cb,
175         },
176         {
177                 .name   = "bsrange",
178                 .type   = FIO_OPT_RANGE,
179                 .off1   = td_var_offset(min_bs[DDIR_READ]),
180                 .off2   = td_var_offset(max_bs[DDIR_READ]),
181         },
182         {
183                 .name   = "read_bsrange",
184                 .type   = FIO_OPT_RANGE,
185                 .off1   = td_var_offset(min_bs[DDIR_READ]),
186                 .off2   = td_var_offset(max_bs[DDIR_READ]),
187         },
188         {
189                 .name   = "write_bsrange",
190                 .type   = FIO_OPT_RANGE,
191                 .off1   = td_var_offset(min_bs[DDIR_WRITE]),
192                 .off2   = td_var_offset(max_bs[DDIR_WRITE]),
193         },
194         {
195                 .name   = "nrfiles",
196                 .type   = FIO_OPT_INT,
197                 .off1   = td_var_offset(nr_files),
198         },
199         {
200                 .name   = "iodepth",
201                 .type   = FIO_OPT_INT,
202                 .off1   = td_var_offset(iodepth),
203         },
204         {
205                 .name   = "fsync",
206                 .type   = FIO_OPT_INT,
207                 .off1   = td_var_offset(fsync_blocks),
208         },
209         {
210                 .name   = "rwmixcycle",
211                 .type   = FIO_OPT_INT,
212                 .off1   = td_var_offset(rwmixcycle),
213         },
214         {
215                 .name   = "rwmixread",
216                 .type   = FIO_OPT_INT,
217                 .off1   = td_var_offset(rwmixread),
218                 .max_val= 100,
219         },
220         {
221                 .name   = "rwmixwrite",
222                 .type   = FIO_OPT_INT,
223                 .off1   = td_var_offset(rwmixwrite),
224                 .max_val= 100,
225         },
226         {
227                 .name   = "nice",
228                 .type   = FIO_OPT_INT,
229                 .off1   = td_var_offset(nice),
230         },
231 #ifdef FIO_HAVE_IOPRIO
232         {
233                 .name   = "prio",
234                 .type   = FIO_OPT_INT,
235                 .cb     = str_prio_cb,
236         },
237         {
238                 .name   = "prioclass",
239                 .type   = FIO_OPT_INT,
240                 .cb     = str_prioclass_cb,
241         },
242 #endif
243         {
244                 .name   = "thinktime",
245                 .type   = FIO_OPT_INT,
246                 .off1   = td_var_offset(thinktime)
247         },
248         {
249                 .name   = "rate",
250                 .type   = FIO_OPT_INT,
251                 .off1   = td_var_offset(rate)
252         },
253         {
254                 .name   = "ratemin",
255                 .type   = FIO_OPT_INT,
256                 .off1   = td_var_offset(ratemin)
257         },
258         {
259                 .name   = "ratecycle",
260                 .type   = FIO_OPT_INT,
261                 .off1   = td_var_offset(ratecycle)
262         },
263         {
264                 .name   = "startdelay",
265                 .type   = FIO_OPT_INT,
266                 .off1   = td_var_offset(start_delay)
267         },
268         {
269                 .name   = "timeout",
270                 .type   = FIO_OPT_STR_VAL_TIME,
271                 .off1   = td_var_offset(timeout)
272         },
273         {
274                 .name   = "invalidate",
275                 .type   = FIO_OPT_INT,
276                 .off1   = td_var_offset(invalidate_cache)
277         },
278         {
279                 .name   = "sync",
280                 .type   = FIO_OPT_INT,
281                 .off1   = td_var_offset(sync_io)
282         },
283         {
284                 .name   = "bwavgtime",
285                 .type   = FIO_OPT_INT,
286                 .off1   = td_var_offset(bw_avg_time)
287         },
288         {
289                 .name   = "create_serialize",
290                 .type   = FIO_OPT_INT,
291                 .off1   = td_var_offset(create_serialize)
292         },
293         {
294                 .name   = "create_fsync",
295                 .type   = FIO_OPT_INT,
296                 .off1   = td_var_offset(create_fsync)
297         },
298         {
299                 .name   = "loops",
300                 .type   = FIO_OPT_INT,
301                 .off1   = td_var_offset(loops)
302         },
303         {
304                 .name   = "numjobs",
305                 .type   = FIO_OPT_INT,
306                 .off1   = td_var_offset(numjobs)
307         },
308         {
309                 .name   = "cpuload",
310                 .type   = FIO_OPT_INT,
311                 .off1   = td_var_offset(cpuload)
312         },
313         {
314                 .name   = "cpuchunks",
315                 .type   = FIO_OPT_INT,
316                 .off1   = td_var_offset(cpucycle)
317         },
318         {
319                 .name   = "direct",
320                 .type   = FIO_OPT_INT,
321                 .off1   = td_var_offset(odirect)
322         },
323         {
324                 .name   = "overwrite",
325                 .type   = FIO_OPT_INT,
326                 .off1   = td_var_offset(overwrite)
327         },
328 #ifdef FIO_HAVE_CPU_AFFINITY
329         {
330                 .name   = "cpumask",
331                 .type   = FIO_OPT_INT,
332                 .cb     = str_cpumask_cb,
333         },
334 #endif
335         {
336                 .name   = "end_fsync",
337                 .type   = FIO_OPT_INT,
338                 .off1   = td_var_offset(end_fsync)
339         },
340         {
341                 .name   = "unlink",
342                 .type   = FIO_OPT_STR_SET,
343                 .off1   = td_var_offset(unlink),
344         },
345         {
346                 .name   = "exitall",
347                 .type   = FIO_OPT_STR_SET,
348                 .cb     = str_exitall_cb,
349         },
350         {
351                 .name   = "stonewall",
352                 .type   = FIO_OPT_STR_SET,
353                 .off1   = td_var_offset(stonewall),
354         },
355         {
356                 .name   = "thread",
357                 .type   = FIO_OPT_STR_SET,
358                 .off1   = td_var_offset(thread),
359         },
360         {
361                 .name   = "write_bw_log",
362                 .type   = FIO_OPT_STR_SET,
363                 .off1   = td_var_offset(write_bw_log),
364         },
365         {
366                 .name   = "write_lat_log",
367                 .type   = FIO_OPT_STR_SET,
368                 .off1   = td_var_offset(write_lat_log),
369         },
370         {
371                 .name   = "norandommap",
372                 .type   = FIO_OPT_STR_SET,
373                 .off1   = td_var_offset(norandommap),
374         },
375         {
376                 .name   = "bs_unaligned",
377                 .type   = FIO_OPT_STR_SET,
378                 .off1   = td_var_offset(bs_unaligned),
379         },
380         {
381                 .name = NULL,
382         },
383 };
384
385 #define FIO_JOB_OPTS    (sizeof(options) / sizeof(struct fio_option))
386 #define FIO_CMD_OPTS    (16)
387 #define FIO_GETOPT_JOB  (0x89988998)
388
389 /*
390  * Command line options. These will contain the above, plus a few
391  * extra that only pertain to fio itself and not jobs.
392  */
393 static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
394         {
395                 .name           = "output",
396                 .has_arg        = required_argument,
397                 .val            = 'o',
398         },
399         {
400                 .name           = "timeout",
401                 .has_arg        = required_argument,
402                 .val            = 't',
403         },
404         {
405                 .name           = "latency-log",
406                 .has_arg        = required_argument,
407                 .val            = 'l',
408         },
409         {
410                 .name           = "bandwidth-log",
411                 .has_arg        = required_argument,
412                 .val            = 'b',
413         },
414         {
415                 .name           = "minimal",
416                 .has_arg        = optional_argument,
417                 .val            = 'm',
418         },
419         {
420                 .name           = "version",
421                 .has_arg        = no_argument,
422                 .val            = 'v',
423         },
424         {
425                 .name           = NULL,
426         },
427 };
428
429 static int def_timeout = DEF_TIMEOUT;
430
431 static char fio_version_string[] = "fio 1.7";
432
433 static char **ini_file;
434 static int max_jobs = MAX_JOBS;
435
436 struct thread_data def_thread;
437 struct thread_data *threads = NULL;
438
439 int rate_quit = 0;
440 int exitall_on_terminate = 0;
441 int terse_output = 0;
442 unsigned long long mlock_size = 0;
443 FILE *f_out = NULL;
444 FILE *f_err = NULL;
445
446 static int write_lat_log = DEF_WRITE_LAT_LOG;
447 static int write_bw_log = DEF_WRITE_BW_LOG;
448
449 /*
450  * Return a free job structure.
451  */
452 static struct thread_data *get_new_job(int global, struct thread_data *parent)
453 {
454         struct thread_data *td;
455
456         if (global)
457                 return &def_thread;
458         if (thread_number >= max_jobs)
459                 return NULL;
460
461         td = &threads[thread_number++];
462         *td = *parent;
463
464         td->thread_number = thread_number;
465         return td;
466 }
467
468 static void put_job(struct thread_data *td)
469 {
470         if (td == &def_thread)
471                 return;
472
473         memset(&threads[td->thread_number - 1], 0, sizeof(*td));
474         thread_number--;
475 }
476
477 /*
478  * Lazy way of fixing up options that depend on each other. We could also
479  * define option callback handlers, but this is easier.
480  */
481 static void fixup_options(struct thread_data *td)
482 {
483         if (!td->rwmixread && td->rwmixwrite)
484                 td->rwmixread = 100 - td->rwmixwrite;
485
486         if (td->write_iolog_file && td->read_iolog_file) {
487                 log_err("fio: read iolog overrides write_iolog\n");
488                 free(td->write_iolog_file);
489                 td->write_iolog_file = NULL;
490         }
491
492         if (td->io_ops->flags & FIO_SYNCIO)
493                 td->iodepth = 1;
494         else {
495                 if (!td->iodepth)
496                         td->iodepth = td->nr_files;
497         }
498
499         /*
500          * only really works for sequential io for now, and with 1 file
501          */
502         if (td->zone_size && !td->sequential && td->nr_files == 1)
503                 td->zone_size = 0;
504
505         /*
506          * Reads can do overwrites, we always need to pre-create the file
507          */
508         if (td_read(td) || td_rw(td))
509                 td->overwrite = 1;
510
511         if (td->bs[DDIR_READ] != DEF_BS)
512                 td->bs[DDIR_WRITE] = td->bs[DDIR_READ];
513         if (!td->min_bs[DDIR_READ])
514                 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
515         if (!td->max_bs[DDIR_READ])
516                 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
517         if (!td->min_bs[DDIR_WRITE])
518                 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
519         if (!td->max_bs[DDIR_WRITE])
520                 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
521
522         td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
523
524         if (td_read(td) && !td_rw(td))
525                 td->verify = 0;
526
527         if (td->norandommap && td->verify != VERIFY_NONE) {
528                 log_err("fio: norandommap given, verify disabled\n");
529                 td->verify = VERIFY_NONE;
530         }
531         if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
532                 log_err("fio: bs_unaligned may not work with raw io\n");
533 }
534
535 /*
536  * Adds a job to the list of things todo. Sanitizes the various options
537  * to make sure we don't have conflicts, and initializes various
538  * members of td.
539  */
540 static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
541 {
542         char *ddir_str[] = { "read", "write", "randread", "randwrite",
543                              "rw", NULL, "randrw" };
544         struct stat sb;
545         int numjobs, ddir, i;
546         struct fio_file *f;
547
548         /*
549          * the def_thread is just for options, it's not a real job
550          */
551         if (td == &def_thread)
552                 return 0;
553
554         /*
555          * Set default io engine, if none set
556          */
557         if (!td->io_ops) {
558                 td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME);
559                 if (!td->io_ops) {
560                         log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME);
561                         return 1;
562                 }
563         }
564
565         if (td->odirect)
566                 td->io_ops->flags |= FIO_RAWIO;
567
568         fixup_options(td);
569
570         td->filetype = FIO_TYPE_FILE;
571         if (!stat(jobname, &sb)) {
572                 if (S_ISBLK(sb.st_mode))
573                         td->filetype = FIO_TYPE_BD;
574                 else if (S_ISCHR(sb.st_mode))
575                         td->filetype = FIO_TYPE_CHAR;
576         }
577
578         if (td->filename)
579                 td->nr_uniq_files = 1;
580         else
581                 td->nr_uniq_files = td->nr_files;
582
583         if (td->filetype == FIO_TYPE_FILE || td->filename) {
584                 char tmp[PATH_MAX];
585                 int len = 0;
586                 int i;
587
588                 if (td->directory && td->directory[0] != '\0')
589                         sprintf(tmp, "%s/", td->directory);
590
591                 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
592
593                 for_each_file(td, f, i) {
594                         memset(f, 0, sizeof(*f));
595                         f->fd = -1;
596
597                         if (td->filename)
598                                 sprintf(tmp + len, "%s", td->filename);
599                         else
600                                 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
601                         f->file_name = strdup(tmp);
602                 }
603         } else {
604                 td->nr_files = 1;
605                 td->files = malloc(sizeof(struct fio_file));
606                 f = &td->files[0];
607
608                 memset(f, 0, sizeof(*f));
609                 f->fd = -1;
610                 f->file_name = strdup(jobname);
611         }
612
613         for_each_file(td, f, i) {
614                 f->file_size = td->total_file_size / td->nr_files;
615                 f->file_offset = td->start_offset;
616         }
617                 
618         fio_sem_init(&td->mutex, 0);
619
620         td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
621         td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
622         td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
623
624         if (td->stonewall && td->thread_number > 1)
625                 groupid++;
626
627         td->groupid = groupid;
628
629         if (setup_rate(td))
630                 goto err;
631
632         if (td->write_lat_log) {
633                 setup_log(&td->slat_log);
634                 setup_log(&td->clat_log);
635         }
636         if (td->write_bw_log)
637                 setup_log(&td->bw_log);
638
639         if (!td->name)
640                 td->name = strdup(jobname);
641
642         ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
643
644         if (!terse_output) {
645                 if (!job_add_num) {
646                         if (td->io_ops->flags & FIO_CPUIO)
647                                 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
648                         else
649                                 fprintf(f_out, "%s: (g=%d): rw=%s, odir=%d, bs=%d-%d/%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->name, td->groupid, ddir_str[ddir], td->odirect, td->min_bs[DDIR_READ], td->max_bs[DDIR_READ], td->min_bs[DDIR_WRITE], td->max_bs[DDIR_WRITE], td->rate, td->io_ops->name, td->iodepth);
650                 } else if (job_add_num == 1)
651                         fprintf(f_out, "...\n");
652         }
653
654         /*
655          * recurse add identical jobs, clear numjobs and stonewall options
656          * as they don't apply to sub-jobs
657          */
658         numjobs = td->numjobs;
659         while (--numjobs) {
660                 struct thread_data *td_new = get_new_job(0, td);
661
662                 if (!td_new)
663                         goto err;
664
665                 td_new->numjobs = 1;
666                 td_new->stonewall = 0;
667                 job_add_num = numjobs - 1;
668
669                 if (add_job(td_new, jobname, job_add_num))
670                         goto err;
671         }
672         return 0;
673 err:
674         put_job(td);
675         return -1;
676 }
677
678 /*
679  * Initialize the various random states we need (random io, block size ranges,
680  * read/write mix, etc).
681  */
682 int init_random_state(struct thread_data *td)
683 {
684         unsigned long seeds[4];
685         int fd, num_maps, blocks, i;
686         struct fio_file *f;
687
688         if (td->io_ops->flags & FIO_CPUIO)
689                 return 0;
690
691         fd = open("/dev/urandom", O_RDONLY);
692         if (fd == -1) {
693                 td_verror(td, errno);
694                 return 1;
695         }
696
697         if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
698                 td_verror(td, EIO);
699                 close(fd);
700                 return 1;
701         }
702
703         close(fd);
704
705         os_random_seed(seeds[0], &td->bsrange_state);
706         os_random_seed(seeds[1], &td->verify_state);
707         os_random_seed(seeds[2], &td->rwmix_state);
708
709         if (td->sequential)
710                 return 0;
711
712         if (td->rand_repeatable)
713                 seeds[3] = DEF_RANDSEED;
714
715         if (!td->norandommap) {
716                 for_each_file(td, f, i) {
717                         blocks = (f->file_size + td->rw_min_bs - 1) / td->rw_min_bs;
718                         num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
719                         f->file_map = malloc(num_maps * sizeof(long));
720                         f->num_maps = num_maps;
721                         memset(f->file_map, 0, num_maps * sizeof(long));
722                 }
723         }
724
725         os_random_seed(seeds[3], &td->random_state);
726         return 0;
727 }
728
729 static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
730 {
731 #ifdef FIO_HAVE_CPU_AFFINITY
732         unsigned int i;
733
734         CPU_ZERO(&cpumask);
735
736         for (i = 0; i < sizeof(int) * 8; i++) {
737                 if ((1 << i) & cpu)
738                         CPU_SET(i, &cpumask);
739         }
740 #endif
741 }
742
743 static int is_empty_or_comment(char *line)
744 {
745         unsigned int i;
746
747         for (i = 0; i < strlen(line); i++) {
748                 if (line[i] == ';')
749                         return 1;
750                 if (!isspace(line[i]) && !iscntrl(line[i]))
751                         return 0;
752         }
753
754         return 1;
755 }
756
757 static int str_rw_cb(void *data, const char *mem)
758 {
759         struct thread_data *td = data;
760
761         if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
762                 td->ddir = DDIR_READ;
763                 td->sequential = 1;
764                 return 0;
765         } else if (!strncmp(mem, "randread", 8)) {
766                 td->ddir = DDIR_READ;
767                 td->sequential = 0;
768                 return 0;
769         } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
770                 td->ddir = DDIR_WRITE;
771                 td->sequential = 1;
772                 return 0;
773         } else if (!strncmp(mem, "randwrite", 9)) {
774                 td->ddir = DDIR_WRITE;
775                 td->sequential = 0;
776                 return 0;
777         } else if (!strncmp(mem, "rw", 2)) {
778                 td->ddir = 0;
779                 td->iomix = 1;
780                 td->sequential = 1;
781                 return 0;
782         } else if (!strncmp(mem, "randrw", 6)) {
783                 td->ddir = 0;
784                 td->iomix = 1;
785                 td->sequential = 0;
786                 return 0;
787         }
788
789         log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
790         return 1;
791 }
792
793 static int str_verify_cb(void *data, const char *mem)
794 {
795         struct thread_data *td = data;
796
797         if (!strncmp(mem, "0", 1)) {
798                 td->verify = VERIFY_NONE;
799                 return 0;
800         } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
801                 td->verify = VERIFY_MD5;
802                 return 0;
803         } else if (!strncmp(mem, "crc32", 5)) {
804                 td->verify = VERIFY_CRC32;
805                 return 0;
806         }
807
808         log_err("fio: verify types: md5, crc32\n");
809         return 1;
810 }
811
812 static int str_mem_cb(void *data, const char *mem)
813 {
814         struct thread_data *td = data;
815
816         if (!strncmp(mem, "malloc", 6)) {
817                 td->mem_type = MEM_MALLOC;
818                 return 0;
819         } else if (!strncmp(mem, "shm", 3)) {
820                 td->mem_type = MEM_SHM;
821                 return 0;
822         } else if (!strncmp(mem, "mmap", 4)) {
823                 td->mem_type = MEM_MMAP;
824                 return 0;
825         }
826
827         log_err("fio: mem type: malloc, shm, mmap\n");
828         return 1;
829 }
830
831 static int str_ioengine_cb(void *data, const char *str)
832 {
833         struct thread_data *td = data;
834
835         td->io_ops = load_ioengine(td, str);
836         if (td->io_ops)
837                 return 0;
838
839         log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
840         return 1;
841 }
842
843 static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
844 {
845         mlock_size = *val;
846         return 0;
847 }
848
849 #ifdef FIO_HAVE_IOPRIO
850 static int str_prioclass_cb(void *data, unsigned int *val)
851 {
852         struct thread_data *td = data;
853
854         td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
855         return 0;
856 }
857
858 static int str_prio_cb(void *data, unsigned int *val)
859 {
860         struct thread_data *td = data;
861
862         td->ioprio |= *val;
863         return 0;
864 }
865 #endif
866
867 static int str_exitall_cb(void)
868 {
869         exitall_on_terminate = 1;
870         return 0;
871 }
872
873 static int str_cpumask_cb(void *data, unsigned int *val)
874 {
875         struct thread_data *td = data;
876
877         fill_cpu_mask(td->cpumask, *val);
878         return 0;
879 }
880
881 /*
882  * This is our [ini] type file parser.
883  */
884 int parse_jobs_ini(char *file, int stonewall_flag)
885 {
886         unsigned int global;
887         struct thread_data *td;
888         char *string, *name;
889         fpos_t off;
890         FILE *f;
891         char *p;
892         int ret = 0, stonewall;
893
894         f = fopen(file, "r");
895         if (!f) {
896                 perror("fopen job file");
897                 return 1;
898         }
899
900         string = malloc(4096);
901         name = malloc(256);
902         memset(name, 0, 256);
903
904         stonewall = stonewall_flag;
905         do {
906                 p = fgets(string, 4095, f);
907                 if (!p)
908                         break;
909                 if (is_empty_or_comment(p))
910                         continue;
911                 if (sscanf(p, "[%255s]", name) != 1)
912                         continue;
913
914                 global = !strncmp(name, "global", 6);
915
916                 name[strlen(name) - 1] = '\0';
917
918                 td = get_new_job(global, &def_thread);
919                 if (!td) {
920                         ret = 1;
921                         break;
922                 }
923
924                 /*
925                  * Seperate multiple job files by a stonewall
926                  */
927                 if (!global && stonewall) {
928                         td->stonewall = stonewall;
929                         stonewall = 0;
930                 }
931
932                 fgetpos(f, &off);
933                 while ((p = fgets(string, 4096, f)) != NULL) {
934                         if (is_empty_or_comment(p))
935                                 continue;
936
937                         strip_blank_front(&p);
938
939                         if (p[0] == '[')
940                                 break;
941
942                         strip_blank_end(p);
943
944                         fgetpos(f, &off);
945
946                         /*
947                          * Don't break here, continue parsing options so we
948                          * dump all the bad ones. Makes trial/error fixups
949                          * easier on the user.
950                          */
951                         ret |= parse_option(p, options, td);
952                 }
953
954                 if (!ret) {
955                         fsetpos(f, &off);
956                         ret = add_job(td, name, 0);
957                 } else {
958                         log_err("fio: job %s dropped\n", name);
959                         put_job(td);
960                 }
961         } while (!ret);
962
963         free(string);
964         free(name);
965         fclose(f);
966         return ret;
967 }
968
969 static int fill_def_thread(void)
970 {
971         memset(&def_thread, 0, sizeof(def_thread));
972
973         if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
974                 perror("sched_getaffinity");
975                 return 1;
976         }
977
978         /*
979          * fill globals
980          */
981         def_thread.ddir = DDIR_READ;
982         def_thread.iomix = 0;
983         def_thread.bs[DDIR_READ] = DEF_BS;
984         def_thread.bs[DDIR_WRITE] = DEF_BS;
985         def_thread.min_bs[DDIR_READ] = def_thread.min_bs[DDIR_WRITE] = 0;
986         def_thread.max_bs[DDIR_READ] = def_thread.max_bs[DDIR_WRITE] = 0;
987         def_thread.odirect = DEF_ODIRECT;
988         def_thread.ratecycle = DEF_RATE_CYCLE;
989         def_thread.sequential = DEF_SEQUENTIAL;
990         def_thread.timeout = def_timeout;
991         def_thread.overwrite = DEF_OVERWRITE;
992         def_thread.invalidate_cache = DEF_INVALIDATE;
993         def_thread.sync_io = DEF_SYNCIO;
994         def_thread.mem_type = MEM_MALLOC;
995         def_thread.bw_avg_time = DEF_BWAVGTIME;
996         def_thread.create_serialize = DEF_CREATE_SER;
997         def_thread.create_fsync = DEF_CREATE_FSYNC;
998         def_thread.loops = DEF_LOOPS;
999         def_thread.verify = DEF_VERIFY;
1000         def_thread.stonewall = DEF_STONEWALL;
1001         def_thread.numjobs = DEF_NUMJOBS;
1002         def_thread.use_thread = DEF_USE_THREAD;
1003         def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1004         def_thread.rwmixread = DEF_RWMIX_READ;
1005         def_thread.nice = DEF_NICE;
1006         def_thread.rand_repeatable = DEF_RAND_REPEAT;
1007         def_thread.nr_files = DEF_NR_FILES;
1008         def_thread.unlink = DEF_UNLINK;
1009         def_thread.write_bw_log = write_bw_log;
1010         def_thread.write_lat_log = write_lat_log;
1011         def_thread.norandommap = DEF_NO_RAND_MAP;
1012 #ifdef FIO_HAVE_DISK_UTIL
1013         def_thread.do_disk_util = 1;
1014 #endif
1015
1016         return 0;
1017 }
1018
1019 static void usage(void)
1020 {
1021         printf("%s\n", fio_version_string);
1022         printf("\t--output\tWrite output to file\n");
1023         printf("\t--timeout\tRuntime in seconds\n");
1024         printf("\t--latency-log\tGenerate per-job latency logs\n");
1025         printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1026         printf("\t--minimal\tMinimal (terse) output\n");
1027         printf("\t--version\tPrint version info and exit\n");
1028 }
1029
1030 static int parse_cmd_line(int argc, char *argv[])
1031 {
1032         struct thread_data *td = NULL;
1033         int c, ini_idx = 0, lidx, ret;
1034
1035         while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
1036                 switch (c) {
1037                 case 't':
1038                         def_timeout = atoi(optarg);
1039                         break;
1040                 case 'l':
1041                         write_lat_log = 1;
1042                         break;
1043                 case 'w':
1044                         write_bw_log = 1;
1045                         break;
1046                 case 'o':
1047                         f_out = fopen(optarg, "w+");
1048                         if (!f_out) {
1049                                 perror("fopen output");
1050                                 exit(1);
1051                         }
1052                         f_err = f_out;
1053                         break;
1054                 case 'm':
1055                         terse_output = 1;
1056                         break;
1057                 case 'h':
1058                         usage();
1059                         exit(0);
1060                 case 'v':
1061                         printf("%s\n", fio_version_string);
1062                         exit(0);
1063                 case FIO_GETOPT_JOB: {
1064                         const char *opt = long_options[lidx].name;
1065                         char *val = optarg;
1066
1067                         if (!strncmp(opt, "name", 4) && td) {
1068                                 ret = add_job(td, td->name ?: "fio", 0);
1069                                 if (ret) {
1070                                         put_job(td);
1071                                         return 0;
1072                                 }
1073                                 td = NULL;
1074                         }
1075                         if (!td) {
1076                                 int global = !strncmp(val, "global", 6);
1077
1078                                 td = get_new_job(global, &def_thread);
1079                                 if (!td)
1080                                         return 0;
1081                         }
1082
1083                         ret = parse_cmd_option(opt, val, options, td);
1084                         if (ret) {
1085                                 log_err("fio: job dropped\n");
1086                                 put_job(td);
1087                                 td = NULL;
1088                         }
1089                         break;
1090                 }
1091                 default:
1092                         printf("optarg <<%s>>\n", argv[optind]);
1093                         break;
1094                 }
1095         }
1096
1097         if (td) {
1098                 ret = add_job(td, td->name ?: "fio", 0);
1099                 if (ret)
1100                         put_job(td);
1101         }
1102
1103         while (optind < argc) {
1104                 ini_idx++;
1105                 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1106                 ini_file[ini_idx - 1] = strdup(argv[optind]);
1107                 optind++;
1108         }
1109
1110         return ini_idx;
1111 }
1112
1113 static void free_shm(void)
1114 {
1115         struct shmid_ds sbuf;
1116
1117         if (threads) {
1118                 shmdt((void *) threads);
1119                 threads = NULL;
1120                 shmctl(shm_id, IPC_RMID, &sbuf);
1121         }
1122 }
1123
1124 /*
1125  * The thread area is shared between the main process and the job
1126  * threads/processes. So setup a shared memory segment that will hold
1127  * all the job info.
1128  */
1129 static int setup_thread_area(void)
1130 {
1131         /*
1132          * 1024 is too much on some machines, scale max_jobs if
1133          * we get a failure that looks like too large a shm segment
1134          */
1135         do {
1136                 size_t size = max_jobs * sizeof(struct thread_data);
1137
1138                 shm_id = shmget(0, size, IPC_CREAT | 0600);
1139                 if (shm_id != -1)
1140                         break;
1141                 if (errno != EINVAL) {
1142                         perror("shmget");
1143                         break;
1144                 }
1145
1146                 max_jobs >>= 1;
1147         } while (max_jobs);
1148
1149         if (shm_id == -1)
1150                 return 1;
1151
1152         threads = shmat(shm_id, NULL, 0);
1153         if (threads == (void *) -1) {
1154                 perror("shmat");
1155                 return 1;
1156         }
1157
1158         atexit(free_shm);
1159         return 0;
1160 }
1161
1162 /*
1163  * Copy the fio options into the long options map, so we mirror
1164  * job and cmd line options.
1165  */
1166 static void dupe_job_options(void)
1167 {
1168         struct fio_option *o;
1169         unsigned int i;
1170
1171         i = 0;
1172         while (long_options[i].name)
1173                 i++;
1174
1175         o = &options[0];
1176         while (o->name) {
1177                 long_options[i].name = o->name;
1178                 long_options[i].val = FIO_GETOPT_JOB;
1179                 if (o->type == FIO_OPT_STR_SET)
1180                         long_options[i].has_arg = no_argument;
1181                 else
1182                         long_options[i].has_arg = required_argument;
1183
1184                 i++;
1185                 o++;
1186                 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1187         }
1188 }
1189
1190 int parse_options(int argc, char *argv[])
1191 {
1192         int job_files, i;
1193
1194         f_out = stdout;
1195         f_err = stderr;
1196
1197         dupe_job_options();
1198
1199         if (setup_thread_area())
1200                 return 1;
1201         if (fill_def_thread())
1202                 return 1;
1203
1204         job_files = parse_cmd_line(argc, argv);
1205
1206         for (i = 0; i < job_files; i++) {
1207                 if (fill_def_thread())
1208                         return 1;
1209                 if (parse_jobs_ini(ini_file[i], i))
1210                         return 1;
1211                 free(ini_file[i]);
1212         }
1213
1214         free(ini_file);
1215
1216         if (!thread_number) {
1217                 log_err("No jobs defined(s)\n");
1218                 return 1;
1219         }
1220
1221         return 0;
1222 }