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