Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU System Emulator block accounting |
| 3 | * |
| 4 | * Copyright (c) 2011 Christoph Hellwig |
Alberto Garcia | aece5ed | 2015-10-28 17:33:18 +0200 | [diff] [blame] | 5 | * Copyright (c) 2015 Igalia, S.L. |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 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 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 27 | #include "block/accounting.h" |
| 28 | #include "block/block_int.h" |
Paolo Bonzini | a56ebc6 | 2014-11-28 11:38:03 +0000 | [diff] [blame] | 29 | #include "qemu/timer.h" |
Alberto Garcia | 918a17a | 2015-10-28 17:33:09 +0200 | [diff] [blame] | 30 | #include "sysemu/qtest.h" |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 31 | |
Alberto Garcia | 5519593 | 2015-10-28 17:33:00 +0200 | [diff] [blame] | 32 | static QEMUClockType clock_type = QEMU_CLOCK_REALTIME; |
Alberto Garcia | 918a17a | 2015-10-28 17:33:09 +0200 | [diff] [blame] | 33 | static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000; |
Alberto Garcia | 5519593 | 2015-10-28 17:33:00 +0200 | [diff] [blame] | 34 | |
Paolo Bonzini | 9caa6f3 | 2017-06-05 14:39:07 +0200 | [diff] [blame] | 35 | void block_acct_init(BlockAcctStats *stats) |
Alberto Garcia | 362e929 | 2015-10-28 17:33:04 +0200 | [diff] [blame] | 36 | { |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 37 | qemu_mutex_init(&stats->lock); |
Alberto Garcia | 918a17a | 2015-10-28 17:33:09 +0200 | [diff] [blame] | 38 | if (qtest_enabled()) { |
| 39 | clock_type = QEMU_CLOCK_VIRTUAL; |
| 40 | } |
Denis V. Lunev | 62a6c30 | 2022-08-24 11:50:44 +0200 | [diff] [blame] | 41 | stats->account_invalid = true; |
| 42 | stats->account_failed = true; |
Alberto Garcia | 362e929 | 2015-10-28 17:33:04 +0200 | [diff] [blame] | 43 | } |
| 44 | |
Denis V. Lunev | b2aaf35 | 2022-08-24 11:50:43 +0200 | [diff] [blame] | 45 | static bool bool_from_onoffauto(OnOffAuto val, bool def) |
Paolo Bonzini | 9caa6f3 | 2017-06-05 14:39:07 +0200 | [diff] [blame] | 46 | { |
Denis V. Lunev | b2aaf35 | 2022-08-24 11:50:43 +0200 | [diff] [blame] | 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(); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void block_acct_setup(BlockAcctStats *stats, enum OnOffAuto account_invalid, |
| 60 | enum OnOffAuto account_failed) |
| 61 | { |
Denis V. Lunev | 62a6c30 | 2022-08-24 11:50:44 +0200 | [diff] [blame] | 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); |
Paolo Bonzini | 9caa6f3 | 2017-06-05 14:39:07 +0200 | [diff] [blame] | 66 | } |
| 67 | |
Alberto Garcia | 979e9b0 | 2015-10-28 17:33:05 +0200 | [diff] [blame] | 68 | void block_acct_cleanup(BlockAcctStats *stats) |
| 69 | { |
| 70 | BlockAcctTimedStats *s, *next; |
| 71 | QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) { |
| 72 | g_free(s); |
| 73 | } |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 74 | qemu_mutex_destroy(&stats->lock); |
Alberto Garcia | 979e9b0 | 2015-10-28 17:33:05 +0200 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length) |
| 78 | { |
| 79 | BlockAcctTimedStats *s; |
| 80 | unsigned i; |
| 81 | |
| 82 | s = g_new0(BlockAcctTimedStats, 1); |
| 83 | s->interval_length = interval_length; |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 84 | s->stats = stats; |
| 85 | qemu_mutex_lock(&stats->lock); |
Alberto Garcia | 979e9b0 | 2015-10-28 17:33:05 +0200 | [diff] [blame] | 86 | QSLIST_INSERT_HEAD(&stats->intervals, s, entries); |
| 87 | |
| 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); |
| 91 | } |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 92 | qemu_mutex_unlock(&stats->lock); |
Alberto Garcia | 979e9b0 | 2015-10-28 17:33:05 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats, |
| 96 | BlockAcctTimedStats *s) |
| 97 | { |
| 98 | if (s == NULL) { |
| 99 | return QSLIST_FIRST(&stats->intervals); |
| 100 | } else { |
| 101 | return QSLIST_NEXT(s, entries); |
| 102 | } |
| 103 | } |
| 104 | |
Benoît Canet | 5366d0c | 2014-09-05 15:46:18 +0200 | [diff] [blame] | 105 | void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie, |
| 106 | int64_t bytes, enum BlockAcctType type) |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 107 | { |
Benoît Canet | 28298fd | 2014-09-05 15:46:17 +0200 | [diff] [blame] | 108 | assert(type < BLOCK_MAX_IOTYPE); |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 109 | |
| 110 | cookie->bytes = bytes; |
Alberto Garcia | 5519593 | 2015-10-28 17:33:00 +0200 | [diff] [blame] | 111 | cookie->start_time_ns = qemu_clock_get_ns(clock_type); |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 112 | cookie->type = type; |
| 113 | } |
| 114 | |
Vladimir Sementsov-Ogievskiy | b741ae7 | 2018-03-09 19:52:11 +0300 | [diff] [blame] | 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] |
| 120 | */ |
| 121 | static int block_latency_histogram_compare_func(const void *key, const void *it) |
| 122 | { |
| 123 | uint64_t k = *(uint64_t *)key; |
| 124 | uint64_t a = ((uint64_t *)it)[0]; |
| 125 | uint64_t b = ((uint64_t *)it)[1]; |
| 126 | |
| 127 | return k < a ? -1 : (k < b ? 0 : 1); |
| 128 | } |
| 129 | |
| 130 | static void block_latency_histogram_account(BlockLatencyHistogram *hist, |
| 131 | int64_t latency_ns) |
| 132 | { |
| 133 | uint64_t *pos; |
| 134 | |
| 135 | if (hist->bins == NULL) { |
| 136 | /* histogram disabled */ |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | if (latency_ns < hist->boundaries[0]) { |
| 142 | hist->bins[0]++; |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | if (latency_ns >= hist->boundaries[hist->nbins - 2]) { |
| 147 | hist->bins[hist->nbins - 1]++; |
| 148 | return; |
| 149 | } |
| 150 | |
| 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); |
| 155 | |
| 156 | hist->bins[pos - hist->boundaries + 1]++; |
| 157 | } |
| 158 | |
| 159 | int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type, |
| 160 | uint64List *boundaries) |
| 161 | { |
| 162 | BlockLatencyHistogram *hist = &stats->latency_histogram[type]; |
| 163 | uint64List *entry; |
| 164 | uint64_t *ptr; |
| 165 | uint64_t prev = 0; |
| 166 | int new_nbins = 1; |
| 167 | |
| 168 | for (entry = boundaries; entry; entry = entry->next) { |
| 169 | if (entry->value <= prev) { |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | new_nbins++; |
| 173 | prev = entry->value; |
| 174 | } |
| 175 | |
| 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++) |
| 181 | { |
| 182 | *ptr = entry->value; |
| 183 | } |
| 184 | |
| 185 | g_free(hist->bins); |
| 186 | hist->bins = g_new0(uint64_t, hist->nbins); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | void block_latency_histograms_clear(BlockAcctStats *stats) |
| 192 | { |
| 193 | int i; |
| 194 | |
| 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)); |
| 200 | } |
| 201 | } |
| 202 | |
Paolo Bonzini | 39c1b42 | 2017-06-05 14:39:06 +0200 | [diff] [blame] | 203 | static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie, |
| 204 | bool failed) |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 205 | { |
Alberto Garcia | 979e9b0 | 2015-10-28 17:33:05 +0200 | [diff] [blame] | 206 | BlockAcctTimedStats *s; |
Alberto Garcia | cb38fff | 2015-10-28 17:33:02 +0200 | [diff] [blame] | 207 | int64_t time_ns = qemu_clock_get_ns(clock_type); |
| 208 | int64_t latency_ns = time_ns - cookie->start_time_ns; |
| 209 | |
Alberto Garcia | 918a17a | 2015-10-28 17:33:09 +0200 | [diff] [blame] | 210 | if (qtest_enabled()) { |
| 211 | latency_ns = qtest_latency_ns; |
| 212 | } |
| 213 | |
Benoît Canet | 28298fd | 2014-09-05 15:46:17 +0200 | [diff] [blame] | 214 | assert(cookie->type < BLOCK_MAX_IOTYPE); |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 215 | |
Anton Nefedov | f344446 | 2019-09-23 15:17:31 +0300 | [diff] [blame] | 216 | if (cookie->type == BLOCK_ACCT_NONE) { |
| 217 | return; |
| 218 | } |
| 219 | |
Gan Qixin | c37c973 | 2020-12-03 15:50:52 +0800 | [diff] [blame] | 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]++; |
| 226 | } |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 227 | |
Gan Qixin | c37c973 | 2020-12-03 15:50:52 +0800 | [diff] [blame] | 228 | block_latency_histogram_account(&stats->latency_histogram[cookie->type], |
| 229 | latency_ns); |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 230 | |
Gan Qixin | c37c973 | 2020-12-03 15:50:52 +0800 | [diff] [blame] | 231 | if (!failed || stats->account_failed) { |
| 232 | stats->total_time_ns[cookie->type] += latency_ns; |
| 233 | stats->last_access_time_ns = time_ns; |
Vladimir Sementsov-Ogievskiy | b741ae7 | 2018-03-09 19:52:11 +0300 | [diff] [blame] | 234 | |
Gan Qixin | c37c973 | 2020-12-03 15:50:52 +0800 | [diff] [blame] | 235 | QSLIST_FOREACH(s, &stats->intervals, entries) { |
| 236 | timed_average_account(&s->latency[cookie->type], latency_ns); |
| 237 | } |
Alberto Garcia | 979e9b0 | 2015-10-28 17:33:05 +0200 | [diff] [blame] | 238 | } |
Alberto Garcia | 362e929 | 2015-10-28 17:33:04 +0200 | [diff] [blame] | 239 | } |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 240 | |
Anton Nefedov | f344446 | 2019-09-23 15:17:31 +0300 | [diff] [blame] | 241 | cookie->type = BLOCK_ACCT_NONE; |
Alberto Garcia | 7ee12da | 2015-10-28 17:33:03 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Paolo Bonzini | 39c1b42 | 2017-06-05 14:39:06 +0200 | [diff] [blame] | 244 | void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie) |
| 245 | { |
| 246 | block_account_one_io(stats, cookie, false); |
| 247 | } |
| 248 | |
| 249 | void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie) |
| 250 | { |
| 251 | block_account_one_io(stats, cookie, true); |
| 252 | } |
| 253 | |
Alberto Garcia | 7ee12da | 2015-10-28 17:33:03 +0200 | [diff] [blame] | 254 | void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type) |
| 255 | { |
| 256 | assert(type < BLOCK_MAX_IOTYPE); |
| 257 | |
Paolo Bonzini | 39c1b42 | 2017-06-05 14:39:06 +0200 | [diff] [blame] | 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. |
| 261 | */ |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 262 | qemu_mutex_lock(&stats->lock); |
Alberto Garcia | 7ee12da | 2015-10-28 17:33:03 +0200 | [diff] [blame] | 263 | stats->invalid_ops[type]++; |
Alberto Garcia | 362e929 | 2015-10-28 17:33:04 +0200 | [diff] [blame] | 264 | |
| 265 | if (stats->account_invalid) { |
| 266 | stats->last_access_time_ns = qemu_clock_get_ns(clock_type); |
| 267 | } |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 268 | qemu_mutex_unlock(&stats->lock); |
Alberto Garcia | 7ee12da | 2015-10-28 17:33:03 +0200 | [diff] [blame] | 269 | } |
Benoît Canet | 5e5a94b | 2014-09-05 15:46:16 +0200 | [diff] [blame] | 270 | |
Peter Lieven | f4564d5 | 2015-02-02 14:52:18 +0100 | [diff] [blame] | 271 | void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type, |
| 272 | int num_requests) |
| 273 | { |
| 274 | assert(type < BLOCK_MAX_IOTYPE); |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 275 | |
| 276 | qemu_mutex_lock(&stats->lock); |
Peter Lieven | f4564d5 | 2015-02-02 14:52:18 +0100 | [diff] [blame] | 277 | stats->merged[type] += num_requests; |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 278 | qemu_mutex_unlock(&stats->lock); |
Peter Lieven | f4564d5 | 2015-02-02 14:52:18 +0100 | [diff] [blame] | 279 | } |
Alberto Garcia | cb38fff | 2015-10-28 17:33:02 +0200 | [diff] [blame] | 280 | |
| 281 | int64_t block_acct_idle_time_ns(BlockAcctStats *stats) |
| 282 | { |
| 283 | return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns; |
| 284 | } |
Alberto Garcia | 96e4ded | 2015-10-28 17:33:06 +0200 | [diff] [blame] | 285 | |
| 286 | double block_acct_queue_depth(BlockAcctTimedStats *stats, |
| 287 | enum BlockAcctType type) |
| 288 | { |
| 289 | uint64_t sum, elapsed; |
| 290 | |
| 291 | assert(type < BLOCK_MAX_IOTYPE); |
| 292 | |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 293 | qemu_mutex_lock(&stats->stats->lock); |
Alberto Garcia | 96e4ded | 2015-10-28 17:33:06 +0200 | [diff] [blame] | 294 | sum = timed_average_sum(&stats->latency[type], &elapsed); |
Paolo Bonzini | 5b50bf7 | 2017-06-05 14:39:08 +0200 | [diff] [blame] | 295 | qemu_mutex_unlock(&stats->stats->lock); |
Alberto Garcia | 96e4ded | 2015-10-28 17:33:06 +0200 | [diff] [blame] | 296 | |
| 297 | return (double) sum / elapsed; |
| 298 | } |