Merge branch 'div-by-zero' of https://github.com/vincentkfu/fio
[fio.git] / iolog.h
CommitLineData
5995a6a4
JA
1#ifndef FIO_IOLOG_H
2#define FIO_IOLOG_H
3
3d2d14bc
SW
4#include <stdio.h>
5
05775438 6#include "lib/rbtree.h"
c7c6cb4c 7#include "lib/ieee754.h"
836fcc0f 8#include "flist.h"
618ee94c 9#include "ioengines.h"
802ad4a8 10
5995a6a4
JA
11/*
12 * Use for maintaining statistics
13 */
14struct io_stat {
7b9f733a
JA
15 uint64_t max_val;
16 uint64_t min_val;
17 uint64_t samples;
5995a6a4 18
802ad4a8
JA
19 fio_fp64_t mean;
20 fio_fp64_t S;
5995a6a4
JA
21};
22
1e613c9c
KC
23struct io_hist {
24 uint64_t samples;
25 unsigned long hist_last;
d730bc58 26 struct flist_head list;
1e613c9c
KC
27};
28
af2fde19
SW
29
30union 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
5995a6a4
JA
38/*
39 * A single data sample
40 */
41struct io_sample {
1b42725f 42 uint64_t time;
af2fde19 43 union io_sample_data data;
b26317c9 44 uint32_t __ddir;
b2a432bf 45 uint8_t priority_bit;
5fff9543 46 uint64_t bs;
5995a6a4
JA
47};
48
ae588852
JA
49struct io_sample_offset {
50 struct io_sample s;
51 uint64_t offset;
52};
53
ea51b956
JA
54enum {
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,
1e613c9c 60 IO_LOG_TYPE_HIST,
ea51b956
JA
61};
62
6d37f67f 63#define DEF_LOG_ENTRIES 1024
7e419452
JA
64#define MAX_LOG_ENTRIES (1024 * DEF_LOG_ENTRIES)
65
7e419452
JA
66struct io_logs {
67 struct flist_head list;
68 uint64_t nr_samples;
69 uint64_t max_samples;
70 void *log;
71};
6d37f67f 72
5995a6a4
JA
73/*
74 * Dynamically growing data sample log
75 */
76struct io_log {
b8bc8cba
JA
77 /*
78 * Entries already logged
79 */
7e419452
JA
80 struct flist_head io_logs;
81 uint32_t cur_log_max;
b8bc8cba 82
1fed2080
JA
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
b26317c9
JA
89 unsigned int log_ddir_mask;
90
cb7e0ace
JA
91 char *filename;
92
aee2ab67
JA
93 struct thread_data *td;
94
1b42725f 95 unsigned int log_type;
ea51b956 96
3c568239
JA
97 /*
98 * If we fail extending the log, stop collecting more entries.
99 */
7e419452 100 bool disabled;
3c568239 101
ae588852
JA
102 /*
103 * Log offsets
104 */
105 unsigned int log_offset;
106
aee2ab67
JA
107 /*
108 * Max size of log entries before a chunk is compressed
109 */
110 unsigned int log_gz;
111
b26317c9
JA
112 /*
113 * Don't deflate for storing, just store the compressed bits
114 */
115 unsigned int log_gz_store;
116
b8bc8cba
JA
117 /*
118 * Windowed average, for logging single entries average over some
119 * period of time.
120 */
6eaf09d6 121 struct io_stat avg_window[DDIR_RWDIR_CNT];
b8bc8cba 122 unsigned long avg_msec;
8355119b 123 unsigned long avg_last[DDIR_RWDIR_CNT];
aee2ab67 124
8aa89d70
JA
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 */
1e613c9c
KC
130 struct io_hist hist_window[DDIR_RWDIR_CNT];
131 unsigned long hist_msec;
868f6f03 132 unsigned int hist_coarseness;
1e613c9c 133
aee2ab67
JA
134 pthread_mutex_t chunk_lock;
135 unsigned int chunk_seq;
136 struct flist_head chunk_list;
858bce70
JA
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;
5995a6a4
JA
142};
143
49e98daa
JA
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)
b26317c9
JA
149
150static 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
ae588852
JA
157static 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
165static inline size_t log_entry_sz(struct io_log *log)
166{
167 return __log_entry_sz(log->log_offset);
168}
169
868f6f03
KC
170static 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
ae588852
JA
175static inline struct io_sample *__get_sample(void *samples, int log_offset,
176 uint64_t sample)
177{
6f9bbb55
CB
178 uint64_t sample_offset = sample * __log_entry_sz(log_offset);
179 return (struct io_sample *) ((char *) samples + sample_offset);
ae588852
JA
180}
181
7e419452
JA
182struct io_logs *iolog_cur_log(struct io_log *);
183uint64_t iolog_nr_samples(struct io_log *);
1fed2080 184void regrow_logs(struct thread_data *);
7e419452 185
ae588852 186static inline struct io_sample *get_sample(struct io_log *iolog,
7e419452 187 struct io_logs *cur_log,
ae588852
JA
188 uint64_t sample)
189{
7e419452 190 return __get_sample(cur_log->log, iolog->log_offset, sample);
ae588852
JA
191}
192
0d29de83
JA
193enum {
194 IP_F_ONRB = 1,
195 IP_F_ONLIST = 2,
196 IP_F_TRIMMED = 4,
f9401285 197 IP_F_IN_FLIGHT = 8,
0d29de83
JA
198};
199
5995a6a4
JA
200/*
201 * When logging io actions, this matches a single sent io_u
202 */
203struct io_piece {
204 union {
e96c0125 205 struct fio_rb_node rb_node;
5995a6a4
JA
206 struct flist_head list;
207 };
0d29de83 208 struct flist_head trim_list;
5995a6a4
JA
209 union {
210 int fileno;
211 struct fio_file *file;
212 };
213 unsigned long long offset;
da0a7bd2 214 unsigned short numberio;
5995a6a4 215 unsigned long len;
a917a8b3 216 unsigned int flags;
5995a6a4
JA
217 enum fio_ddir ddir;
218 union {
219 unsigned long delay;
220 unsigned int file_action;
221 };
222};
223
224/*
225 * Log exports
226 */
227enum 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
8062f527 234struct io_u;
5995a6a4 235extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
1f440ece 236extern void log_io_u(const struct thread_data *, const struct io_u *);
5995a6a4 237extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
b153f94a 238extern bool __must_check init_iolog(struct thread_data *td);
5995a6a4 239extern void log_io_piece(struct thread_data *, struct io_u *);
890b6656 240extern void unlog_io_piece(struct thread_data *, struct io_u *);
94a78d52 241extern void trim_io_piece(const struct io_u *);
5995a6a4
JA
242extern void queue_io_piece(struct thread_data *, struct io_piece *);
243extern void prune_io_piece_log(struct thread_data *);
244extern void write_iolog_close(struct thread_data *);
24660963 245extern int iolog_compress_init(struct thread_data *, struct sk_out *);
155f2f02 246extern void iolog_compress_exit(struct thread_data *);
0cba0f91 247extern size_t log_chunk_sizes(struct io_log *);
71e6e5a2 248extern int init_io_u_buffers(struct thread_data *);
5995a6a4 249
b26317c9
JA
250#ifdef CONFIG_ZLIB
251extern int iolog_file_inflate(const char *);
252#endif
253
5995a6a4
JA
254/*
255 * Logging
256 */
aee2ab67
JA
257struct log_params {
258 struct thread_data *td;
259 unsigned long avg_msec;
1e613c9c
KC
260 unsigned long hist_msec;
261 int hist_coarseness;
aee2ab67
JA
262 int log_type;
263 int log_offset;
264 int log_gz;
b26317c9 265 int log_gz_store;
aee2ab67
JA
266 int log_compress;
267};
268
a47591e4
JA
269static inline bool per_unit_log(struct io_log *log)
270{
83636545 271 return log && (!log->avg_msec || log->log_gz || log->log_gz_store);
a47591e4
JA
272}
273
b392f36d
JA
274static 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
a79f17bf
SW
281static inline void ipo_bytes_align(unsigned int replay_align, struct io_piece *ipo)
282{
3dd2e8aa 283 if (!replay_align)
a79f17bf
SW
284 return;
285
286 ipo->offset &= ~(replay_align - (uint64_t)1);
287}
288
a47591e4 289extern void finalize_logs(struct thread_data *td, bool);
aee2ab67 290extern void setup_log(struct io_log **, struct log_params *, const char *);
60a25727 291extern void flush_log(struct io_log *, bool);
bead01d7 292extern void flush_samples(FILE *, void *, uint64_t);
6cc0e5aa 293extern uint64_t hist_sum(int, int, uint64_t *, uint64_t *);
518dac09 294extern void free_log(struct io_log *);
a47591e4
JA
295extern void fio_writeout_logs(bool);
296extern void td_writeout_logs(struct thread_data *, bool);
7e419452 297extern int iolog_cur_flush(struct io_log *, struct io_logs *);
5995a6a4 298
0d29de83
JA
299static inline void init_ipo(struct io_piece *ipo)
300{
8812d7f2 301 INIT_FLIST_HEAD(&ipo->list);
0d29de83
JA
302 INIT_FLIST_HEAD(&ipo->trim_list);
303}
304
0cba0f91
JA
305struct iolog_compress {
306 struct flist_head list;
307 void *buf;
308 size_t len;
309 unsigned int seq;
310};
311
5995a6a4 312#endif