blob: 5588c852b9c828cc49b31c4ae4a43758efadd3dc [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 = {
88 { .ival = "sync", }, { .ival = "libaio", },
89 { .ival = "posixaio", }, { .ival = "mmap", },
90 { .ival = "splice", }, { .ival = "sg", },
91 { .ival = "null", }, { .ival = "net", },
92 { .ival = "syslet-rw", },
93 },
Jens Axboee1f36502006-10-27 10:54:08 +020094 },
95 {
Jens Axboe03b74b32007-01-11 11:04:31 +010096 .name = "iodepth",
97 .type = FIO_OPT_INT,
98 .off1 = td_var_offset(iodepth),
99 .help = "Amount of IO buffers to keep in flight",
100 .def = "1",
101 },
102 {
Jens Axboee916b392007-02-20 14:37:26 +0100103 .name = "iodepth_low",
104 .type = FIO_OPT_INT,
105 .off1 = td_var_offset(iodepth_low),
106 .help = "Low water mark for queuing depth",
107 },
108 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100109 .name = "size",
110 .type = FIO_OPT_STR_VAL,
111 .off1 = td_var_offset(total_file_size),
112 .help = "Size of device or file",
113 },
114 {
115 .name = "bs",
116 .type = FIO_OPT_STR_VAL_INT,
117 .off1 = td_var_offset(bs[DDIR_READ]),
118 .off2 = td_var_offset(bs[DDIR_WRITE]),
119 .help = "Block size unit",
120 .def = "4k",
121 },
122 {
123 .name = "bsrange",
124 .type = FIO_OPT_RANGE,
125 .off1 = td_var_offset(min_bs[DDIR_READ]),
126 .off2 = td_var_offset(max_bs[DDIR_READ]),
127 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
128 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
129 .help = "Set block size range (in more detail than bs)",
130 },
131 {
132 .name = "bs_unaligned",
133 .type = FIO_OPT_STR_SET,
134 .off1 = td_var_offset(bs_unaligned),
135 .help = "Don't sector align IO buffer sizes",
136 },
137 {
138 .name = "offset",
139 .type = FIO_OPT_STR_VAL,
140 .off1 = td_var_offset(start_offset),
141 .help = "Start IO from this offset",
142 .def = "0",
143 },
144 {
145 .name = "randrepeat",
146 .type = FIO_OPT_BOOL,
147 .off1 = td_var_offset(rand_repeatable),
148 .help = "Use repeatable random IO pattern",
149 .def = "1",
150 },
151 {
152 .name = "norandommap",
153 .type = FIO_OPT_STR_SET,
154 .off1 = td_var_offset(norandommap),
155 .help = "Accept potential duplicate random blocks",
156 },
157 {
158 .name = "nrfiles",
159 .type = FIO_OPT_INT,
160 .off1 = td_var_offset(nr_files),
161 .help = "Split job workload between this number of files",
162 .def = "1",
163 },
164 {
Jens Axboe0aabe162007-02-23 08:45:55 +0100165 .name = "file_service_type",
166 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100167 .off1 = td_var_offset(file_service_type),
Jens Axboe0aabe162007-02-23 08:45:55 +0100168 .help = "How to select which file to service next",
169 .def = "roundrobin",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100170 .posval = {
171 { .ival = "random", .oval = FIO_FSERVICE_RANDOM },
172 { .ival = "roundrobin", .oval = FIO_FSERVICE_RR },
173 },
Jens Axboe0aabe162007-02-23 08:45:55 +0100174 },
175 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100176 .name = "fsync",
177 .type = FIO_OPT_INT,
178 .off1 = td_var_offset(fsync_blocks),
179 .help = "Issue fsync for writes every given number of blocks",
180 .def = "0",
181 },
182 {
183 .name = "direct",
184 .type = FIO_OPT_BOOL,
185 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100186 .help = "Use O_DIRECT IO (negates buffered)",
187 .def = "0",
188 },
189 {
190 .name = "buffered",
191 .type = FIO_OPT_BOOL,
192 .off1 = td_var_offset(odirect),
193 .neg = 1,
194 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100195 .def = "1",
196 },
197 {
198 .name = "overwrite",
199 .type = FIO_OPT_BOOL,
200 .off1 = td_var_offset(overwrite),
201 .help = "When writing, set whether to overwrite current data",
202 .def = "0",
203 },
204 {
205 .name = "loops",
206 .type = FIO_OPT_INT,
207 .off1 = td_var_offset(loops),
208 .help = "Number of times to run the job",
209 .def = "1",
210 },
211 {
212 .name = "numjobs",
213 .type = FIO_OPT_INT,
214 .off1 = td_var_offset(numjobs),
215 .help = "Duplicate this job this many times",
216 .def = "1",
217 },
218 {
219 .name = "startdelay",
220 .type = FIO_OPT_INT,
221 .off1 = td_var_offset(start_delay),
222 .help = "Only start job when this period has passed",
223 .def = "0",
224 },
225 {
226 .name = "runtime",
227 .alias = "timeout",
228 .type = FIO_OPT_STR_VAL_TIME,
229 .off1 = td_var_offset(timeout),
230 .help = "Stop workload when this amount of time has passed",
231 .def = "0",
232 },
233 {
Jens Axboee1f36502006-10-27 10:54:08 +0200234 .name = "mem",
235 .type = FIO_OPT_STR,
236 .cb = str_mem_cb,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100237 .off1 = td_var_offset(mem_type),
Jens Axboe15f79182007-01-10 12:26:18 +0100238 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100239 .def = "malloc",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100240 .posval = {
241 { .ival = "malloc", .oval = MEM_MALLOC },
242 { .ival = "shm", .oval = MEM_SHM },
243#ifdef FIO_HAVE_HUGETLB
244 { .ival = "shmhuge", .oval = MEM_SHMHUGE },
245#endif
246 { .ival = "mmap", .oval = MEM_MMAP },
247#ifdef FIO_HAVE_HUGETLB
248 { .ival = "mmaphuge", .oval = MEM_MMAPHUGE },
249#endif
250 },
Jens Axboee1f36502006-10-27 10:54:08 +0200251 },
252 {
253 .name = "verify",
254 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100255 .off1 = td_var_offset(verify),
Jens Axboe15f79182007-01-10 12:26:18 +0100256 .help = "Verify sum function",
Jens Axboeee738492007-01-10 11:23:16 +0100257 .def = "0",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100258 .posval = {
259 { .ival = "0", .oval = VERIFY_NONE },
260 { .ival = "crc32", .oval = VERIFY_CRC32 },
261 { .ival = "md5", .oval = VERIFY_MD5 },
262 },
Jens Axboee1f36502006-10-27 10:54:08 +0200263 },
264 {
265 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200266 .type = FIO_OPT_STR_STORE,
267 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100268 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200269 },
270 {
Jens Axboe076efc72006-10-27 11:24:25 +0200271 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200272 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200273 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100274 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200275 },
276 {
277 .name = "exec_prerun",
278 .type = FIO_OPT_STR_STORE,
279 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100280 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200281 },
282 {
283 .name = "exec_postrun",
284 .type = FIO_OPT_STR_STORE,
285 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100286 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200287 },
288#ifdef FIO_HAVE_IOSCHED_SWITCH
289 {
290 .name = "ioscheduler",
291 .type = FIO_OPT_STR_STORE,
292 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100293 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200294 },
295#endif
296 {
Jens Axboee1f36502006-10-27 10:54:08 +0200297 .name = "zonesize",
298 .type = FIO_OPT_STR_VAL,
299 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100300 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100301 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200302 },
303 {
304 .name = "zoneskip",
305 .type = FIO_OPT_STR_VAL,
306 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100307 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100308 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200309 },
310 {
311 .name = "lockmem",
312 .type = FIO_OPT_STR_VAL,
313 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100314 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100315 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200316 },
317 {
Jens Axboee1f36502006-10-27 10:54:08 +0200318 .name = "rwmixcycle",
319 .type = FIO_OPT_INT,
320 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100321 .help = "Cycle period for mixed read/write workloads (msec)",
322 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200323 },
324 {
325 .name = "rwmixread",
326 .type = FIO_OPT_INT,
327 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100328 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100329 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100330 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200331 },
332 {
333 .name = "rwmixwrite",
334 .type = FIO_OPT_INT,
335 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100336 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100337 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100338 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200339 },
340 {
341 .name = "nice",
342 .type = FIO_OPT_INT,
343 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100344 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100345 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100346 .maxval = 20,
347 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200348 },
349#ifdef FIO_HAVE_IOPRIO
350 {
351 .name = "prio",
352 .type = FIO_OPT_INT,
353 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100354 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100355 .minval = 0,
356 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200357 },
358 {
359 .name = "prioclass",
360 .type = FIO_OPT_INT,
361 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100362 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100363 .minval = 0,
364 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200365 },
366#endif
367 {
368 .name = "thinktime",
369 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100370 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100371 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100372 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200373 },
374 {
Jens Axboe48097d52007-02-17 06:30:44 +0100375 .name = "thinktime_spin",
376 .type = FIO_OPT_INT,
377 .off1 = td_var_offset(thinktime_spin),
378 .help = "Start thinktime by spinning this amount (usec)",
379 .def = "0",
380 },
381 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100382 .name = "thinktime_blocks",
383 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100384 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100385 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100386 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100387 },
388 {
Jens Axboee1f36502006-10-27 10:54:08 +0200389 .name = "rate",
390 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100391 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100392 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200393 },
394 {
395 .name = "ratemin",
396 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100397 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100398 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200399 },
400 {
401 .name = "ratecycle",
402 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100403 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100404 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100405 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200406 },
407 {
Jens Axboee1f36502006-10-27 10:54:08 +0200408 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100409 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100410 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100411 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100412 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200413 },
414 {
415 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100416 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100417 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100418 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100419 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200420 },
421 {
422 .name = "bwavgtime",
423 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100424 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100425 .help = "Time window over which to calculate bandwidth (msec)",
426 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200427 },
428 {
429 .name = "create_serialize",
Jens Axboe13335dd2007-01-10 13:14:02 +0100430 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100431 .off1 = td_var_offset(create_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100432 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100433 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200434 },
435 {
436 .name = "create_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100437 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100438 .off1 = td_var_offset(create_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100439 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100440 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200441 },
442 {
Jens Axboee1f36502006-10-27 10:54:08 +0200443 .name = "cpuload",
444 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100445 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100446 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200447 },
448 {
449 .name = "cpuchunks",
450 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100451 .off1 = td_var_offset(cpucycle),
Jens Axboefd28ca42007-01-09 21:20:13 +0100452 .help = "Length of the CPU burn cycles",
Jens Axboee1f36502006-10-27 10:54:08 +0200453 },
Jens Axboee1f36502006-10-27 10:54:08 +0200454#ifdef FIO_HAVE_CPU_AFFINITY
455 {
456 .name = "cpumask",
457 .type = FIO_OPT_INT,
458 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100459 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200460 },
461#endif
462 {
463 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100464 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100465 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100466 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100467 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200468 },
469 {
470 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100471 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200472 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100473 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100474 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200475 },
476 {
477 .name = "exitall",
478 .type = FIO_OPT_STR_SET,
479 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100480 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200481 },
482 {
483 .name = "stonewall",
484 .type = FIO_OPT_STR_SET,
485 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100486 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200487 },
488 {
489 .name = "thread",
490 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100491 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100492 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200493 },
494 {
495 .name = "write_bw_log",
496 .type = FIO_OPT_STR_SET,
497 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100498 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200499 },
500 {
501 .name = "write_lat_log",
502 .type = FIO_OPT_STR_SET,
503 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100504 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200505 },
506 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100507 .name = "hugepage-size",
508 .type = FIO_OPT_STR_VAL,
509 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100510 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100511 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100512 },
513 {
Jens Axboee1f36502006-10-27 10:54:08 +0200514 .name = NULL,
515 },
516};
517
Jens Axboeb4692822006-10-27 13:43:22 +0200518#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
519#define FIO_CMD_OPTS (16)
520#define FIO_GETOPT_JOB (0x89988998)
521
522/*
523 * Command line options. These will contain the above, plus a few
524 * extra that only pertain to fio itself and not jobs.
525 */
526static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
527 {
528 .name = "output",
529 .has_arg = required_argument,
530 .val = 'o',
531 },
532 {
533 .name = "timeout",
534 .has_arg = required_argument,
535 .val = 't',
536 },
537 {
538 .name = "latency-log",
539 .has_arg = required_argument,
540 .val = 'l',
541 },
542 {
543 .name = "bandwidth-log",
544 .has_arg = required_argument,
545 .val = 'b',
546 },
547 {
548 .name = "minimal",
549 .has_arg = optional_argument,
550 .val = 'm',
551 },
552 {
553 .name = "version",
554 .has_arg = no_argument,
555 .val = 'v',
556 },
557 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100558 .name = "help",
559 .has_arg = no_argument,
560 .val = 'h',
561 },
562 {
563 .name = "cmdhelp",
564 .has_arg = required_argument,
565 .val = 'c',
566 },
567 {
Jens Axboeb4692822006-10-27 13:43:22 +0200568 .name = NULL,
569 },
570};
571
Jens Axboeee738492007-01-10 11:23:16 +0100572static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200573
Jens Axboe5cd033b2007-01-09 08:19:01 +0100574static char fio_version_string[] = "fio 1.11";
Jens Axboeebac4652005-12-08 15:25:21 +0100575
Jens Axboe972cfd22006-06-09 11:08:56 +0200576static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100577static int max_jobs = MAX_JOBS;
578
579struct thread_data def_thread;
580struct thread_data *threads = NULL;
581
Jens Axboeebac4652005-12-08 15:25:21 +0100582int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200583int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200584unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200585FILE *f_out = NULL;
586FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100587
Jens Axboeee738492007-01-10 11:23:16 +0100588static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100589int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200590
Jens Axboe906c8d72006-06-13 09:37:56 +0200591/*
592 * Return a free job structure.
593 */
Jens Axboeebac4652005-12-08 15:25:21 +0100594static struct thread_data *get_new_job(int global, struct thread_data *parent)
595{
596 struct thread_data *td;
597
598 if (global)
599 return &def_thread;
600 if (thread_number >= max_jobs)
601 return NULL;
602
603 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200604 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100605
Jens Axboeebac4652005-12-08 15:25:21 +0100606 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100607 return td;
608}
609
610static void put_job(struct thread_data *td)
611{
Jens Axboe549577a2006-10-30 12:23:41 +0100612 if (td == &def_thread)
613 return;
614
Jens Axboe16edf252007-02-10 14:59:22 +0100615 if (td->error)
616 fprintf(f_out, "fio: %s\n", td->verror);
617
Jens Axboeebac4652005-12-08 15:25:21 +0100618 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
619 thread_number--;
620}
621
Jens Axboedad915e2006-10-27 11:10:18 +0200622/*
623 * Lazy way of fixing up options that depend on each other. We could also
624 * define option callback handlers, but this is easier.
625 */
Jens Axboee1f36502006-10-27 10:54:08 +0200626static void fixup_options(struct thread_data *td)
627{
Jens Axboee1f36502006-10-27 10:54:08 +0200628 if (!td->rwmixread && td->rwmixwrite)
629 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200630
Jens Axboe076efc72006-10-27 11:24:25 +0200631 if (td->write_iolog_file && td->read_iolog_file) {
632 log_err("fio: read iolog overrides write_iolog\n");
633 free(td->write_iolog_file);
634 td->write_iolog_file = NULL;
635 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100636
637 if (td->io_ops->flags & FIO_SYNCIO)
638 td->iodepth = 1;
639 else {
640 if (!td->iodepth)
641 td->iodepth = td->nr_files;
642 }
643
644 /*
645 * only really works for sequential io for now, and with 1 file
646 */
Jens Axboe413dd452007-02-23 09:26:09 +0100647 if (td->zone_size && td_random(td) && td->nr_files == 1)
Jens Axboe16b462a2006-10-30 12:35:18 +0100648 td->zone_size = 0;
649
650 /*
651 * Reads can do overwrites, we always need to pre-create the file
652 */
653 if (td_read(td) || td_rw(td))
654 td->overwrite = 1;
655
Jens Axboea00735e2006-11-03 08:58:08 +0100656 if (!td->min_bs[DDIR_READ])
657 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
658 if (!td->max_bs[DDIR_READ])
659 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
660 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100661 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100662 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100663 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100664
665 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
666
Jens Axboe16b462a2006-10-30 12:35:18 +0100667 if (td_read(td) && !td_rw(td))
668 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100669
670 if (td->norandommap && td->verify != VERIFY_NONE) {
671 log_err("fio: norandommap given, verify disabled\n");
672 td->verify = VERIFY_NONE;
673 }
Jens Axboe690adba2006-10-30 15:25:09 +0100674 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
675 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100676
677 /*
678 * O_DIRECT and char doesn't mix, clear that flag if necessary.
679 */
680 if (td->filetype == FIO_TYPE_CHAR && td->odirect)
681 td->odirect = 0;
Jens Axboe48097d52007-02-17 06:30:44 +0100682
683 /*
684 * thinktime_spin must be less than thinktime
685 */
686 if (td->thinktime_spin > td->thinktime)
687 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100688
689 /*
690 * The low water mark cannot be bigger than the iodepth
691 */
692 if (td->iodepth_low > td->iodepth || !td->iodepth_low)
693 td->iodepth_low = td->iodepth;
Jens Axboee1f36502006-10-27 10:54:08 +0200694}
695
Jens Axboe906c8d72006-06-13 09:37:56 +0200696/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100697 * This function leaks the buffer
698 */
699static char *to_kmg(unsigned int val)
700{
701 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100702 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100703 char *p = post;
704
Jens Axboe245142f2006-11-08 12:49:21 +0100705 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100706 if (val & 1023)
707 break;
708
709 val >>= 10;
710 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100711 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100712
713 snprintf(buf, 31, "%u%c", val, *p);
714 return buf;
715}
716
717/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200718 * Adds a job to the list of things todo. Sanitizes the various options
719 * to make sure we don't have conflicts, and initializes various
720 * members of td.
721 */
Jens Axboe75154842006-06-01 13:56:09 +0200722static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100723{
Jens Axboe413dd452007-02-23 09:26:09 +0100724 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
725 "randread", "randwrite", "randrw" };
Jens Axboeebac4652005-12-08 15:25:21 +0100726 struct stat sb;
Jens Axboe413dd452007-02-23 09:26:09 +0100727 int numjobs, i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200728 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100729
Jens Axboeebac4652005-12-08 15:25:21 +0100730 /*
731 * the def_thread is just for options, it's not a real job
732 */
733 if (td == &def_thread)
734 return 0;
735
Jens Axboeee738492007-01-10 11:23:16 +0100736 assert(td->io_ops);
Jens Axboedf641192006-10-12 07:55:41 +0200737
Jens Axboe690adba2006-10-30 15:25:09 +0100738 if (td->odirect)
739 td->io_ops->flags |= FIO_RAWIO;
740
Jens Axboeebac4652005-12-08 15:25:21 +0100741 td->filetype = FIO_TYPE_FILE;
Jens Axboe890df532007-02-17 01:51:13 +0100742 if (td->filename && !lstat(td->filename, &sb)) {
Jens Axboe0af7b542006-02-17 10:10:12 +0100743 if (S_ISBLK(sb.st_mode))
744 td->filetype = FIO_TYPE_BD;
745 else if (S_ISCHR(sb.st_mode))
746 td->filetype = FIO_TYPE_CHAR;
747 }
Jens Axboeebac4652005-12-08 15:25:21 +0100748
Jens Axboee0a22332006-12-20 12:54:25 +0100749 fixup_options(td);
750
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200751 if (td->filename)
752 td->nr_uniq_files = 1;
753 else
754 td->nr_uniq_files = td->nr_files;
755
756 if (td->filetype == FIO_TYPE_FILE || td->filename) {
Jens Axboee9c047a2006-06-07 21:13:04 +0200757 char tmp[PATH_MAX];
Jens Axboe53cdc682006-10-18 11:50:58 +0200758 int len = 0;
Jens Axboee9c047a2006-06-07 21:13:04 +0200759
Jens Axboe16edf252007-02-10 14:59:22 +0100760 if (td->directory && td->directory[0] != '\0') {
761 if (lstat(td->directory, &sb) < 0) {
762 log_err("fio: %s is not a directory\n", td->directory);
Jens Axboee1161c32007-02-22 19:36:48 +0100763 td_verror(td, errno, "lstat");
Jens Axboe16edf252007-02-10 14:59:22 +0100764 return 1;
765 }
766 if (!S_ISDIR(sb.st_mode)) {
767 log_err("fio: %s is not a directory\n", td->directory);
768 return 1;
769 }
Jens Axboe8aeebd52007-01-08 10:47:43 +0100770 len = sprintf(tmp, "%s/", td->directory);
Jens Axboe16edf252007-02-10 14:59:22 +0100771 }
Jens Axboeebac4652005-12-08 15:25:21 +0100772
Jens Axboe53cdc682006-10-18 11:50:58 +0200773 td->files = malloc(sizeof(struct fio_file) * td->nr_files);
774
775 for_each_file(td, f, i) {
776 memset(f, 0, sizeof(*f));
777 f->fd = -1;
778
Jens Axboe13f8e2d2006-10-24 09:29:45 +0200779 if (td->filename)
780 sprintf(tmp + len, "%s", td->filename);
781 else
782 sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe53cdc682006-10-18 11:50:58 +0200783 f->file_name = strdup(tmp);
784 }
785 } else {
786 td->nr_files = 1;
787 td->files = malloc(sizeof(struct fio_file));
788 f = &td->files[0];
789
790 memset(f, 0, sizeof(*f));
791 f->fd = -1;
792 f->file_name = strdup(jobname);
793 }
794
795 for_each_file(td, f, i) {
796 f->file_size = td->total_file_size / td->nr_files;
797 f->file_offset = td->start_offset;
798 }
799
Jens Axboebbfd6b02006-06-07 19:42:54 +0200800 fio_sem_init(&td->mutex, 0);
Jens Axboeebac4652005-12-08 15:25:21 +0100801
Jens Axboe079ad092007-02-20 13:58:20 +0100802 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
803 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
804 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100805
Jens Axboeebac4652005-12-08 15:25:21 +0100806 if (td->stonewall && td->thread_number > 1)
807 groupid++;
808
809 td->groupid = groupid;
810
811 if (setup_rate(td))
812 goto err;
813
Jens Axboeec94ec52006-10-20 10:59:19 +0200814 if (td->write_lat_log) {
Jens Axboe079ad092007-02-20 13:58:20 +0100815 setup_log(&td->ts.slat_log);
816 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100817 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200818 if (td->write_bw_log)
Jens Axboe079ad092007-02-20 13:58:20 +0100819 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100820
Jens Axboeb4692822006-10-27 13:43:22 +0200821 if (!td->name)
822 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200823
Jens Axboec6ae0a52006-06-12 11:04:44 +0200824 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200825 if (!job_add_num) {
Jens Axboe2866c822006-10-09 15:57:48 +0200826 if (td->io_ops->flags & FIO_CPUIO)
Jens Axboeb990b5c2006-09-14 09:48:22 +0200827 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100828 else {
829 char *c1, *c2, *c3, *c4;
830
831 c1 = to_kmg(td->min_bs[DDIR_READ]);
832 c2 = to_kmg(td->max_bs[DDIR_READ]);
833 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
834 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
835
Jens Axboe413dd452007-02-23 09:26:09 +0100836 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 +0100837
838 free(c1);
839 free(c2);
840 free(c3);
841 free(c4);
842 }
Jens Axboeb990b5c2006-09-14 09:48:22 +0200843 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +0200844 fprintf(f_out, "...\n");
845 }
Jens Axboeebac4652005-12-08 15:25:21 +0100846
847 /*
848 * recurse add identical jobs, clear numjobs and stonewall options
849 * as they don't apply to sub-jobs
850 */
851 numjobs = td->numjobs;
852 while (--numjobs) {
853 struct thread_data *td_new = get_new_job(0, td);
854
855 if (!td_new)
856 goto err;
857
858 td_new->numjobs = 1;
859 td_new->stonewall = 0;
Jens Axboe75154842006-06-01 13:56:09 +0200860 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100861
Jens Axboe75154842006-06-01 13:56:09 +0200862 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +0100863 goto err;
864 }
865 return 0;
866err:
867 put_job(td);
868 return -1;
869}
870
Jens Axboe906c8d72006-06-13 09:37:56 +0200871/*
872 * Initialize the various random states we need (random io, block size ranges,
873 * read/write mix, etc).
874 */
Jens Axboeebac4652005-12-08 15:25:21 +0100875int init_random_state(struct thread_data *td)
876{
Jens Axboe0aabe162007-02-23 08:45:55 +0100877 unsigned long seeds[5];
Jens Axboe53cdc682006-10-18 11:50:58 +0200878 int fd, num_maps, blocks, i;
Jens Axboe0ab8db82006-10-18 17:16:23 +0200879 struct fio_file *f;
Jens Axboeebac4652005-12-08 15:25:21 +0100880
Jens Axboef48b4672006-10-27 11:30:07 +0200881 if (td->io_ops->flags & FIO_CPUIO)
882 return 0;
883
Jens Axboe1ac267b2006-05-31 20:29:00 +0200884 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +0100885 if (fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +0100886 td_verror(td, errno, "open");
Jens Axboeebac4652005-12-08 15:25:21 +0100887 return 1;
888 }
889
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200890 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboee1161c32007-02-22 19:36:48 +0100891 td_verror(td, EIO, "read");
Jens Axboeebac4652005-12-08 15:25:21 +0100892 close(fd);
893 return 1;
894 }
895
896 close(fd);
897
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200898 os_random_seed(seeds[0], &td->bsrange_state);
899 os_random_seed(seeds[1], &td->verify_state);
900 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100901
Jens Axboe413dd452007-02-23 09:26:09 +0100902 if (td->file_service_type == FIO_FSERVICE_RANDOM)
903 os_random_seed(seeds[3], &td->next_file_state);
904
905 if (!td_random(td))
Jens Axboeebac4652005-12-08 15:25:21 +0100906 return 0;
907
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200908 if (td->rand_repeatable)
Jens Axboe0aabe162007-02-23 08:45:55 +0100909 seeds[4] = FIO_RANDSEED * td->thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100910
Jens Axboebb8895e2006-10-30 15:14:48 +0100911 if (!td->norandommap) {
912 for_each_file(td, f, i) {
Jens Axboea4a81712007-02-13 20:07:26 +0100913 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +0100914 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +0100915 f->file_map = malloc(num_maps * sizeof(long));
916 f->num_maps = num_maps;
917 memset(f->file_map, 0, num_maps * sizeof(long));
918 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200919 }
Jens Axboeebac4652005-12-08 15:25:21 +0100920
Jens Axboe0aabe162007-02-23 08:45:55 +0100921 os_random_seed(seeds[4], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +0100922 return 0;
923}
924
925static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
926{
927#ifdef FIO_HAVE_CPU_AFFINITY
928 unsigned int i;
929
930 CPU_ZERO(&cpumask);
931
932 for (i = 0; i < sizeof(int) * 8; i++) {
933 if ((1 << i) & cpu)
934 CPU_SET(i, &cpumask);
935 }
936#endif
937}
938
Jens Axboeebac4652005-12-08 15:25:21 +0100939static int is_empty_or_comment(char *line)
940{
941 unsigned int i;
942
943 for (i = 0; i < strlen(line); i++) {
944 if (line[i] == ';')
945 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +0100946 if (line[i] == '#')
947 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +0100948 if (!isspace(line[i]) && !iscntrl(line[i]))
949 return 0;
950 }
951
952 return 1;
953}
954
Jens Axboe313cb202006-12-21 09:50:00 +0100955/*
956 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
957 */
958static char *get_mmap_file(const char *str)
959{
960 char *p = strstr(str, ":");
961
962 if (!p)
963 return NULL;
964
965 p++;
966 strip_blank_front(&p);
967 strip_blank_end(p);
968 return strdup(p);
969}
970
Jens Axboeb4692822006-10-27 13:43:22 +0200971static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +0100972{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200973 struct thread_data *td = data;
974
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100975 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
Jens Axboe313cb202006-12-21 09:50:00 +0100976 td->mmapfile = get_mmap_file(mem);
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100977 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +0100978 log_err("fio: mmaphuge:/path/to/file\n");
979 return 1;
980 }
Jens Axboeebac4652005-12-08 15:25:21 +0100981 }
982
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100983 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100984}
985
Jens Axboeb4692822006-10-27 13:43:22 +0200986static int str_ioengine_cb(void *data, const char *str)
Jens Axboeebac4652005-12-08 15:25:21 +0100987{
Jens Axboecb2c86f2006-10-26 13:48:34 +0200988 struct thread_data *td = data;
989
Jens Axboe2866c822006-10-09 15:57:48 +0200990 td->io_ops = load_ioengine(td, str);
991 if (td->io_ops)
Jens Axboeebac4652005-12-08 15:25:21 +0100992 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100993
Jens Axboeebac4652005-12-08 15:25:21 +0100994 return 1;
995}
996
Jens Axboee1f36502006-10-27 10:54:08 +0200997static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
998{
999 mlock_size = *val;
1000 return 0;
1001}
1002
Jens Axboe34cfcda2006-11-03 14:00:45 +01001003#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001004static int str_prioclass_cb(void *data, unsigned int *val)
1005{
1006 struct thread_data *td = data;
1007
1008 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1009 return 0;
1010}
1011
1012static int str_prio_cb(void *data, unsigned int *val)
1013{
1014 struct thread_data *td = data;
1015
1016 td->ioprio |= *val;
1017 return 0;
1018}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001019#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001020
1021static int str_exitall_cb(void)
1022{
1023 exitall_on_terminate = 1;
1024 return 0;
1025}
1026
1027static int str_cpumask_cb(void *data, unsigned int *val)
1028{
1029 struct thread_data *td = data;
1030
1031 fill_cpu_mask(td->cpumask, *val);
1032 return 0;
1033}
1034
Jens Axboe07261982006-06-07 10:51:12 +02001035/*
1036 * This is our [ini] type file parser.
1037 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001038static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001039{
Jens Axboee1f36502006-10-27 10:54:08 +02001040 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001041 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001042 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001043 fpos_t off;
1044 FILE *f;
1045 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001046 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001047
1048 f = fopen(file, "r");
1049 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001050 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001051 return 1;
1052 }
1053
1054 string = malloc(4096);
1055 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001056 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001057
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001058 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001059 do {
1060 p = fgets(string, 4095, f);
1061 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001062 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001063 if (is_empty_or_comment(p))
1064 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001065 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001066 continue;
1067
1068 global = !strncmp(name, "global", 6);
1069
1070 name[strlen(name) - 1] = '\0';
1071
1072 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001073 if (!td) {
1074 ret = 1;
1075 break;
1076 }
Jens Axboeebac4652005-12-08 15:25:21 +01001077
Jens Axboe972cfd22006-06-09 11:08:56 +02001078 /*
1079 * Seperate multiple job files by a stonewall
1080 */
Jens Axboef9481912006-06-09 11:37:28 +02001081 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001082 td->stonewall = stonewall;
1083 stonewall = 0;
1084 }
1085
Jens Axboeebac4652005-12-08 15:25:21 +01001086 fgetpos(f, &off);
1087 while ((p = fgets(string, 4096, f)) != NULL) {
1088 if (is_empty_or_comment(p))
1089 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001090
Jens Axboeb6754f92006-05-30 14:29:32 +02001091 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001092
1093 if (p[0] == '[')
1094 break;
1095
Jens Axboe4ae3f762006-05-30 13:35:14 +02001096 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001097
Jens Axboee1f36502006-10-27 10:54:08 +02001098 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001099
Jens Axboe45410ac2006-06-07 11:10:39 +02001100 /*
1101 * Don't break here, continue parsing options so we
1102 * dump all the bad ones. Makes trial/error fixups
1103 * easier on the user.
1104 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001105 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001106 }
Jens Axboeebac4652005-12-08 15:25:21 +01001107
Jens Axboe45410ac2006-06-07 11:10:39 +02001108 if (!ret) {
1109 fsetpos(f, &off);
1110 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001111 } else {
1112 log_err("fio: job %s dropped\n", name);
1113 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001114 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001115 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001116
1117 free(string);
1118 free(name);
1119 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001120 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001121}
1122
1123static int fill_def_thread(void)
1124{
1125 memset(&def_thread, 0, sizeof(def_thread));
1126
1127 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1128 perror("sched_getaffinity");
1129 return 1;
1130 }
1131
1132 /*
Jens Axboeee738492007-01-10 11:23:16 +01001133 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001134 */
Jens Axboeee738492007-01-10 11:23:16 +01001135 fill_default_options(&def_thread, options);
1136
Jens Axboe972cfd22006-06-09 11:08:56 +02001137 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001138 def_thread.write_bw_log = write_bw_log;
1139 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001140
Jens Axboeebac4652005-12-08 15:25:21 +01001141#ifdef FIO_HAVE_DISK_UTIL
1142 def_thread.do_disk_util = 1;
1143#endif
1144
1145 return 0;
1146}
1147
Jens Axboe0ab8db82006-10-18 17:16:23 +02001148static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001149{
1150 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001151 printf("\t--output\tWrite output to file\n");
1152 printf("\t--timeout\tRuntime in seconds\n");
1153 printf("\t--latency-log\tGenerate per-job latency logs\n");
1154 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1155 printf("\t--minimal\tMinimal (terse) output\n");
1156 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001157 printf("\t--help\t\tPrint this page\n");
1158 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001159}
1160
Jens Axboe972cfd22006-06-09 11:08:56 +02001161static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001162{
Jens Axboeb4692822006-10-27 13:43:22 +02001163 struct thread_data *td = NULL;
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001164 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001165
Jens Axboeb4692822006-10-27 13:43:22 +02001166 while ((c = getopt_long(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001167 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001168 case 't':
1169 def_timeout = atoi(optarg);
1170 break;
1171 case 'l':
1172 write_lat_log = 1;
1173 break;
1174 case 'w':
1175 write_bw_log = 1;
1176 break;
1177 case 'o':
1178 f_out = fopen(optarg, "w+");
1179 if (!f_out) {
1180 perror("fopen output");
1181 exit(1);
1182 }
1183 f_err = f_out;
1184 break;
1185 case 'm':
1186 terse_output = 1;
1187 break;
1188 case 'h':
1189 usage();
1190 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001191 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001192 ret = show_cmd_help(options, optarg);
1193 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001194 case 'v':
1195 printf("%s\n", fio_version_string);
1196 exit(0);
1197 case FIO_GETOPT_JOB: {
1198 const char *opt = long_options[lidx].name;
1199 char *val = optarg;
1200
Jens Axboec2b1e752006-10-30 09:03:13 +01001201 if (!strncmp(opt, "name", 4) && td) {
1202 ret = add_job(td, td->name ?: "fio", 0);
1203 if (ret) {
1204 put_job(td);
1205 return 0;
1206 }
1207 td = NULL;
1208 }
Jens Axboeb4692822006-10-27 13:43:22 +02001209 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001210 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001211
1212 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001213 if (!td)
1214 return 0;
1215 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001216
Jens Axboeb1508cf2006-11-02 09:12:40 +01001217 ret = parse_cmd_option(opt, val, options, td);
Jens Axboe78217df2007-02-23 11:18:30 +01001218 if (ret)
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001219 dont_add_job = 1;
Jens Axboeb4692822006-10-27 13:43:22 +02001220 break;
1221 }
1222 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001223 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001224 }
1225 }
Jens Axboec9fad892006-05-27 17:26:50 +02001226
Jens Axboeb4692822006-10-27 13:43:22 +02001227 if (td) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001228 if (dont_add_job)
Jens Axboeb4692822006-10-27 13:43:22 +02001229 put_job(td);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001230 else {
1231 ret = add_job(td, td->name ?: "fio", 0);
1232 if (ret)
1233 put_job(td);
1234 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001235 }
Jens Axboe774a6172006-08-24 08:44:26 +02001236
Jens Axboeb4692822006-10-27 13:43:22 +02001237 while (optind < argc) {
1238 ini_idx++;
1239 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1240 ini_file[ini_idx - 1] = strdup(argv[optind]);
1241 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001242 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001243
1244 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001245}
1246
1247static void free_shm(void)
1248{
1249 struct shmid_ds sbuf;
1250
1251 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001252 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001253 threads = NULL;
1254 shmctl(shm_id, IPC_RMID, &sbuf);
1255 }
1256}
1257
Jens Axboe906c8d72006-06-13 09:37:56 +02001258/*
1259 * The thread area is shared between the main process and the job
1260 * threads/processes. So setup a shared memory segment that will hold
1261 * all the job info.
1262 */
Jens Axboeebac4652005-12-08 15:25:21 +01001263static int setup_thread_area(void)
1264{
1265 /*
1266 * 1024 is too much on some machines, scale max_jobs if
1267 * we get a failure that looks like too large a shm segment
1268 */
1269 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001270 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001271
Jens Axboe906c8d72006-06-13 09:37:56 +02001272 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001273 if (shm_id != -1)
1274 break;
1275 if (errno != EINVAL) {
1276 perror("shmget");
1277 break;
1278 }
1279
1280 max_jobs >>= 1;
1281 } while (max_jobs);
1282
1283 if (shm_id == -1)
1284 return 1;
1285
1286 threads = shmat(shm_id, NULL, 0);
1287 if (threads == (void *) -1) {
1288 perror("shmat");
1289 return 1;
1290 }
1291
1292 atexit(free_shm);
1293 return 0;
1294}
1295
Jens Axboeb4692822006-10-27 13:43:22 +02001296/*
1297 * Copy the fio options into the long options map, so we mirror
1298 * job and cmd line options.
1299 */
1300static void dupe_job_options(void)
1301{
1302 struct fio_option *o;
1303 unsigned int i;
1304
1305 i = 0;
1306 while (long_options[i].name)
1307 i++;
1308
1309 o = &options[0];
1310 while (o->name) {
1311 long_options[i].name = o->name;
1312 long_options[i].val = FIO_GETOPT_JOB;
1313 if (o->type == FIO_OPT_STR_SET)
1314 long_options[i].has_arg = no_argument;
1315 else
1316 long_options[i].has_arg = required_argument;
1317
1318 i++;
1319 o++;
1320 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1321 }
1322}
1323
Jens Axboeebac4652005-12-08 15:25:21 +01001324int parse_options(int argc, char *argv[])
1325{
Jens Axboe972cfd22006-06-09 11:08:56 +02001326 int job_files, i;
1327
Jens Axboeb4692822006-10-27 13:43:22 +02001328 f_out = stdout;
1329 f_err = stderr;
1330
Jens Axboe13335dd2007-01-10 13:14:02 +01001331 options_init(options);
1332
Jens Axboeb4692822006-10-27 13:43:22 +02001333 dupe_job_options();
1334
Jens Axboeebac4652005-12-08 15:25:21 +01001335 if (setup_thread_area())
1336 return 1;
1337 if (fill_def_thread())
1338 return 1;
1339
Jens Axboe972cfd22006-06-09 11:08:56 +02001340 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001341
Jens Axboe972cfd22006-06-09 11:08:56 +02001342 for (i = 0; i < job_files; i++) {
1343 if (fill_def_thread())
1344 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001345 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001346 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001347 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001348 }
Jens Axboeebac4652005-12-08 15:25:21 +01001349
Jens Axboe88c6ed82006-06-09 11:28:10 +02001350 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001351
1352 if (!thread_number) {
1353 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001354 return 1;
1355 }
1356
Jens Axboeebac4652005-12-08 15:25:21 +01001357 return 0;
1358}