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