/
simulateArrayLetsHeap2.cpp
290 lines (230 loc) · 9.47 KB
/
simulateArrayLetsHeap2.cpp
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#ifndef SIMULATE_ARRAYLETS_HEAP2
#define SIMULATE_ARRAYLETS_HEAP2
#include <iostream>
#include <fstream>
#include <string>
#define LINUX_ARRAYLET
#include "util.hpp"
// To run:
// For MAC
// g++ -g3 -Wno-write-strings -std=c++11 simulateArrayLetsHeap2.cpp -o simulateArrayLetsHeap2
// For Linux with no c++11 support
// g++ -g3 -Wno-write-strings -std=c++0x simulateArrayLetsHeap2.cpp -o simulateArrayLetsHeap2
// Note: Insert -lrt flag for linux systems
// ./simulateArrayLetsHeap2 12 1000
char * mmapContiguous(size_t totalArraySize, size_t arrayletSize, int fhs[], char * addresses[], int32_t flags)
{
int mmapProt = 0;
int mmapFlags = 0;
mmapProt = PROT_READ | PROT_WRITE;
if(flags & MMAP_FLAG_SHARED_ANON) {
mmapFlags = MAP_SHARED | MAP_ANON;
} else if(flags & MMAP_FLAG_PRIVATE_ANON) {
mmapFlags = MAP_PRIVATE | MAP_ANON;
} else {
std::cerr << "Flags parameter not recognized.\n";
return NULL;
}
char * contiguousMap = (char *)mmap(
NULL,
totalArraySize, // File size
mmapProt,
mmapFlags, // Must be shared
-1,
0);
if (contiguousMap == MAP_FAILED) {
std::cerr << "Failed to mmap contiguousMap\n";
return NULL;
}
// else {
// std::cout << "Successfully mmaped contiguousMap at address: " << (void *)contiguousMap << "\n";
// }
addresses[ARRAYLET_COUNT] = contiguousMap;
mmapFlags = MAP_SHARED | MAP_FIXED;
for (size_t i = 0; i < ARRAYLET_COUNT; i++) {
addresses[i] = (char *)mmap(
(void *)(contiguousMap+i*arrayletSize),
arrayletSize, // File size
mmapProt,
mmapFlags,
fhs[i],
0);
if (addresses[i] == MAP_FAILED) {
std::cout << "Failed to mmap addresses[" << i << "]\n";
return NULL;
}
// else {
// std::cout << "Successfully mmaped leaf at address: " << i << ": " << (void *)addresses[i] << " :" << *addresses[i] << "\n";
// }
}
return contiguousMap;
}
char * copyModifyManualHeap(size_t pagesize, size_t arrayletSize, size_t totalArraySize, long arrayLetOffsets[], char * heapMmap)
{
char * tempArray = (char *)malloc(totalArraySize);
// for(size_t i = 0; i < ARRAYLET_COUNT; i++) {
// for(size_t j = 0; j < arrayletSize; j++)
// {
// tempArray[j+(i*arrayletSize)] = (heapMmap+arrayLetOffsets[i])[j];
// }
// }
for(size_t i = 0; i < ARRAYLET_COUNT; i++) {
std::memcpy(tempArray+(i*arrayletSize), heapMmap+arrayLetOffsets[i],arrayletSize);
}
// Modify temporary array with asterisks
for(size_t i = 0; i < 256; i++) {
for(size_t j = 0; j < ARRAYLET_COUNT; j++)
{
tempArray[i+j*arrayletSize] = '*';
tempArray[i+j*arrayletSize+(arrayletSize/4)] = '*';
tempArray[i+j*arrayletSize+(arrayletSize/4)] = '*';
}
}
// Copy tempArray back into the heap
int offsetIdx = 0;
// for(size_t i = 0, j = 0; i < totalArraySize-ARRAYLET_COUNT; i++) {
// (heapMmap+arrayLetOffsets[offsetIdx])[j++] = tempArray[i];
// if(i != 0 && (i % (arrayletSize-1) == 0)) {
// (heapMmap+arrayLetOffsets[offsetIdx])[--j] = '\0';
// offsetIdx++;
// j = 0;
// }
// }
for(size_t i = 0; i < ARRAYLET_COUNT; i++) {
std::memcpy(heapMmap+arrayLetOffsets[i], tempArray+(i*arrayletSize), arrayletSize);
(heapMmap+arrayLetOffsets[i])[arrayletSize-2] = '\0';
}
return tempArray;
}
/**
* Uses shm_open and ftruncate to simulate heap as well as
* arraylets. Populates heap in random locations with numbers
* to then alocate another contiguous block of memory to make
* the arraylets look contiguous.
*/
int main(int argc, char** argv) {
if (argc != 3) {
std::cout<<"USAGE: " << argv[0] << " seed# iterations#" << std::endl;
std::cout << "Example: " << argv[0] << " 6363 50000" << std::endl;
return 1;
}
ElapsedTimer timer;
PaddedRandom rnd;
int seed = atoi(argv[1]);
int iterations = atoi(argv[2]);
rnd.setSeed(seed);
size_t pagesize = getpagesize(); // 4096 bytes
std::cout << "System page size: " << pagesize << " bytes.\n";
size_t arrayletSize = getArrayletSize(pagesize);
std::cout << "arrayletSize size: " << arrayletSize << " bytes.\n";
// 1. Simulate heap by allocating 256MB of memory
// No read, write or exec priviledges given
int mmapProt = 0;
int mmapFlags = 0;
mmapProt = PROT_NONE;
mmapFlags = MAP_SHARED | MAP_ANON;
char * heapMmap = (char *)mmap(
NULL,
FOUR_GB, // File size
mmapProt,
mmapFlags, // Must be shared
-1, // File handle
0);
if (heapMmap == MAP_FAILED) {
std::cerr << "Failed to mmap\n";
return 1;
} else {
std::cout << "Successfully mmaped heapMmap at address: " << (void *)heapMmap << "\n";
}
// 2. Populate heap in random locations with simulated arraylets
int fhs[ARRAYLET_COUNT]; // File handles for each arraylet
char * arrayletAddrs[ARRAYLET_COUNT];
size_t totalArraySize = 0;
char * tempNums[SIXTEEN] = {"33", "55", "66", "88", "99", "00", "11", "22", "33", "77", "AA", "EE", "CC", "BB", "DD", "FF"};
char * arrayletNames[ARRAYLET_COUNT];
char * nums[ARRAYLET_COUNT];
long arrayLetOffsets[ARRAYLET_COUNT];
for(size_t i = 0; i < ARRAYLET_COUNT; i++) {
std::string numStr = std::to_string((long long int)i);
numStr = "t" + numStr;
arrayletNames[i] = strdup(numStr.c_str());
nums[i] = tempNums[i%SIXTEEN];
arrayLetOffsets[i] = getPageAlignedOffset(pagesize, rnd.nextNatural() % FOUR_GB);
}
for(size_t i = 0; i < ARRAYLET_COUNT; i++)
{
fhs[i] = shm_open(arrayletNames[i], O_RDWR | O_CREAT | O_EXCL, 0600);
if (fhs[i] == -1) {
std::cerr << "Error while reading file " << arrayletNames[i] << "\n";
return 1;
}
shm_unlink(arrayletNames[i]);
ftruncate(fhs[i], arrayletSize);
arrayletAddrs[i] = (char *)mmap(
(void *)(heapMmap+arrayLetOffsets[i]),
arrayletSize, // File size
PROT_READ|PROT_WRITE,
MAP_SHARED | MAP_FIXED, // Must be shared
fhs[i], // File handle
0);
if (arrayletAddrs[i] == MAP_FAILED) {
std::cerr << "Failed to mmap\n";
return 1;
} else {
std::cout << "Successfully mmaped arrayletAddrs[" << i << "] at address: " << (void *)arrayletAddrs[i] << "\n";
}
for (size_t j = 0; j < arrayletSize; j++) {
strncpy(arrayletAddrs[i]+j, nums[i], 1);
totalArraySize++;
}
}
std::cout << "Arraylets created successfully.\n";
std::cout << "ArrayLets combined have size: " << totalArraySize << " bytes." << '\n';
// ************************************************************************************************
char * addresses[ARRAYLET_COUNT + 1];
double perIter[iterations], ignoreTimes[iterations];
// char * contiguousMap = mmapContiguous(totalArraySize, arrayletSize, fhs, addresses);
// modifyContiguousMem(pagesize, arrayletSize, contiguousMap);
// copyModifyManualHeap(pagesize, totalArraySize, arrayLetOffsets, heapMmap);
timer.startTimer();
double lastTime = timer.getElapsedMicros();
double middleTime = lastTime;
for(size_t i = 0; i < iterations; i++) {
// 3. Make Arraylets look contiguous with mmap
char * contiguousMap = mmapContiguous(totalArraySize, arrayletSize, fhs, addresses, MMAP_FLAG_SHARED_ANON);
// ************************************************************************************************
// 4. Modify contiguous memory view and observe change in the heap
modifyContiguousMem(pagesize, arrayletSize, contiguousMap);
// Free addresses
middleTime = timer.getElapsedMicros();
freeAddresses(addresses, arrayletSize);
// // 3. 4. Both copy arraylets into a separate array, modify this array to then copy it back to the heap
// char * allocArray = copyModifyManualHeap(pagesize, arrayletSize, totalArraySize, arrayLetOffsets, heapMmap);
// middleTime = timer.getElapsedMicros();
// freeAllocArray((void *)allocArray);
perIter[i] = timer.getElapsedMicros() - lastTime;
lastTime = timer.getElapsedMicros();
ignoreTimes[i] = lastTime - middleTime;
}
int64_t elapsedTime = timer.getElapsedMicros();
size_t perIterationSum = 0;
size_t ignoreTotal = 0;
for(size_t i = 0; i < iterations; i++) {
perIterationSum += perIter[i];
ignoreTotal += ignoreTimes[i];
}
size_t avgPerIter = perIterationSum / iterations;
size_t avgIgnore = ignoreTotal / iterations;
printResults(elapsedTime, ignoreTotal, avgPerIter, avgIgnore);
// Prints arraylets from heap location
// for(size_t i = 0; i < ARRAYLET_COUNT; i++) {
// std::cout << "heapMmap+arrayLetOffsets[" << i << "]: ";
// for(size_t j = 0; j < arrayletSize; j++) {
// std::cout << (heapMmap+arrayLetOffsets[i])[j];
// }
// std::cout << std::endl;
// }
munmap(heapMmap, FOUR_GB);
return 0;
}
#endif /* SIMULATE_ARRAYLETS_HEAP2 */