[PATCH] Use mmap for mlocked memory
[fio.git] / fio-ini.c
CommitLineData
ebac4655
JA
1#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <fcntl.h>
5#include <ctype.h>
6#include <string.h>
7#include <errno.h>
8#include <sys/ipc.h>
9#include <sys/shm.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12
13#include "fio.h"
14
20dc95c4
JA
15#define DEF_BS (4096)
16#define DEF_TIMEOUT (0)
17#define DEF_RATE_CYCLE (1000)
18#define DEF_ODIRECT (1)
19#define DEF_IO_ENGINE (FIO_SYNCIO)
ebac4655 20#define DEF_IO_ENGINE_NAME "sync"
20dc95c4
JA
21#define DEF_SEQUENTIAL (1)
22#define DEF_RAND_REPEAT (1)
23#define DEF_OVERWRITE (1)
24#define DEF_CREATE (1)
25#define DEF_INVALIDATE (1)
26#define DEF_SYNCIO (0)
27#define DEF_RANDSEED (0xb1899bedUL)
28#define DEF_BWAVGTIME (500)
29#define DEF_CREATE_SER (1)
ebac4655 30#define DEF_CREATE_FSYNC (1)
20dc95c4
JA
31#define DEF_LOOPS (1)
32#define DEF_VERIFY (0)
33#define DEF_STONEWALL (0)
34#define DEF_NUMJOBS (1)
35#define DEF_USE_THREAD (0)
36#define DEF_FILE_SIZE (1024 * 1024 * 1024UL)
37#define DEF_ZONE_SIZE (0)
38#define DEF_ZONE_SKIP (0)
ebac4655 39
e8e387c1 40static char fio_version_string[] = "fio 1.3";
ebac4655
JA
41
42static int repeatable = DEF_RAND_REPEAT;
43static char *ini_file;
44static int max_jobs = MAX_JOBS;
45
46struct thread_data def_thread;
47struct thread_data *threads = NULL;
48
49int rate_quit = 0;
50int write_lat_log = 0;
51int write_bw_log = 0;
52int exitall_on_terminate = 0;
c04f7ec3 53unsigned long long mlock_size = 0;
ebac4655
JA
54
55static int setup_rate(struct thread_data *td)
56{
57 int nr_reads_per_sec;
58
59 if (!td->rate)
60 return 0;
61
62 if (td->rate < td->ratemin) {
63 fprintf(stderr, "min rate larger than nominal rate\n");
64 return -1;
65 }
66
67 nr_reads_per_sec = (td->rate * 1024) / td->min_bs;
68 td->rate_usec_cycle = 1000000 / nr_reads_per_sec;
69 td->rate_pending_usleep = 0;
70 return 0;
71}
72
73static void setup_log(struct io_log **log)
74{
75 struct io_log *l = malloc(sizeof(*l));
76
77 l->nr_samples = 0;
78 l->max_samples = 1024;
79 l->log = malloc(l->max_samples * sizeof(struct io_sample));
80 *log = l;
81}
82
83void finish_log(struct thread_data *td, struct io_log *log, const char *name)
84{
aea47d44 85 char file_name[256];
ebac4655
JA
86 FILE *f;
87 unsigned int i;
88
aea47d44 89 snprintf(file_name, 200, "client%d_%s.log", td->thread_number, name);
ebac4655
JA
90 f = fopen(file_name, "w");
91 if (!f) {
92 perror("fopen log");
93 return;
94 }
95
96 for (i = 0; i < log->nr_samples; i++)
97 fprintf(f, "%lu, %lu, %u\n", log->log[i].time, log->log[i].val, log->log[i].ddir);
98
99 fclose(f);
100 free(log->log);
101 free(log);
102}
103
104static struct thread_data *get_new_job(int global, struct thread_data *parent)
105{
106 struct thread_data *td;
107
108 if (global)
109 return &def_thread;
110 if (thread_number >= max_jobs)
111 return NULL;
112
113 td = &threads[thread_number++];
4ae3f763
JA
114 if (parent)
115 *td = *parent;
116 else
117 memset(td, 0, sizeof(*td));
ebac4655
JA
118
119 td->fd = -1;
120 td->thread_number = thread_number;
121
122 td->ddir = parent->ddir;
123 td->ioprio = parent->ioprio;
124 td->sequential = parent->sequential;
125 td->bs = parent->bs;
126 td->min_bs = parent->min_bs;
127 td->max_bs = parent->max_bs;
128 td->odirect = parent->odirect;
129 td->thinktime = parent->thinktime;
130 td->fsync_blocks = parent->fsync_blocks;
131 td->start_delay = parent->start_delay;
132 td->timeout = parent->timeout;
133 td->io_engine = parent->io_engine;
134 td->create_file = parent->create_file;
135 td->overwrite = parent->overwrite;
136 td->invalidate_cache = parent->invalidate_cache;
137 td->file_size = parent->file_size;
138 td->file_offset = parent->file_offset;
20dc95c4
JA
139 td->zone_size = parent->zone_size;
140 td->zone_skip = parent->zone_skip;
ebac4655
JA
141 td->rate = parent->rate;
142 td->ratemin = parent->ratemin;
143 td->ratecycle = parent->ratecycle;
144 td->iodepth = parent->iodepth;
145 td->sync_io = parent->sync_io;
146 td->mem_type = parent->mem_type;
147 td->bw_avg_time = parent->bw_avg_time;
148 td->create_serialize = parent->create_serialize;
149 td->create_fsync = parent->create_fsync;
150 td->loops = parent->loops;
151 td->verify = parent->verify;
152 td->stonewall = parent->stonewall;
153 td->numjobs = parent->numjobs;
154 td->use_thread = parent->use_thread;
155 td->do_disk_util = parent->do_disk_util;
156 memcpy(&td->cpumask, &parent->cpumask, sizeof(td->cpumask));
157 strcpy(td->io_engine_name, parent->io_engine_name);
158
159 return td;
160}
161
162static void put_job(struct thread_data *td)
163{
164 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
165 thread_number--;
166}
167
8756e4d4 168static int add_job(struct thread_data *td, const char *jobname)
ebac4655 169{
3d60d1ed
JA
170 char *ddir_str[] = { "read", "write", "randread", "randwrite",
171 "rw", NULL, "randrw" };
ebac4655
JA
172 struct stat sb;
173 int numjobs, ddir;
174
175#ifndef FIO_HAVE_LIBAIO
176 if (td->io_engine == FIO_LIBAIO) {
177 fprintf(stderr, "Linux libaio not available\n");
178 return 1;
179 }
180#endif
181#ifndef FIO_HAVE_POSIXAIO
182 if (td->io_engine == FIO_POSIXAIO) {
183 fprintf(stderr, "posix aio not available\n");
184 return 1;
185 }
186#endif
ebac4655
JA
187
188 /*
189 * the def_thread is just for options, it's not a real job
190 */
191 if (td == &def_thread)
192 return 0;
193
194 if (td->io_engine & FIO_SYNCIO)
195 td->iodepth = 1;
196 else {
197 if (!td->iodepth)
198 td->iodepth = 1;
199 }
200
20dc95c4
JA
201 /*
202 * only really works for sequential io for now
203 */
204 if (td->zone_size && !td->sequential)
205 td->zone_size = 0;
206
ebac4655 207 td->filetype = FIO_TYPE_FILE;
0af7b542
JA
208 if (!stat(jobname, &sb)) {
209 if (S_ISBLK(sb.st_mode))
210 td->filetype = FIO_TYPE_BD;
211 else if (S_ISCHR(sb.st_mode))
212 td->filetype = FIO_TYPE_CHAR;
213 }
ebac4655
JA
214
215 if (td->filetype == FIO_TYPE_FILE) {
216 if (td->directory[0] != '\0')
eba09dd3 217 sprintf(td->file_name, "%s/%s.%d", td->directory, jobname, td->jobnum);
ebac4655 218 else
eba09dd3 219 sprintf(td->file_name, "%s.%d", jobname, td->jobnum);
ebac4655 220 } else
aea47d44 221 strncpy(td->file_name, jobname, sizeof(td->file_name) - 1);
ebac4655
JA
222
223 sem_init(&td->mutex, 0, 0);
224
225 td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX;
226 td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX;
227 td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX;
228
229 if (td->min_bs == -1U)
230 td->min_bs = td->bs;
231 if (td->max_bs == -1U)
232 td->max_bs = td->bs;
3d60d1ed 233 if (td_read(td) && !td_rw(td))
ebac4655
JA
234 td->verify = 0;
235
236 if (td->stonewall && td->thread_number > 1)
237 groupid++;
238
239 td->groupid = groupid;
240
241 if (setup_rate(td))
242 goto err;
243
244 if (write_lat_log) {
245 setup_log(&td->slat_log);
246 setup_log(&td->clat_log);
247 }
248 if (write_bw_log)
249 setup_log(&td->bw_log);
250
3d60d1ed 251 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
8756e4d4 252 printf("Client%d (g=%d): rw=%s, odir=%d, bs=%d-%d, rate=%d, ioengine=%s, iodepth=%d\n", td->thread_number, td->groupid, ddir_str[ddir], td->odirect, td->min_bs, td->max_bs, td->rate, td->io_engine_name, td->iodepth);
ebac4655
JA
253
254 /*
255 * recurse add identical jobs, clear numjobs and stonewall options
256 * as they don't apply to sub-jobs
257 */
258 numjobs = td->numjobs;
259 while (--numjobs) {
260 struct thread_data *td_new = get_new_job(0, td);
261
262 if (!td_new)
263 goto err;
264
265 td_new->numjobs = 1;
266 td_new->stonewall = 0;
eba09dd3 267 td_new->jobnum = numjobs;
ebac4655 268
8756e4d4 269 if (add_job(td_new, jobname))
ebac4655
JA
270 goto err;
271 }
272 return 0;
273err:
274 put_job(td);
275 return -1;
276}
277
278int init_random_state(struct thread_data *td)
279{
280 unsigned long seed;
281 int fd, num_maps, blocks;
282
283 fd = open("/dev/random", O_RDONLY);
284 if (fd == -1) {
285 td_verror(td, errno);
286 return 1;
287 }
288
289 if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) {
290 td_verror(td, EIO);
291 close(fd);
292 return 1;
293 }
294
295 close(fd);
296
297 srand48_r(seed, &td->bsrange_state);
298 srand48_r(seed, &td->verify_state);
299
300 if (td->sequential)
301 return 0;
302
303 if (repeatable)
304 seed = DEF_RANDSEED;
305
306 blocks = (td->io_size + td->min_bs - 1) / td->min_bs;
307 num_maps = blocks / BLOCKS_PER_MAP;
308 td->file_map = malloc(num_maps * sizeof(long));
309 td->num_maps = num_maps;
310 memset(td->file_map, 0, num_maps * sizeof(long));
311
312 srand48_r(seed, &td->random_state);
313 return 0;
314}
315
316static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
317{
318#ifdef FIO_HAVE_CPU_AFFINITY
319 unsigned int i;
320
321 CPU_ZERO(&cpumask);
322
323 for (i = 0; i < sizeof(int) * 8; i++) {
324 if ((1 << i) & cpu)
325 CPU_SET(i, &cpumask);
326 }
327#endif
328}
329
330static unsigned long get_mult(char c)
331{
332 switch (c) {
333 case 'k':
334 case 'K':
335 return 1024;
336 case 'm':
337 case 'M':
338 return 1024 * 1024;
339 case 'g':
340 case 'G':
341 return 1024 * 1024 * 1024;
342 default:
343 return 1;
344 }
345}
346
347/*
348 * convert string after '=' into decimal value, noting any size suffix
349 */
350static int str_cnv(char *p, unsigned long long *val)
351{
352 char *str;
353 int len;
354
f5d3e5ec 355 str = strchr(p, '=');
ebac4655
JA
356 if (!str)
357 return 1;
358
359 str++;
360 len = strlen(str);
361
362 *val = strtoul(str, NULL, 10);
363 if (*val == ULONG_MAX && errno == ERANGE)
364 return 1;
365
9d0d4247 366 *val *= get_mult(str[len - 1]);
ebac4655
JA
367 return 0;
368}
369
370static int check_strcnv(char *p, char *name, unsigned long long *val)
371{
20dc95c4 372 if (strncmp(p, name, strlen(name) - 1))
ebac4655
JA
373 return 1;
374
375 return str_cnv(p, val);
376}
377
378static void strip_blank_front(char **p)
379{
380 char *s = *p;
381
4ae3f763 382 while (isspace(*s))
ebac4655
JA
383 s++;
384}
385
386static void strip_blank_end(char *p)
387{
4ae3f763 388 char *s = p + strlen(p) - 1;
ebac4655 389
4ae3f763
JA
390 while (isspace(*s) || iscntrl(*s))
391 s--;
aea47d44 392
4ae3f763 393 *(s + 1) = '\0';
aea47d44
JA
394}
395
ebac4655
JA
396typedef int (str_cb_fn)(struct thread_data *, char *);
397
398static int check_str(char *p, char *name, str_cb_fn *cb, struct thread_data *td)
399{
400 char *s = strstr(p, name);
401
402 if (!s)
403 return 1;
404
f5d3e5ec 405 s = strchr(s, '=');
ebac4655
JA
406 if (!s)
407 return 1;
408
409 s++;
410 strip_blank_front(&s);
411 return cb(td, s);
412}
413
414static int check_strstore(char *p, char *name, char *dest)
415{
416 char *s = strstr(p, name);
417
418 if (!s)
419 return 1;
420
f5d3e5ec 421 s = strchr(p, '=');
ebac4655
JA
422 if (!s)
423 return 1;
424
425 s++;
426 strip_blank_front(&s);
427
428 strcpy(dest, s);
ebac4655
JA
429 return 0;
430}
431
01617be6 432static int __check_range(char *str, unsigned long *val)
ebac4655 433{
01617be6 434 char suffix;
ebac4655 435
01617be6
JA
436 if (sscanf(str, "%lu%c", val, &suffix) == 2) {
437 *val *= get_mult(suffix);
ebac4655
JA
438 return 0;
439 }
440
01617be6 441 if (sscanf(str, "%lu", val) == 1)
ebac4655 442 return 0;
ebac4655 443
01617be6
JA
444 return 1;
445}
446
447static int check_range(char *p, char *name, unsigned long *s, unsigned long *e)
448{
449 char option[128];
450 char *str, *p1, *p2;
451
452 strcpy(option, p);
453 p = option;
454
455 str = strstr(p, name);
456 if (!str)
457 return 1;
ebac4655 458
01617be6
JA
459 p += strlen(name);
460
461 str = strchr(p, '=');
462 if (!str)
463 return 1;
464
465 /*
466 * 'p' now holds whatever is after the '=' sign
467 */
468 p1 = str + 1;
469
470 /*
471 * terminate p1 at the '-' sign
472 */
473 p = strchr(p1, '-');
474 if (!p)
475 return 1;
476
477 p2 = p + 1;
478 *p = '\0';
479
480 if (!__check_range(p1, s) && !__check_range(p2, e))
ebac4655
JA
481 return 0;
482
483 return 1;
ebac4655
JA
484}
485
486static int check_int(char *p, char *name, unsigned int *val)
487{
f5d3e5ec 488 char *str;
ebac4655 489
b6754f9d
JA
490 if (strncmp(p, name, strlen(name)))
491 return 1;
492
f5d3e5ec
JA
493 str = strstr(p, name);
494 if (!str)
495 return 1;
496
497 str = strchr(p, '=');
498 if (!str)
499 return 1;
500
501 str++;
ebac4655 502
f5d3e5ec 503 if (sscanf(str, "%u", val) == 1)
ebac4655
JA
504 return 0;
505
506 return 1;
507}
508
509static int check_strset(char *p, char *name)
510{
511 return strncmp(p, name, strlen(name));
512}
513
514static int is_empty_or_comment(char *line)
515{
516 unsigned int i;
517
518 for (i = 0; i < strlen(line); i++) {
519 if (line[i] == ';')
520 return 1;
521 if (!isspace(line[i]) && !iscntrl(line[i]))
522 return 0;
523 }
524
525 return 1;
526}
527
528static int str_rw_cb(struct thread_data *td, char *mem)
529{
530 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
531 td->ddir = DDIR_READ;
532 td->sequential = 1;
533 return 0;
534 } else if (!strncmp(mem, "randread", 8)) {
535 td->ddir = DDIR_READ;
536 td->sequential = 0;
537 return 0;
538 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
539 td->ddir = DDIR_WRITE;
540 td->sequential = 1;
541 return 0;
542 } else if (!strncmp(mem, "randwrite", 9)) {
543 td->ddir = DDIR_WRITE;
544 td->sequential = 0;
545 return 0;
3d60d1ed
JA
546 } else if (!strncmp(mem, "rw", 2)) {
547 td->ddir = 0;
548 td->iomix = 1;
549 td->sequential = 1;
550 return 0;
551 } else if (!strncmp(mem, "randrw", 6)) {
552 td->ddir = 0;
553 td->iomix = 1;
554 td->sequential = 0;
555 return 0;
ebac4655
JA
556 }
557
558 fprintf(stderr, "bad data direction: %s\n", mem);
559 return 1;
560}
561
562static int str_verify_cb(struct thread_data *td, char *mem)
563{
564 if (!strncmp(mem, "0", 1)) {
565 td->verify = VERIFY_NONE;
566 return 0;
567 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
568 td->verify = VERIFY_MD5;
569 return 0;
570 } else if (!strncmp(mem, "crc32", 5)) {
571 td->verify = VERIFY_CRC32;
572 return 0;
573 }
574
575 fprintf(stderr, "bad verify type: %s\n", mem);
576 return 1;
577}
578
579static int str_mem_cb(struct thread_data *td, char *mem)
580{
581 if (!strncmp(mem, "malloc", 6)) {
582 td->mem_type = MEM_MALLOC;
583 return 0;
584 } else if (!strncmp(mem, "shm", 3)) {
585 td->mem_type = MEM_SHM;
586 return 0;
587 } else if (!strncmp(mem, "mmap", 4)) {
588 td->mem_type = MEM_MMAP;
589 return 0;
590 }
591
592 fprintf(stderr, "bad mem type: %s\n", mem);
593 return 1;
594}
595
596static int str_ioengine_cb(struct thread_data *td, char *str)
597{
598 if (!strncmp(str, "linuxaio", 8) || !strncmp(str, "aio", 3) ||
599 !strncmp(str, "libaio", 6)) {
600 strcpy(td->io_engine_name, "libaio");
601 td->io_engine = FIO_LIBAIO;
602 return 0;
603 } else if (!strncmp(str, "posixaio", 8)) {
604 strcpy(td->io_engine_name, "posixaio");
605 td->io_engine = FIO_POSIXAIO;
606 return 0;
607 } else if (!strncmp(str, "sync", 4)) {
608 strcpy(td->io_engine_name, "sync");
609 td->io_engine = FIO_SYNCIO;
610 return 0;
611 } else if (!strncmp(str, "mmap", 4)) {
612 strcpy(td->io_engine_name, "mmap");
613 td->io_engine = FIO_MMAPIO;
614 return 0;
615 } else if (!strncmp(str, "sgio", 4)) {
616 strcpy(td->io_engine_name, "sgio");
617 td->io_engine = FIO_SGIO;
618 return 0;
8756e4d4
JA
619 } else if (!strncmp(str, "splice", 6)) {
620 strcpy(td->io_engine_name, "splice");
621 td->io_engine = FIO_SPLICEIO;
622 return 0;
ebac4655
JA
623 }
624
625 fprintf(stderr, "bad ioengine type: %s\n", str);
626 return 1;
627}
628
aea47d44
JA
629static int str_iolog_cb(struct thread_data *td, char *file)
630{
aea47d44
JA
631 strncpy(td->iolog_file, file, sizeof(td->iolog_file) - 1);
632
633 return 0;
634}
ebac4655
JA
635
636int parse_jobs_ini(char *file)
637{
638 unsigned int prioclass, prio, cpu, global;
639 unsigned long long ull;
640 unsigned long ul1, ul2;
641 struct thread_data *td;
642 char *string, *name;
643 fpos_t off;
644 FILE *f;
645 char *p;
646
647 f = fopen(file, "r");
648 if (!f) {
aea47d44 649 perror("fopen job file");
ebac4655
JA
650 return 1;
651 }
652
653 string = malloc(4096);
654 name = malloc(256);
655
656 while ((p = fgets(string, 4096, f)) != NULL) {
657 if (is_empty_or_comment(p))
658 continue;
659 if (sscanf(p, "[%s]", name) != 1)
660 continue;
661
662 global = !strncmp(name, "global", 6);
663
664 name[strlen(name) - 1] = '\0';
665
666 td = get_new_job(global, &def_thread);
667 if (!td)
668 return 1;
669
ebac4655
JA
670 fgetpos(f, &off);
671 while ((p = fgets(string, 4096, f)) != NULL) {
672 if (is_empty_or_comment(p))
673 continue;
674 if (strstr(p, "["))
675 break;
b6754f9d 676 strip_blank_front(&p);
4ae3f763 677 strip_blank_end(p);
aea47d44 678
ebac4655
JA
679 if (!check_int(p, "prio", &prio)) {
680#ifndef FIO_HAVE_IOPRIO
681 fprintf(stderr, "io priorities not available\n");
682 return 1;
683#endif
8756e4d4 684 td->ioprio |= prio;
ebac4655
JA
685 fgetpos(f, &off);
686 continue;
687 }
688 if (!check_int(p, "prioclass", &prioclass)) {
689#ifndef FIO_HAVE_IOPRIO
690 fprintf(stderr, "io priorities not available\n");
691 return 1;
692#endif
8756e4d4 693 td->ioprio |= prioclass << IOPRIO_CLASS_SHIFT;
ebac4655
JA
694 fgetpos(f, &off);
695 continue;
696 }
697 if (!check_int(p, "direct", &td->odirect)) {
698 fgetpos(f, &off);
699 continue;
700 }
701 if (!check_int(p, "rate", &td->rate)) {
702 fgetpos(f, &off);
703 continue;
704 }
705 if (!check_int(p, "ratemin", &td->ratemin)) {
706 fgetpos(f, &off);
707 continue;
708 }
709 if (!check_int(p, "ratecycle", &td->ratecycle)) {
710 fgetpos(f, &off);
711 continue;
712 }
713 if (!check_int(p, "thinktime", &td->thinktime)) {
714 fgetpos(f, &off);
715 continue;
716 }
717 if (!check_int(p, "cpumask", &cpu)) {
718#ifndef FIO_HAVE_CPU_AFFINITY
719 fprintf(stderr, "cpu affinity not available\n");
720 return 1;
721#endif
722 fill_cpu_mask(td->cpumask, cpu);
723 fgetpos(f, &off);
724 continue;
725 }
726 if (!check_int(p, "fsync", &td->fsync_blocks)) {
727 fgetpos(f, &off);
fc1a4713 728 td->end_fsync = 1;
ebac4655
JA
729 continue;
730 }
731 if (!check_int(p, "startdelay", &td->start_delay)) {
732 fgetpos(f, &off);
733 continue;
734 }
735 if (!check_int(p, "timeout", &td->timeout)) {
736 fgetpos(f, &off);
737 continue;
738 }
739 if (!check_int(p, "invalidate",&td->invalidate_cache)) {
740 fgetpos(f, &off);
741 continue;
742 }
743 if (!check_int(p, "iodepth", &td->iodepth)) {
744 fgetpos(f, &off);
745 continue;
746 }
747 if (!check_int(p, "sync", &td->sync_io)) {
748 fgetpos(f, &off);
749 continue;
750 }
751 if (!check_int(p, "bwavgtime", &td->bw_avg_time)) {
752 fgetpos(f, &off);
753 continue;
754 }
755 if (!check_int(p, "create_serialize", &td->create_serialize)) {
756 fgetpos(f, &off);
757 continue;
758 }
759 if (!check_int(p, "create_fsync", &td->create_fsync)) {
760 fgetpos(f, &off);
761 continue;
762 }
fc1a4713
JA
763 if (!check_int(p, "end_fsync", &td->end_fsync)) {
764 fgetpos(f, &off);
765 continue;
766 }
ebac4655
JA
767 if (!check_int(p, "loops", &td->loops)) {
768 fgetpos(f, &off);
769 continue;
770 }
771 if (!check_int(p, "numjobs", &td->numjobs)) {
772 fgetpos(f, &off);
773 continue;
774 }
775 if (!check_int(p, "overwrite", &td->overwrite)) {
776 fgetpos(f, &off);
777 continue;
778 }
779 if (!check_range(p, "bsrange", &ul1, &ul2)) {
780 if (ul1 > ul2) {
781 td->max_bs = ul1;
782 td->min_bs = ul2;
783 } else {
784 td->max_bs = ul2;
785 td->min_bs = ul1;
786 }
787 fgetpos(f, &off);
788 continue;
789 }
790 if (!check_strcnv(p, "bs", &ull)) {
791 td->bs = ull;
792 fgetpos(f, &off);
793 continue;
794 }
795 if (!check_strcnv(p, "size", &td->file_size)) {
796 fgetpos(f, &off);
797 continue;
798 }
799 if (!check_strcnv(p, "offset", &td->file_offset)) {
800 fgetpos(f, &off);
20dc95c4
JA
801 continue;
802 }
803 if (!check_strcnv(p, "zonesize", &td->zone_size)) {
804 fgetpos(f, &off);
805 continue;
806 }
807 if (!check_strcnv(p, "zoneskip", &td->zone_skip)) {
808 fgetpos(f, &off);
ebac4655
JA
809 continue;
810 }
c04f7ec3
JA
811 if (!check_strcnv(p, "lockmem", &mlock_size)) {
812 fgetpos(f, &off);
813 continue;
814 }
ebac4655
JA
815 if (!check_strstore(p, "directory", td->directory)) {
816 fgetpos(f, &off);
817 continue;
818 }
819 if (!check_str(p, "mem", str_mem_cb, td)) {
820 fgetpos(f, &off);
821 continue;
822 }
823 if (!check_str(p, "verify", str_verify_cb, td)) {
824 fgetpos(f, &off);
825 continue;
826 }
827 if (!check_str(p, "rw", str_rw_cb, td)) {
828 fgetpos(f, &off);
829 continue;
830 }
831 if (!check_str(p, "ioengine", str_ioengine_cb, td)) {
832 fgetpos(f, &off);
833 continue;
834 }
835 if (!check_strset(p, "create")) {
836 td->create_file = 1;
837 fgetpos(f, &off);
838 continue;
839 }
840 if (!check_strset(p, "exitall")) {
841 exitall_on_terminate = 1;
842 fgetpos(f, &off);
843 continue;
844 }
845 if (!check_strset(p, "stonewall")) {
846 td->stonewall = 1;
847 fgetpos(f, &off);
848 continue;
849 }
850 if (!check_strset(p, "thread")) {
851 td->use_thread = 1;
852 fgetpos(f, &off);
853 continue;
854 }
aea47d44
JA
855 if (!check_str(p, "iolog", str_iolog_cb, td)) {
856 td->iolog = 1;
857 fgetpos(f, &off);
858 continue;
859 }
ebac4655
JA
860
861 printf("Client%d: bad option %s\n",td->thread_number,p);
aea47d44 862 return 1;
ebac4655
JA
863 }
864 fsetpos(f, &off);
865
8756e4d4 866 if (add_job(td, name))
ebac4655
JA
867 return 1;
868 }
869
870 free(string);
871 free(name);
872 fclose(f);
873 return 0;
874}
875
876static int fill_def_thread(void)
877{
878 memset(&def_thread, 0, sizeof(def_thread));
879
880 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
881 perror("sched_getaffinity");
882 return 1;
883 }
884
885 /*
886 * fill globals
887 */
888 def_thread.ddir = DDIR_READ;
3d60d1ed 889 def_thread.iomix = 0;
ebac4655
JA
890 def_thread.bs = DEF_BS;
891 def_thread.min_bs = -1;
892 def_thread.max_bs = -1;
893 def_thread.io_engine = DEF_IO_ENGINE;
894 strcpy(def_thread.io_engine_name, DEF_IO_ENGINE_NAME);
895 def_thread.odirect = DEF_ODIRECT;
896 def_thread.ratecycle = DEF_RATE_CYCLE;
897 def_thread.sequential = DEF_SEQUENTIAL;
898 def_thread.timeout = DEF_TIMEOUT;
899 def_thread.create_file = DEF_CREATE;
900 def_thread.overwrite = DEF_OVERWRITE;
901 def_thread.invalidate_cache = DEF_INVALIDATE;
902 def_thread.sync_io = DEF_SYNCIO;
903 def_thread.mem_type = MEM_MALLOC;
904 def_thread.bw_avg_time = DEF_BWAVGTIME;
905 def_thread.create_serialize = DEF_CREATE_SER;
906 def_thread.create_fsync = DEF_CREATE_FSYNC;
907 def_thread.loops = DEF_LOOPS;
908 def_thread.verify = DEF_VERIFY;
909 def_thread.stonewall = DEF_STONEWALL;
910 def_thread.numjobs = DEF_NUMJOBS;
911 def_thread.use_thread = DEF_USE_THREAD;
912#ifdef FIO_HAVE_DISK_UTIL
913 def_thread.do_disk_util = 1;
914#endif
915
916 return 0;
917}
918
4785f995
JA
919static void usage(char *name)
920{
921 printf("%s\n", fio_version_string);
922 printf("\t-s IO is sequential\n");
923 printf("\t-b Block size in KiB for each IO\n");
924 printf("\t-t Runtime in seconds\n");
925 printf("\t-R Exit all threads on failure to meet rate goal\n");
926 printf("\t-o Use O_DIRECT\n");
927 printf("\t-l Generate per-job latency logs\n");
928 printf("\t-w Generate per-job bandwidth logs\n");
929 printf("\t-f Job file (Required)\n");
930 printf("\t-v Print version info and exit\n");
931}
932
ebac4655
JA
933static void parse_cmd_line(int argc, char *argv[])
934{
935 int c;
936
4785f995 937 while ((c = getopt(argc, argv, "s:b:t:r:R:o:f:lwvh")) != EOF) {
ebac4655
JA
938 switch (c) {
939 case 's':
940 def_thread.sequential = !!atoi(optarg);
941 break;
942 case 'b':
943 def_thread.bs = atoi(optarg);
944 def_thread.bs <<= 10;
945 if (!def_thread.bs) {
946 printf("bad block size\n");
947 def_thread.bs = DEF_BS;
948 }
949 break;
950 case 't':
951 def_thread.timeout = atoi(optarg);
952 break;
953 case 'r':
954 repeatable = !!atoi(optarg);
955 break;
956 case 'R':
957 rate_quit = !!atoi(optarg);
958 break;
959 case 'o':
960 def_thread.odirect = !!atoi(optarg);
961 break;
962 case 'f':
963 ini_file = strdup(optarg);
964 break;
965 case 'l':
966 write_lat_log = 1;
967 break;
968 case 'w':
969 write_bw_log = 1;
970 break;
4785f995
JA
971 case 'h':
972 usage(argv[0]);
973 exit(0);
ebac4655
JA
974 case 'v':
975 printf("%s\n", fio_version_string);
976 exit(0);
977 }
978 }
c9fad893
JA
979
980 if (!ini_file && argc > 1 && argv[argc - 1][0] != '-')
981 ini_file = strdup(argv[argc - 1]);
ebac4655
JA
982}
983
984static void free_shm(void)
985{
986 struct shmid_ds sbuf;
987
988 if (threads) {
989 shmdt(threads);
990 threads = NULL;
991 shmctl(shm_id, IPC_RMID, &sbuf);
992 }
993}
994
995static int setup_thread_area(void)
996{
997 /*
998 * 1024 is too much on some machines, scale max_jobs if
999 * we get a failure that looks like too large a shm segment
1000 */
1001 do {
1002 int s = max_jobs * sizeof(struct thread_data);
1003
1004 shm_id = shmget(0, s, IPC_CREAT | 0600);
1005 if (shm_id != -1)
1006 break;
1007 if (errno != EINVAL) {
1008 perror("shmget");
1009 break;
1010 }
1011
1012 max_jobs >>= 1;
1013 } while (max_jobs);
1014
1015 if (shm_id == -1)
1016 return 1;
1017
1018 threads = shmat(shm_id, NULL, 0);
1019 if (threads == (void *) -1) {
1020 perror("shmat");
1021 return 1;
1022 }
1023
1024 atexit(free_shm);
1025 return 0;
1026}
1027
1028int parse_options(int argc, char *argv[])
1029{
1030 if (setup_thread_area())
1031 return 1;
1032 if (fill_def_thread())
1033 return 1;
1034
1035 parse_cmd_line(argc, argv);
1036
1037 if (!ini_file) {
1038 printf("Need job file\n");
4785f995 1039 usage(argv[0]);
ebac4655
JA
1040 return 1;
1041 }
1042
c9fad893
JA
1043 if (parse_jobs_ini(ini_file)) {
1044 usage(argv[0]);
ebac4655 1045 return 1;
c9fad893 1046 }
ebac4655
JA
1047
1048 return 0;
1049}