forked from lanl/gs_patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gs_patterns_core.cpp
364 lines (296 loc) · 12 KB
/
gs_patterns_core.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
#include <assert.h> /// TODO: use cassert instead
#include <math.h>
#include <string>
#include <sstream>
#include "utils.h"
#include "gs_patterns.h"
namespace gs_patterns
{
namespace gs_patterns_core
{
using namespace gs_patterns;
void translate_iaddr(const std::string & binary, char * source_line, addr_t iaddr)
{
char path[MAX_LINE_LENGTH];
char cmd[MAX_LINE_LENGTH];
FILE *fp;
sprintf(cmd, "addr2line -e %s 0x%lx", binary.c_str(), iaddr);
/* Open the command for reading. */
fp = popen(cmd, "r");
if (NULL == fp) {
throw GSError("Failed to run command");
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path), fp) != NULL) {
strcpy(source_line, path);
source_line[strcspn(source_line, "\n")] = 0;
}
/* close */
pclose(fp);
return;
}
void create_metrics_file(FILE * fp, FILE * fp2, const std::string & file_prefix, Metrics & target_metrics, bool & first_spatter)
{
int i = 0;
int j = 0;
//Create stride histogram and create spatter
int sidx;
int firstgs = 1;
int unique_strides;
int64_t hbin = 0;
int64_t n_stride[OBOUNDS_ALLOC];
double outbounds;
if (file_prefix.empty()) throw GSFileError ("Empty file prefix provided.");
if (first_spatter) printf("\n");
printf("\n");
for (i = 0; i < target_metrics.ntop; i++) {
printf("***************************************************************************************\n");
unique_strides = 0;
for (j = 0; j < OBOUNDS_ALLOC; j++)
n_stride[j] = 0;
for (j = 1; j < target_metrics.offset[i]; j++) {
sidx = target_metrics.patterns[i][j] - target_metrics.patterns[i][j - 1] + OBOUNDS + 1;
sidx = (sidx < 1) ? 0 : sidx;
sidx = (sidx > OBOUNDS_ALLOC - 1) ? OBOUNDS_ALLOC - 1 : sidx;
n_stride[sidx]++;
}
for (j = 0; j < OBOUNDS_ALLOC; j++) {
if (n_stride[j] > 0) {
unique_strides++;
}
}
outbounds = (double) (n_stride[0] + n_stride[OBOUNDS_ALLOC-1]) / (double) target_metrics.offset[i];
if (((unique_strides > NSTRIDES) || (outbounds > OUTTHRESH) && (target_metrics.offset[i] > USTRIDES ) )) {
//if (true) {
if (firstgs) {
firstgs = 0;
printf("***************************************************************************************\n");
printf("%sS\n", target_metrics.type_as_string().c_str());
}
printf("***************************************************************************************\n");
//create a binary file
FILE * fp_bin;
char bin_name[1024];
sprintf(bin_name, "%s.%s.%03d.%02dB.sbin", file_prefix.c_str(), target_metrics.getShortNameLower().c_str(), \
i, target_metrics.size[i]);
printf("%s\n", bin_name);
//std::string bin_name = \
// file_prefix + "." + target_metrics.getShortNameLower().c_str() + "." + std::to_string(i) + "." + \
// std::to_string(target_metrics.size[i]) + "B.sbin";
fp_bin = fopen(bin_name, "w");
if (NULL == fp_bin)
throw GSFileError("Could not open " + std::string(bin_name) + "!");
printf("%sIADDR -- %p\n", target_metrics.getShortName().c_str(), (void*) target_metrics.top[i]);
printf("SRCLINE -- %s\n", target_metrics.get_srcline()[target_metrics.top_idx[i]]);
printf("GATHER %c -- %6.3f%c (%4ld-bit chunks)\n",
'%', 100.0 * (double) target_metrics.tot[i] / target_metrics.cnt, '%', VBITS);
printf("DTYPE -- %d bytes\n", target_metrics.size[i]);
printf("NINDICES -- %d\n", target_metrics.offset[i]);
printf("INDICES:\n");
int64_t nlcnt = 0;
for (j = 0; j < target_metrics.offset[i]; j++) {
if (j <= 49) {
printf("%10ld ", target_metrics.patterns[i][j]);
fflush(stdout);
if (( ++nlcnt % 10) == 0)
printf("\n");
} else if (j >= (target_metrics.offset[i] - 50)) {
printf("%10ld ", target_metrics.patterns[i][j]);
fflush(stdout);
if (( ++nlcnt % 10) == 0)
printf("\n");
} else if (j == 50)
printf("...\n");
}
printf("\n");
printf("DIST HISTOGRAM --\n");
hbin = 0;
for(j=0; j<OBOUNDS_ALLOC; j++) {
if (j == 0) {
printf("( -inf, %5ld]: %ld\n", (int64_t)(-(VBITS+1)), n_stride[j]);
hbin = 0;
} else if (j == OBOUNDS +1) {
printf("[%5ld, 0): %ld\n", (int64_t)-VBITS, hbin);
hbin = 0;
} else if (j == (OBOUNDS_ALLOC-2) ) {
printf("[ 0, %5ld]: %ld\n", VBITS, hbin);
hbin = 0;
} else if (j == (OBOUNDS_ALLOC-1)) {
printf("[%5ld, inf): %ld\n", VBITS+1, n_stride[j]);
} else {
hbin += n_stride[j];
}
}
if (first_spatter) {
first_spatter = false;
fprintf(fp, " {\"kernel\":\"%s\", \"pattern\":[", target_metrics.getName().c_str());
} else {
fprintf(fp, ",\n {\"kernel\":\"%s\", \"pattern\":[", target_metrics.getName().c_str());
}
fwrite(target_metrics.patterns[i], sizeof(uint64_t), target_metrics.offset[i], fp_bin);
fclose(fp_bin);
for (j = 0; j < target_metrics.offset[i] - 1; j++)
fprintf(fp, "%ld,", target_metrics.patterns[i][j]);
fprintf(fp, "%ld", target_metrics.patterns[i][target_metrics.offset[i] - 1]);
fprintf(fp, "], \"count\":1}");
fprintf(fp2, "0x%lx,%s,%d,%s,%d,%6.3f\n",
target_metrics.top[i],
target_metrics.get_srcline()[target_metrics.top_idx[i]],
target_metrics.size[i],
target_metrics.getShortName().c_str(),
target_metrics.offset[i],
100.0 * (double) target_metrics.tot[i] / target_metrics.cnt);
}
printf("***************************************************************************************\n\n");
}
}
void normalize_stats(Metrics & target_metrics)
{
//Normalize
int64_t smallest;
for (int i = 0; i < target_metrics.ntop; i++) {
//Find smallest
smallest = 0x7FFFFFFFFFFFFFFFL;
for (int j = 0; j < target_metrics.offset[i]; j++) {
if (target_metrics.patterns[i][j] < smallest)
smallest = target_metrics.patterns[i][j];
}
//Normalize
for (int j = 0; j < target_metrics.offset[i]; j++) {
target_metrics.patterns[i][j] -= smallest;
}
}
}
int get_top_target(InstrInfo & target_iinfo, Metrics & target_metrics)
{
int target_ntop = 0;
for (int j = 0; j < NTOP; j++)
{
int bestcnt = 0;
addr_t best_iaddr = 0;
int bestidx = -1;
for (int k = 0; k < NGS; k++)
{
if (target_iinfo.get_icnt()[k] == 0)
continue;
if (target_iinfo.get_iaddrs()[k] == 0) {
break;
}
if (target_iinfo.get_icnt()[k] > bestcnt) {
bestcnt = target_iinfo.get_icnt()[k];
best_iaddr = target_iinfo.get_iaddrs()[k];
bestidx = k;
}
}
if (best_iaddr == 0)
{
break;
}
else
{
target_ntop++;
target_metrics.top[j] = best_iaddr;
target_metrics.top_idx[j] = bestidx;
target_metrics.tot[j] = target_iinfo.get_icnt()[bestidx];
target_iinfo.get_icnt()[bestidx] = 0;
//printf("%sIADDR -- %016lx: %16lu -- %s\n", target_metrics.getShortName().c_str(), target_metrics.top[j], target_metrics.tot[j], target_metrics.get_srcline()[bestidx]);
}
} // for
return target_ntop;
}
bool handle_2nd_pass_trace_entry(const InstrAddrAdapter & ia,
Metrics & gather_metrics, Metrics & scatter_metrics,
addr_t & iaddr, int64_t & maddr, uint64_t & mcnt,
addr_t * gather_base, addr_t * scatter_base)
{
int iret = 0;
int i = 0;
bool breakout = false;
/*****************************/
/** INSTR 0xa-0x10 and 0x1e **/
/*****************************/
if (!ia.is_valid()) {
std::ostringstream os;
os << "Invalid " << ia;
throw GSDataError(os.str());
}
if (ia.is_other_instr())
{
iaddr = ia.get_iaddr(); // was get_address in orig code -> get_iaddr()
}
else if (ia.is_mem_instr())
{
/***********************/
/** MEM **/
/***********************/
maddr = ia.get_maddr();
if (CTA == ia.get_mem_instr_type() && ia.get_address() == ia.get_base_addr()) {
iaddr = ia.get_iaddr();
}
if ((++mcnt % PERSAMPLE) == 0) {
printf(".");
fflush(stdout);
}
// gather ?
if (GATHER == ia.get_mem_access_type())
{
for (i = 0; i < gather_metrics.ntop; i++)
{
//found it
if (iaddr == gather_metrics.top[i])
{
gather_metrics.size[i] = ia.get_size();
if (gather_base[i] == 0)
gather_base[i] = maddr;
//Add index
if (gather_metrics.offset[i] >= gather_metrics.get_pattern_size(i)) {
if (!gather_metrics.grow(i)) {
printf("WARNING: Unable to increase PSIZE. Truncating trace...\n");
breakout = true;
break;
}
}
gather_metrics.patterns[i][gather_metrics.offset[i]++] = (int64_t) (maddr - gather_base[i]);
break;
}
}
}
// scatter ?
else if (SCATTER == ia.get_mem_access_type())
{
for (i = 0; i < scatter_metrics.ntop; i++)
{
//found it
if (iaddr == scatter_metrics.top[i])
{
scatter_metrics.size[i] = ia.get_size();
//set base
if (scatter_base[i] == 0)
scatter_base[i] = maddr;
//Add index
if (scatter_metrics.offset[i] >= scatter_metrics.get_pattern_size(i)) {
if (!scatter_metrics.grow(i)) {
printf("WARNING: Unable to increase PSIZE. Truncating trace...\n");
breakout = true;
break;
}
}
scatter_metrics.patterns[i][scatter_metrics.offset[i]++] = (int64_t) (maddr - scatter_base[i]);
break;
}
}
}
else
{ // belt and suspenders, yep = but helps to validate correct logic in children of InstrAddresInfo
throw GSDataError("Unknown Memory Access Type: " + std::to_string(ia.get_mem_access_type()));
}
} // MEM
return breakout;
}
} // namespace gs_patterns_core
std::ostream & operator<<(std::ostream & os, const gs_patterns::InstrAddrAdapter & ia)
{
ia.output(os);
return os;
}
} // namespace gs_patterns