xref: /openbmc/linux/tools/perf/arch/x86/util/topdown.c (revision 2dd6532e)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include "api/fs/fs.h"
4 #include "util/pmu.h"
5 #include "util/topdown.h"
6 #include "topdown.h"
7 #include "evsel.h"
8 
9 /* Check whether there is a PMU which supports the perf metrics. */
10 bool topdown_sys_has_perf_metrics(void)
11 {
12 	static bool has_perf_metrics;
13 	static bool cached;
14 	struct perf_pmu *pmu;
15 
16 	if (cached)
17 		return has_perf_metrics;
18 
19 	/*
20 	 * The perf metrics feature is a core PMU feature.
21 	 * The PERF_TYPE_RAW type is the type of a core PMU.
22 	 * The slots event is only available when the core PMU
23 	 * supports the perf metrics feature.
24 	 */
25 	pmu = perf_pmu__find_by_type(PERF_TYPE_RAW);
26 	if (pmu && pmu_have_event(pmu->name, "slots"))
27 		has_perf_metrics = true;
28 
29 	cached = true;
30 	return has_perf_metrics;
31 }
32 
33 /*
34  * Check whether we can use a group for top down.
35  * Without a group may get bad results due to multiplexing.
36  */
37 bool arch_topdown_check_group(bool *warn)
38 {
39 	int n;
40 
41 	if (sysctl__read_int("kernel/nmi_watchdog", &n) < 0)
42 		return false;
43 	if (n > 0) {
44 		*warn = true;
45 		return false;
46 	}
47 	return true;
48 }
49 
50 void arch_topdown_group_warn(void)
51 {
52 	fprintf(stderr,
53 		"nmi_watchdog enabled with topdown. May give wrong results.\n"
54 		"Disable with echo 0 > /proc/sys/kernel/nmi_watchdog\n");
55 }
56 
57 #define TOPDOWN_SLOTS		0x0400
58 
59 /*
60  * Check whether a topdown group supports sample-read.
61  *
62  * Only Topdown metric supports sample-read. The slots
63  * event must be the leader of the topdown group.
64  */
65 
66 bool arch_topdown_sample_read(struct evsel *leader)
67 {
68 	if (!evsel__sys_has_perf_metrics(leader))
69 		return false;
70 
71 	if (leader->core.attr.config == TOPDOWN_SLOTS)
72 		return true;
73 
74 	return false;
75 }
76