Commit | Line | Data |
---|---|---|
abe4da87 JA |
1 | /* |
2 | * fio - the flexible io tester | |
3 | * | |
4 | * Copyright (C) 2005 Jens Axboe <axboe@suse.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
19 | * | |
20 | */ | |
892199bd JA |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <unistd.h> | |
24 | #include <fcntl.h> | |
25 | #include <string.h> | |
26 | #include <errno.h> | |
27 | #include <signal.h> | |
28 | #include <time.h> | |
7dd1389e | 29 | #include <ctype.h> |
18e0b78c | 30 | #include <sched.h> |
43000118 | 31 | #include <libaio.h> |
e128065d | 32 | #include <math.h> |
02983297 | 33 | #include <limits.h> |
49d2caab | 34 | #include <assert.h> |
892199bd JA |
35 | #include <sys/time.h> |
36 | #include <sys/types.h> | |
37 | #include <sys/stat.h> | |
38 | #include <sys/wait.h> | |
39 | #include <semaphore.h> | |
40 | #include <sys/ipc.h> | |
41 | #include <sys/shm.h> | |
42 | #include <asm/unistd.h> | |
946d8870 | 43 | #include <asm/types.h> |
49d2caab | 44 | #include <asm/bitops.h> |
892199bd | 45 | |
2c83567e | 46 | #include "list.h" |
e8457004 | 47 | #include "md5.h" |
2c83567e | 48 | |
4240cfa1 JA |
49 | #define MAX_JOBS (1024) |
50 | ||
892199bd JA |
51 | /* |
52 | * assume we don't have _get either, if _set isn't defined | |
53 | */ | |
54 | #ifndef __NR_ioprio_set | |
892199bd JA |
55 | #if defined(__i386__) |
56 | #define __NR_ioprio_set 289 | |
57 | #define __NR_ioprio_get 290 | |
58 | #elif defined(__powerpc__) || defined(__powerpc64__) | |
59 | #define __NR_ioprio_set 273 | |
60 | #define __NR_ioprio_get 274 | |
61 | #elif defined(__x86_64__) | |
62 | #define __NR_ioprio_set 251 | |
63 | #define __NR_ioprio_get 252 | |
64 | #elif defined(__ia64__) | |
65 | #define __NR_ioprio_set 1274 | |
66 | #define __NR_ioprio_get 1275 | |
67 | #elif defined(__alpha__) | |
68 | #define __NR_ioprio_set 442 | |
69 | #define __NR_ioprio_get 443 | |
70 | #elif defined(__s390x__) || defined(__s390__) | |
71 | #define __NR_ioprio_set 282 | |
72 | #define __NR_ioprio_get 283 | |
73 | #else | |
74 | #error "Unsupported arch" | |
75 | #endif | |
b95799ca | 76 | #endif |
892199bd | 77 | |
b95799ca JA |
78 | #ifndef __NR_fadvise64 |
79 | #if defined(__i386__) | |
80 | #define __NR_fadvise64 250 | |
81 | #elif defined(__powerpc__) || defined(__powerpc64__) | |
82 | #define __NR_fadvise64 233 | |
83 | #elif defined(__x86_64__) | |
84 | #define __NR_fadvise64 221 | |
85 | #elif defined(__ia64__) | |
86 | #define __NR_fadvise64 1234 | |
87 | #elif defined(__alpha__) | |
88 | #define __NR_fadvise64 413 | |
89 | #elif defined(__s390x__) || defined(__s390__) | |
90 | #define __NR_fadvise64 253 | |
91 | #else | |
92 | #error "Unsupported arch" | |
93 | #endif | |
892199bd JA |
94 | #endif |
95 | ||
96 | static int ioprio_set(int which, int who, int ioprio) | |
97 | { | |
98 | return syscall(__NR_ioprio_set, which, who, ioprio); | |
99 | } | |
100 | ||
b95799ca JA |
101 | /* |
102 | * we want fadvise64 really, but it's so tangled... later | |
103 | */ | |
104 | static int fadvise(int fd, loff_t offset, size_t len, int advice) | |
105 | { | |
106 | #if 0 | |
107 | return syscall(__NR_fadvise64, fd, offset, offset >> 32, len, advice); | |
108 | #else | |
109 | return posix_fadvise(fd, (off_t) offset, len, advice); | |
110 | #endif | |
111 | } | |
112 | ||
892199bd JA |
113 | enum { |
114 | IOPRIO_WHO_PROCESS = 1, | |
115 | IOPRIO_WHO_PGRP, | |
116 | IOPRIO_WHO_USER, | |
117 | }; | |
118 | ||
119 | #define IOPRIO_CLASS_SHIFT 13 | |
120 | ||
892199bd JA |
121 | #define MASK (4095) |
122 | ||
4240cfa1 JA |
123 | #define DEF_BS (4096) |
124 | #define DEF_TIMEOUT (30) | |
125 | #define DEF_RATE_CYCLE (1000) | |
126 | #define DEF_ODIRECT (1) | |
127 | #define DEF_SEQUENTIAL (1) | |
4240cfa1 | 128 | #define DEF_RAND_REPEAT (1) |
02983297 JA |
129 | #define DEF_OVERWRITE (0) |
130 | #define DEF_CREATE (1) | |
b95799ca | 131 | #define DEF_INVALIDATE (1) |
99c6704f | 132 | #define DEF_SYNCIO (0) |
eb39963f | 133 | #define DEF_RANDSEED (0xb1899bedUL) |
1d035750 | 134 | #define DEF_BWAVGTIME (500) |
fc097bfe JA |
135 | #define DEF_CREATE_SER (1) |
136 | #define DEF_CREATE_FSYNC (1) | |
b6794fbf | 137 | #define DEF_LOOPS (1) |
cfc702bd | 138 | #define DEF_VERIFY (0) |
4240cfa1 JA |
139 | |
140 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) | |
892199bd | 141 | |
4240cfa1 | 142 | static int repeatable = DEF_RAND_REPEAT; |
02bdd9ba | 143 | static int rate_quit = 1; |
a0a9b35b JA |
144 | static int write_lat_log; |
145 | static int write_bw_log; | |
98dd52d6 | 146 | static int exitall_on_terminate; |
892199bd | 147 | |
892199bd | 148 | static int thread_number; |
7dd1389e | 149 | static char *ini_file; |
892199bd | 150 | |
8867c0a8 JA |
151 | static int max_jobs = MAX_JOBS; |
152 | ||
8dbff0b1 JA |
153 | static char run_str[MAX_JOBS + 1]; |
154 | ||
892199bd JA |
155 | static int shm_id; |
156 | ||
4240cfa1 JA |
157 | enum { |
158 | DDIR_READ = 0, | |
159 | DDIR_WRITE, | |
160 | }; | |
892199bd | 161 | |
02bdd9ba JA |
162 | /* |
163 | * thread life cycle | |
164 | */ | |
165 | enum { | |
166 | TD_NOT_CREATED = 0, | |
167 | TD_CREATED, | |
e8457004 JA |
168 | TD_RUNNING, |
169 | TD_VERIFYING, | |
02bdd9ba JA |
170 | TD_EXITED, |
171 | TD_REAPED, | |
172 | }; | |
173 | ||
99c6704f JA |
174 | enum { |
175 | MEM_MALLOC, | |
176 | MEM_SHM, | |
177 | }; | |
178 | ||
2c83567e JA |
179 | /* |
180 | * The io unit | |
181 | */ | |
182 | struct io_u { | |
183 | struct iocb iocb; | |
57d753e3 | 184 | struct timeval start_time; |
2c83567e JA |
185 | struct timeval issue_time; |
186 | ||
2c83567e JA |
187 | char *buf; |
188 | unsigned int buflen; | |
4ac89145 | 189 | unsigned long long offset; |
2c83567e JA |
190 | |
191 | struct list_head list; | |
192 | }; | |
193 | ||
57d753e3 JA |
194 | struct io_stat { |
195 | unsigned long val; | |
196 | unsigned long val_sq; | |
197 | unsigned long max_val; | |
198 | unsigned long min_val; | |
199 | unsigned long samples; | |
200 | }; | |
201 | ||
a0a9b35b JA |
202 | struct io_sample { |
203 | unsigned long time; | |
204 | unsigned long val; | |
205 | }; | |
206 | ||
207 | struct io_log { | |
208 | unsigned long nr_samples; | |
209 | unsigned long max_samples; | |
210 | struct io_sample *log; | |
211 | }; | |
212 | ||
645785e5 JA |
213 | struct io_piece { |
214 | struct list_head list; | |
215 | unsigned long long offset; | |
216 | unsigned int len; | |
217 | }; | |
218 | ||
e8457004 JA |
219 | #define FIO_HDR_MAGIC 0xf00baaef |
220 | ||
221 | struct verify_header { | |
222 | unsigned int fio_magic; | |
223 | unsigned int len; | |
224 | char md5_digest[MD5_HASH_WORDS * 4]; | |
225 | }; | |
226 | ||
02983297 JA |
227 | #define td_read(td) ((td)->ddir == DDIR_READ) |
228 | #define should_fsync(td) (!td_read(td) && !(td)->odirect) | |
229 | ||
49d2caab JA |
230 | #define BLOCKS_PER_MAP (8 * sizeof(long)) |
231 | #define RAND_MAP_IDX(sector) ((sector) / BLOCKS_PER_MAP) | |
232 | #define RAND_MAP_BIT(sector) ((sector) & (BLOCKS_PER_MAP - 1)) | |
233 | ||
892199bd JA |
234 | struct thread_data { |
235 | char file_name[256]; | |
236 | int thread_number; | |
237 | int error; | |
238 | int fd; | |
892199bd | 239 | pid_t pid; |
6b71c826 | 240 | char *orig_buffer; |
4240cfa1 | 241 | volatile int terminate; |
02bdd9ba | 242 | volatile int runstate; |
40ef7f64 | 243 | volatile int old_runstate; |
f737299d JA |
244 | unsigned int ddir; |
245 | unsigned int ioprio; | |
246 | unsigned int sequential; | |
247 | unsigned int bs; | |
8c033f93 JA |
248 | unsigned int min_bs; |
249 | unsigned int max_bs; | |
f737299d | 250 | unsigned int odirect; |
e97712ed | 251 | unsigned int thinktime; |
4240cfa1 | 252 | unsigned int fsync_blocks; |
fc24389f | 253 | unsigned int start_delay; |
47d45203 | 254 | unsigned int timeout; |
43000118 | 255 | unsigned int use_aio; |
02983297 JA |
256 | unsigned int create_file; |
257 | unsigned int overwrite; | |
b95799ca | 258 | unsigned int invalidate_cache; |
1d035750 | 259 | unsigned int bw_avg_time; |
fc097bfe JA |
260 | unsigned int create_serialize; |
261 | unsigned int create_fsync; | |
b6794fbf | 262 | unsigned int loops; |
02983297 JA |
263 | unsigned long long file_size; |
264 | unsigned long long file_offset; | |
74b4b5fb | 265 | unsigned int sync_io; |
99c6704f | 266 | unsigned int mem_type; |
cfc702bd | 267 | unsigned int verify; |
18e0b78c | 268 | cpu_set_t cpumask; |
86184d14 | 269 | |
7889f07b | 270 | struct drand48_data bsrange_state; |
e8457004 | 271 | struct drand48_data verify_state; |
7889f07b | 272 | |
99c6704f JA |
273 | int shm_id; |
274 | ||
63a09e51 JA |
275 | off_t cur_off; |
276 | ||
254605cd | 277 | io_context_t aio_ctx; |
43000118 | 278 | unsigned int aio_depth; |
43000118 | 279 | struct io_event *aio_events; |
2c83567e JA |
280 | |
281 | unsigned int cur_depth; | |
282 | struct list_head io_u_freelist; | |
283 | struct list_head io_u_busylist; | |
43000118 | 284 | |
7dd1389e | 285 | unsigned int rate; |
4240cfa1 JA |
286 | unsigned int ratemin; |
287 | unsigned int ratecycle; | |
288 | unsigned long rate_usec_cycle; | |
289 | long rate_pending_usleep; | |
49d2caab | 290 | unsigned long rate_bytes; |
4240cfa1 | 291 | struct timeval lastrate; |
86184d14 | 292 | |
892199bd | 293 | unsigned long runtime; /* sec */ |
49d2caab | 294 | unsigned long long io_size; |
e2ba35d9 | 295 | |
4240cfa1 | 296 | unsigned long io_blocks; |
49d2caab JA |
297 | unsigned long io_bytes; |
298 | unsigned long this_io_bytes; | |
299 | unsigned long last_bytes; | |
892199bd | 300 | sem_t mutex; |
49d2caab | 301 | |
892199bd | 302 | struct drand48_data random_state; |
49d2caab JA |
303 | unsigned long *file_map; |
304 | unsigned int num_maps; | |
892199bd JA |
305 | |
306 | /* | |
e128065d | 307 | * bandwidth and latency stats |
892199bd | 308 | */ |
57d753e3 JA |
309 | struct io_stat clat_stat; /* completion latency */ |
310 | struct io_stat slat_stat; /* submission latency */ | |
311 | ||
312 | struct io_stat bw_stat; /* bandwidth stats */ | |
49d2caab | 313 | unsigned long stat_io_bytes; |
fd1ae4c9 | 314 | struct timeval stat_sample_time; |
4240cfa1 | 315 | |
a0a9b35b JA |
316 | struct io_log *lat_log; |
317 | struct io_log *bw_log; | |
318 | ||
4240cfa1 | 319 | struct timeval start; |
645785e5 JA |
320 | |
321 | struct list_head io_hist_list; | |
892199bd JA |
322 | }; |
323 | ||
324 | static struct thread_data *threads; | |
47d45203 | 325 | static struct thread_data def_thread; |
892199bd JA |
326 | |
327 | static sem_t startup_sem; | |
328 | ||
5c24b2c4 | 329 | static void sig_handler(int sig) |
892199bd JA |
330 | { |
331 | int i; | |
332 | ||
213b446c JA |
333 | for (i = 0; i < thread_number; i++) { |
334 | struct thread_data *td = &threads[i]; | |
335 | ||
336 | td->terminate = 1; | |
337 | td->start_delay = 0; | |
338 | } | |
02bdd9ba JA |
339 | } |
340 | ||
e8457004 | 341 | static int init_random_state(struct thread_data *td) |
946d8870 | 342 | { |
e8457004 | 343 | unsigned long seed; |
49d2caab | 344 | int fd, num_maps, blocks; |
946d8870 | 345 | |
e8457004 JA |
346 | fd = open("/dev/random", O_RDONLY); |
347 | if (fd == -1) { | |
348 | td->error = errno; | |
349 | return 1; | |
946d8870 JA |
350 | } |
351 | ||
e8457004 JA |
352 | if (read(fd, &seed, sizeof(seed)) < (int) sizeof(seed)) { |
353 | td->error = EIO; | |
354 | close(fd); | |
355 | return 1; | |
946d8870 JA |
356 | } |
357 | ||
e8457004 | 358 | close(fd); |
7889f07b JA |
359 | |
360 | srand48_r(seed, &td->bsrange_state); | |
e8457004 | 361 | srand48_r(seed, &td->verify_state); |
892199bd JA |
362 | |
363 | if (td->sequential) | |
364 | return 0; | |
365 | ||
e8457004 JA |
366 | if (repeatable) |
367 | seed = DEF_RANDSEED; | |
892199bd | 368 | |
49d2caab JA |
369 | blocks = (td->io_size + td->min_bs - 1) / td->min_bs; |
370 | num_maps = blocks / BLOCKS_PER_MAP; | |
371 | td->file_map = malloc(num_maps * sizeof(long)); | |
372 | td->num_maps = num_maps; | |
373 | memset(td->file_map, 0, num_maps * sizeof(long)); | |
374 | ||
892199bd JA |
375 | srand48_r(seed, &td->random_state); |
376 | return 0; | |
377 | } | |
378 | ||
5c24b2c4 | 379 | static unsigned long utime_since(struct timeval *s, struct timeval *e) |
892199bd JA |
380 | { |
381 | double sec, usec; | |
382 | ||
383 | sec = e->tv_sec - s->tv_sec; | |
384 | usec = e->tv_usec - s->tv_usec; | |
385 | if (sec > 0 && usec < 0) { | |
386 | sec--; | |
387 | usec += 1000000; | |
388 | } | |
389 | ||
390 | sec *= (double) 1000000; | |
391 | ||
392 | return sec + usec; | |
393 | } | |
394 | ||
5c24b2c4 | 395 | static unsigned long mtime_since(struct timeval *s, struct timeval *e) |
892199bd JA |
396 | { |
397 | double sec, usec; | |
398 | ||
399 | sec = e->tv_sec - s->tv_sec; | |
400 | usec = e->tv_usec - s->tv_usec; | |
401 | if (sec > 0 && usec < 0) { | |
402 | sec--; | |
403 | usec += 1000000; | |
404 | } | |
405 | ||
406 | sec *= (double) 1000; | |
407 | usec /= (double) 1000; | |
408 | ||
409 | return sec + usec; | |
410 | } | |
411 | ||
be33abe4 JA |
412 | static unsigned long mtime_since_now(struct timeval *s) |
413 | { | |
414 | struct timeval t; | |
415 | ||
416 | gettimeofday(&t, NULL); | |
417 | return mtime_since(s, &t); | |
418 | } | |
419 | ||
98168d55 JA |
420 | static inline unsigned long msec_now(struct timeval *s) |
421 | { | |
422 | return s->tv_sec * 1000 + s->tv_usec / 1000; | |
423 | } | |
424 | ||
49d2caab JA |
425 | static int random_map_free(struct thread_data *td, unsigned long long block) |
426 | { | |
427 | unsigned int idx = RAND_MAP_IDX(block); | |
428 | unsigned int bit = RAND_MAP_BIT(block); | |
429 | ||
430 | return (td->file_map[idx] & (1UL << bit)) == 0; | |
431 | } | |
432 | ||
433 | static int get_next_free_block(struct thread_data *td, unsigned long long *b) | |
892199bd | 434 | { |
49d2caab JA |
435 | int i; |
436 | ||
437 | *b = 0; | |
438 | i = 0; | |
439 | while ((*b) * td->min_bs < td->io_size) { | |
440 | if (td->file_map[i] != -1UL) { | |
441 | *b += ffz(td->file_map[i]); | |
442 | return 0; | |
443 | } | |
444 | ||
445 | *b += BLOCKS_PER_MAP; | |
446 | i++; | |
447 | } | |
448 | ||
449 | return 1; | |
450 | } | |
451 | ||
452 | static void mark_random_map(struct thread_data *td, struct io_u *io_u) | |
453 | { | |
454 | unsigned long block = io_u->offset / td->min_bs; | |
455 | unsigned int blocks = 0; | |
456 | ||
457 | while (blocks < (io_u->buflen / td->min_bs)) { | |
458 | int idx, bit; | |
459 | ||
460 | if (!random_map_free(td, block)) | |
461 | break; | |
462 | ||
463 | idx = RAND_MAP_IDX(block); | |
464 | bit = RAND_MAP_BIT(block); | |
465 | ||
466 | assert(idx < td->num_maps); | |
467 | ||
468 | td->file_map[idx] |= (1UL << bit); | |
469 | block++; | |
470 | blocks++; | |
471 | } | |
472 | ||
473 | if ((blocks * td->min_bs) < io_u->buflen) | |
474 | io_u->buflen = blocks * td->min_bs; | |
475 | } | |
476 | ||
477 | static int get_next_offset(struct thread_data *td, unsigned long long *offset) | |
478 | { | |
479 | unsigned long long b; | |
d32d9284 | 480 | long r; |
892199bd JA |
481 | |
482 | if (!td->sequential) { | |
49d2caab JA |
483 | unsigned long max_blocks = td->io_size / td->min_bs; |
484 | int loops = 50; | |
485 | ||
486 | do { | |
487 | lrand48_r(&td->random_state, &r); | |
488 | b = ((max_blocks - 1) * r / (RAND_MAX+1.0)); | |
489 | loops--; | |
490 | } while (!random_map_free(td, b) && loops); | |
491 | ||
492 | if (!loops) { | |
493 | if (get_next_free_block(td, &b)) | |
494 | return 1; | |
495 | } | |
7889f07b | 496 | } else |
49d2caab | 497 | b = td->last_bytes / td->min_bs; |
7889f07b | 498 | |
49d2caab JA |
499 | *offset = (b * td->min_bs) + td->file_offset; |
500 | return 0; | |
7889f07b JA |
501 | } |
502 | ||
503 | static unsigned int get_next_buflen(struct thread_data *td) | |
504 | { | |
505 | unsigned int buflen; | |
d32d9284 | 506 | long r; |
7889f07b JA |
507 | |
508 | if (td->min_bs == td->max_bs) | |
509 | buflen = td->min_bs; | |
510 | else { | |
d32d9284 | 511 | lrand48_r(&td->bsrange_state, &r); |
7889f07b JA |
512 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
513 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); | |
892199bd JA |
514 | } |
515 | ||
49d2caab JA |
516 | if (buflen > td->io_size - td->this_io_bytes) |
517 | buflen = td->io_size - td->this_io_bytes; | |
7889f07b | 518 | |
7889f07b | 519 | return buflen; |
892199bd JA |
520 | } |
521 | ||
57d753e3 JA |
522 | static inline void add_stat_sample(struct thread_data *td, struct io_stat *is, |
523 | unsigned long val) | |
892199bd | 524 | { |
57d753e3 JA |
525 | if (val > is->max_val) |
526 | is->max_val = val; | |
527 | if (val < is->min_val) | |
528 | is->min_val = val; | |
529 | ||
530 | is->val += val; | |
531 | is->val_sq += val * val; | |
532 | is->samples++; | |
533 | } | |
fd1ae4c9 | 534 | |
a0a9b35b JA |
535 | static void add_log_sample(struct thread_data *td, struct io_log *log, |
536 | unsigned long val) | |
537 | { | |
538 | if (log->nr_samples == log->max_samples) { | |
539 | int new_size = sizeof(struct io_sample) * log->max_samples * 2; | |
540 | ||
541 | log->log = realloc(log->log, new_size); | |
542 | log->max_samples <<= 1; | |
543 | } | |
544 | ||
545 | log->log[log->nr_samples].val = val; | |
546 | log->log[log->nr_samples].time = mtime_since_now(&td->start); | |
547 | log->nr_samples++; | |
548 | } | |
549 | ||
57d753e3 JA |
550 | static void add_clat_sample(struct thread_data *td, unsigned long msec) |
551 | { | |
552 | add_stat_sample(td, &td->clat_stat, msec); | |
a0a9b35b JA |
553 | |
554 | if (td->lat_log) | |
555 | add_log_sample(td, td->lat_log, msec); | |
57d753e3 | 556 | } |
fd1ae4c9 | 557 | |
57d753e3 JA |
558 | static void add_slat_sample(struct thread_data *td, unsigned long msec) |
559 | { | |
560 | add_stat_sample(td, &td->slat_stat, msec); | |
561 | } | |
fd1ae4c9 | 562 | |
645785e5 | 563 | static void add_bw_sample(struct thread_data *td) |
57d753e3 JA |
564 | { |
565 | unsigned long spent = mtime_since_now(&td->stat_sample_time); | |
566 | unsigned long rate; | |
567 | ||
1d035750 | 568 | if (spent < td->bw_avg_time) |
57d753e3 JA |
569 | return; |
570 | ||
49d2caab | 571 | rate = (td->this_io_bytes - td->stat_io_bytes) / spent; |
57d753e3 JA |
572 | add_stat_sample(td, &td->bw_stat, rate); |
573 | ||
a0a9b35b JA |
574 | if (td->bw_log) |
575 | add_log_sample(td, td->bw_log, rate); | |
576 | ||
57d753e3 | 577 | gettimeofday(&td->stat_sample_time, NULL); |
49d2caab | 578 | td->stat_io_bytes = td->this_io_bytes; |
892199bd JA |
579 | } |
580 | ||
5c24b2c4 | 581 | static void usec_sleep(int usec) |
892199bd | 582 | { |
86184d14 JA |
583 | struct timespec req = { .tv_sec = 0, .tv_nsec = usec * 1000 }; |
584 | struct timespec rem; | |
892199bd JA |
585 | |
586 | do { | |
86184d14 JA |
587 | rem.tv_sec = rem.tv_nsec = 0; |
588 | nanosleep(&req, &rem); | |
589 | if (!rem.tv_nsec) | |
892199bd | 590 | break; |
86184d14 JA |
591 | |
592 | req.tv_nsec = rem.tv_nsec; | |
892199bd JA |
593 | } while (1); |
594 | } | |
595 | ||
9e850933 JA |
596 | static void rate_throttle(struct thread_data *td, unsigned long time_spent, |
597 | unsigned int bytes) | |
86184d14 | 598 | { |
9e850933 JA |
599 | unsigned long usec_cycle; |
600 | ||
4240cfa1 JA |
601 | if (!td->rate) |
602 | return; | |
603 | ||
9e850933 JA |
604 | usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs); |
605 | ||
606 | if (time_spent < usec_cycle) { | |
607 | unsigned long s = usec_cycle - time_spent; | |
86184d14 JA |
608 | |
609 | td->rate_pending_usleep += s; | |
fad86e6a | 610 | if (td->rate_pending_usleep >= 100000) { |
86184d14 JA |
611 | usec_sleep(td->rate_pending_usleep); |
612 | td->rate_pending_usleep = 0; | |
613 | } | |
4240cfa1 | 614 | } else { |
9e850933 | 615 | long overtime = time_spent - usec_cycle; |
42b2b9fe | 616 | |
4240cfa1 JA |
617 | td->rate_pending_usleep -= overtime; |
618 | } | |
619 | } | |
620 | ||
5c24b2c4 | 621 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
4240cfa1 | 622 | { |
7607bc6b | 623 | unsigned long spent; |
4240cfa1 JA |
624 | unsigned long rate; |
625 | ||
626 | /* | |
627 | * allow a 2 second settle period in the beginning | |
628 | */ | |
7607bc6b | 629 | if (mtime_since(&td->start, now) < 2000) |
4240cfa1 JA |
630 | return 0; |
631 | ||
632 | /* | |
633 | * if rate blocks is set, sample is running | |
634 | */ | |
49d2caab | 635 | if (td->rate_bytes) { |
4240cfa1 JA |
636 | spent = mtime_since(&td->lastrate, now); |
637 | if (spent < td->ratecycle) | |
638 | return 0; | |
639 | ||
49d2caab | 640 | rate = (td->this_io_bytes - td->rate_bytes) / spent; |
4240cfa1 JA |
641 | if (rate < td->ratemin) { |
642 | printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate); | |
02bdd9ba | 643 | if (rate_quit) |
e6402082 | 644 | sig_handler(0); |
4240cfa1 JA |
645 | return 1; |
646 | } | |
86184d14 | 647 | } |
4240cfa1 | 648 | |
49d2caab | 649 | td->rate_bytes = td->this_io_bytes; |
4240cfa1 JA |
650 | memcpy(&td->lastrate, now, sizeof(*now)); |
651 | return 0; | |
86184d14 JA |
652 | } |
653 | ||
67903a2e JA |
654 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
655 | { | |
656 | if (mtime_since(&td->start, t) >= td->timeout * 1000) | |
657 | return 1; | |
658 | ||
659 | return 0; | |
660 | } | |
661 | ||
e8457004 JA |
662 | static void fill_random_bytes(struct thread_data *td, |
663 | unsigned char *p, unsigned int len) | |
664 | { | |
645785e5 | 665 | unsigned int todo; |
40ef7f64 | 666 | double r; |
e8457004 JA |
667 | |
668 | while (len) { | |
40ef7f64 | 669 | drand48_r(&td->verify_state, &r); |
e8457004 | 670 | |
40ef7f64 JA |
671 | /* |
672 | * lrand48_r seems to be broken and only fill the bottom | |
673 | * 32-bits, even on 64-bit archs with 64-bit longs | |
674 | */ | |
675 | todo = sizeof(r); | |
e8457004 JA |
676 | if (todo > len) |
677 | todo = len; | |
678 | ||
679 | memcpy(p, &r, todo); | |
680 | ||
681 | len -= todo; | |
682 | p += todo; | |
683 | } | |
684 | } | |
685 | ||
9d0c6ca2 JA |
686 | static void hexdump(void *buffer, int len) |
687 | { | |
688 | unsigned char *p = buffer; | |
689 | int i; | |
690 | ||
691 | for (i = 0; i < len; i++) | |
692 | printf("%02x", p[i]); | |
693 | printf("\n"); | |
694 | } | |
695 | ||
645785e5 | 696 | static int verify_io_u(struct io_u *io_u) |
e8457004 JA |
697 | { |
698 | struct verify_header *hdr = (struct verify_header *) io_u->buf; | |
699 | unsigned char *p = (unsigned char *) io_u->buf; | |
700 | struct md5_ctx md5_ctx; | |
9d0c6ca2 | 701 | int ret; |
e8457004 | 702 | |
840b216f | 703 | if (hdr->fio_magic != FIO_HDR_MAGIC) |
e8457004 JA |
704 | return 1; |
705 | ||
706 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
707 | p += sizeof(*hdr); | |
708 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); | |
709 | ||
9d0c6ca2 JA |
710 | ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); |
711 | if (ret) { | |
712 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); | |
713 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); | |
714 | } | |
715 | ||
716 | return ret; | |
e8457004 JA |
717 | } |
718 | ||
cfc702bd JA |
719 | /* |
720 | * fill body of io_u->buf with random data and add a header with the | |
721 | * (eg) sha1sum of that data. | |
722 | */ | |
e8457004 | 723 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) |
cfc702bd | 724 | { |
e8457004 JA |
725 | struct md5_ctx md5_ctx; |
726 | struct verify_header hdr; | |
727 | unsigned char *p = (unsigned char *) io_u->buf; | |
728 | ||
729 | hdr.fio_magic = FIO_HDR_MAGIC; | |
730 | hdr.len = io_u->buflen; | |
731 | p += sizeof(hdr); | |
732 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); | |
733 | ||
734 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
735 | md5_update(&md5_ctx, p, io_u->buflen - sizeof(hdr)); | |
736 | memcpy(hdr.md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); | |
737 | memcpy(io_u->buf, &hdr, sizeof(hdr)); | |
cfc702bd JA |
738 | } |
739 | ||
2c83567e JA |
740 | static void put_io_u(struct thread_data *td, struct io_u *io_u) |
741 | { | |
742 | list_del(&io_u->list); | |
743 | list_add(&io_u->list, &td->io_u_freelist); | |
744 | td->cur_depth--; | |
745 | } | |
746 | ||
f0f3411b JA |
747 | #define queue_full(td) (list_empty(&(td)->io_u_freelist)) |
748 | ||
e8457004 JA |
749 | static struct io_u *__get_io_u(struct thread_data *td) |
750 | { | |
751 | struct io_u *io_u; | |
752 | ||
f0f3411b | 753 | if (queue_full(td)) |
e8457004 JA |
754 | return NULL; |
755 | ||
756 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); | |
757 | list_del(&io_u->list); | |
758 | list_add(&io_u->list, &td->io_u_busylist); | |
f4bb2243 | 759 | td->cur_depth++; |
e8457004 JA |
760 | return io_u; |
761 | } | |
762 | ||
2c83567e JA |
763 | static struct io_u *get_io_u(struct thread_data *td) |
764 | { | |
765 | struct io_u *io_u; | |
766 | ||
e8457004 JA |
767 | io_u = __get_io_u(td); |
768 | if (!io_u) | |
2c83567e JA |
769 | return NULL; |
770 | ||
49d2caab JA |
771 | if (get_next_offset(td, &io_u->offset)) |
772 | return NULL; | |
773 | ||
b2a369fb JA |
774 | io_u->buflen = get_next_buflen(td); |
775 | if (!io_u->buflen) { | |
e8457004 | 776 | put_io_u(td, io_u); |
7889f07b | 777 | return NULL; |
e8457004 | 778 | } |
2c83567e | 779 | |
49d2caab JA |
780 | if (io_u->buflen + io_u->offset > td->io_size) |
781 | io_u->buflen = td->io_size - io_u->offset; | |
782 | ||
783 | if (!td->sequential) | |
784 | mark_random_map(td, io_u); | |
785 | ||
786 | td->last_bytes += io_u->buflen; | |
787 | ||
9d0c6ca2 | 788 | if (td->verify) |
e8457004 | 789 | populate_io_u(td, io_u); |
cfc702bd | 790 | |
2c83567e JA |
791 | if (td->use_aio) { |
792 | if (td_read(td)) | |
793 | io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
794 | else | |
795 | io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
796 | } | |
797 | ||
57d753e3 | 798 | gettimeofday(&io_u->start_time, NULL); |
2c83567e JA |
799 | return io_u; |
800 | } | |
801 | ||
40ef7f64 JA |
802 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
803 | { | |
804 | td->old_runstate = td->runstate; | |
805 | td->runstate = runstate; | |
806 | } | |
807 | ||
645785e5 JA |
808 | static int get_next_verify(struct thread_data *td, |
809 | unsigned long long *offset, unsigned int *len) | |
810 | { | |
811 | struct io_piece *ipo; | |
812 | ||
813 | if (list_empty(&td->io_hist_list)) | |
814 | return 1; | |
815 | ||
816 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
817 | list_del(&ipo->list); | |
818 | ||
819 | *offset = ipo->offset; | |
820 | *len = ipo->len; | |
821 | free(ipo); | |
822 | return 0; | |
823 | } | |
824 | ||
9d0c6ca2 JA |
825 | static void prune_io_piece_log(struct thread_data *td) |
826 | { | |
827 | struct io_piece *ipo; | |
828 | ||
829 | while (!list_empty(&td->io_hist_list)) { | |
830 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
831 | ||
832 | list_del(&ipo->list); | |
833 | free(ipo); | |
834 | } | |
835 | } | |
836 | ||
9d0c6ca2 JA |
837 | /* |
838 | * log a succesful write, so we can unwind the log for verify | |
839 | */ | |
840 | static void log_io_piece(struct thread_data *td, struct io_u *io_u) | |
841 | { | |
49d2caab | 842 | struct io_piece *ipo = malloc(sizeof(struct io_piece)); |
9d0c6ca2 JA |
843 | struct list_head *entry; |
844 | ||
845 | INIT_LIST_HEAD(&ipo->list); | |
846 | ipo->offset = io_u->offset; | |
847 | ipo->len = io_u->buflen; | |
848 | ||
49d2caab JA |
849 | /* |
850 | * for random io where the writes extend the file, it will typically | |
851 | * be laid out with the block scattered as written. it's faster to | |
852 | * read them in in that order again, so don't sort | |
853 | */ | |
854 | if (td->sequential || !td->overwrite) { | |
9d0c6ca2 JA |
855 | list_add_tail(&ipo->list, &td->io_hist_list); |
856 | return; | |
857 | } | |
858 | ||
859 | /* | |
860 | * for random io, sort the list so verify will run faster | |
861 | */ | |
862 | entry = &td->io_hist_list; | |
863 | while ((entry = entry->prev) != &td->io_hist_list) { | |
864 | struct io_piece *__ipo = list_entry(entry, struct io_piece, list); | |
865 | ||
9d0c6ca2 JA |
866 | if (__ipo->offset < ipo->offset) |
867 | break; | |
868 | } | |
869 | ||
870 | list_add(&ipo->list, entry); | |
871 | } | |
872 | ||
91fc5dc9 | 873 | static void do_sync_verify(struct thread_data *td) |
cfc702bd | 874 | { |
40ef7f64 | 875 | struct timeval t; |
e8457004 | 876 | struct io_u *io_u = NULL; |
645785e5 | 877 | int ret; |
e8457004 | 878 | |
40ef7f64 | 879 | td_set_runstate(td, TD_VERIFYING); |
e8457004 JA |
880 | |
881 | io_u = __get_io_u(td); | |
882 | ||
40ef7f64 | 883 | if (!td->odirect) { |
49d2caab | 884 | if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) { |
40ef7f64 JA |
885 | td->error = errno; |
886 | goto out; | |
887 | } | |
888 | } | |
889 | ||
e8457004 JA |
890 | do { |
891 | if (td->terminate) | |
892 | break; | |
40ef7f64 JA |
893 | |
894 | gettimeofday(&t, NULL); | |
895 | if (runtime_exceeded(td, &t)) | |
896 | break; | |
897 | ||
645785e5 JA |
898 | if (get_next_verify(td, &io_u->offset, &io_u->buflen)) |
899 | break; | |
900 | ||
901 | if (td->cur_off != io_u->offset) { | |
902 | if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) { | |
903 | td->error = errno; | |
904 | break; | |
905 | } | |
906 | } | |
e8457004 JA |
907 | |
908 | ret = read(td->fd, io_u->buf, io_u->buflen); | |
909 | if (ret < (int) io_u->buflen) { | |
910 | if (ret == -1) { | |
911 | td->error = errno; | |
912 | break; | |
913 | } else if (!ret) | |
914 | break; | |
915 | else | |
916 | io_u->buflen = ret; | |
917 | } | |
918 | ||
645785e5 | 919 | if (verify_io_u(io_u)) |
e8457004 JA |
920 | break; |
921 | ||
645785e5 | 922 | td->cur_off = io_u->offset + io_u->buflen; |
e8457004 JA |
923 | } while (1); |
924 | ||
925 | out: | |
40ef7f64 | 926 | td_set_runstate(td, TD_RUNNING); |
e8457004 | 927 | put_io_u(td, io_u); |
cfc702bd JA |
928 | } |
929 | ||
43000118 | 930 | static void do_sync_io(struct thread_data *td) |
892199bd | 931 | { |
7889f07b | 932 | unsigned long msec, usec; |
e8457004 | 933 | struct io_u *io_u = NULL; |
2c83567e | 934 | struct timeval e; |
892199bd | 935 | |
49d2caab | 936 | while (td->this_io_bytes < td->io_size) { |
892199bd JA |
937 | int ret; |
938 | ||
939 | if (td->terminate) | |
940 | break; | |
941 | ||
2c83567e | 942 | io_u = get_io_u(td); |
7889f07b JA |
943 | if (!io_u) |
944 | break; | |
2c83567e | 945 | |
63a09e51 JA |
946 | if (td->cur_off != io_u->offset) { |
947 | if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) { | |
948 | td->error = errno; | |
949 | break; | |
950 | } | |
892199bd JA |
951 | } |
952 | ||
02983297 | 953 | if (td_read(td)) |
2c83567e | 954 | ret = read(td->fd, io_u->buf, io_u->buflen); |
892199bd | 955 | else |
2c83567e | 956 | ret = write(td->fd, io_u->buf, io_u->buflen); |
892199bd | 957 | |
2c83567e | 958 | if (ret < (int) io_u->buflen) { |
892199bd JA |
959 | if (ret == -1) |
960 | td->error = errno; | |
961 | break; | |
962 | } | |
963 | ||
645785e5 JA |
964 | if (!td_read(td)) |
965 | log_io_piece(td, io_u); | |
966 | ||
4240cfa1 | 967 | td->io_blocks++; |
49d2caab JA |
968 | td->io_bytes += io_u->buflen; |
969 | td->this_io_bytes += io_u->buflen; | |
63a09e51 | 970 | td->cur_off = io_u->offset + io_u->buflen; |
4240cfa1 | 971 | |
86184d14 JA |
972 | gettimeofday(&e, NULL); |
973 | ||
57d753e3 | 974 | usec = utime_since(&io_u->start_time, &e); |
86184d14 | 975 | |
9e850933 | 976 | rate_throttle(td, usec, io_u->buflen); |
892199bd | 977 | |
4240cfa1 JA |
978 | if (check_min_rate(td, &e)) { |
979 | td->error = ENODATA; | |
980 | break; | |
981 | } | |
892199bd | 982 | |
4240cfa1 | 983 | msec = usec / 1000; |
57d753e3 | 984 | add_clat_sample(td, msec); |
645785e5 | 985 | add_bw_sample(td); |
67903a2e JA |
986 | |
987 | if (runtime_exceeded(td, &e)) | |
988 | break; | |
2c83567e | 989 | |
cdf92433 | 990 | put_io_u(td, io_u); |
e8457004 | 991 | io_u = NULL; |
cdf92433 | 992 | |
e97712ed JA |
993 | if (td->thinktime) |
994 | usec_sleep(td->thinktime); | |
995 | ||
cdf92433 JA |
996 | if (should_fsync(td) && td->fsync_blocks && |
997 | (td->io_blocks % td->fsync_blocks) == 0) | |
998 | fsync(td->fd); | |
892199bd JA |
999 | } |
1000 | ||
e8457004 JA |
1001 | if (io_u) |
1002 | put_io_u(td, io_u); | |
1003 | ||
4240cfa1 | 1004 | if (should_fsync(td)) |
892199bd | 1005 | fsync(td->fd); |
892199bd | 1006 | } |
43000118 | 1007 | |
2c83567e | 1008 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) |
56b0eff0 | 1009 | { |
2c83567e | 1010 | struct iocb *iocb = &io_u->iocb; |
56b0eff0 JA |
1011 | int ret; |
1012 | ||
1013 | do { | |
254605cd | 1014 | ret = io_submit(td->aio_ctx, 1, &iocb); |
56b0eff0 JA |
1015 | if (ret == 1) |
1016 | return 0; | |
a592bd33 | 1017 | else if (ret == EAGAIN) |
56b0eff0 | 1018 | usleep(100); |
a592bd33 JA |
1019 | else if (ret == EINTR) |
1020 | continue; | |
56b0eff0 JA |
1021 | else |
1022 | break; | |
1023 | } while (1); | |
1024 | ||
a592bd33 | 1025 | return ret; |
56b0eff0 JA |
1026 | } |
1027 | ||
98168d55 | 1028 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
2c83567e JA |
1029 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) |
1030 | ||
f0f3411b | 1031 | static int ios_completed(struct thread_data *td, int nr) |
2c83567e JA |
1032 | { |
1033 | unsigned long msec; | |
1034 | struct io_u *io_u; | |
1035 | struct timeval e; | |
9e850933 | 1036 | int i, bytes_done; |
2c83567e | 1037 | |
f0f3411b | 1038 | gettimeofday(&e, NULL); |
2c83567e | 1039 | |
9e850933 | 1040 | for (i = 0, bytes_done = 0; i < nr; i++) { |
2c83567e JA |
1041 | io_u = ev_to_iou(td->aio_events + i); |
1042 | ||
f0f3411b | 1043 | td->io_blocks++; |
49d2caab JA |
1044 | td->io_bytes += io_u->buflen; |
1045 | td->this_io_bytes += io_u->buflen; | |
8c033f93 | 1046 | |
f0f3411b | 1047 | msec = mtime_since(&io_u->issue_time, &e); |
2c83567e | 1048 | |
f0f3411b | 1049 | add_clat_sample(td, msec); |
645785e5 JA |
1050 | add_bw_sample(td); |
1051 | ||
1052 | if (!td_read(td)) | |
1053 | log_io_piece(td, io_u); | |
2c83567e | 1054 | |
f4bb2243 | 1055 | bytes_done += io_u->buflen; |
2c83567e JA |
1056 | put_io_u(td, io_u); |
1057 | } | |
9e850933 JA |
1058 | |
1059 | return bytes_done; | |
2c83567e JA |
1060 | } |
1061 | ||
1062 | static void cleanup_pending_aio(struct thread_data *td) | |
1063 | { | |
1064 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
1065 | struct list_head *entry, *n; | |
1066 | struct io_u *io_u; | |
1067 | int r; | |
1068 | ||
1069 | /* | |
1070 | * get immediately available events, if any | |
1071 | */ | |
1072 | r = io_getevents(td->aio_ctx, 0, td->cur_depth, td->aio_events, &ts); | |
1073 | if (r > 0) | |
f0f3411b | 1074 | ios_completed(td, r); |
2c83567e JA |
1075 | |
1076 | /* | |
1077 | * now cancel remaining active events | |
1078 | */ | |
1079 | list_for_each_safe(entry, n, &td->io_u_busylist) { | |
1080 | io_u = list_entry(entry, struct io_u, list); | |
1081 | ||
1082 | r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events); | |
1083 | if (!r) | |
1084 | put_io_u(td, io_u); | |
1085 | } | |
1086 | ||
1087 | if (td->cur_depth) { | |
1088 | r = io_getevents(td->aio_ctx, td->cur_depth, td->cur_depth, td->aio_events, NULL); | |
1089 | if (r > 0) | |
f0f3411b | 1090 | ios_completed(td, r); |
2c83567e JA |
1091 | } |
1092 | } | |
98168d55 | 1093 | |
d32d9284 JA |
1094 | static int async_do_verify(struct thread_data *td, struct io_u **io_u) |
1095 | { | |
1096 | struct io_u *v_io_u = *io_u; | |
1097 | int ret = 0; | |
1098 | ||
1099 | if (v_io_u) { | |
645785e5 | 1100 | ret = verify_io_u(v_io_u); |
d32d9284 JA |
1101 | put_io_u(td, v_io_u); |
1102 | *io_u = NULL; | |
1103 | } | |
1104 | ||
1105 | return ret; | |
1106 | } | |
1107 | ||
91fc5dc9 | 1108 | static void do_async_verify(struct thread_data *td) |
cfc702bd | 1109 | { |
f4bb2243 | 1110 | struct timeval t; |
d32d9284 | 1111 | struct io_u *io_u, *v_io_u = NULL; |
645785e5 | 1112 | int ret; |
f4bb2243 JA |
1113 | |
1114 | td_set_runstate(td, TD_VERIFYING); | |
1115 | ||
f4bb2243 JA |
1116 | do { |
1117 | if (td->terminate) | |
1118 | break; | |
1119 | ||
1120 | gettimeofday(&t, NULL); | |
1121 | if (runtime_exceeded(td, &t)) | |
1122 | break; | |
1123 | ||
1124 | io_u = __get_io_u(td); | |
1125 | if (!io_u) | |
1126 | break; | |
1127 | ||
645785e5 JA |
1128 | if (get_next_verify(td, &io_u->offset, &io_u->buflen)) { |
1129 | put_io_u(td, io_u); | |
1130 | break; | |
f4bb2243 JA |
1131 | } |
1132 | ||
1133 | io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
1134 | ret = io_u_queue(td, io_u); | |
1135 | if (ret) { | |
1136 | put_io_u(td, io_u); | |
1137 | td->error = ret; | |
1138 | break; | |
1139 | } | |
1140 | ||
f0f3411b JA |
1141 | /* |
1142 | * we have one pending to verify, do that while the next | |
1143 | * we are doing io on the next one | |
1144 | */ | |
d32d9284 JA |
1145 | if (async_do_verify(td, &v_io_u)) |
1146 | break; | |
f0f3411b JA |
1147 | |
1148 | ret = io_getevents(td->aio_ctx, 1, 1, td->aio_events, NULL); | |
1149 | if (ret != 1) { | |
1150 | if (ret < 0) | |
1151 | td->error = ret; | |
f4bb2243 JA |
1152 | break; |
1153 | } | |
1154 | ||
f0f3411b | 1155 | v_io_u = ev_to_iou(td->aio_events); |
f4bb2243 | 1156 | |
645785e5 | 1157 | td->cur_off = v_io_u->offset + v_io_u->buflen; |
f0f3411b JA |
1158 | |
1159 | /* | |
d32d9284 | 1160 | * if we can't submit more io, we need to verify now |
f0f3411b | 1161 | */ |
d32d9284 JA |
1162 | if (queue_full(td) && async_do_verify(td, &v_io_u)) |
1163 | break; | |
1164 | ||
f4bb2243 JA |
1165 | } while (1); |
1166 | ||
d32d9284 | 1167 | async_do_verify(td, &v_io_u); |
f0f3411b | 1168 | |
f4bb2243 JA |
1169 | if (td->cur_depth) |
1170 | cleanup_pending_aio(td); | |
1171 | ||
1172 | td_set_runstate(td, TD_RUNNING); | |
cfc702bd JA |
1173 | } |
1174 | ||
43000118 JA |
1175 | static void do_async_io(struct thread_data *td) |
1176 | { | |
1177 | struct timeval s, e; | |
7889f07b | 1178 | unsigned long usec; |
43000118 | 1179 | |
49d2caab | 1180 | while (td->this_io_bytes < td->io_size) { |
43000118 JA |
1181 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
1182 | struct timespec *timeout; | |
2c83567e JA |
1183 | int ret, min_evts = 0; |
1184 | struct io_u *io_u; | |
9e850933 | 1185 | unsigned int bytes_done; |
43000118 JA |
1186 | |
1187 | if (td->terminate) | |
1188 | break; | |
1189 | ||
2c83567e | 1190 | io_u = get_io_u(td); |
7889f07b JA |
1191 | if (!io_u) |
1192 | break; | |
43000118 | 1193 | |
57d753e3 | 1194 | memcpy(&s, &io_u->start_time, sizeof(s)); |
8baf1bcc | 1195 | |
2c83567e | 1196 | ret = io_u_queue(td, io_u); |
56b0eff0 | 1197 | if (ret) { |
a3fdb993 | 1198 | put_io_u(td, io_u); |
a592bd33 | 1199 | td->error = ret; |
43000118 JA |
1200 | break; |
1201 | } | |
1202 | ||
57d753e3 JA |
1203 | gettimeofday(&io_u->issue_time, NULL); |
1204 | add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time)); | |
2c83567e | 1205 | if (td->cur_depth < td->aio_depth) { |
43000118 JA |
1206 | timeout = &ts; |
1207 | min_evts = 0; | |
1208 | } else { | |
1209 | timeout = NULL; | |
1210 | min_evts = 1; | |
1211 | } | |
1212 | ||
2c83567e | 1213 | ret = io_getevents(td->aio_ctx, min_evts, td->cur_depth, td->aio_events, timeout); |
43000118 JA |
1214 | if (ret < 0) { |
1215 | td->error = errno; | |
1216 | break; | |
1217 | } else if (!ret) | |
1218 | continue; | |
1219 | ||
f0f3411b | 1220 | bytes_done = ios_completed(td, ret); |
43000118 | 1221 | |
98168d55 JA |
1222 | /* |
1223 | * the rate is batched for now, it should work for batches | |
1224 | * of completions except the very first one which may look | |
1225 | * a little bursty | |
1226 | */ | |
2c83567e | 1227 | gettimeofday(&e, NULL); |
43000118 JA |
1228 | usec = utime_since(&s, &e); |
1229 | ||
9e850933 | 1230 | rate_throttle(td, usec, bytes_done); |
43000118 JA |
1231 | |
1232 | if (check_min_rate(td, &e)) { | |
1233 | td->error = ENODATA; | |
1234 | break; | |
1235 | } | |
67903a2e JA |
1236 | |
1237 | if (runtime_exceeded(td, &e)) | |
1238 | break; | |
765d9223 JA |
1239 | |
1240 | if (td->thinktime) | |
1241 | usec_sleep(td->thinktime); | |
cdf92433 JA |
1242 | |
1243 | if (should_fsync(td) && td->fsync_blocks && | |
1244 | (td->io_blocks % td->fsync_blocks) == 0) | |
1245 | fsync(td->fd); | |
43000118 | 1246 | } |
56b0eff0 | 1247 | |
2c83567e JA |
1248 | if (td->cur_depth) |
1249 | cleanup_pending_aio(td); | |
4ac89145 JA |
1250 | |
1251 | if (should_fsync(td)) | |
1252 | fsync(td->fd); | |
56b0eff0 JA |
1253 | } |
1254 | ||
1255 | static void cleanup_aio(struct thread_data *td) | |
1256 | { | |
254605cd JA |
1257 | io_destroy(td->aio_ctx); |
1258 | ||
43000118 JA |
1259 | if (td->aio_events) |
1260 | free(td->aio_events); | |
43000118 JA |
1261 | } |
1262 | ||
1263 | static int init_aio(struct thread_data *td) | |
1264 | { | |
254605cd | 1265 | if (io_queue_init(td->aio_depth, &td->aio_ctx)) { |
43000118 JA |
1266 | td->error = errno; |
1267 | return 1; | |
1268 | } | |
1269 | ||
43000118 | 1270 | td->aio_events = malloc(td->aio_depth * sizeof(struct io_event)); |
43000118 JA |
1271 | return 0; |
1272 | } | |
1273 | ||
2c83567e JA |
1274 | static void cleanup_io_u(struct thread_data *td) |
1275 | { | |
1276 | struct list_head *entry, *n; | |
1277 | struct io_u *io_u; | |
1278 | ||
1279 | list_for_each_safe(entry, n, &td->io_u_freelist) { | |
1280 | io_u = list_entry(entry, struct io_u, list); | |
1281 | ||
1282 | list_del(&io_u->list); | |
2c83567e JA |
1283 | free(io_u); |
1284 | } | |
6b71c826 | 1285 | |
99c6704f JA |
1286 | if (td->mem_type == MEM_MALLOC) |
1287 | free(td->orig_buffer); | |
1288 | else if (td->mem_type == MEM_SHM) { | |
1289 | struct shmid_ds sbuf; | |
1290 | ||
1291 | shmdt(td->orig_buffer); | |
1292 | shmctl(td->shm_id, IPC_RMID, &sbuf); | |
1293 | } | |
2c83567e JA |
1294 | } |
1295 | ||
99c6704f | 1296 | static int init_io_u(struct thread_data *td) |
2c83567e JA |
1297 | { |
1298 | struct io_u *io_u; | |
99c6704f | 1299 | int i, max_units, mem_size; |
6b71c826 | 1300 | char *p; |
2c83567e JA |
1301 | |
1302 | if (!td->use_aio) | |
1303 | max_units = 1; | |
1304 | else | |
1305 | max_units = td->aio_depth; | |
1306 | ||
7889f07b | 1307 | mem_size = td->max_bs * max_units + MASK; |
99c6704f JA |
1308 | |
1309 | if (td->mem_type == MEM_MALLOC) | |
1310 | td->orig_buffer = malloc(mem_size); | |
1311 | else if (td->mem_type == MEM_SHM) { | |
1312 | td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600); | |
1313 | if (td->shm_id < 0) { | |
1314 | td->error = errno; | |
1315 | perror("shmget"); | |
1316 | return 1; | |
1317 | } | |
1318 | ||
1319 | td->orig_buffer = shmat(td->shm_id, NULL, 0); | |
1320 | if (td->orig_buffer == (void *) -1) { | |
1321 | td->error = errno; | |
1322 | perror("shmat"); | |
1323 | return 1; | |
1324 | } | |
1325 | } | |
6b71c826 | 1326 | |
2c83567e JA |
1327 | INIT_LIST_HEAD(&td->io_u_freelist); |
1328 | INIT_LIST_HEAD(&td->io_u_busylist); | |
645785e5 | 1329 | INIT_LIST_HEAD(&td->io_hist_list); |
2c83567e | 1330 | |
99c6704f | 1331 | p = ALIGN(td->orig_buffer); |
2c83567e JA |
1332 | for (i = 0; i < max_units; i++) { |
1333 | io_u = malloc(sizeof(*io_u)); | |
1334 | memset(io_u, 0, sizeof(*io_u)); | |
1335 | INIT_LIST_HEAD(&io_u->list); | |
1336 | ||
7889f07b | 1337 | io_u->buf = p + td->max_bs * i; |
2c83567e JA |
1338 | list_add(&io_u->list, &td->io_u_freelist); |
1339 | } | |
99c6704f JA |
1340 | |
1341 | return 0; | |
2c83567e JA |
1342 | } |
1343 | ||
a0a9b35b JA |
1344 | static void setup_log(struct io_log **log) |
1345 | { | |
1346 | struct io_log *l = malloc(sizeof(*l)); | |
1347 | ||
1348 | l->nr_samples = 0; | |
1349 | l->max_samples = 1024; | |
1350 | l->log = malloc(l->max_samples * sizeof(struct io_sample)); | |
1351 | *log = l; | |
1352 | } | |
1353 | ||
1354 | static void finish_log(struct thread_data *td, struct io_log *log, char *name) | |
1355 | { | |
1356 | char file_name[128]; | |
1357 | FILE *f; | |
645785e5 | 1358 | unsigned int i; |
a0a9b35b JA |
1359 | |
1360 | sprintf(file_name, "client%d_%s.log", td->thread_number, name); | |
1361 | f = fopen(file_name, "w"); | |
1362 | if (!f) { | |
1363 | perror("fopen log"); | |
1364 | return; | |
1365 | } | |
1366 | ||
1367 | for (i = 0; i < log->nr_samples; i++) | |
1368 | fprintf(f, "%lu, %lu\n", log->log[i].time, log->log[i].val); | |
1369 | ||
1370 | fclose(f); | |
1371 | free(log->log); | |
1372 | free(log); | |
1373 | } | |
1374 | ||
02983297 JA |
1375 | static int create_file(struct thread_data *td) |
1376 | { | |
7889f07b | 1377 | unsigned long long left; |
645785e5 | 1378 | unsigned int bs; |
02983297 | 1379 | char *b; |
645785e5 | 1380 | int r; |
02983297 | 1381 | |
02983297 JA |
1382 | /* |
1383 | * unless specifically asked for overwrite, let normal io extend it | |
1384 | */ | |
1385 | if (!td_read(td) && !td->overwrite) | |
1386 | return 0; | |
1387 | ||
57d753e3 JA |
1388 | if (!td->file_size) { |
1389 | fprintf(stderr, "Need size for create\n"); | |
1390 | td->error = EINVAL; | |
1391 | return 1; | |
1392 | } | |
1393 | ||
42fd89a7 JA |
1394 | printf("Client%d: Laying out IO file\n", td->thread_number); |
1395 | ||
02983297 JA |
1396 | td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
1397 | if (td->fd < 0) { | |
1398 | td->error = errno; | |
1399 | return 1; | |
1400 | } | |
1401 | ||
49d2caab | 1402 | td->io_size = td->file_size; |
7889f07b JA |
1403 | b = malloc(td->max_bs); |
1404 | memset(b, 0, td->max_bs); | |
1405 | ||
1406 | left = td->file_size; | |
1407 | while (left) { | |
1408 | bs = td->max_bs; | |
1409 | if (bs > left) | |
1410 | bs = left; | |
02983297 | 1411 | |
7889f07b | 1412 | r = write(td->fd, b, bs); |
02983297 | 1413 | |
645785e5 | 1414 | if (r == (int) bs) { |
7889f07b | 1415 | left -= bs; |
02983297 | 1416 | continue; |
7889f07b | 1417 | } else { |
02983297 JA |
1418 | if (r < 0) |
1419 | td->error = errno; | |
1420 | else | |
1421 | td->error = EIO; | |
1422 | ||
1423 | break; | |
1424 | } | |
1425 | } | |
1426 | ||
fc097bfe JA |
1427 | if (td->create_fsync) |
1428 | fsync(td->fd); | |
1429 | ||
02983297 JA |
1430 | close(td->fd); |
1431 | td->fd = -1; | |
1432 | free(b); | |
1433 | return 0; | |
1434 | } | |
1435 | ||
1436 | static int file_exists(struct thread_data *td) | |
1437 | { | |
1438 | struct stat st; | |
1439 | ||
1440 | if (stat(td->file_name, &st) != -1) | |
1441 | return 1; | |
1442 | ||
1443 | return errno != ENOENT; | |
1444 | } | |
1445 | ||
1446 | static int setup_file(struct thread_data *td) | |
1447 | { | |
1448 | struct stat st; | |
1449 | int flags = 0; | |
1450 | ||
1451 | if (!file_exists(td)) { | |
1452 | if (!td->create_file) { | |
1453 | td->error = ENOENT; | |
1454 | return 1; | |
1455 | } | |
1456 | if (create_file(td)) | |
1457 | return 1; | |
1458 | } | |
1459 | ||
1460 | if (td->odirect) | |
1461 | flags |= O_DIRECT; | |
1462 | ||
1463 | if (td_read(td)) | |
1464 | td->fd = open(td->file_name, flags | O_RDONLY); | |
1465 | else { | |
1466 | if (!td->overwrite) | |
1467 | flags |= O_TRUNC; | |
74b4b5fb JA |
1468 | if (td->sync_io) |
1469 | flags |= O_SYNC; | |
e8457004 JA |
1470 | if (td->verify) |
1471 | flags |= O_RDWR; | |
1472 | else | |
1473 | flags |= O_WRONLY; | |
02983297 | 1474 | |
e8457004 | 1475 | td->fd = open(td->file_name, flags | O_CREAT, 0600); |
02983297 JA |
1476 | } |
1477 | ||
1478 | if (td->fd == -1) { | |
1479 | td->error = errno; | |
1480 | return 1; | |
1481 | } | |
1482 | ||
1483 | if (td_read(td)) { | |
1484 | if (fstat(td->fd, &st) == -1) { | |
1485 | td->error = errno; | |
1486 | return 1; | |
1487 | } | |
1488 | ||
1489 | if (td->file_size > st.st_size) | |
1490 | st.st_size = td->file_size; | |
1491 | } else { | |
1492 | if (!td->file_size) | |
1493 | td->file_size = 1024 * 1024 * 1024; | |
1494 | ||
1495 | st.st_size = td->file_size; | |
1496 | } | |
1497 | ||
49d2caab JA |
1498 | if (td->file_offset > st.st_size) { |
1499 | fprintf(stderr, "Client%d: offset larger than length\n", td->thread_number); | |
1500 | return 1; | |
1501 | } | |
1502 | ||
1503 | td->io_size = st.st_size - td->file_offset; | |
1504 | if (td->io_size == 0) { | |
02983297 JA |
1505 | fprintf(stderr, "Client%d: no io blocks\n", td->thread_number); |
1506 | td->error = EINVAL; | |
1507 | return 1; | |
1508 | } | |
1509 | ||
b95799ca JA |
1510 | if (td->invalidate_cache) { |
1511 | if (fadvise(td->fd, 0, st.st_size, POSIX_FADV_DONTNEED) < 0) { | |
1512 | td->error = errno; | |
1513 | return 1; | |
1514 | } | |
1515 | } | |
1516 | ||
02983297 JA |
1517 | return 0; |
1518 | } | |
1519 | ||
d32d9284 JA |
1520 | static void clear_io_state(struct thread_data *td) |
1521 | { | |
9d0c6ca2 JA |
1522 | if (!td->use_aio) |
1523 | lseek(td->fd, SEEK_SET, 0); | |
1524 | ||
d32d9284 | 1525 | td->cur_off = 0; |
49d2caab JA |
1526 | td->last_bytes = 0; |
1527 | td->stat_io_bytes = 0; | |
1528 | td->this_io_bytes = 0; | |
1529 | ||
1530 | if (td->file_map) | |
1531 | memset(td->file_map, 0, td->num_maps * sizeof(long)); | |
d32d9284 JA |
1532 | } |
1533 | ||
5c24b2c4 | 1534 | static void *thread_main(int shm_id, int offset, char *argv[]) |
892199bd JA |
1535 | { |
1536 | struct thread_data *td; | |
02983297 | 1537 | int ret = 1; |
2c83567e | 1538 | void *data; |
892199bd | 1539 | |
7292613b JA |
1540 | setsid(); |
1541 | ||
892199bd | 1542 | data = shmat(shm_id, NULL, 0); |
4ac89145 JA |
1543 | if (data == (void *) -1) { |
1544 | perror("shmat"); | |
1545 | return NULL; | |
1546 | } | |
1547 | ||
892199bd JA |
1548 | td = data + offset * sizeof(struct thread_data); |
1549 | td->pid = getpid(); | |
1550 | ||
99c6704f JA |
1551 | if (init_io_u(td)) |
1552 | goto err; | |
2c83567e | 1553 | |
18e0b78c JA |
1554 | if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) { |
1555 | td->error = errno; | |
1556 | goto err; | |
1557 | } | |
1558 | ||
4240cfa1 | 1559 | sprintf(argv[0], "fio%d", offset); |
892199bd | 1560 | |
43000118 JA |
1561 | if (td->use_aio && init_aio(td)) |
1562 | goto err; | |
1563 | ||
f737299d | 1564 | if (td->ioprio) { |
892199bd JA |
1565 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
1566 | td->error = errno; | |
599002b3 | 1567 | goto err; |
892199bd JA |
1568 | } |
1569 | } | |
1570 | ||
1571 | sem_post(&startup_sem); | |
1572 | sem_wait(&td->mutex); | |
43000118 | 1573 | |
fc097bfe JA |
1574 | if (!td->create_serialize && setup_file(td)) |
1575 | goto err; | |
1576 | ||
49d2caab JA |
1577 | if (init_random_state(td)) |
1578 | goto err; | |
1579 | ||
7292613b JA |
1580 | gettimeofday(&td->start, NULL); |
1581 | ||
293753bb JA |
1582 | while (td->loops--) { |
1583 | gettimeofday(&td->stat_sample_time, NULL); | |
1584 | ||
1585 | if (td->ratemin) | |
1586 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); | |
7292613b | 1587 | |
d32d9284 | 1588 | clear_io_state(td); |
9d0c6ca2 | 1589 | prune_io_piece_log(td); |
fd1ae4c9 | 1590 | |
b2de0ed2 | 1591 | if (!td->use_aio) |
b6794fbf | 1592 | do_sync_io(td); |
b2de0ed2 JA |
1593 | else |
1594 | do_async_io(td); | |
1595 | ||
91fc5dc9 JA |
1596 | if (td->error) |
1597 | break; | |
1598 | ||
b2de0ed2 JA |
1599 | if (!td->verify) |
1600 | continue; | |
cfc702bd | 1601 | |
b2de0ed2 | 1602 | clear_io_state(td); |
d32d9284 | 1603 | |
91fc5dc9 JA |
1604 | if (!td->use_aio) |
1605 | do_sync_verify(td); | |
1606 | else | |
1607 | do_async_verify(td); | |
1608 | ||
1609 | if (td->error) | |
1610 | break; | |
b6794fbf | 1611 | } |
7292613b | 1612 | |
be33abe4 | 1613 | td->runtime = mtime_since_now(&td->start); |
892199bd | 1614 | ret = 0; |
a0a9b35b JA |
1615 | |
1616 | if (td->bw_log) | |
1617 | finish_log(td, td->bw_log, "bw"); | |
1618 | if (td->lat_log) | |
1619 | finish_log(td, td->lat_log, "lat"); | |
4ac89145 | 1620 | |
98dd52d6 JA |
1621 | if (exitall_on_terminate) |
1622 | sig_handler(0); | |
1623 | ||
892199bd | 1624 | err: |
7292613b JA |
1625 | if (td->fd != -1) { |
1626 | close(td->fd); | |
1627 | td->fd = -1; | |
1628 | } | |
4ac89145 JA |
1629 | if (td->use_aio) |
1630 | cleanup_aio(td); | |
2c83567e | 1631 | cleanup_io_u(td); |
599002b3 | 1632 | if (ret) { |
892199bd | 1633 | sem_post(&startup_sem); |
599002b3 JA |
1634 | sem_wait(&td->mutex); |
1635 | } | |
40ef7f64 | 1636 | td_set_runstate(td, TD_EXITED); |
4240cfa1 | 1637 | shmdt(data); |
892199bd JA |
1638 | return NULL; |
1639 | } | |
1640 | ||
5c24b2c4 | 1641 | static void free_shm(void) |
892199bd | 1642 | { |
c269123b JA |
1643 | struct shmid_ds sbuf; |
1644 | ||
1645 | if (threads) { | |
1646 | shmdt(threads); | |
1647 | threads = NULL; | |
1648 | shmctl(shm_id, IPC_RMID, &sbuf); | |
1649 | } | |
892199bd JA |
1650 | } |
1651 | ||
57d753e3 JA |
1652 | static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max, |
1653 | double *mean, double *dev) | |
1654 | { | |
1655 | double n; | |
1656 | ||
1657 | if (is->samples == 0) | |
1658 | return 0; | |
1659 | ||
1660 | *min = is->min_val; | |
1661 | *max = is->max_val; | |
1662 | ||
1663 | n = (double) is->samples; | |
1664 | *mean = (double) is->val / n; | |
1665 | *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1)); | |
1666 | return 1; | |
1667 | } | |
1668 | ||
5c24b2c4 | 1669 | static void show_thread_status(struct thread_data *td) |
892199bd JA |
1670 | { |
1671 | int prio, prio_class; | |
57d753e3 JA |
1672 | unsigned long min, max, bw = 0; |
1673 | double mean, dev; | |
892199bd | 1674 | |
49d2caab | 1675 | if (!td->io_bytes && !td->error) |
213b446c JA |
1676 | return; |
1677 | ||
892199bd | 1678 | if (td->runtime) |
49d2caab | 1679 | bw = td->io_bytes / td->runtime; |
892199bd JA |
1680 | |
1681 | prio = td->ioprio & 0xff; | |
1682 | prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT; | |
1683 | ||
49d2caab | 1684 | printf("Client%d: err=%2d, io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", td->thread_number, td->error, td->io_bytes >> 20, bw, td->runtime); |
fd1ae4c9 | 1685 | |
57d753e3 JA |
1686 | if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev)) |
1687 | printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
1688 | if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev)) | |
1689 | printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
1690 | if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) | |
1691 | printf(" bw (KiB/s) : min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
892199bd JA |
1692 | } |
1693 | ||
5c24b2c4 | 1694 | static int setup_rate(struct thread_data *td) |
86184d14 | 1695 | { |
4240cfa1 JA |
1696 | int nr_reads_per_sec; |
1697 | ||
1698 | if (!td->rate) | |
1699 | return 0; | |
1700 | ||
1701 | if (td->rate < td->ratemin) { | |
1702 | fprintf(stderr, "min rate larger than nominal rate\n"); | |
1703 | return -1; | |
1704 | } | |
86184d14 | 1705 | |
49d2caab | 1706 | nr_reads_per_sec = (td->rate * 1024) / td->min_bs; |
86184d14 JA |
1707 | td->rate_usec_cycle = 1000000 / nr_reads_per_sec; |
1708 | td->rate_pending_usleep = 0; | |
4240cfa1 | 1709 | return 0; |
86184d14 JA |
1710 | } |
1711 | ||
47d45203 | 1712 | static struct thread_data *get_new_job(int global) |
892199bd | 1713 | { |
4240cfa1 JA |
1714 | struct thread_data *td; |
1715 | ||
47d45203 JA |
1716 | if (global) |
1717 | return &def_thread; | |
8867c0a8 | 1718 | if (thread_number >= max_jobs) |
4240cfa1 JA |
1719 | return NULL; |
1720 | ||
1721 | td = &threads[thread_number++]; | |
fc24389f | 1722 | memset(td, 0, sizeof(*td)); |
892199bd | 1723 | |
e4ed35c3 | 1724 | td->fd = -1; |
86184d14 | 1725 | td->thread_number = thread_number; |
76cb7b42 | 1726 | |
47d45203 | 1727 | td->ddir = def_thread.ddir; |
76cb7b42 JA |
1728 | td->ioprio = def_thread.ioprio; |
1729 | td->sequential = def_thread.sequential; | |
47d45203 | 1730 | td->bs = def_thread.bs; |
8c033f93 JA |
1731 | td->min_bs = def_thread.min_bs; |
1732 | td->max_bs = def_thread.max_bs; | |
47d45203 | 1733 | td->odirect = def_thread.odirect; |
e97712ed | 1734 | td->thinktime = def_thread.thinktime; |
76cb7b42 JA |
1735 | td->fsync_blocks = def_thread.fsync_blocks; |
1736 | td->start_delay = def_thread.start_delay; | |
67903a2e | 1737 | td->timeout = def_thread.timeout; |
76cb7b42 | 1738 | td->use_aio = def_thread.use_aio; |
02983297 JA |
1739 | td->create_file = def_thread.create_file; |
1740 | td->overwrite = def_thread.overwrite; | |
b95799ca | 1741 | td->invalidate_cache = def_thread.invalidate_cache; |
76cb7b42 | 1742 | td->file_size = def_thread.file_size; |
9b5cf6c0 | 1743 | td->file_offset = def_thread.file_offset; |
76cb7b42 JA |
1744 | td->rate = def_thread.rate; |
1745 | td->ratemin = def_thread.ratemin; | |
1746 | td->ratecycle = def_thread.ratecycle; | |
1747 | td->aio_depth = def_thread.aio_depth; | |
99c6704f JA |
1748 | td->sync_io = def_thread.sync_io; |
1749 | td->mem_type = def_thread.mem_type; | |
1d035750 | 1750 | td->bw_avg_time = def_thread.bw_avg_time; |
fc097bfe JA |
1751 | td->create_serialize = def_thread.create_serialize; |
1752 | td->create_fsync = def_thread.create_fsync; | |
b6794fbf | 1753 | td->loops = def_thread.loops; |
cfc702bd | 1754 | td->verify = def_thread.verify; |
47d45203 | 1755 | memcpy(&td->cpumask, &def_thread.cpumask, sizeof(td->cpumask)); |
f737299d JA |
1756 | |
1757 | return td; | |
1758 | } | |
1759 | ||
4240cfa1 JA |
1760 | static void put_job(struct thread_data *td) |
1761 | { | |
1762 | memset(&threads[td->thread_number - 1], 0, sizeof(*td)); | |
1763 | thread_number--; | |
1764 | } | |
1765 | ||
5c24b2c4 JA |
1766 | static int add_job(struct thread_data *td, const char *filename, int prioclass, |
1767 | int prio) | |
f737299d | 1768 | { |
47d45203 JA |
1769 | if (td == &def_thread) |
1770 | return 0; | |
1771 | ||
f737299d | 1772 | strcpy(td->file_name, filename); |
4240cfa1 | 1773 | sem_init(&td->mutex, 1, 0); |
f737299d JA |
1774 | td->ioprio = (prioclass << IOPRIO_CLASS_SHIFT) | prio; |
1775 | ||
57d753e3 JA |
1776 | td->clat_stat.min_val = ULONG_MAX; |
1777 | td->slat_stat.min_val = ULONG_MAX; | |
1778 | td->bw_stat.min_val = ULONG_MAX; | |
1779 | ||
8dbff0b1 JA |
1780 | run_str[td->thread_number - 1] = 'P'; |
1781 | ||
4ac89145 JA |
1782 | if (td->use_aio && !td->aio_depth) |
1783 | td->aio_depth = 1; | |
43000118 | 1784 | |
645785e5 | 1785 | if (td->min_bs == -1U) |
8bdcfab5 | 1786 | td->min_bs = td->bs; |
645785e5 | 1787 | if (td->max_bs == -1U) |
8bdcfab5 | 1788 | td->max_bs = td->bs; |
645785e5 | 1789 | if (td_read(td)) |
840b216f | 1790 | td->verify = 0; |
8bdcfab5 | 1791 | |
4240cfa1 JA |
1792 | if (setup_rate(td)) |
1793 | return -1; | |
f737299d | 1794 | |
a0a9b35b JA |
1795 | if (write_lat_log) |
1796 | setup_log(&td->lat_log); | |
1797 | if (write_bw_log) | |
1798 | setup_log(&td->bw_log); | |
1799 | ||
7889f07b | 1800 | printf("Client%d: file=%s, rw=%d, prio=%d/%d, seq=%d, odir=%d, bs=%d-%d, rate=%d, aio=%d, aio_depth=%d\n", td->thread_number, filename, td->ddir, prioclass, prio, td->sequential, td->odirect, td->min_bs, td->max_bs, td->rate, td->use_aio, td->aio_depth); |
4240cfa1 | 1801 | return 0; |
892199bd JA |
1802 | } |
1803 | ||
18e0b78c JA |
1804 | static void fill_cpu_mask(cpu_set_t cpumask, int cpu) |
1805 | { | |
f737299d | 1806 | unsigned int i; |
18e0b78c JA |
1807 | |
1808 | CPU_ZERO(&cpumask); | |
1809 | ||
1810 | for (i = 0; i < sizeof(int) * 8; i++) { | |
1811 | if ((1 << i) & cpu) | |
1812 | CPU_SET(i, &cpumask); | |
1813 | } | |
1814 | } | |
1815 | ||
7889f07b JA |
1816 | unsigned long get_mult(char c) |
1817 | { | |
1818 | switch (c) { | |
1819 | case 'k': | |
1820 | case 'K': | |
1821 | return 1024; | |
1822 | case 'm': | |
1823 | case 'M': | |
1824 | return 1024 * 1024; | |
1825 | case 'g': | |
1826 | case 'G': | |
1827 | return 1024 * 1024 * 1024; | |
1828 | default: | |
1829 | return 1; | |
1830 | } | |
1831 | } | |
1832 | ||
02983297 JA |
1833 | /* |
1834 | * convert string after '=' into decimal value, noting any size suffix | |
1835 | */ | |
1836 | static int str_cnv(char *p, unsigned long long *val) | |
1837 | { | |
02983297 JA |
1838 | char *str; |
1839 | int len; | |
1840 | ||
1841 | str = strstr(p, "="); | |
1842 | if (!str) | |
1843 | return 1; | |
1844 | ||
1845 | str++; | |
1846 | len = strlen(str); | |
02983297 JA |
1847 | |
1848 | *val = strtoul(str, NULL, 10); | |
1849 | if (*val == ULONG_MAX && errno == ERANGE) | |
1850 | return 1; | |
1851 | ||
7889f07b | 1852 | *val *= get_mult(str[len - 2]); |
02983297 | 1853 | return 0; |
02983297 JA |
1854 | } |
1855 | ||
02983297 JA |
1856 | static int check_strcnv(char *p, char *name, unsigned long long *val) |
1857 | { | |
1858 | if (!strstr(p, name)) | |
1859 | return 1; | |
1860 | ||
1861 | return str_cnv(p, val); | |
1862 | } | |
1863 | ||
99c6704f JA |
1864 | static int check_str(char *p, char *name, char *option) |
1865 | { | |
1866 | char *s = strstr(p, name); | |
1867 | ||
1868 | if (!s) | |
1869 | return 1; | |
1870 | ||
1871 | s += strlen(name); | |
1872 | if (strstr(s, option)) | |
1873 | return 0; | |
1874 | ||
1875 | return 1; | |
1876 | } | |
1877 | ||
7889f07b JA |
1878 | static int check_range(char *p, char *name, unsigned long *s, unsigned long *e) |
1879 | { | |
1880 | char str[128]; | |
1881 | char s1, s2; | |
1882 | ||
1883 | sprintf(str, "%s=%%lu%%c-%%lu%%c", name); | |
1884 | if (sscanf(p, str, s, &s1, e, &s2) == 4) { | |
1885 | *s *= get_mult(s1); | |
1886 | *e *= get_mult(s2); | |
1887 | return 0; | |
1888 | } | |
1889 | ||
1890 | sprintf(str, "%s = %%lu%%c-%%lu%%c", name); | |
1891 | if (sscanf(p, str, s, &s1, e, &s2) == 4) { | |
1892 | *s *= get_mult(s1); | |
1893 | *e *= get_mult(s2); | |
1894 | return 0; | |
1895 | } | |
1896 | ||
1897 | sprintf(str, "%s=%%lu-%%lu", name); | |
1898 | if (sscanf(p, str, s, e) == 2) | |
1899 | return 0; | |
1900 | ||
1901 | sprintf(str, "%s = %%lu-%%lu", name); | |
1902 | if (sscanf(p, str, s, e) == 2) | |
1903 | return 0; | |
1904 | ||
1905 | return 1; | |
1906 | ||
1907 | } | |
1908 | ||
5c24b2c4 | 1909 | static int check_int(char *p, char *name, unsigned int *val) |
7dd1389e JA |
1910 | { |
1911 | char str[128]; | |
1912 | ||
1913 | sprintf(str, "%s=%%d", name); | |
1914 | if (sscanf(p, str, val) == 1) | |
1915 | return 0; | |
1916 | ||
1917 | sprintf(str, "%s = %%d", name); | |
1918 | if (sscanf(p, str, val) == 1) | |
1919 | return 0; | |
1920 | ||
1921 | return 1; | |
1922 | } | |
1923 | ||
7292613b | 1924 | static int is_empty_or_comment(char *line) |
7dd1389e JA |
1925 | { |
1926 | unsigned int i; | |
1927 | ||
7292613b | 1928 | for (i = 0; i < strlen(line); i++) { |
7292613b | 1929 | if (line[i] == ';') |
47d45203 JA |
1930 | return 1; |
1931 | if (!isspace(line[i]) && !iscntrl(line[i])) | |
7292613b JA |
1932 | return 0; |
1933 | } | |
7dd1389e JA |
1934 | |
1935 | return 1; | |
1936 | } | |
1937 | ||
5c24b2c4 | 1938 | static int parse_jobs_ini(char *file) |
7dd1389e | 1939 | { |
47d45203 | 1940 | unsigned int prioclass, prio, cpu, global; |
8c033f93 | 1941 | unsigned long long ull; |
7889f07b | 1942 | unsigned long ul1, ul2; |
f737299d | 1943 | struct thread_data *td; |
7dd1389e JA |
1944 | char *string, *name; |
1945 | fpos_t off; | |
1946 | FILE *f; | |
1947 | char *p; | |
1948 | ||
1949 | f = fopen(file, "r"); | |
1950 | if (!f) { | |
1951 | perror("fopen"); | |
4240cfa1 | 1952 | return 1; |
7dd1389e JA |
1953 | } |
1954 | ||
1955 | string = malloc(4096); | |
1956 | name = malloc(256); | |
1957 | ||
7dd1389e | 1958 | while ((p = fgets(string, 4096, f)) != NULL) { |
7292613b JA |
1959 | if (is_empty_or_comment(p)) |
1960 | continue; | |
7dd1389e JA |
1961 | if (sscanf(p, "[%s]", name) != 1) |
1962 | continue; | |
1963 | ||
47d45203 JA |
1964 | global = !strncmp(name, "global", 6); |
1965 | ||
7dd1389e JA |
1966 | name[strlen(name) - 1] = '\0'; |
1967 | ||
47d45203 | 1968 | td = get_new_job(global); |
4240cfa1 JA |
1969 | if (!td) |
1970 | break; | |
f737299d | 1971 | |
7dd1389e | 1972 | prioclass = 2; |
f737299d | 1973 | prio = 4; |
7dd1389e JA |
1974 | |
1975 | fgetpos(f, &off); | |
1976 | while ((p = fgets(string, 4096, f)) != NULL) { | |
7292613b | 1977 | if (is_empty_or_comment(p)) |
e6402082 JA |
1978 | continue; |
1979 | if (strstr(p, "[")) | |
7dd1389e | 1980 | break; |
f737299d | 1981 | if (!check_int(p, "rw", &td->ddir)) { |
7dd1389e JA |
1982 | fgetpos(f, &off); |
1983 | continue; | |
1984 | } | |
1985 | if (!check_int(p, "prio", &prio)) { | |
1986 | fgetpos(f, &off); | |
1987 | continue; | |
1988 | } | |
1989 | if (!check_int(p, "prioclass", &prioclass)) { | |
1990 | fgetpos(f, &off); | |
1991 | continue; | |
1992 | } | |
f737299d | 1993 | if (!check_int(p, "direct", &td->odirect)) { |
7dd1389e JA |
1994 | fgetpos(f, &off); |
1995 | continue; | |
1996 | } | |
f737299d | 1997 | if (!check_int(p, "rate", &td->rate)) { |
7dd1389e JA |
1998 | fgetpos(f, &off); |
1999 | continue; | |
2000 | } | |
4240cfa1 JA |
2001 | if (!check_int(p, "ratemin", &td->ratemin)) { |
2002 | fgetpos(f, &off); | |
2003 | continue; | |
2004 | } | |
2005 | if (!check_int(p, "ratecycle", &td->ratecycle)) { | |
2006 | fgetpos(f, &off); | |
2007 | continue; | |
2008 | } | |
e97712ed | 2009 | if (!check_int(p, "thinktime", &td->thinktime)) { |
7dd1389e JA |
2010 | fgetpos(f, &off); |
2011 | continue; | |
2012 | } | |
18e0b78c | 2013 | if (!check_int(p, "cpumask", &cpu)) { |
f737299d | 2014 | fill_cpu_mask(td->cpumask, cpu); |
18e0b78c JA |
2015 | fgetpos(f, &off); |
2016 | continue; | |
2017 | } | |
4240cfa1 JA |
2018 | if (!check_int(p, "fsync", &td->fsync_blocks)) { |
2019 | fgetpos(f, &off); | |
2020 | continue; | |
2021 | } | |
fc24389f JA |
2022 | if (!check_int(p, "startdelay", &td->start_delay)) { |
2023 | fgetpos(f, &off); | |
2024 | continue; | |
2025 | } | |
67903a2e JA |
2026 | if (!check_int(p, "timeout", &td->timeout)) { |
2027 | fgetpos(f, &off); | |
2028 | continue; | |
2029 | } | |
b95799ca JA |
2030 | if (!check_int(p, "invalidate",&td->invalidate_cache)) { |
2031 | fgetpos(f, &off); | |
2032 | continue; | |
2033 | } | |
43000118 JA |
2034 | if (!check_int(p, "aio_depth", &td->aio_depth)) { |
2035 | fgetpos(f, &off); | |
2036 | continue; | |
2037 | } | |
74b4b5fb JA |
2038 | if (!check_int(p, "sync", &td->sync_io)) { |
2039 | fgetpos(f, &off); | |
2040 | continue; | |
2041 | } | |
1d035750 JA |
2042 | if (!check_int(p, "bwavgtime", &td->bw_avg_time)) { |
2043 | fgetpos(f, &off); | |
2044 | continue; | |
2045 | } | |
fc097bfe JA |
2046 | if (!check_int(p, "create_serialize", &td->create_serialize)) { |
2047 | fgetpos(f, &off); | |
2048 | continue; | |
2049 | } | |
2050 | if (!check_int(p, "create_fsync", &td->create_fsync)) { | |
2051 | fgetpos(f, &off); | |
2052 | continue; | |
2053 | } | |
b6794fbf JA |
2054 | if (!check_int(p, "loops", &td->loops)) { |
2055 | fgetpos(f, &off); | |
2056 | continue; | |
2057 | } | |
e8457004 JA |
2058 | if (!check_int(p, "verify", &td->verify)) { |
2059 | fgetpos(f, &off); | |
2060 | continue; | |
2061 | } | |
7889f07b | 2062 | if (!check_range(p, "bsrange", &ul1, &ul2)) { |
7f27790b JA |
2063 | if (ul1 & 511) |
2064 | printf("bad min block size, must be a multiple of 512\n"); | |
2065 | else | |
2066 | td->min_bs = ul1; | |
2067 | if (ul2 & 511) | |
2068 | printf("bad max block size, must be a multiple of 512\n"); | |
2069 | else | |
2070 | td->max_bs = ul2; | |
7889f07b JA |
2071 | fgetpos(f, &off); |
2072 | continue; | |
2073 | } | |
8c033f93 | 2074 | if (!check_strcnv(p, "bs", &ull)) { |
7f27790b JA |
2075 | if (ull & 511) |
2076 | printf("bad block size, must be a multiple of 512\n"); | |
2077 | else | |
2078 | td->bs = ull; | |
8c033f93 JA |
2079 | fgetpos(f, &off); |
2080 | continue; | |
2081 | } | |
02983297 JA |
2082 | if (!check_strcnv(p, "size", &td->file_size)) { |
2083 | fgetpos(f, &off); | |
2084 | continue; | |
2085 | } | |
2086 | if (!check_strcnv(p, "offset", &td->file_offset)) { | |
2087 | fgetpos(f, &off); | |
2088 | continue; | |
2089 | } | |
99c6704f | 2090 | if (!check_str(p, "mem", "malloc")) { |
99c6704f JA |
2091 | td->mem_type = MEM_MALLOC; |
2092 | fgetpos(f, &off); | |
2093 | continue; | |
2094 | } | |
2095 | if (!check_str(p, "mem", "shm")) { | |
99c6704f JA |
2096 | td->mem_type = MEM_SHM; |
2097 | fgetpos(f, &off); | |
2098 | continue; | |
2099 | } | |
43000118 | 2100 | if (!strncmp(p, "sequential", 10)) { |
f737299d | 2101 | td->sequential = 1; |
7dd1389e JA |
2102 | fgetpos(f, &off); |
2103 | continue; | |
2104 | } | |
43000118 | 2105 | if (!strncmp(p, "random", 6)) { |
f737299d | 2106 | td->sequential = 0; |
7dd1389e JA |
2107 | fgetpos(f, &off); |
2108 | continue; | |
2109 | } | |
43000118 JA |
2110 | if (!strncmp(p, "aio", 3)) { |
2111 | td->use_aio = 1; | |
2112 | fgetpos(f, &off); | |
2113 | continue; | |
2114 | } | |
02983297 JA |
2115 | if (!strncmp(p, "create", 6)) { |
2116 | td->create_file = 1; | |
2117 | fgetpos(f, &off); | |
2118 | continue; | |
2119 | } | |
2120 | if (!strncmp(p, "overwrite", 9)) { | |
2121 | td->overwrite = 1; | |
2122 | fgetpos(f, &off); | |
2123 | continue; | |
2124 | } | |
98dd52d6 JA |
2125 | if (!strncmp(p, "exitall", 7)) { |
2126 | exitall_on_terminate = 1; | |
2127 | fgetpos(f, &off); | |
2128 | continue; | |
2129 | } | |
e6402082 | 2130 | printf("Client%d: bad option %s\n",td->thread_number,p); |
7dd1389e JA |
2131 | } |
2132 | fsetpos(f, &off); | |
2133 | ||
4240cfa1 JA |
2134 | if (add_job(td, name, prioclass, prio)) |
2135 | put_job(td); | |
7dd1389e JA |
2136 | } |
2137 | ||
2138 | free(string); | |
2139 | free(name); | |
fc7d63df | 2140 | fclose(f); |
4240cfa1 | 2141 | return 0; |
7dd1389e JA |
2142 | } |
2143 | ||
5c24b2c4 | 2144 | static int parse_options(int argc, char *argv[]) |
892199bd | 2145 | { |
01c4d8de | 2146 | int i; |
892199bd JA |
2147 | |
2148 | for (i = 1; i < argc; i++) { | |
2149 | char *parm = argv[i]; | |
2150 | ||
2151 | if (parm[0] != '-') | |
2152 | break; | |
2153 | ||
2154 | parm++; | |
2155 | switch (*parm) { | |
2156 | case 's': | |
2157 | parm++; | |
47d45203 | 2158 | def_thread.sequential = !!atoi(parm); |
892199bd JA |
2159 | break; |
2160 | case 'b': | |
2161 | parm++; | |
47d45203 JA |
2162 | def_thread.bs = atoi(parm); |
2163 | def_thread.bs <<= 10; | |
2164 | if (!def_thread.bs) { | |
4240cfa1 | 2165 | printf("bad block size\n"); |
47d45203 | 2166 | def_thread.bs = DEF_BS; |
4240cfa1 | 2167 | } |
892199bd JA |
2168 | break; |
2169 | case 't': | |
2170 | parm++; | |
47d45203 | 2171 | def_thread.timeout = atoi(parm); |
892199bd | 2172 | break; |
892199bd JA |
2173 | case 'r': |
2174 | parm++; | |
2175 | repeatable = !!atoi(parm); | |
2176 | break; | |
02bdd9ba JA |
2177 | case 'R': |
2178 | parm++; | |
2179 | rate_quit = !!atoi(parm); | |
2180 | break; | |
892199bd JA |
2181 | case 'o': |
2182 | parm++; | |
47d45203 | 2183 | def_thread.odirect = !!atoi(parm); |
892199bd | 2184 | break; |
7dd1389e JA |
2185 | case 'f': |
2186 | if (i + 1 >= argc) { | |
2187 | printf("-f needs file as arg\n"); | |
2188 | break; | |
2189 | } | |
2190 | ini_file = strdup(argv[i+1]); | |
a642279f | 2191 | i++; |
7dd1389e | 2192 | break; |
a0a9b35b JA |
2193 | case 'l': |
2194 | write_lat_log = 1; | |
2195 | break; | |
2196 | case 'w': | |
2197 | write_bw_log = 1; | |
2198 | break; | |
892199bd | 2199 | default: |
7dd1389e | 2200 | printf("bad option %s\n", argv[i]); |
892199bd JA |
2201 | break; |
2202 | } | |
2203 | } | |
2204 | ||
892199bd JA |
2205 | return i; |
2206 | } | |
2207 | ||
3f39453a | 2208 | static void print_thread_status(struct thread_data *td, int nr_running, |
8dbff0b1 | 2209 | int t_rate, int m_rate) |
3f39453a | 2210 | { |
3f39453a JA |
2211 | printf("Threads now running: %d", nr_running); |
2212 | if (m_rate || t_rate) | |
2213 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); | |
8dbff0b1 JA |
2214 | printf(" : [%s]\r", run_str); |
2215 | fflush(stdout); | |
3f39453a JA |
2216 | } |
2217 | ||
40ef7f64 JA |
2218 | static void check_str_update(struct thread_data *td, int n, int t, int m) |
2219 | { | |
2220 | char c = run_str[td->thread_number - 1]; | |
2221 | ||
2222 | if (td->runstate == td->old_runstate) | |
2223 | return; | |
2224 | ||
2225 | switch (td->runstate) { | |
2226 | case TD_REAPED: | |
2227 | c = '_'; | |
2228 | break; | |
f4bb2243 JA |
2229 | case TD_EXITED: |
2230 | c = 'E'; | |
2231 | break; | |
40ef7f64 JA |
2232 | case TD_RUNNING: |
2233 | c = '+'; | |
2234 | break; | |
2235 | case TD_VERIFYING: | |
2236 | c = 'V'; | |
2237 | break; | |
2238 | case TD_CREATED: | |
2239 | c = 'C'; | |
2240 | break; | |
2241 | case TD_NOT_CREATED: | |
2242 | c = 'P'; | |
2243 | break; | |
2244 | default: | |
2245 | printf("state %d\n", td->runstate); | |
2246 | } | |
2247 | ||
2248 | run_str[td->thread_number - 1] = c; | |
2249 | print_thread_status(td, n, t, m); | |
2250 | td->old_runstate = td->runstate; | |
2251 | } | |
2252 | ||
213b446c | 2253 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
02bdd9ba | 2254 | { |
213b446c | 2255 | int i; |
02bdd9ba | 2256 | |
3f39453a JA |
2257 | /* |
2258 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
2259 | */ | |
02bdd9ba JA |
2260 | for (i = 0; i < thread_number; i++) { |
2261 | struct thread_data *td = &threads[i]; | |
2262 | ||
40ef7f64 JA |
2263 | check_str_update(td, *nr_running, *t_rate, *m_rate); |
2264 | ||
213b446c JA |
2265 | if (td->runstate != TD_EXITED) |
2266 | continue; | |
02bdd9ba | 2267 | |
40ef7f64 | 2268 | td_set_runstate(td, TD_REAPED); |
213b446c JA |
2269 | waitpid(td->pid, NULL, 0); |
2270 | (*nr_running)--; | |
2271 | (*m_rate) -= td->ratemin; | |
2272 | (*t_rate) -= td->rate; | |
40ef7f64 | 2273 | check_str_update(td, *nr_running, *t_rate, *m_rate); |
e6402082 JA |
2274 | |
2275 | if (td->terminate) | |
2276 | continue; | |
213b446c | 2277 | } |
02bdd9ba JA |
2278 | } |
2279 | ||
fc24389f JA |
2280 | static void run_threads(char *argv[]) |
2281 | { | |
be33abe4 | 2282 | struct timeval genesis; |
fc24389f JA |
2283 | struct thread_data *td; |
2284 | unsigned long spent; | |
213b446c | 2285 | int i, todo, nr_running, m_rate, t_rate; |
fc24389f | 2286 | |
fc24389f JA |
2287 | printf("Starting %d threads\n", thread_number); |
2288 | fflush(stdout); | |
2289 | ||
7292613b JA |
2290 | signal(SIGINT, sig_handler); |
2291 | ||
fc24389f | 2292 | todo = thread_number; |
02bdd9ba | 2293 | nr_running = 0; |
213b446c | 2294 | m_rate = t_rate = 0; |
fc24389f | 2295 | |
8bdcfab5 JA |
2296 | for (i = 0; i < thread_number; i++) { |
2297 | td = &threads[i]; | |
2298 | ||
fc097bfe JA |
2299 | if (!td->create_serialize) |
2300 | continue; | |
2301 | ||
8bdcfab5 JA |
2302 | /* |
2303 | * do file setup here so it happens sequentially, | |
2304 | * we don't want X number of threads getting their | |
2305 | * client data interspersed on disk | |
2306 | */ | |
2307 | if (setup_file(td)) { | |
40ef7f64 | 2308 | td_set_runstate(td, TD_REAPED); |
8bdcfab5 JA |
2309 | todo--; |
2310 | } | |
2311 | } | |
2312 | ||
2313 | gettimeofday(&genesis, NULL); | |
2314 | ||
213b446c | 2315 | while (todo) { |
3f39453a JA |
2316 | /* |
2317 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
2318 | */ | |
fc24389f JA |
2319 | for (i = 0; i < thread_number; i++) { |
2320 | td = &threads[i]; | |
2321 | ||
02bdd9ba | 2322 | if (td->runstate != TD_NOT_CREATED) |
fc24389f JA |
2323 | continue; |
2324 | ||
213b446c JA |
2325 | /* |
2326 | * never got a chance to start, killed by other | |
2327 | * thread for some reason | |
2328 | */ | |
2329 | if (td->terminate) { | |
2330 | todo--; | |
2331 | continue; | |
2332 | } | |
2333 | ||
fc24389f | 2334 | if (td->start_delay) { |
be33abe4 | 2335 | spent = mtime_since_now(&genesis); |
fc24389f JA |
2336 | |
2337 | if (td->start_delay * 1000 > spent) | |
2338 | continue; | |
2339 | } | |
2340 | ||
40ef7f64 JA |
2341 | td_set_runstate(td, TD_CREATED); |
2342 | check_str_update(td, nr_running, t_rate, m_rate); | |
fc24389f JA |
2343 | sem_init(&startup_sem, 1, 1); |
2344 | todo--; | |
2345 | ||
2346 | if (fork()) | |
2347 | sem_wait(&startup_sem); | |
2348 | else { | |
2349 | thread_main(shm_id, i, argv); | |
2350 | exit(0); | |
2351 | } | |
2352 | } | |
2353 | ||
3f39453a | 2354 | /* |
e8457004 | 2355 | * start created threads (TD_CREATED -> TD_RUNNING) |
3f39453a | 2356 | */ |
fc24389f JA |
2357 | for (i = 0; i < thread_number; i++) { |
2358 | struct thread_data *td = &threads[i]; | |
2359 | ||
3f39453a JA |
2360 | if (td->runstate != TD_CREATED) |
2361 | continue; | |
2362 | ||
40ef7f64 | 2363 | td_set_runstate(td, TD_RUNNING); |
3f39453a JA |
2364 | nr_running++; |
2365 | m_rate += td->ratemin; | |
2366 | t_rate += td->rate; | |
40ef7f64 | 2367 | check_str_update(td, nr_running, t_rate, m_rate); |
3f39453a | 2368 | sem_post(&td->mutex); |
fc24389f JA |
2369 | } |
2370 | ||
e8457004 JA |
2371 | for (i = 0; i < thread_number; i++) { |
2372 | struct thread_data *td = &threads[i]; | |
2373 | ||
2374 | if (td->runstate == TD_RUNNING) | |
2375 | run_str[td->thread_number - 1] = '+'; | |
2376 | else if (td->runstate == TD_VERIFYING) | |
2377 | run_str[td->thread_number - 1] = 'V'; | |
2378 | else | |
2379 | continue; | |
2380 | ||
40ef7f64 | 2381 | check_str_update(td, nr_running, t_rate, m_rate); |
e8457004 JA |
2382 | } |
2383 | ||
213b446c | 2384 | reap_threads(&nr_running, &t_rate, &m_rate); |
02bdd9ba | 2385 | |
fc24389f JA |
2386 | if (todo) |
2387 | usleep(100000); | |
2388 | } | |
02bdd9ba JA |
2389 | |
2390 | while (nr_running) { | |
213b446c | 2391 | reap_threads(&nr_running, &t_rate, &m_rate); |
02bdd9ba JA |
2392 | usleep(10000); |
2393 | } | |
fc24389f JA |
2394 | } |
2395 | ||
8867c0a8 | 2396 | int setup_thread_area(void) |
892199bd | 2397 | { |
8867c0a8 JA |
2398 | /* |
2399 | * 1024 is too much on some machines, scale max_jobs if | |
2400 | * we get a failure that looks like too large a shm segment | |
2401 | */ | |
2402 | do { | |
2403 | int s = max_jobs * sizeof(struct thread_data); | |
18e0b78c | 2404 | |
8867c0a8 JA |
2405 | shm_id = shmget(0, s, IPC_CREAT | 0600); |
2406 | if (shm_id != -1) | |
2407 | break; | |
2408 | if (errno != EINVAL) { | |
2409 | perror("shmget"); | |
2410 | break; | |
2411 | } | |
2412 | ||
2413 | max_jobs >>= 1; | |
d4fac444 | 2414 | } while (max_jobs); |
8867c0a8 JA |
2415 | |
2416 | if (shm_id == -1) | |
892199bd | 2417 | return 1; |
892199bd JA |
2418 | |
2419 | threads = shmat(shm_id, NULL, 0); | |
8867c0a8 | 2420 | if (threads == (void *) -1) { |
86184d14 JA |
2421 | perror("shmat"); |
2422 | return 1; | |
2423 | } | |
892199bd JA |
2424 | |
2425 | atexit(free_shm); | |
8867c0a8 JA |
2426 | return 0; |
2427 | } | |
2428 | ||
2429 | int main(int argc, char *argv[]) | |
2430 | { | |
8c033f93 | 2431 | static unsigned long max_run[2], min_run[2]; |
57d753e3 | 2432 | static unsigned long max_bw[2], min_bw[2]; |
22334044 | 2433 | static unsigned long io_mb[2], agg[2]; |
8867c0a8 JA |
2434 | int i; |
2435 | ||
2436 | if (setup_thread_area()) | |
2437 | return 1; | |
892199bd | 2438 | |
47d45203 | 2439 | if (sched_getaffinity(getpid(), sizeof(cpu_set_t), &def_thread.cpumask) == -1) { |
4240cfa1 JA |
2440 | perror("sched_getaffinity"); |
2441 | return 1; | |
2442 | } | |
2443 | ||
47d45203 JA |
2444 | /* |
2445 | * fill globals | |
2446 | */ | |
2447 | def_thread.ddir = DDIR_READ; | |
2448 | def_thread.bs = DEF_BS; | |
7889f07b JA |
2449 | def_thread.min_bs = -1; |
2450 | def_thread.max_bs = -1; | |
02983297 | 2451 | def_thread.odirect = DEF_ODIRECT; |
47d45203 | 2452 | def_thread.ratecycle = DEF_RATE_CYCLE; |
02983297 | 2453 | def_thread.sequential = DEF_SEQUENTIAL; |
47d45203 | 2454 | def_thread.timeout = DEF_TIMEOUT; |
02983297 JA |
2455 | def_thread.create_file = DEF_CREATE; |
2456 | def_thread.overwrite = DEF_OVERWRITE; | |
b95799ca | 2457 | def_thread.invalidate_cache = DEF_INVALIDATE; |
99c6704f JA |
2458 | def_thread.sync_io = DEF_SYNCIO; |
2459 | def_thread.mem_type = MEM_MALLOC; | |
1d035750 | 2460 | def_thread.bw_avg_time = DEF_BWAVGTIME; |
fc097bfe JA |
2461 | def_thread.create_serialize = DEF_CREATE_SER; |
2462 | def_thread.create_fsync = DEF_CREATE_FSYNC; | |
b6794fbf | 2463 | def_thread.loops = DEF_LOOPS; |
cfc702bd | 2464 | def_thread.verify = DEF_VERIFY; |
47d45203 | 2465 | |
892199bd | 2466 | i = parse_options(argc, argv); |
7dd1389e | 2467 | |
5961d92c JA |
2468 | if (!ini_file) { |
2469 | printf("Need job file\n"); | |
2470 | return 1; | |
2471 | } | |
2472 | ||
2473 | if (parse_jobs_ini(ini_file)) | |
2474 | return 1; | |
7dd1389e | 2475 | |
4240cfa1 JA |
2476 | if (!thread_number) { |
2477 | printf("Nothing to do\n"); | |
2478 | return 1; | |
2479 | } | |
7dd1389e | 2480 | |
fc24389f | 2481 | run_threads(argv); |
892199bd | 2482 | |
892199bd JA |
2483 | min_bw[0] = min_run[0] = ~0UL; |
2484 | min_bw[1] = min_run[1] = ~0UL; | |
22334044 JA |
2485 | io_mb[0] = io_mb[1] = 0; |
2486 | agg[0] = agg[1] = 0; | |
892199bd JA |
2487 | for (i = 0; i < thread_number; i++) { |
2488 | struct thread_data *td = &threads[i]; | |
2489 | unsigned long bw = 0; | |
2490 | ||
22334044 JA |
2491 | if (!td->error) { |
2492 | if (td->runtime < min_run[td->ddir]) | |
2493 | min_run[td->ddir] = td->runtime; | |
2494 | if (td->runtime > max_run[td->ddir]) | |
2495 | max_run[td->ddir] = td->runtime; | |
892199bd | 2496 | |
892199bd | 2497 | if (td->runtime) |
49d2caab | 2498 | bw = td->io_bytes / td->runtime; |
22334044 JA |
2499 | if (bw < min_bw[td->ddir]) |
2500 | min_bw[td->ddir] = bw; | |
2501 | if (bw > max_bw[td->ddir]) | |
2502 | max_bw[td->ddir] = bw; | |
2503 | ||
49d2caab | 2504 | io_mb[td->ddir] += td->io_bytes >> 20; |
892199bd JA |
2505 | } |
2506 | ||
2507 | show_thread_status(td); | |
2508 | } | |
22334044 JA |
2509 | |
2510 | if (max_run[0]) | |
49d2caab | 2511 | agg[0] = (io_mb[0] * 1024 * 1000) / max_run[0]; |
22334044 | 2512 | if (max_run[1]) |
49d2caab | 2513 | agg[1] = (io_mb[1] * 1024 * 1000) / max_run[1]; |
22334044 | 2514 | |
57d753e3 | 2515 | printf("\nRun status:\n"); |
892199bd | 2516 | if (max_run[DDIR_READ]) |
22334044 | 2517 | printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", io_mb[0], agg[0], min_bw[0], max_bw[0], min_run[0], max_run[0]); |
892199bd | 2518 | if (max_run[DDIR_WRITE]) |
22334044 | 2519 | printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", io_mb[1], agg[1], min_bw[1], max_bw[1], min_run[1], max_run[1]); |
fc24389f | 2520 | |
892199bd JA |
2521 | return 0; |
2522 | } |