Skip to content

Commit 0edb210

Browse files
Fix-Pointxiaoxiang781216
authored andcommitted
benchmarks: support test-tlb
This commit added support for test-tlb, a memory latency micro-benchmark. Signed-off-by: ouyangxiangzhen <[email protected]>
1 parent ddabb6e commit 0edb210

File tree

5 files changed

+301
-0
lines changed

5 files changed

+301
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
From fe4108f726a0e30d429f031a6006f030c5eda113 Mon Sep 17 00:00:00 2001
2+
From: ouyangxiangzhen <[email protected]>
3+
Date: Thu, 6 Jun 2024 19:53:37 +0800
4+
Subject: [PATCH] test-tlb: port for NuttX
5+
6+
VELAPLATFO-25227
7+
8+
This commit ports the test-tlb test program to the NuttX. Additionally, it implements dynamic CPU frequency calculation.
9+
10+
Change-Id: If1c15f36742d7ee5a7d13147b41b3372047dad08
11+
Signed-off-by: ouyangxiangzhen <[email protected]>
12+
---
13+
test-tlb.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++----
14+
1 file changed, 63 insertions(+), 5 deletions(-)
15+
16+
diff --git a/test-tlb.c b/test-tlb.c
17+
index 5c8aaa5..767794a 100644
18+
--- a/test-tlb.c
19+
+++ b/test-tlb.c
20+
@@ -14,13 +14,58 @@
21+
#include <math.h>
22+
#include <time.h>
23+
24+
-#define PAGE_SIZE 4096
25+
-
26+
-#define FREQ 3.9
27+
28+
static int test_hugepage = 0;
29+
static int random_list = 0;
30+
31+
+/* Fix definition conflict */
32+
+#ifndef PAGE_SIZE
33+
+#define PAGE_SIZE 4096
34+
+#endif
35+
+
36+
+/* dynamic calculate freq */
37+
+#define MEASURE_GAP 500000
38+
+
39+
+#ifdef __x86_64__
40+
+static inline unsigned long rdcnt_relax(void)
41+
+{
42+
+ unsigned int lo,hi;
43+
+ asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
44+
+ return ((unsigned long)hi << 32) | (unsigned long)lo;
45+
+}
46+
+#elif defined(__aarch64__)
47+
+static inline unsigned long rdcnt_relax(void)
48+
+{
49+
+ unsigned long time;
50+
+ asm volatile("mrs %0, cntvct_el0" : "=r"(time));
51+
+ return time;
52+
+}
53+
+#else
54+
+#error "Unsupported architecture for dynamic frequencies measuring"
55+
+#endif
56+
+
57+
+static inline unsigned long rdcnt(void)
58+
+{
59+
+ return rdcnt_relax();
60+
+}
61+
+
62+
+static unsigned long measure_freq(void)
63+
+{
64+
+ unsigned long cycles_per_useconds;
65+
+ unsigned long start;
66+
+ unsigned long end;
67+
+
68+
+ start = rdcnt();
69+
+ usleep(MEASURE_GAP);
70+
+ end = rdcnt();
71+
+
72+
+ cycles_per_useconds = (end - start) / MEASURE_GAP;
73+
+
74+
+ printf("cycles_per_microseconds: %lu\n", cycles_per_useconds);
75+
+
76+
+ return cycles_per_useconds;
77+
+}
78+
+
79+
static void die(const char *fmt, ...)
80+
{
81+
va_list argp;
82+
@@ -69,7 +114,7 @@ static unsigned long warmup(void *map)
83+
84+
static double do_test(void *map)
85+
{
86+
- unsigned long count = 0, offset = 0, usec;
87+
+ unsigned long volatile count = 0, offset = 0, usec;
88+
struct timeval start, end;
89+
struct itimerval itval = {
90+
.it_interval = { 0, 0 },
91+
@@ -190,7 +235,14 @@ static void *create_map(void *map, unsigned long size, unsigned long stride)
92+
if (map) {
93+
if (test_hugepage)
94+
return map;
95+
+
96+
+#ifdef __NuttX__
97+
+ if (munmap(map, size) != 0)
98+
+ die("munmap failed\n");
99+
+ map = NULL;
100+
+#else
101+
flags |= MAP_FIXED;
102+
+#endif
103+
}
104+
105+
mapsize = size;
106+
@@ -237,9 +289,14 @@ int main(int argc, char **argv)
107+
const char *arg;
108+
void *map;
109+
double cycles;
110+
+ unsigned long freq;
111+
112+
srandom(time(NULL));
113+
114+
+ /* Fix segmentation fault when execution without any arguments in NuttX */
115+
+ if (argc < 3)
116+
+ die("bad arguments: test-tlb [-H] <size> <stride>");
117+
+
118+
while ((arg = argv[1]) != NULL) {
119+
if (*arg != '-')
120+
break;
121+
@@ -261,6 +318,7 @@ int main(int argc, char **argv)
122+
argv++;
123+
}
124+
125+
+ freq = measure_freq();
126+
size = get_num(argv[1]);
127+
stride = get_num(argv[2]);
128+
if (stride < 4 || size < stride)
129+
@@ -281,6 +339,6 @@ int main(int argc, char **argv)
130+
}
131+
132+
printf("%6.2fns (~%.1f cycles)\n",
133+
- cycles, cycles*FREQ);
134+
+ cycles, cycles * freq);
135+
return 0;
136+
}
137+
--
138+
2.34.1
139+

