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> | |
e128065d | 29 | #include <math.h> |
49d2caab | 30 | #include <assert.h> |
189873de | 31 | #include <pthread.h> |
debf703a JA |
32 | #include <dirent.h> |
33 | #include <libgen.h> | |
892199bd JA |
34 | #include <sys/types.h> |
35 | #include <sys/stat.h> | |
36 | #include <sys/wait.h> | |
892199bd JA |
37 | #include <sys/ipc.h> |
38 | #include <sys/shm.h> | |
c94deb1c | 39 | #include <sys/ioctl.h> |
6e2c38cc | 40 | #include <sys/mman.h> |
892199bd JA |
41 | #include <asm/unistd.h> |
42 | ||
27c32a38 | 43 | #include "fio.h" |
892199bd | 44 | |
892199bd JA |
45 | #define MASK (4095) |
46 | ||
4240cfa1 | 47 | #define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK)) |
892199bd | 48 | |
27c32a38 JA |
49 | int groupid = 0; |
50 | int thread_number = 0; | |
51 | char run_str[MAX_JOBS + 1]; | |
52 | int shm_id = 0; | |
6f75d67c | 53 | static LIST_HEAD(disk_list); |
892199bd | 54 | |
8882f4e9 JA |
55 | static void update_io_ticks(void); |
56 | static void disk_util_timer_arm(void); | |
57 | ||
02bdd9ba JA |
58 | /* |
59 | * thread life cycle | |
60 | */ | |
61 | enum { | |
62 | TD_NOT_CREATED = 0, | |
63 | TD_CREATED, | |
e8457004 JA |
64 | TD_RUNNING, |
65 | TD_VERIFYING, | |
02bdd9ba JA |
66 | TD_EXITED, |
67 | TD_REAPED, | |
68 | }; | |
69 | ||
2c83567e JA |
70 | /* |
71 | * The io unit | |
72 | */ | |
73 | struct io_u { | |
74 | struct iocb iocb; | |
57d753e3 | 75 | struct timeval start_time; |
2c83567e JA |
76 | struct timeval issue_time; |
77 | ||
2c83567e JA |
78 | char *buf; |
79 | unsigned int buflen; | |
4ac89145 | 80 | unsigned long long offset; |
2c83567e JA |
81 | |
82 | struct list_head list; | |
83 | }; | |
84 | ||
62bb4285 | 85 | #define should_fsync(td) (td_write(td) && !(td)->odirect) |
02983297 | 86 | |
892199bd JA |
87 | static sem_t startup_sem; |
88 | ||
27c32a38 JA |
89 | #define TERMINATE_ALL (-1) |
90 | ||
91 | static void terminate_threads(int groupid) | |
892199bd JA |
92 | { |
93 | int i; | |
94 | ||
213b446c JA |
95 | for (i = 0; i < thread_number; i++) { |
96 | struct thread_data *td = &threads[i]; | |
97 | ||
27c32a38 JA |
98 | if (groupid == TERMINATE_ALL || groupid == td->groupid) { |
99 | td->terminate = 1; | |
100 | td->start_delay = 0; | |
101 | } | |
213b446c | 102 | } |
02bdd9ba JA |
103 | } |
104 | ||
27c32a38 | 105 | static void sig_handler(int sig) |
946d8870 | 106 | { |
8882f4e9 JA |
107 | switch (sig) { |
108 | case SIGALRM: | |
109 | update_io_ticks(); | |
110 | disk_util_timer_arm(); | |
111 | break; | |
112 | default: | |
c501c98b | 113 | printf("fio: terminating on signal\n"); |
8882f4e9 JA |
114 | terminate_threads(TERMINATE_ALL); |
115 | break; | |
116 | } | |
892199bd JA |
117 | } |
118 | ||
5c24b2c4 | 119 | static unsigned long utime_since(struct timeval *s, struct timeval *e) |
892199bd JA |
120 | { |
121 | double sec, usec; | |
122 | ||
123 | sec = e->tv_sec - s->tv_sec; | |
124 | usec = e->tv_usec - s->tv_usec; | |
125 | if (sec > 0 && usec < 0) { | |
126 | sec--; | |
127 | usec += 1000000; | |
128 | } | |
129 | ||
130 | sec *= (double) 1000000; | |
131 | ||
132 | return sec + usec; | |
133 | } | |
134 | ||
fd11d7af JA |
135 | static unsigned long utime_since_now(struct timeval *s) |
136 | { | |
137 | struct timeval t; | |
138 | ||
139 | gettimeofday(&t, NULL); | |
140 | return utime_since(s, &t); | |
141 | } | |
142 | ||
5c24b2c4 | 143 | static unsigned long mtime_since(struct timeval *s, struct timeval *e) |
892199bd JA |
144 | { |
145 | double sec, usec; | |
146 | ||
147 | sec = e->tv_sec - s->tv_sec; | |
148 | usec = e->tv_usec - s->tv_usec; | |
149 | if (sec > 0 && usec < 0) { | |
150 | sec--; | |
151 | usec += 1000000; | |
152 | } | |
153 | ||
154 | sec *= (double) 1000; | |
155 | usec /= (double) 1000; | |
156 | ||
157 | return sec + usec; | |
158 | } | |
159 | ||
be33abe4 JA |
160 | static unsigned long mtime_since_now(struct timeval *s) |
161 | { | |
162 | struct timeval t; | |
163 | ||
164 | gettimeofday(&t, NULL); | |
165 | return mtime_since(s, &t); | |
166 | } | |
167 | ||
98168d55 JA |
168 | static inline unsigned long msec_now(struct timeval *s) |
169 | { | |
170 | return s->tv_sec * 1000 + s->tv_usec / 1000; | |
171 | } | |
172 | ||
49d2caab JA |
173 | static int random_map_free(struct thread_data *td, unsigned long long block) |
174 | { | |
75b2ab2c JA |
175 | unsigned int idx = RAND_MAP_IDX(td, block); |
176 | unsigned int bit = RAND_MAP_BIT(td, block); | |
49d2caab JA |
177 | |
178 | return (td->file_map[idx] & (1UL << bit)) == 0; | |
179 | } | |
180 | ||
181 | static int get_next_free_block(struct thread_data *td, unsigned long long *b) | |
892199bd | 182 | { |
49d2caab JA |
183 | int i; |
184 | ||
185 | *b = 0; | |
186 | i = 0; | |
187 | while ((*b) * td->min_bs < td->io_size) { | |
188 | if (td->file_map[i] != -1UL) { | |
189 | *b += ffz(td->file_map[i]); | |
190 | return 0; | |
191 | } | |
192 | ||
193 | *b += BLOCKS_PER_MAP; | |
194 | i++; | |
195 | } | |
196 | ||
197 | return 1; | |
198 | } | |
199 | ||
200 | static void mark_random_map(struct thread_data *td, struct io_u *io_u) | |
201 | { | |
202 | unsigned long block = io_u->offset / td->min_bs; | |
203 | unsigned int blocks = 0; | |
204 | ||
205 | while (blocks < (io_u->buflen / td->min_bs)) { | |
206 | int idx, bit; | |
207 | ||
208 | if (!random_map_free(td, block)) | |
209 | break; | |
210 | ||
75b2ab2c JA |
211 | idx = RAND_MAP_IDX(td, block); |
212 | bit = RAND_MAP_BIT(td, block); | |
49d2caab JA |
213 | |
214 | assert(idx < td->num_maps); | |
215 | ||
216 | td->file_map[idx] |= (1UL << bit); | |
217 | block++; | |
218 | blocks++; | |
219 | } | |
220 | ||
221 | if ((blocks * td->min_bs) < io_u->buflen) | |
222 | io_u->buflen = blocks * td->min_bs; | |
223 | } | |
224 | ||
225 | static int get_next_offset(struct thread_data *td, unsigned long long *offset) | |
226 | { | |
75b2ab2c | 227 | unsigned long long b, rb; |
d32d9284 | 228 | long r; |
892199bd JA |
229 | |
230 | if (!td->sequential) { | |
49d2caab JA |
231 | unsigned long max_blocks = td->io_size / td->min_bs; |
232 | int loops = 50; | |
233 | ||
234 | do { | |
235 | lrand48_r(&td->random_state, &r); | |
236 | b = ((max_blocks - 1) * r / (RAND_MAX+1.0)); | |
75b2ab2c | 237 | rb = b + (td->file_offset / td->min_bs); |
49d2caab | 238 | loops--; |
75b2ab2c | 239 | } while (!random_map_free(td, rb) && loops); |
49d2caab JA |
240 | |
241 | if (!loops) { | |
242 | if (get_next_free_block(td, &b)) | |
243 | return 1; | |
244 | } | |
7889f07b | 245 | } else |
49d2caab | 246 | b = td->last_bytes / td->min_bs; |
7889f07b | 247 | |
49d2caab | 248 | *offset = (b * td->min_bs) + td->file_offset; |
75b2ab2c JA |
249 | if (*offset > td->file_size) |
250 | return 1; | |
251 | ||
49d2caab | 252 | return 0; |
7889f07b JA |
253 | } |
254 | ||
255 | static unsigned int get_next_buflen(struct thread_data *td) | |
256 | { | |
257 | unsigned int buflen; | |
d32d9284 | 258 | long r; |
7889f07b JA |
259 | |
260 | if (td->min_bs == td->max_bs) | |
261 | buflen = td->min_bs; | |
262 | else { | |
d32d9284 | 263 | lrand48_r(&td->bsrange_state, &r); |
7889f07b JA |
264 | buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0)); |
265 | buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1); | |
892199bd JA |
266 | } |
267 | ||
49d2caab JA |
268 | if (buflen > td->io_size - td->this_io_bytes) |
269 | buflen = td->io_size - td->this_io_bytes; | |
7889f07b | 270 | |
7889f07b | 271 | return buflen; |
892199bd JA |
272 | } |
273 | ||
57d753e3 JA |
274 | static inline void add_stat_sample(struct thread_data *td, struct io_stat *is, |
275 | unsigned long val) | |
892199bd | 276 | { |
57d753e3 JA |
277 | if (val > is->max_val) |
278 | is->max_val = val; | |
279 | if (val < is->min_val) | |
280 | is->min_val = val; | |
281 | ||
282 | is->val += val; | |
283 | is->val_sq += val * val; | |
284 | is->samples++; | |
285 | } | |
fd1ae4c9 | 286 | |
a0a9b35b JA |
287 | static void add_log_sample(struct thread_data *td, struct io_log *log, |
288 | unsigned long val) | |
289 | { | |
290 | if (log->nr_samples == log->max_samples) { | |
291 | int new_size = sizeof(struct io_sample) * log->max_samples * 2; | |
292 | ||
293 | log->log = realloc(log->log, new_size); | |
294 | log->max_samples <<= 1; | |
295 | } | |
296 | ||
297 | log->log[log->nr_samples].val = val; | |
298 | log->log[log->nr_samples].time = mtime_since_now(&td->start); | |
299 | log->nr_samples++; | |
300 | } | |
301 | ||
57d753e3 JA |
302 | static void add_clat_sample(struct thread_data *td, unsigned long msec) |
303 | { | |
304 | add_stat_sample(td, &td->clat_stat, msec); | |
a0a9b35b JA |
305 | |
306 | if (td->lat_log) | |
307 | add_log_sample(td, td->lat_log, msec); | |
57d753e3 | 308 | } |
fd1ae4c9 | 309 | |
57d753e3 JA |
310 | static void add_slat_sample(struct thread_data *td, unsigned long msec) |
311 | { | |
312 | add_stat_sample(td, &td->slat_stat, msec); | |
313 | } | |
fd1ae4c9 | 314 | |
645785e5 | 315 | static void add_bw_sample(struct thread_data *td) |
57d753e3 JA |
316 | { |
317 | unsigned long spent = mtime_since_now(&td->stat_sample_time); | |
318 | unsigned long rate; | |
319 | ||
1d035750 | 320 | if (spent < td->bw_avg_time) |
57d753e3 JA |
321 | return; |
322 | ||
49d2caab | 323 | rate = (td->this_io_bytes - td->stat_io_bytes) / spent; |
57d753e3 JA |
324 | add_stat_sample(td, &td->bw_stat, rate); |
325 | ||
a0a9b35b JA |
326 | if (td->bw_log) |
327 | add_log_sample(td, td->bw_log, rate); | |
328 | ||
57d753e3 | 329 | gettimeofday(&td->stat_sample_time, NULL); |
49d2caab | 330 | td->stat_io_bytes = td->this_io_bytes; |
892199bd JA |
331 | } |
332 | ||
fd11d7af JA |
333 | /* |
334 | * busy looping version for the last few usec | |
335 | */ | |
336 | static void __usec_sleep(int usec) | |
337 | { | |
338 | struct timeval start; | |
339 | ||
340 | gettimeofday(&start, NULL); | |
341 | while (utime_since_now(&start) < usec) | |
3782a8cd | 342 | nop; |
fd11d7af JA |
343 | } |
344 | ||
d15c5195 | 345 | static void usec_sleep(struct thread_data *td, unsigned long usec) |
892199bd | 346 | { |
d15c5195 JA |
347 | struct timespec req, rem; |
348 | ||
349 | req.tv_sec = usec / 1000000; | |
350 | req.tv_nsec = usec * 1000 - req.tv_sec * 1000000; | |
892199bd JA |
351 | |
352 | do { | |
fd11d7af JA |
353 | if (usec < 5000) { |
354 | __usec_sleep(usec); | |
355 | break; | |
356 | } | |
d15c5195 | 357 | |
86184d14 | 358 | rem.tv_sec = rem.tv_nsec = 0; |
d15c5195 JA |
359 | if (nanosleep(&req, &rem) < 0) |
360 | break; | |
361 | ||
362 | if ((rem.tv_sec + rem.tv_nsec) == 0) | |
892199bd | 363 | break; |
86184d14 JA |
364 | |
365 | req.tv_nsec = rem.tv_nsec; | |
d15c5195 JA |
366 | req.tv_sec = rem.tv_sec; |
367 | ||
368 | usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000; | |
369 | } while (!td->terminate); | |
892199bd JA |
370 | } |
371 | ||
9e850933 JA |
372 | static void rate_throttle(struct thread_data *td, unsigned long time_spent, |
373 | unsigned int bytes) | |
86184d14 | 374 | { |
9e850933 JA |
375 | unsigned long usec_cycle; |
376 | ||
4240cfa1 JA |
377 | if (!td->rate) |
378 | return; | |
379 | ||
9e850933 JA |
380 | usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs); |
381 | ||
382 | if (time_spent < usec_cycle) { | |
383 | unsigned long s = usec_cycle - time_spent; | |
86184d14 JA |
384 | |
385 | td->rate_pending_usleep += s; | |
fad86e6a | 386 | if (td->rate_pending_usleep >= 100000) { |
d15c5195 | 387 | usec_sleep(td, td->rate_pending_usleep); |
86184d14 JA |
388 | td->rate_pending_usleep = 0; |
389 | } | |
4240cfa1 | 390 | } else { |
9e850933 | 391 | long overtime = time_spent - usec_cycle; |
42b2b9fe | 392 | |
4240cfa1 JA |
393 | td->rate_pending_usleep -= overtime; |
394 | } | |
395 | } | |
396 | ||
5c24b2c4 | 397 | static int check_min_rate(struct thread_data *td, struct timeval *now) |
4240cfa1 | 398 | { |
7607bc6b | 399 | unsigned long spent; |
4240cfa1 JA |
400 | unsigned long rate; |
401 | ||
402 | /* | |
403 | * allow a 2 second settle period in the beginning | |
404 | */ | |
7607bc6b | 405 | if (mtime_since(&td->start, now) < 2000) |
4240cfa1 JA |
406 | return 0; |
407 | ||
408 | /* | |
409 | * if rate blocks is set, sample is running | |
410 | */ | |
49d2caab | 411 | if (td->rate_bytes) { |
4240cfa1 JA |
412 | spent = mtime_since(&td->lastrate, now); |
413 | if (spent < td->ratecycle) | |
414 | return 0; | |
415 | ||
49d2caab | 416 | rate = (td->this_io_bytes - td->rate_bytes) / spent; |
4240cfa1 JA |
417 | if (rate < td->ratemin) { |
418 | printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate); | |
02bdd9ba | 419 | if (rate_quit) |
27c32a38 | 420 | terminate_threads(td->groupid); |
4240cfa1 JA |
421 | return 1; |
422 | } | |
86184d14 | 423 | } |
4240cfa1 | 424 | |
49d2caab | 425 | td->rate_bytes = td->this_io_bytes; |
4240cfa1 JA |
426 | memcpy(&td->lastrate, now, sizeof(*now)); |
427 | return 0; | |
86184d14 JA |
428 | } |
429 | ||
67903a2e JA |
430 | static inline int runtime_exceeded(struct thread_data *td, struct timeval *t) |
431 | { | |
01f79976 JA |
432 | if (!td->timeout) |
433 | return 0; | |
67903a2e JA |
434 | if (mtime_since(&td->start, t) >= td->timeout * 1000) |
435 | return 1; | |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
e8457004 JA |
440 | static void fill_random_bytes(struct thread_data *td, |
441 | unsigned char *p, unsigned int len) | |
442 | { | |
645785e5 | 443 | unsigned int todo; |
40ef7f64 | 444 | double r; |
e8457004 JA |
445 | |
446 | while (len) { | |
40ef7f64 | 447 | drand48_r(&td->verify_state, &r); |
e8457004 | 448 | |
40ef7f64 JA |
449 | /* |
450 | * lrand48_r seems to be broken and only fill the bottom | |
451 | * 32-bits, even on 64-bit archs with 64-bit longs | |
452 | */ | |
453 | todo = sizeof(r); | |
e8457004 JA |
454 | if (todo > len) |
455 | todo = len; | |
456 | ||
457 | memcpy(p, &r, todo); | |
458 | ||
459 | len -= todo; | |
460 | p += todo; | |
461 | } | |
462 | } | |
463 | ||
9d0c6ca2 JA |
464 | static void hexdump(void *buffer, int len) |
465 | { | |
466 | unsigned char *p = buffer; | |
467 | int i; | |
468 | ||
469 | for (i = 0; i < len; i++) | |
470 | printf("%02x", p[i]); | |
471 | printf("\n"); | |
472 | } | |
473 | ||
7f46ef08 JA |
474 | static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u) |
475 | { | |
476 | unsigned char *p = (unsigned char *) io_u->buf; | |
477 | unsigned long c; | |
478 | ||
479 | p += sizeof(*hdr); | |
480 | c = crc32(p, hdr->len - sizeof(*hdr)); | |
481 | ||
482 | return c != hdr->crc32; | |
483 | } | |
484 | ||
485 | static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u) | |
e8457004 | 486 | { |
e8457004 JA |
487 | unsigned char *p = (unsigned char *) io_u->buf; |
488 | struct md5_ctx md5_ctx; | |
9d0c6ca2 | 489 | int ret; |
e8457004 | 490 | |
e8457004 JA |
491 | memset(&md5_ctx, 0, sizeof(md5_ctx)); |
492 | p += sizeof(*hdr); | |
493 | md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr)); | |
494 | ||
9d0c6ca2 JA |
495 | ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); |
496 | if (ret) { | |
497 | hexdump(hdr->md5_digest, sizeof(hdr->md5_digest)); | |
498 | hexdump(md5_ctx.hash, sizeof(md5_ctx.hash)); | |
499 | } | |
500 | ||
501 | return ret; | |
e8457004 JA |
502 | } |
503 | ||
7f46ef08 JA |
504 | static int verify_io_u(struct io_u *io_u) |
505 | { | |
506 | struct verify_header *hdr = (struct verify_header *) io_u->buf; | |
507 | int ret; | |
508 | ||
509 | if (hdr->fio_magic != FIO_HDR_MAGIC) | |
510 | return 1; | |
511 | ||
512 | if (hdr->verify_type == VERIFY_MD5) | |
513 | ret = verify_io_u_md5(hdr, io_u); | |
514 | else if (hdr->verify_type == VERIFY_CRC32) | |
515 | ret = verify_io_u_crc32(hdr, io_u); | |
516 | else { | |
517 | fprintf(stderr, "Bad verify type %d\n", hdr->verify_type); | |
518 | ret = 1; | |
519 | } | |
520 | ||
521 | return ret; | |
522 | } | |
523 | ||
524 | static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len) | |
525 | { | |
526 | hdr->crc32 = crc32(p, len); | |
527 | } | |
528 | ||
529 | static void fill_md5(struct verify_header *hdr, void *p, unsigned int len) | |
530 | { | |
531 | struct md5_ctx md5_ctx; | |
532 | ||
533 | memset(&md5_ctx, 0, sizeof(md5_ctx)); | |
534 | md5_update(&md5_ctx, p, len); | |
535 | memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash)); | |
536 | } | |
537 | ||
cfc702bd JA |
538 | /* |
539 | * fill body of io_u->buf with random data and add a header with the | |
540 | * (eg) sha1sum of that data. | |
541 | */ | |
e8457004 | 542 | static void populate_io_u(struct thread_data *td, struct io_u *io_u) |
cfc702bd | 543 | { |
e8457004 | 544 | unsigned char *p = (unsigned char *) io_u->buf; |
7f46ef08 | 545 | struct verify_header hdr; |
e8457004 JA |
546 | |
547 | hdr.fio_magic = FIO_HDR_MAGIC; | |
548 | hdr.len = io_u->buflen; | |
549 | p += sizeof(hdr); | |
550 | fill_random_bytes(td, p, io_u->buflen - sizeof(hdr)); | |
551 | ||
7f46ef08 JA |
552 | if (td->verify == VERIFY_MD5) { |
553 | fill_md5(&hdr, p, io_u->buflen - sizeof(hdr)); | |
554 | hdr.verify_type = VERIFY_MD5; | |
555 | } else { | |
556 | fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr)); | |
557 | hdr.verify_type = VERIFY_CRC32; | |
558 | } | |
559 | ||
e8457004 | 560 | memcpy(io_u->buf, &hdr, sizeof(hdr)); |
cfc702bd JA |
561 | } |
562 | ||
2c83567e JA |
563 | static void put_io_u(struct thread_data *td, struct io_u *io_u) |
564 | { | |
565 | list_del(&io_u->list); | |
566 | list_add(&io_u->list, &td->io_u_freelist); | |
567 | td->cur_depth--; | |
568 | } | |
569 | ||
f0f3411b JA |
570 | #define queue_full(td) (list_empty(&(td)->io_u_freelist)) |
571 | ||
e8457004 JA |
572 | static struct io_u *__get_io_u(struct thread_data *td) |
573 | { | |
574 | struct io_u *io_u; | |
575 | ||
f0f3411b | 576 | if (queue_full(td)) |
e8457004 JA |
577 | return NULL; |
578 | ||
579 | io_u = list_entry(td->io_u_freelist.next, struct io_u, list); | |
580 | list_del(&io_u->list); | |
581 | list_add(&io_u->list, &td->io_u_busylist); | |
f4bb2243 | 582 | td->cur_depth++; |
e8457004 JA |
583 | return io_u; |
584 | } | |
585 | ||
2c83567e JA |
586 | static struct io_u *get_io_u(struct thread_data *td) |
587 | { | |
588 | struct io_u *io_u; | |
589 | ||
e8457004 JA |
590 | io_u = __get_io_u(td); |
591 | if (!io_u) | |
2c83567e JA |
592 | return NULL; |
593 | ||
406e7b7c JA |
594 | if (get_next_offset(td, &io_u->offset)) { |
595 | put_io_u(td, io_u); | |
49d2caab | 596 | return NULL; |
406e7b7c | 597 | } |
49d2caab | 598 | |
b2a369fb JA |
599 | io_u->buflen = get_next_buflen(td); |
600 | if (!io_u->buflen) { | |
e8457004 | 601 | put_io_u(td, io_u); |
7889f07b | 602 | return NULL; |
e8457004 | 603 | } |
2c83567e | 604 | |
75b2ab2c JA |
605 | if (io_u->buflen + io_u->offset > td->file_size) |
606 | io_u->buflen = td->file_size - io_u->offset; | |
49d2caab JA |
607 | |
608 | if (!td->sequential) | |
609 | mark_random_map(td, io_u); | |
610 | ||
611 | td->last_bytes += io_u->buflen; | |
612 | ||
7f46ef08 | 613 | if (td->verify != VERIFY_NONE) |
e8457004 | 614 | populate_io_u(td, io_u); |
cfc702bd | 615 | |
2c83567e JA |
616 | if (td->use_aio) { |
617 | if (td_read(td)) | |
618 | io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
619 | else | |
620 | io_prep_pwrite(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
621 | } | |
622 | ||
57d753e3 | 623 | gettimeofday(&io_u->start_time, NULL); |
2c83567e JA |
624 | return io_u; |
625 | } | |
626 | ||
40ef7f64 JA |
627 | static inline void td_set_runstate(struct thread_data *td, int runstate) |
628 | { | |
629 | td->old_runstate = td->runstate; | |
630 | td->runstate = runstate; | |
631 | } | |
632 | ||
645785e5 JA |
633 | static int get_next_verify(struct thread_data *td, |
634 | unsigned long long *offset, unsigned int *len) | |
635 | { | |
636 | struct io_piece *ipo; | |
637 | ||
638 | if (list_empty(&td->io_hist_list)) | |
639 | return 1; | |
640 | ||
641 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
642 | list_del(&ipo->list); | |
643 | ||
644 | *offset = ipo->offset; | |
645 | *len = ipo->len; | |
646 | free(ipo); | |
647 | return 0; | |
648 | } | |
649 | ||
9d0c6ca2 JA |
650 | static void prune_io_piece_log(struct thread_data *td) |
651 | { | |
652 | struct io_piece *ipo; | |
653 | ||
654 | while (!list_empty(&td->io_hist_list)) { | |
655 | ipo = list_entry(td->io_hist_list.next, struct io_piece, list); | |
656 | ||
657 | list_del(&ipo->list); | |
658 | free(ipo); | |
659 | } | |
660 | } | |
661 | ||
9d0c6ca2 JA |
662 | /* |
663 | * log a succesful write, so we can unwind the log for verify | |
664 | */ | |
665 | static void log_io_piece(struct thread_data *td, struct io_u *io_u) | |
666 | { | |
49d2caab | 667 | struct io_piece *ipo = malloc(sizeof(struct io_piece)); |
9d0c6ca2 JA |
668 | struct list_head *entry; |
669 | ||
670 | INIT_LIST_HEAD(&ipo->list); | |
671 | ipo->offset = io_u->offset; | |
672 | ipo->len = io_u->buflen; | |
673 | ||
49d2caab JA |
674 | /* |
675 | * for random io where the writes extend the file, it will typically | |
676 | * be laid out with the block scattered as written. it's faster to | |
677 | * read them in in that order again, so don't sort | |
678 | */ | |
679 | if (td->sequential || !td->overwrite) { | |
9d0c6ca2 JA |
680 | list_add_tail(&ipo->list, &td->io_hist_list); |
681 | return; | |
682 | } | |
683 | ||
684 | /* | |
685 | * for random io, sort the list so verify will run faster | |
686 | */ | |
687 | entry = &td->io_hist_list; | |
688 | while ((entry = entry->prev) != &td->io_hist_list) { | |
689 | struct io_piece *__ipo = list_entry(entry, struct io_piece, list); | |
690 | ||
9d0c6ca2 JA |
691 | if (__ipo->offset < ipo->offset) |
692 | break; | |
693 | } | |
694 | ||
695 | list_add(&ipo->list, entry); | |
696 | } | |
697 | ||
91fc5dc9 | 698 | static void do_sync_verify(struct thread_data *td) |
cfc702bd | 699 | { |
40ef7f64 | 700 | struct timeval t; |
e8457004 | 701 | struct io_u *io_u = NULL; |
645785e5 | 702 | int ret; |
e8457004 | 703 | |
40ef7f64 | 704 | td_set_runstate(td, TD_VERIFYING); |
e8457004 JA |
705 | |
706 | io_u = __get_io_u(td); | |
707 | ||
40ef7f64 | 708 | if (!td->odirect) { |
6e2c38cc JA |
709 | if (!td->use_mmap) { |
710 | if (fadvise(td->fd, td->file_offset, td->io_size, POSIX_FADV_DONTNEED) < 0) { | |
711 | td->error = errno; | |
712 | goto out; | |
713 | } | |
714 | } else { | |
715 | if (madvise(td->mmap, td->io_size, MADV_DONTNEED)) { | |
716 | td->error = errno; | |
717 | goto out; | |
718 | } | |
40ef7f64 JA |
719 | } |
720 | } | |
721 | ||
e8457004 JA |
722 | do { |
723 | if (td->terminate) | |
724 | break; | |
40ef7f64 JA |
725 | |
726 | gettimeofday(&t, NULL); | |
727 | if (runtime_exceeded(td, &t)) | |
728 | break; | |
729 | ||
645785e5 JA |
730 | if (get_next_verify(td, &io_u->offset, &io_u->buflen)) |
731 | break; | |
732 | ||
733 | if (td->cur_off != io_u->offset) { | |
734 | if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) { | |
735 | td->error = errno; | |
736 | break; | |
737 | } | |
738 | } | |
e8457004 JA |
739 | |
740 | ret = read(td->fd, io_u->buf, io_u->buflen); | |
741 | if (ret < (int) io_u->buflen) { | |
742 | if (ret == -1) { | |
743 | td->error = errno; | |
744 | break; | |
745 | } else if (!ret) | |
746 | break; | |
747 | else | |
748 | io_u->buflen = ret; | |
749 | } | |
750 | ||
645785e5 | 751 | if (verify_io_u(io_u)) |
e8457004 JA |
752 | break; |
753 | ||
645785e5 | 754 | td->cur_off = io_u->offset + io_u->buflen; |
e8457004 JA |
755 | } while (1); |
756 | ||
757 | out: | |
40ef7f64 | 758 | td_set_runstate(td, TD_RUNNING); |
e8457004 | 759 | put_io_u(td, io_u); |
cfc702bd JA |
760 | } |
761 | ||
6e2c38cc JA |
762 | static int __do_sync_mmap(struct thread_data *td, struct io_u *io_u) |
763 | { | |
764 | unsigned long long real_off = io_u->offset - td->file_offset; | |
765 | ||
766 | if (td_read(td)) | |
767 | memcpy(io_u->buf, td->mmap + real_off, io_u->buflen); | |
768 | else | |
769 | memcpy(td->mmap + real_off, io_u->buf, io_u->buflen); | |
c009cdac JA |
770 | |
771 | /* | |
772 | * not really direct, but should drop the pages from the cache | |
773 | */ | |
774 | if (td->odirect) { | |
775 | msync(td->mmap + real_off, io_u->buflen, MS_SYNC); | |
776 | madvise(td->mmap + real_off, io_u->buflen, MADV_DONTNEED); | |
777 | } | |
6e2c38cc JA |
778 | |
779 | return io_u->buflen; | |
780 | } | |
781 | ||
782 | static int __do_sync_rw(struct thread_data *td, struct io_u *io_u) | |
783 | { | |
784 | if (td->cur_off != io_u->offset) { | |
785 | if (lseek(td->fd, io_u->offset, SEEK_SET) == -1) { | |
786 | td->error = errno; | |
787 | return 1; | |
788 | } | |
789 | } | |
790 | ||
791 | if (td_read(td)) | |
792 | return read(td->fd, io_u->buf, io_u->buflen); | |
793 | else | |
794 | return write(td->fd, io_u->buf, io_u->buflen); | |
795 | } | |
796 | ||
797 | static void sync_td(struct thread_data *td) | |
798 | { | |
799 | if (!td->use_mmap) | |
800 | fsync(td->fd); | |
801 | else | |
802 | msync(td->mmap, td->file_size, MS_SYNC); | |
803 | } | |
804 | ||
43000118 | 805 | static void do_sync_io(struct thread_data *td) |
892199bd | 806 | { |
7889f07b | 807 | unsigned long msec, usec; |
e8457004 | 808 | struct io_u *io_u = NULL; |
2c83567e | 809 | struct timeval e; |
892199bd | 810 | |
49d2caab | 811 | while (td->this_io_bytes < td->io_size) { |
892199bd JA |
812 | int ret; |
813 | ||
814 | if (td->terminate) | |
815 | break; | |
816 | ||
2c83567e | 817 | io_u = get_io_u(td); |
7889f07b JA |
818 | if (!io_u) |
819 | break; | |
2c83567e | 820 | |
6e2c38cc JA |
821 | if (!td->use_mmap) |
822 | ret = __do_sync_rw(td, io_u); | |
892199bd | 823 | else |
6e2c38cc | 824 | ret = __do_sync_mmap(td, io_u); |
892199bd | 825 | |
2c83567e | 826 | if (ret < (int) io_u->buflen) { |
892199bd JA |
827 | if (ret == -1) |
828 | td->error = errno; | |
829 | break; | |
830 | } | |
831 | ||
62bb4285 | 832 | if (td_write(td)) |
645785e5 JA |
833 | log_io_piece(td, io_u); |
834 | ||
4240cfa1 | 835 | td->io_blocks++; |
49d2caab JA |
836 | td->io_bytes += io_u->buflen; |
837 | td->this_io_bytes += io_u->buflen; | |
63a09e51 | 838 | td->cur_off = io_u->offset + io_u->buflen; |
4240cfa1 | 839 | |
86184d14 JA |
840 | gettimeofday(&e, NULL); |
841 | ||
57d753e3 | 842 | usec = utime_since(&io_u->start_time, &e); |
86184d14 | 843 | |
9e850933 | 844 | rate_throttle(td, usec, io_u->buflen); |
892199bd | 845 | |
4240cfa1 JA |
846 | if (check_min_rate(td, &e)) { |
847 | td->error = ENODATA; | |
848 | break; | |
849 | } | |
892199bd | 850 | |
4240cfa1 | 851 | msec = usec / 1000; |
57d753e3 | 852 | add_clat_sample(td, msec); |
645785e5 | 853 | add_bw_sample(td); |
67903a2e JA |
854 | |
855 | if (runtime_exceeded(td, &e)) | |
856 | break; | |
2c83567e | 857 | |
cdf92433 | 858 | put_io_u(td, io_u); |
e8457004 | 859 | io_u = NULL; |
cdf92433 | 860 | |
e97712ed | 861 | if (td->thinktime) |
d15c5195 | 862 | usec_sleep(td, td->thinktime); |
e97712ed | 863 | |
cdf92433 JA |
864 | if (should_fsync(td) && td->fsync_blocks && |
865 | (td->io_blocks % td->fsync_blocks) == 0) | |
6e2c38cc | 866 | sync_td(td); |
892199bd JA |
867 | } |
868 | ||
e8457004 JA |
869 | if (io_u) |
870 | put_io_u(td, io_u); | |
871 | ||
4240cfa1 | 872 | if (should_fsync(td)) |
6e2c38cc | 873 | sync_td(td); |
892199bd | 874 | } |
43000118 | 875 | |
1ad72b11 JA |
876 | static int io_u_getevents(struct thread_data *td, int min, int max, |
877 | struct timespec *t) | |
878 | { | |
879 | int r; | |
880 | ||
881 | do { | |
882 | r = io_getevents(td->aio_ctx, min, max, td->aio_events, t); | |
883 | if (r != -EAGAIN && r != -EINTR) | |
884 | break; | |
885 | } while (1); | |
886 | ||
887 | return r; | |
888 | } | |
889 | ||
2c83567e | 890 | static int io_u_queue(struct thread_data *td, struct io_u *io_u) |
56b0eff0 | 891 | { |
2c83567e | 892 | struct iocb *iocb = &io_u->iocb; |
56b0eff0 JA |
893 | int ret; |
894 | ||
895 | do { | |
254605cd | 896 | ret = io_submit(td->aio_ctx, 1, &iocb); |
56b0eff0 JA |
897 | if (ret == 1) |
898 | return 0; | |
406e7b7c | 899 | else if (ret == -EAGAIN) |
56b0eff0 | 900 | usleep(100); |
406e7b7c | 901 | else if (ret == -EINTR) |
a592bd33 | 902 | continue; |
56b0eff0 JA |
903 | else |
904 | break; | |
905 | } while (1); | |
906 | ||
a592bd33 | 907 | return ret; |
56b0eff0 JA |
908 | } |
909 | ||
98168d55 | 910 | #define iocb_time(iocb) ((unsigned long) (iocb)->data) |
2c83567e JA |
911 | #define ev_to_iou(ev) (struct io_u *) ((unsigned long) (ev)->obj) |
912 | ||
f0f3411b | 913 | static int ios_completed(struct thread_data *td, int nr) |
2c83567e JA |
914 | { |
915 | unsigned long msec; | |
916 | struct io_u *io_u; | |
917 | struct timeval e; | |
9e850933 | 918 | int i, bytes_done; |
2c83567e | 919 | |
f0f3411b | 920 | gettimeofday(&e, NULL); |
2c83567e | 921 | |
9e850933 | 922 | for (i = 0, bytes_done = 0; i < nr; i++) { |
2c83567e JA |
923 | io_u = ev_to_iou(td->aio_events + i); |
924 | ||
f0f3411b | 925 | td->io_blocks++; |
49d2caab JA |
926 | td->io_bytes += io_u->buflen; |
927 | td->this_io_bytes += io_u->buflen; | |
8c033f93 | 928 | |
f0f3411b | 929 | msec = mtime_since(&io_u->issue_time, &e); |
2c83567e | 930 | |
f0f3411b | 931 | add_clat_sample(td, msec); |
645785e5 JA |
932 | add_bw_sample(td); |
933 | ||
62bb4285 | 934 | if (td_write(td)) |
645785e5 | 935 | log_io_piece(td, io_u); |
2c83567e | 936 | |
f4bb2243 | 937 | bytes_done += io_u->buflen; |
2c83567e JA |
938 | put_io_u(td, io_u); |
939 | } | |
9e850933 JA |
940 | |
941 | return bytes_done; | |
2c83567e JA |
942 | } |
943 | ||
944 | static void cleanup_pending_aio(struct thread_data *td) | |
945 | { | |
946 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; | |
947 | struct list_head *entry, *n; | |
948 | struct io_u *io_u; | |
949 | int r; | |
950 | ||
951 | /* | |
952 | * get immediately available events, if any | |
953 | */ | |
1ad72b11 | 954 | r = io_u_getevents(td, 0, td->cur_depth, &ts); |
2c83567e | 955 | if (r > 0) |
f0f3411b | 956 | ios_completed(td, r); |
2c83567e JA |
957 | |
958 | /* | |
959 | * now cancel remaining active events | |
960 | */ | |
961 | list_for_each_safe(entry, n, &td->io_u_busylist) { | |
962 | io_u = list_entry(entry, struct io_u, list); | |
963 | ||
964 | r = io_cancel(td->aio_ctx, &io_u->iocb, td->aio_events); | |
965 | if (!r) | |
966 | put_io_u(td, io_u); | |
967 | } | |
968 | ||
969 | if (td->cur_depth) { | |
1ad72b11 | 970 | r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL); |
2c83567e | 971 | if (r > 0) |
f0f3411b | 972 | ios_completed(td, r); |
2c83567e JA |
973 | } |
974 | } | |
98168d55 | 975 | |
d32d9284 JA |
976 | static int async_do_verify(struct thread_data *td, struct io_u **io_u) |
977 | { | |
978 | struct io_u *v_io_u = *io_u; | |
979 | int ret = 0; | |
980 | ||
981 | if (v_io_u) { | |
645785e5 | 982 | ret = verify_io_u(v_io_u); |
d32d9284 JA |
983 | put_io_u(td, v_io_u); |
984 | *io_u = NULL; | |
985 | } | |
986 | ||
987 | return ret; | |
988 | } | |
989 | ||
91fc5dc9 | 990 | static void do_async_verify(struct thread_data *td) |
cfc702bd | 991 | { |
f4bb2243 | 992 | struct timeval t; |
d32d9284 | 993 | struct io_u *io_u, *v_io_u = NULL; |
645785e5 | 994 | int ret; |
f4bb2243 JA |
995 | |
996 | td_set_runstate(td, TD_VERIFYING); | |
997 | ||
f4bb2243 JA |
998 | do { |
999 | if (td->terminate) | |
1000 | break; | |
1001 | ||
1002 | gettimeofday(&t, NULL); | |
1003 | if (runtime_exceeded(td, &t)) | |
1004 | break; | |
1005 | ||
1006 | io_u = __get_io_u(td); | |
1007 | if (!io_u) | |
1008 | break; | |
1009 | ||
645785e5 JA |
1010 | if (get_next_verify(td, &io_u->offset, &io_u->buflen)) { |
1011 | put_io_u(td, io_u); | |
1012 | break; | |
f4bb2243 JA |
1013 | } |
1014 | ||
1015 | io_prep_pread(&io_u->iocb, td->fd, io_u->buf, io_u->buflen, io_u->offset); | |
1016 | ret = io_u_queue(td, io_u); | |
1017 | if (ret) { | |
1018 | put_io_u(td, io_u); | |
1019 | td->error = ret; | |
1020 | break; | |
1021 | } | |
1022 | ||
f0f3411b JA |
1023 | /* |
1024 | * we have one pending to verify, do that while the next | |
1025 | * we are doing io on the next one | |
1026 | */ | |
d32d9284 JA |
1027 | if (async_do_verify(td, &v_io_u)) |
1028 | break; | |
f0f3411b | 1029 | |
1ad72b11 | 1030 | ret = io_u_getevents(td, 1, 1, NULL); |
f0f3411b JA |
1031 | if (ret != 1) { |
1032 | if (ret < 0) | |
1033 | td->error = ret; | |
f4bb2243 JA |
1034 | break; |
1035 | } | |
1036 | ||
f0f3411b | 1037 | v_io_u = ev_to_iou(td->aio_events); |
f4bb2243 | 1038 | |
645785e5 | 1039 | td->cur_off = v_io_u->offset + v_io_u->buflen; |
f0f3411b JA |
1040 | |
1041 | /* | |
d32d9284 | 1042 | * if we can't submit more io, we need to verify now |
f0f3411b | 1043 | */ |
d32d9284 JA |
1044 | if (queue_full(td) && async_do_verify(td, &v_io_u)) |
1045 | break; | |
1046 | ||
f4bb2243 JA |
1047 | } while (1); |
1048 | ||
d32d9284 | 1049 | async_do_verify(td, &v_io_u); |
f0f3411b | 1050 | |
f4bb2243 JA |
1051 | if (td->cur_depth) |
1052 | cleanup_pending_aio(td); | |
1053 | ||
1054 | td_set_runstate(td, TD_RUNNING); | |
cfc702bd JA |
1055 | } |
1056 | ||
43000118 JA |
1057 | static void do_async_io(struct thread_data *td) |
1058 | { | |
1059 | struct timeval s, e; | |
7889f07b | 1060 | unsigned long usec; |
43000118 | 1061 | |
49d2caab | 1062 | while (td->this_io_bytes < td->io_size) { |
43000118 JA |
1063 | struct timespec ts = { .tv_sec = 0, .tv_nsec = 0}; |
1064 | struct timespec *timeout; | |
2c83567e JA |
1065 | int ret, min_evts = 0; |
1066 | struct io_u *io_u; | |
9e850933 | 1067 | unsigned int bytes_done; |
43000118 JA |
1068 | |
1069 | if (td->terminate) | |
1070 | break; | |
1071 | ||
2c83567e | 1072 | io_u = get_io_u(td); |
7889f07b JA |
1073 | if (!io_u) |
1074 | break; | |
43000118 | 1075 | |
57d753e3 | 1076 | memcpy(&s, &io_u->start_time, sizeof(s)); |
8baf1bcc | 1077 | |
2c83567e | 1078 | ret = io_u_queue(td, io_u); |
56b0eff0 | 1079 | if (ret) { |
a3fdb993 | 1080 | put_io_u(td, io_u); |
a592bd33 | 1081 | td->error = ret; |
43000118 JA |
1082 | break; |
1083 | } | |
1084 | ||
57d753e3 JA |
1085 | gettimeofday(&io_u->issue_time, NULL); |
1086 | add_slat_sample(td, mtime_since(&io_u->start_time, &io_u->issue_time)); | |
2c83567e | 1087 | if (td->cur_depth < td->aio_depth) { |
43000118 JA |
1088 | timeout = &ts; |
1089 | min_evts = 0; | |
1090 | } else { | |
1091 | timeout = NULL; | |
1092 | min_evts = 1; | |
1093 | } | |
1094 | ||
1ad72b11 | 1095 | ret = io_u_getevents(td, min_evts, td->cur_depth, timeout); |
43000118 | 1096 | if (ret < 0) { |
406e7b7c | 1097 | td->error = ret; |
43000118 JA |
1098 | break; |
1099 | } else if (!ret) | |
1100 | continue; | |
1101 | ||
f0f3411b | 1102 | bytes_done = ios_completed(td, ret); |
43000118 | 1103 | |
98168d55 JA |
1104 | /* |
1105 | * the rate is batched for now, it should work for batches | |
1106 | * of completions except the very first one which may look | |
1107 | * a little bursty | |
1108 | */ | |
2c83567e | 1109 | gettimeofday(&e, NULL); |
43000118 JA |
1110 | usec = utime_since(&s, &e); |
1111 | ||
9e850933 | 1112 | rate_throttle(td, usec, bytes_done); |
43000118 JA |
1113 | |
1114 | if (check_min_rate(td, &e)) { | |
1115 | td->error = ENODATA; | |
1116 | break; | |
1117 | } | |
67903a2e JA |
1118 | |
1119 | if (runtime_exceeded(td, &e)) | |
1120 | break; | |
765d9223 JA |
1121 | |
1122 | if (td->thinktime) | |
d15c5195 | 1123 | usec_sleep(td, td->thinktime); |
cdf92433 JA |
1124 | |
1125 | if (should_fsync(td) && td->fsync_blocks && | |
1126 | (td->io_blocks % td->fsync_blocks) == 0) | |
1127 | fsync(td->fd); | |
43000118 | 1128 | } |
56b0eff0 | 1129 | |
2c83567e JA |
1130 | if (td->cur_depth) |
1131 | cleanup_pending_aio(td); | |
4ac89145 JA |
1132 | |
1133 | if (should_fsync(td)) | |
1134 | fsync(td->fd); | |
56b0eff0 JA |
1135 | } |
1136 | ||
1137 | static void cleanup_aio(struct thread_data *td) | |
1138 | { | |
254605cd JA |
1139 | io_destroy(td->aio_ctx); |
1140 | ||
43000118 JA |
1141 | if (td->aio_events) |
1142 | free(td->aio_events); | |
43000118 JA |
1143 | } |
1144 | ||
1145 | static int init_aio(struct thread_data *td) | |
1146 | { | |
254605cd | 1147 | if (io_queue_init(td->aio_depth, &td->aio_ctx)) { |
43000118 JA |
1148 | td->error = errno; |
1149 | return 1; | |
1150 | } | |
1151 | ||
43000118 | 1152 | td->aio_events = malloc(td->aio_depth * sizeof(struct io_event)); |
43000118 JA |
1153 | return 0; |
1154 | } | |
1155 | ||
2c83567e JA |
1156 | static void cleanup_io_u(struct thread_data *td) |
1157 | { | |
1158 | struct list_head *entry, *n; | |
1159 | struct io_u *io_u; | |
1160 | ||
1161 | list_for_each_safe(entry, n, &td->io_u_freelist) { | |
1162 | io_u = list_entry(entry, struct io_u, list); | |
1163 | ||
1164 | list_del(&io_u->list); | |
2c83567e JA |
1165 | free(io_u); |
1166 | } | |
6b71c826 | 1167 | |
99c6704f JA |
1168 | if (td->mem_type == MEM_MALLOC) |
1169 | free(td->orig_buffer); | |
1170 | else if (td->mem_type == MEM_SHM) { | |
1171 | struct shmid_ds sbuf; | |
1172 | ||
1173 | shmdt(td->orig_buffer); | |
1174 | shmctl(td->shm_id, IPC_RMID, &sbuf); | |
891e70f8 JA |
1175 | } else if (td->mem_type == MEM_MMAP) |
1176 | munmap(td->orig_buffer, td->orig_buffer_size); | |
1177 | else | |
1178 | fprintf(stderr, "Bad memory type %d\n", td->mem_type); | |
1179 | ||
1180 | td->orig_buffer = NULL; | |
2c83567e JA |
1181 | } |
1182 | ||
99c6704f | 1183 | static int init_io_u(struct thread_data *td) |
2c83567e JA |
1184 | { |
1185 | struct io_u *io_u; | |
891e70f8 | 1186 | int i, max_units; |
6b71c826 | 1187 | char *p; |
2c83567e JA |
1188 | |
1189 | if (!td->use_aio) | |
1190 | max_units = 1; | |
1191 | else | |
1192 | max_units = td->aio_depth; | |
1193 | ||
891e70f8 | 1194 | td->orig_buffer_size = td->max_bs * max_units + MASK; |
99c6704f JA |
1195 | |
1196 | if (td->mem_type == MEM_MALLOC) | |
891e70f8 | 1197 | td->orig_buffer = malloc(td->orig_buffer_size); |
99c6704f | 1198 | else if (td->mem_type == MEM_SHM) { |
891e70f8 | 1199 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600); |
99c6704f JA |
1200 | if (td->shm_id < 0) { |
1201 | td->error = errno; | |
1202 | perror("shmget"); | |
1203 | return 1; | |
1204 | } | |
1205 | ||
1206 | td->orig_buffer = shmat(td->shm_id, NULL, 0); | |
1207 | if (td->orig_buffer == (void *) -1) { | |
1208 | td->error = errno; | |
1209 | perror("shmat"); | |
891e70f8 JA |
1210 | td->orig_buffer = NULL; |
1211 | return 1; | |
1212 | } | |
1213 | } else if (td->mem_type == MEM_MMAP) { | |
1214 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); | |
1215 | if (td->orig_buffer == MAP_FAILED) { | |
1216 | td->error = errno; | |
1217 | perror("mmap"); | |
1218 | td->orig_buffer = NULL; | |
99c6704f JA |
1219 | return 1; |
1220 | } | |
1221 | } | |
6b71c826 | 1222 | |
2c83567e JA |
1223 | INIT_LIST_HEAD(&td->io_u_freelist); |
1224 | INIT_LIST_HEAD(&td->io_u_busylist); | |
645785e5 | 1225 | INIT_LIST_HEAD(&td->io_hist_list); |
2c83567e | 1226 | |
99c6704f | 1227 | p = ALIGN(td->orig_buffer); |
2c83567e JA |
1228 | for (i = 0; i < max_units; i++) { |
1229 | io_u = malloc(sizeof(*io_u)); | |
1230 | memset(io_u, 0, sizeof(*io_u)); | |
1231 | INIT_LIST_HEAD(&io_u->list); | |
1232 | ||
7889f07b | 1233 | io_u->buf = p + td->max_bs * i; |
2c83567e JA |
1234 | list_add(&io_u->list, &td->io_u_freelist); |
1235 | } | |
99c6704f JA |
1236 | |
1237 | return 0; | |
2c83567e JA |
1238 | } |
1239 | ||
02983297 JA |
1240 | static int create_file(struct thread_data *td) |
1241 | { | |
7889f07b | 1242 | unsigned long long left; |
645785e5 | 1243 | unsigned int bs; |
02983297 | 1244 | char *b; |
645785e5 | 1245 | int r; |
02983297 | 1246 | |
02983297 JA |
1247 | /* |
1248 | * unless specifically asked for overwrite, let normal io extend it | |
1249 | */ | |
62bb4285 | 1250 | if (td_write(td) && !td->overwrite) |
02983297 JA |
1251 | return 0; |
1252 | ||
57d753e3 JA |
1253 | if (!td->file_size) { |
1254 | fprintf(stderr, "Need size for create\n"); | |
1255 | td->error = EINVAL; | |
1256 | return 1; | |
1257 | } | |
1258 | ||
42fd89a7 JA |
1259 | printf("Client%d: Laying out IO file\n", td->thread_number); |
1260 | ||
02983297 JA |
1261 | td->fd = open(td->file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
1262 | if (td->fd < 0) { | |
1263 | td->error = errno; | |
1264 | return 1; | |
1265 | } | |
1266 | ||
c94deb1c JA |
1267 | if (ftruncate(td->fd, td->file_size) == -1) { |
1268 | td->error = errno; | |
1269 | return 1; | |
1270 | } | |
1271 | ||
49d2caab | 1272 | td->io_size = td->file_size; |
7889f07b JA |
1273 | b = malloc(td->max_bs); |
1274 | memset(b, 0, td->max_bs); | |
1275 | ||
1276 | left = td->file_size; | |
1277 | while (left) { | |
1278 | bs = td->max_bs; | |
1279 | if (bs > left) | |
1280 | bs = left; | |
02983297 | 1281 | |
7889f07b | 1282 | r = write(td->fd, b, bs); |
02983297 | 1283 | |
645785e5 | 1284 | if (r == (int) bs) { |
7889f07b | 1285 | left -= bs; |
02983297 | 1286 | continue; |
7889f07b | 1287 | } else { |
02983297 JA |
1288 | if (r < 0) |
1289 | td->error = errno; | |
1290 | else | |
1291 | td->error = EIO; | |
1292 | ||
1293 | break; | |
1294 | } | |
1295 | } | |
1296 | ||
fc097bfe JA |
1297 | if (td->create_fsync) |
1298 | fsync(td->fd); | |
1299 | ||
02983297 JA |
1300 | close(td->fd); |
1301 | td->fd = -1; | |
1302 | free(b); | |
1303 | return 0; | |
1304 | } | |
1305 | ||
1306 | static int file_exists(struct thread_data *td) | |
1307 | { | |
1308 | struct stat st; | |
1309 | ||
1310 | if (stat(td->file_name, &st) != -1) | |
1311 | return 1; | |
1312 | ||
1313 | return errno != ENOENT; | |
1314 | } | |
1315 | ||
c4c8f7b3 | 1316 | static int file_size(struct thread_data *td) |
02983297 JA |
1317 | { |
1318 | struct stat st; | |
c94deb1c JA |
1319 | |
1320 | if (fstat(td->fd, &st) == -1) { | |
1321 | td->error = errno; | |
1322 | return 1; | |
1323 | } | |
1324 | ||
c94deb1c | 1325 | if (td_read(td)) { |
091d845a | 1326 | if (!td->file_size || (st.st_size > td->file_size)) |
c4c8f7b3 | 1327 | td->file_size = st.st_size; |
c94deb1c JA |
1328 | } else { |
1329 | if (!td->file_size) | |
1330 | td->file_size = 1024 * 1024 * 1024; | |
c4c8f7b3 JA |
1331 | } |
1332 | ||
1333 | return 0; | |
1334 | } | |
1335 | ||
1336 | static int bdev_size(struct thread_data *td) | |
1337 | { | |
1338 | size_t bytes; | |
c94deb1c | 1339 | |
c4c8f7b3 JA |
1340 | if (ioctl(td->fd, BLKGETSIZE64, &bytes) < 0) { |
1341 | td->error = errno; | |
1342 | return 1; | |
c94deb1c JA |
1343 | } |
1344 | ||
091d845a | 1345 | if (!td->file_size || (bytes > td->file_size)) |
c4c8f7b3 JA |
1346 | td->file_size = bytes; |
1347 | ||
1348 | return 0; | |
1349 | } | |
1350 | ||
1351 | static int get_file_size(struct thread_data *td) | |
1352 | { | |
1353 | int ret; | |
1354 | ||
1355 | if (td->filetype == FIO_TYPE_FILE) | |
1356 | ret = file_size(td); | |
1357 | else | |
1358 | ret = bdev_size(td); | |
1359 | ||
1360 | if (ret) | |
1361 | return ret; | |
1362 | ||
1363 | if (td->file_offset > td->file_size) { | |
091d845a | 1364 | fprintf(stderr, "Client%d: offset larger than length (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->file_size); |
c94deb1c JA |
1365 | return 1; |
1366 | } | |
1367 | ||
c4c8f7b3 | 1368 | td->io_size = td->file_size - td->file_offset; |
c94deb1c JA |
1369 | if (td->io_size == 0) { |
1370 | fprintf(stderr, "Client%d: no io blocks\n", td->thread_number); | |
1371 | td->error = EINVAL; | |
1372 | return 1; | |
1373 | } | |
1374 | ||
1375 | return 0; | |
1376 | } | |
1377 | ||
6e2c38cc JA |
1378 | static int setup_file_mmap(struct thread_data *td) |
1379 | { | |
1380 | int flags; | |
1381 | ||
1382 | if (td_read(td)) | |
1383 | flags = PROT_READ; | |
1384 | else { | |
1385 | flags = PROT_WRITE; | |
1386 | ||
7f46ef08 | 1387 | if (td->verify != VERIFY_NONE) |
6e2c38cc JA |
1388 | flags |= PROT_READ; |
1389 | } | |
1390 | ||
1391 | td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset); | |
1392 | if (td->mmap == MAP_FAILED) { | |
1393 | td->mmap = NULL; | |
1394 | td->error = errno; | |
1395 | return 1; | |
1396 | } | |
1397 | ||
1398 | if (td->invalidate_cache) { | |
1399 | if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) { | |
1400 | td->error = errno; | |
1401 | return 1; | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | if (td->sequential) { | |
1406 | if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) { | |
1407 | td->error = errno; | |
1408 | return 1; | |
1409 | } | |
1410 | } else { | |
1411 | if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) { | |
1412 | td->error = errno; | |
1413 | return 1; | |
1414 | } | |
1415 | } | |
1416 | ||
1417 | return 0; | |
1418 | } | |
1419 | ||
1420 | static int setup_file_plain(struct thread_data *td) | |
1421 | { | |
1422 | if (td->invalidate_cache) { | |
1423 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) { | |
1424 | td->error = errno; | |
1425 | return 1; | |
1426 | } | |
1427 | } | |
1428 | ||
1429 | if (td->sequential) { | |
1430 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) { | |
1431 | td->error = errno; | |
1432 | return 1; | |
1433 | } | |
1434 | } else { | |
1435 | if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) { | |
1436 | td->error = errno; | |
1437 | return 1; | |
1438 | } | |
1439 | } | |
1440 | ||
1441 | return 0; | |
1442 | } | |
1443 | ||
c94deb1c JA |
1444 | static int setup_file(struct thread_data *td) |
1445 | { | |
02983297 JA |
1446 | int flags = 0; |
1447 | ||
1448 | if (!file_exists(td)) { | |
1449 | if (!td->create_file) { | |
1450 | td->error = ENOENT; | |
1451 | return 1; | |
1452 | } | |
1453 | if (create_file(td)) | |
1454 | return 1; | |
1455 | } | |
1456 | ||
1457 | if (td->odirect) | |
1458 | flags |= O_DIRECT; | |
1459 | ||
1460 | if (td_read(td)) | |
1461 | td->fd = open(td->file_name, flags | O_RDONLY); | |
1462 | else { | |
1463 | if (!td->overwrite) | |
1464 | flags |= O_TRUNC; | |
74b4b5fb JA |
1465 | if (td->sync_io) |
1466 | flags |= O_SYNC; | |
6e2c38cc JA |
1467 | |
1468 | flags |= O_RDWR; | |
02983297 | 1469 | |
e8457004 | 1470 | td->fd = open(td->file_name, flags | O_CREAT, 0600); |
02983297 JA |
1471 | } |
1472 | ||
1473 | if (td->fd == -1) { | |
1474 | td->error = errno; | |
1475 | return 1; | |
1476 | } | |
1477 | ||
c94deb1c | 1478 | if (get_file_size(td)) |
49d2caab | 1479 | return 1; |
49d2caab | 1480 | |
62bb4285 | 1481 | if (td_write(td) && ftruncate(td->fd, td->file_size) == -1) { |
c94deb1c | 1482 | td->error = errno; |
02983297 JA |
1483 | return 1; |
1484 | } | |
1485 | ||
6e2c38cc JA |
1486 | if (!td->use_mmap) |
1487 | return setup_file_plain(td); | |
1488 | else | |
1489 | return setup_file_mmap(td); | |
02983297 JA |
1490 | } |
1491 | ||
debf703a JA |
1492 | static int check_dev_match(dev_t dev, char *path) |
1493 | { | |
1494 | int major, minor; | |
1495 | char line[256], *p; | |
1496 | FILE *f; | |
1497 | ||
1498 | f = fopen(path, "r"); | |
1499 | if (!f) { | |
1500 | perror("open path"); | |
1501 | return 1; | |
1502 | } | |
1503 | ||
1504 | p = fgets(line, sizeof(line), f); | |
1505 | if (!p) { | |
1506 | fclose(f); | |
1507 | return 1; | |
1508 | } | |
1509 | ||
1510 | if (sscanf(p, "%u:%u", &major, &minor) != 2) { | |
1511 | fclose(f); | |
1512 | return 1; | |
1513 | } | |
1514 | ||
1515 | if (((major << 8) | minor) == dev) { | |
1516 | fclose(f); | |
1517 | return 0; | |
1518 | } | |
1519 | ||
1520 | fclose(f); | |
1521 | return 1; | |
1522 | } | |
1523 | ||
1524 | static char *find_block_dir(dev_t dev, char *path) | |
1525 | { | |
1526 | struct dirent *dir; | |
1527 | char *found = NULL; | |
1528 | struct stat st; | |
1529 | DIR *D; | |
1530 | ||
1531 | D = opendir(path); | |
1532 | if (!D) | |
1533 | return NULL; | |
1534 | ||
1535 | while ((dir = readdir(D)) != NULL) { | |
1536 | char full_path[256]; | |
1537 | ||
1538 | if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) | |
1539 | continue; | |
1540 | if (!strcmp(dir->d_name, "device")) | |
1541 | continue; | |
1542 | ||
1543 | sprintf(full_path, "%s/%s", path, dir->d_name); | |
1544 | ||
1545 | if (!strcmp(dir->d_name, "dev")) { | |
1546 | if (!check_dev_match(dev, full_path)) { | |
1547 | found = path; | |
1548 | break; | |
1549 | } | |
1550 | } | |
1551 | ||
1552 | if (stat(full_path, &st) == -1) { | |
1553 | perror("stat"); | |
1554 | break; | |
1555 | } | |
1556 | ||
1557 | if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode)) | |
1558 | continue; | |
1559 | ||
1560 | if ((found = find_block_dir(dev, full_path)) != NULL) | |
1561 | break; | |
1562 | } | |
1563 | ||
1564 | closedir(D); | |
1565 | return found; | |
1566 | } | |
1567 | ||
6f75d67c | 1568 | static int get_io_ticks(struct disk_util *du) |
debf703a JA |
1569 | { |
1570 | int i1, i2, i3, i4, i5, i6, i7, i8, i9; | |
1571 | unsigned long long ull1, ull2; | |
1572 | char line[256]; | |
1573 | FILE *f; | |
1574 | char *p; | |
1575 | ||
6f75d67c | 1576 | f = fopen(du->path, "r"); |
debf703a JA |
1577 | if (!f) |
1578 | return 0; | |
1579 | ||
1580 | p = fgets(line, sizeof(line), f); | |
1581 | if (!p) { | |
1582 | fclose(f); | |
1583 | return 0; | |
1584 | } | |
1585 | ||
1586 | if (sscanf(p, "%8u %8u %8llu %8u %8u %8u %8llu %8u %8u %8u %8u", &i1, &i2, &ull1, &i3, &i4, &i5, &ull2, &i6, &i7, &i8, &i9) != 11) { | |
1587 | fclose(f); | |
1588 | return 0; | |
1589 | } | |
1590 | ||
1591 | fclose(f); | |
1592 | return i8; | |
1593 | } | |
1594 | ||
6f75d67c | 1595 | static void update_io_tick_disk(struct disk_util *du) |
debf703a | 1596 | { |
6f75d67c JA |
1597 | unsigned long ticks = get_io_ticks(du); |
1598 | struct timeval t; | |
1599 | ||
1600 | du->io_ticks += (ticks - du->last_io_ticks); | |
1601 | du->last_io_ticks = ticks; | |
1602 | gettimeofday(&t, NULL); | |
1603 | du->msec += mtime_since(&du->time, &t); | |
1604 | memcpy(&du->time, &t, sizeof(t)); | |
1605 | } | |
1606 | ||
1607 | static void update_io_ticks(void) | |
1608 | { | |
1609 | struct list_head *entry; | |
1610 | struct disk_util *du; | |
1611 | ||
1612 | list_for_each(entry, &disk_list) { | |
1613 | du = list_entry(entry, struct disk_util, list); | |
1614 | update_io_tick_disk(du); | |
1615 | } | |
1616 | } | |
1617 | ||
1618 | static int disk_util_exists(dev_t dev) | |
1619 | { | |
1620 | struct list_head *entry; | |
1621 | struct disk_util *du; | |
1622 | ||
1623 | list_for_each(entry, &disk_list) { | |
1624 | du = list_entry(entry, struct disk_util, list); | |
1625 | ||
1626 | if (du->dev == dev) | |
1627 | return 1; | |
1628 | } | |
debf703a | 1629 | |
6f75d67c | 1630 | return 0; |
debf703a JA |
1631 | } |
1632 | ||
6f75d67c JA |
1633 | static void disk_util_add(dev_t dev, char *path) |
1634 | { | |
1635 | struct disk_util *du = malloc(sizeof(*du)); | |
1636 | ||
1637 | memset(du, 0, sizeof(*du)); | |
1638 | INIT_LIST_HEAD(&du->list); | |
1639 | sprintf(du->path, "%s/stat", path); | |
1640 | du->name = basename(path); | |
1641 | du->dev = dev; | |
1642 | ||
1643 | gettimeofday(&du->time, NULL); | |
1644 | du->last_io_ticks = get_io_ticks(du); | |
1645 | ||
1646 | list_add_tail(&du->list, &disk_list); | |
1647 | } | |
1648 | ||
1649 | static void init_disk_util(struct thread_data *td) | |
debf703a JA |
1650 | { |
1651 | struct stat st; | |
1652 | char foo[256], tmp[256]; | |
1653 | dev_t dev; | |
1654 | char *p, *dir; | |
1655 | ||
6f75d67c JA |
1656 | if (!stat(td->file_name, &st)) { |
1657 | if (S_ISBLK(st.st_mode)) | |
1658 | dev = st.st_rdev; | |
1659 | else | |
1660 | dev = st.st_dev; | |
1661 | } else { | |
1662 | /* | |
1663 | * must be a file, open "." in that path | |
1664 | */ | |
8cf48a05 JA |
1665 | strcpy(foo, td->file_name); |
1666 | p = dirname(foo); | |
6f75d67c JA |
1667 | if (stat(p, &st)) { |
1668 | perror("disk util stat"); | |
1669 | return; | |
1670 | } | |
debf703a | 1671 | |
debf703a | 1672 | dev = st.st_dev; |
6f75d67c JA |
1673 | } |
1674 | ||
1675 | if (disk_util_exists(dev)) | |
1676 | return; | |
debf703a JA |
1677 | |
1678 | sprintf(foo, "/sys/block"); | |
1679 | dir = find_block_dir(dev, foo); | |
1680 | if (!dir) | |
6f75d67c | 1681 | return; |
debf703a JA |
1682 | |
1683 | /* | |
1684 | * if this is inside a partition dir, jump back to parent | |
1685 | */ | |
1686 | sprintf(tmp, "%s/queue", dir); | |
1687 | if (stat(tmp, &st)) { | |
1688 | p = dirname(dir); | |
1689 | sprintf(tmp, "%s/queue", p); | |
1690 | if (stat(tmp, &st)) { | |
1691 | fprintf(stderr, "unknown sysfs layout\n"); | |
6f75d67c | 1692 | return; |
debf703a | 1693 | } |
6f75d67c | 1694 | sprintf(foo, "%s", p); |
debf703a JA |
1695 | } |
1696 | ||
6f75d67c JA |
1697 | disk_util_add(dev, foo); |
1698 | } | |
1699 | ||
1700 | static void disk_util_timer_arm(void) | |
1701 | { | |
1702 | struct itimerval itimer; | |
1703 | ||
1704 | itimer.it_value.tv_sec = 0; | |
1705 | itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000; | |
1706 | setitimer(ITIMER_REAL, &itimer, NULL); | |
1707 | } | |
1708 | ||
d32d9284 JA |
1709 | static void clear_io_state(struct thread_data *td) |
1710 | { | |
9d0c6ca2 JA |
1711 | if (!td->use_aio) |
1712 | lseek(td->fd, SEEK_SET, 0); | |
1713 | ||
d32d9284 | 1714 | td->cur_off = 0; |
49d2caab JA |
1715 | td->last_bytes = 0; |
1716 | td->stat_io_bytes = 0; | |
1717 | td->this_io_bytes = 0; | |
1718 | ||
1719 | if (td->file_map) | |
1720 | memset(td->file_map, 0, td->num_maps * sizeof(long)); | |
d32d9284 JA |
1721 | } |
1722 | ||
f6dcd824 JA |
1723 | static void update_rusage_stat(struct thread_data *td) |
1724 | { | |
1725 | if (!td->runtime) | |
1726 | return; | |
1727 | ||
1728 | getrusage(RUSAGE_SELF, &td->ru_end); | |
1729 | ||
1730 | td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime); | |
1731 | td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime); | |
1732 | td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw); | |
1733 | ||
1734 | ||
1735 | memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end)); | |
1736 | } | |
1737 | ||
189873de | 1738 | static void *thread_main(void *data) |
892199bd | 1739 | { |
189873de | 1740 | struct thread_data *td = data; |
02983297 | 1741 | int ret = 1; |
892199bd | 1742 | |
7292613b | 1743 | setsid(); |
892199bd JA |
1744 | td->pid = getpid(); |
1745 | ||
99c6704f JA |
1746 | if (init_io_u(td)) |
1747 | goto err; | |
2c83567e | 1748 | |
18e0b78c JA |
1749 | if (sched_setaffinity(td->pid, sizeof(td->cpumask), &td->cpumask) == -1) { |
1750 | td->error = errno; | |
1751 | goto err; | |
1752 | } | |
1753 | ||
43000118 JA |
1754 | if (td->use_aio && init_aio(td)) |
1755 | goto err; | |
1756 | ||
f737299d | 1757 | if (td->ioprio) { |
892199bd JA |
1758 | if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) { |
1759 | td->error = errno; | |
599002b3 | 1760 | goto err; |
892199bd JA |
1761 | } |
1762 | } | |
1763 | ||
1764 | sem_post(&startup_sem); | |
1765 | sem_wait(&td->mutex); | |
43000118 | 1766 | |
fc097bfe JA |
1767 | if (!td->create_serialize && setup_file(td)) |
1768 | goto err; | |
1769 | ||
49d2caab JA |
1770 | if (init_random_state(td)) |
1771 | goto err; | |
1772 | ||
293753bb | 1773 | while (td->loops--) { |
f6dcd824 JA |
1774 | getrusage(RUSAGE_SELF, &td->ru_start); |
1775 | gettimeofday(&td->start, NULL); | |
1776 | memcpy(&td->stat_sample_time, &td->start, sizeof(td->start)); | |
293753bb JA |
1777 | |
1778 | if (td->ratemin) | |
1779 | memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate)); | |
7292613b | 1780 | |
d32d9284 | 1781 | clear_io_state(td); |
9d0c6ca2 | 1782 | prune_io_piece_log(td); |
fd1ae4c9 | 1783 | |
b2de0ed2 | 1784 | if (!td->use_aio) |
b6794fbf | 1785 | do_sync_io(td); |
b2de0ed2 JA |
1786 | else |
1787 | do_async_io(td); | |
1788 | ||
91fc5dc9 JA |
1789 | if (td->error) |
1790 | break; | |
1791 | ||
f6dcd824 JA |
1792 | td->runtime += mtime_since_now(&td->start); |
1793 | update_rusage_stat(td); | |
1794 | ||
7f46ef08 | 1795 | if (td->verify == VERIFY_NONE) |
b2de0ed2 | 1796 | continue; |
cfc702bd | 1797 | |
b2de0ed2 | 1798 | clear_io_state(td); |
d32d9284 | 1799 | |
91fc5dc9 JA |
1800 | if (!td->use_aio) |
1801 | do_sync_verify(td); | |
1802 | else | |
1803 | do_async_verify(td); | |
1804 | ||
1805 | if (td->error) | |
1806 | break; | |
b6794fbf | 1807 | } |
7292613b | 1808 | |
892199bd | 1809 | ret = 0; |
a0a9b35b JA |
1810 | |
1811 | if (td->bw_log) | |
1812 | finish_log(td, td->bw_log, "bw"); | |
1813 | if (td->lat_log) | |
1814 | finish_log(td, td->lat_log, "lat"); | |
4ac89145 | 1815 | |
98dd52d6 | 1816 | if (exitall_on_terminate) |
27c32a38 | 1817 | terminate_threads(td->groupid); |
98dd52d6 | 1818 | |
892199bd | 1819 | err: |
7292613b JA |
1820 | if (td->fd != -1) { |
1821 | close(td->fd); | |
1822 | td->fd = -1; | |
1823 | } | |
6e2c38cc JA |
1824 | if (td->mmap) |
1825 | munmap(td->mmap, td->file_size); | |
4ac89145 JA |
1826 | if (td->use_aio) |
1827 | cleanup_aio(td); | |
2c83567e | 1828 | cleanup_io_u(td); |
599002b3 | 1829 | if (ret) { |
892199bd | 1830 | sem_post(&startup_sem); |
599002b3 JA |
1831 | sem_wait(&td->mutex); |
1832 | } | |
40ef7f64 | 1833 | td_set_runstate(td, TD_EXITED); |
189873de JA |
1834 | return NULL; |
1835 | ||
1836 | } | |
1837 | ||
1838 | static void *fork_main(int shm_id, int offset) | |
1839 | { | |
1840 | struct thread_data *td; | |
1841 | void *data; | |
1842 | ||
1843 | data = shmat(shm_id, NULL, 0); | |
1844 | if (data == (void *) -1) { | |
1845 | perror("shmat"); | |
1846 | return NULL; | |
1847 | } | |
1848 | ||
1849 | td = data + offset * sizeof(struct thread_data); | |
1850 | thread_main(td); | |
4240cfa1 | 1851 | shmdt(data); |
892199bd JA |
1852 | return NULL; |
1853 | } | |
1854 | ||
57d753e3 JA |
1855 | static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max, |
1856 | double *mean, double *dev) | |
1857 | { | |
1858 | double n; | |
1859 | ||
1860 | if (is->samples == 0) | |
1861 | return 0; | |
1862 | ||
1863 | *min = is->min_val; | |
1864 | *max = is->max_val; | |
1865 | ||
1866 | n = (double) is->samples; | |
1867 | *mean = (double) is->val / n; | |
1868 | *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1)); | |
1869 | return 1; | |
1870 | } | |
1871 | ||
557e4102 JA |
1872 | static void show_thread_status(struct thread_data *td, |
1873 | struct group_run_stats *rs) | |
892199bd JA |
1874 | { |
1875 | int prio, prio_class; | |
f6dcd824 | 1876 | unsigned long min, max, bw = 0; |
92b229ed | 1877 | double mean, dev, usr_cpu, sys_cpu; |
892199bd | 1878 | |
49d2caab | 1879 | if (!td->io_bytes && !td->error) |
213b446c JA |
1880 | return; |
1881 | ||
892199bd | 1882 | if (td->runtime) |
49d2caab | 1883 | bw = td->io_bytes / td->runtime; |
892199bd JA |
1884 | |
1885 | prio = td->ioprio & 0xff; | |
1886 | prio_class = td->ioprio >> IOPRIO_CLASS_SHIFT; | |
1887 | ||
f6dcd824 | 1888 | printf("Client%d (g=%d): err=%2d, io=%6luMiB, bw=%6luKiB/s, runt=%6lumsec\n", td->thread_number, td->groupid, td->error, td->io_bytes >> 20, bw, td->runtime); |
fd1ae4c9 | 1889 | |
57d753e3 JA |
1890 | if (calc_lat(&td->slat_stat, &min, &max, &mean, &dev)) |
1891 | printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
1892 | if (calc_lat(&td->clat_stat, &min, &max, &mean, &dev)) | |
1893 | printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev); | |
557e4102 JA |
1894 | if (calc_lat(&td->bw_stat, &min, &max, &mean, &dev)) { |
1895 | double p_of_agg; | |
1896 | ||
1897 | p_of_agg = mean * 100 / (double) rs->agg[td->ddir]; | |
1898 | printf(" bw (KiB/s) : min=%5lu, max=%5lu, per=%3.2f%%, avg=%5.02f, dev=%5.02f\n", min, max, p_of_agg, mean, dev); | |
1899 | } | |
92b229ed JA |
1900 | |
1901 | if (td->runtime) { | |
f6dcd824 JA |
1902 | usr_cpu = (double) td->usr_time * 100 / (double) td->runtime; |
1903 | sys_cpu = (double) td->sys_time * 100 / (double) td->runtime; | |
92b229ed JA |
1904 | } else { |
1905 | usr_cpu = 0; | |
1906 | sys_cpu = 0; | |
1907 | } | |
1908 | ||
f6dcd824 | 1909 | printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx); |
892199bd JA |
1910 | } |
1911 | ||
3f39453a | 1912 | static void print_thread_status(struct thread_data *td, int nr_running, |
8dbff0b1 | 1913 | int t_rate, int m_rate) |
3f39453a | 1914 | { |
3f39453a JA |
1915 | printf("Threads now running: %d", nr_running); |
1916 | if (m_rate || t_rate) | |
1917 | printf(", commitrate %d/%dKiB/sec", t_rate, m_rate); | |
8dbff0b1 JA |
1918 | printf(" : [%s]\r", run_str); |
1919 | fflush(stdout); | |
3f39453a JA |
1920 | } |
1921 | ||
40ef7f64 JA |
1922 | static void check_str_update(struct thread_data *td, int n, int t, int m) |
1923 | { | |
1924 | char c = run_str[td->thread_number - 1]; | |
1925 | ||
1926 | if (td->runstate == td->old_runstate) | |
1927 | return; | |
1928 | ||
1929 | switch (td->runstate) { | |
1930 | case TD_REAPED: | |
1931 | c = '_'; | |
1932 | break; | |
f4bb2243 JA |
1933 | case TD_EXITED: |
1934 | c = 'E'; | |
1935 | break; | |
40ef7f64 | 1936 | case TD_RUNNING: |
af678352 JA |
1937 | if (td_read(td)) { |
1938 | if (td->sequential) | |
1939 | c = 'R'; | |
1940 | else | |
1941 | c = 'r'; | |
1942 | } else { | |
1943 | if (td->sequential) | |
1944 | c = 'W'; | |
1945 | else | |
1946 | c = 'w'; | |
1947 | } | |
40ef7f64 JA |
1948 | break; |
1949 | case TD_VERIFYING: | |
1950 | c = 'V'; | |
1951 | break; | |
1952 | case TD_CREATED: | |
1953 | c = 'C'; | |
1954 | break; | |
1955 | case TD_NOT_CREATED: | |
1956 | c = 'P'; | |
1957 | break; | |
1958 | default: | |
1959 | printf("state %d\n", td->runstate); | |
1960 | } | |
1961 | ||
1962 | run_str[td->thread_number - 1] = c; | |
1963 | print_thread_status(td, n, t, m); | |
1964 | td->old_runstate = td->runstate; | |
1965 | } | |
1966 | ||
213b446c | 1967 | static void reap_threads(int *nr_running, int *t_rate, int *m_rate) |
02bdd9ba | 1968 | { |
213b446c | 1969 | int i; |
02bdd9ba | 1970 | |
3f39453a JA |
1971 | /* |
1972 | * reap exited threads (TD_EXITED -> TD_REAPED) | |
1973 | */ | |
02bdd9ba JA |
1974 | for (i = 0; i < thread_number; i++) { |
1975 | struct thread_data *td = &threads[i]; | |
1976 | ||
40ef7f64 JA |
1977 | check_str_update(td, *nr_running, *t_rate, *m_rate); |
1978 | ||
213b446c JA |
1979 | if (td->runstate != TD_EXITED) |
1980 | continue; | |
02bdd9ba | 1981 | |
40ef7f64 | 1982 | td_set_runstate(td, TD_REAPED); |
189873de JA |
1983 | |
1984 | if (td->use_thread) { | |
1985 | long ret; | |
1986 | ||
1987 | if (pthread_join(td->thread, (void *) &ret)) | |
1988 | perror("thread_join"); | |
1989 | } else | |
1990 | waitpid(td->pid, NULL, 0); | |
1991 | ||
213b446c JA |
1992 | (*nr_running)--; |
1993 | (*m_rate) -= td->ratemin; | |
1994 | (*t_rate) -= td->rate; | |
40ef7f64 | 1995 | check_str_update(td, *nr_running, *t_rate, *m_rate); |
213b446c | 1996 | } |
02bdd9ba JA |
1997 | } |
1998 | ||
fc24389f JA |
1999 | static void run_threads(char *argv[]) |
2000 | { | |
be33abe4 | 2001 | struct timeval genesis; |
fc24389f JA |
2002 | struct thread_data *td; |
2003 | unsigned long spent; | |
2a81240d | 2004 | int i, todo, nr_running, m_rate, t_rate, nr_started; |
fc24389f | 2005 | |
fc24389f JA |
2006 | printf("Starting %d threads\n", thread_number); |
2007 | fflush(stdout); | |
2008 | ||
7292613b | 2009 | signal(SIGINT, sig_handler); |
8882f4e9 | 2010 | signal(SIGALRM, sig_handler); |
7292613b | 2011 | |
fc24389f | 2012 | todo = thread_number; |
02bdd9ba | 2013 | nr_running = 0; |
2a81240d | 2014 | nr_started = 0; |
213b446c | 2015 | m_rate = t_rate = 0; |
fc24389f | 2016 | |
8bdcfab5 JA |
2017 | for (i = 0; i < thread_number; i++) { |
2018 | td = &threads[i]; | |
2019 | ||
6f75d67c JA |
2020 | init_disk_util(td); |
2021 | ||
fc097bfe JA |
2022 | if (!td->create_serialize) |
2023 | continue; | |
2024 | ||
8bdcfab5 JA |
2025 | /* |
2026 | * do file setup here so it happens sequentially, | |
2027 | * we don't want X number of threads getting their | |
2028 | * client data interspersed on disk | |
2029 | */ | |
2030 | if (setup_file(td)) { | |
40ef7f64 | 2031 | td_set_runstate(td, TD_REAPED); |
8bdcfab5 JA |
2032 | todo--; |
2033 | } | |
2034 | } | |
2035 | ||
2036 | gettimeofday(&genesis, NULL); | |
2037 | ||
213b446c | 2038 | while (todo) { |
3f39453a JA |
2039 | /* |
2040 | * create threads (TD_NOT_CREATED -> TD_CREATED) | |
2041 | */ | |
fc24389f JA |
2042 | for (i = 0; i < thread_number; i++) { |
2043 | td = &threads[i]; | |
2044 | ||
02bdd9ba | 2045 | if (td->runstate != TD_NOT_CREATED) |
fc24389f JA |
2046 | continue; |
2047 | ||
213b446c JA |
2048 | /* |
2049 | * never got a chance to start, killed by other | |
2050 | * thread for some reason | |
2051 | */ | |
2052 | if (td->terminate) { | |
2053 | todo--; | |
2054 | continue; | |
2055 | } | |
2056 | ||
fc24389f | 2057 | if (td->start_delay) { |
be33abe4 | 2058 | spent = mtime_since_now(&genesis); |
fc24389f JA |
2059 | |
2060 | if (td->start_delay * 1000 > spent) | |
2061 | continue; | |
2062 | } | |
2063 | ||
2a81240d | 2064 | if (td->stonewall && (nr_started || nr_running)) |
ea6f96a2 | 2065 | break; |
2a81240d | 2066 | |
40ef7f64 JA |
2067 | td_set_runstate(td, TD_CREATED); |
2068 | check_str_update(td, nr_running, t_rate, m_rate); | |
fc24389f JA |
2069 | sem_init(&startup_sem, 1, 1); |
2070 | todo--; | |
2a81240d | 2071 | nr_started++; |
fc24389f | 2072 | |
189873de JA |
2073 | if (td->use_thread) { |
2074 | if (pthread_create(&td->thread, NULL, thread_main, td)) { | |
2075 | perror("thread_create"); | |
2076 | nr_started--; | |
2077 | } | |
2078 | } else { | |
2079 | if (fork()) | |
2080 | sem_wait(&startup_sem); | |
2081 | else { | |
2082 | fork_main(shm_id, i); | |
2083 | exit(0); | |
2084 | } | |
fc24389f JA |
2085 | } |
2086 | } | |
2087 | ||
3f39453a | 2088 | /* |
e8457004 | 2089 | * start created threads (TD_CREATED -> TD_RUNNING) |
3f39453a | 2090 | */ |
fc24389f JA |
2091 | for (i = 0; i < thread_number; i++) { |
2092 | struct thread_data *td = &threads[i]; | |
2093 | ||
3f39453a JA |
2094 | if (td->runstate != TD_CREATED) |
2095 | continue; | |
2096 | ||
40ef7f64 | 2097 | td_set_runstate(td, TD_RUNNING); |
3f39453a | 2098 | nr_running++; |
2a81240d | 2099 | nr_started--; |
3f39453a JA |
2100 | m_rate += td->ratemin; |
2101 | t_rate += td->rate; | |
40ef7f64 | 2102 | check_str_update(td, nr_running, t_rate, m_rate); |
3f39453a | 2103 | sem_post(&td->mutex); |
fc24389f JA |
2104 | } |
2105 | ||
e8457004 JA |
2106 | for (i = 0; i < thread_number; i++) { |
2107 | struct thread_data *td = &threads[i]; | |
2108 | ||
b48889bb JA |
2109 | if (td->runstate != TD_RUNNING && |
2110 | td->runstate != TD_VERIFYING) | |
e8457004 JA |
2111 | continue; |
2112 | ||
40ef7f64 | 2113 | check_str_update(td, nr_running, t_rate, m_rate); |
e8457004 JA |
2114 | } |
2115 | ||
213b446c | 2116 | reap_threads(&nr_running, &t_rate, &m_rate); |
02bdd9ba | 2117 | |
fc24389f JA |
2118 | if (todo) |
2119 | usleep(100000); | |
2120 | } | |
02bdd9ba JA |
2121 | |
2122 | while (nr_running) { | |
213b446c | 2123 | reap_threads(&nr_running, &t_rate, &m_rate); |
02bdd9ba JA |
2124 | usleep(10000); |
2125 | } | |
6f75d67c JA |
2126 | |
2127 | update_io_ticks(); | |
fc24389f JA |
2128 | } |
2129 | ||
0d80f40d | 2130 | static void show_group_stats(struct group_run_stats *rs, int id) |
8867c0a8 | 2131 | { |
0d80f40d JA |
2132 | printf("\nRun status group %d:\n", id); |
2133 | ||
2134 | if (rs->max_run[DDIR_READ]) | |
2135 | printf(" READ: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", rs->io_mb[0], rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]); | |
2136 | if (rs->max_run[DDIR_WRITE]) | |
2137 | printf(" WRITE: io=%luMiB, aggrb=%lu, minb=%lu, maxb=%lu, mint=%lumsec, maxt=%lumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]); | |
2138 | } | |
2139 | ||
6f75d67c JA |
2140 | static void show_disk_util(void) |
2141 | { | |
2142 | struct list_head *entry; | |
2143 | struct disk_util *du; | |
2144 | double util; | |
2145 | ||
2146 | printf("\nDisk utilization:\n"); | |
2147 | ||
2148 | list_for_each(entry, &disk_list) { | |
2149 | du = list_entry(entry, struct disk_util, list); | |
2150 | ||
2151 | util = (double) 100 * du->io_ticks / (double) du->msec; | |
2152 | if (util > 100.0) | |
2153 | util = 100.0; | |
2154 | ||
2155 | printf(" %s: %3.2f%%\n", du->name, util); | |
2156 | } | |
2157 | } | |
2158 | ||
0d80f40d JA |
2159 | static void show_run_stats(void) |
2160 | { | |
2161 | struct group_run_stats *runstats, *rs; | |
557e4102 | 2162 | struct thread_data *td; |
8867c0a8 JA |
2163 | int i; |
2164 | ||
0d80f40d JA |
2165 | runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1)); |
2166 | ||
2167 | for (i = 0; i < groupid + 1; i++) { | |
2168 | rs = &runstats[i]; | |
2169 | ||
f6dcd824 | 2170 | memset(rs, 0, sizeof(*rs)); |
0d80f40d JA |
2171 | rs->min_bw[0] = rs->min_run[0] = ~0UL; |
2172 | rs->min_bw[1] = rs->min_run[1] = ~0UL; | |
0d80f40d JA |
2173 | } |
2174 | ||
2175 | for (i = 0; i < thread_number; i++) { | |
0d80f40d JA |
2176 | unsigned long bw = 0; |
2177 | ||
557e4102 JA |
2178 | td = &threads[i]; |
2179 | ||
2180 | if (td->error) | |
2181 | continue; | |
2182 | ||
0d80f40d JA |
2183 | rs = &runstats[td->groupid]; |
2184 | ||
557e4102 JA |
2185 | if (td->runtime < rs->min_run[td->ddir]) |
2186 | rs->min_run[td->ddir] = td->runtime; | |
2187 | if (td->runtime > rs->max_run[td->ddir]) | |
2188 | rs->max_run[td->ddir] = td->runtime; | |
0d80f40d | 2189 | |
557e4102 JA |
2190 | if (td->runtime) |
2191 | bw = td->io_bytes / td->runtime; | |
2192 | if (bw < rs->min_bw[td->ddir]) | |
2193 | rs->min_bw[td->ddir] = bw; | |
2194 | if (bw > rs->max_bw[td->ddir]) | |
2195 | rs->max_bw[td->ddir] = bw; | |
0d80f40d | 2196 | |
557e4102 | 2197 | rs->io_mb[td->ddir] += td->io_bytes >> 20; |
0d80f40d | 2198 | } |
9d489c62 | 2199 | |
0d80f40d JA |
2200 | for (i = 0; i < groupid + 1; i++) { |
2201 | rs = &runstats[i]; | |
2202 | ||
2203 | if (rs->max_run[0]) | |
2204 | rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0]; | |
2205 | if (rs->max_run[1]) | |
2206 | rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1]; | |
0d80f40d | 2207 | } |
557e4102 JA |
2208 | |
2209 | for (i = 0; i < thread_number; i++) { | |
2210 | td = &threads[i]; | |
2211 | rs = &runstats[td->groupid]; | |
2212 | ||
c4c8f7b3 | 2213 | show_thread_status(td, rs); |
557e4102 | 2214 | } |
9d489c62 JA |
2215 | |
2216 | for (i = 0; i < groupid + 1; i++) | |
2217 | show_group_stats(&runstats[i], i); | |
6f75d67c JA |
2218 | |
2219 | show_disk_util(); | |
0d80f40d JA |
2220 | } |
2221 | ||
2222 | int main(int argc, char *argv[]) | |
2223 | { | |
27c32a38 | 2224 | memset(run_str, 0, sizeof(run_str)); |
5961d92c | 2225 | |
27c32a38 | 2226 | if (parse_options(argc, argv)) |
5961d92c | 2227 | return 1; |
7dd1389e | 2228 | |
4240cfa1 JA |
2229 | if (!thread_number) { |
2230 | printf("Nothing to do\n"); | |
2231 | return 1; | |
2232 | } | |
7dd1389e | 2233 | |
8882f4e9 | 2234 | disk_util_timer_arm(); |
6f75d67c | 2235 | |
fc24389f | 2236 | run_threads(argv); |
0d80f40d | 2237 | show_run_stats(); |
fc24389f | 2238 | |
892199bd JA |
2239 | return 0; |
2240 | } |