diff mbox series

[v3,for-next,12/12] RDMA/hns: Dump detailed driver-specific UCTX

Message ID 1627356452-30564-13-git-send-email-liangwenpeng@huawei.com (mailing list archive)
State Superseded
Delegated to: Leon Romanovsky
Headers show
Series RDMA/hns: Add support for Dynamic Context Attachment | expand

Commit Message

Wenpeng Liang July 27, 2021, 3:27 a.m. UTC
From: Xi Wang <wangxi11@huawei.com>

Dump DCA mem pool status in UCTX restrack.

Sample output:
$ rdma res show ctx dev hns_0 -dd
 dev hns_0 ctxn 7 pid 1410 comm python3 drv_dca-loading 37.50 drv_dca-total 65536 drv_dca-free 40960
 dev hns_0 ctxn 8 pid 1410 comm python3 drv_dca-loading 0.00 drv_dca-total 0 drv_dca-free 0

Signed-off-by: Xi Wang <wangxi11@huawei.com>
Signed-off-by: Wenpeng Liang <liangwenpeng@huawei.com>
---
 drivers/infiniband/hw/hns/hns_roce_device.h   |  2 +
 drivers/infiniband/hw/hns/hns_roce_main.c     |  1 +
 drivers/infiniband/hw/hns/hns_roce_restrack.c | 85 +++++++++++++++++++++++++++
 3 files changed, 88 insertions(+)

Comments

Leon Romanovsky July 27, 2021, 11:57 a.m. UTC | #1
On Tue, Jul 27, 2021 at 11:27:32AM +0800, Wenpeng Liang wrote:
> From: Xi Wang <wangxi11@huawei.com>
> 
> Dump DCA mem pool status in UCTX restrack.
> 
> Sample output:
> $ rdma res show ctx dev hns_0 -dd
>  dev hns_0 ctxn 7 pid 1410 comm python3 drv_dca-loading 37.50 drv_dca-total 65536 drv_dca-free 40960
>  dev hns_0 ctxn 8 pid 1410 comm python3 drv_dca-loading 0.00 drv_dca-total 0 drv_dca-free 0
> 
> Signed-off-by: Xi Wang <wangxi11@huawei.com>
> Signed-off-by: Wenpeng Liang <liangwenpeng@huawei.com>
> ---
>  drivers/infiniband/hw/hns/hns_roce_device.h   |  2 +
>  drivers/infiniband/hw/hns/hns_roce_main.c     |  1 +
>  drivers/infiniband/hw/hns/hns_roce_restrack.c | 85 +++++++++++++++++++++++++++
>  3 files changed, 88 insertions(+)

<...>

