1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2cd82a32eSJiri Olsa #include <linux/list.h> 3c5de47f2SSukadev Bhattiprolu #include <linux/compiler.h> 432858480SArnaldo Carvalho de Melo #include <linux/string.h> 57f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 6c47a5599SJin Yao #include <linux/ctype.h> 7cd82a32eSJiri Olsa #include <sys/types.h> 8c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h> 97a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h> 10cd82a32eSJiri Olsa #include <unistd.h> 11cd82a32eSJiri Olsa #include <stdio.h> 12dc0a6202SAdrian Hunter #include <stdbool.h> 13cd82a32eSJiri Olsa #include <dirent.h> 14cd0cfad7SBorislav Petkov #include <api/fs/fs.h> 15410136f5SStephane Eranian #include <locale.h> 16c47a5599SJin Yao #include <fnmatch.h> 17acef233bSJing Zhang #include <math.h> 185e51b0bbSArnaldo Carvalho de Melo #include "debug.h" 19e12ee9f7SAdrian Hunter #include "evsel.h" 20cd82a32eSJiri Olsa #include "pmu.h" 21336b92daSRavi Bangoria #include "pmus.h" 22c7e97f21SNamhyung Kim #include <util/pmu-bison.h> 23c7e97f21SNamhyung Kim #include <util/pmu-flex.h> 24cd82a32eSJiri Olsa #include "parse-events.h" 25e5c6109fSIan Rogers #include "print-events.h" 26933f82ffSSukadev Bhattiprolu #include "header.h" 27a067558eSArnaldo Carvalho de Melo #include "string2.h" 28fa0d9846SArnaldo Carvalho de Melo #include "strbuf.h" 29d9664582SAndi Kleen #include "fncache.h" 306593f019SJames Clark #include "util/evsel_config.h" 31cd82a32eSJiri Olsa 32e46fc8d9SArnaldo Carvalho de Melo struct perf_pmu perf_pmu__fake; 33e46fc8d9SArnaldo Carvalho de Melo 34c3245d20SIan Rogers #define UNIT_MAX_LEN 31 /* max length for event unit name */ 35c3245d20SIan Rogers 36c3245d20SIan Rogers /** 37c3245d20SIan Rogers * struct perf_pmu_alias - An event either read from sysfs or builtin in 38c3245d20SIan Rogers * pmu-events.c, created by parsing the pmu-events json files. 39c3245d20SIan Rogers */ 40c3245d20SIan Rogers struct perf_pmu_alias { 41c3245d20SIan Rogers /** @name: Name of the event like "mem-loads". */ 42c3245d20SIan Rogers char *name; 43c3245d20SIan Rogers /** @desc: Optional short description of the event. */ 44c3245d20SIan Rogers char *desc; 45c3245d20SIan Rogers /** @long_desc: Optional long description. */ 46c3245d20SIan Rogers char *long_desc; 47c3245d20SIan Rogers /** 48c3245d20SIan Rogers * @topic: Optional topic such as cache or pipeline, particularly for 49c3245d20SIan Rogers * json events. 50c3245d20SIan Rogers */ 51c3245d20SIan Rogers char *topic; 52c3245d20SIan Rogers /** 53c3245d20SIan Rogers * @str: Comma separated parameter list like 54c3245d20SIan Rogers * "event=0xcd,umask=0x1,ldlat=0x3". 55c3245d20SIan Rogers */ 56c3245d20SIan Rogers char *str; 57c3245d20SIan Rogers /** @terms: Owned list of the original parsed parameters. */ 58c3245d20SIan Rogers struct list_head terms; 59c3245d20SIan Rogers /** @list: List element of struct perf_pmu aliases. */ 60c3245d20SIan Rogers struct list_head list; 61*7b723dbbSIan Rogers /** 62*7b723dbbSIan Rogers * @pmu_name: The name copied from the json struct pmu_event. This can 63*7b723dbbSIan Rogers * differ from the PMU name as it won't have suffixes. 64*7b723dbbSIan Rogers */ 65*7b723dbbSIan Rogers char *pmu_name; 66c3245d20SIan Rogers /** @unit: Units for the event, such as bytes or cache lines. */ 67c3245d20SIan Rogers char unit[UNIT_MAX_LEN+1]; 68c3245d20SIan Rogers /** @scale: Value to scale read counter values by. */ 69c3245d20SIan Rogers double scale; 70c3245d20SIan Rogers /** 71c3245d20SIan Rogers * @per_pkg: Does the file 72c3245d20SIan Rogers * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or 73c3245d20SIan Rogers * equivalent json value exist and have the value 1. 74c3245d20SIan Rogers */ 75c3245d20SIan Rogers bool per_pkg; 76c3245d20SIan Rogers /** 77c3245d20SIan Rogers * @snapshot: Does the file 78c3245d20SIan Rogers * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot 79c3245d20SIan Rogers * exist and have the value 1. 80c3245d20SIan Rogers */ 81c3245d20SIan Rogers bool snapshot; 82c3245d20SIan Rogers /** 83c3245d20SIan Rogers * @deprecated: Is the event hidden and so not shown in perf list by 84c3245d20SIan Rogers * default. 85c3245d20SIan Rogers */ 86c3245d20SIan Rogers bool deprecated; 87*7b723dbbSIan Rogers /** @from_sysfs: Was the alias from sysfs or a json event? */ 88*7b723dbbSIan Rogers bool from_sysfs; 89*7b723dbbSIan Rogers /** @info_loaded: Have the scale, unit and other values been read from disk? */ 90*7b723dbbSIan Rogers bool info_loaded; 91c3245d20SIan Rogers }; 92c3245d20SIan Rogers 93fe13d43dSIan Rogers /** 94fe13d43dSIan Rogers * struct perf_pmu_format - Values from a format file read from 95fe13d43dSIan Rogers * <sysfs>/devices/cpu/format/ held in struct perf_pmu. 96fe13d43dSIan Rogers * 97fe13d43dSIan Rogers * For example, the contents of <sysfs>/devices/cpu/format/event may be 98fe13d43dSIan Rogers * "config:0-7" and will be represented here as name="event", 99fe13d43dSIan Rogers * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. 100fe13d43dSIan Rogers */ 101ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 10250402641SIan Rogers /** @list: Element on list within struct perf_pmu. */ 10350402641SIan Rogers struct list_head list; 10450402641SIan Rogers /** @bits: Which config bits are set by this format value. */ 10550402641SIan Rogers DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 106fe13d43dSIan Rogers /** @name: The modifier/file name. */ 107ab1bf653SArnaldo Carvalho de Melo char *name; 108fe13d43dSIan Rogers /** 109fe13d43dSIan Rogers * @value : Which config value the format relates to. Supported values 110fe13d43dSIan Rogers * are from PERF_PMU_FORMAT_VALUE_CONFIG to 111fe13d43dSIan Rogers * PERF_PMU_FORMAT_VALUE_CONFIG_END. 112fe13d43dSIan Rogers */ 11350402641SIan Rogers u16 value; 11450402641SIan Rogers /** @loaded: Has the contents been loaded/parsed. */ 11550402641SIan Rogers bool loaded; 116ab1bf653SArnaldo Carvalho de Melo }; 117ab1bf653SArnaldo Carvalho de Melo 11850402641SIan Rogers static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name) 11950402641SIan Rogers { 12050402641SIan Rogers struct perf_pmu_format *format; 12150402641SIan Rogers 12250402641SIan Rogers format = zalloc(sizeof(*format)); 12350402641SIan Rogers if (!format) 12450402641SIan Rogers return NULL; 12550402641SIan Rogers 12650402641SIan Rogers format->name = strdup(name); 12750402641SIan Rogers if (!format->name) { 12850402641SIan Rogers free(format); 12950402641SIan Rogers return NULL; 13050402641SIan Rogers } 13150402641SIan Rogers list_add_tail(&format->list, list); 13250402641SIan Rogers return format; 13350402641SIan Rogers } 13450402641SIan Rogers 13550402641SIan Rogers /* Called at the end of parsing a format. */ 13650402641SIan Rogers void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits) 13750402641SIan Rogers { 13850402641SIan Rogers struct perf_pmu_format *format = vformat; 13950402641SIan Rogers 14050402641SIan Rogers format->value = config; 14150402641SIan Rogers memcpy(format->bits, bits, sizeof(format->bits)); 14250402641SIan Rogers } 14350402641SIan Rogers 14450402641SIan Rogers static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file) 14550402641SIan Rogers { 14650402641SIan Rogers void *scanner; 14750402641SIan Rogers int ret; 14850402641SIan Rogers 14950402641SIan Rogers ret = perf_pmu_lex_init(&scanner); 15050402641SIan Rogers if (ret) 15150402641SIan Rogers return; 15250402641SIan Rogers 15350402641SIan Rogers perf_pmu_set_in(file, scanner); 15450402641SIan Rogers ret = perf_pmu_parse(format, scanner); 15550402641SIan Rogers perf_pmu_lex_destroy(scanner); 15650402641SIan Rogers format->loaded = true; 15750402641SIan Rogers } 15850402641SIan Rogers 15950402641SIan Rogers static void perf_pmu_format__load(struct perf_pmu *pmu, struct perf_pmu_format *format) 16050402641SIan Rogers { 16150402641SIan Rogers char path[PATH_MAX]; 16250402641SIan Rogers FILE *file = NULL; 16350402641SIan Rogers 16450402641SIan Rogers if (format->loaded) 16550402641SIan Rogers return; 16650402641SIan Rogers 16750402641SIan Rogers if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format")) 16850402641SIan Rogers return; 16950402641SIan Rogers 17050402641SIan Rogers assert(strlen(path) + strlen(format->name) + 2 < sizeof(path)); 17150402641SIan Rogers strcat(path, "/"); 17250402641SIan Rogers strcat(path, format->name); 17350402641SIan Rogers 17450402641SIan Rogers file = fopen(path, "r"); 17550402641SIan Rogers if (!file) 17650402641SIan Rogers return; 17750402641SIan Rogers __perf_pmu_format__load(format, file); 17850402641SIan Rogers fclose(file); 17950402641SIan Rogers } 18050402641SIan Rogers 181cd82a32eSJiri Olsa /* 182cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 183cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 184cd82a32eSJiri Olsa */ 18550402641SIan Rogers int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load) 186cd82a32eSJiri Olsa { 187cd82a32eSJiri Olsa struct dirent *evt_ent; 188cd82a32eSJiri Olsa DIR *format_dir; 189cd82a32eSJiri Olsa int ret = 0; 190cd82a32eSJiri Olsa 191e293a5e8SNamhyung Kim format_dir = fdopendir(dirfd); 192cd82a32eSJiri Olsa if (!format_dir) 193cd82a32eSJiri Olsa return -EINVAL; 194cd82a32eSJiri Olsa 19550402641SIan Rogers while ((evt_ent = readdir(format_dir)) != NULL) { 19650402641SIan Rogers struct perf_pmu_format *format; 197cd82a32eSJiri Olsa char *name = evt_ent->d_name; 198cd82a32eSJiri Olsa 199cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 200cd82a32eSJiri Olsa continue; 201cd82a32eSJiri Olsa 20250402641SIan Rogers format = perf_pmu__new_format(&pmu->format, name); 20350402641SIan Rogers if (!format) { 20450402641SIan Rogers ret = -ENOMEM; 205cd82a32eSJiri Olsa break; 20650402641SIan Rogers } 207cd82a32eSJiri Olsa 20850402641SIan Rogers if (eager_load) { 20950402641SIan Rogers FILE *file; 21050402641SIan Rogers int fd = openat(dirfd, name, O_RDONLY); 21150402641SIan Rogers 21250402641SIan Rogers if (fd < 0) { 21350402641SIan Rogers ret = -errno; 21450402641SIan Rogers break; 21550402641SIan Rogers } 2163d88aec0SIan Rogers file = fdopen(fd, "r"); 2173d88aec0SIan Rogers if (!file) { 2183d88aec0SIan Rogers close(fd); 2193d88aec0SIan Rogers break; 2203d88aec0SIan Rogers } 22150402641SIan Rogers __perf_pmu_format__load(format, file); 2223d88aec0SIan Rogers fclose(file); 2233d88aec0SIan Rogers } 224cd82a32eSJiri Olsa } 225cd82a32eSJiri Olsa 226cd82a32eSJiri Olsa closedir(format_dir); 227cd82a32eSJiri Olsa return ret; 228cd82a32eSJiri Olsa } 229cd82a32eSJiri Olsa 230cd82a32eSJiri Olsa /* 231cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 232cd82a32eSJiri Olsa * located at: 233cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 234cd82a32eSJiri Olsa */ 235838a8c5fSIan Rogers static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name) 236cd82a32eSJiri Olsa { 237e293a5e8SNamhyung Kim int fd; 238cd82a32eSJiri Olsa 239e293a5e8SNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY); 240e293a5e8SNamhyung Kim if (fd < 0) 241d9664582SAndi Kleen return 0; 242cd82a32eSJiri Olsa 243e293a5e8SNamhyung Kim /* it'll close the fd */ 24450402641SIan Rogers if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false)) 245cd82a32eSJiri Olsa return -1; 246cd82a32eSJiri Olsa 247cd82a32eSJiri Olsa return 0; 248cd82a32eSJiri Olsa } 249cd82a32eSJiri Olsa 250a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval) 251d02fc6bcSAndi Kleen { 252d02fc6bcSAndi Kleen char *lc; 253d02fc6bcSAndi Kleen int ret = 0; 254d02fc6bcSAndi Kleen 255d02fc6bcSAndi Kleen /* 256d02fc6bcSAndi Kleen * save current locale 257d02fc6bcSAndi Kleen */ 258d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 259d02fc6bcSAndi Kleen 260d02fc6bcSAndi Kleen /* 261d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 262d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 263d02fc6bcSAndi Kleen * call below. 264d02fc6bcSAndi Kleen */ 265d02fc6bcSAndi Kleen lc = strdup(lc); 266d02fc6bcSAndi Kleen if (!lc) { 267d02fc6bcSAndi Kleen ret = -ENOMEM; 268d02fc6bcSAndi Kleen goto out; 269d02fc6bcSAndi Kleen } 270d02fc6bcSAndi Kleen 271d02fc6bcSAndi Kleen /* 272d02fc6bcSAndi Kleen * force to C locale to ensure kernel 273d02fc6bcSAndi Kleen * scale string is converted correctly. 274d02fc6bcSAndi Kleen * kernel uses default C locale. 275d02fc6bcSAndi Kleen */ 276d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 277d02fc6bcSAndi Kleen 278d02fc6bcSAndi Kleen *sval = strtod(scale, end); 279d02fc6bcSAndi Kleen 280d02fc6bcSAndi Kleen out: 281d02fc6bcSAndi Kleen /* restore locale */ 282d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 283d02fc6bcSAndi Kleen free(lc); 284d02fc6bcSAndi Kleen return ret; 285d02fc6bcSAndi Kleen } 286d02fc6bcSAndi Kleen 287*7b723dbbSIan Rogers static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 288410136f5SStephane Eranian { 289410136f5SStephane Eranian struct stat st; 290410136f5SStephane Eranian ssize_t sret; 291*7b723dbbSIan Rogers size_t len; 292410136f5SStephane Eranian char scale[128]; 293410136f5SStephane Eranian int fd, ret = -1; 294410136f5SStephane Eranian char path[PATH_MAX]; 295410136f5SStephane Eranian 296*7b723dbbSIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 297*7b723dbbSIan Rogers if (!len) 298*7b723dbbSIan Rogers return 0; 299*7b723dbbSIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/%s.scale", pmu->name, alias->name); 300410136f5SStephane Eranian 301*7b723dbbSIan Rogers fd = open(path, O_RDONLY); 302410136f5SStephane Eranian if (fd == -1) 303410136f5SStephane Eranian return -1; 304410136f5SStephane Eranian 305410136f5SStephane Eranian if (fstat(fd, &st) < 0) 306410136f5SStephane Eranian goto error; 307410136f5SStephane Eranian 308410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 309410136f5SStephane Eranian if (sret < 0) 310410136f5SStephane Eranian goto error; 311410136f5SStephane Eranian 3129ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 3139ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 3149ecae065SMadhavan Srinivasan else 315410136f5SStephane Eranian scale[sret] = '\0'; 3169ecae065SMadhavan Srinivasan 317a55ab7c4SJin Yao ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); 318410136f5SStephane Eranian error: 319410136f5SStephane Eranian close(fd); 320410136f5SStephane Eranian return ret; 321410136f5SStephane Eranian } 322410136f5SStephane Eranian 323*7b723dbbSIan Rogers static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 324410136f5SStephane Eranian { 325410136f5SStephane Eranian char path[PATH_MAX]; 326*7b723dbbSIan Rogers size_t len; 327410136f5SStephane Eranian ssize_t sret; 328410136f5SStephane Eranian int fd; 329410136f5SStephane Eranian 330410136f5SStephane Eranian 331*7b723dbbSIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 332*7b723dbbSIan Rogers if (!len) 333*7b723dbbSIan Rogers return 0; 334*7b723dbbSIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/%s.unit", pmu->name, alias->name); 335*7b723dbbSIan Rogers 336*7b723dbbSIan Rogers fd = open(path, O_RDONLY); 337410136f5SStephane Eranian if (fd == -1) 338410136f5SStephane Eranian return -1; 339410136f5SStephane Eranian 340410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 341410136f5SStephane Eranian if (sret < 0) 342410136f5SStephane Eranian goto error; 343410136f5SStephane Eranian 344410136f5SStephane Eranian close(fd); 345410136f5SStephane Eranian 3469ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 3479ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 3489ecae065SMadhavan Srinivasan else 349410136f5SStephane Eranian alias->unit[sret] = '\0'; 350410136f5SStephane Eranian 351410136f5SStephane Eranian return 0; 352410136f5SStephane Eranian error: 353410136f5SStephane Eranian close(fd); 354410136f5SStephane Eranian alias->unit[0] = '\0'; 355410136f5SStephane Eranian return -1; 356410136f5SStephane Eranian } 357410136f5SStephane Eranian 358044330c1SMatt Fleming static int 359*7b723dbbSIan Rogers perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 360044330c1SMatt Fleming { 361044330c1SMatt Fleming char path[PATH_MAX]; 362*7b723dbbSIan Rogers size_t len; 363044330c1SMatt Fleming int fd; 364044330c1SMatt Fleming 365*7b723dbbSIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 366*7b723dbbSIan Rogers if (!len) 367*7b723dbbSIan Rogers return 0; 368*7b723dbbSIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/%s.per-pkg", pmu->name, alias->name); 369044330c1SMatt Fleming 370*7b723dbbSIan Rogers fd = open(path, O_RDONLY); 371044330c1SMatt Fleming if (fd == -1) 372044330c1SMatt Fleming return -1; 373044330c1SMatt Fleming 374044330c1SMatt Fleming close(fd); 375044330c1SMatt Fleming 376044330c1SMatt Fleming alias->per_pkg = true; 377044330c1SMatt Fleming return 0; 378044330c1SMatt Fleming } 379044330c1SMatt Fleming 380*7b723dbbSIan Rogers static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 3811d9e446bSJiri Olsa { 3821d9e446bSJiri Olsa char path[PATH_MAX]; 383*7b723dbbSIan Rogers size_t len; 3841d9e446bSJiri Olsa int fd; 3851d9e446bSJiri Olsa 386*7b723dbbSIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 387*7b723dbbSIan Rogers if (!len) 388*7b723dbbSIan Rogers return 0; 389*7b723dbbSIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/%s.snapshot", pmu->name, alias->name); 3901d9e446bSJiri Olsa 391*7b723dbbSIan Rogers fd = open(path, O_RDONLY); 3921d9e446bSJiri Olsa if (fd == -1) 3931d9e446bSJiri Olsa return -1; 3941d9e446bSJiri Olsa 3951d9e446bSJiri Olsa alias->snapshot = true; 3961d9e446bSJiri Olsa close(fd); 3971d9e446bSJiri Olsa return 0; 3981d9e446bSJiri Olsa } 3991d9e446bSJiri Olsa 4006dde6429SThomas Richter /* Delete an alias entry. */ 401c3245d20SIan Rogers static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 4026dde6429SThomas Richter { 4036dde6429SThomas Richter zfree(&newalias->name); 4046dde6429SThomas Richter zfree(&newalias->desc); 4056dde6429SThomas Richter zfree(&newalias->long_desc); 4066dde6429SThomas Richter zfree(&newalias->topic); 4076dde6429SThomas Richter zfree(&newalias->str); 40832705de7SJin Yao zfree(&newalias->pmu_name); 4096dde6429SThomas Richter parse_events_terms__purge(&newalias->terms); 4106dde6429SThomas Richter free(newalias); 4116dde6429SThomas Richter } 4126dde6429SThomas Richter 413eec11310SNamhyung Kim static void perf_pmu__del_aliases(struct perf_pmu *pmu) 414eec11310SNamhyung Kim { 415eec11310SNamhyung Kim struct perf_pmu_alias *alias, *tmp; 416eec11310SNamhyung Kim 417eec11310SNamhyung Kim list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) { 418eec11310SNamhyung Kim list_del(&alias->list); 419eec11310SNamhyung Kim perf_pmu_free_alias(alias); 420eec11310SNamhyung Kim } 421eec11310SNamhyung Kim } 422eec11310SNamhyung Kim 423f63a536fSIan Rogers static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *pmu, const char *name) 4246dde6429SThomas Richter { 425f63a536fSIan Rogers struct perf_pmu_alias *alias; 4266dde6429SThomas Richter 427f63a536fSIan Rogers list_for_each_entry(alias, &pmu->aliases, list) { 428f63a536fSIan Rogers if (!strcasecmp(alias->name, name)) 429f63a536fSIan Rogers return alias; 43032705de7SJin Yao } 431f63a536fSIan Rogers return NULL; 432f63a536fSIan Rogers } 433f63a536fSIan Rogers 434f63a536fSIan Rogers static bool assign_str(const char *name, const char *field, char **old_str, 435f63a536fSIan Rogers const char *new_str) 436f63a536fSIan Rogers { 437f63a536fSIan Rogers if (!*old_str && new_str) { 438f63a536fSIan Rogers *old_str = strdup(new_str); 4396dde6429SThomas Richter return true; 4406dde6429SThomas Richter } 441f63a536fSIan Rogers 442f63a536fSIan Rogers if (!new_str || !strcasecmp(*old_str, new_str)) 443f63a536fSIan Rogers return false; /* Nothing to update. */ 444f63a536fSIan Rogers 445f63a536fSIan Rogers pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n", 446f63a536fSIan Rogers name, field, *old_str, new_str); 447f63a536fSIan Rogers zfree(old_str); 448f63a536fSIan Rogers *old_str = strdup(new_str); 449f63a536fSIan Rogers return true; 4506dde6429SThomas Richter } 451f63a536fSIan Rogers 452*7b723dbbSIan Rogers static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 453*7b723dbbSIan Rogers { 454*7b723dbbSIan Rogers if (!alias->from_sysfs || alias->info_loaded) 455*7b723dbbSIan Rogers return; 456*7b723dbbSIan Rogers 457*7b723dbbSIan Rogers /* 458*7b723dbbSIan Rogers * load unit name and scale if available 459*7b723dbbSIan Rogers */ 460*7b723dbbSIan Rogers perf_pmu__parse_unit(pmu, alias); 461*7b723dbbSIan Rogers perf_pmu__parse_scale(pmu, alias); 462*7b723dbbSIan Rogers perf_pmu__parse_per_pkg(pmu, alias); 463*7b723dbbSIan Rogers perf_pmu__parse_snapshot(pmu, alias); 464*7b723dbbSIan Rogers } 465*7b723dbbSIan Rogers 466*7b723dbbSIan Rogers struct update_alias_data { 467*7b723dbbSIan Rogers struct perf_pmu *pmu; 468*7b723dbbSIan Rogers struct perf_pmu_alias *alias; 469*7b723dbbSIan Rogers }; 470*7b723dbbSIan Rogers 471f63a536fSIan Rogers static int update_alias(const struct pmu_event *pe, 472f63a536fSIan Rogers const struct pmu_events_table *table __maybe_unused, 473f63a536fSIan Rogers void *vdata) 474f63a536fSIan Rogers { 475*7b723dbbSIan Rogers struct update_alias_data *data = vdata; 476f63a536fSIan Rogers int ret = 0; 477f63a536fSIan Rogers 478*7b723dbbSIan Rogers read_alias_info(data->pmu, data->alias); 479*7b723dbbSIan Rogers assign_str(pe->name, "desc", &data->alias->desc, pe->desc); 480*7b723dbbSIan Rogers assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc); 481*7b723dbbSIan Rogers assign_str(pe->name, "topic", &data->alias->topic, pe->topic); 482*7b723dbbSIan Rogers data->alias->per_pkg = pe->perpkg; 483*7b723dbbSIan Rogers if (assign_str(pe->name, "value", &data->alias->str, pe->event)) { 484*7b723dbbSIan Rogers parse_events_terms__purge(&data->alias->terms); 485*7b723dbbSIan Rogers ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL); 486f63a536fSIan Rogers } 487f63a536fSIan Rogers if (!ret && pe->unit) { 488f63a536fSIan Rogers char *unit; 489f63a536fSIan Rogers 490*7b723dbbSIan Rogers ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale); 491f63a536fSIan Rogers if (!ret) 492*7b723dbbSIan Rogers snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit); 493f63a536fSIan Rogers } 494f63a536fSIan Rogers return ret; 4956dde6429SThomas Richter } 4966dde6429SThomas Richter 497*7b723dbbSIan Rogers static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name, 498edb217ffSIan Rogers const char *desc, const char *val, FILE *val_fd, 499edb217ffSIan Rogers const struct pmu_event *pe) 500a6146d50SZheng Yan { 5010c24d6fbSThomas Richter struct parse_events_term *term; 5025c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 503a6146d50SZheng Yan int ret; 5040c24d6fbSThomas Richter char newval[256]; 505330f40a0SIan Rogers const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL; 506bd680861SIan Rogers bool deprecated = false, perpkg = false; 507eab35953SJin Yao 508f63a536fSIan Rogers if (perf_pmu__find_alias(pmu, name)) { 509f63a536fSIan Rogers /* Alias was already created/loaded. */ 510f63a536fSIan Rogers return 0; 511f63a536fSIan Rogers } 512f63a536fSIan Rogers 513eab35953SJin Yao if (pe) { 514330f40a0SIan Rogers long_desc = pe->long_desc; 515330f40a0SIan Rogers topic = pe->topic; 516330f40a0SIan Rogers unit = pe->unit; 517bd680861SIan Rogers perpkg = pe->perpkg; 5189ed8b7dcSIan Rogers deprecated = pe->deprecated; 519330f40a0SIan Rogers pmu_name = pe->pmu; 520eab35953SJin Yao } 521a6146d50SZheng Yan 522a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 523a6146d50SZheng Yan if (!alias) 524a6146d50SZheng Yan return -ENOMEM; 525a6146d50SZheng Yan 526a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 527410136f5SStephane Eranian alias->scale = 1.0; 528410136f5SStephane Eranian alias->unit[0] = '\0'; 529bd680861SIan Rogers alias->per_pkg = perpkg; 53084530920SStephane Eranian alias->snapshot = false; 5319ed8b7dcSIan Rogers alias->deprecated = deprecated; 532410136f5SStephane Eranian 533edb217ffSIan Rogers ret = parse_events_terms(&alias->terms, val, val_fd); 534a6146d50SZheng Yan if (ret) { 53570c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 536a6146d50SZheng Yan free(alias); 537a6146d50SZheng Yan return ret; 538a6146d50SZheng Yan } 539a6146d50SZheng Yan 540a6146d50SZheng Yan alias->name = strdup(name); 54108e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 542c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 543c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 544dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 545f63a536fSIan Rogers alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 546fedb2b51SAndi Kleen if (unit) { 547f63a536fSIan Rogers if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) { 548f63a536fSIan Rogers perf_pmu_free_alias(alias); 549fedb2b51SAndi Kleen return -1; 550f63a536fSIan Rogers } 551fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 552fedb2b51SAndi Kleen } 553f63a536fSIan Rogers if (!pe) { 554f63a536fSIan Rogers /* Update an event from sysfs with json data. */ 555*7b723dbbSIan Rogers struct update_alias_data data = { 556*7b723dbbSIan Rogers .pmu = pmu, 557*7b723dbbSIan Rogers .alias = alias, 558*7b723dbbSIan Rogers }; 559*7b723dbbSIan Rogers 560*7b723dbbSIan Rogers alias->from_sysfs = true; 5617c52f10cSIan Rogers if (pmu->events_table) { 562e6ff1eedSIan Rogers if (pmu_events_table__find_event(pmu->events_table, pmu, name, 563*7b723dbbSIan Rogers update_alias, &data) == 0) 564e6ff1eedSIan Rogers pmu->loaded_json_aliases++; 5657c52f10cSIan Rogers } 566f63a536fSIan Rogers } 567f63a536fSIan Rogers 568f63a536fSIan Rogers /* Scan event and remove leading zeroes, spaces, newlines, some 569f63a536fSIan Rogers * platforms have terms specified as 570f63a536fSIan Rogers * event=0x0091 (read from files ../<PMU>/events/<FILE> 571f63a536fSIan Rogers * and terms specified as event=0x91 (read from JSON files). 572f63a536fSIan Rogers * 573f63a536fSIan Rogers * Rebuild string to make alias->str member comparable. 574f63a536fSIan Rogers */ 575f63a536fSIan Rogers ret = 0; 576f63a536fSIan Rogers list_for_each_entry(term, &alias->terms, list) { 577f63a536fSIan Rogers if (ret) 578f63a536fSIan Rogers ret += scnprintf(newval + ret, sizeof(newval) - ret, 579f63a536fSIan Rogers ","); 580f63a536fSIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 581f63a536fSIan Rogers ret += scnprintf(newval + ret, sizeof(newval) - ret, 582f63a536fSIan Rogers "%s=%#x", term->config, term->val.num); 583f63a536fSIan Rogers else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 584f63a536fSIan Rogers ret += scnprintf(newval + ret, sizeof(newval) - ret, 585f63a536fSIan Rogers "%s=%s", term->config, term->val.str); 586f63a536fSIan Rogers } 5870c24d6fbSThomas Richter alias->str = strdup(newval); 588e6ff1eedSIan Rogers if (!pe) 589e6ff1eedSIan Rogers pmu->sysfs_aliases++; 590e6ff1eedSIan Rogers else 591e6ff1eedSIan Rogers pmu->loaded_json_aliases++; 592f26d22f1SIan Rogers list_add_tail(&alias->list, &pmu->aliases); 593a6146d50SZheng Yan return 0; 594a6146d50SZheng Yan } 595a6146d50SZheng Yan 59646441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 59746441bdcSMatt Fleming { 59846441bdcSMatt Fleming size_t len; 59946441bdcSMatt Fleming 60046441bdcSMatt Fleming len = strlen(name); 60146441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 60246441bdcSMatt Fleming return true; 60346441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 60446441bdcSMatt Fleming return true; 605044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 606044330c1SMatt Fleming return true; 6071d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 6081d9e446bSJiri Olsa return true; 60946441bdcSMatt Fleming 61046441bdcSMatt Fleming return false; 61146441bdcSMatt Fleming } 61246441bdcSMatt Fleming 613a6146d50SZheng Yan /* 614a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 615a6146d50SZheng Yan * specified in 'dir' parameter. 616a6146d50SZheng Yan */ 617f26d22f1SIan Rogers static int pmu_aliases_parse(struct perf_pmu *pmu, int dirfd) 618a6146d50SZheng Yan { 619a6146d50SZheng Yan struct dirent *evt_ent; 620a6146d50SZheng Yan DIR *event_dir; 621e293a5e8SNamhyung Kim int fd; 622a6146d50SZheng Yan 623e293a5e8SNamhyung Kim event_dir = fdopendir(dirfd); 624a6146d50SZheng Yan if (!event_dir) 625a6146d50SZheng Yan return -EINVAL; 626a6146d50SZheng Yan 627940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 628a6146d50SZheng Yan char *name = evt_ent->d_name; 629a6146d50SZheng Yan FILE *file; 630a6146d50SZheng Yan 631a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 632a6146d50SZheng Yan continue; 633a6146d50SZheng Yan 634410136f5SStephane Eranian /* 63546441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 636410136f5SStephane Eranian */ 63746441bdcSMatt Fleming if (pmu_alias_info_file(name)) 638410136f5SStephane Eranian continue; 639410136f5SStephane Eranian 640e293a5e8SNamhyung Kim fd = openat(dirfd, name, O_RDONLY); 6410ea8920eSIan Rogers if (fd == -1) { 6420ea8920eSIan Rogers pr_debug("Cannot open %s\n", name); 6430ea8920eSIan Rogers continue; 6440ea8920eSIan Rogers } 645e293a5e8SNamhyung Kim file = fdopen(fd, "r"); 646940db6dcSAndi Kleen if (!file) { 6470ea8920eSIan Rogers close(fd); 648940db6dcSAndi Kleen continue; 649940db6dcSAndi Kleen } 650410136f5SStephane Eranian 651*7b723dbbSIan Rogers if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL, 652edb217ffSIan Rogers /*val=*/ NULL, file, /*pe=*/ NULL) < 0) 653940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 654a6146d50SZheng Yan fclose(file); 655a6146d50SZheng Yan } 656a6146d50SZheng Yan 657a6146d50SZheng Yan closedir(event_dir); 658940db6dcSAndi Kleen return 0; 659a6146d50SZheng Yan } 660a6146d50SZheng Yan 661a6146d50SZheng Yan /* 662a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 663a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 664a6146d50SZheng Yan */ 665838a8c5fSIan Rogers static int pmu_aliases(struct perf_pmu *pmu, int dirfd, const char *name) 666a6146d50SZheng Yan { 667e293a5e8SNamhyung Kim int fd; 668a6146d50SZheng Yan 669e293a5e8SNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY); 670e293a5e8SNamhyung Kim if (fd < 0) 671d9664582SAndi Kleen return 0; 672a6146d50SZheng Yan 673e293a5e8SNamhyung Kim /* it'll close the fd */ 674f26d22f1SIan Rogers if (pmu_aliases_parse(pmu, fd)) 675a6146d50SZheng Yan return -1; 676a6146d50SZheng Yan 677a6146d50SZheng Yan return 0; 678a6146d50SZheng Yan } 679a6146d50SZheng Yan 6805c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 681a6146d50SZheng Yan struct list_head *terms) 682a6146d50SZheng Yan { 6837c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 684a6146d50SZheng Yan LIST_HEAD(list); 685a6146d50SZheng Yan int ret; 686a6146d50SZheng Yan 687a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 6887c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 689a6146d50SZheng Yan if (ret) { 690682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 691a6146d50SZheng Yan return ret; 692a6146d50SZheng Yan } 693c2f1ceadSAndi Kleen /* 694c2f1ceadSAndi Kleen * Weak terms don't override command line options, 695c2f1ceadSAndi Kleen * which we don't want for implicit terms in aliases. 696c2f1ceadSAndi Kleen */ 697c2f1ceadSAndi Kleen cloned->weak = true; 6987c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 699a6146d50SZheng Yan } 700a6146d50SZheng Yan list_splice(&list, terms); 701a6146d50SZheng Yan return 0; 702a6146d50SZheng Yan } 703a6146d50SZheng Yan 70466ec1191SMark Rutland /* 70566ec1191SMark Rutland * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 70666ec1191SMark Rutland * may have a "cpus" file. 70766ec1191SMark Rutland */ 708d06593aaSIan Rogers static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core) 7097ae92e74SYan, Zheng { 710f854839bSJiri Olsa struct perf_cpu_map *cpus; 7117e3fcffeSMark Rutland const char *templates[] = { 712d50a79cdSJames Clark "cpumask", 713d50a79cdSJames Clark "cpus", 7147e3fcffeSMark Rutland NULL 7157e3fcffeSMark Rutland }; 7167e3fcffeSMark Rutland const char **template; 717d50a79cdSJames Clark char pmu_name[PATH_MAX]; 718d50a79cdSJames Clark struct perf_pmu pmu = {.name = pmu_name}; 719d50a79cdSJames Clark FILE *file; 7207ae92e74SYan, Zheng 721d50a79cdSJames Clark strlcpy(pmu_name, name, sizeof(pmu_name)); 7227e3fcffeSMark Rutland for (template = templates; *template; template++) { 7233a69672eSNamhyung Kim file = perf_pmu__open_file_at(&pmu, dirfd, *template); 724d50a79cdSJames Clark if (!file) 725d50a79cdSJames Clark continue; 726d50a79cdSJames Clark cpus = perf_cpu_map__read(file); 7273a69672eSNamhyung Kim fclose(file); 72866ec1191SMark Rutland if (cpus) 72966ec1191SMark Rutland return cpus; 7307e3fcffeSMark Rutland } 7317ae92e74SYan, Zheng 732d06593aaSIan Rogers /* Nothing found, for core PMUs assume this means all CPUs. */ 733d06593aaSIan Rogers return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL; 73466ec1191SMark Rutland } 7357ae92e74SYan, Zheng 7363a69672eSNamhyung Kim static bool pmu_is_uncore(int dirfd, const char *name) 73766ec1191SMark Rutland { 7383a69672eSNamhyung Kim int fd; 7397ae92e74SYan, Zheng 7403a69672eSNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH); 7413a69672eSNamhyung Kim if (fd < 0) 7423a69672eSNamhyung Kim return false; 7433a69672eSNamhyung Kim 7443a69672eSNamhyung Kim close(fd); 7453a69672eSNamhyung Kim return true; 7467ae92e74SYan, Zheng } 7477ae92e74SYan, Zheng 74851d54847SJohn Garry static char *pmu_id(const char *name) 74951d54847SJohn Garry { 75051d54847SJohn Garry char path[PATH_MAX], *str; 75151d54847SJohn Garry size_t len; 75251d54847SJohn Garry 7535f2c8efaSJames Clark perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier"); 75451d54847SJohn Garry 7555f2c8efaSJames Clark if (filename__read_str(path, &str, &len) < 0) 75651d54847SJohn Garry return NULL; 75751d54847SJohn Garry 75851d54847SJohn Garry str[len - 1] = 0; /* remove line feed */ 75951d54847SJohn Garry 76051d54847SJohn Garry return str; 76151d54847SJohn Garry } 76251d54847SJohn Garry 7634bf7e81aSIan Rogers /** 7644bf7e81aSIan Rogers * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in 7654bf7e81aSIan Rogers * sysfs on some platforms like ARM or Intel hybrid. Looking for 7664bf7e81aSIan Rogers * possible the cpus file in sysfs files to identify whether this is a 7674bf7e81aSIan Rogers * core device. 7684bf7e81aSIan Rogers * @name: The PMU name such as "cpu_atom". 76914b22ae0SGanapatrao Kulkarni */ 7704bf7e81aSIan Rogers static int is_sysfs_pmu_core(const char *name) 77114b22ae0SGanapatrao Kulkarni { 77214b22ae0SGanapatrao Kulkarni char path[PATH_MAX]; 77314b22ae0SGanapatrao Kulkarni 774f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus")) 77514b22ae0SGanapatrao Kulkarni return 0; 776d9664582SAndi Kleen return file_available(path); 77714b22ae0SGanapatrao Kulkarni } 77814b22ae0SGanapatrao Kulkarni 77929be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu) 780d77ade9fSAndi Kleen { 781d77ade9fSAndi Kleen char *cpuid; 782d77ade9fSAndi Kleen static bool printed; 783d77ade9fSAndi Kleen 784d77ade9fSAndi Kleen cpuid = getenv("PERF_CPUID"); 785d77ade9fSAndi Kleen if (cpuid) 786d77ade9fSAndi Kleen cpuid = strdup(cpuid); 787d77ade9fSAndi Kleen if (!cpuid) 78854e32dc0SGanapatrao Kulkarni cpuid = get_cpuid_str(pmu); 789d77ade9fSAndi Kleen if (!cpuid) 790d77ade9fSAndi Kleen return NULL; 791d77ade9fSAndi Kleen 792d77ade9fSAndi Kleen if (!printed) { 793d77ade9fSAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 794d77ade9fSAndi Kleen printed = true; 795d77ade9fSAndi Kleen } 796d77ade9fSAndi Kleen return cpuid; 797d77ade9fSAndi Kleen } 798d77ade9fSAndi Kleen 7991ba3752aSIan Rogers __weak const struct pmu_events_table *pmu_events_table__find(void) 800e126bef5SJohn Garry { 80196d2a746SIan Rogers return perf_pmu__find_events_table(NULL); 802e126bef5SJohn Garry } 803e126bef5SJohn Garry 804f8ea2c15SIan Rogers __weak const struct pmu_metrics_table *pmu_metrics_table__find(void) 805f8ea2c15SIan Rogers { 806f8ea2c15SIan Rogers return perf_pmu__find_metrics_table(NULL); 807f8ea2c15SIan Rogers } 808f8ea2c15SIan Rogers 809240e6fd0SIan Rogers /** 810240e6fd0SIan Rogers * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any 811240e6fd0SIan Rogers * trailing suffix? The Suffix must be in form 812240e6fd0SIan Rogers * tok_{digits}, or tok{digits}. 813240e6fd0SIan Rogers * @pmu_name: The pmu_name with possible suffix. 814240e6fd0SIan Rogers * @tok: The possible match to pmu_name without suffix. 815c07d5c92SJohn Garry */ 816240e6fd0SIan Rogers static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok) 817c47a5599SJin Yao { 818c07d5c92SJohn Garry const char *p; 819c47a5599SJin Yao 820c47a5599SJin Yao if (strncmp(pmu_name, tok, strlen(tok))) 821c47a5599SJin Yao return false; 822c47a5599SJin Yao 823c47a5599SJin Yao p = pmu_name + strlen(tok); 824c47a5599SJin Yao if (*p == 0) 825c47a5599SJin Yao return true; 826c47a5599SJin Yao 827c07d5c92SJohn Garry if (*p == '_') 828c47a5599SJin Yao ++p; 829c07d5c92SJohn Garry 830c07d5c92SJohn Garry /* Ensure we end in a number */ 831c07d5c92SJohn Garry while (1) { 832c07d5c92SJohn Garry if (!isdigit(*p)) 833c47a5599SJin Yao return false; 834c07d5c92SJohn Garry if (*(++p) == 0) 835c07d5c92SJohn Garry break; 836c07d5c92SJohn Garry } 837c47a5599SJin Yao 838c47a5599SJin Yao return true; 839c47a5599SJin Yao } 840c47a5599SJin Yao 841240e6fd0SIan Rogers /** 842240e6fd0SIan Rogers * pmu_uncore_alias_match - does name match the PMU name? 843240e6fd0SIan Rogers * @pmu_name: the json struct pmu_event name. This may lack a suffix (which 844240e6fd0SIan Rogers * matches) or be of the form "socket,pmuname" which will match 845240e6fd0SIan Rogers * "socketX_pmunameY". 846240e6fd0SIan Rogers * @name: a real full PMU name as from sysfs. 847240e6fd0SIan Rogers */ 848240e6fd0SIan Rogers static bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 849730670b1SJohn Garry { 850730670b1SJohn Garry char *tmp = NULL, *tok, *str; 851730670b1SJohn Garry bool res; 852730670b1SJohn Garry 853240e6fd0SIan Rogers if (strchr(pmu_name, ',') == NULL) 854240e6fd0SIan Rogers return perf_pmu__match_ignoring_suffix(name, pmu_name); 855240e6fd0SIan Rogers 856730670b1SJohn Garry str = strdup(pmu_name); 857730670b1SJohn Garry if (!str) 858730670b1SJohn Garry return false; 859730670b1SJohn Garry 860730670b1SJohn Garry /* 861730670b1SJohn Garry * uncore alias may be from different PMU with common prefix 862730670b1SJohn Garry */ 863730670b1SJohn Garry tok = strtok_r(str, ",", &tmp); 864730670b1SJohn Garry if (strncmp(pmu_name, tok, strlen(tok))) { 865730670b1SJohn Garry res = false; 866730670b1SJohn Garry goto out; 867730670b1SJohn Garry } 868730670b1SJohn Garry 869730670b1SJohn Garry /* 870730670b1SJohn Garry * Match more complex aliases where the alias name is a comma-delimited 871730670b1SJohn Garry * list of tokens, orderly contained in the matching PMU name. 872730670b1SJohn Garry * 873730670b1SJohn Garry * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 874730670b1SJohn Garry * match "socket" in "socketX_pmunameY" and then "pmuname" in 875730670b1SJohn Garry * "pmunameY". 876730670b1SJohn Garry */ 877c07d5c92SJohn Garry while (1) { 878c07d5c92SJohn Garry char *next_tok = strtok_r(NULL, ",", &tmp); 879c07d5c92SJohn Garry 880730670b1SJohn Garry name = strstr(name, tok); 881c07d5c92SJohn Garry if (!name || 882240e6fd0SIan Rogers (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) { 883730670b1SJohn Garry res = false; 884730670b1SJohn Garry goto out; 885730670b1SJohn Garry } 886c07d5c92SJohn Garry if (!next_tok) 887c07d5c92SJohn Garry break; 888c07d5c92SJohn Garry tok = next_tok; 889c07d5c92SJohn Garry name += strlen(tok); 890730670b1SJohn Garry } 891730670b1SJohn Garry 892730670b1SJohn Garry res = true; 893730670b1SJohn Garry out: 894730670b1SJohn Garry free(str); 895730670b1SJohn Garry return res; 896730670b1SJohn Garry } 897730670b1SJohn Garry 898660842e4SIan Rogers static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, 8991ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 900660842e4SIan Rogers void *vdata) 901660842e4SIan Rogers { 902f26d22f1SIan Rogers struct perf_pmu *pmu = vdata; 903660842e4SIan Rogers 904*7b723dbbSIan Rogers perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL, pe); 905660842e4SIan Rogers return 0; 906660842e4SIan Rogers } 907660842e4SIan Rogers 908933f82ffSSukadev Bhattiprolu /* 909d685819bSIan Rogers * From the pmu_events_table, find the events that correspond to the given 910d685819bSIan Rogers * PMU and add them to the list 'head'. 911933f82ffSSukadev Bhattiprolu */ 912838a8c5fSIan Rogers void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table) 913933f82ffSSukadev Bhattiprolu { 914f26d22f1SIan Rogers pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu); 915933f82ffSSukadev Bhattiprolu } 916933f82ffSSukadev Bhattiprolu 917838a8c5fSIan Rogers static void pmu_add_cpu_aliases(struct perf_pmu *pmu) 918e45ad701SJohn Garry { 9197c52f10cSIan Rogers if (!pmu->events_table) 920e45ad701SJohn Garry return; 921e45ad701SJohn Garry 922e6ff1eedSIan Rogers if (pmu->cpu_aliases_added) 923e6ff1eedSIan Rogers return; 924e6ff1eedSIan Rogers 9257c52f10cSIan Rogers pmu_add_cpu_aliases_table(pmu, pmu->events_table); 926e6ff1eedSIan Rogers pmu->cpu_aliases_added = true; 927e45ad701SJohn Garry } 928e45ad701SJohn Garry 92929be2fe0SIan Rogers static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, 9301ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 931f26d22f1SIan Rogers void *vdata) 9324513c719SJohn Garry { 933f26d22f1SIan Rogers struct perf_pmu *pmu = vdata; 9344513c719SJohn Garry 9354513c719SJohn Garry if (!pe->compat || !pe->pmu) 9364513c719SJohn Garry return 0; 9374513c719SJohn Garry 9384513c719SJohn Garry if (!strcmp(pmu->id, pe->compat) && 9394513c719SJohn Garry pmu_uncore_alias_match(pe->pmu, pmu->name)) { 940*7b723dbbSIan Rogers perf_pmu__new_alias(pmu, 941edb217ffSIan Rogers pe->name, 942edb217ffSIan Rogers pe->desc, 943edb217ffSIan Rogers pe->event, 944edb217ffSIan Rogers /*val_fd=*/ NULL, 945eab35953SJin Yao pe); 9464513c719SJohn Garry } 9474513c719SJohn Garry 9484513c719SJohn Garry return 0; 9494513c719SJohn Garry } 9504513c719SJohn Garry 951838a8c5fSIan Rogers void pmu_add_sys_aliases(struct perf_pmu *pmu) 9524513c719SJohn Garry { 9534513c719SJohn Garry if (!pmu->id) 9544513c719SJohn Garry return; 9554513c719SJohn Garry 956f26d22f1SIan Rogers pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu); 9574513c719SJohn Garry } 9584513c719SJohn Garry 959c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 960dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 961dc0a6202SAdrian Hunter { 962dc0a6202SAdrian Hunter return NULL; 963dc0a6202SAdrian Hunter } 964dc0a6202SAdrian Hunter 96513d60ba0SKan Liang char * __weak 96613d60ba0SKan Liang pmu_find_real_name(const char *name) 96713d60ba0SKan Liang { 96813d60ba0SKan Liang return (char *)name; 96913d60ba0SKan Liang } 97013d60ba0SKan Liang 97113d60ba0SKan Liang char * __weak 97213d60ba0SKan Liang pmu_find_alias_name(const char *name __maybe_unused) 97313d60ba0SKan Liang { 97413d60ba0SKan Liang return NULL; 97513d60ba0SKan Liang } 97613d60ba0SKan Liang 9773a69672eSNamhyung Kim static int pmu_max_precise(int dirfd, struct perf_pmu *pmu) 97890a86bdeSJiri Olsa { 97990a86bdeSJiri Olsa int max_precise = -1; 98090a86bdeSJiri Olsa 9813a69672eSNamhyung Kim perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise); 98290a86bdeSJiri Olsa return max_precise; 98390a86bdeSJiri Olsa } 98490a86bdeSJiri Olsa 9851eaf496eSIan Rogers struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name) 986cd82a32eSJiri Olsa { 987cd82a32eSJiri Olsa struct perf_pmu *pmu; 988cd82a32eSJiri Olsa __u32 type; 98913d60ba0SKan Liang char *name = pmu_find_real_name(lookup_name); 99013d60ba0SKan Liang char *alias_name; 99149afa7f6SJin Yao 992cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 993cd82a32eSJiri Olsa if (!pmu) 994cd82a32eSJiri Olsa return NULL; 995cd82a32eSJiri Olsa 996f63a536fSIan Rogers pmu->name = strdup(name); 997f63a536fSIan Rogers if (!pmu->name) 998f63a536fSIan Rogers goto err; 99988ed9184SIan Rogers 100088ed9184SIan Rogers /* 100188ed9184SIan Rogers * Read type early to fail fast if a lookup name isn't a PMU. Ensure 100288ed9184SIan Rogers * that type value is successfully assigned (return 1). 100388ed9184SIan Rogers */ 100488ed9184SIan Rogers if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1) 100588ed9184SIan Rogers goto err; 100688ed9184SIan Rogers 100788ed9184SIan Rogers INIT_LIST_HEAD(&pmu->format); 100888ed9184SIan Rogers INIT_LIST_HEAD(&pmu->aliases); 100988ed9184SIan Rogers INIT_LIST_HEAD(&pmu->caps); 101088ed9184SIan Rogers 1011838a8c5fSIan Rogers /* 1012838a8c5fSIan Rogers * The pmu data we store & need consists of the pmu 1013838a8c5fSIan Rogers * type value and format definitions. Load both right 1014838a8c5fSIan Rogers * now. 1015838a8c5fSIan Rogers */ 1016838a8c5fSIan Rogers if (pmu_format(pmu, dirfd, name)) { 1017838a8c5fSIan Rogers free(pmu); 1018838a8c5fSIan Rogers return NULL; 1019838a8c5fSIan Rogers } 1020838a8c5fSIan Rogers /* 1021838a8c5fSIan Rogers * Check the aliases first to avoid unnecessary work. 1022838a8c5fSIan Rogers */ 1023838a8c5fSIan Rogers if (pmu_aliases(pmu, dirfd, name)) { 1024838a8c5fSIan Rogers free(pmu); 1025838a8c5fSIan Rogers return NULL; 1026838a8c5fSIan Rogers } 1027d06593aaSIan Rogers pmu->is_core = is_pmu_core(name); 1028d06593aaSIan Rogers pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core); 102913d60ba0SKan Liang 103013d60ba0SKan Liang alias_name = pmu_find_alias_name(name); 103113d60ba0SKan Liang if (alias_name) { 103213d60ba0SKan Liang pmu->alias_name = strdup(alias_name); 103313d60ba0SKan Liang if (!pmu->alias_name) 103413d60ba0SKan Liang goto err; 103513d60ba0SKan Liang } 103613d60ba0SKan Liang 103754e32dc0SGanapatrao Kulkarni pmu->type = type; 10383a69672eSNamhyung Kim pmu->is_uncore = pmu_is_uncore(dirfd, name); 103951d54847SJohn Garry if (pmu->is_uncore) 104051d54847SJohn Garry pmu->id = pmu_id(name); 10413a69672eSNamhyung Kim pmu->max_precise = pmu_max_precise(dirfd, pmu); 10427c52f10cSIan Rogers pmu->events_table = perf_pmu__find_events_table(pmu); 1043838a8c5fSIan Rogers pmu_add_sys_aliases(pmu); 10441eaf496eSIan Rogers list_add_tail(&pmu->list, pmus); 1045dc0a6202SAdrian Hunter 1046dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 1047dc0a6202SAdrian Hunter 1048cd82a32eSJiri Olsa return pmu; 104913d60ba0SKan Liang err: 1050efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->name); 105113d60ba0SKan Liang free(pmu); 105213d60ba0SKan Liang return NULL; 1053cd82a32eSJiri Olsa } 1054cd82a32eSJiri Olsa 1055628eaa4eSIan Rogers /* Creates the PMU when sysfs scanning fails. */ 1056628eaa4eSIan Rogers struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus) 1057628eaa4eSIan Rogers { 1058628eaa4eSIan Rogers struct perf_pmu *pmu = zalloc(sizeof(*pmu)); 1059628eaa4eSIan Rogers 1060628eaa4eSIan Rogers if (!pmu) 1061628eaa4eSIan Rogers return NULL; 1062628eaa4eSIan Rogers 1063628eaa4eSIan Rogers pmu->name = strdup("cpu"); 1064628eaa4eSIan Rogers if (!pmu->name) { 1065628eaa4eSIan Rogers free(pmu); 1066628eaa4eSIan Rogers return NULL; 1067628eaa4eSIan Rogers } 1068628eaa4eSIan Rogers 1069628eaa4eSIan Rogers pmu->is_core = true; 1070628eaa4eSIan Rogers pmu->type = PERF_TYPE_RAW; 1071628eaa4eSIan Rogers pmu->cpus = cpu_map__online(); 1072628eaa4eSIan Rogers 1073628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->format); 1074628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->aliases); 1075628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->caps); 1076628eaa4eSIan Rogers list_add_tail(&pmu->list, core_pmus); 1077628eaa4eSIan Rogers return pmu; 1078628eaa4eSIan Rogers } 1079628eaa4eSIan Rogers 1080e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) 1081e552b7beSRob Herring { 1082e552b7beSRob Herring struct perf_pmu_format *format; 1083e552b7beSRob Herring 108468c25043SIan Rogers if (pmu->formats_checked) 108568c25043SIan Rogers return; 108668c25043SIan Rogers 108768c25043SIan Rogers pmu->formats_checked = true; 108868c25043SIan Rogers 1089e552b7beSRob Herring /* fake pmu doesn't have format list */ 1090e552b7beSRob Herring if (pmu == &perf_pmu__fake) 1091e552b7beSRob Herring return; 1092e552b7beSRob Herring 109350402641SIan Rogers list_for_each_entry(format, &pmu->format, list) { 109450402641SIan Rogers perf_pmu_format__load(pmu, format); 1095e552b7beSRob Herring if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) { 1096e552b7beSRob Herring pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" 1097e552b7beSRob Herring "which is not supported by this version of perf!\n", 1098e552b7beSRob Herring pmu->name, format->name, format->value); 1099e552b7beSRob Herring return; 1100e552b7beSRob Herring } 1101e552b7beSRob Herring } 110250402641SIan Rogers } 1103e552b7beSRob Herring 1104c6d616feSIan Rogers bool evsel__is_aux_event(const struct evsel *evsel) 1105e12ee9f7SAdrian Hunter { 1106e76026bdSArnaldo Carvalho de Melo struct perf_pmu *pmu = evsel__find_pmu(evsel); 1107e12ee9f7SAdrian Hunter 1108e12ee9f7SAdrian Hunter return pmu && pmu->auxtrace; 1109e12ee9f7SAdrian Hunter } 1110e12ee9f7SAdrian Hunter 11116593f019SJames Clark /* 11126593f019SJames Clark * Set @config_name to @val as long as the user hasn't already set or cleared it 11136593f019SJames Clark * by passing a config term on the command line. 11146593f019SJames Clark * 11156593f019SJames Clark * @val is the value to put into the bits specified by @config_name rather than 11166593f019SJames Clark * the bit pattern. It is shifted into position by this function, so to set 11176593f019SJames Clark * something to true, pass 1 for val rather than a pre shifted value. 11186593f019SJames Clark */ 11196593f019SJames Clark #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask)) 11206593f019SJames Clark void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel, 11216593f019SJames Clark const char *config_name, u64 val) 11226593f019SJames Clark { 11236593f019SJames Clark u64 user_bits = 0, bits; 11246593f019SJames Clark struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG); 11256593f019SJames Clark 11266593f019SJames Clark if (term) 11276593f019SJames Clark user_bits = term->val.cfg_chg; 11286593f019SJames Clark 1129da6a5afdSIan Rogers bits = perf_pmu__format_bits(pmu, config_name); 11306593f019SJames Clark 11316593f019SJames Clark /* Do nothing if the user changed the value */ 11326593f019SJames Clark if (bits & user_bits) 11336593f019SJames Clark return; 11346593f019SJames Clark 11356593f019SJames Clark /* Otherwise replace it */ 11366593f019SJames Clark evsel->core.attr.config &= ~bits; 11376593f019SJames Clark evsel->core.attr.config |= field_prep(bits, val); 11386593f019SJames Clark } 11396593f019SJames Clark 11405c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 114109ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 1142cd82a32eSJiri Olsa { 11435c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1144cd82a32eSJiri Olsa 1145cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 1146cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 1147cd82a32eSJiri Olsa return format; 1148cd82a32eSJiri Olsa 1149cd82a32eSJiri Olsa return NULL; 1150cd82a32eSJiri Olsa } 1151cd82a32eSJiri Olsa 1152da6a5afdSIan Rogers __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name) 115309ff6071SAdrian Hunter { 1154da6a5afdSIan Rogers struct perf_pmu_format *format = pmu_find_format(&pmu->format, name); 115509ff6071SAdrian Hunter __u64 bits = 0; 115609ff6071SAdrian Hunter int fbit; 115709ff6071SAdrian Hunter 115809ff6071SAdrian Hunter if (!format) 115909ff6071SAdrian Hunter return 0; 116009ff6071SAdrian Hunter 116109ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 116209ff6071SAdrian Hunter bits |= 1ULL << fbit; 116309ff6071SAdrian Hunter 116409ff6071SAdrian Hunter return bits; 116509ff6071SAdrian Hunter } 116609ff6071SAdrian Hunter 11677eb54733SIan Rogers int perf_pmu__format_type(struct perf_pmu *pmu, const char *name) 1168a1ac7de6SAdrian Hunter { 11697eb54733SIan Rogers struct perf_pmu_format *format = pmu_find_format(&pmu->format, name); 1170a1ac7de6SAdrian Hunter 1171a1ac7de6SAdrian Hunter if (!format) 1172a1ac7de6SAdrian Hunter return -1; 1173a1ac7de6SAdrian Hunter 117450402641SIan Rogers perf_pmu_format__load(pmu, format); 1175a1ac7de6SAdrian Hunter return format->value; 1176a1ac7de6SAdrian Hunter } 1177a1ac7de6SAdrian Hunter 1178cd82a32eSJiri Olsa /* 1179dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 11804d39c89fSIngo Molnar * and unformatted value (value parameter). 1181cd82a32eSJiri Olsa */ 1182dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1183dc0a6202SAdrian Hunter bool zero) 1184cd82a32eSJiri Olsa { 1185cd82a32eSJiri Olsa unsigned long fbit, vbit; 1186cd82a32eSJiri Olsa 1187cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1188cd82a32eSJiri Olsa 1189cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 1190cd82a32eSJiri Olsa continue; 1191cd82a32eSJiri Olsa 1192dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 1193dc0a6202SAdrian Hunter *v |= (1llu << fbit); 1194dc0a6202SAdrian Hunter else if (zero) 1195dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 1196cd82a32eSJiri Olsa } 1197cd82a32eSJiri Olsa } 1198cd82a32eSJiri Olsa 11990efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 12000efe6b67SAdrian Hunter { 12011b9caa10SJiri Olsa int w; 12020efe6b67SAdrian Hunter 12031b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 12041b9caa10SJiri Olsa if (!w) 12051b9caa10SJiri Olsa return 0; 12061b9caa10SJiri Olsa if (w < 64) 12071b9caa10SJiri Olsa return (1ULL << w) - 1; 12081b9caa10SJiri Olsa return -1; 12090efe6b67SAdrian Hunter } 12100efe6b67SAdrian Hunter 1211cd82a32eSJiri Olsa /* 1212688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 1213688d4dfcSCody P Schafer * in the remaining terms. 1214688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 1215688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 1216688d4dfcSCody P Schafer * in a config string) later on in the term list. 1217688d4dfcSCody P Schafer */ 1218688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 1219688d4dfcSCody P Schafer struct list_head *head_terms, 1220688d4dfcSCody P Schafer __u64 *value) 1221688d4dfcSCody P Schafer { 1222688d4dfcSCody P Schafer struct parse_events_term *t; 1223688d4dfcSCody P Schafer 1224688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 12252a3d252dSIan Rogers if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 12262a3d252dSIan Rogers t->config && !strcmp(t->config, term->config)) { 1227688d4dfcSCody P Schafer t->used = true; 1228688d4dfcSCody P Schafer *value = t->val.num; 1229688d4dfcSCody P Schafer return 0; 1230688d4dfcSCody P Schafer } 1231688d4dfcSCody P Schafer } 1232688d4dfcSCody P Schafer 1233bb963e16SNamhyung Kim if (verbose > 0) 1234688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 1235688d4dfcSCody P Schafer 1236688d4dfcSCody P Schafer return -1; 1237688d4dfcSCody P Schafer } 1238688d4dfcSCody P Schafer 1239ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 1240e64b020bSJiri Olsa { 1241e64b020bSJiri Olsa struct perf_pmu_format *format; 124211db4e29SMasami Hiramatsu char *str = NULL; 124311db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1244f1417ceaSXin Gao unsigned int i = 0; 1245e64b020bSJiri Olsa 1246ffeb883eSHe Kuang if (!formats) 1247e64b020bSJiri Olsa return NULL; 1248e64b020bSJiri Olsa 1249e64b020bSJiri Olsa /* sysfs exported terms */ 1250ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 125111db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 125211db4e29SMasami Hiramatsu goto error; 1253e64b020bSJiri Olsa 1254ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 125511db4e29SMasami Hiramatsu error: 1256ffeb883eSHe Kuang strbuf_release(&buf); 1257e64b020bSJiri Olsa 1258e64b020bSJiri Olsa return str; 1259e64b020bSJiri Olsa } 1260e64b020bSJiri Olsa 1261688d4dfcSCody P Schafer /* 1262cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 126388aca8d9SCody P Schafer * user input data - term parameter. 1264cd82a32eSJiri Olsa */ 1265804fee5dSIan Rogers static int pmu_config_term(struct perf_pmu *pmu, 1266cd82a32eSJiri Olsa struct perf_event_attr *attr, 1267dc0a6202SAdrian Hunter struct parse_events_term *term, 1268688d4dfcSCody P Schafer struct list_head *head_terms, 1269e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1270cd82a32eSJiri Olsa { 12715c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1272cd82a32eSJiri Olsa __u64 *vp; 12730efe6b67SAdrian Hunter __u64 val, max_val; 1274cd82a32eSJiri Olsa 1275cd82a32eSJiri Olsa /* 1276688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1277688d4dfcSCody P Schafer * skip it in normal eval. 1278688d4dfcSCody P Schafer */ 1279688d4dfcSCody P Schafer if (term->used) 1280688d4dfcSCody P Schafer return 0; 1281688d4dfcSCody P Schafer 1282688d4dfcSCody P Schafer /* 1283cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1284cd82a32eSJiri Olsa * to be done for them. 1285cd82a32eSJiri Olsa */ 1286cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1287cd82a32eSJiri Olsa return 0; 1288cd82a32eSJiri Olsa 1289804fee5dSIan Rogers format = pmu_find_format(&pmu->format, term->config); 1290688d4dfcSCody P Schafer if (!format) { 1291804fee5dSIan Rogers char *pmu_term = pmu_formats_string(&pmu->format); 12924ac22b48SIan Rogers char *unknown_term; 12934ac22b48SIan Rogers char *help_msg; 1294ffeb883eSHe Kuang 12954ac22b48SIan Rogers if (asprintf(&unknown_term, 12964ac22b48SIan Rogers "unknown term '%s' for pmu '%s'", 1297804fee5dSIan Rogers term->config, pmu->name) < 0) 12984ac22b48SIan Rogers unknown_term = NULL; 12994ac22b48SIan Rogers help_msg = parse_events_formats_error_string(pmu_term); 13004ac22b48SIan Rogers if (err) { 13016c191289SIan Rogers parse_events_error__handle(err, term->err_term, 13024ac22b48SIan Rogers unknown_term, 13034ac22b48SIan Rogers help_msg); 13044ac22b48SIan Rogers } else { 13054ac22b48SIan Rogers pr_debug("%s (%s)\n", unknown_term, help_msg); 13064ac22b48SIan Rogers free(unknown_term); 1307e64b020bSJiri Olsa } 13084ac22b48SIan Rogers free(pmu_term); 1309cd82a32eSJiri Olsa return -EINVAL; 1310688d4dfcSCody P Schafer } 131150402641SIan Rogers perf_pmu_format__load(pmu, format); 1312cd82a32eSJiri Olsa switch (format->value) { 1313cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1314cd82a32eSJiri Olsa vp = &attr->config; 1315cd82a32eSJiri Olsa break; 1316cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1317cd82a32eSJiri Olsa vp = &attr->config1; 1318cd82a32eSJiri Olsa break; 1319cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1320cd82a32eSJiri Olsa vp = &attr->config2; 1321cd82a32eSJiri Olsa break; 1322204e7c49SRob Herring case PERF_PMU_FORMAT_VALUE_CONFIG3: 1323204e7c49SRob Herring vp = &attr->config3; 1324204e7c49SRob Herring break; 1325cd82a32eSJiri Olsa default: 1326cd82a32eSJiri Olsa return -EINVAL; 1327cd82a32eSJiri Olsa } 1328cd82a32eSJiri Olsa 132916fa7e82SJiri Olsa /* 1330688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1331688d4dfcSCody P Schafer * using event parameters. 133216fa7e82SJiri Olsa */ 133399e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 133499e7138eSJiri Olsa if (term->no_value && 133599e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 133699e7138eSJiri Olsa if (err) { 13376c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1338448d732cSIan Rogers strdup("no value assigned for term"), 1339448d732cSIan Rogers NULL); 134099e7138eSJiri Olsa } 134199e7138eSJiri Olsa return -EINVAL; 134299e7138eSJiri Olsa } 134399e7138eSJiri Olsa 1344688d4dfcSCody P Schafer val = term->val.num; 134599e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1346688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1347bb963e16SNamhyung Kim if (verbose > 0) { 1348688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1349688d4dfcSCody P Schafer term->config, term->val.str); 1350e64b020bSJiri Olsa } 1351e64b020bSJiri Olsa if (err) { 13526c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1353448d732cSIan Rogers strdup("expected numeric value"), 1354448d732cSIan Rogers NULL); 1355e64b020bSJiri Olsa } 1356688d4dfcSCody P Schafer return -EINVAL; 1357688d4dfcSCody P Schafer } 1358688d4dfcSCody P Schafer 1359688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1360688d4dfcSCody P Schafer return -EINVAL; 1361688d4dfcSCody P Schafer } else 1362688d4dfcSCody P Schafer return -EINVAL; 1363688d4dfcSCody P Schafer 13640efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 13650efe6b67SAdrian Hunter if (val > max_val) { 13660efe6b67SAdrian Hunter if (err) { 1367448d732cSIan Rogers char *err_str; 1368448d732cSIan Rogers 13696c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1370448d732cSIan Rogers asprintf(&err_str, 13710efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1372448d732cSIan Rogers (unsigned long long)max_val) < 0 1373448d732cSIan Rogers ? strdup("value too big for format") 1374448d732cSIan Rogers : err_str, 1375448d732cSIan Rogers NULL); 13760efe6b67SAdrian Hunter return -EINVAL; 13770efe6b67SAdrian Hunter } 13780efe6b67SAdrian Hunter /* 13790efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 13800efe6b67SAdrian Hunter * silently truncated. 13810efe6b67SAdrian Hunter */ 13820efe6b67SAdrian Hunter } 13830efe6b67SAdrian Hunter 1384688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1385cd82a32eSJiri Olsa return 0; 1386cd82a32eSJiri Olsa } 1387cd82a32eSJiri Olsa 1388804fee5dSIan Rogers int perf_pmu__config_terms(struct perf_pmu *pmu, 1389cff7f956SJiri Olsa struct perf_event_attr *attr, 1390dc0a6202SAdrian Hunter struct list_head *head_terms, 1391e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1392cd82a32eSJiri Olsa { 13936cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1394cd82a32eSJiri Olsa 1395688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 1396804fee5dSIan Rogers if (pmu_config_term(pmu, attr, term, head_terms, zero, err)) 1397cd82a32eSJiri Olsa return -EINVAL; 1398688d4dfcSCody P Schafer } 1399cd82a32eSJiri Olsa 1400cd82a32eSJiri Olsa return 0; 1401cd82a32eSJiri Olsa } 1402cd82a32eSJiri Olsa 1403cd82a32eSJiri Olsa /* 1404cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1405cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1406cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1407cd82a32eSJiri Olsa */ 1408cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1409e64b020bSJiri Olsa struct list_head *head_terms, 1410e64b020bSJiri Olsa struct parse_events_error *err) 1411cd82a32eSJiri Olsa { 1412dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1413dc0a6202SAdrian Hunter 1414804fee5dSIan Rogers return perf_pmu__config_terms(pmu, attr, head_terms, zero, err); 1415cd82a32eSJiri Olsa } 1416cd82a32eSJiri Olsa 14175c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 14186cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1419a6146d50SZheng Yan { 1420e6ff1eedSIan Rogers struct perf_pmu_alias *alias; 1421a6146d50SZheng Yan char *name; 1422a6146d50SZheng Yan 1423a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1424a6146d50SZheng Yan return NULL; 1425a6146d50SZheng Yan 1426a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1427a6146d50SZheng Yan if (term->val.num != 1) 1428a6146d50SZheng Yan return NULL; 1429a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1430a6146d50SZheng Yan return NULL; 1431a6146d50SZheng Yan name = term->config; 1432c3245d20SIan Rogers 1433a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1434a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1435a6146d50SZheng Yan return NULL; 1436a6146d50SZheng Yan name = term->val.str; 1437a6146d50SZheng Yan } else { 1438a6146d50SZheng Yan return NULL; 1439a6146d50SZheng Yan } 1440a6146d50SZheng Yan 1441e6ff1eedSIan Rogers alias = perf_pmu__find_alias(pmu, name); 1442e6ff1eedSIan Rogers if (alias || pmu->cpu_aliases_added) 1443e6ff1eedSIan Rogers return alias; 1444e6ff1eedSIan Rogers 1445e6ff1eedSIan Rogers /* Alias doesn't exist, try to get it from the json events. */ 1446e6ff1eedSIan Rogers if (pmu->events_table && 1447e6ff1eedSIan Rogers pmu_events_table__find_event(pmu->events_table, pmu, name, 1448e6ff1eedSIan Rogers pmu_add_cpu_aliases_map_callback, 1449e6ff1eedSIan Rogers pmu) == 0) { 1450e6ff1eedSIan Rogers alias = perf_pmu__find_alias(pmu, name); 1451e6ff1eedSIan Rogers } 1452e6ff1eedSIan Rogers return alias; 1453a6146d50SZheng Yan } 1454a6146d50SZheng Yan 1455410136f5SStephane Eranian 1456*7b723dbbSIan Rogers static int check_info_data(struct perf_pmu *pmu, 1457*7b723dbbSIan Rogers struct perf_pmu_alias *alias, 14589d31cb93SIan Rogers struct perf_pmu_info *info, 14599d31cb93SIan Rogers struct parse_events_error *err, 14609d31cb93SIan Rogers int column) 1461410136f5SStephane Eranian { 1462*7b723dbbSIan Rogers read_alias_info(pmu, alias); 1463410136f5SStephane Eranian /* 1464410136f5SStephane Eranian * Only one term in event definition can 14651d9e446bSJiri Olsa * define unit, scale and snapshot, fail 14661d9e446bSJiri Olsa * if there's more than one. 1467410136f5SStephane Eranian */ 14689d31cb93SIan Rogers if (info->unit && alias->unit[0]) { 14699d31cb93SIan Rogers parse_events_error__handle(err, column, 14709d31cb93SIan Rogers strdup("Attempt to set event's unit twice"), 14719d31cb93SIan Rogers NULL); 1472410136f5SStephane Eranian return -EINVAL; 14739d31cb93SIan Rogers } 14749d31cb93SIan Rogers if (info->scale && alias->scale) { 14759d31cb93SIan Rogers parse_events_error__handle(err, column, 14769d31cb93SIan Rogers strdup("Attempt to set event's scale twice"), 14779d31cb93SIan Rogers NULL); 14789d31cb93SIan Rogers return -EINVAL; 14799d31cb93SIan Rogers } 14809d31cb93SIan Rogers if (info->snapshot && alias->snapshot) { 14819d31cb93SIan Rogers parse_events_error__handle(err, column, 14829d31cb93SIan Rogers strdup("Attempt to set event snapshot twice"), 14839d31cb93SIan Rogers NULL); 14849d31cb93SIan Rogers return -EINVAL; 14859d31cb93SIan Rogers } 1486410136f5SStephane Eranian 1487b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 14881d9e446bSJiri Olsa info->unit = alias->unit; 1489410136f5SStephane Eranian 1490410136f5SStephane Eranian if (alias->scale) 14911d9e446bSJiri Olsa info->scale = alias->scale; 14921d9e446bSJiri Olsa 14931d9e446bSJiri Olsa if (alias->snapshot) 14941d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1495410136f5SStephane Eranian 1496410136f5SStephane Eranian return 0; 1497410136f5SStephane Eranian } 1498410136f5SStephane Eranian 1499a6146d50SZheng Yan /* 1500a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1501a6146d50SZheng Yan * defined for the alias 1502a6146d50SZheng Yan */ 1503410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 15049d31cb93SIan Rogers struct perf_pmu_info *info, struct parse_events_error *err) 1505a6146d50SZheng Yan { 15066cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 15075c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1508a6146d50SZheng Yan int ret; 1509a6146d50SZheng Yan 1510044330c1SMatt Fleming info->per_pkg = false; 1511044330c1SMatt Fleming 15128a398897SStephane Eranian /* 15138a398897SStephane Eranian * Mark unit and scale as not set 15148a398897SStephane Eranian * (different from default values, see below) 15158a398897SStephane Eranian */ 151646441bdcSMatt Fleming info->unit = NULL; 151746441bdcSMatt Fleming info->scale = 0.0; 15181d9e446bSJiri Olsa info->snapshot = false; 1519410136f5SStephane Eranian 1520a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1521a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1522a6146d50SZheng Yan if (!alias) 1523a6146d50SZheng Yan continue; 1524a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 15259d31cb93SIan Rogers if (ret) { 15269d31cb93SIan Rogers parse_events_error__handle(err, term->err_term, 15279d31cb93SIan Rogers strdup("Failure to duplicate terms"), 15289d31cb93SIan Rogers NULL); 1529a6146d50SZheng Yan return ret; 15309d31cb93SIan Rogers } 1531410136f5SStephane Eranian 1532*7b723dbbSIan Rogers ret = check_info_data(pmu, alias, info, err, term->err_term); 1533410136f5SStephane Eranian if (ret) 1534410136f5SStephane Eranian return ret; 1535410136f5SStephane Eranian 1536044330c1SMatt Fleming if (alias->per_pkg) 1537044330c1SMatt Fleming info->per_pkg = true; 1538044330c1SMatt Fleming 1539e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 15401dc92556SIan Rogers parse_events_term__delete(term); 1541a6146d50SZheng Yan } 15428a398897SStephane Eranian 15438a398897SStephane Eranian /* 15448a398897SStephane Eranian * if no unit or scale found in aliases, then 15458a398897SStephane Eranian * set defaults as for evsel 15468a398897SStephane Eranian * unit cannot left to NULL 15478a398897SStephane Eranian */ 154846441bdcSMatt Fleming if (info->unit == NULL) 154946441bdcSMatt Fleming info->unit = ""; 15508a398897SStephane Eranian 155146441bdcSMatt Fleming if (info->scale == 0.0) 155246441bdcSMatt Fleming info->scale = 1.0; 15538a398897SStephane Eranian 1554a6146d50SZheng Yan return 0; 1555a6146d50SZheng Yan } 1556a6146d50SZheng Yan 1557c3245d20SIan Rogers struct find_event_args { 1558c3245d20SIan Rogers const char *event; 1559c3245d20SIan Rogers void *state; 1560c3245d20SIan Rogers pmu_event_callback cb; 1561c3245d20SIan Rogers }; 1562c3245d20SIan Rogers 1563c3245d20SIan Rogers static int find_event_callback(void *state, struct pmu_event_info *info) 1564838a8c5fSIan Rogers { 1565c3245d20SIan Rogers struct find_event_args *args = state; 1566838a8c5fSIan Rogers 1567c3245d20SIan Rogers if (!strcmp(args->event, info->name)) 1568c3245d20SIan Rogers return args->cb(args->state, info); 1569838a8c5fSIan Rogers 1570c3245d20SIan Rogers return 0; 1571838a8c5fSIan Rogers } 1572c3245d20SIan Rogers 1573c3245d20SIan Rogers int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb) 1574c3245d20SIan Rogers { 1575c3245d20SIan Rogers struct find_event_args args = { 1576c3245d20SIan Rogers .event = event, 1577c3245d20SIan Rogers .state = state, 1578c3245d20SIan Rogers .cb = cb, 1579c3245d20SIan Rogers }; 1580c3245d20SIan Rogers 1581c3245d20SIan Rogers return perf_pmu__for_each_event(pmu, &args, find_event_callback); 1582c3245d20SIan Rogers } 1583c3245d20SIan Rogers 1584804fee5dSIan Rogers static void perf_pmu__del_formats(struct list_head *formats) 1585d26383dcSNamhyung Kim { 1586d26383dcSNamhyung Kim struct perf_pmu_format *fmt, *tmp; 1587d26383dcSNamhyung Kim 1588d26383dcSNamhyung Kim list_for_each_entry_safe(fmt, tmp, formats, list) { 1589d26383dcSNamhyung Kim list_del(&fmt->list); 1590efe98a7aSArnaldo Carvalho de Melo zfree(&fmt->name); 1591d26383dcSNamhyung Kim free(fmt); 1592d26383dcSNamhyung Kim } 1593d26383dcSNamhyung Kim } 1594d26383dcSNamhyung Kim 159507d2b820SIan Rogers bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name) 159607d2b820SIan Rogers { 159707d2b820SIan Rogers struct perf_pmu_format *format; 159807d2b820SIan Rogers 159907d2b820SIan Rogers list_for_each_entry(format, &pmu->format, list) { 160007d2b820SIan Rogers if (!strcmp(format->name, name)) 160107d2b820SIan Rogers return true; 160207d2b820SIan Rogers } 160307d2b820SIan Rogers return false; 160407d2b820SIan Rogers } 160507d2b820SIan Rogers 1606d504fae9SJohn Garry bool is_pmu_core(const char *name) 1607d504fae9SJohn Garry { 16086fbd67b0SThomas Richter return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name); 16094bf7e81aSIan Rogers } 16104bf7e81aSIan Rogers 16116fd1e519SIan Rogers bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu) 16126fd1e519SIan Rogers { 1613e20d1f2fSIan Rogers return pmu->is_core; 16146fd1e519SIan Rogers } 16156fd1e519SIan Rogers 161652c7b4d3SIan Rogers bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu) 161752c7b4d3SIan Rogers { 1618710dffc9SIan Rogers return !pmu->is_core || perf_pmus__num_core_pmus() == 1; 161952c7b4d3SIan Rogers } 162052c7b4d3SIan Rogers 1621e6ff1eedSIan Rogers bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name) 1622abe9544eSIan Rogers { 1623e6ff1eedSIan Rogers if (perf_pmu__find_alias(pmu, name) != NULL) 1624e6ff1eedSIan Rogers return true; 1625e6ff1eedSIan Rogers if (pmu->cpu_aliases_added || !pmu->events_table) 1626e6ff1eedSIan Rogers return false; 1627e6ff1eedSIan Rogers return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0; 16284cabc3d1SAndi Kleen } 1629c3245d20SIan Rogers 1630e6ff1eedSIan Rogers size_t perf_pmu__num_events(struct perf_pmu *pmu) 1631c3245d20SIan Rogers { 1632e6ff1eedSIan Rogers size_t nr = pmu->sysfs_aliases; 1633c3245d20SIan Rogers 1634e6ff1eedSIan Rogers if (pmu->cpu_aliases_added) 1635e6ff1eedSIan Rogers nr += pmu->loaded_json_aliases; 1636e6ff1eedSIan Rogers else if (pmu->events_table) 1637e6ff1eedSIan Rogers nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases; 1638c3245d20SIan Rogers 1639c3245d20SIan Rogers return pmu->selectable ? nr + 1 : nr; 1640c3245d20SIan Rogers } 1641c3245d20SIan Rogers 1642c3245d20SIan Rogers static int sub_non_neg(int a, int b) 1643c3245d20SIan Rogers { 1644c3245d20SIan Rogers if (b > a) 1645c3245d20SIan Rogers return 0; 1646c3245d20SIan Rogers return a - b; 1647c3245d20SIan Rogers } 1648c3245d20SIan Rogers 1649c3245d20SIan Rogers static char *format_alias(char *buf, int len, const struct perf_pmu *pmu, 1650c3245d20SIan Rogers const struct perf_pmu_alias *alias) 1651c3245d20SIan Rogers { 1652c3245d20SIan Rogers struct parse_events_term *term; 1653c3245d20SIan Rogers int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1654c3245d20SIan Rogers 1655c3245d20SIan Rogers list_for_each_entry(term, &alias->terms, list) { 1656c3245d20SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1657c3245d20SIan Rogers used += snprintf(buf + used, sub_non_neg(len, used), 1658c3245d20SIan Rogers ",%s=%s", term->config, 1659c3245d20SIan Rogers term->val.str); 1660c3245d20SIan Rogers } 1661c3245d20SIan Rogers 1662c3245d20SIan Rogers if (sub_non_neg(len, used) > 0) { 1663c3245d20SIan Rogers buf[used] = '/'; 1664c3245d20SIan Rogers used++; 1665c3245d20SIan Rogers } 1666c3245d20SIan Rogers if (sub_non_neg(len, used) > 0) { 1667c3245d20SIan Rogers buf[used] = '\0'; 1668c3245d20SIan Rogers used++; 1669c3245d20SIan Rogers } else 1670c3245d20SIan Rogers buf[len - 1] = '\0'; 1671c3245d20SIan Rogers 1672c3245d20SIan Rogers return buf; 1673c3245d20SIan Rogers } 1674c3245d20SIan Rogers 1675e6ff1eedSIan Rogers int perf_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb) 1676c3245d20SIan Rogers { 1677c3245d20SIan Rogers char buf[1024]; 1678c3245d20SIan Rogers struct perf_pmu_alias *event; 1679c3245d20SIan Rogers struct pmu_event_info info = { 1680c3245d20SIan Rogers .pmu = pmu, 1681c3245d20SIan Rogers }; 1682c3245d20SIan Rogers int ret = 0; 1683c3245d20SIan Rogers 1684e6ff1eedSIan Rogers pmu_add_cpu_aliases(pmu); 1685c3245d20SIan Rogers list_for_each_entry(event, &pmu->aliases, list) { 1686c3245d20SIan Rogers size_t buf_used; 1687c3245d20SIan Rogers 1688c3245d20SIan Rogers info.pmu_name = event->pmu_name ?: pmu->name; 1689c3245d20SIan Rogers info.alias = NULL; 1690c3245d20SIan Rogers if (event->desc) { 1691c3245d20SIan Rogers info.name = event->name; 1692c3245d20SIan Rogers buf_used = 0; 1693c3245d20SIan Rogers } else { 1694c3245d20SIan Rogers info.name = format_alias(buf, sizeof(buf), pmu, event); 1695c3245d20SIan Rogers if (pmu->is_core) { 1696c3245d20SIan Rogers info.alias = info.name; 1697c3245d20SIan Rogers info.name = event->name; 1698c3245d20SIan Rogers } 1699c3245d20SIan Rogers buf_used = strlen(buf) + 1; 1700c3245d20SIan Rogers } 1701c3245d20SIan Rogers info.scale_unit = NULL; 1702c3245d20SIan Rogers if (strlen(event->unit) || event->scale != 1.0) { 1703c3245d20SIan Rogers info.scale_unit = buf + buf_used; 1704c3245d20SIan Rogers buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used, 1705c3245d20SIan Rogers "%G%s", event->scale, event->unit) + 1; 1706c3245d20SIan Rogers } 1707c3245d20SIan Rogers info.desc = event->desc; 1708c3245d20SIan Rogers info.long_desc = event->long_desc; 1709c3245d20SIan Rogers info.encoding_desc = buf + buf_used; 1710c3245d20SIan Rogers buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used, 1711c3245d20SIan Rogers "%s/%s/", info.pmu_name, event->str) + 1; 1712c3245d20SIan Rogers info.topic = event->topic; 1713c3245d20SIan Rogers info.str = event->str; 1714c3245d20SIan Rogers info.deprecated = event->deprecated; 1715c3245d20SIan Rogers ret = cb(state, &info); 1716c3245d20SIan Rogers if (ret) 1717c3245d20SIan Rogers return ret; 1718c3245d20SIan Rogers } 1719c3245d20SIan Rogers if (pmu->selectable) { 1720c3245d20SIan Rogers info.name = buf; 1721c3245d20SIan Rogers snprintf(buf, sizeof(buf), "%s//", pmu->name); 1722c3245d20SIan Rogers info.alias = NULL; 1723c3245d20SIan Rogers info.scale_unit = NULL; 1724c3245d20SIan Rogers info.desc = NULL; 1725c3245d20SIan Rogers info.long_desc = NULL; 1726c3245d20SIan Rogers info.encoding_desc = NULL; 1727c3245d20SIan Rogers info.topic = NULL; 1728c3245d20SIan Rogers info.pmu_name = pmu->name; 1729c3245d20SIan Rogers info.deprecated = false; 1730c3245d20SIan Rogers ret = cb(state, &info); 1731c3245d20SIan Rogers } 1732c3245d20SIan Rogers return ret; 17334cabc3d1SAndi Kleen } 17347d4bdab5SAdrian Hunter 1735e3edd6cfSIan Rogers bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name) 1736e3edd6cfSIan Rogers { 1737e3edd6cfSIan Rogers return !strcmp(pmu->name, pmu_name) || 1738e3edd6cfSIan Rogers (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)); 1739e3edd6cfSIan Rogers } 1740e3edd6cfSIan Rogers 1741251aa040SIan Rogers bool perf_pmu__is_software(const struct perf_pmu *pmu) 1742251aa040SIan Rogers { 1743251aa040SIan Rogers if (pmu->is_core || pmu->is_uncore || pmu->auxtrace) 1744251aa040SIan Rogers return false; 1745251aa040SIan Rogers switch (pmu->type) { 1746251aa040SIan Rogers case PERF_TYPE_HARDWARE: return false; 1747251aa040SIan Rogers case PERF_TYPE_SOFTWARE: return true; 1748251aa040SIan Rogers case PERF_TYPE_TRACEPOINT: return true; 1749251aa040SIan Rogers case PERF_TYPE_HW_CACHE: return false; 1750251aa040SIan Rogers case PERF_TYPE_RAW: return false; 1751251aa040SIan Rogers case PERF_TYPE_BREAKPOINT: return true; 1752251aa040SIan Rogers default: break; 1753251aa040SIan Rogers } 1754251aa040SIan Rogers return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe"); 1755251aa040SIan Rogers } 1756251aa040SIan Rogers 1757d50a79cdSJames Clark FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 17587d4bdab5SAdrian Hunter { 17597d4bdab5SAdrian Hunter char path[PATH_MAX]; 17607d4bdab5SAdrian Hunter 1761f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) || 1762f8ad6018SJames Clark !file_available(path)) 17637d4bdab5SAdrian Hunter return NULL; 17647d4bdab5SAdrian Hunter 17657d4bdab5SAdrian Hunter return fopen(path, "r"); 17667d4bdab5SAdrian Hunter } 17677d4bdab5SAdrian Hunter 17683a69672eSNamhyung Kim FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name) 17693a69672eSNamhyung Kim { 17703a69672eSNamhyung Kim int fd; 17713a69672eSNamhyung Kim 17723a69672eSNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY); 17733a69672eSNamhyung Kim if (fd < 0) 17743a69672eSNamhyung Kim return NULL; 17753a69672eSNamhyung Kim 17763a69672eSNamhyung Kim return fdopen(fd, "r"); 17773a69672eSNamhyung Kim } 17783a69672eSNamhyung Kim 17797d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 17807d4bdab5SAdrian Hunter ...) 17817d4bdab5SAdrian Hunter { 17827d4bdab5SAdrian Hunter va_list args; 17837d4bdab5SAdrian Hunter FILE *file; 17847d4bdab5SAdrian Hunter int ret = EOF; 17857d4bdab5SAdrian Hunter 17867d4bdab5SAdrian Hunter va_start(args, fmt); 17877d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 17887d4bdab5SAdrian Hunter if (file) { 17897d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 17907d4bdab5SAdrian Hunter fclose(file); 17917d4bdab5SAdrian Hunter } 17927d4bdab5SAdrian Hunter va_end(args); 17937d4bdab5SAdrian Hunter return ret; 17947d4bdab5SAdrian Hunter } 17959fbc61f8SKan Liang 17963a69672eSNamhyung Kim int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name, 17973a69672eSNamhyung Kim const char *fmt, ...) 17983a69672eSNamhyung Kim { 17993a69672eSNamhyung Kim va_list args; 18003a69672eSNamhyung Kim FILE *file; 18013a69672eSNamhyung Kim int ret = EOF; 18023a69672eSNamhyung Kim 18033a69672eSNamhyung Kim va_start(args, fmt); 18043a69672eSNamhyung Kim file = perf_pmu__open_file_at(pmu, dirfd, name); 18053a69672eSNamhyung Kim if (file) { 18063a69672eSNamhyung Kim ret = vfscanf(file, fmt, args); 18073a69672eSNamhyung Kim fclose(file); 18083a69672eSNamhyung Kim } 18093a69672eSNamhyung Kim va_end(args); 18103a69672eSNamhyung Kim return ret; 18113a69672eSNamhyung Kim } 18123a69672eSNamhyung Kim 1813c2b6a896SGerman Gomez bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name) 1814c2b6a896SGerman Gomez { 1815c2b6a896SGerman Gomez char path[PATH_MAX]; 1816c2b6a896SGerman Gomez 1817c2b6a896SGerman Gomez if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name)) 1818c2b6a896SGerman Gomez return false; 1819c2b6a896SGerman Gomez 1820c2b6a896SGerman Gomez return file_available(path); 1821c2b6a896SGerman Gomez } 1822c2b6a896SGerman Gomez 18239fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 18249fbc61f8SKan Liang { 18259fbc61f8SKan Liang struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 18269fbc61f8SKan Liang 18279fbc61f8SKan Liang if (!caps) 18289fbc61f8SKan Liang return -ENOMEM; 18299fbc61f8SKan Liang 18309fbc61f8SKan Liang caps->name = strdup(name); 18319fbc61f8SKan Liang if (!caps->name) 18329fbc61f8SKan Liang goto free_caps; 18339fbc61f8SKan Liang caps->value = strndup(value, strlen(value) - 1); 18349fbc61f8SKan Liang if (!caps->value) 18359fbc61f8SKan Liang goto free_name; 18369fbc61f8SKan Liang list_add_tail(&caps->list, list); 18379fbc61f8SKan Liang return 0; 18389fbc61f8SKan Liang 18399fbc61f8SKan Liang free_name: 184057f14b5aSArnaldo Carvalho de Melo zfree(&caps->name); 18419fbc61f8SKan Liang free_caps: 18429fbc61f8SKan Liang free(caps); 18439fbc61f8SKan Liang 18449fbc61f8SKan Liang return -ENOMEM; 18459fbc61f8SKan Liang } 18469fbc61f8SKan Liang 1847eec11310SNamhyung Kim static void perf_pmu__del_caps(struct perf_pmu *pmu) 1848eec11310SNamhyung Kim { 1849eec11310SNamhyung Kim struct perf_pmu_caps *caps, *tmp; 1850eec11310SNamhyung Kim 1851eec11310SNamhyung Kim list_for_each_entry_safe(caps, tmp, &pmu->caps, list) { 1852eec11310SNamhyung Kim list_del(&caps->list); 1853efe98a7aSArnaldo Carvalho de Melo zfree(&caps->name); 1854efe98a7aSArnaldo Carvalho de Melo zfree(&caps->value); 1855eec11310SNamhyung Kim free(caps); 1856eec11310SNamhyung Kim } 1857eec11310SNamhyung Kim } 1858eec11310SNamhyung Kim 18599fbc61f8SKan Liang /* 18609fbc61f8SKan Liang * Reading/parsing the given pmu capabilities, which should be located at: 18619fbc61f8SKan Liang * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 18629fbc61f8SKan Liang * Return the number of capabilities 18639fbc61f8SKan Liang */ 18649fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu) 18659fbc61f8SKan Liang { 18669fbc61f8SKan Liang struct stat st; 18679fbc61f8SKan Liang char caps_path[PATH_MAX]; 18689fbc61f8SKan Liang DIR *caps_dir; 18699fbc61f8SKan Liang struct dirent *evt_ent; 1870b39094d3SNamhyung Kim int caps_fd; 18713339ec44SRavi Bangoria 18723339ec44SRavi Bangoria if (pmu->caps_initialized) 18733339ec44SRavi Bangoria return pmu->nr_caps; 18743339ec44SRavi Bangoria 18753339ec44SRavi Bangoria pmu->nr_caps = 0; 18769fbc61f8SKan Liang 1877f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps")) 18789fbc61f8SKan Liang return -1; 18799fbc61f8SKan Liang 18803339ec44SRavi Bangoria if (stat(caps_path, &st) < 0) { 18813339ec44SRavi Bangoria pmu->caps_initialized = true; 18829fbc61f8SKan Liang return 0; /* no error if caps does not exist */ 18833339ec44SRavi Bangoria } 18849fbc61f8SKan Liang 18859fbc61f8SKan Liang caps_dir = opendir(caps_path); 18869fbc61f8SKan Liang if (!caps_dir) 18879fbc61f8SKan Liang return -EINVAL; 18889fbc61f8SKan Liang 1889b39094d3SNamhyung Kim caps_fd = dirfd(caps_dir); 1890b39094d3SNamhyung Kim 18919fbc61f8SKan Liang while ((evt_ent = readdir(caps_dir)) != NULL) { 18929fbc61f8SKan Liang char *name = evt_ent->d_name; 18939fbc61f8SKan Liang char value[128]; 18949fbc61f8SKan Liang FILE *file; 1895b39094d3SNamhyung Kim int fd; 18969fbc61f8SKan Liang 18979fbc61f8SKan Liang if (!strcmp(name, ".") || !strcmp(name, "..")) 18989fbc61f8SKan Liang continue; 18999fbc61f8SKan Liang 1900b39094d3SNamhyung Kim fd = openat(caps_fd, name, O_RDONLY); 19010ea8920eSIan Rogers if (fd == -1) 19029fbc61f8SKan Liang continue; 19030ea8920eSIan Rogers file = fdopen(fd, "r"); 19040ea8920eSIan Rogers if (!file) { 19050ea8920eSIan Rogers close(fd); 19060ea8920eSIan Rogers continue; 19070ea8920eSIan Rogers } 19089fbc61f8SKan Liang 19099fbc61f8SKan Liang if (!fgets(value, sizeof(value), file) || 19109fbc61f8SKan Liang (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 19119fbc61f8SKan Liang fclose(file); 19129fbc61f8SKan Liang continue; 19139fbc61f8SKan Liang } 19149fbc61f8SKan Liang 19153339ec44SRavi Bangoria pmu->nr_caps++; 19169fbc61f8SKan Liang fclose(file); 19179fbc61f8SKan Liang } 19189fbc61f8SKan Liang 19199fbc61f8SKan Liang closedir(caps_dir); 19209fbc61f8SKan Liang 19213339ec44SRavi Bangoria pmu->caps_initialized = true; 19223339ec44SRavi Bangoria return pmu->nr_caps; 19239fbc61f8SKan Liang } 1924e4064776SJin Yao 1925b9f01032SIan Rogers static void perf_pmu__compute_config_masks(struct perf_pmu *pmu) 1926e4064776SJin Yao { 1927e4064776SJin Yao struct perf_pmu_format *format; 1928b9f01032SIan Rogers 1929b9f01032SIan Rogers if (pmu->config_masks_computed) 1930b9f01032SIan Rogers return; 1931e4064776SJin Yao 1932e4064776SJin Yao list_for_each_entry(format, &pmu->format, list) { 1933b9f01032SIan Rogers unsigned int i; 1934b9f01032SIan Rogers __u64 *mask; 1935b9f01032SIan Rogers 1936b9f01032SIan Rogers if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) 1937e4064776SJin Yao continue; 1938e4064776SJin Yao 1939b9f01032SIan Rogers pmu->config_masks_present = true; 1940b9f01032SIan Rogers mask = &pmu->config_masks[format->value]; 1941b9f01032SIan Rogers 1942e4064776SJin Yao for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1943b9f01032SIan Rogers *mask |= 1ULL << i; 1944e4064776SJin Yao } 1945b9f01032SIan Rogers pmu->config_masks_computed = true; 1946b9f01032SIan Rogers } 1947b9f01032SIan Rogers 1948b9f01032SIan Rogers void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 1949b9f01032SIan Rogers const char *name, int config_num, 1950b9f01032SIan Rogers const char *config_name) 1951b9f01032SIan Rogers { 1952b9f01032SIan Rogers __u64 bits; 1953b9f01032SIan Rogers char buf[100]; 1954b9f01032SIan Rogers 1955b9f01032SIan Rogers perf_pmu__compute_config_masks(pmu); 1956e4064776SJin Yao 1957e4064776SJin Yao /* 1958e4064776SJin Yao * Kernel doesn't export any valid format bits. 1959e4064776SJin Yao */ 1960b9f01032SIan Rogers if (!pmu->config_masks_present) 1961e4064776SJin Yao return; 1962e4064776SJin Yao 1963b9f01032SIan Rogers bits = config & ~pmu->config_masks[config_num]; 1964e4064776SJin Yao if (bits == 0) 1965e4064776SJin Yao return; 1966e4064776SJin Yao 1967e4064776SJin Yao bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1968e4064776SJin Yao 1969b9f01032SIan Rogers pr_warning("WARNING: event '%s' not valid (bits %s of %s " 1970e4064776SJin Yao "'%llx' not supported by kernel)!\n", 1971b9f01032SIan Rogers name ?: "N/A", buf, config_name, config); 1972e4064776SJin Yao } 1973c5a26ea4SJin Yao 1974c47a5599SJin Yao int perf_pmu__match(char *pattern, char *name, char *tok) 1975c47a5599SJin Yao { 197613d60ba0SKan Liang if (!name) 197713d60ba0SKan Liang return -1; 197813d60ba0SKan Liang 1979c47a5599SJin Yao if (fnmatch(pattern, name, 0)) 1980c47a5599SJin Yao return -1; 1981c47a5599SJin Yao 1982240e6fd0SIan Rogers if (tok && !perf_pmu__match_ignoring_suffix(name, tok)) 1983c47a5599SJin Yao return -1; 1984c47a5599SJin Yao 1985c47a5599SJin Yao return 0; 1986c47a5599SJin Yao } 19871d3351e6SJin Yao 1988acef233bSJing Zhang double __weak perf_pmu__cpu_slots_per_cycle(void) 1989acef233bSJing Zhang { 1990acef233bSJing Zhang return NAN; 1991acef233bSJing Zhang } 1992f8ad6018SJames Clark 1993f8ad6018SJames Clark int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size) 1994f8ad6018SJames Clark { 1995f8ad6018SJames Clark const char *sysfs = sysfs__mountpoint(); 1996f8ad6018SJames Clark 1997f8ad6018SJames Clark if (!sysfs) 1998f8ad6018SJames Clark return 0; 1999f8ad6018SJames Clark return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs); 2000f8ad6018SJames Clark } 2001f8ad6018SJames Clark 2002e293a5e8SNamhyung Kim int perf_pmu__event_source_devices_fd(void) 2003e293a5e8SNamhyung Kim { 2004e293a5e8SNamhyung Kim char path[PATH_MAX]; 2005e293a5e8SNamhyung Kim const char *sysfs = sysfs__mountpoint(); 2006e293a5e8SNamhyung Kim 2007e293a5e8SNamhyung Kim if (!sysfs) 2008e293a5e8SNamhyung Kim return -1; 2009e293a5e8SNamhyung Kim 2010e293a5e8SNamhyung Kim scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs); 2011e293a5e8SNamhyung Kim return open(path, O_DIRECTORY); 2012e293a5e8SNamhyung Kim } 2013e293a5e8SNamhyung Kim 2014f8ad6018SJames Clark /* 2015f8ad6018SJames Clark * Fill 'buf' with the path to a file or folder in 'pmu_name' in 2016f8ad6018SJames Clark * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format" 2017f8ad6018SJames Clark * then pathname will be filled with 2018f8ad6018SJames Clark * "/sys/bus/event_source/devices/cs_etm/format" 2019f8ad6018SJames Clark * 2020e1a3aad3SIan Rogers * Return 0 if the sysfs mountpoint couldn't be found, if no characters were 2021e1a3aad3SIan Rogers * written or if the buffer size is exceeded. 2022f8ad6018SJames Clark */ 2023f8ad6018SJames Clark int perf_pmu__pathname_scnprintf(char *buf, size_t size, 2024f8ad6018SJames Clark const char *pmu_name, const char *filename) 2025f8ad6018SJames Clark { 2026e1a3aad3SIan Rogers size_t len; 2027f8ad6018SJames Clark 2028e1a3aad3SIan Rogers len = perf_pmu__event_source_devices_scnprintf(buf, size); 2029e1a3aad3SIan Rogers if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size) 2030f8ad6018SJames Clark return 0; 2031e1a3aad3SIan Rogers 2032e1a3aad3SIan Rogers return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename); 2033f8ad6018SJames Clark } 2034eec11310SNamhyung Kim 2035e293a5e8SNamhyung Kim int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags) 2036e293a5e8SNamhyung Kim { 2037e293a5e8SNamhyung Kim char path[PATH_MAX]; 2038e293a5e8SNamhyung Kim 2039e293a5e8SNamhyung Kim scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename); 2040e293a5e8SNamhyung Kim return openat(dirfd, path, flags); 2041e293a5e8SNamhyung Kim } 2042e293a5e8SNamhyung Kim 20431eaf496eSIan Rogers void perf_pmu__delete(struct perf_pmu *pmu) 2044eec11310SNamhyung Kim { 2045eec11310SNamhyung Kim perf_pmu__del_formats(&pmu->format); 2046eec11310SNamhyung Kim perf_pmu__del_aliases(pmu); 2047eec11310SNamhyung Kim perf_pmu__del_caps(pmu); 2048eec11310SNamhyung Kim 2049eec11310SNamhyung Kim perf_cpu_map__put(pmu->cpus); 2050eec11310SNamhyung Kim 2051efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->default_config); 2052efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->name); 2053efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->alias_name); 2054eec11310SNamhyung Kim free(pmu); 2055eec11310SNamhyung Kim } 20569d5da30eSJames Clark 20579d5da30eSJames Clark struct perf_pmu *pmu__find_core_pmu(void) 20589d5da30eSJames Clark { 20599d5da30eSJames Clark struct perf_pmu *pmu = NULL; 20609d5da30eSJames Clark 20619d5da30eSJames Clark while ((pmu = perf_pmus__scan_core(pmu))) { 20629d5da30eSJames Clark /* 20639d5da30eSJames Clark * The cpumap should cover all CPUs. Otherwise, some CPUs may 20649d5da30eSJames Clark * not support some events or have different event IDs. 20659d5da30eSJames Clark */ 20669d5da30eSJames Clark if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu) 20679d5da30eSJames Clark return NULL; 20689d5da30eSJames Clark 20699d5da30eSJames Clark return pmu; 20709d5da30eSJames Clark } 20719d5da30eSJames Clark return NULL; 20729d5da30eSJames Clark } 2073