-
Notifications
You must be signed in to change notification settings - Fork 0
/
memtracer_mt_work_bk.cpp
278 lines (222 loc) · 6.99 KB
/
memtracer_mt_work_bk.cpp
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
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <inttypes.h>
#include <stdlib.h>
#include <stddef.h>
#include <assert.h>
#include "pin.H"
#include "portability.H"
PIN_LOCK lock;
/*
* Thread specific data
*/
TLS_KEY mlog_key;
// function to access thread-specific data
INT32 numThreads = 0;
const INT32 MaxNumThreads = 4;
//static UINT32 icount = 0;
//UINT32 icount_lastmem;
UINT32 sincelast=0;
UINT32 memrefs; // number of memory accesses so far
#define MAX_REFS 200000000 // we want to limit the size of the log files so we will limit trace files to 2M lines
/*
* Name of the output file
*/
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "buffer.out", "output file");
class MLOG
{
public:
MLOG(THREADID tid);
~MLOG();
UINT64 _icount;
UINT64 _icount_lastmem;
UINT64 _total;
// VOID DumpBufferToFile( struct MEMREF * reference, UINT64 numElements, THREADID tid );
// private:
//ofstream _ofile;
FILE* _ofile;
// FILE * trace[2];
};
MLOG::MLOG(THREADID tid)
{
_icount=0;
_icount_lastmem=0;
_total=0;
string filename = "xz -2 -c > " + KnobOutputFile.Value() + "." + decstr(tid);
_ofile = popen(filename.c_str(),"w");
if ( ! _ofile )
{
cerr << "Error: could not open output file." << endl;
exit(1);
}
}
MLOG::~MLOG()
{
// _ofile.close();
}
MLOG* get_tls(THREADID tid)
{
MLOG * mlog = static_cast<MLOG*>( PIN_GetThreadData( mlog_key, tid ) );
return mlog;
}
// This function is called before every instruction is executed
//VOID docount() { icount++; }
VOID docount(THREADID tid) {
MLOG* mlog = get_tls(tid);
mlog->_icount++;
}
//FILE * trace;
// This function is called before every instruction is executed
//VOID docount() { icount++; }
VOID RecordMemRead(VOID * ip, VOID * addr, THREADID tid)
{
MLOG* mlog = get_tls(tid);
int _sincelast = mlog->_icount - mlog->_icount_lastmem - 1;
mlog->_icount_lastmem = mlog->_icount;
//mlog->_total+=mlog->_icount - mlog->_icount_lastmem;
//assert (mlog->_total==mlog->_icount);
fprintf(mlog->_ofile,"l %p %p %u\n", ip, addr, _sincelast);
//sincelast=0;
// fclose(trace);
}
// Print a memory write record
VOID RecordMemWrite(VOID * ip, VOID * addr, THREADID tid)
{
MLOG* mlog = get_tls(tid);
int _sincelast = mlog->_icount - mlog->_icount_lastmem - 1;
mlog->_icount_lastmem = mlog->_icount;
fprintf(mlog->_ofile,"s %p %p %u\n", ip, addr, _sincelast);
//sincelast=0;
//fclose(trace);
}
// update lastmem info and write to file (load)
/*VOID RecordMemRead(VOID * ip, VOID * addr, THREADID tid)
{
MLOG* mlog = get_tls(tid);
if(mlog->_icount == mlog->_icount_lastmem)
{
sincelast = 0;
mlog->_total+=sincelast;
}
else {
sincelast = mlog->_icount - mlog->_icount_lastmem - 1;
mlog->_icount_lastmem = mlog->_icount;
//mlog->_total+=mlog->_icount - mlog->_icount_lastmem;
mlog->_total+=sincelast+1;;
}
// cout << "TID : " << tid << " READ " << mlog->_icount << " " << mlog->_total << endl;
assert (mlog->_total==mlog->_icount);
fprintf(mlog->_ofile,"l %p %p %u\n", ip, addr, sincelast);
sincelast=0;
// fclose(trace);
}
// Print a memory write record
VOID RecordMemWrite(VOID * ip, VOID * addr, THREADID tid)
{
MLOG* mlog = get_tls(tid);
if(mlog->_icount == mlog->_icount_lastmem)
{
sincelast = 0;
mlog->_total+=sincelast;
}
else {
sincelast = mlog->_icount - mlog->_icount_lastmem - 1;
mlog->_icount_lastmem = mlog->_icount;
//mlog->_total+=mlog->_icount - mlog->_icount_lastmem;
mlog->_total+=sincelast+1;;
}
// cout << "TID : " << tid << " WRITE " << mlog->_icount << " " << mlog->_total << endl;
assert (mlog->_total==mlog->_icount);
fprintf(mlog->_ofile,"s %p %p %u\n", ip, addr, sincelast);
sincelast=0;
//fclose(trace);
}
*/
// Is called for every instruction and instruments reads and writes
VOID Instruction(INS ins, VOID *v)
{
// Insert a call to docount before every instruction, no arguments are passed
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount,IARG_THREAD_ID, IARG_END);
// instruments loads using a predicated call, i.e.
// the call happens iff the load will be actually executed
// (this does not matter for ia32 but arm and ipf have predicated instructions)
if (INS_IsMemoryRead(ins))
{
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
IARG_INST_PTR,
IARG_MEMORYREAD_EA,
IARG_THREAD_ID,
IARG_END);
}
// instruments stores using a predicated call, i.e.
// the call happens iff the store will be actually executed
if (INS_IsMemoryWrite(ins))
{
INS_InsertPredicatedCall(
ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
IARG_INST_PTR,
IARG_MEMORYWRITE_EA,
IARG_THREAD_ID,
IARG_END);
}
}
/*
* Note that opening a file in a callback is only supported on Linux systems.
* See buffer-win.cpp for how to work around this issue on Windows.
*/
VOID ThreadStart(THREADID tid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
// There is a new MLOG for every thread. Opens the output file.
MLOG * mlog = new MLOG(tid);
// A thread will need to look up its MLOG, so save pointer in TLS
PIN_SetThreadData(mlog_key, mlog, tid);
}
VOID ThreadFini(THREADID tid, const CONTEXT *ctxt, INT32 code, VOID *v)
{
MLOG * mlog = static_cast<MLOG*>(PIN_GetThreadData(mlog_key, tid));
cout << "Count[" << decstr(tid) << "]= " << mlog->_icount << endl;
cout << "Total count[" << decstr(tid) << "]= " << mlog->_total << endl;
delete mlog;
PIN_SetThreadData(mlog_key, 0, tid);
}
/*VOID Fini(INT32 code, VOID *v)
{
fclose(trace);
}*/
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
cerr << "This tool counts the number of dynamic instructions executed" << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
/* argc, argv are the entire command line: pin -t <toolname> -- ... */
/* ===================================================================== */
int main(int argc, char * argv[])
{
InitLock(&lock);
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
//OutFile.open(KnobOutputFile.Value().c_str());
mlog_key = PIN_CreateThreadDataKey(0);
//trace = fopen("mem.trace", "w");
// Register Instruction to be called to instrument instructions
INS_AddInstrumentFunction(Instruction, 0);
// Register Fini to be called when the application exits
// PIN_AddFiniFunction(Fini, 0);
// add callbacks
PIN_AddThreadStartFunction(ThreadStart, 0);
PIN_AddThreadFiniFunction(ThreadFini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}