-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulator-find-main.cpp
More file actions
314 lines (274 loc) · 10.7 KB
/
simulator-find-main.cpp
File metadata and controls
314 lines (274 loc) · 10.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
#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.h"
#include "refstate.h"
#include "summary.hpp"
// #include "lastmap.h"
#include "version.hpp"
// ----------------------------------------------------------------------
// Types
class Object;
class CCNode;
// TODO typedef std::map< string, std::vector< Summary * > > GroupSum_t;
// ----------------------------------------------------------------------
// Globals
// -- Keyset needed for Heap but isn't really needed.
KeySet_t keyset;
// -- Object to key object map. Also NOT needed.
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
// ----------------------------------------------------------------------
// TODO Delete?
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;
}
// END TODO Delete?
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;
}
// ----------------------------------------------------------------------
// 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;
Method *main_method = ClassInfo::get_main_method();
unsigned int main_id = main_method->getId();
// DEBUG
unsigned int debug_stack_edges = 0;
// END DEBUG
// -- Allocation time
unsigned int AllocationTime = 0;
while ( ! tokenizer.isDone()) {
tokenizer.getLine();
if (tokenizer.isDone()) {
break;
}
// DEBUG only
// if (Exec.NowUp() % 105000 == 100000) {
// // cout << " Method time: " << Exec.Now() << " Alloc time: " << AllocationTime << endl;
// cout << " Update time: " << Exec.NowUp() << " | Method time: TODO | 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)];
string sitename("DONTCARE");
assert(thread);
// We need to keep track of allocation time.
obj = Heap.allocate( tokenizer.getInt(1), // id
tokenizer.getInt(2), // size
tokenizer.getChar(0), // kind of alloc
tokenizer.getString(3), // type
as, // AllocSite pointer
sitename, // Non Java lib allocsite name
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);
// TODO TODO Object *oldObj = Heap.getObject(oldTgtId);
// NOTE: We don't really need the edges here.
Exec.IncUpdateTime();
}
break;
case 'D':
// D <object> <thread-id>
// 0 1 2
// We really don't need anything here.
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];
// check to see if this is THE main method.
if (method == main_method) {
// We're done!
cout << "main_time:" << Exec.NowUp() << endl;
cout << "alloc_time:" << Exec.NowAlloc() << endl;
return Exec.NowUp();
}
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
// We really don't need anything here.
break;
default:
// cout << "ERROR: Unknown entry " << tokenizer.curChar() << endl;
break;
}
}
cout << "DEBUG_STACK_EDGES: " << debug_stack_edges << endl;
return total_objects;
}
// ---------------------------------------------------------------------------
// ------[ OUTPUT FUNCTIONS ]-------------------------------------------------
// ---------------------------------------------------------------------------
// ----------------------------------------------------------------------
int main(int argc, char* argv[])
{
if (argc != 5) {
cout << "Usage: " << argv[0] << " <namesfile> <main.class> <main.function> <output filename>" << endl;
cout << " git version: " << build_git_sha << endl;
cout << " build date : " << build_git_time << endl;
cout << " CC kind : " << Exec.get_kind() << endl;
exit(1);
}
cout << "# git version: " << build_git_sha << endl;
cout << "# build date : " << build_git_time << endl;
cout << "---------------[ START ]-----------------------------------------------------------" << endl;
string basename(argv[4]);
// TODO TODO string summary_filename( basename + "-SUMMARY.csv" );
Exec.set_output( NULL );
string main_class(argv[2]);
string main_function(argv[3]);
// TODO Need the main package name
cout << "Read names file..." << endl;
ClassInfo::read_names_file( argv[1],
main_class,
main_function );
cout << "Start trace..." << endl;
FILE* f = fdopen(0, "r");
unsigned int main_time = read_trace_file(f);
cout << "---------------[ DONE ]------------------------------------------------------------" << endl;
cout << "# git version: " << build_git_sha << endl;
cout << "# build date : " << build_git_time << endl;
}