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;
84 #ifdef FIO_HAVE_LIBAIO
87 #ifdef FIO_HAVE_POSIXAIO
93 #ifdef FIO_HAVE_SYSLET
94 struct syslet_req req;
97 struct timeval start_time;
98 struct timeval issue_time;
101 * Allocated/set buffer and length
104 unsigned long buflen;
105 unsigned long long offset;
108 * IO engine state, may be different from above when we get
109 * partial transfers / residual data counts
112 unsigned long xfer_buflen;
120 * io engine private data
127 struct fio_file *file;
129 struct list_head list;
133 * io_ops->queue() return values
136 FIO_Q_COMPLETED = 0, /* completed sync */
137 FIO_Q_QUEUED = 1, /* queued, will complete async */
138 FIO_Q_BUSY = 2, /* no more room, call ->commit() */
141 #define FIO_HDR_MAGIC 0xf00baaef
144 VERIFY_NONE = 0, /* no verification */
145 VERIFY_MD5, /* md5 sum data blocks */
146 VERIFY_CRC32, /* crc32 sum data blocks */
150 * A header structure associated with each checksummed data block
152 struct verify_header {
153 unsigned int fio_magic;
155 unsigned int verify_type;
157 char md5_digest[MD5_HASH_WORDS * 4];
162 struct group_run_stats {
163 unsigned long long max_run[2], min_run[2];
164 unsigned long long max_bw[2], min_bw[2];
165 unsigned long long io_kb[2];
166 unsigned long long agg[2];
170 * What type of allocation to use for io buffers
173 MEM_MALLOC = 0, /* ordinary malloc */
174 MEM_SHM, /* use shared memory segments */
175 MEM_SHMHUGE, /* use shared memory segments with huge pages */
176 MEM_MMAP, /* use anonynomous mmap */
177 MEM_MMAPHUGE, /* memory mapped huge file */
181 * The type of object we are working on
184 FIO_TYPE_FILE = 1, /* plain file */
185 FIO_TYPE_BD, /* block device */
186 FIO_TYPE_CHAR, /* character device */
189 enum fio_ioengine_flags {
190 FIO_SYNCIO = 1 << 0, /* io engine has synchronous ->queue */
191 FIO_CPUIO = 1 << 1, /* cpu burner, doesn't do real io */
192 FIO_MMAPIO = 1 << 2, /* uses memory mapped io */
193 FIO_RAWIO = 1 << 3, /* some sort of direct/raw io */
194 FIO_NETIO = 1 << 4, /* networked io */
195 FIO_NULLIO = 1 << 5, /* no real data transfer (cpu/null) */
199 * Each thread_data structure has a number of files associated with it,
200 * this structure holds state information for a single file.
204 * A file may not be a file descriptor, let the io engine decide
207 unsigned long file_data;
212 unsigned long long file_size;
213 unsigned long long real_file_size;
214 unsigned long long file_offset;
215 unsigned long long last_pos;
216 unsigned long long last_completed_pos;
219 * block map for random io
221 unsigned long *file_map;
222 unsigned int num_maps;
223 unsigned int last_free_lookup;
229 struct io_log *slat_log;
230 struct io_log *clat_log;
231 struct io_log *bw_log;
234 * bandwidth and latency stats
236 struct io_stat clat_stat[2]; /* completion latency */
237 struct io_stat slat_stat[2]; /* submission latency */
238 struct io_stat bw_stat[2]; /* bandwidth stats */
240 unsigned long long stat_io_bytes[2];
241 struct timeval stat_sample_time[2];
244 * fio system usage accounting
246 struct rusage ru_start;
247 struct rusage ru_end;
248 unsigned long usr_time;
249 unsigned long sys_time;
254 * How many depth levels to log
256 #define FIO_IO_U_MAP_NR 8
257 #define FIO_IO_U_LAT_NR 12
260 * This describes a single thread/process executing a fio job.
271 struct thread_stat ts;
272 enum fio_filetype filetype;
273 struct fio_file *files;
274 unsigned int nr_files;
275 unsigned int nr_uniq_files;
276 unsigned int next_file;
280 size_t orig_buffer_size;
281 volatile int terminate;
282 volatile int runstate;
286 unsigned int last_was_sync;
288 unsigned int sequential;
289 unsigned int odirect;
290 unsigned int invalidate_cache;
291 unsigned int create_serialize;
292 unsigned int create_fsync;
293 unsigned int end_fsync;
294 unsigned int sync_io;
296 unsigned int use_thread;
298 unsigned int do_disk_util;
299 unsigned int override_sync;
300 unsigned int rand_repeatable;
301 unsigned int write_lat_log;
302 unsigned int write_bw_log;
303 unsigned int norandommap;
304 unsigned int bs_unaligned;
307 unsigned int min_bs[2];
308 unsigned int max_bs[2];
309 unsigned int hugepage_size;
310 unsigned int rw_min_bs;
311 unsigned int thinktime;
312 unsigned int thinktime_spin;
313 unsigned int thinktime_blocks;
314 unsigned int fsync_blocks;
315 unsigned int start_delay;
316 unsigned long timeout;
317 unsigned int overwrite;
318 unsigned int bw_avg_time;
320 unsigned long long zone_size;
321 unsigned long long zone_skip;
322 enum fio_memtype mem_type;
325 unsigned int stonewall;
326 unsigned int numjobs;
327 unsigned int iodepth;
328 unsigned int iodepth_low;
329 os_cpu_mask_t cpumask;
331 unsigned int read_iolog;
332 unsigned int rwmixcycle;
333 unsigned int rwmixread;
334 unsigned int rwmixwrite;
337 char *read_iolog_file;
338 char *write_iolog_file;
345 os_random_state_t bsrange_state;
346 os_random_state_t verify_state;
351 * IO engine hooks, contains everything needed to submit an io_u
352 * to any of the available IO engines.
354 struct ioengine_ops *io_ops;
357 * Current IO depth and list of free and busy io_u's.
359 unsigned int cur_depth;
360 unsigned int io_u_map[FIO_IO_U_MAP_NR];
361 unsigned int io_u_lat[FIO_IO_U_LAT_NR];
362 unsigned long total_io_u;
363 struct list_head io_u_freelist;
364 struct list_head io_u_busylist;
365 struct list_head io_u_requeues;
371 unsigned int ratemin;
372 unsigned int ratecycle;
373 unsigned long rate_usec_cycle;
374 long rate_pending_usleep;
375 unsigned long rate_bytes;
376 struct timeval lastrate;
378 unsigned long runtime[2]; /* msec */
379 unsigned long long io_size;
380 unsigned long long total_file_size;
381 unsigned long long start_offset;
382 unsigned long long total_io_size;
384 unsigned long io_issues[2];
385 unsigned long long io_blocks[2];
386 unsigned long long io_bytes[2];
387 unsigned long long this_io_bytes[2];
388 unsigned long long zone_bytes;
392 * State for random io, a bitmap of blocks done vs not done
394 os_random_state_t random_state;
397 * CPU "io" cycle burner
399 unsigned int cpuload;
400 unsigned int cpucycle;
402 struct timeval start; /* start of this loop */
403 struct timeval epoch; /* time job was started */
404 struct timeval end_time;/* time job ended */
407 * read/write mixed workload state
409 os_random_state_t rwmix_state;
410 struct timeval rwmix_switch;
411 enum fio_ddir rwmix_ddir;
414 * Pre-run and post-run shell
422 struct list_head io_hist_list;
423 struct list_head io_log_list;
426 #define __td_verror(td, err, msg) \
432 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, error=%s", __FILE__, __LINE__, (msg)); \
436 #define td_verror(td, err) __td_verror((td), (err), strerror((err)))
437 #define td_vmsg(td, err, msg) __td_verror((td), (err), (msg))
439 extern int exitall_on_terminate;
440 extern int thread_number;
443 extern int terse_output;
446 extern int temp_stall_ts;
447 extern unsigned long long mlock_size;
449 extern struct thread_data *threads;
451 #define td_read(td) ((td)->ddir == DDIR_READ)
452 #define td_write(td) ((td)->ddir == DDIR_WRITE)
453 #define td_rw(td) ((td)->iomix != 0)
455 #define BLOCKS_PER_MAP (8 * sizeof(long))
456 #define TO_MAP_BLOCK(td, f, b) ((b) - ((f)->file_offset / (td)->rw_min_bs))
457 #define RAND_MAP_IDX(td, f, b) (TO_MAP_BLOCK(td, f, b) / BLOCKS_PER_MAP)
458 #define RAND_MAP_BIT(td, f, b) (TO_MAP_BLOCK(td, f, b) & (BLOCKS_PER_MAP - 1))
460 #define MAX_JOBS (1024)
462 static inline int should_fsync(struct thread_data *td)
464 if (td->last_was_sync)
468 if (td_write(td) || td_rw(td) || td->override_sync)
475 * Disk utils as read in /sys/block/<dev>/stat
477 struct disk_util_stat {
480 unsigned long long sectors[2];
483 unsigned time_in_queue;
487 * Per-device disk util management
490 struct list_head list;
496 struct disk_util_stat dus;
497 struct disk_util_stat last_dus;
504 * Callback for io completion
506 typedef int (endio_handler)(struct io_u *);
508 #define DISK_UTIL_MSEC (250)
511 #define min(a, b) ((a) < (b) ? (a) : (b))
514 #define max(a, b) ((a) > (b) ? (a) : (b))
520 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
521 extern void write_iolog_put(struct thread_data *, struct io_u *);
522 extern int __must_check init_iolog(struct thread_data *td);
523 extern void log_io_piece(struct thread_data *, struct io_u *);
524 extern void prune_io_piece_log(struct thread_data *);
525 extern void write_iolog_close(struct thread_data *);
530 extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long);
531 extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long);
532 extern void add_bw_sample(struct thread_data *, enum fio_ddir, struct timeval *);
533 extern void show_run_stats(void);
534 extern void init_disk_util(struct thread_data *);
535 extern void update_rusage_stat(struct thread_data *);
536 extern void update_io_ticks(void);
537 extern void disk_util_timer_arm(void);
538 extern void setup_log(struct io_log **);
539 extern void finish_log(struct thread_data *, struct io_log *, const char *);
540 extern void __finish_log(struct io_log *, const char *);
541 extern int setup_rate(struct thread_data *);
542 extern struct io_log *agg_io_log[2];
543 extern int write_bw_log;
544 extern void add_agg_sample(unsigned long, enum fio_ddir);
549 extern unsigned long utime_since(struct timeval *, struct timeval *);
550 extern unsigned long utime_since_now(struct timeval *);
551 extern unsigned long mtime_since(struct timeval *, struct timeval *);
552 extern unsigned long mtime_since_now(struct timeval *);
553 extern unsigned long time_since_now(struct timeval *);
554 extern unsigned long mtime_since_genesis(void);
555 extern void __usec_sleep(unsigned int);
556 extern void usec_sleep(struct thread_data *, unsigned long);
557 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int, int);
558 extern void fill_start_time(struct timeval *);
559 extern void fio_gettime(struct timeval *, void *);
564 extern int __must_check parse_options(int, char **);
565 extern int __must_check init_random_state(struct thread_data *);
568 * File setup/shutdown
570 extern void close_files(struct thread_data *);
571 extern int __must_check setup_files(struct thread_data *);
572 extern int __must_check open_files(struct thread_data *);
573 extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
578 extern void print_thread_status(void);
579 extern void print_status_init(int);
582 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
583 * will never back again. It may cycle between running/verififying/fsyncing.
584 * Once the thread reaches TD_EXITED, it is just waiting for the core to
601 extern void populate_verify_io_u(struct thread_data *, struct io_u *);
602 extern int __must_check get_next_verify(struct thread_data *td, struct io_u *);
603 extern int __must_check verify_io_u(struct io_u *);
608 extern int __must_check fio_pin_memory(void);
609 extern void fio_unpin_memory(void);
610 extern int __must_check allocate_io_mem(struct thread_data *);
611 extern void free_io_mem(struct thread_data *);
616 #define queue_full(td) list_empty(&(td)->io_u_freelist)
617 extern struct io_u *__get_io_u(struct thread_data *);
618 extern struct io_u *get_io_u(struct thread_data *);
619 extern void put_io_u(struct thread_data *, struct io_u *);
620 extern void requeue_io_u(struct thread_data *, struct io_u **);
621 extern long __must_check io_u_sync_complete(struct thread_data *, struct io_u *, endio_handler *);
622 extern long __must_check io_u_queued_complete(struct thread_data *, int, endio_handler *);
623 extern void io_u_queued(struct thread_data *, struct io_u *);
626 * io engine entry points
628 extern int __must_check td_io_init(struct thread_data *);
629 extern int __must_check td_io_prep(struct thread_data *, struct io_u *);
630 extern int __must_check td_io_queue(struct thread_data *, struct io_u *);
631 extern int __must_check td_io_sync(struct thread_data *, struct fio_file *);
632 extern int __must_check td_io_getevents(struct thread_data *, int, int, struct timespec *);
633 extern int __must_check td_io_commit(struct thread_data *);
636 * This is a pretty crappy semaphore implementation, but with the use that fio
637 * has (just signalling start/go conditions), it doesn't have to be better.
638 * Naturally this would not work for any type of contended semaphore or
641 static inline void fio_sem_init(volatile int *sem, int val)
646 static inline void fio_sem_down(volatile int *sem)
654 static inline void fio_sem_up(volatile int *sem)
660 * If logging output to a file, stderr should go to both stderr and f_err
662 #define log_err(args...) do { \
663 fprintf(f_err, ##args); \
664 if (f_err != stderr) \
665 fprintf(stderr, ##args); \
668 struct ioengine_ops {
669 struct list_head list;
673 int (*setup)(struct thread_data *);
674 int (*init)(struct thread_data *);
675 int (*prep)(struct thread_data *, struct io_u *);
676 int (*queue)(struct thread_data *, struct io_u *);
677 int (*commit)(struct thread_data *);
678 int (*getevents)(struct thread_data *, int, int, struct timespec *);
679 struct io_u *(*event)(struct thread_data *, int);
680 int (*cancel)(struct thread_data *, struct io_u *);
681 void (*cleanup)(struct thread_data *);
687 #define FIO_IOOPS_VERSION 5
689 extern struct ioengine_ops *load_ioengine(struct thread_data *, const char *);
690 extern void register_ioengine(struct ioengine_ops *);
691 extern void unregister_ioengine(struct ioengine_ops *);
692 extern void close_ioengine(struct thread_data *);
695 * Mark unused variables passed to ops functions as unused, to silence gcc
697 #define fio_unused __attribute((__unused__))
698 #define fio_init __attribute__((constructor))
699 #define fio_exit __attribute__((destructor))
701 #define for_each_td(td, i) \
702 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
703 #define for_each_file(td, f, i) \
704 for ((i) = 0, (f) = &(td)->files[0]; (i) < (int) (td)->nr_files; (i)++, (f)++)
706 #define fio_assert(td, cond) do { \
709 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
710 (td)->runstate = TD_EXITED; \
711 (td)->error = EFAULT; \