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