2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
4 * Parts came from builtin-{top,stat,record}.c, see those files for further
7 * Released under the GPL v2. (and only v2, not any later version)
14 #include "thread_map.h"
16 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
18 void perf_evsel__init(struct perf_evsel *evsel,
19 struct perf_event_attr *attr, int idx)
23 INIT_LIST_HEAD(&evsel->node);
26 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
28 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
31 perf_evsel__init(evsel, attr, idx);
36 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
38 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
39 return evsel->fd != NULL ? 0 : -ENOMEM;
42 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
44 evsel->id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
45 return evsel->id != NULL ? 0 : -ENOMEM;
48 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
50 evsel->counts = zalloc((sizeof(*evsel->counts) +
51 (ncpus * sizeof(struct perf_counts_values))));
52 return evsel->counts != NULL ? 0 : -ENOMEM;
55 void perf_evsel__free_fd(struct perf_evsel *evsel)
57 xyarray__delete(evsel->fd);
61 void perf_evsel__free_id(struct perf_evsel *evsel)
63 xyarray__delete(evsel->id);
67 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
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;
78 void perf_evsel__exit(struct perf_evsel *evsel)
80 assert(list_empty(&evsel->node));
81 xyarray__delete(evsel->fd);
82 xyarray__delete(evsel->id);
85 void perf_evsel__delete(struct perf_evsel *evsel)
87 perf_evsel__exit(evsel);
88 close_cgroup(evsel->cgrp);
92 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
93 int cpu, int thread, bool scale)
95 struct perf_counts_values count;
96 size_t nv = scale ? 3 : 1;
98 if (FD(evsel, cpu, thread) < 0)
101 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
104 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
110 else if (count.run < count.ena)
111 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
113 count.ena = count.run = 0;
115 evsel->counts->cpu[cpu] = count;
119 int __perf_evsel__read(struct perf_evsel *evsel,
120 int ncpus, int nthreads, bool scale)
122 size_t nv = scale ? 3 : 1;
124 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
126 aggr->val = aggr->ena = aggr->run = 0;
128 for (cpu = 0; cpu < ncpus; cpu++) {
129 for (thread = 0; thread < nthreads; thread++) {
130 if (FD(evsel, cpu, thread) < 0)
133 if (readn(FD(evsel, cpu, thread),
134 &count, nv * sizeof(u64)) < 0)
137 aggr->val += count.val;
139 aggr->ena += count.ena;
140 aggr->run += count.run;
145 evsel->counts->scaled = 0;
147 if (aggr->run == 0) {
148 evsel->counts->scaled = -1;
153 if (aggr->run < aggr->ena) {
154 evsel->counts->scaled = 1;
155 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
158 aggr->ena = aggr->run = 0;
163 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
164 struct thread_map *threads, bool group, bool inherit)
167 unsigned long flags = 0;
170 if (evsel->fd == NULL &&
171 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
175 flags = PERF_FLAG_PID_CGROUP;
176 pid = evsel->cgrp->fd;
179 for (cpu = 0; cpu < cpus->nr; cpu++) {
182 evsel->attr.inherit = (cpus->map[cpu] < 0) && inherit;
184 for (thread = 0; thread < threads->nr; thread++) {
187 pid = threads->map[thread];
189 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
193 if (FD(evsel, cpu, thread) < 0)
196 if (group && group_fd == -1)
197 group_fd = FD(evsel, cpu, thread);
205 while (--thread >= 0) {
206 close(FD(evsel, cpu, thread));
207 FD(evsel, cpu, thread) = -1;
209 thread = threads->nr;
210 } while (--cpu >= 0);
223 struct thread_map map;
225 } empty_thread_map = {
230 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
231 struct thread_map *threads, bool group, bool inherit)
234 /* Work around old compiler warnings about strict aliasing */
235 cpus = &empty_cpu_map.map;
239 threads = &empty_thread_map.map;
241 return __perf_evsel__open(evsel, cpus, threads, group, inherit);
244 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
245 struct cpu_map *cpus, bool group, bool inherit)
247 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit);
250 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
251 struct thread_map *threads, bool group, bool inherit)
253 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit);
256 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
257 struct perf_sample *sample)
259 const u64 *array = event->sample.array;
261 array += ((event->header.size -
262 sizeof(event->header)) / sizeof(u64)) - 1;
264 if (type & PERF_SAMPLE_CPU) {
265 u32 *p = (u32 *)array;
270 if (type & PERF_SAMPLE_STREAM_ID) {
271 sample->stream_id = *array;
275 if (type & PERF_SAMPLE_ID) {
280 if (type & PERF_SAMPLE_TIME) {
281 sample->time = *array;
285 if (type & PERF_SAMPLE_TID) {
286 u32 *p = (u32 *)array;
294 int perf_event__parse_sample(const union perf_event *event, u64 type,
295 bool sample_id_all, struct perf_sample *data)
299 data->cpu = data->pid = data->tid = -1;
300 data->stream_id = data->id = data->time = -1ULL;
302 if (event->header.type != PERF_RECORD_SAMPLE) {
305 return perf_event__parse_id_sample(event, type, data);
308 array = event->sample.array;
310 if (type & PERF_SAMPLE_IP) {
311 data->ip = event->ip.ip;
315 if (type & PERF_SAMPLE_TID) {
316 u32 *p = (u32 *)array;
322 if (type & PERF_SAMPLE_TIME) {
327 if (type & PERF_SAMPLE_ADDR) {
333 if (type & PERF_SAMPLE_ID) {
338 if (type & PERF_SAMPLE_STREAM_ID) {
339 data->stream_id = *array;
343 if (type & PERF_SAMPLE_CPU) {
344 u32 *p = (u32 *)array;
349 if (type & PERF_SAMPLE_PERIOD) {
350 data->period = *array;
354 if (type & PERF_SAMPLE_READ) {
355 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
359 if (type & PERF_SAMPLE_CALLCHAIN) {
360 data->callchain = (struct ip_callchain *)array;
361 array += 1 + data->callchain->nr;
364 if (type & PERF_SAMPLE_RAW) {
365 u32 *p = (u32 *)array;