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