Merge branch 'fix-randtrimwrite' of https://github.com/minwooim/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
0e14633c
AK
29enum {
30 IO_LOG_SAMPLE_AVG = 0,
31 IO_LOG_SAMPLE_MAX,
32 IO_LOG_SAMPLE_BOTH,
33};
34
35struct io_sample_value {
36 uint64_t val0;
37 uint64_t val1;
38};
af2fde19
SW
39
40union io_sample_data {
0e14633c 41 struct io_sample_value val;
af2fde19
SW
42 struct io_u_plat_entry *plat_entry;
43};
44
0e14633c 45#define sample_val(value) ((union io_sample_data) { .val.val0 = value })
af2fde19
SW
46#define sample_plat(plat) ((union io_sample_data) { .plat_entry = plat })
47
5995a6a4
JA
48/*
49 * A single data sample
50 */
51struct io_sample {
1b42725f 52 uint64_t time;
af2fde19 53 union io_sample_data data;
b26317c9 54 uint32_t __ddir;
03ec570f 55 uint16_t priority;
5fff9543 56 uint64_t bs;
9f0a81a6 57 uint64_t aux[];
5995a6a4
JA
58};
59
9f0a81a6
SK
60/*
61 * Enumerate indexes of auxiliary log data in struct io_sample aux[] array
62 */
63enum {
64 IOS_AUX_OFFSET_INDEX,
14d3134a 65 IOS_AUX_ISSUE_TIME_INDEX,
ae588852
JA
66};
67
ea51b956
JA
68enum {
69 IO_LOG_TYPE_LAT = 1,
70 IO_LOG_TYPE_CLAT,
71 IO_LOG_TYPE_SLAT,
72 IO_LOG_TYPE_BW,
73 IO_LOG_TYPE_IOPS,
1e613c9c 74 IO_LOG_TYPE_HIST,
ea51b956
JA
75};
76
6d37f67f 77#define DEF_LOG_ENTRIES 1024
7e419452
JA
78#define MAX_LOG_ENTRIES (1024 * DEF_LOG_ENTRIES)
79
7e419452
JA
80struct io_logs {
81 struct flist_head list;
82 uint64_t nr_samples;
83 uint64_t max_samples;
84 void *log;
85};
6d37f67f 86
5995a6a4
JA
87/*
88 * Dynamically growing data sample log
89 */
90struct io_log {
b8bc8cba
JA
91 /*
92 * Entries already logged
93 */
7e419452
JA
94 struct flist_head io_logs;
95 uint32_t cur_log_max;
b8bc8cba 96
1fed2080
JA
97 /*
98 * When the current log runs out of space, store events here until
99 * we have a chance to regrow
100 */
101 struct io_logs *pending;
102
b26317c9
JA
103 unsigned int log_ddir_mask;
104
cb7e0ace
JA
105 char *filename;
106
aee2ab67
JA
107 struct thread_data *td;
108
1b42725f 109 unsigned int log_type;
ea51b956 110
3c568239
JA
111 /*
112 * If we fail extending the log, stop collecting more entries.
113 */
7e419452 114 bool disabled;
3c568239 115
ae588852
JA
116 /*
117 * Log offsets
118 */
119 unsigned int log_offset;
120
03ec570f
DLM
121 /*
122 * Log I/O priorities
123 */
124 unsigned int log_prio;
125
14d3134a
SK
126 /*
127 * Log I/O issuing time
128 */
129 unsigned int log_issue_time;
130
aee2ab67
JA
131 /*
132 * Max size of log entries before a chunk is compressed
133 */
134 unsigned int log_gz;
135
b26317c9
JA
136 /*
137 * Don't deflate for storing, just store the compressed bits
138 */
139 unsigned int log_gz_store;
140
b8bc8cba
JA
141 /*
142 * Windowed average, for logging single entries average over some
143 * period of time.
144 */
6eaf09d6 145 struct io_stat avg_window[DDIR_RWDIR_CNT];
b8bc8cba 146 unsigned long avg_msec;
8355119b 147 unsigned long avg_last[DDIR_RWDIR_CNT];
aee2ab67 148
8aa89d70
JA
149 /*
150 * Windowed latency histograms, for keeping track of when we need to
151 * save a copy of the histogram every approximately hist_msec
152 * milliseconds.
153 */
1e613c9c
KC
154 struct io_hist hist_window[DDIR_RWDIR_CNT];
155 unsigned long hist_msec;
868f6f03 156 unsigned int hist_coarseness;
1e613c9c 157
aee2ab67
JA
158 pthread_mutex_t chunk_lock;
159 unsigned int chunk_seq;
160 struct flist_head chunk_list;
858bce70
JA
161
162 pthread_mutex_t deferred_free_lock;
163#define IOLOG_MAX_DEFER 8
164 void *deferred_items[IOLOG_MAX_DEFER];
165 unsigned int deferred;
5995a6a4
JA
166};
167
49e98daa
JA
168/*
169 * If the upper bit is set, then we have the offset as well
170 */
171#define LOG_OFFSET_SAMPLE_BIT 0x80000000U
03ec570f
DLM
172/*
173 * If the bit following the upper bit is set, then we have the priority
174 */
175#define LOG_PRIO_SAMPLE_BIT 0x40000000U
0e14633c
AK
176/*
177 * If the bit following prioity sample vit is set, we report both avg and max
178 */
179#define LOG_AVG_MAX_SAMPLE_BIT 0x20000000U
14d3134a
SK
180/*
181 * If the bit following AVG_MAX_SAMPLE_BIT is set, we report the issue time also
182 */
183#define LOG_ISSUE_TIME_SAMPLE_BIT 0x10000000U
03ec570f 184
0e14633c 185#define LOG_SAMPLE_BITS (LOG_OFFSET_SAMPLE_BIT | LOG_PRIO_SAMPLE_BIT |\
14d3134a
SK
186 LOG_AVG_MAX_SAMPLE_BIT |\
187 LOG_ISSUE_TIME_SAMPLE_BIT)
03ec570f 188#define io_sample_ddir(io) ((io)->__ddir & ~LOG_SAMPLE_BITS)
b26317c9
JA
189
190static inline void io_sample_set_ddir(struct io_log *log,
191 struct io_sample *io,
192 enum fio_ddir ddir)
193{
194 io->__ddir = ddir | log->log_ddir_mask;
195}
196
14d3134a 197static inline size_t __log_entry_sz(bool log_offset, bool log_issue_time)
ae588852 198{
9f0a81a6
SK
199 size_t ret = sizeof(struct io_sample);
200
ae588852 201 if (log_offset)
9f0a81a6
SK
202 ret += sizeof(uint64_t);
203
14d3134a
SK
204 if (log_issue_time)
205 ret += sizeof(uint64_t);
206
9f0a81a6 207 return ret;
ae588852
JA
208}
209
210static inline size_t log_entry_sz(struct io_log *log)
211{
14d3134a 212 return __log_entry_sz(log->log_offset, log->log_issue_time);
ae588852
JA
213}
214
868f6f03
KC
215static inline size_t log_sample_sz(struct io_log *log, struct io_logs *cur_log)
216{
217 return cur_log->nr_samples * log_entry_sz(log);
218}
219
14d3134a
SK
220static inline struct io_sample *__get_sample(void *samples, bool log_offset,
221 bool log_issue_time,
ae588852
JA
222 uint64_t sample)
223{
14d3134a
SK
224 uint64_t sample_offset = sample *
225 __log_entry_sz(log_offset, log_issue_time);
6f9bbb55 226 return (struct io_sample *) ((char *) samples + sample_offset);
ae588852
JA
227}
228
7e419452
JA
229struct io_logs *iolog_cur_log(struct io_log *);
230uint64_t iolog_nr_samples(struct io_log *);
1fed2080 231void regrow_logs(struct thread_data *);
76204de3 232void regrow_agg_logs(void);
7e419452 233
ae588852 234static inline struct io_sample *get_sample(struct io_log *iolog,
7e419452 235 struct io_logs *cur_log,
ae588852
JA
236 uint64_t sample)
237{
14d3134a
SK
238 return __get_sample(cur_log->log,
239 iolog->log_offset, iolog->log_issue_time, sample);
ae588852
JA
240}
241
0d29de83
JA
242enum {
243 IP_F_ONRB = 1,
244 IP_F_ONLIST = 2,
245 IP_F_TRIMMED = 4,
f9401285 246 IP_F_IN_FLIGHT = 8,
0d29de83
JA
247};
248
5995a6a4
JA
249/*
250 * When logging io actions, this matches a single sent io_u
251 */
252struct io_piece {
253 union {
e96c0125 254 struct fio_rb_node rb_node;
5995a6a4
JA
255 struct flist_head list;
256 };
0d29de83 257 struct flist_head trim_list;
5995a6a4
JA
258 union {
259 int fileno;
260 struct fio_file *file;
261 };
262 unsigned long long offset;
da0a7bd2 263 unsigned short numberio;
5995a6a4 264 unsigned long len;
a917a8b3 265 unsigned int flags;
5995a6a4 266 enum fio_ddir ddir;
315bbf01
MG
267 unsigned long delay;
268 unsigned int file_action;
5995a6a4
JA
269};
270
271/*
272 * Log exports
273 */
274enum file_log_act {
275 FIO_LOG_ADD_FILE,
276 FIO_LOG_OPEN_FILE,
277 FIO_LOG_CLOSE_FILE,
278 FIO_LOG_UNLINK_FILE,
279};
280
8062f527 281struct io_u;
5995a6a4 282extern int __must_check read_iolog_get(struct thread_data *, struct io_u *);
1f440ece 283extern void log_io_u(const struct thread_data *, const struct io_u *);
5995a6a4 284extern void log_file(struct thread_data *, struct fio_file *, enum file_log_act);
b153f94a 285extern bool __must_check init_iolog(struct thread_data *td);
5995a6a4 286extern void log_io_piece(struct thread_data *, struct io_u *);
890b6656 287extern void unlog_io_piece(struct thread_data *, struct io_u *);
94a78d52 288extern void trim_io_piece(const struct io_u *);
5995a6a4
JA
289extern void queue_io_piece(struct thread_data *, struct io_piece *);
290extern void prune_io_piece_log(struct thread_data *);
291extern void write_iolog_close(struct thread_data *);
a21ed2b5 292int64_t iolog_items_to_fetch(struct thread_data *td);
24660963 293extern int iolog_compress_init(struct thread_data *, struct sk_out *);
155f2f02 294extern void iolog_compress_exit(struct thread_data *);
0cba0f91 295extern size_t log_chunk_sizes(struct io_log *);
71e6e5a2 296extern int init_io_u_buffers(struct thread_data *);
315bbf01
MG
297extern unsigned long long delay_since_ttime(const struct thread_data *,
298 unsigned long long);
5995a6a4 299
b26317c9
JA
300#ifdef CONFIG_ZLIB
301extern int iolog_file_inflate(const char *);
302#endif
303
5995a6a4
JA
304/*
305 * Logging
306 */
aee2ab67
JA
307struct log_params {
308 struct thread_data *td;
309 unsigned long avg_msec;
1e613c9c
KC
310 unsigned long hist_msec;
311 int hist_coarseness;
aee2ab67
JA
312 int log_type;
313 int log_offset;
03ec570f 314 int log_prio;
14d3134a 315 int log_issue_time;
aee2ab67 316 int log_gz;
b26317c9 317 int log_gz_store;
aee2ab67
JA
318 int log_compress;
319};
320
a47591e4
JA
321static inline bool per_unit_log(struct io_log *log)
322{
83636545 323 return log && (!log->avg_msec || log->log_gz || log->log_gz_store);
a47591e4
JA
324}
325
b392f36d
JA
326static inline bool inline_log(struct io_log *log)
327{
328 return log->log_type == IO_LOG_TYPE_LAT ||
329 log->log_type == IO_LOG_TYPE_CLAT ||
330 log->log_type == IO_LOG_TYPE_SLAT;
331}
332
a79f17bf
SW
333static inline void ipo_bytes_align(unsigned int replay_align, struct io_piece *ipo)
334{
3dd2e8aa 335 if (!replay_align)
a79f17bf
SW
336 return;
337
338 ipo->offset &= ~(replay_align - (uint64_t)1);
339}
340
a47591e4 341extern void finalize_logs(struct thread_data *td, bool);
aee2ab67 342extern void setup_log(struct io_log **, struct log_params *, const char *);
60a25727 343extern void flush_log(struct io_log *, bool);
bead01d7 344extern void flush_samples(FILE *, void *, uint64_t);
6cc0e5aa 345extern uint64_t hist_sum(int, int, uint64_t *, uint64_t *);
518dac09 346extern void free_log(struct io_log *);
a47591e4
JA
347extern void fio_writeout_logs(bool);
348extern void td_writeout_logs(struct thread_data *, bool);
7e419452 349extern int iolog_cur_flush(struct io_log *, struct io_logs *);
5995a6a4 350
0d29de83
JA
351static inline void init_ipo(struct io_piece *ipo)
352{
8812d7f2 353 INIT_FLIST_HEAD(&ipo->list);
0d29de83
JA
354 INIT_FLIST_HEAD(&ipo->trim_list);
355}
356
0cba0f91
JA
357struct iolog_compress {
358 struct flist_head list;
359 void *buf;
360 size_t len;
361 unsigned int seq;
362};
363
5995a6a4 364#endif