[PATCH] Make it easier to match up filename and job number
[fio.git] / fio.h
CommitLineData
ebac4655
JA
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>
3c39a379
JA
9#include <errno.h>
10#include <stdlib.h>
11#include <stdio.h>
6d6f031f 12#include <unistd.h>
ebac4655
JA
13
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;
aea47d44 44 int ddir;
ebac4655
JA
45};
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;
b1ff3403 68 unsigned int index;
ebac4655
JA
69
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 {
9104f874
JA
98 unsigned long long max_run[2], min_run[2];
99 unsigned long long max_bw[2], min_bw[2];
e9b2a3fa 100 unsigned long long io_kb[2];
9104f874 101 unsigned long long agg[2];
ebac4655
JA
102};
103
e9c047a0
JA
104enum 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 */
ebac4655 139struct thread_data {
e9c047a0
JA
140 char name[32];
141 char *file_name;
ef899b63 142 char *directory;
ebac4655
JA
143 char verror[80];
144 pthread_t thread;
145 int thread_number;
146 int groupid;
e9c047a0 147 enum fio_filetype filetype;
ebac4655
JA
148 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;
e9c047a0 156 enum fio_ddir ddir;
3d60d1ed 157 unsigned int iomix;
ebac4655 158 unsigned int ioprio;
e9c047a0
JA
159
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;
172
ebac4655
JA
173 unsigned int bs;
174 unsigned int min_bs;
175 unsigned int max_bs;
ebac4655
JA
176 unsigned int thinktime;
177 unsigned int fsync_blocks;
178 unsigned int start_delay;
179 unsigned int timeout;
e9c047a0 180 enum fio_iotype io_engine;
ebac4655 181 unsigned int overwrite;
ebac4655 182 unsigned int bw_avg_time;
ebac4655
JA
183 unsigned int loops;
184 unsigned long long file_size;
838a3cd3 185 unsigned long long real_file_size;
ebac4655 186 unsigned long long file_offset;
20dc95c4
JA
187 unsigned long long zone_size;
188 unsigned long long zone_skip;
e9c047a0 189 enum fio_memtype mem_type;
ebac4655
JA
190 unsigned int stonewall;
191 unsigned int numjobs;
ebac4655
JA
192 unsigned int iodepth;
193 os_cpu_mask_t cpumask;
aea47d44 194 unsigned int iolog;
843a7413
JA
195 unsigned int read_iolog;
196 unsigned int write_iolog;
a6ccc7be
JA
197 unsigned int rwmixcycle;
198 unsigned int rwmixread;
b6f4d880 199 unsigned int nice;
aea47d44 200
ef899b63 201 char *iolog_file;
843a7413
JA
202 void *iolog_buf;
203 FILE *iolog_f;
ebac4655 204
da86774e 205 char *sysfs_root;
da86774e
JA
206 char *ioscheduler;
207
6dfd46b9
JA
208 os_random_state_t bsrange_state;
209 os_random_state_t verify_state;
ebac4655
JA
210
211 int shm_id;
212
e9c047a0
JA
213 /*
214 * IO engine hooks, contains everything needed to submit an io_u
215 * to any of the available IO engines.
216 */
ebac4655
JA
217 void *io_data;
218 char io_engine_name[16];
219 int (*io_prep)(struct thread_data *, struct io_u *);
220 int (*io_queue)(struct thread_data *, struct io_u *);
221 int (*io_getevents)(struct thread_data *, int, int, struct timespec *);
222 struct io_u *(*io_event)(struct thread_data *, int);
223 int (*io_cancel)(struct thread_data *, struct io_u *);
224 void (*io_cleanup)(struct thread_data *);
225 int (*io_sync)(struct thread_data *);
226
e9c047a0
JA
227 /*
228 * Current IO depth and list of free and busy io_u's.
229 */
ebac4655
JA
230 unsigned int cur_depth;
231 struct list_head io_u_freelist;
232 struct list_head io_u_busylist;
233
e9c047a0
JA
234 /*
235 * Rate state
236 */
ebac4655
JA
237 unsigned int rate;
238 unsigned int ratemin;
239 unsigned int ratecycle;
240 unsigned long rate_usec_cycle;
241 long rate_pending_usleep;
242 unsigned long rate_bytes;
243 struct timeval lastrate;
244
245 unsigned long runtime[2]; /* msec */
246 unsigned long long io_size;
247 unsigned long long total_io_size;
248
9104f874
JA
249 unsigned long long io_blocks[2];
250 unsigned long long io_bytes[2];
251 unsigned long long zone_bytes;
252 unsigned long long this_io_bytes[2];
20dc95c4 253 unsigned long long last_pos;
bbfd6b00 254 volatile int mutex;
ebac4655 255
e9c047a0
JA
256 /*
257 * State for random io, a bitmap of blocks done vs not done
258 */
6dfd46b9 259 os_random_state_t random_state;
ebac4655
JA
260 unsigned long *file_map;
261 unsigned int num_maps;
262
263 /*
264 * bandwidth and latency stats
265 */
266 struct io_stat clat_stat[2]; /* completion latency */
267 struct io_stat slat_stat[2]; /* submission latency */
268 struct io_stat bw_stat[2]; /* bandwidth stats */
269
9104f874 270 unsigned long long stat_io_bytes[2];
ebac4655
JA
271 struct timeval stat_sample_time[2];
272
273 struct io_log *slat_log;
274 struct io_log *clat_log;
275 struct io_log *bw_log;
276
277 struct timeval start; /* start of this loop */
278 struct timeval epoch; /* time job was started */
279
e9c047a0
JA
280 /*
281 * fio system usage accounting
282 */
ebac4655
JA
283 struct rusage ru_start;
284 struct rusage ru_end;
285 unsigned long usr_time;
286 unsigned long sys_time;
287 unsigned long ctx;
288
e9c047a0
JA
289 /*
290 * read/write mixed workload state
291 */
6dfd46b9 292 os_random_state_t rwmix_state;
a6ccc7be 293 struct timeval rwmix_switch;
e9c047a0 294 enum fio_ddir rwmix_ddir;
a6ccc7be 295
4e0ba8af
JA
296 /*
297 * Pre-run and post-run shell
298 */
299 char *exec_prerun;
300 char *exec_postrun;
301
e9c047a0
JA
302 /*
303 * IO historic logs
304 */
ebac4655 305 struct list_head io_hist_list;
aea47d44 306 struct list_head io_log_list;
ebac4655
JA
307};
308
309#define td_verror(td, err) \
310 do { \
311 int e = (err); \
312 (td)->error = e; \
313 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, error=%s", __FILE__, __LINE__, strerror(e)); \
314 } while (0)
315
b1ff3403
JA
316extern struct io_u *__get_io_u(struct thread_data *);
317extern void put_io_u(struct thread_data *, struct io_u *);
ebac4655
JA
318
319extern int rate_quit;
320extern int write_lat_log;
321extern int write_bw_log;
322extern int exitall_on_terminate;
323extern int thread_number;
324extern int shm_id;
325extern int groupid;
eb8bbf48
JA
326extern FILE *f_out;
327extern FILE *f_err;
ebac4655
JA
328
329extern struct thread_data *threads;
330
ebac4655
JA
331#define td_read(td) ((td)->ddir == DDIR_READ)
332#define td_write(td) ((td)->ddir == DDIR_WRITE)
3d60d1ed 333#define td_rw(td) ((td)->iomix != 0)
ebac4655
JA
334
335#define BLOCKS_PER_MAP (8 * sizeof(long))
336#define TO_MAP_BLOCK(td, b) ((b) - ((td)->file_offset / (td)->min_bs))
337#define RAND_MAP_IDX(td, b) (TO_MAP_BLOCK(td, b) / BLOCKS_PER_MAP)
338#define RAND_MAP_BIT(td, b) (TO_MAP_BLOCK(td, b) & (BLOCKS_PER_MAP - 1))
339
340#define MAX_JOBS (1024)
341
342struct disk_util_stat {
343 unsigned ios[2];
344 unsigned merges[2];
345 unsigned long long sectors[2];
346 unsigned ticks[2];
347 unsigned io_ticks;
348 unsigned time_in_queue;
349};
350
351struct disk_util {
352 struct list_head list;
353
354 char *name;
355 char path[256];
356 dev_t dev;
357
358 struct disk_util_stat dus;
359 struct disk_util_stat last_dus;
360
361 unsigned long msec;
362 struct timeval time;
363};
364
365struct io_completion_data {
366 int nr; /* input */
367
368 int error; /* output */
369 unsigned long bytes_done[2]; /* output */
370};
371
372#define DISK_UTIL_MSEC (250)
373
6a0106a0
JA
374#ifndef min
375#define min(a, b) ((a) < (b) ? (a) : (b))
376#endif
377
6796209a
JA
378/*
379 * Log exports
380 */
381extern int read_iolog_get(struct thread_data *, struct io_u *);
382extern void write_iolog_put(struct thread_data *, struct io_u *);
383extern int init_iolog(struct thread_data *td);
384extern void log_io_piece(struct thread_data *, struct io_u *);
385extern void prune_io_piece_log(struct thread_data *);
386extern void write_iolog_close(struct thread_data *);
387
388/*
389 * Logging
390 */
391extern void add_clat_sample(struct thread_data *, int, unsigned long);
392extern void add_slat_sample(struct thread_data *, int, unsigned long);
393extern void add_bw_sample(struct thread_data *, int);
394extern void show_run_stats(void);
395extern void init_disk_util(struct thread_data *);
396extern void update_rusage_stat(struct thread_data *);
397extern void update_io_ticks(void);
398extern void disk_util_timer_arm(void);
8914a9d8
JA
399extern void setup_log(struct io_log **);
400extern void finish_log(struct thread_data *, struct io_log *, const char *);
401extern int setup_rate(struct thread_data *);
6796209a
JA
402
403/*
404 * Time functions
405 */
406extern unsigned long utime_since(struct timeval *, struct timeval *);
407extern unsigned long mtime_since(struct timeval *, struct timeval *);
408extern unsigned long mtime_since_now(struct timeval *);
409extern unsigned long time_since_now(struct timeval *);
410extern void usec_sleep(struct thread_data *, unsigned long);
411extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
412
8914a9d8
JA
413/*
414 * Init functions
415 */
416extern int parse_options(int, char **);
417extern int init_random_state(struct thread_data *);
418
bbfd6b00
JA
419/*
420 * This is a pretty crappy semaphore implementation, but with the use that fio
421 * has (just signalling start/go conditions), it doesn't have to be better.
422 * Naturally this would not work for any type of contended semaphore or
423 * for real locking.
424 */
425static inline void fio_sem_init(volatile int volatile *sem, int val)
426{
427 *sem = val;
428}
429
430static inline void fio_sem_down(volatile int volatile *sem)
431{
432 while (*sem == 0)
433 usleep(10000);
434
435 (*sem)--;
436}
437
438static inline void fio_sem_up(volatile int volatile *sem)
439{
440 (*sem)++;
441}
442
3b70d7e5
JA
443/*
444 * If logging output to a file, stderr should go to both stderr and f_err
445 */
446#define log_err(args...) do { \
447 fprintf(f_err, ##args); \
448 if (f_err != stderr) \
449 fprintf(stderr, ##args); \
450 } while (0)
451
ebac4655 452#endif