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