09e56d66cb4079cb013a1c9e8d55e9cafbe62d6f
[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
13 #include "list.h"
14 #include "md5.h"
15 #include "crc32.h"
16 #include "arch.h"
17 #include "os.h"
18
19 struct io_stat {
20         unsigned long val;
21         unsigned long val_sq;
22         unsigned long max_val;
23         unsigned long min_val;
24         unsigned long samples;
25 };
26
27 struct io_sample {
28         unsigned long time;
29         unsigned long val;
30         unsigned int ddir;
31 };
32
33 struct io_log {
34         unsigned long nr_samples;
35         unsigned long max_samples;
36         struct io_sample *log;
37 };
38
39 struct io_piece {
40         struct list_head list;
41         unsigned long long offset;
42         unsigned int len;
43         int ddir;
44 };
45
46 /*
47  * The io unit
48  */
49 struct io_u {
50         union {
51 #ifdef FIO_HAVE_LIBAIO
52                 struct iocb iocb;
53 #endif
54 #ifdef FIO_HAVE_POSIXAIO
55                 struct aiocb aiocb;
56 #endif
57 #ifdef FIO_HAVE_SGIO
58                 struct sg_io_hdr hdr;
59 #endif
60         };
61         struct timeval start_time;
62         struct timeval issue_time;
63
64         char *buf;
65         unsigned int buflen;
66         unsigned long long offset;
67         unsigned int index;
68
69         unsigned int resid;
70         unsigned int error;
71
72         unsigned char seen;
73         unsigned char ddir;
74
75         struct list_head list;
76 };
77
78 #define FIO_HDR_MAGIC   0xf00baaef
79
80 enum {
81         VERIFY_NONE = 0,
82         VERIFY_MD5,
83         VERIFY_CRC32,
84 };
85
86 struct verify_header {
87         unsigned int fio_magic;
88         unsigned int len;
89         unsigned int verify_type;
90         union {
91                 char md5_digest[MD5_HASH_WORDS * 4];
92                 unsigned long crc32;
93         };
94 };
95
96 struct group_run_stats {
97         unsigned long long max_run[2], min_run[2];
98         unsigned long long max_bw[2], min_bw[2];
99         unsigned long long io_kb[2];
100         unsigned long long agg[2];
101 };
102
103 struct thread_data {
104         char name[64];
105         char file_name[256];
106         char *directory;
107         char verror[80];
108         pthread_t thread;
109         int thread_number;
110         int groupid;
111         int filetype;
112         int error;
113         int fd;
114         void *mmap;
115         pid_t pid;
116         char *orig_buffer;
117         size_t orig_buffer_size;
118         volatile int terminate;
119         volatile int runstate;
120         volatile int old_runstate;
121         unsigned int ddir;
122         unsigned int iomix;
123         unsigned int ioprio;
124         unsigned int sequential;
125         unsigned int bs;
126         unsigned int min_bs;
127         unsigned int max_bs;
128         unsigned int odirect;
129         unsigned int thinktime;
130         unsigned int fsync_blocks;
131         unsigned int start_delay;
132         unsigned int timeout;
133         unsigned int io_engine;
134         unsigned int create_file;
135         unsigned int overwrite;
136         unsigned int invalidate_cache;
137         unsigned int bw_avg_time;
138         unsigned int create_serialize;
139         unsigned int create_fsync;
140         unsigned int end_fsync;
141         unsigned int loops;
142         unsigned long long file_size;
143         unsigned long long real_file_size;
144         unsigned long long file_offset;
145         unsigned long long zone_size;
146         unsigned long long zone_skip;
147         unsigned int sync_io;
148         unsigned int mem_type;
149         unsigned int verify;
150         unsigned int stonewall;
151         unsigned int numjobs;
152         unsigned int use_thread;
153         unsigned int iodepth;
154         os_cpu_mask_t cpumask;
155         unsigned int jobnum;
156         unsigned int iolog;
157         unsigned int read_iolog;
158         unsigned int write_iolog;
159         unsigned int rwmixcycle;
160         unsigned int rwmixread;
161         unsigned int nice;
162
163         char *iolog_file;
164         void *iolog_buf;
165         FILE *iolog_f;
166
167         char *sysfs_root;
168
169         char *ioscheduler;
170
171         os_random_state_t bsrange_state;
172         os_random_state_t verify_state;
173
174         int shm_id;
175
176         void *io_data;
177         char io_engine_name[16];
178         int (*io_prep)(struct thread_data *, struct io_u *);
179         int (*io_queue)(struct thread_data *, struct io_u *);
180         int (*io_getevents)(struct thread_data *, int, int, struct timespec *);
181         struct io_u *(*io_event)(struct thread_data *, int);
182         int (*io_cancel)(struct thread_data *, struct io_u *);
183         void (*io_cleanup)(struct thread_data *);
184         int (*io_sync)(struct thread_data *);
185
186         unsigned int cur_depth;
187         struct list_head io_u_freelist;
188         struct list_head io_u_busylist;
189
190         unsigned int rate;
191         unsigned int ratemin;
192         unsigned int ratecycle;
193         unsigned long rate_usec_cycle;
194         long rate_pending_usleep;
195         unsigned long rate_bytes;
196         struct timeval lastrate;
197
198         unsigned long runtime[2];               /* msec */
199         unsigned long long io_size;
200         unsigned long long total_io_size;
201
202         unsigned long long io_blocks[2];
203         unsigned long long io_bytes[2];
204         unsigned long long zone_bytes;
205         unsigned long long this_io_bytes[2];
206         unsigned long long last_pos;
207         volatile int mutex;
208
209         os_random_state_t random_state;
210         unsigned long *file_map;
211         unsigned int num_maps;
212
213         /*
214          * bandwidth and latency stats
215          */
216         struct io_stat clat_stat[2];            /* completion latency */
217         struct io_stat slat_stat[2];            /* submission latency */
218         struct io_stat bw_stat[2];              /* bandwidth stats */
219
220         unsigned long long stat_io_bytes[2];
221         struct timeval stat_sample_time[2];
222
223         struct io_log *slat_log;
224         struct io_log *clat_log;
225         struct io_log *bw_log;
226
227         struct timeval start;   /* start of this loop */
228         struct timeval epoch;   /* time job was started */
229
230         struct rusage ru_start;
231         struct rusage ru_end;
232         unsigned long usr_time;
233         unsigned long sys_time;
234         unsigned long ctx;
235
236         unsigned int do_disk_util;
237         unsigned int override_sync;
238
239         os_random_state_t rwmix_state;
240         struct timeval rwmix_switch;
241         int rwmix_ddir;
242
243         /*
244          * Pre-run and post-run shell
245          */
246         char *exec_prerun;
247         char *exec_postrun;
248
249         struct list_head io_hist_list;
250         struct list_head io_log_list;
251 };
252
253 #define td_verror(td, err)                                              \
254         do {                                                            \
255                 int e = (err);                                          \
256                 (td)->error = e;                                        \
257                 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, error=%s", __FILE__, __LINE__, strerror(e));  \
258         } while (0)
259
260 extern struct io_u *__get_io_u(struct thread_data *);
261 extern void put_io_u(struct thread_data *, struct io_u *);
262
263 extern int rate_quit;
264 extern int write_lat_log;
265 extern int write_bw_log;
266 extern int exitall_on_terminate;
267 extern int thread_number;
268 extern int shm_id;
269 extern int groupid;
270
271 extern struct thread_data *threads;
272
273 enum {
274         DDIR_READ = 0,
275         DDIR_WRITE,
276 };
277
278 /*
279  * What type of allocation to use for io buffers
280  */
281 enum {
282         MEM_MALLOC,     /* ordinary malloc */
283         MEM_SHM,        /* use shared memory segments */
284         MEM_MMAP,       /* use anonynomous mmap */
285 };
286
287 /*
288  * The type of object we are working on
289  */
290 enum {
291         FIO_TYPE_FILE = 1,
292         FIO_TYPE_BD,
293         FIO_TYPE_CHAR,
294 };
295
296 enum {
297         FIO_SYNCIO      = 1 << 0,
298         FIO_MMAPIO      = 1 << 1 | FIO_SYNCIO,
299         FIO_LIBAIO      = 1 << 2,
300         FIO_POSIXAIO    = 1 << 3,
301         FIO_SGIO        = 1 << 4,
302         FIO_SPLICEIO    = 1 << 5 | FIO_SYNCIO,
303 };
304
305 #define td_read(td)             ((td)->ddir == DDIR_READ)
306 #define td_write(td)            ((td)->ddir == DDIR_WRITE)
307 #define td_rw(td)               ((td)->iomix != 0)
308
309 #define BLOCKS_PER_MAP          (8 * sizeof(long))
310 #define TO_MAP_BLOCK(td, b)     ((b) - ((td)->file_offset / (td)->min_bs))
311 #define RAND_MAP_IDX(td, b)     (TO_MAP_BLOCK(td, b) / BLOCKS_PER_MAP)
312 #define RAND_MAP_BIT(td, b)     (TO_MAP_BLOCK(td, b) & (BLOCKS_PER_MAP - 1))
313
314 #define MAX_JOBS        (1024)
315
316 struct disk_util_stat {
317         unsigned ios[2];
318         unsigned merges[2];
319         unsigned long long sectors[2];
320         unsigned ticks[2];
321         unsigned io_ticks;
322         unsigned time_in_queue;
323 };
324
325 struct disk_util {
326         struct list_head list;
327
328         char *name;
329         char path[256];
330         dev_t dev;
331
332         struct disk_util_stat dus;
333         struct disk_util_stat last_dus;
334
335         unsigned long msec;
336         struct timeval time;
337 };
338
339 struct io_completion_data {
340         int nr;                         /* input */
341
342         int error;                      /* output */
343         unsigned long bytes_done[2];    /* output */
344 };
345
346 #define DISK_UTIL_MSEC  (250)
347
348 #ifndef min
349 #define min(a, b)       ((a) < (b) ? (a) : (b))
350 #endif
351
352 /*
353  * Log exports
354  */
355 extern int read_iolog_get(struct thread_data *, struct io_u *);
356 extern void write_iolog_put(struct thread_data *, struct io_u *);
357 extern int init_iolog(struct thread_data *td);
358 extern void log_io_piece(struct thread_data *, struct io_u *);
359 extern void prune_io_piece_log(struct thread_data *);
360 extern void write_iolog_close(struct thread_data *);
361
362 /*
363  * Logging
364  */
365 extern void add_clat_sample(struct thread_data *, int, unsigned long);
366 extern void add_slat_sample(struct thread_data *, int, unsigned long);
367 extern void add_bw_sample(struct thread_data *, int);
368 extern void show_run_stats(void);
369 extern void init_disk_util(struct thread_data *);
370 extern void update_rusage_stat(struct thread_data *);
371 extern void update_io_ticks(void);
372 extern void disk_util_timer_arm(void);
373 extern void setup_log(struct io_log **);
374 extern void finish_log(struct thread_data *, struct io_log *, const char *);
375 extern int setup_rate(struct thread_data *);
376
377 /*
378  * Time functions
379  */
380 extern unsigned long utime_since(struct timeval *, struct timeval *);
381 extern unsigned long mtime_since(struct timeval *, struct timeval *);
382 extern unsigned long mtime_since_now(struct timeval *);
383 extern unsigned long time_since_now(struct timeval *);
384 extern void usec_sleep(struct thread_data *, unsigned long);
385 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
386
387 /*
388  * Init functions
389  */
390 extern int parse_options(int, char **);
391 extern int init_random_state(struct thread_data *);
392
393 /*
394  * This is a pretty crappy semaphore implementation, but with the use that fio
395  * has (just signalling start/go conditions), it doesn't have to be better.
396  * Naturally this would not work for any type of contended semaphore or
397  * for real locking.
398  */
399 static inline void fio_sem_init(volatile int volatile *sem, int val)
400 {
401         *sem = val;
402 }
403
404 static inline void fio_sem_down(volatile int volatile *sem)
405 {
406         while (*sem == 0)
407                 usleep(10000);
408
409         (*sem)--;
410 }
411
412 static inline void fio_sem_up(volatile int volatile *sem)
413 {
414         (*sem)++;
415 }
416
417 #endif