Parse the new RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX netlink attribute to show resource usage percentage alongside current counts: Before: 0: mlx5_0: qp 123 cq 45 mr 200 pd 10 After: 0: mlx5_0: qp 123 (0.0%) cq 45 (0.0%) mr 200 (0.0%) pd 10 (0.0%) JSON output gains "max" and "usage" fields per resource type. Backward compatible: no output change when kernel lacks the new attribute. Link: https://lore.kernel.org/all/20260423061352.359749-1-cuitao@kylinos.cn/ Signed-off-by: Tao Cui --- rdma/include/uapi/rdma/rdma_netlink.h | 5 +++++ rdma/res.c | 15 +++++++++++++++ rdma/utils.c | 1 + 3 files changed, 21 insertions(+) diff --git a/rdma/include/uapi/rdma/rdma_netlink.h b/rdma/include/uapi/rdma/rdma_netlink.h index 8709e558..12ca07ef 100644 --- a/rdma/include/uapi/rdma/rdma_netlink.h +++ b/rdma/include/uapi/rdma/rdma_netlink.h @@ -604,6 +604,11 @@ enum rdma_nldev_attr { RDMA_NLDEV_ATTR_RES_FRMR_POOL_PINNED, /* u32 */ RDMA_NLDEV_ATTR_RES_FRMR_POOL_KEY_KERNEL_VENDOR_KEY, /* u64 */ + /* + * Resource summary entry maximum value, used to calculate usage rate. + */ + RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX, /* u64 */ + /* * Always the end */ diff --git a/rdma/res.c b/rdma/res.c index 062f0007..cf9c79a3 100644 --- a/rdma/res.c +++ b/rdma/res.c @@ -56,6 +56,21 @@ static int res_print_summary(struct nlattr **tb) name = mnl_attr_get_str(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_NAME]); curr = mnl_attr_get_u64(nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]); res_print_u64(name, curr, nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_CURR]); + + if (nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX]) { + uint64_t max; + + max = mnl_attr_get_u64( + nla_line[RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX]); + print_u64(PRINT_JSON, "max", NULL, max); + if (max > 0) { + char usage_str[32]; + + snprintf(usage_str, sizeof(usage_str), "(%.1f%%)", + (double)curr * 100.0 / (double)max); + print_string(PRINT_ANY, "usage", "%s ", usage_str); + } + } } return 0; } diff --git a/rdma/utils.c b/rdma/utils.c index 87003b2c..90ea1c55 100644 --- a/rdma/utils.c +++ b/rdma/utils.c @@ -480,6 +480,7 @@ static const enum mnl_attr_data_type nldev_policy[RDMA_NLDEV_ATTR_MAX] = { [RDMA_NLDEV_ATTR_EVENT_TYPE] = MNL_TYPE_U8, [RDMA_NLDEV_SYS_ATTR_MONITOR_MODE] = MNL_TYPE_U8, [RDMA_NLDEV_ATTR_STAT_OPCOUNTER_ENABLED] = MNL_TYPE_U8, + [RDMA_NLDEV_ATTR_RES_SUMMARY_ENTRY_MAX] = MNL_TYPE_U64, }; static int rd_attr_check(const struct nlattr *attr, int *typep) -- 2.43.0