Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf test: Iterate over shell tests in alphabetical order

The for_each_shell_test macro iterated over all shell tests in the
directory using readdir, which does not guarantee any ordering, causing
problems on certain fs. However, the order in which they are visited
determines the id of the test, in case one wants to run a single test.

This patch replaces readdir with scandir using alphabetical sorting.
This guarantees that, given the same set of tests, all machines will
see the tests in the same order, and, thus, that test ids are
consistent.

Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
Reported-by: Ian Rogers <irogers@google.com>
Acked-by: Ian Rogers <irogers@google.com>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Fabian Hemmer <copy@copy.sh>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Tommi Rantala <tommi.t.rantala@nokia.com>
Link: http://lore.kernel.org/lkml/20210525230521.244553-1-rickyman7@gmail.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Riccardo Mancini and committed by
Arnaldo Carvalho de Melo
da963834 41ca1d1e

+21 -17
+21 -17
tools/perf/tests/builtin-test.c
··· 510 510 return description ? strim(description + 1) : NULL; 511 511 } 512 512 513 - #define for_each_shell_test(dir, base, ent) \ 514 - while ((ent = readdir(dir)) != NULL) \ 513 + #define for_each_shell_test(entlist, nr, base, ent) \ 514 + for (int __i = 0; __i < nr && (ent = entlist[__i]); __i++) \ 515 515 if (!is_directory(base, ent) && ent->d_name[0] != '.') 516 516 517 517 static const char *shell_tests__dir(char *path, size_t size) ··· 538 538 539 539 static int shell_tests__max_desc_width(void) 540 540 { 541 - DIR *dir; 541 + struct dirent **entlist; 542 542 struct dirent *ent; 543 + int n_dirs; 543 544 char path_dir[PATH_MAX]; 544 545 const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); 545 546 int width = 0; ··· 548 547 if (path == NULL) 549 548 return -1; 550 549 551 - dir = opendir(path); 552 - if (!dir) 550 + n_dirs = scandir(path, &entlist, NULL, alphasort); 551 + if (n_dirs == -1) 553 552 return -1; 554 553 555 - for_each_shell_test(dir, path, ent) { 554 + for_each_shell_test(entlist, n_dirs, path, ent) { 556 555 char bf[256]; 557 556 const char *desc = shell_test__description(bf, sizeof(bf), path, ent->d_name); 558 557 ··· 564 563 } 565 564 } 566 565 567 - closedir(dir); 566 + free(entlist); 567 + 568 568 return width; 569 569 } 570 570 ··· 591 589 592 590 static int run_shell_tests(int argc, const char *argv[], int i, int width) 593 591 { 594 - DIR *dir; 592 + struct dirent **entlist; 595 593 struct dirent *ent; 594 + int n_dirs; 596 595 char path_dir[PATH_MAX]; 597 596 struct shell_test st = { 598 597 .dir = shell_tests__dir(path_dir, sizeof(path_dir)), ··· 602 599 if (st.dir == NULL) 603 600 return -1; 604 601 605 - dir = opendir(st.dir); 606 - if (!dir) { 602 + n_dirs = scandir(st.dir, &entlist, NULL, alphasort); 603 + if (n_dirs == -1) { 607 604 pr_err("failed to open shell test directory: %s\n", 608 605 st.dir); 609 606 return -1; 610 607 } 611 608 612 - for_each_shell_test(dir, st.dir, ent) { 609 + for_each_shell_test(entlist, n_dirs, st.dir, ent) { 613 610 int curr = i++; 614 611 char desc[256]; 615 612 struct test test = { ··· 626 623 test_and_print(&test, false, -1); 627 624 } 628 625 629 - closedir(dir); 626 + free(entlist); 630 627 return 0; 631 628 } 632 629 ··· 725 722 726 723 static int perf_test__list_shell(int argc, const char **argv, int i) 727 724 { 728 - DIR *dir; 725 + struct dirent **entlist; 729 726 struct dirent *ent; 727 + int n_dirs; 730 728 char path_dir[PATH_MAX]; 731 729 const char *path = shell_tests__dir(path_dir, sizeof(path_dir)); 732 730 733 731 if (path == NULL) 734 732 return -1; 735 733 736 - dir = opendir(path); 737 - if (!dir) 734 + n_dirs = scandir(path, &entlist, NULL, alphasort); 735 + if (n_dirs == -1) 738 736 return -1; 739 737 740 - for_each_shell_test(dir, path, ent) { 738 + for_each_shell_test(entlist, n_dirs, path, ent) { 741 739 int curr = i++; 742 740 char bf[256]; 743 741 struct test t = { ··· 751 747 pr_info("%2d: %s\n", i, t.desc); 752 748 } 753 749 754 - closedir(dir); 750 + free(entlist); 755 751 return 0; 756 752 } 757 753