perf test: Refactor shell tests allowing subdirs

This is a prelude to adding more tests to shell tests and in order to
support putting those tests into subdirectories, I need to change the
test code that scans/finds and runs them.

To support subdirs I have to recurse so it's time to refactor the code
to allow this and centralize the shell script finding into one location
and only one single scan that builds a list of all the found tests in
memory instead of it being duplicated in 3 places.

This code also optimizes things like knowing the max width of desciption
strings (as we can do that while we scan instead of a whole new pass of
opening files).

It also more cleanly filters scripts to see only *.sh files thus
skipping random other files in directories like *~ backup files, other
random junk/data files that may appear and the scripts must be
executable to make the cut (this ensures the script lib dir is not seen
as scripts to run).

This avoids perf test running previous older versions of test scripts
that are editor backup files as well as skipping perf.data files that
may appear and so on.

Reviewed-by: Leo Yan <leo.yan@linaro.org>
Signed-off-by: Carsten Haitzler <carsten.haitzler@arm.com>
Tested-by: Leo Yan <leo.yan@linaro.org>
Cc: Mathieu Poirier <mathieu.poirier@linaro.org>
Cc: Mike Leach <mike.leach@linaro.org>
Cc: Suzuki Poulouse <suzuki.poulose@arm.com>
Cc: coresight@lists.linaro.org
Link: https://lore.kernel.org/r/20220812121641.336465-2-carsten.haitzler@foss.arm.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by Carsten Haitzler and committed by Arnaldo Carvalho de Melo 7391db64 aa0d6e9c

