blob: a87fcef472a951a45e174acd5e811628ad789319 [file] [log] [blame]
Jens Axboeebac4652005-12-08 15:25:21 +01001#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>
Jens Axboe3c39a372006-06-06 20:56:12 +02009#include <errno.h>
10#include <stdlib.h>
11#include <stdio.h>
Jens Axboe6d6f0312006-06-07 21:39:13 +020012#include <unistd.h>
Jens Axboeebac4652005-12-08 15:25:21 +010013
14#include "list.h"
15#include "md5.h"
16#include "crc32.h"
17#include "arch.h"
18#include "os.h"
19
20struct io_stat {
21 unsigned long val;
22 unsigned long val_sq;
23 unsigned long max_val;
24 unsigned long min_val;
25 unsigned long samples;
26};
27
28struct io_sample {
29 unsigned long time;
30 unsigned long val;
31 unsigned int ddir;
32};
33
34struct io_log {
35 unsigned long nr_samples;
36 unsigned long max_samples;
37 struct io_sample *log;
38};
39
40struct io_piece {
41 struct list_head list;
42 unsigned long long offset;
43 unsigned int len;
Jens Axboeaea47d42006-05-26 19:27:29 +020044 int ddir;
Jens Axboeebac4652005-12-08 15:25:21 +010045};
46
47/*
48 * The io unit
49 */
50struct io_u {
51 union {
52#ifdef FIO_HAVE_LIBAIO
53 struct iocb iocb;
54#endif
55#ifdef FIO_HAVE_POSIXAIO
56 struct aiocb aiocb;
57#endif
58#ifdef FIO_HAVE_SGIO
59 struct sg_io_hdr hdr;
60#endif
61 };
62 struct timeval start_time;
63 struct timeval issue_time;
64
65 char *buf;
66 unsigned int buflen;
67 unsigned long long offset;
Jens Axboeb1ff3402006-02-17 11:35:58 +010068 unsigned int index;
Jens Axboeebac4652005-12-08 15:25:21 +010069
70 unsigned int resid;
71 unsigned int error;
72
73 unsigned char seen;
74 unsigned char ddir;
75
76 struct list_head list;
77};
78
79#define FIO_HDR_MAGIC 0xf00baaef
80
81enum {
82 VERIFY_NONE = 0,
83 VERIFY_MD5,
84 VERIFY_CRC32,
85};
86
87struct verify_header {
88 unsigned int fio_magic;
89 unsigned int len;
90 unsigned int verify_type;
91 union {
92 char md5_digest[MD5_HASH_WORDS * 4];
93 unsigned long crc32;
94 };
95};
96
97struct group_run_stats {
Jens Axboe9104f872005-12-28 23:50:45 +010098 unsigned long long max_run[2], min_run[2];
99 unsigned long long max_bw[2], min_bw[2];
Jens Axboee9b2a3f2006-06-06 15:38:33 +0200100 unsigned long long io_kb[2];
Jens Axboe9104f872005-12-28 23:50:45 +0100101 unsigned long long agg[2];
Jens Axboeebac4652005-12-08 15:25:21 +0100102};
103
Jens Axboee9c047a2006-06-07 21:13:04 +0200104enum fio_ddir {
105 DDIR_READ = 0,
106 DDIR_WRITE,
107};
108
109/*
110 * What type of allocation to use for io buffers
111 */
112enum fio_memtype {
113 MEM_MALLOC = 0, /* ordinary malloc */
114 MEM_SHM, /* use shared memory segments */
115 MEM_MMAP, /* use anonynomous mmap */
116};
117
118/*
119 * The type of object we are working on
120 */
121enum fio_filetype {
122 FIO_TYPE_FILE = 1,
123 FIO_TYPE_BD,
124 FIO_TYPE_CHAR,
125};
126
127enum fio_iotype {
128 FIO_SYNCIO = 1 << 0,
129 FIO_MMAPIO = 1 << 1 | FIO_SYNCIO,
130 FIO_LIBAIO = 1 << 2,
131 FIO_POSIXAIO = 1 << 3,
132 FIO_SGIO = 1 << 4,
133 FIO_SPLICEIO = 1 << 5 | FIO_SYNCIO,
134};
135
136/*
137 * This describes a single thread/process executing a fio job.
138 */
Jens Axboeebac4652005-12-08 15:25:21 +0100139struct thread_data {
Jens Axboee9c047a2006-06-07 21:13:04 +0200140 char name[32];
141 char *file_name;
Jens Axboeef899b62006-06-06 09:31:00 +0200142 char *directory;
Jens Axboeebac4652005-12-08 15:25:21 +0100143 char verror[80];
144 pthread_t thread;
145 int thread_number;
146 int groupid;
Jens Axboee9c047a2006-06-07 21:13:04 +0200147 enum fio_filetype filetype;
Jens Axboeebac4652005-12-08 15:25:21 +0100148 int error;
149 int fd;
150 void *mmap;
151 pid_t pid;
152 char *orig_buffer;
153 size_t orig_buffer_size;
154 volatile int terminate;
155 volatile int runstate;
Jens Axboee9c047a2006-06-07 21:13:04 +0200156 enum fio_ddir ddir;
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200157 unsigned int iomix;
Jens Axboeebac4652005-12-08 15:25:21 +0100158 unsigned int ioprio;
Jens Axboee9c047a2006-06-07 21:13:04 +0200159
160 unsigned char sequential;
161 unsigned char odirect;
162 unsigned char create_file;
163 unsigned char invalidate_cache;
164 unsigned char create_serialize;
165 unsigned char create_fsync;
166 unsigned char end_fsync;
167 unsigned char sync_io;
168 unsigned char verify;
169 unsigned char use_thread;
170 unsigned char do_disk_util;
171 unsigned char override_sync;
Jens Axboe9ebc27e2006-06-12 10:21:50 +0200172 unsigned char rand_repeatable;
Jens Axboee9c047a2006-06-07 21:13:04 +0200173
Jens Axboeebac4652005-12-08 15:25:21 +0100174 unsigned int bs;
175 unsigned int min_bs;
176 unsigned int max_bs;
Jens Axboeebac4652005-12-08 15:25:21 +0100177 unsigned int thinktime;
178 unsigned int fsync_blocks;
179 unsigned int start_delay;
Jens Axboe906c8d72006-06-13 09:37:56 +0200180 unsigned long timeout;
Jens Axboee9c047a2006-06-07 21:13:04 +0200181 enum fio_iotype io_engine;
Jens Axboeebac4652005-12-08 15:25:21 +0100182 unsigned int overwrite;
Jens Axboeebac4652005-12-08 15:25:21 +0100183 unsigned int bw_avg_time;
Jens Axboeebac4652005-12-08 15:25:21 +0100184 unsigned int loops;
185 unsigned long long file_size;
Jens Axboe838a3cd2006-01-20 12:38:29 +0100186 unsigned long long real_file_size;
Jens Axboeebac4652005-12-08 15:25:21 +0100187 unsigned long long file_offset;
Jens Axboe20dc95c2005-12-09 10:29:35 +0100188 unsigned long long zone_size;
189 unsigned long long zone_skip;
Jens Axboee9c047a2006-06-07 21:13:04 +0200190 enum fio_memtype mem_type;
Jens Axboeebac4652005-12-08 15:25:21 +0100191 unsigned int stonewall;
192 unsigned int numjobs;
Jens Axboeebac4652005-12-08 15:25:21 +0100193 unsigned int iodepth;
194 os_cpu_mask_t cpumask;
Jens Axboeaea47d42006-05-26 19:27:29 +0200195 unsigned int iolog;
Jens Axboe843a7412006-06-01 21:14:21 -0700196 unsigned int read_iolog;
197 unsigned int write_iolog;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200198 unsigned int rwmixcycle;
199 unsigned int rwmixread;
Jens Axboeb6f4d882006-06-02 10:32:51 +0200200 unsigned int nice;
Jens Axboeaea47d42006-05-26 19:27:29 +0200201
Jens Axboeef899b62006-06-06 09:31:00 +0200202 char *iolog_file;
Jens Axboe843a7412006-06-01 21:14:21 -0700203 void *iolog_buf;
204 FILE *iolog_f;
Jens Axboeebac4652005-12-08 15:25:21 +0100205
Jens Axboeda867742006-06-06 10:39:10 -0700206 char *sysfs_root;
Jens Axboeda867742006-06-06 10:39:10 -0700207 char *ioscheduler;
208
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200209 os_random_state_t bsrange_state;
210 os_random_state_t verify_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100211
212 int shm_id;
213
Jens Axboee9c047a2006-06-07 21:13:04 +0200214 /*
215 * IO engine hooks, contains everything needed to submit an io_u
216 * to any of the available IO engines.
217 */
Jens Axboeebac4652005-12-08 15:25:21 +0100218 void *io_data;
219 char io_engine_name[16];
220 int (*io_prep)(struct thread_data *, struct io_u *);
221 int (*io_queue)(struct thread_data *, struct io_u *);
222 int (*io_getevents)(struct thread_data *, int, int, struct timespec *);
223 struct io_u *(*io_event)(struct thread_data *, int);
224 int (*io_cancel)(struct thread_data *, struct io_u *);
225 void (*io_cleanup)(struct thread_data *);
226 int (*io_sync)(struct thread_data *);
227
Jens Axboee9c047a2006-06-07 21:13:04 +0200228 /*
229 * Current IO depth and list of free and busy io_u's.
230 */
Jens Axboeebac4652005-12-08 15:25:21 +0100231 unsigned int cur_depth;
232 struct list_head io_u_freelist;
233 struct list_head io_u_busylist;
234
Jens Axboee9c047a2006-06-07 21:13:04 +0200235 /*
236 * Rate state
237 */
Jens Axboeebac4652005-12-08 15:25:21 +0100238 unsigned int rate;
239 unsigned int ratemin;
240 unsigned int ratecycle;
241 unsigned long rate_usec_cycle;
242 long rate_pending_usleep;
243 unsigned long rate_bytes;
244 struct timeval lastrate;
245
246 unsigned long runtime[2]; /* msec */
247 unsigned long long io_size;
248 unsigned long long total_io_size;
249
Jens Axboe9104f872005-12-28 23:50:45 +0100250 unsigned long long io_blocks[2];
251 unsigned long long io_bytes[2];
252 unsigned long long zone_bytes;
253 unsigned long long this_io_bytes[2];
Jens Axboe20dc95c2005-12-09 10:29:35 +0100254 unsigned long long last_pos;
Jens Axboebbfd6b02006-06-07 19:42:54 +0200255 volatile int mutex;
Jens Axboeebac4652005-12-08 15:25:21 +0100256
Jens Axboee9c047a2006-06-07 21:13:04 +0200257 /*
258 * State for random io, a bitmap of blocks done vs not done
259 */
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200260 os_random_state_t random_state;
Jens Axboeebac4652005-12-08 15:25:21 +0100261 unsigned long *file_map;
262 unsigned int num_maps;
263
264 /*
265 * bandwidth and latency stats
266 */
267 struct io_stat clat_stat[2]; /* completion latency */
268 struct io_stat slat_stat[2]; /* submission latency */
269 struct io_stat bw_stat[2]; /* bandwidth stats */
270
Jens Axboe9104f872005-12-28 23:50:45 +0100271 unsigned long long stat_io_bytes[2];
Jens Axboeebac4652005-12-08 15:25:21 +0100272 struct timeval stat_sample_time[2];
273
274 struct io_log *slat_log;
275 struct io_log *clat_log;
276 struct io_log *bw_log;
277
278 struct timeval start; /* start of this loop */
279 struct timeval epoch; /* time job was started */
280
Jens Axboee9c047a2006-06-07 21:13:04 +0200281 /*
282 * fio system usage accounting
283 */
Jens Axboeebac4652005-12-08 15:25:21 +0100284 struct rusage ru_start;
285 struct rusage ru_end;
286 unsigned long usr_time;
287 unsigned long sys_time;
288 unsigned long ctx;
289
Jens Axboee9c047a2006-06-07 21:13:04 +0200290 /*
291 * read/write mixed workload state
292 */
Jens Axboe6dfd46b2006-06-07 13:57:06 +0200293 os_random_state_t rwmix_state;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200294 struct timeval rwmix_switch;
Jens Axboee9c047a2006-06-07 21:13:04 +0200295 enum fio_ddir rwmix_ddir;
Jens Axboea6ccc7b2006-06-02 10:14:15 +0200296
Jens Axboe4e0ba8a2006-06-06 09:36:28 +0200297 /*
298 * Pre-run and post-run shell
299 */
300 char *exec_prerun;
301 char *exec_postrun;
302
Jens Axboee9c047a2006-06-07 21:13:04 +0200303 /*
304 * IO historic logs
305 */
Jens Axboeebac4652005-12-08 15:25:21 +0100306 struct list_head io_hist_list;
Jens Axboeaea47d42006-05-26 19:27:29 +0200307 struct list_head io_log_list;
Jens Axboeebac4652005-12-08 15:25:21 +0100308};
309
310#define td_verror(td, err) \
311 do { \
312 int e = (err); \
313 (td)->error = e; \
314 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, error=%s", __FILE__, __LINE__, strerror(e)); \
315 } while (0)
316
Jens Axboeb1ff3402006-02-17 11:35:58 +0100317extern struct io_u *__get_io_u(struct thread_data *);
318extern void put_io_u(struct thread_data *, struct io_u *);
Jens Axboeebac4652005-12-08 15:25:21 +0100319
320extern int rate_quit;
321extern int write_lat_log;
322extern int write_bw_log;
323extern int exitall_on_terminate;
324extern int thread_number;
325extern int shm_id;
326extern int groupid;
Jens Axboec6ae0a52006-06-12 11:04:44 +0200327extern int terse_output;
Jens Axboeeb8bbf42006-06-08 21:40:11 +0200328extern FILE *f_out;
329extern FILE *f_err;
Jens Axboeebac4652005-12-08 15:25:21 +0100330
331extern struct thread_data *threads;
332
Jens Axboeebac4652005-12-08 15:25:21 +0100333#define td_read(td) ((td)->ddir == DDIR_READ)
334#define td_write(td) ((td)->ddir == DDIR_WRITE)
Jens Axboe3d60d1e2006-05-25 06:31:06 +0200335#define td_rw(td) ((td)->iomix != 0)
Jens Axboeebac4652005-12-08 15:25:21 +0100336
337#define BLOCKS_PER_MAP (8 * sizeof(long))
338#define TO_MAP_BLOCK(td, b) ((b) - ((td)->file_offset / (td)->min_bs))
339#define RAND_MAP_IDX(td, b) (TO_MAP_BLOCK(td, b) / BLOCKS_PER_MAP)
340#define RAND_MAP_BIT(td, b) (TO_MAP_BLOCK(td, b) & (BLOCKS_PER_MAP - 1))
341
342#define MAX_JOBS (1024)
343
344struct disk_util_stat {
345 unsigned ios[2];
346 unsigned merges[2];
347 unsigned long long sectors[2];
348 unsigned ticks[2];
349 unsigned io_ticks;
350 unsigned time_in_queue;
351};
352
353struct disk_util {
354 struct list_head list;
355
356 char *name;
357 char path[256];
358 dev_t dev;
359
360 struct disk_util_stat dus;
361 struct disk_util_stat last_dus;
362
363 unsigned long msec;
364 struct timeval time;
365};
366
367struct io_completion_data {
368 int nr; /* input */
369
370 int error; /* output */
371 unsigned long bytes_done[2]; /* output */
372};
373
374#define DISK_UTIL_MSEC (250)
375
Jens Axboe6a0106a2006-05-05 10:34:43 +0200376#ifndef min
377#define min(a, b) ((a) < (b) ? (a) : (b))
378#endif
379
Jens Axboe67962092006-06-07 08:45:01 +0200380/*
381 * Log exports
382 */
383extern int read_iolog_get(struct thread_data *, struct io_u *);
384extern void write_iolog_put(struct thread_data *, struct io_u *);
385extern int init_iolog(struct thread_data *td);
386extern void log_io_piece(struct thread_data *, struct io_u *);
387extern void prune_io_piece_log(struct thread_data *);
388extern void write_iolog_close(struct thread_data *);
389
390/*
391 * Logging
392 */
393extern void add_clat_sample(struct thread_data *, int, unsigned long);
394extern void add_slat_sample(struct thread_data *, int, unsigned long);
395extern void add_bw_sample(struct thread_data *, int);
396extern void show_run_stats(void);
397extern void init_disk_util(struct thread_data *);
398extern void update_rusage_stat(struct thread_data *);
399extern void update_io_ticks(void);
400extern void disk_util_timer_arm(void);
Jens Axboe8914a9d2006-06-07 11:14:56 +0200401extern void setup_log(struct io_log **);
402extern void finish_log(struct thread_data *, struct io_log *, const char *);
403extern int setup_rate(struct thread_data *);
Jens Axboe67962092006-06-07 08:45:01 +0200404
405/*
406 * Time functions
407 */
408extern unsigned long utime_since(struct timeval *, struct timeval *);
409extern unsigned long mtime_since(struct timeval *, struct timeval *);
410extern unsigned long mtime_since_now(struct timeval *);
411extern unsigned long time_since_now(struct timeval *);
412extern void usec_sleep(struct thread_data *, unsigned long);
413extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
414
Jens Axboe8914a9d2006-06-07 11:14:56 +0200415/*
416 * Init functions
417 */
418extern int parse_options(int, char **);
419extern int init_random_state(struct thread_data *);
420
Jens Axboebbfd6b02006-06-07 19:42:54 +0200421/*
422 * This is a pretty crappy semaphore implementation, but with the use that fio
423 * has (just signalling start/go conditions), it doesn't have to be better.
424 * Naturally this would not work for any type of contended semaphore or
425 * for real locking.
426 */
Jens Axboe1056eaa2006-07-13 10:33:45 -0700427static inline void fio_sem_init(volatile int *sem, int val)
Jens Axboebbfd6b02006-06-07 19:42:54 +0200428{
429 *sem = val;
430}
431
Jens Axboe1056eaa2006-07-13 10:33:45 -0700432static inline void fio_sem_down(volatile int *sem)
Jens Axboebbfd6b02006-06-07 19:42:54 +0200433{
434 while (*sem == 0)
435 usleep(10000);
436
437 (*sem)--;
438}
439
Jens Axboe1056eaa2006-07-13 10:33:45 -0700440static inline void fio_sem_up(volatile int *sem)
Jens Axboebbfd6b02006-06-07 19:42:54 +0200441{
442 (*sem)++;
443}
444
Jens Axboe3b70d7e2006-06-08 21:48:46 +0200445/*
446 * If logging output to a file, stderr should go to both stderr and f_err
447 */
448#define log_err(args...) do { \
449 fprintf(f_err, ##args); \
450 if (f_err != stderr) \
451 fprintf(stderr, ##args); \
452 } while (0)
453
Jens Axboeebac4652005-12-08 15:25:21 +0100454#endif