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/throttle-groups.h"
30 #include "block/write-threshold.h"
31 #include "qapi/error.h"
32 #include "qapi/qapi-commands-block-core.h"
33 #include "qapi/qobject-output-visitor.h"
34 #include "qapi/qapi-visit-block-core.h"
35 #include "qapi/qmp/qbool.h"
36 #include "qapi/qmp/qdict.h"
37 #include "qapi/qmp/qlist.h"
38 #include "qapi/qmp/qnum.h"
39 #include "qapi/qmp/qstring.h"
40 #include "qemu/qemu-print.h"
41 #include "sysemu/block-backend.h"
42 #include "qemu/cutils.h"
44 BlockDeviceInfo
*bdrv_block_device_info(BlockBackend
*blk
,
49 ImageInfo
**p_image_info
;
50 BlockDriverState
*bs0
, *backing
;
51 BlockDeviceInfo
*info
;
54 error_setg(errp
, "Block device %s is ejected", bs
->node_name
);
58 bdrv_refresh_filename(bs
);
60 info
= g_malloc0(sizeof(*info
));
61 info
->file
= g_strdup(bs
->filename
);
62 info
->ro
= bs
->read_only
;
63 info
->drv
= g_strdup(bs
->drv
->format_name
);
64 info
->encrypted
= bs
->encrypted
;
65 info
->encryption_key_missing
= false;
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
->has_node_name
= true;
76 info
->node_name
= g_strdup(bs
->node_name
);
79 backing
= bdrv_cow_bs(bs
);
81 info
->has_backing_file
= true;
82 info
->backing_file
= g_strdup(backing
->filename
);
85 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
86 info
->has_dirty_bitmaps
= true;
87 info
->dirty_bitmaps
= bdrv_query_dirty_bitmaps(bs
);
90 info
->detect_zeroes
= bs
->detect_zeroes
;
92 if (blk
&& blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
94 BlockBackendPublic
*blkp
= blk_get_public(blk
);
96 throttle_group_get_config(&blkp
->throttle_group_member
, &cfg
);
98 info
->bps
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
;
99 info
->bps_rd
= cfg
.buckets
[THROTTLE_BPS_READ
].avg
;
100 info
->bps_wr
= cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
;
102 info
->iops
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
;
103 info
->iops_rd
= cfg
.buckets
[THROTTLE_OPS_READ
].avg
;
104 info
->iops_wr
= cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
;
106 info
->has_bps_max
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
107 info
->bps_max
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
108 info
->has_bps_rd_max
= cfg
.buckets
[THROTTLE_BPS_READ
].max
;
109 info
->bps_rd_max
= cfg
.buckets
[THROTTLE_BPS_READ
].max
;
110 info
->has_bps_wr_max
= cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
111 info
->bps_wr_max
= cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
113 info
->has_iops_max
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
114 info
->iops_max
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
115 info
->has_iops_rd_max
= cfg
.buckets
[THROTTLE_OPS_READ
].max
;
116 info
->iops_rd_max
= cfg
.buckets
[THROTTLE_OPS_READ
].max
;
117 info
->has_iops_wr_max
= cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
118 info
->iops_wr_max
= cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
120 info
->has_bps_max_length
= info
->has_bps_max
;
121 info
->bps_max_length
=
122 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
;
123 info
->has_bps_rd_max_length
= info
->has_bps_rd_max
;
124 info
->bps_rd_max_length
=
125 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
;
126 info
->has_bps_wr_max_length
= info
->has_bps_wr_max
;
127 info
->bps_wr_max_length
=
128 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
;
130 info
->has_iops_max_length
= info
->has_iops_max
;
131 info
->iops_max_length
=
132 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
;
133 info
->has_iops_rd_max_length
= info
->has_iops_rd_max
;
134 info
->iops_rd_max_length
=
135 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
;
136 info
->has_iops_wr_max_length
= info
->has_iops_wr_max
;
137 info
->iops_wr_max_length
=
138 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
;
140 info
->has_iops_size
= cfg
.op_size
;
141 info
->iops_size
= cfg
.op_size
;
143 info
->has_group
= true;
145 g_strdup(throttle_group_get_name(&blkp
->throttle_group_member
));
148 info
->write_threshold
= bdrv_write_threshold_get(bs
);
151 p_image_info
= &info
->image
;
152 info
->backing_file_depth
= 0;
154 Error
*local_err
= NULL
;
155 bdrv_query_image_info(bs0
, p_image_info
, &local_err
);
157 error_propagate(errp
, local_err
);
158 qapi_free_BlockDeviceInfo(info
);
162 /* stop gathering data for flat output */
167 if (bs0
->drv
&& bdrv_filter_or_cow_child(bs0
)) {
169 * Put any filtered child here (for backwards compatibility to when
170 * we put bs0->backing here, which might be any filtered child).
172 info
->backing_file_depth
++;
173 bs0
= bdrv_filter_or_cow_bs(bs0
);
174 (*p_image_info
)->has_backing_image
= true;
175 p_image_info
= &((*p_image_info
)->backing_image
);
180 /* Skip automatically inserted nodes that the user isn't aware of for
181 * query-block (blk != NULL), but not for query-named-block-nodes */
183 bs0
= bdrv_skip_implicit_filters(bs0
);
191 * Returns 0 on success, with *p_list either set to describe snapshot
192 * information, or NULL because there are no snapshots. Returns -errno on
193 * error, with *p_list untouched.
195 int bdrv_query_snapshot_info_list(BlockDriverState
*bs
,
196 SnapshotInfoList
**p_list
,
200 QEMUSnapshotInfo
*sn_tab
= NULL
;
201 SnapshotInfoList
*head
= NULL
, **tail
= &head
;
204 sn_count
= bdrv_snapshot_list(bs
, &sn_tab
);
206 const char *dev
= bdrv_get_device_name(bs
);
209 error_setg(errp
, "Device '%s' is not inserted", dev
);
213 "Device '%s' does not support internal snapshots",
217 error_setg_errno(errp
, -sn_count
,
218 "Can't list snapshots of device '%s'", dev
);
224 for (i
= 0; i
< sn_count
; i
++) {
225 info
= g_new0(SnapshotInfo
, 1);
226 info
->id
= g_strdup(sn_tab
[i
].id_str
);
227 info
->name
= g_strdup(sn_tab
[i
].name
);
228 info
->vm_state_size
= sn_tab
[i
].vm_state_size
;
229 info
->date_sec
= sn_tab
[i
].date_sec
;
230 info
->date_nsec
= sn_tab
[i
].date_nsec
;
231 info
->vm_clock_sec
= sn_tab
[i
].vm_clock_nsec
/ 1000000000;
232 info
->vm_clock_nsec
= sn_tab
[i
].vm_clock_nsec
% 1000000000;
233 info
->icount
= sn_tab
[i
].icount
;
234 info
->has_icount
= sn_tab
[i
].icount
!= -1ULL;
236 QAPI_LIST_APPEND(tail
, info
);
245 * bdrv_query_image_info:
246 * @bs: block device to examine
247 * @p_info: location to store image information
248 * @errp: location to store error information
250 * Store "flat" image information in @p_info.
252 * "Flat" means it does *not* query backing image information,
253 * i.e. (*pinfo)->has_backing_image will be set to false and
254 * (*pinfo)->backing_image to NULL even when the image does in fact have
257 * @p_info will be set only on success. On error, store error in @errp.
259 void bdrv_query_image_info(BlockDriverState
*bs
,
264 const char *backing_filename
;
270 aio_context_acquire(bdrv_get_aio_context(bs
));
272 size
= bdrv_getlength(bs
);
274 error_setg_errno(errp
, -size
, "Can't get image size '%s'",
279 bdrv_refresh_filename(bs
);
281 info
= g_new0(ImageInfo
, 1);
282 info
->filename
= g_strdup(bs
->filename
);
283 info
->format
= g_strdup(bdrv_get_format_name(bs
));
284 info
->virtual_size
= size
;
285 info
->actual_size
= bdrv_get_allocated_file_size(bs
);
286 info
->has_actual_size
= info
->actual_size
>= 0;
288 info
->encrypted
= true;
289 info
->has_encrypted
= true;
291 if (bdrv_get_info(bs
, &bdi
) >= 0) {
292 if (bdi
.cluster_size
!= 0) {
293 info
->cluster_size
= bdi
.cluster_size
;
294 info
->has_cluster_size
= true;
296 info
->dirty_flag
= bdi
.is_dirty
;
297 info
->has_dirty_flag
= true;
299 info
->format_specific
= bdrv_get_specific_info(bs
, &err
);
301 error_propagate(errp
, err
);
302 qapi_free_ImageInfo(info
);
305 info
->has_format_specific
= info
->format_specific
!= NULL
;
307 backing_filename
= bs
->backing_file
;
308 if (backing_filename
[0] != '\0') {
309 char *backing_filename2
;
311 info
->backing_filename
= g_strdup(backing_filename
);
312 info
->has_backing_filename
= true;
313 backing_filename2
= bdrv_get_full_backing_filename(bs
, NULL
);
315 /* Always report the full_backing_filename if present, even if it's the
316 * same as backing_filename. That they are same is useful info. */
317 if (backing_filename2
) {
318 info
->full_backing_filename
= g_strdup(backing_filename2
);
319 info
->has_full_backing_filename
= true;
322 if (bs
->backing_format
[0]) {
323 info
->backing_filename_format
= g_strdup(bs
->backing_format
);
324 info
->has_backing_filename_format
= true;
326 g_free(backing_filename2
);
329 ret
= bdrv_query_snapshot_info_list(bs
, &info
->snapshots
, &err
);
332 if (info
->snapshots
) {
333 info
->has_snapshots
= true;
336 /* recoverable error */
342 error_propagate(errp
, err
);
343 qapi_free_ImageInfo(info
);
350 aio_context_release(bdrv_get_aio_context(bs
));
353 /* @p_info will be set only on success. */
354 static void bdrv_query_info(BlockBackend
*blk
, BlockInfo
**p_info
,
357 BlockInfo
*info
= g_malloc0(sizeof(*info
));
358 BlockDriverState
*bs
= blk_bs(blk
);
361 /* Skip automatically inserted nodes that the user isn't aware of */
362 bs
= bdrv_skip_implicit_filters(bs
);
364 info
->device
= g_strdup(blk_name(blk
));
365 info
->type
= g_strdup("unknown");
366 info
->locked
= blk_dev_is_medium_locked(blk
);
367 info
->removable
= blk_dev_has_removable_media(blk
);
369 qdev
= blk_get_attached_dev_id(blk
);
371 info
->has_qdev
= true;
377 if (blk_dev_has_tray(blk
)) {
378 info
->has_tray_open
= true;
379 info
->tray_open
= blk_dev_is_tray_open(blk
);
382 if (blk_iostatus_is_enabled(blk
)) {
383 info
->has_io_status
= true;
384 info
->io_status
= blk_iostatus(blk
);
387 if (bs
&& !QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
388 info
->has_dirty_bitmaps
= true;
389 info
->dirty_bitmaps
= bdrv_query_dirty_bitmaps(bs
);
393 info
->has_inserted
= true;
394 info
->inserted
= bdrv_block_device_info(blk
, bs
, false, errp
);
395 if (info
->inserted
== NULL
) {
404 qapi_free_BlockInfo(info
);
407 static uint64List
*uint64_list(uint64_t *list
, int size
)
410 uint64List
*out_list
= NULL
;
411 uint64List
**tail
= &out_list
;
413 for (i
= 0; i
< size
; i
++) {
414 QAPI_LIST_APPEND(tail
, list
[i
]);
420 static void bdrv_latency_histogram_stats(BlockLatencyHistogram
*hist
,
422 BlockLatencyHistogramInfo
**info
)
424 *not_null
= hist
->bins
!= NULL
;
426 *info
= g_new0(BlockLatencyHistogramInfo
, 1);
428 (*info
)->boundaries
= uint64_list(hist
->boundaries
, hist
->nbins
- 1);
429 (*info
)->bins
= uint64_list(hist
->bins
, hist
->nbins
);
433 static void bdrv_query_blk_stats(BlockDeviceStats
*ds
, BlockBackend
*blk
)
435 BlockAcctStats
*stats
= blk_get_stats(blk
);
436 BlockAcctTimedStats
*ts
= NULL
;
438 ds
->rd_bytes
= stats
->nr_bytes
[BLOCK_ACCT_READ
];
439 ds
->wr_bytes
= stats
->nr_bytes
[BLOCK_ACCT_WRITE
];
440 ds
->unmap_bytes
= stats
->nr_bytes
[BLOCK_ACCT_UNMAP
];
441 ds
->rd_operations
= stats
->nr_ops
[BLOCK_ACCT_READ
];
442 ds
->wr_operations
= stats
->nr_ops
[BLOCK_ACCT_WRITE
];
443 ds
->unmap_operations
= stats
->nr_ops
[BLOCK_ACCT_UNMAP
];
445 ds
->failed_rd_operations
= stats
->failed_ops
[BLOCK_ACCT_READ
];
446 ds
->failed_wr_operations
= stats
->failed_ops
[BLOCK_ACCT_WRITE
];
447 ds
->failed_flush_operations
= stats
->failed_ops
[BLOCK_ACCT_FLUSH
];
448 ds
->failed_unmap_operations
= stats
->failed_ops
[BLOCK_ACCT_UNMAP
];
450 ds
->invalid_rd_operations
= stats
->invalid_ops
[BLOCK_ACCT_READ
];
451 ds
->invalid_wr_operations
= stats
->invalid_ops
[BLOCK_ACCT_WRITE
];
452 ds
->invalid_flush_operations
=
453 stats
->invalid_ops
[BLOCK_ACCT_FLUSH
];
454 ds
->invalid_unmap_operations
= stats
->invalid_ops
[BLOCK_ACCT_UNMAP
];
456 ds
->rd_merged
= stats
->merged
[BLOCK_ACCT_READ
];
457 ds
->wr_merged
= stats
->merged
[BLOCK_ACCT_WRITE
];
458 ds
->unmap_merged
= stats
->merged
[BLOCK_ACCT_UNMAP
];
459 ds
->flush_operations
= stats
->nr_ops
[BLOCK_ACCT_FLUSH
];
460 ds
->wr_total_time_ns
= stats
->total_time_ns
[BLOCK_ACCT_WRITE
];
461 ds
->rd_total_time_ns
= stats
->total_time_ns
[BLOCK_ACCT_READ
];
462 ds
->flush_total_time_ns
= stats
->total_time_ns
[BLOCK_ACCT_FLUSH
];
463 ds
->unmap_total_time_ns
= stats
->total_time_ns
[BLOCK_ACCT_UNMAP
];
465 ds
->has_idle_time_ns
= stats
->last_access_time_ns
> 0;
466 if (ds
->has_idle_time_ns
) {
467 ds
->idle_time_ns
= block_acct_idle_time_ns(stats
);
470 ds
->account_invalid
= stats
->account_invalid
;
471 ds
->account_failed
= stats
->account_failed
;
473 while ((ts
= block_acct_interval_next(stats
, ts
))) {
474 BlockDeviceTimedStats
*dev_stats
= g_malloc0(sizeof(*dev_stats
));
476 TimedAverage
*rd
= &ts
->latency
[BLOCK_ACCT_READ
];
477 TimedAverage
*wr
= &ts
->latency
[BLOCK_ACCT_WRITE
];
478 TimedAverage
*fl
= &ts
->latency
[BLOCK_ACCT_FLUSH
];
480 dev_stats
->interval_length
= ts
->interval_length
;
482 dev_stats
->min_rd_latency_ns
= timed_average_min(rd
);
483 dev_stats
->max_rd_latency_ns
= timed_average_max(rd
);
484 dev_stats
->avg_rd_latency_ns
= timed_average_avg(rd
);
486 dev_stats
->min_wr_latency_ns
= timed_average_min(wr
);
487 dev_stats
->max_wr_latency_ns
= timed_average_max(wr
);
488 dev_stats
->avg_wr_latency_ns
= timed_average_avg(wr
);
490 dev_stats
->min_flush_latency_ns
= timed_average_min(fl
);
491 dev_stats
->max_flush_latency_ns
= timed_average_max(fl
);
492 dev_stats
->avg_flush_latency_ns
= timed_average_avg(fl
);
494 dev_stats
->avg_rd_queue_depth
=
495 block_acct_queue_depth(ts
, BLOCK_ACCT_READ
);
496 dev_stats
->avg_wr_queue_depth
=
497 block_acct_queue_depth(ts
, BLOCK_ACCT_WRITE
);
499 QAPI_LIST_PREPEND(ds
->timed_stats
, dev_stats
);
502 bdrv_latency_histogram_stats(&stats
->latency_histogram
[BLOCK_ACCT_READ
],
503 &ds
->has_rd_latency_histogram
,
504 &ds
->rd_latency_histogram
);
505 bdrv_latency_histogram_stats(&stats
->latency_histogram
[BLOCK_ACCT_WRITE
],
506 &ds
->has_wr_latency_histogram
,
507 &ds
->wr_latency_histogram
);
508 bdrv_latency_histogram_stats(&stats
->latency_histogram
[BLOCK_ACCT_FLUSH
],
509 &ds
->has_flush_latency_histogram
,
510 &ds
->flush_latency_histogram
);
513 static BlockStats
*bdrv_query_bds_stats(BlockDriverState
*bs
,
516 BdrvChild
*parent_child
;
517 BlockDriverState
*filter_or_cow_bs
;
518 BlockStats
*s
= NULL
;
520 s
= g_malloc0(sizeof(*s
));
521 s
->stats
= g_malloc0(sizeof(*s
->stats
));
527 /* Skip automatically inserted nodes that the user isn't aware of in
528 * a BlockBackend-level command. Stay at the exact node for a node-level
531 bs
= bdrv_skip_implicit_filters(bs
);
534 if (bdrv_get_node_name(bs
)[0]) {
535 s
->has_node_name
= true;
536 s
->node_name
= g_strdup(bdrv_get_node_name(bs
));
539 s
->stats
->wr_highest_offset
= stat64_get(&bs
->wr_highest_offset
);
541 s
->driver_specific
= bdrv_get_specific_stats(bs
);
542 if (s
->driver_specific
) {
543 s
->has_driver_specific
= true;
546 parent_child
= bdrv_primary_child(bs
);
548 !(parent_child
->role
& (BDRV_CHILD_DATA
| BDRV_CHILD_FILTERED
)))
553 * Look for a unique data-storing child. We do not need to look for
554 * filtered children, as there would be only one and it would have been
558 QLIST_FOREACH(c
, &bs
->children
, next
) {
559 if (c
->role
& BDRV_CHILD_DATA
) {
562 * There are multiple data-storing children and we cannot
563 * choose between them.
573 s
->has_parent
= true;
574 s
->parent
= bdrv_query_bds_stats(parent_child
->bs
, blk_level
);
577 filter_or_cow_bs
= bdrv_filter_or_cow_bs(bs
);
578 if (blk_level
&& filter_or_cow_bs
) {
580 * Put any filtered or COW child here (for backwards
581 * compatibility to when we put bs0->backing here, which might
584 s
->has_backing
= true;
585 s
->backing
= bdrv_query_bds_stats(filter_or_cow_bs
, blk_level
);
591 BlockInfoList
*qmp_query_block(Error
**errp
)
593 BlockInfoList
*head
= NULL
, **p_next
= &head
;
595 Error
*local_err
= NULL
;
597 for (blk
= blk_all_next(NULL
); blk
; blk
= blk_all_next(blk
)) {
600 if (!*blk_name(blk
) && !blk_get_attached_dev(blk
)) {
604 info
= g_malloc0(sizeof(*info
));
605 bdrv_query_info(blk
, &info
->value
, &local_err
);
607 error_propagate(errp
, local_err
);
609 qapi_free_BlockInfoList(head
);
614 p_next
= &info
->next
;
620 BlockStatsList
*qmp_query_blockstats(bool has_query_nodes
,
624 BlockStatsList
*head
= NULL
, **tail
= &head
;
626 BlockDriverState
*bs
;
628 /* Just to be safe if query_nodes is not always initialized */
629 if (has_query_nodes
&& query_nodes
) {
630 for (bs
= bdrv_next_node(NULL
); bs
; bs
= bdrv_next_node(bs
)) {
631 AioContext
*ctx
= bdrv_get_aio_context(bs
);
633 aio_context_acquire(ctx
);
634 QAPI_LIST_APPEND(tail
, bdrv_query_bds_stats(bs
, false));
635 aio_context_release(ctx
);
638 for (blk
= blk_all_next(NULL
); blk
; blk
= blk_all_next(blk
)) {
639 AioContext
*ctx
= blk_get_aio_context(blk
);
643 if (!*blk_name(blk
) && !blk_get_attached_dev(blk
)) {
647 aio_context_acquire(ctx
);
648 s
= bdrv_query_bds_stats(blk_bs(blk
), true);
649 s
->has_device
= true;
650 s
->device
= g_strdup(blk_name(blk
));
652 qdev
= blk_get_attached_dev_id(blk
);
660 bdrv_query_blk_stats(s
->stats
, blk
);
661 aio_context_release(ctx
);
663 QAPI_LIST_APPEND(tail
, s
);
670 void bdrv_snapshot_dump(QEMUSnapshotInfo
*sn
)
672 char date_buf
[128], clock_buf
[128];
673 char icount_buf
[128] = {0};
680 qemu_printf("%-10s%-17s%8s%20s%13s%11s",
681 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT");
684 localtime_r(&ti
, &tm
);
685 strftime(date_buf
, sizeof(date_buf
),
686 "%Y-%m-%d %H:%M:%S", &tm
);
687 secs
= sn
->vm_clock_nsec
/ 1000000000;
688 snprintf(clock_buf
, sizeof(clock_buf
),
689 "%02d:%02d:%02d.%03d",
691 (int)((secs
/ 60) % 60),
693 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
694 sizing
= size_to_str(sn
->vm_state_size
);
695 if (sn
->icount
!= -1ULL) {
696 snprintf(icount_buf
, sizeof(icount_buf
),
697 "%"PRId64
, sn
->icount
);
699 qemu_printf("%-9s %-16s %8s%20s%13s%11s",
700 sn
->id_str
, sn
->name
,
709 static void dump_qdict(int indentation
, QDict
*dict
);
710 static void dump_qlist(int indentation
, QList
*list
);
712 static void dump_qobject(int comp_indent
, QObject
*obj
)
714 switch (qobject_type(obj
)) {
716 QNum
*value
= qobject_to(QNum
, obj
);
717 char *tmp
= qnum_to_string(value
);
718 qemu_printf("%s", tmp
);
722 case QTYPE_QSTRING
: {
723 QString
*value
= qobject_to(QString
, obj
);
724 qemu_printf("%s", qstring_get_str(value
));
728 QDict
*value
= qobject_to(QDict
, obj
);
729 dump_qdict(comp_indent
, value
);
733 QList
*value
= qobject_to(QList
, obj
);
734 dump_qlist(comp_indent
, value
);
738 QBool
*value
= qobject_to(QBool
, obj
);
739 qemu_printf("%s", qbool_get_bool(value
) ? "true" : "false");
747 static void dump_qlist(int indentation
, QList
*list
)
749 const QListEntry
*entry
;
752 for (entry
= qlist_first(list
); entry
; entry
= qlist_next(entry
), i
++) {
753 QType type
= qobject_type(entry
->value
);
754 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
755 qemu_printf("%*s[%i]:%c", indentation
* 4, "", i
,
756 composite
? '\n' : ' ');
757 dump_qobject(indentation
+ 1, entry
->value
);
764 static void dump_qdict(int indentation
, QDict
*dict
)
766 const QDictEntry
*entry
;
768 for (entry
= qdict_first(dict
); entry
; entry
= qdict_next(dict
, entry
)) {
769 QType type
= qobject_type(entry
->value
);
770 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
771 char *key
= g_malloc(strlen(entry
->key
) + 1);
774 /* replace dashes with spaces in key (variable) names */
775 for (i
= 0; entry
->key
[i
]; i
++) {
776 key
[i
] = entry
->key
[i
] == '-' ? ' ' : entry
->key
[i
];
779 qemu_printf("%*s%s:%c", indentation
* 4, "", key
,
780 composite
? '\n' : ' ');
781 dump_qobject(indentation
+ 1, entry
->value
);
789 void bdrv_image_info_specific_dump(ImageInfoSpecific
*info_spec
)
792 Visitor
*v
= qobject_output_visitor_new(&obj
);
794 visit_type_ImageInfoSpecific(v
, NULL
, &info_spec
, &error_abort
);
795 visit_complete(v
, &obj
);
796 data
= qdict_get(qobject_to(QDict
, obj
), "data");
797 dump_qobject(1, data
);
802 void bdrv_image_info_dump(ImageInfo
*info
)
804 char *size_buf
, *dsize_buf
;
805 if (!info
->has_actual_size
) {
806 dsize_buf
= g_strdup("unavailable");
808 dsize_buf
= size_to_str(info
->actual_size
);
810 size_buf
= size_to_str(info
->virtual_size
);
811 qemu_printf("image: %s\n"
813 "virtual size: %s (%" PRId64
" bytes)\n"
815 info
->filename
, info
->format
, size_buf
,
821 if (info
->has_encrypted
&& info
->encrypted
) {
822 qemu_printf("encrypted: yes\n");
825 if (info
->has_cluster_size
) {
826 qemu_printf("cluster_size: %" PRId64
"\n",
830 if (info
->has_dirty_flag
&& info
->dirty_flag
) {
831 qemu_printf("cleanly shut down: no\n");
834 if (info
->has_backing_filename
) {
835 qemu_printf("backing file: %s", info
->backing_filename
);
836 if (!info
->has_full_backing_filename
) {
837 qemu_printf(" (cannot determine actual path)");
838 } else if (strcmp(info
->backing_filename
,
839 info
->full_backing_filename
) != 0) {
840 qemu_printf(" (actual path: %s)", info
->full_backing_filename
);
843 if (info
->has_backing_filename_format
) {
844 qemu_printf("backing file format: %s\n",
845 info
->backing_filename_format
);
849 if (info
->has_snapshots
) {
850 SnapshotInfoList
*elem
;
852 qemu_printf("Snapshot list:\n");
853 bdrv_snapshot_dump(NULL
);
856 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
857 * we convert to the block layer's native QEMUSnapshotInfo for now.
859 for (elem
= info
->snapshots
; elem
; elem
= elem
->next
) {
860 QEMUSnapshotInfo sn
= {
861 .vm_state_size
= elem
->value
->vm_state_size
,
862 .date_sec
= elem
->value
->date_sec
,
863 .date_nsec
= elem
->value
->date_nsec
,
864 .vm_clock_nsec
= elem
->value
->vm_clock_sec
* 1000000000ULL +
865 elem
->value
->vm_clock_nsec
,
866 .icount
= elem
->value
->has_icount
?
867 elem
->value
->icount
: -1ULL,
870 pstrcpy(sn
.id_str
, sizeof(sn
.id_str
), elem
->value
->id
);
871 pstrcpy(sn
.name
, sizeof(sn
.name
), elem
->value
->name
);
872 bdrv_snapshot_dump(&sn
);
877 if (info
->has_format_specific
) {
878 qemu_printf("Format specific information:\n");
879 bdrv_image_info_specific_dump(info
->format_specific
);