blob: 8efb24ab58bf17428797e364e045ecb490d6f1eb [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 Axboee916b392007-02-20 14:37:26 +0100116 .name = "iodepth_low",
117 .type = FIO_OPT_INT,
118 .off1 = td_var_offset(iodepth_low),
119 .help = "Low water mark for queuing depth",
120 },
121 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100122 .name = "size",
123 .type = FIO_OPT_STR_VAL,
124 .off1 = td_var_offset(total_file_size),
125 .help = "Size of device or file",
126 },
127 {
128 .name = "bs",
129 .type = FIO_OPT_STR_VAL_INT,
130 .off1 = td_var_offset(bs[DDIR_READ]),
131 .off2 = td_var_offset(bs[DDIR_WRITE]),
132 .help = "Block size unit",
133 .def = "4k",
134 },
135 {
136 .name = "bsrange",
137 .type = FIO_OPT_RANGE,
138 .off1 = td_var_offset(min_bs[DDIR_READ]),
139 .off2 = td_var_offset(max_bs[DDIR_READ]),
140 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
141 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
142 .help = "Set block size range (in more detail than bs)",
143 },
144 {
145 .name = "bs_unaligned",
146 .type = FIO_OPT_STR_SET,
147 .off1 = td_var_offset(bs_unaligned),
148 .help = "Don't sector align IO buffer sizes",
149 },
150 {
151 .name = "offset",
152 .type = FIO_OPT_STR_VAL,
153 .off1 = td_var_offset(start_offset),
154 .help = "Start IO from this offset",
155 .def = "0",
156 },
157 {
158 .name = "randrepeat",
159 .type = FIO_OPT_BOOL,
160 .off1 = td_var_offset(rand_repeatable),
161 .help = "Use repeatable random IO pattern",
162 .def = "1",
163 },
164 {
165 .name = "norandommap",
166 .type = FIO_OPT_STR_SET,
167 .off1 = td_var_offset(norandommap),
168 .help = "Accept potential duplicate random blocks",
169 },
170 {
171 .name = "nrfiles",
172 .type = FIO_OPT_INT,
173 .off1 = td_var_offset(nr_files),
174 .help = "Split job workload between this number of files",
175 .def = "1",
176 },
177 {
Jens Axboe0aabe162007-02-23 08:45:55 +0100178 .name = "file_service_type",
179 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100180 .off1 = td_var_offset(file_service_type),
Jens Axboe0aabe162007-02-23 08:45:55 +0100181 .help = "How to select which file to service next",
182 .def = "roundrobin",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100183 .posval = {
184 { .ival = "random", .oval = FIO_FSERVICE_RANDOM },
185 { .ival = "roundrobin", .oval = FIO_FSERVICE_RR },
186 },
Jens Axboe0aabe162007-02-23 08:45:55 +0100187 },
188 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100189 .name = "fsync",
190 .type = FIO_OPT_INT,
191 .off1 = td_var_offset(fsync_blocks),
192 .help = "Issue fsync for writes every given number of blocks",
193 .def = "0",
194 },
195 {
196 .name = "direct",
197 .type = FIO_OPT_BOOL,
198 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100199 .help = "Use O_DIRECT IO (negates buffered)",
200 .def = "0",
201 },
202 {
203 .name = "buffered",
204 .type = FIO_OPT_BOOL,
205 .off1 = td_var_offset(odirect),
206 .neg = 1,
207 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100208 .def = "1",
209 },
210 {
211 .name = "overwrite",
212 .type = FIO_OPT_BOOL,
213 .off1 = td_var_offset(overwrite),
214 .help = "When writing, set whether to overwrite current data",
215 .def = "0",
216 },
217 {
218 .name = "loops",
219 .type = FIO_OPT_INT,
220 .off1 = td_var_offset(loops),
221 .help = "Number of times to run the job",
222 .def = "1",
223 },
224 {
225 .name = "numjobs",
226 .type = FIO_OPT_INT,
227 .off1 = td_var_offset(numjobs),
228 .help = "Duplicate this job this many times",
229 .def = "1",
230 },
231 {
232 .name = "startdelay",
233 .type = FIO_OPT_INT,
234 .off1 = td_var_offset(start_delay),
235 .help = "Only start job when this period has passed",
236 .def = "0",
237 },
238 {
239 .name = "runtime",
240 .alias = "timeout",
241 .type = FIO_OPT_STR_VAL_TIME,
242 .off1 = td_var_offset(timeout),
243 .help = "Stop workload when this amount of time has passed",
244 .def = "0",
245 },
246 {
Jens Axboee1f36502006-10-27 10:54:08 +0200247 .name = "mem",
248 .type = FIO_OPT_STR,
249 .cb = str_mem_cb,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100250 .off1 = td_var_offset(mem_type),
Jens Axboe15f79182007-01-10 12:26:18 +0100251 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100252 .def = "malloc",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100253 .posval = {
254 { .ival = "malloc", .oval = MEM_MALLOC },
255 { .ival = "shm", .oval = MEM_SHM },
256#ifdef FIO_HAVE_HUGETLB
257 { .ival = "shmhuge", .oval = MEM_SHMHUGE },
258#endif
259 { .ival = "mmap", .oval = MEM_MMAP },
260#ifdef FIO_HAVE_HUGETLB
261 { .ival = "mmaphuge", .oval = MEM_MMAPHUGE },
262#endif
263 },
Jens Axboee1f36502006-10-27 10:54:08 +0200264 },
265 {
266 .name = "verify",
267 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100268 .off1 = td_var_offset(verify),
Jens Axboe15f79182007-01-10 12:26:18 +0100269 .help = "Verify sum function",
Jens Axboeee738492007-01-10 11:23:16 +0100270 .def = "0",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100271 .posval = {
272 { .ival = "0", .oval = VERIFY_NONE },
273 { .ival = "crc32", .oval = VERIFY_CRC32 },
274 { .ival = "md5", .oval = VERIFY_MD5 },
275 },
Jens Axboee1f36502006-10-27 10:54:08 +0200276 },
277 {
278 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200279 .type = FIO_OPT_STR_STORE,
280 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100281 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200282 },
283 {
Jens Axboe076efc72006-10-27 11:24:25 +0200284 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200285 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200286 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100287 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200288 },
289 {
290 .name = "exec_prerun",
291 .type = FIO_OPT_STR_STORE,
292 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100293 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200294 },
295 {
296 .name = "exec_postrun",
297 .type = FIO_OPT_STR_STORE,
298 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100299 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200300 },
301#ifdef FIO_HAVE_IOSCHED_SWITCH
302 {
303 .name = "ioscheduler",
304 .type = FIO_OPT_STR_STORE,
305 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100306 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200307 },
308#endif
309 {
Jens Axboee1f36502006-10-27 10:54:08 +0200310 .name = "zonesize",
311 .type = FIO_OPT_STR_VAL,
312 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100313 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100314 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200315 },
316 {
317 .name = "zoneskip",
318 .type = FIO_OPT_STR_VAL,
319 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100320 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100321 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200322 },
323 {
324 .name = "lockmem",
325 .type = FIO_OPT_STR_VAL,
326 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100327 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100328 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200329 },
330 {
Jens Axboee1f36502006-10-27 10:54:08 +0200331 .name = "rwmixcycle",
332 .type = FIO_OPT_INT,
333 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100334 .help = "Cycle period for mixed read/write workloads (msec)",
335 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200336 },
337 {
338 .name = "rwmixread",
339 .type = FIO_OPT_INT,
340 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100341 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100342 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100343 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200344 },
345 {
346 .name = "rwmixwrite",
347 .type = FIO_OPT_INT,
348 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100349 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100350 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100351 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200352 },
353 {
354 .name = "nice",
355 .type = FIO_OPT_INT,
356 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100357 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100358 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100359 .maxval = 20,
360 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200361 },
362#ifdef FIO_HAVE_IOPRIO
363 {
364 .name = "prio",
365 .type = FIO_OPT_INT,
366 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100367 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100368 .minval = 0,
369 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200370 },
371 {
372 .name = "prioclass",
373 .type = FIO_OPT_INT,
374 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100375 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100376 .minval = 0,
377 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200378 },
379#endif
380 {
381 .name = "thinktime",
382 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100383 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100384 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100385 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200386 },
387 {
Jens Axboe48097d52007-02-17 06:30:44 +0100388 .name = "thinktime_spin",
389 .type = FIO_OPT_INT,
390 .off1 = td_var_offset(thinktime_spin),
391 .help = "Start thinktime by spinning this amount (usec)",
392 .def = "0",
393 },
394 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100395 .name = "thinktime_blocks",
396 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100397 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100398 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100399 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100400 },
401 {
Jens Axboee1f36502006-10-27 10:54:08 +0200402 .name = "rate",
403 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100404 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100405 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200406 },
407 {
408 .name = "ratemin",
409 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100410 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100411 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200412 },
413 {
414 .name = "ratecycle",
415 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100416 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100417 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100418 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200419 },
420 {
Jens Axboee1f36502006-10-27 10:54:08 +0200421 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100422 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100423 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100424 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100425 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200426 },
427 {
428 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100429 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100430 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100431 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100432 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200433 },
434 {
435 .name = "bwavgtime",
436 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100437 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100438 .help = "Time window over which to calculate bandwidth (msec)",
439 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200440 },
441 {
442 .name = "create_serialize",
Jens Axboe13335dd2007-01-10 13:14:02 +0100443 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100444 .off1 = td_var_offset(create_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100445 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100446 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200447 },
448 {
449 .name = "create_fsync",
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_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100452 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100453 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200454 },
455 {
Jens Axboee1f36502006-10-27 10:54:08 +0200456 .name = "cpuload",
457 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100458 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100459 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200460 },
461 {
462 .name = "cpuchunks",
463 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100464 .off1 = td_var_offset(cpucycle),
Jens Axboefd28ca42007-01-09 21:20:13 +0100465 .help = "Length of the CPU burn cycles",
Jens Axboee1f36502006-10-27 10:54:08 +0200466 },
Jens Axboee1f36502006-10-27 10:54:08 +0200467#ifdef FIO_HAVE_CPU_AFFINITY
468 {
469 .name = "cpumask",
470 .type = FIO_OPT_INT,
471 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100472 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200473 },
474#endif
475 {
476 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100477 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100478 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100479 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100480 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200481 },
482 {
483 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100484 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200485 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100486 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100487 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200488 },
489 {
490 .name = "exitall",
491 .type = FIO_OPT_STR_SET,
492 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100493 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200494 },
495 {
496 .name = "stonewall",
497 .type = FIO_OPT_STR_SET,
498 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100499 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200500 },
501 {
502 .name = "thread",
503 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100504 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100505 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200506 },
507 {
508 .name = "write_bw_log",
509 .type = FIO_OPT_STR_SET,
510 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100511 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200512 },
513 {
514 .name = "write_lat_log",
515 .type = FIO_OPT_STR_SET,
516 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100517 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200518 },
519 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100520 .name = "hugepage-size",
521 .type = FIO_OPT_STR_VAL,
522 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100523 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100524 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100525 },
526 {
Jens Axboee1f36502006-10-27 10:54:08 +0200527 .name = NULL,
528 },
529};
530
Jens Axboeb4692822006-10-27 13:43:22 +0200531#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
532#define FIO_CMD_OPTS (16)
533#define FIO_GETOPT_JOB (0x89988998)
534
535/*
536 * Command line options. These will contain the above, plus a few
537 * extra that only pertain to fio itself and not jobs.
538 */
539static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
540 {
541 .name = "output",
542 .has_arg = required_argument,
543 .val = 'o',
544 },
545 {
546 .name = "timeout",
547 .has_arg = required_argument,
548 .val = 't',
549 },
550 {
551 .name = "latency-log",
552 .has_arg = required_argument,
553 .val = 'l',
554 },
555 {
556 .name = "bandwidth-log",
557 .has_arg = required_argument,
558 .val = 'b',
559 },
560 {
561 .name = "minimal",
562 .has_arg = optional_argument,
563 .val = 'm',
564 },
565 {
566 .name = "version",
567 .has_arg = no_argument,
568 .val = 'v',
569 },
570 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100571 .name = "help",
572 .has_arg = no_argument,
573 .val = 'h',
574 },
575 {
576 .name = "cmdhelp",
577 .has_arg = required_argument,
578 .val = 'c',
579 },
580 {
Jens Axboeb4692822006-10-27 13:43:22 +0200581 .name = NULL,
582 },
583};
584
Jens Axboeee738492007-01-10 11:23:16 +0100585static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200586
Jens Axboe5cd033b2007-01-09 08:19:01 +0100587static char fio_version_string[] = "fio 1.11";
Jens Axboeebac4652005-12-08 15:25:21 +0100588
Jens Axboe972cfd22006-06-09 11:08:56 +0200589static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100590static int max_jobs = MAX_JOBS;
591
592struct thread_data def_thread;
593struct thread_data *threads = NULL;
594
Jens Axboeebac4652005-12-08 15:25:21 +0100595int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200596int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200597unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200598FILE *f_out = NULL;
599FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100600
Jens Axboeee738492007-01-10 11:23:16 +0100601static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100602int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200603
Jens Axboe906c8d72006-06-13 09:37:56 +0200604/*
605 * Return a free job structure.
606 */
Jens Axboeebac4652005-12-08 15:25:21 +0100607static struct thread_data *get_new_job(int global, struct thread_data *parent)
608{
609 struct thread_data *td;
610
611 if (global)
612 return &def_thread;
613 if (thread_number >= max_jobs)
614 return NULL;
615
616 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200617 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100618
Jens Axboeebac4652005-12-08 15:25:21 +0100619 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100620 return td;
621}
622
623static void put_job(struct thread_data *td)
624{
Jens Axboe549577a2006-10-30 12:23:41 +0100625 if (td == &def_thread)
626 return;
627
Jens Axboe16edf252007-02-10 14:59:22 +0100628 if (td->error)
629 fprintf(f_out, "fio: %s\n", td->verror);
630
Jens Axboeebac4652005-12-08 15:25:21 +0100631 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
632 thread_number--;
633}
634
Jens Axboedad915e2006-10-27 11:10:18 +0200635/*
636 * Lazy way of fixing up options that depend on each other. We could also
637 * define option callback handlers, but this is easier.
638 */
Jens Axboee1f36502006-10-27 10:54:08 +0200639static void fixup_options(struct thread_data *td)
640{
Jens Axboee1f36502006-10-27 10:54:08 +0200641 if (!td->rwmixread && td->rwmixwrite)
642 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200643
Jens Axboe076efc72006-10-27 11:24:25 +0200644 if (td->write_iolog_file && td->read_iolog_file) {
645 log_err("fio: read iolog overrides write_iolog\n");
646 free(td->write_iolog_file);
647 td->write_iolog_file = NULL;
648 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100649
650 if (td->io_ops->flags & FIO_SYNCIO)
651 td->iodepth = 1;
652 else {
653 if (!td->iodepth)
654 td->iodepth = td->nr_files;
655 }
656
657 /*
658 * only really works for sequential io for now, and with 1 file
659 */
Jens Axboe413dd452007-02-23 09:26:09 +0100660 if (td->zone_size && td_random(td) && td->nr_files == 1)
Jens Axboe16b462a2006-10-30 12:35:18 +0100661 td->zone_size = 0;
662
663 /*
664 * Reads can do overwrites, we always need to pre-create the file
665 */
666 if (td_read(td) || td_rw(td))
667 td->overwrite = 1;
668
Jens Axboea00735e2006-11-03 08:58:08 +0100669 if (!td->min_bs[DDIR_READ])
670 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
671 if (!td->max_bs[DDIR_READ])
672 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
673 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100674 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100675 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100676 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100677
678 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
679
Jens Axboe16b462a2006-10-30 12:35:18 +0100680 if (td_read(td) && !td_rw(td))
681 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100682
683 if (td->norandommap && td->verify != VERIFY_NONE) {
684 log_err("fio: norandommap given, verify disabled\n");
685 td->verify = VERIFY_NONE;
686 }
Jens Axboe690adba2006-10-30 15:25:09 +0100687 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
688 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100689
690 /*
691 * O_DIRECT and char doesn't mix, clear that flag if necessary.
692 */
693 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
694 td->odirect = 0;
Jens Axboe48097d52007-02-17 06:30:44 +0100695
696 /*
697 * thinktime_spin must be less than thinktime
698 */
699 if (td->thinktime_spin > td->thinktime)
700 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100701
702 /*
703 * The low water mark cannot be bigger than the iodepth
704 */
705 if (td->iodepth_low > td->iodepth || !td->iodepth_low)
706 td->iodepth_low = td->iodepth;
Jens Axboee1f36502006-10-27 10:54:08 +0200707}
708
Jens Axboe906c8d72006-06-13 09:37:56 +0200709/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100710 * This function leaks the buffer
711 */
712static char *to_kmg(unsigned int val)
713{
714 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100715 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100716 char *p = post;
717
Jens Axboe245142f2006-11-08 12:49:21 +0100718 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100719 if (val & 1023)
720 break;
721
722 val >>= 10;
723 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100724 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100725
726 snprintf(buf, 31, "%u%c", val, *p);
727 return buf;
728}
729
730/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200731 * Adds a job to the list of things todo. Sanitizes the various options
732 * to make sure we don't have conflicts, and initializes various
733 * members of td.
734 */
Jens Axboe75154842006-06-01 13:56:09 +0200735static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100736{
Jens Axboe413dd452007-02-23 09:26:09 +0100737 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
738 "randread", "randwrite", "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100739 struct stat sb;
Jens Axboe413dd452007-02-23 09:26:09 +0100740 int numjobs, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200741 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100742
Jens Axboeebac4652005-12-08 15:25:21 +0100743 /*
744 * the def_thread is just for options, it's not a real job
745 */
746 if (td == &def_thread)
747 return 0;
748
Jens Axboeee738492007-01-10 11:23:16 +0100749 assert(td->io_ops);
Jens Axboedf641192006-10-12 07:55:41 +0200750
Jens Axboe690adba2006-10-30 15:25:09 +0100751 if (td->odirect)
752 td->io_ops->flags |= FIO_RAWIO;
753
Jens Axboeebac4652005-12-08 15:25:21 +0100754 td->filetype = FIO_TYPE_FILE;
Jens Axboe890df532007-02-17 01:51:13 +0100755 if (td->filename && !lstat(td->filename, &sb)) {
Jens Axboe0af7b542006-02-17 10:10:12 +0100756 if (S_ISBLK(sb.st_mode))
757 td->filetype = FIO_TYPE_BD;
758 else if (S_ISCHR(sb.st_mode))
759 td->filetype = FIO_TYPE_CHAR;
760 }
Jens Axboeebac4652005-12-08 15:25:21 +0100761
Jens Axboee0a22332006-12-20 12:54:25 +0100762 fixup_options(td);
763
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200764 if (td->filename)
765 td->nr_uniq_files = 1;
766 else
767 td->nr_uniq_files = td->nr_files;
768
769 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200770 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200771 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200772
Jens Axboe16edf252007-02-10 14:59:22 +0100773 if (td->directory && td->directory[0] != '\0') {
774 if (lstat(td->directory, &sb) < 0) {
775 log_err("fio: %s is not a directory\n", td->directory);
Jens Axboee1161c32007-02-22 19:36:48 +0100776 td_verror(td, errno, "lstat");
Jens Axboe16edf252007-02-10 14:59:22 +0100777 return 1;
778 }
779 if (!S_ISDIR(sb.st_mode)) {
780 log_err("fio: %s is not a directory\n", td->directory);
781 return 1;
782 }
Jens Axboe8aeebd52007-01-08 10:47:43 +0100783 len = sprintf(tmp, "%s/", td->directory);
Jens Axboe16edf252007-02-10 14:59:22 +0100784 }
Jens Axboeebac4652005-12-08 15:25:21 +0100785
Jens Axboe53cdc682006-10-18 11:50:58 +0200786 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
787
788 for_each_file(td, f, i) {
789 memset(f, 0, sizeof(*f));
790 f->fd = -1;
791
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200792 if (td->filename)
793 sprintf(tmp + len, "%s", td->filename);
794 else
795 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200796 f->file_name = strdup(tmp);
797 }
798 } else {
799 td->nr_files = 1;
800 td->files = malloc(sizeof(struct fio_file));
801 f = &td->files[0];
802
803 memset(f, 0, sizeof(*f));
804 f->fd = -1;
805 f->file_name = strdup(jobname);
806 }
807
808 for_each_file(td, f, i) {
809 f->file_size = td->total_file_size / td->nr_files;
810 f->file_offset = td->start_offset;
811 }
812
Jens Axboebbfd6b02006-06-07 19:42:54 +0200813 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100814
Jens Axboe079ad092007-02-20 13:58:20 +0100815 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
816 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
817 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100818
Jens Axboeebac4652005-12-08 15:25:21 +0100819 if (td->stonewall && td->thread_number > 1)
820 groupid++;
821
822 td->groupid = groupid;
823
824 if (setup_rate(td))
825 goto err;
826
Jens Axboeec94ec52006-10-20 10:59:19 +0200827 if (td->write_lat_log) {
Jens Axboe079ad092007-02-20 13:58:20 +0100828 setup_log(&td->ts.slat_log);
829 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100830 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200831 if (td->write_bw_log)
Jens Axboe079ad092007-02-20 13:58:20 +0100832 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100833
Jens Axboeb4692822006-10-27 13:43:22 +0200834 if (!td->name)
835 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200836
Jens Axboec6ae0a52006-06-12 11:04:44 +0200837 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200838 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200839 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200840 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100841 else {
842 char *c1, *c2, *c3, *c4;
843
844 c1 = to_kmg(td->min_bs[DDIR_READ]);
845 c2 = to_kmg(td->max_bs[DDIR_READ]);
846 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
847 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
848
Jens Axboe413dd452007-02-23 09:26:09 +0100849 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 +0100850
851 free(c1);
852 free(c2);
853 free(c3);
854 free(c4);
855 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200856 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200857 fprintf(f_out, "...\n");
858 }
Jens Axboeebac4652005-12-08 15:25:21 +0100859
860 /*
861 * recurse add identical jobs, clear numjobs and stonewall options
862 * as they don't apply to sub-jobs
863 */
864 numjobs = td->numjobs;
865 while (--numjobs) {
866 struct thread_data *td_new = get_new_job(0, td);
867
868 if (!td_new)
869 goto err;
870
871 td_new->numjobs = 1;
872 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200873 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100874
Jens Axboe75154842006-06-01 13:56:09 +0200875 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100876 goto err;
877 }
878 return 0;
879err:
880 put_job(td);
881 return -1;
882}
883
Jens Axboe906c8d72006-06-13 09:37:56 +0200884/*
885 * Initialize the various random states we need (random io, block size ranges,
886 * read/write mix, etc).
887 */
Jens Axboeebac4652005-12-08 15:25:21 +0100888int init_random_state(struct thread_data *td)
889{
Jens Axboe0aabe162007-02-23 08:45:55 +0100890 unsigned long seeds[5];
Jens Axboe53cdc682006-10-18 11:50:58 +0200891 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200892 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100893
Jens Axboef48b4672006-10-27 11:30:07 +0200894 if (td->io_ops->flags & FIO_CPUIO)
895 return 0;
896
Jens Axboe1ac267b2006-05-31 20:29:00 +0200897 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100898 if (fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100899 td_verror(td, errno, "open");
Jens Axboeebac4652005-12-08 15:25:21 +0100900 return 1;
901 }
902
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200903 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100904 td_verror(td, EIO, "read");
Jens Axboeebac4652005-12-08 15:25:21 +0100905 close(fd);
906 return 1;
907 }
908
909 close(fd);
910
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200911 os_random_seed(seeds[0], &td->bsrange_state);
912 os_random_seed(seeds[1], &td->verify_state);
913 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100914
Jens Axboe413dd452007-02-23 09:26:09 +0100915 if (td->file_service_type == FIO_FSERVICE_RANDOM)
916 os_random_seed(seeds[3], &td->next_file_state);
917
918 if (!td_random(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100919 return 0;
920
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200921 if (td->rand_repeatable)
Jens Axboe0aabe162007-02-23 08:45:55 +0100922 seeds[4] = FIO_RANDSEED * td->thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100923
Jens Axboebb8895e2006-10-30 15:14:48 +0100924 if (!td->norandommap) {
925 for_each_file(td, f, i) {
Jens Axboea4a81712007-02-13 20:07:26 +0100926 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100927 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100928 f->file_map = malloc(num_maps * sizeof(long));
929 f->num_maps = num_maps;
930 memset(f->file_map, 0, num_maps * sizeof(long));
931 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200932 }
Jens Axboeebac4652005-12-08 15:25:21 +0100933
Jens Axboe0aabe162007-02-23 08:45:55 +0100934 os_random_seed(seeds[4], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100935 return 0;
936}
937
938static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
939{
940#ifdef FIO_HAVE_CPU_AFFINITY
941 unsigned int i;
942
943 CPU_ZERO(&cpumask);
944
945 for (i = 0; i < sizeof(int) * 8; i++) {
946 if ((1 << i) & cpu)
947 CPU_SET(i, &cpumask);
948 }
949#endif
950}
951
Jens Axboeebac4652005-12-08 15:25:21 +0100952static int is_empty_or_comment(char *line)
953{
954 unsigned int i;
955
956 for (i = 0; i < strlen(line); i++) {
957 if (line[i] == ';')
958 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100959 if (line[i] == '#')
960 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100961 if (!isspace(line[i]) && !iscntrl(line[i]))
962 return 0;
963 }
964
965 return 1;
966}
967
Jens Axboe313cb202006-12-21 09:50:00 +0100968/*
969 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
970 */
971static char *get_mmap_file(const char *str)
972{
973 char *p = strstr(str, ":");
974
975 if (!p)
976 return NULL;
977
978 p++;
979 strip_blank_front(&p);
980 strip_blank_end(p);
981 return strdup(p);
982}
983
Jens Axboeb4692822006-10-27 13:43:22 +0200984static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100985{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200986 struct thread_data *td = data;
987
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100988 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
Jens Axboe313cb202006-12-21 09:50:00 +0100989 td->mmapfile = get_mmap_file(mem);
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100990 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +0100991 log_err("fio: mmaphuge:/path/to/file\n");
992 return 1;
993 }
Jens Axboeebac4652005-12-08 15:25:21 +0100994 }
995
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100996 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100997}
998
Jens Axboeb4692822006-10-27 13:43:22 +0200999static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +01001000{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001001 struct thread_data *td = data;
1002
Jens Axboe2866c822006-10-09 15:57:48 +02001003 td->io_ops = load_ioengine(td, str);
1004 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +01001005 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001006
Jens Axboeebac4652005-12-08 15:25:21 +01001007 return 1;
1008}
1009
Jens Axboee1f36502006-10-27 10:54:08 +02001010static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1011{
1012 mlock_size = *val;
1013 return 0;
1014}
1015
Jens Axboe34cfcda2006-11-03 14:00:45 +01001016#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001017static int str_prioclass_cb(void *data, unsigned int *val)
1018{
1019 struct thread_data *td = data;
1020
1021 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1022 return 0;
1023}
1024
1025static int str_prio_cb(void *data, unsigned int *val)
1026{
1027 struct thread_data *td = data;
1028
1029 td->ioprio |= *val;
1030 return 0;
1031}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001032#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001033
1034static int str_exitall_cb(void)
1035{
1036 exitall_on_terminate = 1;
1037 return 0;
1038}
1039
1040static int str_cpumask_cb(void *data, unsigned int *val)
1041{
1042 struct thread_data *td = data;
1043
1044 fill_cpu_mask(td->cpumask, *val);
1045 return 0;
1046}
1047
Jens Axboe07261982006-06-07 10:51:12 +02001048/*
1049 * This is our [ini] type file parser.
1050 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001051static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001052{
Jens Axboee1f36502006-10-27 10:54:08 +02001053 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001054 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001055 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001056 fpos_t off;
1057 FILE *f;
1058 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001059 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001060
1061 f = fopen(file, "r");
1062 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001063 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001064 return 1;
1065 }
1066
1067 string = malloc(4096);
1068 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001069 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001070
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001071 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001072 do {
1073 p = fgets(string, 4095, f);
1074 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001075 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001076 if (is_empty_or_comment(p))
1077 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001078 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001079 continue;
1080
1081 global = !strncmp(name, "global", 6);
1082
1083 name[strlen(name) - 1] = '\0';
1084
1085 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001086 if (!td) {
1087 ret = 1;
1088 break;
1089 }
Jens Axboeebac4652005-12-08 15:25:21 +01001090
Jens Axboe972cfd22006-06-09 11:08:56 +02001091 /*
1092 * Seperate multiple job files by a stonewall
1093 */
Jens Axboef9481912006-06-09 11:37:28 +02001094 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001095 td->stonewall = stonewall;
1096 stonewall = 0;
1097 }
1098
Jens Axboeebac4652005-12-08 15:25:21 +01001099 fgetpos(f, &off);
1100 while ((p = fgets(string, 4096, f)) != NULL) {
1101 if (is_empty_or_comment(p))
1102 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001103
Jens Axboeb6754f92006-05-30 14:29:32 +02001104 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001105
1106 if (p[0] == '[')
1107 break;
1108
Jens Axboe4ae3f762006-05-30 13:35:14 +02001109 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001110
Jens Axboee1f36502006-10-27 10:54:08 +02001111 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001112
Jens Axboe45410ac2006-06-07 11:10:39 +02001113 /*
1114 * Don't break here, continue parsing options so we
1115 * dump all the bad ones. Makes trial/error fixups
1116 * easier on the user.
1117 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001118 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001119 }
Jens Axboeebac4652005-12-08 15:25:21 +01001120
Jens Axboe45410ac2006-06-07 11:10:39 +02001121 if (!ret) {
1122 fsetpos(f, &off);
1123 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001124 } else {
1125 log_err("fio: job %s dropped\n", name);
1126 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001127 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001128 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001129
1130 free(string);
1131 free(name);
1132 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001133 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001134}
1135
1136static int fill_def_thread(void)
1137{
1138 memset(&def_thread, 0, sizeof(def_thread));
1139
1140 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1141 perror("sched_getaffinity");
1142 return 1;
1143 }
1144
1145 /*
Jens Axboeee738492007-01-10 11:23:16 +01001146 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001147 */
Jens Axboeee738492007-01-10 11:23:16 +01001148 fill_default_options(&def_thread, options);
1149
Jens Axboe972cfd22006-06-09 11:08:56 +02001150 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001151 def_thread.write_bw_log = write_bw_log;
1152 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001153
Jens Axboeebac4652005-12-08 15:25:21 +01001154#ifdef FIO_HAVE_DISK_UTIL
1155 def_thread.do_disk_util = 1;
1156#endif
1157
1158 return 0;
1159}
1160
Jens Axboe0ab8db82006-10-18 17:16:23 +02001161static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001162{
1163 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001164 printf("\t--output\tWrite output to file\n");
1165 printf("\t--timeout\tRuntime in seconds\n");
1166 printf("\t--latency-log\tGenerate per-job latency logs\n");
1167 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1168 printf("\t--minimal\tMinimal (terse) output\n");
1169 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001170 printf("\t--help\t\tPrint this page\n");
1171 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001172}
1173
Jens Axboe972cfd22006-06-09 11:08:56 +02001174static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001175{
Jens Axboeb4692822006-10-27 13:43:22 +02001176 struct thread_data *td = NULL;
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001177 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001178
Jens Axboeb4692822006-10-27 13:43:22 +02001179 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001180 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001181 case 't':
1182 def_timeout = atoi(optarg);
1183 break;
1184 case 'l':
1185 write_lat_log = 1;
1186 break;
1187 case 'w':
1188 write_bw_log = 1;
1189 break;
1190 case 'o':
1191 f_out = fopen(optarg, "w+");
1192 if (!f_out) {
1193 perror("fopen output");
1194 exit(1);
1195 }
1196 f_err = f_out;
1197 break;
1198 case 'm':
1199 terse_output = 1;
1200 break;
1201 case 'h':
1202 usage();
1203 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001204 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001205 ret = show_cmd_help(options, optarg);
1206 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001207 case 'v':
1208 printf("%s\n", fio_version_string);
1209 exit(0);
1210 case FIO_GETOPT_JOB: {
1211 const char *opt = long_options[lidx].name;
1212 char *val = optarg;
1213
Jens Axboec2b1e752006-10-30 09:03:13 +01001214 if (!strncmp(opt, "name", 4) && td) {
1215 ret = add_job(td, td->name ?: "fio", 0);
1216 if (ret) {
1217 put_job(td);
1218 return 0;
1219 }
1220 td = NULL;
1221 }
Jens Axboeb4692822006-10-27 13:43:22 +02001222 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001223 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001224
1225 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001226 if (!td)
1227 return 0;
1228 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001229
Jens Axboeb1508cf2006-11-02 09:12:40 +01001230 ret = parse_cmd_option(opt, val, options, td);
Jens Axboe78217df2007-02-23 11:18:30 +01001231 if (ret)
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001232 dont_add_job = 1;
Jens Axboeb4692822006-10-27 13:43:22 +02001233 break;
1234 }
1235 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001236 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001237 }
1238 }
Jens Axboec9fad892006-05-27 17:26:50 +02001239
Jens Axboeb4692822006-10-27 13:43:22 +02001240 if (td) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001241 if (dont_add_job)
Jens Axboeb4692822006-10-27 13:43:22 +02001242 put_job(td);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001243 else {
1244 ret = add_job(td, td->name ?: "fio", 0);
1245 if (ret)
1246 put_job(td);
1247 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001248 }
Jens Axboe774a6172006-08-24 08:44:26 +02001249
Jens Axboeb4692822006-10-27 13:43:22 +02001250 while (optind < argc) {
1251 ini_idx++;
1252 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1253 ini_file[ini_idx - 1] = strdup(argv[optind]);
1254 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001255 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001256
1257 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001258}
1259
1260static void free_shm(void)
1261{
1262 struct shmid_ds sbuf;
1263
1264 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001265 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001266 threads = NULL;
1267 shmctl(shm_id, IPC_RMID, &sbuf);
1268 }
1269}
1270
Jens Axboe906c8d72006-06-13 09:37:56 +02001271/*
1272 * The thread area is shared between the main process and the job
1273 * threads/processes. So setup a shared memory segment that will hold
1274 * all the job info.
1275 */
Jens Axboeebac4652005-12-08 15:25:21 +01001276static int setup_thread_area(void)
1277{
1278 /*
1279 * 1024 is too much on some machines, scale max_jobs if
1280 * we get a failure that looks like too large a shm segment
1281 */
1282 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001283 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001284
Jens Axboe906c8d72006-06-13 09:37:56 +02001285 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001286 if (shm_id != -1)
1287 break;
1288 if (errno != EINVAL) {
1289 perror("shmget");
1290 break;
1291 }
1292
1293 max_jobs >>= 1;
1294 } while (max_jobs);
1295
1296 if (shm_id == -1)
1297 return 1;
1298
1299 threads = shmat(shm_id, NULL, 0);
1300 if (threads == (void *) -1) {
1301 perror("shmat");
1302 return 1;
1303 }
1304
1305 atexit(free_shm);
1306 return 0;
1307}
1308
Jens Axboeb4692822006-10-27 13:43:22 +02001309/*
1310 * Copy the fio options into the long options map, so we mirror
1311 * job and cmd line options.
1312 */
1313static void dupe_job_options(void)
1314{
1315 struct fio_option *o;
1316 unsigned int i;
1317
1318 i = 0;
1319 while (long_options[i].name)
1320 i++;
1321
1322 o = &options[0];
1323 while (o->name) {
1324 long_options[i].name = o->name;
1325 long_options[i].val = FIO_GETOPT_JOB;
1326 if (o->type == FIO_OPT_STR_SET)
1327 long_options[i].has_arg = no_argument;
1328 else
1329 long_options[i].has_arg = required_argument;
1330
1331 i++;
1332 o++;
1333 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1334 }
1335}
1336
Jens Axboeebac4652005-12-08 15:25:21 +01001337int parse_options(int argc, char *argv[])
1338{
Jens Axboe972cfd22006-06-09 11:08:56 +02001339 int job_files, i;
1340
Jens Axboeb4692822006-10-27 13:43:22 +02001341 f_out = stdout;
1342 f_err = stderr;
1343
Jens Axboe13335dd2007-01-10 13:14:02 +01001344 options_init(options);
1345
Jens Axboeb4692822006-10-27 13:43:22 +02001346 dupe_job_options();
1347
Jens Axboeebac4652005-12-08 15:25:21 +01001348 if (setup_thread_area())
1349 return 1;
1350 if (fill_def_thread())
1351 return 1;
1352
Jens Axboe972cfd22006-06-09 11:08:56 +02001353 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001354
Jens Axboe972cfd22006-06-09 11:08:56 +02001355 for (i = 0; i < job_files; i++) {
1356 if (fill_def_thread())
1357 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001358 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001359 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001360 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001361 }
Jens Axboeebac4652005-12-08 15:25:21 +01001362
Jens Axboe88c6ed82006-06-09 11:28:10 +02001363 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001364
1365 if (!thread_number) {
1366 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001367 return 1;
1368 }
1369
Jens Axboeebac4652005-12-08 15:25:21 +01001370 return 0;
1371}