Changed files
+238 -134
tools
+1
tools/perf/tests/Build
··· 1 1 # SPDX-License-Identifier: GPL-2.0 2 2 3 3 perf-y += builtin-test.o 4 + perf-y += builtin-test-list.o 4 5 perf-y += parse-events.o 5 6 perf-y += dso-data.o 6 7 perf-y += attr.o
+207
tools/perf/tests/builtin-test-list.c
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + #include <dirent.h> 3 + #include <errno.h> 4 + #include <fcntl.h> 5 + #include <linux/ctype.h> 6 + #include <linux/kernel.h> 7 + #include <linux/string.h> 8 + #include <linux/zalloc.h> 9 + #include <string.h> 10 + #include <stdlib.h> 11 + #include <sys/types.h> 12 + #include <unistd.h> 13 + #include <subcmd/exec-cmd.h> 14 + #include <subcmd/parse-options.h> 15 + #include <sys/wait.h> 16 + #include <sys/stat.h> 17 + #include "builtin.h" 18 + #include "builtin-test-list.h" 19 + #include "color.h" 20 + #include "debug.h" 21 + #include "hist.h" 22 + #include "intlist.h" 23 + #include "string2.h" 24 + #include "symbol.h" 25 + #include "tests.h" 26 + #include "util/rlimit.h" 27 + 28 + 29 + /* 30 + * As this is a singleton built once for the run of the process, there is 31 + * no value in trying to free it and just let it stay around until process 32 + * exits when it's cleaned up. 33 + */ 34 + static size_t files_num = 0; 35 + static struct script_file *files = NULL; 36 + static int files_max_width = 0; 37 + 38 + static const char *shell_tests__dir(char *path, size_t size) 39 + { 40 + const char *devel_dirs[] = { "./tools/perf/tests", "./tests", }; 41 + char *exec_path; 42 + unsigned int i; 43 + 44 + for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { 45 + struct stat st; 46 + 47 + if (!lstat(devel_dirs[i], &st)) { 48 + scnprintf(path, size, "%s/shell", devel_dirs[i]); 49 + if (!lstat(devel_dirs[i], &st)) 50 + return path; 51 + } 52 + } 53 + 54 + /* Then installed path. */ 55 + exec_path = get_argv_exec_path(); 56 + scnprintf(path, size, "%s/tests/shell", exec_path); 57 + free(exec_path); 58 + return path; 59 + } 60 + 61 + static const char *shell_test__description(char *description, size_t size, 62 + const char *path, const char *name) 63 + { 64 + FILE *fp; 65 + char filename[PATH_MAX]; 66 + int ch; 67 + 68 + path__join(filename, sizeof(filename), path, name); 69 + fp = fopen(filename, "r"); 70 + if (!fp) 71 + return NULL; 72 + 73 + /* Skip first line - should be #!/bin/sh Shebang */ 74 + do { 75 + ch = fgetc(fp); 76 + } while (ch != EOF && ch != '\n'); 77 + 78 + description = fgets(description, size, fp); 79 + fclose(fp); 80 + 81 + /* Assume first char on line is omment everything after that desc */ 82 + return description ? strim(description + 1) : NULL; 83 + } 84 + 85 + /* Is this full file path a shell script */ 86 + static bool is_shell_script(const char *path) 87 + { 88 + const char *ext; 89 + 90 + ext = strrchr(path, '.'); 91 + if (!ext) 92 + return false; 93 + if (!strcmp(ext, ".sh")) { /* Has .sh extension */ 94 + if (access(path, R_OK | X_OK) == 0) /* Is executable */ 95 + return true; 96 + } 97 + return false; 98 + } 99 + 100 + /* Is this file in this dir a shell script (for test purposes) */ 101 + static bool is_test_script(const char *path, const char *name) 102 + { 103 + char filename[PATH_MAX]; 104 + 105 + path__join(filename, sizeof(filename), path, name); 106 + if (!is_shell_script(filename)) return false; 107 + return true; 108 + } 109 + 110 + /* Duplicate a string and fall over and die if we run out of memory */ 111 + static char *strdup_check(const char *str) 112 + { 113 + char *newstr; 114 + 115 + newstr = strdup(str); 116 + if (!newstr) { 117 + pr_err("Out of memory while duplicating test script string\n"); 118 + abort(); 119 + } 120 + return newstr; 121 + } 122 + 123 + static void append_script(const char *dir, const char *file, const char *desc) 124 + { 125 + struct script_file *files_tmp; 126 + size_t files_num_tmp; 127 + int width; 128 + 129 + files_num_tmp = files_num + 1; 130 + if (files_num_tmp >= SIZE_MAX) { 131 + pr_err("Too many script files\n"); 132 + abort(); 133 + } 134 + /* Realloc is good enough, though we could realloc by chunks, not that 135 + * anyone will ever measure performance here */ 136 + files_tmp = realloc(files, 137 + (files_num_tmp + 1) * sizeof(struct script_file)); 138 + if (files_tmp == NULL) { 139 + pr_err("Out of memory while building test list\n"); 140 + abort(); 141 + } 142 + /* Add file to end and NULL terminate the struct array */ 143 + files = files_tmp; 144 + files_num = files_num_tmp; 145 + files[files_num - 1].dir = strdup_check(dir); 146 + files[files_num - 1].file = strdup_check(file); 147 + files[files_num - 1].desc = strdup_check(desc); 148 + files[files_num].dir = NULL; 149 + files[files_num].file = NULL; 150 + files[files_num].desc = NULL; 151 + 152 + width = strlen(desc); /* Track max width of desc */ 153 + if (width > files_max_width) 154 + files_max_width = width; 155 + } 156 + 157 + static void append_scripts_in_dir(const char *path) 158 + { 159 + struct dirent **entlist; 160 + struct dirent *ent; 161 + int n_dirs, i; 162 + char filename[PATH_MAX]; 163 + 164 + /* List files, sorted by alpha */ 165 + n_dirs = scandir(path, &entlist, NULL, alphasort); 166 + if (n_dirs == -1) 167 + return; 168 + for (i = 0; i < n_dirs && (ent = entlist[i]); i++) { 169 + if (ent->d_name[0] == '.') 170 + continue; /* Skip hidden files */ 171 + if (is_test_script(path, ent->d_name)) { /* It's a test */ 172 + char bf[256]; 173 + const char *desc = shell_test__description 174 + (bf, sizeof(bf), path, ent->d_name); 175 + 176 + if (desc) /* It has a desc line - valid script */ 177 + append_script(path, ent->d_name, desc); 178 + } else if (is_directory(path, ent)) { /* Scan the subdir */ 179 + path__join(filename, sizeof(filename), 180 + path, ent->d_name); 181 + append_scripts_in_dir(filename); 182 + } 183 + } 184 + for (i = 0; i < n_dirs; i++) /* Clean up */ 185 + zfree(&entlist[i]); 186 + free(entlist); 187 + } 188 + 189 + const struct script_file *list_script_files(void) 190 + { 191 + char path_dir[PATH_MAX]; 192 + const char *path; 193 + 194 + if (files) 195 + return files; /* Singleton - we already know our list */ 196 + 197 + path = shell_tests__dir(path_dir, sizeof(path_dir)); /* Walk dir */ 198 + append_scripts_in_dir(path); 199 + 200 + return files; 201 + } 202 + 203 + int list_script_max_width(void) 204 + { 205 + list_script_files(); /* Ensure we have scanned all scripts */ 206 + return files_max_width; 207 + }
+12
tools/perf/tests/builtin-test-list.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + 3 + struct script_file { 4 + char *dir; 5 + char *file; 6 + char *desc; 7 + }; 8 + 9 + /* List available script tests to run - singleton - never freed */ 10 + const struct script_file *list_script_files(void); 11 + /* Get maximum width of description string */ 12 + int list_script_max_width(void);
+18 -134
tools/perf/tests/builtin-test.c
··· 28 28 #include <subcmd/exec-cmd.h> 29 29 #include <linux/zalloc.h> 30 30 31 + #include "builtin-test-list.h" 32 + 31 33 static bool dont_fork; 32 34 33 35 struct test_suite *__weak arch_tests[] = { ··· 276 274 return err; 277 275 } 278 276 279 - static const char *shell_test__description(char *description, size_t size, 280 - const char *path, const char *name) 281 - { 282 - FILE *fp; 283 - char filename[PATH_MAX]; 284 - int ch; 285 - 286 - path__join(filename, sizeof(filename), path, name); 287 - fp = fopen(filename, "r"); 288 - if (!fp) 289 - return NULL; 290 - 291 - /* Skip shebang */ 292 - do { 293 - ch = fgetc(fp); 294 - } while (ch != EOF && ch != '\n'); 295 - 296 - description = fgets(description, size, fp); 297 - fclose(fp); 298 - 299 - return description ? strim(description + 1) : NULL; 300 - } 301 - 302 - #define for_each_shell_test(entlist, nr, base, ent) \ 303 - for (int __i = 0; __i < nr && (ent = entlist[__i]); __i++) \ 304 - if (!is_directory(base, ent) && \ 305 - is_executable_file(base, ent) && \ 306 - ent->d_name[0] != '.') 307 - 308 - static const char *shell_tests__dir(char *path, size_t size) 309 - { 310 - const char *devel_dirs[] = { "./tools/perf/tests", "./tests", }; 311 - char *exec_path; 312 - unsigned int i; 313 - 314 - for (i = 0; i < ARRAY_SIZE(devel_dirs); ++i) { 315 - struct stat st; 316 - if (!lstat(devel_dirs[i], &st)) { 317 - scnprintf(path, size, "%s/shell", devel_dirs[i]); 318 - if (!lstat(devel_dirs[i], &st)) 319 - return path; 320 - } 321 - } 322 - 323 - /* Then installed path. */ 324 - exec_path = get_argv_exec_path(); 325 - scnprintf(path, size, "%s/tests/shell", exec_path); 326 - free(exec_path); 327 - return path; 328 - } 329 - 330 - static int shell_tests__max_desc_width(void) 331 - { 332 - struct dirent **entlist; 333 - struct dirent *ent; 334 - int n_dirs, e; 335 - char path_dir[PATH_MAX]; 336 - const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); 337 - int width = 0; 338 - 339 - if (path == NULL) 340 - return -1; 341 - 342 - n_dirs = scandir(path, &entlist, NULL, alphasort); 343 - if (n_dirs == -1) 344 - return -1; 345 - 346 - for_each_shell_test(entlist, n_dirs, path, ent) { 347 - char bf[256]; 348 - const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name); 349 - 350 - if (desc) { 351 - int len = strlen(desc); 352 - 353 - if (width < len) 354 - width = len; 355 - } 356 - } 357 - 358 - for (e = 0; e < n_dirs; e++) 359 - zfree(&entlist[e]); 360 - free(entlist); 361 - return width; 362 - } 363 - 364 277 struct shell_test { 365 278 const char *dir; 366 279 const char *file; ··· 302 385 static int run_shell_tests(int argc, const char *argv[], int i, int width, 303 386 struct intlist *skiplist) 304 387 { 305 - struct dirent **entlist; 306 - struct dirent *ent; 307 - int n_dirs, e; 308 - char path_dir[PATH_MAX]; 309 - struct shell_test st = { 310 - .dir = shell_tests__dir(path_dir, sizeof(path_dir)), 311 - }; 388 + struct shell_test st; 389 + const struct script_file *files, *file; 312 390 313 - if (st.dir == NULL) 314 - return -1; 315 - 316 - n_dirs = scandir(st.dir, &entlist, NULL, alphasort); 317 - if (n_dirs == -1) { 318 - pr_err("failed to open shell test directory: %s\n", 319 - st.dir); 320 - return -1; 321 - } 322 - 323 - for_each_shell_test(entlist, n_dirs, st.dir, ent) { 391 + files = list_script_files(); 392 + if (!files) 393 + return 0; 394 + for (file = files; file->dir; file++) { 324 395 int curr = i++; 325 - char desc[256]; 326 396 struct test_case test_cases[] = { 327 397 { 328 - .desc = shell_test__description(desc, 329 - sizeof(desc), 330 - st.dir, 331 - ent->d_name), 398 + .desc = file->desc, 332 399 .run_case = shell_test__run, 333 400 }, 334 401 { .name = NULL, } ··· 322 421 .test_cases = test_cases, 323 422 .priv = &st, 324 423 }; 424 + st.dir = file->dir; 325 425 326 426 if (test_suite.desc == NULL || 327 427 !perf_test__matches(test_suite.desc, curr, argc, argv)) 328 428 continue; 329 429 330 - st.file = ent->d_name; 430 + st.file = file->file; 331 431 pr_info("%3d: %-*s:", i, width, test_suite.desc); 332 432 333 433 if (intlist__find(skiplist, i)) { ··· 338 436 339 437 test_and_print(&test_suite, 0); 340 438 } 341 - 342 - for (e = 0; e < n_dirs; e++) 343 - zfree(&entlist[e]); 344 - free(entlist); 345 439 return 0; 346 440 } 347 441 ··· 346 448 struct test_suite *t; 347 449 unsigned int j, k; 348 450 int i = 0; 349 - int width = shell_tests__max_desc_width(); 451 + int width = list_script_max_width(); 350 452 351 453 for_each_test(j, k, t) { 352 454 int len = strlen(test_description(t, -1)); ··· 427 529 428 530 static int perf_test__list_shell(int argc, const char **argv, int i) 429 531 { 430 - struct dirent **entlist; 431 - struct dirent *ent; 432 - int n_dirs, e; 433 - char path_dir[PATH_MAX]; 434 - const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); 532 + const struct script_file *files, *file; 435 533 436 - if (path == NULL) 437 - return -1; 438 - 439 - n_dirs = scandir(path, &entlist, NULL, alphasort); 440 - if (n_dirs == -1) 441 - return -1; 442 - 443 - for_each_shell_test(entlist, n_dirs, path, ent) { 534 + files = list_script_files(); 535 + if (!files) 536 + return 0; 537 + for (file = files; file->dir; file++) { 444 538 int curr = i++; 445 - char bf[256]; 446 539 struct test_suite t = { 447 - .desc = shell_test__description(bf, sizeof(bf), path, ent->d_name), 540 + .desc = file->desc 448 541 }; 449 542 450 543 if (!perf_test__matches(t.desc, curr, argc, argv)) 451 544 continue; 452 545 453 546 pr_info("%3d: %s\n", i, t.desc); 454 - 455 547 } 456 - 457 - for (e = 0; e < n_dirs; e++) 458 - zfree(&entlist[e]); 459 - free(entlist); 460 548 return 0; 461 549 } 462 550