blob: 27370bb43eb04d4dceb184cedecb106057058c3b [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"
29
Jens Axboe3d433822012-03-21 22:25:22 +010030const char fio_version_string[] = FIO_VERSION;
Jens Axboe214e1ec2007-03-15 10:48:13 +010031
Jens Axboeee738492007-01-10 11:23:16 +010032#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010033
Jens Axboe214e1ec2007-03-15 10:48:13 +010034static char **ini_file;
Jens Axboefca70352011-07-06 20:12:54 +020035static int max_jobs = FIO_MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020036static int dump_cmdline;
Jens Axboe8c029372011-10-05 09:54:30 +020037static int def_timeout;
Jens Axboe111e0322013-03-07 11:31:20 +010038static int parse_only;
Jens Axboee1f36502006-10-27 10:54:08 +020039
Jens Axboebe4ecfd2008-12-08 14:10:52 +010040static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010041struct thread_data *threads = NULL;
Jens Axboee1f36502006-10-27 10:54:08 +020042
Jens Axboe214e1ec2007-03-15 10:48:13 +010043int exitall_on_terminate = 0;
Jens Axboef3afa572012-09-17 13:34:16 +020044int output_format = FIO_OUTPUT_NORMAL;
Jens Axboe5be4c942013-02-11 16:33:25 +010045int eta_print = FIO_ETA_AUTO;
Jens Axboee382e662013-02-22 20:48:56 +010046int eta_new_line = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +010047unsigned long long mlock_size = 0;
48FILE *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
Jens Axboe67bf9822013-01-10 11:23:19 +0100570#ifndef CONFIG_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100571 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700572 log_info("fio: this platform does not support fdatasync()"
573 " falling back to using fsync(). Use the 'fsync'"
574 " option instead of 'fdatasync' to get rid of"
575 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100576 o->fsync_blocks = o->fdatasync_blocks;
577 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700578 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700579 }
580#endif
581
Bruce Cran93bcfd22012-02-20 20:18:19 +0100582#ifdef WIN32
583 /*
584 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
585 * so fail if we're passed those flags
586 */
587 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
588 log_err("fio: Windows does not support direct or non-buffered io with"
589 " the synchronous ioengines. Use the 'windowsaio' ioengine"
590 " with 'direct=1' and 'iodepth=1' instead.\n");
591 ret = 1;
592 }
593#endif
594
Jens Axboe811ac502012-03-02 22:23:36 +0100595 /*
596 * For fully compressible data, just zero them at init time.
597 * It's faster than repeatedly filling it.
598 */
599 if (td->o.compress_percentage == 100) {
600 td->o.zero_buffers = 1;
601 td->o.compress_percentage = 0;
602 }
603
Jens Axboe5881fda2012-11-15 15:21:23 -0700604 /*
605 * Using a non-uniform random distribution excludes usage of
606 * a random map
607 */
608 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
609 td->o.norandommap = 1;
610
Jens Axboea9523c62011-01-17 16:49:54 -0700611 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200612}
613
Jens Axboe906c8d72006-06-13 09:37:56 +0200614/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100615 * This function leaks the buffer
616 */
Jens Axboe99d633a2012-03-15 15:55:04 +0100617char *fio_uint_to_kmg(unsigned int val)
Jens Axboef8977ee2006-11-06 14:03:03 +0100618{
619 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100620 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100621 char *p = post;
622
Jens Axboe245142f2006-11-08 12:49:21 +0100623 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100624 if (val & 1023)
625 break;
626
627 val >>= 10;
628 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100629 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100630
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100631 snprintf(buf, 32, "%u%c", val, *p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100632 return buf;
633}
634
Jens Axboe09629a92007-03-09 09:00:06 +0100635/* External engines are specified by "external:name.o") */
636static const char *get_engine_name(const char *str)
637{
638 char *p = strstr(str, ":");
639
640 if (!p)
641 return str;
642
643 p++;
644 strip_blank_front(&p);
645 strip_blank_end(p);
646 return p;
647}
648
Jens Axboee132cba2007-03-14 14:23:54 +0100649static int exists_and_not_file(const char *filename)
650{
651 struct stat sb;
652
653 if (lstat(filename, &sb) == -1)
654 return 0;
655
Bruce Cranecc314b2011-01-04 10:59:30 +0100656 /* \\.\ is the device namespace in Windows, where every file
657 * is a device node */
658 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100659 return 0;
660
661 return 1;
662}
663
Jens Axboe4c07ad82011-03-28 09:51:09 +0200664static void td_fill_rand_seeds_os(struct thread_data *td)
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200665{
Jens Axboed24945f2012-12-04 13:10:29 +0100666 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
667 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
668 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200669
670 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100671 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200672
Jens Axboed24945f2012-12-04 13:10:29 +0100673 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
674 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200675
676 if (!td_random(td))
677 return;
678
679 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100680 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200681
Jens Axboed24945f2012-12-04 13:10:29 +0100682 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200683}
684
685static void td_fill_rand_seeds_internal(struct thread_data *td)
686{
Jens Axboed24945f2012-12-04 13:10:29 +0100687 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
688 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
689 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200690
691 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100692 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200693
Jens Axboed24945f2012-12-04 13:10:29 +0100694 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
695 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200696
697 if (!td_random(td))
698 return;
699
700 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100701 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe4c07ad82011-03-28 09:51:09 +0200702
Jens Axboed24945f2012-12-04 13:10:29 +0100703 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200704}
705
Jens Axboe4c07ad82011-03-28 09:51:09 +0200706void td_fill_rand_seeds(struct thread_data *td)
707{
708 if (td->o.use_os_rand)
709 td_fill_rand_seeds_os(td);
710 else
711 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600712
Jens Axboed24945f2012-12-04 13:10:29 +0100713 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200714}
715
Jens Axboe9c60ce62007-03-15 09:14:47 +0100716/*
Steven Langde890a12011-11-09 14:03:34 +0100717 * Initializes the ioengine configured for a job, if it has not been done so
718 * already.
719 */
720int ioengine_load(struct thread_data *td)
721{
722 const char *engine;
723
724 /*
725 * Engine has already been loaded.
726 */
727 if (td->io_ops)
728 return 0;
729
730 engine = get_engine_name(td->o.ioengine);
731 td->io_ops = load_ioengine(td, engine);
732 if (!td->io_ops) {
733 log_err("fio: failed to load engine %s\n", engine);
734 return 1;
735 }
736
737 if (td->io_ops->option_struct_size && td->io_ops->options) {
738 /*
739 * In cases where td->eo is set, clone it for a child thread.
740 * This requires that the parent thread has the same ioengine,
741 * but that requirement must be enforced by the code which
742 * cloned the thread.
743 */
744 void *origeo = td->eo;
745 /*
746 * Otherwise use the default thread options.
747 */
748 if (!origeo && td != &def_thread && def_thread.eo &&
749 def_thread.io_ops->options == td->io_ops->options)
750 origeo = def_thread.eo;
751
752 options_init(td->io_ops->options);
753 td->eo = malloc(td->io_ops->option_struct_size);
754 /*
755 * Use the default thread as an option template if this uses the
756 * same options structure and there are non-default options
757 * used.
758 */
759 if (origeo) {
760 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
761 options_mem_dupe(td->eo, td->io_ops->options);
762 } else {
763 memset(td->eo, 0, td->io_ops->option_struct_size);
764 fill_default_options(td->eo, td->io_ops->options);
765 }
766 *(struct thread_data **)td->eo = td;
767 }
768
769 return 0;
770}
771
Jens Axboed72be542012-11-30 19:37:46 +0100772static void init_flags(struct thread_data *td)
773{
774 struct thread_options *o = &td->o;
775
776 if (o->verify_backlog)
777 td->flags |= TD_F_VER_BACKLOG;
778 if (o->trim_backlog)
779 td->flags |= TD_F_TRIM_BACKLOG;
780 if (o->read_iolog_file)
781 td->flags |= TD_F_READ_IOLOG;
782 if (o->refill_buffers)
783 td->flags |= TD_F_REFILL_BUFFERS;
784 if (o->scramble_buffers)
785 td->flags |= TD_F_SCRAMBLE_BUFFERS;
786 if (o->verify != VERIFY_NONE)
787 td->flags |= TD_F_VER_NONE;
788}
789
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700790static int setup_random_seeds(struct thread_data *td)
791{
792 unsigned long seed;
793 unsigned int i;
794
795 if (!td->o.rand_repeatable)
796 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
797
798 for (seed = 0x89, i = 0; i < 4; i++)
799 seed *= 0x9e370001UL;
800
801 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
802 td->rand_seeds[i] = seed;
803 seed *= 0x9e370001UL;
804 }
805
806 td_fill_rand_seeds(td);
807 return 0;
808}
809
Steven Langde890a12011-11-09 14:03:34 +0100810/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200811 * Adds a job to the list of things todo. Sanitizes the various options
812 * to make sure we don't have conflicts, and initializes various
813 * members of td.
814 */
Jens Axboeb7cfb2e2012-03-12 07:47:27 +0100815static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
Jens Axboe46bcd492012-03-14 11:31:21 +0100816 int recursed, int client_type)
Jens Axboeebac4652005-12-08 15:25:21 +0100817{
Jens Axboeaf52b342007-03-13 10:07:47 +0100818 unsigned int i;
Jens Axboeaf52b342007-03-13 10:07:47 +0100819 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100820 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100821
Jens Axboeebac4652005-12-08 15:25:21 +0100822 /*
823 * the def_thread is just for options, it's not a real job
824 */
825 if (td == &def_thread)
826 return 0;
827
Jens Axboed72be542012-11-30 19:37:46 +0100828 init_flags(td);
829
Jens Axboecca73aa2007-04-04 11:09:19 +0200830 /*
831 * if we are just dumping the output command line, don't add the job
832 */
Jens Axboe111e0322013-03-07 11:31:20 +0100833 if (dump_cmdline || parse_only) {
Jens Axboecca73aa2007-04-04 11:09:19 +0200834 put_job(td);
835 return 0;
836 }
837
Jens Axboe46bcd492012-03-14 11:31:21 +0100838 td->client_type = client_type;
839
Jens Axboe58c55ba2010-03-09 12:20:08 +0100840 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +0200841 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +0100842
Steven Langde890a12011-11-09 14:03:34 +0100843 if (ioengine_load(td))
Jens Axboe205927a2007-03-15 11:06:32 +0100844 goto err;
Jens Axboedf641192006-10-12 07:55:41 +0200845
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100846 if (td->o.odirect)
Jens Axboe690adba2006-10-30 15:25:09 +0100847 td->io_ops->flags |= FIO_RAWIO;
848
Jens Axboee132cba2007-03-14 14:23:54 +0100849 file_alloced = 0;
Jens Axboe691c8fb2008-03-07 14:26:26 +0100850 if (!td->o.filename && !td->files_index && !td->o.read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +0100851 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100852
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100853 if (td->o.nr_files == 1 && exists_and_not_file(jobname))
Jens Axboee132cba2007-03-14 14:23:54 +0100854 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100855 else {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100856 for (i = 0; i < td->o.nr_files; i++) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100857 sprintf(fname, "%s.%d.%d", jobname,
858 td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100859 add_file(td, fname);
860 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100861 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100862 }
Jens Axboeebac4652005-12-08 15:25:21 +0100863
Jens Axboe4e991c22007-03-15 11:41:11 +0100864 if (fixup_options(td))
865 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +0100866
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100867 flow_init_job(td);
868
Steven Langde890a12011-11-09 14:03:34 +0100869 /*
870 * IO engines only need this for option callbacks, and the address may
871 * change in subprocesses.
872 */
873 if (td->eo)
874 *(struct thread_data **)td->eo = NULL;
875
Jens Axboe07eb79d2007-04-12 13:02:38 +0200876 if (td->io_ops->flags & FIO_DISKLESSIO) {
877 struct fio_file *f;
878
879 for_each_file(td, f, i)
880 f->real_file_size = -1ULL;
881 }
882
Jens Axboe521da522012-08-02 11:21:36 +0200883 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboeebac4652005-12-08 15:25:21 +0100884
Yu-ju Hong83349192011-08-13 00:53:44 +0200885 td->ts.clat_percentiles = td->o.clat_percentiles;
Vincent Kang Fu435d1952013-02-06 08:43:40 +0100886 td->ts.percentile_precision = td->o.percentile_precision;
Vincent Kang Fufd112d32013-02-02 09:28:55 +0100887 memcpy(td->ts.percentile_list, td->o.percentile_list, sizeof(td->o.percentile_list));
Yu-ju Hong83349192011-08-13 00:53:44 +0200888
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200889 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
890 td->ts.clat_stat[i].min_val = ULONG_MAX;
891 td->ts.slat_stat[i].min_val = ULONG_MAX;
892 td->ts.lat_stat[i].min_val = ULONG_MAX;
893 td->ts.bw_stat[i].min_val = ULONG_MAX;
894 }
Adam DeBellinscdd54112010-09-28 13:22:55 +0900895 td->ddir_seq_nr = td->o.ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +0100896
Jens Axboe755c3182009-08-25 22:14:29 +0200897 if ((td->o.stonewall || td->o.new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100898 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100899 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100900 }
Jens Axboeebac4652005-12-08 15:25:21 +0100901
902 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100903 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100904
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700905 if (setup_random_seeds(td)) {
Bruce Cran93bcfd22012-02-20 20:18:19 +0100906 td_verror(td, errno, "init_random_state");
Jens Axboe9c60ce62007-03-15 09:14:47 +0100907 goto err;
Bruce Cran93bcfd22012-02-20 20:18:19 +0100908 }
Jens Axboe9c60ce62007-03-15 09:14:47 +0100909
Jens Axboeebac4652005-12-08 15:25:21 +0100910 if (setup_rate(td))
911 goto err;
912
Jens Axboe203160d2012-03-29 21:17:12 +0200913 if (td->o.lat_log_file) {
Jens Axboeea51b952012-03-14 11:39:13 +0100914 setup_log(&td->lat_log, td->o.log_avg_msec, IO_LOG_TYPE_LAT);
915 setup_log(&td->slat_log, td->o.log_avg_msec, IO_LOG_TYPE_SLAT);
916 setup_log(&td->clat_log, td->o.log_avg_msec, IO_LOG_TYPE_CLAT);
Jens Axboeebac4652005-12-08 15:25:21 +0100917 }
Jens Axboe203160d2012-03-29 21:17:12 +0200918 if (td->o.bw_log_file)
Jens Axboeea51b952012-03-14 11:39:13 +0100919 setup_log(&td->bw_log, td->o.log_avg_msec, IO_LOG_TYPE_BW);
Jens Axboe203160d2012-03-29 21:17:12 +0200920 if (td->o.iops_log_file)
Jens Axboeea51b952012-03-14 11:39:13 +0100921 setup_log(&td->iops_log, td->o.log_avg_msec, IO_LOG_TYPE_IOPS);
Jens Axboeebac4652005-12-08 15:25:21 +0100922
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100923 if (!td->o.name)
924 td->o.name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200925
Jens Axboef3afa572012-09-17 13:34:16 +0200926 if (output_format == FIO_OUTPUT_NORMAL) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200927 if (!job_add_num) {
Jens Axboeb7cfb2e2012-03-12 07:47:27 +0100928 if (is_backend && !recursed)
Jens Axboe2f122b12012-03-15 13:10:19 +0100929 fio_server_send_add_job(td);
Jens Axboe807f9972012-03-02 10:25:24 +0100930
Jens Axboe03530502012-03-19 21:45:12 +0100931 if (!(td->io_ops->flags & FIO_NOIO)) {
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200932 char *c1, *c2, *c3, *c4, *c5, *c6;
Jens Axboef8977ee2006-11-06 14:03:03 +0100933
Jens Axboe99d633a2012-03-15 15:55:04 +0100934 c1 = fio_uint_to_kmg(td->o.min_bs[DDIR_READ]);
935 c2 = fio_uint_to_kmg(td->o.max_bs[DDIR_READ]);
936 c3 = fio_uint_to_kmg(td->o.min_bs[DDIR_WRITE]);
937 c4 = fio_uint_to_kmg(td->o.max_bs[DDIR_WRITE]);
Jens Axboed79db122012-09-24 08:51:24 +0200938 c5 = fio_uint_to_kmg(td->o.min_bs[DDIR_TRIM]);
939 c6 = fio_uint_to_kmg(td->o.max_bs[DDIR_TRIM]);
Jens Axboef8977ee2006-11-06 14:03:03 +0100940
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200941 log_info("%s: (g=%d): rw=%s, bs=%s-%s/%s-%s/%s-%s,"
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100942 " ioengine=%s, iodepth=%u\n",
943 td->o.name, td->groupid,
Jens Axboe807f9972012-03-02 10:25:24 +0100944 ddir_str(td->o.td_ddir),
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200945 c1, c2, c3, c4, c5, c6,
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100946 td->io_ops->name,
947 td->o.iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100948
949 free(c1);
950 free(c2);
951 free(c3);
952 free(c4);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200953 free(c5);
954 free(c6);
Jens Axboef8977ee2006-11-06 14:03:03 +0100955 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200956 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +0100957 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +0200958 }
Jens Axboeebac4652005-12-08 15:25:21 +0100959
960 /*
961 * recurse add identical jobs, clear numjobs and stonewall options
962 * as they don't apply to sub-jobs
963 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100964 numjobs = td->o.numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100965 while (--numjobs) {
Steven Langde890a12011-11-09 14:03:34 +0100966 struct thread_data *td_new = get_new_job(0, td, 1);
Jens Axboeebac4652005-12-08 15:25:21 +0100967
968 if (!td_new)
969 goto err;
970
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100971 td_new->o.numjobs = 1;
972 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +0200973 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100974
975 if (file_alloced) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100976 td_new->o.filename = NULL;
Jens Axboee132cba2007-03-14 14:23:54 +0100977 td_new->files_index = 0;
Jens Axboe4e6ea2f2009-03-04 20:17:22 +0100978 td_new->files_size = 0;
Jens Axboee132cba2007-03-14 14:23:54 +0100979 td_new->files = NULL;
980 }
981
Jens Axboe75154842006-06-01 13:56:09 +0200982 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100983
Jens Axboe46bcd492012-03-14 11:31:21 +0100984 if (add_job(td_new, jobname, job_add_num, 1, client_type))
Jens Axboeebac4652005-12-08 15:25:21 +0100985 goto err;
986 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100987
Jens Axboeebac4652005-12-08 15:25:21 +0100988 return 0;
989err:
990 put_job(td);
991 return -1;
992}
993
Jens Axboe79d16312010-03-04 12:43:20 +0100994/*
995 * Parse as if 'o' was a command line
996 */
Jens Axboe46bcd492012-03-14 11:31:21 +0100997void add_job_opts(const char **o, int client_type)
Jens Axboe79d16312010-03-04 12:43:20 +0100998{
999 struct thread_data *td, *td_parent;
1000 int i, in_global = 1;
1001 char jobname[32];
1002
1003 i = 0;
1004 td_parent = td = NULL;
1005 while (o[i]) {
1006 if (!strncmp(o[i], "name", 4)) {
1007 in_global = 0;
1008 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001009 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001010 td = NULL;
1011 sprintf(jobname, "%s", o[i] + 5);
1012 }
1013 if (in_global && !td_parent)
Steven Langde890a12011-11-09 14:03:34 +01001014 td_parent = get_new_job(1, &def_thread, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001015 else if (!in_global && !td) {
1016 if (!td_parent)
1017 td_parent = &def_thread;
Steven Langde890a12011-11-09 14:03:34 +01001018 td = get_new_job(0, td_parent, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001019 }
1020 if (in_global)
1021 fio_options_parse(td_parent, (char **) &o[i], 1);
1022 else
1023 fio_options_parse(td, (char **) &o[i], 1);
1024 i++;
1025 }
1026
1027 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001028 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001029}
1030
Jens Axboe01f06b62008-02-18 20:53:47 +01001031static int skip_this_section(const char *name)
1032{
Jens Axboead0a2732011-03-11 10:16:17 +01001033 int i;
1034
1035 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +01001036 return 0;
1037 if (!strncmp(name, "global", 6))
1038 return 0;
1039
Jens Axboead0a2732011-03-11 10:16:17 +01001040 for (i = 0; i < nr_job_sections; i++)
1041 if (!strcmp(job_sections[i], name))
1042 return 0;
1043
1044 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001045}
1046
Jens Axboeebac4652005-12-08 15:25:21 +01001047static int is_empty_or_comment(char *line)
1048{
1049 unsigned int i;
1050
1051 for (i = 0; i < strlen(line); i++) {
1052 if (line[i] == ';')
1053 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001054 if (line[i] == '#')
1055 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +02001056 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +01001057 return 0;
1058 }
1059
1060 return 1;
1061}
1062
Jens Axboe313cb202006-12-21 09:50:00 +01001063/*
Jens Axboe07261982006-06-07 10:51:12 +02001064 * This is our [ini] type file parser.
1065 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001066int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
Jens Axboeebac4652005-12-08 15:25:21 +01001067{
Jens Axboee1f36502006-10-27 10:54:08 +02001068 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001069 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001070 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001071 FILE *f;
1072 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001073 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +02001074 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +02001075 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +01001076 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001077 char **opts;
1078 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +01001079
Jens Axboe50d16972011-09-29 17:45:28 -06001080 if (is_buf)
1081 f = NULL;
1082 else {
1083 if (!strcmp(file, "-"))
1084 f = stdin;
1085 else
1086 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001087
Jens Axboe50d16972011-09-29 17:45:28 -06001088 if (!f) {
1089 perror("fopen job file");
1090 return 1;
1091 }
Jens Axboeebac4652005-12-08 15:25:21 +01001092 }
1093
1094 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +02001095
1096 /*
1097 * it's really 256 + small bit, 280 should suffice
1098 */
1099 name = malloc(280);
1100 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +01001101
Jens Axboe3b8b7132008-06-10 19:46:23 +02001102 alloc_opts = 8;
1103 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +02001104 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001105
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001106 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001107 do {
Jens Axboeccf8f122007-09-27 10:48:55 +02001108 /*
1109 * if skip_fgets is set, we already have loaded a line we
1110 * haven't handled.
1111 */
1112 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -06001113 if (is_buf)
1114 p = strsep(&file, "\n");
1115 else
Jens Axboe43f74792011-10-15 14:37:26 +02001116 p = fgets(string, 4096, f);
Jens Axboeccf8f122007-09-27 10:48:55 +02001117 if (!p)
1118 break;
1119 }
gurudas paicdc7f192007-03-29 10:10:56 +02001120
Jens Axboeccf8f122007-09-27 10:48:55 +02001121 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +02001122 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001123 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +02001124
Jens Axboeebac4652005-12-08 15:25:21 +01001125 if (is_empty_or_comment(p))
1126 continue;
Bruce Cran84dd1882011-05-05 08:14:09 -06001127 if (sscanf(p, "[%255[^\n]]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001128 if (inside_skip)
1129 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001130 log_err("fio: option <%s> outside of [] job section\n",
1131 p);
Jens Axboe088b4202007-07-19 14:53:01 +02001132 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001133 }
Jens Axboeebac4652005-12-08 15:25:21 +01001134
Jens Axboe7a4b80a2010-05-22 20:43:11 +02001135 name[strlen(name) - 1] = '\0';
1136
Jens Axboe01f06b62008-02-18 20:53:47 +01001137 if (skip_this_section(name)) {
1138 inside_skip = 1;
1139 continue;
1140 } else
1141 inside_skip = 0;
1142
Jens Axboeebac4652005-12-08 15:25:21 +01001143 global = !strncmp(name, "global", 6);
1144
Jens Axboecca73aa2007-04-04 11:09:19 +02001145 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +02001146 if (first_sect)
1147 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +02001148 if (!global)
1149 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +02001150 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001151 }
1152
Steven Langde890a12011-11-09 14:03:34 +01001153 td = get_new_job(global, &def_thread, 0);
Jens Axboe45410ac2006-06-07 11:10:39 +02001154 if (!td) {
1155 ret = 1;
1156 break;
1157 }
Jens Axboeebac4652005-12-08 15:25:21 +01001158
Jens Axboe972cfd22006-06-09 11:08:56 +02001159 /*
1160 * Seperate multiple job files by a stonewall
1161 */
Jens Axboef9481912006-06-09 11:37:28 +02001162 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001163 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +02001164 stonewall = 0;
1165 }
1166
Jens Axboe3b8b7132008-06-10 19:46:23 +02001167 num_opts = 0;
1168 memset(opts, 0, alloc_opts * sizeof(char *));
1169
Jens Axboe50d16972011-09-29 17:45:28 -06001170 while (1) {
1171 if (is_buf)
1172 p = strsep(&file, "\n");
1173 else
1174 p = fgets(string, 4096, f);
1175 if (!p)
1176 break;
1177
Jens Axboeebac4652005-12-08 15:25:21 +01001178 if (is_empty_or_comment(p))
1179 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001180
Jens Axboeb6754f92006-05-30 14:29:32 +02001181 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001182
Jens Axboeccf8f122007-09-27 10:48:55 +02001183 /*
1184 * new section, break out and make sure we don't
1185 * fgets() a new line at the top.
1186 */
1187 if (p[0] == '[') {
1188 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001189 break;
Jens Axboeccf8f122007-09-27 10:48:55 +02001190 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001191
Jens Axboe4ae3f762006-05-30 13:35:14 +02001192 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001193
Jens Axboe3b8b7132008-06-10 19:46:23 +02001194 if (num_opts == alloc_opts) {
1195 alloc_opts <<= 1;
1196 opts = realloc(opts,
1197 alloc_opts * sizeof(char *));
1198 }
1199
1200 opts[num_opts] = strdup(p);
1201 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +01001202 }
Jens Axboeebac4652005-12-08 15:25:21 +01001203
Jens Axboe3b8b7132008-06-10 19:46:23 +02001204 ret = fio_options_parse(td, opts, num_opts);
1205 if (!ret) {
1206 if (dump_cmdline)
1207 for (i = 0; i < num_opts; i++)
1208 log_info("--%s ", opts[i]);
1209
Jens Axboe46bcd492012-03-14 11:31:21 +01001210 ret = add_job(td, name, 0, 0, type);
Jens Axboe3b8b7132008-06-10 19:46:23 +02001211 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +01001212 log_err("fio: job %s dropped\n", name);
1213 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001214 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001215
1216 for (i = 0; i < num_opts; i++)
1217 free(opts[i]);
1218 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001219 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001220
Jens Axboecca73aa2007-04-04 11:09:19 +02001221 if (dump_cmdline)
1222 log_info("\n");
1223
Jens Axboe7e356b22011-10-14 10:55:16 +02001224 i = 0;
1225 while (i < nr_job_sections) {
1226 free(job_sections[i]);
1227 i++;
1228 }
1229
Jens Axboe3b8b7132008-06-10 19:46:23 +02001230 for (i = 0; i < num_opts; i++)
1231 free(opts[i]);
1232
Jens Axboeebac4652005-12-08 15:25:21 +01001233 free(string);
1234 free(name);
Jens Axboe25775942008-06-10 20:26:06 +02001235 free(opts);
Jens Axboe50d16972011-09-29 17:45:28 -06001236 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001237 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001238 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001239}
1240
1241static int fill_def_thread(void)
1242{
1243 memset(&def_thread, 0, sizeof(def_thread));
1244
Jens Axboe375b2692007-05-22 09:13:02 +02001245 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboe8c029372011-10-05 09:54:30 +02001246 def_thread.o.timeout = def_timeout;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001247 def_thread.o.error_dump = 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001248 /*
Jens Axboeee738492007-01-10 11:23:16 +01001249 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001250 */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001251 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001252 return 0;
1253}
1254
Jens Axboe45378b32007-12-19 13:54:38 +01001255static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02001256{
Jens Axboe3d433822012-03-21 22:25:22 +01001257 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01001258 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboe83881282011-10-17 19:58:51 +02001259 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1260 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
1261 "\t\t\tdiskutil,job,mutex,profile,time,net\n");
Jens Axboe111e0322013-03-07 11:31:20 +01001262 printf(" --parse-only\t\tParse options only, don't start any IO\n");
Jens Axboe83881282011-10-17 19:58:51 +02001263 printf(" --output\t\tWrite output to file\n");
liang xieb2cecdc2012-08-31 08:22:42 -07001264 printf(" --runtime\t\tRuntime in seconds\n");
Jens Axboe83881282011-10-17 19:58:51 +02001265 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1266 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1267 printf(" --minimal\t\tMinimal (terse) output\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001268 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
Jens Axboe75db6e22011-11-07 22:07:36 +01001269 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001270 printf(" --version\t\tPrint version info and exit\n");
Jens Axboe83881282011-10-17 19:58:51 +02001271 printf(" --help\t\tPrint this page\n");
Jens Axboe23893642012-12-17 14:44:08 +01001272 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
Jens Axboe83881282011-10-17 19:58:51 +02001273 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001274 " them\n");
Steven Langde890a12011-11-09 14:03:34 +01001275 printf(" --enghelp=engine\tPrint ioengine help, or list"
1276 " available ioengines\n");
1277 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1278 " cmd\n");
Jens Axboe83881282011-10-17 19:58:51 +02001279 printf(" --showcmd\t\tTurn a job file into command line options\n");
1280 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1281 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboee382e662013-02-22 20:48:56 +01001282 printf(" --eta-newline=time\tForce a new line for every 'time'");
1283 printf(" period passed\n");
Jens Axboe83881282011-10-17 19:58:51 +02001284 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001285 " writes\n");
Jens Axboe83881282011-10-17 19:58:51 +02001286 printf(" --section=name\tOnly run specified section in job file\n");
1287 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
Jens Axboe2b386d22008-03-26 10:32:57 +01001288 " (def 1024)\n");
Jens Axboe83881282011-10-17 19:58:51 +02001289 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1290 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1291 printf(" --server=args\t\tStart a backend fio server\n");
1292 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1293 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001294 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1295 "\t\t\t(option=system,percpu) or run unit work\n"
1296 "\t\t\tcalibration only (option=calibrate)\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001297 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
Jens Axboe23893642012-12-17 14:44:08 +01001298 printf("\n Jens Axboe <jaxboe@fusionio.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001299}
1300
Jens Axboe79e48f72008-02-01 20:37:09 +01001301#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001302struct debug_level debug_levels[] = {
Jens Axboe0b8d11e2012-03-02 19:44:15 +01001303 { .name = "process",
1304 .help = "Process creation/exit logging",
1305 .shift = FD_PROCESS,
1306 },
1307 { .name = "file",
1308 .help = "File related action logging",
1309 .shift = FD_FILE,
1310 },
1311 { .name = "io",
1312 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1313 .shift = FD_IO,
1314 },
1315 { .name = "mem",
1316 .help = "Memory allocation/freeing logging",
1317 .shift = FD_MEM,
1318 },
1319 { .name = "blktrace",
1320 .help = "blktrace action logging",
1321 .shift = FD_BLKTRACE,
1322 },
1323 { .name = "verify",
1324 .help = "IO verification action logging",
1325 .shift = FD_VERIFY,
1326 },
1327 { .name = "random",
1328 .help = "Random generation logging",
1329 .shift = FD_RANDOM,
1330 },
1331 { .name = "parse",
1332 .help = "Parser logging",
1333 .shift = FD_PARSE,
1334 },
1335 { .name = "diskutil",
1336 .help = "Disk utility logging actions",
1337 .shift = FD_DISKUTIL,
1338 },
1339 { .name = "job",
1340 .help = "Logging related to creating/destroying jobs",
1341 .shift = FD_JOB,
1342 },
1343 { .name = "mutex",
1344 .help = "Mutex logging",
1345 .shift = FD_MUTEX
1346 },
1347 { .name = "profile",
1348 .help = "Logging related to profiles",
1349 .shift = FD_PROFILE,
1350 },
1351 { .name = "time",
1352 .help = "Logging related to time keeping functions",
1353 .shift = FD_TIME,
1354 },
1355 { .name = "net",
1356 .help = "Network logging",
1357 .shift = FD_NET,
1358 },
Jens Axboe02444ad2008-10-07 11:33:00 +02001359 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001360};
1361
Jens Axboec09823a2008-02-19 20:16:57 +01001362static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001363{
1364 struct debug_level *dl;
1365 char *p = (char *) string;
1366 char *opt;
1367 int i;
1368
1369 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001370 log_info("fio: dumping debug options:");
1371 for (i = 0; debug_levels[i].name; i++) {
1372 dl = &debug_levels[i];
1373 log_info("%s,", dl->name);
1374 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001375 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001376 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001377 }
1378
1379 while ((opt = strsep(&p, ",")) != NULL) {
1380 int found = 0;
1381
Jens Axboe5e1d3062008-05-23 11:55:53 +02001382 if (!strncmp(opt, "all", 3)) {
1383 log_info("fio: set all debug options\n");
1384 fio_debug = ~0UL;
1385 continue;
1386 }
1387
Jens Axboeee56ad52008-02-01 10:30:20 +01001388 for (i = 0; debug_levels[i].name; i++) {
1389 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001390 found = !strncmp(opt, dl->name, strlen(dl->name));
1391 if (!found)
1392 continue;
1393
1394 if (dl->shift == FD_JOB) {
1395 opt = strchr(opt, ':');
1396 if (!opt) {
1397 log_err("fio: missing job number\n");
1398 break;
1399 }
1400 opt++;
1401 fio_debug_jobno = atoi(opt);
1402 log_info("fio: set debug jobno %d\n",
1403 fio_debug_jobno);
1404 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001405 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001406 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001407 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001408 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001409 }
1410
1411 if (!found)
1412 log_err("fio: debug mask %s not found\n", opt);
1413 }
Jens Axboec09823a2008-02-19 20:16:57 +01001414 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001415}
Jens Axboe79e48f72008-02-01 20:37:09 +01001416#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001417static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001418{
1419 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001420 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001421}
1422#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001423
Jens Axboe4c6107f2011-01-18 05:22:22 -07001424static void fio_options_fill_optstring(void)
1425{
1426 char *ostr = cmd_optstr;
1427 int i, c;
1428
1429 c = i = 0;
1430 while (l_opts[i].name) {
1431 ostr[c++] = l_opts[i].val;
1432 if (l_opts[i].has_arg == required_argument)
1433 ostr[c++] = ':';
1434 else if (l_opts[i].has_arg == optional_argument) {
1435 ostr[c++] = ':';
1436 ostr[c++] = ':';
1437 }
1438 i++;
1439 }
1440 ostr[c] = '\0';
1441}
1442
Jens Axboec2c94582011-10-05 20:31:30 +02001443static int client_flag_set(char c)
1444{
1445 int i;
1446
1447 i = 0;
1448 while (l_opts[i].name) {
1449 int val = l_opts[i].val;
1450
1451 if (c == (val & 0xff))
1452 return (val & FIO_CLIENT_FLAG);
1453
1454 i++;
1455 }
1456
1457 return 0;
1458}
1459
Jens Axboefa2ea802011-10-08 21:07:29 +02001460void parse_cmd_client(void *client, char *opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +02001461{
Jens Axboefa2ea802011-10-08 21:07:29 +02001462 fio_client_add_cmd_option(client, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001463}
1464
Jens Axboe46bcd492012-03-14 11:31:21 +01001465int parse_cmd_line(int argc, char *argv[], int client_type)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001466{
1467 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001468 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001469 char *ostr = cmd_optstr;
Jens Axboe402668f2011-10-10 15:28:58 +02001470 void *pid_file = NULL;
Jens Axboe9bae26e2011-10-07 10:07:22 +02001471 void *cur_client = NULL;
Jens Axboe81179ee2011-10-04 12:42:06 +02001472 int backend = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001473
Jens Axboec2c94582011-10-05 20:31:30 +02001474 /*
1475 * Reset optind handling, since we may call this multiple times
1476 * for the backend.
1477 */
1478 optind = 1;
Jens Axboe7a4b8242011-10-05 17:35:15 +02001479
Jens Axboec2c94582011-10-05 20:31:30 +02001480 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
Jens Axboe085399d2011-10-06 09:01:22 +02001481 did_arg = 1;
1482
Jens Axboec2c94582011-10-05 20:31:30 +02001483 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
Jens Axboefa2ea802011-10-08 21:07:29 +02001484 parse_cmd_client(cur_client, argv[optind - 1]);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001485 c &= ~FIO_CLIENT_FLAG;
1486 }
1487
Jens Axboe214e1ec2007-03-15 10:48:13 +01001488 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001489 case 'a':
1490 smalloc_pool_size = atoi(optarg);
1491 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001492 case 't':
Jens Axboe8c029372011-10-05 09:54:30 +02001493 def_timeout = atoi(optarg);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001494 break;
1495 case 'l':
1496 write_lat_log = 1;
1497 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001498 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001499 write_bw_log = 1;
1500 break;
1501 case 'o':
1502 f_out = fopen(optarg, "w+");
1503 if (!f_out) {
1504 perror("fopen output");
1505 exit(1);
1506 }
1507 f_err = f_out;
1508 break;
1509 case 'm':
Jens Axboef3afa572012-09-17 13:34:16 +02001510 output_format = FIO_OUTPUT_TERSE;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001511 break;
Jens Axboef3afa572012-09-17 13:34:16 +02001512 case 'F':
1513 if (!strcmp(optarg, "minimal") ||
1514 !strcmp(optarg, "terse") ||
1515 !strcmp(optarg, "csv"))
1516 output_format = FIO_OUTPUT_TERSE;
1517 else if (!strcmp(optarg, "json"))
1518 output_format = FIO_OUTPUT_JSON;
1519 else
1520 output_format = FIO_OUTPUT_NORMAL;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001521 break;
1522 case 'h':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001523 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001524 usage(argv[0]);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001525 do_exit++;
1526 }
Jens Axboec2c94582011-10-05 20:31:30 +02001527 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001528 case 'c':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001529 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001530 fio_show_option_help(optarg);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001531 do_exit++;
1532 }
Jens Axboec2c94582011-10-05 20:31:30 +02001533 break;
Steven Langde890a12011-11-09 14:03:34 +01001534 case 'i':
1535 if (!cur_client) {
1536 fio_show_ioengine_help(optarg);
1537 do_exit++;
1538 }
1539 break;
Jens Axboecca73aa2007-04-04 11:09:19 +02001540 case 's':
1541 dump_cmdline = 1;
1542 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001543 case 'r':
1544 read_only = 1;
1545 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001546 case 'v':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001547 if (!cur_client) {
Jens Axboe3d433822012-03-21 22:25:22 +01001548 log_info("%s\n", fio_version_string);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001549 do_exit++;
1550 }
Jens Axboec2c94582011-10-05 20:31:30 +02001551 break;
Jens Axboef57a9c52011-09-09 21:01:37 +02001552 case 'V':
1553 terse_version = atoi(optarg);
Jens Axboe09786f52012-10-04 17:06:49 +02001554 if (!(terse_version == 2 || terse_version == 3 ||
1555 terse_version == 4)) {
Jens Axboef57a9c52011-09-09 21:01:37 +02001556 log_err("fio: bad terse version format\n");
1557 exit_val = 1;
1558 do_exit++;
1559 }
1560 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001561 case 'e':
1562 if (!strcmp("always", optarg))
1563 eta_print = FIO_ETA_ALWAYS;
1564 else if (!strcmp("never", optarg))
1565 eta_print = FIO_ETA_NEVER;
1566 break;
Jens Axboee382e662013-02-22 20:48:56 +01001567 case 'E': {
1568 long long t = 0;
1569
1570 if (str_to_decimal(optarg, &t, 0, NULL)) {
1571 log_err("fio: failed parsing eta time %s\n", optarg);
1572 exit_val = 1;
1573 do_exit++;
1574 }
1575 eta_new_line = t;
1576 break;
1577 }
Jens Axboeee56ad52008-02-01 10:30:20 +01001578 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001579 if (set_debug(optarg))
1580 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001581 break;
Jens Axboe111e0322013-03-07 11:31:20 +01001582 case 'P':
1583 parse_only = 1;
1584 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001585 case 'x': {
1586 size_t new_size;
1587
Jens Axboe01f06b62008-02-18 20:53:47 +01001588 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001589 log_err("fio: can't use global as only "
1590 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001591 do_exit++;
1592 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001593 break;
1594 }
Jens Axboead0a2732011-03-11 10:16:17 +01001595 new_size = (nr_job_sections + 1) * sizeof(char *);
1596 job_sections = realloc(job_sections, new_size);
1597 job_sections[nr_job_sections] = strdup(optarg);
1598 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01001599 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001600 }
Jens Axboe9ac8a792009-11-13 21:23:07 +01001601 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001602 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001603 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001604 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001605 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001606 char *val = optarg;
1607
1608 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe46bcd492012-03-14 11:31:21 +01001609 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe66251552011-05-12 10:14:57 +02001610 if (ret)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001611 return 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001612 td = NULL;
1613 }
1614 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001615 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001616 int global = 0;
1617
Jens Axboe01f06b62008-02-18 20:53:47 +01001618 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001619 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001620
Jens Axboe01f06b62008-02-18 20:53:47 +01001621 if (is_section && skip_this_section(val))
1622 continue;
1623
Steven Langde890a12011-11-09 14:03:34 +01001624 td = get_new_job(global, &def_thread, 1);
1625 if (!td || ioengine_load(td))
Jens Axboe214e1ec2007-03-15 10:48:13 +01001626 return 0;
Steven Langde890a12011-11-09 14:03:34 +01001627 fio_options_set_ioengine_opts(l_opts, td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001628 }
1629
1630 ret = fio_cmd_option_parse(td, opt, val);
Steven Langde890a12011-11-09 14:03:34 +01001631
1632 if (!ret && !strcmp(opt, "ioengine")) {
1633 free_ioengine(td);
1634 if (ioengine_load(td))
1635 return 0;
1636 fio_options_set_ioengine_opts(l_opts, td);
1637 }
1638 break;
1639 }
1640 case FIO_GETOPT_IOENGINE: {
1641 const char *opt = l_opts[lidx].name;
1642 char *val = optarg;
1643 opt = l_opts[lidx].name;
1644 val = optarg;
1645 ret = fio_cmd_ioengine_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001646 break;
1647 }
Jens Axboea9523c62011-01-17 16:49:54 -07001648 case 'w':
1649 warnings_fatal = 1;
1650 break;
Jens Axboefca70352011-07-06 20:12:54 +02001651 case 'j':
1652 max_jobs = atoi(optarg);
1653 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1654 log_err("fio: invalid max jobs: %d\n", max_jobs);
1655 do_exit++;
1656 exit_val = 1;
1657 }
1658 break;
Jens Axboe50d16972011-09-29 17:45:28 -06001659 case 'S':
Jens Axboea37f69b2011-10-01 12:24:21 -06001660 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06001661 log_err("fio: can't be both client and server\n");
1662 do_exit++;
1663 exit_val = 1;
1664 break;
1665 }
Jens Axboe87aa8f12011-10-06 21:24:13 +02001666 if (optarg)
Jens Axboebebe6392011-10-07 10:00:51 +02001667 fio_server_set_arg(optarg);
Jens Axboe50d16972011-09-29 17:45:28 -06001668 is_backend = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001669 backend = 1;
Jens Axboe50d16972011-09-29 17:45:28 -06001670 break;
Jens Axboee46d8092011-10-03 09:11:02 +02001671 case 'D':
Jens Axboe402668f2011-10-10 15:28:58 +02001672 pid_file = strdup(optarg);
Jens Axboee46d8092011-10-03 09:11:02 +02001673 break;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001674 case 'I':
1675 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1676 /* exit on error and calibration only */
1677 do_exit++;
1678 if (ret == -1)
1679 exit_val = 1;
1680 }
1681 break;
Jens Axboe132159a2011-09-30 15:01:32 -06001682 case 'C':
1683 if (is_backend) {
1684 log_err("fio: can't be both client and server\n");
1685 do_exit++;
1686 exit_val = 1;
1687 break;
1688 }
Jens Axboea5276612012-03-04 15:15:08 +01001689 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
Jens Axboebebe6392011-10-07 10:00:51 +02001690 log_err("fio: failed adding client %s\n", optarg);
1691 do_exit++;
1692 exit_val = 1;
1693 break;
1694 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02001695 /*
1696 * If the next argument exists and isn't an option,
1697 * assume it's a job file for this client only.
1698 */
1699 while (optind < argc) {
1700 if (!strncmp(argv[optind], "--", 2) ||
1701 !strncmp(argv[optind], "-", 1))
1702 break;
1703
1704 fio_client_add_ini_file(cur_client, argv[optind]);
1705 optind++;
1706 }
Jens Axboe132159a2011-09-30 15:01:32 -06001707 break;
Jens Axboe7d11f872012-12-17 12:03:29 +01001708 case 'T':
1709 do_exit++;
1710 exit_val = fio_monotonic_clocktest();
1711 break;
Jens Axboe798827c2013-01-23 18:11:48 -07001712 case '?':
1713 log_err("%s: unrecognized option '%s'\n", argv[0],
1714 argv[optind - 1]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001715 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001716 do_exit++;
1717 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001718 break;
1719 }
Jens Axboec7d5c942011-10-03 11:48:17 +02001720 if (do_exit)
1721 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001722 }
1723
Jens Axboec2c94582011-10-05 20:31:30 +02001724 if (do_exit) {
1725 if (exit_val && !(is_backend || nr_clients))
1726 exit(exit_val);
1727 }
Jens Axboe536582b2008-02-18 20:26:32 +01001728
Jens Axboea37f69b2011-10-01 12:24:21 -06001729 if (nr_clients && fio_clients_connect()) {
Jens Axboe132159a2011-09-30 15:01:32 -06001730 do_exit++;
1731 exit_val = 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02001732 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -06001733 }
1734
Jens Axboe81179ee2011-10-04 12:42:06 +02001735 if (is_backend && backend)
Jens Axboe402668f2011-10-10 15:28:58 +02001736 return fio_start_server(pid_file);
Jens Axboe50d16972011-09-29 17:45:28 -06001737
Jens Axboe214e1ec2007-03-15 10:48:13 +01001738 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001739 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001740 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001741 }
1742
Jens Axboe7874f8b2011-10-06 09:18:20 +02001743 while (!ret && optind < argc) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001744 ini_idx++;
1745 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1746 ini_file[ini_idx - 1] = strdup(argv[optind]);
1747 optind++;
1748 }
1749
1750 return ini_idx;
1751}
1752
Jens Axboe0420ba62012-02-29 11:16:52 +01001753int fio_init_options(void)
Jens Axboeebac4652005-12-08 15:25:21 +01001754{
Jens Axboeb4692822006-10-27 13:43:22 +02001755 f_out = stdout;
1756 f_err = stderr;
1757
Jens Axboe4c6107f2011-01-18 05:22:22 -07001758 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001759 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001760
Jens Axboe9d9eb2e2011-10-03 12:01:42 +02001761 atexit(free_shm);
1762
Jens Axboeebac4652005-12-08 15:25:21 +01001763 if (fill_def_thread())
1764 return 1;
1765
Jens Axboe0420ba62012-02-29 11:16:52 +01001766 return 0;
1767}
1768
Jens Axboe51167792012-03-08 21:34:18 +01001769extern int fio_check_options(struct thread_options *);
1770
Jens Axboe0420ba62012-02-29 11:16:52 +01001771int parse_options(int argc, char *argv[])
1772{
Jens Axboe46bcd492012-03-14 11:31:21 +01001773 const int type = FIO_CLIENT_TYPE_CLI;
Jens Axboe0420ba62012-02-29 11:16:52 +01001774 int job_files, i;
1775
1776 if (fio_init_options())
1777 return 1;
Jens Axboe51167792012-03-08 21:34:18 +01001778 if (fio_test_cconv(&def_thread.o))
1779 log_err("fio: failed internal cconv test\n");
Jens Axboe0420ba62012-02-29 11:16:52 +01001780
Jens Axboe46bcd492012-03-14 11:31:21 +01001781 job_files = parse_cmd_line(argc, argv, type);
Jens Axboeebac4652005-12-08 15:25:21 +01001782
Jens Axboecdf54d82011-10-04 08:31:40 +02001783 if (job_files > 0) {
1784 for (i = 0; i < job_files; i++) {
1785 if (fill_def_thread())
Jens Axboe132159a2011-09-30 15:01:32 -06001786 return 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02001787 if (nr_clients) {
1788 if (fio_clients_send_ini(ini_file[i]))
1789 return 1;
1790 free(ini_file[i]);
1791 } else if (!is_backend) {
Jens Axboe46bcd492012-03-14 11:31:21 +01001792 if (parse_jobs_ini(ini_file[i], 0, i, type))
Jens Axboecdf54d82011-10-04 08:31:40 +02001793 return 1;
1794 free(ini_file[i]);
1795 }
Jens Axboe132159a2011-09-30 15:01:32 -06001796 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02001797 } else if (nr_clients) {
1798 if (fill_def_thread())
1799 return 1;
1800 if (fio_clients_send_ini(NULL))
1801 return 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02001802 }
Jens Axboeebac4652005-12-08 15:25:21 +01001803
Jens Axboe88c6ed82006-06-09 11:28:10 +02001804 free(ini_file);
Jens Axboe7e356b22011-10-14 10:55:16 +02001805 fio_options_free(&def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001806
1807 if (!thread_number) {
Jens Axboe111e0322013-03-07 11:31:20 +01001808 if (dump_cmdline || parse_only)
Jens Axboecca73aa2007-04-04 11:09:19 +02001809 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01001810 if (exec_profile)
1811 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06001812 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06001813 return 0;
Jens Axboe085399d2011-10-06 09:01:22 +02001814 if (did_arg)
1815 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001816
Jens Axboed65e11c2011-10-05 09:56:53 +02001817 log_err("No jobs(s) defined\n\n");
Jens Axboe085399d2011-10-06 09:01:22 +02001818
1819 if (!did_arg) {
1820 usage(argv[0]);
1821 return 1;
1822 }
1823
1824 return 0;
Jens Axboeb4692822006-10-27 13:43:22 +02001825 }
1826
Jens Axboebe4ecfd2008-12-08 14:10:52 +01001827 if (def_thread.o.gtod_offload) {
1828 fio_gtod_init();
1829 fio_gtod_offload = 1;
1830 fio_gtod_cpu = def_thread.o.gtod_cpu;
1831 }
1832
Jens Axboef3afa572012-09-17 13:34:16 +02001833 if (output_format == FIO_OUTPUT_NORMAL)
Jens Axboe3d433822012-03-21 22:25:22 +01001834 log_info("%s\n", fio_version_string);
Jens Axboef6dea4d2011-10-13 13:37:07 +02001835
Jens Axboeebac4652005-12-08 15:25:21 +01001836 return 0;
1837}
Jens Axboe588b7f02012-03-19 20:37:41 +01001838
1839void options_default_fill(struct thread_options *o)
1840{
1841 memcpy(o, &def_thread.o, sizeof(*o));
1842}