]>
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 | 31 | #include "qapi-visit.h" |
b3db211f | 32 | #include "qapi/qobject-output-visitor.h" |
a8d8ecb7 | 33 | #include "qapi/qmp/types.h" |
d829a211 | 34 | #include "sysemu/block-backend.h" |
f348b6d1 | 35 | #include "qemu/cutils.h" |
f364ec65 | 36 | |
c83f9fba KW |
37 | BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, |
38 | BlockDriverState *bs, Error **errp) | |
c13163fb | 39 | { |
d5a8ee60 AG |
40 | ImageInfo **p_image_info; |
41 | BlockDriverState *bs0; | |
c13163fb BC |
42 | BlockDeviceInfo *info = g_malloc0(sizeof(*info)); |
43 | ||
44 | info->file = g_strdup(bs->filename); | |
45 | info->ro = bs->read_only; | |
46 | info->drv = g_strdup(bs->drv->format_name); | |
47 | info->encrypted = bs->encrypted; | |
48 | info->encryption_key_missing = bdrv_key_required(bs); | |
49 | ||
9e193c5a KW |
50 | info->cache = g_new(BlockdevCacheInfo, 1); |
51 | *info->cache = (BlockdevCacheInfo) { | |
c83f9fba | 52 | .writeback = blk ? blk_enable_write_cache(blk) : true, |
9e193c5a KW |
53 | .direct = !!(bs->open_flags & BDRV_O_NOCACHE), |
54 | .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH), | |
55 | }; | |
56 | ||
c13163fb BC |
57 | if (bs->node_name[0]) { |
58 | info->has_node_name = true; | |
59 | info->node_name = g_strdup(bs->node_name); | |
60 | } | |
61 | ||
62 | if (bs->backing_file[0]) { | |
63 | info->has_backing_file = true; | |
64 | info->backing_file = g_strdup(bs->backing_file); | |
65 | } | |
66 | ||
67 | info->backing_file_depth = bdrv_get_backing_file_depth(bs); | |
465bee1d | 68 | info->detect_zeroes = bs->detect_zeroes; |
c13163fb | 69 | |
79c719b7 | 70 | if (blk && blk_get_public(blk)->throttle_state) { |
c13163fb | 71 | ThrottleConfig cfg; |
76f4afb4 | 72 | |
79c719b7 | 73 | throttle_group_get_config(blk, &cfg); |
76f4afb4 | 74 | |
c13163fb BC |
75 | info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg; |
76 | info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg; | |
77 | info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg; | |
78 | ||
79 | info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg; | |
80 | info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg; | |
81 | info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg; | |
82 | ||
83 | info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; | |
84 | info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; | |
85 | info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; | |
86 | info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; | |
87 | info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; | |
88 | info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; | |
89 | ||
90 | info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; | |
91 | info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; | |
92 | info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; | |
93 | info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; | |
94 | info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; | |
95 | info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; | |
96 | ||
398befdf AG |
97 | info->has_bps_max_length = info->has_bps_max; |
98 | info->bps_max_length = | |
99 | cfg.buckets[THROTTLE_BPS_TOTAL].burst_length; | |
100 | info->has_bps_rd_max_length = info->has_bps_rd_max; | |
101 | info->bps_rd_max_length = | |
102 | cfg.buckets[THROTTLE_BPS_READ].burst_length; | |
103 | info->has_bps_wr_max_length = info->has_bps_wr_max; | |
104 | info->bps_wr_max_length = | |
105 | cfg.buckets[THROTTLE_BPS_WRITE].burst_length; | |
106 | ||
107 | info->has_iops_max_length = info->has_iops_max; | |
108 | info->iops_max_length = | |
109 | cfg.buckets[THROTTLE_OPS_TOTAL].burst_length; | |
110 | info->has_iops_rd_max_length = info->has_iops_rd_max; | |
111 | info->iops_rd_max_length = | |
112 | cfg.buckets[THROTTLE_OPS_READ].burst_length; | |
113 | info->has_iops_wr_max_length = info->has_iops_wr_max; | |
114 | info->iops_wr_max_length = | |
115 | cfg.buckets[THROTTLE_OPS_WRITE].burst_length; | |
116 | ||
c13163fb BC |
117 | info->has_iops_size = cfg.op_size; |
118 | info->iops_size = cfg.op_size; | |
b8fe1694 AG |
119 | |
120 | info->has_group = true; | |
79c719b7 | 121 | info->group = g_strdup(throttle_group_get_name(blk)); |
c13163fb BC |
122 | } |
123 | ||
e2462113 FR |
124 | info->write_threshold = bdrv_write_threshold_get(bs); |
125 | ||
d5a8ee60 AG |
126 | bs0 = bs; |
127 | p_image_info = &info->image; | |
128 | while (1) { | |
129 | Error *local_err = NULL; | |
130 | bdrv_query_image_info(bs0, p_image_info, &local_err); | |
131 | if (local_err) { | |
132 | error_propagate(errp, local_err); | |
133 | qapi_free_BlockDeviceInfo(info); | |
134 | return NULL; | |
135 | } | |
760e0063 KW |
136 | if (bs0->drv && bs0->backing) { |
137 | bs0 = bs0->backing->bs; | |
d5a8ee60 AG |
138 | (*p_image_info)->has_backing_image = true; |
139 | p_image_info = &((*p_image_info)->backing_image); | |
140 | } else { | |
141 | break; | |
142 | } | |
143 | } | |
144 | ||
c13163fb BC |
145 | return info; |
146 | } | |
147 | ||
fb0ed453 WX |
148 | /* |
149 | * Returns 0 on success, with *p_list either set to describe snapshot | |
150 | * information, or NULL because there are no snapshots. Returns -errno on | |
151 | * error, with *p_list untouched. | |
152 | */ | |
153 | int bdrv_query_snapshot_info_list(BlockDriverState *bs, | |
154 | SnapshotInfoList **p_list, | |
155 | Error **errp) | |
f364ec65 WX |
156 | { |
157 | int i, sn_count; | |
158 | QEMUSnapshotInfo *sn_tab = NULL; | |
fb0ed453 WX |
159 | SnapshotInfoList *info_list, *cur_item = NULL, *head = NULL; |
160 | SnapshotInfo *info; | |
161 | ||
f364ec65 | 162 | sn_count = bdrv_snapshot_list(bs, &sn_tab); |
fb0ed453 WX |
163 | if (sn_count < 0) { |
164 | const char *dev = bdrv_get_device_name(bs); | |
165 | switch (sn_count) { | |
166 | case -ENOMEDIUM: | |
167 | error_setg(errp, "Device '%s' is not inserted", dev); | |
168 | break; | |
169 | case -ENOTSUP: | |
170 | error_setg(errp, | |
171 | "Device '%s' does not support internal snapshots", | |
172 | dev); | |
173 | break; | |
174 | default: | |
175 | error_setg_errno(errp, -sn_count, | |
176 | "Can't list snapshots of device '%s'", dev); | |
177 | break; | |
178 | } | |
179 | return sn_count; | |
180 | } | |
f364ec65 WX |
181 | |
182 | for (i = 0; i < sn_count; i++) { | |
fb0ed453 WX |
183 | info = g_new0(SnapshotInfo, 1); |
184 | info->id = g_strdup(sn_tab[i].id_str); | |
185 | info->name = g_strdup(sn_tab[i].name); | |
186 | info->vm_state_size = sn_tab[i].vm_state_size; | |
187 | info->date_sec = sn_tab[i].date_sec; | |
188 | info->date_nsec = sn_tab[i].date_nsec; | |
189 | info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; | |
190 | info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; | |
f364ec65 | 191 | |
fb0ed453 WX |
192 | info_list = g_new0(SnapshotInfoList, 1); |
193 | info_list->value = info; | |
f364ec65 WX |
194 | |
195 | /* XXX: waiting for the qapi to support qemu-queue.h types */ | |
196 | if (!cur_item) { | |
fb0ed453 | 197 | head = cur_item = info_list; |
f364ec65 WX |
198 | } else { |
199 | cur_item->next = info_list; | |
200 | cur_item = info_list; | |
201 | } | |
202 | ||
203 | } | |
204 | ||
205 | g_free(sn_tab); | |
fb0ed453 WX |
206 | *p_list = head; |
207 | return 0; | |
f364ec65 WX |
208 | } |
209 | ||
43526ec8 WX |
210 | /** |
211 | * bdrv_query_image_info: | |
212 | * @bs: block device to examine | |
213 | * @p_info: location to store image information | |
214 | * @errp: location to store error information | |
215 | * | |
553a7e87 WX |
216 | * Store "flat" image information in @p_info. |
217 | * | |
218 | * "Flat" means it does *not* query backing image information, | |
219 | * i.e. (*pinfo)->has_backing_image will be set to false and | |
220 | * (*pinfo)->backing_image to NULL even when the image does in fact have | |
221 | * a backing image. | |
222 | * | |
43526ec8 WX |
223 | * @p_info will be set only on success. On error, store error in @errp. |
224 | */ | |
225 | void bdrv_query_image_info(BlockDriverState *bs, | |
226 | ImageInfo **p_info, | |
227 | Error **errp) | |
f364ec65 | 228 | { |
52bf1e72 | 229 | int64_t size; |
43526ec8 | 230 | const char *backing_filename; |
f364ec65 | 231 | BlockDriverInfo bdi; |
43526ec8 WX |
232 | int ret; |
233 | Error *err = NULL; | |
52bf1e72 | 234 | ImageInfo *info; |
f364ec65 | 235 | |
1963f8d5 PB |
236 | aio_context_acquire(bdrv_get_aio_context(bs)); |
237 | ||
52bf1e72 MA |
238 | size = bdrv_getlength(bs); |
239 | if (size < 0) { | |
240 | error_setg_errno(errp, -size, "Can't get size of device '%s'", | |
241 | bdrv_get_device_name(bs)); | |
1963f8d5 | 242 | goto out; |
52bf1e72 | 243 | } |
f364ec65 | 244 | |
52bf1e72 | 245 | info = g_new0(ImageInfo, 1); |
43526ec8 | 246 | info->filename = g_strdup(bs->filename); |
f364ec65 | 247 | info->format = g_strdup(bdrv_get_format_name(bs)); |
52bf1e72 | 248 | info->virtual_size = size; |
f364ec65 WX |
249 | info->actual_size = bdrv_get_allocated_file_size(bs); |
250 | info->has_actual_size = info->actual_size >= 0; | |
251 | if (bdrv_is_encrypted(bs)) { | |
252 | info->encrypted = true; | |
253 | info->has_encrypted = true; | |
254 | } | |
255 | if (bdrv_get_info(bs, &bdi) >= 0) { | |
256 | if (bdi.cluster_size != 0) { | |
257 | info->cluster_size = bdi.cluster_size; | |
258 | info->has_cluster_size = true; | |
259 | } | |
260 | info->dirty_flag = bdi.is_dirty; | |
261 | info->has_dirty_flag = true; | |
262 | } | |
eae041fe HR |
263 | info->format_specific = bdrv_get_specific_info(bs); |
264 | info->has_format_specific = info->format_specific != NULL; | |
265 | ||
43526ec8 | 266 | backing_filename = bs->backing_file; |
f364ec65 | 267 | if (backing_filename[0] != '\0') { |
9a29e18f | 268 | char *backing_filename2 = g_malloc0(PATH_MAX); |
f364ec65 WX |
269 | info->backing_filename = g_strdup(backing_filename); |
270 | info->has_backing_filename = true; | |
9a29e18f | 271 | bdrv_get_full_backing_filename(bs, backing_filename2, PATH_MAX, &err); |
9f07429e | 272 | if (err) { |
a5002d53 JS |
273 | /* Can't reconstruct the full backing filename, so we must omit |
274 | * this field and apply a Best Effort to this query. */ | |
564d64bd | 275 | g_free(backing_filename2); |
a5002d53 JS |
276 | backing_filename2 = NULL; |
277 | error_free(err); | |
0fa296eb | 278 | err = NULL; |
9f07429e | 279 | } |
f364ec65 | 280 | |
12dcb1c0 JS |
281 | /* Always report the full_backing_filename if present, even if it's the |
282 | * same as backing_filename. That they are same is useful info. */ | |
283 | if (backing_filename2) { | |
284 | info->full_backing_filename = g_strdup(backing_filename2); | |
f364ec65 WX |
285 | info->has_full_backing_filename = true; |
286 | } | |
287 | ||
288 | if (bs->backing_format[0]) { | |
289 | info->backing_filename_format = g_strdup(bs->backing_format); | |
290 | info->has_backing_filename_format = true; | |
291 | } | |
564d64bd | 292 | g_free(backing_filename2); |
f364ec65 | 293 | } |
43526ec8 WX |
294 | |
295 | ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err); | |
296 | switch (ret) { | |
297 | case 0: | |
298 | if (info->snapshots) { | |
299 | info->has_snapshots = true; | |
300 | } | |
301 | break; | |
302 | /* recoverable error */ | |
303 | case -ENOMEDIUM: | |
304 | case -ENOTSUP: | |
305 | error_free(err); | |
306 | break; | |
307 | default: | |
308 | error_propagate(errp, err); | |
309 | qapi_free_ImageInfo(info); | |
1963f8d5 | 310 | goto out; |
43526ec8 WX |
311 | } |
312 | ||
313 | *p_info = info; | |
1963f8d5 PB |
314 | |
315 | out: | |
316 | aio_context_release(bdrv_get_aio_context(bs)); | |
f364ec65 WX |
317 | } |
318 | ||
553a7e87 | 319 | /* @p_info will be set only on success. */ |
d829a211 MA |
320 | static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, |
321 | Error **errp) | |
f364ec65 WX |
322 | { |
323 | BlockInfo *info = g_malloc0(sizeof(*info)); | |
d829a211 | 324 | BlockDriverState *bs = blk_bs(blk); |
d829a211 | 325 | info->device = g_strdup(blk_name(blk)); |
f364ec65 | 326 | info->type = g_strdup("unknown"); |
a7f53e26 MA |
327 | info->locked = blk_dev_is_medium_locked(blk); |
328 | info->removable = blk_dev_has_removable_media(blk); | |
f364ec65 | 329 | |
327032ce | 330 | if (blk_dev_has_tray(blk)) { |
f364ec65 | 331 | info->has_tray_open = true; |
a7f53e26 | 332 | info->tray_open = blk_dev_is_tray_open(blk); |
f364ec65 WX |
333 | } |
334 | ||
373340b2 | 335 | if (blk_iostatus_is_enabled(blk)) { |
f364ec65 | 336 | info->has_io_status = true; |
373340b2 | 337 | info->io_status = blk_iostatus(blk); |
f364ec65 WX |
338 | } |
339 | ||
5433c24f | 340 | if (bs && !QLIST_EMPTY(&bs->dirty_bitmaps)) { |
21b56835 FZ |
341 | info->has_dirty_bitmaps = true; |
342 | info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs); | |
343 | } | |
344 | ||
5433c24f | 345 | if (bs && bs->drv) { |
f364ec65 | 346 | info->has_inserted = true; |
c83f9fba | 347 | info->inserted = bdrv_block_device_info(blk, bs, errp); |
d5a8ee60 AG |
348 | if (info->inserted == NULL) { |
349 | goto err; | |
553a7e87 | 350 | } |
f364ec65 | 351 | } |
553a7e87 WX |
352 | |
353 | *p_info = info; | |
354 | return; | |
355 | ||
356 | err: | |
357 | qapi_free_BlockInfo(info); | |
f364ec65 WX |
358 | } |
359 | ||
c21cc6ca KW |
360 | static BlockStats *bdrv_query_stats(BlockBackend *blk, |
361 | const BlockDriverState *bs, | |
b07363a1 KW |
362 | bool query_backing); |
363 | ||
54302156 | 364 | static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk) |
2b77e60a KW |
365 | { |
366 | BlockAcctStats *stats = blk_get_stats(blk); | |
367 | BlockAcctTimedStats *ts = NULL; | |
368 | ||
54302156 HR |
369 | ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ]; |
370 | ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE]; | |
371 | ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ]; | |
372 | ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE]; | |
2b77e60a | 373 | |
54302156 HR |
374 | ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ]; |
375 | ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE]; | |
376 | ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH]; | |
2b77e60a | 377 | |
54302156 HR |
378 | ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ]; |
379 | ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE]; | |
380 | ds->invalid_flush_operations = | |
2b77e60a KW |
381 | stats->invalid_ops[BLOCK_ACCT_FLUSH]; |
382 | ||
54302156 HR |
383 | ds->rd_merged = stats->merged[BLOCK_ACCT_READ]; |
384 | ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE]; | |
385 | ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH]; | |
386 | ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE]; | |
387 | ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ]; | |
388 | ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH]; | |
2b77e60a | 389 | |
54302156 HR |
390 | ds->has_idle_time_ns = stats->last_access_time_ns > 0; |
391 | if (ds->has_idle_time_ns) { | |
392 | ds->idle_time_ns = block_acct_idle_time_ns(stats); | |
2b77e60a KW |
393 | } |
394 | ||
54302156 HR |
395 | ds->account_invalid = stats->account_invalid; |
396 | ds->account_failed = stats->account_failed; | |
2b77e60a KW |
397 | |
398 | while ((ts = block_acct_interval_next(stats, ts))) { | |
399 | BlockDeviceTimedStatsList *timed_stats = | |
400 | g_malloc0(sizeof(*timed_stats)); | |
401 | BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats)); | |
54302156 | 402 | timed_stats->next = ds->timed_stats; |
2b77e60a | 403 | timed_stats->value = dev_stats; |
54302156 | 404 | ds->timed_stats = timed_stats; |
2b77e60a KW |
405 | |
406 | TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ]; | |
407 | TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE]; | |
408 | TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH]; | |
409 | ||
410 | dev_stats->interval_length = ts->interval_length; | |
411 | ||
412 | dev_stats->min_rd_latency_ns = timed_average_min(rd); | |
413 | dev_stats->max_rd_latency_ns = timed_average_max(rd); | |
414 | dev_stats->avg_rd_latency_ns = timed_average_avg(rd); | |
415 | ||
416 | dev_stats->min_wr_latency_ns = timed_average_min(wr); | |
417 | dev_stats->max_wr_latency_ns = timed_average_max(wr); | |
418 | dev_stats->avg_wr_latency_ns = timed_average_avg(wr); | |
419 | ||
420 | dev_stats->min_flush_latency_ns = timed_average_min(fl); | |
421 | dev_stats->max_flush_latency_ns = timed_average_max(fl); | |
422 | dev_stats->avg_flush_latency_ns = timed_average_avg(fl); | |
423 | ||
424 | dev_stats->avg_rd_queue_depth = | |
425 | block_acct_queue_depth(ts, BLOCK_ACCT_READ); | |
426 | dev_stats->avg_wr_queue_depth = | |
427 | block_acct_queue_depth(ts, BLOCK_ACCT_WRITE); | |
428 | } | |
429 | } | |
430 | ||
b07363a1 KW |
431 | static void bdrv_query_bds_stats(BlockStats *s, const BlockDriverState *bs, |
432 | bool query_backing) | |
f364ec65 | 433 | { |
4875a779 FZ |
434 | if (bdrv_get_node_name(bs)[0]) { |
435 | s->has_node_name = true; | |
436 | s->node_name = g_strdup(bdrv_get_node_name(bs)); | |
437 | } | |
438 | ||
53d8f9d8 HR |
439 | s->stats->wr_highest_offset = bs->wr_highest_offset; |
440 | ||
f364ec65 WX |
441 | if (bs->file) { |
442 | s->has_parent = true; | |
c21cc6ca | 443 | s->parent = bdrv_query_stats(NULL, bs->file->bs, query_backing); |
f364ec65 WX |
444 | } |
445 | ||
760e0063 | 446 | if (query_backing && bs->backing) { |
c8059b97 | 447 | s->has_backing = true; |
c21cc6ca | 448 | s->backing = bdrv_query_stats(NULL, bs->backing->bs, query_backing); |
c8059b97 FZ |
449 | } |
450 | ||
b07363a1 KW |
451 | } |
452 | ||
c21cc6ca KW |
453 | static BlockStats *bdrv_query_stats(BlockBackend *blk, |
454 | const BlockDriverState *bs, | |
b07363a1 KW |
455 | bool query_backing) |
456 | { | |
457 | BlockStats *s; | |
458 | ||
459 | s = g_malloc0(sizeof(*s)); | |
460 | s->stats = g_malloc0(sizeof(*s->stats)); | |
461 | ||
c21cc6ca | 462 | if (blk) { |
0e8f44be HR |
463 | s->has_device = true; |
464 | s->device = g_strdup(blk_name(blk)); | |
54302156 | 465 | bdrv_query_blk_stats(s->stats, blk); |
c21cc6ca KW |
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 | 654 | bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); |
853ccfed PX |
655 | func_fprintf(f, "%*s[%i]:%c", indentation * 4, "", i, |
656 | composite ? '\n' : ' '); | |
a8d8ecb7 HR |
657 | dump_qobject(func_fprintf, f, indentation + 1, entry->value); |
658 | if (!composite) { | |
659 | func_fprintf(f, "\n"); | |
660 | } | |
661 | } | |
662 | } | |
663 | ||
664 | static void dump_qdict(fprintf_function func_fprintf, void *f, int indentation, | |
665 | QDict *dict) | |
666 | { | |
667 | const QDictEntry *entry; | |
668 | ||
669 | for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { | |
1310a3d3 | 670 | QType type = qobject_type(entry->value); |
a8d8ecb7 | 671 | bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); |
5eda6227 | 672 | char *key = g_malloc(strlen(entry->key) + 1); |
a8d8ecb7 HR |
673 | int i; |
674 | ||
675 | /* replace dashes with spaces in key (variable) names */ | |
676 | for (i = 0; entry->key[i]; i++) { | |
677 | key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; | |
678 | } | |
679 | key[i] = 0; | |
853ccfed PX |
680 | func_fprintf(f, "%*s%s:%c", indentation * 4, "", key, |
681 | composite ? '\n' : ' '); | |
a8d8ecb7 HR |
682 | dump_qobject(func_fprintf, f, indentation + 1, entry->value); |
683 | if (!composite) { | |
684 | func_fprintf(f, "\n"); | |
685 | } | |
5eda6227 | 686 | g_free(key); |
a8d8ecb7 HR |
687 | } |
688 | } | |
689 | ||
690 | void bdrv_image_info_specific_dump(fprintf_function func_fprintf, void *f, | |
691 | ImageInfoSpecific *info_spec) | |
692 | { | |
a8d8ecb7 | 693 | QObject *obj, *data; |
3b098d56 | 694 | Visitor *v = qmp_output_visitor_new(&obj); |
a8d8ecb7 | 695 | |
3b098d56 EB |
696 | visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort); |
697 | visit_complete(v, &obj); | |
a8d8ecb7 HR |
698 | assert(qobject_type(obj) == QTYPE_QDICT); |
699 | data = qdict_get(qobject_to_qdict(obj), "data"); | |
700 | dump_qobject(func_fprintf, f, 1, data); | |
3ac2f2f7 | 701 | qobject_decref(obj); |
3b098d56 | 702 | visit_free(v); |
a8d8ecb7 HR |
703 | } |
704 | ||
5b917044 WX |
705 | void bdrv_image_info_dump(fprintf_function func_fprintf, void *f, |
706 | ImageInfo *info) | |
f364ec65 WX |
707 | { |
708 | char size_buf[128], dsize_buf[128]; | |
709 | if (!info->has_actual_size) { | |
710 | snprintf(dsize_buf, sizeof(dsize_buf), "unavailable"); | |
711 | } else { | |
712 | get_human_readable_size(dsize_buf, sizeof(dsize_buf), | |
713 | info->actual_size); | |
714 | } | |
715 | get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size); | |
5b917044 WX |
716 | func_fprintf(f, |
717 | "image: %s\n" | |
718 | "file format: %s\n" | |
719 | "virtual size: %s (%" PRId64 " bytes)\n" | |
720 | "disk size: %s\n", | |
721 | info->filename, info->format, size_buf, | |
722 | info->virtual_size, | |
723 | dsize_buf); | |
f364ec65 WX |
724 | |
725 | if (info->has_encrypted && info->encrypted) { | |
5b917044 | 726 | func_fprintf(f, "encrypted: yes\n"); |
f364ec65 WX |
727 | } |
728 | ||
729 | if (info->has_cluster_size) { | |
5b917044 WX |
730 | func_fprintf(f, "cluster_size: %" PRId64 "\n", |
731 | info->cluster_size); | |
f364ec65 WX |
732 | } |
733 | ||
734 | if (info->has_dirty_flag && info->dirty_flag) { | |
5b917044 | 735 | func_fprintf(f, "cleanly shut down: no\n"); |
f364ec65 WX |
736 | } |
737 | ||
738 | if (info->has_backing_filename) { | |
5b917044 | 739 | func_fprintf(f, "backing file: %s", info->backing_filename); |
5c9d9ca5 JS |
740 | if (!info->has_full_backing_filename) { |
741 | func_fprintf(f, " (cannot determine actual path)"); | |
742 | } else if (strcmp(info->backing_filename, | |
743 | info->full_backing_filename) != 0) { | |
5b917044 | 744 | func_fprintf(f, " (actual path: %s)", info->full_backing_filename); |
f364ec65 | 745 | } |
5b917044 | 746 | func_fprintf(f, "\n"); |
f364ec65 | 747 | if (info->has_backing_filename_format) { |
5b917044 WX |
748 | func_fprintf(f, "backing file format: %s\n", |
749 | info->backing_filename_format); | |
f364ec65 WX |
750 | } |
751 | } | |
752 | ||
753 | if (info->has_snapshots) { | |
754 | SnapshotInfoList *elem; | |
f364ec65 | 755 | |
5b917044 WX |
756 | func_fprintf(f, "Snapshot list:\n"); |
757 | bdrv_snapshot_dump(func_fprintf, f, NULL); | |
758 | func_fprintf(f, "\n"); | |
f364ec65 WX |
759 | |
760 | /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but | |
761 | * we convert to the block layer's native QEMUSnapshotInfo for now. | |
762 | */ | |
763 | for (elem = info->snapshots; elem; elem = elem->next) { | |
764 | QEMUSnapshotInfo sn = { | |
765 | .vm_state_size = elem->value->vm_state_size, | |
766 | .date_sec = elem->value->date_sec, | |
767 | .date_nsec = elem->value->date_nsec, | |
768 | .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + | |
769 | elem->value->vm_clock_nsec, | |
770 | }; | |
771 | ||
772 | pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); | |
773 | pstrcpy(sn.name, sizeof(sn.name), elem->value->name); | |
5b917044 WX |
774 | bdrv_snapshot_dump(func_fprintf, f, &sn); |
775 | func_fprintf(f, "\n"); | |
f364ec65 WX |
776 | } |
777 | } | |
a8d8ecb7 HR |
778 | |
779 | if (info->has_format_specific) { | |
780 | func_fprintf(f, "Format specific information:\n"); | |
781 | bdrv_image_info_specific_dump(func_fprintf, f, info->format_specific); | |
782 | } | |
f364ec65 | 783 | } |