-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpincpt.cpp
More file actions
425 lines (341 loc) · 12.5 KB
/
Copy pathpincpt.cpp
File metadata and controls
425 lines (341 loc) · 12.5 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
/** On Linux IA-32 architectures, Pintools are built non-PIC (Position Independent Code),
* which allows the compiler to inline both local and global functions. Tools for Linux
* Intel(R) 64 architectures are built PIC, but the compiler will not inline any globally
* visible function due to function pre-emption. Therefore, it is advisable to declare the
* subroutines called by the analysis function as 'static' on Linux Intel(R) 64
* architectures. **/
#include "pincpt.h"
#include "bucket.h"
#include "cachesim.h"
#include "config.h"
#include "datastructs.h"
#include "imgload.h"
#include "mcslock.h"
#include "memoryblock.h"
#include "region.h"
#include "pin.H"
#include <algorithm>
#include <list>
#include <unordered_map>
#include <vector>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <unistd.h>
using std::list;
using std::string;
using std::unordered_map;
using std::vector;
// static unsigned long abefore_cnt = 0;
// each memory block can be contained in multiple stacks
// so we need to store the iterators to the memory block within each stack
//
// use hash map with custom hash function:
using AddrMap = std::unordered_map<Addr, IteratorContainer, std::hash<S>>;
AddrMap g_addrMap;
extern unsigned g_max_threads;
extern string g_application_name;
static unsigned num_threads = 0;
static PIN_MUTEX thread_mutex; // add / remove threads
static MCSLock mcslock{};
bool g_pindist_do_profile{true};
PIN_RWMUTEX g_rwlock; // required for new datastruct and region entry/exit events (see
// imgload.cpp)
unsigned g_max_threads{0};
static void on_block_new(Addr addr)
{
IteratorContainer &ic = g_addrMap[addr];
ic.ds_num_ = Datastruct::datastruct_num(addr);
ic.addr_ = addr;
for (PartitionedCache *pc : g_cachesims) {
// ic.iterators.emplace_back(pc->on_block_new(ic.ds_num)); // TODO: move ?
ic.iterators_.push_back(pc->on_block_new(ic.ds_num_)); // TODO: move ?
pc->incr_access_inf(ic.ds_num_);
}
}
[[maybe_unused]] static void on_block_prev(Addr addr)
{
size_t ds_num = g_addrMap[addr].ds_num_;
for (PartitionedCache *pc : g_cachesims) {
pc->incr_access(0, ds_num);
}
}
static void on_block_seen(AddrMap::iterator &it)
{
IteratorContainer &ic = it->second;
size_t ds_num = ic.ds_num_;
if (Datastruct::datastructs[ds_num].is_freed) {
ic.ds_num_ = Datastruct::datastruct_num(ic.addr_);
}
size_t idx = 0;
for (PartitionedCache *pc : g_cachesims) {
if (idx < ic.iterators_.size()) {
int bucket = pc->on_block_seen(ds_num, ic.iterators_[idx]);
pc->incr_access(bucket, ds_num);
} else {
// ic.iterators.emplace_back(pc->on_block_new(ic.ds_num)); // TODO: move ?
ic.iterators_.push_back(pc->on_block_new(ic.ds_num_)); // TODO: move ?
pc->incr_access_inf(ic.ds_num_);
}
idx++;
}
}
static void RD_accessBlock(Addr addr)
{
static AddrWrapper addr_prev[MAX_THREADS] = {0};
bool prev = (addr == addr_prev[PIN_ThreadId()].addr);
if (prev) {
// abefore_cnt++;
#if !RD_DO_NOT_COUNT_ZERO_DISTANCE
on_block_prev(addr);
#endif
// assert(g_cachesims[0]->stack_begin() == g_addrMap.find(addr)->second[0]);
} else {
auto it = g_addrMap.find(addr);
if (it == g_addrMap.end()) {
on_block_new(addr);
} else {
on_block_seen(it);
}
}
addr_prev[PIN_ThreadId()].addr = addr;
}
static void free_memory()
{
for (auto ®ion : g_regions) {
delete region.second;
region.second = nullptr;
}
for (auto &cs_ptr : g_cachesims) {
delete cs_ptr;
cs_ptr = nullptr;
}
}
void RD_print_csv()
{
const char *csv_header = "cachelinesize,region,data_object,addr,nbytes,line,file_"
"name,min_dist,threads,partition0,partition1\n";
// generate filename
constexpr size_t FILENAME_SIZE = 256u;
char csv_filename[FILENAME_SIZE];
snprintf(csv_filename,
FILENAME_SIZE,
"pincpt-%s-%04ut.csv",
g_application_name.c_str(),
g_max_threads);
FILE *csv_out = fopen(csv_filename, "w");
fprintf(csv_out, "%s", csv_header);
for (auto &cs : g_cachesims) {
cs->print_csv(csv_out, "main");
}
for (const auto ®ion : g_regions) {
region.second->print_csv(csv_out);
}
fclose(csv_out);
free_memory(); // TODO: move somewhere else
}
void RD_init()
{
g_addrMap.clear();
// global cache simulation
// g_cachesims.push_back(new CacheSim{});
Datastruct ds{};
Datastruct::register_datastruct(ds);
}
/* ===================================================================== */
/* Command line options */
/* ===================================================================== */
KNOB<int>
KnobMinDist(KNOB_MODE_WRITEONCE, "pintool", "m", "4096", "minimum bucket distance");
KNOB<int> KnobDoubleSteps(
KNOB_MODE_WRITEONCE, "pintool", "s", "1", "number of buckets for doubling distance");
KNOB<bool>
KnobPIDPrefix(KNOB_MODE_WRITEONCE, "pintool", "p", "0", "prepend output by --PID--");
/* ===================================================================== */
/* Direct Callbacks */
/* ===================================================================== */
// size: #bytes accessed
static void memAccess(ADDRINT addr, UINT32 size)
{
// printf("########## %p\n", (void*)addr);
// bytes accessed: [addr, ... , addr+size-1]
// calculate memory block (cacheline) of low address
Addr a1 = (void *)(addr & MEMBLOCK_MASK);
// calculate memory block (cacheline) of high address
Addr a2 = (void *)((addr + size - 1) & MEMBLOCK_MASK);
#if RD_DEBUG
static int threads;
#endif
mcslock.lock(PIN_ThreadId());
PIN_RWMutexReadLock(&g_rwlock);
#if RD_DEBUG
threads++;
assert(threads == 1);
#endif
// single memory block accessed
if (a1 == a2) {
if (VERBOSE > 1)
fprintf(stderr, " => %p\n", a1);
RD_accessBlock(a1);
}
// memory access spans across two memory blocks
// => two memory blocks accessed
else {
if (VERBOSE > 1)
fprintf(stderr, " => CROSS %p/%p\n", a1, a2);
RD_accessBlock(a1);
RD_accessBlock(a2);
}
#if RD_DEBUG
assert(threads == 1);
threads--;
#endif
PIN_RWMutexUnlock(&g_rwlock);
mcslock.unlock(PIN_ThreadId());
}
static /* PIN_FAST_ANALYSIS_CALL */ VOID memRead(THREADID tid, ADDRINT addr, UINT32 size)
{
if (VERBOSE > 1)
fprintf(stderr, "R %p/%d", (void *)addr, size);
// if (g_pindist_do_profile)
memAccess(addr, size);
}
static VOID /* PIN_FAST_ANALYSIS_CALL */ memWrite(THREADID tid, ADDRINT addr, UINT32 size)
{
if (VERBOSE > 1)
fprintf(stderr, "W %p/%d", (void *)addr, size);
// if (g_pindist_do_profile)
memAccess(addr, size);
}
// [[maybe_unused]] static /* PIN_FAST_ANALYSIS_CALL */ VOID stackAccess() {
// stackAccesses++; }
/* ===================================================================== */
/* Instrumentation */
/* ===================================================================== */
VOID Instruction(INS ins, VOID *v)
{
if (!g_pindist_do_profile) {
return;
}
if (IGNORE_STACK && (INS_IsStackRead(ins) || INS_IsStackWrite(ins))) {
// INS_InsertPredicatedCall(ins, IPOINT_BEFORE, (AFUNPTR)stackAccess, IARG_END);
return;
}
// IARG_FAST_ANALYSIS_CALL
UINT32 memOperands = INS_MemoryOperandCount(ins);
for (UINT32 memOp = 0; memOp < memOperands; memOp++) {
if (INS_MemoryOperandIsRead(ins, memOp))
INS_InsertPredicatedCall(ins,
IPOINT_BEFORE,
(AFUNPTR)memRead,
/*IARG_FAST_ANALYSIS_CALL*/ IARG_THREAD_ID,
IARG_MEMORYOP_EA,
memOp,
IARG_UINT32,
INS_MemoryOperandSize(ins, memOp),
IARG_END);
if (INS_MemoryOperandIsWritten(ins, memOp))
INS_InsertPredicatedCall(ins,
IPOINT_BEFORE,
(AFUNPTR)memWrite,
/*IARG_FAST_ANALYSIS_CALL*/ IARG_THREAD_ID,
IARG_MEMORYOP_EA,
memOp,
IARG_UINT32,
INS_MemoryOperandSize(ins, memOp),
IARG_END);
}
}
/* ===================================================================== */
/* Callbacks from Pin */
/* ===================================================================== */
VOID ThreadStart(THREADID tid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
fprintf(stderr, "Thread %d started\n", tid);
PIN_MutexLock(&thread_mutex);
num_threads++;
g_max_threads = num_threads > g_max_threads ? num_threads : g_max_threads;
if (num_threads > MAX_THREADS) {
/* TODO */
}
PIN_MutexUnlock(&thread_mutex);
}
VOID ThreadFini(THREADID tid, const CONTEXT *ctxt, INT32 flags, VOID *v)
{
fprintf(stderr, "Thread %d finished\n", tid);
PIN_MutexLock(&thread_mutex);
num_threads--;
PIN_MutexUnlock(&thread_mutex);
}
/* ===================================================================== */
/* Output results at exit */
/* ===================================================================== */
extern void print_fnargmap(); // TODO: move to header
VOID Exit(INT32 code, VOID *v)
{
// fprintf(out, "%s ignored stack accesses: %lu\n", pStr, stackAccesses);
// fprintf(out, "%s ignored accesses by thread != 0: %lu reads, %lu writes\n", pStr,
// ignoredReads, ignoredWrites);
RD_print_csv();
print_fnargmap();
// free_memory();
}
/* ===================================================================== */
/* Usage/Main Function of the Pin Tool */
/* ===================================================================== */
INT32 Usage()
{
PIN_ERROR("PinDist: Get the Stack Reuse Distance Histogram\n" +
KNOB_BASE::StringKnobSummary() + "\n");
return -1;
}
int main(int argc, char *argv[])
{
if (PIN_Init(argc, argv))
return Usage();
PIN_InitSymbols();
IMG_AddInstrumentFunction(ImageLoad, 0);
INS_AddInstrumentFunction(Instruction, 0);
PIN_AddFiniFunction(Exit, 0);
PIN_AddThreadStartFunction(ThreadStart, 0);
PIN_AddThreadFiniFunction(ThreadFini, 0);
PIN_RWMutexInit(&g_rwlock);
PIN_MutexInit(&thread_mutex);
// required buckets for a64fx:
// 4-way L1d 64KiB => 4 Buckets with distance 64KiB / 4
// 16-way L2 8MiB => 16 Buckets with distance 8MiB / 16
//
int KiB = 1024;
int MiB = 1024 * KiB;
int L1ways = 4;
int L2ways = 16;
[[maybe_unused]] int L1d_capacity_per_way = 64 * KiB / 4;
int L2_capacity_per_way = 8 * MiB / 16;
Bucket::min_dists.push_back(0);
for (int i = 0; i < L1ways; i++)
Bucket::min_dists.push_back(L1d_capacity_per_way * (i + 1) / MEMBLOCKLEN);
// 12 threads
// for (int i = 0; i < L1ways; i++)
// Bucket::min_dists.push_back(12 * L1d_capacity_per_way * (i + 1) / MEMBLOCKLEN);
// 48 threads
// for (int i = 0; i < L1ways; i++)
// Bucket::min_dists.push_back(48 * L1d_capacity_per_way * (i + 1) / MEMBLOCKLEN);
for (int i = 0; i < L2ways; i++)
Bucket::min_dists.push_back(L2_capacity_per_way * (i + 1) / MEMBLOCKLEN);
// 4 shared caches (48 threads)
// for (int i = 0; i < L2ways; i++)
// Bucket::min_dists.push_back(4 * L2_capacity_per_way * (i + 1) / MEMBLOCKLEN);
// bucket for cold misses (infinite reuse distance)
Bucket::min_dists.push_back(Bucket::inf_dist);
// sort buckets in ascending order
std::sort(Bucket::min_dists.begin(), Bucket::min_dists.end());
// remove duplicates in buckets
Bucket::min_dists.erase(
std::unique(std::begin(Bucket::min_dists), std::end(Bucket::min_dists)),
std::end(Bucket::min_dists));
RD_init();
// stackAccesses = 0;
PIN_StartProgram();
return 0;
}