perf evsel: Add prev_raw_count field
[linux-2.6-block.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"
d2709c7c
DH
21#include <linux/hw_breakpoint.h>
22#include <linux/perf_event.h>
26d33022 23#include "perf_regs.h"
69aad6f1 24
594ac61a
ACM
25static struct {
26 bool sample_id_all;
27 bool exclude_guest;
28} perf_missing_features;
29
c52b12ed
ACM
30#define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
31
bde09467 32static int __perf_evsel__sample_size(u64 sample_type)
c2a70653
ACM
33{
34 u64 mask = sample_type & PERF_SAMPLE_MASK;
35 int size = 0;
36 int i;
37
38 for (i = 0; i < 64; i++) {
39 if (mask & (1ULL << i))
40 size++;
41 }
42
43 size *= sizeof(u64);
44
45 return size;
46}
47
4bf9ce1b 48void hists__init(struct hists *hists)
0e2a5f10
ACM
49{
50 memset(hists, 0, sizeof(*hists));
51 hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
52 hists->entries_in = &hists->entries_in_array[0];
53 hists->entries_collapsed = RB_ROOT;
54 hists->entries = RB_ROOT;
55 pthread_mutex_init(&hists->lock, NULL);
56}
57
7be5ebe8
ACM
58void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
59 enum perf_event_sample_format bit)
60{
61 if (!(evsel->attr.sample_type & bit)) {
62 evsel->attr.sample_type |= bit;
63 evsel->sample_size += sizeof(u64);
64 }
65}
66
67void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
68 enum perf_event_sample_format bit)
69{
70 if (evsel->attr.sample_type & bit) {
71 evsel->attr.sample_type &= ~bit;
72 evsel->sample_size -= sizeof(u64);
73 }
74}
75
7a5a5ca5
ACM
76void perf_evsel__set_sample_id(struct perf_evsel *evsel)
77{
78 perf_evsel__set_sample_bit(evsel, ID);
79 evsel->attr.read_format |= PERF_FORMAT_ID;
80}
81
ef1d1af2
ACM
82void perf_evsel__init(struct perf_evsel *evsel,
83 struct perf_event_attr *attr, int idx)
84{
85 evsel->idx = idx;
86 evsel->attr = *attr;
2cfda562 87 evsel->leader = evsel;
ef1d1af2 88 INIT_LIST_HEAD(&evsel->node);
1980c2eb 89 hists__init(&evsel->hists);
bde09467 90 evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
ef1d1af2
ACM
91}
92
23a2f3ab 93struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
69aad6f1
ACM
94{
95 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
96
ef1d1af2
ACM
97 if (evsel != NULL)
98 perf_evsel__init(evsel, attr, idx);
69aad6f1
ACM
99
100 return evsel;
101}
102
201b7334 103struct event_format *event_format__new(const char *sys, const char *name)
efd2b924
ACM
104{
105 int fd, n;
106 char *filename;
107 void *bf = NULL, *nbf;
108 size_t size = 0, alloc_size = 0;
109 struct event_format *format = NULL;
110
111 if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
112 goto out;
113
114 fd = open(filename, O_RDONLY);
115 if (fd < 0)
116 goto out_free_filename;
117
118 do {
119 if (size == alloc_size) {
120 alloc_size += BUFSIZ;
121 nbf = realloc(bf, alloc_size);
122 if (nbf == NULL)
123 goto out_free_bf;
124 bf = nbf;
125 }
126
127 n = read(fd, bf + size, BUFSIZ);
128 if (n < 0)
129 goto out_free_bf;
130 size += n;
131 } while (n > 0);
132
133 pevent_parse_format(&format, bf, size, sys);
134
135out_free_bf:
136 free(bf);
137 close(fd);
138out_free_filename:
139 free(filename);
140out:
141 return format;
142}
143
144struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
145{
146 struct perf_evsel *evsel = zalloc(sizeof(*evsel));
147
148 if (evsel != NULL) {
149 struct perf_event_attr attr = {
0b80f8b3
ACM
150 .type = PERF_TYPE_TRACEPOINT,
151 .sample_type = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
152 PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
efd2b924
ACM
153 };
154
e48ffe2b
ACM
155 if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
156 goto out_free;
157
efd2b924
ACM
158 evsel->tp_format = event_format__new(sys, name);
159 if (evsel->tp_format == NULL)
160 goto out_free;
161
0b80f8b3 162 event_attr_init(&attr);
efd2b924 163 attr.config = evsel->tp_format->id;
0b80f8b3 164 attr.sample_period = 1;
efd2b924 165 perf_evsel__init(evsel, &attr, idx);
efd2b924
ACM
166 }
167
168 return evsel;
169
170out_free:
e48ffe2b 171 free(evsel->name);
efd2b924
ACM
172 free(evsel);
173 return NULL;
174}
175
8ad7013b 176const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
c410431c
ACM
177 "cycles",
178 "instructions",
179 "cache-references",
180 "cache-misses",
181 "branches",
182 "branch-misses",
183 "bus-cycles",
184 "stalled-cycles-frontend",
185 "stalled-cycles-backend",
186 "ref-cycles",
187};
188
dd4f5223 189static const char *__perf_evsel__hw_name(u64 config)
c410431c
ACM
190{
191 if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
192 return perf_evsel__hw_names[config];
193
194 return "unknown-hardware";
195}
196
27f18617 197static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
c410431c 198{
27f18617 199 int colon = 0, r = 0;
c410431c 200 struct perf_event_attr *attr = &evsel->attr;
c410431c
ACM
201 bool exclude_guest_default = false;
202
203#define MOD_PRINT(context, mod) do { \
204 if (!attr->exclude_##context) { \
27f18617 205 if (!colon) colon = ++r; \
c410431c
ACM
206 r += scnprintf(bf + r, size - r, "%c", mod); \
207 } } while(0)
208
209 if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
210 MOD_PRINT(kernel, 'k');
211 MOD_PRINT(user, 'u');
212 MOD_PRINT(hv, 'h');
213 exclude_guest_default = true;
214 }
215
216 if (attr->precise_ip) {
217 if (!colon)
27f18617 218 colon = ++r;
c410431c
ACM
219 r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
220 exclude_guest_default = true;
221 }
222
223 if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
224 MOD_PRINT(host, 'H');
225 MOD_PRINT(guest, 'G');
226 }
227#undef MOD_PRINT
228 if (colon)
27f18617 229 bf[colon - 1] = ':';
c410431c
ACM
230 return r;
231}
232
27f18617
ACM
233static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
234{
235 int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
236 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
237}
238
8ad7013b 239const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
335c2f5d
ACM
240 "cpu-clock",
241 "task-clock",
242 "page-faults",
243 "context-switches",
8ad7013b 244 "cpu-migrations",
335c2f5d
ACM
245 "minor-faults",
246 "major-faults",
247 "alignment-faults",
248 "emulation-faults",
249};
250
dd4f5223 251static const char *__perf_evsel__sw_name(u64 config)
335c2f5d
ACM
252{
253 if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
254 return perf_evsel__sw_names[config];
255 return "unknown-software";
256}
257
258static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
259{
260 int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
261 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
262}
263
287e74aa
JO
264static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
265{
266 int r;
267
268 r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
269
270 if (type & HW_BREAKPOINT_R)
271 r += scnprintf(bf + r, size - r, "r");
272
273 if (type & HW_BREAKPOINT_W)
274 r += scnprintf(bf + r, size - r, "w");
275
276 if (type & HW_BREAKPOINT_X)
277 r += scnprintf(bf + r, size - r, "x");
278
279 return r;
280}
281
282static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
283{
284 struct perf_event_attr *attr = &evsel->attr;
285 int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
286 return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
287}
288
0b668bc9
ACM
289const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
290 [PERF_EVSEL__MAX_ALIASES] = {
291 { "L1-dcache", "l1-d", "l1d", "L1-data", },
292 { "L1-icache", "l1-i", "l1i", "L1-instruction", },
293 { "LLC", "L2", },
294 { "dTLB", "d-tlb", "Data-TLB", },
295 { "iTLB", "i-tlb", "Instruction-TLB", },
296 { "branch", "branches", "bpu", "btb", "bpc", },
297 { "node", },
298};
299
300const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
301 [PERF_EVSEL__MAX_ALIASES] = {
302 { "load", "loads", "read", },
303 { "store", "stores", "write", },
304 { "prefetch", "prefetches", "speculative-read", "speculative-load", },
305};
306
307const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
308 [PERF_EVSEL__MAX_ALIASES] = {
309 { "refs", "Reference", "ops", "access", },
310 { "misses", "miss", },
311};
312
313#define C(x) PERF_COUNT_HW_CACHE_##x
314#define CACHE_READ (1 << C(OP_READ))
315#define CACHE_WRITE (1 << C(OP_WRITE))
316#define CACHE_PREFETCH (1 << C(OP_PREFETCH))
317#define COP(x) (1 << x)
318
319/*
320 * cache operartion stat
321 * L1I : Read and prefetch only
322 * ITLB and BPU : Read-only
323 */
324static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
325 [C(L1D)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
326 [C(L1I)] = (CACHE_READ | CACHE_PREFETCH),
327 [C(LL)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
328 [C(DTLB)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
329 [C(ITLB)] = (CACHE_READ),
330 [C(BPU)] = (CACHE_READ),
331 [C(NODE)] = (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
332};
333
334bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
335{
336 if (perf_evsel__hw_cache_stat[type] & COP(op))
337 return true; /* valid */
338 else
339 return false; /* invalid */
340}
341
342int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
343 char *bf, size_t size)
344{
345 if (result) {
346 return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
347 perf_evsel__hw_cache_op[op][0],
348 perf_evsel__hw_cache_result[result][0]);
349 }
350
351 return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
352 perf_evsel__hw_cache_op[op][1]);
353}
354
dd4f5223 355static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
0b668bc9
ACM
356{
357 u8 op, result, type = (config >> 0) & 0xff;
358 const char *err = "unknown-ext-hardware-cache-type";
359
360 if (type > PERF_COUNT_HW_CACHE_MAX)
361 goto out_err;
362
363 op = (config >> 8) & 0xff;
364 err = "unknown-ext-hardware-cache-op";
365 if (op > PERF_COUNT_HW_CACHE_OP_MAX)
366 goto out_err;
367
368 result = (config >> 16) & 0xff;
369 err = "unknown-ext-hardware-cache-result";
370 if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
371 goto out_err;
372
373 err = "invalid-cache";
374 if (!perf_evsel__is_cache_op_valid(type, op))
375 goto out_err;
376
377 return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
378out_err:
379 return scnprintf(bf, size, "%s", err);
380}
381
382static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
383{
384 int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
385 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
386}
387
6eef3d9c
ACM
388static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
389{
390 int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
391 return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
392}
393
7289f83c 394const char *perf_evsel__name(struct perf_evsel *evsel)
a4460836 395{
7289f83c 396 char bf[128];
a4460836 397
7289f83c
ACM
398 if (evsel->name)
399 return evsel->name;
c410431c
ACM
400
401 switch (evsel->attr.type) {
402 case PERF_TYPE_RAW:
6eef3d9c 403 perf_evsel__raw_name(evsel, bf, sizeof(bf));
c410431c
ACM
404 break;
405
406 case PERF_TYPE_HARDWARE:
7289f83c 407 perf_evsel__hw_name(evsel, bf, sizeof(bf));
c410431c 408 break;
0b668bc9
ACM
409
410 case PERF_TYPE_HW_CACHE:
7289f83c 411 perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
0b668bc9
ACM
412 break;
413
335c2f5d 414 case PERF_TYPE_SOFTWARE:
7289f83c 415 perf_evsel__sw_name(evsel, bf, sizeof(bf));
335c2f5d
ACM
416 break;
417
a4460836 418 case PERF_TYPE_TRACEPOINT:
7289f83c 419 scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
a4460836
ACM
420 break;
421
287e74aa
JO
422 case PERF_TYPE_BREAKPOINT:
423 perf_evsel__bp_name(evsel, bf, sizeof(bf));
424 break;
425
c410431c 426 default:
ca1b1457
RR
427 scnprintf(bf, sizeof(bf), "unknown attr type: %d",
428 evsel->attr.type);
a4460836 429 break;
c410431c
ACM
430 }
431
7289f83c
ACM
432 evsel->name = strdup(bf);
433
434 return evsel->name ?: "unknown";
c410431c
ACM
435}
436
774cb499
JO
437/*
438 * The enable_on_exec/disabled value strategy:
439 *
440 * 1) For any type of traced program:
441 * - all independent events and group leaders are disabled
442 * - all group members are enabled
443 *
444 * Group members are ruled by group leaders. They need to
445 * be enabled, because the group scheduling relies on that.
446 *
447 * 2) For traced programs executed by perf:
448 * - all independent events and group leaders have
449 * enable_on_exec set
450 * - we don't specifically enable or disable any event during
451 * the record command
452 *
453 * Independent events and group leaders are initially disabled
454 * and get enabled by exec. Group members are ruled by group
455 * leaders as stated in 1).
456 *
457 * 3) For traced programs attached by perf (pid/tid):
458 * - we specifically enable or disable all events during
459 * the record command
460 *
461 * When attaching events to already running traced we
462 * enable/disable events specifically, as there's no
463 * initial traced exec call.
464 */
cac21425
JO
465void perf_evsel__config(struct perf_evsel *evsel,
466 struct perf_record_opts *opts)
0f82ebc4
ACM
467{
468 struct perf_event_attr *attr = &evsel->attr;
469 int track = !evsel->idx; /* only the first counter needs these */
470
594ac61a 471 attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
0f82ebc4 472 attr->inherit = !opts->no_inherit;
0f82ebc4 473
7be5ebe8
ACM
474 perf_evsel__set_sample_bit(evsel, IP);
475 perf_evsel__set_sample_bit(evsel, TID);
0f82ebc4
ACM
476
477 /*
478 * We default some events to a 1 default interval. But keep
479 * it a weak assumption overridable by the user.
480 */
481 if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
482 opts->user_interval != ULLONG_MAX)) {
483 if (opts->freq) {
7be5ebe8 484 perf_evsel__set_sample_bit(evsel, PERIOD);
0f82ebc4
ACM
485 attr->freq = 1;
486 attr->sample_freq = opts->freq;
487 } else {
488 attr->sample_period = opts->default_interval;
489 }
490 }
491
492 if (opts->no_samples)
493 attr->sample_freq = 0;
494
495 if (opts->inherit_stat)
496 attr->inherit_stat = 1;
497
498 if (opts->sample_address) {
7be5ebe8 499 perf_evsel__set_sample_bit(evsel, ADDR);
0f82ebc4
ACM
500 attr->mmap_data = track;
501 }
502
26d33022 503 if (opts->call_graph) {
7be5ebe8 504 perf_evsel__set_sample_bit(evsel, CALLCHAIN);
0f82ebc4 505
26d33022 506 if (opts->call_graph == CALLCHAIN_DWARF) {
7be5ebe8
ACM
507 perf_evsel__set_sample_bit(evsel, REGS_USER);
508 perf_evsel__set_sample_bit(evsel, STACK_USER);
26d33022
JO
509 attr->sample_regs_user = PERF_REGS_MASK;
510 attr->sample_stack_user = opts->stack_dump_size;
511 attr->exclude_callchain_user = 1;
512 }
513 }
514
e40ee742 515 if (perf_target__has_cpu(&opts->target))
7be5ebe8 516 perf_evsel__set_sample_bit(evsel, CPU);
0f82ebc4 517
3e76ac78 518 if (opts->period)
7be5ebe8 519 perf_evsel__set_sample_bit(evsel, PERIOD);
3e76ac78 520
594ac61a 521 if (!perf_missing_features.sample_id_all &&
d67356e7 522 (opts->sample_time || !opts->no_inherit ||
aa22dd49 523 perf_target__has_cpu(&opts->target)))
7be5ebe8 524 perf_evsel__set_sample_bit(evsel, TIME);
0f82ebc4
ACM
525
526 if (opts->raw_samples) {
7be5ebe8
ACM
527 perf_evsel__set_sample_bit(evsel, TIME);
528 perf_evsel__set_sample_bit(evsel, RAW);
529 perf_evsel__set_sample_bit(evsel, CPU);
0f82ebc4
ACM
530 }
531
532 if (opts->no_delay) {
533 attr->watermark = 0;
534 attr->wakeup_events = 1;
535 }
bdfebd84 536 if (opts->branch_stack) {
7be5ebe8 537 perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
bdfebd84
RAV
538 attr->branch_sample_type = opts->branch_stack;
539 }
0f82ebc4
ACM
540
541 attr->mmap = track;
542 attr->comm = track;
543
774cb499
JO
544 /*
545 * XXX see the function comment above
546 *
547 * Disabling only independent events or group leaders,
548 * keeping group members enabled.
549 */
823254ed 550 if (perf_evsel__is_group_leader(evsel))
774cb499
JO
551 attr->disabled = 1;
552
553 /*
554 * Setting enable_on_exec for independent events and
555 * group leaders for traced executed by perf.
556 */
823254ed 557 if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
0f82ebc4 558 attr->enable_on_exec = 1;
0f82ebc4
ACM
559}
560
69aad6f1
ACM
561int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
562{
4af4c955 563 int cpu, thread;
69aad6f1 564 evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
4af4c955
DA
565
566 if (evsel->fd) {
567 for (cpu = 0; cpu < ncpus; cpu++) {
568 for (thread = 0; thread < nthreads; thread++) {
569 FD(evsel, cpu, thread) = -1;
570 }
571 }
572 }
573
69aad6f1
ACM
574 return evsel->fd != NULL ? 0 : -ENOMEM;
575}
576
745cefc5
ACM
577int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
578 const char *filter)
579{
580 int cpu, thread;
581
582 for (cpu = 0; cpu < ncpus; cpu++) {
583 for (thread = 0; thread < nthreads; thread++) {
584 int fd = FD(evsel, cpu, thread),
585 err = ioctl(fd, PERF_EVENT_IOC_SET_FILTER, filter);
586
587 if (err)
588 return err;
589 }
590 }
591
592 return 0;
593}
594
70db7533
ACM
595int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
596{
a91e5431
ACM
597 evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
598 if (evsel->sample_id == NULL)
599 return -ENOMEM;
600
601 evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
602 if (evsel->id == NULL) {
603 xyarray__delete(evsel->sample_id);
604 evsel->sample_id = NULL;
605 return -ENOMEM;
606 }
607
608 return 0;
70db7533
ACM
609}
610
c52b12ed
ACM
611int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
612{
613 evsel->counts = zalloc((sizeof(*evsel->counts) +
614 (ncpus * sizeof(struct perf_counts_values))));
615 return evsel->counts != NULL ? 0 : -ENOMEM;
616}
617
69aad6f1
ACM
618void perf_evsel__free_fd(struct perf_evsel *evsel)
619{
620 xyarray__delete(evsel->fd);
621 evsel->fd = NULL;
622}
623
70db7533
ACM
624void perf_evsel__free_id(struct perf_evsel *evsel)
625{
a91e5431
ACM
626 xyarray__delete(evsel->sample_id);
627 evsel->sample_id = NULL;
628 free(evsel->id);
70db7533
ACM
629 evsel->id = NULL;
630}
631
c52b12ed
ACM
632void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
633{
634 int cpu, thread;
635
636 for (cpu = 0; cpu < ncpus; cpu++)
637 for (thread = 0; thread < nthreads; ++thread) {
638 close(FD(evsel, cpu, thread));
639 FD(evsel, cpu, thread) = -1;
640 }
641}
642
ef1d1af2 643void perf_evsel__exit(struct perf_evsel *evsel)
69aad6f1
ACM
644{
645 assert(list_empty(&evsel->node));
646 xyarray__delete(evsel->fd);
a91e5431
ACM
647 xyarray__delete(evsel->sample_id);
648 free(evsel->id);
ef1d1af2
ACM
649}
650
651void perf_evsel__delete(struct perf_evsel *evsel)
652{
653 perf_evsel__exit(evsel);
023695d9 654 close_cgroup(evsel->cgrp);
6a4bb04c 655 free(evsel->group_name);
e48ffe2b 656 if (evsel->tp_format)
efd2b924 657 pevent_free_format(evsel->tp_format);
f0c55bcf 658 free(evsel->name);
69aad6f1
ACM
659 free(evsel);
660}
c52b12ed 661
c7a79c47
SE
662static inline void compute_deltas(struct perf_evsel *evsel,
663 int cpu,
664 struct perf_counts_values *count)
665{
666 struct perf_counts_values tmp;
667
668 if (!evsel->prev_raw_counts)
669 return;
670
671 if (cpu == -1) {
672 tmp = evsel->prev_raw_counts->aggr;
673 evsel->prev_raw_counts->aggr = *count;
674 } else {
675 tmp = evsel->prev_raw_counts->cpu[cpu];
676 evsel->prev_raw_counts->cpu[cpu] = *count;
677 }
678
679 count->val = count->val - tmp.val;
680 count->ena = count->ena - tmp.ena;
681 count->run = count->run - tmp.run;
682}
683
c52b12ed
ACM
684int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
685 int cpu, int thread, bool scale)
686{
687 struct perf_counts_values count;
688 size_t nv = scale ? 3 : 1;
689
690 if (FD(evsel, cpu, thread) < 0)
691 return -EINVAL;
692
4eed11d5
ACM
693 if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
694 return -ENOMEM;
695
c52b12ed
ACM
696 if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
697 return -errno;
698
c7a79c47
SE
699 compute_deltas(evsel, cpu, &count);
700
c52b12ed
ACM
701 if (scale) {
702 if (count.run == 0)
703 count.val = 0;
704 else if (count.run < count.ena)
705 count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
706 } else
707 count.ena = count.run = 0;
708
709 evsel->counts->cpu[cpu] = count;
710 return 0;
711}
712
713int __perf_evsel__read(struct perf_evsel *evsel,
714 int ncpus, int nthreads, bool scale)
715{
716 size_t nv = scale ? 3 : 1;
717 int cpu, thread;
718 struct perf_counts_values *aggr = &evsel->counts->aggr, count;
719
52bcd994 720 aggr->val = aggr->ena = aggr->run = 0;
c52b12ed
ACM
721
722 for (cpu = 0; cpu < ncpus; cpu++) {
723 for (thread = 0; thread < nthreads; thread++) {
724 if (FD(evsel, cpu, thread) < 0)
725 continue;
726
727 if (readn(FD(evsel, cpu, thread),
728 &count, nv * sizeof(u64)) < 0)
729 return -errno;
730
731 aggr->val += count.val;
732 if (scale) {
733 aggr->ena += count.ena;
734 aggr->run += count.run;
735 }
736 }
737 }
738
c7a79c47
SE
739 compute_deltas(evsel, -1, aggr);
740
c52b12ed
ACM
741 evsel->counts->scaled = 0;
742 if (scale) {
743 if (aggr->run == 0) {
744 evsel->counts->scaled = -1;
745 aggr->val = 0;
746 return 0;
747 }
748
749 if (aggr->run < aggr->ena) {
750 evsel->counts->scaled = 1;
751 aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
752 }
753 } else
754 aggr->ena = aggr->run = 0;
755
756 return 0;
757}
48290609 758
6a4bb04c
JO
759static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
760{
761 struct perf_evsel *leader = evsel->leader;
762 int fd;
763
823254ed 764 if (perf_evsel__is_group_leader(evsel))
6a4bb04c
JO
765 return -1;
766
767 /*
768 * Leader must be already processed/open,
769 * if not it's a bug.
770 */
771 BUG_ON(!leader->fd);
772
773 fd = FD(leader, cpu, thread);
774 BUG_ON(fd == -1);
775
776 return fd;
777}
778
0252208e 779static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 780 struct thread_map *threads)
48290609 781{
0252208e 782 int cpu, thread;
023695d9 783 unsigned long flags = 0;
727ab04e 784 int pid = -1, err;
48290609 785
0252208e
ACM
786 if (evsel->fd == NULL &&
787 perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
727ab04e 788 return -ENOMEM;
4eed11d5 789
023695d9
SE
790 if (evsel->cgrp) {
791 flags = PERF_FLAG_PID_CGROUP;
792 pid = evsel->cgrp->fd;
793 }
794
594ac61a
ACM
795fallback_missing_features:
796 if (perf_missing_features.exclude_guest)
797 evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
798retry_sample_id:
799 if (perf_missing_features.sample_id_all)
800 evsel->attr.sample_id_all = 0;
801
86bd5e86 802 for (cpu = 0; cpu < cpus->nr; cpu++) {
9d04f178 803
0252208e 804 for (thread = 0; thread < threads->nr; thread++) {
6a4bb04c 805 int group_fd;
023695d9
SE
806
807 if (!evsel->cgrp)
808 pid = threads->map[thread];
809
6a4bb04c
JO
810 group_fd = get_group_fd(evsel, cpu, thread);
811
0252208e 812 FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
023695d9 813 pid,
f08199d3 814 cpus->map[cpu],
023695d9 815 group_fd, flags);
727ab04e
ACM
816 if (FD(evsel, cpu, thread) < 0) {
817 err = -errno;
594ac61a 818 goto try_fallback;
727ab04e 819 }
0252208e 820 }
48290609
ACM
821 }
822
823 return 0;
824
594ac61a
ACM
825try_fallback:
826 if (err != -EINVAL || cpu > 0 || thread > 0)
827 goto out_close;
828
829 if (!perf_missing_features.exclude_guest &&
830 (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
831 perf_missing_features.exclude_guest = true;
832 goto fallback_missing_features;
833 } else if (!perf_missing_features.sample_id_all) {
834 perf_missing_features.sample_id_all = true;
835 goto retry_sample_id;
836 }
837
48290609 838out_close:
0252208e
ACM
839 do {
840 while (--thread >= 0) {
841 close(FD(evsel, cpu, thread));
842 FD(evsel, cpu, thread) = -1;
843 }
844 thread = threads->nr;
845 } while (--cpu >= 0);
727ab04e
ACM
846 return err;
847}
848
849void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
850{
851 if (evsel->fd == NULL)
852 return;
853
854 perf_evsel__close_fd(evsel, ncpus, nthreads);
855 perf_evsel__free_fd(evsel);
856 evsel->fd = NULL;
48290609
ACM
857}
858
0252208e
ACM
859static struct {
860 struct cpu_map map;
861 int cpus[1];
862} empty_cpu_map = {
863 .map.nr = 1,
864 .cpus = { -1, },
865};
866
867static struct {
868 struct thread_map map;
869 int threads[1];
870} empty_thread_map = {
871 .map.nr = 1,
872 .threads = { -1, },
873};
874
f08199d3 875int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
6a4bb04c 876 struct thread_map *threads)
48290609 877{
0252208e
ACM
878 if (cpus == NULL) {
879 /* Work around old compiler warnings about strict aliasing */
880 cpus = &empty_cpu_map.map;
48290609
ACM
881 }
882
0252208e
ACM
883 if (threads == NULL)
884 threads = &empty_thread_map.map;
48290609 885
6a4bb04c 886 return __perf_evsel__open(evsel, cpus, threads);
48290609
ACM
887}
888
f08199d3 889int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
6a4bb04c 890 struct cpu_map *cpus)
48290609 891{
6a4bb04c 892 return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
0252208e 893}
48290609 894
f08199d3 895int perf_evsel__open_per_thread(struct perf_evsel *evsel,
6a4bb04c 896 struct thread_map *threads)
0252208e 897{
6a4bb04c 898 return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
48290609 899}
70082dd9 900
0807d2d8
ACM
901static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
902 const union perf_event *event,
903 struct perf_sample *sample)
d0dd74e8 904{
0807d2d8 905 u64 type = evsel->attr.sample_type;
d0dd74e8 906 const u64 *array = event->sample.array;
0807d2d8 907 bool swapped = evsel->needs_swap;
37073f9e 908 union u64_swap u;
d0dd74e8
ACM
909
910 array += ((event->header.size -
911 sizeof(event->header)) / sizeof(u64)) - 1;
912
913 if (type & PERF_SAMPLE_CPU) {
37073f9e
JO
914 u.val64 = *array;
915 if (swapped) {
916 /* undo swap of u64, then swap on individual u32s */
917 u.val64 = bswap_64(u.val64);
918 u.val32[0] = bswap_32(u.val32[0]);
919 }
920
921 sample->cpu = u.val32[0];
d0dd74e8
ACM
922 array--;
923 }
924
925 if (type & PERF_SAMPLE_STREAM_ID) {
926 sample->stream_id = *array;
927 array--;
928 }
929
930 if (type & PERF_SAMPLE_ID) {
931 sample->id = *array;
932 array--;
933 }
934
935 if (type & PERF_SAMPLE_TIME) {
936 sample->time = *array;
937 array--;
938 }
939
940 if (type & PERF_SAMPLE_TID) {
37073f9e
JO
941 u.val64 = *array;
942 if (swapped) {
943 /* undo swap of u64, then swap on individual u32s */
944 u.val64 = bswap_64(u.val64);
945 u.val32[0] = bswap_32(u.val32[0]);
946 u.val32[1] = bswap_32(u.val32[1]);
947 }
948
949 sample->pid = u.val32[0];
950 sample->tid = u.val32[1];
d0dd74e8
ACM
951 }
952
953 return 0;
954}
955
98e1da90
FW
956static bool sample_overlap(const union perf_event *event,
957 const void *offset, u64 size)
958{
959 const void *base = event;
960
961 if (offset + size > base + event->header.size)
962 return true;
963
964 return false;
965}
966
a3f698fe 967int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
0807d2d8 968 struct perf_sample *data)
d0dd74e8 969{
a3f698fe 970 u64 type = evsel->attr.sample_type;
0f6a3015 971 u64 regs_user = evsel->attr.sample_regs_user;
0807d2d8 972 bool swapped = evsel->needs_swap;
d0dd74e8
ACM
973 const u64 *array;
974
936be503
DA
975 /*
976 * used for cross-endian analysis. See git commit 65014ab3
977 * for why this goofiness is needed.
978 */
6a11f92e 979 union u64_swap u;
936be503 980
f3bda2c9 981 memset(data, 0, sizeof(*data));
d0dd74e8
ACM
982 data->cpu = data->pid = data->tid = -1;
983 data->stream_id = data->id = data->time = -1ULL;
a4a03fc7 984 data->period = 1;
d0dd74e8
ACM
985
986 if (event->header.type != PERF_RECORD_SAMPLE) {
a3f698fe 987 if (!evsel->attr.sample_id_all)
d0dd74e8 988 return 0;
0807d2d8 989 return perf_evsel__parse_id_sample(evsel, event, data);
d0dd74e8
ACM
990 }
991
992 array = event->sample.array;
993
a3f698fe 994 if (evsel->sample_size + sizeof(event->header) > event->header.size)
a2854124
FW
995 return -EFAULT;
996
d0dd74e8
ACM
997 if (type & PERF_SAMPLE_IP) {
998 data->ip = event->ip.ip;
999 array++;
1000 }
1001
1002 if (type & PERF_SAMPLE_TID) {
936be503
DA
1003 u.val64 = *array;
1004 if (swapped) {
1005 /* undo swap of u64, then swap on individual u32s */
1006 u.val64 = bswap_64(u.val64);
1007 u.val32[0] = bswap_32(u.val32[0]);
1008 u.val32[1] = bswap_32(u.val32[1]);
1009 }
1010
1011 data->pid = u.val32[0];
1012 data->tid = u.val32[1];
d0dd74e8
ACM
1013 array++;
1014 }
1015
1016 if (type & PERF_SAMPLE_TIME) {
1017 data->time = *array;
1018 array++;
1019 }
1020
7cec0922 1021 data->addr = 0;
d0dd74e8
ACM
1022 if (type & PERF_SAMPLE_ADDR) {
1023 data->addr = *array;
1024 array++;
1025 }
1026
1027 data->id = -1ULL;
1028 if (type & PERF_SAMPLE_ID) {
1029 data->id = *array;
1030 array++;
1031 }
1032
1033 if (type & PERF_SAMPLE_STREAM_ID) {
1034 data->stream_id = *array;
1035 array++;
1036 }
1037
1038 if (type & PERF_SAMPLE_CPU) {
936be503
DA
1039
1040 u.val64 = *array;
1041 if (swapped) {
1042 /* undo swap of u64, then swap on individual u32s */
1043 u.val64 = bswap_64(u.val64);
1044 u.val32[0] = bswap_32(u.val32[0]);
1045 }
1046
1047 data->cpu = u.val32[0];
d0dd74e8
ACM
1048 array++;
1049 }
1050
1051 if (type & PERF_SAMPLE_PERIOD) {
1052 data->period = *array;
1053 array++;
1054 }
1055
1056 if (type & PERF_SAMPLE_READ) {
f9d36996 1057 fprintf(stderr, "PERF_SAMPLE_READ is unsupported for now\n");
d0dd74e8
ACM
1058 return -1;
1059 }
1060
1061 if (type & PERF_SAMPLE_CALLCHAIN) {
98e1da90
FW
1062 if (sample_overlap(event, array, sizeof(data->callchain->nr)))
1063 return -EFAULT;
1064
d0dd74e8 1065 data->callchain = (struct ip_callchain *)array;
98e1da90
FW
1066
1067 if (sample_overlap(event, array, data->callchain->nr))
1068 return -EFAULT;
1069
d0dd74e8
ACM
1070 array += 1 + data->callchain->nr;
1071 }
1072
1073 if (type & PERF_SAMPLE_RAW) {
8e303f20
JO
1074 const u64 *pdata;
1075
936be503
DA
1076 u.val64 = *array;
1077 if (WARN_ONCE(swapped,
1078 "Endianness of raw data not corrected!\n")) {
1079 /* undo swap of u64, then swap on individual u32s */
1080 u.val64 = bswap_64(u.val64);
1081 u.val32[0] = bswap_32(u.val32[0]);
1082 u.val32[1] = bswap_32(u.val32[1]);
1083 }
98e1da90
FW
1084
1085 if (sample_overlap(event, array, sizeof(u32)))
1086 return -EFAULT;
1087
936be503 1088 data->raw_size = u.val32[0];
8e303f20 1089 pdata = (void *) array + sizeof(u32);
98e1da90 1090
8e303f20 1091 if (sample_overlap(event, pdata, data->raw_size))
98e1da90
FW
1092 return -EFAULT;
1093
8e303f20 1094 data->raw_data = (void *) pdata;
fa30c964
SE
1095
1096 array = (void *)array + data->raw_size + sizeof(u32);
d0dd74e8
ACM
1097 }
1098
b5387528
RAV
1099 if (type & PERF_SAMPLE_BRANCH_STACK) {
1100 u64 sz;
1101
1102 data->branch_stack = (struct branch_stack *)array;
1103 array++; /* nr */
1104
1105 sz = data->branch_stack->nr * sizeof(struct branch_entry);
1106 sz /= sizeof(u64);
1107 array += sz;
1108 }
0f6a3015
JO
1109
1110 if (type & PERF_SAMPLE_REGS_USER) {
1111 /* First u64 tells us if we have any regs in sample. */
1112 u64 avail = *array++;
1113
1114 if (avail) {
1115 data->user_regs.regs = (u64 *)array;
1116 array += hweight_long(regs_user);
1117 }
1118 }
1119
1120 if (type & PERF_SAMPLE_STACK_USER) {
1121 u64 size = *array++;
1122
1123 data->user_stack.offset = ((char *)(array - 1)
1124 - (char *) event);
1125
1126 if (!size) {
1127 data->user_stack.size = 0;
1128 } else {
1129 data->user_stack.data = (char *)array;
1130 array += size / sizeof(*array);
1131 data->user_stack.size = *array;
1132 }
1133 }
1134
d0dd74e8
ACM
1135 return 0;
1136}
74eec26f
AV
1137
1138int perf_event__synthesize_sample(union perf_event *event, u64 type,
1139 const struct perf_sample *sample,
1140 bool swapped)
1141{
1142 u64 *array;
1143
1144 /*
1145 * used for cross-endian analysis. See git commit 65014ab3
1146 * for why this goofiness is needed.
1147 */
6a11f92e 1148 union u64_swap u;
74eec26f
AV
1149
1150 array = event->sample.array;
1151
1152 if (type & PERF_SAMPLE_IP) {
1153 event->ip.ip = sample->ip;
1154 array++;
1155 }
1156
1157 if (type & PERF_SAMPLE_TID) {
1158 u.val32[0] = sample->pid;
1159 u.val32[1] = sample->tid;
1160 if (swapped) {
1161 /*
a3f698fe 1162 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
1163 */
1164 u.val32[0] = bswap_32(u.val32[0]);
1165 u.val32[1] = bswap_32(u.val32[1]);
1166 u.val64 = bswap_64(u.val64);
1167 }
1168
1169 *array = u.val64;
1170 array++;
1171 }
1172
1173 if (type & PERF_SAMPLE_TIME) {
1174 *array = sample->time;
1175 array++;
1176 }
1177
1178 if (type & PERF_SAMPLE_ADDR) {
1179 *array = sample->addr;
1180 array++;
1181 }
1182
1183 if (type & PERF_SAMPLE_ID) {
1184 *array = sample->id;
1185 array++;
1186 }
1187
1188 if (type & PERF_SAMPLE_STREAM_ID) {
1189 *array = sample->stream_id;
1190 array++;
1191 }
1192
1193 if (type & PERF_SAMPLE_CPU) {
1194 u.val32[0] = sample->cpu;
1195 if (swapped) {
1196 /*
a3f698fe 1197 * Inverse of what is done in perf_evsel__parse_sample
74eec26f
AV
1198 */
1199 u.val32[0] = bswap_32(u.val32[0]);
1200 u.val64 = bswap_64(u.val64);
1201 }
1202 *array = u.val64;
1203 array++;
1204 }
1205
1206 if (type & PERF_SAMPLE_PERIOD) {
1207 *array = sample->period;
1208 array++;
1209 }
1210
1211 return 0;
1212}
5555ded4 1213
efd2b924
ACM
1214struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1215{
1216 return pevent_find_field(evsel->tp_format, name);
1217}
1218
5d2074ea 1219void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
5555ded4
ACM
1220 const char *name)
1221{
efd2b924 1222 struct format_field *field = perf_evsel__field(evsel, name);
5555ded4
ACM
1223 int offset;
1224
efd2b924
ACM
1225 if (!field)
1226 return NULL;
5555ded4
ACM
1227
1228 offset = field->offset;
1229
1230 if (field->flags & FIELD_IS_DYNAMIC) {
1231 offset = *(int *)(sample->raw_data + field->offset);
1232 offset &= 0xffff;
1233 }
1234
1235 return sample->raw_data + offset;
1236}
1237
1238u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1239 const char *name)
1240{
efd2b924 1241 struct format_field *field = perf_evsel__field(evsel, name);
e6b6f679
ACM
1242 void *ptr;
1243 u64 value;
5555ded4 1244
efd2b924
ACM
1245 if (!field)
1246 return 0;
5555ded4 1247
e6b6f679 1248 ptr = sample->raw_data + field->offset;
5555ded4 1249
e6b6f679
ACM
1250 switch (field->size) {
1251 case 1:
1252 return *(u8 *)ptr;
1253 case 2:
1254 value = *(u16 *)ptr;
1255 break;
1256 case 4:
1257 value = *(u32 *)ptr;
1258 break;
1259 case 8:
1260 value = *(u64 *)ptr;
1261 break;
1262 default:
1263 return 0;
1264 }
1265
1266 if (!evsel->needs_swap)
1267 return value;
1268
1269 switch (field->size) {
1270 case 2:
1271 return bswap_16(value);
1272 case 4:
1273 return bswap_32(value);
1274 case 8:
1275 return bswap_64(value);
1276 default:
1277 return 0;
1278 }
1279
1280 return 0;
5555ded4 1281}
0698aedd
ACM
1282
1283static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1284{
1285 va_list args;
1286 int ret = 0;
1287
1288 if (!*first) {
1289 ret += fprintf(fp, ",");
1290 } else {
1291 ret += fprintf(fp, ":");
1292 *first = false;
1293 }
1294
1295 va_start(args, fmt);
1296 ret += vfprintf(fp, fmt, args);
1297 va_end(args);
1298 return ret;
1299}
1300
1301static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1302{
1303 if (value == 0)
1304 return 0;
1305
1306 return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1307}
1308
1309#define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1310
c79a4393
ACM
1311struct bit_names {
1312 int bit;
1313 const char *name;
1314};
1315
1316static int bits__fprintf(FILE *fp, const char *field, u64 value,
1317 struct bit_names *bits, bool *first)
1318{
1319 int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1320 bool first_bit = true;
1321
1322 do {
1323 if (value & bits[i].bit) {
1324 printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1325 first_bit = false;
1326 }
1327 } while (bits[++i].name != NULL);
1328
1329 return printed;
1330}
1331
1332static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1333{
1334#define bit_name(n) { PERF_SAMPLE_##n, #n }
1335 struct bit_names bits[] = {
1336 bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1337 bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1338 bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1339 bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1340 { .name = NULL, }
1341 };
1342#undef bit_name
1343 return bits__fprintf(fp, "sample_type", value, bits, first);
1344}
1345
1346static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1347{
1348#define bit_name(n) { PERF_FORMAT_##n, #n }
1349 struct bit_names bits[] = {
1350 bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1351 bit_name(ID), bit_name(GROUP),
1352 { .name = NULL, }
1353 };
1354#undef bit_name
1355 return bits__fprintf(fp, "read_format", value, bits, first);
1356}
1357
0698aedd
ACM
1358int perf_evsel__fprintf(struct perf_evsel *evsel,
1359 struct perf_attr_details *details, FILE *fp)
1360{
1361 bool first = true;
1362 int printed = fprintf(fp, "%s", perf_evsel__name(evsel));
1363
1364 if (details->verbose || details->freq) {
1365 printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1366 (u64)evsel->attr.sample_freq);
1367 }
1368
1369 if (details->verbose) {
1370 if_print(type);
1371 if_print(config);
1372 if_print(config1);
1373 if_print(config2);
1374 if_print(size);
c79a4393
ACM
1375 printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1376 if (evsel->attr.read_format)
1377 printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
0698aedd
ACM
1378 if_print(disabled);
1379 if_print(inherit);
1380 if_print(pinned);
1381 if_print(exclusive);
1382 if_print(exclude_user);
1383 if_print(exclude_kernel);
1384 if_print(exclude_hv);
1385 if_print(exclude_idle);
1386 if_print(mmap);
1387 if_print(comm);
1388 if_print(freq);
1389 if_print(inherit_stat);
1390 if_print(enable_on_exec);
1391 if_print(task);
1392 if_print(watermark);
1393 if_print(precise_ip);
1394 if_print(mmap_data);
1395 if_print(sample_id_all);
1396 if_print(exclude_host);
1397 if_print(exclude_guest);
1398 if_print(__reserved_1);
1399 if_print(wakeup_events);
1400 if_print(bp_type);
1401 if_print(branch_sample_type);
1402 }
1403
1404 fputc('\n', fp);
1405 return ++printed;
1406}
c0a54341
ACM
1407
1408bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1409 char *msg, size_t msgsize)
1410{
1411 if ((err == ENOENT || err == ENXIO) &&
1412 evsel->attr.type == PERF_TYPE_HARDWARE &&
1413 evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1414 /*
1415 * If it's cycles then fall back to hrtimer based
1416 * cpu-clock-tick sw counter, which is always available even if
1417 * no PMU support.
1418 *
1419 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1420 * b0a873e).
1421 */
1422 scnprintf(msg, msgsize, "%s",
1423"The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1424
1425 evsel->attr.type = PERF_TYPE_SOFTWARE;
1426 evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1427
1428 free(evsel->name);
1429 evsel->name = NULL;
1430 return true;
1431 }
1432
1433 return false;
1434}
56e52e85
ACM
1435
1436int perf_evsel__open_strerror(struct perf_evsel *evsel,
1437 struct perf_target *target,
1438 int err, char *msg, size_t size)
1439{
1440 switch (err) {
1441 case EPERM:
1442 case EACCES:
1443 return scnprintf(msg, size, "%s",
1444 "You may not have permission to collect %sstats.\n"
1445 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1446 " -1 - Not paranoid at all\n"
1447 " 0 - Disallow raw tracepoint access for unpriv\n"
1448 " 1 - Disallow cpu events for unpriv\n"
1449 " 2 - Disallow kernel profiling for unpriv",
1450 target->system_wide ? "system-wide " : "");
1451 case ENOENT:
1452 return scnprintf(msg, size, "The %s event is not supported.",
1453 perf_evsel__name(evsel));
1454 case EMFILE:
1455 return scnprintf(msg, size, "%s",
1456 "Too many events are opened.\n"
1457 "Try again after reducing the number of events.");
1458 case ENODEV:
1459 if (target->cpu_list)
1460 return scnprintf(msg, size, "%s",
1461 "No such device - did you specify an out-of-range profile CPU?\n");
1462 break;
1463 case EOPNOTSUPP:
1464 if (evsel->attr.precise_ip)
1465 return scnprintf(msg, size, "%s",
1466 "\'precise\' request may not be supported. Try removing 'p' modifier.");
1467#if defined(__i386__) || defined(__x86_64__)
1468 if (evsel->attr.type == PERF_TYPE_HARDWARE)
1469 return scnprintf(msg, size, "%s",
1470 "No hardware sampling interrupt available.\n"
1471 "No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
1472#endif
1473 break;
1474 default:
1475 break;
1476 }
1477
1478 return scnprintf(msg, size,
1479 "The sys_perf_event_open() syscall returned with %d (%s) for event (%s). \n"
1480 "/bin/dmesg may provide additional information.\n"
1481 "No CONFIG_PERF_EVENTS=y kernel support configured?\n",
1482 err, strerror(err), perf_evsel__name(evsel));
1483}