blob: 1c62c5ad10af97a2ed1c610323ceccfddde89646 [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_mem_cb(void *, const char *);
Jens Axboee1f36502006-10-27 10:54:08 +020026static int str_lockmem_cb(void *, unsigned long *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010027#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +020028static int str_prio_cb(void *, unsigned int *);
29static int str_prioclass_cb(void *, unsigned int *);
Jens Axboe34cfcda2006-11-03 14:00:45 +010030#endif
Jens Axboee1f36502006-10-27 10:54:08 +020031static int str_exitall_cb(void);
32static int str_cpumask_cb(void *, unsigned int *);
Jens Axboe1907dbc2007-03-12 11:44:28 +010033static int str_fst_cb(void *, const char *);
Jens Axboeaf52b342007-03-13 10:07:47 +010034static int str_filename_cb(void *, const char *);
35static int str_directory_cb(void *, const char *);
Jens Axboebbf6b542007-03-13 15:28:55 +010036static int str_opendir_cb(void *, const char *);
Jens Axboee1f36502006-10-27 10:54:08 +020037
Jens Axboeee738492007-01-10 11:23:16 +010038#define __stringify_1(x) #x
39#define __stringify(x) __stringify_1(x)
40
Jens Axboee1f36502006-10-27 10:54:08 +020041/*
42 * Map of job/command line options
43 */
44static struct fio_option options[] = {
45 {
Jens Axboe61697c32007-02-05 15:04:46 +010046 .name = "description",
47 .type = FIO_OPT_STR_STORE,
48 .off1 = td_var_offset(description),
49 .help = "Text job description",
50 },
51 {
Jens Axboee1f36502006-10-27 10:54:08 +020052 .name = "name",
53 .type = FIO_OPT_STR_STORE,
54 .off1 = td_var_offset(name),
Jens Axboefd28ca42007-01-09 21:20:13 +010055 .help = "Name of this job",
Jens Axboee1f36502006-10-27 10:54:08 +020056 },
57 {
58 .name = "directory",
59 .type = FIO_OPT_STR_STORE,
60 .off1 = td_var_offset(directory),
Jens Axboeaf52b342007-03-13 10:07:47 +010061 .cb = str_directory_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +010062 .help = "Directory to store files in",
Jens Axboee1f36502006-10-27 10:54:08 +020063 },
64 {
65 .name = "filename",
66 .type = FIO_OPT_STR_STORE,
67 .off1 = td_var_offset(filename),
Jens Axboeaf52b342007-03-13 10:07:47 +010068 .cb = str_filename_cb,
69 .help = "File(s) to use for the workload",
Jens Axboee1f36502006-10-27 10:54:08 +020070 },
71 {
Jens Axboebbf6b542007-03-13 15:28:55 +010072 .name = "opendir",
73 .type = FIO_OPT_STR_STORE,
74 .off1 = td_var_offset(opendir),
75 .cb = str_opendir_cb,
76 .help = "Recursively add files from this directory and down",
77 },
78 {
Jens Axboee1f36502006-10-27 10:54:08 +020079 .name = "rw",
80 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +010081 .off1 = td_var_offset(td_ddir),
Jens Axboe15f79182007-01-10 12:26:18 +010082 .help = "IO direction",
Jens Axboeee738492007-01-10 11:23:16 +010083 .def = "read",
Jens Axboeb1ec1da2007-02-23 10:29:16 +010084 .posval = {
Jens Axboe78372132007-03-14 13:24:07 +010085 { .ival = "read",
86 .oval = TD_DDIR_READ,
87 .help = "Sequential read",
Jens Axboeb1ec1da2007-02-23 10:29:16 +010088 },
Jens Axboe78372132007-03-14 13:24:07 +010089 { .ival = "write",
90 .oval = TD_DDIR_WRITE,
91 .help = "Sequential write",
92 },
93 { .ival = "randread",
94 .oval = TD_DDIR_RANDREAD,
95 .help = "Random read",
96 },
97 { .ival = "randwrite",
98 .oval = TD_DDIR_RANDWRITE,
99 .help = "Random write",
Jens Axboe9d6d0192007-03-14 13:28:31 +0100100 },
Jens Axboe78372132007-03-14 13:24:07 +0100101 { .ival = "rw",
102 .oval = TD_DDIR_RW,
103 .help = "Sequential read and write mix",
104 },
105 { .ival = "randrw",
106 .oval = TD_DDIR_RANDRW,
107 .help = "Random read and write mix"
108 },
109 },
Jens Axboee1f36502006-10-27 10:54:08 +0200110 },
111 {
112 .name = "ioengine",
Jens Axboe09629a92007-03-09 09:00:06 +0100113 .type = FIO_OPT_STR_STORE,
114 .off1 = td_var_offset(ioengine),
Jens Axboe15f79182007-01-10 12:26:18 +0100115 .help = "IO engine to use",
Jens Axboeee738492007-01-10 11:23:16 +0100116 .def = "sync",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100117 .posval = {
Jens Axboe78372132007-03-14 13:24:07 +0100118 { .ival = "sync",
119 .help = "Use read/write",
120 },
Jens Axboe10cad1a2007-02-23 11:23:03 +0100121#ifdef FIO_HAVE_LIBAIO
Jens Axboe78372132007-03-14 13:24:07 +0100122 { .ival = "libaio",
123 .help = "Linux native asynchronous IO",
124 },
Jens Axboe10cad1a2007-02-23 11:23:03 +0100125#endif
126#ifdef FIO_HAVE_POSIXAIO
Jens Axboe78372132007-03-14 13:24:07 +0100127 { .ival = "posixaio",
128 .help = "POSIX asynchronous IO",
129 },
Jens Axboe10cad1a2007-02-23 11:23:03 +0100130#endif
Jens Axboe78372132007-03-14 13:24:07 +0100131 { .ival = "mmap",
132 .help = "Memory mapped IO",
133 },
Jens Axboe10cad1a2007-02-23 11:23:03 +0100134#ifdef FIO_HAVE_SPLICE
Jens Axboe78372132007-03-14 13:24:07 +0100135 { .ival = "splice",
136 .help = "splice/vmsplice based IO",
137 },
Jens Axboe10cad1a2007-02-23 11:23:03 +0100138#endif
139#ifdef FIO_HAVE_SGIO
Jens Axboe78372132007-03-14 13:24:07 +0100140 { .ival = "sg",
141 .help = "SCSI generic v3 IO",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100142 },
Jens Axboe78372132007-03-14 13:24:07 +0100143#endif
144 { .ival = "null",
145 .help = "Testing engine (no data transfer)",
146 },
147 { .ival = "net",
148 .help = "Network IO",
149 },
150#ifdef FIO_HAVE_SYSLET
151 { .ival = "syslet-rw",
152 .help = "syslet enabled async pread/pwrite IO",
153 },
154#endif
155 { .ival = "cpuio",
156 .help = "CPU cycler burner engine",
157 },
158 { .ival = "external",
159 .help = "Load external engine (append name)",
160 },
161 },
Jens Axboee1f36502006-10-27 10:54:08 +0200162 },
163 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100164 .name = "iodepth",
165 .type = FIO_OPT_INT,
166 .off1 = td_var_offset(iodepth),
167 .help = "Amount of IO buffers to keep in flight",
168 .def = "1",
169 },
170 {
Jens Axboecb5ab512007-02-26 12:57:09 +0100171 .name = "iodepth_batch",
172 .type = FIO_OPT_INT,
173 .off1 = td_var_offset(iodepth_batch),
174 .help = "Number of IO to submit in one go",
175 },
176 {
Jens Axboee916b392007-02-20 14:37:26 +0100177 .name = "iodepth_low",
178 .type = FIO_OPT_INT,
179 .off1 = td_var_offset(iodepth_low),
180 .help = "Low water mark for queuing depth",
181 },
182 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100183 .name = "size",
184 .type = FIO_OPT_STR_VAL,
185 .off1 = td_var_offset(total_file_size),
186 .help = "Size of device or file",
187 },
188 {
189 .name = "bs",
190 .type = FIO_OPT_STR_VAL_INT,
191 .off1 = td_var_offset(bs[DDIR_READ]),
192 .off2 = td_var_offset(bs[DDIR_WRITE]),
193 .help = "Block size unit",
194 .def = "4k",
195 },
196 {
197 .name = "bsrange",
198 .type = FIO_OPT_RANGE,
199 .off1 = td_var_offset(min_bs[DDIR_READ]),
200 .off2 = td_var_offset(max_bs[DDIR_READ]),
201 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
202 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
203 .help = "Set block size range (in more detail than bs)",
204 },
205 {
206 .name = "bs_unaligned",
207 .type = FIO_OPT_STR_SET,
208 .off1 = td_var_offset(bs_unaligned),
209 .help = "Don't sector align IO buffer sizes",
210 },
211 {
212 .name = "offset",
213 .type = FIO_OPT_STR_VAL,
214 .off1 = td_var_offset(start_offset),
215 .help = "Start IO from this offset",
216 .def = "0",
217 },
218 {
219 .name = "randrepeat",
220 .type = FIO_OPT_BOOL,
221 .off1 = td_var_offset(rand_repeatable),
222 .help = "Use repeatable random IO pattern",
223 .def = "1",
224 },
225 {
226 .name = "norandommap",
227 .type = FIO_OPT_STR_SET,
228 .off1 = td_var_offset(norandommap),
229 .help = "Accept potential duplicate random blocks",
230 },
231 {
232 .name = "nrfiles",
233 .type = FIO_OPT_INT,
234 .off1 = td_var_offset(nr_files),
235 .help = "Split job workload between this number of files",
236 .def = "1",
237 },
238 {
Jens Axboeb5af8292007-03-08 12:43:13 +0100239 .name = "openfiles",
240 .type = FIO_OPT_INT,
241 .off1 = td_var_offset(open_files),
242 .help = "Number of files to keep open at the same time",
243 },
244 {
Jens Axboe0aabe162007-02-23 08:45:55 +0100245 .name = "file_service_type",
246 .type = FIO_OPT_STR,
Jens Axboe1907dbc2007-03-12 11:44:28 +0100247 .cb = str_fst_cb,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100248 .off1 = td_var_offset(file_service_type),
Jens Axboe0aabe162007-02-23 08:45:55 +0100249 .help = "How to select which file to service next",
250 .def = "roundrobin",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100251 .posval = {
Jens Axboe78372132007-03-14 13:24:07 +0100252 { .ival = "random",
253 .oval = FIO_FSERVICE_RANDOM,
254 .help = "Choose a file at random",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100255 },
Jens Axboe78372132007-03-14 13:24:07 +0100256 { .ival = "roundrobin",
257 .oval = FIO_FSERVICE_RR,
258 .help = "Round robin select files",
259 },
260 },
Jens Axboe0aabe162007-02-23 08:45:55 +0100261 },
262 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100263 .name = "fsync",
264 .type = FIO_OPT_INT,
265 .off1 = td_var_offset(fsync_blocks),
266 .help = "Issue fsync for writes every given number of blocks",
267 .def = "0",
268 },
269 {
270 .name = "direct",
271 .type = FIO_OPT_BOOL,
272 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100273 .help = "Use O_DIRECT IO (negates buffered)",
274 .def = "0",
275 },
276 {
277 .name = "buffered",
278 .type = FIO_OPT_BOOL,
279 .off1 = td_var_offset(odirect),
280 .neg = 1,
281 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100282 .def = "1",
283 },
284 {
285 .name = "overwrite",
286 .type = FIO_OPT_BOOL,
287 .off1 = td_var_offset(overwrite),
288 .help = "When writing, set whether to overwrite current data",
289 .def = "0",
290 },
291 {
292 .name = "loops",
293 .type = FIO_OPT_INT,
294 .off1 = td_var_offset(loops),
295 .help = "Number of times to run the job",
296 .def = "1",
297 },
298 {
299 .name = "numjobs",
300 .type = FIO_OPT_INT,
301 .off1 = td_var_offset(numjobs),
302 .help = "Duplicate this job this many times",
303 .def = "1",
304 },
305 {
306 .name = "startdelay",
307 .type = FIO_OPT_INT,
308 .off1 = td_var_offset(start_delay),
309 .help = "Only start job when this period has passed",
310 .def = "0",
311 },
312 {
313 .name = "runtime",
314 .alias = "timeout",
315 .type = FIO_OPT_STR_VAL_TIME,
316 .off1 = td_var_offset(timeout),
317 .help = "Stop workload when this amount of time has passed",
318 .def = "0",
319 },
320 {
Jens Axboee1f36502006-10-27 10:54:08 +0200321 .name = "mem",
322 .type = FIO_OPT_STR,
323 .cb = str_mem_cb,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100324 .off1 = td_var_offset(mem_type),
Jens Axboe15f79182007-01-10 12:26:18 +0100325 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100326 .def = "malloc",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100327 .posval = {
Jens Axboe78372132007-03-14 13:24:07 +0100328 { .ival = "malloc",
329 .oval = MEM_MALLOC,
330 .help = "Use malloc(3) for IO buffers",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100331 },
Jens Axboe78372132007-03-14 13:24:07 +0100332 { .ival = "shm",
333 .oval = MEM_SHM,
334 .help = "Use shared memory segments for IO buffers",
335 },
336#ifdef FIO_HAVE_HUGETLB
337 { .ival = "shmhuge",
338 .oval = MEM_SHMHUGE,
339 .help = "Like shm, but use huge pages",
340 },
341#endif
342 { .ival = "mmap",
343 .oval = MEM_MMAP,
344 .help = "Use mmap(2) (file or anon) for IO buffers",
345 },
346#ifdef FIO_HAVE_HUGETLB
347 { .ival = "mmaphuge",
348 .oval = MEM_MMAPHUGE,
349 .help = "Like mmap, but use huge pages",
350 },
351#endif
352 },
Jens Axboee1f36502006-10-27 10:54:08 +0200353 },
354 {
355 .name = "verify",
356 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100357 .off1 = td_var_offset(verify),
Jens Axboe9d6d0192007-03-14 13:28:31 +0100358 .help = "Verify data written",
Jens Axboeee738492007-01-10 11:23:16 +0100359 .def = "0",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100360 .posval = {
Jens Axboe78372132007-03-14 13:24:07 +0100361 { .ival = "0",
362 .oval = VERIFY_NONE,
363 .help = "Don't do IO verification",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100364 },
Jens Axboe78372132007-03-14 13:24:07 +0100365 { .ival = "crc32",
366 .oval = VERIFY_CRC32,
367 .help = "Use crc32 checksums for verification",
368 },
369 { .ival = "md5",
370 .oval = VERIFY_MD5,
371 .help = "Use md5 checksums for verification",
372 },
373 },
Jens Axboee1f36502006-10-27 10:54:08 +0200374 },
375 {
376 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200377 .type = FIO_OPT_STR_STORE,
378 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100379 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200380 },
381 {
Jens Axboe076efc72006-10-27 11:24:25 +0200382 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200383 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200384 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100385 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200386 },
387 {
388 .name = "exec_prerun",
389 .type = FIO_OPT_STR_STORE,
390 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100391 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200392 },
393 {
394 .name = "exec_postrun",
395 .type = FIO_OPT_STR_STORE,
396 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100397 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200398 },
399#ifdef FIO_HAVE_IOSCHED_SWITCH
400 {
401 .name = "ioscheduler",
402 .type = FIO_OPT_STR_STORE,
403 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100404 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200405 },
406#endif
407 {
Jens Axboee1f36502006-10-27 10:54:08 +0200408 .name = "zonesize",
409 .type = FIO_OPT_STR_VAL,
410 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100411 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100412 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200413 },
414 {
415 .name = "zoneskip",
416 .type = FIO_OPT_STR_VAL,
417 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100418 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100419 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200420 },
421 {
422 .name = "lockmem",
423 .type = FIO_OPT_STR_VAL,
424 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100425 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100426 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200427 },
428 {
Jens Axboee1f36502006-10-27 10:54:08 +0200429 .name = "rwmixcycle",
430 .type = FIO_OPT_INT,
431 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100432 .help = "Cycle period for mixed read/write workloads (msec)",
433 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200434 },
435 {
436 .name = "rwmixread",
437 .type = FIO_OPT_INT,
438 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100439 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100440 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100441 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200442 },
443 {
444 .name = "rwmixwrite",
445 .type = FIO_OPT_INT,
446 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100447 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100448 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100449 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200450 },
451 {
452 .name = "nice",
453 .type = FIO_OPT_INT,
454 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100455 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100456 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100457 .maxval = 20,
458 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200459 },
460#ifdef FIO_HAVE_IOPRIO
461 {
462 .name = "prio",
463 .type = FIO_OPT_INT,
464 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100465 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100466 .minval = 0,
467 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200468 },
469 {
470 .name = "prioclass",
471 .type = FIO_OPT_INT,
472 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100473 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100474 .minval = 0,
475 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200476 },
477#endif
478 {
479 .name = "thinktime",
480 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100481 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100482 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100483 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200484 },
485 {
Jens Axboe48097d52007-02-17 06:30:44 +0100486 .name = "thinktime_spin",
487 .type = FIO_OPT_INT,
488 .off1 = td_var_offset(thinktime_spin),
Jens Axboe34403fb2007-03-02 21:43:25 +0100489 .help = "Start think time by spinning this amount (usec)",
Jens Axboe48097d52007-02-17 06:30:44 +0100490 .def = "0",
491 },
492 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100493 .name = "thinktime_blocks",
494 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100495 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100496 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100497 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100498 },
499 {
Jens Axboee1f36502006-10-27 10:54:08 +0200500 .name = "rate",
501 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100502 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100503 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200504 },
505 {
506 .name = "ratemin",
507 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100508 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100509 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200510 },
511 {
512 .name = "ratecycle",
513 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100514 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100515 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100516 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200517 },
518 {
Jens Axboee1f36502006-10-27 10:54:08 +0200519 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100520 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100521 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100522 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100523 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200524 },
525 {
526 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100527 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100528 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100529 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100530 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200531 },
532 {
533 .name = "bwavgtime",
534 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100535 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100536 .help = "Time window over which to calculate bandwidth (msec)",
537 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200538 },
539 {
540 .name = "create_serialize",
Jens Axboe13335dd2007-01-10 13:14:02 +0100541 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100542 .off1 = td_var_offset(create_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100543 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100544 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200545 },
546 {
547 .name = "create_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100548 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100549 .off1 = td_var_offset(create_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100550 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100551 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200552 },
553 {
Jens Axboee1f36502006-10-27 10:54:08 +0200554 .name = "cpuload",
555 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100556 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100557 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200558 },
559 {
560 .name = "cpuchunks",
561 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100562 .off1 = td_var_offset(cpucycle),
Jens Axboeba0fbe12007-03-09 14:34:23 +0100563 .help = "Length of the CPU burn cycles (usecs)",
564 .def = "50000",
Jens Axboee1f36502006-10-27 10:54:08 +0200565 },
Jens Axboee1f36502006-10-27 10:54:08 +0200566#ifdef FIO_HAVE_CPU_AFFINITY
567 {
568 .name = "cpumask",
569 .type = FIO_OPT_INT,
570 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100571 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200572 },
573#endif
574 {
575 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100576 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100577 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100578 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100579 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200580 },
581 {
Jens Axboeebb14152007-03-13 14:42:15 +0100582 .name = "fsync_on_close",
583 .type = FIO_OPT_BOOL,
584 .off1 = td_var_offset(fsync_on_close),
585 .help = "fsync files on close",
586 .def = "0",
587 },
588 {
Jens Axboee1f36502006-10-27 10:54:08 +0200589 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100590 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200591 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100592 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100593 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200594 },
595 {
596 .name = "exitall",
597 .type = FIO_OPT_STR_SET,
598 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100599 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200600 },
601 {
602 .name = "stonewall",
603 .type = FIO_OPT_STR_SET,
604 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100605 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200606 },
607 {
608 .name = "thread",
609 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100610 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100611 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200612 },
613 {
614 .name = "write_bw_log",
615 .type = FIO_OPT_STR_SET,
616 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100617 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200618 },
619 {
620 .name = "write_lat_log",
621 .type = FIO_OPT_STR_SET,
622 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100623 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200624 },
625 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100626 .name = "hugepage-size",
627 .type = FIO_OPT_STR_VAL,
628 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100629 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100630 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100631 },
632 {
Jens Axboeb2560f32007-03-06 12:43:03 +0100633 .name = "group_reporting",
634 .type = FIO_OPT_STR_SET,
635 .off1 = td_var_offset(group_reporting),
636 .help = "Do reporting on a per-group basis",
637 },
638 {
Jens Axboee1f36502006-10-27 10:54:08 +0200639 .name = NULL,
640 },
641};
642
Jens Axboeb4692822006-10-27 13:43:22 +0200643#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
644#define FIO_CMD_OPTS (16)
645#define FIO_GETOPT_JOB (0x89988998)
646
647/*
648 * Command line options. These will contain the above, plus a few
649 * extra that only pertain to fio itself and not jobs.
650 */
651static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
652 {
653 .name = "output",
654 .has_arg = required_argument,
655 .val = 'o',
656 },
657 {
658 .name = "timeout",
659 .has_arg = required_argument,
660 .val = 't',
661 },
662 {
663 .name = "latency-log",
664 .has_arg = required_argument,
665 .val = 'l',
666 },
667 {
668 .name = "bandwidth-log",
669 .has_arg = required_argument,
670 .val = 'b',
671 },
672 {
673 .name = "minimal",
674 .has_arg = optional_argument,
675 .val = 'm',
676 },
677 {
678 .name = "version",
679 .has_arg = no_argument,
680 .val = 'v',
681 },
682 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100683 .name = "help",
684 .has_arg = no_argument,
685 .val = 'h',
686 },
687 {
688 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100689 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +0100690 .val = 'c',
691 },
692 {
Jens Axboeb4692822006-10-27 13:43:22 +0200693 .name = NULL,
694 },
695};
696
Jens Axboeee738492007-01-10 11:23:16 +0100697static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200698
Jens Axboe48946b42007-03-14 11:37:39 +0100699static char fio_version_string[] = "fio 1.14";
Jens Axboeebac4652005-12-08 15:25:21 +0100700
Jens Axboe972cfd22006-06-09 11:08:56 +0200701static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100702static int max_jobs = MAX_JOBS;
703
704struct thread_data def_thread;
705struct thread_data *threads = NULL;
706
Jens Axboeebac4652005-12-08 15:25:21 +0100707int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200708int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200709unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200710FILE *f_out = NULL;
711FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100712
Jens Axboeee738492007-01-10 11:23:16 +0100713static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100714int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200715
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100716static int prev_group_jobs;
717
Joel Becker9728ce32007-03-01 08:24:39 +0100718FILE *get_f_out()
719{
720 return f_out;
721}
722
723FILE *get_f_err()
724{
725 return f_err;
726}
727
Jens Axboe906c8d72006-06-13 09:37:56 +0200728/*
729 * Return a free job structure.
730 */
Jens Axboeebac4652005-12-08 15:25:21 +0100731static struct thread_data *get_new_job(int global, struct thread_data *parent)
732{
733 struct thread_data *td;
734
735 if (global)
736 return &def_thread;
737 if (thread_number >= max_jobs)
738 return NULL;
739
740 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200741 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100742
Jens Axboeebac4652005-12-08 15:25:21 +0100743 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100744 return td;
745}
746
747static void put_job(struct thread_data *td)
748{
Jens Axboe549577a2006-10-30 12:23:41 +0100749 if (td == &def_thread)
750 return;
751
Jens Axboe16edf252007-02-10 14:59:22 +0100752 if (td->error)
753 fprintf(f_out, "fio: %s\n", td->verror);
754
Jens Axboeebac4652005-12-08 15:25:21 +0100755 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
756 thread_number--;
757}
758
Jens Axboedad915e2006-10-27 11:10:18 +0200759/*
760 * Lazy way of fixing up options that depend on each other. We could also
761 * define option callback handlers, but this is easier.
762 */
Jens Axboee1f36502006-10-27 10:54:08 +0200763static void fixup_options(struct thread_data *td)
764{
Jens Axboee1f36502006-10-27 10:54:08 +0200765 if (!td->rwmixread && td->rwmixwrite)
766 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200767
Jens Axboe076efc72006-10-27 11:24:25 +0200768 if (td->write_iolog_file && td->read_iolog_file) {
769 log_err("fio: read iolog overrides write_iolog\n");
770 free(td->write_iolog_file);
771 td->write_iolog_file = NULL;
772 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100773
774 if (td->io_ops->flags & FIO_SYNCIO)
775 td->iodepth = 1;
776 else {
777 if (!td->iodepth)
Jens Axboeb5af8292007-03-08 12:43:13 +0100778 td->iodepth = td->open_files;
Jens Axboe16b462a2006-10-30 12:35:18 +0100779 }
780
781 /*
782 * only really works for sequential io for now, and with 1 file
783 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100784 if (td->zone_size && td_random(td) && td->open_files == 1)
Jens Axboe16b462a2006-10-30 12:35:18 +0100785 td->zone_size = 0;
786
787 /*
788 * Reads can do overwrites, we always need to pre-create the file
789 */
790 if (td_read(td) || td_rw(td))
791 td->overwrite = 1;
792
Jens Axboea00735e2006-11-03 08:58:08 +0100793 if (!td->min_bs[DDIR_READ])
794 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
795 if (!td->max_bs[DDIR_READ])
796 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
797 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100798 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100799 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100800 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100801
802 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
803
Jens Axboe16b462a2006-10-30 12:35:18 +0100804 if (td_read(td) && !td_rw(td))
805 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100806
807 if (td->norandommap && td->verify != VERIFY_NONE) {
808 log_err("fio: norandommap given, verify disabled\n");
809 td->verify = VERIFY_NONE;
810 }
Jens Axboe690adba2006-10-30 15:25:09 +0100811 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
812 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100813
814 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100815 * thinktime_spin must be less than thinktime
816 */
817 if (td->thinktime_spin > td->thinktime)
818 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100819
820 /*
821 * The low water mark cannot be bigger than the iodepth
822 */
Jens Axboe9467b772007-02-27 19:56:43 +0100823 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
824 /*
825 * syslet work around - if the workload is sequential,
826 * we want to let the queue drain all the way down to
827 * avoid seeking between async threads
828 */
829 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
830 td->iodepth_low = 1;
831 else
832 td->iodepth_low = td->iodepth;
833 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100834
835 /*
836 * If batch number isn't set, default to the same as iodepth
837 */
838 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
839 td->iodepth_batch = td->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100840
Jens Axboebbf6b542007-03-13 15:28:55 +0100841 if (td->nr_files > td->files_index)
Jens Axboe9f9214f2007-03-13 14:02:16 +0100842 td->nr_files = td->files_index;
843
844 if (td->open_files > td->nr_files || !td->open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100845 td->open_files = td->nr_files;
Jens Axboee1f36502006-10-27 10:54:08 +0200846}
847
Jens Axboe906c8d72006-06-13 09:37:56 +0200848/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100849 * This function leaks the buffer
850 */
851static char *to_kmg(unsigned int val)
852{
853 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100854 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100855 char *p = post;
856
Jens Axboe245142f2006-11-08 12:49:21 +0100857 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100858 if (val & 1023)
859 break;
860
861 val >>= 10;
862 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100863 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100864
865 snprintf(buf, 31, "%u%c", val, *p);
866 return buf;
867}
868
Jens Axboe09629a92007-03-09 09:00:06 +0100869/* External engines are specified by "external:name.o") */
870static const char *get_engine_name(const char *str)
871{
872 char *p = strstr(str, ":");
873
874 if (!p)
875 return str;
876
877 p++;
878 strip_blank_front(&p);
879 strip_blank_end(p);
880 return p;
881}
882
Jens Axboee132cba2007-03-14 14:23:54 +0100883static int exists_and_not_file(const char *filename)
884{
885 struct stat sb;
886
887 if (lstat(filename, &sb) == -1)
888 return 0;
889
890 if (S_ISREG(sb.st_mode))
891 return 0;
892
893 return 1;
894}
895
Jens Axboef8977ee2006-11-06 14:03:03 +0100896/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200897 * Adds a job to the list of things todo. Sanitizes the various options
898 * to make sure we don't have conflicts, and initializes various
899 * members of td.
900 */
Jens Axboe75154842006-06-01 13:56:09 +0200901static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100902{
Jens Axboe413dd452007-02-23 09:26:09 +0100903 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
904 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100905 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200906 struct fio_file *f;
Jens Axboe09629a92007-03-09 09:00:06 +0100907 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100908 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100909 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100910
Jens Axboeebac4652005-12-08 15:25:21 +0100911 /*
912 * the def_thread is just for options, it's not a real job
913 */
914 if (td == &def_thread)
915 return 0;
916
Jens Axboe09629a92007-03-09 09:00:06 +0100917 engine = get_engine_name(td->ioengine);
918 td->io_ops = load_ioengine(td, engine);
919 if (!td->io_ops) {
920 log_err("fio: failed to load engine %s\n", engine);
921 return 1;
922 }
Jens Axboedf641192006-10-12 07:55:41 +0200923
Jens Axboe9cedf162007-03-12 11:29:30 +0100924 if (td->use_thread)
925 nr_thread++;
926 else
927 nr_process++;
928
Jens Axboe690adba2006-10-30 15:25:09 +0100929 if (td->odirect)
930 td->io_ops->flags |= FIO_RAWIO;
931
Jens Axboee132cba2007-03-14 14:23:54 +0100932 file_alloced = 0;
Jens Axboebbf6b542007-03-13 15:28:55 +0100933 if (!td->filename && !td->files_index) {
Jens Axboee132cba2007-03-14 14:23:54 +0100934 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +0100935
Jens Axboee132cba2007-03-14 14:23:54 +0100936 if (td->nr_files == 1 && exists_and_not_file(jobname))
937 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +0100938 else {
939 for (i = 0; i < td->nr_files; i++) {
Jens Axboee132cba2007-03-14 14:23:54 +0100940 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +0100941 add_file(td, fname);
942 }
Jens Axboeaf52b342007-03-13 10:07:47 +0100943 }
Jens Axboe0af7b542006-02-17 10:10:12 +0100944 }
Jens Axboeebac4652005-12-08 15:25:21 +0100945
Jens Axboee0a22332006-12-20 12:54:25 +0100946 fixup_options(td);
947
Jens Axboe53cdc682006-10-18 11:50:58 +0200948 for_each_file(td, f, i) {
Jens Axboeaf52b342007-03-13 10:07:47 +0100949 if (td->directory && f->filetype == FIO_TYPE_FILE) {
950 sprintf(fname, "%s/%s", td->directory, f->file_name);
951 f->file_name = strdup(fname);
952 }
Jens Axboe53cdc682006-10-18 11:50:58 +0200953 }
954
Jens Axboe07739b52007-03-08 20:25:46 +0100955 td->mutex = fio_sem_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +0100956
Jens Axboe756867b2007-03-06 15:19:24 +0100957 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
958 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
959 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +0100960
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100961 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
962 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +0100963 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100964 }
Jens Axboeebac4652005-12-08 15:25:21 +0100965
966 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100967 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +0100968
969 if (setup_rate(td))
970 goto err;
971
Jens Axboeec94ec52006-10-20 10:59:19 +0200972 if (td->write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +0100973 setup_log(&td->ts.slat_log);
974 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100975 }
Jens Axboeec94ec52006-10-20 10:59:19 +0200976 if (td->write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +0100977 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +0100978
Jens Axboeb4692822006-10-27 13:43:22 +0200979 if (!td->name)
980 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +0200981
Jens Axboec6ae0a52006-06-12 11:04:44 +0200982 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +0200983 if (!job_add_num) {
Jens Axboeba0fbe12007-03-09 14:34:23 +0100984 if (!strcmp(td->io_ops->name, "cpuio"))
Jens Axboeb990b5c2006-09-14 09:48:22 +0200985 fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +0100986 else {
987 char *c1, *c2, *c3, *c4;
988
989 c1 = to_kmg(td->min_bs[DDIR_READ]);
990 c2 = to_kmg(td->max_bs[DDIR_READ]);
991 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
992 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
993
Jens Axboe413dd452007-02-23 09:26:09 +0100994 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 +0100995
996 free(c1);
997 free(c2);
998 free(c3);
999 free(c4);
1000 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001001 } else if (job_add_num == 1)
Jens Axboec6ae0a52006-06-12 11:04:44 +02001002 fprintf(f_out, "...\n");
1003 }
Jens Axboeebac4652005-12-08 15:25:21 +01001004
1005 /*
1006 * recurse add identical jobs, clear numjobs and stonewall options
1007 * as they don't apply to sub-jobs
1008 */
1009 numjobs = td->numjobs;
1010 while (--numjobs) {
1011 struct thread_data *td_new = get_new_job(0, td);
1012
1013 if (!td_new)
1014 goto err;
1015
1016 td_new->numjobs = 1;
1017 td_new->stonewall = 0;
Jens Axboee132cba2007-03-14 14:23:54 +01001018
1019 if (file_alloced) {
1020 td_new->filename = NULL;
1021 td_new->files_index = 0;
1022 td_new->files = NULL;
1023 }
1024
Jens Axboe75154842006-06-01 13:56:09 +02001025 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001026
Jens Axboe75154842006-06-01 13:56:09 +02001027 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +01001028 goto err;
1029 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001030
1031 if (td->numjobs > 1) {
1032 groupid++;
1033 prev_group_jobs = 0;
1034 }
1035
Jens Axboeebac4652005-12-08 15:25:21 +01001036 return 0;
1037err:
1038 put_job(td);
1039 return -1;
1040}
1041
Jens Axboe906c8d72006-06-13 09:37:56 +02001042/*
1043 * Initialize the various random states we need (random io, block size ranges,
1044 * read/write mix, etc).
1045 */
Jens Axboeebac4652005-12-08 15:25:21 +01001046int init_random_state(struct thread_data *td)
1047{
Jens Axboe0aabe162007-02-23 08:45:55 +01001048 unsigned long seeds[5];
Jens Axboeaf52b342007-03-13 10:07:47 +01001049 int fd, num_maps, blocks;
Jens Axboe0ab8db82006-10-18 17:16:23 +02001050 struct fio_file *f;
Jens Axboeaf52b342007-03-13 10:07:47 +01001051 unsigned int i;
Jens Axboeebac4652005-12-08 15:25:21 +01001052
Jens Axboeba0fbe12007-03-09 14:34:23 +01001053 if (td->io_ops->flags & FIO_DISKLESSIO)
Jens Axboef48b4672006-10-27 11:30:07 +02001054 return 0;
1055
Jens Axboe1ac267b2006-05-31 20:29:00 +02001056 fd = open("/dev/urandom", O_RDONLY);
Jens Axboeebac4652005-12-08 15:25:21 +01001057 if (fd == -1) {
Jens Axboee1161c32007-02-22 19:36:48 +01001058 td_verror(td, errno, "open");
Jens Axboeebac4652005-12-08 15:25:21 +01001059 return 1;
1060 }
1061
Jens Axboea6ccc7b2006-06-02 10:14:15 +02001062 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
Jens Axboee1161c32007-02-22 19:36:48 +01001063 td_verror(td, EIO, "read");
Jens Axboeebac4652005-12-08 15:25:21 +01001064 close(fd);
1065 return 1;
1066 }
1067
1068 close(fd);
1069
Jens Axboe6dfd46b2006-06-07 13:57:06 +02001070 os_random_seed(seeds[0], &td->bsrange_state);
1071 os_random_seed(seeds[1], &td->verify_state);
1072 os_random_seed(seeds[2], &td->rwmix_state);
Jens Axboeebac4652005-12-08 15:25:21 +01001073
Jens Axboe413dd452007-02-23 09:26:09 +01001074 if (td->file_service_type == FIO_FSERVICE_RANDOM)
1075 os_random_seed(seeds[3], &td->next_file_state);
1076
1077 if (!td_random(td))
Jens Axboeebac4652005-12-08 15:25:21 +01001078 return 0;
1079
Jens Axboe9ebc27e2006-06-12 10:21:50 +02001080 if (td->rand_repeatable)
Jens Axboe0aabe162007-02-23 08:45:55 +01001081 seeds[4] = FIO_RANDSEED * td->thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +01001082
Jens Axboebb8895e2006-10-30 15:14:48 +01001083 if (!td->norandommap) {
1084 for_each_file(td, f, i) {
Jens Axboea4a81712007-02-13 20:07:26 +01001085 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
Jens Axboec7c280e2006-11-02 20:10:33 +01001086 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
Jens Axboebb8895e2006-10-30 15:14:48 +01001087 f->file_map = malloc(num_maps * sizeof(long));
Jens Axboebda4fd92007-03-12 11:21:48 +01001088 if (!f->file_map) {
1089 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
1090 return 1;
1091 }
Jens Axboebb8895e2006-10-30 15:14:48 +01001092 f->num_maps = num_maps;
1093 memset(f->file_map, 0, num_maps * sizeof(long));
1094 }
Jens Axboe53cdc682006-10-18 11:50:58 +02001095 }
Jens Axboeebac4652005-12-08 15:25:21 +01001096
Jens Axboe0aabe162007-02-23 08:45:55 +01001097 os_random_seed(seeds[4], &td->random_state);
Jens Axboeebac4652005-12-08 15:25:21 +01001098 return 0;
1099}
1100
1101static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1102{
1103#ifdef FIO_HAVE_CPU_AFFINITY
1104 unsigned int i;
1105
1106 CPU_ZERO(&cpumask);
1107
1108 for (i = 0; i < sizeof(int) * 8; i++) {
1109 if ((1 << i) & cpu)
1110 CPU_SET(i, &cpumask);
1111 }
1112#endif
1113}
1114
Jens Axboeebac4652005-12-08 15:25:21 +01001115static int is_empty_or_comment(char *line)
1116{
1117 unsigned int i;
1118
1119 for (i = 0; i < strlen(line); i++) {
1120 if (line[i] == ';')
1121 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001122 if (line[i] == '#')
1123 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001124 if (!isspace(line[i]) && !iscntrl(line[i]))
1125 return 0;
1126 }
1127
1128 return 1;
1129}
1130
Jens Axboe313cb202006-12-21 09:50:00 +01001131/*
1132 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1133 */
Jens Axboe1907dbc2007-03-12 11:44:28 +01001134static char *get_opt_postfix(const char *str)
Jens Axboe313cb202006-12-21 09:50:00 +01001135{
1136 char *p = strstr(str, ":");
1137
1138 if (!p)
1139 return NULL;
1140
1141 p++;
1142 strip_blank_front(&p);
1143 strip_blank_end(p);
1144 return strdup(p);
1145}
1146
Jens Axboeb4692822006-10-27 13:43:22 +02001147static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +01001148{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001149 struct thread_data *td = data;
1150
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001151 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
Jens Axboe1907dbc2007-03-12 11:44:28 +01001152 td->mmapfile = get_opt_postfix(mem);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001153 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +01001154 log_err("fio: mmaphuge:/path/to/file\n");
1155 return 1;
1156 }
Jens Axboeebac4652005-12-08 15:25:21 +01001157 }
1158
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001159 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001160}
1161
Jens Axboee1f36502006-10-27 10:54:08 +02001162static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1163{
1164 mlock_size = *val;
1165 return 0;
1166}
1167
Jens Axboe34cfcda2006-11-03 14:00:45 +01001168#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001169static int str_prioclass_cb(void *data, unsigned int *val)
1170{
1171 struct thread_data *td = data;
1172
1173 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1174 return 0;
1175}
1176
1177static int str_prio_cb(void *data, unsigned int *val)
1178{
1179 struct thread_data *td = data;
1180
1181 td->ioprio |= *val;
1182 return 0;
1183}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001184#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001185
1186static int str_exitall_cb(void)
1187{
1188 exitall_on_terminate = 1;
1189 return 0;
1190}
1191
1192static int str_cpumask_cb(void *data, unsigned int *val)
1193{
1194 struct thread_data *td = data;
1195
1196 fill_cpu_mask(td->cpumask, *val);
1197 return 0;
1198}
1199
Jens Axboe1907dbc2007-03-12 11:44:28 +01001200static int str_fst_cb(void *data, const char *str)
1201{
1202 struct thread_data *td = data;
1203 char *nr = get_opt_postfix(str);
1204
1205 td->file_service_nr = 1;
1206 if (nr)
1207 td->file_service_nr = atoi(nr);
1208
1209 return 0;
1210}
1211
Jens Axboeaf52b342007-03-13 10:07:47 +01001212static int str_filename_cb(void *data, const char *input)
1213{
1214 struct thread_data *td = data;
Jens Axboecae61952007-03-13 11:12:14 +01001215 char *fname, *str, *p;
Jens Axboeaf52b342007-03-13 10:07:47 +01001216
Jens Axboecae61952007-03-13 11:12:14 +01001217 p = str = strdup(input);
Jens Axboee13363e2007-03-13 11:18:57 +01001218
1219 strip_blank_front(&str);
1220 strip_blank_end(str);
1221
Jens Axboebbf6b542007-03-13 15:28:55 +01001222 if (!td->files_index)
1223 td->nr_files = 0;
1224
Jens Axboe0ad920e2007-03-13 11:06:45 +01001225 while ((fname = strsep(&str, ":")) != NULL) {
Jens Axboee13363e2007-03-13 11:18:57 +01001226 if (!strlen(fname))
1227 break;
Jens Axboeaf52b342007-03-13 10:07:47 +01001228 add_file(td, fname);
Jens Axboebbf6b542007-03-13 15:28:55 +01001229 td->nr_files++;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001230 }
Jens Axboeaf52b342007-03-13 10:07:47 +01001231
Jens Axboecae61952007-03-13 11:12:14 +01001232 free(p);
Jens Axboeaf52b342007-03-13 10:07:47 +01001233 return 0;
1234}
1235
1236static int str_directory_cb(void *data, const char fio_unused *str)
1237{
1238 struct thread_data *td = data;
1239 struct stat sb;
1240
1241 if (lstat(td->directory, &sb) < 0) {
1242 log_err("fio: %s is not a directory\n", td->directory);
1243 td_verror(td, errno, "lstat");
1244 return 1;
1245 }
1246 if (!S_ISDIR(sb.st_mode)) {
1247 log_err("fio: %s is not a directory\n", td->directory);
1248 return 1;
1249 }
1250
1251 return 0;
1252}
1253
Jens Axboebbf6b542007-03-13 15:28:55 +01001254static int str_opendir_cb(void *data, const char fio_unused *str)
1255{
1256 struct thread_data *td = data;
1257
1258 if (!td->files_index)
1259 td->nr_files = 0;
1260
1261 return add_dir_files(td, td->opendir);
1262}
1263
Jens Axboe07261982006-06-07 10:51:12 +02001264/*
1265 * This is our [ini] type file parser.
1266 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001267static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001268{
Jens Axboee1f36502006-10-27 10:54:08 +02001269 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001270 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001271 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001272 fpos_t off;
1273 FILE *f;
1274 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001275 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001276
1277 f = fopen(file, "r");
1278 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001279 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001280 return 1;
1281 }
1282
1283 string = malloc(4096);
1284 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001285 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001286
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001287 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001288 do {
1289 p = fgets(string, 4095, f);
1290 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001291 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001292 if (is_empty_or_comment(p))
1293 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001294 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001295 continue;
1296
1297 global = !strncmp(name, "global", 6);
1298
1299 name[strlen(name) - 1] = '\0';
1300
1301 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001302 if (!td) {
1303 ret = 1;
1304 break;
1305 }
Jens Axboeebac4652005-12-08 15:25:21 +01001306
Jens Axboe972cfd22006-06-09 11:08:56 +02001307 /*
1308 * Seperate multiple job files by a stonewall
1309 */
Jens Axboef9481912006-06-09 11:37:28 +02001310 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001311 td->stonewall = stonewall;
1312 stonewall = 0;
1313 }
1314
Jens Axboeebac4652005-12-08 15:25:21 +01001315 fgetpos(f, &off);
1316 while ((p = fgets(string, 4096, f)) != NULL) {
1317 if (is_empty_or_comment(p))
1318 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001319
Jens Axboeb6754f92006-05-30 14:29:32 +02001320 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001321
1322 if (p[0] == '[')
1323 break;
1324
Jens Axboe4ae3f762006-05-30 13:35:14 +02001325 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001326
Jens Axboee1f36502006-10-27 10:54:08 +02001327 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001328
Jens Axboe45410ac2006-06-07 11:10:39 +02001329 /*
1330 * Don't break here, continue parsing options so we
1331 * dump all the bad ones. Makes trial/error fixups
1332 * easier on the user.
1333 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001334 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001335 }
Jens Axboeebac4652005-12-08 15:25:21 +01001336
Jens Axboe45410ac2006-06-07 11:10:39 +02001337 if (!ret) {
1338 fsetpos(f, &off);
1339 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001340 } else {
1341 log_err("fio: job %s dropped\n", name);
1342 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001343 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001344 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001345
1346 free(string);
1347 free(name);
1348 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001349 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001350}
1351
1352static int fill_def_thread(void)
1353{
1354 memset(&def_thread, 0, sizeof(def_thread));
1355
1356 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1357 perror("sched_getaffinity");
1358 return 1;
1359 }
1360
1361 /*
Jens Axboeee738492007-01-10 11:23:16 +01001362 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001363 */
Jens Axboeee738492007-01-10 11:23:16 +01001364 fill_default_options(&def_thread, options);
1365
Jens Axboe972cfd22006-06-09 11:08:56 +02001366 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001367 def_thread.write_bw_log = write_bw_log;
1368 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001369
Jens Axboeebac4652005-12-08 15:25:21 +01001370#ifdef FIO_HAVE_DISK_UTIL
1371 def_thread.do_disk_util = 1;
1372#endif
1373
1374 return 0;
1375}
1376
Jens Axboe0ab8db82006-10-18 17:16:23 +02001377static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001378{
1379 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001380 printf("\t--output\tWrite output to file\n");
1381 printf("\t--timeout\tRuntime in seconds\n");
1382 printf("\t--latency-log\tGenerate per-job latency logs\n");
1383 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1384 printf("\t--minimal\tMinimal (terse) output\n");
1385 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001386 printf("\t--help\t\tPrint this page\n");
1387 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001388}
1389
Jens Axboe972cfd22006-06-09 11:08:56 +02001390static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001391{
Jens Axboeb4692822006-10-27 13:43:22 +02001392 struct thread_data *td = NULL;
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001393 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001394
Jens Axboe0be06ea2007-03-01 14:23:28 +01001395 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001396 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001397 case 't':
1398 def_timeout = atoi(optarg);
1399 break;
1400 case 'l':
1401 write_lat_log = 1;
1402 break;
1403 case 'w':
1404 write_bw_log = 1;
1405 break;
1406 case 'o':
1407 f_out = fopen(optarg, "w+");
1408 if (!f_out) {
1409 perror("fopen output");
1410 exit(1);
1411 }
1412 f_err = f_out;
1413 break;
1414 case 'm':
1415 terse_output = 1;
1416 break;
1417 case 'h':
1418 usage();
1419 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001420 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001421 ret = show_cmd_help(options, optarg);
1422 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001423 case 'v':
1424 printf("%s\n", fio_version_string);
1425 exit(0);
1426 case FIO_GETOPT_JOB: {
1427 const char *opt = long_options[lidx].name;
1428 char *val = optarg;
1429
Jens Axboec2b1e752006-10-30 09:03:13 +01001430 if (!strncmp(opt, "name", 4) && td) {
1431 ret = add_job(td, td->name ?: "fio", 0);
1432 if (ret) {
1433 put_job(td);
1434 return 0;
1435 }
1436 td = NULL;
1437 }
Jens Axboeb4692822006-10-27 13:43:22 +02001438 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001439 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001440
1441 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001442 if (!td)
1443 return 0;
1444 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001445
Jens Axboeb1508cf2006-11-02 09:12:40 +01001446 ret = parse_cmd_option(opt, val, options, td);
Jens Axboe78217df2007-02-23 11:18:30 +01001447 if (ret)
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001448 dont_add_job = 1;
Jens Axboeb4692822006-10-27 13:43:22 +02001449 break;
1450 }
1451 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001452 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001453 }
1454 }
Jens Axboec9fad892006-05-27 17:26:50 +02001455
Jens Axboeb4692822006-10-27 13:43:22 +02001456 if (td) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001457 if (dont_add_job)
Jens Axboeb4692822006-10-27 13:43:22 +02001458 put_job(td);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001459 else {
1460 ret = add_job(td, td->name ?: "fio", 0);
1461 if (ret)
1462 put_job(td);
1463 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001464 }
Jens Axboe774a6172006-08-24 08:44:26 +02001465
Jens Axboeb4692822006-10-27 13:43:22 +02001466 while (optind < argc) {
1467 ini_idx++;
1468 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1469 ini_file[ini_idx - 1] = strdup(argv[optind]);
1470 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001471 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001472
1473 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001474}
1475
1476static void free_shm(void)
1477{
1478 struct shmid_ds sbuf;
1479
1480 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001481 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001482 threads = NULL;
1483 shmctl(shm_id, IPC_RMID, &sbuf);
1484 }
1485}
1486
Jens Axboe906c8d72006-06-13 09:37:56 +02001487/*
1488 * The thread area is shared between the main process and the job
1489 * threads/processes. So setup a shared memory segment that will hold
1490 * all the job info.
1491 */
Jens Axboeebac4652005-12-08 15:25:21 +01001492static int setup_thread_area(void)
1493{
1494 /*
1495 * 1024 is too much on some machines, scale max_jobs if
1496 * we get a failure that looks like too large a shm segment
1497 */
1498 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001499 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001500
Jens Axboe906c8d72006-06-13 09:37:56 +02001501 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001502 if (shm_id != -1)
1503 break;
1504 if (errno != EINVAL) {
1505 perror("shmget");
1506 break;
1507 }
1508
1509 max_jobs >>= 1;
1510 } while (max_jobs);
1511
1512 if (shm_id == -1)
1513 return 1;
1514
1515 threads = shmat(shm_id, NULL, 0);
1516 if (threads == (void *) -1) {
1517 perror("shmat");
1518 return 1;
1519 }
1520
1521 atexit(free_shm);
1522 return 0;
1523}
1524
Jens Axboeb4692822006-10-27 13:43:22 +02001525/*
1526 * Copy the fio options into the long options map, so we mirror
1527 * job and cmd line options.
1528 */
1529static void dupe_job_options(void)
1530{
1531 struct fio_option *o;
1532 unsigned int i;
1533
1534 i = 0;
1535 while (long_options[i].name)
1536 i++;
1537
1538 o = &options[0];
1539 while (o->name) {
1540 long_options[i].name = o->name;
1541 long_options[i].val = FIO_GETOPT_JOB;
1542 if (o->type == FIO_OPT_STR_SET)
1543 long_options[i].has_arg = no_argument;
1544 else
1545 long_options[i].has_arg = required_argument;
1546
1547 i++;
1548 o++;
1549 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1550 }
1551}
1552
Jens Axboeebac4652005-12-08 15:25:21 +01001553int parse_options(int argc, char *argv[])
1554{
Jens Axboe972cfd22006-06-09 11:08:56 +02001555 int job_files, i;
1556
Jens Axboeb4692822006-10-27 13:43:22 +02001557 f_out = stdout;
1558 f_err = stderr;
1559
Jens Axboe13335dd2007-01-10 13:14:02 +01001560 options_init(options);
1561
Jens Axboeb4692822006-10-27 13:43:22 +02001562 dupe_job_options();
1563
Jens Axboeebac4652005-12-08 15:25:21 +01001564 if (setup_thread_area())
1565 return 1;
1566 if (fill_def_thread())
1567 return 1;
1568
Jens Axboe972cfd22006-06-09 11:08:56 +02001569 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001570
Jens Axboe972cfd22006-06-09 11:08:56 +02001571 for (i = 0; i < job_files; i++) {
1572 if (fill_def_thread())
1573 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001574 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001575 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001576 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001577 }
Jens Axboeebac4652005-12-08 15:25:21 +01001578
Jens Axboe88c6ed82006-06-09 11:28:10 +02001579 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001580
1581 if (!thread_number) {
1582 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001583 return 1;
1584 }
1585
Jens Axboeebac4652005-12-08 15:25:21 +01001586 return 0;
1587}