Commit | Line | Data |
---|---|---|
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> | |
ebac4655 JA |
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; | |
aea47d44 | 43 | int ddir; |
ebac4655 JA |
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; | |
b1ff3403 | 67 | unsigned int index; |
ebac4655 JA |
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 { | |
9104f874 JA |
97 | unsigned long long max_run[2], min_run[2]; |
98 | unsigned long long max_bw[2], min_bw[2]; | |
e9b2a3fa | 99 | unsigned long long io_kb[2]; |
9104f874 | 100 | unsigned long long agg[2]; |
ebac4655 JA |
101 | }; |
102 | ||
e9c047a0 JA |
103 | enum fio_ddir { |
104 | DDIR_READ = 0, | |
105 | DDIR_WRITE, | |
106 | }; | |
107 | ||
108 | /* | |
109 | * What type of allocation to use for io buffers | |
110 | */ | |
111 | enum fio_memtype { | |
112 | MEM_MALLOC = 0, /* ordinary malloc */ | |
113 | MEM_SHM, /* use shared memory segments */ | |
114 | MEM_MMAP, /* use anonynomous mmap */ | |
115 | }; | |
116 | ||
117 | /* | |
118 | * The type of object we are working on | |
119 | */ | |
120 | enum fio_filetype { | |
121 | FIO_TYPE_FILE = 1, | |
122 | FIO_TYPE_BD, | |
123 | FIO_TYPE_CHAR, | |
124 | }; | |
125 | ||
126 | enum fio_iotype { | |
127 | FIO_SYNCIO = 1 << 0, | |
128 | FIO_MMAPIO = 1 << 1 | FIO_SYNCIO, | |
129 | FIO_LIBAIO = 1 << 2, | |
130 | FIO_POSIXAIO = 1 << 3, | |
131 | FIO_SGIO = 1 << 4, | |
132 | FIO_SPLICEIO = 1 << 5 | FIO_SYNCIO, | |
133 | }; | |
134 | ||
135 | /* | |
136 | * This describes a single thread/process executing a fio job. | |
137 | */ | |
ebac4655 | 138 | struct thread_data { |
e9c047a0 JA |
139 | char name[32]; |
140 | char *file_name; | |
ef899b63 | 141 | char *directory; |
ebac4655 JA |
142 | char verror[80]; |
143 | pthread_t thread; | |
144 | int thread_number; | |
145 | int groupid; | |
e9c047a0 | 146 | enum fio_filetype filetype; |
ebac4655 JA |
147 | int error; |
148 | int fd; | |
149 | void *mmap; | |
150 | pid_t pid; | |
151 | char *orig_buffer; | |
152 | size_t orig_buffer_size; | |
153 | volatile int terminate; | |
154 | volatile int runstate; | |
155 | volatile int old_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; | |
eba09dd3 | 194 | unsigned int jobnum; |
aea47d44 | 195 | unsigned int iolog; |
843a7413 JA |
196 | unsigned int read_iolog; |
197 | unsigned int write_iolog; | |
a6ccc7be JA |
198 | unsigned int rwmixcycle; |
199 | unsigned int rwmixread; | |
b6f4d880 | 200 | unsigned int nice; |
aea47d44 | 201 | |
ef899b63 | 202 | char *iolog_file; |
843a7413 JA |
203 | void *iolog_buf; |
204 | FILE *iolog_f; | |
ebac4655 | 205 | |
da86774e | 206 | char *sysfs_root; |
da86774e JA |
207 | char *ioscheduler; |
208 | ||
6dfd46b9 JA |
209 | os_random_state_t bsrange_state; |
210 | os_random_state_t verify_state; | |
ebac4655 JA |
211 | |
212 | int shm_id; | |
213 | ||
e9c047a0 JA |
214 | /* |
215 | * IO engine hooks, contains everything needed to submit an io_u | |
216 | * to any of the available IO engines. | |
217 | */ | |
ebac4655 JA |
218 | 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 | ||
e9c047a0 JA |
228 | /* |
229 | * Current IO depth and list of free and busy io_u's. | |
230 | */ | |
ebac4655 JA |
231 | unsigned int cur_depth; |
232 | struct list_head io_u_freelist; | |
233 | struct list_head io_u_busylist; | |
234 | ||
e9c047a0 JA |
235 | /* |
236 | * Rate state | |
237 | */ | |
ebac4655 JA |
238 | 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 | ||
9104f874 JA |
250 | 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]; | |
20dc95c4 | 254 | unsigned long long last_pos; |
bbfd6b00 | 255 | volatile int mutex; |
ebac4655 | 256 | |
e9c047a0 JA |
257 | /* |
258 | * State for random io, a bitmap of blocks done vs not done | |
259 | */ | |
6dfd46b9 | 260 | os_random_state_t random_state; |
ebac4655 JA |
261 | 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 | ||
9104f874 | 271 | unsigned long long stat_io_bytes[2]; |
ebac4655 JA |
272 | 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 | ||
e9c047a0 JA |
281 | /* |
282 | * fio system usage accounting | |
283 | */ | |
ebac4655 JA |
284 | struct rusage ru_start; |
285 | struct rusage ru_end; | |
286 | unsigned long usr_time; | |
287 | unsigned long sys_time; | |
288 | unsigned long ctx; | |
289 | ||
e9c047a0 JA |
290 | /* |
291 | * read/write mixed workload state | |
292 | */ | |
6dfd46b9 | 293 | os_random_state_t rwmix_state; |
a6ccc7be | 294 | struct timeval rwmix_switch; |
e9c047a0 | 295 | enum fio_ddir rwmix_ddir; |
a6ccc7be | 296 | |
4e0ba8af JA |
297 | /* |
298 | * Pre-run and post-run shell | |
299 | */ | |
300 | char *exec_prerun; | |
301 | char *exec_postrun; | |
302 | ||
e9c047a0 JA |
303 | /* |
304 | * IO historic logs | |
305 | */ | |
ebac4655 | 306 | struct list_head io_hist_list; |
aea47d44 | 307 | struct list_head io_log_list; |
ebac4655 JA |
308 | }; |
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 | ||
b1ff3403 JA |
317 | extern struct io_u *__get_io_u(struct thread_data *); |
318 | extern void put_io_u(struct thread_data *, struct io_u *); | |
ebac4655 JA |
319 | |
320 | extern int rate_quit; | |
321 | extern int write_lat_log; | |
322 | extern int write_bw_log; | |
323 | extern int exitall_on_terminate; | |
324 | extern int thread_number; | |
325 | extern int shm_id; | |
326 | extern int groupid; | |
327 | ||
328 | extern struct thread_data *threads; | |
329 | ||
ebac4655 JA |
330 | #define td_read(td) ((td)->ddir == DDIR_READ) |
331 | #define td_write(td) ((td)->ddir == DDIR_WRITE) | |
3d60d1ed | 332 | #define td_rw(td) ((td)->iomix != 0) |
ebac4655 JA |
333 | |
334 | #define BLOCKS_PER_MAP (8 * sizeof(long)) | |
335 | #define TO_MAP_BLOCK(td, b) ((b) - ((td)->file_offset / (td)->min_bs)) | |
336 | #define RAND_MAP_IDX(td, b) (TO_MAP_BLOCK(td, b) / BLOCKS_PER_MAP) | |
337 | #define RAND_MAP_BIT(td, b) (TO_MAP_BLOCK(td, b) & (BLOCKS_PER_MAP - 1)) | |
338 | ||
339 | #define MAX_JOBS (1024) | |
340 | ||
341 | struct disk_util_stat { | |
342 | unsigned ios[2]; | |
343 | unsigned merges[2]; | |
344 | unsigned long long sectors[2]; | |
345 | unsigned ticks[2]; | |
346 | unsigned io_ticks; | |
347 | unsigned time_in_queue; | |
348 | }; | |
349 | ||
350 | struct disk_util { | |
351 | struct list_head list; | |
352 | ||
353 | char *name; | |
354 | char path[256]; | |
355 | dev_t dev; | |
356 | ||
357 | struct disk_util_stat dus; | |
358 | struct disk_util_stat last_dus; | |
359 | ||
360 | unsigned long msec; | |
361 | struct timeval time; | |
362 | }; | |
363 | ||
364 | struct io_completion_data { | |
365 | int nr; /* input */ | |
366 | ||
367 | int error; /* output */ | |
368 | unsigned long bytes_done[2]; /* output */ | |
369 | }; | |
370 | ||
371 | #define DISK_UTIL_MSEC (250) | |
372 | ||
6a0106a0 JA |
373 | #ifndef min |
374 | #define min(a, b) ((a) < (b) ? (a) : (b)) | |
375 | #endif | |
376 | ||
6796209a JA |
377 | /* |
378 | * Log exports | |
379 | */ | |
380 | extern int read_iolog_get(struct thread_data *, struct io_u *); | |
381 | extern void write_iolog_put(struct thread_data *, struct io_u *); | |
382 | extern int init_iolog(struct thread_data *td); | |
383 | extern void log_io_piece(struct thread_data *, struct io_u *); | |
384 | extern void prune_io_piece_log(struct thread_data *); | |
385 | extern void write_iolog_close(struct thread_data *); | |
386 | ||
387 | /* | |
388 | * Logging | |
389 | */ | |
390 | extern void add_clat_sample(struct thread_data *, int, unsigned long); | |
391 | extern void add_slat_sample(struct thread_data *, int, unsigned long); | |
392 | extern void add_bw_sample(struct thread_data *, int); | |
393 | extern void show_run_stats(void); | |
394 | extern void init_disk_util(struct thread_data *); | |
395 | extern void update_rusage_stat(struct thread_data *); | |
396 | extern void update_io_ticks(void); | |
397 | extern void disk_util_timer_arm(void); | |
8914a9d8 JA |
398 | extern void setup_log(struct io_log **); |
399 | extern void finish_log(struct thread_data *, struct io_log *, const char *); | |
400 | extern int setup_rate(struct thread_data *); | |
6796209a JA |
401 | |
402 | /* | |
403 | * Time functions | |
404 | */ | |
405 | extern unsigned long utime_since(struct timeval *, struct timeval *); | |
406 | extern unsigned long mtime_since(struct timeval *, struct timeval *); | |
407 | extern unsigned long mtime_since_now(struct timeval *); | |
408 | extern unsigned long time_since_now(struct timeval *); | |
409 | extern void usec_sleep(struct thread_data *, unsigned long); | |
410 | extern void rate_throttle(struct thread_data *, unsigned long, unsigned int); | |
411 | ||
8914a9d8 JA |
412 | /* |
413 | * Init functions | |
414 | */ | |
415 | extern int parse_options(int, char **); | |
416 | extern int init_random_state(struct thread_data *); | |
417 | ||
bbfd6b00 JA |
418 | /* |
419 | * This is a pretty crappy semaphore implementation, but with the use that fio | |
420 | * has (just signalling start/go conditions), it doesn't have to be better. | |
421 | * Naturally this would not work for any type of contended semaphore or | |
422 | * for real locking. | |
423 | */ | |
424 | static inline void fio_sem_init(volatile int volatile *sem, int val) | |
425 | { | |
426 | *sem = val; | |
427 | } | |
428 | ||
429 | static inline void fio_sem_down(volatile int volatile *sem) | |
430 | { | |
431 | while (*sem == 0) | |
432 | usleep(10000); | |
433 | ||
434 | (*sem)--; | |
435 | } | |
436 | ||
437 | static inline void fio_sem_up(volatile int volatile *sem) | |
438 | { | |
439 | (*sem)++; | |
440 | } | |
441 | ||
ebac4655 | 442 | #endif |