[PATCH] Add write_iolog support
[fio.git] / fio.c
CommitLineData
ebac4655
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 */
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>
29#include <math.h>
30#include <assert.h>
31#include <dirent.h>
32#include <libgen.h>
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/wait.h>
36#include <sys/ipc.h>
37#include <sys/shm.h>
38#include <sys/ioctl.h>
39#include <sys/mman.h>
40
41#include "fio.h"
42#include "os.h"
43
44#define MASK (4095)
45
46#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
47
48int groupid = 0;
49int thread_number = 0;
50static char run_str[MAX_JOBS + 1];
51int shm_id = 0;
52static LIST_HEAD(disk_list);
53static struct itimerval itimer;
5289b847 54static struct timeval genesis;
ebac4655
JA
55
56static void update_io_ticks(void);
57static void disk_util_timer_arm(void);
58static void print_thread_status(void);
59
c04f7ec3
JA
60extern unsigned long long mlock_size;
61
ebac4655
JA
62/*
63 * thread life cycle
64 */
65enum {
66 TD_NOT_CREATED = 0,
67 TD_CREATED,
75154845 68 TD_INITIALIZED,
ebac4655
JA
69 TD_RUNNING,
70 TD_VERIFYING,
71 TD_EXITED,
72 TD_REAPED,
73};
74
3d60d1ed 75#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
ebac4655
JA
76
77static sem_t startup_sem;
78
79#define TERMINATE_ALL (-1)
75154845 80#define JOB_START_TIMEOUT (5 * 1000)
ebac4655
JA
81
82static void terminate_threads(int group_id)
83{
84 int i;
85
86 for (i = 0; i < thread_number; i++) {
87 struct thread_data *td = &threads[i];
88
89 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
90 td->terminate = 1;
91 td->start_delay = 0;
92 }
93 }
94}
95
96static void sig_handler(int sig)
97{
98 switch (sig) {
99 case SIGALRM:
100 update_io_ticks();
101 disk_util_timer_arm();
102 print_thread_status();
103 break;
104 default:
105 printf("\nfio: terminating on signal\n");
106 fflush(stdout);
107 terminate_threads(TERMINATE_ALL);
108 break;
109 }
110}
111
112static unsigned long utime_since(struct timeval *s, struct timeval *e)
113{
114 double sec, usec;
115
116 sec = e->tv_sec - s->tv_sec;
117 usec = e->tv_usec - s->tv_usec;
118 if (sec > 0 && usec < 0) {
119 sec--;
120 usec += 1000000;
121 }
122
123 sec *= (double) 1000000;
124
125 return sec + usec;
126}
127
128static unsigned long utime_since_now(struct timeval *s)
129{
130 struct timeval t;
131
132 gettimeofday(&t, NULL);
133 return utime_since(s, &t);
134}
135
136static unsigned long mtime_since(struct timeval *s, struct timeval *e)
137{
138 double sec, usec;
139
140 sec = e->tv_sec - s->tv_sec;
141 usec = e->tv_usec - s->tv_usec;
142 if (sec > 0 && usec < 0) {
143 sec--;
144 usec += 1000000;
145 }
146
147 sec *= (double) 1000;
148 usec /= (double) 1000;
149
150 return sec + usec;
151}
152
153static unsigned long mtime_since_now(struct timeval *s)
154{
155 struct timeval t;
156
157 gettimeofday(&t, NULL);
158 return mtime_since(s, &t);
159}
160
161static inline unsigned long msec_now(struct timeval *s)
162{
163 return s->tv_sec * 1000 + s->tv_usec / 1000;
164}
165
5289b847
JA
166static unsigned long time_since_now(struct timeval *s)
167{
168 return mtime_since_now(s) / 1000;
169}
170
ebac4655
JA
171static int random_map_free(struct thread_data *td, unsigned long long block)
172{
173 unsigned int idx = RAND_MAP_IDX(td, block);
174 unsigned int bit = RAND_MAP_BIT(td, block);
175
176 return (td->file_map[idx] & (1UL << bit)) == 0;
177}
178
179static int get_next_free_block(struct thread_data *td, unsigned long long *b)
180{
181 int i;
182
183 *b = 0;
184 i = 0;
185 while ((*b) * td->min_bs < td->io_size) {
186 if (td->file_map[i] != -1UL) {
187 *b += ffz(td->file_map[i]);
188 return 0;
189 }
190
191 *b += BLOCKS_PER_MAP;
192 i++;
193 }
194
195 return 1;
196}
197
198static void mark_random_map(struct thread_data *td, struct io_u *io_u)
199{
200bc855 200 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
ebac4655
JA
201 unsigned int blocks = 0;
202
203 while (blocks < (io_u->buflen / td->min_bs)) {
204 unsigned int idx, bit;
205
206 if (!random_map_free(td, block))
207 break;
208
209 idx = RAND_MAP_IDX(td, block);
210 bit = RAND_MAP_BIT(td, block);
211
212 assert(idx < td->num_maps);
213
214 td->file_map[idx] |= (1UL << bit);
215 block++;
216 blocks++;
217 }
218
219 if ((blocks * td->min_bs) < io_u->buflen)
220 io_u->buflen = blocks * td->min_bs;
221}
222
ebac4655
JA
223static inline void add_stat_sample(struct io_stat *is, unsigned long val)
224{
225 if (val > is->max_val)
226 is->max_val = val;
227 if (val < is->min_val)
228 is->min_val = val;
229
230 is->val += val;
231 is->val_sq += val * val;
232 is->samples++;
233}
234
235static void add_log_sample(struct thread_data *td, struct io_log *iolog,
236 unsigned long val, int ddir)
237{
238 if (iolog->nr_samples == iolog->max_samples) {
239 int new_size = sizeof(struct io_sample) * iolog->max_samples*2;
240
241 iolog->log = realloc(iolog->log, new_size);
242 iolog->max_samples <<= 1;
243 }
244
245 iolog->log[iolog->nr_samples].val = val;
246 iolog->log[iolog->nr_samples].time = mtime_since_now(&td->epoch);
247 iolog->log[iolog->nr_samples].ddir = ddir;
248 iolog->nr_samples++;
249}
250
251static void add_clat_sample(struct thread_data *td, int ddir,unsigned long msec)
252{
253 add_stat_sample(&td->clat_stat[ddir], msec);
254
255 if (td->clat_log)
256 add_log_sample(td, td->clat_log, msec, ddir);
257}
258
259static void add_slat_sample(struct thread_data *td, int ddir,unsigned long msec)
260{
261 add_stat_sample(&td->slat_stat[ddir], msec);
262
263 if (td->slat_log)
264 add_log_sample(td, td->slat_log, msec, ddir);
265}
266
267static void add_bw_sample(struct thread_data *td, int ddir)
268{
269 unsigned long spent = mtime_since_now(&td->stat_sample_time[ddir]);
270 unsigned long rate;
271
272 if (spent < td->bw_avg_time)
273 return;
274
275 rate = (td->this_io_bytes[ddir] - td->stat_io_bytes[ddir]) / spent;
276 add_stat_sample(&td->bw_stat[ddir], rate);
277
278 if (td->bw_log)
279 add_log_sample(td, td->bw_log, rate, ddir);
280
281 gettimeofday(&td->stat_sample_time[ddir], NULL);
282 td->stat_io_bytes[ddir] = td->this_io_bytes[ddir];
283}
284
20dc95c4
JA
285static int get_next_offset(struct thread_data *td, unsigned long long *offset)
286{
287 unsigned long long b, rb;
288 long r;
289
290 if (!td->sequential) {
085227ad 291 unsigned long long max_blocks = td->io_size / td->min_bs;
20dc95c4
JA
292 int loops = 50;
293
294 do {
295 lrand48_r(&td->random_state, &r);
085227ad 296 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
20dc95c4
JA
297 rb = b + (td->file_offset / td->min_bs);
298 loops--;
299 } while (!random_map_free(td, rb) && loops);
300
301 if (!loops) {
302 if (get_next_free_block(td, &b))
303 return 1;
304 }
305 } else
306 b = td->last_pos / td->min_bs;
307
308 *offset = (b * td->min_bs) + td->file_offset;
838a3cd3 309 if (*offset > td->real_file_size)
20dc95c4
JA
310 return 1;
311
312 return 0;
313}
314
315static unsigned int get_next_buflen(struct thread_data *td)
316{
317 unsigned int buflen;
318 long r;
319
320 if (td->min_bs == td->max_bs)
321 buflen = td->min_bs;
322 else {
323 lrand48_r(&td->bsrange_state, &r);
324 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
325 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
326 }
327
328 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
329 buflen = td->io_size - td->this_io_bytes[td->ddir];
330
331 return buflen;
332}
333
ebac4655
JA
334/*
335 * busy looping version for the last few usec
336 */
337static void __usec_sleep(unsigned int usec)
338{
339 struct timeval start;
340
341 gettimeofday(&start, NULL);
342 while (utime_since_now(&start) < usec)
343 nop;
344}
345
346static void usec_sleep(struct thread_data *td, unsigned long usec)
347{
348 struct timespec req, rem;
349
350 req.tv_sec = usec / 1000000;
351 req.tv_nsec = usec * 1000 - req.tv_sec * 1000000;
352
353 do {
354 if (usec < 5000) {
355 __usec_sleep(usec);
356 break;
357 }
358
359 rem.tv_sec = rem.tv_nsec = 0;
360 if (nanosleep(&req, &rem) < 0)
361 break;
362
363 if ((rem.tv_sec + rem.tv_nsec) == 0)
364 break;
365
366 req.tv_nsec = rem.tv_nsec;
367 req.tv_sec = rem.tv_sec;
368
369 usec = rem.tv_sec * 1000000 + rem.tv_nsec / 1000;
370 } while (!td->terminate);
371}
372
373static void rate_throttle(struct thread_data *td, unsigned long time_spent,
374 unsigned int bytes)
375{
376 unsigned long usec_cycle;
377
378 if (!td->rate)
379 return;
380
381 usec_cycle = td->rate_usec_cycle * (bytes / td->min_bs);
382
383 if (time_spent < usec_cycle) {
384 unsigned long s = usec_cycle - time_spent;
385
386 td->rate_pending_usleep += s;
387 if (td->rate_pending_usleep >= 100000) {
388 usec_sleep(td, td->rate_pending_usleep);
389 td->rate_pending_usleep = 0;
390 }
391 } else {
392 long overtime = time_spent - usec_cycle;
393
394 td->rate_pending_usleep -= overtime;
395 }
396}
397
398static int check_min_rate(struct thread_data *td, struct timeval *now)
399{
400 unsigned long spent;
401 unsigned long rate;
402 int ddir = td->ddir;
403
404 /*
405 * allow a 2 second settle period in the beginning
406 */
407 if (mtime_since(&td->start, now) < 2000)
408 return 0;
409
410 /*
411 * if rate blocks is set, sample is running
412 */
413 if (td->rate_bytes) {
414 spent = mtime_since(&td->lastrate, now);
415 if (spent < td->ratecycle)
416 return 0;
417
418 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
419 if (rate < td->ratemin) {
420 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
421 if (rate_quit)
422 terminate_threads(td->groupid);
423 return 1;
424 }
425 }
426
427 td->rate_bytes = td->this_io_bytes[ddir];
428 memcpy(&td->lastrate, now, sizeof(*now));
429 return 0;
430}
431
432static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
433{
434 if (!td->timeout)
435 return 0;
436 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
437 return 1;
438
439 return 0;
440}
441
442static void fill_random_bytes(struct thread_data *td,
443 unsigned char *p, unsigned int len)
444{
445 unsigned int todo;
446 double r;
447
448 while (len) {
449 drand48_r(&td->verify_state, &r);
450
451 /*
452 * lrand48_r seems to be broken and only fill the bottom
453 * 32-bits, even on 64-bit archs with 64-bit longs
454 */
455 todo = sizeof(r);
456 if (todo > len)
457 todo = len;
458
459 memcpy(p, &r, todo);
460
461 len -= todo;
462 p += todo;
463 }
464}
465
466static void hexdump(void *buffer, int len)
467{
468 unsigned char *p = buffer;
469 int i;
470
471 for (i = 0; i < len; i++)
472 printf("%02x", p[i]);
473 printf("\n");
474}
475
476static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
477{
478 unsigned char *p = (unsigned char *) io_u->buf;
479 unsigned long c;
480 int ret;
481
482 p += sizeof(*hdr);
483 c = crc32(p, hdr->len - sizeof(*hdr));
484 ret = c != hdr->crc32;
485
486 if (ret) {
487 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
488 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
489 }
490
491 return ret;
492}
493
494static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
495{
496 unsigned char *p = (unsigned char *) io_u->buf;
497 struct md5_ctx md5_ctx;
498 int ret;
499
500 memset(&md5_ctx, 0, sizeof(md5_ctx));
501 p += sizeof(*hdr);
502 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
503
504 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
505 if (ret) {
506 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
507 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
508 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
509 }
510
511 return ret;
512}
513
514static int verify_io_u(struct io_u *io_u)
515{
516 struct verify_header *hdr = (struct verify_header *) io_u->buf;
517 int ret;
518
519 if (hdr->fio_magic != FIO_HDR_MAGIC)
520 return 1;
521
522 if (hdr->verify_type == VERIFY_MD5)
523 ret = verify_io_u_md5(hdr, io_u);
524 else if (hdr->verify_type == VERIFY_CRC32)
525 ret = verify_io_u_crc32(hdr, io_u);
526 else {
527 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
528 ret = 1;
529 }
530
531 return ret;
532}
533
534static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
535{
536 hdr->crc32 = crc32(p, len);
537}
538
539static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
540{
541 struct md5_ctx md5_ctx;
542
543 memset(&md5_ctx, 0, sizeof(md5_ctx));
544 md5_update(&md5_ctx, p, len);
545 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
546}
547
3d60d1ed
JA
548unsigned int hweight32(unsigned int w)
549{
550 unsigned int res = w - ((w >> 1) & 0x55555555);
551
552 res = (res & 0x33333333) + ((res >> 2) & 0x33333333);
553 res = (res + (res >> 4)) & 0x0F0F0F0F;
554 res = res + (res >> 8);
555
556 return (res + (res >> 16)) & 0x000000FF;
557}
558
559unsigned long hweight64(unsigned long long w)
560{
561#if __WORDSIZE == 32
562 return hweight32((unsigned int)(w >> 32)) + hweight32((unsigned int)w);
563#elif __WORDSIZE == 64
564 unsigned long long v = w - ((w >> 1) & 0x5555555555555555ul);
565
566 v = (v & 0x3333333333333333ul) + ((v >> 2) & 0x3333333333333333ul);
567 v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0Ful;
568 v = v + (v >> 8);
569 v = v + (v >> 16);
570
571 return (v + (v >> 32)) & 0x00000000000000FFul;
572#else
573#error __WORDSIZE not defined
574#endif
575}
576
577static int get_rw_ddir(struct thread_data *td)
578{
579 /*
580 * perhaps cheasy, but use the hamming weight of the position
581 * as a randomizer for data direction.
582 */
583 if (td_rw(td))
584 return hweight64(td->last_pos) & 1;
585 else if (td_read(td))
586 return DDIR_READ;
587 else
588 return DDIR_WRITE;
589}
590
ebac4655
JA
591/*
592 * fill body of io_u->buf with random data and add a header with the
593 * (eg) sha1sum of that data.
594 */
595static void populate_io_u(struct thread_data *td, struct io_u *io_u)
596{
597 unsigned char *p = (unsigned char *) io_u->buf;
598 struct verify_header hdr;
599
600 hdr.fio_magic = FIO_HDR_MAGIC;
601 hdr.len = io_u->buflen;
602 p += sizeof(hdr);
603 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
604
605 if (td->verify == VERIFY_MD5) {
606 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
607 hdr.verify_type = VERIFY_MD5;
608 } else {
609 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
610 hdr.verify_type = VERIFY_CRC32;
611 }
612
613 memcpy(io_u->buf, &hdr, sizeof(hdr));
614}
615
aea47d44 616static int td_io_prep(struct thread_data *td, struct io_u *io_u)
20dc95c4 617{
20dc95c4
JA
618 if (td->io_prep && td->io_prep(td, io_u))
619 return 1;
620
621 return 0;
622}
623
b1ff3403 624void put_io_u(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
625{
626 list_del(&io_u->list);
627 list_add(&io_u->list, &td->io_u_freelist);
628 td->cur_depth--;
629}
630
843a7413 631static void write_iolog_put(struct thread_data *td, struct io_u *io_u)
aea47d44 632{
843a7413
JA
633 fprintf(td->iolog_f, "%d,%llu,%u\n", io_u->ddir, io_u->offset, io_u->buflen);
634}
aea47d44 635
843a7413
JA
636static int read_iolog_get(struct thread_data *td, struct io_u *io_u)
637{
638 struct io_piece *ipo;
aea47d44 639
843a7413 640 if (!list_empty(&td->io_log_list)) {
aea47d44
JA
641 ipo = list_entry(td->io_log_list.next, struct io_piece, list);
642 list_del(&ipo->list);
643 io_u->offset = ipo->offset;
644 io_u->buflen = ipo->len;
645 io_u->ddir = ipo->ddir;
646 free(ipo);
647 return 0;
648 }
649
843a7413
JA
650 return 1;
651}
652
653static int fill_io_u(struct thread_data *td, struct io_u *io_u)
654{
655 /*
656 * If using an iolog, grab next piece if any available.
657 */
658 if (td->read_iolog)
659 return read_iolog_get(td, io_u);
660
aea47d44
JA
661 /*
662 * No log, let the seq/rand engine retrieve the next position.
663 */
664 if (!get_next_offset(td, &io_u->offset)) {
665 io_u->buflen = get_next_buflen(td);
666
667 if (io_u->buflen) {
668 io_u->ddir = get_rw_ddir(td);
843a7413
JA
669
670 /*
671 * If using a write iolog, store this entry.
672 */
673 if (td->write_iolog)
674 write_iolog_put(td, io_u);
675
aea47d44
JA
676 return 0;
677 }
678 }
679
680 return 1;
681}
682
ebac4655
JA
683#define queue_full(td) (list_empty(&(td)->io_u_freelist))
684
b1ff3403 685struct io_u *__get_io_u(struct thread_data *td)
ebac4655
JA
686{
687 struct io_u *io_u;
688
689 if (queue_full(td))
690 return NULL;
691
692 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
693 io_u->error = 0;
694 io_u->resid = 0;
695 list_del(&io_u->list);
696 list_add(&io_u->list, &td->io_u_busylist);
697 td->cur_depth++;
698 return io_u;
699}
700
ebac4655
JA
701static struct io_u *get_io_u(struct thread_data *td)
702{
703 struct io_u *io_u;
704
705 io_u = __get_io_u(td);
706 if (!io_u)
707 return NULL;
708
20dc95c4
JA
709 if (td->zone_bytes >= td->zone_size) {
710 td->zone_bytes = 0;
711 td->last_pos += td->zone_skip;
712 }
713
aea47d44 714 if (fill_io_u(td, io_u)) {
ebac4655
JA
715 put_io_u(td, io_u);
716 return NULL;
717 }
718
838a3cd3
JA
719 if (io_u->buflen + io_u->offset > td->real_file_size)
720 io_u->buflen = td->real_file_size - io_u->offset;
ebac4655
JA
721
722 if (!io_u->buflen) {
723 put_io_u(td, io_u);
724 return NULL;
725 }
726
843a7413 727 if (!td->read_iolog && !td->sequential)
ebac4655
JA
728 mark_random_map(td, io_u);
729
20dc95c4 730 td->last_pos += io_u->buflen;
ebac4655
JA
731
732 if (td->verify != VERIFY_NONE)
733 populate_io_u(td, io_u);
734
aea47d44 735 if (td_io_prep(td, io_u)) {
ebac4655
JA
736 put_io_u(td, io_u);
737 return NULL;
738 }
739
740 gettimeofday(&io_u->start_time, NULL);
741 return io_u;
742}
743
744static inline void td_set_runstate(struct thread_data *td, int runstate)
745{
746 td->old_runstate = td->runstate;
747 td->runstate = runstate;
748}
749
aea47d44 750static int get_next_verify(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
751{
752 struct io_piece *ipo;
753
754 if (list_empty(&td->io_hist_list))
755 return 1;
756
757 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
758 list_del(&ipo->list);
759
aea47d44
JA
760 io_u->offset = ipo->offset;
761 io_u->buflen = ipo->len;
762 io_u->ddir = DDIR_READ;
ebac4655
JA
763 free(ipo);
764 return 0;
765}
766
767static void prune_io_piece_log(struct thread_data *td)
768{
769 struct io_piece *ipo;
770
771 while (!list_empty(&td->io_hist_list)) {
772 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
773
774 list_del(&ipo->list);
775 free(ipo);
776 }
777}
778
779/*
780 * log a succesful write, so we can unwind the log for verify
781 */
782static void log_io_piece(struct thread_data *td, struct io_u *io_u)
783{
784 struct io_piece *ipo = malloc(sizeof(struct io_piece));
785 struct list_head *entry;
786
787 INIT_LIST_HEAD(&ipo->list);
788 ipo->offset = io_u->offset;
789 ipo->len = io_u->buflen;
790
791 /*
792 * for random io where the writes extend the file, it will typically
793 * be laid out with the block scattered as written. it's faster to
794 * read them in in that order again, so don't sort
795 */
796 if (td->sequential || !td->overwrite) {
797 list_add_tail(&ipo->list, &td->io_hist_list);
798 return;
799 }
800
801 /*
802 * for random io, sort the list so verify will run faster
803 */
804 entry = &td->io_hist_list;
805 while ((entry = entry->prev) != &td->io_hist_list) {
806 struct io_piece *__ipo = list_entry(entry, struct io_piece, list);
807
808 if (__ipo->offset < ipo->offset)
809 break;
810 }
811
812 list_add(&ipo->list, entry);
813}
814
843a7413
JA
815static void write_iolog_close(struct thread_data *td)
816{
817 fflush(td->iolog_f);
818 fclose(td->iolog_f);
819 free(td->iolog_buf);
820}
821
aea47d44
JA
822static int init_iolog(struct thread_data *td)
823{
824 unsigned long long offset;
825 unsigned int bytes;
826 char *str, *p;
827 FILE *f;
828 int rw, i, reads, writes;
829
843a7413 830 if (!td->read_iolog && !td->write_iolog)
aea47d44
JA
831 return 0;
832
843a7413
JA
833 if (td->read_iolog)
834 f = fopen(td->iolog_file, "r");
835 else
836 f = fopen(td->iolog_file, "w");
837
aea47d44
JA
838 if (!f) {
839 perror("fopen iolog");
843a7413 840 printf("file %s, %d/%d\n", td->iolog_file, td->read_iolog, td->write_iolog);
aea47d44
JA
841 return 1;
842 }
843
843a7413
JA
844 /*
845 * That's it for writing, setup a log buffer and we're done.
846 */
847 if (td->write_iolog) {
848 td->iolog_f = f;
849 td->iolog_buf = malloc(8192);
850 setvbuf(f, td->iolog_buf, _IOFBF, 8192);
851 return 0;
852 }
853
854 /*
855 * Read in the read iolog and store it, reuse the infrastructure
856 * for doing verifications.
857 */
aea47d44
JA
858 str = malloc(4096);
859 reads = writes = i = 0;
860 while ((p = fgets(str, 4096, f)) != NULL) {
861 struct io_piece *ipo;
862
863 if (sscanf(p, "%d,%llu,%u", &rw, &offset, &bytes) != 3) {
864 fprintf(stderr, "bad iolog: %s\n", p);
865 continue;
866 }
867 if (rw == DDIR_READ)
868 reads++;
869 else if (rw == DDIR_WRITE)
870 writes++;
871 else {
872 fprintf(stderr, "bad ddir: %d\n", rw);
873 continue;
874 }
875
876 ipo = malloc(sizeof(*ipo));
877 INIT_LIST_HEAD(&ipo->list);
878 ipo->offset = offset;
879 ipo->len = bytes;
880 if (bytes > td->max_bs)
881 td->max_bs = bytes;
882 ipo->ddir = rw;
883 list_add_tail(&ipo->list, &td->io_log_list);
884 i++;
885 }
886
887 free(str);
888 fclose(f);
889
890 if (!i)
891 return 1;
892
893 if (reads && !writes)
894 td->ddir = DDIR_READ;
895 else if (!reads && writes)
896 td->ddir = DDIR_READ;
897 else
898 td->iomix = 1;
899
900 return 0;
901}
902
ebac4655
JA
903static int sync_td(struct thread_data *td)
904{
905 if (td->io_sync)
906 return td->io_sync(td);
907
908 return 0;
909}
910
911static int io_u_getevents(struct thread_data *td, int min, int max,
912 struct timespec *t)
913{
914 return td->io_getevents(td, min, max, t);
915}
916
917static int io_u_queue(struct thread_data *td, struct io_u *io_u)
918{
919 gettimeofday(&io_u->issue_time, NULL);
920
921 return td->io_queue(td, io_u);
922}
923
924#define iocb_time(iocb) ((unsigned long) (iocb)->data)
925
926static void io_completed(struct thread_data *td, struct io_u *io_u,
927 struct io_completion_data *icd)
928{
929 struct timeval e;
930 unsigned long msec;
931
932 gettimeofday(&e, NULL);
933
934 if (!io_u->error) {
20dc95c4 935 unsigned int bytes = io_u->buflen - io_u->resid;
3d60d1ed 936 const int idx = io_u->ddir;
ebac4655
JA
937
938 td->io_blocks[idx]++;
20dc95c4
JA
939 td->io_bytes[idx] += bytes;
940 td->zone_bytes += bytes;
941 td->this_io_bytes[idx] += bytes;
ebac4655
JA
942
943 msec = mtime_since(&io_u->issue_time, &e);
944
3d60d1ed
JA
945 add_clat_sample(td, idx, msec);
946 add_bw_sample(td, idx);
ebac4655 947
3d60d1ed 948 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
ebac4655
JA
949 log_io_piece(td, io_u);
950
20dc95c4 951 icd->bytes_done[idx] += bytes;
ebac4655
JA
952 } else
953 icd->error = io_u->error;
954}
955
956static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
957{
958 struct io_u *io_u;
959 int i;
960
961 icd->error = 0;
962 icd->bytes_done[0] = icd->bytes_done[1] = 0;
963
964 for (i = 0; i < icd->nr; i++) {
965 io_u = td->io_event(td, i);
966
967 io_completed(td, io_u, icd);
968 put_io_u(td, io_u);
969 }
970}
971
972static void cleanup_pending_aio(struct thread_data *td)
973{
974 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
975 struct list_head *entry, *n;
976 struct io_completion_data icd;
977 struct io_u *io_u;
978 int r;
979
980 /*
981 * get immediately available events, if any
982 */
983 r = io_u_getevents(td, 0, td->cur_depth, &ts);
984 if (r > 0) {
985 icd.nr = r;
986 ios_completed(td, &icd);
987 }
988
989 /*
990 * now cancel remaining active events
991 */
992 if (td->io_cancel) {
993 list_for_each_safe(entry, n, &td->io_u_busylist) {
994 io_u = list_entry(entry, struct io_u, list);
995
996 r = td->io_cancel(td, io_u);
997 if (!r)
998 put_io_u(td, io_u);
999 }
1000 }
1001
1002 if (td->cur_depth) {
1003 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
1004 if (r > 0) {
1005 icd.nr = r;
1006 ios_completed(td, &icd);
1007 }
1008 }
1009}
1010
1011static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
1012{
1013 struct io_u *v_io_u = *io_u;
1014 int ret = 0;
1015
1016 if (v_io_u) {
1017 ret = verify_io_u(v_io_u);
1018 put_io_u(td, v_io_u);
1019 *io_u = NULL;
1020 }
1021
1022 return ret;
1023}
1024
1025static void do_verify(struct thread_data *td)
1026{
1027 struct timeval t;
1028 struct io_u *io_u, *v_io_u = NULL;
1029 struct io_completion_data icd;
1030 int ret;
1031
1032 td_set_runstate(td, TD_VERIFYING);
1033
1034 do {
1035 if (td->terminate)
1036 break;
1037
1038 gettimeofday(&t, NULL);
1039 if (runtime_exceeded(td, &t))
1040 break;
1041
1042 io_u = __get_io_u(td);
1043 if (!io_u)
1044 break;
1045
aea47d44 1046 if (get_next_verify(td, io_u)) {
ebac4655
JA
1047 put_io_u(td, io_u);
1048 break;
1049 }
1050
aea47d44 1051 if (td_io_prep(td, io_u)) {
ebac4655
JA
1052 put_io_u(td, io_u);
1053 break;
1054 }
1055
1056 ret = io_u_queue(td, io_u);
1057 if (ret) {
1058 put_io_u(td, io_u);
1059 td_verror(td, ret);
1060 break;
1061 }
1062
1063 /*
1064 * we have one pending to verify, do that while
1065 * we are doing io on the next one
1066 */
1067 if (do_io_u_verify(td, &v_io_u))
1068 break;
1069
1070 ret = io_u_getevents(td, 1, 1, NULL);
1071 if (ret != 1) {
1072 if (ret < 0)
1073 td_verror(td, ret);
1074 break;
1075 }
1076
1077 v_io_u = td->io_event(td, 0);
1078 icd.nr = 1;
1079 icd.error = 0;
1080 io_completed(td, v_io_u, &icd);
1081
1082 if (icd.error) {
1083 td_verror(td, icd.error);
1084 put_io_u(td, v_io_u);
1085 v_io_u = NULL;
1086 break;
1087 }
1088
ebac4655
JA
1089 /*
1090 * if we can't submit more io, we need to verify now
1091 */
1092 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
1093 break;
1094
1095 } while (1);
1096
1097 do_io_u_verify(td, &v_io_u);
1098
1099 if (td->cur_depth)
1100 cleanup_pending_aio(td);
1101
1102 td_set_runstate(td, TD_RUNNING);
1103}
1104
1105static void do_io(struct thread_data *td)
1106{
1107 struct io_completion_data icd;
1108 struct timeval s, e;
1109 unsigned long usec;
1110
1111 while (td->this_io_bytes[td->ddir] < td->io_size) {
1112 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
1113 struct timespec *timeout;
1114 int ret, min_evts = 0;
1115 struct io_u *io_u;
1116
1117 if (td->terminate)
1118 break;
1119
1120 io_u = get_io_u(td);
1121 if (!io_u)
1122 break;
1123
1124 memcpy(&s, &io_u->start_time, sizeof(s));
1125
1126 ret = io_u_queue(td, io_u);
1127 if (ret) {
1128 put_io_u(td, io_u);
1129 td_verror(td, ret);
1130 break;
1131 }
1132
1133 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
1134
1135 if (td->cur_depth < td->iodepth) {
1136 timeout = &ts;
1137 min_evts = 0;
1138 } else {
1139 timeout = NULL;
1140 min_evts = 1;
1141 }
1142
1143 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
1144 if (ret < 0) {
1145 td_verror(td, ret);
1146 break;
1147 } else if (!ret)
1148 continue;
1149
1150 icd.nr = ret;
1151 ios_completed(td, &icd);
1152 if (icd.error) {
1153 td_verror(td, icd.error);
1154 break;
1155 }
1156
1157 /*
1158 * the rate is batched for now, it should work for batches
1159 * of completions except the very first one which may look
1160 * a little bursty
1161 */
1162 gettimeofday(&e, NULL);
1163 usec = utime_since(&s, &e);
1164
1165 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
1166
1167 if (check_min_rate(td, &e)) {
1168 td_verror(td, ENOMEM);
1169 break;
1170 }
1171
1172 if (runtime_exceeded(td, &e))
1173 break;
1174
1175 if (td->thinktime)
1176 usec_sleep(td, td->thinktime);
1177
1178 if (should_fsync(td) && td->fsync_blocks &&
1179 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
1180 sync_td(td);
1181 }
1182
1183 if (td->cur_depth)
1184 cleanup_pending_aio(td);
1185
fc1a4713 1186 if (should_fsync(td) && td->end_fsync)
ebac4655
JA
1187 sync_td(td);
1188}
1189
1190static void cleanup_io(struct thread_data *td)
1191{
1192 if (td->io_cleanup)
1193 td->io_cleanup(td);
1194}
1195
1196static int init_io(struct thread_data *td)
1197{
1198 if (td->io_engine == FIO_SYNCIO)
1199 return fio_syncio_init(td);
1200 else if (td->io_engine == FIO_MMAPIO)
1201 return fio_mmapio_init(td);
1202 else if (td->io_engine == FIO_LIBAIO)
1203 return fio_libaio_init(td);
1204 else if (td->io_engine == FIO_POSIXAIO)
1205 return fio_posixaio_init(td);
1206 else if (td->io_engine == FIO_SGIO)
1207 return fio_sgio_init(td);
8756e4d4
JA
1208 else if (td->io_engine == FIO_SPLICEIO)
1209 return fio_spliceio_init(td);
ebac4655
JA
1210 else {
1211 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
1212 return 1;
1213 }
1214}
1215
1216static void cleanup_io_u(struct thread_data *td)
1217{
1218 struct list_head *entry, *n;
1219 struct io_u *io_u;
1220
1221 list_for_each_safe(entry, n, &td->io_u_freelist) {
1222 io_u = list_entry(entry, struct io_u, list);
1223
1224 list_del(&io_u->list);
1225 free(io_u);
1226 }
1227
1228 if (td->mem_type == MEM_MALLOC)
1229 free(td->orig_buffer);
1230 else if (td->mem_type == MEM_SHM) {
1231 struct shmid_ds sbuf;
1232
1233 shmdt(td->orig_buffer);
1234 shmctl(td->shm_id, IPC_RMID, &sbuf);
1235 } else if (td->mem_type == MEM_MMAP)
1236 munmap(td->orig_buffer, td->orig_buffer_size);
1237 else
1238 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
1239
1240 td->orig_buffer = NULL;
1241}
1242
1243static int init_io_u(struct thread_data *td)
1244{
1245 struct io_u *io_u;
1246 int i, max_units;
1247 char *p;
1248
1249 if (td->io_engine & FIO_SYNCIO)
1250 max_units = 1;
1251 else
1252 max_units = td->iodepth;
1253
1254 td->orig_buffer_size = td->max_bs * max_units + MASK;
1255
1256 if (td->mem_type == MEM_MALLOC)
1257 td->orig_buffer = malloc(td->orig_buffer_size);
1258 else if (td->mem_type == MEM_SHM) {
1259 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
1260 if (td->shm_id < 0) {
1261 td_verror(td, errno);
1262 perror("shmget");
1263 return 1;
1264 }
1265
1266 td->orig_buffer = shmat(td->shm_id, NULL, 0);
1267 if (td->orig_buffer == (void *) -1) {
1268 td_verror(td, errno);
1269 perror("shmat");
1270 td->orig_buffer = NULL;
1271 return 1;
1272 }
1273 } else if (td->mem_type == MEM_MMAP) {
1274 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1275 if (td->orig_buffer == MAP_FAILED) {
1276 td_verror(td, errno);
1277 perror("mmap");
1278 td->orig_buffer = NULL;
1279 return 1;
1280 }
1281 }
1282
ebac4655
JA
1283 p = ALIGN(td->orig_buffer);
1284 for (i = 0; i < max_units; i++) {
1285 io_u = malloc(sizeof(*io_u));
1286 memset(io_u, 0, sizeof(*io_u));
1287 INIT_LIST_HEAD(&io_u->list);
1288
1289 io_u->buf = p + td->max_bs * i;
b1ff3403 1290 io_u->index = i;
ebac4655
JA
1291 list_add(&io_u->list, &td->io_u_freelist);
1292 }
1293
1294 return 0;
1295}
1296
1297static int create_file(struct thread_data *td, unsigned long long size,
1298 int extend)
1299{
1300 unsigned long long left;
1301 unsigned int bs;
1302 int r, oflags;
1303 char *b;
1304
1305 /*
1306 * unless specifically asked for overwrite, let normal io extend it
1307 */
1308 if (td_write(td) && !td->overwrite)
1309 return 0;
1310
1311 if (!size) {
1312 fprintf(stderr, "Need size for create\n");
1313 td_verror(td, EINVAL);
1314 return 1;
1315 }
1316
1317 if (!extend) {
1318 oflags = O_CREAT | O_TRUNC;
1319 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
1320 } else {
1321 oflags = O_APPEND;
1322 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
1323 }
1324
1325 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
1326 if (td->fd < 0) {
1327 td_verror(td, errno);
1328 return 1;
1329 }
1330
1331 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
1332 td_verror(td, errno);
1333 return 1;
1334 }
1335
1336 td->io_size = td->file_size;
1337 b = malloc(td->max_bs);
1338 memset(b, 0, td->max_bs);
1339
1340 left = size;
1341 while (left && !td->terminate) {
1342 bs = td->max_bs;
1343 if (bs > left)
1344 bs = left;
1345
1346 r = write(td->fd, b, bs);
1347
1348 if (r == (int) bs) {
1349 left -= bs;
1350 continue;
1351 } else {
1352 if (r < 0)
1353 td_verror(td, errno);
1354 else
1355 td_verror(td, EIO);
1356
1357 break;
1358 }
1359 }
1360
1361 if (td->terminate)
1362 unlink(td->file_name);
1363 else if (td->create_fsync)
1364 fsync(td->fd);
1365
1366 close(td->fd);
1367 td->fd = -1;
1368 free(b);
1369 return 0;
1370}
1371
1372static int file_size(struct thread_data *td)
1373{
1374 struct stat st;
1375
1376 if (fstat(td->fd, &st) == -1) {
1377 td_verror(td, errno);
1378 return 1;
1379 }
1380
838a3cd3
JA
1381 td->real_file_size = st.st_size;
1382
1383 if (!td->file_size || td->file_size > td->real_file_size)
1384 td->file_size = td->real_file_size;
ebac4655 1385
e8e387c1 1386 td->file_size -= td->file_offset;
ebac4655
JA
1387 return 0;
1388}
1389
1390static int bdev_size(struct thread_data *td)
1391{
9104f874 1392 unsigned long long bytes;
ebac4655
JA
1393 int r;
1394
1395 r = blockdev_size(td->fd, &bytes);
1396 if (r) {
1397 td_verror(td, r);
1398 return 1;
1399 }
1400
838a3cd3
JA
1401 td->real_file_size = bytes;
1402
ebac4655
JA
1403 /*
1404 * no extend possibilities, so limit size to device size if too large
1405 */
838a3cd3
JA
1406 if (!td->file_size || td->file_size > td->real_file_size)
1407 td->file_size = td->real_file_size;
ebac4655 1408
e8e387c1 1409 td->file_size -= td->file_offset;
ebac4655
JA
1410 return 0;
1411}
1412
1413static int get_file_size(struct thread_data *td)
1414{
0af7b542 1415 int ret = 0;
ebac4655
JA
1416
1417 if (td->filetype == FIO_TYPE_FILE)
1418 ret = file_size(td);
0af7b542 1419 else if (td->filetype == FIO_TYPE_BD)
ebac4655 1420 ret = bdev_size(td);
0af7b542
JA
1421 else
1422 td->real_file_size = -1;
ebac4655
JA
1423
1424 if (ret)
1425 return ret;
1426
e8e387c1
JA
1427 if (td->file_offset > td->real_file_size) {
1428 fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size);
ebac4655
JA
1429 return 1;
1430 }
1431
838a3cd3 1432 td->io_size = td->file_size;
ebac4655
JA
1433 if (td->io_size == 0) {
1434 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1435 td_verror(td, EINVAL);
1436 return 1;
1437 }
1438
20dc95c4
JA
1439 if (!td->zone_size)
1440 td->zone_size = td->io_size;
1441
ebac4655
JA
1442 td->total_io_size = td->io_size * td->loops;
1443 return 0;
1444}
1445
1446static int setup_file_mmap(struct thread_data *td)
1447{
1448 int flags;
1449
3d60d1ed
JA
1450 if (td_rw(td))
1451 flags = PROT_READ | PROT_WRITE;
1452 else if (td_write(td)) {
ebac4655
JA
1453 flags = PROT_WRITE;
1454
1455 if (td->verify != VERIFY_NONE)
1456 flags |= PROT_READ;
3d60d1ed
JA
1457 } else
1458 flags = PROT_READ;
ebac4655
JA
1459
1460 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1461 if (td->mmap == MAP_FAILED) {
1462 td->mmap = NULL;
1463 td_verror(td, errno);
1464 return 1;
1465 }
1466
1467 if (td->invalidate_cache) {
1468 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1469 td_verror(td, errno);
1470 return 1;
1471 }
1472 }
1473
1474 if (td->sequential) {
1475 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1476 td_verror(td, errno);
1477 return 1;
1478 }
1479 } else {
1480 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1481 td_verror(td, errno);
1482 return 1;
1483 }
1484 }
1485
1486 return 0;
1487}
1488
1489static int setup_file_plain(struct thread_data *td)
1490{
1491 if (td->invalidate_cache) {
1492 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1493 td_verror(td, errno);
1494 return 1;
1495 }
1496 }
1497
1498 if (td->sequential) {
1499 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1500 td_verror(td, errno);
1501 return 1;
1502 }
1503 } else {
1504 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1505 td_verror(td, errno);
1506 return 1;
1507 }
1508 }
1509
1510 return 0;
1511}
1512
1513static int setup_file(struct thread_data *td)
1514{
1515 struct stat st;
1516 int flags = 0;
1517
1518 if (stat(td->file_name, &st) == -1) {
1519 if (errno != ENOENT) {
1520 td_verror(td, errno);
1521 return 1;
1522 }
1523 if (!td->create_file) {
1524 td_verror(td, ENOENT);
1525 return 1;
1526 }
1527 if (create_file(td, td->file_size, 0))
1528 return 1;
1529 } else if (td->filetype == FIO_TYPE_FILE) {
6a0106a0 1530 if (st.st_size < (off_t) td->file_size) {
ebac4655
JA
1531 if (create_file(td, td->file_size - st.st_size, 1))
1532 return 1;
1533 }
1534 }
1535
1536 if (td->odirect)
1537 flags |= O_DIRECT;
1538
3d60d1ed 1539 if (td_write(td) || td_rw(td)) {
ebac4655
JA
1540 if (td->filetype == FIO_TYPE_FILE) {
1541 if (!td->overwrite)
1542 flags |= O_TRUNC;
1543
1544 flags |= O_CREAT;
1545 }
1546 if (td->sync_io)
1547 flags |= O_SYNC;
1548
1549 flags |= O_RDWR;
1550
1551 td->fd = open(td->file_name, flags, 0600);
3d60d1ed
JA
1552 } else {
1553 if (td->filetype == FIO_TYPE_CHAR)
1554 flags |= O_RDWR;
1555 else
1556 flags |= O_RDONLY;
1557
1558 td->fd = open(td->file_name, flags);
ebac4655
JA
1559 }
1560
1561 if (td->fd == -1) {
1562 td_verror(td, errno);
1563 return 1;
1564 }
1565
1566 if (get_file_size(td))
1567 return 1;
1568
1569 if (td->io_engine != FIO_MMAPIO)
1570 return setup_file_plain(td);
1571 else
1572 return setup_file_mmap(td);
1573}
1574
1575static int check_dev_match(dev_t dev, char *path)
1576{
1577 unsigned int major, minor;
1578 char line[256], *p;
1579 FILE *f;
1580
1581 f = fopen(path, "r");
1582 if (!f) {
1583 perror("open path");
1584 return 1;
1585 }
1586
1587 p = fgets(line, sizeof(line), f);
1588 if (!p) {
1589 fclose(f);
1590 return 1;
1591 }
1592
1593 if (sscanf(p, "%u:%u", &major, &minor) != 2) {
1594 fclose(f);
1595 return 1;
1596 }
1597
1598 if (((major << 8) | minor) == dev) {
1599 fclose(f);
1600 return 0;
1601 }
1602
1603 fclose(f);
1604 return 1;
1605}
1606
1607static int find_block_dir(dev_t dev, char *path)
1608{
1609 struct dirent *dir;
1610 struct stat st;
1611 int found = 0;
1612 DIR *D;
1613
1614 D = opendir(path);
1615 if (!D)
1616 return 0;
1617
1618 while ((dir = readdir(D)) != NULL) {
1619 char full_path[256];
1620
1621 if (!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, ".."))
1622 continue;
1623 if (!strcmp(dir->d_name, "device"))
1624 continue;
1625
1626 sprintf(full_path, "%s/%s", path, dir->d_name);
1627
1628 if (!strcmp(dir->d_name, "dev")) {
1629 if (!check_dev_match(dev, full_path)) {
1630 found = 1;
1631 break;
1632 }
1633 }
1634
1635 if (stat(full_path, &st) == -1) {
1636 perror("stat");
1637 break;
1638 }
1639
1640 if (!S_ISDIR(st.st_mode) || S_ISLNK(st.st_mode))
1641 continue;
1642
1643 found = find_block_dir(dev, full_path);
1644 if (found) {
1645 strcpy(path, full_path);
1646 break;
1647 }
1648 }
1649
1650 closedir(D);
1651 return found;
1652}
1653
1654static int get_io_ticks(struct disk_util *du, struct disk_util_stat *dus)
1655{
1656 unsigned in_flight;
1657 char line[256];
1658 FILE *f;
1659 char *p;
1660
1661 f = fopen(du->path, "r");
1662 if (!f)
1663 return 1;
1664
1665 p = fgets(line, sizeof(line), f);
1666 if (!p) {
1667 fclose(f);
1668 return 1;
1669 }
1670
1671 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) {
1672 fclose(f);
1673 return 1;
1674 }
1675
1676 fclose(f);
1677 return 0;
1678}
1679
1680static void update_io_tick_disk(struct disk_util *du)
1681{
1682 struct disk_util_stat __dus, *dus, *ldus;
1683 struct timeval t;
1684
1685 if (get_io_ticks(du, &__dus))
1686 return;
1687
1688 dus = &du->dus;
1689 ldus = &du->last_dus;
1690
1691 dus->sectors[0] += (__dus.sectors[0] - ldus->sectors[0]);
1692 dus->sectors[1] += (__dus.sectors[1] - ldus->sectors[1]);
1693 dus->ios[0] += (__dus.ios[0] - ldus->ios[0]);
1694 dus->ios[1] += (__dus.ios[1] - ldus->ios[1]);
1695 dus->merges[0] += (__dus.merges[0] - ldus->merges[0]);
1696 dus->merges[1] += (__dus.merges[1] - ldus->merges[1]);
1697 dus->ticks[0] += (__dus.ticks[0] - ldus->ticks[0]);
1698 dus->ticks[1] += (__dus.ticks[1] - ldus->ticks[1]);
1699 dus->io_ticks += (__dus.io_ticks - ldus->io_ticks);
1700 dus->time_in_queue += (__dus.time_in_queue - ldus->time_in_queue);
1701
1702 gettimeofday(&t, NULL);
1703 du->msec += mtime_since(&du->time, &t);
1704 memcpy(&du->time, &t, sizeof(t));
1705 memcpy(ldus, &__dus, sizeof(__dus));
1706}
1707
1708static void update_io_ticks(void)
1709{
1710 struct list_head *entry;
1711 struct disk_util *du;
1712
1713 list_for_each(entry, &disk_list) {
1714 du = list_entry(entry, struct disk_util, list);
1715 update_io_tick_disk(du);
1716 }
1717}
1718
1719static int disk_util_exists(dev_t dev)
1720{
1721 struct list_head *entry;
1722 struct disk_util *du;
1723
1724 list_for_each(entry, &disk_list) {
1725 du = list_entry(entry, struct disk_util, list);
1726
1727 if (du->dev == dev)
1728 return 1;
1729 }
1730
1731 return 0;
1732}
1733
1734static void disk_util_add(dev_t dev, char *path)
1735{
1736 struct disk_util *du = malloc(sizeof(*du));
1737
1738 memset(du, 0, sizeof(*du));
1739 INIT_LIST_HEAD(&du->list);
1740 sprintf(du->path, "%s/stat", path);
1741 du->name = strdup(basename(path));
1742 du->dev = dev;
1743
1744 gettimeofday(&du->time, NULL);
1745 get_io_ticks(du, &du->last_dus);
1746
1747 list_add_tail(&du->list, &disk_list);
1748}
1749
1750static void init_disk_util(struct thread_data *td)
1751{
1752 struct stat st;
1753 char foo[256], tmp[256];
1754 dev_t dev;
1755 char *p;
1756
1757 if (!td->do_disk_util)
1758 return;
1759
1760 if (!stat(td->file_name, &st)) {
1761 if (S_ISBLK(st.st_mode))
1762 dev = st.st_rdev;
1763 else
1764 dev = st.st_dev;
1765 } else {
1766 /*
1767 * must be a file, open "." in that path
1768 */
1769 strcpy(foo, td->file_name);
1770 p = dirname(foo);
1771 if (stat(p, &st)) {
1772 perror("disk util stat");
1773 return;
1774 }
1775
1776 dev = st.st_dev;
1777 }
1778
1779 if (disk_util_exists(dev))
1780 return;
1781
1782 sprintf(foo, "/sys/block");
1783 if (!find_block_dir(dev, foo))
1784 return;
1785
1786 /*
d07f439e
JA
1787 * If there's a ../queue/ directory there, we are inside a partition.
1788 * Check if that is the case and jump back. For loop/md/dm etc we
1789 * are already in the right spot.
ebac4655 1790 */
d07f439e
JA
1791 sprintf(tmp, "%s/../queue", foo);
1792 if (!stat(tmp, &st)) {
1793 p = dirname(foo);
1794 sprintf(tmp, "%s/queue", p);
ebac4655 1795 if (stat(tmp, &st)) {
d07f439e
JA
1796 fprintf(stderr, "unknown sysfs layout\n");
1797 return;
ebac4655 1798 }
d07f439e 1799 sprintf(foo, "%s", p);
ebac4655
JA
1800 }
1801
1802 disk_util_add(dev, foo);
1803}
1804
1805static void disk_util_timer_arm(void)
1806{
1807 itimer.it_value.tv_sec = 0;
1808 itimer.it_value.tv_usec = DISK_UTIL_MSEC * 1000;
1809 setitimer(ITIMER_REAL, &itimer, NULL);
1810}
1811
1812static void clear_io_state(struct thread_data *td)
1813{
1814 if (td->io_engine == FIO_SYNCIO)
1815 lseek(td->fd, SEEK_SET, 0);
1816
20dc95c4 1817 td->last_pos = 0;
ebac4655
JA
1818 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1819 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 1820 td->zone_bytes = 0;
ebac4655
JA
1821
1822 if (td->file_map)
1823 memset(td->file_map, 0, td->num_maps * sizeof(long));
1824}
1825
1826static void update_rusage_stat(struct thread_data *td)
1827{
1828 if (!(td->runtime[0] + td->runtime[1]))
1829 return;
1830
1831 getrusage(RUSAGE_SELF, &td->ru_end);
1832
1833 td->usr_time += mtime_since(&td->ru_start.ru_utime, &td->ru_end.ru_utime);
1834 td->sys_time += mtime_since(&td->ru_start.ru_stime, &td->ru_end.ru_stime);
1835 td->ctx += td->ru_end.ru_nvcsw + td->ru_end.ru_nivcsw - (td->ru_start.ru_nvcsw + td->ru_start.ru_nivcsw);
1836
1837
1838 memcpy(&td->ru_start, &td->ru_end, sizeof(td->ru_end));
1839}
1840
1841static void *thread_main(void *data)
1842{
1843 struct thread_data *td = data;
1844 int ret = 1;
1845
1846 if (!td->use_thread)
1847 setsid();
1848
1849 td->pid = getpid();
1850
aea47d44
JA
1851 INIT_LIST_HEAD(&td->io_u_freelist);
1852 INIT_LIST_HEAD(&td->io_u_busylist);
1853 INIT_LIST_HEAD(&td->io_hist_list);
1854 INIT_LIST_HEAD(&td->io_log_list);
1855
ebac4655
JA
1856 if (init_io_u(td))
1857 goto err;
1858
1859 if (fio_setaffinity(td) == -1) {
1860 td_verror(td, errno);
1861 goto err;
1862 }
1863
1864 if (init_io(td))
1865 goto err;
1866
aea47d44
JA
1867 if (init_iolog(td))
1868 goto err;
1869
ebac4655
JA
1870 if (td->ioprio) {
1871 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1872 td_verror(td, errno);
1873 goto err;
1874 }
1875 }
1876
75154845
JA
1877 if (init_random_state(td))
1878 goto err;
1879
1880 td_set_runstate(td, TD_INITIALIZED);
ebac4655
JA
1881 sem_post(&startup_sem);
1882 sem_wait(&td->mutex);
843a7413 1883 ret = 0;
ebac4655
JA
1884
1885 if (!td->create_serialize && setup_file(td))
1886 goto err;
1887
ebac4655
JA
1888 gettimeofday(&td->epoch, NULL);
1889
1890 while (td->loops--) {
1891 getrusage(RUSAGE_SELF, &td->ru_start);
1892 gettimeofday(&td->start, NULL);
1893 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1894
1895 if (td->ratemin)
1896 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1897
1898 clear_io_state(td);
1899 prune_io_piece_log(td);
1900
1901 do_io(td);
1902
1903 td->runtime[td->ddir] += mtime_since_now(&td->start);
aea47d44 1904 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
3d60d1ed
JA
1905 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1906
ebac4655
JA
1907 update_rusage_stat(td);
1908
1909 if (td->error || td->terminate)
1910 break;
1911
1912 if (td->verify == VERIFY_NONE)
1913 continue;
1914
1915 clear_io_state(td);
1916 gettimeofday(&td->start, NULL);
1917
1918 do_verify(td);
1919
1920 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1921
1922 if (td->error || td->terminate)
1923 break;
1924 }
1925
ebac4655
JA
1926 if (td->bw_log)
1927 finish_log(td, td->bw_log, "bw");
1928 if (td->slat_log)
1929 finish_log(td, td->slat_log, "slat");
1930 if (td->clat_log)
1931 finish_log(td, td->clat_log, "clat");
843a7413
JA
1932 if (td->write_iolog)
1933 write_iolog_close(td);
ebac4655
JA
1934
1935 if (exitall_on_terminate)
1936 terminate_threads(td->groupid);
1937
1938err:
1939 if (td->fd != -1) {
1940 close(td->fd);
1941 td->fd = -1;
1942 }
1943 if (td->mmap)
1944 munmap(td->mmap, td->file_size);
1945 cleanup_io(td);
1946 cleanup_io_u(td);
1947 if (ret) {
1948 sem_post(&startup_sem);
1949 sem_wait(&td->mutex);
1950 }
1951 td_set_runstate(td, TD_EXITED);
1952 return NULL;
1953
1954}
1955
1956static void *fork_main(int shmid, int offset)
1957{
1958 struct thread_data *td;
1959 void *data;
1960
1961 data = shmat(shmid, NULL, 0);
1962 if (data == (void *) -1) {
1963 perror("shmat");
1964 return NULL;
1965 }
1966
1967 td = data + offset * sizeof(struct thread_data);
1968 thread_main(td);
1969 shmdt(data);
1970 return NULL;
1971}
1972
1973static int calc_lat(struct io_stat *is, unsigned long *min, unsigned long *max,
1974 double *mean, double *dev)
1975{
1976 double n;
1977
1978 if (is->samples == 0)
1979 return 0;
1980
1981 *min = is->min_val;
1982 *max = is->max_val;
1983
1984 n = (double) is->samples;
1985 *mean = (double) is->val / n;
1986 *dev = sqrt(((double) is->val_sq - (*mean * *mean) / n) / (n - 1));
1987 if (!(*min + *max) && !(*mean + *dev))
1988 return 0;
1989
1990 return 1;
1991}
1992
1993static void show_ddir_status(struct thread_data *td, struct group_run_stats *rs,
1994 int ddir)
1995{
1996 char *ddir_str[] = { "read ", "write" };
200bc855
JA
1997 unsigned long min, max;
1998 unsigned long long bw;
ebac4655
JA
1999 double mean, dev;
2000
2001 if (!td->runtime[ddir])
2002 return;
2003
2004 bw = td->io_bytes[ddir] / td->runtime[ddir];
200bc855 2005 printf(" %s: io=%6lluMiB, bw=%6lluKiB/s, runt=%6lumsec\n", ddir_str[ddir], td->io_bytes[ddir] >> 20, bw, td->runtime[ddir]);
ebac4655
JA
2006
2007 if (calc_lat(&td->slat_stat[ddir], &min, &max, &mean, &dev))
2008 printf(" slat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
2009
2010 if (calc_lat(&td->clat_stat[ddir], &min, &max, &mean, &dev))
2011 printf(" clat (msec): min=%5lu, max=%5lu, avg=%5.02f, dev=%5.02f\n", min, max, mean, dev);
2012
2013 if (calc_lat(&td->bw_stat[ddir], &min, &max, &mean, &dev)) {
2014 double p_of_agg;
2015
2016 p_of_agg = mean * 100 / (double) rs->agg[ddir];
2017 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);
2018 }
2019}
2020
2021static void show_thread_status(struct thread_data *td,
2022 struct group_run_stats *rs)
2023{
2024 double usr_cpu, sys_cpu;
2025
2026 if (!(td->io_bytes[0] + td->io_bytes[1]) && !td->error)
2027 return;
2028
2029 printf("Client%d (groupid=%d): err=%2d:\n", td->thread_number, td->groupid, td->error);
2030
2031 show_ddir_status(td, rs, td->ddir);
aea47d44
JA
2032 if (td->io_bytes[td->ddir ^ 1])
2033 show_ddir_status(td, rs, td->ddir ^ 1);
ebac4655
JA
2034
2035 if (td->runtime[0] + td->runtime[1]) {
2036 double runt = td->runtime[0] + td->runtime[1];
2037
2038 usr_cpu = (double) td->usr_time * 100 / runt;
2039 sys_cpu = (double) td->sys_time * 100 / runt;
2040 } else {
2041 usr_cpu = 0;
2042 sys_cpu = 0;
2043 }
2044
2045 printf(" cpu : usr=%3.2f%%, sys=%3.2f%%, ctx=%lu\n", usr_cpu, sys_cpu, td->ctx);
2046}
2047
2048static void check_str_update(struct thread_data *td)
2049{
2050 char c = run_str[td->thread_number - 1];
2051
2052 if (td->runstate == td->old_runstate)
2053 return;
2054
2055 switch (td->runstate) {
2056 case TD_REAPED:
2057 c = '_';
2058 break;
2059 case TD_EXITED:
2060 c = 'E';
2061 break;
2062 case TD_RUNNING:
3d60d1ed
JA
2063 if (td_rw(td)) {
2064 if (td->sequential)
2065 c = 'M';
2066 else
2067 c = 'm';
2068 } else if (td_read(td)) {
ebac4655
JA
2069 if (td->sequential)
2070 c = 'R';
2071 else
2072 c = 'r';
2073 } else {
2074 if (td->sequential)
2075 c = 'W';
2076 else
2077 c = 'w';
2078 }
2079 break;
2080 case TD_VERIFYING:
2081 c = 'V';
2082 break;
2083 case TD_CREATED:
2084 c = 'C';
2085 break;
75154845
JA
2086 case TD_INITIALIZED:
2087 c = 'I';
2088 break;
ebac4655
JA
2089 case TD_NOT_CREATED:
2090 c = 'P';
2091 break;
2092 default:
2093 printf("state %d\n", td->runstate);
2094 }
2095
2096 run_str[td->thread_number - 1] = c;
2097 td->old_runstate = td->runstate;
2098}
2099
5289b847
JA
2100static void eta_to_str(char *str, int eta_sec)
2101{
2102 unsigned int d, h, m, s;
2103 static int always_d, always_h;
2104
2105 d = h = m = s = 0;
2106
2107 s = eta_sec % 60;
2108 eta_sec /= 60;
2109 m = eta_sec % 60;
2110 eta_sec /= 60;
2111 h = eta_sec % 24;
2112 eta_sec /= 24;
2113 d = eta_sec;
2114
2115 if (d || always_d) {
2116 always_d = 1;
2117 str += sprintf(str, "%02dd:", d);
2118 }
2119 if (h || always_h) {
2120 always_h = 1;
2121 str += sprintf(str, "%02dh:", h);
2122 }
2123
2124 str += sprintf(str, "%02dm:", m);
2125 str += sprintf(str, "%02ds", s);
2126}
2127
6a0106a0
JA
2128static int thread_eta(struct thread_data *td, unsigned long elapsed)
2129{
2130 unsigned long long bytes_total, bytes_done;
2131 unsigned int eta_sec = 0;
2132
2133 bytes_total = td->total_io_size;
3d60d1ed
JA
2134
2135 /*
2136 * if writing, bytes_total will be twice the size. If mixing,
2137 * assume a 50/50 split and thus bytes_total will be 50% larger.
2138 */
2139 if (td->verify) {
2140 if (td_rw(td))
2141 bytes_total = bytes_total * 3 / 2;
2142 else
2143 bytes_total <<= 1;
2144 }
6a0106a0
JA
2145 if (td->zone_size && td->zone_skip)
2146 bytes_total /= (td->zone_skip / td->zone_size);
2147
2148 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
2149 double perc;
8b611c34 2150
6a0106a0
JA
2151 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
2152 perc = (double) bytes_done / (double) bytes_total;
2153 if (perc > 1.0)
2154 perc = 1.0;
2155
2156 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
2157
8b611c34 2158 if (td->timeout && eta_sec > (td->timeout - elapsed))
6a0106a0 2159 eta_sec = td->timeout - elapsed;
75154845
JA
2160 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
2161 || td->runstate == TD_INITIALIZED) {
6a0106a0
JA
2162 int t_eta = 0, r_eta = 0;
2163
2164 /*
2165 * We can only guess - assume it'll run the full timeout
2166 * if given, otherwise assume it'll run at the specified rate.
2167 */
2168 if (td->timeout)
2169 t_eta = td->timeout + td->start_delay - elapsed;
2170 if (td->rate) {
2171 r_eta = (bytes_total / 1024) / td->rate;
2172 r_eta += td->start_delay - elapsed;
2173 }
2174
2175 if (r_eta && t_eta)
2176 eta_sec = min(r_eta, t_eta);
2177 else if (r_eta)
2178 eta_sec = r_eta;
2179 else if (t_eta)
2180 eta_sec = t_eta;
2181 else
2182 eta_sec = INT_MAX;
2183 } else {
2184 /*
2185 * thread is already done
2186 */
2187 eta_sec = 0;
2188 }
2189
2190 return eta_sec;
2191}
2192
ebac4655
JA
2193static void print_thread_status(void)
2194{
6a0106a0
JA
2195 unsigned long elapsed = time_since_now(&genesis);
2196 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
5289b847 2197 char eta_str[32];
6a0106a0
JA
2198 double perc = 0.0;
2199
2200 eta_secs = malloc(thread_number * sizeof(int));
2201 memset(eta_secs, 0, thread_number * sizeof(int));
ebac4655 2202
ebac4655
JA
2203 nr_running = t_rate = m_rate = 0;
2204 for (i = 0; i < thread_number; i++) {
2205 struct thread_data *td = &threads[i];
2206
2207 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
2208 nr_running++;
2209 t_rate += td->rate;
2210 m_rate += td->ratemin;
2211 }
2212
6a0106a0
JA
2213 if (elapsed >= 3)
2214 eta_secs[i] = thread_eta(td, elapsed);
8b611c34
JA
2215 else
2216 eta_secs[i] = INT_MAX;
ebac4655
JA
2217
2218 check_str_update(td);
2219 }
2220
6a0106a0
JA
2221 if (exitall_on_terminate)
2222 eta_sec = INT_MAX;
2223 else
2224 eta_sec = 0;
5289b847 2225
6a0106a0
JA
2226 for (i = 0; i < thread_number; i++) {
2227 if (exitall_on_terminate) {
2228 if (eta_secs[i] < eta_sec)
2229 eta_sec = eta_secs[i];
2230 } else {
2231 if (eta_secs[i] > eta_sec)
2232 eta_sec = eta_secs[i];
5289b847 2233 }
6a0106a0 2234 }
5289b847 2235
8b611c34 2236 if (eta_sec != INT_MAX && elapsed) {
6a0106a0
JA
2237 perc = (double) elapsed / (double) (elapsed + eta_sec);
2238 eta_to_str(eta_str, eta_sec);
ebac4655
JA
2239 }
2240
5289b847 2241 printf("Threads now running (%d)", nr_running);
ebac4655
JA
2242 if (m_rate || t_rate)
2243 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
8b611c34 2244 if (eta_sec != INT_MAX) {
6a0106a0
JA
2245 perc *= 100.0;
2246 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
2247 }
5289b847 2248 printf("\r");
ebac4655 2249 fflush(stdout);
6a0106a0 2250 free(eta_secs);
ebac4655
JA
2251}
2252
2253static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
2254{
2255 int i;
2256
2257 /*
2258 * reap exited threads (TD_EXITED -> TD_REAPED)
2259 */
2260 for (i = 0; i < thread_number; i++) {
2261 struct thread_data *td = &threads[i];
2262
2263 if (td->runstate != TD_EXITED)
2264 continue;
2265
2266 td_set_runstate(td, TD_REAPED);
2267
2268 if (td->use_thread) {
2269 long ret;
2270
2271 if (pthread_join(td->thread, (void *) &ret))
2272 perror("thread_join");
2273 } else
2274 waitpid(td->pid, NULL, 0);
2275
2276 (*nr_running)--;
2277 (*m_rate) -= td->ratemin;
2278 (*t_rate) -= td->rate;
2279 }
2280}
2281
fcb6ade2
JA
2282static void fio_unpin_memory(void *pinned)
2283{
2284 if (pinned) {
2285 if (munlock(pinned, mlock_size) < 0)
2286 perror("munlock");
2287 munmap(pinned, mlock_size);
2288 }
2289}
2290
2291static void *fio_pin_memory(void)
2292{
2293 long pagesize, pages;
2294 void *ptr;
2295
2296 if (!mlock_size)
2297 return NULL;
2298
2299 /*
2300 * Don't allow mlock of more than real_mem-128MB
2301 */
2302 pagesize = sysconf(_SC_PAGESIZE);
2303 pages = sysconf(_SC_PHYS_PAGES);
2304 if (pages != -1 && pagesize != -1) {
2305 unsigned long long real_mem = pages * pagesize;
2306
2307 if ((mlock_size + 128 * 1024 * 1024) > real_mem) {
2308 mlock_size = real_mem - 128 * 1024 * 1024;
2309 printf("fio: limiting mlocked memory to %lluMiB\n",
2310 mlock_size >> 20);
2311 }
2312 }
2313
2314 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
2315 if (!ptr) {
2316 perror("malloc locked mem");
2317 return NULL;
2318 }
2319 if (mlock(ptr, mlock_size) < 0) {
2320 munmap(ptr, mlock_size);
2321 perror("mlock");
2322 return NULL;
2323 }
2324
2325 return ptr;
2326}
2327
ebac4655
JA
2328static void run_threads(void)
2329{
ebac4655
JA
2330 struct thread_data *td;
2331 unsigned long spent;
2332 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2
JA
2333 void *mlocked_mem;
2334
2335 mlocked_mem = fio_pin_memory();
ebac4655
JA
2336
2337 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
fcb6ade2 2338 fflush(stdout);
c04f7ec3 2339
4efa970e
JA
2340 signal(SIGINT, sig_handler);
2341 signal(SIGALRM, sig_handler);
2342
ebac4655
JA
2343 todo = thread_number;
2344 nr_running = 0;
2345 nr_started = 0;
2346 m_rate = t_rate = 0;
2347
2348 for (i = 0; i < thread_number; i++) {
2349 td = &threads[i];
2350
2351 run_str[td->thread_number - 1] = 'P';
2352
2353 init_disk_util(td);
2354
2355 if (!td->create_serialize)
2356 continue;
2357
2358 /*
2359 * do file setup here so it happens sequentially,
2360 * we don't want X number of threads getting their
2361 * client data interspersed on disk
2362 */
2363 if (setup_file(td)) {
2364 td_set_runstate(td, TD_REAPED);
2365 todo--;
2366 }
2367 }
2368
2369 gettimeofday(&genesis, NULL);
2370
2371 while (todo) {
75154845
JA
2372 struct thread_data *map[MAX_JOBS];
2373 struct timeval this_start;
2374 int this_jobs = 0, left;
2375
ebac4655
JA
2376 /*
2377 * create threads (TD_NOT_CREATED -> TD_CREATED)
2378 */
2379 for (i = 0; i < thread_number; i++) {
2380 td = &threads[i];
2381
2382 if (td->runstate != TD_NOT_CREATED)
2383 continue;
2384
2385 /*
2386 * never got a chance to start, killed by other
2387 * thread for some reason
2388 */
2389 if (td->terminate) {
2390 todo--;
2391 continue;
2392 }
2393
2394 if (td->start_delay) {
2395 spent = mtime_since_now(&genesis);
2396
2397 if (td->start_delay * 1000 > spent)
2398 continue;
2399 }
2400
2401 if (td->stonewall && (nr_started || nr_running))
2402 break;
2403
75154845
JA
2404 /*
2405 * Set state to created. Thread will transition
2406 * to TD_INITIALIZED when it's done setting up.
2407 */
ebac4655 2408 td_set_runstate(td, TD_CREATED);
75154845 2409 map[this_jobs++] = td;
ebac4655 2410 sem_init(&startup_sem, 0, 1);
ebac4655
JA
2411 nr_started++;
2412
2413 if (td->use_thread) {
2414 if (pthread_create(&td->thread, NULL, thread_main, td)) {
2415 perror("thread_create");
2416 nr_started--;
2417 }
2418 } else {
2419 if (fork())
2420 sem_wait(&startup_sem);
2421 else {
2422 fork_main(shm_id, i);
2423 exit(0);
2424 }
2425 }
2426 }
2427
2428 /*
75154845
JA
2429 * Wait for the started threads to transition to
2430 * TD_INITIALIZED.
ebac4655 2431 */
75154845
JA
2432 printf("fio: Waiting for threads to initialize...\n");
2433 gettimeofday(&this_start, NULL);
2434 left = this_jobs;
2435 while (left) {
2436 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
2437 break;
2438
2439 usleep(100000);
2440
2441 for (i = 0; i < this_jobs; i++) {
2442 td = map[i];
2443 if (!td)
2444 continue;
2445 if (td->runstate == TD_INITIALIZED ||
2446 td->runstate >= TD_EXITED) {
2447 map[i] = NULL;
2448 left--;
2449 continue;
2450 }
2451 }
2452 }
2453
2454 if (left) {
2455 fprintf(stderr, "fio: %d jobs failed to start\n", left);
2456 for (i = 0; i < this_jobs; i++) {
2457 td = map[i];
2458 if (!td)
2459 continue;
2460 kill(td->pid, SIGTERM);
2461 }
2462 break;
2463 }
2464
2465 /*
2466 * start created threads (TD_INITIALIZED -> TD_RUNNING)
2467 */
2468 printf("fio: Go for launch\n");
ebac4655
JA
2469 for (i = 0; i < thread_number; i++) {
2470 td = &threads[i];
2471
75154845 2472 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
2473 continue;
2474
2475 td_set_runstate(td, TD_RUNNING);
2476 nr_running++;
2477 nr_started--;
2478 m_rate += td->ratemin;
2479 t_rate += td->rate;
75154845 2480 todo--;
ebac4655
JA
2481 sem_post(&td->mutex);
2482 }
2483
2484 reap_threads(&nr_running, &t_rate, &m_rate);
2485
2486 if (todo)
2487 usleep(100000);
2488 }
2489
2490 while (nr_running) {
2491 reap_threads(&nr_running, &t_rate, &m_rate);
2492 usleep(10000);
2493 }
2494
2495 update_io_ticks();
fcb6ade2 2496 fio_unpin_memory(mlocked_mem);
ebac4655
JA
2497}
2498
2499static void show_group_stats(struct group_run_stats *rs, int id)
2500{
2501 printf("\nRun status group %d (all jobs):\n", id);
2502
2503 if (rs->max_run[DDIR_READ])
9104f874 2504 printf(" READ: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[0], rs->agg[0], rs->min_bw[0], rs->max_bw[0], rs->min_run[0], rs->max_run[0]);
ebac4655 2505 if (rs->max_run[DDIR_WRITE])
9104f874 2506 printf(" WRITE: io=%lluMiB, aggrb=%llu, minb=%llu, maxb=%llu, mint=%llumsec, maxt=%llumsec\n", rs->io_mb[1], rs->agg[1], rs->min_bw[1], rs->max_bw[1], rs->min_run[1], rs->max_run[1]);
ebac4655
JA
2507}
2508
2509static void show_disk_util(void)
2510{
2511 struct disk_util_stat *dus;
2512 struct list_head *entry;
2513 struct disk_util *du;
2514 double util;
2515
2516 printf("\nDisk stats (read/write):\n");
2517
2518 list_for_each(entry, &disk_list) {
2519 du = list_entry(entry, struct disk_util, list);
2520 dus = &du->dus;
2521
2522 util = (double) 100 * du->dus.io_ticks / (double) du->msec;
2523 if (util > 100.0)
2524 util = 100.0;
2525
2526 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);
2527 }
2528}
2529
2530static void show_run_stats(void)
2531{
2532 struct group_run_stats *runstats, *rs;
2533 struct thread_data *td;
2534 int i;
2535
2536 runstats = malloc(sizeof(struct group_run_stats) * (groupid + 1));
2537
2538 for (i = 0; i < groupid + 1; i++) {
2539 rs = &runstats[i];
2540
2541 memset(rs, 0, sizeof(*rs));
2542 rs->min_bw[0] = rs->min_run[0] = ~0UL;
2543 rs->min_bw[1] = rs->min_run[1] = ~0UL;
2544 }
2545
2546 for (i = 0; i < thread_number; i++) {
200bc855 2547 unsigned long long rbw, wbw;
ebac4655
JA
2548
2549 td = &threads[i];
2550
2551 if (td->error) {
2552 printf("Client%d: %s\n", td->thread_number, td->verror);
2553 continue;
2554 }
2555
2556 rs = &runstats[td->groupid];
2557
2558 if (td->runtime[0] < rs->min_run[0] || !rs->min_run[0])
2559 rs->min_run[0] = td->runtime[0];
2560 if (td->runtime[0] > rs->max_run[0])
2561 rs->max_run[0] = td->runtime[0];
2562 if (td->runtime[1] < rs->min_run[1] || !rs->min_run[1])
2563 rs->min_run[1] = td->runtime[1];
2564 if (td->runtime[1] > rs->max_run[1])
2565 rs->max_run[1] = td->runtime[1];
2566
2567 rbw = wbw = 0;
2568 if (td->runtime[0])
200bc855 2569 rbw = td->io_bytes[0] / (unsigned long long) td->runtime[0];
ebac4655 2570 if (td->runtime[1])
200bc855 2571 wbw = td->io_bytes[1] / (unsigned long long) td->runtime[1];
ebac4655
JA
2572
2573 if (rbw < rs->min_bw[0])
2574 rs->min_bw[0] = rbw;
2575 if (wbw < rs->min_bw[1])
2576 rs->min_bw[1] = wbw;
2577 if (rbw > rs->max_bw[0])
2578 rs->max_bw[0] = rbw;
2579 if (wbw > rs->max_bw[1])
2580 rs->max_bw[1] = wbw;
2581
2582 rs->io_mb[0] += td->io_bytes[0] >> 20;
2583 rs->io_mb[1] += td->io_bytes[1] >> 20;
2584 }
2585
2586 for (i = 0; i < groupid + 1; i++) {
2587 rs = &runstats[i];
2588
2589 if (rs->max_run[0])
2590 rs->agg[0] = (rs->io_mb[0]*1024*1000) / rs->max_run[0];
2591 if (rs->max_run[1])
2592 rs->agg[1] = (rs->io_mb[1]*1024*1000) / rs->max_run[1];
2593 }
2594
2595 /*
2596 * don't overwrite last signal output
2597 */
2598 printf("\n");
2599
2600 for (i = 0; i < thread_number; i++) {
2601 td = &threads[i];
2602 rs = &runstats[td->groupid];
2603
2604 show_thread_status(td, rs);
2605 }
2606
2607 for (i = 0; i < groupid + 1; i++)
2608 show_group_stats(&runstats[i], i);
2609
2610 show_disk_util();
2611}
2612
2613int main(int argc, char *argv[])
2614{
2615 if (parse_options(argc, argv))
2616 return 1;
2617
2618 if (!thread_number) {
2619 printf("Nothing to do\n");
2620 return 1;
2621 }
2622
2623 disk_util_timer_arm();
2624
2625 run_threads();
2626 show_run_stats();
2627
2628 return 0;
2629}