blob: b3ce07c9fce15dbdc67746b5455343ed15723365 [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 Axboe55093ac2009-05-20 11:53:04 +020023static char fio_version_string[] = "fio 1.26.4";
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 Axboeb4692822006-10-27 13:43:22 +0200130 .name = NULL,
131 },
132};
133
Joel Becker9728ce32007-03-01 08:24:39 +0100134FILE *get_f_out()
135{
136 return f_out;
137}
138
139FILE *get_f_err()
140{
141 return f_err;
142}
143
Jens Axboe906c8d72006-06-13 09:37:56 +0200144/*
145 * Return a free job structure.
146 */
Jens Axboeebac4652005-12-08 15:25:21 +0100147static struct thread_data *get_new_job(int global, struct thread_data *parent)
148{
149 struct thread_data *td;
150
151 if (global)
152 return &def_thread;
153 if (thread_number >= max_jobs)
154 return NULL;
155
156 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200157 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100158
Jens Axboecade3ef2007-03-23 15:29:45 +0100159 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100160 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100161
Jens Axboeebac4652005-12-08 15:25:21 +0100162 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100163 return td;
164}
165
166static void put_job(struct thread_data *td)
167{
Jens Axboe549577a2006-10-30 12:23:41 +0100168 if (td == &def_thread)
169 return;
170
Jens Axboe16edf252007-02-10 14:59:22 +0100171 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100172 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100173
Jens Axboeebac4652005-12-08 15:25:21 +0100174 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
175 thread_number--;
176}
177
Jens Axboe127f6862007-03-15 12:09:57 +0100178static int setup_rate(struct thread_data *td)
179{
180 unsigned long nr_reads_per_msec;
181 unsigned long long rate;
182 unsigned int bs;
183
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100184 if (!td->o.rate && !td->o.rate_iops)
Jens Axboe127f6862007-03-15 12:09:57 +0100185 return 0;
186
187 if (td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100188 bs = td->o.rw_min_bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100189 else if (td_read(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100190 bs = td->o.min_bs[DDIR_READ];
Jens Axboe127f6862007-03-15 12:09:57 +0100191 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100192 bs = td->o.min_bs[DDIR_WRITE];
Jens Axboe127f6862007-03-15 12:09:57 +0100193
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100194 if (td->o.rate) {
195 rate = td->o.rate;
Jens Axboe127f6862007-03-15 12:09:57 +0100196 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
197 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100198 nr_reads_per_msec = td->o.rate_iops * 1000UL;
Jens Axboe127f6862007-03-15 12:09:57 +0100199
200 if (!nr_reads_per_msec) {
201 log_err("rate lower than supported\n");
202 return -1;
203 }
204
205 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
206 td->rate_pending_usleep = 0;
207 return 0;
208}
209
Jens Axboe83472392009-02-19 21:32:12 +0100210static int fixed_block_size(struct thread_options *o)
211{
212 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
213 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
214 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
215}
216
Jens Axboedad915e2006-10-27 11:10:18 +0200217/*
218 * Lazy way of fixing up options that depend on each other. We could also
219 * define option callback handlers, but this is easier.
220 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100221static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200222{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100223 struct thread_options *o = &td->o;
Jens Axboedad915e2006-10-27 11:10:18 +0200224
Jens Axboef356d012009-01-05 09:56:29 +0100225#ifndef FIO_HAVE_PSHARED_MUTEX
226 if (!td->o.use_thread) {
227 log_info("fio: this platform does not support process shared"
228 " mutexes, forcing use of threads. Use the 'thread'"
229 " option to get rid of this warning.\n");
230 td->o.use_thread = 1;
231 }
232#endif
233
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100234#ifndef FIO_HAVE_CPU_AFFINITY
235 if (td->o.gtod_cpu) {
236 log_err("fio: platform must support CPU affinity for"
237 "gettimeofday() offloading\n");
238 return 1;
239 }
240#endif
241
Jens Axboe724e4432007-09-11 20:02:05 +0200242 if (read_only && td_write(td)) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100243 log_err("fio: job <%s> has write bit set, but fio is in"
244 " read-only mode\n", td->o.name);
Jens Axboe724e4432007-09-11 20:02:05 +0200245 return 1;
246 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100247
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100248 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200249 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100250 free(o->write_iolog_file);
251 o->write_iolog_file = NULL;
Jens Axboe076efc72006-10-27 11:24:25 +0200252 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100253
Jens Axboe16b462a2006-10-30 12:35:18 +0100254 /*
255 * only really works for sequential io for now, and with 1 file
256 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100257 if (o->zone_size && td_random(td) && o->open_files == 1)
258 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100259
260 /*
261 * Reads can do overwrites, we always need to pre-create the file
262 */
263 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100264 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100265
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100266 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100267 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100268 if (!o->max_bs[DDIR_READ])
269 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
270 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100271 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100272 if (!o->max_bs[DDIR_WRITE])
273 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100274
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100275 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100276
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100277 /*
278 * For random IO, allow blockalign offset other than min_bs.
279 */
280 if (!o->ba[DDIR_READ] || !td_random(td))
281 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
282 if (!o->ba[DDIR_WRITE] || !td_random(td))
283 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
284
285 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
286 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
287 !td->o.norandommap) {
288 log_err("fio: Any use of blockalign= turns off randommap\n");
289 td->o.norandommap = 1;
290 }
291
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100292 if (!o->file_size_high)
293 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100294
Jens Axboe83472392009-02-19 21:32:12 +0100295 if (o->norandommap && o->verify != VERIFY_NONE
296 && !fixed_block_size(o)) {
297 log_err("fio: norandommap given for variable block sizes, "
298 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100299 o->verify = VERIFY_NONE;
Jens Axboebb8895e2006-10-30 15:14:48 +0100300 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100301 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100302 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100303
304 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100305 * thinktime_spin must be less than thinktime
306 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100307 if (o->thinktime_spin > o->thinktime)
308 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100309
310 /*
311 * The low water mark cannot be bigger than the iodepth
312 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100313 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100314 /*
315 * syslet work around - if the workload is sequential,
316 * we want to let the queue drain all the way down to
317 * avoid seeking between async threads
318 */
319 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100320 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100321 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100322 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100323 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100324
325 /*
326 * If batch number isn't set, default to the same as iodepth
327 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100328 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
329 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100330
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100331 if (o->nr_files > td->files_index)
332 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100333
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100334 if (o->open_files > o->nr_files || !o->open_files)
335 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100336
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100337 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100338 log_err("fio: rate and rate_iops are mutually exclusive\n");
339 return 1;
340 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100341 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100342 log_err("fio: minimum rate exceeds rate\n");
343 return 1;
344 }
345
Jens Axboecf4464c2007-04-17 20:14:42 +0200346 if (!o->timeout && o->time_based) {
347 log_err("fio: time_based requires a runtime/timeout setting\n");
348 o->time_based = 0;
349 }
350
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100351 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200352 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100353
Jens Axboe79713142008-02-06 14:40:14 +0100354 if (td_rw(td) && td->o.verify != VERIFY_NONE)
355 log_info("fio: mixed read/write workload with verify. May not "
356 "work as expected, unless you pre-populated the file\n");
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100357
Jens Axboe41ccd842008-05-22 09:17:33 +0200358 if (td->o.verify != VERIFY_NONE)
359 td->o.refill_buffers = 1;
360
Jens Axboe34f1c042009-06-02 14:19:25 +0200361 if (td->o.pre_read)
362 td->o.invalidate_cache = 0;
363
Jens Axboe4e991c22007-03-15 11:41:11 +0100364 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200365}
366
Jens Axboe906c8d72006-06-13 09:37:56 +0200367/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100368 * This function leaks the buffer
369 */
370static char *to_kmg(unsigned int val)
371{
372 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100373 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100374 char *p = post;
375
Jens Axboe245142f2006-11-08 12:49:21 +0100376 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100377 if (val & 1023)
378 break;
379
380 val >>= 10;
381 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100382 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100383
384 snprintf(buf, 31, "%u%c", val, *p);
385 return buf;
386}
387
Jens Axboe09629a92007-03-09 09:00:06 +0100388/* External engines are specified by "external:name.o") */
389static const char *get_engine_name(const char *str)
390{
391 char *p = strstr(str, ":");
392
393 if (!p)
394 return str;
395
396 p++;
397 strip_blank_front(&p);
398 strip_blank_end(p);
399 return p;
400}
401
Jens Axboee132cba2007-03-14 14:23:54 +0100402static int exists_and_not_file(const char *filename)
403{
404 struct stat sb;
405
406 if (lstat(filename, &sb) == -1)
407 return 0;
408
409 if (S_ISREG(sb.st_mode))
410 return 0;
411
412 return 1;
413}
414
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200415void td_fill_rand_seeds(struct thread_data *td)
416{
417 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
418 os_random_seed(td->rand_seeds[1], &td->verify_state);
419 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
420
421 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
422 os_random_seed(td->rand_seeds[3], &td->next_file_state);
423
424 os_random_seed(td->rand_seeds[5], &td->file_size_state);
425
426 if (!td_random(td))
427 return;
428
429 if (td->o.rand_repeatable)
430 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
431
432 os_random_seed(td->rand_seeds[4], &td->random_state);
433}
434
Jens Axboef8977ee2006-11-06 14:03:03 +0100435/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100436 * Initialize the various random states we need (random io, block size ranges,
437 * read/write mix, etc).
438 */
439static int init_random_state(struct thread_data *td)
440{
Jens Axboe68727072007-03-15 20:49:25 +0100441 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100442
443 fd = open("/dev/urandom", O_RDONLY);
444 if (fd == -1) {
445 td_verror(td, errno, "open");
446 return 1;
447 }
448
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200449 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
450 (int) sizeof(td->rand_seeds)) {
Jens Axboe9c60ce62007-03-15 09:14:47 +0100451 td_verror(td, EIO, "read");
452 close(fd);
453 return 1;
454 }
455
456 close(fd);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200457 td_fill_rand_seeds(td);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100458 return 0;
459}
460
Jens Axboe9c60ce62007-03-15 09:14:47 +0100461/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200462 * Adds a job to the list of things todo. Sanitizes the various options
463 * to make sure we don't have conflicts, and initializes various
464 * members of td.
465 */
Jens Axboe75154842006-06-01 13:56:09 +0200466static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100467{
Jens Axboe413dd452007-02-23 09:26:09 +0100468 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
469 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100470 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100471 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100472 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100473 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100474
Jens Axboeebac4652005-12-08 15:25:21 +0100475 /*
476 * the def_thread is just for options, it's not a real job
477 */
478 if (td == &def_thread)
479 return 0;
480
Jens Axboecca73aa2007-04-04 11:09:19 +0200481 /*
482 * if we are just dumping the output command line, don't add the job
483 */
484 if (dump_cmdline) {
485 put_job(td);
486 return 0;
487 }
488
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100489 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100490 td->io_ops = load_ioengine(td, engine);
491 if (!td->io_ops) {
492 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100493 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100494 }
Jens Axboedf641192006-10-12 07:55:41 +0200495
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100496 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100497 nr_thread++;
498 else
499 nr_process++;
500
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100501 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100502 td->io_ops->flags |= FIO_RAWIO;
503
Jens Axboee132cba2007-03-14 14:23:54 +0100504 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100505 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100506 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100507
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100508 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100509 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100510 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100511 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100512 sprintf(fname, "%s.%d.%d", jobname,
513 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100514 add_file(td, fname);
515 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100516 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100517 }
Jens Axboeebac4652005-12-08 15:25:21 +0100518
Jens Axboe4e991c22007-03-15 11:41:11 +0100519 if (fixup_options(td))
520 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100521
Jens Axboe07eb79d2007-04-12 13:02:38 +0200522 if (td->io_ops->flags & FIO_DISKLESSIO) {
523 struct fio_file *f;
524
525 for_each_file(td, f, i)
526 f->real_file_size = -1ULL;
527 }
528
Jens Axboecdd18ad2008-02-27 18:58:00 +0100529 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100530
Jens Axboe756867b2007-03-06 15:19:24 +0100531 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
532 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
533 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboe1e3d53a2007-03-22 19:01:48 +0100534 td->ddir_nr = td->o.ddir_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100535
Jens Axboeb3d62a72007-03-20 14:23:26 +0100536 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
537 && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100538 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100539 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100540 }
Jens Axboeebac4652005-12-08 15:25:21 +0100541
542 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100543 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100544
Jens Axboe9c60ce62007-03-15 09:14:47 +0100545 if (init_random_state(td))
546 goto err;
547
Jens Axboeebac4652005-12-08 15:25:21 +0100548 if (setup_rate(td))
549 goto err;
550
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100551 if (td->o.write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100552 setup_log(&td->ts.slat_log);
553 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100554 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100555 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100556 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100557
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100558 if (!td->o.name)
559 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200560
Jens Axboec6ae0a52006-06-12 11:04:44 +0200561 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200562 if (!job_add_num) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100563 if (!strcmp(td->io_ops->name, "cpuio")) {
564 log_info("%s: ioengine=cpu, cpuload=%u,"
565 " cpucycle=%u\n", td->o.name,
566 td->o.cpuload,
567 td->o.cpucycle);
568 } else {
Jens Axboef8977ee2006-11-06 14:03:03 +0100569 char *c1, *c2, *c3, *c4;
570
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100571 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
572 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
573 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
574 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100575
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100576 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
577 " ioengine=%s, iodepth=%u\n",
578 td->o.name, td->groupid,
579 ddir_str[td->o.td_ddir],
580 c1, c2, c3, c4,
581 td->io_ops->name,
582 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100583
584 free(c1);
585 free(c2);
586 free(c3);
587 free(c4);
588 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200589 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100590 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200591 }
Jens Axboeebac4652005-12-08 15:25:21 +0100592
593 /*
594 * recurse add identical jobs, clear numjobs and stonewall options
595 * as they don't apply to sub-jobs
596 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100597 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100598 while (--numjobs) {
599 struct thread_data *td_new = get_new_job(0, td);
600
601 if (!td_new)
602 goto err;
603
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100604 td_new->o.numjobs = 1;
605 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200606 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100607
608 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100609 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100610 td_new->files_index = 0;
Jens Axboe4e6ea2f2009-03-04 20:17:22 +0100611 td_new->files_size = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100612 td_new->files = NULL;
613 }
614
Jens Axboe75154842006-06-01 13:56:09 +0200615 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100616
Jens Axboe75154842006-06-01 13:56:09 +0200617 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100618 goto err;
619 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100620
Jens Axboeebac4652005-12-08 15:25:21 +0100621 return 0;
622err:
623 put_job(td);
624 return -1;
625}
626
Jens Axboe01f06b62008-02-18 20:53:47 +0100627static int skip_this_section(const char *name)
628{
629 if (!job_section)
630 return 0;
631 if (!strncmp(name, "global", 6))
632 return 0;
633
Jens Axboeb36beb62009-03-05 21:14:21 +0100634 return strcmp(job_section, name);
Jens Axboe01f06b62008-02-18 20:53:47 +0100635}
636
Jens Axboeebac4652005-12-08 15:25:21 +0100637static int is_empty_or_comment(char *line)
638{
639 unsigned int i;
640
641 for (i = 0; i < strlen(line); i++) {
642 if (line[i] == ';')
643 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100644 if (line[i] == '#')
645 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100646 if (!isspace(line[i]) && !iscntrl(line[i]))
647 return 0;
648 }
649
650 return 1;
651}
652
Jens Axboe313cb202006-12-21 09:50:00 +0100653/*
Jens Axboe07261982006-06-07 10:51:12 +0200654 * This is our [ini] type file parser.
655 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100656static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100657{
Jens Axboee1f36502006-10-27 10:54:08 +0200658 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100659 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100660 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100661 FILE *f;
662 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200663 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200664 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200665 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100666 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200667 char **opts;
668 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +0100669
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200670 if (!strcmp(file, "-"))
671 f = stdin;
672 else
673 f = fopen(file, "r");
674
Jens Axboeebac4652005-12-08 15:25:21 +0100675 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200676 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100677 return 1;
678 }
679
680 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200681
682 /*
683 * it's really 256 + small bit, 280 should suffice
684 */
685 name = malloc(280);
686 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100687
Jens Axboe3b8b7132008-06-10 19:46:23 +0200688 alloc_opts = 8;
689 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +0200690 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200691
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200692 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100693 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200694 /*
695 * if skip_fgets is set, we already have loaded a line we
696 * haven't handled.
697 */
698 if (!skip_fgets) {
699 p = fgets(string, 4095, f);
700 if (!p)
701 break;
702 }
gurudas paicdc7f192007-03-29 10:10:56 +0200703
Jens Axboeccf8f122007-09-27 10:48:55 +0200704 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200705 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800706 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200707
Jens Axboeebac4652005-12-08 15:25:21 +0100708 if (is_empty_or_comment(p))
709 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800710 if (sscanf(p, "[%255s]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100711 if (inside_skip)
712 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100713 log_err("fio: option <%s> outside of [] job section\n",
714 p);
Jens Axboe088b4202007-07-19 14:53:01 +0200715 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800716 }
Jens Axboeebac4652005-12-08 15:25:21 +0100717
Jens Axboe01f06b62008-02-18 20:53:47 +0100718 if (skip_this_section(name)) {
719 inside_skip = 1;
720 continue;
721 } else
722 inside_skip = 0;
723
Jens Axboeebac4652005-12-08 15:25:21 +0100724 global = !strncmp(name, "global", 6);
725
726 name[strlen(name) - 1] = '\0';
727
Jens Axboecca73aa2007-04-04 11:09:19 +0200728 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200729 if (first_sect)
730 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200731 if (!global)
732 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200733 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200734 }
735
Jens Axboeebac4652005-12-08 15:25:21 +0100736 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200737 if (!td) {
738 ret = 1;
739 break;
740 }
Jens Axboeebac4652005-12-08 15:25:21 +0100741
Jens Axboe972cfd22006-06-09 11:08:56 +0200742 /*
743 * Seperate multiple job files by a stonewall
744 */
Jens Axboef9481912006-06-09 11:37:28 +0200745 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100746 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200747 stonewall = 0;
748 }
749
Jens Axboe3b8b7132008-06-10 19:46:23 +0200750 num_opts = 0;
751 memset(opts, 0, alloc_opts * sizeof(char *));
752
Jens Axboeebac4652005-12-08 15:25:21 +0100753 while ((p = fgets(string, 4096, f)) != NULL) {
754 if (is_empty_or_comment(p))
755 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200756
Jens Axboeb6754f92006-05-30 14:29:32 +0200757 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100758
Jens Axboeccf8f122007-09-27 10:48:55 +0200759 /*
760 * new section, break out and make sure we don't
761 * fgets() a new line at the top.
762 */
763 if (p[0] == '[') {
764 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100765 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200766 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100767
Jens Axboe4ae3f762006-05-30 13:35:14 +0200768 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200769
Jens Axboe3b8b7132008-06-10 19:46:23 +0200770 if (num_opts == alloc_opts) {
771 alloc_opts <<= 1;
772 opts = realloc(opts,
773 alloc_opts * sizeof(char *));
774 }
775
776 opts[num_opts] = strdup(p);
777 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +0100778 }
Jens Axboeebac4652005-12-08 15:25:21 +0100779
Jens Axboe3b8b7132008-06-10 19:46:23 +0200780 ret = fio_options_parse(td, opts, num_opts);
781 if (!ret) {
782 if (dump_cmdline)
783 for (i = 0; i < num_opts; i++)
784 log_info("--%s ", opts[i]);
785
Jens Axboe45410ac2006-06-07 11:10:39 +0200786 ret = add_job(td, name, 0);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200787 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100788 log_err("fio: job %s dropped\n", name);
789 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200790 }
Jens Axboe3b8b7132008-06-10 19:46:23 +0200791
792 for (i = 0; i < num_opts; i++)
793 free(opts[i]);
794 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100795 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100796
Jens Axboecca73aa2007-04-04 11:09:19 +0200797 if (dump_cmdline)
798 log_info("\n");
799
Jens Axboe3b8b7132008-06-10 19:46:23 +0200800 for (i = 0; i < num_opts; i++)
801 free(opts[i]);
802
Jens Axboeebac4652005-12-08 15:25:21 +0100803 free(string);
804 free(name);
Jens Axboe25775942008-06-10 20:26:06 +0200805 free(opts);
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200806 if (f != stdin)
807 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200808 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100809}
810
811static int fill_def_thread(void)
812{
813 memset(&def_thread, 0, sizeof(def_thread));
814
Jens Axboe375b2692007-05-22 09:13:02 +0200815 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100816
817 /*
Jens Axboeee738492007-01-10 11:23:16 +0100818 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100819 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100820 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100821
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100822 def_thread.o.timeout = def_timeout;
823 def_thread.o.write_bw_log = write_bw_log;
824 def_thread.o.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100825
Jens Axboeebac4652005-12-08 15:25:21 +0100826 return 0;
827}
828
Jens Axboeebac4652005-12-08 15:25:21 +0100829static void free_shm(void)
830{
831 struct shmid_ds sbuf;
832
833 if (threads) {
Jens Axboe5e1d3062008-05-23 11:55:53 +0200834 void *tp = threads;
835
Jens Axboeebac4652005-12-08 15:25:21 +0100836 threads = NULL;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200837 file_hash_exit();
838 fio_debug_jobp = NULL;
839 shmdt(tp);
Jens Axboeebac4652005-12-08 15:25:21 +0100840 shmctl(shm_id, IPC_RMID, &sbuf);
841 }
Jens Axboe2e5cdb12008-03-01 18:52:49 +0100842
843 scleanup();
Jens Axboeebac4652005-12-08 15:25:21 +0100844}
845
Jens Axboe906c8d72006-06-13 09:37:56 +0200846/*
847 * The thread area is shared between the main process and the job
848 * threads/processes. So setup a shared memory segment that will hold
Jens Axboe380065a2008-03-01 18:56:24 +0100849 * all the job info. We use the end of the region for keeping track of
850 * open files across jobs, for file sharing.
Jens Axboe906c8d72006-06-13 09:37:56 +0200851 */
Jens Axboeebac4652005-12-08 15:25:21 +0100852static int setup_thread_area(void)
853{
Jens Axboe380065a2008-03-01 18:56:24 +0100854 void *hash;
855
Jens Axboeebac4652005-12-08 15:25:21 +0100856 /*
857 * 1024 is too much on some machines, scale max_jobs if
858 * we get a failure that looks like too large a shm segment
859 */
860 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200861 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100862
Jens Axboe380065a2008-03-01 18:56:24 +0100863 size += file_hash_size;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200864 size += sizeof(unsigned int);
Jens Axboe380065a2008-03-01 18:56:24 +0100865
Jens Axboe906c8d72006-06-13 09:37:56 +0200866 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100867 if (shm_id != -1)
868 break;
869 if (errno != EINVAL) {
870 perror("shmget");
871 break;
872 }
873
874 max_jobs >>= 1;
875 } while (max_jobs);
876
877 if (shm_id == -1)
878 return 1;
879
880 threads = shmat(shm_id, NULL, 0);
881 if (threads == (void *) -1) {
882 perror("shmat");
883 return 1;
884 }
885
Jens Axboe1f809d12007-10-25 18:34:02 +0200886 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboe380065a2008-03-01 18:56:24 +0100887 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
Jens Axboe5e1d3062008-05-23 11:55:53 +0200888 fio_debug_jobp = (void *) hash + file_hash_size;
889 *fio_debug_jobp = -1;
Jens Axboe380065a2008-03-01 18:56:24 +0100890 file_hash_init(hash);
Jens Axboeebac4652005-12-08 15:25:21 +0100891 atexit(free_shm);
892 return 0;
893}
894
Jens Axboe45378b32007-12-19 13:54:38 +0100895static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +0200896{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100897 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +0100898 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboeee56ad52008-02-01 10:30:20 +0100899 printf("\t--debug=options\tEnable debug logging\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +0100900 printf("\t--output\tWrite output to file\n");
901 printf("\t--timeout\tRuntime in seconds\n");
902 printf("\t--latency-log\tGenerate per-job latency logs\n");
903 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
904 printf("\t--minimal\tMinimal (terse) output\n");
905 printf("\t--version\tPrint version info and exit\n");
906 printf("\t--help\t\tPrint this page\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100907 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
908 " them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +0200909 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +0200910 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
911 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100912 printf("\t--readonly\tTurn on safety read-only checks, preventing"
913 " writes\n");
Jens Axboe01f06b62008-02-18 20:53:47 +0100914 printf("\t--section=name\tOnly run specified section in job file\n");
Jens Axboe2b386d22008-03-26 10:32:57 +0100915 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
916 " (def 1024)\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200917}
918
Jens Axboe79e48f72008-02-01 20:37:09 +0100919#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +0100920struct debug_level debug_levels[] = {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100921 { .name = "process", .shift = FD_PROCESS, },
922 { .name = "file", .shift = FD_FILE, },
923 { .name = "io", .shift = FD_IO, },
924 { .name = "mem", .shift = FD_MEM, },
925 { .name = "blktrace", .shift = FD_BLKTRACE },
926 { .name = "verify", .shift = FD_VERIFY },
Jens Axboe84422ac2008-02-19 20:10:26 +0100927 { .name = "random", .shift = FD_RANDOM },
Jens Axboea3d741f2008-02-27 18:32:33 +0100928 { .name = "parse", .shift = FD_PARSE },
Jens Axboecd991b92008-03-07 13:19:35 +0100929 { .name = "diskutil", .shift = FD_DISKUTIL },
Jens Axboe5e1d3062008-05-23 11:55:53 +0200930 { .name = "job", .shift = FD_JOB },
Jens Axboe29adda32009-01-05 19:06:39 +0100931 { .name = "mutex", .shift = FD_MUTEX },
Jens Axboe02444ad2008-10-07 11:33:00 +0200932 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +0100933};
934
Jens Axboec09823a2008-02-19 20:16:57 +0100935static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +0100936{
937 struct debug_level *dl;
938 char *p = (char *) string;
939 char *opt;
940 int i;
941
942 if (!strcmp(string, "?") || !strcmp(string, "help")) {
943 int i;
944
945 log_info("fio: dumping debug options:");
946 for (i = 0; debug_levels[i].name; i++) {
947 dl = &debug_levels[i];
948 log_info("%s,", dl->name);
949 }
Jens Axboebd6f78b2008-02-01 20:27:52 +0100950 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100951 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +0100952 }
953
954 while ((opt = strsep(&p, ",")) != NULL) {
955 int found = 0;
956
Jens Axboe5e1d3062008-05-23 11:55:53 +0200957 if (!strncmp(opt, "all", 3)) {
958 log_info("fio: set all debug options\n");
959 fio_debug = ~0UL;
960 continue;
961 }
962
Jens Axboeee56ad52008-02-01 10:30:20 +0100963 for (i = 0; debug_levels[i].name; i++) {
964 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +0200965 found = !strncmp(opt, dl->name, strlen(dl->name));
966 if (!found)
967 continue;
968
969 if (dl->shift == FD_JOB) {
970 opt = strchr(opt, ':');
971 if (!opt) {
972 log_err("fio: missing job number\n");
973 break;
974 }
975 opt++;
976 fio_debug_jobno = atoi(opt);
977 log_info("fio: set debug jobno %d\n",
978 fio_debug_jobno);
979 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +0100980 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100981 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +0100982 }
Jens Axboe5e1d3062008-05-23 11:55:53 +0200983 break;
Jens Axboeee56ad52008-02-01 10:30:20 +0100984 }
985
986 if (!found)
987 log_err("fio: debug mask %s not found\n", opt);
988 }
Jens Axboec09823a2008-02-19 20:16:57 +0100989 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +0100990}
Jens Axboe79e48f72008-02-01 20:37:09 +0100991#else
Jens Axboe69b98d42008-05-30 22:25:32 +0200992static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +0100993{
994 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100995 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +0100996}
997#endif
Jens Axboeee56ad52008-02-01 10:30:20 +0100998
Jens Axboe214e1ec2007-03-15 10:48:13 +0100999static int parse_cmd_line(int argc, char *argv[])
1000{
1001 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001002 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001003
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001004 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001005 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001006 case 'a':
1007 smalloc_pool_size = atoi(optarg);
1008 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001009 case 't':
1010 def_timeout = atoi(optarg);
1011 break;
1012 case 'l':
1013 write_lat_log = 1;
1014 break;
1015 case 'w':
1016 write_bw_log = 1;
1017 break;
1018 case 'o':
1019 f_out = fopen(optarg, "w+");
1020 if (!f_out) {
1021 perror("fopen output");
1022 exit(1);
1023 }
1024 f_err = f_out;
1025 break;
1026 case 'm':
1027 terse_output = 1;
1028 break;
1029 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +01001030 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001031 exit(0);
1032 case 'c':
1033 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +02001034 case 's':
1035 dump_cmdline = 1;
1036 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001037 case 'r':
1038 read_only = 1;
1039 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001040 case 'v':
1041 printf("%s\n", fio_version_string);
1042 exit(0);
Aaron Carrolle592a062007-09-14 09:49:41 +02001043 case 'e':
1044 if (!strcmp("always", optarg))
1045 eta_print = FIO_ETA_ALWAYS;
1046 else if (!strcmp("never", optarg))
1047 eta_print = FIO_ETA_NEVER;
1048 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001049 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001050 if (set_debug(optarg))
1051 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001052 break;
Jens Axboe01f06b62008-02-18 20:53:47 +01001053 case 'x':
1054 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001055 log_err("fio: can't use global as only "
1056 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001057 do_exit++;
1058 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001059 break;
1060 }
1061 if (job_section)
1062 free(job_section);
1063 job_section = strdup(optarg);
1064 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001065 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001066 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001067 char *val = optarg;
1068
1069 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001070 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001071 if (ret) {
1072 put_job(td);
1073 return 0;
1074 }
1075 td = NULL;
1076 }
1077 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001078 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001079 int global = 0;
1080
Jens Axboe01f06b62008-02-18 20:53:47 +01001081 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001082 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001083
Jens Axboe01f06b62008-02-18 20:53:47 +01001084 if (is_section && skip_this_section(val))
1085 continue;
1086
Jens Axboe214e1ec2007-03-15 10:48:13 +01001087 td = get_new_job(global, &def_thread);
1088 if (!td)
1089 return 0;
1090 }
1091
1092 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001093 break;
1094 }
1095 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001096 do_exit++;
1097 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001098 break;
1099 }
1100 }
1101
Jens Axboec09823a2008-02-19 20:16:57 +01001102 if (do_exit)
1103 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001104
Jens Axboe214e1ec2007-03-15 10:48:13 +01001105 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001106 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001107 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe7d6a8902008-02-18 20:59:18 +01001108 if (ret)
1109 put_job(td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001110 }
1111
1112 while (optind < argc) {
1113 ini_idx++;
1114 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1115 ini_file[ini_idx - 1] = strdup(argv[optind]);
1116 optind++;
1117 }
1118
1119 return ini_idx;
1120}
1121
Jens Axboeebac4652005-12-08 15:25:21 +01001122int parse_options(int argc, char *argv[])
1123{
Jens Axboe972cfd22006-06-09 11:08:56 +02001124 int job_files, i;
1125
Jens Axboeb4692822006-10-27 13:43:22 +02001126 f_out = stdout;
1127 f_err = stderr;
1128
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001129 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001130
Jens Axboeebac4652005-12-08 15:25:21 +01001131 if (setup_thread_area())
1132 return 1;
1133 if (fill_def_thread())
1134 return 1;
1135
Jens Axboe972cfd22006-06-09 11:08:56 +02001136 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001137
Jens Axboe972cfd22006-06-09 11:08:56 +02001138 for (i = 0; i < job_files; i++) {
1139 if (fill_def_thread())
1140 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001141 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001142 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001143 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001144 }
Jens Axboeebac4652005-12-08 15:25:21 +01001145
Jens Axboe88c6ed82006-06-09 11:28:10 +02001146 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001147 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001148
1149 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001150 if (dump_cmdline)
1151 return 0;
1152
Jens Axboeb4692822006-10-27 13:43:22 +02001153 log_err("No jobs defined(s)\n");
Jens Axboe45378b32007-12-19 13:54:38 +01001154 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +02001155 return 1;
1156 }
1157
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001158 if (def_thread.o.gtod_offload) {
1159 fio_gtod_init();
1160 fio_gtod_offload = 1;
1161 fio_gtod_cpu = def_thread.o.gtod_cpu;
1162 }
1163
Jens Axboeebac4652005-12-08 15:25:21 +01001164 return 0;
1165}