blob: c7fdcddccd45b110288d13040b3da357d999e062 [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 Axboe243bfe12014-04-02 15:46:58 -060027#include "filelock.h"
Jens Axboeebac4652005-12-08 15:25:21 +010028
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040029#include "lib/getopt.h"
Jens Axboee25b6d52013-04-05 14:44:51 +020030#include "lib/strcasestr.h"
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040031
Jens Axboe10aa1362014-04-01 21:10:36 -060032#include "crc/test.h"
33
Jens Axboe3d433822012-03-21 22:25:22 +010034const char fio_version_string[] = FIO_VERSION;
Jens Axboe214e1ec2007-03-15 10:48:13 +010035
Jens Axboeee738492007-01-10 11:23:16 +010036#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010037
Jens Axboe214e1ec2007-03-15 10:48:13 +010038static char **ini_file;
Jens Axboefca70352011-07-06 20:12:54 +020039static int max_jobs = FIO_MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020040static int dump_cmdline;
Jens Axboe25bd16c2014-04-06 10:09:30 -060041static long long def_timeout;
Jens Axboe111e0322013-03-07 11:31:20 +010042static int parse_only;
Jens Axboee1f36502006-10-27 10:54:08 +020043
Jens Axboebe4ecfd2008-12-08 14:10:52 +010044static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010045struct thread_data *threads = NULL;
Jens Axboe10aa1362014-04-01 21:10:36 -060046static char **job_sections;
47static int nr_job_sections;
Jens Axboee1f36502006-10-27 10:54:08 +020048
Jens Axboe214e1ec2007-03-15 10:48:13 +010049int exitall_on_terminate = 0;
Jens Axboef3afa572012-09-17 13:34:16 +020050int output_format = FIO_OUTPUT_NORMAL;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +010051int append_terse_output = 0;
Jens Axboe5be4c942013-02-11 16:33:25 +010052int eta_print = FIO_ETA_AUTO;
Jens Axboee382e662013-02-22 20:48:56 +010053int eta_new_line = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +010054FILE *f_out = NULL;
55FILE *f_err = NULL;
Jens Axboe07b32322010-03-05 09:48:44 +010056char *exec_profile = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -070057int warnings_fatal = 0;
Jens Axboe4d658652011-10-17 15:05:47 +020058int terse_version = 3;
Jens Axboe50d16972011-09-29 17:45:28 -060059int is_backend = 0;
Jens Axboea37f69b2011-10-01 12:24:21 -060060int nr_clients = 0;
Jens Axboee46d8092011-10-03 09:11:02 +020061int log_syslog = 0;
Jens Axboeee738492007-01-10 11:23:16 +010062
Jens Axboe214e1ec2007-03-15 10:48:13 +010063int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020064int read_only = 0;
Jens Axboe06464902013-04-24 20:38:54 -060065int status_interval = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020066
Jens Axboede54cfd2014-11-10 20:34:00 -070067char *trigger_file = NULL;
68char *trigger_cmd = NULL;
69long long trigger_timeout = 0;
70
Jens Axboe214e1ec2007-03-15 10:48:13 +010071static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020072
Jens Axboeee56ad52008-02-01 10:30:20 +010073unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020074unsigned int fio_debug_jobno = -1;
75unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010076
Jens Axboe4c6107f2011-01-18 05:22:22 -070077static char cmd_optstr[256];
Jens Axboe085399d2011-10-06 09:01:22 +020078static int did_arg;
Jens Axboe4c6107f2011-01-18 05:22:22 -070079
Jens Axboe7a4b8242011-10-05 17:35:15 +020080#define FIO_CLIENT_FLAG (1 << 16)
81
Jens Axboeb4692822006-10-27 13:43:22 +020082/*
83 * Command line options. These will contain the above, plus a few
84 * extra that only pertain to fio itself and not jobs.
85 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010086static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020087 {
Jens Axboe08d2a192011-05-11 13:28:30 +020088 .name = (char *) "output",
Jens Axboeb4692822006-10-27 13:43:22 +020089 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020090 .val = 'o' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020091 },
92 {
Jens Axboe08d2a192011-05-11 13:28:30 +020093 .name = (char *) "timeout",
Jens Axboeb4692822006-10-27 13:43:22 +020094 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020095 .val = 't' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020096 },
97 {
Jens Axboe08d2a192011-05-11 13:28:30 +020098 .name = (char *) "latency-log",
Jens Axboeb4692822006-10-27 13:43:22 +020099 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200100 .val = 'l' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200101 },
102 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200103 .name = (char *) "bandwidth-log",
Jens Axboeb4692822006-10-27 13:43:22 +0200104 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200105 .val = 'b' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200106 },
107 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200108 .name = (char *) "minimal",
Stefan Hajnoczic4ec0a12014-02-04 14:27:10 +0100109 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200110 .val = 'm' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200111 },
112 {
Jens Axboef3afa572012-09-17 13:34:16 +0200113 .name = (char *) "output-format",
Shaohua Licc372b12012-09-17 09:12:18 +0200114 .has_arg = optional_argument,
Jens Axboef3afa572012-09-17 13:34:16 +0200115 .val = 'F' | FIO_CLIENT_FLAG,
Shaohua Licc372b12012-09-17 09:12:18 +0200116 },
117 {
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +0100118 .name = (char *) "append-terse",
119 .has_arg = optional_argument,
120 .val = 'f',
121 },
122 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200123 .name = (char *) "version",
Jens Axboeb4692822006-10-27 13:43:22 +0200124 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200125 .val = 'v' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200126 },
127 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200128 .name = (char *) "help",
Jens Axboefd28ca42007-01-09 21:20:13 +0100129 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200130 .val = 'h' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100131 },
132 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200133 .name = (char *) "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100134 .has_arg = optional_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200135 .val = 'c' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100136 },
137 {
Jens Axboe06464902013-04-24 20:38:54 -0600138 .name = (char *) "enghelp",
Steven Langde890a12011-11-09 14:03:34 +0100139 .has_arg = optional_argument,
Jens Axboe06464902013-04-24 20:38:54 -0600140 .val = 'i' | FIO_CLIENT_FLAG,
Steven Langde890a12011-11-09 14:03:34 +0100141 },
142 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200143 .name = (char *) "showcmd",
Jens Axboecca73aa2007-04-04 11:09:19 +0200144 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200145 .val = 's' | FIO_CLIENT_FLAG,
Jens Axboe724e4432007-09-11 20:02:05 +0200146 },
147 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200148 .name = (char *) "readonly",
Jens Axboe724e4432007-09-11 20:02:05 +0200149 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200150 .val = 'r' | FIO_CLIENT_FLAG,
Jens Axboecca73aa2007-04-04 11:09:19 +0200151 },
152 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200153 .name = (char *) "eta",
Aaron Carrolle592a062007-09-14 09:49:41 +0200154 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200155 .val = 'e' | FIO_CLIENT_FLAG,
Aaron Carrolle592a062007-09-14 09:49:41 +0200156 },
157 {
Jens Axboee382e662013-02-22 20:48:56 +0100158 .name = (char *) "eta-newline",
159 .has_arg = required_argument,
160 .val = 'E' | FIO_CLIENT_FLAG,
161 },
162 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200163 .name = (char *) "debug",
Jens Axboeee56ad52008-02-01 10:30:20 +0100164 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200165 .val = 'd' | FIO_CLIENT_FLAG,
Jens Axboeee56ad52008-02-01 10:30:20 +0100166 },
167 {
Jens Axboe111e0322013-03-07 11:31:20 +0100168 .name = (char *) "parse-only",
169 .has_arg = no_argument,
170 .val = 'P' | FIO_CLIENT_FLAG,
171 },
172 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200173 .name = (char *) "section",
Jens Axboe01f06b62008-02-18 20:53:47 +0100174 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200175 .val = 'x' | FIO_CLIENT_FLAG,
Jens Axboe01f06b62008-02-18 20:53:47 +0100176 },
Jens Axboebac4af12014-07-03 13:42:28 -0600177#ifdef CONFIG_ZLIB
178 {
179 .name = (char *) "inflate-log",
180 .has_arg = required_argument,
181 .val = 'X' | FIO_CLIENT_FLAG,
182 },
183#endif
Jens Axboe01f06b62008-02-18 20:53:47 +0100184 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200185 .name = (char *) "alloc-size",
Jens Axboe2b386d22008-03-26 10:32:57 +0100186 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200187 .val = 'a' | FIO_CLIENT_FLAG,
Jens Axboe2b386d22008-03-26 10:32:57 +0100188 },
189 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200190 .name = (char *) "profile",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100191 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200192 .val = 'p' | FIO_CLIENT_FLAG,
Jens Axboe9ac8a792009-11-13 21:23:07 +0100193 },
194 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200195 .name = (char *) "warnings-fatal",
Jens Axboea9523c62011-01-17 16:49:54 -0700196 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200197 .val = 'w' | FIO_CLIENT_FLAG,
Jens Axboea9523c62011-01-17 16:49:54 -0700198 },
199 {
Jens Axboefca70352011-07-06 20:12:54 +0200200 .name = (char *) "max-jobs",
201 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200202 .val = 'j' | FIO_CLIENT_FLAG,
Jens Axboefca70352011-07-06 20:12:54 +0200203 },
204 {
Jens Axboef57a9c52011-09-09 21:01:37 +0200205 .name = (char *) "terse-version",
206 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200207 .val = 'V' | FIO_CLIENT_FLAG,
Jens Axboef57a9c52011-09-09 21:01:37 +0200208 },
209 {
Jens Axboe50d16972011-09-29 17:45:28 -0600210 .name = (char *) "server",
Jens Axboe87aa8f12011-10-06 21:24:13 +0200211 .has_arg = optional_argument,
Jens Axboe50d16972011-09-29 17:45:28 -0600212 .val = 'S',
213 },
Jens Axboee46d8092011-10-03 09:11:02 +0200214 { .name = (char *) "daemonize",
Jens Axboe402668f2011-10-10 15:28:58 +0200215 .has_arg = required_argument,
Jens Axboee46d8092011-10-03 09:11:02 +0200216 .val = 'D',
217 },
Jens Axboe50d16972011-09-29 17:45:28 -0600218 {
Jens Axboe132159a2011-09-30 15:01:32 -0600219 .name = (char *) "client",
220 .has_arg = required_argument,
221 .val = 'C',
222 },
223 {
Jens Axboe35899182014-10-07 20:56:28 -0600224 .name = (char *) "remote-config",
225 .has_arg = required_argument,
226 .val = 'R',
227 },
228 {
Jens Axboe7d11f872012-12-17 12:03:29 +0100229 .name = (char *) "cpuclock-test",
230 .has_arg = no_argument,
231 .val = 'T',
232 },
233 {
Jens Axboefec0f212014-02-07 14:39:33 -0700234 .name = (char *) "crctest",
235 .has_arg = optional_argument,
236 .val = 'G',
237 },
238 {
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100239 .name = (char *) "idle-prof",
240 .has_arg = required_argument,
241 .val = 'I',
242 },
243 {
Jens Axboe06464902013-04-24 20:38:54 -0600244 .name = (char *) "status-interval",
245 .has_arg = required_argument,
246 .val = 'L',
247 },
248 {
Jens Axboede54cfd2014-11-10 20:34:00 -0700249 .name = (char *) "trigger",
250 .has_arg = required_argument,
251 .val = 'W',
252 },
253 {
254 .name = (char *) "trigger-timeout",
255 .has_arg = required_argument,
256 .val = 'B',
257 },
258 {
Jens Axboeb4692822006-10-27 13:43:22 +0200259 .name = NULL,
260 },
261};
262
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200263void free_threads_shm(void)
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200264{
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200265 if (threads) {
266 void *tp = threads;
Jens Axboeef7035a2014-06-18 15:30:09 -0700267#ifndef CONFIG_NO_SHM
268 struct shmid_ds sbuf;
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200269
270 threads = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200271 shmdt(tp);
272 shmctl(shm_id, IPC_RMID, &sbuf);
273 shm_id = -1;
Jens Axboeef7035a2014-06-18 15:30:09 -0700274#else
275 threads = NULL;
276 free(tp);
277#endif
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200278 }
279}
280
Jens Axboe10aa1362014-04-01 21:10:36 -0600281static void free_shm(void)
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200282{
283 if (threads) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200284 file_hash_exit();
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100285 flow_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200286 fio_debug_jobp = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200287 free_threads_shm();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200288 }
289
Jens Axboeeb0c74a2014-02-11 10:33:06 -0700290 options_free(fio_options, &def_thread);
Jens Axboe243bfe12014-04-02 15:46:58 -0600291 fio_filelock_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200292 scleanup();
293}
294
295/*
296 * The thread area is shared between the main process and the job
297 * threads/processes. So setup a shared memory segment that will hold
298 * all the job info. We use the end of the region for keeping track of
299 * open files across jobs, for file sharing.
300 */
301static int setup_thread_area(void)
302{
303 void *hash;
304
305 if (threads)
306 return 0;
307
308 /*
309 * 1024 is too much on some machines, scale max_jobs if
310 * we get a failure that looks like too large a shm segment
311 */
312 do {
313 size_t size = max_jobs * sizeof(struct thread_data);
314
315 size += file_hash_size;
316 size += sizeof(unsigned int);
317
Jens Axboeef7035a2014-06-18 15:30:09 -0700318#ifndef CONFIG_NO_SHM
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200319 shm_id = shmget(0, size, IPC_CREAT | 0600);
320 if (shm_id != -1)
321 break;
Jens Axboef0c77f02012-09-21 15:05:45 +0200322 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200323 perror("shmget");
324 break;
325 }
Jens Axboeef7035a2014-06-18 15:30:09 -0700326#else
327 threads = malloc(size);
328 if (threads)
329 break;
330#endif
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200331
332 max_jobs >>= 1;
333 } while (max_jobs);
334
Jens Axboeef7035a2014-06-18 15:30:09 -0700335#ifndef CONFIG_NO_SHM
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200336 if (shm_id == -1)
337 return 1;
338
339 threads = shmat(shm_id, NULL, 0);
340 if (threads == (void *) -1) {
341 perror("shmat");
342 return 1;
343 }
Jens Axboeef7035a2014-06-18 15:30:09 -0700344#endif
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200345
346 memset(threads, 0, max_jobs * sizeof(struct thread_data));
347 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
348 fio_debug_jobp = (void *) hash + file_hash_size;
349 *fio_debug_jobp = -1;
350 file_hash_init(hash);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100351
352 flow_init();
353
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200354 return 0;
355}
356
Jens Axboe25bd16c2014-04-06 10:09:30 -0600357static void set_cmd_options(struct thread_data *td)
358{
359 struct thread_options *o = &td->o;
360
361 if (!o->timeout)
362 o->timeout = def_timeout;
363}
364
Jens Axboe906c8d72006-06-13 09:37:56 +0200365/*
366 * Return a free job structure.
367 */
Steven Langde890a12011-11-09 14:03:34 +0100368static struct thread_data *get_new_job(int global, struct thread_data *parent,
Jens Axboeb01e1f32014-06-24 09:19:30 -0600369 int preserve_eo, const char *jobname)
Jens Axboeebac4652005-12-08 15:25:21 +0100370{
371 struct thread_data *td;
372
Jens Axboe25bd16c2014-04-06 10:09:30 -0600373 if (global) {
374 set_cmd_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +0100375 return &def_thread;
Jens Axboe25bd16c2014-04-06 10:09:30 -0600376 }
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200377 if (setup_thread_area()) {
378 log_err("error: failed to setup shm segment\n");
379 return NULL;
380 }
Bruce Crane61f1ec2011-01-14 18:30:26 +0100381 if (thread_number >= max_jobs) {
382 log_err("error: maximum number of jobs (%d) reached.\n",
383 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100384 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100385 }
Jens Axboeebac4652005-12-08 15:25:21 +0100386
387 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200388 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100389
Steven Langde890a12011-11-09 14:03:34 +0100390 td->io_ops = NULL;
391 if (!preserve_eo)
392 td->eo = NULL;
393
Jens Axboee0b0d892009-12-08 10:10:14 +0100394 td->o.uid = td->o.gid = -1U;
395
Jens Axboecade3ef2007-03-23 15:29:45 +0100396 dup_files(td, parent);
Steven Langde890a12011-11-09 14:03:34 +0100397 fio_options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100398
Jens Axboe15dc1932010-03-05 10:59:06 +0100399 profile_add_hooks(td);
400
Jens Axboeebac4652005-12-08 15:25:21 +0100401 td->thread_number = thread_number;
Jiri Horky5a65b4e2014-07-25 09:55:03 +0200402 td->subjob_number = 0;
Jens Axboe108fea72012-11-14 13:09:45 -0700403
Jens Axboeb01e1f32014-06-24 09:19:30 -0600404 if (jobname)
405 td->o.name = strdup(jobname);
406
Jens Axboebcdcb5a2014-04-11 11:42:13 -0600407 if (!parent->o.group_reporting)
Jens Axboe108fea72012-11-14 13:09:45 -0700408 stat_number++;
409
Jens Axboe25bd16c2014-04-06 10:09:30 -0600410 set_cmd_options(td);
Jens Axboeebac4652005-12-08 15:25:21 +0100411 return td;
412}
413
414static void put_job(struct thread_data *td)
415{
Jens Axboe549577a2006-10-30 12:23:41 +0100416 if (td == &def_thread)
417 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600418
Jens Axboe58c55ba2010-03-09 12:20:08 +0100419 profile_td_exit(td);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100420 flow_exit_job(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100421
Jens Axboe16edf252007-02-10 14:59:22 +0100422 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100423 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100424
Jens Axboe7e356b22011-10-14 10:55:16 +0200425 fio_options_free(td);
Steven Langde890a12011-11-09 14:03:34 +0100426 if (td->io_ops)
427 free_ioengine(td);
Jens Axboe7e356b22011-10-14 10:55:16 +0200428
Jens Axboeb01e1f32014-06-24 09:19:30 -0600429 if (td->o.name)
430 free(td->o.name);
431
Jens Axboeebac4652005-12-08 15:25:21 +0100432 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
433 thread_number--;
434}
435
Jens Axboe581e7142009-06-09 12:47:16 +0200436static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100437{
Jens Axboe581e7142009-06-09 12:47:16 +0200438 unsigned int bs = td->o.min_bs[ddir];
Jens Axboe127f6862007-03-15 12:09:57 +0100439
Jens Axboeff58fce2010-08-25 12:02:08 +0200440 assert(ddir_rw(ddir));
441
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100442 if (td->o.rate[ddir])
Steven Lang1b8dbf22011-11-09 13:48:01 +0100443 td->rate_bps[ddir] = td->o.rate[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100444 else
Steven Lang1b8dbf22011-11-09 13:48:01 +0100445 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100446
Steven Lang1b8dbf22011-11-09 13:48:01 +0100447 if (!td->rate_bps[ddir]) {
Jens Axboe127f6862007-03-15 12:09:57 +0100448 log_err("rate lower than supported\n");
449 return -1;
450 }
451
Jens Axboe581e7142009-06-09 12:47:16 +0200452 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100453 return 0;
454}
455
Jens Axboe581e7142009-06-09 12:47:16 +0200456static int setup_rate(struct thread_data *td)
457{
458 int ret = 0;
459
460 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
461 ret = __setup_rate(td, DDIR_READ);
462 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
463 ret |= __setup_rate(td, DDIR_WRITE);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200464 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
465 ret |= __setup_rate(td, DDIR_TRIM);
Jens Axboe581e7142009-06-09 12:47:16 +0200466
467 return ret;
468}
469
Jens Axboe83472392009-02-19 21:32:12 +0100470static int fixed_block_size(struct thread_options *o)
471{
472 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
473 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200474 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
475 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
476 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
Jens Axboe83472392009-02-19 21:32:12 +0100477}
478
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800479
480static unsigned long long get_rand_start_delay(struct thread_data *td)
481{
482 unsigned long long delayrange;
483 unsigned long r;
484
485 delayrange = td->o.start_delay_high - td->o.start_delay;
486
Jens Axboef6787012014-11-05 18:39:23 -0700487 r = __rand(&td->delay_state);
Jens Axboe559073f2014-11-05 18:34:02 -0700488 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800489
490 delayrange += td->o.start_delay;
491 return delayrange;
492}
493
Jens Axboedad915e2006-10-27 11:10:18 +0200494/*
495 * Lazy way of fixing up options that depend on each other. We could also
496 * define option callback handlers, but this is easier.
497 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100498static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200499{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100500 struct thread_options *o = &td->o;
Jens Axboeeefd98b2013-07-25 12:30:06 -0600501 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200502
Jens Axboef356d012009-01-05 09:56:29 +0100503#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100504 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100505 log_info("fio: this platform does not support process shared"
506 " mutexes, forcing use of threads. Use the 'thread'"
507 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100508 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700509 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100510 }
511#endif
512
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100513 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200514 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100515 free(o->write_iolog_file);
516 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700517 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200518 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100519
Jens Axboe16b462a2006-10-30 12:35:18 +0100520 /*
Steven Noonaned335852012-01-31 13:58:00 +0100521 * only really works with 1 file
Jens Axboe16b462a2006-10-30 12:35:18 +0100522 */
Jens Axboe627aa1a2013-02-07 13:06:18 +0100523 if (o->zone_size && o->open_files > 1)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100524 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100525
526 /*
Steven Noonaned335852012-01-31 13:58:00 +0100527 * If zone_range isn't specified, backward compatibility dictates it
528 * should be made equal to zone_size.
529 */
530 if (o->zone_size && !o->zone_range)
531 o->zone_range = o->zone_size;
532
533 /*
Jens Axboe16b462a2006-10-30 12:35:18 +0100534 * Reads can do overwrites, we always need to pre-create the file
535 */
536 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100537 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100538
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100539 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100540 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100541 if (!o->max_bs[DDIR_READ])
542 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
543 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100544 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100545 if (!o->max_bs[DDIR_WRITE])
546 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200547 if (!o->min_bs[DDIR_TRIM])
548 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
549 if (!o->max_bs[DDIR_TRIM])
550 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
551
Jens Axboea00735e2006-11-03 08:58:08 +0100552
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100553 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200554 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
Jens Axboea00735e2006-11-03 08:58:08 +0100555
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100556 /*
557 * For random IO, allow blockalign offset other than min_bs.
558 */
559 if (!o->ba[DDIR_READ] || !td_random(td))
560 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
561 if (!o->ba[DDIR_WRITE] || !td_random(td))
562 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200563 if (!o->ba[DDIR_TRIM] || !td_random(td))
564 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100565
566 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200567 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
568 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100569 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100570 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100571 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700572 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100573 }
574
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100575 if (!o->file_size_high)
576 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100577
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800578 if (o->start_delay_high)
579 o->start_delay = get_rand_start_delay(td);
580
Jens Axboe83472392009-02-19 21:32:12 +0100581 if (o->norandommap && o->verify != VERIFY_NONE
582 && !fixed_block_size(o)) {
583 log_err("fio: norandommap given for variable block sizes, "
584 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100585 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700586 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100587 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100588 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100589 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100590
591 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100592 * thinktime_spin must be less than thinktime
593 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100594 if (o->thinktime_spin > o->thinktime)
595 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100596
597 /*
598 * The low water mark cannot be bigger than the iodepth
599 */
Jens Axboe67bf9822013-01-10 11:23:19 +0100600 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
601 o->iodepth_low = o->iodepth;
Jens Axboecb5ab512007-02-26 12:57:09 +0100602
603 /*
604 * If batch number isn't set, default to the same as iodepth
605 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100606 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
607 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100608
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100609 if (o->nr_files > td->files_index)
610 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100611
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100612 if (o->open_files > o->nr_files || !o->open_files)
613 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100614
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200615 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
616 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
617 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
618 (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 +0100619 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700620 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100621 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200622 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
623 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
624 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
625 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
626 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
627 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100628 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700629 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100630 }
631
Jens Axboecf4464c2007-04-17 20:14:42 +0200632 if (!o->timeout && o->time_based) {
633 log_err("fio: time_based requires a runtime/timeout setting\n");
634 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700635 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200636 }
637
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100638 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200639 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100640
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100641 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700642 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700643 log_info("Multiple writers may overwrite blocks that "
644 "belong to other jobs. This can cause "
645 "verification failures.\n");
646 ret = warnings_fatal;
647 }
648
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100649 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200650 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
651 !o->verify_interval)
652 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboe023fa7d2014-07-23 09:47:26 +0200653
654 /*
655 * Verify interval must be smaller or equal to the
656 * write size.
657 */
658 if (o->verify_interval > o->min_bs[DDIR_WRITE])
659 o->verify_interval = o->min_bs[DDIR_WRITE];
660 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
661 o->verify_interval = o->min_bs[DDIR_READ];
Jens Axboed9905882010-03-18 20:43:00 +0100662 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200663
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100664 if (o->pre_read) {
665 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700666 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200667 log_info("fio: cannot pre-read files with an IO engine"
668 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700669 ret = warnings_fatal;
670 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200671 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200672
Steven Noonanad705bc2013-04-08 15:05:25 -0700673 if (!o->unit_base) {
674 if (td->io_ops->flags & FIO_BIT_BASED)
675 o->unit_base = 1;
676 else
677 o->unit_base = 8;
678 }
679
Jens Axboe67bf9822013-01-10 11:23:19 +0100680#ifndef CONFIG_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100681 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700682 log_info("fio: this platform does not support fdatasync()"
683 " falling back to using fsync(). Use the 'fsync'"
684 " option instead of 'fdatasync' to get rid of"
685 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100686 o->fsync_blocks = o->fdatasync_blocks;
687 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700688 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700689 }
690#endif
691
Bruce Cran93bcfd22012-02-20 20:18:19 +0100692#ifdef WIN32
693 /*
694 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
695 * so fail if we're passed those flags
696 */
697 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
698 log_err("fio: Windows does not support direct or non-buffered io with"
699 " the synchronous ioengines. Use the 'windowsaio' ioengine"
700 " with 'direct=1' and 'iodepth=1' instead.\n");
701 ret = 1;
702 }
703#endif
704
Jens Axboe811ac502012-03-02 22:23:36 +0100705 /*
706 * For fully compressible data, just zero them at init time.
707 * It's faster than repeatedly filling it.
708 */
709 if (td->o.compress_percentage == 100) {
710 td->o.zero_buffers = 1;
711 td->o.compress_percentage = 0;
712 }
713
Jens Axboe5881fda2012-11-15 15:21:23 -0700714 /*
715 * Using a non-uniform random distribution excludes usage of
716 * a random map
717 */
718 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
719 td->o.norandommap = 1;
720
Jens Axboe8d916c92013-04-25 10:11:41 -0600721 /*
722 * If size is set but less than the min block size, complain
723 */
724 if (o->size && o->size < td_min_bs(td)) {
725 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
726 ret = 1;
727 }
728
Chris Masond01612f2013-11-15 15:52:58 -0700729 /*
730 * O_ATOMIC implies O_DIRECT
731 */
732 if (td->o.oatomic)
733 td->o.odirect = 1;
734
Jens Axboe04778ba2014-01-10 20:57:01 -0700735 /*
736 * If randseed is set, that overrides randrepeat
737 */
738 if (td->o.rand_seed)
739 td->o.rand_repeatable = 0;
740
Jens Axboeb1bebc32014-03-18 13:30:32 -0600741 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
742 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
743 ret = 1;
744 }
745
Jens Axboea9523c62011-01-17 16:49:54 -0700746 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200747}
748
Jens Axboe906c8d72006-06-13 09:37:56 +0200749/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100750 * This function leaks the buffer
751 */
Jens Axboe99d633a2012-03-15 15:55:04 +0100752char *fio_uint_to_kmg(unsigned int val)
Jens Axboef8977ee2006-11-06 14:03:03 +0100753{
754 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100755 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100756 char *p = post;
757
Jens Axboe245142f2006-11-08 12:49:21 +0100758 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100759 if (val & 1023)
760 break;
761
762 val >>= 10;
763 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100764 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100765
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100766 snprintf(buf, 32, "%u%c", val, *p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100767 return buf;
768}
769
Jens Axboe09629a92007-03-09 09:00:06 +0100770/* External engines are specified by "external:name.o") */
771static const char *get_engine_name(const char *str)
772{
773 char *p = strstr(str, ":");
774
775 if (!p)
776 return str;
777
778 p++;
779 strip_blank_front(&p);
780 strip_blank_end(p);
781 return p;
782}
783
Jens Axboee132cba2007-03-14 14:23:54 +0100784static int exists_and_not_file(const char *filename)
785{
786 struct stat sb;
787
788 if (lstat(filename, &sb) == -1)
789 return 0;
790
Bruce Cranecc314b2011-01-04 10:59:30 +0100791 /* \\.\ is the device namespace in Windows, where every file
792 * is a device node */
793 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100794 return 0;
795
796 return 1;
797}
798
Jens Axboe4c07ad82011-03-28 09:51:09 +0200799static void td_fill_rand_seeds_internal(struct thread_data *td)
800{
Jens Axboef6787012014-11-05 18:39:23 -0700801 init_rand_seed(&td->bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
802 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
803 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200804
805 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboef6787012014-11-05 18:39:23 -0700806 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200807
Jens Axboef6787012014-11-05 18:39:23 -0700808 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
809 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
810 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200811
812 if (!td_random(td))
813 return;
814
815 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100816 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe4c07ad82011-03-28 09:51:09 +0200817
Jens Axboef6787012014-11-05 18:39:23 -0700818 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
819 init_rand_seed(&td->seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
820 init_rand_seed(&td->seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
821 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 +0200822}
823
Jens Axboe4c07ad82011-03-28 09:51:09 +0200824void td_fill_rand_seeds(struct thread_data *td)
825{
Christian Ehrhardt56e2a5f2014-02-20 09:10:17 -0800826 if (td->o.allrand_repeatable) {
Jens Axboee66dac22014-09-22 10:02:07 -0600827 unsigned int i;
828
829 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
Christian Ehrhardt56e2a5f2014-02-20 09:10:17 -0800830 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
831 + i;
832 }
833
Jens Axboe559073f2014-11-05 18:34:02 -0700834 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600835
Jens Axboed24945f2012-12-04 13:10:29 +0100836 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
Jens Axboee66dac22014-09-22 10:02:07 -0600837 frand_copy(&td->buf_state_prev, &td->buf_state);
838
839 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200840}
841
Jens Axboe9c60ce62007-03-15 09:14:47 +0100842/*
Steven Langde890a12011-11-09 14:03:34 +0100843 * Initializes the ioengine configured for a job, if it has not been done so
844 * already.
845 */
846int ioengine_load(struct thread_data *td)
847{
848 const char *engine;
849
850 /*
851 * Engine has already been loaded.
852 */
853 if (td->io_ops)
854 return 0;
Jens Axboe6e5c4b82013-05-27 09:50:31 +0200855 if (!td->o.ioengine) {
856 log_err("fio: internal fault, no IO engine specified\n");
857 return 1;
858 }
Steven Langde890a12011-11-09 14:03:34 +0100859
860 engine = get_engine_name(td->o.ioengine);
861 td->io_ops = load_ioengine(td, engine);
862 if (!td->io_ops) {
863 log_err("fio: failed to load engine %s\n", engine);
864 return 1;
865 }
866
867 if (td->io_ops->option_struct_size && td->io_ops->options) {
868 /*
869 * In cases where td->eo is set, clone it for a child thread.
870 * This requires that the parent thread has the same ioengine,
871 * but that requirement must be enforced by the code which
872 * cloned the thread.
873 */
874 void *origeo = td->eo;
875 /*
876 * Otherwise use the default thread options.
877 */
878 if (!origeo && td != &def_thread && def_thread.eo &&
879 def_thread.io_ops->options == td->io_ops->options)
880 origeo = def_thread.eo;
881
882 options_init(td->io_ops->options);
883 td->eo = malloc(td->io_ops->option_struct_size);
884 /*
885 * Use the default thread as an option template if this uses the
886 * same options structure and there are non-default options
887 * used.
888 */
889 if (origeo) {
890 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
891 options_mem_dupe(td->eo, td->io_ops->options);
892 } else {
893 memset(td->eo, 0, td->io_ops->option_struct_size);
894 fill_default_options(td->eo, td->io_ops->options);
895 }
896 *(struct thread_data **)td->eo = td;
897 }
898
899 return 0;
900}
901
Jens Axboed72be542012-11-30 19:37:46 +0100902static void init_flags(struct thread_data *td)
903{
904 struct thread_options *o = &td->o;
905
906 if (o->verify_backlog)
907 td->flags |= TD_F_VER_BACKLOG;
908 if (o->trim_backlog)
909 td->flags |= TD_F_TRIM_BACKLOG;
910 if (o->read_iolog_file)
911 td->flags |= TD_F_READ_IOLOG;
912 if (o->refill_buffers)
913 td->flags |= TD_F_REFILL_BUFFERS;
Jens Axboebedc9dc2014-03-17 12:51:09 -0600914 if (o->scramble_buffers)
Jens Axboed72be542012-11-30 19:37:46 +0100915 td->flags |= TD_F_SCRAMBLE_BUFFERS;
916 if (o->verify != VERIFY_NONE)
917 td->flags |= TD_F_VER_NONE;
918}
919
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700920static int setup_random_seeds(struct thread_data *td)
921{
922 unsigned long seed;
923 unsigned int i;
924
Jens Axboe04778ba2014-01-10 20:57:01 -0700925 if (!td->o.rand_repeatable && !td->o.rand_seed)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700926 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
927
Jens Axboe04778ba2014-01-10 20:57:01 -0700928 if (!td->o.rand_seed)
929 seed = 0x89;
930 else
931 seed = td->o.rand_seed;
932
933 for (i = 0; i < 4; i++)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700934 seed *= 0x9e370001UL;
935
936 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
937 td->rand_seeds[i] = seed;
938 seed *= 0x9e370001UL;
939 }
940
941 td_fill_rand_seeds(td);
942 return 0;
943}
944
Jens Axboede98bd32013-04-05 11:09:20 +0200945enum {
946 FPRE_NONE = 0,
947 FPRE_JOBNAME,
948 FPRE_JOBNUM,
949 FPRE_FILENUM
950};
951
952static struct fpre_keyword {
953 const char *keyword;
954 size_t strlen;
955 int key;
956} fpre_keywords[] = {
957 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
958 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
959 { .keyword = "$filenum", .key = FPRE_FILENUM, },
960 { .keyword = NULL, },
961 };
962
Jens Axboe3660cea2014-04-15 08:18:08 -0600963static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
Jens Axboede98bd32013-04-05 11:09:20 +0200964 const char *jobname, int jobnum, int filenum)
965{
966 struct fpre_keyword *f;
967 char copy[PATH_MAX];
Jens Axboeb400a202014-04-14 10:13:46 -0600968 size_t dst_left = PATH_MAX - 1;
Jens Axboede98bd32013-04-05 11:09:20 +0200969
970 if (!o->filename_format || !strlen(o->filename_format)) {
971 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
972 return NULL;
973 }
974
975 for (f = &fpre_keywords[0]; f->keyword; f++)
976 f->strlen = strlen(f->keyword);
977
Jens Axboe3660cea2014-04-15 08:18:08 -0600978 buf[buf_size - 1] = '\0';
979 strncpy(buf, o->filename_format, buf_size - 1);
980
Jens Axboede98bd32013-04-05 11:09:20 +0200981 memset(copy, 0, sizeof(copy));
982 for (f = &fpre_keywords[0]; f->keyword; f++) {
983 do {
984 size_t pre_len, post_start = 0;
985 char *str, *dst = copy;
986
Jens Axboee25b6d52013-04-05 14:44:51 +0200987 str = strcasestr(buf, f->keyword);
Jens Axboede98bd32013-04-05 11:09:20 +0200988 if (!str)
989 break;
990
991 pre_len = str - buf;
992 if (strlen(str) != f->strlen)
993 post_start = pre_len + f->strlen;
994
995 if (pre_len) {
996 strncpy(dst, buf, pre_len);
997 dst += pre_len;
Jens Axboe73a467e2014-04-14 08:34:43 -0600998 dst_left -= pre_len;
Jens Axboede98bd32013-04-05 11:09:20 +0200999 }
1000
1001 switch (f->key) {
Jens Axboe73a467e2014-04-14 08:34:43 -06001002 case FPRE_JOBNAME: {
1003 int ret;
1004
1005 ret = snprintf(dst, dst_left, "%s", jobname);
1006 if (ret < 0)
1007 break;
1008 dst += ret;
1009 dst_left -= ret;
Jens Axboede98bd32013-04-05 11:09:20 +02001010 break;
Jens Axboe73a467e2014-04-14 08:34:43 -06001011 }
1012 case FPRE_JOBNUM: {
1013 int ret;
1014
1015 ret = snprintf(dst, dst_left, "%d", jobnum);
1016 if (ret < 0)
1017 break;
1018 dst += ret;
1019 dst_left -= ret;
Jens Axboede98bd32013-04-05 11:09:20 +02001020 break;
Jens Axboe73a467e2014-04-14 08:34:43 -06001021 }
1022 case FPRE_FILENUM: {
1023 int ret;
1024
1025 ret = snprintf(dst, dst_left, "%d", filenum);
1026 if (ret < 0)
1027 break;
1028 dst += ret;
1029 dst_left -= ret;
Jens Axboede98bd32013-04-05 11:09:20 +02001030 break;
Jens Axboe73a467e2014-04-14 08:34:43 -06001031 }
Jens Axboede98bd32013-04-05 11:09:20 +02001032 default:
1033 assert(0);
1034 break;
1035 }
1036
1037 if (post_start)
Jens Axboe73a467e2014-04-14 08:34:43 -06001038 strncpy(dst, buf + post_start, dst_left);
Jens Axboede98bd32013-04-05 11:09:20 +02001039
Jens Axboe3660cea2014-04-15 08:18:08 -06001040 strncpy(buf, copy, buf_size - 1);
Jens Axboede98bd32013-04-05 11:09:20 +02001041 } while (1);
1042 }
1043
1044 return buf;
1045}
Jens Axboef9633d72013-09-06 09:59:56 -06001046
1047int parse_dryrun(void)
1048{
1049 return dump_cmdline || parse_only;
1050}
1051
Steven Langde890a12011-11-09 14:03:34 +01001052/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001053 * Adds a job to the list of things todo. Sanitizes the various options
1054 * to make sure we don't have conflicts, and initializes various
1055 * members of td.
1056 */
Jens Axboeb7cfb2e2012-03-12 07:47:27 +01001057static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
Jens Axboe46bcd492012-03-14 11:31:21 +01001058 int recursed, int client_type)
Jens Axboeebac4652005-12-08 15:25:21 +01001059{
Jens Axboeaf52b342007-03-13 10:07:47 +01001060 unsigned int i;
Jens Axboeaf52b342007-03-13 10:07:47 +01001061 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +01001062 int numjobs, file_alloced;
Jens Axboede98bd32013-04-05 11:09:20 +02001063 struct thread_options *o = &td->o;
Jens Axboe987c4f42014-07-01 16:29:12 -06001064 char logname[PATH_MAX + 32];
Jens Axboeebac4652005-12-08 15:25:21 +01001065
Jens Axboeebac4652005-12-08 15:25:21 +01001066 /*
1067 * the def_thread is just for options, it's not a real job
1068 */
1069 if (td == &def_thread)
1070 return 0;
1071
Jens Axboed72be542012-11-30 19:37:46 +01001072 init_flags(td);
1073
Jens Axboecca73aa2007-04-04 11:09:19 +02001074 /*
1075 * if we are just dumping the output command line, don't add the job
1076 */
Jens Axboef9633d72013-09-06 09:59:56 -06001077 if (parse_dryrun()) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001078 put_job(td);
1079 return 0;
1080 }
1081
Jens Axboe46bcd492012-03-14 11:31:21 +01001082 td->client_type = client_type;
1083
Jens Axboe58c55ba2010-03-09 12:20:08 +01001084 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +02001085 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +01001086
Steven Langde890a12011-11-09 14:03:34 +01001087 if (ioengine_load(td))
Jens Axboe205927a2007-03-15 11:06:32 +01001088 goto err;
Jens Axboedf641192006-10-12 07:55:41 +02001089
Jens Axboede98bd32013-04-05 11:09:20 +02001090 if (o->odirect)
Jens Axboe690adba2006-10-30 15:25:09 +01001091 td->io_ops->flags |= FIO_RAWIO;
1092
Jens Axboee132cba2007-03-14 14:23:54 +01001093 file_alloced = 0;
Jens Axboede98bd32013-04-05 11:09:20 +02001094 if (!o->filename && !td->files_index && !o->read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +01001095 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +01001096
Jens Axboede98bd32013-04-05 11:09:20 +02001097 if (o->nr_files == 1 && exists_and_not_file(jobname))
Jens Axboe5903e7b2014-02-26 13:42:13 -08001098 add_file(td, jobname, job_add_num, 0);
Jens Axboe7b05a212007-03-13 11:17:07 +01001099 else {
Jens Axboede98bd32013-04-05 11:09:20 +02001100 for (i = 0; i < o->nr_files; i++)
Jens Axboe3660cea2014-04-15 08:18:08 -06001101 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
Jens Axboeaf52b342007-03-13 10:07:47 +01001102 }
Jens Axboe0af7b542006-02-17 10:10:12 +01001103 }
Jens Axboeebac4652005-12-08 15:25:21 +01001104
Jens Axboe4e991c22007-03-15 11:41:11 +01001105 if (fixup_options(td))
1106 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +01001107
Dan Ehrenberg9e684a42012-02-20 11:05:14 +01001108 flow_init_job(td);
1109
Steven Langde890a12011-11-09 14:03:34 +01001110 /*
1111 * IO engines only need this for option callbacks, and the address may
1112 * change in subprocesses.
1113 */
1114 if (td->eo)
1115 *(struct thread_data **)td->eo = NULL;
1116
Jens Axboe07eb79d2007-04-12 13:02:38 +02001117 if (td->io_ops->flags & FIO_DISKLESSIO) {
1118 struct fio_file *f;
1119
1120 for_each_file(td, f, i)
1121 f->real_file_size = -1ULL;
1122 }
1123
Jens Axboe521da522012-08-02 11:21:36 +02001124 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboeebac4652005-12-08 15:25:21 +01001125
Jens Axboede98bd32013-04-05 11:09:20 +02001126 td->ts.clat_percentiles = o->clat_percentiles;
1127 td->ts.percentile_precision = o->percentile_precision;
1128 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
Yu-ju Hong83349192011-08-13 00:53:44 +02001129
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001130 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1131 td->ts.clat_stat[i].min_val = ULONG_MAX;
1132 td->ts.slat_stat[i].min_val = ULONG_MAX;
1133 td->ts.lat_stat[i].min_val = ULONG_MAX;
1134 td->ts.bw_stat[i].min_val = ULONG_MAX;
1135 }
Jens Axboede98bd32013-04-05 11:09:20 +02001136 td->ddir_seq_nr = o->ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +01001137
Jens Axboede98bd32013-04-05 11:09:20 +02001138 if ((o->stonewall || o->new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001139 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001140 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001141 }
Jens Axboeebac4652005-12-08 15:25:21 +01001142
1143 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001144 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +01001145
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001146 if (setup_random_seeds(td)) {
Bruce Cran93bcfd22012-02-20 20:18:19 +01001147 td_verror(td, errno, "init_random_state");
Jens Axboe9c60ce62007-03-15 09:14:47 +01001148 goto err;
Bruce Cran93bcfd22012-02-20 20:18:19 +01001149 }
Jens Axboe9c60ce62007-03-15 09:14:47 +01001150
Jens Axboeebac4652005-12-08 15:25:21 +01001151 if (setup_rate(td))
1152 goto err;
1153
Jens Axboe987c4f42014-07-01 16:29:12 -06001154 if (o->lat_log_file) {
Jens Axboe38a812d2014-07-03 09:10:39 -06001155 struct log_params p = {
1156 .td = td,
1157 .avg_msec = o->log_avg_msec,
1158 .log_type = IO_LOG_TYPE_LAT,
1159 .log_offset = o->log_offset,
1160 .log_gz = o->log_gz,
Jens Axboebac4af12014-07-03 13:42:28 -06001161 .log_gz_store = o->log_gz_store,
Jens Axboe38a812d2014-07-03 09:10:39 -06001162 };
Jens Axboebac4af12014-07-03 13:42:28 -06001163 const char *suf;
Jens Axboe38a812d2014-07-03 09:10:39 -06001164
Jens Axboebac4af12014-07-03 13:42:28 -06001165 if (p.log_gz_store)
1166 suf = "log.fz";
1167 else
1168 suf = "log";
1169
Jens Axboef4786002014-07-09 10:31:34 +02001170 snprintf(logname, sizeof(logname), "%s_lat.%d.%s",
1171 o->lat_log_file, td->thread_number, suf);
Jens Axboe38a812d2014-07-03 09:10:39 -06001172 setup_log(&td->lat_log, &p, logname);
Jens Axboef4786002014-07-09 10:31:34 +02001173 snprintf(logname, sizeof(logname), "%s_slat.%d.%s",
1174 o->lat_log_file, td->thread_number, suf);
Jens Axboe38a812d2014-07-03 09:10:39 -06001175 setup_log(&td->slat_log, &p, logname);
Jens Axboef4786002014-07-09 10:31:34 +02001176 snprintf(logname, sizeof(logname), "%s_clat.%d.%s",
1177 o->lat_log_file, td->thread_number, suf);
Jens Axboe38a812d2014-07-03 09:10:39 -06001178 setup_log(&td->clat_log, &p, logname);
Jens Axboeebac4652005-12-08 15:25:21 +01001179 }
Jens Axboe987c4f42014-07-01 16:29:12 -06001180 if (o->bw_log_file) {
Jens Axboe38a812d2014-07-03 09:10:39 -06001181 struct log_params p = {
1182 .td = td,
1183 .avg_msec = o->log_avg_msec,
1184 .log_type = IO_LOG_TYPE_BW,
1185 .log_offset = o->log_offset,
1186 .log_gz = o->log_gz,
Jens Axboebac4af12014-07-03 13:42:28 -06001187 .log_gz_store = o->log_gz_store,
Jens Axboe38a812d2014-07-03 09:10:39 -06001188 };
Jens Axboebac4af12014-07-03 13:42:28 -06001189 const char *suf;
Jens Axboe38a812d2014-07-03 09:10:39 -06001190
Jens Axboebac4af12014-07-03 13:42:28 -06001191 if (p.log_gz_store)
1192 suf = "log.fz";
1193 else
1194 suf = "log";
1195
Jens Axboef4786002014-07-09 10:31:34 +02001196 snprintf(logname, sizeof(logname), "%s_bw.%d.%s",
1197 o->bw_log_file, td->thread_number, suf);
Jens Axboe38a812d2014-07-03 09:10:39 -06001198 setup_log(&td->bw_log, &p, logname);
Jens Axboe987c4f42014-07-01 16:29:12 -06001199 }
1200 if (o->iops_log_file) {
Jens Axboe38a812d2014-07-03 09:10:39 -06001201 struct log_params p = {
1202 .td = td,
1203 .avg_msec = o->log_avg_msec,
1204 .log_type = IO_LOG_TYPE_IOPS,
1205 .log_offset = o->log_offset,
1206 .log_gz = o->log_gz,
Jens Axboebac4af12014-07-03 13:42:28 -06001207 .log_gz_store = o->log_gz_store,
Jens Axboe38a812d2014-07-03 09:10:39 -06001208 };
Jens Axboebac4af12014-07-03 13:42:28 -06001209 const char *suf;
Jens Axboe38a812d2014-07-03 09:10:39 -06001210
Jens Axboebac4af12014-07-03 13:42:28 -06001211 if (p.log_gz_store)
1212 suf = "log.fz";
1213 else
1214 suf = "log";
1215
Jens Axboef4786002014-07-09 10:31:34 +02001216 snprintf(logname, sizeof(logname), "%s_iops.%d.%s",
1217 o->iops_log_file, td->thread_number, suf);
Jens Axboe38a812d2014-07-03 09:10:39 -06001218 setup_log(&td->iops_log, &p, logname);
Jens Axboe987c4f42014-07-01 16:29:12 -06001219 }
Jens Axboeebac4652005-12-08 15:25:21 +01001220
Jens Axboede98bd32013-04-05 11:09:20 +02001221 if (!o->name)
1222 o->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +02001223
Jens Axboef3afa572012-09-17 13:34:16 +02001224 if (output_format == FIO_OUTPUT_NORMAL) {
Jens Axboeb990b5c2006-09-14 09:48:22 +02001225 if (!job_add_num) {
Jens Axboeb7cfb2e2012-03-12 07:47:27 +01001226 if (is_backend && !recursed)
Jens Axboe2f122b12012-03-15 13:10:19 +01001227 fio_server_send_add_job(td);
Jens Axboe807f9972012-03-02 10:25:24 +01001228
Jens Axboe03530502012-03-19 21:45:12 +01001229 if (!(td->io_ops->flags & FIO_NOIO)) {
Jens Axboed21e2532013-07-25 13:01:11 -06001230 char *c1, *c2, *c3, *c4;
1231 char *c5 = NULL, *c6 = NULL;
Jens Axboef8977ee2006-11-06 14:03:03 +01001232
Jens Axboe22f80452013-04-09 20:29:16 +02001233 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1234 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1235 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1236 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +01001237
Jens Axboed21e2532013-07-25 13:01:11 -06001238 if (!o->bs_is_seq_rand) {
1239 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1240 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1241 }
1242
1243 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1244 td->groupid,
1245 ddir_str(o->td_ddir));
1246
1247 if (o->bs_is_seq_rand)
1248 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1249 c1, c2, c3, c4);
1250 else
1251 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1252 c1, c2, c3, c4, c5, c6);
1253
1254 log_info("ioengine=%s, iodepth=%u\n",
Jens Axboede98bd32013-04-05 11:09:20 +02001255 td->io_ops->name, o->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +01001256
1257 free(c1);
1258 free(c2);
1259 free(c3);
1260 free(c4);
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001261 free(c5);
1262 free(c6);
Jens Axboef8977ee2006-11-06 14:03:03 +01001263 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001264 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +01001265 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001266 }
Jens Axboeebac4652005-12-08 15:25:21 +01001267
1268 /*
1269 * recurse add identical jobs, clear numjobs and stonewall options
1270 * as they don't apply to sub-jobs
1271 */
Jens Axboede98bd32013-04-05 11:09:20 +02001272 numjobs = o->numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +01001273 while (--numjobs) {
Jens Axboeb01e1f32014-06-24 09:19:30 -06001274 struct thread_data *td_new = get_new_job(0, td, 1, jobname);
Jens Axboeebac4652005-12-08 15:25:21 +01001275
1276 if (!td_new)
1277 goto err;
1278
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001279 td_new->o.numjobs = 1;
1280 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +02001281 td_new->o.new_group = 0;
Jiri Horky5a65b4e2014-07-25 09:55:03 +02001282 td_new->subjob_number = numjobs;
Jens Axboee132cba2007-03-14 14:23:54 +01001283
1284 if (file_alloced) {
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001285 if (td_new->files) {
1286 struct fio_file *f;
1287 for_each_file(td_new, f, i) {
1288 if (f->file_name)
Andrey Kuzminece6d642014-04-01 12:27:04 -06001289 sfree(f->file_name);
1290 sfree(f);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001291 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001292 free(td_new->files);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001293 td_new->files = NULL;
1294 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001295 td_new->files_index = 0;
1296 td_new->files_size = 0;
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001297 if (td_new->o.filename) {
1298 free(td_new->o.filename);
1299 td_new->o.filename = NULL;
1300 }
Jens Axboee132cba2007-03-14 14:23:54 +01001301 }
1302
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001303 if (add_job(td_new, jobname, numjobs, 1, client_type))
Jens Axboeebac4652005-12-08 15:25:21 +01001304 goto err;
1305 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001306
Jens Axboeebac4652005-12-08 15:25:21 +01001307 return 0;
1308err:
1309 put_job(td);
1310 return -1;
1311}
1312
Jens Axboe79d16312010-03-04 12:43:20 +01001313/*
1314 * Parse as if 'o' was a command line
1315 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001316void add_job_opts(const char **o, int client_type)
Jens Axboe79d16312010-03-04 12:43:20 +01001317{
1318 struct thread_data *td, *td_parent;
1319 int i, in_global = 1;
1320 char jobname[32];
1321
1322 i = 0;
1323 td_parent = td = NULL;
1324 while (o[i]) {
1325 if (!strncmp(o[i], "name", 4)) {
1326 in_global = 0;
1327 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001328 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001329 td = NULL;
1330 sprintf(jobname, "%s", o[i] + 5);
1331 }
1332 if (in_global && !td_parent)
Jens Axboeb01e1f32014-06-24 09:19:30 -06001333 td_parent = get_new_job(1, &def_thread, 0, jobname);
Jens Axboe79d16312010-03-04 12:43:20 +01001334 else if (!in_global && !td) {
1335 if (!td_parent)
1336 td_parent = &def_thread;
Jens Axboeb01e1f32014-06-24 09:19:30 -06001337 td = get_new_job(0, td_parent, 0, jobname);
Jens Axboe79d16312010-03-04 12:43:20 +01001338 }
1339 if (in_global)
Jens Axboe292cc472013-11-26 20:39:35 -07001340 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001341 else
Jens Axboe292cc472013-11-26 20:39:35 -07001342 fio_options_parse(td, (char **) &o[i], 1, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001343 i++;
1344 }
1345
1346 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001347 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001348}
1349
Jens Axboe01f06b62008-02-18 20:53:47 +01001350static int skip_this_section(const char *name)
1351{
Jens Axboead0a2732011-03-11 10:16:17 +01001352 int i;
1353
1354 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +01001355 return 0;
1356 if (!strncmp(name, "global", 6))
1357 return 0;
1358
Jens Axboead0a2732011-03-11 10:16:17 +01001359 for (i = 0; i < nr_job_sections; i++)
1360 if (!strcmp(job_sections[i], name))
1361 return 0;
1362
1363 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001364}
1365
Jens Axboeebac4652005-12-08 15:25:21 +01001366static int is_empty_or_comment(char *line)
1367{
1368 unsigned int i;
1369
1370 for (i = 0; i < strlen(line); i++) {
1371 if (line[i] == ';')
1372 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001373 if (line[i] == '#')
1374 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +02001375 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +01001376 return 0;
1377 }
1378
1379 return 1;
1380}
1381
Jens Axboe313cb202006-12-21 09:50:00 +01001382/*
Jens Axboe07261982006-06-07 10:51:12 +02001383 * This is our [ini] type file parser.
1384 */
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001385int __parse_jobs_ini(struct thread_data *td,
1386 char *file, int is_buf, int stonewall_flag, int type,
1387 int nested, char *name, char ***popts, int *aopts, int *nopts)
Jens Axboeebac4652005-12-08 15:25:21 +01001388{
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001389 unsigned int global = 0;
1390 char *string;
Jens Axboeebac4652005-12-08 15:25:21 +01001391 FILE *f;
1392 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001393 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +02001394 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +02001395 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +01001396 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001397 char **opts;
1398 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +01001399
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001400 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1401 assert(td || !nested);
1402
Jens Axboe50d16972011-09-29 17:45:28 -06001403 if (is_buf)
1404 f = NULL;
1405 else {
1406 if (!strcmp(file, "-"))
1407 f = stdin;
1408 else
1409 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001410
Jens Axboe50d16972011-09-29 17:45:28 -06001411 if (!f) {
Jens Axboe35899182014-10-07 20:56:28 -06001412 int __err = errno;
1413
1414 log_err("fio: unable to open '%s' job file\n", file);
Jens Axboe682c7e62014-10-08 09:55:22 -06001415 if (td)
1416 td_verror(td, __err, "job file open");
Jens Axboe50d16972011-09-29 17:45:28 -06001417 return 1;
1418 }
Jens Axboeebac4652005-12-08 15:25:21 +01001419 }
1420
1421 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +02001422
1423 /*
1424 * it's really 256 + small bit, 280 should suffice
1425 */
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001426 if (!nested) {
1427 name = malloc(280);
1428 memset(name, 0, 280);
1429 }
Jens Axboeebac4652005-12-08 15:25:21 +01001430
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001431 opts = NULL;
1432 if (nested && popts) {
1433 opts = *popts;
1434 alloc_opts = *aopts;
1435 num_opts = *nopts;
1436 }
1437
1438 if (!opts) {
1439 alloc_opts = 8;
1440 opts = malloc(sizeof(char *) * alloc_opts);
1441 num_opts = 0;
1442 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001443
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001444 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001445 do {
Jens Axboeccf8f122007-09-27 10:48:55 +02001446 /*
1447 * if skip_fgets is set, we already have loaded a line we
1448 * haven't handled.
1449 */
1450 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -06001451 if (is_buf)
1452 p = strsep(&file, "\n");
1453 else
Jens Axboe43f74792011-10-15 14:37:26 +02001454 p = fgets(string, 4096, f);
Jens Axboeccf8f122007-09-27 10:48:55 +02001455 if (!p)
1456 break;
1457 }
gurudas paicdc7f192007-03-29 10:10:56 +02001458
Jens Axboeccf8f122007-09-27 10:48:55 +02001459 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +02001460 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001461 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +02001462
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001463 dprint(FD_PARSE, "%s\n", p);
Jens Axboeebac4652005-12-08 15:25:21 +01001464 if (is_empty_or_comment(p))
1465 continue;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001466
1467 if (!nested) {
1468 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1469 if (inside_skip)
1470 continue;
1471
1472 log_err("fio: option <%s> outside of "
1473 "[] job section\n", p);
Jens Axboec0e19152014-10-06 19:06:55 -06001474 ret = 1;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001475 break;
1476 }
1477
1478 name[strlen(name) - 1] = '\0';
1479
1480 if (skip_this_section(name)) {
1481 inside_skip = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001482 continue;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001483 } else
1484 inside_skip = 0;
1485
1486 dprint(FD_PARSE, "Parsing section [%s]\n", name);
1487
1488 global = !strncmp(name, "global", 6);
1489
1490 if (dump_cmdline) {
1491 if (first_sect)
1492 log_info("fio ");
1493 if (!global)
1494 log_info("--name=%s ", name);
1495 first_sect = 0;
1496 }
1497
1498 td = get_new_job(global, &def_thread, 0, name);
1499 if (!td) {
1500 ret = 1;
1501 break;
1502 }
1503
1504 /*
1505 * Separate multiple job files by a stonewall
1506 */
1507 if (!global && stonewall) {
1508 td->o.stonewall = stonewall;
1509 stonewall = 0;
1510 }
1511
1512 num_opts = 0;
1513 memset(opts, 0, alloc_opts * sizeof(char *));
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001514 }
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001515 else
1516 skip_fgets = 1;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001517
Jens Axboe50d16972011-09-29 17:45:28 -06001518 while (1) {
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001519 if (!skip_fgets) {
1520 if (is_buf)
1521 p = strsep(&file, "\n");
1522 else
1523 p = fgets(string, 4096, f);
1524 if (!p)
1525 break;
1526 dprint(FD_PARSE, "%s", p);
1527 }
Jens Axboe50d16972011-09-29 17:45:28 -06001528 else
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001529 skip_fgets = 0;
Jens Axboe50d16972011-09-29 17:45:28 -06001530
Jens Axboeebac4652005-12-08 15:25:21 +01001531 if (is_empty_or_comment(p))
1532 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001533
Jens Axboeb6754f92006-05-30 14:29:32 +02001534 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001535
Jens Axboeccf8f122007-09-27 10:48:55 +02001536 /*
1537 * new section, break out and make sure we don't
1538 * fgets() a new line at the top.
1539 */
1540 if (p[0] == '[') {
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001541 if (nested) {
1542 log_err("No new sections in included files\n");
1543 return 1;
1544 }
1545
Jens Axboeccf8f122007-09-27 10:48:55 +02001546 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001547 break;
Jens Axboeccf8f122007-09-27 10:48:55 +02001548 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001549
Jens Axboe4ae3f762006-05-30 13:35:14 +02001550 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001551
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001552 if (!strncmp(p, "include", strlen("include"))) {
1553 char *filename = p + strlen("include") + 1;
1554
1555 if ((ret = __parse_jobs_ini(td, filename,
1556 is_buf, stonewall_flag, type, 1,
1557 name, &opts, &alloc_opts, &num_opts))) {
1558 log_err("Error %d while parsing include file %s\n",
1559 ret, filename);
1560 break;
1561 }
1562 continue;
1563 }
1564
Jens Axboe3b8b7132008-06-10 19:46:23 +02001565 if (num_opts == alloc_opts) {
1566 alloc_opts <<= 1;
1567 opts = realloc(opts,
1568 alloc_opts * sizeof(char *));
1569 }
1570
1571 opts[num_opts] = strdup(p);
1572 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +01001573 }
Jens Axboeebac4652005-12-08 15:25:21 +01001574
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001575 if (nested) {
1576 *popts = opts;
1577 *aopts = alloc_opts;
1578 *nopts = num_opts;
1579 goto out;
1580 }
1581
Jens Axboe292cc472013-11-26 20:39:35 -07001582 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1583 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001584 ret = add_job(td, name, 0, 0, type);
Jens Axboe292cc472013-11-26 20:39:35 -07001585 else {
Jens Axboeb1508cf2006-11-02 09:12:40 +01001586 log_err("fio: job %s dropped\n", name);
1587 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001588 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001589
1590 for (i = 0; i < num_opts; i++)
1591 free(opts[i]);
1592 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001593 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001594
Jens Axboecca73aa2007-04-04 11:09:19 +02001595 if (dump_cmdline)
1596 log_info("\n");
1597
Jens Axboe7e356b22011-10-14 10:55:16 +02001598 i = 0;
1599 while (i < nr_job_sections) {
1600 free(job_sections[i]);
1601 i++;
1602 }
1603
Jens Axboe25775942008-06-10 20:26:06 +02001604 free(opts);
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001605out:
1606 free(string);
1607 if (!nested)
1608 free(name);
Jens Axboe50d16972011-09-29 17:45:28 -06001609 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001610 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001611 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001612}
1613
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001614int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1615{
1616 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
1617 0, NULL, NULL, NULL, NULL);
1618}
1619
Jens Axboeebac4652005-12-08 15:25:21 +01001620static int fill_def_thread(void)
1621{
1622 memset(&def_thread, 0, sizeof(def_thread));
1623
Jens Axboe375b2692007-05-22 09:13:02 +02001624 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001625 def_thread.o.error_dump = 1;
Jens Axboe25bd16c2014-04-06 10:09:30 -06001626
Jens Axboeebac4652005-12-08 15:25:21 +01001627 /*
Jens Axboeee738492007-01-10 11:23:16 +01001628 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001629 */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001630 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001631 return 0;
1632}
1633
Jens Axboe45378b32007-12-19 13:54:38 +01001634static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02001635{
Jens Axboe3d433822012-03-21 22:25:22 +01001636 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01001637 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboe83881282011-10-17 19:58:51 +02001638 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1639 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
Jens Axboe4f3fe6c2014-07-09 23:01:03 +02001640 "\t\t\tdiskutil,job,mutex,profile,time,net,rate,compress\n");
Jens Axboe111e0322013-03-07 11:31:20 +01001641 printf(" --parse-only\t\tParse options only, don't start any IO\n");
Jens Axboe83881282011-10-17 19:58:51 +02001642 printf(" --output\t\tWrite output to file\n");
liang xieb2cecdc2012-08-31 08:22:42 -07001643 printf(" --runtime\t\tRuntime in seconds\n");
Jens Axboe83881282011-10-17 19:58:51 +02001644 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1645 printf(" --minimal\t\tMinimal (terse) output\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001646 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
Jens Axboe75db6e22011-11-07 22:07:36 +01001647 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001648 printf(" --version\t\tPrint version info and exit\n");
Jens Axboe83881282011-10-17 19:58:51 +02001649 printf(" --help\t\tPrint this page\n");
Jens Axboe23893642012-12-17 14:44:08 +01001650 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
Jens Axboefec0f212014-02-07 14:39:33 -07001651 printf(" --crctest\t\tTest speed of checksum functions\n");
Jens Axboe83881282011-10-17 19:58:51 +02001652 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001653 " them\n");
Steven Langde890a12011-11-09 14:03:34 +01001654 printf(" --enghelp=engine\tPrint ioengine help, or list"
1655 " available ioengines\n");
1656 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1657 " cmd\n");
Jens Axboe83881282011-10-17 19:58:51 +02001658 printf(" --showcmd\t\tTurn a job file into command line options\n");
1659 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1660 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboee382e662013-02-22 20:48:56 +01001661 printf(" --eta-newline=time\tForce a new line for every 'time'");
1662 printf(" period passed\n");
Jens Axboe06464902013-04-24 20:38:54 -06001663 printf(" --status-interval=t\tForce full status dump every");
1664 printf(" 't' period passed\n");
Jens Axboe83881282011-10-17 19:58:51 +02001665 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001666 " writes\n");
Jens Axboe83881282011-10-17 19:58:51 +02001667 printf(" --section=name\tOnly run specified section in job file\n");
1668 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
Jens Axboe2b386d22008-03-26 10:32:57 +01001669 " (def 1024)\n");
Jens Axboe83881282011-10-17 19:58:51 +02001670 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1671 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1672 printf(" --server=args\t\tStart a backend fio server\n");
1673 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1674 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
Jens Axboe35899182014-10-07 20:56:28 -06001675 printf(" --remote-config=file\tTell fio server to load this local job file\n");
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001676 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1677 "\t\t\t(option=system,percpu) or run unit work\n"
1678 "\t\t\tcalibration only (option=calibrate)\n");
Jens Axboebac4af12014-07-03 13:42:28 -06001679#ifdef CONFIG_ZLIB
1680 printf(" --inflate-log=log\tInflate and output compressed log\n");
1681#endif
Jens Axboede54cfd2014-11-10 20:34:00 -07001682 printf(" --trigger=file:cmd\tExecute trigger cmd when file exists\n");
1683 printf(" --trigger-timeout=t\tExecute trigger af this time\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001684 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
Jens Axboebfca2c72014-01-29 16:39:04 -07001685 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1686 printf("\n Jens Axboe <axboe@fb.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001687}
1688
Jens Axboe79e48f72008-02-01 20:37:09 +01001689#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001690struct debug_level debug_levels[] = {
Jens Axboe0b8d11e2012-03-02 19:44:15 +01001691 { .name = "process",
1692 .help = "Process creation/exit logging",
1693 .shift = FD_PROCESS,
1694 },
1695 { .name = "file",
1696 .help = "File related action logging",
1697 .shift = FD_FILE,
1698 },
1699 { .name = "io",
1700 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1701 .shift = FD_IO,
1702 },
1703 { .name = "mem",
1704 .help = "Memory allocation/freeing logging",
1705 .shift = FD_MEM,
1706 },
1707 { .name = "blktrace",
1708 .help = "blktrace action logging",
1709 .shift = FD_BLKTRACE,
1710 },
1711 { .name = "verify",
1712 .help = "IO verification action logging",
1713 .shift = FD_VERIFY,
1714 },
1715 { .name = "random",
1716 .help = "Random generation logging",
1717 .shift = FD_RANDOM,
1718 },
1719 { .name = "parse",
1720 .help = "Parser logging",
1721 .shift = FD_PARSE,
1722 },
1723 { .name = "diskutil",
1724 .help = "Disk utility logging actions",
1725 .shift = FD_DISKUTIL,
1726 },
1727 { .name = "job",
1728 .help = "Logging related to creating/destroying jobs",
1729 .shift = FD_JOB,
1730 },
1731 { .name = "mutex",
1732 .help = "Mutex logging",
1733 .shift = FD_MUTEX
1734 },
1735 { .name = "profile",
1736 .help = "Logging related to profiles",
1737 .shift = FD_PROFILE,
1738 },
1739 { .name = "time",
1740 .help = "Logging related to time keeping functions",
1741 .shift = FD_TIME,
1742 },
1743 { .name = "net",
1744 .help = "Network logging",
1745 .shift = FD_NET,
1746 },
Jens Axboe3e260a42013-12-09 12:38:53 -07001747 { .name = "rate",
1748 .help = "Rate logging",
1749 .shift = FD_RATE,
1750 },
Jens Axboe4f3fe6c2014-07-09 23:01:03 +02001751 { .name = "compress",
1752 .help = "Log compression logging",
1753 .shift = FD_COMPRESS,
1754 },
Jens Axboe02444ad2008-10-07 11:33:00 +02001755 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001756};
1757
Jens Axboec09823a2008-02-19 20:16:57 +01001758static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001759{
1760 struct debug_level *dl;
1761 char *p = (char *) string;
1762 char *opt;
1763 int i;
1764
1765 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001766 log_info("fio: dumping debug options:");
1767 for (i = 0; debug_levels[i].name; i++) {
1768 dl = &debug_levels[i];
1769 log_info("%s,", dl->name);
1770 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001771 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001772 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001773 }
1774
1775 while ((opt = strsep(&p, ",")) != NULL) {
1776 int found = 0;
1777
Jens Axboe5e1d3062008-05-23 11:55:53 +02001778 if (!strncmp(opt, "all", 3)) {
1779 log_info("fio: set all debug options\n");
1780 fio_debug = ~0UL;
1781 continue;
1782 }
1783
Jens Axboeee56ad52008-02-01 10:30:20 +01001784 for (i = 0; debug_levels[i].name; i++) {
1785 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001786 found = !strncmp(opt, dl->name, strlen(dl->name));
1787 if (!found)
1788 continue;
1789
1790 if (dl->shift == FD_JOB) {
1791 opt = strchr(opt, ':');
1792 if (!opt) {
1793 log_err("fio: missing job number\n");
1794 break;
1795 }
1796 opt++;
1797 fio_debug_jobno = atoi(opt);
1798 log_info("fio: set debug jobno %d\n",
1799 fio_debug_jobno);
1800 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001801 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001802 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001803 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001804 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001805 }
1806
1807 if (!found)
1808 log_err("fio: debug mask %s not found\n", opt);
1809 }
Jens Axboec09823a2008-02-19 20:16:57 +01001810 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001811}
Jens Axboe79e48f72008-02-01 20:37:09 +01001812#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001813static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001814{
1815 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001816 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001817}
1818#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001819
Jens Axboe4c6107f2011-01-18 05:22:22 -07001820static void fio_options_fill_optstring(void)
1821{
1822 char *ostr = cmd_optstr;
1823 int i, c;
1824
1825 c = i = 0;
1826 while (l_opts[i].name) {
1827 ostr[c++] = l_opts[i].val;
1828 if (l_opts[i].has_arg == required_argument)
1829 ostr[c++] = ':';
1830 else if (l_opts[i].has_arg == optional_argument) {
1831 ostr[c++] = ':';
1832 ostr[c++] = ':';
1833 }
1834 i++;
1835 }
1836 ostr[c] = '\0';
1837}
1838
Jens Axboec2c94582011-10-05 20:31:30 +02001839static int client_flag_set(char c)
1840{
1841 int i;
1842
1843 i = 0;
1844 while (l_opts[i].name) {
1845 int val = l_opts[i].val;
1846
1847 if (c == (val & 0xff))
1848 return (val & FIO_CLIENT_FLAG);
1849
1850 i++;
1851 }
1852
1853 return 0;
1854}
1855
Jens Axboe10aa1362014-04-01 21:10:36 -06001856static void parse_cmd_client(void *client, char *opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +02001857{
Jens Axboefa2ea802011-10-08 21:07:29 +02001858 fio_client_add_cmd_option(client, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001859}
1860
Jens Axboe46bcd492012-03-14 11:31:21 +01001861int parse_cmd_line(int argc, char *argv[], int client_type)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001862{
1863 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001864 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001865 char *ostr = cmd_optstr;
Jens Axboe402668f2011-10-10 15:28:58 +02001866 void *pid_file = NULL;
Jens Axboe9bae26e2011-10-07 10:07:22 +02001867 void *cur_client = NULL;
Jens Axboe81179ee2011-10-04 12:42:06 +02001868 int backend = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001869
Jens Axboec2c94582011-10-05 20:31:30 +02001870 /*
1871 * Reset optind handling, since we may call this multiple times
1872 * for the backend.
1873 */
1874 optind = 1;
Jens Axboe7a4b8242011-10-05 17:35:15 +02001875
Jens Axboec2c94582011-10-05 20:31:30 +02001876 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
1877 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
Jens Axboefa2ea802011-10-08 21:07:29 +02001878 parse_cmd_client(cur_client, argv[optind - 1]);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001879 c &= ~FIO_CLIENT_FLAG;
1880 }
1881
Jens Axboe214e1ec2007-03-15 10:48:13 +01001882 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001883 case 'a':
1884 smalloc_pool_size = atoi(optarg);
1885 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001886 case 't':
Jens Axboe25bd16c2014-04-06 10:09:30 -06001887 if (check_str_time(optarg, &def_timeout, 1)) {
1888 log_err("fio: failed parsing time %s\n", optarg);
1889 do_exit++;
1890 exit_val = 1;
1891 }
Jens Axboe214e1ec2007-03-15 10:48:13 +01001892 break;
1893 case 'l':
Jens Axboe987c4f42014-07-01 16:29:12 -06001894 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
1895 do_exit++;
1896 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001897 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001898 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001899 write_bw_log = 1;
1900 break;
1901 case 'o':
Rik Faith52482242014-05-29 14:13:39 -06001902 if (f_out && f_out != stdout)
Jens Axboe5e1d8742014-04-11 11:39:46 -06001903 fclose(f_out);
1904
Jens Axboe214e1ec2007-03-15 10:48:13 +01001905 f_out = fopen(optarg, "w+");
1906 if (!f_out) {
1907 perror("fopen output");
1908 exit(1);
1909 }
1910 f_err = f_out;
1911 break;
1912 case 'm':
Jens Axboef3afa572012-09-17 13:34:16 +02001913 output_format = FIO_OUTPUT_TERSE;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001914 break;
Jens Axboef3afa572012-09-17 13:34:16 +02001915 case 'F':
Stefan Hajnoczid10983d2014-02-04 14:27:11 +01001916 if (!optarg) {
1917 log_err("fio: missing --output-format argument\n");
1918 exit_val = 1;
1919 do_exit++;
1920 break;
1921 }
Jens Axboef3afa572012-09-17 13:34:16 +02001922 if (!strcmp(optarg, "minimal") ||
1923 !strcmp(optarg, "terse") ||
1924 !strcmp(optarg, "csv"))
1925 output_format = FIO_OUTPUT_TERSE;
1926 else if (!strcmp(optarg, "json"))
1927 output_format = FIO_OUTPUT_JSON;
1928 else
1929 output_format = FIO_OUTPUT_NORMAL;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001930 break;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +01001931 case 'f':
1932 append_terse_output = 1;
1933 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001934 case 'h':
Jens Axboe7d8ea972014-04-14 13:37:46 -06001935 did_arg = 1;
Jens Axboe7874f8b2011-10-06 09:18:20 +02001936 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001937 usage(argv[0]);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001938 do_exit++;
1939 }
Jens Axboec2c94582011-10-05 20:31:30 +02001940 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001941 case 'c':
Jens Axboe7d8ea972014-04-14 13:37:46 -06001942 did_arg = 1;
Jens Axboe7874f8b2011-10-06 09:18:20 +02001943 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001944 fio_show_option_help(optarg);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001945 do_exit++;
1946 }
Jens Axboec2c94582011-10-05 20:31:30 +02001947 break;
Steven Langde890a12011-11-09 14:03:34 +01001948 case 'i':
Jens Axboe7d8ea972014-04-14 13:37:46 -06001949 did_arg = 1;
Steven Langde890a12011-11-09 14:03:34 +01001950 if (!cur_client) {
1951 fio_show_ioengine_help(optarg);
1952 do_exit++;
1953 }
1954 break;
Jens Axboecca73aa2007-04-04 11:09:19 +02001955 case 's':
Jens Axboe7d8ea972014-04-14 13:37:46 -06001956 did_arg = 1;
Jens Axboecca73aa2007-04-04 11:09:19 +02001957 dump_cmdline = 1;
1958 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001959 case 'r':
1960 read_only = 1;
1961 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001962 case 'v':
Jens Axboe7d8ea972014-04-14 13:37:46 -06001963 did_arg = 1;
Jens Axboe7874f8b2011-10-06 09:18:20 +02001964 if (!cur_client) {
Jens Axboe3d433822012-03-21 22:25:22 +01001965 log_info("%s\n", fio_version_string);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001966 do_exit++;
1967 }
Jens Axboec2c94582011-10-05 20:31:30 +02001968 break;
Jens Axboef57a9c52011-09-09 21:01:37 +02001969 case 'V':
1970 terse_version = atoi(optarg);
Jens Axboe09786f52012-10-04 17:06:49 +02001971 if (!(terse_version == 2 || terse_version == 3 ||
1972 terse_version == 4)) {
Jens Axboef57a9c52011-09-09 21:01:37 +02001973 log_err("fio: bad terse version format\n");
1974 exit_val = 1;
1975 do_exit++;
1976 }
1977 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001978 case 'e':
1979 if (!strcmp("always", optarg))
1980 eta_print = FIO_ETA_ALWAYS;
1981 else if (!strcmp("never", optarg))
1982 eta_print = FIO_ETA_NEVER;
1983 break;
Jens Axboee382e662013-02-22 20:48:56 +01001984 case 'E': {
1985 long long t = 0;
1986
Stephen M. Cameron79dc9142014-11-10 20:31:26 -07001987 if (check_str_time(optarg, &t, 1)) {
Jens Axboee382e662013-02-22 20:48:56 +01001988 log_err("fio: failed parsing eta time %s\n", optarg);
1989 exit_val = 1;
1990 do_exit++;
1991 }
Stephen M. Cameron79dc9142014-11-10 20:31:26 -07001992 eta_new_line = t / 1000;
Jens Axboee382e662013-02-22 20:48:56 +01001993 break;
1994 }
Jens Axboeee56ad52008-02-01 10:30:20 +01001995 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001996 if (set_debug(optarg))
1997 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001998 break;
Jens Axboe111e0322013-03-07 11:31:20 +01001999 case 'P':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002000 did_arg = 1;
Jens Axboe111e0322013-03-07 11:31:20 +01002001 parse_only = 1;
2002 break;
Jens Axboead0a2732011-03-11 10:16:17 +01002003 case 'x': {
2004 size_t new_size;
2005
Jens Axboe01f06b62008-02-18 20:53:47 +01002006 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01002007 log_err("fio: can't use global as only "
2008 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01002009 do_exit++;
2010 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01002011 break;
2012 }
Jens Axboead0a2732011-03-11 10:16:17 +01002013 new_size = (nr_job_sections + 1) * sizeof(char *);
2014 job_sections = realloc(job_sections, new_size);
2015 job_sections[nr_job_sections] = strdup(optarg);
2016 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01002017 break;
Jens Axboead0a2732011-03-11 10:16:17 +01002018 }
Jens Axboebac4af12014-07-03 13:42:28 -06002019#ifdef CONFIG_ZLIB
2020 case 'X':
2021 exit_val = iolog_file_inflate(optarg);
2022 did_arg++;
2023 do_exit++;
2024 break;
2025#endif
Jens Axboe9ac8a792009-11-13 21:23:07 +01002026 case 'p':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002027 did_arg = 1;
Jens Axboe4af7c002014-04-11 11:38:38 -06002028 if (exec_profile)
2029 free(exec_profile);
Jens Axboe07b32322010-03-05 09:48:44 +01002030 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01002031 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002032 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01002033 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002034 char *val = optarg;
2035
2036 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002037 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe66251552011-05-12 10:14:57 +02002038 if (ret)
Jens Axboe4a4ac4e2014-04-14 08:17:30 -06002039 goto out_free;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002040 td = NULL;
Jens Axboe7d8ea972014-04-14 13:37:46 -06002041 did_arg = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002042 }
2043 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01002044 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02002045 int global = 0;
2046
Jens Axboe01f06b62008-02-18 20:53:47 +01002047 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02002048 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002049
Jens Axboe01f06b62008-02-18 20:53:47 +01002050 if (is_section && skip_this_section(val))
2051 continue;
2052
Jens Axboeb01e1f32014-06-24 09:19:30 -06002053 td = get_new_job(global, &def_thread, 1, NULL);
Jens Axboe1cefc792014-06-09 13:54:22 -06002054 if (!td || ioengine_load(td)) {
2055 if (td) {
2056 put_job(td);
2057 td = NULL;
2058 }
2059 do_exit++;
2060 break;
2061 }
Steven Langde890a12011-11-09 14:03:34 +01002062 fio_options_set_ioengine_opts(l_opts, td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01002063 }
2064
Jens Axboe9d918182013-05-23 20:00:35 +02002065 if ((!val || !strlen(val)) &&
2066 l_opts[lidx].has_arg == required_argument) {
2067 log_err("fio: option %s requires an argument\n", opt);
2068 ret = 1;
2069 } else
2070 ret = fio_cmd_option_parse(td, opt, val);
2071
Jens Axboebfb3ea22013-05-23 19:54:43 +02002072 if (ret) {
2073 if (td) {
2074 put_job(td);
2075 td = NULL;
2076 }
Jens Axboea88c8c12013-04-24 22:25:22 -06002077 do_exit++;
Jens Axboebfb3ea22013-05-23 19:54:43 +02002078 }
Steven Langde890a12011-11-09 14:03:34 +01002079
2080 if (!ret && !strcmp(opt, "ioengine")) {
2081 free_ioengine(td);
Jens Axboe1cefc792014-06-09 13:54:22 -06002082 if (ioengine_load(td)) {
Jens Axboe9c574af2014-07-03 15:33:24 -06002083 put_job(td);
2084 td = NULL;
Jens Axboe1cefc792014-06-09 13:54:22 -06002085 do_exit++;
2086 break;
2087 }
Steven Langde890a12011-11-09 14:03:34 +01002088 fio_options_set_ioengine_opts(l_opts, td);
2089 }
2090 break;
2091 }
2092 case FIO_GETOPT_IOENGINE: {
2093 const char *opt = l_opts[lidx].name;
2094 char *val = optarg;
Jens Axboeb1ed74e2014-04-14 12:07:36 -06002095
2096 if (!td)
2097 break;
2098
Steven Langde890a12011-11-09 14:03:34 +01002099 ret = fio_cmd_ioengine_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01002100 break;
2101 }
Jens Axboea9523c62011-01-17 16:49:54 -07002102 case 'w':
2103 warnings_fatal = 1;
2104 break;
Jens Axboefca70352011-07-06 20:12:54 +02002105 case 'j':
2106 max_jobs = atoi(optarg);
2107 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2108 log_err("fio: invalid max jobs: %d\n", max_jobs);
2109 do_exit++;
2110 exit_val = 1;
2111 }
2112 break;
Jens Axboe50d16972011-09-29 17:45:28 -06002113 case 'S':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002114 did_arg = 1;
Jens Axboeef7035a2014-06-18 15:30:09 -07002115#ifndef CONFIG_NO_SHM
Jens Axboea37f69b2011-10-01 12:24:21 -06002116 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06002117 log_err("fio: can't be both client and server\n");
2118 do_exit++;
2119 exit_val = 1;
2120 break;
2121 }
Jens Axboe87aa8f12011-10-06 21:24:13 +02002122 if (optarg)
Jens Axboebebe6392011-10-07 10:00:51 +02002123 fio_server_set_arg(optarg);
Jens Axboe50d16972011-09-29 17:45:28 -06002124 is_backend = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +02002125 backend = 1;
Jens Axboeef7035a2014-06-18 15:30:09 -07002126#else
2127 log_err("fio: client/server requires SHM support\n");
2128 do_exit++;
2129 exit_val = 1;
2130#endif
Jens Axboe50d16972011-09-29 17:45:28 -06002131 break;
Jens Axboee46d8092011-10-03 09:11:02 +02002132 case 'D':
Jens Axboe6cfe9a82014-04-14 13:29:52 -06002133 if (pid_file)
2134 free(pid_file);
Jens Axboe402668f2011-10-10 15:28:58 +02002135 pid_file = strdup(optarg);
Jens Axboee46d8092011-10-03 09:11:02 +02002136 break;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01002137 case 'I':
2138 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2139 /* exit on error and calibration only */
Jens Axboe7d8ea972014-04-14 13:37:46 -06002140 did_arg = 1;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01002141 do_exit++;
Jens Axboe7d8ea972014-04-14 13:37:46 -06002142 if (ret == -1)
Huadong Liuf2a2ce02013-01-30 13:22:24 +01002143 exit_val = 1;
2144 }
2145 break;
Jens Axboe132159a2011-09-30 15:01:32 -06002146 case 'C':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002147 did_arg = 1;
Jens Axboe132159a2011-09-30 15:01:32 -06002148 if (is_backend) {
2149 log_err("fio: can't be both client and server\n");
2150 do_exit++;
2151 exit_val = 1;
2152 break;
2153 }
Jens Axboea5276612012-03-04 15:15:08 +01002154 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
Jens Axboebebe6392011-10-07 10:00:51 +02002155 log_err("fio: failed adding client %s\n", optarg);
2156 do_exit++;
2157 exit_val = 1;
2158 break;
2159 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02002160 /*
2161 * If the next argument exists and isn't an option,
2162 * assume it's a job file for this client only.
2163 */
2164 while (optind < argc) {
2165 if (!strncmp(argv[optind], "--", 2) ||
2166 !strncmp(argv[optind], "-", 1))
2167 break;
2168
Jens Axboe35899182014-10-07 20:56:28 -06002169 if (fio_client_add_ini_file(cur_client, argv[optind], 0))
2170 break;
Jens Axboe14ea90e2012-08-26 17:33:34 +02002171 optind++;
2172 }
Jens Axboe132159a2011-09-30 15:01:32 -06002173 break;
Jens Axboe35899182014-10-07 20:56:28 -06002174 case 'R':
2175 did_arg = 1;
2176 if (fio_client_add_ini_file(cur_client, optarg, 1)) {
2177 do_exit++;
2178 exit_val = 1;
2179 }
2180 break;
Jens Axboe7d11f872012-12-17 12:03:29 +01002181 case 'T':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002182 did_arg = 1;
Jens Axboe7d11f872012-12-17 12:03:29 +01002183 do_exit++;
2184 exit_val = fio_monotonic_clocktest();
2185 break;
Jens Axboefec0f212014-02-07 14:39:33 -07002186 case 'G':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002187 did_arg = 1;
Jens Axboefec0f212014-02-07 14:39:33 -07002188 do_exit++;
2189 exit_val = fio_crctest(optarg);
2190 break;
Jens Axboe06464902013-04-24 20:38:54 -06002191 case 'L': {
2192 long long val;
2193
Stephen M. Cameron79dc9142014-11-10 20:31:26 -07002194 if (check_str_time(optarg, &val, 1)) {
Jens Axboe06464902013-04-24 20:38:54 -06002195 log_err("fio: failed parsing time %s\n", optarg);
2196 do_exit++;
2197 exit_val = 1;
2198 break;
2199 }
Stephen M. Cameron79dc9142014-11-10 20:31:26 -07002200 status_interval = val / 1000;
Jens Axboe06464902013-04-24 20:38:54 -06002201 break;
2202 }
Jens Axboede54cfd2014-11-10 20:34:00 -07002203 case 'W': {
2204 char *split, *cmd;
2205 size_t sz;
2206
2207 split = strchr(optarg, ':');
2208 if (!split) {
2209 log_err("fio: trigger is file:command\n");
2210 do_exit++;
2211 exit_val = 1;
2212 }
2213
2214 sz = split - optarg;
2215 trigger_file = calloc(1, sz + 1);
2216 strncpy(trigger_file, optarg, sz);
2217
2218 split++;
2219 cmd = trigger_cmd = strdup(split);
2220 strip_blank_front(&trigger_cmd);
2221 strip_blank_end(trigger_cmd);
2222 if (strlen(trigger_cmd) == 0) {
2223 free(cmd);
2224 trigger_cmd = NULL;
2225 }
2226 break;
2227 }
2228 case 'B':
2229 if (check_str_time(optarg, &trigger_timeout, 1)) {
2230 log_err("fio: failed parsing time %s\n", optarg);
2231 do_exit++;
2232 exit_val = 1;
2233 }
2234 trigger_timeout /= 1000000;
2235 break;
Jens Axboe798827c2013-01-23 18:11:48 -07002236 case '?':
2237 log_err("%s: unrecognized option '%s'\n", argv[0],
2238 argv[optind - 1]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01002239 default:
Jens Axboec09823a2008-02-19 20:16:57 +01002240 do_exit++;
2241 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002242 break;
2243 }
Jens Axboec7d5c942011-10-03 11:48:17 +02002244 if (do_exit)
2245 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002246 }
2247
Jens Axboe0b14f0a2013-05-23 21:27:54 +02002248 if (do_exit && !(is_backend || nr_clients))
2249 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01002250
Jens Axboefb296042014-04-02 08:28:49 -06002251 if (nr_clients && fio_clients_connect())
2252 exit(1);
Jens Axboe132159a2011-09-30 15:01:32 -06002253
Jens Axboe81179ee2011-10-04 12:42:06 +02002254 if (is_backend && backend)
Jens Axboe402668f2011-10-10 15:28:58 +02002255 return fio_start_server(pid_file);
Jens Axboeb8ba87a2014-04-11 11:37:34 -06002256 else if (pid_file)
2257 free(pid_file);
Jens Axboe50d16972011-09-29 17:45:28 -06002258
Jens Axboe214e1ec2007-03-15 10:48:13 +01002259 if (td) {
Jens Axboe7d8ea972014-04-14 13:37:46 -06002260 if (!ret) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002261 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe7d8ea972014-04-14 13:37:46 -06002262 if (ret)
2263 did_arg = 1;
2264 }
Jens Axboe214e1ec2007-03-15 10:48:13 +01002265 }
2266
Jens Axboe7874f8b2011-10-06 09:18:20 +02002267 while (!ret && optind < argc) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01002268 ini_idx++;
2269 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2270 ini_file[ini_idx - 1] = strdup(argv[optind]);
2271 optind++;
2272 }
2273
Jens Axboe4a4ac4e2014-04-14 08:17:30 -06002274out_free:
2275 if (pid_file)
2276 free(pid_file);
2277
Jens Axboe214e1ec2007-03-15 10:48:13 +01002278 return ini_idx;
2279}
2280
Jens Axboe0420ba62012-02-29 11:16:52 +01002281int fio_init_options(void)
Jens Axboeebac4652005-12-08 15:25:21 +01002282{
Jens Axboeb4692822006-10-27 13:43:22 +02002283 f_out = stdout;
2284 f_err = stderr;
2285
Jens Axboe4c6107f2011-01-18 05:22:22 -07002286 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01002287 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02002288
Jens Axboe9d9eb2e2011-10-03 12:01:42 +02002289 atexit(free_shm);
2290
Jens Axboeebac4652005-12-08 15:25:21 +01002291 if (fill_def_thread())
2292 return 1;
2293
Jens Axboe0420ba62012-02-29 11:16:52 +01002294 return 0;
2295}
2296
Jens Axboe51167792012-03-08 21:34:18 +01002297extern int fio_check_options(struct thread_options *);
2298
Jens Axboe0420ba62012-02-29 11:16:52 +01002299int parse_options(int argc, char *argv[])
2300{
Jens Axboe46bcd492012-03-14 11:31:21 +01002301 const int type = FIO_CLIENT_TYPE_CLI;
Jens Axboe0420ba62012-02-29 11:16:52 +01002302 int job_files, i;
2303
2304 if (fio_init_options())
2305 return 1;
Jens Axboe51167792012-03-08 21:34:18 +01002306 if (fio_test_cconv(&def_thread.o))
2307 log_err("fio: failed internal cconv test\n");
Jens Axboe0420ba62012-02-29 11:16:52 +01002308
Jens Axboe46bcd492012-03-14 11:31:21 +01002309 job_files = parse_cmd_line(argc, argv, type);
Jens Axboeebac4652005-12-08 15:25:21 +01002310
Jens Axboecdf54d82011-10-04 08:31:40 +02002311 if (job_files > 0) {
2312 for (i = 0; i < job_files; i++) {
Jens Axboebc4f5ef2014-04-06 10:10:32 -06002313 if (i && fill_def_thread())
Jens Axboe132159a2011-09-30 15:01:32 -06002314 return 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02002315 if (nr_clients) {
2316 if (fio_clients_send_ini(ini_file[i]))
2317 return 1;
2318 free(ini_file[i]);
2319 } else if (!is_backend) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002320 if (parse_jobs_ini(ini_file[i], 0, i, type))
Jens Axboecdf54d82011-10-04 08:31:40 +02002321 return 1;
2322 free(ini_file[i]);
2323 }
Jens Axboe132159a2011-09-30 15:01:32 -06002324 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02002325 } else if (nr_clients) {
2326 if (fill_def_thread())
2327 return 1;
2328 if (fio_clients_send_ini(NULL))
2329 return 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02002330 }
Jens Axboeebac4652005-12-08 15:25:21 +01002331
Jens Axboe88c6ed82006-06-09 11:28:10 +02002332 free(ini_file);
Jens Axboe7e356b22011-10-14 10:55:16 +02002333 fio_options_free(&def_thread);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08002334 filesetup_mem_free();
Jens Axboeb4692822006-10-27 13:43:22 +02002335
2336 if (!thread_number) {
Jens Axboef9633d72013-09-06 09:59:56 -06002337 if (parse_dryrun())
Jens Axboecca73aa2007-04-04 11:09:19 +02002338 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01002339 if (exec_profile)
2340 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06002341 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06002342 return 0;
Jens Axboe085399d2011-10-06 09:01:22 +02002343 if (did_arg)
2344 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02002345
Jens Axboed65e11c2011-10-05 09:56:53 +02002346 log_err("No jobs(s) defined\n\n");
Jens Axboe085399d2011-10-06 09:01:22 +02002347
2348 if (!did_arg) {
2349 usage(argv[0]);
2350 return 1;
2351 }
2352
2353 return 0;
Jens Axboeb4692822006-10-27 13:43:22 +02002354 }
2355
Jens Axboebe4ecfd2008-12-08 14:10:52 +01002356 if (def_thread.o.gtod_offload) {
2357 fio_gtod_init();
2358 fio_gtod_offload = 1;
2359 fio_gtod_cpu = def_thread.o.gtod_cpu;
2360 }
2361
Jens Axboef3afa572012-09-17 13:34:16 +02002362 if (output_format == FIO_OUTPUT_NORMAL)
Jens Axboe3d433822012-03-21 22:25:22 +01002363 log_info("%s\n", fio_version_string);
Jens Axboef6dea4d2011-10-13 13:37:07 +02002364
Jens Axboeebac4652005-12-08 15:25:21 +01002365 return 0;
2366}
Jens Axboe588b7f02012-03-19 20:37:41 +01002367
2368void options_default_fill(struct thread_options *o)
2369{
2370 memcpy(o, &def_thread.o, sizeof(*o));
2371}