7
7
#include <errno.h>
8
8
9
9
#define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
10
- #define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)
11
10
12
11
// 内存读写测试函数
13
12
void test_memory_rw (void * ptr , size_t size , const char * page_type ) {
@@ -47,17 +46,8 @@ void test_individual_alloc_rw_free() {
47
46
printf ("2MB page allocated at %p\n" , ptr_2m );
48
47
test_memory_rw (ptr_2m , 2 * 1024 * 1024 , "2MB" );
49
48
assert (munmap (ptr_2m , 2 * 1024 * 1024 ) == 0 );
50
- printf ("2MB page freed\n" );
51
-
52
- // 1GB 页面测试
53
- void * ptr_1g = mmap (NULL , 1024 * 1024 * 1024 , PROT_READ | PROT_WRITE ,
54
- MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB , -1 , 0 );
55
- assert (ptr_1g != MAP_FAILED );
56
- printf ("1GB page allocated at %p\n" , ptr_1g );
57
- test_memory_rw (ptr_1g , 1024 * 1024 * 1024 , "1GB" );
58
- assert (munmap (ptr_1g , 1024 * 1024 * 1024 ) == 0 );
59
- printf ("1GB page freed\n" );
60
-
49
+ printf ("2MB page freed\n" );
50
+
61
51
printf ("========== END test_individual_alloc_rw_free ==========\n" );
62
52
}
63
53
@@ -78,23 +68,15 @@ void test_batch_alloc_rw_free() {
78
68
assert (ptr_2m != MAP_FAILED );
79
69
printf ("Batch allocated 2MB page at %p\n" , ptr_2m );
80
70
81
- ptr_1g = mmap (NULL , 1024 * 1024 * 1024 , PROT_READ | PROT_WRITE ,
82
- MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB , -1 , 0 );
83
- assert (ptr_1g != MAP_FAILED );
84
- printf ("Batch allocated 1GB page at %p\n" , ptr_1g );
85
-
86
71
// 统一读写测试
87
72
test_memory_rw (ptr_4k , 4096 , "4KB batch" );
88
73
test_memory_rw (ptr_2m , 2 * 1024 * 1024 , "2MB batch" );
89
- test_memory_rw (ptr_1g , 1024 * 1024 * 1024 , "1GB batch" );
90
74
91
75
// 统一释放
92
76
assert (munmap (ptr_4k , 4096 ) == 0 );
93
77
printf ("Batch freed 4KB page\n" );
94
78
assert (munmap (ptr_2m , 2 * 1024 * 1024 ) == 0 );
95
79
printf ("Batch freed 2MB page\n" );
96
- assert (munmap (ptr_1g , 1024 * 1024 * 1024 ) == 0 );
97
- printf ("Batch freed 1GB page\n" );
98
80
99
81
printf ("========== END test_batch_alloc_rw_free ==========\n" );
100
82
}
@@ -103,7 +85,7 @@ void test_batch_alloc_rw_free() {
103
85
void test_interleaved_alloc_rw_free () {
104
86
printf ("========== START test_interleaved_alloc_rw_free ==========\n" );
105
87
106
- // 4KB -> 读写 -> 2MB -> 读写 -> 释放4KB -> 1GB -> 读写 -> 释放2MB -> 释放1GB
88
+ // 4KB -> 读写 -> 2MB -> 读写 -> 释放4KB-> 释放2MB
107
89
void * ptr_4k = mmap (NULL , 4096 , PROT_READ | PROT_WRITE ,
108
90
MAP_PRIVATE | MAP_ANONYMOUS , -1 , 0 );
109
91
assert (ptr_4k != MAP_FAILED );
@@ -117,19 +99,10 @@ void test_interleaved_alloc_rw_free() {
117
99
test_memory_rw (ptr_2m , 2 * 1024 * 1024 , "2MB interleaved" );
118
100
119
101
assert (munmap (ptr_4k , 4096 ) == 0 );
120
- printf ("Interleaved: freed 4KB page\n" );
121
-
122
- void * ptr_1g = mmap (NULL , 1024 * 1024 * 1024 , PROT_READ | PROT_WRITE ,
123
- MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB , -1 , 0 );
124
- assert (ptr_1g != MAP_FAILED );
125
- printf ("Interleaved: allocated 1GB page\n" );
126
- test_memory_rw (ptr_1g , 1024 * 1024 * 1024 , "1GB interleaved" );
127
-
102
+ printf ("Interleaved: freed 4KB page\n" );
103
+
128
104
assert (munmap (ptr_2m , 2 * 1024 * 1024 ) == 0 );
129
- printf ("Interleaved: freed 2MB page\n" );
130
-
131
- assert (munmap (ptr_1g , 1024 * 1024 * 1024 ) == 0 );
132
- printf ("Interleaved: freed 1GB page\n" );
105
+ printf ("Interleaved: freed 2MB page\n" );
133
106
134
107
printf ("========== END test_interleaved_alloc_rw_free ==========\n" );
135
108
}
@@ -166,23 +139,8 @@ void test_eager_vs_lazy_allocation() {
166
139
assert (ptr_2m_lazy != MAP_FAILED );
167
140
printf ("2MB lazy allocation completed\n" );
168
141
test_memory_rw (ptr_2m_lazy , 2 * 1024 * 1024 , "2MB lazy" );
169
- assert (munmap (ptr_2m_lazy , 2 * 1024 * 1024 ) == 0 );
170
-
171
- // 1GB 立即分配和懒分配
172
- void * ptr_1g_eager = mmap (NULL , 1024 * 1024 * 1024 , PROT_READ | PROT_WRITE ,
173
- MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB | MAP_POPULATE , -1 , 0 );
174
- assert (ptr_1g_eager != MAP_FAILED );
175
- printf ("1GB eager allocation completed\n" );
176
- test_memory_rw (ptr_1g_eager , 1024 * 1024 * 1024 , "1GB eager" );
177
- assert (munmap (ptr_1g_eager , 1024 * 1024 * 1024 ) == 0 );
178
-
179
- void * ptr_1g_lazy = mmap (NULL , 1024 * 1024 * 1024 , PROT_READ | PROT_WRITE ,
180
- MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB , -1 , 0 );
181
- assert (ptr_1g_lazy != MAP_FAILED );
182
- printf ("1GB lazy allocation completed\n" );
183
- test_memory_rw (ptr_1g_lazy , 1024 * 1024 * 1024 , "1GB lazy" );
184
- assert (munmap (ptr_1g_lazy , 1024 * 1024 * 1024 ) == 0 );
185
-
142
+ assert (munmap (ptr_2m_lazy , 2 * 1024 * 1024 ) == 0 );
143
+
186
144
printf ("All eager/lazy allocations freed\n" );
187
145
printf ("========== END test_eager_vs_lazy_allocation ==========\n" );
188
146
}
@@ -260,21 +218,7 @@ void test_linear_mapping() {
260
218
printf ("2MB linear mapping freed\n" );
261
219
} else {
262
220
printf ("2MB linear mapping failed\n" );
263
- }
264
-
265
- // 1GB 线性映射
266
- void * ptr_1g = mmap ((char * )base_addr + 0x20000000 , 1024 * 1024 * 1024 ,
267
- PROT_READ | PROT_WRITE ,
268
- MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_HUGETLB | MAP_HUGE_1GB ,
269
- -1 , 0 );
270
- if (ptr_1g != MAP_FAILED && ptr_1g == (char * )base_addr + 0x20000000 ) {
271
- printf ("1GB linear mapping at %p\n" , ptr_1g );
272
- test_memory_rw (ptr_1g , 1024 * 1024 * 1024 , "1GB linear" );
273
- assert (munmap (ptr_1g , 1024 * 1024 * 1024 ) == 0 );
274
- printf ("1GB linear mapping freed\n" );
275
- } else {
276
- printf ("1GB linear mapping failed\n" );
277
- }
221
+ }
278
222
279
223
printf ("========== END test_linear_mapping ==========\n" );
280
224
}
0 commit comments