From nobody Sat Feb 7 09:37:04 2026 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8628C18A6AB for ; Thu, 9 Jan 2025 18:40:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736448054; cv=none; b=jSeKmV0RFq9kkHYFbdcR7ER4IaxzlVhrJxKlXTb2gxLAqoIyUDRooJuIiQu2uFI43mzXl3yLkoOGL6GiFtTN955NpfGMb6wR3YKgYV6cjmv7Pj57/vvW97lHU7PFbOsQaZU9HB5QsiPF1iRqWJMVapt3ZeJ/0tHGGRlNYnCSO0A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736448054; c=relaxed/simple; bh=oe8jug5TfATH6UqI+SDmWJVfhN4/Z1/tSDQisOCdIus=; h=Date:In-Reply-To:Message-Id:Mime-Version:References:Subject:From: To:Content-Type; b=lkYTiFAihJjTbpmjAqVny7WFOLtzqWIk12Hn19h8VzZo8KzAcHAICmOtdSwJdJbMWDOWK1rA9A8E+0nK8au5EOaglb56OPzD7CO/u++dJ30r71/FR/fPEurK0x53Q95g1+03nWKlE/dIWNH4JGNFOKbuNwqQ4kE68Z+ZD+3qmJc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=pFABiYZD; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="pFABiYZD" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e3fea893dc5so5677832276.1 for ; Thu, 09 Jan 2025 10:40:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736448051; x=1737052851; darn=vger.kernel.org; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :from:to:cc:subject:date:message-id:reply-to; bh=rt3FANMhbwUxwhbrCeMm//1cFuRBbV1k6BaJKVQj2ZQ=; b=pFABiYZDdJk918INbKyWTu4V5c+DvS4cigTEab4II54v52IFTIkTccJiLQJSxnL8sD n2N6vMrGr6LNm6lFiTCxOW+d9FB52Cz7c0YULsYdiXms2aQV0NMuqLUYWiMvvndhi8JF UGSllrWPYltaye7foYmU6pwQ8fJW4oUVyz/T0IQv+u3q8hiL/NnLMuB928xslkCTSDez Ev3gHQzrhR+hQ7IuYBeV87zJQEfhXLarxvbNRUUij0m2EsycGracYgDdLdB/kThshzB0 pZ4xECvPL+ExC1C5wWgobIA6EAeIJeaDgNAK/wrHSFgH8yNbE4//e6aC1++hyYN8qS/f a7qQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736448051; x=1737052851; h=to:from:subject:references:mime-version:message-id:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rt3FANMhbwUxwhbrCeMm//1cFuRBbV1k6BaJKVQj2ZQ=; b=t7+PDv7y4PN1v3BoJqUggLd5TZdT4tha/7c81UuMIMMW8AGlSPzCFE8WjK/z9pzzA8 k4GbaCztI3Ffwun8y6bRa0tEHTSpOmUl/yOw6fTacDLKc+/YjUbDJxuhTXMJiEY6QntI HNtRUiLdTARBzX875HrV+R0F9WVaEvfDRO+kY7KwJr8GwvokMpaAA8C3uAi5l0RFXx3b 6snZ64FMh5RQafOjPYS90DLYAoABRAx7YN93lAUILHSr81r2ftNW7QkwWnwk1Lj6RF+q /lGFY+rDEEtLinXA/Hp2mtN6wxVLVkZogBCcsaMkrWASDfOd2cf9XCNq+AO1XOfpYT+2 hAAg== X-Forwarded-Encrypted: i=1; AJvYcCV3pbonKKzh+HsVLp7f1zByT+c7cng1PI8YyEeItAUf7kGw8AwmhzxX1IxjvvroVA6m0YXEuN/b+l2xk1Q=@vger.kernel.org X-Gm-Message-State: AOJu0Yy21I/YjfTUvJId2Ws1/4LPl4noxyKhF0i5QSgphH05153kX4uS /eiVF9mL4Ya8N3YoRF6nzlPBVqTr8+Y3tUcJR5FRuyGBC9FjGubJ2frI154ZLRl+UbV49mtJovp C3JWDRQ== X-Google-Smtp-Source: AGHT+IH9UgRU4CWMXTaZxquWuFp+sw1dty/9wjdv4pye804lLFn0wbP4mpAGQoDdB8NGQA76faifi5HdBglV X-Received: from irogers.svl.corp.google.com ([2620:15c:2c5:11:7ea3:d1e5:495a:9a4f]) (user=irogers job=sendgmr) by 2002:a25:20c2:0:b0:e4d:c8d5:81ef with SMTP id 3f1490d57ef6-e550136cda0mr16901276.0.1736448051590; Thu, 09 Jan 2025 10:40:51 -0800 (PST) Date: Thu, 9 Jan 2025 10:40:42 -0800 In-Reply-To: <20250109184046.193736-1-irogers@google.com> Message-Id: <20250109184046.193736-2-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250109184046.193736-1-irogers@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Subject: [PATCH v3 1/5] perf test: Rename functions and variables for better clarity From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Mark Rutland , Alexander Shishkin , Jiri Olsa , Ian Rogers , Adrian Hunter , Kan Liang , James Clark , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The relationship between subtests and test cases is somewhat confusing, so let's do away with the notion of sub-tests and switch to just working with some number of test cases. Add a test_suite__for_each_test_case as in many cases, except the special one test case situation, the iteration can just be on all test cases. Switch variable names to be more intention revealing of what their value is. This work was motivated by discussion with Kan where it was noted the code is becoming overly indented: https://lore.kernel.org/lkml/20241109160219.49976-1-irogers@google.com/ Unifying more of the sub-test/no-sub-tests avoids one level of indentation in a number of places. Signed-off-by: Ian Rogers --- tools/perf/tests/builtin-test.c | 201 +++++++++++++++----------------- 1 file changed, 93 insertions(+), 108 deletions(-) diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-tes= t.c index a5b9ccd0033a..c3e082efb7f4 100644 --- a/tools/perf/tests/builtin-test.c +++ b/tools/perf/tests/builtin-test.c @@ -149,58 +149,51 @@ static struct test_workload *workloads[] =3D { #define workloads__for_each(workload) \ for (unsigned i =3D 0; i < ARRAY_SIZE(workloads) && ({ workload =3D workl= oads[i]; 1; }); i++) =20 -static int num_subtests(const struct test_suite *t) +#define test_suite__for_each_test_case(suite, idx) \ + for (idx =3D 0; (suite)->test_cases && (suite)->test_cases[idx].name !=3D= NULL; idx++) + +static int test_suite__num_test_cases(const struct test_suite *t) { int num; =20 - if (!t->test_cases) - return 0; - - num =3D 0; - while (t->test_cases[num].name) - num++; + test_suite__for_each_test_case(t, num); =20 return num; } =20 -static bool has_subtests(const struct test_suite *t) -{ - return num_subtests(t) > 1; -} - -static const char *skip_reason(const struct test_suite *t, int subtest) +static const char *skip_reason(const struct test_suite *t, int test_case) { if (!t->test_cases) return NULL; =20 - return t->test_cases[subtest >=3D 0 ? subtest : 0].skip_reason; + return t->test_cases[test_case >=3D 0 ? test_case : 0].skip_reason; } =20 -static const char *test_description(const struct test_suite *t, int subtes= t) +static const char *test_description(const struct test_suite *t, int test_c= ase) { - if (t->test_cases && subtest >=3D 0) - return t->test_cases[subtest].desc; + if (t->test_cases && test_case >=3D 0) + return t->test_cases[test_case].desc; =20 return t->desc; } =20 -static test_fnptr test_function(const struct test_suite *t, int subtest) +static test_fnptr test_function(const struct test_suite *t, int test_case) { - if (subtest <=3D 0) + if (test_case <=3D 0) return t->test_cases[0].run_case; =20 - return t->test_cases[subtest].run_case; + return t->test_cases[test_case].run_case; } =20 -static bool test_exclusive(const struct test_suite *t, int subtest) +static bool test_exclusive(const struct test_suite *t, int test_case) { - if (subtest <=3D 0) + if (test_case <=3D 0) return t->test_cases[0].exclusive; =20 - return t->test_cases[subtest].exclusive; + return t->test_cases[test_case].exclusive; } =20 -static bool perf_test__matches(const char *desc, int curr, int argc, const= char *argv[]) +static bool perf_test__matches(const char *desc, int suite_num, int argc, = const char *argv[]) { int i; =20 @@ -212,7 +205,7 @@ static bool perf_test__matches(const char *desc, int cu= rr, int argc, const char long nr =3D strtoul(argv[i], &end, 10); =20 if (*end =3D=3D '\0') { - if (nr =3D=3D curr + 1) + if (nr =3D=3D suite_num + 1) return true; continue; } @@ -227,8 +220,8 @@ static bool perf_test__matches(const char *desc, int cu= rr, int argc, const char struct child_test { struct child_process process; struct test_suite *test; - int test_num; - int subtest; + int suite_num; + int test_case_num; }; =20 static jmp_buf run_test_jmp_buf; @@ -258,7 +251,7 @@ static int run_test_child(struct child_process *process) =20 pr_debug("--- start ---\n"); pr_debug("test child forked, pid %d\n", getpid()); - err =3D test_function(child->test, child->subtest)(child->test, child->su= btest); + err =3D test_function(child->test, child->test_case_num)(child->test, chi= ld->test_case_num); pr_debug("---- end(%d) ----\n", err); =20 err_out: @@ -270,15 +263,16 @@ static int run_test_child(struct child_process *proce= ss) =20 #define TEST_RUNNING -3 =20 -static int print_test_result(struct test_suite *t, int i, int subtest, int= result, int width, - int running) +static int print_test_result(struct test_suite *t, int curr_suite, int cur= r_test_case, + int result, int width, int running) { - if (has_subtests(t)) { + if (test_suite__num_test_cases(t) > 1) { int subw =3D width > 2 ? width - 2 : width; =20 - pr_info("%3d.%1d: %-*s:", i + 1, subtest + 1, subw, test_description(t, = subtest)); + pr_info("%3d.%1d: %-*s:", curr_suite + 1, curr_test_case + 1, subw, + test_description(t, curr_test_case)); } else - pr_info("%3d: %-*s:", i + 1, width, test_description(t, subtest)); + pr_info("%3d: %-*s:", curr_suite + 1, width, test_description(t, curr_te= st_case)); =20 switch (result) { case TEST_RUNNING: @@ -288,7 +282,7 @@ static int print_test_result(struct test_suite *t, int = i, int subtest, int resul pr_info(" Ok\n"); break; case TEST_SKIP: { - const char *reason =3D skip_reason(t, subtest); + const char *reason =3D skip_reason(t, curr_test_case); =20 if (reason) color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (%s)\n", reason); @@ -310,7 +304,7 @@ static void finish_test(struct child_test **child_tests= , int running_test, int c { struct child_test *child_test =3D child_tests[running_test]; struct test_suite *t; - int i, subi, err; + int curr_suite, curr_test_case, err; bool err_done =3D false; struct strbuf err_output =3D STRBUF_INIT; int last_running =3D -1; @@ -321,15 +315,15 @@ static void finish_test(struct child_test **child_tes= ts, int running_test, int c return; } t =3D child_test->test; - i =3D child_test->test_num; - subi =3D child_test->subtest; + curr_suite =3D child_test->suite_num; + curr_test_case =3D child_test->test_case_num; err =3D child_test->process.err; /* * For test suites with subtests, display the suite name ahead of the * sub test names. */ - if (has_subtests(t) && subi =3D=3D 0) - pr_info("%3d: %-*s:\n", i + 1, width, test_description(t, -1)); + if (test_suite__num_test_cases(t) > 1 && curr_test_case =3D=3D 0) + pr_info("%3d: %-*s:\n", curr_suite + 1, width, test_description(t, -1)); =20 /* * Busy loop reading from the child's stdout/stderr that are set to be @@ -338,10 +332,11 @@ static void finish_test(struct child_test **child_tes= ts, int running_test, int c if (err > 0) fcntl(err, F_SETFL, O_NONBLOCK); if (verbose > 1) { - if (has_subtests(t)) - pr_info("%3d.%1d: %s:\n", i + 1, subi + 1, test_description(t, subi)); + if (test_suite__num_test_cases(t) > 1) + pr_info("%3d.%1d: %s:\n", curr_suite + 1, curr_test_case + 1, + test_description(t, curr_test_case)); else - pr_info("%3d: %s:\n", i + 1, test_description(t, -1)); + pr_info("%3d: %s:\n", curr_suite + 1, test_description(t, -1)); } while (!err_done) { struct pollfd pfds[1] =3D { @@ -366,7 +361,8 @@ static void finish_test(struct child_test **child_tests= , int running_test, int c */ fprintf(debug_file(), PERF_COLOR_DELETE_LINE); } - print_test_result(t, i, subi, TEST_RUNNING, width, running); + print_test_result(t, curr_suite, curr_test_case, TEST_RUNNING, + width, running); last_running =3D running; } } @@ -404,14 +400,14 @@ static void finish_test(struct child_test **child_tes= ts, int running_test, int c fprintf(stderr, "%s", err_output.buf); =20 strbuf_release(&err_output); - print_test_result(t, i, subi, ret, width, /*running=3D*/0); + print_test_result(t, curr_suite, curr_test_case, ret, width, /*running=3D= */0); if (err > 0) close(err); zfree(&child_tests[running_test]); } =20 -static int start_test(struct test_suite *test, int i, int subi, struct chi= ld_test **child, - int width, int pass) +static int start_test(struct test_suite *test, int curr_suite, int curr_te= st_case, + struct child_test **child, int width, int pass) { int err; =20 @@ -419,17 +415,18 @@ static int start_test(struct test_suite *test, int i,= int subi, struct child_tes if (dont_fork) { if (pass =3D=3D 1) { pr_debug("--- start ---\n"); - err =3D test_function(test, subi)(test, subi); + err =3D test_function(test, curr_suite)(test, curr_test_case); pr_debug("---- end ----\n"); - print_test_result(test, i, subi, err, width, /*running=3D*/0); + print_test_result(test, curr_suite, curr_test_case, err, width, + /*running=3D*/0); } return 0; } - if (pass =3D=3D 1 && !sequential && test_exclusive(test, subi)) { + if (pass =3D=3D 1 && !sequential && test_exclusive(test, curr_test_case))= { /* When parallel, skip exclusive tests on the first pass. */ return 0; } - if (pass !=3D 1 && (sequential || !test_exclusive(test, subi))) { + if (pass !=3D 1 && (sequential || !test_exclusive(test, curr_test_case)))= { /* Sequential and non-exclusive tests were run on the first pass. */ return 0; } @@ -438,8 +435,8 @@ static int start_test(struct test_suite *test, int i, i= nt subi, struct child_tes return -ENOMEM; =20 (*child)->test =3D test; - (*child)->test_num =3D i; - (*child)->subtest =3D subi; + (*child)->suite_num =3D curr_suite; + (*child)->test_case_num =3D curr_test_case; (*child)->process.pid =3D -1; (*child)->process.no_stdin =3D 1; if (verbose <=3D 0) { @@ -479,19 +476,15 @@ static int __cmd_test(struct test_suite **suites, int= argc, const char *argv[], int err =3D 0; =20 for (struct test_suite **t =3D suites; *t; t++) { - int len =3D strlen(test_description(*t, -1)); + int i, len =3D strlen(test_description(*t, -1)); =20 if (width < len) width =3D len; =20 - if (has_subtests(*t)) { - for (int subi =3D 0, subn =3D num_subtests(*t); subi < subn; subi++) { - len =3D strlen(test_description(*t, subi)); - if (width < len) - width =3D len; - num_tests++; - } - } else { + test_suite__for_each_test_case(*t, i) { + len =3D strlen(test_description(*t, i)); + if (width < len) + width =3D len; num_tests++; } } @@ -510,7 +503,7 @@ static int __cmd_test(struct test_suite **suites, int a= rgc, const char *argv[], continue; =20 pr_debug3("Killing %d pid %d\n", - child_test->test_num + 1, + child_test->suite_num + 1, child_test->process.pid); kill(child_test->process.pid, err); } @@ -526,47 +519,43 @@ static int __cmd_test(struct test_suite **suites, int= argc, const char *argv[], */ for (int pass =3D 1; pass <=3D 2; pass++) { int child_test_num =3D 0; - int i =3D 0; + int curr_suite =3D 0; =20 - for (struct test_suite **t =3D suites; *t; t++) { - int curr =3D i++; + for (struct test_suite **t =3D suites; *t; t++, curr_suite++) { + int curr_test_case; =20 - if (!perf_test__matches(test_description(*t, -1), curr, argc, argv)) { + if (!perf_test__matches(test_description(*t, -1), curr_suite, argc, arg= v)) { /* * Test suite shouldn't be run based on - * description. See if subtest should. + * description. See if any test case should. */ bool skip =3D true; =20 - for (int subi =3D 0, subn =3D num_subtests(*t); subi < subn; subi++) { - if (perf_test__matches(test_description(*t, subi), - curr, argc, argv)) + test_suite__for_each_test_case(*t, curr_test_case) { + if (perf_test__matches(test_description(*t, curr_test_case), + curr_suite, argc, argv)) { skip =3D false; + break; + } } - if (skip) continue; } =20 - if (intlist__find(skiplist, i)) { - pr_info("%3d: %-*s:", curr + 1, width, test_description(*t, -1)); + if (intlist__find(skiplist, curr_suite + 1)) { + pr_info("%3d: %-*s:", curr_suite + 1, width, + test_description(*t, -1)); color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n"); continue; } =20 - if (!has_subtests(*t)) { - err =3D start_test(*t, curr, -1, &child_tests[child_test_num++], - width, pass); - if (err) - goto err_out; - continue; - } - for (int subi =3D 0, subn =3D num_subtests(*t); subi < subn; subi++) { - if (!perf_test__matches(test_description(*t, subi), - curr, argc, argv)) + test_suite__for_each_test_case(*t, curr_test_case) { + if (!perf_test__matches(test_description(*t, curr_test_case), + curr_suite, argc, argv)) continue; =20 - err =3D start_test(*t, curr, subi, &child_tests[child_test_num++], + err =3D start_test(*t, curr_suite, curr_test_case, + &child_tests[child_test_num++], width, pass); if (err) goto err_out; @@ -592,23 +581,22 @@ static int __cmd_test(struct test_suite **suites, int= argc, const char *argv[], =20 static int perf_test__list(struct test_suite **suites, int argc, const cha= r **argv) { - int i =3D 0; + int curr_suite =3D 0; =20 - for (struct test_suite **t =3D suites; *t; t++) { - int curr =3D i++; + for (struct test_suite **t =3D suites; *t; t++, curr_suite++) { + int curr_test_case; =20 - if (!perf_test__matches(test_description(*t, -1), curr, argc, argv)) + if (!perf_test__matches(test_description(*t, -1), curr_suite, argc, argv= )) continue; =20 - pr_info("%3d: %s\n", i, test_description(*t, -1)); + pr_info("%3d: %s\n", curr_suite + 1, test_description(*t, -1)); =20 - if (has_subtests(*t)) { - int subn =3D num_subtests(*t); - int subi; + if (test_suite__num_test_cases(*t) <=3D 1) + continue; =20 - for (subi =3D 0; subi < subn; subi++) - pr_info("%3d:%1d: %s\n", i, subi + 1, - test_description(*t, subi)); + test_suite__for_each_test_case(*t, curr_test_case) { + pr_info("%3d:%1d: %s\n", curr_suite + 1, curr_test_case + 1, + test_description(*t, curr_test_case)); } } return 0; @@ -665,27 +653,24 @@ static struct test_suite **build_suites(void) if (suites[2] =3D=3D NULL) suites[2] =3D create_script_test_suites(); =20 -#define for_each_test(t) \ +#define for_each_suite(suite) \ for (size_t i =3D 0, j =3D 0; i < ARRAY_SIZE(suites); i++, j =3D 0) \ - while ((t =3D suites[i][j++]) !=3D NULL) + while ((suite =3D suites[i][j++]) !=3D NULL) =20 - for_each_test(t) + for_each_suite(t) num_suites++; =20 result =3D calloc(num_suites + 1, sizeof(struct test_suite *)); =20 for (int pass =3D 1; pass <=3D 2; pass++) { - for_each_test(t) { + for_each_suite(t) { bool exclusive =3D false; + int curr_test_case; =20 - if (!has_subtests(t)) { - exclusive =3D test_exclusive(t, -1); - } else { - for (int subi =3D 0, subn =3D num_subtests(t); subi < subn; subi++) { - if (test_exclusive(t, subi)) { - exclusive =3D true; - break; - } + test_suite__for_each_test_case(t, curr_test_case) { + if (test_exclusive(t, curr_test_case)) { + exclusive =3D true; + break; } } if ((!exclusive && pass =3D=3D 1) || (exclusive && pass =3D=3D 2)) @@ -693,7 +678,7 @@ static struct test_suite **build_suites(void) } } return result; -#undef for_each_test +#undef for_each_suite } =20 int cmd_test(int argc, const char **argv) --=20 2.47.1.613.gc27f4b7a9f-goog