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