-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbenchmark_3.cc
More file actions
213 lines (189 loc) · 6.29 KB
/
Copy pathbenchmark_3.cc
File metadata and controls
213 lines (189 loc) · 6.29 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
// Debugging Settings
//#define DEBUG
//#define DEBUG_V1
//#define DEBUG_V2
//#define DEBUG_V3
//#define DEBUG_V4
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include "benchmark_common.h"
// Debugging
#include <typeinfo>
#include <assert.h>
alignas(long long) static char pool[1ull << 30];
template<typename ALLOC>
void run_allocation(const unsigned long long T_bytes, const unsigned long long A_bytes, const unsigned long long S_bytes, ALLOC alloc) {
// T_bytes = Total amount of memory to be allocated
// A_bytes = Active memory. The maximum amount of memory to be allocated at a time
// S_bytes = Size in bytes of chunk to be allocated
// alloc = Allocator to be used to allocate memory
#ifdef DEBUG_V2
std::cout << "Running Allocation" << std::endl;
#endif // DEBUG_V2
const unsigned long long A_count = A_bytes / S_bytes;
const unsigned long long remaining_count = (T_bytes - A_bytes) / S_bytes;
std::vector<char *> allocated;
allocated.reserve(A_count);
escape(allocated.data());
std::clock_t c_start = std::clock();
#ifdef DEBUG_V2
std::cout << "Running initial allocation for " << allocated.size() << " chunks of " << S_bytes << " bytes" << std::endl;
#endif // DEBUG_V2
for (unsigned long long i = 0; i < A_count; i++) {
allocated.emplace_back(alloc.allocate(S_bytes));
++(*allocated[i]);
}
clobber();
#ifdef DEBUG_V2
std::cout << "Running additional allocation for " << remaining_count << " chunks of " << S_bytes << " bytes" << std::endl;
#endif // DEBUG_V2
for (unsigned long long i = 0; i < remaining_count; i++)
{
unsigned long long idx = i % A_count;
alloc.deallocate(allocated[idx], S_bytes);
allocated[idx] = alloc.allocate(S_bytes);
++(*allocated[idx]);
if (idx == A_count - 1) {
#ifdef DEBUG_V2
std::cout << "Clobbering memory after reaching index " << i << " in loop (" << idx << " in vector)" << std::endl;
std::cout << "Clobber number: " << i / idx << std::endl;
#endif // DEBUG_V2
clobber();
}
}
std::clock_t c_end = std::clock();
std::cout << (c_end - c_start) * 1.0 / CLOCKS_PER_SEC << " " << std::flush;
#ifdef DEBUG_V2
std::cout << "Deallocating remaining " << A_count << " chunks" << std::endl;
#endif // DEBUG_V2
for (unsigned long long i = 0; i < A_count; i++) {
alloc.deallocate(allocated[i], S_bytes);
}
}
void run_row(size_t T, size_t A, size_t S) {
// T = Total amount of memory to be allocated (power of 2)
// A = Active memory - maximum amount of memory to be allocated at a time (power of 2)
// S = Size in bytes of chunk to be allocated (power of 2)
unsigned long long expanded_T = 1ull << T;
unsigned long long expanded_A = 1ull << A;
unsigned long long expanded_S = 1ull << S;
unsigned long long chunks = 1ull << (A - S);
#ifdef DEBUG_V1
std::cout << "T: 2^" << T << "=" << expanded_T << " A: 2^" << A << "=" << expanded_A << " S: 2^" << S << "=" << expanded_S << " Chunks: " << chunks << std::endl;
#endif // DEBUG_V1
std::cout << "T=2^" << T << " A=2^" << A << " S=2^" << S << " " << std::flush;
for (size_t i = 0; i < 8; i++)
{
int pid = fork();
if (pid == 0) { // Child process
switch (i)
{
case 0: {
#ifdef DEBUG_V2
std::cout << std::endl << "AS1" << std::endl;
#endif // DEBUG_V2
std::allocator<char> alloc;
run_allocation<std::allocator<char>>(expanded_T, expanded_A, expanded_S, alloc);
break;
}
case 1: {
#ifdef DEBUG_V2
std::cout << std::endl << "AS2" << std::endl;
#endif // DEBUG_V2
BloombergLP::bslma::NewDeleteAllocator alloc;
run_allocation<typename alloc_adaptors<char>::polymorphic>(expanded_T, expanded_A, expanded_S, &alloc);
break;
}
case 2: {
#ifdef DEBUG_V2
std::cout << std::endl << "AS3" << std::endl;
#endif // DEBUG_V2
BloombergLP::bdlma::BufferedSequentialAllocator alloc(pool, sizeof(pool));
run_allocation<typename alloc_adaptors<char>::monotonic>(expanded_T, expanded_A, expanded_S, &alloc);
break;
}
case 3: {
#ifdef DEBUG_V2
std::cout << std::endl << "AS5" << std::endl;
#endif // DEBUG_V2
BloombergLP::bdlma::BufferedSequentialAllocator alloc(pool, sizeof(pool));
run_allocation<typename alloc_adaptors<char>::polymorphic>(expanded_T, expanded_A, expanded_S, &alloc);
break;
}
case 4: {
#ifdef DEBUG_V2
std::cout << std::endl << "AS7" << std::endl;
#endif // DEBUG_V2
BloombergLP::bdlma::MultipoolAllocator alloc;
run_allocation<typename alloc_adaptors<char>::multipool>(expanded_T, expanded_A, expanded_S, &alloc);
break;
}
case 5: {
#ifdef DEBUG_V2
std::cout << std::endl << "AS9" << std::endl;
#endif // DEBUG_V2
BloombergLP::bdlma::MultipoolAllocator alloc;
run_allocation<typename alloc_adaptors<char>::polymorphic>(expanded_T, expanded_A, expanded_S, &alloc);
break;
}
case 6: {
#ifdef DEBUG_V2
std::cout << std::endl << "AS11" << std::endl;
#endif // DEBUG_V2
BloombergLP::bdlma::BufferedSequentialAllocator underlying_alloc(pool, sizeof(pool));
BloombergLP::bdlma::MultipoolAllocator alloc(&underlying_alloc);
run_allocation<typename alloc_adaptors<char>::multipool>(expanded_T, expanded_A, expanded_S, &alloc);
break;
}
case 7: {
#ifdef DEBUG_V2
std::cout << std::endl << "AS13" << std::endl;
#endif // DEBUG_V2
BloombergLP::bdlma::BufferedSequentialAllocator underlying_alloc(pool, sizeof(pool));
BloombergLP::bdlma::MultipoolAllocator alloc(&underlying_alloc);
run_allocation<typename alloc_adaptors<char>::polymorphic>(expanded_T, expanded_A, expanded_S, &alloc);
break;
}
default:
break;
}
exit(0);
}
else {
int error;
wait(&error);
if (error) {
#ifdef DEBUG_V1
std::cout << "Error code " << error << "at T: " << T << " A: " << A << " S: " << S << std::endl;
#endif // DEBUG_V1
std::cout << "FAIL " << std::flush;
}
}
}
std::cout << std::endl;
}
void run_table(size_t T) {
std::cout << std::endl << "Running table of memory size 2^" << T << " bytes" << std::endl;
run_row(T, 15, 10);
run_row(T, 16, 10);
run_row(T, 17, 10);
run_row(T, 18, 10);
run_row(T, 19, 10);
run_row(T, 20, 10);
run_row(T, 20, 11);
run_row(T, 20, 12);
run_row(T, 20, 13);
run_row(T, 20, 14);
run_row(T, 20, 15);
}
int main(int argc, char *argv[]) {
std::cout << "Started" << std::endl;
run_table(30);
run_table(31);
run_table(32);
run_table(33);
run_table(34);
run_table(35);
std::cout << "Done" << std::endl;
}