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