ade4f1b80b379fe163ab085c3795b0fe1e30416a
[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 milliseconds.
120    */
121         struct io_hist hist_window[DDIR_RWDIR_CNT];
122         unsigned long hist_msec;
123         int hist_coarseness;
124
125         pthread_mutex_t chunk_lock;
126         unsigned int chunk_seq;
127         struct flist_head chunk_list;
128 };
129
130 /*
131  * If the upper bit is set, then we have the offset as well
132  */
133 #define LOG_OFFSET_SAMPLE_BIT   0x80000000U
134 #define io_sample_ddir(io)      ((io)->__ddir & ~LOG_OFFSET_SAMPLE_BIT)
135
136 static inline void io_sample_set_ddir(struct io_log *log,
137                                       struct io_sample *io,
138                                       enum fio_ddir ddir)
139 {
140         io->__ddir = ddir | log->log_ddir_mask;
141 }
142
143 static inline size_t __log_entry_sz(int log_offset)
144 {
145         if (log_offset)
146                 return sizeof(struct io_sample_offset);
147         else
148                 return sizeof(struct io_sample);
149 }
150
151 static inline size_t log_entry_sz(struct io_log *log)
152 {
153         return __log_entry_sz(log->log_offset);
154 }
155
156 static inline struct io_sample *__get_sample(void *samples, int log_offset,
157                                              uint64_t sample)
158 {
159         uint64_t sample_offset = sample * __log_entry_sz(log_offset);
160         return (struct io_sample *) ((char *) samples + sample_offset);
161 }
162
163 struct io_logs *iolog_cur_log(struct io_log *);
164 uint64_t iolog_nr_samples(struct io_log *);
165 void regrow_logs(struct thread_data *);
166
167 static inline struct io_sample *get_sample(struct io_log *iolog,
168                                            struct io_logs *cur_log,
169                                            uint64_t sample)
170 {
171         return __get_sample(cur_log->log, iolog->log_offset, sample);
172 }
173
174 enum {
175         IP_F_ONRB       = 1,
176         IP_F_ONLIST     = 2,
177         IP_F_TRIMMED    = 4,
178         IP_F_IN_FLIGHT  = 8,
179 };
180
181 /*
182  * When logging io actions, this matches a single sent io_u
183  */
184 struct io_piece {
185         union {
186                 struct rb_node rb_node;
187                 struct flist_head list;
188         };
189         struct flist_head trim_list;
190         union {
191                 int fileno;
192                 struct fio_file *file;
193         };
194         unsigned long long offset;
195         unsigned short numberio;
196         unsigned long len;
197         unsigned int flags;
198         enum fio_ddir ddir;
199         union {
200                 unsigned long delay;
201                 unsigned int file_action;
202         };
203 };
204
205 /*
206  * Log exports
207  */
208 enum file_log_act {
209         FIO_LOG_ADD_FILE,
210         FIO_LOG_OPEN_FILE,
211         FIO_LOG_CLOSE_FILE,
212         FIO_LOG_UNLINK_FILE,
213 };
214
215 struct io_u;
216 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
217 extern void log_io_u(const struct thread_data *, const struct io_u *);
218 extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
219 extern int __must_check init_iolog(struct thread_data *td);
220 extern void log_io_piece(struct thread_data *, struct io_u *);
221 extern void unlog_io_piece(struct thread_data *, struct io_u *);
222 extern void trim_io_piece(struct thread_data *, const struct io_u *);
223 extern void queue_io_piece(struct thread_data *, struct io_piece *);
224 extern void prune_io_piece_log(struct thread_data *);
225 extern void write_iolog_close(struct thread_data *);
226 extern int iolog_compress_init(struct thread_data *, struct sk_out *);
227 extern void iolog_compress_exit(struct thread_data *);
228 extern size_t log_chunk_sizes(struct io_log *);
229
230 #ifdef CONFIG_ZLIB
231 extern int iolog_file_inflate(const char *);
232 #endif
233
234 /*
235  * Logging
236  */
237 struct log_params {
238         struct thread_data *td;
239         unsigned long avg_msec;
240         unsigned long hist_msec;
241         int hist_coarseness;
242         int log_type;
243         int log_offset;
244         int log_gz;
245         int log_gz_store;
246         int log_compress;
247 };
248
249 static inline bool per_unit_log(struct io_log *log)
250 {
251         return log && !log->avg_msec;
252 }
253
254 static inline bool inline_log(struct io_log *log)
255 {
256         return log->log_type == IO_LOG_TYPE_LAT ||
257                 log->log_type == IO_LOG_TYPE_CLAT ||
258                 log->log_type == IO_LOG_TYPE_SLAT;
259 }
260
261 extern void finalize_logs(struct thread_data *td, bool);
262 extern void setup_log(struct io_log **, struct log_params *, const char *);
263 extern void flush_log(struct io_log *, bool);
264 extern void flush_samples(FILE *, void *, uint64_t);
265 extern void free_log(struct io_log *);
266 extern void fio_writeout_logs(bool);
267 extern void td_writeout_logs(struct thread_data *, bool);
268 extern int iolog_cur_flush(struct io_log *, struct io_logs *);
269
270 static inline void init_ipo(struct io_piece *ipo)
271 {
272         memset(ipo, 0, sizeof(*ipo));
273         INIT_FLIST_HEAD(&ipo->trim_list);
274 }
275
276 struct iolog_compress {
277         struct flist_head list;
278         void *buf;
279         size_t len;
280         unsigned int seq;
281 };
282
283 #endif