xref: /openbmc/linux/tools/perf/util/evsel.c (revision 5bd8e16d)
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 
10 #include <byteswap.h>
11 #include <linux/bitops.h>
12 #include <lk/debugfs.h>
13 #include <traceevent/event-parse.h>
14 #include <linux/hw_breakpoint.h>
15 #include <linux/perf_event.h>
16 #include <sys/resource.h>
17 #include "asm/bug.h"
18 #include "evsel.h"
19 #include "evlist.h"
20 #include "util.h"
21 #include "cpumap.h"
22 #include "thread_map.h"
23 #include "target.h"
24 #include "perf_regs.h"
25 #include "debug.h"
26 
27 static struct {
28 	bool sample_id_all;
29 	bool exclude_guest;
30 	bool mmap2;
31 } perf_missing_features;
32 
33 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
34 
35 int __perf_evsel__sample_size(u64 sample_type)
36 {
37 	u64 mask = sample_type & PERF_SAMPLE_MASK;
38 	int size = 0;
39 	int i;
40 
41 	for (i = 0; i < 64; i++) {
42 		if (mask & (1ULL << i))
43 			size++;
44 	}
45 
46 	size *= sizeof(u64);
47 
48 	return size;
49 }
50 
51 /**
52  * __perf_evsel__calc_id_pos - calculate id_pos.
53  * @sample_type: sample type
54  *
55  * This function returns the position of the event id (PERF_SAMPLE_ID or
56  * PERF_SAMPLE_IDENTIFIER) in a sample event i.e. in the array of struct
57  * sample_event.
58  */
59 static int __perf_evsel__calc_id_pos(u64 sample_type)
60 {
61 	int idx = 0;
62 
63 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
64 		return 0;
65 
66 	if (!(sample_type & PERF_SAMPLE_ID))
67 		return -1;
68 
69 	if (sample_type & PERF_SAMPLE_IP)
70 		idx += 1;
71 
72 	if (sample_type & PERF_SAMPLE_TID)
73 		idx += 1;
74 
75 	if (sample_type & PERF_SAMPLE_TIME)
76 		idx += 1;
77 
78 	if (sample_type & PERF_SAMPLE_ADDR)
79 		idx += 1;
80 
81 	return idx;
82 }
83 
84 /**
85  * __perf_evsel__calc_is_pos - calculate is_pos.
86  * @sample_type: sample type
87  *
88  * This function returns the position (counting backwards) of the event id
89  * (PERF_SAMPLE_ID or PERF_SAMPLE_IDENTIFIER) in a non-sample event i.e. if
90  * sample_id_all is used there is an id sample appended to non-sample events.
91  */
92 static int __perf_evsel__calc_is_pos(u64 sample_type)
93 {
94 	int idx = 1;
95 
96 	if (sample_type & PERF_SAMPLE_IDENTIFIER)
97 		return 1;
98 
99 	if (!(sample_type & PERF_SAMPLE_ID))
100 		return -1;
101 
102 	if (sample_type & PERF_SAMPLE_CPU)
103 		idx += 1;
104 
105 	if (sample_type & PERF_SAMPLE_STREAM_ID)
106 		idx += 1;
107 
108 	return idx;
109 }
110 
111 void perf_evsel__calc_id_pos(struct perf_evsel *evsel)
112 {
113 	evsel->id_pos = __perf_evsel__calc_id_pos(evsel->attr.sample_type);
114 	evsel->is_pos = __perf_evsel__calc_is_pos(evsel->attr.sample_type);
115 }
116 
117 void hists__init(struct hists *hists)
118 {
119 	memset(hists, 0, sizeof(*hists));
120 	hists->entries_in_array[0] = hists->entries_in_array[1] = RB_ROOT;
121 	hists->entries_in = &hists->entries_in_array[0];
122 	hists->entries_collapsed = RB_ROOT;
123 	hists->entries = RB_ROOT;
124 	pthread_mutex_init(&hists->lock, NULL);
125 }
126 
127 void __perf_evsel__set_sample_bit(struct perf_evsel *evsel,
128 				  enum perf_event_sample_format bit)
129 {
130 	if (!(evsel->attr.sample_type & bit)) {
131 		evsel->attr.sample_type |= bit;
132 		evsel->sample_size += sizeof(u64);
133 		perf_evsel__calc_id_pos(evsel);
134 	}
135 }
136 
137 void __perf_evsel__reset_sample_bit(struct perf_evsel *evsel,
138 				    enum perf_event_sample_format bit)
139 {
140 	if (evsel->attr.sample_type & bit) {
141 		evsel->attr.sample_type &= ~bit;
142 		evsel->sample_size -= sizeof(u64);
143 		perf_evsel__calc_id_pos(evsel);
144 	}
145 }
146 
147 void perf_evsel__set_sample_id(struct perf_evsel *evsel,
148 			       bool can_sample_identifier)
149 {
150 	if (can_sample_identifier) {
151 		perf_evsel__reset_sample_bit(evsel, ID);
152 		perf_evsel__set_sample_bit(evsel, IDENTIFIER);
153 	} else {
154 		perf_evsel__set_sample_bit(evsel, ID);
155 	}
156 	evsel->attr.read_format |= PERF_FORMAT_ID;
157 }
158 
159 void perf_evsel__init(struct perf_evsel *evsel,
160 		      struct perf_event_attr *attr, int idx)
161 {
162 	evsel->idx	   = idx;
163 	evsel->attr	   = *attr;
164 	evsel->leader	   = evsel;
165 	INIT_LIST_HEAD(&evsel->node);
166 	hists__init(&evsel->hists);
167 	evsel->sample_size = __perf_evsel__sample_size(attr->sample_type);
168 	perf_evsel__calc_id_pos(evsel);
169 }
170 
171 struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx)
172 {
173 	struct perf_evsel *evsel = zalloc(sizeof(*evsel));
174 
175 	if (evsel != NULL)
176 		perf_evsel__init(evsel, attr, idx);
177 
178 	return evsel;
179 }
180 
181 struct event_format *event_format__new(const char *sys, const char *name)
182 {
183 	int fd, n;
184 	char *filename;
185 	void *bf = NULL, *nbf;
186 	size_t size = 0, alloc_size = 0;
187 	struct event_format *format = NULL;
188 
189 	if (asprintf(&filename, "%s/%s/%s/format", tracing_events_path, sys, name) < 0)
190 		goto out;
191 
192 	fd = open(filename, O_RDONLY);
193 	if (fd < 0)
194 		goto out_free_filename;
195 
196 	do {
197 		if (size == alloc_size) {
198 			alloc_size += BUFSIZ;
199 			nbf = realloc(bf, alloc_size);
200 			if (nbf == NULL)
201 				goto out_free_bf;
202 			bf = nbf;
203 		}
204 
205 		n = read(fd, bf + size, alloc_size - size);
206 		if (n < 0)
207 			goto out_free_bf;
208 		size += n;
209 	} while (n > 0);
210 
211 	pevent_parse_format(&format, bf, size, sys);
212 
213 out_free_bf:
214 	free(bf);
215 	close(fd);
216 out_free_filename:
217 	free(filename);
218 out:
219 	return format;
220 }
221 
222 struct perf_evsel *perf_evsel__newtp(const char *sys, const char *name, int idx)
223 {
224 	struct perf_evsel *evsel = zalloc(sizeof(*evsel));
225 
226 	if (evsel != NULL) {
227 		struct perf_event_attr attr = {
228 			.type	       = PERF_TYPE_TRACEPOINT,
229 			.sample_type   = (PERF_SAMPLE_RAW | PERF_SAMPLE_TIME |
230 					  PERF_SAMPLE_CPU | PERF_SAMPLE_PERIOD),
231 		};
232 
233 		if (asprintf(&evsel->name, "%s:%s", sys, name) < 0)
234 			goto out_free;
235 
236 		evsel->tp_format = event_format__new(sys, name);
237 		if (evsel->tp_format == NULL)
238 			goto out_free;
239 
240 		event_attr_init(&attr);
241 		attr.config = evsel->tp_format->id;
242 		attr.sample_period = 1;
243 		perf_evsel__init(evsel, &attr, idx);
244 	}
245 
246 	return evsel;
247 
248 out_free:
249 	free(evsel->name);
250 	free(evsel);
251 	return NULL;
252 }
253 
254 const char *perf_evsel__hw_names[PERF_COUNT_HW_MAX] = {
255 	"cycles",
256 	"instructions",
257 	"cache-references",
258 	"cache-misses",
259 	"branches",
260 	"branch-misses",
261 	"bus-cycles",
262 	"stalled-cycles-frontend",
263 	"stalled-cycles-backend",
264 	"ref-cycles",
265 };
266 
267 static const char *__perf_evsel__hw_name(u64 config)
268 {
269 	if (config < PERF_COUNT_HW_MAX && perf_evsel__hw_names[config])
270 		return perf_evsel__hw_names[config];
271 
272 	return "unknown-hardware";
273 }
274 
275 static int perf_evsel__add_modifiers(struct perf_evsel *evsel, char *bf, size_t size)
276 {
277 	int colon = 0, r = 0;
278 	struct perf_event_attr *attr = &evsel->attr;
279 	bool exclude_guest_default = false;
280 
281 #define MOD_PRINT(context, mod)	do {					\
282 		if (!attr->exclude_##context) {				\
283 			if (!colon) colon = ++r;			\
284 			r += scnprintf(bf + r, size - r, "%c", mod);	\
285 		} } while(0)
286 
287 	if (attr->exclude_kernel || attr->exclude_user || attr->exclude_hv) {
288 		MOD_PRINT(kernel, 'k');
289 		MOD_PRINT(user, 'u');
290 		MOD_PRINT(hv, 'h');
291 		exclude_guest_default = true;
292 	}
293 
294 	if (attr->precise_ip) {
295 		if (!colon)
296 			colon = ++r;
297 		r += scnprintf(bf + r, size - r, "%.*s", attr->precise_ip, "ppp");
298 		exclude_guest_default = true;
299 	}
300 
301 	if (attr->exclude_host || attr->exclude_guest == exclude_guest_default) {
302 		MOD_PRINT(host, 'H');
303 		MOD_PRINT(guest, 'G');
304 	}
305 #undef MOD_PRINT
306 	if (colon)
307 		bf[colon - 1] = ':';
308 	return r;
309 }
310 
311 static int perf_evsel__hw_name(struct perf_evsel *evsel, char *bf, size_t size)
312 {
313 	int r = scnprintf(bf, size, "%s", __perf_evsel__hw_name(evsel->attr.config));
314 	return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
315 }
316 
317 const char *perf_evsel__sw_names[PERF_COUNT_SW_MAX] = {
318 	"cpu-clock",
319 	"task-clock",
320 	"page-faults",
321 	"context-switches",
322 	"cpu-migrations",
323 	"minor-faults",
324 	"major-faults",
325 	"alignment-faults",
326 	"emulation-faults",
327 	"dummy",
328 };
329 
330 static const char *__perf_evsel__sw_name(u64 config)
331 {
332 	if (config < PERF_COUNT_SW_MAX && perf_evsel__sw_names[config])
333 		return perf_evsel__sw_names[config];
334 	return "unknown-software";
335 }
336 
337 static int perf_evsel__sw_name(struct perf_evsel *evsel, char *bf, size_t size)
338 {
339 	int r = scnprintf(bf, size, "%s", __perf_evsel__sw_name(evsel->attr.config));
340 	return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
341 }
342 
343 static int __perf_evsel__bp_name(char *bf, size_t size, u64 addr, u64 type)
344 {
345 	int r;
346 
347 	r = scnprintf(bf, size, "mem:0x%" PRIx64 ":", addr);
348 
349 	if (type & HW_BREAKPOINT_R)
350 		r += scnprintf(bf + r, size - r, "r");
351 
352 	if (type & HW_BREAKPOINT_W)
353 		r += scnprintf(bf + r, size - r, "w");
354 
355 	if (type & HW_BREAKPOINT_X)
356 		r += scnprintf(bf + r, size - r, "x");
357 
358 	return r;
359 }
360 
361 static int perf_evsel__bp_name(struct perf_evsel *evsel, char *bf, size_t size)
362 {
363 	struct perf_event_attr *attr = &evsel->attr;
364 	int r = __perf_evsel__bp_name(bf, size, attr->bp_addr, attr->bp_type);
365 	return r + perf_evsel__add_modifiers(evsel, bf + r, size - r);
366 }
367 
368 const char *perf_evsel__hw_cache[PERF_COUNT_HW_CACHE_MAX]
369 				[PERF_EVSEL__MAX_ALIASES] = {
370  { "L1-dcache",	"l1-d",		"l1d",		"L1-data",		},
371  { "L1-icache",	"l1-i",		"l1i",		"L1-instruction",	},
372  { "LLC",	"L2",							},
373  { "dTLB",	"d-tlb",	"Data-TLB",				},
374  { "iTLB",	"i-tlb",	"Instruction-TLB",			},
375  { "branch",	"branches",	"bpu",		"btb",		"bpc",	},
376  { "node",								},
377 };
378 
379 const char *perf_evsel__hw_cache_op[PERF_COUNT_HW_CACHE_OP_MAX]
380 				   [PERF_EVSEL__MAX_ALIASES] = {
381  { "load",	"loads",	"read",					},
382  { "store",	"stores",	"write",				},
383  { "prefetch",	"prefetches",	"speculative-read", "speculative-load",	},
384 };
385 
386 const char *perf_evsel__hw_cache_result[PERF_COUNT_HW_CACHE_RESULT_MAX]
387 				       [PERF_EVSEL__MAX_ALIASES] = {
388  { "refs",	"Reference",	"ops",		"access",		},
389  { "misses",	"miss",							},
390 };
391 
392 #define C(x)		PERF_COUNT_HW_CACHE_##x
393 #define CACHE_READ	(1 << C(OP_READ))
394 #define CACHE_WRITE	(1 << C(OP_WRITE))
395 #define CACHE_PREFETCH	(1 << C(OP_PREFETCH))
396 #define COP(x)		(1 << x)
397 
398 /*
399  * cache operartion stat
400  * L1I : Read and prefetch only
401  * ITLB and BPU : Read-only
402  */
403 static unsigned long perf_evsel__hw_cache_stat[C(MAX)] = {
404  [C(L1D)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
405  [C(L1I)]	= (CACHE_READ | CACHE_PREFETCH),
406  [C(LL)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
407  [C(DTLB)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
408  [C(ITLB)]	= (CACHE_READ),
409  [C(BPU)]	= (CACHE_READ),
410  [C(NODE)]	= (CACHE_READ | CACHE_WRITE | CACHE_PREFETCH),
411 };
412 
413 bool perf_evsel__is_cache_op_valid(u8 type, u8 op)
414 {
415 	if (perf_evsel__hw_cache_stat[type] & COP(op))
416 		return true;	/* valid */
417 	else
418 		return false;	/* invalid */
419 }
420 
421 int __perf_evsel__hw_cache_type_op_res_name(u8 type, u8 op, u8 result,
422 					    char *bf, size_t size)
423 {
424 	if (result) {
425 		return scnprintf(bf, size, "%s-%s-%s", perf_evsel__hw_cache[type][0],
426 				 perf_evsel__hw_cache_op[op][0],
427 				 perf_evsel__hw_cache_result[result][0]);
428 	}
429 
430 	return scnprintf(bf, size, "%s-%s", perf_evsel__hw_cache[type][0],
431 			 perf_evsel__hw_cache_op[op][1]);
432 }
433 
434 static int __perf_evsel__hw_cache_name(u64 config, char *bf, size_t size)
435 {
436 	u8 op, result, type = (config >>  0) & 0xff;
437 	const char *err = "unknown-ext-hardware-cache-type";
438 
439 	if (type > PERF_COUNT_HW_CACHE_MAX)
440 		goto out_err;
441 
442 	op = (config >>  8) & 0xff;
443 	err = "unknown-ext-hardware-cache-op";
444 	if (op > PERF_COUNT_HW_CACHE_OP_MAX)
445 		goto out_err;
446 
447 	result = (config >> 16) & 0xff;
448 	err = "unknown-ext-hardware-cache-result";
449 	if (result > PERF_COUNT_HW_CACHE_RESULT_MAX)
450 		goto out_err;
451 
452 	err = "invalid-cache";
453 	if (!perf_evsel__is_cache_op_valid(type, op))
454 		goto out_err;
455 
456 	return __perf_evsel__hw_cache_type_op_res_name(type, op, result, bf, size);
457 out_err:
458 	return scnprintf(bf, size, "%s", err);
459 }
460 
461 static int perf_evsel__hw_cache_name(struct perf_evsel *evsel, char *bf, size_t size)
462 {
463 	int ret = __perf_evsel__hw_cache_name(evsel->attr.config, bf, size);
464 	return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
465 }
466 
467 static int perf_evsel__raw_name(struct perf_evsel *evsel, char *bf, size_t size)
468 {
469 	int ret = scnprintf(bf, size, "raw 0x%" PRIx64, evsel->attr.config);
470 	return ret + perf_evsel__add_modifiers(evsel, bf + ret, size - ret);
471 }
472 
473 const char *perf_evsel__name(struct perf_evsel *evsel)
474 {
475 	char bf[128];
476 
477 	if (evsel->name)
478 		return evsel->name;
479 
480 	switch (evsel->attr.type) {
481 	case PERF_TYPE_RAW:
482 		perf_evsel__raw_name(evsel, bf, sizeof(bf));
483 		break;
484 
485 	case PERF_TYPE_HARDWARE:
486 		perf_evsel__hw_name(evsel, bf, sizeof(bf));
487 		break;
488 
489 	case PERF_TYPE_HW_CACHE:
490 		perf_evsel__hw_cache_name(evsel, bf, sizeof(bf));
491 		break;
492 
493 	case PERF_TYPE_SOFTWARE:
494 		perf_evsel__sw_name(evsel, bf, sizeof(bf));
495 		break;
496 
497 	case PERF_TYPE_TRACEPOINT:
498 		scnprintf(bf, sizeof(bf), "%s", "unknown tracepoint");
499 		break;
500 
501 	case PERF_TYPE_BREAKPOINT:
502 		perf_evsel__bp_name(evsel, bf, sizeof(bf));
503 		break;
504 
505 	default:
506 		scnprintf(bf, sizeof(bf), "unknown attr type: %d",
507 			  evsel->attr.type);
508 		break;
509 	}
510 
511 	evsel->name = strdup(bf);
512 
513 	return evsel->name ?: "unknown";
514 }
515 
516 const char *perf_evsel__group_name(struct perf_evsel *evsel)
517 {
518 	return evsel->group_name ?: "anon group";
519 }
520 
521 int perf_evsel__group_desc(struct perf_evsel *evsel, char *buf, size_t size)
522 {
523 	int ret;
524 	struct perf_evsel *pos;
525 	const char *group_name = perf_evsel__group_name(evsel);
526 
527 	ret = scnprintf(buf, size, "%s", group_name);
528 
529 	ret += scnprintf(buf + ret, size - ret, " { %s",
530 			 perf_evsel__name(evsel));
531 
532 	for_each_group_member(pos, evsel)
533 		ret += scnprintf(buf + ret, size - ret, ", %s",
534 				 perf_evsel__name(pos));
535 
536 	ret += scnprintf(buf + ret, size - ret, " }");
537 
538 	return ret;
539 }
540 
541 /*
542  * The enable_on_exec/disabled value strategy:
543  *
544  *  1) For any type of traced program:
545  *    - all independent events and group leaders are disabled
546  *    - all group members are enabled
547  *
548  *     Group members are ruled by group leaders. They need to
549  *     be enabled, because the group scheduling relies on that.
550  *
551  *  2) For traced programs executed by perf:
552  *     - all independent events and group leaders have
553  *       enable_on_exec set
554  *     - we don't specifically enable or disable any event during
555  *       the record command
556  *
557  *     Independent events and group leaders are initially disabled
558  *     and get enabled by exec. Group members are ruled by group
559  *     leaders as stated in 1).
560  *
561  *  3) For traced programs attached by perf (pid/tid):
562  *     - we specifically enable or disable all events during
563  *       the record command
564  *
565  *     When attaching events to already running traced we
566  *     enable/disable events specifically, as there's no
567  *     initial traced exec call.
568  */
569 void perf_evsel__config(struct perf_evsel *evsel,
570 			struct perf_record_opts *opts)
571 {
572 	struct perf_evsel *leader = evsel->leader;
573 	struct perf_event_attr *attr = &evsel->attr;
574 	int track = !evsel->idx; /* only the first counter needs these */
575 
576 	attr->sample_id_all = perf_missing_features.sample_id_all ? 0 : 1;
577 	attr->inherit	    = !opts->no_inherit;
578 
579 	perf_evsel__set_sample_bit(evsel, IP);
580 	perf_evsel__set_sample_bit(evsel, TID);
581 
582 	if (evsel->sample_read) {
583 		perf_evsel__set_sample_bit(evsel, READ);
584 
585 		/*
586 		 * We need ID even in case of single event, because
587 		 * PERF_SAMPLE_READ process ID specific data.
588 		 */
589 		perf_evsel__set_sample_id(evsel, false);
590 
591 		/*
592 		 * Apply group format only if we belong to group
593 		 * with more than one members.
594 		 */
595 		if (leader->nr_members > 1) {
596 			attr->read_format |= PERF_FORMAT_GROUP;
597 			attr->inherit = 0;
598 		}
599 	}
600 
601 	/*
602 	 * We default some events to a 1 default interval. But keep
603 	 * it a weak assumption overridable by the user.
604 	 */
605 	if (!attr->sample_period || (opts->user_freq != UINT_MAX &&
606 				     opts->user_interval != ULLONG_MAX)) {
607 		if (opts->freq) {
608 			perf_evsel__set_sample_bit(evsel, PERIOD);
609 			attr->freq		= 1;
610 			attr->sample_freq	= opts->freq;
611 		} else {
612 			attr->sample_period = opts->default_interval;
613 		}
614 	}
615 
616 	/*
617 	 * Disable sampling for all group members other
618 	 * than leader in case leader 'leads' the sampling.
619 	 */
620 	if ((leader != evsel) && leader->sample_read) {
621 		attr->sample_freq   = 0;
622 		attr->sample_period = 0;
623 	}
624 
625 	if (opts->no_samples)
626 		attr->sample_freq = 0;
627 
628 	if (opts->inherit_stat)
629 		attr->inherit_stat = 1;
630 
631 	if (opts->sample_address) {
632 		perf_evsel__set_sample_bit(evsel, ADDR);
633 		attr->mmap_data = track;
634 	}
635 
636 	if (opts->call_graph) {
637 		perf_evsel__set_sample_bit(evsel, CALLCHAIN);
638 
639 		if (opts->call_graph == CALLCHAIN_DWARF) {
640 			perf_evsel__set_sample_bit(evsel, REGS_USER);
641 			perf_evsel__set_sample_bit(evsel, STACK_USER);
642 			attr->sample_regs_user = PERF_REGS_MASK;
643 			attr->sample_stack_user = opts->stack_dump_size;
644 			attr->exclude_callchain_user = 1;
645 		}
646 	}
647 
648 	if (perf_target__has_cpu(&opts->target))
649 		perf_evsel__set_sample_bit(evsel, CPU);
650 
651 	if (opts->period)
652 		perf_evsel__set_sample_bit(evsel, PERIOD);
653 
654 	if (!perf_missing_features.sample_id_all &&
655 	    (opts->sample_time || !opts->no_inherit ||
656 	     perf_target__has_cpu(&opts->target)))
657 		perf_evsel__set_sample_bit(evsel, TIME);
658 
659 	if (opts->raw_samples) {
660 		perf_evsel__set_sample_bit(evsel, TIME);
661 		perf_evsel__set_sample_bit(evsel, RAW);
662 		perf_evsel__set_sample_bit(evsel, CPU);
663 	}
664 
665 	if (opts->sample_address)
666 		attr->sample_type	|= PERF_SAMPLE_DATA_SRC;
667 
668 	if (opts->no_delay) {
669 		attr->watermark = 0;
670 		attr->wakeup_events = 1;
671 	}
672 	if (opts->branch_stack) {
673 		perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
674 		attr->branch_sample_type = opts->branch_stack;
675 	}
676 
677 	if (opts->sample_weight)
678 		attr->sample_type	|= PERF_SAMPLE_WEIGHT;
679 
680 	attr->mmap  = track;
681 	attr->mmap2 = track && !perf_missing_features.mmap2;
682 	attr->comm  = track;
683 
684 	/*
685 	 * XXX see the function comment above
686 	 *
687 	 * Disabling only independent events or group leaders,
688 	 * keeping group members enabled.
689 	 */
690 	if (perf_evsel__is_group_leader(evsel))
691 		attr->disabled = 1;
692 
693 	/*
694 	 * Setting enable_on_exec for independent events and
695 	 * group leaders for traced executed by perf.
696 	 */
697 	if (perf_target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
698 		attr->enable_on_exec = 1;
699 }
700 
701 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
702 {
703 	int cpu, thread;
704 	evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
705 
706 	if (evsel->fd) {
707 		for (cpu = 0; cpu < ncpus; cpu++) {
708 			for (thread = 0; thread < nthreads; thread++) {
709 				FD(evsel, cpu, thread) = -1;
710 			}
711 		}
712 	}
713 
714 	return evsel->fd != NULL ? 0 : -ENOMEM;
715 }
716 
717 static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthreads,
718 			  int ioc,  void *arg)
719 {
720 	int cpu, thread;
721 
722 	for (cpu = 0; cpu < ncpus; cpu++) {
723 		for (thread = 0; thread < nthreads; thread++) {
724 			int fd = FD(evsel, cpu, thread),
725 			    err = ioctl(fd, ioc, arg);
726 
727 			if (err)
728 				return err;
729 		}
730 	}
731 
732 	return 0;
733 }
734 
735 int perf_evsel__set_filter(struct perf_evsel *evsel, int ncpus, int nthreads,
736 			   const char *filter)
737 {
738 	return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
739 				     PERF_EVENT_IOC_SET_FILTER,
740 				     (void *)filter);
741 }
742 
743 int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
744 {
745 	return perf_evsel__run_ioctl(evsel, ncpus, nthreads,
746 				     PERF_EVENT_IOC_ENABLE,
747 				     0);
748 }
749 
750 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
751 {
752 	evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
753 	if (evsel->sample_id == NULL)
754 		return -ENOMEM;
755 
756 	evsel->id = zalloc(ncpus * nthreads * sizeof(u64));
757 	if (evsel->id == NULL) {
758 		xyarray__delete(evsel->sample_id);
759 		evsel->sample_id = NULL;
760 		return -ENOMEM;
761 	}
762 
763 	return 0;
764 }
765 
766 void perf_evsel__reset_counts(struct perf_evsel *evsel, int ncpus)
767 {
768 	memset(evsel->counts, 0, (sizeof(*evsel->counts) +
769 				 (ncpus * sizeof(struct perf_counts_values))));
770 }
771 
772 int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus)
773 {
774 	evsel->counts = zalloc((sizeof(*evsel->counts) +
775 				(ncpus * sizeof(struct perf_counts_values))));
776 	return evsel->counts != NULL ? 0 : -ENOMEM;
777 }
778 
779 void perf_evsel__free_fd(struct perf_evsel *evsel)
780 {
781 	xyarray__delete(evsel->fd);
782 	evsel->fd = NULL;
783 }
784 
785 void perf_evsel__free_id(struct perf_evsel *evsel)
786 {
787 	xyarray__delete(evsel->sample_id);
788 	evsel->sample_id = NULL;
789 	free(evsel->id);
790 	evsel->id = NULL;
791 }
792 
793 void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
794 {
795 	int cpu, thread;
796 
797 	for (cpu = 0; cpu < ncpus; cpu++)
798 		for (thread = 0; thread < nthreads; ++thread) {
799 			close(FD(evsel, cpu, thread));
800 			FD(evsel, cpu, thread) = -1;
801 		}
802 }
803 
804 void perf_evsel__free_counts(struct perf_evsel *evsel)
805 {
806 	free(evsel->counts);
807 }
808 
809 void perf_evsel__exit(struct perf_evsel *evsel)
810 {
811 	assert(list_empty(&evsel->node));
812 	perf_evsel__free_fd(evsel);
813 	perf_evsel__free_id(evsel);
814 }
815 
816 void perf_evsel__delete(struct perf_evsel *evsel)
817 {
818 	perf_evsel__exit(evsel);
819 	close_cgroup(evsel->cgrp);
820 	free(evsel->group_name);
821 	if (evsel->tp_format)
822 		pevent_free_format(evsel->tp_format);
823 	free(evsel->name);
824 	free(evsel);
825 }
826 
827 static inline void compute_deltas(struct perf_evsel *evsel,
828 				  int cpu,
829 				  struct perf_counts_values *count)
830 {
831 	struct perf_counts_values tmp;
832 
833 	if (!evsel->prev_raw_counts)
834 		return;
835 
836 	if (cpu == -1) {
837 		tmp = evsel->prev_raw_counts->aggr;
838 		evsel->prev_raw_counts->aggr = *count;
839 	} else {
840 		tmp = evsel->prev_raw_counts->cpu[cpu];
841 		evsel->prev_raw_counts->cpu[cpu] = *count;
842 	}
843 
844 	count->val = count->val - tmp.val;
845 	count->ena = count->ena - tmp.ena;
846 	count->run = count->run - tmp.run;
847 }
848 
849 int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
850 			      int cpu, int thread, bool scale)
851 {
852 	struct perf_counts_values count;
853 	size_t nv = scale ? 3 : 1;
854 
855 	if (FD(evsel, cpu, thread) < 0)
856 		return -EINVAL;
857 
858 	if (evsel->counts == NULL && perf_evsel__alloc_counts(evsel, cpu + 1) < 0)
859 		return -ENOMEM;
860 
861 	if (readn(FD(evsel, cpu, thread), &count, nv * sizeof(u64)) < 0)
862 		return -errno;
863 
864 	compute_deltas(evsel, cpu, &count);
865 
866 	if (scale) {
867 		if (count.run == 0)
868 			count.val = 0;
869 		else if (count.run < count.ena)
870 			count.val = (u64)((double)count.val * count.ena / count.run + 0.5);
871 	} else
872 		count.ena = count.run = 0;
873 
874 	evsel->counts->cpu[cpu] = count;
875 	return 0;
876 }
877 
878 int __perf_evsel__read(struct perf_evsel *evsel,
879 		       int ncpus, int nthreads, bool scale)
880 {
881 	size_t nv = scale ? 3 : 1;
882 	int cpu, thread;
883 	struct perf_counts_values *aggr = &evsel->counts->aggr, count;
884 
885 	aggr->val = aggr->ena = aggr->run = 0;
886 
887 	for (cpu = 0; cpu < ncpus; cpu++) {
888 		for (thread = 0; thread < nthreads; thread++) {
889 			if (FD(evsel, cpu, thread) < 0)
890 				continue;
891 
892 			if (readn(FD(evsel, cpu, thread),
893 				  &count, nv * sizeof(u64)) < 0)
894 				return -errno;
895 
896 			aggr->val += count.val;
897 			if (scale) {
898 				aggr->ena += count.ena;
899 				aggr->run += count.run;
900 			}
901 		}
902 	}
903 
904 	compute_deltas(evsel, -1, aggr);
905 
906 	evsel->counts->scaled = 0;
907 	if (scale) {
908 		if (aggr->run == 0) {
909 			evsel->counts->scaled = -1;
910 			aggr->val = 0;
911 			return 0;
912 		}
913 
914 		if (aggr->run < aggr->ena) {
915 			evsel->counts->scaled = 1;
916 			aggr->val = (u64)((double)aggr->val * aggr->ena / aggr->run + 0.5);
917 		}
918 	} else
919 		aggr->ena = aggr->run = 0;
920 
921 	return 0;
922 }
923 
924 static int get_group_fd(struct perf_evsel *evsel, int cpu, int thread)
925 {
926 	struct perf_evsel *leader = evsel->leader;
927 	int fd;
928 
929 	if (perf_evsel__is_group_leader(evsel))
930 		return -1;
931 
932 	/*
933 	 * Leader must be already processed/open,
934 	 * if not it's a bug.
935 	 */
936 	BUG_ON(!leader->fd);
937 
938 	fd = FD(leader, cpu, thread);
939 	BUG_ON(fd == -1);
940 
941 	return fd;
942 }
943 
944 #define __PRINT_ATTR(fmt, cast, field)  \
945 	fprintf(fp, "  %-19s "fmt"\n", #field, cast attr->field)
946 
947 #define PRINT_ATTR_U32(field)  __PRINT_ATTR("%u" , , field)
948 #define PRINT_ATTR_X32(field)  __PRINT_ATTR("%#x", , field)
949 #define PRINT_ATTR_U64(field)  __PRINT_ATTR("%" PRIu64, (uint64_t), field)
950 #define PRINT_ATTR_X64(field)  __PRINT_ATTR("%#"PRIx64, (uint64_t), field)
951 
952 #define PRINT_ATTR2N(name1, field1, name2, field2)	\
953 	fprintf(fp, "  %-19s %u    %-19s %u\n",		\
954 	name1, attr->field1, name2, attr->field2)
955 
956 #define PRINT_ATTR2(field1, field2) \
957 	PRINT_ATTR2N(#field1, field1, #field2, field2)
958 
959 static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
960 {
961 	size_t ret = 0;
962 
963 	ret += fprintf(fp, "%.60s\n", graph_dotted_line);
964 	ret += fprintf(fp, "perf_event_attr:\n");
965 
966 	ret += PRINT_ATTR_U32(type);
967 	ret += PRINT_ATTR_U32(size);
968 	ret += PRINT_ATTR_X64(config);
969 	ret += PRINT_ATTR_U64(sample_period);
970 	ret += PRINT_ATTR_U64(sample_freq);
971 	ret += PRINT_ATTR_X64(sample_type);
972 	ret += PRINT_ATTR_X64(read_format);
973 
974 	ret += PRINT_ATTR2(disabled, inherit);
975 	ret += PRINT_ATTR2(pinned, exclusive);
976 	ret += PRINT_ATTR2(exclude_user, exclude_kernel);
977 	ret += PRINT_ATTR2(exclude_hv, exclude_idle);
978 	ret += PRINT_ATTR2(mmap, comm);
979 	ret += PRINT_ATTR2(freq, inherit_stat);
980 	ret += PRINT_ATTR2(enable_on_exec, task);
981 	ret += PRINT_ATTR2(watermark, precise_ip);
982 	ret += PRINT_ATTR2(mmap_data, sample_id_all);
983 	ret += PRINT_ATTR2(exclude_host, exclude_guest);
984 	ret += PRINT_ATTR2N("excl.callchain_kern", exclude_callchain_kernel,
985 			    "excl.callchain_user", exclude_callchain_user);
986 
987 	ret += PRINT_ATTR_U32(wakeup_events);
988 	ret += PRINT_ATTR_U32(wakeup_watermark);
989 	ret += PRINT_ATTR_X32(bp_type);
990 	ret += PRINT_ATTR_X64(bp_addr);
991 	ret += PRINT_ATTR_X64(config1);
992 	ret += PRINT_ATTR_U64(bp_len);
993 	ret += PRINT_ATTR_X64(config2);
994 	ret += PRINT_ATTR_X64(branch_sample_type);
995 	ret += PRINT_ATTR_X64(sample_regs_user);
996 	ret += PRINT_ATTR_U32(sample_stack_user);
997 
998 	ret += fprintf(fp, "%.60s\n", graph_dotted_line);
999 
1000 	return ret;
1001 }
1002 
1003 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1004 			      struct thread_map *threads)
1005 {
1006 	int cpu, thread;
1007 	unsigned long flags = 0;
1008 	int pid = -1, err;
1009 	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
1010 
1011 	if (evsel->fd == NULL &&
1012 	    perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
1013 		return -ENOMEM;
1014 
1015 	if (evsel->cgrp) {
1016 		flags = PERF_FLAG_PID_CGROUP;
1017 		pid = evsel->cgrp->fd;
1018 	}
1019 
1020 fallback_missing_features:
1021 	if (perf_missing_features.mmap2)
1022 		evsel->attr.mmap2 = 0;
1023 	if (perf_missing_features.exclude_guest)
1024 		evsel->attr.exclude_guest = evsel->attr.exclude_host = 0;
1025 retry_sample_id:
1026 	if (perf_missing_features.sample_id_all)
1027 		evsel->attr.sample_id_all = 0;
1028 
1029 	if (verbose >= 2)
1030 		perf_event_attr__fprintf(&evsel->attr, stderr);
1031 
1032 	for (cpu = 0; cpu < cpus->nr; cpu++) {
1033 
1034 		for (thread = 0; thread < threads->nr; thread++) {
1035 			int group_fd;
1036 
1037 			if (!evsel->cgrp)
1038 				pid = threads->map[thread];
1039 
1040 			group_fd = get_group_fd(evsel, cpu, thread);
1041 retry_open:
1042 			pr_debug2("perf_event_open: pid %d  cpu %d  group_fd %d  flags %#lx\n",
1043 				  pid, cpus->map[cpu], group_fd, flags);
1044 
1045 			FD(evsel, cpu, thread) = sys_perf_event_open(&evsel->attr,
1046 								     pid,
1047 								     cpus->map[cpu],
1048 								     group_fd, flags);
1049 			if (FD(evsel, cpu, thread) < 0) {
1050 				err = -errno;
1051 				goto try_fallback;
1052 			}
1053 			set_rlimit = NO_CHANGE;
1054 		}
1055 	}
1056 
1057 	return 0;
1058 
1059 try_fallback:
1060 	/*
1061 	 * perf stat needs between 5 and 22 fds per CPU. When we run out
1062 	 * of them try to increase the limits.
1063 	 */
1064 	if (err == -EMFILE && set_rlimit < INCREASED_MAX) {
1065 		struct rlimit l;
1066 		int old_errno = errno;
1067 
1068 		if (getrlimit(RLIMIT_NOFILE, &l) == 0) {
1069 			if (set_rlimit == NO_CHANGE)
1070 				l.rlim_cur = l.rlim_max;
1071 			else {
1072 				l.rlim_cur = l.rlim_max + 1000;
1073 				l.rlim_max = l.rlim_cur;
1074 			}
1075 			if (setrlimit(RLIMIT_NOFILE, &l) == 0) {
1076 				set_rlimit++;
1077 				errno = old_errno;
1078 				goto retry_open;
1079 			}
1080 		}
1081 		errno = old_errno;
1082 	}
1083 
1084 	if (err != -EINVAL || cpu > 0 || thread > 0)
1085 		goto out_close;
1086 
1087 	if (!perf_missing_features.mmap2 && evsel->attr.mmap2) {
1088 		perf_missing_features.mmap2 = true;
1089 		goto fallback_missing_features;
1090 	} else if (!perf_missing_features.exclude_guest &&
1091 		   (evsel->attr.exclude_guest || evsel->attr.exclude_host)) {
1092 		perf_missing_features.exclude_guest = true;
1093 		goto fallback_missing_features;
1094 	} else if (!perf_missing_features.sample_id_all) {
1095 		perf_missing_features.sample_id_all = true;
1096 		goto retry_sample_id;
1097 	}
1098 
1099 out_close:
1100 	do {
1101 		while (--thread >= 0) {
1102 			close(FD(evsel, cpu, thread));
1103 			FD(evsel, cpu, thread) = -1;
1104 		}
1105 		thread = threads->nr;
1106 	} while (--cpu >= 0);
1107 	return err;
1108 }
1109 
1110 void perf_evsel__close(struct perf_evsel *evsel, int ncpus, int nthreads)
1111 {
1112 	if (evsel->fd == NULL)
1113 		return;
1114 
1115 	perf_evsel__close_fd(evsel, ncpus, nthreads);
1116 	perf_evsel__free_fd(evsel);
1117 	evsel->fd = NULL;
1118 }
1119 
1120 static struct {
1121 	struct cpu_map map;
1122 	int cpus[1];
1123 } empty_cpu_map = {
1124 	.map.nr	= 1,
1125 	.cpus	= { -1, },
1126 };
1127 
1128 static struct {
1129 	struct thread_map map;
1130 	int threads[1];
1131 } empty_thread_map = {
1132 	.map.nr	 = 1,
1133 	.threads = { -1, },
1134 };
1135 
1136 int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
1137 		     struct thread_map *threads)
1138 {
1139 	if (cpus == NULL) {
1140 		/* Work around old compiler warnings about strict aliasing */
1141 		cpus = &empty_cpu_map.map;
1142 	}
1143 
1144 	if (threads == NULL)
1145 		threads = &empty_thread_map.map;
1146 
1147 	return __perf_evsel__open(evsel, cpus, threads);
1148 }
1149 
1150 int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
1151 			     struct cpu_map *cpus)
1152 {
1153 	return __perf_evsel__open(evsel, cpus, &empty_thread_map.map);
1154 }
1155 
1156 int perf_evsel__open_per_thread(struct perf_evsel *evsel,
1157 				struct thread_map *threads)
1158 {
1159 	return __perf_evsel__open(evsel, &empty_cpu_map.map, threads);
1160 }
1161 
1162 static int perf_evsel__parse_id_sample(const struct perf_evsel *evsel,
1163 				       const union perf_event *event,
1164 				       struct perf_sample *sample)
1165 {
1166 	u64 type = evsel->attr.sample_type;
1167 	const u64 *array = event->sample.array;
1168 	bool swapped = evsel->needs_swap;
1169 	union u64_swap u;
1170 
1171 	array += ((event->header.size -
1172 		   sizeof(event->header)) / sizeof(u64)) - 1;
1173 
1174 	if (type & PERF_SAMPLE_IDENTIFIER) {
1175 		sample->id = *array;
1176 		array--;
1177 	}
1178 
1179 	if (type & PERF_SAMPLE_CPU) {
1180 		u.val64 = *array;
1181 		if (swapped) {
1182 			/* undo swap of u64, then swap on individual u32s */
1183 			u.val64 = bswap_64(u.val64);
1184 			u.val32[0] = bswap_32(u.val32[0]);
1185 		}
1186 
1187 		sample->cpu = u.val32[0];
1188 		array--;
1189 	}
1190 
1191 	if (type & PERF_SAMPLE_STREAM_ID) {
1192 		sample->stream_id = *array;
1193 		array--;
1194 	}
1195 
1196 	if (type & PERF_SAMPLE_ID) {
1197 		sample->id = *array;
1198 		array--;
1199 	}
1200 
1201 	if (type & PERF_SAMPLE_TIME) {
1202 		sample->time = *array;
1203 		array--;
1204 	}
1205 
1206 	if (type & PERF_SAMPLE_TID) {
1207 		u.val64 = *array;
1208 		if (swapped) {
1209 			/* undo swap of u64, then swap on individual u32s */
1210 			u.val64 = bswap_64(u.val64);
1211 			u.val32[0] = bswap_32(u.val32[0]);
1212 			u.val32[1] = bswap_32(u.val32[1]);
1213 		}
1214 
1215 		sample->pid = u.val32[0];
1216 		sample->tid = u.val32[1];
1217 	}
1218 
1219 	return 0;
1220 }
1221 
1222 static inline bool overflow(const void *endp, u16 max_size, const void *offset,
1223 			    u64 size)
1224 {
1225 	return size > max_size || offset + size > endp;
1226 }
1227 
1228 #define OVERFLOW_CHECK(offset, size, max_size)				\
1229 	do {								\
1230 		if (overflow(endp, (max_size), (offset), (size)))	\
1231 			return -EFAULT;					\
1232 	} while (0)
1233 
1234 #define OVERFLOW_CHECK_u64(offset) \
1235 	OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
1236 
1237 int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
1238 			     struct perf_sample *data)
1239 {
1240 	u64 type = evsel->attr.sample_type;
1241 	bool swapped = evsel->needs_swap;
1242 	const u64 *array;
1243 	u16 max_size = event->header.size;
1244 	const void *endp = (void *)event + max_size;
1245 	u64 sz;
1246 
1247 	/*
1248 	 * used for cross-endian analysis. See git commit 65014ab3
1249 	 * for why this goofiness is needed.
1250 	 */
1251 	union u64_swap u;
1252 
1253 	memset(data, 0, sizeof(*data));
1254 	data->cpu = data->pid = data->tid = -1;
1255 	data->stream_id = data->id = data->time = -1ULL;
1256 	data->period = 1;
1257 	data->weight = 0;
1258 
1259 	if (event->header.type != PERF_RECORD_SAMPLE) {
1260 		if (!evsel->attr.sample_id_all)
1261 			return 0;
1262 		return perf_evsel__parse_id_sample(evsel, event, data);
1263 	}
1264 
1265 	array = event->sample.array;
1266 
1267 	/*
1268 	 * The evsel's sample_size is based on PERF_SAMPLE_MASK which includes
1269 	 * up to PERF_SAMPLE_PERIOD.  After that overflow() must be used to
1270 	 * check the format does not go past the end of the event.
1271 	 */
1272 	if (evsel->sample_size + sizeof(event->header) > event->header.size)
1273 		return -EFAULT;
1274 
1275 	data->id = -1ULL;
1276 	if (type & PERF_SAMPLE_IDENTIFIER) {
1277 		data->id = *array;
1278 		array++;
1279 	}
1280 
1281 	if (type & PERF_SAMPLE_IP) {
1282 		data->ip = *array;
1283 		array++;
1284 	}
1285 
1286 	if (type & PERF_SAMPLE_TID) {
1287 		u.val64 = *array;
1288 		if (swapped) {
1289 			/* undo swap of u64, then swap on individual u32s */
1290 			u.val64 = bswap_64(u.val64);
1291 			u.val32[0] = bswap_32(u.val32[0]);
1292 			u.val32[1] = bswap_32(u.val32[1]);
1293 		}
1294 
1295 		data->pid = u.val32[0];
1296 		data->tid = u.val32[1];
1297 		array++;
1298 	}
1299 
1300 	if (type & PERF_SAMPLE_TIME) {
1301 		data->time = *array;
1302 		array++;
1303 	}
1304 
1305 	data->addr = 0;
1306 	if (type & PERF_SAMPLE_ADDR) {
1307 		data->addr = *array;
1308 		array++;
1309 	}
1310 
1311 	if (type & PERF_SAMPLE_ID) {
1312 		data->id = *array;
1313 		array++;
1314 	}
1315 
1316 	if (type & PERF_SAMPLE_STREAM_ID) {
1317 		data->stream_id = *array;
1318 		array++;
1319 	}
1320 
1321 	if (type & PERF_SAMPLE_CPU) {
1322 
1323 		u.val64 = *array;
1324 		if (swapped) {
1325 			/* undo swap of u64, then swap on individual u32s */
1326 			u.val64 = bswap_64(u.val64);
1327 			u.val32[0] = bswap_32(u.val32[0]);
1328 		}
1329 
1330 		data->cpu = u.val32[0];
1331 		array++;
1332 	}
1333 
1334 	if (type & PERF_SAMPLE_PERIOD) {
1335 		data->period = *array;
1336 		array++;
1337 	}
1338 
1339 	if (type & PERF_SAMPLE_READ) {
1340 		u64 read_format = evsel->attr.read_format;
1341 
1342 		OVERFLOW_CHECK_u64(array);
1343 		if (read_format & PERF_FORMAT_GROUP)
1344 			data->read.group.nr = *array;
1345 		else
1346 			data->read.one.value = *array;
1347 
1348 		array++;
1349 
1350 		if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1351 			OVERFLOW_CHECK_u64(array);
1352 			data->read.time_enabled = *array;
1353 			array++;
1354 		}
1355 
1356 		if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1357 			OVERFLOW_CHECK_u64(array);
1358 			data->read.time_running = *array;
1359 			array++;
1360 		}
1361 
1362 		/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1363 		if (read_format & PERF_FORMAT_GROUP) {
1364 			const u64 max_group_nr = UINT64_MAX /
1365 					sizeof(struct sample_read_value);
1366 
1367 			if (data->read.group.nr > max_group_nr)
1368 				return -EFAULT;
1369 			sz = data->read.group.nr *
1370 			     sizeof(struct sample_read_value);
1371 			OVERFLOW_CHECK(array, sz, max_size);
1372 			data->read.group.values =
1373 					(struct sample_read_value *)array;
1374 			array = (void *)array + sz;
1375 		} else {
1376 			OVERFLOW_CHECK_u64(array);
1377 			data->read.one.id = *array;
1378 			array++;
1379 		}
1380 	}
1381 
1382 	if (type & PERF_SAMPLE_CALLCHAIN) {
1383 		const u64 max_callchain_nr = UINT64_MAX / sizeof(u64);
1384 
1385 		OVERFLOW_CHECK_u64(array);
1386 		data->callchain = (struct ip_callchain *)array++;
1387 		if (data->callchain->nr > max_callchain_nr)
1388 			return -EFAULT;
1389 		sz = data->callchain->nr * sizeof(u64);
1390 		OVERFLOW_CHECK(array, sz, max_size);
1391 		array = (void *)array + sz;
1392 	}
1393 
1394 	if (type & PERF_SAMPLE_RAW) {
1395 		OVERFLOW_CHECK_u64(array);
1396 		u.val64 = *array;
1397 		if (WARN_ONCE(swapped,
1398 			      "Endianness of raw data not corrected!\n")) {
1399 			/* undo swap of u64, then swap on individual u32s */
1400 			u.val64 = bswap_64(u.val64);
1401 			u.val32[0] = bswap_32(u.val32[0]);
1402 			u.val32[1] = bswap_32(u.val32[1]);
1403 		}
1404 		data->raw_size = u.val32[0];
1405 		array = (void *)array + sizeof(u32);
1406 
1407 		OVERFLOW_CHECK(array, data->raw_size, max_size);
1408 		data->raw_data = (void *)array;
1409 		array = (void *)array + data->raw_size;
1410 	}
1411 
1412 	if (type & PERF_SAMPLE_BRANCH_STACK) {
1413 		const u64 max_branch_nr = UINT64_MAX /
1414 					  sizeof(struct branch_entry);
1415 
1416 		OVERFLOW_CHECK_u64(array);
1417 		data->branch_stack = (struct branch_stack *)array++;
1418 
1419 		if (data->branch_stack->nr > max_branch_nr)
1420 			return -EFAULT;
1421 		sz = data->branch_stack->nr * sizeof(struct branch_entry);
1422 		OVERFLOW_CHECK(array, sz, max_size);
1423 		array = (void *)array + sz;
1424 	}
1425 
1426 	if (type & PERF_SAMPLE_REGS_USER) {
1427 		OVERFLOW_CHECK_u64(array);
1428 		data->user_regs.abi = *array;
1429 		array++;
1430 
1431 		if (data->user_regs.abi) {
1432 			u64 regs_user = evsel->attr.sample_regs_user;
1433 
1434 			sz = hweight_long(regs_user) * sizeof(u64);
1435 			OVERFLOW_CHECK(array, sz, max_size);
1436 			data->user_regs.regs = (u64 *)array;
1437 			array = (void *)array + sz;
1438 		}
1439 	}
1440 
1441 	if (type & PERF_SAMPLE_STACK_USER) {
1442 		OVERFLOW_CHECK_u64(array);
1443 		sz = *array++;
1444 
1445 		data->user_stack.offset = ((char *)(array - 1)
1446 					  - (char *) event);
1447 
1448 		if (!sz) {
1449 			data->user_stack.size = 0;
1450 		} else {
1451 			OVERFLOW_CHECK(array, sz, max_size);
1452 			data->user_stack.data = (char *)array;
1453 			array = (void *)array + sz;
1454 			OVERFLOW_CHECK_u64(array);
1455 			data->user_stack.size = *array++;
1456 		}
1457 	}
1458 
1459 	data->weight = 0;
1460 	if (type & PERF_SAMPLE_WEIGHT) {
1461 		OVERFLOW_CHECK_u64(array);
1462 		data->weight = *array;
1463 		array++;
1464 	}
1465 
1466 	data->data_src = PERF_MEM_DATA_SRC_NONE;
1467 	if (type & PERF_SAMPLE_DATA_SRC) {
1468 		OVERFLOW_CHECK_u64(array);
1469 		data->data_src = *array;
1470 		array++;
1471 	}
1472 
1473 	return 0;
1474 }
1475 
1476 size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
1477 				     u64 sample_regs_user, u64 read_format)
1478 {
1479 	size_t sz, result = sizeof(struct sample_event);
1480 
1481 	if (type & PERF_SAMPLE_IDENTIFIER)
1482 		result += sizeof(u64);
1483 
1484 	if (type & PERF_SAMPLE_IP)
1485 		result += sizeof(u64);
1486 
1487 	if (type & PERF_SAMPLE_TID)
1488 		result += sizeof(u64);
1489 
1490 	if (type & PERF_SAMPLE_TIME)
1491 		result += sizeof(u64);
1492 
1493 	if (type & PERF_SAMPLE_ADDR)
1494 		result += sizeof(u64);
1495 
1496 	if (type & PERF_SAMPLE_ID)
1497 		result += sizeof(u64);
1498 
1499 	if (type & PERF_SAMPLE_STREAM_ID)
1500 		result += sizeof(u64);
1501 
1502 	if (type & PERF_SAMPLE_CPU)
1503 		result += sizeof(u64);
1504 
1505 	if (type & PERF_SAMPLE_PERIOD)
1506 		result += sizeof(u64);
1507 
1508 	if (type & PERF_SAMPLE_READ) {
1509 		result += sizeof(u64);
1510 		if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1511 			result += sizeof(u64);
1512 		if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1513 			result += sizeof(u64);
1514 		/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1515 		if (read_format & PERF_FORMAT_GROUP) {
1516 			sz = sample->read.group.nr *
1517 			     sizeof(struct sample_read_value);
1518 			result += sz;
1519 		} else {
1520 			result += sizeof(u64);
1521 		}
1522 	}
1523 
1524 	if (type & PERF_SAMPLE_CALLCHAIN) {
1525 		sz = (sample->callchain->nr + 1) * sizeof(u64);
1526 		result += sz;
1527 	}
1528 
1529 	if (type & PERF_SAMPLE_RAW) {
1530 		result += sizeof(u32);
1531 		result += sample->raw_size;
1532 	}
1533 
1534 	if (type & PERF_SAMPLE_BRANCH_STACK) {
1535 		sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1536 		sz += sizeof(u64);
1537 		result += sz;
1538 	}
1539 
1540 	if (type & PERF_SAMPLE_REGS_USER) {
1541 		if (sample->user_regs.abi) {
1542 			result += sizeof(u64);
1543 			sz = hweight_long(sample_regs_user) * sizeof(u64);
1544 			result += sz;
1545 		} else {
1546 			result += sizeof(u64);
1547 		}
1548 	}
1549 
1550 	if (type & PERF_SAMPLE_STACK_USER) {
1551 		sz = sample->user_stack.size;
1552 		result += sizeof(u64);
1553 		if (sz) {
1554 			result += sz;
1555 			result += sizeof(u64);
1556 		}
1557 	}
1558 
1559 	if (type & PERF_SAMPLE_WEIGHT)
1560 		result += sizeof(u64);
1561 
1562 	if (type & PERF_SAMPLE_DATA_SRC)
1563 		result += sizeof(u64);
1564 
1565 	return result;
1566 }
1567 
1568 int perf_event__synthesize_sample(union perf_event *event, u64 type,
1569 				  u64 sample_regs_user, u64 read_format,
1570 				  const struct perf_sample *sample,
1571 				  bool swapped)
1572 {
1573 	u64 *array;
1574 	size_t sz;
1575 	/*
1576 	 * used for cross-endian analysis. See git commit 65014ab3
1577 	 * for why this goofiness is needed.
1578 	 */
1579 	union u64_swap u;
1580 
1581 	array = event->sample.array;
1582 
1583 	if (type & PERF_SAMPLE_IDENTIFIER) {
1584 		*array = sample->id;
1585 		array++;
1586 	}
1587 
1588 	if (type & PERF_SAMPLE_IP) {
1589 		*array = sample->ip;
1590 		array++;
1591 	}
1592 
1593 	if (type & PERF_SAMPLE_TID) {
1594 		u.val32[0] = sample->pid;
1595 		u.val32[1] = sample->tid;
1596 		if (swapped) {
1597 			/*
1598 			 * Inverse of what is done in perf_evsel__parse_sample
1599 			 */
1600 			u.val32[0] = bswap_32(u.val32[0]);
1601 			u.val32[1] = bswap_32(u.val32[1]);
1602 			u.val64 = bswap_64(u.val64);
1603 		}
1604 
1605 		*array = u.val64;
1606 		array++;
1607 	}
1608 
1609 	if (type & PERF_SAMPLE_TIME) {
1610 		*array = sample->time;
1611 		array++;
1612 	}
1613 
1614 	if (type & PERF_SAMPLE_ADDR) {
1615 		*array = sample->addr;
1616 		array++;
1617 	}
1618 
1619 	if (type & PERF_SAMPLE_ID) {
1620 		*array = sample->id;
1621 		array++;
1622 	}
1623 
1624 	if (type & PERF_SAMPLE_STREAM_ID) {
1625 		*array = sample->stream_id;
1626 		array++;
1627 	}
1628 
1629 	if (type & PERF_SAMPLE_CPU) {
1630 		u.val32[0] = sample->cpu;
1631 		if (swapped) {
1632 			/*
1633 			 * Inverse of what is done in perf_evsel__parse_sample
1634 			 */
1635 			u.val32[0] = bswap_32(u.val32[0]);
1636 			u.val64 = bswap_64(u.val64);
1637 		}
1638 		*array = u.val64;
1639 		array++;
1640 	}
1641 
1642 	if (type & PERF_SAMPLE_PERIOD) {
1643 		*array = sample->period;
1644 		array++;
1645 	}
1646 
1647 	if (type & PERF_SAMPLE_READ) {
1648 		if (read_format & PERF_FORMAT_GROUP)
1649 			*array = sample->read.group.nr;
1650 		else
1651 			*array = sample->read.one.value;
1652 		array++;
1653 
1654 		if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
1655 			*array = sample->read.time_enabled;
1656 			array++;
1657 		}
1658 
1659 		if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
1660 			*array = sample->read.time_running;
1661 			array++;
1662 		}
1663 
1664 		/* PERF_FORMAT_ID is forced for PERF_SAMPLE_READ */
1665 		if (read_format & PERF_FORMAT_GROUP) {
1666 			sz = sample->read.group.nr *
1667 			     sizeof(struct sample_read_value);
1668 			memcpy(array, sample->read.group.values, sz);
1669 			array = (void *)array + sz;
1670 		} else {
1671 			*array = sample->read.one.id;
1672 			array++;
1673 		}
1674 	}
1675 
1676 	if (type & PERF_SAMPLE_CALLCHAIN) {
1677 		sz = (sample->callchain->nr + 1) * sizeof(u64);
1678 		memcpy(array, sample->callchain, sz);
1679 		array = (void *)array + sz;
1680 	}
1681 
1682 	if (type & PERF_SAMPLE_RAW) {
1683 		u.val32[0] = sample->raw_size;
1684 		if (WARN_ONCE(swapped,
1685 			      "Endianness of raw data not corrected!\n")) {
1686 			/*
1687 			 * Inverse of what is done in perf_evsel__parse_sample
1688 			 */
1689 			u.val32[0] = bswap_32(u.val32[0]);
1690 			u.val32[1] = bswap_32(u.val32[1]);
1691 			u.val64 = bswap_64(u.val64);
1692 		}
1693 		*array = u.val64;
1694 		array = (void *)array + sizeof(u32);
1695 
1696 		memcpy(array, sample->raw_data, sample->raw_size);
1697 		array = (void *)array + sample->raw_size;
1698 	}
1699 
1700 	if (type & PERF_SAMPLE_BRANCH_STACK) {
1701 		sz = sample->branch_stack->nr * sizeof(struct branch_entry);
1702 		sz += sizeof(u64);
1703 		memcpy(array, sample->branch_stack, sz);
1704 		array = (void *)array + sz;
1705 	}
1706 
1707 	if (type & PERF_SAMPLE_REGS_USER) {
1708 		if (sample->user_regs.abi) {
1709 			*array++ = sample->user_regs.abi;
1710 			sz = hweight_long(sample_regs_user) * sizeof(u64);
1711 			memcpy(array, sample->user_regs.regs, sz);
1712 			array = (void *)array + sz;
1713 		} else {
1714 			*array++ = 0;
1715 		}
1716 	}
1717 
1718 	if (type & PERF_SAMPLE_STACK_USER) {
1719 		sz = sample->user_stack.size;
1720 		*array++ = sz;
1721 		if (sz) {
1722 			memcpy(array, sample->user_stack.data, sz);
1723 			array = (void *)array + sz;
1724 			*array++ = sz;
1725 		}
1726 	}
1727 
1728 	if (type & PERF_SAMPLE_WEIGHT) {
1729 		*array = sample->weight;
1730 		array++;
1731 	}
1732 
1733 	if (type & PERF_SAMPLE_DATA_SRC) {
1734 		*array = sample->data_src;
1735 		array++;
1736 	}
1737 
1738 	return 0;
1739 }
1740 
1741 struct format_field *perf_evsel__field(struct perf_evsel *evsel, const char *name)
1742 {
1743 	return pevent_find_field(evsel->tp_format, name);
1744 }
1745 
1746 void *perf_evsel__rawptr(struct perf_evsel *evsel, struct perf_sample *sample,
1747 			 const char *name)
1748 {
1749 	struct format_field *field = perf_evsel__field(evsel, name);
1750 	int offset;
1751 
1752 	if (!field)
1753 		return NULL;
1754 
1755 	offset = field->offset;
1756 
1757 	if (field->flags & FIELD_IS_DYNAMIC) {
1758 		offset = *(int *)(sample->raw_data + field->offset);
1759 		offset &= 0xffff;
1760 	}
1761 
1762 	return sample->raw_data + offset;
1763 }
1764 
1765 u64 perf_evsel__intval(struct perf_evsel *evsel, struct perf_sample *sample,
1766 		       const char *name)
1767 {
1768 	struct format_field *field = perf_evsel__field(evsel, name);
1769 	void *ptr;
1770 	u64 value;
1771 
1772 	if (!field)
1773 		return 0;
1774 
1775 	ptr = sample->raw_data + field->offset;
1776 
1777 	switch (field->size) {
1778 	case 1:
1779 		return *(u8 *)ptr;
1780 	case 2:
1781 		value = *(u16 *)ptr;
1782 		break;
1783 	case 4:
1784 		value = *(u32 *)ptr;
1785 		break;
1786 	case 8:
1787 		value = *(u64 *)ptr;
1788 		break;
1789 	default:
1790 		return 0;
1791 	}
1792 
1793 	if (!evsel->needs_swap)
1794 		return value;
1795 
1796 	switch (field->size) {
1797 	case 2:
1798 		return bswap_16(value);
1799 	case 4:
1800 		return bswap_32(value);
1801 	case 8:
1802 		return bswap_64(value);
1803 	default:
1804 		return 0;
1805 	}
1806 
1807 	return 0;
1808 }
1809 
1810 static int comma_fprintf(FILE *fp, bool *first, const char *fmt, ...)
1811 {
1812 	va_list args;
1813 	int ret = 0;
1814 
1815 	if (!*first) {
1816 		ret += fprintf(fp, ",");
1817 	} else {
1818 		ret += fprintf(fp, ":");
1819 		*first = false;
1820 	}
1821 
1822 	va_start(args, fmt);
1823 	ret += vfprintf(fp, fmt, args);
1824 	va_end(args);
1825 	return ret;
1826 }
1827 
1828 static int __if_fprintf(FILE *fp, bool *first, const char *field, u64 value)
1829 {
1830 	if (value == 0)
1831 		return 0;
1832 
1833 	return comma_fprintf(fp, first, " %s: %" PRIu64, field, value);
1834 }
1835 
1836 #define if_print(field) printed += __if_fprintf(fp, &first, #field, evsel->attr.field)
1837 
1838 struct bit_names {
1839 	int bit;
1840 	const char *name;
1841 };
1842 
1843 static int bits__fprintf(FILE *fp, const char *field, u64 value,
1844 			 struct bit_names *bits, bool *first)
1845 {
1846 	int i = 0, printed = comma_fprintf(fp, first, " %s: ", field);
1847 	bool first_bit = true;
1848 
1849 	do {
1850 		if (value & bits[i].bit) {
1851 			printed += fprintf(fp, "%s%s", first_bit ? "" : "|", bits[i].name);
1852 			first_bit = false;
1853 		}
1854 	} while (bits[++i].name != NULL);
1855 
1856 	return printed;
1857 }
1858 
1859 static int sample_type__fprintf(FILE *fp, bool *first, u64 value)
1860 {
1861 #define bit_name(n) { PERF_SAMPLE_##n, #n }
1862 	struct bit_names bits[] = {
1863 		bit_name(IP), bit_name(TID), bit_name(TIME), bit_name(ADDR),
1864 		bit_name(READ), bit_name(CALLCHAIN), bit_name(ID), bit_name(CPU),
1865 		bit_name(PERIOD), bit_name(STREAM_ID), bit_name(RAW),
1866 		bit_name(BRANCH_STACK), bit_name(REGS_USER), bit_name(STACK_USER),
1867 		bit_name(IDENTIFIER),
1868 		{ .name = NULL, }
1869 	};
1870 #undef bit_name
1871 	return bits__fprintf(fp, "sample_type", value, bits, first);
1872 }
1873 
1874 static int read_format__fprintf(FILE *fp, bool *first, u64 value)
1875 {
1876 #define bit_name(n) { PERF_FORMAT_##n, #n }
1877 	struct bit_names bits[] = {
1878 		bit_name(TOTAL_TIME_ENABLED), bit_name(TOTAL_TIME_RUNNING),
1879 		bit_name(ID), bit_name(GROUP),
1880 		{ .name = NULL, }
1881 	};
1882 #undef bit_name
1883 	return bits__fprintf(fp, "read_format", value, bits, first);
1884 }
1885 
1886 int perf_evsel__fprintf(struct perf_evsel *evsel,
1887 			struct perf_attr_details *details, FILE *fp)
1888 {
1889 	bool first = true;
1890 	int printed = 0;
1891 
1892 	if (details->event_group) {
1893 		struct perf_evsel *pos;
1894 
1895 		if (!perf_evsel__is_group_leader(evsel))
1896 			return 0;
1897 
1898 		if (evsel->nr_members > 1)
1899 			printed += fprintf(fp, "%s{", evsel->group_name ?: "");
1900 
1901 		printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1902 		for_each_group_member(pos, evsel)
1903 			printed += fprintf(fp, ",%s", perf_evsel__name(pos));
1904 
1905 		if (evsel->nr_members > 1)
1906 			printed += fprintf(fp, "}");
1907 		goto out;
1908 	}
1909 
1910 	printed += fprintf(fp, "%s", perf_evsel__name(evsel));
1911 
1912 	if (details->verbose || details->freq) {
1913 		printed += comma_fprintf(fp, &first, " sample_freq=%" PRIu64,
1914 					 (u64)evsel->attr.sample_freq);
1915 	}
1916 
1917 	if (details->verbose) {
1918 		if_print(type);
1919 		if_print(config);
1920 		if_print(config1);
1921 		if_print(config2);
1922 		if_print(size);
1923 		printed += sample_type__fprintf(fp, &first, evsel->attr.sample_type);
1924 		if (evsel->attr.read_format)
1925 			printed += read_format__fprintf(fp, &first, evsel->attr.read_format);
1926 		if_print(disabled);
1927 		if_print(inherit);
1928 		if_print(pinned);
1929 		if_print(exclusive);
1930 		if_print(exclude_user);
1931 		if_print(exclude_kernel);
1932 		if_print(exclude_hv);
1933 		if_print(exclude_idle);
1934 		if_print(mmap);
1935 		if_print(mmap2);
1936 		if_print(comm);
1937 		if_print(freq);
1938 		if_print(inherit_stat);
1939 		if_print(enable_on_exec);
1940 		if_print(task);
1941 		if_print(watermark);
1942 		if_print(precise_ip);
1943 		if_print(mmap_data);
1944 		if_print(sample_id_all);
1945 		if_print(exclude_host);
1946 		if_print(exclude_guest);
1947 		if_print(__reserved_1);
1948 		if_print(wakeup_events);
1949 		if_print(bp_type);
1950 		if_print(branch_sample_type);
1951 	}
1952 out:
1953 	fputc('\n', fp);
1954 	return ++printed;
1955 }
1956 
1957 bool perf_evsel__fallback(struct perf_evsel *evsel, int err,
1958 			  char *msg, size_t msgsize)
1959 {
1960 	if ((err == ENOENT || err == ENXIO || err == ENODEV) &&
1961 	    evsel->attr.type   == PERF_TYPE_HARDWARE &&
1962 	    evsel->attr.config == PERF_COUNT_HW_CPU_CYCLES) {
1963 		/*
1964 		 * If it's cycles then fall back to hrtimer based
1965 		 * cpu-clock-tick sw counter, which is always available even if
1966 		 * no PMU support.
1967 		 *
1968 		 * PPC returns ENXIO until 2.6.37 (behavior changed with commit
1969 		 * b0a873e).
1970 		 */
1971 		scnprintf(msg, msgsize, "%s",
1972 "The cycles event is not supported, trying to fall back to cpu-clock-ticks");
1973 
1974 		evsel->attr.type   = PERF_TYPE_SOFTWARE;
1975 		evsel->attr.config = PERF_COUNT_SW_CPU_CLOCK;
1976 
1977 		free(evsel->name);
1978 		evsel->name = NULL;
1979 		return true;
1980 	}
1981 
1982 	return false;
1983 }
1984 
1985 int perf_evsel__open_strerror(struct perf_evsel *evsel,
1986 			      struct perf_target *target,
1987 			      int err, char *msg, size_t size)
1988 {
1989 	switch (err) {
1990 	case EPERM:
1991 	case EACCES:
1992 		return scnprintf(msg, size,
1993 		 "You may not have permission to collect %sstats.\n"
1994 		 "Consider tweaking /proc/sys/kernel/perf_event_paranoid:\n"
1995 		 " -1 - Not paranoid at all\n"
1996 		 "  0 - Disallow raw tracepoint access for unpriv\n"
1997 		 "  1 - Disallow cpu events for unpriv\n"
1998 		 "  2 - Disallow kernel profiling for unpriv",
1999 				 target->system_wide ? "system-wide " : "");
2000 	case ENOENT:
2001 		return scnprintf(msg, size, "The %s event is not supported.",
2002 				 perf_evsel__name(evsel));
2003 	case EMFILE:
2004 		return scnprintf(msg, size, "%s",
2005 			 "Too many events are opened.\n"
2006 			 "Try again after reducing the number of events.");
2007 	case ENODEV:
2008 		if (target->cpu_list)
2009 			return scnprintf(msg, size, "%s",
2010 	 "No such device - did you specify an out-of-range profile CPU?\n");
2011 		break;
2012 	case EOPNOTSUPP:
2013 		if (evsel->attr.precise_ip)
2014 			return scnprintf(msg, size, "%s",
2015 	"\'precise\' request may not be supported. Try removing 'p' modifier.");
2016 #if defined(__i386__) || defined(__x86_64__)
2017 		if (evsel->attr.type == PERF_TYPE_HARDWARE)
2018 			return scnprintf(msg, size, "%s",
2019 	"No hardware sampling interrupt available.\n"
2020 	"No APIC? If so then you can boot the kernel with the \"lapic\" boot parameter to force-enable it.");
2021 #endif
2022 		break;
2023 	default:
2024 		break;
2025 	}
2026 
2027 	return scnprintf(msg, size,
2028 	"The sys_perf_event_open() syscall returned with %d (%s) for event (%s).  \n"
2029 	"/bin/dmesg may provide additional information.\n"
2030 	"No CONFIG_PERF_EVENTS=y kernel support configured?\n",
2031 			 err, strerror(err), perf_evsel__name(evsel));
2032 }
2033