18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <stdio.h>
38c2ecf20Sopenharmony_ci#include "api/fs/fs.h"
48c2ecf20Sopenharmony_ci#include "util/pmu.h"
58c2ecf20Sopenharmony_ci#include "util/topdown.h"
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci/*
88c2ecf20Sopenharmony_ci * Check whether we can use a group for top down.
98c2ecf20Sopenharmony_ci * Without a group may get bad results due to multiplexing.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_cibool arch_topdown_check_group(bool *warn)
128c2ecf20Sopenharmony_ci{
138c2ecf20Sopenharmony_ci	int n;
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci	if (sysctl__read_int("kernel/nmi_watchdog", &n) < 0)
168c2ecf20Sopenharmony_ci		return false;
178c2ecf20Sopenharmony_ci	if (n > 0) {
188c2ecf20Sopenharmony_ci		*warn = true;
198c2ecf20Sopenharmony_ci		return false;
208c2ecf20Sopenharmony_ci	}
218c2ecf20Sopenharmony_ci	return true;
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_civoid arch_topdown_group_warn(void)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	fprintf(stderr,
278c2ecf20Sopenharmony_ci		"nmi_watchdog enabled with topdown. May give wrong results.\n"
288c2ecf20Sopenharmony_ci		"Disable with echo 0 > /proc/sys/kernel/nmi_watchdog\n");
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define TOPDOWN_SLOTS		0x0400
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic bool is_topdown_slots_event(struct evsel *counter)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	if (!counter->pmu_name)
368c2ecf20Sopenharmony_ci		return false;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci	if (strcmp(counter->pmu_name, "cpu"))
398c2ecf20Sopenharmony_ci		return false;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	if (counter->core.attr.config == TOPDOWN_SLOTS)
428c2ecf20Sopenharmony_ci		return true;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return false;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci/*
488c2ecf20Sopenharmony_ci * Check whether a topdown group supports sample-read.
498c2ecf20Sopenharmony_ci *
508c2ecf20Sopenharmony_ci * Only Topdown metic supports sample-read. The slots
518c2ecf20Sopenharmony_ci * event must be the leader of the topdown group.
528c2ecf20Sopenharmony_ci */
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cibool arch_topdown_sample_read(struct evsel *leader)
558c2ecf20Sopenharmony_ci{
568c2ecf20Sopenharmony_ci	if (!pmu_have_event("cpu", "slots"))
578c2ecf20Sopenharmony_ci		return false;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	if (is_topdown_slots_event(leader))
608c2ecf20Sopenharmony_ci		return true;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	return false;
638c2ecf20Sopenharmony_ci}
64