Streamline thread_data data direction setting and checking
[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
15 #include "list.h"
16 #include "md5.h"
17 #include "crc32.h"
18 #include "arch.h"
19 #include "os.h"
20
21 #ifdef FIO_HAVE_SYSLET
22 #include "syslet.h"
23 #endif
24
25 enum fio_ddir {
26         DDIR_READ = 0,
27         DDIR_WRITE,
28         DDIR_SYNC,
29 };
30
31 enum td_ddir {
32         TD_DDIR_READ    = 1 << 0,
33         TD_DDIR_WRITE   = 1 << 1,
34         TD_DDIR_RAND    = 1 << 2,
35         TD_DDIR_RW      = TD_DDIR_READ | TD_DDIR_WRITE,
36 };
37
38 /*
39  * Use for maintaining statistics
40  */
41 struct io_stat {
42         unsigned long max_val;
43         unsigned long min_val;
44         unsigned long samples;
45
46         double mean;
47         double S;
48 };
49
50 /*
51  * A single data sample
52  */
53 struct io_sample {
54         unsigned long time;
55         unsigned long val;
56         enum fio_ddir ddir;
57 };
58
59 /*
60  * Dynamically growing data sample log
61  */
62 struct io_log {
63         unsigned long nr_samples;
64         unsigned long max_samples;
65         struct io_sample *log;
66 };
67
68 /*
69  * When logging io actions, this matches a single sent io_u
70  */
71 struct io_piece {
72         struct list_head list;
73         struct fio_file *file;
74         unsigned long long offset;
75         unsigned long len;
76         enum fio_ddir ddir;
77 };
78
79 #ifdef FIO_HAVE_SYSLET
80 struct syslet_req {
81         struct syslet_uatom atom;
82         long ret;
83 };
84 #endif
85
86 enum {
87         IO_U_F_FREE     = 1 << 0,
88         IO_U_F_FLIGHT   = 1 << 1,
89 };
90
91 /*
92  * The io unit
93  */
94 struct io_u {
95         union {
96 #ifdef FIO_HAVE_LIBAIO
97                 struct iocb iocb;
98 #endif
99 #ifdef FIO_HAVE_POSIXAIO
100                 struct aiocb aiocb;
101 #endif
102 #ifdef FIO_HAVE_SGIO
103                 struct sg_io_hdr hdr;
104 #endif
105 #ifdef FIO_HAVE_SYSLET
106                 struct syslet_req req;
107 #endif
108         };
109         struct timeval start_time;
110         struct timeval issue_time;
111
112         /*
113          * Allocated/set buffer and length
114          */
115         void *buf;
116         unsigned long buflen;
117         unsigned long long offset;
118
119         /*
120          * IO engine state, may be different from above when we get
121          * partial transfers / residual data counts
122          */
123         void *xfer_buf;
124         unsigned long xfer_buflen;
125
126         unsigned int resid;
127         unsigned int error;
128
129         enum fio_ddir ddir;
130
131         /*
132          * io engine private data
133          */
134         union {
135                 unsigned int index;
136                 unsigned int seen;
137         };
138
139         unsigned int flags;
140
141         struct fio_file *file;
142
143         struct list_head list;
144 };
145
146 /*
147  * io_ops->queue() return values
148  */
149 enum {
150         FIO_Q_COMPLETED = 0,            /* completed sync */
151         FIO_Q_QUEUED    = 1,            /* queued, will complete async */
152         FIO_Q_BUSY      = 2,            /* no more room, call ->commit() */
153 };
154
155 #define FIO_HDR_MAGIC   0xf00baaef
156
157 enum {
158         VERIFY_NONE = 0,                /* no verification */
159         VERIFY_MD5,                     /* md5 sum data blocks */
160         VERIFY_CRC32,                   /* crc32 sum data blocks */
161 };
162
163 /*
164  * A header structure associated with each checksummed data block
165  */
166 struct verify_header {
167         unsigned int fio_magic;
168         unsigned int len;
169         unsigned int verify_type;
170         union {
171                 char md5_digest[MD5_HASH_WORDS * 4];
172                 unsigned long crc32;
173         };
174 };
175
176 struct group_run_stats {
177         unsigned long long max_run[2], min_run[2];
178         unsigned long long max_bw[2], min_bw[2];
179         unsigned long long io_kb[2];
180         unsigned long long agg[2];
181 };
182
183 /*
184  * What type of allocation to use for io buffers
185  */
186 enum fio_memtype {
187         MEM_MALLOC = 0, /* ordinary malloc */
188         MEM_SHM,        /* use shared memory segments */
189         MEM_SHMHUGE,    /* use shared memory segments with huge pages */
190         MEM_MMAP,       /* use anonynomous mmap */
191         MEM_MMAPHUGE,   /* memory mapped huge file */
192 };
193
194 /*
195  * The type of object we are working on
196  */
197 enum fio_filetype {
198         FIO_TYPE_FILE = 1,              /* plain file */
199         FIO_TYPE_BD,                    /* block device */
200         FIO_TYPE_CHAR,                  /* character device */
201 };
202
203 enum fio_ioengine_flags {
204         FIO_SYNCIO      = 1 << 0,       /* io engine has synchronous ->queue */
205         FIO_CPUIO       = 1 << 1,       /* cpu burner, doesn't do real io */
206         FIO_MMAPIO      = 1 << 2,       /* uses memory mapped io */
207         FIO_RAWIO       = 1 << 3,       /* some sort of direct/raw io */
208         FIO_NETIO       = 1 << 4,       /* networked io */
209         FIO_NULLIO      = 1 << 5,       /* no real data transfer (cpu/null) */
210 };
211
212 /*
213  * Each thread_data structure has a number of files associated with it,
214  * this structure holds state information for a single file.
215  */
216 struct fio_file {
217         /*
218          * A file may not be a file descriptor, let the io engine decide
219          */
220         union {
221                 unsigned long file_data;
222                 int fd;
223         };
224         char *file_name;
225         void *mmap;
226         unsigned long long file_size;
227         unsigned long long real_file_size;
228         unsigned long long file_offset;
229         unsigned long long last_pos;
230         unsigned long long last_completed_pos;
231
232         /*
233          * block map for random io
234          */
235         unsigned long *file_map;
236         unsigned int num_maps;
237         unsigned int last_free_lookup;
238
239         unsigned int unlink;
240 };
241
242 struct thread_stat {
243         struct io_log *slat_log;
244         struct io_log *clat_log;
245         struct io_log *bw_log;
246
247         /*
248          * bandwidth and latency stats
249          */
250         struct io_stat clat_stat[2];            /* completion latency */
251         struct io_stat slat_stat[2];            /* submission latency */
252         struct io_stat bw_stat[2];              /* bandwidth stats */
253
254         unsigned long long stat_io_bytes[2];
255         struct timeval stat_sample_time[2];
256
257         /*
258          * fio system usage accounting
259          */
260         struct rusage ru_start;
261         struct rusage ru_end;
262         unsigned long usr_time;
263         unsigned long sys_time;
264         unsigned long ctx;
265 };
266
267 /*
268  * How many depth levels to log
269  */
270 #define FIO_IO_U_MAP_NR 8
271 #define FIO_IO_U_LAT_NR 12
272
273 /*
274  * This describes a single thread/process executing a fio job.
275  */
276 struct thread_data {
277         char *description;
278         char *name;
279         char *directory;
280         char *filename;
281         char verror[80];
282         pthread_t thread;
283         int thread_number;
284         int groupid;
285         struct thread_stat ts;
286         enum fio_filetype filetype;
287         struct fio_file *files;
288         unsigned int nr_files;
289         unsigned int nr_uniq_files;
290         union {
291                 unsigned int next_file;
292                 os_random_state_t next_file_state;
293         };
294         int error;
295         pid_t pid;
296         char *orig_buffer;
297         size_t orig_buffer_size;
298         volatile int terminate;
299         volatile int runstate;
300         enum td_ddir td_ddir;
301         unsigned int ioprio;
302         unsigned int last_was_sync;
303
304         unsigned int odirect;
305         unsigned int invalidate_cache;
306         unsigned int create_serialize;
307         unsigned int create_fsync;
308         unsigned int end_fsync;
309         unsigned int sync_io;
310         unsigned int verify;
311         unsigned int use_thread;
312         unsigned int unlink;
313         unsigned int do_disk_util;
314         unsigned int override_sync;
315         unsigned int rand_repeatable;
316         unsigned int write_lat_log;
317         unsigned int write_bw_log;
318         unsigned int norandommap;
319         unsigned int bs_unaligned;
320
321         unsigned int bs[2];
322         unsigned int min_bs[2];
323         unsigned int max_bs[2];
324         unsigned int hugepage_size;
325         unsigned int rw_min_bs;
326         unsigned int thinktime;
327         unsigned int thinktime_spin;
328         unsigned int thinktime_blocks;
329         unsigned int fsync_blocks;
330         unsigned int start_delay;
331         unsigned long timeout;
332         unsigned int overwrite;
333         unsigned int bw_avg_time;
334         unsigned int loops;
335         unsigned long long zone_size;
336         unsigned long long zone_skip;
337         enum fio_memtype mem_type;
338         char *mmapfile;
339         int mmapfd;
340         unsigned int stonewall;
341         unsigned int numjobs;
342         unsigned int iodepth;
343         unsigned int iodepth_low;
344         os_cpu_mask_t cpumask;
345         unsigned int iolog;
346         unsigned int read_iolog;
347         unsigned int rwmixcycle;
348         unsigned int rwmixread;
349         unsigned int rwmixwrite;
350         unsigned int nice;
351         unsigned int file_service_type;
352
353         char *read_iolog_file;
354         char *write_iolog_file;
355         void *iolog_buf;
356         FILE *iolog_f;
357
358         char *sysfs_root;
359         char *ioscheduler;
360
361         os_random_state_t bsrange_state;
362         os_random_state_t verify_state;
363
364         int shm_id;
365
366         /*
367          * IO engine hooks, contains everything needed to submit an io_u
368          * to any of the available IO engines.
369          */
370         struct ioengine_ops *io_ops;
371
372         /*
373          * Current IO depth and list of free and busy io_u's.
374          */
375         unsigned int cur_depth;
376         unsigned int io_u_map[FIO_IO_U_MAP_NR];
377         unsigned int io_u_lat[FIO_IO_U_LAT_NR];
378         unsigned long total_io_u;
379         struct list_head io_u_freelist;
380         struct list_head io_u_busylist;
381         struct list_head io_u_requeues;
382
383         /*
384          * Rate state
385          */
386         unsigned int rate;
387         unsigned int ratemin;
388         unsigned int ratecycle;
389         unsigned long rate_usec_cycle;
390         long rate_pending_usleep;
391         unsigned long rate_bytes;
392         struct timeval lastrate;
393
394         unsigned long runtime[2];               /* msec */
395         unsigned long long io_size;
396         unsigned long long total_file_size;
397         unsigned long long start_offset;
398         unsigned long long total_io_size;
399
400         unsigned long io_issues[2];
401         unsigned long long io_blocks[2];
402         unsigned long long io_bytes[2];
403         unsigned long long this_io_bytes[2];
404         unsigned long long zone_bytes;
405         volatile int mutex;
406
407         /*
408          * State for random io, a bitmap of blocks done vs not done
409          */
410         os_random_state_t random_state;
411
412         /*
413          * CPU "io" cycle burner
414          */
415         unsigned int cpuload;
416         unsigned int cpucycle;
417
418         struct timeval start;   /* start of this loop */
419         struct timeval epoch;   /* time job was started */
420         struct timeval end_time;/* time job ended */
421
422         /*
423          * read/write mixed workload state
424          */
425         os_random_state_t rwmix_state;
426         struct timeval rwmix_switch;
427         enum fio_ddir rwmix_ddir;
428
429         /*
430          * Pre-run and post-run shell
431          */
432         char *exec_prerun;
433         char *exec_postrun;
434
435         /*
436          * IO historic logs
437          */
438         struct list_head io_hist_list;
439         struct list_head io_log_list;
440
441         /*
442          * timeout handling
443          */
444         struct timeval timeout_end;
445         struct itimerval timer;
446 };
447
448 /*
449  * roundrobin available files, or choose one at random.
450  */
451 enum {
452         FIO_FSERVICE_RANDOM     = 1,
453         FIO_FSERVICE_RR         = 2,
454 };
455
456 /*
457  * 30 second per-io_u timeout, with 5 second intervals to avoid resetting
458  * the timer on each queue operation.
459  */
460 #define IO_U_TIMEOUT_INC        5
461 #define IO_U_TIMEOUT            30
462
463 #define __td_verror(td, err, msg, func)                                 \
464         do {                                                            \
465                 if ((td)->error)                                        \
466                         break;                                          \
467                 int e = (err);                                          \
468                 (td)->error = e;                                        \
469                 snprintf(td->verror, sizeof(td->verror) - 1, "file:%s:%d, func=%s, error=%s", __FILE__, __LINE__, (func), (msg));       \
470         } while (0)
471
472
473 #define td_verror(td, err, func)        \
474         __td_verror((td), (err), strerror((err)), (func))
475 #define td_vmsg(td, err, msg, func)     \
476         __td_verror((td), (err), (msg), (func))
477
478 extern int exitall_on_terminate;
479 extern int thread_number;
480 extern int shm_id;
481 extern int groupid;
482 extern int terse_output;
483 extern FILE *f_out;
484 extern FILE *f_err;
485 extern int temp_stall_ts;
486 extern unsigned long long mlock_size;
487
488 extern struct thread_data *threads;
489
490 #define td_read(td)             ((td)->td_ddir & TD_DDIR_READ)
491 #define td_write(td)            ((td)->td_ddir & TD_DDIR_WRITE)
492 #define td_rw(td)               (((td)->td_ddir & TD_DDIR_RW) == TD_DDIR_RW)
493 #define td_random(td)           ((td)->td_ddir & TD_DDIR_RAND)
494
495 #define BLOCKS_PER_MAP          (8 * sizeof(long))
496 #define TO_MAP_BLOCK(td, f, b)  ((b) - ((f)->file_offset / (td)->rw_min_bs))
497 #define RAND_MAP_IDX(td, f, b)  (TO_MAP_BLOCK(td, f, b) / BLOCKS_PER_MAP)
498 #define RAND_MAP_BIT(td, f, b)  (TO_MAP_BLOCK(td, f, b) & (BLOCKS_PER_MAP - 1))
499
500 #define MAX_JOBS        (1024)
501
502 static inline int should_fsync(struct thread_data *td)
503 {
504         if (td->last_was_sync)
505                 return 0;
506         if (td->odirect)
507                 return 0;
508         if (td_write(td) || td_rw(td) || td->override_sync)
509                 return 1;
510
511         return 0;
512 }
513
514 /*
515  * Disk utils as read in /sys/block/<dev>/stat
516  */
517 struct disk_util_stat {
518         unsigned ios[2];
519         unsigned merges[2];
520         unsigned long long sectors[2];
521         unsigned ticks[2];
522         unsigned io_ticks;
523         unsigned time_in_queue;
524 };
525
526 /*
527  * Per-device disk util management
528  */
529 struct disk_util {
530         struct list_head list;
531
532         char *name;
533         char path[256];
534         dev_t dev;
535
536         struct disk_util_stat dus;
537         struct disk_util_stat last_dus;
538
539         unsigned long msec;
540         struct timeval time;
541 };
542
543 /*
544  * Callback for io completion
545  */
546 typedef int (endio_handler)(struct io_u *);
547
548 #define DISK_UTIL_MSEC  (250)
549
550 #ifndef min
551 #define min(a, b)       ((a) < (b) ? (a) : (b))
552 #endif
553 #ifndef max
554 #define max(a, b)       ((a) > (b) ? (a) : (b))
555 #endif
556
557 /*
558  * Log exports
559  */
560 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
561 extern void write_iolog_put(struct thread_data *, struct io_u *);
562 extern int __must_check init_iolog(struct thread_data *td);
563 extern void log_io_piece(struct thread_data *, struct io_u *);
564 extern void prune_io_piece_log(struct thread_data *);
565 extern void write_iolog_close(struct thread_data *);
566
567 /*
568  * Logging
569  */
570 extern void add_clat_sample(struct thread_data *, enum fio_ddir, unsigned long);
571 extern void add_slat_sample(struct thread_data *, enum fio_ddir, unsigned long);
572 extern void add_bw_sample(struct thread_data *, enum fio_ddir, struct timeval *);
573 extern void show_run_stats(void);
574 extern void init_disk_util(struct thread_data *);
575 extern void update_rusage_stat(struct thread_data *);
576 extern void update_io_ticks(void);
577 extern void disk_util_timer_arm(void);
578 extern void setup_log(struct io_log **);
579 extern void finish_log(struct thread_data *, struct io_log *, const char *);
580 extern void __finish_log(struct io_log *, const char *);
581 extern int setup_rate(struct thread_data *);
582 extern struct io_log *agg_io_log[2];
583 extern int write_bw_log;
584 extern void add_agg_sample(unsigned long, enum fio_ddir);
585
586 /*
587  * Time functions
588  */
589 extern unsigned long utime_since(struct timeval *, struct timeval *);
590 extern unsigned long utime_since_now(struct timeval *);
591 extern unsigned long mtime_since(struct timeval *, struct timeval *);
592 extern unsigned long mtime_since_now(struct timeval *);
593 extern unsigned long time_since_now(struct timeval *);
594 extern unsigned long mtime_since_genesis(void);
595 extern void __usec_sleep(unsigned int);
596 extern void usec_sleep(struct thread_data *, unsigned long);
597 extern void rate_throttle(struct thread_data *, unsigned long, unsigned int);
598 extern void fill_start_time(struct timeval *);
599 extern void fio_gettime(struct timeval *, void *);
600 extern void set_genesis_time(void);
601
602 /*
603  * Init functions
604  */
605 extern int __must_check parse_options(int, char **);
606 extern int __must_check init_random_state(struct thread_data *);
607
608 /*
609  * File setup/shutdown
610  */
611 extern void close_files(struct thread_data *);
612 extern int __must_check setup_files(struct thread_data *);
613 extern int __must_check open_files(struct thread_data *);
614 extern int __must_check file_invalidate_cache(struct thread_data *, struct fio_file *);
615
616 /*
617  * ETA/status stuff
618  */
619 extern void print_thread_status(void);
620 extern void print_status_init(int);
621
622 /*
623  * Thread life cycle. Once a thread has a runstate beyond TD_INITIALIZED, it
624  * will never back again. It may cycle between running/verififying/fsyncing.
625  * Once the thread reaches TD_EXITED, it is just waiting for the core to
626  * reap it.
627  */
628 enum {
629         TD_NOT_CREATED = 0,
630         TD_CREATED,
631         TD_INITIALIZED,
632         TD_RUNNING,
633         TD_VERIFYING,
634         TD_FSYNCING,
635         TD_EXITED,
636         TD_REAPED,
637 };
638
639 /*
640  * Verify helpers
641  */
642 extern void populate_verify_io_u(struct thread_data *, struct io_u *);
643 extern int __must_check get_next_verify(struct thread_data *td, struct io_u *);
644 extern int __must_check verify_io_u(struct io_u *);
645
646 /*
647  * Memory helpers
648  */
649 extern int __must_check fio_pin_memory(void);
650 extern void fio_unpin_memory(void);
651 extern int __must_check allocate_io_mem(struct thread_data *);
652 extern void free_io_mem(struct thread_data *);
653
654 /*
655  * io unit handling
656  */
657 #define queue_full(td)  list_empty(&(td)->io_u_freelist)
658 extern struct io_u *__get_io_u(struct thread_data *);
659 extern struct io_u *get_io_u(struct thread_data *);
660 extern void put_io_u(struct thread_data *, struct io_u *);
661 extern void requeue_io_u(struct thread_data *, struct io_u **);
662 extern long __must_check io_u_sync_complete(struct thread_data *, struct io_u *, endio_handler *);
663 extern long __must_check io_u_queued_complete(struct thread_data *, int, endio_handler *);
664 extern void io_u_queued(struct thread_data *, struct io_u *);
665 extern void io_u_init_timeout(void);
666 extern void io_u_set_timeout(struct thread_data *);
667
668 /*
669  * io engine entry points
670  */
671 extern int __must_check td_io_init(struct thread_data *);
672 extern int __must_check td_io_prep(struct thread_data *, struct io_u *);
673 extern int __must_check td_io_queue(struct thread_data *, struct io_u *);
674 extern int __must_check td_io_sync(struct thread_data *, struct fio_file *);
675 extern int __must_check td_io_getevents(struct thread_data *, int, int, struct timespec *);
676 extern int __must_check td_io_commit(struct thread_data *);
677
678 /*
679  * This is a pretty crappy semaphore implementation, but with the use that fio
680  * has (just signalling start/go conditions), it doesn't have to be better.
681  * Naturally this would not work for any type of contended semaphore or
682  * for real locking.
683  */
684 static inline void fio_sem_init(volatile int *sem, int val)
685 {
686         *sem = val;
687 }
688
689 static inline void fio_sem_down(volatile int *sem)
690 {
691         while (*sem == 0)
692                 usleep(10000);
693
694         (*sem)--;
695 }
696
697 static inline void fio_sem_up(volatile int *sem)
698 {
699         (*sem)++;
700 }
701
702 /*
703  * If logging output to a file, stderr should go to both stderr and f_err
704  */
705 #define log_err(args...)        do {            \
706         fprintf(f_err, ##args);                 \
707         if (f_err != stderr)                    \
708                 fprintf(stderr, ##args);        \
709         } while (0)
710
711 struct ioengine_ops {
712         struct list_head list;
713         char name[16];
714         int version;
715         int flags;
716         int (*setup)(struct thread_data *);
717         int (*init)(struct thread_data *);
718         int (*prep)(struct thread_data *, struct io_u *);
719         int (*queue)(struct thread_data *, struct io_u *);
720         int (*commit)(struct thread_data *);
721         int (*getevents)(struct thread_data *, int, int, struct timespec *);
722         struct io_u *(*event)(struct thread_data *, int);
723         int (*cancel)(struct thread_data *, struct io_u *);
724         void (*cleanup)(struct thread_data *);
725         void *data;
726         void *dlhandle;
727         unsigned long priv;
728 };
729
730 #define FIO_IOOPS_VERSION       5
731
732 extern struct ioengine_ops *load_ioengine(struct thread_data *, const char *);
733 extern void register_ioengine(struct ioengine_ops *);
734 extern void unregister_ioengine(struct ioengine_ops *);
735 extern void close_ioengine(struct thread_data *);
736
737 /*
738  * Mark unused variables passed to ops functions as unused, to silence gcc
739  */
740 #define fio_unused      __attribute((__unused__))
741 #define fio_init        __attribute__((constructor))
742 #define fio_exit        __attribute__((destructor))
743
744 #define for_each_td(td, i)      \
745         for ((i) = 0, (td) = &threads[0]; (i) < (int) thread_number; (i)++, (td)++)
746 #define for_each_file(td, f, i) \
747         for ((i) = 0, (f) = &(td)->files[0]; (i) < (int) (td)->nr_files; (i)++, (f)++)
748
749 #define fio_assert(td, cond)    do {    \
750         if (!(cond)) {                  \
751                 int *__foo = NULL;      \
752                 fprintf(stderr, "file:%s:%d, assert %s failed\n", __FILE__, __LINE__, #cond);   \
753                 (td)->runstate = TD_EXITED;     \
754                 (td)->error = EFAULT;           \
755                 *__foo = 0;                     \
756         }       \
757 } while (0)
758
759 #endif