blob: 2d467171f0bc0410adaccb897bf12679aaf913fc [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>
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"
Jens Axboecb2c86f2006-10-26 13:48:34 +020017#include "parse.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010018#include "smalloc.h"
Jens Axboe380065a2008-03-01 18:56:24 +010019#include "filehash.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020020#include "verify.h"
Jens Axboe79d16312010-03-04 12:43:20 +010021#include "profile.h"
Jens Axboeebac4652005-12-08 15:25:21 +010022
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040023#include "lib/getopt.h"
24
Jens Axboe964f5b92011-01-14 20:51:54 +010025static char fio_version_string[] = "fio 1.50-rc3";
Jens Axboe214e1ec2007-03-15 10:48:13 +010026
Jens Axboeee738492007-01-10 11:23:16 +010027#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010028
Jens Axboe214e1ec2007-03-15 10:48:13 +010029static char **ini_file;
30static int max_jobs = MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020031static int dump_cmdline;
Jens Axboee1f36502006-10-27 10:54:08 +020032
Jens Axboebe4ecfd2008-12-08 14:10:52 +010033static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010034struct thread_data *threads = NULL;
Jens Axboee1f36502006-10-27 10:54:08 +020035
Jens Axboe214e1ec2007-03-15 10:48:13 +010036int exitall_on_terminate = 0;
37int terse_output = 0;
Aaron Carrolle592a062007-09-14 09:49:41 +020038int eta_print;
Jens Axboe214e1ec2007-03-15 10:48:13 +010039unsigned long long mlock_size = 0;
40FILE *f_out = NULL;
41FILE *f_err = NULL;
Jens Axboe01f06b62008-02-18 20:53:47 +010042char *job_section = NULL;
Jens Axboe07b32322010-03-05 09:48:44 +010043char *exec_profile = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -070044int warnings_fatal = 0;
Jens Axboeee738492007-01-10 11:23:16 +010045
Jens Axboe214e1ec2007-03-15 10:48:13 +010046int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020047int read_only = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020048
Jens Axboe5ec10ea2008-03-06 15:42:00 +010049static int def_timeout;
50static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010051
52static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020053
Jens Axboeee56ad52008-02-01 10:30:20 +010054unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020055unsigned int fio_debug_jobno = -1;
56unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010057
Jens Axboeb4692822006-10-27 13:43:22 +020058/*
59 * Command line options. These will contain the above, plus a few
60 * extra that only pertain to fio itself and not jobs.
61 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010062static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020063 {
64 .name = "output",
65 .has_arg = required_argument,
66 .val = 'o',
67 },
68 {
69 .name = "timeout",
70 .has_arg = required_argument,
71 .val = 't',
72 },
73 {
74 .name = "latency-log",
75 .has_arg = required_argument,
76 .val = 'l',
77 },
78 {
79 .name = "bandwidth-log",
80 .has_arg = required_argument,
81 .val = 'b',
82 },
83 {
84 .name = "minimal",
85 .has_arg = optional_argument,
86 .val = 'm',
87 },
88 {
89 .name = "version",
90 .has_arg = no_argument,
91 .val = 'v',
92 },
93 {
Jens Axboefd28ca42007-01-09 21:20:13 +010094 .name = "help",
95 .has_arg = no_argument,
96 .val = 'h',
97 },
98 {
99 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100100 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +0100101 .val = 'c',
102 },
103 {
Jens Axboecca73aa2007-04-04 11:09:19 +0200104 .name = "showcmd",
105 .has_arg = no_argument,
Jens Axboe724e4432007-09-11 20:02:05 +0200106 .val = 's',
107 },
108 {
109 .name = "readonly",
110 .has_arg = no_argument,
111 .val = 'r',
Jens Axboecca73aa2007-04-04 11:09:19 +0200112 },
113 {
Aaron Carrolle592a062007-09-14 09:49:41 +0200114 .name = "eta",
115 .has_arg = required_argument,
116 .val = 'e',
117 },
118 {
Jens Axboeee56ad52008-02-01 10:30:20 +0100119 .name = "debug",
120 .has_arg = required_argument,
121 .val = 'd',
122 },
123 {
Jens Axboe01f06b62008-02-18 20:53:47 +0100124 .name = "section",
125 .has_arg = required_argument,
126 .val = 'x',
127 },
128 {
Jens Axboe2b386d22008-03-26 10:32:57 +0100129 .name = "alloc-size",
130 .has_arg = required_argument,
131 .val = 'a',
132 },
133 {
Jens Axboe9ac8a792009-11-13 21:23:07 +0100134 .name = "profile",
135 .has_arg = required_argument,
136 .val = 'p',
137 },
138 {
Jens Axboea9523c62011-01-17 16:49:54 -0700139 .name = "warnings-fatal",
140 .has_arg = no_argument,
141 .val = 'w',
142 },
143 {
Jens Axboeb4692822006-10-27 13:43:22 +0200144 .name = NULL,
145 },
146};
147
Joel Becker9728ce32007-03-01 08:24:39 +0100148FILE *get_f_out()
149{
150 return f_out;
151}
152
153FILE *get_f_err()
154{
155 return f_err;
156}
157
Jens Axboe906c8d72006-06-13 09:37:56 +0200158/*
159 * Return a free job structure.
160 */
Jens Axboeebac4652005-12-08 15:25:21 +0100161static struct thread_data *get_new_job(int global, struct thread_data *parent)
162{
163 struct thread_data *td;
164
165 if (global)
166 return &def_thread;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100167 if (thread_number >= max_jobs) {
168 log_err("error: maximum number of jobs (%d) reached.\n",
169 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100170 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100171 }
Jens Axboeebac4652005-12-08 15:25:21 +0100172
173 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200174 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100175
Jens Axboee0b0d892009-12-08 10:10:14 +0100176 td->o.uid = td->o.gid = -1U;
177
Jens Axboecade3ef2007-03-23 15:29:45 +0100178 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100179 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100180
Jens Axboe15dc1932010-03-05 10:59:06 +0100181 profile_add_hooks(td);
182
Jens Axboeebac4652005-12-08 15:25:21 +0100183 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100184 return td;
185}
186
187static void put_job(struct thread_data *td)
188{
Jens Axboe549577a2006-10-30 12:23:41 +0100189 if (td == &def_thread)
190 return;
Jens Axboe58c55ba2010-03-09 12:20:08 +0100191
192 profile_td_exit(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100193
Jens Axboe16edf252007-02-10 14:59:22 +0100194 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100195 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100196
Jens Axboeebac4652005-12-08 15:25:21 +0100197 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
198 thread_number--;
199}
200
Jens Axboe581e7142009-06-09 12:47:16 +0200201static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100202{
Jens Axboe581e7142009-06-09 12:47:16 +0200203 unsigned int bs = td->o.min_bs[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100204 unsigned long long bytes_per_sec;
Jens Axboe127f6862007-03-15 12:09:57 +0100205
Jens Axboeff58fce2010-08-25 12:02:08 +0200206 assert(ddir_rw(ddir));
207
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100208 if (td->o.rate[ddir])
209 bytes_per_sec = td->o.rate[ddir];
210 else
211 bytes_per_sec = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100212
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100213 if (!bytes_per_sec) {
Jens Axboe127f6862007-03-15 12:09:57 +0100214 log_err("rate lower than supported\n");
215 return -1;
216 }
217
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100218 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec;
Jens Axboe581e7142009-06-09 12:47:16 +0200219 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100220 return 0;
221}
222
Jens Axboe581e7142009-06-09 12:47:16 +0200223static int setup_rate(struct thread_data *td)
224{
225 int ret = 0;
226
227 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
228 ret = __setup_rate(td, DDIR_READ);
229 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
230 ret |= __setup_rate(td, DDIR_WRITE);
231
232 return ret;
233}
234
Jens Axboe83472392009-02-19 21:32:12 +0100235static int fixed_block_size(struct thread_options *o)
236{
237 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
238 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
239 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
240}
241
Jens Axboedad915e2006-10-27 11:10:18 +0200242/*
243 * Lazy way of fixing up options that depend on each other. We could also
244 * define option callback handlers, but this is easier.
245 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100246static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200247{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100248 struct thread_options *o = &td->o;
Jens Axboea9523c62011-01-17 16:49:54 -0700249 int ret;
Jens Axboedad915e2006-10-27 11:10:18 +0200250
Jens Axboef356d012009-01-05 09:56:29 +0100251#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100252 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100253 log_info("fio: this platform does not support process shared"
254 " mutexes, forcing use of threads. Use the 'thread'"
255 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100256 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700257 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100258 }
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 Axboea9523c62011-01-17 16:49:54 -0700265 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200266 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100267
Jens Axboe16b462a2006-10-30 12:35:18 +0100268 /*
269 * only really works for sequential io for now, and with 1 file
270 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100271 if (o->zone_size && td_random(td) && o->open_files == 1)
272 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100273
274 /*
275 * Reads can do overwrites, we always need to pre-create the file
276 */
277 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100278 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100279
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100280 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100281 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100282 if (!o->max_bs[DDIR_READ])
283 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
284 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100285 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100286 if (!o->max_bs[DDIR_WRITE])
287 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100288
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100289 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100290
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100291 /*
292 * For random IO, allow blockalign offset other than min_bs.
293 */
294 if (!o->ba[DDIR_READ] || !td_random(td))
295 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
296 if (!o->ba[DDIR_WRITE] || !td_random(td))
297 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
298
299 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
300 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100301 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100302 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100303 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700304 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100305 }
306
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100307 if (!o->file_size_high)
308 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100309
Jens Axboe83472392009-02-19 21:32:12 +0100310 if (o->norandommap && o->verify != VERIFY_NONE
311 && !fixed_block_size(o)) {
312 log_err("fio: norandommap given for variable block sizes, "
313 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100314 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700315 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100316 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100317 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100318 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100319
320 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100321 * thinktime_spin must be less than thinktime
322 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100323 if (o->thinktime_spin > o->thinktime)
324 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100325
326 /*
327 * The low water mark cannot be bigger than the iodepth
328 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100329 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100330 /*
331 * syslet work around - if the workload is sequential,
332 * we want to let the queue drain all the way down to
333 * avoid seeking between async threads
334 */
335 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100336 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100337 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100338 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100339 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100340
341 /*
342 * If batch number isn't set, default to the same as iodepth
343 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100344 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
345 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100346
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100347 if (o->nr_files > td->files_index)
348 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100349
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100350 if (o->open_files > o->nr_files || !o->open_files)
351 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100352
Jens Axboe581e7142009-06-09 12:47:16 +0200353 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
354 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
355 o->rate_iops_min[1]))) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100356 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700357 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100358 }
Jens Axboe581e7142009-06-09 12:47:16 +0200359 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
360 (o->rate_iops[0] < o->rate_iops_min[0]) ||
361 (o->rate_iops[1] < o->rate_iops_min[1])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100362 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700363 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100364 }
365
Jens Axboecf4464c2007-04-17 20:14:42 +0200366 if (!o->timeout && o->time_based) {
367 log_err("fio: time_based requires a runtime/timeout setting\n");
368 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700369 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200370 }
371
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100372 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200373 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100374
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100375 if (o->verify != VERIFY_NONE) {
Jens Axboea9523c62011-01-17 16:49:54 -0700376 if (td_rw(td)) {
377 log_info("fio: mixed read/write workload with verify. "
378 "May not work as expected, unless you "
379 "pre-populated the file\n");
380 ret = warnings_fatal;
381 }
382 if (td_write(td) && o->numjobs) {
383 log_info("Multiple writers may overwrite blocks that "
384 "belong to other jobs. This can cause "
385 "verification failures.\n");
386 ret = warnings_fatal;
387 }
388
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100389 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200390 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
391 !o->verify_interval)
392 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboed9905882010-03-18 20:43:00 +0100393 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200394
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100395 if (o->pre_read) {
396 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700397 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200398 log_info("fio: cannot pre-read files with an IO engine"
399 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700400 ret = warnings_fatal;
401 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200402 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200403
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700404#ifndef FIO_HAVE_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100405 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700406 log_info("fio: this platform does not support fdatasync()"
407 " falling back to using fsync(). Use the 'fsync'"
408 " option instead of 'fdatasync' to get rid of"
409 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100410 o->fsync_blocks = o->fdatasync_blocks;
411 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700412 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700413 }
414#endif
415
Jens Axboea9523c62011-01-17 16:49:54 -0700416 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200417}
418
Jens Axboe906c8d72006-06-13 09:37:56 +0200419/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100420 * This function leaks the buffer
421 */
422static char *to_kmg(unsigned int val)
423{
424 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100425 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100426 char *p = post;
427
Jens Axboe245142f2006-11-08 12:49:21 +0100428 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100429 if (val & 1023)
430 break;
431
432 val >>= 10;
433 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100434 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100435
436 snprintf(buf, 31, "%u%c", val, *p);
437 return buf;
438}
439
Jens Axboe09629a92007-03-09 09:00:06 +0100440/* External engines are specified by "external:name.o") */
441static const char *get_engine_name(const char *str)
442{
443 char *p = strstr(str, ":");
444
445 if (!p)
446 return str;
447
448 p++;
449 strip_blank_front(&p);
450 strip_blank_end(p);
451 return p;
452}
453
Jens Axboee132cba2007-03-14 14:23:54 +0100454static int exists_and_not_file(const char *filename)
455{
456 struct stat sb;
457
458 if (lstat(filename, &sb) == -1)
459 return 0;
460
Bruce Cranecc314b2011-01-04 10:59:30 +0100461 /* \\.\ is the device namespace in Windows, where every file
462 * is a device node */
463 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100464 return 0;
465
466 return 1;
467}
468
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200469void td_fill_rand_seeds(struct thread_data *td)
470{
471 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
472 os_random_seed(td->rand_seeds[1], &td->verify_state);
473 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
474
475 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
476 os_random_seed(td->rand_seeds[3], &td->next_file_state);
477
478 os_random_seed(td->rand_seeds[5], &td->file_size_state);
Jens Axboe0d29de82010-09-01 13:54:15 +0200479 os_random_seed(td->rand_seeds[6], &td->trim_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200480
481 if (!td_random(td))
482 return;
483
484 if (td->o.rand_repeatable)
485 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
486
487 os_random_seed(td->rand_seeds[4], &td->random_state);
488}
489
Jens Axboef8977ee2006-11-06 14:03:03 +0100490/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100491 * Initialize the various random states we need (random io, block size ranges,
492 * read/write mix, etc).
493 */
494static int init_random_state(struct thread_data *td)
495{
Jens Axboe68727072007-03-15 20:49:25 +0100496 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100497
498 fd = open("/dev/urandom", O_RDONLY);
499 if (fd == -1) {
500 td_verror(td, errno, "open");
501 return 1;
502 }
503
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200504 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
505 (int) sizeof(td->rand_seeds)) {
Jens Axboe9c60ce62007-03-15 09:14:47 +0100506 td_verror(td, EIO, "read");
507 close(fd);
508 return 1;
509 }
510
511 close(fd);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200512 td_fill_rand_seeds(td);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100513 return 0;
514}
515
Jens Axboe9c60ce62007-03-15 09:14:47 +0100516/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200517 * Adds a job to the list of things todo. Sanitizes the various options
518 * to make sure we don't have conflicts, and initializes various
519 * members of td.
520 */
Jens Axboe75154842006-06-01 13:56:09 +0200521static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100522{
Jens Axboe413dd452007-02-23 09:26:09 +0100523 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
524 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100525 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100526 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100527 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100528 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100529
Jens Axboeebac4652005-12-08 15:25:21 +0100530 /*
531 * the def_thread is just for options, it's not a real job
532 */
533 if (td == &def_thread)
534 return 0;
535
Jens Axboecca73aa2007-04-04 11:09:19 +0200536 /*
537 * if we are just dumping the output command line, don't add the job
538 */
539 if (dump_cmdline) {
540 put_job(td);
541 return 0;
542 }
543
Jens Axboe58c55ba2010-03-09 12:20:08 +0100544 if (profile_td_init(td))
545 return 1;
546
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100547 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100548 td->io_ops = load_ioengine(td, engine);
549 if (!td->io_ops) {
550 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100551 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100552 }
Jens Axboedf641192006-10-12 07:55:41 +0200553
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100554 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100555 nr_thread++;
556 else
557 nr_process++;
558
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100559 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100560 td->io_ops->flags |= FIO_RAWIO;
561
Jens Axboee132cba2007-03-14 14:23:54 +0100562 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100563 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100564 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100565
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100566 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100567 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100568 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100569 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100570 sprintf(fname, "%s.%d.%d", jobname,
571 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100572 add_file(td, fname);
573 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100574 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100575 }
Jens Axboeebac4652005-12-08 15:25:21 +0100576
Jens Axboe4e991c22007-03-15 11:41:11 +0100577 if (fixup_options(td))
578 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100579
Jens Axboe07eb79d2007-04-12 13:02:38 +0200580 if (td->io_ops->flags & FIO_DISKLESSIO) {
581 struct fio_file *f;
582
583 for_each_file(td, f, i)
584 f->real_file_size = -1ULL;
585 }
586
Jens Axboecdd18ad2008-02-27 18:58:00 +0100587 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100588
Jens Axboe756867b2007-03-06 15:19:24 +0100589 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
590 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
Jens Axboe02af0982010-06-24 09:59:34 +0200591 td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX;
Jens Axboe756867b2007-03-06 15:19:24 +0100592 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Adam DeBellinscdd54112010-09-28 13:22:55 +0900593 td->ddir_seq_nr = td->o.ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100594
Jens Axboe755c3182009-08-25 22:14:29 +0200595 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100596 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100597 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100598 }
Jens Axboeebac4652005-12-08 15:25:21 +0100599
600 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100601 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100602
Jens Axboe9c60ce62007-03-15 09:14:47 +0100603 if (init_random_state(td))
604 goto err;
605
Jens Axboeebac4652005-12-08 15:25:21 +0100606 if (setup_rate(td))
607 goto err;
608
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100609 if (td->o.write_lat_log) {
Jens Axboe02af0982010-06-24 09:59:34 +0200610 setup_log(&td->ts.lat_log);
Jens Axboe756867b2007-03-06 15:19:24 +0100611 setup_log(&td->ts.slat_log);
612 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100613 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100614 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100615 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100616
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100617 if (!td->o.name)
618 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200619
Jens Axboec6ae0a52006-06-12 11:04:44 +0200620 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200621 if (!job_add_num) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100622 if (!strcmp(td->io_ops->name, "cpuio")) {
623 log_info("%s: ioengine=cpu, cpuload=%u,"
624 " cpucycle=%u\n", td->o.name,
625 td->o.cpuload,
626 td->o.cpucycle);
627 } else {
Jens Axboef8977ee2006-11-06 14:03:03 +0100628 char *c1, *c2, *c3, *c4;
629
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100630 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
631 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
632 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
633 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100634
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100635 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
636 " ioengine=%s, iodepth=%u\n",
637 td->o.name, td->groupid,
638 ddir_str[td->o.td_ddir],
639 c1, c2, c3, c4,
640 td->io_ops->name,
641 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100642
643 free(c1);
644 free(c2);
645 free(c3);
646 free(c4);
647 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200648 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100649 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200650 }
Jens Axboeebac4652005-12-08 15:25:21 +0100651
652 /*
653 * recurse add identical jobs, clear numjobs and stonewall options
654 * as they don't apply to sub-jobs
655 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100656 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100657 while (--numjobs) {
658 struct thread_data *td_new = get_new_job(0, td);
659
660 if (!td_new)
661 goto err;
662
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100663 td_new->o.numjobs = 1;
664 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200665 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100666
667 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100668 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100669 td_new->files_index = 0;
Jens Axboe4e6ea2f2009-03-04 20:17:22 +0100670 td_new->files_size = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100671 td_new->files = NULL;
672 }
673
Jens Axboe75154842006-06-01 13:56:09 +0200674 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100675
Jens Axboe75154842006-06-01 13:56:09 +0200676 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100677 goto err;
678 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100679
Jens Axboeebac4652005-12-08 15:25:21 +0100680 return 0;
681err:
682 put_job(td);
683 return -1;
684}
685
Jens Axboe79d16312010-03-04 12:43:20 +0100686/*
687 * Parse as if 'o' was a command line
688 */
689void add_job_opts(const char **o)
690{
691 struct thread_data *td, *td_parent;
692 int i, in_global = 1;
693 char jobname[32];
694
695 i = 0;
696 td_parent = td = NULL;
697 while (o[i]) {
698 if (!strncmp(o[i], "name", 4)) {
699 in_global = 0;
700 if (td)
701 add_job(td, jobname, 0);
702 td = NULL;
703 sprintf(jobname, "%s", o[i] + 5);
704 }
705 if (in_global && !td_parent)
706 td_parent = get_new_job(1, &def_thread);
707 else if (!in_global && !td) {
708 if (!td_parent)
709 td_parent = &def_thread;
710 td = get_new_job(0, td_parent);
711 }
712 if (in_global)
713 fio_options_parse(td_parent, (char **) &o[i], 1);
714 else
715 fio_options_parse(td, (char **) &o[i], 1);
716 i++;
717 }
718
719 if (td)
720 add_job(td, jobname, 0);
721}
722
Jens Axboe01f06b62008-02-18 20:53:47 +0100723static int skip_this_section(const char *name)
724{
725 if (!job_section)
726 return 0;
727 if (!strncmp(name, "global", 6))
728 return 0;
729
Jens Axboeb36beb62009-03-05 21:14:21 +0100730 return strcmp(job_section, name);
Jens Axboe01f06b62008-02-18 20:53:47 +0100731}
732
Jens Axboeebac4652005-12-08 15:25:21 +0100733static int is_empty_or_comment(char *line)
734{
735 unsigned int i;
736
737 for (i = 0; i < strlen(line); i++) {
738 if (line[i] == ';')
739 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100740 if (line[i] == '#')
741 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100742 if (!isspace(line[i]) && !iscntrl(line[i]))
743 return 0;
744 }
745
746 return 1;
747}
748
Jens Axboe313cb202006-12-21 09:50:00 +0100749/*
Jens Axboe07261982006-06-07 10:51:12 +0200750 * This is our [ini] type file parser.
751 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100752static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100753{
Jens Axboee1f36502006-10-27 10:54:08 +0200754 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100755 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100756 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100757 FILE *f;
758 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200759 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200760 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200761 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100762 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200763 char **opts;
764 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +0100765
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200766 if (!strcmp(file, "-"))
767 f = stdin;
768 else
769 f = fopen(file, "r");
770
Jens Axboeebac4652005-12-08 15:25:21 +0100771 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200772 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100773 return 1;
774 }
775
776 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200777
778 /*
779 * it's really 256 + small bit, 280 should suffice
780 */
781 name = malloc(280);
782 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100783
Jens Axboe3b8b7132008-06-10 19:46:23 +0200784 alloc_opts = 8;
785 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +0200786 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200787
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200788 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100789 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200790 /*
791 * if skip_fgets is set, we already have loaded a line we
792 * haven't handled.
793 */
794 if (!skip_fgets) {
795 p = fgets(string, 4095, f);
796 if (!p)
797 break;
798 }
gurudas paicdc7f192007-03-29 10:10:56 +0200799
Jens Axboeccf8f122007-09-27 10:48:55 +0200800 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200801 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800802 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200803
Jens Axboeebac4652005-12-08 15:25:21 +0100804 if (is_empty_or_comment(p))
805 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800806 if (sscanf(p, "[%255s]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100807 if (inside_skip)
808 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100809 log_err("fio: option <%s> outside of [] job section\n",
810 p);
Jens Axboe088b4202007-07-19 14:53:01 +0200811 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800812 }
Jens Axboeebac4652005-12-08 15:25:21 +0100813
Jens Axboe7a4b80a2010-05-22 20:43:11 +0200814 name[strlen(name) - 1] = '\0';
815
Jens Axboe01f06b62008-02-18 20:53:47 +0100816 if (skip_this_section(name)) {
817 inside_skip = 1;
818 continue;
819 } else
820 inside_skip = 0;
821
Jens Axboeebac4652005-12-08 15:25:21 +0100822 global = !strncmp(name, "global", 6);
823
Jens Axboecca73aa2007-04-04 11:09:19 +0200824 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200825 if (first_sect)
826 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200827 if (!global)
828 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200829 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200830 }
831
Jens Axboeebac4652005-12-08 15:25:21 +0100832 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200833 if (!td) {
834 ret = 1;
835 break;
836 }
Jens Axboeebac4652005-12-08 15:25:21 +0100837
Jens Axboe972cfd22006-06-09 11:08:56 +0200838 /*
839 * Seperate multiple job files by a stonewall
840 */
Jens Axboef9481912006-06-09 11:37:28 +0200841 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100842 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200843 stonewall = 0;
844 }
845
Jens Axboe3b8b7132008-06-10 19:46:23 +0200846 num_opts = 0;
847 memset(opts, 0, alloc_opts * sizeof(char *));
848
Jens Axboeebac4652005-12-08 15:25:21 +0100849 while ((p = fgets(string, 4096, f)) != NULL) {
850 if (is_empty_or_comment(p))
851 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200852
Jens Axboeb6754f92006-05-30 14:29:32 +0200853 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100854
Jens Axboeccf8f122007-09-27 10:48:55 +0200855 /*
856 * new section, break out and make sure we don't
857 * fgets() a new line at the top.
858 */
859 if (p[0] == '[') {
860 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100861 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200862 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100863
Jens Axboe4ae3f762006-05-30 13:35:14 +0200864 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200865
Jens Axboe3b8b7132008-06-10 19:46:23 +0200866 if (num_opts == alloc_opts) {
867 alloc_opts <<= 1;
868 opts = realloc(opts,
869 alloc_opts * sizeof(char *));
870 }
871
872 opts[num_opts] = strdup(p);
873 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +0100874 }
Jens Axboeebac4652005-12-08 15:25:21 +0100875
Jens Axboe3b8b7132008-06-10 19:46:23 +0200876 ret = fio_options_parse(td, opts, num_opts);
877 if (!ret) {
878 if (dump_cmdline)
879 for (i = 0; i < num_opts; i++)
880 log_info("--%s ", opts[i]);
881
Jens Axboe45410ac2006-06-07 11:10:39 +0200882 ret = add_job(td, name, 0);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200883 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100884 log_err("fio: job %s dropped\n", name);
885 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200886 }
Jens Axboe3b8b7132008-06-10 19:46:23 +0200887
888 for (i = 0; i < num_opts; i++)
889 free(opts[i]);
890 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100891 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100892
Jens Axboecca73aa2007-04-04 11:09:19 +0200893 if (dump_cmdline)
894 log_info("\n");
895
Jens Axboe3b8b7132008-06-10 19:46:23 +0200896 for (i = 0; i < num_opts; i++)
897 free(opts[i]);
898
Jens Axboeebac4652005-12-08 15:25:21 +0100899 free(string);
900 free(name);
Jens Axboe25775942008-06-10 20:26:06 +0200901 free(opts);
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200902 if (f != stdin)
903 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200904 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100905}
906
907static int fill_def_thread(void)
908{
909 memset(&def_thread, 0, sizeof(def_thread));
910
Jens Axboe375b2692007-05-22 09:13:02 +0200911 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100912
913 /*
Jens Axboeee738492007-01-10 11:23:16 +0100914 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100915 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100916 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100917
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100918 def_thread.o.timeout = def_timeout;
Jens Axboeebac4652005-12-08 15:25:21 +0100919 return 0;
920}
921
Jens Axboeebac4652005-12-08 15:25:21 +0100922static void free_shm(void)
923{
924 struct shmid_ds sbuf;
925
926 if (threads) {
Jens Axboe5e1d3062008-05-23 11:55:53 +0200927 void *tp = threads;
928
Jens Axboeebac4652005-12-08 15:25:21 +0100929 threads = NULL;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200930 file_hash_exit();
931 fio_debug_jobp = NULL;
932 shmdt(tp);
Jens Axboeebac4652005-12-08 15:25:21 +0100933 shmctl(shm_id, IPC_RMID, &sbuf);
934 }
Jens Axboe2e5cdb12008-03-01 18:52:49 +0100935
936 scleanup();
Jens Axboeebac4652005-12-08 15:25:21 +0100937}
938
Jens Axboe906c8d72006-06-13 09:37:56 +0200939/*
940 * The thread area is shared between the main process and the job
941 * threads/processes. So setup a shared memory segment that will hold
Jens Axboe380065a2008-03-01 18:56:24 +0100942 * all the job info. We use the end of the region for keeping track of
943 * open files across jobs, for file sharing.
Jens Axboe906c8d72006-06-13 09:37:56 +0200944 */
Jens Axboeebac4652005-12-08 15:25:21 +0100945static int setup_thread_area(void)
946{
Jens Axboe380065a2008-03-01 18:56:24 +0100947 void *hash;
948
Jens Axboeebac4652005-12-08 15:25:21 +0100949 /*
950 * 1024 is too much on some machines, scale max_jobs if
951 * we get a failure that looks like too large a shm segment
952 */
953 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200954 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100955
Jens Axboe380065a2008-03-01 18:56:24 +0100956 size += file_hash_size;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200957 size += sizeof(unsigned int);
Jens Axboe380065a2008-03-01 18:56:24 +0100958
Jens Axboe906c8d72006-06-13 09:37:56 +0200959 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100960 if (shm_id != -1)
961 break;
962 if (errno != EINVAL) {
963 perror("shmget");
964 break;
965 }
966
967 max_jobs >>= 1;
968 } while (max_jobs);
969
970 if (shm_id == -1)
971 return 1;
972
973 threads = shmat(shm_id, NULL, 0);
974 if (threads == (void *) -1) {
975 perror("shmat");
976 return 1;
977 }
978
Jens Axboe1f809d12007-10-25 18:34:02 +0200979 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboe380065a2008-03-01 18:56:24 +0100980 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
Jens Axboe5e1d3062008-05-23 11:55:53 +0200981 fio_debug_jobp = (void *) hash + file_hash_size;
982 *fio_debug_jobp = -1;
Jens Axboe380065a2008-03-01 18:56:24 +0100983 file_hash_init(hash);
Jens Axboeebac4652005-12-08 15:25:21 +0100984 atexit(free_shm);
985 return 0;
986}
987
Jens Axboe45378b32007-12-19 13:54:38 +0100988static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +0200989{
Jens Axboe45378b32007-12-19 13:54:38 +0100990 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboeee56ad52008-02-01 10:30:20 +0100991 printf("\t--debug=options\tEnable debug logging\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +0100992 printf("\t--output\tWrite output to file\n");
993 printf("\t--timeout\tRuntime in seconds\n");
994 printf("\t--latency-log\tGenerate per-job latency logs\n");
995 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
996 printf("\t--minimal\tMinimal (terse) output\n");
997 printf("\t--version\tPrint version info and exit\n");
998 printf("\t--help\t\tPrint this page\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100999 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
1000 " them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +02001001 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +02001002 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
1003 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001004 printf("\t--readonly\tTurn on safety read-only checks, preventing"
1005 " writes\n");
Jens Axboe01f06b62008-02-18 20:53:47 +01001006 printf("\t--section=name\tOnly run specified section in job file\n");
Jens Axboe2b386d22008-03-26 10:32:57 +01001007 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
1008 " (def 1024)\n");
Jens Axboea9523c62011-01-17 16:49:54 -07001009 printf("\t--warnings-fatal Fio parser warnings are fatal\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001010 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1011 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001012}
1013
Jens Axboe79e48f72008-02-01 20:37:09 +01001014#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001015struct debug_level debug_levels[] = {
Jens Axboebd6f78b2008-02-01 20:27:52 +01001016 { .name = "process", .shift = FD_PROCESS, },
1017 { .name = "file", .shift = FD_FILE, },
1018 { .name = "io", .shift = FD_IO, },
1019 { .name = "mem", .shift = FD_MEM, },
1020 { .name = "blktrace", .shift = FD_BLKTRACE },
1021 { .name = "verify", .shift = FD_VERIFY },
Jens Axboe84422ac2008-02-19 20:10:26 +01001022 { .name = "random", .shift = FD_RANDOM },
Jens Axboea3d741f2008-02-27 18:32:33 +01001023 { .name = "parse", .shift = FD_PARSE },
Jens Axboecd991b92008-03-07 13:19:35 +01001024 { .name = "diskutil", .shift = FD_DISKUTIL },
Jens Axboe5e1d3062008-05-23 11:55:53 +02001025 { .name = "job", .shift = FD_JOB },
Jens Axboe29adda32009-01-05 19:06:39 +01001026 { .name = "mutex", .shift = FD_MUTEX },
Jens Axboe79d16312010-03-04 12:43:20 +01001027 { .name = "profile", .shift = FD_PROFILE },
Jens Axboec223da82010-03-24 13:23:53 +01001028 { .name = "time", .shift = FD_TIME },
Jens Axboe02444ad2008-10-07 11:33:00 +02001029 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001030};
1031
Jens Axboec09823a2008-02-19 20:16:57 +01001032static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001033{
1034 struct debug_level *dl;
1035 char *p = (char *) string;
1036 char *opt;
1037 int i;
1038
1039 if (!strcmp(string, "?") || !strcmp(string, "help")) {
1040 int i;
1041
1042 log_info("fio: dumping debug options:");
1043 for (i = 0; debug_levels[i].name; i++) {
1044 dl = &debug_levels[i];
1045 log_info("%s,", dl->name);
1046 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001047 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001048 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001049 }
1050
1051 while ((opt = strsep(&p, ",")) != NULL) {
1052 int found = 0;
1053
Jens Axboe5e1d3062008-05-23 11:55:53 +02001054 if (!strncmp(opt, "all", 3)) {
1055 log_info("fio: set all debug options\n");
1056 fio_debug = ~0UL;
1057 continue;
1058 }
1059
Jens Axboeee56ad52008-02-01 10:30:20 +01001060 for (i = 0; debug_levels[i].name; i++) {
1061 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001062 found = !strncmp(opt, dl->name, strlen(dl->name));
1063 if (!found)
1064 continue;
1065
1066 if (dl->shift == FD_JOB) {
1067 opt = strchr(opt, ':');
1068 if (!opt) {
1069 log_err("fio: missing job number\n");
1070 break;
1071 }
1072 opt++;
1073 fio_debug_jobno = atoi(opt);
1074 log_info("fio: set debug jobno %d\n",
1075 fio_debug_jobno);
1076 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001077 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001078 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001079 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001080 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001081 }
1082
1083 if (!found)
1084 log_err("fio: debug mask %s not found\n", opt);
1085 }
Jens Axboec09823a2008-02-19 20:16:57 +01001086 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001087}
Jens Axboe79e48f72008-02-01 20:37:09 +01001088#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001089static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001090{
1091 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001092 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001093}
1094#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001095
Jens Axboe214e1ec2007-03-15 10:48:13 +01001096static int parse_cmd_line(int argc, char *argv[])
1097{
1098 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001099 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001100
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001101 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001102 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001103 case 'a':
1104 smalloc_pool_size = atoi(optarg);
1105 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001106 case 't':
1107 def_timeout = atoi(optarg);
1108 break;
1109 case 'l':
1110 write_lat_log = 1;
1111 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001112 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001113 write_bw_log = 1;
1114 break;
1115 case 'o':
1116 f_out = fopen(optarg, "w+");
1117 if (!f_out) {
1118 perror("fopen output");
1119 exit(1);
1120 }
1121 f_err = f_out;
1122 break;
1123 case 'm':
1124 terse_output = 1;
1125 break;
1126 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +01001127 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001128 exit(0);
1129 case 'c':
1130 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +02001131 case 's':
1132 dump_cmdline = 1;
1133 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001134 case 'r':
1135 read_only = 1;
1136 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001137 case 'v':
Jens Axboebce964a2011-01-12 09:07:15 +01001138 /* already being printed, just quit */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001139 exit(0);
Aaron Carrolle592a062007-09-14 09:49:41 +02001140 case 'e':
1141 if (!strcmp("always", optarg))
1142 eta_print = FIO_ETA_ALWAYS;
1143 else if (!strcmp("never", optarg))
1144 eta_print = FIO_ETA_NEVER;
1145 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001146 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001147 if (set_debug(optarg))
1148 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001149 break;
Jens Axboe01f06b62008-02-18 20:53:47 +01001150 case 'x':
1151 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001152 log_err("fio: can't use global as only "
1153 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001154 do_exit++;
1155 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001156 break;
1157 }
1158 if (job_section)
1159 free(job_section);
1160 job_section = strdup(optarg);
1161 break;
Jens Axboe9ac8a792009-11-13 21:23:07 +01001162 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001163 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001164 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001165 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001166 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001167 char *val = optarg;
1168
1169 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001170 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001171 if (ret) {
1172 put_job(td);
1173 return 0;
1174 }
1175 td = NULL;
1176 }
1177 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001178 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001179 int global = 0;
1180
Jens Axboe01f06b62008-02-18 20:53:47 +01001181 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001182 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001183
Jens Axboe01f06b62008-02-18 20:53:47 +01001184 if (is_section && skip_this_section(val))
1185 continue;
1186
Jens Axboe214e1ec2007-03-15 10:48:13 +01001187 td = get_new_job(global, &def_thread);
1188 if (!td)
1189 return 0;
1190 }
1191
1192 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001193 break;
1194 }
Jens Axboea9523c62011-01-17 16:49:54 -07001195 case 'w':
1196 warnings_fatal = 1;
1197 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001198 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001199 do_exit++;
1200 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001201 break;
1202 }
1203 }
1204
Jens Axboec09823a2008-02-19 20:16:57 +01001205 if (do_exit)
1206 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001207
Jens Axboe214e1ec2007-03-15 10:48:13 +01001208 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001209 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001210 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe7d6a8902008-02-18 20:59:18 +01001211 if (ret)
1212 put_job(td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001213 }
1214
1215 while (optind < argc) {
1216 ini_idx++;
1217 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1218 ini_file[ini_idx - 1] = strdup(argv[optind]);
1219 optind++;
1220 }
1221
1222 return ini_idx;
1223}
1224
Jens Axboeebac4652005-12-08 15:25:21 +01001225int parse_options(int argc, char *argv[])
1226{
Jens Axboe972cfd22006-06-09 11:08:56 +02001227 int job_files, i;
1228
Jens Axboeb4692822006-10-27 13:43:22 +02001229 f_out = stdout;
1230 f_err = stderr;
1231
Jens Axboe2d7760d2010-12-28 09:23:06 +01001232 log_info("%s\n", fio_version_string);
1233
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001234 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001235
Jens Axboeebac4652005-12-08 15:25:21 +01001236 if (setup_thread_area())
1237 return 1;
1238 if (fill_def_thread())
1239 return 1;
1240
Jens Axboe972cfd22006-06-09 11:08:56 +02001241 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001242
Jens Axboe972cfd22006-06-09 11:08:56 +02001243 for (i = 0; i < job_files; i++) {
1244 if (fill_def_thread())
1245 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001246 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001247 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001248 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001249 }
Jens Axboeebac4652005-12-08 15:25:21 +01001250
Jens Axboe88c6ed82006-06-09 11:28:10 +02001251 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001252 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001253
1254 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001255 if (dump_cmdline)
1256 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01001257 if (exec_profile)
1258 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001259
Bruce Cran03e20d62011-01-02 20:14:54 +01001260 log_err("No jobs(s) defined\n\n");
Jens Axboe45378b32007-12-19 13:54:38 +01001261 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +02001262 return 1;
1263 }
1264
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001265 if (def_thread.o.gtod_offload) {
1266 fio_gtod_init();
1267 fio_gtod_offload = 1;
1268 fio_gtod_cpu = def_thread.o.gtod_cpu;
1269 }
1270
Jens Axboeebac4652005-12-08 15:25:21 +01001271 return 0;
1272}