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