blob: a8acdc04b4e1be49fc080be3f0a9a17e09281cef [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 Axboebe4ecfd2008-12-08 14:10:52 +0100217#ifndef FIO_HAVE_CPU_AFFINITY
218 if (td->o.gtod_cpu) {
219 log_err("fio: platform must support CPU affinity for"
220 "gettimeofday() offloading\n");
221 return 1;
222 }
223#endif
224
Jens Axboe724e4432007-09-11 20:02:05 +0200225 if (read_only && td_write(td)) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100226 log_err("fio: job <%s> has write bit set, but fio is in"
227 " read-only mode\n", td->o.name);
Jens Axboe724e4432007-09-11 20:02:05 +0200228 return 1;
229 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100230
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100231 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200232 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100233 free(o->write_iolog_file);
234 o->write_iolog_file = NULL;
Jens Axboe076efc72006-10-27 11:24:25 +0200235 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100236
Jens Axboe16b462a2006-10-30 12:35:18 +0100237 /*
238 * only really works for sequential io for now, and with 1 file
239 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100240 if (o->zone_size && td_random(td) && o->open_files == 1)
241 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100242
243 /*
244 * Reads can do overwrites, we always need to pre-create the file
245 */
246 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100247 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100248
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100249 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100250 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100251 if (!o->max_bs[DDIR_READ])
252 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
253 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100254 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100255 if (!o->max_bs[DDIR_WRITE])
256 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100257
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100258 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100259
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100260 if (!o->file_size_high)
261 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100262
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100263 if (o->norandommap && o->verify != VERIFY_NONE) {
Jens Axboebb8895e2006-10-30 15:14:48 +0100264 log_err("fio: norandommap given, verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100265 o->verify = VERIFY_NONE;
Jens Axboebb8895e2006-10-30 15:14:48 +0100266 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100267 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100268 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100269
270 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100271 * thinktime_spin must be less than thinktime
272 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100273 if (o->thinktime_spin > o->thinktime)
274 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100275
276 /*
277 * The low water mark cannot be bigger than the iodepth
278 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100279 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100280 /*
281 * syslet work around - if the workload is sequential,
282 * we want to let the queue drain all the way down to
283 * avoid seeking between async threads
284 */
285 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100286 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100287 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100288 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100289 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100290
291 /*
292 * If batch number isn't set, default to the same as iodepth
293 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100294 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
295 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100296
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100297 if (o->nr_files > td->files_index)
298 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100299
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100300 if (o->open_files > o->nr_files || !o->open_files)
301 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100302
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100303 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100304 log_err("fio: rate and rate_iops are mutually exclusive\n");
305 return 1;
306 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100307 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100308 log_err("fio: minimum rate exceeds rate\n");
309 return 1;
310 }
311
Jens Axboecf4464c2007-04-17 20:14:42 +0200312 if (!o->timeout && o->time_based) {
313 log_err("fio: time_based requires a runtime/timeout setting\n");
314 o->time_based = 0;
315 }
316
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100317 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200318 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100319
Jens Axboe79713142008-02-06 14:40:14 +0100320 if (td_rw(td) && td->o.verify != VERIFY_NONE)
321 log_info("fio: mixed read/write workload with verify. May not "
322 "work as expected, unless you pre-populated the file\n");
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100323
Jens Axboe41ccd842008-05-22 09:17:33 +0200324 if (td->o.verify != VERIFY_NONE)
325 td->o.refill_buffers = 1;
326
Jens Axboe4e991c22007-03-15 11:41:11 +0100327 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200328}
329
Jens Axboe906c8d72006-06-13 09:37:56 +0200330/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100331 * This function leaks the buffer
332 */
333static char *to_kmg(unsigned int val)
334{
335 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100336 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100337 char *p = post;
338
Jens Axboe245142f2006-11-08 12:49:21 +0100339 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100340 if (val & 1023)
341 break;
342
343 val >>= 10;
344 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100345 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100346
347 snprintf(buf, 31, "%u%c", val, *p);
348 return buf;
349}
350
Jens Axboe09629a92007-03-09 09:00:06 +0100351/* External engines are specified by "external:name.o") */
352static const char *get_engine_name(const char *str)
353{
354 char *p = strstr(str, ":");
355
356 if (!p)
357 return str;
358
359 p++;
360 strip_blank_front(&p);
361 strip_blank_end(p);
362 return p;
363}
364
Jens Axboee132cba2007-03-14 14:23:54 +0100365static int exists_and_not_file(const char *filename)
366{
367 struct stat sb;
368
369 if (lstat(filename, &sb) == -1)
370 return 0;
371
372 if (S_ISREG(sb.st_mode))
373 return 0;
374
375 return 1;
376}
377
Jens Axboef8977ee2006-11-06 14:03:03 +0100378/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100379 * Initialize the various random states we need (random io, block size ranges,
380 * read/write mix, etc).
381 */
382static int init_random_state(struct thread_data *td)
383{
384 unsigned long seeds[6];
Jens Axboe68727072007-03-15 20:49:25 +0100385 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100386
387 fd = open("/dev/urandom", O_RDONLY);
388 if (fd == -1) {
389 td_verror(td, errno, "open");
390 return 1;
391 }
392
393 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
394 td_verror(td, EIO, "read");
395 close(fd);
396 return 1;
397 }
398
399 close(fd);
400
401 os_random_seed(seeds[0], &td->bsrange_state);
402 os_random_seed(seeds[1], &td->verify_state);
403 os_random_seed(seeds[2], &td->rwmix_state);
404
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100405 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100406 os_random_seed(seeds[3], &td->next_file_state);
407
408 os_random_seed(seeds[5], &td->file_size_state);
409
410 if (!td_random(td))
411 return 0;
412
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100413 if (td->o.rand_repeatable)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100414 seeds[4] = FIO_RANDSEED * td->thread_number;
415
Jens Axboe9c60ce62007-03-15 09:14:47 +0100416 os_random_seed(seeds[4], &td->random_state);
417 return 0;
418}
419
Jens Axboe9c60ce62007-03-15 09:14:47 +0100420/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200421 * Adds a job to the list of things todo. Sanitizes the various options
422 * to make sure we don't have conflicts, and initializes various
423 * members of td.
424 */
Jens Axboe75154842006-06-01 13:56:09 +0200425static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100426{
Jens Axboe413dd452007-02-23 09:26:09 +0100427 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
428 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100429 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100430 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100431 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100432 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100433
Jens Axboeebac4652005-12-08 15:25:21 +0100434 /*
435 * the def_thread is just for options, it's not a real job
436 */
437 if (td == &def_thread)
438 return 0;
439
Jens Axboecca73aa2007-04-04 11:09:19 +0200440 /*
441 * if we are just dumping the output command line, don't add the job
442 */
443 if (dump_cmdline) {
444 put_job(td);
445 return 0;
446 }
447
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100448 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100449 td->io_ops = load_ioengine(td, engine);
450 if (!td->io_ops) {
451 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100452 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100453 }
Jens Axboedf641192006-10-12 07:55:41 +0200454
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100455 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100456 nr_thread++;
457 else
458 nr_process++;
459
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100460 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100461 td->io_ops->flags |= FIO_RAWIO;
462
Jens Axboee132cba2007-03-14 14:23:54 +0100463 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100464 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100465 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100466
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100467 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100468 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100469 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100470 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100471 sprintf(fname, "%s.%d.%d", jobname,
472 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100473 add_file(td, fname);
474 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100475 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100476 }
Jens Axboeebac4652005-12-08 15:25:21 +0100477
Jens Axboe4e991c22007-03-15 11:41:11 +0100478 if (fixup_options(td))
479 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100480
Jens Axboe07eb79d2007-04-12 13:02:38 +0200481 if (td->io_ops->flags & FIO_DISKLESSIO) {
482 struct fio_file *f;
483
484 for_each_file(td, f, i)
485 f->real_file_size = -1ULL;
486 }
487
Jens Axboecdd18ad2008-02-27 18:58:00 +0100488 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100489
Jens Axboe756867b2007-03-06 15:19:24 +0100490 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
491 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
492 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboe1e3d53a2007-03-22 19:01:48 +0100493 td->ddir_nr = td->o.ddir_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100494
Jens Axboeb3d62a72007-03-20 14:23:26 +0100495 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
496 && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100497 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100498 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100499 }
Jens Axboeebac4652005-12-08 15:25:21 +0100500
501 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100502 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100503
Jens Axboe9c60ce62007-03-15 09:14:47 +0100504 if (init_random_state(td))
505 goto err;
506
Jens Axboeebac4652005-12-08 15:25:21 +0100507 if (setup_rate(td))
508 goto err;
509
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100510 if (td->o.write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100511 setup_log(&td->ts.slat_log);
512 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100513 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100514 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100515 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100516
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100517 if (!td->o.name)
518 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200519
Jens Axboec6ae0a52006-06-12 11:04:44 +0200520 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200521 if (!job_add_num) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100522 if (!strcmp(td->io_ops->name, "cpuio")) {
523 log_info("%s: ioengine=cpu, cpuload=%u,"
524 " cpucycle=%u\n", td->o.name,
525 td->o.cpuload,
526 td->o.cpucycle);
527 } else {
Jens Axboef8977ee2006-11-06 14:03:03 +0100528 char *c1, *c2, *c3, *c4;
529
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100530 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
531 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
532 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
533 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100534
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100535 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
536 " ioengine=%s, iodepth=%u\n",
537 td->o.name, td->groupid,
538 ddir_str[td->o.td_ddir],
539 c1, c2, c3, c4,
540 td->io_ops->name,
541 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100542
543 free(c1);
544 free(c2);
545 free(c3);
546 free(c4);
547 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200548 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100549 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200550 }
Jens Axboeebac4652005-12-08 15:25:21 +0100551
552 /*
553 * recurse add identical jobs, clear numjobs and stonewall options
554 * as they don't apply to sub-jobs
555 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100556 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100557 while (--numjobs) {
558 struct thread_data *td_new = get_new_job(0, td);
559
560 if (!td_new)
561 goto err;
562
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100563 td_new->o.numjobs = 1;
564 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200565 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100566
567 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100568 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100569 td_new->files_index = 0;
570 td_new->files = NULL;
571 }
572
Jens Axboe75154842006-06-01 13:56:09 +0200573 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100574
Jens Axboe75154842006-06-01 13:56:09 +0200575 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100576 goto err;
577 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100578
Jens Axboeebac4652005-12-08 15:25:21 +0100579 return 0;
580err:
581 put_job(td);
582 return -1;
583}
584
Jens Axboe01f06b62008-02-18 20:53:47 +0100585static int skip_this_section(const char *name)
586{
587 if (!job_section)
588 return 0;
589 if (!strncmp(name, "global", 6))
590 return 0;
591
592 return strncmp(job_section, name, strlen(job_section));
593}
594
Jens Axboeebac4652005-12-08 15:25:21 +0100595static int is_empty_or_comment(char *line)
596{
597 unsigned int i;
598
599 for (i = 0; i < strlen(line); i++) {
600 if (line[i] == ';')
601 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100602 if (line[i] == '#')
603 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100604 if (!isspace(line[i]) && !iscntrl(line[i]))
605 return 0;
606 }
607
608 return 1;
609}
610
Jens Axboe313cb202006-12-21 09:50:00 +0100611/*
Jens Axboe07261982006-06-07 10:51:12 +0200612 * This is our [ini] type file parser.
613 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100614static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100615{
Jens Axboee1f36502006-10-27 10:54:08 +0200616 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100617 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100618 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100619 FILE *f;
620 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200621 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200622 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200623 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100624 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200625 char **opts;
626 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +0100627
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200628 if (!strcmp(file, "-"))
629 f = stdin;
630 else
631 f = fopen(file, "r");
632
Jens Axboeebac4652005-12-08 15:25:21 +0100633 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200634 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100635 return 1;
636 }
637
638 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200639
640 /*
641 * it's really 256 + small bit, 280 should suffice
642 */
643 name = malloc(280);
644 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100645
Jens Axboe3b8b7132008-06-10 19:46:23 +0200646 alloc_opts = 8;
647 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +0200648 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200649
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200650 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100651 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200652 /*
653 * if skip_fgets is set, we already have loaded a line we
654 * haven't handled.
655 */
656 if (!skip_fgets) {
657 p = fgets(string, 4095, f);
658 if (!p)
659 break;
660 }
gurudas paicdc7f192007-03-29 10:10:56 +0200661
Jens Axboeccf8f122007-09-27 10:48:55 +0200662 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200663 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800664 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200665
Jens Axboeebac4652005-12-08 15:25:21 +0100666 if (is_empty_or_comment(p))
667 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800668 if (sscanf(p, "[%255s]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100669 if (inside_skip)
670 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100671 log_err("fio: option <%s> outside of [] job section\n",
672 p);
Jens Axboe088b4202007-07-19 14:53:01 +0200673 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800674 }
Jens Axboeebac4652005-12-08 15:25:21 +0100675
Jens Axboe01f06b62008-02-18 20:53:47 +0100676 if (skip_this_section(name)) {
677 inside_skip = 1;
678 continue;
679 } else
680 inside_skip = 0;
681
Jens Axboeebac4652005-12-08 15:25:21 +0100682 global = !strncmp(name, "global", 6);
683
684 name[strlen(name) - 1] = '\0';
685
Jens Axboecca73aa2007-04-04 11:09:19 +0200686 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200687 if (first_sect)
688 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200689 if (!global)
690 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200691 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200692 }
693
Jens Axboeebac4652005-12-08 15:25:21 +0100694 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200695 if (!td) {
696 ret = 1;
697 break;
698 }
Jens Axboeebac4652005-12-08 15:25:21 +0100699
Jens Axboe972cfd22006-06-09 11:08:56 +0200700 /*
701 * Seperate multiple job files by a stonewall
702 */
Jens Axboef9481912006-06-09 11:37:28 +0200703 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100704 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200705 stonewall = 0;
706 }
707
Jens Axboe3b8b7132008-06-10 19:46:23 +0200708 num_opts = 0;
709 memset(opts, 0, alloc_opts * sizeof(char *));
710
Jens Axboeebac4652005-12-08 15:25:21 +0100711 while ((p = fgets(string, 4096, f)) != NULL) {
712 if (is_empty_or_comment(p))
713 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200714
Jens Axboeb6754f92006-05-30 14:29:32 +0200715 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100716
Jens Axboeccf8f122007-09-27 10:48:55 +0200717 /*
718 * new section, break out and make sure we don't
719 * fgets() a new line at the top.
720 */
721 if (p[0] == '[') {
722 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100723 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200724 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100725
Jens Axboe4ae3f762006-05-30 13:35:14 +0200726 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200727
Jens Axboe3b8b7132008-06-10 19:46:23 +0200728 if (num_opts == alloc_opts) {
729 alloc_opts <<= 1;
730 opts = realloc(opts,
731 alloc_opts * sizeof(char *));
732 }
733
734 opts[num_opts] = strdup(p);
735 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +0100736 }
Jens Axboeebac4652005-12-08 15:25:21 +0100737
Jens Axboe3b8b7132008-06-10 19:46:23 +0200738 ret = fio_options_parse(td, opts, num_opts);
739 if (!ret) {
740 if (dump_cmdline)
741 for (i = 0; i < num_opts; i++)
742 log_info("--%s ", opts[i]);
743
Jens Axboe45410ac2006-06-07 11:10:39 +0200744 ret = add_job(td, name, 0);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200745 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100746 log_err("fio: job %s dropped\n", name);
747 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200748 }
Jens Axboe3b8b7132008-06-10 19:46:23 +0200749
750 for (i = 0; i < num_opts; i++)
751 free(opts[i]);
752 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100753 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100754
Jens Axboecca73aa2007-04-04 11:09:19 +0200755 if (dump_cmdline)
756 log_info("\n");
757
Jens Axboe3b8b7132008-06-10 19:46:23 +0200758 for (i = 0; i < num_opts; i++)
759 free(opts[i]);
760
Jens Axboeebac4652005-12-08 15:25:21 +0100761 free(string);
762 free(name);
Jens Axboe25775942008-06-10 20:26:06 +0200763 free(opts);
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200764 if (f != stdin)
765 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200766 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100767}
768
769static int fill_def_thread(void)
770{
771 memset(&def_thread, 0, sizeof(def_thread));
772
Jens Axboe375b2692007-05-22 09:13:02 +0200773 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100774
775 /*
Jens Axboeee738492007-01-10 11:23:16 +0100776 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100777 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100778 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100779
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100780 def_thread.o.timeout = def_timeout;
781 def_thread.o.write_bw_log = write_bw_log;
782 def_thread.o.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100783
Jens Axboeebac4652005-12-08 15:25:21 +0100784 return 0;
785}
786
Jens Axboeebac4652005-12-08 15:25:21 +0100787static void free_shm(void)
788{
789 struct shmid_ds sbuf;
790
791 if (threads) {
Jens Axboe5e1d3062008-05-23 11:55:53 +0200792 void *tp = threads;
793
Jens Axboeebac4652005-12-08 15:25:21 +0100794 threads = NULL;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200795 file_hash_exit();
796 fio_debug_jobp = NULL;
797 shmdt(tp);
Jens Axboeebac4652005-12-08 15:25:21 +0100798 shmctl(shm_id, IPC_RMID, &sbuf);
799 }
Jens Axboe2e5cdb12008-03-01 18:52:49 +0100800
801 scleanup();
Jens Axboeebac4652005-12-08 15:25:21 +0100802}
803
Jens Axboe906c8d72006-06-13 09:37:56 +0200804/*
805 * The thread area is shared between the main process and the job
806 * threads/processes. So setup a shared memory segment that will hold
Jens Axboe380065a2008-03-01 18:56:24 +0100807 * all the job info. We use the end of the region for keeping track of
808 * open files across jobs, for file sharing.
Jens Axboe906c8d72006-06-13 09:37:56 +0200809 */
Jens Axboeebac4652005-12-08 15:25:21 +0100810static int setup_thread_area(void)
811{
Jens Axboe380065a2008-03-01 18:56:24 +0100812 void *hash;
813
Jens Axboeebac4652005-12-08 15:25:21 +0100814 /*
815 * 1024 is too much on some machines, scale max_jobs if
816 * we get a failure that looks like too large a shm segment
817 */
818 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200819 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100820
Jens Axboe380065a2008-03-01 18:56:24 +0100821 size += file_hash_size;
Jens Axboe5e1d3062008-05-23 11:55:53 +0200822 size += sizeof(unsigned int);
Jens Axboe380065a2008-03-01 18:56:24 +0100823
Jens Axboe906c8d72006-06-13 09:37:56 +0200824 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100825 if (shm_id != -1)
826 break;
827 if (errno != EINVAL) {
828 perror("shmget");
829 break;
830 }
831
832 max_jobs >>= 1;
833 } while (max_jobs);
834
835 if (shm_id == -1)
836 return 1;
837
838 threads = shmat(shm_id, NULL, 0);
839 if (threads == (void *) -1) {
840 perror("shmat");
841 return 1;
842 }
843
Jens Axboe1f809d12007-10-25 18:34:02 +0200844 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboe380065a2008-03-01 18:56:24 +0100845 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
Jens Axboe5e1d3062008-05-23 11:55:53 +0200846 fio_debug_jobp = (void *) hash + file_hash_size;
847 *fio_debug_jobp = -1;
Jens Axboe380065a2008-03-01 18:56:24 +0100848 file_hash_init(hash);
Jens Axboeebac4652005-12-08 15:25:21 +0100849 atexit(free_shm);
850 return 0;
851}
852
Jens Axboe45378b32007-12-19 13:54:38 +0100853static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +0200854{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100855 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +0100856 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboeee56ad52008-02-01 10:30:20 +0100857 printf("\t--debug=options\tEnable debug logging\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +0100858 printf("\t--output\tWrite output to file\n");
859 printf("\t--timeout\tRuntime in seconds\n");
860 printf("\t--latency-log\tGenerate per-job latency logs\n");
861 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
862 printf("\t--minimal\tMinimal (terse) output\n");
863 printf("\t--version\tPrint version info and exit\n");
864 printf("\t--help\t\tPrint this page\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100865 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
866 " them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +0200867 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +0200868 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
869 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100870 printf("\t--readonly\tTurn on safety read-only checks, preventing"
871 " writes\n");
Jens Axboe01f06b62008-02-18 20:53:47 +0100872 printf("\t--section=name\tOnly run specified section in job file\n");
Jens Axboe2b386d22008-03-26 10:32:57 +0100873 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
874 " (def 1024)\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200875}
876
Jens Axboe79e48f72008-02-01 20:37:09 +0100877#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +0100878struct debug_level debug_levels[] = {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100879 { .name = "process", .shift = FD_PROCESS, },
880 { .name = "file", .shift = FD_FILE, },
881 { .name = "io", .shift = FD_IO, },
882 { .name = "mem", .shift = FD_MEM, },
883 { .name = "blktrace", .shift = FD_BLKTRACE },
884 { .name = "verify", .shift = FD_VERIFY },
Jens Axboe84422ac2008-02-19 20:10:26 +0100885 { .name = "random", .shift = FD_RANDOM },
Jens Axboea3d741f2008-02-27 18:32:33 +0100886 { .name = "parse", .shift = FD_PARSE },
Jens Axboecd991b92008-03-07 13:19:35 +0100887 { .name = "diskutil", .shift = FD_DISKUTIL },
Jens Axboe5e1d3062008-05-23 11:55:53 +0200888 { .name = "job", .shift = FD_JOB },
Jens Axboe02444ad2008-10-07 11:33:00 +0200889 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +0100890};
891
Jens Axboec09823a2008-02-19 20:16:57 +0100892static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +0100893{
894 struct debug_level *dl;
895 char *p = (char *) string;
896 char *opt;
897 int i;
898
899 if (!strcmp(string, "?") || !strcmp(string, "help")) {
900 int i;
901
902 log_info("fio: dumping debug options:");
903 for (i = 0; debug_levels[i].name; i++) {
904 dl = &debug_levels[i];
905 log_info("%s,", dl->name);
906 }
Jens Axboebd6f78b2008-02-01 20:27:52 +0100907 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100908 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +0100909 }
910
911 while ((opt = strsep(&p, ",")) != NULL) {
912 int found = 0;
913
Jens Axboe5e1d3062008-05-23 11:55:53 +0200914 if (!strncmp(opt, "all", 3)) {
915 log_info("fio: set all debug options\n");
916 fio_debug = ~0UL;
917 continue;
918 }
919
Jens Axboeee56ad52008-02-01 10:30:20 +0100920 for (i = 0; debug_levels[i].name; i++) {
921 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +0200922 found = !strncmp(opt, dl->name, strlen(dl->name));
923 if (!found)
924 continue;
925
926 if (dl->shift == FD_JOB) {
927 opt = strchr(opt, ':');
928 if (!opt) {
929 log_err("fio: missing job number\n");
930 break;
931 }
932 opt++;
933 fio_debug_jobno = atoi(opt);
934 log_info("fio: set debug jobno %d\n",
935 fio_debug_jobno);
936 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +0100937 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +0100938 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +0100939 }
Jens Axboe5e1d3062008-05-23 11:55:53 +0200940 break;
Jens Axboeee56ad52008-02-01 10:30:20 +0100941 }
942
943 if (!found)
944 log_err("fio: debug mask %s not found\n", opt);
945 }
Jens Axboec09823a2008-02-19 20:16:57 +0100946 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +0100947}
Jens Axboe79e48f72008-02-01 20:37:09 +0100948#else
Jens Axboe69b98d42008-05-30 22:25:32 +0200949static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +0100950{
951 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100952 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +0100953}
954#endif
Jens Axboeee56ad52008-02-01 10:30:20 +0100955
Jens Axboe214e1ec2007-03-15 10:48:13 +0100956static int parse_cmd_line(int argc, char *argv[])
957{
958 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +0100959 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100960
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100961 while ((c = getopt_long_only(argc, argv, "", l_opts, &lidx)) != -1) {
Jens Axboe214e1ec2007-03-15 10:48:13 +0100962 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +0100963 case 'a':
964 smalloc_pool_size = atoi(optarg);
965 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100966 case 't':
967 def_timeout = atoi(optarg);
968 break;
969 case 'l':
970 write_lat_log = 1;
971 break;
972 case 'w':
973 write_bw_log = 1;
974 break;
975 case 'o':
976 f_out = fopen(optarg, "w+");
977 if (!f_out) {
978 perror("fopen output");
979 exit(1);
980 }
981 f_err = f_out;
982 break;
983 case 'm':
984 terse_output = 1;
985 break;
986 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +0100987 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100988 exit(0);
989 case 'c':
990 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +0200991 case 's':
992 dump_cmdline = 1;
993 break;
Jens Axboe724e4432007-09-11 20:02:05 +0200994 case 'r':
995 read_only = 1;
996 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100997 case 'v':
998 printf("%s\n", fio_version_string);
999 exit(0);
Aaron Carrolle592a062007-09-14 09:49:41 +02001000 case 'e':
1001 if (!strcmp("always", optarg))
1002 eta_print = FIO_ETA_ALWAYS;
1003 else if (!strcmp("never", optarg))
1004 eta_print = FIO_ETA_NEVER;
1005 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001006 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001007 if (set_debug(optarg))
1008 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001009 break;
Jens Axboe01f06b62008-02-18 20:53:47 +01001010 case 'x':
1011 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001012 log_err("fio: can't use global as only "
1013 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001014 do_exit++;
1015 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001016 break;
1017 }
1018 if (job_section)
1019 free(job_section);
1020 job_section = strdup(optarg);
1021 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001022 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001023 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001024 char *val = optarg;
1025
1026 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001027 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001028 if (ret) {
1029 put_job(td);
1030 return 0;
1031 }
1032 td = NULL;
1033 }
1034 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001035 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001036 int global = 0;
1037
Jens Axboe01f06b62008-02-18 20:53:47 +01001038 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001039 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001040
Jens Axboe01f06b62008-02-18 20:53:47 +01001041 if (is_section && skip_this_section(val))
1042 continue;
1043
Jens Axboe214e1ec2007-03-15 10:48:13 +01001044 td = get_new_job(global, &def_thread);
1045 if (!td)
1046 return 0;
1047 }
1048
1049 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001050 break;
1051 }
1052 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001053 do_exit++;
1054 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001055 break;
1056 }
1057 }
1058
Jens Axboec09823a2008-02-19 20:16:57 +01001059 if (do_exit)
1060 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001061
Jens Axboe214e1ec2007-03-15 10:48:13 +01001062 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001063 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001064 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe7d6a8902008-02-18 20:59:18 +01001065 if (ret)
1066 put_job(td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001067 }
1068
1069 while (optind < argc) {
1070 ini_idx++;
1071 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1072 ini_file[ini_idx - 1] = strdup(argv[optind]);
1073 optind++;
1074 }
1075
1076 return ini_idx;
1077}
1078
Jens Axboeebac4652005-12-08 15:25:21 +01001079int parse_options(int argc, char *argv[])
1080{
Jens Axboe972cfd22006-06-09 11:08:56 +02001081 int job_files, i;
1082
Jens Axboeb4692822006-10-27 13:43:22 +02001083 f_out = stdout;
1084 f_err = stderr;
1085
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001086 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001087
Jens Axboeebac4652005-12-08 15:25:21 +01001088 if (setup_thread_area())
1089 return 1;
1090 if (fill_def_thread())
1091 return 1;
1092
Jens Axboe972cfd22006-06-09 11:08:56 +02001093 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001094
Jens Axboe972cfd22006-06-09 11:08:56 +02001095 for (i = 0; i < job_files; i++) {
1096 if (fill_def_thread())
1097 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001098 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001099 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001100 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001101 }
Jens Axboeebac4652005-12-08 15:25:21 +01001102
Jens Axboe88c6ed82006-06-09 11:28:10 +02001103 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001104 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001105
1106 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001107 if (dump_cmdline)
1108 return 0;
1109
Jens Axboeb4692822006-10-27 13:43:22 +02001110 log_err("No jobs defined(s)\n");
Jens Axboe45378b32007-12-19 13:54:38 +01001111 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +02001112 return 1;
1113 }
1114
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001115 if (def_thread.o.gtod_offload) {
1116 fio_gtod_init();
1117 fio_gtod_offload = 1;
1118 fio_gtod_cpu = def_thread.o.gtod_cpu;
1119 }
1120
Jens Axboeebac4652005-12-08 15:25:21 +01001121 return 0;
1122}