]> git.proxmox.com Git - mirror_qemu.git/blame - block/qapi.c
block/qapi: Factor out bdrv_query_blk_stats()
[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"
f364ec65
WX
26#include "block/qapi.h"
27#include "block/block_int.h"
76f4afb4 28#include "block/throttle-groups.h"
e2462113 29#include "block/write-threshold.h"
f364ec65 30#include "qmp-commands.h"
a8d8ecb7
HR
31#include "qapi-visit.h"
32#include "qapi/qmp-output-visitor.h"
33#include "qapi/qmp/types.h"
d829a211 34#include "sysemu/block-backend.h"
f364ec65 35
d5a8ee60 36BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs, Error **errp)
c13163fb 37{
d5a8ee60
AG
38 ImageInfo **p_image_info;
39 BlockDriverState *bs0;
c13163fb
BC
40 BlockDeviceInfo *info = g_malloc0(sizeof(*info));
41
42 info->file = g_strdup(bs->filename);
43 info->ro = bs->read_only;
44 info->drv = g_strdup(bs->drv->format_name);
45 info->encrypted = bs->encrypted;
46 info->encryption_key_missing = bdrv_key_required(bs);
47
9e193c5a
KW
48 info->cache = g_new(BlockdevCacheInfo, 1);
49 *info->cache = (BlockdevCacheInfo) {
50 .writeback = bdrv_enable_write_cache(bs),
51 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
52 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
53 };
54
c13163fb
BC
55 if (bs->node_name[0]) {
56 info->has_node_name = true;
57 info->node_name = g_strdup(bs->node_name);
58 }
59
60 if (bs->backing_file[0]) {
61 info->has_backing_file = true;
62 info->backing_file = g_strdup(bs->backing_file);
63 }
64
65 info->backing_file_depth = bdrv_get_backing_file_depth(bs);
465bee1d 66 info->detect_zeroes = bs->detect_zeroes;
c13163fb 67
a0d64a61 68 if (bs->throttle_state) {
c13163fb 69 ThrottleConfig cfg;
76f4afb4
AG
70
71 throttle_group_get_config(bs, &cfg);
72
c13163fb
BC
73 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
74 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
75 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
76
77 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
78 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
79 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
80
81 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
82 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
83 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
84 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
85 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
86 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
87
88 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
89 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
90 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
91 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
92 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
93 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
94
398befdf
AG
95 info->has_bps_max_length = info->has_bps_max;
96 info->bps_max_length =
97 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length;
98 info->has_bps_rd_max_length = info->has_bps_rd_max;
99 info->bps_rd_max_length =
100 cfg.buckets[THROTTLE_BPS_READ].burst_length;
101 info->has_bps_wr_max_length = info->has_bps_wr_max;
102 info->bps_wr_max_length =
103 cfg.buckets[THROTTLE_BPS_WRITE].burst_length;
104
105 info->has_iops_max_length = info->has_iops_max;
106 info->iops_max_length =
107 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length;
108 info->has_iops_rd_max_length = info->has_iops_rd_max;
109 info->iops_rd_max_length =
110 cfg.buckets[THROTTLE_OPS_READ].burst_length;
111 info->has_iops_wr_max_length = info->has_iops_wr_max;
112 info->iops_wr_max_length =
113 cfg.buckets[THROTTLE_OPS_WRITE].burst_length;
114
c13163fb
BC
115 info->has_iops_size = cfg.op_size;
116 info->iops_size = cfg.op_size;
b8fe1694
AG
117
118 info->has_group = true;
119 info->group = g_strdup(throttle_group_get_name(bs));
c13163fb
BC
120 }
121
e2462113
FR
122 info->write_threshold = bdrv_write_threshold_get(bs);
123
d5a8ee60
AG
124 bs0 = bs;
125 p_image_info = &info->image;
126 while (1) {
127 Error *local_err = NULL;
128 bdrv_query_image_info(bs0, p_image_info, &local_err);
129 if (local_err) {
130 error_propagate(errp, local_err);
131 qapi_free_BlockDeviceInfo(info);
132 return NULL;
133 }
760e0063
KW
134 if (bs0->drv && bs0->backing) {
135 bs0 = bs0->backing->bs;
d5a8ee60
AG
136 (*p_image_info)->has_backing_image = true;
137 p_image_info = &((*p_image_info)->backing_image);
138 } else {
139 break;
140 }
141 }
142
c13163fb
BC
143 return info;
144}
145
fb0ed453
WX
146/*
147 * Returns 0 on success, with *p_list either set to describe snapshot
148 * information, or NULL because there are no snapshots. Returns -errno on
149 * error, with *p_list untouched.
150 */
151int bdrv_query_snapshot_info_list(BlockDriverState *bs,
152 SnapshotInfoList **p_list,
153 Error **errp)
f364ec65
WX
154{
155 int i, sn_count;
156 QEMUSnapshotInfo *sn_tab = NULL;
fb0ed453
WX
157 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
158 SnapshotInfo *info;
159
f364ec65 160 sn_count = bdrv_snapshot_list(bs, &sn_tab);
fb0ed453
WX
161 if (sn_count < 0) {
162 const char *dev = bdrv_get_device_name(bs);
163 switch (sn_count) {
164 case -ENOMEDIUM:
165 error_setg(errp, "Device '%s' is not inserted", dev);
166 break;
167 case -ENOTSUP:
168 error_setg(errp,
169 "Device '%s' does not support internal snapshots",
170 dev);
171 break;
172 default:
173 error_setg_errno(errp, -sn_count,
174 "Can't list snapshots of device '%s'", dev);
175 break;
176 }
177 return sn_count;
178 }
f364ec65
WX
179
180 for (i = 0; i < sn_count; i++) {
fb0ed453
WX
181 info = g_new0(SnapshotInfo, 1);
182 info->id = g_strdup(sn_tab[i].id_str);
183 info->name = g_strdup(sn_tab[i].name);
184 info->vm_state_size = sn_tab[i].vm_state_size;
185 info->date_sec = sn_tab[i].date_sec;
186 info->date_nsec = sn_tab[i].date_nsec;
187 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
188 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
f364ec65 189
fb0ed453
WX
190 info_list = g_new0(SnapshotInfoList, 1);
191 info_list->value = info;
f364ec65
WX
192
193 /* XXX: waiting for the qapi to support qemu-queue.h types */
194 if (!cur_item) {
fb0ed453 195 head = cur_item = info_list;
f364ec65
WX
196 } else {
197 cur_item->next = info_list;
198 cur_item = info_list;
199 }
200
201 }
202
203 g_free(sn_tab);
fb0ed453
WX
204 *p_list = head;
205 return 0;
f364ec65
WX
206}
207
43526ec8
WX
208/**
209 * bdrv_query_image_info:
210 * @bs: block device to examine
211 * @p_info: location to store image information
212 * @errp: location to store error information
213 *
553a7e87
WX
214 * Store "flat" image information in @p_info.
215 *
216 * "Flat" means it does *not* query backing image information,
217 * i.e. (*pinfo)->has_backing_image will be set to false and
218 * (*pinfo)->backing_image to NULL even when the image does in fact have
219 * a backing image.
220 *
43526ec8
WX
221 * @p_info will be set only on success. On error, store error in @errp.
222 */
223void bdrv_query_image_info(BlockDriverState *bs,
224 ImageInfo **p_info,
225 Error **errp)
f364ec65 226{
52bf1e72 227 int64_t size;
43526ec8 228 const char *backing_filename;
f364ec65 229 BlockDriverInfo bdi;
43526ec8
WX
230 int ret;
231 Error *err = NULL;
52bf1e72 232 ImageInfo *info;
f364ec65 233
1963f8d5
PB
234 aio_context_acquire(bdrv_get_aio_context(bs));
235
52bf1e72
MA
236 size = bdrv_getlength(bs);
237 if (size < 0) {
238 error_setg_errno(errp, -size, "Can't get size of device '%s'",
239 bdrv_get_device_name(bs));
1963f8d5 240 goto out;
52bf1e72 241 }
f364ec65 242
52bf1e72 243 info = g_new0(ImageInfo, 1);
43526ec8 244 info->filename = g_strdup(bs->filename);
f364ec65 245 info->format = g_strdup(bdrv_get_format_name(bs));
52bf1e72 246 info->virtual_size = size;
f364ec65
WX
247 info->actual_size = bdrv_get_allocated_file_size(bs);
248 info->has_actual_size = info->actual_size >= 0;
249 if (bdrv_is_encrypted(bs)) {
250 info->encrypted = true;
251 info->has_encrypted = true;
252 }
253 if (bdrv_get_info(bs, &bdi) >= 0) {
254 if (bdi.cluster_size != 0) {
255 info->cluster_size = bdi.cluster_size;
256 info->has_cluster_size = true;
257 }
258 info->dirty_flag = bdi.is_dirty;
259 info->has_dirty_flag = true;
260 }
eae041fe
HR
261 info->format_specific = bdrv_get_specific_info(bs);
262 info->has_format_specific = info->format_specific != NULL;
263
43526ec8 264 backing_filename = bs->backing_file;
f364ec65 265 if (backing_filename[0] != '\0') {
9a29e18f 266 char *backing_filename2 = g_malloc0(PATH_MAX);
f364ec65
WX
267 info->backing_filename = g_strdup(backing_filename);
268 info->has_backing_filename = true;
9a29e18f 269 bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
9f07429e 270 if (err) {
a5002d53
JS
271 /* Can't reconstruct the full backing filename, so we must omit
272 * this field and apply a Best Effort to this query. */
564d64bd 273 g_free(backing_filename2);
a5002d53
JS
274 backing_filename2 = NULL;
275 error_free(err);
0fa296eb 276 err = NULL;
9f07429e 277 }
f364ec65 278
12dcb1c0
JS
279 /* Always report the full_backing_filename if present, even if it's the
280 * same as backing_filename. That they are same is useful info. */
281 if (backing_filename2) {
282 info->full_backing_filename = g_strdup(backing_filename2);
f364ec65
WX
283 info->has_full_backing_filename = true;
284 }
285
286 if (bs->backing_format[0]) {
287 info->backing_filename_format = g_strdup(bs->backing_format);
288 info->has_backing_filename_format = true;
289 }
564d64bd 290 g_free(backing_filename2);
f364ec65 291 }
43526ec8
WX
292
293 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
294 switch (ret) {
295 case 0:
296 if (info->snapshots) {
297 info->has_snapshots = true;
298 }
299 break;
300 /* recoverable error */
301 case -ENOMEDIUM:
302 case -ENOTSUP:
303 error_free(err);
304 break;
305 default:
306 error_propagate(errp, err);
307 qapi_free_ImageInfo(info);
1963f8d5 308 goto out;
43526ec8
WX
309 }
310
311 *p_info = info;
1963f8d5
PB
312
313out:
314 aio_context_release(bdrv_get_aio_context(bs));
f364ec65
WX
315}
316
553a7e87 317/* @p_info will be set only on success. */
d829a211
MA
318static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
319 Error **errp)
f364ec65
WX
320{
321 BlockInfo *info = g_malloc0(sizeof(*info));
d829a211 322 BlockDriverState *bs = blk_bs(blk);
d829a211 323 info->device = g_strdup(blk_name(blk));
f364ec65 324 info->type = g_strdup("unknown");
a7f53e26
MA
325 info->locked = blk_dev_is_medium_locked(blk);
326 info->removable = blk_dev_has_removable_media(blk);
f364ec65 327
327032ce 328 if (blk_dev_has_tray(blk)) {
f364ec65 329 info->has_tray_open = true;
a7f53e26 330 info->tray_open = blk_dev_is_tray_open(blk);
f364ec65
WX
331 }
332
373340b2 333 if (blk_iostatus_is_enabled(blk)) {
f364ec65 334 info->has_io_status = true;
373340b2 335 info->io_status = blk_iostatus(blk);
f364ec65
WX
336 }
337
5433c24f 338 if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
21b56835
FZ
339 info->has_dirty_bitmaps = true;
340 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
341 }
342
5433c24f 343 if (bs && bs->drv) {
f364ec65 344 info->has_inserted = true;
d5a8ee60
AG
345 info->inserted = bdrv_block_device_info(bs, errp);
346 if (info->inserted == NULL) {
347 goto err;
553a7e87 348 }
f364ec65 349 }
553a7e87
WX
350
351 *p_info = info;
352 return;
353
354 err:
355 qapi_free_BlockInfo(info);
f364ec65
WX
356}
357
2b77e60a
KW
358static void bdrv_query_blk_stats(BlockStats *s, BlockBackend *blk)
359{
360 BlockAcctStats *stats = blk_get_stats(blk);
361 BlockAcctTimedStats *ts = NULL;
362
363 s->stats->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
364 s->stats->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
365 s->stats->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
366 s->stats->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
367
368 s->stats->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
369 s->stats->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
370 s->stats->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
371
372 s->stats->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
373 s->stats->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
374 s->stats->invalid_flush_operations =
375 stats->invalid_ops[BLOCK_ACCT_FLUSH];
376
377 s->stats->rd_merged = stats->merged[BLOCK_ACCT_READ];
378 s->stats->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
379 s->stats->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
380 s->stats->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
381 s->stats->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
382 s->stats->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
383
384 s->stats->has_idle_time_ns = stats->last_access_time_ns > 0;
385 if (s->stats->has_idle_time_ns) {
386 s->stats->idle_time_ns = block_acct_idle_time_ns(stats);
387 }
388
389 s->stats->account_invalid = stats->account_invalid;
390 s->stats->account_failed = stats->account_failed;
391
392 while ((ts = block_acct_interval_next(stats, ts))) {
393 BlockDeviceTimedStatsList *timed_stats =
394 g_malloc0(sizeof(*timed_stats));
395 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
396 timed_stats->next = s->stats->timed_stats;
397 timed_stats->value = dev_stats;
398 s->stats->timed_stats = timed_stats;
399
400 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
401 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
402 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
403
404 dev_stats->interval_length = ts->interval_length;
405
406 dev_stats->min_rd_latency_ns = timed_average_min(rd);
407 dev_stats->max_rd_latency_ns = timed_average_max(rd);
408 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
409
410 dev_stats->min_wr_latency_ns = timed_average_min(wr);
411 dev_stats->max_wr_latency_ns = timed_average_max(wr);
412 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
413
414 dev_stats->min_flush_latency_ns = timed_average_min(fl);
415 dev_stats->max_flush_latency_ns = timed_average_max(fl);
416 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
417
418 dev_stats->avg_rd_queue_depth =
419 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
420 dev_stats->avg_wr_queue_depth =
421 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
422 }
423}
424
f71eaa74
FZ
425static BlockStats *bdrv_query_stats(const BlockDriverState *bs,
426 bool query_backing)
f364ec65
WX
427{
428 BlockStats *s;
429
430 s = g_malloc0(sizeof(*s));
431
bfb197e0 432 if (bdrv_get_device_name(bs)[0]) {
f364ec65 433 s->has_device = true;
bfb197e0 434 s->device = g_strdup(bdrv_get_device_name(bs));
f364ec65
WX
435 }
436
4875a779
FZ
437 if (bdrv_get_node_name(bs)[0]) {
438 s->has_node_name = true;
439 s->node_name = g_strdup(bdrv_get_node_name(bs));
440 }
441
f364ec65 442 s->stats = g_malloc0(sizeof(*s->stats));
7f0e9da6 443 if (bs->blk) {
2b77e60a 444 bdrv_query_blk_stats(s, bs->blk);
7f0e9da6 445 }
f364ec65 446
53d8f9d8
HR
447 s->stats->wr_highest_offset = bs->wr_highest_offset;
448
f364ec65
WX
449 if (bs->file) {
450 s->has_parent = true;
9a4f4c31 451 s->parent = bdrv_query_stats(bs->file->bs, query_backing);
f364ec65
WX
452 }
453
760e0063 454 if (query_backing && bs->backing) {
c8059b97 455 s->has_backing = true;
760e0063 456 s->backing = bdrv_query_stats(bs->backing->bs, query_backing);
c8059b97
FZ
457 }
458
f364ec65
WX
459 return s;
460}
461
462BlockInfoList *qmp_query_block(Error **errp)
463{
464 BlockInfoList *head = NULL, **p_next = &head;
d829a211 465 BlockBackend *blk;
553a7e87 466 Error *local_err = NULL;
f364ec65 467
d829a211 468 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
f364ec65 469 BlockInfoList *info = g_malloc0(sizeof(*info));
d829a211 470 bdrv_query_info(blk, &info->value, &local_err);
84d18f06 471 if (local_err) {
553a7e87 472 error_propagate(errp, local_err);
903c341d
MA
473 g_free(info);
474 qapi_free_BlockInfoList(head);
475 return NULL;
553a7e87 476 }
f364ec65
WX
477
478 *p_next = info;
479 p_next = &info->next;
480 }
481
482 return head;
483}
484
f71eaa74
FZ
485BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
486 bool query_nodes,
487 Error **errp)
f364ec65
WX
488{
489 BlockStatsList *head = NULL, **p_next = &head;
490 BlockDriverState *bs = NULL;
491
f71eaa74
FZ
492 /* Just to be safe if query_nodes is not always initialized */
493 query_nodes = has_query_nodes && query_nodes;
494
495 while ((bs = query_nodes ? bdrv_next_node(bs) : bdrv_next(bs))) {
f364ec65 496 BlockStatsList *info = g_malloc0(sizeof(*info));
13344f3a
SH
497 AioContext *ctx = bdrv_get_aio_context(bs);
498
499 aio_context_acquire(ctx);
f71eaa74 500 info->value = bdrv_query_stats(bs, !query_nodes);
13344f3a 501 aio_context_release(ctx);
f364ec65
WX
502
503 *p_next = info;
504 p_next = &info->next;
505 }
506
507 return head;
508}
509
510#define NB_SUFFIXES 4
511
512static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
513{
2c20fa2c 514 static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
f364ec65
WX
515 int64_t base;
516 int i;
517
518 if (size <= 999) {
519 snprintf(buf, buf_size, "%" PRId64, size);
520 } else {
521 base = 1024;
522 for (i = 0; i < NB_SUFFIXES; i++) {
523 if (size < (10 * base)) {
524 snprintf(buf, buf_size, "%0.1f%c",
525 (double)size / base,
526 suffixes[i]);
527 break;
528 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
529 snprintf(buf, buf_size, "%" PRId64 "%c",
530 ((size + (base >> 1)) / base),
531 suffixes[i]);
532 break;
533 }
534 base = base * 1024;
535 }
536 }
537 return buf;
538}
539
5b917044
WX
540void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
541 QEMUSnapshotInfo *sn)
f364ec65
WX
542{
543 char buf1[128], date_buf[128], clock_buf[128];
544 struct tm tm;
545 time_t ti;
546 int64_t secs;
547
548 if (!sn) {
5b917044
WX
549 func_fprintf(f,
550 "%-10s%-20s%7s%20s%15s",
551 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
f364ec65
WX
552 } else {
553 ti = sn->date_sec;
554 localtime_r(&ti, &tm);
555 strftime(date_buf, sizeof(date_buf),
556 "%Y-%m-%d %H:%M:%S", &tm);
557 secs = sn->vm_clock_nsec / 1000000000;
558 snprintf(clock_buf, sizeof(clock_buf),
559 "%02d:%02d:%02d.%03d",
560 (int)(secs / 3600),
561 (int)((secs / 60) % 60),
562 (int)(secs % 60),
563 (int)((sn->vm_clock_nsec / 1000000) % 1000));
5b917044
WX
564 func_fprintf(f,
565 "%-10s%-20s%7s%20s%15s",
566 sn->id_str, sn->name,
567 get_human_readable_size(buf1, sizeof(buf1),
568 sn->vm_state_size),
569 date_buf,
570 clock_buf);
f364ec65 571 }
f364ec65
WX
572}
573
a8d8ecb7
HR
574static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
575 QDict *dict);
576static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
577 QList *list);
578
579static void dump_qobject(fprintf_function func_fprintf, void *f,
580 int comp_indent, QObject *obj)
581{
582 switch (qobject_type(obj)) {
583 case QTYPE_QINT: {
584 QInt *value = qobject_to_qint(obj);
585 func_fprintf(f, "%" PRId64, qint_get_int(value));
586 break;
587 }
588 case QTYPE_QSTRING: {
589 QString *value = qobject_to_qstring(obj);
590 func_fprintf(f, "%s", qstring_get_str(value));
591 break;
592 }
593 case QTYPE_QDICT: {
594 QDict *value = qobject_to_qdict(obj);
595 dump_qdict(func_fprintf, f, comp_indent, value);
596 break;
597 }
598 case QTYPE_QLIST: {
599 QList *value = qobject_to_qlist(obj);
600 dump_qlist(func_fprintf, f, comp_indent, value);
601 break;
602 }
603 case QTYPE_QFLOAT: {
604 QFloat *value = qobject_to_qfloat(obj);
605 func_fprintf(f, "%g", qfloat_get_double(value));
606 break;
607 }
608 case QTYPE_QBOOL: {
609 QBool *value = qobject_to_qbool(obj);
fc48ffc3 610 func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
a8d8ecb7
HR
611 break;
612 }
a8d8ecb7
HR
613 default:
614 abort();
615 }
616}
617
618static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
619 QList *list)
620{
621 const QListEntry *entry;
622 int i = 0;
623
624 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
1310a3d3 625 QType type = qobject_type(entry->value);
a8d8ecb7
HR
626 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
627 const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
628
629 func_fprintf(f, format, indentation * 4, "", i);
630 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
631 if (!composite) {
632 func_fprintf(f, "\n");
633 }
634 }
635}
636
637static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
638 QDict *dict)
639{
640 const QDictEntry *entry;
641
642 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
1310a3d3 643 QType type = qobject_type(entry->value);
a8d8ecb7
HR
644 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
645 const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
646 char key[strlen(entry->key) + 1];
647 int i;
648
649 /* replace dashes with spaces in key (variable) names */
650 for (i = 0; entry->key[i]; i++) {
651 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
652 }
653 key[i] = 0;
654
655 func_fprintf(f, format, indentation * 4, "", key);
656 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
657 if (!composite) {
658 func_fprintf(f, "\n");
659 }
660 }
661}
662
663void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
664 ImageInfoSpecific *info_spec)
665{
a8d8ecb7
HR
666 QmpOutputVisitor *ov = qmp_output_visitor_new();
667 QObject *obj, *data;
668
51e72bc1 669 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), NULL, &info_spec,
35d0d40a 670 &error_abort);
a8d8ecb7
HR
671 obj = qmp_output_get_qobject(ov);
672 assert(qobject_type(obj) == QTYPE_QDICT);
673 data = qdict_get(qobject_to_qdict(obj), "data");
674 dump_qobject(func_fprintf, f, 1, data);
675 qmp_output_visitor_cleanup(ov);
676}
677
5b917044
WX
678void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
679 ImageInfo *info)
f364ec65
WX
680{
681 char size_buf[128], dsize_buf[128];
682 if (!info->has_actual_size) {
683 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
684 } else {
685 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
686 info->actual_size);
687 }
688 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
5b917044
WX
689 func_fprintf(f,
690 "image: %s\n"
691 "file format: %s\n"
692 "virtual size: %s (%" PRId64 " bytes)\n"
693 "disk size: %s\n",
694 info->filename, info->format, size_buf,
695 info->virtual_size,
696 dsize_buf);
f364ec65
WX
697
698 if (info->has_encrypted && info->encrypted) {
5b917044 699 func_fprintf(f, "encrypted: yes\n");
f364ec65
WX
700 }
701
702 if (info->has_cluster_size) {
5b917044
WX
703 func_fprintf(f, "cluster_size: %" PRId64 "\n",
704 info->cluster_size);
f364ec65
WX
705 }
706
707 if (info->has_dirty_flag && info->dirty_flag) {
5b917044 708 func_fprintf(f, "cleanly shut down: no\n");
f364ec65
WX
709 }
710
711 if (info->has_backing_filename) {
5b917044 712 func_fprintf(f, "backing file: %s", info->backing_filename);
5c9d9ca5
JS
713 if (!info->has_full_backing_filename) {
714 func_fprintf(f, " (cannot determine actual path)");
715 } else if (strcmp(info->backing_filename,
716 info->full_backing_filename) != 0) {
5b917044 717 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
f364ec65 718 }
5b917044 719 func_fprintf(f, "\n");
f364ec65 720 if (info->has_backing_filename_format) {
5b917044
WX
721 func_fprintf(f, "backing file format: %s\n",
722 info->backing_filename_format);
f364ec65
WX
723 }
724 }
725
726 if (info->has_snapshots) {
727 SnapshotInfoList *elem;
f364ec65 728
5b917044
WX
729 func_fprintf(f, "Snapshot list:\n");
730 bdrv_snapshot_dump(func_fprintf, f, NULL);
731 func_fprintf(f, "\n");
f364ec65
WX
732
733 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
734 * we convert to the block layer's native QEMUSnapshotInfo for now.
735 */
736 for (elem = info->snapshots; elem; elem = elem->next) {
737 QEMUSnapshotInfo sn = {
738 .vm_state_size = elem->value->vm_state_size,
739 .date_sec = elem->value->date_sec,
740 .date_nsec = elem->value->date_nsec,
741 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
742 elem->value->vm_clock_nsec,
743 };
744
745 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
746 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
5b917044
WX
747 bdrv_snapshot_dump(func_fprintf, f, &sn);
748 func_fprintf(f, "\n");
f364ec65
WX
749 }
750 }
a8d8ecb7
HR
751
752 if (info->has_format_specific) {
753 func_fprintf(f, "Format specific information:\n");
754 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
755 }
f364ec65 756}