-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheap.cpp
More file actions
599 lines (547 loc) · 18.3 KB
/
heap.cpp
File metadata and controls
599 lines (547 loc) · 18.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
#include "heap.hpp"
#include "memorymgrdef.hpp"
// -- Global flags
bool HeapState::do_refcounting = true;
bool HeapState::debug = false;
unsigned int Object::g_counter = 0;
bool HeapState::initialize_memory_basic( unsigned long int heapsize )
{
// Initialize the BASIC memory manager
this->m_memmgr_p = new MemoryMgr();
bool result = this->m_memmgr_p->initialize_memory( heapsize );
return result;
}
bool HeapState::initialize_memory_deferred( unsigned long int heapsize,
string &group_filename,
int numgroups )
{
cout << "initialize_memory_deferred:" << endl;
// Initialize the BASIC memory manager
this->m_memmgrdef_p = new MemoryMgrDef();
this->m_memmgr_p = static_cast<MemoryMgr *>(this->m_memmgrdef_p);
bool result = this->m_memmgrdef_p->initialize_memory( heapsize );
if (!result) {
return false;
}
result = this->m_memmgrdef_p->initialize_special_group( group_filename, numgroups );
return result;
}
// Initialize memory for the GC simulator
// - heapsize in bytes
// - group_filename is the group2list.csv file output from dgroups2db.py
// - numgroups to include (right now this can only be one)
bool HeapState::initialize_memory_deferred_VER2( unsigned long heapsize,
string &group_filename,
int numgroups )
{
cout << "initialize_memory_deferred:" << endl;
// Initialize the BASIC memory manager
this->m_memmgrdef_p = new MemoryMgrDef();
this->m_memmgr_p = static_cast<MemoryMgr *>(this->m_memmgrdef_p);
bool result = this->m_memmgrdef_p->initialize_memory( heapsize );
if (!result) {
return false;
}
result = this->m_memmgrdef_p->initialize_special_group( group_filename, numgroups );
return result;
}
// Initialize memory for the Program Aware GC simulator
// - heapsize in bytes
bool HeapState::initialize_memory_PAGC_VER1( unsigned long heapsize,
string &PAGC_config_filename )
{
cout << "initialize_memory_PAGC_VER1:" << endl;
// Initialize the BASIC memory manager
this->m_memmgr_PAGC_VER1_p = new MemoryMgrDef();
this->m_memmgr_p = static_cast<MemoryMgr *>(this->m_memmgr_PAGC_VER1_p);
bool result = this->m_memmgr_PAGC_VER1_p->initialize_memory( heapsize );
if (!result) {
return false;
}
// TODO: result = this->m_memmgr_PAGC_VER1_p->initialize_special_group( group_filename, numgroups );
return result;
}
// - TODO
Object* HeapState::allocate( unsigned int id,
unsigned int size,
char kind,
char *type,
AllocSite *site,
unsigned int els,
Thread *thread,
unsigned int create_time )
{
// Design decision: allocation time isn't 0 based.
this->m_alloc_time += size;
Object* obj = new Object( id,
size,
kind,
type,
site,
els,
thread,
create_time,
this );
m_objects[obj->getId()] = obj;
// Call the Memory Manager allocate
bool success = this->m_memmgr_p->allocate( obj, create_time, this->getAllocTime() );
if (!success) {
cout << "OOM Error:"
<< " ObjId: " << obj->getId()
<< " type: " << obj->getType()
<< " size: " << obj->getSize() << endl;
exit(1);
}
return obj;
}
// -- Manage heap
Object* HeapState::getObject(unsigned int id)
{
ObjectMap::iterator p = m_objects.find(id);
if (p != m_objects.end()) {
return (*p).second;
}
else {
return 0;
}
}
Edge* HeapState::make_edge( Object* source,
FieldId_t field_id,
Object* target,
unsigned int cur_time )
{
Edge* new_edge = new Edge( source, field_id,
target, cur_time );
m_edges.insert(new_edge);
assert(target != NULL);
target->setPointedAtByHeap();
if (m_edges.size() % 100000 == 0) {
cout << "EDGES: " << m_edges.size() << endl;
}
return new_edge;
}
// - TODO
void HeapState::make_edge2( unsigned int objId, unsigned int tgtId )
{
this->m_memmgr_p->add_edge( objId, tgtId );
}
// - TODO
void HeapState::remove_edge2( unsigned int objId, unsigned int oldTgtId )
{
this->m_memmgr_p->remove_edge( objId, oldTgtId );
this->m_memmgr_p->remove_from_srcidmap( objId,
oldTgtId );
this->m_memmgr_p->remove_from_tgtidmap( objId,
oldTgtId );
}
void HeapState::makeDead(Object * obj, unsigned int death_time)
{
if (this->m_memmgr_p->is_in_live_set(obj)) {
this->m_memmgr_p->makeDead( obj, death_time );
}
}
Method * HeapState::get_method_death_site( Object *obj )
{
Method *dsite = obj->getDeathSite();
if (obj->getDiedByHeapFlag()) {
// DIED BY HEAP
if (!dsite) {
if (obj->wasDecrementedToZero()) {
// So died by heap but no saved death site. First alternative is
// to look for the a site that decremented to 0.
dsite = obj->getMethodDecToZero();
} else {
// TODO: No dsite here yet
// TODO TODO TODO
// This probably should be the garbage cycles. Question is
// where should we get this?
}
}
} else {
if (obj->getDiedByStackFlag()) {
// DIED BY STACK.
// Look for last heap activity.
dsite = obj->getLastMethodDecRC();
}
}
return dsite;
}
// TODO Documentation :)
void HeapState::end_of_program(unsigned int cur_time)
{
// We don't really care to clean up anything at the end of the program.
}
// TODO Documentation :)
void HeapState::set_reason_for_cycles( deque< deque<int> >& cycles )
{
for ( deque< deque<int> >::iterator it = cycles.begin();
it != cycles.end();
++it ) {
Reason reason = UNKNOWN_REASON;
unsigned int last_action_time = 0;
for ( deque<int>::iterator objit = it->begin();
objit != it->end();
++objit ) {
Object* object = this->getObject(*objit);
unsigned int objtime = object->getLastActionTime();
if (objtime > last_action_time) {
reason = object->getReason();
last_action_time = objtime;
}
}
for ( deque<int>::iterator objit = it->begin();
objit != it->end();
++objit ) {
Object* object = this->getObject(*objit);
object->setReason( reason, last_action_time );
}
}
}
NodeId_t HeapState::getNodeId( ObjectId_t objId, bimap< ObjectId_t, NodeId_t >& bmap ) {
bimap< ObjectId_t, NodeId_t >::left_map::const_iterator liter = bmap.left.find(objId);
if (liter == bmap.left.end()) {
// Haven't mapped a NodeId yet to this ObjectId
NodeId_t nodeId = bmap.size();
bmap.insert( bimap< ObjectId_t, NodeId_t >::value_type( objId, nodeId ) );
return nodeId;
} else {
// We have a NodeId
return liter->second;
}
}
unsigned long int HeapState::getLiveSize() const
{
return this->m_memmgr_p->getLiveSize();
}
unsigned long int HeapState::getMaxLiveSize() const
{
return this->m_memmgr_p->getMaxLiveSize();
}
deque<GCRecord_t> HeapState::get_GC_history()
{
return this->m_memmgr_p->get_GC_history();
}
int HeapState::get_number_of_collections() const
{
return this->m_memmgr_p->get_number_of_collections();
}
unsigned int HeapState::get_mark_total() const
{
return this->m_memmgr_p->get_mark_total();
}
unsigned int HeapState::get_mark_saved() const
{
return this->m_memmgr_p->get_mark_saved();
}
int HeapState::get_number_edges_removed() const
{
return this->m_memmgr_p->get_number_edges_removed();
}
int HeapState::get_number_attempts_edges_removed() const
{
return this->m_memmgr_p->get_number_attempts_edges_removed();
}
unsigned int HeapState::get_region_edges_count() const
{
return this->m_memmgr_p->get_region_edges_count();
}
unsigned int HeapState::get_in_edges_count() const
{
return this->m_memmgr_p->get_in_edges_count();
}
unsigned int HeapState::get_out_edges_count() const
{
return this->m_memmgr_p->get_out_edges_count();
}
unsigned int HeapState::get_nonregion_edges_count() const
{
return this->m_memmgr_p->get_nonregion_edges_count();
}
// -- Return a string with some information
string Object::info() {
stringstream ss;
ss << "OBJ 0x"
<< hex
<< m_id
<< dec
<< "("
<< m_type << " "
<< (m_site != 0 ? m_site->info() : "<NONE>")
<< " @"
<< m_createTime
<< ")";
return ss.str();
}
string Object::info2() {
stringstream ss;
ss << "OBJ 0x"
<< hex
<< m_id
<< dec
<< "("
<< m_type << " "
<< (m_site != 0 ? m_site->info() : "<NONE>")
<< " @"
<< m_createTime
<< ")"
<< " : " << (m_deathTime - m_createTime);
return ss.str();
}
void Object::updateField( Edge* edge,
FieldId_t fieldId,
unsigned int cur_time,
Method *method,
Reason reason,
Object *death_root )
{
EdgeMap::iterator p = this->m_fields.find(fieldId);
if (p != this->m_fields.end()) {
// -- Old edge
Edge* old_edge = p->second;
if (old_edge) {
// -- Now we know the end time
Object *old_target = old_edge->getTarget();
if (old_target) {
if (reason == HEAP) {
old_target->setHeapReason( cur_time );
} else if (reason == STACK) {
old_target->setStackReason( cur_time );
} else {
cerr << "Invalid reason." << endl;
assert( false );
}
old_target->decrementRefCountReal( cur_time,
method,
reason,
death_root );
}
old_edge->setEndTime(cur_time);
}
}
// -- Do store
this->m_fields[fieldId] = edge;
Object* target = NULL;
if (edge) {
target = edge->getTarget();
// -- Increment new ref
if (target) {
target->incrementRefCountReal();
// TODO: An increment of the refcount means this isn't a candidate root
// for a garbage cycle.
}
}
if (HeapState::debug) {
cout << "Update "
<< m_id << "." << fieldId
<< " --> " << (target ? target->m_id : 0)
<< " (" << (target ? target->getRefCount() : 0) << ")"
<< endl;
}
}
void Object::mark_red()
{
if ( (this->m_color == GREEN) || (this->m_color == BLACK) ) {
// Only recolor if object is GREEN or BLACK.
// Ignore if already RED or BLUE.
this->recolor( RED );
for ( EdgeMap::iterator p = this->m_fields.begin();
p != this->m_fields.end();
p++ ) {
Edge* edge = p->second;
if (edge) {
Object* target = edge->getTarget();
target->mark_red();
}
}
}
}
void Object::scan()
{
if (this->m_color == RED) {
if (this->m_refCount > 0) {
this->scan_green();
} else {
this->recolor( BLUE );
// -- Visit all edges
for ( EdgeMap::iterator p = this->m_fields.begin();
p != this->m_fields.end();
p++ ) {
Edge* target_edge = p->second;
if (target_edge) {
Object* next_target_object = target_edge->getTarget();
if (next_target_object) {
next_target_object->scan();
}
}
}
}
}
}
void Object::scan_green()
{
this->recolor( GREEN );
for ( EdgeMap::iterator p = this->m_fields.begin();
p != this->m_fields.end();
p++ ) {
Edge* target_edge = p->second;
if (target_edge) {
Object* next_target_object = target_edge->getTarget();
if (next_target_object) {
if (next_target_object->getColor() != GREEN) {
next_target_object->scan_green();
}
}
}
}
}
deque<int> Object::collect_blue( EdgeList& edgelist )
{
deque<int> result;
if (this->getColor() == BLUE) {
this->recolor( GREEN );
result.push_back( this->getId() );
for ( EdgeMap::iterator p = this->m_fields.begin();
p != this->m_fields.end();
p++ ) {
Edge* target_edge = p->second;
if (target_edge) {
Object* next_target_object = target_edge->getTarget();
if (next_target_object) {
deque<int> new_result = next_target_object->collect_blue(edgelist);
if (new_result.size() > 0) {
for_each( new_result.begin(),
new_result.end(),
[&result] (int& n) { result.push_back(n); } );
}
pair<int,int> newedge(this->getId(), next_target_object->getId());
edgelist.push_back( newedge );
// NOTE: this may add an edge that isn't in the cyclic garbage.
// These invalid edges will be filtered out later when
// we know for sure what the cyclic component is.
}
}
}
}
return result;
}
void Object::makeDead( unsigned int death_time,
unsigned int death_time_alloc )
{
// -- Record the death time
this->m_deathTime = death_time;
this->m_deathTime_alloc = death_time_alloc;
if (this->m_deadFlag) {
cerr << "Object[ " << this->getId() << " ] : double Death event." << endl;
} else {
this->m_deadFlag = true;
}
// -- Visit all edges
for ( EdgeMap::iterator p = this->m_fields.begin();
p != this->m_fields.end();
p++ ) {
Edge* edge = p->second;
if (edge) {
// -- Edge dies now
edge->setEndTime(death_time);
}
}
if (HeapState::debug) {
cout << "Dead object " << m_id << " of type " << m_type << endl;
}
}
void Object::recolor( Color newColor )
{
// Maintain the invariant that the reference count of a node is
// the number of GREEN or BLACK pointers to it.
for ( EdgeMap::iterator p = this->m_fields.begin();
p != this->m_fields.end();
p++ ) {
Edge* edge = p->second;
if (edge) {
Object* target = edge->getTarget();
if (target) {
if ( ((this->m_color == GREEN) || (this->m_color == BLACK)) &&
((newColor != GREEN) && (newColor != BLACK)) ) {
// decrement reference count of target
target->decrementRefCount();
} else if ( ((this->m_color != GREEN) && (this->m_color != BLACK)) &&
((newColor == GREEN) || (newColor == BLACK)) ) {
// increment reference count of target
target->incrementRefCountReal();
}
}
}
}
this->m_color = newColor;
}
void Object::decrementRefCountReal( unsigned int cur_time,
Method *method,
Reason reason,
Object *death_root )
{
this->decrementRefCount();
this->m_lastMethodDecRC = method;
if (this->m_refCount == 0) {
ObjectPtrMap_t& whereis = this->m_heapptr->get_whereis();
KeySet_t& keyset = this->m_heapptr->get_keyset();
// TODO Should we even bother with this check?
// Maybe just set it to true.
if (!m_decToZero) {
m_decToZero = true;
m_methodRCtoZero = method;
this->g_counter++;
}
if (reason == STACK) {
this->setDiedByStackFlag();
} else {
this->setDiedByHeapFlag();
}
// -- Visit all edges
this->recolor(GREEN);
// -- Who's my key object?
if (death_root != NULL) {
this->setDeathRoot( death_root );
} else {
this->setDeathRoot( this );
}
Object *my_death_root = this->getDeathRoot();
if (!my_death_root) {
my_death_root = this;
}
whereis[this] = my_death_root;
KeySet_t::iterator itset = keyset.find(my_death_root);
if (itset == keyset.end()) {
keyset[my_death_root] = new std::set< Object * >();
keyset[my_death_root]->insert( my_death_root );
}
keyset[my_death_root]->insert( this );
// Edges are now dead.
for ( EdgeMap::iterator p = this->m_fields.begin();
p != this->m_fields.end();
++p ) {
Edge* target_edge = p->second;
if (target_edge) {
unsigned int fieldId = target_edge->getSourceField();
this->updateField( NULL,
fieldId,
cur_time,
method,
reason,
this->getDeathRoot() );
}
}
// DEBUG
// if (Object::g_counter % 1000 == 1) {
// cout << ".";
// }
}
}
void Object::incrementRefCountReal()
{
if ((this->m_refCount == 0) && this->m_decToZero) {
this->m_incFromZero = true;
this->m_methodRCtoZero = NULL;
}
this->incrementRefCount();
this->m_maxRefCount = std::max( m_refCount, m_maxRefCount );
}