8 #include <sys/resource.h>
17 #include "compiler/compiler.h"
18 #include "thread_options.h"
21 #include "arch/arch.h"
35 #include "oslib/getopt.h"
37 #include "lib/rbtree.h"
42 #include "io_u_queue.h"
43 #include "workqueue.h"
45 #ifdef CONFIG_SOLARISAIO
46 #include <sys/asynch.h>
50 #include <linux/mempolicy.h>
54 * "local" is pseudo-policy
56 #define MPOL_LOCAL MPOL_MAX
60 * offset generator types
68 TD_F_VER_BACKLOG = 1U << 0,
69 TD_F_TRIM_BACKLOG = 1U << 1,
70 TD_F_READ_IOLOG = 1U << 2,
71 TD_F_REFILL_BUFFERS = 1U << 3,
72 TD_F_SCRAMBLE_BUFFERS = 1U << 4,
73 TD_F_VER_NONE = 1U << 5,
74 TD_F_PROFILE_OPS = 1U << 6,
75 TD_F_COMPRESS = 1U << 7,
77 TD_F_COMPRESS_LOG = 1U << 9,
78 TD_F_VSTATE_SAVED = 1U << 10,
79 TD_F_NEED_LOCK = 1U << 11,
80 TD_F_CHILD = 1U << 12,
81 TD_F_NO_PROGRESS = 1U << 13,
90 FIO_RAND_FILE_SIZE_OFF,
93 FIO_RAND_SEQ_RAND_READ_OFF,
94 FIO_RAND_SEQ_RAND_WRITE_OFF,
95 FIO_RAND_SEQ_RAND_TRIM_OFF,
107 RATE_PROCESS_LINEAR = 0,
108 RATE_PROCESS_POISSON = 1,
112 * Per-thread/process specific data. Only used for the network client
116 void sk_out_assign(struct sk_out *);
117 void sk_out_drop(void);
119 struct zone_split_index {
121 uint8_t size_perc_prev;
125 * This describes a single thread/process executing a fio job.
128 struct thread_options o;
129 struct flist_head opt_list;
132 char verror[FIO_VERROR_SIZE];
134 unsigned int thread_number;
135 unsigned int subjob_number;
136 unsigned int groupid;
137 struct thread_stat ts;
141 struct io_log *slat_log;
142 struct io_log *clat_log;
143 struct io_log *lat_log;
144 struct io_log *bw_log;
145 struct io_log *iops_log;
147 struct workqueue log_compress_wq;
149 struct thread_data *parent;
151 uint64_t stat_io_bytes[DDIR_RWDIR_CNT];
152 struct timeval bw_sample_time;
154 uint64_t stat_io_blocks[DDIR_RWDIR_CNT];
155 struct timeval iops_sample_time;
157 volatile int update_rusage;
158 struct fio_mutex *rusage_sem;
159 struct rusage ru_start;
160 struct rusage ru_end;
162 struct fio_file **files;
163 unsigned char *file_locks;
164 unsigned int files_size;
165 unsigned int files_index;
166 unsigned int nr_open_files;
167 unsigned int nr_done_files;
168 unsigned int nr_normal_files;
170 unsigned int next_file;
171 struct frand_state next_file_state;
179 size_t orig_buffer_size;
180 volatile int terminate;
181 volatile int runstate;
182 unsigned int last_was_sync;
183 enum fio_ddir last_ddir;
192 unsigned long rand_seeds[FIO_RAND_NR_OFFS];
194 struct frand_state bsrange_state;
195 struct frand_state verify_state;
196 struct frand_state trim_state;
197 struct frand_state delay_state;
199 struct frand_state buf_state;
200 struct frand_state buf_state_prev;
201 struct frand_state dedupe_state;
202 struct frand_state zone_state;
204 struct zone_split_index **zone_state_index;
206 unsigned int verify_batch;
207 unsigned int trim_batch;
209 struct thread_io_list *vstate;
214 * IO engine hooks, contains everything needed to submit an io_u
215 * to any of the available IO engines.
217 struct ioengine_ops *io_ops;
220 * Queue depth of io_u's that fio MIGHT do
222 unsigned int cur_depth;
225 * io_u's about to be committed
227 unsigned int io_u_queued;
230 * io_u's submitted but not completed yet
232 unsigned int io_u_in_flight;
235 * List of free and busy io_u's
237 struct io_u_ring io_u_requeues;
238 struct io_u_queue io_u_freelist;
239 struct io_u_queue io_u_all;
240 pthread_mutex_t io_u_lock;
241 pthread_cond_t free_cond;
244 * async verify offload
246 struct flist_head verify_list;
247 pthread_t *verify_threads;
248 unsigned int nr_verify_threads;
249 pthread_cond_t verify_cond;
250 int verify_thread_exit;
255 uint64_t rate_bps[DDIR_RWDIR_CNT];
256 unsigned long rate_next_io_time[DDIR_RWDIR_CNT];
257 unsigned long rate_bytes[DDIR_RWDIR_CNT];
258 unsigned long rate_blocks[DDIR_RWDIR_CNT];
259 unsigned long rate_io_issue_bytes[DDIR_RWDIR_CNT];
260 struct timeval lastrate[DDIR_RWDIR_CNT];
262 struct frand_state poisson_state;
265 * Enforced rate submission/completion workqueue
267 struct workqueue io_wq;
269 uint64_t total_io_size;
270 uint64_t fill_device_size;
275 uint64_t io_issues[DDIR_RWDIR_CNT];
276 uint64_t io_issue_bytes[DDIR_RWDIR_CNT];
282 uint64_t io_blocks[DDIR_RWDIR_CNT];
283 uint64_t this_io_blocks[DDIR_RWDIR_CNT];
284 uint64_t io_bytes[DDIR_RWDIR_CNT];
285 uint64_t this_io_bytes[DDIR_RWDIR_CNT];
286 uint64_t io_skip_bytes;
288 struct fio_mutex *mutex;
289 uint64_t bytes_done[DDIR_RWDIR_CNT];
292 * State for random io, a bitmap of blocks done vs not done
294 struct frand_state random_state;
296 struct timeval start; /* start of this loop */
297 struct timeval epoch; /* time job was started */
298 struct timeval last_issue;
300 struct timeval tv_cache;
301 struct timeval terminate_time;
302 unsigned int tv_cache_nr;
303 unsigned int tv_cache_mask;
304 unsigned int ramp_time_over;
307 * Time since last latency_window was started
309 struct timeval latency_ts;
310 unsigned int latency_qd;
311 unsigned int latency_qd_high;
312 unsigned int latency_qd_low;
313 unsigned int latency_failed;
314 uint64_t latency_ios;
318 * read/write mixed workload state
320 struct frand_state rwmix_state;
321 unsigned long rwmix_issues;
322 enum fio_ddir rwmix_ddir;
323 unsigned int ddir_seq_nr;
326 * rand/seq mixed workload state
328 struct frand_state seq_rand_state[DDIR_RWDIR_CNT];
331 * IO history logs for verification. We use a tree for sorting,
332 * if we are overwriting. Otherwise just use a fifo.
334 struct rb_root io_hist_tree;
335 struct flist_head io_hist_list;
336 unsigned long io_hist_len;
341 struct flist_head io_log_list;
344 * For tracking/handling discards
346 struct flist_head trim_list;
347 unsigned long trim_entries;
349 struct flist_head next_rand_list;
352 * for fileservice, how often to switch to a new file
354 unsigned int file_service_nr;
355 unsigned int file_service_left;
356 struct fio_file *file_service_file;
358 unsigned int sync_file_range_nr;
361 * For generating file sizes
363 struct frand_state file_size_state;
368 unsigned int total_err_count;
371 struct fio_flow *flow;
374 * Can be overloaded by profiles
376 struct prof_io_ops prof_io_ops;
383 * when should interactive ETA output be generated
391 #define __td_verror(td, err, msg, func) \
393 unsigned int ____e = (err); \
396 (td)->error = ____e; \
397 if (!(td)->first_error) \
398 snprintf(td->verror, sizeof(td->verror), "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg)); \
402 #define td_clear_error(td) do { \
405 (td)->parent->error = 0; \
408 #define td_verror(td, err, func) do { \
409 __td_verror((td), (err), strerror((err)), (func)); \
411 __td_verror((td)->parent, (err), strerror((err)), (func)); \
414 #define td_vmsg(td, err, msg, func) do { \
415 __td_verror((td), (err), (msg), (func)); \
417 __td_verror((td)->parent, (err), (msg), (func)); \
420 #define __fio_stringify_1(x) #x
421 #define __fio_stringify(x) __fio_stringify_1(x)
423 extern int exitall_on_terminate;
424 extern unsigned int thread_number;
425 extern unsigned int stat_number;
428 extern int output_format;
429 extern int append_terse_output;
430 extern int temp_stall_ts;
431 extern uintptr_t page_mask, page_size;
432 extern int read_only;
433 extern int eta_print;
434 extern int eta_new_line;
435 extern unsigned long done_secs;
436 extern char *job_section;
437 extern int fio_gtod_offload;
438 extern int fio_gtod_cpu;
439 extern enum fio_cs fio_clock_source;
440 extern int fio_clock_source_set;
441 extern int warnings_fatal;
442 extern int terse_version;
443 extern int is_backend;
444 extern int nr_clients;
445 extern int log_syslog;
446 extern int status_interval;
447 extern const char fio_version_string[];
448 extern int helper_do_stat;
449 extern pthread_cond_t helper_cond;
450 extern char *trigger_file;
451 extern char *trigger_cmd;
452 extern char *trigger_remote_cmd;
453 extern long long trigger_timeout;
454 extern char *aux_path;
456 extern struct thread_data *threads;
458 static inline void fio_ro_check(const struct thread_data *td, struct io_u *io_u)
460 assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
463 #define REAL_MAX_JOBS 2048
465 static inline int should_fsync(struct thread_data *td)
467 if (td->last_was_sync)
469 if (td_write(td) || td_rw(td) || td->o.override_sync)
476 * Init/option functions
478 extern int __must_check fio_init_options(void);
479 extern int __must_check parse_options(int, char **);
480 extern int parse_jobs_ini(char *, int, int, int);
481 extern int parse_cmd_line(int, char **, int);
482 extern int fio_backend(struct sk_out *);
483 extern void reset_fio_state(void);
484 extern void clear_io_state(struct thread_data *, int);
485 extern int fio_options_parse(struct thread_data *, char **, int);
486 extern void fio_keywords_init(void);
487 extern void fio_keywords_exit(void);
488 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
489 extern int fio_cmd_ioengine_option_parse(struct thread_data *, const char *, char *);
490 extern void fio_fill_default_options(struct thread_data *);
491 extern int fio_show_option_help(const char *);
492 extern void fio_options_set_ioengine_opts(struct option *long_options, struct thread_data *td);
493 extern void fio_options_dup_and_init(struct option *);
494 extern void fio_options_mem_dupe(struct thread_data *);
495 extern void options_mem_dupe(void *data, struct fio_option *options);
496 extern void td_fill_rand_seeds(struct thread_data *);
497 extern void add_job_opts(const char **, int);
498 extern char *num2str(uint64_t, int, int, int, int);
499 extern int ioengine_load(struct thread_data *);
500 extern int parse_dryrun(void);
501 extern int fio_running_or_pending_io_threads(void);
502 extern int fio_set_fd_nonblocking(int, const char *);
503 extern void sig_show_status(int sig);
504 extern struct thread_data *get_global_options(void);
506 extern uintptr_t page_mask;
507 extern uintptr_t page_size;
508 extern int initialize_fio(char *envp[]);
509 extern void deinitialize_fio(void);
511 #define FIO_GETOPT_JOB 0x89000000
512 #define FIO_GETOPT_IOENGINE 0x98000000
513 #define FIO_NR_OPTIONS (FIO_MAX_OPTS + 128)
518 extern void print_thread_status(void);
519 extern void print_status_init(int);
520 extern char *fio_uint_to_kmg(unsigned int val);
523 * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
524 * will never back again. It may cycle between running/verififying/fsyncing.
525 * Once the thread reaches TD_EXITED, it is just waiting for the core to
544 extern void td_set_runstate(struct thread_data *, int);
545 extern int td_bump_runstate(struct thread_data *, int);
546 extern void td_restore_runstate(struct thread_data *, int);
547 extern const char *runstate_to_name(int runstate);
550 * Allow 60 seconds for a job to quit on its own, otherwise reap with
553 #define FIO_REAP_TIMEOUT 60
555 #define TERMINATE_ALL (-1U)
556 extern void fio_terminate_threads(unsigned int);
557 extern void fio_mark_td_terminate(struct thread_data *);
562 extern int __must_check fio_pin_memory(struct thread_data *);
563 extern void fio_unpin_memory(struct thread_data *);
564 extern int __must_check allocate_io_mem(struct thread_data *);
565 extern void free_io_mem(struct thread_data *);
566 extern void free_threads_shm(void);
569 * Reset stats after ramp time completes
571 extern void reset_all_stats(struct thread_data *);
576 #ifdef FIO_HAVE_BLKTRACE
577 extern int is_blktrace(const char *, int *);
578 extern int load_blktrace(struct thread_data *, const char *, int);
581 extern int io_queue_event(struct thread_data *td, struct io_u *io_u, int *ret,
582 enum fio_ddir ddir, uint64_t *bytes_issued, int from_verify,
583 struct timeval *comp_time);
586 * Latency target helpers
588 extern void lat_target_check(struct thread_data *);
589 extern void lat_target_init(struct thread_data *);
590 extern void lat_target_reset(struct thread_data *);
592 #define for_each_td(td, i) \
593 for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
594 #define for_each_file(td, f, i) \
595 if ((td)->files_index) \
596 for ((i) = 0, (f) = (td)->files[0]; \
597 (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
600 #define fio_assert(td, cond) do { \
603 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond); \
604 td_set_runstate((td), TD_EXITED); \
605 (td)->error = EFAULT; \
610 static inline int fio_fill_issue_time(struct thread_data *td)
612 if (td->o.read_iolog_file ||
613 !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
619 static inline int __should_check_rate(struct thread_data *td,
622 struct thread_options *o = &td->o;
625 * If some rate setting was given, we need to check it
627 if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
628 o->rate_iops_min[ddir])
634 static inline int should_check_rate(struct thread_data *td)
638 if (td->bytes_done[DDIR_READ])
639 ret |= __should_check_rate(td, DDIR_READ);
640 if (td->bytes_done[DDIR_WRITE])
641 ret |= __should_check_rate(td, DDIR_WRITE);
642 if (td->bytes_done[DDIR_TRIM])
643 ret |= __should_check_rate(td, DDIR_TRIM);
648 static inline unsigned int td_max_bs(struct thread_data *td)
652 max_bs = max(td->o.max_bs[DDIR_READ], td->o.max_bs[DDIR_WRITE]);
653 return max(td->o.max_bs[DDIR_TRIM], max_bs);
656 static inline unsigned int td_min_bs(struct thread_data *td)
660 min_bs = min(td->o.min_bs[DDIR_READ], td->o.min_bs[DDIR_WRITE]);
661 return min(td->o.min_bs[DDIR_TRIM], min_bs);
664 static inline int td_async_processing(struct thread_data *td)
666 return (td->flags & TD_F_NEED_LOCK) != 0;
670 * We currently only need to do locking if we have verifier threads
671 * accessing our internal structures too
673 static inline void td_io_u_lock(struct thread_data *td)
675 if (td_async_processing(td))
676 pthread_mutex_lock(&td->io_u_lock);
679 static inline void td_io_u_unlock(struct thread_data *td)
681 if (td_async_processing(td))
682 pthread_mutex_unlock(&td->io_u_lock);
685 static inline void td_io_u_free_notify(struct thread_data *td)
687 if (td_async_processing(td))
688 pthread_cond_signal(&td->free_cond);
691 extern const char *fio_get_arch_string(int);
692 extern const char *fio_get_os_string(int);
695 #define ARRAY_SIZE(x) (sizeof((x)) / (sizeof((x)[0])))
696 #define FIELD_SIZE(s, f) (sizeof(((typeof(s))0)->f))
700 __FIO_OUTPUT_TERSE = 0,
701 __FIO_OUTPUT_JSON = 1,
702 __FIO_OUTPUT_NORMAL = 2,
703 __FIO_OUTPUT_JSON_PLUS = 3,
706 FIO_OUTPUT_TERSE = 1U << __FIO_OUTPUT_TERSE,
707 FIO_OUTPUT_JSON = 1U << __FIO_OUTPUT_JSON,
708 FIO_OUTPUT_NORMAL = 1U << __FIO_OUTPUT_NORMAL,
709 FIO_OUTPUT_JSON_PLUS = 1U << __FIO_OUTPUT_JSON_PLUS,
713 FIO_RAND_DIST_RANDOM = 0,
715 FIO_RAND_DIST_PARETO,
720 #define FIO_DEF_ZIPF 1.1
721 #define FIO_DEF_PARETO 0.2
724 FIO_RAND_GEN_TAUSWORTHE = 0,
726 FIO_RAND_GEN_TAUSWORTHE64,
734 extern void exec_trigger(const char *);
735 extern void check_trigger_file(void);