Merge branch 'histogram-delta' of https://github.com/cronburg/fio into histogram
[fio.git] / iolog.h
1 #ifndef FIO_IOLOG_H
2 #define FIO_IOLOG_H
3
4 #include "lib/rbtree.h"
5 #include "lib/ieee754.h"
6 #include "flist.h"
7 #include "ioengine.h"
8
9 /*
10  * Use for maintaining statistics
11  */
12 struct io_stat {
13         uint64_t max_val;
14         uint64_t min_val;
15         uint64_t samples;
16
17         fio_fp64_t mean;
18         fio_fp64_t S;
19 };
20
21 struct io_u_plat_list {
22         struct flist_head list;
23 };
24
25 struct io_hist {
26         uint64_t samples;
27         unsigned long hist_last;
28         struct io_u_plat_list list;
29 };
30
31 /*
32  * A single data sample
33  */
34 struct io_sample {
35         uint64_t time;
36         uint64_t val;
37         uint32_t __ddir;
38         uint32_t bs;
39 };
40
41 struct io_sample_offset {
42         struct io_sample s;
43         uint64_t offset;
44 };
45
46 enum {
47         IO_LOG_TYPE_LAT = 1,
48         IO_LOG_TYPE_CLAT,
49         IO_LOG_TYPE_SLAT,
50         IO_LOG_TYPE_BW,
51         IO_LOG_TYPE_IOPS,
52         IO_LOG_TYPE_HIST,
53 };
54
55 #define DEF_LOG_ENTRIES         1024
56 #define MAX_LOG_ENTRIES         (1024 * DEF_LOG_ENTRIES)
57
58 struct io_logs {
59         struct flist_head list;
60         uint64_t nr_samples;
61         uint64_t max_samples;
62         void *log;
63 };
64
65 /*
66  * Dynamically growing data sample log
67  */
68 struct io_log {
69         /*
70          * Entries already logged
71          */
72         struct flist_head io_logs;
73         uint32_t cur_log_max;
74
75         /*
76          * When the current log runs out of space, store events here until
77          * we have a chance to regrow
78          */
79         struct io_logs *pending;
80
81         unsigned int log_ddir_mask;
82
83         char *filename;
84
85         struct thread_data *td;
86
87         unsigned int log_type;
88
89         /*
90          * If we fail extending the log, stop collecting more entries.
91          */
92         bool disabled;
93
94         /*
95          * Log offsets
96          */
97         unsigned int log_offset;
98
99         /*
100          * Max size of log entries before a chunk is compressed
101          */
102         unsigned int log_gz;
103
104         /*
105          * Don't deflate for storing, just store the compressed bits
106          */
107         unsigned int log_gz_store;
108
109         /*
110          * Windowed average, for logging single entries average over some
111          * period of time.
112          */
113         struct io_stat avg_window[DDIR_RWDIR_CNT];
114         unsigned long avg_msec;
115         unsigned long avg_last;
116
117         /*
118          * Windowed latency histograms, for keeping track of when we need to
119          * save a copy of the histogram every approximately hist_msec
120          * milliseconds.
121          */
122         struct io_hist hist_window[DDIR_RWDIR_CNT];
123         unsigned long hist_msec;
124         int hist_coarseness;
125
126         pthread_mutex_t chunk_lock;
127         unsigned int chunk_seq;
128         struct flist_head chunk_list;
129 };
130
131 /*
132  * If the upper bit is set, then we have the offset as well
133  */
134 #define LOG_OFFSET_SAMPLE_BIT   0x80000000U
135 #define io_sample_ddir(io)      ((io)->__ddir & ~LOG_OFFSET_SAMPLE_BIT)
136
137 static inline void io_sample_set_ddir(struct io_log *log,
138                                       struct io_sample *io,
139                                       enum fio_ddir ddir)
140 {
141         io->__ddir = ddir | log->log_ddir_mask;
142 }
143
144 static inline size_t __log_entry_sz(int log_offset)
145 {
146         if (log_offset)
147                 return sizeof(struct io_sample_offset);
148         else
149                 return sizeof(struct io_sample);
150 }
151
152 static inline size_t log_entry_sz(struct io_log *log)
153 {
154         return __log_entry_sz(log->log_offset);
155 }
156
157 static inline struct io_sample *__get_sample(void *samples, int log_offset,
158                                              uint64_t sample)
159 {
160         uint64_t sample_offset = sample * __log_entry_sz(log_offset);
161         return (struct io_sample *) ((char *) samples + sample_offset);
162 }
163
164 struct io_logs *iolog_cur_log(struct io_log *);
165 uint64_t iolog_nr_samples(struct io_log *);
166 void regrow_logs(struct thread_data *);
167
168 static inline struct io_sample *get_sample(struct io_log *iolog,
169                                            struct io_logs *cur_log,
170                                            uint64_t sample)
171 {
172         return __get_sample(cur_log->log, iolog->log_offset, sample);
173 }
174
175 enum {
176         IP_F_ONRB       = 1,
177         IP_F_ONLIST     = 2,
178         IP_F_TRIMMED    = 4,
179         IP_F_IN_FLIGHT  = 8,
180 };
181
182 /*
183  * When logging io actions, this matches a single sent io_u
184  */
185 struct io_piece {
186         union {
187                 struct rb_node rb_node;
188                 struct flist_head list;
189         };
190         struct flist_head trim_list;
191         union {
192                 int fileno;
193                 struct fio_file *file;
194         };
195         unsigned long long offset;
196         unsigned short numberio;
197         unsigned long len;
198         unsigned int flags;
199         enum fio_ddir ddir;
200         union {
201                 unsigned long delay;
202                 unsigned int file_action;
203         };
204 };
205
206 /*
207  * Log exports
208  */
209 enum file_log_act {
210         FIO_LOG_ADD_FILE,
211         FIO_LOG_OPEN_FILE,
212         FIO_LOG_CLOSE_FILE,
213         FIO_LOG_UNLINK_FILE,
214 };
215
216 struct io_u;
217 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
218 extern void log_io_u(const struct thread_data *, const struct io_u *);
219 extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
220 extern int __must_check init_iolog(struct thread_data *td);
221 extern void log_io_piece(struct thread_data *, struct io_u *);
222 extern void unlog_io_piece(struct thread_data *, struct io_u *);
223 extern void trim_io_piece(struct thread_data *, const struct io_u *);
224 extern void queue_io_piece(struct thread_data *, struct io_piece *);
225 extern void prune_io_piece_log(struct thread_data *);
226 extern void write_iolog_close(struct thread_data *);
227 extern int iolog_compress_init(struct thread_data *, struct sk_out *);
228 extern void iolog_compress_exit(struct thread_data *);
229 extern size_t log_chunk_sizes(struct io_log *);
230
231 #ifdef CONFIG_ZLIB
232 extern int iolog_file_inflate(const char *);
233 #endif
234
235 /*
236  * Logging
237  */
238 struct log_params {
239         struct thread_data *td;
240         unsigned long avg_msec;
241         unsigned long hist_msec;
242         int hist_coarseness;
243         int log_type;
244         int log_offset;
245         int log_gz;
246         int log_gz_store;
247         int log_compress;
248 };
249
250 static inline bool per_unit_log(struct io_log *log)
251 {
252         return log && !log->avg_msec;
253 }
254
255 static inline bool inline_log(struct io_log *log)
256 {
257         return log->log_type == IO_LOG_TYPE_LAT ||
258                 log->log_type == IO_LOG_TYPE_CLAT ||
259                 log->log_type == IO_LOG_TYPE_SLAT;
260 }
261
262 extern void finalize_logs(struct thread_data *td, bool);
263 extern void setup_log(struct io_log **, struct log_params *, const char *);
264 extern void flush_log(struct io_log *, bool);
265 extern void flush_samples(FILE *, void *, uint64_t);
266 extern void free_log(struct io_log *);
267 extern void fio_writeout_logs(bool);
268 extern void td_writeout_logs(struct thread_data *, bool);
269 extern int iolog_cur_flush(struct io_log *, struct io_logs *);
270
271 static inline void init_ipo(struct io_piece *ipo)
272 {
273         memset(ipo, 0, sizeof(*ipo));
274         INIT_FLIST_HEAD(&ipo->trim_list);
275 }
276
277 struct iolog_compress {
278         struct flist_head list;
279         void *buf;
280         size_t len;
281         unsigned int seq;
282 };
283
284 #endif