Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / perf / arch / x86 / util / topdown.c
blobf63747d0abdf9e46ac41fb6b9faa54e115eed6b5
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"
11 /* Check whether there is a PMU which supports the perf metrics. */
12 bool topdown_sys_has_perf_metrics(void)
14 static bool has_perf_metrics;
15 static bool cached;
16 struct perf_pmu *pmu;
18 if (cached)
19 return has_perf_metrics;
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.
27 pmu = perf_pmus__find_by_type(PERF_TYPE_RAW);
28 if (pmu && perf_pmu__have_event(pmu, "slots"))
29 has_perf_metrics = true;
31 cached = true;
32 return has_perf_metrics;
35 #define TOPDOWN_SLOTS 0x0400
36 bool arch_is_topdown_slots(const struct evsel *evsel)
38 if (evsel->core.attr.config == TOPDOWN_SLOTS)
39 return true;
41 return false;
44 bool arch_is_topdown_metrics(const struct evsel *evsel)
46 int config = evsel->core.attr.config;
47 const char *name_from_config;
48 struct perf_pmu *pmu;
50 /* All topdown events have an event code of 0. */
51 if ((config & 0xFF) != 0)
52 return false;
54 pmu = evsel__find_pmu(evsel);
55 if (!pmu || !pmu->is_core)
56 return false;
58 name_from_config = perf_pmu__name_from_config(pmu, config);
59 return name_from_config && strcasestr(name_from_config, "topdown");
63 * Check whether a topdown group supports sample-read.
65 * Only Topdown metric supports sample-read. The slots
66 * event must be the leader of the topdown group.
68 bool arch_topdown_sample_read(struct evsel *leader)
70 struct evsel *evsel;
72 if (!evsel__sys_has_perf_metrics(leader))
73 return false;
75 if (!arch_is_topdown_slots(leader))
76 return false;
79 * If slots event as leader event but no topdown metric events
80 * in group, slots event should still sample as leader.
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;
89 return false;