blob: 72d106afbb4892f5ce586a8b3101efbe331b0478 [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;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +010046int append_terse_output = 0;
Jens Axboe5be4c942013-02-11 16:33:25 +010047int eta_print = FIO_ETA_AUTO;
Jens Axboee382e662013-02-22 20:48:56 +010048int eta_new_line = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +010049FILE *f_out = NULL;
50FILE *f_err = NULL;
Jens Axboead0a2732011-03-11 10:16:17 +010051char **job_sections = NULL;
52int nr_job_sections = 0;
Jens Axboe07b32322010-03-05 09:48:44 +010053char *exec_profile = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -070054int warnings_fatal = 0;
Jens Axboe4d658652011-10-17 15:05:47 +020055int terse_version = 3;
Jens Axboe50d16972011-09-29 17:45:28 -060056int is_backend = 0;
Jens Axboea37f69b2011-10-01 12:24:21 -060057int nr_clients = 0;
Jens Axboee46d8092011-10-03 09:11:02 +020058int log_syslog = 0;
Jens Axboeee738492007-01-10 11:23:16 +010059
Jens Axboe214e1ec2007-03-15 10:48:13 +010060int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020061int read_only = 0;
Jens Axboe06464902013-04-24 20:38:54 -060062int status_interval = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020063
Jens Axboe5ec10ea2008-03-06 15:42:00 +010064static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010065
66static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020067
Jens Axboeee56ad52008-02-01 10:30:20 +010068unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020069unsigned int fio_debug_jobno = -1;
70unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010071
Jens Axboe4c6107f2011-01-18 05:22:22 -070072static char cmd_optstr[256];
Jens Axboe085399d2011-10-06 09:01:22 +020073static int did_arg;
Jens Axboe4c6107f2011-01-18 05:22:22 -070074
Jens Axboe7a4b8242011-10-05 17:35:15 +020075#define FIO_CLIENT_FLAG (1 << 16)
76
Jens Axboeb4692822006-10-27 13:43:22 +020077/*
78 * Command line options. These will contain the above, plus a few
79 * extra that only pertain to fio itself and not jobs.
80 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010081static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020082 {
Jens Axboe08d2a192011-05-11 13:28:30 +020083 .name = (char *) "output",
Jens Axboeb4692822006-10-27 13:43:22 +020084 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020085 .val = 'o' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020086 },
87 {
Jens Axboe08d2a192011-05-11 13:28:30 +020088 .name = (char *) "timeout",
Jens Axboeb4692822006-10-27 13:43:22 +020089 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020090 .val = 't' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020091 },
92 {
Jens Axboe08d2a192011-05-11 13:28:30 +020093 .name = (char *) "latency-log",
Jens Axboeb4692822006-10-27 13:43:22 +020094 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020095 .val = 'l' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020096 },
97 {
Jens Axboe08d2a192011-05-11 13:28:30 +020098 .name = (char *) "bandwidth-log",
Jens Axboeb4692822006-10-27 13:43:22 +020099 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200100 .val = 'b' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200101 },
102 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200103 .name = (char *) "minimal",
Stefan Hajnoczic4ec0a12014-02-04 14:27:10 +0100104 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200105 .val = 'm' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200106 },
107 {
Jens Axboef3afa572012-09-17 13:34:16 +0200108 .name = (char *) "output-format",
Shaohua Licc372b12012-09-17 09:12:18 +0200109 .has_arg = optional_argument,
Jens Axboef3afa572012-09-17 13:34:16 +0200110 .val = 'F' | FIO_CLIENT_FLAG,
Shaohua Licc372b12012-09-17 09:12:18 +0200111 },
112 {
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +0100113 .name = (char *) "append-terse",
114 .has_arg = optional_argument,
115 .val = 'f',
116 },
117 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200118 .name = (char *) "version",
Jens Axboeb4692822006-10-27 13:43:22 +0200119 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200120 .val = 'v' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200121 },
122 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200123 .name = (char *) "help",
Jens Axboefd28ca42007-01-09 21:20:13 +0100124 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200125 .val = 'h' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100126 },
127 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200128 .name = (char *) "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100129 .has_arg = optional_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200130 .val = 'c' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100131 },
132 {
Jens Axboe06464902013-04-24 20:38:54 -0600133 .name = (char *) "enghelp",
Steven Langde890a12011-11-09 14:03:34 +0100134 .has_arg = optional_argument,
Jens Axboe06464902013-04-24 20:38:54 -0600135 .val = 'i' | FIO_CLIENT_FLAG,
Steven Langde890a12011-11-09 14:03:34 +0100136 },
137 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200138 .name = (char *) "showcmd",
Jens Axboecca73aa2007-04-04 11:09:19 +0200139 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200140 .val = 's' | FIO_CLIENT_FLAG,
Jens Axboe724e4432007-09-11 20:02:05 +0200141 },
142 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200143 .name = (char *) "readonly",
Jens Axboe724e4432007-09-11 20:02:05 +0200144 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200145 .val = 'r' | FIO_CLIENT_FLAG,
Jens Axboecca73aa2007-04-04 11:09:19 +0200146 },
147 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200148 .name = (char *) "eta",
Aaron Carrolle592a062007-09-14 09:49:41 +0200149 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200150 .val = 'e' | FIO_CLIENT_FLAG,
Aaron Carrolle592a062007-09-14 09:49:41 +0200151 },
152 {
Jens Axboee382e662013-02-22 20:48:56 +0100153 .name = (char *) "eta-newline",
154 .has_arg = required_argument,
155 .val = 'E' | FIO_CLIENT_FLAG,
156 },
157 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200158 .name = (char *) "debug",
Jens Axboeee56ad52008-02-01 10:30:20 +0100159 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200160 .val = 'd' | FIO_CLIENT_FLAG,
Jens Axboeee56ad52008-02-01 10:30:20 +0100161 },
162 {
Jens Axboe111e0322013-03-07 11:31:20 +0100163 .name = (char *) "parse-only",
164 .has_arg = no_argument,
165 .val = 'P' | FIO_CLIENT_FLAG,
166 },
167 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200168 .name = (char *) "section",
Jens Axboe01f06b62008-02-18 20:53:47 +0100169 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200170 .val = 'x' | FIO_CLIENT_FLAG,
Jens Axboe01f06b62008-02-18 20:53:47 +0100171 },
172 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200173 .name = (char *) "alloc-size",
Jens Axboe2b386d22008-03-26 10:32:57 +0100174 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200175 .val = 'a' | FIO_CLIENT_FLAG,
Jens Axboe2b386d22008-03-26 10:32:57 +0100176 },
177 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200178 .name = (char *) "profile",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100179 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200180 .val = 'p' | FIO_CLIENT_FLAG,
Jens Axboe9ac8a792009-11-13 21:23:07 +0100181 },
182 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200183 .name = (char *) "warnings-fatal",
Jens Axboea9523c62011-01-17 16:49:54 -0700184 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200185 .val = 'w' | FIO_CLIENT_FLAG,
Jens Axboea9523c62011-01-17 16:49:54 -0700186 },
187 {
Jens Axboefca70352011-07-06 20:12:54 +0200188 .name = (char *) "max-jobs",
189 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200190 .val = 'j' | FIO_CLIENT_FLAG,
Jens Axboefca70352011-07-06 20:12:54 +0200191 },
192 {
Jens Axboef57a9c52011-09-09 21:01:37 +0200193 .name = (char *) "terse-version",
194 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200195 .val = 'V' | FIO_CLIENT_FLAG,
Jens Axboef57a9c52011-09-09 21:01:37 +0200196 },
197 {
Jens Axboe50d16972011-09-29 17:45:28 -0600198 .name = (char *) "server",
Jens Axboe87aa8f12011-10-06 21:24:13 +0200199 .has_arg = optional_argument,
Jens Axboe50d16972011-09-29 17:45:28 -0600200 .val = 'S',
201 },
Jens Axboee46d8092011-10-03 09:11:02 +0200202 { .name = (char *) "daemonize",
Jens Axboe402668f2011-10-10 15:28:58 +0200203 .has_arg = required_argument,
Jens Axboee46d8092011-10-03 09:11:02 +0200204 .val = 'D',
205 },
Jens Axboe50d16972011-09-29 17:45:28 -0600206 {
Jens Axboe132159a2011-09-30 15:01:32 -0600207 .name = (char *) "client",
208 .has_arg = required_argument,
209 .val = 'C',
210 },
211 {
Jens Axboe7d11f872012-12-17 12:03:29 +0100212 .name = (char *) "cpuclock-test",
213 .has_arg = no_argument,
214 .val = 'T',
215 },
216 {
Jens Axboefec0f212014-02-07 14:39:33 -0700217 .name = (char *) "crctest",
218 .has_arg = optional_argument,
219 .val = 'G',
220 },
221 {
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100222 .name = (char *) "idle-prof",
223 .has_arg = required_argument,
224 .val = 'I',
225 },
226 {
Jens Axboe06464902013-04-24 20:38:54 -0600227 .name = (char *) "status-interval",
228 .has_arg = required_argument,
229 .val = 'L',
230 },
231 {
Jens Axboeb4692822006-10-27 13:43:22 +0200232 .name = NULL,
233 },
234};
235
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200236void free_threads_shm(void)
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200237{
238 struct shmid_ds sbuf;
239
240 if (threads) {
241 void *tp = threads;
242
243 threads = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200244 shmdt(tp);
245 shmctl(shm_id, IPC_RMID, &sbuf);
246 shm_id = -1;
247 }
248}
249
250void free_shm(void)
251{
252 if (threads) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200253 file_hash_exit();
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100254 flow_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200255 fio_debug_jobp = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200256 free_threads_shm();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200257 }
258
Jens Axboeeb0c74a2014-02-11 10:33:06 -0700259 options_free(fio_options, &def_thread);
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200260 scleanup();
261}
262
263/*
264 * The thread area is shared between the main process and the job
265 * threads/processes. So setup a shared memory segment that will hold
266 * all the job info. We use the end of the region for keeping track of
267 * open files across jobs, for file sharing.
268 */
269static int setup_thread_area(void)
270{
271 void *hash;
272
273 if (threads)
274 return 0;
275
276 /*
277 * 1024 is too much on some machines, scale max_jobs if
278 * we get a failure that looks like too large a shm segment
279 */
280 do {
281 size_t size = max_jobs * sizeof(struct thread_data);
282
283 size += file_hash_size;
284 size += sizeof(unsigned int);
285
286 shm_id = shmget(0, size, IPC_CREAT | 0600);
287 if (shm_id != -1)
288 break;
Jens Axboef0c77f02012-09-21 15:05:45 +0200289 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200290 perror("shmget");
291 break;
292 }
293
294 max_jobs >>= 1;
295 } while (max_jobs);
296
297 if (shm_id == -1)
298 return 1;
299
300 threads = shmat(shm_id, NULL, 0);
301 if (threads == (void *) -1) {
302 perror("shmat");
303 return 1;
304 }
305
306 memset(threads, 0, max_jobs * sizeof(struct thread_data));
307 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
308 fio_debug_jobp = (void *) hash + file_hash_size;
309 *fio_debug_jobp = -1;
310 file_hash_init(hash);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100311
312 flow_init();
313
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200314 return 0;
315}
316
Jens Axboe906c8d72006-06-13 09:37:56 +0200317/*
318 * Return a free job structure.
319 */
Steven Langde890a12011-11-09 14:03:34 +0100320static struct thread_data *get_new_job(int global, struct thread_data *parent,
321 int preserve_eo)
Jens Axboeebac4652005-12-08 15:25:21 +0100322{
323 struct thread_data *td;
324
325 if (global)
326 return &def_thread;
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200327 if (setup_thread_area()) {
328 log_err("error: failed to setup shm segment\n");
329 return NULL;
330 }
Bruce Crane61f1ec2011-01-14 18:30:26 +0100331 if (thread_number >= max_jobs) {
332 log_err("error: maximum number of jobs (%d) reached.\n",
333 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100334 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100335 }
Jens Axboeebac4652005-12-08 15:25:21 +0100336
337 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200338 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100339
Steven Langde890a12011-11-09 14:03:34 +0100340 td->io_ops = NULL;
341 if (!preserve_eo)
342 td->eo = NULL;
343
Jens Axboee0b0d892009-12-08 10:10:14 +0100344 td->o.uid = td->o.gid = -1U;
345
Jens Axboecade3ef2007-03-23 15:29:45 +0100346 dup_files(td, parent);
Steven Langde890a12011-11-09 14:03:34 +0100347 fio_options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100348
Jens Axboe15dc1932010-03-05 10:59:06 +0100349 profile_add_hooks(td);
350
Jens Axboeebac4652005-12-08 15:25:21 +0100351 td->thread_number = thread_number;
Jens Axboe108fea72012-11-14 13:09:45 -0700352
353 if (!parent || !parent->o.group_reporting)
354 stat_number++;
355
Jens Axboeebac4652005-12-08 15:25:21 +0100356 return td;
357}
358
359static void put_job(struct thread_data *td)
360{
Jens Axboe549577a2006-10-30 12:23:41 +0100361 if (td == &def_thread)
362 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600363
Jens Axboe58c55ba2010-03-09 12:20:08 +0100364 profile_td_exit(td);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100365 flow_exit_job(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100366
Jens Axboe16edf252007-02-10 14:59:22 +0100367 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100368 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100369
Jens Axboe7e356b22011-10-14 10:55:16 +0200370 fio_options_free(td);
Steven Langde890a12011-11-09 14:03:34 +0100371 if (td->io_ops)
372 free_ioengine(td);
Jens Axboe7e356b22011-10-14 10:55:16 +0200373
Jens Axboeebac4652005-12-08 15:25:21 +0100374 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
375 thread_number--;
376}
377
Jens Axboe581e7142009-06-09 12:47:16 +0200378static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100379{
Jens Axboe581e7142009-06-09 12:47:16 +0200380 unsigned int bs = td->o.min_bs[ddir];
Jens Axboe127f6862007-03-15 12:09:57 +0100381
Jens Axboeff58fce2010-08-25 12:02:08 +0200382 assert(ddir_rw(ddir));
383
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100384 if (td->o.rate[ddir])
Steven Lang1b8dbf22011-11-09 13:48:01 +0100385 td->rate_bps[ddir] = td->o.rate[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100386 else
Steven Lang1b8dbf22011-11-09 13:48:01 +0100387 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100388
Steven Lang1b8dbf22011-11-09 13:48:01 +0100389 if (!td->rate_bps[ddir]) {
Jens Axboe127f6862007-03-15 12:09:57 +0100390 log_err("rate lower than supported\n");
391 return -1;
392 }
393
Jens Axboe581e7142009-06-09 12:47:16 +0200394 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100395 return 0;
396}
397
Jens Axboe581e7142009-06-09 12:47:16 +0200398static int setup_rate(struct thread_data *td)
399{
400 int ret = 0;
401
402 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
403 ret = __setup_rate(td, DDIR_READ);
404 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
405 ret |= __setup_rate(td, DDIR_WRITE);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200406 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
407 ret |= __setup_rate(td, DDIR_TRIM);
Jens Axboe581e7142009-06-09 12:47:16 +0200408
409 return ret;
410}
411
Jens Axboe83472392009-02-19 21:32:12 +0100412static int fixed_block_size(struct thread_options *o)
413{
414 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
415 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200416 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
417 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
418 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
Jens Axboe83472392009-02-19 21:32:12 +0100419}
420
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800421
422static unsigned long long get_rand_start_delay(struct thread_data *td)
423{
424 unsigned long long delayrange;
425 unsigned long r;
426
427 delayrange = td->o.start_delay_high - td->o.start_delay;
428
429 if (td->o.use_os_rand) {
430 r = os_random_long(&td->delay_state);
431 delayrange = (unsigned long long) ((double) delayrange * (r / (OS_RAND_MAX + 1.0)));
432 } else {
433 r = __rand(&td->__delay_state);
434 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
435 }
436
437 delayrange += td->o.start_delay;
438 return delayrange;
439}
440
Jens Axboedad915e2006-10-27 11:10:18 +0200441/*
442 * Lazy way of fixing up options that depend on each other. We could also
443 * define option callback handlers, but this is easier.
444 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100445static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200446{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100447 struct thread_options *o = &td->o;
Jens Axboeeefd98b2013-07-25 12:30:06 -0600448 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200449
Jens Axboef356d012009-01-05 09:56:29 +0100450#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100451 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100452 log_info("fio: this platform does not support process shared"
453 " mutexes, forcing use of threads. Use the 'thread'"
454 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100455 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700456 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100457 }
458#endif
459
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100460 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200461 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100462 free(o->write_iolog_file);
463 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700464 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200465 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100466
Jens Axboe16b462a2006-10-30 12:35:18 +0100467 /*
Steven Noonaned335852012-01-31 13:58:00 +0100468 * only really works with 1 file
Jens Axboe16b462a2006-10-30 12:35:18 +0100469 */
Jens Axboe627aa1a2013-02-07 13:06:18 +0100470 if (o->zone_size && o->open_files > 1)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100471 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100472
473 /*
Steven Noonaned335852012-01-31 13:58:00 +0100474 * If zone_range isn't specified, backward compatibility dictates it
475 * should be made equal to zone_size.
476 */
477 if (o->zone_size && !o->zone_range)
478 o->zone_range = o->zone_size;
479
480 /*
Jens Axboe16b462a2006-10-30 12:35:18 +0100481 * Reads can do overwrites, we always need to pre-create the file
482 */
483 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100484 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100485
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100486 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100487 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100488 if (!o->max_bs[DDIR_READ])
489 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
490 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100491 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100492 if (!o->max_bs[DDIR_WRITE])
493 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200494 if (!o->min_bs[DDIR_TRIM])
495 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
496 if (!o->max_bs[DDIR_TRIM])
497 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
498
Jens Axboea00735e2006-11-03 08:58:08 +0100499
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100500 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200501 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
Jens Axboea00735e2006-11-03 08:58:08 +0100502
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100503 /*
504 * For random IO, allow blockalign offset other than min_bs.
505 */
506 if (!o->ba[DDIR_READ] || !td_random(td))
507 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
508 if (!o->ba[DDIR_WRITE] || !td_random(td))
509 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200510 if (!o->ba[DDIR_TRIM] || !td_random(td))
511 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100512
513 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200514 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
515 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100516 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100517 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100518 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700519 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100520 }
521
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100522 if (!o->file_size_high)
523 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100524
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800525 if (o->start_delay_high)
526 o->start_delay = get_rand_start_delay(td);
527
Jens Axboe83472392009-02-19 21:32:12 +0100528 if (o->norandommap && o->verify != VERIFY_NONE
529 && !fixed_block_size(o)) {
530 log_err("fio: norandommap given for variable block sizes, "
531 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100532 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700533 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100534 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100535 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100536 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100537
538 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100539 * thinktime_spin must be less than thinktime
540 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100541 if (o->thinktime_spin > o->thinktime)
542 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100543
544 /*
545 * The low water mark cannot be bigger than the iodepth
546 */
Jens Axboe67bf9822013-01-10 11:23:19 +0100547 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
548 o->iodepth_low = o->iodepth;
Jens Axboecb5ab512007-02-26 12:57:09 +0100549
550 /*
551 * If batch number isn't set, default to the same as iodepth
552 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100553 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
554 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100555
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100556 if (o->nr_files > td->files_index)
557 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100558
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100559 if (o->open_files > o->nr_files || !o->open_files)
560 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100561
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200562 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
563 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
564 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
565 (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 +0100566 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700567 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100568 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200569 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
570 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
571 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
572 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
573 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
574 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100575 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700576 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100577 }
578
Jens Axboecf4464c2007-04-17 20:14:42 +0200579 if (!o->timeout && o->time_based) {
580 log_err("fio: time_based requires a runtime/timeout setting\n");
581 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700582 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200583 }
584
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100585 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200586 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100587
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100588 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700589 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700590 log_info("Multiple writers may overwrite blocks that "
591 "belong to other jobs. This can cause "
592 "verification failures.\n");
593 ret = warnings_fatal;
594 }
595
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100596 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200597 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
598 !o->verify_interval)
599 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboed9905882010-03-18 20:43:00 +0100600 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200601
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100602 if (o->pre_read) {
603 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700604 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200605 log_info("fio: cannot pre-read files with an IO engine"
606 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700607 ret = warnings_fatal;
608 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200609 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200610
Steven Noonanad705bc2013-04-08 15:05:25 -0700611 if (!o->unit_base) {
612 if (td->io_ops->flags & FIO_BIT_BASED)
613 o->unit_base = 1;
614 else
615 o->unit_base = 8;
616 }
617
Jens Axboe67bf9822013-01-10 11:23:19 +0100618#ifndef CONFIG_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100619 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700620 log_info("fio: this platform does not support fdatasync()"
621 " falling back to using fsync(). Use the 'fsync'"
622 " option instead of 'fdatasync' to get rid of"
623 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100624 o->fsync_blocks = o->fdatasync_blocks;
625 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700626 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700627 }
628#endif
629
Bruce Cran93bcfd22012-02-20 20:18:19 +0100630#ifdef WIN32
631 /*
632 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
633 * so fail if we're passed those flags
634 */
635 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
636 log_err("fio: Windows does not support direct or non-buffered io with"
637 " the synchronous ioengines. Use the 'windowsaio' ioengine"
638 " with 'direct=1' and 'iodepth=1' instead.\n");
639 ret = 1;
640 }
641#endif
642
Jens Axboe811ac502012-03-02 22:23:36 +0100643 /*
644 * For fully compressible data, just zero them at init time.
645 * It's faster than repeatedly filling it.
646 */
647 if (td->o.compress_percentage == 100) {
648 td->o.zero_buffers = 1;
649 td->o.compress_percentage = 0;
650 }
651
Jens Axboe5881fda2012-11-15 15:21:23 -0700652 /*
653 * Using a non-uniform random distribution excludes usage of
654 * a random map
655 */
656 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
657 td->o.norandommap = 1;
658
Jens Axboe8d916c92013-04-25 10:11:41 -0600659 /*
660 * If size is set but less than the min block size, complain
661 */
662 if (o->size && o->size < td_min_bs(td)) {
663 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
664 ret = 1;
665 }
666
Chris Masond01612f2013-11-15 15:52:58 -0700667 /*
668 * O_ATOMIC implies O_DIRECT
669 */
670 if (td->o.oatomic)
671 td->o.odirect = 1;
672
Jens Axboe04778ba2014-01-10 20:57:01 -0700673 /*
674 * If randseed is set, that overrides randrepeat
675 */
676 if (td->o.rand_seed)
677 td->o.rand_repeatable = 0;
678
Jens Axboeb1bebc32014-03-18 13:30:32 -0600679 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
680 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
681 ret = 1;
682 }
683
Jens Axboea9523c62011-01-17 16:49:54 -0700684 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200685}
686
Jens Axboe906c8d72006-06-13 09:37:56 +0200687/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100688 * This function leaks the buffer
689 */
Jens Axboe99d633a2012-03-15 15:55:04 +0100690char *fio_uint_to_kmg(unsigned int val)
Jens Axboef8977ee2006-11-06 14:03:03 +0100691{
692 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100693 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100694 char *p = post;
695
Jens Axboe245142f2006-11-08 12:49:21 +0100696 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100697 if (val & 1023)
698 break;
699
700 val >>= 10;
701 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100702 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100703
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100704 snprintf(buf, 32, "%u%c", val, *p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100705 return buf;
706}
707
Jens Axboe09629a92007-03-09 09:00:06 +0100708/* External engines are specified by "external:name.o") */
709static const char *get_engine_name(const char *str)
710{
711 char *p = strstr(str, ":");
712
713 if (!p)
714 return str;
715
716 p++;
717 strip_blank_front(&p);
718 strip_blank_end(p);
719 return p;
720}
721
Jens Axboee132cba2007-03-14 14:23:54 +0100722static int exists_and_not_file(const char *filename)
723{
724 struct stat sb;
725
726 if (lstat(filename, &sb) == -1)
727 return 0;
728
Bruce Cranecc314b2011-01-04 10:59:30 +0100729 /* \\.\ is the device namespace in Windows, where every file
730 * is a device node */
731 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100732 return 0;
733
734 return 1;
735}
736
Jens Axboe4c07ad82011-03-28 09:51:09 +0200737static void td_fill_rand_seeds_os(struct thread_data *td)
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200738{
Jens Axboed24945f2012-12-04 13:10:29 +0100739 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
740 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
741 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200742
743 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100744 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200745
Jens Axboed24945f2012-12-04 13:10:29 +0100746 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
747 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800748 os_random_seed(td->rand_seeds[FIO_RAND_START_DELAY], &td->delay_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200749
750 if (!td_random(td))
751 return;
752
753 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100754 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200755
Jens Axboed24945f2012-12-04 13:10:29 +0100756 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
Jens Axboed9472272013-07-25 10:20:45 -0600757
758 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], &td->seq_rand_state[DDIR_READ]);
759 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], &td->seq_rand_state[DDIR_WRITE]);
760 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], &td->seq_rand_state[DDIR_TRIM]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200761}
762
763static void td_fill_rand_seeds_internal(struct thread_data *td)
764{
Jens Axboed24945f2012-12-04 13:10:29 +0100765 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
766 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
767 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200768
769 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100770 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200771
Jens Axboed24945f2012-12-04 13:10:29 +0100772 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
773 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800774 init_rand_seed(&td->__delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200775
776 if (!td_random(td))
777 return;
778
779 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100780 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe4c07ad82011-03-28 09:51:09 +0200781
Jens Axboed24945f2012-12-04 13:10:29 +0100782 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
Jens Axboed9472272013-07-25 10:20:45 -0600783 init_rand_seed(&td->__seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
784 init_rand_seed(&td->__seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
785 init_rand_seed(&td->__seq_rand_state[DDIR_TRIM], td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF]);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200786}
787
Jens Axboe4c07ad82011-03-28 09:51:09 +0200788void td_fill_rand_seeds(struct thread_data *td)
789{
Christian Ehrhardt56e2a5f2014-02-20 09:10:17 -0800790 if (td->o.allrand_repeatable) {
791 for (int i = 0; i < FIO_RAND_NR_OFFS; i++)
792 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
793 + i;
794 }
795
Jens Axboe4c07ad82011-03-28 09:51:09 +0200796 if (td->o.use_os_rand)
797 td_fill_rand_seeds_os(td);
798 else
799 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600800
Jens Axboed24945f2012-12-04 13:10:29 +0100801 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200802}
803
Jens Axboe9c60ce62007-03-15 09:14:47 +0100804/*
Steven Langde890a12011-11-09 14:03:34 +0100805 * Initializes the ioengine configured for a job, if it has not been done so
806 * already.
807 */
808int ioengine_load(struct thread_data *td)
809{
810 const char *engine;
811
812 /*
813 * Engine has already been loaded.
814 */
815 if (td->io_ops)
816 return 0;
Jens Axboe6e5c4b82013-05-27 09:50:31 +0200817 if (!td->o.ioengine) {
818 log_err("fio: internal fault, no IO engine specified\n");
819 return 1;
820 }
Steven Langde890a12011-11-09 14:03:34 +0100821
822 engine = get_engine_name(td->o.ioengine);
823 td->io_ops = load_ioengine(td, engine);
824 if (!td->io_ops) {
825 log_err("fio: failed to load engine %s\n", engine);
826 return 1;
827 }
828
829 if (td->io_ops->option_struct_size && td->io_ops->options) {
830 /*
831 * In cases where td->eo is set, clone it for a child thread.
832 * This requires that the parent thread has the same ioengine,
833 * but that requirement must be enforced by the code which
834 * cloned the thread.
835 */
836 void *origeo = td->eo;
837 /*
838 * Otherwise use the default thread options.
839 */
840 if (!origeo && td != &def_thread && def_thread.eo &&
841 def_thread.io_ops->options == td->io_ops->options)
842 origeo = def_thread.eo;
843
844 options_init(td->io_ops->options);
845 td->eo = malloc(td->io_ops->option_struct_size);
846 /*
847 * Use the default thread as an option template if this uses the
848 * same options structure and there are non-default options
849 * used.
850 */
851 if (origeo) {
852 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
853 options_mem_dupe(td->eo, td->io_ops->options);
854 } else {
855 memset(td->eo, 0, td->io_ops->option_struct_size);
856 fill_default_options(td->eo, td->io_ops->options);
857 }
858 *(struct thread_data **)td->eo = td;
859 }
860
861 return 0;
862}
863
Jens Axboed72be542012-11-30 19:37:46 +0100864static void init_flags(struct thread_data *td)
865{
866 struct thread_options *o = &td->o;
867
868 if (o->verify_backlog)
869 td->flags |= TD_F_VER_BACKLOG;
870 if (o->trim_backlog)
871 td->flags |= TD_F_TRIM_BACKLOG;
872 if (o->read_iolog_file)
873 td->flags |= TD_F_READ_IOLOG;
874 if (o->refill_buffers)
875 td->flags |= TD_F_REFILL_BUFFERS;
Jens Axboebedc9dc2014-03-17 12:51:09 -0600876 if (o->scramble_buffers)
Jens Axboed72be542012-11-30 19:37:46 +0100877 td->flags |= TD_F_SCRAMBLE_BUFFERS;
878 if (o->verify != VERIFY_NONE)
879 td->flags |= TD_F_VER_NONE;
880}
881
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700882static int setup_random_seeds(struct thread_data *td)
883{
884 unsigned long seed;
885 unsigned int i;
886
Jens Axboe04778ba2014-01-10 20:57:01 -0700887 if (!td->o.rand_repeatable && !td->o.rand_seed)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700888 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
889
Jens Axboe04778ba2014-01-10 20:57:01 -0700890 if (!td->o.rand_seed)
891 seed = 0x89;
892 else
893 seed = td->o.rand_seed;
894
895 for (i = 0; i < 4; i++)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700896 seed *= 0x9e370001UL;
897
898 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
899 td->rand_seeds[i] = seed;
900 seed *= 0x9e370001UL;
901 }
902
903 td_fill_rand_seeds(td);
904 return 0;
905}
906
Jens Axboede98bd32013-04-05 11:09:20 +0200907enum {
908 FPRE_NONE = 0,
909 FPRE_JOBNAME,
910 FPRE_JOBNUM,
911 FPRE_FILENUM
912};
913
914static struct fpre_keyword {
915 const char *keyword;
916 size_t strlen;
917 int key;
918} fpre_keywords[] = {
919 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
920 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
921 { .keyword = "$filenum", .key = FPRE_FILENUM, },
922 { .keyword = NULL, },
923 };
924
925static char *make_filename(char *buf, struct thread_options *o,
926 const char *jobname, int jobnum, int filenum)
927{
928 struct fpre_keyword *f;
929 char copy[PATH_MAX];
930
931 if (!o->filename_format || !strlen(o->filename_format)) {
932 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
933 return NULL;
934 }
935
936 for (f = &fpre_keywords[0]; f->keyword; f++)
937 f->strlen = strlen(f->keyword);
938
939 strcpy(buf, o->filename_format);
940 memset(copy, 0, sizeof(copy));
941 for (f = &fpre_keywords[0]; f->keyword; f++) {
942 do {
943 size_t pre_len, post_start = 0;
944 char *str, *dst = copy;
945
Jens Axboee25b6d52013-04-05 14:44:51 +0200946 str = strcasestr(buf, f->keyword);
Jens Axboede98bd32013-04-05 11:09:20 +0200947 if (!str)
948 break;
949
950 pre_len = str - buf;
951 if (strlen(str) != f->strlen)
952 post_start = pre_len + f->strlen;
953
954 if (pre_len) {
955 strncpy(dst, buf, pre_len);
956 dst += pre_len;
957 }
958
959 switch (f->key) {
960 case FPRE_JOBNAME:
961 dst += sprintf(dst, "%s", jobname);
962 break;
963 case FPRE_JOBNUM:
964 dst += sprintf(dst, "%d", jobnum);
965 break;
966 case FPRE_FILENUM:
967 dst += sprintf(dst, "%d", filenum);
968 break;
969 default:
970 assert(0);
971 break;
972 }
973
974 if (post_start)
975 strcpy(dst, buf + post_start);
976
977 strcpy(buf, copy);
978 } while (1);
979 }
980
981 return buf;
982}
Jens Axboef9633d72013-09-06 09:59:56 -0600983
984int parse_dryrun(void)
985{
986 return dump_cmdline || parse_only;
987}
988
Steven Langde890a12011-11-09 14:03:34 +0100989/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200990 * Adds a job to the list of things todo. Sanitizes the various options
991 * to make sure we don't have conflicts, and initializes various
992 * members of td.
993 */
Jens Axboeb7cfb2e2012-03-12 07:47:27 +0100994static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
Jens Axboe46bcd492012-03-14 11:31:21 +0100995 int recursed, int client_type)
Jens Axboeebac4652005-12-08 15:25:21 +0100996{
Jens Axboeaf52b342007-03-13 10:07:47 +0100997 unsigned int i;
Jens Axboeaf52b342007-03-13 10:07:47 +0100998 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100999 int numjobs, file_alloced;
Jens Axboede98bd32013-04-05 11:09:20 +02001000 struct thread_options *o = &td->o;
Jens Axboeebac4652005-12-08 15:25:21 +01001001
Jens Axboeebac4652005-12-08 15:25:21 +01001002 /*
1003 * the def_thread is just for options, it's not a real job
1004 */
1005 if (td == &def_thread)
1006 return 0;
1007
Jens Axboed72be542012-11-30 19:37:46 +01001008 init_flags(td);
1009
Jens Axboecca73aa2007-04-04 11:09:19 +02001010 /*
1011 * if we are just dumping the output command line, don't add the job
1012 */
Jens Axboef9633d72013-09-06 09:59:56 -06001013 if (parse_dryrun()) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001014 put_job(td);
1015 return 0;
1016 }
1017
Jens Axboe46bcd492012-03-14 11:31:21 +01001018 td->client_type = client_type;
1019
Jens Axboe58c55ba2010-03-09 12:20:08 +01001020 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +02001021 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +01001022
Steven Langde890a12011-11-09 14:03:34 +01001023 if (ioengine_load(td))
Jens Axboe205927a2007-03-15 11:06:32 +01001024 goto err;
Jens Axboedf641192006-10-12 07:55:41 +02001025
Jens Axboede98bd32013-04-05 11:09:20 +02001026 if (o->odirect)
Jens Axboe690adba2006-10-30 15:25:09 +01001027 td->io_ops->flags |= FIO_RAWIO;
1028
Jens Axboee132cba2007-03-14 14:23:54 +01001029 file_alloced = 0;
Jens Axboede98bd32013-04-05 11:09:20 +02001030 if (!o->filename && !td->files_index && !o->read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +01001031 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +01001032
Jens Axboede98bd32013-04-05 11:09:20 +02001033 if (o->nr_files == 1 && exists_and_not_file(jobname))
Jens Axboe5903e7b2014-02-26 13:42:13 -08001034 add_file(td, jobname, job_add_num, 0);
Jens Axboe7b05a212007-03-13 11:17:07 +01001035 else {
Jens Axboede98bd32013-04-05 11:09:20 +02001036 for (i = 0; i < o->nr_files; i++)
Jens Axboe5903e7b2014-02-26 13:42:13 -08001037 add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
Jens Axboeaf52b342007-03-13 10:07:47 +01001038 }
Jens Axboe0af7b542006-02-17 10:10:12 +01001039 }
Jens Axboeebac4652005-12-08 15:25:21 +01001040
Jens Axboe4e991c22007-03-15 11:41:11 +01001041 if (fixup_options(td))
1042 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +01001043
Dan Ehrenberg9e684a42012-02-20 11:05:14 +01001044 flow_init_job(td);
1045
Steven Langde890a12011-11-09 14:03:34 +01001046 /*
1047 * IO engines only need this for option callbacks, and the address may
1048 * change in subprocesses.
1049 */
1050 if (td->eo)
1051 *(struct thread_data **)td->eo = NULL;
1052
Jens Axboe07eb79d2007-04-12 13:02:38 +02001053 if (td->io_ops->flags & FIO_DISKLESSIO) {
1054 struct fio_file *f;
1055
1056 for_each_file(td, f, i)
1057 f->real_file_size = -1ULL;
1058 }
1059
Jens Axboe521da522012-08-02 11:21:36 +02001060 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboeebac4652005-12-08 15:25:21 +01001061
Jens Axboede98bd32013-04-05 11:09:20 +02001062 td->ts.clat_percentiles = o->clat_percentiles;
1063 td->ts.percentile_precision = o->percentile_precision;
1064 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
Yu-ju Hong83349192011-08-13 00:53:44 +02001065
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001066 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1067 td->ts.clat_stat[i].min_val = ULONG_MAX;
1068 td->ts.slat_stat[i].min_val = ULONG_MAX;
1069 td->ts.lat_stat[i].min_val = ULONG_MAX;
1070 td->ts.bw_stat[i].min_val = ULONG_MAX;
1071 }
Jens Axboede98bd32013-04-05 11:09:20 +02001072 td->ddir_seq_nr = o->ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +01001073
Jens Axboede98bd32013-04-05 11:09:20 +02001074 if ((o->stonewall || o->new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001075 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001076 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001077 }
Jens Axboeebac4652005-12-08 15:25:21 +01001078
1079 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001080 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +01001081
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001082 if (setup_random_seeds(td)) {
Bruce Cran93bcfd22012-02-20 20:18:19 +01001083 td_verror(td, errno, "init_random_state");
Jens Axboe9c60ce62007-03-15 09:14:47 +01001084 goto err;
Bruce Cran93bcfd22012-02-20 20:18:19 +01001085 }
Jens Axboe9c60ce62007-03-15 09:14:47 +01001086
Jens Axboeebac4652005-12-08 15:25:21 +01001087 if (setup_rate(td))
1088 goto err;
1089
Jens Axboe22f80452013-04-09 20:29:16 +02001090 if (o->lat_log_file) {
1091 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
1092 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
1093 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
Jens Axboeebac4652005-12-08 15:25:21 +01001094 }
Jens Axboe22f80452013-04-09 20:29:16 +02001095 if (o->bw_log_file)
1096 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1097 if (o->iops_log_file)
1098 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
Jens Axboeebac4652005-12-08 15:25:21 +01001099
Jens Axboede98bd32013-04-05 11:09:20 +02001100 if (!o->name)
1101 o->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +02001102
Jens Axboef3afa572012-09-17 13:34:16 +02001103 if (output_format == FIO_OUTPUT_NORMAL) {
Jens Axboeb990b5c2006-09-14 09:48:22 +02001104 if (!job_add_num) {
Jens Axboeb7cfb2e2012-03-12 07:47:27 +01001105 if (is_backend && !recursed)
Jens Axboe2f122b12012-03-15 13:10:19 +01001106 fio_server_send_add_job(td);
Jens Axboe807f9972012-03-02 10:25:24 +01001107
Jens Axboe03530502012-03-19 21:45:12 +01001108 if (!(td->io_ops->flags & FIO_NOIO)) {
Jens Axboed21e2532013-07-25 13:01:11 -06001109 char *c1, *c2, *c3, *c4;
1110 char *c5 = NULL, *c6 = NULL;
Jens Axboef8977ee2006-11-06 14:03:03 +01001111
Jens Axboe22f80452013-04-09 20:29:16 +02001112 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1113 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1114 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1115 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +01001116
Jens Axboed21e2532013-07-25 13:01:11 -06001117 if (!o->bs_is_seq_rand) {
1118 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1119 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1120 }
1121
1122 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1123 td->groupid,
1124 ddir_str(o->td_ddir));
1125
1126 if (o->bs_is_seq_rand)
1127 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1128 c1, c2, c3, c4);
1129 else
1130 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1131 c1, c2, c3, c4, c5, c6);
1132
1133 log_info("ioengine=%s, iodepth=%u\n",
Jens Axboede98bd32013-04-05 11:09:20 +02001134 td->io_ops->name, o->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +01001135
1136 free(c1);
1137 free(c2);
1138 free(c3);
1139 free(c4);
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001140 free(c5);
1141 free(c6);
Jens Axboef8977ee2006-11-06 14:03:03 +01001142 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001143 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +01001144 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001145 }
Jens Axboeebac4652005-12-08 15:25:21 +01001146
1147 /*
1148 * recurse add identical jobs, clear numjobs and stonewall options
1149 * as they don't apply to sub-jobs
1150 */
Jens Axboede98bd32013-04-05 11:09:20 +02001151 numjobs = o->numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +01001152 while (--numjobs) {
Steven Langde890a12011-11-09 14:03:34 +01001153 struct thread_data *td_new = get_new_job(0, td, 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001154
1155 if (!td_new)
1156 goto err;
1157
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001158 td_new->o.numjobs = 1;
1159 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +02001160 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +01001161
1162 if (file_alloced) {
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001163 if (td_new->files) {
1164 struct fio_file *f;
1165 for_each_file(td_new, f, i) {
1166 if (f->file_name)
Andrey Kuzminece6d642014-04-01 12:27:04 -06001167 sfree(f->file_name);
1168 sfree(f);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001169 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001170 free(td_new->files);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001171 td_new->files = NULL;
1172 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001173 td_new->files_index = 0;
1174 td_new->files_size = 0;
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001175 if (td_new->o.filename) {
1176 free(td_new->o.filename);
1177 td_new->o.filename = NULL;
1178 }
Jens Axboee132cba2007-03-14 14:23:54 +01001179 }
1180
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001181 if (add_job(td_new, jobname, numjobs, 1, client_type))
Jens Axboeebac4652005-12-08 15:25:21 +01001182 goto err;
1183 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001184
Jens Axboeebac4652005-12-08 15:25:21 +01001185 return 0;
1186err:
1187 put_job(td);
1188 return -1;
1189}
1190
Jens Axboe79d16312010-03-04 12:43:20 +01001191/*
1192 * Parse as if 'o' was a command line
1193 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001194void add_job_opts(const char **o, int client_type)
Jens Axboe79d16312010-03-04 12:43:20 +01001195{
1196 struct thread_data *td, *td_parent;
1197 int i, in_global = 1;
1198 char jobname[32];
1199
1200 i = 0;
1201 td_parent = td = NULL;
1202 while (o[i]) {
1203 if (!strncmp(o[i], "name", 4)) {
1204 in_global = 0;
1205 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001206 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001207 td = NULL;
1208 sprintf(jobname, "%s", o[i] + 5);
1209 }
1210 if (in_global && !td_parent)
Steven Langde890a12011-11-09 14:03:34 +01001211 td_parent = get_new_job(1, &def_thread, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001212 else if (!in_global && !td) {
1213 if (!td_parent)
1214 td_parent = &def_thread;
Steven Langde890a12011-11-09 14:03:34 +01001215 td = get_new_job(0, td_parent, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001216 }
1217 if (in_global)
Jens Axboe292cc472013-11-26 20:39:35 -07001218 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001219 else
Jens Axboe292cc472013-11-26 20:39:35 -07001220 fio_options_parse(td, (char **) &o[i], 1, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001221 i++;
1222 }
1223
1224 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001225 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001226}
1227
Jens Axboe01f06b62008-02-18 20:53:47 +01001228static int skip_this_section(const char *name)
1229{
Jens Axboead0a2732011-03-11 10:16:17 +01001230 int i;
1231
1232 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +01001233 return 0;
1234 if (!strncmp(name, "global", 6))
1235 return 0;
1236
Jens Axboead0a2732011-03-11 10:16:17 +01001237 for (i = 0; i < nr_job_sections; i++)
1238 if (!strcmp(job_sections[i], name))
1239 return 0;
1240
1241 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001242}
1243
Jens Axboeebac4652005-12-08 15:25:21 +01001244static int is_empty_or_comment(char *line)
1245{
1246 unsigned int i;
1247
1248 for (i = 0; i < strlen(line); i++) {
1249 if (line[i] == ';')
1250 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001251 if (line[i] == '#')
1252 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +02001253 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +01001254 return 0;
1255 }
1256
1257 return 1;
1258}
1259
Jens Axboe313cb202006-12-21 09:50:00 +01001260/*
Jens Axboe07261982006-06-07 10:51:12 +02001261 * This is our [ini] type file parser.
1262 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001263int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
Jens Axboeebac4652005-12-08 15:25:21 +01001264{
Jens Axboee1f36502006-10-27 10:54:08 +02001265 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001266 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001267 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001268 FILE *f;
1269 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001270 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +02001271 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +02001272 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +01001273 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001274 char **opts;
1275 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +01001276
Jens Axboe50d16972011-09-29 17:45:28 -06001277 if (is_buf)
1278 f = NULL;
1279 else {
1280 if (!strcmp(file, "-"))
1281 f = stdin;
1282 else
1283 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001284
Jens Axboe50d16972011-09-29 17:45:28 -06001285 if (!f) {
1286 perror("fopen job file");
1287 return 1;
1288 }
Jens Axboeebac4652005-12-08 15:25:21 +01001289 }
1290
1291 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +02001292
1293 /*
1294 * it's really 256 + small bit, 280 should suffice
1295 */
1296 name = malloc(280);
1297 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +01001298
Jens Axboe3b8b7132008-06-10 19:46:23 +02001299 alloc_opts = 8;
1300 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +02001301 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001302
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001303 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001304 do {
Jens Axboeccf8f122007-09-27 10:48:55 +02001305 /*
1306 * if skip_fgets is set, we already have loaded a line we
1307 * haven't handled.
1308 */
1309 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -06001310 if (is_buf)
1311 p = strsep(&file, "\n");
1312 else
Jens Axboe43f74792011-10-15 14:37:26 +02001313 p = fgets(string, 4096, f);
Jens Axboeccf8f122007-09-27 10:48:55 +02001314 if (!p)
1315 break;
1316 }
gurudas paicdc7f192007-03-29 10:10:56 +02001317
Jens Axboeccf8f122007-09-27 10:48:55 +02001318 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +02001319 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001320 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +02001321
Jens Axboeebac4652005-12-08 15:25:21 +01001322 if (is_empty_or_comment(p))
1323 continue;
Bruce Cran84dd1882011-05-05 08:14:09 -06001324 if (sscanf(p, "[%255[^\n]]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001325 if (inside_skip)
1326 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001327 log_err("fio: option <%s> outside of [] job section\n",
1328 p);
Jens Axboe088b4202007-07-19 14:53:01 +02001329 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001330 }
Jens Axboeebac4652005-12-08 15:25:21 +01001331
Jens Axboe7a4b80a2010-05-22 20:43:11 +02001332 name[strlen(name) - 1] = '\0';
1333
Jens Axboe01f06b62008-02-18 20:53:47 +01001334 if (skip_this_section(name)) {
1335 inside_skip = 1;
1336 continue;
1337 } else
1338 inside_skip = 0;
1339
Jens Axboeebac4652005-12-08 15:25:21 +01001340 global = !strncmp(name, "global", 6);
1341
Jens Axboecca73aa2007-04-04 11:09:19 +02001342 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +02001343 if (first_sect)
1344 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +02001345 if (!global)
1346 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +02001347 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001348 }
1349
Steven Langde890a12011-11-09 14:03:34 +01001350 td = get_new_job(global, &def_thread, 0);
Jens Axboe45410ac2006-06-07 11:10:39 +02001351 if (!td) {
1352 ret = 1;
1353 break;
1354 }
Jens Axboeebac4652005-12-08 15:25:21 +01001355
Jens Axboe972cfd22006-06-09 11:08:56 +02001356 /*
Anatol Pomozovde8f6de2013-09-26 16:31:34 -07001357 * Separate multiple job files by a stonewall
Jens Axboe972cfd22006-06-09 11:08:56 +02001358 */
Jens Axboef9481912006-06-09 11:37:28 +02001359 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001360 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +02001361 stonewall = 0;
1362 }
1363
Jens Axboe3b8b7132008-06-10 19:46:23 +02001364 num_opts = 0;
1365 memset(opts, 0, alloc_opts * sizeof(char *));
1366
Jens Axboe50d16972011-09-29 17:45:28 -06001367 while (1) {
1368 if (is_buf)
1369 p = strsep(&file, "\n");
1370 else
1371 p = fgets(string, 4096, f);
1372 if (!p)
1373 break;
1374
Jens Axboeebac4652005-12-08 15:25:21 +01001375 if (is_empty_or_comment(p))
1376 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001377
Jens Axboeb6754f92006-05-30 14:29:32 +02001378 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001379
Jens Axboeccf8f122007-09-27 10:48:55 +02001380 /*
1381 * new section, break out and make sure we don't
1382 * fgets() a new line at the top.
1383 */
1384 if (p[0] == '[') {
1385 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001386 break;
Jens Axboeccf8f122007-09-27 10:48:55 +02001387 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001388
Jens Axboe4ae3f762006-05-30 13:35:14 +02001389 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001390
Jens Axboe3b8b7132008-06-10 19:46:23 +02001391 if (num_opts == alloc_opts) {
1392 alloc_opts <<= 1;
1393 opts = realloc(opts,
1394 alloc_opts * sizeof(char *));
1395 }
1396
1397 opts[num_opts] = strdup(p);
1398 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +01001399 }
Jens Axboeebac4652005-12-08 15:25:21 +01001400
Jens Axboe292cc472013-11-26 20:39:35 -07001401 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1402 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001403 ret = add_job(td, name, 0, 0, type);
Jens Axboe292cc472013-11-26 20:39:35 -07001404 else {
Jens Axboeb1508cf2006-11-02 09:12:40 +01001405 log_err("fio: job %s dropped\n", name);
1406 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001407 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001408
1409 for (i = 0; i < num_opts; i++)
1410 free(opts[i]);
1411 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001412 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001413
Jens Axboecca73aa2007-04-04 11:09:19 +02001414 if (dump_cmdline)
1415 log_info("\n");
1416
Jens Axboe7e356b22011-10-14 10:55:16 +02001417 i = 0;
1418 while (i < nr_job_sections) {
1419 free(job_sections[i]);
1420 i++;
1421 }
1422
Jens Axboe3b8b7132008-06-10 19:46:23 +02001423 for (i = 0; i < num_opts; i++)
1424 free(opts[i]);
1425
Jens Axboeebac4652005-12-08 15:25:21 +01001426 free(string);
1427 free(name);
Jens Axboe25775942008-06-10 20:26:06 +02001428 free(opts);
Jens Axboe50d16972011-09-29 17:45:28 -06001429 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001430 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001431 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001432}
1433
1434static int fill_def_thread(void)
1435{
1436 memset(&def_thread, 0, sizeof(def_thread));
1437
Jens Axboe375b2692007-05-22 09:13:02 +02001438 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboe8c029372011-10-05 09:54:30 +02001439 def_thread.o.timeout = def_timeout;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001440 def_thread.o.error_dump = 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001441 /*
Jens Axboeee738492007-01-10 11:23:16 +01001442 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001443 */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001444 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001445 return 0;
1446}
1447
Jens Axboe45378b32007-12-19 13:54:38 +01001448static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02001449{
Jens Axboe3d433822012-03-21 22:25:22 +01001450 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01001451 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboe83881282011-10-17 19:58:51 +02001452 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1453 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
Jens Axboe3e260a42013-12-09 12:38:53 -07001454 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
Jens Axboe111e0322013-03-07 11:31:20 +01001455 printf(" --parse-only\t\tParse options only, don't start any IO\n");
Jens Axboe83881282011-10-17 19:58:51 +02001456 printf(" --output\t\tWrite output to file\n");
liang xieb2cecdc2012-08-31 08:22:42 -07001457 printf(" --runtime\t\tRuntime in seconds\n");
Jens Axboe83881282011-10-17 19:58:51 +02001458 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1459 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1460 printf(" --minimal\t\tMinimal (terse) output\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001461 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
Jens Axboe75db6e22011-11-07 22:07:36 +01001462 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001463 printf(" --version\t\tPrint version info and exit\n");
Jens Axboe83881282011-10-17 19:58:51 +02001464 printf(" --help\t\tPrint this page\n");
Jens Axboe23893642012-12-17 14:44:08 +01001465 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
Jens Axboefec0f212014-02-07 14:39:33 -07001466 printf(" --crctest\t\tTest speed of checksum functions\n");
Jens Axboe83881282011-10-17 19:58:51 +02001467 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001468 " them\n");
Steven Langde890a12011-11-09 14:03:34 +01001469 printf(" --enghelp=engine\tPrint ioengine help, or list"
1470 " available ioengines\n");
1471 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1472 " cmd\n");
Jens Axboe83881282011-10-17 19:58:51 +02001473 printf(" --showcmd\t\tTurn a job file into command line options\n");
1474 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1475 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboee382e662013-02-22 20:48:56 +01001476 printf(" --eta-newline=time\tForce a new line for every 'time'");
1477 printf(" period passed\n");
Jens Axboe06464902013-04-24 20:38:54 -06001478 printf(" --status-interval=t\tForce full status dump every");
1479 printf(" 't' period passed\n");
Jens Axboe83881282011-10-17 19:58:51 +02001480 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001481 " writes\n");
Jens Axboe83881282011-10-17 19:58:51 +02001482 printf(" --section=name\tOnly run specified section in job file\n");
1483 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
Jens Axboe2b386d22008-03-26 10:32:57 +01001484 " (def 1024)\n");
Jens Axboe83881282011-10-17 19:58:51 +02001485 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1486 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1487 printf(" --server=args\t\tStart a backend fio server\n");
1488 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1489 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001490 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1491 "\t\t\t(option=system,percpu) or run unit work\n"
1492 "\t\t\tcalibration only (option=calibrate)\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001493 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
Jens Axboebfca2c72014-01-29 16:39:04 -07001494 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1495 printf("\n Jens Axboe <axboe@fb.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001496}
1497
Jens Axboe79e48f72008-02-01 20:37:09 +01001498#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001499struct debug_level debug_levels[] = {
Jens Axboe0b8d11e2012-03-02 19:44:15 +01001500 { .name = "process",
1501 .help = "Process creation/exit logging",
1502 .shift = FD_PROCESS,
1503 },
1504 { .name = "file",
1505 .help = "File related action logging",
1506 .shift = FD_FILE,
1507 },
1508 { .name = "io",
1509 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1510 .shift = FD_IO,
1511 },
1512 { .name = "mem",
1513 .help = "Memory allocation/freeing logging",
1514 .shift = FD_MEM,
1515 },
1516 { .name = "blktrace",
1517 .help = "blktrace action logging",
1518 .shift = FD_BLKTRACE,
1519 },
1520 { .name = "verify",
1521 .help = "IO verification action logging",
1522 .shift = FD_VERIFY,
1523 },
1524 { .name = "random",
1525 .help = "Random generation logging",
1526 .shift = FD_RANDOM,
1527 },
1528 { .name = "parse",
1529 .help = "Parser logging",
1530 .shift = FD_PARSE,
1531 },
1532 { .name = "diskutil",
1533 .help = "Disk utility logging actions",
1534 .shift = FD_DISKUTIL,
1535 },
1536 { .name = "job",
1537 .help = "Logging related to creating/destroying jobs",
1538 .shift = FD_JOB,
1539 },
1540 { .name = "mutex",
1541 .help = "Mutex logging",
1542 .shift = FD_MUTEX
1543 },
1544 { .name = "profile",
1545 .help = "Logging related to profiles",
1546 .shift = FD_PROFILE,
1547 },
1548 { .name = "time",
1549 .help = "Logging related to time keeping functions",
1550 .shift = FD_TIME,
1551 },
1552 { .name = "net",
1553 .help = "Network logging",
1554 .shift = FD_NET,
1555 },
Jens Axboe3e260a42013-12-09 12:38:53 -07001556 { .name = "rate",
1557 .help = "Rate logging",
1558 .shift = FD_RATE,
1559 },
Jens Axboe02444ad2008-10-07 11:33:00 +02001560 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001561};
1562
Jens Axboec09823a2008-02-19 20:16:57 +01001563static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001564{
1565 struct debug_level *dl;
1566 char *p = (char *) string;
1567 char *opt;
1568 int i;
1569
1570 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001571 log_info("fio: dumping debug options:");
1572 for (i = 0; debug_levels[i].name; i++) {
1573 dl = &debug_levels[i];
1574 log_info("%s,", dl->name);
1575 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001576 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001577 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001578 }
1579
1580 while ((opt = strsep(&p, ",")) != NULL) {
1581 int found = 0;
1582
Jens Axboe5e1d3062008-05-23 11:55:53 +02001583 if (!strncmp(opt, "all", 3)) {
1584 log_info("fio: set all debug options\n");
1585 fio_debug = ~0UL;
1586 continue;
1587 }
1588
Jens Axboeee56ad52008-02-01 10:30:20 +01001589 for (i = 0; debug_levels[i].name; i++) {
1590 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001591 found = !strncmp(opt, dl->name, strlen(dl->name));
1592 if (!found)
1593 continue;
1594
1595 if (dl->shift == FD_JOB) {
1596 opt = strchr(opt, ':');
1597 if (!opt) {
1598 log_err("fio: missing job number\n");
1599 break;
1600 }
1601 opt++;
1602 fio_debug_jobno = atoi(opt);
1603 log_info("fio: set debug jobno %d\n",
1604 fio_debug_jobno);
1605 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001606 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001607 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001608 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001609 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001610 }
1611
1612 if (!found)
1613 log_err("fio: debug mask %s not found\n", opt);
1614 }
Jens Axboec09823a2008-02-19 20:16:57 +01001615 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001616}
Jens Axboe79e48f72008-02-01 20:37:09 +01001617#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001618static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001619{
1620 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001621 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001622}
1623#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001624
Jens Axboe4c6107f2011-01-18 05:22:22 -07001625static void fio_options_fill_optstring(void)
1626{
1627 char *ostr = cmd_optstr;
1628 int i, c;
1629
1630 c = i = 0;
1631 while (l_opts[i].name) {
1632 ostr[c++] = l_opts[i].val;
1633 if (l_opts[i].has_arg == required_argument)
1634 ostr[c++] = ':';
1635 else if (l_opts[i].has_arg == optional_argument) {
1636 ostr[c++] = ':';
1637 ostr[c++] = ':';
1638 }
1639 i++;
1640 }
1641 ostr[c] = '\0';
1642}
1643
Jens Axboec2c94582011-10-05 20:31:30 +02001644static int client_flag_set(char c)
1645{
1646 int i;
1647
1648 i = 0;
1649 while (l_opts[i].name) {
1650 int val = l_opts[i].val;
1651
1652 if (c == (val & 0xff))
1653 return (val & FIO_CLIENT_FLAG);
1654
1655 i++;
1656 }
1657
1658 return 0;
1659}
1660
Jens Axboefa2ea802011-10-08 21:07:29 +02001661void parse_cmd_client(void *client, char *opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +02001662{
Jens Axboefa2ea802011-10-08 21:07:29 +02001663 fio_client_add_cmd_option(client, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001664}
1665
Jens Axboefec0f212014-02-07 14:39:33 -07001666extern int fio_crctest(const char *);
1667
Jens Axboe46bcd492012-03-14 11:31:21 +01001668int parse_cmd_line(int argc, char *argv[], int client_type)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001669{
1670 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001671 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001672 char *ostr = cmd_optstr;
Jens Axboe402668f2011-10-10 15:28:58 +02001673 void *pid_file = NULL;
Jens Axboe9bae26e2011-10-07 10:07:22 +02001674 void *cur_client = NULL;
Jens Axboe81179ee2011-10-04 12:42:06 +02001675 int backend = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001676
Jens Axboec2c94582011-10-05 20:31:30 +02001677 /*
1678 * Reset optind handling, since we may call this multiple times
1679 * for the backend.
1680 */
1681 optind = 1;
Jens Axboe7a4b8242011-10-05 17:35:15 +02001682
Jens Axboec2c94582011-10-05 20:31:30 +02001683 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
Jens Axboe085399d2011-10-06 09:01:22 +02001684 did_arg = 1;
1685
Jens Axboec2c94582011-10-05 20:31:30 +02001686 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
Jens Axboefa2ea802011-10-08 21:07:29 +02001687 parse_cmd_client(cur_client, argv[optind - 1]);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001688 c &= ~FIO_CLIENT_FLAG;
1689 }
1690
Jens Axboe214e1ec2007-03-15 10:48:13 +01001691 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001692 case 'a':
1693 smalloc_pool_size = atoi(optarg);
1694 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001695 case 't':
Jens Axboe8c029372011-10-05 09:54:30 +02001696 def_timeout = atoi(optarg);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001697 break;
1698 case 'l':
1699 write_lat_log = 1;
1700 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001701 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001702 write_bw_log = 1;
1703 break;
1704 case 'o':
1705 f_out = fopen(optarg, "w+");
1706 if (!f_out) {
1707 perror("fopen output");
1708 exit(1);
1709 }
1710 f_err = f_out;
1711 break;
1712 case 'm':
Jens Axboef3afa572012-09-17 13:34:16 +02001713 output_format = FIO_OUTPUT_TERSE;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001714 break;
Jens Axboef3afa572012-09-17 13:34:16 +02001715 case 'F':
Stefan Hajnoczid10983d2014-02-04 14:27:11 +01001716 if (!optarg) {
1717 log_err("fio: missing --output-format argument\n");
1718 exit_val = 1;
1719 do_exit++;
1720 break;
1721 }
Jens Axboef3afa572012-09-17 13:34:16 +02001722 if (!strcmp(optarg, "minimal") ||
1723 !strcmp(optarg, "terse") ||
1724 !strcmp(optarg, "csv"))
1725 output_format = FIO_OUTPUT_TERSE;
1726 else if (!strcmp(optarg, "json"))
1727 output_format = FIO_OUTPUT_JSON;
1728 else
1729 output_format = FIO_OUTPUT_NORMAL;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001730 break;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +01001731 case 'f':
1732 append_terse_output = 1;
1733 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001734 case 'h':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001735 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001736 usage(argv[0]);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001737 do_exit++;
1738 }
Jens Axboec2c94582011-10-05 20:31:30 +02001739 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001740 case 'c':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001741 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001742 fio_show_option_help(optarg);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001743 do_exit++;
1744 }
Jens Axboec2c94582011-10-05 20:31:30 +02001745 break;
Steven Langde890a12011-11-09 14:03:34 +01001746 case 'i':
1747 if (!cur_client) {
1748 fio_show_ioengine_help(optarg);
1749 do_exit++;
1750 }
1751 break;
Jens Axboecca73aa2007-04-04 11:09:19 +02001752 case 's':
1753 dump_cmdline = 1;
1754 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001755 case 'r':
1756 read_only = 1;
1757 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001758 case 'v':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001759 if (!cur_client) {
Jens Axboe3d433822012-03-21 22:25:22 +01001760 log_info("%s\n", fio_version_string);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001761 do_exit++;
1762 }
Jens Axboec2c94582011-10-05 20:31:30 +02001763 break;
Jens Axboef57a9c52011-09-09 21:01:37 +02001764 case 'V':
1765 terse_version = atoi(optarg);
Jens Axboe09786f52012-10-04 17:06:49 +02001766 if (!(terse_version == 2 || terse_version == 3 ||
1767 terse_version == 4)) {
Jens Axboef57a9c52011-09-09 21:01:37 +02001768 log_err("fio: bad terse version format\n");
1769 exit_val = 1;
1770 do_exit++;
1771 }
1772 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001773 case 'e':
1774 if (!strcmp("always", optarg))
1775 eta_print = FIO_ETA_ALWAYS;
1776 else if (!strcmp("never", optarg))
1777 eta_print = FIO_ETA_NEVER;
1778 break;
Jens Axboee382e662013-02-22 20:48:56 +01001779 case 'E': {
1780 long long t = 0;
1781
Jens Axboe0de5b262014-02-21 15:26:01 -08001782 if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
Jens Axboee382e662013-02-22 20:48:56 +01001783 log_err("fio: failed parsing eta time %s\n", optarg);
1784 exit_val = 1;
1785 do_exit++;
1786 }
1787 eta_new_line = t;
1788 break;
1789 }
Jens Axboeee56ad52008-02-01 10:30:20 +01001790 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001791 if (set_debug(optarg))
1792 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001793 break;
Jens Axboe111e0322013-03-07 11:31:20 +01001794 case 'P':
1795 parse_only = 1;
1796 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001797 case 'x': {
1798 size_t new_size;
1799
Jens Axboe01f06b62008-02-18 20:53:47 +01001800 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001801 log_err("fio: can't use global as only "
1802 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001803 do_exit++;
1804 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001805 break;
1806 }
Jens Axboead0a2732011-03-11 10:16:17 +01001807 new_size = (nr_job_sections + 1) * sizeof(char *);
1808 job_sections = realloc(job_sections, new_size);
1809 job_sections[nr_job_sections] = strdup(optarg);
1810 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01001811 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001812 }
Jens Axboe9ac8a792009-11-13 21:23:07 +01001813 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001814 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001815 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001816 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001817 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001818 char *val = optarg;
1819
1820 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe46bcd492012-03-14 11:31:21 +01001821 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe66251552011-05-12 10:14:57 +02001822 if (ret)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001823 return 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001824 td = NULL;
1825 }
1826 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001827 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001828 int global = 0;
1829
Jens Axboe01f06b62008-02-18 20:53:47 +01001830 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001831 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001832
Jens Axboe01f06b62008-02-18 20:53:47 +01001833 if (is_section && skip_this_section(val))
1834 continue;
1835
Steven Langde890a12011-11-09 14:03:34 +01001836 td = get_new_job(global, &def_thread, 1);
1837 if (!td || ioengine_load(td))
Jens Axboe214e1ec2007-03-15 10:48:13 +01001838 return 0;
Steven Langde890a12011-11-09 14:03:34 +01001839 fio_options_set_ioengine_opts(l_opts, td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001840 }
1841
Jens Axboe9d918182013-05-23 20:00:35 +02001842 if ((!val || !strlen(val)) &&
1843 l_opts[lidx].has_arg == required_argument) {
1844 log_err("fio: option %s requires an argument\n", opt);
1845 ret = 1;
1846 } else
1847 ret = fio_cmd_option_parse(td, opt, val);
1848
Jens Axboebfb3ea22013-05-23 19:54:43 +02001849 if (ret) {
1850 if (td) {
1851 put_job(td);
1852 td = NULL;
1853 }
Jens Axboea88c8c12013-04-24 22:25:22 -06001854 do_exit++;
Jens Axboebfb3ea22013-05-23 19:54:43 +02001855 }
Steven Langde890a12011-11-09 14:03:34 +01001856
1857 if (!ret && !strcmp(opt, "ioengine")) {
1858 free_ioengine(td);
1859 if (ioengine_load(td))
1860 return 0;
1861 fio_options_set_ioengine_opts(l_opts, td);
1862 }
1863 break;
1864 }
1865 case FIO_GETOPT_IOENGINE: {
1866 const char *opt = l_opts[lidx].name;
1867 char *val = optarg;
Steven Langde890a12011-11-09 14:03:34 +01001868 ret = fio_cmd_ioengine_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001869 break;
1870 }
Jens Axboea9523c62011-01-17 16:49:54 -07001871 case 'w':
1872 warnings_fatal = 1;
1873 break;
Jens Axboefca70352011-07-06 20:12:54 +02001874 case 'j':
1875 max_jobs = atoi(optarg);
1876 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1877 log_err("fio: invalid max jobs: %d\n", max_jobs);
1878 do_exit++;
1879 exit_val = 1;
1880 }
1881 break;
Jens Axboe50d16972011-09-29 17:45:28 -06001882 case 'S':
Jens Axboea37f69b2011-10-01 12:24:21 -06001883 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06001884 log_err("fio: can't be both client and server\n");
1885 do_exit++;
1886 exit_val = 1;
1887 break;
1888 }
Jens Axboe87aa8f12011-10-06 21:24:13 +02001889 if (optarg)
Jens Axboebebe6392011-10-07 10:00:51 +02001890 fio_server_set_arg(optarg);
Jens Axboe50d16972011-09-29 17:45:28 -06001891 is_backend = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001892 backend = 1;
Jens Axboe50d16972011-09-29 17:45:28 -06001893 break;
Jens Axboee46d8092011-10-03 09:11:02 +02001894 case 'D':
Jens Axboe402668f2011-10-10 15:28:58 +02001895 pid_file = strdup(optarg);
Jens Axboee46d8092011-10-03 09:11:02 +02001896 break;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001897 case 'I':
1898 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1899 /* exit on error and calibration only */
1900 do_exit++;
1901 if (ret == -1)
1902 exit_val = 1;
1903 }
1904 break;
Jens Axboe132159a2011-09-30 15:01:32 -06001905 case 'C':
1906 if (is_backend) {
1907 log_err("fio: can't be both client and server\n");
1908 do_exit++;
1909 exit_val = 1;
1910 break;
1911 }
Jens Axboea5276612012-03-04 15:15:08 +01001912 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
Jens Axboebebe6392011-10-07 10:00:51 +02001913 log_err("fio: failed adding client %s\n", optarg);
1914 do_exit++;
1915 exit_val = 1;
1916 break;
1917 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02001918 /*
1919 * If the next argument exists and isn't an option,
1920 * assume it's a job file for this client only.
1921 */
1922 while (optind < argc) {
1923 if (!strncmp(argv[optind], "--", 2) ||
1924 !strncmp(argv[optind], "-", 1))
1925 break;
1926
1927 fio_client_add_ini_file(cur_client, argv[optind]);
1928 optind++;
1929 }
Jens Axboe132159a2011-09-30 15:01:32 -06001930 break;
Jens Axboe7d11f872012-12-17 12:03:29 +01001931 case 'T':
1932 do_exit++;
1933 exit_val = fio_monotonic_clocktest();
1934 break;
Jens Axboefec0f212014-02-07 14:39:33 -07001935 case 'G':
1936 do_exit++;
1937 exit_val = fio_crctest(optarg);
1938 break;
Jens Axboe06464902013-04-24 20:38:54 -06001939 case 'L': {
1940 long long val;
1941
Jens Axboe7cd0ec52014-03-28 08:09:10 -06001942 if (check_str_time(optarg, &val, 0)) {
Jens Axboe06464902013-04-24 20:38:54 -06001943 log_err("fio: failed parsing time %s\n", optarg);
1944 do_exit++;
1945 exit_val = 1;
1946 break;
1947 }
1948 status_interval = val * 1000;
1949 break;
1950 }
Jens Axboe798827c2013-01-23 18:11:48 -07001951 case '?':
1952 log_err("%s: unrecognized option '%s'\n", argv[0],
1953 argv[optind - 1]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001954 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001955 do_exit++;
1956 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001957 break;
1958 }
Jens Axboec7d5c942011-10-03 11:48:17 +02001959 if (do_exit)
1960 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001961 }
1962
Jens Axboe0b14f0a2013-05-23 21:27:54 +02001963 if (do_exit && !(is_backend || nr_clients))
1964 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001965
Jens Axboea37f69b2011-10-01 12:24:21 -06001966 if (nr_clients && fio_clients_connect()) {
Jens Axboe132159a2011-09-30 15:01:32 -06001967 do_exit++;
1968 exit_val = 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02001969 return -1;
Jens Axboe132159a2011-09-30 15:01:32 -06001970 }
1971
Jens Axboe81179ee2011-10-04 12:42:06 +02001972 if (is_backend && backend)
Jens Axboe402668f2011-10-10 15:28:58 +02001973 return fio_start_server(pid_file);
Jens Axboe50d16972011-09-29 17:45:28 -06001974
Jens Axboe214e1ec2007-03-15 10:48:13 +01001975 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001976 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001977 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001978 }
1979
Jens Axboe7874f8b2011-10-06 09:18:20 +02001980 while (!ret && optind < argc) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001981 ini_idx++;
1982 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1983 ini_file[ini_idx - 1] = strdup(argv[optind]);
1984 optind++;
1985 }
1986
1987 return ini_idx;
1988}
1989
Jens Axboe0420ba62012-02-29 11:16:52 +01001990int fio_init_options(void)
Jens Axboeebac4652005-12-08 15:25:21 +01001991{
Jens Axboeb4692822006-10-27 13:43:22 +02001992 f_out = stdout;
1993 f_err = stderr;
1994
Jens Axboe4c6107f2011-01-18 05:22:22 -07001995 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001996 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001997
Jens Axboe9d9eb2e2011-10-03 12:01:42 +02001998 atexit(free_shm);
1999
Jens Axboeebac4652005-12-08 15:25:21 +01002000 if (fill_def_thread())
2001 return 1;
2002
Jens Axboe0420ba62012-02-29 11:16:52 +01002003 return 0;
2004}
2005
Jens Axboe51167792012-03-08 21:34:18 +01002006extern int fio_check_options(struct thread_options *);
2007
Jens Axboe0420ba62012-02-29 11:16:52 +01002008int parse_options(int argc, char *argv[])
2009{
Jens Axboe46bcd492012-03-14 11:31:21 +01002010 const int type = FIO_CLIENT_TYPE_CLI;
Jens Axboe0420ba62012-02-29 11:16:52 +01002011 int job_files, i;
2012
2013 if (fio_init_options())
2014 return 1;
Jens Axboe51167792012-03-08 21:34:18 +01002015 if (fio_test_cconv(&def_thread.o))
2016 log_err("fio: failed internal cconv test\n");
Jens Axboe0420ba62012-02-29 11:16:52 +01002017
Jens Axboe46bcd492012-03-14 11:31:21 +01002018 job_files = parse_cmd_line(argc, argv, type);
Jens Axboeebac4652005-12-08 15:25:21 +01002019
Jens Axboecdf54d82011-10-04 08:31:40 +02002020 if (job_files > 0) {
2021 for (i = 0; i < job_files; i++) {
2022 if (fill_def_thread())
Jens Axboe132159a2011-09-30 15:01:32 -06002023 return 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02002024 if (nr_clients) {
2025 if (fio_clients_send_ini(ini_file[i]))
2026 return 1;
2027 free(ini_file[i]);
2028 } else if (!is_backend) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002029 if (parse_jobs_ini(ini_file[i], 0, i, type))
Jens Axboecdf54d82011-10-04 08:31:40 +02002030 return 1;
2031 free(ini_file[i]);
2032 }
Jens Axboe132159a2011-09-30 15:01:32 -06002033 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02002034 } else if (nr_clients) {
2035 if (fill_def_thread())
2036 return 1;
2037 if (fio_clients_send_ini(NULL))
2038 return 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02002039 }
Jens Axboeebac4652005-12-08 15:25:21 +01002040
Jens Axboe88c6ed82006-06-09 11:28:10 +02002041 free(ini_file);
Jens Axboe7e356b22011-10-14 10:55:16 +02002042 fio_options_free(&def_thread);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08002043 filesetup_mem_free();
Jens Axboeb4692822006-10-27 13:43:22 +02002044
2045 if (!thread_number) {
Jens Axboef9633d72013-09-06 09:59:56 -06002046 if (parse_dryrun())
Jens Axboecca73aa2007-04-04 11:09:19 +02002047 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01002048 if (exec_profile)
2049 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06002050 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06002051 return 0;
Jens Axboe085399d2011-10-06 09:01:22 +02002052 if (did_arg)
2053 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02002054
Jens Axboed65e11c2011-10-05 09:56:53 +02002055 log_err("No jobs(s) defined\n\n");
Jens Axboe085399d2011-10-06 09:01:22 +02002056
2057 if (!did_arg) {
2058 usage(argv[0]);
2059 return 1;
2060 }
2061
2062 return 0;
Jens Axboeb4692822006-10-27 13:43:22 +02002063 }
2064
Jens Axboebe4ecfd2008-12-08 14:10:52 +01002065 if (def_thread.o.gtod_offload) {
2066 fio_gtod_init();
2067 fio_gtod_offload = 1;
2068 fio_gtod_cpu = def_thread.o.gtod_cpu;
2069 }
2070
Jens Axboef3afa572012-09-17 13:34:16 +02002071 if (output_format == FIO_OUTPUT_NORMAL)
Jens Axboe3d433822012-03-21 22:25:22 +01002072 log_info("%s\n", fio_version_string);
Jens Axboef6dea4d2011-10-13 13:37:07 +02002073
Jens Axboeebac4652005-12-08 15:25:21 +01002074 return 0;
2075}
Jens Axboe588b7f02012-03-19 20:37:41 +01002076
2077void options_default_fill(struct thread_options *o)
2078{
2079 memcpy(o, &def_thread.o, sizeof(*o));
2080}