All of lore.kernel.org
 help / color / mirror / Atom feed
From: Hou Tao <houtao@huaweicloud.com>
To: bpf@vger.kernel.org
Cc: Martin KaFai Lau <martin.lau@linux.dev>,
	Alexei Starovoitov <alexei.starovoitov@gmail.com>,
	Andrii Nakryiko <andrii@kernel.org>, Song Liu <song@kernel.org>,
	Hao Luo <haoluo@google.com>,
	Yonghong Song <yonghong.song@linux.dev>,
	Daniel Borkmann <daniel@iogearbox.net>,
	KP Singh <kpsingh@kernel.org>,
	Stanislav Fomichev <sdf@google.com>, Jiri Olsa <jolsa@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	houtao1@huawei.com
Subject: [PATCH bpf-next 1/2] selftests/bpf: Move bench specific metrics into union of structs
Date: Thu, 21 Dec 2023 22:15:00 +0800	[thread overview]
Message-ID: <20231221141501.3588586-2-houtao@huaweicloud.com> (raw)
In-Reply-To: <20231221141501.3588586-1-houtao@huaweicloud.com>

From: Hou Tao <houtao1@huawei.com>

Various benchmarks define its specific metrics in bench_res. This not
only bloats the size of bench_res, but also make the code that tries to
reuse the space hard to follow.

So move benchmark specific metrics into stand-alone structs and pack
these structs into a union to reduce the size of bench_res.

Signed-off-by: Hou Tao <houtao1@huawei.com>
---
 tools/testing/selftests/bpf/bench.c               |  9 +++++----
 tools/testing/selftests/bpf/bench.h               | 15 ++++++++++++---
 .../testing/selftests/bpf/benchs/bench_htab_mem.c | 10 +++++-----
 .../benchs/bench_local_storage_rcu_tasks_trace.c  | 10 +++++-----
 4 files changed, 27 insertions(+), 17 deletions(-)

