8 #include <sys/resource.h>
19 #include "compiler/compiler.h"
23 #include "arch/arch.h"
36 #include "lib/getopt.h"
45 #ifdef FIO_HAVE_SOLARISAIO
46 #include <sys/asynch.h>
50 * What type of allocation to use for io buffers
53 MEM_MALLOC = 0, /* ordinary malloc */
54 MEM_SHM, /* use shared memory segments */
55 MEM_SHMHUGE, /* use shared memory segments with huge pages */
56 MEM_MMAP, /* use anonynomous mmap */
57 MEM_MMAPHUGE, /* memory mapped huge file */
61 * offset generator types
73 struct thread_options {
84 unsigned int ddir_seq_nr;
87 unsigned int iodepth_low;
88 unsigned int iodepth_batch;
89 unsigned int iodepth_batch_complete;
91 unsigned long long size;
92 unsigned int size_percent;
93 unsigned int fill_device;
94 unsigned long long file_size_low;
95 unsigned long long file_size_high;
96 unsigned long long start_offset;
100 unsigned int min_bs[2];
101 unsigned int max_bs[2];
102 struct bssplit *bssplit[2];
103 unsigned int bssplit_nr[2];
105 unsigned int nr_files;
106 unsigned int open_files;
107 enum file_lock_mode file_lock_mode;
108 unsigned int lockfile_batch;
110 unsigned int odirect;
111 unsigned int invalidate_cache;
112 unsigned int create_serialize;
113 unsigned int create_fsync;
114 unsigned int create_on_open;
115 unsigned int end_fsync;
116 unsigned int pre_read;
117 unsigned int sync_io;
119 unsigned int do_verify;
120 unsigned int verifysort;
121 unsigned int verify_interval;
122 unsigned int verify_offset;
123 char verify_pattern[MAX_PATTERN_SIZE];
124 unsigned int verify_pattern_bytes;
125 unsigned int verify_fatal;
126 unsigned int verify_dump;
127 unsigned int verify_async;
128 unsigned long long verify_backlog;
129 unsigned int verify_batch;
130 unsigned int use_thread;
132 unsigned int do_disk_util;
133 unsigned int override_sync;
134 unsigned int rand_repeatable;
135 unsigned int use_os_rand;
136 unsigned int write_lat_log;
137 unsigned int write_bw_log;
138 unsigned int write_iops_log;
139 unsigned int norandommap;
140 unsigned int softrandommap;
141 unsigned int bs_unaligned;
142 unsigned int fsync_on_close;
144 unsigned int hugepage_size;
145 unsigned int rw_min_bs;
146 unsigned int thinktime;
147 unsigned int thinktime_spin;
148 unsigned int thinktime_blocks;
149 unsigned int fsync_blocks;
150 unsigned int fdatasync_blocks;
151 unsigned int barrier_blocks;
152 unsigned long long start_delay;
153 unsigned long long timeout;
154 unsigned long long ramp_time;
155 unsigned int overwrite;
156 unsigned int bw_avg_time;
157 unsigned int iops_avg_time;
159 unsigned long long zone_size;
160 unsigned long long zone_skip;
161 enum fio_memtype mem_type;
162 unsigned int mem_align;
164 unsigned int stonewall;
165 unsigned int new_group;
166 unsigned int numjobs;
167 os_cpu_mask_t cpumask;
168 unsigned int cpumask_set;
169 os_cpu_mask_t verify_cpumask;
170 unsigned int verify_cpumask_set;
172 unsigned int rwmixcycle;
173 unsigned int rwmix[2];
175 unsigned int file_service_type;
176 unsigned int group_reporting;
177 unsigned int fadvise_hint;
178 enum fio_fallocate_mode fallocate_mode;
179 unsigned int zero_buffers;
180 unsigned int refill_buffers;
181 unsigned int scramble_buffers;
182 unsigned int time_based;
183 unsigned int disable_lat;
184 unsigned int disable_clat;
185 unsigned int disable_slat;
186 unsigned int disable_bw;
187 unsigned int gtod_reduce;
188 unsigned int gtod_cpu;
189 unsigned int gtod_offload;
190 enum fio_cs clocksource;
191 unsigned int no_stall;
192 unsigned int trim_percentage;
193 unsigned int trim_batch;
194 unsigned int trim_zero;
195 unsigned long long trim_backlog;
196 unsigned int clat_percentiles;
197 unsigned int overwrite_plist;
198 fio_fp64_t percentile_list[FIO_IO_U_LIST_MAX_LEN];
200 char *read_iolog_file;
201 char *write_iolog_file;
205 char *replay_redirect;
208 * Pre-run and post-run shell
213 unsigned int rate[2];
214 unsigned int ratemin[2];
215 unsigned int ratecycle;
216 unsigned int rate_iops[2];
217 unsigned int rate_iops_min[2];
222 * CPU "io" cycle burner
224 unsigned int cpuload;
225 unsigned int cpucycle;
230 unsigned int continue_on_error;
233 * Benchmark profile type
238 * blkio cgroup support
241 unsigned int cgroup_weight;
242 unsigned int cgroup_nodelete;
247 unsigned int sync_file_range;
251 * This describes a single thread/process executing a fio job.
254 struct thread_options o;
256 char verror[FIO_VERROR_SIZE];
260 struct thread_stat ts;
262 struct io_log *slat_log;
263 struct io_log *clat_log;
264 struct io_log *lat_log;
265 struct io_log *bw_log;
266 struct io_log *iops_log;
268 uint64_t stat_io_bytes[2];
269 struct timeval bw_sample_time;
271 uint64_t stat_io_blocks[2];
272 struct timeval iops_sample_time;
274 struct rusage ru_start;
275 struct rusage ru_end;
277 struct fio_file **files;
278 unsigned int files_size;
279 unsigned int files_index;
280 unsigned int nr_open_files;
281 unsigned int nr_done_files;
282 unsigned int nr_normal_files;
284 unsigned int next_file;
285 os_random_state_t next_file_state;
286 struct frand_state __next_file_state;
292 size_t orig_buffer_size;
293 volatile int terminate;
294 volatile int runstate;
296 unsigned int ioprio_set;
297 unsigned int last_was_sync;
298 enum fio_ddir last_ddir;
308 unsigned long rand_seeds[8];
311 os_random_state_t bsrange_state;
312 struct frand_state __bsrange_state;
315 os_random_state_t verify_state;
316 struct frand_state __verify_state;
319 os_random_state_t trim_state;
320 struct frand_state __trim_state;
323 struct frand_state buf_state;
325 unsigned int verify_batch;
326 unsigned int trim_batch;
331 * IO engine hooks, contains everything needed to submit an io_u
332 * to any of the available IO engines.
334 struct ioengine_ops *io_ops;
337 * Current IO depth and list of free and busy io_u's.
339 unsigned int cur_depth;
340 unsigned int io_u_queued;
341 struct flist_head io_u_freelist;
342 struct flist_head io_u_busylist;
343 struct flist_head io_u_requeues;
344 pthread_mutex_t io_u_lock;
345 pthread_cond_t free_cond;
348 * async verify offload
350 struct flist_head verify_list;
351 pthread_t *verify_threads;
352 unsigned int nr_verify_threads;
353 pthread_cond_t verify_cond;
354 int verify_thread_exit;
359 unsigned long long rate_bps[2];
360 long rate_pending_usleep[2];
361 unsigned long rate_bytes[2];
362 unsigned long rate_blocks[2];
363 struct timeval lastrate[2];
365 unsigned long long total_io_size;
366 unsigned long long fill_device_size;
368 unsigned long io_issues[2];
369 unsigned long long io_blocks[2];
370 unsigned long long this_io_blocks[2];
371 unsigned long long io_bytes[2];
372 unsigned long long io_skip_bytes;
373 unsigned long long this_io_bytes[2];
374 unsigned long long zone_bytes;
375 struct fio_mutex *mutex;
378 * State for random io, a bitmap of blocks done vs not done
381 os_random_state_t random_state;
382 struct frand_state __random_state;
385 struct timeval start; /* start of this loop */
386 struct timeval epoch; /* time job was started */
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 ramp_time_over;
394 * read/write mixed workload state
397 os_random_state_t rwmix_state;
398 struct frand_state __rwmix_state;
400 unsigned long rwmix_issues;
401 enum fio_ddir rwmix_ddir;
402 unsigned int ddir_seq_nr;
405 * IO history logs for verification. We use a tree for sorting,
406 * if we are overwriting. Otherwise just use a fifo.
408 struct rb_root io_hist_tree;
409 struct flist_head io_hist_list;
410 unsigned long io_hist_len;
415 struct flist_head io_log_list;
418 * For tracking/handling discards
420 struct flist_head trim_list;
421 unsigned long trim_entries;
424 * for fileservice, how often to switch to a new file
426 unsigned int file_service_nr;
427 unsigned int file_service_left;
428 struct fio_file *file_service_file;
430 unsigned int sync_file_range_nr;
433 * For generating file sizes
436 os_random_state_t file_size_state;
437 struct frand_state __file_size_state;
443 unsigned int total_err_count;
447 * Can be overloaded by profiles
449 struct prof_io_ops prof_io_ops;
454 * when should interactive ETA output be generated
462 #define __td_verror(td, err, msg, func) \
468 if (!(td)->first_error) \
469 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
473 #define td_clear_error(td) \
475 #define td_verror(td, err, func) \
476 __td_verror((td), (err), strerror((err)), (func))
477 #define td_vmsg(td, err, msg, func) \
478 __td_verror((td), (err), (msg), (func))
480 #define __fio_stringify_1(x) #x
481 #define __fio_stringify(x) __fio_stringify_1(x)
483 extern int exitall_on_terminate;
484 extern unsigned int thread_number;
485 extern unsigned int nr_process, nr_thread;
488 extern int terse_output;
489 extern int temp_stall_ts;
490 extern unsigned long long mlock_size;
491 extern unsigned long page_mask, page_size;
492 extern int read_only;
493 extern int eta_print;
494 extern unsigned long done_secs;
495 extern char *job_section;
496 extern int fio_gtod_offload;
497 extern int fio_gtod_cpu;
498 extern enum fio_cs fio_clock_source;
499 extern int warnings_fatal;
500 extern int terse_version;
501 extern int is_backend;
502 extern int nr_clients;
503 extern int log_syslog;
504 extern const char fio_version_string[];
505 extern const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN];
507 extern struct thread_data *threads;
509 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
511 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
514 #define BLOCKS_PER_MAP (8 * sizeof(unsigned long))
515 #define TO_MAP_BLOCK(f, b) (b)
516 #define RAND_MAP_IDX(f, b) (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
517 #define RAND_MAP_BIT(f, b) (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
519 #define REAL_MAX_JOBS 2048
521 #define td_non_fatal_error(e) ((e) == EIO || (e) == EILSEQ)
523 static inline void update_error_count(struct thread_data *td, int err)
525 td->total_err_count++;
526 if (td->total_err_count == 1)
527 td->first_error = err;
530 static inline int should_fsync(struct thread_data *td)
532 if (td->last_was_sync)
536 if (td_write(td) || td_rw(td) || td->o.override_sync)
543 * Init/option functions
545 extern int __must_check parse_options(int, char **);
546 extern int parse_jobs_ini(char *, int, int);
547 extern int parse_cmd_line(int, char **);
548 extern int exec_run(void);
549 extern void reset_fio_state(void);
550 extern int fio_options_parse(struct thread_data *, char **, int);
551 extern void fio_keywords_init(void);
552 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
553 extern int fio_cmd_ioengine_option_parse(struct thread_data *, const char *, char *);
554 extern void fio_fill_default_options(struct thread_data *);
555 extern int fio_show_option_help(const char *);
556 extern void fio_options_set_ioengine_opts(struct option *long_options, struct thread_data *td);
557 extern void fio_options_dup_and_init(struct option *);
558 extern void fio_options_mem_dupe(struct thread_data *);
559 extern void options_mem_dupe(void *data, struct fio_option *options);
560 extern void td_fill_rand_seeds(struct thread_data *);
561 extern void add_job_opts(const char **);
562 extern char *num2str(unsigned long, int, int, int);
563 extern int ioengine_load(struct thread_data *);
565 #define FIO_GETOPT_JOB 0x89000000
566 #define FIO_GETOPT_IOENGINE 0x98000000
567 #define FIO_NR_OPTIONS (FIO_MAX_OPTS + 128)
572 extern void print_thread_status(void);
573 extern void print_status_init(int);
576 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
577 * will never back again. It may cycle between running/verififying/fsyncing.
578 * Once the thread reaches TD_EXITED, it is just waiting for the core to
594 extern void td_set_runstate(struct thread_data *, int);
595 #define TERMINATE_ALL (-1)
596 extern void fio_terminate_threads(int);
601 extern int __must_check fio_pin_memory(void);
602 extern void fio_unpin_memory(void);
603 extern int __must_check allocate_io_mem(struct thread_data *);
604 extern void free_io_mem(struct thread_data *);
607 * Reset stats after ramp time completes
609 extern void reset_all_stats(struct thread_data *);
614 #ifdef FIO_HAVE_BLKTRACE
615 extern int is_blktrace(const char *);
616 extern int load_blktrace(struct thread_data *, const char *);
620 * Mark unused variables passed to ops functions as unused, to silence gcc
622 #define fio_unused __attribute((__unused__))
623 #define fio_init __attribute__((constructor))
624 #define fio_exit __attribute__((destructor))
626 #define for_each_td(td, i) \
627 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
628 #define for_each_file(td, f, i) \
629 if ((td)->files_index) \
630 for ((i) = 0, (f) = (td)->files[0]; \
631 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
634 #define fio_assert(td, cond) do { \
637 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
638 td_set_runstate((td), TD_EXITED); \
639 (td)->error = EFAULT; \
644 static inline int fio_fill_issue_time(struct thread_data *td)
646 if (td->o.read_iolog_file ||
647 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
653 static inline int __should_check_rate(struct thread_data *td,
656 struct thread_options *o = &td->o;
659 * If some rate setting was given, we need to check it
661 if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
662 o->rate_iops_min[ddir])
668 static inline int should_check_rate(struct thread_data *td,
669 unsigned long *bytes_done)
674 ret |= __should_check_rate(td, 0);
676 ret |= __should_check_rate(td, 1);
681 static inline int is_power_of_2(unsigned int val)
683 return (val != 0 && ((val & (val - 1)) == 0));
687 * We currently only need to do locking if we have verifier threads
688 * accessing our internal structures too
690 static inline void td_io_u_lock(struct thread_data *td)
692 if (td->o.verify_async)
693 pthread_mutex_lock(&td->io_u_lock);
696 static inline void td_io_u_unlock(struct thread_data *td)
698 if (td->o.verify_async)
699 pthread_mutex_unlock(&td->io_u_lock);
702 static inline void td_io_u_free_notify(struct thread_data *td)
704 if (td->o.verify_async)
705 pthread_cond_signal(&td->free_cond);
708 extern const char *fio_get_arch_string(int);
709 extern const char *fio_get_os_string(int);