-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemorymgrdef.cpp
More file actions
378 lines (368 loc) · 14.1 KB
/
memorymgrdef.cpp
File metadata and controls
378 lines (368 loc) · 14.1 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//==============================================================================
// MemoryMgrDef
//==============================================================================
#include "heap.hpp"
#include "memorymgrdef.hpp"
bool MemoryMgr::debug = false;
string MemoryMgr::ALLOC = "ALLOC";
string MemoryMgrDef::SPECIAL= "SPECIAL";
// string MemoryMgrDef::ALLOC = "ALLOC";
class Object;
bool MemoryMgrDef::allocate( Object *object,
unsigned int create_time,
unsigned int new_alloc_time )
{
assert(this->m_alloc_region);
int collected_regular = 0; // Amount collected from the REGULAR region
int collected_special = 0; // Amount collected from the SPECIAL region
this->m_alloc_time = new_alloc_time;
// Decisions for collection should be done here at the MemoryMgr level.
// Check if we have space
unsigned int objSize = object->getSize();
if (objSize > this->m_free) {
// Not enough free space.
// 1. We collect the REGULAR region first on a failed allocation.
collected_regular = this->m_alloc_region->collect( create_time, new_alloc_time );
// Increment the GC count
this->m_times_GC++;
// Count the mark count
this->m_mark_nonregion_total += ( this->m_nonregion_edges.size() +
this->m_in_edges_p->size() +
this->m_in_edges_p->size() );
// Add back the collected
this->m_free += collected_regular;
if (objSize > this->m_free) {
// We still couldn't allocate enough so:
// 2. Try again with the SPECIAL region.
collected_special += this->m_defregion_p->collect( create_time, new_alloc_time );
// Add back the SPECIAL collected
this->m_free += collected_special;
// Count the mark cost since we had to collect the SPECIAL region
this->m_mark_region_total += this->m_nonregion_edges.size();
if (objSize > this->m_free) {
// Out Of Memory.
cerr << "OOM: free = " << this->m_free
<< " | objsize = " << objSize
<< " | collected regular = " << collected_regular
<< " | collected special = " << collected_special << endl;
return false;
}
} else {
// This is the mark savings.
this->m_mark_saved_total += this->m_region_edges_p->size();
}
}
// This has to be true because of the collections
assert( objSize <= this->m_free );
// Check for duplicates
auto iter_live = this->m_live_set.find( object );
if (iter_live != this->m_live_set.end()) {
// Found a dupe.
// Always return true, but ignore the actual allocation.
return true;
}
// Check which region to allocate into
ObjectId_t objId = object->getId();
auto iter_sgroup = this->m_specgroup.find(objId);
if (iter_sgroup != this->m_specgroup.end()) {
// allocate into SPECIAL (aka DEFERRED) group
this->m_defregion_p->allocate( object, create_time );
} else {
// allocate into REGULAR group
this->m_alloc_region->allocate( object, create_time );
}
unsigned long int temp = this->m_liveSize + object->getSize();
// Max live size calculation
// We silently peg to ULONG_MAX the wraparound.
// TODO: Maybe we should just error here as this probably isn't possible.
this->m_liveSize = ( (temp < this->m_liveSize) ? ULONG_MAX : temp );
// Add to live set.
this->m_live_set.insert( object );
// Keep tally of what our maximum live size is for the program run
if (this->m_maxLiveSize < this->m_liveSize) {
this->m_maxLiveSize = this->m_liveSize;
}
this->m_free -= objSize;
return true;
}
// On an U(pdate) event
void MemoryMgrDef::add_edge( ObjectId_t src,
ObjectId_t tgt )
{
// DEBUG
// cout << "Adding edge (" << src << "," << tgt << ")";
ObjectIdSet_t::iterator iter = this->m_specgroup.find(src);
ObjectIdSet_t::iterator tgt_iter = this->m_specgroup.find(tgt);
//----------------------------------------------------------------------
// Add to edge maps
if (iter != this->m_specgroup.end()) {
// Source is in special group
if (tgt_iter != this->m_specgroup.end()) {
// DEBUG: cout << "..[src in SPECIAL] ";
// Target is in special group
ObjectId2SetMap_t::iterator itmp = this->m_region_edges_p->find(src);
if (itmp != this->m_region_edges_p->end()) {
// Already in the map
ObjectIdSet_t &myset = itmp->second;
myset.insert(tgt);
} else {
// Not in the map
ObjectIdSet_t myset;
myset.insert(tgt);
(*this->m_region_edges_p)[src] = myset;
}
// // DEBUG ONLY
// itmp = this->m_region_edges_p->find(src);
// assert(itmp != this->m_region_edges_p->end());
// ObjectIdSet_t &myset = itmp->second;
// ObjectIdSet_t::iterator tmpiter = myset.find(tgt);
// assert(tmpiter != myset.end());
// // END DEBUG
this->m_region_edges_count++;
} else {
// Target is NOT in special group
// cout << "..[src in REGULAR] ";
ObjectId2SetMap_t::iterator itmp = this->m_out_edges_p->find(src);
if (itmp != this->m_out_edges_p->end()) {
// Already in the map
ObjectIdSet_t &myset = itmp->second;
myset.insert(tgt);
} else {
// Not in the map
ObjectIdSet_t myset;
myset.insert(tgt);
(*this->m_out_edges_p)[src] = myset;
}
this->m_out_edges_count++;
}
} else {
// Source is NOT in special group
// cout << "..[src in REGULAR] ";
if (tgt_iter != this->m_specgroup.end()) {
// Target is in special group
// DEBUG: cout << "..[tgt in SPECIAL] ";
ObjectId2SetMap_t::iterator itmp = this->m_in_edges_p->find(src);
if (itmp != this->m_in_edges_p->end()) {
// Already in the map
ObjectIdSet_t &myset = itmp->second;
myset.insert(tgt);
} else {
// Not in the map
ObjectIdSet_t myset;
myset.insert(tgt);
(*this->m_in_edges_p)[src] = myset;
}
this->m_in_edges_count++;
} else {
// cout << "..[tgt in REGULAR] ";
// Target is NOT in special group
ObjectId2SetMap_t::iterator itmp = this->m_nonregion_edges.find(src);
if (itmp != this->m_nonregion_edges.end()) {
// Already in the map
ObjectIdSet_t &myset = itmp->second;
myset.insert(tgt);
} else {
// Not in the map
ObjectIdSet_t myset;
myset.insert(tgt);
this->m_nonregion_edges[src] = myset;
}
this->m_nonregion_edges_count++;
}
}
//----------------------------------------------------------------------
// Add to look up maps
// Src map
ObjectId2SetMap_t::iterator miter = this->m_srcidmap.find(src);
if (miter != this->m_srcidmap.end()) {
// Already exists
this->m_srcidmap[src].insert(tgt);
} else {
// Doesn't exist
ObjectIdSet_t tmpset;
tmpset.insert(tgt);
this->m_srcidmap[src] = tmpset;
}
//----------------------------------------------------------------------
// Tgt map
miter = this->m_tgtidmap.find(tgt);
if (miter != this->m_tgtidmap.end()) {
// Already exists
this->m_tgtidmap[src].insert(src);
} else {
// Doesn't exist
ObjectIdSet_t tmpset;
tmpset.insert(src);
this->m_tgtidmap[tgt] = tmpset;
}
}
void MemoryMgrDef::remove_edge( ObjectId_t src,
ObjectId_t oldTgtId )
{
// DEBUG
// cout << "DEF: Remove edge (" << src << "," << oldTgtId << ")" << endl;
ObjectId2SetMap_t::iterator iter;
this->m_attempts_edges_removed++;
//----------------------------------------------------------------------
// Remove edge from region maps
// Look in the special region
iter = this->m_region_edges_p->find(src);
if (iter != this->m_region_edges_p->end()) {
// Found in region
ObjectIdSet_t &myset = iter->second;
myset.erase(oldTgtId);
this->m_edges_removed++;
goto END;
}
// Look outside the special region
iter = this->m_nonregion_edges.find(src);
if (iter != this->m_nonregion_edges.end()) {
// Found in nonregion
ObjectIdSet_t &myset = iter->second;
myset.erase(oldTgtId);
this->m_edges_removed++;
goto END;
}
// Look in the in to out
iter = this->m_in_edges_p->find(src);
if (iter != this->m_in_edges_p->end()) {
// Found in IN region
ObjectIdSet_t &myset = iter->second;
myset.erase(oldTgtId);
this->m_edges_removed++;
goto END;
}
// Look in the out to in
iter = this->m_out_edges_p->find(src);
if (iter != this->m_out_edges_p->end()) {
// Found in IN region
ObjectIdSet_t &myset = iter->second;
myset.erase(oldTgtId);
this->m_edges_removed++;
goto END;
// Well this isn't needed but symmetric.
// If ever there's any new code after this, this makes it less likely
// that a bug's introduced.
}
END:
return;
}
bool MemoryMgrDef::initialize_memory( unsigned long int heapsize )
{
// Do I send in a vector of NAMES for the regions?
MemoryMgr::initialize_memory( heapsize );
this->m_defregion_p = this->new_region( MemoryMgrDef::SPECIAL,
1 );
// The super-edge sets
this->m_region_edges_p = new ObjectId2SetMap_t();
this->m_in_edges_p = new ObjectId2SetMap_t();
this->m_out_edges_p = new ObjectId2SetMap_t();
return true;
}
// Initialize the grouped region of objects
bool MemoryMgrDef::initialize_special_group( string &group_filename,
int numgroups )
{
cout << "initialize_special_group: ";
std::ifstream infile( group_filename );
string line;
string s;
this->m_numgroups = numgroups;
// assert( numgroups == 1 );
int group_count = 0;
// The file is a CSV file with the following header:
// groupId,number,death_time,list
// First line is a header:
std::getline(infile, line);
// TODO: Maybe make sure we have the right file?
// Check the header which should be exactly like this:
// groupId,number,death_time,list
while (std::getline(infile, line)) {
size_t pos = 0;
string token;
unsigned long int num;
int count = 0;
//------------------------------------------------------------
// Get the group Id
pos = line.find(",");
assert( pos != string::npos );
s = line.substr(0, pos);
// DEBUG: cout << "GID: " << s << endl;
int groupId = std::stoi(s);
line.erase(0, pos + 1);
//------------------------------------------------------------
// Get the number of objects in the group
pos = line.find(",");
assert( pos != string::npos );
s = line.substr(0, pos);
// DEBUG: cout << "NUM: " << s << endl;
int total = std::stoi(s);
line.erase(0, pos + 1);
//------------------------------------------------------------
// Get the deathtime
pos = line.find(",");
assert( pos != string::npos );
s = line.substr(0, pos);
// DEBUG: cout << "DTIME: " << s << endl;
int dtime = std::stoi(s);
// TODO TODO TODO TODO
// This assumes that there's only one region here, so it doesn't work if
// there are multiple regions. To do multiple regions, we need a map from
// region number to region. This way it should match the groups text file
// we are getting the death groups information.
this->m_defregion_p->set_region_deathtime(dtime);
line.erase(0, pos + 1);
//------------------------------------------------------------
// Get the object Ids
while ((pos = line.find(",")) != string::npos) {
token = line.substr(0, pos);
num = std::stoi(token);
line.erase(0, pos + 1);
++count;
this->m_specgroup.insert(num);
}
// Get the last number
num = std::stoi(line);
this->m_specgroup.insert(num);
++count;
++group_count;
// DEBUG
cout << "Special group[ " << groupId << " ]: "
<< "total objects read in = " << total << " | "
<< "set size = " << this->m_specgroup.size() << endl;
// END DEBUG
if (group_count >= numgroups) {
break;
}
}
return true;
}
// TODO DOC
bool MemoryMgrDef::makeDead( Object *object, unsigned int death_time )
{
// Check which region the object belongs to:
bool result;
ObjectId_t objId = object->getId();
auto iter_sgroup = this->m_specgroup.find(objId);
if (iter_sgroup != this->m_specgroup.end()) {
// In the SPECIAL (aka DEFERRED) group
result = this->m_defregion_p->makeDead( object );
} else {
// In the REGULAR group
result = this->m_alloc_region->makeDead( object );
}
unsigned long int temp = this->m_liveSize - object->getSize();
if (temp > this->m_liveSize) {
// OVERFLOW, underflow?
this->m_liveSize = 0;
cout << "UNDERFLOW of substraction." << endl;
// TODO If this happens, maybe we should think about why it happens.
} else {
// All good. Fight on.
this->m_liveSize = temp;
}
if (!object->isDead()) {
object->makeDead( death_time, this->m_alloc_time );
}
return result;
}