Move getrusage() out of thread_stat
[fio.git] / fio.h
1 #ifndef FIO_H
2 #define FIO_H
3
4 #include <sched.h>
5 #include <limits.h>
6 #include <pthread.h>
7 #include <sys/time.h>
8 #include <sys/resource.h>
9 #include <errno.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <inttypes.h>
15 #include <assert.h>
16
17 struct thread_data;
18
19 #include "compiler/compiler.h"
20 #include "flist.h"
21 #include "fifo.h"
22 #include "rbtree.h"
23 #include "arch/arch.h"
24 #include "os/os.h"
25 #include "mutex.h"
26 #include "log.h"
27 #include "debug.h"
28 #include "file.h"
29 #include "io_ddir.h"
30 #include "ioengine.h"
31 #include "iolog.h"
32 #include "helpers.h"
33 #include "options.h"
34 #include "profile.h"
35 #include "time.h"
36 #include "lib/getopt.h"
37 #include "lib/rand.h"
38 #include "server.h"
39
40 #ifdef FIO_HAVE_GUASI
41 #include <guasi.h>
42 #endif
43
44 #ifdef FIO_HAVE_SOLARISAIO
45 #include <sys/asynch.h>
46 #endif
47
48 struct group_run_stats {
49         uint64_t max_run[2], min_run[2];
50         uint64_t max_bw[2], min_bw[2];
51         uint64_t io_kb[2];
52         uint64_t agg[2];
53         uint32_t kb_base;
54 };
55
56 /*
57  * What type of allocation to use for io buffers
58  */
59 enum fio_memtype {
60         MEM_MALLOC = 0, /* ordinary malloc */
61         MEM_SHM,        /* use shared memory segments */
62         MEM_SHMHUGE,    /* use shared memory segments with huge pages */
63         MEM_MMAP,       /* use anonynomous mmap */
64         MEM_MMAPHUGE,   /* memory mapped huge file */
65 };
66
67 /*
68  * offset generator types
69  */
70 enum {
71         RW_SEQ_SEQ      = 0,
72         RW_SEQ_IDENT,
73 };
74
75 /*
76  * How many depth levels to log
77  */
78 #define FIO_IO_U_MAP_NR 7
79 #define FIO_IO_U_LAT_U_NR 10
80 #define FIO_IO_U_LAT_M_NR 12
81
82 /*
83  * Aggregate clat samples to report percentile(s) of them.
84  *
85  * EXECUTIVE SUMMARY
86  *
87  * FIO_IO_U_PLAT_BITS determines the maximum statistical error on the
88  * value of resulting percentiles. The error will be approximately
89  * 1/2^(FIO_IO_U_PLAT_BITS+1) of the value.
90  *
91  * FIO_IO_U_PLAT_GROUP_NR and FIO_IO_U_PLAT_BITS determine the maximum
92  * range being tracked for latency samples. The maximum value tracked
93  * accurately will be 2^(GROUP_NR + PLAT_BITS -1) microseconds.
94  *
95  * FIO_IO_U_PLAT_GROUP_NR and FIO_IO_U_PLAT_BITS determine the memory
96  * requirement of storing those aggregate counts. The memory used will
97  * be (FIO_IO_U_PLAT_GROUP_NR * 2^FIO_IO_U_PLAT_BITS) * sizeof(int)
98  * bytes.
99  *
100  * FIO_IO_U_PLAT_NR is the total number of buckets.
101  *
102  * DETAILS
103  *
104  * Suppose the clat varies from 0 to 999 (usec), the straightforward
105  * method is to keep an array of (999 + 1) buckets, in which a counter
106  * keeps the count of samples which fall in the bucket, e.g.,
107  * {[0],[1],...,[999]}. However this consumes a huge amount of space,
108  * and can be avoided if an approximation is acceptable.
109  *
110  * One such method is to let the range of the bucket to be greater
111  * than one. This method has low accuracy when the value is small. For
112  * example, let the buckets be {[0,99],[100,199],...,[900,999]}, and
113  * the represented value of each bucket be the mean of the range. Then
114  * a value 0 has an round-off error of 49.5. To improve on this, we
115  * use buckets with non-uniform ranges, while bounding the error of
116  * each bucket within a ratio of the sample value. A simple example
117  * would be when error_bound = 0.005, buckets are {
118  * {[0],[1],...,[99]}, {[100,101],[102,103],...,[198,199]},..,
119  * {[900,909],[910,919]...}  }. The total range is partitioned into
120  * groups with different ranges, then buckets with uniform ranges. An
121  * upper bound of the error is (range_of_bucket/2)/value_of_bucket
122  *
123  * For better efficiency, we implement this using base two. We group
124  * samples by their Most Significant Bit (MSB), extract the next M bit
125  * of them as an index within the group, and discard the rest of the
126  * bits.
127  *
128  * E.g., assume a sample 'x' whose MSB is bit n (starting from bit 0),
129  * and use M bit for indexing
130  *
131  *        | n |    M bits   | bit (n-M-1) ... bit 0 |
132  *
133  * Because x is at least 2^n, and bit 0 to bit (n-M-1) is at most
134  * (2^(n-M) - 1), discarding bit 0 to (n-M-1) makes the round-off
135  * error
136  *
137  *           2^(n-M)-1    2^(n-M)    1
138  *      e <= --------- <= ------- = ---
139  *             2^n          2^n     2^M
140  *
141  * Furthermore, we use "mean" of the range to represent the bucket,
142  * the error e can be lowered by half to 1 / 2^(M+1). By using M bits
143  * as the index, each group must contains 2^M buckets.
144  *
145  * E.g. Let M (FIO_IO_U_PLAT_BITS) be 6
146  *      Error bound is 1/2^(6+1) = 0.0078125 (< 1%)
147  *
148  *      Group   MSB     #discarded      range of                #buckets
149  *                      error_bits      value
150  *      ----------------------------------------------------------------
151  *      0*      0~5     0               [0,63]                  64
152  *      1*      6       0               [64,127]                64
153  *      2       7       1               [128,255]               64
154  *      3       8       2               [256,511]               64
155  *      4       9       3               [512,1023]              64
156  *      ...     ...     ...             [...,...]               ...
157  *      18      23      17              [8838608,+inf]**        64
158  *
159  *  * Special cases: when n < (M-1) or when n == (M-1), in both cases,
160  *    the value cannot be rounded off. Use all bits of the sample as
161  *    index.
162  *
163  *  ** If a sample's MSB is greater than 23, it will be counted as 23.
164  */
165
166 #define FIO_IO_U_PLAT_BITS 6
167 #define FIO_IO_U_PLAT_VAL (1 << FIO_IO_U_PLAT_BITS)
168 #define FIO_IO_U_PLAT_GROUP_NR 19
169 #define FIO_IO_U_PLAT_NR (FIO_IO_U_PLAT_GROUP_NR * FIO_IO_U_PLAT_VAL)
170 #define FIO_IO_U_LIST_MAX_LEN 20 /* The size of the default and user-specified
171                                         list of percentiles */
172
173 #define MAX_PATTERN_SIZE 512
174
175 struct thread_stat {
176         char *name;
177         char *verror;
178         int32_t error;
179         int32_t groupid;
180         uint32_t pid;
181         char *description;
182         uint32_t members;
183
184         /*
185          * bandwidth and latency stats
186          */
187         struct io_stat clat_stat[2];            /* completion latency */
188         struct io_stat slat_stat[2];            /* submission latency */
189         struct io_stat lat_stat[2];             /* total latency */
190         struct io_stat bw_stat[2];              /* bandwidth stats */
191
192         /*
193          * fio system usage accounting
194          */
195         uint64_t usr_time;
196         uint64_t sys_time;
197         uint64_t ctx;
198         uint64_t minf, majf;
199
200         /*
201          * IO depth and latency stats
202          */
203         uint64_t clat_percentiles;
204         double *percentile_list;
205
206         uint32_t io_u_map[FIO_IO_U_MAP_NR];
207         uint32_t io_u_submit[FIO_IO_U_MAP_NR];
208         uint32_t io_u_complete[FIO_IO_U_MAP_NR];
209         uint32_t io_u_lat_u[FIO_IO_U_LAT_U_NR];
210         uint32_t io_u_lat_m[FIO_IO_U_LAT_M_NR];
211         uint32_t io_u_plat[2][FIO_IO_U_PLAT_NR];
212         uint64_t total_io_u[3];
213         uint64_t short_io_u[3];
214         uint64_t total_submit;
215         uint64_t total_complete;
216
217         uint64_t io_bytes[2];
218         uint64_t runtime[2];
219         uint64_t total_run_time;
220
221         /*
222          * IO Error related stats
223          */
224         uint16_t continue_on_error;
225         uint64_t total_err_count;
226         int32_t first_error;
227
228         uint32_t kb_base;
229 };
230
231 struct bssplit {
232         unsigned int bs;
233         unsigned char perc;
234 };
235
236 struct thread_options {
237         int pad;
238         char *description;
239         char *name;
240         char *directory;
241         char *filename;
242         char *opendir;
243         char *ioengine;
244         enum td_ddir td_ddir;
245         unsigned int rw_seq;
246         unsigned int kb_base;
247         unsigned int ddir_seq_nr;
248         long ddir_seq_add;
249         unsigned int iodepth;
250         unsigned int iodepth_low;
251         unsigned int iodepth_batch;
252         unsigned int iodepth_batch_complete;
253
254         unsigned long long size;
255         unsigned int size_percent;
256         unsigned int fill_device;
257         unsigned long long file_size_low;
258         unsigned long long file_size_high;
259         unsigned long long start_offset;
260
261         unsigned int bs[2];
262         unsigned int ba[2];
263         unsigned int min_bs[2];
264         unsigned int max_bs[2];
265         struct bssplit *bssplit[2];
266         unsigned int bssplit_nr[2];
267
268         unsigned int nr_files;
269         unsigned int open_files;
270         enum file_lock_mode file_lock_mode;
271         unsigned int lockfile_batch;
272
273         unsigned int odirect;
274         unsigned int invalidate_cache;
275         unsigned int create_serialize;
276         unsigned int create_fsync;
277         unsigned int create_on_open;
278         unsigned int end_fsync;
279         unsigned int pre_read;
280         unsigned int sync_io;
281         unsigned int verify;
282         unsigned int do_verify;
283         unsigned int verifysort;
284         unsigned int verify_interval;
285         unsigned int verify_offset;
286         char verify_pattern[MAX_PATTERN_SIZE];
287         unsigned int verify_pattern_bytes;
288         unsigned int verify_fatal;
289         unsigned int verify_dump;
290         unsigned int verify_async;
291         unsigned long long verify_backlog;
292         unsigned int verify_batch;
293         unsigned int use_thread;
294         unsigned int unlink;
295         unsigned int do_disk_util;
296         unsigned int override_sync;
297         unsigned int rand_repeatable;
298         unsigned int use_os_rand;
299         unsigned int write_lat_log;
300         unsigned int write_bw_log;
301         unsigned int norandommap;
302         unsigned int softrandommap;
303         unsigned int bs_unaligned;
304         unsigned int fsync_on_close;
305
306         unsigned int hugepage_size;
307         unsigned int rw_min_bs;
308         unsigned int thinktime;
309         unsigned int thinktime_spin;
310         unsigned int thinktime_blocks;
311         unsigned int fsync_blocks;
312         unsigned int fdatasync_blocks;
313         unsigned int barrier_blocks;
314         unsigned long long start_delay;
315         unsigned long long timeout;
316         unsigned long long ramp_time;
317         unsigned int overwrite;
318         unsigned int bw_avg_time;
319         unsigned int loops;
320         unsigned long long zone_size;
321         unsigned long long zone_skip;
322         enum fio_memtype mem_type;
323         unsigned int mem_align;
324
325         unsigned int stonewall;
326         unsigned int new_group;
327         unsigned int numjobs;
328         os_cpu_mask_t cpumask;
329         unsigned int cpumask_set;
330         os_cpu_mask_t verify_cpumask;
331         unsigned int verify_cpumask_set;
332         unsigned int iolog;
333         unsigned int rwmixcycle;
334         unsigned int rwmix[2];
335         unsigned int nice;
336         unsigned int file_service_type;
337         unsigned int group_reporting;
338         unsigned int fadvise_hint;
339         enum fio_fallocate_mode fallocate_mode;
340         unsigned int zero_buffers;
341         unsigned int refill_buffers;
342         unsigned int scramble_buffers;
343         unsigned int time_based;
344         unsigned int disable_lat;
345         unsigned int disable_clat;
346         unsigned int disable_slat;
347         unsigned int disable_bw;
348         unsigned int gtod_reduce;
349         unsigned int gtod_cpu;
350         unsigned int gtod_offload;
351         enum fio_cs clocksource;
352         unsigned int no_stall;
353         unsigned int trim_percentage;
354         unsigned int trim_batch;
355         unsigned int trim_zero;
356         unsigned long long trim_backlog;
357         unsigned int clat_percentiles;
358         unsigned int overwrite_plist;
359         double percentile_list[FIO_IO_U_LIST_MAX_LEN];
360
361         char *read_iolog_file;
362         char *write_iolog_file;
363         char *bw_log_file;
364         char *lat_log_file;
365         char *replay_redirect;
366
367         /*
368          * Pre-run and post-run shell
369          */
370         char *exec_prerun;
371         char *exec_postrun;
372
373         unsigned int rate[2];
374         unsigned int ratemin[2];
375         unsigned int ratecycle;
376         unsigned int rate_iops[2];
377         unsigned int rate_iops_min[2];
378
379         char *ioscheduler;
380
381         /*
382          * CPU "io" cycle burner
383          */
384         unsigned int cpuload;
385         unsigned int cpucycle;
386
387         /*
388          * I/O Error handling
389          */
390         unsigned int continue_on_error;
391
392         /*
393          * Benchmark profile type
394          */
395         char *profile;
396
397         /*
398          * blkio cgroup support
399          */
400         char *cgroup;
401         unsigned int cgroup_weight;
402         unsigned int cgroup_nodelete;
403
404         unsigned int uid;
405         unsigned int gid;
406
407         unsigned int sync_file_range;
408
409         unsigned int userspace_libaio_reap;
410 };
411
412 #define FIO_VERROR_SIZE 128
413
414 /*
415  * This describes a single thread/process executing a fio job.
416  */
417 struct thread_data {
418         struct thread_options o;
419         char verror[FIO_VERROR_SIZE];
420         pthread_t thread;
421         int thread_number;
422         int groupid;
423         struct thread_stat ts;
424
425         struct io_log *slat_log;
426         struct io_log *clat_log;
427         struct io_log *lat_log;
428         struct io_log *bw_log;
429
430         uint64_t stat_io_bytes[2];
431         struct timeval stat_sample_time[2];
432
433         struct rusage ru_start;
434         struct rusage ru_end;
435
436         struct fio_file **files;
437         unsigned int files_size;
438         unsigned int files_index;
439         unsigned int nr_open_files;
440         unsigned int nr_done_files;
441         unsigned int nr_normal_files;
442         union {
443                 unsigned int next_file;
444                 os_random_state_t next_file_state;
445                 struct frand_state __next_file_state;
446         };
447         int error;
448         int done;
449         pid_t pid;
450         char *orig_buffer;
451         size_t orig_buffer_size;
452         volatile int terminate;
453         volatile int runstate;
454         unsigned int ioprio;
455         unsigned int ioprio_set;
456         unsigned int last_was_sync;
457         enum fio_ddir last_ddir;
458
459         char *mmapfile;
460         int mmapfd;
461
462         void *iolog_buf;
463         FILE *iolog_f;
464
465         char *sysfs_root;
466
467         unsigned long rand_seeds[8];
468
469         union {
470                 os_random_state_t bsrange_state;
471                 struct frand_state __bsrange_state;
472         };
473         union {
474                 os_random_state_t verify_state;
475                 struct frand_state __verify_state;
476         };
477         union {
478                 os_random_state_t trim_state;
479                 struct frand_state __trim_state;
480         };
481
482         struct frand_state buf_state;
483
484         unsigned int verify_batch;
485         unsigned int trim_batch;
486
487         int shm_id;
488
489         /*
490          * IO engine hooks, contains everything needed to submit an io_u
491          * to any of the available IO engines.
492          */
493         struct ioengine_ops *io_ops;
494
495         /*
496          * Current IO depth and list of free and busy io_u's.
497          */
498         unsigned int cur_depth;
499         unsigned int io_u_queued;
500         struct flist_head io_u_freelist;
501         struct flist_head io_u_busylist;
502         struct flist_head io_u_requeues;
503         pthread_mutex_t io_u_lock;
504         pthread_cond_t free_cond;
505
506         /*
507          * async verify offload
508          */
509         struct flist_head verify_list;
510         pthread_t *verify_threads;
511         unsigned int nr_verify_threads;
512         pthread_cond_t verify_cond;
513         int verify_thread_exit;
514
515         /*
516          * Rate state
517          */
518         unsigned long rate_nsec_cycle[2];
519         long rate_pending_usleep[2];
520         unsigned long rate_bytes[2];
521         unsigned long rate_blocks[2];
522         struct timeval lastrate[2];
523
524         unsigned long long total_io_size;
525         unsigned long long fill_device_size;
526
527         unsigned long io_issues[2];
528         unsigned long long io_blocks[2];
529         unsigned long long io_bytes[2];
530         unsigned long long io_skip_bytes;
531         unsigned long long this_io_bytes[2];
532         unsigned long long zone_bytes;
533         struct fio_mutex *mutex;
534
535         /*
536          * State for random io, a bitmap of blocks done vs not done
537          */
538         union {
539                 os_random_state_t random_state;
540                 struct frand_state __random_state;
541         };
542
543         struct timeval start;   /* start of this loop */
544         struct timeval epoch;   /* time job was started */
545         struct timeval last_issue;
546         struct timeval tv_cache;
547         unsigned int tv_cache_nr;
548         unsigned int tv_cache_mask;
549         unsigned int ramp_time_over;
550
551         /*
552          * read/write mixed workload state
553          */
554         union {
555                 os_random_state_t rwmix_state;
556                 struct frand_state __rwmix_state;
557         };
558         unsigned long rwmix_issues;
559         enum fio_ddir rwmix_ddir;
560         unsigned int ddir_seq_nr;
561
562         /*
563          * IO history logs for verification. We use a tree for sorting,
564          * if we are overwriting. Otherwise just use a fifo.
565          */
566         struct rb_root io_hist_tree;
567         struct flist_head io_hist_list;
568         unsigned long io_hist_len;
569
570         /*
571          * For IO replaying
572          */
573         struct flist_head io_log_list;
574
575         /*
576          * For tracking/handling discards
577          */
578         struct flist_head trim_list;
579         unsigned long trim_entries;
580
581         /*
582          * for fileservice, how often to switch to a new file
583          */
584         unsigned int file_service_nr;
585         unsigned int file_service_left;
586         struct fio_file *file_service_file;
587
588         unsigned int sync_file_range_nr;
589
590         /*
591          * For generating file sizes
592          */
593         union {
594                 os_random_state_t file_size_state;
595                 struct frand_state __file_size_state;
596         };
597
598         /*
599          * Error counts
600          */
601         unsigned int total_err_count;
602         int first_error;
603
604         /*
605          * Can be overloaded by profiles
606          */
607         struct prof_io_ops prof_io_ops;
608         void *prof_data;
609 };
610
611 /*
612  * when should interactive ETA output be generated
613  */
614 enum {
615         FIO_ETA_AUTO,
616         FIO_ETA_ALWAYS,
617         FIO_ETA_NEVER,
618 };
619
620 #define __td_verror(td, err, msg, func)                                 \
621         do {                                                            \
622                 if ((td)->error)                                        \
623                         break;                                          \
624                 int e = (err);                                          \
625                 (td)->error = e;                                        \
626                 if (!(td)->first_error)                                 \
627                         snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg));               \
628         } while (0)
629
630
631 #define td_clear_error(td)              \
632         (td)->error = 0;
633 #define td_verror(td, err, func)        \
634         __td_verror((td), (err), strerror((err)), (func))
635 #define td_vmsg(td, err, msg, func)     \
636         __td_verror((td), (err), (msg), (func))
637
638 extern int exitall_on_terminate;
639 extern int thread_number;
640 extern int nr_process, nr_thread;
641 extern int shm_id;
642 extern int groupid;
643 extern int terse_output;
644 extern int temp_stall_ts;
645 extern unsigned long long mlock_size;
646 extern unsigned long page_mask, page_size;
647 extern int read_only;
648 extern int eta_print;
649 extern unsigned long done_secs;
650 extern char *job_section;
651 extern int fio_gtod_offload;
652 extern int fio_gtod_cpu;
653 extern enum fio_cs fio_clock_source;
654 extern int warnings_fatal;
655 extern int terse_version;
656 extern int is_backend;
657 extern int nr_clients;
658 extern int log_syslog;
659
660 extern struct thread_data *threads;
661
662 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
663 {
664         assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
665 }
666
667 #define BLOCKS_PER_MAP          (8 * sizeof(unsigned long))
668 #define TO_MAP_BLOCK(f, b)      (b)
669 #define RAND_MAP_IDX(f, b)      (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
670 #define RAND_MAP_BIT(f, b)      (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
671
672 #define REAL_MAX_JOBS           2048
673
674 #define td_non_fatal_error(e)   ((e) == EIO || (e) == EILSEQ)
675
676 static inline void update_error_count(struct thread_data *td, int err)
677 {
678         td->total_err_count++;
679         if (td->total_err_count == 1)
680                 td->first_error = err;
681 }
682
683 static inline int should_fsync(struct thread_data *td)
684 {
685         if (td->last_was_sync)
686                 return 0;
687         if (td->o.odirect)
688                 return 0;
689         if (td_write(td) || td_rw(td) || td->o.override_sync)
690                 return 1;
691
692         return 0;
693 }
694
695 /*
696  * Init/option functions
697  */
698 extern int __must_check parse_options(int, char **);
699 extern int parse_jobs_ini(char *, int, int);
700 extern int exec_run(void);
701 extern void reset_fio_state(void);
702 extern int fio_options_parse(struct thread_data *, char **, int);
703 extern void fio_keywords_init(void);
704 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
705 extern void fio_fill_default_options(struct thread_data *);
706 extern int fio_show_option_help(const char *);
707 extern void fio_options_dup_and_init(struct option *);
708 extern void options_mem_dupe(struct thread_data *);
709 extern void options_mem_free(struct thread_data *);
710 extern void td_fill_rand_seeds(struct thread_data *);
711 extern void add_job_opts(const char **);
712 extern char *num2str(unsigned long, int, int, int);
713
714 #define FIO_GETOPT_JOB          0x89988998
715 #define FIO_NR_OPTIONS          (FIO_MAX_OPTS + 128)
716
717 /*
718  * ETA/status stuff
719  */
720 extern void print_thread_status(void);
721 extern void print_status_init(int);
722
723 /*
724  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
725  * will never back again. It may cycle between running/verififying/fsyncing.
726  * Once the thread reaches TD_EXITED, it is just waiting for the core to
727  * reap it.
728  */
729 enum {
730         TD_NOT_CREATED = 0,
731         TD_CREATED,
732         TD_INITIALIZED,
733         TD_RAMP,
734         TD_RUNNING,
735         TD_PRE_READING,
736         TD_VERIFYING,
737         TD_FSYNCING,
738         TD_EXITED,
739         TD_REAPED,
740 };
741
742 extern void td_set_runstate(struct thread_data *, int);
743
744 /*
745  * Memory helpers
746  */
747 extern int __must_check fio_pin_memory(void);
748 extern void fio_unpin_memory(void);
749 extern int __must_check allocate_io_mem(struct thread_data *);
750 extern void free_io_mem(struct thread_data *);
751
752 /*
753  * Reset stats after ramp time completes
754  */
755 extern void reset_all_stats(struct thread_data *);
756
757 /*
758  * blktrace support
759  */
760 #ifdef FIO_HAVE_BLKTRACE
761 extern int is_blktrace(const char *);
762 extern int load_blktrace(struct thread_data *, const char *);
763 #endif
764
765 /*
766  * Mark unused variables passed to ops functions as unused, to silence gcc
767  */
768 #define fio_unused      __attribute((__unused__))
769 #define fio_init        __attribute__((constructor))
770 #define fio_exit        __attribute__((destructor))
771
772 #define for_each_td(td, i)      \
773         for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
774 #define for_each_file(td, f, i) \
775         if ((td)->files_index)                                          \
776                 for ((i) = 0, (f) = (td)->files[0];                     \
777                  (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
778                  (i)++)
779
780 #define fio_assert(td, cond)    do {    \
781         if (!(cond)) {                  \
782                 int *__foo = NULL;      \
783                 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond);   \
784                 td_set_runstate((td), TD_EXITED);       \
785                 (td)->error = EFAULT;           \
786                 *__foo = 0;                     \
787         }       \
788 } while (0)
789
790 static inline int fio_fill_issue_time(struct thread_data *td)
791 {
792         if (td->o.read_iolog_file ||
793             !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
794                 return 1;
795
796         return 0;
797 }
798
799 static inline int __should_check_rate(struct thread_data *td,
800                                       enum fio_ddir ddir)
801 {
802         struct thread_options *o = &td->o;
803
804         /*
805          * If some rate setting was given, we need to check it
806          */
807         if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
808             o->rate_iops_min[ddir])
809                 return 1;
810
811         return 0;
812 }
813
814 static inline int should_check_rate(struct thread_data *td,
815                                     unsigned long *bytes_done)
816 {
817         int ret = 0;
818
819         if (bytes_done[0])
820                 ret |= __should_check_rate(td, 0);
821         if (bytes_done[1])
822                 ret |= __should_check_rate(td, 1);
823
824         return ret;
825 }
826
827 static inline int is_power_of_2(unsigned int val)
828 {
829         return (val != 0 && ((val & (val - 1)) == 0));
830 }
831
832 /*
833  * We currently only need to do locking if we have verifier threads
834  * accessing our internal structures too
835  */
836 static inline void td_io_u_lock(struct thread_data *td)
837 {
838         if (td->o.verify_async)
839                 pthread_mutex_lock(&td->io_u_lock);
840 }
841
842 static inline void td_io_u_unlock(struct thread_data *td)
843 {
844         if (td->o.verify_async)
845                 pthread_mutex_unlock(&td->io_u_lock);
846 }
847
848 static inline void td_io_u_free_notify(struct thread_data *td)
849 {
850         if (td->o.verify_async)
851                 pthread_cond_signal(&td->free_cond);
852 }
853
854 #endif