-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_pred_mt.cpp
338 lines (290 loc) · 11.2 KB
/
all_pred_mt.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
//
// cond_pin.cpp
//
//
// Created by Surya Narayanan on 20/09/12.
// Copyright 2012 __MyCompanyName__. All rights reserved.
//
#include <iostream>
#include <fstream>
#include "pin.H"
#include <map>
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>
#include "instlib.H"
#define INCLUDEPRED
#include "all_pred.h"
#include "predictor.h"
#include "tour.h"
using namespace std;
//map<string, string> InstAndCat;
// uop type encoding
/*
#define IS_BR_CONDITIONAL (1 << 0) // conditional branch
#define IS_BR_INDIRECT (1 << 1) // indirect branch
#define IS_BR_CALL (1 << 2) // call
*/
#define MAX_THREAD 4
//structure to compute result
ofstream OutFile;
struct br_pre
{
INT64 total_br;
INT64 mispredict;
INT64 tmispredict;
INT64 predict;
INT64 tpredict;
bool gpred;
bool tpred;
my_predictor mypred;
my_predictor_1 mypred_1;
//my_predictor_2 mypred_2;
}pred[MAX_THREAD];
//structure to store the per-branch result
typedef struct branch_data {
INT tid;
INT64 pc;
INT64 total_ind_br;
INT64 pred_ind_br;
INT64 mispred_ind_br;
string fn_name_br;
}ind_br;
map<INT64,ind_br>branch;
PIN_LOCK lock;
INT32 numThreads = 0;
static TLS_KEY tls_key;
class thread_data_t
{
public:
thread_data_t() : _count(0) {}
UINT64 _count;
// UINT8 _pad[PADSIZE];
};
thread_data_t* get_tls(THREADID threadid)
{
thread_data_t* tdata =
static_cast<thread_data_t*>(PIN_GetThreadData(tls_key, threadid));
return tdata;
}
VOID ThreadStart(THREADID threadid, CONTEXT *ctxt, INT32 flags, VOID *v)
{
GetLock(&lock, threadid+1);
numThreads++;
cout << " Starting thread " << threadid << endl;
ReleaseLock(&lock);
thread_data_t* tdata = new thread_data_t;
PIN_SetThreadData(tls_key, tdata, threadid);
}
typedef struct inputs {
INT tid;
INT64 pc;
INT type;
bool taken;
INT64 target;
struct inputs *next;
}in;
int x,y,total_br,mispredict,predict = 0;
in *head = NULL;
static my_predictor *mypred;
VOID collect (ADDRINT add_pc, bool tk, ADDRINT target_add, UINT32 value,THREADID th_id)
{
GetLock(&lock, th_id+1);
in *root;
root = new in;
root -> tid = th_id;
root->pc = add_pc;
root->type = value;
root->taken =tk;
root->target = target_add;
root->next=head;
head = root;
ReleaseLock(&lock);
// cout << ++y << " address :" << hex << add_pc << " taken: " << tk << " target :" << hex << target_add << endl;
}
VOID docount(THREADID threadid)
{
//icount++;
//cout << "in docount" << endl;
thread_data_t* tdata = get_tls(threadid);
//tdata[tid]->_count++;
tdata->_count++;
}
// Pin calls this function every time a new instruction is encountered
VOID Instruction(INS ins, VOID *v)
{
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)docount,IARG_THREAD_ID, IARG_END);
if (INS_Category(ins) == XED_CATEGORY_COND_BR)
// if(INS_IsBranchOrCall(ins))
{
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(collect),IARG_INST_PTR, IARG_BRANCH_TAKEN, IARG_BRANCH_TARGET_ADDR,IARG_UINT32,IS_BR_CONDITIONAL,IARG_THREAD_ID,IARG_END);
}
else if ( INS_Category(ins) == XED_CATEGORY_UNCOND_BR)
// else if (INS_IsIndirectBranchOrCall(ins))
{
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(collect),IARG_INST_PTR, IARG_BRANCH_TAKEN, IARG_BRANCH_TARGET_ADDR,IARG_UINT32, IS_BR_INDIRECT,IARG_THREAD_ID,IARG_END);
}
else if ( INS_Category(ins) == XED_CATEGORY_CALL || INS_Category(ins) == XED_CATEGORY_RET)
// else if (INS_IsRet(ins))
{
INS_InsertCall(ins, IPOINT_BEFORE, AFUNPTR(collect),IARG_INST_PTR, IARG_BRANCH_TAKEN, IARG_BRANCH_TARGET_ADDR,IARG_UINT32, IS_BR_CALL,IARG_THREAD_ID,IARG_END);
}
}
KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool",
"o", "inscategory.out", "specify output file name");
// This function is called when the application exits
VOID Fini(INT32 code, VOID *v)
{
cout << "processing data... " << endl;
OutFile.flush();
// Write to a file since cout and cerr maybe closed by the application
char br_taken;
int bt,bnt=0;
OutFile.setf(ios::showbase);
//OutFile << "Instruction Category Extension" << endl;
// map<string, string>::iterator it;
// for (it = InstAndCat.begin(); it != InstAndCat.end(); it++) {
// OutFile << it->first << " " << it->second << endl;
// }
in *temp;
temp = head;
while (temp!=NULL)
{
if ( temp->type == IS_BR_CONDITIONAL)
{//cout << "saw cond_br " << endl;
branch[temp->pc].tid = temp->tid;
branch[temp->pc].pc = temp->pc;
branch[temp->pc].total_ind_br++;
branch[temp->pc].fn_name_br=RTN_FindNameByAddress(temp->pc);
pred[temp->tid].total_br++;
if (temp->taken == 0)
{ //cout << "not taken" << endl;
br_taken='N';
bnt++;
}
else {
br_taken='T';
bt++;
// cout << "taken" << endl;
}
// One bit predictor
// cout << " 1 bit " << endl;
pred[temp->tid].mypred_1.bimodal1bit1024 += pred[temp->tid].mypred_1.predictor1bit(pred[temp->tid].mypred_1.table1024, temp->pc % 1024, br_taken);
//Two bit predictor
// cout << " 2 bit " << endl;
pred[temp->tid].mypred_1.bimodal2bit1024 += pred[temp->tid].mypred_1.predictor2bit(pred[temp->tid].mypred_1.table1024, temp->pc % BR_SIZE, br_taken);
int index = temp->pc % BR_SIZE;
//global vs local predictor
/*pred[temp->tid].tpred = pred[temp->tid].mypred_2.make_prediction (temp->pc);
if (pred[temp->tid].tpred == temp->taken)
{ pred[temp->tid].tpredict++;
}
else
{ pred[temp->tid].tmispredict++;
}
*/
//tournament predictor
// cout << " tour " << endl;
pred[temp->tid].mypred_1.tCounter += pred[temp->tid].mypred_1.tournamentPred(pred[temp->tid].mypred_1.gtable[8][index], pred[temp->tid].mypred_1.table1024[index] % 4, pred[temp->tid].mypred_1.gtable[8][index ^ pred[temp->tid].mypred_1.globalReg & (BR_SIZE-1)] % 4, br_taken);
// Gshare Predictor (2 - 10 bits global register)
for (int i = 2; i <= 10; i++) {
int cutOff = 1 << i; //Use to obtain bits from global register
pred[temp->tid].mypred_1.gCounter[i - 2] += pred[temp->tid].mypred_1.predictor2bit(pred[temp->tid].mypred_1.gtable[i - 2], (index) ^ (pred[temp->tid].mypred_1.globalReg % cutOff), br_taken);
}
pred[temp->tid].mypred_1.globalReg = pred[temp->tid].mypred_1.globalReg << 1;
if (br_taken == 'T') {
pred[temp->tid].mypred_1.alwaysT++;
pred[temp->tid].mypred_1.globalReg++;
} else {
pred[temp->tid].mypred_1.alwaysNT++;
}
pred[temp->tid].mypred_1.globalReg &= (BR_SIZE-1); //keep global register to 10 bits
//TAGE
// cout << " TAGE " << endl;
pred[temp->tid].gpred = pred[temp->tid].mypred.predict_brcond (temp->pc,temp->type );
//mypred->FetchHistoryUpdate (temp->tid & 0x3ffff, temp->pc ,temp->type,temp->taken & 0x7f );
if (pred[temp->tid].gpred == temp->taken)
{ pred[temp->tid].predict++;
branch[temp->pc].pred_ind_br++;
}
else
{ pred[temp->tid].mispredict++;
branch[temp->pc].mispred_ind_br++;
}
}
else
{
// pred[temp->tid].total_br++;
// pred[temp->tid].predict++;
}
pred[temp->tid].mypred.FetchHistoryUpdate (temp->pc, temp->type ,temp->taken,temp->target);
pred[temp->tid].mypred.update_brcond (temp->pc, temp->type ,temp->taken,temp->target);
// }
temp = temp ->next;
}
cout.setf(ios::fixed, ios::floatfield);
cout.setf(ios::showpoint);
/*//cout << " TID :" << temp->tid << " address :" << hex << temp -> pc << " taken: " << temp->taken << " target :" << hex << temp -> target << endl;
OutFile << " " << temp->tid << " " << temp -> pc << " " <<temp->type << " " << temp->taken << " " << temp -> target << endl;
temp = temp ->next;
// cout << "total_br :" << total_br << " predict :" <<predict << " misprecdict :" << mispredict << endl;
*/
map<INT64, ind_br>::iterator it;
/* for (it = branch.begin(); it != branch.end(); it++) {
OutFile << hex << it->first << " " << it -> second.tid << " " << dec << it ->second.total_ind_br <<" "<<it->second.pred_ind_br <<" " << it-> second.mispred_ind_br << " " << it -> second.fn_name_br << endl;
}*/
//for (x=0;x<=MAX_THREAD;x++)
cout << "done" << endl;
OutFile << " I_count br_count 1bit 2bit tour TAGE " << endl;
for (x=0;x<=2;x++)
{
thread_data_t* tdata = get_tls(x);
//double pre=(double) (pred[x].predict)/pred[x].total_br*100;
//double misp=(double) (pred[x].mispredict)/pred[x].total_br*100;
// OutFile << "1-bit Instructions " << tdata->_count << " total_br_" <<x<<" :" << pred[x].total_br << " predict_" <<x<<" :" <<pred[x].mypred_1.bimodal1bit1024 << endl;
// OutFile << "2-bit Instructions " << tdata->_count << " total_br_" <<x<<" :" << pred[x].total_br << " predict_" <<x<<" :" <<pred[x].mypred_1.bimodal2bit1024 << endl;
// OutFile << "global Instructions " << tdata->_count << " total_br_" <<x<<" :" << pred[x].total_br << " predict_" <<x<<" :" <<pred[x].tpredict << endl;
// OutFile << "TOUR Instructions " << tdata->_count << " total_br_" <<x<<" :" << pred[x].total_br << " predict_" <<x<<" :" << pred[x].mypred_1.tCounter << endl;
// OutFile << "TAGE Instructions " << tdata->_count << " total_br_" <<x<<" :" << pred[x].total_br << " predict_" <<x<<" :" <<pred[x].predict << endl;
//cout << "total_br_" <<x<<" :" << pred[x].total_br << " predict_" <<x<<" :" <<pred[x].predict << " misprecdict_" <<x<<" :" << pred[x].mispredict <<" % Predict :" << pre << " % Mispredict :" << misp << endl;
OutFile << tdata->_count << " " << pred[x].total_br << " " << pred[x].mypred_1.bimodal1bit1024 << " " << pred[x].mypred_1.bimodal2bit1024 << " " << pred[x].mypred_1.tCounter << " " << pred[x].predict << endl;
// cout << "br_count " << pred[x].mypred_1.iCount << endl;
}
OutFile.flush();
OutFile.close();
}
/* ===================================================================== */
/* Print Help Message */
/* ===================================================================== */
INT32 Usage()
{
cerr << "This tool gets the category and extension of each instruction executed" << endl;
cerr << endl << KNOB_BASE::StringKnobSummary() << endl;
return -1;
}
/* ===================================================================== */
/* Main */
/* ===================================================================== */
/* argc, argv are the entire command line: pin -t <toolname> -- ... */
/* ===================================================================== */
int main(int argc, char * argv[])
{
mypred = new my_predictor();
// Initialize pin
if (PIN_Init(argc, argv)) return Usage();
PIN_InitSymbols();
// Initialize the lock
OutFile.open(KnobOutputFile.Value().c_str());
InitLock(&lock);
// Obtain a key for TLS storage.
tls_key = PIN_CreateThreadDataKey(0);
// Register ThreadStart to be called when a thread starts.
PIN_AddThreadStartFunction(ThreadStart, 0);
// Register Instruction to be called to instrument instructions
INS_AddInstrumentFunction(Instruction, 0);
// Register Fini to be called when the application exits
PIN_AddFiniFunction(Fini, 0);
// Start the program, never returns
PIN_StartProgram();
return 0;
}