-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprofile.hpp
331 lines (306 loc) · 10.4 KB
/
profile.hpp
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
#include "common/Compat.hpp"
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <string.h>
#include <string>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <thread>
#include <unistd.h>
#include <unordered_map>
#include <vector>
#ifdef __linux__
#include <asm/unistd.h>
#include <linux/perf_event.h>
extern "C" {
#include "jevents.h"
}
#endif
#define GLOBAL 1
extern bool writeHeader;
struct PerfEvents {
const size_t printFieldWidth = 10;
size_t counters;
#ifdef __linux__
struct read_format {
uint64_t value = 0; /* The value of the event */
uint64_t time_enabled = 0; /* if PERF_FORMAT_TOTAL_TIME_ENABLED */
uint64_t time_running = 0; /* if PERF_FORMAT_TOTAL_TIME_RUNNING */
uint64_t id = 0; /* if PERF_FORMAT_ID */
};
#endif
struct event {
#ifdef __linux__
struct perf_event_attr pe;
int fd;
read_format prev;
read_format data;
#endif
double readCounter() {
#ifdef __linux__
return (data.value - prev.value) *
(double)(data.time_enabled - prev.time_enabled) /
(data.time_running - prev.time_running);
#else
return 0;
#endif
}
};
std::unordered_map<std::string, std::vector<event>> events;
std::vector<std::string> ordered_names;
PerfEvents() {
if (GLOBAL)
counters = 1;
else {
counters = std::thread::hardware_concurrency();
}
#ifdef __linux__
char* cpustr = get_cpu_str();
std::string cpu(cpustr);
// see https://download.01.org/perfmon/mapfile.csv for cpu strings
if (cpu == "GenuineIntel-6-57-core") {
// Knights Landing
add("cycles", "cpu/cpu-cycles/");
add("LLC-misses", "cpu/cache-misses/");
add("l1-misses", "MEM_UOPS_RETIRED.L1_MISS_LOADS");
// e.add("l1-hits", "mem_load_retired.l1_hit");
add("stores", "MEM_UOPS_RETIRED.ALL_STORES");
add("loads", "MEM_UOPS_RETIRED.ALL_LOADS");
add("instr.", "instructions");
} else if (cpu == "GenuineIntel-6-55-core") {
// Skylake X
add("cycles", "cpu/cpu-cycles/");
add("LLC-misses", "cpu/cache-misses/");
add("LLC-misses2", "mem_load_retired.l3_miss");
add("l1-misses", PERF_TYPE_HW_CACHE,
PERF_COUNT_HW_CACHE_L1D | (PERF_COUNT_HW_CACHE_OP_READ << 8) |
(PERF_COUNT_HW_CACHE_RESULT_MISS << 16));
add("instr.", "instructions");
add("br. misses", "cpu/branch-misses/");
add("all_rd", "offcore_requests.all_data_rd");
add("br. misses", "cpu/branch-misses/");
add("stores", "mem_inst_retired.all_stores");
add("loads", "mem_inst_retired.all_loads");
add("mem_stall", "cycle_activity.stalls_mem_any");
//add("page-faults", "page-faults");
} else {
add("cycles", PERF_TYPE_HARDWARE, PERF_COUNT_HW_CPU_CYCLES);
add("LLC-misses", PERF_TYPE_HW_CACHE,
PERF_COUNT_HW_CACHE_LL | (PERF_COUNT_HW_CACHE_OP_READ << 8) |
(PERF_COUNT_HW_CACHE_RESULT_MISS << 16));
add("l1-misses", PERF_TYPE_HW_CACHE,
PERF_COUNT_HW_CACHE_L1D | (PERF_COUNT_HW_CACHE_OP_READ << 8) |
(PERF_COUNT_HW_CACHE_RESULT_MISS << 16));
add("l1-hits", PERF_TYPE_HW_CACHE,
PERF_COUNT_HW_CACHE_L1D | (PERF_COUNT_HW_CACHE_OP_READ << 8) |
(PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16));
add("stores", "cpu/mem-stores/");
add("loads", "cpu/mem-loads/");
add("instr.", PERF_TYPE_HARDWARE, PERF_COUNT_HW_INSTRUCTIONS);
add("br. misses", PERF_TYPE_HARDWARE, PERF_COUNT_HW_BRANCH_MISSES);
}
add("task-clock", PERF_TYPE_SOFTWARE, PERF_COUNT_SW_TASK_CLOCK);
#endif
registerAll();
}
void add(std::string name, uint64_t type, uint64_t eventID) {
if (getenv("EXTERNALPROFILE")) return;
#ifdef __linux__
ordered_names.push_back(name);
auto& eventsPerThread = events[name];
eventsPerThread.assign(counters, event());
for (auto& event : eventsPerThread) {
auto& pe = event.pe;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = type;
pe.size = sizeof(struct perf_event_attr);
pe.config = eventID;
pe.disabled = true;
pe.inherit = 1;
pe.inherit_stat = 0;
pe.exclude_kernel = true;
pe.exclude_hv = true;
pe.read_format =
PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
}
#else
compat::unused(name, type, eventID);
#endif
}
void add(std::string name, std::string str) {
if (getenv("EXTERNALPROFILE")) return;
#ifdef __linux__
ordered_names.push_back(name);
auto& eventsPerThread = events[name];
eventsPerThread.assign(counters, event());
for (auto& event : eventsPerThread) {
auto& pe = event.pe;
memset(&pe, 0, sizeof(struct perf_event_attr));
if (resolve_event(const_cast<char*>(str.c_str()), &pe) < 0)
std::cerr << "Error resolving perf event " << str << std::endl;
pe.disabled = true;
pe.inherit = 1;
pe.inherit_stat = 0;
pe.exclude_kernel = true;
pe.exclude_hv = true;
pe.read_format =
PERF_FORMAT_TOTAL_TIME_ENABLED | PERF_FORMAT_TOTAL_TIME_RUNNING;
}
#else
compat::unused(name, str);
#endif
}
void registerAll() {
for (auto& ev : events) {
size_t i = 0;
for (auto& event : ev.second) {
#ifdef __linux__
if (GLOBAL)
event.fd =
syscall(__NR_perf_event_open, &event.pe, 0, -1, -1, 0);
else
event.fd = syscall(__NR_perf_event_open, &event.pe, 0, i, -1, 0);
if (event.fd < 0)
std::cerr << "Error opening perf event " << ev.first
<< std::endl;
#else
compat::unused(event);
#endif
++i;
}
}
}
void startAll() {
for (auto& ev : events) {
for (auto& event : ev.second) {
#ifdef __linux__
ioctl(event.fd, PERF_EVENT_IOC_ENABLE, 0);
if (read(event.fd, &event.prev, sizeof(uint64_t) * 3) !=
sizeof(uint64_t) * 3)
std::cerr << "Error reading counter " << ev.first << std::endl;
#else
compat::unused(event);
#endif
}
}
}
~PerfEvents() {
for (auto& ev : events)
for (auto& event : ev.second)
#ifdef __linux__
close(event.fd);
#else
compat::unused(event);
#endif
}
void readAll() {
for (auto& ev : events)
for (auto& event : ev.second) {
#ifdef __linux__
if (read(event.fd, &event.data, sizeof(uint64_t) * 3) !=
sizeof(uint64_t) * 3)
std::cerr << "Error reading counter " << ev.first << std::endl;
ioctl(event.fd, PERF_EVENT_IOC_DISABLE, 0);
#else
compat::unused(event);
#endif
}
}
void printHeader(std::ostream& out) {
for (auto& name : ordered_names)
out << std::setw(printFieldWidth) << name << ",";
}
void printAll(std::ostream& out, double n) {
for (auto& name : ordered_names) {
double aggr = 0;
for (auto& event : events[name]) aggr += event.readCounter();
out << std::setw(printFieldWidth) << aggr / n << ",";
}
}
double operator[](std::string index) {
double aggr = 0;
for (auto& event : events[index]) aggr += event.readCounter();
return aggr;
};
void timeAndProfile(std::string s, uint64_t count, std::function<void()> fn,
uint64_t repetitions = 1, bool mem = false);
};
inline double gettime() {
struct timeval now_tv;
gettimeofday(&now_tv, NULL);
return ((double)now_tv.tv_sec) + ((double)now_tv.tv_usec) / 1000000.0;
}
size_t getCurrentRSS() {
long rss = 0L;
FILE* fp = NULL;
if ((fp = fopen("/proc/self/statm", "r")) == NULL)
return (size_t)0L; /* Can't open? */
if (fscanf(fp, "%*s%ld", &rss) != 1) {
fclose(fp);
return (size_t)0L; /* Can't read? */
}
fclose(fp);
return (size_t)rss * (size_t)sysconf(_SC_PAGESIZE);
}
void PerfEvents::timeAndProfile(std::string s, uint64_t count,
std::function<void()> fn, uint64_t repetitions,
bool mem) {
using namespace std;
// warmup round
double warumupStart = gettime();
while (gettime() - warumupStart < 0.15) fn();
uint64_t memStart = 0;
if (mem) memStart = getCurrentRSS();
startAll();
double start = gettime();
size_t performedRep = 0;
for (; performedRep < repetitions || gettime() - start < 0.5;
++performedRep) {
fn();
}
double end = gettime();
readAll();
std::cout.precision(3);
std::cout.setf(std::ios::fixed, std::ios::floatfield);
if (writeHeader) {
std::cout << setw(20) << "name"
<< "," << setw(printFieldWidth) << " time"
<< "," << setw(printFieldWidth) << " CPUs"
<< "," << setw(printFieldWidth) << " IPC"
<< "," << setw(printFieldWidth) << " GHz"
<< "," << setw(printFieldWidth) << " Bandwidth"
<< ",";
printHeader(std::cout);
std::cout << std::endl;
}
auto runtime = end - start;
std::cout << setw(20) << s << "," << setw(printFieldWidth)
<< (runtime * 1e3 / performedRep) << ",";
#ifdef __linux__
if (!getenv("EXTERNALPROFILE")) {
std::cout << setw(printFieldWidth)
<< ((*this)["task-clock"] / (runtime * 1e9)) << ",";
std::cout << setw(printFieldWidth)
<< ((*this)["instr."] / (*this)["cycles"]) << ",";
std::cout << setw(printFieldWidth)
<< ((*this)["cycles"] /
(this->events["cycles"][0].data.time_enabled -
this->events["cycles"][0].prev.time_enabled))
<< ",";
std::cout << setw(printFieldWidth)
<< ((((*this)["all_rd"] * 64.0) / (1024 * 1024)) /
(end - start))
<< ",";
}
#endif
// std::cout <<
// (((e["all_requests"]*64.0)/(1024*1024))/(e.events["cycles"][0].data.time_enabled/1e9))
// << " allMB/s,";
printAll(std::cout, count * performedRep);
if (mem) std::cout << (getCurrentRSS() - memStart) / (1024.0 * 1024) << "MB";
std::cout << std::endl;
writeHeader = false;
}