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