-
Notifications
You must be signed in to change notification settings - Fork 6
/
papi_util.cpp
112 lines (87 loc) · 2.89 KB
/
papi_util.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
#include "microbench.h"
static constexpr int PAPI_CACHE_EVENT_COUNT = 3;
static constexpr int PAPI_INST_EVENT_COUNT = 4;
// This is only for low level initialization - do not call this
// if only counters are read or started
void InitPAPI() {
int retval;
retval = PAPI_library_init(PAPI_VER_CURRENT);
if(retval != PAPI_VER_CURRENT && retval > 0) {
fprintf(stderr,"PAPI library version mismatch!\n");
exit(1);
}
if (retval < 0) {
fprintf(stderr, "PAPI failed to start (1): %s\n", PAPI_strerror(retval));
exit(1);
}
retval = PAPI_is_initialized();
if (retval != PAPI_LOW_LEVEL_INITED) {
fprintf(stderr, "PAPI failed to start (2): %s\n", PAPI_strerror(retval));
exit(1);
}
return;
}
void InitInstMonitor() {
//InitPAPI();
return;
}
void StartInstMonitor() {
int events[PAPI_INST_EVENT_COUNT] = \
/*{PAPI_LD_INS, PAPI_SR_INS, */{PAPI_BR_INS, PAPI_TOT_CYC, PAPI_TOT_INS, PAPI_BR_MSP};
long long counters[PAPI_INST_EVENT_COUNT];
int retval;
if ((retval = PAPI_start_counters(events, PAPI_INST_EVENT_COUNT)) != PAPI_OK) {
fprintf(stderr, "PAPI failed to start counters: %s\n", PAPI_strerror(retval));
exit(1);
}
return;
}
void EndInstMonitor() {
long long counters[PAPI_INST_EVENT_COUNT];
int retval;
if ((retval = PAPI_stop_counters(counters, PAPI_INST_EVENT_COUNT)) != PAPI_OK) {
fprintf(stderr, "PAPI failed to stop counters: %s\n", PAPI_strerror(retval));
exit(1);
}
//std::cout << "Total load = " << counters[0] << "\n";
//std::cout << "Total store = " << counters[1] << "\n";
std::cout << "Total branch = " << counters[0] << "\n";
std::cout << "Total cycle = " << counters[1] << "\n";
std::cout << "Total instruction = " << counters[2] << "\n";
std::cout << "Total branch misprediction = " << counters[3] << "\n";
return;
}
/*
* StartCacheMonitor() - Uses PAPI Library to start monitoring L1, L2, L3 cache misses
*/
void StartCacheMonitor() {
int events[PAPI_CACHE_EVENT_COUNT] = \
{PAPI_L1_TCM, PAPI_L2_TCM, PAPI_L3_TCM};//, PAPI_LD_INS, PAPI_SR_INS};
long long counters[PAPI_CACHE_EVENT_COUNT];
int retval;
if ((retval = PAPI_start_counters(events, PAPI_CACHE_EVENT_COUNT)) != PAPI_OK) {
fprintf(stderr, "PAPI failed to start counters: %s\n", PAPI_strerror(retval));
exit(1);
}
return;
}
/*
* EndCacheMonitor() - Ends and prints PAPI result on cache misses
*/
void EndCacheMonitor() {
// We use this array to receive counter values
long long counters[PAPI_CACHE_EVENT_COUNT];
int retval;
if ((retval = PAPI_stop_counters(counters, PAPI_CACHE_EVENT_COUNT)) != PAPI_OK) {
fprintf(stderr, "PAPI failed to stop counters: %s\n", PAPI_strerror(retval));
exit(1);
}
std::cout << "L1 miss = " << counters[0] << "\n";
std::cout << "L2 miss = " << counters[1] << "\n";
std::cout << "L3 miss = " << counters[2] << "\n";
return;
}
void InitCacheMonitor() {
InitPAPI();
return;
}