8 #include <sys/resource.h>
18 #include "compiler/compiler.h"
22 #include "arch/arch.h"
35 #ifdef FIO_HAVE_SOLARISAIO
36 #include <sys/asynch.h>
40 * Use for maintaining statistics
43 unsigned long max_val;
44 unsigned long min_val;
45 unsigned long samples;
52 * A single data sample
62 * Dynamically growing data sample log
65 unsigned long nr_samples;
66 unsigned long max_samples;
67 struct io_sample *log;
71 * When logging io actions, this matches a single sent io_u
75 struct rb_node rb_node;
76 struct flist_head list;
80 struct fio_file *file;
82 unsigned long long offset;
87 unsigned int file_action;
91 struct group_run_stats {
92 unsigned long long max_run[2], min_run[2];
93 unsigned long long max_bw[2], min_bw[2];
94 unsigned long long io_kb[2];
95 unsigned long long agg[2];
99 * What type of allocation to use for io buffers
102 MEM_MALLOC = 0, /* ordinary malloc */
103 MEM_SHM, /* use shared memory segments */
104 MEM_SHMHUGE, /* use shared memory segments with huge pages */
105 MEM_MMAP, /* use anonynomous mmap */
106 MEM_MMAPHUGE, /* memory mapped huge file */
109 enum fio_ioengine_flags {
110 FIO_SYNCIO = 1 << 0, /* io engine has synchronous ->queue */
111 FIO_RAWIO = 1 << 1, /* some sort of direct/raw io */
112 FIO_DISKLESSIO = 1 << 2, /* no disk involved */
113 FIO_NOEXTEND = 1 << 3, /* engine can't extend file */
114 FIO_NODISKUTIL = 1 << 4, /* diskutil can't handle filename */
115 FIO_UNIDIR = 1 << 5, /* engine is uni-directional */
116 FIO_NOIO = 1 << 6, /* thread does only pseudo IO */
117 FIO_SIGQUIT = 1 << 7, /* needs SIGQUIT to exit */
121 * How many depth levels to log
123 #define FIO_IO_U_MAP_NR 8
124 #define FIO_IO_U_LAT_U_NR 10
125 #define FIO_IO_U_LAT_M_NR 12
136 struct io_log *slat_log;
137 struct io_log *clat_log;
138 struct io_log *bw_log;
141 * bandwidth and latency stats
143 struct io_stat clat_stat[2]; /* completion latency */
144 struct io_stat slat_stat[2]; /* submission latency */
145 struct io_stat bw_stat[2]; /* bandwidth stats */
147 unsigned long long stat_io_bytes[2];
148 struct timeval stat_sample_time[2];
151 * fio system usage accounting
153 struct rusage ru_start;
154 struct rusage ru_end;
155 unsigned long usr_time;
156 unsigned long sys_time;
158 unsigned long minf, majf;
161 * IO depth and latency stats
163 unsigned int io_u_map[FIO_IO_U_MAP_NR];
164 unsigned int io_u_submit[FIO_IO_U_MAP_NR];
165 unsigned int io_u_complete[FIO_IO_U_MAP_NR];
166 unsigned int io_u_lat_u[FIO_IO_U_LAT_U_NR];
167 unsigned int io_u_lat_m[FIO_IO_U_LAT_M_NR];
168 unsigned long total_io_u[2];
169 unsigned long short_io_u[2];
170 unsigned long total_submit;
171 unsigned long total_complete;
173 unsigned long long io_bytes[2];
174 unsigned long runtime[2];
175 unsigned long total_run_time;
183 struct thread_options {
191 enum td_ddir td_ddir;
192 unsigned int ddir_nr;
193 unsigned int iodepth;
194 unsigned int iodepth_low;
195 unsigned int iodepth_batch;
196 unsigned int iodepth_batch_complete;
198 unsigned long long size;
199 unsigned int fill_device;
200 unsigned long long file_size_low;
201 unsigned long long file_size_high;
202 unsigned long long start_offset;
206 unsigned int min_bs[2];
207 unsigned int max_bs[2];
208 struct bssplit *bssplit[2];
209 unsigned int bssplit_nr[2];
211 unsigned int nr_files;
212 unsigned int open_files;
213 enum file_lock_mode file_lock_mode;
214 unsigned int lockfile_batch;
216 unsigned int odirect;
217 unsigned int invalidate_cache;
218 unsigned int create_serialize;
219 unsigned int create_fsync;
220 unsigned int create_on_open;
221 unsigned int end_fsync;
222 unsigned int pre_read;
223 unsigned int sync_io;
225 unsigned int do_verify;
226 unsigned int verifysort;
227 unsigned int verify_interval;
228 unsigned int verify_offset;
229 unsigned int verify_pattern;
230 unsigned int verify_pattern_bytes;
231 unsigned int verify_fatal;
232 unsigned int use_thread;
234 unsigned int do_disk_util;
235 unsigned int override_sync;
236 unsigned int rand_repeatable;
237 unsigned int write_lat_log;
238 unsigned int write_bw_log;
239 unsigned int norandommap;
240 unsigned int softrandommap;
241 unsigned int bs_unaligned;
242 unsigned int fsync_on_close;
244 unsigned int hugepage_size;
245 unsigned int rw_min_bs;
246 unsigned int thinktime;
247 unsigned int thinktime_spin;
248 unsigned int thinktime_blocks;
249 unsigned int fsync_blocks;
250 unsigned int start_delay;
251 unsigned long long timeout;
252 unsigned long long ramp_time;
253 unsigned int overwrite;
254 unsigned int bw_avg_time;
256 unsigned long long zone_size;
257 unsigned long long zone_skip;
258 enum fio_memtype mem_type;
260 unsigned int stonewall;
261 unsigned int new_group;
262 unsigned int numjobs;
263 os_cpu_mask_t cpumask;
264 unsigned int cpumask_set;
266 unsigned int rwmixcycle;
267 unsigned int rwmix[2];
269 unsigned int file_service_type;
270 unsigned int group_reporting;
271 unsigned int fadvise_hint;
272 unsigned int zero_buffers;
273 unsigned int refill_buffers;
274 unsigned int time_based;
275 unsigned int disable_clat;
276 unsigned int disable_slat;
277 unsigned int disable_bw;
278 unsigned int gtod_reduce;
279 unsigned int gtod_cpu;
280 unsigned int gtod_offload;
282 char *read_iolog_file;
283 char *write_iolog_file;
288 * Pre-run and post-run shell
294 unsigned int ratemin;
295 unsigned int ratecycle;
296 unsigned int rate_iops;
297 unsigned int rate_iops_min;
302 * CPU "io" cycle burner
304 unsigned int cpuload;
305 unsigned int cpucycle;
308 #define FIO_VERROR_SIZE 128
311 * This describes a single thread/process executing a fio job.
314 struct thread_options o;
315 char verror[FIO_VERROR_SIZE];
319 struct thread_stat ts;
320 struct fio_file **files;
321 unsigned int files_size;
322 unsigned int files_index;
323 unsigned int nr_open_files;
324 unsigned int nr_done_files;
325 unsigned int nr_normal_files;
327 unsigned int next_file;
328 os_random_state_t next_file_state;
334 size_t orig_buffer_size;
335 volatile int terminate;
336 volatile int runstate;
338 unsigned int ioprio_set;
339 unsigned int last_was_sync;
349 unsigned long rand_seeds[6];
351 os_random_state_t bsrange_state;
352 os_random_state_t verify_state;
357 * IO engine hooks, contains everything needed to submit an io_u
358 * to any of the available IO engines.
360 struct ioengine_ops *io_ops;
363 * Current IO depth and list of free and busy io_u's.
365 unsigned int cur_depth;
366 unsigned int io_u_queued;
367 struct flist_head io_u_freelist;
368 struct flist_head io_u_busylist;
369 struct flist_head io_u_requeues;
374 unsigned long rate_usec_cycle;
375 long rate_pending_usleep;
376 unsigned long rate_bytes;
377 unsigned long rate_blocks;
378 struct timeval lastrate;
380 unsigned long long total_io_size;
382 unsigned long io_issues[2];
383 unsigned long long io_blocks[2];
384 unsigned long long io_bytes[2];
385 unsigned long long io_skip_bytes;
386 unsigned long long this_io_bytes[2];
387 unsigned long long zone_bytes;
388 struct fio_mutex *mutex;
391 * State for random io, a bitmap of blocks done vs not done
393 os_random_state_t random_state;
395 struct timeval start; /* start of this loop */
396 struct timeval epoch; /* time job was started */
397 struct timeval rw_end[2];
398 struct timeval last_issue;
399 struct timeval tv_cache;
400 unsigned int tv_cache_nr;
401 unsigned int tv_cache_mask;
402 unsigned int rw_end_set[2];
403 unsigned int ramp_time_over;
406 * read/write mixed workload state
408 os_random_state_t rwmix_state;
409 unsigned long rwmix_issues;
410 enum fio_ddir rwmix_ddir;
411 unsigned int ddir_nr;
414 * IO history logs for verification. We use a tree for sorting,
415 * if we are overwriting. Otherwise just use a fifo.
417 struct rb_root io_hist_tree;
418 struct flist_head io_hist_list;
423 struct flist_head io_log_list;
426 * for fileservice, how often to switch to a new file
428 unsigned int file_service_nr;
429 unsigned int file_service_left;
430 struct fio_file *file_service_file;
433 * For generating file sizes
435 os_random_state_t file_size_state;
439 * roundrobin available files, or choose one at random, or do each one
443 FIO_FSERVICE_RANDOM = 1,
445 FIO_FSERVICE_SEQ = 3,
449 * when should interactive ETA output be generated
457 #define __td_verror(td, err, msg, func) \
463 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
467 #define td_verror(td, err, func) \
468 __td_verror((td), (err), strerror((err)), (func))
469 #define td_vmsg(td, err, msg, func) \
470 __td_verror((td), (err), (msg), (func))
472 extern int exitall_on_terminate;
473 extern int thread_number;
474 extern int nr_process, nr_thread;
477 extern int terse_output;
478 extern int temp_stall_ts;
479 extern unsigned long long mlock_size;
480 extern unsigned long page_mask, page_size;
481 extern int read_only;
482 extern int eta_print;
483 extern unsigned long done_secs;
484 extern char *job_section;
485 extern int fio_gtod_offload;
486 extern int fio_gtod_cpu;
488 extern struct thread_data *threads;
490 #define td_read(td) ((td)->o.td_ddir & TD_DDIR_READ)
491 #define td_write(td) ((td)->o.td_ddir & TD_DDIR_WRITE)
492 #define td_rw(td) (((td)->o.td_ddir & TD_DDIR_RW) == TD_DDIR_RW)
493 #define td_random(td) ((td)->o.td_ddir & TD_DDIR_RAND)
494 #define file_randommap(td, f) (!(td)->o.norandommap && (f)->file_map)
496 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
498 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
501 #define BLOCKS_PER_MAP (8 * sizeof(int))
502 #define TO_MAP_BLOCK(f, b) (b)
503 #define RAND_MAP_IDX(f, b) (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
504 #define RAND_MAP_BIT(f, b) (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
506 #define MAX_JOBS (1024)
508 static inline int should_fsync(struct thread_data *td)
510 if (td->last_was_sync)
514 if (td_write(td) || td_rw(td) || td->o.override_sync)
521 * Disk utils as read in /sys/block/<dev>/stat
523 struct disk_util_stat {
526 unsigned long long sectors[2];
529 unsigned time_in_queue;
533 * Per-device disk util management
536 struct flist_head list;
537 /* If this disk is a slave, hook it into the master's
538 * list using this head.
540 struct flist_head slavelist;
547 struct disk_util_stat dus;
548 struct disk_util_stat last_dus;
550 /* For software raids, this entry maintains pointers to the
551 * entries for the slave devices. The disk_util entries for
552 * the slaves devices should primarily be maintained through
553 * the disk_list list, i.e. for memory allocation and
554 * de-allocation, etc. Whereas this list should be used only
555 * for aggregating a software RAID's disk util figures.
557 struct flist_head slaves;
562 struct fio_mutex *lock;
566 static inline void disk_util_inc(struct disk_util *du)
569 fio_mutex_down(du->lock);
571 fio_mutex_up(du->lock);
575 static inline void disk_util_dec(struct disk_util *du)
578 fio_mutex_down(du->lock);
580 fio_mutex_up(du->lock);
584 #define DISK_UTIL_MSEC (250)
596 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
597 extern void log_io_u(struct thread_data *, struct io_u *);
598 extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
599 extern int __must_check init_iolog(struct thread_data *td);
600 extern void log_io_piece(struct thread_data *, struct io_u *);
601 extern void queue_io_piece(struct thread_data *, struct io_piece *);
602 extern void prune_io_piece_log(struct thread_data *);
603 extern void write_iolog_close(struct thread_data *);
608 extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long,
610 extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long,
612 extern void add_bw_sample(struct thread_data *, enum fio_ddir, unsigned int,
614 extern void show_run_stats(void);
615 extern void init_disk_util(struct thread_data *);
616 extern void update_rusage_stat(struct thread_data *);
617 extern void update_io_ticks(void);
618 extern void setup_log(struct io_log **);
619 extern void finish_log(struct thread_data *, struct io_log *, const char *);
620 extern void finish_log_named(struct thread_data *, struct io_log *, const char *, const char *);
621 extern void __finish_log(struct io_log *, const char *);
622 extern struct io_log *agg_io_log[2];
623 extern int write_bw_log;
624 extern void add_agg_sample(unsigned long, enum fio_ddir, unsigned int);
629 extern unsigned long long utime_since(struct timeval *, struct timeval *);
630 extern unsigned long long utime_since_now(struct timeval *);
631 extern unsigned long mtime_since(struct timeval *, struct timeval *);
632 extern unsigned long mtime_since_now(struct timeval *);
633 extern unsigned long time_since_now(struct timeval *);
634 extern unsigned long mtime_since_genesis(void);
635 extern void usec_spin(unsigned int);
636 extern void usec_sleep(struct thread_data *, unsigned long);
637 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
638 extern void fill_start_time(struct timeval *);
639 extern void fio_gettime(struct timeval *, void *);
640 extern void fio_gtod_init(void);
641 extern void fio_gtod_update(void);
642 extern void set_genesis_time(void);
643 extern int ramp_time_over(struct thread_data *);
644 extern int in_ramp_time(struct thread_data *);
647 * Init/option functions
649 extern int __must_check parse_options(int, char **);
650 extern int fio_options_parse(struct thread_data *, char **, int);
651 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
652 extern void fio_fill_default_options(struct thread_data *);
653 extern int fio_show_option_help(const char *);
654 extern void fio_options_dup_and_init(struct option *);
655 extern void options_mem_dupe(struct thread_data *);
656 extern void options_mem_free(struct thread_data *);
657 extern void td_fill_rand_seeds(struct thread_data *);
658 #define FIO_GETOPT_JOB 0x89988998
659 #define FIO_NR_OPTIONS 128
664 extern void print_thread_status(void);
665 extern void print_status_init(int);
670 #ifdef FIO_HAVE_DISK_UTIL
671 extern void show_disk_util(void);
672 extern void init_disk_util(struct thread_data *);
673 extern void update_io_ticks(void);
675 #define show_disk_util()
676 #define init_disk_util(td)
677 #define update_io_ticks()
681 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
682 * will never back again. It may cycle between running/verififying/fsyncing.
683 * Once the thread reaches TD_EXITED, it is just waiting for the core to
699 extern void td_set_runstate(struct thread_data *, int);
704 extern int __must_check fio_pin_memory(void);
705 extern void fio_unpin_memory(void);
706 extern int __must_check allocate_io_mem(struct thread_data *);
707 extern void free_io_mem(struct thread_data *);
710 * Reset stats after ramp time completes
712 extern void reset_all_stats(struct thread_data *);
717 #ifdef FIO_HAVE_BLKTRACE
718 extern int is_blktrace(const char *);
719 extern int load_blktrace(struct thread_data *, const char *);
723 * Mark unused variables passed to ops functions as unused, to silence gcc
725 #define fio_unused __attribute((__unused__))
726 #define fio_init __attribute__((constructor))
727 #define fio_exit __attribute__((destructor))
729 #define for_each_td(td, i) \
730 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
731 #define for_each_file(td, f, i) \
732 if ((td)->files_index) \
733 for ((i) = 0, (f) = (td)->files[0]; \
734 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
737 #define fio_assert(td, cond) do { \
740 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
741 (td)->runstate = TD_EXITED; \
742 (td)->error = EFAULT; \
747 static inline void fio_file_reset(struct fio_file *f)
749 f->last_free_lookup = 0;
750 f->last_pos = f->file_offset;
752 memset(f->file_map, 0, f->num_maps * sizeof(int));
755 static inline void clear_error(struct thread_data *td)
758 td->verror[0] = '\0';
762 static inline void dprint_io_u(struct io_u *io_u, const char *p)
764 struct fio_file *f = io_u->file;
766 dprint(FD_IO, "%s: io_u %p: off=%llu/len=%lu/ddir=%d", p, io_u,
767 (unsigned long long) io_u->offset,
768 io_u->buflen, io_u->ddir);
769 if (fio_debug & (1 << FD_IO)) {
771 log_info("/%s", f->file_name);
777 #define dprint_io_u(io_u, p)
780 static inline int fio_fill_issue_time(struct thread_data *td)
782 if (td->o.read_iolog_file ||
783 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)