blob: b9dee19bb2894a39d0000a56b013279087d4026f [file] [log] [blame]
Jens Axboe906c8d72006-06-13 09:37:56 +02001/*
Jens Axboecb2c86f2006-10-26 13:48:34 +02002 * This file contains job initialization and setup functions.
Jens Axboe906c8d72006-06-13 09:37:56 +02003 */
Jens Axboeebac4652005-12-08 15:25:21 +01004#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>
Jens Axboeb4692822006-10-27 13:43:22 +020011#include <getopt.h>
Jens Axboeebac4652005-12-08 15:25:21 +010012#include <sys/ipc.h>
13#include <sys/shm.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16
17#include "fio.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020018#include "parse.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010019#include "smalloc.h"
Jens Axboe380065a2008-03-01 18:56:24 +010020#include "filehash.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020021#include "verify.h"
Jens Axboeebac4652005-12-08 15:25:21 +010022
Jens Axboed2a1ed12009-12-08 10:10:31 +010023static char fio_version_string[] = "fio 1.36-rc2";
Jens Axboe214e1ec2007-03-15 10:48:13 +010024
Jens Axboeee738492007-01-10 11:23:16 +010025#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010026
Jens Axboe214e1ec2007-03-15 10:48:13 +010027static char **ini_file;
28static int max_jobs = MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020029static int dump_cmdline;
Jens Axboee1f36502006-10-27 10:54:08 +020030
Jens Axboebe4ecfd2008-12-08 14:10:52 +010031static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010032struct thread_data *threads = NULL;
Jens Axboee1f36502006-10-27 10:54:08 +020033
Jens Axboe214e1ec2007-03-15 10:48:13 +010034int exitall_on_terminate = 0;
35int terse_output = 0;
Aaron Carrolle592a062007-09-14 09:49:41 +020036int eta_print;
Jens Axboe214e1ec2007-03-15 10:48:13 +010037unsigned long long mlock_size = 0;
38FILE *f_out = NULL;
39FILE *f_err = NULL;
Jens Axboe01f06b62008-02-18 20:53:47 +010040char *job_section = NULL;
Jens Axboeee738492007-01-10 11:23:16 +010041
Jens Axboe214e1ec2007-03-15 10:48:13 +010042int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020043int read_only = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020044
Jens Axboe5ec10ea2008-03-06 15:42:00 +010045static int def_timeout;
46static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010047
48static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020049
Jens Axboeee56ad52008-02-01 10:30:20 +010050unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020051unsigned int fio_debug_jobno = -1;
52unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010053
Jens Axboeb4692822006-10-27 13:43:22 +020054/*
55 * Command line options. These will contain the above, plus a few
56 * extra that only pertain to fio itself and not jobs.
57 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010058static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020059 {
60 .name = "output",
61 .has_arg = required_argument,
62 .val = 'o',
63 },
64 {
65 .name = "timeout",
66 .has_arg = required_argument,
67 .val = 't',
68 },
69 {
70 .name = "latency-log",
71 .has_arg = required_argument,
72 .val = 'l',
73 },
74 {
75 .name = "bandwidth-log",
76 .has_arg = required_argument,
77 .val = 'b',
78 },
79 {
80 .name = "minimal",
81 .has_arg = optional_argument,
82 .val = 'm',
83 },
84 {
85 .name = "version",
86 .has_arg = no_argument,
87 .val = 'v',
88 },
89 {
Jens Axboefd28ca42007-01-09 21:20:13 +010090 .name = "help",
91 .has_arg = no_argument,
92 .val = 'h',
93 },
94 {
95 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +010096 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +010097 .val = 'c',
98 },
99 {
Jens Axboecca73aa2007-04-04 11:09:19 +0200100 .name = "showcmd",
101 .has_arg = no_argument,
Jens Axboe724e4432007-09-11 20:02:05 +0200102 .val = 's',
103 },
104 {
105 .name = "readonly",
106 .has_arg = no_argument,
107 .val = 'r',
Jens Axboecca73aa2007-04-04 11:09:19 +0200108 },
109 {
Aaron Carrolle592a062007-09-14 09:49:41 +0200110 .name = "eta",
111 .has_arg = required_argument,
112 .val = 'e',
113 },
114 {
Jens Axboeee56ad52008-02-01 10:30:20 +0100115 .name = "debug",
116 .has_arg = required_argument,
117 .val = 'd',
118 },
119 {
Jens Axboe01f06b62008-02-18 20:53:47 +0100120 .name = "section",
121 .has_arg = required_argument,
122 .val = 'x',
123 },
124 {
Jens Axboe2b386d22008-03-26 10:32:57 +0100125 .name = "alloc-size",
126 .has_arg = required_argument,
127 .val = 'a',
128 },
129 {
Jens Axboe9ac8a792009-11-13 21:23:07 +0100130 .name = "profile",
131 .has_arg = required_argument,
132 .val = 'p',
133 },
134 {
Jens Axboeb4692822006-10-27 13:43:22 +0200135 .name = NULL,
136 },
137};
138
Jens Axboe9ac8a792009-11-13 21:23:07 +0100139static const char *tiobench_opts[] = {
Jens Axboe3f5bf572009-11-13 21:25:03 +0100140 "buffered=0", "size=4*1024*$mb_memory", "bs=4k", "timeout=600",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100141 "numjobs=4", "group_reporting", "thread", "overwrite=1",
142 "filename=.fio.tio.1:.fio.tio.2:.fio.tio.3:.fio.tio.4",
143 "name=seqwrite", "rw=write", "end_fsync=1",
144 "name=randwrite", "stonewall", "rw=randwrite", "end_fsync=1",
145 "name=seqread", "stonewall", "rw=read",
146 "name=randread", "stonewall", "rw=randread", NULL,
147};
148
149static const char **fio_prof_strings[PROFILE_END] = {
150 NULL,
151 tiobench_opts,
152};
153
154static const char *profiles[PROFILE_END] = {
155 "none",
156 "tiobench",
157};
158
Joel Becker9728ce32007-03-01 08:24:39 +0100159FILE *get_f_out()
160{
161 return f_out;
162}
163
164FILE *get_f_err()
165{
166 return f_err;
167}
168
Jens Axboe906c8d72006-06-13 09:37:56 +0200169/*
170 * Return a free job structure.
171 */
Jens Axboeebac4652005-12-08 15:25:21 +0100172static struct thread_data *get_new_job(int global, struct thread_data *parent)
173{
174 struct thread_data *td;
175
176 if (global)
177 return &def_thread;
178 if (thread_number >= max_jobs)
179 return NULL;
180
181 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200182 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100183
Jens Axboee0b0d892009-12-08 10:10:14 +0100184 td->o.uid = td->o.gid = -1U;
185
Jens Axboecade3ef2007-03-23 15:29:45 +0100186 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100187 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100188
Jens Axboeebac4652005-12-08 15:25:21 +0100189 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100190 return td;
191}
192
193static void put_job(struct thread_data *td)
194{
Jens Axboe549577a2006-10-30 12:23:41 +0100195 if (td == &def_thread)
196 return;
197
Jens Axboe16edf252007-02-10 14:59:22 +0100198 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100199 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100200
Jens Axboeebac4652005-12-08 15:25:21 +0100201 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
202 thread_number--;
203}
204
Jens Axboe581e7142009-06-09 12:47:16 +0200205static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100206{
Jens Axboe581e7142009-06-09 12:47:16 +0200207 unsigned int bs = td->o.min_bs[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100208 unsigned long long bytes_per_sec;
Jens Axboe127f6862007-03-15 12:09:57 +0100209
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100210 if (td->o.rate[ddir])
211 bytes_per_sec = td->o.rate[ddir];
212 else
213 bytes_per_sec = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100214
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100215 if (!bytes_per_sec) {
Jens Axboe127f6862007-03-15 12:09:57 +0100216 log_err("rate lower than supported\n");
217 return -1;
218 }
219
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100220 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec;
Jens Axboe581e7142009-06-09 12:47:16 +0200221 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100222 return 0;
223}
224
Jens Axboe581e7142009-06-09 12:47:16 +0200225static int setup_rate(struct thread_data *td)
226{
227 int ret = 0;
228
229 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
230 ret = __setup_rate(td, DDIR_READ);
231 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
232 ret |= __setup_rate(td, DDIR_WRITE);
233
234 return ret;
235}
236
Jens Axboe83472392009-02-19 21:32:12 +0100237static int fixed_block_size(struct thread_options *o)
238{
239 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
240 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
241 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
242}
243
Jens Axboedad915e2006-10-27 11:10:18 +0200244/*
245 * Lazy way of fixing up options that depend on each other. We could also
246 * define option callback handlers, but this is easier.
247 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100248static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200249{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100250 struct thread_options *o = &td->o;
Jens Axboedad915e2006-10-27 11:10:18 +0200251
Jens Axboef356d012009-01-05 09:56:29 +0100252#ifndef FIO_HAVE_PSHARED_MUTEX
253 if (!td->o.use_thread) {
254 log_info("fio: this platform does not support process shared"
255 " mutexes, forcing use of threads. Use the 'thread'"
256 " option to get rid of this warning.\n");
257 td->o.use_thread = 1;
258 }
259#endif
260
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100261 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200262 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100263 free(o->write_iolog_file);
264 o->write_iolog_file = NULL;
Jens Axboe076efc72006-10-27 11:24:25 +0200265 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100266
Jens Axboe16b462a2006-10-30 12:35:18 +0100267 /*
268 * only really works for sequential io for now, and with 1 file
269 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100270 if (o->zone_size && td_random(td) && o->open_files == 1)
271 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100272
273 /*
274 * Reads can do overwrites, we always need to pre-create the file
275 */
276 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100277 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100278
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100279 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100280 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100281 if (!o->max_bs[DDIR_READ])
282 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
283 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100284 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100285 if (!o->max_bs[DDIR_WRITE])
286 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100287
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100288 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100289
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100290 /*
291 * For random IO, allow blockalign offset other than min_bs.
292 */
293 if (!o->ba[DDIR_READ] || !td_random(td))
294 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
295 if (!o->ba[DDIR_WRITE] || !td_random(td))
296 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
297
298 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
299 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
300 !td->o.norandommap) {
301 log_err("fio: Any use of blockalign= turns off randommap\n");
302 td->o.norandommap = 1;
303 }
304
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100305 if (!o->file_size_high)
306 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100307
Jens Axboe83472392009-02-19 21:32:12 +0100308 if (o->norandommap && o->verify != VERIFY_NONE
309 && !fixed_block_size(o)) {
310 log_err("fio: norandommap given for variable block sizes, "
311 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100312 o->verify = VERIFY_NONE;
Jens Axboebb8895e2006-10-30 15:14:48 +0100313 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100314 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100315 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100316
317 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100318 * thinktime_spin must be less than thinktime
319 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100320 if (o->thinktime_spin > o->thinktime)
321 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100322
323 /*
324 * The low water mark cannot be bigger than the iodepth
325 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100326 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100327 /*
328 * syslet work around - if the workload is sequential,
329 * we want to let the queue drain all the way down to
330 * avoid seeking between async threads
331 */
332 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100333 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100334 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100335 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100336 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100337
338 /*
339 * If batch number isn't set, default to the same as iodepth
340 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100341 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
342 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100343
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100344 if (o->nr_files > td->files_index)
345 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100346
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100347 if (o->open_files > o->nr_files || !o->open_files)
348 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100349
Jens Axboe581e7142009-06-09 12:47:16 +0200350 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
351 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
352 o->rate_iops_min[1]))) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100353 log_err("fio: rate and rate_iops are mutually exclusive\n");
354 return 1;
355 }
Jens Axboe581e7142009-06-09 12:47:16 +0200356 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
357 (o->rate_iops[0] < o->rate_iops_min[0]) ||
358 (o->rate_iops[1] < o->rate_iops_min[1])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100359 log_err("fio: minimum rate exceeds rate\n");
360 return 1;
361 }
362
Jens Axboecf4464c2007-04-17 20:14:42 +0200363 if (!o->timeout && o->time_based) {
364 log_err("fio: time_based requires a runtime/timeout setting\n");
365 o->time_based = 0;
366 }
367
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100368 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200369 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100370
Jens Axboe79713142008-02-06 14:40:14 +0100371 if (td_rw(td) && td->o.verify != VERIFY_NONE)
372 log_info("fio: mixed read/write workload with verify. May not "
373 "work as expected, unless you pre-populated the file\n");
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100374
Jens Axboe41ccd842008-05-22 09:17:33 +0200375 if (td->o.verify != VERIFY_NONE)
376 td->o.refill_buffers = 1;
377
Jens Axboe9c0d2242009-07-01 12:26:28 +0200378 if (td->o.pre_read) {
Jens Axboe34f1c042009-06-02 14:19:25 +0200379 td->o.invalidate_cache = 0;
Jens Axboe9c0d2242009-07-01 12:26:28 +0200380 if (td->io_ops->flags & FIO_PIPEIO)
381 log_info("fio: cannot pre-read files with an IO engine"
382 " that isn't seekable. Pre-read disabled.\n");
383 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200384
Jens Axboe4e991c22007-03-15 11:41:11 +0100385 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200386}
387
Jens Axboe906c8d72006-06-13 09:37:56 +0200388/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100389 * This function leaks the buffer
390 */
391static char *to_kmg(unsigned int val)
392{
393 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100394 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100395 char *p = post;
396
Jens Axboe245142f2006-11-08 12:49:21 +0100397 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100398 if (val & 1023)
399 break;
400
401 val >>= 10;
402 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100403 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100404
405 snprintf(buf, 31, "%u%c", val, *p);
406 return buf;
407}
408
Jens Axboe09629a92007-03-09 09:00:06 +0100409/* External engines are specified by "external:name.o") */
410static const char *get_engine_name(const char *str)
411{
412 char *p = strstr(str, ":");
413
414 if (!p)
415 return str;
416
417 p++;
418 strip_blank_front(&p);
419 strip_blank_end(p);
420 return p;
421}
422
Jens Axboee132cba2007-03-14 14:23:54 +0100423static int exists_and_not_file(const char *filename)
424{
425 struct stat sb;
426
427 if (lstat(filename, &sb) == -1)
428 return 0;
429
430 if (S_ISREG(sb.st_mode))
431 return 0;
432
433 return 1;
434}
435
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200436void td_fill_rand_seeds(struct thread_data *td)
437{
438 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
439 os_random_seed(td->rand_seeds[1], &td->verify_state);
440 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
441
442 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
443 os_random_seed(td->rand_seeds[3], &td->next_file_state);
444
445 os_random_seed(td->rand_seeds[5], &td->file_size_state);
446
447 if (!td_random(td))
448 return;
449
450 if (td->o.rand_repeatable)
451 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
452
453 os_random_seed(td->rand_seeds[4], &td->random_state);
454}
455
Jens Axboef8977ee2006-11-06 14:03:03 +0100456/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100457 * Initialize the various random states we need (random io, block size ranges,
458 * read/write mix, etc).
459 */
460static int init_random_state(struct thread_data *td)
461{
Jens Axboe68727072007-03-15 20:49:25 +0100462 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100463
464 fd = open("/dev/urandom", O_RDONLY);
465 if (fd == -1) {
466 td_verror(td, errno, "open");
467 return 1;
468 }
469
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200470 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
471 (int) sizeof(td->rand_seeds)) {
Jens Axboe9c60ce62007-03-15 09:14:47 +0100472 td_verror(td, EIO, "read");
473 close(fd);
474 return 1;
475 }
476
477 close(fd);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200478 td_fill_rand_seeds(td);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100479 return 0;
480}
481
Jens Axboe9c60ce62007-03-15 09:14:47 +0100482/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200483 * Adds a job to the list of things todo. Sanitizes the various options
484 * to make sure we don't have conflicts, and initializes various
485 * members of td.
486 */
Jens Axboe75154842006-06-01 13:56:09 +0200487static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100488{
Jens Axboe413dd452007-02-23 09:26:09 +0100489 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
490 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100491 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100492 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100493 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100494 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100495
Jens Axboeebac4652005-12-08 15:25:21 +0100496 /*
497 * the def_thread is just for options, it's not a real job
498 */
499 if (td == &def_thread)
500 return 0;
501
Jens Axboecca73aa2007-04-04 11:09:19 +0200502 /*
503 * if we are just dumping the output command line, don't add the job
504 */
505 if (dump_cmdline) {
506 put_job(td);
507 return 0;
508 }
509
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100510 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100511 td->io_ops = load_ioengine(td, engine);
512 if (!td->io_ops) {
513 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100514 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100515 }
Jens Axboedf641192006-10-12 07:55:41 +0200516
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100517 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100518 nr_thread++;
519 else
520 nr_process++;
521
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100522 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100523 td->io_ops->flags |= FIO_RAWIO;
524
Jens Axboee132cba2007-03-14 14:23:54 +0100525 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100526 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100527 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100528
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100529 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100530 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100531 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100532 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100533 sprintf(fname, "%s.%d.%d", jobname,
534 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100535 add_file(td, fname);
536 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100537 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100538 }
Jens Axboeebac4652005-12-08 15:25:21 +0100539
Jens Axboe4e991c22007-03-15 11:41:11 +0100540 if (fixup_options(td))
541 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100542
Jens Axboe07eb79d2007-04-12 13:02:38 +0200543 if (td->io_ops->flags & FIO_DISKLESSIO) {
544 struct fio_file *f;
545
546 for_each_file(td, f, i)
547 f->real_file_size = -1ULL;
548 }
549
Jens Axboecdd18ad2008-02-27 18:58:00 +0100550 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100551
Jens Axboe756867b2007-03-06 15:19:24 +0100552 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
553 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
554 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboe1e3d53a2007-03-22 19:01:48 +0100555 td->ddir_nr = td->o.ddir_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100556
Jens Axboe755c3182009-08-25 22:14:29 +0200557 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100558 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100559 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100560 }
Jens Axboeebac4652005-12-08 15:25:21 +0100561
562 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100563 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100564
Jens Axboe9c60ce62007-03-15 09:14:47 +0100565 if (init_random_state(td))
566 goto err;
567
Jens Axboeebac4652005-12-08 15:25:21 +0100568 if (setup_rate(td))
569 goto err;
570
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100571 if (td->o.write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100572 setup_log(&td->ts.slat_log);
573 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100574 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100575 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100576 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100577
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100578 if (!td->o.name)
579 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200580
Jens Axboec6ae0a52006-06-12 11:04:44 +0200581 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200582 if (!job_add_num) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100583 if (!strcmp(td->io_ops->name, "cpuio")) {
584 log_info("%s: ioengine=cpu, cpuload=%u,"
585 " cpucycle=%u\n", td->o.name,
586 td->o.cpuload,
587 td->o.cpucycle);
588 } else {
Jens Axboef8977ee2006-11-06 14:03:03 +0100589 char *c1, *c2, *c3, *c4;
590
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100591 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
592 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
593 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
594 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100595
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100596 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
597 " ioengine=%s, iodepth=%u\n",
598 td->o.name, td->groupid,
599 ddir_str[td->o.td_ddir],
600 c1, c2, c3, c4,
601 td->io_ops->name,
602 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100603
604 free(c1);
605 free(c2);
606 free(c3);
607 free(c4);
608 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200609 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100610 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200611 }
Jens Axboeebac4652005-12-08 15:25:21 +0100612
613 /*
614 * recurse add identical jobs, clear numjobs and stonewall options
615 * as they don't apply to sub-jobs
616 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100617 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100618 while (--numjobs) {
619 struct thread_data *td_new = get_new_job(0, td);
620
621 if (!td_new)
622 goto err;
623
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100624 td_new->o.numjobs = 1;
625 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200626 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100627
628 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100629 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100630 td_new->files_index = 0;
Jens Axboe4e6ea2f2009-03-04 20:17:22 +0100631 td_new->files_size = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100632 td_new->files = NULL;
633 }
634
Jens Axboe75154842006-06-01 13:56:09 +0200635 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100636
Jens Axboe75154842006-06-01 13:56:09 +0200637 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100638 goto err;
639 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100640
Jens Axboeebac4652005-12-08 15:25:21 +0100641 return 0;
642err:
643 put_job(td);
644 return -1;
645}
646
Jens Axboe01f06b62008-02-18 20:53:47 +0100647static int skip_this_section(const char *name)
648{
649 if (!job_section)
650 return 0;
651 if (!strncmp(name, "global", 6))
652 return 0;
653
Jens Axboeb36beb62009-03-05 21:14:21 +0100654 return strcmp(job_section, name);
Jens Axboe01f06b62008-02-18 20:53:47 +0100655}
656
Jens Axboeebac4652005-12-08 15:25:21 +0100657static int is_empty_or_comment(char *line)
658{
659 unsigned int i;
660
661 for (i = 0; i < strlen(line); i++) {
662 if (line[i] == ';')
663 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100664 if (line[i] == '#')
665 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100666 if (!isspace(line[i]) && !iscntrl(line[i]))
667 return 0;
668 }
669
670 return 1;
671}
672
Jens Axboe313cb202006-12-21 09:50:00 +0100673/*
Jens Axboe07261982006-06-07 10:51:12 +0200674 * This is our [ini] type file parser.
675 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100676static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100677{
Jens Axboee1f36502006-10-27 10:54:08 +0200678 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100679 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100680 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100681 FILE *f;
682 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200683 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200684 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200685 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100686 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200687 char **opts;
688 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +0100689
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200690 if (!strcmp(file, "-"))
691 f = stdin;
692 else
693 f = fopen(file, "r");
694
Jens Axboeebac4652005-12-08 15:25:21 +0100695 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200696 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100697 return 1;
698 }
699
700 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200701
702 /*
703 * it's really 256 + small bit, 280 should suffice
704 */
705 name = malloc(280);
706 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100707
Jens Axboe3b8b7132008-06-10 19:46:23 +0200708 alloc_opts = 8;
709 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +0200710 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200711
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200712 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100713 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200714 /*
715 * if skip_fgets is set, we already have loaded a line we
716 * haven't handled.
717 */
718 if (!skip_fgets) {
719 p = fgets(string, 4095, f);
720 if (!p)
721 break;
722 }
gurudas paicdc7f192007-03-29 10:10:56 +0200723
Jens Axboeccf8f122007-09-27 10:48:55 +0200724 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200725 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800726 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200727
Jens Axboeebac4652005-12-08 15:25:21 +0100728 if (is_empty_or_comment(p))
729 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800730 if (sscanf(p, "[%255s]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100731 if (inside_skip)
732 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100733 log_err("fio: option <%s> outside of [] job section\n",
734 p);
Jens Axboe088b4202007-07-19 14:53:01 +0200735 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800736 }
Jens Axboeebac4652005-12-08 15:25:21 +0100737
Jens Axboe01f06b62008-02-18 20:53:47 +0100738 if (skip_this_section(name)) {
739 inside_skip = 1;
740 continue;
741 } else
742 inside_skip = 0;
743
Jens Axboeebac4652005-12-08 15:25:21 +0100744 global = !strncmp(name, "global", 6);
745
746 name[strlen(name) - 1] = '\0';
747
Jens Axboecca73aa2007-04-04 11:09:19 +0200748 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200749 if (first_sect)
750 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200751 if (!global)
752 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200753 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200754 }
755
Jens Axboeebac4652005-12-08 15:25:21 +0100756 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200757 if (!td) {
758 ret = 1;
759 break;
760 }
Jens Axboeebac4652005-12-08 15:25:21 +0100761
Jens Axboe972cfd22006-06-09 11:08:56 +0200762 /*
763 * Seperate multiple job files by a stonewall
764 */
Jens Axboef9481912006-06-09 11:37:28 +0200765 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100766 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200767 stonewall = 0;
768 }
769
Jens Axboe3b8b7132008-06-10 19:46:23 +0200770 num_opts = 0;
771 memset(opts, 0, alloc_opts * sizeof(char *));
772
Jens Axboeebac4652005-12-08 15:25:21 +0100773 while ((p = fgets(string, 4096, f)) != NULL) {
774 if (is_empty_or_comment(p))
775 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200776
Jens Axboeb6754f92006-05-30 14:29:32 +0200777 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100778
Jens Axboeccf8f122007-09-27 10:48:55 +0200779 /*
780 * new section, break out and make sure we don't
781 * fgets() a new line at the top.
782 */
783 if (p[0] == '[') {
784 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100785 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200786 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100787
Jens Axboe4ae3f762006-05-30 13:35:14 +0200788 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200789
Jens Axboe3b8b7132008-06-10 19:46:23 +0200790 if (num_opts == alloc_opts) {
791 alloc_opts <<= 1;
792 opts = realloc(opts,
793 alloc_opts * sizeof(char *));
794 }
795
796 opts[num_opts] = strdup(p);
797 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +0100798 }
Jens Axboeebac4652005-12-08 15:25:21 +0100799
Jens Axboe3b8b7132008-06-10 19:46:23 +0200800 ret = fio_options_parse(td, opts, num_opts);
801 if (!ret) {
802 if (dump_cmdline)
803 for (i = 0; i < num_opts; i++)
804 log_info("--%s ", opts[i]);
805
Jens Axboe45410ac2006-06-07 11:10:39 +0200806 ret = add_job(td, name, 0);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200807 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100808 log_err("fio: job %s dropped\n", name);
809 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200810 }
Jens Axboe3b8b7132008-06-10 19:46:23 +0200811
812 for (i = 0; i < num_opts; i++)
813 free(opts[i]);
814 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100815 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100816
Jens Axboecca73aa2007-04-04 11:09:19 +0200817 if (dump_cmdline)
818 log_info("\n");
819
Jens Axboe3b8b7132008-06-10 19:46:23 +0200820 for (i = 0; i < num_opts; i++)
821 free(opts[i]);
822
Jens Axboeebac4652005-12-08 15:25:21 +0100823 free(string);
824 free(name);
Jens Axboe25775942008-06-10 20:26:06 +0200825 free(opts);
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200826 if (f != stdin)
827 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200828 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100829}
830
831static int fill_def_thread(void)
832{
833 memset(&def_thread, 0, sizeof(def_thread));
834
Jens Axboe375b2692007-05-22 09:13:02 +0200835 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100836
837 /*
Jens Axboeee738492007-01-10 11:23:16 +0100838 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100839 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100840 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100841
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100842 def_thread.o.timeout = def_timeout;
843 def_thread.o.write_bw_log = write_bw_log;
844 def_thread.o.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100845
Jens Axboeebac4652005-12-08 15:25:21 +0100846 return 0;
847}
848
Jens Axboeebac4652005-12-08 15:25:21 +0100849static void free_shm(void)
850{
851 struct shmid_ds sbuf;
852
853 if (threads) {
Jens Axboe5e1d3062008-05-23 11:55:53 +0200854 void *tp = threads;
855
Jens Axboeebac4652005-12-08 15:25:21 +0100856 threads = NULL;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200857 file_hash_exit();
858 fio_debug_jobp = NULL;
859 shmdt(tp);
Jens Axboeebac4652005-12-08 15:25:21 +0100860 shmctl(shm_id, IPC_RMID, &sbuf);
861 }
Jens Axboe2e5cdb12008-03-01 18:52:49 +0100862
863 scleanup();
Jens Axboeebac4652005-12-08 15:25:21 +0100864}
865
Jens Axboe906c8d72006-06-13 09:37:56 +0200866/*
867 * The thread area is shared between the main process and the job
868 * threads/processes. So setup a shared memory segment that will hold
Jens Axboe380065a2008-03-01 18:56:24 +0100869 * all the job info. We use the end of the region for keeping track of
870 * open files across jobs, for file sharing.
Jens Axboe906c8d72006-06-13 09:37:56 +0200871 */
Jens Axboeebac4652005-12-08 15:25:21 +0100872static int setup_thread_area(void)
873{
Jens Axboe380065a2008-03-01 18:56:24 +0100874 void *hash;
875
Jens Axboeebac4652005-12-08 15:25:21 +0100876 /*
877 * 1024 is too much on some machines, scale max_jobs if
878 * we get a failure that looks like too large a shm segment
879 */
880 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200881 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100882
Jens Axboe380065a2008-03-01 18:56:24 +0100883 size += file_hash_size;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200884 size += sizeof(unsigned int);
Jens Axboe380065a2008-03-01 18:56:24 +0100885
Jens Axboe906c8d72006-06-13 09:37:56 +0200886 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100887 if (shm_id != -1)
888 break;
889 if (errno != EINVAL) {
890 perror("shmget");
891 break;
892 }
893
894 max_jobs >>= 1;
895 } while (max_jobs);
896
897 if (shm_id == -1)
898 return 1;
899
900 threads = shmat(shm_id, NULL, 0);
901 if (threads == (void *) -1) {
902 perror("shmat");
903 return 1;
904 }
905
Jens Axboe1f809d12007-10-25 18:34:02 +0200906 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboe380065a2008-03-01 18:56:24 +0100907 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
Jens Axboe5e1d3062008-05-23 11:55:53 +0200908 fio_debug_jobp = (void *) hash + file_hash_size;
909 *fio_debug_jobp = -1;
Jens Axboe380065a2008-03-01 18:56:24 +0100910 file_hash_init(hash);
Jens Axboeebac4652005-12-08 15:25:21 +0100911 atexit(free_shm);
912 return 0;
913}
914
Jens Axboe45378b32007-12-19 13:54:38 +0100915static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +0200916{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100917 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +0100918 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboeee56ad52008-02-01 10:30:20 +0100919 printf("\t--debug=options\tEnable debug logging\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +0100920 printf("\t--output\tWrite output to file\n");
921 printf("\t--timeout\tRuntime in seconds\n");
922 printf("\t--latency-log\tGenerate per-job latency logs\n");
923 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
924 printf("\t--minimal\tMinimal (terse) output\n");
925 printf("\t--version\tPrint version info and exit\n");
926 printf("\t--help\t\tPrint this page\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100927 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
928 " them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +0200929 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +0200930 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
931 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100932 printf("\t--readonly\tTurn on safety read-only checks, preventing"
933 " writes\n");
Jens Axboe01f06b62008-02-18 20:53:47 +0100934 printf("\t--section=name\tOnly run specified section in job file\n");
Jens Axboe2b386d22008-03-26 10:32:57 +0100935 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
936 " (def 1024)\n");
Jens Axboefd4442b2009-06-15 23:29:54 +0200937 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200938}
939
Jens Axboe79e48f72008-02-01 20:37:09 +0100940#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +0100941struct debug_level debug_levels[] = {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100942 { .name = "process", .shift = FD_PROCESS, },
943 { .name = "file", .shift = FD_FILE, },
944 { .name = "io", .shift = FD_IO, },
945 { .name = "mem", .shift = FD_MEM, },
946 { .name = "blktrace", .shift = FD_BLKTRACE },
947 { .name = "verify", .shift = FD_VERIFY },
Jens Axboe84422ac2008-02-19 20:10:26 +0100948 { .name = "random", .shift = FD_RANDOM },
Jens Axboea3d741f2008-02-27 18:32:33 +0100949 { .name = "parse", .shift = FD_PARSE },
Jens Axboecd991b92008-03-07 13:19:35 +0100950 { .name = "diskutil", .shift = FD_DISKUTIL },
Jens Axboe5e1d3062008-05-23 11:55:53 +0200951 { .name = "job", .shift = FD_JOB },
Jens Axboe29adda32009-01-05 19:06:39 +0100952 { .name = "mutex", .shift = FD_MUTEX },
Jens Axboe02444ad2008-10-07 11:33:00 +0200953 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +0100954};
955
Jens Axboec09823a2008-02-19 20:16:57 +0100956static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +0100957{
958 struct debug_level *dl;
959 char *p = (char *) string;
960 char *opt;
961 int i;
962
963 if (!strcmp(string, "?") || !strcmp(string, "help")) {
964 int i;
965
966 log_info("fio: dumping debug options:");
967 for (i = 0; debug_levels[i].name; i++) {
968 dl = &debug_levels[i];
969 log_info("%s,", dl->name);
970 }
Jens Axboebd6f78b2008-02-01 20:27:52 +0100971 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100972 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +0100973 }
974
975 while ((opt = strsep(&p, ",")) != NULL) {
976 int found = 0;
977
Jens Axboe5e1d3062008-05-23 11:55:53 +0200978 if (!strncmp(opt, "all", 3)) {
979 log_info("fio: set all debug options\n");
980 fio_debug = ~0UL;
981 continue;
982 }
983
Jens Axboeee56ad52008-02-01 10:30:20 +0100984 for (i = 0; debug_levels[i].name; i++) {
985 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +0200986 found = !strncmp(opt, dl->name, strlen(dl->name));
987 if (!found)
988 continue;
989
990 if (dl->shift == FD_JOB) {
991 opt = strchr(opt, ':');
992 if (!opt) {
993 log_err("fio: missing job number\n");
994 break;
995 }
996 opt++;
997 fio_debug_jobno = atoi(opt);
998 log_info("fio: set debug jobno %d\n",
999 fio_debug_jobno);
1000 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001001 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001002 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001003 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001004 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001005 }
1006
1007 if (!found)
1008 log_err("fio: debug mask %s not found\n", opt);
1009 }
Jens Axboec09823a2008-02-19 20:16:57 +01001010 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001011}
Jens Axboe79e48f72008-02-01 20:37:09 +01001012#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001013static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001014{
1015 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001016 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001017}
1018#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001019
Jens Axboe9ac8a792009-11-13 21:23:07 +01001020static int load_profile(const char *profile)
1021{
1022 struct thread_data *td, *td_parent;
1023 const char **o;
1024 int i, in_global = 1;
1025 char jobname[32];
1026
1027 dprint(FD_PARSE, "loading profile %s\n", profile);
1028
1029 for (i = 0; i < PROFILE_END; i++) {
1030 if (!strcmp(profile, profiles[i]))
1031 break;
1032 }
1033
1034 if (i == PROFILE_END) {
1035 log_err("fio: unknown profile %s\n", profile);
1036 return 1;
1037 }
1038
1039 o = fio_prof_strings[i];
1040 if (!o)
1041 return 0;
1042
1043 i = 0;
1044 td_parent = td = NULL;
1045 while (o[i]) {
1046 if (!strncmp(o[i], "name", 4)) {
1047 in_global = 0;
1048 if (td)
1049 add_job(td, jobname, 0);
1050 td = NULL;
1051 sprintf(jobname, "%s", o[i] + 5);
1052 }
1053 if (in_global && !td_parent)
1054 td_parent = get_new_job(1, &def_thread);
1055 else if (!in_global && !td) {
1056 if (!td_parent)
1057 td_parent = &def_thread;
1058 td = get_new_job(0, td_parent);
1059 }
1060 if (in_global)
1061 fio_options_parse(td_parent, (char **) &o[i], 1);
1062 else
1063 fio_options_parse(td, (char **) &o[i], 1);
1064 i++;
1065 }
1066 if (td)
1067 add_job(td, jobname, 0);
1068 return 0;
1069}
1070
Jens Axboe214e1ec2007-03-15 10:48:13 +01001071static int parse_cmd_line(int argc, char *argv[])
1072{
1073 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001074 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001075
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001076 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001077 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001078 case 'a':
1079 smalloc_pool_size = atoi(optarg);
1080 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001081 case 't':
1082 def_timeout = atoi(optarg);
1083 break;
1084 case 'l':
1085 write_lat_log = 1;
1086 break;
1087 case 'w':
1088 write_bw_log = 1;
1089 break;
1090 case 'o':
1091 f_out = fopen(optarg, "w+");
1092 if (!f_out) {
1093 perror("fopen output");
1094 exit(1);
1095 }
1096 f_err = f_out;
1097 break;
1098 case 'm':
1099 terse_output = 1;
1100 break;
1101 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +01001102 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001103 exit(0);
1104 case 'c':
1105 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +02001106 case 's':
1107 dump_cmdline = 1;
1108 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001109 case 'r':
1110 read_only = 1;
1111 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001112 case 'v':
1113 printf("%s\n", fio_version_string);
1114 exit(0);
Aaron Carrolle592a062007-09-14 09:49:41 +02001115 case 'e':
1116 if (!strcmp("always", optarg))
1117 eta_print = FIO_ETA_ALWAYS;
1118 else if (!strcmp("never", optarg))
1119 eta_print = FIO_ETA_NEVER;
1120 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001121 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001122 if (set_debug(optarg))
1123 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001124 break;
Jens Axboe01f06b62008-02-18 20:53:47 +01001125 case 'x':
1126 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001127 log_err("fio: can't use global as only "
1128 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001129 do_exit++;
1130 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001131 break;
1132 }
1133 if (job_section)
1134 free(job_section);
1135 job_section = strdup(optarg);
1136 break;
Jens Axboe9ac8a792009-11-13 21:23:07 +01001137 case 'p':
1138 if (load_profile(optarg))
1139 do_exit++;
1140 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001141 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001142 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001143 char *val = optarg;
1144
1145 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001146 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001147 if (ret) {
1148 put_job(td);
1149 return 0;
1150 }
1151 td = NULL;
1152 }
1153 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001154 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001155 int global = 0;
1156
Jens Axboe01f06b62008-02-18 20:53:47 +01001157 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001158 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001159
Jens Axboe01f06b62008-02-18 20:53:47 +01001160 if (is_section && skip_this_section(val))
1161 continue;
1162
Jens Axboe214e1ec2007-03-15 10:48:13 +01001163 td = get_new_job(global, &def_thread);
1164 if (!td)
1165 return 0;
1166 }
1167
1168 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001169 break;
1170 }
1171 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001172 do_exit++;
1173 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001174 break;
1175 }
1176 }
1177
Jens Axboec09823a2008-02-19 20:16:57 +01001178 if (do_exit)
1179 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001180
Jens Axboe214e1ec2007-03-15 10:48:13 +01001181 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001182 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001183 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe7d6a8902008-02-18 20:59:18 +01001184 if (ret)
1185 put_job(td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001186 }
1187
1188 while (optind < argc) {
1189 ini_idx++;
1190 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1191 ini_file[ini_idx - 1] = strdup(argv[optind]);
1192 optind++;
1193 }
1194
1195 return ini_idx;
1196}
1197
Jens Axboeebac4652005-12-08 15:25:21 +01001198int parse_options(int argc, char *argv[])
1199{
Jens Axboe972cfd22006-06-09 11:08:56 +02001200 int job_files, i;
1201
Jens Axboeb4692822006-10-27 13:43:22 +02001202 f_out = stdout;
1203 f_err = stderr;
1204
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001205 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001206
Jens Axboeebac4652005-12-08 15:25:21 +01001207 if (setup_thread_area())
1208 return 1;
1209 if (fill_def_thread())
1210 return 1;
1211
Jens Axboe972cfd22006-06-09 11:08:56 +02001212 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001213
Jens Axboe972cfd22006-06-09 11:08:56 +02001214 for (i = 0; i < job_files; i++) {
1215 if (fill_def_thread())
1216 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001217 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001218 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001219 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001220 }
Jens Axboeebac4652005-12-08 15:25:21 +01001221
Jens Axboe88c6ed82006-06-09 11:28:10 +02001222 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001223 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001224
1225 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001226 if (dump_cmdline)
1227 return 0;
1228
Jens Axboefd4442b2009-06-15 23:29:54 +02001229 log_err("No jobs defined(s)\n\n");
Jens Axboe45378b32007-12-19 13:54:38 +01001230 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +02001231 return 1;
1232 }
1233
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001234 if (def_thread.o.gtod_offload) {
1235 fio_gtod_init();
1236 fio_gtod_offload = 1;
1237 fio_gtod_cpu = def_thread.o.gtod_cpu;
1238 }
1239
Jens Axboeebac4652005-12-08 15:25:21 +01001240 return 0;
1241}