linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Kilroy <andrew.kilroy@arm.com>
To: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
	acme@kernel.org, john.garry@huawei.com, irogers@google.com,
	ak@linux.intel.com
Cc: Andrew Kilroy <andrew.kilroy@arm.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@redhat.com>, Namhyung Kim <namhyung@kernel.org>
Subject: [RFC PATCH v2 4/5] perf stat: Detect if topdown kernel events supported
Date: Tue, 11 Jan 2022 15:07:48 +0000	[thread overview]
Message-ID: <20220111150749.13365-5-andrew.kilroy@arm.com> (raw)
In-Reply-To: <20220111150749.13365-1-andrew.kilroy@arm.com>

This patch checks if the kernel events topdown implementation can be set
up.

Do this by iterating over two arrays defining what kernel events should
be present.  If one of those arrays define at least one event that is
present, the kernel events are supported.

If no topdown kernel events are detected, the json metrics approach is
attempted.

Signed-off-by: Andrew Kilroy <andrew.kilroy@arm.com>
---
 tools/perf/builtin-stat.c | 114 +++++++++++++++++++++++++++++++++++---
 1 file changed, 106 insertions(+), 8 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 6122f3a764f8..2f579d29f9f5 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -93,6 +93,7 @@
 
 #include <linux/ctype.h>
 #include <perf/evlist.h>
+#include <linux/string.h>
 
 #define DEFAULT_SEPARATOR	" "
 #define FREEZE_ON_SMI_PATH	"devices/cpu/freeze_on_smi"
@@ -1649,6 +1650,75 @@ static int perf_stat_init_aggr_mode_file(struct perf_stat *st)
 	return 0;
 }
 
+// Return the number of elements in the array excluding the final
+// NULL array element.
+static size_t str_array_len(const char **str)
+{
+	size_t c = 0;
+	while (str[c] != NULL) {
+		++c;
+	}
+	return c;
+}
+
+// Checks if topdown kernel events listed by the given
+// array of event names are supported.
+//
+// The input array is not modified.
+//
+// Returns 1 if supported,
+//         0 if not supported,
+//         -1 if some unexpected error occurred while checking
+static int check_events_available(const char **orig_events_array)
+{
+	char *str = NULL;
+	size_t basic_events_len = str_array_len(orig_events_array);
+	size_t basic_events_cpy_bytes = sizeof(const char *) * (basic_events_len + 1);
+	const char **basic_events = NULL;
+
+	// This function shouldn't have any side effects.
+	// Since topdown_filter_events mutates the arrays it inspects,
+	// this function takes temporary shallow copies of the input
+	// string array
+	basic_events = memdup(orig_events_array, basic_events_cpy_bytes);
+	if (basic_events == NULL) {
+		pr_err("Out of memory, could not copy topdown events array\n");
+		return -1;
+	}
+
+	if (topdown_filter_events(basic_events, &str, 1) < 0) {
+		pr_err("Out of memory, could not form events string\n");
+		free(basic_events);
+		return -1;
+	}
+	if (basic_events[0] && str) {
+		free(basic_events);
+		free(str);
+		return 1;
+	}
+	free(basic_events);
+	free(str);
+
+	return 0;
+}
+
+// Checks if topdown kernel events support has been detected
+// on this system.
+//
+// Returns 1 if supported,
+//         0 if not supported,
+//         -1 if some unexpected error occurred while checking
+static int topdown_kernel_events_supported(void)
+{
+	int l1_and_l2_available = check_events_available(topdown_metric_L2_attrs);
+
+	if (l1_and_l2_available == 0) {
+		return check_events_available(topdown_attrs);
+	} else {
+		return l1_and_l2_available;
+	}
+}
+
 static int try_non_json_metrics_topdown(void)
 {
 	int err = 0;
@@ -1736,6 +1806,27 @@ static int try_json_metrics_topdown(void)
 	return 0;
 }
 
