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, bool account_invalid, 36 bool account_failed) 37 { 38 stats->account_invalid = account_invalid; 39 stats->account_failed = account_failed; 40 41 if (qtest_enabled()) { 42 clock_type = QEMU_CLOCK_VIRTUAL; 43 } 44 } 45 46 void block_acct_cleanup(BlockAcctStats *stats) 47 { 48 BlockAcctTimedStats *s, *next; 49 QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) { 50 g_free(s); 51 } 52 } 53 54 void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length) 55 { 56 BlockAcctTimedStats *s; 57 unsigned i; 58 59 s = g_new0(BlockAcctTimedStats, 1); 60 s->interval_length = interval_length; 61 QSLIST_INSERT_HEAD(&stats->intervals, s, entries); 62 63 for (i = 0; i < BLOCK_MAX_IOTYPE; i++) { 64 timed_average_init(&s->latency[i], clock_type, 65 (uint64_t) interval_length * NANOSECONDS_PER_SECOND); 66 } 67 } 68 69 BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats, 70 BlockAcctTimedStats *s) 71 { 72 if (s == NULL) { 73 return QSLIST_FIRST(&stats->intervals); 74 } else { 75 return QSLIST_NEXT(s, entries); 76 } 77 } 78 79 void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie, 80 int64_t bytes, enum BlockAcctType type) 81 { 82 assert(type < BLOCK_MAX_IOTYPE); 83 84 cookie->bytes = bytes; 85 cookie->start_time_ns = qemu_clock_get_ns(clock_type); 86 cookie->type = type; 87 } 88 89 static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie, 90 bool failed) 91 { 92 BlockAcctTimedStats *s; 93 int64_t time_ns = qemu_clock_get_ns(clock_type); 94 int64_t latency_ns = time_ns - cookie->start_time_ns; 95 96 if (qtest_enabled()) { 97 latency_ns = qtest_latency_ns; 98 } 99 100 assert(cookie->type < BLOCK_MAX_IOTYPE); 101 102 if (failed) { 103 stats->failed_ops[cookie->type]++; 104 } else { 105 stats->nr_bytes[cookie->type] += cookie->bytes; 106 stats->nr_ops[cookie->type]++; 107 } 108 109 if (!failed || stats->account_failed) { 110 stats->total_time_ns[cookie->type] += latency_ns; 111 stats->last_access_time_ns = time_ns; 112 113 QSLIST_FOREACH(s, &stats->intervals, entries) { 114 timed_average_account(&s->latency[cookie->type], latency_ns); 115 } 116 } 117 } 118 119 void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie) 120 { 121 block_account_one_io(stats, cookie, false); 122 } 123 124 void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie) 125 { 126 block_account_one_io(stats, cookie, true); 127 } 128 129 void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type) 130 { 131 assert(type < BLOCK_MAX_IOTYPE); 132 133 /* block_account_one_io() updates total_time_ns[], but this one does 134 * not. The reason is that invalid requests are accounted during their 135 * submission, therefore there's no actual I/O involved. 136 */ 137 stats->invalid_ops[type]++; 138 139 if (stats->account_invalid) { 140 stats->last_access_time_ns = qemu_clock_get_ns(clock_type); 141 } 142 } 143 144 void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type, 145 int num_requests) 146 { 147 assert(type < BLOCK_MAX_IOTYPE); 148 stats->merged[type] += num_requests; 149 } 150 151 int64_t block_acct_idle_time_ns(BlockAcctStats *stats) 152 { 153 return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns; 154 } 155 156 double block_acct_queue_depth(BlockAcctTimedStats *stats, 157 enum BlockAcctType type) 158 { 159 uint64_t sum, elapsed; 160 161 assert(type < BLOCK_MAX_IOTYPE); 162 163 sum = timed_average_sum(&stats->latency[type], &elapsed); 164 165 return (double) sum / elapsed; 166 } 167