blob: e944856031b9623753b0add9a003b26119caf590 [file] [log] [blame]
Jens Axboe906c8d72006-06-13 09:37:56 +02001/*
Jens Axboecb2c86f2006-10-26 13:48:34 +02002 * This file contains job initialization and setup functions.
Jens Axboe906c8d72006-06-13 09:37:56 +02003 */
Jens Axboeebac4652005-12-08 15:25:21 +01004#include <stdio.h>
5#include <stdlib.h>
6#include <unistd.h>
7#include <fcntl.h>
8#include <ctype.h>
9#include <string.h>
10#include <errno.h>
11#include <sys/ipc.h>
Jens Axboeebac4652005-12-08 15:25:21 +010012#include <sys/types.h>
13#include <sys/stat.h>
14
15#include "fio.h"
Olega5e0ee12013-03-12 00:06:53 -070016#ifndef FIO_NO_HAVE_SHM_H
17#include <sys/shm.h>
18#endif
19
Jens Axboecb2c86f2006-10-26 13:48:34 +020020#include "parse.h"
Jens Axboe2e5cdb12008-03-01 18:52:49 +010021#include "smalloc.h"
Jens Axboe380065a2008-03-01 18:56:24 +010022#include "filehash.h"
Jens Axboe4f5af7b2009-06-03 08:45:40 +020023#include "verify.h"
Jens Axboe79d16312010-03-04 12:43:20 +010024#include "profile.h"
Jens Axboe50d16972011-09-29 17:45:28 -060025#include "server.h"
Huadong Liuf2a2ce02013-01-30 13:22:24 +010026#include "idletime.h"
Jens Axboeebac4652005-12-08 15:25:21 +010027
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040028#include "lib/getopt.h"
Jens Axboee25b6d52013-04-05 14:44:51 +020029#include "lib/strcasestr.h"
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040030
Jens Axboe10aa1362014-04-01 21:10:36 -060031#include "crc/test.h"
32
Jens Axboe3d433822012-03-21 22:25:22 +010033const char fio_version_string[] = FIO_VERSION;
Jens Axboe214e1ec2007-03-15 10:48:13 +010034
Jens Axboeee738492007-01-10 11:23:16 +010035#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010036
Jens Axboe214e1ec2007-03-15 10:48:13 +010037static char **ini_file;
Jens Axboefca70352011-07-06 20:12:54 +020038static int max_jobs = FIO_MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020039static int dump_cmdline;
Jens Axboe8c029372011-10-05 09:54:30 +020040static int def_timeout;
Jens Axboe111e0322013-03-07 11:31:20 +010041static int parse_only;
Jens Axboee1f36502006-10-27 10:54:08 +020042
Jens Axboebe4ecfd2008-12-08 14:10:52 +010043static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010044struct thread_data *threads = NULL;
Jens Axboe10aa1362014-04-01 21:10:36 -060045static char **job_sections;
46static int nr_job_sections;
Jens Axboee1f36502006-10-27 10:54:08 +020047
Jens Axboe214e1ec2007-03-15 10:48:13 +010048int exitall_on_terminate = 0;
Jens Axboef3afa572012-09-17 13:34:16 +020049int output_format = FIO_OUTPUT_NORMAL;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +010050int append_terse_output = 0;
Jens Axboe5be4c942013-02-11 16:33:25 +010051int eta_print = FIO_ETA_AUTO;
Jens Axboee382e662013-02-22 20:48:56 +010052int eta_new_line = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +010053FILE *f_out = NULL;
54FILE *f_err = NULL;
Jens Axboe07b32322010-03-05 09:48:44 +010055char *exec_profile = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -070056int warnings_fatal = 0;
Jens Axboe4d658652011-10-17 15:05:47 +020057int terse_version = 3;
Jens Axboe50d16972011-09-29 17:45:28 -060058int is_backend = 0;
Jens Axboea37f69b2011-10-01 12:24:21 -060059int nr_clients = 0;
Jens Axboee46d8092011-10-03 09:11:02 +020060int log_syslog = 0;
Jens Axboeee738492007-01-10 11:23:16 +010061
Jens Axboe214e1ec2007-03-15 10:48:13 +010062int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020063int read_only = 0;
Jens Axboe06464902013-04-24 20:38:54 -060064int status_interval = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020065
Jens Axboe5ec10ea2008-03-06 15:42:00 +010066static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010067
68static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020069
Jens Axboeee56ad52008-02-01 10:30:20 +010070unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020071unsigned int fio_debug_jobno = -1;
72unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010073
Jens Axboe4c6107f2011-01-18 05:22:22 -070074static char cmd_optstr[256];
Jens Axboe085399d2011-10-06 09:01:22 +020075static int did_arg;
Jens Axboe4c6107f2011-01-18 05:22:22 -070076
Jens Axboe7a4b8242011-10-05 17:35:15 +020077#define FIO_CLIENT_FLAG (1 << 16)
78
Jens Axboeb4692822006-10-27 13:43:22 +020079/*
80 * Command line options. These will contain the above, plus a few
81 * extra that only pertain to fio itself and not jobs.
82 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010083static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020084 {
Jens Axboe08d2a192011-05-11 13:28:30 +020085 .name = (char *) "output",
Jens Axboeb4692822006-10-27 13:43:22 +020086 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020087 .val = 'o' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020088 },
89 {
Jens Axboe08d2a192011-05-11 13:28:30 +020090 .name = (char *) "timeout",
Jens Axboeb4692822006-10-27 13:43:22 +020091 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020092 .val = 't' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020093 },
94 {
Jens Axboe08d2a192011-05-11 13:28:30 +020095 .name = (char *) "latency-log",
Jens Axboeb4692822006-10-27 13:43:22 +020096 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020097 .val = 'l' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020098 },
99 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200100 .name = (char *) "bandwidth-log",
Jens Axboeb4692822006-10-27 13:43:22 +0200101 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200102 .val = 'b' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200103 },
104 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200105 .name = (char *) "minimal",
Stefan Hajnoczic4ec0a12014-02-04 14:27:10 +0100106 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200107 .val = 'm' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200108 },
109 {
Jens Axboef3afa572012-09-17 13:34:16 +0200110 .name = (char *) "output-format",
Shaohua Licc372b12012-09-17 09:12:18 +0200111 .has_arg = optional_argument,
Jens Axboef3afa572012-09-17 13:34:16 +0200112 .val = 'F' | FIO_CLIENT_FLAG,
Shaohua Licc372b12012-09-17 09:12:18 +0200113 },
114 {
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +0100115 .name = (char *) "append-terse",
116 .has_arg = optional_argument,
117 .val = 'f',
118 },
119 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200120 .name = (char *) "version",
Jens Axboeb4692822006-10-27 13:43:22 +0200121 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200122 .val = 'v' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200123 },
124 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200125 .name = (char *) "help",
Jens Axboefd28ca42007-01-09 21:20:13 +0100126 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200127 .val = 'h' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100128 },
129 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200130 .name = (char *) "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100131 .has_arg = optional_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200132 .val = 'c' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100133 },
134 {
Jens Axboe06464902013-04-24 20:38:54 -0600135 .name = (char *) "enghelp",
Steven Langde890a12011-11-09 14:03:34 +0100136 .has_arg = optional_argument,
Jens Axboe06464902013-04-24 20:38:54 -0600137 .val = 'i' | FIO_CLIENT_FLAG,
Steven Langde890a12011-11-09 14:03:34 +0100138 },
139 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200140 .name = (char *) "showcmd",
Jens Axboecca73aa2007-04-04 11:09:19 +0200141 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200142 .val = 's' | FIO_CLIENT_FLAG,
Jens Axboe724e4432007-09-11 20:02:05 +0200143 },
144 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200145 .name = (char *) "readonly",
Jens Axboe724e4432007-09-11 20:02:05 +0200146 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200147 .val = 'r' | FIO_CLIENT_FLAG,
Jens Axboecca73aa2007-04-04 11:09:19 +0200148 },
149 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200150 .name = (char *) "eta",
Aaron Carrolle592a062007-09-14 09:49:41 +0200151 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200152 .val = 'e' | FIO_CLIENT_FLAG,
Aaron Carrolle592a062007-09-14 09:49:41 +0200153 },
154 {
Jens Axboee382e662013-02-22 20:48:56 +0100155 .name = (char *) "eta-newline",
156 .has_arg = required_argument,
157 .val = 'E' | FIO_CLIENT_FLAG,
158 },
159 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200160 .name = (char *) "debug",
Jens Axboeee56ad52008-02-01 10:30:20 +0100161 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200162 .val = 'd' | FIO_CLIENT_FLAG,
Jens Axboeee56ad52008-02-01 10:30:20 +0100163 },
164 {
Jens Axboe111e0322013-03-07 11:31:20 +0100165 .name = (char *) "parse-only",
166 .has_arg = no_argument,
167 .val = 'P' | FIO_CLIENT_FLAG,
168 },
169 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200170 .name = (char *) "section",
Jens Axboe01f06b62008-02-18 20:53:47 +0100171 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200172 .val = 'x' | FIO_CLIENT_FLAG,
Jens Axboe01f06b62008-02-18 20:53:47 +0100173 },
174 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200175 .name = (char *) "alloc-size",
Jens Axboe2b386d22008-03-26 10:32:57 +0100176 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200177 .val = 'a' | FIO_CLIENT_FLAG,
Jens Axboe2b386d22008-03-26 10:32:57 +0100178 },
179 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200180 .name = (char *) "profile",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100181 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200182 .val = 'p' | FIO_CLIENT_FLAG,
Jens Axboe9ac8a792009-11-13 21:23:07 +0100183 },
184 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200185 .name = (char *) "warnings-fatal",
Jens Axboea9523c62011-01-17 16:49:54 -0700186 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200187 .val = 'w' | FIO_CLIENT_FLAG,
Jens Axboea9523c62011-01-17 16:49:54 -0700188 },
189 {
Jens Axboefca70352011-07-06 20:12:54 +0200190 .name = (char *) "max-jobs",
191 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200192 .val = 'j' | FIO_CLIENT_FLAG,
Jens Axboefca70352011-07-06 20:12:54 +0200193 },
194 {
Jens Axboef57a9c52011-09-09 21:01:37 +0200195 .name = (char *) "terse-version",
196 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200197 .val = 'V' | FIO_CLIENT_FLAG,
Jens Axboef57a9c52011-09-09 21:01:37 +0200198 },
199 {
Jens Axboe50d16972011-09-29 17:45:28 -0600200 .name = (char *) "server",
Jens Axboe87aa8f12011-10-06 21:24:13 +0200201 .has_arg = optional_argument,
Jens Axboe50d16972011-09-29 17:45:28 -0600202 .val = 'S',
203 },
Jens Axboee46d8092011-10-03 09:11:02 +0200204 { .name = (char *) "daemonize",
Jens Axboe402668f2011-10-10 15:28:58 +0200205 .has_arg = required_argument,
Jens Axboee46d8092011-10-03 09:11:02 +0200206 .val = 'D',
207 },
Jens Axboe50d16972011-09-29 17:45:28 -0600208 {
Jens Axboe132159a2011-09-30 15:01:32 -0600209 .name = (char *) "client",
210 .has_arg = required_argument,
211 .val = 'C',
212 },
213 {
Jens Axboe7d11f872012-12-17 12:03:29 +0100214 .name = (char *) "cpuclock-test",
215 .has_arg = no_argument,
216 .val = 'T',
217 },
218 {
Jens Axboefec0f212014-02-07 14:39:33 -0700219 .name = (char *) "crctest",
220 .has_arg = optional_argument,
221 .val = 'G',
222 },
223 {
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100224 .name = (char *) "idle-prof",
225 .has_arg = required_argument,
226 .val = 'I',
227 },
228 {
Jens Axboe06464902013-04-24 20:38:54 -0600229 .name = (char *) "status-interval",
230 .has_arg = required_argument,
231 .val = 'L',
232 },
233 {
Jens Axboeb4692822006-10-27 13:43:22 +0200234 .name = NULL,
235 },
236};
237
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200238void free_threads_shm(void)
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200239{
240 struct shmid_ds sbuf;
241
242 if (threads) {
243 void *tp = threads;
244
245 threads = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200246 shmdt(tp);
247 shmctl(shm_id, IPC_RMID, &sbuf);
248 shm_id = -1;
249 }
250}
251
Jens Axboe10aa1362014-04-01 21:10:36 -0600252static void free_shm(void)
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200253{
254 if (threads) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200255 file_hash_exit();
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100256 flow_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200257 fio_debug_jobp = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200258 free_threads_shm();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200259 }
260
Jens Axboeeb0c74a2014-02-11 10:33:06 -0700261 options_free(fio_options, &def_thread);
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200262 scleanup();
263}
264
265/*
266 * The thread area is shared between the main process and the job
267 * threads/processes. So setup a shared memory segment that will hold
268 * all the job info. We use the end of the region for keeping track of
269 * open files across jobs, for file sharing.
270 */
271static int setup_thread_area(void)
272{
273 void *hash;
274
275 if (threads)
276 return 0;
277
278 /*
279 * 1024 is too much on some machines, scale max_jobs if
280 * we get a failure that looks like too large a shm segment
281 */
282 do {
283 size_t size = max_jobs * sizeof(struct thread_data);
284
285 size += file_hash_size;
286 size += sizeof(unsigned int);
287
288 shm_id = shmget(0, size, IPC_CREAT | 0600);
289 if (shm_id != -1)
290 break;
Jens Axboef0c77f02012-09-21 15:05:45 +0200291 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200292 perror("shmget");
293 break;
294 }
295
296 max_jobs >>= 1;
297 } while (max_jobs);
298
299 if (shm_id == -1)
300 return 1;
301
302 threads = shmat(shm_id, NULL, 0);
303 if (threads == (void *) -1) {
304 perror("shmat");
305 return 1;
306 }
307
308 memset(threads, 0, max_jobs * sizeof(struct thread_data));
309 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
310 fio_debug_jobp = (void *) hash + file_hash_size;
311 *fio_debug_jobp = -1;
312 file_hash_init(hash);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100313
314 flow_init();
315
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200316 return 0;
317}
318
Jens Axboe906c8d72006-06-13 09:37:56 +0200319/*
320 * Return a free job structure.
321 */
Steven Langde890a12011-11-09 14:03:34 +0100322static struct thread_data *get_new_job(int global, struct thread_data *parent,
323 int preserve_eo)
Jens Axboeebac4652005-12-08 15:25:21 +0100324{
325 struct thread_data *td;
326
327 if (global)
328 return &def_thread;
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200329 if (setup_thread_area()) {
330 log_err("error: failed to setup shm segment\n");
331 return NULL;
332 }
Bruce Crane61f1ec2011-01-14 18:30:26 +0100333 if (thread_number >= max_jobs) {
334 log_err("error: maximum number of jobs (%d) reached.\n",
335 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100336 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100337 }
Jens Axboeebac4652005-12-08 15:25:21 +0100338
339 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200340 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100341
Steven Langde890a12011-11-09 14:03:34 +0100342 td->io_ops = NULL;
343 if (!preserve_eo)
344 td->eo = NULL;
345
Jens Axboee0b0d892009-12-08 10:10:14 +0100346 td->o.uid = td->o.gid = -1U;
347
Jens Axboecade3ef2007-03-23 15:29:45 +0100348 dup_files(td, parent);
Steven Langde890a12011-11-09 14:03:34 +0100349 fio_options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100350
Jens Axboe15dc1932010-03-05 10:59:06 +0100351 profile_add_hooks(td);
352
Jens Axboeebac4652005-12-08 15:25:21 +0100353 td->thread_number = thread_number;
Jens Axboe108fea72012-11-14 13:09:45 -0700354
355 if (!parent || !parent->o.group_reporting)
356 stat_number++;
357
Jens Axboeebac4652005-12-08 15:25:21 +0100358 return td;
359}
360
361static void put_job(struct thread_data *td)
362{
Jens Axboe549577a2006-10-30 12:23:41 +0100363 if (td == &def_thread)
364 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600365
Jens Axboe58c55ba2010-03-09 12:20:08 +0100366 profile_td_exit(td);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100367 flow_exit_job(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100368
Jens Axboe16edf252007-02-10 14:59:22 +0100369 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100370 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100371
Jens Axboe7e356b22011-10-14 10:55:16 +0200372 fio_options_free(td);
Steven Langde890a12011-11-09 14:03:34 +0100373 if (td->io_ops)
374 free_ioengine(td);
Jens Axboe7e356b22011-10-14 10:55:16 +0200375
Jens Axboeebac4652005-12-08 15:25:21 +0100376 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
377 thread_number--;
378}
379
Jens Axboe581e7142009-06-09 12:47:16 +0200380static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100381{
Jens Axboe581e7142009-06-09 12:47:16 +0200382 unsigned int bs = td->o.min_bs[ddir];
Jens Axboe127f6862007-03-15 12:09:57 +0100383
Jens Axboeff58fce2010-08-25 12:02:08 +0200384 assert(ddir_rw(ddir));
385
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100386 if (td->o.rate[ddir])
Steven Lang1b8dbf22011-11-09 13:48:01 +0100387 td->rate_bps[ddir] = td->o.rate[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100388 else
Steven Lang1b8dbf22011-11-09 13:48:01 +0100389 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100390
Steven Lang1b8dbf22011-11-09 13:48:01 +0100391 if (!td->rate_bps[ddir]) {
Jens Axboe127f6862007-03-15 12:09:57 +0100392 log_err("rate lower than supported\n");
393 return -1;
394 }
395
Jens Axboe581e7142009-06-09 12:47:16 +0200396 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100397 return 0;
398}
399
Jens Axboe581e7142009-06-09 12:47:16 +0200400static int setup_rate(struct thread_data *td)
401{
402 int ret = 0;
403
404 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
405 ret = __setup_rate(td, DDIR_READ);
406 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
407 ret |= __setup_rate(td, DDIR_WRITE);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200408 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
409 ret |= __setup_rate(td, DDIR_TRIM);
Jens Axboe581e7142009-06-09 12:47:16 +0200410
411 return ret;
412}
413
Jens Axboe83472392009-02-19 21:32:12 +0100414static int fixed_block_size(struct thread_options *o)
415{
416 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
417 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200418 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
419 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
420 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
Jens Axboe83472392009-02-19 21:32:12 +0100421}
422
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800423
424static unsigned long long get_rand_start_delay(struct thread_data *td)
425{
426 unsigned long long delayrange;
427 unsigned long r;
428
429 delayrange = td->o.start_delay_high - td->o.start_delay;
430
431 if (td->o.use_os_rand) {
432 r = os_random_long(&td->delay_state);
433 delayrange = (unsigned long long) ((double) delayrange * (r / (OS_RAND_MAX + 1.0)));
434 } else {
435 r = __rand(&td->__delay_state);
436 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
437 }
438
439 delayrange += td->o.start_delay;
440 return delayrange;
441}
442
Jens Axboedad915e2006-10-27 11:10:18 +0200443/*
444 * Lazy way of fixing up options that depend on each other. We could also
445 * define option callback handlers, but this is easier.
446 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100447static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200448{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100449 struct thread_options *o = &td->o;
Jens Axboeeefd98b2013-07-25 12:30:06 -0600450 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200451
Jens Axboef356d012009-01-05 09:56:29 +0100452#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100453 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100454 log_info("fio: this platform does not support process shared"
455 " mutexes, forcing use of threads. Use the 'thread'"
456 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100457 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700458 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100459 }
460#endif
461
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100462 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200463 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100464 free(o->write_iolog_file);
465 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700466 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200467 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100468
Jens Axboe16b462a2006-10-30 12:35:18 +0100469 /*
Steven Noonaned335852012-01-31 13:58:00 +0100470 * only really works with 1 file
Jens Axboe16b462a2006-10-30 12:35:18 +0100471 */
Jens Axboe627aa1a2013-02-07 13:06:18 +0100472 if (o->zone_size && o->open_files > 1)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100473 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100474
475 /*
Steven Noonaned335852012-01-31 13:58:00 +0100476 * If zone_range isn't specified, backward compatibility dictates it
477 * should be made equal to zone_size.
478 */
479 if (o->zone_size && !o->zone_range)
480 o->zone_range = o->zone_size;
481
482 /*
Jens Axboe16b462a2006-10-30 12:35:18 +0100483 * Reads can do overwrites, we always need to pre-create the file
484 */
485 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100486 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100487
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100488 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100489 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100490 if (!o->max_bs[DDIR_READ])
491 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
492 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100493 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100494 if (!o->max_bs[DDIR_WRITE])
495 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200496 if (!o->min_bs[DDIR_TRIM])
497 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
498 if (!o->max_bs[DDIR_TRIM])
499 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
500
Jens Axboea00735e2006-11-03 08:58:08 +0100501
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100502 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200503 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
Jens Axboea00735e2006-11-03 08:58:08 +0100504
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100505 /*
506 * For random IO, allow blockalign offset other than min_bs.
507 */
508 if (!o->ba[DDIR_READ] || !td_random(td))
509 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
510 if (!o->ba[DDIR_WRITE] || !td_random(td))
511 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200512 if (!o->ba[DDIR_TRIM] || !td_random(td))
513 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100514
515 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200516 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
517 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100518 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100519 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100520 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700521 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100522 }
523
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100524 if (!o->file_size_high)
525 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100526
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800527 if (o->start_delay_high)
528 o->start_delay = get_rand_start_delay(td);
529
Jens Axboe83472392009-02-19 21:32:12 +0100530 if (o->norandommap && o->verify != VERIFY_NONE
531 && !fixed_block_size(o)) {
532 log_err("fio: norandommap given for variable block sizes, "
533 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100534 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700535 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100536 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100537 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100538 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100539
540 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100541 * thinktime_spin must be less than thinktime
542 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100543 if (o->thinktime_spin > o->thinktime)
544 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100545
546 /*
547 * The low water mark cannot be bigger than the iodepth
548 */
Jens Axboe67bf9822013-01-10 11:23:19 +0100549 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
550 o->iodepth_low = o->iodepth;
Jens Axboecb5ab512007-02-26 12:57:09 +0100551
552 /*
553 * If batch number isn't set, default to the same as iodepth
554 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100555 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
556 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100557
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100558 if (o->nr_files > td->files_index)
559 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100560
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100561 if (o->open_files > o->nr_files || !o->open_files)
562 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100563
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200564 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
565 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
566 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
567 (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 +0100568 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700569 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100570 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200571 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
572 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
573 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
574 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
575 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
576 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100577 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700578 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100579 }
580
Jens Axboecf4464c2007-04-17 20:14:42 +0200581 if (!o->timeout && o->time_based) {
582 log_err("fio: time_based requires a runtime/timeout setting\n");
583 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700584 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200585 }
586
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100587 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200588 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100589
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100590 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700591 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700592 log_info("Multiple writers may overwrite blocks that "
593 "belong to other jobs. This can cause "
594 "verification failures.\n");
595 ret = warnings_fatal;
596 }
597
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100598 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200599 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
600 !o->verify_interval)
601 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboed9905882010-03-18 20:43:00 +0100602 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200603
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100604 if (o->pre_read) {
605 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700606 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200607 log_info("fio: cannot pre-read files with an IO engine"
608 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700609 ret = warnings_fatal;
610 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200611 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200612
Steven Noonanad705bc2013-04-08 15:05:25 -0700613 if (!o->unit_base) {
614 if (td->io_ops->flags & FIO_BIT_BASED)
615 o->unit_base = 1;
616 else
617 o->unit_base = 8;
618 }
619
Jens Axboe67bf9822013-01-10 11:23:19 +0100620#ifndef CONFIG_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100621 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700622 log_info("fio: this platform does not support fdatasync()"
623 " falling back to using fsync(). Use the 'fsync'"
624 " option instead of 'fdatasync' to get rid of"
625 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100626 o->fsync_blocks = o->fdatasync_blocks;
627 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700628 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700629 }
630#endif
631
Bruce Cran93bcfd22012-02-20 20:18:19 +0100632#ifdef WIN32
633 /*
634 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
635 * so fail if we're passed those flags
636 */
637 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
638 log_err("fio: Windows does not support direct or non-buffered io with"
639 " the synchronous ioengines. Use the 'windowsaio' ioengine"
640 " with 'direct=1' and 'iodepth=1' instead.\n");
641 ret = 1;
642 }
643#endif
644
Jens Axboe811ac502012-03-02 22:23:36 +0100645 /*
646 * For fully compressible data, just zero them at init time.
647 * It's faster than repeatedly filling it.
648 */
649 if (td->o.compress_percentage == 100) {
650 td->o.zero_buffers = 1;
651 td->o.compress_percentage = 0;
652 }
653
Jens Axboe5881fda2012-11-15 15:21:23 -0700654 /*
655 * Using a non-uniform random distribution excludes usage of
656 * a random map
657 */
658 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
659 td->o.norandommap = 1;
660
Jens Axboe8d916c92013-04-25 10:11:41 -0600661 /*
662 * If size is set but less than the min block size, complain
663 */
664 if (o->size && o->size < td_min_bs(td)) {
665 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
666 ret = 1;
667 }
668
Chris Masond01612f2013-11-15 15:52:58 -0700669 /*
670 * O_ATOMIC implies O_DIRECT
671 */
672 if (td->o.oatomic)
673 td->o.odirect = 1;
674
Jens Axboe04778ba2014-01-10 20:57:01 -0700675 /*
676 * If randseed is set, that overrides randrepeat
677 */
678 if (td->o.rand_seed)
679 td->o.rand_repeatable = 0;
680
Jens Axboeb1bebc32014-03-18 13:30:32 -0600681 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
682 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
683 ret = 1;
684 }
685
Jens Axboea9523c62011-01-17 16:49:54 -0700686 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200687}
688
Jens Axboe906c8d72006-06-13 09:37:56 +0200689/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100690 * This function leaks the buffer
691 */
Jens Axboe99d633a2012-03-15 15:55:04 +0100692char *fio_uint_to_kmg(unsigned int val)
Jens Axboef8977ee2006-11-06 14:03:03 +0100693{
694 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100695 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100696 char *p = post;
697
Jens Axboe245142f2006-11-08 12:49:21 +0100698 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100699 if (val & 1023)
700 break;
701
702 val >>= 10;
703 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100704 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100705
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100706 snprintf(buf, 32, "%u%c", val, *p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100707 return buf;
708}
709
Jens Axboe09629a92007-03-09 09:00:06 +0100710/* External engines are specified by "external:name.o") */
711static const char *get_engine_name(const char *str)
712{
713 char *p = strstr(str, ":");
714
715 if (!p)
716 return str;
717
718 p++;
719 strip_blank_front(&p);
720 strip_blank_end(p);
721 return p;
722}
723
Jens Axboee132cba2007-03-14 14:23:54 +0100724static int exists_and_not_file(const char *filename)
725{
726 struct stat sb;
727
728 if (lstat(filename, &sb) == -1)
729 return 0;
730
Bruce Cranecc314b2011-01-04 10:59:30 +0100731 /* \\.\ is the device namespace in Windows, where every file
732 * is a device node */
733 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100734 return 0;
735
736 return 1;
737}
738
Jens Axboe4c07ad82011-03-28 09:51:09 +0200739static void td_fill_rand_seeds_os(struct thread_data *td)
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200740{
Jens Axboed24945f2012-12-04 13:10:29 +0100741 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
742 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
743 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200744
745 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100746 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200747
Jens Axboed24945f2012-12-04 13:10:29 +0100748 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
749 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800750 os_random_seed(td->rand_seeds[FIO_RAND_START_DELAY], &td->delay_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200751
752 if (!td_random(td))
753 return;
754
755 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100756 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200757
Jens Axboed24945f2012-12-04 13:10:29 +0100758 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
Jens Axboed9472272013-07-25 10:20:45 -0600759
760 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], &td->seq_rand_state[DDIR_READ]);
761 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], &td->seq_rand_state[DDIR_WRITE]);
762 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_TRIM_OFF], &td->seq_rand_state[DDIR_TRIM]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200763}
764
765static void td_fill_rand_seeds_internal(struct thread_data *td)
766{
Jens Axboed24945f2012-12-04 13:10:29 +0100767 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
768 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
769 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200770
771 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100772 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200773
Jens Axboed24945f2012-12-04 13:10:29 +0100774 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
775 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800776 init_rand_seed(&td->__delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200777
778 if (!td_random(td))
779 return;
780
781 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100782 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe4c07ad82011-03-28 09:51:09 +0200783
Jens Axboed24945f2012-12-04 13:10:29 +0100784 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
Jens Axboed9472272013-07-25 10:20:45 -0600785 init_rand_seed(&td->__seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
786 init_rand_seed(&td->__seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
787 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 +0200788}
789
Jens Axboe4c07ad82011-03-28 09:51:09 +0200790void td_fill_rand_seeds(struct thread_data *td)
791{
Christian Ehrhardt56e2a5f2014-02-20 09:10:17 -0800792 if (td->o.allrand_repeatable) {
793 for (int i = 0; i < FIO_RAND_NR_OFFS; i++)
794 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
795 + i;
796 }
797
Jens Axboe4c07ad82011-03-28 09:51:09 +0200798 if (td->o.use_os_rand)
799 td_fill_rand_seeds_os(td);
800 else
801 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600802
Jens Axboed24945f2012-12-04 13:10:29 +0100803 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200804}
805
Jens Axboe9c60ce62007-03-15 09:14:47 +0100806/*
Steven Langde890a12011-11-09 14:03:34 +0100807 * Initializes the ioengine configured for a job, if it has not been done so
808 * already.
809 */
810int ioengine_load(struct thread_data *td)
811{
812 const char *engine;
813
814 /*
815 * Engine has already been loaded.
816 */
817 if (td->io_ops)
818 return 0;
Jens Axboe6e5c4b82013-05-27 09:50:31 +0200819 if (!td->o.ioengine) {
820 log_err("fio: internal fault, no IO engine specified\n");
821 return 1;
822 }
Steven Langde890a12011-11-09 14:03:34 +0100823
824 engine = get_engine_name(td->o.ioengine);
825 td->io_ops = load_ioengine(td, engine);
826 if (!td->io_ops) {
827 log_err("fio: failed to load engine %s\n", engine);
828 return 1;
829 }
830
831 if (td->io_ops->option_struct_size && td->io_ops->options) {
832 /*
833 * In cases where td->eo is set, clone it for a child thread.
834 * This requires that the parent thread has the same ioengine,
835 * but that requirement must be enforced by the code which
836 * cloned the thread.
837 */
838 void *origeo = td->eo;
839 /*
840 * Otherwise use the default thread options.
841 */
842 if (!origeo && td != &def_thread && def_thread.eo &&
843 def_thread.io_ops->options == td->io_ops->options)
844 origeo = def_thread.eo;
845
846 options_init(td->io_ops->options);
847 td->eo = malloc(td->io_ops->option_struct_size);
848 /*
849 * Use the default thread as an option template if this uses the
850 * same options structure and there are non-default options
851 * used.
852 */
853 if (origeo) {
854 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
855 options_mem_dupe(td->eo, td->io_ops->options);
856 } else {
857 memset(td->eo, 0, td->io_ops->option_struct_size);
858 fill_default_options(td->eo, td->io_ops->options);
859 }
860 *(struct thread_data **)td->eo = td;
861 }
862
863 return 0;
864}
865
Jens Axboed72be542012-11-30 19:37:46 +0100866static void init_flags(struct thread_data *td)
867{
868 struct thread_options *o = &td->o;
869
870 if (o->verify_backlog)
871 td->flags |= TD_F_VER_BACKLOG;
872 if (o->trim_backlog)
873 td->flags |= TD_F_TRIM_BACKLOG;
874 if (o->read_iolog_file)
875 td->flags |= TD_F_READ_IOLOG;
876 if (o->refill_buffers)
877 td->flags |= TD_F_REFILL_BUFFERS;
Jens Axboebedc9dc2014-03-17 12:51:09 -0600878 if (o->scramble_buffers)
Jens Axboed72be542012-11-30 19:37:46 +0100879 td->flags |= TD_F_SCRAMBLE_BUFFERS;
880 if (o->verify != VERIFY_NONE)
881 td->flags |= TD_F_VER_NONE;
882}
883
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700884static int setup_random_seeds(struct thread_data *td)
885{
886 unsigned long seed;
887 unsigned int i;
888
Jens Axboe04778ba2014-01-10 20:57:01 -0700889 if (!td->o.rand_repeatable && !td->o.rand_seed)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700890 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
891
Jens Axboe04778ba2014-01-10 20:57:01 -0700892 if (!td->o.rand_seed)
893 seed = 0x89;
894 else
895 seed = td->o.rand_seed;
896
897 for (i = 0; i < 4; i++)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700898 seed *= 0x9e370001UL;
899
900 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
901 td->rand_seeds[i] = seed;
902 seed *= 0x9e370001UL;
903 }
904
905 td_fill_rand_seeds(td);
906 return 0;
907}
908
Jens Axboede98bd32013-04-05 11:09:20 +0200909enum {
910 FPRE_NONE = 0,
911 FPRE_JOBNAME,
912 FPRE_JOBNUM,
913 FPRE_FILENUM
914};
915
916static struct fpre_keyword {
917 const char *keyword;
918 size_t strlen;
919 int key;
920} fpre_keywords[] = {
921 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
922 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
923 { .keyword = "$filenum", .key = FPRE_FILENUM, },
924 { .keyword = NULL, },
925 };
926
927static char *make_filename(char *buf, struct thread_options *o,
928 const char *jobname, int jobnum, int filenum)
929{
930 struct fpre_keyword *f;
931 char copy[PATH_MAX];
932
933 if (!o->filename_format || !strlen(o->filename_format)) {
934 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
935 return NULL;
936 }
937
938 for (f = &fpre_keywords[0]; f->keyword; f++)
939 f->strlen = strlen(f->keyword);
940
941 strcpy(buf, o->filename_format);
942 memset(copy, 0, sizeof(copy));
943 for (f = &fpre_keywords[0]; f->keyword; f++) {
944 do {
945 size_t pre_len, post_start = 0;
946 char *str, *dst = copy;
947
Jens Axboee25b6d52013-04-05 14:44:51 +0200948 str = strcasestr(buf, f->keyword);
Jens Axboede98bd32013-04-05 11:09:20 +0200949 if (!str)
950 break;
951
952 pre_len = str - buf;
953 if (strlen(str) != f->strlen)
954 post_start = pre_len + f->strlen;
955
956 if (pre_len) {
957 strncpy(dst, buf, pre_len);
958 dst += pre_len;
959 }
960
961 switch (f->key) {
962 case FPRE_JOBNAME:
963 dst += sprintf(dst, "%s", jobname);
964 break;
965 case FPRE_JOBNUM:
966 dst += sprintf(dst, "%d", jobnum);
967 break;
968 case FPRE_FILENUM:
969 dst += sprintf(dst, "%d", filenum);
970 break;
971 default:
972 assert(0);
973 break;
974 }
975
976 if (post_start)
977 strcpy(dst, buf + post_start);
978
979 strcpy(buf, copy);
980 } while (1);
981 }
982
983 return buf;
984}
Jens Axboef9633d72013-09-06 09:59:56 -0600985
986int parse_dryrun(void)
987{
988 return dump_cmdline || parse_only;
989}
990
Steven Langde890a12011-11-09 14:03:34 +0100991/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200992 * Adds a job to the list of things todo. Sanitizes the various options
993 * to make sure we don't have conflicts, and initializes various
994 * members of td.
995 */
Jens Axboeb7cfb2e2012-03-12 07:47:27 +0100996static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
Jens Axboe46bcd492012-03-14 11:31:21 +0100997 int recursed, int client_type)
Jens Axboeebac4652005-12-08 15:25:21 +0100998{
Jens Axboeaf52b342007-03-13 10:07:47 +0100999 unsigned int i;
Jens Axboeaf52b342007-03-13 10:07:47 +01001000 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +01001001 int numjobs, file_alloced;
Jens Axboede98bd32013-04-05 11:09:20 +02001002 struct thread_options *o = &td->o;
Jens Axboeebac4652005-12-08 15:25:21 +01001003
Jens Axboeebac4652005-12-08 15:25:21 +01001004 /*
1005 * the def_thread is just for options, it's not a real job
1006 */
1007 if (td == &def_thread)
1008 return 0;
1009
Jens Axboed72be542012-11-30 19:37:46 +01001010 init_flags(td);
1011
Jens Axboecca73aa2007-04-04 11:09:19 +02001012 /*
1013 * if we are just dumping the output command line, don't add the job
1014 */
Jens Axboef9633d72013-09-06 09:59:56 -06001015 if (parse_dryrun()) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001016 put_job(td);
1017 return 0;
1018 }
1019
Jens Axboe46bcd492012-03-14 11:31:21 +01001020 td->client_type = client_type;
1021
Jens Axboe58c55ba2010-03-09 12:20:08 +01001022 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +02001023 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +01001024
Steven Langde890a12011-11-09 14:03:34 +01001025 if (ioengine_load(td))
Jens Axboe205927a2007-03-15 11:06:32 +01001026 goto err;
Jens Axboedf641192006-10-12 07:55:41 +02001027
Jens Axboede98bd32013-04-05 11:09:20 +02001028 if (o->odirect)
Jens Axboe690adba2006-10-30 15:25:09 +01001029 td->io_ops->flags |= FIO_RAWIO;
1030
Jens Axboee132cba2007-03-14 14:23:54 +01001031 file_alloced = 0;
Jens Axboede98bd32013-04-05 11:09:20 +02001032 if (!o->filename && !td->files_index && !o->read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +01001033 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +01001034
Jens Axboede98bd32013-04-05 11:09:20 +02001035 if (o->nr_files == 1 && exists_and_not_file(jobname))
Jens Axboe5903e7b2014-02-26 13:42:13 -08001036 add_file(td, jobname, job_add_num, 0);
Jens Axboe7b05a212007-03-13 11:17:07 +01001037 else {
Jens Axboede98bd32013-04-05 11:09:20 +02001038 for (i = 0; i < o->nr_files; i++)
Jens Axboe5903e7b2014-02-26 13:42:13 -08001039 add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
Jens Axboeaf52b342007-03-13 10:07:47 +01001040 }
Jens Axboe0af7b542006-02-17 10:10:12 +01001041 }
Jens Axboeebac4652005-12-08 15:25:21 +01001042
Jens Axboe4e991c22007-03-15 11:41:11 +01001043 if (fixup_options(td))
1044 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +01001045
Dan Ehrenberg9e684a42012-02-20 11:05:14 +01001046 flow_init_job(td);
1047
Steven Langde890a12011-11-09 14:03:34 +01001048 /*
1049 * IO engines only need this for option callbacks, and the address may
1050 * change in subprocesses.
1051 */
1052 if (td->eo)
1053 *(struct thread_data **)td->eo = NULL;
1054
Jens Axboe07eb79d2007-04-12 13:02:38 +02001055 if (td->io_ops->flags & FIO_DISKLESSIO) {
1056 struct fio_file *f;
1057
1058 for_each_file(td, f, i)
1059 f->real_file_size = -1ULL;
1060 }
1061
Jens Axboe521da522012-08-02 11:21:36 +02001062 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboeebac4652005-12-08 15:25:21 +01001063
Jens Axboede98bd32013-04-05 11:09:20 +02001064 td->ts.clat_percentiles = o->clat_percentiles;
1065 td->ts.percentile_precision = o->percentile_precision;
1066 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
Yu-ju Hong83349192011-08-13 00:53:44 +02001067
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001068 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1069 td->ts.clat_stat[i].min_val = ULONG_MAX;
1070 td->ts.slat_stat[i].min_val = ULONG_MAX;
1071 td->ts.lat_stat[i].min_val = ULONG_MAX;
1072 td->ts.bw_stat[i].min_val = ULONG_MAX;
1073 }
Jens Axboede98bd32013-04-05 11:09:20 +02001074 td->ddir_seq_nr = o->ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +01001075
Jens Axboede98bd32013-04-05 11:09:20 +02001076 if ((o->stonewall || o->new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001077 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001078 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001079 }
Jens Axboeebac4652005-12-08 15:25:21 +01001080
1081 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001082 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +01001083
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001084 if (setup_random_seeds(td)) {
Bruce Cran93bcfd22012-02-20 20:18:19 +01001085 td_verror(td, errno, "init_random_state");
Jens Axboe9c60ce62007-03-15 09:14:47 +01001086 goto err;
Bruce Cran93bcfd22012-02-20 20:18:19 +01001087 }
Jens Axboe9c60ce62007-03-15 09:14:47 +01001088
Jens Axboeebac4652005-12-08 15:25:21 +01001089 if (setup_rate(td))
1090 goto err;
1091
Jens Axboe8e255772014-04-01 13:34:49 -06001092 if (o->lat_log_file || write_lat_log) {
Jens Axboe22f80452013-04-09 20:29:16 +02001093 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
1094 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
1095 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
Jens Axboeebac4652005-12-08 15:25:21 +01001096 }
Jens Axboe8e255772014-04-01 13:34:49 -06001097 if (o->bw_log_file || write_bw_log)
Jens Axboe22f80452013-04-09 20:29:16 +02001098 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1099 if (o->iops_log_file)
1100 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
Jens Axboeebac4652005-12-08 15:25:21 +01001101
Jens Axboede98bd32013-04-05 11:09:20 +02001102 if (!o->name)
1103 o->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +02001104
Jens Axboef3afa572012-09-17 13:34:16 +02001105 if (output_format == FIO_OUTPUT_NORMAL) {
Jens Axboeb990b5c2006-09-14 09:48:22 +02001106 if (!job_add_num) {
Jens Axboeb7cfb2e2012-03-12 07:47:27 +01001107 if (is_backend && !recursed)
Jens Axboe2f122b12012-03-15 13:10:19 +01001108 fio_server_send_add_job(td);
Jens Axboe807f9972012-03-02 10:25:24 +01001109
Jens Axboe03530502012-03-19 21:45:12 +01001110 if (!(td->io_ops->flags & FIO_NOIO)) {
Jens Axboed21e2532013-07-25 13:01:11 -06001111 char *c1, *c2, *c3, *c4;
1112 char *c5 = NULL, *c6 = NULL;
Jens Axboef8977ee2006-11-06 14:03:03 +01001113
Jens Axboe22f80452013-04-09 20:29:16 +02001114 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1115 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1116 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1117 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +01001118
Jens Axboed21e2532013-07-25 13:01:11 -06001119 if (!o->bs_is_seq_rand) {
1120 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1121 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1122 }
1123
1124 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1125 td->groupid,
1126 ddir_str(o->td_ddir));
1127
1128 if (o->bs_is_seq_rand)
1129 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1130 c1, c2, c3, c4);
1131 else
1132 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1133 c1, c2, c3, c4, c5, c6);
1134
1135 log_info("ioengine=%s, iodepth=%u\n",
Jens Axboede98bd32013-04-05 11:09:20 +02001136 td->io_ops->name, o->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +01001137
1138 free(c1);
1139 free(c2);
1140 free(c3);
1141 free(c4);
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001142 free(c5);
1143 free(c6);
Jens Axboef8977ee2006-11-06 14:03:03 +01001144 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001145 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +01001146 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001147 }
Jens Axboeebac4652005-12-08 15:25:21 +01001148
1149 /*
1150 * recurse add identical jobs, clear numjobs and stonewall options
1151 * as they don't apply to sub-jobs
1152 */
Jens Axboede98bd32013-04-05 11:09:20 +02001153 numjobs = o->numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +01001154 while (--numjobs) {
Steven Langde890a12011-11-09 14:03:34 +01001155 struct thread_data *td_new = get_new_job(0, td, 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001156
1157 if (!td_new)
1158 goto err;
1159
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001160 td_new->o.numjobs = 1;
1161 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +02001162 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +01001163
1164 if (file_alloced) {
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001165 if (td_new->files) {
1166 struct fio_file *f;
1167 for_each_file(td_new, f, i) {
1168 if (f->file_name)
Andrey Kuzminece6d642014-04-01 12:27:04 -06001169 sfree(f->file_name);
1170 sfree(f);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001171 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001172 free(td_new->files);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001173 td_new->files = NULL;
1174 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001175 td_new->files_index = 0;
1176 td_new->files_size = 0;
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001177 if (td_new->o.filename) {
1178 free(td_new->o.filename);
1179 td_new->o.filename = NULL;
1180 }
Jens Axboee132cba2007-03-14 14:23:54 +01001181 }
1182
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001183 if (add_job(td_new, jobname, numjobs, 1, client_type))
Jens Axboeebac4652005-12-08 15:25:21 +01001184 goto err;
1185 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001186
Jens Axboeebac4652005-12-08 15:25:21 +01001187 return 0;
1188err:
1189 put_job(td);
1190 return -1;
1191}
1192
Jens Axboe79d16312010-03-04 12:43:20 +01001193/*
1194 * Parse as if 'o' was a command line
1195 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001196void add_job_opts(const char **o, int client_type)
Jens Axboe79d16312010-03-04 12:43:20 +01001197{
1198 struct thread_data *td, *td_parent;
1199 int i, in_global = 1;
1200 char jobname[32];
1201
1202 i = 0;
1203 td_parent = td = NULL;
1204 while (o[i]) {
1205 if (!strncmp(o[i], "name", 4)) {
1206 in_global = 0;
1207 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001208 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001209 td = NULL;
1210 sprintf(jobname, "%s", o[i] + 5);
1211 }
1212 if (in_global && !td_parent)
Steven Langde890a12011-11-09 14:03:34 +01001213 td_parent = get_new_job(1, &def_thread, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001214 else if (!in_global && !td) {
1215 if (!td_parent)
1216 td_parent = &def_thread;
Steven Langde890a12011-11-09 14:03:34 +01001217 td = get_new_job(0, td_parent, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001218 }
1219 if (in_global)
Jens Axboe292cc472013-11-26 20:39:35 -07001220 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001221 else
Jens Axboe292cc472013-11-26 20:39:35 -07001222 fio_options_parse(td, (char **) &o[i], 1, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001223 i++;
1224 }
1225
1226 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001227 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001228}
1229
Jens Axboe01f06b62008-02-18 20:53:47 +01001230static int skip_this_section(const char *name)
1231{
Jens Axboead0a2732011-03-11 10:16:17 +01001232 int i;
1233
1234 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +01001235 return 0;
1236 if (!strncmp(name, "global", 6))
1237 return 0;
1238
Jens Axboead0a2732011-03-11 10:16:17 +01001239 for (i = 0; i < nr_job_sections; i++)
1240 if (!strcmp(job_sections[i], name))
1241 return 0;
1242
1243 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001244}
1245
Jens Axboeebac4652005-12-08 15:25:21 +01001246static int is_empty_or_comment(char *line)
1247{
1248 unsigned int i;
1249
1250 for (i = 0; i < strlen(line); i++) {
1251 if (line[i] == ';')
1252 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001253 if (line[i] == '#')
1254 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +02001255 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +01001256 return 0;
1257 }
1258
1259 return 1;
1260}
1261
Jens Axboe313cb202006-12-21 09:50:00 +01001262/*
Jens Axboe07261982006-06-07 10:51:12 +02001263 * This is our [ini] type file parser.
1264 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001265int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
Jens Axboeebac4652005-12-08 15:25:21 +01001266{
Jens Axboee1f36502006-10-27 10:54:08 +02001267 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001268 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001269 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001270 FILE *f;
1271 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001272 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +02001273 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +02001274 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +01001275 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001276 char **opts;
1277 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +01001278
Jens Axboe50d16972011-09-29 17:45:28 -06001279 if (is_buf)
1280 f = NULL;
1281 else {
1282 if (!strcmp(file, "-"))
1283 f = stdin;
1284 else
1285 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001286
Jens Axboe50d16972011-09-29 17:45:28 -06001287 if (!f) {
1288 perror("fopen job file");
1289 return 1;
1290 }
Jens Axboeebac4652005-12-08 15:25:21 +01001291 }
1292
1293 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +02001294
1295 /*
1296 * it's really 256 + small bit, 280 should suffice
1297 */
1298 name = malloc(280);
1299 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +01001300
Jens Axboe3b8b7132008-06-10 19:46:23 +02001301 alloc_opts = 8;
1302 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +02001303 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001304
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001305 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001306 do {
Jens Axboeccf8f122007-09-27 10:48:55 +02001307 /*
1308 * if skip_fgets is set, we already have loaded a line we
1309 * haven't handled.
1310 */
1311 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -06001312 if (is_buf)
1313 p = strsep(&file, "\n");
1314 else
Jens Axboe43f74792011-10-15 14:37:26 +02001315 p = fgets(string, 4096, f);
Jens Axboeccf8f122007-09-27 10:48:55 +02001316 if (!p)
1317 break;
1318 }
gurudas paicdc7f192007-03-29 10:10:56 +02001319
Jens Axboeccf8f122007-09-27 10:48:55 +02001320 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +02001321 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001322 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +02001323
Jens Axboeebac4652005-12-08 15:25:21 +01001324 if (is_empty_or_comment(p))
1325 continue;
Bruce Cran84dd1882011-05-05 08:14:09 -06001326 if (sscanf(p, "[%255[^\n]]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001327 if (inside_skip)
1328 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001329 log_err("fio: option <%s> outside of [] job section\n",
1330 p);
Jens Axboe088b4202007-07-19 14:53:01 +02001331 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001332 }
Jens Axboeebac4652005-12-08 15:25:21 +01001333
Jens Axboe7a4b80a2010-05-22 20:43:11 +02001334 name[strlen(name) - 1] = '\0';
1335
Jens Axboe01f06b62008-02-18 20:53:47 +01001336 if (skip_this_section(name)) {
1337 inside_skip = 1;
1338 continue;
1339 } else
1340 inside_skip = 0;
1341
Jens Axboeebac4652005-12-08 15:25:21 +01001342 global = !strncmp(name, "global", 6);
1343
Jens Axboecca73aa2007-04-04 11:09:19 +02001344 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +02001345 if (first_sect)
1346 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +02001347 if (!global)
1348 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +02001349 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001350 }
1351
Steven Langde890a12011-11-09 14:03:34 +01001352 td = get_new_job(global, &def_thread, 0);
Jens Axboe45410ac2006-06-07 11:10:39 +02001353 if (!td) {
1354 ret = 1;
1355 break;
1356 }
Jens Axboeebac4652005-12-08 15:25:21 +01001357
Jens Axboe972cfd22006-06-09 11:08:56 +02001358 /*
Anatol Pomozovde8f6de2013-09-26 16:31:34 -07001359 * Separate multiple job files by a stonewall
Jens Axboe972cfd22006-06-09 11:08:56 +02001360 */
Jens Axboef9481912006-06-09 11:37:28 +02001361 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001362 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +02001363 stonewall = 0;
1364 }
1365
Jens Axboe3b8b7132008-06-10 19:46:23 +02001366 num_opts = 0;
1367 memset(opts, 0, alloc_opts * sizeof(char *));
1368
Jens Axboe50d16972011-09-29 17:45:28 -06001369 while (1) {
1370 if (is_buf)
1371 p = strsep(&file, "\n");
1372 else
1373 p = fgets(string, 4096, f);
1374 if (!p)
1375 break;
1376
Jens Axboeebac4652005-12-08 15:25:21 +01001377 if (is_empty_or_comment(p))
1378 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001379
Jens Axboeb6754f92006-05-30 14:29:32 +02001380 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001381
Jens Axboeccf8f122007-09-27 10:48:55 +02001382 /*
1383 * new section, break out and make sure we don't
1384 * fgets() a new line at the top.
1385 */
1386 if (p[0] == '[') {
1387 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001388 break;
Jens Axboeccf8f122007-09-27 10:48:55 +02001389 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001390
Jens Axboe4ae3f762006-05-30 13:35:14 +02001391 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001392
Jens Axboe3b8b7132008-06-10 19:46:23 +02001393 if (num_opts == alloc_opts) {
1394 alloc_opts <<= 1;
1395 opts = realloc(opts,
1396 alloc_opts * sizeof(char *));
1397 }
1398
1399 opts[num_opts] = strdup(p);
1400 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +01001401 }
Jens Axboeebac4652005-12-08 15:25:21 +01001402
Jens Axboe292cc472013-11-26 20:39:35 -07001403 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1404 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001405 ret = add_job(td, name, 0, 0, type);
Jens Axboe292cc472013-11-26 20:39:35 -07001406 else {
Jens Axboeb1508cf2006-11-02 09:12:40 +01001407 log_err("fio: job %s dropped\n", name);
1408 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001409 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001410
1411 for (i = 0; i < num_opts; i++)
1412 free(opts[i]);
1413 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001414 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001415
Jens Axboecca73aa2007-04-04 11:09:19 +02001416 if (dump_cmdline)
1417 log_info("\n");
1418
Jens Axboe7e356b22011-10-14 10:55:16 +02001419 i = 0;
1420 while (i < nr_job_sections) {
1421 free(job_sections[i]);
1422 i++;
1423 }
1424
Jens Axboe3b8b7132008-06-10 19:46:23 +02001425 for (i = 0; i < num_opts; i++)
1426 free(opts[i]);
1427
Jens Axboeebac4652005-12-08 15:25:21 +01001428 free(string);
1429 free(name);
Jens Axboe25775942008-06-10 20:26:06 +02001430 free(opts);
Jens Axboe50d16972011-09-29 17:45:28 -06001431 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001432 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001433 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001434}
1435
1436static int fill_def_thread(void)
1437{
1438 memset(&def_thread, 0, sizeof(def_thread));
1439
Jens Axboe375b2692007-05-22 09:13:02 +02001440 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboe8c029372011-10-05 09:54:30 +02001441 def_thread.o.timeout = def_timeout;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001442 def_thread.o.error_dump = 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001443 /*
Jens Axboeee738492007-01-10 11:23:16 +01001444 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001445 */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001446 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001447 return 0;
1448}
1449
Jens Axboe45378b32007-12-19 13:54:38 +01001450static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02001451{
Jens Axboe3d433822012-03-21 22:25:22 +01001452 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01001453 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboe83881282011-10-17 19:58:51 +02001454 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1455 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
Jens Axboe3e260a42013-12-09 12:38:53 -07001456 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
Jens Axboe111e0322013-03-07 11:31:20 +01001457 printf(" --parse-only\t\tParse options only, don't start any IO\n");
Jens Axboe83881282011-10-17 19:58:51 +02001458 printf(" --output\t\tWrite output to file\n");
liang xieb2cecdc2012-08-31 08:22:42 -07001459 printf(" --runtime\t\tRuntime in seconds\n");
Jens Axboe83881282011-10-17 19:58:51 +02001460 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1461 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1462 printf(" --minimal\t\tMinimal (terse) output\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001463 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
Jens Axboe75db6e22011-11-07 22:07:36 +01001464 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001465 printf(" --version\t\tPrint version info and exit\n");
Jens Axboe83881282011-10-17 19:58:51 +02001466 printf(" --help\t\tPrint this page\n");
Jens Axboe23893642012-12-17 14:44:08 +01001467 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
Jens Axboefec0f212014-02-07 14:39:33 -07001468 printf(" --crctest\t\tTest speed of checksum functions\n");
Jens Axboe83881282011-10-17 19:58:51 +02001469 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001470 " them\n");
Steven Langde890a12011-11-09 14:03:34 +01001471 printf(" --enghelp=engine\tPrint ioengine help, or list"
1472 " available ioengines\n");
1473 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1474 " cmd\n");
Jens Axboe83881282011-10-17 19:58:51 +02001475 printf(" --showcmd\t\tTurn a job file into command line options\n");
1476 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1477 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboee382e662013-02-22 20:48:56 +01001478 printf(" --eta-newline=time\tForce a new line for every 'time'");
1479 printf(" period passed\n");
Jens Axboe06464902013-04-24 20:38:54 -06001480 printf(" --status-interval=t\tForce full status dump every");
1481 printf(" 't' period passed\n");
Jens Axboe83881282011-10-17 19:58:51 +02001482 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001483 " writes\n");
Jens Axboe83881282011-10-17 19:58:51 +02001484 printf(" --section=name\tOnly run specified section in job file\n");
1485 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
Jens Axboe2b386d22008-03-26 10:32:57 +01001486 " (def 1024)\n");
Jens Axboe83881282011-10-17 19:58:51 +02001487 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1488 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1489 printf(" --server=args\t\tStart a backend fio server\n");
1490 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1491 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001492 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1493 "\t\t\t(option=system,percpu) or run unit work\n"
1494 "\t\t\tcalibration only (option=calibrate)\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001495 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
Jens Axboebfca2c72014-01-29 16:39:04 -07001496 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1497 printf("\n Jens Axboe <axboe@fb.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001498}
1499
Jens Axboe79e48f72008-02-01 20:37:09 +01001500#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001501struct debug_level debug_levels[] = {
Jens Axboe0b8d11e2012-03-02 19:44:15 +01001502 { .name = "process",
1503 .help = "Process creation/exit logging",
1504 .shift = FD_PROCESS,
1505 },
1506 { .name = "file",
1507 .help = "File related action logging",
1508 .shift = FD_FILE,
1509 },
1510 { .name = "io",
1511 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1512 .shift = FD_IO,
1513 },
1514 { .name = "mem",
1515 .help = "Memory allocation/freeing logging",
1516 .shift = FD_MEM,
1517 },
1518 { .name = "blktrace",
1519 .help = "blktrace action logging",
1520 .shift = FD_BLKTRACE,
1521 },
1522 { .name = "verify",
1523 .help = "IO verification action logging",
1524 .shift = FD_VERIFY,
1525 },
1526 { .name = "random",
1527 .help = "Random generation logging",
1528 .shift = FD_RANDOM,
1529 },
1530 { .name = "parse",
1531 .help = "Parser logging",
1532 .shift = FD_PARSE,
1533 },
1534 { .name = "diskutil",
1535 .help = "Disk utility logging actions",
1536 .shift = FD_DISKUTIL,
1537 },
1538 { .name = "job",
1539 .help = "Logging related to creating/destroying jobs",
1540 .shift = FD_JOB,
1541 },
1542 { .name = "mutex",
1543 .help = "Mutex logging",
1544 .shift = FD_MUTEX
1545 },
1546 { .name = "profile",
1547 .help = "Logging related to profiles",
1548 .shift = FD_PROFILE,
1549 },
1550 { .name = "time",
1551 .help = "Logging related to time keeping functions",
1552 .shift = FD_TIME,
1553 },
1554 { .name = "net",
1555 .help = "Network logging",
1556 .shift = FD_NET,
1557 },
Jens Axboe3e260a42013-12-09 12:38:53 -07001558 { .name = "rate",
1559 .help = "Rate logging",
1560 .shift = FD_RATE,
1561 },
Jens Axboe02444ad2008-10-07 11:33:00 +02001562 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001563};
1564
Jens Axboec09823a2008-02-19 20:16:57 +01001565static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001566{
1567 struct debug_level *dl;
1568 char *p = (char *) string;
1569 char *opt;
1570 int i;
1571
1572 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001573 log_info("fio: dumping debug options:");
1574 for (i = 0; debug_levels[i].name; i++) {
1575 dl = &debug_levels[i];
1576 log_info("%s,", dl->name);
1577 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001578 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001579 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001580 }
1581
1582 while ((opt = strsep(&p, ",")) != NULL) {
1583 int found = 0;
1584
Jens Axboe5e1d3062008-05-23 11:55:53 +02001585 if (!strncmp(opt, "all", 3)) {
1586 log_info("fio: set all debug options\n");
1587 fio_debug = ~0UL;
1588 continue;
1589 }
1590
Jens Axboeee56ad52008-02-01 10:30:20 +01001591 for (i = 0; debug_levels[i].name; i++) {
1592 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001593 found = !strncmp(opt, dl->name, strlen(dl->name));
1594 if (!found)
1595 continue;
1596
1597 if (dl->shift == FD_JOB) {
1598 opt = strchr(opt, ':');
1599 if (!opt) {
1600 log_err("fio: missing job number\n");
1601 break;
1602 }
1603 opt++;
1604 fio_debug_jobno = atoi(opt);
1605 log_info("fio: set debug jobno %d\n",
1606 fio_debug_jobno);
1607 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001608 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001609 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001610 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001611 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001612 }
1613
1614 if (!found)
1615 log_err("fio: debug mask %s not found\n", opt);
1616 }
Jens Axboec09823a2008-02-19 20:16:57 +01001617 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001618}
Jens Axboe79e48f72008-02-01 20:37:09 +01001619#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001620static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001621{
1622 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001623 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001624}
1625#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001626
Jens Axboe4c6107f2011-01-18 05:22:22 -07001627static void fio_options_fill_optstring(void)
1628{
1629 char *ostr = cmd_optstr;
1630 int i, c;
1631
1632 c = i = 0;
1633 while (l_opts[i].name) {
1634 ostr[c++] = l_opts[i].val;
1635 if (l_opts[i].has_arg == required_argument)
1636 ostr[c++] = ':';
1637 else if (l_opts[i].has_arg == optional_argument) {
1638 ostr[c++] = ':';
1639 ostr[c++] = ':';
1640 }
1641 i++;
1642 }
1643 ostr[c] = '\0';
1644}
1645
Jens Axboec2c94582011-10-05 20:31:30 +02001646static int client_flag_set(char c)
1647{
1648 int i;
1649
1650 i = 0;
1651 while (l_opts[i].name) {
1652 int val = l_opts[i].val;
1653
1654 if (c == (val & 0xff))
1655 return (val & FIO_CLIENT_FLAG);
1656
1657 i++;
1658 }
1659
1660 return 0;
1661}
1662
Jens Axboe10aa1362014-04-01 21:10:36 -06001663static void parse_cmd_client(void *client, char *opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +02001664{
Jens Axboefa2ea802011-10-08 21:07:29 +02001665 fio_client_add_cmd_option(client, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001666}
1667
Jens Axboe46bcd492012-03-14 11:31:21 +01001668int parse_cmd_line(int argc, char *argv[], int client_type)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001669{
1670 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001671 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001672 char *ostr = cmd_optstr;
Jens Axboe402668f2011-10-10 15:28:58 +02001673 void *pid_file = NULL;
Jens Axboe9bae26e2011-10-07 10:07:22 +02001674 void *cur_client = NULL;
Jens Axboe81179ee2011-10-04 12:42:06 +02001675 int backend = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001676
Jens Axboec2c94582011-10-05 20:31:30 +02001677 /*
1678 * Reset optind handling, since we may call this multiple times
1679 * for the backend.
1680 */
1681 optind = 1;
Jens Axboe7a4b8242011-10-05 17:35:15 +02001682
Jens Axboec2c94582011-10-05 20:31:30 +02001683 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
Jens Axboe085399d2011-10-06 09:01:22 +02001684 did_arg = 1;
1685
Jens Axboec2c94582011-10-05 20:31:30 +02001686 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
Jens Axboefa2ea802011-10-08 21:07:29 +02001687 parse_cmd_client(cur_client, argv[optind - 1]);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001688 c &= ~FIO_CLIENT_FLAG;
1689 }
1690
Jens Axboe214e1ec2007-03-15 10:48:13 +01001691 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001692 case 'a':
1693 smalloc_pool_size = atoi(optarg);
1694 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001695 case 't':
Jens Axboe8c029372011-10-05 09:54:30 +02001696 def_timeout = atoi(optarg);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001697 break;
1698 case 'l':
1699 write_lat_log = 1;
1700 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001701 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001702 write_bw_log = 1;
1703 break;
1704 case 'o':
1705 f_out = fopen(optarg, "w+");
1706 if (!f_out) {
1707 perror("fopen output");
1708 exit(1);
1709 }
1710 f_err = f_out;
1711 break;
1712 case 'm':
Jens Axboef3afa572012-09-17 13:34:16 +02001713 output_format = FIO_OUTPUT_TERSE;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001714 break;
Jens Axboef3afa572012-09-17 13:34:16 +02001715 case 'F':
Stefan Hajnoczid10983d2014-02-04 14:27:11 +01001716 if (!optarg) {
1717 log_err("fio: missing --output-format argument\n");
1718 exit_val = 1;
1719 do_exit++;
1720 break;
1721 }
Jens Axboef3afa572012-09-17 13:34:16 +02001722 if (!strcmp(optarg, "minimal") ||
1723 !strcmp(optarg, "terse") ||
1724 !strcmp(optarg, "csv"))
1725 output_format = FIO_OUTPUT_TERSE;
1726 else if (!strcmp(optarg, "json"))
1727 output_format = FIO_OUTPUT_JSON;
1728 else
1729 output_format = FIO_OUTPUT_NORMAL;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001730 break;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +01001731 case 'f':
1732 append_terse_output = 1;
1733 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001734 case 'h':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001735 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001736 usage(argv[0]);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001737 do_exit++;
1738 }
Jens Axboec2c94582011-10-05 20:31:30 +02001739 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001740 case 'c':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001741 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001742 fio_show_option_help(optarg);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001743 do_exit++;
1744 }
Jens Axboec2c94582011-10-05 20:31:30 +02001745 break;
Steven Langde890a12011-11-09 14:03:34 +01001746 case 'i':
1747 if (!cur_client) {
1748 fio_show_ioengine_help(optarg);
1749 do_exit++;
1750 }
1751 break;
Jens Axboecca73aa2007-04-04 11:09:19 +02001752 case 's':
1753 dump_cmdline = 1;
1754 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001755 case 'r':
1756 read_only = 1;
1757 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001758 case 'v':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001759 if (!cur_client) {
Jens Axboe3d433822012-03-21 22:25:22 +01001760 log_info("%s\n", fio_version_string);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001761 do_exit++;
1762 }
Jens Axboec2c94582011-10-05 20:31:30 +02001763 break;
Jens Axboef57a9c52011-09-09 21:01:37 +02001764 case 'V':
1765 terse_version = atoi(optarg);
Jens Axboe09786f52012-10-04 17:06:49 +02001766 if (!(terse_version == 2 || terse_version == 3 ||
1767 terse_version == 4)) {
Jens Axboef57a9c52011-09-09 21:01:37 +02001768 log_err("fio: bad terse version format\n");
1769 exit_val = 1;
1770 do_exit++;
1771 }
1772 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001773 case 'e':
1774 if (!strcmp("always", optarg))
1775 eta_print = FIO_ETA_ALWAYS;
1776 else if (!strcmp("never", optarg))
1777 eta_print = FIO_ETA_NEVER;
1778 break;
Jens Axboee382e662013-02-22 20:48:56 +01001779 case 'E': {
1780 long long t = 0;
1781
Jens Axboe0de5b262014-02-21 15:26:01 -08001782 if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
Jens Axboee382e662013-02-22 20:48:56 +01001783 log_err("fio: failed parsing eta time %s\n", optarg);
1784 exit_val = 1;
1785 do_exit++;
1786 }
1787 eta_new_line = t;
1788 break;
1789 }
Jens Axboeee56ad52008-02-01 10:30:20 +01001790 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001791 if (set_debug(optarg))
1792 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001793 break;
Jens Axboe111e0322013-03-07 11:31:20 +01001794 case 'P':
1795 parse_only = 1;
1796 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001797 case 'x': {
1798 size_t new_size;
1799
Jens Axboe01f06b62008-02-18 20:53:47 +01001800 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001801 log_err("fio: can't use global as only "
1802 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001803 do_exit++;
1804 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001805 break;
1806 }
Jens Axboead0a2732011-03-11 10:16:17 +01001807 new_size = (nr_job_sections + 1) * sizeof(char *);
1808 job_sections = realloc(job_sections, new_size);
1809 job_sections[nr_job_sections] = strdup(optarg);
1810 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01001811 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001812 }
Jens Axboe9ac8a792009-11-13 21:23:07 +01001813 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001814 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001815 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001816 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001817 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001818 char *val = optarg;
1819
1820 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe46bcd492012-03-14 11:31:21 +01001821 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe66251552011-05-12 10:14:57 +02001822 if (ret)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001823 return 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001824 td = NULL;
1825 }
1826 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001827 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001828 int global = 0;
1829
Jens Axboe01f06b62008-02-18 20:53:47 +01001830 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001831 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001832
Jens Axboe01f06b62008-02-18 20:53:47 +01001833 if (is_section && skip_this_section(val))
1834 continue;
1835
Steven Langde890a12011-11-09 14:03:34 +01001836 td = get_new_job(global, &def_thread, 1);
1837 if (!td || ioengine_load(td))
Jens Axboe214e1ec2007-03-15 10:48:13 +01001838 return 0;
Steven Langde890a12011-11-09 14:03:34 +01001839 fio_options_set_ioengine_opts(l_opts, td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001840 }
1841
Jens Axboe9d918182013-05-23 20:00:35 +02001842 if ((!val || !strlen(val)) &&
1843 l_opts[lidx].has_arg == required_argument) {
1844 log_err("fio: option %s requires an argument\n", opt);
1845 ret = 1;
1846 } else
1847 ret = fio_cmd_option_parse(td, opt, val);
1848
Jens Axboebfb3ea22013-05-23 19:54:43 +02001849 if (ret) {
1850 if (td) {
1851 put_job(td);
1852 td = NULL;
1853 }
Jens Axboea88c8c12013-04-24 22:25:22 -06001854 do_exit++;
Jens Axboebfb3ea22013-05-23 19:54:43 +02001855 }
Steven Langde890a12011-11-09 14:03:34 +01001856
1857 if (!ret && !strcmp(opt, "ioengine")) {
1858 free_ioengine(td);
1859 if (ioengine_load(td))
1860 return 0;
1861 fio_options_set_ioengine_opts(l_opts, td);
1862 }
1863 break;
1864 }
1865 case FIO_GETOPT_IOENGINE: {
1866 const char *opt = l_opts[lidx].name;
1867 char *val = optarg;
Steven Langde890a12011-11-09 14:03:34 +01001868 ret = fio_cmd_ioengine_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001869 break;
1870 }
Jens Axboea9523c62011-01-17 16:49:54 -07001871 case 'w':
1872 warnings_fatal = 1;
1873 break;
Jens Axboefca70352011-07-06 20:12:54 +02001874 case 'j':
1875 max_jobs = atoi(optarg);
1876 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1877 log_err("fio: invalid max jobs: %d\n", max_jobs);
1878 do_exit++;
1879 exit_val = 1;
1880 }
1881 break;
Jens Axboe50d16972011-09-29 17:45:28 -06001882 case 'S':
Jens Axboea37f69b2011-10-01 12:24:21 -06001883 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06001884 log_err("fio: can't be both client and server\n");
1885 do_exit++;
1886 exit_val = 1;
1887 break;
1888 }
Jens Axboe87aa8f12011-10-06 21:24:13 +02001889 if (optarg)
Jens Axboebebe6392011-10-07 10:00:51 +02001890 fio_server_set_arg(optarg);
Jens Axboe50d16972011-09-29 17:45:28 -06001891 is_backend = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001892 backend = 1;
Jens Axboe50d16972011-09-29 17:45:28 -06001893 break;
Jens Axboee46d8092011-10-03 09:11:02 +02001894 case 'D':
Jens Axboe402668f2011-10-10 15:28:58 +02001895 pid_file = strdup(optarg);
Jens Axboee46d8092011-10-03 09:11:02 +02001896 break;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001897 case 'I':
1898 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1899 /* exit on error and calibration only */
1900 do_exit++;
1901 if (ret == -1)
1902 exit_val = 1;
1903 }
1904 break;
Jens Axboe132159a2011-09-30 15:01:32 -06001905 case 'C':
1906 if (is_backend) {
1907 log_err("fio: can't be both client and server\n");
1908 do_exit++;
1909 exit_val = 1;
1910 break;
1911 }
Jens Axboea5276612012-03-04 15:15:08 +01001912 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
Jens Axboebebe6392011-10-07 10:00:51 +02001913 log_err("fio: failed adding client %s\n", optarg);
1914 do_exit++;
1915 exit_val = 1;
1916 break;
1917 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02001918 /*
1919 * If the next argument exists and isn't an option,
1920 * assume it's a job file for this client only.
1921 */
1922 while (optind < argc) {
1923 if (!strncmp(argv[optind], "--", 2) ||
1924 !strncmp(argv[optind], "-", 1))
1925 break;
1926
1927 fio_client_add_ini_file(cur_client, argv[optind]);
1928 optind++;
1929 }
Jens Axboe132159a2011-09-30 15:01:32 -06001930 break;
Jens Axboe7d11f872012-12-17 12:03:29 +01001931 case 'T':
1932 do_exit++;
1933 exit_val = fio_monotonic_clocktest();
1934 break;
Jens Axboefec0f212014-02-07 14:39:33 -07001935 case 'G':
1936 do_exit++;
1937 exit_val = fio_crctest(optarg);
1938 break;
Jens Axboe06464902013-04-24 20:38:54 -06001939 case 'L': {
1940 long long val;
1941
Jens Axboe7cd0ec52014-03-28 08:09:10 -06001942 if (check_str_time(optarg, &val, 0)) {
Jens Axboe06464902013-04-24 20:38:54 -06001943 log_err("fio: failed parsing time %s\n", optarg);
1944 do_exit++;
1945 exit_val = 1;
1946 break;
1947 }
1948 status_interval = val * 1000;
1949 break;
1950 }
Jens Axboe798827c2013-01-23 18:11:48 -07001951 case '?':
1952 log_err("%s: unrecognized option '%s'\n", argv[0],
1953 argv[optind - 1]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001954 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001955 do_exit++;
1956 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001957 break;
1958 }
Jens Axboec7d5c942011-10-03 11:48:17 +02001959 if (do_exit)
1960 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001961 }
1962
Jens Axboe0b14f0a2013-05-23 21:27:54 +02001963 if (do_exit && !(is_backend || nr_clients))
1964 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001965
Jens Axboefb296042014-04-02 08:28:49 -06001966 if (nr_clients && fio_clients_connect())
1967 exit(1);
Jens Axboe132159a2011-09-30 15:01:32 -06001968
Jens Axboe81179ee2011-10-04 12:42:06 +02001969 if (is_backend && backend)
Jens Axboe402668f2011-10-10 15:28:58 +02001970 return fio_start_server(pid_file);
Jens Axboe50d16972011-09-29 17:45:28 -06001971
Jens Axboe214e1ec2007-03-15 10:48:13 +01001972 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001973 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001974 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001975 }
1976
Jens Axboe7874f8b2011-10-06 09:18:20 +02001977 while (!ret && optind < argc) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001978 ini_idx++;
1979 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1980 ini_file[ini_idx - 1] = strdup(argv[optind]);
1981 optind++;
1982 }
1983
1984 return ini_idx;
1985}
1986
Jens Axboe0420ba62012-02-29 11:16:52 +01001987int fio_init_options(void)
Jens Axboeebac4652005-12-08 15:25:21 +01001988{
Jens Axboeb4692822006-10-27 13:43:22 +02001989 f_out = stdout;
1990 f_err = stderr;
1991
Jens Axboe4c6107f2011-01-18 05:22:22 -07001992 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001993 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001994
Jens Axboe9d9eb2e2011-10-03 12:01:42 +02001995 atexit(free_shm);
1996
Jens Axboeebac4652005-12-08 15:25:21 +01001997 if (fill_def_thread())
1998 return 1;
1999
Jens Axboe0420ba62012-02-29 11:16:52 +01002000 return 0;
2001}
2002
Jens Axboe51167792012-03-08 21:34:18 +01002003extern int fio_check_options(struct thread_options *);
2004
Jens Axboe0420ba62012-02-29 11:16:52 +01002005int parse_options(int argc, char *argv[])
2006{
Jens Axboe46bcd492012-03-14 11:31:21 +01002007 const int type = FIO_CLIENT_TYPE_CLI;
Jens Axboe0420ba62012-02-29 11:16:52 +01002008 int job_files, i;
2009
2010 if (fio_init_options())
2011 return 1;
Jens Axboe51167792012-03-08 21:34:18 +01002012 if (fio_test_cconv(&def_thread.o))
2013 log_err("fio: failed internal cconv test\n");
Jens Axboe0420ba62012-02-29 11:16:52 +01002014
Jens Axboe46bcd492012-03-14 11:31:21 +01002015 job_files = parse_cmd_line(argc, argv, type);
Jens Axboeebac4652005-12-08 15:25:21 +01002016
Jens Axboecdf54d82011-10-04 08:31:40 +02002017 if (job_files > 0) {
2018 for (i = 0; i < job_files; i++) {
2019 if (fill_def_thread())
Jens Axboe132159a2011-09-30 15:01:32 -06002020 return 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02002021 if (nr_clients) {
2022 if (fio_clients_send_ini(ini_file[i]))
2023 return 1;
2024 free(ini_file[i]);
2025 } else if (!is_backend) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002026 if (parse_jobs_ini(ini_file[i], 0, i, type))
Jens Axboecdf54d82011-10-04 08:31:40 +02002027 return 1;
2028 free(ini_file[i]);
2029 }
Jens Axboe132159a2011-09-30 15:01:32 -06002030 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02002031 } else if (nr_clients) {
2032 if (fill_def_thread())
2033 return 1;
2034 if (fio_clients_send_ini(NULL))
2035 return 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02002036 }
Jens Axboeebac4652005-12-08 15:25:21 +01002037
Jens Axboe88c6ed82006-06-09 11:28:10 +02002038 free(ini_file);
Jens Axboe7e356b22011-10-14 10:55:16 +02002039 fio_options_free(&def_thread);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08002040 filesetup_mem_free();
Jens Axboeb4692822006-10-27 13:43:22 +02002041
2042 if (!thread_number) {
Jens Axboef9633d72013-09-06 09:59:56 -06002043 if (parse_dryrun())
Jens Axboecca73aa2007-04-04 11:09:19 +02002044 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01002045 if (exec_profile)
2046 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06002047 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06002048 return 0;
Jens Axboe085399d2011-10-06 09:01:22 +02002049 if (did_arg)
2050 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02002051
Jens Axboed65e11c2011-10-05 09:56:53 +02002052 log_err("No jobs(s) defined\n\n");
Jens Axboe085399d2011-10-06 09:01:22 +02002053
2054 if (!did_arg) {
2055 usage(argv[0]);
2056 return 1;
2057 }
2058
2059 return 0;
Jens Axboeb4692822006-10-27 13:43:22 +02002060 }
2061
Jens Axboebe4ecfd2008-12-08 14:10:52 +01002062 if (def_thread.o.gtod_offload) {
2063 fio_gtod_init();
2064 fio_gtod_offload = 1;
2065 fio_gtod_cpu = def_thread.o.gtod_cpu;
2066 }
2067
Jens Axboef3afa572012-09-17 13:34:16 +02002068 if (output_format == FIO_OUTPUT_NORMAL)
Jens Axboe3d433822012-03-21 22:25:22 +01002069 log_info("%s\n", fio_version_string);
Jens Axboef6dea4d2011-10-13 13:37:07 +02002070
Jens Axboeebac4652005-12-08 15:25:21 +01002071 return 0;
2072}
Jens Axboe588b7f02012-03-19 20:37:41 +01002073
2074void options_default_fill(struct thread_options *o)
2075{
2076 memcpy(o, &def_thread.o, sizeof(*o));
2077}