blob: d9c78ec5e69b2953701c799ac292f3295df25c73 [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 Axboee46d8092011-10-03 09:11:02 +020050int log_syslog = 0;
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 },
Jens Axboee46d8092011-10-03 09:11:02 +0200165 { .name = (char *) "daemonize",
166 .has_arg = no_argument,
167 .val = 'D',
168 },
Jens Axboe50d16972011-09-29 17:45:28 -0600169 {
Jens Axboe132159a2011-09-30 15:01:32 -0600170 .name = (char *) "net-port",
171 .has_arg = required_argument,
172 .val = 'P',
173 },
174 {
175 .name = (char *) "client",
176 .has_arg = required_argument,
177 .val = 'C',
178 },
179 {
Jens Axboeb4692822006-10-27 13:43:22 +0200180 .name = NULL,
181 },
182};
183
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200184static void free_shm(void)
185{
186 struct shmid_ds sbuf;
187
188 if (threads) {
189 void *tp = threads;
190
191 threads = NULL;
192 file_hash_exit();
193 fio_debug_jobp = NULL;
194 shmdt(tp);
195 shmctl(shm_id, IPC_RMID, &sbuf);
196 }
197
198 scleanup();
199}
200
201/*
202 * The thread area is shared between the main process and the job
203 * threads/processes. So setup a shared memory segment that will hold
204 * all the job info. We use the end of the region for keeping track of
205 * open files across jobs, for file sharing.
206 */
207static int setup_thread_area(void)
208{
209 void *hash;
210
211 if (threads)
212 return 0;
213
214 /*
215 * 1024 is too much on some machines, scale max_jobs if
216 * we get a failure that looks like too large a shm segment
217 */
218 do {
219 size_t size = max_jobs * sizeof(struct thread_data);
220
221 size += file_hash_size;
222 size += sizeof(unsigned int);
223
224 shm_id = shmget(0, size, IPC_CREAT | 0600);
225 if (shm_id != -1)
226 break;
227 if (errno != EINVAL) {
228 perror("shmget");
229 break;
230 }
231
232 max_jobs >>= 1;
233 } while (max_jobs);
234
235 if (shm_id == -1)
236 return 1;
237
238 threads = shmat(shm_id, NULL, 0);
239 if (threads == (void *) -1) {
240 perror("shmat");
241 return 1;
242 }
243
244 memset(threads, 0, max_jobs * sizeof(struct thread_data));
245 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
246 fio_debug_jobp = (void *) hash + file_hash_size;
247 *fio_debug_jobp = -1;
248 file_hash_init(hash);
249 return 0;
250}
251
Jens Axboe906c8d72006-06-13 09:37:56 +0200252/*
253 * Return a free job structure.
254 */
Jens Axboeebac4652005-12-08 15:25:21 +0100255static struct thread_data *get_new_job(int global, struct thread_data *parent)
256{
257 struct thread_data *td;
258
259 if (global)
260 return &def_thread;
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200261 if (setup_thread_area()) {
262 log_err("error: failed to setup shm segment\n");
263 return NULL;
264 }
Bruce Crane61f1ec2011-01-14 18:30:26 +0100265 if (thread_number >= max_jobs) {
266 log_err("error: maximum number of jobs (%d) reached.\n",
267 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100268 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100269 }
Jens Axboeebac4652005-12-08 15:25:21 +0100270
271 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200272 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100273
Jens Axboee0b0d892009-12-08 10:10:14 +0100274 td->o.uid = td->o.gid = -1U;
275
Jens Axboecade3ef2007-03-23 15:29:45 +0100276 dup_files(td, parent);
Jens Axboed23bb322007-03-23 15:57:56 +0100277 options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100278
Jens Axboe15dc1932010-03-05 10:59:06 +0100279 profile_add_hooks(td);
280
Jens Axboeebac4652005-12-08 15:25:21 +0100281 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100282 return td;
283}
284
285static void put_job(struct thread_data *td)
286{
Jens Axboe549577a2006-10-30 12:23:41 +0100287 if (td == &def_thread)
288 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600289
Jens Axboe58c55ba2010-03-09 12:20:08 +0100290 profile_td_exit(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100291
Jens Axboe16edf252007-02-10 14:59:22 +0100292 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100293 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100294
Jens Axboeebac4652005-12-08 15:25:21 +0100295 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
296 thread_number--;
297}
298
Jens Axboe581e7142009-06-09 12:47:16 +0200299static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100300{
Jens Axboe581e7142009-06-09 12:47:16 +0200301 unsigned int bs = td->o.min_bs[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100302 unsigned long long bytes_per_sec;
Jens Axboe127f6862007-03-15 12:09:57 +0100303
Jens Axboeff58fce2010-08-25 12:02:08 +0200304 assert(ddir_rw(ddir));
305
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100306 if (td->o.rate[ddir])
307 bytes_per_sec = td->o.rate[ddir];
308 else
309 bytes_per_sec = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100310
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100311 if (!bytes_per_sec) {
Jens Axboe127f6862007-03-15 12:09:57 +0100312 log_err("rate lower than supported\n");
313 return -1;
314 }
315
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100316 td->rate_nsec_cycle[ddir] = 1000000000ULL / bytes_per_sec;
Jens Axboe581e7142009-06-09 12:47:16 +0200317 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100318 return 0;
319}
320
Jens Axboe581e7142009-06-09 12:47:16 +0200321static int setup_rate(struct thread_data *td)
322{
323 int ret = 0;
324
325 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
326 ret = __setup_rate(td, DDIR_READ);
327 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
328 ret |= __setup_rate(td, DDIR_WRITE);
329
330 return ret;
331}
332
Jens Axboe83472392009-02-19 21:32:12 +0100333static int fixed_block_size(struct thread_options *o)
334{
335 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
336 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
337 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE];
338}
339
Jens Axboedad915e2006-10-27 11:10:18 +0200340/*
341 * Lazy way of fixing up options that depend on each other. We could also
342 * define option callback handlers, but this is easier.
343 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100344static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200345{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100346 struct thread_options *o = &td->o;
Jens Axboeff217452011-01-18 22:33:02 -0700347 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200348
Jens Axboef356d012009-01-05 09:56:29 +0100349#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100350 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100351 log_info("fio: this platform does not support process shared"
352 " mutexes, forcing use of threads. Use the 'thread'"
353 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100354 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700355 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100356 }
357#endif
358
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100359 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200360 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100361 free(o->write_iolog_file);
362 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700363 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200364 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100365
Jens Axboe16b462a2006-10-30 12:35:18 +0100366 /*
367 * only really works for sequential io for now, and with 1 file
368 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100369 if (o->zone_size && td_random(td) && o->open_files == 1)
370 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100371
372 /*
373 * Reads can do overwrites, we always need to pre-create the file
374 */
375 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100376 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100377
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100378 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100379 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100380 if (!o->max_bs[DDIR_READ])
381 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
382 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100383 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100384 if (!o->max_bs[DDIR_WRITE])
385 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100386
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100387 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Jens Axboea00735e2006-11-03 08:58:08 +0100388
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100389 /*
390 * For random IO, allow blockalign offset other than min_bs.
391 */
392 if (!o->ba[DDIR_READ] || !td_random(td))
393 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
394 if (!o->ba[DDIR_WRITE] || !td_random(td))
395 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
396
397 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
398 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100399 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100400 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100401 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700402 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100403 }
404
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100405 if (!o->file_size_high)
406 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100407
Jens Axboe83472392009-02-19 21:32:12 +0100408 if (o->norandommap && o->verify != VERIFY_NONE
409 && !fixed_block_size(o)) {
410 log_err("fio: norandommap given for variable block sizes, "
411 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100412 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700413 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100414 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100415 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100416 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100417
418 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100419 * thinktime_spin must be less than thinktime
420 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100421 if (o->thinktime_spin > o->thinktime)
422 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100423
424 /*
425 * The low water mark cannot be bigger than the iodepth
426 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100427 if (o->iodepth_low > o->iodepth || !o->iodepth_low) {
Jens Axboe9467b772007-02-27 19:56:43 +0100428 /*
429 * syslet work around - if the workload is sequential,
430 * we want to let the queue drain all the way down to
431 * avoid seeking between async threads
432 */
433 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100434 o->iodepth_low = 1;
Jens Axboe9467b772007-02-27 19:56:43 +0100435 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100436 o->iodepth_low = o->iodepth;
Jens Axboe9467b772007-02-27 19:56:43 +0100437 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100438
439 /*
440 * If batch number isn't set, default to the same as iodepth
441 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100442 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
443 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100444
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100445 if (o->nr_files > td->files_index)
446 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100447
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100448 if (o->open_files > o->nr_files || !o->open_files)
449 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100450
Jens Axboe581e7142009-06-09 12:47:16 +0200451 if (((o->rate[0] + o->rate[1]) && (o->rate_iops[0] + o->rate_iops[1]))||
452 ((o->ratemin[0] + o->ratemin[1]) && (o->rate_iops_min[0] +
453 o->rate_iops_min[1]))) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100454 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700455 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100456 }
Jens Axboe581e7142009-06-09 12:47:16 +0200457 if ((o->rate[0] < o->ratemin[0]) || (o->rate[1] < o->ratemin[1]) ||
458 (o->rate_iops[0] < o->rate_iops_min[0]) ||
459 (o->rate_iops[1] < o->rate_iops_min[1])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100460 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700461 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100462 }
463
Jens Axboecf4464c2007-04-17 20:14:42 +0200464 if (!o->timeout && o->time_based) {
465 log_err("fio: time_based requires a runtime/timeout setting\n");
466 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700467 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200468 }
469
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100470 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200471 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100472
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100473 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700474 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700475 log_info("Multiple writers may overwrite blocks that "
476 "belong to other jobs. This can cause "
477 "verification failures.\n");
478 ret = warnings_fatal;
479 }
480
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100481 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200482 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
483 !o->verify_interval)
484 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboed9905882010-03-18 20:43:00 +0100485 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200486
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100487 if (o->pre_read) {
488 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700489 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200490 log_info("fio: cannot pre-read files with an IO engine"
491 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700492 ret = warnings_fatal;
493 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200494 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200495
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700496#ifndef FIO_HAVE_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100497 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700498 log_info("fio: this platform does not support fdatasync()"
499 " falling back to using fsync(). Use the 'fsync'"
500 " option instead of 'fdatasync' to get rid of"
501 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100502 o->fsync_blocks = o->fdatasync_blocks;
503 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700504 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700505 }
506#endif
507
Jens Axboea9523c62011-01-17 16:49:54 -0700508 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200509}
510
Jens Axboe906c8d72006-06-13 09:37:56 +0200511/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100512 * This function leaks the buffer
513 */
514static char *to_kmg(unsigned int val)
515{
516 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100517 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100518 char *p = post;
519
Jens Axboe245142f2006-11-08 12:49:21 +0100520 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100521 if (val & 1023)
522 break;
523
524 val >>= 10;
525 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100526 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100527
528 snprintf(buf, 31, "%u%c", val, *p);
529 return buf;
530}
531
Jens Axboe09629a92007-03-09 09:00:06 +0100532/* External engines are specified by "external:name.o") */
533static const char *get_engine_name(const char *str)
534{
535 char *p = strstr(str, ":");
536
537 if (!p)
538 return str;
539
540 p++;
541 strip_blank_front(&p);
542 strip_blank_end(p);
543 return p;
544}
545
Jens Axboee132cba2007-03-14 14:23:54 +0100546static int exists_and_not_file(const char *filename)
547{
548 struct stat sb;
549
550 if (lstat(filename, &sb) == -1)
551 return 0;
552
Bruce Cranecc314b2011-01-04 10:59:30 +0100553 /* \\.\ is the device namespace in Windows, where every file
554 * is a device node */
555 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100556 return 0;
557
558 return 1;
559}
560
Jens Axboe4c07ad82011-03-28 09:51:09 +0200561static void td_fill_rand_seeds_os(struct thread_data *td)
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200562{
563 os_random_seed(td->rand_seeds[0], &td->bsrange_state);
564 os_random_seed(td->rand_seeds[1], &td->verify_state);
565 os_random_seed(td->rand_seeds[2], &td->rwmix_state);
566
567 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
568 os_random_seed(td->rand_seeds[3], &td->next_file_state);
569
570 os_random_seed(td->rand_seeds[5], &td->file_size_state);
Jens Axboe0d29de82010-09-01 13:54:15 +0200571 os_random_seed(td->rand_seeds[6], &td->trim_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200572
573 if (!td_random(td))
574 return;
575
576 if (td->o.rand_repeatable)
577 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
578
579 os_random_seed(td->rand_seeds[4], &td->random_state);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200580}
581
582static void td_fill_rand_seeds_internal(struct thread_data *td)
583{
584 init_rand_seed(&td->__bsrange_state, td->rand_seeds[0]);
585 init_rand_seed(&td->__verify_state, td->rand_seeds[1]);
586 init_rand_seed(&td->__rwmix_state, td->rand_seeds[2]);
587
588 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
589 init_rand_seed(&td->__next_file_state, td->rand_seeds[3]);
590
591 init_rand_seed(&td->__file_size_state, td->rand_seeds[5]);
592 init_rand_seed(&td->__trim_state, td->rand_seeds[6]);
593
594 if (!td_random(td))
595 return;
596
597 if (td->o.rand_repeatable)
598 td->rand_seeds[4] = FIO_RANDSEED * td->thread_number;
599
Jens Axboe2615cc42011-03-28 09:35:09 +0200600 init_rand_seed(&td->__random_state, td->rand_seeds[4]);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200601}
602
Jens Axboe4c07ad82011-03-28 09:51:09 +0200603void td_fill_rand_seeds(struct thread_data *td)
604{
605 if (td->o.use_os_rand)
606 td_fill_rand_seeds_os(td);
607 else
608 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600609
610 init_rand_seed(&td->buf_state, td->rand_seeds[7]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200611}
612
Jens Axboef8977ee2006-11-06 14:03:03 +0100613/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100614 * Initialize the various random states we need (random io, block size ranges,
615 * read/write mix, etc).
616 */
617static int init_random_state(struct thread_data *td)
618{
Jens Axboe68727072007-03-15 20:49:25 +0100619 int fd;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100620
621 fd = open("/dev/urandom", O_RDONLY);
622 if (fd == -1) {
623 td_verror(td, errno, "open");
624 return 1;
625 }
626
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200627 if (read(fd, td->rand_seeds, sizeof(td->rand_seeds)) <
628 (int) sizeof(td->rand_seeds)) {
Jens Axboe9c60ce62007-03-15 09:14:47 +0100629 td_verror(td, EIO, "read");
630 close(fd);
631 return 1;
632 }
633
634 close(fd);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200635 td_fill_rand_seeds(td);
Jens Axboe9c60ce62007-03-15 09:14:47 +0100636 return 0;
637}
638
Jens Axboe9c60ce62007-03-15 09:14:47 +0100639/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200640 * Adds a job to the list of things todo. Sanitizes the various options
641 * to make sure we don't have conflicts, and initializes various
642 * members of td.
643 */
Jens Axboe75154842006-06-01 13:56:09 +0200644static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100645{
Jens Axboe413dd452007-02-23 09:26:09 +0100646 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
647 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100648 unsigned int i;
Jens Axboe09629a92007-03-09 09:00:06 +0100649 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100650 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100651 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100652
Jens Axboeebac4652005-12-08 15:25:21 +0100653 /*
654 * the def_thread is just for options, it's not a real job
655 */
656 if (td == &def_thread)
657 return 0;
658
Jens Axboecca73aa2007-04-04 11:09:19 +0200659 /*
660 * if we are just dumping the output command line, don't add the job
661 */
662 if (dump_cmdline) {
663 put_job(td);
664 return 0;
665 }
666
Jens Axboe58c55ba2010-03-09 12:20:08 +0100667 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +0200668 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +0100669
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100670 engine = get_engine_name(td->o.ioengine);
Jens Axboe09629a92007-03-09 09:00:06 +0100671 td->io_ops = load_ioengine(td, engine);
672 if (!td->io_ops) {
673 log_err("fio: failed to load engine %s\n", engine);
Jens Axboe205927a2007-03-15 11:06:32 +0100674 goto err;
Jens Axboe09629a92007-03-09 09:00:06 +0100675 }
Jens Axboedf641192006-10-12 07:55:41 +0200676
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100677 if (td->o.use_thread)
Jens Axboe9cedf162007-03-12 11:29:30 +0100678 nr_thread++;
679 else
680 nr_process++;
681
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100682 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100683 td->io_ops->flags |= FIO_RAWIO;
684
Jens Axboee132cba2007-03-14 14:23:54 +0100685 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100686 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100687 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100688
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100689 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100690 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100691 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100692 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100693 sprintf(fname, "%s.%d.%d", jobname,
694 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100695 add_file(td, fname);
696 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100697 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100698 }
Jens Axboeebac4652005-12-08 15:25:21 +0100699
Jens Axboe4e991c22007-03-15 11:41:11 +0100700 if (fixup_options(td))
701 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100702
Jens Axboe07eb79d2007-04-12 13:02:38 +0200703 if (td->io_ops->flags & FIO_DISKLESSIO) {
704 struct fio_file *f;
705
706 for_each_file(td, f, i)
707 f->real_file_size = -1ULL;
708 }
709
Jens Axboecdd18ad2008-02-27 18:58:00 +0100710 td->mutex = fio_mutex_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100711
Yu-ju Hong83349192011-08-13 00:53:44 +0200712 td->ts.clat_percentiles = td->o.clat_percentiles;
713 if (td->o.overwrite_plist)
714 td->ts.percentile_list = td->o.percentile_list;
715 else
716 td->ts.percentile_list = NULL;
717
Jens Axboe756867b2007-03-06 15:19:24 +0100718 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
719 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
Jens Axboe02af0982010-06-24 09:59:34 +0200720 td->ts.lat_stat[0].min_val = td->ts.lat_stat[1].min_val = ULONG_MAX;
Jens Axboe756867b2007-03-06 15:19:24 +0100721 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Adam DeBellinscdd54112010-09-28 13:22:55 +0900722 td->ddir_seq_nr = td->o.ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100723
Jens Axboe755c3182009-08-25 22:14:29 +0200724 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100725 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100726 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100727 }
Jens Axboeebac4652005-12-08 15:25:21 +0100728
729 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100730 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100731
Jens Axboe9c60ce62007-03-15 09:14:47 +0100732 if (init_random_state(td))
733 goto err;
734
Jens Axboeebac4652005-12-08 15:25:21 +0100735 if (setup_rate(td))
736 goto err;
737
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100738 if (td->o.write_lat_log) {
Jens Axboe7b9f7332011-10-03 10:06:44 +0200739 setup_log(&td->lat_log);
740 setup_log(&td->slat_log);
741 setup_log(&td->clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100742 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100743 if (td->o.write_bw_log)
Jens Axboe7b9f7332011-10-03 10:06:44 +0200744 setup_log(&td->bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100745
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100746 if (!td->o.name)
747 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200748
Jens Axboec6ae0a52006-06-12 11:04:44 +0200749 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200750 if (!job_add_num) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100751 if (!strcmp(td->io_ops->name, "cpuio")) {
752 log_info("%s: ioengine=cpu, cpuload=%u,"
753 " cpucycle=%u\n", td->o.name,
754 td->o.cpuload,
755 td->o.cpucycle);
756 } else {
Jens Axboef8977ee2006-11-06 14:03:03 +0100757 char *c1, *c2, *c3, *c4;
758
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100759 c1 = to_kmg(td->o.min_bs[DDIR_READ]);
760 c2 = to_kmg(td->o.max_bs[DDIR_READ]);
761 c3 = to_kmg(td->o.min_bs[DDIR_WRITE]);
762 c4 = to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100763
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100764 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s,"
765 " ioengine=%s, iodepth=%u\n",
766 td->o.name, td->groupid,
767 ddir_str[td->o.td_ddir],
768 c1, c2, c3, c4,
769 td->io_ops->name,
770 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100771
772 free(c1);
773 free(c2);
774 free(c3);
775 free(c4);
776 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200777 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100778 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200779 }
Jens Axboeebac4652005-12-08 15:25:21 +0100780
781 /*
782 * recurse add identical jobs, clear numjobs and stonewall options
783 * as they don't apply to sub-jobs
784 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100785 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100786 while (--numjobs) {
787 struct thread_data *td_new = get_new_job(0, td);
788
789 if (!td_new)
790 goto err;
791
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100792 td_new->o.numjobs = 1;
793 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200794 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100795
796 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100797 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100798 td_new->files_index = 0;
Jens Axboe4e6ea2f2009-03-04 20:17:22 +0100799 td_new->files_size = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100800 td_new->files = NULL;
801 }
802
Jens Axboe75154842006-06-01 13:56:09 +0200803 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100804
Jens Axboe75154842006-06-01 13:56:09 +0200805 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100806 goto err;
807 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100808
Jens Axboeebac4652005-12-08 15:25:21 +0100809 return 0;
810err:
811 put_job(td);
812 return -1;
813}
814
Jens Axboe79d16312010-03-04 12:43:20 +0100815/*
816 * Parse as if 'o' was a command line
817 */
818void add_job_opts(const char **o)
819{
820 struct thread_data *td, *td_parent;
821 int i, in_global = 1;
822 char jobname[32];
823
824 i = 0;
825 td_parent = td = NULL;
826 while (o[i]) {
827 if (!strncmp(o[i], "name", 4)) {
828 in_global = 0;
829 if (td)
830 add_job(td, jobname, 0);
831 td = NULL;
832 sprintf(jobname, "%s", o[i] + 5);
833 }
834 if (in_global && !td_parent)
835 td_parent = get_new_job(1, &def_thread);
836 else if (!in_global && !td) {
837 if (!td_parent)
838 td_parent = &def_thread;
839 td = get_new_job(0, td_parent);
840 }
841 if (in_global)
842 fio_options_parse(td_parent, (char **) &o[i], 1);
843 else
844 fio_options_parse(td, (char **) &o[i], 1);
845 i++;
846 }
847
848 if (td)
849 add_job(td, jobname, 0);
850}
851
Jens Axboe01f06b62008-02-18 20:53:47 +0100852static int skip_this_section(const char *name)
853{
Jens Axboead0a2732011-03-11 10:16:17 +0100854 int i;
855
856 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +0100857 return 0;
858 if (!strncmp(name, "global", 6))
859 return 0;
860
Jens Axboead0a2732011-03-11 10:16:17 +0100861 for (i = 0; i < nr_job_sections; i++)
862 if (!strcmp(job_sections[i], name))
863 return 0;
864
865 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +0100866}
867
Jens Axboeebac4652005-12-08 15:25:21 +0100868static int is_empty_or_comment(char *line)
869{
870 unsigned int i;
871
872 for (i = 0; i < strlen(line); i++) {
873 if (line[i] == ';')
874 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100875 if (line[i] == '#')
876 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +0200877 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +0100878 return 0;
879 }
880
881 return 1;
882}
883
Jens Axboe313cb202006-12-21 09:50:00 +0100884/*
Jens Axboe07261982006-06-07 10:51:12 +0200885 * This is our [ini] type file parser.
886 */
Jens Axboe50d16972011-09-29 17:45:28 -0600887int parse_jobs_ini(char *file, int is_buf, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +0100888{
Jens Axboee1f36502006-10-27 10:54:08 +0200889 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +0100890 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +0100891 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +0100892 FILE *f;
893 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200894 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +0200895 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +0200896 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +0100897 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200898 char **opts;
899 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +0100900
Jens Axboe50d16972011-09-29 17:45:28 -0600901 if (is_buf)
902 f = NULL;
903 else {
904 if (!strcmp(file, "-"))
905 f = stdin;
906 else
907 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +0200908
Jens Axboe50d16972011-09-29 17:45:28 -0600909 if (!f) {
910 perror("fopen job file");
911 return 1;
912 }
Jens Axboeebac4652005-12-08 15:25:21 +0100913 }
914
915 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +0200916
917 /*
918 * it's really 256 + small bit, 280 should suffice
919 */
920 name = malloc(280);
921 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +0100922
Jens Axboe3b8b7132008-06-10 19:46:23 +0200923 alloc_opts = 8;
924 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +0200925 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +0200926
Jens Axboe0c7e37a2006-06-13 14:53:38 +0200927 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +0100928 do {
Jens Axboeccf8f122007-09-27 10:48:55 +0200929 /*
930 * if skip_fgets is set, we already have loaded a line we
931 * haven't handled.
932 */
933 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -0600934 if (is_buf)
935 p = strsep(&file, "\n");
936 else
937 p = fgets(string, 4095, f);
Jens Axboeccf8f122007-09-27 10:48:55 +0200938 if (!p)
939 break;
940 }
gurudas paicdc7f192007-03-29 10:10:56 +0200941
Jens Axboeccf8f122007-09-27 10:48:55 +0200942 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +0200943 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800944 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +0200945
Jens Axboeebac4652005-12-08 15:25:21 +0100946 if (is_empty_or_comment(p))
947 continue;
Bruce Cran84dd1882011-05-05 08:14:09 -0600948 if (sscanf(p, "[%255[^\n]]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +0100949 if (inside_skip)
950 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100951 log_err("fio: option <%s> outside of [] job section\n",
952 p);
Jens Axboe088b4202007-07-19 14:53:01 +0200953 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -0800954 }
Jens Axboeebac4652005-12-08 15:25:21 +0100955
Jens Axboe7a4b80a2010-05-22 20:43:11 +0200956 name[strlen(name) - 1] = '\0';
957
Jens Axboe01f06b62008-02-18 20:53:47 +0100958 if (skip_this_section(name)) {
959 inside_skip = 1;
960 continue;
961 } else
962 inside_skip = 0;
963
Jens Axboeebac4652005-12-08 15:25:21 +0100964 global = !strncmp(name, "global", 6);
965
Jens Axboecca73aa2007-04-04 11:09:19 +0200966 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +0200967 if (first_sect)
968 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +0200969 if (!global)
970 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +0200971 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +0200972 }
973
Jens Axboeebac4652005-12-08 15:25:21 +0100974 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +0200975 if (!td) {
976 ret = 1;
977 break;
978 }
Jens Axboeebac4652005-12-08 15:25:21 +0100979
Jens Axboe972cfd22006-06-09 11:08:56 +0200980 /*
981 * Seperate multiple job files by a stonewall
982 */
Jens Axboef9481912006-06-09 11:37:28 +0200983 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100984 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +0200985 stonewall = 0;
986 }
987
Jens Axboe3b8b7132008-06-10 19:46:23 +0200988 num_opts = 0;
989 memset(opts, 0, alloc_opts * sizeof(char *));
990
Jens Axboe50d16972011-09-29 17:45:28 -0600991 while (1) {
992 if (is_buf)
993 p = strsep(&file, "\n");
994 else
995 p = fgets(string, 4096, f);
996 if (!p)
997 break;
998
Jens Axboeebac4652005-12-08 15:25:21 +0100999 if (is_empty_or_comment(p))
1000 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001001
Jens Axboeb6754f92006-05-30 14:29:32 +02001002 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001003
Jens Axboeccf8f122007-09-27 10:48:55 +02001004 /*
1005 * new section, break out and make sure we don't
1006 * fgets() a new line at the top.
1007 */
1008 if (p[0] == '[') {
1009 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001010 break;
Jens Axboeccf8f122007-09-27 10:48:55 +02001011 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001012
Jens Axboe4ae3f762006-05-30 13:35:14 +02001013 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001014
Jens Axboe3b8b7132008-06-10 19:46:23 +02001015 if (num_opts == alloc_opts) {
1016 alloc_opts <<= 1;
1017 opts = realloc(opts,
1018 alloc_opts * sizeof(char *));
1019 }
1020
1021 opts[num_opts] = strdup(p);
1022 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +01001023 }
Jens Axboeebac4652005-12-08 15:25:21 +01001024
Jens Axboe3b8b7132008-06-10 19:46:23 +02001025 ret = fio_options_parse(td, opts, num_opts);
1026 if (!ret) {
1027 if (dump_cmdline)
1028 for (i = 0; i < num_opts; i++)
1029 log_info("--%s ", opts[i]);
1030
Jens Axboe45410ac2006-06-07 11:10:39 +02001031 ret = add_job(td, name, 0);
Jens Axboe3b8b7132008-06-10 19:46:23 +02001032 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +01001033 log_err("fio: job %s dropped\n", name);
1034 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001035 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001036
1037 for (i = 0; i < num_opts; i++)
1038 free(opts[i]);
1039 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001040 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001041
Jens Axboecca73aa2007-04-04 11:09:19 +02001042 if (dump_cmdline)
1043 log_info("\n");
1044
Jens Axboe3b8b7132008-06-10 19:46:23 +02001045 for (i = 0; i < num_opts; i++)
1046 free(opts[i]);
1047
Jens Axboeebac4652005-12-08 15:25:21 +01001048 free(string);
1049 free(name);
Jens Axboe25775942008-06-10 20:26:06 +02001050 free(opts);
Jens Axboe50d16972011-09-29 17:45:28 -06001051 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001052 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001053 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001054}
1055
1056static int fill_def_thread(void)
1057{
1058 memset(&def_thread, 0, sizeof(def_thread));
1059
Jens Axboe375b2692007-05-22 09:13:02 +02001060 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboeebac4652005-12-08 15:25:21 +01001061
1062 /*
Jens Axboeee738492007-01-10 11:23:16 +01001063 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001064 */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001065 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001066 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 Axboeeb7976e2011-10-01 12:42:00 -06001116 { .name = "net", .shift = FD_NET },
Jens Axboe02444ad2008-10-07 11:33:00 +02001117 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001118};
1119
Jens Axboec09823a2008-02-19 20:16:57 +01001120static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001121{
1122 struct debug_level *dl;
1123 char *p = (char *) string;
1124 char *opt;
1125 int i;
1126
1127 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001128 log_info("fio: dumping debug options:");
1129 for (i = 0; debug_levels[i].name; i++) {
1130 dl = &debug_levels[i];
1131 log_info("%s,", dl->name);
1132 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001133 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001134 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001135 }
1136
1137 while ((opt = strsep(&p, ",")) != NULL) {
1138 int found = 0;
1139
Jens Axboe5e1d3062008-05-23 11:55:53 +02001140 if (!strncmp(opt, "all", 3)) {
1141 log_info("fio: set all debug options\n");
1142 fio_debug = ~0UL;
1143 continue;
1144 }
1145
Jens Axboeee56ad52008-02-01 10:30:20 +01001146 for (i = 0; debug_levels[i].name; i++) {
1147 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001148 found = !strncmp(opt, dl->name, strlen(dl->name));
1149 if (!found)
1150 continue;
1151
1152 if (dl->shift == FD_JOB) {
1153 opt = strchr(opt, ':');
1154 if (!opt) {
1155 log_err("fio: missing job number\n");
1156 break;
1157 }
1158 opt++;
1159 fio_debug_jobno = atoi(opt);
1160 log_info("fio: set debug jobno %d\n",
1161 fio_debug_jobno);
1162 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001163 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001164 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001165 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001166 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001167 }
1168
1169 if (!found)
1170 log_err("fio: debug mask %s not found\n", opt);
1171 }
Jens Axboec09823a2008-02-19 20:16:57 +01001172 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001173}
Jens Axboe79e48f72008-02-01 20:37:09 +01001174#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001175static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001176{
1177 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001178 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001179}
1180#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001181
Jens Axboe4c6107f2011-01-18 05:22:22 -07001182static void fio_options_fill_optstring(void)
1183{
1184 char *ostr = cmd_optstr;
1185 int i, c;
1186
1187 c = i = 0;
1188 while (l_opts[i].name) {
1189 ostr[c++] = l_opts[i].val;
1190 if (l_opts[i].has_arg == required_argument)
1191 ostr[c++] = ':';
1192 else if (l_opts[i].has_arg == optional_argument) {
1193 ostr[c++] = ':';
1194 ostr[c++] = ':';
1195 }
1196 i++;
1197 }
1198 ostr[c] = '\0';
1199}
1200
Jens Axboe214e1ec2007-03-15 10:48:13 +01001201static int parse_cmd_line(int argc, char *argv[])
1202{
1203 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001204 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001205 char *ostr = cmd_optstr;
Jens Axboee46d8092011-10-03 09:11:02 +02001206 int daemonize_server = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001207
Jens Axboe4c6107f2011-01-18 05:22:22 -07001208 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001209 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001210 case 'a':
1211 smalloc_pool_size = atoi(optarg);
1212 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001213 case 't':
Jens Axboeab2cabd2011-07-23 13:43:13 +02001214 def_thread.o.timeout = atoi(optarg);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001215 break;
1216 case 'l':
1217 write_lat_log = 1;
1218 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001219 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001220 write_bw_log = 1;
1221 break;
1222 case 'o':
1223 f_out = fopen(optarg, "w+");
1224 if (!f_out) {
1225 perror("fopen output");
1226 exit(1);
1227 }
1228 f_err = f_out;
1229 break;
1230 case 'm':
1231 terse_output = 1;
1232 break;
1233 case 'h':
Jens Axboe45378b32007-12-19 13:54:38 +01001234 usage(argv[0]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001235 exit(0);
1236 case 'c':
1237 exit(fio_show_option_help(optarg));
Jens Axboecca73aa2007-04-04 11:09:19 +02001238 case 's':
1239 dump_cmdline = 1;
1240 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001241 case 'r':
1242 read_only = 1;
1243 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001244 case 'v':
Jens Axboeca740f42011-01-25 13:06:16 +01001245 log_info("%s\n", fio_version_string);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001246 exit(0);
Jens Axboef57a9c52011-09-09 21:01:37 +02001247 case 'V':
1248 terse_version = atoi(optarg);
1249 if (terse_version != 2) {
1250 log_err("fio: bad terse version format\n");
1251 exit_val = 1;
1252 do_exit++;
1253 }
1254 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001255 case 'e':
1256 if (!strcmp("always", optarg))
1257 eta_print = FIO_ETA_ALWAYS;
1258 else if (!strcmp("never", optarg))
1259 eta_print = FIO_ETA_NEVER;
1260 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001261 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001262 if (set_debug(optarg))
1263 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001264 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001265 case 'x': {
1266 size_t new_size;
1267
Jens Axboe01f06b62008-02-18 20:53:47 +01001268 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001269 log_err("fio: can't use global as only "
1270 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001271 do_exit++;
1272 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001273 break;
1274 }
Jens Axboead0a2732011-03-11 10:16:17 +01001275 new_size = (nr_job_sections + 1) * sizeof(char *);
1276 job_sections = realloc(job_sections, new_size);
1277 job_sections[nr_job_sections] = strdup(optarg);
1278 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01001279 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001280 }
Jens Axboe9ac8a792009-11-13 21:23:07 +01001281 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001282 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001283 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001284 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001285 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001286 char *val = optarg;
1287
1288 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001289 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe66251552011-05-12 10:14:57 +02001290 if (ret)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001291 return 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001292 td = NULL;
1293 }
1294 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001295 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001296 int global = 0;
1297
Jens Axboe01f06b62008-02-18 20:53:47 +01001298 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001299 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001300
Jens Axboe01f06b62008-02-18 20:53:47 +01001301 if (is_section && skip_this_section(val))
1302 continue;
1303
Jens Axboe214e1ec2007-03-15 10:48:13 +01001304 td = get_new_job(global, &def_thread);
1305 if (!td)
1306 return 0;
1307 }
1308
1309 ret = fio_cmd_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001310 break;
1311 }
Jens Axboea9523c62011-01-17 16:49:54 -07001312 case 'w':
1313 warnings_fatal = 1;
1314 break;
Jens Axboefca70352011-07-06 20:12:54 +02001315 case 'j':
1316 max_jobs = atoi(optarg);
1317 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1318 log_err("fio: invalid max jobs: %d\n", max_jobs);
1319 do_exit++;
1320 exit_val = 1;
1321 }
1322 break;
Jens Axboe50d16972011-09-29 17:45:28 -06001323 case 'S':
Jens Axboea37f69b2011-10-01 12:24:21 -06001324 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06001325 log_err("fio: can't be both client and server\n");
1326 do_exit++;
1327 exit_val = 1;
1328 break;
1329 }
Jens Axboe50d16972011-09-29 17:45:28 -06001330 is_backend = 1;
1331 break;
Jens Axboee46d8092011-10-03 09:11:02 +02001332 case 'D':
1333 daemonize_server = 1;
1334 break;
Jens Axboe132159a2011-09-30 15:01:32 -06001335 case 'P':
1336 fio_net_port = atoi(optarg);
1337 break;
1338 case 'C':
1339 if (is_backend) {
1340 log_err("fio: can't be both client and server\n");
1341 do_exit++;
1342 exit_val = 1;
1343 break;
1344 }
Jens Axboea37f69b2011-10-01 12:24:21 -06001345 fio_client_add(optarg);
Jens Axboe132159a2011-09-30 15:01:32 -06001346 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001347 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001348 do_exit++;
1349 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001350 break;
1351 }
Jens Axboec7d5c942011-10-03 11:48:17 +02001352 if (do_exit)
1353 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001354 }
1355
Jens Axboec09823a2008-02-19 20:16:57 +01001356 if (do_exit)
1357 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001358
Jens Axboea37f69b2011-10-01 12:24:21 -06001359 if (nr_clients && fio_clients_connect()) {
Jens Axboe132159a2011-09-30 15:01:32 -06001360 do_exit++;
1361 exit_val = 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02001362 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -06001363 }
1364
Jens Axboe50d16972011-09-29 17:45:28 -06001365 if (is_backend)
Jens Axboee46d8092011-10-03 09:11:02 +02001366 return fio_start_server(daemonize_server);
Jens Axboe50d16972011-09-29 17:45:28 -06001367
Jens Axboe214e1ec2007-03-15 10:48:13 +01001368 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001369 if (!ret)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001370 ret = add_job(td, td->o.name ?: "fio", 0);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001371 }
1372
1373 while (optind < argc) {
1374 ini_idx++;
1375 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1376 ini_file[ini_idx - 1] = strdup(argv[optind]);
1377 optind++;
1378 }
1379
1380 return ini_idx;
1381}
1382
Jens Axboeebac4652005-12-08 15:25:21 +01001383int parse_options(int argc, char *argv[])
1384{
Jens Axboe972cfd22006-06-09 11:08:56 +02001385 int job_files, i;
1386
Jens Axboeb4692822006-10-27 13:43:22 +02001387 f_out = stdout;
1388 f_err = stderr;
1389
Jens Axboe4c6107f2011-01-18 05:22:22 -07001390 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001391 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001392
Jens Axboe9d9eb2e2011-10-03 12:01:42 +02001393 atexit(free_shm);
1394
Jens Axboeebac4652005-12-08 15:25:21 +01001395 if (fill_def_thread())
1396 return 1;
1397
Jens Axboe972cfd22006-06-09 11:08:56 +02001398 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001399
Jens Axboecdf54d82011-10-04 08:31:40 +02001400 if (job_files > 0) {
1401 for (i = 0; i < job_files; i++) {
1402 if (fill_def_thread())
Jens Axboe132159a2011-09-30 15:01:32 -06001403 return 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02001404 if (nr_clients) {
1405 if (fio_clients_send_ini(ini_file[i]))
1406 return 1;
1407 free(ini_file[i]);
1408 } else if (!is_backend) {
1409 if (parse_jobs_ini(ini_file[i], 0, i))
1410 return 1;
1411 free(ini_file[i]);
1412 }
Jens Axboe132159a2011-09-30 15:01:32 -06001413 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001414 }
Jens Axboeebac4652005-12-08 15:25:21 +01001415
Jens Axboe88c6ed82006-06-09 11:28:10 +02001416 free(ini_file);
Jens Axboed23bb322007-03-23 15:57:56 +01001417 options_mem_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001418
1419 if (!thread_number) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001420 if (dump_cmdline)
1421 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01001422 if (exec_profile)
1423 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06001424 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06001425 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001426
Jens Axboecdf54d82011-10-04 08:31:40 +02001427 if (job_files > 0) {
1428 log_err("No jobs(s) defined\n\n");
1429 usage(argv[0]);
1430 }
Jens Axboeb4692822006-10-27 13:43:22 +02001431 return 1;
1432 }
1433
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001434 if (def_thread.o.gtod_offload) {
1435 fio_gtod_init();
1436 fio_gtod_offload = 1;
1437 fio_gtod_cpu = def_thread.o.gtod_cpu;
1438 }
1439
Jens Axboeca740f42011-01-25 13:06:16 +01001440 log_info("%s\n", fio_version_string);
Jens Axboeebac4652005-12-08 15:25:21 +01001441 return 0;
1442}