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