-
Notifications
You must be signed in to change notification settings - Fork 10
/
profiler.cpp
315 lines (261 loc) · 11 KB
/
profiler.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
/**
* Copyright (C) 2018 Dean De Leo, email: dleo[at]cwi.nl
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "profiler.hpp"
#include <cassert>
#include <cstring>
#include <iostream> // debug only
#if defined(HAVE_LIBPAPI)
#include <papi.h>
#endif
#define ERROR(msg) RAISE_EXCEPTION(Exception, msg)
using namespace details_profiler;
using namespace std;
/*****************************************************************************
* *
* BaseProfiler *
* *
*****************************************************************************/
namespace details_profiler {
// thread unsafe
bool BaseProfiler::library_initialised = false;
void BaseProfiler::initialise_library(){
if(library_initialised) return; // already initialised
#if !defined(HAVE_LIBPAPI)
ERROR("Library PAPI not available. Configure the program with the support for this library");
#else
int rc = PAPI_library_init(PAPI_VER_CURRENT);
if (rc != PAPI_VER_CURRENT){ ERROR("Library PAPI version mismatch"); }
library_initialised = true;
#endif
}
BaseProfiler::BaseProfiler() { initialise_library(); }
int BaseProfiler::get_event_code(const char* event_name){
#if defined(HAVE_LIBPAPI)
if(!library_initialised) ERROR("Library PAPI not initialised");
// PAPI_event_name_to_code doesn't accept a const char* argument
char buffer[PAPI_MAX_STR_LEN];
strncpy(buffer, event_name, PAPI_MAX_STR_LEN);
buffer[PAPI_MAX_STR_LEN -1] = '\0';
int event_code { 0 };
int rc = PAPI_event_name_to_code(buffer, &event_code);
if(rc != PAPI_OK){ return -1; }
PAPI_event_info_t event_info;
rc = PAPI_get_event_info(event_code, &event_info);
if(rc != PAPI_OK){ return -1; }
if(IS_PRESET(event_info.event_code)){
// check whether there exist some derivations to infer this event
if(event_info.count > 0){
return event_code;
} else {
return -1;
}
} else { // native event
return event_code;
}
#else
return -1;
#endif
}
/*****************************************************************************
* *
* GenericProfiler *
* *
*****************************************************************************/
GenericProfiler::GenericProfiler(){ }
GenericProfiler::~GenericProfiler(){
#if defined(HAVE_LIBPAPI)
if(m_event_set != PAPI_NULL)
unregister_events();
#endif
}
void GenericProfiler::add_events(const char* errorstring, const char* event_name){
add_events(errorstring, &event_name, 1);
}
void GenericProfiler::add_events(const char* errorstring, const char* alternative_events[], size_t num_alternative_events){
#if defined(HAVE_LIBPAPI)
if (m_events_sz >= m_events_capacity) ERROR("No space left to add the events: " << m_events_sz);
int rc = -1;
size_t i = 0;
while(rc == -1 && i < num_alternative_events){
rc = get_event_code(alternative_events[i]);
// add the event code && stop the execution
if(rc != -1){
m_events[m_events_sz] = rc;
m_events_sz++;
}
i++;
}
if(rc == -1) {
cerr << "[" __FILE__ << ":" << __LINE__ << "] " << errorstring << endl;
ERROR(errorstring);
}
#endif
}
void GenericProfiler::register_events(){
#if defined(HAVE_LIBPAPI)
int rc {0};
m_event_set = PAPI_NULL;
rc = PAPI_create_eventset(&m_event_set);
if(rc != PAPI_OK) {
cerr << "[" __FILE__ << ":" << __LINE__ << "] PAPI_create_eventset: " << PAPI_strerror(rc) << " (" << rc << ")" << endl;
ERROR("Cannot create the event set (opaque object identifier for the PAPI library)");
}
rc = PAPI_add_events(m_event_set, m_events, m_events_sz);
if(rc != PAPI_OK) {
cerr << "[" __FILE__ << ":" << __LINE__ << "] PAPI_add_events: " << PAPI_strerror(rc) << " (" << rc << ")" << endl;
ERROR("Cannot trace the interested set of events in this architecture");
}
#endif
}
void GenericProfiler::unregister_events(){
#if defined(HAVE_LIBPAPI)
int event_state = 0;
int rc = PAPI_state(m_event_set, &event_state);
if(rc == PAPI_OK && !(event_state & PAPI_STOPPED)){
PAPI_stop(m_event_set, nullptr); // ignore rc
}
rc = PAPI_remove_events(m_event_set, m_events, m_events_sz);
if(rc != PAPI_OK)
cerr << "[" __FILE__ << ":" << __LINE__ << "] PAPI_remove_events: " << PAPI_strerror(rc) << " (" << rc << ")" << endl;
rc = PAPI_destroy_eventset(&m_event_set);
if(rc != PAPI_OK)
cerr << "[" __FILE__ << ":" << __LINE__ << "] PAPI_destroy_eventset: " << PAPI_strerror(rc) << " (" << rc << ")" << endl;
#endif
}
void GenericProfiler::start(){
#if defined(HAVE_LIBPAPI)
int rc = PAPI_start(m_event_set);
if(rc != PAPI_OK){
ERROR("[GenericProfiler::start] Cannot start the event set: " << PAPI_strerror(rc) << "(rc: " << rc << ")");
}
#endif
}
void GenericProfiler::stop(long long* resultset){
#if defined(HAVE_LIBPAPI)
int rc = PAPI_stop(m_event_set, (long long*) resultset);
if(rc != PAPI_OK){
ERROR("[GenericProfiler::stop] Cannot stop the event set: " << PAPI_strerror(rc) << "(rc: " << rc << ")");
}
#endif
}
void GenericProfiler::snapshot(long long* resultset){
#if defined(HAVE_LIBPAPI)
int rc = PAPI_accum(m_event_set, resultset);
if(rc != PAPI_OK){
ERROR("[GenericProfiler::snapshot] Cannot obtain a snapshot from the event set: " << PAPI_strerror(rc) << "(rc: " << rc << ")");
}
#endif
}
} // namespace details_profiler
/*****************************************************************************
* *
* CachesProfiler *
* *
*****************************************************************************/
CachesProfiler::CachesProfiler() {
add_events("Cannot infer cache-1 faults", "PAPI_L1_DCM");
// on my damn AMD box L3 events are uncore :/
const char* LLC_events[] = {"PAPI_L3_DCM", "PAPI_L3_TCM", "LLC-LOAD-MISSES"};
add_events("Cannot infer cache-3 faults", LLC_events, 3);
const char* TLB_events[] = {"PAPI_TLB_DM", "PAPI_TLB_TM"};
add_events("Cannot infer TLB misses", TLB_events, 2);
register_events();
}
CachesProfiler::~CachesProfiler() { }
void CachesProfiler::start(){
GenericProfiler::start();
}
CachesSnapshot CachesProfiler::snapshot(){
#if defined(HAVE_LIBPAPI)
static_assert((sizeof(long long) * 3) == sizeof(m_current_snapshot), "Size mismatch, need to pass an array of types `long long'");
GenericProfiler::snapshot((long long*) &m_current_snapshot);
return m_current_snapshot;
#else
return CachesSnapshot{};
#endif
}
CachesSnapshot CachesProfiler::stop(){
CachesSnapshot m_result;
#if defined(HAVE_LIBPAPI)
static_assert(sizeof(long long) *3 == sizeof(m_result), "Size mismatch, need to pass an array of types `long long'");
GenericProfiler::stop((long long*) &m_result);
m_result += m_current_snapshot;
m_current_snapshot = {0,0,0};
#endif
return m_result;
}
void CachesSnapshot::operator+=(CachesSnapshot snapshot){
m_cache_l1_misses += snapshot.m_cache_l1_misses;
m_cache_llc_misses += snapshot.m_cache_llc_misses;
m_cache_tlb_misses += snapshot.m_cache_tlb_misses;
}
std::ostream& operator<<(std::ostream& out, const CachesSnapshot& snapshot){
out << "L1 faults: " << snapshot.m_cache_l1_misses << ", " <<
"LLC faults: " << snapshot.m_cache_llc_misses << ", " <<
"TLB faults: " << snapshot.m_cache_tlb_misses;
return out;
}
/*****************************************************************************
* *
* BranchMispredictionsProfiler *
* *
*****************************************************************************/
BranchMispredictionsProfiler::BranchMispredictionsProfiler() {
add_events("Cannot infer conditional branches", "PAPI_BR_CN");
add_events("Cannot infer branch mispredictions", "PAPI_BR_MSP");
// on my damn AMD box L3 events are uncore :/
add_events("Cannot infer cache-1 faults", "PAPI_L1_DCM");
const char* LLC_events[] = {"PAPI_L3_DCM", "PAPI_L3_TCM", "LLC-LOAD-MISSES"};
add_events("Cannot infer cache-3 faults", LLC_events, 3);
register_events();
}
BranchMispredictionsProfiler::~BranchMispredictionsProfiler() { }
void BranchMispredictionsProfiler::start(){
GenericProfiler::start();
}
BranchMispredictionsSnapshot BranchMispredictionsProfiler::snapshot(){
#if defined(HAVE_LIBPAPI)
static_assert((sizeof(long long) * 4) == sizeof(m_current_snapshot), "Size mismatch, need to pass an array of types `long long'");
GenericProfiler::snapshot((long long*) &m_current_snapshot);
return m_current_snapshot;
#else
return BranchMispredictionsSnapshot{};
#endif
}
BranchMispredictionsSnapshot BranchMispredictionsProfiler::stop(){
BranchMispredictionsSnapshot m_result;
#if defined(HAVE_LIBPAPI)
static_assert(sizeof(long long) *4 == sizeof(m_result), "Size mismatch, need to pass an array of types `long long'");
GenericProfiler::stop((long long*) &m_result);
m_result += m_current_snapshot;
m_current_snapshot = {0,0,0};
#endif
return m_result;
}
void BranchMispredictionsSnapshot::operator+=(BranchMispredictionsSnapshot snapshot){
m_conditional_branches += snapshot.m_conditional_branches;
m_branch_mispredictions += snapshot.m_branch_mispredictions;
m_cache_l1_misses += snapshot.m_cache_l1_misses;
m_cache_llc_misses += snapshot.m_cache_llc_misses;
}
std::ostream& operator<<(std::ostream& out, const BranchMispredictionsSnapshot& snapshot){
out << "Conditional branches: " << snapshot.m_conditional_branches << ", " <<
"Branch mispredictions: " << snapshot.m_branch_mispredictions << ", " <<
"L1 cache faults: " << snapshot.m_cache_l1_misses << ", " <<
"LLC cache faults: " << snapshot.m_cache_llc_misses;
return out;
}