Improve logging accuracy
[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 /*
22  * A single data sample
23  */
24 struct io_sample {
25         uint64_t time;
26         uint64_t val;
27         uint32_t __ddir;
28         uint32_t bs;
29 };
30
31 struct io_sample_offset {
32         struct io_sample s;
33         uint64_t offset;
34 };
35
36 enum {
37         IO_LOG_TYPE_LAT = 1,
38         IO_LOG_TYPE_CLAT,
39         IO_LOG_TYPE_SLAT,
40         IO_LOG_TYPE_BW,
41         IO_LOG_TYPE_IOPS,
42 };
43
44 #define DEF_LOG_ENTRIES         1024
45
46 /*
47  * Dynamically growing data sample log
48  */
49 struct io_log {
50         /*
51          * Entries already logged
52          */
53         uint64_t nr_samples;
54         uint64_t max_samples;
55         void *log;
56
57         unsigned int log_ddir_mask;
58
59         char *filename;
60
61         struct thread_data *td;
62
63         unsigned int log_type;
64
65         /*
66          * If we fail extending the log, stop collecting more entries.
67          */
68         unsigned int disabled;
69
70         /*
71          * Log offsets
72          */
73         unsigned int log_offset;
74
75         /*
76          * Max size of log entries before a chunk is compressed
77          */
78         unsigned int log_gz;
79
80         /*
81          * Don't deflate for storing, just store the compressed bits
82          */
83         unsigned int log_gz_store;
84
85         /*
86          * Windowed average, for logging single entries average over some
87          * period of time.
88          */
89         struct io_stat avg_window[DDIR_RWDIR_CNT];
90         unsigned long avg_msec;
91         unsigned long avg_last;
92
93         pthread_mutex_t chunk_lock;
94         unsigned int chunk_seq;
95         struct flist_head chunk_list;
96 };
97
98 /*
99  * If the upper bit is set, then we have the offset as well
100  */
101 #define LOG_OFFSET_SAMPLE_BIT   0x80000000U
102 #define io_sample_ddir(io)      ((io)->__ddir & ~LOG_OFFSET_SAMPLE_BIT)
103
104 static inline void io_sample_set_ddir(struct io_log *log,
105                                       struct io_sample *io,
106                                       enum fio_ddir ddir)
107 {
108         io->__ddir = ddir | log->log_ddir_mask;
109 }
110
111 static inline size_t __log_entry_sz(int log_offset)
112 {
113         if (log_offset)
114                 return sizeof(struct io_sample_offset);
115         else
116                 return sizeof(struct io_sample);
117 }
118
119 static inline size_t log_entry_sz(struct io_log *log)
120 {
121         return __log_entry_sz(log->log_offset);
122 }
123
124 static inline struct io_sample *__get_sample(void *samples, int log_offset,
125                                              uint64_t sample)
126 {
127         uint64_t sample_offset = sample * __log_entry_sz(log_offset);
128         return (struct io_sample *) ((char *) samples + sample_offset);
129 }
130
131 static inline struct io_sample *get_sample(struct io_log *iolog,
132                                            uint64_t sample)
133 {
134         return __get_sample(iolog->log, iolog->log_offset, sample);
135 }
136
137 enum {
138         IP_F_ONRB       = 1,
139         IP_F_ONLIST     = 2,
140         IP_F_TRIMMED    = 4,
141         IP_F_IN_FLIGHT  = 8,
142 };
143
144 /*
145  * When logging io actions, this matches a single sent io_u
146  */
147 struct io_piece {
148         union {
149                 struct rb_node rb_node;
150                 struct flist_head list;
151         };
152         struct flist_head trim_list;
153         union {
154                 int fileno;
155                 struct fio_file *file;
156         };
157         unsigned long long offset;
158         unsigned short numberio;
159         unsigned long len;
160         unsigned int flags;
161         enum fio_ddir ddir;
162         union {
163                 unsigned long delay;
164                 unsigned int file_action;
165         };
166 };
167
168 /*
169  * Log exports
170  */
171 enum file_log_act {
172         FIO_LOG_ADD_FILE,
173         FIO_LOG_OPEN_FILE,
174         FIO_LOG_CLOSE_FILE,
175         FIO_LOG_UNLINK_FILE,
176 };
177
178 struct io_u;
179 extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
180 extern void log_io_u(const struct thread_data *, const struct io_u *);
181 extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
182 extern int __must_check init_iolog(struct thread_data *td);
183 extern void log_io_piece(struct thread_data *, struct io_u *);
184 extern void unlog_io_piece(struct thread_data *, struct io_u *);
185 extern void trim_io_piece(struct thread_data *, const struct io_u *);
186 extern void queue_io_piece(struct thread_data *, struct io_piece *);
187 extern void prune_io_piece_log(struct thread_data *);
188 extern void write_iolog_close(struct thread_data *);
189 extern int iolog_compress_init(struct thread_data *, struct sk_out *);
190 extern void iolog_compress_exit(struct thread_data *);
191 extern size_t log_chunk_sizes(struct io_log *);
192
193 #ifdef CONFIG_ZLIB
194 extern int iolog_file_inflate(const char *);
195 #endif
196
197 /*
198  * Logging
199  */
200 struct log_params {
201         struct thread_data *td;
202         unsigned long avg_msec;
203         int log_type;
204         int log_offset;
205         int log_gz;
206         int log_gz_store;
207         int log_compress;
208 };
209
210 static inline bool per_unit_log(struct io_log *log)
211 {
212         return log && !log->avg_msec;
213 }
214
215 extern void finalize_logs(struct thread_data *td, bool);
216 extern void setup_log(struct io_log **, struct log_params *, const char *);
217 extern void flush_log(struct io_log *, int);
218 extern void flush_samples(FILE *, void *, uint64_t);
219 extern void free_log(struct io_log *);
220 extern void fio_writeout_logs(bool);
221 extern void td_writeout_logs(struct thread_data *, bool);
222 extern int iolog_flush(struct io_log *, int);
223
224 static inline void init_ipo(struct io_piece *ipo)
225 {
226         memset(ipo, 0, sizeof(*ipo));
227         INIT_FLIST_HEAD(&ipo->trim_list);
228 }
229
230 struct iolog_compress {
231         struct flist_head list;
232         void *buf;
233         size_t len;
234         unsigned int seq;
235 };
236
237 #endif