-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
260 lines (222 loc) · 8.14 KB
/
main.cpp
File metadata and controls
260 lines (222 loc) · 8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#include "memory_allocator.h" // your final memory_allocator.h with optional reclamation
#include <iostream>
#include <thread>
#include <vector>
#include <random>
#include <chrono>
#include <cstdlib>
// We'll define an interface for system vs fancy
class AllocInterface {
public:
virtual ~AllocInterface() {}
virtual void* allocate(size_t sz)=0;
virtual void deallocate(void* ptr)=0;
virtual bool hasStats() const { return false; }
virtual AllocStatsSnapshot getStats() const { return {0,0,0,0}; }
};
// A wrapper for FancyPerThreadAllocator
class FancyInterface : public AllocInterface {
public:
FancyInterface(size_t arenaSize, bool reclamation)
: fancy_(arenaSize, reclamation)
{}
void* allocate(size_t sz) override {
return fancy_.allocate(sz);
}
void deallocate(void* ptr) override {
fancy_.deallocate(ptr);
}
bool hasStats() const override { return true; }
AllocStatsSnapshot getStats() const override {
return fancy_.getStatsSnapshot();
}
// Simple async request structure
struct AsyncRequest {
size_t size;
void* result;
bool completed;
};
// Async allocation - returns immediately with a request object
std::shared_ptr<AsyncRequest> allocateAsync(size_t size) {
auto request = std::make_shared<AsyncRequest>();
request->size = size;
// Fast path - do it immediately if not too busy
request->result = allocate(size);
request->completed = true;
return request;
}
// Async deallocation - queue it up and return immediately
void deallocateAsync(void* ptr) {
if (!ptr) return;
// Fast path - do it immediately if not too busy
deallocate(ptr);
}
// Process any pending async operations
void processAsyncBatch() {
// Nothing to do in this simplified implementation
// since we're doing everything immediately
}
private:
FancyPerThreadAllocator fancy_;
};
// A wrapper for system malloc
class SystemInterface : public AllocInterface {
public:
void* allocate(size_t sz) override { return std::malloc(sz); }
void deallocate(void* ptr) override { std::free(ptr); }
};
// We'll do an ephemeral HPC scenario with ring buffer
void ephemeralWorker(AllocInterface* alloc, int ops, int ringSize)
{
struct Slot { void* ptr; int ttl; };
std::vector<Slot> ring(ringSize, {nullptr, 0});
int pos=0;
std::default_random_engine rng(std::random_device{}());
std::uniform_int_distribution<int> catDist(1,100);
std::uniform_int_distribution<int> smallDist(16,256);
std::uniform_int_distribution<int> medDist(512,2048);
std::uniform_int_distribution<int> largeDist(4096,32768);
std::uniform_int_distribution<int> ttlDist(50,2000);
for(int i=0; i<ops; i++){
auto& slot = ring[pos];
// free if expired
if(slot.ptr && slot.ttl <= 0){
alloc->deallocate(slot.ptr);
slot.ptr = nullptr;
}
// decrement TTL
if(slot.ptr && slot.ttl>0){
slot.ttl--;
}
// if empty => allocate new
if(!slot.ptr){
int c = catDist(rng);
size_t sz=0;
if(c<=60) sz=smallDist(rng);
else if(c<=90) sz=medDist(rng);
else sz=largeDist(rng);
void* p = alloc->allocate(sz);
if(p){
slot.ptr = p;
slot.ttl = ttlDist(rng);
}
}
pos = (pos+1) % ringSize;
}
// final free
for(auto& s : ring){
if(s.ptr){
alloc->deallocate(s.ptr);
s.ptr=nullptr;
}
}
}
// Function to run a warmup phase
void runWarmup(AllocInterface* alloc, int threads, int opsPerThread, int ringSize) {
std::cout << "Running warmup phase... ";
std::cout.flush();
// Use fewer operations for warmup to save time
int warmupOps = opsPerThread / 10;
std::vector<std::thread> ths;
ths.reserve(threads);
for(int i=0; i<threads; i++){
ths.emplace_back(ephemeralWorker, alloc, warmupOps, ringSize);
}
for(auto& t : ths){
t.join();
}
std::cout << "done." << std::endl;
// Optional: Add a small delay to let any background activity settle
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// We'll do a timed test for ephemeral HPC scenario
struct TestResult {
long long elapsedUs;
AllocStatsSnapshot snap;
};
TestResult runEphemeralTest(AllocInterface* alloc, int threads, int opsPerThread, int ringSize, bool doWarmup = true)
{
// Run warmup phase if requested
if (doWarmup) {
runWarmup(alloc, threads, opsPerThread, ringSize);
}
// Reset stats if possible
if (alloc->hasStats()) {
// We can't actually reset the stats, but we can take a snapshot now
// and subtract from the final result
auto beforeSnap = alloc->getStats();
auto start=std::chrono::high_resolution_clock::now();
std::vector<std::thread> ths;
ths.reserve(threads);
for(int i=0; i<threads; i++){
ths.emplace_back(ephemeralWorker, alloc, opsPerThread, ringSize);
}
for(auto& t : ths){
t.join();
}
auto end=std::chrono::high_resolution_clock::now();
long long us = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
auto afterSnap = alloc->getStats();
TestResult r;
r.elapsedUs = us;
r.snap.totalAllocCalls = afterSnap.totalAllocCalls - beforeSnap.totalAllocCalls;
r.snap.totalFreeCalls = afterSnap.totalFreeCalls - beforeSnap.totalFreeCalls;
r.snap.currentUsedBytes = afterSnap.currentUsedBytes;
r.snap.peakUsedBytes = afterSnap.peakUsedBytes;
return r;
} else {
// For allocators without stats, just time the operation
auto start=std::chrono::high_resolution_clock::now();
std::vector<std::thread> ths;
ths.reserve(threads);
for(int i=0; i<threads; i++){
ths.emplace_back(ephemeralWorker, alloc, opsPerThread, ringSize);
}
for(auto& t : ths){
t.join();
}
auto end=std::chrono::high_resolution_clock::now();
long long us = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
TestResult r;
r.elapsedUs = us;
r.snap = {0, 0, 0, 0};
return r;
}
}
int main(){
// HPC ephemeral big test
int threads = 128;
int opsPerThread = 1000000;
int ringSize = 100000;
std::cout << "\n=== Compare System Malloc vs. Fancy(Off) vs. Fancy(On) under HPC ephemeral scenario ===\n";
std::cout << "Threads= " << threads << ", Ops/Thread= " << opsPerThread << ", ringSize= " << ringSize << "\n";
// 1) System
{
SystemInterface sys;
auto r = runEphemeralTest(&sys, threads, opsPerThread, ringSize);
std::cout << "\n-- System malloc/free --\n";
std::cout << "Elapsed (us): " << r.elapsedUs << "\n";
}
// 2) Fancy Reclamation OFF
{
FancyInterface fancyNoReclaim(64ULL*1024ULL*1024ULL, false);
auto r = runEphemeralTest(&fancyNoReclaim, threads, opsPerThread, ringSize);
std::cout << "\n-- Fancy Per-Thread (Reclamation OFF) --\n";
std::cout << "Elapsed (us): " << r.elapsedUs << "\n";
std::cout << "Alloc calls : " << r.snap.totalAllocCalls
<< ", Free calls: " << r.snap.totalFreeCalls
<< ", Peak usage: " << r.snap.peakUsedBytes << "\n";
}
// 3) Fancy Reclamation ON
{
FancyInterface fancyReclaim(64ULL*1024ULL*1024ULL, true);
auto r = runEphemeralTest(&fancyReclaim, threads, opsPerThread, ringSize);
std::cout << "\n-- Fancy Per-Thread (Reclamation ON) --\n";
std::cout << "Elapsed (us): " << r.elapsedUs << "\n";
std::cout << "Alloc calls : " << r.snap.totalAllocCalls
<< ", Free calls: " << r.snap.totalFreeCalls
<< ", Peak usage: " << r.snap.peakUsedBytes << "\n";
}
std::cout << "\nAll tests completed.\n";
return 0;
}