blob: c8f27ade76e7f2aafaef4e98320d189c2a26e347 [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 *);
36
Jens Axboeee738492007-01-10 11:23:16 +010037#define __stringify_1(x) #x
38#define __stringify(x) __stringify_1(x)
39
Jens Axboee1f36502006-10-27 10:54:08 +020040/*
41 * Map of job/command line options
42 */
43static struct fio_option options[] = {
44 {
Jens Axboe61697c32007-02-05 15:04:46 +010045 .name = "description",
46 .type = FIO_OPT_STR_STORE,
47 .off1 = td_var_offset(description),
48 .help = "Text job description",
49 },
50 {
Jens Axboee1f36502006-10-27 10:54:08 +020051 .name = "name",
52 .type = FIO_OPT_STR_STORE,
53 .off1 = td_var_offset(name),
Jens Axboefd28ca42007-01-09 21:20:13 +010054 .help = "Name of this job",
Jens Axboee1f36502006-10-27 10:54:08 +020055 },
56 {
57 .name = "directory",
58 .type = FIO_OPT_STR_STORE,
59 .off1 = td_var_offset(directory),
Jens Axboefd28ca42007-01-09 21:20:13 +010060 .help = "Directory to store files in",
Jens Axboee1f36502006-10-27 10:54:08 +020061 },
62 {
63 .name = "filename",
64 .type = FIO_OPT_STR_STORE,
65 .off1 = td_var_offset(filename),
Jens Axboefd28ca42007-01-09 21:20:13 +010066 .help = "Force the use of a specific file",
Jens Axboee1f36502006-10-27 10:54:08 +020067 },
68 {
69 .name = "rw",
70 .type = FIO_OPT_STR,
71 .cb = str_rw_cb,
Jens Axboe15f79182007-01-10 12:26:18 +010072 .help = "IO direction",
Jens Axboeee738492007-01-10 11:23:16 +010073 .def = "read",
Jens Axboe15f79182007-01-10 12:26:18 +010074 .posval = { "read", "write", "randwrite", "randread", "rw",
75 "randrw", },
Jens Axboee1f36502006-10-27 10:54:08 +020076 },
77 {
78 .name = "ioengine",
79 .type = FIO_OPT_STR,
80 .cb = str_ioengine_cb,
Jens Axboe15f79182007-01-10 12:26:18 +010081 .help = "IO engine to use",
Jens Axboeee738492007-01-10 11:23:16 +010082 .def = "sync",
Jens Axboe15f79182007-01-10 12:26:18 +010083 .posval = { "sync", "libaio", "posixaio", "mmap", "splice",
Jens Axboea4f4fdd2007-02-14 01:16:39 +010084 "sg", "null", "net", "syslet-rw" },
Jens Axboee1f36502006-10-27 10:54:08 +020085 },
86 {
Jens Axboe03b74b32007-01-11 11:04:31 +010087 .name = "iodepth",
88 .type = FIO_OPT_INT,
89 .off1 = td_var_offset(iodepth),
90 .help = "Amount of IO buffers to keep in flight",
91 .def = "1",
92 },
93 {
Jens Axboee916b392007-02-20 14:37:26 +010094 .name = "iodepth_low",
95 .type = FIO_OPT_INT,
96 .off1 = td_var_offset(iodepth_low),
97 .help = "Low water mark for queuing depth",
98 },
99 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100100 .name = "size",
101 .type = FIO_OPT_STR_VAL,
102 .off1 = td_var_offset(total_file_size),
103 .help = "Size of device or file",
104 },
105 {
106 .name = "bs",
107 .type = FIO_OPT_STR_VAL_INT,
108 .off1 = td_var_offset(bs[DDIR_READ]),
109 .off2 = td_var_offset(bs[DDIR_WRITE]),
110 .help = "Block size unit",
111 .def = "4k",
112 },
113 {
114 .name = "bsrange",
115 .type = FIO_OPT_RANGE,
116 .off1 = td_var_offset(min_bs[DDIR_READ]),
117 .off2 = td_var_offset(max_bs[DDIR_READ]),
118 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
119 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
120 .help = "Set block size range (in more detail than bs)",
121 },
122 {
123 .name = "bs_unaligned",
124 .type = FIO_OPT_STR_SET,
125 .off1 = td_var_offset(bs_unaligned),
126 .help = "Don't sector align IO buffer sizes",
127 },
128 {
129 .name = "offset",
130 .type = FIO_OPT_STR_VAL,
131 .off1 = td_var_offset(start_offset),
132 .help = "Start IO from this offset",
133 .def = "0",
134 },
135 {
136 .name = "randrepeat",
137 .type = FIO_OPT_BOOL,
138 .off1 = td_var_offset(rand_repeatable),
139 .help = "Use repeatable random IO pattern",
140 .def = "1",
141 },
142 {
143 .name = "norandommap",
144 .type = FIO_OPT_STR_SET,
145 .off1 = td_var_offset(norandommap),
146 .help = "Accept potential duplicate random blocks",
147 },
148 {
149 .name = "nrfiles",
150 .type = FIO_OPT_INT,
151 .off1 = td_var_offset(nr_files),
152 .help = "Split job workload between this number of files",
153 .def = "1",
154 },
155 {
156 .name = "fsync",
157 .type = FIO_OPT_INT,
158 .off1 = td_var_offset(fsync_blocks),
159 .help = "Issue fsync for writes every given number of blocks",
160 .def = "0",
161 },
162 {
163 .name = "direct",
164 .type = FIO_OPT_BOOL,
165 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100166 .help = "Use O_DIRECT IO (negates buffered)",
167 .def = "0",
168 },
169 {
170 .name = "buffered",
171 .type = FIO_OPT_BOOL,
172 .off1 = td_var_offset(odirect),
173 .neg = 1,
174 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100175 .def = "1",
176 },
177 {
178 .name = "overwrite",
179 .type = FIO_OPT_BOOL,
180 .off1 = td_var_offset(overwrite),
181 .help = "When writing, set whether to overwrite current data",
182 .def = "0",
183 },
184 {
185 .name = "loops",
186 .type = FIO_OPT_INT,
187 .off1 = td_var_offset(loops),
188 .help = "Number of times to run the job",
189 .def = "1",
190 },
191 {
192 .name = "numjobs",
193 .type = FIO_OPT_INT,
194 .off1 = td_var_offset(numjobs),
195 .help = "Duplicate this job this many times",
196 .def = "1",
197 },
198 {
199 .name = "startdelay",
200 .type = FIO_OPT_INT,
201 .off1 = td_var_offset(start_delay),
202 .help = "Only start job when this period has passed",
203 .def = "0",
204 },
205 {
206 .name = "runtime",
207 .alias = "timeout",
208 .type = FIO_OPT_STR_VAL_TIME,
209 .off1 = td_var_offset(timeout),
210 .help = "Stop workload when this amount of time has passed",
211 .def = "0",
212 },
213 {
Jens Axboee1f36502006-10-27 10:54:08 +0200214 .name = "mem",
215 .type = FIO_OPT_STR,
216 .cb = str_mem_cb,
Jens Axboe15f79182007-01-10 12:26:18 +0100217 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100218 .def = "malloc",
Jens Axboe15f79182007-01-10 12:26:18 +0100219 .posval = { "malloc", "shm", "shmhuge", "mmap", "mmaphuge", },
Jens Axboee1f36502006-10-27 10:54:08 +0200220 },
221 {
222 .name = "verify",
223 .type = FIO_OPT_STR,
224 .cb = str_verify_cb,
Jens Axboe15f79182007-01-10 12:26:18 +0100225 .help = "Verify sum function",
Jens Axboeee738492007-01-10 11:23:16 +0100226 .def = "0",
Jens Axboe15f79182007-01-10 12:26:18 +0100227 .posval = { "crc32", "md5", },
Jens Axboee1f36502006-10-27 10:54:08 +0200228 },
229 {
230 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200231 .type = FIO_OPT_STR_STORE,
232 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100233 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200234 },
235 {
Jens Axboe076efc72006-10-27 11:24:25 +0200236 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200237 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200238 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100239 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200240 },
241 {
242 .name = "exec_prerun",
243 .type = FIO_OPT_STR_STORE,
244 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100245 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200246 },
247 {
248 .name = "exec_postrun",
249 .type = FIO_OPT_STR_STORE,
250 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100251 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200252 },
253#ifdef FIO_HAVE_IOSCHED_SWITCH
254 {
255 .name = "ioscheduler",
256 .type = FIO_OPT_STR_STORE,
257 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100258 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200259 },
260#endif
261 {
Jens Axboee1f36502006-10-27 10:54:08 +0200262 .name = "zonesize",
263 .type = FIO_OPT_STR_VAL,
264 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100265 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100266 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200267 },
268 {
269 .name = "zoneskip",
270 .type = FIO_OPT_STR_VAL,
271 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100272 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100273 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200274 },
275 {
276 .name = "lockmem",
277 .type = FIO_OPT_STR_VAL,
278 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100279 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100280 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200281 },
282 {
Jens Axboee1f36502006-10-27 10:54:08 +0200283 .name = "rwmixcycle",
284 .type = FIO_OPT_INT,
285 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100286 .help = "Cycle period for mixed read/write workloads (msec)",
287 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200288 },
289 {
290 .name = "rwmixread",
291 .type = FIO_OPT_INT,
292 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100293 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100294 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100295 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200296 },
297 {
298 .name = "rwmixwrite",
299 .type = FIO_OPT_INT,
300 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100301 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100302 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100303 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200304 },
305 {
306 .name = "nice",
307 .type = FIO_OPT_INT,
308 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100309 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100310 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100311 .maxval = 20,
312 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200313 },
314#ifdef FIO_HAVE_IOPRIO
315 {
316 .name = "prio",
317 .type = FIO_OPT_INT,
318 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100319 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100320 .minval = 0,
321 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200322 },
323 {
324 .name = "prioclass",
325 .type = FIO_OPT_INT,
326 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100327 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100328 .minval = 0,
329 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200330 },
331#endif
332 {
333 .name = "thinktime",
334 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100335 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100336 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100337 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200338 },
339 {
Jens Axboe48097d52007-02-17 06:30:44 +0100340 .name = "thinktime_spin",
341 .type = FIO_OPT_INT,
342 .off1 = td_var_offset(thinktime_spin),
343 .help = "Start thinktime by spinning this amount (usec)",
344 .def = "0",
345 },
346 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100347 .name = "thinktime_blocks",
348 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100349 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100350 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100351 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100352 },
353 {
Jens Axboee1f36502006-10-27 10:54:08 +0200354 .name = "rate",
355 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100356 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100357 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200358 },
359 {
360 .name = "ratemin",
361 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100362 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100363 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200364 },
365 {
366 .name = "ratecycle",
367 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100368 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100369 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100370 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200371 },
372 {
Jens Axboee1f36502006-10-27 10:54:08 +0200373 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100374 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100375 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100376 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100377 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200378 },
379 {
380 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100381 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100382 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100383 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100384 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200385 },
386 {
387 .name = "bwavgtime",
388 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100389 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100390 .help = "Time window over which to calculate bandwidth (msec)",
391 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200392 },
393 {
394 .name = "create_serialize",
Jens Axboe13335dd2007-01-10 13:14:02 +0100395 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100396 .off1 = td_var_offset(create_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100397 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100398 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200399 },
400 {
401 .name = "create_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100402 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100403 .off1 = td_var_offset(create_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100404 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100405 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200406 },
407 {
Jens Axboee1f36502006-10-27 10:54:08 +0200408 .name = "cpuload",
409 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100410 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100411 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200412 },
413 {
414 .name = "cpuchunks",
415 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100416 .off1 = td_var_offset(cpucycle),
Jens Axboefd28ca42007-01-09 21:20:13 +0100417 .help = "Length of the CPU burn cycles",
Jens Axboee1f36502006-10-27 10:54:08 +0200418 },
Jens Axboee1f36502006-10-27 10:54:08 +0200419#ifdef FIO_HAVE_CPU_AFFINITY
420 {
421 .name = "cpumask",
422 .type = FIO_OPT_INT,
423 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100424 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200425 },
426#endif
427 {
428 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100429 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100430 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100431 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100432 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200433 },
434 {
435 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100436 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200437 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100438 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100439 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200440 },
441 {
442 .name = "exitall",
443 .type = FIO_OPT_STR_SET,
444 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100445 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200446 },
447 {
448 .name = "stonewall",
449 .type = FIO_OPT_STR_SET,
450 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100451 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200452 },
453 {
454 .name = "thread",
455 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100456 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100457 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200458 },
459 {
460 .name = "write_bw_log",
461 .type = FIO_OPT_STR_SET,
462 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100463 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200464 },
465 {
466 .name = "write_lat_log",
467 .type = FIO_OPT_STR_SET,
468 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100469 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200470 },
471 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100472 .name = "hugepage-size",
473 .type = FIO_OPT_STR_VAL,
474 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100475 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100476 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100477 },
478 {
Jens Axboee1f36502006-10-27 10:54:08 +0200479 .name = NULL,
480 },
481};
482
Jens Axboeb4692822006-10-27 13:43:22 +0200483#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
484#define FIO_CMD_OPTS (16)
485#define FIO_GETOPT_JOB (0x89988998)
486
487/*
488 * Command line options. These will contain the above, plus a few
489 * extra that only pertain to fio itself and not jobs.
490 */
491static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
492 {
493 .name = "output",
494 .has_arg = required_argument,
495 .val = 'o',
496 },
497 {
498 .name = "timeout",
499 .has_arg = required_argument,
500 .val = 't',
501 },
502 {
503 .name = "latency-log",
504 .has_arg = required_argument,
505 .val = 'l',
506 },
507 {
508 .name = "bandwidth-log",
509 .has_arg = required_argument,
510 .val = 'b',
511 },
512 {
513 .name = "minimal",
514 .has_arg = optional_argument,
515 .val = 'm',
516 },
517 {
518 .name = "version",
519 .has_arg = no_argument,
520 .val = 'v',
521 },
522 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100523 .name = "help",
524 .has_arg = no_argument,
525 .val = 'h',
526 },
527 {
528 .name = "cmdhelp",
529 .has_arg = required_argument,
530 .val = 'c',
531 },
532 {
Jens Axboeb4692822006-10-27 13:43:22 +0200533 .name = NULL,
534 },
535};
536
Jens Axboeee738492007-01-10 11:23:16 +0100537static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200538
Jens Axboe5cd033b2007-01-09 08:19:01 +0100539static char fio_version_string[] = "fio 1.11";
Jens Axboeebac4652005-12-08 15:25:21 +0100540
Jens Axboe972cfd22006-06-09 11:08:56 +0200541static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100542static int max_jobs = MAX_JOBS;
543
544struct thread_data def_thread;
545struct thread_data *threads = NULL;
546
Jens Axboeebac4652005-12-08 15:25:21 +0100547int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200548int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200549unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200550FILE *f_out = NULL;
551FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100552
Jens Axboeee738492007-01-10 11:23:16 +0100553static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100554int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200555
Jens Axboe906c8d72006-06-13 09:37:56 +0200556/*
557 * Return a free job structure.
558 */
Jens Axboeebac4652005-12-08 15:25:21 +0100559static struct thread_data *get_new_job(int global, struct thread_data *parent)
560{
561 struct thread_data *td;
562
563 if (global)
564 return &def_thread;
565 if (thread_number >= max_jobs)
566 return NULL;
567
568 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200569 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100570
Jens Axboeebac4652005-12-08 15:25:21 +0100571 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100572 return td;
573}
574
575static void put_job(struct thread_data *td)
576{
Jens Axboe549577a2006-10-30 12:23:41 +0100577 if (td == &def_thread)
578 return;
579
Jens Axboe16edf252007-02-10 14:59:22 +0100580 if (td->error)
581 fprintf(f_out, "fio: %s\n", td->verror);
582
Jens Axboeebac4652005-12-08 15:25:21 +0100583 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
584 thread_number--;
585}
586
Jens Axboedad915e2006-10-27 11:10:18 +0200587/*
588 * Lazy way of fixing up options that depend on each other. We could also
589 * define option callback handlers, but this is easier.
590 */
Jens Axboee1f36502006-10-27 10:54:08 +0200591static void fixup_options(struct thread_data *td)
592{
Jens Axboee1f36502006-10-27 10:54:08 +0200593 if (!td->rwmixread && td->rwmixwrite)
594 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200595
Jens Axboe076efc72006-10-27 11:24:25 +0200596 if (td->write_iolog_file && td->read_iolog_file) {
597 log_err("fio: read iolog overrides write_iolog\n");
598 free(td->write_iolog_file);
599 td->write_iolog_file = NULL;
600 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100601
602 if (td->io_ops->flags & FIO_SYNCIO)
603 td->iodepth = 1;
604 else {
605 if (!td->iodepth)
606 td->iodepth = td->nr_files;
607 }
608
609 /*
610 * only really works for sequential io for now, and with 1 file
611 */
612 if (td->zone_size && !td->sequential && td->nr_files == 1)
613 td->zone_size = 0;
614
615 /*
616 * Reads can do overwrites, we always need to pre-create the file
617 */
618 if (td_read(td) || td_rw(td))
619 td->overwrite = 1;
620
Jens Axboea00735e2006-11-03 08:58:08 +0100621 if (!td->min_bs[DDIR_READ])
622 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
623 if (!td->max_bs[DDIR_READ])
624 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
625 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100626 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100627 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100628 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100629
630 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
631
Jens Axboe16b462a2006-10-30 12:35:18 +0100632 if (td_read(td) && !td_rw(td))
633 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100634
635 if (td->norandommap && td->verify != VERIFY_NONE) {
636 log_err("fio: norandommap given, verify disabled\n");
637 td->verify = VERIFY_NONE;
638 }
Jens Axboe690adba2006-10-30 15:25:09 +0100639 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
640 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100641
642 /*
643 * O_DIRECT and char doesn't mix, clear that flag if necessary.
644 */
645 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
646 td->odirect = 0;
Jens Axboe48097d52007-02-17 06:30:44 +0100647
648 /*
649 * thinktime_spin must be less than thinktime
650 */
651 if (td->thinktime_spin > td->thinktime)
652 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100653
654 /*
655 * The low water mark cannot be bigger than the iodepth
656 */
657 if (td->iodepth_low > td->iodepth || !td->iodepth_low)
658 td->iodepth_low = td->iodepth;
Jens Axboee1f36502006-10-27 10:54:08 +0200659}
660
Jens Axboe906c8d72006-06-13 09:37:56 +0200661/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100662 * This function leaks the buffer
663 */
664static char *to_kmg(unsigned int val)
665{
666 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100667 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100668 char *p = post;
669
Jens Axboe245142f2006-11-08 12:49:21 +0100670 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100671 if (val & 1023)
672 break;
673
674 val >>= 10;
675 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100676 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100677
678 snprintf(buf, 31, "%u%c", val, *p);
679 return buf;
680}
681
682/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200683 * Adds a job to the list of things todo. Sanitizes the various options
684 * to make sure we don't have conflicts, and initializes various
685 * members of td.
686 */
Jens Axboe75154842006-06-01 13:56:09 +0200687static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100688{
Jens Axboe3c9b60c2006-11-07 08:36:14 +0100689 const char *ddir_str[] = { "read", "write", "randread", "randwrite",
690 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100691 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200692 int numjobs, ddir, i;
693 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100694
Jens Axboeebac4652005-12-08 15:25:21 +0100695 /*
696 * the def_thread is just for options, it's not a real job
697 */
698 if (td == &def_thread)
699 return 0;
700
Jens Axboeee738492007-01-10 11:23:16 +0100701 assert(td->io_ops);
Jens Axboedf641192006-10-12 07:55:41 +0200702
Jens Axboe690adba2006-10-30 15:25:09 +0100703 if (td->odirect)
704 td->io_ops->flags |= FIO_RAWIO;
705
Jens Axboeebac4652005-12-08 15:25:21 +0100706 td->filetype = FIO_TYPE_FILE;
Jens Axboe890df532007-02-17 01:51:13 +0100707 if (td->filename && !lstat(td->filename, &sb)) {
Jens Axboe0af7b542006-02-17 10:10:12 +0100708 if (S_ISBLK(sb.st_mode))
709 td->filetype = FIO_TYPE_BD;
710 else if (S_ISCHR(sb.st_mode))
711 td->filetype = FIO_TYPE_CHAR;
712 }
Jens Axboeebac4652005-12-08 15:25:21 +0100713
Jens Axboee0a22332006-12-20 12:54:25 +0100714 fixup_options(td);
715
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200716 if (td->filename)
717 td->nr_uniq_files = 1;
718 else
719 td->nr_uniq_files = td->nr_files;
720
721 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200722 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200723 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200724
Jens Axboe16edf252007-02-10 14:59:22 +0100725 if (td->directory && td->directory[0] != '\0') {
726 if (lstat(td->directory, &sb) < 0) {
727 log_err("fio: %s is not a directory\n", td->directory);
Jens Axboee1161c32007-02-22 19:36:48 +0100728 td_verror(td, errno, "lstat");
Jens Axboe16edf252007-02-10 14:59:22 +0100729 return 1;
730 }
731 if (!S_ISDIR(sb.st_mode)) {
732 log_err("fio: %s is not a directory\n", td->directory);
733 return 1;
734 }
Jens Axboe8aeebd52007-01-08 10:47:43 +0100735 len = sprintf(tmp, "%s/", td->directory);
Jens Axboe16edf252007-02-10 14:59:22 +0100736 }
Jens Axboeebac4652005-12-08 15:25:21 +0100737
Jens Axboe53cdc682006-10-18 11:50:58 +0200738 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
739
740 for_each_file(td, f, i) {
741 memset(f, 0, sizeof(*f));
742 f->fd = -1;
743
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200744 if (td->filename)
745 sprintf(tmp + len, "%s", td->filename);
746 else
747 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200748 f->file_name = strdup(tmp);
749 }
750 } else {
751 td->nr_files = 1;
752 td->files = malloc(sizeof(struct fio_file));
753 f = &td->files[0];
754
755 memset(f, 0, sizeof(*f));
756 f->fd = -1;
757 f->file_name = strdup(jobname);
758 }
759
760 for_each_file(td, f, i) {
761 f->file_size = td->total_file_size / td->nr_files;
762 f->file_offset = td->start_offset;
763 }
764
Jens Axboebbfd6b02006-06-07 19:42:54 +0200765 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100766
Jens Axboe079ad092007-02-20 13:58:20 +0100767 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
768 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
769 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100770
Jens Axboeebac4652005-12-08 15:25:21 +0100771 if (td->stonewall && td->thread_number > 1)
772 groupid++;
773
774 td->groupid = groupid;
775
776 if (setup_rate(td))
777 goto err;
778
Jens Axboeec94ec52006-10-20 10:59:19 +0200779 if (td->write_lat_log) {
Jens Axboe079ad092007-02-20 13:58:20 +0100780 setup_log(&td->ts.slat_log);
781 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100782 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200783 if (td->write_bw_log)
Jens Axboe079ad092007-02-20 13:58:20 +0100784 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100785
Jens Axboeb4692822006-10-27 13:43:22 +0200786 if (!td->name)
787 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200788
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200789 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200790
Jens Axboec6ae0a52006-06-12 11:04:44 +0200791 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200792 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200793 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200794 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100795 else {
796 char *c1, *c2, *c3, *c4;
797
798 c1 = to_kmg(td->min_bs[DDIR_READ]);
799 c2 = to_kmg(td->max_bs[DDIR_READ]);
800 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
801 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
802
Jens Axboed56cbab2007-01-11 19:24:55 +0100803 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 +0100804
805 free(c1);
806 free(c2);
807 free(c3);
808 free(c4);
809 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200810 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200811 fprintf(f_out, "...\n");
812 }
Jens Axboeebac4652005-12-08 15:25:21 +0100813
814 /*
815 * recurse add identical jobs, clear numjobs and stonewall options
816 * as they don't apply to sub-jobs
817 */
818 numjobs = td->numjobs;
819 while (--numjobs) {
820 struct thread_data *td_new = get_new_job(0, td);
821
822 if (!td_new)
823 goto err;
824
825 td_new->numjobs = 1;
826 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200827 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100828
Jens Axboe75154842006-06-01 13:56:09 +0200829 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100830 goto err;
831 }
832 return 0;
833err:
834 put_job(td);
835 return -1;
836}
837
Jens Axboe906c8d72006-06-13 09:37:56 +0200838/*
839 * Initialize the various random states we need (random io, block size ranges,
840 * read/write mix, etc).
841 */
Jens Axboeebac4652005-12-08 15:25:21 +0100842int init_random_state(struct thread_data *td)
843{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200844 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200845 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200846 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100847
Jens Axboef48b4672006-10-27 11:30:07 +0200848 if (td->io_ops->flags & FIO_CPUIO)
849 return 0;
850
Jens Axboe1ac267b2006-05-31 20:29:00 +0200851 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100852 if (fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100853 td_verror(td, errno, "open");
Jens Axboeebac4652005-12-08 15:25:21 +0100854 return 1;
855 }
856
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200857 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100858 td_verror(td, EIO, "read");
Jens Axboeebac4652005-12-08 15:25:21 +0100859 close(fd);
860 return 1;
861 }
862
863 close(fd);
864
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200865 os_random_seed(seeds[0], &td->bsrange_state);
866 os_random_seed(seeds[1], &td->verify_state);
867 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100868
869 if (td->sequential)
870 return 0;
871
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200872 if (td->rand_repeatable)
Jens Axboe2f073b02007-02-17 02:09:01 +0100873 seeds[3] = FIO_RANDSEED * td->thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100874
Jens Axboebb8895e2006-10-30 15:14:48 +0100875 if (!td->norandommap) {
876 for_each_file(td, f, i) {
Jens Axboea4a81712007-02-13 20:07:26 +0100877 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100878 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100879 f->file_map = malloc(num_maps * sizeof(long));
880 f->num_maps = num_maps;
881 memset(f->file_map, 0, num_maps * sizeof(long));
882 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200883 }
Jens Axboeebac4652005-12-08 15:25:21 +0100884
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200885 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100886 return 0;
887}
888
889static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
890{
891#ifdef FIO_HAVE_CPU_AFFINITY
892 unsigned int i;
893
894 CPU_ZERO(&cpumask);
895
896 for (i = 0; i < sizeof(int) * 8; i++) {
897 if ((1 << i) & cpu)
898 CPU_SET(i, &cpumask);
899 }
900#endif
901}
902
Jens Axboeebac4652005-12-08 15:25:21 +0100903static int is_empty_or_comment(char *line)
904{
905 unsigned int i;
906
907 for (i = 0; i < strlen(line); i++) {
908 if (line[i] == ';')
909 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100910 if (line[i] == '#')
911 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100912 if (!isspace(line[i]) && !iscntrl(line[i]))
913 return 0;
914 }
915
916 return 1;
917}
918
Jens Axboeb4692822006-10-27 13:43:22 +0200919static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100920{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200921 struct thread_data *td = data;
922
Jens Axboeebac4652005-12-08 15:25:21 +0100923 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
924 td->ddir = DDIR_READ;
925 td->sequential = 1;
926 return 0;
927 } else if (!strncmp(mem, "randread", 8)) {
928 td->ddir = DDIR_READ;
929 td->sequential = 0;
930 return 0;
931 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
932 td->ddir = DDIR_WRITE;
933 td->sequential = 1;
934 return 0;
935 } else if (!strncmp(mem, "randwrite", 9)) {
936 td->ddir = DDIR_WRITE;
937 td->sequential = 0;
938 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200939 } else if (!strncmp(mem, "rw", 2)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100940 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200941 td->iomix = 1;
942 td->sequential = 1;
943 return 0;
944 } else if (!strncmp(mem, "randrw", 6)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100945 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200946 td->iomix = 1;
947 td->sequential = 0;
948 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100949 }
950
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200951 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100952 return 1;
953}
954
Jens Axboeb4692822006-10-27 13:43:22 +0200955static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100956{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200957 struct thread_data *td = data;
958
Jens Axboeebac4652005-12-08 15:25:21 +0100959 if (!strncmp(mem, "0", 1)) {
960 td->verify = VERIFY_NONE;
961 return 0;
962 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
963 td->verify = VERIFY_MD5;
964 return 0;
965 } else if (!strncmp(mem, "crc32", 5)) {
966 td->verify = VERIFY_CRC32;
967 return 0;
968 }
969
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200970 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100971 return 1;
972}
973
Jens Axboe313cb202006-12-21 09:50:00 +0100974/*
975 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
976 */
977static char *get_mmap_file(const char *str)
978{
979 char *p = strstr(str, ":");
980
981 if (!p)
982 return NULL;
983
984 p++;
985 strip_blank_front(&p);
986 strip_blank_end(p);
987 return strdup(p);
988}
989
Jens Axboeb4692822006-10-27 13:43:22 +0200990static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100991{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200992 struct thread_data *td = data;
993
Jens Axboeebac4652005-12-08 15:25:21 +0100994 if (!strncmp(mem, "malloc", 6)) {
995 td->mem_type = MEM_MALLOC;
996 return 0;
Jens Axboed0bdaf42006-12-20 14:40:44 +0100997 } else if (!strncmp(mem, "mmaphuge", 8)) {
998#ifdef FIO_HAVE_HUGETLB
Jens Axboed0bdaf42006-12-20 14:40:44 +0100999 /*
1000 * mmaphuge must be appended with the actual file
1001 */
Jens Axboe313cb202006-12-21 09:50:00 +01001002 td->mmapfile = get_mmap_file(mem);
1003 if (!td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +01001004 log_err("fio: mmaphuge:/path/to/file\n");
1005 return 1;
1006 }
1007
Jens Axboed0bdaf42006-12-20 14:40:44 +01001008 td->mem_type = MEM_MMAPHUGE;
1009 return 0;
1010#else
1011 log_err("fio: mmaphuge not available\n");
1012 return 1;
1013#endif
Jens Axboeebac4652005-12-08 15:25:21 +01001014 } else if (!strncmp(mem, "mmap", 4)) {
Jens Axboe313cb202006-12-21 09:50:00 +01001015 /*
1016 * Check if the user wants file backed memory. It's ok
1017 * if there's no file given, we'll just use anon mamp then.
1018 */
1019 td->mmapfile = get_mmap_file(mem);
Jens Axboeebac4652005-12-08 15:25:21 +01001020 td->mem_type = MEM_MMAP;
1021 return 0;
Jens Axboe74b025b2006-12-19 15:18:14 +01001022 } else if (!strncmp(mem, "shmhuge", 7)) {
1023#ifdef FIO_HAVE_HUGETLB
1024 td->mem_type = MEM_SHMHUGE;
1025 return 0;
1026#else
1027 log_err("fio: shmhuge not available\n");
1028 return 1;
1029#endif
Jens Axboe0268b8b2006-12-20 12:48:23 +01001030 } else if (!strncmp(mem, "shm", 3)) {
1031 td->mem_type = MEM_SHM;
1032 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001033 }
1034
Jens Axboed0bdaf42006-12-20 14:40:44 +01001035 log_err("fio: mem type: malloc, shm, shmhuge, mmap, mmaphuge\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001036 return 1;
1037}
1038
Jens Axboeb4692822006-10-27 13:43:22 +02001039static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +01001040{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001041 struct thread_data *td = data;
1042
Jens Axboe2866c822006-10-09 15:57:48 +02001043 td->io_ops = load_ioengine(td, str);
1044 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +01001045 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001046
Jens Axboe94cd00d2007-02-22 14:08:36 +01001047 log_err("fio: ioengine= libaio, posixaio, sync, syslet-rw, mmap, sgio, splice, cpu, null\n");
Jens Axboe5f350952006-11-07 15:20:59 +01001048 log_err("fio: or specify path to dynamic ioengine module\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001049 return 1;
1050}
1051
Jens Axboee1f36502006-10-27 10:54:08 +02001052static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1053{
1054 mlock_size = *val;
1055 return 0;
1056}
1057
Jens Axboe34cfcda2006-11-03 14:00:45 +01001058#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001059static int str_prioclass_cb(void *data, unsigned int *val)
1060{
1061 struct thread_data *td = data;
1062
1063 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1064 return 0;
1065}
1066
1067static int str_prio_cb(void *data, unsigned int *val)
1068{
1069 struct thread_data *td = data;
1070
1071 td->ioprio |= *val;
1072 return 0;
1073}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001074#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001075
1076static int str_exitall_cb(void)
1077{
1078 exitall_on_terminate = 1;
1079 return 0;
1080}
1081
1082static int str_cpumask_cb(void *data, unsigned int *val)
1083{
1084 struct thread_data *td = data;
1085
1086 fill_cpu_mask(td->cpumask, *val);
1087 return 0;
1088}
1089
Jens Axboe07261982006-06-07 10:51:12 +02001090/*
1091 * This is our [ini] type file parser.
1092 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001093static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001094{
Jens Axboee1f36502006-10-27 10:54:08 +02001095 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001096 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001097 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001098 fpos_t off;
1099 FILE *f;
1100 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001101 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001102
1103 f = fopen(file, "r");
1104 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001105 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001106 return 1;
1107 }
1108
1109 string = malloc(4096);
1110 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001111 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001112
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001113 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001114 do {
1115 p = fgets(string, 4095, f);
1116 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001117 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001118 if (is_empty_or_comment(p))
1119 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001120 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001121 continue;
1122
1123 global = !strncmp(name, "global", 6);
1124
1125 name[strlen(name) - 1] = '\0';
1126
1127 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001128 if (!td) {
1129 ret = 1;
1130 break;
1131 }
Jens Axboeebac4652005-12-08 15:25:21 +01001132
Jens Axboe972cfd22006-06-09 11:08:56 +02001133 /*
1134 * Seperate multiple job files by a stonewall
1135 */
Jens Axboef9481912006-06-09 11:37:28 +02001136 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001137 td->stonewall = stonewall;
1138 stonewall = 0;
1139 }
1140
Jens Axboeebac4652005-12-08 15:25:21 +01001141 fgetpos(f, &off);
1142 while ((p = fgets(string, 4096, f)) != NULL) {
1143 if (is_empty_or_comment(p))
1144 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001145
Jens Axboeb6754f92006-05-30 14:29:32 +02001146 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001147
1148 if (p[0] == '[')
1149 break;
1150
Jens Axboe4ae3f762006-05-30 13:35:14 +02001151 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001152
Jens Axboee1f36502006-10-27 10:54:08 +02001153 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001154
Jens Axboe45410ac2006-06-07 11:10:39 +02001155 /*
1156 * Don't break here, continue parsing options so we
1157 * dump all the bad ones. Makes trial/error fixups
1158 * easier on the user.
1159 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001160 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001161 }
Jens Axboeebac4652005-12-08 15:25:21 +01001162
Jens Axboe45410ac2006-06-07 11:10:39 +02001163 if (!ret) {
1164 fsetpos(f, &off);
1165 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001166 } else {
1167 log_err("fio: job %s dropped\n", name);
1168 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001169 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001170 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001171
1172 free(string);
1173 free(name);
1174 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001175 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001176}
1177
1178static int fill_def_thread(void)
1179{
1180 memset(&def_thread, 0, sizeof(def_thread));
1181
1182 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1183 perror("sched_getaffinity");
1184 return 1;
1185 }
1186
1187 /*
Jens Axboeee738492007-01-10 11:23:16 +01001188 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001189 */
Jens Axboeee738492007-01-10 11:23:16 +01001190 fill_default_options(&def_thread, options);
1191
Jens Axboe972cfd22006-06-09 11:08:56 +02001192 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001193 def_thread.write_bw_log = write_bw_log;
1194 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001195
Jens Axboeebac4652005-12-08 15:25:21 +01001196#ifdef FIO_HAVE_DISK_UTIL
1197 def_thread.do_disk_util = 1;
1198#endif
1199
1200 return 0;
1201}
1202
Jens Axboe0ab8db82006-10-18 17:16:23 +02001203static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001204{
1205 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001206 printf("\t--output\tWrite output to file\n");
1207 printf("\t--timeout\tRuntime in seconds\n");
1208 printf("\t--latency-log\tGenerate per-job latency logs\n");
1209 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1210 printf("\t--minimal\tMinimal (terse) output\n");
1211 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001212 printf("\t--help\t\tPrint this page\n");
1213 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001214}
1215
Jens Axboe972cfd22006-06-09 11:08:56 +02001216static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001217{
Jens Axboeb4692822006-10-27 13:43:22 +02001218 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001219 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001220
Jens Axboeb4692822006-10-27 13:43:22 +02001221 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001222 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001223 case 't':
1224 def_timeout = atoi(optarg);
1225 break;
1226 case 'l':
1227 write_lat_log = 1;
1228 break;
1229 case 'w':
1230 write_bw_log = 1;
1231 break;
1232 case 'o':
1233 f_out = fopen(optarg, "w+");
1234 if (!f_out) {
1235 perror("fopen output");
1236 exit(1);
1237 }
1238 f_err = f_out;
1239 break;
1240 case 'm':
1241 terse_output = 1;
1242 break;
1243 case 'h':
1244 usage();
1245 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001246 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001247 ret = show_cmd_help(options, optarg);
1248 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001249 case 'v':
1250 printf("%s\n", fio_version_string);
1251 exit(0);
1252 case FIO_GETOPT_JOB: {
1253 const char *opt = long_options[lidx].name;
1254 char *val = optarg;
1255
Jens Axboec2b1e752006-10-30 09:03:13 +01001256 if (!strncmp(opt, "name", 4) && td) {
1257 ret = add_job(td, td->name ?: "fio", 0);
1258 if (ret) {
1259 put_job(td);
1260 return 0;
1261 }
1262 td = NULL;
1263 }
Jens Axboeb4692822006-10-27 13:43:22 +02001264 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001265 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001266
1267 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001268 if (!td)
1269 return 0;
1270 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001271
Jens Axboeb1508cf2006-11-02 09:12:40 +01001272 ret = parse_cmd_option(opt, val, options, td);
1273 if (ret) {
1274 log_err("fio: job dropped\n");
1275 put_job(td);
1276 td = NULL;
1277 }
Jens Axboeb4692822006-10-27 13:43:22 +02001278 break;
1279 }
1280 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001281 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001282 }
1283 }
Jens Axboec9fad892006-05-27 17:26:50 +02001284
Jens Axboeb4692822006-10-27 13:43:22 +02001285 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001286 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001287 if (ret)
1288 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001289 }
Jens Axboe774a6172006-08-24 08:44:26 +02001290
Jens Axboeb4692822006-10-27 13:43:22 +02001291 while (optind < argc) {
1292 ini_idx++;
1293 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1294 ini_file[ini_idx - 1] = strdup(argv[optind]);
1295 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001296 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001297
1298 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001299}
1300
1301static void free_shm(void)
1302{
1303 struct shmid_ds sbuf;
1304
1305 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001306 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001307 threads = NULL;
1308 shmctl(shm_id, IPC_RMID, &sbuf);
1309 }
1310}
1311
Jens Axboe906c8d72006-06-13 09:37:56 +02001312/*
1313 * The thread area is shared between the main process and the job
1314 * threads/processes. So setup a shared memory segment that will hold
1315 * all the job info.
1316 */
Jens Axboeebac4652005-12-08 15:25:21 +01001317static int setup_thread_area(void)
1318{
1319 /*
1320 * 1024 is too much on some machines, scale max_jobs if
1321 * we get a failure that looks like too large a shm segment
1322 */
1323 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001324 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001325
Jens Axboe906c8d72006-06-13 09:37:56 +02001326 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001327 if (shm_id != -1)
1328 break;
1329 if (errno != EINVAL) {
1330 perror("shmget");
1331 break;
1332 }
1333
1334 max_jobs >>= 1;
1335 } while (max_jobs);
1336
1337 if (shm_id == -1)
1338 return 1;
1339
1340 threads = shmat(shm_id, NULL, 0);
1341 if (threads == (void *) -1) {
1342 perror("shmat");
1343 return 1;
1344 }
1345
1346 atexit(free_shm);
1347 return 0;
1348}
1349
Jens Axboeb4692822006-10-27 13:43:22 +02001350/*
1351 * Copy the fio options into the long options map, so we mirror
1352 * job and cmd line options.
1353 */
1354static void dupe_job_options(void)
1355{
1356 struct fio_option *o;
1357 unsigned int i;
1358
1359 i = 0;
1360 while (long_options[i].name)
1361 i++;
1362
1363 o = &options[0];
1364 while (o->name) {
1365 long_options[i].name = o->name;
1366 long_options[i].val = FIO_GETOPT_JOB;
1367 if (o->type == FIO_OPT_STR_SET)
1368 long_options[i].has_arg = no_argument;
1369 else
1370 long_options[i].has_arg = required_argument;
1371
1372 i++;
1373 o++;
1374 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1375 }
1376}
1377
Jens Axboeebac4652005-12-08 15:25:21 +01001378int parse_options(int argc, char *argv[])
1379{
Jens Axboe972cfd22006-06-09 11:08:56 +02001380 int job_files, i;
1381
Jens Axboeb4692822006-10-27 13:43:22 +02001382 f_out = stdout;
1383 f_err = stderr;
1384
Jens Axboe13335dd2007-01-10 13:14:02 +01001385 options_init(options);
1386
Jens Axboeb4692822006-10-27 13:43:22 +02001387 dupe_job_options();
1388
Jens Axboeebac4652005-12-08 15:25:21 +01001389 if (setup_thread_area())
1390 return 1;
1391 if (fill_def_thread())
1392 return 1;
1393
Jens Axboe972cfd22006-06-09 11:08:56 +02001394 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001395
Jens Axboe972cfd22006-06-09 11:08:56 +02001396 for (i = 0; i < job_files; i++) {
1397 if (fill_def_thread())
1398 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001399 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001400 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001401 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001402 }
Jens Axboeebac4652005-12-08 15:25:21 +01001403
Jens Axboe88c6ed82006-06-09 11:28:10 +02001404 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001405
1406 if (!thread_number) {
1407 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001408 return 1;
1409 }
1410
Jens Axboeebac4652005-12-08 15:25:21 +01001411 return 0;
1412}