2 * Block layer qmp and info dump related functions
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "block/qapi.h"
26 #include "block/block_int.h"
27 #include "qmp-commands.h"
28 #include "qapi-visit.h"
29 #include "qapi/qmp-output-visitor.h"
30 #include "qapi/qmp/types.h"
31 #include "sysemu/block-backend.h"
33 BlockDeviceInfo *bdrv_block_device_info(BlockDriverState *bs)
35 BlockDeviceInfo *info = g_malloc0(sizeof(*info));
37 info->file = g_strdup(bs->filename);
38 info->ro = bs->read_only;
39 info->drv = g_strdup(bs->drv->format_name);
40 info->encrypted = bs->encrypted;
41 info->encryption_key_missing = bdrv_key_required(bs);
43 if (bs->node_name[0]) {
44 info->has_node_name = true;
45 info->node_name = g_strdup(bs->node_name);
48 if (bs->backing_file[0]) {
49 info->has_backing_file = true;
50 info->backing_file = g_strdup(bs->backing_file);
53 info->backing_file_depth = bdrv_get_backing_file_depth(bs);
54 info->detect_zeroes = bs->detect_zeroes;
56 if (bs->io_limits_enabled) {
58 throttle_get_config(&bs->throttle_state, &cfg);
59 info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg;
60 info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg;
61 info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg;
63 info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg;
64 info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg;
65 info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg;
67 info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
68 info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max;
69 info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
70 info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max;
71 info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
72 info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max;
74 info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
75 info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max;
76 info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
77 info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max;
78 info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
79 info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max;
81 info->has_iops_size = cfg.op_size;
82 info->iops_size = cfg.op_size;
89 * Returns 0 on success, with *p_list either set to describe snapshot
90 * information, or NULL because there are no snapshots. Returns -errno on
91 * error, with *p_list untouched.
93 int bdrv_query_snapshot_info_list(BlockDriverState *bs,
94 SnapshotInfoList **p_list,
98 QEMUSnapshotInfo *sn_tab = NULL;
99 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
102 sn_count = bdrv_snapshot_list(bs, &sn_tab);
104 const char *dev = bdrv_get_device_name(bs);
107 error_setg(errp, "Device '%s' is not inserted", dev);
111 "Device '%s' does not support internal snapshots",
115 error_setg_errno(errp, -sn_count,
116 "Can't list snapshots of device '%s'", dev);
122 for (i = 0; i < sn_count; i++) {
123 info = g_new0(SnapshotInfo, 1);
124 info->id = g_strdup(sn_tab[i].id_str);
125 info->name = g_strdup(sn_tab[i].name);
126 info->vm_state_size = sn_tab[i].vm_state_size;
127 info->date_sec = sn_tab[i].date_sec;
128 info->date_nsec = sn_tab[i].date_nsec;
129 info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
130 info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
132 info_list = g_new0(SnapshotInfoList, 1);
133 info_list->value = info;
135 /* XXX: waiting for the qapi to support qemu-queue.h types */
137 head = cur_item = info_list;
139 cur_item->next = info_list;
140 cur_item = info_list;
151 * bdrv_query_image_info:
152 * @bs: block device to examine
153 * @p_info: location to store image information
154 * @errp: location to store error information
156 * Store "flat" image information in @p_info.
158 * "Flat" means it does *not* query backing image information,
159 * i.e. (*pinfo)->has_backing_image will be set to false and
160 * (*pinfo)->backing_image to NULL even when the image does in fact have
163 * @p_info will be set only on success. On error, store error in @errp.
165 void bdrv_query_image_info(BlockDriverState *bs,
170 const char *backing_filename;
171 char backing_filename2[1024];
177 size = bdrv_getlength(bs);
179 error_setg_errno(errp, -size, "Can't get size of device '%s'",
180 bdrv_get_device_name(bs));
184 info = g_new0(ImageInfo, 1);
185 info->filename = g_strdup(bs->filename);
186 info->format = g_strdup(bdrv_get_format_name(bs));
187 info->virtual_size = size;
188 info->actual_size = bdrv_get_allocated_file_size(bs);
189 info->has_actual_size = info->actual_size >= 0;
190 if (bdrv_is_encrypted(bs)) {
191 info->encrypted = true;
192 info->has_encrypted = true;
194 if (bdrv_get_info(bs, &bdi) >= 0) {
195 if (bdi.cluster_size != 0) {
196 info->cluster_size = bdi.cluster_size;
197 info->has_cluster_size = true;
199 info->dirty_flag = bdi.is_dirty;
200 info->has_dirty_flag = true;
202 info->format_specific = bdrv_get_specific_info(bs);
203 info->has_format_specific = info->format_specific != NULL;
205 backing_filename = bs->backing_file;
206 if (backing_filename[0] != '\0') {
207 info->backing_filename = g_strdup(backing_filename);
208 info->has_backing_filename = true;
209 bdrv_get_full_backing_filename(bs, backing_filename2,
210 sizeof(backing_filename2));
212 if (strcmp(backing_filename, backing_filename2) != 0) {
213 info->full_backing_filename =
214 g_strdup(backing_filename2);
215 info->has_full_backing_filename = true;
218 if (bs->backing_format[0]) {
219 info->backing_filename_format = g_strdup(bs->backing_format);
220 info->has_backing_filename_format = true;
224 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
227 if (info->snapshots) {
228 info->has_snapshots = true;
231 /* recoverable error */
237 error_propagate(errp, err);
238 qapi_free_ImageInfo(info);
245 /* @p_info will be set only on success. */
246 static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
249 BlockInfo *info = g_malloc0(sizeof(*info));
250 BlockDriverState *bs = blk_bs(blk);
251 BlockDriverState *bs0;
252 ImageInfo **p_image_info;
253 Error *local_err = NULL;
254 info->device = g_strdup(blk_name(blk));
255 info->type = g_strdup("unknown");
256 info->locked = blk_dev_is_medium_locked(blk);
257 info->removable = blk_dev_has_removable_media(blk);
259 if (blk_dev_has_removable_media(blk)) {
260 info->has_tray_open = true;
261 info->tray_open = blk_dev_is_tray_open(blk);
264 if (bdrv_iostatus_is_enabled(bs)) {
265 info->has_io_status = true;
266 info->io_status = bs->iostatus;
269 if (!QLIST_EMPTY(&bs->dirty_bitmaps)) {
270 info->has_dirty_bitmaps = true;
271 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
275 info->has_inserted = true;
276 info->inserted = bdrv_block_device_info(bs);
279 p_image_info = &info->inserted->image;
281 bdrv_query_image_info(bs0, p_image_info, &local_err);
283 error_propagate(errp, local_err);
286 if (bs0->drv && bs0->backing_hd) {
287 bs0 = bs0->backing_hd;
288 (*p_image_info)->has_backing_image = true;
289 p_image_info = &((*p_image_info)->backing_image);
300 qapi_free_BlockInfo(info);
303 static BlockStats *bdrv_query_stats(const BlockDriverState *bs)
307 s = g_malloc0(sizeof(*s));
309 if (bdrv_get_device_name(bs)[0]) {
310 s->has_device = true;
311 s->device = g_strdup(bdrv_get_device_name(bs));
314 if (bdrv_get_node_name(bs)[0]) {
315 s->has_node_name = true;
316 s->node_name = g_strdup(bdrv_get_node_name(bs));
319 s->stats = g_malloc0(sizeof(*s->stats));
320 s->stats->rd_bytes = bs->stats.nr_bytes[BLOCK_ACCT_READ];
321 s->stats->wr_bytes = bs->stats.nr_bytes[BLOCK_ACCT_WRITE];
322 s->stats->rd_operations = bs->stats.nr_ops[BLOCK_ACCT_READ];
323 s->stats->wr_operations = bs->stats.nr_ops[BLOCK_ACCT_WRITE];
324 s->stats->wr_highest_offset =
325 bs->stats.wr_highest_sector * BDRV_SECTOR_SIZE;
326 s->stats->flush_operations = bs->stats.nr_ops[BLOCK_ACCT_FLUSH];
327 s->stats->wr_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_WRITE];
328 s->stats->rd_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_READ];
329 s->stats->flush_total_time_ns = bs->stats.total_time_ns[BLOCK_ACCT_FLUSH];
332 s->has_parent = true;
333 s->parent = bdrv_query_stats(bs->file);
336 if (bs->backing_hd) {
337 s->has_backing = true;
338 s->backing = bdrv_query_stats(bs->backing_hd);
344 BlockInfoList *qmp_query_block(Error **errp)
346 BlockInfoList *head = NULL, **p_next = &head;
348 Error *local_err = NULL;
350 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
351 BlockInfoList *info = g_malloc0(sizeof(*info));
352 bdrv_query_info(blk, &info->value, &local_err);
354 error_propagate(errp, local_err);
359 p_next = &info->next;
365 qapi_free_BlockInfoList(head);
369 BlockStatsList *qmp_query_blockstats(Error **errp)
371 BlockStatsList *head = NULL, **p_next = &head;
372 BlockDriverState *bs = NULL;
374 while ((bs = bdrv_next(bs))) {
375 BlockStatsList *info = g_malloc0(sizeof(*info));
376 AioContext *ctx = bdrv_get_aio_context(bs);
378 aio_context_acquire(ctx);
379 info->value = bdrv_query_stats(bs);
380 aio_context_release(ctx);
383 p_next = &info->next;
389 #define NB_SUFFIXES 4
391 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
393 static const char suffixes[NB_SUFFIXES] = "KMGT";
398 snprintf(buf, buf_size, "%" PRId64, size);
401 for (i = 0; i < NB_SUFFIXES; i++) {
402 if (size < (10 * base)) {
403 snprintf(buf, buf_size, "%0.1f%c",
407 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
408 snprintf(buf, buf_size, "%" PRId64 "%c",
409 ((size + (base >> 1)) / base),
419 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
420 QEMUSnapshotInfo *sn)
422 char buf1[128], date_buf[128], clock_buf[128];
429 "%-10s%-20s%7s%20s%15s",
430 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
433 localtime_r(&ti, &tm);
434 strftime(date_buf, sizeof(date_buf),
435 "%Y-%m-%d %H:%M:%S", &tm);
436 secs = sn->vm_clock_nsec / 1000000000;
437 snprintf(clock_buf, sizeof(clock_buf),
438 "%02d:%02d:%02d.%03d",
440 (int)((secs / 60) % 60),
442 (int)((sn->vm_clock_nsec / 1000000) % 1000));
444 "%-10s%-20s%7s%20s%15s",
445 sn->id_str, sn->name,
446 get_human_readable_size(buf1, sizeof(buf1),
453 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
455 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
458 static void dump_qobject(fprintf_function func_fprintf, void *f,
459 int comp_indent, QObject *obj)
461 switch (qobject_type(obj)) {
463 QInt *value = qobject_to_qint(obj);
464 func_fprintf(f, "%" PRId64, qint_get_int(value));
467 case QTYPE_QSTRING: {
468 QString *value = qobject_to_qstring(obj);
469 func_fprintf(f, "%s", qstring_get_str(value));
473 QDict *value = qobject_to_qdict(obj);
474 dump_qdict(func_fprintf, f, comp_indent, value);
478 QList *value = qobject_to_qlist(obj);
479 dump_qlist(func_fprintf, f, comp_indent, value);
483 QFloat *value = qobject_to_qfloat(obj);
484 func_fprintf(f, "%g", qfloat_get_double(value));
488 QBool *value = qobject_to_qbool(obj);
489 func_fprintf(f, "%s", qbool_get_int(value) ? "true" : "false");
493 QString *value = qerror_human((QError *)obj);
494 func_fprintf(f, "%s", qstring_get_str(value));
506 static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
509 const QListEntry *entry;
512 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
513 qtype_code type = qobject_type(entry->value);
514 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
515 const char *format = composite ? "%*s[%i]:\n" : "%*s[%i]: ";
517 func_fprintf(f, format, indentation * 4, "", i);
518 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
520 func_fprintf(f, "\n");
525 static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
528 const QDictEntry *entry;
530 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
531 qtype_code type = qobject_type(entry->value);
532 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
533 const char *format = composite ? "%*s%s:\n" : "%*s%s: ";
534 char key[strlen(entry->key) + 1];
537 /* replace dashes with spaces in key (variable) names */
538 for (i = 0; entry->key[i]; i++) {
539 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
543 func_fprintf(f, format, indentation * 4, "", key);
544 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
546 func_fprintf(f, "\n");
551 void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
552 ImageInfoSpecific *info_spec)
554 QmpOutputVisitor *ov = qmp_output_visitor_new();
557 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov), &info_spec, NULL,
559 obj = qmp_output_get_qobject(ov);
560 assert(qobject_type(obj) == QTYPE_QDICT);
561 data = qdict_get(qobject_to_qdict(obj), "data");
562 dump_qobject(func_fprintf, f, 1, data);
563 qmp_output_visitor_cleanup(ov);
566 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
569 char size_buf[128], dsize_buf[128];
570 if (!info->has_actual_size) {
571 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
573 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
576 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
580 "virtual size: %s (%" PRId64 " bytes)\n"
582 info->filename, info->format, size_buf,
586 if (info->has_encrypted && info->encrypted) {
587 func_fprintf(f, "encrypted: yes\n");
590 if (info->has_cluster_size) {
591 func_fprintf(f, "cluster_size: %" PRId64 "\n",
595 if (info->has_dirty_flag && info->dirty_flag) {
596 func_fprintf(f, "cleanly shut down: no\n");
599 if (info->has_backing_filename) {
600 func_fprintf(f, "backing file: %s", info->backing_filename);
601 if (info->has_full_backing_filename) {
602 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
604 func_fprintf(f, "\n");
605 if (info->has_backing_filename_format) {
606 func_fprintf(f, "backing file format: %s\n",
607 info->backing_filename_format);
611 if (info->has_snapshots) {
612 SnapshotInfoList *elem;
614 func_fprintf(f, "Snapshot list:\n");
615 bdrv_snapshot_dump(func_fprintf, f, NULL);
616 func_fprintf(f, "\n");
618 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
619 * we convert to the block layer's native QEMUSnapshotInfo for now.
621 for (elem = info->snapshots; elem; elem = elem->next) {
622 QEMUSnapshotInfo sn = {
623 .vm_state_size = elem->value->vm_state_size,
624 .date_sec = elem->value->date_sec,
625 .date_nsec = elem->value->date_nsec,
626 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
627 elem->value->vm_clock_nsec,
630 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
631 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
632 bdrv_snapshot_dump(func_fprintf, f, &sn);
633 func_fprintf(f, "\n");
637 if (info->has_format_specific) {
638 func_fprintf(f, "Format specific information:\n");
639 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);