]>
Commit | Line | Data |
---|---|---|
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 | ||
80c71a24 | 26 | #include "qemu/osdep.h" |
5e5a94b6 BC |
27 | #include "block/accounting.h" |
28 | #include "block/block_int.h" | |
a56ebc6b | 29 | #include "qemu/timer.h" |
918a17a4 | 30 | #include "sysemu/qtest.h" |
5e5a94b6 | 31 | |
5519593c | 32 | static QEMUClockType clock_type = QEMU_CLOCK_REALTIME; |
918a17a4 | 33 | static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000; |
5519593c | 34 | |
9caa6f3d | 35 | void block_acct_init(BlockAcctStats *stats) |
362e9299 | 36 | { |
5b50bf77 | 37 | qemu_mutex_init(&stats->lock); |
918a17a4 AG |
38 | if (qtest_enabled()) { |
39 | clock_type = QEMU_CLOCK_VIRTUAL; | |
40 | } | |
362e9299 AG |
41 | } |
42 | ||
9caa6f3d PB |
43 | void block_acct_setup(BlockAcctStats *stats, bool account_invalid, |
44 | bool account_failed) | |
45 | { | |
46 | stats->account_invalid = account_invalid; | |
47 | stats->account_failed = account_failed; | |
48 | } | |
49 | ||
979e9b03 AG |
50 | void block_acct_cleanup(BlockAcctStats *stats) |
51 | { | |
52 | BlockAcctTimedStats *s, *next; | |
53 | QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) { | |
54 | g_free(s); | |
55 | } | |
5b50bf77 | 56 | qemu_mutex_destroy(&stats->lock); |
979e9b03 AG |
57 | } |
58 | ||
59 | void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length) | |
60 | { | |
61 | BlockAcctTimedStats *s; | |
62 | unsigned i; | |
63 | ||
64 | s = g_new0(BlockAcctTimedStats, 1); | |
65 | s->interval_length = interval_length; | |
5b50bf77 PB |
66 | s->stats = stats; |
67 | qemu_mutex_lock(&stats->lock); | |
979e9b03 AG |
68 | QSLIST_INSERT_HEAD(&stats->intervals, s, entries); |
69 | ||
70 | for (i = 0; i < BLOCK_MAX_IOTYPE; i++) { | |
71 | timed_average_init(&s->latency[i], clock_type, | |
72 | (uint64_t) interval_length * NANOSECONDS_PER_SECOND); | |
73 | } | |
5b50bf77 | 74 | qemu_mutex_unlock(&stats->lock); |
979e9b03 AG |
75 | } |
76 | ||
77 | BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats, | |
78 | BlockAcctTimedStats *s) | |
79 | { | |
80 | if (s == NULL) { | |
81 | return QSLIST_FIRST(&stats->intervals); | |
82 | } else { | |
83 | return QSLIST_NEXT(s, entries); | |
84 | } | |
85 | } | |
86 | ||
5366d0c8 BC |
87 | void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie, |
88 | int64_t bytes, enum BlockAcctType type) | |
5e5a94b6 | 89 | { |
28298fd3 | 90 | assert(type < BLOCK_MAX_IOTYPE); |
5e5a94b6 BC |
91 | |
92 | cookie->bytes = bytes; | |
5519593c | 93 | cookie->start_time_ns = qemu_clock_get_ns(clock_type); |
5e5a94b6 BC |
94 | cookie->type = type; |
95 | } | |
96 | ||
b741ae74 VSO |
97 | /* block_latency_histogram_compare_func: |
98 | * Compare @key with interval [@it[0], @it[1]). | |
99 | * Return: -1 if @key < @it[0] | |
100 | * 0 if @key in [@it[0], @it[1]) | |
101 | * +1 if @key >= @it[1] | |
102 | */ | |
103 | static int block_latency_histogram_compare_func(const void *key, const void *it) | |
104 | { | |
105 | uint64_t k = *(uint64_t *)key; | |
106 | uint64_t a = ((uint64_t *)it)[0]; | |
107 | uint64_t b = ((uint64_t *)it)[1]; | |
108 | ||
109 | return k < a ? -1 : (k < b ? 0 : 1); | |
110 | } | |
111 | ||
112 | static void block_latency_histogram_account(BlockLatencyHistogram *hist, | |
113 | int64_t latency_ns) | |
114 | { | |
115 | uint64_t *pos; | |
116 | ||
117 | if (hist->bins == NULL) { | |
118 | /* histogram disabled */ | |
119 | return; | |
120 | } | |
121 | ||
122 | ||
123 | if (latency_ns < hist->boundaries[0]) { | |
124 | hist->bins[0]++; | |
125 | return; | |
126 | } | |
127 | ||
128 | if (latency_ns >= hist->boundaries[hist->nbins - 2]) { | |
129 | hist->bins[hist->nbins - 1]++; | |
130 | return; | |
131 | } | |
132 | ||
133 | pos = bsearch(&latency_ns, hist->boundaries, hist->nbins - 2, | |
134 | sizeof(hist->boundaries[0]), | |
135 | block_latency_histogram_compare_func); | |
136 | assert(pos != NULL); | |
137 | ||
138 | hist->bins[pos - hist->boundaries + 1]++; | |
139 | } | |
140 | ||
141 | int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type, | |
142 | uint64List *boundaries) | |
143 | { | |
144 | BlockLatencyHistogram *hist = &stats->latency_histogram[type]; | |
145 | uint64List *entry; | |
146 | uint64_t *ptr; | |
147 | uint64_t prev = 0; | |
148 | int new_nbins = 1; | |
149 | ||
150 | for (entry = boundaries; entry; entry = entry->next) { | |
151 | if (entry->value <= prev) { | |
152 | return -EINVAL; | |
153 | } | |
154 | new_nbins++; | |
155 | prev = entry->value; | |
156 | } | |
157 | ||
158 | hist->nbins = new_nbins; | |
159 | g_free(hist->boundaries); | |
160 | hist->boundaries = g_new(uint64_t, hist->nbins - 1); | |
161 | for (entry = boundaries, ptr = hist->boundaries; entry; | |
162 | entry = entry->next, ptr++) | |
163 | { | |
164 | *ptr = entry->value; | |
165 | } | |
166 | ||
167 | g_free(hist->bins); | |
168 | hist->bins = g_new0(uint64_t, hist->nbins); | |
169 | ||
170 | return 0; | |
171 | } | |
172 | ||
173 | void block_latency_histograms_clear(BlockAcctStats *stats) | |
174 | { | |
175 | int i; | |
176 | ||
177 | for (i = 0; i < BLOCK_MAX_IOTYPE; i++) { | |
178 | BlockLatencyHistogram *hist = &stats->latency_histogram[i]; | |
179 | g_free(hist->bins); | |
180 | g_free(hist->boundaries); | |
181 | memset(hist, 0, sizeof(*hist)); | |
182 | } | |
183 | } | |
184 | ||
39c1b425 PB |
185 | static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie, |
186 | bool failed) | |
5e5a94b6 | 187 | { |
979e9b03 | 188 | BlockAcctTimedStats *s; |
cb38fffb AG |
189 | int64_t time_ns = qemu_clock_get_ns(clock_type); |
190 | int64_t latency_ns = time_ns - cookie->start_time_ns; | |
191 | ||
918a17a4 AG |
192 | if (qtest_enabled()) { |
193 | latency_ns = qtest_latency_ns; | |
194 | } | |
195 | ||
28298fd3 | 196 | assert(cookie->type < BLOCK_MAX_IOTYPE); |
5e5a94b6 | 197 | |
5b50bf77 PB |
198 | qemu_mutex_lock(&stats->lock); |
199 | ||
39c1b425 PB |
200 | if (failed) { |
201 | stats->failed_ops[cookie->type]++; | |
202 | } else { | |
203 | stats->nr_bytes[cookie->type] += cookie->bytes; | |
204 | stats->nr_ops[cookie->type]++; | |
979e9b03 | 205 | } |
918a17a4 | 206 | |
b741ae74 VSO |
207 | block_latency_histogram_account(&stats->latency_histogram[cookie->type], |
208 | latency_ns); | |
209 | ||
39c1b425 | 210 | if (!failed || stats->account_failed) { |
362e9299 AG |
211 | stats->total_time_ns[cookie->type] += latency_ns; |
212 | stats->last_access_time_ns = time_ns; | |
979e9b03 AG |
213 | |
214 | QSLIST_FOREACH(s, &stats->intervals, entries) { | |
215 | timed_average_account(&s->latency[cookie->type], latency_ns); | |
216 | } | |
362e9299 | 217 | } |
5b50bf77 PB |
218 | |
219 | qemu_mutex_unlock(&stats->lock); | |
7ee12daf AG |
220 | } |
221 | ||
39c1b425 PB |
222 | void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie) |
223 | { | |
224 | block_account_one_io(stats, cookie, false); | |
225 | } | |
226 | ||
227 | void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie) | |
228 | { | |
229 | block_account_one_io(stats, cookie, true); | |
230 | } | |
231 | ||
7ee12daf AG |
232 | void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type) |
233 | { | |
234 | assert(type < BLOCK_MAX_IOTYPE); | |
235 | ||
39c1b425 PB |
236 | /* block_account_one_io() updates total_time_ns[], but this one does |
237 | * not. The reason is that invalid requests are accounted during their | |
238 | * submission, therefore there's no actual I/O involved. | |
239 | */ | |
5b50bf77 | 240 | qemu_mutex_lock(&stats->lock); |
7ee12daf | 241 | stats->invalid_ops[type]++; |
362e9299 AG |
242 | |
243 | if (stats->account_invalid) { | |
244 | stats->last_access_time_ns = qemu_clock_get_ns(clock_type); | |
245 | } | |
5b50bf77 | 246 | qemu_mutex_unlock(&stats->lock); |
7ee12daf | 247 | } |
5e5a94b6 | 248 | |
f4564d53 PL |
249 | void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type, |
250 | int num_requests) | |
251 | { | |
252 | assert(type < BLOCK_MAX_IOTYPE); | |
5b50bf77 PB |
253 | |
254 | qemu_mutex_lock(&stats->lock); | |
f4564d53 | 255 | stats->merged[type] += num_requests; |
5b50bf77 | 256 | qemu_mutex_unlock(&stats->lock); |
f4564d53 | 257 | } |
cb38fffb AG |
258 | |
259 | int64_t block_acct_idle_time_ns(BlockAcctStats *stats) | |
260 | { | |
261 | return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns; | |
262 | } | |
96e4deda AG |
263 | |
264 | double block_acct_queue_depth(BlockAcctTimedStats *stats, | |
265 | enum BlockAcctType type) | |
266 | { | |
267 | uint64_t sum, elapsed; | |
268 | ||
269 | assert(type < BLOCK_MAX_IOTYPE); | |
270 | ||
5b50bf77 | 271 | qemu_mutex_lock(&stats->stats->lock); |
96e4deda | 272 | sum = timed_average_sum(&stats->latency[type], &elapsed); |
5b50bf77 | 273 | qemu_mutex_unlock(&stats->stats->lock); |
96e4deda AG |
274 | |
275 | return (double) sum / elapsed; | |
276 | } |