Commit | Line | Data |
---|---|---|
f8a95309 ACM |
1 | /* |
2 | * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> | |
3 | * | |
4 | * Parts came from builtin-{top,stat,record}.c, see those files for further | |
5 | * copyright notes. | |
6 | * | |
7 | * Released under the GPL v2. (and only v2, not any later version) | |
8 | */ | |
9 | ||
69aad6f1 | 10 | #include "evsel.h" |
70082dd9 | 11 | #include "evlist.h" |
69aad6f1 | 12 | #include "util.h" |
86bd5e86 | 13 | #include "cpumap.h" |
fd78260b | 14 | #include "thread_map.h" |
69aad6f1 | 15 | |
c52b12ed ACM |
16 | #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y)) |
17 | ||
ef1d1af2 ACM |
18 | void perf_evsel__init(struct perf_evsel *evsel, |
19 | struct perf_event_attr *attr, int idx) | |
20 | { | |
21 | evsel->idx = idx; | |
22 | evsel->attr = *attr; | |
23 | INIT_LIST_HEAD(&evsel->node); | |
24 | } | |
25 | ||
23a2f3ab | 26 | struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx) |
69aad6f1 ACM |
27 | { |
28 | struct perf_evsel *evsel = zalloc(sizeof(*evsel)); | |
29 | ||
ef1d1af2 ACM |
30 | if (evsel != NULL) |
31 | perf_evsel__init(evsel, attr, idx); | |
69aad6f1 ACM |
32 | |
33 | return evsel; | |
34 | } | |
35 | ||
36 | int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads) | |
37 | { | |
38 | evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int)); | |
39 | return evsel->fd != NULL ? 0 : -ENOMEM; | |
40 | } | |
41 | ||
70db7533 ACM |
42 | int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads) |
43 | { | |
44 | evsel->id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id)); | |
45 | return evsel->id != NULL ? 0 : -ENOMEM; | |
46 | } | |
47 | ||
c52b12ed ACM |
48 | int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus) |
49 | { | |
50 | evsel->counts = zalloc((sizeof(*evsel->counts) + | |
51 | (ncpus * sizeof(struct perf_counts_values)))); | |
52 | return evsel->counts != NULL ? 0 : -ENOMEM; | |
53 | } | |
54 | ||
69aad6f1 ACM |
55 | void perf_evsel__free_fd(struct perf_evsel *evsel) |
56 | { | |
57 | xyarray__delete(evsel->fd); | |
58 | evsel->fd = NULL; | |
59 | } | |
60 | ||
70db7533 ACM |
61 | void perf_evsel__free_id(struct perf_evsel *evsel) |
62 | { | |
63 | xyarray__delete(evsel->id); | |
64 | evsel->id = NULL; | |
65 | } | |
66 | ||
c52b12ed ACM |
67 | void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads) |
68 | { | |
69 | int cpu, thread; | |
70 | ||
71 | for (cpu = 0; cpu < ncpus; cpu++) | |
72 | for (thread = 0; thread < nthreads; ++thread) { | |
73 | close(FD(evsel, cpu, thread)); | |
74 | FD(evsel, cpu, thread) = -1; | |
75 | } | |
76 | } | |
77 | ||
ef1d1af2 | 78 | void perf_evsel__exit(struct perf_evsel *evsel) |
69aad6f1 ACM |
79 | { |
80 | assert(list_empty(&evsel->node)); | |
81 | xyarray__delete(evsel->fd); | |
70db7533 | 82 | xyarray__delete(evsel->id); |
ef1d1af2 ACM |
83 | } |
84 | ||
85 | void perf_evsel__delete(struct perf_evsel *evsel) | |
86 | { | |
87 | perf_evsel__exit(evsel); | |
023695d9 | 88 | close_cgroup(evsel->cgrp); |
f0c55bcf | 89 | free(evsel->name); |
69aad6f1 ACM |
90 | free(evsel); |
91 | } | |
c52b12ed ACM |
92 | |
93 | int __perf_evsel__read_on_cpu(struct perf_evsel *evsel, | |
94 | int cpu, int thread, bool scale) | |
95 | { | |
96 | struct perf_counts_values count; | |
97 | size_t nv = scale ? 3 : 1; | |
98 | ||
99 | if (FD(evsel, cpu, thread) < 0) | |
100 | return -EINVAL; | |
101 | ||
4eed11d5 ACM |
102 | if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0) |
103 | return -ENOMEM; | |
104 | ||
c52b12ed ACM |
105 | if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0) |
106 | return -errno; | |
107 | ||
108 | if (scale) { | |
109 | if (count.run == 0) | |
110 | count.val = 0; | |
111 | else if (count.run < count.ena) | |
112 | count.val = (u64)((double)count.val * count.ena / count.run + 0.5); | |
113 | } else | |
114 | count.ena = count.run = 0; | |
115 | ||
116 | evsel->counts->cpu[cpu] = count; | |
117 | return 0; | |
118 | } | |
119 | ||
120 | int __perf_evsel__read(struct perf_evsel *evsel, | |
121 | int ncpus, int nthreads, bool scale) | |
122 | { | |
123 | size_t nv = scale ? 3 : 1; | |
124 | int cpu, thread; | |
125 | struct perf_counts_values *aggr = &evsel->counts->aggr, count; | |
126 | ||
52bcd994 | 127 | aggr->val = aggr->ena = aggr->run = 0; |
c52b12ed ACM |
128 | |
129 | for (cpu = 0; cpu < ncpus; cpu++) { | |
130 | for (thread = 0; thread < nthreads; thread++) { | |
131 | if (FD(evsel, cpu, thread) < 0) | |
132 | continue; | |
133 | ||
134 | if (readn(FD(evsel, cpu, thread), | |
135 | &count, nv * sizeof(u64)) < 0) | |
136 | return -errno; | |
137 | ||
138 | aggr->val += count.val; | |
139 | if (scale) { | |
140 | aggr->ena += count.ena; | |
141 | aggr->run += count.run; | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | evsel->counts->scaled = 0; | |
147 | if (scale) { | |
148 | if (aggr->run == 0) { | |
149 | evsel->counts->scaled = -1; | |
150 | aggr->val = 0; | |
151 | return 0; | |
152 | } | |
153 | ||
154 | if (aggr->run < aggr->ena) { | |
155 | evsel->counts->scaled = 1; | |
156 | aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5); | |
157 | } | |
158 | } else | |
159 | aggr->ena = aggr->run = 0; | |
160 | ||
161 | return 0; | |
162 | } | |
48290609 | 163 | |
0252208e | 164 | static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
9d04f178 | 165 | struct thread_map *threads, bool group, bool inherit) |
48290609 | 166 | { |
0252208e | 167 | int cpu, thread; |
023695d9 SE |
168 | unsigned long flags = 0; |
169 | int pid = -1; | |
48290609 | 170 | |
0252208e ACM |
171 | if (evsel->fd == NULL && |
172 | perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0) | |
4eed11d5 ACM |
173 | return -1; |
174 | ||
023695d9 SE |
175 | if (evsel->cgrp) { |
176 | flags = PERF_FLAG_PID_CGROUP; | |
177 | pid = evsel->cgrp->fd; | |
178 | } | |
179 | ||
86bd5e86 | 180 | for (cpu = 0; cpu < cpus->nr; cpu++) { |
f08199d3 | 181 | int group_fd = -1; |
e603dc15 ACM |
182 | /* |
183 | * Don't allow mmap() of inherited per-task counters. This | |
184 | * would create a performance issue due to all children writing | |
185 | * to the same buffer. | |
186 | * | |
187 | * FIXME: | |
188 | * Proper fix is not to pass 'inherit' to perf_evsel__open*, | |
189 | * but a 'flags' parameter, with 'group' folded there as well, | |
190 | * then introduce a PERF_O_{MMAP,GROUP,INHERIT} enum, and if | |
191 | * O_MMAP is set, emit a warning if cpu < 0 and O_INHERIT is | |
192 | * set. Lets go for the minimal fix first tho. | |
193 | */ | |
194 | evsel->attr.inherit = (cpus->map[cpu] >= 0) && inherit; | |
9d04f178 | 195 | |
0252208e | 196 | for (thread = 0; thread < threads->nr; thread++) { |
023695d9 SE |
197 | |
198 | if (!evsel->cgrp) | |
199 | pid = threads->map[thread]; | |
200 | ||
0252208e | 201 | FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr, |
023695d9 | 202 | pid, |
f08199d3 | 203 | cpus->map[cpu], |
023695d9 | 204 | group_fd, flags); |
0252208e ACM |
205 | if (FD(evsel, cpu, thread) < 0) |
206 | goto out_close; | |
f08199d3 ACM |
207 | |
208 | if (group && group_fd == -1) | |
209 | group_fd = FD(evsel, cpu, thread); | |
0252208e | 210 | } |
48290609 ACM |
211 | } |
212 | ||
213 | return 0; | |
214 | ||
215 | out_close: | |
0252208e ACM |
216 | do { |
217 | while (--thread >= 0) { | |
218 | close(FD(evsel, cpu, thread)); | |
219 | FD(evsel, cpu, thread) = -1; | |
220 | } | |
221 | thread = threads->nr; | |
222 | } while (--cpu >= 0); | |
48290609 ACM |
223 | return -1; |
224 | } | |
225 | ||
0252208e ACM |
226 | static struct { |
227 | struct cpu_map map; | |
228 | int cpus[1]; | |
229 | } empty_cpu_map = { | |
230 | .map.nr = 1, | |
231 | .cpus = { -1, }, | |
232 | }; | |
233 | ||
234 | static struct { | |
235 | struct thread_map map; | |
236 | int threads[1]; | |
237 | } empty_thread_map = { | |
238 | .map.nr = 1, | |
239 | .threads = { -1, }, | |
240 | }; | |
241 | ||
f08199d3 | 242 | int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus, |
9d04f178 | 243 | struct thread_map *threads, bool group, bool inherit) |
48290609 | 244 | { |
0252208e ACM |
245 | if (cpus == NULL) { |
246 | /* Work around old compiler warnings about strict aliasing */ | |
247 | cpus = &empty_cpu_map.map; | |
48290609 ACM |
248 | } |
249 | ||
0252208e ACM |
250 | if (threads == NULL) |
251 | threads = &empty_thread_map.map; | |
48290609 | 252 | |
9d04f178 | 253 | return __perf_evsel__open(evsel, cpus, threads, group, inherit); |
48290609 ACM |
254 | } |
255 | ||
f08199d3 | 256 | int perf_evsel__open_per_cpu(struct perf_evsel *evsel, |
9d04f178 | 257 | struct cpu_map *cpus, bool group, bool inherit) |
48290609 | 258 | { |
9d04f178 | 259 | return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit); |
0252208e | 260 | } |
48290609 | 261 | |
f08199d3 | 262 | int perf_evsel__open_per_thread(struct perf_evsel *evsel, |
9d04f178 | 263 | struct thread_map *threads, bool group, bool inherit) |
0252208e | 264 | { |
9d04f178 | 265 | return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit); |
48290609 | 266 | } |
70082dd9 | 267 | |
8115d60c ACM |
268 | static int perf_event__parse_id_sample(const union perf_event *event, u64 type, |
269 | struct perf_sample *sample) | |
d0dd74e8 ACM |
270 | { |
271 | const u64 *array = event->sample.array; | |
272 | ||
273 | array += ((event->header.size - | |
274 | sizeof(event->header)) / sizeof(u64)) - 1; | |
275 | ||
276 | if (type & PERF_SAMPLE_CPU) { | |
277 | u32 *p = (u32 *)array; | |
278 | sample->cpu = *p; | |
279 | array--; | |
280 | } | |
281 | ||
282 | if (type & PERF_SAMPLE_STREAM_ID) { | |
283 | sample->stream_id = *array; | |
284 | array--; | |
285 | } | |
286 | ||
287 | if (type & PERF_SAMPLE_ID) { | |
288 | sample->id = *array; | |
289 | array--; | |
290 | } | |
291 | ||
292 | if (type & PERF_SAMPLE_TIME) { | |
293 | sample->time = *array; | |
294 | array--; | |
295 | } | |
296 | ||
297 | if (type & PERF_SAMPLE_TID) { | |
298 | u32 *p = (u32 *)array; | |
299 | sample->pid = p[0]; | |
300 | sample->tid = p[1]; | |
301 | } | |
302 | ||
303 | return 0; | |
304 | } | |
305 | ||
8115d60c ACM |
306 | int perf_event__parse_sample(const union perf_event *event, u64 type, |
307 | bool sample_id_all, struct perf_sample *data) | |
d0dd74e8 ACM |
308 | { |
309 | const u64 *array; | |
310 | ||
311 | data->cpu = data->pid = data->tid = -1; | |
312 | data->stream_id = data->id = data->time = -1ULL; | |
313 | ||
314 | if (event->header.type != PERF_RECORD_SAMPLE) { | |
315 | if (!sample_id_all) | |
316 | return 0; | |
8115d60c | 317 | return perf_event__parse_id_sample(event, type, data); |
d0dd74e8 ACM |
318 | } |
319 | ||
320 | array = event->sample.array; | |
321 | ||
322 | if (type & PERF_SAMPLE_IP) { | |
323 | data->ip = event->ip.ip; | |
324 | array++; | |
325 | } | |
326 | ||
327 | if (type & PERF_SAMPLE_TID) { | |
328 | u32 *p = (u32 *)array; | |
329 | data->pid = p[0]; | |
330 | data->tid = p[1]; | |
331 | array++; | |
332 | } | |
333 | ||
334 | if (type & PERF_SAMPLE_TIME) { | |
335 | data->time = *array; | |
336 | array++; | |
337 | } | |
338 | ||
339 | if (type & PERF_SAMPLE_ADDR) { | |
340 | data->addr = *array; | |
341 | array++; | |
342 | } | |
343 | ||
344 | data->id = -1ULL; | |
345 | if (type & PERF_SAMPLE_ID) { | |
346 | data->id = *array; | |
347 | array++; | |
348 | } | |
349 | ||
350 | if (type & PERF_SAMPLE_STREAM_ID) { | |
351 | data->stream_id = *array; | |
352 | array++; | |
353 | } | |
354 | ||
355 | if (type & PERF_SAMPLE_CPU) { | |
356 | u32 *p = (u32 *)array; | |
357 | data->cpu = *p; | |
358 | array++; | |
359 | } | |
360 | ||
361 | if (type & PERF_SAMPLE_PERIOD) { | |
362 | data->period = *array; | |
363 | array++; | |
364 | } | |
365 | ||
366 | if (type & PERF_SAMPLE_READ) { | |
367 | fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n"); | |
368 | return -1; | |
369 | } | |
370 | ||
371 | if (type & PERF_SAMPLE_CALLCHAIN) { | |
372 | data->callchain = (struct ip_callchain *)array; | |
373 | array += 1 + data->callchain->nr; | |
374 | } | |
375 | ||
376 | if (type & PERF_SAMPLE_RAW) { | |
377 | u32 *p = (u32 *)array; | |
378 | data->raw_size = *p; | |
379 | p++; | |
380 | data->raw_data = p; | |
381 | } | |
382 | ||
383 | return 0; | |
384 | } |