]> git.proxmox.com Git - mirror_qemu.git/blame - block/qapi.c
block/accounting: introduce latency histogram
[mirror_qemu.git] / block / qapi.c
CommitLineData
f364ec65
WX
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
80c71a24 25#include "qemu/osdep.h"
f364ec65
WX
26#include "block/qapi.h"
27#include "block/block_int.h"
76f4afb4 28#include "block/throttle-groups.h"
e2462113 29#include "block/write-threshold.h"
e688df6b 30#include "qapi/error.h"
9af23989 31#include "qapi/qapi-commands-block-core.h"
b3db211f 32#include "qapi/qobject-output-visitor.h"
9af23989 33#include "qapi/qapi-visit-block-core.h"
6b673957 34#include "qapi/qmp/qbool.h"
452fcdbc 35#include "qapi/qmp/qdict.h"
47e6b297 36#include "qapi/qmp/qlist.h"
15280c36 37#include "qapi/qmp/qnum.h"
6b673957 38#include "qapi/qmp/qstring.h"
d829a211 39#include "sysemu/block-backend.h"
f348b6d1 40#include "qemu/cutils.h"
f364ec65 41
c83f9fba
KW
42BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk,
43 BlockDriverState *bs, Error **errp)
c13163fb 44{
d5a8ee60
AG
45 ImageInfo **p_image_info;
46 BlockDriverState *bs0;
d470ad42 47 BlockDeviceInfo *info;
c13163fb 48
d470ad42
HR
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));
c13163fb
BC
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;
c01c214b 59 info->encryption_key_missing = false;
c13163fb 60
9e193c5a
KW
61 info->cache = g_new(BlockdevCacheInfo, 1);
62 *info->cache = (BlockdevCacheInfo) {
c83f9fba 63 .writeback = blk ? blk_enable_write_cache(blk) : true,
9e193c5a
KW
64 .direct = !!(bs->open_flags & BDRV_O_NOCACHE),
65 .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH),
66 };
67
c13163fb
BC
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
465bee1d 78 info->detect_zeroes = bs->detect_zeroes;
c13163fb 79
022cdc9f 80 if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) {
c13163fb 81 ThrottleConfig cfg;
022cdc9f 82 BlockBackendPublic *blkp = blk_get_public(blk);
76f4afb4 83
022cdc9f 84 throttle_group_get_config(&blkp->throttle_group_member, &cfg);
76f4afb4 85
c13163fb
BC
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
398befdf
AG
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
c13163fb
BC
128 info->has_iops_size = cfg.op_size;
129 info->iops_size = cfg.op_size;
b8fe1694
AG
130
131 info->has_group = true;
022cdc9f
MP
132 info->group =
133 g_strdup(throttle_group_get_name(&blkp->throttle_group_member));
c13163fb
BC
134 }
135
e2462113
FR
136 info->write_threshold = bdrv_write_threshold_get(bs);
137
d5a8ee60
AG
138 bs0 = bs;
139 p_image_info = &info->image;
d3c8c674 140 info->backing_file_depth = 0;
d5a8ee60
AG
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 }
d3c8c674 149
760e0063 150 if (bs0->drv && bs0->backing) {
d3c8c674 151 info->backing_file_depth++;
760e0063 152 bs0 = bs0->backing->bs;
d5a8ee60
AG
153 (*p_image_info)->has_backing_image = true;
154 p_image_info = &((*p_image_info)->backing_image);
155 } else {
156 break;
157 }
d3c8c674
KW
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 */
8e8eb0a9 161 while (blk && bs0->drv && bs0->implicit) {
d3c8c674 162 bs0 = backing_bs(bs0);
8e8eb0a9 163 assert(bs0);
d3c8c674 164 }
d5a8ee60
AG
165 }
166
c13163fb
BC
167 return info;
168}
169
fb0ed453
WX
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 */
175int bdrv_query_snapshot_info_list(BlockDriverState *bs,
176 SnapshotInfoList **p_list,
177 Error **errp)
f364ec65
WX
178{
179 int i, sn_count;
180 QEMUSnapshotInfo *sn_tab = NULL;
fb0ed453
WX
181 SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL;
182 SnapshotInfo *info;
183
f364ec65 184 sn_count = bdrv_snapshot_list(bs, &sn_tab);
fb0ed453
WX
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 }
f364ec65
WX
203
204 for (i = 0; i < sn_count; i++) {
fb0ed453
WX
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;
f364ec65 213
fb0ed453
WX
214 info_list = g_new0(SnapshotInfoList, 1);
215 info_list->value = info;
f364ec65
WX
216
217 /* XXX: waiting for the qapi to support qemu-queue.h types */
218 if (!cur_item) {
fb0ed453 219 head = cur_item = info_list;
f364ec65
WX
220 } else {
221 cur_item->next = info_list;
222 cur_item = info_list;
223 }
224
225 }
226
227 g_free(sn_tab);
fb0ed453
WX
228 *p_list = head;
229 return 0;
f364ec65
WX
230}
231
43526ec8
WX
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 *
553a7e87
WX
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 *
43526ec8
WX
245 * @p_info will be set only on success. On error, store error in @errp.
246 */
247void bdrv_query_image_info(BlockDriverState *bs,
248 ImageInfo **p_info,
249 Error **errp)
f364ec65 250{
52bf1e72 251 int64_t size;
43526ec8 252 const char *backing_filename;
f364ec65 253 BlockDriverInfo bdi;
43526ec8
WX
254 int ret;
255 Error *err = NULL;
52bf1e72 256 ImageInfo *info;
f364ec65 257
1963f8d5
PB
258 aio_context_acquire(bdrv_get_aio_context(bs));
259
52bf1e72
MA
260 size = bdrv_getlength(bs);
261 if (size < 0) {
9adceb02
FZ
262 error_setg_errno(errp, -size, "Can't get image size '%s'",
263 bs->exact_filename);
1963f8d5 264 goto out;
52bf1e72 265 }
f364ec65 266
52bf1e72 267 info = g_new0(ImageInfo, 1);
43526ec8 268 info->filename = g_strdup(bs->filename);
f364ec65 269 info->format = g_strdup(bdrv_get_format_name(bs));
52bf1e72 270 info->virtual_size = size;
f364ec65
WX
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 }
eae041fe
HR
285 info->format_specific = bdrv_get_specific_info(bs);
286 info->has_format_specific = info->format_specific != NULL;
287
43526ec8 288 backing_filename = bs->backing_file;
f364ec65 289 if (backing_filename[0] != '\0') {
9a29e18f 290 char *backing_filename2 = g_malloc0(PATH_MAX);
f364ec65
WX
291 info->backing_filename = g_strdup(backing_filename);
292 info->has_backing_filename = true;
9a29e18f 293 bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err);
9f07429e 294 if (err) {
a5002d53
JS
295 /* Can't reconstruct the full backing filename, so we must omit
296 * this field and apply a Best Effort to this query. */
564d64bd 297 g_free(backing_filename2);
a5002d53
JS
298 backing_filename2 = NULL;
299 error_free(err);
0fa296eb 300 err = NULL;
9f07429e 301 }
f364ec65 302
12dcb1c0
JS
303 /* Always report the full_backing_filename if present, even if it's the
304 * same as backing_filename. That they are same is useful info. */
305 if (backing_filename2) {
306 info->full_backing_filename = g_strdup(backing_filename2);
f364ec65
WX
307 info->has_full_backing_filename = true;
308 }
309
310 if (bs->backing_format[0]) {
311 info->backing_filename_format = g_strdup(bs->backing_format);
312 info->has_backing_filename_format = true;
313 }
564d64bd 314 g_free(backing_filename2);
f364ec65 315 }
43526ec8
WX
316
317 ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err);
318 switch (ret) {
319 case 0:
320 if (info->snapshots) {
321 info->has_snapshots = true;
322 }
323 break;
324 /* recoverable error */
325 case -ENOMEDIUM:
326 case -ENOTSUP:
327 error_free(err);
328 break;
329 default:
330 error_propagate(errp, err);
331 qapi_free_ImageInfo(info);
1963f8d5 332 goto out;
43526ec8
WX
333 }
334
335 *p_info = info;
1963f8d5
PB
336
337out:
338 aio_context_release(bdrv_get_aio_context(bs));
f364ec65
WX
339}
340
553a7e87 341/* @p_info will be set only on success. */
d829a211
MA
342static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info,
343 Error **errp)
f364ec65
WX
344{
345 BlockInfo *info = g_malloc0(sizeof(*info));
d829a211 346 BlockDriverState *bs = blk_bs(blk);
46eade7b
KW
347 char *qdev;
348
d3c8c674
KW
349 /* Skip automatically inserted nodes that the user isn't aware of */
350 while (bs && bs->drv && bs->implicit) {
351 bs = backing_bs(bs);
352 }
353
d829a211 354 info->device = g_strdup(blk_name(blk));
f364ec65 355 info->type = g_strdup("unknown");
a7f53e26
MA
356 info->locked = blk_dev_is_medium_locked(blk);
357 info->removable = blk_dev_has_removable_media(blk);
f364ec65 358
46eade7b
KW
359 qdev = blk_get_attached_dev_id(blk);
360 if (qdev && *qdev) {
361 info->has_qdev = true;
362 info->qdev = qdev;
363 } else {
364 g_free(qdev);
365 }
366
327032ce 367 if (blk_dev_has_tray(blk)) {
f364ec65 368 info->has_tray_open = true;
a7f53e26 369 info->tray_open = blk_dev_is_tray_open(blk);
f364ec65
WX
370 }
371
373340b2 372 if (blk_iostatus_is_enabled(blk)) {
f364ec65 373 info->has_io_status = true;
373340b2 374 info->io_status = blk_iostatus(blk);
f364ec65
WX
375 }
376
5433c24f 377 if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) {
21b56835
FZ
378 info->has_dirty_bitmaps = true;
379 info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs);
380 }
381
5433c24f 382 if (bs && bs->drv) {
f364ec65 383 info->has_inserted = true;
c83f9fba 384 info->inserted = bdrv_block_device_info(blk, bs, errp);
d5a8ee60
AG
385 if (info->inserted == NULL) {
386 goto err;
553a7e87 387 }
f364ec65 388 }
553a7e87
WX
389
390 *p_info = info;
391 return;
392
393 err:
394 qapi_free_BlockInfo(info);
f364ec65
WX
395}
396
54302156 397static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk)
2b77e60a
KW
398{
399 BlockAcctStats *stats = blk_get_stats(blk);
400 BlockAcctTimedStats *ts = NULL;
401
54302156
HR
402 ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ];
403 ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE];
404 ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ];
405 ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE];
2b77e60a 406
54302156
HR
407 ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ];
408 ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE];
409 ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH];
2b77e60a 410
54302156
HR
411 ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ];
412 ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE];
413 ds->invalid_flush_operations =
2b77e60a
KW
414 stats->invalid_ops[BLOCK_ACCT_FLUSH];
415
54302156
HR
416 ds->rd_merged = stats->merged[BLOCK_ACCT_READ];
417 ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE];
418 ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH];
419 ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE];
420 ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ];
421 ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH];
2b77e60a 422
54302156
HR
423 ds->has_idle_time_ns = stats->last_access_time_ns > 0;
424 if (ds->has_idle_time_ns) {
425 ds->idle_time_ns = block_acct_idle_time_ns(stats);
2b77e60a
KW
426 }
427
54302156
HR
428 ds->account_invalid = stats->account_invalid;
429 ds->account_failed = stats->account_failed;
2b77e60a
KW
430
431 while ((ts = block_acct_interval_next(stats, ts))) {
432 BlockDeviceTimedStatsList *timed_stats =
433 g_malloc0(sizeof(*timed_stats));
434 BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats));
54302156 435 timed_stats->next = ds->timed_stats;
2b77e60a 436 timed_stats->value = dev_stats;
54302156 437 ds->timed_stats = timed_stats;
2b77e60a
KW
438
439 TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ];
440 TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE];
441 TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH];
442
443 dev_stats->interval_length = ts->interval_length;
444
445 dev_stats->min_rd_latency_ns = timed_average_min(rd);
446 dev_stats->max_rd_latency_ns = timed_average_max(rd);
447 dev_stats->avg_rd_latency_ns = timed_average_avg(rd);
448
449 dev_stats->min_wr_latency_ns = timed_average_min(wr);
450 dev_stats->max_wr_latency_ns = timed_average_max(wr);
451 dev_stats->avg_wr_latency_ns = timed_average_avg(wr);
452
453 dev_stats->min_flush_latency_ns = timed_average_min(fl);
454 dev_stats->max_flush_latency_ns = timed_average_max(fl);
455 dev_stats->avg_flush_latency_ns = timed_average_avg(fl);
456
457 dev_stats->avg_rd_queue_depth =
458 block_acct_queue_depth(ts, BLOCK_ACCT_READ);
459 dev_stats->avg_wr_queue_depth =
460 block_acct_queue_depth(ts, BLOCK_ACCT_WRITE);
461 }
462}
463
d3c8c674
KW
464static BlockStats *bdrv_query_bds_stats(BlockDriverState *bs,
465 bool blk_level)
f364ec65 466{
20a6d768
DL
467 BlockStats *s = NULL;
468
469 s = g_malloc0(sizeof(*s));
470 s->stats = g_malloc0(sizeof(*s->stats));
471
472 if (!bs) {
473 return s;
474 }
475
d3c8c674
KW
476 /* Skip automatically inserted nodes that the user isn't aware of in
477 * a BlockBackend-level command. Stay at the exact node for a node-level
478 * command. */
479 while (blk_level && bs->drv && bs->implicit) {
480 bs = backing_bs(bs);
481 assert(bs);
482 }
483
4875a779
FZ
484 if (bdrv_get_node_name(bs)[0]) {
485 s->has_node_name = true;
486 s->node_name = g_strdup(bdrv_get_node_name(bs));
487 }
488
f7946da2 489 s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset);
53d8f9d8 490
f364ec65
WX
491 if (bs->file) {
492 s->has_parent = true;
d3c8c674 493 s->parent = bdrv_query_bds_stats(bs->file->bs, blk_level);
f364ec65
WX
494 }
495
d3c8c674 496 if (blk_level && bs->backing) {
c8059b97 497 s->has_backing = true;
d3c8c674 498 s->backing = bdrv_query_bds_stats(bs->backing->bs, blk_level);
c8059b97
FZ
499 }
500
20a6d768 501 return s;
b07363a1
KW
502}
503
f364ec65
WX
504BlockInfoList *qmp_query_block(Error **errp)
505{
506 BlockInfoList *head = NULL, **p_next = &head;
d829a211 507 BlockBackend *blk;
553a7e87 508 Error *local_err = NULL;
f364ec65 509
d5b68844
KW
510 for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) {
511 BlockInfoList *info;
512
ec18b0a9 513 if (!*blk_name(blk) && !blk_get_attached_dev(blk)) {
d5b68844
KW
514 continue;
515 }
516
517 info = g_malloc0(sizeof(*info));
d829a211 518 bdrv_query_info(blk, &info->value, &local_err);
84d18f06 519 if (local_err) {
553a7e87 520 error_propagate(errp, local_err);
903c341d
MA
521 g_free(info);
522 qapi_free_BlockInfoList(head);
523 return NULL;
553a7e87 524 }
f364ec65
WX
525
526 *p_next = info;
527 p_next = &info->next;
528 }
529
530 return head;
531}
532
f71eaa74
FZ
533BlockStatsList *qmp_query_blockstats(bool has_query_nodes,
534 bool query_nodes,
535 Error **errp)
f364ec65
WX
536{
537 BlockStatsList *head = NULL, **p_next = &head;
a6baa608
DL
538 BlockBackend *blk;
539 BlockDriverState *bs;
f364ec65 540
f71eaa74 541 /* Just to be safe if query_nodes is not always initialized */
a6baa608
DL
542 if (has_query_nodes && query_nodes) {
543 for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) {
544 BlockStatsList *info = g_malloc0(sizeof(*info));
545 AioContext *ctx = bdrv_get_aio_context(bs);
13344f3a 546
a6baa608
DL
547 aio_context_acquire(ctx);
548 info->value = bdrv_query_bds_stats(bs, false);
549 aio_context_release(ctx);
f364ec65 550
a6baa608
DL
551 *p_next = info;
552 p_next = &info->next;
553 }
554 } else {
555 for (blk = blk_next(NULL); blk; blk = blk_next(blk)) {
556 BlockStatsList *info = g_malloc0(sizeof(*info));
557 AioContext *ctx = blk_get_aio_context(blk);
558 BlockStats *s;
559
560 aio_context_acquire(ctx);
561 s = bdrv_query_bds_stats(blk_bs(blk), true);
562 s->has_device = true;
563 s->device = g_strdup(blk_name(blk));
564 bdrv_query_blk_stats(s->stats, blk);
565 aio_context_release(ctx);
566
567 info->value = s;
568 *p_next = info;
569 p_next = &info->next;
570 }
f364ec65
WX
571 }
572
573 return head;
574}
575
576#define NB_SUFFIXES 4
577
578static char *get_human_readable_size(char *buf, int buf_size, int64_t size)
579{
2c20fa2c 580 static const char suffixes[NB_SUFFIXES] = {'K', 'M', 'G', 'T'};
f364ec65
WX
581 int64_t base;
582 int i;
583
584 if (size <= 999) {
585 snprintf(buf, buf_size, "%" PRId64, size);
586 } else {
587 base = 1024;
588 for (i = 0; i < NB_SUFFIXES; i++) {
589 if (size < (10 * base)) {
590 snprintf(buf, buf_size, "%0.1f%c",
591 (double)size / base,
592 suffixes[i]);
593 break;
594 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
595 snprintf(buf, buf_size, "%" PRId64 "%c",
596 ((size + (base >> 1)) / base),
597 suffixes[i]);
598 break;
599 }
600 base = base * 1024;
601 }
602 }
603 return buf;
604}
605
5b917044
WX
606void bdrv_snapshot_dump(fprintf_function func_fprintf, void *f,
607 QEMUSnapshotInfo *sn)
f364ec65
WX
608{
609 char buf1[128], date_buf[128], clock_buf[128];
610 struct tm tm;
611 time_t ti;
612 int64_t secs;
613
614 if (!sn) {
5b917044
WX
615 func_fprintf(f,
616 "%-10s%-20s%7s%20s%15s",
617 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
f364ec65
WX
618 } else {
619 ti = sn->date_sec;
620 localtime_r(&ti, &tm);
621 strftime(date_buf, sizeof(date_buf),
622 "%Y-%m-%d %H:%M:%S", &tm);
623 secs = sn->vm_clock_nsec / 1000000000;
624 snprintf(clock_buf, sizeof(clock_buf),
625 "%02d:%02d:%02d.%03d",
626 (int)(secs / 3600),
627 (int)((secs / 60) % 60),
628 (int)(secs % 60),
629 (int)((sn->vm_clock_nsec / 1000000) % 1000));
5b917044
WX
630 func_fprintf(f,
631 "%-10s%-20s%7s%20s%15s",
632 sn->id_str, sn->name,
633 get_human_readable_size(buf1, sizeof(buf1),
634 sn->vm_state_size),
635 date_buf,
636 clock_buf);
f364ec65 637 }
f364ec65
WX
638}
639
a8d8ecb7
HR
640static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
641 QDict *dict);
642static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
643 QList *list);
644
645static void dump_qobject(fprintf_function func_fprintf, void *f,
646 int comp_indent, QObject *obj)
647{
648 switch (qobject_type(obj)) {
01b2ffce 649 case QTYPE_QNUM: {
7dc847eb 650 QNum *value = qobject_to(QNum, obj);
01b2ffce
MAL
651 char *tmp = qnum_to_string(value);
652 func_fprintf(f, "%s", tmp);
653 g_free(tmp);
a8d8ecb7
HR
654 break;
655 }
656 case QTYPE_QSTRING: {
7dc847eb 657 QString *value = qobject_to(QString, obj);
a8d8ecb7
HR
658 func_fprintf(f, "%s", qstring_get_str(value));
659 break;
660 }
661 case QTYPE_QDICT: {
7dc847eb 662 QDict *value = qobject_to(QDict, obj);
a8d8ecb7
HR
663 dump_qdict(func_fprintf, f, comp_indent, value);
664 break;
665 }
666 case QTYPE_QLIST: {
7dc847eb 667 QList *value = qobject_to(QList, obj);
a8d8ecb7
HR
668 dump_qlist(func_fprintf, f, comp_indent, value);
669 break;
670 }
a8d8ecb7 671 case QTYPE_QBOOL: {
7dc847eb 672 QBool *value = qobject_to(QBool, obj);
fc48ffc3 673 func_fprintf(f, "%s", qbool_get_bool(value) ? "true" : "false");
a8d8ecb7
HR
674 break;
675 }
a8d8ecb7
HR
676 default:
677 abort();
678 }
679}
680
681static void dump_qlist(fprintf_function func_fprintf, void *f, int indentation,
682 QList *list)
683{
684 const QListEntry *entry;
685 int i = 0;
686
687 for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) {
1310a3d3 688 QType type = qobject_type(entry->value);
a8d8ecb7 689 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
853ccfed
PX
690 func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i,
691 composite ? '\n' : ' ');
a8d8ecb7
HR
692 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
693 if (!composite) {
694 func_fprintf(f, "\n");
695 }
696 }
697}
698
699static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation,
700 QDict *dict)
701{
702 const QDictEntry *entry;
703
704 for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) {
1310a3d3 705 QType type = qobject_type(entry->value);
a8d8ecb7 706 bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST);
5eda6227 707 char *key = g_malloc(strlen(entry->key) + 1);
a8d8ecb7
HR
708 int i;
709
710 /* replace dashes with spaces in key (variable) names */
711 for (i = 0; entry->key[i]; i++) {
712 key[i] = entry->key[i] == '-' ? ' ' : entry->key[i];
713 }
714 key[i] = 0;
853ccfed
PX
715 func_fprintf(f, "%*s%s:%c", indentation * 4, "", key,
716 composite ? '\n' : ' ');
a8d8ecb7
HR
717 dump_qobject(func_fprintf, f, indentation + 1, entry->value);
718 if (!composite) {
719 func_fprintf(f, "\n");
720 }
5eda6227 721 g_free(key);
a8d8ecb7
HR
722 }
723}
724
725void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f,
726 ImageInfoSpecific *info_spec)
727{
a8d8ecb7 728 QObject *obj, *data;
7d5e199a 729 Visitor *v = qobject_output_visitor_new(&obj);
a8d8ecb7 730
3b098d56
EB
731 visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort);
732 visit_complete(v, &obj);
7dc847eb 733 data = qdict_get(qobject_to(QDict, obj), "data");
a8d8ecb7 734 dump_qobject(func_fprintf, f, 1, data);
3ac2f2f7 735 qobject_decref(obj);
3b098d56 736 visit_free(v);
a8d8ecb7
HR
737}
738
5b917044
WX
739void bdrv_image_info_dump(fprintf_function func_fprintf, void *f,
740 ImageInfo *info)
f364ec65
WX
741{
742 char size_buf[128], dsize_buf[128];
743 if (!info->has_actual_size) {
744 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
745 } else {
746 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
747 info->actual_size);
748 }
749 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
5b917044
WX
750 func_fprintf(f,
751 "image: %s\n"
752 "file format: %s\n"
753 "virtual size: %s (%" PRId64 " bytes)\n"
754 "disk size: %s\n",
755 info->filename, info->format, size_buf,
756 info->virtual_size,
757 dsize_buf);
f364ec65
WX
758
759 if (info->has_encrypted && info->encrypted) {
5b917044 760 func_fprintf(f, "encrypted: yes\n");
f364ec65
WX
761 }
762
763 if (info->has_cluster_size) {
5b917044
WX
764 func_fprintf(f, "cluster_size: %" PRId64 "\n",
765 info->cluster_size);
f364ec65
WX
766 }
767
768 if (info->has_dirty_flag && info->dirty_flag) {
5b917044 769 func_fprintf(f, "cleanly shut down: no\n");
f364ec65
WX
770 }
771
772 if (info->has_backing_filename) {
5b917044 773 func_fprintf(f, "backing file: %s", info->backing_filename);
5c9d9ca5
JS
774 if (!info->has_full_backing_filename) {
775 func_fprintf(f, " (cannot determine actual path)");
776 } else if (strcmp(info->backing_filename,
777 info->full_backing_filename) != 0) {
5b917044 778 func_fprintf(f, " (actual path: %s)", info->full_backing_filename);
f364ec65 779 }
5b917044 780 func_fprintf(f, "\n");
f364ec65 781 if (info->has_backing_filename_format) {
5b917044
WX
782 func_fprintf(f, "backing file format: %s\n",
783 info->backing_filename_format);
f364ec65
WX
784 }
785 }
786
787 if (info->has_snapshots) {
788 SnapshotInfoList *elem;
f364ec65 789
5b917044
WX
790 func_fprintf(f, "Snapshot list:\n");
791 bdrv_snapshot_dump(func_fprintf, f, NULL);
792 func_fprintf(f, "\n");
f364ec65
WX
793
794 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
795 * we convert to the block layer's native QEMUSnapshotInfo for now.
796 */
797 for (elem = info->snapshots; elem; elem = elem->next) {
798 QEMUSnapshotInfo sn = {
799 .vm_state_size = elem->value->vm_state_size,
800 .date_sec = elem->value->date_sec,
801 .date_nsec = elem->value->date_nsec,
802 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
803 elem->value->vm_clock_nsec,
804 };
805
806 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
807 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
5b917044
WX
808 bdrv_snapshot_dump(func_fprintf, f, &sn);
809 func_fprintf(f, "\n");
f364ec65
WX
810 }
811 }
a8d8ecb7
HR
812
813 if (info->has_format_specific) {
814 func_fprintf(f, "Format specific information:\n");
815 bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific);
816 }
f364ec65 817}