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