Add helpers include file
[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 <getopt.h>
15 #include <inttypes.h>
16 #include <assert.h>
17
18 #include "compiler/compiler.h"
19 #include "flist.h"
20 #include "fifo.h"
21 #include "rbtree.h"
22 #include "arch/arch.h"
23 #include "os/os.h"
24 #include "mutex.h"
25 #include "log.h"
26 #include "debug.h"
27 #include "file.h"
28 #include "io_ddir.h"
29 #include "ioengine.h"
30 #include "iolog.h"
31 #include "profiles.h"
32 #include "helpers.h"
33
34 #ifdef FIO_HAVE_GUASI
35 #include <guasi.h>
36 #endif
37
38 #ifdef FIO_HAVE_SOLARISAIO
39 #include <sys/asynch.h>
40 #endif
41
42 struct group_run_stats {
43         unsigned long long max_run[2], min_run[2];
44         unsigned long long max_bw[2], min_bw[2];
45         unsigned long long io_kb[2];
46         unsigned long long agg[2];
47         unsigned int kb_base;
48 };
49
50 /*
51  * What type of allocation to use for io buffers
52  */
53 enum fio_memtype {
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 */
59 };
60
61 /*
62  * How many depth levels to log
63  */
64 #define FIO_IO_U_MAP_NR 8
65 #define FIO_IO_U_LAT_U_NR 10
66 #define FIO_IO_U_LAT_M_NR 12
67
68 #define MAX_PATTERN_SIZE 512
69
70 struct thread_stat {
71         char *name;
72         char *verror;
73         int error;
74         int groupid;
75         pid_t pid;
76         char *description;
77         int members;
78
79         struct io_log *slat_log;
80         struct io_log *clat_log;
81         struct io_log *bw_log;
82
83         /*
84          * bandwidth and latency stats
85          */
86         struct io_stat clat_stat[2];            /* completion latency */
87         struct io_stat slat_stat[2];            /* submission latency */
88         struct io_stat bw_stat[2];              /* bandwidth stats */
89
90         unsigned long long stat_io_bytes[2];
91         struct timeval stat_sample_time[2];
92
93         /*
94          * fio system usage accounting
95          */
96         struct rusage ru_start;
97         struct rusage ru_end;
98         unsigned long usr_time;
99         unsigned long sys_time;
100         unsigned long ctx;
101         unsigned long minf, majf;
102
103         /*
104          * IO depth and latency stats
105          */
106         unsigned int io_u_map[FIO_IO_U_MAP_NR];
107         unsigned int io_u_submit[FIO_IO_U_MAP_NR];
108         unsigned int io_u_complete[FIO_IO_U_MAP_NR];
109         unsigned int io_u_lat_u[FIO_IO_U_LAT_U_NR];
110         unsigned int io_u_lat_m[FIO_IO_U_LAT_M_NR];
111         unsigned long total_io_u[2];
112         unsigned long short_io_u[2];
113         unsigned long total_submit;
114         unsigned long total_complete;
115
116         unsigned long long io_bytes[2];
117         unsigned long runtime[2];
118         unsigned long total_run_time;
119
120         /*
121          * IO Error related stats
122          */
123         unsigned continue_on_error;
124         unsigned long total_err_count;
125         int first_error;
126
127         unsigned int kb_base;
128 };
129
130 struct bssplit {
131         unsigned int bs;
132         unsigned char perc;
133 };
134
135 struct thread_options {
136         int pad;
137         char *description;
138         char *name;
139         char *directory;
140         char *filename;
141         char *opendir;
142         char *ioengine;
143         enum td_ddir td_ddir;
144         unsigned int kb_base;
145         unsigned int ddir_nr;
146         unsigned int iodepth;
147         unsigned int iodepth_low;
148         unsigned int iodepth_batch;
149         unsigned int iodepth_batch_complete;
150
151         unsigned long long size;
152         unsigned int fill_device;
153         unsigned long long file_size_low;
154         unsigned long long file_size_high;
155         unsigned long long start_offset;
156
157         unsigned int bs[2];
158         unsigned int ba[2];
159         unsigned int min_bs[2];
160         unsigned int max_bs[2];
161         struct bssplit *bssplit[2];
162         unsigned int bssplit_nr[2];
163
164         unsigned int nr_files;
165         unsigned int open_files;
166         enum file_lock_mode file_lock_mode;
167         unsigned int lockfile_batch;
168
169         unsigned int odirect;
170         unsigned int invalidate_cache;
171         unsigned int create_serialize;
172         unsigned int create_fsync;
173         unsigned int create_on_open;
174         unsigned int end_fsync;
175         unsigned int pre_read;
176         unsigned int sync_io;
177         unsigned int verify;
178         unsigned int do_verify;
179         unsigned int verifysort;
180         unsigned int verify_interval;
181         unsigned int verify_offset;
182         char verify_pattern[MAX_PATTERN_SIZE];
183         unsigned int verify_pattern_bytes;
184         unsigned int verify_fatal;
185         unsigned int verify_async;
186         unsigned int use_thread;
187         unsigned int unlink;
188         unsigned int do_disk_util;
189         unsigned int override_sync;
190         unsigned int rand_repeatable;
191         unsigned int write_lat_log;
192         unsigned int write_bw_log;
193         unsigned int norandommap;
194         unsigned int softrandommap;
195         unsigned int bs_unaligned;
196         unsigned int fsync_on_close;
197
198         unsigned int hugepage_size;
199         unsigned int rw_min_bs;
200         unsigned int thinktime;
201         unsigned int thinktime_spin;
202         unsigned int thinktime_blocks;
203         unsigned int fsync_blocks;
204         unsigned int fdatasync_blocks;
205         unsigned int start_delay;
206         unsigned long long timeout;
207         unsigned long long ramp_time;
208         unsigned int overwrite;
209         unsigned int bw_avg_time;
210         unsigned int loops;
211         unsigned long long zone_size;
212         unsigned long long zone_skip;
213         enum fio_memtype mem_type;
214         unsigned int mem_align;
215
216         unsigned int stonewall;
217         unsigned int new_group;
218         unsigned int numjobs;
219         os_cpu_mask_t cpumask;
220         unsigned int cpumask_set;
221         os_cpu_mask_t verify_cpumask;
222         unsigned int verify_cpumask_set;
223         unsigned int iolog;
224         unsigned int rwmixcycle;
225         unsigned int rwmix[2];
226         unsigned int nice;
227         unsigned int file_service_type;
228         unsigned int group_reporting;
229         unsigned int fadvise_hint;
230         unsigned int zero_buffers;
231         unsigned int refill_buffers;
232         unsigned int time_based;
233         unsigned int disable_clat;
234         unsigned int disable_slat;
235         unsigned int disable_bw;
236         unsigned int gtod_reduce;
237         unsigned int gtod_cpu;
238         unsigned int gtod_offload;
239
240         char *read_iolog_file;
241         char *write_iolog_file;
242         char *bw_log_file;
243         char *lat_log_file;
244
245         /*
246          * Pre-run and post-run shell
247          */
248         char *exec_prerun;
249         char *exec_postrun;
250
251         unsigned int rate[2];
252         unsigned int ratemin[2];
253         unsigned int ratecycle;
254         unsigned int rate_iops[2];
255         unsigned int rate_iops_min[2];
256
257         char *ioscheduler;
258
259         /*
260          * CPU "io" cycle burner
261          */
262         unsigned int cpuload;
263         unsigned int cpucycle;
264
265         /*
266          * I/O Error handling
267          */
268         unsigned int continue_on_error;
269
270         /*
271          * Benchmark profile type
272          */
273         unsigned int profile;
274 };
275
276 #define FIO_VERROR_SIZE 128
277
278 /*
279  * This describes a single thread/process executing a fio job.
280  */
281 struct thread_data {
282         struct thread_options o;
283         char verror[FIO_VERROR_SIZE];
284         pthread_t thread;
285         int thread_number;
286         int groupid;
287         struct thread_stat ts;
288         struct fio_file **files;
289         unsigned int files_size;
290         unsigned int files_index;
291         unsigned int nr_open_files;
292         unsigned int nr_done_files;
293         unsigned int nr_normal_files;
294         union {
295                 unsigned int next_file;
296                 os_random_state_t next_file_state;
297         };
298         int error;
299         int done;
300         pid_t pid;
301         char *orig_buffer;
302         size_t orig_buffer_size;
303         volatile int terminate;
304         volatile int runstate;
305         unsigned int ioprio;
306         unsigned int ioprio_set;
307         unsigned int last_was_sync;
308
309         char *mmapfile;
310         int mmapfd;
311
312         void *iolog_buf;
313         FILE *iolog_f;
314
315         char *sysfs_root;
316
317         unsigned long rand_seeds[6];
318
319         os_random_state_t bsrange_state;
320         os_random_state_t verify_state;
321
322         int shm_id;
323
324         /*
325          * IO engine hooks, contains everything needed to submit an io_u
326          * to any of the available IO engines.
327          */
328         struct ioengine_ops *io_ops;
329
330         /*
331          * Current IO depth and list of free and busy io_u's.
332          */
333         unsigned int cur_depth;
334         unsigned int io_u_queued;
335         struct flist_head io_u_freelist;
336         struct flist_head io_u_busylist;
337         struct flist_head io_u_requeues;
338         pthread_mutex_t io_u_lock;
339         pthread_cond_t free_cond;
340
341         /*
342          * async verify offload
343          */
344         struct flist_head verify_list;
345         pthread_t *verify_threads;
346         unsigned int nr_verify_threads;
347         pthread_cond_t verify_cond;
348         int verify_thread_exit;
349
350         /*
351          * Rate state
352          */
353         unsigned long rate_usec_cycle[2];
354         long rate_pending_usleep[2];
355         unsigned long rate_bytes[2];
356         unsigned long rate_blocks[2];
357         struct timeval lastrate[2];
358
359         unsigned long long total_io_size;
360
361         unsigned long io_issues[2];
362         unsigned long long io_blocks[2];
363         unsigned long long io_bytes[2];
364         unsigned long long io_skip_bytes;
365         unsigned long long this_io_bytes[2];
366         unsigned long long zone_bytes;
367         struct fio_mutex *mutex;
368
369         /*
370          * State for random io, a bitmap of blocks done vs not done
371          */
372         os_random_state_t random_state;
373
374         struct timeval start;   /* start of this loop */
375         struct timeval epoch;   /* time job was started */
376         struct timeval last_issue;
377         struct timeval tv_cache;
378         unsigned int tv_cache_nr;
379         unsigned int tv_cache_mask;
380         unsigned int ramp_time_over;
381
382         /*
383          * read/write mixed workload state
384          */
385         os_random_state_t rwmix_state;
386         unsigned long rwmix_issues;
387         enum fio_ddir rwmix_ddir;
388         unsigned int ddir_nr;
389
390         /*
391          * IO history logs for verification. We use a tree for sorting,
392          * if we are overwriting. Otherwise just use a fifo.
393          */
394         struct rb_root io_hist_tree;
395         struct flist_head io_hist_list;
396
397         /*
398          * For IO replaying
399          */
400         struct flist_head io_log_list;
401
402         /*
403          * for fileservice, how often to switch to a new file
404          */
405         unsigned int file_service_nr;
406         unsigned int file_service_left;
407         struct fio_file *file_service_file;
408
409         /*
410          * For generating file sizes
411          */
412         os_random_state_t file_size_state;
413
414         /*
415          * Error counts
416          */
417         unsigned int total_err_count;
418         int first_error;
419 };
420
421 /*
422  * when should interactive ETA output be generated
423  */
424 enum {
425         FIO_ETA_AUTO,
426         FIO_ETA_ALWAYS,
427         FIO_ETA_NEVER,
428 };
429
430 #define __td_verror(td, err, msg, func)                                 \
431         do {                                                            \
432                 if ((td)->error)                                        \
433                         break;                                          \
434                 int e = (err);                                          \
435                 (td)->error = e;                                        \
436                 if (!(td)->first_error)                                 \
437                         snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg));               \
438         } while (0)
439
440
441 #define td_clear_error(td)              \
442         (td)->error = 0;
443 #define td_verror(td, err, func)        \
444         __td_verror((td), (err), strerror((err)), (func))
445 #define td_vmsg(td, err, msg, func)     \
446         __td_verror((td), (err), (msg), (func))
447
448 extern int exitall_on_terminate;
449 extern int thread_number;
450 extern int nr_process, nr_thread;
451 extern int shm_id;
452 extern int groupid;
453 extern int terse_output;
454 extern int temp_stall_ts;
455 extern unsigned long long mlock_size;
456 extern unsigned long page_mask, page_size;
457 extern int read_only;
458 extern int eta_print;
459 extern unsigned long done_secs;
460 extern char *job_section;
461 extern int fio_gtod_offload;
462 extern int fio_gtod_cpu;
463
464 extern struct thread_data *threads;
465
466 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
467 {
468         assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
469 }
470
471 #define BLOCKS_PER_MAP          (8 * sizeof(int))
472 #define TO_MAP_BLOCK(f, b)      (b)
473 #define RAND_MAP_IDX(f, b)      (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
474 #define RAND_MAP_BIT(f, b)      (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
475
476 #define MAX_JOBS        (1024)
477
478 #define td_non_fatal_error(e)   ((e) == EIO || (e) == EILSEQ)
479
480 static inline void update_error_count(struct thread_data *td, int err)
481 {
482         td->total_err_count++;
483         if (td->total_err_count == 1)
484                 td->first_error = err;
485 }
486
487 static inline int should_fsync(struct thread_data *td)
488 {
489         if (td->last_was_sync)
490                 return 0;
491         if (td->o.odirect)
492                 return 0;
493         if (td_write(td) || td_rw(td) || td->o.override_sync)
494                 return 1;
495
496         return 0;
497 }
498
499 /*
500  * Time functions
501  */
502 extern unsigned long long utime_since(struct timeval *, struct timeval *);
503 extern unsigned long long utime_since_now(struct timeval *);
504 extern unsigned long mtime_since(struct timeval *, struct timeval *);
505 extern unsigned long mtime_since_now(struct timeval *);
506 extern unsigned long time_since_now(struct timeval *);
507 extern unsigned long mtime_since_genesis(void);
508 extern void usec_spin(unsigned int);
509 extern void usec_sleep(struct thread_data *, unsigned long);
510 extern void fill_start_time(struct timeval *);
511 extern void fio_gettime(struct timeval *, void *);
512 extern void fio_gtod_init(void);
513 extern void fio_gtod_update(void);
514 extern void set_genesis_time(void);
515 extern int ramp_time_over(struct thread_data *);
516 extern int in_ramp_time(struct thread_data *);
517
518 /*
519  * Init/option functions
520  */
521 extern int __must_check parse_options(int, char **);
522 extern int fio_options_parse(struct thread_data *, char **, int);
523 extern void fio_keywords_init(void);
524 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
525 extern void fio_fill_default_options(struct thread_data *);
526 extern int fio_show_option_help(const char *);
527 extern void fio_options_dup_and_init(struct option *);
528 extern void options_mem_dupe(struct thread_data *);
529 extern void options_mem_free(struct thread_data *);
530 extern void td_fill_rand_seeds(struct thread_data *);
531 #define FIO_GETOPT_JOB          0x89988998
532 #define FIO_NR_OPTIONS          128
533
534 /*
535  * ETA/status stuff
536  */
537 extern void print_thread_status(void);
538 extern void print_status_init(int);
539
540 /*
541  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
542  * will never back again. It may cycle between running/verififying/fsyncing.
543  * Once the thread reaches TD_EXITED, it is just waiting for the core to
544  * reap it.
545  */
546 enum {
547         TD_NOT_CREATED = 0,
548         TD_CREATED,
549         TD_INITIALIZED,
550         TD_RAMP,
551         TD_RUNNING,
552         TD_PRE_READING,
553         TD_VERIFYING,
554         TD_FSYNCING,
555         TD_EXITED,
556         TD_REAPED,
557 };
558
559 extern void td_set_runstate(struct thread_data *, int);
560
561 /*
562  * Memory helpers
563  */
564 extern int __must_check fio_pin_memory(void);
565 extern void fio_unpin_memory(void);
566 extern int __must_check allocate_io_mem(struct thread_data *);
567 extern void free_io_mem(struct thread_data *);
568
569 /*
570  * Reset stats after ramp time completes
571  */
572 extern void reset_all_stats(struct thread_data *);
573
574 /*
575  * blktrace support
576  */
577 #ifdef FIO_HAVE_BLKTRACE
578 extern int is_blktrace(const char *);
579 extern int load_blktrace(struct thread_data *, const char *);
580 #endif
581
582 /*
583  * Mark unused variables passed to ops functions as unused, to silence gcc
584  */
585 #define fio_unused      __attribute((__unused__))
586 #define fio_init        __attribute__((constructor))
587 #define fio_exit        __attribute__((destructor))
588
589 #define for_each_td(td, i)      \
590         for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
591 #define for_each_file(td, f, i) \
592         if ((td)->files_index)                                          \
593                 for ((i) = 0, (f) = (td)->files[0];                     \
594                  (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
595                  (i)++)
596
597 #define fio_assert(td, cond)    do {    \
598         if (!(cond)) {                  \
599                 int *__foo = NULL;      \
600                 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond);   \
601                 (td)->runstate = TD_EXITED;     \
602                 (td)->error = EFAULT;           \
603                 *__foo = 0;                     \
604         }       \
605 } while (0)
606
607 static inline int fio_fill_issue_time(struct thread_data *td)
608 {
609         if (td->o.read_iolog_file ||
610             !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
611                 return 1;
612
613         return 0;
614 }
615
616 /*
617  * Cheesy number->string conversion, complete with carry rounding error.
618  */
619 static inline char *num2str(unsigned long num, int maxlen, int base, int pow2)
620 {
621         char postfix[] = { ' ', 'K', 'M', 'G', 'P', 'E' };
622         unsigned int thousand;
623         char *buf;
624         int i;
625
626         if (pow2)
627                 thousand = 1024;
628         else
629                 thousand = 1000;
630
631         buf = malloc(128);
632
633         for (i = 0; base > 1; i++)
634                 base /= thousand;
635
636         do {
637                 int len, carry = 0;
638
639                 len = sprintf(buf, "%'lu", num);
640                 if (len <= maxlen) {
641                         if (i >= 1) {
642                                 buf[len] = postfix[i];
643                                 buf[len + 1] = '\0';
644                         }
645                         return buf;
646                 }
647
648                 if ((num % thousand) >= (thousand / 2))
649                         carry = 1;
650
651                 num /= thousand;
652                 num += carry;
653                 i++;
654         } while (i <= 5);
655
656         return buf;
657 }
658
659 static inline int __should_check_rate(struct thread_data *td,
660                                       enum fio_ddir ddir)
661 {
662         struct thread_options *o = &td->o;
663
664         /*
665          * If some rate setting was given, we need to check it
666          */
667         if (o->rate[ddir] || o->ratemin[ddir] || o->rate_iops[ddir] ||
668             o->rate_iops_min[ddir])
669                 return 1;
670
671         return 0;
672 }
673
674 static inline int should_check_rate(struct thread_data *td,
675                                     unsigned long *bytes_done)
676 {
677         int ret = 0;
678
679         if (bytes_done[0])
680                 ret |= __should_check_rate(td, 0);
681         if (bytes_done[1])
682                 ret |= __should_check_rate(td, 1);
683
684         return ret;
685 }
686
687 static inline int is_power_of_2(unsigned int val)
688 {
689         return (val != 0 && ((val & (val - 1)) == 0));
690 }
691
692 /*
693  * We currently only need to do locking if we have verifier threads
694  * accessing our internal structures too
695  */
696 static inline void td_io_u_lock(struct thread_data *td)
697 {
698         if (td->o.verify_async)
699                 pthread_mutex_lock(&td->io_u_lock);
700 }
701
702 static inline void td_io_u_unlock(struct thread_data *td)
703 {
704         if (td->o.verify_async)
705                 pthread_mutex_unlock(&td->io_u_lock);
706 }
707
708 static inline void td_io_u_free_notify(struct thread_data *td)
709 {
710         if (td->o.verify_async)
711                 pthread_cond_signal(&td->free_cond);
712 }
713
714 #endif