blob: 088e814ee2ff445f874f5c5b6e3a20e62995b999 [file] [log] [blame]
Jens Axboe906c8d72006-06-13 09:37:56 +02001/*
Jens Axboecb2c86f2006-10-26 13:48:34 +02002 * This file contains job initialization and setup functions.
Jens Axboe906c8d72006-06-13 09:37:56 +02003 */
Jens Axboeebac4652005-12-08 15:25:21 +01004#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <ctype.h>
9#include <string.h>
10#include <errno.h>
11#include <sys/ipc.h>
12#include <sys/shm.h>
13#include <sys/types.h>
14#include <sys/stat.h>
15
16#include "fio.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020017#include "parse.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010018#include "smalloc.h"
Jens Axboe380065a2008-03-01 18:56:24 +010019#include "filehash.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020020#include "verify.h"
Jens Axboe79d16312010-03-04 12:43:20 +010021#include "profile.h"
Jens Axboe50d16972011-09-29 17:45:28 -060022#include "server.h"
Jens Axboeebac4652005-12-08 15:25:21 +010023
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040024#include "lib/getopt.h"
25
Jens Axboe723cd802011-09-16 08:44:10 +020026static char fio_version_string[] = "fio 1.58";
Jens Axboe214e1ec2007-03-15 10:48:13 +010027
Jens Axboeee738492007-01-10 11:23:16 +010028#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010029
Jens Axboe214e1ec2007-03-15 10:48:13 +010030static char **ini_file;
Jens Axboefca70352011-07-06 20:12:54 +020031static int max_jobs = FIO_MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020032static int dump_cmdline;
Jens Axboee1f36502006-10-27 10:54:08 +020033
Jens Axboebe4ecfd2008-12-08 14:10:52 +010034static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010035struct thread_data *threads = NULL;
Jens Axboee1f36502006-10-27 10:54:08 +020036
Jens Axboe214e1ec2007-03-15 10:48:13 +010037int exitall_on_terminate = 0;
38int terse_output = 0;
Aaron Carrolle592a062007-09-14 09:49:41 +020039int eta_print;
Jens Axboe214e1ec2007-03-15 10:48:13 +010040unsigned long long mlock_size = 0;
41FILE *f_out = NULL;
42FILE *f_err = NULL;
Jens Axboead0a2732011-03-11 10:16:17 +010043char **job_sections = NULL;
44int nr_job_sections = 0;
Jens Axboe07b32322010-03-05 09:48:44 +010045char *exec_profile = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -070046int warnings_fatal = 0;
Jens Axboef57a9c52011-09-09 21:01:37 +020047int terse_version = 2;
Jens Axboe50d16972011-09-29 17:45:28 -060048int is_backend = 0;
Jens Axboe132159a2011-09-30 15:01:32 -060049int is_client = 0;
50char *client;
Jens Axboeee738492007-01-10 11:23:16 +010051
Jens Axboe214e1ec2007-03-15 10:48:13 +010052int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020053int read_only = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020054
Jens Axboe5ec10ea2008-03-06 15:42:00 +010055static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010056
57static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020058
Jens Axboeee56ad52008-02-01 10:30:20 +010059unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020060unsigned int fio_debug_jobno = -1;
61unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010062
Jens Axboe4c6107f2011-01-18 05:22:22 -070063static char cmd_optstr[256];
64
Jens Axboeb4692822006-10-27 13:43:22 +020065/*
66 * Command line options. These will contain the above, plus a few
67 * extra that only pertain to fio itself and not jobs.
68 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010069static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020070 {
Jens Axboe08d2a192011-05-11 13:28:30 +020071 .name = (char *) "output",
Jens Axboeb4692822006-10-27 13:43:22 +020072 .has_arg = required_argument,
73 .val = 'o',
74 },
75 {
Jens Axboe08d2a192011-05-11 13:28:30 +020076 .name = (char *) "timeout",
Jens Axboeb4692822006-10-27 13:43:22 +020077 .has_arg = required_argument,
78 .val = 't',
79 },
80 {
Jens Axboe08d2a192011-05-11 13:28:30 +020081 .name = (char *) "latency-log",
Jens Axboeb4692822006-10-27 13:43:22 +020082 .has_arg = required_argument,
83 .val = 'l',
84 },
85 {
Jens Axboe08d2a192011-05-11 13:28:30 +020086 .name = (char *) "bandwidth-log",
Jens Axboeb4692822006-10-27 13:43:22 +020087 .has_arg = required_argument,
88 .val = 'b',
89 },
90 {
Jens Axboe08d2a192011-05-11 13:28:30 +020091 .name = (char *) "minimal",
Jens Axboeb4692822006-10-27 13:43:22 +020092 .has_arg = optional_argument,
93 .val = 'm',
94 },
95 {
Jens Axboe08d2a192011-05-11 13:28:30 +020096 .name = (char *) "version",
Jens Axboeb4692822006-10-27 13:43:22 +020097 .has_arg = no_argument,
98 .val = 'v',
99 },
100 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200101 .name = (char *) "help",
Jens Axboefd28ca42007-01-09 21:20:13 +0100102 .has_arg = no_argument,
103 .val = 'h',
104 },
105 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200106 .name = (char *) "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100107 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +0100108 .val = 'c',
109 },
110 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200111 .name = (char *) "showcmd",
Jens Axboecca73aa2007-04-04 11:09:19 +0200112 .has_arg = no_argument,
Jens Axboe724e4432007-09-11 20:02:05 +0200113 .val = 's',
114 },
115 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200116 .name = (char *) "readonly",
Jens Axboe724e4432007-09-11 20:02:05 +0200117 .has_arg = no_argument,
118 .val = 'r',
Jens Axboecca73aa2007-04-04 11:09:19 +0200119 },
120 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200121 .name = (char *) "eta",
Aaron Carrolle592a062007-09-14 09:49:41 +0200122 .has_arg = required_argument,
123 .val = 'e',
124 },
125 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200126 .name = (char *) "debug",
Jens Axboeee56ad52008-02-01 10:30:20 +0100127 .has_arg = required_argument,
128 .val = 'd',
129 },
130 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200131 .name = (char *) "section",
Jens Axboe01f06b62008-02-18 20:53:47 +0100132 .has_arg = required_argument,
133 .val = 'x',
134 },
135 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200136 .name = (char *) "alloc-size",
Jens Axboe2b386d22008-03-26 10:32:57 +0100137 .has_arg = required_argument,
138 .val = 'a',
139 },
140 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200141 .name = (char *) "profile",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100142 .has_arg = required_argument,
143 .val = 'p',
144 },
145 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200146 .name = (char *) "warnings-fatal",
Jens Axboea9523c62011-01-17 16:49:54 -0700147 .has_arg = no_argument,
148 .val = 'w',
149 },
150 {
Jens Axboefca70352011-07-06 20:12:54 +0200151 .name = (char *) "max-jobs",
152 .has_arg = required_argument,
153 .val = 'j',
154 },
155 {
Jens Axboef57a9c52011-09-09 21:01:37 +0200156 .name = (char *) "terse-version",
157 .has_arg = required_argument,
158 .val = 'V',
159 },
160 {
Jens Axboe50d16972011-09-29 17:45:28 -0600161 .name = (char *) "server",
162 .has_arg = no_argument,
163 .val = 'S',
164 },
165 {
Jens Axboe132159a2011-09-30 15:01:32 -0600166 .name = (char *) "net-port",
167 .has_arg = required_argument,
168 .val = 'P',
169 },
170 {
171 .name = (char *) "client",
172 .has_arg = required_argument,
173 .val = 'C',
174 },
175 {
Jens Axboeb4692822006-10-27 13:43:22 +0200176 .name = NULL,
177 },
178};
179
Joel Becker9728ce32007-03-01 08:24:39 +0100180FILE *get_f_out()
181{
182 return f_out;
183}
184
185FILE *get_f_err()
186{
187 return f_err;
188}
189
Jens Axboe906c8d72006-06-13 09:37:56 +0200190/*
191 * Return a free job structure.
192 */
Jens Axboeebac4652005-12-08 15:25:21 +0100193static struct thread_data *get_new_job(int global, struct thread_data *parent)
194{
195 struct thread_data *td;
196
197 if (global)
198 return &def_thread;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100199 if (thread_number >= max_jobs) {
200 log_err("error: maximum number of jobs (%d) reached.\n",
201 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100202 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100203 }
Jens Axboeebac4652005-12-08 15:25:21 +0100204
205 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200206 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100207
Jens Axboee0b0d892009-12-08 10:10:14 +0100208 td->o.uid = td->o.gid = -1U;
209
Jens Axboecade3ef2007-03-23 15:29:45 +0100210 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100211 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100212
Jens Axboe15dc1932010-03-05 10:59:06 +0100213 profile_add_hooks(td);
214
Jens Axboeebac4652005-12-08 15:25:21 +0100215 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100216 return td;
217}
218
219static void put_job(struct thread_data *td)
220{
Jens Axboe549577a2006-10-30 12:23:41 +0100221 if (td == &def_thread)
222 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600223
Jens Axboe58c55ba2010-03-09 12:20:08 +0100224 profile_td_exit(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100225
Jens Axboe16edf252007-02-10 14:59:22 +0100226 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100227 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100228
Jens Axboeebac4652005-12-08 15:25:21 +0100229 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
230 thread_number--;
231}
232
Jens Axboe581e7142009-06-09 12:47:16 +0200233static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100234{
Jens Axboe581e7142009-06-09 12:47:16 +0200235 unsigned int bs = td->o.min_bs[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100236 unsigned long long bytes_per_sec;
Jens Axboe127f6862007-03-15 12:09:57 +0100237
Jens Axboeff58fce2010-08-25 12:02:08 +0200238 assert(ddir_rw(ddir));
239
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100240 if (td->o.rate[ddir])
241 bytes_per_sec = td->o.rate[ddir];
242 else
243 bytes_per_sec = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100244
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100245 if (!bytes_per_sec) {
Jens Axboe127f6862007-03-15 12:09:57 +0100246 log_err("rate lower than supported\n");
247 return -1;
248 }
249
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100250 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec;
Jens Axboe581e7142009-06-09 12:47:16 +0200251 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100252 return 0;
253}
254
Jens Axboe581e7142009-06-09 12:47:16 +0200255static int setup_rate(struct thread_data *td)
256{
257 int ret = 0;
258
259 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
260 ret = __setup_rate(td, DDIR_READ);
261 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
262 ret |= __setup_rate(td, DDIR_WRITE);
263
264 return ret;
265}
266
Jens Axboe83472392009-02-19 21:32:12 +0100267static int fixed_block_size(struct thread_options *o)
268{
269 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
270 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
271 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
272}
273
Jens Axboedad915e2006-10-27 11:10:18 +0200274/*
275 * Lazy way of fixing up options that depend on each other. We could also
276 * define option callback handlers, but this is easier.
277 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100278static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200279{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100280 struct thread_options *o = &td->o;
Jens Axboeff217452011-01-18 22:33:02 -0700281 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200282
Jens Axboef356d012009-01-05 09:56:29 +0100283#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100284 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100285 log_info("fio: this platform does not support process shared"
286 " mutexes, forcing use of threads. Use the 'thread'"
287 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100288 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700289 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100290 }
291#endif
292
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100293 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200294 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100295 free(o->write_iolog_file);
296 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700297 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200298 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100299
Jens Axboe16b462a2006-10-30 12:35:18 +0100300 /*
301 * only really works for sequential io for now, and with 1 file
302 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100303 if (o->zone_size && td_random(td) && o->open_files == 1)
304 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100305
306 /*
307 * Reads can do overwrites, we always need to pre-create the file
308 */
309 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100310 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100311
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100312 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100313 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100314 if (!o->max_bs[DDIR_READ])
315 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
316 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100317 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100318 if (!o->max_bs[DDIR_WRITE])
319 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100320
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100321 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100322
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100323 /*
324 * For random IO, allow blockalign offset other than min_bs.
325 */
326 if (!o->ba[DDIR_READ] || !td_random(td))
327 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
328 if (!o->ba[DDIR_WRITE] || !td_random(td))
329 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
330
331 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
332 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100333 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100334 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100335 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700336 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100337 }
338
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100339 if (!o->file_size_high)
340 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100341
Jens Axboe83472392009-02-19 21:32:12 +0100342 if (o->norandommap && o->verify != VERIFY_NONE
343 && !fixed_block_size(o)) {
344 log_err("fio: norandommap given for variable block sizes, "
345 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100346 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700347 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100348 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100349 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100350 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100351
352 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100353 * thinktime_spin must be less than thinktime
354 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100355 if (o->thinktime_spin > o->thinktime)
356 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100357
358 /*
359 * The low water mark cannot be bigger than the iodepth
360 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100361 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100362 /*
363 * syslet work around - if the workload is sequential,
364 * we want to let the queue drain all the way down to
365 * avoid seeking between async threads
366 */
367 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100368 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100369 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100370 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100371 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100372
373 /*
374 * If batch number isn't set, default to the same as iodepth
375 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100376 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
377 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100378
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100379 if (o->nr_files > td->files_index)
380 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100381
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100382 if (o->open_files > o->nr_files || !o->open_files)
383 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100384
Jens Axboe581e7142009-06-09 12:47:16 +0200385 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
386 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
387 o->rate_iops_min[1]))) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100388 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700389 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100390 }
Jens Axboe581e7142009-06-09 12:47:16 +0200391 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
392 (o->rate_iops[0] < o->rate_iops_min[0]) ||
393 (o->rate_iops[1] < o->rate_iops_min[1])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100394 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700395 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100396 }
397
Jens Axboecf4464c2007-04-17 20:14:42 +0200398 if (!o->timeout && o->time_based) {
399 log_err("fio: time_based requires a runtime/timeout setting\n");
400 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700401 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200402 }
403
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100404 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200405 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100406
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100407 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700408 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700409 log_info("Multiple writers may overwrite blocks that "
410 "belong to other jobs. This can cause "
411 "verification failures.\n");
412 ret = warnings_fatal;
413 }
414
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100415 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200416 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
417 !o->verify_interval)
418 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboed9905882010-03-18 20:43:00 +0100419 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200420
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100421 if (o->pre_read) {
422 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700423 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200424 log_info("fio: cannot pre-read files with an IO engine"
425 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700426 ret = warnings_fatal;
427 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200428 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200429
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700430#ifndef FIO_HAVE_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100431 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700432 log_info("fio: this platform does not support fdatasync()"
433 " falling back to using fsync(). Use the 'fsync'"
434 " option instead of 'fdatasync' to get rid of"
435 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100436 o->fsync_blocks = o->fdatasync_blocks;
437 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700438 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700439 }
440#endif
441
Jens Axboea9523c62011-01-17 16:49:54 -0700442 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200443}
444
Jens Axboe906c8d72006-06-13 09:37:56 +0200445/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100446 * This function leaks the buffer
447 */
448static char *to_kmg(unsigned int val)
449{
450 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100451 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100452 char *p = post;
453
Jens Axboe245142f2006-11-08 12:49:21 +0100454 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100455 if (val & 1023)
456 break;
457
458 val >>= 10;
459 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100460 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100461
462 snprintf(buf, 31, "%u%c", val, *p);
463 return buf;
464}
465
Jens Axboe09629a92007-03-09 09:00:06 +0100466/* External engines are specified by "external:name.o") */
467static const char *get_engine_name(const char *str)
468{
469 char *p = strstr(str, ":");
470
471 if (!p)
472 return str;
473
474 p++;
475 strip_blank_front(&p);
476 strip_blank_end(p);
477 return p;
478}
479
Jens Axboee132cba2007-03-14 14:23:54 +0100480static int exists_and_not_file(const char *filename)
481{
482 struct stat sb;
483
484 if (lstat(filename, &sb) == -1)
485 return 0;
486
Bruce Cranecc314b2011-01-04 10:59:30 +0100487 /* \\.\ is the device namespace in Windows, where every file
488 * is a device node */
489 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100490 return 0;
491
492 return 1;
493}
494
Jens Axboe4c07ad82011-03-28 09:51:09 +0200495static void td_fill_rand_seeds_os(struct thread_data *td)
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200496{
497 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
498 os_random_seed(td->rand_seeds[1], &td->verify_state);
499 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
500
501 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
502 os_random_seed(td->rand_seeds[3], &td->next_file_state);
503
504 os_random_seed(td->rand_seeds[5], &td->file_size_state);
Jens Axboe0d29de82010-09-01 13:54:15 +0200505 os_random_seed(td->rand_seeds[6], &td->trim_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200506
507 if (!td_random(td))
508 return;
509
510 if (td->o.rand_repeatable)
511 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
512
513 os_random_seed(td->rand_seeds[4], &td->random_state);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200514}
515
516static void td_fill_rand_seeds_internal(struct thread_data *td)
517{
518 init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]);
519 init_rand_seed(&td->__verify_state, td->rand_seeds[1]);
520 init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]);
521
522 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
523 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]);
524
525 init_rand_seed(&td->__file_size_state, td->rand_seeds[5]);
526 init_rand_seed(&td->__trim_state, td->rand_seeds[6]);
527
528 if (!td_random(td))
529 return;
530
531 if (td->o.rand_repeatable)
532 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
533
Jens Axboe2615cc42011-03-28 09:35:09 +0200534 init_rand_seed(&td->__random_state, td->rand_seeds[4]);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200535}
536
Jens Axboe4c07ad82011-03-28 09:51:09 +0200537void td_fill_rand_seeds(struct thread_data *td)
538{
539 if (td->o.use_os_rand)
540 td_fill_rand_seeds_os(td);
541 else
542 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600543
544 init_rand_seed(&td->buf_state, td->rand_seeds[7]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200545}
546
Jens Axboef8977ee2006-11-06 14:03:03 +0100547/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100548 * Initialize the various random states we need (random io, block size ranges,
549 * read/write mix, etc).
550 */
551static int init_random_state(struct thread_data *td)
552{
Jens Axboe68727072007-03-15 20:49:25 +0100553 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100554
555 fd = open("/dev/urandom", O_RDONLY);
556 if (fd == -1) {
557 td_verror(td, errno, "open");
558 return 1;
559 }
560
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200561 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
562 (int) sizeof(td->rand_seeds)) {
Jens Axboe9c60ce62007-03-15 09:14:47 +0100563 td_verror(td, EIO, "read");
564 close(fd);
565 return 1;
566 }
567
568 close(fd);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200569 td_fill_rand_seeds(td);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100570 return 0;
571}
572
Jens Axboe9c60ce62007-03-15 09:14:47 +0100573/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200574 * Adds a job to the list of things todo. Sanitizes the various options
575 * to make sure we don't have conflicts, and initializes various
576 * members of td.
577 */
Jens Axboe75154842006-06-01 13:56:09 +0200578static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100579{
Jens Axboe413dd452007-02-23 09:26:09 +0100580 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
581 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100582 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100583 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100584 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100585 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100586
Jens Axboeebac4652005-12-08 15:25:21 +0100587 /*
588 * the def_thread is just for options, it's not a real job
589 */
590 if (td == &def_thread)
591 return 0;
592
Jens Axboecca73aa2007-04-04 11:09:19 +0200593 /*
594 * if we are just dumping the output command line, don't add the job
595 */
596 if (dump_cmdline) {
597 put_job(td);
598 return 0;
599 }
600
Jens Axboe58c55ba2010-03-09 12:20:08 +0100601 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +0200602 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +0100603
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100604 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100605 td->io_ops = load_ioengine(td, engine);
606 if (!td->io_ops) {
607 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100608 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100609 }
Jens Axboedf641192006-10-12 07:55:41 +0200610
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100611 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100612 nr_thread++;
613 else
614 nr_process++;
615
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100616 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100617 td->io_ops->flags |= FIO_RAWIO;
618
Jens Axboee132cba2007-03-14 14:23:54 +0100619 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100620 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100621 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100622
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100623 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100624 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100625 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100626 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100627 sprintf(fname, "%s.%d.%d", jobname,
628 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100629 add_file(td, fname);
630 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100631 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100632 }
Jens Axboeebac4652005-12-08 15:25:21 +0100633
Jens Axboe4e991c22007-03-15 11:41:11 +0100634 if (fixup_options(td))
635 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100636
Jens Axboe07eb79d2007-04-12 13:02:38 +0200637 if (td->io_ops->flags & FIO_DISKLESSIO) {
638 struct fio_file *f;
639
640 for_each_file(td, f, i)
641 f->real_file_size = -1ULL;
642 }
643
Jens Axboecdd18ad2008-02-27 18:58:00 +0100644 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100645
Yu-ju Hong83349192011-08-13 00:53:44 +0200646 td->ts.clat_percentiles = td->o.clat_percentiles;
647 if (td->o.overwrite_plist)
648 td->ts.percentile_list = td->o.percentile_list;
649 else
650 td->ts.percentile_list = NULL;
651
Jens Axboe756867b2007-03-06 15:19:24 +0100652 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
653 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
Jens Axboe02af0982010-06-24 09:59:34 +0200654 td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX;
Jens Axboe756867b2007-03-06 15:19:24 +0100655 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Adam DeBellinscdd54112010-09-28 13:22:55 +0900656 td->ddir_seq_nr = td->o.ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100657
Jens Axboe755c3182009-08-25 22:14:29 +0200658 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100659 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100660 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100661 }
Jens Axboeebac4652005-12-08 15:25:21 +0100662
663 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100664 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100665
Jens Axboe9c60ce62007-03-15 09:14:47 +0100666 if (init_random_state(td))
667 goto err;
668
Jens Axboeebac4652005-12-08 15:25:21 +0100669 if (setup_rate(td))
670 goto err;
671
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100672 if (td->o.write_lat_log) {
Jens Axboe02af0982010-06-24 09:59:34 +0200673 setup_log(&td->ts.lat_log);
Jens Axboe756867b2007-03-06 15:19:24 +0100674 setup_log(&td->ts.slat_log);
675 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100676 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100677 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100678 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100679
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100680 if (!td->o.name)
681 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200682
Jens Axboec6ae0a52006-06-12 11:04:44 +0200683 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200684 if (!job_add_num) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100685 if (!strcmp(td->io_ops->name, "cpuio")) {
686 log_info("%s: ioengine=cpu, cpuload=%u,"
687 " cpucycle=%u\n", td->o.name,
688 td->o.cpuload,
689 td->o.cpucycle);
690 } else {
Jens Axboef8977ee2006-11-06 14:03:03 +0100691 char *c1, *c2, *c3, *c4;
692
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100693 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
694 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
695 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
696 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100697
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100698 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
699 " ioengine=%s, iodepth=%u\n",
700 td->o.name, td->groupid,
701 ddir_str[td->o.td_ddir],
702 c1, c2, c3, c4,
703 td->io_ops->name,
704 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100705
706 free(c1);
707 free(c2);
708 free(c3);
709 free(c4);
710 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200711 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100712 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200713 }
Jens Axboeebac4652005-12-08 15:25:21 +0100714
715 /*
716 * recurse add identical jobs, clear numjobs and stonewall options
717 * as they don't apply to sub-jobs
718 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100719 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100720 while (--numjobs) {
721 struct thread_data *td_new = get_new_job(0, td);
722
723 if (!td_new)
724 goto err;
725
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100726 td_new->o.numjobs = 1;
727 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200728 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100729
730 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100731 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100732 td_new->files_index = 0;
Jens Axboe4e6ea2f2009-03-04 20:17:22 +0100733 td_new->files_size = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100734 td_new->files = NULL;
735 }
736
Jens Axboe75154842006-06-01 13:56:09 +0200737 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100738
Jens Axboe75154842006-06-01 13:56:09 +0200739 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100740 goto err;
741 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100742
Jens Axboeebac4652005-12-08 15:25:21 +0100743 return 0;
744err:
745 put_job(td);
746 return -1;
747}
748
Jens Axboe79d16312010-03-04 12:43:20 +0100749/*
750 * Parse as if 'o' was a command line
751 */
752void add_job_opts(const char **o)
753{
754 struct thread_data *td, *td_parent;
755 int i, in_global = 1;
756 char jobname[32];
757
758 i = 0;
759 td_parent = td = NULL;
760 while (o[i]) {
761 if (!strncmp(o[i], "name", 4)) {
762 in_global = 0;
763 if (td)
764 add_job(td, jobname, 0);
765 td = NULL;
766 sprintf(jobname, "%s", o[i] + 5);
767 }
768 if (in_global && !td_parent)
769 td_parent = get_new_job(1, &def_thread);
770 else if (!in_global && !td) {
771 if (!td_parent)
772 td_parent = &def_thread;
773 td = get_new_job(0, td_parent);
774 }
775 if (in_global)
776 fio_options_parse(td_parent, (char **) &o[i], 1);
777 else
778 fio_options_parse(td, (char **) &o[i], 1);
779 i++;
780 }
781
782 if (td)
783 add_job(td, jobname, 0);
784}
785
Jens Axboe01f06b62008-02-18 20:53:47 +0100786static int skip_this_section(const char *name)
787{
Jens Axboead0a2732011-03-11 10:16:17 +0100788 int i;
789
790 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +0100791 return 0;
792 if (!strncmp(name, "global", 6))
793 return 0;
794
Jens Axboead0a2732011-03-11 10:16:17 +0100795 for (i = 0; i < nr_job_sections; i++)
796 if (!strcmp(job_sections[i], name))
797 return 0;
798
799 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +0100800}
801
Jens Axboeebac4652005-12-08 15:25:21 +0100802static int is_empty_or_comment(char *line)
803{
804 unsigned int i;
805
806 for (i = 0; i < strlen(line); i++) {
807 if (line[i] == ';')
808 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100809 if (line[i] == '#')
810 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +0200811 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +0100812 return 0;
813 }
814
815 return 1;
816}
817
Jens Axboe313cb202006-12-21 09:50:00 +0100818/*
Jens Axboe07261982006-06-07 10:51:12 +0200819 * This is our [ini] type file parser.
820 */
Jens Axboe50d16972011-09-29 17:45:28 -0600821int parse_jobs_ini(char *file, int is_buf, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100822{
Jens Axboee1f36502006-10-27 10:54:08 +0200823 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100824 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100825 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100826 FILE *f;
827 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200828 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200829 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200830 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100831 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200832 char **opts;
833 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +0100834
Jens Axboe50d16972011-09-29 17:45:28 -0600835 if (is_buf)
836 f = NULL;
837 else {
838 if (!strcmp(file, "-"))
839 f = stdin;
840 else
841 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200842
Jens Axboe50d16972011-09-29 17:45:28 -0600843 if (!f) {
844 perror("fopen job file");
845 return 1;
846 }
Jens Axboeebac4652005-12-08 15:25:21 +0100847 }
848
849 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200850
851 /*
852 * it's really 256 + small bit, 280 should suffice
853 */
854 name = malloc(280);
855 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100856
Jens Axboe3b8b7132008-06-10 19:46:23 +0200857 alloc_opts = 8;
858 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +0200859 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200860
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200861 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100862 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200863 /*
864 * if skip_fgets is set, we already have loaded a line we
865 * haven't handled.
866 */
867 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -0600868 if (is_buf)
869 p = strsep(&file, "\n");
870 else
871 p = fgets(string, 4095, f);
Jens Axboeccf8f122007-09-27 10:48:55 +0200872 if (!p)
873 break;
874 }
gurudas paicdc7f192007-03-29 10:10:56 +0200875
Jens Axboeccf8f122007-09-27 10:48:55 +0200876 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200877 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800878 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200879
Jens Axboeebac4652005-12-08 15:25:21 +0100880 if (is_empty_or_comment(p))
881 continue;
Bruce Cran84dd1882011-05-05 08:14:09 -0600882 if (sscanf(p, "[%255[^\n]]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100883 if (inside_skip)
884 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100885 log_err("fio: option <%s> outside of [] job section\n",
886 p);
Jens Axboe088b4202007-07-19 14:53:01 +0200887 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800888 }
Jens Axboeebac4652005-12-08 15:25:21 +0100889
Jens Axboe7a4b80a2010-05-22 20:43:11 +0200890 name[strlen(name) - 1] = '\0';
891
Jens Axboe01f06b62008-02-18 20:53:47 +0100892 if (skip_this_section(name)) {
893 inside_skip = 1;
894 continue;
895 } else
896 inside_skip = 0;
897
Jens Axboeebac4652005-12-08 15:25:21 +0100898 global = !strncmp(name, "global", 6);
899
Jens Axboecca73aa2007-04-04 11:09:19 +0200900 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200901 if (first_sect)
902 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200903 if (!global)
904 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200905 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200906 }
907
Jens Axboeebac4652005-12-08 15:25:21 +0100908 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200909 if (!td) {
910 ret = 1;
911 break;
912 }
Jens Axboeebac4652005-12-08 15:25:21 +0100913
Jens Axboe972cfd22006-06-09 11:08:56 +0200914 /*
915 * Seperate multiple job files by a stonewall
916 */
Jens Axboef9481912006-06-09 11:37:28 +0200917 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100918 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200919 stonewall = 0;
920 }
921
Jens Axboe3b8b7132008-06-10 19:46:23 +0200922 num_opts = 0;
923 memset(opts, 0, alloc_opts * sizeof(char *));
924
Jens Axboe50d16972011-09-29 17:45:28 -0600925 while (1) {
926 if (is_buf)
927 p = strsep(&file, "\n");
928 else
929 p = fgets(string, 4096, f);
930 if (!p)
931 break;
932
Jens Axboeebac4652005-12-08 15:25:21 +0100933 if (is_empty_or_comment(p))
934 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200935
Jens Axboeb6754f92006-05-30 14:29:32 +0200936 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100937
Jens Axboeccf8f122007-09-27 10:48:55 +0200938 /*
939 * new section, break out and make sure we don't
940 * fgets() a new line at the top.
941 */
942 if (p[0] == '[') {
943 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100944 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200945 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100946
Jens Axboe4ae3f762006-05-30 13:35:14 +0200947 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200948
Jens Axboe3b8b7132008-06-10 19:46:23 +0200949 if (num_opts == alloc_opts) {
950 alloc_opts <<= 1;
951 opts = realloc(opts,
952 alloc_opts * sizeof(char *));
953 }
954
955 opts[num_opts] = strdup(p);
956 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +0100957 }
Jens Axboeebac4652005-12-08 15:25:21 +0100958
Jens Axboe3b8b7132008-06-10 19:46:23 +0200959 ret = fio_options_parse(td, opts, num_opts);
960 if (!ret) {
961 if (dump_cmdline)
962 for (i = 0; i < num_opts; i++)
963 log_info("--%s ", opts[i]);
964
Jens Axboe45410ac2006-06-07 11:10:39 +0200965 ret = add_job(td, name, 0);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200966 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100967 log_err("fio: job %s dropped\n", name);
968 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200969 }
Jens Axboe3b8b7132008-06-10 19:46:23 +0200970
971 for (i = 0; i < num_opts; i++)
972 free(opts[i]);
973 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100974 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100975
Jens Axboecca73aa2007-04-04 11:09:19 +0200976 if (dump_cmdline)
977 log_info("\n");
978
Jens Axboe3b8b7132008-06-10 19:46:23 +0200979 for (i = 0; i < num_opts; i++)
980 free(opts[i]);
981
Jens Axboeebac4652005-12-08 15:25:21 +0100982 free(string);
983 free(name);
Jens Axboe25775942008-06-10 20:26:06 +0200984 free(opts);
Jens Axboe50d16972011-09-29 17:45:28 -0600985 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200986 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200987 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100988}
989
990static int fill_def_thread(void)
991{
992 memset(&def_thread, 0, sizeof(def_thread));
993
Jens Axboe375b2692007-05-22 09:13:02 +0200994 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100995
996 /*
Jens Axboeee738492007-01-10 11:23:16 +0100997 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100998 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100999 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001000 return 0;
1001}
1002
Jens Axboeebac4652005-12-08 15:25:21 +01001003static void free_shm(void)
1004{
1005 struct shmid_ds sbuf;
1006
1007 if (threads) {
Jens Axboe5e1d3062008-05-23 11:55:53 +02001008 void *tp = threads;
1009
Jens Axboeebac4652005-12-08 15:25:21 +01001010 threads = NULL;
Jens Axboe5e1d3062008-05-23 11:55:53 +02001011 file_hash_exit();
1012 fio_debug_jobp = NULL;
1013 shmdt(tp);
Jens Axboeebac4652005-12-08 15:25:21 +01001014 shmctl(shm_id, IPC_RMID, &sbuf);
1015 }
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001016
1017 scleanup();
Jens Axboeebac4652005-12-08 15:25:21 +01001018}
1019
Jens Axboe906c8d72006-06-13 09:37:56 +02001020/*
1021 * The thread area is shared between the main process and the job
1022 * threads/processes. So setup a shared memory segment that will hold
Jens Axboe380065a2008-03-01 18:56:24 +01001023 * all the job info. We use the end of the region for keeping track of
1024 * open files across jobs, for file sharing.
Jens Axboe906c8d72006-06-13 09:37:56 +02001025 */
Jens Axboeebac4652005-12-08 15:25:21 +01001026static int setup_thread_area(void)
1027{
Jens Axboe380065a2008-03-01 18:56:24 +01001028 void *hash;
1029
Jens Axboeebac4652005-12-08 15:25:21 +01001030 /*
1031 * 1024 is too much on some machines, scale max_jobs if
1032 * we get a failure that looks like too large a shm segment
1033 */
1034 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001035 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001036
Jens Axboe380065a2008-03-01 18:56:24 +01001037 size += file_hash_size;
Jens Axboe5e1d3062008-05-23 11:55:53 +02001038 size += sizeof(unsigned int);
Jens Axboe380065a2008-03-01 18:56:24 +01001039
Jens Axboe906c8d72006-06-13 09:37:56 +02001040 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001041 if (shm_id != -1)
1042 break;
1043 if (errno != EINVAL) {
1044 perror("shmget");
1045 break;
1046 }
1047
1048 max_jobs >>= 1;
1049 } while (max_jobs);
1050
1051 if (shm_id == -1)
1052 return 1;
1053
1054 threads = shmat(shm_id, NULL, 0);
1055 if (threads == (void *) -1) {
1056 perror("shmat");
1057 return 1;
1058 }
1059
Jens Axboe1f809d12007-10-25 18:34:02 +02001060 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboe380065a2008-03-01 18:56:24 +01001061 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
Jens Axboe5e1d3062008-05-23 11:55:53 +02001062 fio_debug_jobp = (void *) hash + file_hash_size;
1063 *fio_debug_jobp = -1;
Jens Axboe380065a2008-03-01 18:56:24 +01001064 file_hash_init(hash);
Jens Axboeebac4652005-12-08 15:25:21 +01001065 atexit(free_shm);
1066 return 0;
1067}
1068
Jens Axboe45378b32007-12-19 13:54:38 +01001069static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02001070{
Bruce Cran43617622011-07-30 12:55:52 +02001071 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01001072 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboeee56ad52008-02-01 10:30:20 +01001073 printf("\t--debug=options\tEnable debug logging\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +01001074 printf("\t--output\tWrite output to file\n");
1075 printf("\t--timeout\tRuntime in seconds\n");
1076 printf("\t--latency-log\tGenerate per-job latency logs\n");
1077 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1078 printf("\t--minimal\tMinimal (terse) output\n");
1079 printf("\t--version\tPrint version info and exit\n");
Jens Axboef57a9c52011-09-09 21:01:37 +02001080 printf("\t--terse-version=x Terse version output format\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +01001081 printf("\t--help\t\tPrint this page\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001082 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
1083 " them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +02001084 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +02001085 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
1086 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001087 printf("\t--readonly\tTurn on safety read-only checks, preventing"
1088 " writes\n");
Jens Axboe01f06b62008-02-18 20:53:47 +01001089 printf("\t--section=name\tOnly run specified section in job file\n");
Jens Axboe2b386d22008-03-26 10:32:57 +01001090 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
1091 " (def 1024)\n");
Jens Axboea9523c62011-01-17 16:49:54 -07001092 printf("\t--warnings-fatal Fio parser warnings are fatal\n");
Jens Axboefca70352011-07-06 20:12:54 +02001093 printf("\t--max-jobs\tMaximum number of threads/processes to support\n");
Jens Axboe132159a2011-09-30 15:01:32 -06001094 printf("\t--server\tStart a backend fio server\n");
1095 printf("\t--client=hostname Talk to remove backend fio server at hostname\n");
1096 printf("\t--net-port=port\tUse specified port for client/server connection\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001097 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1098 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001099}
1100
Jens Axboe79e48f72008-02-01 20:37:09 +01001101#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001102struct debug_level debug_levels[] = {
Jens Axboebd6f78b2008-02-01 20:27:52 +01001103 { .name = "process", .shift = FD_PROCESS, },
1104 { .name = "file", .shift = FD_FILE, },
1105 { .name = "io", .shift = FD_IO, },
1106 { .name = "mem", .shift = FD_MEM, },
1107 { .name = "blktrace", .shift = FD_BLKTRACE },
1108 { .name = "verify", .shift = FD_VERIFY },
Jens Axboe84422ac2008-02-19 20:10:26 +01001109 { .name = "random", .shift = FD_RANDOM },
Jens Axboea3d741f2008-02-27 18:32:33 +01001110 { .name = "parse", .shift = FD_PARSE },
Jens Axboecd991b92008-03-07 13:19:35 +01001111 { .name = "diskutil", .shift = FD_DISKUTIL },
Jens Axboe5e1d3062008-05-23 11:55:53 +02001112 { .name = "job", .shift = FD_JOB },
Jens Axboe29adda32009-01-05 19:06:39 +01001113 { .name = "mutex", .shift = FD_MUTEX },
Jens Axboe79d16312010-03-04 12:43:20 +01001114 { .name = "profile", .shift = FD_PROFILE },
Jens Axboec223da82010-03-24 13:23:53 +01001115 { .name = "time", .shift = FD_TIME },
Jens Axboe02444ad2008-10-07 11:33:00 +02001116 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001117};
1118
Jens Axboec09823a2008-02-19 20:16:57 +01001119static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001120{
1121 struct debug_level *dl;
1122 char *p = (char *) string;
1123 char *opt;
1124 int i;
1125
1126 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001127 log_info("fio: dumping debug options:");
1128 for (i = 0; debug_levels[i].name; i++) {
1129 dl = &debug_levels[i];
1130 log_info("%s,", dl->name);
1131 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001132 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001133 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001134 }
1135
1136 while ((opt = strsep(&p, ",")) != NULL) {
1137 int found = 0;
1138
Jens Axboe5e1d3062008-05-23 11:55:53 +02001139 if (!strncmp(opt, "all", 3)) {
1140 log_info("fio: set all debug options\n");
1141 fio_debug = ~0UL;
1142 continue;
1143 }
1144
Jens Axboeee56ad52008-02-01 10:30:20 +01001145 for (i = 0; debug_levels[i].name; i++) {
1146 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001147 found = !strncmp(opt, dl->name, strlen(dl->name));
1148 if (!found)
1149 continue;
1150
1151 if (dl->shift == FD_JOB) {
1152 opt = strchr(opt, ':');
1153 if (!opt) {
1154 log_err("fio: missing job number\n");
1155 break;
1156 }
1157 opt++;
1158 fio_debug_jobno = atoi(opt);
1159 log_info("fio: set debug jobno %d\n",
1160 fio_debug_jobno);
1161 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001162 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001163 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001164 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001165 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001166 }
1167
1168 if (!found)
1169 log_err("fio: debug mask %s not found\n", opt);
1170 }
Jens Axboec09823a2008-02-19 20:16:57 +01001171 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001172}
Jens Axboe79e48f72008-02-01 20:37:09 +01001173#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001174static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001175{
1176 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001177 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001178}
1179#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001180
Jens Axboe4c6107f2011-01-18 05:22:22 -07001181static void fio_options_fill_optstring(void)
1182{
1183 char *ostr = cmd_optstr;
1184 int i, c;
1185
1186 c = i = 0;
1187 while (l_opts[i].name) {
1188 ostr[c++] = l_opts[i].val;
1189 if (l_opts[i].has_arg == required_argument)
1190 ostr[c++] = ':';
1191 else if (l_opts[i].has_arg == optional_argument) {
1192 ostr[c++] = ':';
1193 ostr[c++] = ':';
1194 }
1195 i++;
1196 }
1197 ostr[c] = '\0';
1198}
1199
Jens Axboe214e1ec2007-03-15 10:48:13 +01001200static int parse_cmd_line(int argc, char *argv[])
1201{
1202 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001203 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001204 char *ostr = cmd_optstr;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001205
Jens Axboe4c6107f2011-01-18 05:22:22 -07001206 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001207 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001208 case 'a':
1209 smalloc_pool_size = atoi(optarg);
1210 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001211 case 't':
Jens Axboeab2cabd2011-07-23 13:43:13 +02001212 def_thread.o.timeout = atoi(optarg);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001213 break;
1214 case 'l':
1215 write_lat_log = 1;
1216 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001217 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001218 write_bw_log = 1;
1219 break;
1220 case 'o':
1221 f_out = fopen(optarg, "w+");
1222 if (!f_out) {
1223 perror("fopen output");
1224 exit(1);
1225 }
1226 f_err = f_out;
1227 break;
1228 case 'm':
1229 terse_output = 1;
1230 break;
1231 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +01001232 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001233 exit(0);
1234 case 'c':
1235 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +02001236 case 's':
1237 dump_cmdline = 1;
1238 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001239 case 'r':
1240 read_only = 1;
1241 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001242 case 'v':
Jens Axboeca740f42011-01-25 13:06:16 +01001243 log_info("%s\n", fio_version_string);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001244 exit(0);
Jens Axboef57a9c52011-09-09 21:01:37 +02001245 case 'V':
1246 terse_version = atoi(optarg);
1247 if (terse_version != 2) {
1248 log_err("fio: bad terse version format\n");
1249 exit_val = 1;
1250 do_exit++;
1251 }
1252 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001253 case 'e':
1254 if (!strcmp("always", optarg))
1255 eta_print = FIO_ETA_ALWAYS;
1256 else if (!strcmp("never", optarg))
1257 eta_print = FIO_ETA_NEVER;
1258 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001259 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001260 if (set_debug(optarg))
1261 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001262 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001263 case 'x': {
1264 size_t new_size;
1265
Jens Axboe01f06b62008-02-18 20:53:47 +01001266 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001267 log_err("fio: can't use global as only "
1268 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001269 do_exit++;
1270 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001271 break;
1272 }
Jens Axboead0a2732011-03-11 10:16:17 +01001273 new_size = (nr_job_sections + 1) * sizeof(char *);
1274 job_sections = realloc(job_sections, new_size);
1275 job_sections[nr_job_sections] = strdup(optarg);
1276 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01001277 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001278 }
Jens Axboe9ac8a792009-11-13 21:23:07 +01001279 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001280 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001281 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001282 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001283 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001284 char *val = optarg;
1285
1286 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001287 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe66251552011-05-12 10:14:57 +02001288 if (ret)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001289 return 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001290 td = NULL;
1291 }
1292 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001293 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001294 int global = 0;
1295
Jens Axboe01f06b62008-02-18 20:53:47 +01001296 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001297 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001298
Jens Axboe01f06b62008-02-18 20:53:47 +01001299 if (is_section && skip_this_section(val))
1300 continue;
1301
Jens Axboe214e1ec2007-03-15 10:48:13 +01001302 td = get_new_job(global, &def_thread);
1303 if (!td)
1304 return 0;
1305 }
1306
1307 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001308 break;
1309 }
Jens Axboea9523c62011-01-17 16:49:54 -07001310 case 'w':
1311 warnings_fatal = 1;
1312 break;
Jens Axboefca70352011-07-06 20:12:54 +02001313 case 'j':
1314 max_jobs = atoi(optarg);
1315 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1316 log_err("fio: invalid max jobs: %d\n", max_jobs);
1317 do_exit++;
1318 exit_val = 1;
1319 }
1320 break;
Jens Axboe50d16972011-09-29 17:45:28 -06001321 case 'S':
Jens Axboe132159a2011-09-30 15:01:32 -06001322 if (is_client) {
1323 log_err("fio: can't be both client and server\n");
1324 do_exit++;
1325 exit_val = 1;
1326 break;
1327 }
Jens Axboe50d16972011-09-29 17:45:28 -06001328 is_backend = 1;
1329 break;
Jens Axboe132159a2011-09-30 15:01:32 -06001330 case 'P':
1331 fio_net_port = atoi(optarg);
1332 break;
1333 case 'C':
1334 if (is_backend) {
1335 log_err("fio: can't be both client and server\n");
1336 do_exit++;
1337 exit_val = 1;
1338 break;
1339 }
1340 is_client = 1;
1341 client = strdup(optarg);
1342 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001343 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001344 do_exit++;
1345 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001346 break;
1347 }
1348 }
1349
Jens Axboec09823a2008-02-19 20:16:57 +01001350 if (do_exit)
1351 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001352
Jens Axboe132159a2011-09-30 15:01:32 -06001353 if (is_client && fio_client_connect(client)) {
1354 do_exit++;
1355 exit_val = 1;
1356 return 1;
1357 }
1358
Jens Axboe50d16972011-09-29 17:45:28 -06001359 if (is_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -06001360 return fio_server();
Jens Axboe50d16972011-09-29 17:45:28 -06001361
Jens Axboe214e1ec2007-03-15 10:48:13 +01001362 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001363 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001364 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001365 }
1366
1367 while (optind < argc) {
1368 ini_idx++;
1369 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1370 ini_file[ini_idx - 1] = strdup(argv[optind]);
1371 optind++;
1372 }
1373
1374 return ini_idx;
1375}
1376
Jens Axboeebac4652005-12-08 15:25:21 +01001377int parse_options(int argc, char *argv[])
1378{
Jens Axboe972cfd22006-06-09 11:08:56 +02001379 int job_files, i;
1380
Jens Axboeb4692822006-10-27 13:43:22 +02001381 f_out = stdout;
1382 f_err = stderr;
1383
Jens Axboe4c6107f2011-01-18 05:22:22 -07001384 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001385 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001386
Jens Axboeebac4652005-12-08 15:25:21 +01001387 if (setup_thread_area())
1388 return 1;
1389 if (fill_def_thread())
1390 return 1;
1391
Jens Axboe972cfd22006-06-09 11:08:56 +02001392 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001393
Jens Axboe972cfd22006-06-09 11:08:56 +02001394 for (i = 0; i < job_files; i++) {
1395 if (fill_def_thread())
1396 return 1;
Jens Axboe132159a2011-09-30 15:01:32 -06001397 if (is_client) {
1398 if (fio_client_send_ini(ini_file[i]))
1399 return 1;
1400 } else {
1401 if (parse_jobs_ini(ini_file[i], 0, i))
1402 return 1;
1403 }
Jens Axboe88c6ed82006-06-09 11:28:10 +02001404 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001405 }
Jens Axboeebac4652005-12-08 15:25:21 +01001406
Jens Axboe88c6ed82006-06-09 11:28:10 +02001407 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001408 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001409
1410 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001411 if (dump_cmdline)
1412 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01001413 if (exec_profile)
1414 return 0;
Jens Axboe5c341e92011-09-29 18:00:35 -06001415 if (is_backend)
1416 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001417
Bruce Cran03e20d62011-01-02 20:14:54 +01001418 log_err("No jobs(s) defined\n\n");
Jens Axboe45378b32007-12-19 13:54:38 +01001419 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +02001420 return 1;
1421 }
1422
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001423 if (def_thread.o.gtod_offload) {
1424 fio_gtod_init();
1425 fio_gtod_offload = 1;
1426 fio_gtod_cpu = def_thread.o.gtod_cpu;
1427 }
1428
Jens Axboeca740f42011-01-25 13:06:16 +01001429 log_info("%s\n", fio_version_string);
Jens Axboeebac4652005-12-08 15:25:21 +01001430 return 0;
1431}