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