Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 1 | #include <linux/list.h> |
| 2 | #include <sys/types.h> |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 3 | #include <unistd.h> |
| 4 | #include <stdio.h> |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 5 | #include <stdbool.h> |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 6 | #include <dirent.h> |
Borislav Petkov | cd0cfad | 2013-12-09 17:14:24 +0100 | [diff] [blame] | 7 | #include <api/fs/fs.h> |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 8 | #include <locale.h> |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 9 | #include "util.h" |
| 10 | #include "pmu.h" |
| 11 | #include "parse-events.h" |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 12 | #include "cpumap.h" |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 13 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 14 | #define UNIT_MAX_LEN 31 /* max length for event unit name */ |
| 15 | |
Arnaldo Carvalho de Melo | ab1bf653 | 2013-01-18 17:05:09 -0300 | [diff] [blame] | 16 | struct perf_pmu_alias { |
| 17 | char *name; |
Cody P Schafer | 885b593 | 2014-08-15 00:26:14 -0700 | [diff] [blame] | 18 | struct list_head terms; /* HEAD struct parse_events_term -> list */ |
| 19 | struct list_head list; /* ELEM */ |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 20 | char unit[UNIT_MAX_LEN+1]; |
| 21 | double scale; |
Arnaldo Carvalho de Melo | ab1bf653 | 2013-01-18 17:05:09 -0300 | [diff] [blame] | 22 | }; |
| 23 | |
| 24 | struct perf_pmu_format { |
| 25 | char *name; |
| 26 | int value; |
| 27 | DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); |
| 28 | struct list_head list; |
| 29 | }; |
| 30 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 31 | #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/" |
| 32 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 33 | int perf_pmu_parse(struct list_head *list, char *name); |
| 34 | extern FILE *perf_pmu_in; |
| 35 | |
| 36 | static LIST_HEAD(pmus); |
| 37 | |
| 38 | /* |
| 39 | * Parse & process all the sysfs attributes located under |
| 40 | * the directory specified in 'dir' parameter. |
| 41 | */ |
Jiri Olsa | cff7f95 | 2012-11-10 01:46:50 +0100 | [diff] [blame] | 42 | int perf_pmu__format_parse(char *dir, struct list_head *head) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 43 | { |
| 44 | struct dirent *evt_ent; |
| 45 | DIR *format_dir; |
| 46 | int ret = 0; |
| 47 | |
| 48 | format_dir = opendir(dir); |
| 49 | if (!format_dir) |
| 50 | return -EINVAL; |
| 51 | |
| 52 | while (!ret && (evt_ent = readdir(format_dir))) { |
| 53 | char path[PATH_MAX]; |
| 54 | char *name = evt_ent->d_name; |
| 55 | FILE *file; |
| 56 | |
| 57 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 58 | continue; |
| 59 | |
| 60 | snprintf(path, PATH_MAX, "%s/%s", dir, name); |
| 61 | |
| 62 | ret = -EINVAL; |
| 63 | file = fopen(path, "r"); |
| 64 | if (!file) |
| 65 | break; |
| 66 | |
| 67 | perf_pmu_in = file; |
| 68 | ret = perf_pmu_parse(head, name); |
| 69 | fclose(file); |
| 70 | } |
| 71 | |
| 72 | closedir(format_dir); |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Reading/parsing the default pmu format definition, which should be |
| 78 | * located at: |
| 79 | * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. |
| 80 | */ |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 81 | static int pmu_format(const char *name, struct list_head *format) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 82 | { |
| 83 | struct stat st; |
| 84 | char path[PATH_MAX]; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 85 | const char *sysfs = sysfs__mountpoint(); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 86 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 87 | if (!sysfs) |
| 88 | return -1; |
| 89 | |
| 90 | snprintf(path, PATH_MAX, |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 91 | "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 92 | |
| 93 | if (stat(path, &st) < 0) |
Robert Richter | 9bc8f9f | 2012-06-14 22:38:37 +0200 | [diff] [blame] | 94 | return 0; /* no error if format does not exist */ |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 95 | |
Jiri Olsa | cff7f95 | 2012-11-10 01:46:50 +0100 | [diff] [blame] | 96 | if (perf_pmu__format_parse(path, format)) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 97 | return -1; |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 102 | static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) |
| 103 | { |
| 104 | struct stat st; |
| 105 | ssize_t sret; |
| 106 | char scale[128]; |
| 107 | int fd, ret = -1; |
| 108 | char path[PATH_MAX]; |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 109 | const char *lc; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 110 | |
| 111 | snprintf(path, PATH_MAX, "%s/%s.scale", dir, name); |
| 112 | |
| 113 | fd = open(path, O_RDONLY); |
| 114 | if (fd == -1) |
| 115 | return -1; |
| 116 | |
| 117 | if (fstat(fd, &st) < 0) |
| 118 | goto error; |
| 119 | |
| 120 | sret = read(fd, scale, sizeof(scale)-1); |
| 121 | if (sret < 0) |
| 122 | goto error; |
| 123 | |
| 124 | scale[sret] = '\0'; |
| 125 | /* |
| 126 | * save current locale |
| 127 | */ |
| 128 | lc = setlocale(LC_NUMERIC, NULL); |
| 129 | |
| 130 | /* |
| 131 | * force to C locale to ensure kernel |
| 132 | * scale string is converted correctly. |
| 133 | * kernel uses default C locale. |
| 134 | */ |
| 135 | setlocale(LC_NUMERIC, "C"); |
| 136 | |
| 137 | alias->scale = strtod(scale, NULL); |
| 138 | |
| 139 | /* restore locale */ |
| 140 | setlocale(LC_NUMERIC, lc); |
| 141 | |
| 142 | ret = 0; |
| 143 | error: |
| 144 | close(fd); |
| 145 | return ret; |
| 146 | } |
| 147 | |
| 148 | static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) |
| 149 | { |
| 150 | char path[PATH_MAX]; |
| 151 | ssize_t sret; |
| 152 | int fd; |
| 153 | |
| 154 | snprintf(path, PATH_MAX, "%s/%s.unit", dir, name); |
| 155 | |
| 156 | fd = open(path, O_RDONLY); |
| 157 | if (fd == -1) |
| 158 | return -1; |
| 159 | |
| 160 | sret = read(fd, alias->unit, UNIT_MAX_LEN); |
| 161 | if (sret < 0) |
| 162 | goto error; |
| 163 | |
| 164 | close(fd); |
| 165 | |
| 166 | alias->unit[sret] = '\0'; |
| 167 | |
| 168 | return 0; |
| 169 | error: |
| 170 | close(fd); |
| 171 | alias->unit[0] = '\0'; |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 176 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 177 | struct perf_pmu_alias *alias; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 178 | char buf[256]; |
| 179 | int ret; |
| 180 | |
| 181 | ret = fread(buf, 1, sizeof(buf), file); |
| 182 | if (ret == 0) |
| 183 | return -EINVAL; |
| 184 | buf[ret] = 0; |
| 185 | |
| 186 | alias = malloc(sizeof(*alias)); |
| 187 | if (!alias) |
| 188 | return -ENOMEM; |
| 189 | |
| 190 | INIT_LIST_HEAD(&alias->terms); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 191 | alias->scale = 1.0; |
| 192 | alias->unit[0] = '\0'; |
| 193 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 194 | ret = parse_events_terms(&alias->terms, buf); |
| 195 | if (ret) { |
| 196 | free(alias); |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | alias->name = strdup(name); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 201 | /* |
| 202 | * load unit name and scale if available |
| 203 | */ |
| 204 | perf_pmu__parse_unit(alias, dir, name); |
| 205 | perf_pmu__parse_scale(alias, dir, name); |
| 206 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 207 | list_add_tail(&alias->list, list); |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 208 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Process all the sysfs attributes located under the directory |
| 214 | * specified in 'dir' parameter. |
| 215 | */ |
| 216 | static int pmu_aliases_parse(char *dir, struct list_head *head) |
| 217 | { |
| 218 | struct dirent *evt_ent; |
| 219 | DIR *event_dir; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 220 | size_t len; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 221 | int ret = 0; |
| 222 | |
| 223 | event_dir = opendir(dir); |
| 224 | if (!event_dir) |
| 225 | return -EINVAL; |
| 226 | |
| 227 | while (!ret && (evt_ent = readdir(event_dir))) { |
| 228 | char path[PATH_MAX]; |
| 229 | char *name = evt_ent->d_name; |
| 230 | FILE *file; |
| 231 | |
| 232 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
| 233 | continue; |
| 234 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 235 | /* |
| 236 | * skip .unit and .scale info files |
| 237 | * parsed in perf_pmu__new_alias() |
| 238 | */ |
| 239 | len = strlen(name); |
| 240 | if (len > 5 && !strcmp(name + len - 5, ".unit")) |
| 241 | continue; |
| 242 | if (len > 6 && !strcmp(name + len - 6, ".scale")) |
| 243 | continue; |
| 244 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 245 | snprintf(path, PATH_MAX, "%s/%s", dir, name); |
| 246 | |
| 247 | ret = -EINVAL; |
| 248 | file = fopen(path, "r"); |
| 249 | if (!file) |
| 250 | break; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 251 | |
| 252 | ret = perf_pmu__new_alias(head, dir, name, file); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 253 | fclose(file); |
| 254 | } |
| 255 | |
| 256 | closedir(event_dir); |
| 257 | return ret; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Reading the pmu event aliases definition, which should be located at: |
| 262 | * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. |
| 263 | */ |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 264 | static int pmu_aliases(const char *name, struct list_head *head) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 265 | { |
| 266 | struct stat st; |
| 267 | char path[PATH_MAX]; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 268 | const char *sysfs = sysfs__mountpoint(); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 269 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 270 | if (!sysfs) |
| 271 | return -1; |
| 272 | |
| 273 | snprintf(path, PATH_MAX, |
| 274 | "%s/bus/event_source/devices/%s/events", sysfs, name); |
| 275 | |
| 276 | if (stat(path, &st) < 0) |
Jiri Olsa | 3fded96 | 2012-10-10 14:53:16 +0200 | [diff] [blame] | 277 | return 0; /* no error if 'events' does not exist */ |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 278 | |
| 279 | if (pmu_aliases_parse(path, head)) |
| 280 | return -1; |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 285 | static int pmu_alias_terms(struct perf_pmu_alias *alias, |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 286 | struct list_head *terms) |
| 287 | { |
Jiri Olsa | 7c2f816 | 2014-04-16 20:49:02 +0200 | [diff] [blame] | 288 | struct parse_events_term *term, *cloned; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 289 | LIST_HEAD(list); |
| 290 | int ret; |
| 291 | |
| 292 | list_for_each_entry(term, &alias->terms, list) { |
Jiri Olsa | 7c2f816 | 2014-04-16 20:49:02 +0200 | [diff] [blame] | 293 | ret = parse_events_term__clone(&cloned, term); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 294 | if (ret) { |
| 295 | parse_events__free_terms(&list); |
| 296 | return ret; |
| 297 | } |
Jiri Olsa | 7c2f816 | 2014-04-16 20:49:02 +0200 | [diff] [blame] | 298 | list_add_tail(&cloned->list, &list); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 299 | } |
| 300 | list_splice(&list, terms); |
| 301 | return 0; |
| 302 | } |
| 303 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 304 | /* |
| 305 | * Reading/parsing the default pmu type value, which should be |
| 306 | * located at: |
| 307 | * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. |
| 308 | */ |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 309 | static int pmu_type(const char *name, __u32 *type) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 310 | { |
| 311 | struct stat st; |
| 312 | char path[PATH_MAX]; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 313 | FILE *file; |
| 314 | int ret = 0; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 315 | const char *sysfs = sysfs__mountpoint(); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 316 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 317 | if (!sysfs) |
| 318 | return -1; |
| 319 | |
| 320 | snprintf(path, PATH_MAX, |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 321 | "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 322 | |
| 323 | if (stat(path, &st) < 0) |
| 324 | return -1; |
| 325 | |
| 326 | file = fopen(path, "r"); |
| 327 | if (!file) |
| 328 | return -EINVAL; |
| 329 | |
| 330 | if (1 != fscanf(file, "%u", type)) |
| 331 | ret = -1; |
| 332 | |
| 333 | fclose(file); |
| 334 | return ret; |
| 335 | } |
| 336 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 337 | /* Add all pmus in sysfs to pmu list: */ |
| 338 | static void pmu_read_sysfs(void) |
| 339 | { |
| 340 | char path[PATH_MAX]; |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 341 | DIR *dir; |
| 342 | struct dirent *dent; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 343 | const char *sysfs = sysfs__mountpoint(); |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 344 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 345 | if (!sysfs) |
| 346 | return; |
| 347 | |
| 348 | snprintf(path, PATH_MAX, |
| 349 | "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); |
| 350 | |
| 351 | dir = opendir(path); |
| 352 | if (!dir) |
| 353 | return; |
| 354 | |
| 355 | while ((dent = readdir(dir))) { |
| 356 | if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) |
| 357 | continue; |
| 358 | /* add to static LIST_HEAD(pmus): */ |
| 359 | perf_pmu__find(dent->d_name); |
| 360 | } |
| 361 | |
| 362 | closedir(dir); |
| 363 | } |
| 364 | |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 365 | static struct cpu_map *pmu_cpumask(const char *name) |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 366 | { |
| 367 | struct stat st; |
| 368 | char path[PATH_MAX]; |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 369 | FILE *file; |
| 370 | struct cpu_map *cpus; |
Arnaldo Carvalho de Melo | cf38fad | 2013-11-05 14:48:50 -0300 | [diff] [blame] | 371 | const char *sysfs = sysfs__mountpoint(); |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 372 | |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 373 | if (!sysfs) |
| 374 | return NULL; |
| 375 | |
| 376 | snprintf(path, PATH_MAX, |
| 377 | "%s/bus/event_source/devices/%s/cpumask", sysfs, name); |
| 378 | |
| 379 | if (stat(path, &st) < 0) |
| 380 | return NULL; |
| 381 | |
| 382 | file = fopen(path, "r"); |
| 383 | if (!file) |
| 384 | return NULL; |
| 385 | |
| 386 | cpus = cpu_map__read(file); |
| 387 | fclose(file); |
| 388 | return cpus; |
| 389 | } |
| 390 | |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 391 | struct perf_event_attr *__attribute__((weak)) |
| 392 | perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) |
| 393 | { |
| 394 | return NULL; |
| 395 | } |
| 396 | |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 397 | static struct perf_pmu *pmu_lookup(const char *name) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 398 | { |
| 399 | struct perf_pmu *pmu; |
| 400 | LIST_HEAD(format); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 401 | LIST_HEAD(aliases); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 402 | __u32 type; |
| 403 | |
| 404 | /* |
| 405 | * The pmu data we store & need consists of the pmu |
| 406 | * type value and format definitions. Load both right |
| 407 | * now. |
| 408 | */ |
| 409 | if (pmu_format(name, &format)) |
| 410 | return NULL; |
| 411 | |
Jiri Olsa | 3fded96 | 2012-10-10 14:53:16 +0200 | [diff] [blame] | 412 | if (pmu_aliases(name, &aliases)) |
| 413 | return NULL; |
| 414 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 415 | if (pmu_type(name, &type)) |
| 416 | return NULL; |
| 417 | |
| 418 | pmu = zalloc(sizeof(*pmu)); |
| 419 | if (!pmu) |
| 420 | return NULL; |
| 421 | |
Yan, Zheng | 7ae92e7 | 2012-09-10 15:53:50 +0800 | [diff] [blame] | 422 | pmu->cpus = pmu_cpumask(name); |
| 423 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 424 | INIT_LIST_HEAD(&pmu->format); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 425 | INIT_LIST_HEAD(&pmu->aliases); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 426 | list_splice(&format, &pmu->format); |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 427 | list_splice(&aliases, &pmu->aliases); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 428 | pmu->name = strdup(name); |
| 429 | pmu->type = type; |
Robert Richter | 9bc8f9f | 2012-06-14 22:38:37 +0200 | [diff] [blame] | 430 | list_add_tail(&pmu->list, &pmus); |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 431 | |
| 432 | pmu->default_config = perf_pmu__get_default_config(pmu); |
| 433 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 434 | return pmu; |
| 435 | } |
| 436 | |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 437 | static struct perf_pmu *pmu_find(const char *name) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 438 | { |
| 439 | struct perf_pmu *pmu; |
| 440 | |
| 441 | list_for_each_entry(pmu, &pmus, list) |
| 442 | if (!strcmp(pmu->name, name)) |
| 443 | return pmu; |
| 444 | |
| 445 | return NULL; |
| 446 | } |
| 447 | |
Robert Richter | 50a9667 | 2012-08-16 21:10:24 +0200 | [diff] [blame] | 448 | struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) |
| 449 | { |
| 450 | /* |
| 451 | * pmu iterator: If pmu is NULL, we start at the begin, |
| 452 | * otherwise return the next pmu. Returns NULL on end. |
| 453 | */ |
| 454 | if (!pmu) { |
| 455 | pmu_read_sysfs(); |
| 456 | pmu = list_prepare_entry(pmu, &pmus, list); |
| 457 | } |
| 458 | list_for_each_entry_continue(pmu, &pmus, list) |
| 459 | return pmu; |
| 460 | return NULL; |
| 461 | } |
| 462 | |
Adrian Hunter | b6b96fb | 2013-07-04 16:20:25 +0300 | [diff] [blame] | 463 | struct perf_pmu *perf_pmu__find(const char *name) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 464 | { |
| 465 | struct perf_pmu *pmu; |
| 466 | |
| 467 | /* |
| 468 | * Once PMU is loaded it stays in the list, |
| 469 | * so we keep us from multiple reading/parsing |
| 470 | * the pmu format definitions. |
| 471 | */ |
| 472 | pmu = pmu_find(name); |
| 473 | if (pmu) |
| 474 | return pmu; |
| 475 | |
| 476 | return pmu_lookup(name); |
| 477 | } |
| 478 | |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 479 | static struct perf_pmu_format * |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 480 | pmu_find_format(struct list_head *formats, char *name) |
| 481 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 482 | struct perf_pmu_format *format; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 483 | |
| 484 | list_for_each_entry(format, formats, list) |
| 485 | if (!strcmp(format->name, name)) |
| 486 | return format; |
| 487 | |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | /* |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 492 | * Sets value based on the format definition (format parameter) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 493 | * and unformated value (value parameter). |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 494 | */ |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 495 | static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, |
| 496 | bool zero) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 497 | { |
| 498 | unsigned long fbit, vbit; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 499 | |
| 500 | for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { |
| 501 | |
| 502 | if (!test_bit(fbit, format)) |
| 503 | continue; |
| 504 | |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 505 | if (value & (1llu << vbit++)) |
| 506 | *v |= (1llu << fbit); |
| 507 | else if (zero) |
| 508 | *v &= ~(1llu << fbit); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 509 | } |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /* |
| 513 | * Setup one of config[12] attr members based on the |
Cody P Schafer | 88aca8d | 2014-01-08 08:43:51 -0800 | [diff] [blame] | 514 | * user input data - term parameter. |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 515 | */ |
| 516 | static int pmu_config_term(struct list_head *formats, |
| 517 | struct perf_event_attr *attr, |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 518 | struct parse_events_term *term, |
| 519 | bool zero) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 520 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 521 | struct perf_pmu_format *format; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 522 | __u64 *vp; |
| 523 | |
| 524 | /* |
| 525 | * Support only for hardcoded and numnerial terms. |
| 526 | * Hardcoded terms should be already in, so nothing |
| 527 | * to be done for them. |
| 528 | */ |
| 529 | if (parse_events__is_hardcoded_term(term)) |
| 530 | return 0; |
| 531 | |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 532 | if (term->type_val != PARSE_EVENTS__TERM_TYPE_NUM) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 533 | return -EINVAL; |
| 534 | |
| 535 | format = pmu_find_format(formats, term->config); |
| 536 | if (!format) |
| 537 | return -EINVAL; |
| 538 | |
| 539 | switch (format->value) { |
| 540 | case PERF_PMU_FORMAT_VALUE_CONFIG: |
| 541 | vp = &attr->config; |
| 542 | break; |
| 543 | case PERF_PMU_FORMAT_VALUE_CONFIG1: |
| 544 | vp = &attr->config1; |
| 545 | break; |
| 546 | case PERF_PMU_FORMAT_VALUE_CONFIG2: |
| 547 | vp = &attr->config2; |
| 548 | break; |
| 549 | default: |
| 550 | return -EINVAL; |
| 551 | } |
| 552 | |
Jiri Olsa | 16fa7e8 | 2012-04-25 18:24:57 +0200 | [diff] [blame] | 553 | /* |
| 554 | * XXX If we ever decide to go with string values for |
| 555 | * non-hardcoded terms, here's the place to translate |
| 556 | * them into value. |
| 557 | */ |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 558 | pmu_format_value(format->bits, term->val.num, vp, zero); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
Jiri Olsa | cff7f95 | 2012-11-10 01:46:50 +0100 | [diff] [blame] | 562 | int perf_pmu__config_terms(struct list_head *formats, |
| 563 | struct perf_event_attr *attr, |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 564 | struct list_head *head_terms, |
| 565 | bool zero) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 566 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 567 | struct parse_events_term *term; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 568 | |
Jiri Olsa | 6b5fc39 | 2012-05-21 09:12:53 +0200 | [diff] [blame] | 569 | list_for_each_entry(term, head_terms, list) |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 570 | if (pmu_config_term(formats, attr, term, zero)) |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 571 | return -EINVAL; |
| 572 | |
| 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Configures event's 'attr' parameter based on the: |
| 578 | * 1) users input - specified in terms parameter |
| 579 | * 2) pmu format definitions - specified by pmu parameter |
| 580 | */ |
| 581 | int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, |
| 582 | struct list_head *head_terms) |
| 583 | { |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 584 | bool zero = !!pmu->default_config; |
| 585 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 586 | attr->type = pmu->type; |
Adrian Hunter | dc0a620 | 2014-07-31 09:00:49 +0300 | [diff] [blame^] | 587 | return perf_pmu__config_terms(&pmu->format, attr, head_terms, zero); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 588 | } |
| 589 | |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 590 | static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, |
| 591 | struct parse_events_term *term) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 592 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 593 | struct perf_pmu_alias *alias; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 594 | char *name; |
| 595 | |
| 596 | if (parse_events__is_hardcoded_term(term)) |
| 597 | return NULL; |
| 598 | |
| 599 | if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { |
| 600 | if (term->val.num != 1) |
| 601 | return NULL; |
| 602 | if (pmu_find_format(&pmu->format, term->config)) |
| 603 | return NULL; |
| 604 | name = term->config; |
| 605 | } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { |
| 606 | if (strcasecmp(term->config, "event")) |
| 607 | return NULL; |
| 608 | name = term->val.str; |
| 609 | } else { |
| 610 | return NULL; |
| 611 | } |
| 612 | |
| 613 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 614 | if (!strcasecmp(alias->name, name)) |
| 615 | return alias; |
| 616 | } |
| 617 | return NULL; |
| 618 | } |
| 619 | |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 620 | |
| 621 | static int check_unit_scale(struct perf_pmu_alias *alias, |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 622 | const char **unit, double *scale) |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 623 | { |
| 624 | /* |
| 625 | * Only one term in event definition can |
| 626 | * define unit and scale, fail if there's |
| 627 | * more than one. |
| 628 | */ |
| 629 | if ((*unit && alias->unit) || |
| 630 | (*scale && alias->scale)) |
| 631 | return -EINVAL; |
| 632 | |
| 633 | if (alias->unit) |
| 634 | *unit = alias->unit; |
| 635 | |
| 636 | if (alias->scale) |
| 637 | *scale = alias->scale; |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 642 | /* |
| 643 | * Find alias in the terms list and replace it with the terms |
| 644 | * defined for the alias |
| 645 | */ |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 646 | int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 647 | const char **unit, double *scale) |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 648 | { |
Arnaldo Carvalho de Melo | 6cee6cd | 2013-01-18 16:29:49 -0300 | [diff] [blame] | 649 | struct parse_events_term *term, *h; |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 650 | struct perf_pmu_alias *alias; |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 651 | int ret; |
| 652 | |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 653 | /* |
| 654 | * Mark unit and scale as not set |
| 655 | * (different from default values, see below) |
| 656 | */ |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 657 | *unit = NULL; |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 658 | *scale = 0.0; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 659 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 660 | list_for_each_entry_safe(term, h, head_terms, list) { |
| 661 | alias = pmu_find_alias(pmu, term); |
| 662 | if (!alias) |
| 663 | continue; |
| 664 | ret = pmu_alias_terms(alias, &term->list); |
| 665 | if (ret) |
| 666 | return ret; |
Stephane Eranian | 410136f | 2013-11-12 17:58:49 +0100 | [diff] [blame] | 667 | |
| 668 | ret = check_unit_scale(alias, unit, scale); |
| 669 | if (ret) |
| 670 | return ret; |
| 671 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 672 | list_del(&term->list); |
| 673 | free(term); |
| 674 | } |
Stephane Eranian | 8a39889 | 2014-01-17 16:34:05 +0100 | [diff] [blame] | 675 | |
| 676 | /* |
| 677 | * if no unit or scale foundin aliases, then |
| 678 | * set defaults as for evsel |
| 679 | * unit cannot left to NULL |
| 680 | */ |
| 681 | if (*unit == NULL) |
| 682 | *unit = ""; |
| 683 | |
| 684 | if (*scale == 0.0) |
| 685 | *scale = 1.0; |
| 686 | |
Zheng Yan | a6146d5 | 2012-06-15 14:31:41 +0800 | [diff] [blame] | 687 | return 0; |
| 688 | } |
| 689 | |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 690 | int perf_pmu__new_format(struct list_head *list, char *name, |
| 691 | int config, unsigned long *bits) |
| 692 | { |
Arnaldo Carvalho de Melo | 5c6ccc3 | 2013-01-18 16:54:00 -0300 | [diff] [blame] | 693 | struct perf_pmu_format *format; |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 694 | |
| 695 | format = zalloc(sizeof(*format)); |
| 696 | if (!format) |
| 697 | return -ENOMEM; |
| 698 | |
| 699 | format->name = strdup(name); |
| 700 | format->value = config; |
| 701 | memcpy(format->bits, bits, sizeof(format->bits)); |
| 702 | |
| 703 | list_add_tail(&format->list, list); |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | void perf_pmu__set_format(unsigned long *bits, long from, long to) |
| 708 | { |
| 709 | long b; |
| 710 | |
| 711 | if (!to) |
| 712 | to = from; |
| 713 | |
Sukadev Bhattiprolu | 1526813 | 2013-01-17 09:11:30 -0800 | [diff] [blame] | 714 | memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); |
Jiri Olsa | cd82a32 | 2012-03-15 20:09:17 +0100 | [diff] [blame] | 715 | for (b = from; b <= to; b++) |
| 716 | set_bit(b, bits); |
| 717 | } |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 718 | |
| 719 | static char *format_alias(char *buf, int len, struct perf_pmu *pmu, |
| 720 | struct perf_pmu_alias *alias) |
| 721 | { |
| 722 | snprintf(buf, len, "%s/%s/", pmu->name, alias->name); |
| 723 | return buf; |
| 724 | } |
| 725 | |
| 726 | static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, |
| 727 | struct perf_pmu_alias *alias) |
| 728 | { |
| 729 | snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); |
| 730 | return buf; |
| 731 | } |
| 732 | |
| 733 | static int cmp_string(const void *a, const void *b) |
| 734 | { |
| 735 | const char * const *as = a; |
| 736 | const char * const *bs = b; |
| 737 | return strcmp(*as, *bs); |
| 738 | } |
| 739 | |
| 740 | void print_pmu_events(const char *event_glob, bool name_only) |
| 741 | { |
| 742 | struct perf_pmu *pmu; |
| 743 | struct perf_pmu_alias *alias; |
| 744 | char buf[1024]; |
| 745 | int printed = 0; |
| 746 | int len, j; |
| 747 | char **aliases; |
| 748 | |
| 749 | pmu = NULL; |
| 750 | len = 0; |
| 751 | while ((pmu = perf_pmu__scan(pmu)) != NULL) |
| 752 | list_for_each_entry(alias, &pmu->aliases, list) |
| 753 | len++; |
| 754 | aliases = malloc(sizeof(char *) * len); |
| 755 | if (!aliases) |
| 756 | return; |
| 757 | pmu = NULL; |
| 758 | j = 0; |
| 759 | while ((pmu = perf_pmu__scan(pmu)) != NULL) |
| 760 | list_for_each_entry(alias, &pmu->aliases, list) { |
| 761 | char *name = format_alias(buf, sizeof(buf), pmu, alias); |
| 762 | bool is_cpu = !strcmp(pmu->name, "cpu"); |
| 763 | |
| 764 | if (event_glob != NULL && |
| 765 | !(strglobmatch(name, event_glob) || |
| 766 | (!is_cpu && strglobmatch(alias->name, |
| 767 | event_glob)))) |
| 768 | continue; |
| 769 | aliases[j] = name; |
| 770 | if (is_cpu && !name_only) |
| 771 | aliases[j] = format_alias_or(buf, sizeof(buf), |
| 772 | pmu, alias); |
| 773 | aliases[j] = strdup(aliases[j]); |
| 774 | j++; |
| 775 | } |
| 776 | len = j; |
| 777 | qsort(aliases, len, sizeof(char *), cmp_string); |
| 778 | for (j = 0; j < len; j++) { |
| 779 | if (name_only) { |
| 780 | printf("%s ", aliases[j]); |
| 781 | continue; |
| 782 | } |
| 783 | printf(" %-50s [Kernel PMU event]\n", aliases[j]); |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 784 | zfree(&aliases[j]); |
Andi Kleen | dc098b3 | 2013-04-20 11:02:29 -0700 | [diff] [blame] | 785 | printed++; |
| 786 | } |
| 787 | if (printed) |
| 788 | printf("\n"); |
| 789 | free(aliases); |
| 790 | } |
Andi Kleen | 4cabc3d | 2013-08-21 16:47:26 -0700 | [diff] [blame] | 791 | |
| 792 | bool pmu_have_event(const char *pname, const char *name) |
| 793 | { |
| 794 | struct perf_pmu *pmu; |
| 795 | struct perf_pmu_alias *alias; |
| 796 | |
| 797 | pmu = NULL; |
| 798 | while ((pmu = perf_pmu__scan(pmu)) != NULL) { |
| 799 | if (strcmp(pname, pmu->name)) |
| 800 | continue; |
| 801 | list_for_each_entry(alias, &pmu->aliases, list) |
| 802 | if (!strcmp(alias->name, name)) |
| 803 | return true; |
| 804 | } |
| 805 | return false; |
| 806 | } |