-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaple.cpp
More file actions
executable file
·232 lines (196 loc) · 6.54 KB
/
maple.cpp
File metadata and controls
executable file
·232 lines (196 loc) · 6.54 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
//
// maple.cpp
// MAPLE: Memory Access ProfiLEr
//
// Created by Reza Karimi on 07/20/2018.
// Copyright © 2018 Reza Karimi. All rights reserved.
//
#include "pin.H"
#include <bitset>
#include <fcntl.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <set>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
#include <cstdlib>
using namespace std;
#define KB 1024
#define MB (KB * 1024)
#define L_CACHE_SIZE (5 * MB)
#define PAGE_SIZE_ (4 * KB)
#define ITEM_SIZE 64 // bytes
#define L_CACHE_SIZE_ITEMS (int)(L_CACHE_SIZE / ITEM_SIZE)
#define OUTOUT_HEADERS "page,time,op\n"
PIN_LOCK lock;
FILE *trace;
int rcount = 0;
int wcount = 0;
int start_logging = 0;
stringstream buffer;
long int *sz;
unsigned long long previous_access;
unsigned long long epoch;
set<unsigned long long> cache_set;
map<unsigned long long, unsigned long long> cache_timestamp;
int output_file;
char buf[50];
unsigned long hits = 0;
unsigned long total = 0;
unsigned int index;
void log(char op, unsigned long long item, unsigned long long page)
{
unsigned long timestamp = (unsigned long)time(0) - epoch;
if (cache_set.count(item))
{ // on a hit
cache_timestamp[item] = timestamp; // just update the timestamp
}
else
{ // on a miss
if (cache_set.size() <= L_CACHE_SIZE_ITEMS)
{ // no need for eviction, yay!
cache_set.insert(item);
cache_timestamp[item] = timestamp;
}
else
{ // must evict someone, so sad :(
unsigned long long victim = -1;
unsigned long long least_recently_used = ULONG_LONG_MAX;
for (map<unsigned long long, unsigned long long>::iterator it =
cache_timestamp.begin();
it != cache_timestamp.end(); ++it)
{
if (it->second < least_recently_used)
{
victim = it->first;
least_recently_used = it->second;
}
}
cache_set.erase(victim);
cache_timestamp.erase(victim);
cache_set.insert(item);
cache_timestamp[item] = timestamp;
}
sprintf(buf, "%llu,%lu,%c\n", page, timestamp, op);
write(output_file, buf, strlen(buf));
}
}
// This routine is executed every time a thread is created.
VOID ThreadStart(THREADID threadid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
PIN_GetLock(&lock, threadid + 1);
cout << "thread begin: " << threadid << endl;
// pid_t pid = getpid();
// pid_t tid = syscall(SYS_gettid);
cout << "Thread " << threadid << syscall(SYS_gettid) << endl;
PIN_ReleaseLock(&lock);
}
// This routine is executed every time a thread is destroyed.
VOID ThreadFini(THREADID threadid, const CONTEXT *ctxt, INT32 code, VOID *v)
{
PIN_GetLock(&lock, threadid + 1);
cout << "thread end: " << threadid << endl;
PIN_ReleaseLock(&lock);
}
// Print a memory read record
VOID RecordMemRead(VOID *ip, VOID *addr, THREADID threadid)
{
PIN_GetLock(&lock, threadid + 1);
buffer << addr;
string s = buffer.str();
//s = s.substr(2, (s.length() - 1)); // removing 0x
unsigned long long virtual_address = strtoull(s.c_str(), NULL, 16);
unsigned long long virtual_item = virtual_address / ITEM_SIZE;
unsigned long long virtual_page = virtual_address / PAGE_SIZE_;
log('r', virtual_item, virtual_page);
buffer.str("");
PIN_ReleaseLock(&lock);
}
// Print a memory write record
VOID RecordMemWrite(VOID *ip, VOID *addr, THREADID threadid)
{
PIN_GetLock(&lock, threadid + 1);
buffer << addr;
string s = buffer.str();
//s = s.substr(2, (s.length() - 1)); // removing 0x
unsigned long long virtual_address = strtoull(s.c_str(), NULL, 16);
unsigned long long virtual_item = virtual_address / ITEM_SIZE;
unsigned long long virtual_page = virtual_address / PAGE_SIZE_;
log('w', virtual_item, virtual_page);
buffer.str("");
PIN_ReleaseLock(&lock);
}
// Is called for every instruction and instruments reads and writes
VOID Instruction(INS ins, VOID *v)
{
// Instruments memory accesses using a predicated call, i.e.
// the instrumentation is called iff the instruction will actually be
// executed.
//
// On the IA-32 and Intel(R) 64 architectures conditional moves and REP
// prefixed instructions appear as predicated instructions in Pin.
UINT32 memOperands = INS_MemoryOperandCount(ins);
// Iterate over each memory operand of the instruction.
for (UINT32 memOp = 0; memOp < memOperands; memOp++)
{
if (INS_MemoryOperandIsRead(ins, memOp))
{
INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)RecordMemRead,
IARG_INST_PTR, IARG_MEMORYOP_EA, memOp,
IARG_THREAD_ID, IARG_END);
}
// Note that in some architectures a single memory operand can be
// both read and written (for instance incl (%eax) on IA-32)
// In that case we instrument it once for read and once for write.
if (INS_MemoryOperandIsWritten(ins, memOp))
{
INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)RecordMemWrite,
IARG_INST_PTR, IARG_MEMORYOP_EA, memOp,
IARG_THREAD_ID, IARG_END);
}
}
}
VOID Fini(INT32 code, VOID *v) { close(output_file); }
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
PIN_ERROR("This Pintool prints a trace of memory addresses\n" +
KNOB_BASE::StringKnobSummary() + "\n");
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
int main(int argc, char *argv[])
{
char file_name[50];
sprintf(file_name, "trace_%lu.csv", (unsigned long)time(0));
output_file = open(file_name, O_CREAT | O_RDWR);
previous_access = -1;
epoch = (unsigned long)time(NULL);
write(output_file, OUTOUT_HEADERS, strlen(OUTOUT_HEADERS));
// Initialze the pin lock
PIN_InitLock(&lock);
// Initialize pin
if (PIN_Init(argc, argv))
return Usage();
INS_AddInstrumentFunction(Instruction, 0);
// Register Analysis routines to be called when a thread begins/ends
PIN_AddThreadStartFunction(ThreadStart, 0);
PIN_AddThreadFiniFunction(ThreadFini, 0);
// Register Fini to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Never returns
PIN_StartProgram();
return 0;
}