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