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