diff --git a/tools/testing/selftests/bpf/bench.c b/tools/testing/selftests/bpf/bench.c
index 73ce11b0547d..4832cd4b1c3d 100644
--- a/tools/testing/selftests/bpf/bench.c
+++ b/tools/testing/selftests/bpf/bench.c
@@ -88,9 +88,10 @@ grace_period_latency_basic_stats(struct bench_res res[], int res_cnt, struct bas
 	memset(gp_stat, 0, sizeof(struct basic_stats));
 
 	for (i = 0; i < res_cnt; i++)
-		gp_stat->mean += res[i].gp_ns / 1000.0 / (double)res[i].gp_ct / (0.0 + res_cnt);
+		gp_stat->mean += res[i].rcu.gp_ns / 1000.0 / (double)res[i].rcu.gp_ct /
+				 (0.0 + res_cnt);
 
-#define IT_MEAN_DIFF (res[i].gp_ns / 1000.0 / (double)res[i].gp_ct - gp_stat->mean)
+#define IT_MEAN_DIFF (res[i].rcu.gp_ns / 1000.0 / (double)res[i].rcu.gp_ct - gp_stat->mean)
 	if (res_cnt > 1) {
 		for (i = 0; i < res_cnt; i++)
 			gp_stat->stddev += (IT_MEAN_DIFF * IT_MEAN_DIFF) / (res_cnt - 1.0);
@@ -106,9 +107,9 @@ grace_period_ticks_basic_stats(struct bench_res res[], int res_cnt, struct basic
 
 	memset(gp_stat, 0, sizeof(struct basic_stats));
 	for (i = 0; i < res_cnt; i++)
-		gp_stat->mean += res[i].stime / (double)res[i].gp_ct / (0.0 + res_cnt);
+		gp_stat->mean += res[i].rcu.stime / (double)res[i].rcu.gp_ct / (0.0 + res_cnt);
 
-#define IT_MEAN_DIFF (res[i].stime / (double)res[i].gp_ct - gp_stat->mean)
+#define IT_MEAN_DIFF (res[i].rcu.stime / (double)res[i].rcu.gp_ct - gp_stat->mean)
 	if (res_cnt > 1) {
 		for (i = 0; i < res_cnt; i++)
 			gp_stat->stddev += (IT_MEAN_DIFF * IT_MEAN_DIFF) / (res_cnt - 1.0);
diff --git a/tools/testing/selftests/bpf/bench.h b/tools/testing/selftests/bpf/bench.h
index 68180d8f8558..a6fcf111221f 100644
--- a/tools/testing/selftests/bpf/bench.h
+++ b/tools/testing/selftests/bpf/bench.h
@@ -42,9 +42,18 @@ struct bench_res {
 	long drops;
 	long false_hits;
 	long important_hits;
-	unsigned long gp_ns;
-	unsigned long gp_ct;
-	unsigned int stime;
+
+	/* benchmark specific metrics */
+	union {
+		struct {
+			unsigned long bytes;
+		} htab;
+		struct {
+			unsigned long gp_ns;
+			unsigned long gp_ct;
+			unsigned int stime;
+		} rcu;
+	};
 };
 
 struct bench {
diff --git a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
index 9146d3f414d2..2f37826332ed 100644
--- a/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
+++ b/tools/testing/selftests/bpf/benchs/bench_htab_mem.c
@@ -293,7 +293,7 @@ static void htab_mem_read_mem_cgrp_file(const char *name, unsigned long *value)
 static void htab_mem_measure(struct bench_res *res)
 {
 	res->hits = atomic_swap(&ctx.skel->bss->op_cnt, 0) / env.producer_cnt;
-	htab_mem_read_mem_cgrp_file("memory.current", &res->gp_ct);
+	htab_mem_read_mem_cgrp_file("memory.current", &res->htab.bytes);
 }
 
 static void htab_mem_report_progress(int iter, struct bench_res *res, long delta_ns)
@@ -301,7 +301,7 @@ static void htab_mem_report_progress(int iter, struct bench_res *res, long delta
 	double loop, mem;
 
 	loop = res->hits / 1000.0 / (delta_ns / 1000000000.0);
-	mem = res->gp_ct / 1048576.0;
+	mem = res->htab.bytes / 1048576.0;
 	printf("Iter %3d (%7.3lfus): ", iter, (delta_ns - 1000000000) / 1000.0);
 	printf("per-prod-op %7.2lfk/s, memory usage %7.2lfMiB\n", loop, mem);
 }
@@ -315,15 +315,15 @@ static void htab_mem_report_final(struct bench_res res[], int res_cnt)
 
 	for (i = 0; i < res_cnt; i++) {
 		loop_mean += res[i].hits / 1000.0 / (0.0 + res_cnt);
-		mem_mean += res[i].gp_ct / 1048576.0 / (0.0 + res_cnt);
+		mem_mean += res[i].htab.bytes / 1048576.0 / (0.0 + res_cnt);
 	}
 	if (res_cnt > 1)  {
 		for (i = 0; i < res_cnt; i++) {
 			loop_stddev += (loop_mean - res[i].hits / 1000.0) *
 				       (loop_mean - res[i].hits / 1000.0) /
 				       (res_cnt - 1.0);
-			mem_stddev += (mem_mean - res[i].gp_ct / 1048576.0) *
-				      (mem_mean - res[i].gp_ct / 1048576.0) /
+			mem_stddev += (mem_mean - res[i].htab.bytes / 1048576.0) *
+				      (mem_mean - res[i].htab.bytes / 1048576.0) /
 				      (res_cnt - 1.0);
 		}
 		loop_stddev = sqrt(loop_stddev);
diff --git a/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c b/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c
index edf0b00418c1..4842f4f2bbea 100644
--- a/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c
+++ b/tools/testing/selftests/bpf/benchs/bench_local_storage_rcu_tasks_trace.c
@@ -190,10 +190,10 @@ static void measure(struct bench_res *res)
 {
 	long ticks;
 
-	res->gp_ct = atomic_swap(&ctx.skel->bss->gp_hits, 0);
-	res->gp_ns = atomic_swap(&ctx.skel->bss->gp_times, 0);
+	res->rcu.gp_ct = atomic_swap(&ctx.skel->bss->gp_hits, 0);
+	res->rcu.gp_ns = atomic_swap(&ctx.skel->bss->gp_times, 0);
 	ticks = kthread_pid_ticks();
-	res->stime = ticks - ctx.prev_kthread_stime;
+	res->rcu.stime = ticks - ctx.prev_kthread_stime;
 	ctx.prev_kthread_stime = ticks;
 }
 
@@ -216,9 +216,9 @@ static void report_progress(int iter, struct bench_res *res, long delta_ns)
 		return;
 
 	printf("Iter %d\t avg tasks_trace grace period latency\t%lf ns\n",
-	       iter, res->gp_ns / (double)res->gp_ct);
+	       iter, res->rcu.gp_ns / (double)res->rcu.gp_ct);
 	printf("Iter %d\t avg ticks per tasks_trace grace period\t%lf\n",
-	       iter, res->stime / (double)res->gp_ct);
+	       iter, res->rcu.stime / (double)res->rcu.gp_ct);
 }
 
 static void report_final(struct bench_res res[], int res_cnt)
-- 
2.29.2


  reply	other threads:[~2023-12-21 14:14 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-21 14:14 [PATCH bpf-next 0/2] bpf: Add benchmark for bpf memory allocator Hou Tao
2023-12-21 14:15 ` Hou Tao [this message]
2024-01-08 20:51   ` [PATCH bpf-next 1/2] selftests/bpf: Move bench specific metrics into union of structs Song Liu
2023-12-21 14:15 ` [PATCH bpf-next 2/2] selftests/bpf: Add benchmark for bpf memory allocator Hou Tao
2024-01-08 21:45   ` Song Liu
2024-01-09  1:16     ` Hou Tao
2024-01-08  1:33 ` [PATCH bpf-next 0/2] bpf: " Hou Tao
2024-01-08 13:34 ` Jiri Olsa

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231221141501.3588586-2-houtao@huaweicloud.com \
    --to=houtao@huaweicloud.com \
    --cc=alexei.starovoitov@gmail.com \
    --cc=andrii@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=haoluo@google.com \
    --cc=houtao1@huawei.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=sdf@google.com \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.