Commit | Line | Data |
---|---|---|
906c8d75 | 1 | /* |
cb2c86fd | 2 | * This file contains job initialization and setup functions. |
906c8d75 | 3 | */ |
ebac4655 JA |
4 | #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> | |
b4692828 JA |
11 | #include <getopt.h> |
12 | #include <assert.h> | |
ebac4655 JA |
13 | #include <sys/ipc.h> |
14 | #include <sys/shm.h> | |
15 | #include <sys/types.h> | |
16 | #include <sys/stat.h> | |
17 | ||
18 | #include "fio.h" | |
cb2c86fd | 19 | #include "parse.h" |
ebac4655 | 20 | |
ee738499 | 21 | #define FIO_RANDSEED (0xb1899bedUL) |
ebac4655 | 22 | |
e1f36503 JA |
23 | #define td_var_offset(var) ((size_t) &((struct thread_data *)0)->var) |
24 | ||
b4692828 | 25 | static int str_mem_cb(void *, const char *); |
e1f36503 | 26 | static int str_lockmem_cb(void *, unsigned long *); |
34cfcdaf | 27 | #ifdef FIO_HAVE_IOPRIO |
e1f36503 JA |
28 | static int str_prio_cb(void *, unsigned int *); |
29 | static int str_prioclass_cb(void *, unsigned int *); | |
34cfcdaf | 30 | #endif |
e1f36503 JA |
31 | static int str_exitall_cb(void); |
32 | static int str_cpumask_cb(void *, unsigned int *); | |
33 | ||
ee738499 JA |
34 | #define __stringify_1(x) #x |
35 | #define __stringify(x) __stringify_1(x) | |
36 | ||
e1f36503 JA |
37 | /* |
38 | * Map of job/command line options | |
39 | */ | |
40 | static struct fio_option options[] = { | |
61697c37 JA |
41 | { |
42 | .name = "description", | |
43 | .type = FIO_OPT_STR_STORE, | |
44 | .off1 = td_var_offset(description), | |
45 | .help = "Text job description", | |
46 | }, | |
e1f36503 JA |
47 | { |
48 | .name = "name", | |
49 | .type = FIO_OPT_STR_STORE, | |
50 | .off1 = td_var_offset(name), | |
fd28ca49 | 51 | .help = "Name of this job", |
e1f36503 JA |
52 | }, |
53 | { | |
54 | .name = "directory", | |
55 | .type = FIO_OPT_STR_STORE, | |
56 | .off1 = td_var_offset(directory), | |
fd28ca49 | 57 | .help = "Directory to store files in", |
e1f36503 JA |
58 | }, |
59 | { | |
60 | .name = "filename", | |
61 | .type = FIO_OPT_STR_STORE, | |
62 | .off1 = td_var_offset(filename), | |
fd28ca49 | 63 | .help = "Force the use of a specific file", |
e1f36503 JA |
64 | }, |
65 | { | |
66 | .name = "rw", | |
67 | .type = FIO_OPT_STR, | |
b1ec1da6 | 68 | .off1 = td_var_offset(td_ddir), |
15f7918f | 69 | .help = "IO direction", |
ee738499 | 70 | .def = "read", |
b1ec1da6 JA |
71 | .posval = { |
72 | { .ival = "read", .oval = TD_DDIR_READ }, | |
73 | { .ival = "write", .oval = TD_DDIR_WRITE }, | |
74 | { .ival = "randread", .oval = TD_DDIR_RANDREAD }, | |
75 | { .ival = "randwrite", .oval = TD_DDIR_RANDWRITE }, | |
76 | { .ival = "rw", .oval = TD_DDIR_RW }, | |
77 | { .ival = "randrw", .oval = TD_DDIR_RANDRW }, | |
78 | }, | |
e1f36503 JA |
79 | }, |
80 | { | |
81 | .name = "ioengine", | |
09629a90 JA |
82 | .type = FIO_OPT_STR_STORE, |
83 | .off1 = td_var_offset(ioengine), | |
15f7918f | 84 | .help = "IO engine to use", |
ee738499 | 85 | .def = "sync", |
b1ec1da6 | 86 | .posval = { |
10cad1aa JA |
87 | { .ival = "sync", }, |
88 | #ifdef FIO_HAVE_LIBAIO | |
89 | { .ival = "libaio", }, | |
90 | #endif | |
91 | #ifdef FIO_HAVE_POSIXAIO | |
92 | { .ival = "posixaio", }, | |
93 | #endif | |
94 | { .ival = "mmap", }, | |
95 | #ifdef FIO_HAVE_SPLICE | |
96 | { .ival = "splice", }, | |
97 | #endif | |
98 | #ifdef FIO_HAVE_SGIO | |
99 | { .ival = "sg", }, | |
100 | #endif | |
b1ec1da6 | 101 | { .ival = "null", }, { .ival = "net", }, |
10cad1aa | 102 | #ifdef FIO_HAVE_SYSLET |
b1ec1da6 | 103 | { .ival = "syslet-rw", }, |
10cad1aa | 104 | #endif |
7b395ca5 | 105 | { .ival = "external", }, |
b1ec1da6 | 106 | }, |
e1f36503 | 107 | }, |
03b74b3e JA |
108 | { |
109 | .name = "iodepth", | |
110 | .type = FIO_OPT_INT, | |
111 | .off1 = td_var_offset(iodepth), | |
112 | .help = "Amount of IO buffers to keep in flight", | |
113 | .def = "1", | |
114 | }, | |
cb5ab512 JA |
115 | { |
116 | .name = "iodepth_batch", | |
117 | .type = FIO_OPT_INT, | |
118 | .off1 = td_var_offset(iodepth_batch), | |
119 | .help = "Number of IO to submit in one go", | |
120 | }, | |
e916b390 JA |
121 | { |
122 | .name = "iodepth_low", | |
123 | .type = FIO_OPT_INT, | |
124 | .off1 = td_var_offset(iodepth_low), | |
125 | .help = "Low water mark for queuing depth", | |
126 | }, | |
03b74b3e JA |
127 | { |
128 | .name = "size", | |
129 | .type = FIO_OPT_STR_VAL, | |
130 | .off1 = td_var_offset(total_file_size), | |
131 | .help = "Size of device or file", | |
132 | }, | |
133 | { | |
134 | .name = "bs", | |
135 | .type = FIO_OPT_STR_VAL_INT, | |
136 | .off1 = td_var_offset(bs[DDIR_READ]), | |
137 | .off2 = td_var_offset(bs[DDIR_WRITE]), | |
138 | .help = "Block size unit", | |
139 | .def = "4k", | |
140 | }, | |
141 | { | |
142 | .name = "bsrange", | |
143 | .type = FIO_OPT_RANGE, | |
144 | .off1 = td_var_offset(min_bs[DDIR_READ]), | |
145 | .off2 = td_var_offset(max_bs[DDIR_READ]), | |
146 | .off3 = td_var_offset(min_bs[DDIR_WRITE]), | |
147 | .off4 = td_var_offset(max_bs[DDIR_WRITE]), | |
148 | .help = "Set block size range (in more detail than bs)", | |
149 | }, | |
150 | { | |
151 | .name = "bs_unaligned", | |
152 | .type = FIO_OPT_STR_SET, | |
153 | .off1 = td_var_offset(bs_unaligned), | |
154 | .help = "Don't sector align IO buffer sizes", | |
155 | }, | |
156 | { | |
157 | .name = "offset", | |
158 | .type = FIO_OPT_STR_VAL, | |
159 | .off1 = td_var_offset(start_offset), | |
160 | .help = "Start IO from this offset", | |
161 | .def = "0", | |
162 | }, | |
163 | { | |
164 | .name = "randrepeat", | |
165 | .type = FIO_OPT_BOOL, | |
166 | .off1 = td_var_offset(rand_repeatable), | |
167 | .help = "Use repeatable random IO pattern", | |
168 | .def = "1", | |
169 | }, | |
170 | { | |
171 | .name = "norandommap", | |
172 | .type = FIO_OPT_STR_SET, | |
173 | .off1 = td_var_offset(norandommap), | |
174 | .help = "Accept potential duplicate random blocks", | |
175 | }, | |
176 | { | |
177 | .name = "nrfiles", | |
178 | .type = FIO_OPT_INT, | |
179 | .off1 = td_var_offset(nr_files), | |
180 | .help = "Split job workload between this number of files", | |
181 | .def = "1", | |
182 | }, | |
b5af8293 JA |
183 | { |
184 | .name = "openfiles", | |
185 | .type = FIO_OPT_INT, | |
186 | .off1 = td_var_offset(open_files), | |
187 | .help = "Number of files to keep open at the same time", | |
188 | }, | |
0aabe160 JA |
189 | { |
190 | .name = "file_service_type", | |
191 | .type = FIO_OPT_STR, | |
b1ec1da6 | 192 | .off1 = td_var_offset(file_service_type), |
0aabe160 JA |
193 | .help = "How to select which file to service next", |
194 | .def = "roundrobin", | |
b1ec1da6 JA |
195 | .posval = { |
196 | { .ival = "random", .oval = FIO_FSERVICE_RANDOM }, | |
197 | { .ival = "roundrobin", .oval = FIO_FSERVICE_RR }, | |
198 | }, | |
0aabe160 | 199 | }, |
03b74b3e JA |
200 | { |
201 | .name = "fsync", | |
202 | .type = FIO_OPT_INT, | |
203 | .off1 = td_var_offset(fsync_blocks), | |
204 | .help = "Issue fsync for writes every given number of blocks", | |
205 | .def = "0", | |
206 | }, | |
207 | { | |
208 | .name = "direct", | |
209 | .type = FIO_OPT_BOOL, | |
210 | .off1 = td_var_offset(odirect), | |
76a43db4 JA |
211 | .help = "Use O_DIRECT IO (negates buffered)", |
212 | .def = "0", | |
213 | }, | |
214 | { | |
215 | .name = "buffered", | |
216 | .type = FIO_OPT_BOOL, | |
217 | .off1 = td_var_offset(odirect), | |
218 | .neg = 1, | |
219 | .help = "Use buffered IO (negates direct)", | |
03b74b3e JA |
220 | .def = "1", |
221 | }, | |
222 | { | |
223 | .name = "overwrite", | |
224 | .type = FIO_OPT_BOOL, | |
225 | .off1 = td_var_offset(overwrite), | |
226 | .help = "When writing, set whether to overwrite current data", | |
227 | .def = "0", | |
228 | }, | |
229 | { | |
230 | .name = "loops", | |
231 | .type = FIO_OPT_INT, | |
232 | .off1 = td_var_offset(loops), | |
233 | .help = "Number of times to run the job", | |
234 | .def = "1", | |
235 | }, | |
236 | { | |
237 | .name = "numjobs", | |
238 | .type = FIO_OPT_INT, | |
239 | .off1 = td_var_offset(numjobs), | |
240 | .help = "Duplicate this job this many times", | |
241 | .def = "1", | |
242 | }, | |
243 | { | |
244 | .name = "startdelay", | |
245 | .type = FIO_OPT_INT, | |
246 | .off1 = td_var_offset(start_delay), | |
247 | .help = "Only start job when this period has passed", | |
248 | .def = "0", | |
249 | }, | |
250 | { | |
251 | .name = "runtime", | |
252 | .alias = "timeout", | |
253 | .type = FIO_OPT_STR_VAL_TIME, | |
254 | .off1 = td_var_offset(timeout), | |
255 | .help = "Stop workload when this amount of time has passed", | |
256 | .def = "0", | |
257 | }, | |
e1f36503 JA |
258 | { |
259 | .name = "mem", | |
260 | .type = FIO_OPT_STR, | |
261 | .cb = str_mem_cb, | |
b1ec1da6 | 262 | .off1 = td_var_offset(mem_type), |
15f7918f | 263 | .help = "Backing type for IO buffers", |
ee738499 | 264 | .def = "malloc", |
b1ec1da6 JA |
265 | .posval = { |
266 | { .ival = "malloc", .oval = MEM_MALLOC }, | |
267 | { .ival = "shm", .oval = MEM_SHM }, | |
268 | #ifdef FIO_HAVE_HUGETLB | |
269 | { .ival = "shmhuge", .oval = MEM_SHMHUGE }, | |
270 | #endif | |
271 | { .ival = "mmap", .oval = MEM_MMAP }, | |
272 | #ifdef FIO_HAVE_HUGETLB | |
273 | { .ival = "mmaphuge", .oval = MEM_MMAPHUGE }, | |
274 | #endif | |
275 | }, | |
e1f36503 JA |
276 | }, |
277 | { | |
278 | .name = "verify", | |
279 | .type = FIO_OPT_STR, | |
b1ec1da6 | 280 | .off1 = td_var_offset(verify), |
15f7918f | 281 | .help = "Verify sum function", |
ee738499 | 282 | .def = "0", |
b1ec1da6 JA |
283 | .posval = { |
284 | { .ival = "0", .oval = VERIFY_NONE }, | |
285 | { .ival = "crc32", .oval = VERIFY_CRC32 }, | |
286 | { .ival = "md5", .oval = VERIFY_MD5 }, | |
287 | }, | |
e1f36503 JA |
288 | }, |
289 | { | |
290 | .name = "write_iolog", | |
076efc7c JA |
291 | .type = FIO_OPT_STR_STORE, |
292 | .off1 = td_var_offset(write_iolog_file), | |
fd28ca49 | 293 | .help = "Store IO pattern to file", |
e1f36503 JA |
294 | }, |
295 | { | |
076efc7c | 296 | .name = "read_iolog", |
e1f36503 | 297 | .type = FIO_OPT_STR_STORE, |
076efc7c | 298 | .off1 = td_var_offset(read_iolog_file), |
fd28ca49 | 299 | .help = "Playback IO pattern from file", |
e1f36503 JA |
300 | }, |
301 | { | |
302 | .name = "exec_prerun", | |
303 | .type = FIO_OPT_STR_STORE, | |
304 | .off1 = td_var_offset(exec_prerun), | |
fd28ca49 | 305 | .help = "Execute this file prior to running job", |
e1f36503 JA |
306 | }, |
307 | { | |
308 | .name = "exec_postrun", | |
309 | .type = FIO_OPT_STR_STORE, | |
310 | .off1 = td_var_offset(exec_postrun), | |
fd28ca49 | 311 | .help = "Execute this file after running job", |
e1f36503 JA |
312 | }, |
313 | #ifdef FIO_HAVE_IOSCHED_SWITCH | |
314 | { | |
315 | .name = "ioscheduler", | |
316 | .type = FIO_OPT_STR_STORE, | |
317 | .off1 = td_var_offset(ioscheduler), | |
fd28ca49 | 318 | .help = "Use this IO scheduler on the backing device", |
e1f36503 JA |
319 | }, |
320 | #endif | |
e1f36503 JA |
321 | { |
322 | .name = "zonesize", | |
323 | .type = FIO_OPT_STR_VAL, | |
324 | .off1 = td_var_offset(zone_size), | |
fd28ca49 | 325 | .help = "Give size of an IO zone", |
ee738499 | 326 | .def = "0", |
e1f36503 JA |
327 | }, |
328 | { | |
329 | .name = "zoneskip", | |
330 | .type = FIO_OPT_STR_VAL, | |
331 | .off1 = td_var_offset(zone_skip), | |
fd28ca49 | 332 | .help = "Space between IO zones", |
ee738499 | 333 | .def = "0", |
e1f36503 JA |
334 | }, |
335 | { | |
336 | .name = "lockmem", | |
337 | .type = FIO_OPT_STR_VAL, | |
338 | .cb = str_lockmem_cb, | |
fd28ca49 | 339 | .help = "Lock down this amount of memory", |
ee738499 | 340 | .def = "0", |
e1f36503 | 341 | }, |
e1f36503 JA |
342 | { |
343 | .name = "rwmixcycle", | |
344 | .type = FIO_OPT_INT, | |
345 | .off1 = td_var_offset(rwmixcycle), | |
ee738499 JA |
346 | .help = "Cycle period for mixed read/write workloads (msec)", |
347 | .def = "500", | |
e1f36503 JA |
348 | }, |
349 | { | |
350 | .name = "rwmixread", | |
351 | .type = FIO_OPT_INT, | |
352 | .off1 = td_var_offset(rwmixread), | |
ee738499 | 353 | .maxval = 100, |
fd28ca49 | 354 | .help = "Percentage of mixed workload that is reads", |
ee738499 | 355 | .def = "50", |
e1f36503 JA |
356 | }, |
357 | { | |
358 | .name = "rwmixwrite", | |
359 | .type = FIO_OPT_INT, | |
360 | .off1 = td_var_offset(rwmixwrite), | |
ee738499 | 361 | .maxval = 100, |
fd28ca49 | 362 | .help = "Percentage of mixed workload that is writes", |
ee738499 | 363 | .def = "50", |
e1f36503 JA |
364 | }, |
365 | { | |
366 | .name = "nice", | |
367 | .type = FIO_OPT_INT, | |
368 | .off1 = td_var_offset(nice), | |
fd28ca49 | 369 | .help = "Set job CPU nice value", |
15f7918f | 370 | .minval = -19, |
ee738499 JA |
371 | .maxval = 20, |
372 | .def = "0", | |
e1f36503 JA |
373 | }, |
374 | #ifdef FIO_HAVE_IOPRIO | |
375 | { | |
376 | .name = "prio", | |
377 | .type = FIO_OPT_INT, | |
378 | .cb = str_prio_cb, | |
fd28ca49 | 379 | .help = "Set job IO priority value", |
15f7918f JA |
380 | .minval = 0, |
381 | .maxval = 7, | |
e1f36503 JA |
382 | }, |
383 | { | |
384 | .name = "prioclass", | |
385 | .type = FIO_OPT_INT, | |
386 | .cb = str_prioclass_cb, | |
fd28ca49 | 387 | .help = "Set job IO priority class", |
15f7918f JA |
388 | .minval = 0, |
389 | .maxval = 3, | |
e1f36503 JA |
390 | }, |
391 | #endif | |
392 | { | |
393 | .name = "thinktime", | |
394 | .type = FIO_OPT_INT, | |
1304923a | 395 | .off1 = td_var_offset(thinktime), |
5fa0f817 | 396 | .help = "Idle time between IO buffers (usec)", |
ee738499 | 397 | .def = "0", |
e1f36503 | 398 | }, |
48097d5c JA |
399 | { |
400 | .name = "thinktime_spin", | |
401 | .type = FIO_OPT_INT, | |
402 | .off1 = td_var_offset(thinktime_spin), | |
34403fb1 | 403 | .help = "Start think time by spinning this amount (usec)", |
48097d5c JA |
404 | .def = "0", |
405 | }, | |
9c1f7434 JA |
406 | { |
407 | .name = "thinktime_blocks", | |
408 | .type = FIO_OPT_INT, | |
1304923a | 409 | .off1 = td_var_offset(thinktime_blocks), |
fd28ca49 | 410 | .help = "IO buffer period between 'thinktime'", |
ee738499 | 411 | .def = "1", |
9c1f7434 | 412 | }, |
e1f36503 JA |
413 | { |
414 | .name = "rate", | |
415 | .type = FIO_OPT_INT, | |
1304923a | 416 | .off1 = td_var_offset(rate), |
fd28ca49 | 417 | .help = "Set bandwidth rate", |
e1f36503 JA |
418 | }, |
419 | { | |
420 | .name = "ratemin", | |
421 | .type = FIO_OPT_INT, | |
1304923a | 422 | .off1 = td_var_offset(ratemin), |
fd28ca49 | 423 | .help = "The bottom limit accepted", |
e1f36503 JA |
424 | }, |
425 | { | |
426 | .name = "ratecycle", | |
427 | .type = FIO_OPT_INT, | |
1304923a | 428 | .off1 = td_var_offset(ratecycle), |
6da1fa7f | 429 | .help = "Window average for rate limits (msec)", |
ee738499 | 430 | .def = "1000", |
e1f36503 | 431 | }, |
e1f36503 JA |
432 | { |
433 | .name = "invalidate", | |
13335ddb | 434 | .type = FIO_OPT_BOOL, |
1304923a | 435 | .off1 = td_var_offset(invalidate_cache), |
fd28ca49 | 436 | .help = "Invalidate buffer/page cache prior to running job", |
ee738499 | 437 | .def = "1", |
e1f36503 JA |
438 | }, |
439 | { | |
440 | .name = "sync", | |
13335ddb | 441 | .type = FIO_OPT_BOOL, |
1304923a | 442 | .off1 = td_var_offset(sync_io), |
fd28ca49 | 443 | .help = "Use O_SYNC for buffered writes", |
ee738499 | 444 | .def = "0", |
e1f36503 JA |
445 | }, |
446 | { | |
447 | .name = "bwavgtime", | |
448 | .type = FIO_OPT_INT, | |
1304923a | 449 | .off1 = td_var_offset(bw_avg_time), |
ee738499 JA |
450 | .help = "Time window over which to calculate bandwidth (msec)", |
451 | .def = "500", | |
e1f36503 JA |
452 | }, |
453 | { | |
454 | .name = "create_serialize", | |
13335ddb | 455 | .type = FIO_OPT_BOOL, |
1304923a | 456 | .off1 = td_var_offset(create_serialize), |
fd28ca49 | 457 | .help = "Serialize creating of job files", |
ee738499 | 458 | .def = "1", |
e1f36503 JA |
459 | }, |
460 | { | |
461 | .name = "create_fsync", | |
13335ddb | 462 | .type = FIO_OPT_BOOL, |
1304923a | 463 | .off1 = td_var_offset(create_fsync), |
fd28ca49 | 464 | .help = "Fsync file after creation", |
ee738499 | 465 | .def = "1", |
e1f36503 | 466 | }, |
e1f36503 JA |
467 | { |
468 | .name = "cpuload", | |
469 | .type = FIO_OPT_INT, | |
1304923a | 470 | .off1 = td_var_offset(cpuload), |
fd28ca49 | 471 | .help = "Use this percentage of CPU", |
e1f36503 JA |
472 | }, |
473 | { | |
474 | .name = "cpuchunks", | |
475 | .type = FIO_OPT_INT, | |
1304923a | 476 | .off1 = td_var_offset(cpucycle), |
fd28ca49 | 477 | .help = "Length of the CPU burn cycles", |
e1f36503 | 478 | }, |
e1f36503 JA |
479 | #ifdef FIO_HAVE_CPU_AFFINITY |
480 | { | |
481 | .name = "cpumask", | |
482 | .type = FIO_OPT_INT, | |
483 | .cb = str_cpumask_cb, | |
fd28ca49 | 484 | .help = "CPU affinity mask", |
e1f36503 JA |
485 | }, |
486 | #endif | |
487 | { | |
488 | .name = "end_fsync", | |
13335ddb | 489 | .type = FIO_OPT_BOOL, |
1304923a | 490 | .off1 = td_var_offset(end_fsync), |
fd28ca49 | 491 | .help = "Include fsync at the end of job", |
ee738499 | 492 | .def = "0", |
e1f36503 JA |
493 | }, |
494 | { | |
495 | .name = "unlink", | |
13335ddb | 496 | .type = FIO_OPT_BOOL, |
e1f36503 | 497 | .off1 = td_var_offset(unlink), |
ee738499 | 498 | .help = "Unlink created files after job has completed", |
e545a6ce | 499 | .def = "0", |
e1f36503 JA |
500 | }, |
501 | { | |
502 | .name = "exitall", | |
503 | .type = FIO_OPT_STR_SET, | |
504 | .cb = str_exitall_cb, | |
fd28ca49 | 505 | .help = "Terminate all jobs when one exits", |
e1f36503 JA |
506 | }, |
507 | { | |
508 | .name = "stonewall", | |
509 | .type = FIO_OPT_STR_SET, | |
510 | .off1 = td_var_offset(stonewall), | |
fd28ca49 | 511 | .help = "Insert a hard barrier between this job and previous", |
e1f36503 JA |
512 | }, |
513 | { | |
514 | .name = "thread", | |
515 | .type = FIO_OPT_STR_SET, | |
d9bb3b80 | 516 | .off1 = td_var_offset(use_thread), |
fd28ca49 | 517 | .help = "Use threads instead of forks", |
e1f36503 JA |
518 | }, |
519 | { | |
520 | .name = "write_bw_log", | |
521 | .type = FIO_OPT_STR_SET, | |
522 | .off1 = td_var_offset(write_bw_log), | |
fd28ca49 | 523 | .help = "Write log of bandwidth during run", |
e1f36503 JA |
524 | }, |
525 | { | |
526 | .name = "write_lat_log", | |
527 | .type = FIO_OPT_STR_SET, | |
528 | .off1 = td_var_offset(write_lat_log), | |
fd28ca49 | 529 | .help = "Write log of latency during run", |
e1f36503 | 530 | }, |
56bb17f2 JA |
531 | { |
532 | .name = "hugepage-size", | |
533 | .type = FIO_OPT_STR_VAL, | |
534 | .off1 = td_var_offset(hugepage_size), | |
fd28ca49 | 535 | .help = "When using hugepages, specify size of each page", |
ee738499 | 536 | .def = __stringify(FIO_HUGE_PAGE), |
56bb17f2 | 537 | }, |
b2560f3c JA |
538 | { |
539 | .name = "group_reporting", | |
540 | .type = FIO_OPT_STR_SET, | |
541 | .off1 = td_var_offset(group_reporting), | |
542 | .help = "Do reporting on a per-group basis", | |
543 | }, | |
e1f36503 JA |
544 | { |
545 | .name = NULL, | |
546 | }, | |
547 | }; | |
548 | ||
b4692828 JA |
549 | #define FIO_JOB_OPTS (sizeof(options) / sizeof(struct fio_option)) |
550 | #define FIO_CMD_OPTS (16) | |
551 | #define FIO_GETOPT_JOB (0x89988998) | |
552 | ||
553 | /* | |
554 | * Command line options. These will contain the above, plus a few | |
555 | * extra that only pertain to fio itself and not jobs. | |
556 | */ | |
557 | static struct option long_options[FIO_JOB_OPTS + FIO_CMD_OPTS] = { | |
558 | { | |
559 | .name = "output", | |
560 | .has_arg = required_argument, | |
561 | .val = 'o', | |
562 | }, | |
563 | { | |
564 | .name = "timeout", | |
565 | .has_arg = required_argument, | |
566 | .val = 't', | |
567 | }, | |
568 | { | |
569 | .name = "latency-log", | |
570 | .has_arg = required_argument, | |
571 | .val = 'l', | |
572 | }, | |
573 | { | |
574 | .name = "bandwidth-log", | |
575 | .has_arg = required_argument, | |
576 | .val = 'b', | |
577 | }, | |
578 | { | |
579 | .name = "minimal", | |
580 | .has_arg = optional_argument, | |
581 | .val = 'm', | |
582 | }, | |
583 | { | |
584 | .name = "version", | |
585 | .has_arg = no_argument, | |
586 | .val = 'v', | |
587 | }, | |
fd28ca49 JA |
588 | { |
589 | .name = "help", | |
590 | .has_arg = no_argument, | |
591 | .val = 'h', | |
592 | }, | |
593 | { | |
594 | .name = "cmdhelp", | |
320beefe | 595 | .has_arg = optional_argument, |
fd28ca49 JA |
596 | .val = 'c', |
597 | }, | |
b4692828 JA |
598 | { |
599 | .name = NULL, | |
600 | }, | |
601 | }; | |
602 | ||
ee738499 | 603 | static int def_timeout = 0; |
972cfd25 | 604 | |
fdf3de27 | 605 | static char fio_version_string[] = "fio 1.13"; |
ebac4655 | 606 | |
972cfd25 | 607 | static char **ini_file; |
ebac4655 JA |
608 | static int max_jobs = MAX_JOBS; |
609 | ||
610 | struct thread_data def_thread; | |
611 | struct thread_data *threads = NULL; | |
612 | ||
ebac4655 | 613 | int exitall_on_terminate = 0; |
c6ae0a5b | 614 | int terse_output = 0; |
c04f7ec3 | 615 | unsigned long long mlock_size = 0; |
eb8bbf48 JA |
616 | FILE *f_out = NULL; |
617 | FILE *f_err = NULL; | |
ebac4655 | 618 | |
ee738499 | 619 | static int write_lat_log = 0; |
bb3884d8 | 620 | int write_bw_log = 0; |
ec94ec56 | 621 | |
9728ce37 JB |
622 | FILE *get_f_out() |
623 | { | |
624 | return f_out; | |
625 | } | |
626 | ||
627 | FILE *get_f_err() | |
628 | { | |
629 | return f_err; | |
630 | } | |
631 | ||
906c8d75 JA |
632 | /* |
633 | * Return a free job structure. | |
634 | */ | |
ebac4655 JA |
635 | static struct thread_data *get_new_job(int global, struct thread_data *parent) |
636 | { | |
637 | struct thread_data *td; | |
638 | ||
639 | if (global) | |
640 | return &def_thread; | |
641 | if (thread_number >= max_jobs) | |
642 | return NULL; | |
643 | ||
644 | td = &threads[thread_number++]; | |
ddaeaa5a | 645 | *td = *parent; |
ebac4655 | 646 | |
ebac4655 | 647 | td->thread_number = thread_number; |
ebac4655 JA |
648 | return td; |
649 | } | |
650 | ||
651 | static void put_job(struct thread_data *td) | |
652 | { | |
549577a7 JA |
653 | if (td == &def_thread) |
654 | return; | |
655 | ||
16edf25d JA |
656 | if (td->error) |
657 | fprintf(f_out, "fio: %s\n", td->verror); | |
658 | ||
ebac4655 JA |
659 | memset(&threads[td->thread_number - 1], 0, sizeof(*td)); |
660 | thread_number--; | |
661 | } | |
662 | ||
dad915e3 JA |
663 | /* |
664 | * Lazy way of fixing up options that depend on each other. We could also | |
665 | * define option callback handlers, but this is easier. | |
666 | */ | |
e1f36503 JA |
667 | static void fixup_options(struct thread_data *td) |
668 | { | |
e1f36503 JA |
669 | if (!td->rwmixread && td->rwmixwrite) |
670 | td->rwmixread = 100 - td->rwmixwrite; | |
dad915e3 | 671 | |
076efc7c JA |
672 | if (td->write_iolog_file && td->read_iolog_file) { |
673 | log_err("fio: read iolog overrides write_iolog\n"); | |
674 | free(td->write_iolog_file); | |
675 | td->write_iolog_file = NULL; | |
676 | } | |
16b462ae JA |
677 | |
678 | if (td->io_ops->flags & FIO_SYNCIO) | |
679 | td->iodepth = 1; | |
680 | else { | |
681 | if (!td->iodepth) | |
b5af8293 | 682 | td->iodepth = td->open_files; |
16b462ae JA |
683 | } |
684 | ||
685 | /* | |
686 | * only really works for sequential io for now, and with 1 file | |
687 | */ | |
b5af8293 | 688 | if (td->zone_size && td_random(td) && td->open_files == 1) |
16b462ae JA |
689 | td->zone_size = 0; |
690 | ||
691 | /* | |
692 | * Reads can do overwrites, we always need to pre-create the file | |
693 | */ | |
694 | if (td_read(td) || td_rw(td)) | |
695 | td->overwrite = 1; | |
696 | ||
a00735e6 JA |
697 | if (!td->min_bs[DDIR_READ]) |
698 | td->min_bs[DDIR_READ]= td->bs[DDIR_READ]; | |
699 | if (!td->max_bs[DDIR_READ]) | |
700 | td->max_bs[DDIR_READ] = td->bs[DDIR_READ]; | |
701 | if (!td->min_bs[DDIR_WRITE]) | |
75e6f36f | 702 | td->min_bs[DDIR_WRITE]= td->bs[DDIR_WRITE]; |
a00735e6 | 703 | if (!td->max_bs[DDIR_WRITE]) |
75e6f36f | 704 | td->max_bs[DDIR_WRITE] = td->bs[DDIR_WRITE]; |
a00735e6 JA |
705 | |
706 | td->rw_min_bs = min(td->min_bs[DDIR_READ], td->min_bs[DDIR_WRITE]); | |
707 | ||
16b462ae JA |
708 | if (td_read(td) && !td_rw(td)) |
709 | td->verify = 0; | |
bb8895e0 JA |
710 | |
711 | if (td->norandommap && td->verify != VERIFY_NONE) { | |
712 | log_err("fio: norandommap given, verify disabled\n"); | |
713 | td->verify = VERIFY_NONE; | |
714 | } | |
690adba3 JA |
715 | if (td->bs_unaligned && (td->odirect || td->io_ops->flags & FIO_RAWIO)) |
716 | log_err("fio: bs_unaligned may not work with raw io\n"); | |
e0a22335 JA |
717 | |
718 | /* | |
719 | * O_DIRECT and char doesn't mix, clear that flag if necessary. | |
720 | */ | |
721 | if (td->filetype == FIO_TYPE_CHAR && td->odirect) | |
722 | td->odirect = 0; | |
48097d5c JA |
723 | |
724 | /* | |
725 | * thinktime_spin must be less than thinktime | |
726 | */ | |
727 | if (td->thinktime_spin > td->thinktime) | |
728 | td->thinktime_spin = td->thinktime; | |
e916b390 JA |
729 | |
730 | /* | |
731 | * The low water mark cannot be bigger than the iodepth | |
732 | */ | |
9467b77c JA |
733 | if (td->iodepth_low > td->iodepth || !td->iodepth_low) { |
734 | /* | |
735 | * syslet work around - if the workload is sequential, | |
736 | * we want to let the queue drain all the way down to | |
737 | * avoid seeking between async threads | |
738 | */ | |
739 | if (!strcmp(td->io_ops->name, "syslet-rw") && !td_random(td)) | |
740 | td->iodepth_low = 1; | |
741 | else | |
742 | td->iodepth_low = td->iodepth; | |
743 | } | |
cb5ab512 JA |
744 | |
745 | /* | |
746 | * If batch number isn't set, default to the same as iodepth | |
747 | */ | |
748 | if (td->iodepth_batch > td->iodepth || !td->iodepth_batch) | |
749 | td->iodepth_batch = td->iodepth; | |
b5af8293 JA |
750 | |
751 | if (td->open_files > td->nr_files || !td->open_files) | |
752 | td->open_files = td->nr_files; | |
e1f36503 JA |
753 | } |
754 | ||
f8977ee6 JA |
755 | /* |
756 | * This function leaks the buffer | |
757 | */ | |
758 | static char *to_kmg(unsigned int val) | |
759 | { | |
760 | char *buf = malloc(32); | |
f3502ba2 | 761 | char post[] = { 0, 'K', 'M', 'G', 'P', 'E', 0 }; |
f8977ee6 JA |
762 | char *p = post; |
763 | ||
245142ff | 764 | do { |
f8977ee6 JA |
765 | if (val & 1023) |
766 | break; | |
767 | ||
768 | val >>= 10; | |
769 | p++; | |
245142ff | 770 | } while (*p); |
f8977ee6 JA |
771 | |
772 | snprintf(buf, 31, "%u%c", val, *p); | |
773 | return buf; | |
774 | } | |
775 | ||
09629a90 JA |
776 | /* External engines are specified by "external:name.o") */ |
777 | static const char *get_engine_name(const char *str) | |
778 | { | |
779 | char *p = strstr(str, ":"); | |
780 | ||
781 | if (!p) | |
782 | return str; | |
783 | ||
784 | p++; | |
785 | strip_blank_front(&p); | |
786 | strip_blank_end(p); | |
787 | return p; | |
788 | } | |
789 | ||
906c8d75 JA |
790 | /* |
791 | * Adds a job to the list of things todo. Sanitizes the various options | |
792 | * to make sure we don't have conflicts, and initializes various | |
793 | * members of td. | |
794 | */ | |
75154845 | 795 | static int add_job(struct thread_data *td, const char *jobname, int job_add_num) |
ebac4655 | 796 | { |
413dd459 JA |
797 | const char *ddir_str[] = { NULL, "read", "write", "rw", NULL, |
798 | "randread", "randwrite", "randrw" }; | |
ebac4655 | 799 | struct stat sb; |
413dd459 | 800 | int numjobs, i; |
53cdc686 | 801 | struct fio_file *f; |
09629a90 | 802 | const char *engine; |
ebac4655 | 803 | |
ebac4655 JA |
804 | /* |
805 | * the def_thread is just for options, it's not a real job | |
806 | */ | |
807 | if (td == &def_thread) | |
808 | return 0; | |
809 | ||
09629a90 JA |
810 | engine = get_engine_name(td->ioengine); |
811 | td->io_ops = load_ioengine(td, engine); | |
812 | if (!td->io_ops) { | |
813 | log_err("fio: failed to load engine %s\n", engine); | |
814 | return 1; | |
815 | } | |
df64119d | 816 | |
690adba3 JA |
817 | if (td->odirect) |
818 | td->io_ops->flags |= FIO_RAWIO; | |
819 | ||
ebac4655 | 820 | td->filetype = FIO_TYPE_FILE; |
890df538 | 821 | if (td->filename && !lstat(td->filename, &sb)) { |
0af7b542 JA |
822 | if (S_ISBLK(sb.st_mode)) |
823 | td->filetype = FIO_TYPE_BD; | |
824 | else if (S_ISCHR(sb.st_mode)) | |
825 | td->filetype = FIO_TYPE_CHAR; | |
826 | } | |
ebac4655 | 827 | |
e0a22335 JA |
828 | fixup_options(td); |
829 | ||
13f8e2d2 JA |
830 | if (td->filename) |
831 | td->nr_uniq_files = 1; | |
832 | else | |
0f14fef3 | 833 | td->nr_uniq_files = td->open_files; |
13f8e2d2 JA |
834 | |
835 | if (td->filetype == FIO_TYPE_FILE || td->filename) { | |
e9c047a0 | 836 | char tmp[PATH_MAX]; |
53cdc686 | 837 | int len = 0; |
e9c047a0 | 838 | |
16edf25d JA |
839 | if (td->directory && td->directory[0] != '\0') { |
840 | if (lstat(td->directory, &sb) < 0) { | |
841 | log_err("fio: %s is not a directory\n", td->directory); | |
e1161c32 | 842 | td_verror(td, errno, "lstat"); |
16edf25d JA |
843 | return 1; |
844 | } | |
845 | if (!S_ISDIR(sb.st_mode)) { | |
846 | log_err("fio: %s is not a directory\n", td->directory); | |
847 | return 1; | |
848 | } | |
8aeebd55 | 849 | len = sprintf(tmp, "%s/", td->directory); |
16edf25d | 850 | } |
ebac4655 | 851 | |
b5af8293 | 852 | td->files = malloc(sizeof(struct fio_file) * td->open_files); |
53cdc686 JA |
853 | |
854 | for_each_file(td, f, i) { | |
855 | memset(f, 0, sizeof(*f)); | |
856 | f->fd = -1; | |
857 | ||
13f8e2d2 JA |
858 | if (td->filename) |
859 | sprintf(tmp + len, "%s", td->filename); | |
860 | else | |
861 | sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i); | |
53cdc686 JA |
862 | f->file_name = strdup(tmp); |
863 | } | |
864 | } else { | |
b5af8293 | 865 | td->open_files = td->nr_files = 1; |
53cdc686 JA |
866 | td->files = malloc(sizeof(struct fio_file)); |
867 | f = &td->files[0]; | |
868 | ||
869 | memset(f, 0, sizeof(*f)); | |
870 | f->fd = -1; | |
871 | f->file_name = strdup(jobname); | |
872 | } | |
873 | ||
874 | for_each_file(td, f, i) { | |
875 | f->file_size = td->total_file_size / td->nr_files; | |
876 | f->file_offset = td->start_offset; | |
877 | } | |
878 | ||
07739b57 | 879 | td->mutex = fio_sem_init(0); |
ebac4655 | 880 | |
756867bd JA |
881 | td->ts.clat_stat[0].min_val = td->ts.clat_stat[1].min_val = ULONG_MAX; |
882 | td->ts.slat_stat[0].min_val = td->ts.slat_stat[1].min_val = ULONG_MAX; | |
883 | td->ts.bw_stat[0].min_val = td->ts.bw_stat[1].min_val = ULONG_MAX; | |
ebac4655 | 884 | |
ebac4655 JA |
885 | if (td->stonewall && td->thread_number > 1) |
886 | groupid++; | |
887 | ||
888 | td->groupid = groupid; | |
889 | ||
890 | if (setup_rate(td)) | |
891 | goto err; | |
892 | ||
ec94ec56 | 893 | if (td->write_lat_log) { |
756867bd JA |
894 | setup_log(&td->ts.slat_log); |
895 | setup_log(&td->ts.clat_log); | |
ebac4655 | 896 | } |
ec94ec56 | 897 | if (td->write_bw_log) |
756867bd | 898 | setup_log(&td->ts.bw_log); |
ebac4655 | 899 | |
b4692828 JA |
900 | if (!td->name) |
901 | td->name = strdup(jobname); | |
01452055 | 902 | |
c6ae0a5b | 903 | if (!terse_output) { |
b990b5c0 | 904 | if (!job_add_num) { |
2866c82d | 905 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 | 906 | fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle); |
f8977ee6 JA |
907 | else { |
908 | char *c1, *c2, *c3, *c4; | |
909 | ||
910 | c1 = to_kmg(td->min_bs[DDIR_READ]); | |
911 | c2 = to_kmg(td->max_bs[DDIR_READ]); | |
912 | c3 = to_kmg(td->min_bs[DDIR_WRITE]); | |
913 | c4 = to_kmg(td->max_bs[DDIR_WRITE]); | |
914 | ||
413dd459 | 915 | 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); |
f8977ee6 JA |
916 | |
917 | free(c1); | |
918 | free(c2); | |
919 | free(c3); | |
920 | free(c4); | |
921 | } | |
b990b5c0 | 922 | } else if (job_add_num == 1) |
c6ae0a5b JA |
923 | fprintf(f_out, "...\n"); |
924 | } | |
ebac4655 JA |
925 | |
926 | /* | |
927 | * recurse add identical jobs, clear numjobs and stonewall options | |
928 | * as they don't apply to sub-jobs | |
929 | */ | |
930 | numjobs = td->numjobs; | |
931 | while (--numjobs) { | |
932 | struct thread_data *td_new = get_new_job(0, td); | |
933 | ||
934 | if (!td_new) | |
935 | goto err; | |
936 | ||
937 | td_new->numjobs = 1; | |
938 | td_new->stonewall = 0; | |
75154845 | 939 | job_add_num = numjobs - 1; |
ebac4655 | 940 | |
75154845 | 941 | if (add_job(td_new, jobname, job_add_num)) |
ebac4655 JA |
942 | goto err; |
943 | } | |
944 | return 0; | |
945 | err: | |
946 | put_job(td); | |
947 | return -1; | |
948 | } | |
949 | ||
906c8d75 JA |
950 | /* |
951 | * Initialize the various random states we need (random io, block size ranges, | |
952 | * read/write mix, etc). | |
953 | */ | |
ebac4655 JA |
954 | int init_random_state(struct thread_data *td) |
955 | { | |
0aabe160 | 956 | unsigned long seeds[5]; |
53cdc686 | 957 | int fd, num_maps, blocks, i; |
0ab8db89 | 958 | struct fio_file *f; |
ebac4655 | 959 | |
f48b467c JA |
960 | if (td->io_ops->flags & FIO_CPUIO) |
961 | return 0; | |
962 | ||
1ac267bb | 963 | fd = open("/dev/urandom", O_RDONLY); |
ebac4655 | 964 | if (fd == -1) { |
e1161c32 | 965 | td_verror(td, errno, "open"); |
ebac4655 JA |
966 | return 1; |
967 | } | |
968 | ||
a6ccc7be | 969 | if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) { |
e1161c32 | 970 | td_verror(td, EIO, "read"); |
ebac4655 JA |
971 | close(fd); |
972 | return 1; | |
973 | } | |
974 | ||
975 | close(fd); | |
976 | ||
6dfd46b9 JA |
977 | os_random_seed(seeds[0], &td->bsrange_state); |
978 | os_random_seed(seeds[1], &td->verify_state); | |
979 | os_random_seed(seeds[2], &td->rwmix_state); | |
ebac4655 | 980 | |
413dd459 JA |
981 | if (td->file_service_type == FIO_FSERVICE_RANDOM) |
982 | os_random_seed(seeds[3], &td->next_file_state); | |
983 | ||
984 | if (!td_random(td)) | |
ebac4655 JA |
985 | return 0; |
986 | ||
9ebc27e1 | 987 | if (td->rand_repeatable) |
0aabe160 | 988 | seeds[4] = FIO_RANDSEED * td->thread_number; |
ebac4655 | 989 | |
bb8895e0 JA |
990 | if (!td->norandommap) { |
991 | for_each_file(td, f, i) { | |
a4a81712 | 992 | blocks = (f->real_file_size + td->rw_min_bs - 1) / td->rw_min_bs; |
c7c280ed | 993 | num_maps = (blocks + BLOCKS_PER_MAP-1)/ BLOCKS_PER_MAP; |
bb8895e0 JA |
994 | f->file_map = malloc(num_maps * sizeof(long)); |
995 | f->num_maps = num_maps; | |
996 | memset(f->file_map, 0, num_maps * sizeof(long)); | |
997 | } | |
53cdc686 | 998 | } |
ebac4655 | 999 | |
0aabe160 | 1000 | os_random_seed(seeds[4], &td->random_state); |
ebac4655 JA |
1001 | return 0; |
1002 | } | |
1003 | ||
1004 | static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu) | |
1005 | { | |
1006 | #ifdef FIO_HAVE_CPU_AFFINITY | |
1007 | unsigned int i; | |
1008 | ||
1009 | CPU_ZERO(&cpumask); | |
1010 | ||
1011 | for (i = 0; i < sizeof(int) * 8; i++) { | |
1012 | if ((1 << i) & cpu) | |
1013 | CPU_SET(i, &cpumask); | |
1014 | } | |
1015 | #endif | |
1016 | } | |
1017 | ||
ebac4655 JA |
1018 | static int is_empty_or_comment(char *line) |
1019 | { | |
1020 | unsigned int i; | |
1021 | ||
1022 | for (i = 0; i < strlen(line); i++) { | |
1023 | if (line[i] == ';') | |
1024 | return 1; | |
5cc2da30 IM |
1025 | if (line[i] == '#') |
1026 | return 1; | |
ebac4655 JA |
1027 | if (!isspace(line[i]) && !iscntrl(line[i])) |
1028 | return 0; | |
1029 | } | |
1030 | ||
1031 | return 1; | |
1032 | } | |
1033 | ||
313cb206 JA |
1034 | /* |
1035 | * Check if mmap/mmaphuge has a :/foo/bar/file at the end. If so, return that. | |
1036 | */ | |
1037 | static char *get_mmap_file(const char *str) | |
1038 | { | |
1039 | char *p = strstr(str, ":"); | |
1040 | ||
1041 | if (!p) | |
1042 | return NULL; | |
1043 | ||
1044 | p++; | |
1045 | strip_blank_front(&p); | |
1046 | strip_blank_end(p); | |
1047 | return strdup(p); | |
1048 | } | |
1049 | ||
b4692828 | 1050 | static int str_mem_cb(void *data, const char *mem) |
ebac4655 | 1051 | { |
cb2c86fd JA |
1052 | struct thread_data *td = data; |
1053 | ||
b1ec1da6 | 1054 | if (td->mem_type == MEM_MMAPHUGE || td->mem_type == MEM_MMAP) { |
313cb206 | 1055 | td->mmapfile = get_mmap_file(mem); |
b1ec1da6 | 1056 | if (td->mem_type == MEM_MMAPHUGE && !td->mmapfile) { |
d0bdaf49 JA |
1057 | log_err("fio: mmaphuge:/path/to/file\n"); |
1058 | return 1; | |
1059 | } | |
ebac4655 JA |
1060 | } |
1061 | ||
b1ec1da6 | 1062 | return 0; |
ebac4655 JA |
1063 | } |
1064 | ||
e1f36503 JA |
1065 | static int str_lockmem_cb(void fio_unused *data, unsigned long *val) |
1066 | { | |
1067 | mlock_size = *val; | |
1068 | return 0; | |
1069 | } | |
1070 | ||
34cfcdaf | 1071 | #ifdef FIO_HAVE_IOPRIO |
e1f36503 JA |
1072 | static int str_prioclass_cb(void *data, unsigned int *val) |
1073 | { | |
1074 | struct thread_data *td = data; | |
1075 | ||
1076 | td->ioprio |= *val << IOPRIO_CLASS_SHIFT; | |
1077 | return 0; | |
1078 | } | |
1079 | ||
1080 | static int str_prio_cb(void *data, unsigned int *val) | |
1081 | { | |
1082 | struct thread_data *td = data; | |
1083 | ||
1084 | td->ioprio |= *val; | |
1085 | return 0; | |
1086 | } | |
34cfcdaf | 1087 | #endif |
e1f36503 JA |
1088 | |
1089 | static int str_exitall_cb(void) | |
1090 | { | |
1091 | exitall_on_terminate = 1; | |
1092 | return 0; | |
1093 | } | |
1094 | ||
1095 | static int str_cpumask_cb(void *data, unsigned int *val) | |
1096 | { | |
1097 | struct thread_data *td = data; | |
1098 | ||
1099 | fill_cpu_mask(td->cpumask, *val); | |
1100 | return 0; | |
1101 | } | |
1102 | ||
07261983 JA |
1103 | /* |
1104 | * This is our [ini] type file parser. | |
1105 | */ | |
1e97cce9 | 1106 | static int parse_jobs_ini(char *file, int stonewall_flag) |
ebac4655 | 1107 | { |
e1f36503 | 1108 | unsigned int global; |
ebac4655 | 1109 | struct thread_data *td; |
fee3bb48 | 1110 | char *string, *name; |
ebac4655 JA |
1111 | fpos_t off; |
1112 | FILE *f; | |
1113 | char *p; | |
0c7e37a0 | 1114 | int ret = 0, stonewall; |
ebac4655 JA |
1115 | |
1116 | f = fopen(file, "r"); | |
1117 | if (!f) { | |
aea47d44 | 1118 | perror("fopen job file"); |
ebac4655 JA |
1119 | return 1; |
1120 | } | |
1121 | ||
1122 | string = malloc(4096); | |
1123 | name = malloc(256); | |
fee3bb48 | 1124 | memset(name, 0, 256); |
ebac4655 | 1125 | |
0c7e37a0 | 1126 | stonewall = stonewall_flag; |
7c124ac1 JA |
1127 | do { |
1128 | p = fgets(string, 4095, f); | |
1129 | if (!p) | |
45410acb | 1130 | break; |
ebac4655 JA |
1131 | if (is_empty_or_comment(p)) |
1132 | continue; | |
fee3bb48 | 1133 | if (sscanf(p, "[%255s]", name) != 1) |
ebac4655 JA |
1134 | continue; |
1135 | ||
1136 | global = !strncmp(name, "global", 6); | |
1137 | ||
1138 | name[strlen(name) - 1] = '\0'; | |
1139 | ||
1140 | td = get_new_job(global, &def_thread); | |
45410acb JA |
1141 | if (!td) { |
1142 | ret = 1; | |
1143 | break; | |
1144 | } | |
ebac4655 | 1145 | |
972cfd25 JA |
1146 | /* |
1147 | * Seperate multiple job files by a stonewall | |
1148 | */ | |
f9481919 | 1149 | if (!global && stonewall) { |
972cfd25 JA |
1150 | td->stonewall = stonewall; |
1151 | stonewall = 0; | |
1152 | } | |
1153 | ||
ebac4655 JA |
1154 | fgetpos(f, &off); |
1155 | while ((p = fgets(string, 4096, f)) != NULL) { | |
1156 | if (is_empty_or_comment(p)) | |
1157 | continue; | |
e1f36503 | 1158 | |
b6754f9d | 1159 | strip_blank_front(&p); |
7c124ac1 JA |
1160 | |
1161 | if (p[0] == '[') | |
1162 | break; | |
1163 | ||
4ae3f763 | 1164 | strip_blank_end(p); |
aea47d44 | 1165 | |
e1f36503 | 1166 | fgetpos(f, &off); |
ebac4655 | 1167 | |
45410acb JA |
1168 | /* |
1169 | * Don't break here, continue parsing options so we | |
1170 | * dump all the bad ones. Makes trial/error fixups | |
1171 | * easier on the user. | |
1172 | */ | |
7c124ac1 | 1173 | ret |= parse_option(p, options, td); |
ebac4655 | 1174 | } |
ebac4655 | 1175 | |
45410acb JA |
1176 | if (!ret) { |
1177 | fsetpos(f, &off); | |
1178 | ret = add_job(td, name, 0); | |
b1508cf9 JA |
1179 | } else { |
1180 | log_err("fio: job %s dropped\n", name); | |
1181 | put_job(td); | |
45410acb | 1182 | } |
7c124ac1 | 1183 | } while (!ret); |
ebac4655 JA |
1184 | |
1185 | free(string); | |
1186 | free(name); | |
1187 | fclose(f); | |
45410acb | 1188 | return ret; |
ebac4655 JA |
1189 | } |
1190 | ||
1191 | static int fill_def_thread(void) | |
1192 | { | |
1193 | memset(&def_thread, 0, sizeof(def_thread)); | |
1194 | ||
1195 | if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) { | |
1196 | perror("sched_getaffinity"); | |
1197 | return 1; | |
1198 | } | |
1199 | ||
1200 | /* | |
ee738499 | 1201 | * fill default options |
ebac4655 | 1202 | */ |
ee738499 JA |
1203 | fill_default_options(&def_thread, options); |
1204 | ||
972cfd25 | 1205 | def_thread.timeout = def_timeout; |
ec94ec56 JA |
1206 | def_thread.write_bw_log = write_bw_log; |
1207 | def_thread.write_lat_log = write_lat_log; | |
ee738499 | 1208 | |
ebac4655 JA |
1209 | #ifdef FIO_HAVE_DISK_UTIL |
1210 | def_thread.do_disk_util = 1; | |
1211 | #endif | |
1212 | ||
1213 | return 0; | |
1214 | } | |
1215 | ||
0ab8db89 | 1216 | static void usage(void) |
4785f995 JA |
1217 | { |
1218 | printf("%s\n", fio_version_string); | |
b4692828 JA |
1219 | printf("\t--output\tWrite output to file\n"); |
1220 | printf("\t--timeout\tRuntime in seconds\n"); | |
1221 | printf("\t--latency-log\tGenerate per-job latency logs\n"); | |
1222 | printf("\t--bandwidth-log\tGenerate per-job bandwidth logs\n"); | |
1223 | printf("\t--minimal\tMinimal (terse) output\n"); | |
1224 | printf("\t--version\tPrint version info and exit\n"); | |
fd28ca49 JA |
1225 | printf("\t--help\t\tPrint this page\n"); |
1226 | printf("\t--cmdhelp=cmd\tPrint command help, \"all\" for all of them\n"); | |
4785f995 JA |
1227 | } |
1228 | ||
972cfd25 | 1229 | static int parse_cmd_line(int argc, char *argv[]) |
ebac4655 | 1230 | { |
b4692828 | 1231 | struct thread_data *td = NULL; |
b1ec1da6 | 1232 | int c, ini_idx = 0, lidx, ret, dont_add_job = 0; |
ebac4655 | 1233 | |
0be06ea2 | 1234 | while ((c = getopt_long_only(argc, argv, "", long_options, &lidx)) != -1) { |
ebac4655 | 1235 | switch (c) { |
b4692828 JA |
1236 | case 't': |
1237 | def_timeout = atoi(optarg); | |
1238 | break; | |
1239 | case 'l': | |
1240 | write_lat_log = 1; | |
1241 | break; | |
1242 | case 'w': | |
1243 | write_bw_log = 1; | |
1244 | break; | |
1245 | case 'o': | |
1246 | f_out = fopen(optarg, "w+"); | |
1247 | if (!f_out) { | |
1248 | perror("fopen output"); | |
1249 | exit(1); | |
1250 | } | |
1251 | f_err = f_out; | |
1252 | break; | |
1253 | case 'm': | |
1254 | terse_output = 1; | |
1255 | break; | |
1256 | case 'h': | |
1257 | usage(); | |
1258 | exit(0); | |
fd28ca49 | 1259 | case 'c': |
29fc6afe JA |
1260 | ret = show_cmd_help(options, optarg); |
1261 | exit(ret); | |
b4692828 JA |
1262 | case 'v': |
1263 | printf("%s\n", fio_version_string); | |
1264 | exit(0); | |
1265 | case FIO_GETOPT_JOB: { | |
1266 | const char *opt = long_options[lidx].name; | |
1267 | char *val = optarg; | |
1268 | ||
c2b1e753 JA |
1269 | if (!strncmp(opt, "name", 4) && td) { |
1270 | ret = add_job(td, td->name ?: "fio", 0); | |
1271 | if (ret) { | |
1272 | put_job(td); | |
1273 | return 0; | |
1274 | } | |
1275 | td = NULL; | |
1276 | } | |
b4692828 | 1277 | if (!td) { |
38d0adb0 | 1278 | int global = !strncmp(val, "global", 6); |
c2b1e753 JA |
1279 | |
1280 | td = get_new_job(global, &def_thread); | |
b4692828 JA |
1281 | if (!td) |
1282 | return 0; | |
1283 | } | |
38d0adb0 | 1284 | |
b1508cf9 | 1285 | ret = parse_cmd_option(opt, val, options, td); |
78217dfa | 1286 | if (ret) |
b1ec1da6 | 1287 | dont_add_job = 1; |
b4692828 JA |
1288 | break; |
1289 | } | |
1290 | default: | |
b4692828 | 1291 | break; |
ebac4655 JA |
1292 | } |
1293 | } | |
c9fad893 | 1294 | |
b4692828 | 1295 | if (td) { |
b1ec1da6 | 1296 | if (dont_add_job) |
b4692828 | 1297 | put_job(td); |
b1ec1da6 JA |
1298 | else { |
1299 | ret = add_job(td, td->name ?: "fio", 0); | |
1300 | if (ret) | |
1301 | put_job(td); | |
1302 | } | |
972cfd25 | 1303 | } |
774a6177 | 1304 | |
b4692828 JA |
1305 | while (optind < argc) { |
1306 | ini_idx++; | |
1307 | ini_file = realloc(ini_file, ini_idx * sizeof(char *)); | |
1308 | ini_file[ini_idx - 1] = strdup(argv[optind]); | |
1309 | optind++; | |
eb8bbf48 | 1310 | } |
972cfd25 JA |
1311 | |
1312 | return ini_idx; | |
ebac4655 JA |
1313 | } |
1314 | ||
1315 | static void free_shm(void) | |
1316 | { | |
1317 | struct shmid_ds sbuf; | |
1318 | ||
1319 | if (threads) { | |
2c0ecd28 | 1320 | shmdt((void *) threads); |
ebac4655 JA |
1321 | threads = NULL; |
1322 | shmctl(shm_id, IPC_RMID, &sbuf); | |
1323 | } | |
1324 | } | |
1325 | ||
906c8d75 JA |
1326 | /* |
1327 | * The thread area is shared between the main process and the job | |
1328 | * threads/processes. So setup a shared memory segment that will hold | |
1329 | * all the job info. | |
1330 | */ | |
ebac4655 JA |
1331 | static int setup_thread_area(void) |
1332 | { | |
1333 | /* | |
1334 | * 1024 is too much on some machines, scale max_jobs if | |
1335 | * we get a failure that looks like too large a shm segment | |
1336 | */ | |
1337 | do { | |
906c8d75 | 1338 | size_t size = max_jobs * sizeof(struct thread_data); |
ebac4655 | 1339 | |
906c8d75 | 1340 | shm_id = shmget(0, size, IPC_CREAT | 0600); |
ebac4655 JA |
1341 | if (shm_id != -1) |
1342 | break; | |
1343 | if (errno != EINVAL) { | |
1344 | perror("shmget"); | |
1345 | break; | |
1346 | } | |
1347 | ||
1348 | max_jobs >>= 1; | |
1349 | } while (max_jobs); | |
1350 | ||
1351 | if (shm_id == -1) | |
1352 | return 1; | |
1353 | ||
1354 | threads = shmat(shm_id, NULL, 0); | |
1355 | if (threads == (void *) -1) { | |
1356 | perror("shmat"); | |
1357 | return 1; | |
1358 | } | |
1359 | ||
1360 | atexit(free_shm); | |
1361 | return 0; | |
1362 | } | |
1363 | ||
b4692828 JA |
1364 | /* |
1365 | * Copy the fio options into the long options map, so we mirror | |
1366 | * job and cmd line options. | |
1367 | */ | |
1368 | static void dupe_job_options(void) | |
1369 | { | |
1370 | struct fio_option *o; | |
1371 | unsigned int i; | |
1372 | ||
1373 | i = 0; | |
1374 | while (long_options[i].name) | |
1375 | i++; | |
1376 | ||
1377 | o = &options[0]; | |
1378 | while (o->name) { | |
1379 | long_options[i].name = o->name; | |
1380 | long_options[i].val = FIO_GETOPT_JOB; | |
1381 | if (o->type == FIO_OPT_STR_SET) | |
1382 | long_options[i].has_arg = no_argument; | |
1383 | else | |
1384 | long_options[i].has_arg = required_argument; | |
1385 | ||
1386 | i++; | |
1387 | o++; | |
1388 | assert(i < FIO_JOB_OPTS + FIO_CMD_OPTS); | |
1389 | } | |
1390 | } | |
1391 | ||
ebac4655 JA |
1392 | int parse_options(int argc, char *argv[]) |
1393 | { | |
972cfd25 JA |
1394 | int job_files, i; |
1395 | ||
b4692828 JA |
1396 | f_out = stdout; |
1397 | f_err = stderr; | |
1398 | ||
13335ddb JA |
1399 | options_init(options); |
1400 | ||
b4692828 JA |
1401 | dupe_job_options(); |
1402 | ||
ebac4655 JA |
1403 | if (setup_thread_area()) |
1404 | return 1; | |
1405 | if (fill_def_thread()) | |
1406 | return 1; | |
1407 | ||
972cfd25 | 1408 | job_files = parse_cmd_line(argc, argv); |
ebac4655 | 1409 | |
972cfd25 JA |
1410 | for (i = 0; i < job_files; i++) { |
1411 | if (fill_def_thread()) | |
1412 | return 1; | |
0c7e37a0 | 1413 | if (parse_jobs_ini(ini_file[i], i)) |
972cfd25 | 1414 | return 1; |
88c6ed80 | 1415 | free(ini_file[i]); |
972cfd25 | 1416 | } |
ebac4655 | 1417 | |
88c6ed80 | 1418 | free(ini_file); |
b4692828 JA |
1419 | |
1420 | if (!thread_number) { | |
1421 | log_err("No jobs defined(s)\n"); | |
b4692828 JA |
1422 | return 1; |
1423 | } | |
1424 | ||
ebac4655 JA |
1425 | return 0; |
1426 | } |