blob: 33a1c281c696607dfea6c3124bec80d8372141cb [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
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100106 },
Jens Axboee1f36502006-10-27 10:54:08 +0200107 },
108 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100109 .name = "iodepth",
110 .type = FIO_OPT_INT,
111 .off1 = td_var_offset(iodepth),
112 .help = "Amount of IO buffers to keep in flight",
113 .def = "1",
114 },
115 {
Jens Axboecb5ab512007-02-26 12:57:09 +0100116 .name = "iodepth_batch",
117 .type = FIO_OPT_INT,
118 .off1 = td_var_offset(iodepth_batch),
119 .help = "Number of IO to submit in one go",
120 },
121 {
Jens Axboee916b392007-02-20 14:37:26 +0100122 .name = "iodepth_low",
123 .type = FIO_OPT_INT,
124 .off1 = td_var_offset(iodepth_low),
125 .help = "Low water mark for queuing depth",
126 },
127 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100128 .name = "size",
129 .type = FIO_OPT_STR_VAL,
130 .off1 = td_var_offset(total_file_size),
131 .help = "Size of device or file",
132 },
133 {
134 .name = "bs",
135 .type = FIO_OPT_STR_VAL_INT,
136 .off1 = td_var_offset(bs[DDIR_READ]),
137 .off2 = td_var_offset(bs[DDIR_WRITE]),
138 .help = "Block size unit",
139 .def = "4k",
140 },
141 {
142 .name = "bsrange",
143 .type = FIO_OPT_RANGE,
144 .off1 = td_var_offset(min_bs[DDIR_READ]),
145 .off2 = td_var_offset(max_bs[DDIR_READ]),
146 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
147 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
148 .help = "Set block size range (in more detail than bs)",
149 },
150 {
151 .name = "bs_unaligned",
152 .type = FIO_OPT_STR_SET,
153 .off1 = td_var_offset(bs_unaligned),
154 .help = "Don't sector align IO buffer sizes",
155 },
156 {
157 .name = "offset",
158 .type = FIO_OPT_STR_VAL,
159 .off1 = td_var_offset(start_offset),
160 .help = "Start IO from this offset",
161 .def = "0",
162 },
163 {
164 .name = "randrepeat",
165 .type = FIO_OPT_BOOL,
166 .off1 = td_var_offset(rand_repeatable),
167 .help = "Use repeatable random IO pattern",
168 .def = "1",
169 },
170 {
171 .name = "norandommap",
172 .type = FIO_OPT_STR_SET,
173 .off1 = td_var_offset(norandommap),
174 .help = "Accept potential duplicate random blocks",
175 },
176 {
177 .name = "nrfiles",
178 .type = FIO_OPT_INT,
179 .off1 = td_var_offset(nr_files),
180 .help = "Split job workload between this number of files",
181 .def = "1",
182 },
183 {
Jens Axboe0aabe162007-02-23 08:45:55 +0100184 .name = "file_service_type",
185 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100186 .off1 = td_var_offset(file_service_type),
Jens Axboe0aabe162007-02-23 08:45:55 +0100187 .help = "How to select which file to service next",
188 .def = "roundrobin",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100189 .posval = {
190 { .ival = "random", .oval = FIO_FSERVICE_RANDOM },
191 { .ival = "roundrobin", .oval = FIO_FSERVICE_RR },
192 },
Jens Axboe0aabe162007-02-23 08:45:55 +0100193 },
194 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100195 .name = "fsync",
196 .type = FIO_OPT_INT,
197 .off1 = td_var_offset(fsync_blocks),
198 .help = "Issue fsync for writes every given number of blocks",
199 .def = "0",
200 },
201 {
202 .name = "direct",
203 .type = FIO_OPT_BOOL,
204 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100205 .help = "Use O_DIRECT IO (negates buffered)",
206 .def = "0",
207 },
208 {
209 .name = "buffered",
210 .type = FIO_OPT_BOOL,
211 .off1 = td_var_offset(odirect),
212 .neg = 1,
213 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100214 .def = "1",
215 },
216 {
217 .name = "overwrite",
218 .type = FIO_OPT_BOOL,
219 .off1 = td_var_offset(overwrite),
220 .help = "When writing, set whether to overwrite current data",
221 .def = "0",
222 },
223 {
224 .name = "loops",
225 .type = FIO_OPT_INT,
226 .off1 = td_var_offset(loops),
227 .help = "Number of times to run the job",
228 .def = "1",
229 },
230 {
231 .name = "numjobs",
232 .type = FIO_OPT_INT,
233 .off1 = td_var_offset(numjobs),
234 .help = "Duplicate this job this many times",
235 .def = "1",
236 },
237 {
238 .name = "startdelay",
239 .type = FIO_OPT_INT,
240 .off1 = td_var_offset(start_delay),
241 .help = "Only start job when this period has passed",
242 .def = "0",
243 },
244 {
245 .name = "runtime",
246 .alias = "timeout",
247 .type = FIO_OPT_STR_VAL_TIME,
248 .off1 = td_var_offset(timeout),
249 .help = "Stop workload when this amount of time has passed",
250 .def = "0",
251 },
252 {
Jens Axboee1f36502006-10-27 10:54:08 +0200253 .name = "mem",
254 .type = FIO_OPT_STR,
255 .cb = str_mem_cb,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100256 .off1 = td_var_offset(mem_type),
Jens Axboe15f79182007-01-10 12:26:18 +0100257 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100258 .def = "malloc",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100259 .posval = {
260 { .ival = "malloc", .oval = MEM_MALLOC },
261 { .ival = "shm", .oval = MEM_SHM },
262#ifdef FIO_HAVE_HUGETLB
263 { .ival = "shmhuge", .oval = MEM_SHMHUGE },
264#endif
265 { .ival = "mmap", .oval = MEM_MMAP },
266#ifdef FIO_HAVE_HUGETLB
267 { .ival = "mmaphuge", .oval = MEM_MMAPHUGE },
268#endif
269 },
Jens Axboee1f36502006-10-27 10:54:08 +0200270 },
271 {
272 .name = "verify",
273 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100274 .off1 = td_var_offset(verify),
Jens Axboe15f79182007-01-10 12:26:18 +0100275 .help = "Verify sum function",
Jens Axboeee738492007-01-10 11:23:16 +0100276 .def = "0",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100277 .posval = {
278 { .ival = "0", .oval = VERIFY_NONE },
279 { .ival = "crc32", .oval = VERIFY_CRC32 },
280 { .ival = "md5", .oval = VERIFY_MD5 },
281 },
Jens Axboee1f36502006-10-27 10:54:08 +0200282 },
283 {
284 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200285 .type = FIO_OPT_STR_STORE,
286 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100287 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200288 },
289 {
Jens Axboe076efc72006-10-27 11:24:25 +0200290 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200291 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200292 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100293 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200294 },
295 {
296 .name = "exec_prerun",
297 .type = FIO_OPT_STR_STORE,
298 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100299 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200300 },
301 {
302 .name = "exec_postrun",
303 .type = FIO_OPT_STR_STORE,
304 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100305 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200306 },
307#ifdef FIO_HAVE_IOSCHED_SWITCH
308 {
309 .name = "ioscheduler",
310 .type = FIO_OPT_STR_STORE,
311 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100312 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200313 },
314#endif
315 {
Jens Axboee1f36502006-10-27 10:54:08 +0200316 .name = "zonesize",
317 .type = FIO_OPT_STR_VAL,
318 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100319 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100320 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200321 },
322 {
323 .name = "zoneskip",
324 .type = FIO_OPT_STR_VAL,
325 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100326 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100327 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200328 },
329 {
330 .name = "lockmem",
331 .type = FIO_OPT_STR_VAL,
332 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100333 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100334 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200335 },
336 {
Jens Axboee1f36502006-10-27 10:54:08 +0200337 .name = "rwmixcycle",
338 .type = FIO_OPT_INT,
339 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100340 .help = "Cycle period for mixed read/write workloads (msec)",
341 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200342 },
343 {
344 .name = "rwmixread",
345 .type = FIO_OPT_INT,
346 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100347 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100348 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100349 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200350 },
351 {
352 .name = "rwmixwrite",
353 .type = FIO_OPT_INT,
354 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100355 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100356 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100357 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200358 },
359 {
360 .name = "nice",
361 .type = FIO_OPT_INT,
362 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100363 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100364 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100365 .maxval = 20,
366 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200367 },
368#ifdef FIO_HAVE_IOPRIO
369 {
370 .name = "prio",
371 .type = FIO_OPT_INT,
372 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100373 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100374 .minval = 0,
375 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200376 },
377 {
378 .name = "prioclass",
379 .type = FIO_OPT_INT,
380 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100381 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100382 .minval = 0,
383 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200384 },
385#endif
386 {
387 .name = "thinktime",
388 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100389 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100390 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100391 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200392 },
393 {
Jens Axboe48097d52007-02-17 06:30:44 +0100394 .name = "thinktime_spin",
395 .type = FIO_OPT_INT,
396 .off1 = td_var_offset(thinktime_spin),
397 .help = "Start thinktime by spinning this amount (usec)",
398 .def = "0",
399 },
400 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100401 .name = "thinktime_blocks",
402 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100403 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100404 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100405 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100406 },
407 {
Jens Axboee1f36502006-10-27 10:54:08 +0200408 .name = "rate",
409 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100410 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100411 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200412 },
413 {
414 .name = "ratemin",
415 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100416 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100417 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200418 },
419 {
420 .name = "ratecycle",
421 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100422 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100423 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100424 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200425 },
426 {
Jens Axboee1f36502006-10-27 10:54:08 +0200427 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100428 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100429 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100430 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100431 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200432 },
433 {
434 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100435 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100436 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100437 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100438 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200439 },
440 {
441 .name = "bwavgtime",
442 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100443 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100444 .help = "Time window over which to calculate bandwidth (msec)",
445 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200446 },
447 {
448 .name = "create_serialize",
Jens Axboe13335dd2007-01-10 13:14:02 +0100449 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100450 .off1 = td_var_offset(create_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100451 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100452 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200453 },
454 {
455 .name = "create_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100456 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100457 .off1 = td_var_offset(create_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100458 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100459 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200460 },
461 {
Jens Axboee1f36502006-10-27 10:54:08 +0200462 .name = "cpuload",
463 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100464 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100465 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200466 },
467 {
468 .name = "cpuchunks",
469 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100470 .off1 = td_var_offset(cpucycle),
Jens Axboefd28ca42007-01-09 21:20:13 +0100471 .help = "Length of the CPU burn cycles",
Jens Axboee1f36502006-10-27 10:54:08 +0200472 },
Jens Axboee1f36502006-10-27 10:54:08 +0200473#ifdef FIO_HAVE_CPU_AFFINITY
474 {
475 .name = "cpumask",
476 .type = FIO_OPT_INT,
477 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100478 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200479 },
480#endif
481 {
482 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100483 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100484 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100485 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100486 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200487 },
488 {
489 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100490 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200491 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100492 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100493 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200494 },
495 {
496 .name = "exitall",
497 .type = FIO_OPT_STR_SET,
498 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100499 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200500 },
501 {
502 .name = "stonewall",
503 .type = FIO_OPT_STR_SET,
504 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100505 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200506 },
507 {
508 .name = "thread",
509 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100510 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100511 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200512 },
513 {
514 .name = "write_bw_log",
515 .type = FIO_OPT_STR_SET,
516 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100517 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200518 },
519 {
520 .name = "write_lat_log",
521 .type = FIO_OPT_STR_SET,
522 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100523 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200524 },
525 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100526 .name = "hugepage-size",
527 .type = FIO_OPT_STR_VAL,
528 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100529 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100530 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100531 },
532 {
Jens Axboee1f36502006-10-27 10:54:08 +0200533 .name = NULL,
534 },
535};
536
Jens Axboeb4692822006-10-27 13:43:22 +0200537#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
538#define FIO_CMD_OPTS (16)
539#define FIO_GETOPT_JOB (0x89988998)
540
541/*
542 * Command line options. These will contain the above, plus a few
543 * extra that only pertain to fio itself and not jobs.
544 */
545static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
546 {
547 .name = "output",
548 .has_arg = required_argument,
549 .val = 'o',
550 },
551 {
552 .name = "timeout",
553 .has_arg = required_argument,
554 .val = 't',
555 },
556 {
557 .name = "latency-log",
558 .has_arg = required_argument,
559 .val = 'l',
560 },
561 {
562 .name = "bandwidth-log",
563 .has_arg = required_argument,
564 .val = 'b',
565 },
566 {
567 .name = "minimal",
568 .has_arg = optional_argument,
569 .val = 'm',
570 },
571 {
572 .name = "version",
573 .has_arg = no_argument,
574 .val = 'v',
575 },
576 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100577 .name = "help",
578 .has_arg = no_argument,
579 .val = 'h',
580 },
581 {
582 .name = "cmdhelp",
583 .has_arg = required_argument,
584 .val = 'c',
585 },
586 {
Jens Axboeb4692822006-10-27 13:43:22 +0200587 .name = NULL,
588 },
589};
590
Jens Axboeee738492007-01-10 11:23:16 +0100591static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200592
Jens Axboe5cd033b2007-01-09 08:19:01 +0100593static char fio_version_string[] = "fio 1.11";
Jens Axboeebac4652005-12-08 15:25:21 +0100594
Jens Axboe972cfd22006-06-09 11:08:56 +0200595static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100596static int max_jobs = MAX_JOBS;
597
598struct thread_data def_thread;
599struct thread_data *threads = NULL;
600
Jens Axboeebac4652005-12-08 15:25:21 +0100601int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200602int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200603unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200604FILE *f_out = NULL;
605FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100606
Jens Axboeee738492007-01-10 11:23:16 +0100607static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100608int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200609
Jens Axboe906c8d72006-06-13 09:37:56 +0200610/*
611 * Return a free job structure.
612 */
Jens Axboeebac4652005-12-08 15:25:21 +0100613static struct thread_data *get_new_job(int global, struct thread_data *parent)
614{
615 struct thread_data *td;
616
617 if (global)
618 return &def_thread;
619 if (thread_number >= max_jobs)
620 return NULL;
621
622 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200623 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100624
Jens Axboeebac4652005-12-08 15:25:21 +0100625 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100626 return td;
627}
628
629static void put_job(struct thread_data *td)
630{
Jens Axboe549577a2006-10-30 12:23:41 +0100631 if (td == &def_thread)
632 return;
633
Jens Axboe16edf252007-02-10 14:59:22 +0100634 if (td->error)
635 fprintf(f_out, "fio: %s\n", td->verror);
636
Jens Axboeebac4652005-12-08 15:25:21 +0100637 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
638 thread_number--;
639}
640
Jens Axboedad915e2006-10-27 11:10:18 +0200641/*
642 * Lazy way of fixing up options that depend on each other. We could also
643 * define option callback handlers, but this is easier.
644 */
Jens Axboee1f36502006-10-27 10:54:08 +0200645static void fixup_options(struct thread_data *td)
646{
Jens Axboee1f36502006-10-27 10:54:08 +0200647 if (!td->rwmixread && td->rwmixwrite)
648 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200649
Jens Axboe076efc72006-10-27 11:24:25 +0200650 if (td->write_iolog_file && td->read_iolog_file) {
651 log_err("fio: read iolog overrides write_iolog\n");
652 free(td->write_iolog_file);
653 td->write_iolog_file = NULL;
654 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100655
656 if (td->io_ops->flags & FIO_SYNCIO)
657 td->iodepth = 1;
658 else {
659 if (!td->iodepth)
660 td->iodepth = td->nr_files;
661 }
662
663 /*
664 * only really works for sequential io for now, and with 1 file
665 */
Jens Axboe413dd452007-02-23 09:26:09 +0100666 if (td->zone_size && td_random(td) && td->nr_files == 1)
Jens Axboe16b462a2006-10-30 12:35:18 +0100667 td->zone_size = 0;
668
669 /*
670 * Reads can do overwrites, we always need to pre-create the file
671 */
672 if (td_read(td) || td_rw(td))
673 td->overwrite = 1;
674
Jens Axboea00735e2006-11-03 08:58:08 +0100675 if (!td->min_bs[DDIR_READ])
676 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
677 if (!td->max_bs[DDIR_READ])
678 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
679 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100680 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100681 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100682 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100683
684 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
685
Jens Axboe16b462a2006-10-30 12:35:18 +0100686 if (td_read(td) && !td_rw(td))
687 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100688
689 if (td->norandommap && td->verify != VERIFY_NONE) {
690 log_err("fio: norandommap given, verify disabled\n");
691 td->verify = VERIFY_NONE;
692 }
Jens Axboe690adba2006-10-30 15:25:09 +0100693 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
694 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100695
696 /*
697 * O_DIRECT and char doesn't mix, clear that flag if necessary.
698 */
699 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
700 td->odirect = 0;
Jens Axboe48097d52007-02-17 06:30:44 +0100701
702 /*
703 * thinktime_spin must be less than thinktime
704 */
705 if (td->thinktime_spin > td->thinktime)
706 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100707
708 /*
709 * The low water mark cannot be bigger than the iodepth
710 */
Jens Axboe9467b772007-02-27 19:56:43 +0100711 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
712 /*
713 * syslet work around - if the workload is sequential,
714 * we want to let the queue drain all the way down to
715 * avoid seeking between async threads
716 */
717 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
718 td->iodepth_low = 1;
719 else
720 td->iodepth_low = td->iodepth;
721 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100722
723 /*
724 * If batch number isn't set, default to the same as iodepth
725 */
726 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
727 td->iodepth_batch = td->iodepth;
Jens Axboee1f36502006-10-27 10:54:08 +0200728}
729
Jens Axboe906c8d72006-06-13 09:37:56 +0200730/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100731 * This function leaks the buffer
732 */
733static char *to_kmg(unsigned int val)
734{
735 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100736 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100737 char *p = post;
738
Jens Axboe245142f2006-11-08 12:49:21 +0100739 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100740 if (val & 1023)
741 break;
742
743 val >>= 10;
744 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100745 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100746
747 snprintf(buf, 31, "%u%c", val, *p);
748 return buf;
749}
750
751/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200752 * Adds a job to the list of things todo. Sanitizes the various options
753 * to make sure we don't have conflicts, and initializes various
754 * members of td.
755 */
Jens Axboe75154842006-06-01 13:56:09 +0200756static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100757{
Jens Axboe413dd452007-02-23 09:26:09 +0100758 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
759 "randread", "randwrite", "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100760 struct stat sb;
Jens Axboe413dd452007-02-23 09:26:09 +0100761 int numjobs, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200762 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100763
Jens Axboeebac4652005-12-08 15:25:21 +0100764 /*
765 * the def_thread is just for options, it's not a real job
766 */
767 if (td == &def_thread)
768 return 0;
769
Jens Axboeee738492007-01-10 11:23:16 +0100770 assert(td->io_ops);
Jens Axboedf641192006-10-12 07:55:41 +0200771
Jens Axboe690adba2006-10-30 15:25:09 +0100772 if (td->odirect)
773 td->io_ops->flags |= FIO_RAWIO;
774
Jens Axboeebac4652005-12-08 15:25:21 +0100775 td->filetype = FIO_TYPE_FILE;
Jens Axboe890df532007-02-17 01:51:13 +0100776 if (td->filename && !lstat(td->filename, &sb)) {
Jens Axboe0af7b542006-02-17 10:10:12 +0100777 if (S_ISBLK(sb.st_mode))
778 td->filetype = FIO_TYPE_BD;
779 else if (S_ISCHR(sb.st_mode))
780 td->filetype = FIO_TYPE_CHAR;
781 }
Jens Axboeebac4652005-12-08 15:25:21 +0100782
Jens Axboee0a22332006-12-20 12:54:25 +0100783 fixup_options(td);
784
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200785 if (td->filename)
786 td->nr_uniq_files = 1;
787 else
788 td->nr_uniq_files = td->nr_files;
789
790 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200791 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200792 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200793
Jens Axboe16edf252007-02-10 14:59:22 +0100794 if (td->directory && td->directory[0] != '\0') {
795 if (lstat(td->directory, &sb) < 0) {
796 log_err("fio: %s is not a directory\n", td->directory);
Jens Axboee1161c32007-02-22 19:36:48 +0100797 td_verror(td, errno, "lstat");
Jens Axboe16edf252007-02-10 14:59:22 +0100798 return 1;
799 }
800 if (!S_ISDIR(sb.st_mode)) {
801 log_err("fio: %s is not a directory\n", td->directory);
802 return 1;
803 }
Jens Axboe8aeebd52007-01-08 10:47:43 +0100804 len = sprintf(tmp, "%s/", td->directory);
Jens Axboe16edf252007-02-10 14:59:22 +0100805 }
Jens Axboeebac4652005-12-08 15:25:21 +0100806
Jens Axboe53cdc682006-10-18 11:50:58 +0200807 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
808
809 for_each_file(td, f, i) {
810 memset(f, 0, sizeof(*f));
811 f->fd = -1;
812
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200813 if (td->filename)
814 sprintf(tmp + len, "%s", td->filename);
815 else
816 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200817 f->file_name = strdup(tmp);
818 }
819 } else {
820 td->nr_files = 1;
821 td->files = malloc(sizeof(struct fio_file));
822 f = &td->files[0];
823
824 memset(f, 0, sizeof(*f));
825 f->fd = -1;
826 f->file_name = strdup(jobname);
827 }
828
829 for_each_file(td, f, i) {
830 f->file_size = td->total_file_size / td->nr_files;
831 f->file_offset = td->start_offset;
832 }
833
Jens Axboebbfd6b02006-06-07 19:42:54 +0200834 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100835
Jens Axboe079ad092007-02-20 13:58:20 +0100836 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
837 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
838 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100839
Jens Axboeebac4652005-12-08 15:25:21 +0100840 if (td->stonewall && td->thread_number > 1)
841 groupid++;
842
843 td->groupid = groupid;
844
845 if (setup_rate(td))
846 goto err;
847
Jens Axboeec94ec52006-10-20 10:59:19 +0200848 if (td->write_lat_log) {
Jens Axboe079ad092007-02-20 13:58:20 +0100849 setup_log(&td->ts.slat_log);
850 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100851 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200852 if (td->write_bw_log)
Jens Axboe079ad092007-02-20 13:58:20 +0100853 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100854
Jens Axboeb4692822006-10-27 13:43:22 +0200855 if (!td->name)
856 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200857
Jens Axboec6ae0a52006-06-12 11:04:44 +0200858 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200859 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200860 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200861 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100862 else {
863 char *c1, *c2, *c3, *c4;
864
865 c1 = to_kmg(td->min_bs[DDIR_READ]);
866 c2 = to_kmg(td->max_bs[DDIR_READ]);
867 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
868 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
869
Jens Axboe413dd452007-02-23 09:26:09 +0100870 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 +0100871
872 free(c1);
873 free(c2);
874 free(c3);
875 free(c4);
876 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200877 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200878 fprintf(f_out, "...\n");
879 }
Jens Axboeebac4652005-12-08 15:25:21 +0100880
881 /*
882 * recurse add identical jobs, clear numjobs and stonewall options
883 * as they don't apply to sub-jobs
884 */
885 numjobs = td->numjobs;
886 while (--numjobs) {
887 struct thread_data *td_new = get_new_job(0, td);
888
889 if (!td_new)
890 goto err;
891
892 td_new->numjobs = 1;
893 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200894 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100895
Jens Axboe75154842006-06-01 13:56:09 +0200896 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100897 goto err;
898 }
899 return 0;
900err:
901 put_job(td);
902 return -1;
903}
904
Jens Axboe906c8d72006-06-13 09:37:56 +0200905/*
906 * Initialize the various random states we need (random io, block size ranges,
907 * read/write mix, etc).
908 */
Jens Axboeebac4652005-12-08 15:25:21 +0100909int init_random_state(struct thread_data *td)
910{
Jens Axboe0aabe162007-02-23 08:45:55 +0100911 unsigned long seeds[5];
Jens Axboe53cdc682006-10-18 11:50:58 +0200912 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200913 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100914
Jens Axboef48b4672006-10-27 11:30:07 +0200915 if (td->io_ops->flags & FIO_CPUIO)
916 return 0;
917
Jens Axboe1ac267b2006-05-31 20:29:00 +0200918 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100919 if (fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100920 td_verror(td, errno, "open");
Jens Axboeebac4652005-12-08 15:25:21 +0100921 return 1;
922 }
923
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200924 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100925 td_verror(td, EIO, "read");
Jens Axboeebac4652005-12-08 15:25:21 +0100926 close(fd);
927 return 1;
928 }
929
930 close(fd);
931
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200932 os_random_seed(seeds[0], &td->bsrange_state);
933 os_random_seed(seeds[1], &td->verify_state);
934 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100935
Jens Axboe413dd452007-02-23 09:26:09 +0100936 if (td->file_service_type == FIO_FSERVICE_RANDOM)
937 os_random_seed(seeds[3], &td->next_file_state);
938
939 if (!td_random(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100940 return 0;
941
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200942 if (td->rand_repeatable)
Jens Axboe0aabe162007-02-23 08:45:55 +0100943 seeds[4] = FIO_RANDSEED * td->thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100944
Jens Axboebb8895e2006-10-30 15:14:48 +0100945 if (!td->norandommap) {
946 for_each_file(td, f, i) {
Jens Axboea4a81712007-02-13 20:07:26 +0100947 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100948 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100949 f->file_map = malloc(num_maps * sizeof(long));
950 f->num_maps = num_maps;
951 memset(f->file_map, 0, num_maps * sizeof(long));
952 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200953 }
Jens Axboeebac4652005-12-08 15:25:21 +0100954
Jens Axboe0aabe162007-02-23 08:45:55 +0100955 os_random_seed(seeds[4], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100956 return 0;
957}
958
959static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
960{
961#ifdef FIO_HAVE_CPU_AFFINITY
962 unsigned int i;
963
964 CPU_ZERO(&cpumask);
965
966 for (i = 0; i < sizeof(int) * 8; i++) {
967 if ((1 << i) & cpu)
968 CPU_SET(i, &cpumask);
969 }
970#endif
971}
972
Jens Axboeebac4652005-12-08 15:25:21 +0100973static int is_empty_or_comment(char *line)
974{
975 unsigned int i;
976
977 for (i = 0; i < strlen(line); i++) {
978 if (line[i] == ';')
979 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100980 if (line[i] == '#')
981 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100982 if (!isspace(line[i]) && !iscntrl(line[i]))
983 return 0;
984 }
985
986 return 1;
987}
988
Jens Axboe313cb202006-12-21 09:50:00 +0100989/*
990 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
991 */
992static char *get_mmap_file(const char *str)
993{
994 char *p = strstr(str, ":");
995
996 if (!p)
997 return NULL;
998
999 p++;
1000 strip_blank_front(&p);
1001 strip_blank_end(p);
1002 return strdup(p);
1003}
1004
Jens Axboeb4692822006-10-27 13:43:22 +02001005static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +01001006{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001007 struct thread_data *td = data;
1008
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001009 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
Jens Axboe313cb202006-12-21 09:50:00 +01001010 td->mmapfile = get_mmap_file(mem);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001011 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +01001012 log_err("fio: mmaphuge:/path/to/file\n");
1013 return 1;
1014 }
Jens Axboeebac4652005-12-08 15:25:21 +01001015 }
1016
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001017 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001018}
1019
Jens Axboeb4692822006-10-27 13:43:22 +02001020static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +01001021{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001022 struct thread_data *td = data;
1023
Jens Axboe2866c822006-10-09 15:57:48 +02001024 td->io_ops = load_ioengine(td, str);
1025 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +01001026 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001027
Jens Axboeebac4652005-12-08 15:25:21 +01001028 return 1;
1029}
1030
Jens Axboee1f36502006-10-27 10:54:08 +02001031static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1032{
1033 mlock_size = *val;
1034 return 0;
1035}
1036
Jens Axboe34cfcda2006-11-03 14:00:45 +01001037#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001038static int str_prioclass_cb(void *data, unsigned int *val)
1039{
1040 struct thread_data *td = data;
1041
1042 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1043 return 0;
1044}
1045
1046static int str_prio_cb(void *data, unsigned int *val)
1047{
1048 struct thread_data *td = data;
1049
1050 td->ioprio |= *val;
1051 return 0;
1052}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001053#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001054
1055static int str_exitall_cb(void)
1056{
1057 exitall_on_terminate = 1;
1058 return 0;
1059}
1060
1061static int str_cpumask_cb(void *data, unsigned int *val)
1062{
1063 struct thread_data *td = data;
1064
1065 fill_cpu_mask(td->cpumask, *val);
1066 return 0;
1067}
1068
Jens Axboe07261982006-06-07 10:51:12 +02001069/*
1070 * This is our [ini] type file parser.
1071 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001072static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001073{
Jens Axboee1f36502006-10-27 10:54:08 +02001074 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001075 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001076 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001077 fpos_t off;
1078 FILE *f;
1079 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001080 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001081
1082 f = fopen(file, "r");
1083 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001084 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001085 return 1;
1086 }
1087
1088 string = malloc(4096);
1089 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001090 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001091
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001092 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001093 do {
1094 p = fgets(string, 4095, f);
1095 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001096 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001097 if (is_empty_or_comment(p))
1098 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001099 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001100 continue;
1101
1102 global = !strncmp(name, "global", 6);
1103
1104 name[strlen(name) - 1] = '\0';
1105
1106 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001107 if (!td) {
1108 ret = 1;
1109 break;
1110 }
Jens Axboeebac4652005-12-08 15:25:21 +01001111
Jens Axboe972cfd22006-06-09 11:08:56 +02001112 /*
1113 * Seperate multiple job files by a stonewall
1114 */
Jens Axboef9481912006-06-09 11:37:28 +02001115 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001116 td->stonewall = stonewall;
1117 stonewall = 0;
1118 }
1119
Jens Axboeebac4652005-12-08 15:25:21 +01001120 fgetpos(f, &off);
1121 while ((p = fgets(string, 4096, f)) != NULL) {
1122 if (is_empty_or_comment(p))
1123 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001124
Jens Axboeb6754f92006-05-30 14:29:32 +02001125 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001126
1127 if (p[0] == '[')
1128 break;
1129
Jens Axboe4ae3f762006-05-30 13:35:14 +02001130 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001131
Jens Axboee1f36502006-10-27 10:54:08 +02001132 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001133
Jens Axboe45410ac2006-06-07 11:10:39 +02001134 /*
1135 * Don't break here, continue parsing options so we
1136 * dump all the bad ones. Makes trial/error fixups
1137 * easier on the user.
1138 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001139 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001140 }
Jens Axboeebac4652005-12-08 15:25:21 +01001141
Jens Axboe45410ac2006-06-07 11:10:39 +02001142 if (!ret) {
1143 fsetpos(f, &off);
1144 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001145 } else {
1146 log_err("fio: job %s dropped\n", name);
1147 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001148 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001149 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001150
1151 free(string);
1152 free(name);
1153 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001154 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001155}
1156
1157static int fill_def_thread(void)
1158{
1159 memset(&def_thread, 0, sizeof(def_thread));
1160
1161 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1162 perror("sched_getaffinity");
1163 return 1;
1164 }
1165
1166 /*
Jens Axboeee738492007-01-10 11:23:16 +01001167 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001168 */
Jens Axboeee738492007-01-10 11:23:16 +01001169 fill_default_options(&def_thread, options);
1170
Jens Axboe972cfd22006-06-09 11:08:56 +02001171 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001172 def_thread.write_bw_log = write_bw_log;
1173 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001174
Jens Axboeebac4652005-12-08 15:25:21 +01001175#ifdef FIO_HAVE_DISK_UTIL
1176 def_thread.do_disk_util = 1;
1177#endif
1178
1179 return 0;
1180}
1181
Jens Axboe0ab8db82006-10-18 17:16:23 +02001182static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001183{
1184 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001185 printf("\t--output\tWrite output to file\n");
1186 printf("\t--timeout\tRuntime in seconds\n");
1187 printf("\t--latency-log\tGenerate per-job latency logs\n");
1188 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1189 printf("\t--minimal\tMinimal (terse) output\n");
1190 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001191 printf("\t--help\t\tPrint this page\n");
1192 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001193}
1194
Jens Axboe972cfd22006-06-09 11:08:56 +02001195static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001196{
Jens Axboeb4692822006-10-27 13:43:22 +02001197 struct thread_data *td = NULL;
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001198 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001199
Jens Axboeb4692822006-10-27 13:43:22 +02001200 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001201 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001202 case 't':
1203 def_timeout = atoi(optarg);
1204 break;
1205 case 'l':
1206 write_lat_log = 1;
1207 break;
1208 case 'w':
1209 write_bw_log = 1;
1210 break;
1211 case 'o':
1212 f_out = fopen(optarg, "w+");
1213 if (!f_out) {
1214 perror("fopen output");
1215 exit(1);
1216 }
1217 f_err = f_out;
1218 break;
1219 case 'm':
1220 terse_output = 1;
1221 break;
1222 case 'h':
1223 usage();
1224 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001225 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001226 ret = show_cmd_help(options, optarg);
1227 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001228 case 'v':
1229 printf("%s\n", fio_version_string);
1230 exit(0);
1231 case FIO_GETOPT_JOB: {
1232 const char *opt = long_options[lidx].name;
1233 char *val = optarg;
1234
Jens Axboec2b1e752006-10-30 09:03:13 +01001235 if (!strncmp(opt, "name", 4) && td) {
1236 ret = add_job(td, td->name ?: "fio", 0);
1237 if (ret) {
1238 put_job(td);
1239 return 0;
1240 }
1241 td = NULL;
1242 }
Jens Axboeb4692822006-10-27 13:43:22 +02001243 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001244 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001245
1246 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001247 if (!td)
1248 return 0;
1249 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001250
Jens Axboeb1508cf2006-11-02 09:12:40 +01001251 ret = parse_cmd_option(opt, val, options, td);
Jens Axboe78217df2007-02-23 11:18:30 +01001252 if (ret)
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001253 dont_add_job = 1;
Jens Axboeb4692822006-10-27 13:43:22 +02001254 break;
1255 }
1256 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001257 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001258 }
1259 }
Jens Axboec9fad892006-05-27 17:26:50 +02001260
Jens Axboeb4692822006-10-27 13:43:22 +02001261 if (td) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001262 if (dont_add_job)
Jens Axboeb4692822006-10-27 13:43:22 +02001263 put_job(td);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001264 else {
1265 ret = add_job(td, td->name ?: "fio", 0);
1266 if (ret)
1267 put_job(td);
1268 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001269 }
Jens Axboe774a6172006-08-24 08:44:26 +02001270
Jens Axboeb4692822006-10-27 13:43:22 +02001271 while (optind < argc) {
1272 ini_idx++;
1273 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1274 ini_file[ini_idx - 1] = strdup(argv[optind]);
1275 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001276 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001277
1278 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001279}
1280
1281static void free_shm(void)
1282{
1283 struct shmid_ds sbuf;
1284
1285 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001286 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001287 threads = NULL;
1288 shmctl(shm_id, IPC_RMID, &sbuf);
1289 }
1290}
1291
Jens Axboe906c8d72006-06-13 09:37:56 +02001292/*
1293 * The thread area is shared between the main process and the job
1294 * threads/processes. So setup a shared memory segment that will hold
1295 * all the job info.
1296 */
Jens Axboeebac4652005-12-08 15:25:21 +01001297static int setup_thread_area(void)
1298{
1299 /*
1300 * 1024 is too much on some machines, scale max_jobs if
1301 * we get a failure that looks like too large a shm segment
1302 */
1303 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001304 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001305
Jens Axboe906c8d72006-06-13 09:37:56 +02001306 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001307 if (shm_id != -1)
1308 break;
1309 if (errno != EINVAL) {
1310 perror("shmget");
1311 break;
1312 }
1313
1314 max_jobs >>= 1;
1315 } while (max_jobs);
1316
1317 if (shm_id == -1)
1318 return 1;
1319
1320 threads = shmat(shm_id, NULL, 0);
1321 if (threads == (void *) -1) {
1322 perror("shmat");
1323 return 1;
1324 }
1325
1326 atexit(free_shm);
1327 return 0;
1328}
1329
Jens Axboeb4692822006-10-27 13:43:22 +02001330/*
1331 * Copy the fio options into the long options map, so we mirror
1332 * job and cmd line options.
1333 */
1334static void dupe_job_options(void)
1335{
1336 struct fio_option *o;
1337 unsigned int i;
1338
1339 i = 0;
1340 while (long_options[i].name)
1341 i++;
1342
1343 o = &options[0];
1344 while (o->name) {
1345 long_options[i].name = o->name;
1346 long_options[i].val = FIO_GETOPT_JOB;
1347 if (o->type == FIO_OPT_STR_SET)
1348 long_options[i].has_arg = no_argument;
1349 else
1350 long_options[i].has_arg = required_argument;
1351
1352 i++;
1353 o++;
1354 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1355 }
1356}
1357
Jens Axboeebac4652005-12-08 15:25:21 +01001358int parse_options(int argc, char *argv[])
1359{
Jens Axboe972cfd22006-06-09 11:08:56 +02001360 int job_files, i;
1361
Jens Axboeb4692822006-10-27 13:43:22 +02001362 f_out = stdout;
1363 f_err = stderr;
1364
Jens Axboe13335dd2007-01-10 13:14:02 +01001365 options_init(options);
1366
Jens Axboeb4692822006-10-27 13:43:22 +02001367 dupe_job_options();
1368
Jens Axboeebac4652005-12-08 15:25:21 +01001369 if (setup_thread_area())
1370 return 1;
1371 if (fill_def_thread())
1372 return 1;
1373
Jens Axboe972cfd22006-06-09 11:08:56 +02001374 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001375
Jens Axboe972cfd22006-06-09 11:08:56 +02001376 for (i = 0; i < job_files; i++) {
1377 if (fill_def_thread())
1378 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001379 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001380 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001381 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001382 }
Jens Axboeebac4652005-12-08 15:25:21 +01001383
Jens Axboe88c6ed82006-06-09 11:28:10 +02001384 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001385
1386 if (!thread_number) {
1387 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001388 return 1;
1389 }
1390
Jens Axboeebac4652005-12-08 15:25:21 +01001391 return 0;
1392}