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