blob: 4d336fa3eb6c08888a9cd7d0d2a3394d40b90479 [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 Axboe509df2b2008-02-19 20:24:10 +010022static char fio_version_string[] = "fio 1.19";
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 Axboe214e1ec2007-03-15 10:48:13 +010030struct thread_data def_thread;
31struct 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 Axboe214e1ec2007-03-15 10:48:13 +010044static int def_timeout = 0;
45static int write_lat_log = 0;
46
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;
50
Jens Axboeb4692822006-10-27 13:43:22 +020051/*
52 * Command line options. These will contain the above, plus a few
53 * extra that only pertain to fio itself and not jobs.
54 */
Jens Axboe214e1ec2007-03-15 10:48:13 +010055static struct option long_options[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020056 {
57 .name = "output",
58 .has_arg = required_argument,
59 .val = 'o',
60 },
61 {
62 .name = "timeout",
63 .has_arg = required_argument,
64 .val = 't',
65 },
66 {
67 .name = "latency-log",
68 .has_arg = required_argument,
69 .val = 'l',
70 },
71 {
72 .name = "bandwidth-log",
73 .has_arg = required_argument,
74 .val = 'b',
75 },
76 {
77 .name = "minimal",
78 .has_arg = optional_argument,
79 .val = 'm',
80 },
81 {
82 .name = "version",
83 .has_arg = no_argument,
84 .val = 'v',
85 },
86 {
Jens Axboefd28ca42007-01-09 21:20:13 +010087 .name = "help",
88 .has_arg = no_argument,
89 .val = 'h',
90 },
91 {
92 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +010093 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +010094 .val = 'c',
95 },
96 {
Jens Axboecca73aa2007-04-04 11:09:19 +020097 .name = "showcmd",
98 .has_arg = no_argument,
Jens Axboe724e4432007-09-11 20:02:05 +020099 .val = 's',
100 },
101 {
102 .name = "readonly",
103 .has_arg = no_argument,
104 .val = 'r',
Jens Axboecca73aa2007-04-04 11:09:19 +0200105 },
106 {
Aaron Carrolle592a062007-09-14 09:49:41 +0200107 .name = "eta",
108 .has_arg = required_argument,
109 .val = 'e',
110 },
111 {
Jens Axboeee56ad52008-02-01 10:30:20 +0100112 .name = "debug",
113 .has_arg = required_argument,
114 .val = 'd',
115 },
116 {
Jens Axboe01f06b62008-02-18 20:53:47 +0100117 .name = "section",
118 .has_arg = required_argument,
119 .val = 'x',
120 },
121 {
Jens Axboeb4692822006-10-27 13:43:22 +0200122 .name = NULL,
123 },
124};
125
Joel Becker9728ce32007-03-01 08:24:39 +0100126FILE *get_f_out()
127{
128 return f_out;
129}
130
131FILE *get_f_err()
132{
133 return f_err;
134}
135
Jens Axboe906c8d72006-06-13 09:37:56 +0200136/*
137 * Return a free job structure.
138 */
Jens Axboeebac4652005-12-08 15:25:21 +0100139static struct thread_data *get_new_job(int global, struct thread_data *parent)
140{
141 struct thread_data *td;
142
143 if (global)
144 return &def_thread;
145 if (thread_number >= max_jobs)
146 return NULL;
147
148 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200149 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100150
Jens Axboecade3ef2007-03-23 15:29:45 +0100151 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100152 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100153
Jens Axboeebac4652005-12-08 15:25:21 +0100154 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100155 return td;
156}
157
158static void put_job(struct thread_data *td)
159{
Jens Axboe549577a2006-10-30 12:23:41 +0100160 if (td == &def_thread)
161 return;
162
Jens Axboe16edf252007-02-10 14:59:22 +0100163 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100164 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100165
Jens Axboeebac4652005-12-08 15:25:21 +0100166 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
167 thread_number--;
168}
169
Jens Axboe127f6862007-03-15 12:09:57 +0100170static int setup_rate(struct thread_data *td)
171{
172 unsigned long nr_reads_per_msec;
173 unsigned long long rate;
174 unsigned int bs;
175
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100176 if (!td->o.rate && !td->o.rate_iops)
Jens Axboe127f6862007-03-15 12:09:57 +0100177 return 0;
178
179 if (td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100180 bs = td->o.rw_min_bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100181 else if (td_read(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100182 bs = td->o.min_bs[DDIR_READ];
Jens Axboe127f6862007-03-15 12:09:57 +0100183 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100184 bs = td->o.min_bs[DDIR_WRITE];
Jens Axboe127f6862007-03-15 12:09:57 +0100185
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100186 if (td->o.rate) {
187 rate = td->o.rate;
Jens Axboe127f6862007-03-15 12:09:57 +0100188 nr_reads_per_msec = (rate * 1024 * 1000LL) / bs;
189 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100190 nr_reads_per_msec = td->o.rate_iops * 1000UL;
Jens Axboe127f6862007-03-15 12:09:57 +0100191
192 if (!nr_reads_per_msec) {
193 log_err("rate lower than supported\n");
194 return -1;
195 }
196
197 td->rate_usec_cycle = 1000000000ULL / nr_reads_per_msec;
198 td->rate_pending_usleep = 0;
199 return 0;
200}
201
Jens Axboedad915e2006-10-27 11:10:18 +0200202/*
203 * Lazy way of fixing up options that depend on each other. We could also
204 * define option callback handlers, but this is easier.
205 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100206static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200207{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100208 struct thread_options *o = &td->o;
Jens Axboedad915e2006-10-27 11:10:18 +0200209
Jens Axboe724e4432007-09-11 20:02:05 +0200210 if (read_only && td_write(td)) {
211 log_err("fio: job <%s> has write bit set, but fio is in read-only mode\n", td->o.name);
212 return 1;
213 }
214
Jens Axboee47f7992007-03-21 14:05:39 +0100215 if (o->rwmix[DDIR_READ] + o->rwmix[DDIR_WRITE] > 100)
216 o->rwmix[DDIR_WRITE] = 100 - o->rwmix[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100217
218 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200219 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100220 free(o->write_iolog_file);
221 o->write_iolog_file = NULL;
Jens Axboe076efc72006-10-27 11:24:25 +0200222 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100223
Jens Axboe16b462a2006-10-30 12:35:18 +0100224 /*
225 * only really works for sequential io for now, and with 1 file
226 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100227 if (o->zone_size && td_random(td) && o->open_files == 1)
228 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100229
230 /*
231 * Reads can do overwrites, we always need to pre-create the file
232 */
233 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100234 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100235
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100236 if (!o->min_bs[DDIR_READ])
237 o->min_bs[DDIR_READ]= o->bs[DDIR_READ];
238 if (!o->max_bs[DDIR_READ])
239 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
240 if (!o->min_bs[DDIR_WRITE])
241 o->min_bs[DDIR_WRITE]= o->bs[DDIR_WRITE];
242 if (!o->max_bs[DDIR_WRITE])
243 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100244
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100245 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100246
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100247 if (!o->file_size_high)
248 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100249
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100250 if (o->norandommap && o->verify != VERIFY_NONE) {
Jens Axboebb8895e2006-10-30 15:14:48 +0100251 log_err("fio: norandommap given, verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100252 o->verify = VERIFY_NONE;
Jens Axboebb8895e2006-10-30 15:14:48 +0100253 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100254 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100255 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100256
257 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100258 * thinktime_spin must be less than thinktime
259 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100260 if (o->thinktime_spin > o->thinktime)
261 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100262
263 /*
264 * The low water mark cannot be bigger than the iodepth
265 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100266 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100267 /*
268 * syslet work around - if the workload is sequential,
269 * we want to let the queue drain all the way down to
270 * avoid seeking between async threads
271 */
272 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100273 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100274 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100275 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100276 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100277
278 /*
279 * If batch number isn't set, default to the same as iodepth
280 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100281 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
282 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100283
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100284 if (o->nr_files > td->files_index)
285 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100286
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100287 if (o->open_files > o->nr_files || !o->open_files)
288 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100289
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100290 if ((o->rate && o->rate_iops) || (o->ratemin && o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100291 log_err("fio: rate and rate_iops are mutually exclusive\n");
292 return 1;
293 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100294 if ((o->rate < o->ratemin) || (o->rate_iops < o->rate_iops_min)) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100295 log_err("fio: minimum rate exceeds rate\n");
296 return 1;
297 }
298
Jens Axboecf4464c2007-04-17 20:14:42 +0200299 if (!o->timeout && o->time_based) {
300 log_err("fio: time_based requires a runtime/timeout setting\n");
301 o->time_based = 0;
302 }
303
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100304 if (o->fill_device && !o->size)
305 o->size = ULONG_LONG_MAX;
Jens Axboe79713142008-02-06 14:40:14 +0100306
307 if (td_rw(td) && td->o.verify != VERIFY_NONE)
308 log_info("fio: mixed read/write workload with verify. May not "
309 "work as expected, unless you pre-populated the file\n");
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100310
Jens Axboe4e991c22007-03-15 11:41:11 +0100311 return 0;
Jens Axboee1f36502006-10-27 10:54:08 +0200312}
313
Jens Axboe906c8d72006-06-13 09:37:56 +0200314/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100315 * This function leaks the buffer
316 */
317static char *to_kmg(unsigned int val)
318{
319 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100320 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100321 char *p = post;
322
Jens Axboe245142f2006-11-08 12:49:21 +0100323 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100324 if (val & 1023)
325 break;
326
327 val >>= 10;
328 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100329 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100330
331 snprintf(buf, 31, "%u%c", val, *p);
332 return buf;
333}
334
Jens Axboe09629a92007-03-09 09:00:06 +0100335/* External engines are specified by "external:name.o") */
336static const char *get_engine_name(const char *str)
337{
338 char *p = strstr(str, ":");
339
340 if (!p)
341 return str;
342
343 p++;
344 strip_blank_front(&p);
345 strip_blank_end(p);
346 return p;
347}
348
Jens Axboee132cba2007-03-14 14:23:54 +0100349static int exists_and_not_file(const char *filename)
350{
351 struct stat sb;
352
353 if (lstat(filename, &sb) == -1)
354 return 0;
355
356 if (S_ISREG(sb.st_mode))
357 return 0;
358
359 return 1;
360}
361
Jens Axboef8977ee2006-11-06 14:03:03 +0100362/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100363 * Initialize the various random states we need (random io, block size ranges,
364 * read/write mix, etc).
365 */
366static int init_random_state(struct thread_data *td)
367{
368 unsigned long seeds[6];
Jens Axboe68727072007-03-15 20:49:25 +0100369 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100370
371 fd = open("/dev/urandom", O_RDONLY);
372 if (fd == -1) {
373 td_verror(td, errno, "open");
374 return 1;
375 }
376
377 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
378 td_verror(td, EIO, "read");
379 close(fd);
380 return 1;
381 }
382
383 close(fd);
384
385 os_random_seed(seeds[0], &td->bsrange_state);
386 os_random_seed(seeds[1], &td->verify_state);
387 os_random_seed(seeds[2], &td->rwmix_state);
388
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100389 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100390 os_random_seed(seeds[3], &td->next_file_state);
391
392 os_random_seed(seeds[5], &td->file_size_state);
393
394 if (!td_random(td))
395 return 0;
396
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100397 if (td->o.rand_repeatable)
Jens Axboe9c60ce62007-03-15 09:14:47 +0100398 seeds[4] = FIO_RANDSEED * td->thread_number;
399
Jens Axboe9c60ce62007-03-15 09:14:47 +0100400 os_random_seed(seeds[4], &td->random_state);
401 return 0;
402}
403
Jens Axboe9c60ce62007-03-15 09:14:47 +0100404/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200405 * Adds a job to the list of things todo. Sanitizes the various options
406 * to make sure we don't have conflicts, and initializes various
407 * members of td.
408 */
Jens Axboe75154842006-06-01 13:56:09 +0200409static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100410{
Jens Axboe413dd452007-02-23 09:26:09 +0100411 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
412 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100413 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100414 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100415 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100416 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100417
Jens Axboeebac4652005-12-08 15:25:21 +0100418 /*
419 * the def_thread is just for options, it's not a real job
420 */
421 if (td == &def_thread)
422 return 0;
423
Jens Axboecca73aa2007-04-04 11:09:19 +0200424 /*
425 * if we are just dumping the output command line, don't add the job
426 */
427 if (dump_cmdline) {
428 put_job(td);
429 return 0;
430 }
431
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100432 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100433 td->io_ops = load_ioengine(td, engine);
434 if (!td->io_ops) {
435 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100436 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100437 }
Jens Axboedf641192006-10-12 07:55:41 +0200438
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100439 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100440 nr_thread++;
441 else
442 nr_process++;
443
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100444 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100445 td->io_ops->flags |= FIO_RAWIO;
446
Jens Axboee132cba2007-03-14 14:23:54 +0100447 file_alloced = 0;
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100448 if (!td->o.filename && !td->files_index) {
Jens Axboee132cba2007-03-14 14:23:54 +0100449 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100450
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100451 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100452 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100453 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100454 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboee132cba2007-03-14 14:23:54 +0100455 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100456 add_file(td, fname);
457 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100458 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100459 }
Jens Axboeebac4652005-12-08 15:25:21 +0100460
Jens Axboe4e991c22007-03-15 11:41:11 +0100461 if (fixup_options(td))
462 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100463
Jens Axboe07eb79d2007-04-12 13:02:38 +0200464 if (td->io_ops->flags & FIO_DISKLESSIO) {
465 struct fio_file *f;
466
467 for_each_file(td, f, i)
468 f->real_file_size = -1ULL;
469 }
470
Jens Axboecdd18ad2008-02-27 18:58:00 +0100471 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100472
Jens Axboe756867b2007-03-06 15:19:24 +0100473 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
474 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
475 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboe1e3d53a2007-03-22 19:01:48 +0100476 td->ddir_nr = td->o.ddir_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100477
Jens Axboeb3d62a72007-03-20 14:23:26 +0100478 if ((td->o.stonewall || td->o.numjobs > 1 || td->o.new_group)
479 && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100480 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100481 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100482 }
Jens Axboeebac4652005-12-08 15:25:21 +0100483
484 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100485 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100486
Jens Axboe9c60ce62007-03-15 09:14:47 +0100487 if (init_random_state(td))
488 goto err;
489
Jens Axboeebac4652005-12-08 15:25:21 +0100490 if (setup_rate(td))
491 goto err;
492
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100493 if (td->o.write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100494 setup_log(&td->ts.slat_log);
495 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100496 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100497 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100498 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100499
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100500 if (!td->o.name)
501 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200502
Jens Axboec6ae0a52006-06-12 11:04:44 +0200503 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200504 if (!job_add_num) {
Jens Axboeba0fbe12007-03-09 14:34:23 +0100505 if (!strcmp(td->io_ops->name, "cpuio"))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100506 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->o.name, td->o.cpuload, td->o.cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100507 else {
508 char *c1, *c2, *c3, *c4;
509
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100510 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
511 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
512 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
513 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100514
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100515 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->o.name, td->groupid, ddir_str[td->o.td_ddir], c1, c2, c3, c4, td->io_ops->name, td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100516
517 free(c1);
518 free(c2);
519 free(c3);
520 free(c4);
521 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200522 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100523 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200524 }
Jens Axboeebac4652005-12-08 15:25:21 +0100525
526 /*
527 * recurse add identical jobs, clear numjobs and stonewall options
528 * as they don't apply to sub-jobs
529 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100530 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100531 while (--numjobs) {
532 struct thread_data *td_new = get_new_job(0, td);
533
534 if (!td_new)
535 goto err;
536
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100537 td_new->o.numjobs = 1;
538 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200539 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100540
541 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100542 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100543 td_new->files_index = 0;
544 td_new->files = NULL;
545 }
546
Jens Axboe75154842006-06-01 13:56:09 +0200547 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100548
Jens Axboe75154842006-06-01 13:56:09 +0200549 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100550 goto err;
551 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100552
Jens Axboeebac4652005-12-08 15:25:21 +0100553 return 0;
554err:
555 put_job(td);
556 return -1;
557}
558
Jens Axboe01f06b62008-02-18 20:53:47 +0100559static int skip_this_section(const char *name)
560{
561 if (!job_section)
562 return 0;
563 if (!strncmp(name, "global", 6))
564 return 0;
565
566 return strncmp(job_section, name, strlen(job_section));
567}
568
Jens Axboeebac4652005-12-08 15:25:21 +0100569static int is_empty_or_comment(char *line)
570{
571 unsigned int i;
572
573 for (i = 0; i < strlen(line); i++) {
574 if (line[i] == ';')
575 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100576 if (line[i] == '#')
577 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100578 if (!isspace(line[i]) && !iscntrl(line[i]))
579 return 0;
580 }
581
582 return 1;
583}
584
Jens Axboe313cb202006-12-21 09:50:00 +0100585/*
Jens Axboe07261982006-06-07 10:51:12 +0200586 * This is our [ini] type file parser.
587 */
Jens Axboe1e97cce2006-12-05 11:44:16 +0100588static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100589{
Jens Axboee1f36502006-10-27 10:54:08 +0200590 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100591 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100592 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100593 FILE *f;
594 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200595 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200596 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200597 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100598 int inside_skip = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100599
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200600 if (!strcmp(file, "-"))
601 f = stdin;
602 else
603 f = fopen(file, "r");
604
Jens Axboeebac4652005-12-08 15:25:21 +0100605 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +0200606 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +0100607 return 1;
608 }
609
610 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200611
612 /*
613 * it's really 256 + small bit, 280 should suffice
614 */
615 name = malloc(280);
616 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100617
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200618 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100619 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200620 /*
621 * if skip_fgets is set, we already have loaded a line we
622 * haven't handled.
623 */
624 if (!skip_fgets) {
625 p = fgets(string, 4095, f);
626 if (!p)
627 break;
628 }
gurudas paicdc7f192007-03-29 10:10:56 +0200629
Jens Axboeccf8f122007-09-27 10:48:55 +0200630 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200631 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800632 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200633
Jens Axboeebac4652005-12-08 15:25:21 +0100634 if (is_empty_or_comment(p))
635 continue;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800636 if (sscanf(p, "[%255s]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100637 if (inside_skip)
638 continue;
Jens Axboe088b4202007-07-19 14:53:01 +0200639 log_err("fio: option <%s> outside of [] job section\n", p);
640 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800641 }
Jens Axboeebac4652005-12-08 15:25:21 +0100642
Jens Axboe01f06b62008-02-18 20:53:47 +0100643 if (skip_this_section(name)) {
644 inside_skip = 1;
645 continue;
646 } else
647 inside_skip = 0;
648
Jens Axboeebac4652005-12-08 15:25:21 +0100649 global = !strncmp(name, "global", 6);
650
651 name[strlen(name) - 1] = '\0';
652
Jens Axboecca73aa2007-04-04 11:09:19 +0200653 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200654 if (first_sect)
655 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200656 if (!global)
657 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200658 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200659 }
660
Jens Axboeebac4652005-12-08 15:25:21 +0100661 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200662 if (!td) {
663 ret = 1;
664 break;
665 }
Jens Axboeebac4652005-12-08 15:25:21 +0100666
Jens Axboe972cfd22006-06-09 11:08:56 +0200667 /*
668 * Seperate multiple job files by a stonewall
669 */
Jens Axboef9481912006-06-09 11:37:28 +0200670 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100671 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200672 stonewall = 0;
673 }
674
Jens Axboeebac4652005-12-08 15:25:21 +0100675 while ((p = fgets(string, 4096, f)) != NULL) {
676 if (is_empty_or_comment(p))
677 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200678
Jens Axboeb6754f92006-05-30 14:29:32 +0200679 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100680
Jens Axboeccf8f122007-09-27 10:48:55 +0200681 /*
682 * new section, break out and make sure we don't
683 * fgets() a new line at the top.
684 */
685 if (p[0] == '[') {
686 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100687 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200688 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100689
Jens Axboe4ae3f762006-05-30 13:35:14 +0200690 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200691
Jens Axboe45410ac2006-06-07 11:10:39 +0200692 /*
693 * Don't break here, continue parsing options so we
694 * dump all the bad ones. Makes trial/error fixups
695 * easier on the user.
696 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100697 ret |= fio_option_parse(td, p);
Jens Axboecca73aa2007-04-04 11:09:19 +0200698 if (!ret && dump_cmdline)
699 log_info("--%s ", p);
Jens Axboeebac4652005-12-08 15:25:21 +0100700 }
Jens Axboeebac4652005-12-08 15:25:21 +0100701
Jens Axboeccf8f122007-09-27 10:48:55 +0200702 if (!ret)
Jens Axboe45410ac2006-06-07 11:10:39 +0200703 ret = add_job(td, name, 0);
Jens Axboeccf8f122007-09-27 10:48:55 +0200704 else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100705 log_err("fio: job %s dropped\n", name);
706 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200707 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100708 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100709
Jens Axboecca73aa2007-04-04 11:09:19 +0200710 if (dump_cmdline)
711 log_info("\n");
712
Jens Axboeebac4652005-12-08 15:25:21 +0100713 free(string);
714 free(name);
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200715 if (f != stdin)
716 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200717 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100718}
719
720static int fill_def_thread(void)
721{
722 memset(&def_thread, 0, sizeof(def_thread));
723
Jens Axboe375b2692007-05-22 09:13:02 +0200724 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100725
726 /*
Jens Axboeee738492007-01-10 11:23:16 +0100727 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100728 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100729 fio_fill_default_options(&def_thread);
Jens Axboeee738492007-01-10 11:23:16 +0100730
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100731 def_thread.o.timeout = def_timeout;
732 def_thread.o.write_bw_log = write_bw_log;
733 def_thread.o.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +0100734
Jens Axboeebac4652005-12-08 15:25:21 +0100735 return 0;
736}
737
Jens Axboeebac4652005-12-08 15:25:21 +0100738static void free_shm(void)
739{
740 struct shmid_ds sbuf;
741
742 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +0200743 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +0100744 threads = NULL;
745 shmctl(shm_id, IPC_RMID, &sbuf);
746 }
Jens Axboe2e5cdb12008-03-01 18:52:49 +0100747
748 scleanup();
Jens Axboeebac4652005-12-08 15:25:21 +0100749}
750
Jens Axboe906c8d72006-06-13 09:37:56 +0200751/*
752 * The thread area is shared between the main process and the job
753 * threads/processes. So setup a shared memory segment that will hold
Jens Axboe380065a2008-03-01 18:56:24 +0100754 * all the job info. We use the end of the region for keeping track of
755 * open files across jobs, for file sharing.
Jens Axboe906c8d72006-06-13 09:37:56 +0200756 */
Jens Axboeebac4652005-12-08 15:25:21 +0100757static int setup_thread_area(void)
758{
Jens Axboe380065a2008-03-01 18:56:24 +0100759 void *hash;
760
Jens Axboeebac4652005-12-08 15:25:21 +0100761 /*
762 * 1024 is too much on some machines, scale max_jobs if
763 * we get a failure that looks like too large a shm segment
764 */
765 do {
Jens Axboe906c8d72006-06-13 09:37:56 +0200766 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +0100767
Jens Axboe380065a2008-03-01 18:56:24 +0100768 size += file_hash_size;
769
Jens Axboe906c8d72006-06-13 09:37:56 +0200770 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +0100771 if (shm_id != -1)
772 break;
773 if (errno != EINVAL) {
774 perror("shmget");
775 break;
776 }
777
778 max_jobs >>= 1;
779 } while (max_jobs);
780
781 if (shm_id == -1)
782 return 1;
783
784 threads = shmat(shm_id, NULL, 0);
785 if (threads == (void *) -1) {
786 perror("shmat");
787 return 1;
788 }
789
Jens Axboe1f809d12007-10-25 18:34:02 +0200790 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboe380065a2008-03-01 18:56:24 +0100791 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
792 file_hash_init(hash);
Jens Axboeebac4652005-12-08 15:25:21 +0100793 atexit(free_shm);
794 return 0;
795}
796
Jens Axboe45378b32007-12-19 13:54:38 +0100797static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +0200798{
Jens Axboe214e1ec2007-03-15 10:48:13 +0100799 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +0100800 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboeee56ad52008-02-01 10:30:20 +0100801 printf("\t--debug=options\tEnable debug logging\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +0100802 printf("\t--output\tWrite output to file\n");
803 printf("\t--timeout\tRuntime in seconds\n");
804 printf("\t--latency-log\tGenerate per-job latency logs\n");
805 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
806 printf("\t--minimal\tMinimal (terse) output\n");
807 printf("\t--version\tPrint version info and exit\n");
808 printf("\t--help\t\tPrint this page\n");
809 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +0200810 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +0200811 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
812 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboe062c6022008-02-18 20:15:03 +0100813 printf("\t--readonly\tTurn on safety read-only checks, preventing writes\n");
Jens Axboe01f06b62008-02-18 20:53:47 +0100814 printf("\t--section=name\tOnly run specified section in job file\n");
Jens Axboeb4692822006-10-27 13:43:22 +0200815}
816
Jens Axboe79e48f72008-02-01 20:37:09 +0100817#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +0100818struct debug_level debug_levels[] = {
Jens Axboebd6f78b2008-02-01 20:27:52 +0100819 { .name = "process", .shift = FD_PROCESS, },
820 { .name = "file", .shift = FD_FILE, },
821 { .name = "io", .shift = FD_IO, },
822 { .name = "mem", .shift = FD_MEM, },
823 { .name = "blktrace", .shift = FD_BLKTRACE },
824 { .name = "verify", .shift = FD_VERIFY },
Jens Axboe84422ac2008-02-19 20:10:26 +0100825 { .name = "random", .shift = FD_RANDOM },
Jens Axboea3d741f2008-02-27 18:32:33 +0100826 { .name = "parse", .shift = FD_PARSE },
Jens Axboeee56ad52008-02-01 10:30:20 +0100827 { },
828};
829
Jens Axboec09823a2008-02-19 20:16:57 +0100830static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +0100831{
832 struct debug_level *dl;
833 char *p = (char *) string;
834 char *opt;
835 int i;
836
837 if (!strcmp(string, "?") || !strcmp(string, "help")) {
838 int i;
839
840 log_info("fio: dumping debug options:");
841 for (i = 0; debug_levels[i].name; i++) {
842 dl = &debug_levels[i];
843 log_info("%s,", dl->name);
844 }
Jens Axboebd6f78b2008-02-01 20:27:52 +0100845 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100846 return 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100847 } else if (!strcmp(string, "all")) {
848 fio_debug = ~0UL;
Jens Axboec09823a2008-02-19 20:16:57 +0100849 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +0100850 }
851
852 while ((opt = strsep(&p, ",")) != NULL) {
853 int found = 0;
854
855 for (i = 0; debug_levels[i].name; i++) {
856 dl = &debug_levels[i];
857 if (!strncmp(opt, dl->name, strlen(opt))) {
858 log_info("fio: set debug option %s\n", opt);
859 found = 1;
Jens Axboebd6f78b2008-02-01 20:27:52 +0100860 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +0100861 break;
862 }
863 }
864
865 if (!found)
866 log_err("fio: debug mask %s not found\n", opt);
867 }
Jens Axboec09823a2008-02-19 20:16:57 +0100868 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +0100869}
Jens Axboe79e48f72008-02-01 20:37:09 +0100870#else
871static void set_debug(const char *string)
872{
873 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100874 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +0100875}
876#endif
Jens Axboeee56ad52008-02-01 10:30:20 +0100877
Jens Axboe214e1ec2007-03-15 10:48:13 +0100878static int parse_cmd_line(int argc, char *argv[])
879{
880 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +0100881 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100882
883 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
884 switch (c) {
885 case 't':
886 def_timeout = atoi(optarg);
887 break;
888 case 'l':
889 write_lat_log = 1;
890 break;
891 case 'w':
892 write_bw_log = 1;
893 break;
894 case 'o':
895 f_out = fopen(optarg, "w+");
896 if (!f_out) {
897 perror("fopen output");
898 exit(1);
899 }
900 f_err = f_out;
901 break;
902 case 'm':
903 terse_output = 1;
904 break;
905 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +0100906 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100907 exit(0);
908 case 'c':
909 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +0200910 case 's':
911 dump_cmdline = 1;
912 break;
Jens Axboe724e4432007-09-11 20:02:05 +0200913 case 'r':
914 read_only = 1;
915 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100916 case 'v':
917 printf("%s\n", fio_version_string);
918 exit(0);
Aaron Carrolle592a062007-09-14 09:49:41 +0200919 case 'e':
920 if (!strcmp("always", optarg))
921 eta_print = FIO_ETA_ALWAYS;
922 else if (!strcmp("never", optarg))
923 eta_print = FIO_ETA_NEVER;
924 break;
Jens Axboeee56ad52008-02-01 10:30:20 +0100925 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +0100926 if (set_debug(optarg))
927 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +0100928 break;
Jens Axboe01f06b62008-02-18 20:53:47 +0100929 case 'x':
930 if (!strcmp(optarg, "global")) {
931 log_err("fio: can't use global as only section\n");
Jens Axboec09823a2008-02-19 20:16:57 +0100932 do_exit++;
933 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +0100934 break;
935 }
936 if (job_section)
937 free(job_section);
938 job_section = strdup(optarg);
939 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100940 case FIO_GETOPT_JOB: {
941 const char *opt = long_options[lidx].name;
942 char *val = optarg;
943
944 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100945 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100946 if (ret) {
947 put_job(td);
948 return 0;
949 }
950 td = NULL;
951 }
952 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100953 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +0200954 int global = 0;
955
Jens Axboe01f06b62008-02-18 20:53:47 +0100956 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +0200957 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100958
Jens Axboe01f06b62008-02-18 20:53:47 +0100959 if (is_section && skip_this_section(val))
960 continue;
961
Jens Axboe214e1ec2007-03-15 10:48:13 +0100962 td = get_new_job(global, &def_thread);
963 if (!td)
964 return 0;
965 }
966
967 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100968 break;
969 }
970 default:
Jens Axboec09823a2008-02-19 20:16:57 +0100971 do_exit++;
972 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +0100973 break;
974 }
975 }
976
Jens Axboec09823a2008-02-19 20:16:57 +0100977 if (do_exit)
978 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +0100979
Jens Axboe214e1ec2007-03-15 10:48:13 +0100980 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +0100981 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100982 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe7d6a8902008-02-18 20:59:18 +0100983 if (ret)
984 put_job(td);
Jens Axboe214e1ec2007-03-15 10:48:13 +0100985 }
986
987 while (optind < argc) {
988 ini_idx++;
989 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
990 ini_file[ini_idx - 1] = strdup(argv[optind]);
991 optind++;
992 }
993
994 return ini_idx;
995}
996
Jens Axboeebac4652005-12-08 15:25:21 +0100997int parse_options(int argc, char *argv[])
998{
Jens Axboe972cfd22006-06-09 11:08:56 +0200999 int job_files, i;
1000
Jens Axboeb4692822006-10-27 13:43:22 +02001001 f_out = stdout;
1002 f_err = stderr;
1003
Jens Axboe214e1ec2007-03-15 10:48:13 +01001004 fio_options_dup_and_init(long_options);
Jens Axboeb4692822006-10-27 13:43:22 +02001005
Jens Axboeebac4652005-12-08 15:25:21 +01001006 if (setup_thread_area())
1007 return 1;
1008 if (fill_def_thread())
1009 return 1;
1010
Jens Axboe972cfd22006-06-09 11:08:56 +02001011 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001012
Jens Axboe972cfd22006-06-09 11:08:56 +02001013 for (i = 0; i < job_files; i++) {
1014 if (fill_def_thread())
1015 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001016 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001017 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001018 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001019 }
Jens Axboeebac4652005-12-08 15:25:21 +01001020
Jens Axboe88c6ed82006-06-09 11:28:10 +02001021 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001022 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001023
1024 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001025 if (dump_cmdline)
1026 return 0;
1027
Jens Axboeb4692822006-10-27 13:43:22 +02001028 log_err("No jobs defined(s)\n");
Jens Axboe45378b32007-12-19 13:54:38 +01001029 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +02001030 return 1;
1031 }
1032
Jens Axboeebac4652005-12-08 15:25:21 +01001033 return 0;
1034}