qapi.c 33 KB

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