xref: /linux/tools/perf/arch/x86/util/topdown.c (revision c34e9ab9a612ee8b18273398ef75c207b01f516d)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "api/fs/fs.h"
3 #include "util/evsel.h"
4 #include "util/evlist.h"
5 #include "util/pmu.h"
6 #include "util/pmus.h"
7 #include "util/topdown.h"
8 #include "topdown.h"
9 #include "evsel.h"
10 
11 /* Check whether there is a PMU which supports the perf metrics. */
12 bool topdown_sys_has_perf_metrics(void)
13 {
14 	static bool has_perf_metrics;
15 	static bool cached;
16 	struct perf_pmu *pmu;
17 
18 	if (cached)
19 		return has_perf_metrics;
20 
21 	/*
22 	 * The perf metrics feature is a core PMU feature.
23 	 * The PERF_TYPE_RAW type is the type of a core PMU.
24 	 * The slots event is only available when the core PMU
25 	 * supports the perf metrics feature.
26 	 */
27 	pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
28 	if (pmu && perf_pmu__have_event(pmu, "slots"))
29 		has_perf_metrics = true;
30 
31 	cached = true;
32 	return has_perf_metrics;
33 }
34 
35 #define TOPDOWN_SLOTS		0x0400
36 bool arch_is_topdown_slots(const struct evsel *evsel)
37 {
38 	if (evsel->core.attr.config == TOPDOWN_SLOTS)
39 		return true;
40 
41 	return false;
42 }
43 
44 bool arch_is_topdown_metrics(const struct evsel *evsel)
45 {
46 	int config = evsel->core.attr.config;
47 	const char *name_from_config;
48 	struct perf_pmu *pmu;
49 
50 	/* All topdown events have an event code of 0. */
51 	if ((config & 0xFF) != 0)
52 		return false;
53 
54 	pmu = evsel__find_pmu(evsel);
55 	if (!pmu || !pmu->is_core)
56 		return false;
57 
58 	name_from_config = perf_pmu__name_from_config(pmu, config);
59 	return name_from_config && strcasestr(name_from_config, "topdown");
60 }
61 
62 /*
63  * Check whether a topdown group supports sample-read.
64  *
65  * Only Topdown metric supports sample-read. The slots
66  * event must be the leader of the topdown group.
67  */
68 bool arch_topdown_sample_read(struct evsel *leader)
69 {
70 	struct evsel *evsel;
71 
72 	if (!evsel__sys_has_perf_metrics(leader))
73 		return false;
74 
75 	if (!arch_is_topdown_slots(leader))
76 		return false;
77 
78 	/*
79 	 * If slots event as leader event but no topdown metric events
80 	 * in group, slots event should still sample as leader.
81 	 */
82 	evlist__for_each_entry(leader->evlist, evsel) {
83 		if (evsel->core.leader != leader->core.leader)
84 			return false;
85 		if (evsel != leader && arch_is_topdown_metrics(evsel))
86 			return true;
87 	}
88 
89 	return false;
90 }
91