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