Skip to content

Commit d790ead

Browse files
captain5050acmel
authored andcommitted
perf tracepoint: Don't scan all tracepoints to test if one exists
In is_valid_tracepoint, rather than scanning "/sys/kernel/tracing/events/*/*" skipping any path where "/sys/kernel/tracing/events/*/*/id" doesn't exist, and then testing if "*:*" matches the tracepoint name, just use the given tracepoint name replace the ':' with '/' and see if the id file exists. This turns a nested directory search into a single file available test. Rather than return 1 for valid and 0 for invalid, return true and false. Signed-off-by: Ian Rogers <[email protected]> Cc: Adrian Hunter <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Kan Liang <[email protected]> Cc: Mark Rutland <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent c9d4923 commit d790ead

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

tools/perf/util/tracepoint.c

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#include <errno.h>
55
#include <fcntl.h>
66
#include <stdio.h>
7+
#include <stdlib.h>
78
#include <sys/param.h>
89
#include <unistd.h>
910

1011
#include <api/fs/tracing_path.h>
12+
#include "fncache.h"
1113

1214
int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
1315
{
@@ -26,39 +28,25 @@ int tp_event_has_id(const char *dir_path, struct dirent *evt_dir)
2628
/*
2729
* Check whether event is in <debugfs_mount_point>/tracing/events
2830
*/
29-
int is_valid_tracepoint(const char *event_string)
31+
bool is_valid_tracepoint(const char *event_string)
3032
{
31-
DIR *sys_dir, *evt_dir;
32-
struct dirent *sys_dirent, *evt_dirent;
33-
char evt_path[MAXPATHLEN];
34-
char *dir_path;
35-
36-
sys_dir = tracing_events__opendir();
37-
if (!sys_dir)
38-
return 0;
39-
40-
for_each_subsystem(sys_dir, sys_dirent) {
41-
dir_path = get_events_file(sys_dirent->d_name);
42-
if (!dir_path)
43-
continue;
44-
evt_dir = opendir(dir_path);
45-
if (!evt_dir)
46-
goto next;
47-
48-
for_each_event(dir_path, evt_dir, evt_dirent) {
49-
snprintf(evt_path, MAXPATHLEN, "%s:%s",
50-
sys_dirent->d_name, evt_dirent->d_name);
51-
if (!strcmp(evt_path, event_string)) {
52-
closedir(evt_dir);
53-
put_events_file(dir_path);
54-
closedir(sys_dir);
55-
return 1;
56-
}
57-
}
58-
closedir(evt_dir);
59-
next:
60-
put_events_file(dir_path);
61-
}
62-
closedir(sys_dir);
63-
return 0;
33+
char *dst, *path = malloc(strlen(event_string) + 4); /* Space for "/id\0". */
34+
bool have_file = false; /* Conservatively return false if memory allocation failed. */
35+
const char *src;
36+
37+
if (!path)
38+
return false;
39+
40+
/* Copy event_string replacing the ':' with '/'. */
41+
for (src = event_string, dst = path; *src; src++, dst++)
42+
*dst = (*src == ':') ? '/' : *src;
43+
/* Add "/id\0". */
44+
memcpy(dst, "/id", 4);
45+
46+
dst = get_events_file(path);
47+
if (dst)
48+
have_file = file_available(dst);
49+
free(dst);
50+
free(path);
51+
return have_file;
6452
}

tools/perf/util/tracepoint.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <dirent.h>
66
#include <string.h>
7+
#include <stdbool.h>
78

89
int tp_event_has_id(const char *dir_path, struct dirent *evt_dir);
910

@@ -20,6 +21,6 @@ int tp_event_has_id(const char *dir_path, struct dirent *evt_dir);
2021
(strcmp(sys_dirent->d_name, ".")) && \
2122
(strcmp(sys_dirent->d_name, "..")))
2223

23-
int is_valid_tracepoint(const char *event_string);
24+
bool is_valid_tracepoint(const char *event_string);
2425

2526
#endif /* __PERF_TRACEPOINT_H */

0 commit comments

Comments
 (0)