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