8 #include <sys/resource.h>
19 #include "compiler/compiler.h"
23 #include "arch/arch.h"
37 #include "lib/getopt.h"
48 #ifdef FIO_HAVE_SOLARISAIO
49 #include <sys/asynch.h>
52 #ifdef FIO_HAVE_LIBNUMA
53 #include <linux/mempolicy.h>
57 * "local" is pseudo-policy
59 #define MPOL_LOCAL MPOL_MAX
63 * What type of allocation to use for io buffers
66 MEM_MALLOC = 0, /* ordinary malloc */
67 MEM_SHM, /* use shared memory segments */
68 MEM_SHMHUGE, /* use shared memory segments with huge pages */
69 MEM_MMAP, /* use anonynomous mmap */
70 MEM_MMAPHUGE, /* memory mapped huge file */
74 * offset generator types
82 * What type of errors to continue on when continue_on_error is used
85 ERROR_TYPE_READ_BIT = 0,
86 ERROR_TYPE_WRITE_BIT = 1,
87 ERROR_TYPE_VERIFY_BIT = 2,
93 ERROR_TYPE_READ = 1 << ERROR_TYPE_READ_BIT,
94 ERROR_TYPE_WRITE = 1 << ERROR_TYPE_WRITE_BIT,
95 ERROR_TYPE_VERIFY = 1 << ERROR_TYPE_VERIFY_BIT,
96 ERROR_TYPE_ANY = 0xffff,
104 struct thread_options {
112 enum td_ddir td_ddir;
114 unsigned int kb_base;
115 unsigned int ddir_seq_nr;
117 unsigned int iodepth;
118 unsigned int iodepth_low;
119 unsigned int iodepth_batch;
120 unsigned int iodepth_batch_complete;
122 unsigned long long size;
123 unsigned int size_percent;
124 unsigned int fill_device;
125 unsigned long long file_size_low;
126 unsigned long long file_size_high;
127 unsigned long long start_offset;
129 unsigned int bs[DDIR_RWDIR_CNT];
130 unsigned int ba[DDIR_RWDIR_CNT];
131 unsigned int min_bs[DDIR_RWDIR_CNT];
132 unsigned int max_bs[DDIR_RWDIR_CNT];
133 struct bssplit *bssplit[DDIR_RWDIR_CNT];
134 unsigned int bssplit_nr[DDIR_RWDIR_CNT];
136 int *ignore_error[ERROR_TYPE_CNT];
137 unsigned int ignore_error_nr[ERROR_TYPE_CNT];
138 unsigned int error_dump;
140 unsigned int nr_files;
141 unsigned int open_files;
142 enum file_lock_mode file_lock_mode;
143 unsigned int lockfile_batch;
145 unsigned int odirect;
146 unsigned int invalidate_cache;
147 unsigned int create_serialize;
148 unsigned int create_fsync;
149 unsigned int create_on_open;
150 unsigned int create_only;
151 unsigned int end_fsync;
152 unsigned int pre_read;
153 unsigned int sync_io;
155 unsigned int do_verify;
156 unsigned int verifysort;
157 unsigned int verify_interval;
158 unsigned int verify_offset;
159 char verify_pattern[MAX_PATTERN_SIZE];
160 unsigned int verify_pattern_bytes;
161 unsigned int verify_fatal;
162 unsigned int verify_dump;
163 unsigned int verify_async;
164 unsigned long long verify_backlog;
165 unsigned int verify_batch;
166 unsigned int use_thread;
168 unsigned int do_disk_util;
169 unsigned int override_sync;
170 unsigned int rand_repeatable;
171 unsigned int use_os_rand;
172 unsigned int write_lat_log;
173 unsigned int write_bw_log;
174 unsigned int write_iops_log;
175 unsigned int log_avg_msec;
176 unsigned int norandommap;
177 unsigned int softrandommap;
178 unsigned int bs_unaligned;
179 unsigned int fsync_on_close;
181 unsigned int random_distribution;
185 unsigned int hugepage_size;
186 unsigned int rw_min_bs;
187 unsigned int thinktime;
188 unsigned int thinktime_spin;
189 unsigned int thinktime_blocks;
190 unsigned int fsync_blocks;
191 unsigned int fdatasync_blocks;
192 unsigned int barrier_blocks;
193 unsigned long long start_delay;
194 unsigned long long timeout;
195 unsigned long long ramp_time;
196 unsigned int overwrite;
197 unsigned int bw_avg_time;
198 unsigned int iops_avg_time;
200 unsigned long long zone_range;
201 unsigned long long zone_size;
202 unsigned long long zone_skip;
203 enum fio_memtype mem_type;
204 unsigned int mem_align;
206 unsigned int max_latency;
208 unsigned int stonewall;
209 unsigned int new_group;
210 unsigned int numjobs;
211 os_cpu_mask_t cpumask;
212 unsigned int cpumask_set;
213 os_cpu_mask_t verify_cpumask;
214 unsigned int verify_cpumask_set;
215 #ifdef FIO_HAVE_LIBNUMA
216 struct bitmask *numa_cpunodesmask;
217 unsigned int numa_cpumask_set;
218 unsigned short numa_mem_mode;
219 unsigned int numa_mem_prefer_node;
220 struct bitmask *numa_memnodesmask;
221 unsigned int numa_memmask_set;
224 unsigned int rwmixcycle;
225 unsigned int rwmix[2];
227 unsigned int file_service_type;
228 unsigned int group_reporting;
229 unsigned int fadvise_hint;
230 enum fio_fallocate_mode fallocate_mode;
231 unsigned int zero_buffers;
232 unsigned int refill_buffers;
233 unsigned int scramble_buffers;
234 unsigned int compress_percentage;
235 unsigned int compress_chunk;
236 unsigned int time_based;
237 unsigned int disable_lat;
238 unsigned int disable_clat;
239 unsigned int disable_slat;
240 unsigned int disable_bw;
241 unsigned int gtod_reduce;
242 unsigned int gtod_cpu;
243 unsigned int gtod_offload;
244 enum fio_cs clocksource;
245 unsigned int no_stall;
246 unsigned int trim_percentage;
247 unsigned int trim_batch;
248 unsigned int trim_zero;
249 unsigned long long trim_backlog;
250 unsigned int clat_percentiles;
251 unsigned int overwrite_plist;
252 fio_fp64_t percentile_list[FIO_IO_U_LIST_MAX_LEN];
254 char *read_iolog_file;
255 char *write_iolog_file;
259 char *replay_redirect;
262 * Pre-run and post-run shell
267 unsigned int rate[DDIR_RWDIR_CNT];
268 unsigned int ratemin[DDIR_RWDIR_CNT];
269 unsigned int ratecycle;
270 unsigned int rate_iops[DDIR_RWDIR_CNT];
271 unsigned int rate_iops_min[DDIR_RWDIR_CNT];
276 * CPU "io" cycle burner
278 unsigned int cpuload;
279 unsigned int cpucycle;
284 enum error_type continue_on_error;
287 * Benchmark profile type
292 * blkio cgroup support
295 unsigned int cgroup_weight;
296 unsigned int cgroup_nodelete;
304 unsigned int flow_sleep;
306 unsigned long long offset_increment;
308 unsigned int sync_file_range;
312 * This describes a single thread/process executing a fio job.
315 struct thread_options o;
317 char verror[FIO_VERROR_SIZE];
321 struct thread_stat ts;
323 struct io_log *slat_log;
324 struct io_log *clat_log;
325 struct io_log *lat_log;
326 struct io_log *bw_log;
327 struct io_log *iops_log;
329 uint64_t stat_io_bytes[DDIR_RWDIR_CNT];
330 struct timeval bw_sample_time;
332 uint64_t stat_io_blocks[DDIR_RWDIR_CNT];
333 struct timeval iops_sample_time;
335 struct rusage ru_start;
336 struct rusage ru_end;
338 struct fio_file **files;
339 unsigned int files_size;
340 unsigned int files_index;
341 unsigned int nr_open_files;
342 unsigned int nr_done_files;
343 unsigned int nr_normal_files;
345 unsigned int next_file;
346 os_random_state_t next_file_state;
347 struct frand_state __next_file_state;
354 size_t orig_buffer_size;
355 volatile int terminate;
356 volatile int runstate;
358 unsigned int ioprio_set;
359 unsigned int last_was_sync;
360 enum fio_ddir last_ddir;
370 unsigned long rand_seeds[8];
373 os_random_state_t bsrange_state;
374 struct frand_state __bsrange_state;
377 os_random_state_t verify_state;
378 struct frand_state __verify_state;
381 os_random_state_t trim_state;
382 struct frand_state __trim_state;
385 struct frand_state buf_state;
387 unsigned int verify_batch;
388 unsigned int trim_batch;
393 * IO engine hooks, contains everything needed to submit an io_u
394 * to any of the available IO engines.
396 struct ioengine_ops *io_ops;
399 * Queue depth of io_u's that fio MIGHT do
401 unsigned int cur_depth;
404 * io_u's about to be committed
406 unsigned int io_u_queued;
409 * io_u's submitted but not completed yet
411 unsigned int io_u_in_flight;
414 * List of free and busy io_u's
416 struct flist_head io_u_freelist;
417 struct flist_head io_u_busylist;
418 struct flist_head io_u_requeues;
419 pthread_mutex_t io_u_lock;
420 pthread_cond_t free_cond;
423 * async verify offload
425 struct flist_head verify_list;
426 pthread_t *verify_threads;
427 unsigned int nr_verify_threads;
428 pthread_cond_t verify_cond;
429 int verify_thread_exit;
434 unsigned long long rate_bps[DDIR_RWDIR_CNT];
435 long rate_pending_usleep[DDIR_RWDIR_CNT];
436 unsigned long rate_bytes[DDIR_RWDIR_CNT];
437 unsigned long rate_blocks[DDIR_RWDIR_CNT];
438 struct timeval lastrate[DDIR_RWDIR_CNT];
440 unsigned long long total_io_size;
441 unsigned long long fill_device_size;
443 unsigned long io_issues[DDIR_RWDIR_CNT];
444 unsigned long long io_blocks[DDIR_RWDIR_CNT];
445 unsigned long long this_io_blocks[DDIR_RWDIR_CNT];
446 unsigned long long io_bytes[DDIR_RWDIR_CNT];
447 unsigned long long io_skip_bytes;
448 unsigned long long this_io_bytes[DDIR_RWDIR_CNT];
449 unsigned long long zone_bytes;
450 struct fio_mutex *mutex;
453 * State for random io, a bitmap of blocks done vs not done
456 os_random_state_t random_state;
457 struct frand_state __random_state;
461 * Used for zipf random distribution
463 struct zipf_state zipf;
465 struct timeval start; /* start of this loop */
466 struct timeval epoch; /* time job was started */
467 struct timeval last_issue;
468 struct timeval tv_cache;
469 unsigned int tv_cache_nr;
470 unsigned int tv_cache_mask;
471 unsigned int ramp_time_over;
474 * read/write mixed workload state
477 os_random_state_t rwmix_state;
478 struct frand_state __rwmix_state;
480 unsigned long rwmix_issues;
481 enum fio_ddir rwmix_ddir;
482 unsigned int ddir_seq_nr;
485 * IO history logs for verification. We use a tree for sorting,
486 * if we are overwriting. Otherwise just use a fifo.
488 struct rb_root io_hist_tree;
489 struct flist_head io_hist_list;
490 unsigned long io_hist_len;
495 struct flist_head io_log_list;
498 * For tracking/handling discards
500 struct flist_head trim_list;
501 unsigned long trim_entries;
504 * for fileservice, how often to switch to a new file
506 unsigned int file_service_nr;
507 unsigned int file_service_left;
508 struct fio_file *file_service_file;
510 unsigned int sync_file_range_nr;
513 * For generating file sizes
516 os_random_state_t file_size_state;
517 struct frand_state __file_size_state;
523 unsigned int total_err_count;
526 struct fio_flow *flow;
529 * Can be overloaded by profiles
531 struct prof_io_ops prof_io_ops;
536 * when should interactive ETA output be generated
544 #define __td_verror(td, err, msg, func) \
550 if (!(td)->first_error) \
551 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
555 #define td_clear_error(td) \
557 #define td_verror(td, err, func) \
558 __td_verror((td), (err), strerror((err)), (func))
559 #define td_vmsg(td, err, msg, func) \
560 __td_verror((td), (err), (msg), (func))
562 #define __fio_stringify_1(x) #x
563 #define __fio_stringify(x) __fio_stringify_1(x)
565 extern int exitall_on_terminate;
566 extern unsigned int thread_number;
567 extern unsigned int nr_process, nr_thread;
570 extern int output_format;
571 extern int temp_stall_ts;
572 extern unsigned long long mlock_size;
573 extern uintptr_t page_mask, page_size;
574 extern int read_only;
575 extern int eta_print;
576 extern unsigned long done_secs;
577 extern char *job_section;
578 extern int fio_gtod_offload;
579 extern int fio_gtod_cpu;
580 extern enum fio_cs fio_clock_source;
581 extern int warnings_fatal;
582 extern int terse_version;
583 extern int is_backend;
584 extern int nr_clients;
585 extern int log_syslog;
586 extern const char fio_version_string[];
587 extern const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN];
589 extern struct thread_data *threads;
591 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
593 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
596 #define BLOCKS_PER_MAP (8 * sizeof(unsigned long))
597 #define TO_MAP_BLOCK(f, b) (b)
598 #define RAND_MAP_IDX(f, b) (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
599 #define RAND_MAP_BIT(f, b) (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
601 #define REAL_MAX_JOBS 2048
603 static inline enum error_type td_error_type(enum fio_ddir ddir, int err)
606 return ERROR_TYPE_VERIFY_BIT;
607 if (ddir == DDIR_READ)
608 return ERROR_TYPE_READ_BIT;
609 return ERROR_TYPE_WRITE_BIT;
612 static int __NON_FATAL_ERR[] = {EIO, EILSEQ};
613 static inline int td_non_fatal_error(struct thread_data *td,
614 enum error_type_bit etype, int err)
617 if (!td->o.ignore_error[etype]) {
618 td->o.ignore_error[etype] = __NON_FATAL_ERR;
619 td->o.ignore_error_nr[etype] = sizeof(__NON_FATAL_ERR)
623 if (!(td->o.continue_on_error & (1 << etype)))
625 for (i = 0; i < td->o.ignore_error_nr[etype]; i++)
626 if (td->o.ignore_error[etype][i] == err)
631 static inline void update_error_count(struct thread_data *td, int err)
633 td->total_err_count++;
634 if (td->total_err_count == 1)
635 td->first_error = err;
638 static inline int should_fsync(struct thread_data *td)
640 if (td->last_was_sync)
644 if (td_write(td) || td_rw(td) || td->o.override_sync)
651 * Init/option functions
653 extern int __must_check parse_options(int, char **);
654 extern int parse_jobs_ini(char *, int, int);
655 extern int parse_cmd_line(int, char **);
656 extern int fio_backend(void);
657 extern void reset_fio_state(void);
658 extern void clear_io_state(struct thread_data *);
659 extern int fio_options_parse(struct thread_data *, char **, int);
660 extern void fio_keywords_init(void);
661 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
662 extern int fio_cmd_ioengine_option_parse(struct thread_data *, const char *, char *);
663 extern void fio_fill_default_options(struct thread_data *);
664 extern int fio_show_option_help(const char *);
665 extern void fio_options_set_ioengine_opts(struct option *long_options, struct thread_data *td);
666 extern void fio_options_dup_and_init(struct option *);
667 extern void fio_options_mem_dupe(struct thread_data *);
668 extern void options_mem_dupe(void *data, struct fio_option *options);
669 extern void td_fill_rand_seeds(struct thread_data *);
670 extern void add_job_opts(const char **);
671 extern char *num2str(unsigned long, int, int, int);
672 extern int ioengine_load(struct thread_data *);
674 #define FIO_GETOPT_JOB 0x89000000
675 #define FIO_GETOPT_IOENGINE 0x98000000
676 #define FIO_NR_OPTIONS (FIO_MAX_OPTS + 128)
681 extern void print_thread_status(void);
682 extern void print_status_init(int);
685 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
686 * will never back again. It may cycle between running/verififying/fsyncing.
687 * Once the thread reaches TD_EXITED, it is just waiting for the core to
703 extern void td_set_runstate(struct thread_data *, int);
704 #define TERMINATE_ALL (-1)
705 extern void fio_terminate_threads(int);
710 extern int __must_check fio_pin_memory(void);
711 extern void fio_unpin_memory(void);
712 extern int __must_check allocate_io_mem(struct thread_data *);
713 extern void free_io_mem(struct thread_data *);
716 * Reset stats after ramp time completes
718 extern void reset_all_stats(struct thread_data *);
723 #ifdef FIO_HAVE_BLKTRACE
724 extern int is_blktrace(const char *);
725 extern int load_blktrace(struct thread_data *, const char *);
729 * Mark unused variables passed to ops functions as unused, to silence gcc
731 #define fio_unused __attribute((__unused__))
732 #define fio_init __attribute__((constructor))
733 #define fio_exit __attribute__((destructor))
735 #define for_each_td(td, i) \
736 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
737 #define for_each_file(td, f, i) \
738 if ((td)->files_index) \
739 for ((i) = 0, (f) = (td)->files[0]; \
740 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
743 #define fio_assert(td, cond) do { \
746 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
747 td_set_runstate((td), TD_EXITED); \
748 (td)->error = EFAULT; \
753 static inline int fio_fill_issue_time(struct thread_data *td)
755 if (td->o.read_iolog_file ||
756 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
762 static inline int __should_check_rate(struct thread_data *td,
765 struct thread_options *o = &td->o;
768 * If some rate setting was given, we need to check it
770 if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
771 o->rate_iops_min[ddir])
777 static inline int should_check_rate(struct thread_data *td,
778 unsigned long *bytes_done)
782 if (bytes_done[DDIR_READ])
783 ret |= __should_check_rate(td, DDIR_READ);
784 if (bytes_done[DDIR_WRITE])
785 ret |= __should_check_rate(td, DDIR_WRITE);
786 if (bytes_done[DDIR_TRIM])
787 ret |= __should_check_rate(td, DDIR_TRIM);
792 static inline int is_power_of_2(unsigned int val)
794 return (val != 0 && ((val & (val - 1)) == 0));
798 * We currently only need to do locking if we have verifier threads
799 * accessing our internal structures too
801 static inline void td_io_u_lock(struct thread_data *td)
803 if (td->o.verify_async)
804 pthread_mutex_lock(&td->io_u_lock);
807 static inline void td_io_u_unlock(struct thread_data *td)
809 if (td->o.verify_async)
810 pthread_mutex_unlock(&td->io_u_lock);
813 static inline void td_io_u_free_notify(struct thread_data *td)
815 if (td->o.verify_async)
816 pthread_cond_signal(&td->free_cond);
819 extern const char *fio_get_arch_string(int);
820 extern const char *fio_get_os_string(int);
823 FIO_OUTPUT_TERSE = 0,
829 FIO_RAND_DIST_RANDOM = 0,
831 FIO_RAND_DIST_PARETO,