xref: /linux/tools/perf/arch/x86/util/evsel.c (revision 6490dda55dcabbd5cf408387f932c9343a22c872)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include "util/evsel.h"
5 #include "util/env.h"
6 #include "util/pmu.h"
7 #include "util/pmus.h"
8 #include "linux/string.h"
9 #include "evsel.h"
10 #include "util/debug.h"
11 #include "env.h"
12 
13 #define IBS_FETCH_L3MISSONLY   (1ULL << 59)
14 #define IBS_OP_L3MISSONLY      (1ULL << 16)
15 
16 void arch_evsel__set_sample_weight(struct evsel *evsel)
17 {
18 	evsel__set_sample_bit(evsel, WEIGHT_STRUCT);
19 }
20 
21 /* Check whether the evsel's PMU supports the perf metrics */
22 bool evsel__sys_has_perf_metrics(const struct evsel *evsel)
23 {
24 	struct perf_pmu *pmu;
25 	u32 type = evsel->core.attr.type;
26 
27 	/*
28 	 * The PERF_TYPE_RAW type is the core PMU type, e.g., "cpu" PMU
29 	 * on a non-hybrid machine, "cpu_core" PMU on a hybrid machine.
30 	 * The slots event is only available for the core PMU, which
31 	 * supports the perf metrics feature.
32 	 * Checking both the PERF_TYPE_RAW type and the slots event
33 	 * should be good enough to detect the perf metrics feature.
34 	 */
35 again:
36 	switch (type) {
37 	case PERF_TYPE_HARDWARE:
38 	case PERF_TYPE_HW_CACHE:
39 		type = evsel->core.attr.config >> PERF_PMU_TYPE_SHIFT;
40 		if (type)
41 			goto again;
42 		break;
43 	case PERF_TYPE_RAW:
44 		break;
45 	default:
46 		return false;
47 	}
48 
49 	pmu = evsel->pmu;
50 	if (pmu && perf_pmu__is_fake(pmu))
51 		pmu = NULL;
52 
53 	if (!pmu) {
54 		while ((pmu = perf_pmus__scan_core(pmu)) != NULL) {
55 			if (pmu->type == PERF_TYPE_RAW)
56 				break;
57 		}
58 	}
59 	return pmu && perf_pmu__have_event(pmu, "slots");
60 }
61 
62 bool arch_evsel__must_be_in_group(const struct evsel *evsel)
63 {
64 	if (!evsel__sys_has_perf_metrics(evsel) || !evsel->name ||
65 	    strcasestr(evsel->name, "uops_retired.slots"))
66 		return false;
67 
68 	return strcasestr(evsel->name, "topdown") || strcasestr(evsel->name, "slots");
69 }
70 
71 int arch_evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
72 {
73 	u64 event = evsel->core.attr.config & PERF_HW_EVENT_MASK;
74 	u64 pmu = evsel->core.attr.config >> PERF_PMU_TYPE_SHIFT;
75 	const char *event_name;
76 
77 	if (event < PERF_COUNT_HW_MAX && evsel__hw_names[event])
78 		event_name = evsel__hw_names[event];
79 	else
80 		event_name = "unknown-hardware";
81 
82 	/* The PMU type is not required for the non-hybrid platform. */
83 	if (!pmu)
84 		return  scnprintf(bf, size, "%s", event_name);
85 
86 	return scnprintf(bf, size, "%s/%s/",
87 			 evsel->pmu ? evsel->pmu->name : "cpu",
88 			 event_name);
89 }
90 
91 static void ibs_l3miss_warn(void)
92 {
93 	pr_warning(
94 "WARNING: Hw internally resets sampling period when L3 Miss Filtering is enabled\n"
95 "and tagged operation does not cause L3 Miss. This causes sampling period skew.\n");
96 }
97 
98 void arch__post_evsel_config(struct evsel *evsel, struct perf_event_attr *attr)
99 {
100 	struct perf_pmu *evsel_pmu, *ibs_fetch_pmu, *ibs_op_pmu;
101 	static int warned_once;
102 
103 	if (warned_once || !x86__is_amd_cpu())
104 		return;
105 
106 	evsel_pmu = evsel__find_pmu(evsel);
107 	if (!evsel_pmu)
108 		return;
109 
110 	ibs_fetch_pmu = perf_pmus__find("ibs_fetch");
111 	ibs_op_pmu = perf_pmus__find("ibs_op");
112 
113 	if (ibs_fetch_pmu && ibs_fetch_pmu->type == evsel_pmu->type) {
114 		if (attr->config & IBS_FETCH_L3MISSONLY) {
115 			ibs_l3miss_warn();
116 			warned_once = 1;
117 		}
118 	} else if (ibs_op_pmu && ibs_op_pmu->type == evsel_pmu->type) {
119 		if (attr->config & IBS_OP_L3MISSONLY) {
120 			ibs_l3miss_warn();
121 			warned_once = 1;
122 		}
123 	}
124 }
125 
126 int arch_evsel__open_strerror(struct evsel *evsel, char *msg, size_t size)
127 {
128 	if (!x86__is_amd_cpu())
129 		return 0;
130 
131 	if (!evsel->core.attr.precise_ip &&
132 	    !(evsel->pmu && !strncmp(evsel->pmu->name, "ibs", 3)))
133 		return 0;
134 
135 	/* More verbose IBS errors. */
136 	if (evsel->core.attr.exclude_kernel || evsel->core.attr.exclude_user ||
137 	    evsel->core.attr.exclude_hv || evsel->core.attr.exclude_idle ||
138 	    evsel->core.attr.exclude_host || evsel->core.attr.exclude_guest) {
139 		return scnprintf(msg, size, "AMD IBS doesn't support privilege filtering. Try "
140 				 "again without the privilege modifiers (like 'k') at the end.");
141 	}
142 
143 	return 0;
144 }
145