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