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