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"
32 BlockDeviceInfo
*bdrv_block_device_info(BlockDriverState
*bs
)
34 BlockDeviceInfo
*info
= g_malloc0(sizeof(*info
));
36 info
->file
= g_strdup(bs
->filename
);
37 info
->ro
= bs
->read_only
;
38 info
->drv
= g_strdup(bs
->drv
->format_name
);
39 info
->encrypted
= bs
->encrypted
;
40 info
->encryption_key_missing
= bdrv_key_required(bs
);
42 if (bs
->node_name
[0]) {
43 info
->has_node_name
= true;
44 info
->node_name
= g_strdup(bs
->node_name
);
47 if (bs
->backing_file
[0]) {
48 info
->has_backing_file
= true;
49 info
->backing_file
= g_strdup(bs
->backing_file
);
52 info
->backing_file_depth
= bdrv_get_backing_file_depth(bs
);
53 info
->detect_zeroes
= bs
->detect_zeroes
;
55 if (bs
->io_limits_enabled
) {
57 throttle_get_config(&bs
->throttle_state
, &cfg
);
58 info
->bps
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].avg
;
59 info
->bps_rd
= cfg
.buckets
[THROTTLE_BPS_READ
].avg
;
60 info
->bps_wr
= cfg
.buckets
[THROTTLE_BPS_WRITE
].avg
;
62 info
->iops
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].avg
;
63 info
->iops_rd
= cfg
.buckets
[THROTTLE_OPS_READ
].avg
;
64 info
->iops_wr
= cfg
.buckets
[THROTTLE_OPS_WRITE
].avg
;
66 info
->has_bps_max
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
67 info
->bps_max
= cfg
.buckets
[THROTTLE_BPS_TOTAL
].max
;
68 info
->has_bps_rd_max
= cfg
.buckets
[THROTTLE_BPS_READ
].max
;
69 info
->bps_rd_max
= cfg
.buckets
[THROTTLE_BPS_READ
].max
;
70 info
->has_bps_wr_max
= cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
71 info
->bps_wr_max
= cfg
.buckets
[THROTTLE_BPS_WRITE
].max
;
73 info
->has_iops_max
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
74 info
->iops_max
= cfg
.buckets
[THROTTLE_OPS_TOTAL
].max
;
75 info
->has_iops_rd_max
= cfg
.buckets
[THROTTLE_OPS_READ
].max
;
76 info
->iops_rd_max
= cfg
.buckets
[THROTTLE_OPS_READ
].max
;
77 info
->has_iops_wr_max
= cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
78 info
->iops_wr_max
= cfg
.buckets
[THROTTLE_OPS_WRITE
].max
;
80 info
->has_iops_size
= cfg
.op_size
;
81 info
->iops_size
= cfg
.op_size
;
88 * Returns 0 on success, with *p_list either set to describe snapshot
89 * information, or NULL because there are no snapshots. Returns -errno on
90 * error, with *p_list untouched.
92 int bdrv_query_snapshot_info_list(BlockDriverState
*bs
,
93 SnapshotInfoList
**p_list
,
97 QEMUSnapshotInfo
*sn_tab
= NULL
;
98 SnapshotInfoList
*info_list
, *cur_item
= NULL
, *head
= NULL
;
101 sn_count
= bdrv_snapshot_list(bs
, &sn_tab
);
103 const char *dev
= bdrv_get_device_name(bs
);
106 error_setg(errp
, "Device '%s' is not inserted", dev
);
110 "Device '%s' does not support internal snapshots",
114 error_setg_errno(errp
, -sn_count
,
115 "Can't list snapshots of device '%s'", dev
);
121 for (i
= 0; i
< sn_count
; i
++) {
122 info
= g_new0(SnapshotInfo
, 1);
123 info
->id
= g_strdup(sn_tab
[i
].id_str
);
124 info
->name
= g_strdup(sn_tab
[i
].name
);
125 info
->vm_state_size
= sn_tab
[i
].vm_state_size
;
126 info
->date_sec
= sn_tab
[i
].date_sec
;
127 info
->date_nsec
= sn_tab
[i
].date_nsec
;
128 info
->vm_clock_sec
= sn_tab
[i
].vm_clock_nsec
/ 1000000000;
129 info
->vm_clock_nsec
= sn_tab
[i
].vm_clock_nsec
% 1000000000;
131 info_list
= g_new0(SnapshotInfoList
, 1);
132 info_list
->value
= info
;
134 /* XXX: waiting for the qapi to support qemu-queue.h types */
136 head
= cur_item
= info_list
;
138 cur_item
->next
= info_list
;
139 cur_item
= info_list
;
150 * bdrv_query_image_info:
151 * @bs: block device to examine
152 * @p_info: location to store image information
153 * @errp: location to store error information
155 * Store "flat" image information in @p_info.
157 * "Flat" means it does *not* query backing image information,
158 * i.e. (*pinfo)->has_backing_image will be set to false and
159 * (*pinfo)->backing_image to NULL even when the image does in fact have
162 * @p_info will be set only on success. On error, store error in @errp.
164 void bdrv_query_image_info(BlockDriverState
*bs
,
168 uint64_t total_sectors
;
169 const char *backing_filename
;
170 char backing_filename2
[1024];
174 ImageInfo
*info
= g_new0(ImageInfo
, 1);
176 bdrv_get_geometry(bs
, &total_sectors
);
178 info
->filename
= g_strdup(bs
->filename
);
179 info
->format
= g_strdup(bdrv_get_format_name(bs
));
180 info
->virtual_size
= total_sectors
* 512;
181 info
->actual_size
= bdrv_get_allocated_file_size(bs
);
182 info
->has_actual_size
= info
->actual_size
>= 0;
183 if (bdrv_is_encrypted(bs
)) {
184 info
->encrypted
= true;
185 info
->has_encrypted
= true;
187 if (bdrv_get_info(bs
, &bdi
) >= 0) {
188 if (bdi
.cluster_size
!= 0) {
189 info
->cluster_size
= bdi
.cluster_size
;
190 info
->has_cluster_size
= true;
192 info
->dirty_flag
= bdi
.is_dirty
;
193 info
->has_dirty_flag
= true;
195 info
->format_specific
= bdrv_get_specific_info(bs
);
196 info
->has_format_specific
= info
->format_specific
!= NULL
;
198 backing_filename
= bs
->backing_file
;
199 if (backing_filename
[0] != '\0') {
200 info
->backing_filename
= g_strdup(backing_filename
);
201 info
->has_backing_filename
= true;
202 bdrv_get_full_backing_filename(bs
, backing_filename2
,
203 sizeof(backing_filename2
));
205 if (strcmp(backing_filename
, backing_filename2
) != 0) {
206 info
->full_backing_filename
=
207 g_strdup(backing_filename2
);
208 info
->has_full_backing_filename
= true;
211 if (bs
->backing_format
[0]) {
212 info
->backing_filename_format
= g_strdup(bs
->backing_format
);
213 info
->has_backing_filename_format
= true;
217 ret
= bdrv_query_snapshot_info_list(bs
, &info
->snapshots
, &err
);
220 if (info
->snapshots
) {
221 info
->has_snapshots
= true;
224 /* recoverable error */
230 error_propagate(errp
, err
);
231 qapi_free_ImageInfo(info
);
238 /* @p_info will be set only on success. */
239 void bdrv_query_info(BlockDriverState
*bs
,
243 BlockInfo
*info
= g_malloc0(sizeof(*info
));
244 BlockDriverState
*bs0
;
245 ImageInfo
**p_image_info
;
246 Error
*local_err
= NULL
;
247 info
->device
= g_strdup(bs
->device_name
);
248 info
->type
= g_strdup("unknown");
249 info
->locked
= bdrv_dev_is_medium_locked(bs
);
250 info
->removable
= bdrv_dev_has_removable_media(bs
);
252 if (bdrv_dev_has_removable_media(bs
)) {
253 info
->has_tray_open
= true;
254 info
->tray_open
= bdrv_dev_is_tray_open(bs
);
257 if (bdrv_iostatus_is_enabled(bs
)) {
258 info
->has_io_status
= true;
259 info
->io_status
= bs
->iostatus
;
262 if (!QLIST_EMPTY(&bs
->dirty_bitmaps
)) {
263 info
->has_dirty_bitmaps
= true;
264 info
->dirty_bitmaps
= bdrv_query_dirty_bitmaps(bs
);
268 info
->has_inserted
= true;
269 info
->inserted
= bdrv_block_device_info(bs
);
272 p_image_info
= &info
->inserted
->image
;
274 bdrv_query_image_info(bs0
, p_image_info
, &local_err
);
276 error_propagate(errp
, local_err
);
279 if (bs0
->drv
&& bs0
->backing_hd
) {
280 bs0
= bs0
->backing_hd
;
281 (*p_image_info
)->has_backing_image
= true;
282 p_image_info
= &((*p_image_info
)->backing_image
);
293 qapi_free_BlockInfo(info
);
296 static BlockStats
*bdrv_query_stats(const BlockDriverState
*bs
)
300 s
= g_malloc0(sizeof(*s
));
302 if (bs
->device_name
[0]) {
303 s
->has_device
= true;
304 s
->device
= g_strdup(bs
->device_name
);
307 s
->stats
= g_malloc0(sizeof(*s
->stats
));
308 s
->stats
->rd_bytes
= bs
->nr_bytes
[BDRV_ACCT_READ
];
309 s
->stats
->wr_bytes
= bs
->nr_bytes
[BDRV_ACCT_WRITE
];
310 s
->stats
->rd_operations
= bs
->nr_ops
[BDRV_ACCT_READ
];
311 s
->stats
->wr_operations
= bs
->nr_ops
[BDRV_ACCT_WRITE
];
312 s
->stats
->wr_highest_offset
= bs
->wr_highest_sector
* BDRV_SECTOR_SIZE
;
313 s
->stats
->flush_operations
= bs
->nr_ops
[BDRV_ACCT_FLUSH
];
314 s
->stats
->wr_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_WRITE
];
315 s
->stats
->rd_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_READ
];
316 s
->stats
->flush_total_time_ns
= bs
->total_time_ns
[BDRV_ACCT_FLUSH
];
319 s
->has_parent
= true;
320 s
->parent
= bdrv_query_stats(bs
->file
);
323 if (bs
->backing_hd
) {
324 s
->has_backing
= true;
325 s
->backing
= bdrv_query_stats(bs
->backing_hd
);
331 BlockInfoList
*qmp_query_block(Error
**errp
)
333 BlockInfoList
*head
= NULL
, **p_next
= &head
;
334 BlockDriverState
*bs
= NULL
;
335 Error
*local_err
= NULL
;
337 while ((bs
= bdrv_next(bs
))) {
338 BlockInfoList
*info
= g_malloc0(sizeof(*info
));
339 bdrv_query_info(bs
, &info
->value
, &local_err
);
341 error_propagate(errp
, local_err
);
346 p_next
= &info
->next
;
352 qapi_free_BlockInfoList(head
);
356 BlockStatsList
*qmp_query_blockstats(Error
**errp
)
358 BlockStatsList
*head
= NULL
, **p_next
= &head
;
359 BlockDriverState
*bs
= NULL
;
361 while ((bs
= bdrv_next(bs
))) {
362 BlockStatsList
*info
= g_malloc0(sizeof(*info
));
363 AioContext
*ctx
= bdrv_get_aio_context(bs
);
365 aio_context_acquire(ctx
);
366 info
->value
= bdrv_query_stats(bs
);
367 aio_context_release(ctx
);
370 p_next
= &info
->next
;
376 #define NB_SUFFIXES 4
378 static char *get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
380 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
385 snprintf(buf
, buf_size
, "%" PRId64
, size
);
388 for (i
= 0; i
< NB_SUFFIXES
; i
++) {
389 if (size
< (10 * base
)) {
390 snprintf(buf
, buf_size
, "%0.1f%c",
394 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
395 snprintf(buf
, buf_size
, "%" PRId64
"%c",
396 ((size
+ (base
>> 1)) / base
),
406 void bdrv_snapshot_dump(fprintf_function func_fprintf
, void *f
,
407 QEMUSnapshotInfo
*sn
)
409 char buf1
[128], date_buf
[128], clock_buf
[128];
416 "%-10s%-20s%7s%20s%15s",
417 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
420 localtime_r(&ti
, &tm
);
421 strftime(date_buf
, sizeof(date_buf
),
422 "%Y-%m-%d %H:%M:%S", &tm
);
423 secs
= sn
->vm_clock_nsec
/ 1000000000;
424 snprintf(clock_buf
, sizeof(clock_buf
),
425 "%02d:%02d:%02d.%03d",
427 (int)((secs
/ 60) % 60),
429 (int)((sn
->vm_clock_nsec
/ 1000000) % 1000));
431 "%-10s%-20s%7s%20s%15s",
432 sn
->id_str
, sn
->name
,
433 get_human_readable_size(buf1
, sizeof(buf1
),
440 static void dump_qdict(fprintf_function func_fprintf
, void *f
, int indentation
,
442 static void dump_qlist(fprintf_function func_fprintf
, void *f
, int indentation
,
445 static void dump_qobject(fprintf_function func_fprintf
, void *f
,
446 int comp_indent
, QObject
*obj
)
448 switch (qobject_type(obj
)) {
450 QInt
*value
= qobject_to_qint(obj
);
451 func_fprintf(f
, "%" PRId64
, qint_get_int(value
));
454 case QTYPE_QSTRING
: {
455 QString
*value
= qobject_to_qstring(obj
);
456 func_fprintf(f
, "%s", qstring_get_str(value
));
460 QDict
*value
= qobject_to_qdict(obj
);
461 dump_qdict(func_fprintf
, f
, comp_indent
, value
);
465 QList
*value
= qobject_to_qlist(obj
);
466 dump_qlist(func_fprintf
, f
, comp_indent
, value
);
470 QFloat
*value
= qobject_to_qfloat(obj
);
471 func_fprintf(f
, "%g", qfloat_get_double(value
));
475 QBool
*value
= qobject_to_qbool(obj
);
476 func_fprintf(f
, "%s", qbool_get_int(value
) ? "true" : "false");
480 QString
*value
= qerror_human((QError
*)obj
);
481 func_fprintf(f
, "%s", qstring_get_str(value
));
493 static void dump_qlist(fprintf_function func_fprintf
, void *f
, int indentation
,
496 const QListEntry
*entry
;
499 for (entry
= qlist_first(list
); entry
; entry
= qlist_next(entry
), i
++) {
500 qtype_code type
= qobject_type(entry
->value
);
501 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
502 const char *format
= composite
? "%*s[%i]:\n" : "%*s[%i]: ";
504 func_fprintf(f
, format
, indentation
* 4, "", i
);
505 dump_qobject(func_fprintf
, f
, indentation
+ 1, entry
->value
);
507 func_fprintf(f
, "\n");
512 static void dump_qdict(fprintf_function func_fprintf
, void *f
, int indentation
,
515 const QDictEntry
*entry
;
517 for (entry
= qdict_first(dict
); entry
; entry
= qdict_next(dict
, entry
)) {
518 qtype_code type
= qobject_type(entry
->value
);
519 bool composite
= (type
== QTYPE_QDICT
|| type
== QTYPE_QLIST
);
520 const char *format
= composite
? "%*s%s:\n" : "%*s%s: ";
521 char key
[strlen(entry
->key
) + 1];
524 /* replace dashes with spaces in key (variable) names */
525 for (i
= 0; entry
->key
[i
]; i
++) {
526 key
[i
] = entry
->key
[i
] == '-' ? ' ' : entry
->key
[i
];
530 func_fprintf(f
, format
, indentation
* 4, "", key
);
531 dump_qobject(func_fprintf
, f
, indentation
+ 1, entry
->value
);
533 func_fprintf(f
, "\n");
538 void bdrv_image_info_specific_dump(fprintf_function func_fprintf
, void *f
,
539 ImageInfoSpecific
*info_spec
)
541 QmpOutputVisitor
*ov
= qmp_output_visitor_new();
544 visit_type_ImageInfoSpecific(qmp_output_get_visitor(ov
), &info_spec
, NULL
,
546 obj
= qmp_output_get_qobject(ov
);
547 assert(qobject_type(obj
) == QTYPE_QDICT
);
548 data
= qdict_get(qobject_to_qdict(obj
), "data");
549 dump_qobject(func_fprintf
, f
, 1, data
);
550 qmp_output_visitor_cleanup(ov
);
553 void bdrv_image_info_dump(fprintf_function func_fprintf
, void *f
,
556 char size_buf
[128], dsize_buf
[128];
557 if (!info
->has_actual_size
) {
558 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
560 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
563 get_human_readable_size(size_buf
, sizeof(size_buf
), info
->virtual_size
);
567 "virtual size: %s (%" PRId64
" bytes)\n"
569 info
->filename
, info
->format
, size_buf
,
573 if (info
->has_encrypted
&& info
->encrypted
) {
574 func_fprintf(f
, "encrypted: yes\n");
577 if (info
->has_cluster_size
) {
578 func_fprintf(f
, "cluster_size: %" PRId64
"\n",
582 if (info
->has_dirty_flag
&& info
->dirty_flag
) {
583 func_fprintf(f
, "cleanly shut down: no\n");
586 if (info
->has_backing_filename
) {
587 func_fprintf(f
, "backing file: %s", info
->backing_filename
);
588 if (info
->has_full_backing_filename
) {
589 func_fprintf(f
, " (actual path: %s)", info
->full_backing_filename
);
591 func_fprintf(f
, "\n");
592 if (info
->has_backing_filename_format
) {
593 func_fprintf(f
, "backing file format: %s\n",
594 info
->backing_filename_format
);
598 if (info
->has_snapshots
) {
599 SnapshotInfoList
*elem
;
601 func_fprintf(f
, "Snapshot list:\n");
602 bdrv_snapshot_dump(func_fprintf
, f
, NULL
);
603 func_fprintf(f
, "\n");
605 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
606 * we convert to the block layer's native QEMUSnapshotInfo for now.
608 for (elem
= info
->snapshots
; elem
; elem
= elem
->next
) {
609 QEMUSnapshotInfo sn
= {
610 .vm_state_size
= elem
->value
->vm_state_size
,
611 .date_sec
= elem
->value
->date_sec
,
612 .date_nsec
= elem
->value
->date_nsec
,
613 .vm_clock_nsec
= elem
->value
->vm_clock_sec
* 1000000000ULL +
614 elem
->value
->vm_clock_nsec
,
617 pstrcpy(sn
.id_str
, sizeof(sn
.id_str
), elem
->value
->id
);
618 pstrcpy(sn
.name
, sizeof(sn
.name
), elem
->value
->name
);
619 bdrv_snapshot_dump(func_fprintf
, f
, &sn
);
620 func_fprintf(f
, "\n");
624 if (info
->has_format_specific
) {
625 func_fprintf(f
, "Format specific information:\n");
626 bdrv_image_info_specific_dump(func_fprintf
, f
, info
->format_specific
);