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