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