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