-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbankGroup.cpp
292 lines (260 loc) · 10.4 KB
/
bankGroup.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
291
292
#include<cstdint>
#include<vector>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<map>
#include "bankGroup.h"
#include "helper.h"
BankGroup::BankGroup(Config *config) {
this->addressGroups = new vector<AddressGroup *>();
this->rowConflictThreshold = config->getRowConflictThreshold();
this->blockSize = config->getInitialBlockSize();
this->nCompareAddresses = config->getNumberOfGroupAddressesToCompare();
this->nMeasurementsPerComparison = config->getNumberOfMeasurementsPerGroupAddressComparisons();
this->fenced = config->areMemoryFencesEnabled();
this->maxRetriesForBankIndexSearch = config->getMaximumNumberOfRetriesForBankGrouping();
this->config = config;
nInitialTHPs = 0;
}
BankGroup::~BankGroup() {
delete addressGroups;
}
void BankGroup::addAddressToBankGroup(void *address) {
addAddressToBankGroup(address, true);
}
bool BankGroup::addAddressToExistingBankGroup(void *address) {
return addAddressToBankGroup(address, false);
}
bool BankGroup::addAddressToBankGroup(void *address, bool allowNewGroupCreation) {
int64_t bankIndex = getBankIndexForAddress(address);
if(bankIndex == -1) {
if(allowNewGroupCreation) {
AddressGroup *newAddressGroup = new AddressGroup(config);
newAddressGroup->addAddressToGroup(address);
addressGroups->push_back(newAddressGroup);
} else {
//printLogMessage(LOG_DEBUG, "Address did not match any bank and was not added.");
}
return false;
}
if(bankIndex >= (int64_t)(addressGroups->size())) {
printLogMessage(LOG_WARNING, "The measured index (" + to_string(bankIndex) + ") is bigger than the number of banks (" + to_string(addressGroups->size()) + ").");
return false;
}
(*addressGroups)[bankIndex]->addAddressToGroup(address);
return true;
}
void BankGroup::addTHPToBankGroup(void *address) {
addTHPToBankGroup(address, true);
}
uint64_t BankGroup::addTHPToExistingBankGroup(void *address) {
return addTHPToBankGroup(address, false);
}
uint64_t BankGroup::addTHPToBankGroup(void *address, bool allowNewGroupCreation) {
nInitialTHPs++;
uint64_t nErrors = 0;
uint64_t logEntryId = printLogMessage(LOG_DEBUG, "Add THP to banks...");
for(uint64_t i = config->getStartOffset(); i < (config->getEndOffset() * sysconf(_SC_PAGESIZE)) / blockSize; i++) {
if(!addAddressToBankGroup((void *)((volatile char *)address + i * blockSize), allowNewGroupCreation)) {
nErrors++;
}
updateLogMessage(LOG_DEBUG, "Added address " + to_string(i + 1) + " of " + to_string((config->getPagesPerTHP() * sysconf(_SC_PAGESIZE)) / blockSize), logEntryId);
}
updateLogMessage(LOG_DEBUG, "Added all addresses of the THP to the banks. There are " + to_string(addressGroups->size()) + " groups now.", logEntryId);
return nErrors;
}
int64_t BankGroup::getBankIndexForAddress(void *address) {
for(uint64_t i = 0; i < maxRetriesForBankIndexSearch + 1; i++) {
uint64_t biggestTime = 0;
int64_t biggestTimeIdx = -1;
for(uint64_t idx = 0; idx < addressGroups->size(); idx++) {
AddressGroup *group = (*addressGroups)[idx];
uint64_t time = group->compareAddressTiming(address);
if(time >= rowConflictThreshold && time > biggestTime) {
//printf("[DEBUG]: Measured access time %ld >= %ld against group %ld with %ld measurements.\n", time, rowConflictThreshold, idx, nMeasurementsPerComparison);
biggestTime = time;
biggestTimeIdx = idx;
}
}
if(biggestTimeIdx != -1) {
return biggestTimeIdx;
}
}
return -1;
}
void BankGroup::regroupAllAddresses() {
uint64_t addressGroupSize = addressGroups->size();
uint64_t logEntryId = printLogMessage(LOG_DEBUG, "Regrouping...");
for(uint64_t idx = 0; idx < addressGroupSize; idx++) {
updateLogMessage(LOG_DEBUG, "Regrouping " + to_string(idx + 1) + " of " + to_string(addressGroupSize), logEntryId);
AddressGroup *group = (*addressGroups)[0];
addressGroups->erase(addressGroups->begin());
for(void *address : *(group->getAddresses())) {
addAddressToBankGroup(address);
}
}
updateLogMessage(LOG_DEBUG, "Regrouping done. There are " + to_string(addressGroups->size()) + " groups now.", logEntryId);
}
uint64_t BankGroup::getNumberOfBanks() {
return addressGroups->size();
}
uint64_t BankGroup::getBlockSize() {
return blockSize;
}
void BankGroup::expandBlocks(uint64_t oldBlockSize, uint64_t newBlockSize) {
uint64_t nNewAddresses = 0;
uint64_t nErrors = 0;
uint64_t addressGroupIdx = 0;
for(AddressGroup *addressGroup: *addressGroups) {
addressGroupIdx++;
uint64_t addressGroupSize = addressGroup->getAddresses()->size();
uint64_t nNewAddressesPerGroup = 0;
uint64_t nErrorsPerGroup = 0;
for(uint64_t addressIdx = 0; addressIdx < addressGroupSize; addressIdx++) {
void *address = (*addressGroup->getAddresses())[addressIdx];
if(uint64_t(address) % oldBlockSize != 0) {
// Address does not start a block of oldBlockSize, so it is a new address
// and does not have to be expanded
continue;
}
for(uint64_t offset = newBlockSize; offset < oldBlockSize; offset+= newBlockSize) {
void *newAddress = (void *)((uint64_t)address + offset);
if(!addAddressToExistingBankGroup(newAddress)) {
nErrorsPerGroup++;
}
nNewAddressesPerGroup++;
}
}
nErrors += nErrorsPerGroup;
nNewAddresses += nNewAddressesPerGroup;
}
printLogMessage(LOG_DEBUG, "Expansion of address groups done. Added " + to_string(nNewAddresses) + " new addresses with " + to_string(nErrors) + " errors..");
}
void BankGroup::simplifyBlocks(uint64_t oldBlockSize, uint64_t newBlockSize) {
for(AddressGroup *addressGroup: *addressGroups) {
sort(addressGroup->getAddresses()->begin(), addressGroup->getAddresses()->end(), less<void *>());
vector<uint64_t> indicesToRemove;
uint64_t lastBlockBegin = 0;
uint64_t validOffset = oldBlockSize;
for(uint64_t i = 0; i < addressGroup->getAddresses()->size(); i++) {
uint64_t currentAddress = (uint64_t)((*addressGroup->getAddresses())[i]);
if(currentAddress % newBlockSize != 0) {
indicesToRemove.push_back(i);
if(currentAddress == lastBlockBegin + validOffset) {
validOffset += oldBlockSize;
}
} else {
if(validOffset != newBlockSize) {
indicesToRemove.push_back(i);
}
lastBlockBegin = currentAddress;
validOffset = oldBlockSize;
}
}
uint64_t offset = 0;
for(uint64_t idx : indicesToRemove) {
addressGroup->getAddresses()->erase(addressGroup->getAddresses()->begin() + idx - offset);
offset++;
}
}
}
void BankGroup::setBlockSizeInSteps(uint64_t newBlockSize) {
if(newBlockSize == blockSize) {
return;
}
if(!isNumberPowerOfTwo(newBlockSize)) {
printLogMessage(LOG_WARNING, "The submitted block size " + to_string(newBlockSize) + " is not a power of 2, performing auto detection.");
detectBlockSize();
return;
}
uint64_t logEntryId = printLogMessage(LOG_DEBUG, "");
while(blockSize != newBlockSize) {
if(newBlockSize > blockSize) {
updateLogMessage(LOG_DEBUG, "Increasing block size to " + to_string(getBlockSize()*2) + ".", logEntryId);
setBlockSize(blockSize*2);
} else {
updateLogMessage(LOG_DEBUG, "Decreasing block size to " + to_string(getBlockSize()/2) + ".", logEntryId);
setBlockSize(blockSize/2);
}
}
}
void BankGroup::setBlockSize(uint64_t newBlockSize) {
if(newBlockSize < blockSize) {
expandBlocks(blockSize, newBlockSize);
} else if(newBlockSize > blockSize) {
simplifyBlocks(blockSize, newBlockSize);
}
for(AddressGroup *addressGroup: *addressGroups) {
addressGroup->setBlockSize(blockSize);
}
blockSize = newBlockSize;
}
void BankGroup::detectBlockSize() {
bool firstRun = true;
uint64_t logEntryId = 0;
while(guessBlockSize() == getBlockSize() && getBlockSize() > config->getMinumumBlockSize()) {
if(firstRun) {
logEntryId = printLogMessage(LOG_DEBUG, "");
firstRun = false;
}
updateLogMessage(LOG_DEBUG, "Current block size is bigger or equal to the actual block size. Reducing block size to " + to_string(getBlockSize()/2) + ".", logEntryId);
setBlockSize(getBlockSize()/2);
}
setBlockSize(guessBlockSize());
config->setBlockSize(getBlockSize());
}
void BankGroup::print(string prefix, bool listAddressGroups, bool listAddresses) {
printf("%sThe bank group contains %ld banks.\n", prefix.c_str(), addressGroups->size());
if(listAddressGroups) {
for(AddressGroup *addressGroup : *addressGroups) {
addressGroup->print(prefix + " ", listAddresses);
}
}
}
bool BankGroup::numberOfBanksIsPowerOfTwo() {
uint64_t numberOfBanks = getNumberOfBanks();
return isNumberPowerOfTwo(numberOfBanks);
}
vector<vector<uint64_t>*> *BankGroup::getPhysicalAddresses() {
vector<vector<uint64_t>*> *groupedPhysicalAddresses = new vector<vector<uint64_t>*>();
for(AddressGroup *addressGroup : *addressGroups) {
vector<uint64_t> *physicalAddresses = new vector<uint64_t>();
for(void *address : *(addressGroup->getAddresses())) {
physicalAddresses->push_back((uint64_t)getPhysicalAddressForVirtualAddress(address));
}
groupedPhysicalAddresses->push_back(physicalAddresses);
}
return groupedPhysicalAddresses;
}
uint64_t BankGroup::guessBlockSize() {
map<uint64_t, uint64_t> followupAddressMap;
for(AddressGroup *addressGroup: *addressGroups) {
sort(addressGroup->getAddresses()->begin(), addressGroup->getAddresses()->end(), less<void *>());
uint64_t followupAddresses = 0;
uint64_t lastAddress = (uint64_t)(*(addressGroup->getAddresses()))[0] - blockSize;
for(void *address: *(addressGroup->getAddresses())) {
if(lastAddress + blockSize == (uint64_t) address) {
followupAddresses++;
} else {
if(followupAddressMap.count(followupAddresses)) {
followupAddressMap[followupAddresses]++;
} else {
followupAddressMap[followupAddresses] = 1;
}
followupAddresses = 1;
}
lastAddress = (uint64_t)address;
}
}
uint64_t guessedBlockSize = 0;
uint64_t maxGuesses = 0;
for(map<uint64_t, uint64_t>::iterator iterator = followupAddressMap.begin(); iterator != followupAddressMap.end(); iterator++) {
//printf("Guessing block size %ld (x%ld)\n", iterator->first * blockSize, iterator->second);
if(iterator->second > maxGuesses) {
maxGuesses = iterator->second;
guessedBlockSize = iterator->first * blockSize;
}
}
return guessedBlockSize;
}