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"
29 void bdrv_collect_snapshots(BlockDriverState *bs , ImageInfo *info)
32 QEMUSnapshotInfo *sn_tab = NULL;
33 SnapshotInfoList *info_list, *cur_item = NULL;
34 sn_count = bdrv_snapshot_list(bs, &sn_tab);
36 for (i = 0; i < sn_count; i++) {
37 info->has_snapshots = true;
38 info_list = g_new0(SnapshotInfoList, 1);
40 info_list->value = g_new0(SnapshotInfo, 1);
41 info_list->value->id = g_strdup(sn_tab[i].id_str);
42 info_list->value->name = g_strdup(sn_tab[i].name);
43 info_list->value->vm_state_size = sn_tab[i].vm_state_size;
44 info_list->value->date_sec = sn_tab[i].date_sec;
45 info_list->value->date_nsec = sn_tab[i].date_nsec;
46 info_list->value->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
47 info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
49 /* XXX: waiting for the qapi to support qemu-queue.h types */
51 info->snapshots = cur_item = info_list;
53 cur_item->next = info_list;
62 void bdrv_collect_image_info(BlockDriverState *bs,
66 uint64_t total_sectors;
67 char backing_filename[1024];
68 char backing_filename2[1024];
71 bdrv_get_geometry(bs, &total_sectors);
73 info->filename = g_strdup(filename);
74 info->format = g_strdup(bdrv_get_format_name(bs));
75 info->virtual_size = total_sectors * 512;
76 info->actual_size = bdrv_get_allocated_file_size(bs);
77 info->has_actual_size = info->actual_size >= 0;
78 if (bdrv_is_encrypted(bs)) {
79 info->encrypted = true;
80 info->has_encrypted = true;
82 if (bdrv_get_info(bs, &bdi) >= 0) {
83 if (bdi.cluster_size != 0) {
84 info->cluster_size = bdi.cluster_size;
85 info->has_cluster_size = true;
87 info->dirty_flag = bdi.is_dirty;
88 info->has_dirty_flag = true;
90 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
91 if (backing_filename[0] != '\0') {
92 info->backing_filename = g_strdup(backing_filename);
93 info->has_backing_filename = true;
94 bdrv_get_full_backing_filename(bs, backing_filename2,
95 sizeof(backing_filename2));
97 if (strcmp(backing_filename, backing_filename2) != 0) {
98 info->full_backing_filename =
99 g_strdup(backing_filename2);
100 info->has_full_backing_filename = true;
103 if (bs->backing_format[0]) {
104 info->backing_filename_format = g_strdup(bs->backing_format);
105 info->has_backing_filename_format = true;
110 BlockInfo *bdrv_query_info(BlockDriverState *bs)
112 BlockInfo *info = g_malloc0(sizeof(*info));
113 info->device = g_strdup(bs->device_name);
114 info->type = g_strdup("unknown");
115 info->locked = bdrv_dev_is_medium_locked(bs);
116 info->removable = bdrv_dev_has_removable_media(bs);
118 if (bdrv_dev_has_removable_media(bs)) {
119 info->has_tray_open = true;
120 info->tray_open = bdrv_dev_is_tray_open(bs);
123 if (bdrv_iostatus_is_enabled(bs)) {
124 info->has_io_status = true;
125 info->io_status = bs->iostatus;
128 if (bs->dirty_bitmap) {
129 info->has_dirty = true;
130 info->dirty = g_malloc0(sizeof(*info->dirty));
131 info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE;
132 info->dirty->granularity =
133 ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap));
137 info->has_inserted = true;
138 info->inserted = g_malloc0(sizeof(*info->inserted));
139 info->inserted->file = g_strdup(bs->filename);
140 info->inserted->ro = bs->read_only;
141 info->inserted->drv = g_strdup(bs->drv->format_name);
142 info->inserted->encrypted = bs->encrypted;
143 info->inserted->encryption_key_missing = bdrv_key_required(bs);
145 if (bs->backing_file[0]) {
146 info->inserted->has_backing_file = true;
147 info->inserted->backing_file = g_strdup(bs->backing_file);
150 info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs);
152 if (bs->io_limits_enabled) {
153 info->inserted->bps =
154 bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
155 info->inserted->bps_rd =
156 bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
157 info->inserted->bps_wr =
158 bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
159 info->inserted->iops =
160 bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
161 info->inserted->iops_rd =
162 bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
163 info->inserted->iops_wr =
164 bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
170 BlockStats *bdrv_query_stats(const BlockDriverState *bs)
174 s = g_malloc0(sizeof(*s));
176 if (bs->device_name[0]) {
177 s->has_device = true;
178 s->device = g_strdup(bs->device_name);
181 s->stats = g_malloc0(sizeof(*s->stats));
182 s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
183 s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
184 s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
185 s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
186 s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
187 s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
188 s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
189 s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
190 s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
193 s->has_parent = true;
194 s->parent = bdrv_query_stats(bs->file);
200 BlockInfoList *qmp_query_block(Error **errp)
202 BlockInfoList *head = NULL, **p_next = &head;
203 BlockDriverState *bs = NULL;
205 while ((bs = bdrv_next(bs))) {
206 BlockInfoList *info = g_malloc0(sizeof(*info));
207 info->value = bdrv_query_info(bs);
210 p_next = &info->next;
216 BlockStatsList *qmp_query_blockstats(Error **errp)
218 BlockStatsList *head = NULL, **p_next = &head;
219 BlockDriverState *bs = NULL;
221 while ((bs = bdrv_next(bs))) {
222 BlockStatsList *info = g_malloc0(sizeof(*info));
223 info->value = bdrv_query_stats(bs);
226 p_next = &info->next;
232 #define NB_SUFFIXES 4
234 static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
236 static const char suffixes[NB_SUFFIXES] = "KMGT";
241 snprintf(buf, buf_size, "%" PRId64, size);
244 for (i = 0; i < NB_SUFFIXES; i++) {
245 if (size < (10 * base)) {
246 snprintf(buf, buf_size, "%0.1f%c",
250 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
251 snprintf(buf, buf_size, "%" PRId64 "%c",
252 ((size + (base >> 1)) / base),
262 void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
263 QEMUSnapshotInfo *sn)
265 char buf1[128], date_buf[128], clock_buf[128];
272 "%-10s%-20s%7s%20s%15s",
273 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
276 localtime_r(&ti, &tm);
277 strftime(date_buf, sizeof(date_buf),
278 "%Y-%m-%d %H:%M:%S", &tm);
279 secs = sn->vm_clock_nsec / 1000000000;
280 snprintf(clock_buf, sizeof(clock_buf),
281 "%02d:%02d:%02d.%03d",
283 (int)((secs / 60) % 60),
285 (int)((sn->vm_clock_nsec / 1000000) % 1000));
287 "%-10s%-20s%7s%20s%15s",
288 sn->id_str, sn->name,
289 get_human_readable_size(buf1, sizeof(buf1),
296 void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
299 char size_buf[128], dsize_buf[128];
300 if (!info->has_actual_size) {
301 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
303 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
306 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
310 "virtual size: %s (%" PRId64 " bytes)\n"
312 info->filename, info->format, size_buf,
316 if (info->has_encrypted && info->encrypted) {
317 func_fprintf(f, "encrypted: yes\n");
320 if (info->has_cluster_size) {
321 func_fprintf(f, "cluster_size: %" PRId64 "\n",
325 if (info->has_dirty_flag && info->dirty_flag) {
326 func_fprintf(f, "cleanly shut down: no\n");
329 if (info->has_backing_filename) {
330 func_fprintf(f, "backing file: %s", info->backing_filename);
331 if (info->has_full_backing_filename) {
332 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
334 func_fprintf(f, "\n");
335 if (info->has_backing_filename_format) {
336 func_fprintf(f, "backing file format: %s\n",
337 info->backing_filename_format);
341 if (info->has_snapshots) {
342 SnapshotInfoList *elem;
344 func_fprintf(f, "Snapshot list:\n");
345 bdrv_snapshot_dump(func_fprintf, f, NULL);
346 func_fprintf(f, "\n");
348 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
349 * we convert to the block layer's native QEMUSnapshotInfo for now.
351 for (elem = info->snapshots; elem; elem = elem->next) {
352 QEMUSnapshotInfo sn = {
353 .vm_state_size = elem->value->vm_state_size,
354 .date_sec = elem->value->date_sec,
355 .date_nsec = elem->value->date_nsec,
356 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
357 elem->value->vm_clock_nsec,
360 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
361 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
362 bdrv_snapshot_dump(func_fprintf, f, &sn);
363 func_fprintf(f, "\n");