8 #include <sys/resource.h>
21 #ifdef FIO_HAVE_SYSLET
32 * Use for maintaining statistics
35 unsigned long max_val;
36 unsigned long min_val;
37 unsigned long samples;
44 * A single data sample
53 * Dynamically growing data sample log
56 unsigned long nr_samples;
57 unsigned long max_samples;
58 struct io_sample *log;
62 * When logging io actions, this matches a single sent io_u
65 struct list_head list;
66 struct fio_file *file;
67 unsigned long long offset;
72 #ifdef FIO_HAVE_SYSLET
74 struct syslet_uatom atom;
81 IO_U_F_FLIGHT = 1 << 1,
89 #ifdef FIO_HAVE_LIBAIO
92 #ifdef FIO_HAVE_POSIXAIO
98 #ifdef FIO_HAVE_SYSLET
99 struct syslet_req req;
102 struct timeval start_time;
103 struct timeval issue_time;
106 * Allocated/set buffer and length
109 unsigned long buflen;
110 unsigned long long offset;
113 * IO engine state, may be different from above when we get
114 * partial transfers / residual data counts
117 unsigned long xfer_buflen;
125 * io engine private data
134 struct fio_file *file;
136 struct list_head list;
140 * io_ops->queue() return values
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() */
148 #define FIO_HDR_MAGIC 0xf00baaef
151 VERIFY_NONE = 0, /* no verification */
152 VERIFY_MD5, /* md5 sum data blocks */
153 VERIFY_CRC32, /* crc32 sum data blocks */
157 * A header structure associated with each checksummed data block
159 struct verify_header {
160 unsigned int fio_magic;
162 unsigned int verify_type;
164 char md5_digest[MD5_HASH_WORDS * 4];
169 struct 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];
177 * What type of allocation to use for io buffers
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 */
188 * The type of object we are working on
191 FIO_TYPE_FILE = 1, /* plain file */
192 FIO_TYPE_BD, /* block device */
193 FIO_TYPE_CHAR, /* character device */
196 enum 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) */
206 * Each thread_data structure has a number of files associated with it,
207 * this structure holds state information for a single file.
211 * A file may not be a file descriptor, let the io engine decide
214 unsigned long file_data;
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;
226 * block map for random io
228 unsigned long *file_map;
229 unsigned int num_maps;
230 unsigned int last_free_lookup;
236 struct io_log *slat_log;
237 struct io_log *clat_log;
238 struct io_log *bw_log;
241 * bandwidth and latency stats
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 */
247 unsigned long long stat_io_bytes[2];
248 struct timeval stat_sample_time[2];
251 * fio system usage accounting
253 struct rusage ru_start;
254 struct rusage ru_end;
255 unsigned long usr_time;
256 unsigned long sys_time;
261 * How many depth levels to log
263 #define FIO_IO_U_MAP_NR 8
264 #define FIO_IO_U_LAT_NR 12
267 * This describes a single thread/process executing a fio job.
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;
287 size_t orig_buffer_size;
288 volatile int terminate;
289 volatile int runstate;
293 unsigned int last_was_sync;
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;
303 unsigned int use_thread;
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;
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;
327 unsigned long long zone_size;
328 unsigned long long zone_skip;
329 enum fio_memtype mem_type;
332 unsigned int stonewall;
333 unsigned int numjobs;
334 unsigned int iodepth;
335 unsigned int iodepth_low;
336 os_cpu_mask_t cpumask;
338 unsigned int read_iolog;
339 unsigned int rwmixcycle;
340 unsigned int rwmixread;
341 unsigned int rwmixwrite;
344 char *read_iolog_file;
345 char *write_iolog_file;
352 os_random_state_t bsrange_state;
353 os_random_state_t verify_state;
358 * IO engine hooks, contains everything needed to submit an io_u
359 * to any of the available IO engines.
361 struct ioengine_ops *io_ops;
364 * Current IO depth and list of free and busy io_u's.
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;
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;
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;
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;
399 * State for random io, a bitmap of blocks done vs not done
401 os_random_state_t random_state;
404 * CPU "io" cycle burner
406 unsigned int cpuload;
407 unsigned int cpucycle;
409 struct timeval start; /* start of this loop */
410 struct timeval epoch; /* time job was started */
411 struct timeval end_time;/* time job ended */
414 * read/write mixed workload state
416 os_random_state_t rwmix_state;
417 struct timeval rwmix_switch;
418 enum fio_ddir rwmix_ddir;
421 * Pre-run and post-run shell
429 struct list_head io_hist_list;
430 struct list_head io_log_list;
435 struct timeval timeout_end;
436 struct itimerval timer;
440 * 30 second per-io_u timeout, with 5 second intervals to avoid resetting
441 * the timer on each queue operation.
443 #define IO_U_TIMEOUT_INC 5
444 #define IO_U_TIMEOUT 30
446 #define __td_verror(td, err, msg, func) \
452 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
456 #define td_verror(td, err, func) \
457 __td_verror((td), (err), strerror((err)), (func))
458 #define td_vmsg(td, err, msg, func) \
459 __td_verror((td), (err), (msg), (func))
461 extern int exitall_on_terminate;
462 extern int thread_number;
465 extern int terse_output;
468 extern int temp_stall_ts;
469 extern unsigned long long mlock_size;
471 extern struct thread_data *threads;
473 #define td_read(td) ((td)->ddir == DDIR_READ)
474 #define td_write(td) ((td)->ddir == DDIR_WRITE)
475 #define td_rw(td) ((td)->iomix != 0)
477 #define BLOCKS_PER_MAP (8 * sizeof(long))
478 #define TO_MAP_BLOCK(td, f, b) ((b) - ((f)->file_offset / (td)->rw_min_bs))
479 #define RAND_MAP_IDX(td, f, b) (TO_MAP_BLOCK(td, f, b) / BLOCKS_PER_MAP)
480 #define RAND_MAP_BIT(td, f, b) (TO_MAP_BLOCK(td, f, b) & (BLOCKS_PER_MAP - 1))
482 #define MAX_JOBS (1024)
484 static inline int should_fsync(struct thread_data *td)
486 if (td->last_was_sync)
490 if (td_write(td) || td_rw(td) || td->override_sync)
497 * Disk utils as read in /sys/block/<dev>/stat
499 struct disk_util_stat {
502 unsigned long long sectors[2];
505 unsigned time_in_queue;
509 * Per-device disk util management
512 struct list_head list;
518 struct disk_util_stat dus;
519 struct disk_util_stat last_dus;
526 * Callback for io completion
528 typedef int (endio_handler)(struct io_u *);
530 #define DISK_UTIL_MSEC (250)
533 #define min(a, b) ((a) < (b) ? (a) : (b))
536 #define max(a, b) ((a) > (b) ? (a) : (b))
542 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
543 extern void write_iolog_put(struct thread_data *, struct io_u *);
544 extern int __must_check init_iolog(struct thread_data *td);
545 extern void log_io_piece(struct thread_data *, struct io_u *);
546 extern void prune_io_piece_log(struct thread_data *);
547 extern void write_iolog_close(struct thread_data *);
552 extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long);
553 extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long);
554 extern void add_bw_sample(struct thread_data *, enum fio_ddir, struct timeval *);
555 extern void show_run_stats(void);
556 extern void init_disk_util(struct thread_data *);
557 extern void update_rusage_stat(struct thread_data *);
558 extern void update_io_ticks(void);
559 extern void disk_util_timer_arm(void);
560 extern void setup_log(struct io_log **);
561 extern void finish_log(struct thread_data *, struct io_log *, const char *);
562 extern void __finish_log(struct io_log *, const char *);
563 extern int setup_rate(struct thread_data *);
564 extern struct io_log *agg_io_log[2];
565 extern int write_bw_log;
566 extern void add_agg_sample(unsigned long, enum fio_ddir);
571 extern unsigned long utime_since(struct timeval *, struct timeval *);
572 extern unsigned long utime_since_now(struct timeval *);
573 extern unsigned long mtime_since(struct timeval *, struct timeval *);
574 extern unsigned long mtime_since_now(struct timeval *);
575 extern unsigned long time_since_now(struct timeval *);
576 extern unsigned long mtime_since_genesis(void);
577 extern void __usec_sleep(unsigned int);
578 extern void usec_sleep(struct thread_data *, unsigned long);
579 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int, int);
580 extern void fill_start_time(struct timeval *);
581 extern void fio_gettime(struct timeval *, void *);
582 extern void set_genesis_time(void);
587 extern int __must_check parse_options(int, char **);
588 extern int __must_check init_random_state(struct thread_data *);
591 * File setup/shutdown
593 extern void close_files(struct thread_data *);
594 extern int __must_check setup_files(struct thread_data *);
595 extern int __must_check open_files(struct thread_data *);
596 extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
601 extern void print_thread_status(void);
602 extern void print_status_init(int);
605 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
606 * will never back again. It may cycle between running/verififying/fsyncing.
607 * Once the thread reaches TD_EXITED, it is just waiting for the core to
624 extern void populate_verify_io_u(struct thread_data *, struct io_u *);
625 extern int __must_check get_next_verify(struct thread_data *td, struct io_u *);
626 extern int __must_check verify_io_u(struct io_u *);
631 extern int __must_check fio_pin_memory(void);
632 extern void fio_unpin_memory(void);
633 extern int __must_check allocate_io_mem(struct thread_data *);
634 extern void free_io_mem(struct thread_data *);
639 #define queue_full(td) list_empty(&(td)->io_u_freelist)
640 extern struct io_u *__get_io_u(struct thread_data *);
641 extern struct io_u *get_io_u(struct thread_data *);
642 extern void put_io_u(struct thread_data *, struct io_u *);
643 extern void requeue_io_u(struct thread_data *, struct io_u **);
644 extern long __must_check io_u_sync_complete(struct thread_data *, struct io_u *, endio_handler *);
645 extern long __must_check io_u_queued_complete(struct thread_data *, int, endio_handler *);
646 extern void io_u_queued(struct thread_data *, struct io_u *);
647 extern void io_u_init_timeout(void);
648 extern void io_u_set_timeout(struct thread_data *);
651 * io engine entry points
653 extern int __must_check td_io_init(struct thread_data *);
654 extern int __must_check td_io_prep(struct thread_data *, struct io_u *);
655 extern int __must_check td_io_queue(struct thread_data *, struct io_u *);
656 extern int __must_check td_io_sync(struct thread_data *, struct fio_file *);
657 extern int __must_check td_io_getevents(struct thread_data *, int, int, struct timespec *);
658 extern int __must_check td_io_commit(struct thread_data *);
661 * This is a pretty crappy semaphore implementation, but with the use that fio
662 * has (just signalling start/go conditions), it doesn't have to be better.
663 * Naturally this would not work for any type of contended semaphore or
666 static inline void fio_sem_init(volatile int *sem, int val)
671 static inline void fio_sem_down(volatile int *sem)
679 static inline void fio_sem_up(volatile int *sem)
685 * If logging output to a file, stderr should go to both stderr and f_err
687 #define log_err(args...) do { \
688 fprintf(f_err, ##args); \
689 if (f_err != stderr) \
690 fprintf(stderr, ##args); \
693 struct ioengine_ops {
694 struct list_head list;
698 int (*setup)(struct thread_data *);
699 int (*init)(struct thread_data *);
700 int (*prep)(struct thread_data *, struct io_u *);
701 int (*queue)(struct thread_data *, struct io_u *);
702 int (*commit)(struct thread_data *);
703 int (*getevents)(struct thread_data *, int, int, struct timespec *);
704 struct io_u *(*event)(struct thread_data *, int);
705 int (*cancel)(struct thread_data *, struct io_u *);
706 void (*cleanup)(struct thread_data *);
712 #define FIO_IOOPS_VERSION 5
714 extern struct ioengine_ops *load_ioengine(struct thread_data *, const char *);
715 extern void register_ioengine(struct ioengine_ops *);
716 extern void unregister_ioengine(struct ioengine_ops *);
717 extern void close_ioengine(struct thread_data *);
720 * Mark unused variables passed to ops functions as unused, to silence gcc
722 #define fio_unused __attribute((__unused__))
723 #define fio_init __attribute__((constructor))
724 #define fio_exit __attribute__((destructor))
726 #define for_each_td(td, i) \
727 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
728 #define for_each_file(td, f, i) \
729 for ((i) = 0, (f) = &(td)->files[0]; (i) < (int) (td)->nr_files; (i)++, (f)++)
731 #define fio_assert(td, cond) do { \
734 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
735 (td)->runstate = TD_EXITED; \
736 (td)->error = EFAULT; \