-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator-ref.cpp
More file actions
1518 lines (1435 loc) · 65.7 KB
/
simulator-ref.cpp
File metadata and controls
1518 lines (1435 loc) · 65.7 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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <deque>
#include <string>
#include <utility>
// TODO #include <stxxl/map>
using namespace std;
#include "tokenizer.h"
#include "classinfo.h"
#include "execution.h"
#include "heap.h"
#include "refstate.h"
#include "summary.hpp"
// #include "lastmap.h"
#include "version.hpp"
// // STXXL related constants
// #define DATA_NODE_BLOCK_SIZE (4096)
// #define DATA_LEAF_BLOCK_SIZE (4096)
// ----------------------------------------------------------------------
// Types
class Object;
class CCNode;
typedef std::map< string, std::vector< Summary * > > GroupSum_t;
typedef std::map< string, Summary * > TypeTotalSum_t;
typedef std::map< unsigned int, Summary * > SizeSum_t;
// An Edge Source is an object and it's specific field.
// Global edge sources have a NULL object pointer.
typedef std::pair< Object *, FieldId_t > EdgeSrc_t;
// ----------------------------------------------------------------------
// We then map Edge Sources to a vector/list of Object pointers.
// For every edge source, we keep a list of all the objects (pointers)
// it has ever pointed to.
// This is the in-memory stl version
typedef std::map< EdgeSrc_t, std::vector< Object * > > EdgeSummary_t;
// We need a reverse map from object pointer to the edge sources that ever
// pointed to that object.
typedef std::map< Object *, std::vector< EdgeSrc_t > > Object2EdgeSrcMap_t;
// Trying an on-disk version but the library I DLed doesn't work well with C++11
// TODO //! [comparator]
// TODO struct CompareGreater_EdgeSummary
// TODO {
// TODO bool operator () (const EdgeSrc_t &a, const EdgeSrc_t &b) const {
// TODO Object *first = std::get<0>(a);
// TODO Object *second = std::get<0>(b);
// TODO if (first != second) {
// TODO return first > second;
// TODO } else {
// TODO FieldId_t field1 = std::get<1>(a);
// TODO FieldId_t field2 = std::get<1>(b);
// TODO return field1 > field2;
// TODO }
// TODO }
// TODO static EdgeSrc_t max_value() {
// TODO return std::make_pair( static_cast<Object *>(NULL),
// TODO std::numeric_limits<unsigned int>::min() );
// TODO }
// TODO };
// TODO //! [comparator]
// TODO // This is the out-of-core (on-disk) version.
// TODO typedef stxxl::map< EdgeSrc_t, std::vector< Object * >,
// TODO CompareGreater_EdgeSummary,
// TODO DATA_NODE_BLOCK_SIZE, DATA_LEAF_BLOCK_SIZE >
// TODO stxxl_EdgeSummary_t;
// TODO // TODO: This is just dummy code to test the STXXL compilation units.
// TODO // Need to replace EdgeSummary_t with the stxxl::map. (Or maybe make it configurable)
// TODO // TODO: Test
// TODO typedef stxxl_EdgeSummary_t EdgeSummary_t;
// TODO
// TODO //! [comparator]
// TODO struct CompareGreater
// TODO {
// TODO bool operator () (const int & a, const int & b) const {
// TODO return a > b;
// TODO }
// TODO static int max_value() {
// TODO return std::numeric_limits<int>::min();
// TODO }
// TODO };
// TODO //! [comparator]
// TODO // This is the out-of-core (on-disk) version.
// TODO //! [comparator]
// TODO typedef stxxl::map<int, char, CompareGreater, DATA_NODE_BLOCK_SIZE, DATA_LEAF_BLOCK_SIZE> stxxl_Object2EdgeSrcMap_t;
// EdgeSummary_t and Object2EdgeSrcMap_t contain the raw data. We need some
// summaries.
// enum class ObjectRefType moved to heap.h - RLV
enum class EdgeSrcType {
STABLE = 1, // Like True Love. Only one target ever.
// TODO: This is an original idea that maybe singly-owned objects
// can be considered part of the stable group.
SERIAL_STABLE = 2, // Kind of like a serial monogamist.
// Can point to different objects but never shares the
// target object
UNSTABLE = 32, // Points to different objects and
// shares objects with other references
UNKNOWN = 1024
};
string edgesrctype2str( EdgeSrcType r)
{
if (r == EdgeSrcType::STABLE) {
return "STABLE";
} else if (r == EdgeSrcType::SERIAL_STABLE) {
return "SERIAL_STABLE";
} else if (r == EdgeSrcType::UNSTABLE) {
return "UNSTABLE";
} else {
return "UNKNOWN";
}
}
string edgesrctype2shortstr( EdgeSrcType r)
{
if (r == EdgeSrcType::STABLE) {
return "S"; // STABLE
} else if (r == EdgeSrcType::SERIAL_STABLE) {
return "ST"; // SERIAL_STABLE
} else if (r == EdgeSrcType::UNSTABLE) {
return "U"; // UNSTABLE
} else {
return "X"; // UNKNOWN
}
}
// Map a reference to its type
typedef std::map< EdgeSrc_t, EdgeSrcType > EdgeSrc2Type_t;
// Map an object to its type
// TODO: Is this needed?
typedef std::map< Object *, ObjectRefType > Object2Type_t;
// ----------------------------------------------------------------------
// Globals
// -- Key object map to set of objects
KeySet_t keyset;
// -- Object to key object map
ObjectPtrMap_t whereis;
// -- The heap
HeapState Heap( whereis, keyset );
// -- Execution state
#ifdef ENABLE_TYPE1
ExecMode cckind = ExecMode::Full; // Full calling context
#else
ExecMode cckind = ExecMode::StackOnly; // Stack-only context
#endif // ENABLE_TYPE1
ExecState Exec(cckind);
// -- Turn on debugging
bool debug = false;
// -- Remember the last event by thread ID
// TODO: Types for object and thread IDs?
// TODO LastMap last_map;
// ----------------------------------------------------------------------
// Analysis
deque< deque<Object*> > cycle_list;
set<unsigned int> root_set;
map<unsigned int, unsigned int> deathrc_map;
map<unsigned int, bool> not_candidate_map;
// TODO EdgeSummary_t edge_summary;
Object2EdgeSrcMap_t obj2ref_map;
// ----------------------------------------------------------------------
void sanity_check()
{
/*
if (Now > obj->getDeathTime() && obj->getRefCount() != 0) {
nonzero_ref++;
printf(" Non-zero-ref-dead %X of type %s\n", obj->getId(), obj->getType().c_str());
}
*/
}
bool member( Object* obj, const ObjectSet& theset )
{
return theset.find(obj) != theset.end();
}
unsigned int closure( ObjectSet& roots,
ObjectSet& premarked,
ObjectSet& result )
{
unsigned int mark_work = 0;
vector<Object*> worklist;
// -- Initialize the worklist with only unmarked roots
for ( ObjectSet::iterator i = roots.begin();
i != roots.end();
i++ ) {
Object* root = *i;
if ( !member(root, premarked) ) {
worklist.push_back(root);
}
}
// -- Do DFS until the worklist is empty
while (worklist.size() > 0) {
Object* obj = worklist.back();
worklist.pop_back();
result.insert(obj);
mark_work++;
const EdgeMap& fields = obj->getFields();
for ( EdgeMap::const_iterator p = fields.begin();
p != fields.end();
p++ ) {
Edge* edge = p->second;
Object* target = edge->getTarget();
if (target) {
// if (target->isLive(Exec.NowUp())) {
if ( !member(target, premarked) &&
!member(target, result) ) {
worklist.push_back(target);
}
// } else {
// cout << "WEIRD: Found a dead object " << target->info() << " from " << obj->info() << endl;
// }
}
}
}
return mark_work;
}
unsigned int count_live( ObjectSet & objects, unsigned int at_time )
{
int count = 0;
// -- How many are actually live
for ( ObjectSet::iterator p = objects.begin();
p != objects.end();
p++ ) {
Object* obj = *p;
if (obj->isLive(at_time)) {
count++;
}
}
return count;
}
// void update_reference_summaries( Object *src,
// FieldId_t fieldId,
// Object *tgt )
// {
// // edge_summary : EdgeSummary_t is a global
// // obj2ref_map : Object2EdgeSrcMap_t is a global
// EdgeSrc_t ref = std::make_pair( src, fieldId );
// // The reference 'ref' points to the new target
// auto iter = edge_summary.find(ref);
// if (iter == edge_summary.end()) {
// // Not found. Create a new vector of Object pointers
// edge_summary[ref] = std::vector< Object * >();
// }
// edge_summary[ref].push_back(tgt);
// // Do the reverse mapping ONLY if tgt is not NULL
// if (tgt) {
// auto rev = obj2ref_map.find(tgt);
// if (rev == obj2ref_map.end()) {
// obj2ref_map[tgt] = std::move(std::vector< EdgeSrc_t >());
// }
// obj2ref_map[tgt].push_back(ref);
// }
// }
// ----------------------------------------------------------------------
// Read and process trace events
void apply_merlin( std::deque< Object * > &new_garbage )
{
if (new_garbage.size() == 0) {
// TODO Log an ERROR or a WARNING
return;
}
// TODO: Use setDeathTime( new_dtime );
// Sort the new_garbage vector according to latest last_timestamp.
std::sort( new_garbage.begin(),
new_garbage.end(),
[]( Object *left, Object *right ) {
return (left->getLastTimestamp() > right->getLastTimestamp());
} );
// Do a standard DFS.
while (new_garbage.size() > 0) {
// Start with the latest for the DFS.
Object *cur = new_garbage[0];
new_garbage.pop_front();
std::deque< Object * > mystack; // stack for DFS
std::set< Object * > labeled; // What Objects have been labeled
mystack.push_front( cur );
while (mystack.size() > 0) {
Object *otmp = mystack[0];
mystack.pop_front();
assert(otmp);
// The current timestamp max
unsigned int tstamp_max = otmp->getLastTimestamp();
otmp->setDeathTime( tstamp_max );
// Remove from new_garbage deque
auto ngiter = std::find( new_garbage.begin(),
new_garbage.end(),
otmp );
if (ngiter != new_garbage.end()) {
// still in the new_garbage deque
new_garbage.erase(ngiter);
}
// Check if labeled already
auto iter = labeled.find(otmp);
if (iter == labeled.end()) {
// Not found => not labeled
labeled.insert(otmp);
// Go through all the edges out of otmp
// -- Visit all edges
for ( auto ptr = otmp->getFields().begin();
ptr != otmp->getFields().end();
ptr++ ) {
Edge *edge = ptr->second;
if ( edge &&
((edge->getEdgeState() == EdgeState::DEAD_BY_OBJECT_DEATH) ||
(edge->getEdgeState() == EdgeState::DEAD_BY_OBJECT_DEATH_NOT_SAVED)) ) {
edge->setEndTime( tstamp_max );
Object *mytgt = edge->getTarget();
if (mytgt) {
// Get timestamp
unsigned int tstmp = mytgt->getLastTimestamp();
if (tstamp_max > tstmp) {
mytgt->setLastTimestamp( tstamp_max );
mytgt->setDeathTime( tstamp_max );
}
// This might be a bug. I probably shouldn't be setting the tstamp_max here.
// But then again where should it be set? It seems that this is the right thing
// to do here.
// TODO else {
// TODO tstamp_max = tstmp;
// TODO }
mystack.push_front( mytgt );
}
}
}
}
}
}
// Until the vector is empty
}
// ----------------------------------------------------------------------
// Read and process trace events
unsigned int read_trace_file( FILE *f )
// MAYBE: ofstream &eifile )
{
Tokenizer tokenizer(f);
unsigned int method_id;
unsigned int object_id;
unsigned int target_id;
unsigned int field_id;
unsigned int thread_id;
unsigned int exception_id;
Object *obj;
Object *target;
Method *method;
unsigned int total_objects = 0;
// Remember all the dead objects
std::deque< Object * > new_garbage;
Method *main_method = ClassInfo::get_main_method();
unsigned int main_id = main_method->getId();
unsigned int latest_death_time = 0;
// -- Allocation time
unsigned int AllocationTime = 0;
while ( ! tokenizer.isDone()) {
tokenizer.getLine();
if (tokenizer.isDone()) {
break;
}
switch (tokenizer.getChar(0)) {
case 'A':
case 'I':
case 'N':
case 'P':
case 'V':
{
// A/I/N/P/V <id> <size> <type> <site> [<els>] <threadid>
// 0 1 2 3 4 5 5/6
unsigned int thrdid = (tokenizer.numTokens() == 6) ? tokenizer.getInt(6)
: tokenizer.getInt(5);
Thread* thread = Exec.getThread(thrdid);
unsigned int els = (tokenizer.numTokens() == 6) ? tokenizer.getInt(5)
: 0;
AllocSite *as = ClassInfo::TheAllocSites[tokenizer.getInt(4)];
string njlib_sitename;
if (thread) {
MethodDeque javalib_context = thread->top_javalib_methods();
assert(javalib_context.size() > 0);
Method *meth = javalib_context.back();
njlib_sitename = ( meth ? meth->getName() : "NONAME" );
// TODO: if (cckind == ExecMode::Full) {
// TODO: // Get full stacktrace
// TODO: DequeId_t strace = thread->stacktrace_using_id();
// TODO: }
} else {
assert(false);
} // if (thread) ... else
// DEBUG
// if (!as) {
// cerr << "DBG: objId[ " << tokenizer.getInt(1) << " ] has no alloc site." << endl;
// } // END DEBUG
obj = Heap.allocate( tokenizer.getInt(1), // id
tokenizer.getInt(2), // size
tokenizer.getChar(0), // kind of alloc
tokenizer.getString(3), // type
as, // AllocSite pointer
njlib_sitename, // NonJava-library alloc sitename
els, // length IF applicable
thread, // thread Id
Exec.NowUp() ); // Current time
AllocationTime = Heap.getAllocTime();
Exec.SetAllocTime( AllocationTime );
total_objects++;
}
break;
case 'U':
{
// U <old-target> <object> <new-target> <field> <thread>
// 0 1 2 3 4 5
// -- Look up objects and perform update
unsigned int objId = tokenizer.getInt(2);
unsigned int tgtId = tokenizer.getInt(3);
unsigned int oldTgtId = tokenizer.getInt(1);
unsigned int threadId = tokenizer.getInt(5);
unsigned int field = tokenizer.getInt(4);
Thread *thread = Exec.getThread(threadId);
Method *topMethod = NULL;
Method *topMethod_using_action = NULL;
if (thread) {
topMethod = thread->TopMethod();
MethodDeque tjmeth = thread->top_javalib_methods();
topMethod_using_action = tjmeth.back();
}
Object *oldObj = Heap.getObject(oldTgtId);
Exec.IncUpdateTime();
obj = Heap.getObject(objId);
// NOTE that we don't need to check for non-NULL source object 'obj'
// here. NULL means that it's a global/static reference.
target = ((tgtId > 0) ? Heap.getObject(tgtId) : NULL);
// TODO DELETE maybe: if (obj) {
// TODO DELETE maybe: update_reference_summaries( obj, field, target );
// TODO DELETE maybe: }
// TODO last_map.setLast( threadId, LastEvent::UPDATE, obj );
// Set lastEvent and heap/stack flags for new target
if (target) {
if ( obj &&
obj != target
) {
target->setPointedAtByHeap();
}
target->setLastTimestamp( Exec.NowUp() );
target->unsetLastUpdateFromStatic();
}
if (oldObj) {
// Set the last time stamp for Merlin Algorithm purposes
oldObj->setLastTimestamp( Exec.NowUp() );
oldObj->setActualLastTimestamp( Exec.NowUp() );
// Last action site
oldObj->setLastActionSite(topMethod_using_action);
string last_action_name = topMethod_using_action->getName();
oldObj->set_nonJavaLib_last_action_context( last_action_name );
}
if (oldTgtId != tgtId) {
if (obj) {
Edge *new_edge = NULL;
// Can't call updateField if obj is NULL
if (target) {
// Increment and decrement refcounts
unsigned int field_id = tokenizer.getInt(4);
// TODO TODO This is probably where the refcount should be
// TODO TODO TODO TODO
new_edge = Heap.make_edge( obj,
field_id,
target,
Exec.NowUp() );
}
obj->updateField_nosave( new_edge,
field_id,
Exec.NowUp(),
topMethod, // for death site info
Reason::HEAP, // reason
NULL, // death root 0 because may not be a root
lastevent, // last event to determine cause
EdgeState::DEAD_BY_UPDATE ); // edge is dead because of update
//
// NOTE: topMethod COULD be NULL here.
// DEBUG ONLY IF NEEDED
// Example:
// if ( (objId == tgtId) && (objId == 166454) ) {
// if ( (objId == 166454) ) {
// tokenizer.debugCurrent();
// }
}
}
}
break;
case 'D':
{
// D <object> <thread-id>
// 0 1 2
unsigned int objId = tokenizer.getInt(1);
obj = Heap.getObject(objId);
if (obj) {
unsigned int now_uptime = Exec.NowUp();
// Merlin algorithm portion. TODO: This is getting unwieldy. Think about
// refactoring.
// Keep track of the latest death time
// TODO: Debug? Well it's a decent sanity check so we may leave it in.
assert( latest_death_time <= now_uptime );
// Ok, so now we can see if the death time has
if (now_uptime > latest_death_time) {
// Do the Merlin algorithm
apply_merlin( new_garbage );
// TODO: What are the parameters?
// - new_garbage for sure
// - anything else?
// For now this simply updates the object death times
}
// Update latest death time
latest_death_time = now_uptime;
// The rest of the bookkeeping
new_garbage.push_back(obj);
unsigned int threadId = tokenizer.getInt(2);
LastEvent lastevent = obj->getLastEvent();
// Set the died by flags
if ( (lastevent == LastEvent::UPDATE_AWAY_TO_NULL) ||
(lastevent == LastEvent::UPDATE_AWAY_TO_VALID) ||
(lastevent == LastEvent::UPDATE_UNKNOWN) ) {
if (obj->wasLastUpdateFromStatic()) {
obj->setDiedByGlobalFlag();
}
// Design decision: all died by globals are
// also died by heap.
obj->setDiedByHeapFlag();
} else if ( (lastevent == LastEvent::ROOT) ||
(lastevent == LastEvent::OBJECT_DEATH_AFTER_ROOT_DECRC) ||
(lastevent == LastEvent::OBJECT_DEATH_AFTER_UPDATE_DECRC) ) {
obj->setDiedByStackFlag();
}
//else {
// cerr << "Unhandled event: " << lastevent2str(lastevent) << endl;
// }
Heap.makeDead_nosave( obj,
now_uptime );
// Get the current method
Method *topMethod = NULL;
MethodDeque top_2_methods;
MethodDeque javalib_context;
// TODO: ContextPair cpair;
CPairType cptype;
Thread *thread;
if (threadId > 0) {
thread = Exec.getThread(threadId);
// Update counters in ExecState for map of
// Object * to simple context pair
} else {
// No thread info. Get from ExecState
thread = Exec.get_last_thread();
}
if (thread) {
// TODO: cptype = thread->getContextPairType();
unsigned int count = 0;
topMethod = thread->TopMethod();
top_2_methods = thread->top_N_methods(2);
// TODO TODO HERE TODO
// Exec.IncDeathContext( top_2_methods );
// TODO TODO HERE TODO
javalib_context = thread->top_javalib_methods();
if (cckind == ExecMode::Full) {
// Get full stacktrace
DequeId_t strace = thread->stacktrace_using_id();
obj->setDeathContextList( strace );
}
// Set the death site
Exec.UpdateObj2DeathContext( obj,
javalib_context );
#ifdef DEBUG_SPECJBB
// if the object is of type [I
// SPECJBB if (obj->getType() == "[I") {
if (obj->getType() == "Lspec/benchmarks/_205_raytrace/Point;") {
MethodDeque cstack = thread->full_method_stack();
cout << "---------------------------------------------------------------------" << endl;
cout << "DEBUG: objectId[ " << obj->getId() << " ]:" << endl
<< " ";
for ( auto iter = cstack.begin();
iter != cstack.end();
iter++ ) {
cout << (*iter)->getName() << " <- ";
}
cout << endl;
}
//
#endif // DEBUG_SPECJBB
// Death reason setting
Reason myreason;
if (thread->isLocalVariable(obj)) {
myreason = Reason::STACK;
} else {
myreason = Reason::HEAP;
}
// Recursively make the edges dead and assign the proper death cause
for ( EdgeMap::iterator p = obj->getEdgeMapBegin();
p != obj->getEdgeMapEnd();
++p ) {
Edge* target_edge = p->second;
if (target_edge) {
unsigned int fieldId = target_edge->getSourceField();
obj->updateField_nosave( NULL,
fieldId,
Exec.NowUp(),
topMethod,
myreason,
obj,
lastevent,
EdgeState::DEAD_BY_OBJECT_DEATH_NOT_SAVED );
// NOTE: STACK is used because the object that died,
// died by STACK.
}
}
} // if (thread)
unsigned int rc = obj->getRefCount();
deathrc_map[objId] = rc;
not_candidate_map[objId] = (rc == 0);
} else {
assert(false);
} // if (obj) ... else
}
break;
case 'M':
{
// M <methodid> <receiver> <threadid>
// 0 1 2 3
// current_cc = current_cc->DemandCallee(method_id, object_id, thread_id);
// TEMP TODO ignore method events
method_id = tokenizer.getInt(1);
method = ClassInfo::TheMethods[method_id];
thread_id = tokenizer.getInt(3);
// Check to see if this is the main function
if (method == main_method) {
Exec.set_main_func_uptime( Exec.NowUp() );
Exec.set_main_func_alloctime( Exec.NowAlloc() );
}
Exec.Call(method, thread_id);
}
break;
case 'E':
case 'X':
{
// E <methodid> <receiver> [<exceptionobj>] <threadid>
// 0 1 2 3 3/4
method_id = tokenizer.getInt(1);
method = ClassInfo::TheMethods[method_id];
thread_id = (tokenizer.numTokens() == 4) ? tokenizer.getInt(3)
: tokenizer.getInt(4);
Exec.Return(method, thread_id);
}
break;
case 'T':
// T <methodid> <receiver> <exceptionobj>
// 0 1 2 3
break;
case 'H':
// H <methodid> <receiver> <exceptionobj>
break;
case 'R':
// R <objid> <threadid>
// 0 1 2
{
unsigned int objId = tokenizer.getInt(1);
Object *object = Heap.getObject(objId);
unsigned int threadId = tokenizer.getInt(2);
// cout << "objId: " << objId << " threadId: " << threadId << endl;
if (object) {
object->setRootFlag(Exec.NowUp());
object->setLastEvent( LastEvent::ROOT );
object->setLastTimestamp( Exec.NowUp() );
object->setActualLastTimestamp( Exec.NowUp() );
Thread *thread = Exec.getThread(threadId);
Method *topMethod_using_action = NULL;
if (thread) {
MethodDeque tjmeth = thread->top_javalib_methods();
topMethod_using_action = tjmeth.back();
}
if (thread) {
thread->objectRoot(object);
}
// Last action site
object->setLastActionSite(topMethod_using_action);
string last_action_name = topMethod_using_action->getName();
object->set_nonJavaLib_last_action_context( last_action_name );
}
root_set.insert(objId);
// TODO last_map.setLast( threadId, LastEvent::ROOT, object );
}
break;
default:
// cout << "ERROR: Unknown entry " << tokenizer.curChar() << endl;
break;
}
}
return total_objects;
}
// ----------------------------------------------------------------------
// Remove edges not in cyclic garbage
void filter_edgelist( deque< pair<int,int> >& edgelist, deque< deque<int> >& cycle_list )
{
set<int> nodes;
deque< pair<int,int> > newlist;
for ( auto it = cycle_list.begin();
it != cycle_list.end();
++it ) {
for ( auto tmp = it->begin();
tmp != it->end();
++tmp ) {
nodes.insert(*tmp);
}
}
for ( auto it = edgelist.begin();
it != edgelist.end();
++it ) {
auto first_it = nodes.find(it->first);
if (first_it == nodes.end()) {
// First node not found, carry on.
continue;
}
auto second_it = nodes.find(it->second);
if (second_it != nodes.end()) {
// Found both edge nodes in cycle set 'nodes'
// Add the edge.
newlist.push_back(*it);
}
}
edgelist = newlist;
}
unsigned int sumSize( std::set< Object * >& s )
{
unsigned int total = 0;
for ( auto it = s.begin();
it != s.end();
++it ) {
total += (*it)->getSize();
}
return total;
}
void update_summaries( Object *key,
std::set< Object * >& tgtSet,
GroupSum_t& pgs,
TypeTotalSum_t& tts,
SizeSum_t& ssum )
{
string mytype = key->getType();
unsigned gsize = tgtSet.size();
// per group summary
auto git = pgs.find(mytype);
if (git == pgs.end()) {
pgs[mytype] = std::move(std::vector< Summary * >());
}
unsigned int total_size = sumSize( tgtSet );
Summary *s = new Summary( gsize, total_size, 1 );
// -- third parameter is number of groups which is simply 1 here.
pgs[mytype].push_back(s);
// type total summary
auto titer = tts.find(mytype);
if (titer == tts.end()) {
Summary *t = new Summary( gsize, total_size, 1 );
tts[mytype] = t;
} else {
tts[mytype]->size += total_size;
tts[mytype]->num_objects += tgtSet.size();
tts[mytype]->num_groups++;
}
// size summary
auto sit = ssum.find(gsize);
if (sit == ssum.end()) {
Summary *u = new Summary( gsize, total_size, 1 );
ssum[gsize] = u;
} else {
ssum[gsize]->size += total_size;
ssum[gsize]->num_groups++;
// Doesn't make sense to make use of the num_objects field.
}
}
void update_summary_from_keyset( KeySet_t &keyset,
GroupSum_t &per_group_summary,
TypeTotalSum_t &type_total_summary,
SizeSum_t &size_summary )
{
for ( auto it = keyset.begin();
it != keyset.end();
++it ) {
Object *key = it->first;
std::set< Object * > *tgtSet = it->second;
// TODO TODO 7 March 2016 - Put into CSV file.
cout << "[ " << key->getType() << " ]: " << tgtSet->size() << endl;
update_summaries( key,
*tgtSet,
per_group_summary,
type_total_summary,
size_summary );
}
}
void summarize_reference_stability( EdgeSrc2Type_t &stability,
EdgeSummary_t &my_refsum,
Object2EdgeSrcMap_t &my_obj2ref )
{
// Check each object to see if it's stable...(see ObjectRefType)
for ( auto it = my_obj2ref.begin();
it != my_obj2ref.end();
++it ) {
// my_obj2ref : Object2EdgeSrcMap_t is a reverse map of sorts.
// For each object in the map, it maps to a vector of references
// that point/refer to that object
// This Object is the target
Object *obj = it->first;
// This is the vector of references
std::vector< EdgeSrc_t > reflist = it->second;
// Convert to a set in order to remove possible duplicates
std::set< EdgeSrc_t > refset( reflist.begin(), reflist.end() );
if (!obj) {
continue;
}
// obj is not NULL.
// if ( (refset.size() <= 1) && (obj->wasLastUpdateNull() != true) ) {
if (refset.size() <= 1) {
obj->setRefTargetType( ObjectRefType::SINGLY_OWNED );
} else {
obj->setRefTargetType( ObjectRefType::MULTI_OWNED );
}
}
// Check each edge source to see if its stable...(see EdgeSrcType)
for ( auto it = my_refsum.begin();
it != my_refsum.end();
++it ) {
// my_refsum : EdgeSummary_t is a map from reference to a vector of
// objects that the reference pointed to. A reference is a pair
// of (Object pointer, fieldId).
// Get the reference and deconstruct into parts.
EdgeSrc_t ref = it->first;
Object *obj = std::get<0>(ref);
FieldId_t fieldId = std::get<1>(ref);
// Get the vector/list of object pointers
std::vector< Object * > objlist = it->second;
// We need to make sure that all elements are not duplicates.
std::set< Object * > objset( objlist.begin(), objlist.end() );
// Is NULL in there?
auto findit = objset.find(NULL);
bool nullflag = (findit != objset.end());
if (nullflag) {
// If NULL is in the list, remove NULL.
objset.erase(findit);
}
unsigned int size = objset.size();
if (size == 1) {
// The reference only points to one object!
// Convert object set back into a vector.
std::vector< Object * > tmplist( objset.begin(), objset.end() );
assert( tmplist.size() == 1 );
Object *tgt = tmplist[0];
// Check for source and target death times
// The checks for NULL are probably NOT necessary.
assert(obj != NULL);
VTime_t src_dtime = obj->getDeathTime();
assert(tgt != NULL);
VTime_t tgt_dtime = tgt->getDeathTime();
if (tgt_dtime != src_dtime) {
// UNSTABLE if target and source deathtimes are different
// NOTE: This used to be UNSTABLE if target died before source.
// We are using a more conservative definition of stability here.
stability[ref] = EdgeSrcType::UNSTABLE;
} else {
stability[ref] = (tgt->wasLastUpdateNull() ? EdgeSrcType::UNSTABLE : EdgeSrcType::STABLE);
}
} else {
// objlist is of length > 1
// This may still be stable if all objects in
// the list are STABLE. Assume they're all stable unless
// otherwise proven.
stability[ref] = EdgeSrcType::SERIAL_STABLE; // aka serial monogamist
for ( auto objit = objset.begin();
objit != objset.end();
++objit ) {
ObjectRefType objtype = (*objit)->getRefTargetType();
if (objtype == ObjectRefType::MULTI_OWNED) {
stability[ref] = EdgeSrcType::UNSTABLE;
break;
} else if (objtype == ObjectRefType::UNKNOWN) {
// Not sure if this is even possible.
// TODO: Make this an assertion?
// Let's assume that UNKNOWNs make it UNSTABLE.
stability[ref] = EdgeSrcType::UNSTABLE;
break;
} // else continue
}
}
}
}
// ---------------------------------------------------------------------------
// ------[ OUTPUT FUNCTIONS ]-------------------------------------------------
// ---------------------------------------------------------------------------
void output_size_summary( string &dgroups_filename,
SizeSum_t &size_summary )
{
ofstream dgroups_file(dgroups_filename);
dgroups_file << "\"num_objects\",\"size_bytes\",\"num_groups\"" << endl;
for ( auto it = size_summary.begin();
it != size_summary.end();
++it ) {
unsigned int gsize = it->first;
Summary *s = it->second;
dgroups_file << s->num_objects << ","
<< s->size << ","
<< s->num_groups << endl;
}
dgroups_file.close();
}
void output_type_summary( string &dgroups_by_type_filename,
TypeTotalSum_t &type_total_summary )
{
ofstream dgroups_by_type_file(dgroups_by_type_filename);
for ( TypeTotalSum_t::iterator it = type_total_summary.begin();
it != type_total_summary.end();
++it ) {
string myType = it->first;
Summary *s = it->second;
dgroups_by_type_file << myType << ","
<< s->size << ","
<< s->num_groups << ","
<< s->num_objects << endl;
}
dgroups_by_type_file.close();
}
// All sorts of hacky debug function. Very brittle.
void debug_type_algo( Object *object,
string& dgroup_kind )
{
KeyType ktype = object->getKeyType();
unsigned int objId = object->getId();
if (ktype == KeyType::UNKNOWN_KEYTYPE) {
cout << "ERROR: objId[ " << objId << " ] : "
<< "Keytype not set but algo determines [ " << dgroup_kind << " ]" << endl;
return;
}
if (dgroup_kind == "CYCLE") {
if (ktype != KeyType::CYCLE) {
goto fail;
}
} else if (dgroup_kind == "CYCKEY") {
if (ktype != KeyType::CYCLEKEY) {
goto fail;
}
} else if (dgroup_kind == "DAG") {
if (ktype != KeyType::DAG) {
goto fail;