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
|
#include "asm/unistd.h" /* from kernel source tree */
#include <unistd.h> /* for syscall */
#include <sys/ioctl.h>
#include "perfcounters.h"
#define C(x) (PERF_COUNT_HW_CACHE_##x)
#define ATTR_CONFIG_CACHE(cache, op, result) \
(((C(cache) & 0xffULL) << 0) | \
((C(op) & 0xffULL) << 8) | \
((C(result) & 0xffULL) << 16))
#define ATTR_CONFIG(event, umask) \
((((event) & 0xffULL) << 0) | \
(((umask) & 0xffULL) << 8))
static struct perf_event_attr perf_event_attr = {
.type = 0, /* set per initilized event */
.size = 0, /* set later */
.config = 0, /* set per initilized event */
{ .sample_period = 0, }, /* is a counter, so no period */
.disabled = 0, /* event is enabled */
.inherit = 0, /* children don't inherit */
.pinned = 0, /* set per initilized event */
.exclusive = 0, /* set per initilized event */
.exclude_user = 0, /* don't count user (when set) */
.exclude_kernel = 0, /* ditto kernel */
.exclude_hv = 0, /* ditto hypervisor */
.exclude_idle = 0, /* don't count when idle */
.mmap = 0, /* include mmap data */
.comm = 0, /* include comm data */
};
struct perf_counter_setup {
char *name;
enum perf_type_id type;
uint64_t config;
};
#if 0
/* these events are always zero */
static struct perf_fd perf_fds[] = {
{
.fd = -1,
.name = "MEM_UNCORE_RETIRED.REMOTE_CACHE_LOCAL_HOME_HIT",
.type = PERF_TYPE_RAW,
.config = ATTR_CONFIG(0x0f, 0x08),
.exclusive = 0,
.pinned = 0,
},
{
.fd = -1,
.name = "MEM_UNCORE_RETIRED.REMOTE_DRAM",
.type = PERF_TYPE_RAW,
.config = ATTR_CONFIG(0x0f, 0x10),
.exclusive = 0, /* child events cannot be exclusive */
.pinned = 0, /* child events cannot be pinned */
},
{ },
};
#endif
static struct perf_counter_setup perf_setup[NR_PERF_COUNTERS] = {
#if 0
{
.name = "MEM_UNCORE_RETIRED.LOCAL_DRAM",
.type = PERF_TYPE_RAW,
.config = ATTR_CONFIG(0x0f, 0x20),
},
{
.name = "L2_RQSTS.PREFETCH_HIT",
.type = PERF_TYPE_RAW,
.config = ATTR_CONFIG(0x24, 0x40),
},
{
.name = "L2_RQSTS.PREFETCH_MISS",
.type = PERF_TYPE_RAW,
.config = ATTR_CONFIG(0x24, 0x80),
},
#endif
{
.name = "MEM_LOAD_RETIRED.OTHER_CORE_L2_HIT_HITM",
.type = PERF_TYPE_RAW,
.config = ATTR_CONFIG(0xcb, 0x08),
},
{
.name = "MEM_LOAD_RETIRED.L3_UNSHARED_HIT",
.type = PERF_TYPE_RAW,
.config = ATTR_CONFIG(0xcb, 0x04),
},
{
.name = "MEM_LOAD_RETIRED.L3_MISS",
.type = PERF_TYPE_RAW,
.config = ATTR_CONFIG(0xcb, 0x10),
},
{
.name = "Off Core Response Counter",
.type = PERF_TYPE_HW_CACHE,
.config = ATTR_CONFIG_CACHE(LL, OP_READ, RESULT_MISS),
#if 0
/* read misses */
.config = ATTR_CONFIG_CACHE(LL, OP_READ, RESULT_MISS),
/* write misses */
.config = ATTR_CONFIG_CACHE(LL, OP_WRITE, RESULT_MISS),
/* prefetch misses */
.config = ATTR_CONFIG_CACHE(LL, OP_PREFETCH, RESULT_MISS),
#endif
},
};
/* from kernel tools/perf/perf.h */
int sys_perf_event_open(struct perf_event_attr *attr, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
attr->size = sizeof(*attr);
return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
}
/* make the temporary attributes shadow those in the perf_fd temporarially */
static void write_global_perf_attr(const struct perf_counter_setup *p)
{
perf_event_attr.type = p->type;
perf_event_attr.config = p->config;
perf_event_attr.pinned = 0;
perf_event_attr.exclusive = 0;
}
int setup_cpu_perf(const int cpu, const int group_leader,
struct perf_counter *perf_counters)
{
const int perf_pid = -1; /* -1: all tasks */
int err = 0, i;
if (-1 == group_leader) {
/* first element determines the group for all others */
perf_counters->fd = -1;
}
for (i = 0; i < NR_PERF_COUNTERS; i++) {
int perf_group;
/* setup the attributes to pass in */
write_global_perf_attr(&perf_setup[i]);
if (0 == i && -1 == group_leader) {
/* but group leader is pinned and exclusive */
perf_event_attr.exclusive = 1;
perf_event_attr.pinned = 1;
perf_group = -1;
} else if (-1 == group_leader) {
/* not first counter, but no group passed in */
perf_group = perf_counters[0].fd;
}
perf_counters[i].fd = sys_perf_event_open(&perf_event_attr,
perf_pid, cpu, perf_group, 0);
if (0 > perf_counters[i].fd) {
err = -1;
goto out;
}
/* save the attributes in the user-visible configuration */
perf_counters[i].type = perf_setup[i].type;
perf_counters[i].config = perf_setup[i].config;
}
out:
return err;
}
static inline int perf_setup_match(const struct perf_counter_setup* ps,
const struct perf_counter *pc)
{
return (ps->type == pc->type && ps->config == pc->config);
}
const char* get_perf_name(const struct perf_counter* perf_counter)
{
char *ret = NULL;
int i;
for (i = 0; i < NR_PERF_COUNTERS; i++) {
if (perf_setup_match(&perf_setup[i], perf_counter)) {
ret = perf_setup[i].name;
break;
}
}
return ret;
}
int read_perf_counter(const struct perf_counter* perf_counter, uint64_t *val)
{
ssize_t ret = read(perf_counter->fd, val, sizeof(*val));
return (ret <= 0);
}
|