Add a typecheck for the endianness conversions
[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) {
ae588852
JA
1149 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT,
1150 o->log_offset);
1151 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT,
1152 o->log_offset);
1153 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT,
1154 o->log_offset);
ebac4655 1155 }
8e255779 1156 if (o->bw_log_file || write_bw_log)
ae588852
JA
1157 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW,
1158 o->log_offset);
22f80458 1159 if (o->iops_log_file)
ae588852
JA
1160 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS,
1161 o->log_offset);
ebac4655 1162
de98bd30
JA
1163 if (!o->name)
1164 o->name = strdup(jobname);
01452055 1165
f3afa57e 1166 if (output_format == FIO_OUTPUT_NORMAL) {
b990b5c0 1167 if (!job_add_num) {
b7cfb2e1 1168 if (is_backend && !recursed)
2f122b13 1169 fio_server_send_add_job(td);
807f9971 1170
0353050f 1171 if (!(td->io_ops->flags & FIO_NOIO)) {
d21e2535
JA
1172 char *c1, *c2, *c3, *c4;
1173 char *c5 = NULL, *c6 = NULL;
f8977ee6 1174
22f80458
JA
1175 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1176 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1177 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1178 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
d21e2535
JA
1179
1180 if (!o->bs_is_seq_rand) {
1181 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1182 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1183 }
1184
1185 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1186 td->groupid,
1187 ddir_str(o->td_ddir));
1188
1189 if (o->bs_is_seq_rand)
1190 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1191 c1, c2, c3, c4);
1192 else
1193 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1194 c1, c2, c3, c4, c5, c6);
1195
1196 log_info("ioengine=%s, iodepth=%u\n",
de98bd30 1197 td->io_ops->name, o->iodepth);
f8977ee6
JA
1198
1199 free(c1);
1200 free(c2);
1201 free(c3);
1202 free(c4);
6eaf09d6
SL
1203 free(c5);
1204 free(c6);
f8977ee6 1205 }
b990b5c0 1206 } else if (job_add_num == 1)
6d86144d 1207 log_info("...\n");
c6ae0a5b 1208 }
ebac4655
JA
1209
1210 /*
1211 * recurse add identical jobs, clear numjobs and stonewall options
1212 * as they don't apply to sub-jobs
1213 */
de98bd30 1214 numjobs = o->numjobs;
ebac4655 1215 while (--numjobs) {
ef3d8e53 1216 struct thread_data *td_new = get_new_job(0, td, 1, jobname);
ebac4655
JA
1217
1218 if (!td_new)
1219 goto err;
1220
2dc1bbeb
JA
1221 td_new->o.numjobs = 1;
1222 td_new->o.stonewall = 0;
92c1d41f 1223 td_new->o.new_group = 0;
e132cbae
JA
1224
1225 if (file_alloced) {
455a50fa
CE
1226 if (td_new->files) {
1227 struct fio_file *f;
1228 for_each_file(td_new, f, i) {
1229 if (f->file_name)
ece6d647
AK
1230 sfree(f->file_name);
1231 sfree(f);
455a50fa 1232 }
ece6d647 1233 free(td_new->files);
455a50fa
CE
1234 td_new->files = NULL;
1235 }
ece6d647
AK
1236 td_new->files_index = 0;
1237 td_new->files_size = 0;
455a50fa
CE
1238 if (td_new->o.filename) {
1239 free(td_new->o.filename);
1240 td_new->o.filename = NULL;
1241 }
e132cbae
JA
1242 }
1243
bcbfeefa 1244 if (add_job(td_new, jobname, numjobs, 1, client_type))
ebac4655
JA
1245 goto err;
1246 }
3c5df6fa 1247
ebac4655
JA
1248 return 0;
1249err:
1250 put_job(td);
1251 return -1;
1252}
1253
79d16311
JA
1254/*
1255 * Parse as if 'o' was a command line
1256 */
46bcd498 1257void add_job_opts(const char **o, int client_type)
79d16311
JA
1258{
1259 struct thread_data *td, *td_parent;
1260 int i, in_global = 1;
1261 char jobname[32];
1262
1263 i = 0;
1264 td_parent = td = NULL;
1265 while (o[i]) {
1266 if (!strncmp(o[i], "name", 4)) {
1267 in_global = 0;
1268 if (td)
46bcd498 1269 add_job(td, jobname, 0, 0, client_type);
79d16311
JA
1270 td = NULL;
1271 sprintf(jobname, "%s", o[i] + 5);
1272 }
1273 if (in_global && !td_parent)
ef3d8e53 1274 td_parent = get_new_job(1, &def_thread, 0, jobname);
79d16311
JA
1275 else if (!in_global && !td) {
1276 if (!td_parent)
1277 td_parent = &def_thread;
ef3d8e53 1278 td = get_new_job(0, td_parent, 0, jobname);
79d16311
JA
1279 }
1280 if (in_global)
292cc475 1281 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
79d16311 1282 else
292cc475 1283 fio_options_parse(td, (char **) &o[i], 1, 0);
79d16311
JA
1284 i++;
1285 }
1286
1287 if (td)
46bcd498 1288 add_job(td, jobname, 0, 0, client_type);
79d16311
JA
1289}
1290
01f06b63
JA
1291static int skip_this_section(const char *name)
1292{
ad0a2735
JA
1293 int i;
1294
1295 if (!nr_job_sections)
01f06b63
JA
1296 return 0;
1297 if (!strncmp(name, "global", 6))
1298 return 0;
1299
ad0a2735
JA
1300 for (i = 0; i < nr_job_sections; i++)
1301 if (!strcmp(job_sections[i], name))
1302 return 0;
1303
1304 return 1;
01f06b63
JA
1305}
1306
ebac4655
JA
1307static int is_empty_or_comment(char *line)
1308{
1309 unsigned int i;
1310
1311 for (i = 0; i < strlen(line); i++) {
1312 if (line[i] == ';')
1313 return 1;
5cc2da30
IM
1314 if (line[i] == '#')
1315 return 1;
76cd9378 1316 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
ebac4655
JA
1317 return 0;
1318 }
1319
1320 return 1;
1321}
1322
07261983
JA
1323/*
1324 * This is our [ini] type file parser.
1325 */
46bcd498 1326int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
ebac4655 1327{
e1f36503 1328 unsigned int global;
ebac4655 1329 struct thread_data *td;
fee3bb48 1330 char *string, *name;
ebac4655
JA
1331 FILE *f;
1332 char *p;
0c7e37a0 1333 int ret = 0, stonewall;
097b2991 1334 int first_sect = 1;
ccf8f127 1335 int skip_fgets = 0;
01f06b63 1336 int inside_skip = 0;
3b8b7135
JA
1337 char **opts;
1338 int i, alloc_opts, num_opts;
ebac4655 1339
50d16976
JA
1340 if (is_buf)
1341 f = NULL;
1342 else {
1343 if (!strcmp(file, "-"))
1344 f = stdin;
1345 else
1346 f = fopen(file, "r");
5a729cbe 1347
50d16976
JA
1348 if (!f) {
1349 perror("fopen job file");
1350 return 1;
1351 }
ebac4655
JA
1352 }
1353
1354 string = malloc(4096);
7f7e6e59
JA
1355
1356 /*
1357 * it's really 256 + small bit, 280 should suffice
1358 */
1359 name = malloc(280);
1360 memset(name, 0, 280);
ebac4655 1361
3b8b7135
JA
1362 alloc_opts = 8;
1363 opts = malloc(sizeof(char *) * alloc_opts);
b7c6302d 1364 num_opts = 0;
3b8b7135 1365
0c7e37a0 1366 stonewall = stonewall_flag;
7c124ac1 1367 do {
ccf8f127
JA
1368 /*
1369 * if skip_fgets is set, we already have loaded a line we
1370 * haven't handled.
1371 */
1372 if (!skip_fgets) {
50d16976
JA
1373 if (is_buf)
1374 p = strsep(&file, "\n");
1375 else
43f74792 1376 p = fgets(string, 4096, f);
ccf8f127
JA
1377 if (!p)
1378 break;
1379 }
cdc7f193 1380
ccf8f127 1381 skip_fgets = 0;
cdc7f193 1382 strip_blank_front(&p);
6c7c7da1 1383 strip_blank_end(p);
cdc7f193 1384
ebac4655
JA
1385 if (is_empty_or_comment(p))
1386 continue;
84dd1886 1387 if (sscanf(p, "[%255[^\n]]", name) != 1) {
01f06b63
JA
1388 if (inside_skip)
1389 continue;
5ec10eaa
JA
1390 log_err("fio: option <%s> outside of [] job section\n",
1391 p);
088b4207 1392 break;
6c7c7da1 1393 }
ebac4655 1394
7a4b80a1
JA
1395 name[strlen(name) - 1] = '\0';
1396
01f06b63
JA
1397 if (skip_this_section(name)) {
1398 inside_skip = 1;
1399 continue;
1400 } else
1401 inside_skip = 0;
1402
ebac4655
JA
1403 global = !strncmp(name, "global", 6);
1404
cca73aa7 1405 if (dump_cmdline) {
097b2991
JA
1406 if (first_sect)
1407 log_info("fio ");
cca73aa7
JA
1408 if (!global)
1409 log_info("--name=%s ", name);
097b2991 1410 first_sect = 0;
cca73aa7
JA
1411 }
1412
ef3d8e53 1413 td = get_new_job(global, &def_thread, 0, name);
45410acb
JA
1414 if (!td) {
1415 ret = 1;
1416 break;
1417 }
ebac4655 1418
972cfd25 1419 /*
de8f6de9 1420 * Separate multiple job files by a stonewall
972cfd25 1421 */
f9481919 1422 if (!global && stonewall) {
2dc1bbeb 1423 td->o.stonewall = stonewall;
972cfd25
JA
1424 stonewall = 0;
1425 }
1426
3b8b7135
JA
1427 num_opts = 0;
1428 memset(opts, 0, alloc_opts * sizeof(char *));
1429
50d16976
JA
1430 while (1) {
1431 if (is_buf)
1432 p = strsep(&file, "\n");
1433 else
1434 p = fgets(string, 4096, f);
1435 if (!p)
1436 break;
1437
ebac4655
JA
1438 if (is_empty_or_comment(p))
1439 continue;
e1f36503 1440
b6754f9d 1441 strip_blank_front(&p);
7c124ac1 1442
ccf8f127
JA
1443 /*
1444 * new section, break out and make sure we don't
1445 * fgets() a new line at the top.
1446 */
1447 if (p[0] == '[') {
1448 skip_fgets = 1;
7c124ac1 1449 break;
ccf8f127 1450 }
7c124ac1 1451
4ae3f763 1452 strip_blank_end(p);
aea47d44 1453
3b8b7135
JA
1454 if (num_opts == alloc_opts) {
1455 alloc_opts <<= 1;
1456 opts = realloc(opts,
1457 alloc_opts * sizeof(char *));
1458 }
1459
1460 opts[num_opts] = strdup(p);
1461 num_opts++;
ebac4655 1462 }
ebac4655 1463
292cc475
JA
1464 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1465 if (!ret)
46bcd498 1466 ret = add_job(td, name, 0, 0, type);
292cc475 1467 else {
b1508cf9
JA
1468 log_err("fio: job %s dropped\n", name);
1469 put_job(td);
45410acb 1470 }
3b8b7135
JA
1471
1472 for (i = 0; i < num_opts; i++)
1473 free(opts[i]);
1474 num_opts = 0;
7c124ac1 1475 } while (!ret);
ebac4655 1476
cca73aa7
JA
1477 if (dump_cmdline)
1478 log_info("\n");
1479
7e356b2d
JA
1480 i = 0;
1481 while (i < nr_job_sections) {
1482 free(job_sections[i]);
1483 i++;
1484 }
1485
ebac4655
JA
1486 free(string);
1487 free(name);
2577594d 1488 free(opts);
50d16976 1489 if (!is_buf && f != stdin)
5a729cbe 1490 fclose(f);
45410acb 1491 return ret;
ebac4655
JA
1492}
1493
1494static int fill_def_thread(void)
1495{
1496 memset(&def_thread, 0, sizeof(def_thread));
1497
375b2695 1498 fio_getaffinity(getpid(), &def_thread.o.cpumask);
8b28bd41 1499 def_thread.o.error_dump = 1;
25bd16ce 1500
ebac4655 1501 /*
ee738499 1502 * fill default options
ebac4655 1503 */
214e1eca 1504 fio_fill_default_options(&def_thread);
ebac4655
JA
1505 return 0;
1506}
1507
45378b35 1508static void usage(const char *name)
4785f995 1509{
3d43382c 1510 printf("%s\n", fio_version_string);
45378b35 1511 printf("%s [options] [job options] <job file(s)>\n", name);
83881283
JA
1512 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1513 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
3e260a46 1514 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
111e032d 1515 printf(" --parse-only\t\tParse options only, don't start any IO\n");
83881283 1516 printf(" --output\t\tWrite output to file\n");
b2cecdc2 1517 printf(" --runtime\t\tRuntime in seconds\n");
83881283
JA
1518 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1519 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1520 printf(" --minimal\t\tMinimal (terse) output\n");
f3afa57e 1521 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
75db6e2c 1522 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
f3afa57e 1523 printf(" --version\t\tPrint version info and exit\n");
83881283 1524 printf(" --help\t\tPrint this page\n");
23893646 1525 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
fec0f21c 1526 printf(" --crctest\t\tTest speed of checksum functions\n");
83881283 1527 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
5ec10eaa 1528 " them\n");
de890a1e
SL
1529 printf(" --enghelp=engine\tPrint ioengine help, or list"
1530 " available ioengines\n");
1531 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1532 " cmd\n");
83881283
JA
1533 printf(" --showcmd\t\tTurn a job file into command line options\n");
1534 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1535 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
e382e661
JA
1536 printf(" --eta-newline=time\tForce a new line for every 'time'");
1537 printf(" period passed\n");
06464907
JA
1538 printf(" --status-interval=t\tForce full status dump every");
1539 printf(" 't' period passed\n");
83881283 1540 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
5ec10eaa 1541 " writes\n");
83881283
JA
1542 printf(" --section=name\tOnly run specified section in job file\n");
1543 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
2b386d25 1544 " (def 1024)\n");
83881283
JA
1545 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1546 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1547 printf(" --server=args\t\tStart a backend fio server\n");
1548 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1549 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
f2a2ce0e
HL
1550 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1551 "\t\t\t(option=system,percpu) or run unit work\n"
1552 "\t\t\tcalibration only (option=calibrate)\n");
aa58d252 1553 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
bfca2c74
JA
1554 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1555 printf("\n Jens Axboe <axboe@fb.com>\n");
4785f995
JA
1556}
1557
79e48f72 1558#ifdef FIO_INC_DEBUG
ee56ad50 1559struct debug_level debug_levels[] = {
0b8d11ed
JA
1560 { .name = "process",
1561 .help = "Process creation/exit logging",
1562 .shift = FD_PROCESS,
1563 },
1564 { .name = "file",
1565 .help = "File related action logging",
1566 .shift = FD_FILE,
1567 },
1568 { .name = "io",
1569 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1570 .shift = FD_IO,
1571 },
1572 { .name = "mem",
1573 .help = "Memory allocation/freeing logging",
1574 .shift = FD_MEM,
1575 },
1576 { .name = "blktrace",
1577 .help = "blktrace action logging",
1578 .shift = FD_BLKTRACE,
1579 },
1580 { .name = "verify",
1581 .help = "IO verification action logging",
1582 .shift = FD_VERIFY,
1583 },
1584 { .name = "random",
1585 .help = "Random generation logging",
1586 .shift = FD_RANDOM,
1587 },
1588 { .name = "parse",
1589 .help = "Parser logging",
1590 .shift = FD_PARSE,
1591 },
1592 { .name = "diskutil",
1593 .help = "Disk utility logging actions",
1594 .shift = FD_DISKUTIL,
1595 },
1596 { .name = "job",
1597 .help = "Logging related to creating/destroying jobs",
1598 .shift = FD_JOB,
1599 },
1600 { .name = "mutex",
1601 .help = "Mutex logging",
1602 .shift = FD_MUTEX
1603 },
1604 { .name = "profile",
1605 .help = "Logging related to profiles",
1606 .shift = FD_PROFILE,
1607 },
1608 { .name = "time",
1609 .help = "Logging related to time keeping functions",
1610 .shift = FD_TIME,
1611 },
1612 { .name = "net",
1613 .help = "Network logging",
1614 .shift = FD_NET,
1615 },
3e260a46
JA
1616 { .name = "rate",
1617 .help = "Rate logging",
1618 .shift = FD_RATE,
1619 },
02444ad1 1620 { .name = NULL, },
ee56ad50
JA
1621};
1622
c09823ab 1623static int set_debug(const char *string)
ee56ad50
JA
1624{
1625 struct debug_level *dl;
1626 char *p = (char *) string;
1627 char *opt;
1628 int i;
1629
1630 if (!strcmp(string, "?") || !strcmp(string, "help")) {
ee56ad50
JA
1631 log_info("fio: dumping debug options:");
1632 for (i = 0; debug_levels[i].name; i++) {
1633 dl = &debug_levels[i];
1634 log_info("%s,", dl->name);
1635 }
bd6f78b2 1636 log_info("all\n");
c09823ab 1637 return 1;
ee56ad50
JA
1638 }
1639
1640 while ((opt = strsep(&p, ",")) != NULL) {
1641 int found = 0;
1642
5e1d306e
JA
1643 if (!strncmp(opt, "all", 3)) {
1644 log_info("fio: set all debug options\n");
1645 fio_debug = ~0UL;
1646 continue;
1647 }
1648
ee56ad50
JA
1649 for (i = 0; debug_levels[i].name; i++) {
1650 dl = &debug_levels[i];
5e1d306e
JA
1651 found = !strncmp(opt, dl->name, strlen(dl->name));
1652 if (!found)
1653 continue;
1654
1655 if (dl->shift == FD_JOB) {
1656 opt = strchr(opt, ':');
1657 if (!opt) {
1658 log_err("fio: missing job number\n");
1659 break;
1660 }
1661 opt++;
1662 fio_debug_jobno = atoi(opt);
1663 log_info("fio: set debug jobno %d\n",
1664 fio_debug_jobno);
1665 } else {
ee56ad50 1666 log_info("fio: set debug option %s\n", opt);
bd6f78b2 1667 fio_debug |= (1UL << dl->shift);
ee56ad50 1668 }
5e1d306e 1669 break;
ee56ad50
JA
1670 }
1671
1672 if (!found)
1673 log_err("fio: debug mask %s not found\n", opt);
1674 }
c09823ab 1675 return 0;
ee56ad50 1676}
79e48f72 1677#else
69b98d4c 1678static int set_debug(const char *string)
79e48f72
JA
1679{
1680 log_err("fio: debug tracing not included in build\n");
c09823ab 1681 return 1;
79e48f72
JA
1682}
1683#endif
9ac8a797 1684
4c6107ff
JA
1685static void fio_options_fill_optstring(void)
1686{
1687 char *ostr = cmd_optstr;
1688 int i, c;
1689
1690 c = i = 0;
1691 while (l_opts[i].name) {
1692 ostr[c++] = l_opts[i].val;
1693 if (l_opts[i].has_arg == required_argument)
1694 ostr[c++] = ':';
1695 else if (l_opts[i].has_arg == optional_argument) {
1696 ostr[c++] = ':';
1697 ostr[c++] = ':';
1698 }
1699 i++;
1700 }
1701 ostr[c] = '\0';
1702}
1703
c2c94585
JA
1704static int client_flag_set(char c)
1705{
1706 int i;
1707
1708 i = 0;
1709 while (l_opts[i].name) {
1710 int val = l_opts[i].val;
1711
1712 if (c == (val & 0xff))
1713 return (val & FIO_CLIENT_FLAG);
1714
1715 i++;
1716 }
1717
1718 return 0;
1719}
1720
10aa136b 1721static void parse_cmd_client(void *client, char *opt)
7a4b8240 1722{
fa2ea806 1723 fio_client_add_cmd_option(client, opt);
7a4b8240
JA
1724}
1725
46bcd498 1726int parse_cmd_line(int argc, char *argv[], int client_type)
ebac4655 1727{
b4692828 1728 struct thread_data *td = NULL;
c09823ab 1729 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
4c6107ff 1730 char *ostr = cmd_optstr;
402668f3 1731 void *pid_file = NULL;
9bae26e8 1732 void *cur_client = NULL;
81179eec 1733 int backend = 0;
ebac4655 1734
c2c94585
JA
1735 /*
1736 * Reset optind handling, since we may call this multiple times
1737 * for the backend.
1738 */
1739 optind = 1;
7a4b8240 1740
c2c94585
JA
1741 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1742 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
fa2ea806 1743 parse_cmd_client(cur_client, argv[optind - 1]);
7a4b8240
JA
1744 c &= ~FIO_CLIENT_FLAG;
1745 }
1746
ebac4655 1747 switch (c) {
2b386d25
JA
1748 case 'a':
1749 smalloc_pool_size = atoi(optarg);
1750 break;
b4692828 1751 case 't':
25bd16ce
JA
1752 if (check_str_time(optarg, &def_timeout, 1)) {
1753 log_err("fio: failed parsing time %s\n", optarg);
1754 do_exit++;
1755 exit_val = 1;
1756 }
b4692828
JA
1757 break;
1758 case 'l':
1759 write_lat_log = 1;
1760 break;
3d73e5a9 1761 case 'b':
b4692828
JA
1762 write_bw_log = 1;
1763 break;
1764 case 'o':
988da120 1765 if (f_out && f_out != stdout)
5e1d8745
JA
1766 fclose(f_out);
1767
b4692828
JA
1768 f_out = fopen(optarg, "w+");
1769 if (!f_out) {
1770 perror("fopen output");
1771 exit(1);
1772 }
1773 f_err = f_out;
1774 break;
1775 case 'm':
f3afa57e 1776 output_format = FIO_OUTPUT_TERSE;
b4692828 1777 break;
f3afa57e 1778 case 'F':
d10983d7
SH
1779 if (!optarg) {
1780 log_err("fio: missing --output-format argument\n");
1781 exit_val = 1;
1782 do_exit++;
1783 break;
1784 }
f3afa57e
JA
1785 if (!strcmp(optarg, "minimal") ||
1786 !strcmp(optarg, "terse") ||
1787 !strcmp(optarg, "csv"))
1788 output_format = FIO_OUTPUT_TERSE;
1789 else if (!strcmp(optarg, "json"))
1790 output_format = FIO_OUTPUT_JSON;
1791 else
1792 output_format = FIO_OUTPUT_NORMAL;
b4692828 1793 break;
2b8c71b0
CE
1794 case 'f':
1795 append_terse_output = 1;
1796 break;
b4692828 1797 case 'h':
7d8ea970 1798 did_arg = 1;
7874f8b7 1799 if (!cur_client) {
c2c94585 1800 usage(argv[0]);
7874f8b7
JA
1801 do_exit++;
1802 }
c2c94585 1803 break;
fd28ca49 1804 case 'c':
7d8ea970 1805 did_arg = 1;
7874f8b7 1806 if (!cur_client) {
c2c94585 1807 fio_show_option_help(optarg);
7874f8b7
JA
1808 do_exit++;
1809 }
c2c94585 1810 break;
de890a1e 1811 case 'i':
7d8ea970 1812 did_arg = 1;
de890a1e
SL
1813 if (!cur_client) {
1814 fio_show_ioengine_help(optarg);
1815 do_exit++;
1816 }
1817 break;
cca73aa7 1818 case 's':
7d8ea970 1819 did_arg = 1;
cca73aa7
JA
1820 dump_cmdline = 1;
1821 break;
724e4435
JA
1822 case 'r':
1823 read_only = 1;
1824 break;
b4692828 1825 case 'v':
7d8ea970 1826 did_arg = 1;
7874f8b7 1827 if (!cur_client) {
3d43382c 1828 log_info("%s\n", fio_version_string);
7874f8b7
JA
1829 do_exit++;
1830 }
c2c94585 1831 break;
f57a9c59
JA
1832 case 'V':
1833 terse_version = atoi(optarg);
09786f5f
JA
1834 if (!(terse_version == 2 || terse_version == 3 ||
1835 terse_version == 4)) {
f57a9c59
JA
1836 log_err("fio: bad terse version format\n");
1837 exit_val = 1;
1838 do_exit++;
1839 }
1840 break;
e592a06b
AC
1841 case 'e':
1842 if (!strcmp("always", optarg))
1843 eta_print = FIO_ETA_ALWAYS;
1844 else if (!strcmp("never", optarg))
1845 eta_print = FIO_ETA_NEVER;
1846 break;
e382e661
JA
1847 case 'E': {
1848 long long t = 0;
1849
0de5b26f 1850 if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
e382e661
JA
1851 log_err("fio: failed parsing eta time %s\n", optarg);
1852 exit_val = 1;
1853 do_exit++;
1854 }
1855 eta_new_line = t;
1856 break;
1857 }
ee56ad50 1858 case 'd':
c09823ab
JA
1859 if (set_debug(optarg))
1860 do_exit++;
ee56ad50 1861 break;
111e032d 1862 case 'P':
7d8ea970 1863 did_arg = 1;
111e032d
JA
1864 parse_only = 1;
1865 break;
ad0a2735
JA
1866 case 'x': {
1867 size_t new_size;
1868
01f06b63 1869 if (!strcmp(optarg, "global")) {
5ec10eaa
JA
1870 log_err("fio: can't use global as only "
1871 "section\n");
c09823ab
JA
1872 do_exit++;
1873 exit_val = 1;
01f06b63
JA
1874 break;
1875 }
ad0a2735
JA
1876 new_size = (nr_job_sections + 1) * sizeof(char *);
1877 job_sections = realloc(job_sections, new_size);
1878 job_sections[nr_job_sections] = strdup(optarg);
1879 nr_job_sections++;
01f06b63 1880 break;
ad0a2735 1881 }
9ac8a797 1882 case 'p':
7d8ea970 1883 did_arg = 1;
4af7c007
JA
1884 if (exec_profile)
1885 free(exec_profile);
07b3232d 1886 exec_profile = strdup(optarg);
9ac8a797 1887 break;
b4692828 1888 case FIO_GETOPT_JOB: {
5ec10eaa 1889 const char *opt = l_opts[lidx].name;
b4692828
JA
1890 char *val = optarg;
1891
c2b1e753 1892 if (!strncmp(opt, "name", 4) && td) {
46bcd498 1893 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
66251554 1894 if (ret)
4a4ac4e3 1895 goto out_free;
c2b1e753 1896 td = NULL;
7d8ea970 1897 did_arg = 1;
c2b1e753 1898 }
b4692828 1899 if (!td) {
01f06b63 1900 int is_section = !strncmp(opt, "name", 4);
3106f220
JA
1901 int global = 0;
1902
01f06b63 1903 if (!is_section || !strncmp(val, "global", 6))
3106f220 1904 global = 1;
c2b1e753 1905
01f06b63
JA
1906 if (is_section && skip_this_section(val))
1907 continue;
1908
ef3d8e53 1909 td = get_new_job(global, &def_thread, 1, NULL);
f6ff24cb
JA
1910 if (!td || ioengine_load(td)) {
1911 if (td) {
1912 put_job(td);
1913 td = NULL;
1914 }
1915 do_exit++;
1916 break;
1917 }
de890a1e 1918 fio_options_set_ioengine_opts(l_opts, td);
b4692828 1919 }
38d0adb0 1920
9d918187
JA
1921 if ((!val || !strlen(val)) &&
1922 l_opts[lidx].has_arg == required_argument) {
1923 log_err("fio: option %s requires an argument\n", opt);
1924 ret = 1;
1925 } else
1926 ret = fio_cmd_option_parse(td, opt, val);
1927
bfb3ea22
JA
1928 if (ret) {
1929 if (td) {
1930 put_job(td);
1931 td = NULL;
1932 }
a88c8c14 1933 do_exit++;
bfb3ea22 1934 }
de890a1e
SL
1935
1936 if (!ret && !strcmp(opt, "ioengine")) {
1937 free_ioengine(td);
f6ff24cb
JA
1938 if (ioengine_load(td)) {
1939 if (td) {
1940 put_job(td);
1941 td = NULL;
1942 }
1943 do_exit++;
1944 break;
1945 }
de890a1e
SL
1946 fio_options_set_ioengine_opts(l_opts, td);
1947 }
1948 break;
1949 }
1950 case FIO_GETOPT_IOENGINE: {
1951 const char *opt = l_opts[lidx].name;
1952 char *val = optarg;
b1ed74e0
JA
1953
1954 if (!td)
1955 break;
1956
de890a1e 1957 ret = fio_cmd_ioengine_option_parse(td, opt, val);
b4692828
JA
1958 break;
1959 }
a9523c6f
JA
1960 case 'w':
1961 warnings_fatal = 1;
1962 break;
fca70358
JA
1963 case 'j':
1964 max_jobs = atoi(optarg);
1965 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1966 log_err("fio: invalid max jobs: %d\n", max_jobs);
1967 do_exit++;
1968 exit_val = 1;
1969 }
1970 break;
50d16976 1971 case 'S':
7d8ea970 1972 did_arg = 1;
c8931876 1973#ifndef CONFIG_NO_SHM
a37f69b7 1974 if (nr_clients) {
132159a5
JA
1975 log_err("fio: can't be both client and server\n");
1976 do_exit++;
1977 exit_val = 1;
1978 break;
1979 }
87aa8f19 1980 if (optarg)
bebe6398 1981 fio_server_set_arg(optarg);
50d16976 1982 is_backend = 1;
81179eec 1983 backend = 1;
c8931876
JA
1984#else
1985 log_err("fio: client/server requires SHM support\n");
1986 do_exit++;
1987 exit_val = 1;
1988#endif
50d16976 1989 break;
e46d8091 1990 case 'D':
6cfe9a8c
JA
1991 if (pid_file)
1992 free(pid_file);
402668f3 1993 pid_file = strdup(optarg);
e46d8091 1994 break;
f2a2ce0e
HL
1995 case 'I':
1996 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1997 /* exit on error and calibration only */
7d8ea970 1998 did_arg = 1;
f2a2ce0e 1999 do_exit++;
7d8ea970 2000 if (ret == -1)
f2a2ce0e
HL
2001 exit_val = 1;
2002 }
2003 break;
132159a5 2004 case 'C':
7d8ea970 2005 did_arg = 1;
132159a5
JA
2006 if (is_backend) {
2007 log_err("fio: can't be both client and server\n");
2008 do_exit++;
2009 exit_val = 1;
2010 break;
2011 }
a5276616 2012 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
bebe6398
JA
2013 log_err("fio: failed adding client %s\n", optarg);
2014 do_exit++;
2015 exit_val = 1;
2016 break;
2017 }
14ea90ed
JA
2018 /*
2019 * If the next argument exists and isn't an option,
2020 * assume it's a job file for this client only.
2021 */
2022 while (optind < argc) {
2023 if (!strncmp(argv[optind], "--", 2) ||
2024 !strncmp(argv[optind], "-", 1))
2025 break;
2026
2027 fio_client_add_ini_file(cur_client, argv[optind]);
2028 optind++;
2029 }
132159a5 2030 break;
7d11f871 2031 case 'T':
7d8ea970 2032 did_arg = 1;
7d11f871
JA
2033 do_exit++;
2034 exit_val = fio_monotonic_clocktest();
2035 break;
fec0f21c 2036 case 'G':
7d8ea970 2037 did_arg = 1;
fec0f21c
JA
2038 do_exit++;
2039 exit_val = fio_crctest(optarg);
2040 break;
06464907
JA
2041 case 'L': {
2042 long long val;
2043
7cd0ec5d 2044 if (check_str_time(optarg, &val, 0)) {
06464907
JA
2045 log_err("fio: failed parsing time %s\n", optarg);
2046 do_exit++;
2047 exit_val = 1;
2048 break;
2049 }
2050 status_interval = val * 1000;
2051 break;
2052 }
798827c8
JA
2053 case '?':
2054 log_err("%s: unrecognized option '%s'\n", argv[0],
2055 argv[optind - 1]);
b4692828 2056 default:
c09823ab
JA
2057 do_exit++;
2058 exit_val = 1;
b4692828 2059 break;
ebac4655 2060 }
c7d5c941
JA
2061 if (do_exit)
2062 break;
ebac4655 2063 }
c9fad893 2064
0b14f0a8
JA
2065 if (do_exit && !(is_backend || nr_clients))
2066 exit(exit_val);
536582bf 2067
fb296043
JA
2068 if (nr_clients && fio_clients_connect())
2069 exit(1);
132159a5 2070
81179eec 2071 if (is_backend && backend)
402668f3 2072 return fio_start_server(pid_file);
b8ba87ac
JA
2073 else if (pid_file)
2074 free(pid_file);
50d16976 2075
b4692828 2076 if (td) {
7d8ea970 2077 if (!ret) {
46bcd498 2078 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
7d8ea970
JA
2079 if (ret)
2080 did_arg = 1;
2081 }
972cfd25 2082 }
774a6177 2083
7874f8b7 2084 while (!ret && optind < argc) {
b4692828
JA
2085 ini_idx++;
2086 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2087 ini_file[ini_idx - 1] = strdup(argv[optind]);
2088 optind++;
eb8bbf48 2089 }
972cfd25 2090
4a4ac4e3
JA
2091out_free:
2092 if (pid_file)
2093 free(pid_file);
2094
972cfd25 2095 return ini_idx;
ebac4655
JA
2096}
2097
0420ba6a 2098int fio_init_options(void)
ebac4655 2099{
b4692828
JA
2100 f_out = stdout;
2101 f_err = stderr;
2102
4c6107ff 2103 fio_options_fill_optstring();
5ec10eaa 2104 fio_options_dup_and_init(l_opts);
b4692828 2105
9d9eb2e7
JA
2106 atexit(free_shm);
2107
ebac4655
JA
2108 if (fill_def_thread())
2109 return 1;
2110
0420ba6a
JA
2111 return 0;
2112}
2113
51167799
JA
2114extern int fio_check_options(struct thread_options *);
2115
0420ba6a
JA
2116int parse_options(int argc, char *argv[])
2117{
46bcd498 2118 const int type = FIO_CLIENT_TYPE_CLI;
0420ba6a
JA
2119 int job_files, i;
2120
2121 if (fio_init_options())
2122 return 1;
51167799
JA
2123 if (fio_test_cconv(&def_thread.o))
2124 log_err("fio: failed internal cconv test\n");
0420ba6a 2125
46bcd498 2126 job_files = parse_cmd_line(argc, argv, type);
ebac4655 2127
cdf54d85
JA
2128 if (job_files > 0) {
2129 for (i = 0; i < job_files; i++) {
bc4f5ef6 2130 if (i && fill_def_thread())
132159a5 2131 return 1;
cdf54d85
JA
2132 if (nr_clients) {
2133 if (fio_clients_send_ini(ini_file[i]))
2134 return 1;
2135 free(ini_file[i]);
2136 } else if (!is_backend) {
46bcd498 2137 if (parse_jobs_ini(ini_file[i], 0, i, type))
cdf54d85
JA
2138 return 1;
2139 free(ini_file[i]);
2140 }
132159a5 2141 }
14ea90ed
JA
2142 } else if (nr_clients) {
2143 if (fill_def_thread())
2144 return 1;
2145 if (fio_clients_send_ini(NULL))
2146 return 1;
972cfd25 2147 }
ebac4655 2148
88c6ed80 2149 free(ini_file);
7e356b2d 2150 fio_options_free(&def_thread);
bcbfeefa 2151 filesetup_mem_free();
b4692828
JA
2152
2153 if (!thread_number) {
f9633d72 2154 if (parse_dryrun())
cca73aa7 2155 return 0;
07b3232d
JA
2156 if (exec_profile)
2157 return 0;
a37f69b7 2158 if (is_backend || nr_clients)
5c341e9a 2159 return 0;
085399db
JA
2160 if (did_arg)
2161 return 0;
cca73aa7 2162
d65e11c6 2163 log_err("No jobs(s) defined\n\n");
085399db
JA
2164
2165 if (!did_arg) {
2166 usage(argv[0]);
2167 return 1;
2168 }
2169
2170 return 0;
b4692828
JA
2171 }
2172
be4ecfdf
JA
2173 if (def_thread.o.gtod_offload) {
2174 fio_gtod_init();
2175 fio_gtod_offload = 1;
2176 fio_gtod_cpu = def_thread.o.gtod_cpu;
2177 }
2178
f3afa57e 2179 if (output_format == FIO_OUTPUT_NORMAL)
3d43382c 2180 log_info("%s\n", fio_version_string);
f6dea4d3 2181
ebac4655
JA
2182 return 0;
2183}
588b7f09
JA
2184
2185void options_default_fill(struct thread_options *o)
2186{
2187 memcpy(o, &def_thread.o, sizeof(*o));
2188}