1ea8d0ed6SKan Liang // SPDX-License-Identifier: GPL-2.0
2ea8d0ed6SKan Liang #include <stdio.h>
3eb39bf32SRavi Bangoria #include <stdlib.h>
4ea8d0ed6SKan Liang #include "util/evsel.h"
5eb39bf32SRavi Bangoria #include "util/env.h"
6d98079c0SIan Rogers #include "util/pmu.h"
71eaf496eSIan Rogers #include "util/pmus.h"
8eb39bf32SRavi Bangoria #include "linux/string.h"
9151e7d75SZhengjun Xing #include "evsel.h"
109ab95b0bSRavi Bangoria #include "util/debug.h"
110cd1ca46SRavi Bangoria #include "env.h"
129ab95b0bSRavi Bangoria
139ab95b0bSRavi Bangoria #define IBS_FETCH_L3MISSONLY (1ULL << 59)
149ab95b0bSRavi Bangoria #define IBS_OP_L3MISSONLY (1ULL << 16)
15ea8d0ed6SKan Liang
arch_evsel__set_sample_weight(struct evsel * evsel)16ea8d0ed6SKan Liang void arch_evsel__set_sample_weight(struct evsel *evsel)
17ea8d0ed6SKan Liang {
18ea8d0ed6SKan Liang evsel__set_sample_bit(evsel, WEIGHT_STRUCT);
19ea8d0ed6SKan Liang }
20eb39bf32SRavi Bangoria
2139d5f412SKan Liang /* Check whether the evsel's PMU supports the perf metrics */
evsel__sys_has_perf_metrics(const struct evsel * evsel)22151e7d75SZhengjun Xing bool evsel__sys_has_perf_metrics(const struct evsel *evsel)
2339d5f412SKan Liang {
2439d5f412SKan Liang const char *pmu_name = evsel->pmu_name ? evsel->pmu_name : "cpu";
2539d5f412SKan Liang
2639d5f412SKan Liang /*
2739d5f412SKan Liang * The PERF_TYPE_RAW type is the core PMU type, e.g., "cpu" PMU
2839d5f412SKan Liang * on a non-hybrid machine, "cpu_core" PMU on a hybrid machine.
2939d5f412SKan Liang * The slots event is only available for the core PMU, which
3039d5f412SKan Liang * supports the perf metrics feature.
3139d5f412SKan Liang * Checking both the PERF_TYPE_RAW type and the slots event
3239d5f412SKan Liang * should be good enough to detect the perf metrics feature.
3339d5f412SKan Liang */
3439d5f412SKan Liang if ((evsel->core.attr.type == PERF_TYPE_RAW) &&
351eaf496eSIan Rogers perf_pmus__have_event(pmu_name, "slots"))
3639d5f412SKan Liang return true;
3739d5f412SKan Liang
3839d5f412SKan Liang return false;
3939d5f412SKan Liang }
4039d5f412SKan Liang
arch_evsel__must_be_in_group(const struct evsel * evsel)41d98079c0SIan Rogers bool arch_evsel__must_be_in_group(const struct evsel *evsel)
42d98079c0SIan Rogers {
43*714b4511SIan Rogers if (!evsel__sys_has_perf_metrics(evsel) || !evsel->name ||
44*714b4511SIan Rogers strcasestr(evsel->name, "uops_retired.slots"))
45d98079c0SIan Rogers return false;
46d98079c0SIan Rogers
47*714b4511SIan Rogers return strcasestr(evsel->name, "topdown") || strcasestr(evsel->name, "slots");
48d98079c0SIan Rogers }
499ab95b0bSRavi Bangoria
arch_evsel__hw_name(struct evsel * evsel,char * bf,size_t size)50ff4207f7SKan Liang int arch_evsel__hw_name(struct evsel *evsel, char *bf, size_t size)
51ff4207f7SKan Liang {
52ff4207f7SKan Liang u64 event = evsel->core.attr.config & PERF_HW_EVENT_MASK;
53ff4207f7SKan Liang u64 pmu = evsel->core.attr.config >> PERF_PMU_TYPE_SHIFT;
54ff4207f7SKan Liang const char *event_name;
55ff4207f7SKan Liang
56ff4207f7SKan Liang if (event < PERF_COUNT_HW_MAX && evsel__hw_names[event])
57ff4207f7SKan Liang event_name = evsel__hw_names[event];
58ff4207f7SKan Liang else
59ff4207f7SKan Liang event_name = "unknown-hardware";
60ff4207f7SKan Liang
61ff4207f7SKan Liang /* The PMU type is not required for the non-hybrid platform. */
62ff4207f7SKan Liang if (!pmu)
63ff4207f7SKan Liang return scnprintf(bf, size, "%s", event_name);
64ff4207f7SKan Liang
65ff4207f7SKan Liang return scnprintf(bf, size, "%s/%s/",
66ff4207f7SKan Liang evsel->pmu_name ? evsel->pmu_name : "cpu",
67ff4207f7SKan Liang event_name);
68ff4207f7SKan Liang }
69ff4207f7SKan Liang
ibs_l3miss_warn(void)709ab95b0bSRavi Bangoria static void ibs_l3miss_warn(void)
719ab95b0bSRavi Bangoria {
729ab95b0bSRavi Bangoria pr_warning(
739ab95b0bSRavi Bangoria "WARNING: Hw internally resets sampling period when L3 Miss Filtering is enabled\n"
749ab95b0bSRavi Bangoria "and tagged operation does not cause L3 Miss. This causes sampling period skew.\n");
759ab95b0bSRavi Bangoria }
769ab95b0bSRavi Bangoria
arch__post_evsel_config(struct evsel * evsel,struct perf_event_attr * attr)779ab95b0bSRavi Bangoria void arch__post_evsel_config(struct evsel *evsel, struct perf_event_attr *attr)
789ab95b0bSRavi Bangoria {
799ab95b0bSRavi Bangoria struct perf_pmu *evsel_pmu, *ibs_fetch_pmu, *ibs_op_pmu;
809ab95b0bSRavi Bangoria static int warned_once;
819ab95b0bSRavi Bangoria
820cd1ca46SRavi Bangoria if (warned_once || !x86__is_amd_cpu())
839ab95b0bSRavi Bangoria return;
849ab95b0bSRavi Bangoria
859ab95b0bSRavi Bangoria evsel_pmu = evsel__find_pmu(evsel);
869ab95b0bSRavi Bangoria if (!evsel_pmu)
879ab95b0bSRavi Bangoria return;
889ab95b0bSRavi Bangoria
891eaf496eSIan Rogers ibs_fetch_pmu = perf_pmus__find("ibs_fetch");
901eaf496eSIan Rogers ibs_op_pmu = perf_pmus__find("ibs_op");
919ab95b0bSRavi Bangoria
929ab95b0bSRavi Bangoria if (ibs_fetch_pmu && ibs_fetch_pmu->type == evsel_pmu->type) {
939ab95b0bSRavi Bangoria if (attr->config & IBS_FETCH_L3MISSONLY) {
949ab95b0bSRavi Bangoria ibs_l3miss_warn();
959ab95b0bSRavi Bangoria warned_once = 1;
969ab95b0bSRavi Bangoria }
979ab95b0bSRavi Bangoria } else if (ibs_op_pmu && ibs_op_pmu->type == evsel_pmu->type) {
989ab95b0bSRavi Bangoria if (attr->config & IBS_OP_L3MISSONLY) {
999ab95b0bSRavi Bangoria ibs_l3miss_warn();
1009ab95b0bSRavi Bangoria warned_once = 1;
1019ab95b0bSRavi Bangoria }
1029ab95b0bSRavi Bangoria }
1039ab95b0bSRavi Bangoria }
104b2ad9549SRavi Bangoria
arch_evsel__open_strerror(struct evsel * evsel,char * msg,size_t size)105b2ad9549SRavi Bangoria int arch_evsel__open_strerror(struct evsel *evsel, char *msg, size_t size)
106b2ad9549SRavi Bangoria {
107b2ad9549SRavi Bangoria if (!x86__is_amd_cpu())
108b2ad9549SRavi Bangoria return 0;
109b2ad9549SRavi Bangoria
110b2ad9549SRavi Bangoria if (!evsel->core.attr.precise_ip &&
111b2ad9549SRavi Bangoria !(evsel->pmu_name && !strncmp(evsel->pmu_name, "ibs", 3)))
112b2ad9549SRavi Bangoria return 0;
113b2ad9549SRavi Bangoria
114b2ad9549SRavi Bangoria /* More verbose IBS errors. */
115b2ad9549SRavi Bangoria if (evsel->core.attr.exclude_kernel || evsel->core.attr.exclude_user ||
116b2ad9549SRavi Bangoria evsel->core.attr.exclude_hv || evsel->core.attr.exclude_idle ||
117b2ad9549SRavi Bangoria evsel->core.attr.exclude_host || evsel->core.attr.exclude_guest) {
118b2ad9549SRavi Bangoria return scnprintf(msg, size, "AMD IBS doesn't support privilege filtering. Try "
119b2ad9549SRavi Bangoria "again without the privilege modifiers (like 'k') at the end.");
120b2ad9549SRavi Bangoria }
121b2ad9549SRavi Bangoria
122b2ad9549SRavi Bangoria return 0;
123b2ad9549SRavi Bangoria }
124