Merge branch 'asprintf' of https://github.com/bvanassche/fio
[fio.git] / diskutil.h
1 #ifndef FIO_DISKUTIL_H
2 #define FIO_DISKUTIL_H
3 #include "json.h"
4 #define FIO_DU_NAME_SZ          64
5
6 #include "lib/output_buffer.h"
7 #include "helper_thread.h"
8 #include "fio_sem.h"
9
10 struct disk_util_stats {
11         uint64_t ios[2];
12         uint64_t merges[2];
13         uint64_t sectors[2];
14         uint64_t ticks[2];
15         uint64_t io_ticks;
16         uint64_t time_in_queue;
17         uint64_t msec;
18 };
19
20 /*
21  * Disk utils as read in /sys/block/<dev>/stat
22  */
23 struct disk_util_stat {
24         uint8_t name[FIO_DU_NAME_SZ];
25         struct disk_util_stats s;
26 };
27
28 struct disk_util_agg {
29         uint64_t ios[2];
30         uint64_t merges[2];
31         uint64_t sectors[2];
32         uint64_t ticks[2];
33         uint64_t io_ticks;
34         uint64_t time_in_queue;
35         uint32_t slavecount;
36         uint32_t pad;
37         fio_fp64_t max_util;
38 };
39
40 /*
41  * Per-device disk util management
42  */
43 struct disk_util {
44         struct flist_head list;
45         /* If this disk is a slave, hook it into the master's
46          * list using this head.
47          */
48         struct flist_head slavelist;
49
50         char *sysfs_root;
51         char path[PATH_MAX];
52         int major, minor;
53
54         struct disk_util_stat dus;
55         struct disk_util_stat last_dus;
56
57         struct disk_util_agg agg;
58
59         /* For software raids, this entry maintains pointers to the
60          * entries for the slave devices. The disk_util entries for
61          * the slaves devices should primarily be maintained through
62          * the disk_list list, i.e. for memory allocation and
63          * de-allocation, etc. Whereas this list should be used only
64          * for aggregating a software RAID's disk util figures.
65          */
66         struct flist_head slaves;
67
68         struct timespec time;
69
70         struct fio_sem *lock;
71         unsigned long users;
72 };
73
74 static inline void disk_util_mod(struct disk_util *du, int val)
75 {
76         if (du) {
77                 struct flist_head *n;
78
79                 fio_sem_down(du->lock);
80                 du->users += val;
81
82                 flist_for_each(n, &du->slavelist) {
83                         struct disk_util *slave;
84
85                         slave = flist_entry(n, struct disk_util, slavelist);
86                         slave->users += val;
87                 }
88                 fio_sem_up(du->lock);
89         }
90 }
91 static inline void disk_util_inc(struct disk_util *du)
92 {
93         disk_util_mod(du, 1);
94 }
95
96 static inline void disk_util_dec(struct disk_util *du)
97 {
98         disk_util_mod(du, -1);
99 }
100
101 #define DISK_UTIL_MSEC  (250)
102
103 extern struct flist_head disk_list;
104
105 /*
106  * disk util stuff
107  */
108 #ifdef FIO_HAVE_DISK_UTIL
109 extern void print_disk_util(struct disk_util_stat *, struct disk_util_agg *, int terse, struct buf_output *);
110 extern void show_disk_util(int terse, struct json_object *parent, struct buf_output *);
111 extern void json_array_add_disk_util(struct disk_util_stat *dus,
112                 struct disk_util_agg *agg, struct json_array *parent);
113 extern void init_disk_util(struct thread_data *);
114 extern int update_io_ticks(void);
115 extern void setup_disk_util(void);
116 extern void disk_util_prune_entries(void);
117 #else
118 /* keep this as a function to avoid a warning in handle_du() */
119 static inline void print_disk_util(struct disk_util_stat *du,
120                                    struct disk_util_agg *agg, int terse,
121                                    struct buf_output *out)
122 {
123 }
124 #define show_disk_util(terse, parent, out)
125 #define disk_util_prune_entries()
126 #define init_disk_util(td)
127 #define setup_disk_util()
128 #define json_array_add_disk_util(dus, agg, parent)
129
130 static inline int update_io_ticks(void)
131 {
132         return helper_should_exit();
133 }
134 #endif
135
136 #endif