Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/block/accounting.c
Line
Count
Source
1
/*
2
 * QEMU System Emulator block accounting
3
 *
4
 * Copyright (c) 2011 Christoph Hellwig
5
 * Copyright (c) 2015 Igalia, S.L.
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
 * THE SOFTWARE.
24
 */
25
26
#include "qemu/osdep.h"
27
#include "block/accounting.h"
28
#include "block/block_int.h"
29
#include "qemu/timer.h"
30
#include "system/qtest.h"
31
#include "qapi/error.h"
32
33
static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
34
static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000;
35
36
void block_acct_init(BlockAcctStats *stats)
37
39
{
38
39
    qemu_mutex_init(&stats->lock);
39
39
    if (qtest_enabled()) {
40
3
        clock_type = QEMU_CLOCK_VIRTUAL;
41
3
    }
42
39
    stats->account_invalid = true;
43
39
    stats->account_failed = true;
44
39
}
45
46
static bool bool_from_onoffauto(OnOffAuto val, bool def)
47
76
{
48
76
    switch (val) {
49
76
    case ON_OFF_AUTO_AUTO:
50
76
        return def;
51
0
    case ON_OFF_AUTO_ON:
52
0
        return true;
53
0
    case ON_OFF_AUTO_OFF:
54
0
        return false;
55
0
    default:
56
0
        abort();
57
76
    }
58
76
}
59
60
bool block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid,
61
                      enum OnOffAuto account_failed, uint32_t *stats_intervals,
62
                      uint32_t num_stats_intervals, Error **errp)
63
38
{
64
38
    stats->account_invalid = bool_from_onoffauto(account_invalid,
65
38
                                                 stats->account_invalid);
66
38
    stats->account_failed = bool_from_onoffauto(account_failed,
67
38
                                                stats->account_failed);
68
38
    if (stats_intervals) {
69
0
        for (int i = 0; i < num_stats_intervals; i++) {
70
0
            if (stats_intervals[i] <= 0) {
71
0
                error_setg(errp, "Invalid interval length: %u", stats_intervals[i]);
72
0
                return false;
73
0
            }
74
0
            block_acct_add_interval(stats, stats_intervals[i]);
75
0
        }
76
0
    }
77
38
    return true;
78
38
}
79
80
void block_acct_cleanup(BlockAcctStats *stats)
81
15
{
82
15
    BlockAcctTimedStats *s, *next;
83
15
    block_latency_histograms_clear(stats);
84
85
15
    QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) {
86
0
        g_free(s);
87
0
    }
88
15
    qemu_mutex_destroy(&stats->lock);
89
15
}
90
91
void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length)
92
0
{
93
0
    BlockAcctTimedStats *s;
94
0
    unsigned i;
95
96
0
    s = g_new0(BlockAcctTimedStats, 1);
97
0
    s->interval_length = interval_length;
98
0
    s->stats = stats;
99
0
    qemu_mutex_lock(&stats->lock);
100
0
    QSLIST_INSERT_HEAD(&stats->intervals, s, entries);
101
102
0
    for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
103
0
        timed_average_init(&s->latency[i], clock_type,
104
0
                           (uint64_t) interval_length * NANOSECONDS_PER_SECOND);
105
0
    }
106
0
    qemu_mutex_unlock(&stats->lock);
107
0
}
108
109
BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
110
                                              BlockAcctTimedStats *s)
111
0
{
112
0
    if (s == NULL) {
113
0
        return QSLIST_FIRST(&stats->intervals);
114
0
    } else {
115
0
        return QSLIST_NEXT(s, entries);
116
0
    }
117
0
}
118
119
void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
120
                      int64_t bytes, enum BlockAcctType type)
121
22.8M
{
122
22.8M
    assert(type < BLOCK_MAX_IOTYPE);
123
124
22.8M
    cookie->bytes = bytes;
125
22.8M
    cookie->start_time_ns = qemu_clock_get_ns(clock_type);
126
22.8M
    cookie->type = type;
127
22.8M
}
128
129
/* block_latency_histogram_compare_func:
130
 * Compare @key with interval [@it[0], @it[1]).
131
 * Return: -1 if @key < @it[0]
132
 *          0 if @key in [@it[0], @it[1])
133
 *         +1 if @key >= @it[1]
134
 */
135
static int block_latency_histogram_compare_func(const void *key, const void *it)
136
0
{
137
0
    uint64_t k = *(uint64_t *)key;
138
0
    uint64_t a = ((uint64_t *)it)[0];
139
0
    uint64_t b = ((uint64_t *)it)[1];
140
141
0
    return k < a ? -1 : (k < b ? 0 : 1);
142
0
}
143
144
static void block_latency_histogram_account(BlockLatencyHistogram *hist,
145
                                            int64_t latency_ns)
146
22.8M
{
147
22.8M
    uint64_t *pos;
148
149
22.8M
    if (hist->bins == NULL) {
150
        /* histogram disabled */
151
22.8M
        return;
152
22.8M
    }
153
154
155
0
    if (latency_ns < hist->boundaries[0]) {
156
0
        hist->bins[0]++;
157
0
        return;
158
0
    }
159
160
0
    if (latency_ns >= hist->boundaries[hist->nbins - 2]) {
161
0
        hist->bins[hist->nbins - 1]++;
162
0
        return;
163
0
    }
164
165
0
    pos = bsearch(&latency_ns, hist->boundaries, hist->nbins - 2,
166
0
                  sizeof(hist->boundaries[0]),
167
0
                  block_latency_histogram_compare_func);
168
0
    assert(pos != NULL);
169
170
0
    hist->bins[pos - hist->boundaries + 1]++;
171
0
}
172
173
int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
174
                                uint64List *boundaries)
