Commit 964461ee authored by Ian Rogers's avatar Ian Rogers Committed by Namhyung Kim

perf tests: Run time generate shell test suites

Rather than special shell test logic, do a single pass to create an
array of test suites. Hold the shell test file name in the test suite
priv field. This makes the special shell test logic in builtin-test.c
redundant so remove it.
Signed-off-by: default avatarIan Rogers <irogers@google.com>
Cc: James Clark <james.clark@arm.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: llvm@lists.linux.dev
Signed-off-by: default avatarNamhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240221034155.1500118-8-irogers@google.com
parent f3295f5b
...@@ -130,6 +130,7 @@ static struct test_suite *generic_tests[] = { ...@@ -130,6 +130,7 @@ static struct test_suite *generic_tests[] = {
static struct test_suite **tests[] = { static struct test_suite **tests[] = {
generic_tests, generic_tests,
arch_tests, arch_tests,
NULL, /* shell tests created at runtime. */
}; };
static struct test_workload *workloads[] = { static struct test_workload *workloads[] = {
...@@ -299,73 +300,12 @@ static int test_and_print(struct test_suite *t, int subtest) ...@@ -299,73 +300,12 @@ static int test_and_print(struct test_suite *t, int subtest)
return err; return err;
} }
struct shell_test {
const char *file;
};
static int shell_test__run(struct test_suite *test, int subdir __maybe_unused)
{
int err;
struct shell_test *st = test->priv;
char *cmd = NULL;
if (asprintf(&cmd, "%s%s", st->file, verbose ? " -v" : "") < 0)
return TEST_FAIL;
err = system(cmd);
free(cmd);
if (!err)
return TEST_OK;
return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
}
static int run_shell_tests(int argc, const char *argv[], int i, int width,
struct intlist *skiplist)
{
struct shell_test st;
const struct script_file *files, *file;
files = list_script_files();
if (!files)
return 0;
for (file = files; file->file; file++) {
int curr = i++;
struct test_case test_cases[] = {
{
.desc = file->desc,
.run_case = shell_test__run,
},
{ .name = NULL, }
};
struct test_suite test_suite = {
.desc = test_cases[0].desc,
.test_cases = test_cases,
.priv = &st,
};
st.file = file->file;
if (test_suite.desc == NULL ||
!perf_test__matches(test_suite.desc, curr, argc, argv))
continue;
pr_info("%3d: %-*s:", i, width, test_suite.desc);
if (intlist__find(skiplist, i)) {
color_fprintf(stderr, PERF_COLOR_YELLOW, " Skip (user override)\n");
continue;
}
test_and_print(&test_suite, 0);
}
return 0;
}
static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
{ {
struct test_suite *t; struct test_suite *t;
unsigned int j, k; unsigned int j, k;
int i = 0; int i = 0;
int width = list_script_max_width(); int width = 0;
for_each_test(j, k, t) { for_each_test(j, k, t) {
int len = strlen(test_description(t, -1)); int len = strlen(test_description(t, -1));
...@@ -440,28 +380,6 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist) ...@@ -440,28 +380,6 @@ static int __cmd_test(int argc, const char *argv[], struct intlist *skiplist)
} }
} }
} }
return run_shell_tests(argc, argv, i, width, skiplist);
}
static int perf_test__list_shell(int argc, const char **argv, int i)
{
const struct script_file *files, *file;
files = list_script_files();
if (!files)
return 0;
for (file = files; file->file; file++) {
int curr = i++;
struct test_suite t = {
.desc = file->desc
};
if (!perf_test__matches(t.desc, curr, argc, argv))
continue;
pr_info("%3d: %s\n", i, t.desc);
}
return 0; return 0;
} }
...@@ -488,9 +406,6 @@ static int perf_test__list(int argc, const char **argv) ...@@ -488,9 +406,6 @@ static int perf_test__list(int argc, const char **argv)
test_description(t, subi)); test_description(t, subi));
} }
} }
perf_test__list_shell(argc, argv, i);
return 0; return 0;
} }
...@@ -550,6 +465,7 @@ int cmd_test(int argc, const char **argv) ...@@ -550,6 +465,7 @@ int cmd_test(int argc, const char **argv)
/* Unbuffered output */ /* Unbuffered output */
setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stdout, NULL, _IONBF, 0);
tests[2] = create_script_test_suites();
argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0); argc = parse_options_subcommand(argc, argv, test_options, test_subcommands, test_usage, 0);
if (argc >= 1 && !strcmp(argv[0], "list")) if (argc >= 1 && !strcmp(argv[0], "list"))
return perf_test__list(argc - 1, argv + 1); return perf_test__list(argc - 1, argv + 1);
......
...@@ -27,16 +27,6 @@ ...@@ -27,16 +27,6 @@
#include "util/rlimit.h" #include "util/rlimit.h"
#include "util/util.h" #include "util/util.h"
/*
* As this is a singleton built once for the run of the process, there is
* no value in trying to free it and just let it stay around until process
* exits when it's cleaned up.
*/
static size_t files_num = 0;
static struct script_file *files = NULL;
static int files_max_width = 0;
static int shell_tests__dir_fd(void) static int shell_tests__dir_fd(void)
{ {
char path[PATH_MAX], *exec_path; char path[PATH_MAX], *exec_path;
...@@ -132,12 +122,30 @@ static char *strdup_check(const char *str) ...@@ -132,12 +122,30 @@ static char *strdup_check(const char *str)
return newstr; return newstr;
} }
static void append_script(int dir_fd, const char *name, char *desc) static int shell_test__run(struct test_suite *test, int subtest __maybe_unused)
{
const char *file = test->priv;
int err;
char *cmd = NULL;
if (asprintf(&cmd, "%s%s", file, verbose ? " -v" : "") < 0)
return TEST_FAIL;
err = system(cmd);
free(cmd);
if (!err)
return TEST_OK;
return WEXITSTATUS(err) == 2 ? TEST_SKIP : TEST_FAIL;
}
static void append_script(int dir_fd, const char *name, char *desc,
struct test_suite ***result,
size_t *result_sz)
{ {
char filename[PATH_MAX], link[128]; char filename[PATH_MAX], link[128];
struct script_file *files_tmp; struct test_suite *test_suite, **result_tmp;
size_t files_num_tmp, len; struct test_case *tests;
int width; size_t len;
snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd); snprintf(link, sizeof(link), "/proc/%d/fd/%d", getpid(), dir_fd);
len = readlink(link, filename, sizeof(filename)); len = readlink(link, filename, sizeof(filename));
...@@ -147,33 +155,43 @@ static void append_script(int dir_fd, const char *name, char *desc) ...@@ -147,33 +155,43 @@ static void append_script(int dir_fd, const char *name, char *desc)
} }
filename[len++] = '/'; filename[len++] = '/';
strcpy(&filename[len], name); strcpy(&filename[len], name);
files_num_tmp = files_num + 1;
if (files_num_tmp >= SIZE_MAX) { tests = calloc(2, sizeof(*tests));
pr_err("Too many script files\n"); if (!tests) {
abort(); pr_err("Out of memory while building script test suite list\n");
return;
} }
tests[0].name = strdup_check(name);
tests[0].desc = strdup_check(desc);
tests[0].run_case = shell_test__run;
test_suite = zalloc(sizeof(*test_suite));
if (!test_suite) {
pr_err("Out of memory while building script test suite list\n");
free(tests);
return;
}
test_suite->desc = desc;
test_suite->test_cases = tests;
test_suite->priv = strdup_check(filename);
/* Realloc is good enough, though we could realloc by chunks, not that /* Realloc is good enough, though we could realloc by chunks, not that
* anyone will ever measure performance here */ * anyone will ever measure performance here */
files_tmp = realloc(files, result_tmp = realloc(*result, (*result_sz + 1) * sizeof(*result_tmp));
(files_num_tmp + 1) * sizeof(struct script_file)); if (result_tmp == NULL) {
if (files_tmp == NULL) { pr_err("Out of memory while building script test suite list\n");
pr_err("Out of memory while building test list\n"); free(tests);
abort(); free(test_suite);
return;
} }
/* Add file to end and NULL terminate the struct array */ /* Add file to end and NULL terminate the struct array */
files = files_tmp; *result = result_tmp;
files_num = files_num_tmp; (*result)[*result_sz] = test_suite;
files[files_num - 1].file = strdup_check(filename); (*result_sz)++;
files[files_num - 1].desc = desc;
files[files_num].file = NULL;
files[files_num].desc = NULL;
width = strlen(desc); /* Track max width of desc */
if (width > files_max_width)
files_max_width = width;
} }
static void append_scripts_in_dir(int dir_fd) static void append_scripts_in_dir(int dir_fd,
struct test_suite ***result,
size_t *result_sz)
{ {
struct dirent **entlist; struct dirent **entlist;
struct dirent *ent; struct dirent *ent;
...@@ -192,7 +210,7 @@ static void append_scripts_in_dir(int dir_fd) ...@@ -192,7 +210,7 @@ static void append_scripts_in_dir(int dir_fd)
char *desc = shell_test__description(dir_fd, ent->d_name); char *desc = shell_test__description(dir_fd, ent->d_name);
if (desc) /* It has a desc line - valid script */ if (desc) /* It has a desc line - valid script */
append_script(dir_fd, ent->d_name, desc); append_script(dir_fd, ent->d_name, desc, result, result_sz);
continue; continue;
} }
if (ent->d_type != DT_DIR) { if (ent->d_type != DT_DIR) {
...@@ -205,32 +223,35 @@ static void append_scripts_in_dir(int dir_fd) ...@@ -205,32 +223,35 @@ static void append_scripts_in_dir(int dir_fd)
continue; continue;
} }
fd = openat(dir_fd, ent->d_name, O_PATH); fd = openat(dir_fd, ent->d_name, O_PATH);
append_scripts_in_dir(fd); append_scripts_in_dir(fd, result, result_sz);
} }
for (i = 0; i < n_dirs; i++) /* Clean up */ for (i = 0; i < n_dirs; i++) /* Clean up */
zfree(&entlist[i]); zfree(&entlist[i]);
free(entlist); free(entlist);
} }
const struct script_file *list_script_files(void) struct test_suite **create_script_test_suites(void)
{ {
int dir_fd; struct test_suite **result = NULL, **result_tmp;
size_t result_sz = 0;
int dir_fd = shell_tests__dir_fd(); /* Walk dir */
if (files) /*
return files; /* Singleton - we already know our list */ * Append scripts if fd is good, otherwise return a NULL terminated zero
* length array.
dir_fd = shell_tests__dir_fd(); /* Walk dir */ */
if (dir_fd < 0) if (dir_fd >= 0)
return NULL; append_scripts_in_dir(dir_fd, &result, &result_sz);
append_scripts_in_dir(dir_fd); result_tmp = realloc(result, (result_sz + 1) * sizeof(*result_tmp));
if (result_tmp == NULL) {
pr_err("Out of memory while building script test suite list\n");
abort();
}
/* NULL terminate the test suite array. */
result = result_tmp;
result[result_sz] = NULL;
if (dir_fd >= 0)
close(dir_fd); close(dir_fd);
return result;
return files;
}
int list_script_max_width(void)
{
list_script_files(); /* Ensure we have scanned all scripts */
return files_max_width;
} }
...@@ -2,14 +2,8 @@ ...@@ -2,14 +2,8 @@
#ifndef TESTS_SCRIPTS_H #ifndef TESTS_SCRIPTS_H
#define TESTS_SCRIPTS_H #define TESTS_SCRIPTS_H
struct script_file { #include "tests.h"
char *file;
char *desc;
};
/* List available script tests to run - singleton - never freed */ struct test_suite **create_script_test_suites(void);
const struct script_file *list_script_files(void);
/* Get maximum width of description string */
int list_script_max_width(void);
#endif /* TESTS_SCRIPTS_H */ #endif /* TESTS_SCRIPTS_H */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment