Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Howells | d2709c7 | 2012-11-19 22:21:03 +0000 | [diff] [blame] | 2 | #include <linux/hw_breakpoint.h> |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 3 | #include <linux/err.h> |
Arnaldo Carvalho de Melo | 7f7c536 | 2019-07-04 11:32:27 -0300 | [diff] [blame] | 4 | #include <linux/zalloc.h> |
Arnaldo Carvalho de Melo | 76b31a2 | 2017-04-18 12:26:44 -0300 | [diff] [blame] | 5 | #include <dirent.h> |
Arnaldo Carvalho de Melo | a43783a | 2017-04-18 10:46:11 -0300 | [diff] [blame] | 6 | #include <errno.h> |
Arnaldo Carvalho de Melo | 86a5e0c | 2017-04-19 19:03:14 -0300 | [diff] [blame] | 7 | #include <sys/ioctl.h> |
Arnaldo Carvalho de Melo | bafae98 | 2018-01-22 16:42:16 -0300 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <fcntl.h> |
Arnaldo Carvalho de Melo | 391e420 | 2017-04-19 18:51:14 -0300 | [diff] [blame] | 11 | #include <sys/param.h> |
Arnaldo Carvalho de Melo | b0742e9 | 2017-04-18 11:08:10 -0300 | [diff] [blame] | 12 | #include "term.h" |
Arnaldo Carvalho de Melo | 4a3cec8 | 2019-08-30 11:11:01 -0300 | [diff] [blame] | 13 | #include "build-id.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 14 | #include "evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 15 | #include "evsel.h" |
Arnaldo Carvalho de Melo | fa0d984 | 2019-08-30 12:52:25 -0300 | [diff] [blame] | 16 | #include <subcmd/pager.h> |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 17 | #include <subcmd/parse-options.h> |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 18 | #include "parse-events.h" |
Josh Poimboeuf | 4b6ab94 | 2015-12-15 09:39:39 -0600 | [diff] [blame] | 19 | #include <subcmd/exec-cmd.h> |
Arnaldo Carvalho de Melo | a067558 | 2017-04-17 16:51:59 -0300 | [diff] [blame] | 20 | #include "string2.h" |
Arnaldo Carvalho de Melo | 8ec20b1 | 2017-04-18 10:57:25 -0300 | [diff] [blame] | 21 | #include "strlist.h" |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 22 | #include "bpf-loader.h" |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 23 | #include "debug.h" |
Jiri Olsa | 592d5a6 | 2015-09-02 09:56:34 +0200 | [diff] [blame] | 24 | #include <api/fs/tracing_path.h> |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 25 | #include <perf/cpumap.h> |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 26 | #include "parse-events-bison.h" |
Jiri Olsa | 1244a32 | 2020-05-25 00:42:08 +0200 | [diff] [blame] | 27 | #define YY_EXTRA_TYPE void* |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 28 | #include "parse-events-flex.h" |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 29 | #include "pmu.h" |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 30 | #include "thread_map.h" |
Masami Hiramatsu | 40218da | 2016-06-24 18:06:46 +0900 | [diff] [blame] | 31 | #include "probe-file.h" |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 32 | #include "asm/bug.h" |
Andi Kleen | ac12f67 | 2016-10-12 14:02:06 -0700 | [diff] [blame] | 33 | #include "util/parse-branch-options.h" |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 34 | #include "metricgroup.h" |
Arnaldo Carvalho de Melo | 95be9d1 | 2019-09-24 15:56:14 -0300 | [diff] [blame] | 35 | #include "util/evsel_config.h" |
Arnaldo Carvalho de Melo | 252a2fd | 2019-09-24 16:07:59 -0300 | [diff] [blame] | 36 | #include "util/event.h" |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 37 | #include "util/pfm.h" |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 38 | #include "util/parse-events-hybrid.h" |
Jin Yao | 5e4edd1 | 2021-04-27 15:01:25 +0800 | [diff] [blame] | 39 | #include "util/pmu-hybrid.h" |
Jin Yao | 943b69a | 2020-08-14 09:21:20 +0800 | [diff] [blame] | 40 | #include "perf.h" |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 41 | |
| 42 | #define MAX_NAME_LEN 100 |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 43 | |
Jiri Olsa | 82ba1f2 | 2012-05-21 09:12:50 +0200 | [diff] [blame] | 44 | #ifdef PARSER_DEBUG |
| 45 | extern int parse_events_debug; |
| 46 | #endif |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 47 | int parse_events_parse(void *parse_state, void *scanner); |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 48 | static int get_config_terms(struct list_head *head_config, |
| 49 | struct list_head *head_terms __maybe_unused); |
Jin Yao | 5e4edd1 | 2021-04-27 15:01:25 +0800 | [diff] [blame] | 50 | static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, |
| 51 | const char *str, char *pmu_name, |
| 52 | struct list_head *list); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 53 | |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 54 | static struct perf_pmu_event_symbol *perf_pmu_events_list; |
| 55 | /* |
| 56 | * The variable indicates the number of supported pmu event symbols. |
| 57 | * 0 means not initialized and ready to init |
| 58 | * -1 means failed to init, don't try anymore |
| 59 | * >0 is the number of supported pmu event symbols |
| 60 | */ |
| 61 | static int perf_pmu_events_list_num; |
| 62 | |
Yunlong Song | 705750f | 2015-02-27 18:21:27 +0800 | [diff] [blame] | 63 | struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 64 | [PERF_COUNT_HW_CPU_CYCLES] = { |
| 65 | .symbol = "cpu-cycles", |
| 66 | .alias = "cycles", |
| 67 | }, |
| 68 | [PERF_COUNT_HW_INSTRUCTIONS] = { |
| 69 | .symbol = "instructions", |
| 70 | .alias = "", |
| 71 | }, |
| 72 | [PERF_COUNT_HW_CACHE_REFERENCES] = { |
| 73 | .symbol = "cache-references", |
| 74 | .alias = "", |
| 75 | }, |
| 76 | [PERF_COUNT_HW_CACHE_MISSES] = { |
| 77 | .symbol = "cache-misses", |
| 78 | .alias = "", |
| 79 | }, |
| 80 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { |
| 81 | .symbol = "branch-instructions", |
| 82 | .alias = "branches", |
| 83 | }, |
| 84 | [PERF_COUNT_HW_BRANCH_MISSES] = { |
| 85 | .symbol = "branch-misses", |
| 86 | .alias = "", |
| 87 | }, |
| 88 | [PERF_COUNT_HW_BUS_CYCLES] = { |
| 89 | .symbol = "bus-cycles", |
| 90 | .alias = "", |
| 91 | }, |
| 92 | [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { |
| 93 | .symbol = "stalled-cycles-frontend", |
| 94 | .alias = "idle-cycles-frontend", |
| 95 | }, |
| 96 | [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { |
| 97 | .symbol = "stalled-cycles-backend", |
| 98 | .alias = "idle-cycles-backend", |
| 99 | }, |
| 100 | [PERF_COUNT_HW_REF_CPU_CYCLES] = { |
| 101 | .symbol = "ref-cycles", |
| 102 | .alias = "", |
| 103 | }, |
| 104 | }; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 105 | |
Yunlong Song | 705750f | 2015-02-27 18:21:27 +0800 | [diff] [blame] | 106 | struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 107 | [PERF_COUNT_SW_CPU_CLOCK] = { |
| 108 | .symbol = "cpu-clock", |
| 109 | .alias = "", |
| 110 | }, |
| 111 | [PERF_COUNT_SW_TASK_CLOCK] = { |
| 112 | .symbol = "task-clock", |
| 113 | .alias = "", |
| 114 | }, |
| 115 | [PERF_COUNT_SW_PAGE_FAULTS] = { |
| 116 | .symbol = "page-faults", |
| 117 | .alias = "faults", |
| 118 | }, |
| 119 | [PERF_COUNT_SW_CONTEXT_SWITCHES] = { |
| 120 | .symbol = "context-switches", |
| 121 | .alias = "cs", |
| 122 | }, |
| 123 | [PERF_COUNT_SW_CPU_MIGRATIONS] = { |
| 124 | .symbol = "cpu-migrations", |
| 125 | .alias = "migrations", |
| 126 | }, |
| 127 | [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { |
| 128 | .symbol = "minor-faults", |
| 129 | .alias = "", |
| 130 | }, |
| 131 | [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { |
| 132 | .symbol = "major-faults", |
| 133 | .alias = "", |
| 134 | }, |
| 135 | [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { |
| 136 | .symbol = "alignment-faults", |
| 137 | .alias = "", |
| 138 | }, |
| 139 | [PERF_COUNT_SW_EMULATION_FAULTS] = { |
| 140 | .symbol = "emulation-faults", |
| 141 | .alias = "", |
| 142 | }, |
Adrian Hunter | d22d1a2 | 2013-08-31 21:50:52 +0300 | [diff] [blame] | 143 | [PERF_COUNT_SW_DUMMY] = { |
| 144 | .symbol = "dummy", |
| 145 | .alias = "", |
| 146 | }, |
Arnaldo Carvalho de Melo | bae9cc4 | 2015-11-27 15:54:33 -0300 | [diff] [blame] | 147 | [PERF_COUNT_SW_BPF_OUTPUT] = { |
| 148 | .symbol = "bpf-output", |
| 149 | .alias = "", |
| 150 | }, |
Namhyung Kim | fb6c79d | 2021-02-10 17:33:27 +0900 | [diff] [blame] | 151 | [PERF_COUNT_SW_CGROUP_SWITCHES] = { |
| 152 | .symbol = "cgroup-switches", |
| 153 | .alias = "", |
| 154 | }, |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 155 | }; |
| 156 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 157 | #define __PERF_EVENT_FIELD(config, name) \ |
| 158 | ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 159 | |
Ingo Molnar | 1fc570a | 2011-04-27 05:20:22 +0200 | [diff] [blame] | 160 | #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 161 | #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) |
Ingo Molnar | 1fc570a | 2011-04-27 05:20:22 +0200 | [diff] [blame] | 162 | #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 163 | #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 164 | |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 165 | #define for_each_subsystem(sys_dir, sys_dirent) \ |
| 166 | while ((sys_dirent = readdir(sys_dir)) != NULL) \ |
| 167 | if (sys_dirent->d_type == DT_DIR && \ |
| 168 | (strcmp(sys_dirent->d_name, ".")) && \ |
| 169 | (strcmp(sys_dirent->d_name, ".."))) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 170 | |
Arnaldo Carvalho de Melo | c02cab2 | 2018-05-17 14:22:37 -0300 | [diff] [blame] | 171 | static int tp_event_has_id(const char *dir_path, struct dirent *evt_dir) |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame] | 172 | { |
| 173 | char evt_path[MAXPATHLEN]; |
| 174 | int fd; |
| 175 | |
Arnaldo Carvalho de Melo | c02cab2 | 2018-05-17 14:22:37 -0300 | [diff] [blame] | 176 | snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, evt_dir->d_name); |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame] | 177 | fd = open(evt_path, O_RDONLY); |
| 178 | if (fd < 0) |
| 179 | return -EINVAL; |
| 180 | close(fd); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Arnaldo Carvalho de Melo | c02cab2 | 2018-05-17 14:22:37 -0300 | [diff] [blame] | 185 | #define for_each_event(dir_path, evt_dir, evt_dirent) \ |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 186 | while ((evt_dirent = readdir(evt_dir)) != NULL) \ |
| 187 | if (evt_dirent->d_type == DT_DIR && \ |
| 188 | (strcmp(evt_dirent->d_name, ".")) && \ |
| 189 | (strcmp(evt_dirent->d_name, "..")) && \ |
Arnaldo Carvalho de Melo | c02cab2 | 2018-05-17 14:22:37 -0300 | [diff] [blame] | 190 | (!tp_event_has_id(dir_path, evt_dirent))) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 191 | |
Li Zefan | 270bbbe | 2009-09-17 16:34:51 +0800 | [diff] [blame] | 192 | #define MAX_EVENT_LENGTH 512 |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 193 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 194 | struct tracepoint_path *tracepoint_id_to_path(u64 config) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 195 | { |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 196 | struct tracepoint_path *path = NULL; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 197 | DIR *sys_dir, *evt_dir; |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 198 | struct dirent *sys_dirent, *evt_dirent; |
Stephane Eranian | 8aa8a7c | 2012-03-13 16:51:02 +0100 | [diff] [blame] | 199 | char id_buf[24]; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 200 | int fd; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 201 | u64 id; |
| 202 | char evt_path[MAXPATHLEN]; |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 203 | char *dir_path; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 204 | |
Arnaldo Carvalho de Melo | 7014e0e | 2018-05-17 14:42:39 -0300 | [diff] [blame] | 205 | sys_dir = tracing_events__opendir(); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 206 | if (!sys_dir) |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 207 | return NULL; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 208 | |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 209 | for_each_subsystem(sys_dir, sys_dirent) { |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 210 | dir_path = get_events_file(sys_dirent->d_name); |
| 211 | if (!dir_path) |
| 212 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 213 | evt_dir = opendir(dir_path); |
| 214 | if (!evt_dir) |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 215 | goto next; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 216 | |
Arnaldo Carvalho de Melo | c02cab2 | 2018-05-17 14:22:37 -0300 | [diff] [blame] | 217 | for_each_event(dir_path, evt_dir, evt_dirent) { |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 218 | |
Jiri Olsa | 77f1815 | 2018-03-19 09:29:01 +0100 | [diff] [blame] | 219 | scnprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, |
| 220 | evt_dirent->d_name); |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 221 | fd = open(evt_path, O_RDONLY); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 222 | if (fd < 0) |
| 223 | continue; |
| 224 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { |
| 225 | close(fd); |
| 226 | continue; |
| 227 | } |
| 228 | close(fd); |
| 229 | id = atoll(id_buf); |
| 230 | if (id == config) { |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 231 | put_events_file(dir_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 232 | closedir(evt_dir); |
| 233 | closedir(sys_dir); |
Julia Lawall | 59b4cae | 2009-12-06 10:16:30 +0100 | [diff] [blame] | 234 | path = zalloc(sizeof(*path)); |
Taeung Song | 5aa365f | 2017-02-01 21:34:05 +0900 | [diff] [blame] | 235 | if (!path) |
| 236 | return NULL; |
Arnaldo Carvalho de Melo | 7125f20 | 2020-03-02 11:55:47 -0300 | [diff] [blame] | 237 | if (asprintf(&path->system, "%.*s", MAX_EVENT_LENGTH, sys_dirent->d_name) < 0) { |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 238 | free(path); |
| 239 | return NULL; |
| 240 | } |
Arnaldo Carvalho de Melo | 7125f20 | 2020-03-02 11:55:47 -0300 | [diff] [blame] | 241 | if (asprintf(&path->name, "%.*s", MAX_EVENT_LENGTH, evt_dirent->d_name) < 0) { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 242 | zfree(&path->system); |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 243 | free(path); |
| 244 | return NULL; |
| 245 | } |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 246 | return path; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | closedir(evt_dir); |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 250 | next: |
| 251 | put_events_file(dir_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 252 | } |
| 253 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 254 | closedir(sys_dir); |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 255 | return NULL; |
| 256 | } |
| 257 | |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 258 | struct tracepoint_path *tracepoint_name_to_path(const char *name) |
| 259 | { |
| 260 | struct tracepoint_path *path = zalloc(sizeof(*path)); |
| 261 | char *str = strchr(name, ':'); |
| 262 | |
| 263 | if (path == NULL || str == NULL) { |
| 264 | free(path); |
| 265 | return NULL; |
| 266 | } |
| 267 | |
| 268 | path->system = strndup(name, str - name); |
| 269 | path->name = strdup(str+1); |
| 270 | |
| 271 | if (path->system == NULL || path->name == NULL) { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 272 | zfree(&path->system); |
| 273 | zfree(&path->name); |
Taeung Song | 506fde1 | 2017-02-01 21:34:06 +0900 | [diff] [blame] | 274 | zfree(&path); |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | return path; |
| 278 | } |
| 279 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 280 | const char *event_type(int type) |
| 281 | { |
| 282 | switch (type) { |
| 283 | case PERF_TYPE_HARDWARE: |
| 284 | return "hardware"; |
| 285 | |
| 286 | case PERF_TYPE_SOFTWARE: |
| 287 | return "software"; |
| 288 | |
| 289 | case PERF_TYPE_TRACEPOINT: |
| 290 | return "tracepoint"; |
| 291 | |
| 292 | case PERF_TYPE_HW_CACHE: |
| 293 | return "hardware-cache"; |
| 294 | |
| 295 | default: |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | return "unknown"; |
| 300 | } |
| 301 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 302 | static char *get_config_str(struct list_head *head_terms, int type_term) |
Wang Nan | e814fdd | 2016-02-19 11:43:59 +0000 | [diff] [blame] | 303 | { |
| 304 | struct parse_events_term *term; |
| 305 | |
| 306 | if (!head_terms) |
| 307 | return NULL; |
| 308 | |
| 309 | list_for_each_entry(term, head_terms, list) |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 310 | if (term->type_term == type_term) |
Wang Nan | e814fdd | 2016-02-19 11:43:59 +0000 | [diff] [blame] | 311 | return term->val.str; |
| 312 | |
| 313 | return NULL; |
| 314 | } |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 315 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 316 | static char *get_config_metric_id(struct list_head *head_terms) |
| 317 | { |
| 318 | return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_METRIC_ID); |
| 319 | } |
| 320 | |
| 321 | static char *get_config_name(struct list_head *head_terms) |
| 322 | { |
| 323 | return get_config_str(head_terms, PARSE_EVENTS__TERM_TYPE_NAME); |
| 324 | } |
| 325 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 326 | static struct evsel * |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 327 | __add_event(struct list_head *list, int *idx, |
| 328 | struct perf_event_attr *attr, |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 329 | bool init_attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 330 | const char *name, const char *metric_id, struct perf_pmu *pmu, |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 331 | struct list_head *config_terms, bool auto_merge_stats, |
| 332 | const char *cpu_list) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 333 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 334 | struct evsel *evsel; |
Namhyung Kim | 5d680be | 2020-09-17 15:02:19 +0900 | [diff] [blame] | 335 | struct perf_cpu_map *cpus = pmu ? perf_cpu_map__get(pmu->cpus) : |
Jiri Olsa | 9c3516d | 2019-07-21 13:24:30 +0200 | [diff] [blame] | 336 | cpu_list ? perf_cpu_map__new(cpu_list) : NULL; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 337 | |
Jin Yao | e406477 | 2021-03-10 13:11:38 +0800 | [diff] [blame] | 338 | if (pmu && attr->type == PERF_TYPE_RAW) |
| 339 | perf_pmu__warn_invalid_config(pmu, attr->config, name); |
| 340 | |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 341 | if (init_attr) |
| 342 | event_attr_init(attr); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 343 | |
Arnaldo Carvalho de Melo | 8f6725a | 2020-05-06 13:27:04 -0300 | [diff] [blame] | 344 | evsel = evsel__new_idx(attr, *idx); |
Namhyung Kim | 0f1b550 | 2020-09-17 15:02:18 +0900 | [diff] [blame] | 345 | if (!evsel) { |
| 346 | perf_cpu_map__put(cpus); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 347 | return NULL; |
Namhyung Kim | 0f1b550 | 2020-09-17 15:02:18 +0900 | [diff] [blame] | 348 | } |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 349 | |
Taeung Song | 75fc5ae | 2017-02-01 21:34:04 +0900 | [diff] [blame] | 350 | (*idx)++; |
Namhyung Kim | 5d680be | 2020-09-17 15:02:19 +0900 | [diff] [blame] | 351 | evsel->core.cpus = cpus; |
Jiri Olsa | fe1f61b | 2019-07-21 13:24:38 +0200 | [diff] [blame] | 352 | evsel->core.own_cpus = perf_cpu_map__get(cpus); |
Jiri Olsa | 648b5af | 2019-08-06 11:35:19 +0200 | [diff] [blame] | 353 | evsel->core.system_wide = pmu ? pmu->is_uncore : false; |
Arnaldo Carvalho de Melo | 63ce844 | 2017-08-31 15:32:18 -0300 | [diff] [blame] | 354 | evsel->auto_merge_stats = auto_merge_stats; |
Jiri Olsa | f30a79b | 2015-06-23 00:36:04 +0200 | [diff] [blame] | 355 | |
Arnaldo Carvalho de Melo | 9db1763 | 2012-06-12 13:45:00 -0300 | [diff] [blame] | 356 | if (name) |
| 357 | evsel->name = strdup(name); |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 358 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 359 | if (metric_id) |
| 360 | evsel->metric_id = strdup(metric_id); |
| 361 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 362 | if (config_terms) |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 363 | list_splice_init(config_terms, &evsel->config_terms); |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 364 | |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 365 | if (list) |
| 366 | list_add_tail(&evsel->core.node, list); |
| 367 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 368 | return evsel; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 369 | } |
| 370 | |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 371 | struct evsel *parse_events__add_event(int idx, struct perf_event_attr *attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 372 | const char *name, const char *metric_id, |
| 373 | struct perf_pmu *pmu) |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 374 | { |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 375 | return __add_event(/*list=*/NULL, &idx, attr, /*init_attr=*/false, name, |
| 376 | metric_id, pmu, /*config_terms=*/NULL, |
| 377 | /*auto_merge_stats=*/false, /*cpu_list=*/NULL); |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 378 | } |
| 379 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 380 | static int add_event(struct list_head *list, int *idx, |
Ian Rogers | 8e8bbfb | 2021-10-15 10:21:24 -0700 | [diff] [blame] | 381 | struct perf_event_attr *attr, const char *name, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 382 | const char *metric_id, struct list_head *config_terms) |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 383 | { |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 384 | return __add_event(list, idx, attr, /*init_attr*/true, name, metric_id, |
| 385 | /*pmu=*/NULL, config_terms, |
| 386 | /*auto_merge_stats=*/false, /*cpu_list=*/NULL) ? 0 : -ENOMEM; |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static int add_event_tool(struct list_head *list, int *idx, |
| 390 | enum perf_tool_event tool_event) |
| 391 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 392 | struct evsel *evsel; |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 393 | struct perf_event_attr attr = { |
| 394 | .type = PERF_TYPE_SOFTWARE, |
| 395 | .config = PERF_COUNT_SW_DUMMY, |
| 396 | }; |
| 397 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 398 | evsel = __add_event(list, idx, &attr, /*init_attr=*/true, /*name=*/NULL, |
| 399 | /*metric_id=*/NULL, /*pmu=*/NULL, |
| 400 | /*config_terms=*/NULL, /*auto_merge_stats=*/false, |
| 401 | /*cpu_list=*/"0"); |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 402 | if (!evsel) |
| 403 | return -ENOMEM; |
| 404 | evsel->tool_event = tool_event; |
Ian Rogers | b194c9c | 2021-11-18 00:47:49 -0800 | [diff] [blame] | 405 | if (tool_event == PERF_TOOL_DURATION_TIME) { |
| 406 | free((char *)evsel->unit); |
| 407 | evsel->unit = strdup("ns"); |
| 408 | } |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 409 | return 0; |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 410 | } |
| 411 | |
Arnaldo Carvalho de Melo | c64e85e | 2020-05-06 13:32:23 -0300 | [diff] [blame] | 412 | static int parse_aliases(char *str, const char *names[][EVSEL__MAX_ALIASES], int size) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 413 | { |
| 414 | int i, j; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 415 | int n, longest = -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 416 | |
| 417 | for (i = 0; i < size; i++) { |
Arnaldo Carvalho de Melo | c64e85e | 2020-05-06 13:32:23 -0300 | [diff] [blame] | 418 | for (j = 0; j < EVSEL__MAX_ALIASES && names[i][j]; j++) { |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 419 | n = strlen(names[i][j]); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 420 | if (n > longest && !strncasecmp(str, names[i][j], n)) |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 421 | longest = n; |
| 422 | } |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 423 | if (longest > 0) |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 424 | return i; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Ingo Molnar | 8953645 | 2009-06-06 21:04:17 +0200 | [diff] [blame] | 427 | return -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 428 | } |
| 429 | |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 430 | typedef int config_term_func_t(struct perf_event_attr *attr, |
| 431 | struct parse_events_term *term, |
| 432 | struct parse_events_error *err); |
| 433 | static int config_term_common(struct perf_event_attr *attr, |
| 434 | struct parse_events_term *term, |
| 435 | struct parse_events_error *err); |
| 436 | static int config_attr(struct perf_event_attr *attr, |
| 437 | struct list_head *head, |
| 438 | struct parse_events_error *err, |
| 439 | config_term_func_t config_term); |
| 440 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 441 | int parse_events_add_cache(struct list_head *list, int *idx, |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 442 | char *type, char *op_result1, char *op_result2, |
Arnaldo Carvalho de Melo | 2c97b0d | 2016-02-19 19:47:04 -0300 | [diff] [blame] | 443 | struct parse_events_error *err, |
Jin Yao | c93afad | 2021-04-27 15:01:24 +0800 | [diff] [blame] | 444 | struct list_head *head_config, |
| 445 | struct parse_events_state *parse_state) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 446 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 447 | struct perf_event_attr attr; |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 448 | LIST_HEAD(config_terms); |
Ian Rogers | 8e8bbfb | 2021-10-15 10:21:24 -0700 | [diff] [blame] | 449 | char name[MAX_NAME_LEN]; |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 450 | const char *config_name, *metric_id; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 451 | int cache_type = -1, cache_op = -1, cache_result = -1; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 452 | char *op_result[2] = { op_result1, op_result2 }; |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 453 | int i, n, ret; |
| 454 | bool hybrid; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 455 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 456 | /* |
| 457 | * No fallback - if we cannot get a clear cache type |
| 458 | * then bail out: |
| 459 | */ |
Arnaldo Carvalho de Melo | c64e85e | 2020-05-06 13:32:23 -0300 | [diff] [blame] | 460 | cache_type = parse_aliases(type, evsel__hw_cache, PERF_COUNT_HW_CACHE_MAX); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 461 | if (cache_type == -1) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 462 | return -EINVAL; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 463 | |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 464 | config_name = get_config_name(head_config); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 465 | n = snprintf(name, MAX_NAME_LEN, "%s", type); |
| 466 | |
| 467 | for (i = 0; (i < 2) && (op_result[i]); i++) { |
| 468 | char *str = op_result[i]; |
| 469 | |
Jiri Olsa | 275ef38 | 2012-09-05 19:51:33 +0200 | [diff] [blame] | 470 | n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 471 | |
| 472 | if (cache_op == -1) { |
Arnaldo Carvalho de Melo | c64e85e | 2020-05-06 13:32:23 -0300 | [diff] [blame] | 473 | cache_op = parse_aliases(str, evsel__hw_cache_op, |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 474 | PERF_COUNT_HW_CACHE_OP_MAX); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 475 | if (cache_op >= 0) { |
Arnaldo Carvalho de Melo | c754c38 | 2020-04-30 10:51:16 -0300 | [diff] [blame] | 476 | if (!evsel__is_cache_op_valid(cache_type, cache_op)) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 477 | return -EINVAL; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 478 | continue; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (cache_result == -1) { |
Arnaldo Carvalho de Melo | c64e85e | 2020-05-06 13:32:23 -0300 | [diff] [blame] | 483 | cache_result = parse_aliases(str, evsel__hw_cache_result, |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 484 | PERF_COUNT_HW_CACHE_RESULT_MAX); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 485 | if (cache_result >= 0) |
| 486 | continue; |
| 487 | } |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 488 | } |
| 489 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 490 | /* |
| 491 | * Fall back to reads: |
| 492 | */ |
Ingo Molnar | 8953645 | 2009-06-06 21:04:17 +0200 | [diff] [blame] | 493 | if (cache_op == -1) |
| 494 | cache_op = PERF_COUNT_HW_CACHE_OP_READ; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 495 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 496 | /* |
| 497 | * Fall back to accesses: |
| 498 | */ |
| 499 | if (cache_result == -1) |
| 500 | cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; |
| 501 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 502 | memset(&attr, 0, sizeof(attr)); |
| 503 | attr.config = cache_type | (cache_op << 8) | (cache_result << 16); |
| 504 | attr.type = PERF_TYPE_HW_CACHE; |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 505 | |
| 506 | if (head_config) { |
Arnaldo Carvalho de Melo | 2c97b0d | 2016-02-19 19:47:04 -0300 | [diff] [blame] | 507 | if (config_attr(&attr, head_config, err, |
Wang Nan | 43d0b97 | 2016-02-19 11:44:01 +0000 | [diff] [blame] | 508 | config_term_common)) |
| 509 | return -EINVAL; |
| 510 | |
| 511 | if (get_config_terms(head_config, &config_terms)) |
| 512 | return -ENOMEM; |
| 513 | } |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 514 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 515 | metric_id = get_config_metric_id(head_config); |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 516 | ret = parse_events__add_cache_hybrid(list, idx, &attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 517 | config_name ? : name, |
| 518 | metric_id, |
| 519 | &config_terms, |
Jin Yao | c93afad | 2021-04-27 15:01:24 +0800 | [diff] [blame] | 520 | &hybrid, parse_state); |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 521 | if (hybrid) |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 522 | goto out_free_terms; |
Jin Yao | 30def61 | 2021-04-27 15:01:22 +0800 | [diff] [blame] | 523 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 524 | ret = add_event(list, idx, &attr, config_name ? : name, metric_id, |
| 525 | &config_terms); |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 526 | out_free_terms: |
| 527 | free_config_terms(&config_terms); |
| 528 | return ret; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 531 | static void tracepoint_error(struct parse_events_error *e, int err, |
Wang Nan | 8c619d6 | 2016-07-13 10:44:03 +0000 | [diff] [blame] | 532 | const char *sys, const char *name) |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 533 | { |
Ian Rogers | 4584f08 | 2019-11-20 10:09:25 -0800 | [diff] [blame] | 534 | const char *str; |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 535 | char help[BUFSIZ]; |
| 536 | |
Adrian Hunter | ec183d2 | 2016-01-26 14:05:20 +0200 | [diff] [blame] | 537 | if (!e) |
| 538 | return; |
| 539 | |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 540 | /* |
| 541 | * We get error directly from syscall errno ( > 0), |
| 542 | * or from encoded pointer's error ( < 0). |
| 543 | */ |
| 544 | err = abs(err); |
| 545 | |
| 546 | switch (err) { |
| 547 | case EACCES: |
Ian Rogers | 4584f08 | 2019-11-20 10:09:25 -0800 | [diff] [blame] | 548 | str = "can't access trace events"; |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 549 | break; |
| 550 | case ENOENT: |
Ian Rogers | 4584f08 | 2019-11-20 10:09:25 -0800 | [diff] [blame] | 551 | str = "unknown tracepoint"; |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 552 | break; |
| 553 | default: |
Ian Rogers | 4584f08 | 2019-11-20 10:09:25 -0800 | [diff] [blame] | 554 | str = "failed to add tracepoint"; |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | |
| 558 | tracing_path__strerror_open_tp(err, help, sizeof(help), sys, name); |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 559 | parse_events_error__handle(e, 0, strdup(str), strdup(help)); |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 560 | } |
| 561 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 562 | static int add_tracepoint(struct list_head *list, int *idx, |
Wang Nan | 8c619d6 | 2016-07-13 10:44:03 +0000 | [diff] [blame] | 563 | const char *sys_name, const char *evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 564 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 565 | struct list_head *head_config) |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 566 | { |
Arnaldo Carvalho de Melo | 8f6725a | 2020-05-06 13:27:04 -0300 | [diff] [blame] | 567 | struct evsel *evsel = evsel__newtp_idx(sys_name, evt_name, (*idx)++); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 568 | |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 569 | if (IS_ERR(evsel)) { |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 570 | tracepoint_error(err, PTR_ERR(evsel), sys_name, evt_name); |
Jiri Olsa | 8dd2a13 | 2015-09-07 10:38:06 +0200 | [diff] [blame] | 571 | return PTR_ERR(evsel); |
Jiri Olsa | 1965817 | 2015-09-07 10:38:07 +0200 | [diff] [blame] | 572 | } |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 573 | |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 574 | if (head_config) { |
| 575 | LIST_HEAD(config_terms); |
| 576 | |
| 577 | if (get_config_terms(head_config, &config_terms)) |
| 578 | return -ENOMEM; |
| 579 | list_splice(&config_terms, &evsel->config_terms); |
| 580 | } |
| 581 | |
Jiri Olsa | b27c4ec | 2019-07-21 13:24:22 +0200 | [diff] [blame] | 582 | list_add_tail(&evsel->core.node, list); |
Arnaldo Carvalho de Melo | 82fe1c2 | 2012-09-26 17:13:07 -0300 | [diff] [blame] | 583 | return 0; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 584 | } |
| 585 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 586 | static int add_tracepoint_multi_event(struct list_head *list, int *idx, |
Wang Nan | 8c619d6 | 2016-07-13 10:44:03 +0000 | [diff] [blame] | 587 | const char *sys_name, const char *evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 588 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 589 | struct list_head *head_config) |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 590 | { |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 591 | char *evt_path; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 592 | struct dirent *evt_ent; |
| 593 | DIR *evt_dir; |
Jiri Olsa | 27bf90b | 2015-10-05 21:31:17 +0200 | [diff] [blame] | 594 | int ret = 0, found = 0; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 595 | |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 596 | evt_path = get_events_file(sys_name); |
| 597 | if (!evt_path) { |
| 598 | tracepoint_error(err, errno, sys_name, evt_name); |
| 599 | return -1; |
| 600 | } |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 601 | evt_dir = opendir(evt_path); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 602 | if (!evt_dir) { |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 603 | put_events_file(evt_path); |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 604 | tracepoint_error(err, errno, sys_name, evt_name); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 605 | return -1; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 606 | } |
| 607 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 608 | while (!ret && (evt_ent = readdir(evt_dir))) { |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 609 | if (!strcmp(evt_ent->d_name, ".") |
| 610 | || !strcmp(evt_ent->d_name, "..") |
| 611 | || !strcmp(evt_ent->d_name, "enable") |
| 612 | || !strcmp(evt_ent->d_name, "filter")) |
| 613 | continue; |
| 614 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 615 | if (!strglobmatch(evt_ent->d_name, evt_name)) |
Masami Hiramatsu | fb1d2ed | 2010-01-05 17:47:17 -0500 | [diff] [blame] | 616 | continue; |
| 617 | |
Jiri Olsa | 27bf90b | 2015-10-05 21:31:17 +0200 | [diff] [blame] | 618 | found++; |
| 619 | |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 620 | ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 621 | err, head_config); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Jiri Olsa | 27bf90b | 2015-10-05 21:31:17 +0200 | [diff] [blame] | 624 | if (!found) { |
| 625 | tracepoint_error(err, ENOENT, sys_name, evt_name); |
| 626 | ret = -1; |
| 627 | } |
| 628 | |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 629 | put_events_file(evt_path); |
Jiri Olsa | 0bd3f08 | 2012-12-17 14:08:36 +0100 | [diff] [blame] | 630 | closedir(evt_dir); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 631 | return ret; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 632 | } |
| 633 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 634 | static int add_tracepoint_event(struct list_head *list, int *idx, |
Wang Nan | 8c619d6 | 2016-07-13 10:44:03 +0000 | [diff] [blame] | 635 | const char *sys_name, const char *evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 636 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 637 | struct list_head *head_config) |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 638 | { |
| 639 | return strpbrk(evt_name, "*?") ? |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 640 | add_tracepoint_multi_event(list, idx, sys_name, evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 641 | err, head_config) : |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 642 | add_tracepoint(list, idx, sys_name, evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 643 | err, head_config); |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 644 | } |
| 645 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 646 | static int add_tracepoint_multi_sys(struct list_head *list, int *idx, |
Wang Nan | 8c619d6 | 2016-07-13 10:44:03 +0000 | [diff] [blame] | 647 | const char *sys_name, const char *evt_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 648 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 649 | struct list_head *head_config) |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 650 | { |
| 651 | struct dirent *events_ent; |
| 652 | DIR *events_dir; |
| 653 | int ret = 0; |
| 654 | |
Arnaldo Carvalho de Melo | 7014e0e | 2018-05-17 14:42:39 -0300 | [diff] [blame] | 655 | events_dir = tracing_events__opendir(); |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 656 | if (!events_dir) { |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 657 | tracepoint_error(err, errno, sys_name, evt_name); |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 658 | return -1; |
| 659 | } |
| 660 | |
| 661 | while (!ret && (events_ent = readdir(events_dir))) { |
| 662 | if (!strcmp(events_ent->d_name, ".") |
| 663 | || !strcmp(events_ent->d_name, "..") |
| 664 | || !strcmp(events_ent->d_name, "enable") |
| 665 | || !strcmp(events_ent->d_name, "header_event") |
| 666 | || !strcmp(events_ent->d_name, "header_page")) |
| 667 | continue; |
| 668 | |
| 669 | if (!strglobmatch(events_ent->d_name, sys_name)) |
| 670 | continue; |
| 671 | |
| 672 | ret = add_tracepoint_event(list, idx, events_ent->d_name, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 673 | evt_name, err, head_config); |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | closedir(events_dir); |
| 677 | return ret; |
| 678 | } |
| 679 | |
Arnaldo Carvalho de Melo | c18cf78 | 2020-10-20 14:12:37 -0300 | [diff] [blame] | 680 | #ifdef HAVE_LIBBPF_SUPPORT |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 681 | struct __add_bpf_event_param { |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 682 | struct parse_events_state *parse_state; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 683 | struct list_head *list; |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 684 | struct list_head *head_config; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 685 | }; |
| 686 | |
Arnaldo Carvalho de Melo | af4a099 | 2019-07-15 16:22:57 -0300 | [diff] [blame] | 687 | static int add_bpf_event(const char *group, const char *event, int fd, struct bpf_object *obj, |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 688 | void *_param) |
| 689 | { |
| 690 | LIST_HEAD(new_evsels); |
| 691 | struct __add_bpf_event_param *param = _param; |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 692 | struct parse_events_state *parse_state = param->parse_state; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 693 | struct list_head *list = param->list; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 694 | struct evsel *pos; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 695 | int err; |
Arnaldo Carvalho de Melo | 2620b7e | 2019-07-15 15:29:34 -0300 | [diff] [blame] | 696 | /* |
| 697 | * Check if we should add the event, i.e. if it is a TP but starts with a '!', |
| 698 | * then don't add the tracepoint, this will be used for something else, like |
| 699 | * adding to a BPF_MAP_TYPE_PROG_ARRAY. |
| 700 | * |
| 701 | * See tools/perf/examples/bpf/augmented_raw_syscalls.c |
| 702 | */ |
| 703 | if (group[0] == '!') |
| 704 | return 0; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 705 | |
| 706 | pr_debug("add bpf event %s:%s and attach bpf program %d\n", |
Wang Nan | cd102d7 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 707 | group, event, fd); |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 708 | |
Arnaldo Carvalho de Melo | 5d369a7 | 2017-08-17 14:13:25 -0300 | [diff] [blame] | 709 | err = parse_events_add_tracepoint(&new_evsels, &parse_state->idx, group, |
| 710 | event, parse_state->error, |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 711 | param->head_config); |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 712 | if (err) { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 713 | struct evsel *evsel, *tmp; |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 714 | |
| 715 | pr_debug("Failed to add BPF event %s:%s\n", |
Wang Nan | cd102d7 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 716 | group, event); |
Jiri Olsa | b27c4ec | 2019-07-21 13:24:22 +0200 | [diff] [blame] | 717 | list_for_each_entry_safe(evsel, tmp, &new_evsels, core.node) { |
| 718 | list_del_init(&evsel->core.node); |
Jiri Olsa | 5eb2dd2 | 2019-07-21 13:23:57 +0200 | [diff] [blame] | 719 | evsel__delete(evsel); |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 720 | } |
| 721 | return err; |
| 722 | } |
Wang Nan | cd102d7 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 723 | pr_debug("adding %s:%s\n", group, event); |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 724 | |
Jiri Olsa | b27c4ec | 2019-07-21 13:24:22 +0200 | [diff] [blame] | 725 | list_for_each_entry(pos, &new_evsels, core.node) { |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 726 | pr_debug("adding %s:%s to %p\n", |
Wang Nan | cd102d7 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 727 | group, event, pos); |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 728 | pos->bpf_fd = fd; |
Arnaldo Carvalho de Melo | af4a099 | 2019-07-15 16:22:57 -0300 | [diff] [blame] | 729 | pos->bpf_obj = obj; |
Wang Nan | 1f45b1d | 2015-10-14 12:41:18 +0000 | [diff] [blame] | 730 | } |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 731 | list_splice(&new_evsels, list); |
| 732 | return 0; |
| 733 | } |
| 734 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 735 | int parse_events_load_bpf_obj(struct parse_events_state *parse_state, |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 736 | struct list_head *list, |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 737 | struct bpf_object *obj, |
| 738 | struct list_head *head_config) |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 739 | { |
| 740 | int err; |
| 741 | char errbuf[BUFSIZ]; |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 742 | struct __add_bpf_event_param param = {parse_state, list, head_config}; |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 743 | static bool registered_unprobe_atexit = false; |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 744 | |
| 745 | if (IS_ERR(obj) || !obj) { |
| 746 | snprintf(errbuf, sizeof(errbuf), |
| 747 | "Internal error: load bpf obj with NULL"); |
| 748 | err = -EINVAL; |
| 749 | goto errout; |
| 750 | } |
| 751 | |
| 752 | /* |
Wang Nan | aa3abf3 | 2015-10-14 12:41:15 +0000 | [diff] [blame] | 753 | * Register atexit handler before calling bpf__probe() so |
| 754 | * bpf__probe() don't need to unprobe probe points its already |
| 755 | * created when failure. |
| 756 | */ |
| 757 | if (!registered_unprobe_atexit) { |
| 758 | atexit(bpf__clear); |
| 759 | registered_unprobe_atexit = true; |
| 760 | } |
| 761 | |
| 762 | err = bpf__probe(obj); |
| 763 | if (err) { |
| 764 | bpf__strerror_probe(obj, err, errbuf, sizeof(errbuf)); |
| 765 | goto errout; |
| 766 | } |
| 767 | |
Wang Nan | 1e5e3ee | 2015-10-14 12:41:16 +0000 | [diff] [blame] | 768 | err = bpf__load(obj); |
| 769 | if (err) { |
| 770 | bpf__strerror_load(obj, err, errbuf, sizeof(errbuf)); |
| 771 | goto errout; |
| 772 | } |
| 773 | |
Wang Nan | cd102d7 | 2016-07-13 10:44:04 +0000 | [diff] [blame] | 774 | err = bpf__foreach_event(obj, add_bpf_event, ¶m); |
Wang Nan | 4edf30e | 2015-10-14 12:41:17 +0000 | [diff] [blame] | 775 | if (err) { |
| 776 | snprintf(errbuf, sizeof(errbuf), |
| 777 | "Attach events in BPF object failed"); |
| 778 | goto errout; |
| 779 | } |
| 780 | |
| 781 | return 0; |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 782 | errout: |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 783 | parse_events_error__handle(parse_state->error, 0, |
Ian Rogers | 5f634c8e | 2020-07-07 14:14:49 -0700 | [diff] [blame] | 784 | strdup(errbuf), strdup("(add -v to see detail)")); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 785 | return err; |
| 786 | } |
| 787 | |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 788 | static int |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 789 | parse_events_config_bpf(struct parse_events_state *parse_state, |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 790 | struct bpf_object *obj, |
| 791 | struct list_head *head_config) |
| 792 | { |
| 793 | struct parse_events_term *term; |
| 794 | int error_pos; |
| 795 | |
| 796 | if (!head_config || list_empty(head_config)) |
| 797 | return 0; |
| 798 | |
| 799 | list_for_each_entry(term, head_config, list) { |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 800 | int err; |
| 801 | |
| 802 | if (term->type_term != PARSE_EVENTS__TERM_TYPE_USER) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 803 | parse_events_error__handle(parse_state->error, term->err_term, |
Ian Rogers | 5f634c8e | 2020-07-07 14:14:49 -0700 | [diff] [blame] | 804 | strdup("Invalid config term for BPF object"), |
| 805 | NULL); |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 806 | return -EINVAL; |
| 807 | } |
| 808 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 809 | err = bpf__config_obj(obj, term, parse_state->evlist, &error_pos); |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 810 | if (err) { |
Ian Rogers | 5f634c8e | 2020-07-07 14:14:49 -0700 | [diff] [blame] | 811 | char errbuf[BUFSIZ]; |
| 812 | int idx; |
| 813 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 814 | bpf__strerror_config_obj(obj, term, parse_state->evlist, |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 815 | &error_pos, err, errbuf, |
| 816 | sizeof(errbuf)); |
Ian Rogers | 5f634c8e | 2020-07-07 14:14:49 -0700 | [diff] [blame] | 817 | |
| 818 | if (err == -BPF_LOADER_ERRNO__OBJCONF_MAP_VALUE) |
| 819 | idx = term->err_val; |
| 820 | else |
| 821 | idx = term->err_term + error_pos; |
| 822 | |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 823 | parse_events_error__handle(parse_state->error, idx, |
Ian Rogers | 5f634c8e | 2020-07-07 14:14:49 -0700 | [diff] [blame] | 824 | strdup(errbuf), |
| 825 | strdup( |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 826 | "Hint:\tValid config terms:\n" |
Wang Nan | e571e02 | 2016-02-22 09:10:35 +0000 | [diff] [blame] | 827 | " \tmap:[<arraymap>].value<indices>=[value]\n" |
| 828 | " \tmap:[<eventmap>].event<indices>=[event]\n" |
Wang Nan | 7630b3e | 2016-02-22 09:10:33 +0000 | [diff] [blame] | 829 | "\n" |
Wang Nan | e571e02 | 2016-02-22 09:10:35 +0000 | [diff] [blame] | 830 | " \twhere <indices> is something like [0,3...5] or [all]\n" |
Ian Rogers | 5f634c8e | 2020-07-07 14:14:49 -0700 | [diff] [blame] | 831 | " \t(add -v to see detail)")); |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 832 | return err; |
| 833 | } |
| 834 | } |
| 835 | return 0; |
| 836 | } |
| 837 | |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 838 | /* |
| 839 | * Split config terms: |
| 840 | * perf record -e bpf.c/call-graph=fp,map:array.value[0]=1/ ... |
| 841 | * 'call-graph=fp' is 'evt config', should be applied to each |
| 842 | * events in bpf.c. |
| 843 | * 'map:array.value[0]=1' is 'obj config', should be processed |
| 844 | * with parse_events_config_bpf. |
| 845 | * |
| 846 | * Move object config terms from the first list to obj_head_config. |
| 847 | */ |
| 848 | static void |
| 849 | split_bpf_config_terms(struct list_head *evt_head_config, |
| 850 | struct list_head *obj_head_config) |
| 851 | { |
| 852 | struct parse_events_term *term, *temp; |
| 853 | |
| 854 | /* |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 855 | * Currently, all possible user config term |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 856 | * belong to bpf object. parse_events__is_hardcoded_term() |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 857 | * happens to be a good flag. |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 858 | * |
| 859 | * See parse_events_config_bpf() and |
| 860 | * config_term_tracepoint(). |
| 861 | */ |
| 862 | list_for_each_entry_safe(term, temp, evt_head_config, list) |
| 863 | if (!parse_events__is_hardcoded_term(term)) |
| 864 | list_move_tail(&term->list, obj_head_config); |
| 865 | } |
| 866 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 867 | int parse_events_load_bpf(struct parse_events_state *parse_state, |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 868 | struct list_head *list, |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 869 | char *bpf_file_name, |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 870 | bool source, |
| 871 | struct list_head *head_config) |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 872 | { |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 873 | int err; |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 874 | struct bpf_object *obj; |
| 875 | LIST_HEAD(obj_head_config); |
| 876 | |
| 877 | if (head_config) |
| 878 | split_bpf_config_terms(head_config, &obj_head_config); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 879 | |
Wang Nan | d509db0 | 2015-10-14 12:41:20 +0000 | [diff] [blame] | 880 | obj = bpf__prepare_load(bpf_file_name, source); |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 881 | if (IS_ERR(obj)) { |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 882 | char errbuf[BUFSIZ]; |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 883 | |
Wang Nan | 6371ca3b | 2015-11-06 13:49:37 +0000 | [diff] [blame] | 884 | err = PTR_ERR(obj); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 885 | |
| 886 | if (err == -ENOTSUP) |
| 887 | snprintf(errbuf, sizeof(errbuf), |
| 888 | "BPF support is not compiled"); |
| 889 | else |
Wang Nan | d3e0ce3 | 2015-11-06 13:58:09 +0000 | [diff] [blame] | 890 | bpf__strerror_prepare_load(bpf_file_name, |
| 891 | source, |
| 892 | -err, errbuf, |
| 893 | sizeof(errbuf)); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 894 | |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 895 | parse_events_error__handle(parse_state->error, 0, |
Ian Rogers | 5f634c8e | 2020-07-07 14:14:49 -0700 | [diff] [blame] | 896 | strdup(errbuf), strdup("(add -v to see detail)")); |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 897 | return err; |
| 898 | } |
| 899 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 900 | err = parse_events_load_bpf_obj(parse_state, list, obj, head_config); |
Wang Nan | a34f3be | 2016-02-22 09:10:31 +0000 | [diff] [blame] | 901 | if (err) |
| 902 | return err; |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 903 | err = parse_events_config_bpf(parse_state, obj, &obj_head_config); |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 904 | |
| 905 | /* |
| 906 | * Caller doesn't know anything about obj_head_config, |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 907 | * so combine them together again before returning. |
Wang Nan | 95088a5 | 2016-02-22 09:10:36 +0000 | [diff] [blame] | 908 | */ |
| 909 | if (head_config) |
| 910 | list_splice_tail(&obj_head_config, head_config); |
| 911 | return err; |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 912 | } |
Arnaldo Carvalho de Melo | c18cf78 | 2020-10-20 14:12:37 -0300 | [diff] [blame] | 913 | #else // HAVE_LIBBPF_SUPPORT |
| 914 | int parse_events_load_bpf_obj(struct parse_events_state *parse_state, |
| 915 | struct list_head *list __maybe_unused, |
| 916 | struct bpf_object *obj __maybe_unused, |
| 917 | struct list_head *head_config __maybe_unused) |
| 918 | { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 919 | parse_events_error__handle(parse_state->error, 0, |
Arnaldo Carvalho de Melo | c18cf78 | 2020-10-20 14:12:37 -0300 | [diff] [blame] | 920 | strdup("BPF support is not compiled"), |
| 921 | strdup("Make sure libbpf-devel is available at build time.")); |
| 922 | return -ENOTSUP; |
| 923 | } |
| 924 | |
| 925 | int parse_events_load_bpf(struct parse_events_state *parse_state, |
| 926 | struct list_head *list __maybe_unused, |
| 927 | char *bpf_file_name __maybe_unused, |
| 928 | bool source __maybe_unused, |
| 929 | struct list_head *head_config __maybe_unused) |
| 930 | { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 931 | parse_events_error__handle(parse_state->error, 0, |
Arnaldo Carvalho de Melo | c18cf78 | 2020-10-20 14:12:37 -0300 | [diff] [blame] | 932 | strdup("BPF support is not compiled"), |
| 933 | strdup("Make sure libbpf-devel is available at build time.")); |
| 934 | return -ENOTSUP; |
| 935 | } |
| 936 | #endif // HAVE_LIBBPF_SUPPORT |
Wang Nan | 84c86ca | 2015-10-14 12:41:14 +0000 | [diff] [blame] | 937 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 938 | static int |
| 939 | parse_breakpoint_type(const char *type, struct perf_event_attr *attr) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 940 | { |
| 941 | int i; |
| 942 | |
| 943 | for (i = 0; i < 3; i++) { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 944 | if (!type || !type[i]) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 945 | break; |
| 946 | |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 947 | #define CHECK_SET_TYPE(bit) \ |
| 948 | do { \ |
| 949 | if (attr->bp_type & bit) \ |
| 950 | return -EINVAL; \ |
| 951 | else \ |
| 952 | attr->bp_type |= bit; \ |
| 953 | } while (0) |
| 954 | |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 955 | switch (type[i]) { |
| 956 | case 'r': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 957 | CHECK_SET_TYPE(HW_BREAKPOINT_R); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 958 | break; |
| 959 | case 'w': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 960 | CHECK_SET_TYPE(HW_BREAKPOINT_W); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 961 | break; |
| 962 | case 'x': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 963 | CHECK_SET_TYPE(HW_BREAKPOINT_X); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 964 | break; |
| 965 | default: |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 966 | return -EINVAL; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 967 | } |
| 968 | } |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 969 | |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 970 | #undef CHECK_SET_TYPE |
| 971 | |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 972 | if (!attr->bp_type) /* Default */ |
| 973 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; |
| 974 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 975 | return 0; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 976 | } |
| 977 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 978 | int parse_events_add_breakpoint(struct list_head *list, int *idx, |
Ian Rogers | aa98d84 | 2020-09-24 17:39:03 -0700 | [diff] [blame] | 979 | u64 addr, char *type, u64 len) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 980 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 981 | struct perf_event_attr attr; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 982 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 983 | memset(&attr, 0, sizeof(attr)); |
Ian Rogers | aa98d84 | 2020-09-24 17:39:03 -0700 | [diff] [blame] | 984 | attr.bp_addr = addr; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 985 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 986 | if (parse_breakpoint_type(type, &attr)) |
| 987 | return -EINVAL; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 988 | |
Jacob Shin | 3741eb9 | 2014-05-29 17:26:51 +0200 | [diff] [blame] | 989 | /* Provide some defaults if len is not specified */ |
| 990 | if (!len) { |
| 991 | if (attr.bp_type == HW_BREAKPOINT_X) |
| 992 | len = sizeof(long); |
| 993 | else |
| 994 | len = HW_BREAKPOINT_LEN_4; |
| 995 | } |
| 996 | |
| 997 | attr.bp_len = len; |
Frederic Weisbecker | aa59a48 | 2010-06-24 21:36:19 +0200 | [diff] [blame] | 998 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 999 | attr.type = PERF_TYPE_BREAKPOINT; |
Jovi Zhang | 4a841d6 | 2012-07-15 03:03:10 +0800 | [diff] [blame] | 1000 | attr.sample_period = 1; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 1001 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1002 | return add_event(list, idx, &attr, /*name=*/NULL, /*mertic_id=*/NULL, |
| 1003 | /*config_terms=*/NULL); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 1004 | } |
| 1005 | |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 1006 | static int check_type_val(struct parse_events_term *term, |
| 1007 | struct parse_events_error *err, |
| 1008 | int type) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1009 | { |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 1010 | if (type == term->type_val) |
| 1011 | return 0; |
| 1012 | |
| 1013 | if (err) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1014 | parse_events_error__handle(err, term->err_val, |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1015 | type == PARSE_EVENTS__TERM_TYPE_NUM |
| 1016 | ? strdup("expected numeric value") |
| 1017 | : strdup("expected string value"), |
| 1018 | NULL); |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 1019 | } |
| 1020 | return -EINVAL; |
| 1021 | } |
| 1022 | |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 1023 | /* |
| 1024 | * Update according to parse-events.l |
| 1025 | */ |
| 1026 | static const char *config_term_names[__PARSE_EVENTS__TERM_TYPE_NR] = { |
| 1027 | [PARSE_EVENTS__TERM_TYPE_USER] = "<sysfs term>", |
| 1028 | [PARSE_EVENTS__TERM_TYPE_CONFIG] = "config", |
| 1029 | [PARSE_EVENTS__TERM_TYPE_CONFIG1] = "config1", |
| 1030 | [PARSE_EVENTS__TERM_TYPE_CONFIG2] = "config2", |
| 1031 | [PARSE_EVENTS__TERM_TYPE_NAME] = "name", |
| 1032 | [PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD] = "period", |
| 1033 | [PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ] = "freq", |
| 1034 | [PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE] = "branch_type", |
| 1035 | [PARSE_EVENTS__TERM_TYPE_TIME] = "time", |
| 1036 | [PARSE_EVENTS__TERM_TYPE_CALLGRAPH] = "call-graph", |
| 1037 | [PARSE_EVENTS__TERM_TYPE_STACKSIZE] = "stack-size", |
| 1038 | [PARSE_EVENTS__TERM_TYPE_NOINHERIT] = "no-inherit", |
| 1039 | [PARSE_EVENTS__TERM_TYPE_INHERIT] = "inherit", |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 1040 | [PARSE_EVENTS__TERM_TYPE_MAX_STACK] = "max-stack", |
Arnaldo Carvalho de Melo | 2fda5ad | 2018-10-19 15:47:34 -0300 | [diff] [blame] | 1041 | [PARSE_EVENTS__TERM_TYPE_MAX_EVENTS] = "nr", |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 1042 | [PARSE_EVENTS__TERM_TYPE_OVERWRITE] = "overwrite", |
| 1043 | [PARSE_EVENTS__TERM_TYPE_NOOVERWRITE] = "no-overwrite", |
Mathieu Poirier | dd60fba | 2016-09-06 10:37:15 -0600 | [diff] [blame] | 1044 | [PARSE_EVENTS__TERM_TYPE_DRV_CFG] = "driver-config", |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1045 | [PARSE_EVENTS__TERM_TYPE_PERCORE] = "percore", |
Adrian Hunter | 1b99215 | 2019-08-06 11:46:05 +0300 | [diff] [blame] | 1046 | [PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT] = "aux-output", |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 1047 | [PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE] = "aux-sample-size", |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1048 | [PARSE_EVENTS__TERM_TYPE_METRIC_ID] = "metric-id", |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 1049 | }; |
| 1050 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 1051 | static bool config_term_shrinked; |
| 1052 | |
| 1053 | static bool |
| 1054 | config_term_avail(int term_type, struct parse_events_error *err) |
| 1055 | { |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1056 | char *err_str; |
| 1057 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 1058 | if (term_type < 0 || term_type >= __PARSE_EVENTS__TERM_TYPE_NR) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1059 | parse_events_error__handle(err, -1, |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1060 | strdup("Invalid term_type"), NULL); |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 1061 | return false; |
| 1062 | } |
| 1063 | if (!config_term_shrinked) |
| 1064 | return true; |
| 1065 | |
| 1066 | switch (term_type) { |
| 1067 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
| 1068 | case PARSE_EVENTS__TERM_TYPE_CONFIG1: |
| 1069 | case PARSE_EVENTS__TERM_TYPE_CONFIG2: |
| 1070 | case PARSE_EVENTS__TERM_TYPE_NAME: |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1071 | case PARSE_EVENTS__TERM_TYPE_METRIC_ID: |
Sukadev Bhattiprolu | 0683554 | 2016-09-15 15:24:56 -0700 | [diff] [blame] | 1072 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1073 | case PARSE_EVENTS__TERM_TYPE_PERCORE: |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 1074 | return true; |
| 1075 | default: |
| 1076 | if (!err) |
| 1077 | return false; |
| 1078 | |
| 1079 | /* term_type is validated so indexing is safe */ |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1080 | if (asprintf(&err_str, "'%s' is not usable in 'perf stat'", |
| 1081 | config_term_names[term_type]) >= 0) |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1082 | parse_events_error__handle(err, -1, err_str, NULL); |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 1083 | return false; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | void parse_events__shrink_config_terms(void) |
| 1088 | { |
| 1089 | config_term_shrinked = true; |
| 1090 | } |
| 1091 | |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 1092 | static int config_term_common(struct perf_event_attr *attr, |
| 1093 | struct parse_events_term *term, |
| 1094 | struct parse_events_error *err) |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 1095 | { |
| 1096 | #define CHECK_TYPE_VAL(type) \ |
| 1097 | do { \ |
| 1098 | if (check_type_val(term, err, PARSE_EVENTS__TERM_TYPE_ ## type)) \ |
| 1099 | return -EINVAL; \ |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1100 | } while (0) |
| 1101 | |
| 1102 | switch (term->type_term) { |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1103 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1104 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1105 | attr->config = term->val.num; |
| 1106 | break; |
| 1107 | case PARSE_EVENTS__TERM_TYPE_CONFIG1: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1108 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1109 | attr->config1 = term->val.num; |
| 1110 | break; |
| 1111 | case PARSE_EVENTS__TERM_TYPE_CONFIG2: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1112 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1113 | attr->config2 = term->val.num; |
| 1114 | break; |
| 1115 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1116 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1117 | break; |
Namhyung Kim | 09af2a5 | 2015-08-09 15:45:23 +0900 | [diff] [blame] | 1118 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: |
| 1119 | CHECK_TYPE_VAL(NUM); |
| 1120 | break; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1121 | case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: |
Andi Kleen | ac12f67 | 2016-10-12 14:02:06 -0700 | [diff] [blame] | 1122 | CHECK_TYPE_VAL(STR); |
| 1123 | if (strcmp(term->val.str, "no") && |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1124 | parse_branch_str(term->val.str, |
| 1125 | &attr->branch_sample_type)) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1126 | parse_events_error__handle(err, term->err_val, |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1127 | strdup("invalid branch sample type"), |
| 1128 | NULL); |
Andi Kleen | ac12f67 | 2016-10-12 14:02:06 -0700 | [diff] [blame] | 1129 | return -EINVAL; |
| 1130 | } |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1131 | break; |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 1132 | case PARSE_EVENTS__TERM_TYPE_TIME: |
| 1133 | CHECK_TYPE_VAL(NUM); |
| 1134 | if (term->val.num > 1) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1135 | parse_events_error__handle(err, term->err_val, |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1136 | strdup("expected 0 or 1"), |
| 1137 | NULL); |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 1138 | return -EINVAL; |
| 1139 | } |
| 1140 | break; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 1141 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: |
| 1142 | CHECK_TYPE_VAL(STR); |
| 1143 | break; |
| 1144 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: |
| 1145 | CHECK_TYPE_VAL(NUM); |
| 1146 | break; |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 1147 | case PARSE_EVENTS__TERM_TYPE_INHERIT: |
| 1148 | CHECK_TYPE_VAL(NUM); |
| 1149 | break; |
| 1150 | case PARSE_EVENTS__TERM_TYPE_NOINHERIT: |
| 1151 | CHECK_TYPE_VAL(NUM); |
| 1152 | break; |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 1153 | case PARSE_EVENTS__TERM_TYPE_OVERWRITE: |
| 1154 | CHECK_TYPE_VAL(NUM); |
| 1155 | break; |
| 1156 | case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: |
| 1157 | CHECK_TYPE_VAL(NUM); |
| 1158 | break; |
Jiri Olsa | 6b5fc39 | 2012-05-21 09:12:53 +0200 | [diff] [blame] | 1159 | case PARSE_EVENTS__TERM_TYPE_NAME: |
| 1160 | CHECK_TYPE_VAL(STR); |
| 1161 | break; |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1162 | case PARSE_EVENTS__TERM_TYPE_METRIC_ID: |
| 1163 | CHECK_TYPE_VAL(STR); |
| 1164 | break; |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 1165 | case PARSE_EVENTS__TERM_TYPE_MAX_STACK: |
| 1166 | CHECK_TYPE_VAL(NUM); |
| 1167 | break; |
Arnaldo Carvalho de Melo | 2fda5ad | 2018-10-19 15:47:34 -0300 | [diff] [blame] | 1168 | case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: |
| 1169 | CHECK_TYPE_VAL(NUM); |
| 1170 | break; |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1171 | case PARSE_EVENTS__TERM_TYPE_PERCORE: |
| 1172 | CHECK_TYPE_VAL(NUM); |
| 1173 | if ((unsigned int)term->val.num > 1) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1174 | parse_events_error__handle(err, term->err_val, |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1175 | strdup("expected 0 or 1"), |
| 1176 | NULL); |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1177 | return -EINVAL; |
| 1178 | } |
| 1179 | break; |
Adrian Hunter | 1b99215 | 2019-08-06 11:46:05 +0300 | [diff] [blame] | 1180 | case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: |
| 1181 | CHECK_TYPE_VAL(NUM); |
| 1182 | break; |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 1183 | case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: |
| 1184 | CHECK_TYPE_VAL(NUM); |
| 1185 | if (term->val.num > UINT_MAX) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1186 | parse_events_error__handle(err, term->err_val, |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 1187 | strdup("too big"), |
| 1188 | NULL); |
| 1189 | return -EINVAL; |
| 1190 | } |
| 1191 | break; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1192 | default: |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1193 | parse_events_error__handle(err, term->err_term, |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1194 | strdup("unknown term"), |
| 1195 | parse_events_formats_error_string(NULL)); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1196 | return -EINVAL; |
| 1197 | } |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1198 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 1199 | /* |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 1200 | * Check term availability after basic checking so |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 1201 | * PARSE_EVENTS__TERM_TYPE_USER can be found and filtered. |
| 1202 | * |
Ingo Molnar | 4d39c89 | 2021-03-23 17:09:15 +0100 | [diff] [blame] | 1203 | * If check availability at the entry of this function, |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 1204 | * user will see "'<sysfs term>' is not usable in 'perf stat'" |
| 1205 | * if an invalid config term is provided for legacy events |
| 1206 | * (for example, instructions/badterm/...), which is confusing. |
| 1207 | */ |
| 1208 | if (!config_term_avail(term->type_term, err)) |
| 1209 | return -EINVAL; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1210 | return 0; |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1211 | #undef CHECK_TYPE_VAL |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1212 | } |
| 1213 | |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 1214 | static int config_term_pmu(struct perf_event_attr *attr, |
| 1215 | struct parse_events_term *term, |
| 1216 | struct parse_events_error *err) |
| 1217 | { |
Mathieu Poirier | dd60fba | 2016-09-06 10:37:15 -0600 | [diff] [blame] | 1218 | if (term->type_term == PARSE_EVENTS__TERM_TYPE_USER || |
| 1219 | term->type_term == PARSE_EVENTS__TERM_TYPE_DRV_CFG) |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 1220 | /* |
| 1221 | * Always succeed for sysfs terms, as we dont know |
| 1222 | * at this point what type they need to have. |
| 1223 | */ |
| 1224 | return 0; |
| 1225 | else |
| 1226 | return config_term_common(attr, term, err); |
| 1227 | } |
| 1228 | |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1229 | static int config_term_tracepoint(struct perf_event_attr *attr, |
| 1230 | struct parse_events_term *term, |
| 1231 | struct parse_events_error *err) |
| 1232 | { |
| 1233 | switch (term->type_term) { |
| 1234 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: |
| 1235 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 1236 | case PARSE_EVENTS__TERM_TYPE_INHERIT: |
| 1237 | case PARSE_EVENTS__TERM_TYPE_NOINHERIT: |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 1238 | case PARSE_EVENTS__TERM_TYPE_MAX_STACK: |
Arnaldo Carvalho de Melo | 2fda5ad | 2018-10-19 15:47:34 -0300 | [diff] [blame] | 1239 | case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 1240 | case PARSE_EVENTS__TERM_TYPE_OVERWRITE: |
| 1241 | case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: |
Adrian Hunter | 1b99215 | 2019-08-06 11:46:05 +0300 | [diff] [blame] | 1242 | case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 1243 | case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1244 | return config_term_common(attr, term, err); |
| 1245 | default: |
| 1246 | if (err) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1247 | parse_events_error__handle(err, term->err_term, |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1248 | strdup("unknown term"), |
| 1249 | strdup("valid terms: call-graph,stack-size\n")); |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1250 | } |
| 1251 | return -EINVAL; |
| 1252 | } |
| 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1257 | static int config_attr(struct perf_event_attr *attr, |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 1258 | struct list_head *head, |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 1259 | struct parse_events_error *err, |
| 1260 | config_term_func_t config_term) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1261 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1262 | struct parse_events_term *term; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1263 | |
| 1264 | list_for_each_entry(term, head, list) |
Jiri Olsa | 3b0e371 | 2015-04-22 21:10:22 +0200 | [diff] [blame] | 1265 | if (config_term(attr, term, err)) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1266 | return -EINVAL; |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1271 | static int get_config_terms(struct list_head *head_config, |
| 1272 | struct list_head *head_terms __maybe_unused) |
| 1273 | { |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1274 | #define ADD_CONFIG_TERM(__type, __weak) \ |
Arnaldo Carvalho de Melo | 35ac0ca | 2020-05-06 13:05:08 -0300 | [diff] [blame] | 1275 | struct evsel_config_term *__t; \ |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1276 | \ |
| 1277 | __t = zalloc(sizeof(*__t)); \ |
| 1278 | if (!__t) \ |
| 1279 | return -ENOMEM; \ |
| 1280 | \ |
| 1281 | INIT_LIST_HEAD(&__t->list); \ |
Arnaldo Carvalho de Melo | 35ac0ca | 2020-05-06 13:05:08 -0300 | [diff] [blame] | 1282 | __t->type = EVSEL__CONFIG_TERM_ ## __type; \ |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1283 | __t->weak = __weak; \ |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1284 | list_add_tail(&__t->list, head_terms) |
| 1285 | |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1286 | #define ADD_CONFIG_TERM_VAL(__type, __name, __val, __weak) \ |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1287 | do { \ |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1288 | ADD_CONFIG_TERM(__type, __weak); \ |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1289 | __t->val.__name = __val; \ |
| 1290 | } while (0) |
| 1291 | |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1292 | #define ADD_CONFIG_TERM_STR(__type, __val, __weak) \ |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1293 | do { \ |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1294 | ADD_CONFIG_TERM(__type, __weak); \ |
Leo Yan | 3220fb8 | 2020-01-17 13:52:51 +0800 | [diff] [blame] | 1295 | __t->val.str = strdup(__val); \ |
| 1296 | if (!__t->val.str) { \ |
| 1297 | zfree(&__t); \ |
| 1298 | return -ENOMEM; \ |
| 1299 | } \ |
| 1300 | __t->free_str = true; \ |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1301 | } while (0) |
| 1302 | |
| 1303 | struct parse_events_term *term; |
| 1304 | |
| 1305 | list_for_each_entry(term, head_config, list) { |
| 1306 | switch (term->type_term) { |
Jiri Olsa | ee4c758 | 2015-07-29 05:42:11 -0400 | [diff] [blame] | 1307 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1308 | ADD_CONFIG_TERM_VAL(PERIOD, period, term->val.num, term->weak); |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 1309 | break; |
Namhyung Kim | 09af2a5 | 2015-08-09 15:45:23 +0900 | [diff] [blame] | 1310 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_FREQ: |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1311 | ADD_CONFIG_TERM_VAL(FREQ, freq, term->val.num, term->weak); |
Namhyung Kim | 09af2a5 | 2015-08-09 15:45:23 +0900 | [diff] [blame] | 1312 | break; |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 1313 | case PARSE_EVENTS__TERM_TYPE_TIME: |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1314 | ADD_CONFIG_TERM_VAL(TIME, time, term->val.num, term->weak); |
Kan Liang | 3206771 | 2015-08-04 04:30:19 -0400 | [diff] [blame] | 1315 | break; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 1316 | case PARSE_EVENTS__TERM_TYPE_CALLGRAPH: |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1317 | ADD_CONFIG_TERM_STR(CALLGRAPH, term->val.str, term->weak); |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 1318 | break; |
Andi Kleen | ac12f67 | 2016-10-12 14:02:06 -0700 | [diff] [blame] | 1319 | case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1320 | ADD_CONFIG_TERM_STR(BRANCH, term->val.str, term->weak); |
Andi Kleen | ac12f67 | 2016-10-12 14:02:06 -0700 | [diff] [blame] | 1321 | break; |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 1322 | case PARSE_EVENTS__TERM_TYPE_STACKSIZE: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1323 | ADD_CONFIG_TERM_VAL(STACK_USER, stack_user, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1324 | term->val.num, term->weak); |
Kan Liang | d457c96 | 2015-08-11 06:30:47 -0400 | [diff] [blame] | 1325 | break; |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 1326 | case PARSE_EVENTS__TERM_TYPE_INHERIT: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1327 | ADD_CONFIG_TERM_VAL(INHERIT, inherit, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1328 | term->val.num ? 1 : 0, term->weak); |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 1329 | break; |
| 1330 | case PARSE_EVENTS__TERM_TYPE_NOINHERIT: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1331 | ADD_CONFIG_TERM_VAL(INHERIT, inherit, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1332 | term->val.num ? 0 : 1, term->weak); |
Wang Nan | 374ce93 | 2015-10-28 10:55:02 +0000 | [diff] [blame] | 1333 | break; |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 1334 | case PARSE_EVENTS__TERM_TYPE_MAX_STACK: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1335 | ADD_CONFIG_TERM_VAL(MAX_STACK, max_stack, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1336 | term->val.num, term->weak); |
Arnaldo Carvalho de Melo | 792d48b | 2016-04-28 19:03:42 -0300 | [diff] [blame] | 1337 | break; |
Arnaldo Carvalho de Melo | 2fda5ad | 2018-10-19 15:47:34 -0300 | [diff] [blame] | 1338 | case PARSE_EVENTS__TERM_TYPE_MAX_EVENTS: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1339 | ADD_CONFIG_TERM_VAL(MAX_EVENTS, max_events, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1340 | term->val.num, term->weak); |
Arnaldo Carvalho de Melo | 2fda5ad | 2018-10-19 15:47:34 -0300 | [diff] [blame] | 1341 | break; |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 1342 | case PARSE_EVENTS__TERM_TYPE_OVERWRITE: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1343 | ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1344 | term->val.num ? 1 : 0, term->weak); |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 1345 | break; |
| 1346 | case PARSE_EVENTS__TERM_TYPE_NOOVERWRITE: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1347 | ADD_CONFIG_TERM_VAL(OVERWRITE, overwrite, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1348 | term->val.num ? 0 : 1, term->weak); |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 1349 | break; |
Mathieu Poirier | dd60fba | 2016-09-06 10:37:15 -0600 | [diff] [blame] | 1350 | case PARSE_EVENTS__TERM_TYPE_DRV_CFG: |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1351 | ADD_CONFIG_TERM_STR(DRV_CFG, term->val.str, term->weak); |
Mathieu Poirier | dd60fba | 2016-09-06 10:37:15 -0600 | [diff] [blame] | 1352 | break; |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1353 | case PARSE_EVENTS__TERM_TYPE_PERCORE: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1354 | ADD_CONFIG_TERM_VAL(PERCORE, percore, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1355 | term->val.num ? true : false, term->weak); |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1356 | break; |
Adrian Hunter | 1b99215 | 2019-08-06 11:46:05 +0300 | [diff] [blame] | 1357 | case PARSE_EVENTS__TERM_TYPE_AUX_OUTPUT: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1358 | ADD_CONFIG_TERM_VAL(AUX_OUTPUT, aux_output, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1359 | term->val.num ? 1 : 0, term->weak); |
Adrian Hunter | 1b99215 | 2019-08-06 11:46:05 +0300 | [diff] [blame] | 1360 | break; |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 1361 | case PARSE_EVENTS__TERM_TYPE_AUX_SAMPLE_SIZE: |
Leo Yan | e884602 | 2020-01-17 13:52:50 +0800 | [diff] [blame] | 1362 | ADD_CONFIG_TERM_VAL(AUX_SAMPLE_SIZE, aux_sample_size, |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1363 | term->val.num, term->weak); |
Adrian Hunter | eb7a52d | 2019-11-15 14:42:17 +0200 | [diff] [blame] | 1364 | break; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1365 | default: |
| 1366 | break; |
| 1367 | } |
| 1368 | } |
Adrian Hunter | a1ac7de | 2019-11-15 14:42:22 +0200 | [diff] [blame] | 1369 | return 0; |
| 1370 | } |
| 1371 | |
| 1372 | /* |
Arnaldo Carvalho de Melo | 35ac0ca | 2020-05-06 13:05:08 -0300 | [diff] [blame] | 1373 | * Add EVSEL__CONFIG_TERM_CFG_CHG where cfg_chg will have a bit set for |
Adrian Hunter | a1ac7de | 2019-11-15 14:42:22 +0200 | [diff] [blame] | 1374 | * each bit of attr->config that the user has changed. |
| 1375 | */ |
| 1376 | static int get_config_chgs(struct perf_pmu *pmu, struct list_head *head_config, |
| 1377 | struct list_head *head_terms) |
| 1378 | { |
| 1379 | struct parse_events_term *term; |
| 1380 | u64 bits = 0; |
| 1381 | int type; |
| 1382 | |
| 1383 | list_for_each_entry(term, head_config, list) { |
| 1384 | switch (term->type_term) { |
| 1385 | case PARSE_EVENTS__TERM_TYPE_USER: |
| 1386 | type = perf_pmu__format_type(&pmu->format, term->config); |
| 1387 | if (type != PERF_PMU_FORMAT_VALUE_CONFIG) |
| 1388 | continue; |
| 1389 | bits |= perf_pmu__format_bits(&pmu->format, term->config); |
| 1390 | break; |
| 1391 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
| 1392 | bits = ~(u64)0; |
| 1393 | break; |
| 1394 | default: |
| 1395 | break; |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | if (bits) |
Ian Rogers | 05e54e2 | 2020-03-06 23:31:21 -0800 | [diff] [blame] | 1400 | ADD_CONFIG_TERM_VAL(CFG_CHG, cfg_chg, bits, false); |
Adrian Hunter | a1ac7de | 2019-11-15 14:42:22 +0200 | [diff] [blame] | 1401 | |
| 1402 | #undef ADD_CONFIG_TERM |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1403 | return 0; |
| 1404 | } |
| 1405 | |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1406 | int parse_events_add_tracepoint(struct list_head *list, int *idx, |
Wang Nan | 8c619d6 | 2016-07-13 10:44:03 +0000 | [diff] [blame] | 1407 | const char *sys, const char *event, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 1408 | struct parse_events_error *err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1409 | struct list_head *head_config) |
| 1410 | { |
| 1411 | if (head_config) { |
| 1412 | struct perf_event_attr attr; |
| 1413 | |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 1414 | if (config_attr(&attr, head_config, err, |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1415 | config_term_tracepoint)) |
| 1416 | return -EINVAL; |
| 1417 | } |
| 1418 | |
| 1419 | if (strpbrk(sys, "*?")) |
| 1420 | return add_tracepoint_multi_sys(list, idx, sys, event, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 1421 | err, head_config); |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1422 | else |
| 1423 | return add_tracepoint_event(list, idx, sys, event, |
Jiri Olsa | 272ed29 | 2015-09-29 17:05:31 +0200 | [diff] [blame] | 1424 | err, head_config); |
He Kuang | e637d17 | 2015-09-28 03:52:16 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 1427 | int parse_events_add_numeric(struct parse_events_state *parse_state, |
Jiri Olsa | 87d650b | 2015-04-22 21:10:24 +0200 | [diff] [blame] | 1428 | struct list_head *list, |
Robert Richter | b527bab | 2012-08-07 19:43:13 +0200 | [diff] [blame] | 1429 | u32 type, u64 config, |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1430 | struct list_head *head_config) |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 1431 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1432 | struct perf_event_attr attr; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1433 | LIST_HEAD(config_terms); |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1434 | const char *name, *metric_id; |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 1435 | bool hybrid; |
| 1436 | int ret; |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 1437 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1438 | memset(&attr, 0, sizeof(attr)); |
| 1439 | attr.type = type; |
| 1440 | attr.config = config; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1441 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1442 | if (head_config) { |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 1443 | if (config_attr(&attr, head_config, parse_state->error, |
He Kuang | 0b8891a | 2015-09-28 03:52:13 +0000 | [diff] [blame] | 1444 | config_term_common)) |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1445 | return -EINVAL; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1446 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1447 | if (get_config_terms(head_config, &config_terms)) |
| 1448 | return -ENOMEM; |
| 1449 | } |
| 1450 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1451 | name = get_config_name(head_config); |
| 1452 | metric_id = get_config_metric_id(head_config); |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 1453 | ret = parse_events__add_numeric_hybrid(parse_state, list, &attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1454 | name, metric_id, |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 1455 | &config_terms, &hybrid); |
| 1456 | if (hybrid) |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 1457 | goto out_free_terms; |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 1458 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1459 | ret = add_event(list, &parse_state->idx, &attr, name, metric_id, |
| 1460 | &config_terms); |
Adrian Hunter | 99fc594 | 2021-09-09 15:55:08 +0300 | [diff] [blame] | 1461 | out_free_terms: |
| 1462 | free_config_terms(&config_terms); |
| 1463 | return ret; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1464 | } |
| 1465 | |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 1466 | int parse_events_add_tool(struct parse_events_state *parse_state, |
| 1467 | struct list_head *list, |
Ian Rogers | 8228e93 | 2021-09-15 14:14:28 -0700 | [diff] [blame] | 1468 | int tool_event) |
Andi Kleen | f0fbb11 | 2019-03-26 15:18:21 -0700 | [diff] [blame] | 1469 | { |
| 1470 | return add_event_tool(list, &parse_state->idx, tool_event); |
| 1471 | } |
| 1472 | |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1473 | static bool config_term_percore(struct list_head *config_terms) |
| 1474 | { |
Arnaldo Carvalho de Melo | 35ac0ca | 2020-05-06 13:05:08 -0300 | [diff] [blame] | 1475 | struct evsel_config_term *term; |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1476 | |
| 1477 | list_for_each_entry(term, config_terms, list) { |
Arnaldo Carvalho de Melo | 35ac0ca | 2020-05-06 13:05:08 -0300 | [diff] [blame] | 1478 | if (term->type == EVSEL__CONFIG_TERM_PERCORE) |
Jin Yao | 064b4e8 | 2019-04-12 21:59:47 +0800 | [diff] [blame] | 1479 | return term->val.percore; |
| 1480 | } |
| 1481 | |
| 1482 | return false; |
| 1483 | } |
| 1484 | |
Jin Yao | 5e4edd1 | 2021-04-27 15:01:25 +0800 | [diff] [blame] | 1485 | static int parse_events__inside_hybrid_pmu(struct parse_events_state *parse_state, |
| 1486 | struct list_head *list, char *name, |
| 1487 | struct list_head *head_config) |
| 1488 | { |
| 1489 | struct parse_events_term *term; |
| 1490 | int ret = -1; |
| 1491 | |
| 1492 | if (parse_state->fake_pmu || !head_config || list_empty(head_config) || |
| 1493 | !perf_pmu__is_hybrid(name)) { |
| 1494 | return -1; |
| 1495 | } |
| 1496 | |
| 1497 | /* |
| 1498 | * More than one term in list. |
| 1499 | */ |
| 1500 | if (head_config->next && head_config->next->next != head_config) |
| 1501 | return -1; |
| 1502 | |
| 1503 | term = list_first_entry(head_config, struct parse_events_term, list); |
| 1504 | if (term && term->config && strcmp(term->config, "event")) { |
| 1505 | ret = parse_events__with_hybrid_pmu(parse_state, term->config, |
| 1506 | name, list); |
| 1507 | } |
| 1508 | |
| 1509 | return ret; |
| 1510 | } |
| 1511 | |
Agustin Vega-Frias | c199c11 | 2018-03-06 09:04:44 -0500 | [diff] [blame] | 1512 | int parse_events_add_pmu(struct parse_events_state *parse_state, |
Jiri Olsa | 36adec8 | 2015-04-22 21:10:19 +0200 | [diff] [blame] | 1513 | struct list_head *list, char *name, |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1514 | struct list_head *head_config, |
| 1515 | bool auto_merge_stats, |
| 1516 | bool use_alias) |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1517 | { |
| 1518 | struct perf_event_attr attr; |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 1519 | struct perf_pmu_info info; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1520 | struct perf_pmu *pmu; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1521 | struct evsel *evsel; |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 1522 | struct parse_events_error *err = parse_state->error; |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1523 | bool use_uncore_alias; |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1524 | LIST_HEAD(config_terms); |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1525 | |
Ian Rogers | 4ac22b4 | 2020-05-13 15:06:35 -0700 | [diff] [blame] | 1526 | if (verbose > 1) { |
| 1527 | fprintf(stderr, "Attempting to add event pmu '%s' with '", |
| 1528 | name); |
| 1529 | if (head_config) { |
| 1530 | struct parse_events_term *term; |
| 1531 | |
| 1532 | list_for_each_entry(term, head_config, list) { |
| 1533 | fprintf(stderr, "%s,", term->config); |
| 1534 | } |
| 1535 | } |
| 1536 | fprintf(stderr, "' that may result in non-fatal errors\n"); |
| 1537 | } |
| 1538 | |
Jiri Olsa | 387ad33 | 2020-06-02 23:47:29 +0200 | [diff] [blame] | 1539 | pmu = parse_state->fake_pmu ?: perf_pmu__find(name); |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 1540 | if (!pmu) { |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1541 | char *err_str; |
| 1542 | |
| 1543 | if (asprintf(&err_str, |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 1544 | "Cannot find PMU `%s'. Missing kernel support?", |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 1545 | name) >= 0) |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 1546 | parse_events_error__handle(err, 0, err_str, NULL); |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1547 | return -EINVAL; |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 1548 | } |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1549 | |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 1550 | if (pmu->default_config) { |
| 1551 | memcpy(&attr, pmu->default_config, |
| 1552 | sizeof(struct perf_event_attr)); |
| 1553 | } else { |
| 1554 | memset(&attr, 0, sizeof(attr)); |
| 1555 | } |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1556 | |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1557 | use_uncore_alias = (pmu->is_uncore && use_alias); |
| 1558 | |
Adrian Hunter | ad96227 | 2014-08-15 22:08:40 +0300 | [diff] [blame] | 1559 | if (!head_config) { |
| 1560 | attr.type = pmu->type; |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1561 | evsel = __add_event(list, &parse_state->idx, &attr, |
| 1562 | /*init_attr=*/true, /*name=*/NULL, |
| 1563 | /*metric_id=*/NULL, pmu, |
| 1564 | /*config_terms=*/NULL, auto_merge_stats, |
| 1565 | /*cpu_list=*/NULL); |
Agustin Vega-Frias | 8c5421c | 2018-03-06 09:04:43 -0500 | [diff] [blame] | 1566 | if (evsel) { |
Ian Rogers | d4953f7 | 2020-03-14 10:03:56 -0700 | [diff] [blame] | 1567 | evsel->pmu_name = name ? strdup(name) : NULL; |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1568 | evsel->use_uncore_alias = use_uncore_alias; |
Agustin Vega-Frias | 8c5421c | 2018-03-06 09:04:43 -0500 | [diff] [blame] | 1569 | return 0; |
| 1570 | } else { |
| 1571 | return -ENOMEM; |
| 1572 | } |
Adrian Hunter | ad96227 | 2014-08-15 22:08:40 +0300 | [diff] [blame] | 1573 | } |
| 1574 | |
Jiri Olsa | 387ad33 | 2020-06-02 23:47:29 +0200 | [diff] [blame] | 1575 | if (!parse_state->fake_pmu && perf_pmu__check_alias(pmu, head_config, &info)) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1576 | return -EINVAL; |
| 1577 | |
Ian Rogers | 4ac22b4 | 2020-05-13 15:06:35 -0700 | [diff] [blame] | 1578 | if (verbose > 1) { |
| 1579 | fprintf(stderr, "After aliases, add event pmu '%s' with '", |
| 1580 | name); |
| 1581 | if (head_config) { |
| 1582 | struct parse_events_term *term; |
| 1583 | |
| 1584 | list_for_each_entry(term, head_config, list) { |
| 1585 | fprintf(stderr, "%s,", term->config); |
| 1586 | } |
| 1587 | } |
| 1588 | fprintf(stderr, "' that may result in non-fatal errors\n"); |
| 1589 | } |
| 1590 | |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1591 | /* |
| 1592 | * Configure hardcoded terms first, no need to check |
| 1593 | * return value when called with fail == 0 ;) |
| 1594 | */ |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 1595 | if (config_attr(&attr, head_config, parse_state->error, config_term_pmu)) |
Jiri Olsa | c056ba6a | 2015-04-22 21:10:18 +0200 | [diff] [blame] | 1596 | return -EINVAL; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1597 | |
Jiri Olsa | 930a2e2 | 2015-07-29 05:42:10 -0400 | [diff] [blame] | 1598 | if (get_config_terms(head_config, &config_terms)) |
| 1599 | return -ENOMEM; |
| 1600 | |
Adrian Hunter | a1ac7de | 2019-11-15 14:42:22 +0200 | [diff] [blame] | 1601 | /* |
| 1602 | * When using default config, record which bits of attr->config were |
| 1603 | * changed by the user. |
| 1604 | */ |
| 1605 | if (pmu->default_config && get_config_chgs(pmu, head_config, &config_terms)) |
| 1606 | return -ENOMEM; |
| 1607 | |
Jin Yao | 5e4edd1 | 2021-04-27 15:01:25 +0800 | [diff] [blame] | 1608 | if (!parse_events__inside_hybrid_pmu(parse_state, list, name, |
| 1609 | head_config)) { |
| 1610 | return 0; |
| 1611 | } |
| 1612 | |
Jiri Olsa | 387ad33 | 2020-06-02 23:47:29 +0200 | [diff] [blame] | 1613 | if (!parse_state->fake_pmu && perf_pmu__config(pmu, &attr, head_config, parse_state->error)) { |
Adrian Hunter | a7d212f | 2021-09-09 15:55:07 +0300 | [diff] [blame] | 1614 | free_config_terms(&config_terms); |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1615 | return -EINVAL; |
Ian Rogers | 38f2c42 | 2019-10-30 15:34:46 -0700 | [diff] [blame] | 1616 | } |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1617 | |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 1618 | evsel = __add_event(list, &parse_state->idx, &attr, /*init_attr=*/true, |
| 1619 | get_config_name(head_config), |
| 1620 | get_config_metric_id(head_config), pmu, |
| 1621 | &config_terms, auto_merge_stats, /*cpu_list=*/NULL); |
Ian Rogers | 33321a0 | 2020-08-25 21:29:10 -0700 | [diff] [blame] | 1622 | if (!evsel) |
| 1623 | return -ENOMEM; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 1624 | |
Jin Yao | 1227942 | 2021-04-27 15:01:20 +0800 | [diff] [blame] | 1625 | if (evsel->name) |
| 1626 | evsel->use_config_name = true; |
| 1627 | |
Ian Rogers | 33321a0 | 2020-08-25 21:29:10 -0700 | [diff] [blame] | 1628 | evsel->pmu_name = name ? strdup(name) : NULL; |
| 1629 | evsel->use_uncore_alias = use_uncore_alias; |
| 1630 | evsel->percore = config_term_percore(&evsel->config_terms); |
| 1631 | |
| 1632 | if (parse_state->fake_pmu) |
| 1633 | return 0; |
| 1634 | |
Ian Rogers | b194c9c | 2021-11-18 00:47:49 -0800 | [diff] [blame] | 1635 | free((char *)evsel->unit); |
| 1636 | evsel->unit = strdup(info.unit); |
Ian Rogers | 33321a0 | 2020-08-25 21:29:10 -0700 | [diff] [blame] | 1637 | evsel->scale = info.scale; |
| 1638 | evsel->per_pkg = info.per_pkg; |
| 1639 | evsel->snapshot = info.snapshot; |
| 1640 | evsel->metric_expr = info.metric_expr; |
| 1641 | evsel->metric_name = info.metric_name; |
| 1642 | return 0; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 1645 | int parse_events_multi_pmu_add(struct parse_events_state *parse_state, |
Ian Rogers | fb08115 | 2021-10-15 10:21:26 -0700 | [diff] [blame] | 1646 | char *str, struct list_head *head, |
| 1647 | struct list_head **listp) |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1648 | { |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1649 | struct parse_events_term *term; |
Ian Rogers | fb08115 | 2021-10-15 10:21:26 -0700 | [diff] [blame] | 1650 | struct list_head *list = NULL; |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1651 | struct perf_pmu *pmu = NULL; |
| 1652 | int ok = 0; |
Ian Rogers | fb08115 | 2021-10-15 10:21:26 -0700 | [diff] [blame] | 1653 | char *config; |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1654 | |
| 1655 | *listp = NULL; |
Ian Rogers | fb08115 | 2021-10-15 10:21:26 -0700 | [diff] [blame] | 1656 | |
| 1657 | if (!head) { |
| 1658 | head = malloc(sizeof(struct list_head)); |
| 1659 | if (!head) |
| 1660 | goto out_err; |
| 1661 | |
| 1662 | INIT_LIST_HEAD(head); |
| 1663 | } |
| 1664 | config = strdup(str); |
| 1665 | if (!config) |
| 1666 | goto out_err; |
| 1667 | |
| 1668 | if (parse_events_term__num(&term, |
| 1669 | PARSE_EVENTS__TERM_TYPE_USER, |
| 1670 | config, 1, false, &config, |
| 1671 | NULL) < 0) { |
| 1672 | free(config); |
| 1673 | goto out_err; |
| 1674 | } |
| 1675 | list_add_tail(&term->list, head); |
| 1676 | |
| 1677 | |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1678 | /* Add it for all PMUs that support the alias */ |
| 1679 | list = malloc(sizeof(struct list_head)); |
| 1680 | if (!list) |
Ian Rogers | fb08115 | 2021-10-15 10:21:26 -0700 | [diff] [blame] | 1681 | goto out_err; |
| 1682 | |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1683 | INIT_LIST_HEAD(list); |
Ian Rogers | fb08115 | 2021-10-15 10:21:26 -0700 | [diff] [blame] | 1684 | |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1685 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
| 1686 | struct perf_pmu_alias *alias; |
| 1687 | |
| 1688 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 1689 | if (!strcasecmp(alias->name, str)) { |
Agustin Vega-Frias | c199c11 | 2018-03-06 09:04:44 -0500 | [diff] [blame] | 1690 | if (!parse_events_add_pmu(parse_state, list, |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1691 | pmu->name, head, |
| 1692 | true, true)) { |
Ian Rogers | d4953f7 | 2020-03-14 10:03:56 -0700 | [diff] [blame] | 1693 | pr_debug("%s -> %s/%s/\n", str, |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1694 | pmu->name, alias->str); |
| 1695 | ok++; |
| 1696 | } |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | } |
John Garry | b4a7276 | 2022-01-17 23:10:15 +0800 | [diff] [blame] | 1700 | |
| 1701 | if (parse_state->fake_pmu) { |
| 1702 | if (!parse_events_add_pmu(parse_state, list, str, head, |
| 1703 | true, true)) { |
| 1704 | pr_debug("%s -> %s/%s/\n", str, "fake_pmu", str); |
| 1705 | ok++; |
| 1706 | } |
| 1707 | } |
| 1708 | |
Ian Rogers | fb08115 | 2021-10-15 10:21:26 -0700 | [diff] [blame] | 1709 | out_err: |
| 1710 | if (ok) |
| 1711 | *listp = list; |
| 1712 | else |
Ian Rogers | b6645a7 | 2019-10-30 15:34:43 -0700 | [diff] [blame] | 1713 | free(list); |
Ian Rogers | fb08115 | 2021-10-15 10:21:26 -0700 | [diff] [blame] | 1714 | |
| 1715 | parse_events_terms__delete(head); |
| 1716 | return ok ? 0 : -1; |
Andi Kleen | 2073ad3 | 2017-03-20 13:17:02 -0700 | [diff] [blame] | 1717 | } |
| 1718 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1719 | int parse_events__modifier_group(struct list_head *list, |
| 1720 | char *event_mod) |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 1721 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1722 | return parse_events__modifier_event(list, event_mod, true); |
| 1723 | } |
| 1724 | |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1725 | /* |
| 1726 | * Check if the two uncore PMUs are from the same uncore block |
| 1727 | * The format of the uncore PMU name is uncore_#blockname_#pmuidx |
| 1728 | */ |
| 1729 | static bool is_same_uncore_block(const char *pmu_name_a, const char *pmu_name_b) |
| 1730 | { |
| 1731 | char *end_a, *end_b; |
| 1732 | |
| 1733 | end_a = strrchr(pmu_name_a, '_'); |
| 1734 | end_b = strrchr(pmu_name_b, '_'); |
| 1735 | |
| 1736 | if (!end_a || !end_b) |
| 1737 | return false; |
| 1738 | |
| 1739 | if ((end_a - pmu_name_a) != (end_b - pmu_name_b)) |
| 1740 | return false; |
| 1741 | |
| 1742 | return (strncmp(pmu_name_a, pmu_name_b, end_a - pmu_name_a) == 0); |
| 1743 | } |
| 1744 | |
| 1745 | static int |
| 1746 | parse_events__set_leader_for_uncore_aliase(char *name, struct list_head *list, |
| 1747 | struct parse_events_state *parse_state) |
| 1748 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1749 | struct evsel *evsel, *leader; |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1750 | uintptr_t *leaders; |
| 1751 | bool is_leader = true; |
| 1752 | int i, nr_pmu = 0, total_members, ret = 0; |
| 1753 | |
Jiri Olsa | b27c4ec | 2019-07-21 13:24:22 +0200 | [diff] [blame] | 1754 | leader = list_first_entry(list, struct evsel, core.node); |
| 1755 | evsel = list_last_entry(list, struct evsel, core.node); |
Jiri Olsa | 38fe0e0 | 2021-07-06 17:16:59 +0200 | [diff] [blame] | 1756 | total_members = evsel->core.idx - leader->core.idx + 1; |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1757 | |
| 1758 | leaders = calloc(total_members, sizeof(uintptr_t)); |
| 1759 | if (WARN_ON(!leaders)) |
| 1760 | return 0; |
| 1761 | |
| 1762 | /* |
| 1763 | * Going through the whole group and doing sanity check. |
| 1764 | * All members must use alias, and be from the same uncore block. |
| 1765 | * Also, storing the leader events in an array. |
| 1766 | */ |
| 1767 | __evlist__for_each_entry(list, evsel) { |
| 1768 | |
| 1769 | /* Only split the uncore group which members use alias */ |
| 1770 | if (!evsel->use_uncore_alias) |
| 1771 | goto out; |
| 1772 | |
| 1773 | /* The events must be from the same uncore block */ |
| 1774 | if (!is_same_uncore_block(leader->pmu_name, evsel->pmu_name)) |
| 1775 | goto out; |
| 1776 | |
| 1777 | if (!is_leader) |
| 1778 | continue; |
| 1779 | /* |
| 1780 | * If the event's PMU name starts to repeat, it must be a new |
| 1781 | * event. That can be used to distinguish the leader from |
| 1782 | * other members, even they have the same event name. |
| 1783 | */ |
Jin Yao | 8510895 | 2020-04-30 08:36:18 +0800 | [diff] [blame] | 1784 | if ((leader != evsel) && |
| 1785 | !strcmp(leader->pmu_name, evsel->pmu_name)) { |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1786 | is_leader = false; |
| 1787 | continue; |
| 1788 | } |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1789 | |
| 1790 | /* Store the leader event for each PMU */ |
| 1791 | leaders[nr_pmu++] = (uintptr_t) evsel; |
| 1792 | } |
| 1793 | |
| 1794 | /* only one event alias */ |
| 1795 | if (nr_pmu == total_members) { |
| 1796 | parse_state->nr_groups--; |
| 1797 | goto handled; |
| 1798 | } |
| 1799 | |
| 1800 | /* |
| 1801 | * An uncore event alias is a joint name which means the same event |
| 1802 | * runs on all PMUs of a block. |
| 1803 | * Perf doesn't support mixed events from different PMUs in the same |
| 1804 | * group. The big group has to be split into multiple small groups |
| 1805 | * which only include the events from the same PMU. |
| 1806 | * |
| 1807 | * Here the uncore event aliases must be from the same uncore block. |
| 1808 | * The number of PMUs must be same for each alias. The number of new |
| 1809 | * small groups equals to the number of PMUs. |
| 1810 | * Setting the leader event for corresponding members in each group. |
| 1811 | */ |
| 1812 | i = 0; |
| 1813 | __evlist__for_each_entry(list, evsel) { |
| 1814 | if (i >= nr_pmu) |
| 1815 | i = 0; |
Jiri Olsa | fba7c86 | 2021-07-06 17:17:00 +0200 | [diff] [blame] | 1816 | evsel__set_leader(evsel, (struct evsel *) leaders[i++]); |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | /* The number of members and group name are same for each group */ |
| 1820 | for (i = 0; i < nr_pmu; i++) { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1821 | evsel = (struct evsel *) leaders[i]; |
Jiri Olsa | 5643b1a | 2019-07-21 13:24:46 +0200 | [diff] [blame] | 1822 | evsel->core.nr_members = total_members / nr_pmu; |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1823 | evsel->group_name = name ? strdup(name) : NULL; |
| 1824 | } |
| 1825 | |
| 1826 | /* Take the new small groups into account */ |
| 1827 | parse_state->nr_groups += nr_pmu - 1; |
| 1828 | |
| 1829 | handled: |
| 1830 | ret = 1; |
| 1831 | out: |
| 1832 | free(leaders); |
| 1833 | return ret; |
| 1834 | } |
| 1835 | |
Ian Rogers | 94dbfd6 | 2021-11-30 09:49:45 -0800 | [diff] [blame] | 1836 | __weak struct evsel *arch_evlist__leader(struct list_head *list) |
| 1837 | { |
| 1838 | return list_first_entry(list, struct evsel, core.node); |
| 1839 | } |
| 1840 | |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1841 | void parse_events__set_leader(char *name, struct list_head *list, |
| 1842 | struct parse_events_state *parse_state) |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1843 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1844 | struct evsel *leader; |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1845 | |
Wang Nan | 854f736 | 2015-09-06 07:13:17 +0000 | [diff] [blame] | 1846 | if (list_empty(list)) { |
| 1847 | WARN_ONCE(true, "WARNING: failed to set leader: empty list"); |
| 1848 | return; |
| 1849 | } |
| 1850 | |
Kan Liang | 3cdc5c2 | 2018-05-07 14:13:43 -0700 | [diff] [blame] | 1851 | if (parse_events__set_leader_for_uncore_aliase(name, list, parse_state)) |
| 1852 | return; |
| 1853 | |
Ian Rogers | 94dbfd6 | 2021-11-30 09:49:45 -0800 | [diff] [blame] | 1854 | leader = arch_evlist__leader(list); |
Ian Rogers | ecdcf63 | 2021-11-30 09:49:44 -0800 | [diff] [blame] | 1855 | __perf_evlist__set_leader(list, &leader->core); |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 1856 | leader->group_name = name ? strdup(name) : NULL; |
Ian Rogers | 94dbfd6 | 2021-11-30 09:49:45 -0800 | [diff] [blame] | 1857 | list_move(&leader->core.node, list); |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 1858 | } |
| 1859 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 1860 | /* list_event is assumed to point to malloc'ed memory */ |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 1861 | void parse_events_update_lists(struct list_head *list_event, |
| 1862 | struct list_head *list_all) |
| 1863 | { |
| 1864 | /* |
| 1865 | * Called for single event definition. Update the |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 1866 | * 'all event' list, and reinit the 'single event' |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 1867 | * list, for next event definition. |
| 1868 | */ |
| 1869 | list_splice_tail(list_event, list_all); |
Jiri Olsa | b847cbd | 2012-05-21 09:12:51 +0200 | [diff] [blame] | 1870 | free(list_event); |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 1871 | } |
| 1872 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1873 | struct event_modifier { |
| 1874 | int eu; |
| 1875 | int ek; |
| 1876 | int eh; |
| 1877 | int eH; |
| 1878 | int eG; |
Jiri Olsa | a1e12da | 2015-04-07 23:25:14 +0200 | [diff] [blame] | 1879 | int eI; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1880 | int precise; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1881 | int precise_max; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1882 | int exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1883 | int sample_read; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1884 | int pinned; |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 1885 | int weak; |
Andi Kleen | 0997a26 | 2020-10-14 07:42:55 -0700 | [diff] [blame] | 1886 | int exclusive; |
Song Liu | 01bd8efc | 2021-04-25 14:43:32 -0700 | [diff] [blame] | 1887 | int bpf_counter; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1888 | }; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1889 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1890 | static int get_event_modifier(struct event_modifier *mod, char *str, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 1891 | struct evsel *evsel) |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1892 | { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 1893 | int eu = evsel ? evsel->core.attr.exclude_user : 0; |
| 1894 | int ek = evsel ? evsel->core.attr.exclude_kernel : 0; |
| 1895 | int eh = evsel ? evsel->core.attr.exclude_hv : 0; |
| 1896 | int eH = evsel ? evsel->core.attr.exclude_host : 0; |
| 1897 | int eG = evsel ? evsel->core.attr.exclude_guest : 0; |
| 1898 | int eI = evsel ? evsel->core.attr.exclude_idle : 0; |
| 1899 | int precise = evsel ? evsel->core.attr.precise_ip : 0; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1900 | int precise_max = 0; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1901 | int sample_read = 0; |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 1902 | int pinned = evsel ? evsel->core.attr.pinned : 0; |
Andi Kleen | 0997a26 | 2020-10-14 07:42:55 -0700 | [diff] [blame] | 1903 | int exclusive = evsel ? evsel->core.attr.exclusive : 0; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1904 | |
| 1905 | int exclude = eu | ek | eh; |
| 1906 | int exclude_GH = evsel ? evsel->exclude_GH : 0; |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 1907 | int weak = 0; |
Song Liu | 01bd8efc | 2021-04-25 14:43:32 -0700 | [diff] [blame] | 1908 | int bpf_counter = 0; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1909 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1910 | memset(mod, 0, sizeof(*mod)); |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 1911 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1912 | while (*str) { |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1913 | if (*str == 'u') { |
| 1914 | if (!exclude) |
| 1915 | exclude = eu = ek = eh = 1; |
Jin Yao | 943b69a | 2020-08-14 09:21:20 +0800 | [diff] [blame] | 1916 | if (!exclude_GH && !perf_guest) |
| 1917 | eG = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1918 | eu = 0; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1919 | } else if (*str == 'k') { |
| 1920 | if (!exclude) |
| 1921 | exclude = eu = ek = eh = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1922 | ek = 0; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1923 | } else if (*str == 'h') { |
| 1924 | if (!exclude) |
| 1925 | exclude = eu = ek = eh = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1926 | eh = 0; |
Joerg Roedel | 99320cc | 2012-01-04 17:54:19 +0100 | [diff] [blame] | 1927 | } else if (*str == 'G') { |
| 1928 | if (!exclude_GH) |
| 1929 | exclude_GH = eG = eH = 1; |
| 1930 | eG = 0; |
| 1931 | } else if (*str == 'H') { |
| 1932 | if (!exclude_GH) |
| 1933 | exclude_GH = eG = eH = 1; |
| 1934 | eH = 0; |
Jiri Olsa | a1e12da | 2015-04-07 23:25:14 +0200 | [diff] [blame] | 1935 | } else if (*str == 'I') { |
| 1936 | eI = 1; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1937 | } else if (*str == 'p') { |
| 1938 | precise++; |
David Ahern | 1342798 | 2012-09-13 14:59:13 -0600 | [diff] [blame] | 1939 | /* use of precise requires exclude_guest */ |
| 1940 | if (!exclude_GH) |
| 1941 | eG = 1; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1942 | } else if (*str == 'P') { |
| 1943 | precise_max = 1; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1944 | } else if (*str == 'S') { |
| 1945 | sample_read = 1; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1946 | } else if (*str == 'D') { |
| 1947 | pinned = 1; |
Andi Kleen | 0997a26 | 2020-10-14 07:42:55 -0700 | [diff] [blame] | 1948 | } else if (*str == 'e') { |
| 1949 | exclusive = 1; |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 1950 | } else if (*str == 'W') { |
| 1951 | weak = 1; |
Song Liu | 01bd8efc | 2021-04-25 14:43:32 -0700 | [diff] [blame] | 1952 | } else if (*str == 'b') { |
| 1953 | bpf_counter = 1; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1954 | } else |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1955 | break; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 1956 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 1957 | ++str; |
| 1958 | } |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 1959 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1960 | /* |
| 1961 | * precise ip: |
| 1962 | * |
| 1963 | * 0 - SAMPLE_IP can have arbitrary skid |
| 1964 | * 1 - SAMPLE_IP must have constant skid |
| 1965 | * 2 - SAMPLE_IP requested to have 0 skid |
| 1966 | * 3 - SAMPLE_IP must have 0 skid |
| 1967 | * |
| 1968 | * See also PERF_RECORD_MISC_EXACT_IP |
| 1969 | */ |
| 1970 | if (precise > 3) |
| 1971 | return -EINVAL; |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 1972 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1973 | mod->eu = eu; |
| 1974 | mod->ek = ek; |
| 1975 | mod->eh = eh; |
| 1976 | mod->eH = eH; |
| 1977 | mod->eG = eG; |
Jiri Olsa | a1e12da | 2015-04-07 23:25:14 +0200 | [diff] [blame] | 1978 | mod->eI = eI; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1979 | mod->precise = precise; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 1980 | mod->precise_max = precise_max; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1981 | mod->exclude_GH = exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 1982 | mod->sample_read = sample_read; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1983 | mod->pinned = pinned; |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 1984 | mod->weak = weak; |
Song Liu | 01bd8efc | 2021-04-25 14:43:32 -0700 | [diff] [blame] | 1985 | mod->bpf_counter = bpf_counter; |
Andi Kleen | 0997a26 | 2020-10-14 07:42:55 -0700 | [diff] [blame] | 1986 | mod->exclusive = exclusive; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 1987 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 1988 | return 0; |
| 1989 | } |
| 1990 | |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 1991 | /* |
| 1992 | * Basic modifier sanity check to validate it contains only one |
| 1993 | * instance of any modifier (apart from 'p') present. |
| 1994 | */ |
| 1995 | static int check_modifier(char *str) |
| 1996 | { |
| 1997 | char *p = str; |
| 1998 | |
| 1999 | /* The sizeof includes 0 byte as well. */ |
Song Liu | 01bd8efc | 2021-04-25 14:43:32 -0700 | [diff] [blame] | 2000 | if (strlen(str) > (sizeof("ukhGHpppPSDIWeb") - 1)) |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 2001 | return -1; |
| 2002 | |
| 2003 | while (*p) { |
| 2004 | if (*p != 'p' && strchr(p + 1, *p)) |
| 2005 | return -1; |
| 2006 | p++; |
| 2007 | } |
| 2008 | |
| 2009 | return 0; |
| 2010 | } |
| 2011 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 2012 | int parse_events__modifier_event(struct list_head *list, char *str, bool add) |
| 2013 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2014 | struct evsel *evsel; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 2015 | struct event_modifier mod; |
| 2016 | |
| 2017 | if (str == NULL) |
| 2018 | return 0; |
| 2019 | |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 2020 | if (check_modifier(str)) |
| 2021 | return -EINVAL; |
| 2022 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 2023 | if (!add && get_event_modifier(&mod, str, NULL)) |
| 2024 | return -EINVAL; |
| 2025 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2026 | __evlist__for_each_entry(list, evsel) { |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 2027 | if (add && get_event_modifier(&mod, str, evsel)) |
| 2028 | return -EINVAL; |
| 2029 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 2030 | evsel->core.attr.exclude_user = mod.eu; |
| 2031 | evsel->core.attr.exclude_kernel = mod.ek; |
| 2032 | evsel->core.attr.exclude_hv = mod.eh; |
| 2033 | evsel->core.attr.precise_ip = mod.precise; |
| 2034 | evsel->core.attr.exclude_host = mod.eH; |
| 2035 | evsel->core.attr.exclude_guest = mod.eG; |
| 2036 | evsel->core.attr.exclude_idle = mod.eI; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 2037 | evsel->exclude_GH = mod.exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 2038 | evsel->sample_read = mod.sample_read; |
Jiri Olsa | 7f94af7 | 2015-10-05 20:06:05 +0200 | [diff] [blame] | 2039 | evsel->precise_max = mod.precise_max; |
Andi Kleen | 5a5dfe4 | 2017-08-31 12:40:26 -0700 | [diff] [blame] | 2040 | evsel->weak_group = mod.weak; |
Song Liu | 01bd8efc | 2021-04-25 14:43:32 -0700 | [diff] [blame] | 2041 | evsel->bpf_counter = mod.bpf_counter; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 2042 | |
Andi Kleen | 0997a26 | 2020-10-14 07:42:55 -0700 | [diff] [blame] | 2043 | if (evsel__is_group_leader(evsel)) { |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 2044 | evsel->core.attr.pinned = mod.pinned; |
Andi Kleen | 0997a26 | 2020-10-14 07:42:55 -0700 | [diff] [blame] | 2045 | evsel->core.attr.exclusive = mod.exclusive; |
| 2046 | } |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 2047 | } |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 2048 | |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 2049 | return 0; |
| 2050 | } |
| 2051 | |
Ian Rogers | 8e8bbfb | 2021-10-15 10:21:24 -0700 | [diff] [blame] | 2052 | int parse_events_name(struct list_head *list, const char *name) |
Robert Richter | ac2ba9f | 2012-08-16 21:10:21 +0200 | [diff] [blame] | 2053 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2054 | struct evsel *evsel; |
Robert Richter | ac2ba9f | 2012-08-16 21:10:21 +0200 | [diff] [blame] | 2055 | |
Arnaldo Carvalho de Melo | e5cadb9 | 2016-06-23 11:26:15 -0300 | [diff] [blame] | 2056 | __evlist__for_each_entry(list, evsel) { |
Robert Richter | ac2ba9f | 2012-08-16 21:10:21 +0200 | [diff] [blame] | 2057 | if (!evsel->name) |
| 2058 | evsel->name = strdup(name); |
| 2059 | } |
| 2060 | |
| 2061 | return 0; |
| 2062 | } |
| 2063 | |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2064 | static int |
| 2065 | comp_pmu(const void *p1, const void *p2) |
| 2066 | { |
| 2067 | struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; |
| 2068 | struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; |
| 2069 | |
Andi Kleen | e312bcf | 2016-09-15 15:24:53 -0700 | [diff] [blame] | 2070 | return strcasecmp(pmu1->symbol, pmu2->symbol); |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | static void perf_pmu__parse_cleanup(void) |
| 2074 | { |
| 2075 | if (perf_pmu_events_list_num > 0) { |
| 2076 | struct perf_pmu_event_symbol *p; |
| 2077 | int i; |
| 2078 | |
| 2079 | for (i = 0; i < perf_pmu_events_list_num; i++) { |
| 2080 | p = perf_pmu_events_list + i; |
Taeung Song | 360e071 | 2017-02-01 21:34:07 +0900 | [diff] [blame] | 2081 | zfree(&p->symbol); |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2082 | } |
Taeung Song | 506fde1 | 2017-02-01 21:34:06 +0900 | [diff] [blame] | 2083 | zfree(&perf_pmu_events_list); |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2084 | perf_pmu_events_list_num = 0; |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | #define SET_SYMBOL(str, stype) \ |
| 2089 | do { \ |
| 2090 | p->symbol = str; \ |
| 2091 | if (!p->symbol) \ |
| 2092 | goto err; \ |
| 2093 | p->type = stype; \ |
| 2094 | } while (0) |
| 2095 | |
| 2096 | /* |
| 2097 | * Read the pmu events list from sysfs |
| 2098 | * Save it into perf_pmu_events_list |
| 2099 | */ |
| 2100 | static void perf_pmu__parse_init(void) |
| 2101 | { |
| 2102 | |
| 2103 | struct perf_pmu *pmu = NULL; |
| 2104 | struct perf_pmu_alias *alias; |
| 2105 | int len = 0; |
| 2106 | |
Andi Kleen | 231bb2a | 2017-01-27 18:03:39 -0800 | [diff] [blame] | 2107 | pmu = NULL; |
| 2108 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
| 2109 | list_for_each_entry(alias, &pmu->aliases, list) { |
John Garry | 864bc8c | 2022-01-17 23:10:13 +0800 | [diff] [blame] | 2110 | char *tmp = strchr(alias->name, '-'); |
| 2111 | |
| 2112 | if (tmp) { |
| 2113 | char *tmp2 = NULL; |
| 2114 | |
| 2115 | tmp2 = strchr(tmp + 1, '-'); |
Andi Kleen | 231bb2a | 2017-01-27 18:03:39 -0800 | [diff] [blame] | 2116 | len++; |
John Garry | 864bc8c | 2022-01-17 23:10:13 +0800 | [diff] [blame] | 2117 | if (tmp2) |
| 2118 | len++; |
| 2119 | } |
| 2120 | |
Andi Kleen | 231bb2a | 2017-01-27 18:03:39 -0800 | [diff] [blame] | 2121 | len++; |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | if (len == 0) { |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2126 | perf_pmu_events_list_num = -1; |
| 2127 | return; |
| 2128 | } |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2129 | perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); |
| 2130 | if (!perf_pmu_events_list) |
| 2131 | return; |
| 2132 | perf_pmu_events_list_num = len; |
| 2133 | |
| 2134 | len = 0; |
Andi Kleen | 231bb2a | 2017-01-27 18:03:39 -0800 | [diff] [blame] | 2135 | pmu = NULL; |
| 2136 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
| 2137 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 2138 | struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; |
| 2139 | char *tmp = strchr(alias->name, '-'); |
John Garry | 864bc8c | 2022-01-17 23:10:13 +0800 | [diff] [blame] | 2140 | char *tmp2 = NULL; |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2141 | |
John Garry | 864bc8c | 2022-01-17 23:10:13 +0800 | [diff] [blame] | 2142 | if (tmp) |
| 2143 | tmp2 = strchr(tmp + 1, '-'); |
| 2144 | if (tmp2) { |
| 2145 | SET_SYMBOL(strndup(alias->name, tmp - alias->name), |
| 2146 | PMU_EVENT_SYMBOL_PREFIX); |
| 2147 | p++; |
| 2148 | tmp++; |
| 2149 | SET_SYMBOL(strndup(tmp, tmp2 - tmp), PMU_EVENT_SYMBOL_SUFFIX); |
| 2150 | p++; |
| 2151 | SET_SYMBOL(strdup(++tmp2), PMU_EVENT_SYMBOL_SUFFIX2); |
| 2152 | len += 3; |
| 2153 | } else if (tmp) { |
Andi Kleen | 231bb2a | 2017-01-27 18:03:39 -0800 | [diff] [blame] | 2154 | SET_SYMBOL(strndup(alias->name, tmp - alias->name), |
| 2155 | PMU_EVENT_SYMBOL_PREFIX); |
| 2156 | p++; |
| 2157 | SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); |
| 2158 | len += 2; |
| 2159 | } else { |
| 2160 | SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); |
| 2161 | len++; |
| 2162 | } |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2163 | } |
| 2164 | } |
| 2165 | qsort(perf_pmu_events_list, len, |
| 2166 | sizeof(struct perf_pmu_event_symbol), comp_pmu); |
| 2167 | |
| 2168 | return; |
| 2169 | err: |
| 2170 | perf_pmu__parse_cleanup(); |
| 2171 | } |
| 2172 | |
Jiri Olsa | 4929e95 | 2020-07-26 09:52:44 +0200 | [diff] [blame] | 2173 | /* |
| 2174 | * This function injects special term in |
| 2175 | * perf_pmu_events_list so the test code |
| 2176 | * can check on this functionality. |
| 2177 | */ |
| 2178 | int perf_pmu__test_parse_init(void) |
| 2179 | { |
John Garry | b4a7276 | 2022-01-17 23:10:15 +0800 | [diff] [blame] | 2180 | struct perf_pmu_event_symbol *list, *tmp, symbols[] = { |
| 2181 | {(char *)"read", PMU_EVENT_SYMBOL}, |
| 2182 | {(char *)"event", PMU_EVENT_SYMBOL_PREFIX}, |
| 2183 | {(char *)"two", PMU_EVENT_SYMBOL_SUFFIX}, |
| 2184 | {(char *)"hyphen", PMU_EVENT_SYMBOL_SUFFIX}, |
| 2185 | {(char *)"hyph", PMU_EVENT_SYMBOL_SUFFIX2}, |
| 2186 | }; |
| 2187 | unsigned long i, j; |
Jiri Olsa | 4929e95 | 2020-07-26 09:52:44 +0200 | [diff] [blame] | 2188 | |
John Garry | b4a7276 | 2022-01-17 23:10:15 +0800 | [diff] [blame] | 2189 | tmp = list = malloc(sizeof(*list) * ARRAY_SIZE(symbols)); |
Jiri Olsa | 4929e95 | 2020-07-26 09:52:44 +0200 | [diff] [blame] | 2190 | if (!list) |
| 2191 | return -ENOMEM; |
| 2192 | |
John Garry | b4a7276 | 2022-01-17 23:10:15 +0800 | [diff] [blame] | 2193 | for (i = 0; i < ARRAY_SIZE(symbols); i++, tmp++) { |
| 2194 | tmp->type = symbols[i].type; |
| 2195 | tmp->symbol = strdup(symbols[i].symbol); |
| 2196 | if (!list->symbol) |
| 2197 | goto err_free; |
Jiri Olsa | 4929e95 | 2020-07-26 09:52:44 +0200 | [diff] [blame] | 2198 | } |
| 2199 | |
| 2200 | perf_pmu_events_list = list; |
John Garry | b4a7276 | 2022-01-17 23:10:15 +0800 | [diff] [blame] | 2201 | perf_pmu_events_list_num = ARRAY_SIZE(symbols); |
| 2202 | |
| 2203 | qsort(perf_pmu_events_list, ARRAY_SIZE(symbols), |
| 2204 | sizeof(struct perf_pmu_event_symbol), comp_pmu); |
Jiri Olsa | 4929e95 | 2020-07-26 09:52:44 +0200 | [diff] [blame] | 2205 | return 0; |
John Garry | b4a7276 | 2022-01-17 23:10:15 +0800 | [diff] [blame] | 2206 | |
| 2207 | err_free: |
| 2208 | for (j = 0, tmp = list; j < i; j++, tmp++) |
| 2209 | free(tmp->symbol); |
| 2210 | free(list); |
| 2211 | return -ENOMEM; |
Jiri Olsa | 4929e95 | 2020-07-26 09:52:44 +0200 | [diff] [blame] | 2212 | } |
| 2213 | |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2214 | enum perf_pmu_event_symbol_type |
| 2215 | perf_pmu__parse_check(const char *name) |
| 2216 | { |
| 2217 | struct perf_pmu_event_symbol p, *r; |
| 2218 | |
| 2219 | /* scan kernel pmu events from sysfs if needed */ |
| 2220 | if (perf_pmu_events_list_num == 0) |
| 2221 | perf_pmu__parse_init(); |
| 2222 | /* |
| 2223 | * name "cpu" could be prefix of cpu-cycles or cpu// events. |
| 2224 | * cpu-cycles has been handled by hardcode. |
| 2225 | * So it must be cpu// events, not kernel pmu event. |
| 2226 | */ |
| 2227 | if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) |
| 2228 | return PMU_EVENT_SYMBOL_ERR; |
| 2229 | |
| 2230 | p.symbol = strdup(name); |
| 2231 | r = bsearch(&p, perf_pmu_events_list, |
| 2232 | (size_t) perf_pmu_events_list_num, |
| 2233 | sizeof(struct perf_pmu_event_symbol), comp_pmu); |
Taeung Song | 360e071 | 2017-02-01 21:34:07 +0900 | [diff] [blame] | 2234 | zfree(&p.symbol); |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2235 | return r ? r->type : PMU_EVENT_SYMBOL_ERR; |
| 2236 | } |
| 2237 | |
Jiri Olsa | 1244a32 | 2020-05-25 00:42:08 +0200 | [diff] [blame] | 2238 | static int parse_events__scanner(const char *str, |
| 2239 | struct parse_events_state *parse_state) |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 2240 | { |
| 2241 | YY_BUFFER_STATE buffer; |
| 2242 | void *scanner; |
| 2243 | int ret; |
| 2244 | |
Jiri Olsa | 1244a32 | 2020-05-25 00:42:08 +0200 | [diff] [blame] | 2245 | ret = parse_events_lex_init_extra(parse_state, &scanner); |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 2246 | if (ret) |
| 2247 | return ret; |
| 2248 | |
| 2249 | buffer = parse_events__scan_string(str, scanner); |
| 2250 | |
| 2251 | #ifdef PARSER_DEBUG |
| 2252 | parse_events_debug = 1; |
Jiri Olsa | ae76264 | 2020-05-20 09:40:50 +0200 | [diff] [blame] | 2253 | parse_events_set_debug(1, scanner); |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 2254 | #endif |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 2255 | ret = parse_events_parse(parse_state, scanner); |
Zheng Yan | ac20de6f | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 2256 | |
| 2257 | parse_events__flush_buffer(buffer, scanner); |
| 2258 | parse_events__delete_buffer(buffer, scanner); |
| 2259 | parse_events_lex_destroy(scanner); |
| 2260 | return ret; |
| 2261 | } |
| 2262 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 2263 | /* |
| 2264 | * parse event config string, return a list of event terms. |
| 2265 | */ |
| 2266 | int parse_events_terms(struct list_head *terms, const char *str) |
| 2267 | { |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 2268 | struct parse_events_state parse_state = { |
Jiri Olsa | 1244a32 | 2020-05-25 00:42:08 +0200 | [diff] [blame] | 2269 | .terms = NULL, |
| 2270 | .stoken = PE_START_TERMS, |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 2271 | }; |
| 2272 | int ret; |
| 2273 | |
Jiri Olsa | 1244a32 | 2020-05-25 00:42:08 +0200 | [diff] [blame] | 2274 | ret = parse_events__scanner(str, &parse_state); |
Jiri Olsa | 4929e95 | 2020-07-26 09:52:44 +0200 | [diff] [blame] | 2275 | perf_pmu__parse_cleanup(); |
| 2276 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 2277 | if (!ret) { |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 2278 | list_splice(parse_state.terms, terms); |
| 2279 | zfree(&parse_state.terms); |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 2280 | return 0; |
| 2281 | } |
| 2282 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 2283 | parse_events_terms__delete(parse_state.terms); |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 2284 | return ret; |
| 2285 | } |
| 2286 | |
Jin Yao | 5e4edd1 | 2021-04-27 15:01:25 +0800 | [diff] [blame] | 2287 | static int parse_events__with_hybrid_pmu(struct parse_events_state *parse_state, |
| 2288 | const char *str, char *pmu_name, |
| 2289 | struct list_head *list) |
| 2290 | { |
| 2291 | struct parse_events_state ps = { |
| 2292 | .list = LIST_HEAD_INIT(ps.list), |
| 2293 | .stoken = PE_START_EVENTS, |
| 2294 | .hybrid_pmu_name = pmu_name, |
| 2295 | .idx = parse_state->idx, |
| 2296 | }; |
| 2297 | int ret; |
| 2298 | |
| 2299 | ret = parse_events__scanner(str, &ps); |
| 2300 | perf_pmu__parse_cleanup(); |
| 2301 | |
| 2302 | if (!ret) { |
| 2303 | if (!list_empty(&ps.list)) { |
| 2304 | list_splice(&ps.list, list); |
| 2305 | parse_state->idx = ps.idx; |
| 2306 | return 0; |
| 2307 | } else |
| 2308 | return -1; |
| 2309 | } |
| 2310 | |
| 2311 | return ret; |
| 2312 | } |
| 2313 | |
Arnaldo Carvalho de Melo | 3bf91aa | 2020-06-03 12:32:55 -0300 | [diff] [blame] | 2314 | int __parse_events(struct evlist *evlist, const char *str, |
| 2315 | struct parse_events_error *err, struct perf_pmu *fake_pmu) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2316 | { |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 2317 | struct parse_events_state parse_state = { |
Arnaldo Carvalho de Melo | 3bf91aa | 2020-06-03 12:32:55 -0300 | [diff] [blame] | 2318 | .list = LIST_HEAD_INIT(parse_state.list), |
| 2319 | .idx = evlist->core.nr_entries, |
| 2320 | .error = err, |
| 2321 | .evlist = evlist, |
| 2322 | .stoken = PE_START_EVENTS, |
| 2323 | .fake_pmu = fake_pmu, |
Jiri Olsa | 46010ab | 2012-06-15 14:31:38 +0800 | [diff] [blame] | 2324 | }; |
Jiri Olsa | 46010ab | 2012-06-15 14:31:38 +0800 | [diff] [blame] | 2325 | int ret; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2326 | |
Jiri Olsa | 1244a32 | 2020-05-25 00:42:08 +0200 | [diff] [blame] | 2327 | ret = parse_events__scanner(str, &parse_state); |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 2328 | perf_pmu__parse_cleanup(); |
Ian Rogers | 8e8714c | 2019-10-25 11:08:22 -0700 | [diff] [blame] | 2329 | |
| 2330 | if (!ret && list_empty(&parse_state.list)) { |
| 2331 | WARN_ONCE(true, "WARNING: event parser found nothing\n"); |
| 2332 | return -1; |
| 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * Add list to the evlist even with errors to allow callers to clean up. |
| 2337 | */ |
Arnaldo Carvalho de Melo | e414fd1 | 2020-11-30 14:52:44 -0300 | [diff] [blame] | 2338 | evlist__splice_list_tail(evlist, &parse_state.list); |
Ian Rogers | 8e8714c | 2019-10-25 11:08:22 -0700 | [diff] [blame] | 2339 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 2340 | if (!ret) { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2341 | struct evsel *last; |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 2342 | |
Jiri Olsa | 3a68312 | 2021-07-06 17:17:01 +0200 | [diff] [blame] | 2343 | evlist->core.nr_groups += parse_state.nr_groups; |
Jiri Olsa | 515dbe4 | 2019-09-03 10:39:52 +0200 | [diff] [blame] | 2344 | last = evlist__last(evlist); |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 2345 | last->cmdline_group_boundary = true; |
| 2346 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 2347 | return 0; |
| 2348 | } |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 2349 | |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 2350 | /* |
| 2351 | * There are 2 users - builtin-record and builtin-test objects. |
Jiri Olsa | c12995a | 2019-07-21 13:23:56 +0200 | [diff] [blame] | 2352 | * Both call evlist__delete in case of error, so we dont |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 2353 | * need to bother. |
| 2354 | */ |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 2355 | return ret; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2356 | } |
| 2357 | |
Ian Rogers | 07eafd4 | 2021-11-07 01:00:01 -0800 | [diff] [blame] | 2358 | void parse_events_error__init(struct parse_events_error *err) |
| 2359 | { |
| 2360 | bzero(err, sizeof(*err)); |
| 2361 | } |
| 2362 | |
| 2363 | void parse_events_error__exit(struct parse_events_error *err) |
| 2364 | { |
| 2365 | zfree(&err->str); |
| 2366 | zfree(&err->help); |
| 2367 | zfree(&err->first_str); |
| 2368 | zfree(&err->first_help); |
| 2369 | } |
| 2370 | |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 2371 | void parse_events_error__handle(struct parse_events_error *err, int idx, |
| 2372 | char *str, char *help) |
| 2373 | { |
| 2374 | if (WARN(!str, "WARNING: failed to provide error string\n")) { |
| 2375 | free(help); |
| 2376 | return; |
| 2377 | } |
| 2378 | switch (err->num_errors) { |
| 2379 | case 0: |
| 2380 | err->idx = idx; |
| 2381 | err->str = str; |
| 2382 | err->help = help; |
| 2383 | break; |
| 2384 | case 1: |
| 2385 | err->first_idx = err->idx; |
| 2386 | err->idx = idx; |
| 2387 | err->first_str = err->str; |
| 2388 | err->str = str; |
| 2389 | err->first_help = err->help; |
| 2390 | err->help = help; |
| 2391 | break; |
| 2392 | default: |
| 2393 | pr_debug("Multiple errors dropping message: %s (%s)\n", |
| 2394 | err->str, err->help); |
| 2395 | free(err->str); |
| 2396 | err->str = str; |
| 2397 | free(err->help); |
| 2398 | err->help = help; |
| 2399 | break; |
| 2400 | } |
| 2401 | err->num_errors++; |
| 2402 | } |
| 2403 | |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2404 | #define MAX_WIDTH 1000 |
| 2405 | static int get_term_width(void) |
| 2406 | { |
| 2407 | struct winsize ws; |
| 2408 | |
| 2409 | get_term_dimensions(&ws); |
| 2410 | return ws.ws_col > MAX_WIDTH ? MAX_WIDTH : ws.ws_col; |
| 2411 | } |
| 2412 | |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 2413 | static void __parse_events_error__print(int err_idx, const char *err_str, |
| 2414 | const char *err_help, const char *event) |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2415 | { |
| 2416 | const char *str = "invalid or unsupported event: "; |
| 2417 | char _buf[MAX_WIDTH]; |
| 2418 | char *buf = (char *) event; |
| 2419 | int idx = 0; |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2420 | if (err_str) { |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2421 | /* -2 for extra '' in the final fprintf */ |
| 2422 | int width = get_term_width() - 2; |
| 2423 | int len_event = strlen(event); |
| 2424 | int len_str, max_len, cut = 0; |
| 2425 | |
| 2426 | /* |
| 2427 | * Maximum error index indent, we will cut |
| 2428 | * the event string if it's bigger. |
| 2429 | */ |
Adrian Hunter | 141b2d3 | 2015-07-17 19:33:51 +0300 | [diff] [blame] | 2430 | int max_err_idx = 13; |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2431 | |
| 2432 | /* |
| 2433 | * Let's be specific with the message when |
| 2434 | * we have the precise error. |
| 2435 | */ |
| 2436 | str = "event syntax error: "; |
| 2437 | len_str = strlen(str); |
| 2438 | max_len = width - len_str; |
| 2439 | |
| 2440 | buf = _buf; |
| 2441 | |
Adam Buchbinder | bd1a0be5 | 2016-02-24 10:02:25 -0800 | [diff] [blame] | 2442 | /* We're cutting from the beginning. */ |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2443 | if (err_idx > max_err_idx) |
| 2444 | cut = err_idx - max_err_idx; |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2445 | |
| 2446 | strncpy(buf, event + cut, max_len); |
| 2447 | |
| 2448 | /* Mark cut parts with '..' on both sides. */ |
| 2449 | if (cut) |
| 2450 | buf[0] = buf[1] = '.'; |
| 2451 | |
| 2452 | if ((len_event - cut) > max_len) { |
| 2453 | buf[max_len - 1] = buf[max_len - 2] = '.'; |
| 2454 | buf[max_len] = 0; |
| 2455 | } |
| 2456 | |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2457 | idx = len_str + err_idx - cut; |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | fprintf(stderr, "%s'%s'\n", str, buf); |
| 2461 | if (idx) { |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2462 | fprintf(stderr, "%*s\\___ %s\n", idx + 1, "", err_str); |
| 2463 | if (err_help) |
| 2464 | fprintf(stderr, "\n%s\n", err_help); |
| 2465 | } |
| 2466 | } |
| 2467 | |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 2468 | void parse_events_error__print(struct parse_events_error *err, |
| 2469 | const char *event) |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2470 | { |
| 2471 | if (!err->num_errors) |
| 2472 | return; |
| 2473 | |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 2474 | __parse_events_error__print(err->idx, err->str, err->help, event); |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2475 | |
| 2476 | if (err->num_errors > 1) { |
| 2477 | fputs("\nInitial error:\n", stderr); |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 2478 | __parse_events_error__print(err->first_idx, err->first_str, |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2479 | err->first_help, event); |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2480 | } |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | #undef MAX_WIDTH |
| 2484 | |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 2485 | int parse_events_option(const struct option *opt, const char *str, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 2486 | int unset __maybe_unused) |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 2487 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2488 | struct evlist *evlist = *(struct evlist **)opt->value; |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2489 | struct parse_events_error err; |
| 2490 | int ret; |
| 2491 | |
Ian Rogers | 07eafd4 | 2021-11-07 01:00:01 -0800 | [diff] [blame] | 2492 | parse_events_error__init(&err); |
Ian Rogers | a910e46 | 2019-11-15 23:46:52 -0800 | [diff] [blame] | 2493 | ret = parse_events(evlist, str, &err); |
Andi Kleen | 9175ce1 | 2012-10-26 13:30:06 -0700 | [diff] [blame] | 2494 | |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 2495 | if (ret) { |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 2496 | parse_events_error__print(&err, str); |
Andi Kleen | 333b566 | 2017-09-13 14:50:06 -0700 | [diff] [blame] | 2497 | fprintf(stderr, "Run 'perf list' for a list of valid events\n"); |
| 2498 | } |
Ian Rogers | 07eafd4 | 2021-11-07 01:00:01 -0800 | [diff] [blame] | 2499 | parse_events_error__exit(&err); |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 2500 | |
Andi Kleen | 9175ce1 | 2012-10-26 13:30:06 -0700 | [diff] [blame] | 2501 | return ret; |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 2502 | } |
| 2503 | |
Arnaldo Carvalho de Melo | d0abbc3 | 2020-04-27 13:58:11 -0300 | [diff] [blame] | 2504 | int parse_events_option_new_evlist(const struct option *opt, const char *str, int unset) |
| 2505 | { |
| 2506 | struct evlist **evlistp = opt->value; |
| 2507 | int ret; |
| 2508 | |
| 2509 | if (*evlistp == NULL) { |
| 2510 | *evlistp = evlist__new(); |
| 2511 | |
| 2512 | if (*evlistp == NULL) { |
| 2513 | fprintf(stderr, "Not enough memory to create evlist\n"); |
| 2514 | return -1; |
| 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | ret = parse_events_option(opt, str, unset); |
| 2519 | if (ret) { |
| 2520 | evlist__delete(*evlistp); |
| 2521 | *evlistp = NULL; |
| 2522 | } |
| 2523 | |
| 2524 | return ret; |
| 2525 | } |
| 2526 | |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2527 | static int |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2528 | foreach_evsel_in_last_glob(struct evlist *evlist, |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2529 | int (*func)(struct evsel *evsel, |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2530 | const void *arg), |
| 2531 | const void *arg) |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 2532 | { |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2533 | struct evsel *last = NULL; |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2534 | int err; |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 2535 | |
Wang Nan | 854f736 | 2015-09-06 07:13:17 +0000 | [diff] [blame] | 2536 | /* |
| 2537 | * Don't return when list_empty, give func a chance to report |
| 2538 | * error when it found last == NULL. |
| 2539 | * |
| 2540 | * So no need to WARN here, let *func do this. |
| 2541 | */ |
Jiri Olsa | 6484d2f | 2019-07-21 13:24:28 +0200 | [diff] [blame] | 2542 | if (evlist->core.nr_entries > 0) |
Jiri Olsa | 515dbe4 | 2019-09-03 10:39:52 +0200 | [diff] [blame] | 2543 | last = evlist__last(evlist); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 2544 | |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 2545 | do { |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2546 | err = (*func)(last, arg); |
| 2547 | if (err) |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 2548 | return -1; |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2549 | if (!last) |
| 2550 | return 0; |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 2551 | |
Jiri Olsa | ce9036a | 2019-07-21 13:24:23 +0200 | [diff] [blame] | 2552 | if (last->core.node.prev == &evlist->core.entries) |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 2553 | return 0; |
Jiri Olsa | b27c4ec | 2019-07-21 13:24:22 +0200 | [diff] [blame] | 2554 | last = list_entry(last->core.node.prev, struct evsel, core.node); |
Wang Nan | 15bfd2c | 2015-07-10 07:36:09 +0000 | [diff] [blame] | 2555 | } while (!last->cmdline_group_boundary); |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 2556 | |
| 2557 | return 0; |
| 2558 | } |
| 2559 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2560 | static int set_filter(struct evsel *evsel, const void *arg) |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2561 | { |
| 2562 | const char *str = arg; |
Mathieu Poirier | 1e85748 | 2016-09-16 08:44:05 -0600 | [diff] [blame] | 2563 | bool found = false; |
| 2564 | int nr_addr_filters = 0; |
| 2565 | struct perf_pmu *pmu = NULL; |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2566 | |
Jack Henschel | 49836f7 | 2018-07-04 14:13:45 +0200 | [diff] [blame] | 2567 | if (evsel == NULL) { |
| 2568 | fprintf(stderr, |
| 2569 | "--filter option should follow a -e tracepoint or HW tracer option\n"); |
| 2570 | return -1; |
| 2571 | } |
Mathieu Poirier | 1e85748 | 2016-09-16 08:44:05 -0600 | [diff] [blame] | 2572 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 2573 | if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) { |
Arnaldo Carvalho de Melo | ad681ad | 2020-04-29 16:19:05 -0300 | [diff] [blame] | 2574 | if (evsel__append_tp_filter(evsel, str) < 0) { |
Mathieu Poirier | 1e85748 | 2016-09-16 08:44:05 -0600 | [diff] [blame] | 2575 | fprintf(stderr, |
| 2576 | "not enough memory to hold filter string\n"); |
| 2577 | return -1; |
| 2578 | } |
| 2579 | |
| 2580 | return 0; |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
Mathieu Poirier | 1e85748 | 2016-09-16 08:44:05 -0600 | [diff] [blame] | 2583 | while ((pmu = perf_pmu__scan(pmu)) != NULL) |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 2584 | if (pmu->type == evsel->core.attr.type) { |
Mathieu Poirier | 1e85748 | 2016-09-16 08:44:05 -0600 | [diff] [blame] | 2585 | found = true; |
| 2586 | break; |
| 2587 | } |
| 2588 | |
| 2589 | if (found) |
| 2590 | perf_pmu__scan_file(pmu, "nr_addr_filters", |
| 2591 | "%d", &nr_addr_filters); |
| 2592 | |
Jack Henschel | 49836f7 | 2018-07-04 14:13:45 +0200 | [diff] [blame] | 2593 | if (!nr_addr_filters) { |
| 2594 | fprintf(stderr, |
| 2595 | "This CPU does not support address filtering\n"); |
| 2596 | return -1; |
| 2597 | } |
Mathieu Poirier | 1e85748 | 2016-09-16 08:44:05 -0600 | [diff] [blame] | 2598 | |
Arnaldo Carvalho de Melo | ad681ad | 2020-04-29 16:19:05 -0300 | [diff] [blame] | 2599 | if (evsel__append_addr_filter(evsel, str) < 0) { |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2600 | fprintf(stderr, |
| 2601 | "not enough memory to hold filter string\n"); |
| 2602 | return -1; |
| 2603 | } |
| 2604 | |
| 2605 | return 0; |
| 2606 | } |
| 2607 | |
| 2608 | int parse_filter(const struct option *opt, const char *str, |
| 2609 | int unset __maybe_unused) |
| 2610 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2611 | struct evlist *evlist = *(struct evlist **)opt->value; |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2612 | |
| 2613 | return foreach_evsel_in_last_glob(evlist, set_filter, |
| 2614 | (const void *)str); |
| 2615 | } |
| 2616 | |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2617 | static int add_exclude_perf_filter(struct evsel *evsel, |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2618 | const void *arg __maybe_unused) |
| 2619 | { |
| 2620 | char new_filter[64]; |
| 2621 | |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 2622 | if (evsel == NULL || evsel->core.attr.type != PERF_TYPE_TRACEPOINT) { |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2623 | fprintf(stderr, |
| 2624 | "--exclude-perf option should follow a -e tracepoint option\n"); |
| 2625 | return -1; |
| 2626 | } |
| 2627 | |
| 2628 | snprintf(new_filter, sizeof(new_filter), "common_pid != %d", getpid()); |
| 2629 | |
Arnaldo Carvalho de Melo | ad681ad | 2020-04-29 16:19:05 -0300 | [diff] [blame] | 2630 | if (evsel__append_tp_filter(evsel, new_filter) < 0) { |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2631 | fprintf(stderr, |
| 2632 | "not enough memory to hold filter string\n"); |
| 2633 | return -1; |
| 2634 | } |
| 2635 | |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
| 2639 | int exclude_perf(const struct option *opt, |
| 2640 | const char *arg __maybe_unused, |
| 2641 | int unset __maybe_unused) |
| 2642 | { |
Jiri Olsa | 63503db | 2019-07-21 13:23:52 +0200 | [diff] [blame] | 2643 | struct evlist *evlist = *(struct evlist **)opt->value; |
Wang Nan | 4ba1faa | 2015-07-10 07:36:10 +0000 | [diff] [blame] | 2644 | |
| 2645 | return foreach_evsel_in_last_glob(evlist, add_exclude_perf_filter, |
| 2646 | NULL); |
| 2647 | } |
| 2648 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 2649 | static const char * const event_type_descriptors[] = { |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 2650 | "Hardware event", |
| 2651 | "Software event", |
| 2652 | "Tracepoint event", |
| 2653 | "Hardware cache event", |
Liming Wang | 41bdcb2 | 2009-12-29 16:37:07 +0800 | [diff] [blame] | 2654 | "Raw hardware event descriptor", |
| 2655 | "Hardware breakpoint", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 2656 | }; |
| 2657 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2658 | static int cmp_string(const void *a, const void *b) |
| 2659 | { |
| 2660 | const char * const *as = a; |
| 2661 | const char * const *bs = b; |
| 2662 | |
| 2663 | return strcmp(*as, *bs); |
| 2664 | } |
| 2665 | |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2666 | /* |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2667 | * Print the events from <debugfs_mount_point>/tracing/events |
| 2668 | */ |
| 2669 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2670 | void print_tracepoint_events(const char *subsys_glob, const char *event_glob, |
| 2671 | bool name_only) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2672 | { |
| 2673 | DIR *sys_dir, *evt_dir; |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 2674 | struct dirent *sys_dirent, *evt_dirent; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2675 | char evt_path[MAXPATHLEN]; |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2676 | char *dir_path; |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2677 | char **evt_list = NULL; |
| 2678 | unsigned int evt_i = 0, evt_num = 0; |
| 2679 | bool evt_num_known = false; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2680 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2681 | restart: |
Arnaldo Carvalho de Melo | 7014e0e | 2018-05-17 14:42:39 -0300 | [diff] [blame] | 2682 | sys_dir = tracing_events__opendir(); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2683 | if (!sys_dir) |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 2684 | return; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2685 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2686 | if (evt_num_known) { |
| 2687 | evt_list = zalloc(sizeof(char *) * evt_num); |
| 2688 | if (!evt_list) |
| 2689 | goto out_close_sys_dir; |
| 2690 | } |
| 2691 | |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 2692 | for_each_subsystem(sys_dir, sys_dirent) { |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 2693 | if (subsys_glob != NULL && |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 2694 | !strglobmatch(sys_dirent->d_name, subsys_glob)) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2695 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 2696 | |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2697 | dir_path = get_events_file(sys_dirent->d_name); |
| 2698 | if (!dir_path) |
| 2699 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 2700 | evt_dir = opendir(dir_path); |
| 2701 | if (!evt_dir) |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2702 | goto next; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 2703 | |
Arnaldo Carvalho de Melo | c02cab2 | 2018-05-17 14:22:37 -0300 | [diff] [blame] | 2704 | for_each_event(dir_path, evt_dir, evt_dirent) { |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 2705 | if (event_glob != NULL && |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 2706 | !strglobmatch(evt_dirent->d_name, event_glob)) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2707 | continue; |
| 2708 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2709 | if (!evt_num_known) { |
| 2710 | evt_num++; |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2711 | continue; |
| 2712 | } |
| 2713 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2714 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 2715 | sys_dirent->d_name, evt_dirent->d_name); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2716 | |
| 2717 | evt_list[evt_i] = strdup(evt_path); |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2718 | if (evt_list[evt_i] == NULL) { |
| 2719 | put_events_file(dir_path); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2720 | goto out_close_evt_dir; |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2721 | } |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2722 | evt_i++; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2723 | } |
| 2724 | closedir(evt_dir); |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2725 | next: |
| 2726 | put_events_file(dir_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2727 | } |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2728 | closedir(sys_dir); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2729 | |
| 2730 | if (!evt_num_known) { |
| 2731 | evt_num_known = true; |
| 2732 | goto restart; |
| 2733 | } |
| 2734 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); |
| 2735 | evt_i = 0; |
| 2736 | while (evt_i < evt_num) { |
| 2737 | if (name_only) { |
| 2738 | printf("%s ", evt_list[evt_i++]); |
| 2739 | continue; |
| 2740 | } |
| 2741 | printf(" %-50s [%s]\n", evt_list[evt_i++], |
| 2742 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); |
| 2743 | } |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 2744 | if (evt_num && pager_in_use()) |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2745 | printf("\n"); |
| 2746 | |
| 2747 | out_free: |
| 2748 | evt_num = evt_i; |
| 2749 | for (evt_i = 0; evt_i < evt_num; evt_i++) |
| 2750 | zfree(&evt_list[evt_i]); |
| 2751 | zfree(&evt_list); |
| 2752 | return; |
| 2753 | |
| 2754 | out_close_evt_dir: |
| 2755 | closedir(evt_dir); |
| 2756 | out_close_sys_dir: |
| 2757 | closedir(sys_dir); |
| 2758 | |
| 2759 | printf("FATAL: not enough memory to print %s\n", |
| 2760 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); |
| 2761 | if (evt_list) |
| 2762 | goto out_free; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 2763 | } |
| 2764 | |
| 2765 | /* |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2766 | * Check whether event is in <debugfs_mount_point>/tracing/events |
| 2767 | */ |
| 2768 | |
| 2769 | int is_valid_tracepoint(const char *event_string) |
| 2770 | { |
| 2771 | DIR *sys_dir, *evt_dir; |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 2772 | struct dirent *sys_dirent, *evt_dirent; |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2773 | char evt_path[MAXPATHLEN]; |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2774 | char *dir_path; |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2775 | |
Arnaldo Carvalho de Melo | 7014e0e | 2018-05-17 14:42:39 -0300 | [diff] [blame] | 2776 | sys_dir = tracing_events__opendir(); |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2777 | if (!sys_dir) |
| 2778 | return 0; |
| 2779 | |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 2780 | for_each_subsystem(sys_dir, sys_dirent) { |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2781 | dir_path = get_events_file(sys_dirent->d_name); |
| 2782 | if (!dir_path) |
| 2783 | continue; |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2784 | evt_dir = opendir(dir_path); |
| 2785 | if (!evt_dir) |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2786 | goto next; |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2787 | |
Arnaldo Carvalho de Melo | c02cab2 | 2018-05-17 14:22:37 -0300 | [diff] [blame] | 2788 | for_each_event(dir_path, evt_dir, evt_dirent) { |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2789 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
Arnaldo Carvalho de Melo | 22a9f41 | 2016-04-08 11:53:02 -0300 | [diff] [blame] | 2790 | sys_dirent->d_name, evt_dirent->d_name); |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2791 | if (!strcmp(evt_path, event_string)) { |
| 2792 | closedir(evt_dir); |
| 2793 | closedir(sys_dir); |
| 2794 | return 1; |
| 2795 | } |
| 2796 | } |
| 2797 | closedir(evt_dir); |
Arnaldo Carvalho de Melo | 25a7d914 | 2018-05-17 14:27:29 -0300 | [diff] [blame] | 2798 | next: |
| 2799 | put_events_file(dir_path); |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 2800 | } |
| 2801 | closedir(sys_dir); |
| 2802 | return 0; |
| 2803 | } |
| 2804 | |
Jin Yao | 6c93f39 | 2021-09-09 14:18:44 +0800 | [diff] [blame] | 2805 | static bool is_event_supported(u8 type, u64 config) |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2806 | { |
| 2807 | bool ret = true; |
Vince Weaver | 88fee52 | 2013-12-30 15:39:45 -0500 | [diff] [blame] | 2808 | int open_return; |
Jiri Olsa | 32dcd02 | 2019-07-21 13:23:51 +0200 | [diff] [blame] | 2809 | struct evsel *evsel; |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2810 | struct perf_event_attr attr = { |
| 2811 | .type = type, |
| 2812 | .config = config, |
| 2813 | .disabled = 1, |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2814 | }; |
Jiri Olsa | 9749b90 | 2019-07-21 13:23:50 +0200 | [diff] [blame] | 2815 | struct perf_thread_map *tmap = thread_map__new_by_tid(0); |
Arnaldo Carvalho de Melo | 8989605 | 2017-02-14 10:59:04 -0300 | [diff] [blame] | 2816 | |
| 2817 | if (tmap == NULL) |
| 2818 | return false; |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2819 | |
Jiri Olsa | 365c3ae | 2019-07-21 13:23:58 +0200 | [diff] [blame] | 2820 | evsel = evsel__new(&attr); |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2821 | if (evsel) { |
Jiri Olsa | 5972d1e | 2019-07-21 13:24:01 +0200 | [diff] [blame] | 2822 | open_return = evsel__open(evsel, NULL, tmap); |
Vince Weaver | 88fee52 | 2013-12-30 15:39:45 -0500 | [diff] [blame] | 2823 | ret = open_return >= 0; |
| 2824 | |
| 2825 | if (open_return == -EACCES) { |
| 2826 | /* |
| 2827 | * This happens if the paranoid value |
| 2828 | * /proc/sys/kernel/perf_event_paranoid is set to 2 |
| 2829 | * Re-run with exclude_kernel set; we don't do that |
| 2830 | * by default as some ARM machines do not support it. |
| 2831 | * |
| 2832 | */ |
Jiri Olsa | 1fc632c | 2019-07-21 13:24:29 +0200 | [diff] [blame] | 2833 | evsel->core.attr.exclude_kernel = 1; |
Jiri Olsa | 5972d1e | 2019-07-21 13:24:01 +0200 | [diff] [blame] | 2834 | ret = evsel__open(evsel, NULL, tmap) >= 0; |
Vince Weaver | 88fee52 | 2013-12-30 15:39:45 -0500 | [diff] [blame] | 2835 | } |
Jiri Olsa | 5eb2dd2 | 2019-07-21 13:23:57 +0200 | [diff] [blame] | 2836 | evsel__delete(evsel); |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2837 | } |
| 2838 | |
Jiri Olsa | 7836e52 | 2019-07-21 13:24:20 +0200 | [diff] [blame] | 2839 | perf_thread_map__put(tmap); |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2840 | return ret; |
| 2841 | } |
| 2842 | |
Masami Hiramatsu | 40218da | 2016-06-24 18:06:46 +0900 | [diff] [blame] | 2843 | void print_sdt_events(const char *subsys_glob, const char *event_glob, |
| 2844 | bool name_only) |
| 2845 | { |
| 2846 | struct probe_cache *pcache; |
| 2847 | struct probe_cache_entry *ent; |
| 2848 | struct strlist *bidlist, *sdtlist; |
| 2849 | struct strlist_config cfg = {.dont_dupstr = true}; |
| 2850 | struct str_node *nd, *nd2; |
| 2851 | char *buf, *path, *ptr = NULL; |
| 2852 | bool show_detail = false; |
| 2853 | int ret; |
| 2854 | |
| 2855 | sdtlist = strlist__new(NULL, &cfg); |
| 2856 | if (!sdtlist) { |
| 2857 | pr_debug("Failed to allocate new strlist for SDT\n"); |
| 2858 | return; |
| 2859 | } |
| 2860 | bidlist = build_id_cache__list_all(true); |
| 2861 | if (!bidlist) { |
| 2862 | pr_debug("Failed to get buildids: %d\n", errno); |
| 2863 | return; |
| 2864 | } |
| 2865 | strlist__for_each_entry(nd, bidlist) { |
Krister Johansen | f045b8c | 2017-07-05 18:48:11 -0700 | [diff] [blame] | 2866 | pcache = probe_cache__new(nd->s, NULL); |
Masami Hiramatsu | 40218da | 2016-06-24 18:06:46 +0900 | [diff] [blame] | 2867 | if (!pcache) |
| 2868 | continue; |
| 2869 | list_for_each_entry(ent, &pcache->entries, node) { |
| 2870 | if (!ent->sdt) |
| 2871 | continue; |
| 2872 | if (subsys_glob && |
| 2873 | !strglobmatch(ent->pev.group, subsys_glob)) |
| 2874 | continue; |
| 2875 | if (event_glob && |
| 2876 | !strglobmatch(ent->pev.event, event_glob)) |
| 2877 | continue; |
| 2878 | ret = asprintf(&buf, "%s:%s@%s", ent->pev.group, |
| 2879 | ent->pev.event, nd->s); |
| 2880 | if (ret > 0) |
| 2881 | strlist__add(sdtlist, buf); |
| 2882 | } |
| 2883 | probe_cache__delete(pcache); |
| 2884 | } |
| 2885 | strlist__delete(bidlist); |
| 2886 | |
| 2887 | strlist__for_each_entry(nd, sdtlist) { |
| 2888 | buf = strchr(nd->s, '@'); |
| 2889 | if (buf) |
| 2890 | *(buf++) = '\0'; |
| 2891 | if (name_only) { |
| 2892 | printf("%s ", nd->s); |
| 2893 | continue; |
| 2894 | } |
| 2895 | nd2 = strlist__next(nd); |
| 2896 | if (nd2) { |
| 2897 | ptr = strchr(nd2->s, '@'); |
| 2898 | if (ptr) |
| 2899 | *ptr = '\0'; |
| 2900 | if (strcmp(nd->s, nd2->s) == 0) |
| 2901 | show_detail = true; |
| 2902 | } |
| 2903 | if (show_detail) { |
| 2904 | path = build_id_cache__origname(buf); |
| 2905 | ret = asprintf(&buf, "%s@%s(%.12s)", nd->s, path, buf); |
| 2906 | if (ret > 0) { |
| 2907 | printf(" %-50s [%s]\n", buf, "SDT event"); |
| 2908 | free(buf); |
| 2909 | } |
Changbin Du | 8bde851 | 2019-03-16 16:05:46 +0800 | [diff] [blame] | 2910 | free(path); |
Masami Hiramatsu | 40218da | 2016-06-24 18:06:46 +0900 | [diff] [blame] | 2911 | } else |
| 2912 | printf(" %-50s [%s]\n", nd->s, "SDT event"); |
| 2913 | if (nd2) { |
| 2914 | if (strcmp(nd->s, nd2->s) != 0) |
| 2915 | show_detail = false; |
| 2916 | if (ptr) |
| 2917 | *ptr = '@'; |
| 2918 | } |
| 2919 | } |
| 2920 | strlist__delete(sdtlist); |
| 2921 | } |
| 2922 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 2923 | int print_hwcache_events(const char *event_glob, bool name_only) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2924 | { |
Jin Yao | 6c93f39 | 2021-09-09 14:18:44 +0800 | [diff] [blame] | 2925 | unsigned int type, op, i, evt_i = 0, evt_num = 0, npmus = 0; |
| 2926 | char name[64], new_name[128]; |
| 2927 | char **evt_list = NULL, **evt_pmus = NULL; |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2928 | bool evt_num_known = false; |
Jin Yao | 6c93f39 | 2021-09-09 14:18:44 +0800 | [diff] [blame] | 2929 | struct perf_pmu *pmu = NULL; |
| 2930 | |
| 2931 | if (perf_pmu__has_hybrid()) { |
| 2932 | npmus = perf_pmu__hybrid_pmu_num(); |
| 2933 | evt_pmus = zalloc(sizeof(char *) * npmus); |
| 2934 | if (!evt_pmus) |
| 2935 | goto out_enomem; |
| 2936 | } |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2937 | |
| 2938 | restart: |
| 2939 | if (evt_num_known) { |
| 2940 | evt_list = zalloc(sizeof(char *) * evt_num); |
| 2941 | if (!evt_list) |
| 2942 | goto out_enomem; |
| 2943 | } |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2944 | |
| 2945 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { |
| 2946 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { |
| 2947 | /* skip invalid cache type */ |
Arnaldo Carvalho de Melo | c754c38 | 2020-04-30 10:51:16 -0300 | [diff] [blame] | 2948 | if (!evsel__is_cache_op_valid(type, op)) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2949 | continue; |
| 2950 | |
| 2951 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { |
Jin Yao | 6c93f39 | 2021-09-09 14:18:44 +0800 | [diff] [blame] | 2952 | unsigned int hybrid_supported = 0, j; |
| 2953 | bool supported; |
| 2954 | |
Arnaldo Carvalho de Melo | 8ab2e96 | 2020-04-29 16:07:09 -0300 | [diff] [blame] | 2955 | __evsel__hw_cache_type_op_res_name(type, op, i, name, sizeof(name)); |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 2956 | if (event_glob != NULL && !strglobmatch(name, event_glob)) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 2957 | continue; |
| 2958 | |
Jin Yao | 6c93f39 | 2021-09-09 14:18:44 +0800 | [diff] [blame] | 2959 | if (!perf_pmu__has_hybrid()) { |
| 2960 | if (!is_event_supported(PERF_TYPE_HW_CACHE, |
| 2961 | type | (op << 8) | (i << 16))) { |
| 2962 | continue; |
| 2963 | } |
| 2964 | } else { |
| 2965 | perf_pmu__for_each_hybrid_pmu(pmu) { |
| 2966 | if (!evt_num_known) { |
| 2967 | evt_num++; |
| 2968 | continue; |
| 2969 | } |
| 2970 | |
| 2971 | supported = is_event_supported( |
| 2972 | PERF_TYPE_HW_CACHE, |
| 2973 | type | (op << 8) | (i << 16) | |
| 2974 | ((__u64)pmu->type << PERF_PMU_TYPE_SHIFT)); |
| 2975 | if (supported) { |
| 2976 | snprintf(new_name, sizeof(new_name), "%s/%s/", |
| 2977 | pmu->name, name); |
| 2978 | evt_pmus[hybrid_supported] = strdup(new_name); |
| 2979 | hybrid_supported++; |
| 2980 | } |
| 2981 | } |
| 2982 | |
| 2983 | if (hybrid_supported == 0) |
| 2984 | continue; |
| 2985 | } |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 2986 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 2987 | if (!evt_num_known) { |
| 2988 | evt_num++; |
| 2989 | continue; |
| 2990 | } |
| 2991 | |
Jin Yao | 6c93f39 | 2021-09-09 14:18:44 +0800 | [diff] [blame] | 2992 | if ((hybrid_supported == 0) || |
| 2993 | (hybrid_supported == npmus)) { |
| 2994 | evt_list[evt_i] = strdup(name); |
| 2995 | if (npmus > 0) { |
| 2996 | for (j = 0; j < npmus; j++) |
| 2997 | zfree(&evt_pmus[j]); |
| 2998 | } |
| 2999 | } else { |
| 3000 | for (j = 0; j < hybrid_supported; j++) { |
| 3001 | evt_list[evt_i++] = evt_pmus[j]; |
| 3002 | evt_pmus[j] = NULL; |
| 3003 | } |
| 3004 | continue; |
| 3005 | } |
| 3006 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3007 | if (evt_list[evt_i] == NULL) |
| 3008 | goto out_enomem; |
| 3009 | evt_i++; |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 3010 | } |
| 3011 | } |
| 3012 | } |
| 3013 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3014 | if (!evt_num_known) { |
| 3015 | evt_num_known = true; |
| 3016 | goto restart; |
| 3017 | } |
Jin Yao | 6c93f39 | 2021-09-09 14:18:44 +0800 | [diff] [blame] | 3018 | |
| 3019 | for (evt_i = 0; evt_i < evt_num; evt_i++) { |
| 3020 | if (!evt_list[evt_i]) |
| 3021 | break; |
| 3022 | } |
| 3023 | |
| 3024 | evt_num = evt_i; |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3025 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); |
| 3026 | evt_i = 0; |
| 3027 | while (evt_i < evt_num) { |
| 3028 | if (name_only) { |
| 3029 | printf("%s ", evt_list[evt_i++]); |
| 3030 | continue; |
| 3031 | } |
| 3032 | printf(" %-50s [%s]\n", evt_list[evt_i++], |
| 3033 | event_type_descriptors[PERF_TYPE_HW_CACHE]); |
| 3034 | } |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 3035 | if (evt_num && pager_in_use()) |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 3036 | printf("\n"); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3037 | |
| 3038 | out_free: |
| 3039 | evt_num = evt_i; |
| 3040 | for (evt_i = 0; evt_i < evt_num; evt_i++) |
| 3041 | zfree(&evt_list[evt_i]); |
| 3042 | zfree(&evt_list); |
Jin Yao | 6c93f39 | 2021-09-09 14:18:44 +0800 | [diff] [blame] | 3043 | |
| 3044 | for (evt_i = 0; evt_i < npmus; evt_i++) |
| 3045 | zfree(&evt_pmus[evt_i]); |
| 3046 | zfree(&evt_pmus); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3047 | return evt_num; |
| 3048 | |
| 3049 | out_enomem: |
| 3050 | printf("FATAL: not enough memory to print %s\n", event_type_descriptors[PERF_TYPE_HW_CACHE]); |
| 3051 | if (evt_list) |
| 3052 | goto out_free; |
| 3053 | return evt_num; |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 3054 | } |
| 3055 | |
Andi Kleen | 5e0861b | 2019-03-26 15:18:23 -0700 | [diff] [blame] | 3056 | static void print_tool_event(const char *name, const char *event_glob, |
| 3057 | bool name_only) |
| 3058 | { |
| 3059 | if (event_glob && !strglobmatch(name, event_glob)) |
| 3060 | return; |
| 3061 | if (name_only) |
| 3062 | printf("%s ", name); |
| 3063 | else |
| 3064 | printf(" %-50s [%s]\n", name, "Tool event"); |
| 3065 | |
| 3066 | } |
| 3067 | |
| 3068 | void print_tool_events(const char *event_glob, bool name_only) |
| 3069 | { |
| 3070 | print_tool_event("duration_time", event_glob, name_only); |
| 3071 | if (pager_in_use()) |
| 3072 | printf("\n"); |
| 3073 | } |
| 3074 | |
Yunlong Song | 705750f | 2015-02-27 18:21:27 +0800 | [diff] [blame] | 3075 | void print_symbol_events(const char *event_glob, unsigned type, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3076 | struct event_symbol *syms, unsigned max, |
| 3077 | bool name_only) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 3078 | { |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3079 | unsigned int i, evt_i = 0, evt_num = 0; |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 3080 | char name[MAX_NAME_LEN]; |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3081 | char **evt_list = NULL; |
| 3082 | bool evt_num_known = false; |
| 3083 | |
| 3084 | restart: |
| 3085 | if (evt_num_known) { |
| 3086 | evt_list = zalloc(sizeof(char *) * evt_num); |
| 3087 | if (!evt_list) |
| 3088 | goto out_enomem; |
| 3089 | syms -= max; |
| 3090 | } |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 3091 | |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 3092 | for (i = 0; i < max; i++, syms++) { |
Arnaldo Carvalho de Melo | 3b2f17a | 2021-05-19 13:50:31 -0300 | [diff] [blame] | 3093 | /* |
| 3094 | * New attr.config still not supported here, the latest |
| 3095 | * example was PERF_COUNT_SW_CGROUP_SWITCHES |
| 3096 | */ |
| 3097 | if (syms->symbol == NULL) |
| 3098 | continue; |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 3099 | |
Arnaldo Carvalho de Melo | 3b2f17a | 2021-05-19 13:50:31 -0300 | [diff] [blame] | 3100 | if (event_glob != NULL && !(strglobmatch(syms->symbol, event_glob) || |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 3101 | (syms->alias && strglobmatch(syms->alias, event_glob)))) |
| 3102 | continue; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 3103 | |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 3104 | if (!is_event_supported(type, i)) |
| 3105 | continue; |
| 3106 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3107 | if (!evt_num_known) { |
| 3108 | evt_num++; |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3109 | continue; |
| 3110 | } |
| 3111 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3112 | if (!name_only && strlen(syms->alias)) |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 3113 | snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias); |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 3114 | else |
Arnaldo Carvalho de Melo | bd8d57f | 2018-12-06 13:52:13 -0300 | [diff] [blame] | 3115 | strlcpy(name, syms->symbol, MAX_NAME_LEN); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 3116 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3117 | evt_list[evt_i] = strdup(name); |
| 3118 | if (evt_list[evt_i] == NULL) |
| 3119 | goto out_enomem; |
| 3120 | evt_i++; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 3121 | } |
| 3122 | |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3123 | if (!evt_num_known) { |
| 3124 | evt_num_known = true; |
| 3125 | goto restart; |
| 3126 | } |
| 3127 | qsort(evt_list, evt_num, sizeof(char *), cmp_string); |
| 3128 | evt_i = 0; |
| 3129 | while (evt_i < evt_num) { |
| 3130 | if (name_only) { |
| 3131 | printf("%s ", evt_list[evt_i++]); |
| 3132 | continue; |
| 3133 | } |
| 3134 | printf(" %-50s [%s]\n", evt_list[evt_i++], event_type_descriptors[type]); |
| 3135 | } |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 3136 | if (evt_num && pager_in_use()) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 3137 | printf("\n"); |
Yunlong Song | ab0e480 | 2015-02-27 18:21:25 +0800 | [diff] [blame] | 3138 | |
| 3139 | out_free: |
| 3140 | evt_num = evt_i; |
| 3141 | for (evt_i = 0; evt_i < evt_num; evt_i++) |
| 3142 | zfree(&evt_list[evt_i]); |
| 3143 | zfree(&evt_list); |
| 3144 | return; |
| 3145 | |
| 3146 | out_enomem: |
| 3147 | printf("FATAL: not enough memory to print %s\n", event_type_descriptors[type]); |
| 3148 | if (evt_list) |
| 3149 | goto out_free; |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | /* |
| 3153 | * Print the help text for the event symbols: |
| 3154 | */ |
Sukadev Bhattiprolu | c8d6828 | 2016-09-15 15:24:48 -0700 | [diff] [blame] | 3155 | void print_events(const char *event_glob, bool name_only, bool quiet_flag, |
Jin Yao | 0e0ae87 | 2021-09-03 10:52:39 +0800 | [diff] [blame] | 3156 | bool long_desc, bool details_flag, bool deprecated, |
| 3157 | const char *pmu_name) |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 3158 | { |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 3159 | print_symbol_events(event_glob, PERF_TYPE_HARDWARE, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3160 | event_symbols_hw, PERF_COUNT_HW_MAX, name_only); |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 3161 | |
| 3162 | print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3163 | event_symbols_sw, PERF_COUNT_SW_MAX, name_only); |
Andi Kleen | 5e0861b | 2019-03-26 15:18:23 -0700 | [diff] [blame] | 3164 | print_tool_events(event_glob, name_only); |
Jiri Olsa | 1dc12760 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 3165 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3166 | print_hwcache_events(event_glob, name_only); |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 3167 | |
Andi Kleen | bf874fc | 2017-03-20 13:17:11 -0700 | [diff] [blame] | 3168 | print_pmu_events(event_glob, name_only, quiet_flag, long_desc, |
Jin Yao | 0e0ae87 | 2021-09-03 10:52:39 +0800 | [diff] [blame] | 3169 | details_flag, deprecated, pmu_name); |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 3170 | |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 3171 | if (event_glob != NULL) |
| 3172 | return; |
Jaswinder Singh Rajput | 73c24cb | 2009-07-01 18:36:18 +0530 | [diff] [blame] | 3173 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3174 | if (!name_only) { |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3175 | printf(" %-50s [%s]\n", |
| 3176 | "rNNN", |
| 3177 | event_type_descriptors[PERF_TYPE_RAW]); |
| 3178 | printf(" %-50s [%s]\n", |
| 3179 | "cpu/t1=v1[,t2=v2,t3 ...]/modifier", |
| 3180 | event_type_descriptors[PERF_TYPE_RAW]); |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 3181 | if (pager_in_use()) |
| 3182 | printf(" (see 'man perf-list' on how to encode it)\n\n"); |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 3183 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3184 | printf(" %-50s [%s]\n", |
Jacob Shin | 3741eb9 | 2014-05-29 17:26:51 +0200 | [diff] [blame] | 3185 | "mem:<addr>[/len][:access]", |
Liming Wang | 41bdcb2 | 2009-12-29 16:37:07 +0800 | [diff] [blame] | 3186 | event_type_descriptors[PERF_TYPE_BREAKPOINT]); |
Arnaldo Carvalho de Melo | dfc431c | 2015-09-30 17:13:26 -0300 | [diff] [blame] | 3187 | if (pager_in_use()) |
| 3188 | printf("\n"); |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3189 | } |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 3190 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 3191 | print_tracepoint_events(NULL, NULL, name_only); |
Masami Hiramatsu | 40218da | 2016-06-24 18:06:46 +0900 | [diff] [blame] | 3192 | |
| 3193 | print_sdt_events(NULL, NULL, name_only); |
Andi Kleen | 71b0acc | 2017-08-31 12:40:32 -0700 | [diff] [blame] | 3194 | |
Jin Yao | 0e0ae87 | 2021-09-03 10:52:39 +0800 | [diff] [blame] | 3195 | metricgroup__print(true, true, NULL, name_only, details_flag, |
| 3196 | pmu_name); |
Stephane Eranian | 7094349 | 2020-05-05 11:29:43 -0700 | [diff] [blame] | 3197 | |
| 3198 | print_libpfm_events(name_only, long_desc); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 3199 | } |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3200 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 3201 | int parse_events__is_hardcoded_term(struct parse_events_term *term) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3202 | { |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 3203 | return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3204 | } |
| 3205 | |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3206 | static int new_term(struct parse_events_term **_term, |
| 3207 | struct parse_events_term *temp, |
| 3208 | char *str, u64 num) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3209 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 3210 | struct parse_events_term *term; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3211 | |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3212 | term = malloc(sizeof(*term)); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3213 | if (!term) |
| 3214 | return -ENOMEM; |
| 3215 | |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3216 | *term = *temp; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3217 | INIT_LIST_HEAD(&term->list); |
Andi Kleen | c2f1cea | 2017-10-20 13:27:55 -0700 | [diff] [blame] | 3218 | term->weak = false; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3219 | |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3220 | switch (term->type_val) { |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3221 | case PARSE_EVENTS__TERM_TYPE_NUM: |
| 3222 | term->val.num = num; |
| 3223 | break; |
| 3224 | case PARSE_EVENTS__TERM_TYPE_STR: |
| 3225 | term->val.str = str; |
| 3226 | break; |
| 3227 | default: |
Adrian Hunter | 4be8be6 | 2013-07-04 16:20:24 +0300 | [diff] [blame] | 3228 | free(term); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3229 | return -EINVAL; |
| 3230 | } |
| 3231 | |
| 3232 | *_term = term; |
| 3233 | return 0; |
| 3234 | } |
| 3235 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 3236 | int parse_events_term__num(struct parse_events_term **term, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 3237 | int type_term, char *config, u64 num, |
Jiri Olsa | 99e7138 | 2017-02-17 15:00:56 +0100 | [diff] [blame] | 3238 | bool no_value, |
Adrian Hunter | bb78ce7 | 2015-05-19 16:05:42 +0300 | [diff] [blame] | 3239 | void *loc_term_, void *loc_val_) |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 3240 | { |
Adrian Hunter | bb78ce7 | 2015-05-19 16:05:42 +0300 | [diff] [blame] | 3241 | YYLTYPE *loc_term = loc_term_; |
| 3242 | YYLTYPE *loc_val = loc_val_; |
| 3243 | |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3244 | struct parse_events_term temp = { |
| 3245 | .type_val = PARSE_EVENTS__TERM_TYPE_NUM, |
| 3246 | .type_term = type_term, |
John Garry | 4f9d4f8 | 2021-09-16 20:34:21 +0800 | [diff] [blame] | 3247 | .config = config ? : strdup(config_term_names[type_term]), |
Jiri Olsa | 99e7138 | 2017-02-17 15:00:56 +0100 | [diff] [blame] | 3248 | .no_value = no_value, |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3249 | .err_term = loc_term ? loc_term->first_column : 0, |
| 3250 | .err_val = loc_val ? loc_val->first_column : 0, |
| 3251 | }; |
| 3252 | |
| 3253 | return new_term(term, &temp, NULL, num); |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 3254 | } |
| 3255 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 3256 | int parse_events_term__str(struct parse_events_term **term, |
Jiri Olsa | cecf3a2e | 2015-04-22 21:10:20 +0200 | [diff] [blame] | 3257 | int type_term, char *config, char *str, |
Adrian Hunter | bb78ce7 | 2015-05-19 16:05:42 +0300 | [diff] [blame] | 3258 | void *loc_term_, void *loc_val_) |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 3259 | { |
Adrian Hunter | bb78ce7 | 2015-05-19 16:05:42 +0300 | [diff] [blame] | 3260 | YYLTYPE *loc_term = loc_term_; |
| 3261 | YYLTYPE *loc_val = loc_val_; |
| 3262 | |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3263 | struct parse_events_term temp = { |
| 3264 | .type_val = PARSE_EVENTS__TERM_TYPE_STR, |
| 3265 | .type_term = type_term, |
| 3266 | .config = config, |
| 3267 | .err_term = loc_term ? loc_term->first_column : 0, |
| 3268 | .err_val = loc_val ? loc_val->first_column : 0, |
| 3269 | }; |
| 3270 | |
| 3271 | return new_term(term, &temp, str, 0); |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 3272 | } |
| 3273 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 3274 | int parse_events_term__sym_hw(struct parse_events_term **term, |
Jiri Olsa | 1d33d6d | 2012-10-10 14:53:17 +0200 | [diff] [blame] | 3275 | char *config, unsigned idx) |
| 3276 | { |
| 3277 | struct event_symbol *sym; |
Ian Rogers | b6645a7 | 2019-10-30 15:34:43 -0700 | [diff] [blame] | 3278 | char *str; |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3279 | struct parse_events_term temp = { |
| 3280 | .type_val = PARSE_EVENTS__TERM_TYPE_STR, |
| 3281 | .type_term = PARSE_EVENTS__TERM_TYPE_USER, |
Ian Rogers | b6645a7 | 2019-10-30 15:34:43 -0700 | [diff] [blame] | 3282 | .config = config, |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3283 | }; |
Jiri Olsa | 1d33d6d | 2012-10-10 14:53:17 +0200 | [diff] [blame] | 3284 | |
Ian Rogers | b6645a7 | 2019-10-30 15:34:43 -0700 | [diff] [blame] | 3285 | if (!temp.config) { |
| 3286 | temp.config = strdup("event"); |
| 3287 | if (!temp.config) |
| 3288 | return -ENOMEM; |
| 3289 | } |
Jiri Olsa | 1d33d6d | 2012-10-10 14:53:17 +0200 | [diff] [blame] | 3290 | BUG_ON(idx >= PERF_COUNT_HW_MAX); |
| 3291 | sym = &event_symbols_hw[idx]; |
| 3292 | |
Ian Rogers | b6645a7 | 2019-10-30 15:34:43 -0700 | [diff] [blame] | 3293 | str = strdup(sym->symbol); |
| 3294 | if (!str) |
| 3295 | return -ENOMEM; |
| 3296 | return new_term(term, &temp, str, 0); |
Jiri Olsa | 1d33d6d | 2012-10-10 14:53:17 +0200 | [diff] [blame] | 3297 | } |
| 3298 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 3299 | int parse_events_term__clone(struct parse_events_term **new, |
| 3300 | struct parse_events_term *term) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 3301 | { |
Ian Rogers | b6645a7 | 2019-10-30 15:34:43 -0700 | [diff] [blame] | 3302 | char *str; |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3303 | struct parse_events_term temp = { |
| 3304 | .type_val = term->type_val, |
| 3305 | .type_term = term->type_term, |
Ian Rogers | b6645a7 | 2019-10-30 15:34:43 -0700 | [diff] [blame] | 3306 | .config = NULL, |
Jiri Olsa | 67b49b3 | 2017-02-17 15:00:55 +0100 | [diff] [blame] | 3307 | .err_term = term->err_term, |
| 3308 | .err_val = term->err_val, |
| 3309 | }; |
| 3310 | |
Ian Rogers | b6645a7 | 2019-10-30 15:34:43 -0700 | [diff] [blame] | 3311 | if (term->config) { |
| 3312 | temp.config = strdup(term->config); |
| 3313 | if (!temp.config) |
| 3314 | return -ENOMEM; |
| 3315 | } |
| 3316 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) |
| 3317 | return new_term(new, &temp, NULL, term->val.num); |
| 3318 | |
| 3319 | str = strdup(term->val.str); |
| 3320 | if (!str) |
| 3321 | return -ENOMEM; |
| 3322 | return new_term(new, &temp, str, 0); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 3323 | } |
| 3324 | |
Ian Rogers | 1dc9255 | 2019-10-30 15:34:47 -0700 | [diff] [blame] | 3325 | void parse_events_term__delete(struct parse_events_term *term) |
| 3326 | { |
| 3327 | if (term->array.nr_ranges) |
| 3328 | zfree(&term->array.ranges); |
| 3329 | |
| 3330 | if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) |
| 3331 | zfree(&term->val.str); |
| 3332 | |
| 3333 | zfree(&term->config); |
| 3334 | free(term); |
| 3335 | } |
| 3336 | |
Andi Kleen | 8255718 | 2017-03-20 13:17:03 -0700 | [diff] [blame] | 3337 | int parse_events_copy_term_list(struct list_head *old, |
| 3338 | struct list_head **new) |
| 3339 | { |
| 3340 | struct parse_events_term *term, *n; |
| 3341 | int ret; |
| 3342 | |
| 3343 | if (!old) { |
| 3344 | *new = NULL; |
| 3345 | return 0; |
| 3346 | } |
| 3347 | |
| 3348 | *new = malloc(sizeof(struct list_head)); |
| 3349 | if (!*new) |
| 3350 | return -ENOMEM; |
| 3351 | INIT_LIST_HEAD(*new); |
| 3352 | |
| 3353 | list_for_each_entry (term, old, list) { |
| 3354 | ret = parse_events_term__clone(&n, term); |
| 3355 | if (ret) |
| 3356 | return ret; |
| 3357 | list_add_tail(&n->list, *new); |
| 3358 | } |
| 3359 | return 0; |
| 3360 | } |
| 3361 | |
Arnaldo Carvalho de Melo | fc0a2c1 | 2016-02-12 16:43:02 -0300 | [diff] [blame] | 3362 | void parse_events_terms__purge(struct list_head *terms) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3363 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 3364 | struct parse_events_term *term, *h; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3365 | |
Wang Nan | a8adfce | 2016-02-12 16:31:23 -0300 | [diff] [blame] | 3366 | list_for_each_entry_safe(term, h, terms, list) { |
| 3367 | list_del_init(&term->list); |
Ian Rogers | 1dc9255 | 2019-10-30 15:34:47 -0700 | [diff] [blame] | 3368 | parse_events_term__delete(term); |
Wang Nan | a8adfce | 2016-02-12 16:31:23 -0300 | [diff] [blame] | 3369 | } |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 3370 | } |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 3371 | |
Arnaldo Carvalho de Melo | 2146afc | 2016-02-12 17:09:17 -0300 | [diff] [blame] | 3372 | void parse_events_terms__delete(struct list_head *terms) |
Arnaldo Carvalho de Melo | fc0a2c1 | 2016-02-12 16:43:02 -0300 | [diff] [blame] | 3373 | { |
Arnaldo Carvalho de Melo | 2146afc | 2016-02-12 17:09:17 -0300 | [diff] [blame] | 3374 | if (!terms) |
| 3375 | return; |
Arnaldo Carvalho de Melo | fc0a2c1 | 2016-02-12 16:43:02 -0300 | [diff] [blame] | 3376 | parse_events_terms__purge(terms); |
Wang Nan | d20a5f2 | 2016-02-12 17:01:17 -0300 | [diff] [blame] | 3377 | free(terms); |
Arnaldo Carvalho de Melo | fc0a2c1 | 2016-02-12 16:43:02 -0300 | [diff] [blame] | 3378 | } |
| 3379 | |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 3380 | void parse_events__clear_array(struct parse_events_array *a) |
| 3381 | { |
Taeung Song | 360e071 | 2017-02-01 21:34:07 +0900 | [diff] [blame] | 3382 | zfree(&a->ranges); |
Wang Nan | 2d055bf | 2016-02-22 09:10:34 +0000 | [diff] [blame] | 3383 | } |
| 3384 | |
Arnaldo Carvalho de Melo | 5d9cdc1 | 2017-08-17 16:13:34 -0300 | [diff] [blame] | 3385 | void parse_events_evlist_error(struct parse_events_state *parse_state, |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 3386 | int idx, const char *str) |
| 3387 | { |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 3388 | if (!parse_state->error) |
Adrian Hunter | a6ced2b | 2015-05-19 16:05:44 +0300 | [diff] [blame] | 3389 | return; |
Ian Rogers | 448d732 | 2019-10-30 15:34:39 -0700 | [diff] [blame] | 3390 | |
Ian Rogers | 6c19128 | 2021-11-07 01:00:00 -0800 | [diff] [blame] | 3391 | parse_events_error__handle(parse_state->error, idx, strdup(str), NULL); |
Jiri Olsa | b39b839 | 2015-04-22 21:10:16 +0200 | [diff] [blame] | 3392 | } |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 3393 | |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 3394 | static void config_terms_list(char *buf, size_t buf_sz) |
| 3395 | { |
| 3396 | int i; |
| 3397 | bool first = true; |
| 3398 | |
| 3399 | buf[0] = '\0'; |
| 3400 | for (i = 0; i < __PARSE_EVENTS__TERM_TYPE_NR; i++) { |
| 3401 | const char *name = config_term_names[i]; |
| 3402 | |
Wang Nan | 1669e50 | 2016-02-19 11:43:58 +0000 | [diff] [blame] | 3403 | if (!config_term_avail(i, NULL)) |
| 3404 | continue; |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 3405 | if (!name) |
| 3406 | continue; |
| 3407 | if (name[0] == '<') |
| 3408 | continue; |
| 3409 | |
| 3410 | if (strlen(buf) + strlen(name) + 2 >= buf_sz) |
| 3411 | return; |
| 3412 | |
| 3413 | if (!first) |
| 3414 | strcat(buf, ","); |
| 3415 | else |
| 3416 | first = false; |
| 3417 | strcat(buf, name); |
| 3418 | } |
| 3419 | } |
| 3420 | |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 3421 | /* |
| 3422 | * Return string contains valid config terms of an event. |
| 3423 | * @additional_terms: For terms such as PMU sysfs terms. |
| 3424 | */ |
| 3425 | char *parse_events_formats_error_string(char *additional_terms) |
| 3426 | { |
| 3427 | char *str; |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 3428 | /* "no-overwrite" is the longest name */ |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 3429 | char static_terms[__PARSE_EVENTS__TERM_TYPE_NR * |
Wang Nan | 626a6b7 | 2016-07-14 08:34:45 +0000 | [diff] [blame] | 3430 | (sizeof("no-overwrite") - 1)]; |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 3431 | |
Wang Nan | 17cb5f8 | 2016-02-19 11:43:57 +0000 | [diff] [blame] | 3432 | config_terms_list(static_terms, sizeof(static_terms)); |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 3433 | /* valid terms */ |
| 3434 | if (additional_terms) { |
Wang Nan | 26dee02 | 2016-02-19 11:43:52 +0000 | [diff] [blame] | 3435 | if (asprintf(&str, "valid terms: %s,%s", |
| 3436 | additional_terms, static_terms) < 0) |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 3437 | goto fail; |
| 3438 | } else { |
Wang Nan | 26dee02 | 2016-02-19 11:43:52 +0000 | [diff] [blame] | 3439 | if (asprintf(&str, "valid terms: %s", static_terms) < 0) |
He Kuang | ffeb883 | 2015-09-28 03:52:14 +0000 | [diff] [blame] | 3440 | goto fail; |
| 3441 | } |
| 3442 | return str; |
| 3443 | |
| 3444 | fail: |
| 3445 | return NULL; |
| 3446 | } |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 3447 | |
| 3448 | struct evsel *parse_events__add_event_hybrid(struct list_head *list, int *idx, |
| 3449 | struct perf_event_attr *attr, |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 3450 | const char *name, |
| 3451 | const char *metric_id, |
| 3452 | struct perf_pmu *pmu, |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 3453 | struct list_head *config_terms) |
| 3454 | { |
Ian Rogers | 2b62b3a | 2021-10-15 10:21:25 -0700 | [diff] [blame] | 3455 | return __add_event(list, idx, attr, /*init_attr=*/true, name, metric_id, |
| 3456 | pmu, config_terms, /*auto_merge_stats=*/false, |
| 3457 | /*cpu_list=*/NULL); |
Jin Yao | 9cbfa2f | 2021-04-27 15:01:21 +0800 | [diff] [blame] | 3458 | } |