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", | |
dad915e3 | 106 | .type = FIO_OPT_INT, |
e1f36503 JA |
107 | .off1 = td_var_offset(write_iolog), |
108 | }, | |
109 | { | |
110 | .name = "iolog", | |
111 | .type = FIO_OPT_STR_STORE, | |
112 | .off1 = td_var_offset(iolog), | |
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 JA |
407 | |
408 | if (td->iolog && !td->write_iolog) | |
409 | td->read_iolog = 1; | |
e1f36503 JA |
410 | } |
411 | ||
906c8d75 JA |
412 | /* |
413 | * Adds a job to the list of things todo. Sanitizes the various options | |
414 | * to make sure we don't have conflicts, and initializes various | |
415 | * members of td. | |
416 | */ | |
75154845 | 417 | static int add_job(struct thread_data *td, const char *jobname, int job_add_num) |
ebac4655 | 418 | { |
3d60d1ed JA |
419 | char *ddir_str[] = { "read", "write", "randread", "randwrite", |
420 | "rw", NULL, "randrw" }; | |
ebac4655 | 421 | struct stat sb; |
53cdc686 JA |
422 | int numjobs, ddir, i; |
423 | struct fio_file *f; | |
ebac4655 JA |
424 | |
425 | #ifndef FIO_HAVE_LIBAIO | |
426 | if (td->io_engine == FIO_LIBAIO) { | |
3b70d7e5 | 427 | log_err("Linux libaio not available\n"); |
ebac4655 JA |
428 | return 1; |
429 | } | |
430 | #endif | |
431 | #ifndef FIO_HAVE_POSIXAIO | |
432 | if (td->io_engine == FIO_POSIXAIO) { | |
3b70d7e5 | 433 | log_err("posix aio not available\n"); |
ebac4655 JA |
434 | return 1; |
435 | } | |
436 | #endif | |
ebac4655 | 437 | |
e1f36503 JA |
438 | fixup_options(td); |
439 | ||
ebac4655 JA |
440 | /* |
441 | * the def_thread is just for options, it's not a real job | |
442 | */ | |
443 | if (td == &def_thread) | |
444 | return 0; | |
445 | ||
df64119d JA |
446 | /* |
447 | * Set default io engine, if none set | |
448 | */ | |
449 | if (!td->io_ops) { | |
450 | td->io_ops = load_ioengine(td, DEF_IO_ENGINE_NAME); | |
451 | if (!td->io_ops) { | |
452 | log_err("default engine %s not there?\n", DEF_IO_ENGINE_NAME); | |
453 | return 1; | |
454 | } | |
455 | } | |
456 | ||
2866c82d | 457 | if (td->io_ops->flags & FIO_SYNCIO) |
ebac4655 JA |
458 | td->iodepth = 1; |
459 | else { | |
460 | if (!td->iodepth) | |
841ddd13 | 461 | td->iodepth = td->nr_files; |
ebac4655 JA |
462 | } |
463 | ||
20dc95c4 | 464 | /* |
53cdc686 | 465 | * only really works for sequential io for now, and with 1 file |
20dc95c4 | 466 | */ |
53cdc686 | 467 | if (td->zone_size && !td->sequential && td->nr_files == 1) |
20dc95c4 JA |
468 | td->zone_size = 0; |
469 | ||
9cc935a1 JA |
470 | /* |
471 | * Reads can do overwrites, we always need to pre-create the file | |
472 | */ | |
473 | if (td_read(td) || td_rw(td)) | |
474 | td->overwrite = 1; | |
475 | ||
ebac4655 | 476 | td->filetype = FIO_TYPE_FILE; |
0af7b542 JA |
477 | if (!stat(jobname, &sb)) { |
478 | if (S_ISBLK(sb.st_mode)) | |
479 | td->filetype = FIO_TYPE_BD; | |
480 | else if (S_ISCHR(sb.st_mode)) | |
481 | td->filetype = FIO_TYPE_CHAR; | |
482 | } | |
ebac4655 | 483 | |
b2a15192 JA |
484 | if (td->odirect) |
485 | td->io_ops->flags |= FIO_RAWIO; | |
486 | ||
13f8e2d2 JA |
487 | if (td->filename) |
488 | td->nr_uniq_files = 1; | |
489 | else | |
490 | td->nr_uniq_files = td->nr_files; | |
491 | ||
492 | if (td->filetype == FIO_TYPE_FILE || td->filename) { | |
e9c047a0 | 493 | char tmp[PATH_MAX]; |
53cdc686 JA |
494 | int len = 0; |
495 | int i; | |
e9c047a0 | 496 | |
ef899b63 | 497 | if (td->directory && td->directory[0] != '\0') |
53cdc686 | 498 | sprintf(tmp, "%s/", td->directory); |
ebac4655 | 499 | |
53cdc686 JA |
500 | td->files = malloc(sizeof(struct fio_file) * td->nr_files); |
501 | ||
502 | for_each_file(td, f, i) { | |
503 | memset(f, 0, sizeof(*f)); | |
504 | f->fd = -1; | |
505 | ||
13f8e2d2 JA |
506 | if (td->filename) |
507 | sprintf(tmp + len, "%s", td->filename); | |
508 | else | |
509 | sprintf(tmp + len, "%s.%d.%d", jobname, td->thread_number, i); | |
53cdc686 JA |
510 | f->file_name = strdup(tmp); |
511 | } | |
512 | } else { | |
513 | td->nr_files = 1; | |
514 | td->files = malloc(sizeof(struct fio_file)); | |
515 | f = &td->files[0]; | |
516 | ||
517 | memset(f, 0, sizeof(*f)); | |
518 | f->fd = -1; | |
519 | f->file_name = strdup(jobname); | |
520 | } | |
521 | ||
522 | for_each_file(td, f, i) { | |
523 | f->file_size = td->total_file_size / td->nr_files; | |
524 | f->file_offset = td->start_offset; | |
525 | } | |
526 | ||
bbfd6b00 | 527 | fio_sem_init(&td->mutex, 0); |
ebac4655 JA |
528 | |
529 | td->clat_stat[0].min_val = td->clat_stat[1].min_val = ULONG_MAX; | |
530 | td->slat_stat[0].min_val = td->slat_stat[1].min_val = ULONG_MAX; | |
531 | td->bw_stat[0].min_val = td->bw_stat[1].min_val = ULONG_MAX; | |
532 | ||
533 | if (td->min_bs == -1U) | |
534 | td->min_bs = td->bs; | |
535 | if (td->max_bs == -1U) | |
536 | td->max_bs = td->bs; | |
3d60d1ed | 537 | if (td_read(td) && !td_rw(td)) |
ebac4655 JA |
538 | td->verify = 0; |
539 | ||
540 | if (td->stonewall && td->thread_number > 1) | |
541 | groupid++; | |
542 | ||
543 | td->groupid = groupid; | |
544 | ||
545 | if (setup_rate(td)) | |
546 | goto err; | |
547 | ||
ec94ec56 | 548 | if (td->write_lat_log) { |
ebac4655 JA |
549 | setup_log(&td->slat_log); |
550 | setup_log(&td->clat_log); | |
551 | } | |
ec94ec56 | 552 | if (td->write_bw_log) |
ebac4655 JA |
553 | setup_log(&td->bw_log); |
554 | ||
01452055 JA |
555 | if (td->name[0] == '\0') |
556 | snprintf(td->name, sizeof(td->name)-1, "client%d", td->thread_number); | |
557 | ||
3d60d1ed | 558 | ddir = td->ddir + (!td->sequential << 1) + (td->iomix << 2); |
75154845 | 559 | |
c6ae0a5b | 560 | if (!terse_output) { |
b990b5c0 | 561 | if (!job_add_num) { |
2866c82d | 562 | if (td->io_ops->flags & FIO_CPUIO) |
b990b5c0 JA |
563 | fprintf(f_out, "%s: ioengine=cpu, cpuload=%u, cpucycle=%u\n", td->name, td->cpuload, td->cpucycle); |
564 | else | |
2866c82d | 565 | 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 | 566 | } else if (job_add_num == 1) |
c6ae0a5b JA |
567 | fprintf(f_out, "...\n"); |
568 | } | |
ebac4655 JA |
569 | |
570 | /* | |
571 | * recurse add identical jobs, clear numjobs and stonewall options | |
572 | * as they don't apply to sub-jobs | |
573 | */ | |
574 | numjobs = td->numjobs; | |
575 | while (--numjobs) { | |
576 | struct thread_data *td_new = get_new_job(0, td); | |
577 | ||
578 | if (!td_new) | |
579 | goto err; | |
580 | ||
581 | td_new->numjobs = 1; | |
582 | td_new->stonewall = 0; | |
75154845 | 583 | job_add_num = numjobs - 1; |
ebac4655 | 584 | |
75154845 | 585 | if (add_job(td_new, jobname, job_add_num)) |
ebac4655 JA |
586 | goto err; |
587 | } | |
588 | return 0; | |
589 | err: | |
590 | put_job(td); | |
591 | return -1; | |
592 | } | |
593 | ||
906c8d75 JA |
594 | /* |
595 | * Initialize the various random states we need (random io, block size ranges, | |
596 | * read/write mix, etc). | |
597 | */ | |
ebac4655 JA |
598 | int init_random_state(struct thread_data *td) |
599 | { | |
a6ccc7be | 600 | unsigned long seeds[4]; |
53cdc686 | 601 | int fd, num_maps, blocks, i; |
0ab8db89 | 602 | struct fio_file *f; |
ebac4655 | 603 | |
1ac267bb | 604 | fd = open("/dev/urandom", O_RDONLY); |
ebac4655 JA |
605 | if (fd == -1) { |
606 | td_verror(td, errno); | |
607 | return 1; | |
608 | } | |
609 | ||
a6ccc7be | 610 | if (read(fd, seeds, sizeof(seeds)) < (int) sizeof(seeds)) { |
ebac4655 JA |
611 | td_verror(td, EIO); |
612 | close(fd); | |
613 | return 1; | |
614 | } | |
615 | ||
616 | close(fd); | |
617 | ||
6dfd46b9 JA |
618 | os_random_seed(seeds[0], &td->bsrange_state); |
619 | os_random_seed(seeds[1], &td->verify_state); | |
620 | os_random_seed(seeds[2], &td->rwmix_state); | |
ebac4655 JA |
621 | |
622 | if (td->sequential) | |
623 | return 0; | |
624 | ||
9ebc27e1 | 625 | if (td->rand_repeatable) |
a6ccc7be | 626 | seeds[3] = DEF_RANDSEED; |
ebac4655 | 627 | |
0ab8db89 | 628 | for_each_file(td, f, i) { |
53cdc686 JA |
629 | blocks = (f->file_size + td->min_bs - 1) / td->min_bs; |
630 | num_maps = blocks / BLOCKS_PER_MAP; | |
631 | f->file_map = malloc(num_maps * sizeof(long)); | |
632 | f->num_maps = num_maps; | |
633 | memset(f->file_map, 0, num_maps * sizeof(long)); | |
634 | } | |
ebac4655 | 635 | |
6dfd46b9 | 636 | os_random_seed(seeds[3], &td->random_state); |
ebac4655 JA |
637 | return 0; |
638 | } | |
639 | ||
640 | static void fill_cpu_mask(os_cpu_mask_t cpumask, int cpu) | |
641 | { | |
642 | #ifdef FIO_HAVE_CPU_AFFINITY | |
643 | unsigned int i; | |
644 | ||
645 | CPU_ZERO(&cpumask); | |
646 | ||
647 | for (i = 0; i < sizeof(int) * 8; i++) { | |
648 | if ((1 << i) & cpu) | |
649 | CPU_SET(i, &cpumask); | |
650 | } | |
651 | #endif | |
652 | } | |
653 | ||
ebac4655 JA |
654 | static int is_empty_or_comment(char *line) |
655 | { | |
656 | unsigned int i; | |
657 | ||
658 | for (i = 0; i < strlen(line); i++) { | |
659 | if (line[i] == ';') | |
660 | return 1; | |
661 | if (!isspace(line[i]) && !iscntrl(line[i])) | |
662 | return 0; | |
663 | } | |
664 | ||
665 | return 1; | |
666 | } | |
667 | ||
cb2c86fd | 668 | static int str_rw_cb(void *data, char *mem) |
ebac4655 | 669 | { |
cb2c86fd JA |
670 | struct thread_data *td = data; |
671 | ||
ebac4655 JA |
672 | if (!strncmp(mem, "read", 4) || !strncmp(mem, "0", 1)) { |
673 | td->ddir = DDIR_READ; | |
674 | td->sequential = 1; | |
675 | return 0; | |
676 | } else if (!strncmp(mem, "randread", 8)) { | |
677 | td->ddir = DDIR_READ; | |
678 | td->sequential = 0; | |
679 | return 0; | |
680 | } else if (!strncmp(mem, "write", 5) || !strncmp(mem, "1", 1)) { | |
681 | td->ddir = DDIR_WRITE; | |
682 | td->sequential = 1; | |
683 | return 0; | |
684 | } else if (!strncmp(mem, "randwrite", 9)) { | |
685 | td->ddir = DDIR_WRITE; | |
686 | td->sequential = 0; | |
687 | return 0; | |
3d60d1ed JA |
688 | } else if (!strncmp(mem, "rw", 2)) { |
689 | td->ddir = 0; | |
690 | td->iomix = 1; | |
691 | td->sequential = 1; | |
692 | return 0; | |
693 | } else if (!strncmp(mem, "randrw", 6)) { | |
694 | td->ddir = 0; | |
695 | td->iomix = 1; | |
696 | td->sequential = 0; | |
697 | return 0; | |
ebac4655 JA |
698 | } |
699 | ||
3b70d7e5 | 700 | log_err("fio: data direction: read, write, randread, randwrite, rw, randrw\n"); |
ebac4655 JA |
701 | return 1; |
702 | } | |
703 | ||
cb2c86fd | 704 | static int str_verify_cb(void *data, char *mem) |
ebac4655 | 705 | { |
cb2c86fd JA |
706 | struct thread_data *td = data; |
707 | ||
ebac4655 JA |
708 | if (!strncmp(mem, "0", 1)) { |
709 | td->verify = VERIFY_NONE; | |
710 | return 0; | |
711 | } else if (!strncmp(mem, "md5", 3) || !strncmp(mem, "1", 1)) { | |
712 | td->verify = VERIFY_MD5; | |
713 | return 0; | |
714 | } else if (!strncmp(mem, "crc32", 5)) { | |
715 | td->verify = VERIFY_CRC32; | |
716 | return 0; | |
717 | } | |
718 | ||
3b70d7e5 | 719 | log_err("fio: verify types: md5, crc32\n"); |
ebac4655 JA |
720 | return 1; |
721 | } | |
722 | ||
cb2c86fd | 723 | static int str_mem_cb(void *data, char *mem) |
ebac4655 | 724 | { |
cb2c86fd JA |
725 | struct thread_data *td = data; |
726 | ||
ebac4655 JA |
727 | if (!strncmp(mem, "malloc", 6)) { |
728 | td->mem_type = MEM_MALLOC; | |
729 | return 0; | |
730 | } else if (!strncmp(mem, "shm", 3)) { | |
731 | td->mem_type = MEM_SHM; | |
732 | return 0; | |
733 | } else if (!strncmp(mem, "mmap", 4)) { | |
734 | td->mem_type = MEM_MMAP; | |
735 | return 0; | |
736 | } | |
737 | ||
3b70d7e5 | 738 | log_err("fio: mem type: malloc, shm, mmap\n"); |
ebac4655 JA |
739 | return 1; |
740 | } | |
741 | ||
cb2c86fd | 742 | static int str_ioengine_cb(void *data, char *str) |
ebac4655 | 743 | { |
cb2c86fd JA |
744 | struct thread_data *td = data; |
745 | ||
2866c82d JA |
746 | td->io_ops = load_ioengine(td, str); |
747 | if (td->io_ops) | |
b990b5c0 | 748 | return 0; |
ebac4655 | 749 | |
b990b5c0 | 750 | log_err("fio: ioengine: { linuxaio, aio, libaio }, posixaio, sync, mmap, sgio, splice, cpu\n"); |
ebac4655 JA |
751 | return 1; |
752 | } | |
753 | ||
e1f36503 JA |
754 | static int str_lockmem_cb(void fio_unused *data, unsigned long *val) |
755 | { | |
756 | mlock_size = *val; | |
757 | return 0; | |
758 | } | |
759 | ||
760 | static int str_prioclass_cb(void *data, unsigned int *val) | |
761 | { | |
762 | struct thread_data *td = data; | |
763 | ||
764 | td->ioprio |= *val << IOPRIO_CLASS_SHIFT; | |
765 | return 0; | |
766 | } | |
767 | ||
768 | static int str_prio_cb(void *data, unsigned int *val) | |
769 | { | |
770 | struct thread_data *td = data; | |
771 | ||
772 | td->ioprio |= *val; | |
773 | return 0; | |
774 | } | |
775 | ||
776 | static int str_exitall_cb(void) | |
777 | { | |
778 | exitall_on_terminate = 1; | |
779 | return 0; | |
780 | } | |
781 | ||
782 | static int str_cpumask_cb(void *data, unsigned int *val) | |
783 | { | |
784 | struct thread_data *td = data; | |
785 | ||
786 | fill_cpu_mask(td->cpumask, *val); | |
787 | return 0; | |
788 | } | |
789 | ||
07261983 JA |
790 | /* |
791 | * This is our [ini] type file parser. | |
792 | */ | |
0c7e37a0 | 793 | int parse_jobs_ini(char *file, int stonewall_flag) |
ebac4655 | 794 | { |
e1f36503 | 795 | unsigned int global; |
ebac4655 | 796 | struct thread_data *td; |
ef899b63 | 797 | char *string, *name, *tmpbuf; |
ebac4655 JA |
798 | fpos_t off; |
799 | FILE *f; | |
800 | char *p; | |
0c7e37a0 | 801 | int ret = 0, stonewall; |
ebac4655 JA |
802 | |
803 | f = fopen(file, "r"); | |
804 | if (!f) { | |
aea47d44 | 805 | perror("fopen job file"); |
ebac4655 JA |
806 | return 1; |
807 | } | |
808 | ||
809 | string = malloc(4096); | |
810 | name = malloc(256); | |
ef899b63 | 811 | tmpbuf = malloc(4096); |
ebac4655 | 812 | |
0c7e37a0 | 813 | stonewall = stonewall_flag; |
ebac4655 | 814 | while ((p = fgets(string, 4096, f)) != NULL) { |
45410acb JA |
815 | if (ret) |
816 | break; | |
ebac4655 JA |
817 | if (is_empty_or_comment(p)) |
818 | continue; | |
819 | if (sscanf(p, "[%s]", name) != 1) | |
820 | continue; | |
821 | ||
822 | global = !strncmp(name, "global", 6); | |
823 | ||
824 | name[strlen(name) - 1] = '\0'; | |
825 | ||
826 | td = get_new_job(global, &def_thread); | |
45410acb JA |
827 | if (!td) { |
828 | ret = 1; | |
829 | break; | |
830 | } | |
ebac4655 | 831 | |
972cfd25 JA |
832 | /* |
833 | * Seperate multiple job files by a stonewall | |
834 | */ | |
f9481919 | 835 | if (!global && stonewall) { |
972cfd25 JA |
836 | td->stonewall = stonewall; |
837 | stonewall = 0; | |
838 | } | |
839 | ||
ebac4655 JA |
840 | fgetpos(f, &off); |
841 | while ((p = fgets(string, 4096, f)) != NULL) { | |
842 | if (is_empty_or_comment(p)) | |
843 | continue; | |
844 | if (strstr(p, "[")) | |
845 | break; | |
e1f36503 | 846 | |
b6754f9d | 847 | strip_blank_front(&p); |
4ae3f763 | 848 | strip_blank_end(p); |
aea47d44 | 849 | |
e1f36503 | 850 | fgetpos(f, &off); |
ebac4655 | 851 | |
45410acb JA |
852 | /* |
853 | * Don't break here, continue parsing options so we | |
854 | * dump all the bad ones. Makes trial/error fixups | |
855 | * easier on the user. | |
856 | */ | |
e1f36503 | 857 | ret = parse_option(p, options, td); |
ebac4655 | 858 | } |
ebac4655 | 859 | |
45410acb JA |
860 | if (!ret) { |
861 | fsetpos(f, &off); | |
862 | ret = add_job(td, name, 0); | |
863 | } | |
864 | if (ret) | |
865 | break; | |
ebac4655 JA |
866 | } |
867 | ||
868 | free(string); | |
869 | free(name); | |
ef899b63 | 870 | free(tmpbuf); |
ebac4655 | 871 | fclose(f); |
45410acb | 872 | return ret; |
ebac4655 JA |
873 | } |
874 | ||
875 | static int fill_def_thread(void) | |
876 | { | |
877 | memset(&def_thread, 0, sizeof(def_thread)); | |
878 | ||
879 | if (fio_getaffinity(getpid(), &def_thread.cpumask) == -1) { | |
880 | perror("sched_getaffinity"); | |
881 | return 1; | |
882 | } | |
883 | ||
884 | /* | |
885 | * fill globals | |
886 | */ | |
887 | def_thread.ddir = DDIR_READ; | |
3d60d1ed | 888 | def_thread.iomix = 0; |
9ebc27e1 | 889 | def_thread.bs = DEF_BS; |
ebac4655 JA |
890 | def_thread.min_bs = -1; |
891 | def_thread.max_bs = -1; | |
9ebc27e1 | 892 | def_thread.odirect = DEF_ODIRECT; |
ebac4655 | 893 | def_thread.ratecycle = DEF_RATE_CYCLE; |
9ebc27e1 | 894 | def_thread.sequential = DEF_SEQUENTIAL; |
972cfd25 | 895 | def_thread.timeout = def_timeout; |
ebac4655 JA |
896 | def_thread.overwrite = DEF_OVERWRITE; |
897 | def_thread.invalidate_cache = DEF_INVALIDATE; | |
898 | def_thread.sync_io = DEF_SYNCIO; | |
899 | def_thread.mem_type = MEM_MALLOC; | |
900 | def_thread.bw_avg_time = DEF_BWAVGTIME; | |
901 | def_thread.create_serialize = DEF_CREATE_SER; | |
902 | def_thread.create_fsync = DEF_CREATE_FSYNC; | |
903 | def_thread.loops = DEF_LOOPS; | |
904 | def_thread.verify = DEF_VERIFY; | |
905 | def_thread.stonewall = DEF_STONEWALL; | |
906 | def_thread.numjobs = DEF_NUMJOBS; | |
907 | def_thread.use_thread = DEF_USE_THREAD; | |
a6ccc7be JA |
908 | def_thread.rwmixcycle = DEF_RWMIX_CYCLE; |
909 | def_thread.rwmixread = DEF_RWMIX_READ; | |
b6f4d880 | 910 | def_thread.nice = DEF_NICE; |
9ebc27e1 | 911 | def_thread.rand_repeatable = DEF_RAND_REPEAT; |
53cdc686 | 912 | def_thread.nr_files = DEF_NR_FILES; |
f6cbb269 | 913 | def_thread.unlink = DEF_UNLINK; |
ec94ec56 JA |
914 | def_thread.write_bw_log = write_bw_log; |
915 | def_thread.write_lat_log = write_lat_log; | |
ebac4655 JA |
916 | #ifdef FIO_HAVE_DISK_UTIL |
917 | def_thread.do_disk_util = 1; | |
918 | #endif | |
919 | ||
920 | return 0; | |
921 | } | |
922 | ||
0ab8db89 | 923 | static void usage(void) |
4785f995 JA |
924 | { |
925 | printf("%s\n", fio_version_string); | |
47623c72 | 926 | printf("\t-o Write output to file\n"); |
4785f995 | 927 | printf("\t-t Runtime in seconds\n"); |
4785f995 JA |
928 | printf("\t-l Generate per-job latency logs\n"); |
929 | printf("\t-w Generate per-job bandwidth logs\n"); | |
c6ae0a5b | 930 | printf("\t-m Minimal (terse) output\n"); |
4785f995 JA |
931 | printf("\t-v Print version info and exit\n"); |
932 | } | |
933 | ||
972cfd25 | 934 | static int parse_cmd_line(int argc, char *argv[]) |
ebac4655 | 935 | { |
972cfd25 | 936 | int c, idx = 1, ini_idx = 0; |
ebac4655 | 937 | |
2e77841b | 938 | while ((c = getopt(argc, argv, "t:o:lwvhm")) != EOF) { |
ebac4655 | 939 | switch (c) { |
ebac4655 | 940 | case 't': |
972cfd25 | 941 | def_timeout = atoi(optarg); |
774a6177 | 942 | idx = optind; |
ebac4655 | 943 | break; |
ebac4655 JA |
944 | case 'l': |
945 | write_lat_log = 1; | |
774a6177 | 946 | idx = optind; |
ebac4655 JA |
947 | break; |
948 | case 'w': | |
949 | write_bw_log = 1; | |
774a6177 | 950 | idx = optind; |
ebac4655 | 951 | break; |
9ebc27e1 | 952 | case 'o': |
eb8bbf48 JA |
953 | f_out = fopen(optarg, "w+"); |
954 | if (!f_out) { | |
955 | perror("fopen output"); | |
956 | exit(1); | |
957 | } | |
958 | f_err = f_out; | |
774a6177 | 959 | idx = optind; |
eb8bbf48 | 960 | break; |
c6ae0a5b JA |
961 | case 'm': |
962 | terse_output = 1; | |
774a6177 | 963 | idx = optind; |
c6ae0a5b | 964 | break; |
4785f995 | 965 | case 'h': |
0ab8db89 | 966 | usage(); |
4785f995 | 967 | exit(0); |
ebac4655 JA |
968 | case 'v': |
969 | printf("%s\n", fio_version_string); | |
970 | exit(0); | |
971 | } | |
972 | } | |
c9fad893 | 973 | |
972cfd25 JA |
974 | while (idx < argc) { |
975 | ini_idx++; | |
976 | ini_file = realloc(ini_file, ini_idx * sizeof(char *)); | |
977 | ini_file[ini_idx - 1] = strdup(argv[idx]); | |
978 | idx++; | |
979 | } | |
774a6177 | 980 | |
eb8bbf48 JA |
981 | if (!f_out) { |
982 | f_out = stdout; | |
983 | f_err = stderr; | |
984 | } | |
972cfd25 JA |
985 | |
986 | return ini_idx; | |
ebac4655 JA |
987 | } |
988 | ||
989 | static void free_shm(void) | |
990 | { | |
991 | struct shmid_ds sbuf; | |
992 | ||
993 | if (threads) { | |
2c0ecd28 | 994 | shmdt((void *) threads); |
ebac4655 JA |
995 | threads = NULL; |
996 | shmctl(shm_id, IPC_RMID, &sbuf); | |
997 | } | |
998 | } | |
999 | ||
906c8d75 JA |
1000 | /* |
1001 | * The thread area is shared between the main process and the job | |
1002 | * threads/processes. So setup a shared memory segment that will hold | |
1003 | * all the job info. | |
1004 | */ | |
ebac4655 JA |
1005 | static int setup_thread_area(void) |
1006 | { | |
1007 | /* | |
1008 | * 1024 is too much on some machines, scale max_jobs if | |
1009 | * we get a failure that looks like too large a shm segment | |
1010 | */ | |
1011 | do { | |
906c8d75 | 1012 | size_t size = max_jobs * sizeof(struct thread_data); |
ebac4655 | 1013 | |
906c8d75 | 1014 | shm_id = shmget(0, size, IPC_CREAT | 0600); |
ebac4655 JA |
1015 | if (shm_id != -1) |
1016 | break; | |
1017 | if (errno != EINVAL) { | |
1018 | perror("shmget"); | |
1019 | break; | |
1020 | } | |
1021 | ||
1022 | max_jobs >>= 1; | |
1023 | } while (max_jobs); | |
1024 | ||
1025 | if (shm_id == -1) | |
1026 | return 1; | |
1027 | ||
1028 | threads = shmat(shm_id, NULL, 0); | |
1029 | if (threads == (void *) -1) { | |
1030 | perror("shmat"); | |
1031 | return 1; | |
1032 | } | |
1033 | ||
1034 | atexit(free_shm); | |
1035 | return 0; | |
1036 | } | |
1037 | ||
1038 | int parse_options(int argc, char *argv[]) | |
1039 | { | |
972cfd25 JA |
1040 | int job_files, i; |
1041 | ||
ebac4655 JA |
1042 | if (setup_thread_area()) |
1043 | return 1; | |
1044 | if (fill_def_thread()) | |
1045 | return 1; | |
1046 | ||
972cfd25 JA |
1047 | job_files = parse_cmd_line(argc, argv); |
1048 | if (!job_files) { | |
1049 | log_err("Need job file(s)\n"); | |
0ab8db89 | 1050 | usage(); |
ebac4655 JA |
1051 | return 1; |
1052 | } | |
1053 | ||
972cfd25 JA |
1054 | for (i = 0; i < job_files; i++) { |
1055 | if (fill_def_thread()) | |
1056 | return 1; | |
0c7e37a0 | 1057 | if (parse_jobs_ini(ini_file[i], i)) |
972cfd25 | 1058 | return 1; |
88c6ed80 | 1059 | free(ini_file[i]); |
972cfd25 | 1060 | } |
ebac4655 | 1061 | |
88c6ed80 | 1062 | free(ini_file); |
ebac4655 JA |
1063 | return 0; |
1064 | } |