| 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 | { |
| 38 | qemu_mutex_init(&stats->lock); |
| 39 | if (qtest_enabled()) { |
| 40 | clock_type = QEMU_CLOCK_VIRTUAL; |
| 41 | } |
| 42 | stats->account_invalid = true; |
| 43 | stats->account_failed = true; |
| 44 | } |
| 45 | |
| 46 | static bool bool_from_onoffauto(OnOffAuto val, bool def) |
| 47 | { |
| 48 | switch (val) { |
| 49 | case ON_OFF_AUTO_AUTO: |
| 50 | return def; |
| 51 | case ON_OFF_AUTO_ON: |
| 52 | return true; |
| 53 | case ON_OFF_AUTO_OFF: |
| 54 | return false; |
| 55 | default: |
| 56 | abort(); |
| 57 | } |
| 58 | } |
| 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 | { |
| 64 | stats->account_invalid = bool_from_onoffauto(account_invalid, |
| 65 | stats->account_invalid); |
| 66 | stats->account_failed = bool_from_onoffauto(account_failed, |
| 67 | stats->account_failed); |
| 68 | if (stats_intervals) { |
| 69 | for (int i = 0; i < num_stats_intervals; i++) { |
| 70 | if (stats_intervals[i] <= 0) { |
| 71 | error_setg(errp, "Invalid interval length: %u", stats_intervals[i]); |
| 72 | return false; |
| 73 | } |
| 74 | block_acct_add_interval(stats, stats_intervals[i]); |
| 75 | } |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | void block_acct_cleanup(BlockAcctStats *stats) |
| 81 | { |
| 82 | BlockAcctTimedStats *s, *next; |
| 83 | QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) { |
| 84 | g_free(s); |
| 85 | } |
| 86 | qemu_mutex_destroy(&stats->lock); |
| 87 | } |
| 88 | |
| 89 | void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length) |
| 90 | { |
| 91 | BlockAcctTimedStats *s; |
| 92 | unsigned i; |
| 93 | |
| 94 | s = g_new0(BlockAcctTimedStats, 1); |
| 95 | s->interval_length = interval_length; |
| 96 | s->stats = stats; |
| 97 | qemu_mutex_lock(&stats->lock); |
| 98 | QSLIST_INSERT_HEAD(&stats->intervals, s, entries); |
| 99 | |
| 100 | for (i = 0; i < BLOCK_MAX_IOTYPE; i++) { |
| 101 | timed_average_init(&s->latency[i], clock_type, |
| 102 | (uint64_t) interval_length * NANOSECONDS_PER_SECOND); |
| 103 | } |
| 104 | qemu_mutex_unlock(&stats->lock); |
| 105 | } |
| 106 | |
| 107 | BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats, |
| 108 | BlockAcctTimedStats *s) |
| 109 | { |
| 110 | if (s == NULL) { |
| 111 | return QSLIST_FIRST(&stats->intervals); |
| 112 | } else { |
| 113 | return QSLIST_NEXT(s, entries); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie, |
| 118 | int64_t bytes, enum BlockAcctType type) |
| 119 | { |
| 120 | assert(type < BLOCK_MAX_IOTYPE); |
| 121 | |
| 122 | cookie->bytes = bytes; |
| 123 | cookie->start_time_ns = qemu_clock_get_ns(clock_type); |
| 124 | cookie->type = type; |
| 125 | } |
| 126 | |
| 127 | /* block_latency_histogram_compare_func: |
| 128 | * Compare @key with interval [@it[0], @it[1]). |
| 129 | * Return: -1 if @key < @it[0] |
| 130 | * 0 if @key in [@it[0], @it[1]) |
| 131 | * +1 if @key >= @it[1] |
| 132 | */ |
| 133 | static int block_latency_histogram_compare_func(const void *key, const void *it) |
| 134 | { |
| 135 | uint64_t k = *(uint64_t *)key; |
| 136 | uint64_t a = ((uint64_t *)it)[0]; |
| 137 | uint64_t b = ((uint64_t *)it)[1]; |
| 138 | |
| 139 | return k < a ? -1 : (k < b ? 0 : 1); |
| 140 | } |
| 141 | |
| 142 | static void block_latency_histogram_account(BlockLatencyHistogram *hist, |
| 143 | int64_t latency_ns) |
| 144 | { |
| 145 | uint64_t *pos; |
| 146 | |
| 147 | if (hist->bins == NULL) { |
| 148 | /* histogram disabled */ |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | if (latency_ns < hist->boundaries[0]) { |
| 154 | hist->bins[0]++; |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | if (latency_ns >= hist->boundaries[hist->nbins - 2]) { |
| 159 | hist->bins[hist->nbins - 1]++; |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | pos = bsearch(&latency_ns, hist->boundaries, hist->nbins - 2, |
| 164 | sizeof(hist->boundaries[0]), |
| 165 | block_latency_histogram_compare_func); |
| 166 | assert(pos != NULL); |
| 167 | |
| 168 | hist->bins[pos - hist->boundaries + 1]++; |
| 169 | } |
| 170 | |
| 171 | int block_latency_histogram_set(BlockAcctStats *stats, enum BlockAcctType type, |
| 172 | uint64List *boundaries) |
| 173 | { |
| 174 | BlockLatencyHistogram *hist = &stats->latency_histogram[type]; |
| 175 | uint64List *entry; |
| 176 | uint64_t *ptr; |
| 177 | uint64_t prev = 0; |
| 178 | int new_nbins = 1; |
| 179 | |
| 180 | for (entry = boundaries; entry; entry = entry->next) { |
| 181 | if (entry->value <= prev) { |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | new_nbins++; |
| 185 | prev = entry->value; |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * block_latency_histogram_account() assumes that it can always access |
| 190 | * hist->boundaries[0], so require at least one boundary. A histogram with |
| 191 | * a single bin is useless anyway. |
| 192 | */ |
| 193 | if (new_nbins <= 1) { |
| 194 | return -EINVAL; |
| 195 | } |
| 196 | |
| 197 | qemu_mutex_lock(&stats->lock); |
| 198 | |
| 199 | hist->nbins = new_nbins; |
| 200 | g_free(hist->boundaries); |
| 201 | hist->boundaries = g_new(uint64_t, hist->nbins - 1); |
| 202 | for (entry = boundaries, ptr = hist->boundaries; entry; |
| 203 | entry = entry->next, ptr++) |
| 204 | { |
| 205 | *ptr = entry->value; |
| 206 | } |
| 207 | |
| 208 | g_free(hist->bins); |
| 209 | hist->bins = g_new0(uint64_t, hist->nbins); |
| 210 | |
| 211 | qemu_mutex_unlock(&stats->lock); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | void block_latency_histograms_clear(BlockAcctStats *stats) |
| 217 | { |
| 218 | int i; |
| 219 | |
| 220 | qemu_mutex_lock(&stats->lock); |
| 221 | |
| 222 | for (i = 0; i < BLOCK_MAX_IOTYPE; i++) { |
| 223 | BlockLatencyHistogram *hist = &stats->latency_histogram[i]; |
| 224 | g_free(hist->bins); |
| 225 | g_free(hist->boundaries); |
| 226 | memset(hist, 0, sizeof(*hist)); |
| 227 | } |
| 228 | |
| 229 | qemu_mutex_unlock(&stats->lock); |
| 230 | } |
| 231 | |
| 232 | static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie, |
| 233 | bool failed) |
| 234 | { |
| 235 | BlockAcctTimedStats *s; |
| 236 | int64_t time_ns = qemu_clock_get_ns(clock_type); |
| 237 | int64_t latency_ns = time_ns - cookie->start_time_ns; |
| 238 | |
| 239 | if (qtest_enabled()) { |
| 240 | latency_ns = qtest_latency_ns; |
| 241 | } |
| 242 | |
| 243 | assert(cookie->type < BLOCK_MAX_IOTYPE); |
| 244 | |
| 245 | if (cookie->type == BLOCK_ACCT_NONE) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | WITH_QEMU_LOCK_GUARD(&stats->lock) { |
| 250 | if (failed) { |
| 251 | stats->failed_ops[cookie->type]++; |
| 252 | } else { |
| 253 | stats->nr_bytes[cookie->type] += cookie->bytes; |
| 254 | stats->nr_ops[cookie->type]++; |
| 255 | } |
| 256 | |
| 257 | block_latency_histogram_account(&stats->latency_histogram[cookie->type], |
| 258 | latency_ns); |
| 259 | |
| 260 | if (!failed || stats->account_failed) { |
| 261 | stats->total_time_ns[cookie->type] += latency_ns; |
| 262 | stats->last_access_time_ns = time_ns; |
| 263 | |
| 264 | QSLIST_FOREACH(s, &stats->intervals, entries) { |
| 265 | timed_average_account(&s->latency[cookie->type], latency_ns); |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | cookie->type = BLOCK_ACCT_NONE; |
| 271 | } |
| 272 | |
| 273 | void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie) |
| 274 | { |
| 275 | block_account_one_io(stats, cookie, false); |
| 276 | } |
| 277 | |
| 278 | void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie) |
| 279 | { |
| 280 | block_account_one_io(stats, cookie, true); |
| 281 | } |
| 282 | |
| 283 | void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type) |
| 284 | { |
| 285 | assert(type < BLOCK_MAX_IOTYPE); |
| 286 | |
| 287 | /* block_account_one_io() updates total_time_ns[], but this one does |
| 288 | * not. The reason is that invalid requests are accounted during their |
| 289 | * submission, therefore there's no actual I/O involved. |
| 290 | */ |
| 291 | qemu_mutex_lock(&stats->lock); |
| 292 | stats->invalid_ops[type]++; |
| 293 | |
| 294 | if (stats->account_invalid) { |
| 295 | stats->last_access_time_ns = qemu_clock_get_ns(clock_type); |
| 296 | } |
| 297 | qemu_mutex_unlock(&stats->lock); |
| 298 | } |
| 299 | |
| 300 | void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type, |
| 301 | int num_requests) |
| 302 | { |
| 303 | assert(type < BLOCK_MAX_IOTYPE); |
| 304 | |
| 305 | qemu_mutex_lock(&stats->lock); |
| 306 | stats->merged[type] += num_requests; |
| 307 | qemu_mutex_unlock(&stats->lock); |
| 308 | } |
| 309 | |
| 310 | int64_t block_acct_idle_time_ns(BlockAcctStats *stats) |
| 311 | { |
| 312 | return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns; |
| 313 | } |
| 314 | |
| 315 | double block_acct_queue_depth(BlockAcctTimedStats *stats, |
| 316 | enum BlockAcctType type) |
| 317 | { |
| 318 | uint64_t sum, elapsed; |
| 319 | |
| 320 | assert(type < BLOCK_MAX_IOTYPE); |
| 321 | assert(qemu_mutex_trylock(&stats->stats->lock) == -EBUSY); |
| 322 | |
| 323 | sum = timed_average_sum(&stats->latency[type], &elapsed); |
| 324 | |
| 325 | return (double) sum / elapsed; |
| 326 | } |