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