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