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