> +static int hns_roce_fill_dca_uctx(struct hns_roce_dca_ctx *ctx,
> +				  struct sk_buff *msg)
> +{
> +	char tmp_str[LOADING_PERCENT_CHARS];
> +	unsigned long flags;
> +	u64 total, free;
> +	u64 percent;
> +	u32 rem = 0;
> +
> +	spin_lock_irqsave(&ctx->pool_lock, flags);
> +	total = ctx->total_size;
> +	free = ctx->free_size;
> +	spin_unlock_irqrestore(&ctx->pool_lock, flags);
> +
> +	percent = calc_dca_loading_percent(total, free, &rem);
> +	scnprintf(tmp_str, sizeof(tmp_str), "%llu.%0*u\n", percent,
> +		  LOADING_PERCENT_SHIFT, rem);
> +
> +	if (rdma_nl_put_driver_string(msg, "dca-loading", tmp_str))
> +		goto err;

Please no, users can calculate percentage by themselves. We don't need
to complicate kernel for it.

Thanks

> +
> +	if (rdma_nl_put_driver_u64(msg, "dca-total", total))
> +		goto err;
> +
> +	if (rdma_nl_put_driver_u64(msg, "dca-free", free))
> +		goto err;
> +
> +	return 0;
> +
> +err:
> +	return -EMSGSIZE;
> +}
> +
> +int hns_roce_fill_res_ctx_entry(struct sk_buff *msg, struct ib_ucontext *ctx)
> +{
> +	struct hns_roce_dev *hr_dev = to_hr_dev(ctx->device);
> +	struct hns_roce_ucontext *uctx = to_hr_ucontext(ctx);
> +	struct nlattr *table_attr;
> +
> +	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
> +	if (!table_attr)
> +		goto err;
> +
> +	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DCA_MODE) {
> +		if (hns_roce_fill_dca_uctx(&uctx->dca_ctx, msg))
> +			goto err_cancel_table;
> +	}
> +
> +	nla_nest_end(msg, table_attr);
> +
> +	return 0;
> +
> +err_cancel_table:
> +	nla_nest_cancel(msg, table_attr);
> +err:
> +	return -EMSGSIZE;
> +
> +	return 0;
> +}
> -- 
> 2.8.1
>
Wenpeng Liang July 28, 2021, 6:13 a.m. UTC | #2
On 2021/7/27 19:57, Leon Romanovsky wrote:
> On Tue, Jul 27, 2021 at 11:27:32AM +0800, Wenpeng Liang wrote:
>> From: Xi Wang <wangxi11@huawei.com>
>>
>> Dump DCA mem pool status in UCTX restrack.
>>
>> Sample output:
>> $ rdma res show ctx dev hns_0 -dd
>>  dev hns_0 ctxn 7 pid 1410 comm python3 drv_dca-loading 37.50 drv_dca-total 65536 drv_dca-free 40960
>>  dev hns_0 ctxn 8 pid 1410 comm python3 drv_dca-loading 0.00 drv_dca-total 0 drv_dca-free 0
>>
>> Signed-off-by: Xi Wang <wangxi11@huawei.com>
>> Signed-off-by: Wenpeng Liang <liangwenpeng@huawei.com>
>> ---
>>  drivers/infiniband/hw/hns/hns_roce_device.h   |  2 +
>>  drivers/infiniband/hw/hns/hns_roce_main.c     |  1 +
>>  drivers/infiniband/hw/hns/hns_roce_restrack.c | 85 +++++++++++++++++++++++++++
>>  3 files changed, 88 insertions(+)
> <...>
> 
>> +static int hns_roce_fill_dca_uctx(struct hns_roce_dca_ctx *ctx,
>> +				  struct sk_buff *msg)
>> +{
>> +	char tmp_str[LOADING_PERCENT_CHARS];
>> +	unsigned long flags;
>> +	u64 total, free;
>> +	u64 percent;
>> +	u32 rem = 0;
>> +
>> +	spin_lock_irqsave(&ctx->pool_lock, flags);
>> +	total = ctx->total_size;
>> +	free = ctx->free_size;
>> +	spin_unlock_irqrestore(&ctx->pool_lock, flags);
>> +
>> +	percent = calc_dca_loading_percent(total, free, &rem);
>> +	scnprintf(tmp_str, sizeof(tmp_str), "%llu.%0*u\n", percent,
>> +		  LOADING_PERCENT_SHIFT, rem);
>> +
>> +	if (rdma_nl_put_driver_string(msg, "dca-loading", tmp_str))
>> +		goto err;
> Please no, users can calculate percentage by themselves. We don't need
> to complicate kernel for it.
> 
> Thanks
> 

The next version will remove "dca-loading".

Thanks

>> +
>> +	if (rdma_nl_put_driver_u64(msg, "dca-total", total))
>> +		goto err;
>> +
>> +	if (rdma_nl_put_driver_u64(msg, "dca-free", free))
>> +		goto err;
>> +
>> +	return 0;
>> +
>> +err:
>> +	return -EMSGSIZE;
>> +}
diff mbox series

Patch

diff --git a/drivers/infiniband/hw/hns/hns_roce_device.h b/drivers/infiniband/hw/hns/hns_roce_device.h
index bef418d..0dfaca4 100644
--- a/drivers/infiniband/hw/hns/hns_roce_device.h
+++ b/drivers/infiniband/hw/hns/hns_roce_device.h
@@ -1304,4 +1304,6 @@  int hns_roce_init(struct hns_roce_dev *hr_dev);
 void hns_roce_exit(struct hns_roce_dev *hr_dev);
 int hns_roce_fill_res_cq_entry(struct sk_buff *msg,
 			       struct ib_cq *ib_cq);
+int hns_roce_fill_res_ctx_entry(struct sk_buff *msg, struct ib_ucontext *ctx);
+
 #endif /* _HNS_ROCE_DEVICE_H */
