All of lore.kernel.org
 help / color / mirror / Atom feed
From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@redhat.com>, Namhyung Kim <namhyung@kernel.org>,
	Jin Yao <yao.jin@linux.intel.com>,
	John Garry <john.garry@huawei.com>,
	"Paul A . Clarke" <pc@us.ibm.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Brendan Higgins <brendanhiggins@google.com>,
	Daniel Latypov <dlatypov@google.com>,
	David Gow <davidgow@google.com>,
	Sohaib Mohamed <sohaib.amhmd@gmail.com>
Cc: eranian@google.com, Ian Rogers <irogers@google.com>
Subject: [PATCH v3 06/22] perf test: Add helper functions for abstraction.
Date: Wed,  3 Nov 2021 23:41:52 -0700	[thread overview]
Message-ID: <20211104064208.3156807-7-irogers@google.com> (raw)
In-Reply-To: <20211104064208.3156807-1-irogers@google.com>

Abstract certain test features so that they can be refactored in later
changes. No functional change.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/tests/builtin-test.c | 94 +++++++++++++++++++++++----------
 tools/perf/tests/tests.h        |  6 ++-
 2 files changed, 71 insertions(+), 29 deletions(-)

diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 7d52e2eb8147..96eb486ffbc9 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -115,6 +115,45 @@ static struct test_suite **tests[] = {
 	arch_tests,
 };
 
+static int num_subtests(const struct test_suite *t)
+{
+	if (t->subtest.get_nr)
+		return t->subtest.get_nr();
+
+	return 0;
+}
+
+static bool has_subtests(const struct test_suite *t)
+{
+	return t->subtest.get_nr || num_subtests(t) > 1;
+}
+
+static const char *skip_reason(const struct test_suite *t, int subtest)
+{
+	if (t->subtest.skip_reason)
+		return t->subtest.skip_reason(subtest);
+
+	return NULL;
+}
+
+static const char *test_description(const struct test_suite *t, int subtest)
+{
+	if (subtest < 0 || !t->subtest.get_desc)
+		return t->desc;
+
+	return t->subtest.get_desc(subtest);
+}
+
+static bool is_supported(const struct test_suite *t)
+{
+	return !t->is_supported || t->is_supported();
+}
+
+static test_fnptr test_function(const struct test_suite *t, int subtest __maybe_unused)
+{
+	return t->func;
+}
+
 static bool perf_test__matches(const char *desc, int curr, int argc, const char *argv[])
 {
 	int i;
@@ -171,7 +210,7 @@ static int run_test(struct test_suite *test, int subtest)
 			}
 		}
 
-		err = test->func(test, subtest);
+		err = test_function(test, subtest)(test, subtest);
 		if (!dont_fork)
 			exit(err);
 	}
@@ -208,7 +247,7 @@ static int test_and_print(struct test_suite *t, bool force_skip, int subtest)
 		err = TEST_SKIP;
 	}
 
-	if (!t->subtest.get_nr)
+	if (!has_subtests(t))
 		pr_debug("%s:", t->desc);
 	else
 		pr_debug("%s subtest %d:", t->desc, subtest + 1);
@@ -218,11 +257,10 @@ static int test_and_print(struct test_suite *t, bool force_skip, int subtest)
 		pr_info(" Ok\n");
 		break;
 	case TEST_SKIP: {
-		const char *skip_reason = NULL;
-		if (t->subtest.skip_reason)
-			skip_reason = t->subtest.skip_reason(subtest);
-		if (skip_reason)
-			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", skip_reason);
+		const char *reason = skip_reason(t, subtest);
+
+		if (reason)
+			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason);
 		else
 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip\n");
 	}
