]>
Commit | Line | Data |
---|---|---|
5e5a94b6 BC |
1 | /* |
2 | * QEMU System Emulator block accounting | |
3 | * | |
4 | * Copyright (c) 2011 Christoph Hellwig | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "block/accounting.h" | |
26 | #include "block/block_int.h" | |
a56ebc6b | 27 | #include "qemu/timer.h" |
918a17a4 | 28 | #include "sysemu/qtest.h" |
5e5a94b6 | 29 | |
5519593c | 30 | static QEMUClockType clock_type = QEMU_CLOCK_REALTIME; |
918a17a4 | 31 | static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000; |
5519593c | 32 | |
362e9299 AG |
33 | void block_acct_init(BlockAcctStats *stats, bool account_invalid, |
34 | bool account_failed) | |
35 | { | |
36 | stats->account_invalid = account_invalid; | |
37 | stats->account_failed = account_failed; | |
918a17a4 AG |
38 | |
39 | if (qtest_enabled()) { | |
40 | clock_type = QEMU_CLOCK_VIRTUAL; | |
41 | } | |
362e9299 AG |
42 | } |
43 | ||
979e9b03 AG |
44 | void block_acct_cleanup(BlockAcctStats *stats) |
45 | { | |
46 | BlockAcctTimedStats *s, *next; | |
47 | QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) { | |
48 | g_free(s); | |
49 | } | |
50 | } | |
51 | ||
52 | void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length) | |
53 | { | |
54 | BlockAcctTimedStats *s; | |
55 | unsigned i; | |
56 | ||
57 | s = g_new0(BlockAcctTimedStats, 1); | |
58 | s->interval_length = interval_length; | |
59 | QSLIST_INSERT_HEAD(&stats->intervals, s, entries); | |
60 | ||
61 | for (i = 0; i < BLOCK_MAX_IOTYPE; i++) { | |
62 | timed_average_init(&s->latency[i], clock_type, | |
63 | (uint64_t) interval_length * NANOSECONDS_PER_SECOND); | |
64 | } | |
65 | } | |
66 | ||
67 | BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats, | |
68 | BlockAcctTimedStats *s) | |
69 | { | |
70 | if (s == NULL) { | |
71 | return QSLIST_FIRST(&stats->intervals); | |
72 | } else { | |
73 | return QSLIST_NEXT(s, entries); | |
74 | } | |
75 | } | |
76 | ||
5366d0c8 BC |
77 | void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie, |
78 | int64_t bytes, enum BlockAcctType type) | |
5e5a94b6 | 79 | { |
28298fd3 | 80 | assert(type < BLOCK_MAX_IOTYPE); |
5e5a94b6 BC |
81 | |
82 | cookie->bytes = bytes; | |
5519593c | 83 | cookie->start_time_ns = qemu_clock_get_ns(clock_type); |
5e5a94b6 BC |
84 | cookie->type = type; |
85 | } | |
86 | ||
5366d0c8 | 87 | void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie) |
5e5a94b6 | 88 | { |
979e9b03 | 89 | BlockAcctTimedStats *s; |
cb38fffb AG |
90 | int64_t time_ns = qemu_clock_get_ns(clock_type); |
91 | int64_t latency_ns = time_ns - cookie->start_time_ns; | |
92 | ||
918a17a4 AG |
93 | if (qtest_enabled()) { |
94 | latency_ns = qtest_latency_ns; | |
95 | } | |
96 | ||
28298fd3 | 97 | assert(cookie->type < BLOCK_MAX_IOTYPE); |
5e5a94b6 | 98 | |
5366d0c8 BC |
99 | stats->nr_bytes[cookie->type] += cookie->bytes; |
100 | stats->nr_ops[cookie->type]++; | |
cb38fffb AG |
101 | stats->total_time_ns[cookie->type] += latency_ns; |
102 | stats->last_access_time_ns = time_ns; | |
979e9b03 AG |
103 | |
104 | QSLIST_FOREACH(s, &stats->intervals, entries) { | |
105 | timed_average_account(&s->latency[cookie->type], latency_ns); | |
106 | } | |
5e5a94b6 BC |
107 | } |
108 | ||
7ee12daf AG |
109 | void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie) |
110 | { | |
7ee12daf AG |
111 | assert(cookie->type < BLOCK_MAX_IOTYPE); |
112 | ||
113 | stats->failed_ops[cookie->type]++; | |
362e9299 AG |
114 | |
115 | if (stats->account_failed) { | |
979e9b03 | 116 | BlockAcctTimedStats *s; |
362e9299 AG |
117 | int64_t time_ns = qemu_clock_get_ns(clock_type); |
118 | int64_t latency_ns = time_ns - cookie->start_time_ns; | |
119 | ||
918a17a4 AG |
120 | if (qtest_enabled()) { |
121 | latency_ns = qtest_latency_ns; | |
122 | } | |
123 | ||
362e9299 AG |
124 | stats->total_time_ns[cookie->type] += latency_ns; |
125 | stats->last_access_time_ns = time_ns; | |
979e9b03 AG |
126 | |
127 | QSLIST_FOREACH(s, &stats->intervals, entries) { | |
128 | timed_average_account(&s->latency[cookie->type], latency_ns); | |
129 | } | |
362e9299 | 130 | } |
7ee12daf AG |
131 | } |
132 | ||
133 | void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type) | |
134 | { | |
135 | assert(type < BLOCK_MAX_IOTYPE); | |
136 | ||
137 | /* block_acct_done() and block_acct_failed() update | |
138 | * total_time_ns[], but this one does not. The reason is that | |
139 | * invalid requests are accounted during their submission, | |
140 | * therefore there's no actual I/O involved. */ | |
141 | ||
142 | stats->invalid_ops[type]++; | |
362e9299 AG |
143 | |
144 | if (stats->account_invalid) { | |
145 | stats->last_access_time_ns = qemu_clock_get_ns(clock_type); | |
146 | } | |
7ee12daf | 147 | } |
5e5a94b6 | 148 | |
f4564d53 PL |
149 | void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type, |
150 | int num_requests) | |
151 | { | |
152 | assert(type < BLOCK_MAX_IOTYPE); | |
153 | stats->merged[type] += num_requests; | |
154 | } | |
cb38fffb AG |
155 | |
156 | int64_t block_acct_idle_time_ns(BlockAcctStats *stats) | |
157 | { | |
158 | return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns; | |
159 | } | |
96e4deda AG |
160 | |
161 | double block_acct_queue_depth(BlockAcctTimedStats *stats, | |
162 | enum BlockAcctType type) | |
163 | { | |
164 | uint64_t sum, elapsed; | |
165 | ||
166 | assert(type < BLOCK_MAX_IOTYPE); | |
167 | ||
168 | sum = timed_average_sum(&stats->latency[type], &elapsed); | |
169 | ||
170 | return (double) sum / elapsed; | |
171 | } |