Move IO engine flags
[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
31 #ifdef FIO_HAVE_GUASI
32 #include <guasi.h>
33 #endif
34
35 #ifdef FIO_HAVE_SOLARISAIO
36 #include <sys/asynch.h>
37 #endif
38
39 /*
40  * Use for maintaining statistics
41  */
42 struct io_stat {
43         unsigned long max_val;
44         unsigned long min_val;
45         unsigned long samples;
46
47         double mean;
48         double S;
49 };
50
51 /*
52  * A single data sample
53  */
54 struct io_sample {
55         unsigned long time;
56         unsigned long val;
57         enum fio_ddir ddir;
58         unsigned int bs;
59 };
60
61 /*
62  * Dynamically growing data sample log
63  */
64 struct io_log {
65         unsigned long nr_samples;
66         unsigned long max_samples;
67         struct io_sample *log;
68 };
69
70 /*
71  * When logging io actions, this matches a single sent io_u
72  */
73 struct io_piece {
74         union {
75                 struct rb_node rb_node;
76                 struct flist_head list;
77         };
78         union {
79                 int fileno;
80                 struct fio_file *file;
81         };
82         unsigned long long offset;
83         unsigned long len;
84         enum fio_ddir ddir;
85         union {
86                 unsigned long delay;
87                 unsigned int file_action;
88         };
89 };
90
91 struct group_run_stats {
92         unsigned long long max_run[2], min_run[2];
93         unsigned long long max_bw[2], min_bw[2];
94         unsigned long long io_kb[2];
95         unsigned long long agg[2];
96 };
97
98 /*
99  * What type of allocation to use for io buffers
100  */
101 enum fio_memtype {
102         MEM_MALLOC = 0, /* ordinary malloc */
103         MEM_SHM,        /* use shared memory segments */
104         MEM_SHMHUGE,    /* use shared memory segments with huge pages */
105         MEM_MMAP,       /* use anonynomous mmap */
106         MEM_MMAPHUGE,   /* memory mapped huge file */
107 };
108
109 /*
110  * How many depth levels to log
111  */
112 #define FIO_IO_U_MAP_NR 8
113 #define FIO_IO_U_LAT_U_NR 10
114 #define FIO_IO_U_LAT_M_NR 12
115
116 struct thread_stat {
117         char *name;
118         char *verror;
119         int error;
120         int groupid;
121         pid_t pid;
122         char *description;
123         int members;
124
125         struct io_log *slat_log;
126         struct io_log *clat_log;
127         struct io_log *bw_log;
128
129         /*
130          * bandwidth and latency stats
131          */
132         struct io_stat clat_stat[2];            /* completion latency */
133         struct io_stat slat_stat[2];            /* submission latency */
134         struct io_stat bw_stat[2];              /* bandwidth stats */
135
136         unsigned long long stat_io_bytes[2];
137         struct timeval stat_sample_time[2];
138
139         /*
140          * fio system usage accounting
141          */
142         struct rusage ru_start;
143         struct rusage ru_end;
144         unsigned long usr_time;
145         unsigned long sys_time;
146         unsigned long ctx;
147         unsigned long minf, majf;
148
149         /*
150          * IO depth and latency stats
151          */
152         unsigned int io_u_map[FIO_IO_U_MAP_NR];
153         unsigned int io_u_submit[FIO_IO_U_MAP_NR];
154         unsigned int io_u_complete[FIO_IO_U_MAP_NR];
155         unsigned int io_u_lat_u[FIO_IO_U_LAT_U_NR];
156         unsigned int io_u_lat_m[FIO_IO_U_LAT_M_NR];
157         unsigned long total_io_u[2];
158         unsigned long short_io_u[2];
159         unsigned long total_submit;
160         unsigned long total_complete;
161
162         unsigned long long io_bytes[2];
163         unsigned long runtime[2];
164         unsigned long total_run_time;
165 };
166
167 struct bssplit {
168         unsigned int bs;
169         unsigned char perc;
170 };
171
172 struct thread_options {
173         int pad;
174         char *description;
175         char *name;
176         char *directory;
177         char *filename;
178         char *opendir;
179         char *ioengine;
180         enum td_ddir td_ddir;
181         unsigned int ddir_nr;
182         unsigned int iodepth;
183         unsigned int iodepth_low;
184         unsigned int iodepth_batch;
185         unsigned int iodepth_batch_complete;
186
187         unsigned long long size;
188         unsigned int fill_device;
189         unsigned long long file_size_low;
190         unsigned long long file_size_high;
191         unsigned long long start_offset;
192
193         unsigned int bs[2];
194         unsigned int ba[2];
195         unsigned int min_bs[2];
196         unsigned int max_bs[2];
197         struct bssplit *bssplit[2];
198         unsigned int bssplit_nr[2];
199
200         unsigned int nr_files;
201         unsigned int open_files;
202         enum file_lock_mode file_lock_mode;
203         unsigned int lockfile_batch;
204
205         unsigned int odirect;
206         unsigned int invalidate_cache;
207         unsigned int create_serialize;
208         unsigned int create_fsync;
209         unsigned int create_on_open;
210         unsigned int end_fsync;
211         unsigned int pre_read;
212         unsigned int sync_io;
213         unsigned int verify;
214         unsigned int do_verify;
215         unsigned int verifysort;
216         unsigned int verify_interval;
217         unsigned int verify_offset;
218         unsigned int verify_pattern;
219         unsigned int verify_pattern_bytes;
220         unsigned int verify_fatal;
221         unsigned int use_thread;
222         unsigned int unlink;
223         unsigned int do_disk_util;
224         unsigned int override_sync;
225         unsigned int rand_repeatable;
226         unsigned int write_lat_log;
227         unsigned int write_bw_log;
228         unsigned int norandommap;
229         unsigned int softrandommap;
230         unsigned int bs_unaligned;
231         unsigned int fsync_on_close;
232
233         unsigned int hugepage_size;
234         unsigned int rw_min_bs;
235         unsigned int thinktime;
236         unsigned int thinktime_spin;
237         unsigned int thinktime_blocks;
238         unsigned int fsync_blocks;
239         unsigned int start_delay;
240         unsigned long long timeout;
241         unsigned long long ramp_time;
242         unsigned int overwrite;
243         unsigned int bw_avg_time;
244         unsigned int loops;
245         unsigned long long zone_size;
246         unsigned long long zone_skip;
247         enum fio_memtype mem_type;
248
249         unsigned int stonewall;
250         unsigned int new_group;
251         unsigned int numjobs;
252         os_cpu_mask_t cpumask;
253         unsigned int cpumask_set;
254         unsigned int iolog;
255         unsigned int rwmixcycle;
256         unsigned int rwmix[2];
257         unsigned int nice;
258         unsigned int file_service_type;
259         unsigned int group_reporting;
260         unsigned int fadvise_hint;
261         unsigned int zero_buffers;
262         unsigned int refill_buffers;
263         unsigned int time_based;
264         unsigned int disable_clat;
265         unsigned int disable_slat;
266         unsigned int disable_bw;
267         unsigned int gtod_reduce;
268         unsigned int gtod_cpu;
269         unsigned int gtod_offload;
270
271         char *read_iolog_file;
272         char *write_iolog_file;
273         char *bw_log_file;
274         char *lat_log_file;
275
276         /*
277          * Pre-run and post-run shell
278          */
279         char *exec_prerun;
280         char *exec_postrun;
281
282         unsigned int rate;
283         unsigned int ratemin;
284         unsigned int ratecycle;
285         unsigned int rate_iops;
286         unsigned int rate_iops_min;
287
288         char *ioscheduler;
289
290         /*
291          * CPU "io" cycle burner
292          */
293         unsigned int cpuload;
294         unsigned int cpucycle;
295 };
296
297 #define FIO_VERROR_SIZE 128
298
299 /*
300  * This describes a single thread/process executing a fio job.
301  */
302 struct thread_data {
303         struct thread_options o;
304         char verror[FIO_VERROR_SIZE];
305         pthread_t thread;
306         int thread_number;
307         int groupid;
308         struct thread_stat ts;
309         struct fio_file **files;
310         unsigned int files_size;
311         unsigned int files_index;
312         unsigned int nr_open_files;
313         unsigned int nr_done_files;
314         unsigned int nr_normal_files;
315         union {
316                 unsigned int next_file;
317                 os_random_state_t next_file_state;
318         };
319         int error;
320         int done;
321         pid_t pid;
322         char *orig_buffer;
323         size_t orig_buffer_size;
324         volatile int terminate;
325         volatile int runstate;
326         unsigned int ioprio;
327         unsigned int ioprio_set;
328         unsigned int last_was_sync;
329
330         char *mmapfile;
331         int mmapfd;
332
333         void *iolog_buf;
334         FILE *iolog_f;
335
336         char *sysfs_root;
337
338         unsigned long rand_seeds[6];
339
340         os_random_state_t bsrange_state;
341         os_random_state_t verify_state;
342
343         int shm_id;
344
345         /*
346          * IO engine hooks, contains everything needed to submit an io_u
347          * to any of the available IO engines.
348          */
349         struct ioengine_ops *io_ops;
350
351         /*
352          * Current IO depth and list of free and busy io_u's.
353          */
354         unsigned int cur_depth;
355         unsigned int io_u_queued;
356         struct flist_head io_u_freelist;
357         struct flist_head io_u_busylist;
358         struct flist_head io_u_requeues;
359
360         /*
361          * Rate state
362          */
363         unsigned long rate_usec_cycle;
364         long rate_pending_usleep;
365         unsigned long rate_bytes;
366         unsigned long rate_blocks;
367         struct timeval lastrate;
368
369         unsigned long long total_io_size;
370
371         unsigned long io_issues[2];
372         unsigned long long io_blocks[2];
373         unsigned long long io_bytes[2];
374         unsigned long long io_skip_bytes;
375         unsigned long long this_io_bytes[2];
376         unsigned long long zone_bytes;
377         struct fio_mutex *mutex;
378
379         /*
380          * State for random io, a bitmap of blocks done vs not done
381          */
382         os_random_state_t random_state;
383
384         struct timeval start;   /* start of this loop */
385         struct timeval epoch;   /* time job was started */
386         struct timeval rw_end[2];
387         struct timeval last_issue;
388         struct timeval tv_cache;
389         unsigned int tv_cache_nr;
390         unsigned int tv_cache_mask;
391         unsigned int rw_end_set[2];
392         unsigned int ramp_time_over;
393
394         /*
395          * read/write mixed workload state
396          */
397         os_random_state_t rwmix_state;
398         unsigned long rwmix_issues;
399         enum fio_ddir rwmix_ddir;
400         unsigned int ddir_nr;
401
402         /*
403          * IO history logs for verification. We use a tree for sorting,
404          * if we are overwriting. Otherwise just use a fifo.
405          */
406         struct rb_root io_hist_tree;
407         struct flist_head io_hist_list;
408
409         /*
410          * For IO replaying
411          */
412         struct flist_head io_log_list;
413
414         /*
415          * for fileservice, how often to switch to a new file
416          */
417         unsigned int file_service_nr;
418         unsigned int file_service_left;
419         struct fio_file *file_service_file;
420
421         /*
422          * For generating file sizes
423          */
424         os_random_state_t file_size_state;
425 };
426
427 /*
428  * roundrobin available files, or choose one at random, or do each one
429  * serially.
430  */
431 enum {
432         FIO_FSERVICE_RANDOM     = 1,
433         FIO_FSERVICE_RR         = 2,
434         FIO_FSERVICE_SEQ        = 3,
435 };
436
437 /*
438  * when should interactive ETA output be generated
439  */
440 enum {
441         FIO_ETA_AUTO,
442         FIO_ETA_ALWAYS,
443         FIO_ETA_NEVER,
444 };
445
446 #define __td_verror(td, err, msg, func)                                 \
447         do {                                                            \
448                 if ((td)->error)                                        \
449                         break;                                          \
450                 int e = (err);                                          \
451                 (td)->error = e;                                        \
452                 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg));       \
453         } while (0)
454
455
456 #define td_verror(td, err, func)        \
457         __td_verror((td), (err), strerror((err)), (func))
458 #define td_vmsg(td, err, msg, func)     \
459         __td_verror((td), (err), (msg), (func))
460
461 extern int exitall_on_terminate;
462 extern int thread_number;
463 extern int nr_process, nr_thread;
464 extern int shm_id;
465 extern int groupid;
466 extern int terse_output;
467 extern int temp_stall_ts;
468 extern unsigned long long mlock_size;
469 extern unsigned long page_mask, page_size;
470 extern int read_only;
471 extern int eta_print;
472 extern unsigned long done_secs;
473 extern char *job_section;
474 extern int fio_gtod_offload;
475 extern int fio_gtod_cpu;
476
477 extern struct thread_data *threads;
478
479 #define td_read(td)             ((td)->o.td_ddir & TD_DDIR_READ)
480 #define td_write(td)            ((td)->o.td_ddir & TD_DDIR_WRITE)
481 #define td_rw(td)               (((td)->o.td_ddir & TD_DDIR_RW) == TD_DDIR_RW)
482 #define td_random(td)           ((td)->o.td_ddir & TD_DDIR_RAND)
483 #define file_randommap(td, f)   (!(td)->o.norandommap && (f)->file_map)
484
485 static inline void fio_ro_check(struct thread_data *td, struct io_u *io_u)
486 {
487         assert(!(io_u->ddir == DDIR_WRITE && !td_write(td)));
488 }
489
490 #define BLOCKS_PER_MAP          (8 * sizeof(int))
491 #define TO_MAP_BLOCK(f, b)      (b)
492 #define RAND_MAP_IDX(f, b)      (TO_MAP_BLOCK(f, b) / BLOCKS_PER_MAP)
493 #define RAND_MAP_BIT(f, b)      (TO_MAP_BLOCK(f, b) & (BLOCKS_PER_MAP - 1))
494
495 #define MAX_JOBS        (1024)
496
497 static inline int should_fsync(struct thread_data *td)
498 {
499         if (td->last_was_sync)
500                 return 0;
501         if (td->o.odirect)
502                 return 0;
503         if (td_write(td) || td_rw(td) || td->o.override_sync)
504                 return 1;
505
506         return 0;
507 }
508
509 /*
510  * Disk utils as read in /sys/block/<dev>/stat
511  */
512 struct disk_util_stat {
513         unsigned ios[2];
514         unsigned merges[2];
515         unsigned long long sectors[2];
516         unsigned ticks[2];
517         unsigned io_ticks;
518         unsigned time_in_queue;
519 };
520
521 /*
522  * Per-device disk util management
523  */
524 struct disk_util {
525         struct flist_head list;
526         /* If this disk is a slave, hook it into the master's
527          * list using this head.
528          */
529         struct flist_head slavelist;
530
531         char *name;
532         char *sysfs_root;
533         char path[256];
534         int major, minor;
535
536         struct disk_util_stat dus;
537         struct disk_util_stat last_dus;
538
539         /* For software raids, this entry maintains pointers to the
540          * entries for the slave devices. The disk_util entries for
541          * the slaves devices should primarily be maintained through
542          * the disk_list list, i.e. for memory allocation and
543          * de-allocation, etc. Whereas this list should be used only
544          * for aggregating a software RAID's disk util figures.
545          */
546         struct flist_head slaves;
547
548         unsigned long msec;
549         struct timeval time;
550
551         struct fio_mutex *lock;
552         unsigned long users;
553 };
554
555 static inline void disk_util_inc(struct disk_util *du)
556 {
557         if (du) {
558                 fio_mutex_down(du->lock);
559                 du->users++;
560                 fio_mutex_up(du->lock);
561         }
562 }
563
564 static inline void disk_util_dec(struct disk_util *du)
565 {
566         if (du) {
567                 fio_mutex_down(du->lock);
568                 du->users--;
569                 fio_mutex_up(du->lock);
570         }
571 }
572
573 #define DISK_UTIL_MSEC  (250)
574
575 /*
576  * Log exports
577  */
578 enum file_log_act {
579         FIO_LOG_ADD_FILE,
580         FIO_LOG_OPEN_FILE,
581         FIO_LOG_CLOSE_FILE,
582         FIO_LOG_UNLINK_FILE,
583 };
584
585 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
586 extern void log_io_u(struct thread_data *, struct io_u *);
587 extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
588 extern int __must_check init_iolog(struct thread_data *td);
589 extern void log_io_piece(struct thread_data *, struct io_u *);
590 extern void queue_io_piece(struct thread_data *, struct io_piece *);
591 extern void prune_io_piece_log(struct thread_data *);
592 extern void write_iolog_close(struct thread_data *);
593
594 /*
595  * Logging
596  */
597 extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long,
598                                 unsigned int);
599 extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long,
600                                 unsigned int);
601 extern void add_bw_sample(struct thread_data *, enum fio_ddir, unsigned int,
602                                 struct timeval *);
603 extern void show_run_stats(void);
604 extern void init_disk_util(struct thread_data *);
605 extern void update_rusage_stat(struct thread_data *);
606 extern void update_io_ticks(void);
607 extern void setup_log(struct io_log **);
608 extern void finish_log(struct thread_data *, struct io_log *, const char *);
609 extern void finish_log_named(struct thread_data *, struct io_log *, const char *, const char *);
610 extern void __finish_log(struct io_log *, const char *);
611 extern struct io_log *agg_io_log[2];
612 extern int write_bw_log;
613 extern void add_agg_sample(unsigned long, enum fio_ddir, unsigned int);
614
615 /*
616  * Time functions
617  */
618 extern unsigned long long utime_since(struct timeval *, struct timeval *);
619 extern unsigned long long utime_since_now(struct timeval *);
620 extern unsigned long mtime_since(struct timeval *, struct timeval *);
621 extern unsigned long mtime_since_now(struct timeval *);
622 extern unsigned long time_since_now(struct timeval *);
623 extern unsigned long mtime_since_genesis(void);
624 extern void usec_spin(unsigned int);
625 extern void usec_sleep(struct thread_data *, unsigned long);
626 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
627 extern void fill_start_time(struct timeval *);
628 extern void fio_gettime(struct timeval *, void *);
629 extern void fio_gtod_init(void);
630 extern void fio_gtod_update(void);
631 extern void set_genesis_time(void);
632 extern int ramp_time_over(struct thread_data *);
633 extern int in_ramp_time(struct thread_data *);
634
635 /*
636  * Init/option functions
637  */
638 extern int __must_check parse_options(int, char **);
639 extern int fio_options_parse(struct thread_data *, char **, int);
640 extern int fio_cmd_option_parse(struct thread_data *, const char *, char *);
641 extern void fio_fill_default_options(struct thread_data *);
642 extern int fio_show_option_help(const char *);
643 extern void fio_options_dup_and_init(struct option *);
644 extern void options_mem_dupe(struct thread_data *);
645 extern void options_mem_free(struct thread_data *);
646 extern void td_fill_rand_seeds(struct thread_data *);
647 #define FIO_GETOPT_JOB          0x89988998
648 #define FIO_NR_OPTIONS          128
649
650 /*
651  * ETA/status stuff
652  */
653 extern void print_thread_status(void);
654 extern void print_status_init(int);
655
656 /*
657  * disk util stuff
658  */
659 #ifdef FIO_HAVE_DISK_UTIL
660 extern void show_disk_util(void);
661 extern void init_disk_util(struct thread_data *);
662 extern void update_io_ticks(void);
663 #else
664 #define show_disk_util()
665 #define init_disk_util(td)
666 #define update_io_ticks()
667 #endif
668
669 /*
670  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
671  * will never back again. It may cycle between running/verififying/fsyncing.
672  * Once the thread reaches TD_EXITED, it is just waiting for the core to
673  * reap it.
674  */
675 enum {
676         TD_NOT_CREATED = 0,
677         TD_CREATED,
678         TD_INITIALIZED,
679         TD_RAMP,
680         TD_RUNNING,
681         TD_PRE_READING,
682         TD_VERIFYING,
683         TD_FSYNCING,
684         TD_EXITED,
685         TD_REAPED,
686 };
687
688 extern void td_set_runstate(struct thread_data *, int);
689
690 /*
691  * Memory helpers
692  */
693 extern int __must_check fio_pin_memory(void);
694 extern void fio_unpin_memory(void);
695 extern int __must_check allocate_io_mem(struct thread_data *);
696 extern void free_io_mem(struct thread_data *);
697
698 /*
699  * Reset stats after ramp time completes
700  */
701 extern void reset_all_stats(struct thread_data *);
702
703 /*
704  * blktrace support
705  */
706 #ifdef FIO_HAVE_BLKTRACE
707 extern int is_blktrace(const char *);
708 extern int load_blktrace(struct thread_data *, const char *);
709 #endif
710
711 /*
712  * Mark unused variables passed to ops functions as unused, to silence gcc
713  */
714 #define fio_unused      __attribute((__unused__))
715 #define fio_init        __attribute__((constructor))
716 #define fio_exit        __attribute__((destructor))
717
718 #define for_each_td(td, i)      \
719         for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
720 #define for_each_file(td, f, i) \
721         if ((td)->files_index)                                          \
722                 for ((i) = 0, (f) = (td)->files[0];                     \
723                  (i) < (td)->o.nr_files && ((f) = (td)->files[i]) != NULL; \
724                  (i)++)
725
726 #define fio_assert(td, cond)    do {    \
727         if (!(cond)) {                  \
728                 int *__foo = NULL;      \
729                 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond);   \
730                 (td)->runstate = TD_EXITED;     \
731                 (td)->error = EFAULT;           \
732                 *__foo = 0;                     \
733         }       \
734 } while (0)
735
736 static inline void fio_file_reset(struct fio_file *f)
737 {
738         f->last_free_lookup = 0;
739         f->last_pos = f->file_offset;
740         if (f->file_map)
741                 memset(f->file_map, 0, f->num_maps * sizeof(int));
742 }
743
744 static inline void clear_error(struct thread_data *td)
745 {
746         td->error = 0;
747         td->verror[0] = '\0';
748 }
749
750 #ifdef FIO_INC_DEBUG
751 static inline void dprint_io_u(struct io_u *io_u, const char *p)
752 {
753         struct fio_file *f = io_u->file;
754
755         dprint(FD_IO, "%s: io_u %p: off=%llu/len=%lu/ddir=%d", p, io_u,
756                                         (unsigned long long) io_u->offset,
757                                         io_u->buflen, io_u->ddir);
758         if (fio_debug & (1 << FD_IO)) {
759                 if (f)
760                         log_info("/%s", f->file_name);
761
762                 log_info("\n");
763         }
764 }
765 #else
766 #define dprint_io_u(io_u, p)
767 #endif
768
769 static inline int fio_fill_issue_time(struct thread_data *td)
770 {
771         if (td->o.read_iolog_file ||
772             !td->o.disable_clat || !td->o.disable_slat || !td->o.disable_bw)
773                 return 1;
774
775         return 0;
776 }
777
778 #endif