forked from MaximeCheramy/MICA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmica.cpp
513 lines (441 loc) · 13 KB
/
mica.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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* 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.
*/
/*************************************************************************
* *
* MICA: Microarchitecture-Independent Characterization of Workloads *
* *
*************************************************************************
*
* implementation by Kenneth Hoste (Ghent University, Belgium), December 2006 - March 2011
* based on code written by Lieven Eeckhout (for ATOM on Alpha)
*
* PLEASE DO NOT REDISTRIBUTE THIS CODE WITHOUT INFORMING THE AUTHORS.
*
* contact: [email protected] , [email protected]
*
*/
/* MICA includes */
#include "mica.h"
#include "mica_init.h"
#include "mica_utils.h"
#include "mica_all.h"
#include "mica_ilp.h"
#include "mica_itypes.h"
#include "mica_ppm.h"
#include "mica_reg.h"
#include "mica_stride.h"
#include "mica_memfootprint.h"
#include "mica_memstackdist.h"
#include "mica_fullmemstackdist.h"
#include "mica_linecount.h"
#include <sstream>
#include <iostream>
#include <iomanip>
#include <unistd.h>
using namespace std;
/* *** Variables *** */
/* global */
INT64 interval_size; // interval size chosen
INT64 interval_ins_count;
INT64 interval_ins_count_for_hpc_alignment;
INT64 total_ins_count;
INT64 total_ins_count_for_hpc_alignment;
ins_buffer_entry* ins_buffer[MAX_MEM_TABLE_ENTRIES];
/* ILP */
UINT32 _ilp_win_size;
char* _itypes_spec_file;
/* ILP, MEMFOOTPRINT, MEMSTACKDIST */
UINT32 _block_size;
/* MEMFOOTPRINT */
UINT32 _page_size;
/* for multiprocess binaries */
int append_pid;
/* helper */
int thread_count = 0;
static bool use_magic_instruction = false;
static bool start_monitoring = false;
/**********************************************
* MAIN *
**********************************************/
//FILE* log;
ofstream log;
/* append <pid>_pin.out to name if necessary */
const char *mkfilename(const char *name)
{
stringstream ret;
if (append_pid)
ret << name << "_" << setfill('0') << setw(5) << getpid() << "_pin.out";
else
ret << name << "_pin.out";
return ret.str().c_str();
}
// find buffer entry for instruction at given address in a hash table
ins_buffer_entry* findInsBufferEntry(ADDRINT a){
ins_buffer_entry* e;
INT64 key = a % MAX_MEM_TABLE_ENTRIES;
e = ins_buffer[key];
if(e != NULL){
do{
if(e->insAddr == a)
break;
e = e->next;
} while(e->next != (ins_buffer_entry*)NULL);
/* ins address not found, installing */
if(e == NULL){
e = (ins_buffer_entry*)checked_malloc(sizeof(ins_buffer_entry));
e->insAddr = a;
e->regReadCnt = 0;
e->regsRead = NULL;
e->regWriteCnt = 0;
e->regsWritten = NULL;
e->next = NULL;
e->setRead = false;
e->setWritten = false;
e->setRegOpCnt = false;
ins_buffer_entry* tmp = e = ins_buffer[key];
while(tmp->next != (ins_buffer_entry*)NULL)
tmp = tmp->next;
tmp->next = e;
}
}
else{
/* new entry in hash table */
e = (ins_buffer_entry*)checked_malloc(sizeof(ins_buffer_entry));
e->insAddr = a;
e->regOpCnt = 0;
e->regReadCnt = 0;
e->regsRead = NULL;
e->regWriteCnt = 0;
e->regsWritten = NULL;
e->next = NULL;
e->setRead = false;
e->setWritten = false;
}
return e;
}
static void instrument_start_monitoring(INS ins, VOID* v) {
if (use_magic_instruction) {
if (INS_IsXchg(ins) && INS_OperandReg(ins, 0) == INS_OperandReg(ins, 1)) {
start_monitoring ^= true;
}
}
}
static void instrument_instruction_count(INS ins, VOID* v) {
if(interval_size == -1) {
if(INS_HasRealRep(ins)){
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR)returnArg, IARG_FIRST_REP_ITERATION, IARG_END);
INS_InsertThenCall(ins, IPOINT_BEFORE, (AFUNPTR)all_instr_full_count_for_hpc_alignment_with_rep, IARG_REG_VALUE, INS_RepCountRegister(ins), IARG_END);
}
else{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)all_instr_full_count_for_hpc_alignment_no_rep, IARG_END);
}
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)all_instr_full_count_always, IARG_END);
}
else{
if(INS_HasRealRep(ins)){
INS_InsertIfCall(ins, IPOINT_BEFORE, (AFUNPTR)returnArg, IARG_FIRST_REP_ITERATION, IARG_END);
INS_InsertThenCall(ins, IPOINT_BEFORE, (AFUNPTR)all_instr_intervals_count_for_hpc_alignment_with_rep, IARG_REG_VALUE, INS_RepCountRegister(ins), IARG_END);
}
else{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)all_instr_intervals_count_for_hpc_alignment_no_rep, IARG_END);
}
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)all_instr_intervals_count_always, IARG_END);
}
}
/* ALL */
VOID Instruction_all(INS ins, VOID* v) {
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
ADDRINT insAddr = INS_Address(ins);
ins_buffer_entry* e = findInsBufferEntry(insAddr);
//instrument_ilp_all(ins, e);
//instrument_itypes(ins, v);
//instrument_ppm(ins, v);
//instrument_reg(ins, e);
//instrument_stride(ins, v);
//instrument_memfootprint(ins, v);
//instrument_memstackdist(ins, v);
instrument_all(ins, v, e);
}
}
VOID Fini_all(INT32 code, VOID* v){
fini_ilp_all(code, v);
fini_itypes(code, v);
fini_ppm(code, v);
fini_reg(code, v);
fini_stride(code, v);
fini_memfootprint(code, v);
fini_memstackdist(code, v);
fini_fullmemstackdist(code, v);
fini_linecount(code, v);
}
/* ILP */
VOID Instruction_ilp_all_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
ADDRINT insAddr = INS_Address(ins);
ins_buffer_entry* e = findInsBufferEntry(insAddr);
instrument_ilp_all(ins, e);
}
}
VOID Fini_ilp_all_only(INT32 code, VOID* v){
fini_ilp_all(code, v);
}
/* ILP_ONE */
VOID Instruction_ilp_one_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
ADDRINT insAddr = INS_Address(ins);
ins_buffer_entry* e = findInsBufferEntry(insAddr);
instrument_ilp_one(ins, e);
}
}
VOID Fini_ilp_one_only(INT32 code, VOID* v){
fini_ilp_one(code, v);
}
/* ITYPES */
VOID Instruction_itypes_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
instrument_itypes(ins, v);
}
}
VOID Fini_itypes_only(INT32 code, VOID* v){
fini_itypes(code, v);
}
/* PPM */
VOID Instruction_ppm_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
instrument_ppm(ins, v);
}
}
VOID Fini_ppm_only(INT32 code, VOID* v){
fini_ppm(code, v);
}
/* REG */
VOID Instruction_reg_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
ADDRINT insAddr = INS_Address(ins);
ins_buffer_entry* e = findInsBufferEntry(insAddr);
instrument_reg(ins, e);
}
}
VOID Fini_reg_only(INT32 code, VOID* v){
fini_reg(code, v);
}
/* STRIDE */
VOID Instruction_stride_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
instrument_stride(ins, v);
}
}
VOID Fini_stride_only(INT32 code, VOID* v){
fini_stride(code, v);
}
/* MEMFOOTPRINT */
VOID Instruction_memfootprint_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
instrument_memfootprint(ins, v);
}
}
VOID Fini_memfootprint_only(INT32 code, VOID* v){
fini_memfootprint(code, v);
}
/* LINECOUNT */
VOID Instruction_linecount_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
instrument_linecount(ins, v);
}
}
VOID Fini_linecount_only(INT32 code, VOID* v){
fini_linecount(code, v);
}
/* MEMSTACKDIST */
VOID Instruction_memstackdist_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
instrument_memstackdist(ins, v);
}
}
VOID Fini_memstackdist_only(INT32 code, VOID* v){
fini_memstackdist(code, v);
}
/* FULLMEMSTACKDIST */
VOID Instruction_fullmemstackdist_only(INS ins, VOID* v){
instrument_start_monitoring(ins, v);
if (start_monitoring) {
instrument_instruction_count(ins, v);
instrument_fullmemstackdist(ins, v);
}
}
VOID Fini_fullmemstackdist_only(INT32 code, VOID* v){
fini_fullmemstackdist(code, v);
}
/* MY TYPE */
VOID Instruction_custom(INS ins, VOID* v){
instrument_instruction_count(ins, v);
cerr << "Please choose a subset of characteristics you want to use, and remove this message (along with the exit call)" << endl;
exit(1);
// Choose subset of characteristics, and make the same adjustments in Fini_custom and init_custom below
//ADDRINT insAddr = INS_Address(ins);
//ins_buffer_entry* e = findInsBufferEntry(insAddr);
//instrument_ilp_all(ins, e);
//instrument_ilp_one(ins, e);
//instrument_itypes(ins, v);
//instrument_ppm(ins, v);
//instrument_reg(ins, e);
//instrument_stride(ins, v);
//instrument_memfootprint(ins, v);
//instrument_memstackdist(ins, v);
}
VOID Fini_custom(INT32 code, VOID* v){
//fini_ilp_all(code, v);
//fini_ilp_one(code, v);
//fini_itypes(code, v);
//fini_ppm(code, v);
//fini_reg(code, v);
//fini_stride(code, v);
//fini_memfootprint(code, v);
//fini_memstackdist(code, v);
}
void init_custom(){
//init_ilp_all();
//init_ilp_one();
//init_itypes();
//init_ppm();
//init_reg();
//init_stride();
//init_memfootprint();
//init_memstackdist();
}
VOID ThreadStart(THREADID id, CONTEXT *context, INT32 flags, VOID *data)
{
if (__sync_fetch_and_add(&thread_count, 1))
{
LOG_MSG("WARNING: Thread creation detected, results can be corrupted!\n");
WARNING_MSG("Thread creation detected, results can be corrupted!");
}
}
/************
* MAIN *
************/
int main(int argc, char* argv[]){
int i;
MODE mode;
setup_mica_log(&log);
read_config(&log, &interval_size, &mode, &_ilp_win_size, &_block_size, &_page_size, &_itypes_spec_file, &append_pid, &use_magic_instruction);
start_monitoring = !use_magic_instruction;
cerr << "interval_size: " << interval_size << ", mode: " << mode << endl;
interval_ins_count = 0;
interval_ins_count_for_hpc_alignment = 0;
total_ins_count = 0;
total_ins_count_for_hpc_alignment = 0;
for(i=0; i < MAX_MEM_TABLE_ENTRIES; i++){
ins_buffer[i] = (ins_buffer_entry*)NULL;
}
switch(mode){
case MODE_ALL:
init_all();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_all, 0);
PIN_AddFiniFunction(Fini_all, 0);
break;
case MODE_ILP:
init_ilp_all();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_ilp_all_only, 0);
PIN_AddFiniFunction(Fini_ilp_all_only, 0);
break;
case MODE_ILP_ONE:
init_ilp_one();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_ilp_one_only, 0);
PIN_AddFiniFunction(Fini_ilp_one_only, 0);
break;
case MODE_ITYPES:
init_itypes();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_itypes_only, 0);
PIN_AddFiniFunction(Fini_itypes_only, 0);
break;
case MODE_PPM:
init_ppm();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_ppm_only, 0);
PIN_AddFiniFunction(Fini_ppm_only, 0);
break;
case MODE_REG:
init_reg();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_reg_only, 0);
PIN_AddFiniFunction(Fini_reg_only, 0);
break;
case MODE_STRIDE:
init_stride();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_stride_only, 0);
PIN_AddFiniFunction(Fini_stride_only, 0);
break;
case MODE_MEMFOOTPRINT:
init_memfootprint();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_memfootprint_only, 0);
PIN_AddFiniFunction(Fini_memfootprint_only, 0);
break;
case MODE_LINECOUNT:
init_linecount();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_linecount_only, 0);
PIN_AddFiniFunction(Fini_linecount_only, 0);
break;
case MODE_MEMSTACKDIST:
init_memstackdist();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_memstackdist_only, 0);
PIN_AddFiniFunction(Fini_memstackdist_only, 0);
break;
case MODE_FULLMEMSTACKDIST:
init_fullmemstackdist();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_fullmemstackdist_only, 0);
PIN_AddFiniFunction(Fini_fullmemstackdist_only, 0);
break;
case MODE_CUSTOM:
init_custom();
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction_custom, 0);
PIN_AddFiniFunction(Fini_custom, 0);
break;
default:
cerr << "FATAL ERROR: Unknown mode while trying to allocate memory for Pin tool!" << endl;
log << "FATAL ERROR: Unknown mode while trying to allocate memory for Pin tool!" << endl;
exit(1);
}
// The tool does not handle multithreaded programs.
// Since results might be bogus, we print a warning
// when presence of multiple threads is detected by PIN.
PIN_AddThreadStartFunction(ThreadStart, NULL);
// starts program, never returns
PIN_StartProgram();
}