Default to terse version 3
[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>
12#include <sys/shm.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15
16#include "fio.h"
cb2c86fd 17#include "parse.h"
2e5cdb11 18#include "smalloc.h"
380065aa 19#include "filehash.h"
4f5af7b2 20#include "verify.h"
79d16311 21#include "profile.h"
50d16976 22#include "server.h"
ebac4655 23
bf2e821a
CC
24#include "lib/getopt.h"
25
692a5d22 26const char fio_version_string[] = FIO_VERSION;
214e1eca 27
ee738499 28#define FIO_RANDSEED (0xb1899bedUL)
ebac4655 29
214e1eca 30static char **ini_file;
fca70358 31static int max_jobs = FIO_MAX_JOBS;
cca73aa7 32static int dump_cmdline;
8c029376 33static int def_timeout;
e1f36503 34
be4ecfdf 35static struct thread_data def_thread;
214e1eca 36struct thread_data *threads = NULL;
e1f36503 37
214e1eca
JA
38int exitall_on_terminate = 0;
39int terse_output = 0;
e592a06b 40int eta_print;
214e1eca
JA
41unsigned long long mlock_size = 0;
42FILE *f_out = NULL;
43FILE *f_err = NULL;
ad0a2735
JA
44char **job_sections = NULL;
45int nr_job_sections = 0;
07b3232d 46char *exec_profile = NULL;
a9523c6f 47int warnings_fatal = 0;
4d658652 48int terse_version = 3;
50d16976 49int is_backend = 0;
a37f69b7 50int nr_clients = 0;
e46d8091 51int log_syslog = 0;
ee738499 52
214e1eca 53int write_bw_log = 0;
4241ea8f 54int read_only = 0;
214e1eca 55
5ec10eaa 56static int write_lat_log;
e1f36503 57
214e1eca 58static int prev_group_jobs;
b4692828 59
ee56ad50 60unsigned long fio_debug = 0;
5e1d306e
JA
61unsigned int fio_debug_jobno = -1;
62unsigned int *fio_debug_jobp = NULL;
ee56ad50 63
4c6107ff 64static char cmd_optstr[256];
085399db 65static int did_arg;
4c6107ff 66
802ad4a8 67const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN] = {
b4b317df
SL
68 { .u.f = 1.00 },
69 { .u.f = 5.00 },
70 { .u.f = 10.00 },
71 { .u.f = 20.00 },
72 { .u.f = 30.00 },
73 { .u.f = 40.00 },
74 { .u.f = 50.00 },
75 { .u.f = 60.00 },
76 { .u.f = 70.00 },
77 { .u.f = 80.00 },
78 { .u.f = 90.00 },
79 { .u.f = 95.00 },
80 { .u.f = 99.00 },
81 { .u.f = 99.50 },
82 { .u.f = 99.90 },
83 { .u.f = 99.95 },
84 { .u.f = 99.99 },
802ad4a8
JA
85};
86
7a4b8240
JA
87#define FIO_CLIENT_FLAG (1 << 16)
88
b4692828
JA
89/*
90 * Command line options. These will contain the above, plus a few
91 * extra that only pertain to fio itself and not jobs.
92 */
5ec10eaa 93static struct option l_opts[FIO_NR_OPTIONS] = {
b4692828 94 {
08d2a19c 95 .name = (char *) "output",
b4692828 96 .has_arg = required_argument,
7a4b8240 97 .val = 'o' | FIO_CLIENT_FLAG,
b4692828
JA
98 },
99 {
08d2a19c 100 .name = (char *) "timeout",
b4692828 101 .has_arg = required_argument,
7a4b8240 102 .val = 't' | FIO_CLIENT_FLAG,
b4692828
JA
103 },
104 {
08d2a19c 105 .name = (char *) "latency-log",
b4692828 106 .has_arg = required_argument,
7a4b8240 107 .val = 'l' | FIO_CLIENT_FLAG,
b4692828
JA
108 },
109 {
08d2a19c 110 .name = (char *) "bandwidth-log",
b4692828 111 .has_arg = required_argument,
7a4b8240 112 .val = 'b' | FIO_CLIENT_FLAG,
b4692828
JA
113 },
114 {
08d2a19c 115 .name = (char *) "minimal",
b4692828 116 .has_arg = optional_argument,
7a4b8240 117 .val = 'm' | FIO_CLIENT_FLAG,
b4692828
JA
118 },
119 {
08d2a19c 120 .name = (char *) "version",
b4692828 121 .has_arg = no_argument,
7a4b8240 122 .val = 'v' | FIO_CLIENT_FLAG,
b4692828 123 },
fd28ca49 124 {
08d2a19c 125 .name = (char *) "help",
fd28ca49 126 .has_arg = no_argument,
7a4b8240 127 .val = 'h' | FIO_CLIENT_FLAG,
fd28ca49
JA
128 },
129 {
08d2a19c 130 .name = (char *) "cmdhelp",
320beefe 131 .has_arg = optional_argument,
7a4b8240 132 .val = 'c' | FIO_CLIENT_FLAG,
fd28ca49 133 },
de890a1e
SL
134 {
135 .name = (char *) "enghelp",
136 .has_arg = optional_argument,
137 .val = 'i' | FIO_CLIENT_FLAG,
138 },
cca73aa7 139 {
08d2a19c 140 .name = (char *) "showcmd",
cca73aa7 141 .has_arg = no_argument,
7a4b8240 142 .val = 's' | FIO_CLIENT_FLAG,
724e4435
JA
143 },
144 {
08d2a19c 145 .name = (char *) "readonly",
724e4435 146 .has_arg = no_argument,
7a4b8240 147 .val = 'r' | FIO_CLIENT_FLAG,
cca73aa7 148 },
e592a06b 149 {
08d2a19c 150 .name = (char *) "eta",
e592a06b 151 .has_arg = required_argument,
7a4b8240 152 .val = 'e' | FIO_CLIENT_FLAG,
e592a06b 153 },
ee56ad50 154 {
08d2a19c 155 .name = (char *) "debug",
ee56ad50 156 .has_arg = required_argument,
7a4b8240 157 .val = 'd' | FIO_CLIENT_FLAG,
ee56ad50 158 },
01f06b63 159 {
08d2a19c 160 .name = (char *) "section",
01f06b63 161 .has_arg = required_argument,
7a4b8240 162 .val = 'x' | FIO_CLIENT_FLAG,
01f06b63 163 },
2b386d25 164 {
08d2a19c 165 .name = (char *) "alloc-size",
2b386d25 166 .has_arg = required_argument,
7a4b8240 167 .val = 'a' | FIO_CLIENT_FLAG,
2b386d25 168 },
9ac8a797 169 {
08d2a19c 170 .name = (char *) "profile",
9ac8a797 171 .has_arg = required_argument,
7a4b8240 172 .val = 'p' | FIO_CLIENT_FLAG,
9ac8a797 173 },
a9523c6f 174 {
08d2a19c 175 .name = (char *) "warnings-fatal",
a9523c6f 176 .has_arg = no_argument,
7a4b8240 177 .val = 'w' | FIO_CLIENT_FLAG,
a9523c6f 178 },
fca70358
JA
179 {
180 .name = (char *) "max-jobs",
181 .has_arg = required_argument,
7a4b8240 182 .val = 'j' | FIO_CLIENT_FLAG,
fca70358 183 },
f57a9c59
JA
184 {
185 .name = (char *) "terse-version",
186 .has_arg = required_argument,
7a4b8240 187 .val = 'V' | FIO_CLIENT_FLAG,
f57a9c59 188 },
50d16976
JA
189 {
190 .name = (char *) "server",
87aa8f19 191 .has_arg = optional_argument,
50d16976
JA
192 .val = 'S',
193 },
e46d8091 194 { .name = (char *) "daemonize",
402668f3 195 .has_arg = required_argument,
e46d8091
JA
196 .val = 'D',
197 },
132159a5
JA
198 {
199 .name = (char *) "client",
200 .has_arg = required_argument,
201 .val = 'C',
202 },
b4692828
JA
203 {
204 .name = NULL,
205 },
206};
207
9d9eb2e7
JA
208static void free_shm(void)
209{
210 struct shmid_ds sbuf;
211
212 if (threads) {
213 void *tp = threads;
214
215 threads = NULL;
216 file_hash_exit();
9e684a49 217 flow_exit();
9d9eb2e7
JA
218 fio_debug_jobp = NULL;
219 shmdt(tp);
220 shmctl(shm_id, IPC_RMID, &sbuf);
221 }
222
223 scleanup();
224}
225
226/*
227 * The thread area is shared between the main process and the job
228 * threads/processes. So setup a shared memory segment that will hold
229 * all the job info. We use the end of the region for keeping track of
230 * open files across jobs, for file sharing.
231 */
232static int setup_thread_area(void)
233{
234 void *hash;
235
236 if (threads)
237 return 0;
238
239 /*
240 * 1024 is too much on some machines, scale max_jobs if
241 * we get a failure that looks like too large a shm segment
242 */
243 do {
244 size_t size = max_jobs * sizeof(struct thread_data);
245
246 size += file_hash_size;
247 size += sizeof(unsigned int);
248
249 shm_id = shmget(0, size, IPC_CREAT | 0600);
250 if (shm_id != -1)
251 break;
85af5794 252 if (errno != EINVAL && errno != ENOMEM) {
9d9eb2e7
JA
253 perror("shmget");
254 break;
255 }
256
257 max_jobs >>= 1;
258 } while (max_jobs);
259
260 if (shm_id == -1)
261 return 1;
262
263 threads = shmat(shm_id, NULL, 0);
264 if (threads == (void *) -1) {
265 perror("shmat");
266 return 1;
267 }
268
269 memset(threads, 0, max_jobs * sizeof(struct thread_data));
270 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
271 fio_debug_jobp = (void *) hash + file_hash_size;
272 *fio_debug_jobp = -1;
273 file_hash_init(hash);
9e684a49
DE
274
275 flow_init();
276
9d9eb2e7
JA
277 return 0;
278}
279
906c8d75
JA
280/*
281 * Return a free job structure.
282 */
de890a1e
SL
283static struct thread_data *get_new_job(int global, struct thread_data *parent,
284 int preserve_eo)
ebac4655
JA
285{
286 struct thread_data *td;
287
288 if (global)
289 return &def_thread;
9d9eb2e7
JA
290 if (setup_thread_area()) {
291 log_err("error: failed to setup shm segment\n");
292 return NULL;
293 }
e61f1ec8
BC
294 if (thread_number >= max_jobs) {
295 log_err("error: maximum number of jobs (%d) reached.\n",
296 max_jobs);
ebac4655 297 return NULL;
e61f1ec8 298 }
ebac4655
JA
299
300 td = &threads[thread_number++];
ddaeaa5a 301 *td = *parent;
ebac4655 302
de890a1e
SL
303 td->io_ops = NULL;
304 if (!preserve_eo)
305 td->eo = NULL;
306
e0b0d892
JA
307 td->o.uid = td->o.gid = -1U;
308
cade3ef4 309 dup_files(td, parent);
de890a1e 310 fio_options_mem_dupe(td);
cade3ef4 311
15dc1934
JA
312 profile_add_hooks(td);
313
ebac4655 314 td->thread_number = thread_number;
ebac4655
JA
315 return td;
316}
317
318static void put_job(struct thread_data *td)
319{
549577a7
JA
320 if (td == &def_thread)
321 return;
84dd1886 322
58c55ba0 323 profile_td_exit(td);
9e684a49 324 flow_exit_job(td);
549577a7 325
16edf25d 326 if (td->error)
6d86144d 327 log_info("fio: %s\n", td->verror);
16edf25d 328
7e356b2d 329 fio_options_free(td);
de890a1e
SL
330 if (td->io_ops)
331 free_ioengine(td);
7e356b2d 332
ebac4655
JA
333 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
334 thread_number--;
335}
336
581e7141 337static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
127f6865 338{
581e7141 339 unsigned int bs = td->o.min_bs[ddir];
127f6865 340
ff58fced
JA
341 assert(ddir_rw(ddir));
342
ba3e4e0c 343 if (td->o.rate[ddir])
1b8dbf25 344 td->rate_bps[ddir] = td->o.rate[ddir];
ba3e4e0c 345 else
1b8dbf25 346 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
127f6865 347
1b8dbf25 348 if (!td->rate_bps[ddir]) {
127f6865
JA
349 log_err("rate lower than supported\n");
350 return -1;
351 }
352
581e7141 353 td->rate_pending_usleep[ddir] = 0;
127f6865
JA
354 return 0;
355}
356
581e7141
JA
357static int setup_rate(struct thread_data *td)
358{
359 int ret = 0;
360
361 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
362 ret = __setup_rate(td, DDIR_READ);
363 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
364 ret |= __setup_rate(td, DDIR_WRITE);
6eaf09d6
SL
365 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
366 ret |= __setup_rate(td, DDIR_TRIM);
581e7141
JA
367
368 return ret;
369}
370
8347239a
JA
371static int fixed_block_size(struct thread_options *o)
372{
373 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
374 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
6eaf09d6
SL
375 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
376 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
377 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
8347239a
JA
378}
379
dad915e3
JA
380/*
381 * Lazy way of fixing up options that depend on each other. We could also
382 * define option callback handlers, but this is easier.
383 */
4e991c23 384static int fixup_options(struct thread_data *td)
e1f36503 385{
2dc1bbeb 386 struct thread_options *o = &td->o;
ff217451 387 int ret = 0;
dad915e3 388
f356d01d 389#ifndef FIO_HAVE_PSHARED_MUTEX
9bbf57cc 390 if (!o->use_thread) {
f356d01d
JA
391 log_info("fio: this platform does not support process shared"
392 " mutexes, forcing use of threads. Use the 'thread'"
393 " option to get rid of this warning.\n");
9bbf57cc 394 o->use_thread = 1;
a9523c6f 395 ret = warnings_fatal;
f356d01d
JA
396 }
397#endif
398
2dc1bbeb 399 if (o->write_iolog_file && o->read_iolog_file) {
076efc7c 400 log_err("fio: read iolog overrides write_iolog\n");
2dc1bbeb
JA
401 free(o->write_iolog_file);
402 o->write_iolog_file = NULL;
a9523c6f 403 ret = warnings_fatal;
076efc7c 404 }
16b462ae 405
16b462ae 406 /*
ed335855 407 * only really works with 1 file
16b462ae 408 */
ed335855 409 if (o->zone_size && o->open_files == 1)
2dc1bbeb 410 o->zone_size = 0;
16b462ae 411
ed335855
SN
412 /*
413 * If zone_range isn't specified, backward compatibility dictates it
414 * should be made equal to zone_size.
415 */
416 if (o->zone_size && !o->zone_range)
417 o->zone_range = o->zone_size;
418
16b462ae
JA
419 /*
420 * Reads can do overwrites, we always need to pre-create the file
421 */
422 if (td_read(td) || td_rw(td))
2dc1bbeb 423 o->overwrite = 1;
16b462ae 424
2dc1bbeb 425 if (!o->min_bs[DDIR_READ])
5ec10eaa 426 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
2dc1bbeb
JA
427 if (!o->max_bs[DDIR_READ])
428 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
429 if (!o->min_bs[DDIR_WRITE])
5ec10eaa 430 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
2dc1bbeb
JA
431 if (!o->max_bs[DDIR_WRITE])
432 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
6eaf09d6
SL
433 if (!o->min_bs[DDIR_TRIM])
434 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
435 if (!o->max_bs[DDIR_TRIM])
436 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
437
a00735e6 438
2dc1bbeb 439 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
6eaf09d6 440 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
a00735e6 441
2b7a01d0
JA
442 /*
443 * For random IO, allow blockalign offset other than min_bs.
444 */
445 if (!o->ba[DDIR_READ] || !td_random(td))
446 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
447 if (!o->ba[DDIR_WRITE] || !td_random(td))
448 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
6eaf09d6
SL
449 if (!o->ba[DDIR_TRIM] || !td_random(td))
450 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
2b7a01d0
JA
451
452 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
6eaf09d6
SL
453 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
454 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
9bbf57cc 455 !o->norandommap) {
2b7a01d0 456 log_err("fio: Any use of blockalign= turns off randommap\n");
9bbf57cc 457 o->norandommap = 1;
a9523c6f 458 ret = warnings_fatal;
2b7a01d0
JA
459 }
460
2dc1bbeb
JA
461 if (!o->file_size_high)
462 o->file_size_high = o->file_size_low;
9c60ce64 463
8347239a
JA
464 if (o->norandommap && o->verify != VERIFY_NONE
465 && !fixed_block_size(o)) {
466 log_err("fio: norandommap given for variable block sizes, "
467 "verify disabled\n");
2dc1bbeb 468 o->verify = VERIFY_NONE;
a9523c6f 469 ret = warnings_fatal;
bb8895e0 470 }
2dc1bbeb 471 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
690adba3 472 log_err("fio: bs_unaligned may not work with raw io\n");
e0a22335 473
48097d5c
JA
474 /*
475 * thinktime_spin must be less than thinktime
476 */
2dc1bbeb
JA
477 if (o->thinktime_spin > o->thinktime)
478 o->thinktime_spin = o->thinktime;
e916b390
JA
479
480 /*
481 * The low water mark cannot be bigger than the iodepth
482 */
2dc1bbeb 483 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
9467b77c
JA
484 /*
485 * syslet work around - if the workload is sequential,
486 * we want to let the queue drain all the way down to
487 * avoid seeking between async threads
488 */
489 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
2dc1bbeb 490 o->iodepth_low = 1;
9467b77c 491 else
2dc1bbeb 492 o->iodepth_low = o->iodepth;
9467b77c 493 }
cb5ab512
JA
494
495 /*
496 * If batch number isn't set, default to the same as iodepth
497 */
2dc1bbeb
JA
498 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
499 o->iodepth_batch = o->iodepth;
b5af8293 500
2dc1bbeb
JA
501 if (o->nr_files > td->files_index)
502 o->nr_files = td->files_index;
9f9214f2 503
2dc1bbeb
JA
504 if (o->open_files > o->nr_files || !o->open_files)
505 o->open_files = o->nr_files;
4e991c23 506
6eaf09d6
SL
507 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
508 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
509 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
510 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
4e991c23 511 log_err("fio: rate and rate_iops are mutually exclusive\n");
a9523c6f 512 ret = 1;
4e991c23 513 }
6eaf09d6
SL
514 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
515 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
516 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
517 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
518 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
519 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
4e991c23 520 log_err("fio: minimum rate exceeds rate\n");
a9523c6f 521 ret = 1;
4e991c23
JA
522 }
523
cf4464ca
JA
524 if (!o->timeout && o->time_based) {
525 log_err("fio: time_based requires a runtime/timeout setting\n");
526 o->time_based = 0;
a9523c6f 527 ret = warnings_fatal;
cf4464ca
JA
528 }
529
aa31f1f1 530 if (o->fill_device && !o->size)
5921e80c 531 o->size = -1ULL;
5ec10eaa 532
9bbf57cc 533 if (o->verify != VERIFY_NONE) {
996936f9 534 if (td_write(td) && o->do_verify && o->numjobs > 1) {
a9523c6f
JA
535 log_info("Multiple writers may overwrite blocks that "
536 "belong to other jobs. This can cause "
537 "verification failures.\n");
538 ret = warnings_fatal;
539 }
540
9bbf57cc 541 o->refill_buffers = 1;
2f1b8e8b
JA
542 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
543 !o->verify_interval)
544 o->verify_interval = o->min_bs[DDIR_WRITE];
d990588e 545 }
41ccd845 546
9bbf57cc
JA
547 if (o->pre_read) {
548 o->invalidate_cache = 0;
a9523c6f 549 if (td->io_ops->flags & FIO_PIPEIO) {
9c0d2241
JA
550 log_info("fio: cannot pre-read files with an IO engine"
551 " that isn't seekable. Pre-read disabled.\n");
a9523c6f
JA
552 ret = warnings_fatal;
553 }
9c0d2241 554 }
34f1c044 555
e72fa4d4 556#ifndef FIO_HAVE_FDATASYNC
9bbf57cc 557 if (o->fdatasync_blocks) {
e72fa4d4
JA
558 log_info("fio: this platform does not support fdatasync()"
559 " falling back to using fsync(). Use the 'fsync'"
560 " option instead of 'fdatasync' to get rid of"
561 " this warning\n");
9bbf57cc
JA
562 o->fsync_blocks = o->fdatasync_blocks;
563 o->fdatasync_blocks = 0;
a9523c6f 564 ret = warnings_fatal;
e72fa4d4
JA
565 }
566#endif
567
93bcfd20
BC
568#ifdef WIN32
569 /*
570 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
571 * so fail if we're passed those flags
572 */
573 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
574 log_err("fio: Windows does not support direct or non-buffered io with"
575 " the synchronous ioengines. Use the 'windowsaio' ioengine"
576 " with 'direct=1' and 'iodepth=1' instead.\n");
577 ret = 1;
578 }
579#endif
580
811ac503
JA
581 /*
582 * For fully compressible data, just zero them at init time.
583 * It's faster than repeatedly filling it.
584 */
585 if (td->o.compress_percentage == 100) {
586 td->o.zero_buffers = 1;
587 td->o.compress_percentage = 0;
588 }
589
a9523c6f 590 return ret;
e1f36503
JA
591}
592
f8977ee6
JA
593/*
594 * This function leaks the buffer
595 */
596static char *to_kmg(unsigned int val)
597{
598 char *buf = malloc(32);
f3502ba2 599 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
f8977ee6
JA
600 char *p = post;
601
245142ff 602 do {
f8977ee6
JA
603 if (val & 1023)
604 break;
605
606 val >>= 10;
607 p++;
245142ff 608 } while (*p);
f8977ee6
JA
609
610 snprintf(buf, 31, "%u%c", val, *p);
611 return buf;
612}
613
09629a90
JA
614/* External engines are specified by "external:name.o") */
615static const char *get_engine_name(const char *str)
616{
617 char *p = strstr(str, ":");
618
619 if (!p)
620 return str;
621
622 p++;
623 strip_blank_front(&p);
624 strip_blank_end(p);
625 return p;
626}
627
e132cbae
JA
628static int exists_and_not_file(const char *filename)
629{
630 struct stat sb;
631
632 if (lstat(filename, &sb) == -1)
633 return 0;
634
ecc314ba
BC
635 /* \\.\ is the device namespace in Windows, where every file
636 * is a device node */
637 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
e132cbae
JA
638 return 0;
639
640 return 1;
641}
642
4c07ad86 643static void td_fill_rand_seeds_os(struct thread_data *td)
5bfc35d7
JA
644{
645 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
646 os_random_seed(td->rand_seeds[1], &td->verify_state);
647 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
648
649 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
650 os_random_seed(td->rand_seeds[3], &td->next_file_state);
651
652 os_random_seed(td->rand_seeds[5], &td->file_size_state);
0d29de83 653 os_random_seed(td->rand_seeds[6], &td->trim_state);
5bfc35d7
JA
654
655 if (!td_random(td))
656 return;
657
658 if (td->o.rand_repeatable)
659 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
660
661 os_random_seed(td->rand_seeds[4], &td->random_state);
4c07ad86
JA
662}
663
664static void td_fill_rand_seeds_internal(struct thread_data *td)
665{
666 init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]);
667 init_rand_seed(&td->__verify_state, td->rand_seeds[1]);
668 init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]);
669
670 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
671 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]);
672
673 init_rand_seed(&td->__file_size_state, td->rand_seeds[5]);
674 init_rand_seed(&td->__trim_state, td->rand_seeds[6]);
675
676 if (!td_random(td))
677 return;
678
679 if (td->o.rand_repeatable)
680 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
681
2615cc4b 682 init_rand_seed(&td->__random_state, td->rand_seeds[4]);
5bfc35d7
JA
683}
684
4c07ad86
JA
685void td_fill_rand_seeds(struct thread_data *td)
686{
687 if (td->o.use_os_rand)
688 td_fill_rand_seeds_os(td);
689 else
690 td_fill_rand_seeds_internal(td);
3545a109
JA
691
692 init_rand_seed(&td->buf_state, td->rand_seeds[7]);
4c07ad86
JA
693}
694
9c60ce64 695
de890a1e
SL
696/*
697 * Initializes the ioengine configured for a job, if it has not been done so
698 * already.
699 */
700int ioengine_load(struct thread_data *td)
701{
702 const char *engine;
703
704 /*
705 * Engine has already been loaded.
706 */
707 if (td->io_ops)
708 return 0;
709
710 engine = get_engine_name(td->o.ioengine);
711 td->io_ops = load_ioengine(td, engine);
712 if (!td->io_ops) {
713 log_err("fio: failed to load engine %s\n", engine);
714 return 1;
715 }
716
717 if (td->io_ops->option_struct_size && td->io_ops->options) {
718 /*
719 * In cases where td->eo is set, clone it for a child thread.
720 * This requires that the parent thread has the same ioengine,
721 * but that requirement must be enforced by the code which
722 * cloned the thread.
723 */
724 void *origeo = td->eo;
725 /*
726 * Otherwise use the default thread options.
727 */
728 if (!origeo && td != &def_thread && def_thread.eo &&
729 def_thread.io_ops->options == td->io_ops->options)
730 origeo = def_thread.eo;
731
732 options_init(td->io_ops->options);
733 td->eo = malloc(td->io_ops->option_struct_size);
734 /*
735 * Use the default thread as an option template if this uses the
736 * same options structure and there are non-default options
737 * used.
738 */
739 if (origeo) {
740 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
741 options_mem_dupe(td->eo, td->io_ops->options);
742 } else {
743 memset(td->eo, 0, td->io_ops->option_struct_size);
744 fill_default_options(td->eo, td->io_ops->options);
745 }
746 *(struct thread_data **)td->eo = td;
747 }
748
749 return 0;
750}
751
906c8d75
JA
752/*
753 * Adds a job to the list of things todo. Sanitizes the various options
754 * to make sure we don't have conflicts, and initializes various
755 * members of td.
756 */
75154845 757static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
ebac4655 758{
413dd459 759 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
6eaf09d6
SL
760 "randread", "randwrite", "randrw",
761 "trim", NULL, NULL, NULL, "randtrim" };
af52b345 762 unsigned int i;
af52b345 763 char fname[PATH_MAX];
e132cbae 764 int numjobs, file_alloced;
ebac4655 765
ebac4655
JA
766 /*
767 * the def_thread is just for options, it's not a real job
768 */
769 if (td == &def_thread)
770 return 0;
771
cca73aa7
JA
772 /*
773 * if we are just dumping the output command line, don't add the job
774 */
775 if (dump_cmdline) {
776 put_job(td);
777 return 0;
778 }
779
58c55ba0 780 if (profile_td_init(td))
66251554 781 goto err;
58c55ba0 782
de890a1e 783 if (ioengine_load(td))
205927a3 784 goto err;
df64119d 785
2dc1bbeb 786 if (td->o.use_thread)
9cedf167
JA
787 nr_thread++;
788 else
789 nr_process++;
790
2dc1bbeb 791 if (td->o.odirect)
690adba3
JA
792 td->io_ops->flags |= FIO_RAWIO;
793
e132cbae 794 file_alloced = 0;
691c8fb0 795 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
e132cbae 796 file_alloced = 1;
80be24f4 797
2dc1bbeb 798 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
e132cbae 799 add_file(td, jobname);
7b05a215 800 else {
2dc1bbeb 801 for (i = 0; i < td->o.nr_files; i++) {
5ec10eaa
JA
802 sprintf(fname, "%s.%d.%d", jobname,
803 td->thread_number, i);
7b05a215
JA
804 add_file(td, fname);
805 }
af52b345 806 }
0af7b542 807 }
ebac4655 808
4e991c23
JA
809 if (fixup_options(td))
810 goto err;
e0a22335 811
9e684a49
DE
812 flow_init_job(td);
813
de890a1e
SL
814 /*
815 * IO engines only need this for option callbacks, and the address may
816 * change in subprocesses.
817 */
818 if (td->eo)
819 *(struct thread_data **)td->eo = NULL;
820
07eb79df
JA
821 if (td->io_ops->flags & FIO_DISKLESSIO) {
822 struct fio_file *f;
823
824 for_each_file(td, f, i)
825 f->real_file_size = -1ULL;
826 }
827
521da527 828 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
ebac4655 829
83349190
YH
830 td->ts.clat_percentiles = td->o.clat_percentiles;
831 if (td->o.overwrite_plist)
802ad4a8 832 memcpy(td->ts.percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
83349190 833 else
802ad4a8 834 memcpy(td->ts.percentile_list, def_percentile_list, sizeof(def_percentile_list));
83349190 835
6eaf09d6
SL
836 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
837 td->ts.clat_stat[i].min_val = ULONG_MAX;
838 td->ts.slat_stat[i].min_val = ULONG_MAX;
839 td->ts.lat_stat[i].min_val = ULONG_MAX;
840 td->ts.bw_stat[i].min_val = ULONG_MAX;
841 }
cdd5411e 842 td->ddir_seq_nr = td->o.ddir_seq_nr;
ebac4655 843
755c3189 844 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
3c5df6fa 845 prev_group_jobs = 0;
ebac4655 846 groupid++;
3c5df6fa 847 }
ebac4655
JA
848
849 td->groupid = groupid;
3c5df6fa 850 prev_group_jobs++;
ebac4655 851
93bcfd20
BC
852 if (init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds))) {
853 td_verror(td, errno, "init_random_state");
9c60ce64 854 goto err;
93bcfd20 855 }
9c60ce64 856
ebac4655
JA
857 if (setup_rate(td))
858 goto err;
859
2dc1bbeb 860 if (td->o.write_lat_log) {
b8bc8cba
JA
861 setup_log(&td->lat_log, td->o.log_avg_msec);
862 setup_log(&td->slat_log, td->o.log_avg_msec);
863 setup_log(&td->clat_log, td->o.log_avg_msec);
ebac4655 864 }
2dc1bbeb 865 if (td->o.write_bw_log)
b8bc8cba 866 setup_log(&td->bw_log, td->o.log_avg_msec);
c8eeb9df 867 if (td->o.write_iops_log)
b8bc8cba 868 setup_log(&td->iops_log, td->o.log_avg_msec);
ebac4655 869
2dc1bbeb
JA
870 if (!td->o.name)
871 td->o.name = strdup(jobname);
01452055 872
c6ae0a5b 873 if (!terse_output) {
b990b5c0 874 if (!job_add_num) {
5ec10eaa
JA
875 if (!strcmp(td->io_ops->name, "cpuio")) {
876 log_info("%s: ioengine=cpu, cpuload=%u,"
877 " cpucycle=%u\n", td->o.name,
878 td->o.cpuload,
879 td->o.cpucycle);
880 } else {
6eaf09d6 881 char *c1, *c2, *c3, *c4, *c5, *c6;
f8977ee6 882
2dc1bbeb
JA
883 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
884 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
885 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
886 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
6eaf09d6
SL
887 c5 = to_kmg(td->o.min_bs[DDIR_TRIM]);
888 c6 = to_kmg(td->o.max_bs[DDIR_TRIM]);
f8977ee6 889
6eaf09d6 890 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s/%s-%s,"
5ec10eaa
JA
891 " ioengine=%s, iodepth=%u\n",
892 td->o.name, td->groupid,
893 ddir_str[td->o.td_ddir],
6eaf09d6 894 c1, c2, c3, c4, c5, c6,
5ec10eaa
JA
895 td->io_ops->name,
896 td->o.iodepth);
f8977ee6
JA
897
898 free(c1);
899 free(c2);
900 free(c3);
901 free(c4);
6eaf09d6
SL
902 free(c5);
903 free(c6);
f8977ee6 904 }
b990b5c0 905 } else if (job_add_num == 1)
6d86144d 906 log_info("...\n");
c6ae0a5b 907 }
ebac4655
JA
908
909 /*
910 * recurse add identical jobs, clear numjobs and stonewall options
911 * as they don't apply to sub-jobs
912 */
2dc1bbeb 913 numjobs = td->o.numjobs;
ebac4655 914 while (--numjobs) {
de890a1e 915 struct thread_data *td_new = get_new_job(0, td, 1);
ebac4655
JA
916
917 if (!td_new)
918 goto err;
919
2dc1bbeb
JA
920 td_new->o.numjobs = 1;
921 td_new->o.stonewall = 0;
92c1d41f 922 td_new->o.new_group = 0;
e132cbae
JA
923
924 if (file_alloced) {
2dc1bbeb 925 td_new->o.filename = NULL;
e132cbae 926 td_new->files_index = 0;
4e6ea2f1 927 td_new->files_size = 0;
e132cbae
JA
928 td_new->files = NULL;
929 }
930
75154845 931 job_add_num = numjobs - 1;
ebac4655 932
75154845 933 if (add_job(td_new, jobname, job_add_num))
ebac4655
JA
934 goto err;
935 }
3c5df6fa 936
ebac4655
JA
937 return 0;
938err:
939 put_job(td);
940 return -1;
941}
942
79d16311
JA
943/*
944 * Parse as if 'o' was a command line
945 */
946void add_job_opts(const char **o)
947{
948 struct thread_data *td, *td_parent;
949 int i, in_global = 1;
950 char jobname[32];
951
952 i = 0;
953 td_parent = td = NULL;
954 while (o[i]) {
955 if (!strncmp(o[i], "name", 4)) {
956 in_global = 0;
957 if (td)
958 add_job(td, jobname, 0);
959 td = NULL;
960 sprintf(jobname, "%s", o[i] + 5);
961 }
962 if (in_global && !td_parent)
de890a1e 963 td_parent = get_new_job(1, &def_thread, 0);
79d16311
JA
964 else if (!in_global && !td) {
965 if (!td_parent)
966 td_parent = &def_thread;
de890a1e 967 td = get_new_job(0, td_parent, 0);
79d16311
JA
968 }
969 if (in_global)
970 fio_options_parse(td_parent, (char **) &o[i], 1);
971 else
972 fio_options_parse(td, (char **) &o[i], 1);
973 i++;
974 }
975
976 if (td)
977 add_job(td, jobname, 0);
978}
979
01f06b63
JA
980static int skip_this_section(const char *name)
981{
ad0a2735
JA
982 int i;
983
984 if (!nr_job_sections)
01f06b63
JA
985 return 0;
986 if (!strncmp(name, "global", 6))
987 return 0;
988
ad0a2735
JA
989 for (i = 0; i < nr_job_sections; i++)
990 if (!strcmp(job_sections[i], name))
991 return 0;
992
993 return 1;
01f06b63
JA
994}
995
ebac4655
JA
996static int is_empty_or_comment(char *line)
997{
998 unsigned int i;
999
1000 for (i = 0; i < strlen(line); i++) {
1001 if (line[i] == ';')
1002 return 1;
5cc2da30
IM
1003 if (line[i] == '#')
1004 return 1;
76cd9378 1005 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
ebac4655
JA
1006 return 0;
1007 }
1008
1009 return 1;
1010}
1011
07261983
JA
1012/*
1013 * This is our [ini] type file parser.
1014 */
50d16976 1015int parse_jobs_ini(char *file, int is_buf, int stonewall_flag)
ebac4655 1016{
e1f36503 1017 unsigned int global;
ebac4655 1018 struct thread_data *td;
fee3bb48 1019 char *string, *name;
ebac4655
JA
1020 FILE *f;
1021 char *p;
0c7e37a0 1022 int ret = 0, stonewall;
097b2991 1023 int first_sect = 1;
ccf8f127 1024 int skip_fgets = 0;
01f06b63 1025 int inside_skip = 0;
3b8b7135
JA
1026 char **opts;
1027 int i, alloc_opts, num_opts;
ebac4655 1028
50d16976
JA
1029 if (is_buf)
1030 f = NULL;
1031 else {
1032 if (!strcmp(file, "-"))
1033 f = stdin;
1034 else
1035 f = fopen(file, "r");
5a729cbe 1036
50d16976
JA
1037 if (!f) {
1038 perror("fopen job file");
1039 return 1;
1040 }
ebac4655
JA
1041 }
1042
1043 string = malloc(4096);
7f7e6e59
JA
1044
1045 /*
1046 * it's really 256 + small bit, 280 should suffice
1047 */
1048 name = malloc(280);
1049 memset(name, 0, 280);
ebac4655 1050
3b8b7135
JA
1051 alloc_opts = 8;
1052 opts = malloc(sizeof(char *) * alloc_opts);
b7c6302d 1053 num_opts = 0;
3b8b7135 1054
0c7e37a0 1055 stonewall = stonewall_flag;
7c124ac1 1056 do {
ccf8f127
JA
1057 /*
1058 * if skip_fgets is set, we already have loaded a line we
1059 * haven't handled.
1060 */
1061 if (!skip_fgets) {
50d16976
JA
1062 if (is_buf)
1063 p = strsep(&file, "\n");
1064 else
43f74792 1065 p = fgets(string, 4096, f);
ccf8f127
JA
1066 if (!p)
1067 break;
1068 }
cdc7f193 1069
ccf8f127 1070 skip_fgets = 0;
cdc7f193 1071 strip_blank_front(&p);
6c7c7da1 1072 strip_blank_end(p);
cdc7f193 1073
ebac4655
JA
1074 if (is_empty_or_comment(p))
1075 continue;
84dd1886 1076 if (sscanf(p, "[%255[^\n]]", name) != 1) {
01f06b63
JA
1077 if (inside_skip)
1078 continue;
5ec10eaa
JA
1079 log_err("fio: option <%s> outside of [] job section\n",
1080 p);
088b4207 1081 break;
6c7c7da1 1082 }
ebac4655 1083
7a4b80a1
JA
1084 name[strlen(name) - 1] = '\0';
1085
01f06b63
JA
1086 if (skip_this_section(name)) {
1087 inside_skip = 1;
1088 continue;
1089 } else
1090 inside_skip = 0;
1091
ebac4655
JA
1092 global = !strncmp(name, "global", 6);
1093
cca73aa7 1094 if (dump_cmdline) {
097b2991
JA
1095 if (first_sect)
1096 log_info("fio ");
cca73aa7
JA
1097 if (!global)
1098 log_info("--name=%s ", name);
097b2991 1099 first_sect = 0;
cca73aa7
JA
1100 }
1101
de890a1e 1102 td = get_new_job(global, &def_thread, 0);
45410acb
JA
1103 if (!td) {
1104 ret = 1;
1105 break;
1106 }
ebac4655 1107
972cfd25
JA
1108 /*
1109 * Seperate multiple job files by a stonewall
1110 */
f9481919 1111 if (!global && stonewall) {
2dc1bbeb 1112 td->o.stonewall = stonewall;
972cfd25
JA
1113 stonewall = 0;
1114 }
1115
3b8b7135
JA
1116 num_opts = 0;
1117 memset(opts, 0, alloc_opts * sizeof(char *));
1118
50d16976
JA
1119 while (1) {
1120 if (is_buf)
1121 p = strsep(&file, "\n");
1122 else
1123 p = fgets(string, 4096, f);
1124 if (!p)
1125 break;
1126
ebac4655
JA
1127 if (is_empty_or_comment(p))
1128 continue;
e1f36503 1129
b6754f9d 1130 strip_blank_front(&p);
7c124ac1 1131
ccf8f127
JA
1132 /*
1133 * new section, break out and make sure we don't
1134 * fgets() a new line at the top.
1135 */
1136 if (p[0] == '[') {
1137 skip_fgets = 1;
7c124ac1 1138 break;
ccf8f127 1139 }
7c124ac1 1140
4ae3f763 1141 strip_blank_end(p);
aea47d44 1142
3b8b7135
JA
1143 if (num_opts == alloc_opts) {
1144 alloc_opts <<= 1;
1145 opts = realloc(opts,
1146 alloc_opts * sizeof(char *));
1147 }
1148
1149 opts[num_opts] = strdup(p);
1150 num_opts++;
ebac4655 1151 }
ebac4655 1152
3b8b7135
JA
1153 ret = fio_options_parse(td, opts, num_opts);
1154 if (!ret) {
1155 if (dump_cmdline)
1156 for (i = 0; i < num_opts; i++)
1157 log_info("--%s ", opts[i]);
1158
45410acb 1159 ret = add_job(td, name, 0);
3b8b7135 1160 } else {
b1508cf9
JA
1161 log_err("fio: job %s dropped\n", name);
1162 put_job(td);
45410acb 1163 }
3b8b7135
JA
1164
1165 for (i = 0; i < num_opts; i++)
1166 free(opts[i]);
1167 num_opts = 0;
7c124ac1 1168 } while (!ret);
ebac4655 1169
cca73aa7
JA
1170 if (dump_cmdline)
1171 log_info("\n");
1172
7e356b2d
JA
1173 i = 0;
1174 while (i < nr_job_sections) {
1175 free(job_sections[i]);
1176 i++;
1177 }
1178
3b8b7135
JA
1179 for (i = 0; i < num_opts; i++)
1180 free(opts[i]);
1181
ebac4655
JA
1182 free(string);
1183 free(name);
2577594d 1184 free(opts);
50d16976 1185 if (!is_buf && f != stdin)
5a729cbe 1186 fclose(f);
45410acb 1187 return ret;
ebac4655
JA
1188}
1189
1190static int fill_def_thread(void)
1191{
1192 memset(&def_thread, 0, sizeof(def_thread));
1193
375b2695 1194 fio_getaffinity(getpid(), &def_thread.o.cpumask);
8c029376 1195 def_thread.o.timeout = def_timeout;
ebac4655
JA
1196
1197 /*
ee738499 1198 * fill default options
ebac4655 1199 */
214e1eca 1200 fio_fill_default_options(&def_thread);
ebac4655
JA
1201 return 0;
1202}
1203
45378b35 1204static void usage(const char *name)
4785f995 1205{
692a5d22 1206 printf("%s\n", fio_version_string);
45378b35 1207 printf("%s [options] [job options] <job file(s)>\n", name);
83881283
JA
1208 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1209 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
1210 "\t\t\tdiskutil,job,mutex,profile,time,net\n");
1211 printf(" --output\t\tWrite output to file\n");
b2cecdc2 1212 printf(" --runtime\t\tRuntime in seconds\n");
83881283
JA
1213 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1214 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1215 printf(" --minimal\t\tMinimal (terse) output\n");
1216 printf(" --version\t\tPrint version info and exit\n");
75db6e2c 1217 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
83881283
JA
1218 printf(" --help\t\tPrint this page\n");
1219 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
5ec10eaa 1220 " them\n");
de890a1e
SL
1221 printf(" --enghelp=engine\tPrint ioengine help, or list"
1222 " available ioengines\n");
1223 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1224 " cmd\n");
83881283
JA
1225 printf(" --showcmd\t\tTurn a job file into command line options\n");
1226 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1227 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
1228 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
5ec10eaa 1229 " writes\n");
83881283
JA
1230 printf(" --section=name\tOnly run specified section in job file\n");
1231 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
2b386d25 1232 " (def 1024)\n");
83881283
JA
1233 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1234 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1235 printf(" --server=args\t\tStart a backend fio server\n");
1236 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1237 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
aa58d252 1238 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
83881283 1239 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
4785f995
JA
1240}
1241
79e48f72 1242#ifdef FIO_INC_DEBUG
ee56ad50 1243struct debug_level debug_levels[] = {
bd6f78b2
JA
1244 { .name = "process", .shift = FD_PROCESS, },
1245 { .name = "file", .shift = FD_FILE, },
1246 { .name = "io", .shift = FD_IO, },
1247 { .name = "mem", .shift = FD_MEM, },
1248 { .name = "blktrace", .shift = FD_BLKTRACE },
1249 { .name = "verify", .shift = FD_VERIFY },
84422acd 1250 { .name = "random", .shift = FD_RANDOM },
a3d741fa 1251 { .name = "parse", .shift = FD_PARSE },
cd991b9e 1252 { .name = "diskutil", .shift = FD_DISKUTIL },
5e1d306e 1253 { .name = "job", .shift = FD_JOB },
29adda3c 1254 { .name = "mutex", .shift = FD_MUTEX },
79d16311 1255 { .name = "profile", .shift = FD_PROFILE },
c223da83 1256 { .name = "time", .shift = FD_TIME },
eb7976ef 1257 { .name = "net", .shift = FD_NET },
02444ad1 1258 { .name = NULL, },
ee56ad50
JA
1259};
1260
c09823ab 1261static int set_debug(const char *string)
ee56ad50
JA
1262{
1263 struct debug_level *dl;
1264 char *p = (char *) string;
1265 char *opt;
1266 int i;
1267
1268 if (!strcmp(string, "?") || !strcmp(string, "help")) {
ee56ad50
JA
1269 log_info("fio: dumping debug options:");
1270 for (i = 0; debug_levels[i].name; i++) {
1271 dl = &debug_levels[i];
1272 log_info("%s,", dl->name);
1273 }
bd6f78b2 1274 log_info("all\n");
c09823ab 1275 return 1;
ee56ad50
JA
1276 }
1277
1278 while ((opt = strsep(&p, ",")) != NULL) {
1279 int found = 0;
1280
5e1d306e
JA
1281 if (!strncmp(opt, "all", 3)) {
1282 log_info("fio: set all debug options\n");
1283 fio_debug = ~0UL;
1284 continue;
1285 }
1286
ee56ad50
JA
1287 for (i = 0; debug_levels[i].name; i++) {
1288 dl = &debug_levels[i];
5e1d306e
JA
1289 found = !strncmp(opt, dl->name, strlen(dl->name));
1290 if (!found)
1291 continue;
1292
1293 if (dl->shift == FD_JOB) {
1294 opt = strchr(opt, ':');
1295 if (!opt) {
1296 log_err("fio: missing job number\n");
1297 break;
1298 }
1299 opt++;
1300 fio_debug_jobno = atoi(opt);
1301 log_info("fio: set debug jobno %d\n",
1302 fio_debug_jobno);
1303 } else {
ee56ad50 1304 log_info("fio: set debug option %s\n", opt);
bd6f78b2 1305 fio_debug |= (1UL << dl->shift);
ee56ad50 1306 }
5e1d306e 1307 break;
ee56ad50
JA
1308 }
1309
1310 if (!found)
1311 log_err("fio: debug mask %s not found\n", opt);
1312 }
c09823ab 1313 return 0;
ee56ad50 1314}
79e48f72 1315#else
69b98d4c 1316static int set_debug(const char *string)
79e48f72
JA
1317{
1318 log_err("fio: debug tracing not included in build\n");
c09823ab 1319 return 1;
79e48f72
JA
1320}
1321#endif
9ac8a797 1322
4c6107ff
JA
1323static void fio_options_fill_optstring(void)
1324{
1325 char *ostr = cmd_optstr;
1326 int i, c;
1327
1328 c = i = 0;
1329 while (l_opts[i].name) {
1330 ostr[c++] = l_opts[i].val;
1331 if (l_opts[i].has_arg == required_argument)
1332 ostr[c++] = ':';
1333 else if (l_opts[i].has_arg == optional_argument) {
1334 ostr[c++] = ':';
1335 ostr[c++] = ':';
1336 }
1337 i++;
1338 }
1339 ostr[c] = '\0';
1340}
1341
c2c94585
JA
1342static int client_flag_set(char c)
1343{
1344 int i;
1345
1346 i = 0;
1347 while (l_opts[i].name) {
1348 int val = l_opts[i].val;
1349
1350 if (c == (val & 0xff))
1351 return (val & FIO_CLIENT_FLAG);
1352
1353 i++;
1354 }
1355
1356 return 0;
1357}
1358
fa2ea806 1359void parse_cmd_client(void *client, char *opt)
7a4b8240 1360{
fa2ea806 1361 fio_client_add_cmd_option(client, opt);
7a4b8240
JA
1362}
1363
81179eec 1364int parse_cmd_line(int argc, char *argv[])
ebac4655 1365{
b4692828 1366 struct thread_data *td = NULL;
c09823ab 1367 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
4c6107ff 1368 char *ostr = cmd_optstr;
402668f3 1369 void *pid_file = NULL;
9bae26e8 1370 void *cur_client = NULL;
81179eec 1371 int backend = 0;
ebac4655 1372
c2c94585
JA
1373 /*
1374 * Reset optind handling, since we may call this multiple times
1375 * for the backend.
1376 */
1377 optind = 1;
7a4b8240 1378
c2c94585 1379 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
085399db
JA
1380 did_arg = 1;
1381
c2c94585 1382 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
fa2ea806 1383 parse_cmd_client(cur_client, argv[optind - 1]);
7a4b8240
JA
1384 c &= ~FIO_CLIENT_FLAG;
1385 }
1386
ebac4655 1387 switch (c) {
2b386d25
JA
1388 case 'a':
1389 smalloc_pool_size = atoi(optarg);
1390 break;
b4692828 1391 case 't':
8c029376 1392 def_timeout = atoi(optarg);
b4692828
JA
1393 break;
1394 case 'l':
1395 write_lat_log = 1;
1396 break;
3d73e5a9 1397 case 'b':
b4692828
JA
1398 write_bw_log = 1;
1399 break;
1400 case 'o':
1401 f_out = fopen(optarg, "w+");
1402 if (!f_out) {
1403 perror("fopen output");
1404 exit(1);
1405 }
1406 f_err = f_out;
1407 break;
1408 case 'm':
1409 terse_output = 1;
1410 break;
1411 case 'h':
7874f8b7 1412 if (!cur_client) {
c2c94585 1413 usage(argv[0]);
7874f8b7
JA
1414 do_exit++;
1415 }
c2c94585 1416 break;
fd28ca49 1417 case 'c':
7874f8b7 1418 if (!cur_client) {
c2c94585 1419 fio_show_option_help(optarg);
7874f8b7
JA
1420 do_exit++;
1421 }
c2c94585 1422 break;
de890a1e
SL
1423 case 'i':
1424 if (!cur_client) {
1425 fio_show_ioengine_help(optarg);
1426 do_exit++;
1427 }
1428 break;
cca73aa7
JA
1429 case 's':
1430 dump_cmdline = 1;
1431 break;
724e4435
JA
1432 case 'r':
1433 read_only = 1;
1434 break;
b4692828 1435 case 'v':
7874f8b7 1436 if (!cur_client) {
692a5d22 1437 log_info("%s\n", fio_version_string);
7874f8b7
JA
1438 do_exit++;
1439 }
c2c94585 1440 break;
f57a9c59
JA
1441 case 'V':
1442 terse_version = atoi(optarg);
3449ab8c
JA
1443 if (!(terse_version == 2 || terse_version == 3) ||
1444 (terse_version == 4)) {
f57a9c59
JA
1445 log_err("fio: bad terse version format\n");
1446 exit_val = 1;
1447 do_exit++;
1448 }
1449 break;
e592a06b
AC
1450 case 'e':
1451 if (!strcmp("always", optarg))
1452 eta_print = FIO_ETA_ALWAYS;
1453 else if (!strcmp("never", optarg))
1454 eta_print = FIO_ETA_NEVER;
1455 break;
ee56ad50 1456 case 'd':
c09823ab
JA
1457 if (set_debug(optarg))
1458 do_exit++;
ee56ad50 1459 break;
ad0a2735
JA
1460 case 'x': {
1461 size_t new_size;
1462
01f06b63 1463 if (!strcmp(optarg, "global")) {
5ec10eaa
JA
1464 log_err("fio: can't use global as only "
1465 "section\n");
c09823ab
JA
1466 do_exit++;
1467 exit_val = 1;
01f06b63
JA
1468 break;
1469 }
ad0a2735
JA
1470 new_size = (nr_job_sections + 1) * sizeof(char *);
1471 job_sections = realloc(job_sections, new_size);
1472 job_sections[nr_job_sections] = strdup(optarg);
1473 nr_job_sections++;
01f06b63 1474 break;
ad0a2735 1475 }
9ac8a797 1476 case 'p':
07b3232d 1477 exec_profile = strdup(optarg);
9ac8a797 1478 break;
b4692828 1479 case FIO_GETOPT_JOB: {
5ec10eaa 1480 const char *opt = l_opts[lidx].name;
b4692828
JA
1481 char *val = optarg;
1482
c2b1e753 1483 if (!strncmp(opt, "name", 4) && td) {
2dc1bbeb 1484 ret = add_job(td, td->o.name ?: "fio", 0);
66251554 1485 if (ret)
c2b1e753 1486 return 0;
c2b1e753
JA
1487 td = NULL;
1488 }
b4692828 1489 if (!td) {
01f06b63 1490 int is_section = !strncmp(opt, "name", 4);
3106f220
JA
1491 int global = 0;
1492
01f06b63 1493 if (!is_section || !strncmp(val, "global", 6))
3106f220 1494 global = 1;
c2b1e753 1495
01f06b63
JA
1496 if (is_section && skip_this_section(val))
1497 continue;
1498
de890a1e
SL
1499 td = get_new_job(global, &def_thread, 1);
1500 if (!td || ioengine_load(td))
b4692828 1501 return 0;
de890a1e 1502 fio_options_set_ioengine_opts(l_opts, td);
b4692828 1503 }
38d0adb0 1504
214e1eca 1505 ret = fio_cmd_option_parse(td, opt, val);
de890a1e
SL
1506
1507 if (!ret && !strcmp(opt, "ioengine")) {
1508 free_ioengine(td);
1509 if (ioengine_load(td))
1510 return 0;
1511 fio_options_set_ioengine_opts(l_opts, td);
1512 }
1513 break;
1514 }
1515 case FIO_GETOPT_IOENGINE: {
1516 const char *opt = l_opts[lidx].name;
1517 char *val = optarg;
1518 opt = l_opts[lidx].name;
1519 val = optarg;
1520 ret = fio_cmd_ioengine_option_parse(td, opt, val);
b4692828
JA
1521 break;
1522 }
a9523c6f
JA
1523 case 'w':
1524 warnings_fatal = 1;
1525 break;
fca70358
JA
1526 case 'j':
1527 max_jobs = atoi(optarg);
1528 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1529 log_err("fio: invalid max jobs: %d\n", max_jobs);
1530 do_exit++;
1531 exit_val = 1;
1532 }
1533 break;
50d16976 1534 case 'S':
a37f69b7 1535 if (nr_clients) {
132159a5
JA
1536 log_err("fio: can't be both client and server\n");
1537 do_exit++;
1538 exit_val = 1;
1539 break;
1540 }
87aa8f19 1541 if (optarg)
bebe6398 1542 fio_server_set_arg(optarg);
50d16976 1543 is_backend = 1;
81179eec 1544 backend = 1;
50d16976 1545 break;
e46d8091 1546 case 'D':
402668f3 1547 pid_file = strdup(optarg);
e46d8091 1548 break;
132159a5
JA
1549 case 'C':
1550 if (is_backend) {
1551 log_err("fio: can't be both client and server\n");
1552 do_exit++;
1553 exit_val = 1;
1554 break;
1555 }
bebe6398
JA
1556 if (fio_client_add(optarg, &cur_client)) {
1557 log_err("fio: failed adding client %s\n", optarg);
1558 do_exit++;
1559 exit_val = 1;
1560 break;
1561 }
bd023601
JA
1562 /*
1563 * If the next argument exists and isn't an option,
1564 * assume it's a job file for this client only.
1565 */
1566 while (optind < argc) {
1567 if (!strncmp(argv[optind], "--", 2) ||
1568 !strncmp(argv[optind], "-", 1))
1569 break;
1570
1571 fio_client_add_ini_file(cur_client, argv[optind]);
1572 optind++;
1573 }
132159a5 1574 break;
b4692828 1575 default:
c09823ab
JA
1576 do_exit++;
1577 exit_val = 1;
b4692828 1578 break;
ebac4655 1579 }
c7d5c941
JA
1580 if (do_exit)
1581 break;
ebac4655 1582 }
c9fad893 1583
c2c94585
JA
1584 if (do_exit) {
1585 if (exit_val && !(is_backend || nr_clients))
1586 exit(exit_val);
1587 }
536582bf 1588
a37f69b7 1589 if (nr_clients && fio_clients_connect()) {
132159a5
JA
1590 do_exit++;
1591 exit_val = 1;
cdf54d85 1592 return -1;
132159a5
JA
1593 }
1594
81179eec 1595 if (is_backend && backend)
402668f3 1596 return fio_start_server(pid_file);
50d16976 1597
b4692828 1598 if (td) {
7d6a8904 1599 if (!ret)
2dc1bbeb 1600 ret = add_job(td, td->o.name ?: "fio", 0);
972cfd25 1601 }
774a6177 1602
7874f8b7 1603 while (!ret && optind < argc) {
b4692828
JA
1604 ini_idx++;
1605 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1606 ini_file[ini_idx - 1] = strdup(argv[optind]);
1607 optind++;
eb8bbf48 1608 }
972cfd25
JA
1609
1610 return ini_idx;
ebac4655
JA
1611}
1612
ebac4655
JA
1613int parse_options(int argc, char *argv[])
1614{
972cfd25
JA
1615 int job_files, i;
1616
b4692828
JA
1617 f_out = stdout;
1618 f_err = stderr;
1619
4c6107ff 1620 fio_options_fill_optstring();
5ec10eaa 1621 fio_options_dup_and_init(l_opts);
b4692828 1622
9d9eb2e7
JA
1623 atexit(free_shm);
1624
ebac4655
JA
1625 if (fill_def_thread())
1626 return 1;
1627
972cfd25 1628 job_files = parse_cmd_line(argc, argv);
ebac4655 1629
cdf54d85
JA
1630 if (job_files > 0) {
1631 for (i = 0; i < job_files; i++) {
1632 if (fill_def_thread())
132159a5 1633 return 1;
cdf54d85
JA
1634 if (nr_clients) {
1635 if (fio_clients_send_ini(ini_file[i]))
1636 return 1;
1637 free(ini_file[i]);
1638 } else if (!is_backend) {
1639 if (parse_jobs_ini(ini_file[i], 0, i))
1640 return 1;
1641 free(ini_file[i]);
1642 }
132159a5 1643 }
bd023601
JA
1644 } else if (nr_clients) {
1645 if (fill_def_thread())
1646 return 1;
1647 if (fio_clients_send_ini(NULL))
1648 return 1;
972cfd25 1649 }
ebac4655 1650
88c6ed80 1651 free(ini_file);
7e356b2d 1652 fio_options_free(&def_thread);
b4692828
JA
1653
1654 if (!thread_number) {
cca73aa7
JA
1655 if (dump_cmdline)
1656 return 0;
07b3232d
JA
1657 if (exec_profile)
1658 return 0;
a37f69b7 1659 if (is_backend || nr_clients)
5c341e9a 1660 return 0;
085399db
JA
1661 if (did_arg)
1662 return 0;
cca73aa7 1663
d65e11c6 1664 log_err("No jobs(s) defined\n\n");
085399db
JA
1665
1666 if (!did_arg) {
1667 usage(argv[0]);
1668 return 1;
1669 }
1670
1671 return 0;
b4692828
JA
1672 }
1673
be4ecfdf
JA
1674 if (def_thread.o.gtod_offload) {
1675 fio_gtod_init();
1676 fio_gtod_offload = 1;
1677 fio_gtod_cpu = def_thread.o.gtod_cpu;
1678 }
1679
f6dea4d3 1680 if (!terse_output)
692a5d22 1681 log_info("%s\n", fio_version_string);
f6dea4d3 1682
ebac4655
JA
1683 return 0;
1684}