]> git.proxmox.com Git - mirror_qemu.git/blob - block/accounting.c
a9419315a3beefcbe8381a1bcb71313fb08d005f
[mirror_qemu.git] / block / accounting.c
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"
27 #include "qemu/timer.h"
28
29 static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
30
31 void block_acct_init(BlockAcctStats *stats, bool account_invalid,
32 bool account_failed)
33 {
34 stats->account_invalid = account_invalid;
35 stats->account_failed = account_failed;
36 }
37
38 void block_acct_cleanup(BlockAcctStats *stats)
39 {
40 BlockAcctTimedStats *s, *next;
41 QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) {
42 g_free(s);
43 }
44 }
45
46 void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length)
47 {
48 BlockAcctTimedStats *s;
49 unsigned i;
50
51 s = g_new0(BlockAcctTimedStats, 1);
52 s->interval_length = interval_length;
53 QSLIST_INSERT_HEAD(&stats->intervals, s, entries);
54
55 for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
56 timed_average_init(&s->latency[i], clock_type,
57 (uint64_t) interval_length * NANOSECONDS_PER_SECOND);
58 }
59 }
60
61 BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
62 BlockAcctTimedStats *s)
63 {
64 if (s == NULL) {
65 return QSLIST_FIRST(&stats->intervals);
66 } else {
67 return QSLIST_NEXT(s, entries);
68 }
69 }
70
71 void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
72 int64_t bytes, enum BlockAcctType type)
73 {
74 assert(type < BLOCK_MAX_IOTYPE);
75
76 cookie->bytes = bytes;
77 cookie->start_time_ns = qemu_clock_get_ns(clock_type);
78 cookie->type = type;
79 }
80
81 void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
82 {
83 BlockAcctTimedStats *s;
84 int64_t time_ns = qemu_clock_get_ns(clock_type);
85 int64_t latency_ns = time_ns - cookie->start_time_ns;
86
87 assert(cookie->type < BLOCK_MAX_IOTYPE);
88
89 stats->nr_bytes[cookie->type] += cookie->bytes;
90 stats->nr_ops[cookie->type]++;
91 stats->total_time_ns[cookie->type] += latency_ns;
92 stats->last_access_time_ns = time_ns;
93
94 QSLIST_FOREACH(s, &stats->intervals, entries) {
95 timed_average_account(&s->latency[cookie->type], latency_ns);
96 }
97 }
98
99 void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie)
100 {
101 assert(cookie->type < BLOCK_MAX_IOTYPE);
102
103 stats->failed_ops[cookie->type]++;
104
105 if (stats->account_failed) {
106 BlockAcctTimedStats *s;
107 int64_t time_ns = qemu_clock_get_ns(clock_type);
108 int64_t latency_ns = time_ns - cookie->start_time_ns;
109
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_invalid(BlockAcctStats *stats, enum BlockAcctType type)
120 {
121 assert(type < BLOCK_MAX_IOTYPE);
122
123 /* block_acct_done() and block_acct_failed() update
124 * total_time_ns[], but this one does not. The reason is that
125 * invalid requests are accounted during their submission,
126 * therefore there's no actual I/O involved. */
127
128 stats->invalid_ops[type]++;
129
130 if (stats->account_invalid) {
131 stats->last_access_time_ns = qemu_clock_get_ns(clock_type);
132 }
133 }
134
135 void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
136 int num_requests)
137 {
138 assert(type < BLOCK_MAX_IOTYPE);
139 stats->merged[type] += num_requests;
140 }
141
142 int64_t block_acct_idle_time_ns(BlockAcctStats *stats)
143 {
144 return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns;
145 }
146
147 double block_acct_queue_depth(BlockAcctTimedStats *stats,
148 enum BlockAcctType type)
149 {
150 uint64_t sum, elapsed;
151
152 assert(type < BLOCK_MAX_IOTYPE);
153
154 sum = timed_average_sum(&stats->latency[type], &elapsed);
155
156 return (double) sum / elapsed;
157 }