Commit 5366d0c8 authored by Benoît Canet's avatar Benoît Canet Committed by Kevin Wolf
Browse files

block: Make the block accounting functions operate on BlockAcctStats



This is the next step for decoupling block accounting functions from
BlockDriverState.
In a future commit the BlockAcctStats structure will be moved from
BlockDriverState to the device models structures.

Note that bdrv_get_stats was introduced so device models can retrieve the
BlockAcctStats structure of a BlockDriverState without being aware of it's
layout.
This function should go away when BlockAcctStats will be embedded in the device
models structures.

CC: Kevin Wolf <kwolf@redhat.com>
CC: Stefan Hajnoczi <stefanha@redhat.com>
CC: Keith Busch <keith.busch@intel.com>
CC: Anthony Liguori <aliguori@amazon.com>
CC: "Michael S. Tsirkin" <mst@redhat.com>
CC: Paolo Bonzini <pbonzini@redhat.com>
CC: Eric Blake <eblake@redhat.com>
CC: Peter Maydell <peter.maydell@linaro.org>
CC: Michael Tokarev <mjt@tls.msk.ru>
CC: John Snow <jsnow@redhat.com>
CC: Markus Armbruster <armbru@redhat.com>
CC: Alexander Graf <agraf@suse.de>
CC: Max Reitz <mreitz@redhat.com>

Signed-off-by: default avatarBenoît Canet <benoit.canet@nodalink.com>
Signed-off-by: default avatarKevin Wolf <kwolf@redhat.com>
parent 28298fd3
Loading
Loading
Loading
Loading
+12 −1
Original line number Diff line number Diff line
@@ -3363,7 +3363,7 @@ static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs,

    bdrv_set_dirty(bs, sector_num, nb_sectors);

    bdrv_acct_highest_sector(bs, sector_num, nb_sectors);
    block_acct_highest_sector(&bs->stats, sector_num, nb_sectors);

    if (bs->growable && ret >= 0) {
        bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors);
@@ -6087,3 +6087,14 @@ void bdrv_refresh_filename(BlockDriverState *bs)
        QDECREF(json);
    }
}

/* This accessor function purpose is to allow the device models to access the
 * BlockAcctStats structure embedded inside a BlockDriverState without being
 * aware of the BlockDriverState structure layout.
 * It will go away when the BlockAcctStats structure will be moved inside
 * the device models.
 */
BlockAcctStats *bdrv_get_stats(BlockDriverState *bs)
{
    return &bs->stats;
}
+10 −13
Original line number Diff line number Diff line
@@ -25,9 +25,8 @@
#include "block/accounting.h"
#include "block/block_int.h"

void
bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
        enum BlockAcctType type)
void block_acct_start(BlockAcctStats *stats, BlockAcctCookie *cookie,
                      int64_t bytes, enum BlockAcctType type)
{
    assert(type < BLOCK_MAX_IOTYPE);

@@ -36,22 +35,20 @@ bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
    cookie->type = type;
}

void
bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
void block_acct_done(BlockAcctStats *stats, BlockAcctCookie *cookie)
{
    assert(cookie->type < BLOCK_MAX_IOTYPE);

    bs->stats.nr_bytes[cookie->type] += cookie->bytes;
    bs->stats.nr_ops[cookie->type]++;
    bs->stats.total_time_ns[cookie->type] += get_clock() -
                                             cookie->start_time_ns;
    stats->nr_bytes[cookie->type] += cookie->bytes;
    stats->nr_ops[cookie->type]++;
    stats->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
}


