1+ #include < iostream>
2+ #include < queue>
3+ #include < snmalloc/snmalloc.h>
4+ #include < thread>
5+ #include < vector>
6+
7+ int main ()
8+ {
9+ std::vector<std::thread> threads;
10+ std::atomic<size_t > running;
11+ snmalloc::Stat requests;
12+
13+ for (size_t i = 0 ; i < 16 ; i++)
14+ {
15+ std::thread ([&running, &requests]() {
16+ std::queue<size_t *> q;
17+ while (true )
18+ {
19+ snmalloc::ScopedAllocator alloc;
20+ running++;
21+
22+ if (rand () % 1000 == 0 )
23+ {
24+ // Deallocate everything in the queue
25+ while (q.size () > 0 )
26+ {
27+ auto p = q.front ();
28+ requests -= *p;
29+ alloc->dealloc (p);
30+ q.pop ();
31+ }
32+ }
33+
34+ for (size_t j = 0 ; j < 1000 ; j++)
35+ {
36+ if (q.size () >= 20000 || (q.size () > 0 && (rand () % 10 == 0 )))
37+ {
38+ auto p = q.front ();
39+ requests -= *p;
40+ alloc->dealloc (p);
41+ q.pop ();
42+ }
43+ else
44+ {
45+ size_t size =
46+ (rand () % 1024 == 0 ) ? 16 * 1024 * (1 << (rand () % 3 )) : 48 ;
47+ requests += size;
48+ auto p = (size_t *)alloc->alloc (size);
49+ *p = size;
50+ q.push (p);
51+ }
52+ }
53+
54+ running--;
55+ std::this_thread::sleep_for (std::chrono::microseconds (rand () % 2000 ));
56+ }
57+ }).detach ();
58+ }
59+
60+ std::thread ([&requests]() {
61+ size_t count = 0 ;
62+ while (count < 2000 )
63+ {
64+ count++;
65+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
66+ // std::cout << "Inflight: " <<
67+ // snmalloc::RemoteDeallocCache::remote_inflight << std::endl; std::cout
68+ // << "Current reservation: " << snmalloc::Globals::get_current_usage() <<
69+ // std::endl; std::cout << "Peak reservation: " <<
70+ // snmalloc::Globals::get_peak_usage() << std::endl; std::cout <<
71+ // "Allocator count: " << snmalloc::Globals::pool().get_count() <<
72+ // std::endl; std::cout << "Running threads: " << running <<
73+ // std::endl; std::cout << "Index: " << count << std::endl;
74+ // std::cout << "------------------------------------------" << std::endl;
75+ std::cout << count << " ," << snmalloc::Alloc::Config::Backend::get_peak_usage () << " ,"
76+ << snmalloc::Alloc::Config::Backend::get_current_usage () << " ," << requests.get_curr ()
77+ << " ," << requests.get_peak () << " ,"
78+ << snmalloc::RemoteDeallocCache::remote_inflight.get_peak ()
79+ << " ," << snmalloc::RemoteDeallocCache::remote_inflight.get_curr ()
80+ << std::endl;
81+ snmalloc::print_alloc_stats<snmalloc::Alloc::Config>();
82+ }
83+ }).join ();
84+
85+ return 0 ;
86+ }
0 commit comments