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