blob: 01e615f92e90e1f37864e591279f037c8ddba55c [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>
Jens Axboeb4692822006-10-27 13:43:22 +020011#include <getopt.h>
12#include <assert.h>
Jens Axboeebac4652005-12-08 15:25:21 +010013#include <sys/ipc.h>
14#include <sys/shm.h>
15#include <sys/types.h>
16#include <sys/stat.h>
17
18#include "fio.h"
Jens Axboecb2c86f2006-10-26 13:48:34 +020019#include "parse.h"
Jens Axboeebac4652005-12-08 15:25:21 +010020
Jens Axboeee738492007-01-10 11:23:16 +010021#define FIO_RANDSEED (0xb1899bedUL)
Jens Axboeebac4652005-12-08 15:25:21 +010022
Jens Axboee1f36502006-10-27 10:54:08 +020023#define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var)
24
Jens Axboeb4692822006-10-27 13:43:22 +020025static int str_rw_cb(void *, const char *);
26static int str_ioengine_cb(void *, const char *);
27static int str_mem_cb(void *, const char *);
28static int str_verify_cb(void *, const char *);
Jens Axboee1f36502006-10-27 10:54:08 +020029static int str_lockmem_cb(void *, unsigned long *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010030#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +020031static int str_prio_cb(void *, unsigned int *);
32static int str_prioclass_cb(void *, unsigned int *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010033#endif
Jens Axboee1f36502006-10-27 10:54:08 +020034static int str_exitall_cb(void);
35static int str_cpumask_cb(void *, unsigned int *);
Jens Axboe0aabe162007-02-23 08:45:55 +010036static int str_file_service_cb(void *, const char *);
Jens Axboee1f36502006-10-27 10:54:08 +020037
Jens Axboeee738492007-01-10 11:23:16 +010038#define __stringify_1(x) #x
39#define __stringify(x) __stringify_1(x)
40
Jens Axboee1f36502006-10-27 10:54:08 +020041/*
42 * Map of job/command line options
43 */
44static struct fio_option options[] = {
45 {
Jens Axboe61697c32007-02-05 15:04:46 +010046 .name = "description",
47 .type = FIO_OPT_STR_STORE,
48 .off1 = td_var_offset(description),
49 .help = "Text job description",
50 },
51 {
Jens Axboee1f36502006-10-27 10:54:08 +020052 .name = "name",
53 .type = FIO_OPT_STR_STORE,
54 .off1 = td_var_offset(name),
Jens Axboefd28ca42007-01-09 21:20:13 +010055 .help = "Name of this job",
Jens Axboee1f36502006-10-27 10:54:08 +020056 },
57 {
58 .name = "directory",
59 .type = FIO_OPT_STR_STORE,
60 .off1 = td_var_offset(directory),
Jens Axboefd28ca42007-01-09 21:20:13 +010061 .help = "Directory to store files in",
Jens Axboee1f36502006-10-27 10:54:08 +020062 },
63 {
64 .name = "filename",
65 .type = FIO_OPT_STR_STORE,
66 .off1 = td_var_offset(filename),
Jens Axboefd28ca42007-01-09 21:20:13 +010067 .help = "Force the use of a specific file",
Jens Axboee1f36502006-10-27 10:54:08 +020068 },
69 {
70 .name = "rw",
71 .type = FIO_OPT_STR,
72 .cb = str_rw_cb,
Jens Axboe15f79182007-01-10 12:26:18 +010073 .help = "IO direction",
Jens Axboeee738492007-01-10 11:23:16 +010074 .def = "read",
Jens Axboe15f79182007-01-10 12:26:18 +010075 .posval = { "read", "write", "randwrite", "randread", "rw",
76 "randrw", },
Jens Axboee1f36502006-10-27 10:54:08 +020077 },
78 {
79 .name = "ioengine",
80 .type = FIO_OPT_STR,
81 .cb = str_ioengine_cb,
Jens Axboe15f79182007-01-10 12:26:18 +010082 .help = "IO engine to use",
Jens Axboeee738492007-01-10 11:23:16 +010083 .def = "sync",
Jens Axboe15f79182007-01-10 12:26:18 +010084 .posval = { "sync", "libaio", "posixaio", "mmap", "splice",
Jens Axboea4f4fdd2007-02-14 01:16:39 +010085 "sg", "null", "net", "syslet-rw" },
Jens Axboee1f36502006-10-27 10:54:08 +020086 },
87 {
Jens Axboe03b74b32007-01-11 11:04:31 +010088 .name = "iodepth",
89 .type = FIO_OPT_INT,
90 .off1 = td_var_offset(iodepth),
91 .help = "Amount of IO buffers to keep in flight",
92 .def = "1",
93 },
94 {
Jens Axboee916b392007-02-20 14:37:26 +010095 .name = "iodepth_low",
96 .type = FIO_OPT_INT,
97 .off1 = td_var_offset(iodepth_low),
98 .help = "Low water mark for queuing depth",
99 },
100 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100101 .name = "size",
102 .type = FIO_OPT_STR_VAL,
103 .off1 = td_var_offset(total_file_size),
104 .help = "Size of device or file",
105 },
106 {
107 .name = "bs",
108 .type = FIO_OPT_STR_VAL_INT,
109 .off1 = td_var_offset(bs[DDIR_READ]),
110 .off2 = td_var_offset(bs[DDIR_WRITE]),
111 .help = "Block size unit",
112 .def = "4k",
113 },
114 {
115 .name = "bsrange",
116 .type = FIO_OPT_RANGE,
117 .off1 = td_var_offset(min_bs[DDIR_READ]),
118 .off2 = td_var_offset(max_bs[DDIR_READ]),
119 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
120 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
121 .help = "Set block size range (in more detail than bs)",
122 },
123 {
124 .name = "bs_unaligned",
125 .type = FIO_OPT_STR_SET,
126 .off1 = td_var_offset(bs_unaligned),
127 .help = "Don't sector align IO buffer sizes",
128 },
129 {
130 .name = "offset",
131 .type = FIO_OPT_STR_VAL,
132 .off1 = td_var_offset(start_offset),
133 .help = "Start IO from this offset",
134 .def = "0",
135 },
136 {
137 .name = "randrepeat",
138 .type = FIO_OPT_BOOL,
139 .off1 = td_var_offset(rand_repeatable),
140 .help = "Use repeatable random IO pattern",
141 .def = "1",
142 },
143 {
144 .name = "norandommap",
145 .type = FIO_OPT_STR_SET,
146 .off1 = td_var_offset(norandommap),
147 .help = "Accept potential duplicate random blocks",
148 },
149 {
150 .name = "nrfiles",
151 .type = FIO_OPT_INT,
152 .off1 = td_var_offset(nr_files),
153 .help = "Split job workload between this number of files",
154 .def = "1",
155 },
156 {
Jens Axboe0aabe162007-02-23 08:45:55 +0100157 .name = "file_service_type",
158 .type = FIO_OPT_STR,
159 .cb = str_file_service_cb,
160 .help = "How to select which file to service next",
161 .def = "roundrobin",
162 .posval = { "random", "roundrobin" },
163 },
164 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100165 .name = "fsync",
166 .type = FIO_OPT_INT,
167 .off1 = td_var_offset(fsync_blocks),
168 .help = "Issue fsync for writes every given number of blocks",
169 .def = "0",
170 },
171 {
172 .name = "direct",
173 .type = FIO_OPT_BOOL,
174 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100175 .help = "Use O_DIRECT IO (negates buffered)",
176 .def = "0",
177 },
178 {
179 .name = "buffered",
180 .type = FIO_OPT_BOOL,
181 .off1 = td_var_offset(odirect),
182 .neg = 1,
183 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100184 .def = "1",
185 },
186 {
187 .name = "overwrite",
188 .type = FIO_OPT_BOOL,
189 .off1 = td_var_offset(overwrite),
190 .help = "When writing, set whether to overwrite current data",
191 .def = "0",
192 },
193 {
194 .name = "loops",
195 .type = FIO_OPT_INT,
196 .off1 = td_var_offset(loops),
197 .help = "Number of times to run the job",
198 .def = "1",
199 },
200 {
201 .name = "numjobs",
202 .type = FIO_OPT_INT,
203 .off1 = td_var_offset(numjobs),
204 .help = "Duplicate this job this many times",
205 .def = "1",
206 },
207 {
208 .name = "startdelay",
209 .type = FIO_OPT_INT,
210 .off1 = td_var_offset(start_delay),
211 .help = "Only start job when this period has passed",
212 .def = "0",
213 },
214 {
215 .name = "runtime",
216 .alias = "timeout",
217 .type = FIO_OPT_STR_VAL_TIME,
218 .off1 = td_var_offset(timeout),
219 .help = "Stop workload when this amount of time has passed",
220 .def = "0",
221 },
222 {
Jens Axboee1f36502006-10-27 10:54:08 +0200223 .name = "mem",
224 .type = FIO_OPT_STR,
225 .cb = str_mem_cb,
Jens Axboe15f79182007-01-10 12:26:18 +0100226 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100227 .def = "malloc",
Jens Axboe15f79182007-01-10 12:26:18 +0100228 .posval = { "malloc", "shm", "shmhuge", "mmap", "mmaphuge", },
Jens Axboee1f36502006-10-27 10:54:08 +0200229 },
230 {
231 .name = "verify",
232 .type = FIO_OPT_STR,
233 .cb = str_verify_cb,
Jens Axboe15f79182007-01-10 12:26:18 +0100234 .help = "Verify sum function",
Jens Axboeee738492007-01-10 11:23:16 +0100235 .def = "0",
Jens Axboe15f79182007-01-10 12:26:18 +0100236 .posval = { "crc32", "md5", },
Jens Axboee1f36502006-10-27 10:54:08 +0200237 },
238 {
239 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200240 .type = FIO_OPT_STR_STORE,
241 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100242 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200243 },
244 {
Jens Axboe076efc72006-10-27 11:24:25 +0200245 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200246 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200247 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100248 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200249 },
250 {
251 .name = "exec_prerun",
252 .type = FIO_OPT_STR_STORE,
253 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100254 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200255 },
256 {
257 .name = "exec_postrun",
258 .type = FIO_OPT_STR_STORE,
259 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100260 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200261 },
262#ifdef FIO_HAVE_IOSCHED_SWITCH
263 {
264 .name = "ioscheduler",
265 .type = FIO_OPT_STR_STORE,
266 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100267 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200268 },
269#endif
270 {
Jens Axboee1f36502006-10-27 10:54:08 +0200271 .name = "zonesize",
272 .type = FIO_OPT_STR_VAL,
273 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100274 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100275 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200276 },
277 {
278 .name = "zoneskip",
279 .type = FIO_OPT_STR_VAL,
280 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100281 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100282 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200283 },
284 {
285 .name = "lockmem",
286 .type = FIO_OPT_STR_VAL,
287 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100288 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100289 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200290 },
291 {
Jens Axboee1f36502006-10-27 10:54:08 +0200292 .name = "rwmixcycle",
293 .type = FIO_OPT_INT,
294 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100295 .help = "Cycle period for mixed read/write workloads (msec)",
296 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200297 },
298 {
299 .name = "rwmixread",
300 .type = FIO_OPT_INT,
301 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100302 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100303 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100304 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200305 },
306 {
307 .name = "rwmixwrite",
308 .type = FIO_OPT_INT,
309 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100310 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100311 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100312 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200313 },
314 {
315 .name = "nice",
316 .type = FIO_OPT_INT,
317 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100318 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100319 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100320 .maxval = 20,
321 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200322 },
323#ifdef FIO_HAVE_IOPRIO
324 {
325 .name = "prio",
326 .type = FIO_OPT_INT,
327 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100328 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100329 .minval = 0,
330 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200331 },
332 {
333 .name = "prioclass",
334 .type = FIO_OPT_INT,
335 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100336 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100337 .minval = 0,
338 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200339 },
340#endif
341 {
342 .name = "thinktime",
343 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100344 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100345 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100346 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200347 },
348 {
Jens Axboe48097d52007-02-17 06:30:44 +0100349 .name = "thinktime_spin",
350 .type = FIO_OPT_INT,
351 .off1 = td_var_offset(thinktime_spin),
352 .help = "Start thinktime by spinning this amount (usec)",
353 .def = "0",
354 },
355 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100356 .name = "thinktime_blocks",
357 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100358 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100359 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100360 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100361 },
362 {
Jens Axboee1f36502006-10-27 10:54:08 +0200363 .name = "rate",
364 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100365 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100366 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200367 },
368 {
369 .name = "ratemin",
370 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100371 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100372 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200373 },
374 {
375 .name = "ratecycle",
376 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100377 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100378 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100379 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200380 },
381 {
Jens Axboee1f36502006-10-27 10:54:08 +0200382 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100383 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100384 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100385 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100386 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200387 },
388 {
389 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100390 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100391 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100392 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100393 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200394 },
395 {
396 .name = "bwavgtime",
397 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100398 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100399 .help = "Time window over which to calculate bandwidth (msec)",
400 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200401 },
402 {
403 .name = "create_serialize",
Jens Axboe13335dd2007-01-10 13:14:02 +0100404 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100405 .off1 = td_var_offset(create_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100406 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100407 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200408 },
409 {
410 .name = "create_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100411 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100412 .off1 = td_var_offset(create_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100413 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100414 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200415 },
416 {
Jens Axboee1f36502006-10-27 10:54:08 +0200417 .name = "cpuload",
418 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100419 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100420 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200421 },
422 {
423 .name = "cpuchunks",
424 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100425 .off1 = td_var_offset(cpucycle),
Jens Axboefd28ca42007-01-09 21:20:13 +0100426 .help = "Length of the CPU burn cycles",
Jens Axboee1f36502006-10-27 10:54:08 +0200427 },
Jens Axboee1f36502006-10-27 10:54:08 +0200428#ifdef FIO_HAVE_CPU_AFFINITY
429 {
430 .name = "cpumask",
431 .type = FIO_OPT_INT,
432 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100433 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200434 },
435#endif
436 {
437 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100438 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100439 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100440 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100441 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200442 },
443 {
444 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100445 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200446 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100447 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100448 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200449 },
450 {
451 .name = "exitall",
452 .type = FIO_OPT_STR_SET,
453 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100454 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200455 },
456 {
457 .name = "stonewall",
458 .type = FIO_OPT_STR_SET,
459 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100460 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200461 },
462 {
463 .name = "thread",
464 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100465 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100466 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200467 },
468 {
469 .name = "write_bw_log",
470 .type = FIO_OPT_STR_SET,
471 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100472 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200473 },
474 {
475 .name = "write_lat_log",
476 .type = FIO_OPT_STR_SET,
477 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100478 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200479 },
480 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100481 .name = "hugepage-size",
482 .type = FIO_OPT_STR_VAL,
483 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100484 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100485 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100486 },
487 {
Jens Axboee1f36502006-10-27 10:54:08 +0200488 .name = NULL,
489 },
490};
491
Jens Axboeb4692822006-10-27 13:43:22 +0200492#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
493#define FIO_CMD_OPTS (16)
494#define FIO_GETOPT_JOB (0x89988998)
495
496/*
497 * Command line options. These will contain the above, plus a few
498 * extra that only pertain to fio itself and not jobs.
499 */
500static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
501 {
502 .name = "output",
503 .has_arg = required_argument,
504 .val = 'o',
505 },
506 {
507 .name = "timeout",
508 .has_arg = required_argument,
509 .val = 't',
510 },
511 {
512 .name = "latency-log",
513 .has_arg = required_argument,
514 .val = 'l',
515 },
516 {
517 .name = "bandwidth-log",
518 .has_arg = required_argument,
519 .val = 'b',
520 },
521 {
522 .name = "minimal",
523 .has_arg = optional_argument,
524 .val = 'm',
525 },
526 {
527 .name = "version",
528 .has_arg = no_argument,
529 .val = 'v',
530 },
531 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100532 .name = "help",
533 .has_arg = no_argument,
534 .val = 'h',
535 },
536 {
537 .name = "cmdhelp",
538 .has_arg = required_argument,
539 .val = 'c',
540 },
541 {
Jens Axboeb4692822006-10-27 13:43:22 +0200542 .name = NULL,
543 },
544};
545
Jens Axboeee738492007-01-10 11:23:16 +0100546static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200547
Jens Axboe5cd033b2007-01-09 08:19:01 +0100548static char fio_version_string[] = "fio 1.11";
Jens Axboeebac4652005-12-08 15:25:21 +0100549
Jens Axboe972cfd22006-06-09 11:08:56 +0200550static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100551static int max_jobs = MAX_JOBS;
552
553struct thread_data def_thread;
554struct thread_data *threads = NULL;
555
Jens Axboeebac4652005-12-08 15:25:21 +0100556int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200557int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200558unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200559FILE *f_out = NULL;
560FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100561
Jens Axboeee738492007-01-10 11:23:16 +0100562static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100563int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200564
Jens Axboe906c8d72006-06-13 09:37:56 +0200565/*
566 * Return a free job structure.
567 */
Jens Axboeebac4652005-12-08 15:25:21 +0100568static struct thread_data *get_new_job(int global, struct thread_data *parent)
569{
570 struct thread_data *td;
571
572 if (global)
573 return &def_thread;
574 if (thread_number >= max_jobs)
575 return NULL;
576
577 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200578 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100579
Jens Axboeebac4652005-12-08 15:25:21 +0100580 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100581 return td;
582}
583
584static void put_job(struct thread_data *td)
585{
Jens Axboe549577a2006-10-30 12:23:41 +0100586 if (td == &def_thread)
587 return;
588
Jens Axboe16edf252007-02-10 14:59:22 +0100589 if (td->error)
590 fprintf(f_out, "fio: %s\n", td->verror);
591
Jens Axboeebac4652005-12-08 15:25:21 +0100592 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
593 thread_number--;
594}
595
Jens Axboedad915e2006-10-27 11:10:18 +0200596/*
597 * Lazy way of fixing up options that depend on each other. We could also
598 * define option callback handlers, but this is easier.
599 */
Jens Axboee1f36502006-10-27 10:54:08 +0200600static void fixup_options(struct thread_data *td)
601{
Jens Axboee1f36502006-10-27 10:54:08 +0200602 if (!td->rwmixread && td->rwmixwrite)
603 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200604
Jens Axboe076efc72006-10-27 11:24:25 +0200605 if (td->write_iolog_file && td->read_iolog_file) {
606 log_err("fio: read iolog overrides write_iolog\n");
607 free(td->write_iolog_file);
608 td->write_iolog_file = NULL;
609 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100610
611 if (td->io_ops->flags & FIO_SYNCIO)
612 td->iodepth = 1;
613 else {
614 if (!td->iodepth)
615 td->iodepth = td->nr_files;
616 }
617
618 /*
619 * only really works for sequential io for now, and with 1 file
620 */
621 if (td->zone_size && !td->sequential && td->nr_files == 1)
622 td->zone_size = 0;
623
624 /*
625 * Reads can do overwrites, we always need to pre-create the file
626 */
627 if (td_read(td) || td_rw(td))
628 td->overwrite = 1;
629
Jens Axboea00735e2006-11-03 08:58:08 +0100630 if (!td->min_bs[DDIR_READ])
631 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
632 if (!td->max_bs[DDIR_READ])
633 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
634 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100635 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100636 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100637 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100638
639 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
640
Jens Axboe16b462a2006-10-30 12:35:18 +0100641 if (td_read(td) && !td_rw(td))
642 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100643
644 if (td->norandommap && td->verify != VERIFY_NONE) {
645 log_err("fio: norandommap given, verify disabled\n");
646 td->verify = VERIFY_NONE;
647 }
Jens Axboe690adba2006-10-30 15:25:09 +0100648 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
649 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100650
651 /*
652 * O_DIRECT and char doesn't mix, clear that flag if necessary.
653 */
654 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
655 td->odirect = 0;
Jens Axboe48097d52007-02-17 06:30:44 +0100656
657 /*
658 * thinktime_spin must be less than thinktime
659 */
660 if (td->thinktime_spin > td->thinktime)
661 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100662
663 /*
664 * The low water mark cannot be bigger than the iodepth
665 */
666 if (td->iodepth_low > td->iodepth || !td->iodepth_low)
667 td->iodepth_low = td->iodepth;
Jens Axboee1f36502006-10-27 10:54:08 +0200668}
669
Jens Axboe906c8d72006-06-13 09:37:56 +0200670/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100671 * This function leaks the buffer
672 */
673static char *to_kmg(unsigned int val)
674{
675 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100676 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100677 char *p = post;
678
Jens Axboe245142f2006-11-08 12:49:21 +0100679 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100680 if (val & 1023)
681 break;
682
683 val >>= 10;
684 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100685 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100686
687 snprintf(buf, 31, "%u%c", val, *p);
688 return buf;
689}
690
691/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200692 * Adds a job to the list of things todo. Sanitizes the various options
693 * to make sure we don't have conflicts, and initializes various
694 * members of td.
695 */
Jens Axboe75154842006-06-01 13:56:09 +0200696static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100697{
Jens Axboe3c9b60c2006-11-07 08:36:14 +0100698 const char *ddir_str[] = { "read", "write", "randread", "randwrite",
699 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100700 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200701 int numjobs, ddir, i;
702 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100703
Jens Axboeebac4652005-12-08 15:25:21 +0100704 /*
705 * the def_thread is just for options, it's not a real job
706 */
707 if (td == &def_thread)
708 return 0;
709
Jens Axboeee738492007-01-10 11:23:16 +0100710 assert(td->io_ops);
Jens Axboedf641192006-10-12 07:55:41 +0200711
Jens Axboe690adba2006-10-30 15:25:09 +0100712 if (td->odirect)
713 td->io_ops->flags |= FIO_RAWIO;
714
Jens Axboeebac4652005-12-08 15:25:21 +0100715 td->filetype = FIO_TYPE_FILE;
Jens Axboe890df532007-02-17 01:51:13 +0100716 if (td->filename && !lstat(td->filename, &sb)) {
Jens Axboe0af7b542006-02-17 10:10:12 +0100717 if (S_ISBLK(sb.st_mode))
718 td->filetype = FIO_TYPE_BD;
719 else if (S_ISCHR(sb.st_mode))
720 td->filetype = FIO_TYPE_CHAR;
721 }
Jens Axboeebac4652005-12-08 15:25:21 +0100722
Jens Axboee0a22332006-12-20 12:54:25 +0100723 fixup_options(td);
724
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200725 if (td->filename)
726 td->nr_uniq_files = 1;
727 else
728 td->nr_uniq_files = td->nr_files;
729
730 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200731 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200732 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200733
Jens Axboe16edf252007-02-10 14:59:22 +0100734 if (td->directory && td->directory[0] != '\0') {
735 if (lstat(td->directory, &sb) < 0) {
736 log_err("fio: %s is not a directory\n", td->directory);
Jens Axboee1161c32007-02-22 19:36:48 +0100737 td_verror(td, errno, "lstat");
Jens Axboe16edf252007-02-10 14:59:22 +0100738 return 1;
739 }
740 if (!S_ISDIR(sb.st_mode)) {
741 log_err("fio: %s is not a directory\n", td->directory);
742 return 1;
743 }
Jens Axboe8aeebd52007-01-08 10:47:43 +0100744 len = sprintf(tmp, "%s/", td->directory);
Jens Axboe16edf252007-02-10 14:59:22 +0100745 }
Jens Axboeebac4652005-12-08 15:25:21 +0100746
Jens Axboe53cdc682006-10-18 11:50:58 +0200747 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
748
749 for_each_file(td, f, i) {
750 memset(f, 0, sizeof(*f));
751 f->fd = -1;
752
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200753 if (td->filename)
754 sprintf(tmp + len, "%s", td->filename);
755 else
756 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200757 f->file_name = strdup(tmp);
758 }
759 } else {
760 td->nr_files = 1;
761 td->files = malloc(sizeof(struct fio_file));
762 f = &td->files[0];
763
764 memset(f, 0, sizeof(*f));
765 f->fd = -1;
766 f->file_name = strdup(jobname);
767 }
768
769 for_each_file(td, f, i) {
770 f->file_size = td->total_file_size / td->nr_files;
771 f->file_offset = td->start_offset;
772 }
773
Jens Axboebbfd6b02006-06-07 19:42:54 +0200774 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100775
Jens Axboe079ad092007-02-20 13:58:20 +0100776 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
777 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
778 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100779
Jens Axboeebac4652005-12-08 15:25:21 +0100780 if (td->stonewall && td->thread_number > 1)
781 groupid++;
782
783 td->groupid = groupid;
784
785 if (setup_rate(td))
786 goto err;
787
Jens Axboeec94ec52006-10-20 10:59:19 +0200788 if (td->write_lat_log) {
Jens Axboe079ad092007-02-20 13:58:20 +0100789 setup_log(&td->ts.slat_log);
790 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100791 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200792 if (td->write_bw_log)
Jens Axboe079ad092007-02-20 13:58:20 +0100793 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100794
Jens Axboeb4692822006-10-27 13:43:22 +0200795 if (!td->name)
796 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200797
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200798 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200799
Jens Axboec6ae0a52006-06-12 11:04:44 +0200800 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200801 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200802 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200803 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100804 else {
805 char *c1, *c2, *c3, *c4;
806
807 c1 = to_kmg(td->min_bs[DDIR_READ]);
808 c2 = to_kmg(td->max_bs[DDIR_READ]);
809 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
810 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
811
Jens Axboed56cbab2007-01-11 19:24:55 +0100812 fprintf(f_out, "%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100813
814 free(c1);
815 free(c2);
816 free(c3);
817 free(c4);
818 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200819 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200820 fprintf(f_out, "...\n");
821 }
Jens Axboeebac4652005-12-08 15:25:21 +0100822
823 /*
824 * recurse add identical jobs, clear numjobs and stonewall options
825 * as they don't apply to sub-jobs
826 */
827 numjobs = td->numjobs;
828 while (--numjobs) {
829 struct thread_data *td_new = get_new_job(0, td);
830
831 if (!td_new)
832 goto err;
833
834 td_new->numjobs = 1;
835 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200836 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100837
Jens Axboe75154842006-06-01 13:56:09 +0200838 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100839 goto err;
840 }
841 return 0;
842err:
843 put_job(td);
844 return -1;
845}
846
Jens Axboe906c8d72006-06-13 09:37:56 +0200847/*
848 * Initialize the various random states we need (random io, block size ranges,
849 * read/write mix, etc).
850 */
Jens Axboeebac4652005-12-08 15:25:21 +0100851int init_random_state(struct thread_data *td)
852{
Jens Axboe0aabe162007-02-23 08:45:55 +0100853 unsigned long seeds[5];
Jens Axboe53cdc682006-10-18 11:50:58 +0200854 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200855 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100856
Jens Axboef48b4672006-10-27 11:30:07 +0200857 if (td->io_ops->flags & FIO_CPUIO)
858 return 0;
859
Jens Axboe1ac267b2006-05-31 20:29:00 +0200860 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100861 if (fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100862 td_verror(td, errno, "open");
Jens Axboeebac4652005-12-08 15:25:21 +0100863 return 1;
864 }
865
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200866 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100867 td_verror(td, EIO, "read");
Jens Axboeebac4652005-12-08 15:25:21 +0100868 close(fd);
869 return 1;
870 }
871
872 close(fd);
873
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200874 os_random_seed(seeds[0], &td->bsrange_state);
875 os_random_seed(seeds[1], &td->verify_state);
876 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboe0aabe162007-02-23 08:45:55 +0100877 os_random_seed(seeds[3], &td->next_file_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100878
879 if (td->sequential)
880 return 0;
881
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200882 if (td->rand_repeatable)
Jens Axboe0aabe162007-02-23 08:45:55 +0100883 seeds[4] = FIO_RANDSEED * td->thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100884
Jens Axboebb8895e2006-10-30 15:14:48 +0100885 if (!td->norandommap) {
886 for_each_file(td, f, i) {
Jens Axboea4a81712007-02-13 20:07:26 +0100887 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100888 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100889 f->file_map = malloc(num_maps * sizeof(long));
890 f->num_maps = num_maps;
891 memset(f->file_map, 0, num_maps * sizeof(long));
892 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200893 }
Jens Axboeebac4652005-12-08 15:25:21 +0100894
Jens Axboe0aabe162007-02-23 08:45:55 +0100895 os_random_seed(seeds[4], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100896 return 0;
897}
898
899static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
900{
901#ifdef FIO_HAVE_CPU_AFFINITY
902 unsigned int i;
903
904 CPU_ZERO(&cpumask);
905
906 for (i = 0; i < sizeof(int) * 8; i++) {
907 if ((1 << i) & cpu)
908 CPU_SET(i, &cpumask);
909 }
910#endif
911}
912
Jens Axboeebac4652005-12-08 15:25:21 +0100913static int is_empty_or_comment(char *line)
914{
915 unsigned int i;
916
917 for (i = 0; i < strlen(line); i++) {
918 if (line[i] == ';')
919 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100920 if (line[i] == '#')
921 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100922 if (!isspace(line[i]) && !iscntrl(line[i]))
923 return 0;
924 }
925
926 return 1;
927}
928
Jens Axboeb4692822006-10-27 13:43:22 +0200929static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100930{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200931 struct thread_data *td = data;
932
Jens Axboeebac4652005-12-08 15:25:21 +0100933 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
934 td->ddir = DDIR_READ;
935 td->sequential = 1;
936 return 0;
937 } else if (!strncmp(mem, "randread", 8)) {
938 td->ddir = DDIR_READ;
939 td->sequential = 0;
940 return 0;
941 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
942 td->ddir = DDIR_WRITE;
943 td->sequential = 1;
944 return 0;
945 } else if (!strncmp(mem, "randwrite", 9)) {
946 td->ddir = DDIR_WRITE;
947 td->sequential = 0;
948 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200949 } else if (!strncmp(mem, "rw", 2)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100950 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200951 td->iomix = 1;
952 td->sequential = 1;
953 return 0;
954 } else if (!strncmp(mem, "randrw", 6)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100955 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200956 td->iomix = 1;
957 td->sequential = 0;
958 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100959 }
960
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200961 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100962 return 1;
963}
964
Jens Axboeb4692822006-10-27 13:43:22 +0200965static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100966{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200967 struct thread_data *td = data;
968
Jens Axboeebac4652005-12-08 15:25:21 +0100969 if (!strncmp(mem, "0", 1)) {
970 td->verify = VERIFY_NONE;
971 return 0;
972 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
973 td->verify = VERIFY_MD5;
974 return 0;
975 } else if (!strncmp(mem, "crc32", 5)) {
976 td->verify = VERIFY_CRC32;
977 return 0;
978 }
979
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200980 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100981 return 1;
982}
983
Jens Axboe313cb202006-12-21 09:50:00 +0100984/*
985 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
986 */
987static char *get_mmap_file(const char *str)
988{
989 char *p = strstr(str, ":");
990
991 if (!p)
992 return NULL;
993
994 p++;
995 strip_blank_front(&p);
996 strip_blank_end(p);
997 return strdup(p);
998}
999
Jens Axboeb4692822006-10-27 13:43:22 +02001000static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +01001001{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001002 struct thread_data *td = data;
1003
Jens Axboeebac4652005-12-08 15:25:21 +01001004 if (!strncmp(mem, "malloc", 6)) {
1005 td->mem_type = MEM_MALLOC;
1006 return 0;
Jens Axboed0bdaf42006-12-20 14:40:44 +01001007 } else if (!strncmp(mem, "mmaphuge", 8)) {
1008#ifdef FIO_HAVE_HUGETLB
Jens Axboed0bdaf42006-12-20 14:40:44 +01001009 /*
1010 * mmaphuge must be appended with the actual file
1011 */
Jens Axboe313cb202006-12-21 09:50:00 +01001012 td->mmapfile = get_mmap_file(mem);
1013 if (!td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +01001014 log_err("fio: mmaphuge:/path/to/file\n");
1015 return 1;
1016 }
1017
Jens Axboed0bdaf42006-12-20 14:40:44 +01001018 td->mem_type = MEM_MMAPHUGE;
1019 return 0;
1020#else
1021 log_err("fio: mmaphuge not available\n");
1022 return 1;
1023#endif
Jens Axboeebac4652005-12-08 15:25:21 +01001024 } else if (!strncmp(mem, "mmap", 4)) {
Jens Axboe313cb202006-12-21 09:50:00 +01001025 /*
1026 * Check if the user wants file backed memory. It's ok
1027 * if there's no file given, we'll just use anon mamp then.
1028 */
1029 td->mmapfile = get_mmap_file(mem);
Jens Axboeebac4652005-12-08 15:25:21 +01001030 td->mem_type = MEM_MMAP;
1031 return 0;
Jens Axboe74b025b2006-12-19 15:18:14 +01001032 } else if (!strncmp(mem, "shmhuge", 7)) {
1033#ifdef FIO_HAVE_HUGETLB
1034 td->mem_type = MEM_SHMHUGE;
1035 return 0;
1036#else
1037 log_err("fio: shmhuge not available\n");
1038 return 1;
1039#endif
Jens Axboe0268b8b2006-12-20 12:48:23 +01001040 } else if (!strncmp(mem, "shm", 3)) {
1041 td->mem_type = MEM_SHM;
1042 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001043 }
1044
Jens Axboed0bdaf42006-12-20 14:40:44 +01001045 log_err("fio: mem type: malloc, shm, shmhuge, mmap, mmaphuge\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001046 return 1;
1047}
1048
Jens Axboeb4692822006-10-27 13:43:22 +02001049static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +01001050{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001051 struct thread_data *td = data;
1052
Jens Axboe2866c822006-10-09 15:57:48 +02001053 td->io_ops = load_ioengine(td, str);
1054 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +01001055 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001056
Jens Axboe94cd00d2007-02-22 14:08:36 +01001057 log_err("fio: ioengine= libaio, posixaio, sync, syslet-rw, mmap, sgio, splice, cpu, null\n");
Jens Axboe5f350952006-11-07 15:20:59 +01001058 log_err("fio: or specify path to dynamic ioengine module\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001059 return 1;
1060}
1061
Jens Axboee1f36502006-10-27 10:54:08 +02001062static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1063{
1064 mlock_size = *val;
1065 return 0;
1066}
1067
Jens Axboe34cfcda2006-11-03 14:00:45 +01001068#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001069static int str_prioclass_cb(void *data, unsigned int *val)
1070{
1071 struct thread_data *td = data;
1072
1073 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1074 return 0;
1075}
1076
1077static int str_prio_cb(void *data, unsigned int *val)
1078{
1079 struct thread_data *td = data;
1080
1081 td->ioprio |= *val;
1082 return 0;
1083}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001084#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001085
1086static int str_exitall_cb(void)
1087{
1088 exitall_on_terminate = 1;
1089 return 0;
1090}
1091
1092static int str_cpumask_cb(void *data, unsigned int *val)
1093{
1094 struct thread_data *td = data;
1095
1096 fill_cpu_mask(td->cpumask, *val);
1097 return 0;
1098}
1099
Jens Axboe0aabe162007-02-23 08:45:55 +01001100static int str_file_service_cb(void *data, const char *str)
1101{
1102 struct thread_data *td = data;
1103
1104 if (!strncmp(str, "random", 6)) {
1105 td->file_service_type = FIO_FSERVICE_RANDOM;
1106 return 0;
1107 } else if (!strncmp(str, "roundrobin", 10)) {
1108 td->file_service_type = FIO_FSERVICE_RR;
1109 return 0;
1110 }
1111
1112 log_err("fio: file_service= random, roundrobin\n");
1113 return 1;
1114}
1115
Jens Axboe07261982006-06-07 10:51:12 +02001116/*
1117 * This is our [ini] type file parser.
1118 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001119static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001120{
Jens Axboee1f36502006-10-27 10:54:08 +02001121 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001122 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001123 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001124 fpos_t off;
1125 FILE *f;
1126 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001127 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001128
1129 f = fopen(file, "r");
1130 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001131 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001132 return 1;
1133 }
1134
1135 string = malloc(4096);
1136 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001137 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001138
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001139 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001140 do {
1141 p = fgets(string, 4095, f);
1142 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001143 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001144 if (is_empty_or_comment(p))
1145 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001146 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001147 continue;
1148
1149 global = !strncmp(name, "global", 6);
1150
1151 name[strlen(name) - 1] = '\0';
1152
1153 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001154 if (!td) {
1155 ret = 1;
1156 break;
1157 }
Jens Axboeebac4652005-12-08 15:25:21 +01001158
Jens Axboe972cfd22006-06-09 11:08:56 +02001159 /*
1160 * Seperate multiple job files by a stonewall
1161 */
Jens Axboef9481912006-06-09 11:37:28 +02001162 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001163 td->stonewall = stonewall;
1164 stonewall = 0;
1165 }
1166
Jens Axboeebac4652005-12-08 15:25:21 +01001167 fgetpos(f, &off);
1168 while ((p = fgets(string, 4096, f)) != NULL) {
1169 if (is_empty_or_comment(p))
1170 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001171
Jens Axboeb6754f92006-05-30 14:29:32 +02001172 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001173
1174 if (p[0] == '[')
1175 break;
1176
Jens Axboe4ae3f762006-05-30 13:35:14 +02001177 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001178
Jens Axboee1f36502006-10-27 10:54:08 +02001179 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001180
Jens Axboe45410ac2006-06-07 11:10:39 +02001181 /*
1182 * Don't break here, continue parsing options so we
1183 * dump all the bad ones. Makes trial/error fixups
1184 * easier on the user.
1185 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001186 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001187 }
Jens Axboeebac4652005-12-08 15:25:21 +01001188
Jens Axboe45410ac2006-06-07 11:10:39 +02001189 if (!ret) {
1190 fsetpos(f, &off);
1191 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001192 } else {
1193 log_err("fio: job %s dropped\n", name);
1194 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001195 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001196 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001197
1198 free(string);
1199 free(name);
1200 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001201 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001202}
1203
1204static int fill_def_thread(void)
1205{
1206 memset(&def_thread, 0, sizeof(def_thread));
1207
1208 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1209 perror("sched_getaffinity");
1210 return 1;
1211 }
1212
1213 /*
Jens Axboeee738492007-01-10 11:23:16 +01001214 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001215 */
Jens Axboeee738492007-01-10 11:23:16 +01001216 fill_default_options(&def_thread, options);
1217
Jens Axboe972cfd22006-06-09 11:08:56 +02001218 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001219 def_thread.write_bw_log = write_bw_log;
1220 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001221
Jens Axboeebac4652005-12-08 15:25:21 +01001222#ifdef FIO_HAVE_DISK_UTIL
1223 def_thread.do_disk_util = 1;
1224#endif
1225
1226 return 0;
1227}
1228
Jens Axboe0ab8db82006-10-18 17:16:23 +02001229static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001230{
1231 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001232 printf("\t--output\tWrite output to file\n");
1233 printf("\t--timeout\tRuntime in seconds\n");
1234 printf("\t--latency-log\tGenerate per-job latency logs\n");
1235 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1236 printf("\t--minimal\tMinimal (terse) output\n");
1237 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001238 printf("\t--help\t\tPrint this page\n");
1239 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001240}
1241
Jens Axboe972cfd22006-06-09 11:08:56 +02001242static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001243{
Jens Axboeb4692822006-10-27 13:43:22 +02001244 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001245 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001246
Jens Axboeb4692822006-10-27 13:43:22 +02001247 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001248 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001249 case 't':
1250 def_timeout = atoi(optarg);
1251 break;
1252 case 'l':
1253 write_lat_log = 1;
1254 break;
1255 case 'w':
1256 write_bw_log = 1;
1257 break;
1258 case 'o':
1259 f_out = fopen(optarg, "w+");
1260 if (!f_out) {
1261 perror("fopen output");
1262 exit(1);
1263 }
1264 f_err = f_out;
1265 break;
1266 case 'm':
1267 terse_output = 1;
1268 break;
1269 case 'h':
1270 usage();
1271 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001272 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001273 ret = show_cmd_help(options, optarg);
1274 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001275 case 'v':
1276 printf("%s\n", fio_version_string);
1277 exit(0);
1278 case FIO_GETOPT_JOB: {
1279 const char *opt = long_options[lidx].name;
1280 char *val = optarg;
1281
Jens Axboec2b1e752006-10-30 09:03:13 +01001282 if (!strncmp(opt, "name", 4) && td) {
1283 ret = add_job(td, td->name ?: "fio", 0);
1284 if (ret) {
1285 put_job(td);
1286 return 0;
1287 }
1288 td = NULL;
1289 }
Jens Axboeb4692822006-10-27 13:43:22 +02001290 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001291 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001292
1293 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001294 if (!td)
1295 return 0;
1296 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001297
Jens Axboeb1508cf2006-11-02 09:12:40 +01001298 ret = parse_cmd_option(opt, val, options, td);
1299 if (ret) {
1300 log_err("fio: job dropped\n");
1301 put_job(td);
1302 td = NULL;
1303 }
Jens Axboeb4692822006-10-27 13:43:22 +02001304 break;
1305 }
1306 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001307 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001308 }
1309 }
Jens Axboec9fad892006-05-27 17:26:50 +02001310
Jens Axboeb4692822006-10-27 13:43:22 +02001311 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001312 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001313 if (ret)
1314 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001315 }
Jens Axboe774a6172006-08-24 08:44:26 +02001316
Jens Axboeb4692822006-10-27 13:43:22 +02001317 while (optind < argc) {
1318 ini_idx++;
1319 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1320 ini_file[ini_idx - 1] = strdup(argv[optind]);
1321 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001322 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001323
1324 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001325}
1326
1327static void free_shm(void)
1328{
1329 struct shmid_ds sbuf;
1330
1331 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001332 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001333 threads = NULL;
1334 shmctl(shm_id, IPC_RMID, &sbuf);
1335 }
1336}
1337
Jens Axboe906c8d72006-06-13 09:37:56 +02001338/*
1339 * The thread area is shared between the main process and the job
1340 * threads/processes. So setup a shared memory segment that will hold
1341 * all the job info.
1342 */
Jens Axboeebac4652005-12-08 15:25:21 +01001343static int setup_thread_area(void)
1344{
1345 /*
1346 * 1024 is too much on some machines, scale max_jobs if
1347 * we get a failure that looks like too large a shm segment
1348 */
1349 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001350 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001351
Jens Axboe906c8d72006-06-13 09:37:56 +02001352 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001353 if (shm_id != -1)
1354 break;
1355 if (errno != EINVAL) {
1356 perror("shmget");
1357 break;
1358 }
1359
1360 max_jobs >>= 1;
1361 } while (max_jobs);
1362
1363 if (shm_id == -1)
1364 return 1;
1365
1366 threads = shmat(shm_id, NULL, 0);
1367 if (threads == (void *) -1) {
1368 perror("shmat");
1369 return 1;
1370 }
1371
1372 atexit(free_shm);
1373 return 0;
1374}
1375
Jens Axboeb4692822006-10-27 13:43:22 +02001376/*
1377 * Copy the fio options into the long options map, so we mirror
1378 * job and cmd line options.
1379 */
1380static void dupe_job_options(void)
1381{
1382 struct fio_option *o;
1383 unsigned int i;
1384
1385 i = 0;
1386 while (long_options[i].name)
1387 i++;
1388
1389 o = &options[0];
1390 while (o->name) {
1391 long_options[i].name = o->name;
1392 long_options[i].val = FIO_GETOPT_JOB;
1393 if (o->type == FIO_OPT_STR_SET)
1394 long_options[i].has_arg = no_argument;
1395 else
1396 long_options[i].has_arg = required_argument;
1397
1398 i++;
1399 o++;
1400 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1401 }
1402}
1403
Jens Axboeebac4652005-12-08 15:25:21 +01001404int parse_options(int argc, char *argv[])
1405{
Jens Axboe972cfd22006-06-09 11:08:56 +02001406 int job_files, i;
1407
Jens Axboeb4692822006-10-27 13:43:22 +02001408 f_out = stdout;
1409 f_err = stderr;
1410
Jens Axboe13335dd2007-01-10 13:14:02 +01001411 options_init(options);
1412
Jens Axboeb4692822006-10-27 13:43:22 +02001413 dupe_job_options();
1414
Jens Axboeebac4652005-12-08 15:25:21 +01001415 if (setup_thread_area())
1416 return 1;
1417 if (fill_def_thread())
1418 return 1;
1419
Jens Axboe972cfd22006-06-09 11:08:56 +02001420 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001421
Jens Axboe972cfd22006-06-09 11:08:56 +02001422 for (i = 0; i < job_files; i++) {
1423 if (fill_def_thread())
1424 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001425 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001426 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001427 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001428 }
Jens Axboeebac4652005-12-08 15:25:21 +01001429
Jens Axboe88c6ed82006-06-09 11:28:10 +02001430 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001431
1432 if (!thread_number) {
1433 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001434 return 1;
1435 }
1436
Jens Axboeebac4652005-12-08 15:25:21 +01001437 return 0;
1438}