8 #include <sys/resource.h>
23 #include "compiler/compiler.h"
27 #include "arch/arch.h"
40 #include "lib/getopt.h"
49 #ifdef FIO_HAVE_SOLARISAIO
50 #include <sys/asynch.h>
54 * What type of allocation to use for io buffers
57 MEM_MALLOC = 0, /* ordinary malloc */
58 MEM_SHM, /* use shared memory segments */
59 MEM_SHMHUGE, /* use shared memory segments with huge pages */
60 MEM_MMAP, /* use anonynomous mmap */
61 MEM_MMAPHUGE, /* memory mapped huge file */
65 * offset generator types
77 struct thread_options {
88 unsigned int ddir_seq_nr;
91 unsigned int iodepth_low;
92 unsigned int iodepth_batch;
93 unsigned int iodepth_batch_complete;
95 unsigned long long size;
96 unsigned int size_percent;
97 unsigned int fill_device;
98 unsigned long long file_size_low;
99 unsigned long long file_size_high;
100 unsigned long long start_offset;
104 unsigned int min_bs[2];
105 unsigned int max_bs[2];
106 struct bssplit *bssplit[2];
107 unsigned int bssplit_nr[2];
109 unsigned int nr_files;
110 unsigned int open_files;
111 enum file_lock_mode file_lock_mode;
112 unsigned int lockfile_batch;
114 unsigned int odirect;
115 unsigned int invalidate_cache;
116 unsigned int create_serialize;
117 unsigned int create_fsync;
118 unsigned int create_on_open;
119 unsigned int end_fsync;
120 unsigned int pre_read;
121 unsigned int sync_io;
123 unsigned int do_verify;
124 unsigned int verifysort;
125 unsigned int verify_interval;
126 unsigned int verify_offset;
127 char verify_pattern[MAX_PATTERN_SIZE];
128 unsigned int verify_pattern_bytes;
129 unsigned int verify_fatal;
130 unsigned int verify_dump;
131 unsigned int verify_async;
132 unsigned long long verify_backlog;
133 unsigned int verify_batch;
134 unsigned int use_thread;
136 unsigned int do_disk_util;
137 unsigned int override_sync;
138 unsigned int rand_repeatable;
139 unsigned int use_os_rand;
140 unsigned int write_lat_log;
141 unsigned int write_bw_log;
142 unsigned int norandommap;
143 unsigned int softrandommap;
144 unsigned int bs_unaligned;
145 unsigned int fsync_on_close;
147 unsigned int hugepage_size;
148 unsigned int rw_min_bs;
149 unsigned int thinktime;
150 unsigned int thinktime_spin;
151 unsigned int thinktime_blocks;
152 unsigned int fsync_blocks;
153 unsigned int fdatasync_blocks;
154 unsigned int barrier_blocks;
155 unsigned long long start_delay;
156 unsigned long long timeout;
157 unsigned long long ramp_time;
158 unsigned int overwrite;
159 unsigned int bw_avg_time;
161 unsigned long long zone_size;
162 unsigned long long zone_skip;
163 enum fio_memtype mem_type;
164 unsigned int mem_align;
166 unsigned int stonewall;
167 unsigned int new_group;
168 unsigned int numjobs;
169 os_cpu_mask_t cpumask;
170 unsigned int cpumask_set;
171 os_cpu_mask_t verify_cpumask;
172 unsigned int verify_cpumask_set;
174 unsigned int rwmixcycle;
175 unsigned int rwmix[2];
177 unsigned int file_service_type;
178 unsigned int group_reporting;
179 unsigned int fadvise_hint;
180 enum fio_fallocate_mode fallocate_mode;
181 unsigned int zero_buffers;
182 unsigned int refill_buffers;
183 unsigned int scramble_buffers;
184 unsigned int time_based;
185 unsigned int disable_lat;
186 unsigned int disable_clat;
187 unsigned int disable_slat;
188 unsigned int disable_bw;
189 unsigned int gtod_reduce;
190 unsigned int gtod_cpu;
191 unsigned int gtod_offload;
192 enum fio_cs clocksource;
193 unsigned int no_stall;
194 unsigned int trim_percentage;
195 unsigned int trim_batch;
196 unsigned int trim_zero;
197 unsigned long long trim_backlog;
198 unsigned int clat_percentiles;
199 unsigned int overwrite_plist;
200 double percentile_list[FIO_IO_U_LIST_MAX_LEN];
202 char *read_iolog_file;
203 char *write_iolog_file;
206 char *replay_redirect;
209 * Pre-run and post-run shell
214 unsigned int rate[2];
215 unsigned int ratemin[2];
216 unsigned int ratecycle;
217 unsigned int rate_iops[2];
218 unsigned int rate_iops_min[2];
223 * CPU "io" cycle burner
225 unsigned int cpuload;
226 unsigned int cpucycle;
231 unsigned int continue_on_error;
234 * Benchmark profile type
239 * blkio cgroup support
242 unsigned int cgroup_weight;
243 unsigned int cgroup_nodelete;
248 unsigned int sync_file_range;
250 unsigned int userspace_libaio_reap;
254 * This describes a single thread/process executing a fio job.
257 struct thread_options o;
258 char verror[FIO_VERROR_SIZE];
262 struct thread_stat ts;
264 struct io_log *slat_log;
265 struct io_log *clat_log;
266 struct io_log *lat_log;
267 struct io_log *bw_log;
269 uint64_t stat_io_bytes[2];
270 struct timeval stat_sample_time[2];
272 struct rusage ru_start;
273 struct rusage ru_end;
275 struct fio_file **files;
276 unsigned int files_size;
277 unsigned int files_index;
278 unsigned int nr_open_files;
279 unsigned int nr_done_files;
280 unsigned int nr_normal_files;
282 unsigned int next_file;
283 os_random_state_t next_file_state;
284 struct frand_state __next_file_state;
290 size_t orig_buffer_size;
291 volatile int terminate;
292 volatile int runstate;
294 unsigned int ioprio_set;
295 unsigned int last_was_sync;
296 enum fio_ddir last_ddir;
306 unsigned long rand_seeds[8];
309 os_random_state_t bsrange_state;
310 struct frand_state __bsrange_state;
313 os_random_state_t verify_state;
314 struct frand_state __verify_state;
317 os_random_state_t trim_state;
318 struct frand_state __trim_state;
321 struct frand_state buf_state;
323 unsigned int verify_batch;
324 unsigned int trim_batch;
329 * IO engine hooks, contains everything needed to submit an io_u
330 * to any of the available IO engines.
332 struct ioengine_ops *io_ops;
335 * Current IO depth and list of free and busy io_u's.
337 unsigned int cur_depth;
338 unsigned int io_u_queued;
339 struct flist_head io_u_freelist;
340 struct flist_head io_u_busylist;
341 struct flist_head io_u_requeues;
342 pthread_mutex_t io_u_lock;
343 pthread_cond_t free_cond;
346 * async verify offload
348 struct flist_head verify_list;
349 pthread_t *verify_threads;
350 unsigned int nr_verify_threads;
351 pthread_cond_t verify_cond;
352 int verify_thread_exit;
357 unsigned long rate_nsec_cycle[2];
358 long rate_pending_usleep[2];
359 unsigned long rate_bytes[2];
360 unsigned long rate_blocks[2];
361 struct timeval lastrate[2];
363 unsigned long long total_io_size;
364 unsigned long long fill_device_size;
366 unsigned long io_issues[2];
367 unsigned long long io_blocks[2];
368 unsigned long long io_bytes[2];
369 unsigned long long io_skip_bytes;
370 unsigned long long this_io_bytes[2];
371 unsigned long long zone_bytes;
372 struct fio_mutex *mutex;
375 * State for random io, a bitmap of blocks done vs not done
378 os_random_state_t random_state;
379 struct frand_state __random_state;
382 struct timeval start; /* start of this loop */
383 struct timeval epoch; /* time job was started */
384 struct timeval last_issue;
385 struct timeval tv_cache;
386 unsigned int tv_cache_nr;
387 unsigned int tv_cache_mask;
388 unsigned int ramp_time_over;
391 * read/write mixed workload state
394 os_random_state_t rwmix_state;
395 struct frand_state __rwmix_state;
397 unsigned long rwmix_issues;
398 enum fio_ddir rwmix_ddir;
399 unsigned int ddir_seq_nr;
402 * IO history logs for verification. We use a tree for sorting,
403 * if we are overwriting. Otherwise just use a fifo.
405 struct rb_root io_hist_tree;
406 struct flist_head io_hist_list;
407 unsigned long io_hist_len;
412 struct flist_head io_log_list;
415 * For tracking/handling discards
417 struct flist_head trim_list;
418 unsigned long trim_entries;
421 * for fileservice, how often to switch to a new file
423 unsigned int file_service_nr;
424 unsigned int file_service_left;
425 struct fio_file *file_service_file;
427 unsigned int sync_file_range_nr;
430 * For generating file sizes
433 os_random_state_t file_size_state;
434 struct frand_state __file_size_state;
440 unsigned int total_err_count;
444 * Can be overloaded by profiles
446 struct prof_io_ops prof_io_ops;
451 * when should interactive ETA output be generated
459 #define __td_verror(td, err, msg, func) \
465 if (!(td)->first_error) \
466 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
470 #define td_clear_error(td) \
472 #define td_verror(td, err, func) \
473 __td_verror((td), (err), strerror((err)), (func))
474 #define td_vmsg(td, err, msg, func) \
475 __td_verror((td), (err), (msg), (func))
477 #define __fio_stringify_1(x) #x
478 #define __fio_stringify(x) __fio_stringify_1(x)
480 extern int exitall_on_terminate;
481 extern int thread_number;
482 extern int nr_process, nr_thread;
485 extern int terse_output;
486 extern int temp_stall_ts;
487 extern unsigned long long mlock_size;
488 extern unsigned long page_mask, page_size;
489 extern int read_only;
490 extern int eta_print;
491 extern unsigned long done_secs;
492 extern char *job_section;
493 extern int fio_gtod_offload;
494 extern int fio_gtod_cpu;
495 extern enum fio_cs fio_clock_source;
496 extern int warnings_fatal;
497 extern int terse_version;
498 extern int is_backend;
499 extern int nr_clients;
500 extern int log_syslog;
502 extern struct thread_data *threads;
504 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
506 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
509 #define BLOCKS_PER_MAP (8 * sizeof(unsigned long))
510 #define TO_MAP_BLOCK(f, b) (b)
511 #define RAND_MAP_IDX(f, b) (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
512 #define RAND_MAP_BIT(f, b) (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
514 #define REAL_MAX_JOBS 2048
516 #define td_non_fatal_error(e) ((e) == EIO || (e) == EILSEQ)
518 static inline void update_error_count(struct thread_data *td, int err)
520 td->total_err_count++;
521 if (td->total_err_count == 1)
522 td->first_error = err;
525 static inline int should_fsync(struct thread_data *td)
527 if (td->last_was_sync)
531 if (td_write(td) || td_rw(td) || td->o.override_sync)
538 * Init/option functions
540 extern int __must_check parse_options(int, char **);
541 extern int parse_jobs_ini(char *, int, int);
542 extern int parse_cmd_line(int, char **);
543 extern int exec_run(void);
544 extern void reset_fio_state(void);
545 extern int fio_options_parse(struct thread_data *, char **, int);
546 extern void fio_keywords_init(void);
547 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
548 extern void fio_fill_default_options(struct thread_data *);
549 extern int fio_show_option_help(const char *);
550 extern void fio_options_dup_and_init(struct option *);
551 extern void options_mem_dupe(struct thread_data *);
552 extern void options_mem_free(struct thread_data *);
553 extern void td_fill_rand_seeds(struct thread_data *);
554 extern void add_job_opts(const char **);
555 extern char *num2str(unsigned long, int, int, int);
557 #define FIO_GETOPT_JOB 0x89988998
558 #define FIO_NR_OPTIONS (FIO_MAX_OPTS + 128)
563 extern void print_thread_status(void);
564 extern void print_status_init(int);
567 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
568 * will never back again. It may cycle between running/verififying/fsyncing.
569 * Once the thread reaches TD_EXITED, it is just waiting for the core to
585 extern void td_set_runstate(struct thread_data *, int);
586 #define TERMINATE_ALL (-1)
587 extern void fio_terminate_threads(int);
592 extern int __must_check fio_pin_memory(void);
593 extern void fio_unpin_memory(void);
594 extern int __must_check allocate_io_mem(struct thread_data *);
595 extern void free_io_mem(struct thread_data *);
598 * Reset stats after ramp time completes
600 extern void reset_all_stats(struct thread_data *);
605 #ifdef FIO_HAVE_BLKTRACE
606 extern int is_blktrace(const char *);
607 extern int load_blktrace(struct thread_data *, const char *);
611 * Mark unused variables passed to ops functions as unused, to silence gcc
613 #define fio_unused __attribute((__unused__))
614 #define fio_init __attribute__((constructor))
615 #define fio_exit __attribute__((destructor))
617 #define for_each_td(td, i) \
618 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
619 #define for_each_file(td, f, i) \
620 if ((td)->files_index) \
621 for ((i) = 0, (f) = (td)->files[0]; \
622 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
625 #define fio_assert(td, cond) do { \
628 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
629 td_set_runstate((td), TD_EXITED); \
630 (td)->error = EFAULT; \
635 static inline int fio_fill_issue_time(struct thread_data *td)
637 if (td->o.read_iolog_file ||
638 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
644 static inline int __should_check_rate(struct thread_data *td,
647 struct thread_options *o = &td->o;
650 * If some rate setting was given, we need to check it
652 if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
653 o->rate_iops_min[ddir])
659 static inline int should_check_rate(struct thread_data *td,
660 unsigned long *bytes_done)
665 ret |= __should_check_rate(td, 0);
667 ret |= __should_check_rate(td, 1);
672 static inline int is_power_of_2(unsigned int val)
674 return (val != 0 && ((val & (val - 1)) == 0));
678 * We currently only need to do locking if we have verifier threads
679 * accessing our internal structures too
681 static inline void td_io_u_lock(struct thread_data *td)
683 if (td->o.verify_async)
684 pthread_mutex_lock(&td->io_u_lock);
687 static inline void td_io_u_unlock(struct thread_data *td)
689 if (td->o.verify_async)
690 pthread_mutex_unlock(&td->io_u_lock);
693 static inline void td_io_u_free_notify(struct thread_data *td)
695 if (td->o.verify_async)
696 pthread_cond_signal(&td->free_cond);