-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulator-GC.cpp
More file actions
403 lines (357 loc) · 14.3 KB
/
simulator-GC.cpp
File metadata and controls
403 lines (357 loc) · 14.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
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <deque>
#include <string>
#include <utility>
using namespace std;
#include "tokenizer.h"
#include "classinfo.h"
#include "execution.h"
#include "heap.hpp"
#include "refstate.h"
#include "summary.hpp"
#include "version.hpp"
#include "memorymgr.h"
// ----------------------------------------------------------------------
// 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;
// ----------------------------------------------------------------------
// 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;
// ----------------------------------------------------------------------
// Analysis
set<unsigned int> root_set;
// ----------------------------------------------------------------------
// Read and process trace events
unsigned int read_trace_file(FILE* f)
{
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;
// -- Allocation time
unsigned int AllocationTime = 0;
while ( !tokenizer.isDone() ) {
tokenizer.getLine();
if (tokenizer.isDone()) {
break;
}
if (Exec.NowUp() % 10000000 == 1) {
cout << " Method time: " << Exec.NowUp() << " Alloc time: " << AllocationTime << endl;
}
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)];
obj = Heap.allocate( tokenizer.getInt(1),
tokenizer.getInt(2),
tokenizer.getChar(0),
tokenizer.getString(3),
as,
els,
thread,
Exec.NowUp() );
unsigned int old_alloc_time = AllocationTime;
AllocationTime += obj->getSize();
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);
if ((objId > 0) && (tgtId > 0)) {
Heap.make_edge2( objId, tgtId );
}
if ((objId > 0) && (oldTgtId > 0)) {
Heap.remove_edge2( objId, oldTgtId );
}
// TODO ============================================================ TODO
// TODO unsigned int threadId = tokenizer.getInt(5);
// TODO: Not sure if we need this code
// TODO Thread *thread = Exec.getThread(threadId);
// TODO Object *oldObj = Heap.getObject(oldTgtId);
// TODO obj = Heap.getObject(objId);
// TODO target = ((tgtId > 0) ? Heap.getObject(tgtId) : NULL);
// TODO if (obj && target) {
// TODO unsigned int field_id = tokenizer.getInt(4);
// TODO Edge* new_edge = Heap.make_edge( obj, field_id,
// TODO target, Exec.NowUp() );
// TODO if (thread) {
// TODO Method *topMethod = thread->TopMethod();
// TODO if (topMethod) {
// TODO topMethod->getName();
// TODO }
// TODO obj->updateField( new_edge,
// TODO field_id,
// TODO Exec.NowUp(),
// TODO topMethod, // for death site info
// TODO HEAP, // reason
// TODO NULL ); // death root 0 because may not be a root
// TODO // NOTE: topMethod COULD be NULL here.
// TODO }
// DEBUG ONLY IF NEEDED
// Example:
// if ( (objId == tgtId) && (objId == 166454) ) {
// if ( (objId == 166454) ) {
// tokenizer.debugCurrent();
// }
// TODO }
// TODO ============================================================ TODO
}
break;
case 'D':
{
// D <object> <thread-id>
// 0 1
unsigned int objId = tokenizer.getInt(1);
obj = Heap.getObject(objId);
if (obj) {
unsigned int threadId = tokenizer.getInt(2);
Thread *thread = Exec.getThread(threadId);
Heap.makeDead(obj, Exec.NowUp());
} else {
assert(false);
}
}
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);
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());
Thread *thread = Exec.getThread(threadId);
if (thread) {
thread->objectRoot(object);
}
}
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;
}
void debug_GC_history( deque< GCRecord_t > &GC_history )
{
for ( deque< GCRecord_t >::iterator iter = GC_history.begin();
iter != GC_history.end();
++iter ) {
cout << "[" << iter->first << "] - " << iter->second << " bytes" << endl;
}
}
// ----------------------------------------------------------------------
int main(int argc, char* argv[])
{
if (argc != 6) {
cout << "Usage: " << argv[0] << " <namesfile> <dgroups-csv-file> <output base name> <memsize> <BASIC/DEF>" << endl;
cout << " git version: " << build_git_sha << endl;
cout << " build date : " << build_git_time << endl;
exit(1);
}
string dgroups_csvfile(argv[2]);
string basename(argv[3]);
string summary_filename( basename + "-SUMMARY.csv" );
int memsize = std::stoi(argv[4]);
cout << "Memory size: " << memsize << " bytes." << endl;
string gctype(argv[5]);
if (gctype != "BASIC" && gctype != "DEF") {
cout << "Invalid GC type: " << gctype << endl
<< "Exiting." << endl;
exit(2);
}
if (gctype == "BASIC") {
Heap.initialize_memory_basic( memsize );
} else if (gctype == "DEF") {
Heap.initialize_memory_deferred( memsize,
dgroups_csvfile,
1 ); // Number of groups to use
} else {
assert(false);
}
// Hard coded number at this point. TODO
cout << "Read names file..." << endl;
ClassInfo::read_names_file_no_mainfunc( argv[1] );
cout << "Start trace..." << endl;
FILE* f = fdopen(0, "r");
unsigned int total_objects = read_trace_file(f);
unsigned int final_time = Exec.NowUp();
cout << "Done at time " << Exec.NowUp() << endl
<< "Total objects: " << total_objects << endl
<< "Heap.size: " << Heap.size() << endl
<< "Number of collections: " << Heap.get_number_of_collections() << endl;
if (gctype == "BASIC") {
cout << "Mark total : " << Heap.get_mark_total() << endl
<< "- total alloc: " << Heap.get_total_alloc() << endl;
} else if (gctype == "DEF") {
cout << "Mark total : " << Heap.get_mark_total() << endl
<< "- mark saved : " << Heap.get_mark_saved() << endl
<< "- total alloc: " << Heap.get_total_alloc() << endl;
}
// TODO << " - number of edges removed: " << Heap.get_number_edges_removed() << endl
// TODO << " - number of edges removal attempts: " << Heap.get_number_attempts_edges_removed() << endl
// TODO << " - number of region edges: " << Heap.get_region_edges_count() << endl
// TODO << " - number of in edges: " << Heap.get_in_edges_count() << endl
// TODO << " - number of out edges: " << Heap.get_out_edges_count() << endl
// TODO << " - number of nonregion edges: " << Heap.get_nonregion_edges_count() << endl;
}
//--------------------------------------------------------------------------------
// COMMENTED OUT CODE
//--------------------------------------------------------------------------------
/*
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();
}
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;
}
*/