blob: 910f2badbcfda054d8a6f300575093eabb572924 [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 Axboe8c029372011-10-05 09:54:30 +020041static int 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 Axboe5ec10ea2008-03-06 15:42:00 +010067static int write_lat_log;
Jens Axboe214e1ec2007-03-15 10:48:13 +010068
69static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020070
Jens Axboeee56ad52008-02-01 10:30:20 +010071unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020072unsigned int fio_debug_jobno = -1;
73unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010074
Jens Axboe4c6107f2011-01-18 05:22:22 -070075static char cmd_optstr[256];
Jens Axboe085399d2011-10-06 09:01:22 +020076static int did_arg;
Jens Axboe4c6107f2011-01-18 05:22:22 -070077
Jens Axboe7a4b8242011-10-05 17:35:15 +020078#define FIO_CLIENT_FLAG (1 << 16)
79
Jens Axboeb4692822006-10-27 13:43:22 +020080/*
81 * Command line options. These will contain the above, plus a few
82 * extra that only pertain to fio itself and not jobs.
83 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010084static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020085 {
Jens Axboe08d2a192011-05-11 13:28:30 +020086 .name = (char *) "output",
Jens Axboeb4692822006-10-27 13:43:22 +020087 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020088 .val = 'o' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020089 },
90 {
Jens Axboe08d2a192011-05-11 13:28:30 +020091 .name = (char *) "timeout",
Jens Axboeb4692822006-10-27 13:43:22 +020092 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020093 .val = 't' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020094 },
95 {
Jens Axboe08d2a192011-05-11 13:28:30 +020096 .name = (char *) "latency-log",
Jens Axboeb4692822006-10-27 13:43:22 +020097 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020098 .val = 'l' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +020099 },
100 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200101 .name = (char *) "bandwidth-log",
Jens Axboeb4692822006-10-27 13:43:22 +0200102 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200103 .val = 'b' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200104 },
105 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200106 .name = (char *) "minimal",
Stefan Hajnoczic4ec0a12014-02-04 14:27:10 +0100107 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200108 .val = 'm' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200109 },
110 {
Jens Axboef3afa572012-09-17 13:34:16 +0200111 .name = (char *) "output-format",
Shaohua Licc372b12012-09-17 09:12:18 +0200112 .has_arg = optional_argument,
Jens Axboef3afa572012-09-17 13:34:16 +0200113 .val = 'F' | FIO_CLIENT_FLAG,
Shaohua Licc372b12012-09-17 09:12:18 +0200114 },
115 {
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +0100116 .name = (char *) "append-terse",
117 .has_arg = optional_argument,
118 .val = 'f',
119 },
120 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200121 .name = (char *) "version",
Jens Axboeb4692822006-10-27 13:43:22 +0200122 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200123 .val = 'v' | FIO_CLIENT_FLAG,
Jens Axboeb4692822006-10-27 13:43:22 +0200124 },
125 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200126 .name = (char *) "help",
Jens Axboefd28ca42007-01-09 21:20:13 +0100127 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200128 .val = 'h' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100129 },
130 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200131 .name = (char *) "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100132 .has_arg = optional_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200133 .val = 'c' | FIO_CLIENT_FLAG,
Jens Axboefd28ca42007-01-09 21:20:13 +0100134 },
135 {
Jens Axboe06464902013-04-24 20:38:54 -0600136 .name = (char *) "enghelp",
Steven Langde890a12011-11-09 14:03:34 +0100137 .has_arg = optional_argument,
Jens Axboe06464902013-04-24 20:38:54 -0600138 .val = 'i' | FIO_CLIENT_FLAG,
Steven Langde890a12011-11-09 14:03:34 +0100139 },
140 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200141 .name = (char *) "showcmd",
Jens Axboecca73aa2007-04-04 11:09:19 +0200142 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200143 .val = 's' | FIO_CLIENT_FLAG,
Jens Axboe724e4432007-09-11 20:02:05 +0200144 },
145 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200146 .name = (char *) "readonly",
Jens Axboe724e4432007-09-11 20:02:05 +0200147 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200148 .val = 'r' | FIO_CLIENT_FLAG,
Jens Axboecca73aa2007-04-04 11:09:19 +0200149 },
150 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200151 .name = (char *) "eta",
Aaron Carrolle592a062007-09-14 09:49:41 +0200152 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200153 .val = 'e' | FIO_CLIENT_FLAG,
Aaron Carrolle592a062007-09-14 09:49:41 +0200154 },
155 {
Jens Axboee382e662013-02-22 20:48:56 +0100156 .name = (char *) "eta-newline",
157 .has_arg = required_argument,
158 .val = 'E' | FIO_CLIENT_FLAG,
159 },
160 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200161 .name = (char *) "debug",
Jens Axboeee56ad52008-02-01 10:30:20 +0100162 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200163 .val = 'd' | FIO_CLIENT_FLAG,
Jens Axboeee56ad52008-02-01 10:30:20 +0100164 },
165 {
Jens Axboe111e0322013-03-07 11:31:20 +0100166 .name = (char *) "parse-only",
167 .has_arg = no_argument,
168 .val = 'P' | FIO_CLIENT_FLAG,
169 },
170 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200171 .name = (char *) "section",
Jens Axboe01f06b62008-02-18 20:53:47 +0100172 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200173 .val = 'x' | FIO_CLIENT_FLAG,
Jens Axboe01f06b62008-02-18 20:53:47 +0100174 },
175 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200176 .name = (char *) "alloc-size",
Jens Axboe2b386d22008-03-26 10:32:57 +0100177 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200178 .val = 'a' | FIO_CLIENT_FLAG,
Jens Axboe2b386d22008-03-26 10:32:57 +0100179 },
180 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200181 .name = (char *) "profile",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100182 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200183 .val = 'p' | FIO_CLIENT_FLAG,
Jens Axboe9ac8a792009-11-13 21:23:07 +0100184 },
185 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200186 .name = (char *) "warnings-fatal",
Jens Axboea9523c62011-01-17 16:49:54 -0700187 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200188 .val = 'w' | FIO_CLIENT_FLAG,
Jens Axboea9523c62011-01-17 16:49:54 -0700189 },
190 {
Jens Axboefca70352011-07-06 20:12:54 +0200191 .name = (char *) "max-jobs",
192 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200193 .val = 'j' | FIO_CLIENT_FLAG,
Jens Axboefca70352011-07-06 20:12:54 +0200194 },
195 {
Jens Axboef57a9c52011-09-09 21:01:37 +0200196 .name = (char *) "terse-version",
197 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200198 .val = 'V' | FIO_CLIENT_FLAG,
Jens Axboef57a9c52011-09-09 21:01:37 +0200199 },
200 {
Jens Axboe50d16972011-09-29 17:45:28 -0600201 .name = (char *) "server",
Jens Axboe87aa8f12011-10-06 21:24:13 +0200202 .has_arg = optional_argument,
Jens Axboe50d16972011-09-29 17:45:28 -0600203 .val = 'S',
204 },
Jens Axboee46d8092011-10-03 09:11:02 +0200205 { .name = (char *) "daemonize",
Jens Axboe402668f2011-10-10 15:28:58 +0200206 .has_arg = required_argument,
Jens Axboee46d8092011-10-03 09:11:02 +0200207 .val = 'D',
208 },
Jens Axboe50d16972011-09-29 17:45:28 -0600209 {
Jens Axboe132159a2011-09-30 15:01:32 -0600210 .name = (char *) "client",
211 .has_arg = required_argument,
212 .val = 'C',
213 },
214 {
Jens Axboe7d11f872012-12-17 12:03:29 +0100215 .name = (char *) "cpuclock-test",
216 .has_arg = no_argument,
217 .val = 'T',
218 },
219 {
Jens Axboefec0f212014-02-07 14:39:33 -0700220 .name = (char *) "crctest",
221 .has_arg = optional_argument,
222 .val = 'G',
223 },
224 {
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100225 .name = (char *) "idle-prof",
226 .has_arg = required_argument,
227 .val = 'I',
228 },
229 {
Jens Axboe06464902013-04-24 20:38:54 -0600230 .name = (char *) "status-interval",
231 .has_arg = required_argument,
232 .val = 'L',
233 },
234 {
Jens Axboeb4692822006-10-27 13:43:22 +0200235 .name = NULL,
236 },
237};
238
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200239void free_threads_shm(void)
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200240{
241 struct shmid_ds sbuf;
242
243 if (threads) {
244 void *tp = threads;
245
246 threads = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200247 shmdt(tp);
248 shmctl(shm_id, IPC_RMID, &sbuf);
249 shm_id = -1;
250 }
251}
252
Jens Axboe10aa1362014-04-01 21:10:36 -0600253static void free_shm(void)
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200254{
255 if (threads) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200256 file_hash_exit();
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100257 flow_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200258 fio_debug_jobp = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200259 free_threads_shm();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200260 }
261
Jens Axboeeb0c74a2014-02-11 10:33:06 -0700262 options_free(fio_options, &def_thread);
Jens Axboe243bfe12014-04-02 15:46:58 -0600263 fio_filelock_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200264 scleanup();
265}
266
267/*
268 * The thread area is shared between the main process and the job
269 * threads/processes. So setup a shared memory segment that will hold
270 * all the job info. We use the end of the region for keeping track of
271 * open files across jobs, for file sharing.
272 */
273static int setup_thread_area(void)
274{
275 void *hash;
276
277 if (threads)
278 return 0;
279
280 /*
281 * 1024 is too much on some machines, scale max_jobs if
282 * we get a failure that looks like too large a shm segment
283 */
284 do {
285 size_t size = max_jobs * sizeof(struct thread_data);
286
287 size += file_hash_size;
288 size += sizeof(unsigned int);
289
290 shm_id = shmget(0, size, IPC_CREAT | 0600);
291 if (shm_id != -1)
292 break;
Jens Axboef0c77f02012-09-21 15:05:45 +0200293 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200294 perror("shmget");
295 break;
296 }
297
298 max_jobs >>= 1;
299 } while (max_jobs);
300
301 if (shm_id == -1)
302 return 1;
303
304 threads = shmat(shm_id, NULL, 0);
305 if (threads == (void *) -1) {
306 perror("shmat");
307 return 1;
308 }
309
310 memset(threads, 0, max_jobs * sizeof(struct thread_data));
311 hash = (void *) threads + max_jobs * sizeof(struct thread_data);
312 fio_debug_jobp = (void *) hash + file_hash_size;
313 *fio_debug_jobp = -1;
314 file_hash_init(hash);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100315
316 flow_init();
317
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200318 return 0;
319}
320
Jens Axboe906c8d72006-06-13 09:37:56 +0200321/*
322 * Return a free job structure.
323 */
Steven Langde890a12011-11-09 14:03:34 +0100324static struct thread_data *get_new_job(int global, struct thread_data *parent,
325 int preserve_eo)
Jens Axboeebac4652005-12-08 15:25:21 +0100326{
327 struct thread_data *td;
328
329 if (global)
330 return &def_thread;
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200331 if (setup_thread_area()) {
332 log_err("error: failed to setup shm segment\n");
333 return NULL;
334 }
Bruce Crane61f1ec2011-01-14 18:30:26 +0100335 if (thread_number >= max_jobs) {
336 log_err("error: maximum number of jobs (%d) reached.\n",
337 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100338 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100339 }
Jens Axboeebac4652005-12-08 15:25:21 +0100340
341 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200342 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100343
Steven Langde890a12011-11-09 14:03:34 +0100344 td->io_ops = NULL;
345 if (!preserve_eo)
346 td->eo = NULL;
347
Jens Axboee0b0d892009-12-08 10:10:14 +0100348 td->o.uid = td->o.gid = -1U;
349
Jens Axboecade3ef2007-03-23 15:29:45 +0100350 dup_files(td, parent);
Steven Langde890a12011-11-09 14:03:34 +0100351 fio_options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100352
Jens Axboe15dc1932010-03-05 10:59:06 +0100353 profile_add_hooks(td);
354
Jens Axboeebac4652005-12-08 15:25:21 +0100355 td->thread_number = thread_number;
Jens Axboe108fea72012-11-14 13:09:45 -0700356
357 if (!parent || !parent->o.group_reporting)
358 stat_number++;
359
Jens Axboeebac4652005-12-08 15:25:21 +0100360 return td;
361}
362
363static void put_job(struct thread_data *td)
364{
Jens Axboe549577a2006-10-30 12:23:41 +0100365 if (td == &def_thread)
366 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600367
Jens Axboe58c55ba2010-03-09 12:20:08 +0100368 profile_td_exit(td);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100369 flow_exit_job(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100370
Jens Axboe16edf252007-02-10 14:59:22 +0100371 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100372 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100373
Jens Axboe7e356b22011-10-14 10:55:16 +0200374 fio_options_free(td);
Steven Langde890a12011-11-09 14:03:34 +0100375 if (td->io_ops)
376 free_ioengine(td);
Jens Axboe7e356b22011-10-14 10:55:16 +0200377
Jens Axboeebac4652005-12-08 15:25:21 +0100378 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
379 thread_number--;
380}
381
Jens Axboe581e7142009-06-09 12:47:16 +0200382static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100383{
Jens Axboe581e7142009-06-09 12:47:16 +0200384 unsigned int bs = td->o.min_bs[ddir];
Jens Axboe127f6862007-03-15 12:09:57 +0100385
Jens Axboeff58fce2010-08-25 12:02:08 +0200386 assert(ddir_rw(ddir));
387
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100388 if (td->o.rate[ddir])
Steven Lang1b8dbf22011-11-09 13:48:01 +0100389 td->rate_bps[ddir] = td->o.rate[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100390 else
Steven Lang1b8dbf22011-11-09 13:48:01 +0100391 td->rate_bps[ddir] = td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100392
Steven Lang1b8dbf22011-11-09 13:48:01 +0100393 if (!td->rate_bps[ddir]) {
Jens Axboe127f6862007-03-15 12:09:57 +0100394 log_err("rate lower than supported\n");
395 return -1;
396 }
397
Jens Axboe581e7142009-06-09 12:47:16 +0200398 td->rate_pending_usleep[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100399 return 0;
400}
401
Jens Axboe581e7142009-06-09 12:47:16 +0200402static int setup_rate(struct thread_data *td)
403{
404 int ret = 0;
405
406 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
407 ret = __setup_rate(td, DDIR_READ);
408 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
409 ret |= __setup_rate(td, DDIR_WRITE);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200410 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
411 ret |= __setup_rate(td, DDIR_TRIM);
Jens Axboe581e7142009-06-09 12:47:16 +0200412
413 return ret;
414}
415
Jens Axboe83472392009-02-19 21:32:12 +0100416static int fixed_block_size(struct thread_options *o)
417{
418 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
419 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200420 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
421 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
422 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
Jens Axboe83472392009-02-19 21:32:12 +0100423}
424
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800425
426static unsigned long long get_rand_start_delay(struct thread_data *td)
427{
428 unsigned long long delayrange;
429 unsigned long r;
430
431 delayrange = td->o.start_delay_high - td->o.start_delay;
432
433 if (td->o.use_os_rand) {
434 r = os_random_long(&td->delay_state);
435 delayrange = (unsigned long long) ((double) delayrange * (r / (OS_RAND_MAX + 1.0)));
436 } else {
437 r = __rand(&td->__delay_state);
438 delayrange = (unsigned long long) ((double) delayrange * (r / (FRAND_MAX + 1.0)));
439 }
440
441 delayrange += td->o.start_delay;
442 return delayrange;
443}
444
Jens Axboedad915e2006-10-27 11:10:18 +0200445/*
446 * Lazy way of fixing up options that depend on each other. We could also
447 * define option callback handlers, but this is easier.
448 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100449static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200450{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100451 struct thread_options *o = &td->o;
Jens Axboeeefd98b2013-07-25 12:30:06 -0600452 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200453
Jens Axboef356d012009-01-05 09:56:29 +0100454#ifndef FIO_HAVE_PSHARED_MUTEX
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100455 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100456 log_info("fio: this platform does not support process shared"
457 " mutexes, forcing use of threads. Use the 'thread'"
458 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100459 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700460 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100461 }
462#endif
463
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100464 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200465 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100466 free(o->write_iolog_file);
467 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700468 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200469 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100470
Jens Axboe16b462a2006-10-30 12:35:18 +0100471 /*
Steven Noonaned335852012-01-31 13:58:00 +0100472 * only really works with 1 file
Jens Axboe16b462a2006-10-30 12:35:18 +0100473 */
Jens Axboe627aa1a2013-02-07 13:06:18 +0100474 if (o->zone_size && o->open_files > 1)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100475 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100476
477 /*
Steven Noonaned335852012-01-31 13:58:00 +0100478 * If zone_range isn't specified, backward compatibility dictates it
479 * should be made equal to zone_size.
480 */
481 if (o->zone_size && !o->zone_range)
482 o->zone_range = o->zone_size;
483
484 /*
Jens Axboe16b462a2006-10-30 12:35:18 +0100485 * Reads can do overwrites, we always need to pre-create the file
486 */
487 if (td_read(td) || td_rw(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100488 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100489
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100490 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100491 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100492 if (!o->max_bs[DDIR_READ])
493 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
494 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100495 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100496 if (!o->max_bs[DDIR_WRITE])
497 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200498 if (!o->min_bs[DDIR_TRIM])
499 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
500 if (!o->max_bs[DDIR_TRIM])
501 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
502
Jens Axboea00735e2006-11-03 08:58:08 +0100503
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100504 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200505 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
Jens Axboea00735e2006-11-03 08:58:08 +0100506
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100507 /*
508 * For random IO, allow blockalign offset other than min_bs.
509 */
510 if (!o->ba[DDIR_READ] || !td_random(td))
511 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
512 if (!o->ba[DDIR_WRITE] || !td_random(td))
513 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200514 if (!o->ba[DDIR_TRIM] || !td_random(td))
515 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100516
517 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200518 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
519 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100520 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100521 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100522 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700523 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100524 }
525
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100526 if (!o->file_size_high)
527 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100528
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800529 if (o->start_delay_high)
530 o->start_delay = get_rand_start_delay(td);
531
Jens Axboe83472392009-02-19 21:32:12 +0100532 if (o->norandommap && o->verify != VERIFY_NONE
533 && !fixed_block_size(o)) {
534 log_err("fio: norandommap given for variable block sizes, "
535 "verify disabled\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100536 o->verify = VERIFY_NONE;
Jens Axboea9523c62011-01-17 16:49:54 -0700537 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100538 }
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100539 if (o->bs_unaligned && (o->odirect || td->io_ops->flags & FIO_RAWIO))
Jens Axboe690adba2006-10-30 15:25:09 +0100540 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100541
542 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100543 * thinktime_spin must be less than thinktime
544 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100545 if (o->thinktime_spin > o->thinktime)
546 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100547
548 /*
549 * The low water mark cannot be bigger than the iodepth
550 */
Jens Axboe67bf9822013-01-10 11:23:19 +0100551 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
552 o->iodepth_low = o->iodepth;
Jens Axboecb5ab512007-02-26 12:57:09 +0100553
554 /*
555 * If batch number isn't set, default to the same as iodepth
556 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100557 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
558 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100559
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100560 if (o->nr_files > td->files_index)
561 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100562
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100563 if (o->open_files > o->nr_files || !o->open_files)
564 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100565
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200566 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
567 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
568 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
569 (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 +0100570 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700571 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100572 }
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200573 if ((o->rate[DDIR_READ] < o->ratemin[DDIR_READ]) ||
574 (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE]) ||
575 (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM]) ||
576 (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ]) ||
577 (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE]) ||
578 (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM])) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100579 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700580 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100581 }
582
Jens Axboecf4464c2007-04-17 20:14:42 +0200583 if (!o->timeout && o->time_based) {
584 log_err("fio: time_based requires a runtime/timeout setting\n");
585 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700586 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200587 }
588
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100589 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200590 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100591
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100592 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700593 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700594 log_info("Multiple writers may overwrite blocks that "
595 "belong to other jobs. This can cause "
596 "verification failures.\n");
597 ret = warnings_fatal;
598 }
599
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100600 o->refill_buffers = 1;
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200601 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
602 !o->verify_interval)
603 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboed9905882010-03-18 20:43:00 +0100604 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200605
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100606 if (o->pre_read) {
607 o->invalidate_cache = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700608 if (td->io_ops->flags & FIO_PIPEIO) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200609 log_info("fio: cannot pre-read files with an IO engine"
610 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700611 ret = warnings_fatal;
612 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200613 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200614
Steven Noonanad705bc2013-04-08 15:05:25 -0700615 if (!o->unit_base) {
616 if (td->io_ops->flags & FIO_BIT_BASED)
617 o->unit_base = 1;
618 else
619 o->unit_base = 8;
620 }
621
Jens Axboe67bf9822013-01-10 11:23:19 +0100622#ifndef CONFIG_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100623 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700624 log_info("fio: this platform does not support fdatasync()"
625 " falling back to using fsync(). Use the 'fsync'"
626 " option instead of 'fdatasync' to get rid of"
627 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100628 o->fsync_blocks = o->fdatasync_blocks;
629 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700630 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700631 }
632#endif
633
Bruce Cran93bcfd22012-02-20 20:18:19 +0100634#ifdef WIN32
635 /*
636 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
637 * so fail if we're passed those flags
638 */
639 if ((td->io_ops->flags & FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
640 log_err("fio: Windows does not support direct or non-buffered io with"
641 " the synchronous ioengines. Use the 'windowsaio' ioengine"
642 " with 'direct=1' and 'iodepth=1' instead.\n");
643 ret = 1;
644 }
645#endif
646
Jens Axboe811ac502012-03-02 22:23:36 +0100647 /*
648 * For fully compressible data, just zero them at init time.
649 * It's faster than repeatedly filling it.
650 */
651 if (td->o.compress_percentage == 100) {
652 td->o.zero_buffers = 1;
653 td->o.compress_percentage = 0;
654 }
655
Jens Axboe5881fda2012-11-15 15:21:23 -0700656 /*
657 * Using a non-uniform random distribution excludes usage of
658 * a random map
659 */
660 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
661 td->o.norandommap = 1;
662
Jens Axboe8d916c92013-04-25 10:11:41 -0600663 /*
664 * If size is set but less than the min block size, complain
665 */
666 if (o->size && o->size < td_min_bs(td)) {
667 log_err("fio: size too small, must be larger than the IO size: %llu\n", (unsigned long long) o->size);
668 ret = 1;
669 }
670
Chris Masond01612f2013-11-15 15:52:58 -0700671 /*
672 * O_ATOMIC implies O_DIRECT
673 */
674 if (td->o.oatomic)
675 td->o.odirect = 1;
676
Jens Axboe04778ba2014-01-10 20:57:01 -0700677 /*
678 * If randseed is set, that overrides randrepeat
679 */
680 if (td->o.rand_seed)
681 td->o.rand_repeatable = 0;
682
Jens Axboeb1bebc32014-03-18 13:30:32 -0600683 if ((td->io_ops->flags & FIO_NOEXTEND) && td->o.file_append) {
684 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
685 ret = 1;
686 }
687
Jens Axboea9523c62011-01-17 16:49:54 -0700688 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200689}
690
Jens Axboe906c8d72006-06-13 09:37:56 +0200691/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100692 * This function leaks the buffer
693 */
Jens Axboe99d633a2012-03-15 15:55:04 +0100694char *fio_uint_to_kmg(unsigned int val)
Jens Axboef8977ee2006-11-06 14:03:03 +0100695{
696 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100697 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100698 char *p = post;
699
Jens Axboe245142f2006-11-08 12:49:21 +0100700 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100701 if (val & 1023)
702 break;
703
704 val >>= 10;
705 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100706 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100707
Ken Raeburn98ffb8f2013-01-30 22:31:09 +0100708 snprintf(buf, 32, "%u%c", val, *p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100709 return buf;
710}
711
Jens Axboe09629a92007-03-09 09:00:06 +0100712/* External engines are specified by "external:name.o") */
713static const char *get_engine_name(const char *str)
714{
715 char *p = strstr(str, ":");
716
717 if (!p)
718 return str;
719
720 p++;
721 strip_blank_front(&p);
722 strip_blank_end(p);
723 return p;
724}
725
Jens Axboee132cba2007-03-14 14:23:54 +0100726static int exists_and_not_file(const char *filename)
727{
728 struct stat sb;
729
730 if (lstat(filename, &sb) == -1)
731 return 0;
732
Bruce Cranecc314b2011-01-04 10:59:30 +0100733 /* \\.\ is the device namespace in Windows, where every file
734 * is a device node */
735 if (S_ISREG(sb.st_mode) && strncmp(filename, "\\\\.\\", 4) != 0)
Jens Axboee132cba2007-03-14 14:23:54 +0100736 return 0;
737
738 return 1;
739}
740
Jens Axboe4c07ad82011-03-28 09:51:09 +0200741static void td_fill_rand_seeds_os(struct thread_data *td)
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200742{
Jens Axboed24945f2012-12-04 13:10:29 +0100743 os_random_seed(td->rand_seeds[FIO_RAND_BS_OFF], &td->bsrange_state);
744 os_random_seed(td->rand_seeds[FIO_RAND_VER_OFF], &td->verify_state);
745 os_random_seed(td->rand_seeds[FIO_RAND_MIX_OFF], &td->rwmix_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200746
747 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100748 os_random_seed(td->rand_seeds[FIO_RAND_FILE_OFF], &td->next_file_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200749
Jens Axboed24945f2012-12-04 13:10:29 +0100750 os_random_seed(td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], &td->file_size_state);
751 os_random_seed(td->rand_seeds[FIO_RAND_TRIM_OFF], &td->trim_state);
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800752 os_random_seed(td->rand_seeds[FIO_RAND_START_DELAY], &td->delay_state);
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200753
754 if (!td_random(td))
755 return;
756
757 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100758 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200759
Jens Axboed24945f2012-12-04 13:10:29 +0100760 os_random_seed(td->rand_seeds[FIO_RAND_BLOCK_OFF], &td->random_state);
Jens Axboed9472272013-07-25 10:20:45 -0600761
762 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], &td->seq_rand_state[DDIR_READ]);
763 os_random_seed(td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF], &td->seq_rand_state[DDIR_WRITE]);
764 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 +0200765}
766
767static void td_fill_rand_seeds_internal(struct thread_data *td)
768{
Jens Axboed24945f2012-12-04 13:10:29 +0100769 init_rand_seed(&td->__bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF]);
770 init_rand_seed(&td->__verify_state, td->rand_seeds[FIO_RAND_VER_OFF]);
771 init_rand_seed(&td->__rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200772
773 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Jens Axboed24945f2012-12-04 13:10:29 +0100774 init_rand_seed(&td->__next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200775
Jens Axboed24945f2012-12-04 13:10:29 +0100776 init_rand_seed(&td->__file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF]);
777 init_rand_seed(&td->__trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF]);
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800778 init_rand_seed(&td->__delay_state, td->rand_seeds[FIO_RAND_START_DELAY]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200779
780 if (!td_random(td))
781 return;
782
783 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100784 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe4c07ad82011-03-28 09:51:09 +0200785
Jens Axboed24945f2012-12-04 13:10:29 +0100786 init_rand_seed(&td->__random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF]);
Jens Axboed9472272013-07-25 10:20:45 -0600787 init_rand_seed(&td->__seq_rand_state[DDIR_READ], td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF]);
788 init_rand_seed(&td->__seq_rand_state[DDIR_WRITE], td->rand_seeds[FIO_RAND_SEQ_RAND_WRITE_OFF]);
789 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 +0200790}
791
Jens Axboe4c07ad82011-03-28 09:51:09 +0200792void td_fill_rand_seeds(struct thread_data *td)
793{
Christian Ehrhardt56e2a5f2014-02-20 09:10:17 -0800794 if (td->o.allrand_repeatable) {
795 for (int i = 0; i < FIO_RAND_NR_OFFS; i++)
796 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
797 + i;
798 }
799
Jens Axboe4c07ad82011-03-28 09:51:09 +0200800 if (td->o.use_os_rand)
801 td_fill_rand_seeds_os(td);
802 else
803 td_fill_rand_seeds_internal(td);
Jens Axboe3545a102011-08-31 15:20:15 -0600804
Jens Axboed24945f2012-12-04 13:10:29 +0100805 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF]);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200806}
807
Jens Axboe9c60ce62007-03-15 09:14:47 +0100808/*
Steven Langde890a12011-11-09 14:03:34 +0100809 * Initializes the ioengine configured for a job, if it has not been done so
810 * already.
811 */
812int ioengine_load(struct thread_data *td)
813{
814 const char *engine;
815
816 /*
817 * Engine has already been loaded.
818 */
819 if (td->io_ops)
820 return 0;
Jens Axboe6e5c4b82013-05-27 09:50:31 +0200821 if (!td->o.ioengine) {
822 log_err("fio: internal fault, no IO engine specified\n");
823 return 1;
824 }
Steven Langde890a12011-11-09 14:03:34 +0100825
826 engine = get_engine_name(td->o.ioengine);
827 td->io_ops = load_ioengine(td, engine);
828 if (!td->io_ops) {
829 log_err("fio: failed to load engine %s\n", engine);
830 return 1;
831 }
832
833 if (td->io_ops->option_struct_size && td->io_ops->options) {
834 /*
835 * In cases where td->eo is set, clone it for a child thread.
836 * This requires that the parent thread has the same ioengine,
837 * but that requirement must be enforced by the code which
838 * cloned the thread.
839 */
840 void *origeo = td->eo;
841 /*
842 * Otherwise use the default thread options.
843 */
844 if (!origeo && td != &def_thread && def_thread.eo &&
845 def_thread.io_ops->options == td->io_ops->options)
846 origeo = def_thread.eo;
847
848 options_init(td->io_ops->options);
849 td->eo = malloc(td->io_ops->option_struct_size);
850 /*
851 * Use the default thread as an option template if this uses the
852 * same options structure and there are non-default options
853 * used.
854 */
855 if (origeo) {
856 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
857 options_mem_dupe(td->eo, td->io_ops->options);
858 } else {
859 memset(td->eo, 0, td->io_ops->option_struct_size);
860 fill_default_options(td->eo, td->io_ops->options);
861 }
862 *(struct thread_data **)td->eo = td;
863 }
864
865 return 0;
866}
867
Jens Axboed72be542012-11-30 19:37:46 +0100868static void init_flags(struct thread_data *td)
869{
870 struct thread_options *o = &td->o;
871
872 if (o->verify_backlog)
873 td->flags |= TD_F_VER_BACKLOG;
874 if (o->trim_backlog)
875 td->flags |= TD_F_TRIM_BACKLOG;
876 if (o->read_iolog_file)
877 td->flags |= TD_F_READ_IOLOG;
878 if (o->refill_buffers)
879 td->flags |= TD_F_REFILL_BUFFERS;
Jens Axboebedc9dc2014-03-17 12:51:09 -0600880 if (o->scramble_buffers)
Jens Axboed72be542012-11-30 19:37:46 +0100881 td->flags |= TD_F_SCRAMBLE_BUFFERS;
882 if (o->verify != VERIFY_NONE)
883 td->flags |= TD_F_VER_NONE;
884}
885
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700886static int setup_random_seeds(struct thread_data *td)
887{
888 unsigned long seed;
889 unsigned int i;
890
Jens Axboe04778ba2014-01-10 20:57:01 -0700891 if (!td->o.rand_repeatable && !td->o.rand_seed)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700892 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
893
Jens Axboe04778ba2014-01-10 20:57:01 -0700894 if (!td->o.rand_seed)
895 seed = 0x89;
896 else
897 seed = td->o.rand_seed;
898
899 for (i = 0; i < 4; i++)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -0700900 seed *= 0x9e370001UL;
901
902 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
903 td->rand_seeds[i] = seed;
904 seed *= 0x9e370001UL;
905 }
906
907 td_fill_rand_seeds(td);
908 return 0;
909}
910
Jens Axboede98bd32013-04-05 11:09:20 +0200911enum {
912 FPRE_NONE = 0,
913 FPRE_JOBNAME,
914 FPRE_JOBNUM,
915 FPRE_FILENUM
916};
917
918static struct fpre_keyword {
919 const char *keyword;
920 size_t strlen;
921 int key;
922} fpre_keywords[] = {
923 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
924 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
925 { .keyword = "$filenum", .key = FPRE_FILENUM, },
926 { .keyword = NULL, },
927 };
928
929static char *make_filename(char *buf, struct thread_options *o,
930 const char *jobname, int jobnum, int filenum)
931{
932 struct fpre_keyword *f;
933 char copy[PATH_MAX];
934
935 if (!o->filename_format || !strlen(o->filename_format)) {
936 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
937 return NULL;
938 }
939
940 for (f = &fpre_keywords[0]; f->keyword; f++)
941 f->strlen = strlen(f->keyword);
942
943 strcpy(buf, o->filename_format);
944 memset(copy, 0, sizeof(copy));
945 for (f = &fpre_keywords[0]; f->keyword; f++) {
946 do {
947 size_t pre_len, post_start = 0;
948 char *str, *dst = copy;
949
Jens Axboee25b6d52013-04-05 14:44:51 +0200950 str = strcasestr(buf, f->keyword);
Jens Axboede98bd32013-04-05 11:09:20 +0200951 if (!str)
952 break;
953
954 pre_len = str - buf;
955 if (strlen(str) != f->strlen)
956 post_start = pre_len + f->strlen;
957
958 if (pre_len) {
959 strncpy(dst, buf, pre_len);
960 dst += pre_len;
961 }
962
963 switch (f->key) {
964 case FPRE_JOBNAME:
965 dst += sprintf(dst, "%s", jobname);
966 break;
967 case FPRE_JOBNUM:
968 dst += sprintf(dst, "%d", jobnum);
969 break;
970 case FPRE_FILENUM:
971 dst += sprintf(dst, "%d", filenum);
972 break;
973 default:
974 assert(0);
975 break;
976 }
977
978 if (post_start)
979 strcpy(dst, buf + post_start);
980
981 strcpy(buf, copy);
982 } while (1);
983 }
984
985 return buf;
986}
Jens Axboef9633d72013-09-06 09:59:56 -0600987
988int parse_dryrun(void)
989{
990 return dump_cmdline || parse_only;
991}
992
Steven Langde890a12011-11-09 14:03:34 +0100993/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200994 * Adds a job to the list of things todo. Sanitizes the various options
995 * to make sure we don't have conflicts, and initializes various
996 * members of td.
997 */
Jens Axboeb7cfb2e2012-03-12 07:47:27 +0100998static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
Jens Axboe46bcd492012-03-14 11:31:21 +0100999 int recursed, int client_type)
Jens Axboeebac4652005-12-08 15:25:21 +01001000{
Jens Axboeaf52b342007-03-13 10:07:47 +01001001 unsigned int i;
Jens Axboeaf52b342007-03-13 10:07:47 +01001002 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +01001003 int numjobs, file_alloced;
Jens Axboede98bd32013-04-05 11:09:20 +02001004 struct thread_options *o = &td->o;
Jens Axboeebac4652005-12-08 15:25:21 +01001005
Jens Axboeebac4652005-12-08 15:25:21 +01001006 /*
1007 * the def_thread is just for options, it's not a real job
1008 */
1009 if (td == &def_thread)
1010 return 0;
1011
Jens Axboed72be542012-11-30 19:37:46 +01001012 init_flags(td);
1013
Jens Axboecca73aa2007-04-04 11:09:19 +02001014 /*
1015 * if we are just dumping the output command line, don't add the job
1016 */
Jens Axboef9633d72013-09-06 09:59:56 -06001017 if (parse_dryrun()) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001018 put_job(td);
1019 return 0;
1020 }
1021
Jens Axboe46bcd492012-03-14 11:31:21 +01001022 td->client_type = client_type;
1023
Jens Axboe58c55ba2010-03-09 12:20:08 +01001024 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +02001025 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +01001026
Steven Langde890a12011-11-09 14:03:34 +01001027 if (ioengine_load(td))
Jens Axboe205927a2007-03-15 11:06:32 +01001028 goto err;
Jens Axboedf641192006-10-12 07:55:41 +02001029
Jens Axboede98bd32013-04-05 11:09:20 +02001030 if (o->odirect)
Jens Axboe690adba2006-10-30 15:25:09 +01001031 td->io_ops->flags |= FIO_RAWIO;
1032
Jens Axboee132cba2007-03-14 14:23:54 +01001033 file_alloced = 0;
Jens Axboede98bd32013-04-05 11:09:20 +02001034 if (!o->filename && !td->files_index && !o->read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +01001035 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +01001036
Jens Axboede98bd32013-04-05 11:09:20 +02001037 if (o->nr_files == 1 && exists_and_not_file(jobname))
Jens Axboe5903e7b2014-02-26 13:42:13 -08001038 add_file(td, jobname, job_add_num, 0);
Jens Axboe7b05a212007-03-13 11:17:07 +01001039 else {
Jens Axboede98bd32013-04-05 11:09:20 +02001040 for (i = 0; i < o->nr_files; i++)
Jens Axboe5903e7b2014-02-26 13:42:13 -08001041 add_file(td, make_filename(fname, o, jobname, job_add_num, i), job_add_num, 0);
Jens Axboeaf52b342007-03-13 10:07:47 +01001042 }
Jens Axboe0af7b542006-02-17 10:10:12 +01001043 }
Jens Axboeebac4652005-12-08 15:25:21 +01001044
Jens Axboe4e991c22007-03-15 11:41:11 +01001045 if (fixup_options(td))
1046 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +01001047
Dan Ehrenberg9e684a42012-02-20 11:05:14 +01001048 flow_init_job(td);
1049
Steven Langde890a12011-11-09 14:03:34 +01001050 /*
1051 * IO engines only need this for option callbacks, and the address may
1052 * change in subprocesses.
1053 */
1054 if (td->eo)
1055 *(struct thread_data **)td->eo = NULL;
1056
Jens Axboe07eb79d2007-04-12 13:02:38 +02001057 if (td->io_ops->flags & FIO_DISKLESSIO) {
1058 struct fio_file *f;
1059
1060 for_each_file(td, f, i)
1061 f->real_file_size = -1ULL;
1062 }
1063
Jens Axboe521da522012-08-02 11:21:36 +02001064 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboeebac4652005-12-08 15:25:21 +01001065
Jens Axboede98bd32013-04-05 11:09:20 +02001066 td->ts.clat_percentiles = o->clat_percentiles;
1067 td->ts.percentile_precision = o->percentile_precision;
1068 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
Yu-ju Hong83349192011-08-13 00:53:44 +02001069
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001070 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1071 td->ts.clat_stat[i].min_val = ULONG_MAX;
1072 td->ts.slat_stat[i].min_val = ULONG_MAX;
1073 td->ts.lat_stat[i].min_val = ULONG_MAX;
1074 td->ts.bw_stat[i].min_val = ULONG_MAX;
1075 }
Jens Axboede98bd32013-04-05 11:09:20 +02001076 td->ddir_seq_nr = o->ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +01001077
Jens Axboede98bd32013-04-05 11:09:20 +02001078 if ((o->stonewall || o->new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001079 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001080 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001081 }
Jens Axboeebac4652005-12-08 15:25:21 +01001082
1083 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001084 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +01001085
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001086 if (setup_random_seeds(td)) {
Bruce Cran93bcfd22012-02-20 20:18:19 +01001087 td_verror(td, errno, "init_random_state");
Jens Axboe9c60ce62007-03-15 09:14:47 +01001088 goto err;
Bruce Cran93bcfd22012-02-20 20:18:19 +01001089 }
Jens Axboe9c60ce62007-03-15 09:14:47 +01001090
Jens Axboeebac4652005-12-08 15:25:21 +01001091 if (setup_rate(td))
1092 goto err;
1093
Jens Axboe8e255772014-04-01 13:34:49 -06001094 if (o->lat_log_file || write_lat_log) {
Jens Axboe22f80452013-04-09 20:29:16 +02001095 setup_log(&td->lat_log, o->log_avg_msec, IO_LOG_TYPE_LAT);
1096 setup_log(&td->slat_log, o->log_avg_msec, IO_LOG_TYPE_SLAT);
1097 setup_log(&td->clat_log, o->log_avg_msec, IO_LOG_TYPE_CLAT);
Jens Axboeebac4652005-12-08 15:25:21 +01001098 }
Jens Axboe8e255772014-04-01 13:34:49 -06001099 if (o->bw_log_file || write_bw_log)
Jens Axboe22f80452013-04-09 20:29:16 +02001100 setup_log(&td->bw_log, o->log_avg_msec, IO_LOG_TYPE_BW);
1101 if (o->iops_log_file)
1102 setup_log(&td->iops_log, o->log_avg_msec, IO_LOG_TYPE_IOPS);
Jens Axboeebac4652005-12-08 15:25:21 +01001103
Jens Axboede98bd32013-04-05 11:09:20 +02001104 if (!o->name)
1105 o->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +02001106
Jens Axboef3afa572012-09-17 13:34:16 +02001107 if (output_format == FIO_OUTPUT_NORMAL) {
Jens Axboeb990b5c2006-09-14 09:48:22 +02001108 if (!job_add_num) {
Jens Axboeb7cfb2e2012-03-12 07:47:27 +01001109 if (is_backend && !recursed)
Jens Axboe2f122b12012-03-15 13:10:19 +01001110 fio_server_send_add_job(td);
Jens Axboe807f9972012-03-02 10:25:24 +01001111
Jens Axboe03530502012-03-19 21:45:12 +01001112 if (!(td->io_ops->flags & FIO_NOIO)) {
Jens Axboed21e2532013-07-25 13:01:11 -06001113 char *c1, *c2, *c3, *c4;
1114 char *c5 = NULL, *c6 = NULL;
Jens Axboef8977ee2006-11-06 14:03:03 +01001115
Jens Axboe22f80452013-04-09 20:29:16 +02001116 c1 = fio_uint_to_kmg(o->min_bs[DDIR_READ]);
1117 c2 = fio_uint_to_kmg(o->max_bs[DDIR_READ]);
1118 c3 = fio_uint_to_kmg(o->min_bs[DDIR_WRITE]);
1119 c4 = fio_uint_to_kmg(o->max_bs[DDIR_WRITE]);
Jens Axboef8977ee2006-11-06 14:03:03 +01001120
Jens Axboed21e2532013-07-25 13:01:11 -06001121 if (!o->bs_is_seq_rand) {
1122 c5 = fio_uint_to_kmg(o->min_bs[DDIR_TRIM]);
1123 c6 = fio_uint_to_kmg(o->max_bs[DDIR_TRIM]);
1124 }
1125
1126 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1127 td->groupid,
1128 ddir_str(o->td_ddir));
1129
1130 if (o->bs_is_seq_rand)
1131 log_info("bs(seq/rand)=%s-%s/%s-%s, ",
1132 c1, c2, c3, c4);
1133 else
1134 log_info("bs=%s-%s/%s-%s/%s-%s, ",
1135 c1, c2, c3, c4, c5, c6);
1136
1137 log_info("ioengine=%s, iodepth=%u\n",
Jens Axboede98bd32013-04-05 11:09:20 +02001138 td->io_ops->name, o->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +01001139
1140 free(c1);
1141 free(c2);
1142 free(c3);
1143 free(c4);
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001144 free(c5);
1145 free(c6);
Jens Axboef8977ee2006-11-06 14:03:03 +01001146 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001147 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +01001148 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001149 }
Jens Axboeebac4652005-12-08 15:25:21 +01001150
1151 /*
1152 * recurse add identical jobs, clear numjobs and stonewall options
1153 * as they don't apply to sub-jobs
1154 */
Jens Axboede98bd32013-04-05 11:09:20 +02001155 numjobs = o->numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +01001156 while (--numjobs) {
Steven Langde890a12011-11-09 14:03:34 +01001157 struct thread_data *td_new = get_new_job(0, td, 1);
Jens Axboeebac4652005-12-08 15:25:21 +01001158
1159 if (!td_new)
1160 goto err;
1161
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001162 td_new->o.numjobs = 1;
1163 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +02001164 td_new->o.new_group = 0;
Jens Axboee132cba2007-03-14 14:23:54 +01001165
1166 if (file_alloced) {
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001167 if (td_new->files) {
1168 struct fio_file *f;
1169 for_each_file(td_new, f, i) {
1170 if (f->file_name)
Andrey Kuzminece6d642014-04-01 12:27:04 -06001171 sfree(f->file_name);
1172 sfree(f);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001173 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001174 free(td_new->files);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001175 td_new->files = NULL;
1176 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001177 td_new->files_index = 0;
1178 td_new->files_size = 0;
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001179 if (td_new->o.filename) {
1180 free(td_new->o.filename);
1181 td_new->o.filename = NULL;
1182 }
Jens Axboee132cba2007-03-14 14:23:54 +01001183 }
1184
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001185 if (add_job(td_new, jobname, numjobs, 1, client_type))
Jens Axboeebac4652005-12-08 15:25:21 +01001186 goto err;
1187 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001188
Jens Axboeebac4652005-12-08 15:25:21 +01001189 return 0;
1190err:
1191 put_job(td);
1192 return -1;
1193}
1194
Jens Axboe79d16312010-03-04 12:43:20 +01001195/*
1196 * Parse as if 'o' was a command line
1197 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001198void add_job_opts(const char **o, int client_type)
Jens Axboe79d16312010-03-04 12:43:20 +01001199{
1200 struct thread_data *td, *td_parent;
1201 int i, in_global = 1;
1202 char jobname[32];
1203
1204 i = 0;
1205 td_parent = td = NULL;
1206 while (o[i]) {
1207 if (!strncmp(o[i], "name", 4)) {
1208 in_global = 0;
1209 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001210 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001211 td = NULL;
1212 sprintf(jobname, "%s", o[i] + 5);
1213 }
1214 if (in_global && !td_parent)
Steven Langde890a12011-11-09 14:03:34 +01001215 td_parent = get_new_job(1, &def_thread, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001216 else if (!in_global && !td) {
1217 if (!td_parent)
1218 td_parent = &def_thread;
Steven Langde890a12011-11-09 14:03:34 +01001219 td = get_new_job(0, td_parent, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001220 }
1221 if (in_global)
Jens Axboe292cc472013-11-26 20:39:35 -07001222 fio_options_parse(td_parent, (char **) &o[i], 1, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001223 else
Jens Axboe292cc472013-11-26 20:39:35 -07001224 fio_options_parse(td, (char **) &o[i], 1, 0);
Jens Axboe79d16312010-03-04 12:43:20 +01001225 i++;
1226 }
1227
1228 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001229 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001230}
1231
Jens Axboe01f06b62008-02-18 20:53:47 +01001232static int skip_this_section(const char *name)
1233{
Jens Axboead0a2732011-03-11 10:16:17 +01001234 int i;
1235
1236 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +01001237 return 0;
1238 if (!strncmp(name, "global", 6))
1239 return 0;
1240
Jens Axboead0a2732011-03-11 10:16:17 +01001241 for (i = 0; i < nr_job_sections; i++)
1242 if (!strcmp(job_sections[i], name))
1243 return 0;
1244
1245 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001246}
1247
Jens Axboeebac4652005-12-08 15:25:21 +01001248static int is_empty_or_comment(char *line)
1249{
1250 unsigned int i;
1251
1252 for (i = 0; i < strlen(line); i++) {
1253 if (line[i] == ';')
1254 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001255 if (line[i] == '#')
1256 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +02001257 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +01001258 return 0;
1259 }
1260
1261 return 1;
1262}
1263
Jens Axboe313cb202006-12-21 09:50:00 +01001264/*
Jens Axboe07261982006-06-07 10:51:12 +02001265 * This is our [ini] type file parser.
1266 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001267int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
Jens Axboeebac4652005-12-08 15:25:21 +01001268{
Jens Axboee1f36502006-10-27 10:54:08 +02001269 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001270 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001271 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001272 FILE *f;
1273 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001274 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +02001275 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +02001276 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +01001277 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001278 char **opts;
1279 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +01001280
Jens Axboe50d16972011-09-29 17:45:28 -06001281 if (is_buf)
1282 f = NULL;
1283 else {
1284 if (!strcmp(file, "-"))
1285 f = stdin;
1286 else
1287 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001288
Jens Axboe50d16972011-09-29 17:45:28 -06001289 if (!f) {
1290 perror("fopen job file");
1291 return 1;
1292 }
Jens Axboeebac4652005-12-08 15:25:21 +01001293 }
1294
1295 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +02001296
1297 /*
1298 * it's really 256 + small bit, 280 should suffice
1299 */
1300 name = malloc(280);
1301 memset(name, 0, 280);
Jens Axboeebac4652005-12-08 15:25:21 +01001302
Jens Axboe3b8b7132008-06-10 19:46:23 +02001303 alloc_opts = 8;
1304 opts = malloc(sizeof(char *) * alloc_opts);
Jens Axboeb7c63022008-06-11 11:47:56 +02001305 num_opts = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001306
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001307 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001308 do {
Jens Axboeccf8f122007-09-27 10:48:55 +02001309 /*
1310 * if skip_fgets is set, we already have loaded a line we
1311 * haven't handled.
1312 */
1313 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -06001314 if (is_buf)
1315 p = strsep(&file, "\n");
1316 else
Jens Axboe43f74792011-10-15 14:37:26 +02001317 p = fgets(string, 4096, f);
Jens Axboeccf8f122007-09-27 10:48:55 +02001318 if (!p)
1319 break;
1320 }
gurudas paicdc7f192007-03-29 10:10:56 +02001321
Jens Axboeccf8f122007-09-27 10:48:55 +02001322 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +02001323 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001324 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +02001325
Jens Axboeebac4652005-12-08 15:25:21 +01001326 if (is_empty_or_comment(p))
1327 continue;
Bruce Cran84dd1882011-05-05 08:14:09 -06001328 if (sscanf(p, "[%255[^\n]]", name) != 1) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001329 if (inside_skip)
1330 continue;
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001331 log_err("fio: option <%s> outside of [] job section\n",
1332 p);
Jens Axboe088b4202007-07-19 14:53:01 +02001333 break;
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001334 }
Jens Axboeebac4652005-12-08 15:25:21 +01001335
Jens Axboe7a4b80a2010-05-22 20:43:11 +02001336 name[strlen(name) - 1] = '\0';
1337
Jens Axboe01f06b62008-02-18 20:53:47 +01001338 if (skip_this_section(name)) {
1339 inside_skip = 1;
1340 continue;
1341 } else
1342 inside_skip = 0;
1343
Jens Axboeebac4652005-12-08 15:25:21 +01001344 global = !strncmp(name, "global", 6);
1345
Jens Axboecca73aa2007-04-04 11:09:19 +02001346 if (dump_cmdline) {
Jens Axboe097b2992007-04-19 09:41:45 +02001347 if (first_sect)
1348 log_info("fio ");
Jens Axboecca73aa2007-04-04 11:09:19 +02001349 if (!global)
1350 log_info("--name=%s ", name);
Jens Axboe097b2992007-04-19 09:41:45 +02001351 first_sect = 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02001352 }
1353
Steven Langde890a12011-11-09 14:03:34 +01001354 td = get_new_job(global, &def_thread, 0);
Jens Axboe45410ac2006-06-07 11:10:39 +02001355 if (!td) {
1356 ret = 1;
1357 break;
1358 }
Jens Axboeebac4652005-12-08 15:25:21 +01001359
Jens Axboe972cfd22006-06-09 11:08:56 +02001360 /*
Anatol Pomozovde8f6de2013-09-26 16:31:34 -07001361 * Separate multiple job files by a stonewall
Jens Axboe972cfd22006-06-09 11:08:56 +02001362 */
Jens Axboef9481912006-06-09 11:37:28 +02001363 if (!global && stonewall) {
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001364 td->o.stonewall = stonewall;
Jens Axboe972cfd22006-06-09 11:08:56 +02001365 stonewall = 0;
1366 }
1367
Jens Axboe3b8b7132008-06-10 19:46:23 +02001368 num_opts = 0;
1369 memset(opts, 0, alloc_opts * sizeof(char *));
1370
Jens Axboe50d16972011-09-29 17:45:28 -06001371 while (1) {
1372 if (is_buf)
1373 p = strsep(&file, "\n");
1374 else
1375 p = fgets(string, 4096, f);
1376 if (!p)
1377 break;
1378
Jens Axboeebac4652005-12-08 15:25:21 +01001379 if (is_empty_or_comment(p))
1380 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001381
Jens Axboeb6754f92006-05-30 14:29:32 +02001382 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001383
Jens Axboeccf8f122007-09-27 10:48:55 +02001384 /*
1385 * new section, break out and make sure we don't
1386 * fgets() a new line at the top.
1387 */
1388 if (p[0] == '[') {
1389 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001390 break;
Jens Axboeccf8f122007-09-27 10:48:55 +02001391 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001392
Jens Axboe4ae3f762006-05-30 13:35:14 +02001393 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001394
Jens Axboe3b8b7132008-06-10 19:46:23 +02001395 if (num_opts == alloc_opts) {
1396 alloc_opts <<= 1;
1397 opts = realloc(opts,
1398 alloc_opts * sizeof(char *));
1399 }
1400
1401 opts[num_opts] = strdup(p);
1402 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +01001403 }
Jens Axboeebac4652005-12-08 15:25:21 +01001404
Jens Axboe292cc472013-11-26 20:39:35 -07001405 ret = fio_options_parse(td, opts, num_opts, dump_cmdline);
1406 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001407 ret = add_job(td, name, 0, 0, type);
Jens Axboe292cc472013-11-26 20:39:35 -07001408 else {
Jens Axboeb1508cf2006-11-02 09:12:40 +01001409 log_err("fio: job %s dropped\n", name);
1410 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001411 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001412
1413 for (i = 0; i < num_opts; i++)
1414 free(opts[i]);
1415 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001416 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001417
Jens Axboecca73aa2007-04-04 11:09:19 +02001418 if (dump_cmdline)
1419 log_info("\n");
1420
Jens Axboe7e356b22011-10-14 10:55:16 +02001421 i = 0;
1422 while (i < nr_job_sections) {
1423 free(job_sections[i]);
1424 i++;
1425 }
1426
Jens Axboe3b8b7132008-06-10 19:46:23 +02001427 for (i = 0; i < num_opts; i++)
1428 free(opts[i]);
1429
Jens Axboeebac4652005-12-08 15:25:21 +01001430 free(string);
1431 free(name);
Jens Axboe25775942008-06-10 20:26:06 +02001432 free(opts);
Jens Axboe50d16972011-09-29 17:45:28 -06001433 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001434 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001435 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001436}
1437
1438static int fill_def_thread(void)
1439{
1440 memset(&def_thread, 0, sizeof(def_thread));
1441
Jens Axboe375b2692007-05-22 09:13:02 +02001442 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Jens Axboe8c029372011-10-05 09:54:30 +02001443 def_thread.o.timeout = def_timeout;
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001444 def_thread.o.error_dump = 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001445 /*
Jens Axboeee738492007-01-10 11:23:16 +01001446 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001447 */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001448 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001449 return 0;
1450}
1451
Jens Axboe45378b32007-12-19 13:54:38 +01001452static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02001453{
Jens Axboe3d433822012-03-21 22:25:22 +01001454 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01001455 printf("%s [options] [job options] <job file(s)>\n", name);
Jens Axboe83881282011-10-17 19:58:51 +02001456 printf(" --debug=options\tEnable debug logging. May be one/more of:\n"
1457 "\t\t\tprocess,file,io,mem,blktrace,verify,random,parse,\n"
Jens Axboe3e260a42013-12-09 12:38:53 -07001458 "\t\t\tdiskutil,job,mutex,profile,time,net,rate\n");
Jens Axboe111e0322013-03-07 11:31:20 +01001459 printf(" --parse-only\t\tParse options only, don't start any IO\n");
Jens Axboe83881282011-10-17 19:58:51 +02001460 printf(" --output\t\tWrite output to file\n");
liang xieb2cecdc2012-08-31 08:22:42 -07001461 printf(" --runtime\t\tRuntime in seconds\n");
Jens Axboe83881282011-10-17 19:58:51 +02001462 printf(" --latency-log\t\tGenerate per-job latency logs\n");
1463 printf(" --bandwidth-log\tGenerate per-job bandwidth logs\n");
1464 printf(" --minimal\t\tMinimal (terse) output\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001465 printf(" --output-format=x\tOutput format (terse,json,normal)\n");
Jens Axboe75db6e22011-11-07 22:07:36 +01001466 printf(" --terse-version=x\tSet terse version output format to 'x'\n");
Jens Axboef3afa572012-09-17 13:34:16 +02001467 printf(" --version\t\tPrint version info and exit\n");
Jens Axboe83881282011-10-17 19:58:51 +02001468 printf(" --help\t\tPrint this page\n");
Jens Axboe23893642012-12-17 14:44:08 +01001469 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
Jens Axboefec0f212014-02-07 14:39:33 -07001470 printf(" --crctest\t\tTest speed of checksum functions\n");
Jens Axboe83881282011-10-17 19:58:51 +02001471 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001472 " them\n");
Steven Langde890a12011-11-09 14:03:34 +01001473 printf(" --enghelp=engine\tPrint ioengine help, or list"
1474 " available ioengines\n");
1475 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
1476 " cmd\n");
Jens Axboe83881282011-10-17 19:58:51 +02001477 printf(" --showcmd\t\tTurn a job file into command line options\n");
1478 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
1479 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboee382e662013-02-22 20:48:56 +01001480 printf(" --eta-newline=time\tForce a new line for every 'time'");
1481 printf(" period passed\n");
Jens Axboe06464902013-04-24 20:38:54 -06001482 printf(" --status-interval=t\tForce full status dump every");
1483 printf(" 't' period passed\n");
Jens Axboe83881282011-10-17 19:58:51 +02001484 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001485 " writes\n");
Jens Axboe83881282011-10-17 19:58:51 +02001486 printf(" --section=name\tOnly run specified section in job file\n");
1487 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
Jens Axboe2b386d22008-03-26 10:32:57 +01001488 " (def 1024)\n");
Jens Axboe83881282011-10-17 19:58:51 +02001489 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
1490 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
1491 printf(" --server=args\t\tStart a backend fio server\n");
1492 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
1493 printf(" --client=hostname\tTalk to remote backend fio server at hostname\n");
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001494 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
1495 "\t\t\t(option=system,percpu) or run unit work\n"
1496 "\t\t\tcalibration only (option=calibrate)\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02001497 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
Jens Axboebfca2c72014-01-29 16:39:04 -07001498 printf("\n Jens Axboe <jaxboe@fusionio.com>");
1499 printf("\n Jens Axboe <axboe@fb.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001500}
1501
Jens Axboe79e48f72008-02-01 20:37:09 +01001502#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01001503struct debug_level debug_levels[] = {
Jens Axboe0b8d11e2012-03-02 19:44:15 +01001504 { .name = "process",
1505 .help = "Process creation/exit logging",
1506 .shift = FD_PROCESS,
1507 },
1508 { .name = "file",
1509 .help = "File related action logging",
1510 .shift = FD_FILE,
1511 },
1512 { .name = "io",
1513 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
1514 .shift = FD_IO,
1515 },
1516 { .name = "mem",
1517 .help = "Memory allocation/freeing logging",
1518 .shift = FD_MEM,
1519 },
1520 { .name = "blktrace",
1521 .help = "blktrace action logging",
1522 .shift = FD_BLKTRACE,
1523 },
1524 { .name = "verify",
1525 .help = "IO verification action logging",
1526 .shift = FD_VERIFY,
1527 },
1528 { .name = "random",
1529 .help = "Random generation logging",
1530 .shift = FD_RANDOM,
1531 },
1532 { .name = "parse",
1533 .help = "Parser logging",
1534 .shift = FD_PARSE,
1535 },
1536 { .name = "diskutil",
1537 .help = "Disk utility logging actions",
1538 .shift = FD_DISKUTIL,
1539 },
1540 { .name = "job",
1541 .help = "Logging related to creating/destroying jobs",
1542 .shift = FD_JOB,
1543 },
1544 { .name = "mutex",
1545 .help = "Mutex logging",
1546 .shift = FD_MUTEX
1547 },
1548 { .name = "profile",
1549 .help = "Logging related to profiles",
1550 .shift = FD_PROFILE,
1551 },
1552 { .name = "time",
1553 .help = "Logging related to time keeping functions",
1554 .shift = FD_TIME,
1555 },
1556 { .name = "net",
1557 .help = "Network logging",
1558 .shift = FD_NET,
1559 },
Jens Axboe3e260a42013-12-09 12:38:53 -07001560 { .name = "rate",
1561 .help = "Rate logging",
1562 .shift = FD_RATE,
1563 },
Jens Axboe02444ad2008-10-07 11:33:00 +02001564 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01001565};
1566
Jens Axboec09823a2008-02-19 20:16:57 +01001567static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01001568{
1569 struct debug_level *dl;
1570 char *p = (char *) string;
1571 char *opt;
1572 int i;
1573
1574 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01001575 log_info("fio: dumping debug options:");
1576 for (i = 0; debug_levels[i].name; i++) {
1577 dl = &debug_levels[i];
1578 log_info("%s,", dl->name);
1579 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01001580 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001581 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01001582 }
1583
1584 while ((opt = strsep(&p, ",")) != NULL) {
1585 int found = 0;
1586
Jens Axboe5e1d3062008-05-23 11:55:53 +02001587 if (!strncmp(opt, "all", 3)) {
1588 log_info("fio: set all debug options\n");
1589 fio_debug = ~0UL;
1590 continue;
1591 }
1592
Jens Axboeee56ad52008-02-01 10:30:20 +01001593 for (i = 0; debug_levels[i].name; i++) {
1594 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02001595 found = !strncmp(opt, dl->name, strlen(dl->name));
1596 if (!found)
1597 continue;
1598
1599 if (dl->shift == FD_JOB) {
1600 opt = strchr(opt, ':');
1601 if (!opt) {
1602 log_err("fio: missing job number\n");
1603 break;
1604 }
1605 opt++;
1606 fio_debug_jobno = atoi(opt);
1607 log_info("fio: set debug jobno %d\n",
1608 fio_debug_jobno);
1609 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01001610 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01001611 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01001612 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02001613 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01001614 }
1615
1616 if (!found)
1617 log_err("fio: debug mask %s not found\n", opt);
1618 }
Jens Axboec09823a2008-02-19 20:16:57 +01001619 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01001620}
Jens Axboe79e48f72008-02-01 20:37:09 +01001621#else
Jens Axboe69b98d42008-05-30 22:25:32 +02001622static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01001623{
1624 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001625 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01001626}
1627#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01001628
Jens Axboe4c6107f2011-01-18 05:22:22 -07001629static void fio_options_fill_optstring(void)
1630{
1631 char *ostr = cmd_optstr;
1632 int i, c;
1633
1634 c = i = 0;
1635 while (l_opts[i].name) {
1636 ostr[c++] = l_opts[i].val;
1637 if (l_opts[i].has_arg == required_argument)
1638 ostr[c++] = ':';
1639 else if (l_opts[i].has_arg == optional_argument) {
1640 ostr[c++] = ':';
1641 ostr[c++] = ':';
1642 }
1643 i++;
1644 }
1645 ostr[c] = '\0';
1646}
1647
Jens Axboec2c94582011-10-05 20:31:30 +02001648static int client_flag_set(char c)
1649{
1650 int i;
1651
1652 i = 0;
1653 while (l_opts[i].name) {
1654 int val = l_opts[i].val;
1655
1656 if (c == (val & 0xff))
1657 return (val & FIO_CLIENT_FLAG);
1658
1659 i++;
1660 }
1661
1662 return 0;
1663}
1664
Jens Axboe10aa1362014-04-01 21:10:36 -06001665static void parse_cmd_client(void *client, char *opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +02001666{
Jens Axboefa2ea802011-10-08 21:07:29 +02001667 fio_client_add_cmd_option(client, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001668}
1669
Jens Axboe46bcd492012-03-14 11:31:21 +01001670int parse_cmd_line(int argc, char *argv[], int client_type)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001671{
1672 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01001673 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07001674 char *ostr = cmd_optstr;
Jens Axboe402668f2011-10-10 15:28:58 +02001675 void *pid_file = NULL;
Jens Axboe9bae26e2011-10-07 10:07:22 +02001676 void *cur_client = NULL;
Jens Axboe81179ee2011-10-04 12:42:06 +02001677 int backend = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001678
Jens Axboec2c94582011-10-05 20:31:30 +02001679 /*
1680 * Reset optind handling, since we may call this multiple times
1681 * for the backend.
1682 */
1683 optind = 1;
Jens Axboe7a4b8242011-10-05 17:35:15 +02001684
Jens Axboec2c94582011-10-05 20:31:30 +02001685 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
Jens Axboe085399d2011-10-06 09:01:22 +02001686 did_arg = 1;
1687
Jens Axboec2c94582011-10-05 20:31:30 +02001688 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
Jens Axboefa2ea802011-10-08 21:07:29 +02001689 parse_cmd_client(cur_client, argv[optind - 1]);
Jens Axboe7a4b8242011-10-05 17:35:15 +02001690 c &= ~FIO_CLIENT_FLAG;
1691 }
1692
Jens Axboe214e1ec2007-03-15 10:48:13 +01001693 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01001694 case 'a':
1695 smalloc_pool_size = atoi(optarg);
1696 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001697 case 't':
Jens Axboe8c029372011-10-05 09:54:30 +02001698 def_timeout = atoi(optarg);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001699 break;
1700 case 'l':
1701 write_lat_log = 1;
1702 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01001703 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01001704 write_bw_log = 1;
1705 break;
1706 case 'o':
1707 f_out = fopen(optarg, "w+");
1708 if (!f_out) {
1709 perror("fopen output");
1710 exit(1);
1711 }
1712 f_err = f_out;
1713 break;
1714 case 'm':
Jens Axboef3afa572012-09-17 13:34:16 +02001715 output_format = FIO_OUTPUT_TERSE;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001716 break;
Jens Axboef3afa572012-09-17 13:34:16 +02001717 case 'F':
Stefan Hajnoczid10983d2014-02-04 14:27:11 +01001718 if (!optarg) {
1719 log_err("fio: missing --output-format argument\n");
1720 exit_val = 1;
1721 do_exit++;
1722 break;
1723 }
Jens Axboef3afa572012-09-17 13:34:16 +02001724 if (!strcmp(optarg, "minimal") ||
1725 !strcmp(optarg, "terse") ||
1726 !strcmp(optarg, "csv"))
1727 output_format = FIO_OUTPUT_TERSE;
1728 else if (!strcmp(optarg, "json"))
1729 output_format = FIO_OUTPUT_JSON;
1730 else
1731 output_format = FIO_OUTPUT_NORMAL;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001732 break;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +01001733 case 'f':
1734 append_terse_output = 1;
1735 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001736 case 'h':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001737 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001738 usage(argv[0]);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001739 do_exit++;
1740 }
Jens Axboec2c94582011-10-05 20:31:30 +02001741 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001742 case 'c':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001743 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02001744 fio_show_option_help(optarg);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001745 do_exit++;
1746 }
Jens Axboec2c94582011-10-05 20:31:30 +02001747 break;
Steven Langde890a12011-11-09 14:03:34 +01001748 case 'i':
1749 if (!cur_client) {
1750 fio_show_ioengine_help(optarg);
1751 do_exit++;
1752 }
1753 break;
Jens Axboecca73aa2007-04-04 11:09:19 +02001754 case 's':
1755 dump_cmdline = 1;
1756 break;
Jens Axboe724e4432007-09-11 20:02:05 +02001757 case 'r':
1758 read_only = 1;
1759 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001760 case 'v':
Jens Axboe7874f8b2011-10-06 09:18:20 +02001761 if (!cur_client) {
Jens Axboe3d433822012-03-21 22:25:22 +01001762 log_info("%s\n", fio_version_string);
Jens Axboe7874f8b2011-10-06 09:18:20 +02001763 do_exit++;
1764 }
Jens Axboec2c94582011-10-05 20:31:30 +02001765 break;
Jens Axboef57a9c52011-09-09 21:01:37 +02001766 case 'V':
1767 terse_version = atoi(optarg);
Jens Axboe09786f52012-10-04 17:06:49 +02001768 if (!(terse_version == 2 || terse_version == 3 ||
1769 terse_version == 4)) {
Jens Axboef57a9c52011-09-09 21:01:37 +02001770 log_err("fio: bad terse version format\n");
1771 exit_val = 1;
1772 do_exit++;
1773 }
1774 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02001775 case 'e':
1776 if (!strcmp("always", optarg))
1777 eta_print = FIO_ETA_ALWAYS;
1778 else if (!strcmp("never", optarg))
1779 eta_print = FIO_ETA_NEVER;
1780 break;
Jens Axboee382e662013-02-22 20:48:56 +01001781 case 'E': {
1782 long long t = 0;
1783
Jens Axboe0de5b262014-02-21 15:26:01 -08001784 if (str_to_decimal(optarg, &t, 0, NULL, 1)) {
Jens Axboee382e662013-02-22 20:48:56 +01001785 log_err("fio: failed parsing eta time %s\n", optarg);
1786 exit_val = 1;
1787 do_exit++;
1788 }
1789 eta_new_line = t;
1790 break;
1791 }
Jens Axboeee56ad52008-02-01 10:30:20 +01001792 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01001793 if (set_debug(optarg))
1794 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01001795 break;
Jens Axboe111e0322013-03-07 11:31:20 +01001796 case 'P':
1797 parse_only = 1;
1798 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001799 case 'x': {
1800 size_t new_size;
1801
Jens Axboe01f06b62008-02-18 20:53:47 +01001802 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001803 log_err("fio: can't use global as only "
1804 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01001805 do_exit++;
1806 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001807 break;
1808 }
Jens Axboead0a2732011-03-11 10:16:17 +01001809 new_size = (nr_job_sections + 1) * sizeof(char *);
1810 job_sections = realloc(job_sections, new_size);
1811 job_sections[nr_job_sections] = strdup(optarg);
1812 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01001813 break;
Jens Axboead0a2732011-03-11 10:16:17 +01001814 }
Jens Axboe9ac8a792009-11-13 21:23:07 +01001815 case 'p':
Jens Axboe07b32322010-03-05 09:48:44 +01001816 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01001817 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001818 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001819 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001820 char *val = optarg;
1821
1822 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe46bcd492012-03-14 11:31:21 +01001823 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe66251552011-05-12 10:14:57 +02001824 if (ret)
Jens Axboe214e1ec2007-03-15 10:48:13 +01001825 return 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001826 td = NULL;
1827 }
1828 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01001829 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02001830 int global = 0;
1831
Jens Axboe01f06b62008-02-18 20:53:47 +01001832 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02001833 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001834
Jens Axboe01f06b62008-02-18 20:53:47 +01001835 if (is_section && skip_this_section(val))
1836 continue;
1837
Steven Langde890a12011-11-09 14:03:34 +01001838 td = get_new_job(global, &def_thread, 1);
1839 if (!td || ioengine_load(td))
Jens Axboe214e1ec2007-03-15 10:48:13 +01001840 return 0;
Steven Langde890a12011-11-09 14:03:34 +01001841 fio_options_set_ioengine_opts(l_opts, td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001842 }
1843
Jens Axboe9d918182013-05-23 20:00:35 +02001844 if ((!val || !strlen(val)) &&
1845 l_opts[lidx].has_arg == required_argument) {
1846 log_err("fio: option %s requires an argument\n", opt);
1847 ret = 1;
1848 } else
1849 ret = fio_cmd_option_parse(td, opt, val);
1850
Jens Axboebfb3ea22013-05-23 19:54:43 +02001851 if (ret) {
1852 if (td) {
1853 put_job(td);
1854 td = NULL;
1855 }
Jens Axboea88c8c12013-04-24 22:25:22 -06001856 do_exit++;
Jens Axboebfb3ea22013-05-23 19:54:43 +02001857 }
Steven Langde890a12011-11-09 14:03:34 +01001858
1859 if (!ret && !strcmp(opt, "ioengine")) {
1860 free_ioengine(td);
1861 if (ioengine_load(td))
1862 return 0;
1863 fio_options_set_ioengine_opts(l_opts, td);
1864 }
1865 break;
1866 }
1867 case FIO_GETOPT_IOENGINE: {
1868 const char *opt = l_opts[lidx].name;
1869 char *val = optarg;
Steven Langde890a12011-11-09 14:03:34 +01001870 ret = fio_cmd_ioengine_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001871 break;
1872 }
Jens Axboea9523c62011-01-17 16:49:54 -07001873 case 'w':
1874 warnings_fatal = 1;
1875 break;
Jens Axboefca70352011-07-06 20:12:54 +02001876 case 'j':
1877 max_jobs = atoi(optarg);
1878 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
1879 log_err("fio: invalid max jobs: %d\n", max_jobs);
1880 do_exit++;
1881 exit_val = 1;
1882 }
1883 break;
Jens Axboe50d16972011-09-29 17:45:28 -06001884 case 'S':
Jens Axboea37f69b2011-10-01 12:24:21 -06001885 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06001886 log_err("fio: can't be both client and server\n");
1887 do_exit++;
1888 exit_val = 1;
1889 break;
1890 }
Jens Axboe87aa8f12011-10-06 21:24:13 +02001891 if (optarg)
Jens Axboebebe6392011-10-07 10:00:51 +02001892 fio_server_set_arg(optarg);
Jens Axboe50d16972011-09-29 17:45:28 -06001893 is_backend = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +02001894 backend = 1;
Jens Axboe50d16972011-09-29 17:45:28 -06001895 break;
Jens Axboee46d8092011-10-03 09:11:02 +02001896 case 'D':
Jens Axboe402668f2011-10-10 15:28:58 +02001897 pid_file = strdup(optarg);
Jens Axboee46d8092011-10-03 09:11:02 +02001898 break;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01001899 case 'I':
1900 if ((ret = fio_idle_prof_parse_opt(optarg))) {
1901 /* exit on error and calibration only */
1902 do_exit++;
1903 if (ret == -1)
1904 exit_val = 1;
1905 }
1906 break;
Jens Axboe132159a2011-09-30 15:01:32 -06001907 case 'C':
1908 if (is_backend) {
1909 log_err("fio: can't be both client and server\n");
1910 do_exit++;
1911 exit_val = 1;
1912 break;
1913 }
Jens Axboea5276612012-03-04 15:15:08 +01001914 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
Jens Axboebebe6392011-10-07 10:00:51 +02001915 log_err("fio: failed adding client %s\n", optarg);
1916 do_exit++;
1917 exit_val = 1;
1918 break;
1919 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02001920 /*
1921 * If the next argument exists and isn't an option,
1922 * assume it's a job file for this client only.
1923 */
1924 while (optind < argc) {
1925 if (!strncmp(argv[optind], "--", 2) ||
1926 !strncmp(argv[optind], "-", 1))
1927 break;
1928
1929 fio_client_add_ini_file(cur_client, argv[optind]);
1930 optind++;
1931 }
Jens Axboe132159a2011-09-30 15:01:32 -06001932 break;
Jens Axboe7d11f872012-12-17 12:03:29 +01001933 case 'T':
1934 do_exit++;
1935 exit_val = fio_monotonic_clocktest();
1936 break;
Jens Axboefec0f212014-02-07 14:39:33 -07001937 case 'G':
1938 do_exit++;
1939 exit_val = fio_crctest(optarg);
1940 break;
Jens Axboe06464902013-04-24 20:38:54 -06001941 case 'L': {
1942 long long val;
1943
Jens Axboe7cd0ec52014-03-28 08:09:10 -06001944 if (check_str_time(optarg, &val, 0)) {
Jens Axboe06464902013-04-24 20:38:54 -06001945 log_err("fio: failed parsing time %s\n", optarg);
1946 do_exit++;
1947 exit_val = 1;
1948 break;
1949 }
1950 status_interval = val * 1000;
1951 break;
1952 }
Jens Axboe798827c2013-01-23 18:11:48 -07001953 case '?':
1954 log_err("%s: unrecognized option '%s'\n", argv[0],
1955 argv[optind - 1]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001956 default:
Jens Axboec09823a2008-02-19 20:16:57 +01001957 do_exit++;
1958 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001959 break;
1960 }
Jens Axboec7d5c942011-10-03 11:48:17 +02001961 if (do_exit)
1962 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01001963 }
1964
Jens Axboe0b14f0a2013-05-23 21:27:54 +02001965 if (do_exit && !(is_backend || nr_clients))
1966 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01001967
Jens Axboefb296042014-04-02 08:28:49 -06001968 if (nr_clients && fio_clients_connect())
1969 exit(1);
Jens Axboe132159a2011-09-30 15:01:32 -06001970
Jens Axboe81179ee2011-10-04 12:42:06 +02001971 if (is_backend && backend)
Jens Axboe402668f2011-10-10 15:28:58 +02001972 return fio_start_server(pid_file);
Jens Axboe50d16972011-09-29 17:45:28 -06001973
Jens Axboe214e1ec2007-03-15 10:48:13 +01001974 if (td) {
Jens Axboe7d6a8902008-02-18 20:59:18 +01001975 if (!ret)
Jens Axboe46bcd492012-03-14 11:31:21 +01001976 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe214e1ec2007-03-15 10:48:13 +01001977 }
1978
Jens Axboe7874f8b2011-10-06 09:18:20 +02001979 while (!ret && optind < argc) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01001980 ini_idx++;
1981 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1982 ini_file[ini_idx - 1] = strdup(argv[optind]);
1983 optind++;
1984 }
1985
1986 return ini_idx;
1987}
1988
Jens Axboe0420ba62012-02-29 11:16:52 +01001989int fio_init_options(void)
Jens Axboeebac4652005-12-08 15:25:21 +01001990{
Jens Axboeb4692822006-10-27 13:43:22 +02001991 f_out = stdout;
1992 f_err = stderr;
1993
Jens Axboe4c6107f2011-01-18 05:22:22 -07001994 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01001995 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02001996
Jens Axboe9d9eb2e2011-10-03 12:01:42 +02001997 atexit(free_shm);
1998
Jens Axboeebac4652005-12-08 15:25:21 +01001999 if (fill_def_thread())
2000 return 1;
2001
Jens Axboe0420ba62012-02-29 11:16:52 +01002002 return 0;
2003}
2004
Jens Axboe51167792012-03-08 21:34:18 +01002005extern int fio_check_options(struct thread_options *);
2006
Jens Axboe0420ba62012-02-29 11:16:52 +01002007int parse_options(int argc, char *argv[])
2008{
Jens Axboe46bcd492012-03-14 11:31:21 +01002009 const int type = FIO_CLIENT_TYPE_CLI;
Jens Axboe0420ba62012-02-29 11:16:52 +01002010 int job_files, i;
2011
2012 if (fio_init_options())
2013 return 1;
Jens Axboe51167792012-03-08 21:34:18 +01002014 if (fio_test_cconv(&def_thread.o))
2015 log_err("fio: failed internal cconv test\n");
Jens Axboe0420ba62012-02-29 11:16:52 +01002016
Jens Axboe46bcd492012-03-14 11:31:21 +01002017 job_files = parse_cmd_line(argc, argv, type);
Jens Axboeebac4652005-12-08 15:25:21 +01002018
Jens Axboecdf54d82011-10-04 08:31:40 +02002019 if (job_files > 0) {
2020 for (i = 0; i < job_files; i++) {
2021 if (fill_def_thread())
Jens Axboe132159a2011-09-30 15:01:32 -06002022 return 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02002023 if (nr_clients) {
2024 if (fio_clients_send_ini(ini_file[i]))
2025 return 1;
2026 free(ini_file[i]);
2027 } else if (!is_backend) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002028 if (parse_jobs_ini(ini_file[i], 0, i, type))
Jens Axboecdf54d82011-10-04 08:31:40 +02002029 return 1;
2030 free(ini_file[i]);
2031 }
Jens Axboe132159a2011-09-30 15:01:32 -06002032 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02002033 } else if (nr_clients) {
2034 if (fill_def_thread())
2035 return 1;
2036 if (fio_clients_send_ini(NULL))
2037 return 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02002038 }
Jens Axboeebac4652005-12-08 15:25:21 +01002039
Jens Axboe88c6ed82006-06-09 11:28:10 +02002040 free(ini_file);
Jens Axboe7e356b22011-10-14 10:55:16 +02002041 fio_options_free(&def_thread);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08002042 filesetup_mem_free();
Jens Axboeb4692822006-10-27 13:43:22 +02002043
2044 if (!thread_number) {
Jens Axboef9633d72013-09-06 09:59:56 -06002045 if (parse_dryrun())
Jens Axboecca73aa2007-04-04 11:09:19 +02002046 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01002047 if (exec_profile)
2048 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06002049 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06002050 return 0;
Jens Axboe085399d2011-10-06 09:01:22 +02002051 if (did_arg)
2052 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02002053
Jens Axboed65e11c2011-10-05 09:56:53 +02002054 log_err("No jobs(s) defined\n\n");
Jens Axboe085399d2011-10-06 09:01:22 +02002055
2056 if (!did_arg) {
2057 usage(argv[0]);
2058 return 1;
2059 }
2060
2061 return 0;
Jens Axboeb4692822006-10-27 13:43:22 +02002062 }
2063
Jens Axboebe4ecfd2008-12-08 14:10:52 +01002064 if (def_thread.o.gtod_offload) {
2065 fio_gtod_init();
2066 fio_gtod_offload = 1;
2067 fio_gtod_cpu = def_thread.o.gtod_cpu;
2068 }
2069
Jens Axboef3afa572012-09-17 13:34:16 +02002070 if (output_format == FIO_OUTPUT_NORMAL)
Jens Axboe3d433822012-03-21 22:25:22 +01002071 log_info("%s\n", fio_version_string);
Jens Axboef6dea4d2011-10-13 13:37:07 +02002072
Jens Axboeebac4652005-12-08 15:25:21 +01002073 return 0;
2074}
Jens Axboe588b7f02012-03-19 20:37:41 +01002075
2076void options_default_fill(struct thread_options *o)
2077{
2078 memcpy(o, &def_thread.o, sizeof(*o));
2079}