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