175
0
{
176
0
    BlockLatencyHistogram *hist = &stats->latency_histogram[type];
177
0
    uint64List *entry;
178
0
    uint64_t *ptr;
179
0
    uint64_t prev = 0;
180
0
    int new_nbins = 1;
181
182
0
    for (entry = boundaries; entry; entry = entry->next) {
183
0
        if (entry->value <= prev) {
184
0
            return -EINVAL;
185
0
        }
186
0
        new_nbins++;
187
0
        prev = entry->value;
188
0
    }
189
190
    /*
191
     * block_latency_histogram_account() assumes that it can always access
192
     * hist->boundaries[0], so require at least one boundary. A histogram with
193
     * a single bin is useless anyway.
194
     */
195
0
    if (new_nbins <= 1) {
196
0
        return -EINVAL;
197
0
    }
198
199
0
    qemu_mutex_lock(&stats->lock);
200
201
0
    hist->nbins = new_nbins;
202
0
    g_free(hist->boundaries);
203
0
    hist->boundaries = g_new(uint64_t, hist->nbins - 1);
204
0
    for (entry = boundaries, ptr = hist->boundaries; entry;
205
0
         entry = entry->next, ptr++)
206
0
    {
207
0
        *ptr = entry->value;
208
0
    }
209
210
0
    g_free(hist->bins);
211
0
    hist->bins = g_new0(uint64_t, hist->nbins);
212
213
0
    qemu_mutex_unlock(&stats->lock);
214
215
0
    return 0;
216
0
}
217
218
void block_latency_histograms_clear(BlockAcctStats *stats)
219
15
{
220
15
    int i;
221
222
15
    qemu_mutex_lock(&stats->lock);
223
224
105
    for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
225
90
        BlockLatencyHistogram *hist = &stats->latency_histogram[i];
226
90
        g_free(hist->bins);
227
90
        g_free(hist->boundaries);
228
90
        memset(hist, 0, sizeof(*hist));
229
90
    }
230
231
15
    qemu_mutex_unlock(&stats->lock);
232
15
}
233
234
static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
235
                                 bool failed)
236
22.8M
{
237
22.8M
    BlockAcctTimedStats *s;
238
22.8M
    int64_t time_ns = qemu_clock_get_ns(clock_type);
239
22.8M
    int64_t latency_ns = time_ns - cookie->start_time_ns;
240
241
22.8M
    if (qtest_enabled()) {
242
22.8M
        latency_ns = qtest_latency_ns;
243
22.8M
    }
244
245
22.8M
    assert(cookie->type < BLOCK_MAX_IOTYPE);
246
247
22.8M
    if (cookie->type == BLOCK_ACCT_NONE) {
248
0
        return;
249
0
    }
250
251
22.8M
    WITH_QEMU_LOCK_GUARD(&stats->lock) {
252
22.8M
        if (failed) {
253
2.57M
            stats->failed_ops[cookie->type]++;
254
20.2M
        } else {
255
20.2M
            stats->nr_bytes[cookie->type] += cookie->bytes;
256
20.2M
            stats->nr_ops[cookie->type]++;
257
20.2M
        }
258
259
22.8M
        block_latency_histogram_account(&stats->latency_histogram[cookie->type],
260
22.8M
                                        latency_ns);
261
262
22.8M
        if (!failed || stats->account_failed) {
263
22.8M
            stats->total_time_ns[cookie->type] += latency_ns;
264
22.8M
            stats->last_access_time_ns = time_ns;
265
266
22.8M
            QSLIST_FOREACH(s, &stats->intervals, entries) {
267
0
                timed_average_account(&s->latency[cookie->type], latency_ns);
268
0
            }
269
22.8M
        }
270
22.8M
    }
271
272
22.8M
    cookie->type = BLOCK_ACCT_NONE;
273
22.8M
}
274
275
void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
276
20.2M
{
277
20.2M
    block_account_one_io(stats, cookie, false);
278
20.2M
}
279
280
void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie)
281
2.57M
{
282
2.57M
    block_account_one_io(stats, cookie, true);
283
2.57M
}
284
285
void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type)
286
9.02k
{
287
9.02k
    assert(type < BLOCK_MAX_IOTYPE);
288
289
    /* block_account_one_io() updates total_time_ns[], but this one does
290
     * not.  The reason is that invalid requests are accounted during their
291
     * submission, therefore there's no actual I/O involved.
292
     */
293
18.0k
    qemu_mutex_lock(&stats->lock);
294
18.0k
    stats->invalid_ops[type]++;
295
296
18.0k
    if (stats->account_invalid) {
297
9.02k
        stats->last_access_time_ns = qemu_clock_get_ns(clock_type);
298
9.02k
    }
299
18.0k
    qemu_mutex_unlock(&stats->lock);
300
18.0k
}
301
302
void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
303
                      int num_requests)
304
0
{
305
0
    assert(type < BLOCK_MAX_IOTYPE);
306
307
0
    qemu_mutex_lock(&stats->lock);
308
0
    stats->merged[type] += num_requests;
309
0
    qemu_mutex_unlock(&stats->lock);
310
0
}
311
312
int64_t block_acct_idle_time_ns(BlockAcctStats *stats)
313
0
{
314
0
    return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns;
315
0
}
316
317
double block_acct_queue_depth(BlockAcctTimedStats *stats,
318
                              enum BlockAcctType type)
319
0
{
320
0
    uint64_t sum, elapsed;
321
322
0
    assert(type < BLOCK_MAX_IOTYPE);
323
0
    assert(qemu_mutex_trylock(&stats->stats->lock) == -EBUSY);
324
325
0
    sum = timed_average_sum(&stats->latency[type], &elapsed);
326
327
0
    return (double) sum / elapsed;
328
0
}