blob: 634dda20afd904d71b19095c2507c4b365b69cc5 [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>
Jens Axboeebac4652005-12-08 15:25:21 +010012#include <sys/types.h>
13#include <sys/stat.h>
14
15#include "fio.h"
Olega5e0ee12013-03-12 00:06:53 -070016#ifndef FIO_NO_HAVE_SHM_H
17#include <sys/shm.h>
18#endif
19
Jens Axboecb2c86f2006-10-26 13:48:34 +020020#include "parse.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010021#include "smalloc.h"
Jens Axboe380065a2008-03-01 18:56:24 +010022#include "filehash.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020023#include "verify.h"
Jens Axboe79d16312010-03-04 12:43:20 +010024#include "profile.h"
Jens Axboe50d16972011-09-29 17:45:28 -060025#include "server.h"
Huadong Liuf2a2ce02013-01-30 13:22:24 +010026#include "idletime.h"
Jens Axboeebac4652005-12-08 15:25:21 +010027
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040028#include "lib/getopt.h"
Jens Axboee25b6d52013-04-05 14:44:51 +020029#include "lib/strcasestr.h"
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040030
Jens Axboe3d433822012-03-21 22:25:22 +010031const char fio_version_string[] = FIO_VERSION;
Jens Axboe214e1ec2007-03-15 10:48:13 +010032
Jens Axboeee738492007-01-10 11:23:16 +010033#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010034
Jens Axboe214e1ec2007-03-15 10:48:13 +010035static char **ini_file;
Jens Axboefca70352011-07-06 20:12:54 +020036static int max_jobs = FIO_MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020037static int dump_cmdline;
Jens Axboe8c029372011-10-05 09:54:30 +020038static int def_timeout;
Jens Axboe111e0322013-03-07 11:31:20 +010039static int parse_only;
Jens Axboee1f36502006-10-27 10:54:08 +020040
Jens Axboebe4ecfd2008-12-08 14:10:52 +010041static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010042struct thread_data *threads = NULL;
Jens Axboee1f36502006-10-27 10:54:08 +020043
Jens Axboe214e1ec2007-03-15 10:48:13 +010044int exitall_on_terminate = 0;
Jens Axboef3afa572012-09-17 13:34:16 +020045int output_format = FIO_OUTPUT_NORMAL;
Jens Axboe5be4c942013-02-11 16:33:25 +010046int eta_print = FIO_ETA_AUTO;
Jens Axboee382e662013-02-22 20:48:56 +010047int eta_new_line = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +010048FILE *f_out = NULL;
49FILE *f_err = NULL;
Jens Axboead0a2732011-03-11 10:16:17 +010050char **job_sections = NULL;
51int nr_job_sections = 0;
Jens Axboe07b32322010-03-05 09:48:44 +010052char *exec_profile = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -070053int warnings_fatal = 0;
Jens Axboe4d658652011-10-17 15:05:47 +020054int terse_version = 3;
Jens Axboe50d16972011-09-29 17:45:28 -060055int is_backend = 0;
Jens Axboea37f69b2011-10-01 12:24:21 -060056int nr_clients = 0;
Jens Axboee46d8092011-10-03 09:11:02 +020057int log_syslog = 0;
Jens Axboeee738492007-01-10 11:23:16 +010058
Jens Axboe214e1ec2007-03-15 10:48:13 +010059int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020060int read_only = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020061
Jens Axboe5ec10ea2008-03-06 15:42:00 +010062static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010063
64static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020065
Jens Axboeee56ad52008-02-01 10:30:20 +010066unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020067unsigned int fio_debug_jobno = -1;
68unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010069
Jens Axboe4c6107f2011-01-18 05:22:22 -070070static char cmd_optstr[256];
Jens Axboe085399d2011-10-06 09:01:22 +020071static int did_arg;
Jens Axboe4c6107f2011-01-18 05:22:22 -070072
Jens Axboe7a4b8242011-10-05 17:35:15 +020073#define FIO_CLIENT_FLAG (1 << 16)
74
Jens Axboeb4692822006-10-27 13:43:22 +020075/*
76 * Command line options. These will contain the above, plus a few
77 * extra that only pertain to fio itself and not jobs.
78 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010079static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020080 {
Jens Axboe08d2a192011-05-11 13:28:30 +020081 .name = (char *) "output",
Jens Axboeb4692822006-10-27 13:43:22 +020082 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020083 .val = 'o' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020084 },
85 {
Jens Axboe08d2a192011-05-11 13:28:30 +020086 .name = (char *) "timeout",
Jens Axboeb4692822006-10-27 13:43:22 +020087 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020088 .val = 't' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020089 },
90 {
Jens Axboe08d2a192011-05-11 13:28:30 +020091 .name = (char *) "latency-log",
Jens Axboeb4692822006-10-27 13:43:22 +020092 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020093 .val = 'l' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020094 },
95 {
Jens Axboe08d2a192011-05-11 13:28:30 +020096 .name = (char *) "bandwidth-log",
Jens Axboeb4692822006-10-27 13:43:22 +020097 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020098 .val = 'b' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020099 },
100 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200101 .name = (char *) "minimal",
Jens Axboeb4692822006-10-27 13:43:22 +0200102 .has_arg = optional_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200103 .val = 'm' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200104 },
105 {
Jens Axboef3afa572012-09-17 13:34:16 +0200106 .name = (char *) "output-format",
Shaohua Licc372b12012-09-17 09:12:18 +0200107 .has_arg = optional_argument,
Jens Axboef3afa572012-09-17 13:34:16 +0200108 .val = 'F' | FIO_CLIENT_FLAG,
Shaohua Licc372b12012-09-17 09:12:18 +0200109 },
110 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200111 .name = (char *) "version",
Jens Axboeb4692822006-10-27 13:43:22 +0200112 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200113 .val = 'v' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200114 },
115 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200116 .name = (char *) "help",
Jens Axboefd28ca42007-01-09 21:20:13 +0100117 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200118 .val = 'h' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100119 },
120 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200121 .name = (char *) "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100122 .has_arg = optional_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200123 .val = 'c' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100124 },
125 {
Steven Langde890a12011-11-09 14:03:34 +0100126 .name = (char *) "enghelp",
127 .has_arg = optional_argument,
128 .val = 'i' | FIO_CLIENT_FLAG,
129 },
130 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200131 .name = (char *) "showcmd",
Jens Axboecca73aa2007-04-04 11:09:19 +0200132 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200133 .val = 's' | FIO_CLIENT_FLAG,
Jens Axboe724e4432007-09-11 20:02:05 +0200134 },
135 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200136 .name = (char *) "readonly",
Jens Axboe724e4432007-09-11 20:02:05 +0200137 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200138 .val = 'r' | FIO_CLIENT_FLAG,
Jens Axboecca73aa2007-04-04 11:09:19 +0200139 },
140 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200141 .name = (char *) "eta",
Aaron Carrolle592a062007-09-14 09:49:41 +0200142 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200143 .val = 'e' | FIO_CLIENT_FLAG,
Aaron Carrolle592a062007-09-14 09:49:41 +0200144 },
145 {
Jens Axboee382e662013-02-22 20:48:56 +0100146 .name = (char *) "eta-newline",
147 .has_arg = required_argument,
148 .val = 'E' | FIO_CLIENT_FLAG,
149 },
150 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200151 .name = (char *) "debug",
Jens Axboeee56ad52008-02-01 10:30:20 +0100152 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200153 .val = 'd' | FIO_CLIENT_FLAG,
Jens Axboeee56ad52008-02-01 10:30:20 +0100154 },
155 {
Jens Axboe111e0322013-03-07 11:31:20 +0100156 .name = (char *) "parse-only",
157 .has_arg = no_argument,
158 .val = 'P' | FIO_CLIENT_FLAG,
159 },
160 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200161 .name = (char *) "section",
Jens Axboe01f06b62008-02-18 20:53:47 +0100162 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200163 .val = 'x' | FIO_CLIENT_FLAG,
Jens Axboe01f06b62008-02-18 20:53:47 +0100164 },
165 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200166 .name = (char *) "alloc-size",
Jens Axboe2b386d22008-03-26 10:32:57 +0100167 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200168 .val = 'a' | FIO_CLIENT_FLAG,
Jens Axboe2b386d22008-03-26 10:32:57 +0100169 },
170 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200171 .name = (char *) "profile",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100172 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200173 .val = 'p' | FIO_CLIENT_FLAG,
Jens Axboe9ac8a792009-11-13 21:23:07 +0100174 },
175 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200176 .name = (char *) "warnings-fatal",
Jens Axboea9523c62011-01-17 16:49:54 -0700177 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200178 .val = 'w' | FIO_CLIENT_FLAG,
Jens Axboea9523c62011-01-17 16:49:54 -0700179 },
180 {
Jens Axboefca70352011-07-06 20:12:54 +0200181 .name = (char *) "max-jobs",
182 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200183 .val = 'j' | FIO_CLIENT_FLAG,
Jens Axboefca70352011-07-06 20:12:54 +0200184 },
185 {
Jens Axboef57a9c52011-09-09 21:01:37 +0200186 .name = (char *) "terse-version",
187 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200188 .val = 'V' | FIO_CLIENT_FLAG,
Jens Axboef57a9c52011-09-09 21:01:37 +0200189 },
190 {
Jens Axboe50d16972011-09-29 17:45:28 -0600191 .name = (char *) "server",
Jens Axboe87aa8f12011-10-06 21:24:13 +0200192 .has_arg = optional_argument,
Jens Axboe50d16972011-09-29 17:45:28 -0600193 .val = 'S',
194 },
Jens Axboee46d8092011-10-03 09:11:02 +0200195 { .name = (char *) "daemonize",
Jens Axboe402668f2011-10-10 15:28:58 +0200196 .has_arg = required_argument,
Jens Axboee46d8092011-10-03 09:11:02 +0200197 .val = 'D',
198 },
Jens Axboe50d16972011-09-29 17:45:28 -0600199 {
Jens Axboe132159a2011-09-30 15:01:32 -0600200 .name = (char *) "client",
201 .has_arg = required_argument,
202 .val = 'C',
203 },
204 {
Jens Axboe7d11f872012-12-17 12:03:29 +0100205 .name = (char *) "cpuclock-test",
206 .has_arg = no_argument,
207 .val = 'T',
208 },
209 {
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100210 .name = (char *) "idle-prof",
211 .has_arg = required_argument,
212 .val = 'I',
213 },
214 {
Jens Axboeb4692822006-10-27 13:43:22 +0200215 .name = NULL,
216 },
217};
218
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200219void free_threads_shm(void)
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200220{
221 struct shmid_ds sbuf;
222
223 if (threads) {
224 void *tp = threads;
225
226 threads = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200227 shmdt(tp);
228 shmctl(shm_id, IPC_RMID, &sbuf);
229 shm_id = -1;
230 }
231}
232
233void free_shm(void)
234{
235 if (threads) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200236 file_hash_exit();
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100237 flow_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200238 fio_debug_jobp = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200239 free_threads_shm();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200240 }
241
242 scleanup();
243}
244
245/*
246 * The thread area is shared between the main process and the job
247 * threads/processes. So setup a shared memory segment that will hold
248 * all the job info. We use the end of the region for keeping track of
249 * open files across jobs, for file sharing.
250 */
251static int setup_thread_area(void)
252{
253 void *hash;
254
255 if (threads)
256 return 0;
257
258 /*
259 * 1024 is too much on some machines, scale max_jobs if
260 * we get a failure that looks like too large a shm segment
261 */
262 do {
263 size_t size = max_jobs * sizeof(struct thread_data);
264
265 size += file_hash_size;
266 size += sizeof(unsigned int);
267
268 shm_id = shmget(0, size, IPC_CREAT | 0600);
269 if (shm_id != -1)
270 break;
Jens Axboef0c77f02012-09-21 15:05:45 +0200271 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200272 perror("shmget");
273 break;
274 }
275
276 max_jobs >>= 1;
277 } while (max_jobs);
278
279 if (shm_id == -1)
280 return 1;
281
282 threads = shmat(shm_id, NULL, 0);
283 if (threads == (void *) -1) {
284 perror("shmat");
285 return 1;
286 }
287
288 memset(threads, 0, max_jobs * sizeof(struct thread_data));
289 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
290 fio_debug_jobp = (void *) hash + file_hash_size;
291 *fio_debug_jobp = -1;
292 file_hash_init(hash);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100293
294 flow_init();
295
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200296 return 0;
297}
298
Jens Axboe906c8d72006-06-13 09:37:56 +0200299/*
300 * Return a free job structure.
301 */
Steven Langde890a12011-11-09 14:03:34 +0100302static struct thread_data *get_new_job(int global, struct thread_data *parent,
303 int preserve_eo)
Jens Axboeebac4652005-12-08 15:25:21 +0100304{
305 struct thread_data *td;
306
307 if (global)
308 return &def_thread;
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200309 if (setup_thread_area()) {
310 log_err("error: failed to setup shm segment\n");
311 return NULL;
312 }
Bruce Crane61f1ec2011-01-14 18:30:26 +0100313 if (thread_number >= max_jobs) {
314 log_err("error: maximum number of jobs (%d) reached.\n",
315 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100316 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100317 }
Jens Axboeebac4652005-12-08 15:25:21 +0100318
319 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200320 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100321
Steven Langde890a12011-11-09 14:03:34 +0100322 td->io_ops = NULL;
323 if (!preserve_eo)
324 td->eo = NULL;
325
Jens Axboee0b0d892009-12-08 10:10:14 +0100326 td->o.uid = td->o.gid = -1U;
327
Jens Axboecade3ef2007-03-23 15:29:45 +0100328 dup_files(td, parent);
Steven Langde890a12011-11-09 14:03:34 +0100329 fio_options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100330
Jens Axboe15dc1932010-03-05 10:59:06 +0100331 profile_add_hooks(td);
332
Jens Axboeebac4652005-12-08 15:25:21 +0100333 td->thread_number = thread_number;
Jens Axboe108fea72012-11-14 13:09:45 -0700334
335 if (!parent || !parent->o.group_reporting)
336 stat_number++;
337
Jens Axboeebac4652005-12-08 15:25:21 +0100338 return td;
339}
340
341static void put_job(struct thread_data *td)
342{
Jens Axboe549577a2006-10-30 12:23:41 +0100343 if (td == &def_thread)
344 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600345
Jens Axboe58c55ba2010-03-09 12:20:08 +0100346 profile_td_exit(td);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100347 flow_exit_job(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100348
Jens Axboe16edf252007-02-10 14:59:22 +0100349 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100350 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100351
Jens Axboe7e356b22011-10-14 10:55:16 +0200352 fio_options_free(td);
Steven Langde890a12011-11-09 14:03:34 +0100353 if (td->io_ops)
354 free_ioengine(td);
Jens Axboe7e356b22011-10-14 10:55:16 +0200355
Jens Axboeebac4652005-12-08 15:25:21 +0100356 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
357 thread_number--;
358}
359
Jens Axboe581e7142009-06-09 12:47:16 +0200360static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100361{
Jens Axboe581e7142009-06-09 12:47:16 +0200362 unsigned int bs = td->o.min_bs[ddir];
Jens Axboe127f6862007-03-15 12:09:57 +0100363
Jens Axboeff58fce2010-08-25 12:02:08 +0200364 assert(ddir_rw(ddir));
365
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100366 if (td->o.rate[ddir])
Steven Lang1b8dbf22011-11-09 13:48:01 +0100367 td->rate_bps[ddir] = td->o.rate[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100368 else
Steven Lang1b8dbf22011-11-09 13:48:01 +0100369 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100370
Steven Lang1b8dbf22011-11-09 13:48:01 +0100371 if (!td->rate_bps[ddir]) {
Jens Axboe127f6862007-03-15 12:09:57 +0100372 log_err("rate lower than supported\n");
373 return -1;
374 }
375
Jens Axboe581e7142009-06-09 12:47:16 +0200376 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100377 return 0;
378}
379
Jens Axboe581e7142009-06-09 12:47:16 +0200380static int setup_rate(struct thread_data *td)
381{
382 int ret = 0;
383
384 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
385 ret = __setup_rate(td, DDIR_READ);
386 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
387 ret |= __setup_rate(td, DDIR_WRITE);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200388 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
389 ret |= __setup_rate(td, DDIR_TRIM);
Jens Axboe581e7142009-06-09 12:47:16 +0200390
391 return ret;
392}
393
Jens Axboe83472392009-02-19 21:32:12 +0100394static int fixed_block_size(struct thread_options *o)
395{
396 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
397 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200398 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
399 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
400 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
Jens Axboe83472392009-02-19 21:32:12 +0100401}
402
Jens Axboedad915e2006-10-27 11:10:18 +0200403/*
404 * Lazy way of fixing up options that depend on each other. We could also
405 * define option callback handlers, but this is easier.
406 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100407static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200408{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100409 struct thread_options *o = &td->o;
Jens Axboeff217452011-01-18 22:33:02 -0700410 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200411
Jens Axboef356d012009-01-05 09:56:29 +0100412#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100413 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100414 log_info("fio: this platform does not support process shared"
415 " mutexes, forcing use of threads. Use the 'thread'"
416 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100417 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700418 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100419 }
420#endif
421
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100422 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200423 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100424 free(o->write_iolog_file);
425 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700426 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200427 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100428
Jens Axboe16b462a2006-10-30 12:35:18 +0100429 /*
Steven Noonaned335852012-01-31 13:58:00 +0100430 * only really works with 1 file
Jens Axboe16b462a2006-10-30 12:35:18 +0100431 */
Jens Axboe627aa1a2013-02-07 13:06:18 +0100432 if (o->zone_size && o->open_files > 1)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100433 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100434
435 /*
Steven Noonaned335852012-01-31 13:58:00 +0100436 * If zone_range isn't specified, backward compatibility dictates it
437 * should be made equal to zone_size.
438 */
439 if (o->zone_size && !o->zone_range)
440 o->zone_range = o->zone_size;
441
442 /*
Jens Axboe16b462a2006-10-30 12:35:18 +0100443 * Reads can do overwrites, we always need to pre-create the file
444 */
445 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100446 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100447
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100448 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100449 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100450 if (!o->max_bs[DDIR_READ])
451 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
452 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100453 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100454 if (!o->max_bs[DDIR_WRITE])
455 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200456 if (!o->min_bs[DDIR_TRIM])
457 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
458 if (!o->max_bs[DDIR_TRIM])
459 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
460
Jens Axboea00735e2006-11-03 08:58:08 +0100461
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100462 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200463 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
Jens Axboea00735e2006-11-03 08:58:08 +0100464
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100465 /*
466 * For random IO, allow blockalign offset other than min_bs.
467 */
468 if (!o->ba[DDIR_READ] || !td_random(td))
469 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
470 if (!o->ba[DDIR_WRITE] || !td_random(td))
471 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200472 if (!o->ba[DDIR_TRIM] || !td_random(td))
473 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100474
475 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200476 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
477 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100478 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100479 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100480 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700481 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100482 }
483
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100484 if (!o->file_size_high)
485 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100486
Jens Axboe83472392009-02-19 21:32:12 +0100487 if (o->norandommap && o->verify != VERIFY_NONE
488 && !fixed_block_size(o)) {
489 log_err("fio: norandommap given for variable block sizes, "
490 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100491 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700492 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100493 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100494 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100495 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100496
497 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100498 * thinktime_spin must be less than thinktime
499 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100500 if (o->thinktime_spin > o->thinktime)
501 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100502
503 /*
504 * The low water mark cannot be bigger than the iodepth
505 */
Jens Axboe67bf9822013-01-10 11:23:19 +0100506 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
507 o->iodepth_low = o->iodepth;
Jens Axboecb5ab512007-02-26 12:57:09 +0100508
509 /*
510 * If batch number isn't set, default to the same as iodepth
511 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100512 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
513 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100514
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100515 if (o->nr_files > td->files_index)
516 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100517
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100518 if (o->open_files > o->nr_files || !o->open_files)
519 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100520
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200521 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
522 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
523 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
524 (o->rate_iops_min[DDIR_READ] + o->rate_iops_min[DDIR_WRITE] + o->rate_iops_min[DDIR_TRIM]))) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100525 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700526 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100527 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200528 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
529 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
530 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
531 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
532 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
533 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100534 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700535 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100536 }
537
Jens Axboecf4464c2007-04-17 20:14:42 +0200538 if (!o->timeout && o->time_based) {
539 log_err("fio: time_based requires a runtime/timeout setting\n");
540 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700541 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200542 }
543
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100544 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200545 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100546
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100547 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700548 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700549 log_info("Multiple writers may overwrite blocks that "
550 "belong to other jobs. This can cause "
551 "verification failures.\n");
552 ret = warnings_fatal;
553 }
554
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100555 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200556 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
557 !o->verify_interval)
558 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboed9905882010-03-18 20:43:00 +0100559 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200560
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100561 if (o->pre_read) {
562 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700563 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200564 log_info("fio: cannot pre-read files with an IO engine"
565 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700566 ret = warnings_fatal;
567 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200568 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200569
Steven Noonanad705bc2013-04-08 15:05:25 -0700570 if (!o->unit_base) {
571 if (td->io_ops->flags & FIO_BIT_BASED)
572 o->unit_base = 1;
573 else
574 o->unit_base = 8;
575 }
576
Jens Axboe67bf9822013-01-10 11:23:19 +0100577#ifndef CONFIG_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100578 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700579 log_info("fio: this platform does not support fdatasync()"
580 " falling back to using fsync(). Use the 'fsync'"
581 " option instead of 'fdatasync' to get rid of"
582 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100583 o->fsync_blocks = o->fdatasync_blocks;
584 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700585 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700586 }
587#endif
588
Bruce Cran93bcfd22012-02-20 20:18:19 +0100589#ifdef WIN32
590 /*
591 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
592 * so fail if we're passed those flags
593 */
594 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
595 log_err("fio: Windows does not support direct or non-buffered io with"
596 " the synchronous ioengines. Use the 'windowsaio' ioengine"
597 " with 'direct=1' and 'iodepth=1' instead.\n");
598 ret = 1;
599 }
600#endif
601
Jens Axboe811ac502012-03-02 22:23:36 +0100602 /*
603 * For fully compressible data, just zero them at init time.
604 * It's faster than repeatedly filling it.
605 */
606 if (td->o.compress_percentage == 100) {
607 td->o.zero_buffers = 1;
608 td->o.compress_percentage = 0;
609 }
610
Jens Axboe5881fda2012-11-15 15:21:23 -0700611 /*
612 * Using a non-uniform random distribution excludes usage of
613 * a random map
614 */
615 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
616 td->o.norandommap = 1;
617
Jens Axboea9523c62011-01-17 16:49:54 -0700618 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200619}
620
Jens Axboe906c8d72006-06-13 09:37:56 +0200621/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100622 * This function leaks the buffer
623 */
Jens Axboe99d633a2012-03-15 15:55:04 +0100624char *fio_uint_to_kmg(unsigned int val)
Jens Axboef8977ee2006-11-06 14:03:03 +0100625{
626 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100627 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100628 char *p = post;
629
Jens Axboe245142f2006-11-08 12:49:21 +0100630 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100631 if (val & 1023)
632 break;
633
634 val >>= 10;
635 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100636 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100637
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100638 snprintf(buf, 32, "%u%c", val, *p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100639 return buf;
640}
641
Jens Axboe09629a92007-03-09 09:00:06 +0100642/* External engines are specified by "external:name.o") */
643static const char *get_engine_name(const char *str)
644{
645 char *p = strstr(str, ":");
646
647 if (!p)
648 return str;
649
650 p++;
651 strip_blank_front(&p);
652 strip_blank_end(p);
653 return p;
654}
655
Jens Axboee132cba2007-03-14 14:23:54 +0100656static int exists_and_not_file(const char *filename)
657{
658 struct stat sb;
659
660 if (lstat(filename, &sb) == -1)
661 return 0;
662
Bruce Cranecc314b2011-01-04 10:59:30 +0100663 /* \\.\ is the device namespace in Windows, where every file
664 * is a device node */
665 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100666 return 0;
667
668 return 1;
669}
670
Jens Axboe4c07ad82011-03-28 09:51:09 +0200671static void td_fill_rand_seeds_os(struct thread_data *td)
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200672{
Jens Axboed24945f2012-12-04 13:10:29 +0100673 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
674 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
675 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200676
677 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100678 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200679
Jens Axboed24945f2012-12-04 13:10:29 +0100680 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
681 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200682
683 if (!td_random(td))
684 return;
685
686 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100687 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200688
Jens Axboed24945f2012-12-04 13:10:29 +0100689 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200690}
691
692static void td_fill_rand_seeds_internal(struct thread_data *td)
693{
Jens Axboed24945f2012-12-04 13:10:29 +0100694 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
695 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
696 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200697
698 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100699 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200700
Jens Axboed24945f2012-12-04 13:10:29 +0100701 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
702 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200703
704 if (!td_random(td))
705 return;
706
707 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100708 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe4c07ad82011-03-28 09:51:09 +0200709
Jens Axboed24945f2012-12-04 13:10:29 +0100710 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200711}
712
Jens Axboe4c07ad82011-03-28 09:51:09 +0200713void td_fill_rand_seeds(struct thread_data *td)
714{
715 if (td->o.use_os_rand)
716 td_fill_rand_seeds_os(td);
717 else
718 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600719
Jens Axboed24945f2012-12-04 13:10:29 +0100720 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200721}
722
Jens Axboe9c60ce62007-03-15 09:14:47 +0100723/*
Steven Langde890a12011-11-09 14:03:34 +0100724 * Initializes the ioengine configured for a job, if it has not been done so
725 * already.
726 */
727int ioengine_load(struct thread_data *td)
728{
729 const char *engine;
730
731 /*
732 * Engine has already been loaded.
733 */
734 if (td->io_ops)
735 return 0;
736
737 engine = get_engine_name(td->o.ioengine);
738 td->io_ops = load_ioengine(td, engine);
739 if (!td->io_ops) {
740 log_err("fio: failed to load engine %s\n", engine);
741 return 1;
742 }
743
744 if (td->io_ops->option_struct_size && td->io_ops->options) {
745 /*
746 * In cases where td->eo is set, clone it for a child thread.
747 * This requires that the parent thread has the same ioengine,
748 * but that requirement must be enforced by the code which
749 * cloned the thread.
750 */
751 void *origeo = td->eo;
752 /*
753 * Otherwise use the default thread options.
754 */
755 if (!origeo && td != &def_thread && def_thread.eo &&
756 def_thread.io_ops->options == td->io_ops->options)
757 origeo = def_thread.eo;
758
759 options_init(td->io_ops->options);
760 td->eo = malloc(td->io_ops->option_struct_size);
761 /*
762 * Use the default thread as an option template if this uses the
763 * same options structure and there are non-default options
764 * used.
765 */
766 if (origeo) {
767 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
768 options_mem_dupe(td->eo, td->io_ops->options);
769 } else {
770 memset(td->eo, 0, td->io_ops->option_struct_size);
771 fill_default_options(td->eo, td->io_ops->options);
772 }
773 *(struct thread_data **)td->eo = td;
774 }
775
776 return 0;
777}
778
Jens Axboed72be542012-11-30 19:37:46 +0100779static void init_flags(struct thread_data *td)
780{
781 struct thread_options *o = &td->o;
782
783 if (o->verify_backlog)
784 td->flags |= TD_F_VER_BACKLOG;
785 if (o->trim_backlog)
786 td->flags |= TD_F_TRIM_BACKLOG;
787 if (o->read_iolog_file)
788 td->flags |= TD_F_READ_IOLOG;
789 if (o->refill_buffers)
790 td->flags |= TD_F_REFILL_BUFFERS;
791 if (o->scramble_buffers)
792 td->flags |= TD_F_SCRAMBLE_BUFFERS;
793 if (o->verify != VERIFY_NONE)
794 td->flags |= TD_F_VER_NONE;
795}
796
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700797static int setup_random_seeds(struct thread_data *td)
798{
799 unsigned long seed;
800 unsigned int i;
801
802 if (!td->o.rand_repeatable)
803 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
804
805 for (seed = 0x89, i = 0; i < 4; i++)
806 seed *= 0x9e370001UL;
807
808 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
809 td->rand_seeds[i] = seed;
810 seed *= 0x9e370001UL;
811 }
812
813 td_fill_rand_seeds(td);
814 return 0;
815}
816
Jens Axboede98bd32013-04-05 11:09:20 +0200817enum {
818 FPRE_NONE = 0,
819 FPRE_JOBNAME,
820 FPRE_JOBNUM,
821 FPRE_FILENUM
822};
823
824static struct fpre_keyword {
825 const char *keyword;
826 size_t strlen;
827 int key;
828} fpre_keywords[] = {
829 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
830 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
831 { .keyword = "$filenum", .key = FPRE_FILENUM, },
832 { .keyword = NULL, },
833 };
834
835static char *make_filename(char *buf, struct thread_options *o,
836 const char *jobname, int jobnum, int filenum)
837{
838 struct fpre_keyword *f;
839 char copy[PATH_MAX];
840
841 if (!o->filename_format || !strlen(o->filename_format)) {
842 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
843 return NULL;
844 }
845
846 for (f = &fpre_keywords[0]; f->keyword; f++)
847 f->strlen = strlen(f->keyword);
848
849 strcpy(buf, o->filename_format);
850 memset(copy, 0, sizeof(copy));
851 for (f = &fpre_keywords[0]; f->keyword; f++) {
852 do {
853 size_t pre_len, post_start = 0;
854 char *str, *dst = copy;
855
Jens Axboee25b6d52013-04-05 14:44:51 +0200856 str = strcasestr(buf, f->keyword);
Jens Axboede98bd32013-04-05 11:09:20 +0200857 if (!str)
858 break;
859
860 pre_len = str - buf;
861 if (strlen(str) != f->strlen)
862 post_start = pre_len + f->strlen;
863
864 if (pre_len) {
865 strncpy(dst, buf, pre_len);
866 dst += pre_len;
867 }
868
869 switch (f->key) {
870 case FPRE_JOBNAME:
871 dst += sprintf(dst, "%s", jobname);
872 break;
873 case FPRE_JOBNUM:
874 dst += sprintf(dst, "%d", jobnum);
875 break;
876 case FPRE_FILENUM:
877 dst += sprintf(dst, "%d", filenum);
878 break;
879 default:
880 assert(0);
881 break;
882 }
883
884 if (post_start)
885 strcpy(dst, buf + post_start);
886
887 strcpy(buf, copy);
888 } while (1);
889 }
890
891 return buf;
892}
Steven Langde890a12011-11-09 14:03:34 +0100893/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200894 * Adds a job to the list of things todo. Sanitizes the various options
895 * to make sure we don't have conflicts, and initializes various
896 * members of td.
897 */
Jens Axboeb7cfb2e2012-03-12 07:47:27 +0100898static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
Jens Axboe46bcd492012-03-14 11:31:21 +0100899 int recursed, int client_type)
Jens Axboeebac4652005-12-08 15:25:21 +0100900{
Jens Axboeaf52b342007-03-13 10:07:47 +0100901 unsigned int i;
Jens Axboeaf52b342007-03-13 10:07:47 +0100902 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100903 int numjobs, file_alloced;
Jens Axboede98bd32013-04-05 11:09:20 +0200904 struct thread_options *o = &td->o;
Jens Axboeebac4652005-12-08 15:25:21 +0100905
Jens Axboeebac4652005-12-08 15:25:21 +0100906 /*
907 * the def_thread is just for options, it's not a real job
908 */
909 if (td == &def_thread)
910 return 0;
911
Jens Axboed72be542012-11-30 19:37:46 +0100912 init_flags(td);
913
Jens Axboecca73aa2007-04-04 11:09:19 +0200914 /*
915 * if we are just dumping the output command line, don't add the job
916 */
Jens Axboe111e0322013-03-07 11:31:20 +0100917 if (dump_cmdline || parse_only) {
Jens Axboecca73aa2007-04-04 11:09:19 +0200918 put_job(td);
919 return 0;
920 }
921
Jens Axboe46bcd492012-03-14 11:31:21 +0100922 td->client_type = client_type;
923
Jens Axboe58c55ba2010-03-09 12:20:08 +0100924 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +0200925 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +0100926
Steven Langde890a12011-11-09 14:03:34 +0100927 if (ioengine_load(td))
Jens Axboe205927a2007-03-15 11:06:32 +0100928 goto err;
Jens Axboedf641192006-10-12 07:55:41 +0200929
Jens Axboede98bd32013-04-05 11:09:20 +0200930 if (o->odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100931 td->io_ops->flags |= FIO_RAWIO;
932
Jens Axboee132cba2007-03-14 14:23:54 +0100933 file_alloced = 0;
Jens Axboede98bd32013-04-05 11:09:20 +0200934 if (!o->filename && !td->files_index && !o->read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100935 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100936
Jens Axboede98bd32013-04-05 11:09:20 +0200937 if (o->nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100938 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100939 else {
Jens Axboede98bd32013-04-05 11:09:20 +0200940 for (i = 0; i < o->nr_files; i++)
941 add_file(td, make_filename(fname, o, jobname, td->thread_number, i));
Jens Axboeaf52b342007-03-13 10:07:47 +0100942 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100943 }
Jens Axboeebac4652005-12-08 15:25:21 +0100944
Jens Axboe4e991c22007-03-15 11:41:11 +0100945 if (fixup_options(td))
946 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100947
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100948 flow_init_job(td);
949
Steven Langde890a12011-11-09 14:03:34 +0100950 /*
951 * IO engines only need this for option callbacks, and the address may
952 * change in subprocesses.
953 */
954 if (td->eo)
955 *(struct thread_data **)td->eo = NULL;
956
Jens Axboe07eb79d2007-04-12 13:02:38 +0200957 if (td->io_ops->flags & FIO_DISKLESSIO) {
958 struct fio_file *f;
959
960 for_each_file(td, f, i)
961 f->real_file_size = -1ULL;
962 }
963
Jens Axboe521da522012-08-02 11:21:36 +0200964 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboeebac4652005-12-08 15:25:21 +0100965
Jens Axboede98bd32013-04-05 11:09:20 +0200966 td->ts.clat_percentiles = o->clat_percentiles;
967 td->ts.percentile_precision = o->percentile_precision;
968 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
Yu-ju Hong83349192011-08-13 00:53:44 +0200969
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200970 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
971 td->ts.clat_stat[i].min_val = ULONG_MAX;
972 td->ts.slat_stat[i].min_val = ULONG_MAX;
973 td->ts.lat_stat[i].min_val = ULONG_MAX;
974 td->ts.bw_stat[i].min_val = ULONG_MAX;
975 }
Jens Axboede98bd32013-04-05 11:09:20 +0200976 td->ddir_seq_nr = o->ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100977
Jens Axboede98bd32013-04-05 11:09:20 +0200978 if ((o->stonewall || o->new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100979 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100980 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100981 }
Jens Axboeebac4652005-12-08 15:25:21 +0100982
983 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100984 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100985
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700986 if (setup_random_seeds(td)) {
Bruce Cran93bcfd22012-02-20 20:18:19 +0100987 td_verror(td, errno, "init_random_state");
Jens Axboe9c60ce62007-03-15 09:14:47 +0100988 goto err;
Bruce Cran93bcfd22012-02-20 20:18:19 +0100989 }
Jens Axboe9c60ce62007-03-15 09:14:47 +0100990
Jens Axboeebac4652005-12-08 15:25:21 +0100991 if (setup_rate(td))
992 goto err;
993
Jens Axboe22f80452013-04-09 20:29:16 +0200994 if (o->lat_log_file) {
995 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
996 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
997 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
Jens Axboeebac4652005-12-08 15:25:21 +0100998 }
Jens Axboe22f80452013-04-09 20:29:16 +0200999 if (o->bw_log_file)
1000 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1001 if (o->iops_log_file)
1002 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
Jens Axboeebac4652005-12-08 15:25:21 +01001003
Jens Axboede98bd32013-04-05 11:09:20 +02001004 if (!o->name)
1005 o->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +02001006
Jens Axboef3afa572012-09-17 13:34:16 +02001007 if (output_format == FIO_OUTPUT_NORMAL) {
Jens Axboeb990b5c2006-09-14 09:48:22 +02001008 if (!job_add_num) {
Jens Axboeb7cfb2e2012-03-12 07:47:27 +01001009 if (is_backend && !recursed)
Jens Axboe2f122b12012-03-15 13:10:19 +01001010 fio_server_send_add_job(td);
Jens Axboe807f9972012-03-02 10:25:24 +01001011
Jens Axboe03530502012-03-19 21:45:12 +01001012 if (!(td->io_ops->flags & FIO_NOIO)) {
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001013 char *c1, *c2, *c3, *c4, *c5, *c6;
Jens Axboef8977ee2006-11-06 14:03:03 +01001014
Jens Axboe22f80452013-04-09 20:29:16 +02001015 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1016 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1017 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1018 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
1019 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1020 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
Jens Axboef8977ee2006-11-06 14:03:03 +01001021
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001022 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s/%s-%s,"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001023 " ioengine=%s, iodepth=%u\n",
1024 td->o.name, td->groupid,
Jens Axboe22f80452013-04-09 20:29:16 +02001025 ddir_str(o->td_ddir),
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001026 c1, c2, c3, c4, c5, c6,
Jens Axboede98bd32013-04-05 11:09:20 +02001027 td->io_ops->name, o->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +01001028
1029 free(c1);
1030 free(c2);
1031 free(c3);
1032 free(c4);
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001033 free(c5);
1034 free(c6);
Jens Axboef8977ee2006-11-06 14:03:03 +01001035 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001036 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +01001037 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001038 }
Jens Axboeebac4652005-12-08 15:25:21 +01001039
1040 /*
1041 * recurse add identical jobs, clear numjobs and stonewall options
1042 * as they don't apply to sub-jobs
1043 */
Jens Axboede98bd32013-04-05 11:09:20 +02001044 numjobs = o->numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +01001045 while (--numjobs) {
Steven Langde890a12011-11-09 14:03:34 +01001046 struct thread_data *td_new = get_new_job(0, td, 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001047
1048 if (!td_new)
1049 goto err;
1050
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001051 td_new->o.numjobs = 1;
1052 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +02001053 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +01001054
1055 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001056 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +01001057 td_new->files_index = 0;
Jens Axboe4e6ea2f2009-03-04 20:17:22 +01001058 td_new->files_size = 0;
Jens Axboee132cba2007-03-14 14:23:54 +01001059 td_new->files = NULL;
1060 }
1061
Jens Axboe75154842006-06-01 13:56:09 +02001062 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001063
Jens Axboe46bcd492012-03-14 11:31:21 +01001064 if (add_job(td_new, jobname, job_add_num, 1, client_type))
Jens Axboeebac4652005-12-08 15:25:21 +01001065 goto err;
1066 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001067
Jens Axboeebac4652005-12-08 15:25:21 +01001068 return 0;
1069err:
1070 put_job(td);
1071 return -1;
1072}
1073
Jens Axboe79d16312010-03-04 12:43:20 +01001074/*
1075 * Parse as if 'o' was a command line
1076 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001077void add_job_opts(const char **o, int client_type)
Jens Axboe79d16312010-03-04 12:43:20 +01001078{
1079 struct thread_data *td, *td_parent;
1080 int i, in_global = 1;
1081 char jobname[32];
1082
1083 i = 0;
1084 td_parent = td = NULL;
1085 while (o[i]) {
1086 if (!strncmp(o[i], "name", 4)) {
1087 in_global = 0;
1088 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001089 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001090 td = NULL;
1091 sprintf(jobname, "%s", o[i] + 5);
1092 }
1093 if (in_global && !td_parent)
Steven Langde890a12011-11-09 14:03:34 +01001094 td_parent = get_new_job(1, &def_thread, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001095 else if (!in_global && !td) {
1096 if (!td_parent)
1097 td_parent = &def_thread;
Steven Langde890a12011-11-09 14:03:34 +01001098 td = get_new_job(0, td_parent, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001099 }
1100 if (in_global)
1101 fio_options_parse(td_parent, (char **) &o[i], 1);
1102 else
1103 fio_options_parse(td, (char **) &o[i], 1);
1104 i++;
1105 }
1106
1107 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001108 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001109}
1110
Jens Axboe01f06b62008-02-18 20:53:47 +01001111static int skip_this_section(const char *name)
1112{
Jens Axboead0a2732011-03-11 10:16:17 +01001113 int i;
1114
1115 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +01001116 return 0;
1117 if (!strncmp(name, "global", 6))
1118 return 0;
1119
Jens Axboead0a2732011-03-11 10:16:17 +01001120 for (i = 0; i < nr_job_sections; i++)
1121 if (!strcmp(job_sections[i], name))
1122 return 0;
1123
1124 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001125}
1126
Jens Axboeebac4652005-12-08 15:25:21 +01001127static int is_empty_or_comment(char *line)
1128{
1129 unsigned int i;
1130
1131 for (i = 0; i < strlen(line); i++) {
1132 if (line[i] == ';')
1133 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001134 if (line[i] == '#')
1135 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +02001136 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +01001137 return 0;
1138 }
1139
1140 return 1;
1141}
1142
Jens Axboe313cb202006-12-21 09:50:00 +01001143/*
Jens Axboe07261982006-06-07 10:51:12 +02001144 * This is our [ini] type file parser.
1145 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001146int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
Jens Axboeebac4652005-12-08 15:25:21 +01001147{
Jens Axboee1f36502006-10-27 10:54:08 +02001148 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001149 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001150 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001151 FILE *f;
1152 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001153 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +02001154 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +02001155 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +01001156 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001157 char **opts;
1158 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +01001159
Jens Axboe50d16972011-09-29 17:45:28 -06001160 if (is_buf)
1161 f = NULL;
1162 else {
1163 if (!strcmp(file, "-"))
1164 f = stdin;
1165 else
1166 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001167
Jens Axboe50d16972011-09-29 17:45:28 -06001168 if (!f) {
1169 perror("fopen job file");
1170 return 1;
1171 }
Jens Axboeebac4652005-12-08 15:25:21 +01001172 }
1173
1174 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +02001175
1176 /*
1177 * it's really 256 + small bit, 280 should suffice
1178 */
1179 name = malloc(280);
1180 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +01001181
Jens Axboe3b8b7132008-06-10 19:46:23 +02001182 alloc_opts = 8;
1183 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +02001184 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001185
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001186 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001187 do {
Jens Axboeccf8f122007-09-27 10:48:55 +02001188 /*
1189 * if skip_fgets is set, we already have loaded a line we
1190 * haven't handled.
1191 */
1192 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -06001193 if (is_buf)
1194 p = strsep(&file, "\n");
1195 else
Jens Axboe43f74792011-10-15 14:37:26 +02001196 p = fgets(string, 4096, f);
Jens Axboeccf8f122007-09-27 10:48:55 +02001197 if (!p)
1198 break;
1199 }
gurudas paicdc7f192007-03-29 10:10:56 +02001200
Jens Axboeccf8f122007-09-27 10:48:55 +02001201 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +02001202 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001203 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +02001204
Jens Axboeebac4652005-12-08 15:25:21 +01001205 if (is_empty_or_comment(p))
1206 continue;
Bruce Cran84dd1882011-05-05 08:14:09 -06001207 if (sscanf(p, "[%255[^\n]]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001208 if (inside_skip)
1209 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001210 log_err("fio: option <%s> outside of [] job section\n",
1211 p);
Jens Axboe088b4202007-07-19 14:53:01 +02001212 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001213 }
Jens Axboeebac4652005-12-08 15:25:21 +01001214
Jens Axboe7a4b80a2010-05-22 20:43:11 +02001215 name[strlen(name) - 1] = '\0';
1216
Jens Axboe01f06b62008-02-18 20:53:47 +01001217 if (skip_this_section(name)) {
1218 inside_skip = 1;
1219 continue;
1220 } else
1221 inside_skip = 0;
1222
Jens Axboeebac4652005-12-08 15:25:21 +01001223 global = !strncmp(name, "global", 6);
1224
Jens Axboecca73aa2007-04-04 11:09:19 +02001225 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +02001226 if (first_sect)
1227 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +02001228 if (!global)
1229 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +02001230 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001231 }
1232
Steven Langde890a12011-11-09 14:03:34 +01001233 td = get_new_job(global, &def_thread, 0);
Jens Axboe45410ac2006-06-07 11:10:39 +02001234 if (!td) {
1235 ret = 1;
1236 break;
1237 }
Jens Axboeebac4652005-12-08 15:25:21 +01001238
Jens Axboe972cfd22006-06-09 11:08:56 +02001239 /*
1240 * Seperate multiple job files by a stonewall
1241 */
Jens Axboef9481912006-06-09 11:37:28 +02001242 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001243 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +02001244 stonewall = 0;
1245 }
1246
Jens Axboe3b8b7132008-06-10 19:46:23 +02001247 num_opts = 0;
1248 memset(opts, 0, alloc_opts * sizeof(char *));
1249
Jens Axboe50d16972011-09-29 17:45:28 -06001250 while (1) {
1251 if (is_buf)
1252 p = strsep(&file, "\n");
1253 else
1254 p = fgets(string, 4096, f);
1255 if (!p)
1256 break;
1257
Jens Axboeebac4652005-12-08 15:25:21 +01001258 if (is_empty_or_comment(p))
1259 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001260
Jens Axboeb6754f92006-05-30 14:29:32 +02001261 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001262
Jens Axboeccf8f122007-09-27 10:48:55 +02001263 /*
1264 * new section, break out and make sure we don't
1265 * fgets() a new line at the top.
1266 */
1267 if (p[0] == '[') {
1268 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001269 break;
Jens Axboeccf8f122007-09-27 10:48:55 +02001270 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001271
Jens Axboe4ae3f762006-05-30 13:35:14 +02001272 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001273
Jens Axboe3b8b7132008-06-10 19:46:23 +02001274 if (num_opts == alloc_opts) {
1275 alloc_opts <<= 1;
1276 opts = realloc(opts,
1277 alloc_opts * sizeof(char *));
1278 }
1279
1280 opts[num_opts] = strdup(p);
1281 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +01001282 }
Jens Axboeebac4652005-12-08 15:25:21 +01001283
Jens Axboe3b8b7132008-06-10 19:46:23 +02001284 ret = fio_options_parse(td, opts, num_opts);
1285 if (!ret) {
1286 if (dump_cmdline)
1287 for (i = 0; i < num_opts; i++)
1288 log_info("--%s ", opts[i]);
1289
Jens Axboe46bcd492012-03-14 11:31:21 +01001290 ret = add_job(td, name, 0, 0, type);
Jens Axboe3b8b7132008-06-10 19:46:23 +02001291 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +01001292 log_err("fio: job %s dropped\n", name);
1293 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001294 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001295
1296 for (i = 0; i < num_opts; i++)
1297 free(opts[i]);
1298 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001299 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001300
Jens Axboecca73aa2007-04-04 11:09:19 +02001301 if (dump_cmdline)
1302 log_info("\n");
1303
Jens Axboe7e356b22011-10-14 10:55:16 +02001304 i = 0;
1305 while (i < nr_job_sections) {
1306 free(job_sections[i]);
1307 i++;
1308 }
1309
Jens Axboe3b8b7132008-06-10 19:46:23 +02001310 for (i = 0; i < num_opts; i++)
1311 free(opts[i]);
1312
Jens Axboeebac4652005-12-08 15:25:21 +01001313 free(string);
1314 free(name);
Jens Axboe25775942008-06-10 20:26:06 +02001315 free(opts);
Jens Axboe50d16972011-09-29 17:45:28 -06001316 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001317 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001318 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001319}
1320
1321static int fill_def_thread(void)
1322{
1323 memset(&def_thread, 0, sizeof(def_thread));
1324
Jens Axboe375b2692007-05-22 09:13:02 +02001325 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboe8c029372011-10-05 09:54:30 +02001326 def_thread.o.timeout = def_timeout;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001327 def_thread.o.error_dump = 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001328 /*
Jens Axboeee738492007-01-10 11:23:16 +01001329 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001330 */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001331 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001332 return 0;
1333}
1334
Jens Axboe45378b32007-12-19 13:54:38 +01001335static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02001336{
Jens Axboe3d433822012-03-21 22:25:22 +01001337 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01001338 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboe83881282011-10-17 19:58:51 +02001339 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1340 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
1341 "\t\t\tdiskutil,job,mutex,profile,time,net\n");
Jens Axboe111e0322013-03-07 11:31:20 +01001342 printf(" --parse-only\t\tParse options only, don't start any IO\n");
Jens Axboe83881282011-10-17 19:58:51 +02001343 printf(" --output\t\tWrite output to file\n");
liang xieb2cecdc2012-08-31 08:22:42 -07001344 printf(" --runtime\t\tRuntime in seconds\n");
Jens Axboe83881282011-10-17 19:58:51 +02001345 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1346 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1347 printf(" --minimal\t\tMinimal (terse) output\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001348 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
Jens Axboe75db6e22011-11-07 22:07:36 +01001349 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001350 printf(" --version\t\tPrint version info and exit\n");
Jens Axboe83881282011-10-17 19:58:51 +02001351 printf(" --help\t\tPrint this page\n");
Jens Axboe23893642012-12-17 14:44:08 +01001352 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
Jens Axboe83881282011-10-17 19:58:51 +02001353 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001354 " them\n");
Steven Langde890a12011-11-09 14:03:34 +01001355 printf(" --enghelp=engine\tPrint ioengine help, or list"
1356 " available ioengines\n");
1357 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1358 " cmd\n");
Jens Axboe83881282011-10-17 19:58:51 +02001359 printf(" --showcmd\t\tTurn a job file into command line options\n");
1360 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1361 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboee382e662013-02-22 20:48:56 +01001362 printf(" --eta-newline=time\tForce a new line for every 'time'");
1363 printf(" period passed\n");
Jens Axboe83881282011-10-17 19:58:51 +02001364 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001365 " writes\n");
Jens Axboe83881282011-10-17 19:58:51 +02001366 printf(" --section=name\tOnly run specified section in job file\n");
1367 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
Jens Axboe2b386d22008-03-26 10:32:57 +01001368 " (def 1024)\n");
Jens Axboe83881282011-10-17 19:58:51 +02001369 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1370 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1371 printf(" --server=args\t\tStart a backend fio server\n");
1372 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1373 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001374 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1375 "\t\t\t(option=system,percpu) or run unit work\n"
1376 "\t\t\tcalibration only (option=calibrate)\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001377 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
Jens Axboe23893642012-12-17 14:44:08 +01001378 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001379}
1380
Jens Axboe79e48f72008-02-01 20:37:09 +01001381#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001382struct debug_level debug_levels[] = {
Jens Axboe0b8d11e2012-03-02 19:44:15 +01001383 { .name = "process",
1384 .help = "Process creation/exit logging",
1385 .shift = FD_PROCESS,
1386 },
1387 { .name = "file",
1388 .help = "File related action logging",
1389 .shift = FD_FILE,
1390 },
1391 { .name = "io",
1392 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1393 .shift = FD_IO,
1394 },
1395 { .name = "mem",
1396 .help = "Memory allocation/freeing logging",
1397 .shift = FD_MEM,
1398 },
1399 { .name = "blktrace",
1400 .help = "blktrace action logging",
1401 .shift = FD_BLKTRACE,
1402 },
1403 { .name = "verify",
1404 .help = "IO verification action logging",
1405 .shift = FD_VERIFY,
1406 },
1407 { .name = "random",
1408 .help = "Random generation logging",
1409 .shift = FD_RANDOM,
1410 },
1411 { .name = "parse",
1412 .help = "Parser logging",
1413 .shift = FD_PARSE,
1414 },
1415 { .name = "diskutil",
1416 .help = "Disk utility logging actions",
1417 .shift = FD_DISKUTIL,
1418 },
1419 { .name = "job",
1420 .help = "Logging related to creating/destroying jobs",
1421 .shift = FD_JOB,
1422 },
1423 { .name = "mutex",
1424 .help = "Mutex logging",
1425 .shift = FD_MUTEX
1426 },
1427 { .name = "profile",
1428 .help = "Logging related to profiles",
1429 .shift = FD_PROFILE,
1430 },
1431 { .name = "time",
1432 .help = "Logging related to time keeping functions",
1433 .shift = FD_TIME,
1434 },
1435 { .name = "net",
1436 .help = "Network logging",
1437 .shift = FD_NET,
1438 },
Jens Axboe02444ad2008-10-07 11:33:00 +02001439 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001440};
1441
Jens Axboec09823a2008-02-19 20:16:57 +01001442static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001443{
1444 struct debug_level *dl;
1445 char *p = (char *) string;
1446 char *opt;
1447 int i;
1448
1449 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001450 log_info("fio: dumping debug options:");
1451 for (i = 0; debug_levels[i].name; i++) {
1452 dl = &debug_levels[i];
1453 log_info("%s,", dl->name);
1454 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001455 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001456 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001457 }
1458
1459 while ((opt = strsep(&p, ",")) != NULL) {
1460 int found = 0;
1461
Jens Axboe5e1d3062008-05-23 11:55:53 +02001462 if (!strncmp(opt, "all", 3)) {
1463 log_info("fio: set all debug options\n");
1464 fio_debug = ~0UL;
1465 continue;
1466 }
1467
Jens Axboeee56ad52008-02-01 10:30:20 +01001468 for (i = 0; debug_levels[i].name; i++) {
1469 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001470 found = !strncmp(opt, dl->name, strlen(dl->name));
1471 if (!found)
1472 continue;
1473
1474 if (dl->shift == FD_JOB) {
1475 opt = strchr(opt, ':');
1476 if (!opt) {
1477 log_err("fio: missing job number\n");
1478 break;
1479 }
1480 opt++;
1481 fio_debug_jobno = atoi(opt);
1482 log_info("fio: set debug jobno %d\n",
1483 fio_debug_jobno);
1484 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001485 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001486 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001487 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001488 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001489 }
1490
1491 if (!found)
1492 log_err("fio: debug mask %s not found\n", opt);
1493 }
Jens Axboec09823a2008-02-19 20:16:57 +01001494 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001495}
Jens Axboe79e48f72008-02-01 20:37:09 +01001496#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001497static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001498{
1499 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001500 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001501}
1502#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001503
Jens Axboe4c6107f2011-01-18 05:22:22 -07001504static void fio_options_fill_optstring(void)
1505{
1506 char *ostr = cmd_optstr;
1507 int i, c;
1508
1509 c = i = 0;
1510 while (l_opts[i].name) {
1511 ostr[c++] = l_opts[i].val;
1512 if (l_opts[i].has_arg == required_argument)
1513 ostr[c++] = ':';
1514 else if (l_opts[i].has_arg == optional_argument) {
1515 ostr[c++] = ':';
1516 ostr[c++] = ':';
1517 }
1518 i++;
1519 }
1520 ostr[c] = '\0';
1521}
1522
Jens Axboec2c94582011-10-05 20:31:30 +02001523static int client_flag_set(char c)
1524{
1525 int i;
1526
1527 i = 0;
1528 while (l_opts[i].name) {
1529 int val = l_opts[i].val;
1530
1531 if (c == (val & 0xff))
1532 return (val & FIO_CLIENT_FLAG);
1533
1534 i++;
1535 }
1536
1537 return 0;
1538}
1539
Jens Axboefa2ea802011-10-08 21:07:29 +02001540void parse_cmd_client(void *client, char *opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +02001541{
Jens Axboefa2ea802011-10-08 21:07:29 +02001542 fio_client_add_cmd_option(client, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001543}
1544
Jens Axboe46bcd492012-03-14 11:31:21 +01001545int parse_cmd_line(int argc, char *argv[], int client_type)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001546{
1547 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001548 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001549 char *ostr = cmd_optstr;
Jens Axboe402668f2011-10-10 15:28:58 +02001550 void *pid_file = NULL;
Jens Axboe9bae26e2011-10-07 10:07:22 +02001551 void *cur_client = NULL;
Jens Axboe81179ee2011-10-04 12:42:06 +02001552 int backend = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001553
Jens Axboec2c94582011-10-05 20:31:30 +02001554 /*
1555 * Reset optind handling, since we may call this multiple times
1556 * for the backend.
1557 */
1558 optind = 1;
Jens Axboe7a4b8242011-10-05 17:35:15 +02001559
Jens Axboec2c94582011-10-05 20:31:30 +02001560 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
Jens Axboe085399d2011-10-06 09:01:22 +02001561 did_arg = 1;
1562
Jens Axboec2c94582011-10-05 20:31:30 +02001563 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
Jens Axboefa2ea802011-10-08 21:07:29 +02001564 parse_cmd_client(cur_client, argv[optind - 1]);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001565 c &= ~FIO_CLIENT_FLAG;
1566 }
1567
Jens Axboe214e1ec2007-03-15 10:48:13 +01001568 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001569 case 'a':
1570 smalloc_pool_size = atoi(optarg);
1571 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001572 case 't':
Jens Axboe8c029372011-10-05 09:54:30 +02001573 def_timeout = atoi(optarg);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001574 break;
1575 case 'l':
1576 write_lat_log = 1;
1577 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001578 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001579 write_bw_log = 1;
1580 break;
1581 case 'o':
1582 f_out = fopen(optarg, "w+");
1583 if (!f_out) {
1584 perror("fopen output");
1585 exit(1);
1586 }
1587 f_err = f_out;
1588 break;
1589 case 'm':
Jens Axboef3afa572012-09-17 13:34:16 +02001590 output_format = FIO_OUTPUT_TERSE;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001591 break;
Jens Axboef3afa572012-09-17 13:34:16 +02001592 case 'F':
1593 if (!strcmp(optarg, "minimal") ||
1594 !strcmp(optarg, "terse") ||
1595 !strcmp(optarg, "csv"))
1596 output_format = FIO_OUTPUT_TERSE;
1597 else if (!strcmp(optarg, "json"))
1598 output_format = FIO_OUTPUT_JSON;
1599 else
1600 output_format = FIO_OUTPUT_NORMAL;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001601 break;
1602 case 'h':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001603 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001604 usage(argv[0]);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001605 do_exit++;
1606 }
Jens Axboec2c94582011-10-05 20:31:30 +02001607 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001608 case 'c':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001609 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001610 fio_show_option_help(optarg);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001611 do_exit++;
1612 }
Jens Axboec2c94582011-10-05 20:31:30 +02001613 break;
Steven Langde890a12011-11-09 14:03:34 +01001614 case 'i':
1615 if (!cur_client) {
1616 fio_show_ioengine_help(optarg);
1617 do_exit++;
1618 }
1619 break;
Jens Axboecca73aa2007-04-04 11:09:19 +02001620 case 's':
1621 dump_cmdline = 1;
1622 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001623 case 'r':
1624 read_only = 1;
1625 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001626 case 'v':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001627 if (!cur_client) {
Jens Axboe3d433822012-03-21 22:25:22 +01001628 log_info("%s\n", fio_version_string);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001629 do_exit++;
1630 }
Jens Axboec2c94582011-10-05 20:31:30 +02001631 break;
Jens Axboef57a9c52011-09-09 21:01:37 +02001632 case 'V':
1633 terse_version = atoi(optarg);
Jens Axboe09786f52012-10-04 17:06:49 +02001634 if (!(terse_version == 2 || terse_version == 3 ||
1635 terse_version == 4)) {
Jens Axboef57a9c52011-09-09 21:01:37 +02001636 log_err("fio: bad terse version format\n");
1637 exit_val = 1;
1638 do_exit++;
1639 }
1640 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001641 case 'e':
1642 if (!strcmp("always", optarg))
1643 eta_print = FIO_ETA_ALWAYS;
1644 else if (!strcmp("never", optarg))
1645 eta_print = FIO_ETA_NEVER;
1646 break;
Jens Axboee382e662013-02-22 20:48:56 +01001647 case 'E': {
1648 long long t = 0;
1649
1650 if (str_to_decimal(optarg, &t, 0, NULL)) {
1651 log_err("fio: failed parsing eta time %s\n", optarg);
1652 exit_val = 1;
1653 do_exit++;
1654 }
1655 eta_new_line = t;
1656 break;
1657 }
Jens Axboeee56ad52008-02-01 10:30:20 +01001658 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001659 if (set_debug(optarg))
1660 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001661 break;
Jens Axboe111e0322013-03-07 11:31:20 +01001662 case 'P':
1663 parse_only = 1;
1664 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001665 case 'x': {
1666 size_t new_size;
1667
Jens Axboe01f06b62008-02-18 20:53:47 +01001668 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001669 log_err("fio: can't use global as only "
1670 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001671 do_exit++;
1672 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001673 break;
1674 }
Jens Axboead0a2732011-03-11 10:16:17 +01001675 new_size = (nr_job_sections + 1) * sizeof(char *);
1676 job_sections = realloc(job_sections, new_size);
1677 job_sections[nr_job_sections] = strdup(optarg);
1678 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01001679 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001680 }
Jens Axboe9ac8a792009-11-13 21:23:07 +01001681 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001682 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001683 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001684 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001685 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001686 char *val = optarg;
1687
1688 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe46bcd492012-03-14 11:31:21 +01001689 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe66251552011-05-12 10:14:57 +02001690 if (ret)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001691 return 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001692 td = NULL;
1693 }
1694 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001695 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001696 int global = 0;
1697
Jens Axboe01f06b62008-02-18 20:53:47 +01001698 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001699 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001700
Jens Axboe01f06b62008-02-18 20:53:47 +01001701 if (is_section && skip_this_section(val))
1702 continue;
1703
Steven Langde890a12011-11-09 14:03:34 +01001704 td = get_new_job(global, &def_thread, 1);
1705 if (!td || ioengine_load(td))
Jens Axboe214e1ec2007-03-15 10:48:13 +01001706 return 0;
Steven Langde890a12011-11-09 14:03:34 +01001707 fio_options_set_ioengine_opts(l_opts, td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001708 }
1709
1710 ret = fio_cmd_option_parse(td, opt, val);
Steven Langde890a12011-11-09 14:03:34 +01001711
1712 if (!ret && !strcmp(opt, "ioengine")) {
1713 free_ioengine(td);
1714 if (ioengine_load(td))
1715 return 0;
1716 fio_options_set_ioengine_opts(l_opts, td);
1717 }
1718 break;
1719 }
1720 case FIO_GETOPT_IOENGINE: {
1721 const char *opt = l_opts[lidx].name;
1722 char *val = optarg;
1723 opt = l_opts[lidx].name;
1724 val = optarg;
1725 ret = fio_cmd_ioengine_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001726 break;
1727 }
Jens Axboea9523c62011-01-17 16:49:54 -07001728 case 'w':
1729 warnings_fatal = 1;
1730 break;
Jens Axboefca70352011-07-06 20:12:54 +02001731 case 'j':
1732 max_jobs = atoi(optarg);
1733 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1734 log_err("fio: invalid max jobs: %d\n", max_jobs);
1735 do_exit++;
1736 exit_val = 1;
1737 }
1738 break;
Jens Axboe50d16972011-09-29 17:45:28 -06001739 case 'S':
Jens Axboea37f69b2011-10-01 12:24:21 -06001740 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06001741 log_err("fio: can't be both client and server\n");
1742 do_exit++;
1743 exit_val = 1;
1744 break;
1745 }
Jens Axboe87aa8f12011-10-06 21:24:13 +02001746 if (optarg)
Jens Axboebebe6392011-10-07 10:00:51 +02001747 fio_server_set_arg(optarg);
Jens Axboe50d16972011-09-29 17:45:28 -06001748 is_backend = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001749 backend = 1;
Jens Axboe50d16972011-09-29 17:45:28 -06001750 break;
Jens Axboee46d8092011-10-03 09:11:02 +02001751 case 'D':
Jens Axboe402668f2011-10-10 15:28:58 +02001752 pid_file = strdup(optarg);
Jens Axboee46d8092011-10-03 09:11:02 +02001753 break;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001754 case 'I':
1755 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1756 /* exit on error and calibration only */
1757 do_exit++;
1758 if (ret == -1)
1759 exit_val = 1;
1760 }
1761 break;
Jens Axboe132159a2011-09-30 15:01:32 -06001762 case 'C':
1763 if (is_backend) {
1764 log_err("fio: can't be both client and server\n");
1765 do_exit++;
1766 exit_val = 1;
1767 break;
1768 }
Jens Axboea5276612012-03-04 15:15:08 +01001769 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
Jens Axboebebe6392011-10-07 10:00:51 +02001770 log_err("fio: failed adding client %s\n", optarg);
1771 do_exit++;
1772 exit_val = 1;
1773 break;
1774 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02001775 /*
1776 * If the next argument exists and isn't an option,
1777 * assume it's a job file for this client only.
1778 */
1779 while (optind < argc) {
1780 if (!strncmp(argv[optind], "--", 2) ||
1781 !strncmp(argv[optind], "-", 1))
1782 break;
1783
1784 fio_client_add_ini_file(cur_client, argv[optind]);
1785 optind++;
1786 }
Jens Axboe132159a2011-09-30 15:01:32 -06001787 break;
Jens Axboe7d11f872012-12-17 12:03:29 +01001788 case 'T':
1789 do_exit++;
1790 exit_val = fio_monotonic_clocktest();
1791 break;
Jens Axboe798827c2013-01-23 18:11:48 -07001792 case '?':
1793 log_err("%s: unrecognized option '%s'\n", argv[0],
1794 argv[optind - 1]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001795 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001796 do_exit++;
1797 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001798 break;
1799 }
Jens Axboec7d5c942011-10-03 11:48:17 +02001800 if (do_exit)
1801 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001802 }
1803
Jens Axboec2c94582011-10-05 20:31:30 +02001804 if (do_exit) {
1805 if (exit_val && !(is_backend || nr_clients))
1806 exit(exit_val);
1807 }
Jens Axboe536582b2008-02-18 20:26:32 +01001808
Jens Axboea37f69b2011-10-01 12:24:21 -06001809 if (nr_clients && fio_clients_connect()) {
Jens Axboe132159a2011-09-30 15:01:32 -06001810 do_exit++;
1811 exit_val = 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02001812 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -06001813 }
1814
Jens Axboe81179ee2011-10-04 12:42:06 +02001815 if (is_backend && backend)
Jens Axboe402668f2011-10-10 15:28:58 +02001816 return fio_start_server(pid_file);
Jens Axboe50d16972011-09-29 17:45:28 -06001817
Jens Axboe214e1ec2007-03-15 10:48:13 +01001818 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001819 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001820 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001821 }
1822
Jens Axboe7874f8b2011-10-06 09:18:20 +02001823 while (!ret && optind < argc) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001824 ini_idx++;
1825 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1826 ini_file[ini_idx - 1] = strdup(argv[optind]);
1827 optind++;
1828 }
1829
1830 return ini_idx;
1831}
1832
Jens Axboe0420ba62012-02-29 11:16:52 +01001833int fio_init_options(void)
Jens Axboeebac4652005-12-08 15:25:21 +01001834{
Jens Axboeb4692822006-10-27 13:43:22 +02001835 f_out = stdout;
1836 f_err = stderr;
1837
Jens Axboe4c6107f2011-01-18 05:22:22 -07001838 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001839 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001840
Jens Axboe9d9eb2e2011-10-03 12:01:42 +02001841 atexit(free_shm);
1842
Jens Axboeebac4652005-12-08 15:25:21 +01001843 if (fill_def_thread())
1844 return 1;
1845
Jens Axboe0420ba62012-02-29 11:16:52 +01001846 return 0;
1847}
1848
Jens Axboe51167792012-03-08 21:34:18 +01001849extern int fio_check_options(struct thread_options *);
1850
Jens Axboe0420ba62012-02-29 11:16:52 +01001851int parse_options(int argc, char *argv[])
1852{
Jens Axboe46bcd492012-03-14 11:31:21 +01001853 const int type = FIO_CLIENT_TYPE_CLI;
Jens Axboe0420ba62012-02-29 11:16:52 +01001854 int job_files, i;
1855
1856 if (fio_init_options())
1857 return 1;
Jens Axboe51167792012-03-08 21:34:18 +01001858 if (fio_test_cconv(&def_thread.o))
1859 log_err("fio: failed internal cconv test\n");
Jens Axboe0420ba62012-02-29 11:16:52 +01001860
Jens Axboe46bcd492012-03-14 11:31:21 +01001861 job_files = parse_cmd_line(argc, argv, type);
Jens Axboeebac4652005-12-08 15:25:21 +01001862
Jens Axboecdf54d82011-10-04 08:31:40 +02001863 if (job_files > 0) {
1864 for (i = 0; i < job_files; i++) {
1865 if (fill_def_thread())
Jens Axboe132159a2011-09-30 15:01:32 -06001866 return 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02001867 if (nr_clients) {
1868 if (fio_clients_send_ini(ini_file[i]))
1869 return 1;
1870 free(ini_file[i]);
1871 } else if (!is_backend) {
Jens Axboe46bcd492012-03-14 11:31:21 +01001872 if (parse_jobs_ini(ini_file[i], 0, i, type))
Jens Axboecdf54d82011-10-04 08:31:40 +02001873 return 1;
1874 free(ini_file[i]);
1875 }
Jens Axboe132159a2011-09-30 15:01:32 -06001876 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02001877 } else if (nr_clients) {
1878 if (fill_def_thread())
1879 return 1;
1880 if (fio_clients_send_ini(NULL))
1881 return 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02001882 }
Jens Axboeebac4652005-12-08 15:25:21 +01001883
Jens Axboe88c6ed82006-06-09 11:28:10 +02001884 free(ini_file);
Jens Axboe7e356b22011-10-14 10:55:16 +02001885 fio_options_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001886
1887 if (!thread_number) {
Jens Axboe111e0322013-03-07 11:31:20 +01001888 if (dump_cmdline || parse_only)
Jens Axboecca73aa2007-04-04 11:09:19 +02001889 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01001890 if (exec_profile)
1891 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06001892 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06001893 return 0;
Jens Axboe085399d2011-10-06 09:01:22 +02001894 if (did_arg)
1895 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001896
Jens Axboed65e11c2011-10-05 09:56:53 +02001897 log_err("No jobs(s) defined\n\n");
Jens Axboe085399d2011-10-06 09:01:22 +02001898
1899 if (!did_arg) {
1900 usage(argv[0]);
1901 return 1;
1902 }
1903
1904 return 0;
Jens Axboeb4692822006-10-27 13:43:22 +02001905 }
1906
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001907 if (def_thread.o.gtod_offload) {
1908 fio_gtod_init();
1909 fio_gtod_offload = 1;
1910 fio_gtod_cpu = def_thread.o.gtod_cpu;
1911 }
1912
Jens Axboef3afa572012-09-17 13:34:16 +02001913 if (output_format == FIO_OUTPUT_NORMAL)
Jens Axboe3d433822012-03-21 22:25:22 +01001914 log_info("%s\n", fio_version_string);
Jens Axboef6dea4d2011-10-13 13:37:07 +02001915
Jens Axboeebac4652005-12-08 15:25:21 +01001916 return 0;
1917}
Jens Axboe588b7f02012-03-19 20:37:41 +01001918
1919void options_default_fill(struct thread_options *o)
1920{
1921 memcpy(o, &def_thread.o, sizeof(*o));
1922}