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