forked from mschuldt/fractal-compression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
counters.cpp
60 lines (49 loc) · 1.04 KB
/
counters.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
#include "counters.h"
static void initCounter(hwCounter_t &x, uint64_t counterConfig)
{
if(!x.init)
{
memset(&(x.attr), 0, sizeof(x.attr));
x.attr.disabled = 0;
x.attr.type = PERF_TYPE_HARDWARE;
x.attr.config = counterConfig;
pid_t pid = 0;
int cpu = -1;
int group_fd = -1;
unsigned long flags = 0;
x.fd = syscall(__NR_perf_event_open, &(x.attr), pid,
cpu, group_fd, flags);
assert(x.fd >= 0);
x.init = true;
}
}
void initTicks(hwCounter_t &x)
{
initCounter(x,PERF_COUNT_HW_CPU_CYCLES);
}
void initInsns(hwCounter_t &x)
{
initCounter(x,PERF_COUNT_HW_INSTRUCTIONS);
}
uint64_t getTicks(hwCounter_t &x)
{
int rc;
if(!x.init)
{
initTicks(x);
}
rc = read(x.fd,&(x.buffer),sizeof(x.buffer));
assert(rc == sizeof(x.buffer));
return x.buffer;
}
uint64_t getInsns(hwCounter_t &x)
{
int rc;
if(!x.init)
{
initInsns(x);
}
rc = read(x.fd,&(x.buffer),sizeof(x.buffer));
assert(rc == sizeof(x.buffer));
return x.buffer;
}