Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 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 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Philippe Mathieu-Daudé | 31e4041 | 2019-09-03 14:05:55 +0200 | [diff] [blame] | 26 | #include "qemu/cutils.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 27 | #include "block/qapi.h" |
| 28 | #include "block/block_int.h" |
Markus Armbruster | e2c1c34 | 2022-12-21 14:35:49 +0100 | [diff] [blame] | 29 | #include "block/dirty-bitmap.h" |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 30 | #include "block/throttle-groups.h" |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 31 | #include "block/write-threshold.h" |
Markus Armbruster | e688df6 | 2018-02-01 12:18:31 +0100 | [diff] [blame] | 32 | #include "qapi/error.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 33 | #include "qapi/qapi-commands-block-core.h" |
Daniel P. Berrange | b3db211 | 2016-09-30 15:45:27 +0100 | [diff] [blame] | 34 | #include "qapi/qobject-output-visitor.h" |
Markus Armbruster | 9af2398 | 2018-02-11 10:36:01 +0100 | [diff] [blame] | 35 | #include "qapi/qapi-visit-block-core.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 36 | #include "qapi/qmp/qbool.h" |
Markus Armbruster | 452fcdb | 2018-02-01 12:18:39 +0100 | [diff] [blame] | 37 | #include "qapi/qmp/qdict.h" |
Markus Armbruster | 47e6b29 | 2018-02-01 12:18:38 +0100 | [diff] [blame] | 38 | #include "qapi/qmp/qlist.h" |
Markus Armbruster | 15280c3 | 2018-02-01 12:18:36 +0100 | [diff] [blame] | 39 | #include "qapi/qmp/qnum.h" |
Markus Armbruster | 6b67395 | 2018-02-01 12:18:35 +0100 | [diff] [blame] | 40 | #include "qapi/qmp/qstring.h" |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 41 | #include "qemu/qemu-print.h" |
Markus Armbruster | d829a21 | 2014-10-07 13:59:23 +0200 | [diff] [blame] | 42 | #include "sysemu/block-backend.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 43 | |
Kevin Wolf | c83f9fb | 2016-03-03 11:37:48 +0100 | [diff] [blame] | 44 | BlockDeviceInfo *bdrv_block_device_info(BlockBackend *blk, |
Peter Krempa | facda54 | 2020-01-20 09:50:49 +0100 | [diff] [blame] | 45 | BlockDriverState *bs, |
| 46 | bool flat, |
| 47 | Error **errp) |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 48 | { |
Alberto Garcia | d5a8ee6 | 2015-04-17 14:52:43 +0300 | [diff] [blame] | 49 | ImageInfo **p_image_info; |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 50 | ImageInfo *backing_info; |
Max Reitz | 0b877d0 | 2018-08-01 20:34:11 +0200 | [diff] [blame] | 51 | BlockDriverState *bs0, *backing; |
Max Reitz | d470ad4 | 2017-11-10 21:31:09 +0100 | [diff] [blame] | 52 | BlockDeviceInfo *info; |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 53 | ERRP_GUARD(); |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 54 | |
Max Reitz | d470ad4 | 2017-11-10 21:31:09 +0100 | [diff] [blame] | 55 | if (!bs->drv) { |
| 56 | error_setg(errp, "Block device %s is ejected", bs->node_name); |
| 57 | return NULL; |
| 58 | } |
| 59 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 60 | bdrv_refresh_filename(bs); |
| 61 | |
Max Reitz | d470ad4 | 2017-11-10 21:31:09 +0100 | [diff] [blame] | 62 | info = g_malloc0(sizeof(*info)); |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 63 | info->file = g_strdup(bs->filename); |
Vladimir Sementsov-Ogievskiy | 307261b | 2021-05-27 18:40:54 +0300 | [diff] [blame] | 64 | info->ro = bdrv_is_read_only(bs); |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 65 | info->drv = g_strdup(bs->drv->format_name); |
| 66 | info->encrypted = bs->encrypted; |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 67 | |
Kevin Wolf | 9e193c5 | 2014-05-22 13:28:45 +0200 | [diff] [blame] | 68 | info->cache = g_new(BlockdevCacheInfo, 1); |
| 69 | *info->cache = (BlockdevCacheInfo) { |
Kevin Wolf | c83f9fb | 2016-03-03 11:37:48 +0100 | [diff] [blame] | 70 | .writeback = blk ? blk_enable_write_cache(blk) : true, |
Kevin Wolf | 9e193c5 | 2014-05-22 13:28:45 +0200 | [diff] [blame] | 71 | .direct = !!(bs->open_flags & BDRV_O_NOCACHE), |
| 72 | .no_flush = !!(bs->open_flags & BDRV_O_NO_FLUSH), |
| 73 | }; |
| 74 | |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 75 | if (bs->node_name[0]) { |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 76 | info->node_name = g_strdup(bs->node_name); |
| 77 | } |
| 78 | |
Max Reitz | 0b877d0 | 2018-08-01 20:34:11 +0200 | [diff] [blame] | 79 | backing = bdrv_cow_bs(bs); |
| 80 | if (backing) { |
Max Reitz | 0b877d0 | 2018-08-01 20:34:11 +0200 | [diff] [blame] | 81 | info->backing_file = g_strdup(backing->filename); |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Vladimir Sementsov-Ogievskiy | 590a63d | 2019-07-29 16:35:56 -0400 | [diff] [blame] | 84 | if (!QLIST_EMPTY(&bs->dirty_bitmaps)) { |
| 85 | info->has_dirty_bitmaps = true; |
| 86 | info->dirty_bitmaps = bdrv_query_dirty_bitmaps(bs); |
| 87 | } |
| 88 | |
Peter Lieven | 465bee1 | 2014-05-18 00:58:19 +0200 | [diff] [blame] | 89 | info->detect_zeroes = bs->detect_zeroes; |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 90 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 91 | if (blk && blk_get_public(blk)->throttle_group_member.throttle_state) { |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 92 | ThrottleConfig cfg; |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 93 | BlockBackendPublic *blkp = blk_get_public(blk); |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 94 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 95 | throttle_group_get_config(&blkp->throttle_group_member, &cfg); |
Alberto Garcia | 76f4afb | 2015-06-08 18:17:44 +0200 | [diff] [blame] | 96 | |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 97 | info->bps = cfg.buckets[THROTTLE_BPS_TOTAL].avg; |
| 98 | info->bps_rd = cfg.buckets[THROTTLE_BPS_READ].avg; |
| 99 | info->bps_wr = cfg.buckets[THROTTLE_BPS_WRITE].avg; |
| 100 | |
| 101 | info->iops = cfg.buckets[THROTTLE_OPS_TOTAL].avg; |
| 102 | info->iops_rd = cfg.buckets[THROTTLE_OPS_READ].avg; |
| 103 | info->iops_wr = cfg.buckets[THROTTLE_OPS_WRITE].avg; |
| 104 | |
| 105 | info->has_bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; |
| 106 | info->bps_max = cfg.buckets[THROTTLE_BPS_TOTAL].max; |
| 107 | info->has_bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; |
| 108 | info->bps_rd_max = cfg.buckets[THROTTLE_BPS_READ].max; |
| 109 | info->has_bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; |
| 110 | info->bps_wr_max = cfg.buckets[THROTTLE_BPS_WRITE].max; |
| 111 | |
| 112 | info->has_iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; |
| 113 | info->iops_max = cfg.buckets[THROTTLE_OPS_TOTAL].max; |
| 114 | info->has_iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; |
| 115 | info->iops_rd_max = cfg.buckets[THROTTLE_OPS_READ].max; |
| 116 | info->has_iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; |
| 117 | info->iops_wr_max = cfg.buckets[THROTTLE_OPS_WRITE].max; |
| 118 | |
Alberto Garcia | 398befd | 2016-02-18 12:27:04 +0200 | [diff] [blame] | 119 | info->has_bps_max_length = info->has_bps_max; |
| 120 | info->bps_max_length = |
| 121 | cfg.buckets[THROTTLE_BPS_TOTAL].burst_length; |
| 122 | info->has_bps_rd_max_length = info->has_bps_rd_max; |
| 123 | info->bps_rd_max_length = |
| 124 | cfg.buckets[THROTTLE_BPS_READ].burst_length; |
| 125 | info->has_bps_wr_max_length = info->has_bps_wr_max; |
| 126 | info->bps_wr_max_length = |
| 127 | cfg.buckets[THROTTLE_BPS_WRITE].burst_length; |
| 128 | |
| 129 | info->has_iops_max_length = info->has_iops_max; |
| 130 | info->iops_max_length = |
| 131 | cfg.buckets[THROTTLE_OPS_TOTAL].burst_length; |
| 132 | info->has_iops_rd_max_length = info->has_iops_rd_max; |
| 133 | info->iops_rd_max_length = |
| 134 | cfg.buckets[THROTTLE_OPS_READ].burst_length; |
| 135 | info->has_iops_wr_max_length = info->has_iops_wr_max; |
| 136 | info->iops_wr_max_length = |
| 137 | cfg.buckets[THROTTLE_OPS_WRITE].burst_length; |
| 138 | |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 139 | info->has_iops_size = cfg.op_size; |
| 140 | info->iops_size = cfg.op_size; |
Alberto Garcia | b8fe169 | 2015-06-08 18:17:46 +0200 | [diff] [blame] | 141 | |
Manos Pitsidianakis | 022cdc9 | 2017-08-25 16:20:23 +0300 | [diff] [blame] | 142 | info->group = |
| 143 | g_strdup(throttle_group_get_name(&blkp->throttle_group_member)); |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Francesco Romani | e246211 | 2015-01-12 14:11:13 +0100 | [diff] [blame] | 146 | info->write_threshold = bdrv_write_threshold_get(bs); |
| 147 | |
Alberto Garcia | d5a8ee6 | 2015-04-17 14:52:43 +0300 | [diff] [blame] | 148 | bs0 = bs; |
| 149 | p_image_info = &info->image; |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 150 | info->backing_file_depth = 0; |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 151 | |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 152 | /* |
| 153 | * Skip automatically inserted nodes that the user isn't aware of for |
| 154 | * query-block (blk != NULL), but not for query-named-block-nodes |
| 155 | */ |
| 156 | bdrv_query_image_info(bs0, p_image_info, flat, blk != NULL, errp); |
| 157 | if (*errp) { |
| 158 | qapi_free_BlockDeviceInfo(info); |
| 159 | return NULL; |
| 160 | } |
Peter Krempa | facda54 | 2020-01-20 09:50:49 +0100 | [diff] [blame] | 161 | |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 162 | backing_info = info->image->backing_image; |
| 163 | while (backing_info) { |
| 164 | info->backing_file_depth++; |
| 165 | backing_info = backing_info->backing_image; |
Alberto Garcia | d5a8ee6 | 2015-04-17 14:52:43 +0300 | [diff] [blame] | 166 | } |
| 167 | |
Benoît Canet | c13163f | 2014-01-23 21:31:34 +0100 | [diff] [blame] | 168 | return info; |
| 169 | } |
| 170 | |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 171 | /* |
| 172 | * Returns 0 on success, with *p_list either set to describe snapshot |
| 173 | * information, or NULL because there are no snapshots. Returns -errno on |
| 174 | * error, with *p_list untouched. |
| 175 | */ |
| 176 | int bdrv_query_snapshot_info_list(BlockDriverState *bs, |
| 177 | SnapshotInfoList **p_list, |
| 178 | Error **errp) |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 179 | { |
| 180 | int i, sn_count; |
| 181 | QEMUSnapshotInfo *sn_tab = NULL; |
Eric Blake | 95b3a8c | 2021-01-13 16:10:13 -0600 | [diff] [blame] | 182 | SnapshotInfoList *head = NULL, **tail = &head; |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 183 | SnapshotInfo *info; |
| 184 | |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 185 | sn_count = bdrv_snapshot_list(bs, &sn_tab); |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 186 | if (sn_count < 0) { |
| 187 | const char *dev = bdrv_get_device_name(bs); |
| 188 | switch (sn_count) { |
| 189 | case -ENOMEDIUM: |
| 190 | error_setg(errp, "Device '%s' is not inserted", dev); |
| 191 | break; |
| 192 | case -ENOTSUP: |
| 193 | error_setg(errp, |
| 194 | "Device '%s' does not support internal snapshots", |
| 195 | dev); |
| 196 | break; |
| 197 | default: |
| 198 | error_setg_errno(errp, -sn_count, |
| 199 | "Can't list snapshots of device '%s'", dev); |
| 200 | break; |
| 201 | } |
| 202 | return sn_count; |
| 203 | } |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 204 | |
| 205 | for (i = 0; i < sn_count; i++) { |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 206 | info = g_new0(SnapshotInfo, 1); |
| 207 | info->id = g_strdup(sn_tab[i].id_str); |
| 208 | info->name = g_strdup(sn_tab[i].name); |
| 209 | info->vm_state_size = sn_tab[i].vm_state_size; |
| 210 | info->date_sec = sn_tab[i].date_sec; |
| 211 | info->date_nsec = sn_tab[i].date_nsec; |
| 212 | info->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; |
| 213 | info->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; |
Pavel Dovgalyuk | b39847a | 2020-10-03 20:13:08 +0300 | [diff] [blame] | 214 | info->icount = sn_tab[i].icount; |
| 215 | info->has_icount = sn_tab[i].icount != -1ULL; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 216 | |
Eric Blake | 95b3a8c | 2021-01-13 16:10:13 -0600 | [diff] [blame] | 217 | QAPI_LIST_APPEND(tail, info); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | g_free(sn_tab); |
Wenchao Xia | fb0ed45 | 2013-06-06 12:27:57 +0800 | [diff] [blame] | 221 | *p_list = head; |
| 222 | return 0; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 223 | } |
| 224 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 225 | /** |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 226 | * Helper function for other query info functions. Store information about @bs |
| 227 | * in @info, setting @errp on error. |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 228 | */ |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 229 | static void bdrv_do_query_node_info(BlockDriverState *bs, |
| 230 | BlockNodeInfo *info, |
| 231 | Error **errp) |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 232 | { |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 233 | int64_t size; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 234 | const char *backing_filename; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 235 | BlockDriverInfo bdi; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 236 | int ret; |
| 237 | Error *err = NULL; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 238 | |
Paolo Bonzini | 1963f8d | 2015-12-23 11:48:23 +0100 | [diff] [blame] | 239 | aio_context_acquire(bdrv_get_aio_context(bs)); |
| 240 | |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 241 | size = bdrv_getlength(bs); |
| 242 | if (size < 0) { |
Fam Zheng | 9adceb0 | 2017-01-19 21:07:58 +0800 | [diff] [blame] | 243 | error_setg_errno(errp, -size, "Can't get image size '%s'", |
| 244 | bs->exact_filename); |
Paolo Bonzini | 1963f8d | 2015-12-23 11:48:23 +0100 | [diff] [blame] | 245 | goto out; |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 246 | } |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 247 | |
Max Reitz | f30c66b | 2019-02-01 20:29:05 +0100 | [diff] [blame] | 248 | bdrv_refresh_filename(bs); |
| 249 | |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 250 | info->filename = g_strdup(bs->filename); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 251 | info->format = g_strdup(bdrv_get_format_name(bs)); |
Markus Armbruster | 52bf1e7 | 2014-06-26 13:23:25 +0200 | [diff] [blame] | 252 | info->virtual_size = size; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 253 | info->actual_size = bdrv_get_allocated_file_size(bs); |
| 254 | info->has_actual_size = info->actual_size >= 0; |
Max Reitz | 8b8277c | 2019-06-12 17:08:20 +0200 | [diff] [blame] | 255 | if (bs->encrypted) { |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 256 | info->encrypted = true; |
| 257 | info->has_encrypted = true; |
| 258 | } |
| 259 | if (bdrv_get_info(bs, &bdi) >= 0) { |
| 260 | if (bdi.cluster_size != 0) { |
| 261 | info->cluster_size = bdi.cluster_size; |
| 262 | info->has_cluster_size = true; |
| 263 | } |
| 264 | info->dirty_flag = bdi.is_dirty; |
| 265 | info->has_dirty_flag = true; |
| 266 | } |
Andrey Shinkevich | 1bf6e9c | 2019-02-08 18:06:06 +0300 | [diff] [blame] | 267 | info->format_specific = bdrv_get_specific_info(bs, &err); |
| 268 | if (err) { |
| 269 | error_propagate(errp, err); |
Andrey Shinkevich | 1bf6e9c | 2019-02-08 18:06:06 +0300 | [diff] [blame] | 270 | goto out; |
| 271 | } |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 272 | backing_filename = bs->backing_file; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 273 | if (backing_filename[0] != '\0') { |
Max Reitz | 6b6833c | 2019-02-01 20:29:15 +0100 | [diff] [blame] | 274 | char *backing_filename2; |
Max Reitz | 0b877d0 | 2018-08-01 20:34:11 +0200 | [diff] [blame] | 275 | |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 276 | info->backing_filename = g_strdup(backing_filename); |
Max Reitz | 6b6833c | 2019-02-01 20:29:15 +0100 | [diff] [blame] | 277 | backing_filename2 = bdrv_get_full_backing_filename(bs, NULL); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 278 | |
John Snow | 12dcb1c | 2015-12-14 14:55:13 -0500 | [diff] [blame] | 279 | /* Always report the full_backing_filename if present, even if it's the |
| 280 | * same as backing_filename. That they are same is useful info. */ |
| 281 | if (backing_filename2) { |
| 282 | info->full_backing_filename = g_strdup(backing_filename2); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | if (bs->backing_format[0]) { |
| 286 | info->backing_filename_format = g_strdup(bs->backing_format); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 287 | } |
Jeff Cody | 564d64b | 2015-01-22 08:03:27 -0500 | [diff] [blame] | 288 | g_free(backing_filename2); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 289 | } |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 290 | |
| 291 | ret = bdrv_query_snapshot_info_list(bs, &info->snapshots, &err); |
| 292 | switch (ret) { |
| 293 | case 0: |
| 294 | if (info->snapshots) { |
| 295 | info->has_snapshots = true; |
| 296 | } |
| 297 | break; |
| 298 | /* recoverable error */ |
| 299 | case -ENOMEDIUM: |
| 300 | case -ENOTSUP: |
| 301 | error_free(err); |
| 302 | break; |
| 303 | default: |
| 304 | error_propagate(errp, err); |
Paolo Bonzini | 1963f8d | 2015-12-23 11:48:23 +0100 | [diff] [blame] | 305 | goto out; |
Wenchao Xia | 43526ec | 2013-06-06 12:27:58 +0800 | [diff] [blame] | 306 | } |
| 307 | |
Paolo Bonzini | 1963f8d | 2015-12-23 11:48:23 +0100 | [diff] [blame] | 308 | out: |
| 309 | aio_context_release(bdrv_get_aio_context(bs)); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 310 | } |
| 311 | |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 312 | /** |
| 313 | * bdrv_query_block_node_info: |
| 314 | * @bs: block node to examine |
| 315 | * @p_info: location to store node information |
| 316 | * @errp: location to store error information |
| 317 | * |
| 318 | * Store image information about @bs in @p_info. |
| 319 | * |
| 320 | * @p_info will be set only on success. On error, store error in @errp. |
| 321 | */ |
| 322 | void bdrv_query_block_node_info(BlockDriverState *bs, |
| 323 | BlockNodeInfo **p_info, |
| 324 | Error **errp) |
| 325 | { |
| 326 | BlockNodeInfo *info; |
| 327 | ERRP_GUARD(); |
| 328 | |
| 329 | info = g_new0(BlockNodeInfo, 1); |
| 330 | bdrv_do_query_node_info(bs, info, errp); |
| 331 | if (*errp) { |
| 332 | qapi_free_BlockNodeInfo(info); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | *p_info = info; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * bdrv_query_image_info: |
| 341 | * @bs: block node to examine |
| 342 | * @p_info: location to store image information |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 343 | * @flat: skip backing node information |
| 344 | * @skip_implicit_filters: skip implicit filters in the backing chain |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 345 | * @errp: location to store error information |
| 346 | * |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 347 | * Store image information in @p_info, potentially recursively covering the |
| 348 | * backing chain. |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 349 | * |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 350 | * If @flat is true, do not query backing image information, i.e. |
| 351 | * (*p_info)->has_backing_image will be set to false and |
| 352 | * (*p_info)->backing_image to NULL even when the image does in fact have a |
| 353 | * backing image. |
| 354 | * |
| 355 | * If @skip_implicit_filters is true, implicit filter nodes in the backing chain |
| 356 | * will be skipped when querying backing image information. |
| 357 | * (@skip_implicit_filters is ignored when @flat is true.) |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 358 | * |
| 359 | * @p_info will be set only on success. On error, store error in @errp. |
| 360 | */ |
| 361 | void bdrv_query_image_info(BlockDriverState *bs, |
| 362 | ImageInfo **p_info, |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 363 | bool flat, |
| 364 | bool skip_implicit_filters, |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 365 | Error **errp) |
| 366 | { |
| 367 | ImageInfo *info; |
| 368 | ERRP_GUARD(); |
| 369 | |
| 370 | info = g_new0(ImageInfo, 1); |
| 371 | bdrv_do_query_node_info(bs, qapi_ImageInfo_base(info), errp); |
| 372 | if (*errp) { |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 373 | goto fail; |
| 374 | } |
| 375 | |
| 376 | if (!flat) { |
| 377 | BlockDriverState *backing; |
| 378 | |
| 379 | /* |
| 380 | * Use any filtered child here (for backwards compatibility to when |
| 381 | * we always took bs->backing, which might be any filtered child). |
| 382 | */ |
| 383 | backing = bdrv_filter_or_cow_bs(bs); |
| 384 | if (skip_implicit_filters) { |
| 385 | backing = bdrv_skip_implicit_filters(backing); |
| 386 | } |
| 387 | |
| 388 | if (backing) { |
| 389 | bdrv_query_image_info(backing, &info->backing_image, false, |
| 390 | skip_implicit_filters, errp); |
| 391 | if (*errp) { |
| 392 | goto fail; |
| 393 | } |
| 394 | } |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | *p_info = info; |
Hanna Reitz | 5d88135 | 2022-06-20 18:26:58 +0200 | [diff] [blame] | 398 | return; |
| 399 | |
| 400 | fail: |
| 401 | assert(*errp); |
| 402 | qapi_free_ImageInfo(info); |
Hanna Reitz | a2085f8 | 2022-06-20 18:26:56 +0200 | [diff] [blame] | 403 | } |
| 404 | |
Hanna Reitz | 6cab339 | 2022-06-20 18:26:59 +0200 | [diff] [blame] | 405 | /** |
| 406 | * bdrv_query_block_graph_info: |
| 407 | * @bs: root node to start from |
| 408 | * @p_info: location to store image information |
| 409 | * @errp: location to store error information |
| 410 | * |
| 411 | * Store image information about the graph starting from @bs in @p_info. |
| 412 | * |
| 413 | * @p_info will be set only on success. On error, store error in @errp. |
| 414 | */ |
| 415 | void bdrv_query_block_graph_info(BlockDriverState *bs, |
| 416 | BlockGraphInfo **p_info, |
| 417 | Error **errp) |
| 418 | { |
| 419 | BlockGraphInfo *info; |
| 420 | BlockChildInfoList **children_list_tail; |
| 421 | BdrvChild *c; |
| 422 | ERRP_GUARD(); |
| 423 | |
| 424 | info = g_new0(BlockGraphInfo, 1); |
| 425 | bdrv_do_query_node_info(bs, qapi_BlockGraphInfo_base(info), errp); |
| 426 | if (*errp) { |
| 427 | goto fail; |
| 428 | } |
| 429 | |
| 430 | children_list_tail = &info->children; |
| 431 | |
| 432 | QLIST_FOREACH(c, &bs->children, next) { |
| 433 | BlockChildInfo *c_info; |
| 434 | |
| 435 | c_info = g_new0(BlockChildInfo, 1); |
| 436 | QAPI_LIST_APPEND(children_list_tail, c_info); |
| 437 | |
| 438 | c_info->name = g_strdup(c->name); |
| 439 | bdrv_query_block_graph_info(c->bs, &c_info->info, errp); |
| 440 | if (*errp) { |
| 441 | goto fail; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | *p_info = info; |
| 446 | return; |
| 447 | |
| 448 | fail: |
| 449 | assert(*errp != NULL); |
| 450 | qapi_free_BlockGraphInfo(info); |
| 451 | } |
| 452 | |
Wenchao Xia | 553a7e8 | 2013-06-06 12:27:59 +0800 | [diff] [blame] | 453 | /* @p_info will be set only on success. */ |
Markus Armbruster | d829a21 | 2014-10-07 13:59:23 +0200 | [diff] [blame] | 454 | static void bdrv_query_info(BlockBackend *blk, BlockInfo **p_info, |
| 455 | Error **errp) |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 456 | { |
| 457 | BlockInfo *info = g_malloc0(sizeof(*info)); |
Markus Armbruster | d829a21 | 2014-10-07 13:59:23 +0200 | [diff] [blame] | 458 | BlockDriverState *bs = blk_bs(blk); |
Kevin Wolf | 46eade7 | 2017-07-11 13:27:38 +0200 | [diff] [blame] | 459 | char *qdev; |
| 460 | |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 461 | /* Skip automatically inserted nodes that the user isn't aware of */ |
Max Reitz | 0a7585d | 2019-06-12 18:23:25 +0200 | [diff] [blame] | 462 | bs = bdrv_skip_implicit_filters(bs); |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 463 | |
Markus Armbruster | d829a21 | 2014-10-07 13:59:23 +0200 | [diff] [blame] | 464 | info->device = g_strdup(blk_name(blk)); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 465 | info->type = g_strdup("unknown"); |
Markus Armbruster | a7f53e2 | 2014-10-07 13:59:25 +0200 | [diff] [blame] | 466 | info->locked = blk_dev_is_medium_locked(blk); |
| 467 | info->removable = blk_dev_has_removable_media(blk); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 468 | |
Kevin Wolf | 46eade7 | 2017-07-11 13:27:38 +0200 | [diff] [blame] | 469 | qdev = blk_get_attached_dev_id(blk); |
| 470 | if (qdev && *qdev) { |
Kevin Wolf | 46eade7 | 2017-07-11 13:27:38 +0200 | [diff] [blame] | 471 | info->qdev = qdev; |
| 472 | } else { |
| 473 | g_free(qdev); |
| 474 | } |
| 475 | |
Max Reitz | 327032c | 2016-01-29 20:49:13 +0100 | [diff] [blame] | 476 | if (blk_dev_has_tray(blk)) { |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 477 | info->has_tray_open = true; |
Markus Armbruster | a7f53e2 | 2014-10-07 13:59:25 +0200 | [diff] [blame] | 478 | info->tray_open = blk_dev_is_tray_open(blk); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 479 | } |
| 480 | |
Max Reitz | 373340b | 2015-10-19 17:53:22 +0200 | [diff] [blame] | 481 | if (blk_iostatus_is_enabled(blk)) { |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 482 | info->has_io_status = true; |
Max Reitz | 373340b | 2015-10-19 17:53:22 +0200 | [diff] [blame] | 483 | info->io_status = blk_iostatus(blk); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 484 | } |
| 485 | |
Max Reitz | 5433c24 | 2015-10-19 17:53:29 +0200 | [diff] [blame] | 486 | if (bs && bs->drv) { |
Peter Krempa | facda54 | 2020-01-20 09:50:49 +0100 | [diff] [blame] | 487 | info->inserted = bdrv_block_device_info(blk, bs, false, errp); |
Alberto Garcia | d5a8ee6 | 2015-04-17 14:52:43 +0300 | [diff] [blame] | 488 | if (info->inserted == NULL) { |
| 489 | goto err; |
Wenchao Xia | 553a7e8 | 2013-06-06 12:27:59 +0800 | [diff] [blame] | 490 | } |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 491 | } |
Wenchao Xia | 553a7e8 | 2013-06-06 12:27:59 +0800 | [diff] [blame] | 492 | |
| 493 | *p_info = info; |
| 494 | return; |
| 495 | |
| 496 | err: |
| 497 | qapi_free_BlockInfo(info); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 498 | } |
| 499 | |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 500 | static uint64List *uint64_list(uint64_t *list, int size) |
| 501 | { |
| 502 | int i; |
| 503 | uint64List *out_list = NULL; |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 504 | uint64List **tail = &out_list; |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 505 | |
| 506 | for (i = 0; i < size; i++) { |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 507 | QAPI_LIST_APPEND(tail, list[i]); |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 508 | } |
| 509 | |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 510 | return out_list; |
| 511 | } |
| 512 | |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 513 | static BlockLatencyHistogramInfo * |
| 514 | bdrv_latency_histogram_stats(BlockLatencyHistogram *hist) |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 515 | { |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 516 | BlockLatencyHistogramInfo *info; |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 517 | |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 518 | if (!hist->bins) { |
| 519 | return NULL; |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 520 | } |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 521 | |
| 522 | info = g_new0(BlockLatencyHistogramInfo, 1); |
| 523 | info->boundaries = uint64_list(hist->boundaries, hist->nbins - 1); |
| 524 | info->bins = uint64_list(hist->bins, hist->nbins); |
| 525 | return info; |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 526 | } |
| 527 | |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 528 | static void bdrv_query_blk_stats(BlockDeviceStats *ds, BlockBackend *blk) |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 529 | { |
| 530 | BlockAcctStats *stats = blk_get_stats(blk); |
| 531 | BlockAcctTimedStats *ts = NULL; |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 532 | BlockLatencyHistogram *hgram; |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 533 | |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 534 | ds->rd_bytes = stats->nr_bytes[BLOCK_ACCT_READ]; |
| 535 | ds->wr_bytes = stats->nr_bytes[BLOCK_ACCT_WRITE]; |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 536 | ds->zone_append_bytes = stats->nr_bytes[BLOCK_ACCT_ZONE_APPEND]; |
Anton Nefedov | 159f85d | 2019-09-23 15:17:30 +0300 | [diff] [blame] | 537 | ds->unmap_bytes = stats->nr_bytes[BLOCK_ACCT_UNMAP]; |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 538 | ds->rd_operations = stats->nr_ops[BLOCK_ACCT_READ]; |
| 539 | ds->wr_operations = stats->nr_ops[BLOCK_ACCT_WRITE]; |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 540 | ds->zone_append_operations = stats->nr_ops[BLOCK_ACCT_ZONE_APPEND]; |
Anton Nefedov | 159f85d | 2019-09-23 15:17:30 +0300 | [diff] [blame] | 541 | ds->unmap_operations = stats->nr_ops[BLOCK_ACCT_UNMAP]; |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 542 | |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 543 | ds->failed_rd_operations = stats->failed_ops[BLOCK_ACCT_READ]; |
| 544 | ds->failed_wr_operations = stats->failed_ops[BLOCK_ACCT_WRITE]; |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 545 | ds->failed_zone_append_operations = |
| 546 | stats->failed_ops[BLOCK_ACCT_ZONE_APPEND]; |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 547 | ds->failed_flush_operations = stats->failed_ops[BLOCK_ACCT_FLUSH]; |
Anton Nefedov | 159f85d | 2019-09-23 15:17:30 +0300 | [diff] [blame] | 548 | ds->failed_unmap_operations = stats->failed_ops[BLOCK_ACCT_UNMAP]; |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 549 | |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 550 | ds->invalid_rd_operations = stats->invalid_ops[BLOCK_ACCT_READ]; |
| 551 | ds->invalid_wr_operations = stats->invalid_ops[BLOCK_ACCT_WRITE]; |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 552 | ds->invalid_zone_append_operations = |
| 553 | stats->invalid_ops[BLOCK_ACCT_ZONE_APPEND]; |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 554 | ds->invalid_flush_operations = |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 555 | stats->invalid_ops[BLOCK_ACCT_FLUSH]; |
Anton Nefedov | 159f85d | 2019-09-23 15:17:30 +0300 | [diff] [blame] | 556 | ds->invalid_unmap_operations = stats->invalid_ops[BLOCK_ACCT_UNMAP]; |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 557 | |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 558 | ds->rd_merged = stats->merged[BLOCK_ACCT_READ]; |
| 559 | ds->wr_merged = stats->merged[BLOCK_ACCT_WRITE]; |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 560 | ds->zone_append_merged = stats->merged[BLOCK_ACCT_ZONE_APPEND]; |
Anton Nefedov | 159f85d | 2019-09-23 15:17:30 +0300 | [diff] [blame] | 561 | ds->unmap_merged = stats->merged[BLOCK_ACCT_UNMAP]; |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 562 | ds->flush_operations = stats->nr_ops[BLOCK_ACCT_FLUSH]; |
| 563 | ds->wr_total_time_ns = stats->total_time_ns[BLOCK_ACCT_WRITE]; |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 564 | ds->zone_append_total_time_ns = |
| 565 | stats->total_time_ns[BLOCK_ACCT_ZONE_APPEND]; |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 566 | ds->rd_total_time_ns = stats->total_time_ns[BLOCK_ACCT_READ]; |
| 567 | ds->flush_total_time_ns = stats->total_time_ns[BLOCK_ACCT_FLUSH]; |
Anton Nefedov | 159f85d | 2019-09-23 15:17:30 +0300 | [diff] [blame] | 568 | ds->unmap_total_time_ns = stats->total_time_ns[BLOCK_ACCT_UNMAP]; |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 569 | |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 570 | ds->has_idle_time_ns = stats->last_access_time_ns > 0; |
| 571 | if (ds->has_idle_time_ns) { |
| 572 | ds->idle_time_ns = block_acct_idle_time_ns(stats); |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Max Reitz | 5430215 | 2016-03-02 18:31:09 +0100 | [diff] [blame] | 575 | ds->account_invalid = stats->account_invalid; |
| 576 | ds->account_failed = stats->account_failed; |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 577 | |
| 578 | while ((ts = block_acct_interval_next(stats, ts))) { |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 579 | BlockDeviceTimedStats *dev_stats = g_malloc0(sizeof(*dev_stats)); |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 580 | |
| 581 | TimedAverage *rd = &ts->latency[BLOCK_ACCT_READ]; |
| 582 | TimedAverage *wr = &ts->latency[BLOCK_ACCT_WRITE]; |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 583 | TimedAverage *zap = &ts->latency[BLOCK_ACCT_ZONE_APPEND]; |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 584 | TimedAverage *fl = &ts->latency[BLOCK_ACCT_FLUSH]; |
| 585 | |
| 586 | dev_stats->interval_length = ts->interval_length; |
| 587 | |
| 588 | dev_stats->min_rd_latency_ns = timed_average_min(rd); |
| 589 | dev_stats->max_rd_latency_ns = timed_average_max(rd); |
| 590 | dev_stats->avg_rd_latency_ns = timed_average_avg(rd); |
| 591 | |
| 592 | dev_stats->min_wr_latency_ns = timed_average_min(wr); |
| 593 | dev_stats->max_wr_latency_ns = timed_average_max(wr); |
| 594 | dev_stats->avg_wr_latency_ns = timed_average_avg(wr); |
| 595 | |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 596 | dev_stats->min_zone_append_latency_ns = timed_average_min(zap); |
| 597 | dev_stats->max_zone_append_latency_ns = timed_average_max(zap); |
| 598 | dev_stats->avg_zone_append_latency_ns = timed_average_avg(zap); |
| 599 | |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 600 | dev_stats->min_flush_latency_ns = timed_average_min(fl); |
| 601 | dev_stats->max_flush_latency_ns = timed_average_max(fl); |
| 602 | dev_stats->avg_flush_latency_ns = timed_average_avg(fl); |
| 603 | |
| 604 | dev_stats->avg_rd_queue_depth = |
| 605 | block_acct_queue_depth(ts, BLOCK_ACCT_READ); |
| 606 | dev_stats->avg_wr_queue_depth = |
| 607 | block_acct_queue_depth(ts, BLOCK_ACCT_WRITE); |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 608 | dev_stats->avg_zone_append_queue_depth = |
| 609 | block_acct_queue_depth(ts, BLOCK_ACCT_ZONE_APPEND); |
Eric Blake | 54aa3de | 2020-11-12 19:13:37 -0600 | [diff] [blame] | 610 | |
| 611 | QAPI_LIST_PREPEND(ds->timed_stats, dev_stats); |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 612 | } |
Vladimir Sementsov-Ogievskiy | 7e5c776 | 2018-03-09 19:52:12 +0300 | [diff] [blame] | 613 | |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 614 | hgram = stats->latency_histogram; |
| 615 | ds->rd_latency_histogram |
| 616 | = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_READ]); |
| 617 | ds->wr_latency_histogram |
| 618 | = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_WRITE]); |
Sam Li | 52eb76f | 2023-05-08 13:19:14 +0800 | [diff] [blame] | 619 | ds->zone_append_latency_histogram |
| 620 | = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_ZONE_APPEND]); |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 621 | ds->flush_latency_histogram |
| 622 | = bdrv_latency_histogram_stats(&hgram[BLOCK_ACCT_FLUSH]); |
Kevin Wolf | 2b77e60 | 2016-02-26 19:02:30 +0100 | [diff] [blame] | 623 | } |
| 624 | |
Kevin Wolf | 6ec75a6 | 2023-05-04 13:57:47 +0200 | [diff] [blame] | 625 | static BlockStats * GRAPH_RDLOCK |
| 626 | bdrv_query_bds_stats(BlockDriverState *bs, bool blk_level) |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 627 | { |
Max Reitz | 3f26191 | 2019-11-28 09:49:22 +0100 | [diff] [blame] | 628 | BdrvChild *parent_child; |
Max Reitz | 0a7585d | 2019-06-12 18:23:25 +0200 | [diff] [blame] | 629 | BlockDriverState *filter_or_cow_bs; |
Dou Liyang | 20a6d76 | 2017-01-15 16:01:14 +0800 | [diff] [blame] | 630 | BlockStats *s = NULL; |
| 631 | |
| 632 | s = g_malloc0(sizeof(*s)); |
| 633 | s->stats = g_malloc0(sizeof(*s->stats)); |
| 634 | |
| 635 | if (!bs) { |
| 636 | return s; |
| 637 | } |
| 638 | |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 639 | /* Skip automatically inserted nodes that the user isn't aware of in |
| 640 | * a BlockBackend-level command. Stay at the exact node for a node-level |
| 641 | * command. */ |
Max Reitz | 0a7585d | 2019-06-12 18:23:25 +0200 | [diff] [blame] | 642 | if (blk_level) { |
| 643 | bs = bdrv_skip_implicit_filters(bs); |
Kevin Wolf | d3c8c67 | 2017-07-18 17:24:05 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Fam Zheng | 4875a77 | 2014-10-31 11:32:56 +0800 | [diff] [blame] | 646 | if (bdrv_get_node_name(bs)[0]) { |
Fam Zheng | 4875a77 | 2014-10-31 11:32:56 +0800 | [diff] [blame] | 647 | s->node_name = g_strdup(bdrv_get_node_name(bs)); |
| 648 | } |
| 649 | |
Paolo Bonzini | f7946da | 2017-06-05 14:39:00 +0200 | [diff] [blame] | 650 | s->stats->wr_highest_offset = stat64_get(&bs->wr_highest_offset); |
Max Reitz | 53d8f9d | 2015-10-19 17:53:20 +0200 | [diff] [blame] | 651 | |
Anton Nefedov | d924559 | 2019-09-23 15:17:37 +0300 | [diff] [blame] | 652 | s->driver_specific = bdrv_get_specific_stats(bs); |
Anton Nefedov | d924559 | 2019-09-23 15:17:37 +0300 | [diff] [blame] | 653 | |
Max Reitz | 3f26191 | 2019-11-28 09:49:22 +0100 | [diff] [blame] | 654 | parent_child = bdrv_primary_child(bs); |
| 655 | if (!parent_child || |
| 656 | !(parent_child->role & (BDRV_CHILD_DATA | BDRV_CHILD_FILTERED))) |
| 657 | { |
| 658 | BdrvChild *c; |
| 659 | |
| 660 | /* |
| 661 | * Look for a unique data-storing child. We do not need to look for |
| 662 | * filtered children, as there would be only one and it would have been |
| 663 | * the primary child. |
| 664 | */ |
| 665 | parent_child = NULL; |
| 666 | QLIST_FOREACH(c, &bs->children, next) { |
| 667 | if (c->role & BDRV_CHILD_DATA) { |
| 668 | if (parent_child) { |
| 669 | /* |
| 670 | * There are multiple data-storing children and we cannot |
| 671 | * choose between them. |
| 672 | */ |
| 673 | parent_child = NULL; |
| 674 | break; |
| 675 | } |
| 676 | parent_child = c; |
| 677 | } |
| 678 | } |
| 679 | } |
| 680 | if (parent_child) { |
Max Reitz | 3f26191 | 2019-11-28 09:49:22 +0100 | [diff] [blame] | 681 | s->parent = bdrv_query_bds_stats(parent_child->bs, blk_level); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 682 | } |
| 683 | |
Max Reitz | 0a7585d | 2019-06-12 18:23:25 +0200 | [diff] [blame] | 684 | filter_or_cow_bs = bdrv_filter_or_cow_bs(bs); |
| 685 | if (blk_level && filter_or_cow_bs) { |
| 686 | /* |
| 687 | * Put any filtered or COW child here (for backwards |
| 688 | * compatibility to when we put bs0->backing here, which might |
| 689 | * be either) |
| 690 | */ |
Max Reitz | 0a7585d | 2019-06-12 18:23:25 +0200 | [diff] [blame] | 691 | s->backing = bdrv_query_bds_stats(filter_or_cow_bs, blk_level); |
Fam Zheng | c8059b9 | 2014-01-23 10:03:26 +0800 | [diff] [blame] | 692 | } |
| 693 | |
Dou Liyang | 20a6d76 | 2017-01-15 16:01:14 +0800 | [diff] [blame] | 694 | return s; |
Kevin Wolf | b07363a | 2016-02-26 21:03:01 +0100 | [diff] [blame] | 695 | } |
| 696 | |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 697 | BlockInfoList *qmp_query_block(Error **errp) |
| 698 | { |
| 699 | BlockInfoList *head = NULL, **p_next = &head; |
Markus Armbruster | d829a21 | 2014-10-07 13:59:23 +0200 | [diff] [blame] | 700 | BlockBackend *blk; |
Wenchao Xia | 553a7e8 | 2013-06-06 12:27:59 +0800 | [diff] [blame] | 701 | Error *local_err = NULL; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 702 | |
Kevin Wolf | d5b6884 | 2017-07-11 13:04:28 +0200 | [diff] [blame] | 703 | for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { |
| 704 | BlockInfoList *info; |
| 705 | |
Kevin Wolf | ec18b0a | 2017-07-11 14:00:57 +0200 | [diff] [blame] | 706 | if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { |
Kevin Wolf | d5b6884 | 2017-07-11 13:04:28 +0200 | [diff] [blame] | 707 | continue; |
| 708 | } |
| 709 | |
| 710 | info = g_malloc0(sizeof(*info)); |
Markus Armbruster | d829a21 | 2014-10-07 13:59:23 +0200 | [diff] [blame] | 711 | bdrv_query_info(blk, &info->value, &local_err); |
Markus Armbruster | 84d18f0 | 2014-01-30 15:07:28 +0100 | [diff] [blame] | 712 | if (local_err) { |
Wenchao Xia | 553a7e8 | 2013-06-06 12:27:59 +0800 | [diff] [blame] | 713 | error_propagate(errp, local_err); |
Markus Armbruster | 903c341 | 2015-11-20 13:53:35 +0100 | [diff] [blame] | 714 | g_free(info); |
| 715 | qapi_free_BlockInfoList(head); |
| 716 | return NULL; |
Wenchao Xia | 553a7e8 | 2013-06-06 12:27:59 +0800 | [diff] [blame] | 717 | } |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 718 | |
| 719 | *p_next = info; |
| 720 | p_next = &info->next; |
| 721 | } |
| 722 | |
| 723 | return head; |
| 724 | } |
| 725 | |
Fam Zheng | f71eaa7 | 2014-10-31 11:32:57 +0800 | [diff] [blame] | 726 | BlockStatsList *qmp_query_blockstats(bool has_query_nodes, |
| 727 | bool query_nodes, |
| 728 | Error **errp) |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 729 | { |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 730 | BlockStatsList *head = NULL, **tail = &head; |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 731 | BlockBackend *blk; |
| 732 | BlockDriverState *bs; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 733 | |
Kevin Wolf | 6ec75a6 | 2023-05-04 13:57:47 +0200 | [diff] [blame] | 734 | GRAPH_RDLOCK_GUARD_MAINLOOP(); |
| 735 | |
Fam Zheng | f71eaa7 | 2014-10-31 11:32:57 +0800 | [diff] [blame] | 736 | /* Just to be safe if query_nodes is not always initialized */ |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 737 | if (has_query_nodes && query_nodes) { |
| 738 | for (bs = bdrv_next_node(NULL); bs; bs = bdrv_next_node(bs)) { |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 739 | AioContext *ctx = bdrv_get_aio_context(bs); |
Fam Zheng | f71eaa7 | 2014-10-31 11:32:57 +0800 | [diff] [blame] | 740 | |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 741 | aio_context_acquire(ctx); |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 742 | QAPI_LIST_APPEND(tail, bdrv_query_bds_stats(bs, false)); |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 743 | aio_context_release(ctx); |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 744 | } |
| 745 | } else { |
Kevin Wolf | 567dcb3 | 2018-07-27 16:09:25 +0200 | [diff] [blame] | 746 | for (blk = blk_all_next(NULL); blk; blk = blk_all_next(blk)) { |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 747 | AioContext *ctx = blk_get_aio_context(blk); |
| 748 | BlockStats *s; |
Kevin Wolf | 5a9cb5a | 2018-07-27 16:07:07 +0200 | [diff] [blame] | 749 | char *qdev; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 750 | |
Kevin Wolf | 567dcb3 | 2018-07-27 16:09:25 +0200 | [diff] [blame] | 751 | if (!*blk_name(blk) && !blk_get_attached_dev(blk)) { |
| 752 | continue; |
| 753 | } |
| 754 | |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 755 | aio_context_acquire(ctx); |
| 756 | s = bdrv_query_bds_stats(blk_bs(blk), true); |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 757 | s->device = g_strdup(blk_name(blk)); |
Kevin Wolf | 5a9cb5a | 2018-07-27 16:07:07 +0200 | [diff] [blame] | 758 | |
| 759 | qdev = blk_get_attached_dev_id(blk); |
| 760 | if (qdev && *qdev) { |
Kevin Wolf | 5a9cb5a | 2018-07-27 16:07:07 +0200 | [diff] [blame] | 761 | s->qdev = qdev; |
| 762 | } else { |
| 763 | g_free(qdev); |
| 764 | } |
| 765 | |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 766 | bdrv_query_blk_stats(s->stats, blk); |
| 767 | aio_context_release(ctx); |
| 768 | |
Eric Blake | c3033fd | 2021-01-13 16:10:12 -0600 | [diff] [blame] | 769 | QAPI_LIST_APPEND(tail, s); |
Dou Liyang | a6baa60 | 2017-01-15 16:01:15 +0800 | [diff] [blame] | 770 | } |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | return head; |
| 774 | } |
| 775 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 776 | void bdrv_snapshot_dump(QEMUSnapshotInfo *sn) |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 777 | { |
Daniel P. Berrangé | 3968355 | 2021-04-30 12:59:06 +0100 | [diff] [blame] | 778 | char clock_buf[128]; |
Pavel Dovgalyuk | b39847a | 2020-10-03 20:13:08 +0300 | [diff] [blame] | 779 | char icount_buf[128] = {0}; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 780 | int64_t secs; |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 781 | char *sizing = NULL; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 782 | |
| 783 | if (!sn) { |
Kevin Wolf | 26513a0 | 2021-02-02 16:59:11 +0100 | [diff] [blame] | 784 | qemu_printf("%-10s%-17s%8s%20s%13s%11s", |
Pavel Dovgalyuk | b39847a | 2020-10-03 20:13:08 +0300 | [diff] [blame] | 785 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK", "ICOUNT"); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 786 | } else { |
Daniel P. Berrangé | 3968355 | 2021-04-30 12:59:06 +0100 | [diff] [blame] | 787 | g_autoptr(GDateTime) date = g_date_time_new_from_unix_local(sn->date_sec); |
| 788 | g_autofree char *date_buf = g_date_time_format(date, "%Y-%m-%d %H:%M:%S"); |
| 789 | |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 790 | secs = sn->vm_clock_nsec / 1000000000; |
| 791 | snprintf(clock_buf, sizeof(clock_buf), |
| 792 | "%02d:%02d:%02d.%03d", |
| 793 | (int)(secs / 3600), |
| 794 | (int)((secs / 60) % 60), |
| 795 | (int)(secs % 60), |
| 796 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 797 | sizing = size_to_str(sn->vm_state_size); |
Pavel Dovgalyuk | b39847a | 2020-10-03 20:13:08 +0300 | [diff] [blame] | 798 | if (sn->icount != -1ULL) { |
| 799 | snprintf(icount_buf, sizeof(icount_buf), |
| 800 | "%"PRId64, sn->icount); |
| 801 | } |
Kevin Wolf | 26513a0 | 2021-02-02 16:59:11 +0100 | [diff] [blame] | 802 | qemu_printf("%-9s %-16s %8s%20s%13s%11s", |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 803 | sn->id_str, sn->name, |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 804 | sizing, |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 805 | date_buf, |
Pavel Dovgalyuk | b39847a | 2020-10-03 20:13:08 +0300 | [diff] [blame] | 806 | clock_buf, |
| 807 | icount_buf); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 808 | } |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 809 | g_free(sizing); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 810 | } |
| 811 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 812 | static void dump_qdict(int indentation, QDict *dict); |
| 813 | static void dump_qlist(int indentation, QList *list); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 814 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 815 | static void dump_qobject(int comp_indent, QObject *obj) |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 816 | { |
| 817 | switch (qobject_type(obj)) { |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 818 | case QTYPE_QNUM: { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 819 | QNum *value = qobject_to(QNum, obj); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 820 | char *tmp = qnum_to_string(value); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 821 | qemu_printf("%s", tmp); |
Marc-André Lureau | 01b2ffc | 2017-06-07 20:35:58 +0400 | [diff] [blame] | 822 | g_free(tmp); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 823 | break; |
| 824 | } |
| 825 | case QTYPE_QSTRING: { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 826 | QString *value = qobject_to(QString, obj); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 827 | qemu_printf("%s", qstring_get_str(value)); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 828 | break; |
| 829 | } |
| 830 | case QTYPE_QDICT: { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 831 | QDict *value = qobject_to(QDict, obj); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 832 | dump_qdict(comp_indent, value); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 833 | break; |
| 834 | } |
| 835 | case QTYPE_QLIST: { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 836 | QList *value = qobject_to(QList, obj); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 837 | dump_qlist(comp_indent, value); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 838 | break; |
| 839 | } |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 840 | case QTYPE_QBOOL: { |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 841 | QBool *value = qobject_to(QBool, obj); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 842 | qemu_printf("%s", qbool_get_bool(value) ? "true" : "false"); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 843 | break; |
| 844 | } |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 845 | default: |
| 846 | abort(); |
| 847 | } |
| 848 | } |
| 849 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 850 | static void dump_qlist(int indentation, QList *list) |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 851 | { |
| 852 | const QListEntry *entry; |
| 853 | int i = 0; |
| 854 | |
| 855 | for (entry = qlist_first(list); entry; entry = qlist_next(entry), i++) { |
Eric Blake | 1310a3d | 2015-12-01 22:20:46 -0700 | [diff] [blame] | 856 | QType type = qobject_type(entry->value); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 857 | bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 858 | qemu_printf("%*s[%i]:%c", indentation * 4, "", i, |
| 859 | composite ? '\n' : ' '); |
| 860 | dump_qobject(indentation + 1, entry->value); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 861 | if (!composite) { |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 862 | qemu_printf("\n"); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 867 | static void dump_qdict(int indentation, QDict *dict) |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 868 | { |
| 869 | const QDictEntry *entry; |
| 870 | |
| 871 | for (entry = qdict_first(dict); entry; entry = qdict_next(dict, entry)) { |
Eric Blake | 1310a3d | 2015-12-01 22:20:46 -0700 | [diff] [blame] | 872 | QType type = qobject_type(entry->value); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 873 | bool composite = (type == QTYPE_QDICT || type == QTYPE_QLIST); |
Peter Xu | 5eda622 | 2016-03-09 13:56:37 +0800 | [diff] [blame] | 874 | char *key = g_malloc(strlen(entry->key) + 1); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 875 | int i; |
| 876 | |
| 877 | /* replace dashes with spaces in key (variable) names */ |
| 878 | for (i = 0; entry->key[i]; i++) { |
| 879 | key[i] = entry->key[i] == '-' ? ' ' : entry->key[i]; |
| 880 | } |
| 881 | key[i] = 0; |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 882 | qemu_printf("%*s%s:%c", indentation * 4, "", key, |
| 883 | composite ? '\n' : ' '); |
| 884 | dump_qobject(indentation + 1, entry->value); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 885 | if (!composite) { |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 886 | qemu_printf("\n"); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 887 | } |
Peter Xu | 5eda622 | 2016-03-09 13:56:37 +0800 | [diff] [blame] | 888 | g_free(key); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Hanna Reitz | 3716470 | 2022-06-20 18:26:53 +0200 | [diff] [blame] | 892 | /* |
| 893 | * Return whether dumping the given QObject with dump_qobject() would |
| 894 | * yield an empty dump, i.e. not print anything. |
| 895 | */ |
| 896 | static bool qobject_is_empty_dump(const QObject *obj) |
| 897 | { |
| 898 | switch (qobject_type(obj)) { |
| 899 | case QTYPE_QNUM: |
| 900 | case QTYPE_QSTRING: |
| 901 | case QTYPE_QBOOL: |
| 902 | return false; |
| 903 | |
| 904 | case QTYPE_QDICT: |
| 905 | return qdict_size(qobject_to(QDict, obj)) == 0; |
| 906 | |
| 907 | case QTYPE_QLIST: |
| 908 | return qlist_empty(qobject_to(QList, obj)); |
| 909 | |
| 910 | default: |
| 911 | abort(); |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Dumps the given ImageInfoSpecific object in a human-readable form, |
| 917 | * prepending an optional prefix if the dump is not empty. |
| 918 | */ |
| 919 | void bdrv_image_info_specific_dump(ImageInfoSpecific *info_spec, |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 920 | const char *prefix, |
| 921 | int indentation) |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 922 | { |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 923 | QObject *obj, *data; |
Daniel P. Berrange | 7d5e199 | 2016-09-30 15:45:28 +0100 | [diff] [blame] | 924 | Visitor *v = qobject_output_visitor_new(&obj); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 925 | |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 926 | visit_type_ImageInfoSpecific(v, NULL, &info_spec, &error_abort); |
| 927 | visit_complete(v, &obj); |
Max Reitz | 7dc847e | 2018-02-24 16:40:29 +0100 | [diff] [blame] | 928 | data = qdict_get(qobject_to(QDict, obj), "data"); |
Hanna Reitz | 3716470 | 2022-06-20 18:26:53 +0200 | [diff] [blame] | 929 | if (!qobject_is_empty_dump(data)) { |
| 930 | if (prefix) { |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 931 | qemu_printf("%*s%s", indentation * 4, "", prefix); |
Hanna Reitz | 3716470 | 2022-06-20 18:26:53 +0200 | [diff] [blame] | 932 | } |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 933 | dump_qobject(indentation + 1, data); |
Hanna Reitz | 3716470 | 2022-06-20 18:26:53 +0200 | [diff] [blame] | 934 | } |
Marc-André Lureau | cb3e7f0 | 2018-04-19 17:01:43 +0200 | [diff] [blame] | 935 | qobject_unref(obj); |
Eric Blake | 3b098d5 | 2016-06-09 10:48:43 -0600 | [diff] [blame] | 936 | visit_free(v); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 937 | } |
| 938 | |
Hanna Reitz | d570177 | 2022-06-20 18:27:04 +0200 | [diff] [blame] | 939 | /** |
| 940 | * Print the given @info object in human-readable form. Every field is indented |
| 941 | * using the given @indentation (four spaces per indentation level). |
| 942 | * |
| 943 | * When using this to print a whole block graph, @protocol can be set to true to |
| 944 | * signify that the given information is associated with a protocol node, i.e. |
| 945 | * just data storage for an image, such that the data it presents is not really |
| 946 | * a full VM disk. If so, several fields change name: For example, "virtual |
| 947 | * size" is printed as "file length". |
| 948 | * (Consider a qcow2 image, which is represented by a qcow2 node and a file |
| 949 | * node. Printing a "virtual size" for the file node does not make sense, |
| 950 | * because without the qcow2 node, it is not really a guest disk, so it does not |
| 951 | * have a "virtual size". Therefore, we call it "file length" instead.) |
| 952 | * |
| 953 | * @protocol is ignored when @indentation is 0, because we take that to mean |
| 954 | * that the associated node is the root node in the queried block graph, and |
| 955 | * thus is always to be interpreted as a standalone guest disk. |
| 956 | */ |
| 957 | void bdrv_node_info_dump(BlockNodeInfo *info, int indentation, bool protocol) |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 958 | { |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 959 | char *size_buf, *dsize_buf; |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 960 | g_autofree char *ind_s = g_strdup_printf("%*s", indentation * 4, ""); |
| 961 | |
Hanna Reitz | d570177 | 2022-06-20 18:27:04 +0200 | [diff] [blame] | 962 | if (indentation == 0) { |
| 963 | /* Top level, consider this a normal image */ |
| 964 | protocol = false; |
| 965 | } |
| 966 | |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 967 | if (!info->has_actual_size) { |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 968 | dsize_buf = g_strdup("unavailable"); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 969 | } else { |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 970 | dsize_buf = size_to_str(info->actual_size); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 971 | } |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 972 | size_buf = size_to_str(info->virtual_size); |
Hanna Reitz | d570177 | 2022-06-20 18:27:04 +0200 | [diff] [blame] | 973 | qemu_printf("%s%s: %s\n" |
| 974 | "%s%s: %s\n" |
| 975 | "%s%s: %s (%" PRId64 " bytes)\n" |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 976 | "%sdisk size: %s\n", |
Hanna Reitz | d570177 | 2022-06-20 18:27:04 +0200 | [diff] [blame] | 977 | ind_s, protocol ? "filename" : "image", info->filename, |
| 978 | ind_s, protocol ? "protocol type" : "file format", |
| 979 | info->format, |
| 980 | ind_s, protocol ? "file length" : "virtual size", |
| 981 | size_buf, info->virtual_size, |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 982 | ind_s, dsize_buf); |
Eric Blake | de38b50 | 2019-04-17 12:11:01 -0500 | [diff] [blame] | 983 | g_free(size_buf); |
| 984 | g_free(dsize_buf); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 985 | |
| 986 | if (info->has_encrypted && info->encrypted) { |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 987 | qemu_printf("%sencrypted: yes\n", ind_s); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | if (info->has_cluster_size) { |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 991 | qemu_printf("%scluster_size: %" PRId64 "\n", |
| 992 | ind_s, info->cluster_size); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | if (info->has_dirty_flag && info->dirty_flag) { |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 996 | qemu_printf("%scleanly shut down: no\n", ind_s); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 997 | } |
| 998 | |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 999 | if (info->backing_filename) { |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 1000 | qemu_printf("%sbacking file: %s", ind_s, info->backing_filename); |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 1001 | if (!info->full_backing_filename) { |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 1002 | qemu_printf(" (cannot determine actual path)"); |
John Snow | 5c9d9ca | 2015-12-14 14:55:14 -0500 | [diff] [blame] | 1003 | } else if (strcmp(info->backing_filename, |
| 1004 | info->full_backing_filename) != 0) { |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 1005 | qemu_printf(" (actual path: %s)", info->full_backing_filename); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 1006 | } |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 1007 | qemu_printf("\n"); |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 1008 | if (info->backing_filename_format) { |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 1009 | qemu_printf("%sbacking file format: %s\n", |
| 1010 | ind_s, info->backing_filename_format); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | if (info->has_snapshots) { |
| 1015 | SnapshotInfoList *elem; |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 1016 | |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 1017 | qemu_printf("%sSnapshot list:\n", ind_s); |
| 1018 | qemu_printf("%s", ind_s); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 1019 | bdrv_snapshot_dump(NULL); |
| 1020 | qemu_printf("\n"); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 1021 | |
| 1022 | /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but |
| 1023 | * we convert to the block layer's native QEMUSnapshotInfo for now. |
| 1024 | */ |
| 1025 | for (elem = info->snapshots; elem; elem = elem->next) { |
| 1026 | QEMUSnapshotInfo sn = { |
| 1027 | .vm_state_size = elem->value->vm_state_size, |
| 1028 | .date_sec = elem->value->date_sec, |
| 1029 | .date_nsec = elem->value->date_nsec, |
| 1030 | .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + |
| 1031 | elem->value->vm_clock_nsec, |
Pavel Dovgalyuk | b39847a | 2020-10-03 20:13:08 +0300 | [diff] [blame] | 1032 | .icount = elem->value->has_icount ? |
| 1033 | elem->value->icount : -1ULL, |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 1034 | }; |
| 1035 | |
| 1036 | pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); |
| 1037 | pstrcpy(sn.name, sizeof(sn.name), elem->value->name); |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 1038 | qemu_printf("%s", ind_s); |
Markus Armbruster | e1ce7d7 | 2019-04-17 21:17:55 +0200 | [diff] [blame] | 1039 | bdrv_snapshot_dump(&sn); |
| 1040 | qemu_printf("\n"); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 1041 | } |
| 1042 | } |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 1043 | |
Markus Armbruster | 54fde4f | 2022-11-04 17:06:52 +0100 | [diff] [blame] | 1044 | if (info->format_specific) { |
Hanna Reitz | 3716470 | 2022-06-20 18:26:53 +0200 | [diff] [blame] | 1045 | bdrv_image_info_specific_dump(info->format_specific, |
Hanna Reitz | 76c9e97 | 2022-06-20 18:27:00 +0200 | [diff] [blame] | 1046 | "Format specific information:\n", |
| 1047 | indentation); |
Max Reitz | a8d8ecb | 2013-10-09 10:46:17 +0200 | [diff] [blame] | 1048 | } |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 1049 | } |