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