blob: 3142d8ca7a8dd9c32e606ea6bc2c7d49843f8b15 [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 Axboeebac4652005-12-08 15:25:21 +010021
Jens Axboe860a8a62008-11-18 19:22:31 +010022static char fio_version_string[] = "fio 1.23";
Jens Axboe214e1ec2007-03-15 10:48:13 +010023
Jens Axboeee738492007-01-10 11:23:16 +010024#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010025
Jens Axboe214e1ec2007-03-15 10:48:13 +010026static char **ini_file;
27static int max_jobs = MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020028static int dump_cmdline;
Jens Axboee1f36502006-10-27 10:54:08 +020029
Jens Axboebe4ecfd2008-12-08 14:10:52 +010030static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010031struct thread_data *threads = NULL;
Jens Axboee1f36502006-10-27 10:54:08 +020032
Jens Axboe214e1ec2007-03-15 10:48:13 +010033int exitall_on_terminate = 0;
34int terse_output = 0;
Aaron Carrolle592a062007-09-14 09:49:41 +020035int eta_print;
Jens Axboe214e1ec2007-03-15 10:48:13 +010036unsigned long long mlock_size = 0;
37FILE *f_out = NULL;
38FILE *f_err = NULL;
Jens Axboe01f06b62008-02-18 20:53:47 +010039char *job_section = NULL;
Jens Axboeee738492007-01-10 11:23:16 +010040
Jens Axboe214e1ec2007-03-15 10:48:13 +010041int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020042int read_only = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020043
Jens Axboe5ec10ea2008-03-06 15:42:00 +010044static int def_timeout;
45static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010046
47static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020048
Jens Axboeee56ad52008-02-01 10:30:20 +010049unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020050unsigned int fio_debug_jobno = -1;
51unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010052
Jens Axboeb4692822006-10-27 13:43:22 +020053/*
54 * Command line options. These will contain the above, plus a few
55 * extra that only pertain to fio itself and not jobs.
56 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010057static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020058 {
59 .name = "output",
60 .has_arg = required_argument,
61 .val = 'o',
62 },
63 {
64 .name = "timeout",
65 .has_arg = required_argument,
66 .val = 't',
67 },
68 {
69 .name = "latency-log",
70 .has_arg = required_argument,
71 .val = 'l',
72 },
73 {
74 .name = "bandwidth-log",
75 .has_arg = required_argument,
76 .val = 'b',
77 },
78 {
79 .name = "minimal",
80 .has_arg = optional_argument,
81 .val = 'm',
82 },
83 {
84 .name = "version",
85 .has_arg = no_argument,
86 .val = 'v',
87 },
88 {
Jens Axboefd28ca42007-01-09 21:20:13 +010089 .name = "help",
90 .has_arg = no_argument,
91 .val = 'h',
92 },
93 {
94 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +010095 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +010096 .val = 'c',
97 },
98 {
Jens Axboecca73aa2007-04-04 11:09:19 +020099 .name = "showcmd",
100 .has_arg = no_argument,
Jens Axboe724e4432007-09-11 20:02:05 +0200101 .val = 's',
102 },
103 {
104 .name = "readonly",
105 .has_arg = no_argument,
106 .val = 'r',
Jens Axboecca73aa2007-04-04 11:09:19 +0200107 },
108 {
Aaron Carrolle592a062007-09-14 09:49:41 +0200109 .name = "eta",
110 .has_arg = required_argument,
111 .val = 'e',
112 },
113 {
Jens Axboeee56ad52008-02-01 10:30:20 +0100114 .name = "debug",
115 .has_arg = required_argument,
116 .val = 'd',
117 },
118 {
Jens Axboe01f06b62008-02-18 20:53:47 +0100119 .name = "section",
120 .has_arg = required_argument,
121 .val = 'x',
122 },
123 {
Jens Axboe2b386d22008-03-26 10:32:57 +0100124 .name = "alloc-size",
125 .has_arg = required_argument,
126 .val = 'a',
127 },
128 {
Jens Axboeb4692822006-10-27 13:43:22 +0200129 .name = NULL,
130 },
131};
132
Joel Becker9728ce32007-03-01 08:24:39 +0100133FILE *get_f_out()
134{
135 return f_out;
136}
137
138FILE *get_f_err()
139{
140 return f_err;
141}
142
Jens Axboe906c8d72006-06-13 09:37:56 +0200143/*
144 * Return a free job structure.
145 */
Jens Axboeebac4652005-12-08 15:25:21 +0100146static struct thread_data *get_new_job(int global, struct thread_data *parent)
147{
148 struct thread_data *td;
149
150 if (global)
151 return &def_thread;
152 if (thread_number >= max_jobs)
153 return NULL;
154
155 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200156 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100157
Jens Axboecade3ef2007-03-23 15:29:45 +0100158 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100159 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100160
Jens Axboeebac4652005-12-08 15:25:21 +0100161 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100162 return td;
163}
164
165static void put_job(struct thread_data *td)
166{
Jens Axboe549577a2006-10-30 12:23:41 +0100167 if (td == &def_thread)
168 return;
169
Jens Axboe16edf252007-02-10 14:59:22 +0100170 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100171 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100172
Jens Axboeebac4652005-12-08 15:25:21 +0100173 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
174 thread_number--;
175}
176
Jens Axboe127f6862007-03-15 12:09:57 +0100177static int setup_rate(struct thread_data *td)
178{
179 unsigned long nr_reads_per_msec;
180 unsigned long long rate;
181 unsigned int bs;
182
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100183 if (!td->o.rate && !td->o.rate_iops)
Jens Axboe127f6862007-03-15 12:09:57 +0100184 return 0;
185
186 if (td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100187 bs = td->o.rw_min_bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100188 else if (td_read(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100189 bs = td->o.min_bs[DDIR_READ];
Jens Axboe127f6862007-03-15 12:09:57 +0100190 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100191 bs = td->o.min_bs[DDIR_WRITE];
Jens Axboe127f6862007-03-15 12:09:57 +0100192
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100193 if (td->o.rate) {
194 rate = td->o.rate;
Jens Axboe127f6862007-03-15 12:09:57 +0100195 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
196 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100197 nr_reads_per_msec = td->o.rate_iops * 1000UL;
Jens Axboe127f6862007-03-15 12:09:57 +0100198
199 if (!nr_reads_per_msec) {
200 log_err("rate lower than supported\n");
201 return -1;
202 }
203
204 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
205 td->rate_pending_usleep = 0;
206 return 0;
207}
208
Jens Axboedad915e2006-10-27 11:10:18 +0200209/*
210 * Lazy way of fixing up options that depend on each other. We could also
211 * define option callback handlers, but this is easier.
212 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100213static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200214{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100215 struct thread_options *o = &td->o;
Jens Axboedad915e2006-10-27 11:10:18 +0200216
Jens Axboef356d012009-01-05 09:56:29 +0100217#ifndef FIO_HAVE_PSHARED_MUTEX
218 if (!td->o.use_thread) {
219 log_info("fio: this platform does not support process shared"
220 " mutexes, forcing use of threads. Use the 'thread'"
221 " option to get rid of this warning.\n");
222 td->o.use_thread = 1;
223 }
224#endif
225
Jens Axboebe4ecfd2008-12-08 14:10:52 +0100226#ifndef FIO_HAVE_CPU_AFFINITY
227 if (td->o.gtod_cpu) {
228 log_err("fio: platform must support CPU affinity for"
229 "gettimeofday() offloading\n");
230 return 1;
231 }
232#endif
233
Jens Axboe724e4432007-09-11 20:02:05 +0200234 if (read_only && td_write(td)) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100235 log_err("fio: job <%s> has write bit set, but fio is in"
236 " read-only mode\n", td->o.name);
Jens Axboe724e4432007-09-11 20:02:05 +0200237 return 1;
238 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100239
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100240 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200241 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100242 free(o->write_iolog_file);
243 o->write_iolog_file = NULL;
Jens Axboe076efc72006-10-27 11:24:25 +0200244 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100245
Jens Axboe16b462a2006-10-30 12:35:18 +0100246 /*
247 * only really works for sequential io for now, and with 1 file
248 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100249 if (o->zone_size && td_random(td) && o->open_files == 1)
250 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100251
252 /*
253 * Reads can do overwrites, we always need to pre-create the file
254 */
255 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100256 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100257
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100258 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100259 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100260 if (!o->max_bs[DDIR_READ])
261 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
262 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100263 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100264 if (!o->max_bs[DDIR_WRITE])
265 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100266
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100267 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100268
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100269 if (!o->file_size_high)
270 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100271
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100272 if (o->norandommap && o->verify != VERIFY_NONE) {
Jens Axboebb8895e2006-10-30 15:14:48 +0100273 log_err("fio: norandommap given, verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100274 o->verify = VERIFY_NONE;
Jens Axboebb8895e2006-10-30 15:14:48 +0100275 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100276 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100277 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100278
279 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100280 * thinktime_spin must be less than thinktime
281 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100282 if (o->thinktime_spin > o->thinktime)
283 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100284
285 /*
286 * The low water mark cannot be bigger than the iodepth
287 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100288 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100289 /*
290 * syslet work around - if the workload is sequential,
291 * we want to let the queue drain all the way down to
292 * avoid seeking between async threads
293 */
294 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100295 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100296 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100297 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100298 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100299
300 /*
301 * If batch number isn't set, default to the same as iodepth
302 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100303 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
304 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100305
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100306 if (o->nr_files > td->files_index)
307 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100308
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100309 if (o->open_files > o->nr_files || !o->open_files)
310 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100311
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100312 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100313 log_err("fio: rate and rate_iops are mutually exclusive\n");
314 return 1;
315 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100316 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100317 log_err("fio: minimum rate exceeds rate\n");
318 return 1;
319 }
320
Jens Axboecf4464c2007-04-17 20:14:42 +0200321 if (!o->timeout && o->time_based) {
322 log_err("fio: time_based requires a runtime/timeout setting\n");
323 o->time_based = 0;
324 }
325
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100326 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200327 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100328
Jens Axboe79713142008-02-06 14:40:14 +0100329 if (td_rw(td) && td->o.verify != VERIFY_NONE)
330 log_info("fio: mixed read/write workload with verify. May not "
331 "work as expected, unless you pre-populated the file\n");
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100332
Jens Axboe41ccd842008-05-22 09:17:33 +0200333 if (td->o.verify != VERIFY_NONE)
334 td->o.refill_buffers = 1;
335
Jens Axboe4e991c22007-03-15 11:41:11 +0100336 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200337}
338
Jens Axboe906c8d72006-06-13 09:37:56 +0200339/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100340 * This function leaks the buffer
341 */
342static char *to_kmg(unsigned int val)
343{
344 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100345 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100346 char *p = post;
347
Jens Axboe245142f2006-11-08 12:49:21 +0100348 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100349 if (val & 1023)
350 break;
351
352 val >>= 10;
353 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100354 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100355
356 snprintf(buf, 31, "%u%c", val, *p);
357 return buf;
358}
359
Jens Axboe09629a92007-03-09 09:00:06 +0100360/* External engines are specified by "external:name.o") */
361static const char *get_engine_name(const char *str)
362{
363 char *p = strstr(str, ":");
364
365 if (!p)
366 return str;
367
368 p++;
369 strip_blank_front(&p);
370 strip_blank_end(p);
371 return p;
372}
373
Jens Axboee132cba2007-03-14 14:23:54 +0100374static int exists_and_not_file(const char *filename)
375{
376 struct stat sb;
377
378 if (lstat(filename, &sb) == -1)
379 return 0;
380
381 if (S_ISREG(sb.st_mode))
382 return 0;
383
384 return 1;
385}
386
Jens Axboef8977ee2006-11-06 14:03:03 +0100387/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100388 * Initialize the various random states we need (random io, block size ranges,
389 * read/write mix, etc).
390 */
391static int init_random_state(struct thread_data *td)
392{
393 unsigned long seeds[6];
Jens Axboe68727072007-03-15 20:49:25 +0100394 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100395
396 fd = open("/dev/urandom", O_RDONLY);
397 if (fd == -1) {
398 td_verror(td, errno, "open");
399 return 1;
400 }
401
402 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
403 td_verror(td, EIO, "read");
404 close(fd);
405 return 1;
406 }
407
408 close(fd);
409
410 os_random_seed(seeds[0], &td->bsrange_state);
411 os_random_seed(seeds[1], &td->verify_state);
412 os_random_seed(seeds[2], &td->rwmix_state);
413
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100414 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100415 os_random_seed(seeds[3], &td->next_file_state);
416
417 os_random_seed(seeds[5], &td->file_size_state);
418
419 if (!td_random(td))
420 return 0;
421
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100422 if (td->o.rand_repeatable)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100423 seeds[4] = FIO_RANDSEED * td->thread_number;
424
Jens Axboe9c60ce62007-03-15 09:14:47 +0100425 os_random_seed(seeds[4], &td->random_state);
426 return 0;
427}
428
Jens Axboe9c60ce62007-03-15 09:14:47 +0100429/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200430 * Adds a job to the list of things todo. Sanitizes the various options
431 * to make sure we don't have conflicts, and initializes various
432 * members of td.
433 */
Jens Axboe75154842006-06-01 13:56:09 +0200434static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100435{
Jens Axboe413dd452007-02-23 09:26:09 +0100436 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
437 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100438 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100439 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100440 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100441 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100442
Jens Axboeebac4652005-12-08 15:25:21 +0100443 /*
444 * the def_thread is just for options, it's not a real job
445 */
446 if (td == &def_thread)
447 return 0;
448
Jens Axboecca73aa2007-04-04 11:09:19 +0200449 /*
450 * if we are just dumping the output command line, don't add the job
451 */
452 if (dump_cmdline) {
453 put_job(td);
454 return 0;
455 }
456
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100457 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100458 td->io_ops = load_ioengine(td, engine);
459 if (!td->io_ops) {
460 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100461 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100462 }
Jens Axboedf641192006-10-12 07:55:41 +0200463
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100464 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100465 nr_thread++;
466 else
467 nr_process++;
468
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100469 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100470 td->io_ops->flags |= FIO_RAWIO;
471
Jens Axboee132cba2007-03-14 14:23:54 +0100472 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100473 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100474 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100475
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100476 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100477 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100478 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100479 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100480 sprintf(fname, "%s.%d.%d", jobname,
481 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100482 add_file(td, fname);
483 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100484 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100485 }
Jens Axboeebac4652005-12-08 15:25:21 +0100486
Jens Axboe4e991c22007-03-15 11:41:11 +0100487 if (fixup_options(td))
488 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100489
Jens Axboe07eb79d2007-04-12 13:02:38 +0200490 if (td->io_ops->flags & FIO_DISKLESSIO) {
491 struct fio_file *f;
492
493 for_each_file(td, f, i)
494 f->real_file_size = -1ULL;
495 }
496
Jens Axboecdd18ad2008-02-27 18:58:00 +0100497 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100498
Jens Axboe756867b2007-03-06 15:19:24 +0100499 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
500 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
501 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboe1e3d53a2007-03-22 19:01:48 +0100502 td->ddir_nr = td->o.ddir_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100503
Jens Axboeb3d62a72007-03-20 14:23:26 +0100504 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
505 && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100506 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100507 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100508 }
Jens Axboeebac4652005-12-08 15:25:21 +0100509
510 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100511 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100512
Jens Axboe9c60ce62007-03-15 09:14:47 +0100513 if (init_random_state(td))
514 goto err;
515
Jens Axboeebac4652005-12-08 15:25:21 +0100516 if (setup_rate(td))
517 goto err;
518
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100519 if (td->o.write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100520 setup_log(&td->ts.slat_log);
521 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100522 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100523 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100524 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100525
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100526 if (!td->o.name)
527 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200528
Jens Axboec6ae0a52006-06-12 11:04:44 +0200529 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200530 if (!job_add_num) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100531 if (!strcmp(td->io_ops->name, "cpuio")) {
532 log_info("%s: ioengine=cpu, cpuload=%u,"
533 " cpucycle=%u\n", td->o.name,
534 td->o.cpuload,
535 td->o.cpucycle);
536 } else {
Jens Axboef8977ee2006-11-06 14:03:03 +0100537 char *c1, *c2, *c3, *c4;
538
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100539 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
540 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
541 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
542 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100543
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100544 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
545 " ioengine=%s, iodepth=%u\n",
546 td->o.name, td->groupid,
547 ddir_str[td->o.td_ddir],
548 c1, c2, c3, c4,
549 td->io_ops->name,
550 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100551
552 free(c1);
553 free(c2);
554 free(c3);
555 free(c4);
556 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200557 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100558 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200559 }
Jens Axboeebac4652005-12-08 15:25:21 +0100560
561 /*
562 * recurse add identical jobs, clear numjobs and stonewall options
563 * as they don't apply to sub-jobs
564 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100565 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100566 while (--numjobs) {
567 struct thread_data *td_new = get_new_job(0, td);
568
569 if (!td_new)
570 goto err;
571
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100572 td_new->o.numjobs = 1;
573 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200574 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100575
576 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100577 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100578 td_new->files_index = 0;
579 td_new->files = NULL;
580 }
581
Jens Axboe75154842006-06-01 13:56:09 +0200582 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100583
Jens Axboe75154842006-06-01 13:56:09 +0200584 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100585 goto err;
586 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100587
Jens Axboeebac4652005-12-08 15:25:21 +0100588 return 0;
589err:
590 put_job(td);
591 return -1;
592}
593
Jens Axboe01f06b62008-02-18 20:53:47 +0100594static int skip_this_section(const char *name)
595{
596 if (!job_section)
597 return 0;
598 if (!strncmp(name, "global", 6))
599 return 0;
600
601 return strncmp(job_section, name, strlen(job_section));
602}
603
Jens Axboeebac4652005-12-08 15:25:21 +0100604static int is_empty_or_comment(char *line)
605{
606 unsigned int i;
607
608 for (i = 0; i < strlen(line); i++) {
609 if (line[i] == ';')
610 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100611 if (line[i] == '#')
612 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100613 if (!isspace(line[i]) && !iscntrl(line[i]))
614 return 0;
615 }
616
617 return 1;
618}
619
Jens Axboe313cb202006-12-21 09:50:00 +0100620/*
Jens Axboe07261982006-06-07 10:51:12 +0200621 * This is our [ini] type file parser.
622 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100623static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100624{
Jens Axboee1f36502006-10-27 10:54:08 +0200625 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100626 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100627 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100628 FILE *f;
629 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200630 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200631 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200632 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100633 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200634 char **opts;
635 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +0100636
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200637 if (!strcmp(file, "-"))
638 f = stdin;
639 else
640 f = fopen(file, "r");
641
Jens Axboeebac4652005-12-08 15:25:21 +0100642 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200643 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100644 return 1;
645 }
646
647 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200648
649 /*
650 * it's really 256 + small bit, 280 should suffice
651 */
652 name = malloc(280);
653 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100654
Jens Axboe3b8b7132008-06-10 19:46:23 +0200655 alloc_opts = 8;
656 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +0200657 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200658
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200659 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100660 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200661 /*
662 * if skip_fgets is set, we already have loaded a line we
663 * haven't handled.
664 */
665 if (!skip_fgets) {
666 p = fgets(string, 4095, f);
667 if (!p)
668 break;
669 }
gurudas paicdc7f192007-03-29 10:10:56 +0200670
Jens Axboeccf8f122007-09-27 10:48:55 +0200671 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200672 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800673 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200674
Jens Axboeebac4652005-12-08 15:25:21 +0100675 if (is_empty_or_comment(p))
676 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800677 if (sscanf(p, "[%255s]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100678 if (inside_skip)
679 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100680 log_err("fio: option <%s> outside of [] job section\n",
681 p);
Jens Axboe088b4202007-07-19 14:53:01 +0200682 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800683 }
Jens Axboeebac4652005-12-08 15:25:21 +0100684
Jens Axboe01f06b62008-02-18 20:53:47 +0100685 if (skip_this_section(name)) {
686 inside_skip = 1;
687 continue;
688 } else
689 inside_skip = 0;
690
Jens Axboeebac4652005-12-08 15:25:21 +0100691 global = !strncmp(name, "global", 6);
692
693 name[strlen(name) - 1] = '\0';
694
Jens Axboecca73aa2007-04-04 11:09:19 +0200695 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200696 if (first_sect)
697 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200698 if (!global)
699 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200700 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200701 }
702
Jens Axboeebac4652005-12-08 15:25:21 +0100703 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200704 if (!td) {
705 ret = 1;
706 break;
707 }
Jens Axboeebac4652005-12-08 15:25:21 +0100708
Jens Axboe972cfd22006-06-09 11:08:56 +0200709 /*
710 * Seperate multiple job files by a stonewall
711 */
Jens Axboef9481912006-06-09 11:37:28 +0200712 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100713 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200714 stonewall = 0;
715 }
716
Jens Axboe3b8b7132008-06-10 19:46:23 +0200717 num_opts = 0;
718 memset(opts, 0, alloc_opts * sizeof(char *));
719
Jens Axboeebac4652005-12-08 15:25:21 +0100720 while ((p = fgets(string, 4096, f)) != NULL) {
721 if (is_empty_or_comment(p))
722 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200723
Jens Axboeb6754f92006-05-30 14:29:32 +0200724 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100725
Jens Axboeccf8f122007-09-27 10:48:55 +0200726 /*
727 * new section, break out and make sure we don't
728 * fgets() a new line at the top.
729 */
730 if (p[0] == '[') {
731 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100732 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200733 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100734
Jens Axboe4ae3f762006-05-30 13:35:14 +0200735 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200736
Jens Axboe3b8b7132008-06-10 19:46:23 +0200737 if (num_opts == alloc_opts) {
738 alloc_opts <<= 1;
739 opts = realloc(opts,
740 alloc_opts * sizeof(char *));
741 }
742
743 opts[num_opts] = strdup(p);
744 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +0100745 }
Jens Axboeebac4652005-12-08 15:25:21 +0100746
Jens Axboe3b8b7132008-06-10 19:46:23 +0200747 ret = fio_options_parse(td, opts, num_opts);
748 if (!ret) {
749 if (dump_cmdline)
750 for (i = 0; i < num_opts; i++)
751 log_info("--%s ", opts[i]);
752
Jens Axboe45410ac2006-06-07 11:10:39 +0200753 ret = add_job(td, name, 0);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200754 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100755 log_err("fio: job %s dropped\n", name);
756 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200757 }
Jens Axboe3b8b7132008-06-10 19:46:23 +0200758
759 for (i = 0; i < num_opts; i++)
760 free(opts[i]);
761 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100762 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100763
Jens Axboecca73aa2007-04-04 11:09:19 +0200764 if (dump_cmdline)
765 log_info("\n");
766
Jens Axboe3b8b7132008-06-10 19:46:23 +0200767 for (i = 0; i < num_opts; i++)
768 free(opts[i]);
769
Jens Axboeebac4652005-12-08 15:25:21 +0100770 free(string);
771 free(name);
Jens Axboe25775942008-06-10 20:26:06 +0200772 free(opts);
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200773 if (f != stdin)
774 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200775 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100776}
777
778static int fill_def_thread(void)
779{
780 memset(&def_thread, 0, sizeof(def_thread));
781
Jens Axboe375b2692007-05-22 09:13:02 +0200782 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100783
784 /*
Jens Axboeee738492007-01-10 11:23:16 +0100785 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100786 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100787 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100788
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100789 def_thread.o.timeout = def_timeout;
790 def_thread.o.write_bw_log = write_bw_log;
791 def_thread.o.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100792
Jens Axboeebac4652005-12-08 15:25:21 +0100793 return 0;
794}
795
Jens Axboeebac4652005-12-08 15:25:21 +0100796static void free_shm(void)
797{
798 struct shmid_ds sbuf;
799
800 if (threads) {
Jens Axboe5e1d3062008-05-23 11:55:53 +0200801 void *tp = threads;
802
Jens Axboeebac4652005-12-08 15:25:21 +0100803 threads = NULL;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200804 file_hash_exit();
805 fio_debug_jobp = NULL;
806 shmdt(tp);
Jens Axboeebac4652005-12-08 15:25:21 +0100807 shmctl(shm_id, IPC_RMID, &sbuf);
808 }
Jens Axboe2e5cdb12008-03-01 18:52:49 +0100809
810 scleanup();
Jens Axboeebac4652005-12-08 15:25:21 +0100811}
812
Jens Axboe906c8d72006-06-13 09:37:56 +0200813/*
814 * The thread area is shared between the main process and the job
815 * threads/processes. So setup a shared memory segment that will hold
Jens Axboe380065a2008-03-01 18:56:24 +0100816 * all the job info. We use the end of the region for keeping track of
817 * open files across jobs, for file sharing.
Jens Axboe906c8d72006-06-13 09:37:56 +0200818 */
Jens Axboeebac4652005-12-08 15:25:21 +0100819static int setup_thread_area(void)
820{
Jens Axboe380065a2008-03-01 18:56:24 +0100821 void *hash;
822
Jens Axboeebac4652005-12-08 15:25:21 +0100823 /*
824 * 1024 is too much on some machines, scale max_jobs if
825 * we get a failure that looks like too large a shm segment
826 */
827 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200828 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100829
Jens Axboe380065a2008-03-01 18:56:24 +0100830 size += file_hash_size;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200831 size += sizeof(unsigned int);
Jens Axboe380065a2008-03-01 18:56:24 +0100832
Jens Axboe906c8d72006-06-13 09:37:56 +0200833 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100834 if (shm_id != -1)
835 break;
836 if (errno != EINVAL) {
837 perror("shmget");
838 break;
839 }
840
841 max_jobs >>= 1;
842 } while (max_jobs);
843
844 if (shm_id == -1)
845 return 1;
846
847 threads = shmat(shm_id, NULL, 0);
848 if (threads == (void *) -1) {
849 perror("shmat");
850 return 1;
851 }
852
Jens Axboe1f809d12007-10-25 18:34:02 +0200853 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboe380065a2008-03-01 18:56:24 +0100854 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
Jens Axboe5e1d3062008-05-23 11:55:53 +0200855 fio_debug_jobp = (void *) hash + file_hash_size;
856 *fio_debug_jobp = -1;
Jens Axboe380065a2008-03-01 18:56:24 +0100857 file_hash_init(hash);
Jens Axboeebac4652005-12-08 15:25:21 +0100858 atexit(free_shm);
859 return 0;
860}
861
Jens Axboe45378b32007-12-19 13:54:38 +0100862static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +0200863{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100864 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +0100865 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboeee56ad52008-02-01 10:30:20 +0100866 printf("\t--debug=options\tEnable debug logging\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +0100867 printf("\t--output\tWrite output to file\n");
868 printf("\t--timeout\tRuntime in seconds\n");
869 printf("\t--latency-log\tGenerate per-job latency logs\n");
870 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
871 printf("\t--minimal\tMinimal (terse) output\n");
872 printf("\t--version\tPrint version info and exit\n");
873 printf("\t--help\t\tPrint this page\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100874 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
875 " them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +0200876 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +0200877 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
878 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100879 printf("\t--readonly\tTurn on safety read-only checks, preventing"
880 " writes\n");
Jens Axboe01f06b62008-02-18 20:53:47 +0100881 printf("\t--section=name\tOnly run specified section in job file\n");
Jens Axboe2b386d22008-03-26 10:32:57 +0100882 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
883 " (def 1024)\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200884}
885
Jens Axboe79e48f72008-02-01 20:37:09 +0100886#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +0100887struct debug_level debug_levels[] = {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100888 { .name = "process", .shift = FD_PROCESS, },
889 { .name = "file", .shift = FD_FILE, },
890 { .name = "io", .shift = FD_IO, },
891 { .name = "mem", .shift = FD_MEM, },
892 { .name = "blktrace", .shift = FD_BLKTRACE },
893 { .name = "verify", .shift = FD_VERIFY },
Jens Axboe84422ac2008-02-19 20:10:26 +0100894 { .name = "random", .shift = FD_RANDOM },
Jens Axboea3d741f2008-02-27 18:32:33 +0100895 { .name = "parse", .shift = FD_PARSE },
Jens Axboecd991b92008-03-07 13:19:35 +0100896 { .name = "diskutil", .shift = FD_DISKUTIL },
Jens Axboe5e1d3062008-05-23 11:55:53 +0200897 { .name = "job", .shift = FD_JOB },
Jens Axboe02444ad2008-10-07 11:33:00 +0200898 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +0100899};
900
Jens Axboec09823a2008-02-19 20:16:57 +0100901static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +0100902{
903 struct debug_level *dl;
904 char *p = (char *) string;
905 char *opt;
906 int i;
907
908 if (!strcmp(string, "?") || !strcmp(string, "help")) {
909 int i;
910
911 log_info("fio: dumping debug options:");
912 for (i = 0; debug_levels[i].name; i++) {
913 dl = &debug_levels[i];
914 log_info("%s,", dl->name);
915 }
Jens Axboebd6f78b2008-02-01 20:27:52 +0100916 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100917 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +0100918 }
919
920 while ((opt = strsep(&p, ",")) != NULL) {
921 int found = 0;
922
Jens Axboe5e1d3062008-05-23 11:55:53 +0200923 if (!strncmp(opt, "all", 3)) {
924 log_info("fio: set all debug options\n");
925 fio_debug = ~0UL;
926 continue;
927 }
928
Jens Axboeee56ad52008-02-01 10:30:20 +0100929 for (i = 0; debug_levels[i].name; i++) {
930 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +0200931 found = !strncmp(opt, dl->name, strlen(dl->name));
932 if (!found)
933 continue;
934
935 if (dl->shift == FD_JOB) {
936 opt = strchr(opt, ':');
937 if (!opt) {
938 log_err("fio: missing job number\n");
939 break;
940 }
941 opt++;
942 fio_debug_jobno = atoi(opt);
943 log_info("fio: set debug jobno %d\n",
944 fio_debug_jobno);
945 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +0100946 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100947 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +0100948 }
Jens Axboe5e1d3062008-05-23 11:55:53 +0200949 break;
Jens Axboeee56ad52008-02-01 10:30:20 +0100950 }
951
952 if (!found)
953 log_err("fio: debug mask %s not found\n", opt);
954 }
Jens Axboec09823a2008-02-19 20:16:57 +0100955 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +0100956}
Jens Axboe79e48f72008-02-01 20:37:09 +0100957#else
Jens Axboe69b98d42008-05-30 22:25:32 +0200958static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +0100959{
960 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100961 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +0100962}
963#endif
Jens Axboeee56ad52008-02-01 10:30:20 +0100964
Jens Axboe214e1ec2007-03-15 10:48:13 +0100965static int parse_cmd_line(int argc, char *argv[])
966{
967 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +0100968 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100969
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100970 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
Jens Axboe214e1ec2007-03-15 10:48:13 +0100971 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +0100972 case 'a':
973 smalloc_pool_size = atoi(optarg);
974 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100975 case 't':
976 def_timeout = atoi(optarg);
977 break;
978 case 'l':
979 write_lat_log = 1;
980 break;
981 case 'w':
982 write_bw_log = 1;
983 break;
984 case 'o':
985 f_out = fopen(optarg, "w+");
986 if (!f_out) {
987 perror("fopen output");
988 exit(1);
989 }
990 f_err = f_out;
991 break;
992 case 'm':
993 terse_output = 1;
994 break;
995 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +0100996 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100997 exit(0);
998 case 'c':
999 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +02001000 case 's':
1001 dump_cmdline = 1;
1002 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001003 case 'r':
1004 read_only = 1;
1005 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001006 case 'v':
1007 printf("%s\n", fio_version_string);
1008 exit(0);
Aaron Carrolle592a062007-09-14 09:49:41 +02001009 case 'e':
1010 if (!strcmp("always", optarg))
1011 eta_print = FIO_ETA_ALWAYS;
1012 else if (!strcmp("never", optarg))
1013 eta_print = FIO_ETA_NEVER;
1014 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001015 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001016 if (set_debug(optarg))
1017 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001018 break;
Jens Axboe01f06b62008-02-18 20:53:47 +01001019 case 'x':
1020 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001021 log_err("fio: can't use global as only "
1022 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001023 do_exit++;
1024 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001025 break;
1026 }
1027 if (job_section)
1028 free(job_section);
1029 job_section = strdup(optarg);
1030 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001031 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001032 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001033 char *val = optarg;
1034
1035 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001036 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001037 if (ret) {
1038 put_job(td);
1039 return 0;
1040 }
1041 td = NULL;
1042 }
1043 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001044 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001045 int global = 0;
1046
Jens Axboe01f06b62008-02-18 20:53:47 +01001047 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001048 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001049
Jens Axboe01f06b62008-02-18 20:53:47 +01001050 if (is_section && skip_this_section(val))
1051 continue;
1052
Jens Axboe214e1ec2007-03-15 10:48:13 +01001053 td = get_new_job(global, &def_thread);
1054 if (!td)
1055 return 0;
1056 }
1057
1058 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001059 break;
1060 }
1061 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001062 do_exit++;
1063 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001064 break;
1065 }
1066 }
1067
Jens Axboec09823a2008-02-19 20:16:57 +01001068 if (do_exit)
1069 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001070
Jens Axboe214e1ec2007-03-15 10:48:13 +01001071 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001072 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001073 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe7d6a8902008-02-18 20:59:18 +01001074 if (ret)
1075 put_job(td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001076 }
1077
1078 while (optind < argc) {
1079 ini_idx++;
1080 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1081 ini_file[ini_idx - 1] = strdup(argv[optind]);
1082 optind++;
1083 }
1084
1085 return ini_idx;
1086}
1087
Jens Axboeebac4652005-12-08 15:25:21 +01001088int parse_options(int argc, char *argv[])
1089{
Jens Axboe972cfd22006-06-09 11:08:56 +02001090 int job_files, i;
1091
Jens Axboeb4692822006-10-27 13:43:22 +02001092 f_out = stdout;
1093 f_err = stderr;
1094
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001095 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001096
Jens Axboeebac4652005-12-08 15:25:21 +01001097 if (setup_thread_area())
1098 return 1;
1099 if (fill_def_thread())
1100 return 1;
1101
Jens Axboe972cfd22006-06-09 11:08:56 +02001102 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001103
Jens Axboe972cfd22006-06-09 11:08:56 +02001104 for (i = 0; i < job_files; i++) {
1105 if (fill_def_thread())
1106 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001107 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001108 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001109 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001110 }
Jens Axboeebac4652005-12-08 15:25:21 +01001111
Jens Axboe88c6ed82006-06-09 11:28:10 +02001112 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001113 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001114
1115 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001116 if (dump_cmdline)
1117 return 0;
1118
Jens Axboeb4692822006-10-27 13:43:22 +02001119 log_err("No jobs defined(s)\n");
Jens Axboe45378b32007-12-19 13:54:38 +01001120 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +02001121 return 1;
1122 }
1123
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001124 if (def_thread.o.gtod_offload) {
1125 fio_gtod_init();
1126 fio_gtod_offload = 1;
1127 fio_gtod_cpu = def_thread.o.gtod_cpu;
1128 }
1129
Jens Axboeebac4652005-12-08 15:25:21 +01001130 return 0;
1131}