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