-
Notifications
You must be signed in to change notification settings - Fork 0
/
pin-cache.cc
135 lines (108 loc) · 3.74 KB
/
pin-cache.cc
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
/**
* Glue code for running the AvDark cache simulator in Pin.
*/
#include "pin.H"
#include <iostream>
#include <fstream>
#include <sys/time.h>
extern "C" {
#include "cache.h"
}
KNOB<string> knob_output(KNOB_MODE_WRITEONCE, "pintool",
"o", "cache.out", "specify log file name");
KNOB<UINT32> knob_size(KNOB_MODE_WRITEONCE, "pintool",
"s", "8388608", "Cache size (bytes)");
KNOB<UINT32> knob_associativity(KNOB_MODE_WRITEONCE, "pintool",
"a", "1", "Cache associativity");
KNOB<UINT32> knob_line_size(KNOB_MODE_WRITEONCE, "pintool",
"l", "64", "Cache line size");
static avdark_cache_t *avdc = NULL;
/**
* Memory access callback. Will be called for every memory access
* executed by the the target application.
*/
static VOID
simulate_access(VOID *addr, UINT32 access_type)
{
/* NOTE: We deliberately ignore the fact that addr may
* straddle multiple cache lines */
avdc_access(avdc, (avdc_pa_t)addr, (avdc_access_type_t)access_type);
}
/**
* PIN instrumentation callback, called for every new instruction that
* PIN discovers in the application. This function is used to
* instrument code blocks by inserting calls to instrumentation
* functions.
*/
static VOID
instruction(INS ins, VOID *not_used)
{
UINT32 no_ops = INS_MemoryOperandCount(ins);
for (UINT32 op = 0; op < no_ops; op++) {
const UINT32 size = INS_MemoryOperandSize(ins, op);
const bool is_rd = INS_MemoryOperandIsRead(ins, op);
const bool is_wr = INS_MemoryOperandIsWritten(ins, op);
const UINT32 atype = is_wr ? AVDC_WRITE : AVDC_READ;
INS_InsertCall(ins, IPOINT_BEFORE,
(AFUNPTR)simulate_access,
IARG_MEMORYOP_EA, op,
IARG_UINT32, atype,
IARG_END);
}
}
/**
* PIN fini callback. Called after the target application has
* terminated. Used to print statistics and do cleanup.
*/
static VOID
fini(INT32 code, VOID *v)
{
std::ofstream out(knob_output.Value().c_str());
int accesses = avdc->stat_data_read + avdc->stat_data_write;
int misses = avdc->stat_data_read_miss + avdc->stat_data_write_miss;
out << "Cache statistics:" << endl;
out << " Writes: " << avdc->stat_data_write << endl;
out << " Write Misses: " << avdc->stat_data_write_miss << endl;
out << " Reads: " << avdc->stat_data_read << endl;
out << " Read Misses: " << avdc->stat_data_read_miss << endl;
out << " Misses: " << misses << endl;
out << " Accesses: " << accesses << endl;
out << " Miss Ratio: " << ((100.0 * misses) / accesses) << "%" << endl;
avdc_delete(avdc);
}
static int
usage()
{
cerr <<
"This is the Advanced Computer Architecture online analysis tool\n"
"\n";
cerr << KNOB_BASE::StringKnobSummary();
cerr << endl;
return -1;
}
int main(int argc, char *argv[])
{
if (PIN_Init(argc, argv))
return usage();
avdc_size_t size = knob_size.Value();
avdc_block_size_t block_size = knob_line_size.Value();
avdc_assoc_t assoc = knob_associativity.Value();
avdc = avdc_new(size, block_size, assoc);
if (!avdc) {
cerr << "Failed to initialize the AvDark cache simulator." << endl;
return -1;
}
INS_AddInstrumentFunction(instruction, 0);
PIN_AddFiniFunction(fini, 0);
PIN_StartProgram();
return 0;
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 8
* indent-tabs-mode: nil
* c-file-style: "linux"
* compile-command: "make -k -C ../../"
* End:
*/