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