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