perf tracepoint: Sort events in iterator

In print_tracepoint_events() use tracing_events__scandir_alphasort() and
scandir alphasort so that the subsystem and events are sorted and don't
need a secondary qsort. Locally this results in the following change:

...
   ext4:ext4_zero_range                               [Tracepoint event]
-  fib6:fib6_table_lookup                             [Tracepoint event]
   fib:fib_table_lookup                               [Tracepoint event]
+  fib6:fib6_table_lookup                             [Tracepoint event]
   filelock:break_lease_block                         [Tracepoint event]
...

ie fib6 now is after fib and not before it. This is more consistent
with how numbers are more generally sorted, such as:

...
  syscalls:sys_enter_renameat                        [Tracepoint event]
  syscalls:sys_enter_renameat2                       [Tracepoint event]
...

and so an improvement over the qsort approach.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Caleb Biggers <caleb.biggers@intel.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Kajol Jain <kjain@linux.ibm.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Leo Yan <leo.yan@linaro.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Perry Taylor <perry.taylor@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Rob Herring <robh@kernel.org>
Cc: Sandipan Das <sandipan.das@amd.com>
Cc: Stephane Eranian <eranian@google.com>
Cc: Weilin Wang <weilin.wang@intel.com>
Cc: Xin Gao <gaoxin@cdjrlc.com>
Cc: Xing Zhengjun <zhengjun.xing@linux.intel.com>
Link: http://lore.kernel.org/lkml/20221114210723.2749751-5-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
This commit is contained in:
Ian Rogers 2022-11-14 13:07:17 -08:00 committed by Arnaldo Carvalho de Melo
parent 1504b6f97b
commit d74060c033
1 changed files with 35 additions and 69 deletions

View File

@ -66,26 +66,21 @@ static int cmp_string(const void *a, const void *b)
void print_tracepoint_events(const char *subsys_glob, void print_tracepoint_events(const char *subsys_glob,
const char *event_glob, bool name_only) const char *event_glob, bool name_only)
{ {
DIR *sys_dir, *evt_dir; struct dirent **sys_namelist = NULL;
struct dirent *sys_dirent, *evt_dirent; bool printed = false;
char evt_path[MAXPATHLEN]; int sys_items = tracing_events__scandir_alphasort(&sys_namelist);
for (int i = 0; i < sys_items; i++) {
struct dirent *sys_dirent = sys_namelist[i];
struct dirent **evt_namelist = NULL;
char *dir_path; char *dir_path;
char **evt_list = NULL; int evt_items;
unsigned int evt_i = 0, evt_num = 0;
bool evt_num_known = false;
restart: if (sys_dirent->d_type != DT_DIR ||
sys_dir = tracing_events__opendir(); !strcmp(sys_dirent->d_name, ".") ||
if (!sys_dir) !strcmp(sys_dirent->d_name, ".."))
return; continue;
if (evt_num_known) {
evt_list = zalloc(sizeof(char *) * evt_num);
if (!evt_list)
goto out_close_sys_dir;
}
for_each_subsystem(sys_dir, sys_dirent) {
if (subsys_glob != NULL && if (subsys_glob != NULL &&
!strglobmatch(sys_dirent->d_name, subsys_glob)) !strglobmatch(sys_dirent->d_name, subsys_glob))
continue; continue;
@ -93,69 +88,40 @@ restart:
dir_path = get_events_file(sys_dirent->d_name); dir_path = get_events_file(sys_dirent->d_name);
if (!dir_path) if (!dir_path)
continue; continue;
evt_dir = opendir(dir_path);
if (!evt_dir)
goto next;
for_each_event(dir_path, evt_dir, evt_dirent) { evt_items = scandir(dir_path, &evt_namelist, NULL, alphasort);
for (int j = 0; j < evt_items; j++) {
struct dirent *evt_dirent = evt_namelist[j];
char evt_path[MAXPATHLEN];
if (evt_dirent->d_type != DT_DIR ||
!strcmp(evt_dirent->d_name, ".") ||
!strcmp(evt_dirent->d_name, ".."))
continue;
if (tp_event_has_id(dir_path, evt_dirent) != 0)
continue;
if (event_glob != NULL && if (event_glob != NULL &&
!strglobmatch(evt_dirent->d_name, event_glob)) !strglobmatch(evt_dirent->d_name, event_glob))
continue; continue;
if (!evt_num_known) {
evt_num++;
continue;
}
snprintf(evt_path, MAXPATHLEN, "%s:%s", snprintf(evt_path, MAXPATHLEN, "%s:%s",
sys_dirent->d_name, evt_dirent->d_name); sys_dirent->d_name, evt_dirent->d_name);
if (name_only)
evt_list[evt_i] = strdup(evt_path); printf("%s ", evt_path);
if (evt_list[evt_i] == NULL) { else {
put_events_file(dir_path); printf(" %-50s [%s]\n", evt_path,
goto out_close_evt_dir;
}
evt_i++;
}
closedir(evt_dir);
next:
put_events_file(dir_path);
}
closedir(sys_dir);
if (!evt_num_known) {
evt_num_known = true;
goto restart;
}
qsort(evt_list, evt_num, sizeof(char *), cmp_string);
evt_i = 0;
while (evt_i < evt_num) {
if (name_only) {
printf("%s ", evt_list[evt_i++]);
continue;
}
printf(" %-50s [%s]\n", evt_list[evt_i++],
event_type_descriptors[PERF_TYPE_TRACEPOINT]); event_type_descriptors[PERF_TYPE_TRACEPOINT]);
} }
if (evt_num && pager_in_use()) printed = true;
}
free(dir_path);
free(evt_namelist);
}
free(sys_namelist);
if (printed && pager_in_use())
printf("\n"); printf("\n");
out_free:
evt_num = evt_i;
for (evt_i = 0; evt_i < evt_num; evt_i++)
zfree(&evt_list[evt_i]);
zfree(&evt_list);
return;
out_close_evt_dir:
closedir(evt_dir);
out_close_sys_dir:
closedir(sys_dir);
printf("FATAL: not enough memory to print %s\n",
event_type_descriptors[PERF_TYPE_TRACEPOINT]);
if (evt_list)
goto out_free;
} }
void print_sdt_events(const char *subsys_glob, const char *event_glob, void print_sdt_events(const char *subsys_glob, const char *event_glob,