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