forked from boegel/MICA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmica_fullmemstackdist.cpp
296 lines (241 loc) · 7.65 KB
/
mica_fullmemstackdist.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* This file is part of MICA, a Pin tool to collect
* microarchitecture-independent program characteristics using the Pin
* instrumentation framework.
*
* Please see the README.txt file distributed with the MICA release for more
* information.
*/
#include "pin.H"
/* MICA includes */
#include "mica_utils.h"
#include "mica_fullmemstackdist.h"
/* Global variables */
extern INT64 interval_size;
extern INT64 interval_ins_count;
extern INT64 interval_ins_count_for_hpc_alignment;
extern INT64 total_ins_count;
extern INT64 total_ins_count_for_hpc_alignment;
extern UINT32 _block_size;
/* A single entry of the cache line reference stack.
* below points to the entry below us in the stack
* above points to the entry above us in the stack
* block_addr is the cache line index of this entry
* distance is the distance in the stack
*/
typedef struct stack_entry_type {
struct stack_entry_type* below;
struct stack_entry_type* above;
ADDRINT block_addr;
INT32 distance;
} stack_entry;
/* A single entry of the hash table, contains an array of stack entries referenced by part of cache line index. */
typedef struct block_type_fast {
ADDRINT id;
stack_entry* stack_entries[MAX_MEM_ENTRIES];
struct block_type_fast* next;
} block_fast;
static stack_entry* stack_top;
static stack_entry* stack_bottom;
static block_fast* hashTableCacheBlocks_fast[MAX_MEM_TABLE_ENTRIES];
static INT64 mem_ref_cnt;
static INT64 cold_refs;
static ofstream output_file;
/* Counters of accesses for each distance. */
#define DIST_MAX 2049
static INT64 distances[DIST_MAX];
/* initializing */
void init_fullmemstackdist(){
int i;
/* initialize */
cold_refs = 0;
mem_ref_cnt = 0;
for(i = 0; i < DIST_MAX; i++) {
distances[i] = 0;
}
/* hash table */
for (i = 0; i < MAX_MEM_TABLE_ENTRIES; i++) {
hashTableCacheBlocks_fast[i] = NULL;
}
stack_bottom = stack_top = NULL;
if(interval_size != -1){
output_file.open(mkfilename("fullmemstackdist_phases_int"), ios::out|ios::trunc);
output_file.close();
}
}
static ADDRINT fullmemstackdist_instr_intervals(){
/* counting instructions is done in all_instr_intervals() */
return (ADDRINT)(interval_ins_count_for_hpc_alignment == interval_size);
}
VOID fullmemstackdist_instr_interval_output(){
int i;
output_file.open(mkfilename("fullmemstackdist_phases_int"), ios::out|ios::app);
output_file << mem_ref_cnt << " " << cold_refs << endl;
for(i=0; i < DIST_MAX; i++){
output_file << " " << distances[i];
}
output_file << endl;
output_file.close();
}
VOID fullmemstackdist_instr_interval_reset(){
int i;
cold_refs = 0;
mem_ref_cnt = 0;
for(i = 0; i < DIST_MAX; i++) {
distances[i] = 0;
}
}
VOID fullmemstackdist_instr_interval(){
fullmemstackdist_instr_interval_output();
fullmemstackdist_instr_interval_reset();
interval_ins_count = 0;
interval_ins_count_for_hpc_alignment = 0;
}
/* hash table support */
/** entry_lookup
*
* Finds an arrray of stack entry references for a given address key (upper part of address) in a hash table.
*/
static stack_entry** entry_lookup(block_fast** table, ADDRINT key){
block_fast* b;
for (b = table[key % MAX_MEM_TABLE_ENTRIES]; b != NULL; b = b->next){
if(b->id == key)
return b->stack_entries;
}
return NULL;
}
/** entry_install
*
* Installs a new array of stack entry references for a given address key (upper part of address) in a hash table.
*/
static stack_entry** entry_install(block_fast** table, ADDRINT key){
block_fast* b;
ADDRINT index = key % MAX_MEM_TABLE_ENTRIES;
b = table[index];
if(b == NULL) {
b = (block_fast*)checked_malloc(sizeof(block_fast));
table[index] = b;
}
else{
while(b->next != NULL){
b = b->next;
}
b->next = (block_fast*)checked_malloc(sizeof(block_fast));
b = b->next;
}
b->next = NULL;
b->id = key;
for(ADDRINT i = 0; i < MAX_MEM_ENTRIES; i++){
b->stack_entries[i] = NULL;
}
return b->stack_entries;
}
/** add_new_stack_entry
*
* Add a new stack entry at the bottom of the stack. The distance is not
* initialized here.
*/
static stack_entry* add_new_stack_entry(ADDRINT a) {
// allocate memory for new stack entry
stack_entry* e = (stack_entry*) checked_malloc(sizeof(stack_entry));
// initialize with address
e->block_addr = a;
e->above = stack_bottom;
e->below = NULL;
if (stack_bottom) stack_bottom->below = e;
stack_bottom = e;
if (!stack_top) stack_top = e;
return e;
}
/** move_to_top
*
* Moves the stack entry e corresponding to the address a to the top of stack.
* The stack entry can't be NULL.
*/
static VOID move_to_top(stack_entry *e){
/* check to see if we are not already at top of stack */
if(e != stack_top){
// update positions
stack_entry *aux = stack_top;
while (aux != e) {
aux->distance++;
aux = aux->below;
}
// disconnect the entry from its current position on the stack
if (e->below) {
e->below->above = e->above;
} else {
stack_bottom = e->above;
}
e->above->below = e->below;
// place it on top of LRU stack
e->below = stack_top;
e->above = NULL;
stack_top->above = e;
stack_top = e;
}
e->distance = 0;
}
/* register memory access (either read of write) determine which cache lines are touched */
VOID fullmemstackdist_memRead(ADDRINT effMemAddr, ADDRINT size){
ADDRINT a, endAddr, addr, upperAddr, indexInChunk;
stack_entry** chunk;
stack_entry* entry_for_addr;
/* Calculate index in cache addresses. The calculation does not
* handle address overflows but those are unlikely to happen. */
addr = effMemAddr >> _block_size;
endAddr = (effMemAddr + size - 1) >> _block_size;
/* The hit is counted for all cache lines involved. */
for(a = addr; a <= endAddr; a++){
/* split the cache line address into hash key of chunk and index in chunk */
upperAddr = a >> LOG_MAX_MEM_ENTRIES;
indexInChunk = a & MASK_MAX_MEM_ENTRIES;
chunk = entry_lookup(hashTableCacheBlocks_fast, upperAddr);
if(chunk == NULL) chunk = entry_install(hashTableCacheBlocks_fast, upperAddr);
entry_for_addr = chunk[indexInChunk];
/* determine reuse distance for this access (if it has been accessed before) */
if (entry_for_addr) {
int d = entry_for_addr->distance;
if (d >= DIST_MAX) d = DIST_MAX - 1;
distances[d]++;
} else {
cold_refs++;
entry_for_addr = add_new_stack_entry(a);
}
/* adjust LRU stack */
move_to_top(entry_for_addr);
/* update hash table for new cache blocks */
if(chunk[indexInChunk] == NULL) chunk[indexInChunk] = stack_top;
mem_ref_cnt++;
}
}
VOID instrument_fullmemstackdist(INS ins, VOID *v){
if( INS_IsMemoryRead(ins) ){
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)fullmemstackdist_memRead, IARG_MEMORYREAD_EA, IARG_MEMORYREAD_SIZE, IARG_END);
if( INS_HasMemoryRead2(ins) )
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)fullmemstackdist_memRead, IARG_MEMORYREAD2_EA, IARG_MEMORYREAD_SIZE, IARG_END);
}
if(INS_IsMemoryWrite(ins)){
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)fullmemstackdist_memRead, IARG_MEMORYWRITE_EA, IARG_MEMORYWRITE_SIZE, IARG_END);
}
if(interval_size != -1){
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR)fullmemstackdist_instr_intervals,IARG_END);
/* only called if interval is 'full' */
INS_InsertThenCall(ins, IPOINT_BEFORE, (AFUNPTR)fullmemstackdist_instr_interval,IARG_END);
}
}
/* finishing... */
VOID fini_fullmemstackdist(INT32 code, VOID* v){
if (interval_size == -1) {
output_file.open(mkfilename("fullmemstackdist_full_int"), ios::out|ios::trunc);
}
else{
output_file.open(mkfilename("fullmemstackdist_phases_int"), ios::out|ios::app);
}
output_file << mem_ref_cnt << " " << cold_refs << endl;
for(int i = 0; i < DIST_MAX; i++){
output_file << " " << distances[i];
}
output_file << endl << "number of instructions: " << total_ins_count_for_hpc_alignment << endl;
output_file.close();
}