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