forked from MaximeCheramy/MICA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mica_memfootprint.cpp
297 lines (224 loc) · 8.56 KB
/
mica_memfootprint.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
297
/*
* 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_memfootprint.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;
extern UINT32 _page_size;
static UINT32 memfootprint_block_size;
static UINT32 page_size;
static ofstream output_file_memfootprint;
static nlist* DmemCacheWorkingSetTable[MAX_MEM_TABLE_ENTRIES];
static nlist* DmemPageWorkingSetTable[MAX_MEM_TABLE_ENTRIES];
static nlist* ImemCacheWorkingSetTable[MAX_MEM_TABLE_ENTRIES];
static nlist* ImemPageWorkingSetTable[MAX_MEM_TABLE_ENTRIES];
static long long DmemCacheWSS() {
long long DmemCacheWorkingSetSize = 0L;
for (int i = 0; i < MAX_MEM_TABLE_ENTRIES; i++) {
for (nlist *np = DmemCacheWorkingSetTable [i]; np != (nlist*) NULL; np = np->next) {
for (ADDRINT j = 0; j < MAX_MEM_BLOCK; j++) {
if ((np->mem)->numReferenced [j]) {
DmemCacheWorkingSetSize++;
}
}
}
}
return DmemCacheWorkingSetSize;
}
static long long ImemCacheWSS() {
long long ImemCacheWorkingSetSize = 0L;
for (int i = 0; i < MAX_MEM_TABLE_ENTRIES; i++) {
for (nlist *np = ImemCacheWorkingSetTable [i]; np != (nlist*) NULL; np = np->next) {
for (ADDRINT j = 0; j < MAX_MEM_BLOCK; j++) {
if ((np->mem)->numReferenced [j]) {
ImemCacheWorkingSetSize++;
}
}
}
}
return ImemCacheWorkingSetSize;
}
static long long DmemPageWSS() {
long long DmemPageWorkingSetSize = 0L;
for (int i = 0; i < MAX_MEM_TABLE_ENTRIES; i++) {
for (nlist *np = DmemPageWorkingSetTable [i]; np != (nlist*) NULL; np = np->next) {
for (ADDRINT j = 0; j < MAX_MEM_BLOCK; j++) {
if ((np->mem)->numReferenced [j]) {
DmemPageWorkingSetSize++;
}
}
}
}
return DmemPageWorkingSetSize;
}
static long long ImemPageWSS() {
long long ImemPageWorkingSetSize = 0L;
for (int i = 0; i < MAX_MEM_TABLE_ENTRIES; i++) {
for (nlist *np = ImemPageWorkingSetTable [i]; np != (nlist*) NULL; np = np->next) {
for (ADDRINT j = 0; j < MAX_MEM_BLOCK; j++) {
if ((np->mem)->numReferenced [j]) {
ImemPageWorkingSetSize++;
}
}
}
}
return ImemPageWorkingSetSize;
}
/* initializing */
void init_memfootprint(){
int i;
for (i = 0; i < MAX_MEM_TABLE_ENTRIES; i++) {
DmemCacheWorkingSetTable[i] = (nlist*) NULL;
DmemPageWorkingSetTable[i] = (nlist*) NULL;
ImemCacheWorkingSetTable[i] = (nlist*) NULL;
ImemPageWorkingSetTable[i] = (nlist*) NULL;
}
memfootprint_block_size = _block_size;
page_size = _page_size;
if(interval_size != -1){
output_file_memfootprint.open(mkfilename("memfootprint_phases_int"), ios::out|ios::trunc);
output_file_memfootprint.close();
}
}
VOID memOp(ADDRINT effMemAddr, ADDRINT size){
if(size > 0){
ADDRINT a;
ADDRINT addr, endAddr, upperAddr, indexInChunk;
memNode* chunk;
/* D-stream (64-byte) cache block memory footprint */
addr = effMemAddr >> memfootprint_block_size;
endAddr = (effMemAddr + size - 1) >> memfootprint_block_size;
for(a = addr; a <= endAddr; a++){
upperAddr = a >> LOG_MAX_MEM_BLOCK;
indexInChunk = a ^ (upperAddr << LOG_MAX_MEM_BLOCK);
chunk = lookup(DmemCacheWorkingSetTable, upperAddr);
if(chunk == (memNode*)NULL)
chunk = install(DmemCacheWorkingSetTable, upperAddr);
//assert(indexInChunk >= 0 && indexInChunk < MAX_MEM_BLOCK);
chunk->numReferenced[indexInChunk] = true;
}
/* D-stream (4KB) page block memory footprint */
addr = effMemAddr >> page_size;
endAddr = (effMemAddr + size - 1) >> page_size;
for(a = addr; a <= endAddr; a++){
upperAddr = a >> LOG_MAX_MEM_BLOCK;
indexInChunk = a ^ (upperAddr << LOG_MAX_MEM_BLOCK);
chunk = lookup(DmemPageWorkingSetTable, upperAddr);
if(chunk == (memNode*)NULL)
chunk = install(DmemPageWorkingSetTable, upperAddr);
//assert(indexInChunk >= 0 && indexInChunk < MAX_MEM_BLOCK);
chunk->numReferenced[indexInChunk] = true;
}
}
}
VOID instrMem(ADDRINT instrAddr, ADDRINT size){
if(size > 0){
ADDRINT a;
ADDRINT addr, endAddr, upperAddr, indexInChunk;
memNode* chunk;
/* I-stream (64-byte) cache block memory footprint */
addr = instrAddr >> memfootprint_block_size;
endAddr = (instrAddr + size - 1) >> memfootprint_block_size;
for(a = addr; a <= endAddr; a++){
upperAddr = a >> LOG_MAX_MEM_BLOCK;
indexInChunk = a ^ (upperAddr << LOG_MAX_MEM_BLOCK);
chunk = lookup(ImemCacheWorkingSetTable, upperAddr);
if(chunk == (memNode*)NULL)
chunk = install(ImemCacheWorkingSetTable, upperAddr);
//assert(indexInChunk >= 0 && indexInChunk < MAX_MEM_BLOCK);
chunk->numReferenced[indexInChunk] = true;
}
/* I-stream (4KB) page block memory footprint */
addr = instrAddr >> page_size;
endAddr = (instrAddr + size - 1) >> page_size;
for(a = addr; a <= endAddr; a++){
upperAddr = a >> LOG_MAX_MEM_BLOCK;
indexInChunk = a ^ (upperAddr << LOG_MAX_MEM_BLOCK);
chunk = lookup(ImemPageWorkingSetTable, upperAddr);
if(chunk == (memNode*)NULL)
chunk = install(ImemPageWorkingSetTable, upperAddr);
//assert(indexInChunk >= 0 && indexInChunk < MAX_MEM_BLOCK);
chunk->numReferenced[indexInChunk] = true;
}
}
}
static VOID memfootprint_instr_full(ADDRINT instrAddr, ADDRINT size){
/* counting instructions is done in all_instr_full() */
instrMem(instrAddr, size);
}
static ADDRINT memfootprint_instr_intervals(ADDRINT instrAddr, ADDRINT size){
/* counting instructions is done in all_instr_intervals() */
instrMem(instrAddr, size);
return (ADDRINT)(interval_ins_count_for_hpc_alignment == interval_size);
}
VOID memfootprint_instr_interval_output(){
output_file_memfootprint.open(mkfilename("memfootprint_phases_int"), ios::out|ios::app);
long long DmemCacheWorkingSetSize = DmemCacheWSS();
long long DmemPageWorkingSetSize = DmemPageWSS();
long long ImemCacheWorkingSetSize = ImemCacheWSS();
long long ImemPageWorkingSetSize = ImemPageWSS();
output_file_memfootprint << DmemCacheWorkingSetSize << " " << DmemPageWorkingSetSize << " " << ImemCacheWorkingSetSize << " " << ImemPageWorkingSetSize << endl;
output_file_memfootprint.close();
}
VOID memfootprint_instr_interval_reset(){
/* clean used memory, to avoid memory shortage for long (CPU2006) benchmarks */
for(ADDRINT i=0; i < MAX_MEM_TABLE_ENTRIES; i++){
free_nlist(DmemCacheWorkingSetTable[i]);
free_nlist(DmemPageWorkingSetTable[i]);
free_nlist(ImemCacheWorkingSetTable[i]);
free_nlist(ImemPageWorkingSetTable[i]);
}
}
static VOID memfootprint_instr_interval(){
memfootprint_instr_interval_output();
memfootprint_instr_interval_reset();
interval_ins_count = 0;
interval_ins_count_for_hpc_alignment = 0;
}
/* instrumenting (instruction level) */
VOID instrument_memfootprint(INS ins, VOID* v){
if(INS_IsMemoryRead(ins)){
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)memOp, IARG_MEMORYREAD_EA, IARG_MEMORYREAD_SIZE, IARG_END);
if(INS_HasMemoryRead2(ins)){
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)memOp, IARG_MEMORYREAD2_EA, IARG_MEMORYREAD_SIZE, IARG_END);
}
}
if(INS_IsMemoryWrite(ins)){
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)memOp, IARG_MEMORYWRITE_EA, IARG_MEMORYWRITE_SIZE, IARG_END);
}
if(interval_size == -1)
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)memfootprint_instr_full, IARG_ADDRINT, INS_Address(ins), IARG_ADDRINT, (ADDRINT)INS_Size(ins), IARG_END);
else{
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR)memfootprint_instr_intervals, IARG_ADDRINT, INS_Address(ins), IARG_ADDRINT, (ADDRINT)INS_Size(ins), IARG_END);
INS_InsertThenCall(ins, IPOINT_BEFORE, (AFUNPTR)memfootprint_instr_interval, IARG_END);
}
}
/* finishing... */
VOID fini_memfootprint(INT32 code, VOID* v){
long long DmemCacheWorkingSetSize = DmemCacheWSS();
long long DmemPageWorkingSetSize = DmemPageWSS();
long long ImemCacheWorkingSetSize = ImemCacheWSS();
long long ImemPageWorkingSetSize = ImemPageWSS();
if(interval_size == -1){
output_file_memfootprint.open(mkfilename("memfootprint_full_int"), ios::out|ios::trunc);
}
else{
output_file_memfootprint.open(mkfilename("memfootprint_phases_int"), ios::out|ios::app);
}
output_file_memfootprint << DmemCacheWorkingSetSize << " " << DmemPageWorkingSetSize << " " << ImemCacheWorkingSetSize << " " << ImemPageWorkingSetSize << endl;
output_file_memfootprint << "number of instructions: " << total_ins_count_for_hpc_alignment << endl;
output_file_memfootprint.close();
}