void bdrv_acct_highest_sector(BlockDriverState *bs, int64_t sector_num,
void block_acct_highest_sector(BlockAcctStats *stats, int64_t sector_num,
                               unsigned int nb_sectors)
{
    if (bs->stats.wr_highest_sector < sector_num + nb_sectors - 1) {
        bs->stats.wr_highest_sector = sector_num + nb_sectors - 1;
    if (stats->wr_highest_sector < sector_num + nb_sectors - 1) {
        stats->wr_highest_sector = sector_num + nb_sectors - 1;
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -277,5 +277,5 @@ uint64_t dma_buf_write(uint8_t *ptr, int32_t len, QEMUSGList *sg)
void dma_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie,
                    QEMUSGList *sg, enum BlockAcctType type)
{
    bdrv_acct_start(bs, cookie, sg->size, type);
    block_acct_start(bdrv_get_stats(bs), cookie, sg->size, type);
}
+1 −1
Original line number Diff line number Diff line
@@ -197,7 +197,7 @@ static void nvme_rw_cb(void *opaque, int ret)
    NvmeCtrl *n = sq->ctrl;
    NvmeCQueue *cq = n->cq[sq->cqid];

    bdrv_acct_done(n->conf.bs, &req->acct);
    block_acct_done(bdrv_get_stats(n->conf.bs), &req->acct);
    if (!ret) {
        req->status = NVME_SUCCESS;
    } else {
+9 −6
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ static int virtio_blk_handle_rw_error(VirtIOBlockReq *req, int error,
        s->rq = req;
    } else if (action == BLOCK_ERROR_ACTION_REPORT) {
        virtio_blk_req_complete(req, VIRTIO_BLK_S_IOERR);
        bdrv_acct_done(s->bs, &req->acct);
        block_acct_done(bdrv_get_stats(s->bs), &req->acct);
        virtio_blk_free_request(req);
    }

@@ -96,7 +96,7 @@ static void virtio_blk_rw_complete(void *opaque, int ret)
    }

    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
    bdrv_acct_done(req->dev->bs, &req->acct);
    block_acct_done(bdrv_get_stats(req->dev->bs), &req->acct);
    virtio_blk_free_request(req);
}

@@ -111,7 +111,7 @@ static void virtio_blk_flush_complete(void *opaque, int ret)
    }

    virtio_blk_req_complete(req, VIRTIO_BLK_S_OK);
    bdrv_acct_done(req->dev->bs, &req->acct);
    block_acct_done(bdrv_get_stats(req->dev->bs), &req->acct);
    virtio_blk_free_request(req);
}

@@ -279,7 +279,8 @@ void virtio_submit_multiwrite(BlockDriverState *bs, MultiReqBuffer *mrb)

static void virtio_blk_handle_flush(VirtIOBlockReq *req, MultiReqBuffer *mrb)
{
    bdrv_acct_start(req->dev->bs, &req->acct, 0, BLOCK_ACCT_FLUSH);
    block_acct_start(bdrv_get_stats(req->dev->bs), &req->acct, 0,
                     BLOCK_ACCT_FLUSH);

    /*
     * Make sure all outstanding writes are posted to the backing device.
@@ -322,7 +323,8 @@ static void virtio_blk_handle_write(VirtIOBlockReq *req, MultiReqBuffer *mrb)
        return;
    }

    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BLOCK_ACCT_WRITE);
    block_acct_start(bdrv_get_stats(req->dev->bs), &req->acct, req->qiov.size,
                     BLOCK_ACCT_WRITE);

    if (mrb->num_writes == 32) {
        virtio_submit_multiwrite(req->dev->bs, mrb);
@@ -353,7 +355,8 @@ static void virtio_blk_handle_read(VirtIOBlockReq *req)
        return;
    }

    bdrv_acct_start(req->dev->bs, &req->acct, req->qiov.size, BLOCK_ACCT_READ);
    block_acct_start(bdrv_get_stats(req->dev->bs), &req->acct, req->qiov.size,
                     BLOCK_ACCT_READ);
    bdrv_aio_readv(req->dev->bs, sector, &req->qiov,
                   req->qiov.size / BDRV_SECTOR_SIZE,
                   virtio_blk_rw_complete, req);
Loading