]> git.proxmox.com Git - mirror_qemu.git/blame - block/accounting.c
qemu-iotests: Reduce racy output in 028
[mirror_qemu.git] / block / accounting.c
CommitLineData
5e5a94b6
BC
1/*
2 * QEMU System Emulator block accounting
3 *
4 * Copyright (c) 2011 Christoph Hellwig
aece5edc 5 * Copyright (c) 2015 Igalia, S.L.
5e5a94b6
BC
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 "block/accounting.h"
27#include "block/block_int.h"
a56ebc6b 28#include "qemu/timer.h"
918a17a4 29#include "sysemu/qtest.h"
5e5a94b6 30
5519593c 31static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
918a17a4 32static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000;
5519593c 33
362e9299
AG
34void block_acct_init(BlockAcctStats *stats, bool account_invalid,
35 bool account_failed)
36{
37 stats->account_invalid = account_invalid;
38 stats->account_failed = account_failed;
918a17a4
AG
39
40 if (qtest_enabled()) {
41 clock_type = QEMU_CLOCK_VIRTUAL;
42 }
362e9299
AG
43}
44
979e9b03
AG
45void block_acct_cleanup(BlockAcctStats *stats)
46{
47 BlockAcctTimedStats *s, *next;
48 QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) {
49 g_free(s);
50 }
51}
52
53void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length)
54{
55 BlockAcctTimedStats *s;
56 unsigned i;
57
58 s = g_new0(BlockAcctTimedStats, 1);
59 s->interval_length = interval_length;
60 QSLIST_INSERT_HEAD(&stats->intervals, s, entries);
61
62 for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
63 timed_average_init(&s->latency[i], clock_type,
64 (uint64_t) interval_length * NANOSECONDS_PER_SECOND);
65 }
66}
67
68BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
69 BlockAcctTimedStats *s)
70{
71 if (s == NULL) {
72 return QSLIST_FIRST(&stats->intervals);
73 } else {
74 return QSLIST_NEXT(s, entries);
75 }
76}
77
5366d0c8
BC
78void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
79 int64_t bytes, enum BlockAcctType type)
5e5a94b6 80{
28298fd3 81 assert(type < BLOCK_MAX_IOTYPE);
5e5a94b6
BC
82
83 cookie->bytes = bytes;
5519593c 84 cookie->start_time_ns = qemu_clock_get_ns(clock_type);
5e5a94b6
BC
85 cookie->type = type;
86}
87
5366d0c8 88void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
5e5a94b6 89{
979e9b03 90 BlockAcctTimedStats *s;
cb38fffb
AG
91 int64_t time_ns = qemu_clock_get_ns(clock_type);
92 int64_t latency_ns = time_ns - cookie->start_time_ns;
93
918a17a4
AG
94 if (qtest_enabled()) {
95 latency_ns = qtest_latency_ns;
96 }
97
28298fd3 98 assert(cookie->type < BLOCK_MAX_IOTYPE);
5e5a94b6 99
5366d0c8
BC
100 stats->nr_bytes[cookie->type] += cookie->bytes;
101 stats->nr_ops[cookie->type]++;
cb38fffb
AG
102 stats->total_time_ns[cookie->type] += latency_ns;
103 stats->last_access_time_ns = time_ns;
979e9b03
AG
104
105 QSLIST_FOREACH(s, &stats->intervals, entries) {
106 timed_average_account(&s->latency[cookie->type], latency_ns);
107 }
5e5a94b6
BC
108}
109
7ee12daf
AG
110void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie)
111{
7ee12daf
AG
112 assert(cookie->type < BLOCK_MAX_IOTYPE);
113
114 stats->failed_ops[cookie->type]++;
362e9299
AG
115
116 if (stats->account_failed) {
979e9b03 117 BlockAcctTimedStats *s;
362e9299
AG
118 int64_t time_ns = qemu_clock_get_ns(clock_type);
119 int64_t latency_ns = time_ns - cookie->start_time_ns;
120
918a17a4
AG
121 if (qtest_enabled()) {
122 latency_ns = qtest_latency_ns;
123 }
124
362e9299
AG
125 stats->total_time_ns[cookie->type] += latency_ns;
126 stats->last_access_time_ns = time_ns;
979e9b03
AG
127
128 QSLIST_FOREACH(s, &stats->intervals, entries) {
129 timed_average_account(&s->latency[cookie->type], latency_ns);
130 }
362e9299 131 }
7ee12daf
AG
132}
133
134void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type)
135{
136 assert(type < BLOCK_MAX_IOTYPE);
137
138 /* block_acct_done() and block_acct_failed() update
139 * total_time_ns[], but this one does not. The reason is that
140 * invalid requests are accounted during their submission,
141 * therefore there's no actual I/O involved. */
142
143 stats->invalid_ops[type]++;
362e9299
AG
144
145 if (stats->account_invalid) {
146 stats->last_access_time_ns = qemu_clock_get_ns(clock_type);
147 }
7ee12daf 148}
5e5a94b6 149
f4564d53
PL
150void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
151 int num_requests)
152{
153 assert(type < BLOCK_MAX_IOTYPE);
154 stats->merged[type] += num_requests;
155}
cb38fffb
AG
156
157int64_t block_acct_idle_time_ns(BlockAcctStats *stats)
158{
159 return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns;
160}
96e4deda
AG
161
162double block_acct_queue_depth(BlockAcctTimedStats *stats,
163 enum BlockAcctType type)
164{
165 uint64_t sum, elapsed;
166
167 assert(type < BLOCK_MAX_IOTYPE);
168
169 sum = timed_average_sum(&stats->latency[type], &elapsed);
170
171 return (double) sum / elapsed;
172}