]> git.proxmox.com Git - mirror_qemu.git/blame - block/qapi.c
qemu-img: Use BlockNodeInfo
[mirror_qemu.git] / block / qapi.c
CommitLineData
f364ec65
WX
1/*
2 * Block layer qmp and info dump related functions
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24
80c71a24 25#include "qemu/osdep.h"
31e40415 26#include "qemu/cutils.h"
f364ec65
WX
27#include "block/qapi.h"
28#include "block/block_int.h"
e2c1c34f 29#include "block/dirty-bitmap.h"
76f4afb4 30#include "block/throttle-groups.h"
e2462113 31#include "block/write-threshold.h"
e688df6b 32#include "qapi/error.h"
9af23989 33#include "qapi/qapi-commands-block-core.h"
b3db211f 34#include "qapi/qobject-output-visitor.h"
9af23989 35#include "qapi/qapi-visit-block-core.h"
6b673957 36#include "qapi/qmp/qbool.h"
452fcdbc 37#include "qapi/qmp/qdict.h"
47e6b297 38#include "qapi/qmp/qlist.h"
15280c36 39#include "qapi/qmp/qnum.h"
6b673957 40#include "qapi/qmp/qstring.h"
e1ce7d74 41#include "qemu/qemu-print.h"
d829a211 42#include "sysemu/block-backend.h"
f348b6d1 43#include "qemu/cutils.h"
f364ec65 44
c83f9fba 45BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
facda544
PK
46 BlockDriverState *bs,
47 bool flat,
48 Error **errp)
c13163fb 49{
d5a8ee60 50 ImageInfo **p_image_info;
0b877d09 51 BlockDriverState *bs0, *backing;
d470ad42 52 BlockDeviceInfo *info;
c13163fb 53
d470ad42
HR
54 if (!bs->drv) {
55 error_setg(errp, "Block device %s is ejected", bs->node_name);
56 return NULL;
57 }
58
f30c66ba
HR
59 bdrv_refresh_filename(bs);
60
d470ad42 61 info = g_malloc0(sizeof(*info));
c13163fb 62 info->file = g_strdup(bs->filename);
307261b2 63 info->ro = bdrv_is_read_only(bs);
c13163fb
BC
64 info->drv = g_strdup(bs->drv->format_name);
65 info->encrypted = bs->encrypted;
c13163fb 66
9e193c5a
KW
67 info->cache = g_new(BlockdevCacheInfo, 1);
68 *info->cache = (BlockdevCacheInfo) {
c83f9fba 69 .writeback = blk ? blk_enable_write_cache(blk) : true,
9e193c5a
KW
70 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
71 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
72 };
73
c13163fb 74 if (bs->node_name[0]) {
c13163fb
BC
75 info->node_name = g_strdup(bs->node_name);
76 }
77
0b877d09
HR
78 backing = bdrv_cow_bs(bs);
79 if (backing) {
0b877d09 80 info->backing_file = g_strdup(backing->filename);
c13163fb
BC
81 }
82
590a63d5
VSO
83 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
84 info->has_dirty_bitmaps = true;
85 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
86 }
87
465bee1d 88 info->detect_zeroes = bs->detect_zeroes;
c13163fb 89
022cdc9f 90 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
c13163fb 91 ThrottleConfig cfg;
022cdc9f 92 BlockBackendPublic *blkp = blk_get_public(blk);
76f4afb4 93
022cdc9f 94 throttle_group_get_config(&blkp->throttle_group_member, &cfg);
76f4afb4 95
c13163fb
BC
96 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
97 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
98 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
99
100 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
101 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
102 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
103
104 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
105 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
106 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
107 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
108 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
109 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
110
111 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
112 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
113 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
114 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
115 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
116 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
117
398befdf
AG
118 info->has_bps_max_length = info->has_bps_max;
119 info->bps_max_length =
120 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
121 info->has_bps_rd_max_length = info->has_bps_rd_max;
122 info->bps_rd_max_length =
123 cfg.buckets[THROTTLE_BPS_READ].burst_length;
124 info->has_bps_wr_max_length = info->has_bps_wr_max;
125 info->bps_wr_max_length =
126 cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
127
128 info->has_iops_max_length = info->has_iops_max;
129 info->iops_max_length =
130 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
131 info->has_iops_rd_max_length = info->has_iops_rd_max;
132 info->iops_rd_max_length =
133 cfg.buckets[THROTTLE_OPS_READ].burst_length;
134 info->has_iops_wr_max_length = info->has_iops_wr_max;
135 info->iops_wr_max_length =
136 cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
137
c13163fb
BC
138 info->has_iops_size = cfg.op_size;
139 info->iops_size = cfg.op_size;
b8fe1694 140
022cdc9f
MP
141 info->group =
142 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
c13163fb
BC
143 }
144
e2462113
FR
145 info->write_threshold = bdrv_write_threshold_get(bs);
146
d5a8ee60
AG
147 bs0 = bs;
148 p_image_info = &info->image;
d3c8c674 149 info->backing_file_depth = 0;
d5a8ee60
AG
150 while (1) {
151 Error *local_err = NULL;
152 bdrv_query_image_info(bs0, p_image_info, &local_err);
153 if (local_err) {
154 error_propagate(errp, local_err);
155 qapi_free_BlockDeviceInfo(info);
156 return NULL;
157 }
d3c8c674 158
facda544
PK
159 /* stop gathering data for flat output */
160 if (flat) {
161 break;
162 }
163
0a7585db
HR
164 if (bs0->drv && bdrv_filter_or_cow_child(bs0)) {
165 /*
166 * Put any filtered child here (for backwards compatibility to when
167 * we put bs0->backing here, which might be any filtered child).
168 */
d3c8c674 169 info->backing_file_depth++;
0a7585db 170 bs0 = bdrv_filter_or_cow_bs(bs0);
d5a8ee60
AG
171 p_image_info = &((*p_image_info)->backing_image);
172 } else {
173 break;
174 }
d3c8c674
KW
175
176 /* Skip automatically inserted nodes that the user isn't aware of for
177 * query-block (blk != NULL), but not for query-named-block-nodes */
0a7585db
HR
178 if (blk) {
179 bs0 = bdrv_skip_implicit_filters(bs0);
d3c8c674 180 }
d5a8ee60
AG
181 }
182
c13163fb
BC
183 return info;
184}
185
fb0ed453
WX
186/*
187 * Returns 0 on success, with *p_list either set to describe snapshot
188 * information, or NULL because there are no snapshots. Returns -errno on
189 * error, with *p_list untouched.
190 */
191int bdrv_query_snapshot_info_list(BlockDriverState *bs,
192 SnapshotInfoList **p_list,
193 Error **errp)
f364ec65
WX
194{
195 int i, sn_count;
196 QEMUSnapshotInfo *sn_tab = NULL;
95b3a8c8 197 SnapshotInfoList *head = NULL, **tail = &head;
fb0ed453
WX
198 SnapshotInfo *info;
199
f364ec65 200 sn_count = bdrv_snapshot_list(bs, &sn_tab);
fb0ed453
WX
201 if (sn_count < 0) {
202 const char *dev = bdrv_get_device_name(bs);
203 switch (sn_count) {
204 case -ENOMEDIUM:
205 error_setg(errp, "Device '%s' is not inserted", dev);
206 break;
207 case -ENOTSUP:
208 error_setg(errp,
209 "Device '%s' does not support internal snapshots",
210 dev);
211 break;
212 default:
213 error_setg_errno(errp, -sn_count,
214 "Can't list snapshots of device '%s'", dev);
215 break;
216 }
217 return sn_count;
218 }
f364ec65
WX
219
220 for (i = 0; i < sn_count; i++) {
fb0ed453
WX
221 info = g_new0(SnapshotInfo, 1);
222 info->id = g_strdup(sn_tab[i].id_str);
223 info->name = g_strdup(sn_tab[i].name);
224 info->vm_state_size = sn_tab[i].vm_state_size;
225 info->date_sec = sn_tab[i].date_sec;
226 info->date_nsec = sn_tab[i].date_nsec;
227 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
228 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
b39847a5
PD
229 info->icount = sn_tab[i].icount;
230 info->has_icount = sn_tab[i].icount != -1ULL;
f364ec65 231
95b3a8c8 232 QAPI_LIST_APPEND(tail, info);
f364ec65
WX
233 }
234
235 g_free(sn_tab);
fb0ed453
WX
236 *p_list = head;
237 return 0;
f364ec65
WX
238}
239
43526ec8 240/**
a2085f89
HR
241 * Helper function for other query info functions. Store information about @bs
242 * in @info, setting @errp on error.
43526ec8 243 */
a2085f89
HR
244static void bdrv_do_query_node_info(BlockDriverState *bs,
245 BlockNodeInfo *info,
246 Error **errp)
f364ec65 247{
52bf1e72 248 int64_t size;
43526ec8 249 const char *backing_filename;
f364ec65 250 BlockDriverInfo bdi;
43526ec8
WX
251 int ret;
252 Error *err = NULL;
f364ec65 253
1963f8d5
PB
254 aio_context_acquire(bdrv_get_aio_context(bs));
255
52bf1e72
MA
256 size = bdrv_getlength(bs);
257 if (size < 0) {
9adceb02
FZ
258 error_setg_errno(errp, -size, "Can't get image size '%s'",
259 bs->exact_filename);
1963f8d5 260 goto out;
52bf1e72 261 }
f364ec65 262
f30c66ba
HR
263 bdrv_refresh_filename(bs);
264
43526ec8 265 info->filename = g_strdup(bs->filename);
f364ec65 266 info->format = g_strdup(bdrv_get_format_name(bs));
52bf1e72 267 info->virtual_size = size;
f364ec65
WX
268 info->actual_size = bdrv_get_allocated_file_size(bs);
269 info->has_actual_size = info->actual_size >= 0;
8b8277cd 270 if (bs->encrypted) {
f364ec65
WX
271 info->encrypted = true;
272 info->has_encrypted = true;
273 }
274 if (bdrv_get_info(bs, &bdi) >= 0) {
275 if (bdi.cluster_size != 0) {
276 info->cluster_size = bdi.cluster_size;
277 info->has_cluster_size = true;
278 }
279 info->dirty_flag = bdi.is_dirty;
280 info->has_dirty_flag = true;
281 }
1bf6e9ca
AS
282 info->format_specific = bdrv_get_specific_info(bs, &err);
283 if (err) {
284 error_propagate(errp, err);
1bf6e9ca
AS
285 goto out;
286 }
43526ec8 287 backing_filename = bs->backing_file;
f364ec65 288 if (backing_filename[0] != '\0') {
6b6833c1 289 char *backing_filename2;
0b877d09 290
f364ec65 291 info->backing_filename = g_strdup(backing_filename);
6b6833c1 292 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL);
f364ec65 293
12dcb1c0
JS
294 /* Always report the full_backing_filename if present, even if it's the
295 * same as backing_filename. That they are same is useful info. */
296 if (backing_filename2) {
297 info->full_backing_filename = g_strdup(backing_filename2);
f364ec65
WX
298 }
299
300 if (bs->backing_format[0]) {
301 info->backing_filename_format = g_strdup(bs->backing_format);
f364ec65 302 }
564d64bd 303 g_free(backing_filename2);
f364ec65 304 }
43526ec8
WX
305
306 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
307 switch (ret) {
308 case 0:
309 if (info->snapshots) {
310 info->has_snapshots = true;
311 }
312 break;
313 /* recoverable error */
314 case -ENOMEDIUM:
315 case -ENOTSUP:
316 error_free(err);
317 break;
318 default:
319 error_propagate(errp, err);
1963f8d5 320 goto out;
43526ec8
WX
321 }
322
1963f8d5
PB
323out:
324 aio_context_release(bdrv_get_aio_context(bs));
f364ec65
WX
325}
326
a2085f89
HR
327/**
328 * bdrv_query_block_node_info:
329 * @bs: block node to examine
330 * @p_info: location to store node information
331 * @errp: location to store error information
332 *
333 * Store image information about @bs in @p_info.
334 *
335 * @p_info will be set only on success. On error, store error in @errp.
336 */
337void bdrv_query_block_node_info(BlockDriverState *bs,
338 BlockNodeInfo **p_info,
339 Error **errp)
340{
341 BlockNodeInfo *info;
342 ERRP_GUARD();
343
344 info = g_new0(BlockNodeInfo, 1);
345 bdrv_do_query_node_info(bs, info, errp);
346 if (*errp) {
347 qapi_free_BlockNodeInfo(info);
348 return;
349 }
350
351 *p_info = info;
352}
353
354/**
355 * bdrv_query_image_info:
356 * @bs: block node to examine
357 * @p_info: location to store image information
358 * @errp: location to store error information
359 *
360 * Store "flat" image information in @p_info.
361 *
362 * "Flat" means it does *not* query backing image information,
363 * i.e. (*pinfo)->has_backing_image will be set to false and
364 * (*pinfo)->backing_image to NULL even when the image does in fact have
365 * a backing image.
366 *
367 * @p_info will be set only on success. On error, store error in @errp.
368 */
369void bdrv_query_image_info(BlockDriverState *bs,
370 ImageInfo **p_info,
371 Error **errp)
372{
373 ImageInfo *info;
374 ERRP_GUARD();
375
376 info = g_new0(ImageInfo, 1);
377 bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp);
378 if (*errp) {
379 qapi_free_ImageInfo(info);
380 return;
381 }
382
383 *p_info = info;
384}
385
553a7e87 386/* @p_info will be set only on success. */
d829a211
MA
387static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
388 Error **errp)
f364ec65
WX
389{
390 BlockInfo *info = g_malloc0(sizeof(*info));
d829a211 391 BlockDriverState *bs = blk_bs(blk);
46eade7b
KW
392 char *qdev;
393
d3c8c674 394 /* Skip automatically inserted nodes that the user isn't aware of */
0a7585db 395 bs = bdrv_skip_implicit_filters(bs);
d3c8c674 396
d829a211 397 info->device = g_strdup(blk_name(blk));
f364ec65 398 info->type = g_strdup("unknown");
a7f53e26
MA
399 info->locked = blk_dev_is_medium_locked(blk);
400 info->removable = blk_dev_has_removable_media(blk);
f364ec65 401
46eade7b
KW
402 qdev = blk_get_attached_dev_id(blk);
403 if (qdev && *qdev) {
46eade7b
KW
404 info->qdev = qdev;
405 } else {
406 g_free(qdev);
407 }
408
327032ce 409 if (blk_dev_has_tray(blk)) {
f364ec65 410 info->has_tray_open = true;
a7f53e26 411 info->tray_open = blk_dev_is_tray_open(blk);
f364ec65
WX
412 }
413
373340b2 414 if (blk_iostatus_is_enabled(blk)) {
f364ec65 415 info->has_io_status = true;
373340b2 416 info->io_status = blk_iostatus(blk);
f364ec65
WX
417 }
418
5433c24f 419 if (bs && bs->drv) {
facda544 420 info->inserted = bdrv_block_device_info(blk, bs, false, errp);
d5a8ee60
AG
421 if (info->inserted == NULL) {
422 goto err;
553a7e87 423 }
f364ec65 424 }
553a7e87
WX
425
426 *p_info = info;
427 return;
428
429 err:
430 qapi_free_BlockInfo(info);
f364ec65
WX
431}
432
7e5c776d
VSO
433static uint64List *uint64_list(uint64_t *list, int size)
434{
435 int i;
436 uint64List *out_list = NULL;
c3033fd3 437 uint64List **tail = &out_list;
7e5c776d
VSO
438
439 for (i = 0; i < size; i++) {
c3033fd3 440 QAPI_LIST_APPEND(tail, list[i]);
7e5c776d
VSO
441 }
442
7e5c776d
VSO
443 return out_list;
444}
445
54fde4ff
MA
446static BlockLatencyHistogramInfo *
447bdrv_latency_histogram_stats(BlockLatencyHistogram *hist)
7e5c776d 448{
54fde4ff 449 BlockLatencyHistogramInfo *info;
7e5c776d 450
54fde4ff
MA
451 if (!hist->bins) {
452 return NULL;
7e5c776d 453 }
54fde4ff
MA
454
455 info = g_new0(BlockLatencyHistogramInfo, 1);
456 info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1);
457 info->bins = uint64_list(hist->bins, hist->nbins);
458 return info;
7e5c776d
VSO
459}
460
54302156 461static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
2b77e60a
KW
462{
463 BlockAcctStats *stats = blk_get_stats(blk);
464 BlockAcctTimedStats *ts = NULL;
54fde4ff 465 BlockLatencyHistogram *hgram;
2b77e60a 466
54302156
HR
467 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
468 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
159f85dd 469 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP];
54302156
HR
470 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
471 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
159f85dd 472 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP];
2b77e60a 473
54302156
HR
474 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
475 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
476 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
159f85dd 477 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP];
2b77e60a 478
54302156
HR
479 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
480 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
481 ds->invalid_flush_operations =
2b77e60a 482 stats->invalid_ops[BLOCK_ACCT_FLUSH];
159f85dd 483 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP];
2b77e60a 484
54302156
HR
485 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
486 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
159f85dd 487 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP];
54302156
HR
488 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
489 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
490 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
491 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
159f85dd 492 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP];
2b77e60a 493
54302156
HR
494 ds->has_idle_time_ns = stats->last_access_time_ns > 0;
495 if (ds->has_idle_time_ns) {
496 ds->idle_time_ns = block_acct_idle_time_ns(stats);
2b77e60a
KW
497 }
498
54302156
HR
499 ds->account_invalid = stats->account_invalid;
500 ds->account_failed = stats->account_failed;
2b77e60a
KW
501
502 while ((ts = block_acct_interval_next(stats, ts))) {
2b77e60a 503 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
2b77e60a
KW
504
505 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
506 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
507 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
508
509 dev_stats->interval_length = ts->interval_length;
510
511 dev_stats->min_rd_latency_ns = timed_average_min(rd);
512 dev_stats->max_rd_latency_ns = timed_average_max(rd);
513 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
514
515 dev_stats->min_wr_latency_ns = timed_average_min(wr);
516 dev_stats->max_wr_latency_ns = timed_average_max(wr);
517 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
518
519 dev_stats->min_flush_latency_ns = timed_average_min(fl);
520 dev_stats->max_flush_latency_ns = timed_average_max(fl);
521 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
522
523 dev_stats->avg_rd_queue_depth =
524 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
525 dev_stats->avg_wr_queue_depth =
526 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
54aa3de7
EB
527
528 QAPI_LIST_PREPEND(ds->timed_stats, dev_stats);
2b77e60a 529 }
7e5c776d 530
54fde4ff
MA
531 hgram = stats->latency_histogram;
532 ds->rd_latency_histogram
533 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]);
534 ds->wr_latency_histogram
535 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]);
536 ds->flush_latency_histogram
537 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]);
2b77e60a
KW
538}
539
d3c8c674
KW
540static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
541 bool blk_level)
f364ec65 542{
3f26191c 543 BdrvChild *parent_child;
0a7585db 544 BlockDriverState *filter_or_cow_bs;
20a6d768
DL
545 BlockStats *s = NULL;
546
547 s = g_malloc0(sizeof(*s));
548 s->stats = g_malloc0(sizeof(*s->stats));
549
550 if (!bs) {
551 return s;
552 }
553
d3c8c674
KW
554 /* Skip automatically inserted nodes that the user isn't aware of in
555 * a BlockBackend-level command. Stay at the exact node for a node-level
556 * command. */
0a7585db
HR
557 if (blk_level) {
558 bs = bdrv_skip_implicit_filters(bs);
d3c8c674
KW
559 }
560
4875a779 561 if (bdrv_get_node_name(bs)[0]) {
4875a779
FZ
562 s->node_name = g_strdup(bdrv_get_node_name(bs));
563 }
564
f7946da2 565 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
53d8f9d8 566
d9245599 567 s->driver_specific = bdrv_get_specific_stats(bs);
d9245599 568
3f26191c
HR
569 parent_child = bdrv_primary_child(bs);
570 if (!parent_child ||
571 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED)))
572 {
573 BdrvChild *c;
574
575 /*
576 * Look for a unique data-storing child. We do not need to look for
577 * filtered children, as there would be only one and it would have been
578 * the primary child.
579 */
580 parent_child = NULL;
581 QLIST_FOREACH(c, &bs->children, next) {
582 if (c->role & BDRV_CHILD_DATA) {
583 if (parent_child) {
584 /*
585 * There are multiple data-storing children and we cannot
586 * choose between them.
587 */
588 parent_child = NULL;
589 break;
590 }
591 parent_child = c;
592 }
593 }
594 }
595 if (parent_child) {
3f26191c 596 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level);
f364ec65
WX
597 }
598
0a7585db
HR
599 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs);
600 if (blk_level && filter_or_cow_bs) {
601 /*
602 * Put any filtered or COW child here (for backwards
603 * compatibility to when we put bs0->backing here, which might
604 * be either)
605 */
0a7585db 606 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level);
c8059b97
FZ
607 }
608
20a6d768 609 return s;
b07363a1
KW
610}
611
f364ec65
WX
612BlockInfoList *qmp_query_block(Error **errp)
613{
614 BlockInfoList *head = NULL, **p_next = &head;
d829a211 615 BlockBackend *blk;
553a7e87 616 Error *local_err = NULL;
f364ec65 617
d5b68844
KW
618 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
619 BlockInfoList *info;
620
ec18b0a9 621 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
d5b68844
KW
622 continue;
623 }
624
625 info = g_malloc0(sizeof(*info));
d829a211 626 bdrv_query_info(blk, &info->value, &local_err);
84d18f06 627 if (local_err) {
553a7e87 628 error_propagate(errp, local_err);
903c341d
MA
629 g_free(info);
630 qapi_free_BlockInfoList(head);
631 return NULL;
553a7e87 632 }
f364ec65
WX
633
634 *p_next = info;
635 p_next = &info->next;
636 }
637
638 return head;
639}
640
f71eaa74
FZ
641BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
642 bool query_nodes,
643 Error **errp)
f364ec65 644{
c3033fd3 645 BlockStatsList *head = NULL, **tail = &head;
a6baa608
DL
646 BlockBackend *blk;
647 BlockDriverState *bs;
f364ec65 648
f71eaa74 649 /* Just to be safe if query_nodes is not always initialized */
a6baa608
DL
650 if (has_query_nodes && query_nodes) {
651 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
a6baa608 652 AioContext *ctx = bdrv_get_aio_context(bs);
13344f3a 653
a6baa608 654 aio_context_acquire(ctx);
c3033fd3 655 QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false));
a6baa608 656 aio_context_release(ctx);
a6baa608
DL
657 }
658 } else {
567dcb31 659 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
a6baa608
DL
660 AioContext *ctx = blk_get_aio_context(blk);
661 BlockStats *s;
5a9cb5a9 662 char *qdev;
a6baa608 663
567dcb31
KW
664 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
665 continue;
666 }
667
a6baa608
DL
668 aio_context_acquire(ctx);
669 s = bdrv_query_bds_stats(blk_bs(blk), true);
a6baa608 670 s->device = g_strdup(blk_name(blk));
5a9cb5a9
KW
671
672 qdev = blk_get_attached_dev_id(blk);
673 if (qdev && *qdev) {
5a9cb5a9
KW
674 s->qdev = qdev;
675 } else {
676 g_free(qdev);
677 }
678
a6baa608
DL
679 bdrv_query_blk_stats(s->stats, blk);
680 aio_context_release(ctx);
681
c3033fd3 682 QAPI_LIST_APPEND(tail, s);
a6baa608 683 }
f364ec65
WX
684 }
685
686 return head;
687}
688
e1ce7d74 689void bdrv_snapshot_dump(QEMUSnapshotInfo *sn)
f364ec65 690{
39683553 691 char clock_buf[128];
b39847a5 692 char icount_buf[128] = {0};
f364ec65 693 int64_t secs;
de38b500 694 char *sizing = NULL;
f364ec65
WX
695
696 if (!sn) {
26513a01 697 qemu_printf("%-10s%-17s%8s%20s%13s%11s",
b39847a5 698 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
f364ec65 699 } else {
39683553
DB
700 g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec);
701 g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S");
702
f364ec65
WX
703 secs = sn->vm_clock_nsec / 1000000000;
704 snprintf(clock_buf, sizeof(clock_buf),
705 "%02d:%02d:%02d.%03d",
706 (int)(secs / 3600),
707 (int)((secs / 60) % 60),
708 (int)(secs % 60),
709 (int)((sn->vm_clock_nsec / 1000000) % 1000));
de38b500 710 sizing = size_to_str(sn->vm_state_size);
b39847a5
PD
711 if (sn->icount != -1ULL) {
712 snprintf(icount_buf, sizeof(icount_buf),
713 "%"PRId64, sn->icount);
714 }
26513a01 715 qemu_printf("%-9s %-16s %8s%20s%13s%11s",
e1ce7d74 716 sn->id_str, sn->name,
de38b500 717 sizing,
e1ce7d74 718 date_buf,
b39847a5
PD
719 clock_buf,
720 icount_buf);
f364ec65 721 }
de38b500 722 g_free(sizing);
f364ec65
WX
723}
724
e1ce7d74
MA
725static void dump_qdict(int indentation, QDict *dict);
726static void dump_qlist(int indentation, QList *list);
a8d8ecb7 727
e1ce7d74 728static void dump_qobject(int comp_indent, QObject *obj)
a8d8ecb7
HR
729{
730 switch (qobject_type(obj)) {
01b2ffce 731 case QTYPE_QNUM: {
7dc847eb 732 QNum *value = qobject_to(QNum, obj);
01b2ffce 733 char *tmp = qnum_to_string(value);
e1ce7d74 734 qemu_printf("%s", tmp);
01b2ffce 735 g_free(tmp);
a8d8ecb7
HR
736 break;
737 }
738 case QTYPE_QSTRING: {
7dc847eb 739 QString *value = qobject_to(QString, obj);
e1ce7d74 740 qemu_printf("%s", qstring_get_str(value));
a8d8ecb7
HR
741 break;
742 }
743 case QTYPE_QDICT: {
7dc847eb 744 QDict *value = qobject_to(QDict, obj);
e1ce7d74 745 dump_qdict(comp_indent, value);
a8d8ecb7
HR
746 break;
747 }
748 case QTYPE_QLIST: {
7dc847eb 749 QList *value = qobject_to(QList, obj);
e1ce7d74 750 dump_qlist(comp_indent, value);
a8d8ecb7
HR
751 break;
752 }
a8d8ecb7 753 case QTYPE_QBOOL: {
7dc847eb 754 QBool *value = qobject_to(QBool, obj);
e1ce7d74 755 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false");
a8d8ecb7
HR
756 break;
757 }
a8d8ecb7
HR
758 default:
759 abort();
760 }
761}
762
e1ce7d74 763static void dump_qlist(int indentation, QList *list)
a8d8ecb7
HR
764{
765 const QListEntry *entry;
766 int i = 0;
767
768 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
1310a3d3 769 QType type = qobject_type(entry->value);
a8d8ecb7 770 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
e1ce7d74
MA
771 qemu_printf("%*s[%i]:%c", indentation * 4, "", i,
772 composite ? '\n' : ' ');
773 dump_qobject(indentation + 1, entry->value);
a8d8ecb7 774 if (!composite) {
e1ce7d74 775 qemu_printf("\n");
a8d8ecb7
HR
776 }
777 }
778}
779
e1ce7d74 780static void dump_qdict(int indentation, QDict *dict)
a8d8ecb7
HR
781{
782 const QDictEntry *entry;
783
784 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
1310a3d3 785 QType type = qobject_type(entry->value);
a8d8ecb7 786 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
5eda6227 787 char *key = g_malloc(strlen(entry->key) + 1);
a8d8ecb7
HR
788 int i;
789
790 /* replace dashes with spaces in key (variable) names */
791 for (i = 0; entry->key[i]; i++) {
792 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
793 }
794 key[i] = 0;
e1ce7d74
MA
795 qemu_printf("%*s%s:%c", indentation * 4, "", key,
796 composite ? '\n' : ' ');
797 dump_qobject(indentation + 1, entry->value);
a8d8ecb7 798 if (!composite) {
e1ce7d74 799 qemu_printf("\n");
a8d8ecb7 800 }
5eda6227 801 g_free(key);
a8d8ecb7
HR
802 }
803}
804
3716470b
HR
805/*
806 * Return whether dumping the given QObject with dump_qobject() would
807 * yield an empty dump, i.e. not print anything.
808 */
809static bool qobject_is_empty_dump(const QObject *obj)
810{
811 switch (qobject_type(obj)) {
812 case QTYPE_QNUM:
813 case QTYPE_QSTRING:
814 case QTYPE_QBOOL:
815 return false;
816
817 case QTYPE_QDICT:
818 return qdict_size(qobject_to(QDict, obj)) == 0;
819
820 case QTYPE_QLIST:
821 return qlist_empty(qobject_to(QList, obj));
822
823 default:
824 abort();
825 }
826}
827
828/**
829 * Dumps the given ImageInfoSpecific object in a human-readable form,
830 * prepending an optional prefix if the dump is not empty.
831 */
832void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec,
833 const char *prefix)
a8d8ecb7 834{
a8d8ecb7 835 QObject *obj, *data;
7d5e199a 836 Visitor *v = qobject_output_visitor_new(&obj);
a8d8ecb7 837
3b098d56
EB
838 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
839 visit_complete(v, &obj);
7dc847eb 840 data = qdict_get(qobject_to(QDict, obj), "data");
3716470b
HR
841 if (!qobject_is_empty_dump(data)) {
842 if (prefix) {
843 qemu_printf("%s", prefix);
844 }
845 dump_qobject(1, data);
846 }
cb3e7f08 847 qobject_unref(obj);
3b098d56 848 visit_free(v);
a8d8ecb7
HR
849}
850
b1f4cd15 851void bdrv_node_info_dump(BlockNodeInfo *info)
f364ec65 852{
de38b500 853 char *size_buf, *dsize_buf;
f364ec65 854 if (!info->has_actual_size) {
de38b500 855 dsize_buf = g_strdup("unavailable");
f364ec65 856 } else {
de38b500 857 dsize_buf = size_to_str(info->actual_size);
f364ec65 858 }
de38b500 859 size_buf = size_to_str(info->virtual_size);
e1ce7d74
MA
860 qemu_printf("image: %s\n"
861 "file format: %s\n"
862 "virtual size: %s (%" PRId64 " bytes)\n"
863 "disk size: %s\n",
864 info->filename, info->format, size_buf,
865 info->virtual_size,
866 dsize_buf);
de38b500
EB
867 g_free(size_buf);
868 g_free(dsize_buf);
f364ec65
WX
869
870 if (info->has_encrypted && info->encrypted) {
e1ce7d74 871 qemu_printf("encrypted: yes\n");
f364ec65
WX
872 }
873
874 if (info->has_cluster_size) {
e1ce7d74
MA
875 qemu_printf("cluster_size: %" PRId64 "\n",
876 info->cluster_size);
f364ec65
WX
877 }
878
879 if (info->has_dirty_flag && info->dirty_flag) {
e1ce7d74 880 qemu_printf("cleanly shut down: no\n");
f364ec65
WX
881 }
882
54fde4ff 883 if (info->backing_filename) {
e1ce7d74 884 qemu_printf("backing file: %s", info->backing_filename);
54fde4ff 885 if (!info->full_backing_filename) {
e1ce7d74 886 qemu_printf(" (cannot determine actual path)");
5c9d9ca5
JS
887 } else if (strcmp(info->backing_filename,
888 info->full_backing_filename) != 0) {
e1ce7d74 889 qemu_printf(" (actual path: %s)", info->full_backing_filename);
f364ec65 890 }
e1ce7d74 891 qemu_printf("\n");
54fde4ff 892 if (info->backing_filename_format) {
e1ce7d74
MA
893 qemu_printf("backing file format: %s\n",
894 info->backing_filename_format);
f364ec65
WX
895 }
896 }
897
898 if (info->has_snapshots) {
899 SnapshotInfoList *elem;
f364ec65 900
e1ce7d74
MA
901 qemu_printf("Snapshot list:\n");
902 bdrv_snapshot_dump(NULL);
903 qemu_printf("\n");
f364ec65
WX
904
905 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
906 * we convert to the block layer's native QEMUSnapshotInfo for now.
907 */
908 for (elem = info->snapshots; elem; elem = elem->next) {
909 QEMUSnapshotInfo sn = {
910 .vm_state_size = elem->value->vm_state_size,
911 .date_sec = elem->value->date_sec,
912 .date_nsec = elem->value->date_nsec,
913 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
914 elem->value->vm_clock_nsec,
b39847a5
PD
915 .icount = elem->value->has_icount ?
916 elem->value->icount : -1ULL,
f364ec65
WX
917 };
918
919 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
920 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
e1ce7d74
MA
921 bdrv_snapshot_dump(&sn);
922 qemu_printf("\n");
f364ec65
WX
923 }
924 }
a8d8ecb7 925
54fde4ff 926 if (info->format_specific) {
3716470b
HR
927 bdrv_image_info_specific_dump(info->format_specific,
928 "Format specific information:\n");
a8d8ecb7 929 }
f364ec65 930}