@@ -397,7 +435,7 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
 	int width = shell_tests__max_desc_width();
 
 	for_each_test(j, k, t) {
-		int len = strlen(t->desc);
+		int len = strlen(test_description(t, -1));
 
 		if (width < len)
 			width = len;
@@ -407,17 +445,15 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
 		int curr = i++, err;
 		int subi;
 
-		if (!perf_test__matches(t->desc, curr, argc, argv)) {
+		if (!perf_test__matches(test_description(t, -1), curr, argc, argv)) {
 			bool skip = true;
 			int subn;
 
-			if (!t->subtest.get_nr)
-				continue;
-
-			subn = t->subtest.get_nr();
+			subn = num_subtests(t);
 
 			for (subi = 0; subi < subn; subi++) {
-				if (perf_test__matches(t->subtest.get_desc(subi), curr, argc, argv))
+				if (perf_test__matches(test_description(t, subi),
+							curr, argc, argv))
 					skip = false;
 			}
 
@@ -425,22 +461,23 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
 				continue;
 		}
 
-		if (t->is_supported && !t->is_supported()) {
-			pr_debug("%2d: %-*s: Disabled\n", i, width, t->desc);
+		if (!is_supported(t)) {
+			pr_debug("%2d: %-*s: Disabled\n", i, width,
+				 test_description(t, -1));
 			continue;
 		}
 
-		pr_info("%2d: %-*s:", i, width, t->desc);
+		pr_info("%2d: %-*s:", i, width, test_description(t, -1));
 
 		if (intlist__find(skiplist, i)) {
 			color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
 			continue;
 		}
 
-		if (!t->subtest.get_nr) {
+		if (!has_subtests(t)) {
 			test_and_print(t, false, -1);
 		} else {
-			int subn = t->subtest.get_nr();
+			int subn = num_subtests(t);
 			/*
 			 * minus 2 to align with normal testcases.
 			 * For subtest we print additional '.x' in number.
@@ -460,18 +497,19 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
 			pr_info("\n");
 
 			for (subi = 0; subi < subn; subi++) {
-				int len = strlen(t->subtest.get_desc(subi));
+				int len = strlen(test_description(t, subi));
 
 				if (subw < len)
 					subw = len;
 			}
 
 			for (subi = 0; subi < subn; subi++) {
-				if (!perf_test__matches(t->subtest.get_desc(subi), curr, argc, argv))
+				if (!perf_test__matches(test_description(t, subi),
+							curr, argc, argv))
 					continue;
 
 				pr_info("%2d.%1d: %-*s:", i, subi + 1, subw,
-					t->subtest.get_desc(subi));
+					test_description(t, subi));
 				err = test_and_print(t, skip, subi);
 				if (err != TEST_OK && t->subtest.skip_if_fail)
 					skip = true;
@@ -526,19 +564,19 @@ static int perf_test__list(int argc, const char **argv)
 	for_each_test(j, k, t) {
 		int curr = i++;
 
-		if (!perf_test__matches(t->desc, curr, argc, argv) ||
-		    (t->is_supported && !t->is_supported()))
+		if (!perf_test__matches(test_description(t, -1), curr, argc, argv) ||
+		    !is_supported(t))
 			continue;
 
-		pr_info("%2d: %s\n", i, t->desc);
+		pr_info("%2d: %s\n", i, test_description(t, -1));
 
-		if (t->subtest.get_nr) {
-			int subn = t->subtest.get_nr();
+		if (has_subtests(t)) {
+			int subn = num_subtests(t);
 			int subi;
 
 			for (subi = 0; subi < subn; subi++)
 				pr_info("%2d:%1d: %s\n", i, subi + 1,
-					t->subtest.get_desc(subi));
+					test_description(t, subi));
 		}
 	}
 
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index c95b0de9f822..5139e24973cc 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -27,9 +27,13 @@ enum {
 	TEST_SKIP = -2,
 };
 
+struct test_suite;
+
+typedef int (*test_fnptr)(struct test_suite *, int);
+
 struct test_suite {
 	const char *desc;
-	int (*func)(struct test_suite *test, int subtest);
+	test_fnptr func;
 	struct {
 		bool skip_if_fail;
 		int (*get_nr)(void);
-- 
2.33.1.1089.g2158813163f-goog


  parent reply	other threads:[~2021-11-04  6:42 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-04  6:41 [PATCH v3 00/22] Start aligning perf test with kunit style Ian Rogers
2021-11-04  6:41 ` [PATCH v3 01/22] perf test: Use macro for "suite" declarations Ian Rogers
2021-11-04  6:41 ` [PATCH v3 02/22] perf test: Use macro for "suite" definitions Ian Rogers
2021-11-04  6:41 ` [PATCH v3 03/22] perf test: Make each test/suite its own struct Ian Rogers
2021-11-13 13:06   ` Arnaldo Carvalho de Melo
2021-11-13 13:29     ` Arnaldo Carvalho de Melo
2021-11-04  6:41 ` [PATCH v3 04/22] perf test: Move each test suite struct to its test Ian Rogers
2021-11-04  6:41 ` [PATCH v3 05/22] perf test: Rename struct test to test_suite Ian Rogers
2021-11-04  6:41 ` Ian Rogers [this message]
2021-11-04  6:41 ` [PATCH v3 07/22] perf test: Add test case struct Ian Rogers
2021-11-04  6:41 ` [PATCH v3 08/22] perf test: Add skip reason to test case Ian Rogers
2021-11-04  6:41 ` [PATCH v3 09/22] perf test: Convert pfm tests to use test cases Ian Rogers
2021-11-04  6:41 ` [PATCH v3 10/22] perf test: Convert pmu event tests to " Ian Rogers
2021-11-04  6:41 ` [PATCH v3 11/22] perf test: Convert watch point " Ian Rogers
2021-11-04  6:41 ` [PATCH v3 12/22] perf test: Convert clang " Ian Rogers
2021-11-04  6:41 ` [PATCH v3 13/22] perf test: Convert bpf " Ian Rogers
2021-11-04  6:42 ` [PATCH v3 14/22] perf test: Convert llvm " Ian Rogers
2021-11-04  6:42 ` [PATCH v3 15/22] perf test: Remove now unused subtest helpers Ian Rogers
2021-11-04  6:42 ` [PATCH v3 16/22] perf test: bp tests use test case Ian Rogers
2021-11-04  6:42 ` [PATCH v3 17/22] perf test: Convert time to tsc test to " Ian Rogers
2021-11-04  6:42 ` [PATCH v3 18/22] perf test: Remove non test case style support Ian Rogers
2021-11-04  6:42 ` [PATCH v3 19/22] perf test: BP tests, remove is_supported use Ian Rogers
2021-11-04  6:42 ` [PATCH v3 20/22] perf test: TSC test, " Ian Rogers
2021-11-04  6:42 ` [PATCH v3 21/22] perf test: Remove is_supported function Ian Rogers
2021-11-04  6:42 ` [PATCH v3 22/22] perf test: Remove skip_if_fail Ian Rogers
2021-11-04 13:51   ` Sohaib Mohamed
2021-11-09 14:32     ` Arnaldo Carvalho de Melo
2021-11-10 15:44       ` Sohaib Mohamed
2021-11-09 11:08 ` [PATCH v3 00/22] Start aligning perf test with kunit style Jiri Olsa
2021-11-10 12:20 ` Arnaldo Carvalho de Melo
2021-11-10 16:12 ` Sohaib Mohamed

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=20211104064208.3156807-7-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=brendanhiggins@google.com \
    --cc=davidgow@google.com \
    --cc=dlatypov@google.com \
    --cc=eranian@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=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=pc@us.ibm.com \
    --cc=peterz@infradead.org \
    --cc=sohaib.amhmd@gmail.com \
    --cc=yao.jin@linux.intel.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.