Improve eta calculation and display for complex jobs
[fio.git] / fio.h
CommitLineData
ebac4655
JA
1#ifndef FIO_H
2#define FIO_H
3
4#include <sched.h>
5#include <limits.h>
6#include <pthread.h>
7#include <sys/time.h>
8#include <sys/resource.h>
3c39a379
JA
9#include <errno.h>
10#include <stdlib.h>
11#include <stdio.h>
6d6f031f 12#include <unistd.h>
34cfcdaf 13#include <string.h>
214e1eca 14#include <getopt.h>
cd14cc10 15#include <inttypes.h>
7101d9c2 16#include <assert.h>
ebac4655 17
317b95d0 18#include "compiler/compiler.h"
ebac4655 19#include "list.h"
e2887563 20#include "fifo.h"
4b87898e 21#include "rbtree.h"
317b95d0
JA
22#include "arch/arch.h"
23#include "os/os.h"
07739b57 24#include "mutex.h"
a3d741fa
JA
25#include "log.h"
26#include "debug.h"
ebac4655 27
609342ff
DL
28#ifdef FIO_HAVE_GUASI
29#include <guasi.h>
30#endif
31
1e97cce9
JA
32enum fio_ddir {
33 DDIR_READ = 0,
34 DDIR_WRITE,
35 DDIR_SYNC,
f29b25a3 36 DDIR_INVAL = -1,
1e97cce9
JA
37};
38
413dd459 39enum td_ddir {
b1ec1da6
JA
40 TD_DDIR_READ = 1 << 0,
41 TD_DDIR_WRITE = 1 << 1,
42 TD_DDIR_RAND = 1 << 2,
43 TD_DDIR_RW = TD_DDIR_READ | TD_DDIR_WRITE,
44 TD_DDIR_RANDREAD = TD_DDIR_READ | TD_DDIR_RAND,
7ff1c14b 45 TD_DDIR_RANDWRITE = TD_DDIR_WRITE | TD_DDIR_RAND,
b1ec1da6 46 TD_DDIR_RANDRW = TD_DDIR_RW | TD_DDIR_RAND,
413dd459
JA
47};
48
4d4e80f2
JA
49enum file_lock_mode {
50 FILE_LOCK_NONE,
51 FILE_LOCK_EXCLUSIVE,
52 FILE_LOCK_READWRITE,
53};
54
1c9e06ee
JA
55/*
56 * Use for maintaining statistics
57 */
ebac4655 58struct io_stat {
ebac4655
JA
59 unsigned long max_val;
60 unsigned long min_val;
61 unsigned long samples;
68704084
JA
62
63 double mean;
64 double S;
ebac4655
JA
65};
66
1c9e06ee
JA
67/*
68 * A single data sample
69 */
ebac4655
JA
70struct io_sample {
71 unsigned long time;
72 unsigned long val;
1e97cce9 73 enum fio_ddir ddir;
ebac4655
JA
74};
75
1c9e06ee
JA
76/*
77 * Dynamically growing data sample log
78 */
ebac4655
JA
79struct io_log {
80 unsigned long nr_samples;
81 unsigned long max_samples;
82 struct io_sample *log;
83};
84
1c9e06ee
JA
85/*
86 * When logging io actions, this matches a single sent io_u
87 */
ebac4655 88struct io_piece {
4b87898e
JA
89 union {
90 struct rb_node rb_node;
91 struct list_head list;
92 };
f29b25a3
JA
93 union {
94 int fileno;
95 struct fio_file *file;
96 };
ebac4655 97 unsigned long long offset;
a4f4fdd7 98 unsigned long len;
1e97cce9 99 enum fio_ddir ddir;
f29b25a3
JA
100 union {
101 unsigned long delay;
102 unsigned int file_action;
103 };
ebac4655
JA
104};
105
0c6e7517
JA
106enum {
107 IO_U_F_FREE = 1 << 0,
108 IO_U_F_FLIGHT = 1 << 1,
109};
110
36690c9b
JA
111struct thread_data;
112
ebac4655
JA
113/*
114 * The io unit
115 */
116struct io_u {
117 union {
118#ifdef FIO_HAVE_LIBAIO
119 struct iocb iocb;
120#endif
121#ifdef FIO_HAVE_POSIXAIO
122 struct aiocb aiocb;
123#endif
124#ifdef FIO_HAVE_SGIO
125 struct sg_io_hdr hdr;
a4f4fdd7 126#endif
609342ff
DL
127#ifdef FIO_HAVE_GUASI
128 guasi_req_t greq;
ebac4655
JA
129#endif
130 };
131 struct timeval start_time;
132 struct timeval issue_time;
133
1c9e06ee
JA
134 /*
135 * Allocated/set buffer and length
136 */
6b9cea23 137 void *buf;
a4f4fdd7 138 unsigned long buflen;
ebac4655 139 unsigned long long offset;
d460eb31 140 unsigned long long endpos;
ebac4655 141
1c9e06ee
JA
142 /*
143 * IO engine state, may be different from above when we get
144 * partial transfers / residual data counts
145 */
cec6b55d 146 void *xfer_buf;
a4f4fdd7 147 unsigned long xfer_buflen;
cec6b55d 148
ebac4655
JA
149 unsigned int resid;
150 unsigned int error;
151
1e97cce9 152 enum fio_ddir ddir;
ebac4655 153
dfd7bc2c
JA
154 /*
155 * io engine private data
156 */
157 union {
158 unsigned int index;
159 unsigned int seen;
160 };
161
0c6e7517
JA
162 unsigned int flags;
163
53cdc686
JA
164 struct fio_file *file;
165
ebac4655 166 struct list_head list;
d7762cf8
JA
167
168 /*
169 * Callback for io completion
170 */
36690c9b 171 int (*end_io)(struct thread_data *, struct io_u *);
ebac4655
JA
172};
173
36167d82
JA
174/*
175 * io_ops->queue() return values
176 */
177enum {
178 FIO_Q_COMPLETED = 0, /* completed sync */
179 FIO_Q_QUEUED = 1, /* queued, will complete async */
755200a3 180 FIO_Q_BUSY = 2, /* no more room, call ->commit() */
36167d82
JA
181};
182
ebac4655
JA
183#define FIO_HDR_MAGIC 0xf00baaef
184
185enum {
1c9e06ee
JA
186 VERIFY_NONE = 0, /* no verification */
187 VERIFY_MD5, /* md5 sum data blocks */
d77a7af3 188 VERIFY_CRC64, /* crc64 sum data blocks */
1c9e06ee 189 VERIFY_CRC32, /* crc32 sum data blocks */
969f7ed3 190 VERIFY_CRC16, /* crc16 sum data blocks */
1e154bdb 191 VERIFY_CRC7, /* crc7 sum data blocks */
cd14cc10
JA
192 VERIFY_SHA256, /* sha256 sum data blocks */
193 VERIFY_SHA512, /* sha512 sum data blocks */
7437ee87 194 VERIFY_META, /* block_num, timestamp etc. */
36690c9b 195 VERIFY_NULL, /* pretend to verify */
ebac4655
JA
196};
197
1c9e06ee 198/*
546dfd9f
JA
199 * A header structure associated with each checksummed data block. It is
200 * followed by a checksum specific header that contains the verification
201 * data.
1c9e06ee 202 */
ebac4655
JA
203struct verify_header {
204 unsigned int fio_magic;
205 unsigned int len;
206 unsigned int verify_type;
546dfd9f
JA
207};
208
209struct vhdr_md5 {
210 uint32_t md5_digest[16];
211};
212struct vhdr_sha512 {
213 uint8_t sha512[128];
214};
215struct vhdr_sha256 {
216 uint8_t sha256[128];
217};
218struct vhdr_crc64 {
219 uint64_t crc64;
220};
221struct vhdr_crc32 {
222 uint32_t crc32;
223};
224struct vhdr_crc16 {
225 uint16_t crc16;
226};
227struct vhdr_crc7 {
228 uint8_t crc7;
ebac4655 229};
7437ee87
SL
230struct vhdr_meta {
231 uint64_t offset;
232 unsigned char thread;
233 unsigned short numberio;
234 unsigned long time_sec;
235 unsigned long time_usec;
236};
ebac4655
JA
237
238struct group_run_stats {
9104f874
JA
239 unsigned long long max_run[2], min_run[2];
240 unsigned long long max_bw[2], min_bw[2];
e9b2a3fa 241 unsigned long long io_kb[2];
9104f874 242 unsigned long long agg[2];
ebac4655
JA
243};
244
e9c047a0
JA
245/*
246 * What type of allocation to use for io buffers
247 */
248enum fio_memtype {
249 MEM_MALLOC = 0, /* ordinary malloc */
250 MEM_SHM, /* use shared memory segments */
74b025b0 251 MEM_SHMHUGE, /* use shared memory segments with huge pages */
e9c047a0 252 MEM_MMAP, /* use anonynomous mmap */
d0bdaf49 253 MEM_MMAPHUGE, /* memory mapped huge file */
e9c047a0
JA
254};
255
256/*
257 * The type of object we are working on
258 */
259enum fio_filetype {
1c9e06ee
JA
260 FIO_TYPE_FILE = 1, /* plain file */
261 FIO_TYPE_BD, /* block device */
262 FIO_TYPE_CHAR, /* character device */
b5605e9d 263 FIO_TYPE_PIPE, /* pipe */
e9c047a0
JA
264};
265
2866c82d 266enum fio_ioengine_flags {
1c9e06ee 267 FIO_SYNCIO = 1 << 0, /* io engine has synchronous ->queue */
ba0fbe10
JA
268 FIO_RAWIO = 1 << 1, /* some sort of direct/raw io */
269 FIO_DISKLESSIO = 1 << 2, /* no disk involved */
0263882a 270 FIO_NOEXTEND = 1 << 3, /* engine can't extend file */
62d984e2 271 FIO_NODISKUTIL = 1 << 4, /* diskutil can't handle filename */
b67740d3 272 FIO_UNIDIR = 1 << 5, /* engine is uni-directional */
1f809d15 273 FIO_NOIO = 1 << 6, /* thread does only pseudo IO */
ad830ed7 274 FIO_SIGQUIT = 1 << 7, /* needs SIGQUIT to exit */
e9c047a0
JA
275};
276
f11bd94d 277enum fio_file_flags {
0ad920e7 278 FIO_FILE_OPEN = 1 << 0, /* file is open */
a9b7b305 279 FIO_FILE_CLOSING = 1 << 1, /* file being closed */
6ae1f57f
JA
280 FIO_FILE_EXTEND = 1 << 2, /* needs extend */
281 FIO_FILE_DONE = 1 << 3, /* io completed to this file */
282 FIO_SIZE_KNOWN = 1 << 4, /* size has been set */
392a3742 283 FIO_FILE_HASHED = 1 << 5, /* file is on hash */
f11bd94d
JA
284};
285
1c9e06ee
JA
286/*
287 * Each thread_data structure has a number of files associated with it,
288 * this structure holds state information for a single file.
289 */
53cdc686 290struct fio_file {
392a3742 291 struct list_head hash_list;
af52b345
JA
292 enum fio_filetype filetype;
293
53cdc686
JA
294 /*
295 * A file may not be a file descriptor, let the io engine decide
296 */
297 union {
298 unsigned long file_data;
e26f8f7a 299 int fd;
53cdc686 300 };
7bb48f84
JA
301
302 /*
303 * filename and possible memory mapping
304 */
1315a68a 305 char *file_name;
53cdc686 306 void *mmap;
5e6c2067 307 unsigned int major, minor;
7bb48f84
JA
308
309 /*
310 * size of the file, offset into file, and io size from that offset
311 */
53cdc686
JA
312 unsigned long long real_file_size;
313 unsigned long long file_offset;
7bb48f84
JA
314 unsigned long long io_size;
315
53cdc686
JA
316 unsigned long long last_pos;
317
29c1349f
JA
318 /*
319 * if io is protected by a semaphore, this is set
320 */
4d4e80f2
JA
321 struct fio_mutex *lock;
322 void *lock_owner;
323 unsigned int lock_batch;
324 enum fio_ddir lock_ddir;
29c1349f 325
1c9e06ee
JA
326 /*
327 * block map for random io
328 */
53cdc686
JA
329 unsigned long *file_map;
330 unsigned int num_maps;
1c9e06ee 331 unsigned int last_free_lookup;
9b031dc8 332
0ad920e7 333 int references;
f11bd94d 334 enum fio_file_flags flags;
53cdc686
JA
335};
336
b2560f3c
JA
337/*
338 * How many depth levels to log
339 */
340#define FIO_IO_U_MAP_NR 8
a9a18a3f
JA
341#define FIO_IO_U_LAT_U_NR 10
342#define FIO_IO_U_LAT_M_NR 12
b2560f3c 343
079ad09b 344struct thread_stat {
756867bd
JA
345 char *name;
346 char *verror;
347 int error;
348 int groupid;
349 pid_t pid;
350 char *description;
6586ee89 351 int members;
756867bd 352
079ad09b
JA
353 struct io_log *slat_log;
354 struct io_log *clat_log;
355 struct io_log *bw_log;
356
357 /*
358 * bandwidth and latency stats
359 */
360 struct io_stat clat_stat[2]; /* completion latency */
361 struct io_stat slat_stat[2]; /* submission latency */
362 struct io_stat bw_stat[2]; /* bandwidth stats */
363
364 unsigned long long stat_io_bytes[2];
365 struct timeval stat_sample_time[2];
366
367 /*
368 * fio system usage accounting
369 */
370 struct rusage ru_start;
371 struct rusage ru_end;
372 unsigned long usr_time;
373 unsigned long sys_time;
374 unsigned long ctx;
81887d5d 375 unsigned long minf, majf;
079ad09b 376
b2560f3c
JA
377 /*
378 * IO depth and latency stats
379 */
380 unsigned int io_u_map[FIO_IO_U_MAP_NR];
04a0feae
JA
381 unsigned int io_u_lat_u[FIO_IO_U_LAT_U_NR];
382 unsigned int io_u_lat_m[FIO_IO_U_LAT_M_NR];
b3605062 383 unsigned long total_io_u[2];
30061b97 384 unsigned long short_io_u[2];
756867bd
JA
385
386 unsigned long long io_bytes[2];
387 unsigned long runtime[2];
388 unsigned long total_run_time;
b2560f3c 389};
71619dc2 390
564ca972
JA
391struct bssplit {
392 unsigned int bs;
393 unsigned char perc;
394};
395
2dc1bbeb 396struct thread_options {
b1ec1da6 397 int pad;
61697c37 398 char *description;
b4692828 399 char *name;
ef899b63 400 char *directory;
13f8e2d2 401 char *filename;
2dc1bbeb 402 char *opendir;
09629a90 403 char *ioengine;
413dd459 404 enum td_ddir td_ddir;
211097b2 405 unsigned int ddir_nr;
2dc1bbeb
JA
406 unsigned int iodepth;
407 unsigned int iodepth_low;
408 unsigned int iodepth_batch;
409
410 unsigned long long size;
aa31f1f1 411 unsigned int fill_device;
2dc1bbeb
JA
412 unsigned long long file_size_low;
413 unsigned long long file_size_high;
414 unsigned long long start_offset;
415
416 unsigned int bs[2];
417 unsigned int min_bs[2];
418 unsigned int max_bs[2];
564ca972
JA
419 struct bssplit *bssplit;
420 unsigned int bssplit_nr;
2dc1bbeb
JA
421
422 unsigned int nr_files;
423 unsigned int open_files;
4d4e80f2 424 enum file_lock_mode file_lock_mode;
29c1349f 425 unsigned int lockfile_batch;
e9c047a0 426
9158d2f7
JA
427 unsigned int odirect;
428 unsigned int invalidate_cache;
429 unsigned int create_serialize;
430 unsigned int create_fsync;
431 unsigned int end_fsync;
432 unsigned int sync_io;
433 unsigned int verify;
e84c73a8 434 unsigned int do_verify;
160b966d 435 unsigned int verifysort;
a59e170d
JA
436 unsigned int verify_interval;
437 unsigned int verify_offset;
90059d65
JA
438 unsigned int verify_pattern;
439 unsigned int verify_pattern_bytes;
a12a3b4d 440 unsigned int verify_fatal;
9158d2f7
JA
441 unsigned int use_thread;
442 unsigned int unlink;
443 unsigned int do_disk_util;
444 unsigned int override_sync;
445 unsigned int rand_repeatable;
446 unsigned int write_lat_log;
447 unsigned int write_bw_log;
bb8895e0 448 unsigned int norandommap;
2b386d25 449 unsigned int softrandommap;
690adba3 450 unsigned int bs_unaligned;
ebb1415f 451 unsigned int fsync_on_close;
e9c047a0 452
56bb17f2 453 unsigned int hugepage_size;
a00735e6 454 unsigned int rw_min_bs;
ebac4655 455 unsigned int thinktime;
48097d5c 456 unsigned int thinktime_spin;
9c1f7434 457 unsigned int thinktime_blocks;
ebac4655
JA
458 unsigned int fsync_blocks;
459 unsigned int start_delay;
0db26797 460 unsigned long long timeout;
ebac4655 461 unsigned int overwrite;
ebac4655 462 unsigned int bw_avg_time;
ebac4655 463 unsigned int loops;
20dc95c4
JA
464 unsigned long long zone_size;
465 unsigned long long zone_skip;
e9c047a0 466 enum fio_memtype mem_type;
2dc1bbeb 467
ebac4655 468 unsigned int stonewall;
b3d62a75 469 unsigned int new_group;
ebac4655 470 unsigned int numjobs;
ebac4655 471 os_cpu_mask_t cpumask;
375b2695 472 unsigned int cpumask_set;
aea47d44 473 unsigned int iolog;
a6ccc7be 474 unsigned int rwmixcycle;
e47f799f 475 unsigned int rwmix[2];
b6f4d880 476 unsigned int nice;
0aabe160 477 unsigned int file_service_type;
b2560f3c 478 unsigned int group_reporting;
d2f3ac35 479 unsigned int fadvise_hint;
e9459e5a 480 unsigned int zero_buffers;
cf4464ca 481 unsigned int time_based;
aea47d44 482
076efc7c
JA
483 char *read_iolog_file;
484 char *write_iolog_file;
2dc1bbeb
JA
485
486 /*
487 * Pre-run and post-run shell
488 */
489 char *exec_prerun;
490 char *exec_postrun;
491
492 unsigned int rate;
493 unsigned int ratemin;
494 unsigned int ratecycle;
495 unsigned int rate_iops;
496 unsigned int rate_iops_min;
497
498 char *ioscheduler;
499
500 /*
501 * CPU "io" cycle burner
502 */
503 unsigned int cpuload;
504 unsigned int cpucycle;
505};
506
e4e33258
JA
507#define FIO_VERROR_SIZE 128
508
2dc1bbeb
JA
509/*
510 * This describes a single thread/process executing a fio job.
511 */
512struct thread_data {
513 struct thread_options o;
e4e33258 514 char verror[FIO_VERROR_SIZE];
2dc1bbeb
JA
515 pthread_t thread;
516 int thread_number;
517 int groupid;
518 struct thread_stat ts;
126d65c6 519 struct fio_file **files;
2dc1bbeb
JA
520 unsigned int files_index;
521 unsigned int nr_open_files;
1020a139 522 unsigned int nr_done_files;
2dc1bbeb
JA
523 unsigned int nr_normal_files;
524 union {
525 unsigned int next_file;
526 os_random_state_t next_file_state;
527 };
528 int error;
20e354ef 529 int done;
2dc1bbeb
JA
530 pid_t pid;
531 char *orig_buffer;
532 size_t orig_buffer_size;
533 volatile int terminate;
534 volatile int runstate;
535 unsigned int ioprio;
ac684785 536 unsigned int ioprio_set;
2dc1bbeb
JA
537 unsigned int last_was_sync;
538
539 char *mmapfile;
540 int mmapfd;
541
843a7413
JA
542 void *iolog_buf;
543 FILE *iolog_f;
ebac4655 544
da86774e 545 char *sysfs_root;
da86774e 546
6dfd46b9
JA
547 os_random_state_t bsrange_state;
548 os_random_state_t verify_state;
ebac4655
JA
549
550 int shm_id;
551
e9c047a0
JA
552 /*
553 * IO engine hooks, contains everything needed to submit an io_u
554 * to any of the available IO engines.
555 */
2866c82d 556 struct ioengine_ops *io_ops;
ebac4655 557
e9c047a0
JA
558 /*
559 * Current IO depth and list of free and busy io_u's.
560 */
ebac4655
JA
561 unsigned int cur_depth;
562 struct list_head io_u_freelist;
563 struct list_head io_u_busylist;
755200a3 564 struct list_head io_u_requeues;
cb5ab512 565 unsigned int io_u_queued;
ebac4655 566
e9c047a0
JA
567 /*
568 * Rate state
569 */
ebac4655
JA
570 unsigned long rate_usec_cycle;
571 long rate_pending_usleep;
572 unsigned long rate_bytes;
4e991c23 573 unsigned long rate_blocks;
ebac4655
JA
574 struct timeval lastrate;
575
ebac4655
JA
576 unsigned long long total_io_size;
577
755200a3 578 unsigned long io_issues[2];
9104f874
JA
579 unsigned long long io_blocks[2];
580 unsigned long long io_bytes[2];
48f5abd3 581 unsigned long long io_skip_bytes;
9104f874 582 unsigned long long this_io_bytes[2];
079ad09b 583 unsigned long long zone_bytes;
cdd18ad8 584 struct fio_mutex *mutex;
ebac4655 585
e9c047a0
JA
586 /*
587 * State for random io, a bitmap of blocks done vs not done
588 */
6dfd46b9 589 os_random_state_t random_state;
ebac4655 590
ebac4655
JA
591 struct timeval start; /* start of this loop */
592 struct timeval epoch; /* time job was started */
38d77cae 593 struct timeval rw_end[2];
a61eddec 594 struct timeval last_issue;
38d77cae 595 unsigned int rw_end_set[2];
ebac4655 596
e9c047a0
JA
597 /*
598 * read/write mixed workload state
599 */
6dfd46b9 600 os_random_state_t rwmix_state;
afe24a5a 601 unsigned long long rwmix_bytes;
a6ccc7be 602 struct timeval rwmix_switch;
e9c047a0 603 enum fio_ddir rwmix_ddir;
211097b2 604 unsigned int ddir_nr;
a6ccc7be 605
e9c047a0 606 /*
8de8f047
JA
607 * IO history logs for verification. We use a tree for sorting,
608 * if we are overwriting. Otherwise just use a fifo.
e9c047a0 609 */
4b87898e 610 struct rb_root io_hist_tree;
8de8f047
JA
611 struct list_head io_hist_list;
612
613 /*
614 * For IO replaying
615 */
aea47d44 616 struct list_head io_log_list;
433afcb4
JA
617
618 /*
619 * timeout handling
620 */
621 struct timeval timeout_end;
622 struct itimerval timer;
1907dbc6
JA
623
624 /*
625 * for fileservice, how often to switch to a new file
626 */
627 unsigned int file_service_nr;
628 unsigned int file_service_left;
629 struct fio_file *file_service_file;
9c60ce64
JA
630
631 /*
632 * For generating file sizes
633 */
634 os_random_state_t file_size_state;
ebac4655
JA
635};
636
0aabe160
JA
637/*
638 * roundrobin available files, or choose one at random.
639 */
640enum {
641 FIO_FSERVICE_RANDOM = 1,
642 FIO_FSERVICE_RR = 2,
643};
644
e592a06b
AC
645/*
646 * when should interactive ETA output be generated
647 */
648enum {
649 FIO_ETA_AUTO,
650 FIO_ETA_ALWAYS,
651 FIO_ETA_NEVER,
652};
653
433afcb4
JA
654/*
655 * 30 second per-io_u timeout, with 5 second intervals to avoid resetting
656 * the timer on each queue operation.
657 */
658#define IO_U_TIMEOUT_INC 5
659#define IO_U_TIMEOUT 30
660
e1161c32 661#define __td_verror(td, err, msg, func) \
ebac4655 662 do { \
19abcd3d
JA
663 if ((td)->error) \
664 break; \
ebac4655
JA
665 int e = (err); \
666 (td)->error = e; \
e1161c32 667 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
ebac4655
JA
668 } while (0)
669
b990b5c0 670
e1161c32
JA
671#define td_verror(td, err, func) \
672 __td_verror((td), (err), strerror((err)), (func))
673#define td_vmsg(td, err, msg, func) \
674 __td_verror((td), (err), (msg), (func))
b990b5c0 675
ebac4655
JA
676extern int exitall_on_terminate;
677extern int thread_number;
9cedf167 678extern int nr_process, nr_thread;
ebac4655
JA
679extern int shm_id;
680extern int groupid;
c6ae0a5b 681extern int terse_output;
53cdc686 682extern int temp_stall_ts;
1e97cce9 683extern unsigned long long mlock_size;
cfc99db7 684extern unsigned long page_mask, page_size;
4241ea8f 685extern int read_only;
e592a06b 686extern int eta_print;
d3eeeabc 687extern unsigned long done_secs;
01f06b63 688extern char *job_section;
ebac4655
JA
689
690extern struct thread_data *threads;
691
2dc1bbeb
JA
692#define td_read(td) ((td)->o.td_ddir & TD_DDIR_READ)
693#define td_write(td) ((td)->o.td_ddir & TD_DDIR_WRITE)
694#define td_rw(td) (((td)->o.td_ddir & TD_DDIR_RW) == TD_DDIR_RW)
695#define td_random(td) ((td)->o.td_ddir & TD_DDIR_RAND)
303032ae 696#define file_randommap(td, f) (!(td)->o.norandommap && (f)->file_map)
ebac4655 697
7101d9c2
JA
698static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
699{
700 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
701}
702
ebac4655 703#define BLOCKS_PER_MAP (8 * sizeof(long))
84422acd 704#define TO_MAP_BLOCK(td, f, b) (b)
53cdc686
JA
705#define RAND_MAP_IDX(td, f, b) (TO_MAP_BLOCK(td, f, b) / BLOCKS_PER_MAP)
706#define RAND_MAP_BIT(td, f, b) (TO_MAP_BLOCK(td, f, b) & (BLOCKS_PER_MAP - 1))
ebac4655
JA
707
708#define MAX_JOBS (1024)
709
87dc1ab1
JA
710static inline int should_fsync(struct thread_data *td)
711{
712 if (td->last_was_sync)
713 return 0;
2dc1bbeb 714 if (td->o.odirect)
87dc1ab1 715 return 0;
2dc1bbeb 716 if (td_write(td) || td_rw(td) || td->o.override_sync)
87dc1ab1
JA
717 return 1;
718
719 return 0;
720}
721
1c9e06ee
JA
722/*
723 * Disk utils as read in /sys/block/<dev>/stat
724 */
ebac4655
JA
725struct disk_util_stat {
726 unsigned ios[2];
727 unsigned merges[2];
728 unsigned long long sectors[2];
729 unsigned ticks[2];
730 unsigned io_ticks;
731 unsigned time_in_queue;
732};
733
1c9e06ee
JA
734/*
735 * Per-device disk util management
736 */
ebac4655
JA
737struct disk_util {
738 struct list_head list;
739
740 char *name;
e11c410c 741 char *sysfs_root;
ebac4655 742 char path[256];
07e5b264 743 int major, minor;
ebac4655
JA
744
745 struct disk_util_stat dus;
746 struct disk_util_stat last_dus;
747
748 unsigned long msec;
749 struct timeval time;
750};
751
ebac4655
JA
752#define DISK_UTIL_MSEC (250)
753
6796209a
JA
754/*
755 * Log exports
756 */
f29b25a3
JA
757enum file_log_act {
758 FIO_LOG_ADD_FILE,
759 FIO_LOG_OPEN_FILE,
760 FIO_LOG_CLOSE_FILE,
f718273e 761 FIO_LOG_UNLINK_FILE,
f29b25a3
JA
762};
763
72cb971b 764extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
f29b25a3
JA
765extern void log_io_u(struct thread_data *, struct io_u *);
766extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
72cb971b 767extern int __must_check init_iolog(struct thread_data *td);
6796209a 768extern void log_io_piece(struct thread_data *, struct io_u *);
691c8fb0 769extern void queue_io_piece(struct thread_data *, struct io_piece *);
6796209a
JA
770extern void prune_io_piece_log(struct thread_data *);
771extern void write_iolog_close(struct thread_data *);
772
773/*
774 * Logging
775 */
1e97cce9
JA
776extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long);
777extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long);
778extern void add_bw_sample(struct thread_data *, enum fio_ddir, struct timeval *);
6796209a
JA
779extern void show_run_stats(void);
780extern void init_disk_util(struct thread_data *);
781extern void update_rusage_stat(struct thread_data *);
782extern void update_io_ticks(void);
783extern void disk_util_timer_arm(void);
8914a9d8
JA
784extern void setup_log(struct io_log **);
785extern void finish_log(struct thread_data *, struct io_log *, const char *);
bb3884d8 786extern void __finish_log(struct io_log *, const char *);
bb3884d8
JA
787extern struct io_log *agg_io_log[2];
788extern int write_bw_log;
789extern void add_agg_sample(unsigned long, enum fio_ddir);
6796209a
JA
790
791/*
792 * Time functions
793 */
10927316
SL
794extern unsigned long long utime_since(struct timeval *, struct timeval *);
795extern unsigned long long utime_since_now(struct timeval *);
6796209a
JA
796extern unsigned long mtime_since(struct timeval *, struct timeval *);
797extern unsigned long mtime_since_now(struct timeval *);
798extern unsigned long time_since_now(struct timeval *);
263e529f 799extern unsigned long mtime_since_genesis(void);
b990b5c0 800extern void __usec_sleep(unsigned int);
6796209a 801extern void usec_sleep(struct thread_data *, unsigned long);
413dd459 802extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
6043c579 803extern void fill_start_time(struct timeval *);
02bcaa8c 804extern void fio_gettime(struct timeval *, void *);
a2f77c9f 805extern void set_genesis_time(void);
6796209a 806
8914a9d8 807/*
214e1eca 808 * Init/option functions
8914a9d8 809 */
72cb971b 810extern int __must_check parse_options(int, char **);
214e1eca
JA
811extern int fio_option_parse(struct thread_data *, const char *);
812extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
813extern void fio_fill_default_options(struct thread_data *);
814extern int fio_show_option_help(const char *);
815extern void fio_options_dup_and_init(struct option *);
d23bb327
JA
816extern void options_mem_dupe(struct thread_data *);
817extern void options_mem_free(struct thread_data *);
214e1eca
JA
818#define FIO_GETOPT_JOB 0x89988998
819#define FIO_NR_OPTIONS 128
8914a9d8 820
53cdc686
JA
821/*
822 * File setup/shutdown
823 */
824extern void close_files(struct thread_data *);
24ffd2c2 825extern void close_and_free_files(struct thread_data *);
b2fdda43
JA
826extern int __must_check setup_files(struct thread_data *);
827extern int __must_check open_files(struct thread_data *);
828extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
b5af8293 829extern int __must_check generic_open_file(struct thread_data *, struct fio_file *);
6977bcd0 830extern int __must_check generic_close_file(struct thread_data *, struct fio_file *);
f29b25a3 831extern int add_file(struct thread_data *, const char *);
0ad920e7 832extern void get_file(struct fio_file *);
6977bcd0 833extern int __must_check put_file(struct thread_data *, struct fio_file *);
4d4e80f2
JA
834extern void lock_file(struct thread_data *, struct fio_file *, enum fio_ddir);
835extern void unlock_file(struct thread_data *, struct fio_file *);
836extern void unlock_file_all(struct thread_data *, struct fio_file *);
bbf6b540 837extern int add_dir_files(struct thread_data *, const char *);
68727076 838extern int init_random_map(struct thread_data *);
cade3ef4 839extern void dup_files(struct thread_data *, struct thread_data *);
f29b25a3
JA
840extern int get_fileno(struct thread_data *, const char *);
841extern void free_release_files(struct thread_data *);
53cdc686 842
263e529f
JA
843/*
844 * ETA/status stuff
845 */
846extern void print_thread_status(void);
847extern void print_status_init(int);
848
9f8f2064
JA
849/*
850 * disk util stuff
851 */
852#ifdef FIO_HAVE_DISK_UTIL
853extern void show_disk_util(void);
854extern void disk_util_timer_arm(void);
855extern void init_disk_util(struct thread_data *);
856extern void update_io_ticks(void);
857#else
858#define show_disk_util()
859#define disk_util_timer_arm()
860#define init_disk_util(td)
861#define update_io_ticks()
862#endif
863
263e529f
JA
864/*
865 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
866 * will never back again. It may cycle between running/verififying/fsyncing.
867 * Once the thread reaches TD_EXITED, it is just waiting for the core to
868 * reap it.
869 */
870enum {
871 TD_NOT_CREATED = 0,
872 TD_CREATED,
873 TD_INITIALIZED,
874 TD_RUNNING,
875 TD_VERIFYING,
876 TD_FSYNCING,
877 TD_EXITED,
878 TD_REAPED,
879};
880
e29d1b70
JA
881/*
882 * Verify helpers
883 */
884extern void populate_verify_io_u(struct thread_data *, struct io_u *);
b2fdda43 885extern int __must_check get_next_verify(struct thread_data *td, struct io_u *);
36690c9b 886extern int __must_check verify_io_u(struct thread_data *, struct io_u *);
e29d1b70 887
2f9ade3c
JA
888/*
889 * Memory helpers
890 */
b2fdda43 891extern int __must_check fio_pin_memory(void);
2f9ade3c 892extern void fio_unpin_memory(void);
b2fdda43 893extern int __must_check allocate_io_mem(struct thread_data *);
2f9ade3c
JA
894extern void free_io_mem(struct thread_data *);
895
10ba535a
JA
896/*
897 * io unit handling
898 */
899#define queue_full(td) list_empty(&(td)->io_u_freelist)
900extern struct io_u *__get_io_u(struct thread_data *);
3d7c391d 901extern struct io_u *get_io_u(struct thread_data *);
10ba535a 902extern void put_io_u(struct thread_data *, struct io_u *);
755200a3 903extern void requeue_io_u(struct thread_data *, struct io_u **);
d7762cf8
JA
904extern long __must_check io_u_sync_complete(struct thread_data *, struct io_u *);
905extern long __must_check io_u_queued_complete(struct thread_data *, int);
7e77dd02 906extern void io_u_queued(struct thread_data *, struct io_u *);
5451792e 907extern void io_u_log_error(struct thread_data *, struct io_u *);
433afcb4
JA
908extern void io_u_init_timeout(void);
909extern void io_u_set_timeout(struct thread_data *);
b3605062 910extern void io_u_mark_depth(struct thread_data *, struct io_u *);
10ba535a
JA
911
912/*
913 * io engine entry points
914 */
b2fdda43
JA
915extern int __must_check td_io_init(struct thread_data *);
916extern int __must_check td_io_prep(struct thread_data *, struct io_u *);
917extern int __must_check td_io_queue(struct thread_data *, struct io_u *);
918extern int __must_check td_io_sync(struct thread_data *, struct fio_file *);
e7d2e616 919extern int __must_check td_io_getevents(struct thread_data *, unsigned int, unsigned int, struct timespec *);
b2fdda43 920extern int __must_check td_io_commit(struct thread_data *);
b5af8293 921extern int __must_check td_io_open_file(struct thread_data *, struct fio_file *);
6977bcd0 922extern int td_io_close_file(struct thread_data *, struct fio_file *);
10ba535a 923
fb7b71a3
JA
924/*
925 * blktrace support
926 */
5e62c22a 927#ifdef FIO_HAVE_BLKTRACE
fb7b71a3
JA
928extern int is_blktrace(const char *);
929extern int load_blktrace(struct thread_data *, const char *);
5e62c22a 930#endif
fb7b71a3 931
2866c82d 932struct ioengine_ops {
5f350952 933 struct list_head list;
2866c82d
JA
934 char name[16];
935 int version;
936 int flags;
ea2877a4 937 int (*setup)(struct thread_data *);
2866c82d
JA
938 int (*init)(struct thread_data *);
939 int (*prep)(struct thread_data *, struct io_u *);
940 int (*queue)(struct thread_data *, struct io_u *);
755200a3 941 int (*commit)(struct thread_data *);
e7d2e616 942 int (*getevents)(struct thread_data *, unsigned int, unsigned int, struct timespec *);
2866c82d
JA
943 struct io_u *(*event)(struct thread_data *, int);
944 int (*cancel)(struct thread_data *, struct io_u *);
945 void (*cleanup)(struct thread_data *);
b5af8293 946 int (*open_file)(struct thread_data *, struct fio_file *);
6977bcd0 947 int (*close_file)(struct thread_data *, struct fio_file *);
2866c82d
JA
948 void *data;
949 void *dlhandle;
950};
951
6977bcd0 952#define FIO_IOOPS_VERSION 9
2866c82d 953
b4692828 954extern struct ioengine_ops *load_ioengine(struct thread_data *, const char *);
b2fdda43 955extern void register_ioengine(struct ioengine_ops *);
5f350952 956extern void unregister_ioengine(struct ioengine_ops *);
2866c82d
JA
957extern void close_ioengine(struct thread_data *);
958
959/*
960 * Mark unused variables passed to ops functions as unused, to silence gcc
961 */
962#define fio_unused __attribute((__unused__))
5f350952
JA
963#define fio_init __attribute__((constructor))
964#define fio_exit __attribute__((destructor))
2866c82d 965
34572e28
JA
966#define for_each_td(td, i) \
967 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
53cdc686 968#define for_each_file(td, f, i) \
691c8fb0
JA
969 if ((td)->files_index) \
970 for ((i) = 0, (f) = (td)->files[0]; \
971 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
972 (i)++)
53cdc686 973
0032bf9f
JA
974#define fio_assert(td, cond) do { \
975 if (!(cond)) { \
340fd243 976 int *__foo = NULL; \
0032bf9f
JA
977 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
978 (td)->runstate = TD_EXITED; \
437c9b71 979 (td)->error = EFAULT; \
340fd243 980 *__foo = 0; \
0032bf9f
JA
981 } \
982} while (0)
983
9bf27b45
JA
984static inline void clear_error(struct thread_data *td)
985{
986 td->error = 0;
987 td->verror[0] = '\0';
988}
989
79e48f72 990#ifdef FIO_INC_DEBUG
2ba1c290
JA
991static inline void dprint_io_u(struct io_u *io_u, const char *p)
992{
993 struct fio_file *f = io_u->file;
994
995 dprint(FD_IO, "%s: io_u %p: off=%llu/len=%lu/ddir=%d", p, io_u,
996 io_u->offset, io_u->buflen, io_u->ddir);
8172fe97
JA
997 if (fio_debug & (1 << FD_IO)) {
998 if (f)
999 log_info("/%s", f->file_name);
1000
1001 log_info("\n");
1002 }
2ba1c290 1003}
79e48f72 1004#else
79e48f72
JA
1005#define dprint_io_u(io_u, p)
1006#endif
2ba1c290 1007
ebac4655 1008#endif