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