blob: 52a5f0301d196fe11b378fce6e59e41800745126 [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"
Elliott Hugheseda3a602017-05-19 18:53:02 -070028#include "steadystate.h"
Jens Axboeebac4652005-12-08 15:25:21 +010029
Elliott Hugheseda3a602017-05-19 18:53:02 -070030#include "oslib/getopt.h"
31#include "oslib/strcasestr.h"
Cigy Cyriacbf2e8212010-08-10 19:51:11 -040032
Jens Axboe10aa1362014-04-01 21:10:36 -060033#include "crc/test.h"
Elliott Hugheseda3a602017-05-19 18:53:02 -070034#include "lib/pow2.h"
Jens Axboe10aa1362014-04-01 21:10:36 -060035
Jens Axboe3d433822012-03-21 22:25:22 +010036const char fio_version_string[] = FIO_VERSION;
Jens Axboe214e1ec2007-03-15 10:48:13 +010037
Jens Axboeee738492007-01-10 11:23:16 +010038#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010039
Jens Axboe214e1ec2007-03-15 10:48:13 +010040static char **ini_file;
Jens Axboefca70352011-07-06 20:12:54 +020041static int max_jobs = FIO_MAX_JOBS;
Jens Axboecca73aa2007-04-04 11:09:19 +020042static int dump_cmdline;
Jens Axboe111e0322013-03-07 11:31:20 +010043static int parse_only;
Jens Axboee1f36502006-10-27 10:54:08 +020044
Jens Axboebe4ecfd2008-12-08 14:10:52 +010045static struct thread_data def_thread;
Jens Axboe214e1ec2007-03-15 10:48:13 +010046struct thread_data *threads = NULL;
Jens Axboe10aa1362014-04-01 21:10:36 -060047static char **job_sections;
48static int nr_job_sections;
Jens Axboee1f36502006-10-27 10:54:08 +020049
Jens Axboe214e1ec2007-03-15 10:48:13 +010050int exitall_on_terminate = 0;
Jens Axboef3afa572012-09-17 13:34:16 +020051int output_format = FIO_OUTPUT_NORMAL;
Jens Axboe5be4c942013-02-11 16:33:25 +010052int eta_print = FIO_ETA_AUTO;
Jens Axboee382e662013-02-22 20:48:56 +010053int eta_new_line = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +010054FILE *f_out = NULL;
55FILE *f_err = NULL;
Jens Axboe07b32322010-03-05 09:48:44 +010056char *exec_profile = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -070057int warnings_fatal = 0;
Jens Axboe4d658652011-10-17 15:05:47 +020058int terse_version = 3;
Jens Axboe50d16972011-09-29 17:45:28 -060059int is_backend = 0;
Jens Axboea37f69b2011-10-01 12:24:21 -060060int nr_clients = 0;
Jens Axboee46d8092011-10-03 09:11:02 +020061int log_syslog = 0;
Jens Axboeee738492007-01-10 11:23:16 +010062
Jens Axboe214e1ec2007-03-15 10:48:13 +010063int write_bw_log = 0;
Jens Axboe4241ea82007-09-12 08:18:36 +020064int read_only = 0;
Jens Axboe06464902013-04-24 20:38:54 -060065int status_interval = 0;
Jens Axboee1f36502006-10-27 10:54:08 +020066
Jens Axboede54cfd2014-11-10 20:34:00 -070067char *trigger_file = NULL;
Jens Axboede54cfd2014-11-10 20:34:00 -070068long long trigger_timeout = 0;
Jens Axboea4432072014-11-19 08:36:12 -070069char *trigger_cmd = NULL;
70char *trigger_remote_cmd = NULL;
Jens Axboede54cfd2014-11-10 20:34:00 -070071
Elliott Hugheseda3a602017-05-19 18:53:02 -070072char *aux_path = NULL;
73
Jens Axboe214e1ec2007-03-15 10:48:13 +010074static int prev_group_jobs;
Jens Axboeb4692822006-10-27 13:43:22 +020075
Jens Axboeee56ad52008-02-01 10:30:20 +010076unsigned long fio_debug = 0;
Jens Axboe5e1d3062008-05-23 11:55:53 +020077unsigned int fio_debug_jobno = -1;
78unsigned int *fio_debug_jobp = NULL;
Jens Axboeee56ad52008-02-01 10:30:20 +010079
Jens Axboe4c6107f2011-01-18 05:22:22 -070080static char cmd_optstr[256];
Jens Axboe085399d2011-10-06 09:01:22 +020081static int did_arg;
Jens Axboe4c6107f2011-01-18 05:22:22 -070082
Jens Axboe7a4b8242011-10-05 17:35:15 +020083#define FIO_CLIENT_FLAG (1 << 16)
84
Jens Axboeb4692822006-10-27 13:43:22 +020085/*
86 * Command line options. These will contain the above, plus a few
87 * extra that only pertain to fio itself and not jobs.
88 */
Jens Axboe5ec10ea2008-03-06 15:42:00 +010089static struct option l_opts[FIO_NR_OPTIONS] = {
Jens Axboeb4692822006-10-27 13:43:22 +020090 {
Jens Axboe08d2a192011-05-11 13:28:30 +020091 .name = (char *) "output",
Jens Axboeb4692822006-10-27 13:43:22 +020092 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +020093 .val = 'o' | 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",
Elliott Hugheseda3a602017-05-19 18:53:02 -0700102 .has_arg = no_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",
Elliott Hugheseda3a602017-05-19 18:53:02 -0700112 .has_arg = required_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 },
Jens Axboebac4af12014-07-03 13:42:28 -0600175#ifdef CONFIG_ZLIB
176 {
177 .name = (char *) "inflate-log",
178 .has_arg = required_argument,
179 .val = 'X' | FIO_CLIENT_FLAG,
180 },
181#endif
Jens Axboe01f06b62008-02-18 20:53:47 +0100182 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200183 .name = (char *) "alloc-size",
Jens Axboe2b386d22008-03-26 10:32:57 +0100184 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200185 .val = 'a' | FIO_CLIENT_FLAG,
Jens Axboe2b386d22008-03-26 10:32:57 +0100186 },
187 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200188 .name = (char *) "profile",
Jens Axboe9ac8a792009-11-13 21:23:07 +0100189 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200190 .val = 'p' | FIO_CLIENT_FLAG,
Jens Axboe9ac8a792009-11-13 21:23:07 +0100191 },
192 {
Jens Axboe08d2a192011-05-11 13:28:30 +0200193 .name = (char *) "warnings-fatal",
Jens Axboea9523c62011-01-17 16:49:54 -0700194 .has_arg = no_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200195 .val = 'w' | FIO_CLIENT_FLAG,
Jens Axboea9523c62011-01-17 16:49:54 -0700196 },
197 {
Jens Axboefca70352011-07-06 20:12:54 +0200198 .name = (char *) "max-jobs",
199 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200200 .val = 'j' | FIO_CLIENT_FLAG,
Jens Axboefca70352011-07-06 20:12:54 +0200201 },
202 {
Jens Axboef57a9c52011-09-09 21:01:37 +0200203 .name = (char *) "terse-version",
204 .has_arg = required_argument,
Jens Axboe7a4b8242011-10-05 17:35:15 +0200205 .val = 'V' | FIO_CLIENT_FLAG,
Jens Axboef57a9c52011-09-09 21:01:37 +0200206 },
207 {
Jens Axboe50d16972011-09-29 17:45:28 -0600208 .name = (char *) "server",
Jens Axboe87aa8f12011-10-06 21:24:13 +0200209 .has_arg = optional_argument,
Jens Axboe50d16972011-09-29 17:45:28 -0600210 .val = 'S',
211 },
Jens Axboee46d8092011-10-03 09:11:02 +0200212 { .name = (char *) "daemonize",
Jens Axboe402668f2011-10-10 15:28:58 +0200213 .has_arg = required_argument,
Jens Axboee46d8092011-10-03 09:11:02 +0200214 .val = 'D',
215 },
Jens Axboe50d16972011-09-29 17:45:28 -0600216 {
Jens Axboe132159a2011-09-30 15:01:32 -0600217 .name = (char *) "client",
218 .has_arg = required_argument,
219 .val = 'C',
220 },
221 {
Jens Axboe35899182014-10-07 20:56:28 -0600222 .name = (char *) "remote-config",
223 .has_arg = required_argument,
224 .val = 'R',
225 },
226 {
Jens Axboe7d11f872012-12-17 12:03:29 +0100227 .name = (char *) "cpuclock-test",
228 .has_arg = no_argument,
229 .val = 'T',
230 },
231 {
Jens Axboefec0f212014-02-07 14:39:33 -0700232 .name = (char *) "crctest",
233 .has_arg = optional_argument,
234 .val = 'G',
235 },
236 {
Huadong Liuf2a2ce02013-01-30 13:22:24 +0100237 .name = (char *) "idle-prof",
238 .has_arg = required_argument,
239 .val = 'I',
240 },
241 {
Jens Axboe06464902013-04-24 20:38:54 -0600242 .name = (char *) "status-interval",
243 .has_arg = required_argument,
244 .val = 'L',
245 },
246 {
Jens Axboea4432072014-11-19 08:36:12 -0700247 .name = (char *) "trigger-file",
Jens Axboede54cfd2014-11-10 20:34:00 -0700248 .has_arg = required_argument,
249 .val = 'W',
250 },
251 {
252 .name = (char *) "trigger-timeout",
253 .has_arg = required_argument,
254 .val = 'B',
255 },
256 {
Jens Axboea4432072014-11-19 08:36:12 -0700257 .name = (char *) "trigger",
258 .has_arg = required_argument,
259 .val = 'H',
260 },
261 {
262 .name = (char *) "trigger-remote",
263 .has_arg = required_argument,
264 .val = 'J',
265 },
266 {
Elliott Hugheseda3a602017-05-19 18:53:02 -0700267 .name = (char *) "aux-path",
268 .has_arg = required_argument,
269 .val = 'K',
270 },
271 {
Jens Axboeb4692822006-10-27 13:43:22 +0200272 .name = NULL,
273 },
274};
275
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200276void free_threads_shm(void)
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200277{
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200278 if (threads) {
279 void *tp = threads;
Jens Axboeef7035a2014-06-18 15:30:09 -0700280#ifndef CONFIG_NO_SHM
281 struct shmid_ds sbuf;
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200282
283 threads = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200284 shmdt(tp);
285 shmctl(shm_id, IPC_RMID, &sbuf);
286 shm_id = -1;
Jens Axboeef7035a2014-06-18 15:30:09 -0700287#else
288 threads = NULL;
289 free(tp);
290#endif
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200291 }
292}
293
Jens Axboe10aa1362014-04-01 21:10:36 -0600294static void free_shm(void)
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200295{
296 if (threads) {
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100297 flow_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200298 fio_debug_jobp = NULL;
Jens Axboe2bb3f0a2012-03-28 12:22:40 +0200299 free_threads_shm();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200300 }
301
Jens Axboea4432072014-11-19 08:36:12 -0700302 free(trigger_file);
303 free(trigger_cmd);
304 free(trigger_remote_cmd);
305 trigger_file = trigger_cmd = trigger_remote_cmd = NULL;
306
Elliott Hugheseda3a602017-05-19 18:53:02 -0700307 options_free(fio_options, &def_thread.o);
Jens Axboe243bfe12014-04-02 15:46:58 -0600308 fio_filelock_exit();
Elliott Hugheseda3a602017-05-19 18:53:02 -0700309 file_hash_exit();
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200310 scleanup();
311}
312
313/*
314 * The thread area is shared between the main process and the job
315 * threads/processes. So setup a shared memory segment that will hold
316 * all the job info. We use the end of the region for keeping track of
317 * open files across jobs, for file sharing.
318 */
319static int setup_thread_area(void)
320{
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200321 if (threads)
322 return 0;
323
324 /*
325 * 1024 is too much on some machines, scale max_jobs if
326 * we get a failure that looks like too large a shm segment
327 */
328 do {
329 size_t size = max_jobs * sizeof(struct thread_data);
330
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200331 size += sizeof(unsigned int);
332
Jens Axboeef7035a2014-06-18 15:30:09 -0700333#ifndef CONFIG_NO_SHM
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200334 shm_id = shmget(0, size, IPC_CREAT | 0600);
335 if (shm_id != -1)
336 break;
Jens Axboef0c77f02012-09-21 15:05:45 +0200337 if (errno != EINVAL && errno != ENOMEM && errno != ENOSPC) {
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200338 perror("shmget");
339 break;
340 }
Jens Axboeef7035a2014-06-18 15:30:09 -0700341#else
342 threads = malloc(size);
343 if (threads)
344 break;
345#endif
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200346
347 max_jobs >>= 1;
348 } while (max_jobs);
349
Jens Axboeef7035a2014-06-18 15:30:09 -0700350#ifndef CONFIG_NO_SHM
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200351 if (shm_id == -1)
352 return 1;
353
354 threads = shmat(shm_id, NULL, 0);
355 if (threads == (void *) -1) {
356 perror("shmat");
357 return 1;
358 }
Elliott Hugheseda3a602017-05-19 18:53:02 -0700359 if (shm_attach_to_open_removed())
360 shmctl(shm_id, IPC_RMID, NULL);
Jens Axboeef7035a2014-06-18 15:30:09 -0700361#endif
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200362
363 memset(threads, 0, max_jobs * sizeof(struct thread_data));
Elliott Hugheseda3a602017-05-19 18:53:02 -0700364 fio_debug_jobp = (void *) threads + max_jobs * sizeof(struct thread_data);
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200365 *fio_debug_jobp = -1;
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100366
367 flow_init();
368
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200369 return 0;
370}
371
Elliott Hugheseda3a602017-05-19 18:53:02 -0700372static void dump_print_option(struct print_option *p)
Jens Axboe25bd16c2014-04-06 10:09:30 -0600373{
Elliott Hugheseda3a602017-05-19 18:53:02 -0700374 const char *delim;
Jens Axboe25bd16c2014-04-06 10:09:30 -0600375
Elliott Hugheseda3a602017-05-19 18:53:02 -0700376 if (!strcmp("description", p->name))
377 delim = "\"";
378 else
379 delim = "";
380
381 log_info("--%s%s", p->name, p->value ? "" : " ");
382 if (p->value)
383 log_info("=%s%s%s ", delim, p->value, delim);
384}
385
386static void dump_opt_list(struct thread_data *td)
387{
388 struct flist_head *entry;
389 struct print_option *p;
390
391 if (flist_empty(&td->opt_list))
392 return;
393
394 flist_for_each(entry, &td->opt_list) {
395 p = flist_entry(entry, struct print_option, list);
396 dump_print_option(p);
397 }
398}
399
400static void fio_dump_options_free(struct thread_data *td)
401{
402 while (!flist_empty(&td->opt_list)) {
403 struct print_option *p;
404
405 p = flist_first_entry(&td->opt_list, struct print_option, list);
406 flist_del_init(&p->list);
407 free(p->name);
408 free(p->value);
409 free(p);
410 }
411}
412
413static void copy_opt_list(struct thread_data *dst, struct thread_data *src)
414{
415 struct flist_head *entry;
416
417 if (flist_empty(&src->opt_list))
418 return;
419
420 flist_for_each(entry, &src->opt_list) {
421 struct print_option *srcp, *dstp;
422
423 srcp = flist_entry(entry, struct print_option, list);
424 dstp = malloc(sizeof(*dstp));
425 dstp->name = strdup(srcp->name);
426 if (srcp->value)
427 dstp->value = strdup(srcp->value);
428 else
429 dstp->value = NULL;
430 flist_add_tail(&dstp->list, &dst->opt_list);
431 }
Jens Axboe25bd16c2014-04-06 10:09:30 -0600432}
433
Jens Axboe906c8d72006-06-13 09:37:56 +0200434/*
435 * Return a free job structure.
436 */
Elliott Hugheseda3a602017-05-19 18:53:02 -0700437static struct thread_data *get_new_job(bool global, struct thread_data *parent,
438 bool preserve_eo, const char *jobname)
Jens Axboeebac4652005-12-08 15:25:21 +0100439{
440 struct thread_data *td;
441
Elliott Hugheseda3a602017-05-19 18:53:02 -0700442 if (global)
Jens Axboeebac4652005-12-08 15:25:21 +0100443 return &def_thread;
Jens Axboe9d9eb2e2011-10-03 12:01:42 +0200444 if (setup_thread_area()) {
445 log_err("error: failed to setup shm segment\n");
446 return NULL;
447 }
Bruce Crane61f1ec2011-01-14 18:30:26 +0100448 if (thread_number >= max_jobs) {
449 log_err("error: maximum number of jobs (%d) reached.\n",
450 max_jobs);
Jens Axboeebac4652005-12-08 15:25:21 +0100451 return NULL;
Bruce Crane61f1ec2011-01-14 18:30:26 +0100452 }
Jens Axboeebac4652005-12-08 15:25:21 +0100453
454 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200455 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100456
Elliott Hugheseda3a602017-05-19 18:53:02 -0700457 INIT_FLIST_HEAD(&td->opt_list);
458 if (parent != &def_thread)
459 copy_opt_list(td, parent);
460
Steven Langde890a12011-11-09 14:03:34 +0100461 td->io_ops = NULL;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700462 td->io_ops_init = 0;
Steven Langde890a12011-11-09 14:03:34 +0100463 if (!preserve_eo)
464 td->eo = NULL;
465
Jens Axboee0b0d892009-12-08 10:10:14 +0100466 td->o.uid = td->o.gid = -1U;
467
Jens Axboecade3ef2007-03-23 15:29:45 +0100468 dup_files(td, parent);
Steven Langde890a12011-11-09 14:03:34 +0100469 fio_options_mem_dupe(td);
Jens Axboecade3ef2007-03-23 15:29:45 +0100470
Jens Axboe15dc1932010-03-05 10:59:06 +0100471 profile_add_hooks(td);
472
Jens Axboeebac4652005-12-08 15:25:21 +0100473 td->thread_number = thread_number;
Jiri Horky5a65b4e2014-07-25 09:55:03 +0200474 td->subjob_number = 0;
Jens Axboe108fea72012-11-14 13:09:45 -0700475
Jens Axboeb01e1f32014-06-24 09:19:30 -0600476 if (jobname)
477 td->o.name = strdup(jobname);
478
Elliott Hugheseda3a602017-05-19 18:53:02 -0700479 if (!parent->o.group_reporting || parent == &def_thread)
Jens Axboe108fea72012-11-14 13:09:45 -0700480 stat_number++;
481
Jens Axboeebac4652005-12-08 15:25:21 +0100482 return td;
483}
484
485static void put_job(struct thread_data *td)
486{
Jens Axboe549577a2006-10-30 12:23:41 +0100487 if (td == &def_thread)
488 return;
Bruce Cran84dd1882011-05-05 08:14:09 -0600489
Jens Axboe58c55ba2010-03-09 12:20:08 +0100490 profile_td_exit(td);
Dan Ehrenberg9e684a42012-02-20 11:05:14 +0100491 flow_exit_job(td);
Jens Axboe549577a2006-10-30 12:23:41 +0100492
Jens Axboe16edf252007-02-10 14:59:22 +0100493 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100494 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100495
Jens Axboe7e356b22011-10-14 10:55:16 +0200496 fio_options_free(td);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700497 fio_dump_options_free(td);
Steven Langde890a12011-11-09 14:03:34 +0100498 if (td->io_ops)
499 free_ioengine(td);
Jens Axboe7e356b22011-10-14 10:55:16 +0200500
Jens Axboeb01e1f32014-06-24 09:19:30 -0600501 if (td->o.name)
502 free(td->o.name);
503
Jens Axboeebac4652005-12-08 15:25:21 +0100504 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
505 thread_number--;
506}
507
Jens Axboe581e7142009-06-09 12:47:16 +0200508static int __setup_rate(struct thread_data *td, enum fio_ddir ddir)
Jens Axboe127f6862007-03-15 12:09:57 +0100509{
Jens Axboe581e7142009-06-09 12:47:16 +0200510 unsigned int bs = td->o.min_bs[ddir];
Jens Axboe127f6862007-03-15 12:09:57 +0100511
Jens Axboeff58fce2010-08-25 12:02:08 +0200512 assert(ddir_rw(ddir));
513
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100514 if (td->o.rate[ddir])
Steven Lang1b8dbf22011-11-09 13:48:01 +0100515 td->rate_bps[ddir] = td->o.rate[ddir];
Radha Ramachandranba3e4e02009-12-09 22:31:44 +0100516 else
Elliott Hugheseda3a602017-05-19 18:53:02 -0700517 td->rate_bps[ddir] = (uint64_t) td->o.rate_iops[ddir] * bs;
Jens Axboe127f6862007-03-15 12:09:57 +0100518
Steven Lang1b8dbf22011-11-09 13:48:01 +0100519 if (!td->rate_bps[ddir]) {
Jens Axboe127f6862007-03-15 12:09:57 +0100520 log_err("rate lower than supported\n");
521 return -1;
522 }
523
Elliott Hugheseda3a602017-05-19 18:53:02 -0700524 td->rate_next_io_time[ddir] = 0;
525 td->rate_io_issue_bytes[ddir] = 0;
526 td->last_usec[ddir] = 0;
Jens Axboe127f6862007-03-15 12:09:57 +0100527 return 0;
528}
529
Jens Axboe581e7142009-06-09 12:47:16 +0200530static int setup_rate(struct thread_data *td)
531{
532 int ret = 0;
533
534 if (td->o.rate[DDIR_READ] || td->o.rate_iops[DDIR_READ])
535 ret = __setup_rate(td, DDIR_READ);
536 if (td->o.rate[DDIR_WRITE] || td->o.rate_iops[DDIR_WRITE])
537 ret |= __setup_rate(td, DDIR_WRITE);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200538 if (td->o.rate[DDIR_TRIM] || td->o.rate_iops[DDIR_TRIM])
539 ret |= __setup_rate(td, DDIR_TRIM);
Jens Axboe581e7142009-06-09 12:47:16 +0200540
541 return ret;
542}
543
Jens Axboe83472392009-02-19 21:32:12 +0100544static int fixed_block_size(struct thread_options *o)
545{
546 return o->min_bs[DDIR_READ] == o->max_bs[DDIR_READ] &&
547 o->min_bs[DDIR_WRITE] == o->max_bs[DDIR_WRITE] &&
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200548 o->min_bs[DDIR_TRIM] == o->max_bs[DDIR_TRIM] &&
549 o->min_bs[DDIR_READ] == o->min_bs[DDIR_WRITE] &&
550 o->min_bs[DDIR_READ] == o->min_bs[DDIR_TRIM];
Jens Axboe83472392009-02-19 21:32:12 +0100551}
552
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800553
554static unsigned long long get_rand_start_delay(struct thread_data *td)
555{
556 unsigned long long delayrange;
Elliott Hugheseda3a602017-05-19 18:53:02 -0700557 uint64_t frand_max;
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800558 unsigned long r;
559
560 delayrange = td->o.start_delay_high - td->o.start_delay;
561
Elliott Hugheseda3a602017-05-19 18:53:02 -0700562 frand_max = rand_max(&td->delay_state);
Jens Axboef6787012014-11-05 18:39:23 -0700563 r = __rand(&td->delay_state);
Elliott Hugheseda3a602017-05-19 18:53:02 -0700564 delayrange = (unsigned long long) ((double) delayrange * (r / (frand_max + 1.0)));
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800565
566 delayrange += td->o.start_delay;
567 return delayrange;
568}
569
Jens Axboedad915e2006-10-27 11:10:18 +0200570/*
Elliott Hugheseda3a602017-05-19 18:53:02 -0700571 * <3 Johannes
572 */
573static unsigned int gcd(unsigned int m, unsigned int n)
574{
575 if (!n)
576 return m;
577
578 return gcd(n, m % n);
579}
580
581/*
Jens Axboedad915e2006-10-27 11:10:18 +0200582 * Lazy way of fixing up options that depend on each other. We could also
583 * define option callback handlers, but this is easier.
584 */
Jens Axboe4e991c22007-03-15 11:41:11 +0100585static int fixup_options(struct thread_data *td)
Jens Axboee1f36502006-10-27 10:54:08 +0200586{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100587 struct thread_options *o = &td->o;
Jens Axboeeefd98b2013-07-25 12:30:06 -0600588 int ret = 0;
Jens Axboedad915e2006-10-27 11:10:18 +0200589
Elliott Hugheseda3a602017-05-19 18:53:02 -0700590#ifndef CONFIG_PSHARED
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100591 if (!o->use_thread) {
Jens Axboef356d012009-01-05 09:56:29 +0100592 log_info("fio: this platform does not support process shared"
593 " mutexes, forcing use of threads. Use the 'thread'"
594 " option to get rid of this warning.\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100595 o->use_thread = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700596 ret = warnings_fatal;
Jens Axboef356d012009-01-05 09:56:29 +0100597 }
598#endif
599
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100600 if (o->write_iolog_file && o->read_iolog_file) {
Jens Axboe076efc72006-10-27 11:24:25 +0200601 log_err("fio: read iolog overrides write_iolog\n");
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100602 free(o->write_iolog_file);
603 o->write_iolog_file = NULL;
Jens Axboea9523c62011-01-17 16:49:54 -0700604 ret = warnings_fatal;
Jens Axboe076efc72006-10-27 11:24:25 +0200605 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100606
Jens Axboe16b462a2006-10-30 12:35:18 +0100607 /*
Steven Noonaned335852012-01-31 13:58:00 +0100608 * only really works with 1 file
Jens Axboe16b462a2006-10-30 12:35:18 +0100609 */
Jens Axboe627aa1a2013-02-07 13:06:18 +0100610 if (o->zone_size && o->open_files > 1)
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100611 o->zone_size = 0;
Jens Axboe16b462a2006-10-30 12:35:18 +0100612
613 /*
Steven Noonaned335852012-01-31 13:58:00 +0100614 * If zone_range isn't specified, backward compatibility dictates it
615 * should be made equal to zone_size.
616 */
617 if (o->zone_size && !o->zone_range)
618 o->zone_range = o->zone_size;
619
620 /*
Jens Axboe16b462a2006-10-30 12:35:18 +0100621 * Reads can do overwrites, we always need to pre-create the file
622 */
Elliott Hugheseda3a602017-05-19 18:53:02 -0700623 if (td_read(td))
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100624 o->overwrite = 1;
Jens Axboe16b462a2006-10-30 12:35:18 +0100625
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100626 if (!o->min_bs[DDIR_READ])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100627 o->min_bs[DDIR_READ] = o->bs[DDIR_READ];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100628 if (!o->max_bs[DDIR_READ])
629 o->max_bs[DDIR_READ] = o->bs[DDIR_READ];
630 if (!o->min_bs[DDIR_WRITE])
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100631 o->min_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100632 if (!o->max_bs[DDIR_WRITE])
633 o->max_bs[DDIR_WRITE] = o->bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200634 if (!o->min_bs[DDIR_TRIM])
635 o->min_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
636 if (!o->max_bs[DDIR_TRIM])
637 o->max_bs[DDIR_TRIM] = o->bs[DDIR_TRIM];
638
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100639 o->rw_min_bs = min(o->min_bs[DDIR_READ], o->min_bs[DDIR_WRITE]);
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200640 o->rw_min_bs = min(o->min_bs[DDIR_TRIM], o->rw_min_bs);
Jens Axboea00735e2006-11-03 08:58:08 +0100641
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100642 /*
643 * For random IO, allow blockalign offset other than min_bs.
644 */
645 if (!o->ba[DDIR_READ] || !td_random(td))
646 o->ba[DDIR_READ] = o->min_bs[DDIR_READ];
647 if (!o->ba[DDIR_WRITE] || !td_random(td))
648 o->ba[DDIR_WRITE] = o->min_bs[DDIR_WRITE];
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200649 if (!o->ba[DDIR_TRIM] || !td_random(td))
650 o->ba[DDIR_TRIM] = o->min_bs[DDIR_TRIM];
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100651
652 if ((o->ba[DDIR_READ] != o->min_bs[DDIR_READ] ||
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200653 o->ba[DDIR_WRITE] != o->min_bs[DDIR_WRITE] ||
654 o->ba[DDIR_TRIM] != o->min_bs[DDIR_TRIM]) &&
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100655 !o->norandommap) {
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100656 log_err("fio: Any use of blockalign= turns off randommap\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100657 o->norandommap = 1;
Jens Axboea9523c62011-01-17 16:49:54 -0700658 ret = warnings_fatal;
Jens Axboe2b7a01d2009-03-11 11:00:13 +0100659 }
660
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100661 if (!o->file_size_high)
662 o->file_size_high = o->file_size_low;
Jens Axboe9c60ce62007-03-15 09:14:47 +0100663
Christian Ehrhardt23ed19b2014-02-20 09:07:02 -0800664 if (o->start_delay_high)
665 o->start_delay = get_rand_start_delay(td);
666
Jens Axboe83472392009-02-19 21:32:12 +0100667 if (o->norandommap && o->verify != VERIFY_NONE
668 && !fixed_block_size(o)) {
669 log_err("fio: norandommap given for variable block sizes, "
Justin Enoa03739b2015-01-29 14:28:38 -0800670 "verify limited\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700671 ret = warnings_fatal;
Jens Axboebb8895e2006-10-30 15:14:48 +0100672 }
Elliott Hugheseda3a602017-05-19 18:53:02 -0700673 if (o->bs_unaligned && (o->odirect || td_ioengine_flagged(td, FIO_RAWIO)))
Jens Axboe690adba2006-10-30 15:25:09 +0100674 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100675
676 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100677 * thinktime_spin must be less than thinktime
678 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100679 if (o->thinktime_spin > o->thinktime)
680 o->thinktime_spin = o->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100681
682 /*
683 * The low water mark cannot be bigger than the iodepth
684 */
Jens Axboe67bf9822013-01-10 11:23:19 +0100685 if (o->iodepth_low > o->iodepth || !o->iodepth_low)
686 o->iodepth_low = o->iodepth;
Jens Axboecb5ab512007-02-26 12:57:09 +0100687
688 /*
689 * If batch number isn't set, default to the same as iodepth
690 */
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100691 if (o->iodepth_batch > o->iodepth || !o->iodepth_batch)
692 o->iodepth_batch = o->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100693
Elliott Hugheseda3a602017-05-19 18:53:02 -0700694 /*
695 * If max batch complete number isn't set or set incorrectly,
696 * default to the same as iodepth_batch_complete_min
697 */
698 if (o->iodepth_batch_complete_min > o->iodepth_batch_complete_max)
699 o->iodepth_batch_complete_max = o->iodepth_batch_complete_min;
700
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100701 if (o->nr_files > td->files_index)
702 o->nr_files = td->files_index;
Jens Axboe9f9214f2007-03-13 14:02:16 +0100703
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100704 if (o->open_files > o->nr_files || !o->open_files)
705 o->open_files = o->nr_files;
Jens Axboe4e991c22007-03-15 11:41:11 +0100706
Shaohua Li6eaf09d2012-09-14 08:49:43 +0200707 if (((o->rate[DDIR_READ] + o->rate[DDIR_WRITE] + o->rate[DDIR_TRIM]) &&
708 (o->rate_iops[DDIR_READ] + o->rate_iops[DDIR_WRITE] + o->rate_iops[DDIR_TRIM])) ||
709 ((o->ratemin[DDIR_READ] + o->ratemin[DDIR_WRITE] + o->ratemin[DDIR_TRIM]) &&
710 (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 +0100711 log_err("fio: rate and rate_iops are mutually exclusive\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700712 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100713 }
Elliott Hugheseda3a602017-05-19 18:53:02 -0700714 if ((o->rate[DDIR_READ] && (o->rate[DDIR_READ] < o->ratemin[DDIR_READ])) ||
715 (o->rate[DDIR_WRITE] && (o->rate[DDIR_WRITE] < o->ratemin[DDIR_WRITE])) ||
716 (o->rate[DDIR_TRIM] && (o->rate[DDIR_TRIM] < o->ratemin[DDIR_TRIM])) ||
717 (o->rate_iops[DDIR_READ] && (o->rate_iops[DDIR_READ] < o->rate_iops_min[DDIR_READ])) ||
718 (o->rate_iops[DDIR_WRITE] && (o->rate_iops[DDIR_WRITE] < o->rate_iops_min[DDIR_WRITE])) ||
719 (o->rate_iops[DDIR_TRIM] && (o->rate_iops[DDIR_TRIM] < o->rate_iops_min[DDIR_TRIM]))) {
Jens Axboe4e991c22007-03-15 11:41:11 +0100720 log_err("fio: minimum rate exceeds rate\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700721 ret = 1;
Jens Axboe4e991c22007-03-15 11:41:11 +0100722 }
723
Jens Axboecf4464c2007-04-17 20:14:42 +0200724 if (!o->timeout && o->time_based) {
725 log_err("fio: time_based requires a runtime/timeout setting\n");
726 o->time_based = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700727 ret = warnings_fatal;
Jens Axboecf4464c2007-04-17 20:14:42 +0200728 }
729
Shawn Lewisaa31f1f2008-01-11 09:45:11 +0100730 if (o->fill_device && !o->size)
Jens Axboe5921e802008-05-30 15:02:38 +0200731 o->size = -1ULL;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100732
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100733 if (o->verify != VERIFY_NONE) {
Jens Axboe996936f2011-01-18 05:41:39 -0700734 if (td_write(td) && o->do_verify && o->numjobs > 1) {
Jens Axboea9523c62011-01-17 16:49:54 -0700735 log_info("Multiple writers may overwrite blocks that "
736 "belong to other jobs. This can cause "
737 "verification failures.\n");
738 ret = warnings_fatal;
739 }
740
Elliott Hugheseda3a602017-05-19 18:53:02 -0700741 if (!fio_option_is_set(o, refill_buffers))
742 o->refill_buffers = 1;
743
Jens Axboe2f1b8e82010-06-18 09:22:56 +0200744 if (o->max_bs[DDIR_WRITE] != o->min_bs[DDIR_WRITE] &&
745 !o->verify_interval)
746 o->verify_interval = o->min_bs[DDIR_WRITE];
Jens Axboe023fa7d2014-07-23 09:47:26 +0200747
748 /*
749 * Verify interval must be smaller or equal to the
750 * write size.
751 */
752 if (o->verify_interval > o->min_bs[DDIR_WRITE])
753 o->verify_interval = o->min_bs[DDIR_WRITE];
754 else if (td_read(td) && o->verify_interval > o->min_bs[DDIR_READ])
755 o->verify_interval = o->min_bs[DDIR_READ];
Elliott Hugheseda3a602017-05-19 18:53:02 -0700756
757 /*
758 * Verify interval must be a factor or both min and max
759 * write size
760 */
761 if (o->verify_interval % o->min_bs[DDIR_WRITE] ||
762 o->verify_interval % o->max_bs[DDIR_WRITE])
763 o->verify_interval = gcd(o->min_bs[DDIR_WRITE],
764 o->max_bs[DDIR_WRITE]);
Jens Axboed9905882010-03-18 20:43:00 +0100765 }
Jens Axboe41ccd842008-05-22 09:17:33 +0200766
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100767 if (o->pre_read) {
Elliott Hugheseda3a602017-05-19 18:53:02 -0700768 if (o->invalidate_cache)
769 o->invalidate_cache = 0;
770 if (td_ioengine_flagged(td, FIO_PIPEIO)) {
Jens Axboe9c0d2242009-07-01 12:26:28 +0200771 log_info("fio: cannot pre-read files with an IO engine"
772 " that isn't seekable. Pre-read disabled.\n");
Jens Axboea9523c62011-01-17 16:49:54 -0700773 ret = warnings_fatal;
774 }
Jens Axboe9c0d2242009-07-01 12:26:28 +0200775 }
Jens Axboe34f1c042009-06-02 14:19:25 +0200776
Steven Noonanad705bc2013-04-08 15:05:25 -0700777 if (!o->unit_base) {
Elliott Hugheseda3a602017-05-19 18:53:02 -0700778 if (td_ioengine_flagged(td, FIO_BIT_BASED))
Steven Noonanad705bc2013-04-08 15:05:25 -0700779 o->unit_base = 1;
780 else
781 o->unit_base = 8;
782 }
783
Jens Axboe67bf9822013-01-10 11:23:19 +0100784#ifndef CONFIG_FDATASYNC
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100785 if (o->fdatasync_blocks) {
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700786 log_info("fio: this platform does not support fdatasync()"
787 " falling back to using fsync(). Use the 'fsync'"
788 " option instead of 'fdatasync' to get rid of"
789 " this warning\n");
Jens Axboe9bbf57c2010-03-18 20:45:43 +0100790 o->fsync_blocks = o->fdatasync_blocks;
791 o->fdatasync_blocks = 0;
Jens Axboea9523c62011-01-17 16:49:54 -0700792 ret = warnings_fatal;
Joshua Aunee72fa4d2010-02-11 00:59:18 -0700793 }
794#endif
795
Bruce Cran93bcfd22012-02-20 20:18:19 +0100796#ifdef WIN32
797 /*
798 * Windows doesn't support O_DIRECT or O_SYNC with the _open interface,
799 * so fail if we're passed those flags
800 */
Elliott Hugheseda3a602017-05-19 18:53:02 -0700801 if (td_ioengine_flagged(td, FIO_SYNCIO) && (td->o.odirect || td->o.sync_io)) {
Bruce Cran93bcfd22012-02-20 20:18:19 +0100802 log_err("fio: Windows does not support direct or non-buffered io with"
803 " the synchronous ioengines. Use the 'windowsaio' ioengine"
804 " with 'direct=1' and 'iodepth=1' instead.\n");
805 ret = 1;
806 }
807#endif
808
Jens Axboe811ac502012-03-02 22:23:36 +0100809 /*
810 * For fully compressible data, just zero them at init time.
Elliott Hugheseda3a602017-05-19 18:53:02 -0700811 * It's faster than repeatedly filling it. For non-zero
812 * compression, we should have refill_buffers set. Set it, unless
813 * the job file already changed it.
Jens Axboe811ac502012-03-02 22:23:36 +0100814 */
Elliott Hugheseda3a602017-05-19 18:53:02 -0700815 if (o->compress_percentage) {
816 if (o->compress_percentage == 100) {
817 o->zero_buffers = 1;
818 o->compress_percentage = 0;
819 } else if (!fio_option_is_set(o, refill_buffers))
820 o->refill_buffers = 1;
Jens Axboe811ac502012-03-02 22:23:36 +0100821 }
822
Jens Axboe5881fda2012-11-15 15:21:23 -0700823 /*
824 * Using a non-uniform random distribution excludes usage of
825 * a random map
826 */
827 if (td->o.random_distribution != FIO_RAND_DIST_RANDOM)
828 td->o.norandommap = 1;
829
Jens Axboe8d916c92013-04-25 10:11:41 -0600830 /*
831 * If size is set but less than the min block size, complain
832 */
833 if (o->size && o->size < td_min_bs(td)) {
Elliott Hugheseda3a602017-05-19 18:53:02 -0700834 log_err("fio: size too small, must not be less than minimum block size: %llu < %u\n",
835 (unsigned long long) o->size, td_min_bs(td));
Jens Axboe8d916c92013-04-25 10:11:41 -0600836 ret = 1;
837 }
838
Chris Masond01612f2013-11-15 15:52:58 -0700839 /*
840 * O_ATOMIC implies O_DIRECT
841 */
842 if (td->o.oatomic)
843 td->o.odirect = 1;
844
Jens Axboe04778ba2014-01-10 20:57:01 -0700845 /*
846 * If randseed is set, that overrides randrepeat
847 */
Elliott Hugheseda3a602017-05-19 18:53:02 -0700848 if (fio_option_is_set(&td->o, rand_seed))
Jens Axboe04778ba2014-01-10 20:57:01 -0700849 td->o.rand_repeatable = 0;
850
Elliott Hugheseda3a602017-05-19 18:53:02 -0700851 if (td_ioengine_flagged(td, FIO_NOEXTEND) && td->o.file_append) {
Jens Axboeb1bebc32014-03-18 13:30:32 -0600852 log_err("fio: can't append/extent with IO engine %s\n", td->io_ops->name);
853 ret = 1;
854 }
855
Jens Axboe80ac0202014-12-16 20:38:53 -0700856 if (fio_option_is_set(o, gtod_cpu)) {
857 fio_gtod_init();
858 fio_gtod_set_cpu(o->gtod_cpu);
859 fio_gtod_offload = 1;
860 }
861
Jens Axboe842d2e22015-02-24 14:50:57 -0800862 td->loops = o->loops;
863 if (!td->loops)
864 td->loops = 1;
865
Elliott Hugheseda3a602017-05-19 18:53:02 -0700866 if (td->o.block_error_hist && td->o.nr_files != 1) {
867 log_err("fio: block error histogram only available "
868 "with a single file per job, but %d files "
869 "provided\n", td->o.nr_files);
870 ret = 1;
871 }
872
Jens Axboea9523c62011-01-17 16:49:54 -0700873 return ret;
Jens Axboee1f36502006-10-27 10:54:08 +0200874}
875
Jens Axboe09629a92007-03-09 09:00:06 +0100876/* External engines are specified by "external:name.o") */
877static const char *get_engine_name(const char *str)
878{
879 char *p = strstr(str, ":");
880
881 if (!p)
882 return str;
883
884 p++;
885 strip_blank_front(&p);
886 strip_blank_end(p);
887 return p;
888}
889
Elliott Hugheseda3a602017-05-19 18:53:02 -0700890static void init_rand_file_service(struct thread_data *td)
Jens Axboee132cba2007-03-14 14:23:54 +0100891{
Elliott Hugheseda3a602017-05-19 18:53:02 -0700892 unsigned long nranges = td->o.nr_files << FIO_FSERVICE_SHIFT;
893 const unsigned int seed = td->rand_seeds[FIO_RAND_FILE_OFF];
Jens Axboee132cba2007-03-14 14:23:54 +0100894
Elliott Hugheseda3a602017-05-19 18:53:02 -0700895 if (td->o.file_service_type == FIO_FSERVICE_ZIPF) {
896 zipf_init(&td->next_file_zipf, nranges, td->zipf_theta, seed);
897 zipf_disable_hash(&td->next_file_zipf);
898 } else if (td->o.file_service_type == FIO_FSERVICE_PARETO) {
899 pareto_init(&td->next_file_zipf, nranges, td->pareto_h, seed);
900 zipf_disable_hash(&td->next_file_zipf);
901 } else if (td->o.file_service_type == FIO_FSERVICE_GAUSS) {
902 gauss_init(&td->next_file_gauss, nranges, td->gauss_dev, seed);
903 gauss_disable_hash(&td->next_file_gauss);
904 }
Jens Axboee132cba2007-03-14 14:23:54 +0100905}
906
Elliott Hugheseda3a602017-05-19 18:53:02 -0700907void td_fill_verify_state_seed(struct thread_data *td)
Jens Axboe4c07ad82011-03-28 09:51:09 +0200908{
Elliott Hugheseda3a602017-05-19 18:53:02 -0700909 bool use64;
910
911 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
912 use64 = 1;
913 else
914 use64 = 0;
915
916 init_rand_seed(&td->verify_state, td->rand_seeds[FIO_RAND_VER_OFF],
917 use64);
918}
919
920static void td_fill_rand_seeds_internal(struct thread_data *td, bool use64)
921{
922 int i;
923
924 init_rand_seed(&td->bsrange_state, td->rand_seeds[FIO_RAND_BS_OFF], use64);
925 td_fill_verify_state_seed(td);
926 init_rand_seed(&td->rwmix_state, td->rand_seeds[FIO_RAND_MIX_OFF], false);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200927
928 if (td->o.file_service_type == FIO_FSERVICE_RANDOM)
Elliott Hugheseda3a602017-05-19 18:53:02 -0700929 init_rand_seed(&td->next_file_state, td->rand_seeds[FIO_RAND_FILE_OFF], use64);
930 else if (td->o.file_service_type & __FIO_FSERVICE_NONUNIFORM)
931 init_rand_file_service(td);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200932
Elliott Hugheseda3a602017-05-19 18:53:02 -0700933 init_rand_seed(&td->file_size_state, td->rand_seeds[FIO_RAND_FILE_SIZE_OFF], use64);
934 init_rand_seed(&td->trim_state, td->rand_seeds[FIO_RAND_TRIM_OFF], use64);
935 init_rand_seed(&td->delay_state, td->rand_seeds[FIO_RAND_START_DELAY], use64);
936 init_rand_seed(&td->poisson_state[0], td->rand_seeds[FIO_RAND_POISSON_OFF], 0);
937 init_rand_seed(&td->poisson_state[1], td->rand_seeds[FIO_RAND_POISSON2_OFF], 0);
938 init_rand_seed(&td->poisson_state[2], td->rand_seeds[FIO_RAND_POISSON3_OFF], 0);
939 init_rand_seed(&td->dedupe_state, td->rand_seeds[FIO_DEDUPE_OFF], false);
940 init_rand_seed(&td->zone_state, td->rand_seeds[FIO_RAND_ZONE_OFF], false);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200941
942 if (!td_random(td))
943 return;
944
945 if (td->o.rand_repeatable)
Jens Axboed24945f2012-12-04 13:10:29 +0100946 td->rand_seeds[FIO_RAND_BLOCK_OFF] = FIO_RANDSEED * td->thread_number;
Jens Axboe4c07ad82011-03-28 09:51:09 +0200947
Elliott Hugheseda3a602017-05-19 18:53:02 -0700948 init_rand_seed(&td->random_state, td->rand_seeds[FIO_RAND_BLOCK_OFF], use64);
949
950 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
951 struct frand_state *s = &td->seq_rand_state[i];
952
953 init_rand_seed(s, td->rand_seeds[FIO_RAND_SEQ_RAND_READ_OFF], false);
954 }
Jens Axboe5bfc35d2009-04-07 13:28:12 +0200955}
956
Jens Axboe4c07ad82011-03-28 09:51:09 +0200957void td_fill_rand_seeds(struct thread_data *td)
958{
Elliott Hugheseda3a602017-05-19 18:53:02 -0700959 bool use64;
960
Christian Ehrhardt56e2a5f2014-02-20 09:10:17 -0800961 if (td->o.allrand_repeatable) {
Jens Axboee66dac22014-09-22 10:02:07 -0600962 unsigned int i;
963
964 for (i = 0; i < FIO_RAND_NR_OFFS; i++)
Christian Ehrhardt56e2a5f2014-02-20 09:10:17 -0800965 td->rand_seeds[i] = FIO_RANDSEED * td->thread_number
966 + i;
967 }
968
Elliott Hugheseda3a602017-05-19 18:53:02 -0700969 if (td->o.random_generator == FIO_RAND_GEN_TAUSWORTHE64)
970 use64 = 1;
971 else
972 use64 = 0;
Jens Axboe3545a102011-08-31 15:20:15 -0600973
Elliott Hugheseda3a602017-05-19 18:53:02 -0700974 td_fill_rand_seeds_internal(td, use64);
975
976 init_rand_seed(&td->buf_state, td->rand_seeds[FIO_RAND_BUF_OFF], use64);
Jens Axboee66dac22014-09-22 10:02:07 -0600977 frand_copy(&td->buf_state_prev, &td->buf_state);
Jens Axboe4c07ad82011-03-28 09:51:09 +0200978}
979
Jens Axboe9c60ce62007-03-15 09:14:47 +0100980/*
Steven Langde890a12011-11-09 14:03:34 +0100981 * Initializes the ioengine configured for a job, if it has not been done so
982 * already.
983 */
984int ioengine_load(struct thread_data *td)
985{
986 const char *engine;
987
988 /*
989 * Engine has already been loaded.
990 */
991 if (td->io_ops)
992 return 0;
Jens Axboe6e5c4b82013-05-27 09:50:31 +0200993 if (!td->o.ioengine) {
994 log_err("fio: internal fault, no IO engine specified\n");
995 return 1;
996 }
Steven Langde890a12011-11-09 14:03:34 +0100997
998 engine = get_engine_name(td->o.ioengine);
999 td->io_ops = load_ioengine(td, engine);
1000 if (!td->io_ops) {
1001 log_err("fio: failed to load engine %s\n", engine);
1002 return 1;
1003 }
1004
1005 if (td->io_ops->option_struct_size && td->io_ops->options) {
1006 /*
1007 * In cases where td->eo is set, clone it for a child thread.
1008 * This requires that the parent thread has the same ioengine,
1009 * but that requirement must be enforced by the code which
1010 * cloned the thread.
1011 */
1012 void *origeo = td->eo;
1013 /*
1014 * Otherwise use the default thread options.
1015 */
1016 if (!origeo && td != &def_thread && def_thread.eo &&
1017 def_thread.io_ops->options == td->io_ops->options)
1018 origeo = def_thread.eo;
1019
1020 options_init(td->io_ops->options);
1021 td->eo = malloc(td->io_ops->option_struct_size);
1022 /*
1023 * Use the default thread as an option template if this uses the
1024 * same options structure and there are non-default options
1025 * used.
1026 */
1027 if (origeo) {
1028 memcpy(td->eo, origeo, td->io_ops->option_struct_size);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001029 options_mem_dupe(td->io_ops->options, td->eo);
Steven Langde890a12011-11-09 14:03:34 +01001030 } else {
1031 memset(td->eo, 0, td->io_ops->option_struct_size);
1032 fill_default_options(td->eo, td->io_ops->options);
1033 }
1034 *(struct thread_data **)td->eo = td;
1035 }
1036
Elliott Hugheseda3a602017-05-19 18:53:02 -07001037 if (td->o.odirect)
1038 td->io_ops->flags |= FIO_RAWIO;
1039
1040 td_set_ioengine_flags(td);
Steven Langde890a12011-11-09 14:03:34 +01001041 return 0;
1042}
1043
Jens Axboed72be542012-11-30 19:37:46 +01001044static void init_flags(struct thread_data *td)
1045{
1046 struct thread_options *o = &td->o;
1047
1048 if (o->verify_backlog)
1049 td->flags |= TD_F_VER_BACKLOG;
1050 if (o->trim_backlog)
1051 td->flags |= TD_F_TRIM_BACKLOG;
1052 if (o->read_iolog_file)
1053 td->flags |= TD_F_READ_IOLOG;
1054 if (o->refill_buffers)
1055 td->flags |= TD_F_REFILL_BUFFERS;
Jens Axboed3d94012014-12-09 13:59:58 -07001056 /*
Jens Axboe2e2956c2014-12-20 14:42:56 -07001057 * Always scramble buffers if asked to
Jens Axboed3d94012014-12-09 13:59:58 -07001058 */
Jens Axboe2e2956c2014-12-20 14:42:56 -07001059 if (o->scramble_buffers && fio_option_is_set(o, scramble_buffers))
1060 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1061 /*
1062 * But also scramble buffers, unless we were explicitly asked
1063 * to zero them.
1064 */
1065 if (o->scramble_buffers && !(o->zero_buffers &&
1066 fio_option_is_set(o, zero_buffers)))
Jens Axboed72be542012-11-30 19:37:46 +01001067 td->flags |= TD_F_SCRAMBLE_BUFFERS;
1068 if (o->verify != VERIFY_NONE)
1069 td->flags |= TD_F_VER_NONE;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001070
1071 if (o->verify_async || o->io_submit_mode == IO_MODE_OFFLOAD)
1072 td->flags |= TD_F_NEED_LOCK;
1073
1074 if (o->mem_type == MEM_CUDA_MALLOC)
1075 td->flags &= ~TD_F_SCRAMBLE_BUFFERS;
Jens Axboed72be542012-11-30 19:37:46 +01001076}
1077
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001078static int setup_random_seeds(struct thread_data *td)
1079{
1080 unsigned long seed;
1081 unsigned int i;
1082
Elliott Hugheseda3a602017-05-19 18:53:02 -07001083 if (!td->o.rand_repeatable && !fio_option_is_set(&td->o, rand_seed))
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001084 return init_random_state(td, td->rand_seeds, sizeof(td->rand_seeds));
1085
Elliott Hugheseda3a602017-05-19 18:53:02 -07001086 seed = td->o.rand_seed;
Jens Axboe04778ba2014-01-10 20:57:01 -07001087 for (i = 0; i < 4; i++)
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001088 seed *= 0x9e370001UL;
1089
1090 for (i = 0; i < FIO_RAND_NR_OFFS; i++) {
Elliott Hugheseda3a602017-05-19 18:53:02 -07001091 td->rand_seeds[i] = seed * td->thread_number + i;
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001092 seed *= 0x9e370001UL;
1093 }
1094
1095 td_fill_rand_seeds(td);
1096 return 0;
1097}
1098
Jens Axboede98bd32013-04-05 11:09:20 +02001099enum {
1100 FPRE_NONE = 0,
1101 FPRE_JOBNAME,
1102 FPRE_JOBNUM,
1103 FPRE_FILENUM
1104};
1105
1106static struct fpre_keyword {
1107 const char *keyword;
1108 size_t strlen;
1109 int key;
1110} fpre_keywords[] = {
1111 { .keyword = "$jobname", .key = FPRE_JOBNAME, },
1112 { .keyword = "$jobnum", .key = FPRE_JOBNUM, },
1113 { .keyword = "$filenum", .key = FPRE_FILENUM, },
1114 { .keyword = NULL, },
1115 };
1116
Jens Axboe3660cea2014-04-15 08:18:08 -06001117static char *make_filename(char *buf, size_t buf_size,struct thread_options *o,
Jens Axboede98bd32013-04-05 11:09:20 +02001118 const char *jobname, int jobnum, int filenum)
1119{
1120 struct fpre_keyword *f;
1121 char copy[PATH_MAX];
Jens Axboeb400a202014-04-14 10:13:46 -06001122 size_t dst_left = PATH_MAX - 1;
Jens Axboede98bd32013-04-05 11:09:20 +02001123
1124 if (!o->filename_format || !strlen(o->filename_format)) {
1125 sprintf(buf, "%s.%d.%d", jobname, jobnum, filenum);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001126 return buf;
Jens Axboede98bd32013-04-05 11:09:20 +02001127 }
1128
1129 for (f = &fpre_keywords[0]; f->keyword; f++)
1130 f->strlen = strlen(f->keyword);
1131
Jens Axboe3660cea2014-04-15 08:18:08 -06001132 buf[buf_size - 1] = '\0';
1133 strncpy(buf, o->filename_format, buf_size - 1);
1134
Jens Axboede98bd32013-04-05 11:09:20 +02001135 memset(copy, 0, sizeof(copy));
1136 for (f = &fpre_keywords[0]; f->keyword; f++) {
1137 do {
1138 size_t pre_len, post_start = 0;
1139 char *str, *dst = copy;
1140
Jens Axboee25b6d52013-04-05 14:44:51 +02001141 str = strcasestr(buf, f->keyword);
Jens Axboede98bd32013-04-05 11:09:20 +02001142 if (!str)
1143 break;
1144
1145 pre_len = str - buf;
1146 if (strlen(str) != f->strlen)
1147 post_start = pre_len + f->strlen;
1148
1149 if (pre_len) {
1150 strncpy(dst, buf, pre_len);
1151 dst += pre_len;
Jens Axboe73a467e2014-04-14 08:34:43 -06001152 dst_left -= pre_len;
Jens Axboede98bd32013-04-05 11:09:20 +02001153 }
1154
1155 switch (f->key) {
Jens Axboe73a467e2014-04-14 08:34:43 -06001156 case FPRE_JOBNAME: {
1157 int ret;
1158
1159 ret = snprintf(dst, dst_left, "%s", jobname);
1160 if (ret < 0)
1161 break;
Jens Axboeb0c36cf2015-01-04 13:33:37 -07001162 else if (ret > dst_left) {
1163 log_err("fio: truncated filename\n");
1164 dst += dst_left;
1165 dst_left = 0;
1166 } else {
1167 dst += ret;
1168 dst_left -= ret;
1169 }
Jens Axboede98bd32013-04-05 11:09:20 +02001170 break;
Jens Axboe73a467e2014-04-14 08:34:43 -06001171 }
1172 case FPRE_JOBNUM: {
1173 int ret;
1174
1175 ret = snprintf(dst, dst_left, "%d", jobnum);
1176 if (ret < 0)
1177 break;
Jens Axboeb0c36cf2015-01-04 13:33:37 -07001178 else if (ret > dst_left) {
1179 log_err("fio: truncated filename\n");
1180 dst += dst_left;
1181 dst_left = 0;
1182 } else {
1183 dst += ret;
1184 dst_left -= ret;
1185 }
Jens Axboede98bd32013-04-05 11:09:20 +02001186 break;
Jens Axboe73a467e2014-04-14 08:34:43 -06001187 }
1188 case FPRE_FILENUM: {
1189 int ret;
1190
1191 ret = snprintf(dst, dst_left, "%d", filenum);
1192 if (ret < 0)
1193 break;
Jens Axboeb0c36cf2015-01-04 13:33:37 -07001194 else if (ret > dst_left) {
1195 log_err("fio: truncated filename\n");
1196 dst += dst_left;
1197 dst_left = 0;
1198 } else {
1199 dst += ret;
1200 dst_left -= ret;
1201 }
Jens Axboede98bd32013-04-05 11:09:20 +02001202 break;
Jens Axboe73a467e2014-04-14 08:34:43 -06001203 }
Jens Axboede98bd32013-04-05 11:09:20 +02001204 default:
1205 assert(0);
1206 break;
1207 }
1208
1209 if (post_start)
Jens Axboe73a467e2014-04-14 08:34:43 -06001210 strncpy(dst, buf + post_start, dst_left);
Jens Axboede98bd32013-04-05 11:09:20 +02001211
Jens Axboe3660cea2014-04-15 08:18:08 -06001212 strncpy(buf, copy, buf_size - 1);
Jens Axboede98bd32013-04-05 11:09:20 +02001213 } while (1);
1214 }
1215
1216 return buf;
1217}
Jens Axboef9633d72013-09-06 09:59:56 -06001218
Elliott Hugheseda3a602017-05-19 18:53:02 -07001219bool parse_dryrun(void)
Jens Axboef9633d72013-09-06 09:59:56 -06001220{
1221 return dump_cmdline || parse_only;
1222}
1223
Elliott Hugheseda3a602017-05-19 18:53:02 -07001224static void gen_log_name(char *name, size_t size, const char *logtype,
1225 const char *logname, unsigned int num,
1226 const char *suf, int per_job)
1227{
1228 if (per_job)
1229 snprintf(name, size, "%s_%s.%d.%s", logname, logtype, num, suf);
1230 else
1231 snprintf(name, size, "%s_%s.%s", logname, logtype, suf);
1232}
1233
1234static int check_waitees(char *waitee)
1235{
1236 struct thread_data *td;
1237 int i, ret = 0;
1238
1239 for_each_td(td, i) {
1240 if (td->subjob_number)
1241 continue;
1242
1243 ret += !strcmp(td->o.name, waitee);
1244 }
1245
1246 return ret;
1247}
1248
1249static bool wait_for_ok(const char *jobname, struct thread_options *o)
1250{
1251 int nw;
1252
1253 if (!o->wait_for)
1254 return true;
1255
1256 if (!strcmp(jobname, o->wait_for)) {
1257 log_err("%s: a job cannot wait for itself (wait_for=%s).\n",
1258 jobname, o->wait_for);
1259 return false;
1260 }
1261
1262 if (!(nw = check_waitees(o->wait_for))) {
1263 log_err("%s: waitee job %s unknown.\n", jobname, o->wait_for);
1264 return false;
1265 }
1266
1267 if (nw > 1) {
1268 log_err("%s: multiple waitees %s found,\n"
1269 "please avoid duplicates when using wait_for option.\n",
1270 jobname, o->wait_for);
1271 return false;
1272 }
1273
1274 return true;
1275}
1276
Steven Langde890a12011-11-09 14:03:34 +01001277/*
Jens Axboe906c8d72006-06-13 09:37:56 +02001278 * Adds a job to the list of things todo. Sanitizes the various options
1279 * to make sure we don't have conflicts, and initializes various
1280 * members of td.
1281 */
Jens Axboeb7cfb2e2012-03-12 07:47:27 +01001282static int add_job(struct thread_data *td, const char *jobname, int job_add_num,
Jens Axboe46bcd492012-03-14 11:31:21 +01001283 int recursed, int client_type)
Jens Axboeebac4652005-12-08 15:25:21 +01001284{
Jens Axboeaf52b342007-03-13 10:07:47 +01001285 unsigned int i;
Jens Axboeaf52b342007-03-13 10:07:47 +01001286 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +01001287 int numjobs, file_alloced;
Jens Axboede98bd32013-04-05 11:09:20 +02001288 struct thread_options *o = &td->o;
Jens Axboe987c4f42014-07-01 16:29:12 -06001289 char logname[PATH_MAX + 32];
Jens Axboeebac4652005-12-08 15:25:21 +01001290
Jens Axboeebac4652005-12-08 15:25:21 +01001291 /*
1292 * the def_thread is just for options, it's not a real job
1293 */
1294 if (td == &def_thread)
1295 return 0;
1296
Jens Axboed72be542012-11-30 19:37:46 +01001297 init_flags(td);
1298
Jens Axboecca73aa2007-04-04 11:09:19 +02001299 /*
1300 * if we are just dumping the output command line, don't add the job
1301 */
Jens Axboef9633d72013-09-06 09:59:56 -06001302 if (parse_dryrun()) {
Jens Axboecca73aa2007-04-04 11:09:19 +02001303 put_job(td);
1304 return 0;
1305 }
1306
Jens Axboe46bcd492012-03-14 11:31:21 +01001307 td->client_type = client_type;
1308
Jens Axboe58c55ba2010-03-09 12:20:08 +01001309 if (profile_td_init(td))
Jens Axboe66251552011-05-12 10:14:57 +02001310 goto err;
Jens Axboe58c55ba2010-03-09 12:20:08 +01001311
Steven Langde890a12011-11-09 14:03:34 +01001312 if (ioengine_load(td))
Jens Axboe205927a2007-03-15 11:06:32 +01001313 goto err;
Jens Axboedf641192006-10-12 07:55:41 +02001314
Jens Axboee132cba2007-03-14 14:23:54 +01001315 file_alloced = 0;
Jens Axboede98bd32013-04-05 11:09:20 +02001316 if (!o->filename && !td->files_index && !o->read_iolog_file) {
Jens Axboee132cba2007-03-14 14:23:54 +01001317 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +01001318
Elliott Hugheseda3a602017-05-19 18:53:02 -07001319 if (o->nr_files == 1 && exists_and_not_regfile(jobname))
Jens Axboe5903e7b2014-02-26 13:42:13 -08001320 add_file(td, jobname, job_add_num, 0);
Jens Axboe7b05a212007-03-13 11:17:07 +01001321 else {
Jens Axboede98bd32013-04-05 11:09:20 +02001322 for (i = 0; i < o->nr_files; i++)
Jens Axboe3660cea2014-04-15 08:18:08 -06001323 add_file(td, make_filename(fname, sizeof(fname), o, jobname, job_add_num, i), job_add_num, 0);
Jens Axboeaf52b342007-03-13 10:07:47 +01001324 }
Jens Axboe0af7b542006-02-17 10:10:12 +01001325 }
Jens Axboeebac4652005-12-08 15:25:21 +01001326
Jens Axboe4e991c22007-03-15 11:41:11 +01001327 if (fixup_options(td))
1328 goto err;
Jens Axboee0a22332006-12-20 12:54:25 +01001329
Elliott Hugheseda3a602017-05-19 18:53:02 -07001330 /*
1331 * Belongs to fixup_options, but o->name is not necessarily set as yet
1332 */
1333 if (!wait_for_ok(jobname, o))
1334 goto err;
1335
Dan Ehrenberg9e684a42012-02-20 11:05:14 +01001336 flow_init_job(td);
1337
Steven Langde890a12011-11-09 14:03:34 +01001338 /*
1339 * IO engines only need this for option callbacks, and the address may
1340 * change in subprocesses.
1341 */
1342 if (td->eo)
1343 *(struct thread_data **)td->eo = NULL;
1344
Elliott Hugheseda3a602017-05-19 18:53:02 -07001345 if (td_ioengine_flagged(td, FIO_DISKLESSIO)) {
Jens Axboe07eb79d2007-04-12 13:02:38 +02001346 struct fio_file *f;
1347
1348 for_each_file(td, f, i)
1349 f->real_file_size = -1ULL;
1350 }
1351
Jens Axboe521da522012-08-02 11:21:36 +02001352 td->mutex = fio_mutex_init(FIO_MUTEX_LOCKED);
Jens Axboeebac4652005-12-08 15:25:21 +01001353
Jens Axboede98bd32013-04-05 11:09:20 +02001354 td->ts.clat_percentiles = o->clat_percentiles;
1355 td->ts.percentile_precision = o->percentile_precision;
1356 memcpy(td->ts.percentile_list, o->percentile_list, sizeof(o->percentile_list));
Yu-ju Hong83349192011-08-13 00:53:44 +02001357
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001358 for (i = 0; i < DDIR_RWDIR_CNT; i++) {
1359 td->ts.clat_stat[i].min_val = ULONG_MAX;
1360 td->ts.slat_stat[i].min_val = ULONG_MAX;
1361 td->ts.lat_stat[i].min_val = ULONG_MAX;
1362 td->ts.bw_stat[i].min_val = ULONG_MAX;
1363 }
Jens Axboede98bd32013-04-05 11:09:20 +02001364 td->ddir_seq_nr = o->ddir_seq_nr;
Jens Axboeebac4652005-12-08 15:25:21 +01001365
Jens Axboede98bd32013-04-05 11:09:20 +02001366 if ((o->stonewall || o->new_group) && prev_group_jobs) {
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001367 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001368 groupid++;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001369 if (groupid == INT_MAX) {
1370 log_err("fio: too many groups defined\n");
1371 goto err;
1372 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001373 }
Jens Axboeebac4652005-12-08 15:25:21 +01001374
1375 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001376 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +01001377
Jens Axboe9dbc7bf2013-01-23 09:26:53 -07001378 if (setup_random_seeds(td)) {
Bruce Cran93bcfd22012-02-20 20:18:19 +01001379 td_verror(td, errno, "init_random_state");
Jens Axboe9c60ce62007-03-15 09:14:47 +01001380 goto err;
Bruce Cran93bcfd22012-02-20 20:18:19 +01001381 }
Jens Axboe9c60ce62007-03-15 09:14:47 +01001382
Jens Axboeebac4652005-12-08 15:25:21 +01001383 if (setup_rate(td))
1384 goto err;
1385
Elliott Hugheseda3a602017-05-19 18:53:02 -07001386 if (o->write_lat_log) {
Jens Axboe38a812d2014-07-03 09:10:39 -06001387 struct log_params p = {
1388 .td = td,
1389 .avg_msec = o->log_avg_msec,
Elliott Hugheseda3a602017-05-19 18:53:02 -07001390 .hist_msec = o->log_hist_msec,
1391 .hist_coarseness = o->log_hist_coarseness,
Jens Axboe38a812d2014-07-03 09:10:39 -06001392 .log_type = IO_LOG_TYPE_LAT,
1393 .log_offset = o->log_offset,
1394 .log_gz = o->log_gz,
Jens Axboebac4af12014-07-03 13:42:28 -06001395 .log_gz_store = o->log_gz_store,
Jens Axboe38a812d2014-07-03 09:10:39 -06001396 };
Elliott Hugheseda3a602017-05-19 18:53:02 -07001397 const char *pre = o->lat_log_file ? o->lat_log_file : o->name;
Jens Axboebac4af12014-07-03 13:42:28 -06001398 const char *suf;
Jens Axboe38a812d2014-07-03 09:10:39 -06001399
Jens Axboebac4af12014-07-03 13:42:28 -06001400 if (p.log_gz_store)
1401 suf = "log.fz";
1402 else
1403 suf = "log";
1404
Elliott Hugheseda3a602017-05-19 18:53:02 -07001405 gen_log_name(logname, sizeof(logname), "lat", pre,
1406 td->thread_number, suf, o->per_job_logs);
Jens Axboe38a812d2014-07-03 09:10:39 -06001407 setup_log(&td->lat_log, &p, logname);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001408
1409 gen_log_name(logname, sizeof(logname), "slat", pre,
1410 td->thread_number, suf, o->per_job_logs);
Jens Axboe38a812d2014-07-03 09:10:39 -06001411 setup_log(&td->slat_log, &p, logname);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001412
1413 gen_log_name(logname, sizeof(logname), "clat", pre,
1414 td->thread_number, suf, o->per_job_logs);
Jens Axboe38a812d2014-07-03 09:10:39 -06001415 setup_log(&td->clat_log, &p, logname);
Jens Axboeebac4652005-12-08 15:25:21 +01001416 }
Elliott Hugheseda3a602017-05-19 18:53:02 -07001417
1418 if (o->write_hist_log) {
Jens Axboe38a812d2014-07-03 09:10:39 -06001419 struct log_params p = {
1420 .td = td,
1421 .avg_msec = o->log_avg_msec,
Elliott Hugheseda3a602017-05-19 18:53:02 -07001422 .hist_msec = o->log_hist_msec,
1423 .hist_coarseness = o->log_hist_coarseness,
1424 .log_type = IO_LOG_TYPE_HIST,
1425 .log_offset = o->log_offset,
1426 .log_gz = o->log_gz,
1427 .log_gz_store = o->log_gz_store,
1428 };
1429 const char *pre = o->hist_log_file ? o->hist_log_file : o->name;
1430 const char *suf;
1431
1432#ifndef CONFIG_ZLIB
1433 if (td->client_type) {
1434 log_err("fio: --write_hist_log requires zlib in client/server mode\n");
1435 goto err;
1436 }
1437#endif
1438
1439 if (p.log_gz_store)
1440 suf = "log.fz";
1441 else
1442 suf = "log";
1443
1444 gen_log_name(logname, sizeof(logname), "clat_hist", pre,
1445 td->thread_number, suf, o->per_job_logs);
1446 setup_log(&td->clat_hist_log, &p, logname);
1447 }
1448
1449 if (o->write_bw_log) {
1450 struct log_params p = {
1451 .td = td,
1452 .avg_msec = o->log_avg_msec,
1453 .hist_msec = o->log_hist_msec,
1454 .hist_coarseness = o->log_hist_coarseness,
Jens Axboe38a812d2014-07-03 09:10:39 -06001455 .log_type = IO_LOG_TYPE_BW,
1456 .log_offset = o->log_offset,
1457 .log_gz = o->log_gz,
Jens Axboebac4af12014-07-03 13:42:28 -06001458 .log_gz_store = o->log_gz_store,
Jens Axboe38a812d2014-07-03 09:10:39 -06001459 };
Elliott Hugheseda3a602017-05-19 18:53:02 -07001460 const char *pre = o->bw_log_file ? o->bw_log_file : o->name;
Jens Axboebac4af12014-07-03 13:42:28 -06001461 const char *suf;
Jens Axboe38a812d2014-07-03 09:10:39 -06001462
Elliott Hugheseda3a602017-05-19 18:53:02 -07001463 if (fio_option_is_set(o, bw_avg_time))
1464 p.avg_msec = min(o->log_avg_msec, o->bw_avg_time);
1465 else
1466 o->bw_avg_time = p.avg_msec;
1467
1468 p.hist_msec = o->log_hist_msec;
1469 p.hist_coarseness = o->log_hist_coarseness;
1470
Jens Axboebac4af12014-07-03 13:42:28 -06001471 if (p.log_gz_store)
1472 suf = "log.fz";
1473 else
1474 suf = "log";
1475
Elliott Hugheseda3a602017-05-19 18:53:02 -07001476 gen_log_name(logname, sizeof(logname), "bw", pre,
1477 td->thread_number, suf, o->per_job_logs);
Jens Axboe38a812d2014-07-03 09:10:39 -06001478 setup_log(&td->bw_log, &p, logname);
Jens Axboe987c4f42014-07-01 16:29:12 -06001479 }
Elliott Hugheseda3a602017-05-19 18:53:02 -07001480 if (o->write_iops_log) {
Jens Axboe38a812d2014-07-03 09:10:39 -06001481 struct log_params p = {
1482 .td = td,
1483 .avg_msec = o->log_avg_msec,
Elliott Hugheseda3a602017-05-19 18:53:02 -07001484 .hist_msec = o->log_hist_msec,
1485 .hist_coarseness = o->log_hist_coarseness,
Jens Axboe38a812d2014-07-03 09:10:39 -06001486 .log_type = IO_LOG_TYPE_IOPS,
1487 .log_offset = o->log_offset,
1488 .log_gz = o->log_gz,
Jens Axboebac4af12014-07-03 13:42:28 -06001489 .log_gz_store = o->log_gz_store,
Jens Axboe38a812d2014-07-03 09:10:39 -06001490 };
Elliott Hugheseda3a602017-05-19 18:53:02 -07001491 const char *pre = o->iops_log_file ? o->iops_log_file : o->name;
Jens Axboebac4af12014-07-03 13:42:28 -06001492 const char *suf;
Jens Axboe38a812d2014-07-03 09:10:39 -06001493
Elliott Hugheseda3a602017-05-19 18:53:02 -07001494 if (fio_option_is_set(o, iops_avg_time))
1495 p.avg_msec = min(o->log_avg_msec, o->iops_avg_time);
1496 else
1497 o->iops_avg_time = p.avg_msec;
1498
1499 p.hist_msec = o->log_hist_msec;
1500 p.hist_coarseness = o->log_hist_coarseness;
1501
Jens Axboebac4af12014-07-03 13:42:28 -06001502 if (p.log_gz_store)
1503 suf = "log.fz";
1504 else
1505 suf = "log";
1506
Elliott Hugheseda3a602017-05-19 18:53:02 -07001507 gen_log_name(logname, sizeof(logname), "iops", pre,
1508 td->thread_number, suf, o->per_job_logs);
Jens Axboe38a812d2014-07-03 09:10:39 -06001509 setup_log(&td->iops_log, &p, logname);
Jens Axboe987c4f42014-07-01 16:29:12 -06001510 }
Jens Axboeebac4652005-12-08 15:25:21 +01001511
Jens Axboede98bd32013-04-05 11:09:20 +02001512 if (!o->name)
1513 o->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +02001514
Elliott Hugheseda3a602017-05-19 18:53:02 -07001515 if (output_format & FIO_OUTPUT_NORMAL) {
Jens Axboeb990b5c2006-09-14 09:48:22 +02001516 if (!job_add_num) {
Jens Axboeb7cfb2e2012-03-12 07:47:27 +01001517 if (is_backend && !recursed)
Jens Axboe2f122b12012-03-15 13:10:19 +01001518 fio_server_send_add_job(td);
Jens Axboe807f9972012-03-02 10:25:24 +01001519
Elliott Hugheseda3a602017-05-19 18:53:02 -07001520 if (!td_ioengine_flagged(td, FIO_NOIO)) {
Jens Axboed21e2532013-07-25 13:01:11 -06001521 char *c1, *c2, *c3, *c4;
1522 char *c5 = NULL, *c6 = NULL;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001523 int i2p = is_power_of_2(o->kb_base);
Jens Axboef8977ee2006-11-06 14:03:03 +01001524
Elliott Hugheseda3a602017-05-19 18:53:02 -07001525 c1 = num2str(o->min_bs[DDIR_READ], 4, 1, i2p, N2S_BYTE);
1526 c2 = num2str(o->max_bs[DDIR_READ], 4, 1, i2p, N2S_BYTE);
1527 c3 = num2str(o->min_bs[DDIR_WRITE], 4, 1, i2p, N2S_BYTE);
1528 c4 = num2str(o->max_bs[DDIR_WRITE], 4, 1, i2p, N2S_BYTE);
Jens Axboef8977ee2006-11-06 14:03:03 +01001529
Jens Axboed21e2532013-07-25 13:01:11 -06001530 if (!o->bs_is_seq_rand) {
Elliott Hugheseda3a602017-05-19 18:53:02 -07001531 c5 = num2str(o->min_bs[DDIR_TRIM], 4, 1, i2p, N2S_BYTE);
1532 c6 = num2str(o->max_bs[DDIR_TRIM], 4, 1, i2p, N2S_BYTE);
Jens Axboed21e2532013-07-25 13:01:11 -06001533 }
1534
1535 log_info("%s: (g=%d): rw=%s, ", td->o.name,
1536 td->groupid,
1537 ddir_str(o->td_ddir));
1538
1539 if (o->bs_is_seq_rand)
Elliott Hugheseda3a602017-05-19 18:53:02 -07001540 log_info("bs=(R) %s-%s, (W) %s-%s, bs_is_seq_rand, ",
Jens Axboed21e2532013-07-25 13:01:11 -06001541 c1, c2, c3, c4);
1542 else
Elliott Hugheseda3a602017-05-19 18:53:02 -07001543 log_info("bs=(R) %s-%s, (W) %s-%s, (T) %s-%s, ",
Jens Axboed21e2532013-07-25 13:01:11 -06001544 c1, c2, c3, c4, c5, c6);
1545
1546 log_info("ioengine=%s, iodepth=%u\n",
Jens Axboede98bd32013-04-05 11:09:20 +02001547 td->io_ops->name, o->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +01001548
1549 free(c1);
1550 free(c2);
1551 free(c3);
1552 free(c4);
Shaohua Li6eaf09d2012-09-14 08:49:43 +02001553 free(c5);
1554 free(c6);
Jens Axboef8977ee2006-11-06 14:03:03 +01001555 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001556 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +01001557 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001558 }
Jens Axboeebac4652005-12-08 15:25:21 +01001559
Elliott Hugheseda3a602017-05-19 18:53:02 -07001560 if (td_steadystate_init(td))
1561 goto err;
1562
Jens Axboeebac4652005-12-08 15:25:21 +01001563 /*
1564 * recurse add identical jobs, clear numjobs and stonewall options
1565 * as they don't apply to sub-jobs
1566 */
Jens Axboede98bd32013-04-05 11:09:20 +02001567 numjobs = o->numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +01001568 while (--numjobs) {
Elliott Hugheseda3a602017-05-19 18:53:02 -07001569 struct thread_data *td_new = get_new_job(false, td, true, jobname);
Jens Axboeebac4652005-12-08 15:25:21 +01001570
1571 if (!td_new)
1572 goto err;
1573
Jens Axboe2dc1bbe2007-03-15 15:01:33 +01001574 td_new->o.numjobs = 1;
1575 td_new->o.stonewall = 0;
Jens Axboe92c1d412007-03-28 10:04:08 +02001576 td_new->o.new_group = 0;
Jiri Horky5a65b4e2014-07-25 09:55:03 +02001577 td_new->subjob_number = numjobs;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001578 td_new->o.ss_dur = o->ss_dur * 1000000l;
1579 td_new->o.ss_limit = o->ss_limit;
Jens Axboee132cba2007-03-14 14:23:54 +01001580
1581 if (file_alloced) {
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001582 if (td_new->files) {
1583 struct fio_file *f;
1584 for_each_file(td_new, f, i) {
1585 if (f->file_name)
Andrey Kuzminece6d642014-04-01 12:27:04 -06001586 sfree(f->file_name);
1587 sfree(f);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001588 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001589 free(td_new->files);
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001590 td_new->files = NULL;
1591 }
Andrey Kuzminece6d642014-04-01 12:27:04 -06001592 td_new->files_index = 0;
1593 td_new->files_size = 0;
Christian Ehrhardt455a50f2014-02-19 10:42:41 -08001594 if (td_new->o.filename) {
1595 free(td_new->o.filename);
1596 td_new->o.filename = NULL;
1597 }
Jens Axboee132cba2007-03-14 14:23:54 +01001598 }
1599
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08001600 if (add_job(td_new, jobname, numjobs, 1, client_type))
Jens Axboeebac4652005-12-08 15:25:21 +01001601 goto err;
1602 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001603
Jens Axboeebac4652005-12-08 15:25:21 +01001604 return 0;
1605err:
1606 put_job(td);
1607 return -1;
1608}
1609
Jens Axboe79d16312010-03-04 12:43:20 +01001610/*
1611 * Parse as if 'o' was a command line
1612 */
Jens Axboe46bcd492012-03-14 11:31:21 +01001613void add_job_opts(const char **o, int client_type)
Jens Axboe79d16312010-03-04 12:43:20 +01001614{
1615 struct thread_data *td, *td_parent;
1616 int i, in_global = 1;
1617 char jobname[32];
1618
1619 i = 0;
1620 td_parent = td = NULL;
1621 while (o[i]) {
1622 if (!strncmp(o[i], "name", 4)) {
1623 in_global = 0;
1624 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001625 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001626 td = NULL;
1627 sprintf(jobname, "%s", o[i] + 5);
1628 }
1629 if (in_global && !td_parent)
Elliott Hugheseda3a602017-05-19 18:53:02 -07001630 td_parent = get_new_job(true, &def_thread, false, jobname);
Jens Axboe79d16312010-03-04 12:43:20 +01001631 else if (!in_global && !td) {
1632 if (!td_parent)
1633 td_parent = &def_thread;
Elliott Hugheseda3a602017-05-19 18:53:02 -07001634 td = get_new_job(false, td_parent, false, jobname);
Jens Axboe79d16312010-03-04 12:43:20 +01001635 }
1636 if (in_global)
Elliott Hugheseda3a602017-05-19 18:53:02 -07001637 fio_options_parse(td_parent, (char **) &o[i], 1);
Jens Axboe79d16312010-03-04 12:43:20 +01001638 else
Elliott Hugheseda3a602017-05-19 18:53:02 -07001639 fio_options_parse(td, (char **) &o[i], 1);
Jens Axboe79d16312010-03-04 12:43:20 +01001640 i++;
1641 }
1642
1643 if (td)
Jens Axboe46bcd492012-03-14 11:31:21 +01001644 add_job(td, jobname, 0, 0, client_type);
Jens Axboe79d16312010-03-04 12:43:20 +01001645}
1646
Jens Axboe01f06b62008-02-18 20:53:47 +01001647static int skip_this_section(const char *name)
1648{
Jens Axboead0a2732011-03-11 10:16:17 +01001649 int i;
1650
1651 if (!nr_job_sections)
Jens Axboe01f06b62008-02-18 20:53:47 +01001652 return 0;
1653 if (!strncmp(name, "global", 6))
1654 return 0;
1655
Jens Axboead0a2732011-03-11 10:16:17 +01001656 for (i = 0; i < nr_job_sections; i++)
1657 if (!strcmp(job_sections[i], name))
1658 return 0;
1659
1660 return 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001661}
1662
Jens Axboeebac4652005-12-08 15:25:21 +01001663static int is_empty_or_comment(char *line)
1664{
1665 unsigned int i;
1666
1667 for (i = 0; i < strlen(line); i++) {
1668 if (line[i] == ';')
1669 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001670 if (line[i] == '#')
1671 return 1;
Jens Axboe76cd9372011-07-08 21:14:57 +02001672 if (!isspace((int) line[i]) && !iscntrl((int) line[i]))
Jens Axboeebac4652005-12-08 15:25:21 +01001673 return 0;
1674 }
1675
1676 return 1;
1677}
1678
Jens Axboe313cb202006-12-21 09:50:00 +01001679/*
Jens Axboe07261982006-06-07 10:51:12 +02001680 * This is our [ini] type file parser.
1681 */
Elliott Hugheseda3a602017-05-19 18:53:02 -07001682static int __parse_jobs_ini(struct thread_data *td,
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001683 char *file, int is_buf, int stonewall_flag, int type,
1684 int nested, char *name, char ***popts, int *aopts, int *nopts)
Jens Axboeebac4652005-12-08 15:25:21 +01001685{
Elliott Hugheseda3a602017-05-19 18:53:02 -07001686 bool global = false;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001687 char *string;
Jens Axboeebac4652005-12-08 15:25:21 +01001688 FILE *f;
1689 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001690 int ret = 0, stonewall;
Jens Axboe097b2992007-04-19 09:41:45 +02001691 int first_sect = 1;
Jens Axboeccf8f122007-09-27 10:48:55 +02001692 int skip_fgets = 0;
Jens Axboe01f06b62008-02-18 20:53:47 +01001693 int inside_skip = 0;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001694 char **opts;
1695 int i, alloc_opts, num_opts;
Jens Axboeebac4652005-12-08 15:25:21 +01001696
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001697 dprint(FD_PARSE, "Parsing ini file %s\n", file);
1698 assert(td || !nested);
1699
Jens Axboe50d16972011-09-29 17:45:28 -06001700 if (is_buf)
1701 f = NULL;
1702 else {
1703 if (!strcmp(file, "-"))
1704 f = stdin;
1705 else
1706 f = fopen(file, "r");
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001707
Jens Axboe50d16972011-09-29 17:45:28 -06001708 if (!f) {
Jens Axboe35899182014-10-07 20:56:28 -06001709 int __err = errno;
1710
1711 log_err("fio: unable to open '%s' job file\n", file);
Jens Axboe682c7e62014-10-08 09:55:22 -06001712 if (td)
1713 td_verror(td, __err, "job file open");
Jens Axboe50d16972011-09-29 17:45:28 -06001714 return 1;
1715 }
Jens Axboeebac4652005-12-08 15:25:21 +01001716 }
1717
1718 string = malloc(4096);
Jens Axboe7f7e6e52007-07-19 14:50:05 +02001719
1720 /*
1721 * it's really 256 + small bit, 280 should suffice
1722 */
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001723 if (!nested) {
1724 name = malloc(280);
1725 memset(name, 0, 280);
1726 }
Jens Axboeebac4652005-12-08 15:25:21 +01001727
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001728 opts = NULL;
1729 if (nested && popts) {
1730 opts = *popts;
1731 alloc_opts = *aopts;
1732 num_opts = *nopts;
1733 }
1734
1735 if (!opts) {
1736 alloc_opts = 8;
1737 opts = malloc(sizeof(char *) * alloc_opts);
1738 num_opts = 0;
1739 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001740
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001741 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001742 do {
Jens Axboeccf8f122007-09-27 10:48:55 +02001743 /*
1744 * if skip_fgets is set, we already have loaded a line we
1745 * haven't handled.
1746 */
1747 if (!skip_fgets) {
Jens Axboe50d16972011-09-29 17:45:28 -06001748 if (is_buf)
1749 p = strsep(&file, "\n");
1750 else
Jens Axboe43f74792011-10-15 14:37:26 +02001751 p = fgets(string, 4096, f);
Jens Axboeccf8f122007-09-27 10:48:55 +02001752 if (!p)
1753 break;
1754 }
gurudas paicdc7f192007-03-29 10:10:56 +02001755
Jens Axboeccf8f122007-09-27 10:48:55 +02001756 skip_fgets = 0;
gurudas paicdc7f192007-03-29 10:10:56 +02001757 strip_blank_front(&p);
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001758 strip_blank_end(p);
gurudas paicdc7f192007-03-29 10:10:56 +02001759
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001760 dprint(FD_PARSE, "%s\n", p);
Jens Axboeebac4652005-12-08 15:25:21 +01001761 if (is_empty_or_comment(p))
1762 continue;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001763
1764 if (!nested) {
1765 if (sscanf(p, "[%255[^\n]]", name) != 1) {
1766 if (inside_skip)
1767 continue;
1768
1769 log_err("fio: option <%s> outside of "
1770 "[] job section\n", p);
Jens Axboec0e19152014-10-06 19:06:55 -06001771 ret = 1;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001772 break;
1773 }
1774
1775 name[strlen(name) - 1] = '\0';
1776
1777 if (skip_this_section(name)) {
1778 inside_skip = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01001779 continue;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001780 } else
1781 inside_skip = 0;
1782
1783 dprint(FD_PARSE, "Parsing section [%s]\n", name);
1784
1785 global = !strncmp(name, "global", 6);
1786
1787 if (dump_cmdline) {
1788 if (first_sect)
1789 log_info("fio ");
1790 if (!global)
1791 log_info("--name=%s ", name);
1792 first_sect = 0;
1793 }
1794
Elliott Hugheseda3a602017-05-19 18:53:02 -07001795 td = get_new_job(global, &def_thread, false, name);
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001796 if (!td) {
1797 ret = 1;
1798 break;
1799 }
1800
1801 /*
1802 * Separate multiple job files by a stonewall
1803 */
1804 if (!global && stonewall) {
1805 td->o.stonewall = stonewall;
1806 stonewall = 0;
1807 }
1808
1809 num_opts = 0;
1810 memset(opts, 0, alloc_opts * sizeof(char *));
Jens Axboe6c7c7da2007-03-29 14:12:57 -08001811 }
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001812 else
1813 skip_fgets = 1;
Jens Axboe3b8b7132008-06-10 19:46:23 +02001814
Jens Axboe50d16972011-09-29 17:45:28 -06001815 while (1) {
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001816 if (!skip_fgets) {
1817 if (is_buf)
1818 p = strsep(&file, "\n");
1819 else
1820 p = fgets(string, 4096, f);
1821 if (!p)
1822 break;
1823 dprint(FD_PARSE, "%s", p);
1824 }
Jens Axboe50d16972011-09-29 17:45:28 -06001825 else
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001826 skip_fgets = 0;
Jens Axboe50d16972011-09-29 17:45:28 -06001827
Jens Axboeebac4652005-12-08 15:25:21 +01001828 if (is_empty_or_comment(p))
1829 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001830
Jens Axboeb6754f92006-05-30 14:29:32 +02001831 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001832
Jens Axboeccf8f122007-09-27 10:48:55 +02001833 /*
1834 * new section, break out and make sure we don't
1835 * fgets() a new line at the top.
1836 */
1837 if (p[0] == '[') {
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001838 if (nested) {
1839 log_err("No new sections in included files\n");
1840 return 1;
1841 }
1842
Jens Axboeccf8f122007-09-27 10:48:55 +02001843 skip_fgets = 1;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001844 break;
Jens Axboeccf8f122007-09-27 10:48:55 +02001845 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001846
Jens Axboe4ae3f762006-05-30 13:35:14 +02001847 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001848
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001849 if (!strncmp(p, "include", strlen("include"))) {
Elliott Hugheseda3a602017-05-19 18:53:02 -07001850 char *filename = p + strlen("include") + 1,
1851 *ts, *full_fn = NULL;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001852
Elliott Hugheseda3a602017-05-19 18:53:02 -07001853 /*
1854 * Allow for the include filename
1855 * specification to be relative.
1856 */
1857 if (access(filename, F_OK) &&
1858 (ts = strrchr(file, '/'))) {
1859 int len = ts - file +
1860 strlen(filename) + 2;
1861
1862 if (!(full_fn = calloc(1, len))) {
1863 ret = ENOMEM;
1864 break;
1865 }
1866
1867 strncpy(full_fn,
1868 file, (ts - file) + 1);
1869 strncpy(full_fn + (ts - file) + 1,
1870 filename, strlen(filename));
1871 full_fn[len - 1] = 0;
1872 filename = full_fn;
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001873 }
Elliott Hugheseda3a602017-05-19 18:53:02 -07001874
1875 ret = __parse_jobs_ini(td, filename, is_buf,
1876 stonewall_flag, type, 1,
1877 name, &opts,
1878 &alloc_opts, &num_opts);
1879
1880 if (ret) {
1881 log_err("Error %d while parsing "
1882 "include file %s\n",
1883 ret, filename);
1884 }
1885
1886 if (full_fn)
1887 free(full_fn);
1888
1889 if (ret)
1890 break;
1891
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001892 continue;
1893 }
1894
Jens Axboe3b8b7132008-06-10 19:46:23 +02001895 if (num_opts == alloc_opts) {
1896 alloc_opts <<= 1;
1897 opts = realloc(opts,
1898 alloc_opts * sizeof(char *));
1899 }
1900
1901 opts[num_opts] = strdup(p);
1902 num_opts++;
Jens Axboeebac4652005-12-08 15:25:21 +01001903 }
Jens Axboeebac4652005-12-08 15:25:21 +01001904
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001905 if (nested) {
1906 *popts = opts;
1907 *aopts = alloc_opts;
1908 *nopts = num_opts;
1909 goto out;
1910 }
1911
Elliott Hugheseda3a602017-05-19 18:53:02 -07001912 ret = fio_options_parse(td, opts, num_opts);
1913 if (!ret) {
1914 if (dump_cmdline)
1915 dump_opt_list(td);
1916
Jens Axboe46bcd492012-03-14 11:31:21 +01001917 ret = add_job(td, name, 0, 0, type);
Elliott Hugheseda3a602017-05-19 18:53:02 -07001918 } else {
Jens Axboeb1508cf2006-11-02 09:12:40 +01001919 log_err("fio: job %s dropped\n", name);
1920 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001921 }
Jens Axboe3b8b7132008-06-10 19:46:23 +02001922
1923 for (i = 0; i < num_opts; i++)
1924 free(opts[i]);
1925 num_opts = 0;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001926 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001927
Jens Axboecca73aa2007-04-04 11:09:19 +02001928 if (dump_cmdline)
1929 log_info("\n");
1930
Jens Axboe7e356b22011-10-14 10:55:16 +02001931 i = 0;
1932 while (i < nr_job_sections) {
1933 free(job_sections[i]);
1934 i++;
1935 }
1936
Jens Axboe25775942008-06-10 20:26:06 +02001937 free(opts);
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001938out:
1939 free(string);
1940 if (!nested)
1941 free(name);
Jens Axboe50d16972011-09-29 17:45:28 -06001942 if (!is_buf && f != stdin)
Aaron Carroll5a729cb2007-09-27 09:03:55 +02001943 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001944 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001945}
1946
Andrey Kuzmin3f28b452014-09-26 12:48:40 -06001947int parse_jobs_ini(char *file, int is_buf, int stonewall_flag, int type)
1948{
1949 return __parse_jobs_ini(NULL, file, is_buf, stonewall_flag, type,
1950 0, NULL, NULL, NULL, NULL);
1951}
1952
Jens Axboeebac4652005-12-08 15:25:21 +01001953static int fill_def_thread(void)
1954{
1955 memset(&def_thread, 0, sizeof(def_thread));
Elliott Hugheseda3a602017-05-19 18:53:02 -07001956 INIT_FLIST_HEAD(&def_thread.opt_list);
Jens Axboeebac4652005-12-08 15:25:21 +01001957
Jens Axboe375b2692007-05-22 09:13:02 +02001958 fio_getaffinity(getpid(), &def_thread.o.cpumask);
Dmitry Monakhov8b28bd42012-09-23 15:46:09 +04001959 def_thread.o.error_dump = 1;
Jens Axboe25bd16c2014-04-06 10:09:30 -06001960
Jens Axboeebac4652005-12-08 15:25:21 +01001961 /*
Jens Axboeee738492007-01-10 11:23:16 +01001962 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001963 */
Jens Axboe214e1ec2007-03-15 10:48:13 +01001964 fio_fill_default_options(&def_thread);
Jens Axboeebac4652005-12-08 15:25:21 +01001965 return 0;
1966}
1967
Elliott Hugheseda3a602017-05-19 18:53:02 -07001968static void show_debug_categories(void)
1969{
1970#ifdef FIO_INC_DEBUG
1971 struct debug_level *dl = &debug_levels[0];
1972 int curlen, first = 1;
1973
1974 curlen = 0;
1975 while (dl->name) {
1976 int has_next = (dl + 1)->name != NULL;
1977
1978 if (first || curlen + strlen(dl->name) >= 80) {
1979 if (!first) {
1980 printf("\n");
1981 curlen = 0;
1982 }
1983 curlen += printf("\t\t\t%s", dl->name);
1984 curlen += 3 * (8 - 1);
1985 if (has_next)
1986 curlen += printf(",");
1987 } else {
1988 curlen += printf("%s", dl->name);
1989 if (has_next)
1990 curlen += printf(",");
1991 }
1992 dl++;
1993 first = 0;
1994 }
1995 printf("\n");
1996#endif
1997}
1998
1999/*
2000 * Following options aren't printed by usage().
2001 * --append-terse - Equivalent to --output-format=terse, see f6a7df53.
2002 * --latency-log - Deprecated option.
2003 */
Jens Axboe45378b32007-12-19 13:54:38 +01002004static void usage(const char *name)
Jens Axboeb4692822006-10-27 13:43:22 +02002005{
Jens Axboe3d433822012-03-21 22:25:22 +01002006 printf("%s\n", fio_version_string);
Jens Axboe45378b32007-12-19 13:54:38 +01002007 printf("%s [options] [job options] <job file(s)>\n", name);
Elliott Hugheseda3a602017-05-19 18:53:02 -07002008 printf(" --debug=options\tEnable debug logging. May be one/more of:\n");
2009 show_debug_categories();
Jens Axboe111e0322013-03-07 11:31:20 +01002010 printf(" --parse-only\t\tParse options only, don't start any IO\n");
Jens Axboe83881282011-10-17 19:58:51 +02002011 printf(" --output\t\tWrite output to file\n");
Elliott Hugheseda3a602017-05-19 18:53:02 -07002012 printf(" --bandwidth-log\tGenerate aggregate bandwidth logs\n");
Jens Axboe83881282011-10-17 19:58:51 +02002013 printf(" --minimal\t\tMinimal (terse) output\n");
Elliott Hugheseda3a602017-05-19 18:53:02 -07002014 printf(" --output-format=type\tOutput format (terse,json,json+,normal)\n");
2015 printf(" --terse-version=type\tSet terse version output format"
2016 " (default 3, or 2 or 4)\n");
Jens Axboef3afa572012-09-17 13:34:16 +02002017 printf(" --version\t\tPrint version info and exit\n");
Jens Axboe83881282011-10-17 19:58:51 +02002018 printf(" --help\t\tPrint this page\n");
Jens Axboe23893642012-12-17 14:44:08 +01002019 printf(" --cpuclock-test\tPerform test/validation of CPU clock\n");
Elliott Hugheseda3a602017-05-19 18:53:02 -07002020 printf(" --crctest=type\tTest speed of checksum functions\n");
Jens Axboe83881282011-10-17 19:58:51 +02002021 printf(" --cmdhelp=cmd\t\tPrint command help, \"all\" for all of"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01002022 " them\n");
Steven Langde890a12011-11-09 14:03:34 +01002023 printf(" --enghelp=engine\tPrint ioengine help, or list"
2024 " available ioengines\n");
2025 printf(" --enghelp=engine,cmd\tPrint help for an ioengine"
2026 " cmd\n");
Jens Axboe83881282011-10-17 19:58:51 +02002027 printf(" --showcmd\t\tTurn a job file into command line options\n");
2028 printf(" --eta=when\t\tWhen ETA estimate should be printed\n");
2029 printf(" \t\tMay be \"always\", \"never\" or \"auto\"\n");
Jens Axboee382e662013-02-22 20:48:56 +01002030 printf(" --eta-newline=time\tForce a new line for every 'time'");
2031 printf(" period passed\n");
Jens Axboe06464902013-04-24 20:38:54 -06002032 printf(" --status-interval=t\tForce full status dump every");
2033 printf(" 't' period passed\n");
Jens Axboe83881282011-10-17 19:58:51 +02002034 printf(" --readonly\t\tTurn on safety read-only checks, preventing"
Jens Axboe5ec10ea2008-03-06 15:42:00 +01002035 " writes\n");
Elliott Hugheseda3a602017-05-19 18:53:02 -07002036 printf(" --section=name\tOnly run specified section in job file,"
2037 " multiple sections can be specified\n");
Jens Axboe83881282011-10-17 19:58:51 +02002038 printf(" --alloc-size=kb\tSet smalloc pool to this size in kb"
Elliott Hugheseda3a602017-05-19 18:53:02 -07002039 " (def 16384)\n");
Jens Axboe83881282011-10-17 19:58:51 +02002040 printf(" --warnings-fatal\tFio parser warnings are fatal\n");
2041 printf(" --max-jobs=nr\t\tMaximum number of threads/processes to support\n");
2042 printf(" --server=args\t\tStart a backend fio server\n");
2043 printf(" --daemonize=pidfile\tBackground fio server, write pid to file\n");
Elliott Hugheseda3a602017-05-19 18:53:02 -07002044 printf(" --client=hostname\tTalk to remote backend(s) fio server at hostname\n");
Jens Axboe35899182014-10-07 20:56:28 -06002045 printf(" --remote-config=file\tTell fio server to load this local job file\n");
Huadong Liuf2a2ce02013-01-30 13:22:24 +01002046 printf(" --idle-prof=option\tReport cpu idleness on a system or percpu basis\n"
2047 "\t\t\t(option=system,percpu) or run unit work\n"
2048 "\t\t\tcalibration only (option=calibrate)\n");
Jens Axboebac4af12014-07-03 13:42:28 -06002049#ifdef CONFIG_ZLIB
2050 printf(" --inflate-log=log\tInflate and output compressed log\n");
2051#endif
Jens Axboea4432072014-11-19 08:36:12 -07002052 printf(" --trigger-file=file\tExecute trigger cmd when file exists\n");
Jens Axboede54cfd2014-11-10 20:34:00 -07002053 printf(" --trigger-timeout=t\tExecute trigger af this time\n");
Jens Axboea4432072014-11-19 08:36:12 -07002054 printf(" --trigger=cmd\t\tSet this command as local trigger\n");
2055 printf(" --trigger-remote=cmd\tSet this command as remote trigger\n");
Elliott Hugheseda3a602017-05-19 18:53:02 -07002056 printf(" --aux-path=path\tUse this path for fio state generated files\n");
Jens Axboeaa58d252010-06-09 09:49:38 +02002057 printf("\nFio was written by Jens Axboe <jens.axboe@oracle.com>");
Jens Axboebfca2c72014-01-29 16:39:04 -07002058 printf("\n Jens Axboe <jaxboe@fusionio.com>");
2059 printf("\n Jens Axboe <axboe@fb.com>\n");
Jens Axboeb4692822006-10-27 13:43:22 +02002060}
2061
Jens Axboe79e48f72008-02-01 20:37:09 +01002062#ifdef FIO_INC_DEBUG
Jens Axboeee56ad52008-02-01 10:30:20 +01002063struct debug_level debug_levels[] = {
Jens Axboe0b8d11e2012-03-02 19:44:15 +01002064 { .name = "process",
2065 .help = "Process creation/exit logging",
2066 .shift = FD_PROCESS,
2067 },
2068 { .name = "file",
2069 .help = "File related action logging",
2070 .shift = FD_FILE,
2071 },
2072 { .name = "io",
2073 .help = "IO and IO engine action logging (offsets, queue, completions, etc)",
2074 .shift = FD_IO,
2075 },
2076 { .name = "mem",
2077 .help = "Memory allocation/freeing logging",
2078 .shift = FD_MEM,
2079 },
2080 { .name = "blktrace",
2081 .help = "blktrace action logging",
2082 .shift = FD_BLKTRACE,
2083 },
2084 { .name = "verify",
2085 .help = "IO verification action logging",
2086 .shift = FD_VERIFY,
2087 },
2088 { .name = "random",
2089 .help = "Random generation logging",
2090 .shift = FD_RANDOM,
2091 },
2092 { .name = "parse",
2093 .help = "Parser logging",
2094 .shift = FD_PARSE,
2095 },
2096 { .name = "diskutil",
2097 .help = "Disk utility logging actions",
2098 .shift = FD_DISKUTIL,
2099 },
2100 { .name = "job",
2101 .help = "Logging related to creating/destroying jobs",
2102 .shift = FD_JOB,
2103 },
2104 { .name = "mutex",
2105 .help = "Mutex logging",
2106 .shift = FD_MUTEX
2107 },
2108 { .name = "profile",
2109 .help = "Logging related to profiles",
2110 .shift = FD_PROFILE,
2111 },
2112 { .name = "time",
2113 .help = "Logging related to time keeping functions",
2114 .shift = FD_TIME,
2115 },
2116 { .name = "net",
2117 .help = "Network logging",
2118 .shift = FD_NET,
2119 },
Jens Axboe3e260a42013-12-09 12:38:53 -07002120 { .name = "rate",
2121 .help = "Rate logging",
2122 .shift = FD_RATE,
2123 },
Jens Axboe4f3fe6c2014-07-09 23:01:03 +02002124 { .name = "compress",
2125 .help = "Log compression logging",
2126 .shift = FD_COMPRESS,
2127 },
Elliott Hugheseda3a602017-05-19 18:53:02 -07002128 { .name = "steadystate",
2129 .help = "Steady state detection logging",
2130 .shift = FD_STEADYSTATE,
2131 },
2132 { .name = "helperthread",
2133 .help = "Helper thread logging",
2134 .shift = FD_HELPERTHREAD,
2135 },
Jens Axboe02444ad2008-10-07 11:33:00 +02002136 { .name = NULL, },
Jens Axboeee56ad52008-02-01 10:30:20 +01002137};
2138
Jens Axboec09823a2008-02-19 20:16:57 +01002139static int set_debug(const char *string)
Jens Axboeee56ad52008-02-01 10:30:20 +01002140{
2141 struct debug_level *dl;
2142 char *p = (char *) string;
2143 char *opt;
2144 int i;
2145
Elliott Hugheseda3a602017-05-19 18:53:02 -07002146 if (!string)
2147 return 0;
2148
Jens Axboeee56ad52008-02-01 10:30:20 +01002149 if (!strcmp(string, "?") || !strcmp(string, "help")) {
Jens Axboeee56ad52008-02-01 10:30:20 +01002150 log_info("fio: dumping debug options:");
2151 for (i = 0; debug_levels[i].name; i++) {
2152 dl = &debug_levels[i];
2153 log_info("%s,", dl->name);
2154 }
Jens Axboebd6f78b2008-02-01 20:27:52 +01002155 log_info("all\n");
Jens Axboec09823a2008-02-19 20:16:57 +01002156 return 1;
Jens Axboeee56ad52008-02-01 10:30:20 +01002157 }
2158
2159 while ((opt = strsep(&p, ",")) != NULL) {
2160 int found = 0;
2161
Jens Axboe5e1d3062008-05-23 11:55:53 +02002162 if (!strncmp(opt, "all", 3)) {
2163 log_info("fio: set all debug options\n");
2164 fio_debug = ~0UL;
2165 continue;
2166 }
2167
Jens Axboeee56ad52008-02-01 10:30:20 +01002168 for (i = 0; debug_levels[i].name; i++) {
2169 dl = &debug_levels[i];
Jens Axboe5e1d3062008-05-23 11:55:53 +02002170 found = !strncmp(opt, dl->name, strlen(dl->name));
2171 if (!found)
2172 continue;
2173
2174 if (dl->shift == FD_JOB) {
2175 opt = strchr(opt, ':');
2176 if (!opt) {
2177 log_err("fio: missing job number\n");
2178 break;
2179 }
2180 opt++;
2181 fio_debug_jobno = atoi(opt);
2182 log_info("fio: set debug jobno %d\n",
2183 fio_debug_jobno);
2184 } else {
Jens Axboeee56ad52008-02-01 10:30:20 +01002185 log_info("fio: set debug option %s\n", opt);
Jens Axboebd6f78b2008-02-01 20:27:52 +01002186 fio_debug |= (1UL << dl->shift);
Jens Axboeee56ad52008-02-01 10:30:20 +01002187 }
Jens Axboe5e1d3062008-05-23 11:55:53 +02002188 break;
Jens Axboeee56ad52008-02-01 10:30:20 +01002189 }
2190
2191 if (!found)
2192 log_err("fio: debug mask %s not found\n", opt);
2193 }
Jens Axboec09823a2008-02-19 20:16:57 +01002194 return 0;
Jens Axboeee56ad52008-02-01 10:30:20 +01002195}
Jens Axboe79e48f72008-02-01 20:37:09 +01002196#else
Jens Axboe69b98d42008-05-30 22:25:32 +02002197static int set_debug(const char *string)
Jens Axboe79e48f72008-02-01 20:37:09 +01002198{
2199 log_err("fio: debug tracing not included in build\n");
Jens Axboec09823a2008-02-19 20:16:57 +01002200 return 1;
Jens Axboe79e48f72008-02-01 20:37:09 +01002201}
2202#endif
Jens Axboeee56ad52008-02-01 10:30:20 +01002203
Jens Axboe4c6107f2011-01-18 05:22:22 -07002204static void fio_options_fill_optstring(void)
2205{
2206 char *ostr = cmd_optstr;
2207 int i, c;
2208
2209 c = i = 0;
2210 while (l_opts[i].name) {
2211 ostr[c++] = l_opts[i].val;
2212 if (l_opts[i].has_arg == required_argument)
2213 ostr[c++] = ':';
2214 else if (l_opts[i].has_arg == optional_argument) {
2215 ostr[c++] = ':';
2216 ostr[c++] = ':';
2217 }
2218 i++;
2219 }
2220 ostr[c] = '\0';
2221}
2222
Jens Axboec2c94582011-10-05 20:31:30 +02002223static int client_flag_set(char c)
2224{
2225 int i;
2226
2227 i = 0;
2228 while (l_opts[i].name) {
2229 int val = l_opts[i].val;
2230
2231 if (c == (val & 0xff))
2232 return (val & FIO_CLIENT_FLAG);
2233
2234 i++;
2235 }
2236
2237 return 0;
2238}
2239
Jens Axboe10aa1362014-04-01 21:10:36 -06002240static void parse_cmd_client(void *client, char *opt)
Jens Axboe7a4b8242011-10-05 17:35:15 +02002241{
Jens Axboefa2ea802011-10-08 21:07:29 +02002242 fio_client_add_cmd_option(client, opt);
Jens Axboe7a4b8242011-10-05 17:35:15 +02002243}
2244
Jens Axboe786715e2014-12-05 09:35:40 -07002245static void show_closest_option(const char *name)
2246{
2247 int best_option, best_distance;
2248 int i, distance;
2249
2250 while (*name == '-')
2251 name++;
2252
2253 best_option = -1;
2254 best_distance = INT_MAX;
2255 i = 0;
2256 while (l_opts[i].name) {
2257 distance = string_distance(name, l_opts[i].name);
2258 if (distance < best_distance) {
2259 best_distance = distance;
2260 best_option = i;
2261 }
2262 i++;
2263 }
2264
Elliott Hugheseda3a602017-05-19 18:53:02 -07002265 if (best_option != -1 && string_distance_ok(name, best_distance))
Jens Axboe786715e2014-12-05 09:35:40 -07002266 log_err("Did you mean %s?\n", l_opts[best_option].name);
2267}
2268
Elliott Hugheseda3a602017-05-19 18:53:02 -07002269static int parse_output_format(const char *optarg)
2270{
2271 char *p, *orig, *opt;
2272 int ret = 0;
2273
2274 p = orig = strdup(optarg);
2275
2276 output_format = 0;
2277
2278 while ((opt = strsep(&p, ",")) != NULL) {
2279 if (!strcmp(opt, "minimal") ||
2280 !strcmp(opt, "terse") ||
2281 !strcmp(opt, "csv"))
2282 output_format |= FIO_OUTPUT_TERSE;
2283 else if (!strcmp(opt, "json"))
2284 output_format |= FIO_OUTPUT_JSON;
2285 else if (!strcmp(opt, "json+"))
2286 output_format |= (FIO_OUTPUT_JSON | FIO_OUTPUT_JSON_PLUS);
2287 else if (!strcmp(opt, "normal"))
2288 output_format |= FIO_OUTPUT_NORMAL;
2289 else {
2290 log_err("fio: invalid output format %s\n", opt);
2291 ret = 1;
2292 break;
2293 }
2294 }
2295
2296 free(orig);
2297 return ret;
2298}
2299
Jens Axboe46bcd492012-03-14 11:31:21 +01002300int parse_cmd_line(int argc, char *argv[], int client_type)
Jens Axboe214e1ec2007-03-15 10:48:13 +01002301{
2302 struct thread_data *td = NULL;
Jens Axboec09823a2008-02-19 20:16:57 +01002303 int c, ini_idx = 0, lidx, ret = 0, do_exit = 0, exit_val = 0;
Jens Axboe4c6107f2011-01-18 05:22:22 -07002304 char *ostr = cmd_optstr;
Elliott Hugheseda3a602017-05-19 18:53:02 -07002305 char *pid_file = NULL;
Jens Axboe9bae26e2011-10-07 10:07:22 +02002306 void *cur_client = NULL;
Jens Axboe81179ee2011-10-04 12:42:06 +02002307 int backend = 0;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002308
Jens Axboec2c94582011-10-05 20:31:30 +02002309 /*
2310 * Reset optind handling, since we may call this multiple times
2311 * for the backend.
2312 */
2313 optind = 1;
Jens Axboe7a4b8242011-10-05 17:35:15 +02002314
Jens Axboec2c94582011-10-05 20:31:30 +02002315 while ((c = getopt_long_only(argc, argv, ostr, l_opts, &lidx)) != -1) {
2316 if ((c & FIO_CLIENT_FLAG) || client_flag_set(c)) {
Jens Axboefa2ea802011-10-08 21:07:29 +02002317 parse_cmd_client(cur_client, argv[optind - 1]);
Jens Axboe7a4b8242011-10-05 17:35:15 +02002318 c &= ~FIO_CLIENT_FLAG;
2319 }
2320
Jens Axboe214e1ec2007-03-15 10:48:13 +01002321 switch (c) {
Jens Axboe2b386d22008-03-26 10:32:57 +01002322 case 'a':
2323 smalloc_pool_size = atoi(optarg);
Elliott Hugheseda3a602017-05-19 18:53:02 -07002324 smalloc_pool_size <<= 10;
2325 sinit();
Jens Axboe214e1ec2007-03-15 10:48:13 +01002326 break;
2327 case 'l':
Jens Axboe987c4f42014-07-01 16:29:12 -06002328 log_err("fio: --latency-log is deprecated. Use per-job latency log options.\n");
2329 do_exit++;
2330 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002331 break;
Jens Axboe3d73e5a2010-03-25 10:38:07 +01002332 case 'b':
Jens Axboe214e1ec2007-03-15 10:48:13 +01002333 write_bw_log = 1;
2334 break;
Elliott Hugheseda3a602017-05-19 18:53:02 -07002335 case 'o': {
2336 FILE *tmp;
2337
Rik Faith52482242014-05-29 14:13:39 -06002338 if (f_out && f_out != stdout)
Jens Axboe5e1d8742014-04-11 11:39:46 -06002339 fclose(f_out);
2340
Elliott Hugheseda3a602017-05-19 18:53:02 -07002341 tmp = fopen(optarg, "w+");
2342 if (!tmp) {
2343 log_err("fio: output file open error: %s\n", strerror(errno));
Stefan Hajnoczid10983d2014-02-04 14:27:11 +01002344 exit_val = 1;
2345 do_exit++;
2346 break;
2347 }
Elliott Hugheseda3a602017-05-19 18:53:02 -07002348 f_err = f_out = tmp;
2349 break;
2350 }
2351 case 'm':
2352 output_format = FIO_OUTPUT_TERSE;
2353 break;
2354 case 'F':
2355 if (parse_output_format(optarg)) {
2356 log_err("fio: failed parsing output-format\n");
2357 exit_val = 1;
2358 do_exit++;
2359 break;
2360 }
Jens Axboe214e1ec2007-03-15 10:48:13 +01002361 break;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +01002362 case 'f':
Elliott Hugheseda3a602017-05-19 18:53:02 -07002363 output_format |= FIO_OUTPUT_TERSE;
Christian Ehrhardt2b8c71b2014-02-20 14:20:04 +01002364 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002365 case 'h':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002366 did_arg = 1;
Jens Axboe7874f8b2011-10-06 09:18:20 +02002367 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02002368 usage(argv[0]);
Jens Axboe7874f8b2011-10-06 09:18:20 +02002369 do_exit++;
2370 }
Jens Axboec2c94582011-10-05 20:31:30 +02002371 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002372 case 'c':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002373 did_arg = 1;
Jens Axboe7874f8b2011-10-06 09:18:20 +02002374 if (!cur_client) {
Jens Axboec2c94582011-10-05 20:31:30 +02002375 fio_show_option_help(optarg);
Jens Axboe7874f8b2011-10-06 09:18:20 +02002376 do_exit++;
2377 }
Jens Axboec2c94582011-10-05 20:31:30 +02002378 break;
Steven Langde890a12011-11-09 14:03:34 +01002379 case 'i':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002380 did_arg = 1;
Steven Langde890a12011-11-09 14:03:34 +01002381 if (!cur_client) {
2382 fio_show_ioengine_help(optarg);
2383 do_exit++;
2384 }
2385 break;
Jens Axboecca73aa2007-04-04 11:09:19 +02002386 case 's':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002387 did_arg = 1;
Jens Axboecca73aa2007-04-04 11:09:19 +02002388 dump_cmdline = 1;
2389 break;
Jens Axboe724e4432007-09-11 20:02:05 +02002390 case 'r':
2391 read_only = 1;
2392 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002393 case 'v':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002394 did_arg = 1;
Jens Axboe7874f8b2011-10-06 09:18:20 +02002395 if (!cur_client) {
Jens Axboe3d433822012-03-21 22:25:22 +01002396 log_info("%s\n", fio_version_string);
Jens Axboe7874f8b2011-10-06 09:18:20 +02002397 do_exit++;
2398 }
Jens Axboec2c94582011-10-05 20:31:30 +02002399 break;
Jens Axboef57a9c52011-09-09 21:01:37 +02002400 case 'V':
2401 terse_version = atoi(optarg);
Jens Axboe09786f52012-10-04 17:06:49 +02002402 if (!(terse_version == 2 || terse_version == 3 ||
2403 terse_version == 4)) {
Jens Axboef57a9c52011-09-09 21:01:37 +02002404 log_err("fio: bad terse version format\n");
2405 exit_val = 1;
2406 do_exit++;
2407 }
2408 break;
Aaron Carrolle592a062007-09-14 09:49:41 +02002409 case 'e':
2410 if (!strcmp("always", optarg))
2411 eta_print = FIO_ETA_ALWAYS;
2412 else if (!strcmp("never", optarg))
2413 eta_print = FIO_ETA_NEVER;
2414 break;
Jens Axboee382e662013-02-22 20:48:56 +01002415 case 'E': {
2416 long long t = 0;
2417
Stephen M. Cameron79dc9142014-11-10 20:31:26 -07002418 if (check_str_time(optarg, &t, 1)) {
Jens Axboee382e662013-02-22 20:48:56 +01002419 log_err("fio: failed parsing eta time %s\n", optarg);
2420 exit_val = 1;
2421 do_exit++;
2422 }
Stephen M. Cameron79dc9142014-11-10 20:31:26 -07002423 eta_new_line = t / 1000;
Jens Axboee382e662013-02-22 20:48:56 +01002424 break;
2425 }
Jens Axboeee56ad52008-02-01 10:30:20 +01002426 case 'd':
Jens Axboec09823a2008-02-19 20:16:57 +01002427 if (set_debug(optarg))
2428 do_exit++;
Jens Axboeee56ad52008-02-01 10:30:20 +01002429 break;
Jens Axboe111e0322013-03-07 11:31:20 +01002430 case 'P':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002431 did_arg = 1;
Jens Axboe111e0322013-03-07 11:31:20 +01002432 parse_only = 1;
2433 break;
Jens Axboead0a2732011-03-11 10:16:17 +01002434 case 'x': {
2435 size_t new_size;
2436
Jens Axboe01f06b62008-02-18 20:53:47 +01002437 if (!strcmp(optarg, "global")) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01002438 log_err("fio: can't use global as only "
2439 "section\n");
Jens Axboec09823a2008-02-19 20:16:57 +01002440 do_exit++;
2441 exit_val = 1;
Jens Axboe01f06b62008-02-18 20:53:47 +01002442 break;
2443 }
Jens Axboead0a2732011-03-11 10:16:17 +01002444 new_size = (nr_job_sections + 1) * sizeof(char *);
2445 job_sections = realloc(job_sections, new_size);
2446 job_sections[nr_job_sections] = strdup(optarg);
2447 nr_job_sections++;
Jens Axboe01f06b62008-02-18 20:53:47 +01002448 break;
Jens Axboead0a2732011-03-11 10:16:17 +01002449 }
Jens Axboebac4af12014-07-03 13:42:28 -06002450#ifdef CONFIG_ZLIB
2451 case 'X':
2452 exit_val = iolog_file_inflate(optarg);
2453 did_arg++;
2454 do_exit++;
2455 break;
2456#endif
Jens Axboe9ac8a792009-11-13 21:23:07 +01002457 case 'p':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002458 did_arg = 1;
Jens Axboe4af7c002014-04-11 11:38:38 -06002459 if (exec_profile)
2460 free(exec_profile);
Jens Axboe07b32322010-03-05 09:48:44 +01002461 exec_profile = strdup(optarg);
Jens Axboe9ac8a792009-11-13 21:23:07 +01002462 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002463 case FIO_GETOPT_JOB: {
Jens Axboe5ec10ea2008-03-06 15:42:00 +01002464 const char *opt = l_opts[lidx].name;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002465 char *val = optarg;
2466
2467 if (!strncmp(opt, "name", 4) && td) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002468 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe66251552011-05-12 10:14:57 +02002469 if (ret)
Jens Axboe4a4ac4e2014-04-14 08:17:30 -06002470 goto out_free;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002471 td = NULL;
Jens Axboe7d8ea972014-04-14 13:37:46 -06002472 did_arg = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002473 }
2474 if (!td) {
Jens Axboe01f06b62008-02-18 20:53:47 +01002475 int is_section = !strncmp(opt, "name", 4);
Jens Axboe3106f222007-04-19 09:53:28 +02002476 int global = 0;
2477
Jens Axboe01f06b62008-02-18 20:53:47 +01002478 if (!is_section || !strncmp(val, "global", 6))
Jens Axboe3106f222007-04-19 09:53:28 +02002479 global = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002480
Jens Axboe01f06b62008-02-18 20:53:47 +01002481 if (is_section && skip_this_section(val))
2482 continue;
2483
Elliott Hugheseda3a602017-05-19 18:53:02 -07002484 td = get_new_job(global, &def_thread, true, NULL);
Jens Axboe1cefc792014-06-09 13:54:22 -06002485 if (!td || ioengine_load(td)) {
2486 if (td) {
2487 put_job(td);
2488 td = NULL;
2489 }
2490 do_exit++;
Elliott Hugheseda3a602017-05-19 18:53:02 -07002491 exit_val = 1;
Jens Axboe1cefc792014-06-09 13:54:22 -06002492 break;
2493 }
Steven Langde890a12011-11-09 14:03:34 +01002494 fio_options_set_ioengine_opts(l_opts, td);
Jens Axboe214e1ec2007-03-15 10:48:13 +01002495 }
2496
Jens Axboe9d918182013-05-23 20:00:35 +02002497 if ((!val || !strlen(val)) &&
2498 l_opts[lidx].has_arg == required_argument) {
2499 log_err("fio: option %s requires an argument\n", opt);
2500 ret = 1;
2501 } else
2502 ret = fio_cmd_option_parse(td, opt, val);
2503
Jens Axboebfb3ea22013-05-23 19:54:43 +02002504 if (ret) {
2505 if (td) {
2506 put_job(td);
2507 td = NULL;
2508 }
Jens Axboea88c8c12013-04-24 22:25:22 -06002509 do_exit++;
Elliott Hugheseda3a602017-05-19 18:53:02 -07002510 exit_val = 1;
Jens Axboebfb3ea22013-05-23 19:54:43 +02002511 }
Steven Langde890a12011-11-09 14:03:34 +01002512
2513 if (!ret && !strcmp(opt, "ioengine")) {
2514 free_ioengine(td);
Jens Axboe1cefc792014-06-09 13:54:22 -06002515 if (ioengine_load(td)) {
Jens Axboe9c574af2014-07-03 15:33:24 -06002516 put_job(td);
2517 td = NULL;
Jens Axboe1cefc792014-06-09 13:54:22 -06002518 do_exit++;
Elliott Hugheseda3a602017-05-19 18:53:02 -07002519 exit_val = 1;
Jens Axboe1cefc792014-06-09 13:54:22 -06002520 break;
2521 }
Steven Langde890a12011-11-09 14:03:34 +01002522 fio_options_set_ioengine_opts(l_opts, td);
2523 }
2524 break;
2525 }
2526 case FIO_GETOPT_IOENGINE: {
2527 const char *opt = l_opts[lidx].name;
2528 char *val = optarg;
Jens Axboeb1ed74e2014-04-14 12:07:36 -06002529
2530 if (!td)
2531 break;
2532
Steven Langde890a12011-11-09 14:03:34 +01002533 ret = fio_cmd_ioengine_option_parse(td, opt, val);
Jens Axboe214e1ec2007-03-15 10:48:13 +01002534 break;
2535 }
Jens Axboea9523c62011-01-17 16:49:54 -07002536 case 'w':
2537 warnings_fatal = 1;
2538 break;
Jens Axboefca70352011-07-06 20:12:54 +02002539 case 'j':
2540 max_jobs = atoi(optarg);
2541 if (!max_jobs || max_jobs > REAL_MAX_JOBS) {
2542 log_err("fio: invalid max jobs: %d\n", max_jobs);
2543 do_exit++;
2544 exit_val = 1;
2545 }
2546 break;
Jens Axboe50d16972011-09-29 17:45:28 -06002547 case 'S':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002548 did_arg = 1;
Jens Axboeef7035a2014-06-18 15:30:09 -07002549#ifndef CONFIG_NO_SHM
Jens Axboea37f69b2011-10-01 12:24:21 -06002550 if (nr_clients) {
Jens Axboe132159a2011-09-30 15:01:32 -06002551 log_err("fio: can't be both client and server\n");
2552 do_exit++;
2553 exit_val = 1;
2554 break;
2555 }
Jens Axboe87aa8f12011-10-06 21:24:13 +02002556 if (optarg)
Jens Axboebebe6392011-10-07 10:00:51 +02002557 fio_server_set_arg(optarg);
Jens Axboe50d16972011-09-29 17:45:28 -06002558 is_backend = 1;
Jens Axboe81179ee2011-10-04 12:42:06 +02002559 backend = 1;
Jens Axboeef7035a2014-06-18 15:30:09 -07002560#else
2561 log_err("fio: client/server requires SHM support\n");
2562 do_exit++;
2563 exit_val = 1;
2564#endif
Jens Axboe50d16972011-09-29 17:45:28 -06002565 break;
Jens Axboee46d8092011-10-03 09:11:02 +02002566 case 'D':
Jens Axboe6cfe9a82014-04-14 13:29:52 -06002567 if (pid_file)
2568 free(pid_file);
Jens Axboe402668f2011-10-10 15:28:58 +02002569 pid_file = strdup(optarg);
Jens Axboee46d8092011-10-03 09:11:02 +02002570 break;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01002571 case 'I':
2572 if ((ret = fio_idle_prof_parse_opt(optarg))) {
2573 /* exit on error and calibration only */
Jens Axboe7d8ea972014-04-14 13:37:46 -06002574 did_arg = 1;
Huadong Liuf2a2ce02013-01-30 13:22:24 +01002575 do_exit++;
Jens Axboe7d8ea972014-04-14 13:37:46 -06002576 if (ret == -1)
Huadong Liuf2a2ce02013-01-30 13:22:24 +01002577 exit_val = 1;
2578 }
2579 break;
Jens Axboe132159a2011-09-30 15:01:32 -06002580 case 'C':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002581 did_arg = 1;
Jens Axboe132159a2011-09-30 15:01:32 -06002582 if (is_backend) {
2583 log_err("fio: can't be both client and server\n");
2584 do_exit++;
2585 exit_val = 1;
2586 break;
2587 }
Elliott Hugheseda3a602017-05-19 18:53:02 -07002588 /* if --client parameter contains a pathname */
2589 if (0 == access(optarg, R_OK)) {
2590 /* file contains a list of host addrs or names */
2591 char hostaddr[PATH_MAX] = {0};
2592 char formatstr[8];
2593 FILE * hostf = fopen(optarg, "r");
2594 if (!hostf) {
2595 log_err("fio: could not open client list file %s for read\n", optarg);
2596 do_exit++;
2597 exit_val = 1;
2598 break;
2599 }
2600 sprintf(formatstr, "%%%ds", PATH_MAX - 1);
2601 /*
2602 * read at most PATH_MAX-1 chars from each
2603 * record in this file
2604 */
2605 while (fscanf(hostf, formatstr, hostaddr) == 1) {
2606 /* expect EVERY host in file to be valid */
2607 if (fio_client_add(&fio_client_ops, hostaddr, &cur_client)) {
2608 log_err("fio: failed adding client %s from file %s\n", hostaddr, optarg);
2609 do_exit++;
2610 exit_val = 1;
2611 break;
2612 }
2613 }
2614 fclose(hostf);
2615 break; /* no possibility of job file for "this client only" */
2616 }
Jens Axboea5276612012-03-04 15:15:08 +01002617 if (fio_client_add(&fio_client_ops, optarg, &cur_client)) {
Jens Axboebebe6392011-10-07 10:00:51 +02002618 log_err("fio: failed adding client %s\n", optarg);
2619 do_exit++;
2620 exit_val = 1;
2621 break;
2622 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02002623 /*
2624 * If the next argument exists and isn't an option,
2625 * assume it's a job file for this client only.
2626 */
2627 while (optind < argc) {
2628 if (!strncmp(argv[optind], "--", 2) ||
2629 !strncmp(argv[optind], "-", 1))
2630 break;
2631
Elliott Hugheseda3a602017-05-19 18:53:02 -07002632 if (fio_client_add_ini_file(cur_client, argv[optind], false))
Jens Axboe35899182014-10-07 20:56:28 -06002633 break;
Jens Axboe14ea90e2012-08-26 17:33:34 +02002634 optind++;
2635 }
Jens Axboe132159a2011-09-30 15:01:32 -06002636 break;
Jens Axboe35899182014-10-07 20:56:28 -06002637 case 'R':
2638 did_arg = 1;
Elliott Hugheseda3a602017-05-19 18:53:02 -07002639 if (fio_client_add_ini_file(cur_client, optarg, true)) {
Jens Axboe35899182014-10-07 20:56:28 -06002640 do_exit++;
2641 exit_val = 1;
2642 }
2643 break;
Jens Axboe7d11f872012-12-17 12:03:29 +01002644 case 'T':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002645 did_arg = 1;
Jens Axboe7d11f872012-12-17 12:03:29 +01002646 do_exit++;
Jens Axboe10564132014-12-23 10:41:34 -07002647 exit_val = fio_monotonic_clocktest(1);
Jens Axboe7d11f872012-12-17 12:03:29 +01002648 break;
Jens Axboefec0f212014-02-07 14:39:33 -07002649 case 'G':
Jens Axboe7d8ea972014-04-14 13:37:46 -06002650 did_arg = 1;
Jens Axboefec0f212014-02-07 14:39:33 -07002651 do_exit++;
2652 exit_val = fio_crctest(optarg);
2653 break;
Jens Axboe06464902013-04-24 20:38:54 -06002654 case 'L': {
2655 long long val;
2656
Stephen M. Cameron79dc9142014-11-10 20:31:26 -07002657 if (check_str_time(optarg, &val, 1)) {
Jens Axboe06464902013-04-24 20:38:54 -06002658 log_err("fio: failed parsing time %s\n", optarg);
2659 do_exit++;
2660 exit_val = 1;
2661 break;
2662 }
Stephen M. Cameron79dc9142014-11-10 20:31:26 -07002663 status_interval = val / 1000;
Jens Axboe06464902013-04-24 20:38:54 -06002664 break;
2665 }
Jens Axboea4432072014-11-19 08:36:12 -07002666 case 'W':
Jens Axboe12585bc2015-01-05 08:46:35 -07002667 if (trigger_file)
2668 free(trigger_file);
Jens Axboea4432072014-11-19 08:36:12 -07002669 trigger_file = strdup(optarg);
Jens Axboede54cfd2014-11-10 20:34:00 -07002670 break;
Jens Axboea4432072014-11-19 08:36:12 -07002671 case 'H':
Jens Axboe12585bc2015-01-05 08:46:35 -07002672 if (trigger_cmd)
2673 free(trigger_cmd);
Jens Axboea4432072014-11-19 08:36:12 -07002674 trigger_cmd = strdup(optarg);
2675 break;
2676 case 'J':
Jens Axboe12585bc2015-01-05 08:46:35 -07002677 if (trigger_remote_cmd)
2678 free(trigger_remote_cmd);
Jens Axboea4432072014-11-19 08:36:12 -07002679 trigger_remote_cmd = strdup(optarg);
2680 break;
Elliott Hugheseda3a602017-05-19 18:53:02 -07002681 case 'K':
2682 if (aux_path)
2683 free(aux_path);
2684 aux_path = strdup(optarg);
2685 break;
Jens Axboede54cfd2014-11-10 20:34:00 -07002686 case 'B':
2687 if (check_str_time(optarg, &trigger_timeout, 1)) {
2688 log_err("fio: failed parsing time %s\n", optarg);
2689 do_exit++;
2690 exit_val = 1;
2691 }
2692 trigger_timeout /= 1000000;
2693 break;
Jens Axboe798827c2013-01-23 18:11:48 -07002694 case '?':
2695 log_err("%s: unrecognized option '%s'\n", argv[0],
2696 argv[optind - 1]);
Jens Axboe786715e2014-12-05 09:35:40 -07002697 show_closest_option(argv[optind - 1]);
Jens Axboe214e1ec2007-03-15 10:48:13 +01002698 default:
Jens Axboec09823a2008-02-19 20:16:57 +01002699 do_exit++;
2700 exit_val = 1;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002701 break;
2702 }
Jens Axboec7d5c942011-10-03 11:48:17 +02002703 if (do_exit)
2704 break;
Jens Axboe214e1ec2007-03-15 10:48:13 +01002705 }
2706
Jens Axboe0b14f0a2013-05-23 21:27:54 +02002707 if (do_exit && !(is_backend || nr_clients))
2708 exit(exit_val);
Jens Axboe536582b2008-02-18 20:26:32 +01002709
Jens Axboefb296042014-04-02 08:28:49 -06002710 if (nr_clients && fio_clients_connect())
2711 exit(1);
Jens Axboe132159a2011-09-30 15:01:32 -06002712
Jens Axboe81179ee2011-10-04 12:42:06 +02002713 if (is_backend && backend)
Jens Axboe402668f2011-10-10 15:28:58 +02002714 return fio_start_server(pid_file);
Jens Axboeb8ba87a2014-04-11 11:37:34 -06002715 else if (pid_file)
2716 free(pid_file);
Jens Axboe50d16972011-09-29 17:45:28 -06002717
Jens Axboe214e1ec2007-03-15 10:48:13 +01002718 if (td) {
Jens Axboe7d8ea972014-04-14 13:37:46 -06002719 if (!ret) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002720 ret = add_job(td, td->o.name ?: "fio", 0, 0, client_type);
Jens Axboe7d8ea972014-04-14 13:37:46 -06002721 if (ret)
Elliott Hugheseda3a602017-05-19 18:53:02 -07002722 exit(1);
Jens Axboe7d8ea972014-04-14 13:37:46 -06002723 }
Jens Axboe214e1ec2007-03-15 10:48:13 +01002724 }
2725
Jens Axboe7874f8b2011-10-06 09:18:20 +02002726 while (!ret && optind < argc) {
Jens Axboe214e1ec2007-03-15 10:48:13 +01002727 ini_idx++;
2728 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
2729 ini_file[ini_idx - 1] = strdup(argv[optind]);
2730 optind++;
2731 }
2732
Jens Axboe4a4ac4e2014-04-14 08:17:30 -06002733out_free:
Jens Axboe214e1ec2007-03-15 10:48:13 +01002734 return ini_idx;
2735}
2736
Jens Axboe0420ba62012-02-29 11:16:52 +01002737int fio_init_options(void)
Jens Axboeebac4652005-12-08 15:25:21 +01002738{
Jens Axboeb4692822006-10-27 13:43:22 +02002739 f_out = stdout;
2740 f_err = stderr;
2741
Jens Axboe4c6107f2011-01-18 05:22:22 -07002742 fio_options_fill_optstring();
Jens Axboe5ec10ea2008-03-06 15:42:00 +01002743 fio_options_dup_and_init(l_opts);
Jens Axboeb4692822006-10-27 13:43:22 +02002744
Jens Axboe9d9eb2e2011-10-03 12:01:42 +02002745 atexit(free_shm);
2746
Jens Axboeebac4652005-12-08 15:25:21 +01002747 if (fill_def_thread())
2748 return 1;
2749
Jens Axboe0420ba62012-02-29 11:16:52 +01002750 return 0;
2751}
2752
Jens Axboe51167792012-03-08 21:34:18 +01002753extern int fio_check_options(struct thread_options *);
2754
Jens Axboe0420ba62012-02-29 11:16:52 +01002755int parse_options(int argc, char *argv[])
2756{
Jens Axboe46bcd492012-03-14 11:31:21 +01002757 const int type = FIO_CLIENT_TYPE_CLI;
Jens Axboe0420ba62012-02-29 11:16:52 +01002758 int job_files, i;
2759
2760 if (fio_init_options())
2761 return 1;
Jens Axboe51167792012-03-08 21:34:18 +01002762 if (fio_test_cconv(&def_thread.o))
2763 log_err("fio: failed internal cconv test\n");
Jens Axboe0420ba62012-02-29 11:16:52 +01002764
Jens Axboe46bcd492012-03-14 11:31:21 +01002765 job_files = parse_cmd_line(argc, argv, type);
Jens Axboeebac4652005-12-08 15:25:21 +01002766
Jens Axboecdf54d82011-10-04 08:31:40 +02002767 if (job_files > 0) {
2768 for (i = 0; i < job_files; i++) {
Jens Axboebc4f5ef2014-04-06 10:10:32 -06002769 if (i && fill_def_thread())
Jens Axboe132159a2011-09-30 15:01:32 -06002770 return 1;
Jens Axboecdf54d82011-10-04 08:31:40 +02002771 if (nr_clients) {
2772 if (fio_clients_send_ini(ini_file[i]))
2773 return 1;
2774 free(ini_file[i]);
2775 } else if (!is_backend) {
Jens Axboe46bcd492012-03-14 11:31:21 +01002776 if (parse_jobs_ini(ini_file[i], 0, i, type))
Jens Axboecdf54d82011-10-04 08:31:40 +02002777 return 1;
2778 free(ini_file[i]);
2779 }
Jens Axboe132159a2011-09-30 15:01:32 -06002780 }
Jens Axboe14ea90e2012-08-26 17:33:34 +02002781 } else if (nr_clients) {
2782 if (fill_def_thread())
2783 return 1;
2784 if (fio_clients_send_ini(NULL))
2785 return 1;
Jens Axboe972cfd22006-06-09 11:08:56 +02002786 }
Jens Axboeebac4652005-12-08 15:25:21 +01002787
Jens Axboe88c6ed82006-06-09 11:28:10 +02002788 free(ini_file);
Jens Axboe7e356b22011-10-14 10:55:16 +02002789 fio_options_free(&def_thread);
Christian Ehrhardtbcbfeef2014-02-20 09:13:06 -08002790 filesetup_mem_free();
Jens Axboeb4692822006-10-27 13:43:22 +02002791
2792 if (!thread_number) {
Jens Axboef9633d72013-09-06 09:59:56 -06002793 if (parse_dryrun())
Jens Axboecca73aa2007-04-04 11:09:19 +02002794 return 0;
Jens Axboe07b32322010-03-05 09:48:44 +01002795 if (exec_profile)
2796 return 0;
Jens Axboea37f69b2011-10-01 12:24:21 -06002797 if (is_backend || nr_clients)
Jens Axboe5c341e92011-09-29 18:00:35 -06002798 return 0;
Jens Axboe085399d2011-10-06 09:01:22 +02002799 if (did_arg)
2800 return 0;
Jens Axboecca73aa2007-04-04 11:09:19 +02002801
Elliott Hugheseda3a602017-05-19 18:53:02 -07002802 log_err("No job(s) defined\n\n");
Jens Axboe085399d2011-10-06 09:01:22 +02002803
2804 if (!did_arg) {
2805 usage(argv[0]);
2806 return 1;
2807 }
2808
2809 return 0;
Jens Axboeb4692822006-10-27 13:43:22 +02002810 }
2811
Elliott Hugheseda3a602017-05-19 18:53:02 -07002812 if (output_format & FIO_OUTPUT_NORMAL)
Jens Axboe3d433822012-03-21 22:25:22 +01002813 log_info("%s\n", fio_version_string);
Jens Axboef6dea4d2011-10-13 13:37:07 +02002814
Jens Axboeebac4652005-12-08 15:25:21 +01002815 return 0;
2816}
Jens Axboe588b7f02012-03-19 20:37:41 +01002817
2818void options_default_fill(struct thread_options *o)
2819{
2820 memcpy(o, &def_thread.o, sizeof(*o));
2821}
Elliott Hugheseda3a602017-05-19 18:53:02 -07002822
2823struct thread_data *get_global_options(void)
2824{
2825 return &def_thread;
2826}