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