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