8 #include <sys/resource.h>
19 #include "compiler/compiler.h"
23 #include "arch/arch.h"
37 #include "lib/getopt.h"
46 #ifdef FIO_HAVE_SOLARISAIO
47 #include <sys/asynch.h>
51 * What type of allocation to use for io buffers
54 MEM_MALLOC = 0, /* ordinary malloc */
55 MEM_SHM, /* use shared memory segments */
56 MEM_SHMHUGE, /* use shared memory segments with huge pages */
57 MEM_MMAP, /* use anonynomous mmap */
58 MEM_MMAPHUGE, /* memory mapped huge file */
62 * offset generator types
70 * What type of errors to continue on when continue_on_error is used
74 ERROR_TYPE_READ = 1 << 0,
75 ERROR_TYPE_WRITE = 1 << 1,
76 ERROR_TYPE_VERIFY = 1 << 2,
77 ERROR_TYPE_ANY = 0xffff,
85 struct thread_options {
96 unsigned int ddir_seq_nr;
99 unsigned int iodepth_low;
100 unsigned int iodepth_batch;
101 unsigned int iodepth_batch_complete;
103 unsigned long long size;
104 unsigned int size_percent;
105 unsigned int fill_device;
106 unsigned long long file_size_low;
107 unsigned long long file_size_high;
108 unsigned long long start_offset;
112 unsigned int min_bs[2];
113 unsigned int max_bs[2];
114 struct bssplit *bssplit[2];
115 unsigned int bssplit_nr[2];
117 unsigned int nr_files;
118 unsigned int open_files;
119 enum file_lock_mode file_lock_mode;
120 unsigned int lockfile_batch;
122 unsigned int odirect;
123 unsigned int invalidate_cache;
124 unsigned int create_serialize;
125 unsigned int create_fsync;
126 unsigned int create_on_open;
127 unsigned int end_fsync;
128 unsigned int pre_read;
129 unsigned int sync_io;
131 unsigned int do_verify;
132 unsigned int verifysort;
133 unsigned int verify_interval;
134 unsigned int verify_offset;
135 char verify_pattern[MAX_PATTERN_SIZE];
136 unsigned int verify_pattern_bytes;
137 unsigned int verify_fatal;
138 unsigned int verify_dump;
139 unsigned int verify_async;
140 unsigned long long verify_backlog;
141 unsigned int verify_batch;
142 unsigned int use_thread;
144 unsigned int do_disk_util;
145 unsigned int override_sync;
146 unsigned int rand_repeatable;
147 unsigned int use_os_rand;
148 unsigned int write_lat_log;
149 unsigned int write_bw_log;
150 unsigned int write_iops_log;
151 unsigned int log_avg_msec;
152 unsigned int norandommap;
153 unsigned int softrandommap;
154 unsigned int bs_unaligned;
155 unsigned int fsync_on_close;
157 unsigned int hugepage_size;
158 unsigned int rw_min_bs;
159 unsigned int thinktime;
160 unsigned int thinktime_spin;
161 unsigned int thinktime_blocks;
162 unsigned int fsync_blocks;
163 unsigned int fdatasync_blocks;
164 unsigned int barrier_blocks;
165 unsigned long long start_delay;
166 unsigned long long timeout;
167 unsigned long long ramp_time;
168 unsigned int overwrite;
169 unsigned int bw_avg_time;
170 unsigned int iops_avg_time;
172 unsigned long long zone_range;
173 unsigned long long zone_size;
174 unsigned long long zone_skip;
175 enum fio_memtype mem_type;
176 unsigned int mem_align;
178 unsigned int stonewall;
179 unsigned int new_group;
180 unsigned int numjobs;
181 os_cpu_mask_t cpumask;
182 unsigned int cpumask_set;
183 os_cpu_mask_t verify_cpumask;
184 unsigned int verify_cpumask_set;
186 unsigned int rwmixcycle;
187 unsigned int rwmix[2];
189 unsigned int file_service_type;
190 unsigned int group_reporting;
191 unsigned int fadvise_hint;
192 enum fio_fallocate_mode fallocate_mode;
193 unsigned int zero_buffers;
194 unsigned int refill_buffers;
195 unsigned int scramble_buffers;
196 unsigned int time_based;
197 unsigned int disable_lat;
198 unsigned int disable_clat;
199 unsigned int disable_slat;
200 unsigned int disable_bw;
201 unsigned int gtod_reduce;
202 unsigned int gtod_cpu;
203 unsigned int gtod_offload;
204 enum fio_cs clocksource;
205 unsigned int no_stall;
206 unsigned int trim_percentage;
207 unsigned int trim_batch;
208 unsigned int trim_zero;
209 unsigned long long trim_backlog;
210 unsigned int clat_percentiles;
211 unsigned int overwrite_plist;
212 fio_fp64_t percentile_list[FIO_IO_U_LIST_MAX_LEN];
214 char *read_iolog_file;
215 char *write_iolog_file;
219 char *replay_redirect;
222 * Pre-run and post-run shell
227 unsigned int rate[2];
228 unsigned int ratemin[2];
229 unsigned int ratecycle;
230 unsigned int rate_iops[2];
231 unsigned int rate_iops_min[2];
236 * CPU "io" cycle burner
238 unsigned int cpuload;
239 unsigned int cpucycle;
244 enum error_type continue_on_error;
247 * Benchmark profile type
252 * blkio cgroup support
255 unsigned int cgroup_weight;
256 unsigned int cgroup_nodelete;
261 unsigned int sync_file_range;
265 * This describes a single thread/process executing a fio job.
268 struct thread_options o;
270 char verror[FIO_VERROR_SIZE];
274 struct thread_stat ts;
276 struct io_log *slat_log;
277 struct io_log *clat_log;
278 struct io_log *lat_log;
279 struct io_log *bw_log;
280 struct io_log *iops_log;
282 uint64_t stat_io_bytes[2];
283 struct timeval bw_sample_time;
285 uint64_t stat_io_blocks[2];
286 struct timeval iops_sample_time;
288 struct rusage ru_start;
289 struct rusage ru_end;
291 struct fio_file **files;
292 unsigned int files_size;
293 unsigned int files_index;
294 unsigned int nr_open_files;
295 unsigned int nr_done_files;
296 unsigned int nr_normal_files;
298 unsigned int next_file;
299 os_random_state_t next_file_state;
300 struct frand_state __next_file_state;
306 size_t orig_buffer_size;
307 volatile int terminate;
308 volatile int runstate;
310 unsigned int ioprio_set;
311 unsigned int last_was_sync;
312 enum fio_ddir last_ddir;
322 unsigned long rand_seeds[8];
325 os_random_state_t bsrange_state;
326 struct frand_state __bsrange_state;
329 os_random_state_t verify_state;
330 struct frand_state __verify_state;
333 os_random_state_t trim_state;
334 struct frand_state __trim_state;
337 struct frand_state buf_state;
339 unsigned int verify_batch;
340 unsigned int trim_batch;
345 * IO engine hooks, contains everything needed to submit an io_u
346 * to any of the available IO engines.
348 struct ioengine_ops *io_ops;
351 * Queue depth of io_u's that fio MIGHT do
353 unsigned int cur_depth;
356 * io_u's about to be committed
358 unsigned int io_u_queued;
361 * io_u's submitted but not completed yet
363 unsigned int io_u_in_flight;
366 * List of free and busy io_u's
368 struct flist_head io_u_freelist;
369 struct flist_head io_u_busylist;
370 struct flist_head io_u_requeues;
371 pthread_mutex_t io_u_lock;
372 pthread_cond_t free_cond;
375 * async verify offload
377 struct flist_head verify_list;
378 pthread_t *verify_threads;
379 unsigned int nr_verify_threads;
380 pthread_cond_t verify_cond;
381 int verify_thread_exit;
386 unsigned long long rate_bps[2];
387 long rate_pending_usleep[2];
388 unsigned long rate_bytes[2];
389 unsigned long rate_blocks[2];
390 struct timeval lastrate[2];
392 unsigned long long total_io_size;
393 unsigned long long fill_device_size;
395 unsigned long io_issues[2];
396 unsigned long long io_blocks[2];
397 unsigned long long this_io_blocks[2];
398 unsigned long long io_bytes[2];
399 unsigned long long io_skip_bytes;
400 unsigned long long this_io_bytes[2];
401 unsigned long long zone_bytes;
402 struct fio_mutex *mutex;
405 * State for random io, a bitmap of blocks done vs not done
408 os_random_state_t random_state;
409 struct frand_state __random_state;
412 struct timeval start; /* start of this loop */
413 struct timeval epoch; /* time job was started */
414 struct timeval last_issue;
415 struct timeval tv_cache;
416 unsigned int tv_cache_nr;
417 unsigned int tv_cache_mask;
418 unsigned int ramp_time_over;
421 * read/write mixed workload state
424 os_random_state_t rwmix_state;
425 struct frand_state __rwmix_state;
427 unsigned long rwmix_issues;
428 enum fio_ddir rwmix_ddir;
429 unsigned int ddir_seq_nr;
432 * IO history logs for verification. We use a tree for sorting,
433 * if we are overwriting. Otherwise just use a fifo.
435 struct rb_root io_hist_tree;
436 struct flist_head io_hist_list;
437 unsigned long io_hist_len;
442 struct flist_head io_log_list;
445 * For tracking/handling discards
447 struct flist_head trim_list;
448 unsigned long trim_entries;
451 * for fileservice, how often to switch to a new file
453 unsigned int file_service_nr;
454 unsigned int file_service_left;
455 struct fio_file *file_service_file;
457 unsigned int sync_file_range_nr;
460 * For generating file sizes
463 os_random_state_t file_size_state;
464 struct frand_state __file_size_state;
470 unsigned int total_err_count;
474 * Can be overloaded by profiles
476 struct prof_io_ops prof_io_ops;
481 * when should interactive ETA output be generated
489 #define __td_verror(td, err, msg, func) \
495 if (!(td)->first_error) \
496 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
500 #define td_clear_error(td) \
502 #define td_verror(td, err, func) \
503 __td_verror((td), (err), strerror((err)), (func))
504 #define td_vmsg(td, err, msg, func) \
505 __td_verror((td), (err), (msg), (func))
507 #define __fio_stringify_1(x) #x
508 #define __fio_stringify(x) __fio_stringify_1(x)
510 extern int exitall_on_terminate;
511 extern unsigned int thread_number;
512 extern unsigned int nr_process, nr_thread;
515 extern int terse_output;
516 extern int temp_stall_ts;
517 extern unsigned long long mlock_size;
518 extern unsigned long page_mask, page_size;
519 extern int read_only;
520 extern int eta_print;
521 extern unsigned long done_secs;
522 extern char *job_section;
523 extern int fio_gtod_offload;
524 extern int fio_gtod_cpu;
525 extern enum fio_cs fio_clock_source;
526 extern int warnings_fatal;
527 extern int terse_version;
528 extern int is_backend;
529 extern int nr_clients;
530 extern int log_syslog;
531 extern const char fio_version_string[];
532 extern const fio_fp64_t def_percentile_list[FIO_IO_U_LIST_MAX_LEN];
534 extern struct thread_data *threads;
536 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
538 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
541 #define BLOCKS_PER_MAP (8 * sizeof(unsigned long))
542 #define TO_MAP_BLOCK(f, b) (b)
543 #define RAND_MAP_IDX(f, b) (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
544 #define RAND_MAP_BIT(f, b) (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
546 #define REAL_MAX_JOBS 2048
548 #define td_non_fatal_error(e) ((e) == EIO || (e) == EILSEQ)
550 static inline enum error_type td_error_type(enum fio_ddir ddir, int err)
553 return ERROR_TYPE_VERIFY;
554 if (ddir == DDIR_READ)
555 return ERROR_TYPE_READ;
556 return ERROR_TYPE_WRITE;
559 static inline void update_error_count(struct thread_data *td, int err)
561 td->total_err_count++;
562 if (td->total_err_count == 1)
563 td->first_error = err;
566 static inline int should_fsync(struct thread_data *td)
568 if (td->last_was_sync)
572 if (td_write(td) || td_rw(td) || td->o.override_sync)
579 * Init/option functions
581 extern int __must_check parse_options(int, char **);
582 extern int parse_jobs_ini(char *, int, int);
583 extern int parse_cmd_line(int, char **);
584 extern int fio_backend(void);
585 extern void reset_fio_state(void);
586 extern void clear_io_state(struct thread_data *);
587 extern int fio_options_parse(struct thread_data *, char **, int);
588 extern void fio_keywords_init(void);
589 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
590 extern int fio_cmd_ioengine_option_parse(struct thread_data *, const char *, char *);
591 extern void fio_fill_default_options(struct thread_data *);
592 extern int fio_show_option_help(const char *);
593 extern void fio_options_set_ioengine_opts(struct option *long_options, struct thread_data *td);
594 extern void fio_options_dup_and_init(struct option *);
595 extern void fio_options_mem_dupe(struct thread_data *);
596 extern void options_mem_dupe(void *data, struct fio_option *options);
597 extern void td_fill_rand_seeds(struct thread_data *);
598 extern void add_job_opts(const char **);
599 extern char *num2str(unsigned long, int, int, int);
600 extern int ioengine_load(struct thread_data *);
602 #define FIO_GETOPT_JOB 0x89000000
603 #define FIO_GETOPT_IOENGINE 0x98000000
604 #define FIO_NR_OPTIONS (FIO_MAX_OPTS + 128)
609 extern void print_thread_status(void);
610 extern void print_status_init(int);
613 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
614 * will never back again. It may cycle between running/verififying/fsyncing.
615 * Once the thread reaches TD_EXITED, it is just waiting for the core to
631 extern void td_set_runstate(struct thread_data *, int);
632 #define TERMINATE_ALL (-1)
633 extern void fio_terminate_threads(int);
638 extern int __must_check fio_pin_memory(void);
639 extern void fio_unpin_memory(void);
640 extern int __must_check allocate_io_mem(struct thread_data *);
641 extern void free_io_mem(struct thread_data *);
644 * Reset stats after ramp time completes
646 extern void reset_all_stats(struct thread_data *);
651 #ifdef FIO_HAVE_BLKTRACE
652 extern int is_blktrace(const char *);
653 extern int load_blktrace(struct thread_data *, const char *);
657 * Mark unused variables passed to ops functions as unused, to silence gcc
659 #define fio_unused __attribute((__unused__))
660 #define fio_init __attribute__((constructor))
661 #define fio_exit __attribute__((destructor))
663 #define for_each_td(td, i) \
664 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
665 #define for_each_file(td, f, i) \
666 if ((td)->files_index) \
667 for ((i) = 0, (f) = (td)->files[0]; \
668 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
671 #define fio_assert(td, cond) do { \
674 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
675 td_set_runstate((td), TD_EXITED); \
676 (td)->error = EFAULT; \
681 static inline int fio_fill_issue_time(struct thread_data *td)
683 if (td->o.read_iolog_file ||
684 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
690 static inline int __should_check_rate(struct thread_data *td,
693 struct thread_options *o = &td->o;
696 * If some rate setting was given, we need to check it
698 if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
699 o->rate_iops_min[ddir])
705 static inline int should_check_rate(struct thread_data *td,
706 unsigned long *bytes_done)
711 ret |= __should_check_rate(td, 0);
713 ret |= __should_check_rate(td, 1);
718 static inline int is_power_of_2(unsigned int val)
720 return (val != 0 && ((val & (val - 1)) == 0));
724 * We currently only need to do locking if we have verifier threads
725 * accessing our internal structures too
727 static inline void td_io_u_lock(struct thread_data *td)
729 if (td->o.verify_async)
730 pthread_mutex_lock(&td->io_u_lock);
733 static inline void td_io_u_unlock(struct thread_data *td)
735 if (td->o.verify_async)
736 pthread_mutex_unlock(&td->io_u_lock);
739 static inline void td_io_u_free_notify(struct thread_data *td)
741 if (td->o.verify_async)
742 pthread_cond_signal(&td->free_cond);
745 extern const char *fio_get_arch_string(int);
746 extern const char *fio_get_os_string(int);