2 * QEMU System Emulator block accounting
4 * Copyright (c) 2011 Christoph Hellwig
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "block/accounting.h"
26 #include "block/block_int.h"
27 #include "qemu/timer.h"
29 static QEMUClockType clock_type
= QEMU_CLOCK_REALTIME
;
31 void block_acct_init(BlockAcctStats
*stats
, bool account_invalid
,
34 stats
->account_invalid
= account_invalid
;
35 stats
->account_failed
= account_failed
;
38 void block_acct_cleanup(BlockAcctStats
*stats
)
40 BlockAcctTimedStats
*s
, *next
;
41 QSLIST_FOREACH_SAFE(s
, &stats
->intervals
, entries
, next
) {
46 void block_acct_add_interval(BlockAcctStats
*stats
, unsigned interval_length
)
48 BlockAcctTimedStats
*s
;
51 s
= g_new0(BlockAcctTimedStats
, 1);
52 s
->interval_length
= interval_length
;
53 QSLIST_INSERT_HEAD(&stats
->intervals
, s
, entries
);
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
);
61 BlockAcctTimedStats
*block_acct_interval_next(BlockAcctStats
*stats
,
62 BlockAcctTimedStats
*s
)
65 return QSLIST_FIRST(&stats
->intervals
);
67 return QSLIST_NEXT(s
, entries
);
71 void block_acct_start(BlockAcctStats
*stats
, BlockAcctCookie
*cookie
,
72 int64_t bytes
, enum BlockAcctType type
)
74 assert(type
< BLOCK_MAX_IOTYPE
);
76 cookie
->bytes
= bytes
;
77 cookie
->start_time_ns
= qemu_clock_get_ns(clock_type
);
81 void block_acct_done(BlockAcctStats
*stats
, BlockAcctCookie
*cookie
)
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
;
87 assert(cookie
->type
< BLOCK_MAX_IOTYPE
);
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
;
94 QSLIST_FOREACH(s
, &stats
->intervals
, entries
) {
95 timed_average_account(&s
->latency
[cookie
->type
], latency_ns
);
99 void block_acct_failed(BlockAcctStats
*stats
, BlockAcctCookie
*cookie
)
101 assert(cookie
->type
< BLOCK_MAX_IOTYPE
);
103 stats
->failed_ops
[cookie
->type
]++;
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
;
110 stats
->total_time_ns
[cookie
->type
] += latency_ns
;
111 stats
->last_access_time_ns
= time_ns
;
113 QSLIST_FOREACH(s
, &stats
->intervals
, entries
) {
114 timed_average_account(&s
->latency
[cookie
->type
], latency_ns
);
119 void block_acct_invalid(BlockAcctStats
*stats
, enum BlockAcctType type
)
121 assert(type
< BLOCK_MAX_IOTYPE
);
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. */
128 stats
->invalid_ops
[type
]++;
130 if (stats
->account_invalid
) {
131 stats
->last_access_time_ns
= qemu_clock_get_ns(clock_type
);
135 void block_acct_merge_done(BlockAcctStats
*stats
, enum BlockAcctType type
,
138 assert(type
< BLOCK_MAX_IOTYPE
);
139 stats
->merged
[type
] += num_requests
;
142 int64_t block_acct_idle_time_ns(BlockAcctStats
*stats
)
144 return qemu_clock_get_ns(clock_type
) - stats
->last_access_time_ns
;