blob: 89c241f2500b0c689ceb0ac952d2d53bdf2a6543 [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 Axboea37f69b2011-10-01 12:24:21 -060049int nr_clients = 0;
Jens Axboeee738492007-01-10 11:23:16 +010050
Jens Axboe214e1ec2007-03-15 10:48:13 +010051int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020052int read_only = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020053
Jens Axboe5ec10ea2008-03-06 15:42:00 +010054static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010055
56static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020057
Jens Axboeee56ad52008-02-01 10:30:20 +010058unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020059unsigned int fio_debug_jobno = -1;
60unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010061
Jens Axboe4c6107f2011-01-18 05:22:22 -070062static char cmd_optstr[256];
63
Jens Axboeb4692822006-10-27 13:43:22 +020064/*
65 * Command line options. These will contain the above, plus a few
66 * extra that only pertain to fio itself and not jobs.
67 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010068static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020069 {
Jens Axboe08d2a192011-05-11 13:28:30 +020070 .name = (char *) "output",
Jens Axboeb4692822006-10-27 13:43:22 +020071 .has_arg = required_argument,
72 .val = 'o',
73 },
74 {
Jens Axboe08d2a192011-05-11 13:28:30 +020075 .name = (char *) "timeout",
Jens Axboeb4692822006-10-27 13:43:22 +020076 .has_arg = required_argument,
77 .val = 't',
78 },
79 {
Jens Axboe08d2a192011-05-11 13:28:30 +020080 .name = (char *) "latency-log",
Jens Axboeb4692822006-10-27 13:43:22 +020081 .has_arg = required_argument,
82 .val = 'l',
83 },
84 {
Jens Axboe08d2a192011-05-11 13:28:30 +020085 .name = (char *) "bandwidth-log",
Jens Axboeb4692822006-10-27 13:43:22 +020086 .has_arg = required_argument,
87 .val = 'b',
88 },
89 {
Jens Axboe08d2a192011-05-11 13:28:30 +020090 .name = (char *) "minimal",
Jens Axboeb4692822006-10-27 13:43:22 +020091 .has_arg = optional_argument,
92 .val = 'm',
93 },
94 {
Jens Axboe08d2a192011-05-11 13:28:30 +020095 .name = (char *) "version",
Jens Axboeb4692822006-10-27 13:43:22 +020096 .has_arg = no_argument,
97 .val = 'v',
98 },
99 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200100 .name = (char *) "help",
Jens Axboefd28ca42007-01-09 21:20:13 +0100101 .has_arg = no_argument,
102 .val = 'h',
103 },
104 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200105 .name = (char *) "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100106 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +0100107 .val = 'c',
108 },
109 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200110 .name = (char *) "showcmd",
Jens Axboecca73aa2007-04-04 11:09:19 +0200111 .has_arg = no_argument,
Jens Axboe724e4432007-09-11 20:02:05 +0200112 .val = 's',
113 },
114 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200115 .name = (char *) "readonly",
Jens Axboe724e4432007-09-11 20:02:05 +0200116 .has_arg = no_argument,
117 .val = 'r',
Jens Axboecca73aa2007-04-04 11:09:19 +0200118 },
119 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200120 .name = (char *) "eta",
Aaron Carrolle592a062007-09-14 09:49:41 +0200121 .has_arg = required_argument,
122 .val = 'e',
123 },
124 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200125 .name = (char *) "debug",
Jens Axboeee56ad52008-02-01 10:30:20 +0100126 .has_arg = required_argument,
127 .val = 'd',
128 },
129 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200130 .name = (char *) "section",
Jens Axboe01f06b62008-02-18 20:53:47 +0100131 .has_arg = required_argument,
132 .val = 'x',
133 },
134 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200135 .name = (char *) "alloc-size",
Jens Axboe2b386d22008-03-26 10:32:57 +0100136 .has_arg = required_argument,
137 .val = 'a',
138 },
139 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200140 .name = (char *) "profile",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100141 .has_arg = required_argument,
142 .val = 'p',
143 },
144 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200145 .name = (char *) "warnings-fatal",
Jens Axboea9523c62011-01-17 16:49:54 -0700146 .has_arg = no_argument,
147 .val = 'w',
148 },
149 {
Jens Axboefca70352011-07-06 20:12:54 +0200150 .name = (char *) "max-jobs",
151 .has_arg = required_argument,
152 .val = 'j',
153 },
154 {
Jens Axboef57a9c52011-09-09 21:01:37 +0200155 .name = (char *) "terse-version",
156 .has_arg = required_argument,
157 .val = 'V',
158 },
159 {
Jens Axboe50d16972011-09-29 17:45:28 -0600160 .name = (char *) "server",
161 .has_arg = no_argument,
162 .val = 'S',
163 },
164 {
Jens Axboe132159a2011-09-30 15:01:32 -0600165 .name = (char *) "net-port",
166 .has_arg = required_argument,
167 .val = 'P',
168 },
169 {
170 .name = (char *) "client",
171 .has_arg = required_argument,
172 .val = 'C',
173 },
174 {
Jens Axboeb4692822006-10-27 13:43:22 +0200175 .name = NULL,
176 },
177};
178
Jens Axboe906c8d72006-06-13 09:37:56 +0200179/*
180 * Return a free job structure.
181 */
Jens Axboeebac4652005-12-08 15:25:21 +0100182static struct thread_data *get_new_job(int global, struct thread_data *parent)
183{
184 struct thread_data *td;
185
186 if (global)
187 return &def_thread;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100188 if (thread_number >= max_jobs) {
189 log_err("error: maximum number of jobs (%d) reached.\n",
190 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100191 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100192 }
Jens Axboeebac4652005-12-08 15:25:21 +0100193
194 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200195 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100196
Jens Axboee0b0d892009-12-08 10:10:14 +0100197 td->o.uid = td->o.gid = -1U;
198
Jens Axboecade3ef2007-03-23 15:29:45 +0100199 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100200 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100201
Jens Axboe15dc1932010-03-05 10:59:06 +0100202 profile_add_hooks(td);
203
Jens Axboeebac4652005-12-08 15:25:21 +0100204 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100205 return td;
206}
207
208static void put_job(struct thread_data *td)
209{
Jens Axboe549577a2006-10-30 12:23:41 +0100210 if (td == &def_thread)
211 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600212
Jens Axboe58c55ba2010-03-09 12:20:08 +0100213 profile_td_exit(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100214
Jens Axboe16edf252007-02-10 14:59:22 +0100215 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100216 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100217
Jens Axboeebac4652005-12-08 15:25:21 +0100218 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
219 thread_number--;
220}
221
Jens Axboe581e7142009-06-09 12:47:16 +0200222static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100223{
Jens Axboe581e7142009-06-09 12:47:16 +0200224 unsigned int bs = td->o.min_bs[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100225 unsigned long long bytes_per_sec;
Jens Axboe127f6862007-03-15 12:09:57 +0100226
Jens Axboeff58fce2010-08-25 12:02:08 +0200227 assert(ddir_rw(ddir));
228
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100229 if (td->o.rate[ddir])
230 bytes_per_sec = td->o.rate[ddir];
231 else
232 bytes_per_sec = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100233
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100234 if (!bytes_per_sec) {
Jens Axboe127f6862007-03-15 12:09:57 +0100235 log_err("rate lower than supported\n");
236 return -1;
237 }
238
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100239 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec;
Jens Axboe581e7142009-06-09 12:47:16 +0200240 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100241 return 0;
242}
243
Jens Axboe581e7142009-06-09 12:47:16 +0200244static int setup_rate(struct thread_data *td)
245{
246 int ret = 0;
247
248 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
249 ret = __setup_rate(td, DDIR_READ);
250 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
251 ret |= __setup_rate(td, DDIR_WRITE);
252
253 return ret;
254}
255
Jens Axboe83472392009-02-19 21:32:12 +0100256static int fixed_block_size(struct thread_options *o)
257{
258 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
259 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
260 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
261}
262
Jens Axboedad915e2006-10-27 11:10:18 +0200263/*
264 * Lazy way of fixing up options that depend on each other. We could also
265 * define option callback handlers, but this is easier.
266 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100267static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200268{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100269 struct thread_options *o = &td->o;
Jens Axboeff217452011-01-18 22:33:02 -0700270 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200271
Jens Axboef356d012009-01-05 09:56:29 +0100272#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100273 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100274 log_info("fio: this platform does not support process shared"
275 " mutexes, forcing use of threads. Use the 'thread'"
276 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100277 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700278 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100279 }
280#endif
281
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100282 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200283 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100284 free(o->write_iolog_file);
285 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700286 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200287 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100288
Jens Axboe16b462a2006-10-30 12:35:18 +0100289 /*
290 * only really works for sequential io for now, and with 1 file
291 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100292 if (o->zone_size && td_random(td) && o->open_files == 1)
293 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100294
295 /*
296 * Reads can do overwrites, we always need to pre-create the file
297 */
298 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100299 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100300
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100301 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100302 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100303 if (!o->max_bs[DDIR_READ])
304 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
305 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100306 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100307 if (!o->max_bs[DDIR_WRITE])
308 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100309
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100310 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100311
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100312 /*
313 * For random IO, allow blockalign offset other than min_bs.
314 */
315 if (!o->ba[DDIR_READ] || !td_random(td))
316 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
317 if (!o->ba[DDIR_WRITE] || !td_random(td))
318 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
319
320 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
321 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100322 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100323 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100324 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700325 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100326 }
327
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100328 if (!o->file_size_high)
329 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100330
Jens Axboe83472392009-02-19 21:32:12 +0100331 if (o->norandommap && o->verify != VERIFY_NONE
332 && !fixed_block_size(o)) {
333 log_err("fio: norandommap given for variable block sizes, "
334 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100335 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700336 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100337 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100338 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100339 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100340
341 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100342 * thinktime_spin must be less than thinktime
343 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100344 if (o->thinktime_spin > o->thinktime)
345 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100346
347 /*
348 * The low water mark cannot be bigger than the iodepth
349 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100350 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100351 /*
352 * syslet work around - if the workload is sequential,
353 * we want to let the queue drain all the way down to
354 * avoid seeking between async threads
355 */
356 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100357 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100358 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100359 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100360 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100361
362 /*
363 * If batch number isn't set, default to the same as iodepth
364 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100365 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
366 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100367
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100368 if (o->nr_files > td->files_index)
369 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100370
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100371 if (o->open_files > o->nr_files || !o->open_files)
372 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100373
Jens Axboe581e7142009-06-09 12:47:16 +0200374 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
375 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
376 o->rate_iops_min[1]))) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100377 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700378 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100379 }
Jens Axboe581e7142009-06-09 12:47:16 +0200380 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
381 (o->rate_iops[0] < o->rate_iops_min[0]) ||
382 (o->rate_iops[1] < o->rate_iops_min[1])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100383 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700384 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100385 }
386
Jens Axboecf4464c2007-04-17 20:14:42 +0200387 if (!o->timeout && o->time_based) {
388 log_err("fio: time_based requires a runtime/timeout setting\n");
389 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700390 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200391 }
392
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100393 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200394 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100395
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100396 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700397 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700398 log_info("Multiple writers may overwrite blocks that "
399 "belong to other jobs. This can cause "
400 "verification failures.\n");
401 ret = warnings_fatal;
402 }
403
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100404 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200405 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
406 !o->verify_interval)
407 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboed9905882010-03-18 20:43:00 +0100408 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200409
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100410 if (o->pre_read) {
411 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700412 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200413 log_info("fio: cannot pre-read files with an IO engine"
414 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700415 ret = warnings_fatal;
416 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200417 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200418
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700419#ifndef FIO_HAVE_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100420 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700421 log_info("fio: this platform does not support fdatasync()"
422 " falling back to using fsync(). Use the 'fsync'"
423 " option instead of 'fdatasync' to get rid of"
424 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100425 o->fsync_blocks = o->fdatasync_blocks;
426 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700427 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700428 }
429#endif
430
Jens Axboea9523c62011-01-17 16:49:54 -0700431 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200432}
433
Jens Axboe906c8d72006-06-13 09:37:56 +0200434/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100435 * This function leaks the buffer
436 */
437static char *to_kmg(unsigned int val)
438{
439 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100440 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100441 char *p = post;
442
Jens Axboe245142f2006-11-08 12:49:21 +0100443 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100444 if (val & 1023)
445 break;
446
447 val >>= 10;
448 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100449 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100450
451 snprintf(buf, 31, "%u%c", val, *p);
452 return buf;
453}
454
Jens Axboe09629a92007-03-09 09:00:06 +0100455/* External engines are specified by "external:name.o") */
456static const char *get_engine_name(const char *str)
457{
458 char *p = strstr(str, ":");
459
460 if (!p)
461 return str;
462
463 p++;
464 strip_blank_front(&p);
465 strip_blank_end(p);
466 return p;
467}
468
Jens Axboee132cba2007-03-14 14:23:54 +0100469static int exists_and_not_file(const char *filename)
470{
471 struct stat sb;
472
473 if (lstat(filename, &sb) == -1)
474 return 0;
475
Bruce Cranecc314b2011-01-04 10:59:30 +0100476 /* \\.\ is the device namespace in Windows, where every file
477 * is a device node */
478 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100479 return 0;
480
481 return 1;
482}
483
Jens Axboe4c07ad82011-03-28 09:51:09 +0200484static void td_fill_rand_seeds_os(struct thread_data *td)
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200485{
486 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
487 os_random_seed(td->rand_seeds[1], &td->verify_state);
488 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
489
490 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
491 os_random_seed(td->rand_seeds[3], &td->next_file_state);
492
493 os_random_seed(td->rand_seeds[5], &td->file_size_state);
Jens Axboe0d29de82010-09-01 13:54:15 +0200494 os_random_seed(td->rand_seeds[6], &td->trim_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200495
496 if (!td_random(td))
497 return;
498
499 if (td->o.rand_repeatable)
500 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
501
502 os_random_seed(td->rand_seeds[4], &td->random_state);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200503}
504
505static void td_fill_rand_seeds_internal(struct thread_data *td)
506{
507 init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]);
508 init_rand_seed(&td->__verify_state, td->rand_seeds[1]);
509 init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]);
510
511 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
512 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]);
513
514 init_rand_seed(&td->__file_size_state, td->rand_seeds[5]);
515 init_rand_seed(&td->__trim_state, td->rand_seeds[6]);
516
517 if (!td_random(td))
518 return;
519
520 if (td->o.rand_repeatable)
521 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
522
Jens Axboe2615cc42011-03-28 09:35:09 +0200523 init_rand_seed(&td->__random_state, td->rand_seeds[4]);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200524}
525
Jens Axboe4c07ad82011-03-28 09:51:09 +0200526void td_fill_rand_seeds(struct thread_data *td)
527{
528 if (td->o.use_os_rand)
529 td_fill_rand_seeds_os(td);
530 else
531 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600532
533 init_rand_seed(&td->buf_state, td->rand_seeds[7]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200534}
535
Jens Axboef8977ee2006-11-06 14:03:03 +0100536/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100537 * Initialize the various random states we need (random io, block size ranges,
538 * read/write mix, etc).
539 */
540static int init_random_state(struct thread_data *td)
541{
Jens Axboe68727072007-03-15 20:49:25 +0100542 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100543
544 fd = open("/dev/urandom", O_RDONLY);
545 if (fd == -1) {
546 td_verror(td, errno, "open");
547 return 1;
548 }
549
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200550 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
551 (int) sizeof(td->rand_seeds)) {
Jens Axboe9c60ce62007-03-15 09:14:47 +0100552 td_verror(td, EIO, "read");
553 close(fd);
554 return 1;
555 }
556
557 close(fd);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200558 td_fill_rand_seeds(td);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100559 return 0;
560}
561
Jens Axboe9c60ce62007-03-15 09:14:47 +0100562/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200563 * Adds a job to the list of things todo. Sanitizes the various options
564 * to make sure we don't have conflicts, and initializes various
565 * members of td.
566 */
Jens Axboe75154842006-06-01 13:56:09 +0200567static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100568{
Jens Axboe413dd452007-02-23 09:26:09 +0100569 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
570 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100571 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100572 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100573 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100574 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100575
Jens Axboeebac4652005-12-08 15:25:21 +0100576 /*
577 * the def_thread is just for options, it's not a real job
578 */
579 if (td == &def_thread)
580 return 0;
581
Jens Axboecca73aa2007-04-04 11:09:19 +0200582 /*
583 * if we are just dumping the output command line, don't add the job
584 */
585 if (dump_cmdline) {
586 put_job(td);
587 return 0;
588 }
589
Jens Axboe58c55ba2010-03-09 12:20:08 +0100590 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +0200591 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +0100592
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100593 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100594 td->io_ops = load_ioengine(td, engine);
595 if (!td->io_ops) {
596 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100597 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100598 }
Jens Axboedf641192006-10-12 07:55:41 +0200599
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100600 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100601 nr_thread++;
602 else
603 nr_process++;
604
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100605 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100606 td->io_ops->flags |= FIO_RAWIO;
607
Jens Axboee132cba2007-03-14 14:23:54 +0100608 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100609 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100610 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100611
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100612 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100613 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100614 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100615 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100616 sprintf(fname, "%s.%d.%d", jobname,
617 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100618 add_file(td, fname);
619 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100620 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100621 }
Jens Axboeebac4652005-12-08 15:25:21 +0100622
Jens Axboe4e991c22007-03-15 11:41:11 +0100623 if (fixup_options(td))
624 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100625
Jens Axboe07eb79d2007-04-12 13:02:38 +0200626 if (td->io_ops->flags & FIO_DISKLESSIO) {
627 struct fio_file *f;
628
629 for_each_file(td, f, i)
630 f->real_file_size = -1ULL;
631 }
632
Jens Axboecdd18ad2008-02-27 18:58:00 +0100633 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100634
Yu-ju Hong83349192011-08-13 00:53:44 +0200635 td->ts.clat_percentiles = td->o.clat_percentiles;
636 if (td->o.overwrite_plist)
637 td->ts.percentile_list = td->o.percentile_list;
638 else
639 td->ts.percentile_list = NULL;
640
Jens Axboe756867b2007-03-06 15:19:24 +0100641 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
642 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
Jens Axboe02af0982010-06-24 09:59:34 +0200643 td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX;
Jens Axboe756867b2007-03-06 15:19:24 +0100644 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Adam DeBellinscdd54112010-09-28 13:22:55 +0900645 td->ddir_seq_nr = td->o.ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100646
Jens Axboe755c3182009-08-25 22:14:29 +0200647 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100648 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100649 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100650 }
Jens Axboeebac4652005-12-08 15:25:21 +0100651
652 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100653 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100654
Jens Axboe9c60ce62007-03-15 09:14:47 +0100655 if (init_random_state(td))
656 goto err;
657
Jens Axboeebac4652005-12-08 15:25:21 +0100658 if (setup_rate(td))
659 goto err;
660
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100661 if (td->o.write_lat_log) {
Jens Axboe02af0982010-06-24 09:59:34 +0200662 setup_log(&td->ts.lat_log);
Jens Axboe756867b2007-03-06 15:19:24 +0100663 setup_log(&td->ts.slat_log);
664 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100665 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100666 if (td->o.write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100667 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100668
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100669 if (!td->o.name)
670 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200671
Jens Axboec6ae0a52006-06-12 11:04:44 +0200672 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200673 if (!job_add_num) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100674 if (!strcmp(td->io_ops->name, "cpuio")) {
675 log_info("%s: ioengine=cpu, cpuload=%u,"
676 " cpucycle=%u\n", td->o.name,
677 td->o.cpuload,
678 td->o.cpucycle);
679 } else {
Jens Axboef8977ee2006-11-06 14:03:03 +0100680 char *c1, *c2, *c3, *c4;
681
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100682 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
683 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
684 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
685 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100686
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100687 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
688 " ioengine=%s, iodepth=%u\n",
689 td->o.name, td->groupid,
690 ddir_str[td->o.td_ddir],
691 c1, c2, c3, c4,
692 td->io_ops->name,
693 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100694
695 free(c1);
696 free(c2);
697 free(c3);
698 free(c4);
699 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200700 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100701 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200702 }
Jens Axboeebac4652005-12-08 15:25:21 +0100703
704 /*
705 * recurse add identical jobs, clear numjobs and stonewall options
706 * as they don't apply to sub-jobs
707 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100708 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100709 while (--numjobs) {
710 struct thread_data *td_new = get_new_job(0, td);
711
712 if (!td_new)
713 goto err;
714
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100715 td_new->o.numjobs = 1;
716 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200717 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100718
719 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100720 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100721 td_new->files_index = 0;
Jens Axboe4e6ea2f2009-03-04 20:17:22 +0100722 td_new->files_size = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100723 td_new->files = NULL;
724 }
725
Jens Axboe75154842006-06-01 13:56:09 +0200726 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100727
Jens Axboe75154842006-06-01 13:56:09 +0200728 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100729 goto err;
730 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100731
Jens Axboeebac4652005-12-08 15:25:21 +0100732 return 0;
733err:
734 put_job(td);
735 return -1;
736}
737
Jens Axboe79d16312010-03-04 12:43:20 +0100738/*
739 * Parse as if 'o' was a command line
740 */
741void add_job_opts(const char **o)
742{
743 struct thread_data *td, *td_parent;
744 int i, in_global = 1;
745 char jobname[32];
746
747 i = 0;
748 td_parent = td = NULL;
749 while (o[i]) {
750 if (!strncmp(o[i], "name", 4)) {
751 in_global = 0;
752 if (td)
753 add_job(td, jobname, 0);
754 td = NULL;
755 sprintf(jobname, "%s", o[i] + 5);
756 }
757 if (in_global && !td_parent)
758 td_parent = get_new_job(1, &def_thread);
759 else if (!in_global && !td) {
760 if (!td_parent)
761 td_parent = &def_thread;
762 td = get_new_job(0, td_parent);
763 }
764 if (in_global)
765 fio_options_parse(td_parent, (char **) &o[i], 1);
766 else
767 fio_options_parse(td, (char **) &o[i], 1);
768 i++;
769 }
770
771 if (td)
772 add_job(td, jobname, 0);
773}
774
Jens Axboe01f06b62008-02-18 20:53:47 +0100775static int skip_this_section(const char *name)
776{
Jens Axboead0a2732011-03-11 10:16:17 +0100777 int i;
778
779 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +0100780 return 0;
781 if (!strncmp(name, "global", 6))
782 return 0;
783
Jens Axboead0a2732011-03-11 10:16:17 +0100784 for (i = 0; i < nr_job_sections; i++)
785 if (!strcmp(job_sections[i], name))
786 return 0;
787
788 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +0100789}
790
Jens Axboeebac4652005-12-08 15:25:21 +0100791static int is_empty_or_comment(char *line)
792{
793 unsigned int i;
794
795 for (i = 0; i < strlen(line); i++) {
796 if (line[i] == ';')
797 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100798 if (line[i] == '#')
799 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +0200800 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +0100801 return 0;
802 }
803
804 return 1;
805}
806
Jens Axboe313cb202006-12-21 09:50:00 +0100807/*
Jens Axboe07261982006-06-07 10:51:12 +0200808 * This is our [ini] type file parser.
809 */
Jens Axboe50d16972011-09-29 17:45:28 -0600810int parse_jobs_ini(char *file, int is_buf, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100811{
Jens Axboee1f36502006-10-27 10:54:08 +0200812 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100813 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100814 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100815 FILE *f;
816 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200817 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200818 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200819 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100820 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200821 char **opts;
822 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +0100823
Jens Axboe50d16972011-09-29 17:45:28 -0600824 if (is_buf)
825 f = NULL;
826 else {
827 if (!strcmp(file, "-"))
828 f = stdin;
829 else
830 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200831
Jens Axboe50d16972011-09-29 17:45:28 -0600832 if (!f) {
833 perror("fopen job file");
834 return 1;
835 }
Jens Axboeebac4652005-12-08 15:25:21 +0100836 }
837
838 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200839
840 /*
841 * it's really 256 + small bit, 280 should suffice
842 */
843 name = malloc(280);
844 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100845
Jens Axboe3b8b7132008-06-10 19:46:23 +0200846 alloc_opts = 8;
847 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +0200848 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200849
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200850 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100851 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200852 /*
853 * if skip_fgets is set, we already have loaded a line we
854 * haven't handled.
855 */
856 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -0600857 if (is_buf)
858 p = strsep(&file, "\n");
859 else
860 p = fgets(string, 4095, f);
Jens Axboeccf8f122007-09-27 10:48:55 +0200861 if (!p)
862 break;
863 }
gurudas paicdc7f192007-03-29 10:10:56 +0200864
Jens Axboeccf8f122007-09-27 10:48:55 +0200865 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200866 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800867 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200868
Jens Axboeebac4652005-12-08 15:25:21 +0100869 if (is_empty_or_comment(p))
870 continue;
Bruce Cran84dd1882011-05-05 08:14:09 -0600871 if (sscanf(p, "[%255[^\n]]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100872 if (inside_skip)
873 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100874 log_err("fio: option <%s> outside of [] job section\n",
875 p);
Jens Axboe088b4202007-07-19 14:53:01 +0200876 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800877 }
Jens Axboeebac4652005-12-08 15:25:21 +0100878
Jens Axboe7a4b80a2010-05-22 20:43:11 +0200879 name[strlen(name) - 1] = '\0';
880
Jens Axboe01f06b62008-02-18 20:53:47 +0100881 if (skip_this_section(name)) {
882 inside_skip = 1;
883 continue;
884 } else
885 inside_skip = 0;
886
Jens Axboeebac4652005-12-08 15:25:21 +0100887 global = !strncmp(name, "global", 6);
888
Jens Axboecca73aa2007-04-04 11:09:19 +0200889 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200890 if (first_sect)
891 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200892 if (!global)
893 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200894 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200895 }
896
Jens Axboeebac4652005-12-08 15:25:21 +0100897 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200898 if (!td) {
899 ret = 1;
900 break;
901 }
Jens Axboeebac4652005-12-08 15:25:21 +0100902
Jens Axboe972cfd22006-06-09 11:08:56 +0200903 /*
904 * Seperate multiple job files by a stonewall
905 */
Jens Axboef9481912006-06-09 11:37:28 +0200906 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100907 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200908 stonewall = 0;
909 }
910
Jens Axboe3b8b7132008-06-10 19:46:23 +0200911 num_opts = 0;
912 memset(opts, 0, alloc_opts * sizeof(char *));
913
Jens Axboe50d16972011-09-29 17:45:28 -0600914 while (1) {
915 if (is_buf)
916 p = strsep(&file, "\n");
917 else
918 p = fgets(string, 4096, f);
919 if (!p)
920 break;
921
Jens Axboeebac4652005-12-08 15:25:21 +0100922 if (is_empty_or_comment(p))
923 continue;
Jens Axboee1f36502006-10-27 10:54:08 +0200924
Jens Axboeb6754f92006-05-30 14:29:32 +0200925 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +0100926
Jens Axboeccf8f122007-09-27 10:48:55 +0200927 /*
928 * new section, break out and make sure we don't
929 * fgets() a new line at the top.
930 */
931 if (p[0] == '[') {
932 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100933 break;
Jens Axboeccf8f122007-09-27 10:48:55 +0200934 }
Jens Axboe7c124ac2006-10-30 13:36:52 +0100935
Jens Axboe4ae3f762006-05-30 13:35:14 +0200936 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +0200937
Jens Axboe3b8b7132008-06-10 19:46:23 +0200938 if (num_opts == alloc_opts) {
939 alloc_opts <<= 1;
940 opts = realloc(opts,
941 alloc_opts * sizeof(char *));
942 }
943
944 opts[num_opts] = strdup(p);
945 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +0100946 }
Jens Axboeebac4652005-12-08 15:25:21 +0100947
Jens Axboe3b8b7132008-06-10 19:46:23 +0200948 ret = fio_options_parse(td, opts, num_opts);
949 if (!ret) {
950 if (dump_cmdline)
951 for (i = 0; i < num_opts; i++)
952 log_info("--%s ", opts[i]);
953
Jens Axboe45410ac2006-06-07 11:10:39 +0200954 ret = add_job(td, name, 0);
Jens Axboe3b8b7132008-06-10 19:46:23 +0200955 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +0100956 log_err("fio: job %s dropped\n", name);
957 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +0200958 }
Jens Axboe3b8b7132008-06-10 19:46:23 +0200959
960 for (i = 0; i < num_opts; i++)
961 free(opts[i]);
962 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100963 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +0100964
Jens Axboecca73aa2007-04-04 11:09:19 +0200965 if (dump_cmdline)
966 log_info("\n");
967
Jens Axboe3b8b7132008-06-10 19:46:23 +0200968 for (i = 0; i < num_opts; i++)
969 free(opts[i]);
970
Jens Axboeebac4652005-12-08 15:25:21 +0100971 free(string);
972 free(name);
Jens Axboe25775942008-06-10 20:26:06 +0200973 free(opts);
Jens Axboe50d16972011-09-29 17:45:28 -0600974 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200975 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +0200976 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +0100977}
978
979static int fill_def_thread(void)
980{
981 memset(&def_thread, 0, sizeof(def_thread));
982
Jens Axboe375b2692007-05-22 09:13:02 +0200983 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +0100984
985 /*
Jens Axboeee738492007-01-10 11:23:16 +0100986 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +0100987 */
Jens Axboe214e1ec2007-03-15 10:48:13 +0100988 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +0100989 return 0;
990}
991
Jens Axboeebac4652005-12-08 15:25:21 +0100992static void free_shm(void)
993{
994 struct shmid_ds sbuf;
995
996 if (threads) {
Jens Axboe5e1d3062008-05-23 11:55:53 +0200997 void *tp = threads;
998
Jens Axboeebac4652005-12-08 15:25:21 +0100999 threads = NULL;
Jens Axboe5e1d3062008-05-23 11:55:53 +02001000 file_hash_exit();
1001 fio_debug_jobp = NULL;
1002 shmdt(tp);
Jens Axboeebac4652005-12-08 15:25:21 +01001003 shmctl(shm_id, IPC_RMID, &sbuf);
1004 }
Jens Axboe2e5cdb12008-03-01 18:52:49 +01001005
1006 scleanup();
Jens Axboeebac4652005-12-08 15:25:21 +01001007}
1008
Jens Axboe906c8d72006-06-13 09:37:56 +02001009/*
1010 * The thread area is shared between the main process and the job
1011 * threads/processes. So setup a shared memory segment that will hold
Jens Axboe380065a2008-03-01 18:56:24 +01001012 * all the job info. We use the end of the region for keeping track of
1013 * open files across jobs, for file sharing.
Jens Axboe906c8d72006-06-13 09:37:56 +02001014 */
Jens Axboeebac4652005-12-08 15:25:21 +01001015static int setup_thread_area(void)
1016{
Jens Axboe380065a2008-03-01 18:56:24 +01001017 void *hash;
1018
Jens Axboeebac4652005-12-08 15:25:21 +01001019 /*
1020 * 1024 is too much on some machines, scale max_jobs if
1021 * we get a failure that looks like too large a shm segment
1022 */
1023 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001024 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001025
Jens Axboe380065a2008-03-01 18:56:24 +01001026 size += file_hash_size;
Jens Axboe5e1d3062008-05-23 11:55:53 +02001027 size += sizeof(unsigned int);
Jens Axboe380065a2008-03-01 18:56:24 +01001028
Jens Axboe906c8d72006-06-13 09:37:56 +02001029 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001030 if (shm_id != -1)
1031 break;
1032 if (errno != EINVAL) {
1033 perror("shmget");
1034 break;
1035 }
1036
1037 max_jobs >>= 1;
1038 } while (max_jobs);
1039
1040 if (shm_id == -1)
1041 return 1;
1042
1043 threads = shmat(shm_id, NULL, 0);
1044 if (threads == (void *) -1) {
1045 perror("shmat");
1046 return 1;
1047 }
1048
Jens Axboe1f809d12007-10-25 18:34:02 +02001049 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Jens Axboe380065a2008-03-01 18:56:24 +01001050 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
Jens Axboe5e1d3062008-05-23 11:55:53 +02001051 fio_debug_jobp = (void *) hash + file_hash_size;
1052 *fio_debug_jobp = -1;
Jens Axboe380065a2008-03-01 18:56:24 +01001053 file_hash_init(hash);
Jens Axboeebac4652005-12-08 15:25:21 +01001054 atexit(free_shm);
1055 return 0;
1056}
1057
Jens Axboe45378b32007-12-19 13:54:38 +01001058static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02001059{
Bruce Cran43617622011-07-30 12:55:52 +02001060 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01001061 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboeee56ad52008-02-01 10:30:20 +01001062 printf("\t--debug=options\tEnable debug logging\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +01001063 printf("\t--output\tWrite output to file\n");
1064 printf("\t--timeout\tRuntime in seconds\n");
1065 printf("\t--latency-log\tGenerate per-job latency logs\n");
1066 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1067 printf("\t--minimal\tMinimal (terse) output\n");
1068 printf("\t--version\tPrint version info and exit\n");
Jens Axboef57a9c52011-09-09 21:01:37 +02001069 printf("\t--terse-version=x Terse version output format\n");
Jens Axboe214e1ec2007-03-15 10:48:13 +01001070 printf("\t--help\t\tPrint this page\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001071 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of"
1072 " them\n");
Jens Axboecca73aa2007-04-04 11:09:19 +02001073 printf("\t--showcmd\tTurn a job file into command line options\n");
Aaron Carrolle592a062007-09-14 09:49:41 +02001074 printf("\t--eta=when\tWhen ETA estimate should be printed\n");
1075 printf("\t \tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001076 printf("\t--readonly\tTurn on safety read-only checks, preventing"
1077 " writes\n");
Jens Axboe01f06b62008-02-18 20:53:47 +01001078 printf("\t--section=name\tOnly run specified section in job file\n");
Jens Axboe2b386d22008-03-26 10:32:57 +01001079 printf("\t--alloc-size=kb\tSet smalloc pool to this size in kb"
1080 " (def 1024)\n");
Jens Axboea9523c62011-01-17 16:49:54 -07001081 printf("\t--warnings-fatal Fio parser warnings are fatal\n");
Jens Axboefca70352011-07-06 20:12:54 +02001082 printf("\t--max-jobs\tMaximum number of threads/processes to support\n");
Jens Axboe132159a2011-09-30 15:01:32 -06001083 printf("\t--server\tStart a backend fio server\n");
1084 printf("\t--client=hostname Talk to remove backend fio server at hostname\n");
1085 printf("\t--net-port=port\tUse specified port for client/server connection\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001086 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
1087 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001088}
1089
Jens Axboe79e48f72008-02-01 20:37:09 +01001090#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001091struct debug_level debug_levels[] = {
Jens Axboebd6f78b2008-02-01 20:27:52 +01001092 { .name = "process", .shift = FD_PROCESS, },
1093 { .name = "file", .shift = FD_FILE, },
1094 { .name = "io", .shift = FD_IO, },
1095 { .name = "mem", .shift = FD_MEM, },
1096 { .name = "blktrace", .shift = FD_BLKTRACE },
1097 { .name = "verify", .shift = FD_VERIFY },
Jens Axboe84422ac2008-02-19 20:10:26 +01001098 { .name = "random", .shift = FD_RANDOM },
Jens Axboea3d741f2008-02-27 18:32:33 +01001099 { .name = "parse", .shift = FD_PARSE },
Jens Axboecd991b92008-03-07 13:19:35 +01001100 { .name = "diskutil", .shift = FD_DISKUTIL },
Jens Axboe5e1d3062008-05-23 11:55:53 +02001101 { .name = "job", .shift = FD_JOB },
Jens Axboe29adda32009-01-05 19:06:39 +01001102 { .name = "mutex", .shift = FD_MUTEX },
Jens Axboe79d16312010-03-04 12:43:20 +01001103 { .name = "profile", .shift = FD_PROFILE },
Jens Axboec223da82010-03-24 13:23:53 +01001104 { .name = "time", .shift = FD_TIME },
Jens Axboeeb7976e2011-10-01 12:42:00 -06001105 { .name = "net", .shift = FD_NET },
Jens Axboe02444ad2008-10-07 11:33:00 +02001106 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001107};
1108
Jens Axboec09823a2008-02-19 20:16:57 +01001109static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001110{
1111 struct debug_level *dl;
1112 char *p = (char *) string;
1113 char *opt;
1114 int i;
1115
1116 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001117 log_info("fio: dumping debug options:");
1118 for (i = 0; debug_levels[i].name; i++) {
1119 dl = &debug_levels[i];
1120 log_info("%s,", dl->name);
1121 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001122 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001123 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001124 }
1125
1126 while ((opt = strsep(&p, ",")) != NULL) {
1127 int found = 0;
1128
Jens Axboe5e1d3062008-05-23 11:55:53 +02001129 if (!strncmp(opt, "all", 3)) {
1130 log_info("fio: set all debug options\n");
1131 fio_debug = ~0UL;
1132 continue;
1133 }
1134
Jens Axboeee56ad52008-02-01 10:30:20 +01001135 for (i = 0; debug_levels[i].name; i++) {
1136 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001137 found = !strncmp(opt, dl->name, strlen(dl->name));
1138 if (!found)
1139 continue;
1140
1141 if (dl->shift == FD_JOB) {
1142 opt = strchr(opt, ':');
1143 if (!opt) {
1144 log_err("fio: missing job number\n");
1145 break;
1146 }
1147 opt++;
1148 fio_debug_jobno = atoi(opt);
1149 log_info("fio: set debug jobno %d\n",
1150 fio_debug_jobno);
1151 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001152 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001153 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001154 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001155 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001156 }
1157
1158 if (!found)
1159 log_err("fio: debug mask %s not found\n", opt);
1160 }
Jens Axboec09823a2008-02-19 20:16:57 +01001161 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001162}
Jens Axboe79e48f72008-02-01 20:37:09 +01001163#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001164static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001165{
1166 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001167 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001168}
1169#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001170
Jens Axboe4c6107f2011-01-18 05:22:22 -07001171static void fio_options_fill_optstring(void)
1172{
1173 char *ostr = cmd_optstr;
1174 int i, c;
1175
1176 c = i = 0;
1177 while (l_opts[i].name) {
1178 ostr[c++] = l_opts[i].val;
1179 if (l_opts[i].has_arg == required_argument)
1180 ostr[c++] = ':';
1181 else if (l_opts[i].has_arg == optional_argument) {
1182 ostr[c++] = ':';
1183 ostr[c++] = ':';
1184 }
1185 i++;
1186 }
1187 ostr[c] = '\0';
1188}
1189
Jens Axboe214e1ec2007-03-15 10:48:13 +01001190static int parse_cmd_line(int argc, char *argv[])
1191{
1192 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001193 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001194 char *ostr = cmd_optstr;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001195
Jens Axboe4c6107f2011-01-18 05:22:22 -07001196 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001197 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001198 case 'a':
1199 smalloc_pool_size = atoi(optarg);
1200 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001201 case 't':
Jens Axboeab2cabd2011-07-23 13:43:13 +02001202 def_thread.o.timeout = atoi(optarg);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001203 break;
1204 case 'l':
1205 write_lat_log = 1;
1206 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001207 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001208 write_bw_log = 1;
1209 break;
1210 case 'o':
1211 f_out = fopen(optarg, "w+");
1212 if (!f_out) {
1213 perror("fopen output");
1214 exit(1);
1215 }
1216 f_err = f_out;
1217 break;
1218 case 'm':
1219 terse_output = 1;
1220 break;
1221 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +01001222 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001223 exit(0);
1224 case 'c':
1225 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +02001226 case 's':
1227 dump_cmdline = 1;
1228 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001229 case 'r':
1230 read_only = 1;
1231 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001232 case 'v':
Jens Axboeca740f42011-01-25 13:06:16 +01001233 log_info("%s\n", fio_version_string);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001234 exit(0);
Jens Axboef57a9c52011-09-09 21:01:37 +02001235 case 'V':
1236 terse_version = atoi(optarg);
1237 if (terse_version != 2) {
1238 log_err("fio: bad terse version format\n");
1239 exit_val = 1;
1240 do_exit++;
1241 }
1242 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001243 case 'e':
1244 if (!strcmp("always", optarg))
1245 eta_print = FIO_ETA_ALWAYS;
1246 else if (!strcmp("never", optarg))
1247 eta_print = FIO_ETA_NEVER;
1248 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001249 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001250 if (set_debug(optarg))
1251 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001252 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001253 case 'x': {
1254 size_t new_size;
1255
Jens Axboe01f06b62008-02-18 20:53:47 +01001256 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001257 log_err("fio: can't use global as only "
1258 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001259 do_exit++;
1260 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001261 break;
1262 }
Jens Axboead0a2732011-03-11 10:16:17 +01001263 new_size = (nr_job_sections + 1) * sizeof(char *);
1264 job_sections = realloc(job_sections, new_size);
1265 job_sections[nr_job_sections] = strdup(optarg);
1266 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01001267 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001268 }
Jens Axboe9ac8a792009-11-13 21:23:07 +01001269 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001270 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001271 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001272 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001273 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001274 char *val = optarg;
1275
1276 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001277 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe66251552011-05-12 10:14:57 +02001278 if (ret)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001279 return 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001280 td = NULL;
1281 }
1282 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001283 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001284 int global = 0;
1285
Jens Axboe01f06b62008-02-18 20:53:47 +01001286 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001287 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001288
Jens Axboe01f06b62008-02-18 20:53:47 +01001289 if (is_section && skip_this_section(val))
1290 continue;
1291
Jens Axboe214e1ec2007-03-15 10:48:13 +01001292 td = get_new_job(global, &def_thread);
1293 if (!td)
1294 return 0;
1295 }
1296
1297 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001298 break;
1299 }
Jens Axboea9523c62011-01-17 16:49:54 -07001300 case 'w':
1301 warnings_fatal = 1;
1302 break;
Jens Axboefca70352011-07-06 20:12:54 +02001303 case 'j':
1304 max_jobs = atoi(optarg);
1305 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1306 log_err("fio: invalid max jobs: %d\n", max_jobs);
1307 do_exit++;
1308 exit_val = 1;
1309 }
1310 break;
Jens Axboe50d16972011-09-29 17:45:28 -06001311 case 'S':
Jens Axboea37f69b2011-10-01 12:24:21 -06001312 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06001313 log_err("fio: can't be both client and server\n");
1314 do_exit++;
1315 exit_val = 1;
1316 break;
1317 }
Jens Axboe50d16972011-09-29 17:45:28 -06001318 is_backend = 1;
1319 break;
Jens Axboe132159a2011-09-30 15:01:32 -06001320 case 'P':
1321 fio_net_port = atoi(optarg);
1322 break;
1323 case 'C':
1324 if (is_backend) {
1325 log_err("fio: can't be both client and server\n");
1326 do_exit++;
1327 exit_val = 1;
1328 break;
1329 }
Jens Axboea37f69b2011-10-01 12:24:21 -06001330 fio_client_add(optarg);
Jens Axboe132159a2011-09-30 15:01:32 -06001331 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001332 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001333 do_exit++;
1334 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001335 break;
1336 }
1337 }
1338
Jens Axboec09823a2008-02-19 20:16:57 +01001339 if (do_exit)
1340 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001341
Jens Axboea37f69b2011-10-01 12:24:21 -06001342 if (nr_clients && fio_clients_connect()) {
Jens Axboe132159a2011-09-30 15:01:32 -06001343 do_exit++;
1344 exit_val = 1;
1345 return 1;
1346 }
1347
Jens Axboe50d16972011-09-29 17:45:28 -06001348 if (is_backend)
Jens Axboe5c341e92011-09-29 18:00:35 -06001349 return fio_server();
Jens Axboe50d16972011-09-29 17:45:28 -06001350
Jens Axboe214e1ec2007-03-15 10:48:13 +01001351 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001352 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001353 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001354 }
1355
1356 while (optind < argc) {
1357 ini_idx++;
1358 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1359 ini_file[ini_idx - 1] = strdup(argv[optind]);
1360 optind++;
1361 }
1362
1363 return ini_idx;
1364}
1365
Jens Axboeebac4652005-12-08 15:25:21 +01001366int parse_options(int argc, char *argv[])
1367{
Jens Axboe972cfd22006-06-09 11:08:56 +02001368 int job_files, i;
1369
Jens Axboeb4692822006-10-27 13:43:22 +02001370 f_out = stdout;
1371 f_err = stderr;
1372
Jens Axboe4c6107f2011-01-18 05:22:22 -07001373 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001374 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001375
Jens Axboeebac4652005-12-08 15:25:21 +01001376 if (setup_thread_area())
1377 return 1;
1378 if (fill_def_thread())
1379 return 1;
1380
Jens Axboe972cfd22006-06-09 11:08:56 +02001381 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001382
Jens Axboe972cfd22006-06-09 11:08:56 +02001383 for (i = 0; i < job_files; i++) {
1384 if (fill_def_thread())
1385 return 1;
Jens Axboea37f69b2011-10-01 12:24:21 -06001386 if (nr_clients) {
1387 if (fio_clients_send_ini(ini_file[i]))
Jens Axboe132159a2011-09-30 15:01:32 -06001388 return 1;
Jens Axboe337d75a2011-10-01 12:36:32 -06001389 free(ini_file[i]);
1390 } else if (!is_backend) {
Jens Axboe132159a2011-09-30 15:01:32 -06001391 if (parse_jobs_ini(ini_file[i], 0, i))
1392 return 1;
Jens Axboe337d75a2011-10-01 12:36:32 -06001393 free(ini_file[i]);
Jens Axboe132159a2011-09-30 15:01:32 -06001394 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001395 }
Jens Axboeebac4652005-12-08 15:25:21 +01001396
Jens Axboe88c6ed82006-06-09 11:28:10 +02001397 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001398 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001399
1400 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001401 if (dump_cmdline)
1402 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01001403 if (exec_profile)
1404 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06001405 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06001406 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001407
Bruce Cran03e20d62011-01-02 20:14:54 +01001408 log_err("No jobs(s) defined\n\n");
Jens Axboe45378b32007-12-19 13:54:38 +01001409 usage(argv[0]);
Jens Axboeb4692822006-10-27 13:43:22 +02001410 return 1;
1411 }
1412
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001413 if (def_thread.o.gtod_offload) {
1414 fio_gtod_init();
1415 fio_gtod_offload = 1;
1416 fio_gtod_cpu = def_thread.o.gtod_cpu;
1417 }
1418
Jens Axboeca740f42011-01-25 13:06:16 +01001419 log_info("%s\n", fio_version_string);
Jens Axboeebac4652005-12-08 15:25:21 +01001420 return 0;
1421}