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);
91 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
92 int cpu, int thread, bool scale)
94 struct perf_counts_values count;
95 size_t nv = scale ? 3 : 1;
97 if (FD(evsel, cpu, thread) < 0)
100 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
103 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
109 else if (count.run < count.ena)
110 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
112 count.ena = count.run = 0;
114 evsel->counts->cpu[cpu] = count;
118 int __perf_evsel__read(struct perf_evsel *evsel,
119 int ncpus, int nthreads, bool scale)
121 size_t nv = scale ? 3 : 1;
123 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
125 aggr->val = aggr->ena = aggr->run = 0;
127 for (cpu = 0; cpu < ncpus; cpu++) {
128 for (thread = 0; thread < nthreads; thread++) {
129 if (FD(evsel, cpu, thread) < 0)
132 if (readn(FD(evsel, cpu, thread),
133 &count, nv * sizeof(u64)) < 0)
136 aggr->val += count.val;
138 aggr->ena += count.ena;
139 aggr->run += count.run;
144 evsel->counts->scaled = 0;
146 if (aggr->run == 0) {
147 evsel->counts->scaled = -1;
152 if (aggr->run < aggr->ena) {
153 evsel->counts->scaled = 1;
154 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
157 aggr->ena = aggr->run = 0;
162 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
163 struct thread_map *threads, bool group, bool inherit)
167 if (evsel->fd == NULL &&
168 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
171 for (cpu = 0; cpu < cpus->nr; cpu++) {
174 evsel->attr.inherit = (cpus->map[cpu] < 0) && inherit;
176 for (thread = 0; thread < threads->nr; thread++) {
177 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
178 threads->map[thread],
181 if (FD(evsel, cpu, thread) < 0)
184 if (group && group_fd == -1)
185 group_fd = FD(evsel, cpu, thread);
193 while (--thread >= 0) {
194 close(FD(evsel, cpu, thread));
195 FD(evsel, cpu, thread) = -1;
197 thread = threads->nr;
198 } while (--cpu >= 0);
211 struct thread_map map;
213 } empty_thread_map = {
218 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
219 struct thread_map *threads, bool group, bool inherit)
222 /* Work around old compiler warnings about strict aliasing */
223 cpus = &empty_cpu_map.map;
227 threads = &empty_thread_map.map;
229 return __perf_evsel__open(evsel, cpus, threads, group, inherit);
232 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
233 struct cpu_map *cpus, bool group, bool inherit)
235 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group, inherit);
238 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
239 struct thread_map *threads, bool group, bool inherit)
241 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group, inherit);
244 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
245 struct perf_sample *sample)
247 const u64 *array = event->sample.array;
249 array += ((event->header.size -
250 sizeof(event->header)) / sizeof(u64)) - 1;
252 if (type & PERF_SAMPLE_CPU) {
253 u32 *p = (u32 *)array;
258 if (type & PERF_SAMPLE_STREAM_ID) {
259 sample->stream_id = *array;
263 if (type & PERF_SAMPLE_ID) {
268 if (type & PERF_SAMPLE_TIME) {
269 sample->time = *array;
273 if (type & PERF_SAMPLE_TID) {
274 u32 *p = (u32 *)array;
282 int perf_event__parse_sample(const union perf_event *event, u64 type,
283 bool sample_id_all, struct perf_sample *data)
287 data->cpu = data->pid = data->tid = -1;
288 data->stream_id = data->id = data->time = -1ULL;
290 if (event->header.type != PERF_RECORD_SAMPLE) {
293 return perf_event__parse_id_sample(event, type, data);
296 array = event->sample.array;
298 if (type & PERF_SAMPLE_IP) {
299 data->ip = event->ip.ip;
303 if (type & PERF_SAMPLE_TID) {
304 u32 *p = (u32 *)array;
310 if (type & PERF_SAMPLE_TIME) {
315 if (type & PERF_SAMPLE_ADDR) {
321 if (type & PERF_SAMPLE_ID) {
326 if (type & PERF_SAMPLE_STREAM_ID) {
327 data->stream_id = *array;
331 if (type & PERF_SAMPLE_CPU) {
332 u32 *p = (u32 *)array;
337 if (type & PERF_SAMPLE_PERIOD) {
338 data->period = *array;
342 if (type & PERF_SAMPLE_READ) {
343 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
347 if (type & PERF_SAMPLE_CALLCHAIN) {
348 data->callchain = (struct ip_callchain *)array;
349 array += 1 + data->callchain->nr;
352 if (type & PERF_SAMPLE_RAW) {
353 u32 *p = (u32 *)array;