perf tracepoint: Sort events in iterator
authorIan Rogers <irogers@google.com>
Mon, 14 Nov 2022 21:07:17 +0000 (13:07 -0800)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Tue, 15 Nov 2022 13:25:15 +0000 (10:25 -0300)
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>
tools/perf/util/print-events.c

index 6df947d..dae9272 100644 (file)
@@ -66,26 +66,21 @@ static int cmp_string(const void *a, const void *b)
 void print_tracepoint_events(const char *subsys_glob,
                             const char *event_glob, bool name_only)
 {
-       DIR *sys_dir, *evt_dir;
-       struct dirent *sys_dirent, *evt_dirent;
-       char evt_path[MAXPATHLEN];
-       char *dir_path;
-       char **evt_list = NULL;
-       unsigned int evt_i = 0, evt_num = 0;
-       bool evt_num_known = false;
-
-restart:
-       sys_dir = tracing_events__opendir();
-       if (!sys_dir)
-               return;
-
-       if (evt_num_known) {
-               evt_list = zalloc(sizeof(char *) * evt_num);
-               if (!evt_list)
-                       goto out_close_sys_dir;
-       }
+       struct dirent **sys_namelist = NULL;
+       bool printed = false;
+       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;
+               int evt_items;
+
+               if (sys_dirent->d_type != DT_DIR ||
+                   !strcmp(sys_dirent->d_name, ".") ||
+                   !strcmp(sys_dirent->d_name, ".."))
+                       continue;
 
-       for_each_subsystem(sys_dir, sys_dirent) {
                if (subsys_glob != NULL &&
                    !strglobmatch(sys_dirent->d_name, subsys_glob))
                        continue;
@@ -93,69 +88,40 @@ restart:
                dir_path = get_events_file(sys_dirent->d_name);
                if (!dir_path)
                        continue;
-               evt_dir = opendir(dir_path);
-               if (!evt_dir)
-                       goto next;
 
-               for_each_event(dir_path, evt_dir, evt_dirent) {
-                       if (event_glob != NULL &&
-                           !strglobmatch(evt_dirent->d_name, event_glob))
+               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 (!evt_num_known) {
-                               evt_num++;
+                       if (tp_event_has_id(dir_path, evt_dirent) != 0)
+                               continue;
+
+                       if (event_glob != NULL &&
+                           !strglobmatch(evt_dirent->d_name, event_glob))
                                continue;
-                       }
 
                        snprintf(evt_path, MAXPATHLEN, "%s:%s",
                                 sys_dirent->d_name, evt_dirent->d_name);
-
-                       evt_list[evt_i] = strdup(evt_path);
-                       if (evt_list[evt_i] == NULL) {
-                               put_events_file(dir_path);
-                               goto out_close_evt_dir;
+                       if (name_only)
+                               printf("%s ", evt_path);
+                       else {
+                               printf("  %-50s [%s]\n", evt_path,
+                                      event_type_descriptors[PERF_TYPE_TRACEPOINT]);
                        }
-                       evt_i++;
+                       printed = true;
                }
-               closedir(evt_dir);
-next:
-               put_events_file(dir_path);
+               free(dir_path);
+               free(evt_namelist);
        }
-       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]);
-       }
-       if (evt_num && pager_in_use())
+       free(sys_namelist);
+       if (printed && pager_in_use())
                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,