David Howells | d2709c7 | 2012-11-19 22:21:03 +0000 | [diff] [blame] | 1 | #include <linux/hw_breakpoint.h> |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 2 | #include "util.h" |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 3 | #include "../perf.h" |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 4 | #include "evlist.h" |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 5 | #include "evsel.h" |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 6 | #include "parse-options.h" |
| 7 | #include "parse-events.h" |
| 8 | #include "exec_cmd.h" |
Kan Liang | 42f60c2 | 2014-10-07 11:08:49 -0400 | [diff] [blame] | 9 | #include "string.h" |
Arnaldo Carvalho de Melo | 5aab621 | 2010-03-25 19:59:00 -0300 | [diff] [blame] | 10 | #include "symbol.h" |
Jason Baron | 5beeded | 2009-07-21 14:16:29 -0400 | [diff] [blame] | 11 | #include "cache.h" |
Arjan van de Ven | 8755a8f | 2009-09-12 07:52:51 +0200 | [diff] [blame] | 12 | #include "header.h" |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 13 | #include "debug.h" |
Borislav Petkov | 553873e | 2013-12-09 17:14:23 +0100 | [diff] [blame] | 14 | #include <api/fs/debugfs.h> |
Zheng Yan | ac20de6 | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 15 | #include "parse-events-bison.h" |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 16 | #define YY_EXTRA_TYPE int |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 17 | #include "parse-events-flex.h" |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 18 | #include "pmu.h" |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 19 | #include "thread_map.h" |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 20 | |
| 21 | #define MAX_NAME_LEN 100 |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 22 | |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 23 | struct event_symbol { |
Ingo Molnar | 83a0944 | 2009-08-15 12:26:57 +0200 | [diff] [blame] | 24 | const char *symbol; |
| 25 | const char *alias; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Jiri Olsa | 82ba1f2 | 2012-05-21 09:12:50 +0200 | [diff] [blame] | 28 | #ifdef PARSER_DEBUG |
| 29 | extern int parse_events_debug; |
| 30 | #endif |
Zheng Yan | ac20de6 | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 31 | int parse_events_parse(void *data, void *scanner); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 32 | |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 33 | static struct perf_pmu_event_symbol *perf_pmu_events_list; |
| 34 | /* |
| 35 | * The variable indicates the number of supported pmu event symbols. |
| 36 | * 0 means not initialized and ready to init |
| 37 | * -1 means failed to init, don't try anymore |
| 38 | * >0 is the number of supported pmu event symbols |
| 39 | */ |
| 40 | static int perf_pmu_events_list_num; |
| 41 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 42 | static struct event_symbol event_symbols_hw[PERF_COUNT_HW_MAX] = { |
| 43 | [PERF_COUNT_HW_CPU_CYCLES] = { |
| 44 | .symbol = "cpu-cycles", |
| 45 | .alias = "cycles", |
| 46 | }, |
| 47 | [PERF_COUNT_HW_INSTRUCTIONS] = { |
| 48 | .symbol = "instructions", |
| 49 | .alias = "", |
| 50 | }, |
| 51 | [PERF_COUNT_HW_CACHE_REFERENCES] = { |
| 52 | .symbol = "cache-references", |
| 53 | .alias = "", |
| 54 | }, |
| 55 | [PERF_COUNT_HW_CACHE_MISSES] = { |
| 56 | .symbol = "cache-misses", |
| 57 | .alias = "", |
| 58 | }, |
| 59 | [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = { |
| 60 | .symbol = "branch-instructions", |
| 61 | .alias = "branches", |
| 62 | }, |
| 63 | [PERF_COUNT_HW_BRANCH_MISSES] = { |
| 64 | .symbol = "branch-misses", |
| 65 | .alias = "", |
| 66 | }, |
| 67 | [PERF_COUNT_HW_BUS_CYCLES] = { |
| 68 | .symbol = "bus-cycles", |
| 69 | .alias = "", |
| 70 | }, |
| 71 | [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = { |
| 72 | .symbol = "stalled-cycles-frontend", |
| 73 | .alias = "idle-cycles-frontend", |
| 74 | }, |
| 75 | [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = { |
| 76 | .symbol = "stalled-cycles-backend", |
| 77 | .alias = "idle-cycles-backend", |
| 78 | }, |
| 79 | [PERF_COUNT_HW_REF_CPU_CYCLES] = { |
| 80 | .symbol = "ref-cycles", |
| 81 | .alias = "", |
| 82 | }, |
| 83 | }; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 84 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 85 | static struct event_symbol event_symbols_sw[PERF_COUNT_SW_MAX] = { |
| 86 | [PERF_COUNT_SW_CPU_CLOCK] = { |
| 87 | .symbol = "cpu-clock", |
| 88 | .alias = "", |
| 89 | }, |
| 90 | [PERF_COUNT_SW_TASK_CLOCK] = { |
| 91 | .symbol = "task-clock", |
| 92 | .alias = "", |
| 93 | }, |
| 94 | [PERF_COUNT_SW_PAGE_FAULTS] = { |
| 95 | .symbol = "page-faults", |
| 96 | .alias = "faults", |
| 97 | }, |
| 98 | [PERF_COUNT_SW_CONTEXT_SWITCHES] = { |
| 99 | .symbol = "context-switches", |
| 100 | .alias = "cs", |
| 101 | }, |
| 102 | [PERF_COUNT_SW_CPU_MIGRATIONS] = { |
| 103 | .symbol = "cpu-migrations", |
| 104 | .alias = "migrations", |
| 105 | }, |
| 106 | [PERF_COUNT_SW_PAGE_FAULTS_MIN] = { |
| 107 | .symbol = "minor-faults", |
| 108 | .alias = "", |
| 109 | }, |
| 110 | [PERF_COUNT_SW_PAGE_FAULTS_MAJ] = { |
| 111 | .symbol = "major-faults", |
| 112 | .alias = "", |
| 113 | }, |
| 114 | [PERF_COUNT_SW_ALIGNMENT_FAULTS] = { |
| 115 | .symbol = "alignment-faults", |
| 116 | .alias = "", |
| 117 | }, |
| 118 | [PERF_COUNT_SW_EMULATION_FAULTS] = { |
| 119 | .symbol = "emulation-faults", |
| 120 | .alias = "", |
| 121 | }, |
Adrian Hunter | d22d1a2 | 2013-08-31 21:50:52 +0300 | [diff] [blame] | 122 | [PERF_COUNT_SW_DUMMY] = { |
| 123 | .symbol = "dummy", |
| 124 | .alias = "", |
| 125 | }, |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 126 | }; |
| 127 | |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 128 | #define __PERF_EVENT_FIELD(config, name) \ |
| 129 | ((config & PERF_EVENT_##name##_MASK) >> PERF_EVENT_##name##_SHIFT) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 130 | |
Ingo Molnar | 1fc570a | 2011-04-27 05:20:22 +0200 | [diff] [blame] | 131 | #define PERF_EVENT_RAW(config) __PERF_EVENT_FIELD(config, RAW) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 132 | #define PERF_EVENT_CONFIG(config) __PERF_EVENT_FIELD(config, CONFIG) |
Ingo Molnar | 1fc570a | 2011-04-27 05:20:22 +0200 | [diff] [blame] | 133 | #define PERF_EVENT_TYPE(config) __PERF_EVENT_FIELD(config, TYPE) |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 134 | #define PERF_EVENT_ID(config) __PERF_EVENT_FIELD(config, EVENT) |
Ingo Molnar | 5242519 | 2009-05-26 09:17:18 +0200 | [diff] [blame] | 135 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 136 | #define for_each_subsystem(sys_dir, sys_dirent, sys_next) \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 137 | while (!readdir_r(sys_dir, &sys_dirent, &sys_next) && sys_next) \ |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 138 | if (sys_dirent.d_type == DT_DIR && \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 139 | (strcmp(sys_dirent.d_name, ".")) && \ |
| 140 | (strcmp(sys_dirent.d_name, ".."))) |
| 141 | |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame] | 142 | static int tp_event_has_id(struct dirent *sys_dir, struct dirent *evt_dir) |
| 143 | { |
| 144 | char evt_path[MAXPATHLEN]; |
| 145 | int fd; |
| 146 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 147 | snprintf(evt_path, MAXPATHLEN, "%s/%s/%s/id", tracing_events_path, |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame] | 148 | sys_dir->d_name, evt_dir->d_name); |
| 149 | fd = open(evt_path, O_RDONLY); |
| 150 | if (fd < 0) |
| 151 | return -EINVAL; |
| 152 | close(fd); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 157 | #define for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 158 | while (!readdir_r(evt_dir, &evt_dirent, &evt_next) && evt_next) \ |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 159 | if (evt_dirent.d_type == DT_DIR && \ |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 160 | (strcmp(evt_dirent.d_name, ".")) && \ |
Peter Zijlstra | ae07b63 | 2009-08-06 16:48:54 +0200 | [diff] [blame] | 161 | (strcmp(evt_dirent.d_name, "..")) && \ |
| 162 | (!tp_event_has_id(&sys_dirent, &evt_dirent))) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 163 | |
Li Zefan | 270bbbe | 2009-09-17 16:34:51 +0800 | [diff] [blame] | 164 | #define MAX_EVENT_LENGTH 512 |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 165 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 166 | |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 167 | struct tracepoint_path *tracepoint_id_to_path(u64 config) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 168 | { |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 169 | struct tracepoint_path *path = NULL; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 170 | DIR *sys_dir, *evt_dir; |
| 171 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
Stephane Eranian | 8aa8a7c | 2012-03-13 16:51:02 +0100 | [diff] [blame] | 172 | char id_buf[24]; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 173 | int fd; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 174 | u64 id; |
| 175 | char evt_path[MAXPATHLEN]; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 176 | char dir_path[MAXPATHLEN]; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 177 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 178 | if (debugfs_valid_mountpoint(tracing_events_path)) |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 179 | return NULL; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 180 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 181 | sys_dir = opendir(tracing_events_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 182 | if (!sys_dir) |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 183 | return NULL; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 184 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 185 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 186 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 187 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 188 | sys_dirent.d_name); |
| 189 | evt_dir = opendir(dir_path); |
| 190 | if (!evt_dir) |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 191 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 192 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 193 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 194 | |
| 195 | snprintf(evt_path, MAXPATHLEN, "%s/%s/id", dir_path, |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 196 | evt_dirent.d_name); |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 197 | fd = open(evt_path, O_RDONLY); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 198 | if (fd < 0) |
| 199 | continue; |
| 200 | if (read(fd, id_buf, sizeof(id_buf)) < 0) { |
| 201 | close(fd); |
| 202 | continue; |
| 203 | } |
| 204 | close(fd); |
| 205 | id = atoll(id_buf); |
| 206 | if (id == config) { |
| 207 | closedir(evt_dir); |
| 208 | closedir(sys_dir); |
Julia Lawall | 59b4cae | 2009-12-06 10:16:30 +0100 | [diff] [blame] | 209 | path = zalloc(sizeof(*path)); |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 210 | path->system = malloc(MAX_EVENT_LENGTH); |
| 211 | if (!path->system) { |
| 212 | free(path); |
| 213 | return NULL; |
| 214 | } |
| 215 | path->name = malloc(MAX_EVENT_LENGTH); |
| 216 | if (!path->name) { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 217 | zfree(&path->system); |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 218 | free(path); |
| 219 | return NULL; |
| 220 | } |
| 221 | strncpy(path->system, sys_dirent.d_name, |
| 222 | MAX_EVENT_LENGTH); |
| 223 | strncpy(path->name, evt_dirent.d_name, |
| 224 | MAX_EVENT_LENGTH); |
| 225 | return path; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | closedir(evt_dir); |
| 229 | } |
| 230 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 231 | closedir(sys_dir); |
Frederic Weisbecker | 1ef2ed1 | 2009-08-28 03:09:58 +0200 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 235 | struct tracepoint_path *tracepoint_name_to_path(const char *name) |
| 236 | { |
| 237 | struct tracepoint_path *path = zalloc(sizeof(*path)); |
| 238 | char *str = strchr(name, ':'); |
| 239 | |
| 240 | if (path == NULL || str == NULL) { |
| 241 | free(path); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | path->system = strndup(name, str - name); |
| 246 | path->name = strdup(str+1); |
| 247 | |
| 248 | if (path->system == NULL || path->name == NULL) { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 249 | zfree(&path->system); |
| 250 | zfree(&path->name); |
Namhyung Kim | e7c93f0 | 2013-06-26 16:14:05 +0900 | [diff] [blame] | 251 | free(path); |
| 252 | path = NULL; |
| 253 | } |
| 254 | |
| 255 | return path; |
| 256 | } |
| 257 | |
David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 258 | const char *event_type(int type) |
| 259 | { |
| 260 | switch (type) { |
| 261 | case PERF_TYPE_HARDWARE: |
| 262 | return "hardware"; |
| 263 | |
| 264 | case PERF_TYPE_SOFTWARE: |
| 265 | return "software"; |
| 266 | |
| 267 | case PERF_TYPE_TRACEPOINT: |
| 268 | return "tracepoint"; |
| 269 | |
| 270 | case PERF_TYPE_HW_CACHE: |
| 271 | return "hardware-cache"; |
| 272 | |
| 273 | default: |
| 274 | break; |
| 275 | } |
| 276 | |
| 277 | return "unknown"; |
| 278 | } |
| 279 | |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 280 | |
| 281 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 282 | static struct perf_evsel * |
| 283 | __add_event(struct list_head *list, int *idx, |
| 284 | struct perf_event_attr *attr, |
| 285 | char *name, struct cpu_map *cpus) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 286 | { |
| 287 | struct perf_evsel *evsel; |
| 288 | |
| 289 | event_attr_init(attr); |
| 290 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 291 | evsel = perf_evsel__new_idx(attr, (*idx)++); |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 292 | if (!evsel) |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 293 | return NULL; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 294 | |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 295 | evsel->cpus = cpus; |
Arnaldo Carvalho de Melo | 9db1763 | 2012-06-12 13:45:00 -0300 | [diff] [blame] | 296 | if (name) |
| 297 | evsel->name = strdup(name); |
Jiri Olsa | b847cbd | 2012-05-21 09:12:51 +0200 | [diff] [blame] | 298 | list_add_tail(&evsel->node, list); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 299 | return evsel; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 300 | } |
| 301 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 302 | static int add_event(struct list_head *list, int *idx, |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 303 | struct perf_event_attr *attr, char *name) |
| 304 | { |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 305 | return __add_event(list, idx, attr, name, NULL) ? 0 : -ENOMEM; |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 306 | } |
| 307 | |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 308 | static int parse_aliases(char *str, const char *names[][PERF_EVSEL__MAX_ALIASES], int size) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 309 | { |
| 310 | int i, j; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 311 | int n, longest = -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 312 | |
| 313 | for (i = 0; i < size; i++) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 314 | for (j = 0; j < PERF_EVSEL__MAX_ALIASES && names[i][j]; j++) { |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 315 | n = strlen(names[i][j]); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 316 | if (n > longest && !strncasecmp(str, names[i][j], n)) |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 317 | longest = n; |
| 318 | } |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 319 | if (longest > 0) |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 320 | return i; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Ingo Molnar | 8953645 | 2009-06-06 21:04:17 +0200 | [diff] [blame] | 323 | return -1; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 324 | } |
| 325 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 326 | int parse_events_add_cache(struct list_head *list, int *idx, |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 327 | char *type, char *op_result1, char *op_result2) |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 328 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 329 | struct perf_event_attr attr; |
| 330 | char name[MAX_NAME_LEN]; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 331 | int cache_type = -1, cache_op = -1, cache_result = -1; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 332 | char *op_result[2] = { op_result1, op_result2 }; |
| 333 | int i, n; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 334 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 335 | /* |
| 336 | * No fallback - if we cannot get a clear cache type |
| 337 | * then bail out: |
| 338 | */ |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 339 | cache_type = parse_aliases(type, perf_evsel__hw_cache, |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 340 | PERF_COUNT_HW_CACHE_MAX); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 341 | if (cache_type == -1) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 342 | return -EINVAL; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 343 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 344 | n = snprintf(name, MAX_NAME_LEN, "%s", type); |
| 345 | |
| 346 | for (i = 0; (i < 2) && (op_result[i]); i++) { |
| 347 | char *str = op_result[i]; |
| 348 | |
Jiri Olsa | 275ef38 | 2012-09-05 19:51:33 +0200 | [diff] [blame] | 349 | n += snprintf(name + n, MAX_NAME_LEN - n, "-%s", str); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 350 | |
| 351 | if (cache_op == -1) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 352 | cache_op = parse_aliases(str, perf_evsel__hw_cache_op, |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 353 | PERF_COUNT_HW_CACHE_OP_MAX); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 354 | if (cache_op >= 0) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 355 | if (!perf_evsel__is_cache_op_valid(cache_type, cache_op)) |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 356 | return -EINVAL; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 357 | continue; |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | if (cache_result == -1) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 362 | cache_result = parse_aliases(str, perf_evsel__hw_cache_result, |
| 363 | PERF_COUNT_HW_CACHE_RESULT_MAX); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 364 | if (cache_result >= 0) |
| 365 | continue; |
| 366 | } |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 367 | } |
| 368 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 369 | /* |
| 370 | * Fall back to reads: |
| 371 | */ |
Ingo Molnar | 8953645 | 2009-06-06 21:04:17 +0200 | [diff] [blame] | 372 | if (cache_op == -1) |
| 373 | cache_op = PERF_COUNT_HW_CACHE_OP_READ; |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 374 | |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 375 | /* |
| 376 | * Fall back to accesses: |
| 377 | */ |
| 378 | if (cache_result == -1) |
| 379 | cache_result = PERF_COUNT_HW_CACHE_RESULT_ACCESS; |
| 380 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 381 | memset(&attr, 0, sizeof(attr)); |
| 382 | attr.config = cache_type | (cache_op << 8) | (cache_result << 16); |
| 383 | attr.type = PERF_TYPE_HW_CACHE; |
| 384 | return add_event(list, idx, &attr, name); |
Ingo Molnar | 8326f44 | 2009-06-05 20:22:46 +0200 | [diff] [blame] | 385 | } |
| 386 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 387 | static int add_tracepoint(struct list_head *list, int *idx, |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 388 | char *sys_name, char *evt_name) |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 389 | { |
Arnaldo Carvalho de Melo | 82fe1c2 | 2012-09-26 17:13:07 -0300 | [diff] [blame] | 390 | struct perf_evsel *evsel; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 391 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 392 | evsel = perf_evsel__newtp_idx(sys_name, evt_name, (*idx)++); |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 393 | if (!evsel) |
Arnaldo Carvalho de Melo | 82fe1c2 | 2012-09-26 17:13:07 -0300 | [diff] [blame] | 394 | return -ENOMEM; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 395 | |
Arnaldo Carvalho de Melo | 82fe1c2 | 2012-09-26 17:13:07 -0300 | [diff] [blame] | 396 | list_add_tail(&evsel->node, list); |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 397 | |
Arnaldo Carvalho de Melo | 82fe1c2 | 2012-09-26 17:13:07 -0300 | [diff] [blame] | 398 | return 0; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 399 | } |
| 400 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 401 | static int add_tracepoint_multi_event(struct list_head *list, int *idx, |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 402 | char *sys_name, char *evt_name) |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 403 | { |
| 404 | char evt_path[MAXPATHLEN]; |
| 405 | struct dirent *evt_ent; |
| 406 | DIR *evt_dir; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 407 | int ret = 0; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 408 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 409 | snprintf(evt_path, MAXPATHLEN, "%s/%s", tracing_events_path, sys_name); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 410 | evt_dir = opendir(evt_path); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 411 | if (!evt_dir) { |
| 412 | perror("Can't open event dir"); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 413 | return -1; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 416 | while (!ret && (evt_ent = readdir(evt_dir))) { |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 417 | if (!strcmp(evt_ent->d_name, ".") |
| 418 | || !strcmp(evt_ent->d_name, "..") |
| 419 | || !strcmp(evt_ent->d_name, "enable") |
| 420 | || !strcmp(evt_ent->d_name, "filter")) |
| 421 | continue; |
| 422 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 423 | if (!strglobmatch(evt_ent->d_name, evt_name)) |
Masami Hiramatsu | fb1d2ed | 2010-01-05 17:47:17 -0500 | [diff] [blame] | 424 | continue; |
| 425 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 426 | ret = add_tracepoint(list, idx, sys_name, evt_ent->d_name); |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 427 | } |
| 428 | |
Jiri Olsa | 0bd3f08 | 2012-12-17 14:08:36 +0100 | [diff] [blame] | 429 | closedir(evt_dir); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 430 | return ret; |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 431 | } |
| 432 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 433 | static int add_tracepoint_event(struct list_head *list, int *idx, |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 434 | char *sys_name, char *evt_name) |
| 435 | { |
| 436 | return strpbrk(evt_name, "*?") ? |
| 437 | add_tracepoint_multi_event(list, idx, sys_name, evt_name) : |
| 438 | add_tracepoint(list, idx, sys_name, evt_name); |
| 439 | } |
| 440 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 441 | static int add_tracepoint_multi_sys(struct list_head *list, int *idx, |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 442 | char *sys_name, char *evt_name) |
| 443 | { |
| 444 | struct dirent *events_ent; |
| 445 | DIR *events_dir; |
| 446 | int ret = 0; |
| 447 | |
| 448 | events_dir = opendir(tracing_events_path); |
| 449 | if (!events_dir) { |
| 450 | perror("Can't open event dir"); |
| 451 | return -1; |
| 452 | } |
| 453 | |
| 454 | while (!ret && (events_ent = readdir(events_dir))) { |
| 455 | if (!strcmp(events_ent->d_name, ".") |
| 456 | || !strcmp(events_ent->d_name, "..") |
| 457 | || !strcmp(events_ent->d_name, "enable") |
| 458 | || !strcmp(events_ent->d_name, "header_event") |
| 459 | || !strcmp(events_ent->d_name, "header_page")) |
| 460 | continue; |
| 461 | |
| 462 | if (!strglobmatch(events_ent->d_name, sys_name)) |
| 463 | continue; |
| 464 | |
| 465 | ret = add_tracepoint_event(list, idx, events_ent->d_name, |
| 466 | evt_name); |
| 467 | } |
| 468 | |
| 469 | closedir(events_dir); |
| 470 | return ret; |
| 471 | } |
| 472 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 473 | int parse_events_add_tracepoint(struct list_head *list, int *idx, |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 474 | char *sys, char *event) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 475 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 476 | int ret; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 477 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 478 | ret = debugfs_valid_mountpoint(tracing_events_path); |
| 479 | if (ret) |
| 480 | return ret; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 481 | |
Jiri Olsa | f35488f | 2012-12-17 14:08:37 +0100 | [diff] [blame] | 482 | if (strpbrk(sys, "*?")) |
| 483 | return add_tracepoint_multi_sys(list, idx, sys, event); |
| 484 | else |
| 485 | return add_tracepoint_event(list, idx, sys, event); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 486 | } |
| 487 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 488 | static int |
| 489 | parse_breakpoint_type(const char *type, struct perf_event_attr *attr) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 490 | { |
| 491 | int i; |
| 492 | |
| 493 | for (i = 0; i < 3; i++) { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 494 | if (!type || !type[i]) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 495 | break; |
| 496 | |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 497 | #define CHECK_SET_TYPE(bit) \ |
| 498 | do { \ |
| 499 | if (attr->bp_type & bit) \ |
| 500 | return -EINVAL; \ |
| 501 | else \ |
| 502 | attr->bp_type |= bit; \ |
| 503 | } while (0) |
| 504 | |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 505 | switch (type[i]) { |
| 506 | case 'r': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 507 | CHECK_SET_TYPE(HW_BREAKPOINT_R); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 508 | break; |
| 509 | case 'w': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 510 | CHECK_SET_TYPE(HW_BREAKPOINT_W); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 511 | break; |
| 512 | case 'x': |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 513 | CHECK_SET_TYPE(HW_BREAKPOINT_X); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 514 | break; |
| 515 | default: |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 516 | return -EINVAL; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 517 | } |
| 518 | } |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 519 | |
Jiri Olsa | 7582732 | 2012-06-29 09:22:54 +0200 | [diff] [blame] | 520 | #undef CHECK_SET_TYPE |
| 521 | |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 522 | if (!attr->bp_type) /* Default */ |
| 523 | attr->bp_type = HW_BREAKPOINT_R | HW_BREAKPOINT_W; |
| 524 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 525 | return 0; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 526 | } |
| 527 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 528 | int parse_events_add_breakpoint(struct list_head *list, int *idx, |
Jacob Shin | 3741eb9 | 2014-05-29 17:26:51 +0200 | [diff] [blame] | 529 | void *ptr, char *type, u64 len) |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 530 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 531 | struct perf_event_attr attr; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 532 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 533 | memset(&attr, 0, sizeof(attr)); |
Jiri Olsa | 9fafd98 | 2012-03-20 19:15:39 +0100 | [diff] [blame] | 534 | attr.bp_addr = (unsigned long) ptr; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 535 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 536 | if (parse_breakpoint_type(type, &attr)) |
| 537 | return -EINVAL; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 538 | |
Jacob Shin | 3741eb9 | 2014-05-29 17:26:51 +0200 | [diff] [blame] | 539 | /* Provide some defaults if len is not specified */ |
| 540 | if (!len) { |
| 541 | if (attr.bp_type == HW_BREAKPOINT_X) |
| 542 | len = sizeof(long); |
| 543 | else |
| 544 | len = HW_BREAKPOINT_LEN_4; |
| 545 | } |
| 546 | |
| 547 | attr.bp_len = len; |
Frederic Weisbecker | aa59a48 | 2010-06-24 21:36:19 +0200 | [diff] [blame] | 548 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 549 | attr.type = PERF_TYPE_BREAKPOINT; |
Jovi Zhang | 4a841d6 | 2012-07-15 03:03:10 +0800 | [diff] [blame] | 550 | attr.sample_period = 1; |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 551 | |
Jiri Olsa | 287e74a | 2012-06-28 23:18:49 +0200 | [diff] [blame] | 552 | return add_event(list, idx, &attr, NULL); |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 553 | } |
| 554 | |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 555 | static int config_term(struct perf_event_attr *attr, |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 556 | struct parse_events_term *term) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 557 | { |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 558 | #define CHECK_TYPE_VAL(type) \ |
| 559 | do { \ |
| 560 | if (PARSE_EVENTS__TERM_TYPE_ ## type != term->type_val) \ |
| 561 | return -EINVAL; \ |
| 562 | } while (0) |
| 563 | |
| 564 | switch (term->type_term) { |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 565 | case PARSE_EVENTS__TERM_TYPE_CONFIG: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 566 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 567 | attr->config = term->val.num; |
| 568 | break; |
| 569 | case PARSE_EVENTS__TERM_TYPE_CONFIG1: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 570 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 571 | attr->config1 = term->val.num; |
| 572 | break; |
| 573 | case PARSE_EVENTS__TERM_TYPE_CONFIG2: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 574 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 575 | attr->config2 = term->val.num; |
| 576 | break; |
| 577 | case PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD: |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 578 | CHECK_TYPE_VAL(NUM); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 579 | attr->sample_period = term->val.num; |
| 580 | break; |
| 581 | case PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE: |
| 582 | /* |
| 583 | * TODO uncomment when the field is available |
| 584 | * attr->branch_sample_type = term->val.num; |
| 585 | */ |
| 586 | break; |
Jiri Olsa | 6b5fc39 | 2012-05-21 09:12:53 +0200 | [diff] [blame] | 587 | case PARSE_EVENTS__TERM_TYPE_NAME: |
| 588 | CHECK_TYPE_VAL(STR); |
| 589 | break; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 590 | default: |
| 591 | return -EINVAL; |
| 592 | } |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 593 | |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 594 | return 0; |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 595 | #undef CHECK_TYPE_VAL |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | static int config_attr(struct perf_event_attr *attr, |
| 599 | struct list_head *head, int fail) |
| 600 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 601 | struct parse_events_term *term; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 602 | |
| 603 | list_for_each_entry(term, head, list) |
| 604 | if (config_term(attr, term) && fail) |
| 605 | return -EINVAL; |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 610 | int parse_events_add_numeric(struct list_head *list, int *idx, |
Robert Richter | b527bab | 2012-08-07 19:43:13 +0200 | [diff] [blame] | 611 | u32 type, u64 config, |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 612 | struct list_head *head_config) |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 613 | { |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 614 | struct perf_event_attr attr; |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 615 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 616 | memset(&attr, 0, sizeof(attr)); |
| 617 | attr.type = type; |
| 618 | attr.config = config; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 619 | |
| 620 | if (head_config && |
| 621 | config_attr(&attr, head_config, 1)) |
| 622 | return -EINVAL; |
| 623 | |
Arnaldo Carvalho de Melo | 9db1763 | 2012-06-12 13:45:00 -0300 | [diff] [blame] | 624 | return add_event(list, idx, &attr, NULL); |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 625 | } |
| 626 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 627 | static int parse_events__is_name_term(struct parse_events_term *term) |
Jiri Olsa | 6b5fc39 | 2012-05-21 09:12:53 +0200 | [diff] [blame] | 628 | { |
| 629 | return term->type_term == PARSE_EVENTS__TERM_TYPE_NAME; |
| 630 | } |
| 631 | |
Arnaldo Carvalho de Melo | 9db1763 | 2012-06-12 13:45:00 -0300 | [diff] [blame] | 632 | static char *pmu_event_name(struct list_head *head_terms) |
Jiri Olsa | 6b5fc39 | 2012-05-21 09:12:53 +0200 | [diff] [blame] | 633 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 634 | struct parse_events_term *term; |
Jiri Olsa | 6b5fc39 | 2012-05-21 09:12:53 +0200 | [diff] [blame] | 635 | |
| 636 | list_for_each_entry(term, head_terms, list) |
| 637 | if (parse_events__is_name_term(term)) |
| 638 | return term->val.str; |
| 639 | |
Arnaldo Carvalho de Melo | 9db1763 | 2012-06-12 13:45:00 -0300 | [diff] [blame] | 640 | return NULL; |
Jiri Olsa | 6b5fc39 | 2012-05-21 09:12:53 +0200 | [diff] [blame] | 641 | } |
| 642 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 643 | int parse_events_add_pmu(struct list_head *list, int *idx, |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 644 | char *name, struct list_head *head_config) |
| 645 | { |
| 646 | struct perf_event_attr attr; |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 647 | struct perf_pmu_info info; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 648 | struct perf_pmu *pmu; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 649 | struct perf_evsel *evsel; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 650 | |
| 651 | pmu = perf_pmu__find(name); |
| 652 | if (!pmu) |
| 653 | return -EINVAL; |
| 654 | |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame] | 655 | if (pmu->default_config) { |
| 656 | memcpy(&attr, pmu->default_config, |
| 657 | sizeof(struct perf_event_attr)); |
| 658 | } else { |
| 659 | memset(&attr, 0, sizeof(attr)); |
| 660 | } |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 661 | |
Adrian Hunter | ad96227 | 2014-08-15 22:08:40 +0300 | [diff] [blame] | 662 | if (!head_config) { |
| 663 | attr.type = pmu->type; |
| 664 | evsel = __add_event(list, idx, &attr, NULL, pmu->cpus); |
| 665 | return evsel ? 0 : -ENOMEM; |
| 666 | } |
| 667 | |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 668 | if (perf_pmu__check_alias(pmu, head_config, &info)) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 669 | return -EINVAL; |
| 670 | |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 671 | /* |
| 672 | * Configure hardcoded terms first, no need to check |
| 673 | * return value when called with fail == 0 ;) |
| 674 | */ |
| 675 | config_attr(&attr, head_config, 0); |
| 676 | |
| 677 | if (perf_pmu__config(pmu, &attr, head_config)) |
| 678 | return -EINVAL; |
| 679 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 680 | evsel = __add_event(list, idx, &attr, pmu_event_name(head_config), |
| 681 | pmu->cpus); |
| 682 | if (evsel) { |
Matt Fleming | 46441bd | 2014-09-24 15:04:06 +0100 | [diff] [blame] | 683 | evsel->unit = info.unit; |
| 684 | evsel->scale = info.scale; |
Matt Fleming | 044330c | 2014-11-21 10:31:12 +0100 | [diff] [blame] | 685 | evsel->per_pkg = info.per_pkg; |
Jiri Olsa | 1d9e446 | 2014-11-21 10:31:13 +0100 | [diff] [blame] | 686 | evsel->snapshot = info.snapshot; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | return evsel ? 0 : -ENOMEM; |
Jiri Olsa | 5f537a2 | 2012-03-15 20:09:18 +0100 | [diff] [blame] | 690 | } |
| 691 | |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 692 | int parse_events__modifier_group(struct list_head *list, |
| 693 | char *event_mod) |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 694 | { |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 695 | return parse_events__modifier_event(list, event_mod, true); |
| 696 | } |
| 697 | |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 698 | void parse_events__set_leader(char *name, struct list_head *list) |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 699 | { |
| 700 | struct perf_evsel *leader; |
| 701 | |
Arnaldo Carvalho de Melo | 63dab22 | 2012-08-14 16:35:48 -0300 | [diff] [blame] | 702 | __perf_evlist__set_leader(list); |
| 703 | leader = list_entry(list->next, struct perf_evsel, node); |
Jiri Olsa | 6a4bb04 | 2012-08-08 12:22:36 +0200 | [diff] [blame] | 704 | leader->group_name = name ? strdup(name) : NULL; |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 705 | } |
| 706 | |
David Ahern | c5cd8ac | 2013-07-02 13:27:25 -0600 | [diff] [blame] | 707 | /* list_event is assumed to point to malloc'ed memory */ |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 708 | void parse_events_update_lists(struct list_head *list_event, |
| 709 | struct list_head *list_all) |
| 710 | { |
| 711 | /* |
| 712 | * Called for single event definition. Update the |
Jiri Olsa | 89efb02 | 2012-08-08 12:14:14 +0200 | [diff] [blame] | 713 | * 'all event' list, and reinit the 'single event' |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 714 | * list, for next event definition. |
| 715 | */ |
| 716 | list_splice_tail(list_event, list_all); |
Jiri Olsa | b847cbd | 2012-05-21 09:12:51 +0200 | [diff] [blame] | 717 | free(list_event); |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 718 | } |
| 719 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 720 | struct event_modifier { |
| 721 | int eu; |
| 722 | int ek; |
| 723 | int eh; |
| 724 | int eH; |
| 725 | int eG; |
| 726 | int precise; |
| 727 | int exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 728 | int sample_read; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 729 | int pinned; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 730 | }; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 731 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 732 | static int get_event_modifier(struct event_modifier *mod, char *str, |
| 733 | struct perf_evsel *evsel) |
| 734 | { |
| 735 | int eu = evsel ? evsel->attr.exclude_user : 0; |
| 736 | int ek = evsel ? evsel->attr.exclude_kernel : 0; |
| 737 | int eh = evsel ? evsel->attr.exclude_hv : 0; |
| 738 | int eH = evsel ? evsel->attr.exclude_host : 0; |
| 739 | int eG = evsel ? evsel->attr.exclude_guest : 0; |
| 740 | int precise = evsel ? evsel->attr.precise_ip : 0; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 741 | int sample_read = 0; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 742 | int pinned = evsel ? evsel->attr.pinned : 0; |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 743 | |
| 744 | int exclude = eu | ek | eh; |
| 745 | int exclude_GH = evsel ? evsel->exclude_GH : 0; |
| 746 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 747 | memset(mod, 0, sizeof(*mod)); |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 748 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 749 | while (*str) { |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 750 | if (*str == 'u') { |
| 751 | if (!exclude) |
| 752 | exclude = eu = ek = eh = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 753 | eu = 0; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 754 | } else if (*str == 'k') { |
| 755 | if (!exclude) |
| 756 | exclude = eu = ek = eh = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 757 | ek = 0; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 758 | } else if (*str == 'h') { |
| 759 | if (!exclude) |
| 760 | exclude = eu = ek = eh = 1; |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 761 | eh = 0; |
Joerg Roedel | 99320cc | 2012-01-04 17:54:19 +0100 | [diff] [blame] | 762 | } else if (*str == 'G') { |
| 763 | if (!exclude_GH) |
| 764 | exclude_GH = eG = eH = 1; |
| 765 | eG = 0; |
| 766 | } else if (*str == 'H') { |
| 767 | if (!exclude_GH) |
| 768 | exclude_GH = eG = eH = 1; |
| 769 | eH = 0; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 770 | } else if (*str == 'p') { |
| 771 | precise++; |
David Ahern | 1342798 | 2012-09-13 14:59:13 -0600 | [diff] [blame] | 772 | /* use of precise requires exclude_guest */ |
| 773 | if (!exclude_GH) |
| 774 | eG = 1; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 775 | } else if (*str == 'S') { |
| 776 | sample_read = 1; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 777 | } else if (*str == 'D') { |
| 778 | pinned = 1; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 779 | } else |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 780 | break; |
Peter Zijlstra | ab60834 | 2010-04-08 23:03:20 +0200 | [diff] [blame] | 781 | |
Paul Mackerras | 61c4598 | 2009-07-01 13:04:34 +1000 | [diff] [blame] | 782 | ++str; |
| 783 | } |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 784 | |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 785 | /* |
| 786 | * precise ip: |
| 787 | * |
| 788 | * 0 - SAMPLE_IP can have arbitrary skid |
| 789 | * 1 - SAMPLE_IP must have constant skid |
| 790 | * 2 - SAMPLE_IP requested to have 0 skid |
| 791 | * 3 - SAMPLE_IP must have 0 skid |
| 792 | * |
| 793 | * See also PERF_RECORD_MISC_EXACT_IP |
| 794 | */ |
| 795 | if (precise > 3) |
| 796 | return -EINVAL; |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 797 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 798 | mod->eu = eu; |
| 799 | mod->ek = ek; |
| 800 | mod->eh = eh; |
| 801 | mod->eH = eH; |
| 802 | mod->eG = eG; |
| 803 | mod->precise = precise; |
| 804 | mod->exclude_GH = exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 805 | mod->sample_read = sample_read; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 806 | mod->pinned = pinned; |
| 807 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 808 | return 0; |
| 809 | } |
| 810 | |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 811 | /* |
| 812 | * Basic modifier sanity check to validate it contains only one |
| 813 | * instance of any modifier (apart from 'p') present. |
| 814 | */ |
| 815 | static int check_modifier(char *str) |
| 816 | { |
| 817 | char *p = str; |
| 818 | |
| 819 | /* The sizeof includes 0 byte as well. */ |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 820 | if (strlen(str) > (sizeof("ukhGHpppSD") - 1)) |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 821 | return -1; |
| 822 | |
| 823 | while (*p) { |
| 824 | if (*p != 'p' && strchr(p + 1, *p)) |
| 825 | return -1; |
| 826 | p++; |
| 827 | } |
| 828 | |
| 829 | return 0; |
| 830 | } |
| 831 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 832 | int parse_events__modifier_event(struct list_head *list, char *str, bool add) |
| 833 | { |
| 834 | struct perf_evsel *evsel; |
| 835 | struct event_modifier mod; |
| 836 | |
| 837 | if (str == NULL) |
| 838 | return 0; |
| 839 | |
Jiri Olsa | 534123f | 2012-11-13 15:32:58 +0100 | [diff] [blame] | 840 | if (check_modifier(str)) |
| 841 | return -EINVAL; |
| 842 | |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 843 | if (!add && get_event_modifier(&mod, str, NULL)) |
| 844 | return -EINVAL; |
| 845 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 846 | __evlist__for_each(list, evsel) { |
Jiri Olsa | f5b1135 | 2012-08-08 12:21:54 +0200 | [diff] [blame] | 847 | if (add && get_event_modifier(&mod, str, evsel)) |
| 848 | return -EINVAL; |
| 849 | |
| 850 | evsel->attr.exclude_user = mod.eu; |
| 851 | evsel->attr.exclude_kernel = mod.ek; |
| 852 | evsel->attr.exclude_hv = mod.eh; |
| 853 | evsel->attr.precise_ip = mod.precise; |
| 854 | evsel->attr.exclude_host = mod.eH; |
| 855 | evsel->attr.exclude_guest = mod.eG; |
| 856 | evsel->exclude_GH = mod.exclude_GH; |
Jiri Olsa | 3c17631 | 2012-10-10 17:39:03 +0200 | [diff] [blame] | 857 | evsel->sample_read = mod.sample_read; |
Michael Ellerman | e9a7c41 | 2013-08-06 23:28:05 +1000 | [diff] [blame] | 858 | |
| 859 | if (perf_evsel__is_group_leader(evsel)) |
| 860 | evsel->attr.pinned = mod.pinned; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 861 | } |
Ingo Molnar | ceb53fb | 2011-04-27 04:06:33 +0200 | [diff] [blame] | 862 | |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
Robert Richter | ac2ba9f | 2012-08-16 21:10:21 +0200 | [diff] [blame] | 866 | int parse_events_name(struct list_head *list, char *name) |
| 867 | { |
| 868 | struct perf_evsel *evsel; |
| 869 | |
Arnaldo Carvalho de Melo | 0050f7a | 2014-01-10 10:37:27 -0300 | [diff] [blame] | 870 | __evlist__for_each(list, evsel) { |
Robert Richter | ac2ba9f | 2012-08-16 21:10:21 +0200 | [diff] [blame] | 871 | if (!evsel->name) |
| 872 | evsel->name = strdup(name); |
| 873 | } |
| 874 | |
| 875 | return 0; |
| 876 | } |
| 877 | |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 878 | static int |
| 879 | comp_pmu(const void *p1, const void *p2) |
| 880 | { |
| 881 | struct perf_pmu_event_symbol *pmu1 = (struct perf_pmu_event_symbol *) p1; |
| 882 | struct perf_pmu_event_symbol *pmu2 = (struct perf_pmu_event_symbol *) p2; |
| 883 | |
| 884 | return strcmp(pmu1->symbol, pmu2->symbol); |
| 885 | } |
| 886 | |
| 887 | static void perf_pmu__parse_cleanup(void) |
| 888 | { |
| 889 | if (perf_pmu_events_list_num > 0) { |
| 890 | struct perf_pmu_event_symbol *p; |
| 891 | int i; |
| 892 | |
| 893 | for (i = 0; i < perf_pmu_events_list_num; i++) { |
| 894 | p = perf_pmu_events_list + i; |
| 895 | free(p->symbol); |
| 896 | } |
| 897 | free(perf_pmu_events_list); |
| 898 | perf_pmu_events_list = NULL; |
| 899 | perf_pmu_events_list_num = 0; |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | #define SET_SYMBOL(str, stype) \ |
| 904 | do { \ |
| 905 | p->symbol = str; \ |
| 906 | if (!p->symbol) \ |
| 907 | goto err; \ |
| 908 | p->type = stype; \ |
| 909 | } while (0) |
| 910 | |
| 911 | /* |
| 912 | * Read the pmu events list from sysfs |
| 913 | * Save it into perf_pmu_events_list |
| 914 | */ |
| 915 | static void perf_pmu__parse_init(void) |
| 916 | { |
| 917 | |
| 918 | struct perf_pmu *pmu = NULL; |
| 919 | struct perf_pmu_alias *alias; |
| 920 | int len = 0; |
| 921 | |
| 922 | pmu = perf_pmu__find("cpu"); |
| 923 | if ((pmu == NULL) || list_empty(&pmu->aliases)) { |
| 924 | perf_pmu_events_list_num = -1; |
| 925 | return; |
| 926 | } |
| 927 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 928 | if (strchr(alias->name, '-')) |
| 929 | len++; |
| 930 | len++; |
| 931 | } |
| 932 | perf_pmu_events_list = malloc(sizeof(struct perf_pmu_event_symbol) * len); |
| 933 | if (!perf_pmu_events_list) |
| 934 | return; |
| 935 | perf_pmu_events_list_num = len; |
| 936 | |
| 937 | len = 0; |
| 938 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 939 | struct perf_pmu_event_symbol *p = perf_pmu_events_list + len; |
| 940 | char *tmp = strchr(alias->name, '-'); |
| 941 | |
| 942 | if (tmp != NULL) { |
| 943 | SET_SYMBOL(strndup(alias->name, tmp - alias->name), |
| 944 | PMU_EVENT_SYMBOL_PREFIX); |
| 945 | p++; |
| 946 | SET_SYMBOL(strdup(++tmp), PMU_EVENT_SYMBOL_SUFFIX); |
| 947 | len += 2; |
| 948 | } else { |
| 949 | SET_SYMBOL(strdup(alias->name), PMU_EVENT_SYMBOL); |
| 950 | len++; |
| 951 | } |
| 952 | } |
| 953 | qsort(perf_pmu_events_list, len, |
| 954 | sizeof(struct perf_pmu_event_symbol), comp_pmu); |
| 955 | |
| 956 | return; |
| 957 | err: |
| 958 | perf_pmu__parse_cleanup(); |
| 959 | } |
| 960 | |
| 961 | enum perf_pmu_event_symbol_type |
| 962 | perf_pmu__parse_check(const char *name) |
| 963 | { |
| 964 | struct perf_pmu_event_symbol p, *r; |
| 965 | |
| 966 | /* scan kernel pmu events from sysfs if needed */ |
| 967 | if (perf_pmu_events_list_num == 0) |
| 968 | perf_pmu__parse_init(); |
| 969 | /* |
| 970 | * name "cpu" could be prefix of cpu-cycles or cpu// events. |
| 971 | * cpu-cycles has been handled by hardcode. |
| 972 | * So it must be cpu// events, not kernel pmu event. |
| 973 | */ |
| 974 | if ((perf_pmu_events_list_num <= 0) || !strcmp(name, "cpu")) |
| 975 | return PMU_EVENT_SYMBOL_ERR; |
| 976 | |
| 977 | p.symbol = strdup(name); |
| 978 | r = bsearch(&p, perf_pmu_events_list, |
| 979 | (size_t) perf_pmu_events_list_num, |
| 980 | sizeof(struct perf_pmu_event_symbol), comp_pmu); |
| 981 | free(p.symbol); |
| 982 | return r ? r->type : PMU_EVENT_SYMBOL_ERR; |
| 983 | } |
| 984 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 985 | static int parse_events__scanner(const char *str, void *data, int start_token) |
Zheng Yan | ac20de6 | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 986 | { |
| 987 | YY_BUFFER_STATE buffer; |
| 988 | void *scanner; |
| 989 | int ret; |
| 990 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 991 | ret = parse_events_lex_init_extra(start_token, &scanner); |
Zheng Yan | ac20de6 | 2012-06-15 14:31:39 +0800 | [diff] [blame] | 992 | if (ret) |
| 993 | return ret; |
| 994 | |
| 995 | buffer = parse_events__scan_string(str, scanner); |
| 996 | |
| 997 | #ifdef PARSER_DEBUG |
| 998 | parse_events_debug = 1; |
| 999 | #endif |
| 1000 | ret = parse_events_parse(data, scanner); |
| 1001 | |
| 1002 | parse_events__flush_buffer(buffer, scanner); |
| 1003 | parse_events__delete_buffer(buffer, scanner); |
| 1004 | parse_events_lex_destroy(scanner); |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1008 | /* |
| 1009 | * parse event config string, return a list of event terms. |
| 1010 | */ |
| 1011 | int parse_events_terms(struct list_head *terms, const char *str) |
| 1012 | { |
Arnaldo Carvalho de Melo | 23b6339 | 2013-01-18 16:56:57 -0300 | [diff] [blame] | 1013 | struct parse_events_terms data = { |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1014 | .terms = NULL, |
| 1015 | }; |
| 1016 | int ret; |
| 1017 | |
| 1018 | ret = parse_events__scanner(str, &data, PE_START_TERMS); |
| 1019 | if (!ret) { |
| 1020 | list_splice(data.terms, terms); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 1021 | zfree(&data.terms); |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1022 | return 0; |
| 1023 | } |
| 1024 | |
Adrian Hunter | b2c34fd | 2013-07-04 16:20:23 +0300 | [diff] [blame] | 1025 | if (data.terms) |
| 1026 | parse_events__free_terms(data.terms); |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1027 | return ret; |
| 1028 | } |
| 1029 | |
Jiri Olsa | d8f7bbc | 2013-01-15 14:39:51 +0100 | [diff] [blame] | 1030 | int parse_events(struct perf_evlist *evlist, const char *str) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1031 | { |
Arnaldo Carvalho de Melo | 23b6339 | 2013-01-18 16:56:57 -0300 | [diff] [blame] | 1032 | struct parse_events_evlist data = { |
Jiri Olsa | 46010ab | 2012-06-15 14:31:38 +0800 | [diff] [blame] | 1033 | .list = LIST_HEAD_INIT(data.list), |
| 1034 | .idx = evlist->nr_entries, |
| 1035 | }; |
Jiri Olsa | 46010ab | 2012-06-15 14:31:38 +0800 | [diff] [blame] | 1036 | int ret; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1037 | |
Jiri Olsa | 90e2b22 | 2012-06-15 14:31:40 +0800 | [diff] [blame] | 1038 | ret = parse_events__scanner(str, &data, PE_START_EVENTS); |
Kan Liang | dcb4e10 | 2014-10-07 11:08:50 -0400 | [diff] [blame] | 1039 | perf_pmu__parse_cleanup(); |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1040 | if (!ret) { |
Jiri Olsa | 46010ab | 2012-06-15 14:31:38 +0800 | [diff] [blame] | 1041 | int entries = data.idx - evlist->nr_entries; |
| 1042 | perf_evlist__splice_list_tail(evlist, &data.list, entries); |
Namhyung Kim | 97f63e4 | 2013-01-22 18:09:29 +0900 | [diff] [blame] | 1043 | evlist->nr_groups += data.nr_groups; |
Jiri Olsa | 89812fc | 2012-03-15 20:09:15 +0100 | [diff] [blame] | 1044 | return 0; |
| 1045 | } |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 1046 | |
Jiri Olsa | 5d7be90 | 2012-03-20 19:15:40 +0100 | [diff] [blame] | 1047 | /* |
| 1048 | * There are 2 users - builtin-record and builtin-test objects. |
| 1049 | * Both call perf_evlist__delete in case of error, so we dont |
| 1050 | * need to bother. |
| 1051 | */ |
Frederic Weisbecker | bcd3279 | 2009-09-11 23:19:45 +0200 | [diff] [blame] | 1052 | return ret; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1053 | } |
| 1054 | |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 1055 | int parse_events_option(const struct option *opt, const char *str, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1056 | int unset __maybe_unused) |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 1057 | { |
| 1058 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; |
Jiri Olsa | d8f7bbc | 2013-01-15 14:39:51 +0100 | [diff] [blame] | 1059 | int ret = parse_events(evlist, str); |
Andi Kleen | 9175ce1 | 2012-10-26 13:30:06 -0700 | [diff] [blame] | 1060 | |
| 1061 | if (ret) { |
| 1062 | fprintf(stderr, "invalid or unsupported event: '%s'\n", str); |
| 1063 | fprintf(stderr, "Run 'perf list' for a list of valid events\n"); |
| 1064 | } |
| 1065 | return ret; |
Jiri Olsa | f120f9d | 2011-07-14 11:25:32 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1068 | int parse_filter(const struct option *opt, const char *str, |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 1069 | int unset __maybe_unused) |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1070 | { |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1071 | struct perf_evlist *evlist = *(struct perf_evlist **)opt->value; |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1072 | struct perf_evsel *last = NULL; |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1073 | |
Arnaldo Carvalho de Melo | 361c99a | 2011-01-11 20:56:53 -0200 | [diff] [blame] | 1074 | if (evlist->nr_entries > 0) |
Arnaldo Carvalho de Melo | 0c21f73 | 2012-08-14 16:42:15 -0300 | [diff] [blame] | 1075 | last = perf_evlist__last(evlist); |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1076 | |
| 1077 | if (last == NULL || last->attr.type != PERF_TYPE_TRACEPOINT) { |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1078 | fprintf(stderr, |
Arnaldo Carvalho de Melo | 281f92f | 2014-10-01 15:05:32 -0300 | [diff] [blame] | 1079 | "--filter option should follow a -e tracepoint option\n"); |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1080 | return -1; |
| 1081 | } |
| 1082 | |
Arnaldo Carvalho de Melo | 69aad6f | 2011-01-03 16:39:04 -0200 | [diff] [blame] | 1083 | last->filter = strdup(str); |
| 1084 | if (last->filter == NULL) { |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1085 | fprintf(stderr, "not enough memory to hold filter string\n"); |
| 1086 | return -1; |
| 1087 | } |
Li Zefan | c171b55 | 2009-10-15 11:22:07 +0800 | [diff] [blame] | 1088 | |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1092 | static const char * const event_type_descriptors[] = { |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1093 | "Hardware event", |
| 1094 | "Software event", |
| 1095 | "Tracepoint event", |
| 1096 | "Hardware cache event", |
Liming Wang | 41bdcb2 | 2009-12-29 16:37:07 +0800 | [diff] [blame] | 1097 | "Raw hardware event descriptor", |
| 1098 | "Hardware breakpoint", |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1099 | }; |
| 1100 | |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1101 | /* |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1102 | * Print the events from <debugfs_mount_point>/tracing/events |
| 1103 | */ |
| 1104 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1105 | void print_tracepoint_events(const char *subsys_glob, const char *event_glob, |
| 1106 | bool name_only) |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1107 | { |
| 1108 | DIR *sys_dir, *evt_dir; |
| 1109 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1110 | char evt_path[MAXPATHLEN]; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1111 | char dir_path[MAXPATHLEN]; |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 1112 | char sbuf[STRERR_BUFSIZE]; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1113 | |
Pekka Enberg | f11cfc6 | 2013-10-15 23:07:27 +0300 | [diff] [blame] | 1114 | if (debugfs_valid_mountpoint(tracing_events_path)) { |
Masami Hiramatsu | 6e81c74 | 2014-08-14 02:22:36 +0000 | [diff] [blame] | 1115 | printf(" [ Tracepoints not available: %s ]\n", |
| 1116 | strerror_r(errno, sbuf, sizeof(sbuf))); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1117 | return; |
Pekka Enberg | f11cfc6 | 2013-10-15 23:07:27 +0300 | [diff] [blame] | 1118 | } |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1119 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1120 | sys_dir = opendir(tracing_events_path); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1121 | if (!sys_dir) |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1122 | return; |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1123 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 1124 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 1125 | if (subsys_glob != NULL && |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1126 | !strglobmatch(sys_dirent.d_name, subsys_glob)) |
| 1127 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1128 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1129 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1130 | sys_dirent.d_name); |
| 1131 | evt_dir = opendir(dir_path); |
| 1132 | if (!evt_dir) |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 1133 | continue; |
Eric Dumazet | 725b136 | 2009-09-24 15:39:09 +0200 | [diff] [blame] | 1134 | |
Ulrich Drepper | 6b58e7f | 2009-09-04 16:39:51 -0300 | [diff] [blame] | 1135 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 1136 | if (event_glob != NULL && |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1137 | !strglobmatch(evt_dirent.d_name, event_glob)) |
| 1138 | continue; |
| 1139 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1140 | if (name_only) { |
| 1141 | printf("%s:%s ", sys_dirent.d_name, evt_dirent.d_name); |
| 1142 | continue; |
| 1143 | } |
| 1144 | |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1145 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
| 1146 | sys_dirent.d_name, evt_dirent.d_name); |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 1147 | printf(" %-50s [%s]\n", evt_path, |
Liming Wang | 41bdcb2 | 2009-12-29 16:37:07 +0800 | [diff] [blame] | 1148 | event_type_descriptors[PERF_TYPE_TRACEPOINT]); |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1149 | } |
| 1150 | closedir(evt_dir); |
| 1151 | } |
Jason Baron | f6bdafe | 2009-07-21 12:20:22 -0400 | [diff] [blame] | 1152 | closedir(sys_dir); |
| 1153 | } |
| 1154 | |
| 1155 | /* |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1156 | * Check whether event is in <debugfs_mount_point>/tracing/events |
| 1157 | */ |
| 1158 | |
| 1159 | int is_valid_tracepoint(const char *event_string) |
| 1160 | { |
| 1161 | DIR *sys_dir, *evt_dir; |
| 1162 | struct dirent *sys_next, *evt_next, sys_dirent, evt_dirent; |
| 1163 | char evt_path[MAXPATHLEN]; |
| 1164 | char dir_path[MAXPATHLEN]; |
| 1165 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1166 | if (debugfs_valid_mountpoint(tracing_events_path)) |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1167 | return 0; |
| 1168 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1169 | sys_dir = opendir(tracing_events_path); |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1170 | if (!sys_dir) |
| 1171 | return 0; |
| 1172 | |
| 1173 | for_each_subsystem(sys_dir, sys_dirent, sys_next) { |
| 1174 | |
Arnaldo Carvalho de Melo | ebf294b | 2011-11-16 14:03:07 -0200 | [diff] [blame] | 1175 | snprintf(dir_path, MAXPATHLEN, "%s/%s", tracing_events_path, |
Thomas Renninger | 20c457b | 2011-01-03 17:50:45 +0100 | [diff] [blame] | 1176 | sys_dirent.d_name); |
| 1177 | evt_dir = opendir(dir_path); |
| 1178 | if (!evt_dir) |
| 1179 | continue; |
| 1180 | |
| 1181 | for_each_event(sys_dirent, evt_dir, evt_dirent, evt_next) { |
| 1182 | snprintf(evt_path, MAXPATHLEN, "%s:%s", |
| 1183 | sys_dirent.d_name, evt_dirent.d_name); |
| 1184 | if (!strcmp(evt_path, event_string)) { |
| 1185 | closedir(evt_dir); |
| 1186 | closedir(sys_dir); |
| 1187 | return 1; |
| 1188 | } |
| 1189 | } |
| 1190 | closedir(evt_dir); |
| 1191 | } |
| 1192 | closedir(sys_dir); |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1196 | static bool is_event_supported(u8 type, unsigned config) |
| 1197 | { |
| 1198 | bool ret = true; |
Vince Weaver | 88fee52 | 2013-12-30 15:39:45 -0500 | [diff] [blame] | 1199 | int open_return; |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1200 | struct perf_evsel *evsel; |
| 1201 | struct perf_event_attr attr = { |
| 1202 | .type = type, |
| 1203 | .config = config, |
| 1204 | .disabled = 1, |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1205 | }; |
| 1206 | struct { |
| 1207 | struct thread_map map; |
| 1208 | int threads[1]; |
| 1209 | } tmap = { |
| 1210 | .map.nr = 1, |
| 1211 | .threads = { 0 }, |
| 1212 | }; |
| 1213 | |
Arnaldo Carvalho de Melo | ef50383 | 2013-11-07 16:41:19 -0300 | [diff] [blame] | 1214 | evsel = perf_evsel__new(&attr); |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1215 | if (evsel) { |
Vince Weaver | 88fee52 | 2013-12-30 15:39:45 -0500 | [diff] [blame] | 1216 | open_return = perf_evsel__open(evsel, NULL, &tmap.map); |
| 1217 | ret = open_return >= 0; |
| 1218 | |
| 1219 | if (open_return == -EACCES) { |
| 1220 | /* |
| 1221 | * This happens if the paranoid value |
| 1222 | * /proc/sys/kernel/perf_event_paranoid is set to 2 |
| 1223 | * Re-run with exclude_kernel set; we don't do that |
| 1224 | * by default as some ARM machines do not support it. |
| 1225 | * |
| 1226 | */ |
| 1227 | evsel->attr.exclude_kernel = 1; |
| 1228 | ret = perf_evsel__open(evsel, NULL, &tmap.map) >= 0; |
| 1229 | } |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1230 | perf_evsel__delete(evsel); |
| 1231 | } |
| 1232 | |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1236 | static void __print_events_type(u8 type, struct event_symbol *syms, |
| 1237 | unsigned max) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1238 | { |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1239 | char name[64]; |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1240 | unsigned i; |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1241 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1242 | for (i = 0; i < max ; i++, syms++) { |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1243 | if (!is_event_supported(type, i)) |
| 1244 | continue; |
| 1245 | |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1246 | if (strlen(syms->alias)) |
| 1247 | snprintf(name, sizeof(name), "%s OR %s", |
| 1248 | syms->symbol, syms->alias); |
| 1249 | else |
| 1250 | snprintf(name, sizeof(name), "%s", syms->symbol); |
| 1251 | |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1252 | printf(" %-50s [%s]\n", name, event_type_descriptors[type]); |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1253 | } |
| 1254 | } |
| 1255 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1256 | void print_events_type(u8 type) |
| 1257 | { |
| 1258 | if (type == PERF_TYPE_SOFTWARE) |
| 1259 | __print_events_type(type, event_symbols_sw, PERF_COUNT_SW_MAX); |
| 1260 | else |
| 1261 | __print_events_type(type, event_symbols_hw, PERF_COUNT_HW_MAX); |
| 1262 | } |
| 1263 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1264 | 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] | 1265 | { |
| 1266 | unsigned int type, op, i, printed = 0; |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 1267 | char name[64]; |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1268 | |
| 1269 | for (type = 0; type < PERF_COUNT_HW_CACHE_MAX; type++) { |
| 1270 | for (op = 0; op < PERF_COUNT_HW_CACHE_OP_MAX; op++) { |
| 1271 | /* skip invalid cache type */ |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 1272 | if (!perf_evsel__is_cache_op_valid(type, op)) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1273 | continue; |
| 1274 | |
| 1275 | for (i = 0; i < PERF_COUNT_HW_CACHE_RESULT_MAX; i++) { |
Arnaldo Carvalho de Melo | 0b668bc | 2012-06-11 14:08:07 -0300 | [diff] [blame] | 1276 | __perf_evsel__hw_cache_type_op_res_name(type, op, i, |
| 1277 | name, sizeof(name)); |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 1278 | if (event_glob != NULL && !strglobmatch(name, event_glob)) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1279 | continue; |
| 1280 | |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1281 | if (!is_event_supported(PERF_TYPE_HW_CACHE, |
| 1282 | type | (op << 8) | (i << 16))) |
| 1283 | continue; |
| 1284 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1285 | if (name_only) |
| 1286 | printf("%s ", name); |
| 1287 | else |
| 1288 | printf(" %-50s [%s]\n", name, |
| 1289 | event_type_descriptors[PERF_TYPE_HW_CACHE]); |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1290 | ++printed; |
| 1291 | } |
| 1292 | } |
| 1293 | } |
| 1294 | |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1295 | if (printed) |
| 1296 | printf("\n"); |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1297 | return printed; |
| 1298 | } |
| 1299 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1300 | static void print_symbol_events(const char *event_glob, unsigned type, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1301 | struct event_symbol *syms, unsigned max, |
| 1302 | bool name_only) |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1303 | { |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1304 | unsigned i, printed = 0; |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 1305 | char name[MAX_NAME_LEN]; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1306 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1307 | for (i = 0; i < max; i++, syms++) { |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1308 | |
Arnaldo Carvalho de Melo | 48000a1 | 2014-12-17 17:24:45 -0300 | [diff] [blame] | 1309 | if (event_glob != NULL && |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1310 | !(strglobmatch(syms->symbol, event_glob) || |
| 1311 | (syms->alias && strglobmatch(syms->alias, event_glob)))) |
| 1312 | continue; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1313 | |
Namhyung Kim | b41f1ce | 2013-08-27 11:41:53 +0900 | [diff] [blame] | 1314 | if (!is_event_supported(type, i)) |
| 1315 | continue; |
| 1316 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1317 | if (name_only) { |
| 1318 | printf("%s ", syms->symbol); |
| 1319 | continue; |
| 1320 | } |
| 1321 | |
Jaswinder Singh Rajput | 74d5b58 | 2009-06-22 16:44:28 +0530 | [diff] [blame] | 1322 | if (strlen(syms->alias)) |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 1323 | 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] | 1324 | else |
Ingo Molnar | 947b4ad | 2011-04-29 22:52:42 +0200 | [diff] [blame] | 1325 | strncpy(name, syms->symbol, MAX_NAME_LEN); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1326 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1327 | printf(" %-50s [%s]\n", name, event_type_descriptors[type]); |
| 1328 | |
| 1329 | printed++; |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1330 | } |
| 1331 | |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1332 | if (printed) |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1333 | printf("\n"); |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Print the help text for the event symbols: |
| 1338 | */ |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1339 | void print_events(const char *event_glob, bool name_only) |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1340 | { |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1341 | if (!name_only) { |
| 1342 | printf("\n"); |
| 1343 | printf("List of pre-defined events (to be used in -e):\n"); |
| 1344 | } |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1345 | |
| 1346 | print_symbol_events(event_glob, PERF_TYPE_HARDWARE, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1347 | event_symbols_hw, PERF_COUNT_HW_MAX, name_only); |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1348 | |
| 1349 | print_symbol_events(event_glob, PERF_TYPE_SOFTWARE, |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1350 | event_symbols_sw, PERF_COUNT_SW_MAX, name_only); |
Jiri Olsa | 1dc1276 | 2012-07-04 00:00:44 +0200 | [diff] [blame] | 1351 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1352 | print_hwcache_events(event_glob, name_only); |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1353 | |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 1354 | print_pmu_events(event_glob, name_only); |
| 1355 | |
Arnaldo Carvalho de Melo | 668b878 | 2011-02-17 15:38:58 -0200 | [diff] [blame] | 1356 | if (event_glob != NULL) |
| 1357 | return; |
Jaswinder Singh Rajput | 73c24cb | 2009-07-01 18:36:18 +0530 | [diff] [blame] | 1358 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1359 | if (!name_only) { |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1360 | printf(" %-50s [%s]\n", |
| 1361 | "rNNN", |
| 1362 | event_type_descriptors[PERF_TYPE_RAW]); |
| 1363 | printf(" %-50s [%s]\n", |
| 1364 | "cpu/t1=v1[,t2=v2,t3 ...]/modifier", |
| 1365 | event_type_descriptors[PERF_TYPE_RAW]); |
Borislav Petkov | af3df2c | 2012-09-28 19:47:07 +0200 | [diff] [blame] | 1366 | printf(" (see 'man perf-list' on how to encode it)\n"); |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1367 | printf("\n"); |
Thomas Gleixner | 86847b6 | 2009-06-06 12:24:17 +0200 | [diff] [blame] | 1368 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1369 | printf(" %-50s [%s]\n", |
Jacob Shin | 3741eb9 | 2014-05-29 17:26:51 +0200 | [diff] [blame] | 1370 | "mem:<addr>[/len][:access]", |
Liming Wang | 41bdcb2 | 2009-12-29 16:37:07 +0800 | [diff] [blame] | 1371 | event_type_descriptors[PERF_TYPE_BREAKPOINT]); |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1372 | printf("\n"); |
| 1373 | } |
Frederic Weisbecker | 1b290d6 | 2009-11-23 15:42:35 +0100 | [diff] [blame] | 1374 | |
Frederic Weisbecker | a3277d2 | 2012-08-09 16:31:52 +0200 | [diff] [blame] | 1375 | print_tracepoint_events(NULL, NULL, name_only); |
Ingo Molnar | 8ad8db3 | 2009-05-26 11:10:09 +0200 | [diff] [blame] | 1376 | } |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1377 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1378 | int parse_events__is_hardcoded_term(struct parse_events_term *term) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1379 | { |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1380 | return term->type_term != PARSE_EVENTS__TERM_TYPE_USER; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1381 | } |
| 1382 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1383 | static int new_term(struct parse_events_term **_term, int type_val, |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1384 | int type_term, char *config, |
Robert Richter | b527bab | 2012-08-07 19:43:13 +0200 | [diff] [blame] | 1385 | char *str, u64 num) |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1386 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1387 | struct parse_events_term *term; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1388 | |
| 1389 | term = zalloc(sizeof(*term)); |
| 1390 | if (!term) |
| 1391 | return -ENOMEM; |
| 1392 | |
| 1393 | INIT_LIST_HEAD(&term->list); |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1394 | term->type_val = type_val; |
| 1395 | term->type_term = type_term; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1396 | term->config = config; |
| 1397 | |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1398 | switch (type_val) { |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1399 | case PARSE_EVENTS__TERM_TYPE_NUM: |
| 1400 | term->val.num = num; |
| 1401 | break; |
| 1402 | case PARSE_EVENTS__TERM_TYPE_STR: |
| 1403 | term->val.str = str; |
| 1404 | break; |
| 1405 | default: |
Adrian Hunter | 4be8be6 | 2013-07-04 16:20:24 +0300 | [diff] [blame] | 1406 | free(term); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1407 | return -EINVAL; |
| 1408 | } |
| 1409 | |
| 1410 | *_term = term; |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1414 | int parse_events_term__num(struct parse_events_term **term, |
Robert Richter | b527bab | 2012-08-07 19:43:13 +0200 | [diff] [blame] | 1415 | int type_term, char *config, u64 num) |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1416 | { |
| 1417 | return new_term(term, PARSE_EVENTS__TERM_TYPE_NUM, type_term, |
| 1418 | config, NULL, num); |
| 1419 | } |
| 1420 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1421 | int parse_events_term__str(struct parse_events_term **term, |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 1422 | int type_term, char *config, char *str) |
| 1423 | { |
| 1424 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, type_term, |
| 1425 | config, str, 0); |
| 1426 | } |
| 1427 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1428 | int parse_events_term__sym_hw(struct parse_events_term **term, |
Jiri Olsa | 1d33d6d | 2012-10-10 14:53:17 +0200 | [diff] [blame] | 1429 | char *config, unsigned idx) |
| 1430 | { |
| 1431 | struct event_symbol *sym; |
| 1432 | |
| 1433 | BUG_ON(idx >= PERF_COUNT_HW_MAX); |
| 1434 | sym = &event_symbols_hw[idx]; |
| 1435 | |
| 1436 | if (config) |
| 1437 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, |
| 1438 | PARSE_EVENTS__TERM_TYPE_USER, config, |
| 1439 | (char *) sym->symbol, 0); |
| 1440 | else |
| 1441 | return new_term(term, PARSE_EVENTS__TERM_TYPE_STR, |
| 1442 | PARSE_EVENTS__TERM_TYPE_USER, |
| 1443 | (char *) "event", (char *) sym->symbol, 0); |
| 1444 | } |
| 1445 | |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1446 | int parse_events_term__clone(struct parse_events_term **new, |
| 1447 | struct parse_events_term *term) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 1448 | { |
| 1449 | return new_term(new, term->type_val, term->type_term, term->config, |
| 1450 | term->val.str, term->val.num); |
| 1451 | } |
| 1452 | |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1453 | void parse_events__free_terms(struct list_head *terms) |
| 1454 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 1455 | struct parse_events_term *term, *h; |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1456 | |
| 1457 | list_for_each_entry_safe(term, h, terms, list) |
| 1458 | free(term); |
Jiri Olsa | 8f707d8 | 2012-03-15 20:09:16 +0100 | [diff] [blame] | 1459 | } |