blob: 4af00ff98b2273d9788312de6640caf80b157dd7 [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),
Jens Axboe9c60ce62007-03-15 09:14:47 +0100186 .help = "Total size of device or files",
187 },
188 {
189 .name = "filesize",
190 .type = FIO_OPT_STR_VAL,
191 .off1 = td_var_offset(file_size_low),
192 .off2 = td_var_offset(file_size_high),
193 .help = "Size of individual files",
Jens Axboe03b74b32007-01-11 11:04:31 +0100194 },
195 {
196 .name = "bs",
197 .type = FIO_OPT_STR_VAL_INT,
198 .off1 = td_var_offset(bs[DDIR_READ]),
199 .off2 = td_var_offset(bs[DDIR_WRITE]),
200 .help = "Block size unit",
201 .def = "4k",
202 },
203 {
204 .name = "bsrange",
205 .type = FIO_OPT_RANGE,
206 .off1 = td_var_offset(min_bs[DDIR_READ]),
207 .off2 = td_var_offset(max_bs[DDIR_READ]),
208 .off3 = td_var_offset(min_bs[DDIR_WRITE]),
209 .off4 = td_var_offset(max_bs[DDIR_WRITE]),
210 .help = "Set block size range (in more detail than bs)",
211 },
212 {
213 .name = "bs_unaligned",
214 .type = FIO_OPT_STR_SET,
215 .off1 = td_var_offset(bs_unaligned),
216 .help = "Don't sector align IO buffer sizes",
217 },
218 {
219 .name = "offset",
220 .type = FIO_OPT_STR_VAL,
221 .off1 = td_var_offset(start_offset),
222 .help = "Start IO from this offset",
223 .def = "0",
224 },
225 {
226 .name = "randrepeat",
227 .type = FIO_OPT_BOOL,
228 .off1 = td_var_offset(rand_repeatable),
229 .help = "Use repeatable random IO pattern",
230 .def = "1",
231 },
232 {
233 .name = "norandommap",
234 .type = FIO_OPT_STR_SET,
235 .off1 = td_var_offset(norandommap),
236 .help = "Accept potential duplicate random blocks",
237 },
238 {
239 .name = "nrfiles",
240 .type = FIO_OPT_INT,
241 .off1 = td_var_offset(nr_files),
242 .help = "Split job workload between this number of files",
243 .def = "1",
244 },
245 {
Jens Axboeb5af8292007-03-08 12:43:13 +0100246 .name = "openfiles",
247 .type = FIO_OPT_INT,
248 .off1 = td_var_offset(open_files),
249 .help = "Number of files to keep open at the same time",
250 },
251 {
Jens Axboe0aabe162007-02-23 08:45:55 +0100252 .name = "file_service_type",
253 .type = FIO_OPT_STR,
Jens Axboe1907dbc2007-03-12 11:44:28 +0100254 .cb = str_fst_cb,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100255 .off1 = td_var_offset(file_service_type),
Jens Axboe0aabe162007-02-23 08:45:55 +0100256 .help = "How to select which file to service next",
257 .def = "roundrobin",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100258 .posval = {
Jens Axboe78372132007-03-14 13:24:07 +0100259 { .ival = "random",
260 .oval = FIO_FSERVICE_RANDOM,
261 .help = "Choose a file at random",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100262 },
Jens Axboe78372132007-03-14 13:24:07 +0100263 { .ival = "roundrobin",
264 .oval = FIO_FSERVICE_RR,
265 .help = "Round robin select files",
266 },
267 },
Jens Axboe0aabe162007-02-23 08:45:55 +0100268 },
269 {
Jens Axboe03b74b32007-01-11 11:04:31 +0100270 .name = "fsync",
271 .type = FIO_OPT_INT,
272 .off1 = td_var_offset(fsync_blocks),
273 .help = "Issue fsync for writes every given number of blocks",
274 .def = "0",
275 },
276 {
277 .name = "direct",
278 .type = FIO_OPT_BOOL,
279 .off1 = td_var_offset(odirect),
Jens Axboe76a43db2007-01-11 13:24:44 +0100280 .help = "Use O_DIRECT IO (negates buffered)",
281 .def = "0",
282 },
283 {
284 .name = "buffered",
285 .type = FIO_OPT_BOOL,
286 .off1 = td_var_offset(odirect),
287 .neg = 1,
288 .help = "Use buffered IO (negates direct)",
Jens Axboe03b74b32007-01-11 11:04:31 +0100289 .def = "1",
290 },
291 {
292 .name = "overwrite",
293 .type = FIO_OPT_BOOL,
294 .off1 = td_var_offset(overwrite),
295 .help = "When writing, set whether to overwrite current data",
296 .def = "0",
297 },
298 {
299 .name = "loops",
300 .type = FIO_OPT_INT,
301 .off1 = td_var_offset(loops),
302 .help = "Number of times to run the job",
303 .def = "1",
304 },
305 {
306 .name = "numjobs",
307 .type = FIO_OPT_INT,
308 .off1 = td_var_offset(numjobs),
309 .help = "Duplicate this job this many times",
310 .def = "1",
311 },
312 {
313 .name = "startdelay",
314 .type = FIO_OPT_INT,
315 .off1 = td_var_offset(start_delay),
316 .help = "Only start job when this period has passed",
317 .def = "0",
318 },
319 {
320 .name = "runtime",
321 .alias = "timeout",
322 .type = FIO_OPT_STR_VAL_TIME,
323 .off1 = td_var_offset(timeout),
324 .help = "Stop workload when this amount of time has passed",
325 .def = "0",
326 },
327 {
Jens Axboee1f36502006-10-27 10:54:08 +0200328 .name = "mem",
329 .type = FIO_OPT_STR,
330 .cb = str_mem_cb,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100331 .off1 = td_var_offset(mem_type),
Jens Axboe15f79182007-01-10 12:26:18 +0100332 .help = "Backing type for IO buffers",
Jens Axboeee738492007-01-10 11:23:16 +0100333 .def = "malloc",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100334 .posval = {
Jens Axboe78372132007-03-14 13:24:07 +0100335 { .ival = "malloc",
336 .oval = MEM_MALLOC,
337 .help = "Use malloc(3) for IO buffers",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100338 },
Jens Axboe78372132007-03-14 13:24:07 +0100339 { .ival = "shm",
340 .oval = MEM_SHM,
341 .help = "Use shared memory segments for IO buffers",
342 },
343#ifdef FIO_HAVE_HUGETLB
344 { .ival = "shmhuge",
345 .oval = MEM_SHMHUGE,
346 .help = "Like shm, but use huge pages",
347 },
348#endif
349 { .ival = "mmap",
350 .oval = MEM_MMAP,
351 .help = "Use mmap(2) (file or anon) for IO buffers",
352 },
353#ifdef FIO_HAVE_HUGETLB
354 { .ival = "mmaphuge",
355 .oval = MEM_MMAPHUGE,
356 .help = "Like mmap, but use huge pages",
357 },
358#endif
359 },
Jens Axboee1f36502006-10-27 10:54:08 +0200360 },
361 {
362 .name = "verify",
363 .type = FIO_OPT_STR,
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100364 .off1 = td_var_offset(verify),
Jens Axboe9d6d0192007-03-14 13:28:31 +0100365 .help = "Verify data written",
Jens Axboeee738492007-01-10 11:23:16 +0100366 .def = "0",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100367 .posval = {
Jens Axboe78372132007-03-14 13:24:07 +0100368 { .ival = "0",
369 .oval = VERIFY_NONE,
370 .help = "Don't do IO verification",
Jens Axboeb1ec1da2007-02-23 10:29:16 +0100371 },
Jens Axboe78372132007-03-14 13:24:07 +0100372 { .ival = "crc32",
373 .oval = VERIFY_CRC32,
374 .help = "Use crc32 checksums for verification",
375 },
376 { .ival = "md5",
377 .oval = VERIFY_MD5,
378 .help = "Use md5 checksums for verification",
379 },
380 },
Jens Axboee1f36502006-10-27 10:54:08 +0200381 },
382 {
383 .name = "write_iolog",
Jens Axboe076efc72006-10-27 11:24:25 +0200384 .type = FIO_OPT_STR_STORE,
385 .off1 = td_var_offset(write_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100386 .help = "Store IO pattern to file",
Jens Axboee1f36502006-10-27 10:54:08 +0200387 },
388 {
Jens Axboe076efc72006-10-27 11:24:25 +0200389 .name = "read_iolog",
Jens Axboee1f36502006-10-27 10:54:08 +0200390 .type = FIO_OPT_STR_STORE,
Jens Axboe076efc72006-10-27 11:24:25 +0200391 .off1 = td_var_offset(read_iolog_file),
Jens Axboefd28ca42007-01-09 21:20:13 +0100392 .help = "Playback IO pattern from file",
Jens Axboee1f36502006-10-27 10:54:08 +0200393 },
394 {
395 .name = "exec_prerun",
396 .type = FIO_OPT_STR_STORE,
397 .off1 = td_var_offset(exec_prerun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100398 .help = "Execute this file prior to running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200399 },
400 {
401 .name = "exec_postrun",
402 .type = FIO_OPT_STR_STORE,
403 .off1 = td_var_offset(exec_postrun),
Jens Axboefd28ca42007-01-09 21:20:13 +0100404 .help = "Execute this file after running job",
Jens Axboee1f36502006-10-27 10:54:08 +0200405 },
406#ifdef FIO_HAVE_IOSCHED_SWITCH
407 {
408 .name = "ioscheduler",
409 .type = FIO_OPT_STR_STORE,
410 .off1 = td_var_offset(ioscheduler),
Jens Axboefd28ca42007-01-09 21:20:13 +0100411 .help = "Use this IO scheduler on the backing device",
Jens Axboee1f36502006-10-27 10:54:08 +0200412 },
413#endif
414 {
Jens Axboee1f36502006-10-27 10:54:08 +0200415 .name = "zonesize",
416 .type = FIO_OPT_STR_VAL,
417 .off1 = td_var_offset(zone_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100418 .help = "Give size of an IO zone",
Jens Axboeee738492007-01-10 11:23:16 +0100419 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200420 },
421 {
422 .name = "zoneskip",
423 .type = FIO_OPT_STR_VAL,
424 .off1 = td_var_offset(zone_skip),
Jens Axboefd28ca42007-01-09 21:20:13 +0100425 .help = "Space between IO zones",
Jens Axboeee738492007-01-10 11:23:16 +0100426 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200427 },
428 {
429 .name = "lockmem",
430 .type = FIO_OPT_STR_VAL,
431 .cb = str_lockmem_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100432 .help = "Lock down this amount of memory",
Jens Axboeee738492007-01-10 11:23:16 +0100433 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200434 },
435 {
Jens Axboee1f36502006-10-27 10:54:08 +0200436 .name = "rwmixcycle",
437 .type = FIO_OPT_INT,
438 .off1 = td_var_offset(rwmixcycle),
Jens Axboeee738492007-01-10 11:23:16 +0100439 .help = "Cycle period for mixed read/write workloads (msec)",
440 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200441 },
442 {
443 .name = "rwmixread",
444 .type = FIO_OPT_INT,
445 .off1 = td_var_offset(rwmixread),
Jens Axboeee738492007-01-10 11:23:16 +0100446 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100447 .help = "Percentage of mixed workload that is reads",
Jens Axboeee738492007-01-10 11:23:16 +0100448 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200449 },
450 {
451 .name = "rwmixwrite",
452 .type = FIO_OPT_INT,
453 .off1 = td_var_offset(rwmixwrite),
Jens Axboeee738492007-01-10 11:23:16 +0100454 .maxval = 100,
Jens Axboefd28ca42007-01-09 21:20:13 +0100455 .help = "Percentage of mixed workload that is writes",
Jens Axboeee738492007-01-10 11:23:16 +0100456 .def = "50",
Jens Axboee1f36502006-10-27 10:54:08 +0200457 },
458 {
459 .name = "nice",
460 .type = FIO_OPT_INT,
461 .off1 = td_var_offset(nice),
Jens Axboefd28ca42007-01-09 21:20:13 +0100462 .help = "Set job CPU nice value",
Jens Axboe15f79182007-01-10 12:26:18 +0100463 .minval = -19,
Jens Axboeee738492007-01-10 11:23:16 +0100464 .maxval = 20,
465 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200466 },
467#ifdef FIO_HAVE_IOPRIO
468 {
469 .name = "prio",
470 .type = FIO_OPT_INT,
471 .cb = str_prio_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100472 .help = "Set job IO priority value",
Jens Axboe15f79182007-01-10 12:26:18 +0100473 .minval = 0,
474 .maxval = 7,
Jens Axboee1f36502006-10-27 10:54:08 +0200475 },
476 {
477 .name = "prioclass",
478 .type = FIO_OPT_INT,
479 .cb = str_prioclass_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100480 .help = "Set job IO priority class",
Jens Axboe15f79182007-01-10 12:26:18 +0100481 .minval = 0,
482 .maxval = 3,
Jens Axboee1f36502006-10-27 10:54:08 +0200483 },
484#endif
485 {
486 .name = "thinktime",
487 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100488 .off1 = td_var_offset(thinktime),
Jens Axboe5fa0f812007-01-11 14:06:35 +0100489 .help = "Idle time between IO buffers (usec)",
Jens Axboeee738492007-01-10 11:23:16 +0100490 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200491 },
492 {
Jens Axboe48097d52007-02-17 06:30:44 +0100493 .name = "thinktime_spin",
494 .type = FIO_OPT_INT,
495 .off1 = td_var_offset(thinktime_spin),
Jens Axboe34403fb2007-03-02 21:43:25 +0100496 .help = "Start think time by spinning this amount (usec)",
Jens Axboe48097d52007-02-17 06:30:44 +0100497 .def = "0",
498 },
499 {
Jens Axboe9c1f7432007-01-03 20:43:19 +0100500 .name = "thinktime_blocks",
501 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100502 .off1 = td_var_offset(thinktime_blocks),
Jens Axboefd28ca42007-01-09 21:20:13 +0100503 .help = "IO buffer period between 'thinktime'",
Jens Axboeee738492007-01-10 11:23:16 +0100504 .def = "1",
Jens Axboe9c1f7432007-01-03 20:43:19 +0100505 },
506 {
Jens Axboee1f36502006-10-27 10:54:08 +0200507 .name = "rate",
508 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100509 .off1 = td_var_offset(rate),
Jens Axboefd28ca42007-01-09 21:20:13 +0100510 .help = "Set bandwidth rate",
Jens Axboee1f36502006-10-27 10:54:08 +0200511 },
512 {
513 .name = "ratemin",
514 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100515 .off1 = td_var_offset(ratemin),
Jens Axboefd28ca42007-01-09 21:20:13 +0100516 .help = "The bottom limit accepted",
Jens Axboee1f36502006-10-27 10:54:08 +0200517 },
518 {
519 .name = "ratecycle",
520 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100521 .off1 = td_var_offset(ratecycle),
Jens Axboe6da1fa72007-01-10 11:27:48 +0100522 .help = "Window average for rate limits (msec)",
Jens Axboeee738492007-01-10 11:23:16 +0100523 .def = "1000",
Jens Axboee1f36502006-10-27 10:54:08 +0200524 },
525 {
Jens Axboee1f36502006-10-27 10:54:08 +0200526 .name = "invalidate",
Jens Axboe13335dd2007-01-10 13:14:02 +0100527 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100528 .off1 = td_var_offset(invalidate_cache),
Jens Axboefd28ca42007-01-09 21:20:13 +0100529 .help = "Invalidate buffer/page cache prior to running job",
Jens Axboeee738492007-01-10 11:23:16 +0100530 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200531 },
532 {
533 .name = "sync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100534 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100535 .off1 = td_var_offset(sync_io),
Jens Axboefd28ca42007-01-09 21:20:13 +0100536 .help = "Use O_SYNC for buffered writes",
Jens Axboeee738492007-01-10 11:23:16 +0100537 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200538 },
539 {
540 .name = "bwavgtime",
541 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100542 .off1 = td_var_offset(bw_avg_time),
Jens Axboeee738492007-01-10 11:23:16 +0100543 .help = "Time window over which to calculate bandwidth (msec)",
544 .def = "500",
Jens Axboee1f36502006-10-27 10:54:08 +0200545 },
546 {
547 .name = "create_serialize",
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_serialize),
Jens Axboefd28ca42007-01-09 21:20:13 +0100550 .help = "Serialize creating of job files",
Jens Axboeee738492007-01-10 11:23:16 +0100551 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200552 },
553 {
554 .name = "create_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100555 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100556 .off1 = td_var_offset(create_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100557 .help = "Fsync file after creation",
Jens Axboeee738492007-01-10 11:23:16 +0100558 .def = "1",
Jens Axboee1f36502006-10-27 10:54:08 +0200559 },
560 {
Jens Axboee1f36502006-10-27 10:54:08 +0200561 .name = "cpuload",
562 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100563 .off1 = td_var_offset(cpuload),
Jens Axboefd28ca42007-01-09 21:20:13 +0100564 .help = "Use this percentage of CPU",
Jens Axboee1f36502006-10-27 10:54:08 +0200565 },
566 {
567 .name = "cpuchunks",
568 .type = FIO_OPT_INT,
Jens Axboe13049232007-01-03 20:45:09 +0100569 .off1 = td_var_offset(cpucycle),
Jens Axboeba0fbe12007-03-09 14:34:23 +0100570 .help = "Length of the CPU burn cycles (usecs)",
571 .def = "50000",
Jens Axboee1f36502006-10-27 10:54:08 +0200572 },
Jens Axboee1f36502006-10-27 10:54:08 +0200573#ifdef FIO_HAVE_CPU_AFFINITY
574 {
575 .name = "cpumask",
576 .type = FIO_OPT_INT,
577 .cb = str_cpumask_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100578 .help = "CPU affinity mask",
Jens Axboee1f36502006-10-27 10:54:08 +0200579 },
580#endif
581 {
582 .name = "end_fsync",
Jens Axboe13335dd2007-01-10 13:14:02 +0100583 .type = FIO_OPT_BOOL,
Jens Axboe13049232007-01-03 20:45:09 +0100584 .off1 = td_var_offset(end_fsync),
Jens Axboefd28ca42007-01-09 21:20:13 +0100585 .help = "Include fsync at the end of job",
Jens Axboeee738492007-01-10 11:23:16 +0100586 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200587 },
588 {
Jens Axboeebb14152007-03-13 14:42:15 +0100589 .name = "fsync_on_close",
590 .type = FIO_OPT_BOOL,
591 .off1 = td_var_offset(fsync_on_close),
592 .help = "fsync files on close",
593 .def = "0",
594 },
595 {
Jens Axboee1f36502006-10-27 10:54:08 +0200596 .name = "unlink",
Jens Axboe13335dd2007-01-10 13:14:02 +0100597 .type = FIO_OPT_BOOL,
Jens Axboee1f36502006-10-27 10:54:08 +0200598 .off1 = td_var_offset(unlink),
Jens Axboeee738492007-01-10 11:23:16 +0100599 .help = "Unlink created files after job has completed",
Jens Axboee545a6c2007-01-14 00:00:29 +0100600 .def = "0",
Jens Axboee1f36502006-10-27 10:54:08 +0200601 },
602 {
603 .name = "exitall",
604 .type = FIO_OPT_STR_SET,
605 .cb = str_exitall_cb,
Jens Axboefd28ca42007-01-09 21:20:13 +0100606 .help = "Terminate all jobs when one exits",
Jens Axboee1f36502006-10-27 10:54:08 +0200607 },
608 {
609 .name = "stonewall",
610 .type = FIO_OPT_STR_SET,
611 .off1 = td_var_offset(stonewall),
Jens Axboefd28ca42007-01-09 21:20:13 +0100612 .help = "Insert a hard barrier between this job and previous",
Jens Axboee1f36502006-10-27 10:54:08 +0200613 },
614 {
615 .name = "thread",
616 .type = FIO_OPT_STR_SET,
Jens Axboed9bb3b82007-01-10 20:30:53 +0100617 .off1 = td_var_offset(use_thread),
Jens Axboefd28ca42007-01-09 21:20:13 +0100618 .help = "Use threads instead of forks",
Jens Axboee1f36502006-10-27 10:54:08 +0200619 },
620 {
621 .name = "write_bw_log",
622 .type = FIO_OPT_STR_SET,
623 .off1 = td_var_offset(write_bw_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100624 .help = "Write log of bandwidth during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200625 },
626 {
627 .name = "write_lat_log",
628 .type = FIO_OPT_STR_SET,
629 .off1 = td_var_offset(write_lat_log),
Jens Axboefd28ca42007-01-09 21:20:13 +0100630 .help = "Write log of latency during run",
Jens Axboee1f36502006-10-27 10:54:08 +0200631 },
632 {
Jens Axboe56bb17f2006-12-20 20:27:36 +0100633 .name = "hugepage-size",
634 .type = FIO_OPT_STR_VAL,
635 .off1 = td_var_offset(hugepage_size),
Jens Axboefd28ca42007-01-09 21:20:13 +0100636 .help = "When using hugepages, specify size of each page",
Jens Axboeee738492007-01-10 11:23:16 +0100637 .def = __stringify(FIO_HUGE_PAGE),
Jens Axboe56bb17f2006-12-20 20:27:36 +0100638 },
639 {
Jens Axboeb2560f32007-03-06 12:43:03 +0100640 .name = "group_reporting",
641 .type = FIO_OPT_STR_SET,
642 .off1 = td_var_offset(group_reporting),
643 .help = "Do reporting on a per-group basis",
644 },
645 {
Jens Axboee1f36502006-10-27 10:54:08 +0200646 .name = NULL,
647 },
648};
649
Jens Axboeb4692822006-10-27 13:43:22 +0200650#define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option))
651#define FIO_CMD_OPTS (16)
652#define FIO_GETOPT_JOB (0x89988998)
653
654/*
655 * Command line options. These will contain the above, plus a few
656 * extra that only pertain to fio itself and not jobs.
657 */
658static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = {
659 {
660 .name = "output",
661 .has_arg = required_argument,
662 .val = 'o',
663 },
664 {
665 .name = "timeout",
666 .has_arg = required_argument,
667 .val = 't',
668 },
669 {
670 .name = "latency-log",
671 .has_arg = required_argument,
672 .val = 'l',
673 },
674 {
675 .name = "bandwidth-log",
676 .has_arg = required_argument,
677 .val = 'b',
678 },
679 {
680 .name = "minimal",
681 .has_arg = optional_argument,
682 .val = 'm',
683 },
684 {
685 .name = "version",
686 .has_arg = no_argument,
687 .val = 'v',
688 },
689 {
Jens Axboefd28ca42007-01-09 21:20:13 +0100690 .name = "help",
691 .has_arg = no_argument,
692 .val = 'h',
693 },
694 {
695 .name = "cmdhelp",
Jens Axboe320beef2007-03-01 10:44:12 +0100696 .has_arg = optional_argument,
Jens Axboefd28ca42007-01-09 21:20:13 +0100697 .val = 'c',
698 },
699 {
Jens Axboeb4692822006-10-27 13:43:22 +0200700 .name = NULL,
701 },
702};
703
Jens Axboeee738492007-01-10 11:23:16 +0100704static int def_timeout = 0;
Jens Axboe972cfd22006-06-09 11:08:56 +0200705
Jens Axboe54592a32007-03-14 14:24:11 +0100706static char fio_version_string[] = "fio 1.14a";
Jens Axboeebac4652005-12-08 15:25:21 +0100707
Jens Axboe972cfd22006-06-09 11:08:56 +0200708static char **ini_file;
Jens Axboeebac4652005-12-08 15:25:21 +0100709static int max_jobs = MAX_JOBS;
710
711struct thread_data def_thread;
712struct thread_data *threads = NULL;
713
Jens Axboeebac4652005-12-08 15:25:21 +0100714int exitall_on_terminate = 0;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200715int terse_output = 0;
Jens Axboec04f7ec2006-05-31 10:13:16 +0200716unsigned long long mlock_size = 0;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200717FILE *f_out = NULL;
718FILE *f_err = NULL;
Jens Axboeebac4652005-12-08 15:25:21 +0100719
Jens Axboeee738492007-01-10 11:23:16 +0100720static int write_lat_log = 0;
Jens Axboebb3884d2007-01-17 17:23:11 +1100721int write_bw_log = 0;
Jens Axboeec94ec52006-10-20 10:59:19 +0200722
Jens Axboe3c5df6f2007-03-12 15:09:24 +0100723static int prev_group_jobs;
724
Joel Becker9728ce32007-03-01 08:24:39 +0100725FILE *get_f_out()
726{
727 return f_out;
728}
729
730FILE *get_f_err()
731{
732 return f_err;
733}
734
Jens Axboe906c8d72006-06-13 09:37:56 +0200735/*
736 * Return a free job structure.
737 */
Jens Axboeebac4652005-12-08 15:25:21 +0100738static struct thread_data *get_new_job(int global, struct thread_data *parent)
739{
740 struct thread_data *td;
741
742 if (global)
743 return &def_thread;
744 if (thread_number >= max_jobs)
745 return NULL;
746
747 td = &threads[thread_number++];
Jens Axboeddaeaa52006-06-08 11:00:58 +0200748 *td = *parent;
Jens Axboeebac4652005-12-08 15:25:21 +0100749
Jens Axboeebac4652005-12-08 15:25:21 +0100750 td->thread_number = thread_number;
Jens Axboeebac4652005-12-08 15:25:21 +0100751 return td;
752}
753
754static void put_job(struct thread_data *td)
755{
Jens Axboe549577a2006-10-30 12:23:41 +0100756 if (td == &def_thread)
757 return;
758
Jens Axboe16edf252007-02-10 14:59:22 +0100759 if (td->error)
Jens Axboe6d861442007-03-15 09:22:23 +0100760 log_info("fio: %s\n", td->verror);
Jens Axboe16edf252007-02-10 14:59:22 +0100761
Jens Axboeebac4652005-12-08 15:25:21 +0100762 memset(&threads[td->thread_number - 1], 0, sizeof(*td));
763 thread_number--;
764}
765
Jens Axboedad915e2006-10-27 11:10:18 +0200766/*
767 * Lazy way of fixing up options that depend on each other. We could also
768 * define option callback handlers, but this is easier.
769 */
Jens Axboee1f36502006-10-27 10:54:08 +0200770static void fixup_options(struct thread_data *td)
771{
Jens Axboee1f36502006-10-27 10:54:08 +0200772 if (!td->rwmixread && td->rwmixwrite)
773 td->rwmixread = 100 - td->rwmixwrite;
Jens Axboedad915e2006-10-27 11:10:18 +0200774
Jens Axboe076efc72006-10-27 11:24:25 +0200775 if (td->write_iolog_file && td->read_iolog_file) {
776 log_err("fio: read iolog overrides write_iolog\n");
777 free(td->write_iolog_file);
778 td->write_iolog_file = NULL;
779 }
Jens Axboe16b462a2006-10-30 12:35:18 +0100780
781 if (td->io_ops->flags & FIO_SYNCIO)
782 td->iodepth = 1;
783 else {
784 if (!td->iodepth)
Jens Axboeb5af8292007-03-08 12:43:13 +0100785 td->iodepth = td->open_files;
Jens Axboe16b462a2006-10-30 12:35:18 +0100786 }
787
788 /*
789 * only really works for sequential io for now, and with 1 file
790 */
Jens Axboeb5af8292007-03-08 12:43:13 +0100791 if (td->zone_size && td_random(td) && td->open_files == 1)
Jens Axboe16b462a2006-10-30 12:35:18 +0100792 td->zone_size = 0;
793
794 /*
795 * Reads can do overwrites, we always need to pre-create the file
796 */
797 if (td_read(td) || td_rw(td))
798 td->overwrite = 1;
799
Jens Axboea00735e2006-11-03 08:58:08 +0100800 if (!td->min_bs[DDIR_READ])
801 td->min_bs[DDIR_READ]= td->bs[DDIR_READ];
802 if (!td->max_bs[DDIR_READ])
803 td->max_bs[DDIR_READ] = td->bs[DDIR_READ];
804 if (!td->min_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100805 td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100806 if (!td->max_bs[DDIR_WRITE])
Jens Axboe75e6f362006-11-03 08:17:09 +0100807 td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE];
Jens Axboea00735e2006-11-03 08:58:08 +0100808
809 td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]);
810
Jens Axboe9c60ce62007-03-15 09:14:47 +0100811 if (!td->file_size_high)
812 td->file_size_high = td->file_size_low;
813
Jens Axboe16b462a2006-10-30 12:35:18 +0100814 if (td_read(td) && !td_rw(td))
815 td->verify = 0;
Jens Axboebb8895e2006-10-30 15:14:48 +0100816
817 if (td->norandommap && td->verify != VERIFY_NONE) {
818 log_err("fio: norandommap given, verify disabled\n");
819 td->verify = VERIFY_NONE;
820 }
Jens Axboe690adba2006-10-30 15:25:09 +0100821 if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO))
822 log_err("fio: bs_unaligned may not work with raw io\n");
Jens Axboee0a22332006-12-20 12:54:25 +0100823
824 /*
Jens Axboe48097d52007-02-17 06:30:44 +0100825 * thinktime_spin must be less than thinktime
826 */
827 if (td->thinktime_spin > td->thinktime)
828 td->thinktime_spin = td->thinktime;
Jens Axboee916b392007-02-20 14:37:26 +0100829
830 /*
831 * The low water mark cannot be bigger than the iodepth
832 */
Jens Axboe9467b772007-02-27 19:56:43 +0100833 if (td->iodepth_low > td->iodepth || !td->iodepth_low) {
834 /*
835 * syslet work around - if the workload is sequential,
836 * we want to let the queue drain all the way down to
837 * avoid seeking between async threads
838 */
839 if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td))
840 td->iodepth_low = 1;
841 else
842 td->iodepth_low = td->iodepth;
843 }
Jens Axboecb5ab512007-02-26 12:57:09 +0100844
845 /*
846 * If batch number isn't set, default to the same as iodepth
847 */
848 if (td->iodepth_batch > td->iodepth || !td->iodepth_batch)
849 td->iodepth_batch = td->iodepth;
Jens Axboeb5af8292007-03-08 12:43:13 +0100850
Jens Axboebbf6b542007-03-13 15:28:55 +0100851 if (td->nr_files > td->files_index)
Jens Axboe9f9214f2007-03-13 14:02:16 +0100852 td->nr_files = td->files_index;
853
854 if (td->open_files > td->nr_files || !td->open_files)
Jens Axboeb5af8292007-03-08 12:43:13 +0100855 td->open_files = td->nr_files;
Jens Axboee1f36502006-10-27 10:54:08 +0200856}
857
Jens Axboe906c8d72006-06-13 09:37:56 +0200858/*
Jens Axboef8977ee2006-11-06 14:03:03 +0100859 * This function leaks the buffer
860 */
861static char *to_kmg(unsigned int val)
862{
863 char *buf = malloc(32);
Jens Axboef3502ba2007-02-14 01:27:09 +0100864 char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 };
Jens Axboef8977ee2006-11-06 14:03:03 +0100865 char *p = post;
866
Jens Axboe245142f2006-11-08 12:49:21 +0100867 do {
Jens Axboef8977ee2006-11-06 14:03:03 +0100868 if (val & 1023)
869 break;
870
871 val >>= 10;
872 p++;
Jens Axboe245142f2006-11-08 12:49:21 +0100873 } while (*p);
Jens Axboef8977ee2006-11-06 14:03:03 +0100874
875 snprintf(buf, 31, "%u%c", val, *p);
876 return buf;
877}
878
Jens Axboe09629a92007-03-09 09:00:06 +0100879/* External engines are specified by "external:name.o") */
880static const char *get_engine_name(const char *str)
881{
882 char *p = strstr(str, ":");
883
884 if (!p)
885 return str;
886
887 p++;
888 strip_blank_front(&p);
889 strip_blank_end(p);
890 return p;
891}
892
Jens Axboee132cba2007-03-14 14:23:54 +0100893static int exists_and_not_file(const char *filename)
894{
895 struct stat sb;
896
897 if (lstat(filename, &sb) == -1)
898 return 0;
899
900 if (S_ISREG(sb.st_mode))
901 return 0;
902
903 return 1;
904}
905
Jens Axboef8977ee2006-11-06 14:03:03 +0100906/*
Jens Axboe9c60ce62007-03-15 09:14:47 +0100907 * Initialize the various random states we need (random io, block size ranges,
908 * read/write mix, etc).
909 */
910static int init_random_state(struct thread_data *td)
911{
912 unsigned long seeds[6];
913 int fd, num_maps, blocks;
914 struct fio_file *f;
915 unsigned int i;
916
917 fd = open("/dev/urandom", O_RDONLY);
918 if (fd == -1) {
919 td_verror(td, errno, "open");
920 return 1;
921 }
922
923 if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) {
924 td_verror(td, EIO, "read");
925 close(fd);
926 return 1;
927 }
928
929 close(fd);
930
931 os_random_seed(seeds[0], &td->bsrange_state);
932 os_random_seed(seeds[1], &td->verify_state);
933 os_random_seed(seeds[2], &td->rwmix_state);
934
935 if (td->file_service_type == FIO_FSERVICE_RANDOM)
936 os_random_seed(seeds[3], &td->next_file_state);
937
938 os_random_seed(seeds[5], &td->file_size_state);
939
940 if (!td_random(td))
941 return 0;
942
943 if (td->rand_repeatable)
944 seeds[4] = FIO_RANDSEED * td->thread_number;
945
946 if (!td->norandommap) {
947 for_each_file(td, f, i) {
948 blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs;
949 num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP;
950 f->file_map = malloc(num_maps * sizeof(long));
951 if (!f->file_map) {
952 log_err("fio: failed allocating random map. If running a large number of jobs, try the 'norandommap' option\n");
953 return 1;
954 }
955 f->num_maps = num_maps;
956 memset(f->file_map, 0, num_maps * sizeof(long));
957 }
958 }
959
960 os_random_seed(seeds[4], &td->random_state);
961 return 0;
962}
963
964
965/*
Jens Axboe906c8d72006-06-13 09:37:56 +0200966 * Adds a job to the list of things todo. Sanitizes the various options
967 * to make sure we don't have conflicts, and initializes various
968 * members of td.
969 */
Jens Axboe75154842006-06-01 13:56:09 +0200970static int add_job(struct thread_data *td, const char *jobname, int job_add_num)
Jens Axboeebac4652005-12-08 15:25:21 +0100971{
Jens Axboe413dd452007-02-23 09:26:09 +0100972 const char *ddir_str[] = { NULL, "read", "write", "rw", NULL,
973 "randread", "randwrite", "randrw" };
Jens Axboeaf52b342007-03-13 10:07:47 +0100974 unsigned int i;
Jens Axboe53cdc682006-10-18 11:50:58 +0200975 struct fio_file *f;
Jens Axboe09629a92007-03-09 09:00:06 +0100976 const char *engine;
Jens Axboeaf52b342007-03-13 10:07:47 +0100977 char fname[PATH_MAX];
Jens Axboee132cba2007-03-14 14:23:54 +0100978 int numjobs, file_alloced;
Jens Axboeebac4652005-12-08 15:25:21 +0100979
Jens Axboeebac4652005-12-08 15:25:21 +0100980 /*
981 * the def_thread is just for options, it's not a real job
982 */
983 if (td == &def_thread)
984 return 0;
985
Jens Axboe09629a92007-03-09 09:00:06 +0100986 engine = get_engine_name(td->ioengine);
987 td->io_ops = load_ioengine(td, engine);
988 if (!td->io_ops) {
989 log_err("fio: failed to load engine %s\n", engine);
990 return 1;
991 }
Jens Axboedf641192006-10-12 07:55:41 +0200992
Jens Axboe9cedf162007-03-12 11:29:30 +0100993 if (td->use_thread)
994 nr_thread++;
995 else
996 nr_process++;
997
Jens Axboe690adba2006-10-30 15:25:09 +0100998 if (td->odirect)
999 td->io_ops->flags |= FIO_RAWIO;
1000
Jens Axboee132cba2007-03-14 14:23:54 +01001001 file_alloced = 0;
Jens Axboebbf6b542007-03-13 15:28:55 +01001002 if (!td->filename && !td->files_index) {
Jens Axboee132cba2007-03-14 14:23:54 +01001003 file_alloced = 1;
Jens Axboe80be24f2007-03-12 11:01:25 +01001004
Jens Axboee132cba2007-03-14 14:23:54 +01001005 if (td->nr_files == 1 && exists_and_not_file(jobname))
1006 add_file(td, jobname);
Jens Axboe7b05a212007-03-13 11:17:07 +01001007 else {
1008 for (i = 0; i < td->nr_files; i++) {
Jens Axboee132cba2007-03-14 14:23:54 +01001009 sprintf(fname, "%s.%d.%d", jobname, td->thread_number, i);
Jens Axboe7b05a212007-03-13 11:17:07 +01001010 add_file(td, fname);
1011 }
Jens Axboeaf52b342007-03-13 10:07:47 +01001012 }
Jens Axboe0af7b542006-02-17 10:10:12 +01001013 }
Jens Axboeebac4652005-12-08 15:25:21 +01001014
Jens Axboee0a22332006-12-20 12:54:25 +01001015 fixup_options(td);
1016
Jens Axboe53cdc682006-10-18 11:50:58 +02001017 for_each_file(td, f, i) {
Jens Axboeaf52b342007-03-13 10:07:47 +01001018 if (td->directory && f->filetype == FIO_TYPE_FILE) {
1019 sprintf(fname, "%s/%s", td->directory, f->file_name);
1020 f->file_name = strdup(fname);
1021 }
Jens Axboe53cdc682006-10-18 11:50:58 +02001022 }
1023
Jens Axboe07739b52007-03-08 20:25:46 +01001024 td->mutex = fio_sem_init(0);
Jens Axboeebac4652005-12-08 15:25:21 +01001025
Jens Axboe756867b2007-03-06 15:19:24 +01001026 td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX;
1027 td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX;
1028 td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX;
Jens Axboeebac4652005-12-08 15:25:21 +01001029
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001030 if ((td->stonewall || td->numjobs > 1) && prev_group_jobs) {
1031 prev_group_jobs = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001032 groupid++;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001033 }
Jens Axboeebac4652005-12-08 15:25:21 +01001034
1035 td->groupid = groupid;
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001036 prev_group_jobs++;
Jens Axboeebac4652005-12-08 15:25:21 +01001037
Jens Axboe9c60ce62007-03-15 09:14:47 +01001038 if (init_random_state(td))
1039 goto err;
1040
Jens Axboeebac4652005-12-08 15:25:21 +01001041 if (setup_rate(td))
1042 goto err;
1043
Jens Axboeec94ec52006-10-20 10:59:19 +02001044 if (td->write_lat_log) {
Jens Axboe756867b2007-03-06 15:19:24 +01001045 setup_log(&td->ts.slat_log);
1046 setup_log(&td->ts.clat_log);
Jens Axboeebac4652005-12-08 15:25:21 +01001047 }
Jens Axboeec94ec52006-10-20 10:59:19 +02001048 if (td->write_bw_log)
Jens Axboe756867b2007-03-06 15:19:24 +01001049 setup_log(&td->ts.bw_log);
Jens Axboeebac4652005-12-08 15:25:21 +01001050
Jens Axboeb4692822006-10-27 13:43:22 +02001051 if (!td->name)
1052 td->name = strdup(jobname);
Jens Axboe01452052006-06-07 10:29:47 +02001053
Jens Axboec6ae0a52006-06-12 11:04:44 +02001054 if (!terse_output) {
Jens Axboeb990b5c2006-09-14 09:48:22 +02001055 if (!job_add_num) {
Jens Axboeba0fbe12007-03-09 14:34:23 +01001056 if (!strcmp(td->io_ops->name, "cpuio"))
Jens Axboe6d861442007-03-15 09:22:23 +01001057 log_info("%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle);
Jens Axboef8977ee2006-11-06 14:03:03 +01001058 else {
1059 char *c1, *c2, *c3, *c4;
1060
1061 c1 = to_kmg(td->min_bs[DDIR_READ]);
1062 c2 = to_kmg(td->max_bs[DDIR_READ]);
1063 c3 = to_kmg(td->min_bs[DDIR_WRITE]);
1064 c4 = to_kmg(td->max_bs[DDIR_WRITE]);
1065
Jens Axboe6d861442007-03-15 09:22:23 +01001066 log_info("%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 +01001067
1068 free(c1);
1069 free(c2);
1070 free(c3);
1071 free(c4);
1072 }
Jens Axboeb990b5c2006-09-14 09:48:22 +02001073 } else if (job_add_num == 1)
Jens Axboe6d861442007-03-15 09:22:23 +01001074 log_info("...\n");
Jens Axboec6ae0a52006-06-12 11:04:44 +02001075 }
Jens Axboeebac4652005-12-08 15:25:21 +01001076
1077 /*
1078 * recurse add identical jobs, clear numjobs and stonewall options
1079 * as they don't apply to sub-jobs
1080 */
1081 numjobs = td->numjobs;
1082 while (--numjobs) {
1083 struct thread_data *td_new = get_new_job(0, td);
1084
1085 if (!td_new)
1086 goto err;
1087
1088 td_new->numjobs = 1;
1089 td_new->stonewall = 0;
Jens Axboee132cba2007-03-14 14:23:54 +01001090
1091 if (file_alloced) {
1092 td_new->filename = NULL;
1093 td_new->files_index = 0;
1094 td_new->files = NULL;
1095 }
1096
Jens Axboe75154842006-06-01 13:56:09 +02001097 job_add_num = numjobs - 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001098
Jens Axboe75154842006-06-01 13:56:09 +02001099 if (add_job(td_new, jobname, job_add_num))
Jens Axboeebac4652005-12-08 15:25:21 +01001100 goto err;
1101 }
Jens Axboe3c5df6f2007-03-12 15:09:24 +01001102
1103 if (td->numjobs > 1) {
1104 groupid++;
1105 prev_group_jobs = 0;
1106 }
1107
Jens Axboeebac4652005-12-08 15:25:21 +01001108 return 0;
1109err:
1110 put_job(td);
1111 return -1;
1112}
1113
Jens Axboeebac4652005-12-08 15:25:21 +01001114static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu)
1115{
1116#ifdef FIO_HAVE_CPU_AFFINITY
1117 unsigned int i;
1118
1119 CPU_ZERO(&cpumask);
1120
1121 for (i = 0; i < sizeof(int) * 8; i++) {
1122 if ((1 << i) & cpu)
1123 CPU_SET(i, &cpumask);
1124 }
1125#endif
1126}
1127
Jens Axboeebac4652005-12-08 15:25:21 +01001128static int is_empty_or_comment(char *line)
1129{
1130 unsigned int i;
1131
1132 for (i = 0; i < strlen(line); i++) {
1133 if (line[i] == ';')
1134 return 1;
Ingo Molnar5cc2da32007-02-20 10:19:44 +01001135 if (line[i] == '#')
1136 return 1;
Jens Axboeebac4652005-12-08 15:25:21 +01001137 if (!isspace(line[i]) && !iscntrl(line[i]))
1138 return 0;
1139 }
1140
1141 return 1;
1142}
1143
Jens Axboe313cb202006-12-21 09:50:00 +01001144/*
1145 * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that.
1146 */
Jens Axboe1907dbc2007-03-12 11:44:28 +01001147static char *get_opt_postfix(const char *str)
Jens Axboe313cb202006-12-21 09:50:00 +01001148{
1149 char *p = strstr(str, ":");
1150
1151 if (!p)
1152 return NULL;
1153
1154 p++;
1155 strip_blank_front(&p);
1156 strip_blank_end(p);
1157 return strdup(p);
1158}
1159
Jens Axboeb4692822006-10-27 13:43:22 +02001160static int str_mem_cb(void *data, const char *mem)
Jens Axboeebac4652005-12-08 15:25:21 +01001161{
Jens Axboecb2c86f2006-10-26 13:48:34 +02001162 struct thread_data *td = data;
1163
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001164 if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) {
Jens Axboe1907dbc2007-03-12 11:44:28 +01001165 td->mmapfile = get_opt_postfix(mem);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001166 if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) {
Jens Axboed0bdaf42006-12-20 14:40:44 +01001167 log_err("fio: mmaphuge:/path/to/file\n");
1168 return 1;
1169 }
Jens Axboeebac4652005-12-08 15:25:21 +01001170 }
1171
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001172 return 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001173}
1174
Jens Axboee1f36502006-10-27 10:54:08 +02001175static int str_lockmem_cb(void fio_unused *data, unsigned long *val)
1176{
1177 mlock_size = *val;
1178 return 0;
1179}
1180
Jens Axboe34cfcda2006-11-03 14:00:45 +01001181#ifdef FIO_HAVE_IOPRIO
Jens Axboee1f36502006-10-27 10:54:08 +02001182static int str_prioclass_cb(void *data, unsigned int *val)
1183{
1184 struct thread_data *td = data;
1185
1186 td->ioprio |= *val << IOPRIO_CLASS_SHIFT;
1187 return 0;
1188}
1189
1190static int str_prio_cb(void *data, unsigned int *val)
1191{
1192 struct thread_data *td = data;
1193
1194 td->ioprio |= *val;
1195 return 0;
1196}
Jens Axboe34cfcda2006-11-03 14:00:45 +01001197#endif
Jens Axboee1f36502006-10-27 10:54:08 +02001198
1199static int str_exitall_cb(void)
1200{
1201 exitall_on_terminate = 1;
1202 return 0;
1203}
1204
1205static int str_cpumask_cb(void *data, unsigned int *val)
1206{
1207 struct thread_data *td = data;
1208
1209 fill_cpu_mask(td->cpumask, *val);
1210 return 0;
1211}
1212
Jens Axboe1907dbc2007-03-12 11:44:28 +01001213static int str_fst_cb(void *data, const char *str)
1214{
1215 struct thread_data *td = data;
1216 char *nr = get_opt_postfix(str);
1217
1218 td->file_service_nr = 1;
1219 if (nr)
1220 td->file_service_nr = atoi(nr);
1221
1222 return 0;
1223}
1224
Jens Axboeaf52b342007-03-13 10:07:47 +01001225static int str_filename_cb(void *data, const char *input)
1226{
1227 struct thread_data *td = data;
Jens Axboecae61952007-03-13 11:12:14 +01001228 char *fname, *str, *p;
Jens Axboeaf52b342007-03-13 10:07:47 +01001229
Jens Axboecae61952007-03-13 11:12:14 +01001230 p = str = strdup(input);
Jens Axboee13363e2007-03-13 11:18:57 +01001231
1232 strip_blank_front(&str);
1233 strip_blank_end(str);
1234
Jens Axboebbf6b542007-03-13 15:28:55 +01001235 if (!td->files_index)
1236 td->nr_files = 0;
1237
Jens Axboe0ad920e2007-03-13 11:06:45 +01001238 while ((fname = strsep(&str, ":")) != NULL) {
Jens Axboee13363e2007-03-13 11:18:57 +01001239 if (!strlen(fname))
1240 break;
Jens Axboeaf52b342007-03-13 10:07:47 +01001241 add_file(td, fname);
Jens Axboebbf6b542007-03-13 15:28:55 +01001242 td->nr_files++;
Jens Axboe0ad920e2007-03-13 11:06:45 +01001243 }
Jens Axboeaf52b342007-03-13 10:07:47 +01001244
Jens Axboecae61952007-03-13 11:12:14 +01001245 free(p);
Jens Axboeaf52b342007-03-13 10:07:47 +01001246 return 0;
1247}
1248
1249static int str_directory_cb(void *data, const char fio_unused *str)
1250{
1251 struct thread_data *td = data;
1252 struct stat sb;
1253
1254 if (lstat(td->directory, &sb) < 0) {
1255 log_err("fio: %s is not a directory\n", td->directory);
1256 td_verror(td, errno, "lstat");
1257 return 1;
1258 }
1259 if (!S_ISDIR(sb.st_mode)) {
1260 log_err("fio: %s is not a directory\n", td->directory);
1261 return 1;
1262 }
1263
1264 return 0;
1265}
1266
Jens Axboebbf6b542007-03-13 15:28:55 +01001267static int str_opendir_cb(void *data, const char fio_unused *str)
1268{
1269 struct thread_data *td = data;
1270
1271 if (!td->files_index)
1272 td->nr_files = 0;
1273
1274 return add_dir_files(td, td->opendir);
1275}
1276
Jens Axboe07261982006-06-07 10:51:12 +02001277/*
1278 * This is our [ini] type file parser.
1279 */
Jens Axboe1e97cce2006-12-05 11:44:16 +01001280static int parse_jobs_ini(char *file, int stonewall_flag)
Jens Axboeebac4652005-12-08 15:25:21 +01001281{
Jens Axboee1f36502006-10-27 10:54:08 +02001282 unsigned int global;
Jens Axboeebac4652005-12-08 15:25:21 +01001283 struct thread_data *td;
Jens Axboefee3bb42006-10-30 13:32:08 +01001284 char *string, *name;
Jens Axboeebac4652005-12-08 15:25:21 +01001285 fpos_t off;
1286 FILE *f;
1287 char *p;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001288 int ret = 0, stonewall;
Jens Axboeebac4652005-12-08 15:25:21 +01001289
1290 f = fopen(file, "r");
1291 if (!f) {
Jens Axboeaea47d42006-05-26 19:27:29 +02001292 perror("fopen job file");
Jens Axboeebac4652005-12-08 15:25:21 +01001293 return 1;
1294 }
1295
1296 string = malloc(4096);
1297 name = malloc(256);
Jens Axboefee3bb42006-10-30 13:32:08 +01001298 memset(name, 0, 256);
Jens Axboeebac4652005-12-08 15:25:21 +01001299
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001300 stonewall = stonewall_flag;
Jens Axboe7c124ac2006-10-30 13:36:52 +01001301 do {
1302 p = fgets(string, 4095, f);
1303 if (!p)
Jens Axboe45410ac2006-06-07 11:10:39 +02001304 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001305 if (is_empty_or_comment(p))
1306 continue;
Jens Axboefee3bb42006-10-30 13:32:08 +01001307 if (sscanf(p, "[%255s]", name) != 1)
Jens Axboeebac4652005-12-08 15:25:21 +01001308 continue;
1309
1310 global = !strncmp(name, "global", 6);
1311
1312 name[strlen(name) - 1] = '\0';
1313
1314 td = get_new_job(global, &def_thread);
Jens Axboe45410ac2006-06-07 11:10:39 +02001315 if (!td) {
1316 ret = 1;
1317 break;
1318 }
Jens Axboeebac4652005-12-08 15:25:21 +01001319
Jens Axboe972cfd22006-06-09 11:08:56 +02001320 /*
1321 * Seperate multiple job files by a stonewall
1322 */
Jens Axboef9481912006-06-09 11:37:28 +02001323 if (!global && stonewall) {
Jens Axboe972cfd22006-06-09 11:08:56 +02001324 td->stonewall = stonewall;
1325 stonewall = 0;
1326 }
1327
Jens Axboeebac4652005-12-08 15:25:21 +01001328 fgetpos(f, &off);
1329 while ((p = fgets(string, 4096, f)) != NULL) {
1330 if (is_empty_or_comment(p))
1331 continue;
Jens Axboee1f36502006-10-27 10:54:08 +02001332
Jens Axboeb6754f92006-05-30 14:29:32 +02001333 strip_blank_front(&p);
Jens Axboe7c124ac2006-10-30 13:36:52 +01001334
1335 if (p[0] == '[')
1336 break;
1337
Jens Axboe4ae3f762006-05-30 13:35:14 +02001338 strip_blank_end(p);
Jens Axboeaea47d42006-05-26 19:27:29 +02001339
Jens Axboee1f36502006-10-27 10:54:08 +02001340 fgetpos(f, &off);
Jens Axboeebac4652005-12-08 15:25:21 +01001341
Jens Axboe45410ac2006-06-07 11:10:39 +02001342 /*
1343 * Don't break here, continue parsing options so we
1344 * dump all the bad ones. Makes trial/error fixups
1345 * easier on the user.
1346 */
Jens Axboe7c124ac2006-10-30 13:36:52 +01001347 ret |= parse_option(p, options, td);
Jens Axboeebac4652005-12-08 15:25:21 +01001348 }
Jens Axboeebac4652005-12-08 15:25:21 +01001349
Jens Axboe45410ac2006-06-07 11:10:39 +02001350 if (!ret) {
1351 fsetpos(f, &off);
1352 ret = add_job(td, name, 0);
Jens Axboeb1508cf2006-11-02 09:12:40 +01001353 } else {
1354 log_err("fio: job %s dropped\n", name);
1355 put_job(td);
Jens Axboe45410ac2006-06-07 11:10:39 +02001356 }
Jens Axboe7c124ac2006-10-30 13:36:52 +01001357 } while (!ret);
Jens Axboeebac4652005-12-08 15:25:21 +01001358
1359 free(string);
1360 free(name);
1361 fclose(f);
Jens Axboe45410ac2006-06-07 11:10:39 +02001362 return ret;
Jens Axboeebac4652005-12-08 15:25:21 +01001363}
1364
1365static int fill_def_thread(void)
1366{
1367 memset(&def_thread, 0, sizeof(def_thread));
1368
1369 if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) {
1370 perror("sched_getaffinity");
1371 return 1;
1372 }
1373
1374 /*
Jens Axboeee738492007-01-10 11:23:16 +01001375 * fill default options
Jens Axboeebac4652005-12-08 15:25:21 +01001376 */
Jens Axboeee738492007-01-10 11:23:16 +01001377 fill_default_options(&def_thread, options);
1378
Jens Axboe972cfd22006-06-09 11:08:56 +02001379 def_thread.timeout = def_timeout;
Jens Axboeec94ec52006-10-20 10:59:19 +02001380 def_thread.write_bw_log = write_bw_log;
1381 def_thread.write_lat_log = write_lat_log;
Jens Axboeee738492007-01-10 11:23:16 +01001382
Jens Axboeebac4652005-12-08 15:25:21 +01001383#ifdef FIO_HAVE_DISK_UTIL
1384 def_thread.do_disk_util = 1;
1385#endif
1386
1387 return 0;
1388}
1389
Jens Axboe0ab8db82006-10-18 17:16:23 +02001390static void usage(void)
Jens Axboe4785f992006-05-26 03:59:10 +02001391{
1392 printf("%s\n", fio_version_string);
Jens Axboeb4692822006-10-27 13:43:22 +02001393 printf("\t--output\tWrite output to file\n");
1394 printf("\t--timeout\tRuntime in seconds\n");
1395 printf("\t--latency-log\tGenerate per-job latency logs\n");
1396 printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n");
1397 printf("\t--minimal\tMinimal (terse) output\n");
1398 printf("\t--version\tPrint version info and exit\n");
Jens Axboefd28ca42007-01-09 21:20:13 +01001399 printf("\t--help\t\tPrint this page\n");
1400 printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n");
Jens Axboe4785f992006-05-26 03:59:10 +02001401}
1402
Jens Axboe972cfd22006-06-09 11:08:56 +02001403static int parse_cmd_line(int argc, char *argv[])
Jens Axboeebac4652005-12-08 15:25:21 +01001404{
Jens Axboeb4692822006-10-27 13:43:22 +02001405 struct thread_data *td = NULL;
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001406 int c, ini_idx = 0, lidx, ret, dont_add_job = 0;
Jens Axboeebac4652005-12-08 15:25:21 +01001407
Jens Axboe0be06ea2007-03-01 14:23:28 +01001408 while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) {
Jens Axboeebac4652005-12-08 15:25:21 +01001409 switch (c) {
Jens Axboeb4692822006-10-27 13:43:22 +02001410 case 't':
1411 def_timeout = atoi(optarg);
1412 break;
1413 case 'l':
1414 write_lat_log = 1;
1415 break;
1416 case 'w':
1417 write_bw_log = 1;
1418 break;
1419 case 'o':
1420 f_out = fopen(optarg, "w+");
1421 if (!f_out) {
1422 perror("fopen output");
1423 exit(1);
1424 }
1425 f_err = f_out;
1426 break;
1427 case 'm':
1428 terse_output = 1;
1429 break;
1430 case 'h':
1431 usage();
1432 exit(0);
Jens Axboefd28ca42007-01-09 21:20:13 +01001433 case 'c':
Jens Axboe29fc6af2007-01-09 21:22:02 +01001434 ret = show_cmd_help(options, optarg);
1435 exit(ret);
Jens Axboeb4692822006-10-27 13:43:22 +02001436 case 'v':
1437 printf("%s\n", fio_version_string);
1438 exit(0);
1439 case FIO_GETOPT_JOB: {
1440 const char *opt = long_options[lidx].name;
1441 char *val = optarg;
1442
Jens Axboec2b1e752006-10-30 09:03:13 +01001443 if (!strncmp(opt, "name", 4) && td) {
1444 ret = add_job(td, td->name ?: "fio", 0);
1445 if (ret) {
1446 put_job(td);
1447 return 0;
1448 }
1449 td = NULL;
1450 }
Jens Axboeb4692822006-10-27 13:43:22 +02001451 if (!td) {
Jens Axboe38d0adb2006-10-30 12:21:25 +01001452 int global = !strncmp(val, "global", 6);
Jens Axboec2b1e752006-10-30 09:03:13 +01001453
1454 td = get_new_job(global, &def_thread);
Jens Axboeb4692822006-10-27 13:43:22 +02001455 if (!td)
1456 return 0;
1457 }
Jens Axboe38d0adb2006-10-30 12:21:25 +01001458
Jens Axboeb1508cf2006-11-02 09:12:40 +01001459 ret = parse_cmd_option(opt, val, options, td);
Jens Axboe78217df2007-02-23 11:18:30 +01001460 if (ret)
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001461 dont_add_job = 1;
Jens Axboeb4692822006-10-27 13:43:22 +02001462 break;
1463 }
1464 default:
Jens Axboeb4692822006-10-27 13:43:22 +02001465 break;
Jens Axboeebac4652005-12-08 15:25:21 +01001466 }
1467 }
Jens Axboec9fad892006-05-27 17:26:50 +02001468
Jens Axboeb4692822006-10-27 13:43:22 +02001469 if (td) {
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001470 if (dont_add_job)
Jens Axboeb4692822006-10-27 13:43:22 +02001471 put_job(td);
Jens Axboeb1ec1da2007-02-23 10:29:16 +01001472 else {
1473 ret = add_job(td, td->name ?: "fio", 0);
1474 if (ret)
1475 put_job(td);
1476 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001477 }
Jens Axboe774a6172006-08-24 08:44:26 +02001478
Jens Axboeb4692822006-10-27 13:43:22 +02001479 while (optind < argc) {
1480 ini_idx++;
1481 ini_file = realloc(ini_file, ini_idx * sizeof(char *));
1482 ini_file[ini_idx - 1] = strdup(argv[optind]);
1483 optind++;
Jens Axboeeb8bbf42006-06-08 21:40:11 +02001484 }
Jens Axboe972cfd22006-06-09 11:08:56 +02001485
1486 return ini_idx;
Jens Axboeebac4652005-12-08 15:25:21 +01001487}
1488
1489static void free_shm(void)
1490{
1491 struct shmid_ds sbuf;
1492
1493 if (threads) {
Jens Axboe2c0ecd22006-06-08 13:25:41 +02001494 shmdt((void *) threads);
Jens Axboeebac4652005-12-08 15:25:21 +01001495 threads = NULL;
1496 shmctl(shm_id, IPC_RMID, &sbuf);
1497 }
1498}
1499
Jens Axboe906c8d72006-06-13 09:37:56 +02001500/*
1501 * The thread area is shared between the main process and the job
1502 * threads/processes. So setup a shared memory segment that will hold
1503 * all the job info.
1504 */
Jens Axboeebac4652005-12-08 15:25:21 +01001505static int setup_thread_area(void)
1506{
1507 /*
1508 * 1024 is too much on some machines, scale max_jobs if
1509 * we get a failure that looks like too large a shm segment
1510 */
1511 do {
Jens Axboe906c8d72006-06-13 09:37:56 +02001512 size_t size = max_jobs * sizeof(struct thread_data);
Jens Axboeebac4652005-12-08 15:25:21 +01001513
Jens Axboe906c8d72006-06-13 09:37:56 +02001514 shm_id = shmget(0, size, IPC_CREAT | 0600);
Jens Axboeebac4652005-12-08 15:25:21 +01001515 if (shm_id != -1)
1516 break;
1517 if (errno != EINVAL) {
1518 perror("shmget");
1519 break;
1520 }
1521
1522 max_jobs >>= 1;
1523 } while (max_jobs);
1524
1525 if (shm_id == -1)
1526 return 1;
1527
1528 threads = shmat(shm_id, NULL, 0);
1529 if (threads == (void *) -1) {
1530 perror("shmat");
1531 return 1;
1532 }
1533
1534 atexit(free_shm);
1535 return 0;
1536}
1537
Jens Axboeb4692822006-10-27 13:43:22 +02001538/*
1539 * Copy the fio options into the long options map, so we mirror
1540 * job and cmd line options.
1541 */
1542static void dupe_job_options(void)
1543{
1544 struct fio_option *o;
1545 unsigned int i;
1546
1547 i = 0;
1548 while (long_options[i].name)
1549 i++;
1550
1551 o = &options[0];
1552 while (o->name) {
1553 long_options[i].name = o->name;
1554 long_options[i].val = FIO_GETOPT_JOB;
1555 if (o->type == FIO_OPT_STR_SET)
1556 long_options[i].has_arg = no_argument;
1557 else
1558 long_options[i].has_arg = required_argument;
1559
1560 i++;
1561 o++;
1562 assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS);
1563 }
1564}
1565
Jens Axboeebac4652005-12-08 15:25:21 +01001566int parse_options(int argc, char *argv[])
1567{
Jens Axboe972cfd22006-06-09 11:08:56 +02001568 int job_files, i;
1569
Jens Axboeb4692822006-10-27 13:43:22 +02001570 f_out = stdout;
1571 f_err = stderr;
1572
Jens Axboe13335dd2007-01-10 13:14:02 +01001573 options_init(options);
1574
Jens Axboeb4692822006-10-27 13:43:22 +02001575 dupe_job_options();
1576
Jens Axboeebac4652005-12-08 15:25:21 +01001577 if (setup_thread_area())
1578 return 1;
1579 if (fill_def_thread())
1580 return 1;
1581
Jens Axboe972cfd22006-06-09 11:08:56 +02001582 job_files = parse_cmd_line(argc, argv);
Jens Axboeebac4652005-12-08 15:25:21 +01001583
Jens Axboe972cfd22006-06-09 11:08:56 +02001584 for (i = 0; i < job_files; i++) {
1585 if (fill_def_thread())
1586 return 1;
Jens Axboe0c7e37a2006-06-13 14:53:38 +02001587 if (parse_jobs_ini(ini_file[i], i))
Jens Axboe972cfd22006-06-09 11:08:56 +02001588 return 1;
Jens Axboe88c6ed82006-06-09 11:28:10 +02001589 free(ini_file[i]);
Jens Axboe972cfd22006-06-09 11:08:56 +02001590 }
Jens Axboeebac4652005-12-08 15:25:21 +01001591
Jens Axboe88c6ed82006-06-09 11:28:10 +02001592 free(ini_file);
Jens Axboeb4692822006-10-27 13:43:22 +02001593
1594 if (!thread_number) {
1595 log_err("No jobs defined(s)\n");
Jens Axboeb4692822006-10-27 13:43:22 +02001596 return 1;
1597 }
1598
Jens Axboeebac4652005-12-08 15:25:21 +01001599 return 0;
1600}