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 */
110 * How many depth levels to log
112 #define FIO_IO_U_MAP_NR 8
113 #define FIO_IO_U_LAT_U_NR 10
114 #define FIO_IO_U_LAT_M_NR 12
125 struct io_log *slat_log;
126 struct io_log *clat_log;
127 struct io_log *bw_log;
130 * bandwidth and latency stats
132 struct io_stat clat_stat[2]; /* completion latency */
133 struct io_stat slat_stat[2]; /* submission latency */
134 struct io_stat bw_stat[2]; /* bandwidth stats */
136 unsigned long long stat_io_bytes[2];
137 struct timeval stat_sample_time[2];
140 * fio system usage accounting
142 struct rusage ru_start;
143 struct rusage ru_end;
144 unsigned long usr_time;
145 unsigned long sys_time;
147 unsigned long minf, majf;
150 * IO depth and latency stats
152 unsigned int io_u_map[FIO_IO_U_MAP_NR];
153 unsigned int io_u_submit[FIO_IO_U_MAP_NR];
154 unsigned int io_u_complete[FIO_IO_U_MAP_NR];
155 unsigned int io_u_lat_u[FIO_IO_U_LAT_U_NR];
156 unsigned int io_u_lat_m[FIO_IO_U_LAT_M_NR];
157 unsigned long total_io_u[2];
158 unsigned long short_io_u[2];
159 unsigned long total_submit;
160 unsigned long total_complete;
162 unsigned long long io_bytes[2];
163 unsigned long runtime[2];
164 unsigned long total_run_time;
172 struct thread_options {
180 enum td_ddir td_ddir;
181 unsigned int ddir_nr;
182 unsigned int iodepth;
183 unsigned int iodepth_low;
184 unsigned int iodepth_batch;
185 unsigned int iodepth_batch_complete;
187 unsigned long long size;
188 unsigned int fill_device;
189 unsigned long long file_size_low;
190 unsigned long long file_size_high;
191 unsigned long long start_offset;
195 unsigned int min_bs[2];
196 unsigned int max_bs[2];
197 struct bssplit *bssplit[2];
198 unsigned int bssplit_nr[2];
200 unsigned int nr_files;
201 unsigned int open_files;
202 enum file_lock_mode file_lock_mode;
203 unsigned int lockfile_batch;
205 unsigned int odirect;
206 unsigned int invalidate_cache;
207 unsigned int create_serialize;
208 unsigned int create_fsync;
209 unsigned int create_on_open;
210 unsigned int end_fsync;
211 unsigned int pre_read;
212 unsigned int sync_io;
214 unsigned int do_verify;
215 unsigned int verifysort;
216 unsigned int verify_interval;
217 unsigned int verify_offset;
218 unsigned int verify_pattern;
219 unsigned int verify_pattern_bytes;
220 unsigned int verify_fatal;
221 unsigned int use_thread;
223 unsigned int do_disk_util;
224 unsigned int override_sync;
225 unsigned int rand_repeatable;
226 unsigned int write_lat_log;
227 unsigned int write_bw_log;
228 unsigned int norandommap;
229 unsigned int softrandommap;
230 unsigned int bs_unaligned;
231 unsigned int fsync_on_close;
233 unsigned int hugepage_size;
234 unsigned int rw_min_bs;
235 unsigned int thinktime;
236 unsigned int thinktime_spin;
237 unsigned int thinktime_blocks;
238 unsigned int fsync_blocks;
239 unsigned int start_delay;
240 unsigned long long timeout;
241 unsigned long long ramp_time;
242 unsigned int overwrite;
243 unsigned int bw_avg_time;
245 unsigned long long zone_size;
246 unsigned long long zone_skip;
247 enum fio_memtype mem_type;
249 unsigned int stonewall;
250 unsigned int new_group;
251 unsigned int numjobs;
252 os_cpu_mask_t cpumask;
253 unsigned int cpumask_set;
255 unsigned int rwmixcycle;
256 unsigned int rwmix[2];
258 unsigned int file_service_type;
259 unsigned int group_reporting;
260 unsigned int fadvise_hint;
261 unsigned int zero_buffers;
262 unsigned int refill_buffers;
263 unsigned int time_based;
264 unsigned int disable_clat;
265 unsigned int disable_slat;
266 unsigned int disable_bw;
267 unsigned int gtod_reduce;
268 unsigned int gtod_cpu;
269 unsigned int gtod_offload;
271 char *read_iolog_file;
272 char *write_iolog_file;
277 * Pre-run and post-run shell
283 unsigned int ratemin;
284 unsigned int ratecycle;
285 unsigned int rate_iops;
286 unsigned int rate_iops_min;
291 * CPU "io" cycle burner
293 unsigned int cpuload;
294 unsigned int cpucycle;
297 #define FIO_VERROR_SIZE 128
300 * This describes a single thread/process executing a fio job.
303 struct thread_options o;
304 char verror[FIO_VERROR_SIZE];
308 struct thread_stat ts;
309 struct fio_file **files;
310 unsigned int files_size;
311 unsigned int files_index;
312 unsigned int nr_open_files;
313 unsigned int nr_done_files;
314 unsigned int nr_normal_files;
316 unsigned int next_file;
317 os_random_state_t next_file_state;
323 size_t orig_buffer_size;
324 volatile int terminate;
325 volatile int runstate;
327 unsigned int ioprio_set;
328 unsigned int last_was_sync;
338 unsigned long rand_seeds[6];
340 os_random_state_t bsrange_state;
341 os_random_state_t verify_state;
346 * IO engine hooks, contains everything needed to submit an io_u
347 * to any of the available IO engines.
349 struct ioengine_ops *io_ops;
352 * Current IO depth and list of free and busy io_u's.
354 unsigned int cur_depth;
355 unsigned int io_u_queued;
356 struct flist_head io_u_freelist;
357 struct flist_head io_u_busylist;
358 struct flist_head io_u_requeues;
363 unsigned long rate_usec_cycle;
364 long rate_pending_usleep;
365 unsigned long rate_bytes;
366 unsigned long rate_blocks;
367 struct timeval lastrate;
369 unsigned long long total_io_size;
371 unsigned long io_issues[2];
372 unsigned long long io_blocks[2];
373 unsigned long long io_bytes[2];
374 unsigned long long io_skip_bytes;
375 unsigned long long this_io_bytes[2];
376 unsigned long long zone_bytes;
377 struct fio_mutex *mutex;
380 * State for random io, a bitmap of blocks done vs not done
382 os_random_state_t random_state;
384 struct timeval start; /* start of this loop */
385 struct timeval epoch; /* time job was started */
386 struct timeval rw_end[2];
387 struct timeval last_issue;
388 struct timeval tv_cache;
389 unsigned int tv_cache_nr;
390 unsigned int tv_cache_mask;
391 unsigned int rw_end_set[2];
392 unsigned int ramp_time_over;
395 * read/write mixed workload state
397 os_random_state_t rwmix_state;
398 unsigned long rwmix_issues;
399 enum fio_ddir rwmix_ddir;
400 unsigned int ddir_nr;
403 * IO history logs for verification. We use a tree for sorting,
404 * if we are overwriting. Otherwise just use a fifo.
406 struct rb_root io_hist_tree;
407 struct flist_head io_hist_list;
412 struct flist_head io_log_list;
415 * for fileservice, how often to switch to a new file
417 unsigned int file_service_nr;
418 unsigned int file_service_left;
419 struct fio_file *file_service_file;
422 * For generating file sizes
424 os_random_state_t file_size_state;
428 * roundrobin available files, or choose one at random, or do each one
432 FIO_FSERVICE_RANDOM = 1,
434 FIO_FSERVICE_SEQ = 3,
438 * when should interactive ETA output be generated
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;
463 extern int nr_process, nr_thread;
466 extern int terse_output;
467 extern int temp_stall_ts;
468 extern unsigned long long mlock_size;
469 extern unsigned long page_mask, page_size;
470 extern int read_only;
471 extern int eta_print;
472 extern unsigned long done_secs;
473 extern char *job_section;
474 extern int fio_gtod_offload;
475 extern int fio_gtod_cpu;
477 extern struct thread_data *threads;
479 #define td_read(td) ((td)->o.td_ddir & TD_DDIR_READ)
480 #define td_write(td) ((td)->o.td_ddir & TD_DDIR_WRITE)
481 #define td_rw(td) (((td)->o.td_ddir & TD_DDIR_RW) == TD_DDIR_RW)
482 #define td_random(td) ((td)->o.td_ddir & TD_DDIR_RAND)
483 #define file_randommap(td, f) (!(td)->o.norandommap && (f)->file_map)
485 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
487 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
490 #define BLOCKS_PER_MAP (8 * sizeof(int))
491 #define TO_MAP_BLOCK(f, b) (b)
492 #define RAND_MAP_IDX(f, b) (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
493 #define RAND_MAP_BIT(f, b) (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
495 #define MAX_JOBS (1024)
497 static inline int should_fsync(struct thread_data *td)
499 if (td->last_was_sync)
503 if (td_write(td) || td_rw(td) || td->o.override_sync)
510 * Disk utils as read in /sys/block/<dev>/stat
512 struct disk_util_stat {
515 unsigned long long sectors[2];
518 unsigned time_in_queue;
522 * Per-device disk util management
525 struct flist_head list;
526 /* If this disk is a slave, hook it into the master's
527 * list using this head.
529 struct flist_head slavelist;
536 struct disk_util_stat dus;
537 struct disk_util_stat last_dus;
539 /* For software raids, this entry maintains pointers to the
540 * entries for the slave devices. The disk_util entries for
541 * the slaves devices should primarily be maintained through
542 * the disk_list list, i.e. for memory allocation and
543 * de-allocation, etc. Whereas this list should be used only
544 * for aggregating a software RAID's disk util figures.
546 struct flist_head slaves;
551 struct fio_mutex *lock;
555 static inline void disk_util_inc(struct disk_util *du)
558 fio_mutex_down(du->lock);
560 fio_mutex_up(du->lock);
564 static inline void disk_util_dec(struct disk_util *du)
567 fio_mutex_down(du->lock);
569 fio_mutex_up(du->lock);
573 #define DISK_UTIL_MSEC (250)
585 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
586 extern void log_io_u(struct thread_data *, struct io_u *);
587 extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
588 extern int __must_check init_iolog(struct thread_data *td);
589 extern void log_io_piece(struct thread_data *, struct io_u *);
590 extern void queue_io_piece(struct thread_data *, struct io_piece *);
591 extern void prune_io_piece_log(struct thread_data *);
592 extern void write_iolog_close(struct thread_data *);
597 extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long,
599 extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long,
601 extern void add_bw_sample(struct thread_data *, enum fio_ddir, unsigned int,
603 extern void show_run_stats(void);
604 extern void init_disk_util(struct thread_data *);
605 extern void update_rusage_stat(struct thread_data *);
606 extern void update_io_ticks(void);
607 extern void setup_log(struct io_log **);
608 extern void finish_log(struct thread_data *, struct io_log *, const char *);
609 extern void finish_log_named(struct thread_data *, struct io_log *, const char *, const char *);
610 extern void __finish_log(struct io_log *, const char *);
611 extern struct io_log *agg_io_log[2];
612 extern int write_bw_log;
613 extern void add_agg_sample(unsigned long, enum fio_ddir, unsigned int);
618 extern unsigned long long utime_since(struct timeval *, struct timeval *);
619 extern unsigned long long utime_since_now(struct timeval *);
620 extern unsigned long mtime_since(struct timeval *, struct timeval *);
621 extern unsigned long mtime_since_now(struct timeval *);
622 extern unsigned long time_since_now(struct timeval *);
623 extern unsigned long mtime_since_genesis(void);
624 extern void usec_spin(unsigned int);
625 extern void usec_sleep(struct thread_data *, unsigned long);
626 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
627 extern void fill_start_time(struct timeval *);
628 extern void fio_gettime(struct timeval *, void *);
629 extern void fio_gtod_init(void);
630 extern void fio_gtod_update(void);
631 extern void set_genesis_time(void);
632 extern int ramp_time_over(struct thread_data *);
633 extern int in_ramp_time(struct thread_data *);
636 * Init/option functions
638 extern int __must_check parse_options(int, char **);
639 extern int fio_options_parse(struct thread_data *, char **, int);
640 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
641 extern void fio_fill_default_options(struct thread_data *);
642 extern int fio_show_option_help(const char *);
643 extern void fio_options_dup_and_init(struct option *);
644 extern void options_mem_dupe(struct thread_data *);
645 extern void options_mem_free(struct thread_data *);
646 extern void td_fill_rand_seeds(struct thread_data *);
647 #define FIO_GETOPT_JOB 0x89988998
648 #define FIO_NR_OPTIONS 128
653 extern void print_thread_status(void);
654 extern void print_status_init(int);
659 #ifdef FIO_HAVE_DISK_UTIL
660 extern void show_disk_util(void);
661 extern void init_disk_util(struct thread_data *);
662 extern void update_io_ticks(void);
664 #define show_disk_util()
665 #define init_disk_util(td)
666 #define update_io_ticks()
670 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
671 * will never back again. It may cycle between running/verififying/fsyncing.
672 * Once the thread reaches TD_EXITED, it is just waiting for the core to
688 extern void td_set_runstate(struct thread_data *, int);
693 extern int __must_check fio_pin_memory(void);
694 extern void fio_unpin_memory(void);
695 extern int __must_check allocate_io_mem(struct thread_data *);
696 extern void free_io_mem(struct thread_data *);
699 * Reset stats after ramp time completes
701 extern void reset_all_stats(struct thread_data *);
706 #ifdef FIO_HAVE_BLKTRACE
707 extern int is_blktrace(const char *);
708 extern int load_blktrace(struct thread_data *, const char *);
712 * Mark unused variables passed to ops functions as unused, to silence gcc
714 #define fio_unused __attribute((__unused__))
715 #define fio_init __attribute__((constructor))
716 #define fio_exit __attribute__((destructor))
718 #define for_each_td(td, i) \
719 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
720 #define for_each_file(td, f, i) \
721 if ((td)->files_index) \
722 for ((i) = 0, (f) = (td)->files[0]; \
723 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
726 #define fio_assert(td, cond) do { \
729 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
730 (td)->runstate = TD_EXITED; \
731 (td)->error = EFAULT; \
736 static inline void fio_file_reset(struct fio_file *f)
738 f->last_free_lookup = 0;
739 f->last_pos = f->file_offset;
741 memset(f->file_map, 0, f->num_maps * sizeof(int));
744 static inline void clear_error(struct thread_data *td)
747 td->verror[0] = '\0';
751 static inline void dprint_io_u(struct io_u *io_u, const char *p)
753 struct fio_file *f = io_u->file;
755 dprint(FD_IO, "%s: io_u %p: off=%llu/len=%lu/ddir=%d", p, io_u,
756 (unsigned long long) io_u->offset,
757 io_u->buflen, io_u->ddir);
758 if (fio_debug & (1 << FD_IO)) {
760 log_info("/%s", f->file_name);
766 #define dprint_io_u(io_u, p)
769 static inline int fio_fill_issue_time(struct thread_data *td)
771 if (td->o.read_iolog_file ||
772 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)