diff --git a/drivers/infiniband/hw/hns/hns_roce_main.c b/drivers/infiniband/hw/hns/hns_roce_main.c
index e37ece8..4f30c29 100644
--- a/drivers/infiniband/hw/hns/hns_roce_main.c
+++ b/drivers/infiniband/hw/hns/hns_roce_main.c
@@ -546,6 +546,7 @@  static const struct ib_device_ops hns_roce_dev_ops = {
 	.destroy_cq = hns_roce_destroy_cq,
 	.disassociate_ucontext = hns_roce_disassociate_ucontext,
 	.fill_res_cq_entry = hns_roce_fill_res_cq_entry,
+	.fill_res_ctx_entry = hns_roce_fill_res_ctx_entry,
 	.get_dma_mr = hns_roce_get_dma_mr,
 	.get_link_layer = hns_roce_get_link_layer,
 	.get_port_immutable = hns_roce_port_immutable,
diff --git a/drivers/infiniband/hw/hns/hns_roce_restrack.c b/drivers/infiniband/hw/hns/hns_roce_restrack.c
index 259444c..4e2e2c2 100644
--- a/drivers/infiniband/hw/hns/hns_roce_restrack.c
+++ b/drivers/infiniband/hw/hns/hns_roce_restrack.c
@@ -118,3 +118,88 @@  int hns_roce_fill_res_cq_entry(struct sk_buff *msg,
 	kfree(context);
 	return ret;
 }
+
+#define LOADING_PERCENT_SCALE 100
+#define LOADING_PERCENT_SHIFT 2
+#define LOADING_PERCENT_CHARS 10
+
+static u64 calc_dca_loading_percent(u64 total, u64 free, u32 *out_rem)
+{
+	u32 all_pages, used_pages, free_pages, scale;
+	u64 percent = 0;
+	u32 rem = 0;
+
+	all_pages = total >> HNS_HW_PAGE_SHIFT;
+	free_pages = free >> HNS_HW_PAGE_SHIFT;
+	if (all_pages >= free_pages) {
+		used_pages = all_pages - free_pages;
+		scale = LOADING_PERCENT_SCALE * LOADING_PERCENT_SCALE;
+		percent = (used_pages * scale) / all_pages;
+		percent = div_u64_rem(percent, LOADING_PERCENT_SCALE, &rem);
+	}
+
+	if (out_rem)
+		*out_rem = rem;
+
+	return percent;
+}
+
+static int hns_roce_fill_dca_uctx(struct hns_roce_dca_ctx *ctx,
+				  struct sk_buff *msg)
+{
+	char tmp_str[LOADING_PERCENT_CHARS];
+	unsigned long flags;
+	u64 total, free;
+	u64 percent;
+	u32 rem = 0;
+
+	spin_lock_irqsave(&ctx->pool_lock, flags);
+	total = ctx->total_size;
+	free = ctx->free_size;
+	spin_unlock_irqrestore(&ctx->pool_lock, flags);
+
+	percent = calc_dca_loading_percent(total, free, &rem);
+	scnprintf(tmp_str, sizeof(tmp_str), "%llu.%0*u\n", percent,
+		  LOADING_PERCENT_SHIFT, rem);
+
+	if (rdma_nl_put_driver_string(msg, "dca-loading", tmp_str))
+		goto err;
+
+	if (rdma_nl_put_driver_u64(msg, "dca-total", total))
+		goto err;
+
+	if (rdma_nl_put_driver_u64(msg, "dca-free", free))
+		goto err;
+
+	return 0;
+
+err:
+	return -EMSGSIZE;
+}
+
+int hns_roce_fill_res_ctx_entry(struct sk_buff *msg, struct ib_ucontext *ctx)
+{
+	struct hns_roce_dev *hr_dev = to_hr_dev(ctx->device);
+	struct hns_roce_ucontext *uctx = to_hr_ucontext(ctx);
+	struct nlattr *table_attr;
+
+	table_attr = nla_nest_start(msg, RDMA_NLDEV_ATTR_DRIVER);
+	if (!table_attr)
+		goto err;
+
+	if (hr_dev->caps.flags & HNS_ROCE_CAP_FLAG_DCA_MODE) {
+		if (hns_roce_fill_dca_uctx(&uctx->dca_ctx, msg))
+			goto err_cancel_table;
+	}
+
+	nla_nest_end(msg, table_attr);
+
+	return 0;
+
+err_cancel_table:
+	nla_nest_cancel(msg, table_attr);
+err:
+	return -EMSGSIZE;
+
+	return 0;
+}