Add 'f' (Finishing) flag to status output
[fio.git] / init.c
CommitLineData
906c8d75 1/*
cb2c86fd 2 * This file contains job initialization and setup functions.
906c8d75 3 */
ebac4655
JA
4#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <ctype.h>
9#include <string.h>
10#include <errno.h>
11#include <sys/ipc.h>
ebac4655
JA
12#include <sys/types.h>
13#include <sys/stat.h>
14
15#include "fio.h"
a5e0ee11
O
16#ifndef FIO_NO_HAVE_SHM_H
17#include <sys/shm.h>
18#endif
19
cb2c86fd 20#include "parse.h"
2e5cdb11 21#include "smalloc.h"
380065aa 22#include "filehash.h"
4f5af7b2 23#include "verify.h"
79d16311 24#include "profile.h"
50d16976 25#include "server.h"
f2a2ce0e 26#include "idletime.h"
ebac4655 27
bf2e821a 28#include "lib/getopt.h"
e25b6d5a 29#include "lib/strcasestr.h"
bf2e821a 30
10aa136b
JA
31#include "crc/test.h"
32
3d43382c 33const char fio_version_string[] = FIO_VERSION;
214e1eca 34
ee738499 35#define FIO_RANDSEED (0xb1899bedUL)
ebac4655 36
214e1eca 37static char **ini_file;
fca70358 38static int max_jobs = FIO_MAX_JOBS;
cca73aa7 39static int dump_cmdline;
8c029376 40static int def_timeout;
111e032d 41static int parse_only;
e1f36503 42
be4ecfdf 43static struct thread_data def_thread;
214e1eca 44struct thread_data *threads = NULL;
10aa136b
JA
45static char **job_sections;
46static int nr_job_sections;
e1f36503 47
214e1eca 48int exitall_on_terminate = 0;
f3afa57e 49int output_format = FIO_OUTPUT_NORMAL;
2b8c71b0 50int append_terse_output = 0;
5be4c944 51int eta_print = FIO_ETA_AUTO;
e382e661 52int eta_new_line = 0;
214e1eca
JA
53FILE *f_out = NULL;
54FILE *f_err = NULL;
07b3232d 55char *exec_profile = NULL;
a9523c6f 56int warnings_fatal = 0;
4d658652 57int terse_version = 3;
50d16976 58int is_backend = 0;
a37f69b7 59int nr_clients = 0;
e46d8091 60int log_syslog = 0;
ee738499 61
214e1eca 62int write_bw_log = 0;
4241ea8f 63int read_only = 0;
06464907 64int status_interval = 0;
214e1eca 65
5ec10eaa 66static int write_lat_log;
e1f36503 67
214e1eca 68static int prev_group_jobs;
b4692828 69
ee56ad50 70unsigned long fio_debug = 0;
5e1d306e
JA
71unsigned int fio_debug_jobno = -1;
72unsigned int *fio_debug_jobp = NULL;
ee56ad50 73
4c6107ff 74static char cmd_optstr[256];
085399db 75static int did_arg;
4c6107ff 76
7a4b8240
JA
77#define FIO_CLIENT_FLAG (1 << 16)
78
b4692828
JA
79/*
80 * Command line options. These will contain the above, plus a few
81 * extra that only pertain to fio itself and not jobs.
82 */
5ec10eaa 83static struct option l_opts[FIO_NR_OPTIONS] = {
b4692828 84 {
08d2a19c 85 .name = (char *) "output",
b4692828 86 .has_arg = required_argument,
7a4b8240 87 .val = 'o' | FIO_CLIENT_FLAG,
b4692828
JA
88 },
89 {
08d2a19c 90 .name = (char *) "timeout",
b4692828 91 .has_arg = required_argument,
7a4b8240 92 .val = 't' | FIO_CLIENT_FLAG,
b4692828
JA
93 },
94 {
08d2a19c 95 .name = (char *) "latency-log",
b4692828 96 .has_arg = required_argument,
7a4b8240 97 .val = 'l' | FIO_CLIENT_FLAG,
b4692828
JA
98 },
99 {
08d2a19c 100 .name = (char *) "bandwidth-log",
b4692828 101 .has_arg = required_argument,
7a4b8240 102 .val = 'b' | FIO_CLIENT_FLAG,
b4692828
JA
103 },
104 {
08d2a19c 105 .name = (char *) "minimal",
c4ec0a1d 106 .has_arg = no_argument,
7a4b8240 107 .val = 'm' | FIO_CLIENT_FLAG,
b4692828 108 },
cc372b17 109 {
f3afa57e 110 .name = (char *) "output-format",
cc372b17 111 .has_arg = optional_argument,
f3afa57e 112 .val = 'F' | FIO_CLIENT_FLAG,
cc372b17 113 },
2b8c71b0
CE
114 {
115 .name = (char *) "append-terse",
116 .has_arg = optional_argument,
117 .val = 'f',
118 },
b4692828 119 {
08d2a19c 120 .name = (char *) "version",
b4692828 121 .has_arg = no_argument,
7a4b8240 122 .val = 'v' | FIO_CLIENT_FLAG,
b4692828 123 },
fd28ca49 124 {
08d2a19c 125 .name = (char *) "help",
fd28ca49 126 .has_arg = no_argument,
7a4b8240 127 .val = 'h' | FIO_CLIENT_FLAG,
fd28ca49
JA
128 },
129 {
08d2a19c 130 .name = (char *) "cmdhelp",
320beefe 131 .has_arg = optional_argument,
7a4b8240 132 .val = 'c' | FIO_CLIENT_FLAG,
fd28ca49 133 },
de890a1e 134 {
06464907 135 .name = (char *) "enghelp",
de890a1e 136 .has_arg = optional_argument,
06464907 137 .val = 'i' | FIO_CLIENT_FLAG,
de890a1e 138 },
cca73aa7 139 {
08d2a19c 140 .name = (char *) "showcmd",
cca73aa7 141 .has_arg = no_argument,
7a4b8240 142 .val = 's' | FIO_CLIENT_FLAG,
724e4435
JA
143 },
144 {
08d2a19c 145 .name = (char *) "readonly",
724e4435 146 .has_arg = no_argument,
7a4b8240 147 .val = 'r' | FIO_CLIENT_FLAG,
cca73aa7 148 },
e592a06b 149 {
08d2a19c 150 .name = (char *) "eta",
e592a06b 151 .has_arg = required_argument,
7a4b8240 152 .val = 'e' | FIO_CLIENT_FLAG,
e592a06b 153 },
e382e661
JA
154 {
155 .name = (char *) "eta-newline",
156 .has_arg = required_argument,
157 .val = 'E' | FIO_CLIENT_FLAG,
158 },
ee56ad50 159 {
08d2a19c 160 .name = (char *) "debug",
ee56ad50 161 .has_arg = required_argument,
7a4b8240 162 .val = 'd' | FIO_CLIENT_FLAG,
ee56ad50 163 },
111e032d
JA
164 {
165 .name = (char *) "parse-only",
166 .has_arg = no_argument,
167 .val = 'P' | FIO_CLIENT_FLAG,
168 },
01f06b63 169 {
08d2a19c 170 .name = (char *) "section",
01f06b63 171 .has_arg = required_argument,
7a4b8240 172 .val = 'x' | FIO_CLIENT_FLAG,
01f06b63 173 },
2b386d25 174 {
08d2a19c 175 .name = (char *) "alloc-size",
2b386d25 176 .has_arg = required_argument,
7a4b8240 177 .val = 'a' | FIO_CLIENT_FLAG,
2b386d25 178 },
9ac8a797 179 {
08d2a19c 180 .name = (char *) "profile",
9ac8a797 181 .has_arg = required_argument,
7a4b8240 182 .val = 'p' | FIO_CLIENT_FLAG,
9ac8a797 183 },
a9523c6f 184 {
08d2a19c 185 .name = (char *) "warnings-fatal",
a9523c6f 186 .has_arg = no_argument,
7a4b8240 187 .val = 'w' | FIO_CLIENT_FLAG,
a9523c6f 188 },
fca70358
JA
189 {
190 .name = (char *) "max-jobs",
191 .has_arg = required_argument,
7a4b8240 192 .val = 'j' | FIO_CLIENT_FLAG,
fca70358 193 },
f57a9c59
JA
194 {
195 .name = (char *) "terse-version",
196 .has_arg = required_argument,
7a4b8240 197 .val = 'V' | FIO_CLIENT_FLAG,
f57a9c59 198 },
50d16976
JA
199 {
200 .name = (char *) "server",
87aa8f19 201 .has_arg = optional_argument,
50d16976
JA
202 .val = 'S',
203 },
e46d8091 204 { .name = (char *) "daemonize",
402668f3 205 .has_arg = required_argument,
e46d8091
JA
206 .val = 'D',
207 },
132159a5
JA
208 {
209 .name = (char *) "client",
210 .has_arg = required_argument,
211 .val = 'C',
212 },
7d11f871
JA
213 {
214 .name = (char *) "cpuclock-test",
215 .has_arg = no_argument,
216 .val = 'T',
217 },
fec0f21c
JA
218 {
219 .name = (char *) "crctest",
220 .has_arg = optional_argument,
221 .val = 'G',
222 },
f2a2ce0e
HL
223 {
224 .name = (char *) "idle-prof",
225 .has_arg = required_argument,
226 .val = 'I',
227 },
06464907
JA
228 {
229 .name = (char *) "status-interval",
230 .has_arg = required_argument,
231 .val = 'L',
232 },
b4692828
JA
233 {
234 .name = NULL,
235 },
236};
237
2bb3f0a7 238void free_threads_shm(void)
9d9eb2e7
JA
239{
240 struct shmid_ds sbuf;
241
242 if (threads) {
243 void *tp = threads;
244
245 threads = NULL;
2bb3f0a7
JA
246 shmdt(tp);
247 shmctl(shm_id, IPC_RMID, &sbuf);
248 shm_id = -1;
249 }
250}
251
10aa136b 252static void free_shm(void)
2bb3f0a7
JA
253{
254 if (threads) {
9d9eb2e7 255 file_hash_exit();
9e684a49 256 flow_exit();
9d9eb2e7 257 fio_debug_jobp = NULL;
2bb3f0a7 258 free_threads_shm();
9d9eb2e7
JA
259 }
260
eb0c74ae 261 options_free(fio_options, &def_thread);
9d9eb2e7
JA
262 scleanup();
263}
264
265/*
266 * The thread area is shared between the main process and the job
267 * threads/processes. So setup a shared memory segment that will hold
268 * all the job info. We use the end of the region for keeping track of
269 * open files across jobs, for file sharing.
270 */
271static int setup_thread_area(void)
272{
273 void *hash;
274
275 if (threads)
276 return 0;
277
278 /*
279 * 1024 is too much on some machines, scale max_jobs if
280 * we get a failure that looks like too large a shm segment
281 */
282 do {
283 size_t size = max_jobs * sizeof(struct thread_data);
284
285 size += file_hash_size;
286 size += sizeof(unsigned int);
287
288 shm_id = shmget(0, size, IPC_CREAT | 0600);
289 if (shm_id != -1)
290 break;
f0c77f03 291 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
9d9eb2e7
JA
292 perror("shmget");
293 break;
294 }
295
296 max_jobs >>= 1;
297 } while (max_jobs);
298
299 if (shm_id == -1)
300 return 1;
301
302 threads = shmat(shm_id, NULL, 0);
303 if (threads == (void *) -1) {
304 perror("shmat");
305 return 1;
306 }
307
308 memset(threads, 0, max_jobs * sizeof(struct thread_data));
309 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
310 fio_debug_jobp = (void *) hash + file_hash_size;
311 *fio_debug_jobp = -1;
312 file_hash_init(hash);
9e684a49
DE
313
314 flow_init();
315
9d9eb2e7
JA
316 return 0;
317}
318
906c8d75
JA
319/*
320 * Return a free job structure.
321 */
de890a1e
SL
322static struct thread_data *get_new_job(int global, struct thread_data *parent,
323 int preserve_eo)
ebac4655
JA
324{
325 struct thread_data *td;
326
327 if (global)
328 return &def_thread;
9d9eb2e7
JA
329 if (setup_thread_area()) {
330 log_err("error: failed to setup shm segment\n");
331 return NULL;
332 }
e61f1ec8
BC
333 if (thread_number >= max_jobs) {
334 log_err("error: maximum number of jobs (%d) reached.\n",
335 max_jobs);
ebac4655 336 return NULL;
e61f1ec8 337 }
ebac4655
JA
338
339 td = &threads[thread_number++];
ddaeaa5a 340 *td = *parent;
ebac4655 341
de890a1e
SL
342 td->io_ops = NULL;
343 if (!preserve_eo)
344 td->eo = NULL;
345
e0b0d892
JA
346 td->o.uid = td->o.gid = -1U;
347
cade3ef4 348 dup_files(td, parent);
de890a1e 349 fio_options_mem_dupe(td);
cade3ef4 350
15dc1934
JA
351 profile_add_hooks(td);
352
ebac4655 353 td->thread_number = thread_number;
108fea77
JA
354
355 if (!parent || !parent->o.group_reporting)
356 stat_number++;
357
ebac4655
JA
358 return td;
359}
360
361static void put_job(struct thread_data *td)
362{
549577a7
JA
363 if (td == &def_thread)
364 return;
84dd1886 365
58c55ba0 366 profile_td_exit(td);
9e684a49 367 flow_exit_job(td);
549577a7 368
16edf25d 369 if (td->error)
6d86144d 370 log_info("fio: %s\n", td->verror);
16edf25d 371
7e356b2d 372 fio_options_free(td);
de890a1e
SL
373 if (td->io_ops)
374 free_ioengine(td);
7e356b2d 375
ebac4655
JA
376 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
377 thread_number--;
378}
379
581e7141 380static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
127f6865 381{
581e7141 382 unsigned int bs = td->o.min_bs[ddir];
127f6865 383
ff58fced
JA
384 assert(ddir_rw(ddir));
385
ba3e4e0c 386 if (td->o.rate[ddir])
1b8dbf25 387 td->rate_bps[ddir] = td->o.rate[ddir];
ba3e4e0c 388 else
1b8dbf25 389 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
127f6865 390
1b8dbf25 391 if (!td->rate_bps[ddir]) {
127f6865
JA
392 log_err("rate lower than supported\n");
393 return -1;
394 }
395
581e7141 396 td->rate_pending_usleep[ddir] = 0;
127f6865
JA
397 return 0;
398}
399
581e7141
JA
400static int setup_rate(struct thread_data *td)
401{
402 int ret = 0;
403
404 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
405 ret = __setup_rate(td, DDIR_READ);
406 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
407 ret |= __setup_rate(td, DDIR_WRITE);
6eaf09d6
SL
408 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
409 ret |= __setup_rate(td, DDIR_TRIM);
581e7141
JA
410
411 return ret;
412}
413
8347239a
JA
414static int fixed_block_size(struct thread_options *o)
415{
416 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
417 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
6eaf09d6
SL
418 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
419 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
420 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
8347239a
JA
421}
422
23ed19b0
CE
423
424static unsigned long long get_rand_start_delay(struct thread_data *td)
425{
426 unsigned long long delayrange;
427 unsigned long r;
428
429 delayrange = td->o.start_delay_high - td->o.start_delay;
430
431 if (td->o.use_os_rand) {
432 r = os_random_long(&td->delay_state);
433 delayrange = (unsigned long long) ((double) delayrange * (r / (OS_RAND_MAX + 1.0)));
434 } else {
435 r = __rand(&td->__delay_state);
436 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
437 }
438
439 delayrange += td->o.start_delay;
440 return delayrange;
441}
442
dad915e3
JA
443/*
444 * Lazy way of fixing up options that depend on each other. We could also
445 * define option callback handlers, but this is easier.
446 */
4e991c23 447static int fixup_options(struct thread_data *td)
e1f36503 448{
2dc1bbeb 449 struct thread_options *o = &td->o;
eefd98b1 450 int ret = 0;
dad915e3 451
f356d01d 452#ifndef FIO_HAVE_PSHARED_MUTEX
9bbf57cc 453 if (!o->use_thread) {
f356d01d
JA
454 log_info("fio: this platform does not support process shared"
455 " mutexes, forcing use of threads. Use the 'thread'"
456 " option to get rid of this warning.\n");
9bbf57cc 457 o->use_thread = 1;
a9523c6f 458 ret = warnings_fatal;
f356d01d
JA
459 }
460#endif
461
2dc1bbeb 462 if (o->write_iolog_file && o->read_iolog_file) {
076efc7c 463 log_err("fio: read iolog overrides write_iolog\n");
2dc1bbeb
JA
464 free(o->write_iolog_file);
465 o->write_iolog_file = NULL;
a9523c6f 466 ret = warnings_fatal;
076efc7c 467 }
16b462ae 468
16b462ae 469 /*
ed335855 470 * only really works with 1 file
16b462ae 471 */
627aa1a8 472 if (o->zone_size && o->open_files > 1)
2dc1bbeb 473 o->zone_size = 0;
16b462ae 474
ed335855
SN
475 /*
476 * If zone_range isn't specified, backward compatibility dictates it
477 * should be made equal to zone_size.
478 */
479 if (o->zone_size && !o->zone_range)
480 o->zone_range = o->zone_size;
481
16b462ae
JA
482 /*
483 * Reads can do overwrites, we always need to pre-create the file
484 */
485 if (td_read(td) || td_rw(td))
2dc1bbeb 486 o->overwrite = 1;
16b462ae 487
2dc1bbeb 488 if (!o->min_bs[DDIR_READ])
5ec10eaa 489 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
2dc1bbeb
JA
490 if (!o->max_bs[DDIR_READ])
491 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
492 if (!o->min_bs[DDIR_WRITE])
5ec10eaa 493 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
2dc1bbeb
JA
494 if (!o->max_bs[DDIR_WRITE])
495 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
6eaf09d6
SL
496 if (!o->min_bs[DDIR_TRIM])
497 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
498 if (!o->max_bs[DDIR_TRIM])
499 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
500
a00735e6 501
2dc1bbeb 502 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
6eaf09d6 503 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
a00735e6 504
2b7a01d0
JA
505 /*
506 * For random IO, allow blockalign offset other than min_bs.
507 */
508 if (!o->ba[DDIR_READ] || !td_random(td))
509 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
510 if (!o->ba[DDIR_WRITE] || !td_random(td))
511 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
6eaf09d6
SL
512 if (!o->ba[DDIR_TRIM] || !td_random(td))
513 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
2b7a01d0
JA
514
515 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
6eaf09d6
SL
516 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
517 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
9bbf57cc 518 !o->norandommap) {
2b7a01d0 519 log_err("fio: Any use of blockalign= turns off randommap\n");
9bbf57cc 520 o->norandommap = 1;
a9523c6f 521 ret = warnings_fatal;
2b7a01d0
JA
522 }
523
2dc1bbeb
JA
524 if (!o->file_size_high)
525 o->file_size_high = o->file_size_low;
9c60ce64 526
23ed19b0
CE
527 if (o->start_delay_high)
528 o->start_delay = get_rand_start_delay(td);
529
8347239a
JA
530 if (o->norandommap && o->verify != VERIFY_NONE
531 && !fixed_block_size(o)) {
532 log_err("fio: norandommap given for variable block sizes, "
533 "verify disabled\n");
2dc1bbeb 534 o->verify = VERIFY_NONE;
a9523c6f 535 ret = warnings_fatal;
bb8895e0 536 }
2dc1bbeb 537 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
690adba3 538 log_err("fio: bs_unaligned may not work with raw io\n");
e0a22335 539
48097d5c
JA
540 /*
541 * thinktime_spin must be less than thinktime
542 */
2dc1bbeb
JA
543 if (o->thinktime_spin > o->thinktime)
544 o->thinktime_spin = o->thinktime;
e916b390
JA
545
546 /*
547 * The low water mark cannot be bigger than the iodepth
548 */
67bf9823
JA
549 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
550 o->iodepth_low = o->iodepth;
cb5ab512
JA
551
552 /*
553 * If batch number isn't set, default to the same as iodepth
554 */
2dc1bbeb
JA
555 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
556 o->iodepth_batch = o->iodepth;
b5af8293 557
2dc1bbeb
JA
558 if (o->nr_files > td->files_index)
559 o->nr_files = td->files_index;
9f9214f2 560
2dc1bbeb
JA
561 if (o->open_files > o->nr_files || !o->open_files)
562 o->open_files = o->nr_files;
4e991c23 563
6eaf09d6
SL
564 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
565 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
566 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
567 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
4e991c23 568 log_err("fio: rate and rate_iops are mutually exclusive\n");
a9523c6f 569 ret = 1;
4e991c23 570 }
6eaf09d6
SL
571 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
572 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
573 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
574 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
575 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
576 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
4e991c23 577 log_err("fio: minimum rate exceeds rate\n");
a9523c6f 578 ret = 1;
4e991c23
JA
579 }
580
cf4464ca
JA
581 if (!o->timeout && o->time_based) {
582 log_err("fio: time_based requires a runtime/timeout setting\n");
583 o->time_based = 0;
a9523c6f 584 ret = warnings_fatal;
cf4464ca
JA
585 }
586
aa31f1f1 587 if (o->fill_device && !o->size)
5921e80c 588 o->size = -1ULL;
5ec10eaa 589
9bbf57cc 590 if (o->verify != VERIFY_NONE) {
996936f9 591 if (td_write(td) && o->do_verify && o->numjobs > 1) {
a9523c6f
JA
592 log_info("Multiple writers may overwrite blocks that "
593 "belong to other jobs. This can cause "
594 "verification failures.\n");
595 ret = warnings_fatal;
596 }
597
9bbf57cc 598 o->refill_buffers = 1;
2f1b8e8b
JA
599 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
600 !o->verify_interval)
601 o->verify_interval = o->min_bs[DDIR_WRITE];
d990588e 602 }
41ccd845 603
9bbf57cc
JA
604 if (o->pre_read) {
605 o->invalidate_cache = 0;
a9523c6f 606 if (td->io_ops->flags & FIO_PIPEIO) {
9c0d2241
JA
607 log_info("fio: cannot pre-read files with an IO engine"
608 " that isn't seekable. Pre-read disabled.\n");
a9523c6f
JA
609 ret = warnings_fatal;
610 }
9c0d2241 611 }
34f1c044 612
ad705bcb
SN
613 if (!o->unit_base) {
614 if (td->io_ops->flags & FIO_BIT_BASED)
615 o->unit_base = 1;
616 else
617 o->unit_base = 8;
618 }
619
67bf9823 620#ifndef CONFIG_FDATASYNC
9bbf57cc 621 if (o->fdatasync_blocks) {
e72fa4d4
JA
622 log_info("fio: this platform does not support fdatasync()"
623 " falling back to using fsync(). Use the 'fsync'"
624 " option instead of 'fdatasync' to get rid of"
625 " this warning\n");
9bbf57cc
JA
626 o->fsync_blocks = o->fdatasync_blocks;
627 o->fdatasync_blocks = 0;
a9523c6f 628 ret = warnings_fatal;
e72fa4d4
JA
629 }
630#endif
631
93bcfd20
BC
632#ifdef WIN32
633 /*
634 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
635 * so fail if we're passed those flags
636 */
637 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
638 log_err("fio: Windows does not support direct or non-buffered io with"
639 " the synchronous ioengines. Use the 'windowsaio' ioengine"
640 " with 'direct=1' and 'iodepth=1' instead.\n");
641 ret = 1;
642 }
643#endif
644
811ac503
JA
645 /*
646 * For fully compressible data, just zero them at init time.
647 * It's faster than repeatedly filling it.
648 */
649 if (td->o.compress_percentage == 100) {
650 td->o.zero_buffers = 1;
651 td->o.compress_percentage = 0;
652 }
653
5881fda4
JA
654 /*
655 * Using a non-uniform random distribution excludes usage of
656 * a random map
657 */
658 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
659 td->o.norandommap = 1;
660
8d916c94
JA
661 /*
662 * If size is set but less than the min block size, complain
663 */
664 if (o->size && o->size < td_min_bs(td)) {
665 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
666 ret = 1;
667 }
668
d01612f3
CM
669 /*
670 * O_ATOMIC implies O_DIRECT
671 */
672 if (td->o.oatomic)
673 td->o.odirect = 1;
674
04778baf
JA
675 /*
676 * If randseed is set, that overrides randrepeat
677 */
678 if (td->o.rand_seed)
679 td->o.rand_repeatable = 0;
680
b1bebc32
JA
681 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
682 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
683 ret = 1;
684 }
685
a9523c6f 686 return ret;
e1f36503
JA
687}
688
f8977ee6
JA
689/*
690 * This function leaks the buffer
691 */
99d633af 692char *fio_uint_to_kmg(unsigned int val)
f8977ee6
JA
693{
694 char *buf = malloc(32);
f3502ba2 695 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
f8977ee6
JA
696 char *p = post;
697
245142ff 698 do {
f8977ee6
JA
699 if (val & 1023)
700 break;
701
702 val >>= 10;
703 p++;
245142ff 704 } while (*p);
f8977ee6 705
98ffb8f3 706 snprintf(buf, 32, "%u%c", val, *p);
f8977ee6
JA
707 return buf;
708}
709
09629a90
JA
710/* External engines are specified by "external:name.o") */
711static const char *get_engine_name(const char *str)
712{
713 char *p = strstr(str, ":");
714
715 if (!p)
716 return str;
717
718 p++;
719 strip_blank_front(&p);
720 strip_blank_end(p);
721 return p;
722}
723
e132cbae
JA
724static int exists_and_not_file(const char *filename)
725{
726 struct stat sb;
727
728 if (lstat(filename, &sb) == -1)
729 return 0;
730
ecc314ba
BC
731 /* \\.\ is the device namespace in Windows, where every file
732 * is a device node */
733 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
e132cbae
JA
734 return 0;
735
736 return 1;
737}
738
4c07ad86 739static void td_fill_rand_seeds_os(struct thread_data *td)
5bfc35d7 740{
d24945f0
JA
741 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
742 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
743 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
5bfc35d7
JA
744
745 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
d24945f0 746 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
5bfc35d7 747
d24945f0
JA
748 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
749 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
23ed19b0 750 os_random_seed(td->rand_seeds[FIO_RAND_START_DELAY], &td->delay_state);
5bfc35d7
JA
751
752 if (!td_random(td))
753 return;
754
755 if (td->o.rand_repeatable)
d24945f0 756 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
5bfc35d7 757
d24945f0 758 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
d9472271
JA
759
760 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], &td->seq_rand_state[DDIR_READ]);
761 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], &td->seq_rand_state[DDIR_WRITE]);
762 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], &td->seq_rand_state[DDIR_TRIM]);
4c07ad86
JA
763}
764
765static void td_fill_rand_seeds_internal(struct thread_data *td)
766{
d24945f0
JA
767 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
768 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
769 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
4c07ad86
JA
770
771 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
d24945f0 772 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
4c07ad86 773
d24945f0
JA
774 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
775 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
23ed19b0 776 init_rand_seed(&td->__delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
4c07ad86
JA
777
778 if (!td_random(td))
779 return;
780
781 if (td->o.rand_repeatable)
d24945f0 782 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
4c07ad86 783
d24945f0 784 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
d9472271
JA
785 init_rand_seed(&td->__seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
786 init_rand_seed(&td->__seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
787 init_rand_seed(&td->__seq_rand_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF]);
5bfc35d7
JA
788}
789
4c07ad86
JA
790void td_fill_rand_seeds(struct thread_data *td)
791{
56e2a5fc
CE
792 if (td->o.allrand_repeatable) {
793 for (int i = 0; i < FIO_RAND_NR_OFFS; i++)
794 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
795 + i;
796 }
797
4c07ad86
JA
798 if (td->o.use_os_rand)
799 td_fill_rand_seeds_os(td);
800 else
801 td_fill_rand_seeds_internal(td);
3545a109 802
d24945f0 803 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
4c07ad86
JA
804}
805
de890a1e
SL
806/*
807 * Initializes the ioengine configured for a job, if it has not been done so
808 * already.
809 */
810int ioengine_load(struct thread_data *td)
811{
812 const char *engine;
813
814 /*
815 * Engine has already been loaded.
816 */
817 if (td->io_ops)
818 return 0;
6e5c4b8e
JA
819 if (!td->o.ioengine) {
820 log_err("fio: internal fault, no IO engine specified\n");
821 return 1;
822 }
de890a1e
SL
823
824 engine = get_engine_name(td->o.ioengine);
825 td->io_ops = load_ioengine(td, engine);
826 if (!td->io_ops) {
827 log_err("fio: failed to load engine %s\n", engine);
828 return 1;
829 }
830
831 if (td->io_ops->option_struct_size && td->io_ops->options) {
832 /*
833 * In cases where td->eo is set, clone it for a child thread.
834 * This requires that the parent thread has the same ioengine,
835 * but that requirement must be enforced by the code which
836 * cloned the thread.
837 */
838 void *origeo = td->eo;
839 /*
840 * Otherwise use the default thread options.
841 */
842 if (!origeo && td != &def_thread && def_thread.eo &&
843 def_thread.io_ops->options == td->io_ops->options)
844 origeo = def_thread.eo;
845
846 options_init(td->io_ops->options);
847 td->eo = malloc(td->io_ops->option_struct_size);
848 /*
849 * Use the default thread as an option template if this uses the
850 * same options structure and there are non-default options
851 * used.
852 */
853 if (origeo) {
854 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
855 options_mem_dupe(td->eo, td->io_ops->options);
856 } else {
857 memset(td->eo, 0, td->io_ops->option_struct_size);
858 fill_default_options(td->eo, td->io_ops->options);
859 }
860 *(struct thread_data **)td->eo = td;
861 }
862
863 return 0;
864}
865
d72be545
JA
866static void init_flags(struct thread_data *td)
867{
868 struct thread_options *o = &td->o;
869
870 if (o->verify_backlog)
871 td->flags |= TD_F_VER_BACKLOG;
872 if (o->trim_backlog)
873 td->flags |= TD_F_TRIM_BACKLOG;
874 if (o->read_iolog_file)
875 td->flags |= TD_F_READ_IOLOG;
876 if (o->refill_buffers)
877 td->flags |= TD_F_REFILL_BUFFERS;
bedc9dc2 878 if (o->scramble_buffers)
d72be545
JA
879 td->flags |= TD_F_SCRAMBLE_BUFFERS;
880 if (o->verify != VERIFY_NONE)
881 td->flags |= TD_F_VER_NONE;
882}
883
9dbc7bfe
JA
884static int setup_random_seeds(struct thread_data *td)
885{
886 unsigned long seed;
887 unsigned int i;
888
04778baf 889 if (!td->o.rand_repeatable && !td->o.rand_seed)
9dbc7bfe
JA
890 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
891
04778baf
JA
892 if (!td->o.rand_seed)
893 seed = 0x89;
894 else
895 seed = td->o.rand_seed;
896
897 for (i = 0; i < 4; i++)
9dbc7bfe
JA
898 seed *= 0x9e370001UL;
899
900 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
901 td->rand_seeds[i] = seed;
902 seed *= 0x9e370001UL;
903 }
904
905 td_fill_rand_seeds(td);
906 return 0;
907}
908
de98bd30
JA
909enum {
910 FPRE_NONE = 0,
911 FPRE_JOBNAME,
912 FPRE_JOBNUM,
913 FPRE_FILENUM
914};
915
916static struct fpre_keyword {
917 const char *keyword;
918 size_t strlen;
919 int key;
920} fpre_keywords[] = {
921 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
922 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
923 { .keyword = "$filenum", .key = FPRE_FILENUM, },
924 { .keyword = NULL, },
925 };
926
927static char *make_filename(char *buf, struct thread_options *o,
928 const char *jobname, int jobnum, int filenum)
929{
930 struct fpre_keyword *f;
931 char copy[PATH_MAX];
932
933 if (!o->filename_format || !strlen(o->filename_format)) {
934 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
935 return NULL;
936 }
937
938 for (f = &fpre_keywords[0]; f->keyword; f++)
939 f->strlen = strlen(f->keyword);
940
941 strcpy(buf, o->filename_format);
942 memset(copy, 0, sizeof(copy));
943 for (f = &fpre_keywords[0]; f->keyword; f++) {
944 do {
945 size_t pre_len, post_start = 0;
946 char *str, *dst = copy;
947
e25b6d5a 948 str = strcasestr(buf, f->keyword);
de98bd30
JA
949 if (!str)
950 break;
951
952 pre_len = str - buf;
953 if (strlen(str) != f->strlen)
954 post_start = pre_len + f->strlen;
955
956 if (pre_len) {
957 strncpy(dst, buf, pre_len);
958 dst += pre_len;
959 }
960
961 switch (f->key) {
962 case FPRE_JOBNAME:
963 dst += sprintf(dst, "%s", jobname);
964 break;
965 case FPRE_JOBNUM:
966 dst += sprintf(dst, "%d", jobnum);
967 break;
968 case FPRE_FILENUM:
969 dst += sprintf(dst, "%d", filenum);
970 break;
971 default:
972 assert(0);
973 break;
974 }
975
976 if (post_start)
977 strcpy(dst, buf + post_start);
978
979 strcpy(buf, copy);
980 } while (1);
981 }
982
983 return buf;
984}
f9633d72
JA
985
986int parse_dryrun(void)
987{
988 return dump_cmdline || parse_only;
989}
990
906c8d75
JA
991/*
992 * Adds a job to the list of things todo. Sanitizes the various options
993 * to make sure we don't have conflicts, and initializes various
994 * members of td.
995 */
b7cfb2e1 996static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
46bcd498 997 int recursed, int client_type)
ebac4655 998{
af52b345 999 unsigned int i;
af52b345 1000 char fname[PATH_MAX];
e132cbae 1001 int numjobs, file_alloced;
de98bd30 1002 struct thread_options *o = &td->o;
ebac4655 1003
ebac4655
JA
1004 /*
1005 * the def_thread is just for options, it's not a real job
1006 */
1007 if (td == &def_thread)
1008 return 0;
1009
d72be545
JA
1010 init_flags(td);
1011
cca73aa7
JA
1012 /*
1013 * if we are just dumping the output command line, don't add the job
1014 */
f9633d72 1015 if (parse_dryrun()) {
cca73aa7
JA
1016 put_job(td);
1017 return 0;
1018 }
1019
46bcd498
JA
1020 td->client_type = client_type;
1021
58c55ba0 1022 if (profile_td_init(td))
66251554 1023 goto err;
58c55ba0 1024
de890a1e 1025 if (ioengine_load(td))
205927a3 1026 goto err;
df64119d 1027
de98bd30 1028 if (o->odirect)
690adba3
JA
1029 td->io_ops->flags |= FIO_RAWIO;
1030
e132cbae 1031 file_alloced = 0;
de98bd30 1032 if (!o->filename && !td->files_index && !o->read_iolog_file) {
e132cbae 1033 file_alloced = 1;
80be24f4 1034
de98bd30 1035 if (o->nr_files == 1 && exists_and_not_file(jobname))
5903e7b7 1036 add_file(td, jobname, job_add_num, 0);
7b05a215 1037 else {
de98bd30 1038 for (i = 0; i < o->nr_files; i++)
5903e7b7 1039 add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
af52b345 1040 }
0af7b542 1041 }
ebac4655 1042
4e991c23
JA
1043 if (fixup_options(td))
1044 goto err;
e0a22335 1045
9e684a49
DE
1046 flow_init_job(td);
1047
de890a1e
SL
1048 /*
1049 * IO engines only need this for option callbacks, and the address may
1050 * change in subprocesses.
1051 */
1052 if (td->eo)
1053 *(struct thread_data **)td->eo = NULL;
1054
07eb79df
JA
1055 if (td->io_ops->flags & FIO_DISKLESSIO) {
1056 struct fio_file *f;
1057
1058 for_each_file(td, f, i)
1059 f->real_file_size = -1ULL;
1060 }
1061
521da527 1062 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
ebac4655 1063
de98bd30
JA
1064 td->ts.clat_percentiles = o->clat_percentiles;
1065 td->ts.percentile_precision = o->percentile_precision;
1066 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
83349190 1067
6eaf09d6
SL
1068 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1069 td->ts.clat_stat[i].min_val = ULONG_MAX;
1070 td->ts.slat_stat[i].min_val = ULONG_MAX;
1071 td->ts.lat_stat[i].min_val = ULONG_MAX;
1072 td->ts.bw_stat[i].min_val = ULONG_MAX;
1073 }
de98bd30 1074 td->ddir_seq_nr = o->ddir_seq_nr;
ebac4655 1075
de98bd30 1076 if ((o->stonewall || o->new_group) && prev_group_jobs) {
3c5df6fa 1077 prev_group_jobs = 0;
ebac4655 1078 groupid++;
3c5df6fa 1079 }
ebac4655
JA
1080
1081 td->groupid = groupid;
3c5df6fa 1082 prev_group_jobs++;
ebac4655 1083
9dbc7bfe 1084 if (setup_random_seeds(td)) {
93bcfd20 1085 td_verror(td, errno, "init_random_state");
9c60ce64 1086 goto err;
93bcfd20 1087 }
9c60ce64 1088
ebac4655
JA
1089 if (setup_rate(td))
1090 goto err;
1091
8e255779 1092 if (o->lat_log_file || write_lat_log) {
22f80458
JA
1093 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
1094 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
1095 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
ebac4655 1096 }
8e255779 1097 if (o->bw_log_file || write_bw_log)
22f80458
JA
1098 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1099 if (o->iops_log_file)
1100 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
ebac4655 1101
de98bd30
JA
1102 if (!o->name)
1103 o->name = strdup(jobname);
01452055 1104
f3afa57e 1105 if (output_format == FIO_OUTPUT_NORMAL) {
b990b5c0 1106 if (!job_add_num) {
b7cfb2e1 1107 if (is_backend && !recursed)
2f122b13 1108 fio_server_send_add_job(td);
807f9971 1109
0353050f 1110 if (!(td->io_ops->flags & FIO_NOIO)) {
d21e2535
JA
1111 char *c1, *c2, *c3, *c4;
1112 char *c5 = NULL, *c6 = NULL;
f8977ee6 1113
22f80458
JA
1114 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1115 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1116 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1117 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
d21e2535
JA
1118
1119 if (!o->bs_is_seq_rand) {
1120 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1121 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1122 }
1123
1124 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1125 td->groupid,
1126 ddir_str(o->td_ddir));
1127
1128 if (o->bs_is_seq_rand)
1129 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1130 c1, c2, c3, c4);
1131 else
1132 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1133 c1, c2, c3, c4, c5, c6);
1134
1135 log_info("ioengine=%s, iodepth=%u\n",
de98bd30 1136 td->io_ops->name, o->iodepth);
f8977ee6
JA
1137
1138 free(c1);
1139 free(c2);
1140 free(c3);
1141 free(c4);
6eaf09d6
SL
1142 free(c5);
1143 free(c6);
f8977ee6 1144 }
b990b5c0 1145 } else if (job_add_num == 1)
6d86144d 1146 log_info("...\n");
c6ae0a5b 1147 }
ebac4655
JA
1148
1149 /*
1150 * recurse add identical jobs, clear numjobs and stonewall options
1151 * as they don't apply to sub-jobs
1152 */
de98bd30 1153 numjobs = o->numjobs;
ebac4655 1154 while (--numjobs) {
de890a1e 1155 struct thread_data *td_new = get_new_job(0, td, 1);
ebac4655
JA
1156
1157 if (!td_new)
1158 goto err;
1159
2dc1bbeb
JA
1160 td_new->o.numjobs = 1;
1161 td_new->o.stonewall = 0;
92c1d41f 1162 td_new->o.new_group = 0;
e132cbae
JA
1163
1164 if (file_alloced) {
455a50fa
CE
1165 if (td_new->files) {
1166 struct fio_file *f;
1167 for_each_file(td_new, f, i) {
1168 if (f->file_name)
ece6d647
AK
1169 sfree(f->file_name);
1170 sfree(f);
455a50fa 1171 }
ece6d647 1172 free(td_new->files);
455a50fa
CE
1173 td_new->files = NULL;
1174 }
ece6d647
AK
1175 td_new->files_index = 0;
1176 td_new->files_size = 0;
455a50fa
CE
1177 if (td_new->o.filename) {
1178 free(td_new->o.filename);
1179 td_new->o.filename = NULL;
1180 }
e132cbae
JA
1181 }
1182
bcbfeefa 1183 if (add_job(td_new, jobname, numjobs, 1, client_type))
ebac4655
JA
1184 goto err;
1185 }
3c5df6fa 1186
ebac4655
JA
1187 return 0;
1188err:
1189 put_job(td);
1190 return -1;
1191}
1192
79d16311
JA
1193/*
1194 * Parse as if 'o' was a command line
1195 */
46bcd498 1196void add_job_opts(const char **o, int client_type)
79d16311
JA
1197{
1198 struct thread_data *td, *td_parent;
1199 int i, in_global = 1;
1200 char jobname[32];
1201
1202 i = 0;
1203 td_parent = td = NULL;
1204 while (o[i]) {
1205 if (!strncmp(o[i], "name", 4)) {
1206 in_global = 0;
1207 if (td)
46bcd498 1208 add_job(td, jobname, 0, 0, client_type);
79d16311
JA
1209 td = NULL;
1210 sprintf(jobname, "%s", o[i] + 5);
1211 }
1212 if (in_global && !td_parent)
de890a1e 1213 td_parent = get_new_job(1, &def_thread, 0);
79d16311
JA
1214 else if (!in_global && !td) {
1215 if (!td_parent)
1216 td_parent = &def_thread;
de890a1e 1217 td = get_new_job(0, td_parent, 0);
79d16311
JA
1218 }
1219 if (in_global)
292cc475 1220 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
79d16311 1221 else
292cc475 1222 fio_options_parse(td, (char **) &o[i], 1, 0);
79d16311
JA
1223 i++;
1224 }
1225
1226 if (td)
46bcd498 1227 add_job(td, jobname, 0, 0, client_type);
79d16311
JA
1228}
1229
01f06b63
JA
1230static int skip_this_section(const char *name)
1231{
ad0a2735
JA
1232 int i;
1233
1234 if (!nr_job_sections)
01f06b63
JA
1235 return 0;
1236 if (!strncmp(name, "global", 6))
1237 return 0;
1238
ad0a2735
JA
1239 for (i = 0; i < nr_job_sections; i++)
1240 if (!strcmp(job_sections[i], name))
1241 return 0;
1242
1243 return 1;
01f06b63
JA
1244}
1245
ebac4655
JA
1246static int is_empty_or_comment(char *line)
1247{
1248 unsigned int i;
1249
1250 for (i = 0; i < strlen(line); i++) {
1251 if (line[i] == ';')
1252 return 1;
5cc2da30
IM
1253 if (line[i] == '#')
1254 return 1;
76cd9378 1255 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
ebac4655
JA
1256 return 0;
1257 }
1258
1259 return 1;
1260}
1261
07261983
JA
1262/*
1263 * This is our [ini] type file parser.
1264 */
46bcd498 1265int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
ebac4655 1266{
e1f36503 1267 unsigned int global;
ebac4655 1268 struct thread_data *td;
fee3bb48 1269 char *string, *name;
ebac4655
JA
1270 FILE *f;
1271 char *p;
0c7e37a0 1272 int ret = 0, stonewall;
097b2991 1273 int first_sect = 1;
ccf8f127 1274 int skip_fgets = 0;
01f06b63 1275 int inside_skip = 0;
3b8b7135
JA
1276 char **opts;
1277 int i, alloc_opts, num_opts;
ebac4655 1278
50d16976
JA
1279 if (is_buf)
1280 f = NULL;
1281 else {
1282 if (!strcmp(file, "-"))
1283 f = stdin;
1284 else
1285 f = fopen(file, "r");
5a729cbe 1286
50d16976
JA
1287 if (!f) {
1288 perror("fopen job file");
1289 return 1;
1290 }
ebac4655
JA
1291 }
1292
1293 string = malloc(4096);
7f7e6e59
JA
1294
1295 /*
1296 * it's really 256 + small bit, 280 should suffice
1297 */
1298 name = malloc(280);
1299 memset(name, 0, 280);
ebac4655 1300
3b8b7135
JA
1301 alloc_opts = 8;
1302 opts = malloc(sizeof(char *) * alloc_opts);
b7c6302d 1303 num_opts = 0;
3b8b7135 1304
0c7e37a0 1305 stonewall = stonewall_flag;
7c124ac1 1306 do {
ccf8f127
JA
1307 /*
1308 * if skip_fgets is set, we already have loaded a line we
1309 * haven't handled.
1310 */
1311 if (!skip_fgets) {
50d16976
JA
1312 if (is_buf)
1313 p = strsep(&file, "\n");
1314 else
43f74792 1315 p = fgets(string, 4096, f);
ccf8f127
JA
1316 if (!p)
1317 break;
1318 }
cdc7f193 1319
ccf8f127 1320 skip_fgets = 0;
cdc7f193 1321 strip_blank_front(&p);
6c7c7da1 1322 strip_blank_end(p);
cdc7f193 1323
ebac4655
JA
1324 if (is_empty_or_comment(p))
1325 continue;
84dd1886 1326 if (sscanf(p, "[%255[^\n]]", name) != 1) {
01f06b63
JA
1327 if (inside_skip)
1328 continue;
5ec10eaa
JA
1329 log_err("fio: option <%s> outside of [] job section\n",
1330 p);
088b4207 1331 break;
6c7c7da1 1332 }
ebac4655 1333
7a4b80a1
JA
1334 name[strlen(name) - 1] = '\0';
1335
01f06b63
JA
1336 if (skip_this_section(name)) {
1337 inside_skip = 1;
1338 continue;
1339 } else
1340 inside_skip = 0;
1341
ebac4655
JA
1342 global = !strncmp(name, "global", 6);
1343
cca73aa7 1344 if (dump_cmdline) {
097b2991
JA
1345 if (first_sect)
1346 log_info("fio ");
cca73aa7
JA
1347 if (!global)
1348 log_info("--name=%s ", name);
097b2991 1349 first_sect = 0;
cca73aa7
JA
1350 }
1351
de890a1e 1352 td = get_new_job(global, &def_thread, 0);
45410acb
JA
1353 if (!td) {
1354 ret = 1;
1355 break;
1356 }
ebac4655 1357
972cfd25 1358 /*
de8f6de9 1359 * Separate multiple job files by a stonewall
972cfd25 1360 */
f9481919 1361 if (!global && stonewall) {
2dc1bbeb 1362 td->o.stonewall = stonewall;
972cfd25
JA
1363 stonewall = 0;
1364 }
1365
3b8b7135
JA
1366 num_opts = 0;
1367 memset(opts, 0, alloc_opts * sizeof(char *));
1368
50d16976
JA
1369 while (1) {
1370 if (is_buf)
1371 p = strsep(&file, "\n");
1372 else
1373 p = fgets(string, 4096, f);
1374 if (!p)
1375 break;
1376
ebac4655
JA
1377 if (is_empty_or_comment(p))
1378 continue;
e1f36503 1379
b6754f9d 1380 strip_blank_front(&p);
7c124ac1 1381
ccf8f127
JA
1382 /*
1383 * new section, break out and make sure we don't
1384 * fgets() a new line at the top.
1385 */
1386 if (p[0] == '[') {
1387 skip_fgets = 1;
7c124ac1 1388 break;
ccf8f127 1389 }
7c124ac1 1390
4ae3f763 1391 strip_blank_end(p);
aea47d44 1392
3b8b7135
JA
1393 if (num_opts == alloc_opts) {
1394 alloc_opts <<= 1;
1395 opts = realloc(opts,
1396 alloc_opts * sizeof(char *));
1397 }
1398
1399 opts[num_opts] = strdup(p);
1400 num_opts++;
ebac4655 1401 }
ebac4655 1402
292cc475
JA
1403 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1404 if (!ret)
46bcd498 1405 ret = add_job(td, name, 0, 0, type);
292cc475 1406 else {
b1508cf9
JA
1407 log_err("fio: job %s dropped\n", name);
1408 put_job(td);
45410acb 1409 }
3b8b7135
JA
1410
1411 for (i = 0; i < num_opts; i++)
1412 free(opts[i]);
1413 num_opts = 0;
7c124ac1 1414 } while (!ret);
ebac4655 1415
cca73aa7
JA
1416 if (dump_cmdline)
1417 log_info("\n");
1418
7e356b2d
JA
1419 i = 0;
1420 while (i < nr_job_sections) {
1421 free(job_sections[i]);
1422 i++;
1423 }
1424
3b8b7135
JA
1425 for (i = 0; i < num_opts; i++)
1426 free(opts[i]);
1427
ebac4655
JA
1428 free(string);
1429 free(name);
2577594d 1430 free(opts);
50d16976 1431 if (!is_buf && f != stdin)
5a729cbe 1432 fclose(f);
45410acb 1433 return ret;
ebac4655
JA
1434}
1435
1436static int fill_def_thread(void)
1437{
1438 memset(&def_thread, 0, sizeof(def_thread));
1439
375b2695 1440 fio_getaffinity(getpid(), &def_thread.o.cpumask);
8c029376 1441 def_thread.o.timeout = def_timeout;
8b28bd41 1442 def_thread.o.error_dump = 1;
ebac4655 1443 /*
ee738499 1444 * fill default options
ebac4655 1445 */
214e1eca 1446 fio_fill_default_options(&def_thread);
ebac4655
JA
1447 return 0;
1448}
1449
45378b35 1450static void usage(const char *name)
4785f995 1451{
3d43382c 1452 printf("%s\n", fio_version_string);
45378b35 1453 printf("%s [options] [job options] <job file(s)>\n", name);
83881283
JA
1454 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1455 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
3e260a46 1456 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
111e032d 1457 printf(" --parse-only\t\tParse options only, don't start any IO\n");
83881283 1458 printf(" --output\t\tWrite output to file\n");
b2cecdc2 1459 printf(" --runtime\t\tRuntime in seconds\n");
83881283
JA
1460 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1461 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1462 printf(" --minimal\t\tMinimal (terse) output\n");
f3afa57e 1463 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
75db6e2c 1464 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
f3afa57e 1465 printf(" --version\t\tPrint version info and exit\n");
83881283 1466 printf(" --help\t\tPrint this page\n");
23893646 1467 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
fec0f21c 1468 printf(" --crctest\t\tTest speed of checksum functions\n");
83881283 1469 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
5ec10eaa 1470 " them\n");
de890a1e
SL
1471 printf(" --enghelp=engine\tPrint ioengine help, or list"
1472 " available ioengines\n");
1473 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1474 " cmd\n");
83881283
JA
1475 printf(" --showcmd\t\tTurn a job file into command line options\n");
1476 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1477 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
e382e661
JA
1478 printf(" --eta-newline=time\tForce a new line for every 'time'");
1479 printf(" period passed\n");
06464907
JA
1480 printf(" --status-interval=t\tForce full status dump every");
1481 printf(" 't' period passed\n");
83881283 1482 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
5ec10eaa 1483 " writes\n");
83881283
JA
1484 printf(" --section=name\tOnly run specified section in job file\n");
1485 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
2b386d25 1486 " (def 1024)\n");
83881283
JA
1487 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1488 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1489 printf(" --server=args\t\tStart a backend fio server\n");
1490 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1491 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
f2a2ce0e
HL
1492 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1493 "\t\t\t(option=system,percpu) or run unit work\n"
1494 "\t\t\tcalibration only (option=calibrate)\n");
aa58d252 1495 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
bfca2c74
JA
1496 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1497 printf("\n Jens Axboe <axboe@fb.com>\n");
4785f995
JA
1498}
1499
79e48f72 1500#ifdef FIO_INC_DEBUG
ee56ad50 1501struct debug_level debug_levels[] = {
0b8d11ed
JA
1502 { .name = "process",
1503 .help = "Process creation/exit logging",
1504 .shift = FD_PROCESS,
1505 },
1506 { .name = "file",
1507 .help = "File related action logging",
1508 .shift = FD_FILE,
1509 },
1510 { .name = "io",
1511 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1512 .shift = FD_IO,
1513 },
1514 { .name = "mem",
1515 .help = "Memory allocation/freeing logging",
1516 .shift = FD_MEM,
1517 },
1518 { .name = "blktrace",
1519 .help = "blktrace action logging",
1520 .shift = FD_BLKTRACE,
1521 },
1522 { .name = "verify",
1523 .help = "IO verification action logging",
1524 .shift = FD_VERIFY,
1525 },
1526 { .name = "random",
1527 .help = "Random generation logging",
1528 .shift = FD_RANDOM,
1529 },
1530 { .name = "parse",
1531 .help = "Parser logging",
1532 .shift = FD_PARSE,
1533 },
1534 { .name = "diskutil",
1535 .help = "Disk utility logging actions",
1536 .shift = FD_DISKUTIL,
1537 },
1538 { .name = "job",
1539 .help = "Logging related to creating/destroying jobs",
1540 .shift = FD_JOB,
1541 },
1542 { .name = "mutex",
1543 .help = "Mutex logging",
1544 .shift = FD_MUTEX
1545 },
1546 { .name = "profile",
1547 .help = "Logging related to profiles",
1548 .shift = FD_PROFILE,
1549 },
1550 { .name = "time",
1551 .help = "Logging related to time keeping functions",
1552 .shift = FD_TIME,
1553 },
1554 { .name = "net",
1555 .help = "Network logging",
1556 .shift = FD_NET,
1557 },
3e260a46
JA
1558 { .name = "rate",
1559 .help = "Rate logging",
1560 .shift = FD_RATE,
1561 },
02444ad1 1562 { .name = NULL, },
ee56ad50
JA
1563};
1564
c09823ab 1565static int set_debug(const char *string)
ee56ad50
JA
1566{
1567 struct debug_level *dl;
1568 char *p = (char *) string;
1569 char *opt;
1570 int i;
1571
1572 if (!strcmp(string, "?") || !strcmp(string, "help")) {
ee56ad50
JA
1573 log_info("fio: dumping debug options:");
1574 for (i = 0; debug_levels[i].name; i++) {
1575 dl = &debug_levels[i];
1576 log_info("%s,", dl->name);
1577 }
bd6f78b2 1578 log_info("all\n");
c09823ab 1579 return 1;
ee56ad50
JA
1580 }
1581
1582 while ((opt = strsep(&p, ",")) != NULL) {
1583 int found = 0;
1584
5e1d306e
JA
1585 if (!strncmp(opt, "all", 3)) {
1586 log_info("fio: set all debug options\n");
1587 fio_debug = ~0UL;
1588 continue;
1589 }
1590
ee56ad50
JA
1591 for (i = 0; debug_levels[i].name; i++) {
1592 dl = &debug_levels[i];
5e1d306e
JA
1593 found = !strncmp(opt, dl->name, strlen(dl->name));
1594 if (!found)
1595 continue;
1596
1597 if (dl->shift == FD_JOB) {
1598 opt = strchr(opt, ':');
1599 if (!opt) {
1600 log_err("fio: missing job number\n");
1601 break;
1602 }
1603 opt++;
1604 fio_debug_jobno = atoi(opt);
1605 log_info("fio: set debug jobno %d\n",
1606 fio_debug_jobno);
1607 } else {
ee56ad50 1608 log_info("fio: set debug option %s\n", opt);
bd6f78b2 1609 fio_debug |= (1UL << dl->shift);
ee56ad50 1610 }
5e1d306e 1611 break;
ee56ad50
JA
1612 }
1613
1614 if (!found)
1615 log_err("fio: debug mask %s not found\n", opt);
1616 }
c09823ab 1617 return 0;
ee56ad50 1618}
79e48f72 1619#else
69b98d4c 1620static int set_debug(const char *string)
79e48f72
JA
1621{
1622 log_err("fio: debug tracing not included in build\n");
c09823ab 1623 return 1;
79e48f72
JA
1624}
1625#endif
9ac8a797 1626
4c6107ff
JA
1627static void fio_options_fill_optstring(void)
1628{
1629 char *ostr = cmd_optstr;
1630 int i, c;
1631
1632 c = i = 0;
1633 while (l_opts[i].name) {
1634 ostr[c++] = l_opts[i].val;
1635 if (l_opts[i].has_arg == required_argument)
1636 ostr[c++] = ':';
1637 else if (l_opts[i].has_arg == optional_argument) {
1638 ostr[c++] = ':';
1639 ostr[c++] = ':';
1640 }
1641 i++;
1642 }
1643 ostr[c] = '\0';
1644}
1645
c2c94585
JA
1646static int client_flag_set(char c)
1647{
1648 int i;
1649
1650 i = 0;
1651 while (l_opts[i].name) {
1652 int val = l_opts[i].val;
1653
1654 if (c == (val & 0xff))
1655 return (val & FIO_CLIENT_FLAG);
1656
1657 i++;
1658 }
1659
1660 return 0;
1661}
1662
10aa136b 1663static void parse_cmd_client(void *client, char *opt)
7a4b8240 1664{
fa2ea806 1665 fio_client_add_cmd_option(client, opt);
7a4b8240
JA
1666}
1667
46bcd498 1668int parse_cmd_line(int argc, char *argv[], int client_type)
ebac4655 1669{
b4692828 1670 struct thread_data *td = NULL;
c09823ab 1671 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
4c6107ff 1672 char *ostr = cmd_optstr;
402668f3 1673 void *pid_file = NULL;
9bae26e8 1674 void *cur_client = NULL;
81179eec 1675 int backend = 0;
ebac4655 1676
c2c94585
JA
1677 /*
1678 * Reset optind handling, since we may call this multiple times
1679 * for the backend.
1680 */
1681 optind = 1;
7a4b8240 1682
c2c94585 1683 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
085399db
JA
1684 did_arg = 1;
1685
c2c94585 1686 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
fa2ea806 1687 parse_cmd_client(cur_client, argv[optind - 1]);
7a4b8240
JA
1688 c &= ~FIO_CLIENT_FLAG;
1689 }
1690
ebac4655 1691 switch (c) {
2b386d25
JA
1692 case 'a':
1693 smalloc_pool_size = atoi(optarg);
1694 break;
b4692828 1695 case 't':
8c029376 1696 def_timeout = atoi(optarg);
b4692828
JA
1697 break;
1698 case 'l':
1699 write_lat_log = 1;
1700 break;
3d73e5a9 1701 case 'b':
b4692828
JA
1702 write_bw_log = 1;
1703 break;
1704 case 'o':
1705 f_out = fopen(optarg, "w+");
1706 if (!f_out) {
1707 perror("fopen output");
1708 exit(1);
1709 }
1710 f_err = f_out;
1711 break;
1712 case 'm':
f3afa57e 1713 output_format = FIO_OUTPUT_TERSE;
b4692828 1714 break;
f3afa57e 1715 case 'F':
d10983d7
SH
1716 if (!optarg) {
1717 log_err("fio: missing --output-format argument\n");
1718 exit_val = 1;
1719 do_exit++;
1720 break;
1721 }
f3afa57e
JA
1722 if (!strcmp(optarg, "minimal") ||
1723 !strcmp(optarg, "terse") ||
1724 !strcmp(optarg, "csv"))
1725 output_format = FIO_OUTPUT_TERSE;
1726 else if (!strcmp(optarg, "json"))
1727 output_format = FIO_OUTPUT_JSON;
1728 else
1729 output_format = FIO_OUTPUT_NORMAL;
b4692828 1730 break;
2b8c71b0
CE
1731 case 'f':
1732 append_terse_output = 1;
1733 break;
b4692828 1734 case 'h':
7874f8b7 1735 if (!cur_client) {
c2c94585 1736 usage(argv[0]);
7874f8b7
JA
1737 do_exit++;
1738 }
c2c94585 1739 break;
fd28ca49 1740 case 'c':
7874f8b7 1741 if (!cur_client) {
c2c94585 1742 fio_show_option_help(optarg);
7874f8b7
JA
1743 do_exit++;
1744 }
c2c94585 1745 break;
de890a1e
SL
1746 case 'i':
1747 if (!cur_client) {
1748 fio_show_ioengine_help(optarg);
1749 do_exit++;
1750 }
1751 break;
cca73aa7
JA
1752 case 's':
1753 dump_cmdline = 1;
1754 break;
724e4435
JA
1755 case 'r':
1756 read_only = 1;
1757 break;
b4692828 1758 case 'v':
7874f8b7 1759 if (!cur_client) {
3d43382c 1760 log_info("%s\n", fio_version_string);
7874f8b7
JA
1761 do_exit++;
1762 }
c2c94585 1763 break;
f57a9c59
JA
1764 case 'V':
1765 terse_version = atoi(optarg);
09786f5f
JA
1766 if (!(terse_version == 2 || terse_version == 3 ||
1767 terse_version == 4)) {
f57a9c59
JA
1768 log_err("fio: bad terse version format\n");
1769 exit_val = 1;
1770 do_exit++;
1771 }
1772 break;
e592a06b
AC
1773 case 'e':
1774 if (!strcmp("always", optarg))
1775 eta_print = FIO_ETA_ALWAYS;
1776 else if (!strcmp("never", optarg))
1777 eta_print = FIO_ETA_NEVER;
1778 break;
e382e661
JA
1779 case 'E': {
1780 long long t = 0;
1781
0de5b26f 1782 if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
e382e661
JA
1783 log_err("fio: failed parsing eta time %s\n", optarg);
1784 exit_val = 1;
1785 do_exit++;
1786 }
1787 eta_new_line = t;
1788 break;
1789 }
ee56ad50 1790 case 'd':
c09823ab
JA
1791 if (set_debug(optarg))
1792 do_exit++;
ee56ad50 1793 break;
111e032d
JA
1794 case 'P':
1795 parse_only = 1;
1796 break;
ad0a2735
JA
1797 case 'x': {
1798 size_t new_size;
1799
01f06b63 1800 if (!strcmp(optarg, "global")) {
5ec10eaa
JA
1801 log_err("fio: can't use global as only "
1802 "section\n");
c09823ab
JA
1803 do_exit++;
1804 exit_val = 1;
01f06b63
JA
1805 break;
1806 }
ad0a2735
JA
1807 new_size = (nr_job_sections + 1) * sizeof(char *);
1808 job_sections = realloc(job_sections, new_size);
1809 job_sections[nr_job_sections] = strdup(optarg);
1810 nr_job_sections++;
01f06b63 1811 break;
ad0a2735 1812 }
9ac8a797 1813 case 'p':
07b3232d 1814 exec_profile = strdup(optarg);
9ac8a797 1815 break;
b4692828 1816 case FIO_GETOPT_JOB: {
5ec10eaa 1817 const char *opt = l_opts[lidx].name;
b4692828
JA
1818 char *val = optarg;
1819
c2b1e753 1820 if (!strncmp(opt, "name", 4) && td) {
46bcd498 1821 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
66251554 1822 if (ret)
c2b1e753 1823 return 0;
c2b1e753
JA
1824 td = NULL;
1825 }
b4692828 1826 if (!td) {
01f06b63 1827 int is_section = !strncmp(opt, "name", 4);
3106f220
JA
1828 int global = 0;
1829
01f06b63 1830 if (!is_section || !strncmp(val, "global", 6))
3106f220 1831 global = 1;
c2b1e753 1832
01f06b63
JA
1833 if (is_section && skip_this_section(val))
1834 continue;
1835
de890a1e
SL
1836 td = get_new_job(global, &def_thread, 1);
1837 if (!td || ioengine_load(td))
b4692828 1838 return 0;
de890a1e 1839 fio_options_set_ioengine_opts(l_opts, td);
b4692828 1840 }
38d0adb0 1841
9d918187
JA
1842 if ((!val || !strlen(val)) &&
1843 l_opts[lidx].has_arg == required_argument) {
1844 log_err("fio: option %s requires an argument\n", opt);
1845 ret = 1;
1846 } else
1847 ret = fio_cmd_option_parse(td, opt, val);
1848
bfb3ea22
JA
1849 if (ret) {
1850 if (td) {
1851 put_job(td);
1852 td = NULL;
1853 }
a88c8c14 1854 do_exit++;
bfb3ea22 1855 }
de890a1e
SL
1856
1857 if (!ret && !strcmp(opt, "ioengine")) {
1858 free_ioengine(td);
1859 if (ioengine_load(td))
1860 return 0;
1861 fio_options_set_ioengine_opts(l_opts, td);
1862 }
1863 break;
1864 }
1865 case FIO_GETOPT_IOENGINE: {
1866 const char *opt = l_opts[lidx].name;
1867 char *val = optarg;
de890a1e 1868 ret = fio_cmd_ioengine_option_parse(td, opt, val);
b4692828
JA
1869 break;
1870 }
a9523c6f
JA
1871 case 'w':
1872 warnings_fatal = 1;
1873 break;
fca70358
JA
1874 case 'j':
1875 max_jobs = atoi(optarg);
1876 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1877 log_err("fio: invalid max jobs: %d\n", max_jobs);
1878 do_exit++;
1879 exit_val = 1;
1880 }
1881 break;
50d16976 1882 case 'S':
a37f69b7 1883 if (nr_clients) {
132159a5
JA
1884 log_err("fio: can't be both client and server\n");
1885 do_exit++;
1886 exit_val = 1;
1887 break;
1888 }
87aa8f19 1889 if (optarg)
bebe6398 1890 fio_server_set_arg(optarg);
50d16976 1891 is_backend = 1;
81179eec 1892 backend = 1;
50d16976 1893 break;
e46d8091 1894 case 'D':
402668f3 1895 pid_file = strdup(optarg);
e46d8091 1896 break;
f2a2ce0e
HL
1897 case 'I':
1898 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1899 /* exit on error and calibration only */
1900 do_exit++;
1901 if (ret == -1)
1902 exit_val = 1;
1903 }
1904 break;
132159a5
JA
1905 case 'C':
1906 if (is_backend) {
1907 log_err("fio: can't be both client and server\n");
1908 do_exit++;
1909 exit_val = 1;
1910 break;
1911 }
a5276616 1912 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
bebe6398
JA
1913 log_err("fio: failed adding client %s\n", optarg);
1914 do_exit++;
1915 exit_val = 1;
1916 break;
1917 }
14ea90ed
JA
1918 /*
1919 * If the next argument exists and isn't an option,
1920 * assume it's a job file for this client only.
1921 */
1922 while (optind < argc) {
1923 if (!strncmp(argv[optind], "--", 2) ||
1924 !strncmp(argv[optind], "-", 1))
1925 break;
1926
1927 fio_client_add_ini_file(cur_client, argv[optind]);
1928 optind++;
1929 }
132159a5 1930 break;
7d11f871
JA
1931 case 'T':
1932 do_exit++;
1933 exit_val = fio_monotonic_clocktest();
1934 break;
fec0f21c
JA
1935 case 'G':
1936 do_exit++;
1937 exit_val = fio_crctest(optarg);
1938 break;
06464907
JA
1939 case 'L': {
1940 long long val;
1941
7cd0ec5d 1942 if (check_str_time(optarg, &val, 0)) {
06464907
JA
1943 log_err("fio: failed parsing time %s\n", optarg);
1944 do_exit++;
1945 exit_val = 1;
1946 break;
1947 }
1948 status_interval = val * 1000;
1949 break;
1950 }
798827c8
JA
1951 case '?':
1952 log_err("%s: unrecognized option '%s'\n", argv[0],
1953 argv[optind - 1]);
b4692828 1954 default:
c09823ab
JA
1955 do_exit++;
1956 exit_val = 1;
b4692828 1957 break;
ebac4655 1958 }
c7d5c941
JA
1959 if (do_exit)
1960 break;
ebac4655 1961 }
c9fad893 1962
0b14f0a8
JA
1963 if (do_exit && !(is_backend || nr_clients))
1964 exit(exit_val);
536582bf 1965
fb296043
JA
1966 if (nr_clients && fio_clients_connect())
1967 exit(1);
132159a5 1968
81179eec 1969 if (is_backend && backend)
402668f3 1970 return fio_start_server(pid_file);
50d16976 1971
b4692828 1972 if (td) {
7d6a8904 1973 if (!ret)
46bcd498 1974 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
972cfd25 1975 }
774a6177 1976
7874f8b7 1977 while (!ret && optind < argc) {
b4692828
JA
1978 ini_idx++;
1979 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1980 ini_file[ini_idx - 1] = strdup(argv[optind]);
1981 optind++;
eb8bbf48 1982 }
972cfd25
JA
1983
1984 return ini_idx;
ebac4655
JA
1985}
1986
0420ba6a 1987int fio_init_options(void)
ebac4655 1988{
b4692828
JA
1989 f_out = stdout;
1990 f_err = stderr;
1991
4c6107ff 1992 fio_options_fill_optstring();
5ec10eaa 1993 fio_options_dup_and_init(l_opts);
b4692828 1994
9d9eb2e7
JA
1995 atexit(free_shm);
1996
ebac4655
JA
1997 if (fill_def_thread())
1998 return 1;
1999
0420ba6a
JA
2000 return 0;
2001}
2002
51167799
JA
2003extern int fio_check_options(struct thread_options *);
2004
0420ba6a
JA
2005int parse_options(int argc, char *argv[])
2006{
46bcd498 2007 const int type = FIO_CLIENT_TYPE_CLI;
0420ba6a
JA
2008 int job_files, i;
2009
2010 if (fio_init_options())
2011 return 1;
51167799
JA
2012 if (fio_test_cconv(&def_thread.o))
2013 log_err("fio: failed internal cconv test\n");
0420ba6a 2014
46bcd498 2015 job_files = parse_cmd_line(argc, argv, type);
ebac4655 2016
cdf54d85
JA
2017 if (job_files > 0) {
2018 for (i = 0; i < job_files; i++) {
2019 if (fill_def_thread())
132159a5 2020 return 1;
cdf54d85
JA
2021 if (nr_clients) {
2022 if (fio_clients_send_ini(ini_file[i]))
2023 return 1;
2024 free(ini_file[i]);
2025 } else if (!is_backend) {
46bcd498 2026 if (parse_jobs_ini(ini_file[i], 0, i, type))
cdf54d85
JA
2027 return 1;
2028 free(ini_file[i]);
2029 }
132159a5 2030 }
14ea90ed
JA
2031 } else if (nr_clients) {
2032 if (fill_def_thread())
2033 return 1;
2034 if (fio_clients_send_ini(NULL))
2035 return 1;
972cfd25 2036 }
ebac4655 2037
88c6ed80 2038 free(ini_file);
7e356b2d 2039 fio_options_free(&def_thread);
bcbfeefa 2040 filesetup_mem_free();
b4692828
JA
2041
2042 if (!thread_number) {
f9633d72 2043 if (parse_dryrun())
cca73aa7 2044 return 0;
07b3232d
JA
2045 if (exec_profile)
2046 return 0;
a37f69b7 2047 if (is_backend || nr_clients)
5c341e9a 2048 return 0;
085399db
JA
2049 if (did_arg)
2050 return 0;
cca73aa7 2051
d65e11c6 2052 log_err("No jobs(s) defined\n\n");
085399db
JA
2053
2054 if (!did_arg) {
2055 usage(argv[0]);
2056 return 1;
2057 }
2058
2059 return 0;
b4692828
JA
2060 }
2061
be4ecfdf
JA
2062 if (def_thread.o.gtod_offload) {
2063 fio_gtod_init();
2064 fio_gtod_offload = 1;
2065 fio_gtod_cpu = def_thread.o.gtod_cpu;
2066 }
2067
f3afa57e 2068 if (output_format == FIO_OUTPUT_NORMAL)
3d43382c 2069 log_info("%s\n", fio_version_string);
f6dea4d3 2070
ebac4655
JA
2071 return 0;
2072}
588b7f09
JA
2073
2074void options_default_fill(struct thread_options *o)
2075{
2076 memcpy(o, &def_thread.o, sizeof(*o));
2077}