[PATCH] Juggle some includes, add cscope make target
[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 */
ebac4655
JA
21#include <unistd.h>
22#include <fcntl.h>
23#include <string.h>
ebac4655
JA
24#include <signal.h>
25#include <time.h>
ebac4655 26#include <assert.h>
ebac4655
JA
27#include <sys/stat.h>
28#include <sys/wait.h>
29#include <sys/ipc.h>
30#include <sys/shm.h>
31#include <sys/ioctl.h>
32#include <sys/mman.h>
33
34#include "fio.h"
35#include "os.h"
36
3c39a379
JA
37#include "fio-time.h"
38#include "fio-stat.h"
39#include "fio-log.h"
40
ebac4655
JA
41#define MASK (4095)
42
43#define ALIGN(buf) (char *) (((unsigned long) (buf) + MASK) & ~(MASK))
44
45int groupid = 0;
46int thread_number = 0;
47static char run_str[MAX_JOBS + 1];
48int shm_id = 0;
5289b847 49static struct timeval genesis;
ebac4655 50
ebac4655
JA
51static void print_thread_status(void);
52
c04f7ec3
JA
53extern unsigned long long mlock_size;
54
ebac4655
JA
55/*
56 * thread life cycle
57 */
58enum {
59 TD_NOT_CREATED = 0,
60 TD_CREATED,
75154845 61 TD_INITIALIZED,
ebac4655
JA
62 TD_RUNNING,
63 TD_VERIFYING,
64 TD_EXITED,
65 TD_REAPED,
66};
67
3d60d1ed 68#define should_fsync(td) ((td_write(td) || td_rw(td)) && (!(td)->odirect || (td)->override_sync))
ebac4655
JA
69
70static sem_t startup_sem;
71
72#define TERMINATE_ALL (-1)
75154845 73#define JOB_START_TIMEOUT (5 * 1000)
ebac4655
JA
74
75static void terminate_threads(int group_id)
76{
77 int i;
78
79 for (i = 0; i < thread_number; i++) {
80 struct thread_data *td = &threads[i];
81
82 if (group_id == TERMINATE_ALL || groupid == td->groupid) {
83 td->terminate = 1;
84 td->start_delay = 0;
85 }
86 }
87}
88
89static void sig_handler(int sig)
90{
91 switch (sig) {
92 case SIGALRM:
93 update_io_ticks();
94 disk_util_timer_arm();
95 print_thread_status();
96 break;
97 default:
98 printf("\nfio: terminating on signal\n");
99 fflush(stdout);
100 terminate_threads(TERMINATE_ALL);
101 break;
102 }
103}
104
ebac4655
JA
105static int random_map_free(struct thread_data *td, unsigned long long block)
106{
107 unsigned int idx = RAND_MAP_IDX(td, block);
108 unsigned int bit = RAND_MAP_BIT(td, block);
109
110 return (td->file_map[idx] & (1UL << bit)) == 0;
111}
112
113static int get_next_free_block(struct thread_data *td, unsigned long long *b)
114{
115 int i;
116
117 *b = 0;
118 i = 0;
119 while ((*b) * td->min_bs < td->io_size) {
120 if (td->file_map[i] != -1UL) {
121 *b += ffz(td->file_map[i]);
122 return 0;
123 }
124
125 *b += BLOCKS_PER_MAP;
126 i++;
127 }
128
129 return 1;
130}
131
132static void mark_random_map(struct thread_data *td, struct io_u *io_u)
133{
200bc855 134 unsigned long long block = io_u->offset / (unsigned long long) td->min_bs;
ebac4655
JA
135 unsigned int blocks = 0;
136
137 while (blocks < (io_u->buflen / td->min_bs)) {
138 unsigned int idx, bit;
139
140 if (!random_map_free(td, block))
141 break;
142
143 idx = RAND_MAP_IDX(td, block);
144 bit = RAND_MAP_BIT(td, block);
145
146 assert(idx < td->num_maps);
147
148 td->file_map[idx] |= (1UL << bit);
149 block++;
150 blocks++;
151 }
152
153 if ((blocks * td->min_bs) < io_u->buflen)
154 io_u->buflen = blocks * td->min_bs;
155}
156
20dc95c4
JA
157static int get_next_offset(struct thread_data *td, unsigned long long *offset)
158{
159 unsigned long long b, rb;
160 long r;
161
162 if (!td->sequential) {
085227ad 163 unsigned long long max_blocks = td->io_size / td->min_bs;
20dc95c4
JA
164 int loops = 50;
165
166 do {
167 lrand48_r(&td->random_state, &r);
085227ad 168 b = ((max_blocks - 1) * r / (unsigned long long) (RAND_MAX+1.0));
20dc95c4
JA
169 rb = b + (td->file_offset / td->min_bs);
170 loops--;
171 } while (!random_map_free(td, rb) && loops);
172
173 if (!loops) {
174 if (get_next_free_block(td, &b))
175 return 1;
176 }
177 } else
178 b = td->last_pos / td->min_bs;
179
180 *offset = (b * td->min_bs) + td->file_offset;
838a3cd3 181 if (*offset > td->real_file_size)
20dc95c4
JA
182 return 1;
183
184 return 0;
185}
186
187static unsigned int get_next_buflen(struct thread_data *td)
188{
189 unsigned int buflen;
190 long r;
191
192 if (td->min_bs == td->max_bs)
193 buflen = td->min_bs;
194 else {
195 lrand48_r(&td->bsrange_state, &r);
196 buflen = (1 + (double) (td->max_bs - 1) * r / (RAND_MAX + 1.0));
197 buflen = (buflen + td->min_bs - 1) & ~(td->min_bs - 1);
198 }
199
200 if (buflen > td->io_size - td->this_io_bytes[td->ddir])
201 buflen = td->io_size - td->this_io_bytes[td->ddir];
202
203 return buflen;
204}
205
ebac4655
JA
206static int check_min_rate(struct thread_data *td, struct timeval *now)
207{
208 unsigned long spent;
209 unsigned long rate;
210 int ddir = td->ddir;
211
212 /*
213 * allow a 2 second settle period in the beginning
214 */
215 if (mtime_since(&td->start, now) < 2000)
216 return 0;
217
218 /*
219 * if rate blocks is set, sample is running
220 */
221 if (td->rate_bytes) {
222 spent = mtime_since(&td->lastrate, now);
223 if (spent < td->ratecycle)
224 return 0;
225
226 rate = (td->this_io_bytes[ddir] - td->rate_bytes) / spent;
227 if (rate < td->ratemin) {
228 printf("Client%d: min rate %d not met, got %ldKiB/sec\n", td->thread_number, td->ratemin, rate);
229 if (rate_quit)
230 terminate_threads(td->groupid);
231 return 1;
232 }
233 }
234
235 td->rate_bytes = td->this_io_bytes[ddir];
236 memcpy(&td->lastrate, now, sizeof(*now));
237 return 0;
238}
239
240static inline int runtime_exceeded(struct thread_data *td, struct timeval *t)
241{
242 if (!td->timeout)
243 return 0;
244 if (mtime_since(&td->epoch, t) >= td->timeout * 1000)
245 return 1;
246
247 return 0;
248}
249
250static void fill_random_bytes(struct thread_data *td,
251 unsigned char *p, unsigned int len)
252{
253 unsigned int todo;
254 double r;
255
256 while (len) {
257 drand48_r(&td->verify_state, &r);
258
259 /*
260 * lrand48_r seems to be broken and only fill the bottom
261 * 32-bits, even on 64-bit archs with 64-bit longs
262 */
263 todo = sizeof(r);
264 if (todo > len)
265 todo = len;
266
267 memcpy(p, &r, todo);
268
269 len -= todo;
270 p += todo;
271 }
272}
273
274static void hexdump(void *buffer, int len)
275{
276 unsigned char *p = buffer;
277 int i;
278
279 for (i = 0; i < len; i++)
280 printf("%02x", p[i]);
281 printf("\n");
282}
283
284static int verify_io_u_crc32(struct verify_header *hdr, struct io_u *io_u)
285{
286 unsigned char *p = (unsigned char *) io_u->buf;
287 unsigned long c;
288 int ret;
289
290 p += sizeof(*hdr);
291 c = crc32(p, hdr->len - sizeof(*hdr));
292 ret = c != hdr->crc32;
293
294 if (ret) {
295 fprintf(stderr, "crc32: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
296 fprintf(stderr, "crc32: wanted %lx, got %lx\n", hdr->crc32, c);
297 }
298
299 return ret;
300}
301
302static int verify_io_u_md5(struct verify_header *hdr, struct io_u *io_u)
303{
304 unsigned char *p = (unsigned char *) io_u->buf;
305 struct md5_ctx md5_ctx;
306 int ret;
307
308 memset(&md5_ctx, 0, sizeof(md5_ctx));
309 p += sizeof(*hdr);
310 md5_update(&md5_ctx, p, hdr->len - sizeof(*hdr));
311
312 ret = memcmp(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
313 if (ret) {
314 fprintf(stderr, "md5: verify failed at %llu/%u\n", io_u->offset, io_u->buflen);
315 hexdump(hdr->md5_digest, sizeof(hdr->md5_digest));
316 hexdump(md5_ctx.hash, sizeof(md5_ctx.hash));
317 }
318
319 return ret;
320}
321
322static int verify_io_u(struct io_u *io_u)
323{
324 struct verify_header *hdr = (struct verify_header *) io_u->buf;
325 int ret;
326
327 if (hdr->fio_magic != FIO_HDR_MAGIC)
328 return 1;
329
330 if (hdr->verify_type == VERIFY_MD5)
331 ret = verify_io_u_md5(hdr, io_u);
332 else if (hdr->verify_type == VERIFY_CRC32)
333 ret = verify_io_u_crc32(hdr, io_u);
334 else {
335 fprintf(stderr, "Bad verify type %d\n", hdr->verify_type);
336 ret = 1;
337 }
338
339 return ret;
340}
341
342static void fill_crc32(struct verify_header *hdr, void *p, unsigned int len)
343{
344 hdr->crc32 = crc32(p, len);
345}
346
347static void fill_md5(struct verify_header *hdr, void *p, unsigned int len)
348{
349 struct md5_ctx md5_ctx;
350
351 memset(&md5_ctx, 0, sizeof(md5_ctx));
352 md5_update(&md5_ctx, p, len);
353 memcpy(hdr->md5_digest, md5_ctx.hash, sizeof(md5_ctx.hash));
354}
355
a6ccc7be 356static int get_rw_ddir(struct thread_data *td)
3d60d1ed 357{
a6ccc7be
JA
358 if (td_rw(td)) {
359 struct timeval now;
360 unsigned long elapsed;
3d60d1ed 361
a6ccc7be
JA
362 gettimeofday(&now, NULL);
363 elapsed = mtime_since_now(&td->rwmix_switch);
3d60d1ed 364
a6ccc7be
JA
365 /*
366 * Check if it's time to seed a new data direction.
367 */
368 if (elapsed >= td->rwmixcycle) {
369 unsigned long v;
370 long r;
3d60d1ed 371
a6ccc7be
JA
372 lrand48_r(&td->random_state, &r);
373 v = 100UL * r / (unsigned long) (RAND_MAX + 1.0);
374 if (v < td->rwmixread)
375 td->rwmix_ddir = DDIR_READ;
376 else
377 td->rwmix_ddir = DDIR_WRITE;
378 memcpy(&td->rwmix_switch, &now, sizeof(now));
379 }
380 return td->rwmix_ddir;
381 } else if (td_read(td))
3d60d1ed
JA
382 return DDIR_READ;
383 else
384 return DDIR_WRITE;
385}
386
ebac4655
JA
387/*
388 * fill body of io_u->buf with random data and add a header with the
389 * (eg) sha1sum of that data.
390 */
391static void populate_io_u(struct thread_data *td, struct io_u *io_u)
392{
393 unsigned char *p = (unsigned char *) io_u->buf;
394 struct verify_header hdr;
395
396 hdr.fio_magic = FIO_HDR_MAGIC;
397 hdr.len = io_u->buflen;
398 p += sizeof(hdr);
399 fill_random_bytes(td, p, io_u->buflen - sizeof(hdr));
400
401 if (td->verify == VERIFY_MD5) {
402 fill_md5(&hdr, p, io_u->buflen - sizeof(hdr));
403 hdr.verify_type = VERIFY_MD5;
404 } else {
405 fill_crc32(&hdr, p, io_u->buflen - sizeof(hdr));
406 hdr.verify_type = VERIFY_CRC32;
407 }
408
409 memcpy(io_u->buf, &hdr, sizeof(hdr));
410}
411
aea47d44 412static int td_io_prep(struct thread_data *td, struct io_u *io_u)
20dc95c4 413{
20dc95c4
JA
414 if (td->io_prep && td->io_prep(td, io_u))
415 return 1;
416
417 return 0;
418}
419
b1ff3403 420void put_io_u(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
421{
422 list_del(&io_u->list);
423 list_add(&io_u->list, &td->io_u_freelist);
424 td->cur_depth--;
425}
426
843a7413
JA
427static int fill_io_u(struct thread_data *td, struct io_u *io_u)
428{
429 /*
430 * If using an iolog, grab next piece if any available.
431 */
432 if (td->read_iolog)
433 return read_iolog_get(td, io_u);
434
aea47d44
JA
435 /*
436 * No log, let the seq/rand engine retrieve the next position.
437 */
438 if (!get_next_offset(td, &io_u->offset)) {
439 io_u->buflen = get_next_buflen(td);
440
441 if (io_u->buflen) {
442 io_u->ddir = get_rw_ddir(td);
843a7413
JA
443
444 /*
445 * If using a write iolog, store this entry.
446 */
447 if (td->write_iolog)
448 write_iolog_put(td, io_u);
449
aea47d44
JA
450 return 0;
451 }
452 }
453
454 return 1;
455}
456
ebac4655
JA
457#define queue_full(td) (list_empty(&(td)->io_u_freelist))
458
b1ff3403 459struct io_u *__get_io_u(struct thread_data *td)
ebac4655
JA
460{
461 struct io_u *io_u;
462
463 if (queue_full(td))
464 return NULL;
465
466 io_u = list_entry(td->io_u_freelist.next, struct io_u, list);
467 io_u->error = 0;
468 io_u->resid = 0;
469 list_del(&io_u->list);
470 list_add(&io_u->list, &td->io_u_busylist);
471 td->cur_depth++;
472 return io_u;
473}
474
ebac4655
JA
475static struct io_u *get_io_u(struct thread_data *td)
476{
477 struct io_u *io_u;
478
479 io_u = __get_io_u(td);
480 if (!io_u)
481 return NULL;
482
20dc95c4
JA
483 if (td->zone_bytes >= td->zone_size) {
484 td->zone_bytes = 0;
485 td->last_pos += td->zone_skip;
486 }
487
aea47d44 488 if (fill_io_u(td, io_u)) {
ebac4655
JA
489 put_io_u(td, io_u);
490 return NULL;
491 }
492
838a3cd3
JA
493 if (io_u->buflen + io_u->offset > td->real_file_size)
494 io_u->buflen = td->real_file_size - io_u->offset;
ebac4655
JA
495
496 if (!io_u->buflen) {
497 put_io_u(td, io_u);
498 return NULL;
499 }
500
843a7413 501 if (!td->read_iolog && !td->sequential)
ebac4655
JA
502 mark_random_map(td, io_u);
503
20dc95c4 504 td->last_pos += io_u->buflen;
ebac4655
JA
505
506 if (td->verify != VERIFY_NONE)
507 populate_io_u(td, io_u);
508
aea47d44 509 if (td_io_prep(td, io_u)) {
ebac4655
JA
510 put_io_u(td, io_u);
511 return NULL;
512 }
513
514 gettimeofday(&io_u->start_time, NULL);
515 return io_u;
516}
517
518static inline void td_set_runstate(struct thread_data *td, int runstate)
519{
520 td->old_runstate = td->runstate;
521 td->runstate = runstate;
522}
523
aea47d44 524static int get_next_verify(struct thread_data *td, struct io_u *io_u)
ebac4655
JA
525{
526 struct io_piece *ipo;
527
528 if (list_empty(&td->io_hist_list))
529 return 1;
530
531 ipo = list_entry(td->io_hist_list.next, struct io_piece, list);
532 list_del(&ipo->list);
533
aea47d44
JA
534 io_u->offset = ipo->offset;
535 io_u->buflen = ipo->len;
536 io_u->ddir = DDIR_READ;
ebac4655
JA
537 free(ipo);
538 return 0;
539}
540
ebac4655
JA
541static int sync_td(struct thread_data *td)
542{
543 if (td->io_sync)
544 return td->io_sync(td);
545
546 return 0;
547}
548
549static int io_u_getevents(struct thread_data *td, int min, int max,
550 struct timespec *t)
551{
552 return td->io_getevents(td, min, max, t);
553}
554
555static int io_u_queue(struct thread_data *td, struct io_u *io_u)
556{
557 gettimeofday(&io_u->issue_time, NULL);
558
559 return td->io_queue(td, io_u);
560}
561
562#define iocb_time(iocb) ((unsigned long) (iocb)->data)
563
564static void io_completed(struct thread_data *td, struct io_u *io_u,
565 struct io_completion_data *icd)
566{
567 struct timeval e;
568 unsigned long msec;
569
570 gettimeofday(&e, NULL);
571
572 if (!io_u->error) {
20dc95c4 573 unsigned int bytes = io_u->buflen - io_u->resid;
3d60d1ed 574 const int idx = io_u->ddir;
ebac4655
JA
575
576 td->io_blocks[idx]++;
20dc95c4
JA
577 td->io_bytes[idx] += bytes;
578 td->zone_bytes += bytes;
579 td->this_io_bytes[idx] += bytes;
ebac4655
JA
580
581 msec = mtime_since(&io_u->issue_time, &e);
582
3d60d1ed
JA
583 add_clat_sample(td, idx, msec);
584 add_bw_sample(td, idx);
ebac4655 585
3d60d1ed 586 if ((td_rw(td) || td_write(td)) && idx == DDIR_WRITE)
ebac4655
JA
587 log_io_piece(td, io_u);
588
20dc95c4 589 icd->bytes_done[idx] += bytes;
ebac4655
JA
590 } else
591 icd->error = io_u->error;
592}
593
594static void ios_completed(struct thread_data *td,struct io_completion_data *icd)
595{
596 struct io_u *io_u;
597 int i;
598
599 icd->error = 0;
600 icd->bytes_done[0] = icd->bytes_done[1] = 0;
601
602 for (i = 0; i < icd->nr; i++) {
603 io_u = td->io_event(td, i);
604
605 io_completed(td, io_u, icd);
606 put_io_u(td, io_u);
607 }
608}
609
610static void cleanup_pending_aio(struct thread_data *td)
611{
612 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
613 struct list_head *entry, *n;
614 struct io_completion_data icd;
615 struct io_u *io_u;
616 int r;
617
618 /*
619 * get immediately available events, if any
620 */
621 r = io_u_getevents(td, 0, td->cur_depth, &ts);
622 if (r > 0) {
623 icd.nr = r;
624 ios_completed(td, &icd);
625 }
626
627 /*
628 * now cancel remaining active events
629 */
630 if (td->io_cancel) {
631 list_for_each_safe(entry, n, &td->io_u_busylist) {
632 io_u = list_entry(entry, struct io_u, list);
633
634 r = td->io_cancel(td, io_u);
635 if (!r)
636 put_io_u(td, io_u);
637 }
638 }
639
640 if (td->cur_depth) {
641 r = io_u_getevents(td, td->cur_depth, td->cur_depth, NULL);
642 if (r > 0) {
643 icd.nr = r;
644 ios_completed(td, &icd);
645 }
646 }
647}
648
649static int do_io_u_verify(struct thread_data *td, struct io_u **io_u)
650{
651 struct io_u *v_io_u = *io_u;
652 int ret = 0;
653
654 if (v_io_u) {
655 ret = verify_io_u(v_io_u);
656 put_io_u(td, v_io_u);
657 *io_u = NULL;
658 }
659
660 return ret;
661}
662
663static void do_verify(struct thread_data *td)
664{
665 struct timeval t;
666 struct io_u *io_u, *v_io_u = NULL;
667 struct io_completion_data icd;
668 int ret;
669
670 td_set_runstate(td, TD_VERIFYING);
671
672 do {
673 if (td->terminate)
674 break;
675
676 gettimeofday(&t, NULL);
677 if (runtime_exceeded(td, &t))
678 break;
679
680 io_u = __get_io_u(td);
681 if (!io_u)
682 break;
683
aea47d44 684 if (get_next_verify(td, io_u)) {
ebac4655
JA
685 put_io_u(td, io_u);
686 break;
687 }
688
aea47d44 689 if (td_io_prep(td, io_u)) {
ebac4655
JA
690 put_io_u(td, io_u);
691 break;
692 }
693
694 ret = io_u_queue(td, io_u);
695 if (ret) {
696 put_io_u(td, io_u);
697 td_verror(td, ret);
698 break;
699 }
700
701 /*
702 * we have one pending to verify, do that while
703 * we are doing io on the next one
704 */
705 if (do_io_u_verify(td, &v_io_u))
706 break;
707
708 ret = io_u_getevents(td, 1, 1, NULL);
709 if (ret != 1) {
710 if (ret < 0)
711 td_verror(td, ret);
712 break;
713 }
714
715 v_io_u = td->io_event(td, 0);
716 icd.nr = 1;
717 icd.error = 0;
718 io_completed(td, v_io_u, &icd);
719
720 if (icd.error) {
721 td_verror(td, icd.error);
722 put_io_u(td, v_io_u);
723 v_io_u = NULL;
724 break;
725 }
726
ebac4655
JA
727 /*
728 * if we can't submit more io, we need to verify now
729 */
730 if (queue_full(td) && do_io_u_verify(td, &v_io_u))
731 break;
732
733 } while (1);
734
735 do_io_u_verify(td, &v_io_u);
736
737 if (td->cur_depth)
738 cleanup_pending_aio(td);
739
740 td_set_runstate(td, TD_RUNNING);
741}
742
743static void do_io(struct thread_data *td)
744{
745 struct io_completion_data icd;
746 struct timeval s, e;
747 unsigned long usec;
748
749 while (td->this_io_bytes[td->ddir] < td->io_size) {
750 struct timespec ts = { .tv_sec = 0, .tv_nsec = 0};
751 struct timespec *timeout;
752 int ret, min_evts = 0;
753 struct io_u *io_u;
754
755 if (td->terminate)
756 break;
757
758 io_u = get_io_u(td);
759 if (!io_u)
760 break;
761
762 memcpy(&s, &io_u->start_time, sizeof(s));
763
764 ret = io_u_queue(td, io_u);
765 if (ret) {
766 put_io_u(td, io_u);
767 td_verror(td, ret);
768 break;
769 }
770
771 add_slat_sample(td, io_u->ddir, mtime_since(&io_u->start_time, &io_u->issue_time));
772
773 if (td->cur_depth < td->iodepth) {
774 timeout = &ts;
775 min_evts = 0;
776 } else {
777 timeout = NULL;
778 min_evts = 1;
779 }
780
781 ret = io_u_getevents(td, min_evts, td->cur_depth, timeout);
782 if (ret < 0) {
783 td_verror(td, ret);
784 break;
785 } else if (!ret)
786 continue;
787
788 icd.nr = ret;
789 ios_completed(td, &icd);
790 if (icd.error) {
791 td_verror(td, icd.error);
792 break;
793 }
794
795 /*
796 * the rate is batched for now, it should work for batches
797 * of completions except the very first one which may look
798 * a little bursty
799 */
800 gettimeofday(&e, NULL);
801 usec = utime_since(&s, &e);
802
803 rate_throttle(td, usec, icd.bytes_done[td->ddir]);
804
805 if (check_min_rate(td, &e)) {
806 td_verror(td, ENOMEM);
807 break;
808 }
809
810 if (runtime_exceeded(td, &e))
811 break;
812
813 if (td->thinktime)
814 usec_sleep(td, td->thinktime);
815
816 if (should_fsync(td) && td->fsync_blocks &&
817 (td->io_blocks[DDIR_WRITE] % td->fsync_blocks) == 0)
818 sync_td(td);
819 }
820
821 if (td->cur_depth)
822 cleanup_pending_aio(td);
823
fc1a4713 824 if (should_fsync(td) && td->end_fsync)
ebac4655
JA
825 sync_td(td);
826}
827
828static void cleanup_io(struct thread_data *td)
829{
830 if (td->io_cleanup)
831 td->io_cleanup(td);
832}
833
834static int init_io(struct thread_data *td)
835{
836 if (td->io_engine == FIO_SYNCIO)
837 return fio_syncio_init(td);
838 else if (td->io_engine == FIO_MMAPIO)
839 return fio_mmapio_init(td);
840 else if (td->io_engine == FIO_LIBAIO)
841 return fio_libaio_init(td);
842 else if (td->io_engine == FIO_POSIXAIO)
843 return fio_posixaio_init(td);
844 else if (td->io_engine == FIO_SGIO)
845 return fio_sgio_init(td);
8756e4d4
JA
846 else if (td->io_engine == FIO_SPLICEIO)
847 return fio_spliceio_init(td);
ebac4655
JA
848 else {
849 fprintf(stderr, "bad io_engine %d\n", td->io_engine);
850 return 1;
851 }
852}
853
854static void cleanup_io_u(struct thread_data *td)
855{
856 struct list_head *entry, *n;
857 struct io_u *io_u;
858
859 list_for_each_safe(entry, n, &td->io_u_freelist) {
860 io_u = list_entry(entry, struct io_u, list);
861
862 list_del(&io_u->list);
863 free(io_u);
864 }
865
866 if (td->mem_type == MEM_MALLOC)
867 free(td->orig_buffer);
868 else if (td->mem_type == MEM_SHM) {
869 struct shmid_ds sbuf;
870
871 shmdt(td->orig_buffer);
872 shmctl(td->shm_id, IPC_RMID, &sbuf);
873 } else if (td->mem_type == MEM_MMAP)
874 munmap(td->orig_buffer, td->orig_buffer_size);
875 else
876 fprintf(stderr, "Bad memory type %d\n", td->mem_type);
877
878 td->orig_buffer = NULL;
879}
880
881static int init_io_u(struct thread_data *td)
882{
883 struct io_u *io_u;
884 int i, max_units;
885 char *p;
886
887 if (td->io_engine & FIO_SYNCIO)
888 max_units = 1;
889 else
890 max_units = td->iodepth;
891
892 td->orig_buffer_size = td->max_bs * max_units + MASK;
893
894 if (td->mem_type == MEM_MALLOC)
895 td->orig_buffer = malloc(td->orig_buffer_size);
896 else if (td->mem_type == MEM_SHM) {
897 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
898 if (td->shm_id < 0) {
899 td_verror(td, errno);
900 perror("shmget");
901 return 1;
902 }
903
904 td->orig_buffer = shmat(td->shm_id, NULL, 0);
905 if (td->orig_buffer == (void *) -1) {
906 td_verror(td, errno);
907 perror("shmat");
908 td->orig_buffer = NULL;
909 return 1;
910 }
911 } else if (td->mem_type == MEM_MMAP) {
912 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
913 if (td->orig_buffer == MAP_FAILED) {
914 td_verror(td, errno);
915 perror("mmap");
916 td->orig_buffer = NULL;
917 return 1;
918 }
919 }
920
ebac4655
JA
921 p = ALIGN(td->orig_buffer);
922 for (i = 0; i < max_units; i++) {
923 io_u = malloc(sizeof(*io_u));
924 memset(io_u, 0, sizeof(*io_u));
925 INIT_LIST_HEAD(&io_u->list);
926
927 io_u->buf = p + td->max_bs * i;
b1ff3403 928 io_u->index = i;
ebac4655
JA
929 list_add(&io_u->list, &td->io_u_freelist);
930 }
931
932 return 0;
933}
934
4b5bd14c
JA
935static void cleanup_allocs(struct thread_data *td)
936{
937 if (td->directory)
938 free(td->directory);
939 if (td->iolog_file)
940 free(td->iolog_file);
941 if (td->exec_prerun)
942 free(td->exec_prerun);
943 if (td->exec_postrun)
944 free(td->exec_postrun);
945 if (td->ioscheduler)
946 free(td->ioscheduler);
947 if (td->sysfs_root)
948 free(td->sysfs_root);
949}
950
ebac4655
JA
951static int create_file(struct thread_data *td, unsigned long long size,
952 int extend)
953{
954 unsigned long long left;
955 unsigned int bs;
956 int r, oflags;
957 char *b;
958
959 /*
960 * unless specifically asked for overwrite, let normal io extend it
961 */
962 if (td_write(td) && !td->overwrite)
963 return 0;
964
965 if (!size) {
966 fprintf(stderr, "Need size for create\n");
967 td_verror(td, EINVAL);
968 return 1;
969 }
970
971 if (!extend) {
972 oflags = O_CREAT | O_TRUNC;
973 printf("Client%d: Laying out IO file (%LuMiB)\n", td->thread_number, size >> 20);
974 } else {
975 oflags = O_APPEND;
976 printf("Client%d: Extending IO file (%Lu -> %LuMiB)\n", td->thread_number, (td->file_size - size) >> 20, td->file_size >> 20);
977 }
978
979 td->fd = open(td->file_name, O_WRONLY | oflags, 0644);
980 if (td->fd < 0) {
981 td_verror(td, errno);
982 return 1;
983 }
984
985 if (!extend && ftruncate(td->fd, td->file_size) == -1) {
986 td_verror(td, errno);
987 return 1;
988 }
989
990 td->io_size = td->file_size;
991 b = malloc(td->max_bs);
992 memset(b, 0, td->max_bs);
993
994 left = size;
995 while (left && !td->terminate) {
996 bs = td->max_bs;
997 if (bs > left)
998 bs = left;
999
1000 r = write(td->fd, b, bs);
1001
1002 if (r == (int) bs) {
1003 left -= bs;
1004 continue;
1005 } else {
1006 if (r < 0)
1007 td_verror(td, errno);
1008 else
1009 td_verror(td, EIO);
1010
1011 break;
1012 }
1013 }
1014
1015 if (td->terminate)
1016 unlink(td->file_name);
1017 else if (td->create_fsync)
1018 fsync(td->fd);
1019
1020 close(td->fd);
1021 td->fd = -1;
1022 free(b);
1023 return 0;
1024}
1025
1026static int file_size(struct thread_data *td)
1027{
1028 struct stat st;
1029
1030 if (fstat(td->fd, &st) == -1) {
1031 td_verror(td, errno);
1032 return 1;
1033 }
1034
838a3cd3
JA
1035 td->real_file_size = st.st_size;
1036
1037 if (!td->file_size || td->file_size > td->real_file_size)
1038 td->file_size = td->real_file_size;
ebac4655 1039
e8e387c1 1040 td->file_size -= td->file_offset;
ebac4655
JA
1041 return 0;
1042}
1043
1044static int bdev_size(struct thread_data *td)
1045{
9104f874 1046 unsigned long long bytes;
ebac4655
JA
1047 int r;
1048
1049 r = blockdev_size(td->fd, &bytes);
1050 if (r) {
1051 td_verror(td, r);
1052 return 1;
1053 }
1054
838a3cd3
JA
1055 td->real_file_size = bytes;
1056
ebac4655
JA
1057 /*
1058 * no extend possibilities, so limit size to device size if too large
1059 */
838a3cd3
JA
1060 if (!td->file_size || td->file_size > td->real_file_size)
1061 td->file_size = td->real_file_size;
ebac4655 1062
e8e387c1 1063 td->file_size -= td->file_offset;
ebac4655
JA
1064 return 0;
1065}
1066
1067static int get_file_size(struct thread_data *td)
1068{
0af7b542 1069 int ret = 0;
ebac4655
JA
1070
1071 if (td->filetype == FIO_TYPE_FILE)
1072 ret = file_size(td);
0af7b542 1073 else if (td->filetype == FIO_TYPE_BD)
ebac4655 1074 ret = bdev_size(td);
0af7b542
JA
1075 else
1076 td->real_file_size = -1;
ebac4655
JA
1077
1078 if (ret)
1079 return ret;
1080
e8e387c1
JA
1081 if (td->file_offset > td->real_file_size) {
1082 fprintf(stderr, "Client%d: offset extends end (%Lu > %Lu)\n", td->thread_number, td->file_offset, td->real_file_size);
ebac4655
JA
1083 return 1;
1084 }
1085
838a3cd3 1086 td->io_size = td->file_size;
ebac4655
JA
1087 if (td->io_size == 0) {
1088 fprintf(stderr, "Client%d: no io blocks\n", td->thread_number);
1089 td_verror(td, EINVAL);
1090 return 1;
1091 }
1092
20dc95c4
JA
1093 if (!td->zone_size)
1094 td->zone_size = td->io_size;
1095
ebac4655
JA
1096 td->total_io_size = td->io_size * td->loops;
1097 return 0;
1098}
1099
1100static int setup_file_mmap(struct thread_data *td)
1101{
1102 int flags;
1103
3d60d1ed
JA
1104 if (td_rw(td))
1105 flags = PROT_READ | PROT_WRITE;
1106 else if (td_write(td)) {
ebac4655
JA
1107 flags = PROT_WRITE;
1108
1109 if (td->verify != VERIFY_NONE)
1110 flags |= PROT_READ;
3d60d1ed
JA
1111 } else
1112 flags = PROT_READ;
ebac4655
JA
1113
1114 td->mmap = mmap(NULL, td->file_size, flags, MAP_SHARED, td->fd, td->file_offset);
1115 if (td->mmap == MAP_FAILED) {
1116 td->mmap = NULL;
1117 td_verror(td, errno);
1118 return 1;
1119 }
1120
1121 if (td->invalidate_cache) {
1122 if (madvise(td->mmap, td->file_size, MADV_DONTNEED) < 0) {
1123 td_verror(td, errno);
1124 return 1;
1125 }
1126 }
1127
1128 if (td->sequential) {
1129 if (madvise(td->mmap, td->file_size, MADV_SEQUENTIAL) < 0) {
1130 td_verror(td, errno);
1131 return 1;
1132 }
1133 } else {
1134 if (madvise(td->mmap, td->file_size, MADV_RANDOM) < 0) {
1135 td_verror(td, errno);
1136 return 1;
1137 }
1138 }
1139
1140 return 0;
1141}
1142
1143static int setup_file_plain(struct thread_data *td)
1144{
1145 if (td->invalidate_cache) {
1146 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_DONTNEED) < 0) {
1147 td_verror(td, errno);
1148 return 1;
1149 }
1150 }
1151
1152 if (td->sequential) {
1153 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_SEQUENTIAL) < 0) {
1154 td_verror(td, errno);
1155 return 1;
1156 }
1157 } else {
1158 if (fadvise(td->fd, td->file_offset, td->file_size, POSIX_FADV_RANDOM) < 0) {
1159 td_verror(td, errno);
1160 return 1;
1161 }
1162 }
1163
1164 return 0;
1165}
1166
1167static int setup_file(struct thread_data *td)
1168{
1169 struct stat st;
1170 int flags = 0;
1171
1172 if (stat(td->file_name, &st) == -1) {
1173 if (errno != ENOENT) {
1174 td_verror(td, errno);
1175 return 1;
1176 }
1177 if (!td->create_file) {
1178 td_verror(td, ENOENT);
1179 return 1;
1180 }
1181 if (create_file(td, td->file_size, 0))
1182 return 1;
1183 } else if (td->filetype == FIO_TYPE_FILE) {
6a0106a0 1184 if (st.st_size < (off_t) td->file_size) {
ebac4655
JA
1185 if (create_file(td, td->file_size - st.st_size, 1))
1186 return 1;
1187 }
1188 }
1189
1190 if (td->odirect)
1191 flags |= O_DIRECT;
1192
3d60d1ed 1193 if (td_write(td) || td_rw(td)) {
ebac4655
JA
1194 if (td->filetype == FIO_TYPE_FILE) {
1195 if (!td->overwrite)
1196 flags |= O_TRUNC;
1197
1198 flags |= O_CREAT;
1199 }
1200 if (td->sync_io)
1201 flags |= O_SYNC;
1202
1203 flags |= O_RDWR;
1204
1205 td->fd = open(td->file_name, flags, 0600);
3d60d1ed
JA
1206 } else {
1207 if (td->filetype == FIO_TYPE_CHAR)
1208 flags |= O_RDWR;
1209 else
1210 flags |= O_RDONLY;
1211
1212 td->fd = open(td->file_name, flags);
ebac4655
JA
1213 }
1214
1215 if (td->fd == -1) {
1216 td_verror(td, errno);
1217 return 1;
1218 }
1219
1220 if (get_file_size(td))
1221 return 1;
1222
1223 if (td->io_engine != FIO_MMAPIO)
1224 return setup_file_plain(td);
1225 else
1226 return setup_file_mmap(td);
1227}
1228
da86774e
JA
1229static int switch_ioscheduler(struct thread_data *td)
1230{
1231 char tmp[256], tmp2[128];
1232 FILE *f;
1233 int ret;
1234
1235 sprintf(tmp, "%s/queue/scheduler", td->sysfs_root);
1236
1237 f = fopen(tmp, "r+");
1238 if (!f) {
1239 td_verror(td, errno);
1240 return 1;
1241 }
1242
1243 /*
1244 * Set io scheduler.
1245 */
1246 ret = fwrite(td->ioscheduler, strlen(td->ioscheduler), 1, f);
1247 if (ferror(f) || ret != 1) {
1248 td_verror(td, errno);
1249 fclose(f);
1250 return 1;
1251 }
1252
1253 rewind(f);
1254
1255 /*
1256 * Read back and check that the selected scheduler is now the default.
1257 */
1258 ret = fread(tmp, 1, sizeof(tmp), f);
1259 if (ferror(f) || ret < 0) {
1260 td_verror(td, errno);
1261 fclose(f);
1262 return 1;
1263 }
1264
1265 sprintf(tmp2, "[%s]", td->ioscheduler);
1266 if (!strstr(tmp, tmp2)) {
1267 fprintf(stderr, "fio: io scheduler %s not found\n", td->ioscheduler);
1268 td_verror(td, EINVAL);
1269 fclose(f);
1270 return 1;
1271 }
1272
1273 fclose(f);
1274 return 0;
1275}
1276
ebac4655
JA
1277static void clear_io_state(struct thread_data *td)
1278{
1279 if (td->io_engine == FIO_SYNCIO)
1280 lseek(td->fd, SEEK_SET, 0);
1281
20dc95c4 1282 td->last_pos = 0;
ebac4655
JA
1283 td->stat_io_bytes[0] = td->stat_io_bytes[1] = 0;
1284 td->this_io_bytes[0] = td->this_io_bytes[1] = 0;
20dc95c4 1285 td->zone_bytes = 0;
ebac4655
JA
1286
1287 if (td->file_map)
1288 memset(td->file_map, 0, td->num_maps * sizeof(long));
1289}
1290
ebac4655
JA
1291static void *thread_main(void *data)
1292{
1293 struct thread_data *td = data;
ebac4655
JA
1294
1295 if (!td->use_thread)
1296 setsid();
1297
1298 td->pid = getpid();
1299
aea47d44
JA
1300 INIT_LIST_HEAD(&td->io_u_freelist);
1301 INIT_LIST_HEAD(&td->io_u_busylist);
1302 INIT_LIST_HEAD(&td->io_hist_list);
1303 INIT_LIST_HEAD(&td->io_log_list);
1304
ebac4655
JA
1305 if (init_io_u(td))
1306 goto err;
1307
1308 if (fio_setaffinity(td) == -1) {
1309 td_verror(td, errno);
1310 goto err;
1311 }
1312
1313 if (init_io(td))
1314 goto err;
1315
aea47d44
JA
1316 if (init_iolog(td))
1317 goto err;
1318
ebac4655
JA
1319 if (td->ioprio) {
1320 if (ioprio_set(IOPRIO_WHO_PROCESS, 0, td->ioprio) == -1) {
1321 td_verror(td, errno);
1322 goto err;
1323 }
1324 }
1325
b6f4d880
JA
1326 if (nice(td->nice) < 0) {
1327 td_verror(td, errno);
1328 goto err;
1329 }
1330
75154845
JA
1331 if (init_random_state(td))
1332 goto err;
1333
da86774e
JA
1334 if (td->ioscheduler && switch_ioscheduler(td))
1335 goto err;
1336
75154845 1337 td_set_runstate(td, TD_INITIALIZED);
ebac4655
JA
1338 sem_post(&startup_sem);
1339 sem_wait(&td->mutex);
1340
1341 if (!td->create_serialize && setup_file(td))
1342 goto err;
1343
ebac4655
JA
1344 gettimeofday(&td->epoch, NULL);
1345
4e0ba8af
JA
1346 if (td->exec_prerun)
1347 system(td->exec_prerun);
1348
ebac4655
JA
1349 while (td->loops--) {
1350 getrusage(RUSAGE_SELF, &td->ru_start);
1351 gettimeofday(&td->start, NULL);
1352 memcpy(&td->stat_sample_time, &td->start, sizeof(td->start));
1353
1354 if (td->ratemin)
1355 memcpy(&td->lastrate, &td->stat_sample_time, sizeof(td->lastrate));
1356
1357 clear_io_state(td);
1358 prune_io_piece_log(td);
1359
1360 do_io(td);
1361
1362 td->runtime[td->ddir] += mtime_since_now(&td->start);
aea47d44 1363 if (td_rw(td) && td->io_bytes[td->ddir ^ 1])
3d60d1ed
JA
1364 td->runtime[td->ddir ^ 1] = td->runtime[td->ddir];
1365
ebac4655
JA
1366 update_rusage_stat(td);
1367
1368 if (td->error || td->terminate)
1369 break;
1370
1371 if (td->verify == VERIFY_NONE)
1372 continue;
1373
1374 clear_io_state(td);
1375 gettimeofday(&td->start, NULL);
1376
1377 do_verify(td);
1378
1379 td->runtime[DDIR_READ] += mtime_since_now(&td->start);
1380
1381 if (td->error || td->terminate)
1382 break;
1383 }
1384
ebac4655
JA
1385 if (td->bw_log)
1386 finish_log(td, td->bw_log, "bw");
1387 if (td->slat_log)
1388 finish_log(td, td->slat_log, "slat");
1389 if (td->clat_log)
1390 finish_log(td, td->clat_log, "clat");
843a7413
JA
1391 if (td->write_iolog)
1392 write_iolog_close(td);
4e0ba8af
JA
1393 if (td->exec_postrun)
1394 system(td->exec_postrun);
ebac4655
JA
1395
1396 if (exitall_on_terminate)
1397 terminate_threads(td->groupid);
1398
1399err:
1400 if (td->fd != -1) {
1401 close(td->fd);
1402 td->fd = -1;
1403 }
1404 if (td->mmap)
1405 munmap(td->mmap, td->file_size);
4b5bd14c 1406 cleanup_allocs(td);
ebac4655
JA
1407 cleanup_io(td);
1408 cleanup_io_u(td);
ebac4655
JA
1409 td_set_runstate(td, TD_EXITED);
1410 return NULL;
1411
1412}
1413
1414static void *fork_main(int shmid, int offset)
1415{
1416 struct thread_data *td;
1417 void *data;
1418
1419 data = shmat(shmid, NULL, 0);
1420 if (data == (void *) -1) {
1421 perror("shmat");
1422 return NULL;
1423 }
1424
1425 td = data + offset * sizeof(struct thread_data);
1426 thread_main(td);
1427 shmdt(data);
1428 return NULL;
1429}
1430
ebac4655
JA
1431static void check_str_update(struct thread_data *td)
1432{
1433 char c = run_str[td->thread_number - 1];
1434
1435 if (td->runstate == td->old_runstate)
1436 return;
1437
1438 switch (td->runstate) {
1439 case TD_REAPED:
1440 c = '_';
1441 break;
1442 case TD_EXITED:
1443 c = 'E';
1444 break;
1445 case TD_RUNNING:
3d60d1ed
JA
1446 if (td_rw(td)) {
1447 if (td->sequential)
1448 c = 'M';
1449 else
1450 c = 'm';
1451 } else if (td_read(td)) {
ebac4655
JA
1452 if (td->sequential)
1453 c = 'R';
1454 else
1455 c = 'r';
1456 } else {
1457 if (td->sequential)
1458 c = 'W';
1459 else
1460 c = 'w';
1461 }
1462 break;
1463 case TD_VERIFYING:
1464 c = 'V';
1465 break;
1466 case TD_CREATED:
1467 c = 'C';
1468 break;
75154845
JA
1469 case TD_INITIALIZED:
1470 c = 'I';
1471 break;
ebac4655
JA
1472 case TD_NOT_CREATED:
1473 c = 'P';
1474 break;
1475 default:
1476 printf("state %d\n", td->runstate);
1477 }
1478
1479 run_str[td->thread_number - 1] = c;
1480 td->old_runstate = td->runstate;
1481}
1482
5289b847
JA
1483static void eta_to_str(char *str, int eta_sec)
1484{
1485 unsigned int d, h, m, s;
1486 static int always_d, always_h;
1487
1488 d = h = m = s = 0;
1489
1490 s = eta_sec % 60;
1491 eta_sec /= 60;
1492 m = eta_sec % 60;
1493 eta_sec /= 60;
1494 h = eta_sec % 24;
1495 eta_sec /= 24;
1496 d = eta_sec;
1497
1498 if (d || always_d) {
1499 always_d = 1;
1500 str += sprintf(str, "%02dd:", d);
1501 }
1502 if (h || always_h) {
1503 always_h = 1;
1504 str += sprintf(str, "%02dh:", h);
1505 }
1506
1507 str += sprintf(str, "%02dm:", m);
1508 str += sprintf(str, "%02ds", s);
1509}
1510
6a0106a0
JA
1511static int thread_eta(struct thread_data *td, unsigned long elapsed)
1512{
1513 unsigned long long bytes_total, bytes_done;
1514 unsigned int eta_sec = 0;
1515
1516 bytes_total = td->total_io_size;
3d60d1ed
JA
1517
1518 /*
1519 * if writing, bytes_total will be twice the size. If mixing,
1520 * assume a 50/50 split and thus bytes_total will be 50% larger.
1521 */
1522 if (td->verify) {
1523 if (td_rw(td))
1524 bytes_total = bytes_total * 3 / 2;
1525 else
1526 bytes_total <<= 1;
1527 }
6a0106a0
JA
1528 if (td->zone_size && td->zone_skip)
1529 bytes_total /= (td->zone_skip / td->zone_size);
1530
1531 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING) {
1532 double perc;
8b611c34 1533
6a0106a0
JA
1534 bytes_done = td->io_bytes[DDIR_READ] + td->io_bytes[DDIR_WRITE];
1535 perc = (double) bytes_done / (double) bytes_total;
1536 if (perc > 1.0)
1537 perc = 1.0;
1538
1539 eta_sec = (elapsed * (1.0 / perc)) - elapsed;
1540
8b611c34 1541 if (td->timeout && eta_sec > (td->timeout - elapsed))
6a0106a0 1542 eta_sec = td->timeout - elapsed;
75154845
JA
1543 } else if (td->runstate == TD_NOT_CREATED || td->runstate == TD_CREATED
1544 || td->runstate == TD_INITIALIZED) {
6a0106a0
JA
1545 int t_eta = 0, r_eta = 0;
1546
1547 /*
1548 * We can only guess - assume it'll run the full timeout
1549 * if given, otherwise assume it'll run at the specified rate.
1550 */
1551 if (td->timeout)
1552 t_eta = td->timeout + td->start_delay - elapsed;
1553 if (td->rate) {
1554 r_eta = (bytes_total / 1024) / td->rate;
1555 r_eta += td->start_delay - elapsed;
1556 }
1557
1558 if (r_eta && t_eta)
1559 eta_sec = min(r_eta, t_eta);
1560 else if (r_eta)
1561 eta_sec = r_eta;
1562 else if (t_eta)
1563 eta_sec = t_eta;
1564 else
1565 eta_sec = INT_MAX;
1566 } else {
1567 /*
1568 * thread is already done
1569 */
1570 eta_sec = 0;
1571 }
1572
1573 return eta_sec;
1574}
1575
ebac4655
JA
1576static void print_thread_status(void)
1577{
6a0106a0
JA
1578 unsigned long elapsed = time_since_now(&genesis);
1579 int i, nr_running, t_rate, m_rate, *eta_secs, eta_sec;
5289b847 1580 char eta_str[32];
6a0106a0
JA
1581 double perc = 0.0;
1582
1583 eta_secs = malloc(thread_number * sizeof(int));
1584 memset(eta_secs, 0, thread_number * sizeof(int));
ebac4655 1585
ebac4655
JA
1586 nr_running = t_rate = m_rate = 0;
1587 for (i = 0; i < thread_number; i++) {
1588 struct thread_data *td = &threads[i];
1589
1590 if (td->runstate == TD_RUNNING || td->runstate == TD_VERIFYING){
1591 nr_running++;
1592 t_rate += td->rate;
1593 m_rate += td->ratemin;
1594 }
1595
6a0106a0
JA
1596 if (elapsed >= 3)
1597 eta_secs[i] = thread_eta(td, elapsed);
8b611c34
JA
1598 else
1599 eta_secs[i] = INT_MAX;
ebac4655
JA
1600
1601 check_str_update(td);
1602 }
1603
6a0106a0
JA
1604 if (exitall_on_terminate)
1605 eta_sec = INT_MAX;
1606 else
1607 eta_sec = 0;
5289b847 1608
6a0106a0
JA
1609 for (i = 0; i < thread_number; i++) {
1610 if (exitall_on_terminate) {
1611 if (eta_secs[i] < eta_sec)
1612 eta_sec = eta_secs[i];
1613 } else {
1614 if (eta_secs[i] > eta_sec)
1615 eta_sec = eta_secs[i];
5289b847 1616 }
6a0106a0 1617 }
5289b847 1618
8b611c34 1619 if (eta_sec != INT_MAX && elapsed) {
6a0106a0
JA
1620 perc = (double) elapsed / (double) (elapsed + eta_sec);
1621 eta_to_str(eta_str, eta_sec);
ebac4655
JA
1622 }
1623
5289b847 1624 printf("Threads now running (%d)", nr_running);
ebac4655
JA
1625 if (m_rate || t_rate)
1626 printf(", commitrate %d/%dKiB/sec", t_rate, m_rate);
8b611c34 1627 if (eta_sec != INT_MAX) {
6a0106a0
JA
1628 perc *= 100.0;
1629 printf(": [%s] [%3.2f%% done] [eta %s]", run_str, perc,eta_str);
1630 }
5289b847 1631 printf("\r");
ebac4655 1632 fflush(stdout);
6a0106a0 1633 free(eta_secs);
ebac4655
JA
1634}
1635
1636static void reap_threads(int *nr_running, int *t_rate, int *m_rate)
1637{
1638 int i;
1639
1640 /*
1641 * reap exited threads (TD_EXITED -> TD_REAPED)
1642 */
1643 for (i = 0; i < thread_number; i++) {
1644 struct thread_data *td = &threads[i];
1645
1646 if (td->runstate != TD_EXITED)
1647 continue;
1648
1649 td_set_runstate(td, TD_REAPED);
1650
1651 if (td->use_thread) {
1652 long ret;
1653
1654 if (pthread_join(td->thread, (void *) &ret))
1655 perror("thread_join");
1656 } else
1657 waitpid(td->pid, NULL, 0);
1658
1659 (*nr_running)--;
1660 (*m_rate) -= td->ratemin;
1661 (*t_rate) -= td->rate;
1662 }
1663}
1664
fcb6ade2
JA
1665static void fio_unpin_memory(void *pinned)
1666{
1667 if (pinned) {
1668 if (munlock(pinned, mlock_size) < 0)
1669 perror("munlock");
1670 munmap(pinned, mlock_size);
1671 }
1672}
1673
1674static void *fio_pin_memory(void)
1675{
1676 long pagesize, pages;
1677 void *ptr;
1678
1679 if (!mlock_size)
1680 return NULL;
1681
1682 /*
1683 * Don't allow mlock of more than real_mem-128MB
1684 */
1685 pagesize = sysconf(_SC_PAGESIZE);
1686 pages = sysconf(_SC_PHYS_PAGES);
1687 if (pages != -1 && pagesize != -1) {
1688 unsigned long long real_mem = pages * pagesize;
1689
1690 if ((mlock_size + 128 * 1024 * 1024) > real_mem) {
1691 mlock_size = real_mem - 128 * 1024 * 1024;
1692 printf("fio: limiting mlocked memory to %lluMiB\n",
1693 mlock_size >> 20);
1694 }
1695 }
1696
1697 ptr = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
1698 if (!ptr) {
1699 perror("malloc locked mem");
1700 return NULL;
1701 }
1702 if (mlock(ptr, mlock_size) < 0) {
1703 munmap(ptr, mlock_size);
1704 perror("mlock");
1705 return NULL;
1706 }
1707
1708 return ptr;
1709}
1710
ebac4655
JA
1711static void run_threads(void)
1712{
ebac4655
JA
1713 struct thread_data *td;
1714 unsigned long spent;
1715 int i, todo, nr_running, m_rate, t_rate, nr_started;
fcb6ade2
JA
1716 void *mlocked_mem;
1717
1718 mlocked_mem = fio_pin_memory();
ebac4655
JA
1719
1720 printf("Starting %d thread%s\n", thread_number, thread_number > 1 ? "s" : "");
fcb6ade2 1721 fflush(stdout);
c04f7ec3 1722
4efa970e
JA
1723 signal(SIGINT, sig_handler);
1724 signal(SIGALRM, sig_handler);
1725
ebac4655
JA
1726 todo = thread_number;
1727 nr_running = 0;
1728 nr_started = 0;
1729 m_rate = t_rate = 0;
1730
1731 for (i = 0; i < thread_number; i++) {
1732 td = &threads[i];
1733
1734 run_str[td->thread_number - 1] = 'P';
1735
1736 init_disk_util(td);
1737
1738 if (!td->create_serialize)
1739 continue;
1740
1741 /*
1742 * do file setup here so it happens sequentially,
1743 * we don't want X number of threads getting their
1744 * client data interspersed on disk
1745 */
1746 if (setup_file(td)) {
1747 td_set_runstate(td, TD_REAPED);
1748 todo--;
1749 }
1750 }
1751
1752 gettimeofday(&genesis, NULL);
1753
1754 while (todo) {
75154845
JA
1755 struct thread_data *map[MAX_JOBS];
1756 struct timeval this_start;
1757 int this_jobs = 0, left;
1758
ebac4655
JA
1759 /*
1760 * create threads (TD_NOT_CREATED -> TD_CREATED)
1761 */
1762 for (i = 0; i < thread_number; i++) {
1763 td = &threads[i];
1764
1765 if (td->runstate != TD_NOT_CREATED)
1766 continue;
1767
1768 /*
1769 * never got a chance to start, killed by other
1770 * thread for some reason
1771 */
1772 if (td->terminate) {
1773 todo--;
1774 continue;
1775 }
1776
1777 if (td->start_delay) {
1778 spent = mtime_since_now(&genesis);
1779
1780 if (td->start_delay * 1000 > spent)
1781 continue;
1782 }
1783
1784 if (td->stonewall && (nr_started || nr_running))
1785 break;
1786
75154845
JA
1787 /*
1788 * Set state to created. Thread will transition
1789 * to TD_INITIALIZED when it's done setting up.
1790 */
ebac4655 1791 td_set_runstate(td, TD_CREATED);
75154845 1792 map[this_jobs++] = td;
ebac4655 1793 sem_init(&startup_sem, 0, 1);
ebac4655
JA
1794 nr_started++;
1795
1796 if (td->use_thread) {
1797 if (pthread_create(&td->thread, NULL, thread_main, td)) {
1798 perror("thread_create");
1799 nr_started--;
1800 }
1801 } else {
1802 if (fork())
1803 sem_wait(&startup_sem);
1804 else {
1805 fork_main(shm_id, i);
1806 exit(0);
1807 }
1808 }
1809 }
1810
1811 /*
75154845
JA
1812 * Wait for the started threads to transition to
1813 * TD_INITIALIZED.
ebac4655 1814 */
75154845
JA
1815 printf("fio: Waiting for threads to initialize...\n");
1816 gettimeofday(&this_start, NULL);
1817 left = this_jobs;
1818 while (left) {
1819 if (mtime_since_now(&this_start) > JOB_START_TIMEOUT)
1820 break;
1821
1822 usleep(100000);
1823
1824 for (i = 0; i < this_jobs; i++) {
1825 td = map[i];
1826 if (!td)
1827 continue;
b6f4d880 1828 if (td->runstate == TD_INITIALIZED) {
75154845
JA
1829 map[i] = NULL;
1830 left--;
b6f4d880
JA
1831 } else if (td->runstate >= TD_EXITED) {
1832 map[i] = NULL;
1833 left--;
1834 todo--;
1835 nr_running++; /* work-around... */
75154845
JA
1836 }
1837 }
1838 }
1839
1840 if (left) {
1841 fprintf(stderr, "fio: %d jobs failed to start\n", left);
1842 for (i = 0; i < this_jobs; i++) {
1843 td = map[i];
1844 if (!td)
1845 continue;
1846 kill(td->pid, SIGTERM);
1847 }
1848 break;
1849 }
1850
1851 /*
b6f4d880 1852 * start created threads (TD_INITIALIZED -> TD_RUNNING).
75154845
JA
1853 */
1854 printf("fio: Go for launch\n");
ebac4655
JA
1855 for (i = 0; i < thread_number; i++) {
1856 td = &threads[i];
1857
75154845 1858 if (td->runstate != TD_INITIALIZED)
ebac4655
JA
1859 continue;
1860
1861 td_set_runstate(td, TD_RUNNING);
1862 nr_running++;
1863 nr_started--;
1864 m_rate += td->ratemin;
1865 t_rate += td->rate;
75154845 1866 todo--;
ebac4655
JA
1867 sem_post(&td->mutex);
1868 }
1869
1870 reap_threads(&nr_running, &t_rate, &m_rate);
1871
1872 if (todo)
1873 usleep(100000);
1874 }
1875
1876 while (nr_running) {
1877 reap_threads(&nr_running, &t_rate, &m_rate);
1878 usleep(10000);
1879 }
1880
1881 update_io_ticks();
fcb6ade2 1882 fio_unpin_memory(mlocked_mem);
ebac4655
JA
1883}
1884
ebac4655
JA
1885int main(int argc, char *argv[])
1886{
1887 if (parse_options(argc, argv))
1888 return 1;
1889
1890 if (!thread_number) {
1891 printf("Nothing to do\n");
1892 return 1;
1893 }
1894
1895 disk_util_timer_arm();
1896
1897 run_threads();
1898 show_run_stats();
1899
1900 return 0;
1901}