blob: bbf6f2a8f3bb9b5452855e8ed5ba0c694610383d [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_ioengine_cb(void *, const char *);
26static int str_mem_cb(void *, const char *);
Jens Axboee1f36502006-10-27 10:54:08 +020027static int str_lockmem_cb(void *, unsigned long *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010028#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +020029static int str_prio_cb(void *, unsigned int *);
30static int str_prioclass_cb(void *, unsigned int *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010031#endif
Jens Axboee1f36502006-10-27 10:54:08 +020032static int str_exitall_cb(void);
33static int str_cpumask_cb(void *, unsigned int *);
34
Jens Axboeee738492007-01-10 11:23:16 +010035#define __stringify_1(x) #x
36#define __stringify(x) __stringify_1(x)
37
Jens Axboee1f36502006-10-27 10:54:08 +020038/*
39 * Map of job/command line options
40 */
41static struct fio_option options[] = {
42 {
Jens Axboe61697c32007-02-05 15:04:46 +010043 .name = "description",
44 .type = FIO_OPT_STR_STORE,
45 .off1 = td_var_offset(description),
46 .help = "Text job description",
47 },
48 {
Jens Axboee1f36502006-10-27 10:54:08 +020049 .name = "name",
50 .type = FIO_OPT_STR_STORE,
51 .off1 = td_var_offset(name),
Jens Axboefd28ca42007-01-09 21:20:13 +010052 .help = "Name of this job",
Jens Axboee1f36502006-10-27 10:54:08 +020053 },
54 {
55 .name = "directory",
56 .type = FIO_OPT_STR_STORE,
57 .off1 = td_var_offset(directory),
Jens Axboefd28ca42007-01-09 21:20:13 +010058 .help = "Directory to store files in",
Jens Axboee1f36502006-10-27 10:54:08 +020059 },
60 {
61 .name = "filename",
62 .type = FIO_OPT_STR_STORE,
63 .off1 = td_var_offset(filename),
Jens Axboefd28ca42007-01-09 21:20:13 +010064 .help = "Force the use of a specific file",
Jens Axboee1f36502006-10-27 10:54:08 +020065 },
66 {
67 .name = "rw",
68 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +010069 .off1 = td_var_offset(td_ddir),
Jens Axboe15f79182007-01-10 12:26:18 +010070 .help = "IO direction",
Jens Axboeee738492007-01-10 11:23:16 +010071 .def = "read",
Jens Axboeb1ec1da2007-02-23 10:29:16 +010072 .posval = {
73 { .ival = "read", .oval = TD_DDIR_READ },
74 { .ival = "write", .oval = TD_DDIR_WRITE },
75 { .ival = "randread", .oval = TD_DDIR_RANDREAD },
76 { .ival = "randwrite", .oval = TD_DDIR_RANDWRITE },
77 { .ival = "rw", .oval = TD_DDIR_RW },
78 { .ival = "randrw", .oval = TD_DDIR_RANDRW },
79 },
Jens Axboee1f36502006-10-27 10:54:08 +020080 },
81 {
82 .name = "ioengine",
83 .type = FIO_OPT_STR,
84 .cb = str_ioengine_cb,
Jens Axboe15f79182007-01-10 12:26:18 +010085 .help = "IO engine to use",
Jens Axboeee738492007-01-10 11:23:16 +010086 .def = "sync",
Jens Axboeb1ec1da2007-02-23 10:29:16 +010087 .posval = {
Jens Axboe10cad1a2007-02-23 11:23:03 +010088 { .ival = "sync", },
89#ifdef FIO_HAVE_LIBAIO
90 { .ival = "libaio", },
91#endif
92#ifdef FIO_HAVE_POSIXAIO
93 { .ival = "posixaio", },
94#endif
95 { .ival = "mmap", },
96#ifdef FIO_HAVE_SPLICE
97 { .ival = "splice", },
98#endif
99#ifdef FIO_HAVE_SGIO
100 { .ival = "sg", },
101#endif
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100102 { .ival = "null", }, { .ival = "net", },
Jens Axboe10cad1a2007-02-23 11:23:03 +0100103#ifdef FIO_HAVE_SYSLET
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100104 { .ival = "syslet-rw", },
Jens Axboe10cad1a2007-02-23 11:23:03 +0100105#endif
Joel Becker7b395ca2007-02-28 11:10:58 +0100106 { .ival = "external", },
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100107 },
Jens Axboee1f36502006-10-27 10:54:08 +0200108 },
109 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100110 .name = "iodepth",
111 .type = FIO_OPT_INT,
112 .off1 = td_var_offset(iodepth),
113 .help = "Amount of IO buffers to keep in flight",
114 .def = "1",
115 },
116 {
Jens Axboecb5ab512007-02-26 12:57:09 +0100117 .name = "iodepth_batch",
118 .type = FIO_OPT_INT,
119 .off1 = td_var_offset(iodepth_batch),
120 .help = "Number of IO to submit in one go",
121 },
122 {
Jens Axboee916b392007-02-20 14:37:26 +0100123 .name = "iodepth_low",
124 .type = FIO_OPT_INT,
125 .off1 = td_var_offset(iodepth_low),
126 .help = "Low water mark for queuing depth",
127 },
128 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100129 .name = "size",
130 .type = FIO_OPT_STR_VAL,
131 .off1 = td_var_offset(total_file_size),
132 .help = "Size of device or file",
133 },
134 {
135 .name = "bs",
136 .type = FIO_OPT_STR_VAL_INT,
137 .off1 = td_var_offset(bs[DDIR_READ]),
138 .off2 = td_var_offset(bs[DDIR_WRITE]),
139 .help = "Block size unit",
140 .def = "4k",
141 },
142 {
143 .name = "bsrange",
144 .type = FIO_OPT_RANGE,
145 .off1 = td_var_offset(min_bs[DDIR_READ]),
146 .off2 = td_var_offset(max_bs[DDIR_READ]),
147 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
148 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
149 .help = "Set block size range (in more detail than bs)",
150 },
151 {
152 .name = "bs_unaligned",
153 .type = FIO_OPT_STR_SET,
154 .off1 = td_var_offset(bs_unaligned),
155 .help = "Don't sector align IO buffer sizes",
156 },
157 {
158 .name = "offset",
159 .type = FIO_OPT_STR_VAL,
160 .off1 = td_var_offset(start_offset),
161 .help = "Start IO from this offset",
162 .def = "0",
163 },
164 {
165 .name = "randrepeat",
166 .type = FIO_OPT_BOOL,
167 .off1 = td_var_offset(rand_repeatable),
168 .help = "Use repeatable random IO pattern",
169 .def = "1",
170 },
171 {
172 .name = "norandommap",
173 .type = FIO_OPT_STR_SET,
174 .off1 = td_var_offset(norandommap),
175 .help = "Accept potential duplicate random blocks",
176 },
177 {
178 .name = "nrfiles",
179 .type = FIO_OPT_INT,
180 .off1 = td_var_offset(nr_files),
181 .help = "Split job workload between this number of files",
182 .def = "1",
183 },
184 {
Jens Axboe0aabe162007-02-23 08:45:55 +0100185 .name = "file_service_type",
186 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100187 .off1 = td_var_offset(file_service_type),
Jens Axboe0aabe162007-02-23 08:45:55 +0100188 .help = "How to select which file to service next",
189 .def = "roundrobin",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100190 .posval = {
191 { .ival = "random", .oval = FIO_FSERVICE_RANDOM },
192 { .ival = "roundrobin", .oval = FIO_FSERVICE_RR },
193 },
Jens Axboe0aabe162007-02-23 08:45:55 +0100194 },
195 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100196 .name = "fsync",
197 .type = FIO_OPT_INT,
198 .off1 = td_var_offset(fsync_blocks),
199 .help = "Issue fsync for writes every given number of blocks",
200 .def = "0",
201 },
202 {
203 .name = "direct",
204 .type = FIO_OPT_BOOL,
205 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100206 .help = "Use O_DIRECT IO (negates buffered)",
207 .def = "0",
208 },
209 {
210 .name = "buffered",
211 .type = FIO_OPT_BOOL,
212 .off1 = td_var_offset(odirect),
213 .neg = 1,
214 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100215 .def = "1",
216 },
217 {
218 .name = "overwrite",
219 .type = FIO_OPT_BOOL,
220 .off1 = td_var_offset(overwrite),
221 .help = "When writing, set whether to overwrite current data",
222 .def = "0",
223 },
224 {
225 .name = "loops",
226 .type = FIO_OPT_INT,
227 .off1 = td_var_offset(loops),
228 .help = "Number of times to run the job",
229 .def = "1",
230 },
231 {
232 .name = "numjobs",
233 .type = FIO_OPT_INT,
234 .off1 = td_var_offset(numjobs),
235 .help = "Duplicate this job this many times",
236 .def = "1",
237 },
238 {
239 .name = "startdelay",
240 .type = FIO_OPT_INT,
241 .off1 = td_var_offset(start_delay),
242 .help = "Only start job when this period has passed",
243 .def = "0",
244 },
245 {
246 .name = "runtime",
247 .alias = "timeout",
248 .type = FIO_OPT_STR_VAL_TIME,
249 .off1 = td_var_offset(timeout),
250 .help = "Stop workload when this amount of time has passed",
251 .def = "0",
252 },
253 {
Jens Axboee1f36502006-10-27 10:54:08 +0200254 .name = "mem",
255 .type = FIO_OPT_STR,
256 .cb = str_mem_cb,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100257 .off1 = td_var_offset(mem_type),
Jens Axboe15f79182007-01-10 12:26:18 +0100258 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100259 .def = "malloc",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100260 .posval = {
261 { .ival = "malloc", .oval = MEM_MALLOC },
262 { .ival = "shm", .oval = MEM_SHM },
263#ifdef FIO_HAVE_HUGETLB
264 { .ival = "shmhuge", .oval = MEM_SHMHUGE },
265#endif
266 { .ival = "mmap", .oval = MEM_MMAP },
267#ifdef FIO_HAVE_HUGETLB
268 { .ival = "mmaphuge", .oval = MEM_MMAPHUGE },
269#endif
270 },
Jens Axboee1f36502006-10-27 10:54:08 +0200271 },
272 {
273 .name = "verify",
274 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100275 .off1 = td_var_offset(verify),
Jens Axboe15f79182007-01-10 12:26:18 +0100276 .help = "Verify sum function",
Jens Axboeee738492007-01-10 11:23:16 +0100277 .def = "0",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100278 .posval = {
279 { .ival = "0", .oval = VERIFY_NONE },
280 { .ival = "crc32", .oval = VERIFY_CRC32 },
281 { .ival = "md5", .oval = VERIFY_MD5 },
282 },
Jens Axboee1f36502006-10-27 10:54:08 +0200283 },
284 {
285 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200286 .type = FIO_OPT_STR_STORE,
287 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100288 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200289 },
290 {
Jens Axboe076efc72006-10-27 11:24:25 +0200291 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200292 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200293 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100294 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200295 },
296 {
297 .name = "exec_prerun",
298 .type = FIO_OPT_STR_STORE,
299 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100300 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200301 },
302 {
303 .name = "exec_postrun",
304 .type = FIO_OPT_STR_STORE,
305 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100306 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200307 },
308#ifdef FIO_HAVE_IOSCHED_SWITCH
309 {
310 .name = "ioscheduler",
311 .type = FIO_OPT_STR_STORE,
312 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100313 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200314 },
315#endif
316 {
Jens Axboee1f36502006-10-27 10:54:08 +0200317 .name = "zonesize",
318 .type = FIO_OPT_STR_VAL,
319 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100320 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100321 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200322 },
323 {
324 .name = "zoneskip",
325 .type = FIO_OPT_STR_VAL,
326 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100327 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100328 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200329 },
330 {
331 .name = "lockmem",
332 .type = FIO_OPT_STR_VAL,
333 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100334 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100335 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200336 },
337 {
Jens Axboee1f36502006-10-27 10:54:08 +0200338 .name = "rwmixcycle",
339 .type = FIO_OPT_INT,
340 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100341 .help = "Cycle period for mixed read/write workloads (msec)",
342 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200343 },
344 {
345 .name = "rwmixread",
346 .type = FIO_OPT_INT,
347 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100348 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100349 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100350 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200351 },
352 {
353 .name = "rwmixwrite",
354 .type = FIO_OPT_INT,
355 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100356 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100357 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100358 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200359 },
360 {
361 .name = "nice",
362 .type = FIO_OPT_INT,
363 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100364 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100365 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100366 .maxval = 20,
367 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200368 },
369#ifdef FIO_HAVE_IOPRIO
370 {
371 .name = "prio",
372 .type = FIO_OPT_INT,
373 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100374 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100375 .minval = 0,
376 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200377 },
378 {
379 .name = "prioclass",
380 .type = FIO_OPT_INT,
381 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100382 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100383 .minval = 0,
384 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200385 },
386#endif
387 {
388 .name = "thinktime",
389 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100390 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100391 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100392 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200393 },
394 {
Jens Axboe48097d52007-02-17 06:30:44 +0100395 .name = "thinktime_spin",
396 .type = FIO_OPT_INT,
397 .off1 = td_var_offset(thinktime_spin),
Jens Axboe34403fb2007-03-02 21:43:25 +0100398 .help = "Start think time by spinning this amount (usec)",
Jens Axboe48097d52007-02-17 06:30:44 +0100399 .def = "0",
400 },
401 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100402 .name = "thinktime_blocks",
403 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100404 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100405 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100406 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100407 },
408 {
Jens Axboee1f36502006-10-27 10:54:08 +0200409 .name = "rate",
410 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100411 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100412 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200413 },
414 {
415 .name = "ratemin",
416 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100417 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100418 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200419 },
420 {
421 .name = "ratecycle",
422 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100423 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100424 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100425 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200426 },
427 {
Jens Axboee1f36502006-10-27 10:54:08 +0200428 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100429 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100430 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100431 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100432 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200433 },
434 {
435 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100436 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100437 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100438 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100439 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200440 },
441 {
442 .name = "bwavgtime",
443 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100444 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100445 .help = "Time window over which to calculate bandwidth (msec)",
446 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200447 },
448 {
449 .name = "create_serialize",
Jens Axboe13335dd2007-01-10 13:14:02 +0100450 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100451 .off1 = td_var_offset(create_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100452 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100453 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200454 },
455 {
456 .name = "create_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100457 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100458 .off1 = td_var_offset(create_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100459 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100460 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200461 },
462 {
Jens Axboee1f36502006-10-27 10:54:08 +0200463 .name = "cpuload",
464 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100465 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100466 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200467 },
468 {
469 .name = "cpuchunks",
470 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100471 .off1 = td_var_offset(cpucycle),
Jens Axboefd28ca42007-01-09 21:20:13 +0100472 .help = "Length of the CPU burn cycles",
Jens Axboee1f36502006-10-27 10:54:08 +0200473 },
Jens Axboee1f36502006-10-27 10:54:08 +0200474#ifdef FIO_HAVE_CPU_AFFINITY
475 {
476 .name = "cpumask",
477 .type = FIO_OPT_INT,
478 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100479 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200480 },
481#endif
482 {
483 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100484 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100485 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100486 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100487 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200488 },
489 {
490 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100491 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200492 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100493 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100494 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200495 },
496 {
497 .name = "exitall",
498 .type = FIO_OPT_STR_SET,
499 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100500 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200501 },
502 {
503 .name = "stonewall",
504 .type = FIO_OPT_STR_SET,
505 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100506 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200507 },
508 {
509 .name = "thread",
510 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100511 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100512 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200513 },
514 {
515 .name = "write_bw_log",
516 .type = FIO_OPT_STR_SET,
517 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100518 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200519 },
520 {
521 .name = "write_lat_log",
522 .type = FIO_OPT_STR_SET,
523 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100524 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200525 },
526 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100527 .name = "hugepage-size",
528 .type = FIO_OPT_STR_VAL,
529 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100530 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100531 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100532 },
533 {
Jens Axboeb2560f32007-03-06 12:43:03 +0100534 .name = "group_reporting",
535 .type = FIO_OPT_STR_SET,
536 .off1 = td_var_offset(group_reporting),
537 .help = "Do reporting on a per-group basis",
538 },
539 {
Jens Axboee1f36502006-10-27 10:54:08 +0200540 .name = NULL,
541 },
542};
543
Jens Axboeb4692822006-10-27 13:43:22 +0200544#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
545#define FIO_CMD_OPTS (16)
546#define FIO_GETOPT_JOB (0x89988998)
547
548/*
549 * Command line options. These will contain the above, plus a few
550 * extra that only pertain to fio itself and not jobs.
551 */
552static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
553 {
554 .name = "output",
555 .has_arg = required_argument,
556 .val = 'o',
557 },
558 {
559 .name = "timeout",
560 .has_arg = required_argument,
561 .val = 't',
562 },
563 {
564 .name = "latency-log",
565 .has_arg = required_argument,
566 .val = 'l',
567 },
568 {
569 .name = "bandwidth-log",
570 .has_arg = required_argument,
571 .val = 'b',
572 },
573 {
574 .name = "minimal",
575 .has_arg = optional_argument,
576 .val = 'm',
577 },
578 {
579 .name = "version",
580 .has_arg = no_argument,
581 .val = 'v',
582 },
583 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100584 .name = "help",
585 .has_arg = no_argument,
586 .val = 'h',
587 },
588 {
589 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100590 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +0100591 .val = 'c',
592 },
593 {
Jens Axboeb4692822006-10-27 13:43:22 +0200594 .name = NULL,
595 },
596};
597
Jens Axboeee738492007-01-10 11:23:16 +0100598static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200599
Jens Axboefdf3de22007-03-03 12:21:34 +0100600static char fio_version_string[] = "fio 1.13";
Jens Axboeebac4652005-12-08 15:25:21 +0100601
Jens Axboe972cfd22006-06-09 11:08:56 +0200602static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100603static int max_jobs = MAX_JOBS;
604
605struct thread_data def_thread;
606struct thread_data *threads = NULL;
607
Jens Axboeebac4652005-12-08 15:25:21 +0100608int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200609int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200610unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200611FILE *f_out = NULL;
612FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100613
Jens Axboeee738492007-01-10 11:23:16 +0100614static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100615int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200616
Joel Becker9728ce32007-03-01 08:24:39 +0100617FILE *get_f_out()
618{
619 return f_out;
620}
621
622FILE *get_f_err()
623{
624 return f_err;
625}
626
Jens Axboe906c8d72006-06-13 09:37:56 +0200627/*
628 * Return a free job structure.
629 */
Jens Axboeebac4652005-12-08 15:25:21 +0100630static struct thread_data *get_new_job(int global, struct thread_data *parent)
631{
632 struct thread_data *td;
633
634 if (global)
635 return &def_thread;
636 if (thread_number >= max_jobs)
637 return NULL;
638
639 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200640 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100641
Jens Axboeebac4652005-12-08 15:25:21 +0100642 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100643 return td;
644}
645
646static void put_job(struct thread_data *td)
647{
Jens Axboe549577a2006-10-30 12:23:41 +0100648 if (td == &def_thread)
649 return;
650
Jens Axboe16edf252007-02-10 14:59:22 +0100651 if (td->error)
652 fprintf(f_out, "fio: %s\n", td->verror);
653
Jens Axboeebac4652005-12-08 15:25:21 +0100654 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
655 thread_number--;
656}
657
Jens Axboedad915e2006-10-27 11:10:18 +0200658/*
659 * Lazy way of fixing up options that depend on each other. We could also
660 * define option callback handlers, but this is easier.
661 */
Jens Axboee1f36502006-10-27 10:54:08 +0200662static void fixup_options(struct thread_data *td)
663{
Jens Axboee1f36502006-10-27 10:54:08 +0200664 if (!td->rwmixread && td->rwmixwrite)
665 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200666
Jens Axboe076efc72006-10-27 11:24:25 +0200667 if (td->write_iolog_file && td->read_iolog_file) {
668 log_err("fio: read iolog overrides write_iolog\n");
669 free(td->write_iolog_file);
670 td->write_iolog_file = NULL;
671 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100672
673 if (td->io_ops->flags & FIO_SYNCIO)
674 td->iodepth = 1;
675 else {
676 if (!td->iodepth)
677 td->iodepth = td->nr_files;
678 }
679
680 /*
681 * only really works for sequential io for now, and with 1 file
682 */
Jens Axboe413dd452007-02-23 09:26:09 +0100683 if (td->zone_size && td_random(td) && td->nr_files == 1)
Jens Axboe16b462a2006-10-30 12:35:18 +0100684 td->zone_size = 0;
685
686 /*
687 * Reads can do overwrites, we always need to pre-create the file
688 */
689 if (td_read(td) || td_rw(td))
690 td->overwrite = 1;
691
Jens Axboea00735e2006-11-03 08:58:08 +0100692 if (!td->min_bs[DDIR_READ])
693 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
694 if (!td->max_bs[DDIR_READ])
695 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
696 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100697 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100698 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100699 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100700
701 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
702
Jens Axboe16b462a2006-10-30 12:35:18 +0100703 if (td_read(td) && !td_rw(td))
704 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100705
706 if (td->norandommap && td->verify != VERIFY_NONE) {
707 log_err("fio: norandommap given, verify disabled\n");
708 td->verify = VERIFY_NONE;
709 }
Jens Axboe690adba2006-10-30 15:25:09 +0100710 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
711 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100712
713 /*
714 * O_DIRECT and char doesn't mix, clear that flag if necessary.
715 */
716 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
717 td->odirect = 0;
Jens Axboe48097d52007-02-17 06:30:44 +0100718
719 /*
720 * thinktime_spin must be less than thinktime
721 */
722 if (td->thinktime_spin > td->thinktime)
723 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100724
725 /*
726 * The low water mark cannot be bigger than the iodepth
727 */
Jens Axboe9467b772007-02-27 19:56:43 +0100728 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
729 /*
730 * syslet work around - if the workload is sequential,
731 * we want to let the queue drain all the way down to
732 * avoid seeking between async threads
733 */
734 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
735 td->iodepth_low = 1;
736 else
737 td->iodepth_low = td->iodepth;
738 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100739
740 /*
741 * If batch number isn't set, default to the same as iodepth
742 */
743 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
744 td->iodepth_batch = td->iodepth;
Jens Axboee1f36502006-10-27 10:54:08 +0200745}
746
Jens Axboe906c8d72006-06-13 09:37:56 +0200747/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100748 * This function leaks the buffer
749 */
750static char *to_kmg(unsigned int val)
751{
752 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100753 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100754 char *p = post;
755
Jens Axboe245142f2006-11-08 12:49:21 +0100756 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100757 if (val & 1023)
758 break;
759
760 val >>= 10;
761 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100762 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100763
764 snprintf(buf, 31, "%u%c", val, *p);
765 return buf;
766}
767
768/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200769 * Adds a job to the list of things todo. Sanitizes the various options
770 * to make sure we don't have conflicts, and initializes various
771 * members of td.
772 */
Jens Axboe75154842006-06-01 13:56:09 +0200773static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100774{
Jens Axboe413dd452007-02-23 09:26:09 +0100775 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
776 "randread", "randwrite", "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100777 struct stat sb;
Jens Axboe413dd452007-02-23 09:26:09 +0100778 int numjobs, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200779 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100780
Jens Axboeebac4652005-12-08 15:25:21 +0100781 /*
782 * the def_thread is just for options, it's not a real job
783 */
784 if (td == &def_thread)
785 return 0;
786
Jens Axboeee738492007-01-10 11:23:16 +0100787 assert(td->io_ops);
Jens Axboedf641192006-10-12 07:55:41 +0200788
Jens Axboe690adba2006-10-30 15:25:09 +0100789 if (td->odirect)
790 td->io_ops->flags |= FIO_RAWIO;
791
Jens Axboeebac4652005-12-08 15:25:21 +0100792 td->filetype = FIO_TYPE_FILE;
Jens Axboe890df532007-02-17 01:51:13 +0100793 if (td->filename && !lstat(td->filename, &sb)) {
Jens Axboe0af7b542006-02-17 10:10:12 +0100794 if (S_ISBLK(sb.st_mode))
795 td->filetype = FIO_TYPE_BD;
796 else if (S_ISCHR(sb.st_mode))
797 td->filetype = FIO_TYPE_CHAR;
798 }
Jens Axboeebac4652005-12-08 15:25:21 +0100799
Jens Axboee0a22332006-12-20 12:54:25 +0100800 fixup_options(td);
801
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200802 if (td->filename)
803 td->nr_uniq_files = 1;
804 else
805 td->nr_uniq_files = td->nr_files;
806
807 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200808 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200809 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200810
Jens Axboe16edf252007-02-10 14:59:22 +0100811 if (td->directory && td->directory[0] != '\0') {
812 if (lstat(td->directory, &sb) < 0) {
813 log_err("fio: %s is not a directory\n", td->directory);
Jens Axboee1161c32007-02-22 19:36:48 +0100814 td_verror(td, errno, "lstat");
Jens Axboe16edf252007-02-10 14:59:22 +0100815 return 1;
816 }
817 if (!S_ISDIR(sb.st_mode)) {
818 log_err("fio: %s is not a directory\n", td->directory);
819 return 1;
820 }
Jens Axboe8aeebd52007-01-08 10:47:43 +0100821 len = sprintf(tmp, "%s/", td->directory);
Jens Axboe16edf252007-02-10 14:59:22 +0100822 }
Jens Axboeebac4652005-12-08 15:25:21 +0100823
Jens Axboe53cdc682006-10-18 11:50:58 +0200824 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
825
826 for_each_file(td, f, i) {
827 memset(f, 0, sizeof(*f));
828 f->fd = -1;
829
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200830 if (td->filename)
831 sprintf(tmp + len, "%s", td->filename);
832 else
833 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200834 f->file_name = strdup(tmp);
835 }
836 } else {
837 td->nr_files = 1;
838 td->files = malloc(sizeof(struct fio_file));
839 f = &td->files[0];
840
841 memset(f, 0, sizeof(*f));
842 f->fd = -1;
843 f->file_name = strdup(jobname);
844 }
845
846 for_each_file(td, f, i) {
847 f->file_size = td->total_file_size / td->nr_files;
848 f->file_offset = td->start_offset;
849 }
850
Jens Axboebbfd6b02006-06-07 19:42:54 +0200851 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100852
Jens Axboe756867b2007-03-06 15:19:24 +0100853 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
854 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
855 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100856
Jens Axboeebac4652005-12-08 15:25:21 +0100857 if (td->stonewall && td->thread_number > 1)
858 groupid++;
859
860 td->groupid = groupid;
861
862 if (setup_rate(td))
863 goto err;
864
Jens Axboeec94ec52006-10-20 10:59:19 +0200865 if (td->write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100866 setup_log(&td->ts.slat_log);
867 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100868 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200869 if (td->write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100870 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100871
Jens Axboeb4692822006-10-27 13:43:22 +0200872 if (!td->name)
873 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200874
Jens Axboec6ae0a52006-06-12 11:04:44 +0200875 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200876 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200877 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200878 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100879 else {
880 char *c1, *c2, *c3, *c4;
881
882 c1 = to_kmg(td->min_bs[DDIR_READ]);
883 c2 = to_kmg(td->max_bs[DDIR_READ]);
884 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
885 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
886
Jens Axboe413dd452007-02-23 09:26:09 +0100887 fprintf(f_out, "%s: (g=%d): rw=%s, bs=%s-%s/%s-%s, ioengine=%s, iodepth=%u\n", td->name, td->groupid, ddir_str[td->td_ddir], c1, c2, c3, c4, td->io_ops->name, td->iodepth);
Jens Axboef8977ee2006-11-06 14:03:03 +0100888
889 free(c1);
890 free(c2);
891 free(c3);
892 free(c4);
893 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200894 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200895 fprintf(f_out, "...\n");
896 }
Jens Axboeebac4652005-12-08 15:25:21 +0100897
898 /*
899 * recurse add identical jobs, clear numjobs and stonewall options
900 * as they don't apply to sub-jobs
901 */
902 numjobs = td->numjobs;
903 while (--numjobs) {
904 struct thread_data *td_new = get_new_job(0, td);
905
906 if (!td_new)
907 goto err;
908
909 td_new->numjobs = 1;
910 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200911 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100912
Jens Axboe75154842006-06-01 13:56:09 +0200913 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100914 goto err;
915 }
916 return 0;
917err:
918 put_job(td);
919 return -1;
920}
921
Jens Axboe906c8d72006-06-13 09:37:56 +0200922/*
923 * Initialize the various random states we need (random io, block size ranges,
924 * read/write mix, etc).
925 */
Jens Axboeebac4652005-12-08 15:25:21 +0100926int init_random_state(struct thread_data *td)
927{
Jens Axboe0aabe162007-02-23 08:45:55 +0100928 unsigned long seeds[5];
Jens Axboe53cdc682006-10-18 11:50:58 +0200929 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200930 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100931
Jens Axboef48b4672006-10-27 11:30:07 +0200932 if (td->io_ops->flags & FIO_CPUIO)
933 return 0;
934
Jens Axboe1ac267b2006-05-31 20:29:00 +0200935 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100936 if (fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100937 td_verror(td, errno, "open");
Jens Axboeebac4652005-12-08 15:25:21 +0100938 return 1;
939 }
940
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200941 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100942 td_verror(td, EIO, "read");
Jens Axboeebac4652005-12-08 15:25:21 +0100943 close(fd);
944 return 1;
945 }
946
947 close(fd);
948
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200949 os_random_seed(seeds[0], &td->bsrange_state);
950 os_random_seed(seeds[1], &td->verify_state);
951 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100952
Jens Axboe413dd452007-02-23 09:26:09 +0100953 if (td->file_service_type == FIO_FSERVICE_RANDOM)
954 os_random_seed(seeds[3], &td->next_file_state);
955
956 if (!td_random(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100957 return 0;
958
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200959 if (td->rand_repeatable)
Jens Axboe0aabe162007-02-23 08:45:55 +0100960 seeds[4] = FIO_RANDSEED * td->thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100961
Jens Axboebb8895e2006-10-30 15:14:48 +0100962 if (!td->norandommap) {
963 for_each_file(td, f, i) {
Jens Axboea4a81712007-02-13 20:07:26 +0100964 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100965 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100966 f->file_map = malloc(num_maps * sizeof(long));
967 f->num_maps = num_maps;
968 memset(f->file_map, 0, num_maps * sizeof(long));
969 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200970 }
Jens Axboeebac4652005-12-08 15:25:21 +0100971
Jens Axboe0aabe162007-02-23 08:45:55 +0100972 os_random_seed(seeds[4], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100973 return 0;
974}
975
976static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
977{
978#ifdef FIO_HAVE_CPU_AFFINITY
979 unsigned int i;
980
981 CPU_ZERO(&cpumask);
982
983 for (i = 0; i < sizeof(int) * 8; i++) {
984 if ((1 << i) & cpu)
985 CPU_SET(i, &cpumask);
986 }
987#endif
988}
989
Jens Axboeebac4652005-12-08 15:25:21 +0100990static int is_empty_or_comment(char *line)
991{
992 unsigned int i;
993
994 for (i = 0; i < strlen(line); i++) {
995 if (line[i] == ';')
996 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100997 if (line[i] == '#')
998 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100999 if (!isspace(line[i]) && !iscntrl(line[i]))
1000 return 0;
1001 }
1002
1003 return 1;
1004}
1005
Jens Axboe313cb202006-12-21 09:50:00 +01001006/*
1007 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1008 */
1009static char *get_mmap_file(const char *str)
1010{
1011 char *p = strstr(str, ":");
1012
1013 if (!p)
1014 return NULL;
1015
1016 p++;
1017 strip_blank_front(&p);
1018 strip_blank_end(p);
1019 return strdup(p);
1020}
1021
Jens Axboeb4692822006-10-27 13:43:22 +02001022static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +01001023{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001024 struct thread_data *td = data;
1025
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001026 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
Jens Axboe313cb202006-12-21 09:50:00 +01001027 td->mmapfile = get_mmap_file(mem);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001028 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +01001029 log_err("fio: mmaphuge:/path/to/file\n");
1030 return 1;
1031 }
Jens Axboeebac4652005-12-08 15:25:21 +01001032 }
1033
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001034 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001035}
1036
Joel Becker7b395ca2007-02-28 11:10:58 +01001037/* External engines are specified by "external:name.o") */
1038static const char *get_engine_name(const char *str)
1039{
1040 char *p = strstr(str, ":");
1041
1042 if (!p)
1043 return str;
1044
1045 p++;
1046 strip_blank_front(&p);
1047 strip_blank_end(p);
1048 return p;
1049}
1050
Jens Axboeb4692822006-10-27 13:43:22 +02001051static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +01001052{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001053 struct thread_data *td = data;
Joel Becker7b395ca2007-02-28 11:10:58 +01001054 const char *name = get_engine_name(str);
Jens Axboecb2c86f2006-10-26 13:48:34 +02001055
Joel Becker7b395ca2007-02-28 11:10:58 +01001056 td->io_ops = load_ioengine(td, name);
Jens Axboe2866c822006-10-09 15:57:48 +02001057 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +01001058 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001059
Jens Axboeebac4652005-12-08 15:25:21 +01001060 return 1;
1061}
1062
Jens Axboee1f36502006-10-27 10:54:08 +02001063static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1064{
1065 mlock_size = *val;
1066 return 0;
1067}
1068
Jens Axboe34cfcda2006-11-03 14:00:45 +01001069#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001070static int str_prioclass_cb(void *data, unsigned int *val)
1071{
1072 struct thread_data *td = data;
1073
1074 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1075 return 0;
1076}
1077
1078static int str_prio_cb(void *data, unsigned int *val)
1079{
1080 struct thread_data *td = data;
1081
1082 td->ioprio |= *val;
1083 return 0;
1084}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001085#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001086
1087static int str_exitall_cb(void)
1088{
1089 exitall_on_terminate = 1;
1090 return 0;
1091}
1092
1093static int str_cpumask_cb(void *data, unsigned int *val)
1094{
1095 struct thread_data *td = data;
1096
1097 fill_cpu_mask(td->cpumask, *val);
1098 return 0;
1099}
1100
Jens Axboe07261982006-06-07 10:51:12 +02001101/*
1102 * This is our [ini] type file parser.
1103 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001104static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001105{
Jens Axboee1f36502006-10-27 10:54:08 +02001106 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001107 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001108 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001109 fpos_t off;
1110 FILE *f;
1111 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001112 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001113
1114 f = fopen(file, "r");
1115 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001116 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001117 return 1;
1118 }
1119
1120 string = malloc(4096);
1121 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001122 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001123
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001124 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001125 do {
1126 p = fgets(string, 4095, f);
1127 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001128 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001129 if (is_empty_or_comment(p))
1130 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001131 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001132 continue;
1133
1134 global = !strncmp(name, "global", 6);
1135
1136 name[strlen(name) - 1] = '\0';
1137
1138 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001139 if (!td) {
1140 ret = 1;
1141 break;
1142 }
Jens Axboeebac4652005-12-08 15:25:21 +01001143
Jens Axboe972cfd22006-06-09 11:08:56 +02001144 /*
1145 * Seperate multiple job files by a stonewall
1146 */
Jens Axboef9481912006-06-09 11:37:28 +02001147 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001148 td->stonewall = stonewall;
1149 stonewall = 0;
1150 }
1151
Jens Axboeebac4652005-12-08 15:25:21 +01001152 fgetpos(f, &off);
1153 while ((p = fgets(string, 4096, f)) != NULL) {
1154 if (is_empty_or_comment(p))
1155 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001156
Jens Axboeb6754f92006-05-30 14:29:32 +02001157 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001158
1159 if (p[0] == '[')
1160 break;
1161
Jens Axboe4ae3f762006-05-30 13:35:14 +02001162 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001163
Jens Axboee1f36502006-10-27 10:54:08 +02001164 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001165
Jens Axboe45410ac2006-06-07 11:10:39 +02001166 /*
1167 * Don't break here, continue parsing options so we
1168 * dump all the bad ones. Makes trial/error fixups
1169 * easier on the user.
1170 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001171 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001172 }
Jens Axboeebac4652005-12-08 15:25:21 +01001173
Jens Axboe45410ac2006-06-07 11:10:39 +02001174 if (!ret) {
1175 fsetpos(f, &off);
1176 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001177 } else {
1178 log_err("fio: job %s dropped\n", name);
1179 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001180 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001181 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001182
1183 free(string);
1184 free(name);
1185 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001186 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001187}
1188
1189static int fill_def_thread(void)
1190{
1191 memset(&def_thread, 0, sizeof(def_thread));
1192
1193 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1194 perror("sched_getaffinity");
1195 return 1;
1196 }
1197
1198 /*
Jens Axboeee738492007-01-10 11:23:16 +01001199 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001200 */
Jens Axboeee738492007-01-10 11:23:16 +01001201 fill_default_options(&def_thread, options);
1202
Jens Axboe972cfd22006-06-09 11:08:56 +02001203 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001204 def_thread.write_bw_log = write_bw_log;
1205 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001206
Jens Axboeebac4652005-12-08 15:25:21 +01001207#ifdef FIO_HAVE_DISK_UTIL
1208 def_thread.do_disk_util = 1;
1209#endif
1210
1211 return 0;
1212}
1213
Jens Axboe0ab8db82006-10-18 17:16:23 +02001214static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001215{
1216 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001217 printf("\t--output\tWrite output to file\n");
1218 printf("\t--timeout\tRuntime in seconds\n");
1219 printf("\t--latency-log\tGenerate per-job latency logs\n");
1220 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1221 printf("\t--minimal\tMinimal (terse) output\n");
1222 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001223 printf("\t--help\t\tPrint this page\n");
1224 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001225}
1226
Jens Axboe972cfd22006-06-09 11:08:56 +02001227static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001228{
Jens Axboeb4692822006-10-27 13:43:22 +02001229 struct thread_data *td = NULL;
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001230 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001231
Jens Axboe0be06ea2007-03-01 14:23:28 +01001232 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001233 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001234 case 't':
1235 def_timeout = atoi(optarg);
1236 break;
1237 case 'l':
1238 write_lat_log = 1;
1239 break;
1240 case 'w':
1241 write_bw_log = 1;
1242 break;
1243 case 'o':
1244 f_out = fopen(optarg, "w+");
1245 if (!f_out) {
1246 perror("fopen output");
1247 exit(1);
1248 }
1249 f_err = f_out;
1250 break;
1251 case 'm':
1252 terse_output = 1;
1253 break;
1254 case 'h':
1255 usage();
1256 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001257 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001258 ret = show_cmd_help(options, optarg);
1259 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001260 case 'v':
1261 printf("%s\n", fio_version_string);
1262 exit(0);
1263 case FIO_GETOPT_JOB: {
1264 const char *opt = long_options[lidx].name;
1265 char *val = optarg;
1266
Jens Axboec2b1e752006-10-30 09:03:13 +01001267 if (!strncmp(opt, "name", 4) && td) {
1268 ret = add_job(td, td->name ?: "fio", 0);
1269 if (ret) {
1270 put_job(td);
1271 return 0;
1272 }
1273 td = NULL;
1274 }
Jens Axboeb4692822006-10-27 13:43:22 +02001275 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001276 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001277
1278 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001279 if (!td)
1280 return 0;
1281 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001282
Jens Axboeb1508cf2006-11-02 09:12:40 +01001283 ret = parse_cmd_option(opt, val, options, td);
Jens Axboe78217df2007-02-23 11:18:30 +01001284 if (ret)
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001285 dont_add_job = 1;
Jens Axboeb4692822006-10-27 13:43:22 +02001286 break;
1287 }
1288 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001289 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001290 }
1291 }
Jens Axboec9fad892006-05-27 17:26:50 +02001292
Jens Axboeb4692822006-10-27 13:43:22 +02001293 if (td) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001294 if (dont_add_job)
Jens Axboeb4692822006-10-27 13:43:22 +02001295 put_job(td);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001296 else {
1297 ret = add_job(td, td->name ?: "fio", 0);
1298 if (ret)
1299 put_job(td);
1300 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001301 }
Jens Axboe774a6172006-08-24 08:44:26 +02001302
Jens Axboeb4692822006-10-27 13:43:22 +02001303 while (optind < argc) {
1304 ini_idx++;
1305 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1306 ini_file[ini_idx - 1] = strdup(argv[optind]);
1307 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001308 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001309
1310 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001311}
1312
1313static void free_shm(void)
1314{
1315 struct shmid_ds sbuf;
1316
1317 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001318 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001319 threads = NULL;
1320 shmctl(shm_id, IPC_RMID, &sbuf);
1321 }
1322}
1323
Jens Axboe906c8d72006-06-13 09:37:56 +02001324/*
1325 * The thread area is shared between the main process and the job
1326 * threads/processes. So setup a shared memory segment that will hold
1327 * all the job info.
1328 */
Jens Axboeebac4652005-12-08 15:25:21 +01001329static int setup_thread_area(void)
1330{
1331 /*
1332 * 1024 is too much on some machines, scale max_jobs if
1333 * we get a failure that looks like too large a shm segment
1334 */
1335 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001336 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001337
Jens Axboe906c8d72006-06-13 09:37:56 +02001338 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001339 if (shm_id != -1)
1340 break;
1341 if (errno != EINVAL) {
1342 perror("shmget");
1343 break;
1344 }
1345
1346 max_jobs >>= 1;
1347 } while (max_jobs);
1348
1349 if (shm_id == -1)
1350 return 1;
1351
1352 threads = shmat(shm_id, NULL, 0);
1353 if (threads == (void *) -1) {
1354 perror("shmat");
1355 return 1;
1356 }
1357
1358 atexit(free_shm);
1359 return 0;
1360}
1361
Jens Axboeb4692822006-10-27 13:43:22 +02001362/*
1363 * Copy the fio options into the long options map, so we mirror
1364 * job and cmd line options.
1365 */
1366static void dupe_job_options(void)
1367{
1368 struct fio_option *o;
1369 unsigned int i;
1370
1371 i = 0;
1372 while (long_options[i].name)
1373 i++;
1374
1375 o = &options[0];
1376 while (o->name) {
1377 long_options[i].name = o->name;
1378 long_options[i].val = FIO_GETOPT_JOB;
1379 if (o->type == FIO_OPT_STR_SET)
1380 long_options[i].has_arg = no_argument;
1381 else
1382 long_options[i].has_arg = required_argument;
1383
1384 i++;
1385 o++;
1386 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1387 }
1388}
1389
Jens Axboeebac4652005-12-08 15:25:21 +01001390int parse_options(int argc, char *argv[])
1391{
Jens Axboe972cfd22006-06-09 11:08:56 +02001392 int job_files, i;
1393
Jens Axboeb4692822006-10-27 13:43:22 +02001394 f_out = stdout;
1395 f_err = stderr;
1396
Jens Axboe13335dd2007-01-10 13:14:02 +01001397 options_init(options);
1398
Jens Axboeb4692822006-10-27 13:43:22 +02001399 dupe_job_options();
1400
Jens Axboeebac4652005-12-08 15:25:21 +01001401 if (setup_thread_area())
1402 return 1;
1403 if (fill_def_thread())
1404 return 1;
1405
Jens Axboe972cfd22006-06-09 11:08:56 +02001406 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001407
Jens Axboe972cfd22006-06-09 11:08:56 +02001408 for (i = 0; i < job_files; i++) {
1409 if (fill_def_thread())
1410 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001411 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001412 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001413 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001414 }
Jens Axboeebac4652005-12-08 15:25:21 +01001415
Jens Axboe88c6ed82006-06-09 11:28:10 +02001416 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001417
1418 if (!thread_number) {
1419 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001420 return 1;
1421 }
1422
Jens Axboeebac4652005-12-08 15:25:21 +01001423 return 0;
1424}