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