Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 9 additions & 65 deletions apps/libc/c/mmap/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <errno.h>

#define MAP_HUGE_2MB (21 << MAP_HUGE_SHIFT)
#define MAP_HUGE_1GB (30 << MAP_HUGE_SHIFT)

// 内存读写测试函数
void test_memory_rw(void *ptr, size_t size, const char *page_type) {
Expand Down Expand Up @@ -47,17 +46,8 @@ void test_individual_alloc_rw_free() {
printf("2MB page allocated at %p\n", ptr_2m);
test_memory_rw(ptr_2m, 2 * 1024 * 1024, "2MB");
assert(munmap(ptr_2m, 2 * 1024 * 1024) == 0);
printf("2MB page freed\n");

// 1GB 页面测试
void *ptr_1g = mmap(NULL, 1024 * 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB, -1, 0);
assert(ptr_1g != MAP_FAILED);
printf("1GB page allocated at %p\n", ptr_1g);
test_memory_rw(ptr_1g, 1024 * 1024 * 1024, "1GB");
assert(munmap(ptr_1g, 1024 * 1024 * 1024) == 0);
printf("1GB page freed\n");

printf("2MB page freed\n");

printf("========== END test_individual_alloc_rw_free ==========\n");
}

Expand All @@ -78,23 +68,15 @@ void test_batch_alloc_rw_free() {
assert(ptr_2m != MAP_FAILED);
printf("Batch allocated 2MB page at %p\n", ptr_2m);

ptr_1g = mmap(NULL, 1024 * 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB, -1, 0);
assert(ptr_1g != MAP_FAILED);
printf("Batch allocated 1GB page at %p\n", ptr_1g);

// 统一读写测试
test_memory_rw(ptr_4k, 4096, "4KB batch");
test_memory_rw(ptr_2m, 2 * 1024 * 1024, "2MB batch");
test_memory_rw(ptr_1g, 1024 * 1024 * 1024, "1GB batch");

// 统一释放
assert(munmap(ptr_4k, 4096) == 0);
printf("Batch freed 4KB page\n");
assert(munmap(ptr_2m, 2 * 1024 * 1024) == 0);
printf("Batch freed 2MB page\n");
assert(munmap(ptr_1g, 1024 * 1024 * 1024) == 0);
printf("Batch freed 1GB page\n");

printf("========== END test_batch_alloc_rw_free ==========\n");
}
Expand All @@ -103,7 +85,7 @@ void test_batch_alloc_rw_free() {
void test_interleaved_alloc_rw_free() {
printf("========== START test_interleaved_alloc_rw_free ==========\n");

// 4KB -> 读写 -> 2MB -> 读写 -> 释放4KB -> 1GB -> 读写 -> 释放2MB -> 释放1GB
// 4KB -> 读写 -> 2MB -> 读写 -> 释放4KB-> 释放2MB
void *ptr_4k = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
assert(ptr_4k != MAP_FAILED);
Expand All @@ -117,19 +99,10 @@ void test_interleaved_alloc_rw_free() {
test_memory_rw(ptr_2m, 2 * 1024 * 1024, "2MB interleaved");

assert(munmap(ptr_4k, 4096) == 0);
printf("Interleaved: freed 4KB page\n");

void *ptr_1g = mmap(NULL, 1024 * 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB, -1, 0);
assert(ptr_1g != MAP_FAILED);
printf("Interleaved: allocated 1GB page\n");
test_memory_rw(ptr_1g, 1024 * 1024 * 1024, "1GB interleaved");

printf("Interleaved: freed 4KB page\n");

assert(munmap(ptr_2m, 2 * 1024 * 1024) == 0);
printf("Interleaved: freed 2MB page\n");

assert(munmap(ptr_1g, 1024 * 1024 * 1024) == 0);
printf("Interleaved: freed 1GB page\n");
printf("Interleaved: freed 2MB page\n");

printf("========== END test_interleaved_alloc_rw_free ==========\n");
}
Expand Down Expand Up @@ -166,23 +139,8 @@ void test_eager_vs_lazy_allocation() {
assert(ptr_2m_lazy != MAP_FAILED);
printf("2MB lazy allocation completed\n");
test_memory_rw(ptr_2m_lazy, 2 * 1024 * 1024, "2MB lazy");
assert(munmap(ptr_2m_lazy, 2 * 1024 * 1024) == 0);

// 1GB 立即分配和懒分配
void *ptr_1g_eager = mmap(NULL, 1024 * 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB | MAP_POPULATE, -1, 0);
assert(ptr_1g_eager != MAP_FAILED);
printf("1GB eager allocation completed\n");
test_memory_rw(ptr_1g_eager, 1024 * 1024 * 1024, "1GB eager");
assert(munmap(ptr_1g_eager, 1024 * 1024 * 1024) == 0);

void *ptr_1g_lazy = mmap(NULL, 1024 * 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB | MAP_HUGE_1GB, -1, 0);
assert(ptr_1g_lazy != MAP_FAILED);
printf("1GB lazy allocation completed\n");
test_memory_rw(ptr_1g_lazy, 1024 * 1024 * 1024, "1GB lazy");
assert(munmap(ptr_1g_lazy, 1024 * 1024 * 1024) == 0);

assert(munmap(ptr_2m_lazy, 2 * 1024 * 1024) == 0);

printf("All eager/lazy allocations freed\n");
printf("========== END test_eager_vs_lazy_allocation ==========\n");
}
Expand Down Expand Up @@ -260,21 +218,7 @@ void test_linear_mapping() {
printf("2MB linear mapping freed\n");
} else {
printf("2MB linear mapping failed\n");
}

// 1GB 线性映射
void *ptr_1g = mmap((char*)base_addr + 0x20000000, 1024 * 1024 * 1024,
PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_HUGETLB | MAP_HUGE_1GB,
-1, 0);
if (ptr_1g != MAP_FAILED && ptr_1g == (char*)base_addr + 0x20000000) {
printf("1GB linear mapping at %p\n", ptr_1g);
test_memory_rw(ptr_1g, 1024 * 1024 * 1024, "1GB linear");
assert(munmap(ptr_1g, 1024 * 1024 * 1024) == 0);
printf("1GB linear mapping freed\n");
} else {
printf("1GB linear mapping failed\n");
}
}

printf("========== END test_linear_mapping ==========\n");
}
Expand Down
28 changes: 28 additions & 0 deletions apps/libc/expect_off.out
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,31 @@ test_sigwait ok3
test_sigsuspend ok1
test_sigsuspend ok2
test_sigsuspend ok3

Starting comprehensive hugepage tests
========== START test_individual_alloc_rw_free ==========
4KB page freed
2MB page freed
========== END test_individual_alloc_rw_free ==========
========== START test_batch_alloc_rw_free ==========
Batch freed 4KB page
Batch freed 2MB page
========== END test_batch_alloc_rw_free ==========
========== START test_interleaved_alloc_rw_free ==========
Interleaved: freed 4KB page
Interleaved: freed 2MB page
========== END test_interleaved_alloc_rw_free ==========
========== START test_eager_vs_lazy_allocation ==========
4KB eager allocation completed
4KB lazy allocation completed
2MB eager allocation completed
2MB lazy allocation completed
All eager/lazy allocations freed
========== END test_eager_vs_lazy_allocation ==========
========== START test_file_mapping_hugepages ==========
========== END test_file_mapping_hugepages ==========
========== START test_linear_mapping ==========
4KB linear mapping freed
2MB linear mapping freed
========== END test_linear_mapping ==========
All hugepage tests completed successfully!
2 changes: 1 addition & 1 deletion apps/libc/testcase_list
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
helloworld_c
sleep_c
signal_c
mmap_c
mmap_c
Loading