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