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