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 25 #include "qemu/osdep.h" 26 #include "qemu/cutils.h" 27 #include "block/qapi.h" 28 #include "block/block_int.h" 29 #include "block/dirty-bitmap.h" 30 #include "block/throttle-groups.h" 31 #include "block/write-threshold.h" 32 #include "qapi/error.h" 33 #include "qapi/qapi-commands-block-core.h" 34 #include "qapi/qobject-output-visitor.h" 35 #include "qapi/qapi-visit-block-core.h" 36 #include "qapi/qmp/qbool.h" 37 #include "qapi/qmp/qdict.h" 38 #include "qapi/qmp/qlist.h" 39 #include "qapi/qmp/qnum.h" 40 #include "qapi/qmp/qstring.h" 41 #include "qemu/qemu-print.h" 42 #include "sysemu/block-backend.h" 43 #include "qemu/cutils.h" 44 45 BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, 46 BlockDriverState *bs, 47 bool flat, 48 Error **errp) 49 { 50 ImageInfo **p_image_info; 51 BlockDriverState *bs0, *backing; 52 BlockDeviceInfo *info; 53 54 if (!bs->drv) { 55 error_setg(errp, "Block device %s is ejected", bs->node_name); 56 return NULL; 57 } 58 59 bdrv_refresh_filename(bs); 60 61 info = g_malloc0(sizeof(*info)); 62 info->file = g_strdup(bs->filename); 63 info->ro = bdrv_is_read_only(bs); 64 info->drv = g_strdup(bs->drv->format_name); 65 info->encrypted = bs->encrypted; 66 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), 72 }; 73 74 if (bs->node_name[0]) { 75 info->node_name = g_strdup(bs->node_name); 76 } 77 78 backing = bdrv_cow_bs(bs); 79 if (backing) { 80 info->backing_file = g_strdup(backing->filename); 81 } 82 83 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { 84 info->has_dirty_bitmaps = true; 85 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs); 86 } 87 88 info->detect_zeroes = bs->detect_zeroes; 89 90 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) { 91 ThrottleConfig cfg; 92 BlockBackendPublic *blkp = blk_get_public(blk); 93 94 throttle_group_get_config(&blkp->throttle_group_member, &cfg); 95 96 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg; 97 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg; 98 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg; 99 100 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg; 101 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg; 102 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg; 103 104 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; 105 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; 106 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; 107 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; 108 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; 109 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; 110 111 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; 112 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; 113 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; 114 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; 115 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; 116 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; 117 118 info->has_bps_max_length = info->has_bps_max; 119 info->bps_max_length = 120 cfg.buckets[THROTTLE_BPS_TOTAL].burst_length; 121 info->has_bps_rd_max_length = info->has_bps_rd_max; 122 info->bps_rd_max_length = 123 cfg.buckets[THROTTLE_BPS_READ].burst_length; 124 info->has_bps_wr_max_length = info->has_bps_wr_max; 125 info->bps_wr_max_length = 126 cfg.buckets[THROTTLE_BPS_WRITE].burst_length; 127 128 info->has_iops_max_length = info->has_iops_max; 129 info->iops_max_length = 130 cfg.buckets[THROTTLE_OPS_TOTAL].burst_length; 131 info->has_iops_rd_max_length = info->has_iops_rd_max; 132 info->iops_rd_max_length = 133 cfg.buckets[THROTTLE_OPS_READ].burst_length; 134 info->has_iops_wr_max_length = info->has_iops_wr_max; 135 info->iops_wr_max_length = 136 cfg.buckets[THROTTLE_OPS_WRITE].burst_length; 137 138 info->has_iops_size = cfg.op_size; 139 info->iops_size = cfg.op_size; 140 141 info->group = 142 g_strdup(throttle_group_get_name(&blkp->throttle_group_member)); 143 } 144 145 info->write_threshold = bdrv_write_threshold_get(bs); 146 147 bs0 = bs; 148 p_image_info = &info->image; 149 info->backing_file_depth = 0; 150 while (1) { 151 Error *local_err = NULL; 152 bdrv_query_image_info(bs0, p_image_info, &local_err); 153 if (local_err) { 154 error_propagate(errp, local_err); 155 qapi_free_BlockDeviceInfo(info); 156 return NULL; 157 } 158 159 /* stop gathering data for flat output */ 160 if (flat) { 161 break; 162 } 163 164 if (bs0->drv && bdrv_filter_or_cow_child(bs0)) { 165 /* 166 * Put any filtered child here (for backwards compatibility to when 167 * we put bs0->backing here, which might be any filtered child). 168 */ 169 info->backing_file_depth++; 170 bs0 = bdrv_filter_or_cow_bs(bs0); 171 p_image_info = &((*p_image_info)->backing_image); 172 } else { 173 break; 174 } 175 176 /* Skip automatically inserted nodes that the user isn't aware of for 177 * query-block (blk != NULL), but not for query-named-block-nodes */ 178 if (blk) { 179 bs0 = bdrv_skip_implicit_filters(bs0); 180 } 181 } 182 183 return info; 184 } 185 186 /* 187 * Returns 0 on success, with *p_list either set to describe snapshot 188 * information, or NULL because there are no snapshots. Returns -errno on 189 * error, with *p_list untouched. 190 */ 191 int bdrv_query_snapshot_info_list(BlockDriverState *bs, 192 SnapshotInfoList **p_list, 193 Error **errp) 194 { 195 int i, sn_count; 196 QEMUSnapshotInfo *sn_tab = NULL; 197 SnapshotInfoList *head = NULL, **tail = &head; 198 SnapshotInfo *info; 199 200 sn_count = bdrv_snapshot_list(bs, &sn_tab); 201 if (sn_count < 0) { 202 const char *dev = bdrv_get_device_name(bs); 203 switch (sn_count) { 204 case -ENOMEDIUM: 205 error_setg(errp, "Device '%s' is not inserted", dev); 206 break; 207 case -ENOTSUP: 208 error_setg(errp, 209 "Device '%s' does not support internal snapshots", 210 dev); 211 break; 212 default: 213 error_setg_errno(errp, -sn_count, 214 "Can't list snapshots of device '%s'", dev); 215 break; 216 } 217 return sn_count; 218 } 219 220 for (i = 0; i < sn_count; i++) { 221 info = g_new0(SnapshotInfo, 1); 222 info->id = g_strdup(sn_tab[i].id_str); 223 info->name = g_strdup(sn_tab[i].name); 224 info->vm_state_size = sn_tab[i].vm_state_size; 225 info->date_sec = sn_tab[i].date_sec; 226 info->date_nsec = sn_tab[i].date_nsec; 227 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; 228 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; 229 info->icount = sn_tab[i].icount; 230 info->has_icount = sn_tab[i].icount != -1ULL; 231 232 QAPI_LIST_APPEND(tail, info); 233 } 234 235 g_free(sn_tab); 236 *p_list = head; 237 return 0; 238 } 239 240 /** 241 * Helper function for other query info functions. Store information about @bs 242 * in @info, setting @errp on error. 243 */ 244 static void bdrv_do_query_node_info(BlockDriverState *bs, 245 BlockNodeInfo *info, 246 Error **errp) 247 { 248 int64_t size; 249 const char *backing_filename; 250 BlockDriverInfo bdi; 251 int ret; 252 Error *err = NULL; 253 254 aio_context_acquire(bdrv_get_aio_context(bs)); 255 256 size = bdrv_getlength(bs); 257 if (size < 0) { 258 error_setg_errno(errp, -size, "Can't get image size '%s'", 259 bs->exact_filename); 260 goto out; 261 } 262 263 bdrv_refresh_filename(bs); 264 265 info->filename = g_strdup(bs->filename); 266 info->format = g_strdup(bdrv_get_format_name(bs)); 267 info->virtual_size = size; 268 info->actual_size = bdrv_get_allocated_file_size(bs); 269 info->has_actual_size = info->actual_size >= 0; 270 if (bs->encrypted) { 271 info->encrypted = true; 272 info->has_encrypted = true; 273 } 274 if (bdrv_get_info(bs, &bdi) >= 0) { 275 if (bdi.cluster_size != 0) { 276 info->cluster_size = bdi.cluster_size; 277 info->has_cluster_size = true; 278 } 279 info->dirty_flag = bdi.is_dirty; 280 info->has_dirty_flag = true; 281 } 282 info->format_specific = bdrv_get_specific_info(bs, &err); 283 if (err) { 284 error_propagate(errp, err); 285 goto out; 286 } 287 backing_filename = bs->backing_file; 288 if (backing_filename[0] != '\0') { 289 char *backing_filename2; 290 291 info->backing_filename = g_strdup(backing_filename); 292 backing_filename2 = bdrv_get_full_backing_filename(bs, NULL); 293 294 /* Always report the full_backing_filename if present, even if it's the 295 * same as backing_filename. That they are same is useful info. */ 296 if (backing_filename2) { 297 info->full_backing_filename = g_strdup(backing_filename2); 298 } 299 300 if (bs->backing_format[0]) { 301 info->backing_filename_format = g_strdup(bs->backing_format); 302 } 303 g_free(backing_filename2); 304 } 305 306 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err); 307 switch (ret) { 308 case 0: 309 if (info->snapshots) { 310 info->has_snapshots = true; 311 } 312 break; 313 /* recoverable error */ 314 case -ENOMEDIUM: 315 case -ENOTSUP: 316 error_free(err); 317 break; 318 default: 319 error_propagate(errp, err); 320 goto out; 321 } 322 323 out: 324 aio_context_release(bdrv_get_aio_context(bs)); 325 } 326 327 /** 328 * bdrv_query_block_node_info: 329 * @bs: block node to examine 330 * @p_info: location to store node information 331 * @errp: location to store error information 332 * 333 * Store image information about @bs in @p_info. 334 * 335 * @p_info will be set only on success. On error, store error in @errp. 336 */ 337 void bdrv_query_block_node_info(BlockDriverState *bs, 338 BlockNodeInfo **p_info, 339 Error **errp) 340 { 341 BlockNodeInfo *info; 342 ERRP_GUARD(); 343 344 info = g_new0(BlockNodeInfo, 1); 345 bdrv_do_query_node_info(bs, info, errp); 346 if (*errp) { 347 qapi_free_BlockNodeInfo(info); 348 return; 349 } 350 351 *p_info = info; 352 } 353 354 /** 355 * bdrv_query_image_info: 356 * @bs: block node to examine 357 * @p_info: location to store image information 358 * @errp: location to store error information 359 * 360 * Store "flat" image information in @p_info. 361 * 362 * "Flat" means it does *not* query backing image information, 363 * i.e. (*pinfo)->has_backing_image will be set to false and 364 * (*pinfo)->backing_image to NULL even when the image does in fact have 365 * a backing image. 366 * 367 * @p_info will be set only on success. On error, store error in @errp. 368 */ 369 void bdrv_query_image_info(BlockDriverState *bs, 370 ImageInfo **p_info, 371 Error **errp) 372 { 373 ImageInfo *info; 374 ERRP_GUARD(); 375 376 info = g_new0(ImageInfo, 1); 377 bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp); 378 if (*errp) { 379 qapi_free_ImageInfo(info); 380 return; 381 } 382 383 *p_info = info; 384 } 385 386 /* @p_info will be set only on success. */ 387 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, 388 Error **errp) 389 { 390 BlockInfo *info = g_malloc0(sizeof(*info)); 391 BlockDriverState *bs = blk_bs(blk); 392 char *qdev; 393 394 /* Skip automatically inserted nodes that the user isn't aware of */ 395 bs = bdrv_skip_implicit_filters(bs); 396 397 info->device = g_strdup(blk_name(blk)); 398 info->type = g_strdup("unknown"); 399 info->locked = blk_dev_is_medium_locked(blk); 400 info->removable = blk_dev_has_removable_media(blk); 401 402 qdev = blk_get_attached_dev_id(blk); 403 if (qdev && *qdev) { 404 info->qdev = qdev; 405 } else { 406 g_free(qdev); 407 } 408 409 if (blk_dev_has_tray(blk)) { 410 info->has_tray_open = true; 411 info->tray_open = blk_dev_is_tray_open(blk); 412 } 413 414 if (blk_iostatus_is_enabled(blk)) { 415 info->has_io_status = true; 416 info->io_status = blk_iostatus(blk); 417 } 418 419 if (bs && bs->drv) { 420 info->inserted = bdrv_block_device_info(blk, bs, false, errp); 421 if (info->inserted == NULL) { 422 goto err; 423 } 424 } 425 426 *p_info = info; 427 return; 428 429 err: 430 qapi_free_BlockInfo(info); 431 } 432 433 static uint64List *uint64_list(uint64_t *list, int size) 434 { 435 int i; 436 uint64List *out_list = NULL; 437 uint64List **tail = &out_list; 438 439 for (i = 0; i < size; i++) { 440 QAPI_LIST_APPEND(tail, list[i]); 441 } 442 443 return out_list; 444 } 445 446 static BlockLatencyHistogramInfo * 447 bdrv_latency_histogram_stats(BlockLatencyHistogram *hist) 448 { 449 BlockLatencyHistogramInfo *info; 450 451 if (!hist->bins) { 452 return NULL; 453 } 454 455 info = g_new0(BlockLatencyHistogramInfo, 1); 456 info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1); 457 info->bins = uint64_list(hist->bins, hist->nbins); 458 return info; 459 } 460 461 static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk) 462 { 463 BlockAcctStats *stats = blk_get_stats(blk); 464 BlockAcctTimedStats *ts = NULL; 465 BlockLatencyHistogram *hgram; 466 467 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ]; 468 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE]; 469 ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP]; 470 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ]; 471 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE]; 472 ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP]; 473 474 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ]; 475 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE]; 476 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH]; 477 ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP]; 478 479 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ]; 480 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE]; 481 ds->invalid_flush_operations = 482 stats->invalid_ops[BLOCK_ACCT_FLUSH]; 483 ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP]; 484 485 ds->rd_merged = stats->merged[BLOCK_ACCT_READ]; 486 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE]; 487 ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP]; 488 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH]; 489 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE]; 490 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ]; 491 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH]; 492 ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP]; 493 494 ds->has_idle_time_ns = stats->last_access_time_ns > 0; 495 if (ds->has_idle_time_ns) { 496 ds->idle_time_ns = block_acct_idle_time_ns(stats); 497 } 498 499 ds->account_invalid = stats->account_invalid; 500 ds->account_failed = stats->account_failed; 501 502 while ((ts = block_acct_interval_next(stats, ts))) { 503 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats)); 504 505 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ]; 506 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE]; 507 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH]; 508 509 dev_stats->interval_length = ts->interval_length; 510 511 dev_stats->min_rd_latency_ns = timed_average_min(rd); 512 dev_stats->max_rd_latency_ns = timed_average_max(rd); 513 dev_stats->avg_rd_latency_ns = timed_average_avg(rd); 514 515 dev_stats->min_wr_latency_ns = timed_average_min(wr); 516 dev_stats->max_wr_latency_ns = timed_average_max(wr); 517 dev_stats->avg_wr_latency_ns = timed_average_avg(wr); 518 519 dev_stats->min_flush_latency_ns = timed_average_min(fl); 520 dev_stats->max_flush_latency_ns = timed_average_max(fl); 521 dev_stats->avg_flush_latency_ns = timed_average_avg(fl); 522 523 dev_stats->avg_rd_queue_depth = 524 block_acct_queue_depth(ts, BLOCK_ACCT_READ); 525 dev_stats->avg_wr_queue_depth = 526 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE); 527 528 QAPI_LIST_PREPEND(ds->timed_stats, dev_stats); 529 } 530 531 hgram = stats->latency_histogram; 532 ds->rd_latency_histogram 533 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]); 534 ds->wr_latency_histogram 535 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]); 536 ds->flush_latency_histogram 537 = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]); 538 } 539 540 static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs, 541 bool blk_level) 542 { 543 BdrvChild *parent_child; 544 BlockDriverState *filter_or_cow_bs; 545 BlockStats *s = NULL; 546 547 s = g_malloc0(sizeof(*s)); 548 s->stats = g_malloc0(sizeof(*s->stats)); 549 550 if (!bs) { 551 return s; 552 } 553 554 /* Skip automatically inserted nodes that the user isn't aware of in 555 * a BlockBackend-level command. Stay at the exact node for a node-level 556 * command. */ 557 if (blk_level) { 558 bs = bdrv_skip_implicit_filters(bs); 559 } 560 561 if (bdrv_get_node_name(bs)[0]) { 562 s->node_name = g_strdup(bdrv_get_node_name(bs)); 563 } 564 565 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset); 566 567 s->driver_specific = bdrv_get_specific_stats(bs); 568 569 parent_child = bdrv_primary_child(bs); 570 if (!parent_child || 571 !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED))) 572 { 573 BdrvChild *c; 574 575 /* 576 * Look for a unique data-storing child. We do not need to look for 577 * filtered children, as there would be only one and it would have been 578 * the primary child. 579 */ 580 parent_child = NULL; 581 QLIST_FOREACH(c, &bs->children, next) { 582 if (c->role & BDRV_CHILD_DATA) { 583 if (parent_child) { 584 /* 585 * There are multiple data-storing children and we cannot 586 * choose between them. 587 */ 588 parent_child = NULL; 589 break; 590 } 591 parent_child = c; 592 } 593 } 594 } 595 if (parent_child) { 596 s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level); 597 } 598 599 filter_or_cow_bs = bdrv_filter_or_cow_bs(bs); 600 if (blk_level && filter_or_cow_bs) { 601 /* 602 * Put any filtered or COW child here (for backwards 603 * compatibility to when we put bs0->backing here, which might 604 * be either) 605 */ 606 s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level); 607 } 608 609 return s; 610 } 611 612 BlockInfoList *qmp_query_block(Error **errp) 613 { 614 BlockInfoList *head = NULL, **p_next = &head; 615 BlockBackend *blk; 616 Error *local_err = NULL; 617 618 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { 619 BlockInfoList *info; 620 621 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { 622 continue; 623 } 624 625 info = g_malloc0(sizeof(*info)); 626 bdrv_query_info(blk, &info->value, &local_err); 627 if (local_err) { 628 error_propagate(errp, local_err); 629 g_free(info); 630 qapi_free_BlockInfoList(head); 631 return NULL; 632 } 633 634 *p_next = info; 635 p_next = &info->next; 636 } 637 638 return head; 639 } 640 641 BlockStatsList *qmp_query_blockstats(bool has_query_nodes, 642 bool query_nodes, 643 Error **errp) 644 { 645 BlockStatsList *head = NULL, **tail = &head; 646 BlockBackend *blk; 647 BlockDriverState *bs; 648 649 /* Just to be safe if query_nodes is not always initialized */ 650 if (has_query_nodes && query_nodes) { 651 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) { 652 AioContext *ctx = bdrv_get_aio_context(bs); 653 654 aio_context_acquire(ctx); 655 QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false)); 656 aio_context_release(ctx); 657 } 658 } else { 659 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { 660 AioContext *ctx = blk_get_aio_context(blk); 661 BlockStats *s; 662 char *qdev; 663 664 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { 665 continue; 666 } 667 668 aio_context_acquire(ctx); 669 s = bdrv_query_bds_stats(blk_bs(blk), true); 670 s->device = g_strdup(blk_name(blk)); 671 672 qdev = blk_get_attached_dev_id(blk); 673 if (qdev && *qdev) { 674 s->qdev = qdev; 675 } else { 676 g_free(qdev); 677 } 678 679 bdrv_query_blk_stats(s->stats, blk); 680 aio_context_release(ctx); 681 682 QAPI_LIST_APPEND(tail, s); 683 } 684 } 685 686 return head; 687 } 688 689 void bdrv_snapshot_dump(QEMUSnapshotInfo *sn) 690 { 691 char clock_buf[128]; 692 char icount_buf[128] = {0}; 693 int64_t secs; 694 char *sizing = NULL; 695 696 if (!sn) { 697 qemu_printf("%-10s%-17s%8s%20s%13s%11s", 698 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT"); 699 } else { 700 g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec); 701 g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S"); 702 703 secs = sn->vm_clock_nsec / 1000000000; 704 snprintf(clock_buf, sizeof(clock_buf), 705 "%02d:%02d:%02d.%03d", 706 (int)(secs / 3600), 707 (int)((secs / 60) % 60), 708 (int)(secs % 60), 709 (int)((sn->vm_clock_nsec / 1000000) % 1000)); 710 sizing = size_to_str(sn->vm_state_size); 711 if (sn->icount != -1ULL) { 712 snprintf(icount_buf, sizeof(icount_buf), 713 "%"PRId64, sn->icount); 714 } 715 qemu_printf("%-9s %-16s %8s%20s%13s%11s", 716 sn->id_str, sn->name, 717 sizing, 718 date_buf, 719 clock_buf, 720 icount_buf); 721 } 722 g_free(sizing); 723 } 724 725 static void dump_qdict(int indentation, QDict *dict); 726 static void dump_qlist(int indentation, QList *list); 727 728 static void dump_qobject(int comp_indent, QObject *obj) 729 { 730 switch (qobject_type(obj)) { 731 case QTYPE_QNUM: { 732 QNum *value = qobject_to(QNum, obj); 733 char *tmp = qnum_to_string(value); 734 qemu_printf("%s", tmp); 735 g_free(tmp); 736 break; 737 } 738 case QTYPE_QSTRING: { 739 QString *value = qobject_to(QString, obj); 740 qemu_printf("%s", qstring_get_str(value)); 741 break; 742 } 743 case QTYPE_QDICT: { 744 QDict *value = qobject_to(QDict, obj); 745 dump_qdict(comp_indent, value); 746 break; 747 } 748 case QTYPE_QLIST: { 749 QList *value = qobject_to(QList, obj); 750 dump_qlist(comp_indent, value); 751 break; 752 } 753 case QTYPE_QBOOL: { 754 QBool *value = qobject_to(QBool, obj); 755 qemu_printf("%s", qbool_get_bool(value) ? "true" : "false"); 756 break; 757 } 758 default: 759 abort(); 760 } 761 } 762 763 static void dump_qlist(int indentation, QList *list) 764 { 765 const QListEntry *entry; 766 int i = 0; 767 768 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { 769 QType type = qobject_type(entry->value); 770 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 771 qemu_printf("%*s[%i]:%c", indentation * 4, "", i, 772 composite ? '\n' : ' '); 773 dump_qobject(indentation + 1, entry->value); 774 if (!composite) { 775 qemu_printf("\n"); 776 } 777 } 778 } 779 780 static void dump_qdict(int indentation, QDict *dict) 781 { 782 const QDictEntry *entry; 783 784 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { 785 QType type = qobject_type(entry->value); 786 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); 787 char *key = g_malloc(strlen(entry->key) + 1); 788 int i; 789 790 /* replace dashes with spaces in key (variable) names */ 791 for (i = 0; entry->key[i]; i++) { 792 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; 793 } 794 key[i] = 0; 795 qemu_printf("%*s%s:%c", indentation * 4, "", key, 796 composite ? '\n' : ' '); 797 dump_qobject(indentation + 1, entry->value); 798 if (!composite) { 799 qemu_printf("\n"); 800 } 801 g_free(key); 802 } 803 } 804 805 /* 806 * Return whether dumping the given QObject with dump_qobject() would 807 * yield an empty dump, i.e. not print anything. 808 */ 809 static bool qobject_is_empty_dump(const QObject *obj) 810 { 811 switch (qobject_type(obj)) { 812 case QTYPE_QNUM: 813 case QTYPE_QSTRING: 814 case QTYPE_QBOOL: 815 return false; 816 817 case QTYPE_QDICT: 818 return qdict_size(qobject_to(QDict, obj)) == 0; 819 820 case QTYPE_QLIST: 821 return qlist_empty(qobject_to(QList, obj)); 822 823 default: 824 abort(); 825 } 826 } 827 828 /** 829 * Dumps the given ImageInfoSpecific object in a human-readable form, 830 * prepending an optional prefix if the dump is not empty. 831 */ 832 void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec, 833 const char *prefix) 834 { 835 QObject *obj, *data; 836 Visitor *v = qobject_output_visitor_new(&obj); 837 838 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort); 839 visit_complete(v, &obj); 840 data = qdict_get(qobject_to(QDict, obj), "data"); 841 if (!qobject_is_empty_dump(data)) { 842 if (prefix) { 843 qemu_printf("%s", prefix); 844 } 845 dump_qobject(1, data); 846 } 847 qobject_unref(obj); 848 visit_free(v); 849 } 850 851 void bdrv_image_info_dump(ImageInfo *info) 852 { 853 char *size_buf, *dsize_buf; 854 if (!info->has_actual_size) { 855 dsize_buf = g_strdup("unavailable"); 856 } else { 857 dsize_buf = size_to_str(info->actual_size); 858 } 859 size_buf = size_to_str(info->virtual_size); 860 qemu_printf("image: %s\n" 861 "file format: %s\n" 862 "virtual size: %s (%" PRId64 " bytes)\n" 863 "disk size: %s\n", 864 info->filename, info->format, size_buf, 865 info->virtual_size, 866 dsize_buf); 867 g_free(size_buf); 868 g_free(dsize_buf); 869 870 if (info->has_encrypted && info->encrypted) { 871 qemu_printf("encrypted: yes\n"); 872 } 873 874 if (info->has_cluster_size) { 875 qemu_printf("cluster_size: %" PRId64 "\n", 876 info->cluster_size); 877 } 878 879 if (info->has_dirty_flag && info->dirty_flag) { 880 qemu_printf("cleanly shut down: no\n"); 881 } 882 883 if (info->backing_filename) { 884 qemu_printf("backing file: %s", info->backing_filename); 885 if (!info->full_backing_filename) { 886 qemu_printf(" (cannot determine actual path)"); 887 } else if (strcmp(info->backing_filename, 888 info->full_backing_filename) != 0) { 889 qemu_printf(" (actual path: %s)", info->full_backing_filename); 890 } 891 qemu_printf("\n"); 892 if (info->backing_filename_format) { 893 qemu_printf("backing file format: %s\n", 894 info->backing_filename_format); 895 } 896 } 897 898 if (info->has_snapshots) { 899 SnapshotInfoList *elem; 900 901 qemu_printf("Snapshot list:\n"); 902 bdrv_snapshot_dump(NULL); 903 qemu_printf("\n"); 904 905 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but 906 * we convert to the block layer's native QEMUSnapshotInfo for now. 907 */ 908 for (elem = info->snapshots; elem; elem = elem->next) { 909 QEMUSnapshotInfo sn = { 910 .vm_state_size = elem->value->vm_state_size, 911 .date_sec = elem->value->date_sec, 912 .date_nsec = elem->value->date_nsec, 913 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + 914 elem->value->vm_clock_nsec, 915 .icount = elem->value->has_icount ? 916 elem->value->icount : -1ULL, 917 }; 918 919 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); 920 pstrcpy(sn.name, sizeof(sn.name), elem->value->name); 921 bdrv_snapshot_dump(&sn); 922 qemu_printf("\n"); 923 } 924 } 925 926 if (info->format_specific) { 927 bdrv_image_info_specific_dump(info->format_specific, 928 "Format specific information:\n"); 929 } 930 } 931