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