+enum topdown_mechanism {
+	TOPDOWN_JSON_METRICS,
+	TOPDOWN_KERNEL_EVENTS,
+	TOPDOWN_DETECTION_ERROR,
+};
+
+static enum topdown_mechanism choose_topdown_mechanism(void)
+{
+	int kernel_events_supported = topdown_kernel_events_supported();
+
+	if (kernel_events_supported > 0) {
+		pr_debug("topdown kernel events are supported\n");
+		return TOPDOWN_KERNEL_EVENTS;
+	} else if (kernel_events_supported == 0) {
+		pr_debug("topdown kernel events are unsupported\n");
+		return TOPDOWN_JSON_METRICS;
+	} else {
+		return TOPDOWN_DETECTION_ERROR;
+	}
+}
+
 /*
  * Add default attributes, if there were no attributes specified or
  * if -d/--detailed, -d -d or -d -d -d is used:
@@ -1935,17 +2026,24 @@ static int add_default_attributes(void)
 	}
 
 	if (topdown_run) {
+		int topdown_err = 0;
 		if (!force_metric_only)
 			stat_config.metric_only = true;
 
-		if (topdown_can_use_json_metrics()) {
-			err = try_json_metrics_topdown();
-			if (err)
-				return err;
-		} else {
-			err = try_non_json_metrics_topdown();
-			if (err)
-				return err;
+		switch (choose_topdown_mechanism()) {
+		case TOPDOWN_JSON_METRICS:
+			topdown_err = try_json_metrics_topdown();
+			break;
+		case TOPDOWN_DETECTION_ERROR:
+			return -1;
+		case TOPDOWN_KERNEL_EVENTS:
+		default:
+			topdown_err = try_non_json_metrics_topdown();
+			break;
+		}
+
+		if (topdown_err < 0) {
+			return -1;
 		}
 	}
 
-- 
2.17.1


  parent reply	other threads:[~2022-01-11 15:08 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-10 12:37 [PATCH v2 0/2] perf vendor events: Arm Neoverse N2 Andrew Kilroy
2021-12-10 12:37 ` [PATCH v2 1/2] perf vendor events: For the " Andrew Kilroy
2021-12-10 13:21   ` John Garry
2021-12-14 18:42     ` [RFC PATCH 0/1] topdown with metrics Andrew Kilroy
2021-12-14 18:42       ` [RFC PATCH 1/1] perf arm64: Implement --topdown " Andrew Kilroy
2021-12-14 20:32         ` Ian Rogers
2021-12-15 10:38           ` James Clark
2021-12-15 10:52           ` John Garry
2021-12-15 12:38             ` Andrew Kilroy
2021-12-15 12:53               ` John Garry
2022-01-06 16:33                 ` Andrew Kilroy
2022-01-06 18:24                   ` John Garry
2022-01-11 15:07                     ` [RFC PATCH v2 0/5] topdown " Andrew Kilroy
2022-01-11 15:07                       ` [RFC PATCH v2 1/5] perf stat: Implement --topdown " Andrew Kilroy
2022-01-28 13:44                         ` John Garry
2022-01-11 15:07                       ` [RFC PATCH v2 2/5] perf stat: Topdown kernel events setup function Andrew Kilroy
2022-01-11 15:07                       ` [RFC PATCH v2 3/5] perf stat: Topdown json metrics " Andrew Kilroy
2022-01-11 15:07                       ` Andrew Kilroy [this message]
2022-01-11 15:07                       ` [RFC PATCH v2 5/5] perf stat: Ensure only topdown kernel events used on x86 Andrew Kilroy
2022-01-20  9:26                       ` [RFC PATCH v2 0/5] topdown with metrics John Garry
2022-01-20 16:22                         ` Al Grant
2022-01-27 11:42                         ` Andrew Kilroy
2022-02-08 15:58                           ` Andrew Kilroy
2021-12-20 17:21             ` [RFC PATCH 1/1] perf arm64: Implement --topdown " Andrew Kilroy
2021-12-21 14:03               ` Andi Kleen
2022-01-27 11:11                 ` Andrew Kilroy
2021-12-17 10:19         ` John Garry
2021-12-21 14:31           ` Andrew Kilroy
2022-01-05 16:58           ` Andrew Kilroy
2022-01-28 18:00             ` John Garry
2021-12-10 12:37 ` [PATCH v2 2/2] perf vendor events: Rename arm64 arch std event files Andrew Kilroy
2021-12-10 13:46   ` John Garry
2021-12-10 19:01     ` Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220111150749.13365-5-andrew.kilroy@arm.com \
    --to=andrew.kilroy@arm.com \
    --cc=acme@kernel.org \
    --cc=ak@linux.intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=irogers@google.com \
    --cc=john.garry@huawei.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=namhyung@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).