[PATCH] Add bw/lat log as job options
[fio.git] / init.c
CommitLineData
906c8d75
JA
1/*
2 * This file contains the ini and command liner parser. It will create
3 * and initialize the specified jobs.
4 */
ebac4655
JA
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
906c8d75
JA
19/*
20 * The default options
21 */
20dc95c4
JA
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)
ebac4655 27#define DEF_IO_ENGINE_NAME "sync"
20dc95c4
JA
28#define DEF_SEQUENTIAL (1)
29#define DEF_RAND_REPEAT (1)
30#define DEF_OVERWRITE (1)
20dc95c4
JA
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)
ebac4655 36#define DEF_CREATE_FSYNC (1)
20dc95c4
JA
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)
a6ccc7be
JA
45#define DEF_RWMIX_CYCLE (500)
46#define DEF_RWMIX_READ (50)
b6f4d880 47#define DEF_NICE (0)
53cdc686 48#define DEF_NR_FILES (1)
f6cbb269 49#define DEF_UNLINK (0)
ec94ec56
JA
50#define DEF_WRITE_BW_LOG (0)
51#define DEF_WRITE_LAT_LOG (0)
ebac4655 52
972cfd25 53static int def_timeout = DEF_TIMEOUT;
972cfd25 54
f4866ec5 55static char fio_version_string[] = "fio 1.5";
ebac4655 56
972cfd25 57static char **ini_file;
ebac4655
JA
58static int max_jobs = MAX_JOBS;
59
60struct thread_data def_thread;
61struct thread_data *threads = NULL;
62
63int rate_quit = 0;
ebac4655 64int exitall_on_terminate = 0;
c6ae0a5b 65int terse_output = 0;
c04f7ec3 66unsigned long long mlock_size = 0;
eb8bbf48
JA
67FILE *f_out = NULL;
68FILE *f_err = NULL;
ebac4655 69
ec94ec56
JA
70static int write_lat_log = DEF_WRITE_LAT_LOG;
71static int write_bw_log = DEF_WRITE_BW_LOG;
72
906c8d75
JA
73/*
74 * Return a free job structure.
75 */
ebac4655
JA
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++];
ddaeaa5a
JA
86 *td = *parent;
87 td->name[0] = '\0';
ebac4655 88
ebac4655 89 td->thread_number = thread_number;
ebac4655
JA
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
906c8d75
JA
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 */
75154845 104static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
ebac4655 105{
3d60d1ed
JA
106 char *ddir_str[] = { "read", "write", "randread", "randwrite",
107 "rw", NULL, "randrw" };
ebac4655 108 struct stat sb;
53cdc686
JA
109 int numjobs, ddir, i;
110 struct fio_file *f;
ebac4655
JA
111
112#ifndef FIO_HAVE_LIBAIO
113 if (td->io_engine == FIO_LIBAIO) {
3b70d7e5 114 log_err("Linux libaio not available\n");
ebac4655
JA
115 return 1;
116 }
117#endif
118#ifndef FIO_HAVE_POSIXAIO
119 if (td->io_engine == FIO_POSIXAIO) {
3b70d7e5 120 log_err("posix aio not available\n");
ebac4655
JA
121 return 1;
122 }
123#endif
ebac4655
JA
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
df64119d
JA
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
2866c82d 142 if (td->io_ops->flags & FIO_SYNCIO)
ebac4655
JA
143 td->iodepth = 1;
144 else {
145 if (!td->iodepth)
841ddd13 146 td->iodepth = td->nr_files;
ebac4655
JA
147 }
148
20dc95c4 149 /*
53cdc686 150 * only really works for sequential io for now, and with 1 file
20dc95c4 151 */
53cdc686 152 if (td->zone_size && !td->sequential && td->nr_files == 1)
20dc95c4
JA
153 td->zone_size = 0;
154
9cc935a1
JA
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
ebac4655 161 td->filetype = FIO_TYPE_FILE;
0af7b542
JA
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 }
ebac4655 168
b2a15192
JA
169 if (td->odirect)
170 td->io_ops->flags |= FIO_RAWIO;
171
ebac4655 172 if (td->filetype == FIO_TYPE_FILE) {
e9c047a0 173 char tmp[PATH_MAX];
53cdc686
JA
174 int len = 0;
175 int i;
e9c047a0 176
ef899b63 177 if (td->directory && td->directory[0] != '\0')
53cdc686 178 sprintf(tmp, "%s/", td->directory);
ebac4655 179
53cdc686
JA
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;
b2a15192 185 f->fileno = i;
53cdc686
JA
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
bbfd6b00 205 fio_sem_init(&td->mutex, 0);
ebac4655
JA
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;
3d60d1ed 215 if (td_read(td) && !td_rw(td))
ebac4655
JA
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
ec94ec56 226 if (td->write_lat_log) {
ebac4655
JA
227 setup_log(&td->slat_log);
228 setup_log(&td->clat_log);
229 }
ec94ec56 230 if (td->write_bw_log)
ebac4655
JA
231 setup_log(&td->bw_log);
232
01452055
JA
233 if (td->name[0] == '\0')
234 snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number);
235
3d60d1ed 236 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
75154845 237
c6ae0a5b 238 if (!terse_output) {
b990b5c0 239 if (!job_add_num) {
2866c82d 240 if (td->io_ops->flags & FIO_CPUIO)
b990b5c0
JA
241 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
242 else
2866c82d 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);
b990b5c0 244 } else if (job_add_num == 1)
c6ae0a5b
JA
245 fprintf(f_out, "...\n");
246 }
ebac4655
JA
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;
75154845 261 job_add_num = numjobs - 1;
ebac4655 262
75154845 263 if (add_job(td_new, jobname, job_add_num))
ebac4655
JA
264 goto err;
265 }
266 return 0;
267err:
268 put_job(td);
269 return -1;
270}
271
906c8d75
JA
272/*
273 * Initialize the various random states we need (random io, block size ranges,
274 * read/write mix, etc).
275 */
ebac4655
JA
276int init_random_state(struct thread_data *td)
277{
a6ccc7be 278 unsigned long seeds[4];
53cdc686 279 int fd, num_maps, blocks, i;
0ab8db89 280 struct fio_file *f;
ebac4655 281
1ac267bb 282 fd = open("/dev/urandom", O_RDONLY);
ebac4655
JA
283 if (fd == -1) {
284 td_verror(td, errno);
285 return 1;
286 }
287
a6ccc7be 288 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
ebac4655
JA
289 td_verror(td, EIO);
290 close(fd);
291 return 1;
292 }
293
294 close(fd);
295
6dfd46b9
JA
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);
ebac4655
JA
299
300 if (td->sequential)
301 return 0;
302
9ebc27e1 303 if (td->rand_repeatable)
a6ccc7be 304 seeds[3] = DEF_RANDSEED;
ebac4655 305
0ab8db89 306 for_each_file(td, f, i) {
53cdc686
JA
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 }
ebac4655 313
6dfd46b9 314 os_random_seed(seeds[3], &td->random_state);
ebac4655
JA
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
906c8d75
JA
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)
ebac4655
JA
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 */
906c8d75 369static int str_to_decimal(char *p, unsigned long long *val, int kilo)
ebac4655
JA
370{
371 char *str;
372 int len;
373
f5d3e5ec 374 str = strchr(p, '=');
ebac4655
JA
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
906c8d75
JA
385 if (kilo)
386 *val *= get_mult_bytes(str[len - 1]);
387 else
388 *val *= get_mult_time(str[len - 1]);
ebac4655
JA
389 return 0;
390}
391
906c8d75 392static int check_str_bytes(char *p, char *name, unsigned long long *val)
ebac4655 393{
20dc95c4 394 if (strncmp(p, name, strlen(name) - 1))
ebac4655
JA
395 return 1;
396
906c8d75
JA
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);
ebac4655
JA
406}
407
408static void strip_blank_front(char **p)
409{
410 char *s = *p;
411
4ae3f763 412 while (isspace(*s))
ebac4655
JA
413 s++;
414}
415
416static void strip_blank_end(char *p)
417{
4ae3f763 418 char *s = p + strlen(p) - 1;
ebac4655 419
4ae3f763
JA
420 while (isspace(*s) || iscntrl(*s))
421 s--;
aea47d44 422
4ae3f763 423 *(s + 1) = '\0';
aea47d44
JA
424}
425
ebac4655
JA
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{
843a7413 430 char *s;
ebac4655 431
843a7413
JA
432 if (strncmp(p, name, strlen(name)))
433 return 1;
434
435 s = strstr(p, name);
ebac4655
JA
436 if (!s)
437 return 1;
438
f5d3e5ec 439 s = strchr(s, '=');
ebac4655
JA
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{
843a7413 450 char *s;
ebac4655 451
843a7413
JA
452 if (strncmp(p, name, strlen(name)))
453 return 1;
454
455 s = strstr(p, name);
ebac4655
JA
456 if (!s)
457 return 1;
458
f5d3e5ec 459 s = strchr(p, '=');
ebac4655
JA
460 if (!s)
461 return 1;
462
463 s++;
464 strip_blank_front(&s);
465
466 strcpy(dest, s);
ebac4655
JA
467 return 0;
468}
469
906c8d75 470static int __check_range_bytes(char *str, unsigned long *val)
ebac4655 471{
01617be6 472 char suffix;
ebac4655 473
01617be6 474 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
906c8d75 475 *val *= get_mult_bytes(suffix);
ebac4655
JA
476 return 0;
477 }
478
01617be6 479 if (sscanf(str, "%lu", val) == 1)
ebac4655 480 return 0;
ebac4655 481
01617be6
JA
482 return 1;
483}
484
906c8d75
JA
485static int check_range_bytes(char *p, char *name, unsigned long *s,
486 unsigned long *e)
01617be6
JA
487{
488 char option[128];
489 char *str, *p1, *p2;
490
843a7413
JA
491 if (strncmp(p, name, strlen(name)))
492 return 1;
493
01617be6
JA
494 strcpy(option, p);
495 p = option;
496
497 str = strstr(p, name);
498 if (!str)
499 return 1;
ebac4655 500
01617be6
JA
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
906c8d75 522 if (!__check_range_bytes(p1, s) && !__check_range_bytes(p2, e))
ebac4655
JA
523 return 0;
524
525 return 1;
ebac4655
JA
526}
527
528static int check_int(char *p, char *name, unsigned int *val)
529{
f5d3e5ec 530 char *str;
ebac4655 531
b6754f9d
JA
532 if (strncmp(p, name, strlen(name)))
533 return 1;
534
f5d3e5ec
JA
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++;
ebac4655 544
f5d3e5ec 545 if (sscanf(str, "%u", val) == 1)
ebac4655
JA
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;
3d60d1ed
JA
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;
ebac4655
JA
598 }
599
3b70d7e5 600 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
ebac4655
JA
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
3b70d7e5 617 log_err("fio: verify types: md5, crc32\n");
ebac4655
JA
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
3b70d7e5 634 log_err("fio: mem type: malloc, shm, mmap\n");
ebac4655
JA
635 return 1;
636}
637
638static int str_ioengine_cb(struct thread_data *td, char *str)
639{
2866c82d
JA
640 td->io_ops = load_ioengine(td, str);
641 if (td->io_ops)
b990b5c0 642 return 0;
ebac4655 643
b990b5c0 644 log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n");
ebac4655
JA
645 return 1;
646}
647
07261983
JA
648/*
649 * This is our [ini] type file parser.
650 */
0c7e37a0 651int parse_jobs_ini(char *file, int stonewall_flag)
ebac4655 652{
a6ccc7be 653 unsigned int prioclass, prio, cpu, global, il;
ebac4655
JA
654 unsigned long long ull;
655 unsigned long ul1, ul2;
656 struct thread_data *td;
ef899b63 657 char *string, *name, *tmpbuf;
ebac4655
JA
658 fpos_t off;
659 FILE *f;
660 char *p;
0c7e37a0 661 int ret = 0, stonewall;
ebac4655
JA
662
663 f = fopen(file, "r");
664 if (!f) {
aea47d44 665 perror("fopen job file");
ebac4655
JA
666 return 1;
667 }
668
669 string = malloc(4096);
670 name = malloc(256);
ef899b63 671 tmpbuf = malloc(4096);
ebac4655 672
0c7e37a0 673 stonewall = stonewall_flag;
ebac4655 674 while ((p = fgets(string, 4096, f)) != NULL) {
45410acb
JA
675 if (ret)
676 break;
ebac4655
JA
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);
45410acb
JA
687 if (!td) {
688 ret = 1;
689 break;
690 }
ebac4655 691
972cfd25
JA
692 /*
693 * Seperate multiple job files by a stonewall
694 */
f9481919 695 if (!global && stonewall) {
972cfd25
JA
696 td->stonewall = stonewall;
697 stonewall = 0;
698 }
699
ebac4655
JA
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;
b6754f9d 706 strip_blank_front(&p);
4ae3f763 707 strip_blank_end(p);
aea47d44 708
ebac4655
JA
709 if (!check_int(p, "prio", &prio)) {
710#ifndef FIO_HAVE_IOPRIO
3b70d7e5 711 log_err("io priorities not available\n");
45410acb
JA
712 ret = 1;
713 break;
ebac4655 714#endif
8756e4d4 715 td->ioprio |= prio;
ebac4655
JA
716 fgetpos(f, &off);
717 continue;
718 }
719 if (!check_int(p, "prioclass", &prioclass)) {
720#ifndef FIO_HAVE_IOPRIO
3b70d7e5 721 log_err("io priorities not available\n");
45410acb
JA
722 ret = 1;
723 break;
5c4e1dbc 724#else
8756e4d4 725 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
ebac4655
JA
726 fgetpos(f, &off);
727 continue;
5c4e1dbc 728#endif
ebac4655 729 }
e9c047a0
JA
730 if (!check_int(p, "direct", &il)) {
731 td->odirect = il;
ebac4655
JA
732 fgetpos(f, &off);
733 continue;
734 }
9ebc27e1
JA
735 if (!check_int(p, "rand_repeatable", &il)) {
736 td->rand_repeatable = il;
737 fgetpos(f, &off);
738 continue;
739 }
ebac4655
JA
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 }
b990b5c0
JA
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 }
ebac4655
JA
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
3b70d7e5 766 log_err("cpu affinity not available\n");
45410acb
JA
767 ret = 1;
768 break;
ebac4655
JA
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);
fc1a4713 776 td->end_fsync = 1;
ebac4655
JA
777 continue;
778 }
779 if (!check_int(p, "startdelay", &td->start_delay)) {
780 fgetpos(f, &off);
781 continue;
782 }
906c8d75 783 if (!check_str_time(p, "timeout", &ull)) {
af959a78 784 td->timeout = ull;
ebac4655
JA
785 fgetpos(f, &off);
786 continue;
787 }
e9c047a0
JA
788 if (!check_int(p, "invalidate", &il)) {
789 td->invalidate_cache = il;
ebac4655
JA
790 fgetpos(f, &off);
791 continue;
792 }
793 if (!check_int(p, "iodepth", &td->iodepth)) {
794 fgetpos(f, &off);
795 continue;
796 }
e9c047a0
JA
797 if (!check_int(p, "sync", &il)) {
798 td->sync_io = il;
ebac4655
JA
799 fgetpos(f, &off);
800 continue;
801 }
802 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
803 fgetpos(f, &off);
804 continue;
805 }
e9c047a0
JA
806 if (!check_int(p, "create_serialize", &il)) {
807 td->create_serialize = il;
ebac4655
JA
808 fgetpos(f, &off);
809 continue;
810 }
e9c047a0
JA
811 if (!check_int(p, "create_fsync", &il)) {
812 td->create_fsync = il;
ebac4655
JA
813 fgetpos(f, &off);
814 continue;
815 }
e9c047a0
JA
816 if (!check_int(p, "end_fsync", &il)) {
817 td->end_fsync = il;
fc1a4713
JA
818 fgetpos(f, &off);
819 continue;
820 }
ebac4655
JA
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 }
e9c047a0
JA
829 if (!check_int(p, "overwrite", &il)) {
830 td->overwrite = il;
ebac4655
JA
831 fgetpos(f, &off);
832 continue;
833 }
a6ccc7be
JA
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 }
b6f4d880
JA
852 if (!check_int(p, "nice", &td->nice)) {
853 fgetpos(f, &off);
854 continue;
855 }
53cdc686
JA
856 if (!check_int(p, "nrfiles", &td->nr_files)) {
857 fgetpos(f, &off);
858 continue;
859 }
906c8d75 860 if (!check_range_bytes(p, "bsrange", &ul1, &ul2)) {
ebac4655
JA
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 }
906c8d75 871 if (!check_str_bytes(p, "bs", &ull)) {
ebac4655
JA
872 td->bs = ull;
873 fgetpos(f, &off);
874 continue;
875 }
53cdc686 876 if (!check_str_bytes(p, "size", &td->total_file_size)) {
ebac4655
JA
877 fgetpos(f, &off);
878 continue;
879 }
53cdc686 880 if (!check_str_bytes(p, "offset", &td->start_offset)) {
ebac4655 881 fgetpos(f, &off);
20dc95c4
JA
882 continue;
883 }
906c8d75 884 if (!check_str_bytes(p, "zonesize", &td->zone_size)) {
20dc95c4
JA
885 fgetpos(f, &off);
886 continue;
887 }
906c8d75 888 if (!check_str_bytes(p, "zoneskip", &td->zone_skip)) {
20dc95c4 889 fgetpos(f, &off);
ebac4655
JA
890 continue;
891 }
906c8d75 892 if (!check_str_bytes(p, "lockmem", &mlock_size)) {
c04f7ec3
JA
893 fgetpos(f, &off);
894 continue;
895 }
ef899b63
JA
896 if (!check_strstore(p, "directory", tmpbuf)) {
897 td->directory = strdup(tmpbuf);
ebac4655
JA
898 fgetpos(f, &off);
899 continue;
900 }
01452055
JA
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 }
ebac4655
JA
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 }
ebac4655
JA
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 }
f6cbb269
JA
937 if (!check_strset(p, "unlink")) {
938 td->unlink = 1;
939 fgetpos(f, &off);
940 continue;
941 }
ec94ec56
JA
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 }
80b9feb0 952 if (!check_strstore(p, "iolog", tmpbuf)) {
4f693b9d 953 if (td->write_iolog) {
3b70d7e5 954 log_err("fio: read iolog overrides given write_iolog\n");
af8c0b4f 955 free(td->iolog_file);
4f693b9d
JA
956 td->write_iolog = 0;
957 }
80b9feb0 958 td->iolog_file = strdup(tmpbuf);
843a7413 959 td->read_iolog = 1;
843a7413
JA
960 fgetpos(f, &off);
961 continue;
962 }
af8c0b4f
JA
963 if (!check_strstore(p, "write_iolog", tmpbuf)) {
964 if (!td->read_iolog) {
965 td->iolog_file = strdup(tmpbuf);
966 td->write_iolog = 1;
4f693b9d 967 } else
3b70d7e5 968 log_err("fio: read iolog overrides given write_iolog\n");
aea47d44
JA
969 fgetpos(f, &off);
970 continue;
971 }
80b9feb0
JA
972 if (!check_strstore(p, "exec_prerun", tmpbuf)) {
973 td->exec_prerun = strdup(tmpbuf);
4e0ba8af
JA
974 fgetpos(f, &off);
975 continue;
976 }
80b9feb0
JA
977 if (!check_strstore(p, "exec_postrun", tmpbuf)) {
978 td->exec_postrun = strdup(tmpbuf);
4e0ba8af
JA
979 fgetpos(f, &off);
980 continue;
981 }
80b9feb0 982 if (!check_strstore(p, "ioscheduler", tmpbuf)) {
22f78b32 983#ifndef FIO_HAVE_IOSCHED_SWITCH
3b70d7e5 984 log_err("io scheduler switching not available\n");
22f78b32
JA
985 ret = 1;
986 break;
987#else
80b9feb0 988 td->ioscheduler = strdup(tmpbuf);
da86774e
JA
989 fgetpos(f, &off);
990 continue;
22f78b32 991#endif
da86774e 992 }
ebac4655 993
45410acb
JA
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 */
ebac4655 999 printf("Client%d: bad option %s\n",td->thread_number,p);
45410acb 1000 ret = 1;
ebac4655 1001 }
ebac4655 1002
45410acb
JA
1003 if (!ret) {
1004 fsetpos(f, &off);
1005 ret = add_job(td, name, 0);
1006 }
1007 if (ret)
1008 break;
ebac4655
JA
1009 }
1010
1011 free(string);
1012 free(name);
ef899b63 1013 free(tmpbuf);
ebac4655 1014 fclose(f);
45410acb 1015 return ret;
ebac4655
JA
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;
3d60d1ed 1031 def_thread.iomix = 0;
9ebc27e1 1032 def_thread.bs = DEF_BS;
ebac4655
JA
1033 def_thread.min_bs = -1;
1034 def_thread.max_bs = -1;
9ebc27e1 1035 def_thread.odirect = DEF_ODIRECT;
ebac4655 1036 def_thread.ratecycle = DEF_RATE_CYCLE;
9ebc27e1 1037 def_thread.sequential = DEF_SEQUENTIAL;
972cfd25 1038 def_thread.timeout = def_timeout;
ebac4655
JA
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;
a6ccc7be
JA
1051 def_thread.rwmixcycle = DEF_RWMIX_CYCLE;
1052 def_thread.rwmixread = DEF_RWMIX_READ;
b6f4d880 1053 def_thread.nice = DEF_NICE;
9ebc27e1 1054 def_thread.rand_repeatable = DEF_RAND_REPEAT;
53cdc686 1055 def_thread.nr_files = DEF_NR_FILES;
f6cbb269 1056 def_thread.unlink = DEF_UNLINK;
ec94ec56
JA
1057 def_thread.write_bw_log = write_bw_log;
1058 def_thread.write_lat_log = write_lat_log;
ebac4655
JA
1059#ifdef FIO_HAVE_DISK_UTIL
1060 def_thread.do_disk_util = 1;
1061#endif
1062
1063 return 0;
1064}
1065
0ab8db89 1066static void usage(void)
4785f995
JA
1067{
1068 printf("%s\n", fio_version_string);
47623c72 1069 printf("\t-o Write output to file\n");
4785f995 1070 printf("\t-t Runtime in seconds\n");
4785f995
JA
1071 printf("\t-l Generate per-job latency logs\n");
1072 printf("\t-w Generate per-job bandwidth logs\n");
c6ae0a5b 1073 printf("\t-m Minimal (terse) output\n");
4785f995
JA
1074 printf("\t-v Print version info and exit\n");
1075}
1076
972cfd25 1077static int parse_cmd_line(int argc, char *argv[])
ebac4655 1078{
972cfd25 1079 int c, idx = 1, ini_idx = 0;
ebac4655 1080
2e77841b 1081 while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) {
ebac4655 1082 switch (c) {
ebac4655 1083 case 't':
972cfd25 1084 def_timeout = atoi(optarg);
774a6177 1085 idx = optind;
ebac4655 1086 break;
ebac4655
JA
1087 case 'l':
1088 write_lat_log = 1;
774a6177 1089 idx = optind;
ebac4655
JA
1090 break;
1091 case 'w':
1092 write_bw_log = 1;
774a6177 1093 idx = optind;
ebac4655 1094 break;
9ebc27e1 1095 case 'o':
eb8bbf48
JA
1096 f_out = fopen(optarg, "w+");
1097 if (!f_out) {
1098 perror("fopen output");
1099 exit(1);
1100 }
1101 f_err = f_out;
774a6177 1102 idx = optind;
eb8bbf48 1103 break;
c6ae0a5b
JA
1104 case 'm':
1105 terse_output = 1;
774a6177 1106 idx = optind;
c6ae0a5b 1107 break;
4785f995 1108 case 'h':
0ab8db89 1109 usage();
4785f995 1110 exit(0);
ebac4655
JA
1111 case 'v':
1112 printf("%s\n", fio_version_string);
1113 exit(0);
1114 }
1115 }
c9fad893 1116
972cfd25
JA
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 }
774a6177 1123
eb8bbf48
JA
1124 if (!f_out) {
1125 f_out = stdout;
1126 f_err = stderr;
1127 }
972cfd25
JA
1128
1129 return ini_idx;
ebac4655
JA
1130}
1131
1132static void free_shm(void)
1133{
1134 struct shmid_ds sbuf;
1135
1136 if (threads) {
2c0ecd28 1137 shmdt((void *) threads);
ebac4655
JA
1138 threads = NULL;
1139 shmctl(shm_id, IPC_RMID, &sbuf);
1140 }
1141}
1142
906c8d75
JA
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 */
ebac4655
JA
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 {
906c8d75 1155 size_t size = max_jobs * sizeof(struct thread_data);
ebac4655 1156
906c8d75 1157 shm_id = shmget(0, size, IPC_CREAT | 0600);
ebac4655
JA
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{
972cfd25
JA
1183 int job_files, i;
1184
ebac4655
JA
1185 if (setup_thread_area())
1186 return 1;
1187 if (fill_def_thread())
1188 return 1;
1189
972cfd25
JA
1190 job_files = parse_cmd_line(argc, argv);
1191 if (!job_files) {
1192 log_err("Need job file(s)\n");
0ab8db89 1193 usage();
ebac4655
JA
1194 return 1;
1195 }
1196
972cfd25
JA
1197 for (i = 0; i < job_files; i++) {
1198 if (fill_def_thread())
1199 return 1;
0c7e37a0 1200 if (parse_jobs_ini(ini_file[i], i))
972cfd25 1201 return 1;
88c6ed80 1202 free(ini_file[i]);
972cfd25 1203 }
ebac4655 1204
88c6ed80 1205 free(ini_file);
ebac4655
JA
1206 return 0;
1207}