Add exitall_on_error option
[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/types.h>
13#include <sys/stat.h>
14
15#include "fio.h"
16#ifndef FIO_NO_HAVE_SHM_H
17#include <sys/shm.h>
18#endif
19
20#include "parse.h"
21#include "smalloc.h"
22#include "filehash.h"
23#include "verify.h"
24#include "profile.h"
25#include "server.h"
26#include "idletime.h"
27#include "filelock.h"
28
29#include "lib/getopt.h"
30#include "oslib/strcasestr.h"
31
32#include "crc/test.h"
33
34const char fio_version_string[] = FIO_VERSION;
35
36#define FIO_RANDSEED (0xb1899bedUL)
37
38static char **ini_file;
39static int max_jobs = FIO_MAX_JOBS;
40static int dump_cmdline;
41static long long def_timeout;
42static int parse_only;
43
44static struct thread_data def_thread;
45struct thread_data *threads = NULL;
46static char **job_sections;
47static int nr_job_sections;
48
49int exitall_on_terminate = 0;
50int exitall_on_terminate_error = 0;
51int output_format = FIO_OUTPUT_NORMAL;
52int eta_print = FIO_ETA_AUTO;
53int eta_new_line = 0;
54FILE *f_out = NULL;
55FILE *f_err = NULL;
56char *exec_profile = NULL;
57int warnings_fatal = 0;
58int terse_version = 3;
59int is_backend = 0;
60int nr_clients = 0;
61int log_syslog = 0;
62
63int write_bw_log = 0;
64int read_only = 0;
65int status_interval = 0;
66
67char *trigger_file = NULL;
68long long trigger_timeout = 0;
69char *trigger_cmd = NULL;
70char *trigger_remote_cmd = NULL;
71
72char *aux_path = NULL;
73
74static int prev_group_jobs;
75
76unsigned long fio_debug = 0;
77unsigned int fio_debug_jobno = -1;
78unsigned int *fio_debug_jobp = NULL;
79
80static char cmd_optstr[256];
81static int did_arg;
82
83#define FIO_CLIENT_FLAG (1 << 16)
84
85/*
86 * Command line options. These will contain the above, plus a few
87 * extra that only pertain to fio itself and not jobs.
88 */
89static struct option l_opts[FIO_NR_OPTIONS] = {
90 {
91 .name = (char *) "output",
92 .has_arg = required_argument,
93 .val = 'o' | FIO_CLIENT_FLAG,
94 },
95 {
96 .name = (char *) "timeout",
97 .has_arg = required_argument,
98 .val = 't' | FIO_CLIENT_FLAG,
99 },
100 {
101 .name = (char *) "latency-log",
102 .has_arg = required_argument,
103 .val = 'l' | FIO_CLIENT_FLAG,
104 },
105 {
106 .name = (char *) "bandwidth-log",
107 .has_arg = required_argument,
108 .val = 'b' | FIO_CLIENT_FLAG,
109 },
110 {
111 .name = (char *) "minimal",
112 .has_arg = no_argument,
113 .val = 'm' | FIO_CLIENT_FLAG,
114 },
115 {
116 .name = (char *) "output-format",
117 .has_arg = optional_argument,
118 .val = 'F' | FIO_CLIENT_FLAG,
119 },
120 {
121 .name = (char *) "append-terse",
122 .has_arg = optional_argument,
123 .val = 'f',
124 },
125 {
126 .name = (char *) "version",
127 .has_arg = no_argument,
128 .val = 'v' | FIO_CLIENT_FLAG,
129 },
130 {
131 .name = (char *) "help",
132 .has_arg = no_argument,
133 .val = 'h' | FIO_CLIENT_FLAG,
134 },
135 {
136 .name = (char *) "cmdhelp",
137 .has_arg = optional_argument,
138 .val = 'c' | FIO_CLIENT_FLAG,
139 },
140 {
141 .name = (char *) "enghelp",
142 .has_arg = optional_argument,
143 .val = 'i' | FIO_CLIENT_FLAG,
144 },
145 {
146 .name = (char *) "showcmd",
147 .has_arg = no_argument,
148 .val = 's' | FIO_CLIENT_FLAG,
149 },
150 {
151 .name = (char *) "readonly",
152 .has_arg = no_argument,
153 .val = 'r' | FIO_CLIENT_FLAG,
154 },
155 {
156 .name = (char *) "eta",
157 .has_arg = required_argument,
158 .val = 'e' | FIO_CLIENT_FLAG,
159 },
160 {
161 .name = (char *) "eta-newline",
162 .has_arg = required_argument,
163 .val = 'E' | FIO_CLIENT_FLAG,
164 },
165 {
166 .name = (char *) "debug",
167 .has_arg = required_argument,
168 .val = 'd' | FIO_CLIENT_FLAG,
169 },
170 {
171 .name = (char *) "parse-only",
172 .has_arg = no_argument,
173 .val = 'P' | FIO_CLIENT_FLAG,
174 },
175 {
176 .name = (char *) "section",
177 .has_arg = required_argument,
178 .val = 'x' | FIO_CLIENT_FLAG,
179 },
180#ifdef CONFIG_ZLIB
181 {
182 .name = (char *) "inflate-log",
183 .has_arg = required_argument,
184 .val = 'X' | FIO_CLIENT_FLAG,
185 },
186#endif
187 {
188 .name = (char *) "alloc-size",
189 .has_arg = required_argument,
190 .val = 'a' | FIO_CLIENT_FLAG,
191 },
192 {
193 .name = (char *) "profile",
194 .has_arg = required_argument,
195 .val = 'p' | FIO_CLIENT_FLAG,
196 },
197 {
198 .name = (char *) "warnings-fatal",
199 .has_arg = no_argument,
200 .val = 'w' | FIO_CLIENT_FLAG,
201 },
202 {
203 .name = (char *) "max-jobs",
204 .has_arg = required_argument,
205 .val = 'j' | FIO_CLIENT_FLAG,
206 },
207 {
208 .name = (char *) "terse-version",
209 .has_arg = required_argument,
210 .val = 'V' | FIO_CLIENT_FLAG,
211 },
212 {
213 .name = (char *) "server",
214 .has_arg = optional_argument,
215 .val = 'S',
216 },
217 { .name = (char *) "daemonize",
218 .has_arg = required_argument,
219 .val = 'D',
220 },
221 {
222 .name = (char *) "client",
223 .has_arg = required_argument,
224 .val = 'C',
225 },
226 {
227 .name = (char *) "remote-config",
228 .has_arg = required_argument,
229 .val = 'R',
230 },
231 {
232 .name = (char *) "cpuclock-test",
233 .has_arg = no_argument,
234 .val = 'T',
235 },
236 {
237 .name = (char *) "crctest",
238 .has_arg = optional_argument,
239 .val = 'G',
240 },
241 {
242 .name = (char *) "idle-prof",
243 .has_arg = required_argument,
244 .val = 'I',
245 },
246 {
247 .name = (char *) "status-interval",
248 .has_arg = required_argument,
249 .val = 'L',
250 },
251 {
252 .name = (char *) "trigger-file",
253 .has_arg = required_argument,
254 .val = 'W',
255 },
256 {
257 .name = (char *) "trigger-timeout",
258 .has_arg = required_argument,
259 .val = 'B',
260 },
261 {
262 .name = (char *) "trigger",
263 .has_arg = required_argument,
264 .val = 'H',
265 },
266 {
267 .name = (char *) "trigger-remote",
268 .has_arg = required_argument,
269 .val = 'J',
270 },
271 {
272 .name = (char *) "aux-path",
273 .has_arg = required_argument,
274 .val = 'K',
275 },
276 {
277 .name = NULL,
278 },
279};
280
281void free_threads_shm(void)
282{
283 if (threads) {
284 void *tp = threads;
285#ifndef CONFIG_NO_SHM
286 struct shmid_ds sbuf;
287
288 threads = NULL;
289 shmdt(tp);
290 shmctl(shm_id, IPC_RMID, &sbuf);
291 shm_id = -1;
292#else
293 threads = NULL;
294 free(tp);
295#endif
296 }
297}
298
299static void free_shm(void)
300{
301 if (threads) {
302 file_hash_exit();
303 flow_exit();
304 fio_debug_jobp = NULL;
305 free_threads_shm();
306 }
307
308 free(trigger_file);
309 free(trigger_cmd);
310 free(trigger_remote_cmd);
311 trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
312
313 options_free(fio_options, &def_thread);
314 fio_filelock_exit();
315 scleanup();
316}
317
318/*
319 * The thread area is shared between the main process and the job
320 * threads/processes. So setup a shared memory segment that will hold
321 * all the job info. We use the end of the region for keeping track of
322 * open files across jobs, for file sharing.
323 */
324static int setup_thread_area(void)
325{
326 void *hash;
327
328 if (threads)
329 return 0;
330
331 /*
332 * 1024 is too much on some machines, scale max_jobs if
333 * we get a failure that looks like too large a shm segment
334 */
335 do {
336 size_t size = max_jobs * sizeof(struct thread_data);
337
338 size += file_hash_size;
339 size += sizeof(unsigned int);
340
341#ifndef CONFIG_NO_SHM
342 shm_id = shmget(0, size, IPC_CREAT | 0600);
343 if (shm_id != -1)
344 break;
345 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
346 perror("shmget");
347 break;
348 }
349#else
350 threads = malloc(size);
351 if (threads)
352 break;
353#endif
354
355 max_jobs >>= 1;
356 } while (max_jobs);
357
358#ifndef CONFIG_NO_SHM
359 if (shm_id == -1)
360 return 1;
361
362 threads = shmat(shm_id, NULL, 0);
363 if (threads == (void *) -1) {
364 perror("shmat");
365 return 1;
366 }
367#endif
368
369 memset(threads, 0, max_jobs * sizeof(struct thread_data));
370 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
371 fio_debug_jobp = (void *) hash + file_hash_size;
372 *fio_debug_jobp = -1;
373 file_hash_init(hash);
374
375 flow_init();
376
377 return 0;
378}
379
380static void set_cmd_options(struct thread_data *td)
381{
382 struct thread_options *o = &td->o;
383
384 if (!o->timeout)
385 o->timeout = def_timeout;
386}
387
388/*
389 * Return a free job structure.
390 */
391static struct thread_data *get_new_job(int global, struct thread_data *parent,
392 int preserve_eo, const char *jobname)
393{
394 struct thread_data *td;
395
396 if (global) {
397 set_cmd_options(&def_thread);
398 return &def_thread;
399 }
400 if (setup_thread_area()) {
401 log_err("error: failed to setup shm segment\n");
402 return NULL;
403 }
404 if (thread_number >= max_jobs) {
405 log_err("error: maximum number of jobs (%d) reached.\n",
406 max_jobs);
407 return NULL;
408 }
409
410 td = &threads[thread_number++];
411 *td = *parent;
412
413 td->io_ops = NULL;
414 if (!preserve_eo)
415 td->eo = NULL;
416
417 td->o.uid = td->o.gid = -1U;
418
419 dup_files(td, parent);
420 fio_options_mem_dupe(td);
421
422 profile_add_hooks(td);
423
424 td->thread_number = thread_number;
425 td->subjob_number = 0;
426
427 if (jobname)
428 td->o.name = strdup(jobname);
429
430 if (!parent->o.group_reporting)
431 stat_number++;
432
433 set_cmd_options(td);
434 return td;
435}
436
437static void put_job(struct thread_data *td)
438{
439 if (td == &def_thread)
440 return;
441
442 profile_td_exit(td);
443 flow_exit_job(td);
444
445 if (td->error)
446 log_info("fio: %s\n", td->verror);
447
448 fio_options_free(td);
449 if (td->io_ops)
450 free_ioengine(td);
451
452 if (td->o.name)
453 free(td->o.name);
454
455 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
456 thread_number--;
457}
458
459static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
460{
461 unsigned int bs = td->o.min_bs[ddir];
462
463 assert(ddir_rw(ddir));
464
465 if (td->o.rate[ddir])
466 td->rate_bps[ddir] = td->o.rate[ddir];
467 else
468 td->rate_bps[ddir] = (uint64_t) td->o.rate_iops[ddir] * bs;
469
470 if (!td->rate_bps[ddir]) {
471 log_err("rate lower than supported\n");
472 return -1;
473 }
474
475 td->rate_next_io_time[ddir] = 0;
476 td->rate_io_issue_bytes[ddir] = 0;
477 td->last_usec = 0;
478 return 0;
479}
480
481static int setup_rate(struct thread_data *td)
482{
483 int ret = 0;
484
485 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
486 ret = __setup_rate(td, DDIR_READ);
487 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
488 ret |= __setup_rate(td, DDIR_WRITE);
489 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
490 ret |= __setup_rate(td, DDIR_TRIM);
491
492 return ret;
493}
494
495static int fixed_block_size(struct thread_options *o)
496{
497 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
498 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
499 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
500 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
501 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
502}
503
504
505static unsigned long long get_rand_start_delay(struct thread_data *td)
506{
507 unsigned long long delayrange;
508 uint64_t frand_max;
509 unsigned long r;
510
511 delayrange = td->o.start_delay_high - td->o.start_delay;
512
513 frand_max = rand_max(&td->delay_state);
514 r = __rand(&td->delay_state);
515 delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
516
517 delayrange += td->o.start_delay;
518 return delayrange;
519}
520
521/*
522 * Lazy way of fixing up options that depend on each other. We could also
523 * define option callback handlers, but this is easier.
524 */
525static int fixup_options(struct thread_data *td)
526{
527 struct thread_options *o = &td->o;
528 int ret = 0;
529
530#ifndef FIO_HAVE_PSHARED_MUTEX
531 if (!o->use_thread) {
532 log_info("fio: this platform does not support process shared"
533 " mutexes, forcing use of threads. Use the 'thread'"
534 " option to get rid of this warning.\n");
535 o->use_thread = 1;
536 ret = warnings_fatal;
537 }
538#endif
539
540 if (o->write_iolog_file && o->read_iolog_file) {
541 log_err("fio: read iolog overrides write_iolog\n");
542 free(o->write_iolog_file);
543 o->write_iolog_file = NULL;
544 ret = warnings_fatal;
545 }
546
547 /*
548 * only really works with 1 file
549 */
550 if (o->zone_size && o->open_files > 1)
551 o->zone_size = 0;
552
553 /*
554 * If zone_range isn't specified, backward compatibility dictates it
555 * should be made equal to zone_size.
556 */
557 if (o->zone_size && !o->zone_range)
558 o->zone_range = o->zone_size;
559
560 /*
561 * Reads can do overwrites, we always need to pre-create the file
562 */
563 if (td_read(td) || td_rw(td))
564 o->overwrite = 1;
565
566 if (!o->min_bs[DDIR_READ])
567 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
568 if (!o->max_bs[DDIR_READ])
569 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
570 if (!o->min_bs[DDIR_WRITE])
571 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
572 if (!o->max_bs[DDIR_WRITE])
573 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
574 if (!o->min_bs[DDIR_TRIM])
575 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
576 if (!o->max_bs[DDIR_TRIM])
577 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
578
579 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
580 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
581
582 /*
583 * For random IO, allow blockalign offset other than min_bs.
584 */
585 if (!o->ba[DDIR_READ] || !td_random(td))
586 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
587 if (!o->ba[DDIR_WRITE] || !td_random(td))
588 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
589 if (!o->ba[DDIR_TRIM] || !td_random(td))
590 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
591
592 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
593 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
594 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
595 !o->norandommap) {
596 log_err("fio: Any use of blockalign= turns off randommap\n");
597 o->norandommap = 1;
598 ret = warnings_fatal;
599 }
600
601 if (!o->file_size_high)
602 o->file_size_high = o->file_size_low;
603
604 if (o->start_delay_high)
605 o->start_delay = get_rand_start_delay(td);
606
607 if (o->norandommap && o->verify != VERIFY_NONE
608 && !fixed_block_size(o)) {
609 log_err("fio: norandommap given for variable block sizes, "
610 "verify limited\n");
611 ret = warnings_fatal;
612 }
613 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
614 log_err("fio: bs_unaligned may not work with raw io\n");
615
616 /*
617 * thinktime_spin must be less than thinktime
618 */
619 if (o->thinktime_spin > o->thinktime)
620 o->thinktime_spin = o->thinktime;
621
622 /*
623 * The low water mark cannot be bigger than the iodepth
624 */
625 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
626 o->iodepth_low = o->iodepth;
627
628 /*
629 * If batch number isn't set, default to the same as iodepth
630 */
631 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
632 o->iodepth_batch = o->iodepth;
633
634 /*
635 * If max batch complete number isn't set or set incorrectly,
636 * default to the same as iodepth_batch_complete_min
637 */
638 if (o->iodepth_batch_complete_min > o->iodepth_batch_complete_max)
639 o->iodepth_batch_complete_max = o->iodepth_batch_complete_min;
640
641 if (o->nr_files > td->files_index)
642 o->nr_files = td->files_index;
643
644 if (o->open_files > o->nr_files || !o->open_files)
645 o->open_files = o->nr_files;
646
647 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
648 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
649 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
650 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
651 log_err("fio: rate and rate_iops are mutually exclusive\n");
652 ret = 1;
653 }
654 if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
655 (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
656 (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
657 (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
658 (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
659 (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
660 log_err("fio: minimum rate exceeds rate\n");
661 ret = 1;
662 }
663
664 if (!o->timeout && o->time_based) {
665 log_err("fio: time_based requires a runtime/timeout setting\n");
666 o->time_based = 0;
667 ret = warnings_fatal;
668 }
669
670 if (o->fill_device && !o->size)
671 o->size = -1ULL;
672
673 if (o->verify != VERIFY_NONE) {
674 if (td_write(td) && o->do_verify && o->numjobs > 1) {
675 log_info("Multiple writers may overwrite blocks that "
676 "belong to other jobs. This can cause "
677 "verification failures.\n");
678 ret = warnings_fatal;
679 }
680
681 if (!fio_option_is_set(o, refill_buffers))
682 o->refill_buffers = 1;
683
684 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
685 !o->verify_interval)
686 o->verify_interval = o->min_bs[DDIR_WRITE];
687
688 /*
689 * Verify interval must be smaller or equal to the
690 * write size.
691 */
692 if (o->verify_interval > o->min_bs[DDIR_WRITE])
693 o->verify_interval = o->min_bs[DDIR_WRITE];
694 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
695 o->verify_interval = o->min_bs[DDIR_READ];
696 }
697
698 if (o->pre_read) {
699 o->invalidate_cache = 0;
700 if (td->io_ops->flags & FIO_PIPEIO) {
701 log_info("fio: cannot pre-read files with an IO engine"
702 " that isn't seekable. Pre-read disabled.\n");
703 ret = warnings_fatal;
704 }
705 }
706
707 if (!o->unit_base) {
708 if (td->io_ops->flags & FIO_BIT_BASED)
709 o->unit_base = 1;
710 else
711 o->unit_base = 8;
712 }
713
714#ifndef CONFIG_FDATASYNC
715 if (o->fdatasync_blocks) {
716 log_info("fio: this platform does not support fdatasync()"
717 " falling back to using fsync(). Use the 'fsync'"
718 " option instead of 'fdatasync' to get rid of"
719 " this warning\n");
720 o->fsync_blocks = o->fdatasync_blocks;
721 o->fdatasync_blocks = 0;
722 ret = warnings_fatal;
723 }
724#endif
725
726#ifdef WIN32
727 /*
728 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
729 * so fail if we're passed those flags
730 */
731 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
732 log_err("fio: Windows does not support direct or non-buffered io with"
733 " the synchronous ioengines. Use the 'windowsaio' ioengine"
734 " with 'direct=1' and 'iodepth=1' instead.\n");
735 ret = 1;
736 }
737#endif
738
739 /*
740 * For fully compressible data, just zero them at init time.
741 * It's faster than repeatedly filling it. For non-zero
742 * compression, we should have refill_buffers set. Set it, unless
743 * the job file already changed it.
744 */
745 if (o->compress_percentage) {
746 if (o->compress_percentage == 100) {
747 o->zero_buffers = 1;
748 o->compress_percentage = 0;
749 } else if (!fio_option_is_set(o, refill_buffers))
750 o->refill_buffers = 1;
751 }
752
753 /*
754 * Using a non-uniform random distribution excludes usage of
755 * a random map
756 */
757 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
758 td->o.norandommap = 1;
759
760 /*
761 * If size is set but less than the min block size, complain
762 */
763 if (o->size && o->size < td_min_bs(td)) {
764 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
765 ret = 1;
766 }
767
768 /*
769 * O_ATOMIC implies O_DIRECT
770 */
771 if (td->o.oatomic)
772 td->o.odirect = 1;
773
774 /*
775 * If randseed is set, that overrides randrepeat
776 */
777 if (fio_option_is_set(&td->o, rand_seed))
778 td->o.rand_repeatable = 0;
779
780 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
781 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
782 ret = 1;
783 }
784
785 if (fio_option_is_set(o, gtod_cpu)) {
786 fio_gtod_init();
787 fio_gtod_set_cpu(o->gtod_cpu);
788 fio_gtod_offload = 1;
789 }
790
791 td->loops = o->loops;
792 if (!td->loops)
793 td->loops = 1;
794
795 if (td->o.block_error_hist && td->o.nr_files != 1) {
796 log_err("fio: block error histogram only available with "
797 "with a single file per job, but %d files "
798 "provided\n", td->o.nr_files);
799 ret = 1;
800 }
801
802 return ret;
803}
804
805/*
806 * This function leaks the buffer
807 */
808char *fio_uint_to_kmg(unsigned int val)
809{
810 char *buf = malloc(32);
811 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
812 char *p = post;
813
814 do {
815 if (val & 1023)
816 break;
817
818 val >>= 10;
819 p++;
820 } while (*p);
821
822 snprintf(buf, 32, "%u%c", val, *p);
823 return buf;
824}
825
826/* External engines are specified by "external:name.o") */
827static const char *get_engine_name(const char *str)
828{
829 char *p = strstr(str, ":");
830
831 if (!p)
832 return str;
833
834 p++;
835 strip_blank_front(&p);
836 strip_blank_end(p);
837 return p;
838}
839
840static int exists_and_not_file(const char *filename)
841{
842 struct stat sb;
843
844 if (lstat(filename, &sb) == -1)
845 return 0;
846
847 /* \\.\ is the device namespace in Windows, where every file
848 * is a device node */
849 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
850 return 0;
851
852 return 1;
853}
854
855static void td_fill_rand_seeds_internal(struct thread_data *td, int use64)
856{
857 init_rand_seed(&td->bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF], use64);
858 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF], use64);
859 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], use64);
860
861 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
862 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
863
864 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
865 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
866 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
867 init_rand_seed(&td->poisson_state, td->rand_seeds[FIO_RAND_POISSON_OFF], 0);
868
869 if (!td_random(td))
870 return;
871
872 if (td->o.rand_repeatable)
873 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
874
875 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
876 init_rand_seed(&td->seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], use64);
877 init_rand_seed(&td->seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], use64);
878 init_rand_seed(&td->seq_rand_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], use64);
879}
880
881void td_fill_rand_seeds(struct thread_data *td)
882{
883 int use64;
884
885 if (td->o.allrand_repeatable) {
886 unsigned int i;
887
888 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
889 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
890 + i;
891 }
892
893 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
894 use64 = 1;
895 else
896 use64 = 0;
897
898 td_fill_rand_seeds_internal(td, use64);
899
900 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
901 frand_copy(&td->buf_state_prev, &td->buf_state);
902
903 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], use64);
904}
905
906/*
907 * Initializes the ioengine configured for a job, if it has not been done so
908 * already.
909 */
910int ioengine_load(struct thread_data *td)
911{
912 const char *engine;
913
914 /*
915 * Engine has already been loaded.
916 */
917 if (td->io_ops)
918 return 0;
919 if (!td->o.ioengine) {
920 log_err("fio: internal fault, no IO engine specified\n");
921 return 1;
922 }
923
924 engine = get_engine_name(td->o.ioengine);
925 td->io_ops = load_ioengine(td, engine);
926 if (!td->io_ops) {
927 log_err("fio: failed to load engine %s\n", engine);
928 return 1;
929 }
930
931 if (td->io_ops->option_struct_size && td->io_ops->options) {
932 /*
933 * In cases where td->eo is set, clone it for a child thread.
934 * This requires that the parent thread has the same ioengine,
935 * but that requirement must be enforced by the code which
936 * cloned the thread.
937 */
938 void *origeo = td->eo;
939 /*
940 * Otherwise use the default thread options.
941 */
942 if (!origeo && td != &def_thread && def_thread.eo &&
943 def_thread.io_ops->options == td->io_ops->options)
944 origeo = def_thread.eo;
945
946 options_init(td->io_ops->options);
947 td->eo = malloc(td->io_ops->option_struct_size);
948 /*
949 * Use the default thread as an option template if this uses the
950 * same options structure and there are non-default options
951 * used.
952 */
953 if (origeo) {
954 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
955 options_mem_dupe(td->eo, td->io_ops->options);
956 } else {
957 memset(td->eo, 0, td->io_ops->option_struct_size);
958 fill_default_options(td->eo, td->io_ops->options);
959 }
960 *(struct thread_data **)td->eo = td;
961 }
962
963 return 0;
964}
965
966static void init_flags(struct thread_data *td)
967{
968 struct thread_options *o = &td->o;
969
970 if (o->verify_backlog)
971 td->flags |= TD_F_VER_BACKLOG;
972 if (o->trim_backlog)
973 td->flags |= TD_F_TRIM_BACKLOG;
974 if (o->read_iolog_file)
975 td->flags |= TD_F_READ_IOLOG;
976 if (o->refill_buffers)
977 td->flags |= TD_F_REFILL_BUFFERS;
978 /*
979 * Always scramble buffers if asked to
980 */
981 if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
982 td->flags |= TD_F_SCRAMBLE_BUFFERS;
983 /*
984 * But also scramble buffers, unless we were explicitly asked
985 * to zero them.
986 */
987 if (o->scramble_buffers && !(o->zero_buffers &&
988 fio_option_is_set(o, zero_buffers)))
989 td->flags |= TD_F_SCRAMBLE_BUFFERS;
990 if (o->verify != VERIFY_NONE)
991 td->flags |= TD_F_VER_NONE;
992
993 if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
994 td->flags |= TD_F_NEED_LOCK;
995}
996
997static int setup_random_seeds(struct thread_data *td)
998{
999 unsigned long seed;
1000 unsigned int i;
1001
1002 if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed))
1003 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
1004
1005 seed = td->o.rand_seed;
1006 for (i = 0; i < 4; i++)
1007 seed *= 0x9e370001UL;
1008
1009 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
1010 td->rand_seeds[i] = seed;
1011 seed *= 0x9e370001UL;
1012 }
1013
1014 td_fill_rand_seeds(td);
1015 return 0;
1016}
1017
1018enum {
1019 FPRE_NONE = 0,
1020 FPRE_JOBNAME,
1021 FPRE_JOBNUM,
1022 FPRE_FILENUM
1023};
1024
1025static struct fpre_keyword {
1026 const char *keyword;
1027 size_t strlen;
1028 int key;
1029} fpre_keywords[] = {
1030 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
1031 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
1032 { .keyword = "$filenum", .key = FPRE_FILENUM, },
1033 { .keyword = NULL, },
1034 };
1035
1036static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
1037 const char *jobname, int jobnum, int filenum)
1038{
1039 struct fpre_keyword *f;
1040 char copy[PATH_MAX];
1041 size_t dst_left = PATH_MAX - 1;
1042
1043 if (!o->filename_format || !strlen(o->filename_format)) {
1044 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
1045 return NULL;
1046 }
1047
1048 for (f = &fpre_keywords[0]; f->keyword; f++)
1049 f->strlen = strlen(f->keyword);
1050
1051 buf[buf_size - 1] = '\0';
1052 strncpy(buf, o->filename_format, buf_size - 1);
1053
1054 memset(copy, 0, sizeof(copy));
1055 for (f = &fpre_keywords[0]; f->keyword; f++) {
1056 do {
1057 size_t pre_len, post_start = 0;
1058 char *str, *dst = copy;
1059
1060 str = strcasestr(buf, f->keyword);
1061 if (!str)
1062 break;
1063
1064 pre_len = str - buf;
1065 if (strlen(str) != f->strlen)
1066 post_start = pre_len + f->strlen;
1067
1068 if (pre_len) {
1069 strncpy(dst, buf, pre_len);
1070 dst += pre_len;
1071 dst_left -= pre_len;
1072 }
1073
1074 switch (f->key) {
1075 case FPRE_JOBNAME: {
1076 int ret;
1077
1078 ret = snprintf(dst, dst_left, "%s", jobname);
1079 if (ret < 0)
1080 break;
1081 else if (ret > dst_left) {
1082 log_err("fio: truncated filename\n");
1083 dst += dst_left;
1084 dst_left = 0;
1085 } else {
1086 dst += ret;
1087 dst_left -= ret;
1088 }
1089 break;
1090 }
1091 case FPRE_JOBNUM: {
1092 int ret;
1093
1094 ret = snprintf(dst, dst_left, "%d", jobnum);
1095 if (ret < 0)
1096 break;
1097 else if (ret > dst_left) {
1098 log_err("fio: truncated filename\n");
1099 dst += dst_left;
1100 dst_left = 0;
1101 } else {
1102 dst += ret;
1103 dst_left -= ret;
1104 }
1105 break;
1106 }
1107 case FPRE_FILENUM: {
1108 int ret;
1109
1110 ret = snprintf(dst, dst_left, "%d", filenum);
1111 if (ret < 0)
1112 break;
1113 else if (ret > dst_left) {
1114 log_err("fio: truncated filename\n");
1115 dst += dst_left;
1116 dst_left = 0;
1117 } else {
1118 dst += ret;
1119 dst_left -= ret;
1120 }
1121 break;
1122 }
1123 default:
1124 assert(0);
1125 break;
1126 }
1127
1128 if (post_start)
1129 strncpy(dst, buf + post_start, dst_left);
1130
1131 strncpy(buf, copy, buf_size - 1);
1132 } while (1);
1133 }
1134
1135 return buf;
1136}
1137
1138int parse_dryrun(void)
1139{
1140 return dump_cmdline || parse_only;
1141}
1142
1143static void gen_log_name(char *name, size_t size, const char *logtype,
1144 const char *logname, unsigned int num,
1145 const char *suf, int per_job)
1146{
1147 if (per_job)
1148 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1149 else
1150 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1151}
1152
1153/*
1154 * Adds a job to the list of things todo. Sanitizes the various options
1155 * to make sure we don't have conflicts, and initializes various
1156 * members of td.
1157 */
1158static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
1159 int recursed, int client_type)
1160{
1161 unsigned int i;
1162 char fname[PATH_MAX];
1163 int numjobs, file_alloced;
1164 struct thread_options *o = &td->o;
1165 char logname[PATH_MAX + 32];
1166
1167 /*
1168 * the def_thread is just for options, it's not a real job
1169 */
1170 if (td == &def_thread)
1171 return 0;
1172
1173 init_flags(td);
1174
1175 /*
1176 * if we are just dumping the output command line, don't add the job
1177 */
1178 if (parse_dryrun()) {
1179 put_job(td);
1180 return 0;
1181 }
1182
1183 td->client_type = client_type;
1184
1185 if (profile_td_init(td))
1186 goto err;
1187
1188 if (ioengine_load(td))
1189 goto err;
1190
1191 if (o->odirect)
1192 td->io_ops->flags |= FIO_RAWIO;
1193
1194 file_alloced = 0;
1195 if (!o->filename && !td->files_index && !o->read_iolog_file) {
1196 file_alloced = 1;
1197
1198 if (o->nr_files == 1 && exists_and_not_file(jobname))
1199 add_file(td, jobname, job_add_num, 0);
1200 else {
1201 for (i = 0; i < o->nr_files; i++)
1202 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
1203 }
1204 }
1205
1206 if (fixup_options(td))
1207 goto err;
1208
1209 flow_init_job(td);
1210
1211 /*
1212 * IO engines only need this for option callbacks, and the address may
1213 * change in subprocesses.
1214 */
1215 if (td->eo)
1216 *(struct thread_data **)td->eo = NULL;
1217
1218 if (td->io_ops->flags & FIO_DISKLESSIO) {
1219 struct fio_file *f;
1220
1221 for_each_file(td, f, i)
1222 f->real_file_size = -1ULL;
1223 }
1224
1225 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
1226
1227 td->ts.clat_percentiles = o->clat_percentiles;
1228 td->ts.percentile_precision = o->percentile_precision;
1229 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
1230
1231 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1232 td->ts.clat_stat[i].min_val = ULONG_MAX;
1233 td->ts.slat_stat[i].min_val = ULONG_MAX;
1234 td->ts.lat_stat[i].min_val = ULONG_MAX;
1235 td->ts.bw_stat[i].min_val = ULONG_MAX;
1236 }
1237 td->ddir_seq_nr = o->ddir_seq_nr;
1238
1239 if ((o->stonewall || o->new_group) && prev_group_jobs) {
1240 prev_group_jobs = 0;
1241 groupid++;
1242 if (groupid == INT_MAX) {
1243 log_err("fio: too many groups defined\n");
1244 goto err;
1245 }
1246 }
1247
1248 td->groupid = groupid;
1249 prev_group_jobs++;
1250
1251 if (setup_random_seeds(td)) {
1252 td_verror(td, errno, "init_random_state");
1253 goto err;
1254 }
1255
1256 if (setup_rate(td))
1257 goto err;
1258
1259 if (o->lat_log_file) {
1260 struct log_params p = {
1261 .td = td,
1262 .avg_msec = o->log_avg_msec,
1263 .log_type = IO_LOG_TYPE_LAT,
1264 .log_offset = o->log_offset,
1265 .log_gz = o->log_gz,
1266 .log_gz_store = o->log_gz_store,
1267 };
1268 const char *suf;
1269
1270 if (p.log_gz_store)
1271 suf = "log.fz";
1272 else
1273 suf = "log";
1274
1275 gen_log_name(logname, sizeof(logname), "lat", o->lat_log_file,
1276 td->thread_number, suf, o->per_job_logs);
1277 setup_log(&td->lat_log, &p, logname);
1278
1279 gen_log_name(logname, sizeof(logname), "slat", o->lat_log_file,
1280 td->thread_number, suf, o->per_job_logs);
1281 setup_log(&td->slat_log, &p, logname);
1282
1283 gen_log_name(logname, sizeof(logname), "clat", o->lat_log_file,
1284 td->thread_number, suf, o->per_job_logs);
1285 setup_log(&td->clat_log, &p, logname);
1286 }
1287 if (o->bw_log_file) {
1288 struct log_params p = {
1289 .td = td,
1290 .avg_msec = o->log_avg_msec,
1291 .log_type = IO_LOG_TYPE_BW,
1292 .log_offset = o->log_offset,
1293 .log_gz = o->log_gz,
1294 .log_gz_store = o->log_gz_store,
1295 };
1296 const char *suf;
1297
1298 if (p.log_gz_store)
1299 suf = "log.fz";
1300 else
1301 suf = "log";
1302
1303 gen_log_name(logname, sizeof(logname), "bw", o->bw_log_file,
1304 td->thread_number, suf, o->per_job_logs);
1305 setup_log(&td->bw_log, &p, logname);
1306 }
1307 if (o->iops_log_file) {
1308 struct log_params p = {
1309 .td = td,
1310 .avg_msec = o->log_avg_msec,
1311 .log_type = IO_LOG_TYPE_IOPS,
1312 .log_offset = o->log_offset,
1313 .log_gz = o->log_gz,
1314 .log_gz_store = o->log_gz_store,
1315 };
1316 const char *suf;
1317
1318 if (p.log_gz_store)
1319 suf = "log.fz";
1320 else
1321 suf = "log";
1322
1323 gen_log_name(logname, sizeof(logname), "iops", o->iops_log_file,
1324 td->thread_number, suf, o->per_job_logs);
1325 setup_log(&td->iops_log, &p, logname);
1326 }
1327
1328 if (!o->name)
1329 o->name = strdup(jobname);
1330
1331 if (output_format & FIO_OUTPUT_NORMAL) {
1332 if (!job_add_num) {
1333 if (is_backend && !recursed)
1334 fio_server_send_add_job(td);
1335
1336 if (!(td->io_ops->flags & FIO_NOIO)) {
1337 char *c1, *c2, *c3, *c4;
1338 char *c5 = NULL, *c6 = NULL;
1339
1340 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1341 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1342 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1343 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1344
1345 if (!o->bs_is_seq_rand) {
1346 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1347 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1348 }
1349
1350 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1351 td->groupid,
1352 ddir_str(o->td_ddir));
1353
1354 if (o->bs_is_seq_rand)
1355 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1356 c1, c2, c3, c4);
1357 else
1358 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1359 c1, c2, c3, c4, c5, c6);
1360
1361 log_info("ioengine=%s, iodepth=%u\n",
1362 td->io_ops->name, o->iodepth);
1363
1364 free(c1);
1365 free(c2);
1366 free(c3);
1367 free(c4);
1368 free(c5);
1369 free(c6);
1370 }
1371 } else if (job_add_num == 1)
1372 log_info("...\n");
1373 }
1374
1375 /*
1376 * recurse add identical jobs, clear numjobs and stonewall options
1377 * as they don't apply to sub-jobs
1378 */
1379 numjobs = o->numjobs;
1380 while (--numjobs) {
1381 struct thread_data *td_new = get_new_job(0, td, 1, jobname);
1382
1383 if (!td_new)
1384 goto err;
1385
1386 td_new->o.numjobs = 1;
1387 td_new->o.stonewall = 0;
1388 td_new->o.new_group = 0;
1389 td_new->subjob_number = numjobs;
1390
1391 if (file_alloced) {
1392 if (td_new->files) {
1393 struct fio_file *f;
1394 for_each_file(td_new, f, i) {
1395 if (f->file_name)
1396 sfree(f->file_name);
1397 sfree(f);
1398 }
1399 free(td_new->files);
1400 td_new->files = NULL;
1401 }
1402 td_new->files_index = 0;
1403 td_new->files_size = 0;
1404 if (td_new->o.filename) {
1405 free(td_new->o.filename);
1406 td_new->o.filename = NULL;
1407 }
1408 }
1409
1410 if (add_job(td_new, jobname, numjobs, 1, client_type))
1411 goto err;
1412 }
1413
1414 return 0;
1415err:
1416 put_job(td);
1417 return -1;
1418}
1419
1420/*
1421 * Parse as if 'o' was a command line
1422 */
1423void add_job_opts(const char **o, int client_type)
1424{
1425 struct thread_data *td, *td_parent;
1426 int i, in_global = 1;
1427 char jobname[32];
1428
1429 i = 0;
1430 td_parent = td = NULL;
1431 while (o[i]) {
1432 if (!strncmp(o[i], "name", 4)) {
1433 in_global = 0;
1434 if (td)
1435 add_job(td, jobname, 0, 0, client_type);
1436 td = NULL;
1437 sprintf(jobname, "%s", o[i] + 5);
1438 }
1439 if (in_global && !td_parent)
1440 td_parent = get_new_job(1, &def_thread, 0, jobname);
1441 else if (!in_global && !td) {
1442 if (!td_parent)
1443 td_parent = &def_thread;
1444 td = get_new_job(0, td_parent, 0, jobname);
1445 }
1446 if (in_global)
1447 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
1448 else
1449 fio_options_parse(td, (char **) &o[i], 1, 0);
1450 i++;
1451 }
1452
1453 if (td)
1454 add_job(td, jobname, 0, 0, client_type);
1455}
1456
1457static int skip_this_section(const char *name)
1458{
1459 int i;
1460
1461 if (!nr_job_sections)
1462 return 0;
1463 if (!strncmp(name, "global", 6))
1464 return 0;
1465
1466 for (i = 0; i < nr_job_sections; i++)
1467 if (!strcmp(job_sections[i], name))
1468 return 0;
1469
1470 return 1;
1471}
1472
1473static int is_empty_or_comment(char *line)
1474{
1475 unsigned int i;
1476
1477 for (i = 0; i < strlen(line); i++) {
1478 if (line[i] == ';')
1479 return 1;
1480 if (line[i] == '#')
1481 return 1;
1482 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
1483 return 0;
1484 }
1485
1486 return 1;
1487}
1488
1489/*
1490 * This is our [ini] type file parser.
1491 */
1492int __parse_jobs_ini(struct thread_data *td,
1493 char *file, int is_buf, int stonewall_flag, int type,
1494 int nested, char *name, char ***popts, int *aopts, int *nopts)
1495{
1496 unsigned int global = 0;
1497 char *string;
1498 FILE *f;
1499 char *p;
1500 int ret = 0, stonewall;
1501 int first_sect = 1;
1502 int skip_fgets = 0;
1503 int inside_skip = 0;
1504 char **opts;
1505 int i, alloc_opts, num_opts;
1506
1507 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1508 assert(td || !nested);
1509
1510 if (is_buf)
1511 f = NULL;
1512 else {
1513 if (!strcmp(file, "-"))
1514 f = stdin;
1515 else
1516 f = fopen(file, "r");
1517
1518 if (!f) {
1519 int __err = errno;
1520
1521 log_err("fio: unable to open '%s' job file\n", file);
1522 if (td)
1523 td_verror(td, __err, "job file open");
1524 return 1;
1525 }
1526 }
1527
1528 string = malloc(4096);
1529
1530 /*
1531 * it's really 256 + small bit, 280 should suffice
1532 */
1533 if (!nested) {
1534 name = malloc(280);
1535 memset(name, 0, 280);
1536 }
1537
1538 opts = NULL;
1539 if (nested && popts) {
1540 opts = *popts;
1541 alloc_opts = *aopts;
1542 num_opts = *nopts;
1543 }
1544
1545 if (!opts) {
1546 alloc_opts = 8;
1547 opts = malloc(sizeof(char *) * alloc_opts);
1548 num_opts = 0;
1549 }
1550
1551 stonewall = stonewall_flag;
1552 do {
1553 /*
1554 * if skip_fgets is set, we already have loaded a line we
1555 * haven't handled.
1556 */
1557 if (!skip_fgets) {
1558 if (is_buf)
1559 p = strsep(&file, "\n");
1560 else
1561 p = fgets(string, 4096, f);
1562 if (!p)
1563 break;
1564 }
1565
1566 skip_fgets = 0;
1567 strip_blank_front(&p);
1568 strip_blank_end(p);
1569
1570 dprint(FD_PARSE, "%s\n", p);
1571 if (is_empty_or_comment(p))
1572 continue;
1573
1574 if (!nested) {
1575 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1576 if (inside_skip)
1577 continue;
1578
1579 log_err("fio: option <%s> outside of "
1580 "[] job section\n", p);
1581 ret = 1;
1582 break;
1583 }
1584
1585 name[strlen(name) - 1] = '\0';
1586
1587 if (skip_this_section(name)) {
1588 inside_skip = 1;
1589 continue;
1590 } else
1591 inside_skip = 0;
1592
1593 dprint(FD_PARSE, "Parsing section [%s]\n", name);
1594
1595 global = !strncmp(name, "global", 6);
1596
1597 if (dump_cmdline) {
1598 if (first_sect)
1599 log_info("fio ");
1600 if (!global)
1601 log_info("--name=%s ", name);
1602 first_sect = 0;
1603 }
1604
1605 td = get_new_job(global, &def_thread, 0, name);
1606 if (!td) {
1607 ret = 1;
1608 break;
1609 }
1610
1611 /*
1612 * Separate multiple job files by a stonewall
1613 */
1614 if (!global && stonewall) {
1615 td->o.stonewall = stonewall;
1616 stonewall = 0;
1617 }
1618
1619 num_opts = 0;
1620 memset(opts, 0, alloc_opts * sizeof(char *));
1621 }
1622 else
1623 skip_fgets = 1;
1624
1625 while (1) {
1626 if (!skip_fgets) {
1627 if (is_buf)
1628 p = strsep(&file, "\n");
1629 else
1630 p = fgets(string, 4096, f);
1631 if (!p)
1632 break;
1633 dprint(FD_PARSE, "%s", p);
1634 }
1635 else
1636 skip_fgets = 0;
1637
1638 if (is_empty_or_comment(p))
1639 continue;
1640
1641 strip_blank_front(&p);
1642
1643 /*
1644 * new section, break out and make sure we don't
1645 * fgets() a new line at the top.
1646 */
1647 if (p[0] == '[') {
1648 if (nested) {
1649 log_err("No new sections in included files\n");
1650 return 1;
1651 }
1652
1653 skip_fgets = 1;
1654 break;
1655 }
1656
1657 strip_blank_end(p);
1658
1659 if (!strncmp(p, "include", strlen("include"))) {
1660 char *filename = p + strlen("include") + 1;
1661
1662 if ((ret = __parse_jobs_ini(td, filename,
1663 is_buf, stonewall_flag, type, 1,
1664 name, &opts, &alloc_opts, &num_opts))) {
1665 log_err("Error %d while parsing include file %s\n",
1666 ret, filename);
1667 break;
1668 }
1669 continue;
1670 }
1671
1672 if (num_opts == alloc_opts) {
1673 alloc_opts <<= 1;
1674 opts = realloc(opts,
1675 alloc_opts * sizeof(char *));
1676 }
1677
1678 opts[num_opts] = strdup(p);
1679 num_opts++;
1680 }
1681
1682 if (nested) {
1683 *popts = opts;
1684 *aopts = alloc_opts;
1685 *nopts = num_opts;
1686 goto out;
1687 }
1688
1689 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1690 if (!ret)
1691 ret = add_job(td, name, 0, 0, type);
1692 else {
1693 log_err("fio: job %s dropped\n", name);
1694 put_job(td);
1695 }
1696
1697 for (i = 0; i < num_opts; i++)
1698 free(opts[i]);
1699 num_opts = 0;
1700 } while (!ret);
1701
1702 if (dump_cmdline)
1703 log_info("\n");
1704
1705 i = 0;
1706 while (i < nr_job_sections) {
1707 free(job_sections[i]);
1708 i++;
1709 }
1710
1711 free(opts);
1712out:
1713 free(string);
1714 if (!nested)
1715 free(name);
1716 if (!is_buf && f != stdin)
1717 fclose(f);
1718 return ret;
1719}
1720
1721int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1722{
1723 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
1724 0, NULL, NULL, NULL, NULL);
1725}
1726
1727static int fill_def_thread(void)
1728{
1729 memset(&def_thread, 0, sizeof(def_thread));
1730
1731 fio_getaffinity(getpid(), &def_thread.o.cpumask);
1732 def_thread.o.error_dump = 1;
1733
1734 /*
1735 * fill default options
1736 */
1737 fio_fill_default_options(&def_thread);
1738 return 0;
1739}
1740
1741static void show_debug_categories(void)
1742{
1743 struct debug_level *dl = &debug_levels[0];
1744 int curlen, first = 1;
1745
1746 curlen = 0;
1747 while (dl->name) {
1748 int has_next = (dl + 1)->name != NULL;
1749
1750 if (first || curlen + strlen(dl->name) >= 80) {
1751 if (!first) {
1752 printf("\n");
1753 curlen = 0;
1754 }
1755 curlen += printf("\t\t\t%s", dl->name);
1756 curlen += 3 * (8 - 1);
1757 if (has_next)
1758 curlen += printf(",");
1759 } else {
1760 curlen += printf("%s", dl->name);
1761 if (has_next)
1762 curlen += printf(",");
1763 }
1764 dl++;
1765 first = 0;
1766 }
1767 printf("\n");
1768}
1769
1770static void usage(const char *name)
1771{
1772 printf("%s\n", fio_version_string);
1773 printf("%s [options] [job options] <job file(s)>\n", name);
1774 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
1775 show_debug_categories();
1776 printf(" --parse-only\t\tParse options only, don't start any IO\n");
1777 printf(" --output\t\tWrite output to file\n");
1778 printf(" --runtime\t\tRuntime in seconds\n");
1779 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1780 printf(" --minimal\t\tMinimal (terse) output\n");
1781 printf(" --output-format=x\tOutput format (terse,json,json+,normal)\n");
1782 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
1783 printf(" --version\t\tPrint version info and exit\n");
1784 printf(" --help\t\tPrint this page\n");
1785 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
1786 printf(" --crctest\t\tTest speed of checksum functions\n");
1787 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
1788 " them\n");
1789 printf(" --enghelp=engine\tPrint ioengine help, or list"
1790 " available ioengines\n");
1791 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1792 " cmd\n");
1793 printf(" --showcmd\t\tTurn a job file into command line options\n");
1794 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1795 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
1796 printf(" --eta-newline=time\tForce a new line for every 'time'");
1797 printf(" period passed\n");
1798 printf(" --status-interval=t\tForce full status dump every");
1799 printf(" 't' period passed\n");
1800 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
1801 " writes\n");
1802 printf(" --section=name\tOnly run specified section in job file\n");
1803 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
1804 " (def 1024)\n");
1805 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1806 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1807 printf(" --server=args\t\tStart a backend fio server\n");
1808 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1809 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
1810 printf(" --remote-config=file\tTell fio server to load this local job file\n");
1811 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1812 "\t\t\t(option=system,percpu) or run unit work\n"
1813 "\t\t\tcalibration only (option=calibrate)\n");
1814#ifdef CONFIG_ZLIB
1815 printf(" --inflate-log=log\tInflate and output compressed log\n");
1816#endif
1817 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
1818 printf(" --trigger-timeout=t\tExecute trigger af this time\n");
1819 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
1820 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
1821 printf(" --aux-path=path\tUse this path for fio state generated files\n");
1822 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1823 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1824 printf("\n Jens Axboe <axboe@fb.com>\n");
1825}
1826
1827#ifdef FIO_INC_DEBUG
1828struct debug_level debug_levels[] = {
1829 { .name = "process",
1830 .help = "Process creation/exit logging",
1831 .shift = FD_PROCESS,
1832 },
1833 { .name = "file",
1834 .help = "File related action logging",
1835 .shift = FD_FILE,
1836 },
1837 { .name = "io",
1838 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1839 .shift = FD_IO,
1840 },
1841 { .name = "mem",
1842 .help = "Memory allocation/freeing logging",
1843 .shift = FD_MEM,
1844 },
1845 { .name = "blktrace",
1846 .help = "blktrace action logging",
1847 .shift = FD_BLKTRACE,
1848 },
1849 { .name = "verify",
1850 .help = "IO verification action logging",
1851 .shift = FD_VERIFY,
1852 },
1853 { .name = "random",
1854 .help = "Random generation logging",
1855 .shift = FD_RANDOM,
1856 },
1857 { .name = "parse",
1858 .help = "Parser logging",
1859 .shift = FD_PARSE,
1860 },
1861 { .name = "diskutil",
1862 .help = "Disk utility logging actions",
1863 .shift = FD_DISKUTIL,
1864 },
1865 { .name = "job",
1866 .help = "Logging related to creating/destroying jobs",
1867 .shift = FD_JOB,
1868 },
1869 { .name = "mutex",
1870 .help = "Mutex logging",
1871 .shift = FD_MUTEX
1872 },
1873 { .name = "profile",
1874 .help = "Logging related to profiles",
1875 .shift = FD_PROFILE,
1876 },
1877 { .name = "time",
1878 .help = "Logging related to time keeping functions",
1879 .shift = FD_TIME,
1880 },
1881 { .name = "net",
1882 .help = "Network logging",
1883 .shift = FD_NET,
1884 },
1885 { .name = "rate",
1886 .help = "Rate logging",
1887 .shift = FD_RATE,
1888 },
1889 { .name = "compress",
1890 .help = "Log compression logging",
1891 .shift = FD_COMPRESS,
1892 },
1893 { .name = NULL, },
1894};
1895
1896static int set_debug(const char *string)
1897{
1898 struct debug_level *dl;
1899 char *p = (char *) string;
1900 char *opt;
1901 int i;
1902
1903 if (!string)
1904 return 0;
1905
1906 if (!strcmp(string, "?") || !strcmp(string, "help")) {
1907 log_info("fio: dumping debug options:");
1908 for (i = 0; debug_levels[i].name; i++) {
1909 dl = &debug_levels[i];
1910 log_info("%s,", dl->name);
1911 }
1912 log_info("all\n");
1913 return 1;
1914 }
1915
1916 while ((opt = strsep(&p, ",")) != NULL) {
1917 int found = 0;
1918
1919 if (!strncmp(opt, "all", 3)) {
1920 log_info("fio: set all debug options\n");
1921 fio_debug = ~0UL;
1922 continue;
1923 }
1924
1925 for (i = 0; debug_levels[i].name; i++) {
1926 dl = &debug_levels[i];
1927 found = !strncmp(opt, dl->name, strlen(dl->name));
1928 if (!found)
1929 continue;
1930
1931 if (dl->shift == FD_JOB) {
1932 opt = strchr(opt, ':');
1933 if (!opt) {
1934 log_err("fio: missing job number\n");
1935 break;
1936 }
1937 opt++;
1938 fio_debug_jobno = atoi(opt);
1939 log_info("fio: set debug jobno %d\n",
1940 fio_debug_jobno);
1941 } else {
1942 log_info("fio: set debug option %s\n", opt);
1943 fio_debug |= (1UL << dl->shift);
1944 }
1945 break;
1946 }
1947
1948 if (!found)
1949 log_err("fio: debug mask %s not found\n", opt);
1950 }
1951 return 0;
1952}
1953#else
1954static int set_debug(const char *string)
1955{
1956 log_err("fio: debug tracing not included in build\n");
1957 return 1;
1958}
1959#endif
1960
1961static void fio_options_fill_optstring(void)
1962{
1963 char *ostr = cmd_optstr;
1964 int i, c;
1965
1966 c = i = 0;
1967 while (l_opts[i].name) {
1968 ostr[c++] = l_opts[i].val;
1969 if (l_opts[i].has_arg == required_argument)
1970 ostr[c++] = ':';
1971 else if (l_opts[i].has_arg == optional_argument) {
1972 ostr[c++] = ':';
1973 ostr[c++] = ':';
1974 }
1975 i++;
1976 }
1977 ostr[c] = '\0';
1978}
1979
1980static int client_flag_set(char c)
1981{
1982 int i;
1983
1984 i = 0;
1985 while (l_opts[i].name) {
1986 int val = l_opts[i].val;
1987
1988 if (c == (val & 0xff))
1989 return (val & FIO_CLIENT_FLAG);
1990
1991 i++;
1992 }
1993
1994 return 0;
1995}
1996
1997static void parse_cmd_client(void *client, char *opt)
1998{
1999 fio_client_add_cmd_option(client, opt);
2000}
2001
2002static void show_closest_option(const char *name)
2003{
2004 int best_option, best_distance;
2005 int i, distance;
2006
2007 while (*name == '-')
2008 name++;
2009
2010 best_option = -1;
2011 best_distance = INT_MAX;
2012 i = 0;
2013 while (l_opts[i].name) {
2014 distance = string_distance(name, l_opts[i].name);
2015 if (distance < best_distance) {
2016 best_distance = distance;
2017 best_option = i;
2018 }
2019 i++;
2020 }
2021
2022 if (best_option != -1 && string_distance_ok(name, best_distance))
2023 log_err("Did you mean %s?\n", l_opts[best_option].name);
2024}
2025
2026static int parse_output_format(const char *optarg)
2027{
2028 char *p, *orig, *opt;
2029 int ret = 0;
2030
2031 p = orig = strdup(optarg);
2032
2033 output_format = 0;
2034
2035 while ((opt = strsep(&p, ",")) != NULL) {
2036 if (!strcmp(opt, "minimal") ||
2037 !strcmp(opt, "terse") ||
2038 !strcmp(opt, "csv"))
2039 output_format |= FIO_OUTPUT_TERSE;
2040 else if (!strcmp(opt, "json"))
2041 output_format |= FIO_OUTPUT_JSON;
2042 else if (!strcmp(opt, "json+"))
2043 output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
2044 else if (!strcmp(opt, "normal"))
2045 output_format |= FIO_OUTPUT_NORMAL;
2046 else {
2047 log_err("fio: invalid output format %s\n", opt);
2048 ret = 1;
2049 break;
2050 }
2051 }
2052
2053 free(orig);
2054 return ret;
2055}
2056
2057int parse_cmd_line(int argc, char *argv[], int client_type)
2058{
2059 struct thread_data *td = NULL;
2060 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
2061 char *ostr = cmd_optstr;
2062 void *pid_file = NULL;
2063 void *cur_client = NULL;
2064 int backend = 0;
2065
2066 /*
2067 * Reset optind handling, since we may call this multiple times
2068 * for the backend.
2069 */
2070 optind = 1;
2071
2072 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2073 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
2074 parse_cmd_client(cur_client, argv[optind - 1]);
2075 c &= ~FIO_CLIENT_FLAG;
2076 }
2077
2078 switch (c) {
2079 case 'a':
2080 smalloc_pool_size = atoi(optarg);
2081 break;
2082 case 't':
2083 if (check_str_time(optarg, &def_timeout, 1)) {
2084 log_err("fio: failed parsing time %s\n", optarg);
2085 do_exit++;
2086 exit_val = 1;
2087 }
2088 break;
2089 case 'l':
2090 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2091 do_exit++;
2092 exit_val = 1;
2093 break;
2094 case 'b':
2095 write_bw_log = 1;
2096 break;
2097 case 'o':
2098 if (f_out && f_out != stdout)
2099 fclose(f_out);
2100
2101 f_out = fopen(optarg, "w+");
2102 if (!f_out) {
2103 perror("fopen output");
2104 exit(1);
2105 }
2106 f_err = f_out;
2107 break;
2108 case 'm':
2109 output_format = FIO_OUTPUT_TERSE;
2110 break;
2111 case 'F':
2112 if (!optarg) {
2113 log_err("fio: missing --output-format argument\n");
2114 exit_val = 1;
2115 do_exit++;
2116 break;
2117 }
2118 if (parse_output_format(optarg)) {
2119 log_err("fio: failed parsing output-format\n");
2120 exit_val = 1;
2121 do_exit++;
2122 break;
2123 }
2124 break;
2125 case 'f':
2126 output_format |= FIO_OUTPUT_TERSE;
2127 break;
2128 case 'h':
2129 did_arg = 1;
2130 if (!cur_client) {
2131 usage(argv[0]);
2132 do_exit++;
2133 }
2134 break;
2135 case 'c':
2136 did_arg = 1;
2137 if (!cur_client) {
2138 fio_show_option_help(optarg);
2139 do_exit++;
2140 }
2141 break;
2142 case 'i':
2143 did_arg = 1;
2144 if (!cur_client) {
2145 fio_show_ioengine_help(optarg);
2146 do_exit++;
2147 }
2148 break;
2149 case 's':
2150 did_arg = 1;
2151 dump_cmdline = 1;
2152 break;
2153 case 'r':
2154 read_only = 1;
2155 break;
2156 case 'v':
2157 did_arg = 1;
2158 if (!cur_client) {
2159 log_info("%s\n", fio_version_string);
2160 do_exit++;
2161 }
2162 break;
2163 case 'V':
2164 terse_version = atoi(optarg);
2165 if (!(terse_version == 2 || terse_version == 3 ||
2166 terse_version == 4)) {
2167 log_err("fio: bad terse version format\n");
2168 exit_val = 1;
2169 do_exit++;
2170 }
2171 break;
2172 case 'e':
2173 if (!strcmp("always", optarg))
2174 eta_print = FIO_ETA_ALWAYS;
2175 else if (!strcmp("never", optarg))
2176 eta_print = FIO_ETA_NEVER;
2177 break;
2178 case 'E': {
2179 long long t = 0;
2180
2181 if (check_str_time(optarg, &t, 1)) {
2182 log_err("fio: failed parsing eta time %s\n", optarg);
2183 exit_val = 1;
2184 do_exit++;
2185 }
2186 eta_new_line = t / 1000;
2187 break;
2188 }
2189 case 'd':
2190 if (set_debug(optarg))
2191 do_exit++;
2192 break;
2193 case 'P':
2194 did_arg = 1;
2195 parse_only = 1;
2196 break;
2197 case 'x': {
2198 size_t new_size;
2199
2200 if (!strcmp(optarg, "global")) {
2201 log_err("fio: can't use global as only "
2202 "section\n");
2203 do_exit++;
2204 exit_val = 1;
2205 break;
2206 }
2207 new_size = (nr_job_sections + 1) * sizeof(char *);
2208 job_sections = realloc(job_sections, new_size);
2209 job_sections[nr_job_sections] = strdup(optarg);
2210 nr_job_sections++;
2211 break;
2212 }
2213#ifdef CONFIG_ZLIB
2214 case 'X':
2215 exit_val = iolog_file_inflate(optarg);
2216 did_arg++;
2217 do_exit++;
2218 break;
2219#endif
2220 case 'p':
2221 did_arg = 1;
2222 if (exec_profile)
2223 free(exec_profile);
2224 exec_profile = strdup(optarg);
2225 break;
2226 case FIO_GETOPT_JOB: {
2227 const char *opt = l_opts[lidx].name;
2228 char *val = optarg;
2229
2230 if (!strncmp(opt, "name", 4) && td) {
2231 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2232 if (ret)
2233 goto out_free;
2234 td = NULL;
2235 did_arg = 1;
2236 }
2237 if (!td) {
2238 int is_section = !strncmp(opt, "name", 4);
2239 int global = 0;
2240
2241 if (!is_section || !strncmp(val, "global", 6))
2242 global = 1;
2243
2244 if (is_section && skip_this_section(val))
2245 continue;
2246
2247 td = get_new_job(global, &def_thread, 1, NULL);
2248 if (!td || ioengine_load(td)) {
2249 if (td) {
2250 put_job(td);
2251 td = NULL;
2252 }
2253 do_exit++;
2254 exit_val = 1;
2255 break;
2256 }
2257 fio_options_set_ioengine_opts(l_opts, td);
2258 }
2259
2260 if ((!val || !strlen(val)) &&
2261 l_opts[lidx].has_arg == required_argument) {
2262 log_err("fio: option %s requires an argument\n", opt);
2263 ret = 1;
2264 } else
2265 ret = fio_cmd_option_parse(td, opt, val);
2266
2267 if (ret) {
2268 if (td) {
2269 put_job(td);
2270 td = NULL;
2271 }
2272 do_exit++;
2273 exit_val = 1;
2274 }
2275
2276 if (!ret && !strcmp(opt, "ioengine")) {
2277 free_ioengine(td);
2278 if (ioengine_load(td)) {
2279 put_job(td);
2280 td = NULL;
2281 do_exit++;
2282 exit_val = 1;
2283 break;
2284 }
2285 fio_options_set_ioengine_opts(l_opts, td);
2286 }
2287 break;
2288 }
2289 case FIO_GETOPT_IOENGINE: {
2290 const char *opt = l_opts[lidx].name;
2291 char *val = optarg;
2292
2293 if (!td)
2294 break;
2295
2296 ret = fio_cmd_ioengine_option_parse(td, opt, val);
2297 break;
2298 }
2299 case 'w':
2300 warnings_fatal = 1;
2301 break;
2302 case 'j':
2303 max_jobs = atoi(optarg);
2304 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2305 log_err("fio: invalid max jobs: %d\n", max_jobs);
2306 do_exit++;
2307 exit_val = 1;
2308 }
2309 break;
2310 case 'S':
2311 did_arg = 1;
2312#ifndef CONFIG_NO_SHM
2313 if (nr_clients) {
2314 log_err("fio: can't be both client and server\n");
2315 do_exit++;
2316 exit_val = 1;
2317 break;
2318 }
2319 if (optarg)
2320 fio_server_set_arg(optarg);
2321 is_backend = 1;
2322 backend = 1;
2323#else
2324 log_err("fio: client/server requires SHM support\n");
2325 do_exit++;
2326 exit_val = 1;
2327#endif
2328 break;
2329 case 'D':
2330 if (pid_file)
2331 free(pid_file);
2332 pid_file = strdup(optarg);
2333 break;
2334 case 'I':
2335 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2336 /* exit on error and calibration only */
2337 did_arg = 1;
2338 do_exit++;
2339 if (ret == -1)
2340 exit_val = 1;
2341 }
2342 break;
2343 case 'C':
2344 did_arg = 1;
2345 if (is_backend) {
2346 log_err("fio: can't be both client and server\n");
2347 do_exit++;
2348 exit_val = 1;
2349 break;
2350 }
2351 /* if --client parameter contains a pathname */
2352 if (0 == access(optarg, R_OK)) {
2353 /* file contains a list of host addrs or names */
2354 char hostaddr[PATH_MAX] = {0};
2355 char formatstr[8];
2356 FILE * hostf = fopen(optarg, "r");
2357 if (!hostf) {
2358 log_err("fio: could not open client list file %s for read\n", optarg);
2359 do_exit++;
2360 exit_val = 1;
2361 break;
2362 }
2363 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2364 /*
2365 * read at most PATH_MAX-1 chars from each
2366 * record in this file
2367 */
2368 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2369 /* expect EVERY host in file to be valid */
2370 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2371 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2372 do_exit++;
2373 exit_val = 1;
2374 break;
2375 }
2376 }
2377 fclose(hostf);
2378 break; /* no possibility of job file for "this client only" */
2379 }
2380 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
2381 log_err("fio: failed adding client %s\n", optarg);
2382 do_exit++;
2383 exit_val = 1;
2384 break;
2385 }
2386 /*
2387 * If the next argument exists and isn't an option,
2388 * assume it's a job file for this client only.
2389 */
2390 while (optind < argc) {
2391 if (!strncmp(argv[optind], "--", 2) ||
2392 !strncmp(argv[optind], "-", 1))
2393 break;
2394
2395 if (fio_client_add_ini_file(cur_client, argv[optind], 0))
2396 break;
2397 optind++;
2398 }
2399 break;
2400 case 'R':
2401 did_arg = 1;
2402 if (fio_client_add_ini_file(cur_client, optarg, 1)) {
2403 do_exit++;
2404 exit_val = 1;
2405 }
2406 break;
2407 case 'T':
2408 did_arg = 1;
2409 do_exit++;
2410 exit_val = fio_monotonic_clocktest(1);
2411 break;
2412 case 'G':
2413 did_arg = 1;
2414 do_exit++;
2415 exit_val = fio_crctest(optarg);
2416 break;
2417 case 'L': {
2418 long long val;
2419
2420 if (check_str_time(optarg, &val, 1)) {
2421 log_err("fio: failed parsing time %s\n", optarg);
2422 do_exit++;
2423 exit_val = 1;
2424 break;
2425 }
2426 status_interval = val / 1000;
2427 break;
2428 }
2429 case 'W':
2430 if (trigger_file)
2431 free(trigger_file);
2432 trigger_file = strdup(optarg);
2433 break;
2434 case 'H':
2435 if (trigger_cmd)
2436 free(trigger_cmd);
2437 trigger_cmd = strdup(optarg);
2438 break;
2439 case 'J':
2440 if (trigger_remote_cmd)
2441 free(trigger_remote_cmd);
2442 trigger_remote_cmd = strdup(optarg);
2443 break;
2444 case 'K':
2445 if (aux_path)
2446 free(aux_path);
2447 aux_path = strdup(optarg);
2448 break;
2449 case 'B':
2450 if (check_str_time(optarg, &trigger_timeout, 1)) {
2451 log_err("fio: failed parsing time %s\n", optarg);
2452 do_exit++;
2453 exit_val = 1;
2454 }
2455 trigger_timeout /= 1000000;
2456 break;
2457 case '?':
2458 log_err("%s: unrecognized option '%s'\n", argv[0],
2459 argv[optind - 1]);
2460 show_closest_option(argv[optind - 1]);
2461 default:
2462 do_exit++;
2463 exit_val = 1;
2464 break;
2465 }
2466 if (do_exit)
2467 break;
2468 }
2469
2470 if (do_exit && !(is_backend || nr_clients))
2471 exit(exit_val);
2472
2473 if (nr_clients && fio_clients_connect())
2474 exit(1);
2475
2476 if (is_backend && backend)
2477 return fio_start_server(pid_file);
2478 else if (pid_file)
2479 free(pid_file);
2480
2481 if (td) {
2482 if (!ret) {
2483 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
2484 if (ret)
2485 did_arg = 1;
2486 }
2487 }
2488
2489 while (!ret && optind < argc) {
2490 ini_idx++;
2491 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2492 ini_file[ini_idx - 1] = strdup(argv[optind]);
2493 optind++;
2494 }
2495
2496out_free:
2497 if (pid_file)
2498 free(pid_file);
2499
2500 return ini_idx;
2501}
2502
2503int fio_init_options(void)
2504{
2505 f_out = stdout;
2506 f_err = stderr;
2507
2508 fio_options_fill_optstring();
2509 fio_options_dup_and_init(l_opts);
2510
2511 atexit(free_shm);
2512
2513 if (fill_def_thread())
2514 return 1;
2515
2516 return 0;
2517}
2518
2519extern int fio_check_options(struct thread_options *);
2520
2521int parse_options(int argc, char *argv[])
2522{
2523 const int type = FIO_CLIENT_TYPE_CLI;
2524 int job_files, i;
2525
2526 if (fio_init_options())
2527 return 1;
2528 if (fio_test_cconv(&def_thread.o))
2529 log_err("fio: failed internal cconv test\n");
2530
2531 job_files = parse_cmd_line(argc, argv, type);
2532
2533 if (job_files > 0) {
2534 for (i = 0; i < job_files; i++) {
2535 if (i && fill_def_thread())
2536 return 1;
2537 if (nr_clients) {
2538 if (fio_clients_send_ini(ini_file[i]))
2539 return 1;
2540 free(ini_file[i]);
2541 } else if (!is_backend) {
2542 if (parse_jobs_ini(ini_file[i], 0, i, type))
2543 return 1;
2544 free(ini_file[i]);
2545 }
2546 }
2547 } else if (nr_clients) {
2548 if (fill_def_thread())
2549 return 1;
2550 if (fio_clients_send_ini(NULL))
2551 return 1;
2552 }
2553
2554 free(ini_file);
2555 fio_options_free(&def_thread);
2556 filesetup_mem_free();
2557
2558 if (!thread_number) {
2559 if (parse_dryrun())
2560 return 0;
2561 if (exec_profile)
2562 return 0;
2563 if (is_backend || nr_clients)
2564 return 0;
2565 if (did_arg)
2566 return 0;
2567
2568 log_err("No jobs(s) defined\n\n");
2569
2570 if (!did_arg) {
2571 usage(argv[0]);
2572 return 1;
2573 }
2574
2575 return 0;
2576 }
2577
2578 if (output_format & FIO_OUTPUT_NORMAL)
2579 log_info("%s\n", fio_version_string);
2580
2581 return 0;
2582}
2583
2584void options_default_fill(struct thread_options *o)
2585{
2586 memcpy(o, &def_thread.o, sizeof(*o));
2587}