a183d72d09bf8bb14b811338e5f07476f140ed11
[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 <semaphore.h>
10
11 #include "list.h"
12 #include "md5.h"
13 #include "crc32.h"
14 #include "arch.h"
15 #include "os.h"
16
17 struct io_stat {
18         unsigned long val;
19         unsigned long val_sq;
20         unsigned long max_val;
21         unsigned long min_val;
22         unsigned long samples;
23 };
24
25 struct io_sample {
26         unsigned long time;
27         unsigned long val;
28         unsigned int ddir;
29 };
30
31 struct io_log {
32         unsigned long nr_samples;
33         unsigned long max_samples;
34         struct io_sample *log;
35 };
36
37 struct io_piece {
38         struct list_head list;
39         unsigned long long offset;
40         unsigned int len;
41 };
42
43 /*
44  * The io unit
45  */
46 struct io_u {
47         union {
48 #ifdef FIO_HAVE_LIBAIO
49                 struct iocb iocb;
50 #endif
51 #ifdef FIO_HAVE_POSIXAIO
52                 struct aiocb aiocb;
53 #endif
54 #ifdef FIO_HAVE_SGIO
55                 struct sg_io_hdr hdr;
56 #endif
57         };
58         struct timeval start_time;
59         struct timeval issue_time;
60
61         char *buf;
62         unsigned int buflen;
63         unsigned long long offset;
64         unsigned int index;
65
66         unsigned int resid;
67         unsigned int error;
68
69         unsigned char seen;
70         unsigned char ddir;
71
72         struct list_head list;
73 };
74
75 #define FIO_HDR_MAGIC   0xf00baaef
76
77 enum {
78         VERIFY_NONE = 0,
79         VERIFY_MD5,
80         VERIFY_CRC32,
81 };
82
83 struct verify_header {
84         unsigned int fio_magic;
85         unsigned int len;
86         unsigned int verify_type;
87         union {
88                 char md5_digest[MD5_HASH_WORDS * 4];
89                 unsigned long crc32;
90         };
91 };
92
93 struct group_run_stats {
94         unsigned long long max_run[2], min_run[2];
95         unsigned long long max_bw[2], min_bw[2];
96         unsigned long long io_mb[2];
97         unsigned long long agg[2];
98 };
99
100 struct thread_data {
101         char file_name[256];
102         char directory[256];
103         char verror[80];
104         pthread_t thread;
105         int thread_number;
106         int groupid;
107         int filetype;
108         int error;
109         int fd;
110         void *mmap;
111         pid_t pid;
112         char *orig_buffer;
113         size_t orig_buffer_size;
114         volatile int terminate;
115         volatile int runstate;
116         volatile int old_runstate;
117         unsigned int ddir;
118         unsigned int ioprio;
119         unsigned int sequential;
120         unsigned int bs;
121         unsigned int min_bs;
122         unsigned int max_bs;
123         unsigned int odirect;
124         unsigned int thinktime;
125         unsigned int fsync_blocks;
126         unsigned int start_delay;
127         unsigned int timeout;
128         unsigned int io_engine;
129         unsigned int create_file;
130         unsigned int overwrite;
131         unsigned int invalidate_cache;
132         unsigned int bw_avg_time;
133         unsigned int create_serialize;
134         unsigned int create_fsync;
135         unsigned int loops;
136         unsigned long long file_size;
137         unsigned long long real_file_size;
138         unsigned long long file_offset;
139         unsigned long long zone_size;
140         unsigned long long zone_skip;
141         unsigned int sync_io;
142         unsigned int mem_type;
143         unsigned int verify;
144         unsigned int stonewall;
145         unsigned int numjobs;
146         unsigned int use_thread;
147         unsigned int iodepth;
148         os_cpu_mask_t cpumask;
149         unsigned int jobnum;
150
151         struct drand48_data bsrange_state;
152         struct drand48_data verify_state;
153
154         int shm_id;
155
156         void *io_data;
157         char io_engine_name[16];
158         int (*io_prep)(struct thread_data *, struct io_u *);
159         int (*io_queue)(struct thread_data *, struct io_u *);
160         int (*io_getevents)(struct thread_data *, int, int, struct timespec *);
161         struct io_u *(*io_event)(struct thread_data *, int);
162         int (*io_cancel)(struct thread_data *, struct io_u *);
163         void (*io_cleanup)(struct thread_data *);
164         int (*io_sync)(struct thread_data *);
165
166         unsigned int cur_depth;
167         struct list_head io_u_freelist;
168         struct list_head io_u_busylist;
169
170         unsigned int rate;
171         unsigned int ratemin;
172         unsigned int ratecycle;
173         unsigned long rate_usec_cycle;
174         long rate_pending_usleep;
175         unsigned long rate_bytes;
176         struct timeval lastrate;
177
178         unsigned long runtime[2];               /* msec */
179         unsigned long long io_size;
180         unsigned long long total_io_size;
181
182         unsigned long long io_blocks[2];
183         unsigned long long io_bytes[2];
184         unsigned long long zone_bytes;
185         unsigned long long this_io_bytes[2];
186         unsigned long long last_pos;
187         sem_t mutex;
188
189         struct drand48_data random_state;
190         unsigned long *file_map;
191         unsigned int num_maps;
192
193         /*
194          * bandwidth and latency stats
195          */
196         struct io_stat clat_stat[2];            /* completion latency */
197         struct io_stat slat_stat[2];            /* submission latency */
198         struct io_stat bw_stat[2];              /* bandwidth stats */
199
200         unsigned long long stat_io_bytes[2];
201         struct timeval stat_sample_time[2];
202
203         struct io_log *slat_log;
204         struct io_log *clat_log;
205         struct io_log *bw_log;
206
207         struct timeval start;   /* start of this loop */
208         struct timeval epoch;   /* time job was started */
209
210         struct rusage ru_start;
211         struct rusage ru_end;
212         unsigned long usr_time;
213         unsigned long sys_time;
214         unsigned long ctx;
215
216         unsigned int do_disk_util;
217         unsigned int override_sync;
218
219         struct list_head io_hist_list;
220 };
221
222 #define td_verror(td, err)                                              \
223         do {                                                            \
224                 int e = (err);                                          \
225                 (td)->error = e;                                        \
226                 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, error=%s", __FILE__, __LINE__, strerror(e));  \
227         } while (0)
228
229 extern int parse_jobs_ini(char *);
230 extern int parse_options(int, char **);
231 extern void finish_log(struct thread_data *, struct io_log *, const char *);
232 extern int init_random_state(struct thread_data *);
233 extern struct io_u *__get_io_u(struct thread_data *);
234 extern void put_io_u(struct thread_data *, struct io_u *);
235
236 extern int rate_quit;
237 extern int write_lat_log;
238 extern int write_bw_log;
239 extern int exitall_on_terminate;
240 extern int thread_number;
241 extern int shm_id;
242 extern int groupid;
243
244 extern struct thread_data *threads;
245
246 enum {
247         DDIR_READ = 0,
248         DDIR_WRITE,
249 };
250
251 /*
252  * What type of allocation to use for io buffers
253  */
254 enum {
255         MEM_MALLOC,     /* ordinary malloc */
256         MEM_SHM,        /* use shared memory segments */
257         MEM_MMAP,       /* use anonynomous mmap */
258 };
259
260 /*
261  * The type of object we are working on
262  */
263 enum {
264         FIO_TYPE_FILE = 1,
265         FIO_TYPE_BD,
266         FIO_TYPE_CHAR,
267 };
268
269 enum {
270         FIO_SYNCIO      = 1 << 0,
271         FIO_MMAPIO      = 1 << 1 | FIO_SYNCIO,
272         FIO_LIBAIO      = 1 << 2,
273         FIO_POSIXAIO    = 1 << 3,
274         FIO_SGIO        = 1 << 4,
275 };
276
277 #define td_read(td)             ((td)->ddir == DDIR_READ)
278 #define td_write(td)            ((td)->ddir == DDIR_WRITE)
279
280 #define BLOCKS_PER_MAP          (8 * sizeof(long))
281 #define TO_MAP_BLOCK(td, b)     ((b) - ((td)->file_offset / (td)->min_bs))
282 #define RAND_MAP_IDX(td, b)     (TO_MAP_BLOCK(td, b) / BLOCKS_PER_MAP)
283 #define RAND_MAP_BIT(td, b)     (TO_MAP_BLOCK(td, b) & (BLOCKS_PER_MAP - 1))
284
285 #define MAX_JOBS        (1024)
286
287 struct disk_util_stat {
288         unsigned ios[2];
289         unsigned merges[2];
290         unsigned long long sectors[2];
291         unsigned ticks[2];
292         unsigned io_ticks;
293         unsigned time_in_queue;
294 };
295
296 struct disk_util {
297         struct list_head list;
298
299         char *name;
300         char path[256];
301         dev_t dev;
302
303         struct disk_util_stat dus;
304         struct disk_util_stat last_dus;
305
306         unsigned long msec;
307         struct timeval time;
308 };
309
310 struct io_completion_data {
311         int nr;                         /* input */
312
313         int error;                      /* output */
314         unsigned long bytes_done[2];    /* output */
315 };
316
317 #define DISK_UTIL_MSEC  (250)
318
319 #ifndef min
320 #define min(a, b)       ((a) < (b) ? (a) : (b))
321 #endif
322
323 #endif