-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemorymgr.cpp
More file actions
337 lines (314 loc) · 11.3 KB
/
memorymgr.cpp
File metadata and controls
337 lines (314 loc) · 11.3 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
#include "memorymgr.h"
// #include "heap.hpp"
#include <utility>
//---------------------------------------------------------------------------------
//===========[ MemoryMgr ]=========================================================
// Initialize all the memory and regions
// Takes a std::vector list of sizes.
// Assuming index of size corresponds to level
bool MemoryMgr::initialize_memory( unsigned long int heapsize )
{
// Do I send in a vector of NAMES for the regions?
this->m_alloc_region = this->new_region( MemoryMgr::ALLOC,
0 ); // Level 0 is required.
this->m_size = heapsize;
this->m_free = heapsize;
if (this->m_alloc_region == NULL) {
cerr << "Unable to allocate in our simulator. REAL OOM in your system. Quitting." << endl;
exit(1);
}
return true;
}
// TODO // Do a garbage collection
// TODO // Returns number of bytes collected
// TODO int MemoryMgr::do_collection()
// TODO {
// TODO return 0;
// TODO }
// Returns true if GC threshold has been exceeded
// false otherwise.
bool MemoryMgr::should_do_collection()
{
assert(false);
// NOT NEEDED NOW.
// Assume that threshold is always valid.
return (this->m_liveSize >= this->m_GC_byte_threshold);
}
// Returns true if allocation caused garbage collection.
// false otherwise.
bool MemoryMgr::allocate( Object *object,
unsigned int create_time,
unsigned int new_alloc_time )
{
assert(this->m_alloc_region);
int collected = 0; // Amount collected
bool GCdone = false;
this->m_alloc_time = new_alloc_time;
ObjectSet_t::iterator iter = this->m_live_set.find( object );
if (iter != this->m_live_set.end()) {
// Found a dupe.
// Always return true, but ignore the actual allocation.
return true;
}
// Decisions for collection should be done here at the MemoryMgr level.
unsigned int objSize = object->getSize();
if (objSize > this->m_free) {
// Not enough free space.
// 1. We collect on a failed allocation.
collected = this->m_alloc_region->collect( create_time, new_alloc_time );
// Record how many edges were traced and add to total
this->m_mark_nonregion_total += this->m_nonregion_edges.size();
// Add the freed space back to free.
this->m_free += collected;
GCdone = true;
if (objSize > this->m_free) {
// Out Of Memory. Game over.
cerr << "OOM: free = " << this->m_free
<< " | objsize = " << objSize
<< " | collected = " << collected << endl;
return false;
}
}
this->m_alloc_region->allocate( object, create_time );
this->m_free -= objSize; // Free goes down.
this->m_used += objSize; // Used goes up.
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.
if (temp < this->m_liveSize) {
cerr << "ERROR: Wraparound " << temp << " < " << this->m_liveSize << endl;
}
this->m_liveSize = 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;
}
// NOT NEEDED NOW: if (!GCdone && this->should_do_collection()) {
// NOT NEEDED NOW: collected += this->m_alloc_region->collect( create_time );
// NOT NEEDED NOW: GCdone = true;
// NOT NEEDED NOW: }
if (GCdone) {
// Increment the GC count
this->m_times_GC++;
}
return true;
}
// Create new region with the given name.
// Returns a reference to the region.
Region *MemoryMgr::new_region( string ®ion_name,
int level )
{
// Debug
cerr << "Creating a new region[ " << region_name << " ]: "
<< " | level = " << level << endl;
RegionMap_t::iterator iter = this->m_region_map.find(region_name);
// Blow up if we create a new region with the same name.
assert(iter == this->m_region_map.end());
assert(level >= 0); // TODO make this more informative
Region *regptr = new Region( region_name, level );
assert(regptr); // TODO make this more informative
this->m_region_map[region_name] = regptr;
return regptr;
}
bool MemoryMgr::makeDead( Object *object, unsigned int death_time )
{
// Which region? Since we only have one region in this basic MemmoryMgr:
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;
}
bool result = this->m_alloc_region->makeDead( object );
if (!object->isDead()) {
object->makeDead( death_time, this->m_alloc_time );
}
// TODO: Think about whether calling object->makeDead is better in region::makeDead
return result;
}
// On an U(pdate) event
void MemoryMgr::add_edge( ObjectId_t src,
ObjectId_t tgt )
{
// DEBUG cout << "Adding edge (" << src << "," << tgt << ")" << endl;
//----------------------------------------------------------------------
// Add to edge maps
// Source is NOT in special group because there IS no special group.
// Target is NOT in special group for the same reason.
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 MemoryMgr::remove_edge( ObjectId_t src,
ObjectId_t oldTgtId )
{
// DEBUG cout << "Remove edge (" << src << "," << oldTgtId << ")" << endl;
ObjectId2SetMap_t::iterator iter;
this->m_attempts_edges_removed++;
//----------------------------------------------------------------------
// Remove edge from region maps
// Look in nonregion because we know everything else is empty
// in the base MemoryMgr.
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++;
} else {
// TODO: DEBUG for edges not found?
// TODO: At least keep track of how many edges weren't found.
}
return;
}
// - TODO
void MemoryMgr::remove_from_srcidmap( ObjectId_t src,
ObjectId_t oldTgtId )
{
//----------------------------------------------------------------------
// Remove from appropriate lookup maps
ObjectId2SetMap_t::iterator miter = this->m_srcidmap.find(src);
if (miter != this->m_srcidmap.end()) {
// Found the source.
// Look for the old target id in the set
ObjectIdSet_t &myset = miter->second;
ObjectIdSet_t::iterator itmp = myset.find(oldTgtId);
if (itmp != myset.end()) {
// Remove old target
myset.erase(itmp);
}
// DEBUG
// itmp = myset.find(oldTgtId);
// assert( itmp == myset.end() );
}
}
// - TODO
void MemoryMgr::remove_from_tgtidmap( ObjectId_t src,
ObjectId_t tgtId )
{
ObjectId2SetMap_t::iterator miter = this->m_tgtidmap.find(tgtId);
if (miter != this->m_tgtidmap.end()) {
// Found the old target.
// Look for the source id in the set
ObjectIdSet_t &myset = miter->second;
ObjectIdSet_t::iterator itmp = myset.find(src);
if (itmp != myset.end()) {
myset.erase(itmp);
}
// DEBUG
// itmp = myset.find(src);
// assert( itmp == myset.end() );
}
}
// - TODO Documentation
void MemoryMgr::remove_object( ObjectId_t objId )
{
ObjectId2SetMap_t::iterator iter;
//------------------------------------------------------------
// Look for objId in m_srcidmap
iter = this->m_srcidmap.find(objId);
if (iter != this->m_srcidmap.end()) {
// Found the objId as source.
// Go through the and remove all outgoing edges:
ObjectIdSet_t &myset = iter->second;
ObjectIdSet_t::iterator siter = myset.begin();
while (siter != myset.end()) {
// Remove target (src, *siter)
this->remove_edge( objId, *siter );
siter++;
}
this->m_srcidmap.erase(iter);
}
//------------------------------------------------------------
// Look for objId in m_tgtidmap
iter = this->m_tgtidmap.find(objId);
if (iter != this->m_tgtidmap.end()) {
// Found the objId as target.
// Go through the and remove all incoming edges:
ObjectIdSet_t &myset = iter->second;
ObjectIdSet_t::iterator siter = myset.begin();
while (siter != myset.end()) {
// Remove target (src, *siter)
this->remove_edge( *siter, objId );
siter++;
}
this->m_tgtidmap.erase(iter);
}
}
deque<GCRecord_t> MemoryMgr::get_GC_history()
{
deque<GCRecord_t> result;
for ( RegionMap_t::iterator iter = this->m_region_map.begin();
iter != this->m_region_map.end();
++iter ) {
Region *ptr = iter->second;
deque<GCRecord_t> myhist = ptr->get_GC_history();
result.insert( result.end(), myhist.begin(), myhist.end() );
}
return result;
}
bool MemoryMgr::is_in_live_set( Object *object )
{
ObjectSet_t::iterator iter = this->m_live_set.find( object );
return (iter != this->m_live_set.end());
}
//==============================================================================
// Debug functions
void Region::print_status()
{
cout << "Region[ " << this->m_name << " ]" << endl
<< " - live : " << this->m_live << endl
<< " - garbage: " << this->m_garbage << endl;
}
void MemoryMgr::print_status()
{
}
unsigned long int MemoryMgr::get_num_GC_attempts( bool printflag )
{
// For every region that we manage:
// get the number of GC attempts per region.
// if printflag: print
// TODO.
return 0;
}