perf tools: Allow handling a NULL cpu_map as meaning "all cpus"
[deliverable/linux.git] / tools / perf / util / evsel.c
CommitLineData
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
936be503 10#include <byteswap.h>
0f6a3015 11#include <linux/bitops.h>
936be503 12#include "asm/bug.h"
efd2b924 13#include "debugfs.h"
5555ded4 14#include "event-parse.h"
69aad6f1 15#include "evsel.h"
70082dd9 16#include "evlist.h"
69aad6f1 17#include "util.h"
86bd5e86 18#include "cpumap.h"
fd78260b 19#include "thread_map.h"
12864b31 20#include "target.h"
287e74aa 21#include "../../../include/linux/hw_breakpoint.h"
26d33022
JO
22#include "../../include/linux/perf_event.h"
23#include "perf_regs.h"
69aad6f1 24
c52b12ed
ACM
25#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
26
bde09467 27static int __perf_evsel__sample_size(u64 sample_type)
c2a70653
ACM
28{
29 u64 mask = sample_type & PERF_SAMPLE_MASK;
30 int size = 0;
31 int i;
32
33 for (i = 0; i < 64; i++) {
34 if (mask & (1ULL << i))
35 size++;
36 }
37
38 size *= sizeof(u64);
39
40 return size;
41}
42
4bf9ce1b 43void hists__init(struct hists *hists)
0e2a5f10
ACM
44{
45 memset(hists, 0, sizeof(*hists));
46 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
47 hists->entries_in = &hists->entries_in_array[0];
48 hists->entries_collapsed = RB_ROOT;
49 hists->entries = RB_ROOT;
50 pthread_mutex_init(&hists->lock, NULL);
51}
52
ef1d1af2
ACM
53void perf_evsel__init(struct perf_evsel *evsel,
54 struct perf_event_attr *attr, int idx)
55{
56 evsel->idx = idx;
57 evsel->attr = *attr;
58 INIT_LIST_HEAD(&evsel->node);
1980c2eb 59 hists__init(&evsel->hists);
bde09467 60 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
ef1d1af2
ACM
61}
62
23a2f3ab 63struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
69aad6f1
ACM
64{
65 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
66
ef1d1af2
ACM
67 if (evsel != NULL)
68 perf_evsel__init(evsel, attr, idx);
69aad6f1
ACM
69
70 return evsel;
71}
72
efd2b924
ACM
73static struct event_format *event_format__new(const char *sys, const char *name)
74{
75 int fd, n;
76 char *filename;
77 void *bf = NULL, *nbf;
78 size_t size = 0, alloc_size = 0;
79 struct event_format *format = NULL;
80
81 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
82 goto out;
83
84 fd = open(filename, O_RDONLY);
85 if (fd < 0)
86 goto out_free_filename;
87
88 do {
89 if (size == alloc_size) {
90 alloc_size += BUFSIZ;
91 nbf = realloc(bf, alloc_size);
92 if (nbf == NULL)
93 goto out_free_bf;
94 bf = nbf;
95 }
96
97 n = read(fd, bf + size, BUFSIZ);
98 if (n < 0)
99 goto out_free_bf;
100 size += n;
101 } while (n > 0);
102
103 pevent_parse_format(&format, bf, size, sys);
104
105out_free_bf:
106 free(bf);
107 close(fd);
108out_free_filename:
109 free(filename);
110out:
111 return format;
112}
113
114struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
115{
116 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
117
118 if (evsel != NULL) {
119 struct perf_event_attr attr = {
0b80f8b3
ACM
120 .type = PERF_TYPE_TRACEPOINT,
121 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
122 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
efd2b924
ACM
123 };
124
125 evsel->tp_format = event_format__new(sys, name);
126 if (evsel->tp_format == NULL)
127 goto out_free;
128
0b80f8b3 129 event_attr_init(&attr);
efd2b924 130 attr.config = evsel->tp_format->id;
0b80f8b3 131 attr.sample_period = 1;
efd2b924
ACM
132 perf_evsel__init(evsel, &attr, idx);
133 evsel->name = evsel->tp_format->name;
134 }
135
136 return evsel;
137
138out_free:
139 free(evsel);
140 return NULL;
141}
142
8ad7013b 143const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
c410431c
ACM
144 "cycles",
145 "instructions",
146 "cache-references",
147 "cache-misses",
148 "branches",
149 "branch-misses",
150 "bus-cycles",
151 "stalled-cycles-frontend",
152 "stalled-cycles-backend",
153 "ref-cycles",
154};
155
dd4f5223 156static const char *__perf_evsel__hw_name(u64 config)
c410431c
ACM
157{
158 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
159 return perf_evsel__hw_names[config];
160
161 return "unknown-hardware";
162}
163
27f18617 164static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
c410431c 165{
27f18617 166 int colon = 0, r = 0;
c410431c 167 struct perf_event_attr *attr = &evsel->attr;
c410431c
ACM
168 bool exclude_guest_default = false;
169
170#define MOD_PRINT(context, mod) do { \
171 if (!attr->exclude_##context) { \
27f18617 172 if (!colon) colon = ++r; \
c410431c
ACM
173 r += scnprintf(bf + r, size - r, "%c", mod); \
174 } } while(0)
175
176 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
177 MOD_PRINT(kernel, 'k');
178 MOD_PRINT(user, 'u');
179 MOD_PRINT(hv, 'h');
180 exclude_guest_default = true;
181 }
182
183 if (attr->precise_ip) {
184 if (!colon)
27f18617 185 colon = ++r;
c410431c
ACM
186 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
187 exclude_guest_default = true;
188 }
189
190 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
191 MOD_PRINT(host, 'H');
192 MOD_PRINT(guest, 'G');
193 }
194#undef MOD_PRINT
195 if (colon)
27f18617 196 bf[colon - 1] = ':';
c410431c
ACM
197 return r;
198}
199
27f18617
ACM
200static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
201{
202 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
203 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
204}
205
8ad7013b 206const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
335c2f5d
ACM
207 "cpu-clock",
208 "task-clock",
209 "page-faults",
210 "context-switches",
8ad7013b 211 "cpu-migrations",
335c2f5d
ACM
212 "minor-faults",
213 "major-faults",
214 "alignment-faults",
215 "emulation-faults",
216};
217
dd4f5223 218static const char *__perf_evsel__sw_name(u64 config)
335c2f5d
ACM
219{
220 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
221 return perf_evsel__sw_names[config];
222 return "unknown-software";
223}
224
225static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
226{
227 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
228 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
229}
230
287e74aa
JO
231static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
232{
233 int r;
234
235 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
236
237 if (type & HW_BREAKPOINT_R)
238 r += scnprintf(bf + r, size - r, "r");
239
240 if (type & HW_BREAKPOINT_W)
241 r += scnprintf(bf + r, size - r, "w");
242
243 if (type & HW_BREAKPOINT_X)
244 r += scnprintf(bf + r, size - r, "x");
245
246 return r;
247}
248
249static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
250{
251 struct perf_event_attr *attr = &evsel->attr;
252 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
253 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
254}
255
0b668bc9
ACM
256const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
257 [PERF_EVSEL__MAX_ALIASES] = {
258 { "L1-dcache", "l1-d", "l1d", "L1-data", },
259 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
260 { "LLC", "L2", },
261 { "dTLB", "d-tlb", "Data-TLB", },
262 { "iTLB", "i-tlb", "Instruction-TLB", },
263 { "branch", "branches", "bpu", "btb", "bpc", },
264 { "node", },
265};
266
267const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
268 [PERF_EVSEL__MAX_ALIASES] = {
269 { "load", "loads", "read", },
270 { "store", "stores", "write", },
271 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
272};
273
274const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
275 [PERF_EVSEL__MAX_ALIASES] = {
276 { "refs", "Reference", "ops", "access", },
277 { "misses", "miss", },
278};
279
280#define C(x) PERF_COUNT_HW_CACHE_##x
281#define CACHE_READ (1 << C(OP_READ))
282#define CACHE_WRITE (1 << C(OP_WRITE))
283#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
284#define COP(x) (1 << x)
285
286/*
287 * cache operartion stat
288 * L1I : Read and prefetch only
289 * ITLB and BPU : Read-only
290 */
291static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
292 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
293 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
294 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
295 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
296 [C(ITLB)] = (CACHE_READ),
297 [C(BPU)] = (CACHE_READ),
298 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
299};
300
301bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
302{
303 if (perf_evsel__hw_cache_stat[type] & COP(op))
304 return true; /* valid */
305 else
306 return false; /* invalid */
307}
308
309int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
310 char *bf, size_t size)
311{
312 if (result) {
313 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
314 perf_evsel__hw_cache_op[op][0],
315 perf_evsel__hw_cache_result[result][0]);
316 }
317
318 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
319 perf_evsel__hw_cache_op[op][1]);
320}
321
dd4f5223 322static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
0b668bc9
ACM
323{
324 u8 op, result, type = (config >> 0) & 0xff;
325 const char *err = "unknown-ext-hardware-cache-type";
326
327 if (type > PERF_COUNT_HW_CACHE_MAX)
328 goto out_err;
329
330 op = (config >> 8) & 0xff;
331 err = "unknown-ext-hardware-cache-op";
332 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
333 goto out_err;
334
335 result = (config >> 16) & 0xff;
336 err = "unknown-ext-hardware-cache-result";
337 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
338 goto out_err;
339
340 err = "invalid-cache";
341 if (!perf_evsel__is_cache_op_valid(type, op))
342 goto out_err;
343
344 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
345out_err:
346 return scnprintf(bf, size, "%s", err);
347}
348
349static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
350{
351 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
352 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
353}
354
6eef3d9c
ACM
355static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
356{
357 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
358 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
359}
360
7289f83c 361const char *perf_evsel__name(struct perf_evsel *evsel)
a4460836 362{
7289f83c 363 char bf[128];
a4460836 364
7289f83c
ACM
365 if (evsel->name)
366 return evsel->name;
c410431c
ACM
367
368 switch (evsel->attr.type) {
369 case PERF_TYPE_RAW:
6eef3d9c 370 perf_evsel__raw_name(evsel, bf, sizeof(bf));
c410431c
ACM
371 break;
372
373 case PERF_TYPE_HARDWARE:
7289f83c 374 perf_evsel__hw_name(evsel, bf, sizeof(bf));
c410431c 375 break;
0b668bc9
ACM
376
377 case PERF_TYPE_HW_CACHE:
7289f83c 378 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
0b668bc9
ACM
379 break;
380
335c2f5d 381 case PERF_TYPE_SOFTWARE:
7289f83c 382 perf_evsel__sw_name(evsel, bf, sizeof(bf));
335c2f5d
ACM
383 break;
384
a4460836 385 case PERF_TYPE_TRACEPOINT:
7289f83c 386 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
a4460836
ACM
387 break;
388
287e74aa
JO
389 case PERF_TYPE_BREAKPOINT:
390 perf_evsel__bp_name(evsel, bf, sizeof(bf));
391 break;
392
c410431c 393 default:
ca1b1457
RR
394 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
395 evsel->attr.type);
a4460836 396 break;
c410431c
ACM
397 }
398
7289f83c
ACM
399 evsel->name = strdup(bf);
400
401 return evsel->name ?: "unknown";
c410431c
ACM
402}
403
5090c6ae
NK
404void perf_evsel__config(struct perf_evsel *evsel, struct perf_record_opts *opts,
405 struct perf_evsel *first)
0f82ebc4
ACM
406{
407 struct perf_event_attr *attr = &evsel->attr;
408 int track = !evsel->idx; /* only the first counter needs these */
409
5e1c81d9 410 attr->disabled = 1;
808e1226 411 attr->sample_id_all = opts->sample_id_all_missing ? 0 : 1;
0f82ebc4
ACM
412 attr->inherit = !opts->no_inherit;
413 attr->read_format = PERF_FORMAT_TOTAL_TIME_ENABLED |
414 PERF_FORMAT_TOTAL_TIME_RUNNING |
415 PERF_FORMAT_ID;
416
417 attr->sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID;
418
419 /*
420 * We default some events to a 1 default interval. But keep
421 * it a weak assumption overridable by the user.
422 */
423 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
424 opts->user_interval != ULLONG_MAX)) {
425 if (opts->freq) {
426 attr->sample_type |= PERF_SAMPLE_PERIOD;
427 attr->freq = 1;
428 attr->sample_freq = opts->freq;
429 } else {
430 attr->sample_period = opts->default_interval;
431 }
432 }
433
434 if (opts->no_samples)
435 attr->sample_freq = 0;
436
437 if (opts->inherit_stat)
438 attr->inherit_stat = 1;
439
440 if (opts->sample_address) {
441 attr->sample_type |= PERF_SAMPLE_ADDR;
442 attr->mmap_data = track;
443 }
444
26d33022 445 if (opts->call_graph) {
0f82ebc4
ACM
446 attr->sample_type |= PERF_SAMPLE_CALLCHAIN;
447
26d33022
JO
448 if (opts->call_graph == CALLCHAIN_DWARF) {
449 attr->sample_type |= PERF_SAMPLE_REGS_USER |
450 PERF_SAMPLE_STACK_USER;
451 attr->sample_regs_user = PERF_REGS_MASK;
452 attr->sample_stack_user = opts->stack_dump_size;
453 attr->exclude_callchain_user = 1;
454 }
455 }
456
e40ee742 457 if (perf_target__has_cpu(&opts->target))
0f82ebc4
ACM
458 attr->sample_type |= PERF_SAMPLE_CPU;
459
3e76ac78
AV
460 if (opts->period)
461 attr->sample_type |= PERF_SAMPLE_PERIOD;
462
808e1226 463 if (!opts->sample_id_all_missing &&
d67356e7 464 (opts->sample_time || !opts->no_inherit ||
aa22dd49 465 perf_target__has_cpu(&opts->target)))
0f82ebc4
ACM
466 attr->sample_type |= PERF_SAMPLE_TIME;
467
468 if (opts->raw_samples) {
469 attr->sample_type |= PERF_SAMPLE_TIME;
470 attr->sample_type |= PERF_SAMPLE_RAW;
471 attr->sample_type |= PERF_SAMPLE_CPU;
472 }
473
474 if (opts->no_delay) {
475 attr->watermark = 0;
476 attr->wakeup_events = 1;
477 }
bdfebd84
RAV
478 if (opts->branch_stack) {
479 attr->sample_type |= PERF_SAMPLE_BRANCH_STACK;
480 attr->branch_sample_type = opts->branch_stack;
481 }
0f82ebc4
ACM
482
483 attr->mmap = track;
484 attr->comm = track;
485
d67356e7
NK
486 if (perf_target__none(&opts->target) &&
487 (!opts->group || evsel == first)) {
0f82ebc4
ACM
488 attr->enable_on_exec = 1;
489 }
490}
491
69aad6f1
ACM
492int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
493{
4af4c955 494 int cpu, thread;
69aad6f1 495 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
4af4c955
DA
496
497 if (evsel->fd) {
498 for (cpu = 0; cpu < ncpus; cpu++) {
499 for (thread = 0; thread < nthreads; thread++) {
500 FD(evsel, cpu, thread) = -1;
501 }
502 }
503 }
504
69aad6f1
ACM
505 return evsel->fd != NULL ? 0 : -ENOMEM;
506}
507
70db7533
ACM
508int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
509{
a91e5431
ACM
510 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
511 if (evsel->sample_id == NULL)
512 return -ENOMEM;
513
514 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
515 if (evsel->id == NULL) {
516 xyarray__delete(evsel->sample_id);
517 evsel->sample_id = NULL;
518 return -ENOMEM;
519 }
520
521 return 0;
70db7533
ACM
522}
523
c52b12ed
ACM
524int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
525{
526 evsel->counts = zalloc((sizeof(*evsel->counts) +
527 (ncpus * sizeof(struct perf_counts_values))));
528 return evsel->counts != NULL ? 0 : -ENOMEM;
529}
530
69aad6f1
ACM
531void perf_evsel__free_fd(struct perf_evsel *evsel)
532{
533 xyarray__delete(evsel->fd);
534 evsel->fd = NULL;
535}
536
70db7533
ACM
537void perf_evsel__free_id(struct perf_evsel *evsel)
538{
a91e5431
ACM
539 xyarray__delete(evsel->sample_id);
540 evsel->sample_id = NULL;
541 free(evsel->id);
70db7533
ACM
542 evsel->id = NULL;
543}
544
c52b12ed
ACM
545void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
546{
547 int cpu, thread;
548
549 for (cpu = 0; cpu < ncpus; cpu++)
550 for (thread = 0; thread < nthreads; ++thread) {
551 close(FD(evsel, cpu, thread));
552 FD(evsel, cpu, thread) = -1;
553 }
554}
555
ef1d1af2 556void perf_evsel__exit(struct perf_evsel *evsel)
69aad6f1
ACM
557{
558 assert(list_empty(&evsel->node));
559 xyarray__delete(evsel->fd);
a91e5431
ACM
560 xyarray__delete(evsel->sample_id);
561 free(evsel->id);
ef1d1af2
ACM
562}
563
564void perf_evsel__delete(struct perf_evsel *evsel)
565{
566 perf_evsel__exit(evsel);
023695d9 567 close_cgroup(evsel->cgrp);
6a4bb04c 568 free(evsel->group_name);
efd2b924
ACM
569 if (evsel->tp_format && evsel->name == evsel->tp_format->name) {
570 evsel->name = NULL;
571 pevent_free_format(evsel->tp_format);
572 }
f0c55bcf 573 free(evsel->name);
69aad6f1
ACM
574 free(evsel);
575}
c52b12ed
ACM
576
577int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
578 int cpu, int thread, bool scale)
579{
580 struct perf_counts_values count;
581 size_t nv = scale ? 3 : 1;
582
583 if (FD(evsel, cpu, thread) < 0)
584 return -EINVAL;
585
4eed11d5
ACM
586 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
587 return -ENOMEM;
588
c52b12ed
ACM
589 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
590 return -errno;
591
592 if (scale) {
593 if (count.run == 0)
594 count.val = 0;
595 else if (count.run < count.ena)
596 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
597 } else
598 count.ena = count.run = 0;
599
600 evsel->counts->cpu[cpu] = count;
601 return 0;
602}
603
604int __perf_evsel__read(struct perf_evsel *evsel,
605 int ncpus, int nthreads, bool scale)
606{
607 size_t nv = scale ? 3 : 1;
608 int cpu, thread;
609 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
610
52bcd994 611 aggr->val = aggr->ena = aggr->run = 0;
c52b12ed
ACM
612
613 for (cpu = 0; cpu < ncpus; cpu++) {
614 for (thread = 0; thread < nthreads; thread++) {
615 if (FD(evsel, cpu, thread) < 0)
616 continue;
617
618 if (readn(FD(evsel, cpu, thread),
619 &count, nv * sizeof(u64)) < 0)
620 return -errno;
621
622 aggr->val += count.val;
623 if (scale) {
624 aggr->ena += count.ena;
625 aggr->run += count.run;
626 }
627 }
628 }
629
630 evsel->counts->scaled = 0;
631 if (scale) {
632 if (aggr->run == 0) {
633 evsel->counts->scaled = -1;
634 aggr->val = 0;
635 return 0;
636 }
637
638 if (aggr->run < aggr->ena) {
639 evsel->counts->scaled = 1;
640 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
641 }
642 } else
643 aggr->ena = aggr->run = 0;
644
645 return 0;
646}
48290609 647
6a4bb04c
JO
648static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
649{
650 struct perf_evsel *leader = evsel->leader;
651 int fd;
652
653 if (!leader)
654 return -1;
655
656 /*
657 * Leader must be already processed/open,
658 * if not it's a bug.
659 */
660 BUG_ON(!leader->fd);
661
662 fd = FD(leader, cpu, thread);
663 BUG_ON(fd == -1);
664
665 return fd;
666}
667
0252208e 668static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 669 struct thread_map *threads)
48290609 670{
0252208e 671 int cpu, thread;
023695d9 672 unsigned long flags = 0;
727ab04e 673 int pid = -1, err;
48290609 674
0252208e
ACM
675 if (evsel->fd == NULL &&
676 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
727ab04e 677 return -ENOMEM;
4eed11d5 678
023695d9
SE
679 if (evsel->cgrp) {
680 flags = PERF_FLAG_PID_CGROUP;
681 pid = evsel->cgrp->fd;
682 }
683
86bd5e86 684 for (cpu = 0; cpu < cpus->nr; cpu++) {
9d04f178 685
0252208e 686 for (thread = 0; thread < threads->nr; thread++) {
6a4bb04c 687 int group_fd;
023695d9
SE
688
689 if (!evsel->cgrp)
690 pid = threads->map[thread];
691
6a4bb04c
JO
692 group_fd = get_group_fd(evsel, cpu, thread);
693
0252208e 694 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
023695d9 695 pid,
f08199d3 696 cpus->map[cpu],
023695d9 697 group_fd, flags);
727ab04e
ACM
698 if (FD(evsel, cpu, thread) < 0) {
699 err = -errno;
0252208e 700 goto out_close;
727ab04e 701 }
0252208e 702 }
48290609
ACM
703 }
704
705 return 0;
706
707out_close:
0252208e
ACM
708 do {
709 while (--thread >= 0) {
710 close(FD(evsel, cpu, thread));
711 FD(evsel, cpu, thread) = -1;
712 }
713 thread = threads->nr;
714 } while (--cpu >= 0);
727ab04e
ACM
715 return err;
716}
717
718void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
719{
720 if (evsel->fd == NULL)
721 return;
722
723 perf_evsel__close_fd(evsel, ncpus, nthreads);
724 perf_evsel__free_fd(evsel);
725 evsel->fd = NULL;
48290609
ACM
726}
727
0252208e
ACM
728static struct {
729 struct cpu_map map;
730 int cpus[1];
731} empty_cpu_map = {
732 .map.nr = 1,
733 .cpus = { -1, },
734};
735
736static struct {
737 struct thread_map map;
738 int threads[1];
739} empty_thread_map = {
740 .map.nr = 1,
741 .threads = { -1, },
742};
743
f08199d3 744int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 745 struct thread_map *threads)
48290609 746{
0252208e
ACM
747 if (cpus == NULL) {
748 /* Work around old compiler warnings about strict aliasing */
749 cpus = &empty_cpu_map.map;
48290609
ACM
750 }
751
0252208e
ACM
752 if (threads == NULL)
753 threads = &empty_thread_map.map;
48290609 754
6a4bb04c 755 return __perf_evsel__open(evsel, cpus, threads);
48290609
ACM
756}
757
f08199d3 758int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
6a4bb04c 759 struct cpu_map *cpus)
48290609 760{
6a4bb04c 761 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
0252208e 762}
48290609 763
f08199d3 764int perf_evsel__open_per_thread(struct perf_evsel *evsel,
6a4bb04c 765 struct thread_map *threads)
0252208e 766{
6a4bb04c 767 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
48290609 768}
70082dd9 769
8115d60c 770static int perf_event__parse_id_sample(const union perf_event *event, u64 type,
37073f9e
JO
771 struct perf_sample *sample,
772 bool swapped)
d0dd74e8
ACM
773{
774 const u64 *array = event->sample.array;
37073f9e 775 union u64_swap u;
d0dd74e8
ACM
776
777 array += ((event->header.size -
778 sizeof(event->header)) / sizeof(u64)) - 1;
779
780 if (type & PERF_SAMPLE_CPU) {
37073f9e
JO
781 u.val64 = *array;
782 if (swapped) {
783 /* undo swap of u64, then swap on individual u32s */
784 u.val64 = bswap_64(u.val64);
785 u.val32[0] = bswap_32(u.val32[0]);
786 }
787
788 sample->cpu = u.val32[0];
d0dd74e8
ACM
789 array--;
790 }
791
792 if (type & PERF_SAMPLE_STREAM_ID) {
793 sample->stream_id = *array;
794 array--;
795 }
796
797 if (type & PERF_SAMPLE_ID) {
798 sample->id = *array;
799 array--;
800 }
801
802 if (type & PERF_SAMPLE_TIME) {
803 sample->time = *array;
804 array--;
805 }
806
807 if (type & PERF_SAMPLE_TID) {
37073f9e
JO
808 u.val64 = *array;
809 if (swapped) {
810 /* undo swap of u64, then swap on individual u32s */
811 u.val64 = bswap_64(u.val64);
812 u.val32[0] = bswap_32(u.val32[0]);
813 u.val32[1] = bswap_32(u.val32[1]);
814 }
815
816 sample->pid = u.val32[0];
817 sample->tid = u.val32[1];
d0dd74e8
ACM
818 }
819
820 return 0;
821}
822
98e1da90
FW
823static bool sample_overlap(const union perf_event *event,
824 const void *offset, u64 size)
825{
826 const void *base = event;
827
828 if (offset + size > base + event->header.size)
829 return true;
830
831 return false;
832}
833
a3f698fe 834int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
936be503 835 struct perf_sample *data, bool swapped)
d0dd74e8 836{
a3f698fe 837 u64 type = evsel->attr.sample_type;
0f6a3015 838 u64 regs_user = evsel->attr.sample_regs_user;
d0dd74e8
ACM
839 const u64 *array;
840
936be503
DA
841 /*
842 * used for cross-endian analysis. See git commit 65014ab3
843 * for why this goofiness is needed.
844 */
6a11f92e 845 union u64_swap u;
936be503 846
f3bda2c9 847 memset(data, 0, sizeof(*data));
d0dd74e8
ACM
848 data->cpu = data->pid = data->tid = -1;
849 data->stream_id = data->id = data->time = -1ULL;
a4a03fc7 850 data->period = 1;
d0dd74e8
ACM
851
852 if (event->header.type != PERF_RECORD_SAMPLE) {
a3f698fe 853 if (!evsel->attr.sample_id_all)
d0dd74e8 854 return 0;
37073f9e 855 return perf_event__parse_id_sample(event, type, data, swapped);
d0dd74e8
ACM
856 }
857
858 array = event->sample.array;
859
a3f698fe 860 if (evsel->sample_size + sizeof(event->header) > event->header.size)
a2854124
FW
861 return -EFAULT;
862
d0dd74e8
ACM
863 if (type & PERF_SAMPLE_IP) {
864 data->ip = event->ip.ip;
865 array++;
866 }
867
868 if (type & PERF_SAMPLE_TID) {
936be503
DA
869 u.val64 = *array;
870 if (swapped) {
871 /* undo swap of u64, then swap on individual u32s */
872 u.val64 = bswap_64(u.val64);
873 u.val32[0] = bswap_32(u.val32[0]);
874 u.val32[1] = bswap_32(u.val32[1]);
875 }
876
877 data->pid = u.val32[0];
878 data->tid = u.val32[1];
d0dd74e8
ACM
879 array++;
880 }
881
882 if (type & PERF_SAMPLE_TIME) {
883 data->time = *array;
884 array++;
885 }
886
7cec0922 887 data->addr = 0;
d0dd74e8
ACM
888 if (type & PERF_SAMPLE_ADDR) {
889 data->addr = *array;
890 array++;
891 }
892
893 data->id = -1ULL;
894 if (type & PERF_SAMPLE_ID) {
895 data->id = *array;
896 array++;
897 }
898
899 if (type & PERF_SAMPLE_STREAM_ID) {
900 data->stream_id = *array;
901 array++;
902 }
903
904 if (type & PERF_SAMPLE_CPU) {
936be503
DA
905
906 u.val64 = *array;
907 if (swapped) {
908 /* undo swap of u64, then swap on individual u32s */
909 u.val64 = bswap_64(u.val64);
910 u.val32[0] = bswap_32(u.val32[0]);
911 }
912
913 data->cpu = u.val32[0];
d0dd74e8
ACM
914 array++;
915 }
916
917 if (type & PERF_SAMPLE_PERIOD) {
918 data->period = *array;
919 array++;
920 }
921
922 if (type & PERF_SAMPLE_READ) {
f9d36996 923 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
d0dd74e8
ACM
924 return -1;
925 }
926
927 if (type & PERF_SAMPLE_CALLCHAIN) {
98e1da90
FW
928 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
929 return -EFAULT;
930
d0dd74e8 931 data->callchain = (struct ip_callchain *)array;
98e1da90
FW
932
933 if (sample_overlap(event, array, data->callchain->nr))
934 return -EFAULT;
935
d0dd74e8
ACM
936 array += 1 + data->callchain->nr;
937 }
938
939 if (type & PERF_SAMPLE_RAW) {
8e303f20
JO
940 const u64 *pdata;
941
936be503
DA
942 u.val64 = *array;
943 if (WARN_ONCE(swapped,
944 "Endianness of raw data not corrected!\n")) {
945 /* undo swap of u64, then swap on individual u32s */
946 u.val64 = bswap_64(u.val64);
947 u.val32[0] = bswap_32(u.val32[0]);
948 u.val32[1] = bswap_32(u.val32[1]);
949 }
98e1da90
FW
950
951 if (sample_overlap(event, array, sizeof(u32)))
952 return -EFAULT;
953
936be503 954 data->raw_size = u.val32[0];
8e303f20 955 pdata = (void *) array + sizeof(u32);
98e1da90 956
8e303f20 957 if (sample_overlap(event, pdata, data->raw_size))
98e1da90
FW
958 return -EFAULT;
959
8e303f20 960 data->raw_data = (void *) pdata;
fa30c964
SE
961
962 array = (void *)array + data->raw_size + sizeof(u32);
d0dd74e8
ACM
963 }
964
b5387528
RAV
965 if (type & PERF_SAMPLE_BRANCH_STACK) {
966 u64 sz;
967
968 data->branch_stack = (struct branch_stack *)array;
969 array++; /* nr */
970
971 sz = data->branch_stack->nr * sizeof(struct branch_entry);
972 sz /= sizeof(u64);
973 array += sz;
974 }
0f6a3015
JO
975
976 if (type & PERF_SAMPLE_REGS_USER) {
977 /* First u64 tells us if we have any regs in sample. */
978 u64 avail = *array++;
979
980 if (avail) {
981 data->user_regs.regs = (u64 *)array;
982 array += hweight_long(regs_user);
983 }
984 }
985
986 if (type & PERF_SAMPLE_STACK_USER) {
987 u64 size = *array++;
988
989 data->user_stack.offset = ((char *)(array - 1)
990 - (char *) event);
991
992 if (!size) {
993 data->user_stack.size = 0;
994 } else {
995 data->user_stack.data = (char *)array;
996 array += size / sizeof(*array);
997 data->user_stack.size = *array;
998 }
999 }
1000
d0dd74e8
ACM
1001 return 0;
1002}
74eec26f
AV
1003
1004int perf_event__synthesize_sample(union perf_event *event, u64 type,
1005 const struct perf_sample *sample,
1006 bool swapped)
1007{
1008 u64 *array;
1009
1010 /*
1011 * used for cross-endian analysis. See git commit 65014ab3
1012 * for why this goofiness is needed.
1013 */
6a11f92e 1014 union u64_swap u;
74eec26f
AV
1015
1016 array = event->sample.array;
1017
1018 if (type & PERF_SAMPLE_IP) {
1019 event->ip.ip = sample->ip;
1020 array++;
1021 }
1022
1023 if (type & PERF_SAMPLE_TID) {
1024 u.val32[0] = sample->pid;
1025 u.val32[1] = sample->tid;
1026 if (swapped) {
1027 /*
a3f698fe 1028 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
1029 */
1030 u.val32[0] = bswap_32(u.val32[0]);
1031 u.val32[1] = bswap_32(u.val32[1]);
1032 u.val64 = bswap_64(u.val64);
1033 }
1034
1035 *array = u.val64;
1036 array++;
1037 }
1038
1039 if (type & PERF_SAMPLE_TIME) {
1040 *array = sample->time;
1041 array++;
1042 }
1043
1044 if (type & PERF_SAMPLE_ADDR) {
1045 *array = sample->addr;
1046 array++;
1047 }
1048
1049 if (type & PERF_SAMPLE_ID) {
1050 *array = sample->id;
1051 array++;
1052 }
1053
1054 if (type & PERF_SAMPLE_STREAM_ID) {
1055 *array = sample->stream_id;
1056 array++;
1057 }
1058
1059 if (type & PERF_SAMPLE_CPU) {
1060 u.val32[0] = sample->cpu;
1061 if (swapped) {
1062 /*
a3f698fe 1063 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
1064 */
1065 u.val32[0] = bswap_32(u.val32[0]);
1066 u.val64 = bswap_64(u.val64);
1067 }
1068 *array = u.val64;
1069 array++;
1070 }
1071
1072 if (type & PERF_SAMPLE_PERIOD) {
1073 *array = sample->period;
1074 array++;
1075 }
1076
1077 return 0;
1078}
5555ded4 1079
efd2b924
ACM
1080struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1081{
1082 return pevent_find_field(evsel->tp_format, name);
1083}
1084
5555ded4
ACM
1085char *perf_evsel__strval(struct perf_evsel *evsel, struct perf_sample *sample,
1086 const char *name)
1087{
efd2b924 1088 struct format_field *field = perf_evsel__field(evsel, name);
5555ded4
ACM
1089 int offset;
1090
efd2b924
ACM
1091 if (!field)
1092 return NULL;
5555ded4
ACM
1093
1094 offset = field->offset;
1095
1096 if (field->flags & FIELD_IS_DYNAMIC) {
1097 offset = *(int *)(sample->raw_data + field->offset);
1098 offset &= 0xffff;
1099 }
1100
1101 return sample->raw_data + offset;
1102}
1103
1104u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1105 const char *name)
1106{
efd2b924 1107 struct format_field *field = perf_evsel__field(evsel, name);
5555ded4
ACM
1108 u64 val;
1109
efd2b924
ACM
1110 if (!field)
1111 return 0;
5555ded4
ACM
1112
1113 val = pevent_read_number(evsel->tp_format->pevent,
1114 sample->raw_data + field->offset, field->size);
1115 return val;
1116
1117}
This page took 0.132028 seconds and 5 git commands to generate.