2 * Block layer qmp and info dump related functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
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 "qemu/osdep.h"
26 #include "qemu/cutils.h"
27 #include "block/qapi.h"
28 #include "block/block_int.h"
29 #include "block/dirty-bitmap.h"
30 #include "block/throttle-groups.h"
31 #include "block/write-threshold.h"
32 #include "qapi/error.h"
33 #include "qapi/qapi-commands-block-core.h"
34 #include "qapi/qobject-output-visitor.h"
35 #include "qapi/qapi-visit-block-core.h"
36 #include "qapi/qmp/qbool.h"
37 #include "qapi/qmp/qdict.h"
38 #include "qapi/qmp/qlist.h"
39 #include "qapi/qmp/qnum.h"
40 #include "qapi/qmp/qstring.h"
41 #include "qemu/qemu-print.h"
42 #include "sysemu/block-backend.h"
43 #include "qemu/cutils.h"
45 BlockDeviceInfo
*bdrv_block_device_info(BlockBackend
*blk
,
50 ImageInfo
**p_image_info
;
51 BlockDriverState
*bs0
, *backing
;
52 BlockDeviceInfo
*info
;
55 error_setg(errp
, "Block device %s is ejected", bs
->node_name
);
59 bdrv_refresh_filename(bs
);
61 info
= g_malloc0(sizeof(*info
));
62 info
->file
= g_strdup(bs
->filename
);
63 info
->ro
= bdrv_is_read_only(bs
);
64 info
->drv
= g_strdup(bs
->drv
->format_name
);
65 info
->encrypted
= bs
->encrypted
;
67 info
->cache
= g_new(BlockdevCacheInfo
, 1);
68 *info
->cache
= (BlockdevCacheInfo
) {
69 .writeback
= blk
? blk_enable_write_cache(blk
) : true,
70 .direct
= !!(bs
->open_flags
& BDRV_O_NOCACHE
),
71 .no_flush
= !!(bs
->open_flags
& BDRV_O_NO_FLUSH
),
74 if (bs
->node_name
[0]) {
75 info
->node_name
= g_strdup(bs
->node_name
);
78 backing
= bdrv_cow_bs(bs
);
80 info
->backing_file
= g_strdup(backing
->filename
);
83 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
84 info
->has_dirty_bitmaps
= true;
85 info
->dirty_bitmaps
= bdrv_query_dirty_bitmaps(bs
);
88 info
->detect_zeroes
= bs
->detect_zeroes
;
90 if (blk
&& blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
92 BlockBackendPublic
*blkp
= blk_get_public(blk
);
94 throttle_group_get_config(&blkp
->throttle_group_member
, &cfg
);
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
;
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
;
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
;
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
;
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
;
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
;
138 info
->has_iops_size
= cfg
.op_size
;
139 info
->iops_size
= cfg
.op_size
;
142 g_strdup(throttle_group_get_name(&blkp
->throttle_group_member
));
145 info
->write_threshold
= bdrv_write_threshold_get(bs
);
148 p_image_info
= &info
->image
;
149 info
->backing_file_depth
= 0;
151 Error
*local_err
= NULL
;
152 bdrv_query_image_info(bs0
, p_image_info
, &local_err
);
154 error_propagate(errp
, local_err
);
155 qapi_free_BlockDeviceInfo(info
);
159 /* stop gathering data for flat output */
164 if (bs0
->drv
&& bdrv_filter_or_cow_child(bs0
)) {
166 * Put any filtered child here (for backwards compatibility to when
167 * we put bs0->backing here, which might be any filtered child).
169 info
->backing_file_depth
++;
170 bs0
= bdrv_filter_or_cow_bs(bs0
);
171 p_image_info
= &((*p_image_info
)->backing_image
);
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 */
179 bs0
= bdrv_skip_implicit_filters(bs0
);
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.
191 int bdrv_query_snapshot_info_list(BlockDriverState
*bs
,
192 SnapshotInfoList
**p_list
,
196 QEMUSnapshotInfo
*sn_tab
= NULL
;
197 SnapshotInfoList
*head
= NULL
, **tail
= &head
;
200 sn_count
= bdrv_snapshot_list(bs
, &sn_tab
);
202 const char *dev
= bdrv_get_device_name(bs
);
205 error_setg(errp
, "Device '%s' is not inserted", dev
);
209 "Device '%s' does not support internal snapshots",
213 error_setg_errno(errp
, -sn_count
,
214 "Can't list snapshots of device '%s'", dev
);
220 for (i
= 0; i
< sn_count
; i
++) {
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;
229 info
->icount
= sn_tab
[i
].icount
;
230 info
->has_icount
= sn_tab
[i
].icount
!= -1ULL;
232 QAPI_LIST_APPEND(tail
, info
);
241 * Helper function for other query info functions. Store information about @bs
242 * in @info, setting @errp on error.
244 static void bdrv_do_query_node_info(BlockDriverState
*bs
,
249 const char *backing_filename
;
254 aio_context_acquire(bdrv_get_aio_context(bs
));
256 size
= bdrv_getlength(bs
);
258 error_setg_errno(errp
, -size
, "Can't get image size '%s'",
263 bdrv_refresh_filename(bs
);
265 info
->filename
= g_strdup(bs
->filename
);
266 info
->format
= g_strdup(bdrv_get_format_name(bs
));
267 info
->virtual_size
= size
;
268 info
->actual_size
= bdrv_get_allocated_file_size(bs
);
269 info
->has_actual_size
= info
->actual_size
>= 0;
271 info
->encrypted
= true;
272 info
->has_encrypted
= true;
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;
279 info
->dirty_flag
= bdi
.is_dirty
;
280 info
->has_dirty_flag
= true;
282 info
->format_specific
= bdrv_get_specific_info(bs
, &err
);
284 error_propagate(errp
, err
);
287 backing_filename
= bs
->backing_file
;
288 if (backing_filename
[0] != '\0') {
289 char *backing_filename2
;
291 info
->backing_filename
= g_strdup(backing_filename
);
292 backing_filename2
= bdrv_get_full_backing_filename(bs
, NULL
);
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
);
300 if (bs
->backing_format
[0]) {
301 info
->backing_filename_format
= g_strdup(bs
->backing_format
);
303 g_free(backing_filename2
);
306 ret
= bdrv_query_snapshot_info_list(bs
, &info
->snapshots
, &err
);
309 if (info
->snapshots
) {
310 info
->has_snapshots
= true;
313 /* recoverable error */
319 error_propagate(errp
, err
);
324 aio_context_release(bdrv_get_aio_context(bs
));
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
333 * Store image information about @bs in @p_info.
335 * @p_info will be set only on success. On error, store error in @errp.
337 void bdrv_query_block_node_info(BlockDriverState
*bs
,
338 BlockNodeInfo
**p_info
,
344 info
= g_new0(BlockNodeInfo
, 1);
345 bdrv_do_query_node_info(bs
, info
, errp
);
347 qapi_free_BlockNodeInfo(info
);
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
360 * Store "flat" image information in @p_info.
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
367 * @p_info will be set only on success. On error, store error in @errp.
369 void bdrv_query_image_info(BlockDriverState
*bs
,
376 info
= g_new0(ImageInfo
, 1);
377 bdrv_do_query_node_info(bs
, qapi_ImageInfo_base(info
), errp
);
379 qapi_free_ImageInfo(info
);
386 /* @p_info will be set only on success. */
387 static void bdrv_query_info(BlockBackend
*blk
, BlockInfo
**p_info
,
390 BlockInfo
*info
= g_malloc0(sizeof(*info
));
391 BlockDriverState
*bs
= blk_bs(blk
);
394 /* Skip automatically inserted nodes that the user isn't aware of */
395 bs
= bdrv_skip_implicit_filters(bs
);
397 info
->device
= g_strdup(blk_name(blk
));
398 info
->type
= g_strdup("unknown");
399 info
->locked
= blk_dev_is_medium_locked(blk
);
400 info
->removable
= blk_dev_has_removable_media(blk
);
402 qdev
= blk_get_attached_dev_id(blk
);
409 if (blk_dev_has_tray(blk
)) {
410 info
->has_tray_open
= true;
411 info
->tray_open
= blk_dev_is_tray_open(blk
);
414 if (blk_iostatus_is_enabled(blk
)) {
415 info
->has_io_status
= true;
416 info
->io_status
= blk_iostatus(blk
);
420 info
->inserted
= bdrv_block_device_info(blk
, bs
, false, errp
);
421 if (info
->inserted
== NULL
) {
430 qapi_free_BlockInfo(info
);
433 static uint64List
*uint64_list(uint64_t *list
, int size
)
436 uint64List
*out_list
= NULL
;
437 uint64List
**tail
= &out_list
;
439 for (i
= 0; i
< size
; i
++) {
440 QAPI_LIST_APPEND(tail
, list
[i
]);
446 static BlockLatencyHistogramInfo
*
447 bdrv_latency_histogram_stats(BlockLatencyHistogram
*hist
)
449 BlockLatencyHistogramInfo
*info
;
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
);
461 static void bdrv_query_blk_stats(BlockDeviceStats
*ds
, BlockBackend
*blk
)
463 BlockAcctStats
*stats
= blk_get_stats(blk
);
464 BlockAcctTimedStats
*ts
= NULL
;
465 BlockLatencyHistogram
*hgram
;
467 ds
->rd_bytes
= stats
->nr_bytes
[BLOCK_ACCT_READ
];
468 ds
->wr_bytes
= stats
->nr_bytes
[BLOCK_ACCT_WRITE
];
469 ds
->unmap_bytes
= stats
->nr_bytes
[BLOCK_ACCT_UNMAP
];
470 ds
->rd_operations
= stats
->nr_ops
[BLOCK_ACCT_READ
];
471 ds
->wr_operations
= stats
->nr_ops
[BLOCK_ACCT_WRITE
];
472 ds
->unmap_operations
= stats
->nr_ops
[BLOCK_ACCT_UNMAP
];
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
];
477 ds
->failed_unmap_operations
= stats
->failed_ops
[BLOCK_ACCT_UNMAP
];
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
=
482 stats
->invalid_ops
[BLOCK_ACCT_FLUSH
];
483 ds
->invalid_unmap_operations
= stats
->invalid_ops
[BLOCK_ACCT_UNMAP
];
485 ds
->rd_merged
= stats
->merged
[BLOCK_ACCT_READ
];
486 ds
->wr_merged
= stats
->merged
[BLOCK_ACCT_WRITE
];
487 ds
->unmap_merged
= stats
->merged
[BLOCK_ACCT_UNMAP
];
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
];
492 ds
->unmap_total_time_ns
= stats
->total_time_ns
[BLOCK_ACCT_UNMAP
];
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
);
499 ds
->account_invalid
= stats
->account_invalid
;
500 ds
->account_failed
= stats
->account_failed
;
502 while ((ts
= block_acct_interval_next(stats
, ts
))) {
503 BlockDeviceTimedStats
*dev_stats
= g_malloc0(sizeof(*dev_stats
));
505 TimedAverage
*rd
= &ts
->latency
[BLOCK_ACCT_READ
];
506 TimedAverage
*wr
= &ts
->latency
[BLOCK_ACCT_WRITE
];
507 TimedAverage
*fl
= &ts
->latency
[BLOCK_ACCT_FLUSH
];
509 dev_stats
->interval_length
= ts
->interval_length
;
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
);
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
);
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
);
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
);
528 QAPI_LIST_PREPEND(ds
->timed_stats
, dev_stats
);
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
]);
540 static BlockStats
*bdrv_query_bds_stats(BlockDriverState
*bs
,
543 BdrvChild
*parent_child
;
544 BlockDriverState
*filter_or_cow_bs
;
545 BlockStats
*s
= NULL
;
547 s
= g_malloc0(sizeof(*s
));
548 s
->stats
= g_malloc0(sizeof(*s
->stats
));
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
558 bs
= bdrv_skip_implicit_filters(bs
);
561 if (bdrv_get_node_name(bs
)[0]) {
562 s
->node_name
= g_strdup(bdrv_get_node_name(bs
));
565 s
->stats
->wr_highest_offset
= stat64_get(&bs
->wr_highest_offset
);
567 s
->driver_specific
= bdrv_get_specific_stats(bs
);
569 parent_child
= bdrv_primary_child(bs
);
571 !(parent_child
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_FILTERED
)))
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
581 QLIST_FOREACH(c
, &bs
->children
, next
) {
582 if (c
->role
& BDRV_CHILD_DATA
) {
585 * There are multiple data-storing children and we cannot
586 * choose between them.
596 s
->parent
= bdrv_query_bds_stats(parent_child
->bs
, blk_level
);
599 filter_or_cow_bs
= bdrv_filter_or_cow_bs(bs
);
600 if (blk_level
&& filter_or_cow_bs
) {
602 * Put any filtered or COW child here (for backwards
603 * compatibility to when we put bs0->backing here, which might
606 s
->backing
= bdrv_query_bds_stats(filter_or_cow_bs
, blk_level
);
612 BlockInfoList
*qmp_query_block(Error
**errp
)
614 BlockInfoList
*head
= NULL
, **p_next
= &head
;
616 Error
*local_err
= NULL
;
618 for (blk
= blk_all_next(NULL
); blk
; blk
= blk_all_next(blk
)) {
621 if (!*blk_name(blk
) && !blk_get_attached_dev(blk
)) {
625 info
= g_malloc0(sizeof(*info
));
626 bdrv_query_info(blk
, &info
->value
, &local_err
);
628 error_propagate(errp
, local_err
);
630 qapi_free_BlockInfoList(head
);
635 p_next
= &info
->next
;
641 BlockStatsList
*qmp_query_blockstats(bool has_query_nodes
,
645 BlockStatsList
*head
= NULL
, **tail
= &head
;
647 BlockDriverState
*bs
;
649 /* Just to be safe if query_nodes is not always initialized */
650 if (has_query_nodes
&& query_nodes
) {
651 for (bs
= bdrv_next_node(NULL
); bs
; bs
= bdrv_next_node(bs
)) {
652 AioContext
*ctx
= bdrv_get_aio_context(bs
);
654 aio_context_acquire(ctx
);
655 QAPI_LIST_APPEND(tail
, bdrv_query_bds_stats(bs
, false));
656 aio_context_release(ctx
);
659 for (blk
= blk_all_next(NULL
); blk
; blk
= blk_all_next(blk
)) {
660 AioContext
*ctx
= blk_get_aio_context(blk
);
664 if (!*blk_name(blk
) && !blk_get_attached_dev(blk
)) {
668 aio_context_acquire(ctx
);
669 s
= bdrv_query_bds_stats(blk_bs(blk
), true);
670 s
->device
= g_strdup(blk_name(blk
));
672 qdev
= blk_get_attached_dev_id(blk
);
679 bdrv_query_blk_stats(s
->stats
, blk
);
680 aio_context_release(ctx
);
682 QAPI_LIST_APPEND(tail
, s
);
689 void bdrv_snapshot_dump(QEMUSnapshotInfo
*sn
)
692 char icount_buf
[128] = {0};
697 qemu_printf("%-10s%-17s%8s%20s%13s%11s",
698 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
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");
703 secs
= sn
->vm_clock_nsec
/ 1000000000;
704 snprintf(clock_buf
, sizeof(clock_buf
),
705 "%02d:%02d:%02d.%03d",
707 (int)((secs
/ 60) % 60),
709 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
710 sizing
= size_to_str(sn
->vm_state_size
);
711 if (sn
->icount
!= -1ULL) {
712 snprintf(icount_buf
, sizeof(icount_buf
),
713 "%"PRId64
, sn
->icount
);
715 qemu_printf("%-9s %-16s %8s%20s%13s%11s",
716 sn
->id_str
, sn
->name
,
725 static void dump_qdict(int indentation
, QDict
*dict
);
726 static void dump_qlist(int indentation
, QList
*list
);
728 static void dump_qobject(int comp_indent
, QObject
*obj
)
730 switch (qobject_type(obj
)) {
732 QNum
*value
= qobject_to(QNum
, obj
);
733 char *tmp
= qnum_to_string(value
);
734 qemu_printf("%s", tmp
);
738 case QTYPE_QSTRING
: {
739 QString
*value
= qobject_to(QString
, obj
);
740 qemu_printf("%s", qstring_get_str(value
));
744 QDict
*value
= qobject_to(QDict
, obj
);
745 dump_qdict(comp_indent
, value
);
749 QList
*value
= qobject_to(QList
, obj
);
750 dump_qlist(comp_indent
, value
);
754 QBool
*value
= qobject_to(QBool
, obj
);
755 qemu_printf("%s", qbool_get_bool(value
) ? "true" : "false");
763 static void dump_qlist(int indentation
, QList
*list
)
765 const QListEntry
*entry
;
768 for (entry
= qlist_first(list
); entry
; entry
= qlist_next(entry
), i
++) {
769 QType type
= qobject_type(entry
->value
);
770 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
771 qemu_printf("%*s[%i]:%c", indentation
* 4, "", i
,
772 composite
? '\n' : ' ');
773 dump_qobject(indentation
+ 1, entry
->value
);
780 static void dump_qdict(int indentation
, QDict
*dict
)
782 const QDictEntry
*entry
;
784 for (entry
= qdict_first(dict
); entry
; entry
= qdict_next(dict
, entry
)) {
785 QType type
= qobject_type(entry
->value
);
786 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
787 char *key
= g_malloc(strlen(entry
->key
) + 1);
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
];
795 qemu_printf("%*s%s:%c", indentation
* 4, "", key
,
796 composite
? '\n' : ' ');
797 dump_qobject(indentation
+ 1, entry
->value
);
806 * Return whether dumping the given QObject with dump_qobject() would
807 * yield an empty dump, i.e. not print anything.
809 static bool qobject_is_empty_dump(const QObject
*obj
)
811 switch (qobject_type(obj
)) {
818 return qdict_size(qobject_to(QDict
, obj
)) == 0;
821 return qlist_empty(qobject_to(QList
, obj
));
829 * Dumps the given ImageInfoSpecific object in a human-readable form,
830 * prepending an optional prefix if the dump is not empty.
832 void bdrv_image_info_specific_dump(ImageInfoSpecific
*info_spec
,
836 Visitor
*v
= qobject_output_visitor_new(&obj
);
838 visit_type_ImageInfoSpecific(v
, NULL
, &info_spec
, &error_abort
);
839 visit_complete(v
, &obj
);
840 data
= qdict_get(qobject_to(QDict
, obj
), "data");
841 if (!qobject_is_empty_dump(data
)) {
843 qemu_printf("%s", prefix
);
845 dump_qobject(1, data
);
851 void bdrv_image_info_dump(ImageInfo
*info
)
853 char *size_buf
, *dsize_buf
;
854 if (!info
->has_actual_size
) {
855 dsize_buf
= g_strdup("unavailable");
857 dsize_buf
= size_to_str(info
->actual_size
);
859 size_buf
= size_to_str(info
->virtual_size
);
860 qemu_printf("image: %s\n"
862 "virtual size: %s (%" PRId64
" bytes)\n"
864 info
->filename
, info
->format
, size_buf
,
870 if (info
->has_encrypted
&& info
->encrypted
) {
871 qemu_printf("encrypted: yes\n");
874 if (info
->has_cluster_size
) {
875 qemu_printf("cluster_size: %" PRId64
"\n",
879 if (info
->has_dirty_flag
&& info
->dirty_flag
) {
880 qemu_printf("cleanly shut down: no\n");
883 if (info
->backing_filename
) {
884 qemu_printf("backing file: %s", info
->backing_filename
);
885 if (!info
->full_backing_filename
) {
886 qemu_printf(" (cannot determine actual path)");
887 } else if (strcmp(info
->backing_filename
,
888 info
->full_backing_filename
) != 0) {
889 qemu_printf(" (actual path: %s)", info
->full_backing_filename
);
892 if (info
->backing_filename_format
) {
893 qemu_printf("backing file format: %s\n",
894 info
->backing_filename_format
);
898 if (info
->has_snapshots
) {
899 SnapshotInfoList
*elem
;
901 qemu_printf("Snapshot list:\n");
902 bdrv_snapshot_dump(NULL
);
905 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
906 * we convert to the block layer's native QEMUSnapshotInfo for now.
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
,
915 .icount
= elem
->value
->has_icount
?
916 elem
->value
->icount
: -1ULL,
919 pstrcpy(sn
.id_str
, sizeof(sn
.id_str
), elem
->value
->id
);
920 pstrcpy(sn
.name
, sizeof(sn
.name
), elem
->value
->name
);
921 bdrv_snapshot_dump(&sn
);
926 if (info
->format_specific
) {
927 bdrv_image_info_specific_dump(info
->format_specific
,
928 "Format specific information:\n");