8 #include <sys/resource.h>
30 * Use for maintaining statistics
33 unsigned long max_val;
34 unsigned long min_val;
35 unsigned long samples;
42 * A single data sample
51 * Dynamically growing data sample log
54 unsigned long nr_samples;
55 unsigned long max_samples;
56 struct io_sample *log;
60 * When logging io actions, this matches a single sent io_u
63 struct list_head list;
64 struct fio_file *file;
65 unsigned long long offset;
70 #ifdef FIO_HAVE_SYSLET
72 struct syslet_uatom atom;
82 #ifdef FIO_HAVE_LIBAIO
85 #ifdef FIO_HAVE_POSIXAIO
91 #ifdef FIO_HAVE_SYSLET
92 struct syslet_req req;
95 struct timeval start_time;
96 struct timeval issue_time;
99 * Allocated/set buffer and length
102 unsigned long buflen;
103 unsigned long long offset;
106 * IO engine state, may be different from above when we get
107 * partial transfers / residual data counts
110 unsigned long xfer_buflen;
118 * io engine private data
125 struct fio_file *file;
127 struct list_head list;
130 #define FIO_HDR_MAGIC 0xf00baaef
133 VERIFY_NONE = 0, /* no verification */
134 VERIFY_MD5, /* md5 sum data blocks */
135 VERIFY_CRC32, /* crc32 sum data blocks */
139 * A header structure associated with each checksummed data block
141 struct verify_header {
142 unsigned int fio_magic;
144 unsigned int verify_type;
146 char md5_digest[MD5_HASH_WORDS * 4];
151 struct group_run_stats {
152 unsigned long long max_run[2], min_run[2];
153 unsigned long long max_bw[2], min_bw[2];
154 unsigned long long io_kb[2];
155 unsigned long long agg[2];
159 * What type of allocation to use for io buffers
162 MEM_MALLOC = 0, /* ordinary malloc */
163 MEM_SHM, /* use shared memory segments */
164 MEM_SHMHUGE, /* use shared memory segments with huge pages */
165 MEM_MMAP, /* use anonynomous mmap */
166 MEM_MMAPHUGE, /* memory mapped huge file */
170 * The type of object we are working on
173 FIO_TYPE_FILE = 1, /* plain file */
174 FIO_TYPE_BD, /* block device */
175 FIO_TYPE_CHAR, /* character device */
178 enum fio_ioengine_flags {
179 FIO_SYNCIO = 1 << 0, /* io engine has synchronous ->queue */
180 FIO_CPUIO = 1 << 1, /* cpu burner, doesn't do real io */
181 FIO_MMAPIO = 1 << 2, /* uses memory mapped io */
182 FIO_RAWIO = 1 << 3, /* some sort of direct/raw io */
183 FIO_NETIO = 1 << 4, /* networked io */
184 FIO_NULLIO = 1 << 5, /* no real data transfer (cpu/null) */
188 * Each thread_data structure has a number of files associated with it,
189 * this structure holds state information for a single file.
193 * A file may not be a file descriptor, let the io engine decide
196 unsigned long file_data;
201 unsigned long long file_size;
202 unsigned long long real_file_size;
203 unsigned long long file_offset;
204 unsigned long long last_pos;
205 unsigned long long last_completed_pos;
208 * block map for random io
210 unsigned long *file_map;
211 unsigned int num_maps;
212 unsigned int last_free_lookup;
218 * How many depth levels to log
220 #define FIO_IO_U_MAP_NR 8
223 * This describes a single thread/process executing a fio job.
234 enum fio_filetype filetype;
235 struct fio_file *files;
236 unsigned int nr_files;
237 unsigned int nr_uniq_files;
238 unsigned int next_file;
242 size_t orig_buffer_size;
243 volatile int terminate;
244 volatile int runstate;
248 unsigned int last_was_sync;
250 unsigned int sequential;
251 unsigned int odirect;
252 unsigned int invalidate_cache;
253 unsigned int create_serialize;
254 unsigned int create_fsync;
255 unsigned int end_fsync;
256 unsigned int sync_io;
258 unsigned int use_thread;
260 unsigned int do_disk_util;
261 unsigned int override_sync;
262 unsigned int rand_repeatable;
263 unsigned int write_lat_log;
264 unsigned int write_bw_log;
265 unsigned int norandommap;
266 unsigned int bs_unaligned;
269 unsigned int min_bs[2];
270 unsigned int max_bs[2];
271 unsigned int hugepage_size;
272 unsigned int rw_min_bs;
273 unsigned int thinktime;
274 unsigned int thinktime_blocks;
275 unsigned int fsync_blocks;
276 unsigned int start_delay;
277 unsigned long timeout;
278 unsigned int overwrite;
279 unsigned int bw_avg_time;
281 unsigned long long zone_size;
282 unsigned long long zone_skip;
283 enum fio_memtype mem_type;
286 unsigned int stonewall;
287 unsigned int numjobs;
288 unsigned int iodepth;
289 os_cpu_mask_t cpumask;
291 unsigned int read_iolog;
292 unsigned int rwmixcycle;
293 unsigned int rwmixread;
294 unsigned int rwmixwrite;
297 char *read_iolog_file;
298 char *write_iolog_file;
305 os_random_state_t bsrange_state;
306 os_random_state_t verify_state;
311 * IO engine hooks, contains everything needed to submit an io_u
312 * to any of the available IO engines.
314 struct ioengine_ops *io_ops;
317 * Current IO depth and list of free and busy io_u's.
319 unsigned int cur_depth;
320 unsigned int io_u_map[FIO_IO_U_MAP_NR];
321 unsigned long total_io_u;
322 struct list_head io_u_freelist;
323 struct list_head io_u_busylist;
329 unsigned int ratemin;
330 unsigned int ratecycle;
331 unsigned long rate_usec_cycle;
332 long rate_pending_usleep;
333 unsigned long rate_bytes;
334 struct timeval lastrate;
336 unsigned long runtime[2]; /* msec */
337 unsigned long long io_size;
338 unsigned long long total_file_size;
339 unsigned long long start_offset;
340 unsigned long long total_io_size;
342 unsigned long long io_blocks[2];
343 unsigned long long io_bytes[2];
344 unsigned long long zone_bytes;
345 unsigned long long this_io_bytes[2];
349 * State for random io, a bitmap of blocks done vs not done
351 os_random_state_t random_state;
354 * CPU "io" cycle burner
356 unsigned int cpuload;
357 unsigned int cpucycle;
360 * bandwidth and latency stats
362 struct io_stat clat_stat[2]; /* completion latency */
363 struct io_stat slat_stat[2]; /* submission latency */
364 struct io_stat bw_stat[2]; /* bandwidth stats */
366 unsigned long long stat_io_bytes[2];
367 struct timeval stat_sample_time[2];
369 struct io_log *slat_log;
370 struct io_log *clat_log;
371 struct io_log *bw_log;
373 struct timeval start; /* start of this loop */
374 struct timeval epoch; /* time job was started */
375 struct timeval end_time;/* time job ended */
378 * fio system usage accounting
380 struct rusage ru_start;
381 struct rusage ru_end;
382 unsigned long usr_time;
383 unsigned long sys_time;
387 * read/write mixed workload state
389 os_random_state_t rwmix_state;
390 struct timeval rwmix_switch;
391 enum fio_ddir rwmix_ddir;
394 * Pre-run and post-run shell
402 struct list_head io_hist_list;
403 struct list_head io_log_list;
406 #define __td_verror(td, err, msg) \
410 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, error=%s", __FILE__, __LINE__, (msg)); \
414 #define td_verror(td, err) __td_verror((td), (err), strerror((err)))
415 #define td_vmsg(td, err, msg) __td_verror((td), (err), (msg))
417 extern int exitall_on_terminate;
418 extern int thread_number;
421 extern int terse_output;
424 extern int temp_stall_ts;
425 extern unsigned long long mlock_size;
427 extern struct thread_data *threads;
429 #define td_read(td) ((td)->ddir == DDIR_READ)
430 #define td_write(td) ((td)->ddir == DDIR_WRITE)
431 #define td_rw(td) ((td)->iomix != 0)
433 #define BLOCKS_PER_MAP (8 * sizeof(long))
434 #define TO_MAP_BLOCK(td, f, b) ((b) - ((f)->file_offset / (td)->rw_min_bs))
435 #define RAND_MAP_IDX(td, f, b) (TO_MAP_BLOCK(td, f, b) / BLOCKS_PER_MAP)
436 #define RAND_MAP_BIT(td, f, b) (TO_MAP_BLOCK(td, f, b) & (BLOCKS_PER_MAP - 1))
438 #define MAX_JOBS (1024)
440 static inline int should_fsync(struct thread_data *td)
442 if (td->last_was_sync)
446 if (td_write(td) || td_rw(td) || td->override_sync)
453 * Disk utils as read in /sys/block/<dev>/stat
455 struct disk_util_stat {
458 unsigned long long sectors[2];
461 unsigned time_in_queue;
465 * Per-device disk util management
468 struct list_head list;
474 struct disk_util_stat dus;
475 struct disk_util_stat last_dus;
482 * Used for passing io_u completion data
484 struct io_completion_data {
487 int error; /* output */
488 unsigned long bytes_done[2]; /* output */
489 struct timeval time; /* output */
492 #define DISK_UTIL_MSEC (250)
495 #define min(a, b) ((a) < (b) ? (a) : (b))
498 #define max(a, b) ((a) > (b) ? (a) : (b))
504 extern int read_iolog_get(struct thread_data *, struct io_u *);
505 extern void write_iolog_put(struct thread_data *, struct io_u *);
506 extern int init_iolog(struct thread_data *td);
507 extern void log_io_piece(struct thread_data *, struct io_u *);
508 extern void prune_io_piece_log(struct thread_data *);
509 extern void write_iolog_close(struct thread_data *);
514 extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long);
515 extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long);
516 extern void add_bw_sample(struct thread_data *, enum fio_ddir, struct timeval *);
517 extern void show_run_stats(void);
518 extern void init_disk_util(struct thread_data *);
519 extern void update_rusage_stat(struct thread_data *);
520 extern void update_io_ticks(void);
521 extern void disk_util_timer_arm(void);
522 extern void setup_log(struct io_log **);
523 extern void finish_log(struct thread_data *, struct io_log *, const char *);
524 extern void __finish_log(struct io_log *, const char *);
525 extern int setup_rate(struct thread_data *);
526 extern struct io_log *agg_io_log[2];
527 extern int write_bw_log;
528 extern void add_agg_sample(unsigned long, enum fio_ddir);
533 extern unsigned long utime_since(struct timeval *, struct timeval *);
534 extern unsigned long utime_since_now(struct timeval *);
535 extern unsigned long mtime_since(struct timeval *, struct timeval *);
536 extern unsigned long mtime_since_now(struct timeval *);
537 extern unsigned long time_since_now(struct timeval *);
538 extern unsigned long mtime_since_genesis(void);
539 extern void __usec_sleep(unsigned int);
540 extern void usec_sleep(struct thread_data *, unsigned long);
541 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int, int);
542 extern void fill_start_time(struct timeval *);
543 extern void fio_gettime(struct timeval *, void *);
548 extern int parse_options(int, char **);
549 extern int init_random_state(struct thread_data *);
552 * File setup/shutdown
554 extern void close_files(struct thread_data *);
555 extern int setup_files(struct thread_data *);
556 extern int open_files(struct thread_data *);
557 extern int file_invalidate_cache(struct thread_data *, struct fio_file *);
562 extern void print_thread_status(void);
563 extern void print_status_init(int);
566 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
567 * will never back again. It may cycle between running/verififying/fsyncing.
568 * Once the thread reaches TD_EXITED, it is just waiting for the core to
585 extern void populate_verify_io_u(struct thread_data *, struct io_u *);
586 extern int get_next_verify(struct thread_data *td, struct io_u *);
587 extern int do_io_u_verify(struct thread_data *, struct io_u **);
592 extern int fio_pin_memory(void);
593 extern void fio_unpin_memory(void);
594 extern int allocate_io_mem(struct thread_data *);
595 extern void free_io_mem(struct thread_data *);
600 #define queue_full(td) list_empty(&(td)->io_u_freelist)
601 extern struct io_u *__get_io_u(struct thread_data *);
602 extern struct io_u *get_io_u(struct thread_data *, struct fio_file *);
603 extern void put_io_u(struct thread_data *, struct io_u *);
604 extern void ios_completed(struct thread_data *, struct io_completion_data *);
605 extern void io_completed(struct thread_data *, struct io_u *, struct io_completion_data *);
608 * io engine entry points
610 extern int td_io_init(struct thread_data *);
611 extern int td_io_prep(struct thread_data *, struct io_u *);
612 extern int td_io_queue(struct thread_data *, struct io_u *);
613 extern int td_io_sync(struct thread_data *, struct fio_file *);
614 extern int td_io_getevents(struct thread_data *, int, int, struct timespec *);
617 * This is a pretty crappy semaphore implementation, but with the use that fio
618 * has (just signalling start/go conditions), it doesn't have to be better.
619 * Naturally this would not work for any type of contended semaphore or
622 static inline void fio_sem_init(volatile int *sem, int val)
627 static inline void fio_sem_down(volatile int *sem)
635 static inline void fio_sem_up(volatile int *sem)
641 * If logging output to a file, stderr should go to both stderr and f_err
643 #define log_err(args...) do { \
644 fprintf(f_err, ##args); \
645 if (f_err != stderr) \
646 fprintf(stderr, ##args); \
649 struct ioengine_ops {
650 struct list_head list;
654 int (*setup)(struct thread_data *);
655 int (*init)(struct thread_data *);
656 int (*prep)(struct thread_data *, struct io_u *);
657 int (*queue)(struct thread_data *, struct io_u *);
658 int (*getevents)(struct thread_data *, int, int, struct timespec *);
659 struct io_u *(*event)(struct thread_data *, int);
660 int (*cancel)(struct thread_data *, struct io_u *);
661 void (*cleanup)(struct thread_data *);
666 #define FIO_IOOPS_VERSION 3
668 extern struct ioengine_ops *load_ioengine(struct thread_data *, const char *);
669 extern int register_ioengine(struct ioengine_ops *);
670 extern void unregister_ioengine(struct ioengine_ops *);
671 extern void close_ioengine(struct thread_data *);
674 * Mark unused variables passed to ops functions as unused, to silence gcc
676 #define fio_unused __attribute((__unused__))
677 #define fio_init __attribute__((constructor))
678 #define fio_exit __attribute__((destructor))
680 #define for_each_td(td, i) \
681 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
682 #define for_each_file(td, f, i) \
683 for ((i) = 0, (f) = &(td)->files[0]; (i) < (int) (td)->nr_files; (i)++, (f)++)
685 #define fio_assert(td, cond) do { \
688 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
689 (td)->runstate = TD_EXITED; \
690 (td)->error = EFAULT; \