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