Disable timeout handling for now
[fio.git] / fio.h
... / ...
CommitLineData
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#include <unistd.h>
13#include <string.h>
14
15#include "list.h"
16#include "md5.h"
17#include "crc32.h"
18#include "arch.h"
19#include "os.h"
20
21#ifdef FIO_HAVE_SYSLET
22#include "syslet.h"
23#endif
24
25enum fio_ddir {
26 DDIR_READ = 0,
27 DDIR_WRITE,
28 DDIR_SYNC,
29};
30
31/*
32 * Use for maintaining statistics
33 */
34struct io_stat {
35 unsigned long max_val;
36 unsigned long min_val;
37 unsigned long samples;
38
39 double mean;
40 double S;
41};
42
43/*
44 * A single data sample
45 */
46struct io_sample {
47 unsigned long time;
48 unsigned long val;
49 enum fio_ddir ddir;
50};
51
52/*
53 * Dynamically growing data sample log
54 */
55struct io_log {
56 unsigned long nr_samples;
57 unsigned long max_samples;
58 struct io_sample *log;
59};
60
61/*
62 * When logging io actions, this matches a single sent io_u
63 */
64struct io_piece {
65 struct list_head list;
66 struct fio_file *file;
67 unsigned long long offset;
68 unsigned long len;
69 enum fio_ddir ddir;
70};
71
72#ifdef FIO_HAVE_SYSLET
73struct syslet_req {
74 struct syslet_uatom atom;
75 long ret;
76};
77#endif
78
79enum {
80 IO_U_F_FREE = 1 << 0,
81 IO_U_F_FLIGHT = 1 << 1,
82};
83
84/*
85 * The io unit
86 */
87struct io_u {
88 union {
89#ifdef FIO_HAVE_LIBAIO
90 struct iocb iocb;
91#endif
92#ifdef FIO_HAVE_POSIXAIO
93 struct aiocb aiocb;
94#endif
95#ifdef FIO_HAVE_SGIO
96 struct sg_io_hdr hdr;
97#endif
98#ifdef FIO_HAVE_SYSLET
99 struct syslet_req req;
100#endif
101 };
102 struct timeval start_time;
103 struct timeval issue_time;
104
105 /*
106 * Allocated/set buffer and length
107 */
108 void *buf;
109 unsigned long buflen;
110 unsigned long long offset;
111
112 /*
113 * IO engine state, may be different from above when we get
114 * partial transfers / residual data counts
115 */
116 void *xfer_buf;
117 unsigned long xfer_buflen;
118
119 unsigned int resid;
120 unsigned int error;
121
122 enum fio_ddir ddir;
123
124 /*
125 * io engine private data
126 */
127 union {
128 unsigned int index;
129 unsigned int seen;
130 };
131
132 unsigned int flags;
133
134 struct fio_file *file;
135
136 struct list_head list;
137};
138
139/*
140 * io_ops->queue() return values
141 */
142enum {
143 FIO_Q_COMPLETED = 0, /* completed sync */
144 FIO_Q_QUEUED = 1, /* queued, will complete async */
145 FIO_Q_BUSY = 2, /* no more room, call ->commit() */
146};
147
148#define FIO_HDR_MAGIC 0xf00baaef
149
150enum {
151 VERIFY_NONE = 0, /* no verification */
152 VERIFY_MD5, /* md5 sum data blocks */
153 VERIFY_CRC32, /* crc32 sum data blocks */
154};
155
156/*
157 * A header structure associated with each checksummed data block
158 */
159struct verify_header {
160 unsigned int fio_magic;
161 unsigned int len;
162 unsigned int verify_type;
163 union {
164 char md5_digest[MD5_HASH_WORDS * 4];
165 unsigned long crc32;
166 };
167};
168
169struct group_run_stats {
170 unsigned long long max_run[2], min_run[2];
171 unsigned long long max_bw[2], min_bw[2];
172 unsigned long long io_kb[2];
173 unsigned long long agg[2];
174};
175
176/*
177 * What type of allocation to use for io buffers
178 */
179enum fio_memtype {
180 MEM_MALLOC = 0, /* ordinary malloc */
181 MEM_SHM, /* use shared memory segments */
182 MEM_SHMHUGE, /* use shared memory segments with huge pages */
183 MEM_MMAP, /* use anonynomous mmap */
184 MEM_MMAPHUGE, /* memory mapped huge file */
185};
186
187/*
188 * The type of object we are working on
189 */
190enum fio_filetype {
191 FIO_TYPE_FILE = 1, /* plain file */
192 FIO_TYPE_BD, /* block device */
193 FIO_TYPE_CHAR, /* character device */
194};
195
196enum fio_ioengine_flags {
197 FIO_SYNCIO = 1 << 0, /* io engine has synchronous ->queue */
198 FIO_CPUIO = 1 << 1, /* cpu burner, doesn't do real io */
199 FIO_MMAPIO = 1 << 2, /* uses memory mapped io */
200 FIO_RAWIO = 1 << 3, /* some sort of direct/raw io */
201 FIO_NETIO = 1 << 4, /* networked io */
202 FIO_NULLIO = 1 << 5, /* no real data transfer (cpu/null) */
203};
204
205/*
206 * Each thread_data structure has a number of files associated with it,
207 * this structure holds state information for a single file.
208 */
209struct fio_file {
210 /*
211 * A file may not be a file descriptor, let the io engine decide
212 */
213 union {
214 unsigned long file_data;
215 int fd;
216 };
217 char *file_name;
218 void *mmap;
219 unsigned long long file_size;
220 unsigned long long real_file_size;
221 unsigned long long file_offset;
222 unsigned long long last_pos;
223 unsigned long long last_completed_pos;
224
225 /*
226 * block map for random io
227 */
228 unsigned long *file_map;
229 unsigned int num_maps;
230 unsigned int last_free_lookup;
231
232 unsigned int unlink;
233};
234
235struct thread_stat {
236 struct io_log *slat_log;
237 struct io_log *clat_log;
238 struct io_log *bw_log;
239
240 /*
241 * bandwidth and latency stats
242 */
243 struct io_stat clat_stat[2]; /* completion latency */
244 struct io_stat slat_stat[2]; /* submission latency */
245 struct io_stat bw_stat[2]; /* bandwidth stats */
246
247 unsigned long long stat_io_bytes[2];
248 struct timeval stat_sample_time[2];
249
250 /*
251 * fio system usage accounting
252 */
253 struct rusage ru_start;
254 struct rusage ru_end;
255 unsigned long usr_time;
256 unsigned long sys_time;
257 unsigned long ctx;
258};
259
260/*
261 * How many depth levels to log
262 */
263#define FIO_IO_U_MAP_NR 8
264#define FIO_IO_U_LAT_NR 12
265
266/*
267 * This describes a single thread/process executing a fio job.
268 */
269struct thread_data {
270 char *description;
271 char *name;
272 char *directory;
273 char *filename;
274 char verror[80];
275 pthread_t thread;
276 int thread_number;
277 int groupid;
278 struct thread_stat ts;
279 enum fio_filetype filetype;
280 struct fio_file *files;
281 unsigned int nr_files;
282 unsigned int nr_uniq_files;
283 unsigned int next_file;
284 int error;
285 pid_t pid;
286 char *orig_buffer;
287 size_t orig_buffer_size;
288 volatile int terminate;
289 volatile int runstate;
290 enum fio_ddir ddir;
291 unsigned int iomix;
292 unsigned int ioprio;
293 unsigned int last_was_sync;
294
295 unsigned int sequential;
296 unsigned int odirect;
297 unsigned int invalidate_cache;
298 unsigned int create_serialize;
299 unsigned int create_fsync;
300 unsigned int end_fsync;
301 unsigned int sync_io;
302 unsigned int verify;
303 unsigned int use_thread;
304 unsigned int unlink;
305 unsigned int do_disk_util;
306 unsigned int override_sync;
307 unsigned int rand_repeatable;
308 unsigned int write_lat_log;
309 unsigned int write_bw_log;
310 unsigned int norandommap;
311 unsigned int bs_unaligned;
312
313 unsigned int bs[2];
314 unsigned int min_bs[2];
315 unsigned int max_bs[2];
316 unsigned int hugepage_size;
317 unsigned int rw_min_bs;
318 unsigned int thinktime;
319 unsigned int thinktime_spin;
320 unsigned int thinktime_blocks;
321 unsigned int fsync_blocks;
322 unsigned int start_delay;
323 unsigned long timeout;
324 unsigned int overwrite;
325 unsigned int bw_avg_time;
326 unsigned int loops;
327 unsigned long long zone_size;
328 unsigned long long zone_skip;
329 enum fio_memtype mem_type;
330 char *mmapfile;
331 int mmapfd;
332 unsigned int stonewall;
333 unsigned int numjobs;
334 unsigned int iodepth;
335 unsigned int iodepth_low;
336 os_cpu_mask_t cpumask;
337 unsigned int iolog;
338 unsigned int read_iolog;
339 unsigned int rwmixcycle;
340 unsigned int rwmixread;
341 unsigned int rwmixwrite;
342 unsigned int nice;
343
344 char *read_iolog_file;
345 char *write_iolog_file;
346 void *iolog_buf;
347 FILE *iolog_f;
348
349 char *sysfs_root;
350 char *ioscheduler;
351
352 os_random_state_t bsrange_state;
353 os_random_state_t verify_state;
354
355 int shm_id;
356
357 /*
358 * IO engine hooks, contains everything needed to submit an io_u
359 * to any of the available IO engines.
360 */
361 struct ioengine_ops *io_ops;
362
363 /*
364 * Current IO depth and list of free and busy io_u's.
365 */
366 unsigned int cur_depth;
367 unsigned int io_u_map[FIO_IO_U_MAP_NR];
368 unsigned int io_u_lat[FIO_IO_U_LAT_NR];
369 unsigned long total_io_u;
370 struct list_head io_u_freelist;
371 struct list_head io_u_busylist;
372 struct list_head io_u_requeues;
373
374 /*
375 * Rate state
376 */
377 unsigned int rate;
378 unsigned int ratemin;
379 unsigned int ratecycle;
380 unsigned long rate_usec_cycle;
381 long rate_pending_usleep;
382 unsigned long rate_bytes;
383 struct timeval lastrate;
384
385 unsigned long runtime[2]; /* msec */
386 unsigned long long io_size;
387 unsigned long long total_file_size;
388 unsigned long long start_offset;
389 unsigned long long total_io_size;
390
391 unsigned long io_issues[2];
392 unsigned long long io_blocks[2];
393 unsigned long long io_bytes[2];
394 unsigned long long this_io_bytes[2];
395 unsigned long long zone_bytes;
396 volatile int mutex;
397
398 /*
399 * State for random io, a bitmap of blocks done vs not done
400 */
401 os_random_state_t random_state;
402
403 /*
404 * CPU "io" cycle burner
405 */
406 unsigned int cpuload;
407 unsigned int cpucycle;
408
409 struct timeval start; /* start of this loop */
410 struct timeval epoch; /* time job was started */
411 struct timeval end_time;/* time job ended */
412
413 /*
414 * read/write mixed workload state
415 */
416 os_random_state_t rwmix_state;
417 struct timeval rwmix_switch;
418 enum fio_ddir rwmix_ddir;
419
420 /*
421 * Pre-run and post-run shell
422 */
423 char *exec_prerun;
424 char *exec_postrun;
425
426 /*
427 * IO historic logs
428 */
429 struct list_head io_hist_list;
430 struct list_head io_log_list;
431
432 /*
433 * timeout handling
434 */
435 struct timeval timeout_end;
436 struct itimerval timer;
437};
438
439/*
440 * 30 second per-io_u timeout, with 5 second intervals to avoid resetting
441 * the timer on each queue operation.
442 */
443#define IO_U_TIMEOUT_INC 5
444#define IO_U_TIMEOUT 30
445
446#define __td_verror(td, err, msg) \
447 do { \
448 if ((td)->error) \
449 break; \
450 int e = (err); \
451 (td)->error = e; \
452 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, error=%s", __FILE__, __LINE__, (msg)); \
453 } while (0)
454
455
456#define td_verror(td, err) __td_verror((td), (err), strerror((err)))
457#define td_vmsg(td, err, msg) __td_verror((td), (err), (msg))
458
459extern int exitall_on_terminate;
460extern int thread_number;
461extern int shm_id;
462extern int groupid;
463extern int terse_output;
464extern FILE *f_out;
465extern FILE *f_err;
466extern int temp_stall_ts;
467extern unsigned long long mlock_size;
468
469extern struct thread_data *threads;
470
471#define td_read(td) ((td)->ddir == DDIR_READ)
472#define td_write(td) ((td)->ddir == DDIR_WRITE)
473#define td_rw(td) ((td)->iomix != 0)
474
475#define BLOCKS_PER_MAP (8 * sizeof(long))
476#define TO_MAP_BLOCK(td, f, b) ((b) - ((f)->file_offset / (td)->rw_min_bs))
477#define RAND_MAP_IDX(td, f, b) (TO_MAP_BLOCK(td, f, b) / BLOCKS_PER_MAP)
478#define RAND_MAP_BIT(td, f, b) (TO_MAP_BLOCK(td, f, b) & (BLOCKS_PER_MAP - 1))
479
480#define MAX_JOBS (1024)
481
482static inline int should_fsync(struct thread_data *td)
483{
484 if (td->last_was_sync)
485 return 0;
486 if (td->odirect)
487 return 0;
488 if (td_write(td) || td_rw(td) || td->override_sync)
489 return 1;
490
491 return 0;
492}
493
494/*
495 * Disk utils as read in /sys/block/<dev>/stat
496 */
497struct disk_util_stat {
498 unsigned ios[2];
499 unsigned merges[2];
500 unsigned long long sectors[2];
501 unsigned ticks[2];
502 unsigned io_ticks;
503 unsigned time_in_queue;
504};
505
506/*
507 * Per-device disk util management
508 */
509struct disk_util {
510 struct list_head list;
511
512 char *name;
513 char path[256];
514 dev_t dev;
515
516 struct disk_util_stat dus;
517 struct disk_util_stat last_dus;
518
519 unsigned long msec;
520 struct timeval time;
521};
522
523/*
524 * Callback for io completion
525 */
526typedef int (endio_handler)(struct io_u *);
527
528#define DISK_UTIL_MSEC (250)
529
530#ifndef min
531#define min(a, b) ((a) < (b) ? (a) : (b))
532#endif
533#ifndef max
534#define max(a, b) ((a) > (b) ? (a) : (b))
535#endif
536
537/*
538 * Log exports
539 */
540extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
541extern void write_iolog_put(struct thread_data *, struct io_u *);
542extern int __must_check init_iolog(struct thread_data *td);
543extern void log_io_piece(struct thread_data *, struct io_u *);
544extern void prune_io_piece_log(struct thread_data *);
545extern void write_iolog_close(struct thread_data *);
546
547/*
548 * Logging
549 */
550extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long);
551extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long);
552extern void add_bw_sample(struct thread_data *, enum fio_ddir, struct timeval *);
553extern void show_run_stats(void);
554extern void init_disk_util(struct thread_data *);
555extern void update_rusage_stat(struct thread_data *);
556extern void update_io_ticks(void);
557extern void disk_util_timer_arm(void);
558extern void setup_log(struct io_log **);
559extern void finish_log(struct thread_data *, struct io_log *, const char *);
560extern void __finish_log(struct io_log *, const char *);
561extern int setup_rate(struct thread_data *);
562extern struct io_log *agg_io_log[2];
563extern int write_bw_log;
564extern void add_agg_sample(unsigned long, enum fio_ddir);
565
566/*
567 * Time functions
568 */
569extern unsigned long utime_since(struct timeval *, struct timeval *);
570extern unsigned long utime_since_now(struct timeval *);
571extern unsigned long mtime_since(struct timeval *, struct timeval *);
572extern unsigned long mtime_since_now(struct timeval *);
573extern unsigned long time_since_now(struct timeval *);
574extern unsigned long mtime_since_genesis(void);
575extern void __usec_sleep(unsigned int);
576extern void usec_sleep(struct thread_data *, unsigned long);
577extern void rate_throttle(struct thread_data *, unsigned long, unsigned int, int);
578extern void fill_start_time(struct timeval *);
579extern void fio_gettime(struct timeval *, void *);
580
581/*
582 * Init functions
583 */
584extern int __must_check parse_options(int, char **);
585extern int __must_check init_random_state(struct thread_data *);
586
587/*
588 * File setup/shutdown
589 */
590extern void close_files(struct thread_data *);
591extern int __must_check setup_files(struct thread_data *);
592extern int __must_check open_files(struct thread_data *);
593extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
594
595/*
596 * ETA/status stuff
597 */
598extern void print_thread_status(void);
599extern void print_status_init(int);
600
601/*
602 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
603 * will never back again. It may cycle between running/verififying/fsyncing.
604 * Once the thread reaches TD_EXITED, it is just waiting for the core to
605 * reap it.
606 */
607enum {
608 TD_NOT_CREATED = 0,
609 TD_CREATED,
610 TD_INITIALIZED,
611 TD_RUNNING,
612 TD_VERIFYING,
613 TD_FSYNCING,
614 TD_EXITED,
615 TD_REAPED,
616};
617
618/*
619 * Verify helpers
620 */
621extern void populate_verify_io_u(struct thread_data *, struct io_u *);
622extern int __must_check get_next_verify(struct thread_data *td, struct io_u *);
623extern int __must_check verify_io_u(struct io_u *);
624
625/*
626 * Memory helpers
627 */
628extern int __must_check fio_pin_memory(void);
629extern void fio_unpin_memory(void);
630extern int __must_check allocate_io_mem(struct thread_data *);
631extern void free_io_mem(struct thread_data *);
632
633/*
634 * io unit handling
635 */
636#define queue_full(td) list_empty(&(td)->io_u_freelist)
637extern struct io_u *__get_io_u(struct thread_data *);
638extern struct io_u *get_io_u(struct thread_data *);
639extern void put_io_u(struct thread_data *, struct io_u *);
640extern void requeue_io_u(struct thread_data *, struct io_u **);
641extern long __must_check io_u_sync_complete(struct thread_data *, struct io_u *, endio_handler *);
642extern long __must_check io_u_queued_complete(struct thread_data *, int, endio_handler *);
643extern void io_u_queued(struct thread_data *, struct io_u *);
644extern void io_u_init_timeout(void);
645extern void io_u_set_timeout(struct thread_data *);
646
647/*
648 * io engine entry points
649 */
650extern int __must_check td_io_init(struct thread_data *);
651extern int __must_check td_io_prep(struct thread_data *, struct io_u *);
652extern int __must_check td_io_queue(struct thread_data *, struct io_u *);
653extern int __must_check td_io_sync(struct thread_data *, struct fio_file *);
654extern int __must_check td_io_getevents(struct thread_data *, int, int, struct timespec *);
655extern int __must_check td_io_commit(struct thread_data *);
656
657/*
658 * This is a pretty crappy semaphore implementation, but with the use that fio
659 * has (just signalling start/go conditions), it doesn't have to be better.
660 * Naturally this would not work for any type of contended semaphore or
661 * for real locking.
662 */
663static inline void fio_sem_init(volatile int *sem, int val)
664{
665 *sem = val;
666}
667
668static inline void fio_sem_down(volatile int *sem)
669{
670 while (*sem == 0)
671 usleep(10000);
672
673 (*sem)--;
674}
675
676static inline void fio_sem_up(volatile int *sem)
677{
678 (*sem)++;
679}
680
681/*
682 * If logging output to a file, stderr should go to both stderr and f_err
683 */
684#define log_err(args...) do { \
685 fprintf(f_err, ##args); \
686 if (f_err != stderr) \
687 fprintf(stderr, ##args); \
688 } while (0)
689
690struct ioengine_ops {
691 struct list_head list;
692 char name[16];
693 int version;
694 int flags;
695 int (*setup)(struct thread_data *);
696 int (*init)(struct thread_data *);
697 int (*prep)(struct thread_data *, struct io_u *);
698 int (*queue)(struct thread_data *, struct io_u *);
699 int (*commit)(struct thread_data *);
700 int (*getevents)(struct thread_data *, int, int, struct timespec *);
701 struct io_u *(*event)(struct thread_data *, int);
702 int (*cancel)(struct thread_data *, struct io_u *);
703 void (*cleanup)(struct thread_data *);
704 void *data;
705 void *dlhandle;
706 unsigned long priv;
707};
708
709#define FIO_IOOPS_VERSION 5
710
711extern struct ioengine_ops *load_ioengine(struct thread_data *, const char *);
712extern void register_ioengine(struct ioengine_ops *);
713extern void unregister_ioengine(struct ioengine_ops *);
714extern void close_ioengine(struct thread_data *);
715
716/*
717 * Mark unused variables passed to ops functions as unused, to silence gcc
718 */
719#define fio_unused __attribute((__unused__))
720#define fio_init __attribute__((constructor))
721#define fio_exit __attribute__((destructor))
722
723#define for_each_td(td, i) \
724 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
725#define for_each_file(td, f, i) \
726 for ((i) = 0, (f) = &(td)->files[0]; (i) < (int) (td)->nr_files; (i)++, (f)++)
727
728#define fio_assert(td, cond) do { \
729 if (!(cond)) { \
730 int *__foo = NULL; \
731 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
732 (td)->runstate = TD_EXITED; \
733 (td)->error = EFAULT; \
734 *__foo = 0; \
735 } \
736} while (0)
737
738#endif