benchmarks/test-tlb/CMakeLists.txt

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#
2+
# Copyright (C) 2024 Xiaomi Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
# use this file except in compliance with the License. You may obtain a copy of
6+
# the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
# License for the specific language governing permissions and limitations under
14+
# the License.
15+
#
16+
17+
if(CONFIG_BENCHMARK_TESTTLB)
18+
19+
set(SRCS test-tlb/test-tlb.c)
20+
21+
set(TESTTLB_UNPACK ${CMAKE_CURRENT_LIST_DIR}/test-tlb)
22+
set(TESTTLB_URL https://github.com/torvalds/test-tlb/archive)
23+
set(TESTTLB_ZIP master.zip)
24+
25+
if(NOT EXISTS ${TESTTLB_UNPACK})
26+
27+
FetchContent_Declare(
28+
testtlb_fetch
29+
URL ${TESTTLB_URL}/${TESTTLB_ZIP} SOURCE_DIR ${TESTTLB_UNPACK} BINARY_DIR
30+
${CMAKE_BINARY_DIR}/apps/benchmarks/test-tlb/test-tlb
31+
PATCH_COMMAND patch -p0 -d ${CMAKE_CURRENT_LIST_DIR} <
32+
${CMAKE_CURRENT_LIST_DIR}/0001-test-tlb-port-for-NuttX.patch
33+
DOWNLOAD_NO_PROGRESS true
34+
TIMEOUT 30)
35+
36+
FetchContent_GetProperties(testtlb_fetch)
37+
if(NOT testtlb_fetch_POPULATED)
38+
FetchContent_Populate(testtlb_fetch)
39+
endif()
40+
41+
endif()
42+
43+
nuttx_add_application(
44+
NAME
45+
testtlb
46+
PRIORITY
47+
${CONFIG_BENCHMARK_TESTTLB_PRIORITY}
48+
STACKSIZE
49+
${CONFIG_BENCHMARK_TESTTLB_STACKSIZE}
50+
MODULE
51+
${CONFIG_BENCHMARK_TESTTLB}
52+
COMPILE_FLAGS
53+
${CFLAGS}
54+
SRCS
55+
${SRCS})
56+
endif()

benchmarks/test-tlb/Kconfig

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#
2+
# Copyright (C) 2024 Xiaomi Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
config BENCHMARK_TESTTLB
18+
tristate "Memory Latency profiling"
19+
depends on ALLOW_GPL_COMPONENTS
20+
default n
21+
---help---
22+
Measure the memory latency
23+
24+
if BENCHMARK_TESTTLB
25+
26+
config BENCHMARK_TESTTLB_PRIORITY
27+
int "TestTLB task priority"
28+
default 100
29+
30+
config BENCHMARK_TESTTLB_STACKSIZE
31+
int "TestTLB stack size"
32+
default DEFAULT_TASK_STACKSIZE
33+
34+
endif

benchmarks/test-tlb/Make.defs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#
2+
# Copyright (C) 2024 Xiaomi Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
ifneq ($(CONFIG_BENCHMARK_TESTTLB),)
18+
CONFIGURED_APPS += $(APPDIR)/benchmarks/test-tlb
19+
endif

benchmarks/test-tlb/Makefile

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#
2+
# Copyright (C) 2024 Xiaomi Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
include $(APPDIR)/Make.defs
18+
19+
PROGNAME = testtlb
20+
PRIORITY = $(CONFIG_BENCHMARK_TESTTLB_PRIORITY)
21+
STACKSIZE = $(CONFIG_BENCHMARK_TESTTLB_STACKSIZE)
22+
MODULE = $(CONFIG_BENCHMARK_TESTTLB)
23+
24+
TESTTLB_UNPACK = test-tlb
25+
TESTTLB_GIT = github.com/torvalds/test-tlb
26+
TESTTLB_URL = https://github.com/torvalds/test-tlb/archive
27+
TESTTLB_VERSION = master
28+
TESTTLB_ZIP = $(TESTTLB_UNPACK)-$(TESTTLB_VERSION).zip
29+
UNPACK ?= unzip -q -o
30+
31+
$(TESTTLB_ZIP):
32+
@echo "Downloading: $(TESTTLB_URL)"
33+
$(Q) curl -L $(TESTTLB_URL)/$(TESTTLB_VERSION).zip -o $(TESTTLB_UNPACK)-$(TESTTLB_VERSION).zip
34+
35+
$(TESTTLB_UNPACK): $(TESTTLB_ZIP)
36+
@echo "Unpacking: $(TESTTLB_ZIP) -> $(TESTTLB_UNPACK)"
37+
$(Q) $(UNPACK) $(TESTTLB_ZIP)
38+
$(Q) mv test-tlb-$(TESTTLB_VERSION) $(TESTTLB_UNPACK)
39+
$(Q) touch $(TESTTLB_UNPACK)
40+
@echo "Patching: Applying patch"
41+
$(Q) cd test-tlb && patch -p1 < ../0001-test-tlb-port-for-NuttX.patch
42+
43+
ifeq ($(wildcard $(TESTTLB_UNPACK)/.git),)
44+
context:: $(TESTTLB_UNPACK)
45+
46+
distclean::
47+
$(call DELDIR, $(TESTTLB_UNPACK))
48+
$(call DELFILE, $(TESTTLB_ZIP))
49+
endif
50+
51+
MAINSRC = test-tlb/test-tlb.c
52+
53+
include $(APPDIR)/Application.mk

0 commit comments

Comments
 (0)