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 script: Move find_scripts to browser/scripts.c

The only use of find_scripts is in browser/scripts.c but the
definition in builtin causes linking problems requiring a stub in
python.c. Move the function to allow the stub to be removed.

Signed-off-by: Ian Rogers <irogers@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
Cc: Colin Ian King <colin.i.king@gmail.com>
Cc: Dapeng Mi <dapeng1.mi@linux.intel.com>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Ilya Leoshkevich <iii@linux.ibm.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Josh Poimboeuf <jpoimboe@redhat.com>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Richter <tmricht@linux.ibm.com>
Cc: Veronika Molnarova <vmolnaro@redhat.com>
Cc: Weilin Wang <weilin.wang@intel.com>
Link: https://lore.kernel.org/r/20241119011644.971342-8-irogers@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
d927e30c f76f94dc

+175 -185
-171
tools/perf/builtin-script.c
··· 3526 3526 free(dlargv); 3527 3527 } 3528 3528 3529 - /* 3530 - * Some scripts specify the required events in their "xxx-record" file, 3531 - * this function will check if the events in perf.data match those 3532 - * mentioned in the "xxx-record". 3533 - * 3534 - * Fixme: All existing "xxx-record" are all in good formats "-e event ", 3535 - * which is covered well now. And new parsing code should be added to 3536 - * cover the future complex formats like event groups etc. 3537 - */ 3538 - static int check_ev_match(int dir_fd, const char *scriptname, struct perf_session *session) 3539 - { 3540 - char line[BUFSIZ]; 3541 - FILE *fp; 3542 - 3543 - { 3544 - char filename[FILENAME_MAX + 5]; 3545 - int fd; 3546 - 3547 - scnprintf(filename, sizeof(filename), "bin/%s-record", scriptname); 3548 - fd = openat(dir_fd, filename, O_RDONLY); 3549 - if (fd == -1) 3550 - return -1; 3551 - fp = fdopen(fd, "r"); 3552 - if (!fp) 3553 - return -1; 3554 - } 3555 - 3556 - while (fgets(line, sizeof(line), fp)) { 3557 - char *p = skip_spaces(line); 3558 - 3559 - if (*p == '#') 3560 - continue; 3561 - 3562 - while (strlen(p)) { 3563 - int match, len; 3564 - struct evsel *pos; 3565 - char evname[128]; 3566 - 3567 - p = strstr(p, "-e"); 3568 - if (!p) 3569 - break; 3570 - 3571 - p += 2; 3572 - p = skip_spaces(p); 3573 - len = strcspn(p, " \t"); 3574 - if (!len) 3575 - break; 3576 - 3577 - snprintf(evname, len + 1, "%s", p); 3578 - 3579 - match = 0; 3580 - evlist__for_each_entry(session->evlist, pos) { 3581 - if (evsel__name_is(pos, evname)) { 3582 - match = 1; 3583 - break; 3584 - } 3585 - } 3586 - 3587 - if (!match) { 3588 - fclose(fp); 3589 - return -1; 3590 - } 3591 - } 3592 - } 3593 - 3594 - fclose(fp); 3595 - return 0; 3596 - } 3597 - 3598 - /* 3599 - * Return -1 if none is found, otherwise the actual scripts number. 3600 - * 3601 - * Currently the only user of this function is the script browser, which 3602 - * will list all statically runnable scripts, select one, execute it and 3603 - * show the output in a perf browser. 3604 - */ 3605 - int find_scripts(char **scripts_array, char **scripts_path_array, int num, 3606 - int pathlen) 3607 - { 3608 - struct dirent *script_dirent, *lang_dirent; 3609 - int scripts_dir_fd, lang_dir_fd; 3610 - DIR *scripts_dir, *lang_dir; 3611 - struct perf_session *session; 3612 - struct perf_data data = { 3613 - .path = input_name, 3614 - .mode = PERF_DATA_MODE_READ, 3615 - }; 3616 - char *temp; 3617 - int i = 0; 3618 - const char *exec_path = get_argv_exec_path(); 3619 - 3620 - session = perf_session__new(&data, NULL); 3621 - if (IS_ERR(session)) 3622 - return PTR_ERR(session); 3623 - 3624 - { 3625 - char scripts_path[PATH_MAX]; 3626 - 3627 - snprintf(scripts_path, sizeof(scripts_path), "%s/scripts", exec_path); 3628 - scripts_dir_fd = open(scripts_path, O_DIRECTORY); 3629 - pr_err("Failed to open directory '%s'", scripts_path); 3630 - if (scripts_dir_fd == -1) { 3631 - perf_session__delete(session); 3632 - return -1; 3633 - } 3634 - } 3635 - scripts_dir = fdopendir(scripts_dir_fd); 3636 - if (!scripts_dir) { 3637 - close(scripts_dir_fd); 3638 - perf_session__delete(session); 3639 - return -1; 3640 - } 3641 - 3642 - while ((lang_dirent = readdir(scripts_dir)) != NULL) { 3643 - if (lang_dirent->d_type != DT_DIR && 3644 - (lang_dirent->d_type == DT_UNKNOWN && 3645 - !is_directory_at(scripts_dir_fd, lang_dirent->d_name))) 3646 - continue; 3647 - if (!strcmp(lang_dirent->d_name, ".") || !strcmp(lang_dirent->d_name, "..")) 3648 - continue; 3649 - 3650 - #ifndef HAVE_LIBPERL_SUPPORT 3651 - if (strstr(lang_dirent->d_name, "perl")) 3652 - continue; 3653 - #endif 3654 - #ifndef HAVE_LIBPYTHON_SUPPORT 3655 - if (strstr(lang_dirent->d_name, "python")) 3656 - continue; 3657 - #endif 3658 - 3659 - lang_dir_fd = openat(scripts_dir_fd, lang_dirent->d_name, O_DIRECTORY); 3660 - if (lang_dir_fd == -1) 3661 - continue; 3662 - lang_dir = fdopendir(lang_dir_fd); 3663 - if (!lang_dir) { 3664 - close(lang_dir_fd); 3665 - continue; 3666 - } 3667 - while ((script_dirent = readdir(lang_dir)) != NULL) { 3668 - if (script_dirent->d_type == DT_DIR) 3669 - continue; 3670 - if (script_dirent->d_type == DT_UNKNOWN && 3671 - is_directory_at(lang_dir_fd, script_dirent->d_name)) 3672 - continue; 3673 - /* Skip those real time scripts: xxxtop.p[yl] */ 3674 - if (strstr(script_dirent->d_name, "top.")) 3675 - continue; 3676 - if (i >= num) 3677 - break; 3678 - scnprintf(scripts_path_array[i], pathlen, "%s/scripts/%s/%s", 3679 - exec_path, 3680 - lang_dirent->d_name, 3681 - script_dirent->d_name); 3682 - temp = strchr(script_dirent->d_name, '.'); 3683 - snprintf(scripts_array[i], 3684 - (temp - script_dirent->d_name) + 1, 3685 - "%s", script_dirent->d_name); 3686 - 3687 - if (check_ev_match(lang_dir_fd, scripts_array[i], session)) 3688 - continue; 3689 - 3690 - i++; 3691 - } 3692 - closedir(lang_dir); 3693 - } 3694 - 3695 - closedir(scripts_dir); 3696 - perf_session__delete(session); 3697 - return i; 3698 - } 3699 - 3700 3529 static char *get_script_path(const char *script_root, const char *suffix) 3701 3530 { 3702 3531 struct dirent *script_dirent, *lang_dirent;
-6
tools/perf/builtin.h
··· 2 2 #ifndef BUILTIN_H 3 3 #define BUILTIN_H 4 4 5 - #include <stddef.h> 6 - #include <linux/compiler.h> 7 - #include <tools/config.h> 8 - 9 5 struct feature_status { 10 6 const char *name; 11 7 const char *macro; ··· 52 56 int cmd_daemon(int argc, const char **argv); 53 57 int cmd_kwork(int argc, const char **argv); 54 58 55 - int find_scripts(char **scripts_array, char **scripts_path_array, int num, 56 - int pathlen); 57 59 #endif
+175 -2
tools/perf/ui/browsers/scripts.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 - #include "../../builtin.h" 3 - #include "../../perf.h" 4 2 #include "../../util/util.h" // perf_exe() 5 3 #include "../util.h" 4 + #include "../../util/evlist.h" 6 5 #include "../../util/hist.h" 7 6 #include "../../util/debug.h" 7 + #include "../../util/session.h" 8 8 #include "../../util/symbol.h" 9 9 #include "../browser.h" 10 10 #include "../libslang.h" 11 11 #include "config.h" 12 + #include <linux/err.h> 12 13 #include <linux/string.h> 13 14 #include <linux/zalloc.h> 15 + #include <subcmd/exec-cmd.h> 14 16 #include <stdlib.h> 15 17 16 18 #define SCRIPT_NAMELEN 128 ··· 77 75 return -1; 78 76 c->index++; 79 77 return 0; 78 + } 79 + 80 + /* 81 + * Some scripts specify the required events in their "xxx-record" file, 82 + * this function will check if the events in perf.data match those 83 + * mentioned in the "xxx-record". 84 + * 85 + * Fixme: All existing "xxx-record" are all in good formats "-e event ", 86 + * which is covered well now. And new parsing code should be added to 87 + * cover the future complex formats like event groups etc. 88 + */ 89 + static int check_ev_match(int dir_fd, const char *scriptname, struct perf_session *session) 90 + { 91 + char line[BUFSIZ]; 92 + FILE *fp; 93 + 94 + { 95 + char filename[FILENAME_MAX + 5]; 96 + int fd; 97 + 98 + scnprintf(filename, sizeof(filename), "bin/%s-record", scriptname); 99 + fd = openat(dir_fd, filename, O_RDONLY); 100 + if (fd == -1) 101 + return -1; 102 + fp = fdopen(fd, "r"); 103 + if (!fp) 104 + return -1; 105 + } 106 + 107 + while (fgets(line, sizeof(line), fp)) { 108 + char *p = skip_spaces(line); 109 + 110 + if (*p == '#') 111 + continue; 112 + 113 + while (strlen(p)) { 114 + int match, len; 115 + struct evsel *pos; 116 + char evname[128]; 117 + 118 + p = strstr(p, "-e"); 119 + if (!p) 120 + break; 121 + 122 + p += 2; 123 + p = skip_spaces(p); 124 + len = strcspn(p, " \t"); 125 + if (!len) 126 + break; 127 + 128 + snprintf(evname, len + 1, "%s", p); 129 + 130 + match = 0; 131 + evlist__for_each_entry(session->evlist, pos) { 132 + if (evsel__name_is(pos, evname)) { 133 + match = 1; 134 + break; 135 + } 136 + } 137 + 138 + if (!match) { 139 + fclose(fp); 140 + return -1; 141 + } 142 + } 143 + } 144 + 145 + fclose(fp); 146 + return 0; 147 + } 148 + 149 + /* 150 + * Return -1 if none is found, otherwise the actual scripts number. 151 + * 152 + * Currently the only user of this function is the script browser, which 153 + * will list all statically runnable scripts, select one, execute it and 154 + * show the output in a perf browser. 155 + */ 156 + static int find_scripts(char **scripts_array, char **scripts_path_array, int num, 157 + int pathlen) 158 + { 159 + struct dirent *script_dirent, *lang_dirent; 160 + int scripts_dir_fd, lang_dir_fd; 161 + DIR *scripts_dir, *lang_dir; 162 + struct perf_session *session; 163 + struct perf_data data = { 164 + .path = input_name, 165 + .mode = PERF_DATA_MODE_READ, 166 + }; 167 + char *temp; 168 + int i = 0; 169 + const char *exec_path = get_argv_exec_path(); 170 + 171 + session = perf_session__new(&data, NULL); 172 + if (IS_ERR(session)) 173 + return PTR_ERR(session); 174 + 175 + { 176 + char scripts_path[PATH_MAX]; 177 + 178 + snprintf(scripts_path, sizeof(scripts_path), "%s/scripts", exec_path); 179 + scripts_dir_fd = open(scripts_path, O_DIRECTORY); 180 + pr_err("Failed to open directory '%s'", scripts_path); 181 + if (scripts_dir_fd == -1) { 182 + perf_session__delete(session); 183 + return -1; 184 + } 185 + } 186 + scripts_dir = fdopendir(scripts_dir_fd); 187 + if (!scripts_dir) { 188 + close(scripts_dir_fd); 189 + perf_session__delete(session); 190 + return -1; 191 + } 192 + 193 + while ((lang_dirent = readdir(scripts_dir)) != NULL) { 194 + if (lang_dirent->d_type != DT_DIR && 195 + (lang_dirent->d_type == DT_UNKNOWN && 196 + !is_directory_at(scripts_dir_fd, lang_dirent->d_name))) 197 + continue; 198 + if (!strcmp(lang_dirent->d_name, ".") || !strcmp(lang_dirent->d_name, "..")) 199 + continue; 200 + 201 + #ifndef HAVE_LIBPERL_SUPPORT 202 + if (strstr(lang_dirent->d_name, "perl")) 203 + continue; 204 + #endif 205 + #ifndef HAVE_LIBPYTHON_SUPPORT 206 + if (strstr(lang_dirent->d_name, "python")) 207 + continue; 208 + #endif 209 + 210 + lang_dir_fd = openat(scripts_dir_fd, lang_dirent->d_name, O_DIRECTORY); 211 + if (lang_dir_fd == -1) 212 + continue; 213 + lang_dir = fdopendir(lang_dir_fd); 214 + if (!lang_dir) { 215 + close(lang_dir_fd); 216 + continue; 217 + } 218 + while ((script_dirent = readdir(lang_dir)) != NULL) { 219 + if (script_dirent->d_type == DT_DIR) 220 + continue; 221 + if (script_dirent->d_type == DT_UNKNOWN && 222 + is_directory_at(lang_dir_fd, script_dirent->d_name)) 223 + continue; 224 + /* Skip those real time scripts: xxxtop.p[yl] */ 225 + if (strstr(script_dirent->d_name, "top.")) 226 + continue; 227 + if (i >= num) 228 + break; 229 + scnprintf(scripts_path_array[i], pathlen, "%s/scripts/%s/%s", 230 + exec_path, 231 + lang_dirent->d_name, 232 + script_dirent->d_name); 233 + temp = strchr(script_dirent->d_name, '.'); 234 + snprintf(scripts_array[i], 235 + (temp - script_dirent->d_name) + 1, 236 + "%s", script_dirent->d_name); 237 + 238 + if (check_ev_match(lang_dir_fd, scripts_array[i], session)) 239 + continue; 240 + 241 + i++; 242 + } 243 + closedir(lang_dir); 244 + } 245 + 246 + closedir(scripts_dir); 247 + perf_session__delete(session); 248 + return i; 80 249 } 81 250 82 251 /*
-6
tools/perf/util/python.c
··· 1307 1307 /* The following are stubs to avoid dragging in builtin-* objects. */ 1308 1308 /* TODO: move the code out of the builtin-* file into util. */ 1309 1309 1310 - int find_scripts(char **scripts_array __maybe_unused, char **scripts_path_array __maybe_unused, 1311 - int num __maybe_unused, int pathlen __maybe_unused) 1312 - { 1313 - return -1; 1314 - } 1315 - 1316 1310 void perf_stat__set_no_csv_summary(int set __maybe_unused) 1317 1311 { 1318 1312 }