gma500: move the i2c code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / tools / perf / util / evsel.c
blob0239eb87b2321ea9e4fe208605020e83b1d7c718
1 /*
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
5 * copyright notes.
7 * Released under the GPL v2. (and only v2, not any later version)
8 */
10 #include "evsel.h"
11 #include "evlist.h"
12 #include "util.h"
13 #include "cpumap.h"
14 #include "thread_map.h"
16 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
18 int __perf_evsel__sample_size(u64 sample_type)
20 u64 mask = sample_type & PERF_SAMPLE_MASK;
21 int size = 0;
22 int i;
24 for (i = 0; i < 64; i++) {
25 if (mask & (1ULL << i))
26 size++;
29 size *= sizeof(u64);
31 return size;
34 void perf_evsel__init(struct perf_evsel *evsel,
35 struct perf_event_attr *attr, int idx)
37 evsel->idx = idx;
38 evsel->attr = *attr;
39 INIT_LIST_HEAD(&evsel->node);
42 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
44 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
46 if (evsel != NULL)
47 perf_evsel__init(evsel, attr, idx);
49 return evsel;
52 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
54 int cpu, thread;
55 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
57 if (evsel->fd) {
58 for (cpu = 0; cpu < ncpus; cpu++) {
59 for (thread = 0; thread < nthreads; thread++) {
60 FD(evsel, cpu, thread) = -1;
65 return evsel->fd != NULL ? 0 : -ENOMEM;
68 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
70 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
71 if (evsel->sample_id == NULL)
72 return -ENOMEM;
74 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
75 if (evsel->id == NULL) {
76 xyarray__delete(evsel->sample_id);
77 evsel->sample_id = NULL;
78 return -ENOMEM;
81 return 0;
84 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
86 evsel->counts = zalloc((sizeof(*evsel->counts) +
87 (ncpus * sizeof(struct perf_counts_values))));
88 return evsel->counts != NULL ? 0 : -ENOMEM;
91 void perf_evsel__free_fd(struct perf_evsel *evsel)
93 xyarray__delete(evsel->fd);
94 evsel->fd = NULL;
97 void perf_evsel__free_id(struct perf_evsel *evsel)
99 xyarray__delete(evsel->sample_id);
100 evsel->sample_id = NULL;
101 free(evsel->id);
102 evsel->id = NULL;
105 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
107 int cpu, thread;
109 for (cpu = 0; cpu < ncpus; cpu++)
110 for (thread = 0; thread < nthreads; ++thread) {
111 close(FD(evsel, cpu, thread));
112 FD(evsel, cpu, thread) = -1;
116 void perf_evsel__exit(struct perf_evsel *evsel)
118 assert(list_empty(&evsel->node));
119 xyarray__delete(evsel->fd);
120 xyarray__delete(evsel->sample_id);
121 free(evsel->id);
124 void perf_evsel__delete(struct perf_evsel *evsel)
126 perf_evsel__exit(evsel);
127 close_cgroup(evsel->cgrp);
128 free(evsel->name);
129 free(evsel);
132 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
133 int cpu, int thread, bool scale)
135 struct perf_counts_values count;
136 size_t nv = scale ? 3 : 1;
138 if (FD(evsel, cpu, thread) < 0)
139 return -EINVAL;
141 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
142 return -ENOMEM;
144 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
145 return -errno;
147 if (scale) {
148 if (count.run == 0)
149 count.val = 0;
150 else if (count.run < count.ena)
151 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
152 } else
153 count.ena = count.run = 0;
155 evsel->counts->cpu[cpu] = count;
156 return 0;
159 int __perf_evsel__read(struct perf_evsel *evsel,
160 int ncpus, int nthreads, bool scale)
162 size_t nv = scale ? 3 : 1;
163 int cpu, thread;
164 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
166 aggr->val = aggr->ena = aggr->run = 0;
168 for (cpu = 0; cpu < ncpus; cpu++) {
169 for (thread = 0; thread < nthreads; thread++) {
170 if (FD(evsel, cpu, thread) < 0)
171 continue;
173 if (readn(FD(evsel, cpu, thread),
174 &count, nv * sizeof(u64)) < 0)
175 return -errno;
177 aggr->val += count.val;
178 if (scale) {
179 aggr->ena += count.ena;
180 aggr->run += count.run;
185 evsel->counts->scaled = 0;
186 if (scale) {
187 if (aggr->run == 0) {
188 evsel->counts->scaled = -1;
189 aggr->val = 0;
190 return 0;
193 if (aggr->run < aggr->ena) {
194 evsel->counts->scaled = 1;
195 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
197 } else
198 aggr->ena = aggr->run = 0;
200 return 0;
203 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
204 struct thread_map *threads, bool group)
206 int cpu, thread;
207 unsigned long flags = 0;
208 int pid = -1;
210 if (evsel->fd == NULL &&
211 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
212 return -1;
214 if (evsel->cgrp) {
215 flags = PERF_FLAG_PID_CGROUP;
216 pid = evsel->cgrp->fd;
219 for (cpu = 0; cpu < cpus->nr; cpu++) {
220 int group_fd = -1;
222 for (thread = 0; thread < threads->nr; thread++) {
224 if (!evsel->cgrp)
225 pid = threads->map[thread];
227 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
228 pid,
229 cpus->map[cpu],
230 group_fd, flags);
231 if (FD(evsel, cpu, thread) < 0)
232 goto out_close;
234 if (group && group_fd == -1)
235 group_fd = FD(evsel, cpu, thread);
239 return 0;
241 out_close:
242 do {
243 while (--thread >= 0) {
244 close(FD(evsel, cpu, thread));
245 FD(evsel, cpu, thread) = -1;
247 thread = threads->nr;
248 } while (--cpu >= 0);
249 return -1;
252 static struct {
253 struct cpu_map map;
254 int cpus[1];
255 } empty_cpu_map = {
256 .map.nr = 1,
257 .cpus = { -1, },
260 static struct {
261 struct thread_map map;
262 int threads[1];
263 } empty_thread_map = {
264 .map.nr = 1,
265 .threads = { -1, },
268 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
269 struct thread_map *threads, bool group)
271 if (cpus == NULL) {
272 /* Work around old compiler warnings about strict aliasing */
273 cpus = &empty_cpu_map.map;
276 if (threads == NULL)
277 threads = &empty_thread_map.map;
279 return __perf_evsel__open(evsel, cpus, threads, group);
282 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
283 struct cpu_map *cpus, bool group)
285 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map, group);
288 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
289 struct thread_map *threads, bool group)
291 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads, group);
294 static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
295 struct perf_sample *sample)
297 const u64 *array = event->sample.array;
299 array += ((event->header.size -
300 sizeof(event->header)) / sizeof(u64)) - 1;
302 if (type & PERF_SAMPLE_CPU) {
303 u32 *p = (u32 *)array;
304 sample->cpu = *p;
305 array--;
308 if (type & PERF_SAMPLE_STREAM_ID) {
309 sample->stream_id = *array;
310 array--;
313 if (type & PERF_SAMPLE_ID) {
314 sample->id = *array;
315 array--;
318 if (type & PERF_SAMPLE_TIME) {
319 sample->time = *array;
320 array--;
323 if (type & PERF_SAMPLE_TID) {
324 u32 *p = (u32 *)array;
325 sample->pid = p[0];
326 sample->tid = p[1];
329 return 0;
332 static bool sample_overlap(const union perf_event *event,
333 const void *offset, u64 size)
335 const void *base = event;
337 if (offset + size > base + event->header.size)
338 return true;
340 return false;
343 int perf_event__parse_sample(const union perf_event *event, u64 type,
344 int sample_size, bool sample_id_all,
345 struct perf_sample *data)
347 const u64 *array;
349 data->cpu = data->pid = data->tid = -1;
350 data->stream_id = data->id = data->time = -1ULL;
352 if (event->header.type != PERF_RECORD_SAMPLE) {
353 if (!sample_id_all)
354 return 0;
355 return perf_event__parse_id_sample(event, type, data);
358 array = event->sample.array;
360 if (sample_size + sizeof(event->header) > event->header.size)
361 return -EFAULT;
363 if (type & PERF_SAMPLE_IP) {
364 data->ip = event->ip.ip;
365 array++;
368 if (type & PERF_SAMPLE_TID) {
369 u32 *p = (u32 *)array;
370 data->pid = p[0];
371 data->tid = p[1];
372 array++;
375 if (type & PERF_SAMPLE_TIME) {
376 data->time = *array;
377 array++;
380 if (type & PERF_SAMPLE_ADDR) {
381 data->addr = *array;
382 array++;
385 data->id = -1ULL;
386 if (type & PERF_SAMPLE_ID) {
387 data->id = *array;
388 array++;
391 if (type & PERF_SAMPLE_STREAM_ID) {
392 data->stream_id = *array;
393 array++;
396 if (type & PERF_SAMPLE_CPU) {
397 u32 *p = (u32 *)array;
398 data->cpu = *p;
399 array++;
402 if (type & PERF_SAMPLE_PERIOD) {
403 data->period = *array;
404 array++;
407 if (type & PERF_SAMPLE_READ) {
408 fprintf(stderr, "PERF_SAMPLE_READ is unsuported for now\n");
409 return -1;
412 if (type & PERF_SAMPLE_CALLCHAIN) {
413 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
414 return -EFAULT;
416 data->callchain = (struct ip_callchain *)array;
418 if (sample_overlap(event, array, data->callchain->nr))
419 return -EFAULT;
421 array += 1 + data->callchain->nr;
424 if (type & PERF_SAMPLE_RAW) {
425 u32 *p = (u32 *)array;
427 if (sample_overlap(event, array, sizeof(u32)))
428 return -EFAULT;
430 data->raw_size = *p;
431 p++;
433 if (sample_overlap(event, p, data->raw_size))
434 return -EFAULT;
436 data->raw_data = p;
439 return 0;