Merge tag 'pull-target-arm-20221121' of https://git.linaro.org/people/pmaydell/qemu...
[qemu/ar7.git] / block / accounting.c
blob2829745377a5743d7c7a4fd0af5a5e402e91c041
1 /*
2 * QEMU System Emulator block accounting
4 * Copyright (c) 2011 Christoph Hellwig
5 * Copyright (c) 2015 Igalia, S.L.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
26 #include "qemu/osdep.h"
27 #include "block/accounting.h"
28 #include "block/block_int.h"
29 #include "qemu/timer.h"
30 #include "sysemu/qtest.h"
32 static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
33 static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000;
35 void block_acct_init(BlockAcctStats *stats)
37 qemu_mutex_init(&stats->lock);
38 if (qtest_enabled()) {
39 clock_type = QEMU_CLOCK_VIRTUAL;
41 stats->account_invalid = true;
42 stats->account_failed = true;
45 static bool bool_from_onoffauto(OnOffAuto val, bool def)
47 switch (val) {
48 case ON_OFF_AUTO_AUTO:
49 return def;
50 case ON_OFF_AUTO_ON:
51 return true;
52 case ON_OFF_AUTO_OFF:
53 return false;
54 default:
55 abort();
59 void block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid,
60 enum OnOffAuto account_failed)
62 stats->account_invalid = bool_from_onoffauto(account_invalid,
63 stats->account_invalid);
64 stats->account_failed = bool_from_onoffauto(account_failed,
65 stats->account_failed);
68 void block_acct_cleanup(BlockAcctStats *stats)
70 BlockAcctTimedStats *s, *next;
71 QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) {
72 g_free(s);
74 qemu_mutex_destroy(&stats->lock);
77 void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length)
79 BlockAcctTimedStats *s;
80 unsigned i;
82 s = g_new0(BlockAcctTimedStats, 1);
83 s->interval_length = interval_length;
84 s->stats = stats;
85 qemu_mutex_lock(&stats->lock);
86 QSLIST_INSERT_HEAD(&stats->intervals, s, entries);
88 for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
89 timed_average_init(&s->latency[i], clock_type,
90 (uint64_t) interval_length * NANOSECONDS_PER_SECOND);
92 qemu_mutex_unlock(&stats->lock);
95 BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
96 BlockAcctTimedStats *s)
98 if (s == NULL) {
99 return QSLIST_FIRST(&stats->intervals);
100 } else {
101 return QSLIST_NEXT(s, entries);
105 void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
106 int64_t bytes, enum BlockAcctType type)
108 assert(type < BLOCK_MAX_IOTYPE);
110 cookie->bytes = bytes;
111 cookie->start_time_ns = qemu_clock_get_ns(clock_type);
112 cookie->type = type;
115 /* block_latency_histogram_compare_func:
116 * Compare @key with interval [@it[0], @it[1]).
117 * Return: -1 if @key < @it[0]
118 * 0 if @key in [@it[0], @it[1])
119 * +1 if @key >= @it[1]
121 static int block_latency_histogram_compare_func(const void *key, const void *it)
123 uint64_t k = *(uint64_t *)key;
124 uint64_t a = ((uint64_t *)it)[0];
125 uint64_t b = ((uint64_t *)it)[1];
127 return k < a ? -1 : (k < b ? 0 : 1);
130 static void block_latency_histogram_account(BlockLatencyHistogram *hist,
131 int64_t latency_ns)
133 uint64_t *pos;
135 if (hist->bins == NULL) {
136 /* histogram disabled */
137 return;
141 if (latency_ns < hist->boundaries[0]) {
142 hist->bins[0]++;
143 return;
146 if (latency_ns >= hist->boundaries[hist->nbins - 2]) {
147 hist->bins[hist->nbins - 1]++;
148 return;
151 pos = bsearch(&latency_ns, hist->boundaries, hist->nbins - 2,
152 sizeof(hist->boundaries[0]),
153 block_latency_histogram_compare_func);
154 assert(pos != NULL);
156 hist->bins[pos - hist->boundaries + 1]++;
159 int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type,
160 uint64List *boundaries)
162 BlockLatencyHistogram *hist = &stats->latency_histogram[type];
163 uint64List *entry;
164 uint64_t *ptr;
165 uint64_t prev = 0;
166 int new_nbins = 1;
168 for (entry = boundaries; entry; entry = entry->next) {
169 if (entry->value <= prev) {
170 return -EINVAL;
172 new_nbins++;
173 prev = entry->value;
176 hist->nbins = new_nbins;
177 g_free(hist->boundaries);
178 hist->boundaries = g_new(uint64_t, hist->nbins - 1);
179 for (entry = boundaries, ptr = hist->boundaries; entry;
180 entry = entry->next, ptr++)
182 *ptr = entry->value;
185 g_free(hist->bins);
186 hist->bins = g_new0(uint64_t, hist->nbins);
188 return 0;
191 void block_latency_histograms_clear(BlockAcctStats *stats)
193 int i;
195 for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
196 BlockLatencyHistogram *hist = &stats->latency_histogram[i];
197 g_free(hist->bins);
198 g_free(hist->boundaries);
199 memset(hist, 0, sizeof(*hist));
203 static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
204 bool failed)
206 BlockAcctTimedStats *s;
207 int64_t time_ns = qemu_clock_get_ns(clock_type);
208 int64_t latency_ns = time_ns - cookie->start_time_ns;
210 if (qtest_enabled()) {
211 latency_ns = qtest_latency_ns;
214 assert(cookie->type < BLOCK_MAX_IOTYPE);
216 if (cookie->type == BLOCK_ACCT_NONE) {
217 return;
220 WITH_QEMU_LOCK_GUARD(&stats->lock) {
221 if (failed) {
222 stats->failed_ops[cookie->type]++;
223 } else {
224 stats->nr_bytes[cookie->type] += cookie->bytes;
225 stats->nr_ops[cookie->type]++;
228 block_latency_histogram_account(&stats->latency_histogram[cookie->type],
229 latency_ns);
231 if (!failed || stats->account_failed) {
232 stats->total_time_ns[cookie->type] += latency_ns;
233 stats->last_access_time_ns = time_ns;
235 QSLIST_FOREACH(s, &stats->intervals, entries) {
236 timed_average_account(&s->latency[cookie->type], latency_ns);
241 cookie->type = BLOCK_ACCT_NONE;
244 void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
246 block_account_one_io(stats, cookie, false);
249 void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie)
251 block_account_one_io(stats, cookie, true);
254 void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type)
256 assert(type < BLOCK_MAX_IOTYPE);
258 /* block_account_one_io() updates total_time_ns[], but this one does
259 * not. The reason is that invalid requests are accounted during their
260 * submission, therefore there's no actual I/O involved.
262 qemu_mutex_lock(&stats->lock);
263 stats->invalid_ops[type]++;
265 if (stats->account_invalid) {
266 stats->last_access_time_ns = qemu_clock_get_ns(clock_type);
268 qemu_mutex_unlock(&stats->lock);
271 void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
272 int num_requests)
274 assert(type < BLOCK_MAX_IOTYPE);
276 qemu_mutex_lock(&stats->lock);
277 stats->merged[type] += num_requests;
278 qemu_mutex_unlock(&stats->lock);
281 int64_t block_acct_idle_time_ns(BlockAcctStats *stats)
283 return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns;
286 double block_acct_queue_depth(BlockAcctTimedStats *stats,
287 enum BlockAcctType type)
289 uint64_t sum, elapsed;
291 assert(type < BLOCK_MAX_IOTYPE);
293 qemu_mutex_lock(&stats->stats->lock);
294 sum = timed_average_sum(&stats->latency[type], &elapsed);
295 qemu_mutex_unlock(&stats->stats->lock);
297 return (double) sum / elapsed;