static void test_average(void) { TimedAverage ta; uint64_t result; int i; /* we will compute some average on a period of 1 second */ timed_average_init(&ta, QEMU_CLOCK_VIRTUAL, NANOSECONDS_PER_SECOND); result = timed_average_min(&ta); g_assert(result == 0); result = timed_average_avg(&ta); g_assert(result == 0); result = timed_average_max(&ta); g_assert(result == 0); for (i = 0; i < 100; i++) { account(&ta); result = timed_average_min(&ta); g_assert(result == 1); result = timed_average_avg(&ta); g_assert(result == 3); result = timed_average_max(&ta); g_assert(result == 5); my_clock_value += NANOSECONDS_PER_SECOND / 10; } my_clock_value += NANOSECONDS_PER_SECOND * 100; result = timed_average_min(&ta); g_assert(result == 0); result = timed_average_avg(&ta); g_assert(result == 0); result = timed_average_max(&ta); g_assert(result == 0); for (i = 0; i < 100; i++) { account(&ta); result = timed_average_min(&ta); g_assert(result == 1); result = timed_average_avg(&ta); g_assert(result == 3); result = timed_average_max(&ta); g_assert(result == 5); my_clock_value += NANOSECONDS_PER_SECOND / 10; } }
static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk) { BlockAcctStats *stats = blk_get_stats(blk); BlockAcctTimedStats *ts = NULL; ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ]; ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE]; ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ]; ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE]; ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ]; ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE]; ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH]; ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ]; ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE]; ds->invalid_flush_operations = stats->invalid_ops[BLOCK_ACCT_FLUSH]; ds->rd_merged = stats->merged[BLOCK_ACCT_READ]; ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE]; ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH]; ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE]; ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ]; ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH]; ds->has_idle_time_ns = stats->last_access_time_ns > 0; if (ds->has_idle_time_ns) { ds->idle_time_ns = block_acct_idle_time_ns(stats); } ds->account_invalid = stats->account_invalid; ds->account_failed = stats->account_failed; while ((ts = block_acct_interval_next(stats, ts))) { BlockDeviceTimedStatsList *timed_stats = g_malloc0(sizeof(*timed_stats)); BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats)); timed_stats->next = ds->timed_stats; timed_stats->value = dev_stats; ds->timed_stats = timed_stats; TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ]; TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE]; TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH]; dev_stats->interval_length = ts->interval_length; dev_stats->min_rd_latency_ns = timed_average_min(rd); dev_stats->max_rd_latency_ns = timed_average_max(rd); dev_stats->avg_rd_latency_ns = timed_average_avg(rd); dev_stats->min_wr_latency_ns = timed_average_min(wr); dev_stats->max_wr_latency_ns = timed_average_max(wr); dev_stats->avg_wr_latency_ns = timed_average_avg(wr); dev_stats->min_flush_latency_ns = timed_average_min(fl); dev_stats->max_flush_latency_ns = timed_average_max(fl); dev_stats->avg_flush_latency_ns = timed_average_avg(fl); dev_stats->avg_rd_queue_depth = block_acct_queue_depth(ts, BLOCK_ACCT_READ); dev_stats->avg_wr_queue_depth = block_acct_queue_depth(ts, BLOCK_ACCT_WRITE); } }
static BlockStats *bdrv_query_stats(const BlockDriverState *bs, bool query_backing) { BlockStats *s; s = g_malloc0(sizeof(*s)); if (bdrv_get_device_name(bs)[0]) { s->has_device = true; s->device = g_strdup(bdrv_get_device_name(bs)); } if (bdrv_get_node_name(bs)[0]) { s->has_node_name = true; s->node_name = g_strdup(bdrv_get_node_name(bs)); } s->stats = g_malloc0(sizeof(*s->stats)); if (bs->blk) { BlockAcctStats *stats = blk_get_stats(bs->blk); BlockAcctTimedStats *ts = NULL; s->stats->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ]; s->stats->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE]; s->stats->rd_operations = stats->nr_ops[BLOCK_ACCT_READ]; s->stats->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE]; s->stats->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ]; s->stats->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE]; s->stats->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH]; s->stats->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ]; s->stats->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE]; s->stats->invalid_flush_operations = stats->invalid_ops[BLOCK_ACCT_FLUSH]; s->stats->rd_merged = stats->merged[BLOCK_ACCT_READ]; s->stats->wr_merged = stats->merged[BLOCK_ACCT_WRITE]; s->stats->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH]; s->stats->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE]; s->stats->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ]; s->stats->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH]; s->stats->has_idle_time_ns = stats->last_access_time_ns > 0; if (s->stats->has_idle_time_ns) { s->stats->idle_time_ns = block_acct_idle_time_ns(stats); } s->stats->account_invalid = stats->account_invalid; s->stats->account_failed = stats->account_failed; while ((ts = block_acct_interval_next(stats, ts))) { BlockDeviceTimedStatsList *timed_stats = g_malloc0(sizeof(*timed_stats)); BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats)); timed_stats->next = s->stats->timed_stats; timed_stats->value = dev_stats; s->stats->timed_stats = timed_stats; TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ]; TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE]; TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH]; dev_stats->interval_length = ts->interval_length; dev_stats->min_rd_latency_ns = timed_average_min(rd); dev_stats->max_rd_latency_ns = timed_average_max(rd); dev_stats->avg_rd_latency_ns = timed_average_avg(rd); dev_stats->min_wr_latency_ns = timed_average_min(wr); dev_stats->max_wr_latency_ns = timed_average_max(wr); dev_stats->avg_wr_latency_ns = timed_average_avg(wr); dev_stats->min_flush_latency_ns = timed_average_min(fl); dev_stats->max_flush_latency_ns = timed_average_max(fl); dev_stats->avg_flush_latency_ns = timed_average_avg(fl); dev_stats->avg_rd_queue_depth = block_acct_queue_depth(ts, BLOCK_ACCT_READ); dev_stats->avg_wr_queue_depth = block_acct_queue_depth(ts, BLOCK_ACCT_WRITE); } } s->stats->wr_highest_offset = bs->wr_highest_offset; if (bs->file) { s->has_parent = true; s->parent = bdrv_query_stats(bs->file->bs, query_backing); } if (query_backing && bs->backing) { s->has_backing = true; s->backing = bdrv_query_stats(bs->backing->bs, query_backing); } return s; }