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