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 "block/qapi.h"
27 #include "block/block_int.h"
28 #include "block/throttle-groups.h"
29 #include "block/write-threshold.h"
30 #include "qapi/error.h"
31 #include "qapi/qapi-commands-block-core.h"
32 #include "qapi/qobject-output-visitor.h"
33 #include "qapi/qapi-visit-block-core.h"
34 #include "qapi/qmp/qbool.h"
35 #include "qapi/qmp/qdict.h"
36 #include "qapi/qmp/qlist.h"
37 #include "qapi/qmp/qnum.h"
38 #include "qapi/qmp/qstring.h"
39 #include "sysemu/block-backend.h"
40 #include "qemu/cutils.h"
42 BlockDeviceInfo
*bdrv_block_device_info(BlockBackend
*blk
,
43 BlockDriverState
*bs
, Error
**errp
)
45 ImageInfo
**p_image_info
;
46 BlockDriverState
*bs0
;
47 BlockDeviceInfo
*info
;
50 error_setg(errp
, "Block device %s is ejected", bs
->node_name
);
54 info
= g_malloc0(sizeof(*info
));
55 info
->file
= g_strdup(bs
->filename
);
56 info
->ro
= bs
->read_only
;
57 info
->drv
= g_strdup(bs
->drv
->format_name
);
58 info
->encrypted
= bs
->encrypted
;
59 info
->encryption_key_missing
= false;
61 info
->cache
= g_new(BlockdevCacheInfo
, 1);
62 *info
->cache
= (BlockdevCacheInfo
) {
63 .writeback
= blk
? blk_enable_write_cache(blk
) : true,
64 .direct
= !!(bs
->open_flags
& BDRV_O_NOCACHE
),
65 .no_flush
= !!(bs
->open_flags
& BDRV_O_NO_FLUSH
),
68 if (bs
->node_name
[0]) {
69 info
->has_node_name
= true;
70 info
->node_name
= g_strdup(bs
->node_name
);
73 if (bs
->backing_file
[0]) {
74 info
->has_backing_file
= true;
75 info
->backing_file
= g_strdup(bs
->backing_file
);
78 info
->detect_zeroes
= bs
->detect_zeroes
;
80 if (blk
&& blk_get_public(blk
)->throttle_group_member
.throttle_state
) {
82 BlockBackendPublic
*blkp
= blk_get_public(blk
);
84 throttle_group_get_config(&blkp
->throttle_group_member
, &cfg
);
86 info
->bps
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
;
87 info
->bps_rd
= cfg
.buckets
[THROTTLE_BPS_READ
].avg
;
88 info
->bps_wr
= cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
;
90 info
->iops
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
;
91 info
->iops_rd
= cfg
.buckets
[THROTTLE_OPS_READ
].avg
;
92 info
->iops_wr
= cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
;
94 info
->has_bps_max
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
95 info
->bps_max
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
96 info
->has_bps_rd_max
= cfg
.buckets
[THROTTLE_BPS_READ
].max
;
97 info
->bps_rd_max
= cfg
.buckets
[THROTTLE_BPS_READ
].max
;
98 info
->has_bps_wr_max
= cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
99 info
->bps_wr_max
= cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
101 info
->has_iops_max
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
102 info
->iops_max
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
103 info
->has_iops_rd_max
= cfg
.buckets
[THROTTLE_OPS_READ
].max
;
104 info
->iops_rd_max
= cfg
.buckets
[THROTTLE_OPS_READ
].max
;
105 info
->has_iops_wr_max
= cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
106 info
->iops_wr_max
= cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
108 info
->has_bps_max_length
= info
->has_bps_max
;
109 info
->bps_max_length
=
110 cfg
.buckets
[THROTTLE_BPS_TOTAL
].burst_length
;
111 info
->has_bps_rd_max_length
= info
->has_bps_rd_max
;
112 info
->bps_rd_max_length
=
113 cfg
.buckets
[THROTTLE_BPS_READ
].burst_length
;
114 info
->has_bps_wr_max_length
= info
->has_bps_wr_max
;
115 info
->bps_wr_max_length
=
116 cfg
.buckets
[THROTTLE_BPS_WRITE
].burst_length
;
118 info
->has_iops_max_length
= info
->has_iops_max
;
119 info
->iops_max_length
=
120 cfg
.buckets
[THROTTLE_OPS_TOTAL
].burst_length
;
121 info
->has_iops_rd_max_length
= info
->has_iops_rd_max
;
122 info
->iops_rd_max_length
=
123 cfg
.buckets
[THROTTLE_OPS_READ
].burst_length
;
124 info
->has_iops_wr_max_length
= info
->has_iops_wr_max
;
125 info
->iops_wr_max_length
=
126 cfg
.buckets
[THROTTLE_OPS_WRITE
].burst_length
;
128 info
->has_iops_size
= cfg
.op_size
;
129 info
->iops_size
= cfg
.op_size
;
131 info
->has_group
= true;
133 g_strdup(throttle_group_get_name(&blkp
->throttle_group_member
));
136 info
->write_threshold
= bdrv_write_threshold_get(bs
);
139 p_image_info
= &info
->image
;
140 info
->backing_file_depth
= 0;
142 Error
*local_err
= NULL
;
143 bdrv_query_image_info(bs0
, p_image_info
, &local_err
);
145 error_propagate(errp
, local_err
);
146 qapi_free_BlockDeviceInfo(info
);
150 if (bs0
->drv
&& bs0
->backing
) {
151 info
->backing_file_depth
++;
152 bs0
= bs0
->backing
->bs
;
153 (*p_image_info
)->has_backing_image
= true;
154 p_image_info
= &((*p_image_info
)->backing_image
);
159 /* Skip automatically inserted nodes that the user isn't aware of for
160 * query-block (blk != NULL), but not for query-named-block-nodes */
161 while (blk
&& bs0
->drv
&& bs0
->implicit
) {
162 bs0
= backing_bs(bs0
);
171 * Returns 0 on success, with *p_list either set to describe snapshot
172 * information, or NULL because there are no snapshots. Returns -errno on
173 * error, with *p_list untouched.
175 int bdrv_query_snapshot_info_list(BlockDriverState
*bs
,
176 SnapshotInfoList
**p_list
,
180 QEMUSnapshotInfo
*sn_tab
= NULL
;
181 SnapshotInfoList
*info_list
, *cur_item
= NULL
, *head
= NULL
;
184 sn_count
= bdrv_snapshot_list(bs
, &sn_tab
);
186 const char *dev
= bdrv_get_device_name(bs
);
189 error_setg(errp
, "Device '%s' is not inserted", dev
);
193 "Device '%s' does not support internal snapshots",
197 error_setg_errno(errp
, -sn_count
,
198 "Can't list snapshots of device '%s'", dev
);
204 for (i
= 0; i
< sn_count
; i
++) {
205 info
= g_new0(SnapshotInfo
, 1);
206 info
->id
= g_strdup(sn_tab
[i
].id_str
);
207 info
->name
= g_strdup(sn_tab
[i
].name
);
208 info
->vm_state_size
= sn_tab
[i
].vm_state_size
;
209 info
->date_sec
= sn_tab
[i
].date_sec
;
210 info
->date_nsec
= sn_tab
[i
].date_nsec
;
211 info
->vm_clock_sec
= sn_tab
[i
].vm_clock_nsec
/ 1000000000;
212 info
->vm_clock_nsec
= sn_tab
[i
].vm_clock_nsec
% 1000000000;
214 info_list
= g_new0(SnapshotInfoList
, 1);
215 info_list
->value
= info
;
217 /* XXX: waiting for the qapi to support qemu-queue.h types */
219 head
= cur_item
= info_list
;
221 cur_item
->next
= info_list
;
222 cur_item
= info_list
;
233 * bdrv_query_image_info:
234 * @bs: block device to examine
235 * @p_info: location to store image information
236 * @errp: location to store error information
238 * Store "flat" image information in @p_info.
240 * "Flat" means it does *not* query backing image information,
241 * i.e. (*pinfo)->has_backing_image will be set to false and
242 * (*pinfo)->backing_image to NULL even when the image does in fact have
245 * @p_info will be set only on success. On error, store error in @errp.
247 void bdrv_query_image_info(BlockDriverState
*bs
,
252 const char *backing_filename
;
258 aio_context_acquire(bdrv_get_aio_context(bs
));
260 size
= bdrv_getlength(bs
);
262 error_setg_errno(errp
, -size
, "Can't get image size '%s'",
267 info
= g_new0(ImageInfo
, 1);
268 info
->filename
= g_strdup(bs
->filename
);
269 info
->format
= g_strdup(bdrv_get_format_name(bs
));
270 info
->virtual_size
= size
;
271 info
->actual_size
= bdrv_get_allocated_file_size(bs
);
272 info
->has_actual_size
= info
->actual_size
>= 0;
273 if (bdrv_is_encrypted(bs
)) {
274 info
->encrypted
= true;
275 info
->has_encrypted
= true;
277 if (bdrv_get_info(bs
, &bdi
) >= 0) {
278 if (bdi
.cluster_size
!= 0) {
279 info
->cluster_size
= bdi
.cluster_size
;
280 info
->has_cluster_size
= true;
282 info
->dirty_flag
= bdi
.is_dirty
;
283 info
->has_dirty_flag
= true;
285 info
->format_specific
= bdrv_get_specific_info(bs
);
286 info
->has_format_specific
= info
->format_specific
!= NULL
;
288 backing_filename
= bs
->backing_file
;
289 if (backing_filename
[0] != '\0') {
290 char *backing_filename2
= g_malloc0(PATH_MAX
);
291 info
->backing_filename
= g_strdup(backing_filename
);
292 info
->has_backing_filename
= true;
293 bdrv_get_full_backing_filename(bs
, backing_filename2
, PATH_MAX
, &err
);
295 /* Can't reconstruct the full backing filename, so we must omit
296 * this field and apply a Best Effort to this query. */
297 g_free(backing_filename2
);
298 backing_filename2
= NULL
;
303 /* Always report the full_backing_filename if present, even if it's the
304 * same as backing_filename. That they are same is useful info. */
305 if (backing_filename2
) {
306 info
->full_backing_filename
= g_strdup(backing_filename2
);
307 info
->has_full_backing_filename
= true;
310 if (bs
->backing_format
[0]) {
311 info
->backing_filename_format
= g_strdup(bs
->backing_format
);
312 info
->has_backing_filename_format
= true;
314 g_free(backing_filename2
);
317 ret
= bdrv_query_snapshot_info_list(bs
, &info
->snapshots
, &err
);
320 if (info
->snapshots
) {
321 info
->has_snapshots
= true;
324 /* recoverable error */
330 error_propagate(errp
, err
);
331 qapi_free_ImageInfo(info
);
338 aio_context_release(bdrv_get_aio_context(bs
));
341 /* @p_info will be set only on success. */
342 static void bdrv_query_info(BlockBackend
*blk
, BlockInfo
**p_info
,
345 BlockInfo
*info
= g_malloc0(sizeof(*info
));
346 BlockDriverState
*bs
= blk_bs(blk
);
349 /* Skip automatically inserted nodes that the user isn't aware of */
350 while (bs
&& bs
->drv
&& bs
->implicit
) {
354 info
->device
= g_strdup(blk_name(blk
));
355 info
->type
= g_strdup("unknown");
356 info
->locked
= blk_dev_is_medium_locked(blk
);
357 info
->removable
= blk_dev_has_removable_media(blk
);
359 qdev
= blk_get_attached_dev_id(blk
);
361 info
->has_qdev
= true;
367 if (blk_dev_has_tray(blk
)) {
368 info
->has_tray_open
= true;
369 info
->tray_open
= blk_dev_is_tray_open(blk
);
372 if (blk_iostatus_is_enabled(blk
)) {
373 info
->has_io_status
= true;
374 info
->io_status
= blk_iostatus(blk
);
377 if (bs
&& !QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
378 info
->has_dirty_bitmaps
= true;
379 info
->dirty_bitmaps
= bdrv_query_dirty_bitmaps(bs
);
383 info
->has_inserted
= true;
384 info
->inserted
= bdrv_block_device_info(blk
, bs
, errp
);
385 if (info
->inserted
== NULL
) {
394 qapi_free_BlockInfo(info
);
397 static uint64List
*uint64_list(uint64_t *list
, int size
)
400 uint64List
*out_list
= NULL
;
401 uint64List
**pout_list
= &out_list
;
403 for (i
= 0; i
< size
; i
++) {
404 uint64List
*entry
= g_new(uint64List
, 1);
405 entry
->value
= list
[i
];
407 pout_list
= &entry
->next
;
415 static void bdrv_latency_histogram_stats(BlockLatencyHistogram
*hist
,
417 BlockLatencyHistogramInfo
**info
)
419 *not_null
= hist
->bins
!= NULL
;
421 *info
= g_new0(BlockLatencyHistogramInfo
, 1);
423 (*info
)->boundaries
= uint64_list(hist
->boundaries
, hist
->nbins
- 1);
424 (*info
)->bins
= uint64_list(hist
->bins
, hist
->nbins
);
428 static void bdrv_query_blk_stats(BlockDeviceStats
*ds
, BlockBackend
*blk
)
430 BlockAcctStats
*stats
= blk_get_stats(blk
);
431 BlockAcctTimedStats
*ts
= NULL
;
433 ds
->rd_bytes
= stats
->nr_bytes
[BLOCK_ACCT_READ
];
434 ds
->wr_bytes
= stats
->nr_bytes
[BLOCK_ACCT_WRITE
];
435 ds
->rd_operations
= stats
->nr_ops
[BLOCK_ACCT_READ
];
436 ds
->wr_operations
= stats
->nr_ops
[BLOCK_ACCT_WRITE
];
438 ds
->failed_rd_operations
= stats
->failed_ops
[BLOCK_ACCT_READ
];
439 ds
->failed_wr_operations
= stats
->failed_ops
[BLOCK_ACCT_WRITE
];
440 ds
->failed_flush_operations
= stats
->failed_ops
[BLOCK_ACCT_FLUSH
];
442 ds
->invalid_rd_operations
= stats
->invalid_ops
[BLOCK_ACCT_READ
];
443 ds
->invalid_wr_operations
= stats
->invalid_ops
[BLOCK_ACCT_WRITE
];
444 ds
->invalid_flush_operations
=
445 stats
->invalid_ops
[BLOCK_ACCT_FLUSH
];
447 ds
->rd_merged
= stats
->merged
[BLOCK_ACCT_READ
];
448 ds
->wr_merged
= stats
->merged
[BLOCK_ACCT_WRITE
];
449 ds
->flush_operations
= stats
->nr_ops
[BLOCK_ACCT_FLUSH
];
450 ds
->wr_total_time_ns
= stats
->total_time_ns
[BLOCK_ACCT_WRITE
];
451 ds
->rd_total_time_ns
= stats
->total_time_ns
[BLOCK_ACCT_READ
];
452 ds
->flush_total_time_ns
= stats
->total_time_ns
[BLOCK_ACCT_FLUSH
];
454 ds
->has_idle_time_ns
= stats
->last_access_time_ns
> 0;
455 if (ds
->has_idle_time_ns
) {
456 ds
->idle_time_ns
= block_acct_idle_time_ns(stats
);
459 ds
->account_invalid
= stats
->account_invalid
;
460 ds
->account_failed
= stats
->account_failed
;
462 while ((ts
= block_acct_interval_next(stats
, ts
))) {
463 BlockDeviceTimedStatsList
*timed_stats
=
464 g_malloc0(sizeof(*timed_stats
));
465 BlockDeviceTimedStats
*dev_stats
= g_malloc0(sizeof(*dev_stats
));
466 timed_stats
->next
= ds
->timed_stats
;
467 timed_stats
->value
= dev_stats
;
468 ds
->timed_stats
= timed_stats
;
470 TimedAverage
*rd
= &ts
->latency
[BLOCK_ACCT_READ
];
471 TimedAverage
*wr
= &ts
->latency
[BLOCK_ACCT_WRITE
];
472 TimedAverage
*fl
= &ts
->latency
[BLOCK_ACCT_FLUSH
];
474 dev_stats
->interval_length
= ts
->interval_length
;
476 dev_stats
->min_rd_latency_ns
= timed_average_min(rd
);
477 dev_stats
->max_rd_latency_ns
= timed_average_max(rd
);
478 dev_stats
->avg_rd_latency_ns
= timed_average_avg(rd
);
480 dev_stats
->min_wr_latency_ns
= timed_average_min(wr
);
481 dev_stats
->max_wr_latency_ns
= timed_average_max(wr
);
482 dev_stats
->avg_wr_latency_ns
= timed_average_avg(wr
);
484 dev_stats
->min_flush_latency_ns
= timed_average_min(fl
);
485 dev_stats
->max_flush_latency_ns
= timed_average_max(fl
);
486 dev_stats
->avg_flush_latency_ns
= timed_average_avg(fl
);
488 dev_stats
->avg_rd_queue_depth
=
489 block_acct_queue_depth(ts
, BLOCK_ACCT_READ
);
490 dev_stats
->avg_wr_queue_depth
=
491 block_acct_queue_depth(ts
, BLOCK_ACCT_WRITE
);
494 bdrv_latency_histogram_stats(&stats
->latency_histogram
[BLOCK_ACCT_READ
],
495 &ds
->has_x_rd_latency_histogram
,
496 &ds
->x_rd_latency_histogram
);
497 bdrv_latency_histogram_stats(&stats
->latency_histogram
[BLOCK_ACCT_WRITE
],
498 &ds
->has_x_wr_latency_histogram
,
499 &ds
->x_wr_latency_histogram
);
500 bdrv_latency_histogram_stats(&stats
->latency_histogram
[BLOCK_ACCT_FLUSH
],
501 &ds
->has_x_flush_latency_histogram
,
502 &ds
->x_flush_latency_histogram
);
505 static BlockStats
*bdrv_query_bds_stats(BlockDriverState
*bs
,
508 BlockStats
*s
= NULL
;
510 s
= g_malloc0(sizeof(*s
));
511 s
->stats
= g_malloc0(sizeof(*s
->stats
));
517 /* Skip automatically inserted nodes that the user isn't aware of in
518 * a BlockBackend-level command. Stay at the exact node for a node-level
520 while (blk_level
&& bs
->drv
&& bs
->implicit
) {
525 if (bdrv_get_node_name(bs
)[0]) {
526 s
->has_node_name
= true;
527 s
->node_name
= g_strdup(bdrv_get_node_name(bs
));
530 s
->stats
->wr_highest_offset
= stat64_get(&bs
->wr_highest_offset
);
533 s
->has_parent
= true;
534 s
->parent
= bdrv_query_bds_stats(bs
->file
->bs
, blk_level
);
537 if (blk_level
&& bs
->backing
) {
538 s
->has_backing
= true;
539 s
->backing
= bdrv_query_bds_stats(bs
->backing
->bs
, blk_level
);
545 BlockInfoList
*qmp_query_block(Error
**errp
)
547 BlockInfoList
*head
= NULL
, **p_next
= &head
;
549 Error
*local_err
= NULL
;
551 for (blk
= blk_all_next(NULL
); blk
; blk
= blk_all_next(blk
)) {
554 if (!*blk_name(blk
) && !blk_get_attached_dev(blk
)) {
558 info
= g_malloc0(sizeof(*info
));
559 bdrv_query_info(blk
, &info
->value
, &local_err
);
561 error_propagate(errp
, local_err
);
563 qapi_free_BlockInfoList(head
);
568 p_next
= &info
->next
;
574 BlockStatsList
*qmp_query_blockstats(bool has_query_nodes
,
578 BlockStatsList
*head
= NULL
, **p_next
= &head
;
580 BlockDriverState
*bs
;
582 /* Just to be safe if query_nodes is not always initialized */
583 if (has_query_nodes
&& query_nodes
) {
584 for (bs
= bdrv_next_node(NULL
); bs
; bs
= bdrv_next_node(bs
)) {
585 BlockStatsList
*info
= g_malloc0(sizeof(*info
));
586 AioContext
*ctx
= bdrv_get_aio_context(bs
);
588 aio_context_acquire(ctx
);
589 info
->value
= bdrv_query_bds_stats(bs
, false);
590 aio_context_release(ctx
);
593 p_next
= &info
->next
;
596 for (blk
= blk_all_next(NULL
); blk
; blk
= blk_all_next(blk
)) {
597 BlockStatsList
*info
= g_malloc0(sizeof(*info
));
598 AioContext
*ctx
= blk_get_aio_context(blk
);
602 if (!*blk_name(blk
) && !blk_get_attached_dev(blk
)) {
606 aio_context_acquire(ctx
);
607 s
= bdrv_query_bds_stats(blk_bs(blk
), true);
608 s
->has_device
= true;
609 s
->device
= g_strdup(blk_name(blk
));
611 qdev
= blk_get_attached_dev_id(blk
);
619 bdrv_query_blk_stats(s
->stats
, blk
);
620 aio_context_release(ctx
);
624 p_next
= &info
->next
;
631 #define NB_SUFFIXES 4
633 static char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
635 static const char suffixes
[NB_SUFFIXES
] = {'K', 'M', 'G', 'T'};
640 snprintf(buf
, buf_size
, "%" PRId64
, size
);
643 for (i
= 0; i
< NB_SUFFIXES
; i
++) {
644 if (size
< (10 * base
)) {
645 snprintf(buf
, buf_size
, "%0.1f%c",
649 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
650 snprintf(buf
, buf_size
, "%" PRId64
"%c",
651 ((size
+ (base
>> 1)) / base
),
661 void bdrv_snapshot_dump(fprintf_function func_fprintf
, void *f
,
662 QEMUSnapshotInfo
*sn
)
664 char buf1
[128], date_buf
[128], clock_buf
[128];
671 "%-10s%-20s%7s%20s%15s",
672 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
675 localtime_r(&ti
, &tm
);
676 strftime(date_buf
, sizeof(date_buf
),
677 "%Y-%m-%d %H:%M:%S", &tm
);
678 secs
= sn
->vm_clock_nsec
/ 1000000000;
679 snprintf(clock_buf
, sizeof(clock_buf
),
680 "%02d:%02d:%02d.%03d",
682 (int)((secs
/ 60) % 60),
684 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
686 "%-10s%-20s%7s%20s%15s",
687 sn
->id_str
, sn
->name
,
688 get_human_readable_size(buf1
, sizeof(buf1
),
695 static void dump_qdict(fprintf_function func_fprintf
, void *f
, int indentation
,
697 static void dump_qlist(fprintf_function func_fprintf
, void *f
, int indentation
,
700 static void dump_qobject(fprintf_function func_fprintf
, void *f
,
701 int comp_indent
, QObject
*obj
)
703 switch (qobject_type(obj
)) {
705 QNum
*value
= qobject_to(QNum
, obj
);
706 char *tmp
= qnum_to_string(value
);
707 func_fprintf(f
, "%s", tmp
);
711 case QTYPE_QSTRING
: {
712 QString
*value
= qobject_to(QString
, obj
);
713 func_fprintf(f
, "%s", qstring_get_str(value
));
717 QDict
*value
= qobject_to(QDict
, obj
);
718 dump_qdict(func_fprintf
, f
, comp_indent
, value
);
722 QList
*value
= qobject_to(QList
, obj
);
723 dump_qlist(func_fprintf
, f
, comp_indent
, value
);
727 QBool
*value
= qobject_to(QBool
, obj
);
728 func_fprintf(f
, "%s", qbool_get_bool(value
) ? "true" : "false");
736 static void dump_qlist(fprintf_function func_fprintf
, void *f
, int indentation
,
739 const QListEntry
*entry
;
742 for (entry
= qlist_first(list
); entry
; entry
= qlist_next(entry
), i
++) {
743 QType type
= qobject_type(entry
->value
);
744 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
745 func_fprintf(f
, "%*s[%i]:%c", indentation
* 4, "", i
,
746 composite
? '\n' : ' ');
747 dump_qobject(func_fprintf
, f
, indentation
+ 1, entry
->value
);
749 func_fprintf(f
, "\n");
754 static void dump_qdict(fprintf_function func_fprintf
, void *f
, int indentation
,
757 const QDictEntry
*entry
;
759 for (entry
= qdict_first(dict
); entry
; entry
= qdict_next(dict
, entry
)) {
760 QType type
= qobject_type(entry
->value
);
761 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
762 char *key
= g_malloc(strlen(entry
->key
) + 1);
765 /* replace dashes with spaces in key (variable) names */
766 for (i
= 0; entry
->key
[i
]; i
++) {
767 key
[i
] = entry
->key
[i
] == '-' ? ' ' : entry
->key
[i
];
770 func_fprintf(f
, "%*s%s:%c", indentation
* 4, "", key
,
771 composite
? '\n' : ' ');
772 dump_qobject(func_fprintf
, f
, indentation
+ 1, entry
->value
);
774 func_fprintf(f
, "\n");
780 void bdrv_image_info_specific_dump(fprintf_function func_fprintf
, void *f
,
781 ImageInfoSpecific
*info_spec
)
784 Visitor
*v
= qobject_output_visitor_new(&obj
);
786 visit_type_ImageInfoSpecific(v
, NULL
, &info_spec
, &error_abort
);
787 visit_complete(v
, &obj
);
788 data
= qdict_get(qobject_to(QDict
, obj
), "data");
789 dump_qobject(func_fprintf
, f
, 1, data
);
794 void bdrv_image_info_dump(fprintf_function func_fprintf
, void *f
,
797 char size_buf
[128], dsize_buf
[128];
798 if (!info
->has_actual_size
) {
799 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
801 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
804 get_human_readable_size(size_buf
, sizeof(size_buf
), info
->virtual_size
);
808 "virtual size: %s (%" PRId64
" bytes)\n"
810 info
->filename
, info
->format
, size_buf
,
814 if (info
->has_encrypted
&& info
->encrypted
) {
815 func_fprintf(f
, "encrypted: yes\n");
818 if (info
->has_cluster_size
) {
819 func_fprintf(f
, "cluster_size: %" PRId64
"\n",
823 if (info
->has_dirty_flag
&& info
->dirty_flag
) {
824 func_fprintf(f
, "cleanly shut down: no\n");
827 if (info
->has_backing_filename
) {
828 func_fprintf(f
, "backing file: %s", info
->backing_filename
);
829 if (!info
->has_full_backing_filename
) {
830 func_fprintf(f
, " (cannot determine actual path)");
831 } else if (strcmp(info
->backing_filename
,
832 info
->full_backing_filename
) != 0) {
833 func_fprintf(f
, " (actual path: %s)", info
->full_backing_filename
);
835 func_fprintf(f
, "\n");
836 if (info
->has_backing_filename_format
) {
837 func_fprintf(f
, "backing file format: %s\n",
838 info
->backing_filename_format
);
842 if (info
->has_snapshots
) {
843 SnapshotInfoList
*elem
;
845 func_fprintf(f
, "Snapshot list:\n");
846 bdrv_snapshot_dump(func_fprintf
, f
, NULL
);
847 func_fprintf(f
, "\n");
849 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
850 * we convert to the block layer's native QEMUSnapshotInfo for now.
852 for (elem
= info
->snapshots
; elem
; elem
= elem
->next
) {
853 QEMUSnapshotInfo sn
= {
854 .vm_state_size
= elem
->value
->vm_state_size
,
855 .date_sec
= elem
->value
->date_sec
,
856 .date_nsec
= elem
->value
->date_nsec
,
857 .vm_clock_nsec
= elem
->value
->vm_clock_sec
* 1000000000ULL +
858 elem
->value
->vm_clock_nsec
,
861 pstrcpy(sn
.id_str
, sizeof(sn
.id_str
), elem
->value
->id
);
862 pstrcpy(sn
.name
, sizeof(sn
.name
), elem
->value
->name
);
863 bdrv_snapshot_dump(func_fprintf
, f
, &sn
);
864 func_fprintf(f
, "\n");
868 if (info
->has_format_specific
) {
869 func_fprintf(f
, "Format specific information:\n");
870 bdrv_image_info_specific_dump(func_fprintf
, f
, info
->format_specific
);