net: support for unix domain sockets
[fio.git] / init.c
... / ...
CommitLineData
1/*
2 * This file contains job initialization and setup functions.
3 */
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>
11#include <sys/ipc.h>
12#include <sys/shm.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15
16#include "fio.h"
17#include "parse.h"
18#include "smalloc.h"
19#include "filehash.h"
20#include "verify.h"
21#include "profile.h"
22#include "server.h"
23
24#include "lib/getopt.h"
25
26#if FIO_PATCH > 0
27static char fio_version_string[] = __fio_stringify(FIO_MAJOR) "." \
28 __fio_stringify(FIO_MINOR) "." \
29 __fio_stringify(FIO_PATCH);
30#else
31static char fio_version_string[] = __fio_stringify(FIO_MAJOR) "." \
32 __fio_stringify(FIO_MINOR);
33#endif
34
35#define FIO_RANDSEED (0xb1899bedUL)
36
37static char **ini_file;
38static int max_jobs = FIO_MAX_JOBS;
39static int dump_cmdline;
40static int def_timeout;
41
42static struct thread_data def_thread;
43struct thread_data *threads = NULL;
44
45int exitall_on_terminate = 0;
46int terse_output = 0;
47int eta_print;
48unsigned long long mlock_size = 0;
49FILE *f_out = NULL;
50FILE *f_err = NULL;
51char **job_sections = NULL;
52int nr_job_sections = 0;
53char *exec_profile = NULL;
54int warnings_fatal = 0;
55int terse_version = 2;
56int is_backend = 0;
57int nr_clients = 0;
58int log_syslog = 0;
59
60int write_bw_log = 0;
61int read_only = 0;
62
63static int write_lat_log;
64
65static int prev_group_jobs;
66
67unsigned long fio_debug = 0;
68unsigned int fio_debug_jobno = -1;
69unsigned int *fio_debug_jobp = NULL;
70
71static char cmd_optstr[256];
72static int did_arg;
73
74const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN] = {
75 { .u.f = 1.0 },
76 { .u.f = 5.0 },
77 { .u.f = 10.0 },
78 { .u.f = 20.0 },
79 { .u.f = 30.0 },
80 { .u.f = 40.0 },
81 { .u.f = 50.0 },
82 { .u.f = 60.0 },
83 { .u.f = 70.0 },
84 { .u.f = 80.0 },
85 { .u.f = 90.0 },
86 { .u.f = 95.0 },
87 { .u.f = 99.0 },
88 { .u.f = 99.5 },
89 { .u.f = 99.9 },
90};
91
92#define FIO_CLIENT_FLAG (1 << 16)
93
94/*
95 * Command line options. These will contain the above, plus a few
96 * extra that only pertain to fio itself and not jobs.
97 */
98static struct option l_opts[FIO_NR_OPTIONS] = {
99 {
100 .name = (char *) "output",
101 .has_arg = required_argument,
102 .val = 'o' | FIO_CLIENT_FLAG,
103 },
104 {
105 .name = (char *) "timeout",
106 .has_arg = required_argument,
107 .val = 't' | FIO_CLIENT_FLAG,
108 },
109 {
110 .name = (char *) "latency-log",
111 .has_arg = required_argument,
112 .val = 'l' | FIO_CLIENT_FLAG,
113 },
114 {
115 .name = (char *) "bandwidth-log",
116 .has_arg = required_argument,
117 .val = 'b' | FIO_CLIENT_FLAG,
118 },
119 {
120 .name = (char *) "minimal",
121 .has_arg = optional_argument,
122 .val = 'm' | FIO_CLIENT_FLAG,
123 },
124 {
125 .name = (char *) "version",
126 .has_arg = no_argument,
127 .val = 'v' | FIO_CLIENT_FLAG,
128 },
129 {
130 .name = (char *) "help",
131 .has_arg = no_argument,
132 .val = 'h' | FIO_CLIENT_FLAG,
133 },
134 {
135 .name = (char *) "cmdhelp",
136 .has_arg = optional_argument,
137 .val = 'c' | FIO_CLIENT_FLAG,
138 },
139 {
140 .name = (char *) "showcmd",
141 .has_arg = no_argument,
142 .val = 's' | FIO_CLIENT_FLAG,
143 },
144 {
145 .name = (char *) "readonly",
146 .has_arg = no_argument,
147 .val = 'r' | FIO_CLIENT_FLAG,
148 },
149 {
150 .name = (char *) "eta",
151 .has_arg = required_argument,
152 .val = 'e' | FIO_CLIENT_FLAG,
153 },
154 {
155 .name = (char *) "debug",
156 .has_arg = required_argument,
157 .val = 'd' | FIO_CLIENT_FLAG,
158 },
159 {
160 .name = (char *) "section",
161 .has_arg = required_argument,
162 .val = 'x' | FIO_CLIENT_FLAG,
163 },
164 {
165 .name = (char *) "alloc-size",
166 .has_arg = required_argument,
167 .val = 'a' | FIO_CLIENT_FLAG,
168 },
169 {
170 .name = (char *) "profile",
171 .has_arg = required_argument,
172 .val = 'p' | FIO_CLIENT_FLAG,
173 },
174 {
175 .name = (char *) "warnings-fatal",
176 .has_arg = no_argument,
177 .val = 'w' | FIO_CLIENT_FLAG,
178 },
179 {
180 .name = (char *) "max-jobs",
181 .has_arg = required_argument,
182 .val = 'j' | FIO_CLIENT_FLAG,
183 },
184 {
185 .name = (char *) "terse-version",
186 .has_arg = required_argument,
187 .val = 'V' | FIO_CLIENT_FLAG,
188 },
189 {
190 .name = (char *) "server",
191 .has_arg = no_argument,
192 .val = 'S',
193 },
194 { .name = (char *) "daemonize",
195 .has_arg = no_argument,
196 .val = 'D',
197 },
198 {
199 .name = (char *) "net-port",
200 .has_arg = required_argument,
201 .val = 'P',
202 },
203 {
204 .name = (char *) "client",
205 .has_arg = required_argument,
206 .val = 'C',
207 },
208 {
209 .name = NULL,
210 },
211};
212
213static void free_shm(void)
214{
215 struct shmid_ds sbuf;
216
217 if (threads) {
218 void *tp = threads;
219
220 threads = NULL;
221 file_hash_exit();
222 fio_debug_jobp = NULL;
223 shmdt(tp);
224 shmctl(shm_id, IPC_RMID, &sbuf);
225 }
226
227 scleanup();
228}
229
230/*
231 * The thread area is shared between the main process and the job
232 * threads/processes. So setup a shared memory segment that will hold
233 * all the job info. We use the end of the region for keeping track of
234 * open files across jobs, for file sharing.
235 */
236static int setup_thread_area(void)
237{
238 void *hash;
239
240 if (threads)
241 return 0;
242
243 /*
244 * 1024 is too much on some machines, scale max_jobs if
245 * we get a failure that looks like too large a shm segment
246 */
247 do {
248 size_t size = max_jobs * sizeof(struct thread_data);
249
250 size += file_hash_size;
251 size += sizeof(unsigned int);
252
253 shm_id = shmget(0, size, IPC_CREAT | 0600);
254 if (shm_id != -1)
255 break;
256 if (errno != EINVAL) {
257 perror("shmget");
258 break;
259 }
260
261 max_jobs >>= 1;
262 } while (max_jobs);
263
264 if (shm_id == -1)
265 return 1;
266
267 threads = shmat(shm_id, NULL, 0);
268 if (threads == (void *) -1) {
269 perror("shmat");
270 return 1;
271 }
272
273 memset(threads, 0, max_jobs * sizeof(struct thread_data));
274 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
275 fio_debug_jobp = (void *) hash + file_hash_size;
276 *fio_debug_jobp = -1;
277 file_hash_init(hash);
278 return 0;
279}
280
281/*
282 * Return a free job structure.
283 */
284static struct thread_data *get_new_job(int global, struct thread_data *parent)
285{
286 struct thread_data *td;
287
288 if (global)
289 return &def_thread;
290 if (setup_thread_area()) {
291 log_err("error: failed to setup shm segment\n");
292 return NULL;
293 }
294 if (thread_number >= max_jobs) {
295 log_err("error: maximum number of jobs (%d) reached.\n",
296 max_jobs);
297 return NULL;
298 }
299
300 td = &threads[thread_number++];
301 *td = *parent;
302
303 td->o.uid = td->o.gid = -1U;
304
305 dup_files(td, parent);
306 options_mem_dupe(td);
307
308 profile_add_hooks(td);
309
310 td->thread_number = thread_number;
311 return td;
312}
313
314static void put_job(struct thread_data *td)
315{
316 if (td == &def_thread)
317 return;
318
319 profile_td_exit(td);
320
321 if (td->error)
322 log_info("fio: %s\n", td->verror);
323
324 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
325 thread_number--;
326}
327
328static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
329{
330 unsigned int bs = td->o.min_bs[ddir];
331 unsigned long long bytes_per_sec;
332
333 assert(ddir_rw(ddir));
334
335 if (td->o.rate[ddir])
336 bytes_per_sec = td->o.rate[ddir];
337 else
338 bytes_per_sec = td->o.rate_iops[ddir] * bs;
339
340 if (!bytes_per_sec) {
341 log_err("rate lower than supported\n");
342 return -1;
343 }
344
345 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec;
346 td->rate_pending_usleep[ddir] = 0;
347 return 0;
348}
349
350static int setup_rate(struct thread_data *td)
351{
352 int ret = 0;
353
354 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
355 ret = __setup_rate(td, DDIR_READ);
356 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
357 ret |= __setup_rate(td, DDIR_WRITE);
358
359 return ret;
360}
361
362static int fixed_block_size(struct thread_options *o)
363{
364 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
365 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
366 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
367}
368
369/*
370 * Lazy way of fixing up options that depend on each other. We could also
371 * define option callback handlers, but this is easier.
372 */
373static int fixup_options(struct thread_data *td)
374{
375 struct thread_options *o = &td->o;
376 int ret = 0;
377
378#ifndef FIO_HAVE_PSHARED_MUTEX
379 if (!o->use_thread) {
380 log_info("fio: this platform does not support process shared"
381 " mutexes, forcing use of threads. Use the 'thread'"
382 " option to get rid of this warning.\n");
383 o->use_thread = 1;
384 ret = warnings_fatal;
385 }
386#endif
387
388 if (o->write_iolog_file && o->read_iolog_file) {
389 log_err("fio: read iolog overrides write_iolog\n");
390 free(o->write_iolog_file);
391 o->write_iolog_file = NULL;
392 ret = warnings_fatal;
393 }
394
395 /*
396 * only really works for sequential io for now, and with 1 file
397 */
398 if (o->zone_size && td_random(td) && o->open_files == 1)
399 o->zone_size = 0;
400
401 /*
402 * Reads can do overwrites, we always need to pre-create the file
403 */
404 if (td_read(td) || td_rw(td))
405 o->overwrite = 1;
406
407 if (!o->min_bs[DDIR_READ])
408 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
409 if (!o->max_bs[DDIR_READ])
410 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
411 if (!o->min_bs[DDIR_WRITE])
412 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
413 if (!o->max_bs[DDIR_WRITE])
414 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
415
416 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
417
418 /*
419 * For random IO, allow blockalign offset other than min_bs.
420 */
421 if (!o->ba[DDIR_READ] || !td_random(td))
422 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
423 if (!o->ba[DDIR_WRITE] || !td_random(td))
424 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
425
426 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
427 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
428 !o->norandommap) {
429 log_err("fio: Any use of blockalign= turns off randommap\n");
430 o->norandommap = 1;
431 ret = warnings_fatal;
432 }
433
434 if (!o->file_size_high)
435 o->file_size_high = o->file_size_low;
436
437 if (o->norandommap && o->verify != VERIFY_NONE
438 && !fixed_block_size(o)) {
439 log_err("fio: norandommap given for variable block sizes, "
440 "verify disabled\n");
441 o->verify = VERIFY_NONE;
442 ret = warnings_fatal;
443 }
444 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
445 log_err("fio: bs_unaligned may not work with raw io\n");
446
447 /*
448 * thinktime_spin must be less than thinktime
449 */
450 if (o->thinktime_spin > o->thinktime)
451 o->thinktime_spin = o->thinktime;
452
453 /*
454 * The low water mark cannot be bigger than the iodepth
455 */
456 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
457 /*
458 * syslet work around - if the workload is sequential,
459 * we want to let the queue drain all the way down to
460 * avoid seeking between async threads
461 */
462 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
463 o->iodepth_low = 1;
464 else
465 o->iodepth_low = o->iodepth;
466 }
467
468 /*
469 * If batch number isn't set, default to the same as iodepth
470 */
471 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
472 o->iodepth_batch = o->iodepth;
473
474 if (o->nr_files > td->files_index)
475 o->nr_files = td->files_index;
476
477 if (o->open_files > o->nr_files || !o->open_files)
478 o->open_files = o->nr_files;
479
480 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
481 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
482 o->rate_iops_min[1]))) {
483 log_err("fio: rate and rate_iops are mutually exclusive\n");
484 ret = 1;
485 }
486 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
487 (o->rate_iops[0] < o->rate_iops_min[0]) ||
488 (o->rate_iops[1] < o->rate_iops_min[1])) {
489 log_err("fio: minimum rate exceeds rate\n");
490 ret = 1;
491 }
492
493 if (!o->timeout && o->time_based) {
494 log_err("fio: time_based requires a runtime/timeout setting\n");
495 o->time_based = 0;
496 ret = warnings_fatal;
497 }
498
499 if (o->fill_device && !o->size)
500 o->size = -1ULL;
501
502 if (o->verify != VERIFY_NONE) {
503 if (td_write(td) && o->do_verify && o->numjobs > 1) {
504 log_info("Multiple writers may overwrite blocks that "
505 "belong to other jobs. This can cause "
506 "verification failures.\n");
507 ret = warnings_fatal;
508 }
509
510 o->refill_buffers = 1;
511 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
512 !o->verify_interval)
513 o->verify_interval = o->min_bs[DDIR_WRITE];
514 }
515
516 if (o->pre_read) {
517 o->invalidate_cache = 0;
518 if (td->io_ops->flags & FIO_PIPEIO) {
519 log_info("fio: cannot pre-read files with an IO engine"
520 " that isn't seekable. Pre-read disabled.\n");
521 ret = warnings_fatal;
522 }
523 }
524
525#ifndef FIO_HAVE_FDATASYNC
526 if (o->fdatasync_blocks) {
527 log_info("fio: this platform does not support fdatasync()"
528 " falling back to using fsync(). Use the 'fsync'"
529 " option instead of 'fdatasync' to get rid of"
530 " this warning\n");
531 o->fsync_blocks = o->fdatasync_blocks;
532 o->fdatasync_blocks = 0;
533 ret = warnings_fatal;
534 }
535#endif
536
537 return ret;
538}
539
540/*
541 * This function leaks the buffer
542 */
543static char *to_kmg(unsigned int val)
544{
545 char *buf = malloc(32);
546 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
547 char *p = post;
548
549 do {
550 if (val & 1023)
551 break;
552
553 val >>= 10;
554 p++;
555 } while (*p);
556
557 snprintf(buf, 31, "%u%c", val, *p);
558 return buf;
559}
560
561/* External engines are specified by "external:name.o") */
562static const char *get_engine_name(const char *str)
563{
564 char *p = strstr(str, ":");
565
566 if (!p)
567 return str;
568
569 p++;
570 strip_blank_front(&p);
571 strip_blank_end(p);
572 return p;
573}
574
575static int exists_and_not_file(const char *filename)
576{
577 struct stat sb;
578
579 if (lstat(filename, &sb) == -1)
580 return 0;
581
582 /* \\.\ is the device namespace in Windows, where every file
583 * is a device node */
584 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
585 return 0;
586
587 return 1;
588}
589
590static void td_fill_rand_seeds_os(struct thread_data *td)
591{
592 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
593 os_random_seed(td->rand_seeds[1], &td->verify_state);
594 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
595
596 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
597 os_random_seed(td->rand_seeds[3], &td->next_file_state);
598
599 os_random_seed(td->rand_seeds[5], &td->file_size_state);
600 os_random_seed(td->rand_seeds[6], &td->trim_state);
601
602 if (!td_random(td))
603 return;
604
605 if (td->o.rand_repeatable)
606 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
607
608 os_random_seed(td->rand_seeds[4], &td->random_state);
609}
610
611static void td_fill_rand_seeds_internal(struct thread_data *td)
612{
613 init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]);
614 init_rand_seed(&td->__verify_state, td->rand_seeds[1]);
615 init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]);
616
617 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
618 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]);
619
620 init_rand_seed(&td->__file_size_state, td->rand_seeds[5]);
621 init_rand_seed(&td->__trim_state, td->rand_seeds[6]);
622
623 if (!td_random(td))
624 return;
625
626 if (td->o.rand_repeatable)
627 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
628
629 init_rand_seed(&td->__random_state, td->rand_seeds[4]);
630}
631
632void td_fill_rand_seeds(struct thread_data *td)
633{
634 if (td->o.use_os_rand)
635 td_fill_rand_seeds_os(td);
636 else
637 td_fill_rand_seeds_internal(td);
638
639 init_rand_seed(&td->buf_state, td->rand_seeds[7]);
640}
641
642/*
643 * Initialize the various random states we need (random io, block size ranges,
644 * read/write mix, etc).
645 */
646static int init_random_state(struct thread_data *td)
647{
648 int fd;
649
650 fd = open("/dev/urandom", O_RDONLY);
651 if (fd == -1) {
652 td_verror(td, errno, "open");
653 return 1;
654 }
655
656 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
657 (int) sizeof(td->rand_seeds)) {
658 td_verror(td, EIO, "read");
659 close(fd);
660 return 1;
661 }
662
663 close(fd);
664 td_fill_rand_seeds(td);
665 return 0;
666}
667
668/*
669 * Adds a job to the list of things todo. Sanitizes the various options
670 * to make sure we don't have conflicts, and initializes various
671 * members of td.
672 */
673static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
674{
675 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
676 "randread", "randwrite", "randrw" };
677 unsigned int i;
678 const char *engine;
679 char fname[PATH_MAX];
680 int numjobs, file_alloced;
681
682 /*
683 * the def_thread is just for options, it's not a real job
684 */
685 if (td == &def_thread)
686 return 0;
687
688 /*
689 * if we are just dumping the output command line, don't add the job
690 */
691 if (dump_cmdline) {
692 put_job(td);
693 return 0;
694 }
695
696 if (profile_td_init(td))
697 goto err;
698
699 engine = get_engine_name(td->o.ioengine);
700 td->io_ops = load_ioengine(td, engine);
701 if (!td->io_ops) {
702 log_err("fio: failed to load engine %s\n", engine);
703 goto err;
704 }
705
706 if (td->o.use_thread)
707 nr_thread++;
708 else
709 nr_process++;
710
711 if (td->o.odirect)
712 td->io_ops->flags |= FIO_RAWIO;
713
714 file_alloced = 0;
715 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
716 file_alloced = 1;
717
718 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
719 add_file(td, jobname);
720 else {
721 for (i = 0; i < td->o.nr_files; i++) {
722 sprintf(fname, "%s.%d.%d", jobname,
723 td->thread_number, i);
724 add_file(td, fname);
725 }
726 }
727 }
728
729 if (fixup_options(td))
730 goto err;
731
732 if (td->io_ops->flags & FIO_DISKLESSIO) {
733 struct fio_file *f;
734
735 for_each_file(td, f, i)
736 f->real_file_size = -1ULL;
737 }
738
739 td->mutex = fio_mutex_init(0);
740
741 td->ts.clat_percentiles = td->o.clat_percentiles;
742 if (td->o.overwrite_plist)
743 memcpy(td->ts.percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
744 else
745 memcpy(td->ts.percentile_list, def_percentile_list, sizeof(def_percentile_list));
746
747 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
748 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
749 td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX;
750 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
751 td->ddir_seq_nr = td->o.ddir_seq_nr;
752
753 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
754 prev_group_jobs = 0;
755 groupid++;
756 }
757
758 td->groupid = groupid;
759 prev_group_jobs++;
760
761 if (init_random_state(td))
762 goto err;
763
764 if (setup_rate(td))
765 goto err;
766
767 if (td->o.write_lat_log) {
768 setup_log(&td->lat_log);
769 setup_log(&td->slat_log);
770 setup_log(&td->clat_log);
771 }
772 if (td->o.write_bw_log)
773 setup_log(&td->bw_log);
774 if (td->o.write_iops_log)
775 setup_log(&td->iops_log);
776
777 if (!td->o.name)
778 td->o.name = strdup(jobname);
779
780 if (!terse_output) {
781 if (!job_add_num) {
782 if (!strcmp(td->io_ops->name, "cpuio")) {
783 log_info("%s: ioengine=cpu, cpuload=%u,"
784 " cpucycle=%u\n", td->o.name,
785 td->o.cpuload,
786 td->o.cpucycle);
787 } else {
788 char *c1, *c2, *c3, *c4;
789
790 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
791 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
792 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
793 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
794
795 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
796 " ioengine=%s, iodepth=%u\n",
797 td->o.name, td->groupid,
798 ddir_str[td->o.td_ddir],
799 c1, c2, c3, c4,
800 td->io_ops->name,
801 td->o.iodepth);
802
803 free(c1);
804 free(c2);
805 free(c3);
806 free(c4);
807 }
808 } else if (job_add_num == 1)
809 log_info("...\n");
810 }
811
812 /*
813 * recurse add identical jobs, clear numjobs and stonewall options
814 * as they don't apply to sub-jobs
815 */
816 numjobs = td->o.numjobs;
817 while (--numjobs) {
818 struct thread_data *td_new = get_new_job(0, td);
819
820 if (!td_new)
821 goto err;
822
823 td_new->o.numjobs = 1;
824 td_new->o.stonewall = 0;
825 td_new->o.new_group = 0;
826
827 if (file_alloced) {
828 td_new->o.filename = NULL;
829 td_new->files_index = 0;
830 td_new->files_size = 0;
831 td_new->files = NULL;
832 }
833
834 job_add_num = numjobs - 1;
835
836 if (add_job(td_new, jobname, job_add_num))
837 goto err;
838 }
839
840 return 0;
841err:
842 put_job(td);
843 return -1;
844}
845
846/*
847 * Parse as if 'o' was a command line
848 */
849void add_job_opts(const char **o)
850{
851 struct thread_data *td, *td_parent;
852 int i, in_global = 1;
853 char jobname[32];
854
855 i = 0;
856 td_parent = td = NULL;
857 while (o[i]) {
858 if (!strncmp(o[i], "name", 4)) {
859 in_global = 0;
860 if (td)
861 add_job(td, jobname, 0);
862 td = NULL;
863 sprintf(jobname, "%s", o[i] + 5);
864 }
865 if (in_global && !td_parent)
866 td_parent = get_new_job(1, &def_thread);
867 else if (!in_global && !td) {
868 if (!td_parent)
869 td_parent = &def_thread;
870 td = get_new_job(0, td_parent);
871 }
872 if (in_global)
873 fio_options_parse(td_parent, (char **) &o[i], 1);
874 else
875 fio_options_parse(td, (char **) &o[i], 1);
876 i++;
877 }
878
879 if (td)
880 add_job(td, jobname, 0);
881}
882
883static int skip_this_section(const char *name)
884{
885 int i;
886
887 if (!nr_job_sections)
888 return 0;
889 if (!strncmp(name, "global", 6))
890 return 0;
891
892 for (i = 0; i < nr_job_sections; i++)
893 if (!strcmp(job_sections[i], name))
894 return 0;
895
896 return 1;
897}
898
899static int is_empty_or_comment(char *line)
900{
901 unsigned int i;
902
903 for (i = 0; i < strlen(line); i++) {
904 if (line[i] == ';')
905 return 1;
906 if (line[i] == '#')
907 return 1;
908 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
909 return 0;
910 }
911
912 return 1;
913}
914
915/*
916 * This is our [ini] type file parser.
917 */
918int parse_jobs_ini(char *file, int is_buf, int stonewall_flag)
919{
920 unsigned int global;
921 struct thread_data *td;
922 char *string, *name;
923 FILE *f;
924 char *p;
925 int ret = 0, stonewall;
926 int first_sect = 1;
927 int skip_fgets = 0;
928 int inside_skip = 0;
929 char **opts;
930 int i, alloc_opts, num_opts;
931
932 if (is_buf)
933 f = NULL;
934 else {
935 if (!strcmp(file, "-"))
936 f = stdin;
937 else
938 f = fopen(file, "r");
939
940 if (!f) {
941 perror("fopen job file");
942 return 1;
943 }
944 }
945
946 string = malloc(4096);
947
948 /*
949 * it's really 256 + small bit, 280 should suffice
950 */
951 name = malloc(280);
952 memset(name, 0, 280);
953
954 alloc_opts = 8;
955 opts = malloc(sizeof(char *) * alloc_opts);
956 num_opts = 0;
957
958 stonewall = stonewall_flag;
959 do {
960 /*
961 * if skip_fgets is set, we already have loaded a line we
962 * haven't handled.
963 */
964 if (!skip_fgets) {
965 if (is_buf)
966 p = strsep(&file, "\n");
967 else
968 p = fgets(string, 4095, f);
969 if (!p)
970 break;
971 }
972
973 skip_fgets = 0;
974 strip_blank_front(&p);
975 strip_blank_end(p);
976
977 if (is_empty_or_comment(p))
978 continue;
979 if (sscanf(p, "[%255[^\n]]", name) != 1) {
980 if (inside_skip)
981 continue;
982 log_err("fio: option <%s> outside of [] job section\n",
983 p);
984 break;
985 }
986
987 name[strlen(name) - 1] = '\0';
988
989 if (skip_this_section(name)) {
990 inside_skip = 1;
991 continue;
992 } else
993 inside_skip = 0;
994
995 global = !strncmp(name, "global", 6);
996
997 if (dump_cmdline) {
998 if (first_sect)
999 log_info("fio ");
1000 if (!global)
1001 log_info("--name=%s ", name);
1002 first_sect = 0;
1003 }
1004
1005 td = get_new_job(global, &def_thread);
1006 if (!td) {
1007 ret = 1;
1008 break;
1009 }
1010
1011 /*
1012 * Seperate multiple job files by a stonewall
1013 */
1014 if (!global && stonewall) {
1015 td->o.stonewall = stonewall;
1016 stonewall = 0;
1017 }
1018
1019 num_opts = 0;
1020 memset(opts, 0, alloc_opts * sizeof(char *));
1021
1022 while (1) {
1023 if (is_buf)
1024 p = strsep(&file, "\n");
1025 else
1026 p = fgets(string, 4096, f);
1027 if (!p)
1028 break;
1029
1030 if (is_empty_or_comment(p))
1031 continue;
1032
1033 strip_blank_front(&p);
1034
1035 /*
1036 * new section, break out and make sure we don't
1037 * fgets() a new line at the top.
1038 */
1039 if (p[0] == '[') {
1040 skip_fgets = 1;
1041 break;
1042 }
1043
1044 strip_blank_end(p);
1045
1046 if (num_opts == alloc_opts) {
1047 alloc_opts <<= 1;
1048 opts = realloc(opts,
1049 alloc_opts * sizeof(char *));
1050 }
1051
1052 opts[num_opts] = strdup(p);
1053 num_opts++;
1054 }
1055
1056 ret = fio_options_parse(td, opts, num_opts);
1057 if (!ret) {
1058 if (dump_cmdline)
1059 for (i = 0; i < num_opts; i++)
1060 log_info("--%s ", opts[i]);
1061
1062 ret = add_job(td, name, 0);
1063 } else {
1064 log_err("fio: job %s dropped\n", name);
1065 put_job(td);
1066 }
1067
1068 for (i = 0; i < num_opts; i++)
1069 free(opts[i]);
1070 num_opts = 0;
1071 } while (!ret);
1072
1073 if (dump_cmdline)
1074 log_info("\n");
1075
1076 for (i = 0; i < num_opts; i++)
1077 free(opts[i]);
1078
1079 free(string);
1080 free(name);
1081 free(opts);
1082 if (!is_buf && f != stdin)
1083 fclose(f);
1084 return ret;
1085}
1086
1087static int fill_def_thread(void)
1088{
1089 memset(&def_thread, 0, sizeof(def_thread));
1090
1091 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1092 def_thread.o.timeout = def_timeout;
1093
1094 /*
1095 * fill default options
1096 */
1097 fio_fill_default_options(&def_thread);
1098 return 0;
1099}
1100
1101static void usage(const char *name)
1102{
1103 printf("fio %s\n", fio_version_string);
1104 printf("%s [options] [job options] <job file(s)>\n", name);
1105 printf("\t--debug=options\tEnable debug logging\n");
1106 printf("\t--output\tWrite output to file\n");
1107 printf("\t--timeout\tRuntime in seconds\n");
1108 printf("\t--latency-log\tGenerate per-job latency logs\n");
1109 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1110 printf("\t--minimal\tMinimal (terse) output\n");
1111 printf("\t--version\tPrint version info and exit\n");
1112 printf("\t--terse-version=x Terse version output format\n");
1113 printf("\t--help\t\tPrint this page\n");
1114 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
1115 " them\n");
1116 printf("\t--showcmd\tTurn a job file into command line options\n");
1117 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
1118 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
1119 printf("\t--readonly\tTurn on safety read-only checks, preventing"
1120 " writes\n");
1121 printf("\t--section=name\tOnly run specified section in job file\n");
1122 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
1123 " (def 1024)\n");
1124 printf("\t--warnings-fatal Fio parser warnings are fatal\n");
1125 printf("\t--max-jobs\tMaximum number of threads/processes to support\n");
1126 printf("\t--server\tStart a backend fio server\n");
1127 printf("\t--client=hostname Talk to remove backend fio server at hostname\n");
1128 printf("\t--net-port=port\tUse specified port for client/server connection\n");
1129 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1130 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
1131}
1132
1133#ifdef FIO_INC_DEBUG
1134struct debug_level debug_levels[] = {
1135 { .name = "process", .shift = FD_PROCESS, },
1136 { .name = "file", .shift = FD_FILE, },
1137 { .name = "io", .shift = FD_IO, },
1138 { .name = "mem", .shift = FD_MEM, },
1139 { .name = "blktrace", .shift = FD_BLKTRACE },
1140 { .name = "verify", .shift = FD_VERIFY },
1141 { .name = "random", .shift = FD_RANDOM },
1142 { .name = "parse", .shift = FD_PARSE },
1143 { .name = "diskutil", .shift = FD_DISKUTIL },
1144 { .name = "job", .shift = FD_JOB },
1145 { .name = "mutex", .shift = FD_MUTEX },
1146 { .name = "profile", .shift = FD_PROFILE },
1147 { .name = "time", .shift = FD_TIME },
1148 { .name = "net", .shift = FD_NET },
1149 { .name = NULL, },
1150};
1151
1152static int set_debug(const char *string)
1153{
1154 struct debug_level *dl;
1155 char *p = (char *) string;
1156 char *opt;
1157 int i;
1158
1159 if (!strcmp(string, "?") || !strcmp(string, "help")) {
1160 log_info("fio: dumping debug options:");
1161 for (i = 0; debug_levels[i].name; i++) {
1162 dl = &debug_levels[i];
1163 log_info("%s,", dl->name);
1164 }
1165 log_info("all\n");
1166 return 1;
1167 }
1168
1169 while ((opt = strsep(&p, ",")) != NULL) {
1170 int found = 0;
1171
1172 if (!strncmp(opt, "all", 3)) {
1173 log_info("fio: set all debug options\n");
1174 fio_debug = ~0UL;
1175 continue;
1176 }
1177
1178 for (i = 0; debug_levels[i].name; i++) {
1179 dl = &debug_levels[i];
1180 found = !strncmp(opt, dl->name, strlen(dl->name));
1181 if (!found)
1182 continue;
1183
1184 if (dl->shift == FD_JOB) {
1185 opt = strchr(opt, ':');
1186 if (!opt) {
1187 log_err("fio: missing job number\n");
1188 break;
1189 }
1190 opt++;
1191 fio_debug_jobno = atoi(opt);
1192 log_info("fio: set debug jobno %d\n",
1193 fio_debug_jobno);
1194 } else {
1195 log_info("fio: set debug option %s\n", opt);
1196 fio_debug |= (1UL << dl->shift);
1197 }
1198 break;
1199 }
1200
1201 if (!found)
1202 log_err("fio: debug mask %s not found\n", opt);
1203 }
1204 return 0;
1205}
1206#else
1207static int set_debug(const char *string)
1208{
1209 log_err("fio: debug tracing not included in build\n");
1210 return 1;
1211}
1212#endif
1213
1214static void fio_options_fill_optstring(void)
1215{
1216 char *ostr = cmd_optstr;
1217 int i, c;
1218
1219 c = i = 0;
1220 while (l_opts[i].name) {
1221 ostr[c++] = l_opts[i].val;
1222 if (l_opts[i].has_arg == required_argument)
1223 ostr[c++] = ':';
1224 else if (l_opts[i].has_arg == optional_argument) {
1225 ostr[c++] = ':';
1226 ostr[c++] = ':';
1227 }
1228 i++;
1229 }
1230 ostr[c] = '\0';
1231}
1232
1233static int client_flag_set(char c)
1234{
1235 int i;
1236
1237 i = 0;
1238 while (l_opts[i].name) {
1239 int val = l_opts[i].val;
1240
1241 if (c == (val & 0xff))
1242 return (val & FIO_CLIENT_FLAG);
1243
1244 i++;
1245 }
1246
1247 return 0;
1248}
1249
1250int parse_cmd_client(char *client, char *opt)
1251{
1252 return fio_client_add_cmd_option(client, opt);
1253}
1254
1255int parse_cmd_line(int argc, char *argv[])
1256{
1257 struct thread_data *td = NULL;
1258 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
1259 char *ostr = cmd_optstr;
1260 int daemonize_server = 0;
1261 char *cur_client = NULL;
1262 int backend = 0;
1263
1264 /*
1265 * Reset optind handling, since we may call this multiple times
1266 * for the backend.
1267 */
1268 optind = 1;
1269
1270 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1271 did_arg = 1;
1272
1273 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
1274 if (parse_cmd_client(cur_client, argv[optind - 1])) {
1275 exit_val = 1;
1276 do_exit++;
1277 break;
1278 }
1279 c &= ~FIO_CLIENT_FLAG;
1280 }
1281
1282 switch (c) {
1283 case 'a':
1284 smalloc_pool_size = atoi(optarg);
1285 break;
1286 case 't':
1287 def_timeout = atoi(optarg);
1288 break;
1289 case 'l':
1290 write_lat_log = 1;
1291 break;
1292 case 'b':
1293 write_bw_log = 1;
1294 break;
1295 case 'o':
1296 f_out = fopen(optarg, "w+");
1297 if (!f_out) {
1298 perror("fopen output");
1299 exit(1);
1300 }
1301 f_err = f_out;
1302 break;
1303 case 'm':
1304 terse_output = 1;
1305 break;
1306 case 'h':
1307 if (!cur_client) {
1308 usage(argv[0]);
1309 do_exit++;
1310 }
1311 break;
1312 case 'c':
1313 if (!cur_client) {
1314 fio_show_option_help(optarg);
1315 do_exit++;
1316 }
1317 break;
1318 case 's':
1319 dump_cmdline = 1;
1320 break;
1321 case 'r':
1322 read_only = 1;
1323 break;
1324 case 'v':
1325 if (!cur_client) {
1326 log_info("fio %s\n", fio_version_string);
1327 do_exit++;
1328 }
1329 break;
1330 case 'V':
1331 terse_version = atoi(optarg);
1332 if (terse_version != 2) {
1333 log_err("fio: bad terse version format\n");
1334 exit_val = 1;
1335 do_exit++;
1336 }
1337 break;
1338 case 'e':
1339 if (!strcmp("always", optarg))
1340 eta_print = FIO_ETA_ALWAYS;
1341 else if (!strcmp("never", optarg))
1342 eta_print = FIO_ETA_NEVER;
1343 break;
1344 case 'd':
1345 if (set_debug(optarg))
1346 do_exit++;
1347 break;
1348 case 'x': {
1349 size_t new_size;
1350
1351 if (!strcmp(optarg, "global")) {
1352 log_err("fio: can't use global as only "
1353 "section\n");
1354 do_exit++;
1355 exit_val = 1;
1356 break;
1357 }
1358 new_size = (nr_job_sections + 1) * sizeof(char *);
1359 job_sections = realloc(job_sections, new_size);
1360 job_sections[nr_job_sections] = strdup(optarg);
1361 nr_job_sections++;
1362 break;
1363 }
1364 case 'p':
1365 exec_profile = strdup(optarg);
1366 break;
1367 case FIO_GETOPT_JOB: {
1368 const char *opt = l_opts[lidx].name;
1369 char *val = optarg;
1370
1371 if (!strncmp(opt, "name", 4) && td) {
1372 ret = add_job(td, td->o.name ?: "fio", 0);
1373 if (ret)
1374 return 0;
1375 td = NULL;
1376 }
1377 if (!td) {
1378 int is_section = !strncmp(opt, "name", 4);
1379 int global = 0;
1380
1381 if (!is_section || !strncmp(val, "global", 6))
1382 global = 1;
1383
1384 if (is_section && skip_this_section(val))
1385 continue;
1386
1387 td = get_new_job(global, &def_thread);
1388 if (!td)
1389 return 0;
1390 }
1391
1392 ret = fio_cmd_option_parse(td, opt, val);
1393 break;
1394 }
1395 case 'w':
1396 warnings_fatal = 1;
1397 break;
1398 case 'j':
1399 max_jobs = atoi(optarg);
1400 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1401 log_err("fio: invalid max jobs: %d\n", max_jobs);
1402 do_exit++;
1403 exit_val = 1;
1404 }
1405 break;
1406 case 'S':
1407 if (nr_clients) {
1408 log_err("fio: can't be both client and server\n");
1409 do_exit++;
1410 exit_val = 1;
1411 break;
1412 }
1413 is_backend = 1;
1414 backend = 1;
1415 break;
1416 case 'D':
1417 daemonize_server = 1;
1418 break;
1419 case 'P':
1420 fio_net_port = atoi(optarg);
1421 break;
1422 case 'C':
1423 if (is_backend) {
1424 log_err("fio: can't be both client and server\n");
1425 do_exit++;
1426 exit_val = 1;
1427 break;
1428 }
1429 fio_client_add(optarg);
1430 cur_client = optarg;
1431 break;
1432 default:
1433 do_exit++;
1434 exit_val = 1;
1435 break;
1436 }
1437 if (do_exit)
1438 break;
1439 }
1440
1441 if (do_exit) {
1442 if (exit_val && !(is_backend || nr_clients))
1443 exit(exit_val);
1444 }
1445
1446 if (nr_clients && fio_clients_connect()) {
1447 do_exit++;
1448 exit_val = 1;
1449 return -1;
1450 }
1451
1452 if (is_backend && backend)
1453 return fio_start_server(daemonize_server);
1454
1455 if (td) {
1456 if (!ret)
1457 ret = add_job(td, td->o.name ?: "fio", 0);
1458 }
1459
1460 while (!ret && optind < argc) {
1461 ini_idx++;
1462 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1463 ini_file[ini_idx - 1] = strdup(argv[optind]);
1464 optind++;
1465 }
1466
1467 return ini_idx;
1468}
1469
1470int parse_options(int argc, char *argv[])
1471{
1472 int job_files, i;
1473
1474 f_out = stdout;
1475 f_err = stderr;
1476
1477 fio_options_fill_optstring();
1478 fio_options_dup_and_init(l_opts);
1479
1480 atexit(free_shm);
1481
1482 if (fill_def_thread())
1483 return 1;
1484
1485 job_files = parse_cmd_line(argc, argv);
1486
1487 if (job_files > 0) {
1488 for (i = 0; i < job_files; i++) {
1489 if (fill_def_thread())
1490 return 1;
1491 if (nr_clients) {
1492 if (fio_clients_send_ini(ini_file[i]))
1493 return 1;
1494 free(ini_file[i]);
1495 } else if (!is_backend) {
1496 if (parse_jobs_ini(ini_file[i], 0, i))
1497 return 1;
1498 free(ini_file[i]);
1499 }
1500 }
1501 }
1502
1503 free(ini_file);
1504 options_mem_free(&def_thread);
1505
1506 if (!thread_number) {
1507 if (dump_cmdline)
1508 return 0;
1509 if (exec_profile)
1510 return 0;
1511 if (is_backend || nr_clients)
1512 return 0;
1513 if (did_arg)
1514 return 0;
1515
1516 log_err("No jobs(s) defined\n\n");
1517
1518 if (!did_arg) {
1519 usage(argv[0]);
1520 return 1;
1521 }
1522
1523 return 0;
1524 }
1525
1526 if (def_thread.o.gtod_offload) {
1527 fio_gtod_init();
1528 fio_gtod_offload = 1;
1529 fio_gtod_cpu = def_thread.o.gtod_cpu;
1530 }
1531
1532 log_info("fio %s\n", fio_version_string);
1533 return 0;
1534}