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
7/*
8 * Check whether we can use a group for top down.
9 * Without a group may get bad results due to multiplexing.
10 */
11bool arch_topdown_check_group(bool *warn)
12{
13	int n;
14
15	if (sysctl__read_int("kernel/nmi_watchdog", &n) < 0)
16		return false;
17	if (n > 0) {
18		*warn = true;
19		return false;
20	}
21	return true;
22}
23
24void arch_topdown_group_warn(void)
25{
26	fprintf(stderr,
27		"nmi_watchdog enabled with topdown. May give wrong results.\n"
28		"Disable with echo 0 > /proc/sys/kernel/nmi_watchdog\n");
29}
30
31#define TOPDOWN_SLOTS		0x0400
32
33static bool is_topdown_slots_event(struct evsel *counter)
34{
35	if (!counter->pmu_name)
36		return false;
37
38	if (strcmp(counter->pmu_name, "cpu"))
39		return false;
40
41	if (counter->core.attr.config == TOPDOWN_SLOTS)
42		return true;
43
44	return false;
45}
46
47/*
48 * Check whether a topdown group supports sample-read.
49 *
50 * Only Topdown metic supports sample-read. The slots
51 * event must be the leader of the topdown group.
52 */
53
54bool arch_topdown_sample_read(struct evsel *leader)
55{
56	if (!pmu_have_event("cpu", "slots"))
57		return false;
58
59	if (is_topdown_slots_event(leader))
60		return true;
61
62	return false;
63}
64