block: split BlockAcctStats creation and setup
[qemu/ar7.git] / block / accounting.c
blobce6dbf7760f7c067b6f754d01f5e9232e107cd03
1 /*
2 * QEMU System Emulator block accounting
4 * Copyright (c) 2011 Christoph Hellwig
5 * Copyright (c) 2015 Igalia, S.L.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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.
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"
32 static QEMUClockType clock_type = QEMU_CLOCK_REALTIME;
33 static const int qtest_latency_ns = NANOSECONDS_PER_SECOND / 1000;
35 void block_acct_init(BlockAcctStats *stats)
37 if (qtest_enabled()) {
38 clock_type = QEMU_CLOCK_VIRTUAL;
42 void block_acct_setup(BlockAcctStats *stats, bool account_invalid,
43 bool account_failed)
45 stats->account_invalid = account_invalid;
46 stats->account_failed = account_failed;
49 void block_acct_cleanup(BlockAcctStats *stats)
51 BlockAcctTimedStats *s, *next;
52 QSLIST_FOREACH_SAFE(s, &stats->intervals, entries, next) {
53 g_free(s);
57 void block_acct_add_interval(BlockAcctStats *stats, unsigned interval_length)
59 BlockAcctTimedStats *s;
60 unsigned i;
62 s = g_new0(BlockAcctTimedStats, 1);
63 s->interval_length = interval_length;
64 QSLIST_INSERT_HEAD(&stats->intervals, s, entries);
66 for (i = 0; i < BLOCK_MAX_IOTYPE; i++) {
67 timed_average_init(&s->latency[i], clock_type,
68 (uint64_t) interval_length * NANOSECONDS_PER_SECOND);
72 BlockAcctTimedStats *block_acct_interval_next(BlockAcctStats *stats,
73 BlockAcctTimedStats *s)
75 if (s == NULL) {
76 return QSLIST_FIRST(&stats->intervals);
77 } else {
78 return QSLIST_NEXT(s, entries);
82 void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
83 int64_t bytes, enum BlockAcctType type)
85 assert(type < BLOCK_MAX_IOTYPE);
87 cookie->bytes = bytes;
88 cookie->start_time_ns = qemu_clock_get_ns(clock_type);
89 cookie->type = type;
92 static void block_account_one_io(BlockAcctStats *stats, BlockAcctCookie *cookie,
93 bool failed)
95 BlockAcctTimedStats *s;
96 int64_t time_ns = qemu_clock_get_ns(clock_type);
97 int64_t latency_ns = time_ns - cookie->start_time_ns;
99 if (qtest_enabled()) {
100 latency_ns = qtest_latency_ns;
103 assert(cookie->type < BLOCK_MAX_IOTYPE);
105 if (failed) {
106 stats->failed_ops[cookie->type]++;
107 } else {
108 stats->nr_bytes[cookie->type] += cookie->bytes;
109 stats->nr_ops[cookie->type]++;
112 if (!failed || stats->account_failed) {
113 stats->total_time_ns[cookie->type] += latency_ns;
114 stats->last_access_time_ns = time_ns;
116 QSLIST_FOREACH(s, &stats->intervals, entries) {
117 timed_average_account(&s->latency[cookie->type], latency_ns);
122 void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
124 block_account_one_io(stats, cookie, false);
127 void block_acct_failed(BlockAcctStats *stats, BlockAcctCookie *cookie)
129 block_account_one_io(stats, cookie, true);
132 void block_acct_invalid(BlockAcctStats *stats, enum BlockAcctType type)
134 assert(type < BLOCK_MAX_IOTYPE);
136 /* block_account_one_io() updates total_time_ns[], but this one does
137 * not. The reason is that invalid requests are accounted during their
138 * submission, therefore there's no actual I/O involved.
140 stats->invalid_ops[type]++;
142 if (stats->account_invalid) {
143 stats->last_access_time_ns = qemu_clock_get_ns(clock_type);
147 void block_acct_merge_done(BlockAcctStats *stats, enum BlockAcctType type,
148 int num_requests)
150 assert(type < BLOCK_MAX_IOTYPE);
151 stats->merged[type] += num_requests;
154 int64_t block_acct_idle_time_ns(BlockAcctStats *stats)
156 return qemu_clock_get_ns(clock_type) - stats->last_access_time_ns;
159 double block_acct_queue_depth(BlockAcctTimedStats *stats,
160 enum BlockAcctType type)
162 uint64_t sum, elapsed;
164 assert(type < BLOCK_MAX_IOTYPE);
166 sum = timed_average_sum(&stats->latency[type], &elapsed);
168 return (double) sum / elapsed;