blob: 6dc221ea4eb31ff95e8796d50aeaf4199845a429 [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 {
94 .name = "size",
95 .type = FIO_OPT_STR_VAL,
96 .off1 = td_var_offset(total_file_size),
97 .help = "Size of device or file",
98 },
99 {
100 .name = "bs",
101 .type = FIO_OPT_STR_VAL_INT,
102 .off1 = td_var_offset(bs[DDIR_READ]),
103 .off2 = td_var_offset(bs[DDIR_WRITE]),
104 .help = "Block size unit",
105 .def = "4k",
106 },
107 {
108 .name = "bsrange",
109 .type = FIO_OPT_RANGE,
110 .off1 = td_var_offset(min_bs[DDIR_READ]),
111 .off2 = td_var_offset(max_bs[DDIR_READ]),
112 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
113 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
114 .help = "Set block size range (in more detail than bs)",
115 },
116 {
117 .name = "bs_unaligned",
118 .type = FIO_OPT_STR_SET,
119 .off1 = td_var_offset(bs_unaligned),
120 .help = "Don't sector align IO buffer sizes",
121 },
122 {
123 .name = "offset",
124 .type = FIO_OPT_STR_VAL,
125 .off1 = td_var_offset(start_offset),
126 .help = "Start IO from this offset",
127 .def = "0",
128 },
129 {
130 .name = "randrepeat",
131 .type = FIO_OPT_BOOL,
132 .off1 = td_var_offset(rand_repeatable),
133 .help = "Use repeatable random IO pattern",
134 .def = "1",
135 },
136 {
137 .name = "norandommap",
138 .type = FIO_OPT_STR_SET,
139 .off1 = td_var_offset(norandommap),
140 .help = "Accept potential duplicate random blocks",
141 },
142 {
143 .name = "nrfiles",
144 .type = FIO_OPT_INT,
145 .off1 = td_var_offset(nr_files),
146 .help = "Split job workload between this number of files",
147 .def = "1",
148 },
149 {
150 .name = "fsync",
151 .type = FIO_OPT_INT,
152 .off1 = td_var_offset(fsync_blocks),
153 .help = "Issue fsync for writes every given number of blocks",
154 .def = "0",
155 },
156 {
157 .name = "direct",
158 .type = FIO_OPT_BOOL,
159 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100160 .help = "Use O_DIRECT IO (negates buffered)",
161 .def = "0",
162 },
163 {
164 .name = "buffered",
165 .type = FIO_OPT_BOOL,
166 .off1 = td_var_offset(odirect),
167 .neg = 1,
168 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100169 .def = "1",
170 },
171 {
172 .name = "overwrite",
173 .type = FIO_OPT_BOOL,
174 .off1 = td_var_offset(overwrite),
175 .help = "When writing, set whether to overwrite current data",
176 .def = "0",
177 },
178 {
179 .name = "loops",
180 .type = FIO_OPT_INT,
181 .off1 = td_var_offset(loops),
182 .help = "Number of times to run the job",
183 .def = "1",
184 },
185 {
186 .name = "numjobs",
187 .type = FIO_OPT_INT,
188 .off1 = td_var_offset(numjobs),
189 .help = "Duplicate this job this many times",
190 .def = "1",
191 },
192 {
193 .name = "startdelay",
194 .type = FIO_OPT_INT,
195 .off1 = td_var_offset(start_delay),
196 .help = "Only start job when this period has passed",
197 .def = "0",
198 },
199 {
200 .name = "runtime",
201 .alias = "timeout",
202 .type = FIO_OPT_STR_VAL_TIME,
203 .off1 = td_var_offset(timeout),
204 .help = "Stop workload when this amount of time has passed",
205 .def = "0",
206 },
207 {
Jens Axboee1f36502006-10-27 10:54:08 +0200208 .name = "mem",
209 .type = FIO_OPT_STR,
210 .cb = str_mem_cb,
Jens Axboe15f79182007-01-10 12:26:18 +0100211 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100212 .def = "malloc",
Jens Axboe15f79182007-01-10 12:26:18 +0100213 .posval = { "malloc", "shm", "shmhuge", "mmap", "mmaphuge", },
Jens Axboee1f36502006-10-27 10:54:08 +0200214 },
215 {
216 .name = "verify",
217 .type = FIO_OPT_STR,
218 .cb = str_verify_cb,
Jens Axboe15f79182007-01-10 12:26:18 +0100219 .help = "Verify sum function",
Jens Axboeee738492007-01-10 11:23:16 +0100220 .def = "0",
Jens Axboe15f79182007-01-10 12:26:18 +0100221 .posval = { "crc32", "md5", },
Jens Axboee1f36502006-10-27 10:54:08 +0200222 },
223 {
224 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200225 .type = FIO_OPT_STR_STORE,
226 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100227 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200228 },
229 {
Jens Axboe076efc72006-10-27 11:24:25 +0200230 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200231 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200232 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100233 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200234 },
235 {
236 .name = "exec_prerun",
237 .type = FIO_OPT_STR_STORE,
238 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100239 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200240 },
241 {
242 .name = "exec_postrun",
243 .type = FIO_OPT_STR_STORE,
244 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100245 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200246 },
247#ifdef FIO_HAVE_IOSCHED_SWITCH
248 {
249 .name = "ioscheduler",
250 .type = FIO_OPT_STR_STORE,
251 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100252 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200253 },
254#endif
255 {
Jens Axboee1f36502006-10-27 10:54:08 +0200256 .name = "zonesize",
257 .type = FIO_OPT_STR_VAL,
258 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100259 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100260 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200261 },
262 {
263 .name = "zoneskip",
264 .type = FIO_OPT_STR_VAL,
265 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100266 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100267 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200268 },
269 {
270 .name = "lockmem",
271 .type = FIO_OPT_STR_VAL,
272 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100273 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100274 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200275 },
276 {
Jens Axboee1f36502006-10-27 10:54:08 +0200277 .name = "rwmixcycle",
278 .type = FIO_OPT_INT,
279 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100280 .help = "Cycle period for mixed read/write workloads (msec)",
281 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200282 },
283 {
284 .name = "rwmixread",
285 .type = FIO_OPT_INT,
286 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100287 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100288 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100289 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200290 },
291 {
292 .name = "rwmixwrite",
293 .type = FIO_OPT_INT,
294 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100295 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100296 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100297 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200298 },
299 {
300 .name = "nice",
301 .type = FIO_OPT_INT,
302 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100303 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100304 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100305 .maxval = 20,
306 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200307 },
308#ifdef FIO_HAVE_IOPRIO
309 {
310 .name = "prio",
311 .type = FIO_OPT_INT,
312 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100313 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100314 .minval = 0,
315 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200316 },
317 {
318 .name = "prioclass",
319 .type = FIO_OPT_INT,
320 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100321 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100322 .minval = 0,
323 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200324 },
325#endif
326 {
327 .name = "thinktime",
328 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100329 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100330 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100331 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200332 },
333 {
Jens Axboe48097d52007-02-17 06:30:44 +0100334 .name = "thinktime_spin",
335 .type = FIO_OPT_INT,
336 .off1 = td_var_offset(thinktime_spin),
337 .help = "Start thinktime by spinning this amount (usec)",
338 .def = "0",
339 },
340 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100341 .name = "thinktime_blocks",
342 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100343 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100344 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100345 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100346 },
347 {
Jens Axboee1f36502006-10-27 10:54:08 +0200348 .name = "rate",
349 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100350 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100351 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200352 },
353 {
354 .name = "ratemin",
355 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100356 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100357 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200358 },
359 {
360 .name = "ratecycle",
361 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100362 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100363 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100364 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200365 },
366 {
Jens Axboee1f36502006-10-27 10:54:08 +0200367 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100368 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100369 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100370 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100371 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200372 },
373 {
374 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100375 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100376 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100377 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100378 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200379 },
380 {
381 .name = "bwavgtime",
382 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100383 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100384 .help = "Time window over which to calculate bandwidth (msec)",
385 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200386 },
387 {
388 .name = "create_serialize",
Jens Axboe13335dd2007-01-10 13:14:02 +0100389 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100390 .off1 = td_var_offset(create_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100391 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100392 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200393 },
394 {
395 .name = "create_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100396 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100397 .off1 = td_var_offset(create_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100398 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100399 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200400 },
401 {
Jens Axboee1f36502006-10-27 10:54:08 +0200402 .name = "cpuload",
403 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100404 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100405 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200406 },
407 {
408 .name = "cpuchunks",
409 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100410 .off1 = td_var_offset(cpucycle),
Jens Axboefd28ca42007-01-09 21:20:13 +0100411 .help = "Length of the CPU burn cycles",
Jens Axboee1f36502006-10-27 10:54:08 +0200412 },
Jens Axboee1f36502006-10-27 10:54:08 +0200413#ifdef FIO_HAVE_CPU_AFFINITY
414 {
415 .name = "cpumask",
416 .type = FIO_OPT_INT,
417 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100418 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200419 },
420#endif
421 {
422 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100423 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100424 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100425 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100426 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200427 },
428 {
429 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100430 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200431 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100432 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100433 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200434 },
435 {
436 .name = "exitall",
437 .type = FIO_OPT_STR_SET,
438 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100439 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200440 },
441 {
442 .name = "stonewall",
443 .type = FIO_OPT_STR_SET,
444 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100445 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200446 },
447 {
448 .name = "thread",
449 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100450 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100451 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200452 },
453 {
454 .name = "write_bw_log",
455 .type = FIO_OPT_STR_SET,
456 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100457 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200458 },
459 {
460 .name = "write_lat_log",
461 .type = FIO_OPT_STR_SET,
462 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100463 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200464 },
465 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100466 .name = "hugepage-size",
467 .type = FIO_OPT_STR_VAL,
468 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100469 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100470 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100471 },
472 {
Jens Axboee1f36502006-10-27 10:54:08 +0200473 .name = NULL,
474 },
475};
476
Jens Axboeb4692822006-10-27 13:43:22 +0200477#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
478#define FIO_CMD_OPTS (16)
479#define FIO_GETOPT_JOB (0x89988998)
480
481/*
482 * Command line options. These will contain the above, plus a few
483 * extra that only pertain to fio itself and not jobs.
484 */
485static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
486 {
487 .name = "output",
488 .has_arg = required_argument,
489 .val = 'o',
490 },
491 {
492 .name = "timeout",
493 .has_arg = required_argument,
494 .val = 't',
495 },
496 {
497 .name = "latency-log",
498 .has_arg = required_argument,
499 .val = 'l',
500 },
501 {
502 .name = "bandwidth-log",
503 .has_arg = required_argument,
504 .val = 'b',
505 },
506 {
507 .name = "minimal",
508 .has_arg = optional_argument,
509 .val = 'm',
510 },
511 {
512 .name = "version",
513 .has_arg = no_argument,
514 .val = 'v',
515 },
516 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100517 .name = "help",
518 .has_arg = no_argument,
519 .val = 'h',
520 },
521 {
522 .name = "cmdhelp",
523 .has_arg = required_argument,
524 .val = 'c',
525 },
526 {
Jens Axboeb4692822006-10-27 13:43:22 +0200527 .name = NULL,
528 },
529};
530
Jens Axboeee738492007-01-10 11:23:16 +0100531static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200532
Jens Axboe5cd033b2007-01-09 08:19:01 +0100533static char fio_version_string[] = "fio 1.11";
Jens Axboeebac4652005-12-08 15:25:21 +0100534
Jens Axboe972cfd22006-06-09 11:08:56 +0200535static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100536static int max_jobs = MAX_JOBS;
537
538struct thread_data def_thread;
539struct thread_data *threads = NULL;
540
Jens Axboeebac4652005-12-08 15:25:21 +0100541int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200542int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200543unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200544FILE *f_out = NULL;
545FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100546
Jens Axboeee738492007-01-10 11:23:16 +0100547static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100548int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200549
Jens Axboe906c8d72006-06-13 09:37:56 +0200550/*
551 * Return a free job structure.
552 */
Jens Axboeebac4652005-12-08 15:25:21 +0100553static struct thread_data *get_new_job(int global, struct thread_data *parent)
554{
555 struct thread_data *td;
556
557 if (global)
558 return &def_thread;
559 if (thread_number >= max_jobs)
560 return NULL;
561
562 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200563 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100564
Jens Axboeebac4652005-12-08 15:25:21 +0100565 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100566 return td;
567}
568
569static void put_job(struct thread_data *td)
570{
Jens Axboe549577a2006-10-30 12:23:41 +0100571 if (td == &def_thread)
572 return;
573
Jens Axboe16edf252007-02-10 14:59:22 +0100574 if (td->error)
575 fprintf(f_out, "fio: %s\n", td->verror);
576
Jens Axboeebac4652005-12-08 15:25:21 +0100577 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
578 thread_number--;
579}
580
Jens Axboedad915e2006-10-27 11:10:18 +0200581/*
582 * Lazy way of fixing up options that depend on each other. We could also
583 * define option callback handlers, but this is easier.
584 */
Jens Axboee1f36502006-10-27 10:54:08 +0200585static void fixup_options(struct thread_data *td)
586{
Jens Axboee1f36502006-10-27 10:54:08 +0200587 if (!td->rwmixread && td->rwmixwrite)
588 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200589
Jens Axboe076efc72006-10-27 11:24:25 +0200590 if (td->write_iolog_file && td->read_iolog_file) {
591 log_err("fio: read iolog overrides write_iolog\n");
592 free(td->write_iolog_file);
593 td->write_iolog_file = NULL;
594 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100595
596 if (td->io_ops->flags & FIO_SYNCIO)
597 td->iodepth = 1;
598 else {
599 if (!td->iodepth)
600 td->iodepth = td->nr_files;
601 }
602
603 /*
604 * only really works for sequential io for now, and with 1 file
605 */
606 if (td->zone_size && !td->sequential && td->nr_files == 1)
607 td->zone_size = 0;
608
609 /*
610 * Reads can do overwrites, we always need to pre-create the file
611 */
612 if (td_read(td) || td_rw(td))
613 td->overwrite = 1;
614
Jens Axboea00735e2006-11-03 08:58:08 +0100615 if (!td->min_bs[DDIR_READ])
616 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
617 if (!td->max_bs[DDIR_READ])
618 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
619 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100620 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100621 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100622 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100623
624 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
625
Jens Axboe16b462a2006-10-30 12:35:18 +0100626 if (td_read(td) && !td_rw(td))
627 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100628
629 if (td->norandommap && td->verify != VERIFY_NONE) {
630 log_err("fio: norandommap given, verify disabled\n");
631 td->verify = VERIFY_NONE;
632 }
Jens Axboe690adba2006-10-30 15:25:09 +0100633 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
634 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100635
636 /*
637 * O_DIRECT and char doesn't mix, clear that flag if necessary.
638 */
639 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
640 td->odirect = 0;
Jens Axboe48097d52007-02-17 06:30:44 +0100641
642 /*
643 * thinktime_spin must be less than thinktime
644 */
645 if (td->thinktime_spin > td->thinktime)
646 td->thinktime_spin = td->thinktime;
Jens Axboee1f36502006-10-27 10:54:08 +0200647}
648
Jens Axboe906c8d72006-06-13 09:37:56 +0200649/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100650 * This function leaks the buffer
651 */
652static char *to_kmg(unsigned int val)
653{
654 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100655 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100656 char *p = post;
657
Jens Axboe245142f2006-11-08 12:49:21 +0100658 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100659 if (val & 1023)
660 break;
661
662 val >>= 10;
663 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100664 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100665
666 snprintf(buf, 31, "%u%c", val, *p);
667 return buf;
668}
669
670/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200671 * Adds a job to the list of things todo. Sanitizes the various options
672 * to make sure we don't have conflicts, and initializes various
673 * members of td.
674 */
Jens Axboe75154842006-06-01 13:56:09 +0200675static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100676{
Jens Axboe3c9b60c2006-11-07 08:36:14 +0100677 const char *ddir_str[] = { "read", "write", "randread", "randwrite",
678 "rw", NULL, "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100679 struct stat sb;
Jens Axboe53cdc682006-10-18 11:50:58 +0200680 int numjobs, ddir, i;
681 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100682
Jens Axboeebac4652005-12-08 15:25:21 +0100683 /*
684 * the def_thread is just for options, it's not a real job
685 */
686 if (td == &def_thread)
687 return 0;
688
Jens Axboeee738492007-01-10 11:23:16 +0100689 assert(td->io_ops);
Jens Axboedf641192006-10-12 07:55:41 +0200690
Jens Axboe690adba2006-10-30 15:25:09 +0100691 if (td->odirect)
692 td->io_ops->flags |= FIO_RAWIO;
693
Jens Axboeebac4652005-12-08 15:25:21 +0100694 td->filetype = FIO_TYPE_FILE;
Jens Axboe890df532007-02-17 01:51:13 +0100695 if (td->filename && !lstat(td->filename, &sb)) {
Jens Axboe0af7b542006-02-17 10:10:12 +0100696 if (S_ISBLK(sb.st_mode))
697 td->filetype = FIO_TYPE_BD;
698 else if (S_ISCHR(sb.st_mode))
699 td->filetype = FIO_TYPE_CHAR;
700 }
Jens Axboeebac4652005-12-08 15:25:21 +0100701
Jens Axboee0a22332006-12-20 12:54:25 +0100702 fixup_options(td);
703
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200704 if (td->filename)
705 td->nr_uniq_files = 1;
706 else
707 td->nr_uniq_files = td->nr_files;
708
709 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200710 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200711 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200712
Jens Axboe16edf252007-02-10 14:59:22 +0100713 if (td->directory && td->directory[0] != '\0') {
714 if (lstat(td->directory, &sb) < 0) {
715 log_err("fio: %s is not a directory\n", td->directory);
716 td_verror(td, errno);
717 return 1;
718 }
719 if (!S_ISDIR(sb.st_mode)) {
720 log_err("fio: %s is not a directory\n", td->directory);
721 return 1;
722 }
Jens Axboe8aeebd52007-01-08 10:47:43 +0100723 len = sprintf(tmp, "%s/", td->directory);
Jens Axboe16edf252007-02-10 14:59:22 +0100724 }
Jens Axboeebac4652005-12-08 15:25:21 +0100725
Jens Axboe53cdc682006-10-18 11:50:58 +0200726 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
727
728 for_each_file(td, f, i) {
729 memset(f, 0, sizeof(*f));
730 f->fd = -1;
731
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200732 if (td->filename)
733 sprintf(tmp + len, "%s", td->filename);
734 else
735 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200736 f->file_name = strdup(tmp);
737 }
738 } else {
739 td->nr_files = 1;
740 td->files = malloc(sizeof(struct fio_file));
741 f = &td->files[0];
742
743 memset(f, 0, sizeof(*f));
744 f->fd = -1;
745 f->file_name = strdup(jobname);
746 }
747
748 for_each_file(td, f, i) {
749 f->file_size = td->total_file_size / td->nr_files;
750 f->file_offset = td->start_offset;
751 }
752
Jens Axboebbfd6b02006-06-07 19:42:54 +0200753 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100754
Jens Axboe079ad092007-02-20 13:58:20 +0100755 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
756 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
757 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100758
Jens Axboeebac4652005-12-08 15:25:21 +0100759 if (td->stonewall && td->thread_number > 1)
760 groupid++;
761
762 td->groupid = groupid;
763
764 if (setup_rate(td))
765 goto err;
766
Jens Axboeec94ec52006-10-20 10:59:19 +0200767 if (td->write_lat_log) {
Jens Axboe079ad092007-02-20 13:58:20 +0100768 setup_log(&td->ts.slat_log);
769 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100770 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200771 if (td->write_bw_log)
Jens Axboe079ad092007-02-20 13:58:20 +0100772 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100773
Jens Axboeb4692822006-10-27 13:43:22 +0200774 if (!td->name)
775 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200776
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200777 ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2);
Jens Axboe75154842006-06-01 13:56:09 +0200778
Jens Axboec6ae0a52006-06-12 11:04:44 +0200779 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200780 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200781 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200782 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100783 else {
784 char *c1, *c2, *c3, *c4;
785
786 c1 = to_kmg(td->min_bs[DDIR_READ]);
787 c2 = to_kmg(td->max_bs[DDIR_READ]);
788 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
789 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
790
Jens Axboed56cbab2007-01-11 19:24:55 +0100791 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 +0100792
793 free(c1);
794 free(c2);
795 free(c3);
796 free(c4);
797 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200798 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200799 fprintf(f_out, "...\n");
800 }
Jens Axboeebac4652005-12-08 15:25:21 +0100801
802 /*
803 * recurse add identical jobs, clear numjobs and stonewall options
804 * as they don't apply to sub-jobs
805 */
806 numjobs = td->numjobs;
807 while (--numjobs) {
808 struct thread_data *td_new = get_new_job(0, td);
809
810 if (!td_new)
811 goto err;
812
813 td_new->numjobs = 1;
814 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200815 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100816
Jens Axboe75154842006-06-01 13:56:09 +0200817 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100818 goto err;
819 }
820 return 0;
821err:
822 put_job(td);
823 return -1;
824}
825
Jens Axboe906c8d72006-06-13 09:37:56 +0200826/*
827 * Initialize the various random states we need (random io, block size ranges,
828 * read/write mix, etc).
829 */
Jens Axboeebac4652005-12-08 15:25:21 +0100830int init_random_state(struct thread_data *td)
831{
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200832 unsigned long seeds[4];
Jens Axboe53cdc682006-10-18 11:50:58 +0200833 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200834 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100835
Jens Axboef48b4672006-10-27 11:30:07 +0200836 if (td->io_ops->flags & FIO_CPUIO)
837 return 0;
838
Jens Axboe1ac267b2006-05-31 20:29:00 +0200839 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100840 if (fd == -1) {
841 td_verror(td, errno);
842 return 1;
843 }
844
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200845 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboeebac4652005-12-08 15:25:21 +0100846 td_verror(td, EIO);
847 close(fd);
848 return 1;
849 }
850
851 close(fd);
852
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200853 os_random_seed(seeds[0], &td->bsrange_state);
854 os_random_seed(seeds[1], &td->verify_state);
855 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100856
857 if (td->sequential)
858 return 0;
859
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200860 if (td->rand_repeatable)
Jens Axboe2f073b02007-02-17 02:09:01 +0100861 seeds[3] = FIO_RANDSEED * td->thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100862
Jens Axboebb8895e2006-10-30 15:14:48 +0100863 if (!td->norandommap) {
864 for_each_file(td, f, i) {
Jens Axboea4a81712007-02-13 20:07:26 +0100865 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100866 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100867 f->file_map = malloc(num_maps * sizeof(long));
868 f->num_maps = num_maps;
869 memset(f->file_map, 0, num_maps * sizeof(long));
870 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200871 }
Jens Axboeebac4652005-12-08 15:25:21 +0100872
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200873 os_random_seed(seeds[3], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100874 return 0;
875}
876
877static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
878{
879#ifdef FIO_HAVE_CPU_AFFINITY
880 unsigned int i;
881
882 CPU_ZERO(&cpumask);
883
884 for (i = 0; i < sizeof(int) * 8; i++) {
885 if ((1 << i) & cpu)
886 CPU_SET(i, &cpumask);
887 }
888#endif
889}
890
Jens Axboeebac4652005-12-08 15:25:21 +0100891static int is_empty_or_comment(char *line)
892{
893 unsigned int i;
894
895 for (i = 0; i < strlen(line); i++) {
896 if (line[i] == ';')
897 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100898 if (line[i] == '#')
899 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100900 if (!isspace(line[i]) && !iscntrl(line[i]))
901 return 0;
902 }
903
904 return 1;
905}
906
Jens Axboeb4692822006-10-27 13:43:22 +0200907static int str_rw_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100908{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200909 struct thread_data *td = data;
910
Jens Axboeebac4652005-12-08 15:25:21 +0100911 if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) {
912 td->ddir = DDIR_READ;
913 td->sequential = 1;
914 return 0;
915 } else if (!strncmp(mem, "randread", 8)) {
916 td->ddir = DDIR_READ;
917 td->sequential = 0;
918 return 0;
919 } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) {
920 td->ddir = DDIR_WRITE;
921 td->sequential = 1;
922 return 0;
923 } else if (!strncmp(mem, "randwrite", 9)) {
924 td->ddir = DDIR_WRITE;
925 td->sequential = 0;
926 return 0;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200927 } else if (!strncmp(mem, "rw", 2)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100928 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200929 td->iomix = 1;
930 td->sequential = 1;
931 return 0;
932 } else if (!strncmp(mem, "randrw", 6)) {
Jens Axboe1e97cce2006-12-05 11:44:16 +0100933 td->ddir = DDIR_READ;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200934 td->iomix = 1;
935 td->sequential = 0;
936 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100937 }
938
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200939 log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100940 return 1;
941}
942
Jens Axboeb4692822006-10-27 13:43:22 +0200943static int str_verify_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100944{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200945 struct thread_data *td = data;
946
Jens Axboeebac4652005-12-08 15:25:21 +0100947 if (!strncmp(mem, "0", 1)) {
948 td->verify = VERIFY_NONE;
949 return 0;
950 } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) {
951 td->verify = VERIFY_MD5;
952 return 0;
953 } else if (!strncmp(mem, "crc32", 5)) {
954 td->verify = VERIFY_CRC32;
955 return 0;
956 }
957
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200958 log_err("fio: verify types: md5, crc32\n");
Jens Axboeebac4652005-12-08 15:25:21 +0100959 return 1;
960}
961
Jens Axboe313cb202006-12-21 09:50:00 +0100962/*
963 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
964 */
965static char *get_mmap_file(const char *str)
966{
967 char *p = strstr(str, ":");
968
969 if (!p)
970 return NULL;
971
972 p++;
973 strip_blank_front(&p);
974 strip_blank_end(p);
975 return strdup(p);
976}
977
Jens Axboeb4692822006-10-27 13:43:22 +0200978static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100979{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200980 struct thread_data *td = data;
981
Jens Axboeebac4652005-12-08 15:25:21 +0100982 if (!strncmp(mem, "malloc", 6)) {
983 td->mem_type = MEM_MALLOC;
984 return 0;
Jens Axboed0bdaf42006-12-20 14:40:44 +0100985 } else if (!strncmp(mem, "mmaphuge", 8)) {
986#ifdef FIO_HAVE_HUGETLB
Jens Axboed0bdaf42006-12-20 14:40:44 +0100987 /*
988 * mmaphuge must be appended with the actual file
989 */
Jens Axboe313cb202006-12-21 09:50:00 +0100990 td->mmapfile = get_mmap_file(mem);
991 if (!td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +0100992 log_err("fio: mmaphuge:/path/to/file\n");
993 return 1;
994 }
995
Jens Axboed0bdaf42006-12-20 14:40:44 +0100996 td->mem_type = MEM_MMAPHUGE;
997 return 0;
998#else
999 log_err("fio: mmaphuge not available\n");
1000 return 1;
1001#endif
Jens Axboeebac4652005-12-08 15:25:21 +01001002 } else if (!strncmp(mem, "mmap", 4)) {
Jens Axboe313cb202006-12-21 09:50:00 +01001003 /*
1004 * Check if the user wants file backed memory. It's ok
1005 * if there's no file given, we'll just use anon mamp then.
1006 */
1007 td->mmapfile = get_mmap_file(mem);
Jens Axboeebac4652005-12-08 15:25:21 +01001008 td->mem_type = MEM_MMAP;
1009 return 0;
Jens Axboe74b025b2006-12-19 15:18:14 +01001010 } else if (!strncmp(mem, "shmhuge", 7)) {
1011#ifdef FIO_HAVE_HUGETLB
1012 td->mem_type = MEM_SHMHUGE;
1013 return 0;
1014#else
1015 log_err("fio: shmhuge not available\n");
1016 return 1;
1017#endif
Jens Axboe0268b8b2006-12-20 12:48:23 +01001018 } else if (!strncmp(mem, "shm", 3)) {
1019 td->mem_type = MEM_SHM;
1020 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001021 }
1022
Jens Axboed0bdaf42006-12-20 14:40:44 +01001023 log_err("fio: mem type: malloc, shm, shmhuge, mmap, mmaphuge\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001024 return 1;
1025}
1026
Jens Axboeb4692822006-10-27 13:43:22 +02001027static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +01001028{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001029 struct thread_data *td = data;
1030
Jens Axboe2866c822006-10-09 15:57:48 +02001031 td->io_ops = load_ioengine(td, str);
1032 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +01001033 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001034
Jens Axboe08aae9a2006-11-24 12:43:49 +01001035 log_err("fio: ioengine= libaio, posixaio, sync, mmap, sgio, splice, cpu, null\n");
Jens Axboe5f350952006-11-07 15:20:59 +01001036 log_err("fio: or specify path to dynamic ioengine module\n");
Jens Axboeebac4652005-12-08 15:25:21 +01001037 return 1;
1038}
1039
Jens Axboee1f36502006-10-27 10:54:08 +02001040static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1041{
1042 mlock_size = *val;
1043 return 0;
1044}
1045
Jens Axboe34cfcda2006-11-03 14:00:45 +01001046#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001047static int str_prioclass_cb(void *data, unsigned int *val)
1048{
1049 struct thread_data *td = data;
1050
1051 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1052 return 0;
1053}
1054
1055static int str_prio_cb(void *data, unsigned int *val)
1056{
1057 struct thread_data *td = data;
1058
1059 td->ioprio |= *val;
1060 return 0;
1061}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001062#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001063
1064static int str_exitall_cb(void)
1065{
1066 exitall_on_terminate = 1;
1067 return 0;
1068}
1069
1070static int str_cpumask_cb(void *data, unsigned int *val)
1071{
1072 struct thread_data *td = data;
1073
1074 fill_cpu_mask(td->cpumask, *val);
1075 return 0;
1076}
1077
Jens Axboe07261982006-06-07 10:51:12 +02001078/*
1079 * This is our [ini] type file parser.
1080 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001081static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001082{
Jens Axboee1f36502006-10-27 10:54:08 +02001083 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001084 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001085 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001086 fpos_t off;
1087 FILE *f;
1088 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001089 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001090
1091 f = fopen(file, "r");
1092 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001093 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001094 return 1;
1095 }
1096
1097 string = malloc(4096);
1098 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001099 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001100
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001101 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001102 do {
1103 p = fgets(string, 4095, f);
1104 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001105 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001106 if (is_empty_or_comment(p))
1107 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001108 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001109 continue;
1110
1111 global = !strncmp(name, "global", 6);
1112
1113 name[strlen(name) - 1] = '\0';
1114
1115 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001116 if (!td) {
1117 ret = 1;
1118 break;
1119 }
Jens Axboeebac4652005-12-08 15:25:21 +01001120
Jens Axboe972cfd22006-06-09 11:08:56 +02001121 /*
1122 * Seperate multiple job files by a stonewall
1123 */
Jens Axboef9481912006-06-09 11:37:28 +02001124 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001125 td->stonewall = stonewall;
1126 stonewall = 0;
1127 }
1128
Jens Axboeebac4652005-12-08 15:25:21 +01001129 fgetpos(f, &off);
1130 while ((p = fgets(string, 4096, f)) != NULL) {
1131 if (is_empty_or_comment(p))
1132 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001133
Jens Axboeb6754f92006-05-30 14:29:32 +02001134 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001135
1136 if (p[0] == '[')
1137 break;
1138
Jens Axboe4ae3f762006-05-30 13:35:14 +02001139 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001140
Jens Axboee1f36502006-10-27 10:54:08 +02001141 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001142
Jens Axboe45410ac2006-06-07 11:10:39 +02001143 /*
1144 * Don't break here, continue parsing options so we
1145 * dump all the bad ones. Makes trial/error fixups
1146 * easier on the user.
1147 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001148 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001149 }
Jens Axboeebac4652005-12-08 15:25:21 +01001150
Jens Axboe45410ac2006-06-07 11:10:39 +02001151 if (!ret) {
1152 fsetpos(f, &off);
1153 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001154 } else {
1155 log_err("fio: job %s dropped\n", name);
1156 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001157 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001158 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001159
1160 free(string);
1161 free(name);
1162 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001163 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001164}
1165
1166static int fill_def_thread(void)
1167{
1168 memset(&def_thread, 0, sizeof(def_thread));
1169
1170 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1171 perror("sched_getaffinity");
1172 return 1;
1173 }
1174
1175 /*
Jens Axboeee738492007-01-10 11:23:16 +01001176 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001177 */
Jens Axboeee738492007-01-10 11:23:16 +01001178 fill_default_options(&def_thread, options);
1179
Jens Axboe972cfd22006-06-09 11:08:56 +02001180 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001181 def_thread.write_bw_log = write_bw_log;
1182 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001183
Jens Axboeebac4652005-12-08 15:25:21 +01001184#ifdef FIO_HAVE_DISK_UTIL
1185 def_thread.do_disk_util = 1;
1186#endif
1187
1188 return 0;
1189}
1190
Jens Axboe0ab8db82006-10-18 17:16:23 +02001191static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001192{
1193 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001194 printf("\t--output\tWrite output to file\n");
1195 printf("\t--timeout\tRuntime in seconds\n");
1196 printf("\t--latency-log\tGenerate per-job latency logs\n");
1197 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1198 printf("\t--minimal\tMinimal (terse) output\n");
1199 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001200 printf("\t--help\t\tPrint this page\n");
1201 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001202}
1203
Jens Axboe972cfd22006-06-09 11:08:56 +02001204static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001205{
Jens Axboeb4692822006-10-27 13:43:22 +02001206 struct thread_data *td = NULL;
Jens Axboec2b1e752006-10-30 09:03:13 +01001207 int c, ini_idx = 0, lidx, ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001208
Jens Axboeb4692822006-10-27 13:43:22 +02001209 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001210 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001211 case 't':
1212 def_timeout = atoi(optarg);
1213 break;
1214 case 'l':
1215 write_lat_log = 1;
1216 break;
1217 case 'w':
1218 write_bw_log = 1;
1219 break;
1220 case 'o':
1221 f_out = fopen(optarg, "w+");
1222 if (!f_out) {
1223 perror("fopen output");
1224 exit(1);
1225 }
1226 f_err = f_out;
1227 break;
1228 case 'm':
1229 terse_output = 1;
1230 break;
1231 case 'h':
1232 usage();
1233 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001234 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001235 ret = show_cmd_help(options, optarg);
1236 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001237 case 'v':
1238 printf("%s\n", fio_version_string);
1239 exit(0);
1240 case FIO_GETOPT_JOB: {
1241 const char *opt = long_options[lidx].name;
1242 char *val = optarg;
1243
Jens Axboec2b1e752006-10-30 09:03:13 +01001244 if (!strncmp(opt, "name", 4) && td) {
1245 ret = add_job(td, td->name ?: "fio", 0);
1246 if (ret) {
1247 put_job(td);
1248 return 0;
1249 }
1250 td = NULL;
1251 }
Jens Axboeb4692822006-10-27 13:43:22 +02001252 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001253 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001254
1255 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001256 if (!td)
1257 return 0;
1258 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001259
Jens Axboeb1508cf2006-11-02 09:12:40 +01001260 ret = parse_cmd_option(opt, val, options, td);
1261 if (ret) {
1262 log_err("fio: job dropped\n");
1263 put_job(td);
1264 td = NULL;
1265 }
Jens Axboeb4692822006-10-27 13:43:22 +02001266 break;
1267 }
1268 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001269 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001270 }
1271 }
Jens Axboec9fad892006-05-27 17:26:50 +02001272
Jens Axboeb4692822006-10-27 13:43:22 +02001273 if (td) {
Jens Axboec2b1e752006-10-30 09:03:13 +01001274 ret = add_job(td, td->name ?: "fio", 0);
Jens Axboeb4692822006-10-27 13:43:22 +02001275 if (ret)
1276 put_job(td);
Jens Axboe972cfd22006-06-09 11:08:56 +02001277 }
Jens Axboe774a6172006-08-24 08:44:26 +02001278
Jens Axboeb4692822006-10-27 13:43:22 +02001279 while (optind < argc) {
1280 ini_idx++;
1281 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1282 ini_file[ini_idx - 1] = strdup(argv[optind]);
1283 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001284 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001285
1286 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001287}
1288
1289static void free_shm(void)
1290{
1291 struct shmid_ds sbuf;
1292
1293 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001294 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001295 threads = NULL;
1296 shmctl(shm_id, IPC_RMID, &sbuf);
1297 }
1298}
1299
Jens Axboe906c8d72006-06-13 09:37:56 +02001300/*
1301 * The thread area is shared between the main process and the job
1302 * threads/processes. So setup a shared memory segment that will hold
1303 * all the job info.
1304 */
Jens Axboeebac4652005-12-08 15:25:21 +01001305static int setup_thread_area(void)
1306{
1307 /*
1308 * 1024 is too much on some machines, scale max_jobs if
1309 * we get a failure that looks like too large a shm segment
1310 */
1311 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001312 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001313
Jens Axboe906c8d72006-06-13 09:37:56 +02001314 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001315 if (shm_id != -1)
1316 break;
1317 if (errno != EINVAL) {
1318 perror("shmget");
1319 break;
1320 }
1321
1322 max_jobs >>= 1;
1323 } while (max_jobs);
1324
1325 if (shm_id == -1)
1326 return 1;
1327
1328 threads = shmat(shm_id, NULL, 0);
1329 if (threads == (void *) -1) {
1330 perror("shmat");
1331 return 1;
1332 }
1333
1334 atexit(free_shm);
1335 return 0;
1336}
1337
Jens Axboeb4692822006-10-27 13:43:22 +02001338/*
1339 * Copy the fio options into the long options map, so we mirror
1340 * job and cmd line options.
1341 */
1342static void dupe_job_options(void)
1343{
1344 struct fio_option *o;
1345 unsigned int i;
1346
1347 i = 0;
1348 while (long_options[i].name)
1349 i++;
1350
1351 o = &options[0];
1352 while (o->name) {
1353 long_options[i].name = o->name;
1354 long_options[i].val = FIO_GETOPT_JOB;
1355 if (o->type == FIO_OPT_STR_SET)
1356 long_options[i].has_arg = no_argument;
1357 else
1358 long_options[i].has_arg = required_argument;
1359
1360 i++;
1361 o++;
1362 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1363 }
1364}
1365
Jens Axboeebac4652005-12-08 15:25:21 +01001366int parse_options(int argc, char *argv[])
1367{
Jens Axboe972cfd22006-06-09 11:08:56 +02001368 int job_files, i;
1369
Jens Axboeb4692822006-10-27 13:43:22 +02001370 f_out = stdout;
1371 f_err = stderr;
1372
Jens Axboe13335dd2007-01-10 13:14:02 +01001373 options_init(options);
1374
Jens Axboeb4692822006-10-27 13:43:22 +02001375 dupe_job_options();
1376
Jens Axboeebac4652005-12-08 15:25:21 +01001377 if (setup_thread_area())
1378 return 1;
1379 if (fill_def_thread())
1380 return 1;
1381
Jens Axboe972cfd22006-06-09 11:08:56 +02001382 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001383
Jens Axboe972cfd22006-06-09 11:08:56 +02001384 for (i = 0; i < job_files; i++) {
1385 if (fill_def_thread())
1386 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001387 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001388 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001389 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001390 }
Jens Axboeebac4652005-12-08 15:25:21 +01001391
Jens Axboe88c6ed82006-06-09 11:28:10 +02001392 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001393
1394 if (!thread_number) {
1395 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001396 return 1;
1397 }
1398
Jens Axboeebac4652005-12-08 15:25:21 +01001399 return 0;
1400}