Skip to content

Commit af80ccc

Browse files
committed
Add test for exploring memory usage
1 parent 8391cc4 commit af80ccc

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

src/test/func/cleanup/cleanup.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <snmalloc/snmalloc.h>
2+
3+
#include <iostream>
4+
#include <thread>
5+
6+
void ecall()
7+
{
8+
snmalloc::ScopedAllocator a;
9+
std::vector<void*> allocs;
10+
size_t count = 0;
11+
for (size_t j = 0; j < 1000; j++)
12+
{
13+
allocs.push_back(a.alloc.alloc(j % 1024));
14+
count += j % 1024;
15+
}
16+
auto p = a.alloc.alloc(1 * 1024 * 1024);
17+
memset(p, 0, 1 * 1024 * 1024);
18+
19+
for (size_t j = 0; j < allocs.size(); j++)
20+
a.alloc.dealloc(allocs[j]);
21+
22+
a.alloc.dealloc(p);
23+
}
24+
25+
void thread_body()
26+
{
27+
for (int i = 0; i < 10000; i++) {
28+
ecall();
29+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
30+
}
31+
}
32+
33+
void monitor_body()
34+
{
35+
for (int i = 0; i < 10000; i++) {
36+
std::cout << "Current: " << snmalloc::Alloc::StateHandle::get_current_usage() << std::endl;
37+
std::cout << "Peak : " << snmalloc::Alloc::StateHandle::get_peak_usage() << std::endl;
38+
std::cout << "Allocs : " << snmalloc::Alloc::StateHandle::pool().get_count() << std::endl;
39+
std::cout << "--------------------------------------------" << std::endl;
40+
std::this_thread::sleep_for(std::chrono::seconds(1));
41+
}
42+
}
43+
44+
int main()
45+
{
46+
std::vector<std::thread> threads;
47+
for (int i = 0; i < 8; i++)
48+
{
49+
threads.push_back(std::thread(thread_body));
50+
}
51+
threads.push_back(std::thread(monitor_body));
52+
53+
for (auto& t : threads)
54+
t.join();
55+
return 0;
56+
}

0 commit comments

Comments
 (0)