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; 61c3245d20SIan Rogers /** @unit: Units for the event, such as bytes or cache lines. */ 62c3245d20SIan Rogers char unit[UNIT_MAX_LEN+1]; 63c3245d20SIan Rogers /** @scale: Value to scale read counter values by. */ 64c3245d20SIan Rogers double scale; 65c3245d20SIan Rogers /** 66c3245d20SIan Rogers * @per_pkg: Does the file 67c3245d20SIan Rogers * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or 68c3245d20SIan Rogers * equivalent json value exist and have the value 1. 69c3245d20SIan Rogers */ 70c3245d20SIan Rogers bool per_pkg; 71c3245d20SIan Rogers /** 72c3245d20SIan Rogers * @snapshot: Does the file 73c3245d20SIan Rogers * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot 74c3245d20SIan Rogers * exist and have the value 1. 75c3245d20SIan Rogers */ 76c3245d20SIan Rogers bool snapshot; 77c3245d20SIan Rogers /** 78c3245d20SIan Rogers * @deprecated: Is the event hidden and so not shown in perf list by 79c3245d20SIan Rogers * default. 80c3245d20SIan Rogers */ 81c3245d20SIan Rogers bool deprecated; 82c3245d20SIan Rogers /** 83c3245d20SIan Rogers * @pmu_name: The name copied from the json struct pmu_event. This can 84c3245d20SIan Rogers * differ from the PMU name as it won't have suffixes. 85c3245d20SIan Rogers */ 86c3245d20SIan Rogers char *pmu_name; 87c3245d20SIan Rogers }; 88c3245d20SIan Rogers 89fe13d43dSIan Rogers /** 90fe13d43dSIan Rogers * struct perf_pmu_format - Values from a format file read from 91fe13d43dSIan Rogers * <sysfs>/devices/cpu/format/ held in struct perf_pmu. 92fe13d43dSIan Rogers * 93fe13d43dSIan Rogers * For example, the contents of <sysfs>/devices/cpu/format/event may be 94fe13d43dSIan Rogers * "config:0-7" and will be represented here as name="event", 95fe13d43dSIan Rogers * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. 96fe13d43dSIan Rogers */ 97ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 9850402641SIan Rogers /** @list: Element on list within struct perf_pmu. */ 9950402641SIan Rogers struct list_head list; 10050402641SIan Rogers /** @bits: Which config bits are set by this format value. */ 10150402641SIan Rogers DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 102fe13d43dSIan Rogers /** @name: The modifier/file name. */ 103ab1bf653SArnaldo Carvalho de Melo char *name; 104fe13d43dSIan Rogers /** 105fe13d43dSIan Rogers * @value : Which config value the format relates to. Supported values 106fe13d43dSIan Rogers * are from PERF_PMU_FORMAT_VALUE_CONFIG to 107fe13d43dSIan Rogers * PERF_PMU_FORMAT_VALUE_CONFIG_END. 108fe13d43dSIan Rogers */ 10950402641SIan Rogers u16 value; 11050402641SIan Rogers /** @loaded: Has the contents been loaded/parsed. */ 11150402641SIan Rogers bool loaded; 112ab1bf653SArnaldo Carvalho de Melo }; 113ab1bf653SArnaldo Carvalho de Melo 11450402641SIan Rogers static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name) 11550402641SIan Rogers { 11650402641SIan Rogers struct perf_pmu_format *format; 11750402641SIan Rogers 11850402641SIan Rogers format = zalloc(sizeof(*format)); 11950402641SIan Rogers if (!format) 12050402641SIan Rogers return NULL; 12150402641SIan Rogers 12250402641SIan Rogers format->name = strdup(name); 12350402641SIan Rogers if (!format->name) { 12450402641SIan Rogers free(format); 12550402641SIan Rogers return NULL; 12650402641SIan Rogers } 12750402641SIan Rogers list_add_tail(&format->list, list); 12850402641SIan Rogers return format; 12950402641SIan Rogers } 13050402641SIan Rogers 13150402641SIan Rogers /* Called at the end of parsing a format. */ 13250402641SIan Rogers void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits) 13350402641SIan Rogers { 13450402641SIan Rogers struct perf_pmu_format *format = vformat; 13550402641SIan Rogers 13650402641SIan Rogers format->value = config; 13750402641SIan Rogers memcpy(format->bits, bits, sizeof(format->bits)); 13850402641SIan Rogers } 13950402641SIan Rogers 14050402641SIan Rogers static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file) 14150402641SIan Rogers { 14250402641SIan Rogers void *scanner; 14350402641SIan Rogers int ret; 14450402641SIan Rogers 14550402641SIan Rogers ret = perf_pmu_lex_init(&scanner); 14650402641SIan Rogers if (ret) 14750402641SIan Rogers return; 14850402641SIan Rogers 14950402641SIan Rogers perf_pmu_set_in(file, scanner); 15050402641SIan Rogers ret = perf_pmu_parse(format, scanner); 15150402641SIan Rogers perf_pmu_lex_destroy(scanner); 15250402641SIan Rogers format->loaded = true; 15350402641SIan Rogers } 15450402641SIan Rogers 15550402641SIan Rogers static void perf_pmu_format__load(struct perf_pmu *pmu, struct perf_pmu_format *format) 15650402641SIan Rogers { 15750402641SIan Rogers char path[PATH_MAX]; 15850402641SIan Rogers FILE *file = NULL; 15950402641SIan Rogers 16050402641SIan Rogers if (format->loaded) 16150402641SIan Rogers return; 16250402641SIan Rogers 16350402641SIan Rogers if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format")) 16450402641SIan Rogers return; 16550402641SIan Rogers 16650402641SIan Rogers assert(strlen(path) + strlen(format->name) + 2 < sizeof(path)); 16750402641SIan Rogers strcat(path, "/"); 16850402641SIan Rogers strcat(path, format->name); 16950402641SIan Rogers 17050402641SIan Rogers file = fopen(path, "r"); 17150402641SIan Rogers if (!file) 17250402641SIan Rogers return; 17350402641SIan Rogers __perf_pmu_format__load(format, file); 17450402641SIan Rogers fclose(file); 17550402641SIan Rogers } 17650402641SIan Rogers 177cd82a32eSJiri Olsa /* 178cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 179cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 180cd82a32eSJiri Olsa */ 18150402641SIan Rogers int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load) 182cd82a32eSJiri Olsa { 183cd82a32eSJiri Olsa struct dirent *evt_ent; 184cd82a32eSJiri Olsa DIR *format_dir; 185cd82a32eSJiri Olsa int ret = 0; 186cd82a32eSJiri Olsa 187e293a5e8SNamhyung Kim format_dir = fdopendir(dirfd); 188cd82a32eSJiri Olsa if (!format_dir) 189cd82a32eSJiri Olsa return -EINVAL; 190cd82a32eSJiri Olsa 19150402641SIan Rogers while ((evt_ent = readdir(format_dir)) != NULL) { 19250402641SIan Rogers struct perf_pmu_format *format; 193cd82a32eSJiri Olsa char *name = evt_ent->d_name; 194cd82a32eSJiri Olsa 195cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 196cd82a32eSJiri Olsa continue; 197cd82a32eSJiri Olsa 19850402641SIan Rogers format = perf_pmu__new_format(&pmu->format, name); 19950402641SIan Rogers if (!format) { 20050402641SIan Rogers ret = -ENOMEM; 201cd82a32eSJiri Olsa break; 20250402641SIan Rogers } 203cd82a32eSJiri Olsa 20450402641SIan Rogers if (eager_load) { 20550402641SIan Rogers FILE *file; 20650402641SIan Rogers int fd = openat(dirfd, name, O_RDONLY); 20750402641SIan Rogers 20850402641SIan Rogers if (fd < 0) { 20950402641SIan Rogers ret = -errno; 21050402641SIan Rogers break; 21150402641SIan Rogers } 2123d88aec0SIan Rogers file = fdopen(fd, "r"); 2133d88aec0SIan Rogers if (!file) { 2143d88aec0SIan Rogers close(fd); 2153d88aec0SIan Rogers break; 2163d88aec0SIan Rogers } 21750402641SIan Rogers __perf_pmu_format__load(format, file); 2183d88aec0SIan Rogers fclose(file); 2193d88aec0SIan Rogers } 220cd82a32eSJiri Olsa } 221cd82a32eSJiri Olsa 222cd82a32eSJiri Olsa closedir(format_dir); 223cd82a32eSJiri Olsa return ret; 224cd82a32eSJiri Olsa } 225cd82a32eSJiri Olsa 226cd82a32eSJiri Olsa /* 227cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 228cd82a32eSJiri Olsa * located at: 229cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 230cd82a32eSJiri Olsa */ 231838a8c5fSIan Rogers static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name) 232cd82a32eSJiri Olsa { 233e293a5e8SNamhyung Kim int fd; 234cd82a32eSJiri Olsa 235e293a5e8SNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY); 236e293a5e8SNamhyung Kim if (fd < 0) 237d9664582SAndi Kleen return 0; 238cd82a32eSJiri Olsa 239e293a5e8SNamhyung Kim /* it'll close the fd */ 24050402641SIan Rogers if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false)) 241cd82a32eSJiri Olsa return -1; 242cd82a32eSJiri Olsa 243cd82a32eSJiri Olsa return 0; 244cd82a32eSJiri Olsa } 245cd82a32eSJiri Olsa 246a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval) 247d02fc6bcSAndi Kleen { 248d02fc6bcSAndi Kleen char *lc; 249d02fc6bcSAndi Kleen int ret = 0; 250d02fc6bcSAndi Kleen 251d02fc6bcSAndi Kleen /* 252d02fc6bcSAndi Kleen * save current locale 253d02fc6bcSAndi Kleen */ 254d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 255d02fc6bcSAndi Kleen 256d02fc6bcSAndi Kleen /* 257d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 258d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 259d02fc6bcSAndi Kleen * call below. 260d02fc6bcSAndi Kleen */ 261d02fc6bcSAndi Kleen lc = strdup(lc); 262d02fc6bcSAndi Kleen if (!lc) { 263d02fc6bcSAndi Kleen ret = -ENOMEM; 264d02fc6bcSAndi Kleen goto out; 265d02fc6bcSAndi Kleen } 266d02fc6bcSAndi Kleen 267d02fc6bcSAndi Kleen /* 268d02fc6bcSAndi Kleen * force to C locale to ensure kernel 269d02fc6bcSAndi Kleen * scale string is converted correctly. 270d02fc6bcSAndi Kleen * kernel uses default C locale. 271d02fc6bcSAndi Kleen */ 272d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 273d02fc6bcSAndi Kleen 274d02fc6bcSAndi Kleen *sval = strtod(scale, end); 275d02fc6bcSAndi Kleen 276d02fc6bcSAndi Kleen out: 277d02fc6bcSAndi Kleen /* restore locale */ 278d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 279d02fc6bcSAndi Kleen free(lc); 280d02fc6bcSAndi Kleen return ret; 281d02fc6bcSAndi Kleen } 282d02fc6bcSAndi Kleen 283edb217ffSIan Rogers static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, const char *name) 284410136f5SStephane Eranian { 285410136f5SStephane Eranian struct stat st; 286410136f5SStephane Eranian ssize_t sret; 287410136f5SStephane Eranian char scale[128]; 288410136f5SStephane Eranian int fd, ret = -1; 289410136f5SStephane Eranian char path[PATH_MAX]; 290410136f5SStephane Eranian 291e293a5e8SNamhyung Kim scnprintf(path, PATH_MAX, "%s.scale", name); 292410136f5SStephane Eranian 293e293a5e8SNamhyung Kim fd = openat(dirfd, path, O_RDONLY); 294410136f5SStephane Eranian if (fd == -1) 295410136f5SStephane Eranian return -1; 296410136f5SStephane Eranian 297410136f5SStephane Eranian if (fstat(fd, &st) < 0) 298410136f5SStephane Eranian goto error; 299410136f5SStephane Eranian 300410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 301410136f5SStephane Eranian if (sret < 0) 302410136f5SStephane Eranian goto error; 303410136f5SStephane Eranian 3049ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 3059ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 3069ecae065SMadhavan Srinivasan else 307410136f5SStephane Eranian scale[sret] = '\0'; 3089ecae065SMadhavan Srinivasan 309a55ab7c4SJin Yao ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); 310410136f5SStephane Eranian error: 311410136f5SStephane Eranian close(fd); 312410136f5SStephane Eranian return ret; 313410136f5SStephane Eranian } 314410136f5SStephane Eranian 315edb217ffSIan Rogers static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, const char *name) 316410136f5SStephane Eranian { 317410136f5SStephane Eranian char path[PATH_MAX]; 318410136f5SStephane Eranian ssize_t sret; 319410136f5SStephane Eranian int fd; 320410136f5SStephane Eranian 321e293a5e8SNamhyung Kim scnprintf(path, PATH_MAX, "%s.unit", name); 322410136f5SStephane Eranian 323e293a5e8SNamhyung Kim fd = openat(dirfd, path, O_RDONLY); 324410136f5SStephane Eranian if (fd == -1) 325410136f5SStephane Eranian return -1; 326410136f5SStephane Eranian 327410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 328410136f5SStephane Eranian if (sret < 0) 329410136f5SStephane Eranian goto error; 330410136f5SStephane Eranian 331410136f5SStephane Eranian close(fd); 332410136f5SStephane Eranian 3339ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 3349ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 3359ecae065SMadhavan Srinivasan else 336410136f5SStephane Eranian alias->unit[sret] = '\0'; 337410136f5SStephane Eranian 338410136f5SStephane Eranian return 0; 339410136f5SStephane Eranian error: 340410136f5SStephane Eranian close(fd); 341410136f5SStephane Eranian alias->unit[0] = '\0'; 342410136f5SStephane Eranian return -1; 343410136f5SStephane Eranian } 344410136f5SStephane Eranian 345044330c1SMatt Fleming static int 346edb217ffSIan Rogers perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, const char *name) 347044330c1SMatt Fleming { 348044330c1SMatt Fleming char path[PATH_MAX]; 349044330c1SMatt Fleming int fd; 350044330c1SMatt Fleming 351e293a5e8SNamhyung Kim scnprintf(path, PATH_MAX, "%s.per-pkg", name); 352044330c1SMatt Fleming 353e293a5e8SNamhyung Kim fd = openat(dirfd, path, O_RDONLY); 354044330c1SMatt Fleming if (fd == -1) 355044330c1SMatt Fleming return -1; 356044330c1SMatt Fleming 357044330c1SMatt Fleming close(fd); 358044330c1SMatt Fleming 359044330c1SMatt Fleming alias->per_pkg = true; 360044330c1SMatt Fleming return 0; 361044330c1SMatt Fleming } 362044330c1SMatt Fleming 3631d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 364edb217ffSIan Rogers int dirfd, const char *name) 3651d9e446bSJiri Olsa { 3661d9e446bSJiri Olsa char path[PATH_MAX]; 3671d9e446bSJiri Olsa int fd; 3681d9e446bSJiri Olsa 369e293a5e8SNamhyung Kim scnprintf(path, PATH_MAX, "%s.snapshot", name); 3701d9e446bSJiri Olsa 371e293a5e8SNamhyung Kim fd = openat(dirfd, path, O_RDONLY); 3721d9e446bSJiri Olsa if (fd == -1) 3731d9e446bSJiri Olsa return -1; 3741d9e446bSJiri Olsa 3751d9e446bSJiri Olsa alias->snapshot = true; 3761d9e446bSJiri Olsa close(fd); 3771d9e446bSJiri Olsa return 0; 3781d9e446bSJiri Olsa } 3791d9e446bSJiri Olsa 3806dde6429SThomas Richter /* Delete an alias entry. */ 381c3245d20SIan Rogers static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 3826dde6429SThomas Richter { 3836dde6429SThomas Richter zfree(&newalias->name); 3846dde6429SThomas Richter zfree(&newalias->desc); 3856dde6429SThomas Richter zfree(&newalias->long_desc); 3866dde6429SThomas Richter zfree(&newalias->topic); 3876dde6429SThomas Richter zfree(&newalias->str); 38832705de7SJin Yao zfree(&newalias->pmu_name); 3896dde6429SThomas Richter parse_events_terms__purge(&newalias->terms); 3906dde6429SThomas Richter free(newalias); 3916dde6429SThomas Richter } 3926dde6429SThomas Richter 393eec11310SNamhyung Kim static void perf_pmu__del_aliases(struct perf_pmu *pmu) 394eec11310SNamhyung Kim { 395eec11310SNamhyung Kim struct perf_pmu_alias *alias, *tmp; 396eec11310SNamhyung Kim 397eec11310SNamhyung Kim list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) { 398eec11310SNamhyung Kim list_del(&alias->list); 399eec11310SNamhyung Kim perf_pmu_free_alias(alias); 400eec11310SNamhyung Kim } 401eec11310SNamhyung Kim } 402eec11310SNamhyung Kim 403f63a536fSIan Rogers static struct perf_pmu_alias *perf_pmu__find_alias(const struct perf_pmu *pmu, const char *name) 4046dde6429SThomas Richter { 405f63a536fSIan Rogers struct perf_pmu_alias *alias; 4066dde6429SThomas Richter 407f63a536fSIan Rogers list_for_each_entry(alias, &pmu->aliases, list) { 408f63a536fSIan Rogers if (!strcasecmp(alias->name, name)) 409f63a536fSIan Rogers return alias; 41032705de7SJin Yao } 411f63a536fSIan Rogers return NULL; 412f63a536fSIan Rogers } 413f63a536fSIan Rogers 414f63a536fSIan Rogers static bool assign_str(const char *name, const char *field, char **old_str, 415f63a536fSIan Rogers const char *new_str) 416f63a536fSIan Rogers { 417f63a536fSIan Rogers if (!*old_str && new_str) { 418f63a536fSIan Rogers *old_str = strdup(new_str); 4196dde6429SThomas Richter return true; 4206dde6429SThomas Richter } 421f63a536fSIan Rogers 422f63a536fSIan Rogers if (!new_str || !strcasecmp(*old_str, new_str)) 423f63a536fSIan Rogers return false; /* Nothing to update. */ 424f63a536fSIan Rogers 425f63a536fSIan Rogers pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n", 426f63a536fSIan Rogers name, field, *old_str, new_str); 427f63a536fSIan Rogers zfree(old_str); 428f63a536fSIan Rogers *old_str = strdup(new_str); 429f63a536fSIan Rogers return true; 4306dde6429SThomas Richter } 431f63a536fSIan Rogers 432f63a536fSIan Rogers static int update_alias(const struct pmu_event *pe, 433f63a536fSIan Rogers const struct pmu_events_table *table __maybe_unused, 434f63a536fSIan Rogers void *vdata) 435f63a536fSIan Rogers { 436f63a536fSIan Rogers struct perf_pmu_alias *alias = vdata; 437f63a536fSIan Rogers int ret = 0; 438f63a536fSIan Rogers 439f63a536fSIan Rogers assign_str(pe->name, "desc", &alias->desc, pe->desc); 440f63a536fSIan Rogers assign_str(pe->name, "long_desc", &alias->long_desc, pe->long_desc); 441f63a536fSIan Rogers assign_str(pe->name, "topic", &alias->topic, pe->topic); 442f63a536fSIan Rogers alias->per_pkg = pe->perpkg; 443f63a536fSIan Rogers if (assign_str(pe->name, "value", &alias->str, pe->event)) { 444f63a536fSIan Rogers parse_events_terms__purge(&alias->terms); 445f63a536fSIan Rogers ret = parse_events_terms(&alias->terms, pe->event, /*input=*/NULL); 446f63a536fSIan Rogers } 447f63a536fSIan Rogers if (!ret && pe->unit) { 448f63a536fSIan Rogers char *unit; 449f63a536fSIan Rogers 450f63a536fSIan Rogers ret = perf_pmu__convert_scale(pe->unit, &unit, &alias->scale); 451f63a536fSIan Rogers if (!ret) 452f63a536fSIan Rogers snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 453f63a536fSIan Rogers } 454f63a536fSIan Rogers return ret; 4556dde6429SThomas Richter } 4566dde6429SThomas Richter 457f26d22f1SIan Rogers static int perf_pmu__new_alias(struct perf_pmu *pmu, int dirfd, const char *name, 458edb217ffSIan Rogers const char *desc, const char *val, FILE *val_fd, 459edb217ffSIan Rogers const struct pmu_event *pe) 460a6146d50SZheng Yan { 4610c24d6fbSThomas Richter struct parse_events_term *term; 4625c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 463a6146d50SZheng Yan int ret; 4640c24d6fbSThomas Richter char newval[256]; 465330f40a0SIan Rogers const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL; 466bd680861SIan Rogers bool deprecated = false, perpkg = false; 467eab35953SJin Yao 468f63a536fSIan Rogers if (perf_pmu__find_alias(pmu, name)) { 469f63a536fSIan Rogers /* Alias was already created/loaded. */ 470f63a536fSIan Rogers return 0; 471f63a536fSIan Rogers } 472f63a536fSIan Rogers 473eab35953SJin Yao if (pe) { 474330f40a0SIan Rogers long_desc = pe->long_desc; 475330f40a0SIan Rogers topic = pe->topic; 476330f40a0SIan Rogers unit = pe->unit; 477bd680861SIan Rogers perpkg = pe->perpkg; 4789ed8b7dcSIan Rogers deprecated = pe->deprecated; 479330f40a0SIan Rogers pmu_name = pe->pmu; 480eab35953SJin Yao } 481a6146d50SZheng Yan 482a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 483a6146d50SZheng Yan if (!alias) 484a6146d50SZheng Yan return -ENOMEM; 485a6146d50SZheng Yan 486a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 487410136f5SStephane Eranian alias->scale = 1.0; 488410136f5SStephane Eranian alias->unit[0] = '\0'; 489bd680861SIan Rogers alias->per_pkg = perpkg; 49084530920SStephane Eranian alias->snapshot = false; 4919ed8b7dcSIan Rogers alias->deprecated = deprecated; 492410136f5SStephane Eranian 493edb217ffSIan Rogers ret = parse_events_terms(&alias->terms, val, val_fd); 494a6146d50SZheng Yan if (ret) { 49570c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 496a6146d50SZheng Yan free(alias); 497a6146d50SZheng Yan return ret; 498a6146d50SZheng Yan } 499a6146d50SZheng Yan 500a6146d50SZheng Yan alias->name = strdup(name); 501e293a5e8SNamhyung Kim if (dirfd >= 0) { 502410136f5SStephane Eranian /* 503410136f5SStephane Eranian * load unit name and scale if available 504410136f5SStephane Eranian */ 505e293a5e8SNamhyung Kim perf_pmu__parse_unit(alias, dirfd, name); 506e293a5e8SNamhyung Kim perf_pmu__parse_scale(alias, dirfd, name); 507e293a5e8SNamhyung Kim perf_pmu__parse_per_pkg(alias, dirfd, name); 508e293a5e8SNamhyung Kim perf_pmu__parse_snapshot(alias, dirfd, name); 50970c646e0SSukadev Bhattiprolu } 510410136f5SStephane Eranian 51108e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 512c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 513c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 514dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 515f63a536fSIan Rogers alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 516fedb2b51SAndi Kleen if (unit) { 517f63a536fSIan Rogers if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) { 518f63a536fSIan Rogers perf_pmu_free_alias(alias); 519fedb2b51SAndi Kleen return -1; 520f63a536fSIan Rogers } 521fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 522fedb2b51SAndi Kleen } 523f63a536fSIan Rogers if (!pe) { 524f63a536fSIan Rogers /* Update an event from sysfs with json data. */ 5257c52f10cSIan Rogers if (pmu->events_table) { 526e6ff1eedSIan Rogers if (pmu_events_table__find_event(pmu->events_table, pmu, name, 527e6ff1eedSIan Rogers update_alias, alias) == 0) 528e6ff1eedSIan Rogers pmu->loaded_json_aliases++; 5297c52f10cSIan Rogers } 530f63a536fSIan Rogers } 531f63a536fSIan Rogers 532f63a536fSIan Rogers /* Scan event and remove leading zeroes, spaces, newlines, some 533f63a536fSIan Rogers * platforms have terms specified as 534f63a536fSIan Rogers * event=0x0091 (read from files ../<PMU>/events/<FILE> 535f63a536fSIan Rogers * and terms specified as event=0x91 (read from JSON files). 536f63a536fSIan Rogers * 537f63a536fSIan Rogers * Rebuild string to make alias->str member comparable. 538f63a536fSIan Rogers */ 539f63a536fSIan Rogers ret = 0; 540f63a536fSIan Rogers list_for_each_entry(term, &alias->terms, list) { 541f63a536fSIan Rogers if (ret) 542f63a536fSIan Rogers ret += scnprintf(newval + ret, sizeof(newval) - ret, 543f63a536fSIan Rogers ","); 544f63a536fSIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 545f63a536fSIan Rogers ret += scnprintf(newval + ret, sizeof(newval) - ret, 546f63a536fSIan Rogers "%s=%#x", term->config, term->val.num); 547f63a536fSIan Rogers else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 548f63a536fSIan Rogers ret += scnprintf(newval + ret, sizeof(newval) - ret, 549f63a536fSIan Rogers "%s=%s", term->config, term->val.str); 550f63a536fSIan Rogers } 5510c24d6fbSThomas Richter alias->str = strdup(newval); 552e6ff1eedSIan Rogers if (!pe) 553e6ff1eedSIan Rogers pmu->sysfs_aliases++; 554e6ff1eedSIan Rogers else 555e6ff1eedSIan Rogers pmu->loaded_json_aliases++; 556f26d22f1SIan Rogers list_add_tail(&alias->list, &pmu->aliases); 557a6146d50SZheng Yan return 0; 558a6146d50SZheng Yan } 559a6146d50SZheng Yan 56046441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 56146441bdcSMatt Fleming { 56246441bdcSMatt Fleming size_t len; 56346441bdcSMatt Fleming 56446441bdcSMatt Fleming len = strlen(name); 56546441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 56646441bdcSMatt Fleming return true; 56746441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 56846441bdcSMatt Fleming return true; 569044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 570044330c1SMatt Fleming return true; 5711d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 5721d9e446bSJiri Olsa return true; 57346441bdcSMatt Fleming 57446441bdcSMatt Fleming return false; 57546441bdcSMatt Fleming } 57646441bdcSMatt Fleming 577a6146d50SZheng Yan /* 578a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 579a6146d50SZheng Yan * specified in 'dir' parameter. 580a6146d50SZheng Yan */ 581f26d22f1SIan Rogers static int pmu_aliases_parse(struct perf_pmu *pmu, int dirfd) 582a6146d50SZheng Yan { 583a6146d50SZheng Yan struct dirent *evt_ent; 584a6146d50SZheng Yan DIR *event_dir; 585e293a5e8SNamhyung Kim int fd; 586a6146d50SZheng Yan 587e293a5e8SNamhyung Kim event_dir = fdopendir(dirfd); 588a6146d50SZheng Yan if (!event_dir) 589a6146d50SZheng Yan return -EINVAL; 590a6146d50SZheng Yan 591940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 592a6146d50SZheng Yan char *name = evt_ent->d_name; 593a6146d50SZheng Yan FILE *file; 594a6146d50SZheng Yan 595a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 596a6146d50SZheng Yan continue; 597a6146d50SZheng Yan 598410136f5SStephane Eranian /* 59946441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 600410136f5SStephane Eranian */ 60146441bdcSMatt Fleming if (pmu_alias_info_file(name)) 602410136f5SStephane Eranian continue; 603410136f5SStephane Eranian 604e293a5e8SNamhyung Kim fd = openat(dirfd, name, O_RDONLY); 6050ea8920eSIan Rogers if (fd == -1) { 6060ea8920eSIan Rogers pr_debug("Cannot open %s\n", name); 6070ea8920eSIan Rogers continue; 6080ea8920eSIan Rogers } 609e293a5e8SNamhyung Kim file = fdopen(fd, "r"); 610940db6dcSAndi Kleen if (!file) { 6110ea8920eSIan Rogers close(fd); 612940db6dcSAndi Kleen continue; 613940db6dcSAndi Kleen } 614410136f5SStephane Eranian 615f26d22f1SIan Rogers if (perf_pmu__new_alias(pmu, dirfd, name, /*desc=*/ NULL, 616edb217ffSIan Rogers /*val=*/ NULL, file, /*pe=*/ NULL) < 0) 617940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 618a6146d50SZheng Yan fclose(file); 619a6146d50SZheng Yan } 620a6146d50SZheng Yan 621a6146d50SZheng Yan closedir(event_dir); 622940db6dcSAndi Kleen return 0; 623a6146d50SZheng Yan } 624a6146d50SZheng Yan 625a6146d50SZheng Yan /* 626a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 627a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 628a6146d50SZheng Yan */ 629838a8c5fSIan Rogers static int pmu_aliases(struct perf_pmu *pmu, int dirfd, const char *name) 630a6146d50SZheng Yan { 631e293a5e8SNamhyung Kim int fd; 632a6146d50SZheng Yan 633e293a5e8SNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY); 634e293a5e8SNamhyung Kim if (fd < 0) 635d9664582SAndi Kleen return 0; 636a6146d50SZheng Yan 637e293a5e8SNamhyung Kim /* it'll close the fd */ 638f26d22f1SIan Rogers if (pmu_aliases_parse(pmu, fd)) 639a6146d50SZheng Yan return -1; 640a6146d50SZheng Yan 641a6146d50SZheng Yan return 0; 642a6146d50SZheng Yan } 643a6146d50SZheng Yan 6445c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 645a6146d50SZheng Yan struct list_head *terms) 646a6146d50SZheng Yan { 6477c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 648a6146d50SZheng Yan LIST_HEAD(list); 649a6146d50SZheng Yan int ret; 650a6146d50SZheng Yan 651a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 6527c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 653a6146d50SZheng Yan if (ret) { 654682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 655a6146d50SZheng Yan return ret; 656a6146d50SZheng Yan } 657c2f1ceadSAndi Kleen /* 658c2f1ceadSAndi Kleen * Weak terms don't override command line options, 659c2f1ceadSAndi Kleen * which we don't want for implicit terms in aliases. 660c2f1ceadSAndi Kleen */ 661c2f1ceadSAndi Kleen cloned->weak = true; 6627c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 663a6146d50SZheng Yan } 664a6146d50SZheng Yan list_splice(&list, terms); 665a6146d50SZheng Yan return 0; 666a6146d50SZheng Yan } 667a6146d50SZheng Yan 66866ec1191SMark Rutland /* 66966ec1191SMark Rutland * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 67066ec1191SMark Rutland * may have a "cpus" file. 67166ec1191SMark Rutland */ 672d06593aaSIan Rogers static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core) 6737ae92e74SYan, Zheng { 674f854839bSJiri Olsa struct perf_cpu_map *cpus; 6757e3fcffeSMark Rutland const char *templates[] = { 676d50a79cdSJames Clark "cpumask", 677d50a79cdSJames Clark "cpus", 6787e3fcffeSMark Rutland NULL 6797e3fcffeSMark Rutland }; 6807e3fcffeSMark Rutland const char **template; 681d50a79cdSJames Clark char pmu_name[PATH_MAX]; 682d50a79cdSJames Clark struct perf_pmu pmu = {.name = pmu_name}; 683d50a79cdSJames Clark FILE *file; 6847ae92e74SYan, Zheng 685d50a79cdSJames Clark strlcpy(pmu_name, name, sizeof(pmu_name)); 6867e3fcffeSMark Rutland for (template = templates; *template; template++) { 6873a69672eSNamhyung Kim file = perf_pmu__open_file_at(&pmu, dirfd, *template); 688d50a79cdSJames Clark if (!file) 689d50a79cdSJames Clark continue; 690d50a79cdSJames Clark cpus = perf_cpu_map__read(file); 6913a69672eSNamhyung Kim fclose(file); 69266ec1191SMark Rutland if (cpus) 69366ec1191SMark Rutland return cpus; 6947e3fcffeSMark Rutland } 6957ae92e74SYan, Zheng 696d06593aaSIan Rogers /* Nothing found, for core PMUs assume this means all CPUs. */ 697d06593aaSIan Rogers return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL; 69866ec1191SMark Rutland } 6997ae92e74SYan, Zheng 7003a69672eSNamhyung Kim static bool pmu_is_uncore(int dirfd, const char *name) 70166ec1191SMark Rutland { 7023a69672eSNamhyung Kim int fd; 7037ae92e74SYan, Zheng 7043a69672eSNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH); 7053a69672eSNamhyung Kim if (fd < 0) 7063a69672eSNamhyung Kim return false; 7073a69672eSNamhyung Kim 7083a69672eSNamhyung Kim close(fd); 7093a69672eSNamhyung Kim return true; 7107ae92e74SYan, Zheng } 7117ae92e74SYan, Zheng 71251d54847SJohn Garry static char *pmu_id(const char *name) 71351d54847SJohn Garry { 71451d54847SJohn Garry char path[PATH_MAX], *str; 71551d54847SJohn Garry size_t len; 71651d54847SJohn Garry 7175f2c8efaSJames Clark perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier"); 71851d54847SJohn Garry 7195f2c8efaSJames Clark if (filename__read_str(path, &str, &len) < 0) 72051d54847SJohn Garry return NULL; 72151d54847SJohn Garry 72251d54847SJohn Garry str[len - 1] = 0; /* remove line feed */ 72351d54847SJohn Garry 72451d54847SJohn Garry return str; 72551d54847SJohn Garry } 72651d54847SJohn Garry 7274bf7e81aSIan Rogers /** 7284bf7e81aSIan Rogers * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in 7294bf7e81aSIan Rogers * sysfs on some platforms like ARM or Intel hybrid. Looking for 7304bf7e81aSIan Rogers * possible the cpus file in sysfs files to identify whether this is a 7314bf7e81aSIan Rogers * core device. 7324bf7e81aSIan Rogers * @name: The PMU name such as "cpu_atom". 73314b22ae0SGanapatrao Kulkarni */ 7344bf7e81aSIan Rogers static int is_sysfs_pmu_core(const char *name) 73514b22ae0SGanapatrao Kulkarni { 73614b22ae0SGanapatrao Kulkarni char path[PATH_MAX]; 73714b22ae0SGanapatrao Kulkarni 738f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus")) 73914b22ae0SGanapatrao Kulkarni return 0; 740d9664582SAndi Kleen return file_available(path); 74114b22ae0SGanapatrao Kulkarni } 74214b22ae0SGanapatrao Kulkarni 74329be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu) 744d77ade9fSAndi Kleen { 745d77ade9fSAndi Kleen char *cpuid; 746d77ade9fSAndi Kleen static bool printed; 747d77ade9fSAndi Kleen 748d77ade9fSAndi Kleen cpuid = getenv("PERF_CPUID"); 749d77ade9fSAndi Kleen if (cpuid) 750d77ade9fSAndi Kleen cpuid = strdup(cpuid); 751d77ade9fSAndi Kleen if (!cpuid) 75254e32dc0SGanapatrao Kulkarni cpuid = get_cpuid_str(pmu); 753d77ade9fSAndi Kleen if (!cpuid) 754d77ade9fSAndi Kleen return NULL; 755d77ade9fSAndi Kleen 756d77ade9fSAndi Kleen if (!printed) { 757d77ade9fSAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 758d77ade9fSAndi Kleen printed = true; 759d77ade9fSAndi Kleen } 760d77ade9fSAndi Kleen return cpuid; 761d77ade9fSAndi Kleen } 762d77ade9fSAndi Kleen 7631ba3752aSIan Rogers __weak const struct pmu_events_table *pmu_events_table__find(void) 764e126bef5SJohn Garry { 76596d2a746SIan Rogers return perf_pmu__find_events_table(NULL); 766e126bef5SJohn Garry } 767e126bef5SJohn Garry 768f8ea2c15SIan Rogers __weak const struct pmu_metrics_table *pmu_metrics_table__find(void) 769f8ea2c15SIan Rogers { 770f8ea2c15SIan Rogers return perf_pmu__find_metrics_table(NULL); 771f8ea2c15SIan Rogers } 772f8ea2c15SIan Rogers 773240e6fd0SIan Rogers /** 774240e6fd0SIan Rogers * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any 775240e6fd0SIan Rogers * trailing suffix? The Suffix must be in form 776240e6fd0SIan Rogers * tok_{digits}, or tok{digits}. 777240e6fd0SIan Rogers * @pmu_name: The pmu_name with possible suffix. 778240e6fd0SIan Rogers * @tok: The possible match to pmu_name without suffix. 779c07d5c92SJohn Garry */ 780240e6fd0SIan Rogers static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok) 781c47a5599SJin Yao { 782c07d5c92SJohn Garry const char *p; 783c47a5599SJin Yao 784c47a5599SJin Yao if (strncmp(pmu_name, tok, strlen(tok))) 785c47a5599SJin Yao return false; 786c47a5599SJin Yao 787c47a5599SJin Yao p = pmu_name + strlen(tok); 788c47a5599SJin Yao if (*p == 0) 789c47a5599SJin Yao return true; 790c47a5599SJin Yao 791c07d5c92SJohn Garry if (*p == '_') 792c47a5599SJin Yao ++p; 793c07d5c92SJohn Garry 794c07d5c92SJohn Garry /* Ensure we end in a number */ 795c07d5c92SJohn Garry while (1) { 796c07d5c92SJohn Garry if (!isdigit(*p)) 797c47a5599SJin Yao return false; 798c07d5c92SJohn Garry if (*(++p) == 0) 799c07d5c92SJohn Garry break; 800c07d5c92SJohn Garry } 801c47a5599SJin Yao 802c47a5599SJin Yao return true; 803c47a5599SJin Yao } 804c47a5599SJin Yao 805240e6fd0SIan Rogers /** 806240e6fd0SIan Rogers * pmu_uncore_alias_match - does name match the PMU name? 807240e6fd0SIan Rogers * @pmu_name: the json struct pmu_event name. This may lack a suffix (which 808240e6fd0SIan Rogers * matches) or be of the form "socket,pmuname" which will match 809240e6fd0SIan Rogers * "socketX_pmunameY". 810240e6fd0SIan Rogers * @name: a real full PMU name as from sysfs. 811240e6fd0SIan Rogers */ 812240e6fd0SIan Rogers static bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 813730670b1SJohn Garry { 814730670b1SJohn Garry char *tmp = NULL, *tok, *str; 815730670b1SJohn Garry bool res; 816730670b1SJohn Garry 817240e6fd0SIan Rogers if (strchr(pmu_name, ',') == NULL) 818240e6fd0SIan Rogers return perf_pmu__match_ignoring_suffix(name, pmu_name); 819240e6fd0SIan Rogers 820730670b1SJohn Garry str = strdup(pmu_name); 821730670b1SJohn Garry if (!str) 822730670b1SJohn Garry return false; 823730670b1SJohn Garry 824730670b1SJohn Garry /* 825730670b1SJohn Garry * uncore alias may be from different PMU with common prefix 826730670b1SJohn Garry */ 827730670b1SJohn Garry tok = strtok_r(str, ",", &tmp); 828730670b1SJohn Garry if (strncmp(pmu_name, tok, strlen(tok))) { 829730670b1SJohn Garry res = false; 830730670b1SJohn Garry goto out; 831730670b1SJohn Garry } 832730670b1SJohn Garry 833730670b1SJohn Garry /* 834730670b1SJohn Garry * Match more complex aliases where the alias name is a comma-delimited 835730670b1SJohn Garry * list of tokens, orderly contained in the matching PMU name. 836730670b1SJohn Garry * 837730670b1SJohn Garry * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 838730670b1SJohn Garry * match "socket" in "socketX_pmunameY" and then "pmuname" in 839730670b1SJohn Garry * "pmunameY". 840730670b1SJohn Garry */ 841c07d5c92SJohn Garry while (1) { 842c07d5c92SJohn Garry char *next_tok = strtok_r(NULL, ",", &tmp); 843c07d5c92SJohn Garry 844730670b1SJohn Garry name = strstr(name, tok); 845c07d5c92SJohn Garry if (!name || 846240e6fd0SIan Rogers (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) { 847730670b1SJohn Garry res = false; 848730670b1SJohn Garry goto out; 849730670b1SJohn Garry } 850c07d5c92SJohn Garry if (!next_tok) 851c07d5c92SJohn Garry break; 852c07d5c92SJohn Garry tok = next_tok; 853c07d5c92SJohn Garry name += strlen(tok); 854730670b1SJohn Garry } 855730670b1SJohn Garry 856730670b1SJohn Garry res = true; 857730670b1SJohn Garry out: 858730670b1SJohn Garry free(str); 859730670b1SJohn Garry return res; 860730670b1SJohn Garry } 861730670b1SJohn Garry 862660842e4SIan Rogers static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, 8631ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 864660842e4SIan Rogers void *vdata) 865660842e4SIan Rogers { 866f26d22f1SIan Rogers struct perf_pmu *pmu = vdata; 867660842e4SIan Rogers 868f26d22f1SIan Rogers perf_pmu__new_alias(pmu, -1, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL, pe); 869660842e4SIan Rogers return 0; 870660842e4SIan Rogers } 871660842e4SIan Rogers 872933f82ffSSukadev Bhattiprolu /* 873d685819bSIan Rogers * From the pmu_events_table, find the events that correspond to the given 874d685819bSIan Rogers * PMU and add them to the list 'head'. 875933f82ffSSukadev Bhattiprolu */ 876838a8c5fSIan Rogers void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table) 877933f82ffSSukadev Bhattiprolu { 878f26d22f1SIan Rogers pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu); 879933f82ffSSukadev Bhattiprolu } 880933f82ffSSukadev Bhattiprolu 881838a8c5fSIan Rogers static void pmu_add_cpu_aliases(struct perf_pmu *pmu) 882e45ad701SJohn Garry { 8837c52f10cSIan Rogers if (!pmu->events_table) 884e45ad701SJohn Garry return; 885e45ad701SJohn Garry 886e6ff1eedSIan Rogers if (pmu->cpu_aliases_added) 887e6ff1eedSIan Rogers return; 888e6ff1eedSIan Rogers 8897c52f10cSIan Rogers pmu_add_cpu_aliases_table(pmu, pmu->events_table); 890e6ff1eedSIan Rogers pmu->cpu_aliases_added = true; 891e45ad701SJohn Garry } 892e45ad701SJohn Garry 89329be2fe0SIan Rogers static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, 8941ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 895f26d22f1SIan Rogers void *vdata) 8964513c719SJohn Garry { 897f26d22f1SIan Rogers struct perf_pmu *pmu = vdata; 8984513c719SJohn Garry 8994513c719SJohn Garry if (!pe->compat || !pe->pmu) 9004513c719SJohn Garry return 0; 9014513c719SJohn Garry 9024513c719SJohn Garry if (!strcmp(pmu->id, pe->compat) && 9034513c719SJohn Garry pmu_uncore_alias_match(pe->pmu, pmu->name)) { 904f26d22f1SIan Rogers perf_pmu__new_alias(pmu, -1, 905edb217ffSIan Rogers pe->name, 906edb217ffSIan Rogers pe->desc, 907edb217ffSIan Rogers pe->event, 908edb217ffSIan Rogers /*val_fd=*/ NULL, 909eab35953SJin Yao pe); 9104513c719SJohn Garry } 9114513c719SJohn Garry 9124513c719SJohn Garry return 0; 9134513c719SJohn Garry } 9144513c719SJohn Garry 915838a8c5fSIan Rogers void pmu_add_sys_aliases(struct perf_pmu *pmu) 9164513c719SJohn Garry { 9174513c719SJohn Garry if (!pmu->id) 9184513c719SJohn Garry return; 9194513c719SJohn Garry 920f26d22f1SIan Rogers pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu); 9214513c719SJohn Garry } 9224513c719SJohn Garry 923c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 924dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 925dc0a6202SAdrian Hunter { 926dc0a6202SAdrian Hunter return NULL; 927dc0a6202SAdrian Hunter } 928dc0a6202SAdrian Hunter 92913d60ba0SKan Liang char * __weak 93013d60ba0SKan Liang pmu_find_real_name(const char *name) 93113d60ba0SKan Liang { 93213d60ba0SKan Liang return (char *)name; 93313d60ba0SKan Liang } 93413d60ba0SKan Liang 93513d60ba0SKan Liang char * __weak 93613d60ba0SKan Liang pmu_find_alias_name(const char *name __maybe_unused) 93713d60ba0SKan Liang { 93813d60ba0SKan Liang return NULL; 93913d60ba0SKan Liang } 94013d60ba0SKan Liang 9413a69672eSNamhyung Kim static int pmu_max_precise(int dirfd, struct perf_pmu *pmu) 94290a86bdeSJiri Olsa { 94390a86bdeSJiri Olsa int max_precise = -1; 94490a86bdeSJiri Olsa 9453a69672eSNamhyung Kim perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise); 94690a86bdeSJiri Olsa return max_precise; 94790a86bdeSJiri Olsa } 94890a86bdeSJiri Olsa 9491eaf496eSIan Rogers struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name) 950cd82a32eSJiri Olsa { 951cd82a32eSJiri Olsa struct perf_pmu *pmu; 952cd82a32eSJiri Olsa __u32 type; 95313d60ba0SKan Liang char *name = pmu_find_real_name(lookup_name); 95413d60ba0SKan Liang char *alias_name; 95549afa7f6SJin Yao 956cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 957cd82a32eSJiri Olsa if (!pmu) 958cd82a32eSJiri Olsa return NULL; 959cd82a32eSJiri Olsa 960f63a536fSIan Rogers pmu->name = strdup(name); 961f63a536fSIan Rogers if (!pmu->name) 962f63a536fSIan Rogers goto err; 963*88ed9184SIan Rogers 964*88ed9184SIan Rogers /* 965*88ed9184SIan Rogers * Read type early to fail fast if a lookup name isn't a PMU. Ensure 966*88ed9184SIan Rogers * that type value is successfully assigned (return 1). 967*88ed9184SIan Rogers */ 968*88ed9184SIan Rogers if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1) 969*88ed9184SIan Rogers goto err; 970*88ed9184SIan Rogers 971*88ed9184SIan Rogers INIT_LIST_HEAD(&pmu->format); 972*88ed9184SIan Rogers INIT_LIST_HEAD(&pmu->aliases); 973*88ed9184SIan Rogers INIT_LIST_HEAD(&pmu->caps); 974*88ed9184SIan Rogers 975838a8c5fSIan Rogers /* 976838a8c5fSIan Rogers * The pmu data we store & need consists of the pmu 977838a8c5fSIan Rogers * type value and format definitions. Load both right 978838a8c5fSIan Rogers * now. 979838a8c5fSIan Rogers */ 980838a8c5fSIan Rogers if (pmu_format(pmu, dirfd, name)) { 981838a8c5fSIan Rogers free(pmu); 982838a8c5fSIan Rogers return NULL; 983838a8c5fSIan Rogers } 984838a8c5fSIan Rogers /* 985838a8c5fSIan Rogers * Check the aliases first to avoid unnecessary work. 986838a8c5fSIan Rogers */ 987838a8c5fSIan Rogers if (pmu_aliases(pmu, dirfd, name)) { 988838a8c5fSIan Rogers free(pmu); 989838a8c5fSIan Rogers return NULL; 990838a8c5fSIan Rogers } 991d06593aaSIan Rogers pmu->is_core = is_pmu_core(name); 992d06593aaSIan Rogers pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core); 99313d60ba0SKan Liang 99413d60ba0SKan Liang alias_name = pmu_find_alias_name(name); 99513d60ba0SKan Liang if (alias_name) { 99613d60ba0SKan Liang pmu->alias_name = strdup(alias_name); 99713d60ba0SKan Liang if (!pmu->alias_name) 99813d60ba0SKan Liang goto err; 99913d60ba0SKan Liang } 100013d60ba0SKan Liang 100154e32dc0SGanapatrao Kulkarni pmu->type = type; 10023a69672eSNamhyung Kim pmu->is_uncore = pmu_is_uncore(dirfd, name); 100351d54847SJohn Garry if (pmu->is_uncore) 100451d54847SJohn Garry pmu->id = pmu_id(name); 10053a69672eSNamhyung Kim pmu->max_precise = pmu_max_precise(dirfd, pmu); 10067c52f10cSIan Rogers pmu->events_table = perf_pmu__find_events_table(pmu); 1007838a8c5fSIan Rogers pmu_add_sys_aliases(pmu); 10081eaf496eSIan Rogers list_add_tail(&pmu->list, pmus); 1009dc0a6202SAdrian Hunter 1010dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 1011dc0a6202SAdrian Hunter 1012cd82a32eSJiri Olsa return pmu; 101313d60ba0SKan Liang err: 1014efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->name); 101513d60ba0SKan Liang free(pmu); 101613d60ba0SKan Liang return NULL; 1017cd82a32eSJiri Olsa } 1018cd82a32eSJiri Olsa 1019628eaa4eSIan Rogers /* Creates the PMU when sysfs scanning fails. */ 1020628eaa4eSIan Rogers struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus) 1021628eaa4eSIan Rogers { 1022628eaa4eSIan Rogers struct perf_pmu *pmu = zalloc(sizeof(*pmu)); 1023628eaa4eSIan Rogers 1024628eaa4eSIan Rogers if (!pmu) 1025628eaa4eSIan Rogers return NULL; 1026628eaa4eSIan Rogers 1027628eaa4eSIan Rogers pmu->name = strdup("cpu"); 1028628eaa4eSIan Rogers if (!pmu->name) { 1029628eaa4eSIan Rogers free(pmu); 1030628eaa4eSIan Rogers return NULL; 1031628eaa4eSIan Rogers } 1032628eaa4eSIan Rogers 1033628eaa4eSIan Rogers pmu->is_core = true; 1034628eaa4eSIan Rogers pmu->type = PERF_TYPE_RAW; 1035628eaa4eSIan Rogers pmu->cpus = cpu_map__online(); 1036628eaa4eSIan Rogers 1037628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->format); 1038628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->aliases); 1039628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->caps); 1040628eaa4eSIan Rogers list_add_tail(&pmu->list, core_pmus); 1041628eaa4eSIan Rogers return pmu; 1042628eaa4eSIan Rogers } 1043628eaa4eSIan Rogers 1044e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) 1045e552b7beSRob Herring { 1046e552b7beSRob Herring struct perf_pmu_format *format; 1047e552b7beSRob Herring 104868c25043SIan Rogers if (pmu->formats_checked) 104968c25043SIan Rogers return; 105068c25043SIan Rogers 105168c25043SIan Rogers pmu->formats_checked = true; 105268c25043SIan Rogers 1053e552b7beSRob Herring /* fake pmu doesn't have format list */ 1054e552b7beSRob Herring if (pmu == &perf_pmu__fake) 1055e552b7beSRob Herring return; 1056e552b7beSRob Herring 105750402641SIan Rogers list_for_each_entry(format, &pmu->format, list) { 105850402641SIan Rogers perf_pmu_format__load(pmu, format); 1059e552b7beSRob Herring if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) { 1060e552b7beSRob Herring pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" 1061e552b7beSRob Herring "which is not supported by this version of perf!\n", 1062e552b7beSRob Herring pmu->name, format->name, format->value); 1063e552b7beSRob Herring return; 1064e552b7beSRob Herring } 1065e552b7beSRob Herring } 106650402641SIan Rogers } 1067e552b7beSRob Herring 1068c6d616feSIan Rogers bool evsel__is_aux_event(const struct evsel *evsel) 1069e12ee9f7SAdrian Hunter { 1070e76026bdSArnaldo Carvalho de Melo struct perf_pmu *pmu = evsel__find_pmu(evsel); 1071e12ee9f7SAdrian Hunter 1072e12ee9f7SAdrian Hunter return pmu && pmu->auxtrace; 1073e12ee9f7SAdrian Hunter } 1074e12ee9f7SAdrian Hunter 10756593f019SJames Clark /* 10766593f019SJames Clark * Set @config_name to @val as long as the user hasn't already set or cleared it 10776593f019SJames Clark * by passing a config term on the command line. 10786593f019SJames Clark * 10796593f019SJames Clark * @val is the value to put into the bits specified by @config_name rather than 10806593f019SJames Clark * the bit pattern. It is shifted into position by this function, so to set 10816593f019SJames Clark * something to true, pass 1 for val rather than a pre shifted value. 10826593f019SJames Clark */ 10836593f019SJames Clark #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask)) 10846593f019SJames Clark void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel, 10856593f019SJames Clark const char *config_name, u64 val) 10866593f019SJames Clark { 10876593f019SJames Clark u64 user_bits = 0, bits; 10886593f019SJames Clark struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG); 10896593f019SJames Clark 10906593f019SJames Clark if (term) 10916593f019SJames Clark user_bits = term->val.cfg_chg; 10926593f019SJames Clark 1093da6a5afdSIan Rogers bits = perf_pmu__format_bits(pmu, config_name); 10946593f019SJames Clark 10956593f019SJames Clark /* Do nothing if the user changed the value */ 10966593f019SJames Clark if (bits & user_bits) 10976593f019SJames Clark return; 10986593f019SJames Clark 10996593f019SJames Clark /* Otherwise replace it */ 11006593f019SJames Clark evsel->core.attr.config &= ~bits; 11016593f019SJames Clark evsel->core.attr.config |= field_prep(bits, val); 11026593f019SJames Clark } 11036593f019SJames Clark 11045c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 110509ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 1106cd82a32eSJiri Olsa { 11075c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1108cd82a32eSJiri Olsa 1109cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 1110cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 1111cd82a32eSJiri Olsa return format; 1112cd82a32eSJiri Olsa 1113cd82a32eSJiri Olsa return NULL; 1114cd82a32eSJiri Olsa } 1115cd82a32eSJiri Olsa 1116da6a5afdSIan Rogers __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name) 111709ff6071SAdrian Hunter { 1118da6a5afdSIan Rogers struct perf_pmu_format *format = pmu_find_format(&pmu->format, name); 111909ff6071SAdrian Hunter __u64 bits = 0; 112009ff6071SAdrian Hunter int fbit; 112109ff6071SAdrian Hunter 112209ff6071SAdrian Hunter if (!format) 112309ff6071SAdrian Hunter return 0; 112409ff6071SAdrian Hunter 112509ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 112609ff6071SAdrian Hunter bits |= 1ULL << fbit; 112709ff6071SAdrian Hunter 112809ff6071SAdrian Hunter return bits; 112909ff6071SAdrian Hunter } 113009ff6071SAdrian Hunter 11317eb54733SIan Rogers int perf_pmu__format_type(struct perf_pmu *pmu, const char *name) 1132a1ac7de6SAdrian Hunter { 11337eb54733SIan Rogers struct perf_pmu_format *format = pmu_find_format(&pmu->format, name); 1134a1ac7de6SAdrian Hunter 1135a1ac7de6SAdrian Hunter if (!format) 1136a1ac7de6SAdrian Hunter return -1; 1137a1ac7de6SAdrian Hunter 113850402641SIan Rogers perf_pmu_format__load(pmu, format); 1139a1ac7de6SAdrian Hunter return format->value; 1140a1ac7de6SAdrian Hunter } 1141a1ac7de6SAdrian Hunter 1142cd82a32eSJiri Olsa /* 1143dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 11444d39c89fSIngo Molnar * and unformatted value (value parameter). 1145cd82a32eSJiri Olsa */ 1146dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1147dc0a6202SAdrian Hunter bool zero) 1148cd82a32eSJiri Olsa { 1149cd82a32eSJiri Olsa unsigned long fbit, vbit; 1150cd82a32eSJiri Olsa 1151cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1152cd82a32eSJiri Olsa 1153cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 1154cd82a32eSJiri Olsa continue; 1155cd82a32eSJiri Olsa 1156dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 1157dc0a6202SAdrian Hunter *v |= (1llu << fbit); 1158dc0a6202SAdrian Hunter else if (zero) 1159dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 1160cd82a32eSJiri Olsa } 1161cd82a32eSJiri Olsa } 1162cd82a32eSJiri Olsa 11630efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 11640efe6b67SAdrian Hunter { 11651b9caa10SJiri Olsa int w; 11660efe6b67SAdrian Hunter 11671b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 11681b9caa10SJiri Olsa if (!w) 11691b9caa10SJiri Olsa return 0; 11701b9caa10SJiri Olsa if (w < 64) 11711b9caa10SJiri Olsa return (1ULL << w) - 1; 11721b9caa10SJiri Olsa return -1; 11730efe6b67SAdrian Hunter } 11740efe6b67SAdrian Hunter 1175cd82a32eSJiri Olsa /* 1176688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 1177688d4dfcSCody P Schafer * in the remaining terms. 1178688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 1179688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 1180688d4dfcSCody P Schafer * in a config string) later on in the term list. 1181688d4dfcSCody P Schafer */ 1182688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 1183688d4dfcSCody P Schafer struct list_head *head_terms, 1184688d4dfcSCody P Schafer __u64 *value) 1185688d4dfcSCody P Schafer { 1186688d4dfcSCody P Schafer struct parse_events_term *t; 1187688d4dfcSCody P Schafer 1188688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 11892a3d252dSIan Rogers if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 11902a3d252dSIan Rogers t->config && !strcmp(t->config, term->config)) { 1191688d4dfcSCody P Schafer t->used = true; 1192688d4dfcSCody P Schafer *value = t->val.num; 1193688d4dfcSCody P Schafer return 0; 1194688d4dfcSCody P Schafer } 1195688d4dfcSCody P Schafer } 1196688d4dfcSCody P Schafer 1197bb963e16SNamhyung Kim if (verbose > 0) 1198688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 1199688d4dfcSCody P Schafer 1200688d4dfcSCody P Schafer return -1; 1201688d4dfcSCody P Schafer } 1202688d4dfcSCody P Schafer 1203ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 1204e64b020bSJiri Olsa { 1205e64b020bSJiri Olsa struct perf_pmu_format *format; 120611db4e29SMasami Hiramatsu char *str = NULL; 120711db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1208f1417ceaSXin Gao unsigned int i = 0; 1209e64b020bSJiri Olsa 1210ffeb883eSHe Kuang if (!formats) 1211e64b020bSJiri Olsa return NULL; 1212e64b020bSJiri Olsa 1213e64b020bSJiri Olsa /* sysfs exported terms */ 1214ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 121511db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 121611db4e29SMasami Hiramatsu goto error; 1217e64b020bSJiri Olsa 1218ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 121911db4e29SMasami Hiramatsu error: 1220ffeb883eSHe Kuang strbuf_release(&buf); 1221e64b020bSJiri Olsa 1222e64b020bSJiri Olsa return str; 1223e64b020bSJiri Olsa } 1224e64b020bSJiri Olsa 1225688d4dfcSCody P Schafer /* 1226cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 122788aca8d9SCody P Schafer * user input data - term parameter. 1228cd82a32eSJiri Olsa */ 1229804fee5dSIan Rogers static int pmu_config_term(struct perf_pmu *pmu, 1230cd82a32eSJiri Olsa struct perf_event_attr *attr, 1231dc0a6202SAdrian Hunter struct parse_events_term *term, 1232688d4dfcSCody P Schafer struct list_head *head_terms, 1233e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1234cd82a32eSJiri Olsa { 12355c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1236cd82a32eSJiri Olsa __u64 *vp; 12370efe6b67SAdrian Hunter __u64 val, max_val; 1238cd82a32eSJiri Olsa 1239cd82a32eSJiri Olsa /* 1240688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1241688d4dfcSCody P Schafer * skip it in normal eval. 1242688d4dfcSCody P Schafer */ 1243688d4dfcSCody P Schafer if (term->used) 1244688d4dfcSCody P Schafer return 0; 1245688d4dfcSCody P Schafer 1246688d4dfcSCody P Schafer /* 1247cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1248cd82a32eSJiri Olsa * to be done for them. 1249cd82a32eSJiri Olsa */ 1250cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1251cd82a32eSJiri Olsa return 0; 1252cd82a32eSJiri Olsa 1253804fee5dSIan Rogers format = pmu_find_format(&pmu->format, term->config); 1254688d4dfcSCody P Schafer if (!format) { 1255804fee5dSIan Rogers char *pmu_term = pmu_formats_string(&pmu->format); 12564ac22b48SIan Rogers char *unknown_term; 12574ac22b48SIan Rogers char *help_msg; 1258ffeb883eSHe Kuang 12594ac22b48SIan Rogers if (asprintf(&unknown_term, 12604ac22b48SIan Rogers "unknown term '%s' for pmu '%s'", 1261804fee5dSIan Rogers term->config, pmu->name) < 0) 12624ac22b48SIan Rogers unknown_term = NULL; 12634ac22b48SIan Rogers help_msg = parse_events_formats_error_string(pmu_term); 12644ac22b48SIan Rogers if (err) { 12656c191289SIan Rogers parse_events_error__handle(err, term->err_term, 12664ac22b48SIan Rogers unknown_term, 12674ac22b48SIan Rogers help_msg); 12684ac22b48SIan Rogers } else { 12694ac22b48SIan Rogers pr_debug("%s (%s)\n", unknown_term, help_msg); 12704ac22b48SIan Rogers free(unknown_term); 1271e64b020bSJiri Olsa } 12724ac22b48SIan Rogers free(pmu_term); 1273cd82a32eSJiri Olsa return -EINVAL; 1274688d4dfcSCody P Schafer } 127550402641SIan Rogers perf_pmu_format__load(pmu, format); 1276cd82a32eSJiri Olsa switch (format->value) { 1277cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1278cd82a32eSJiri Olsa vp = &attr->config; 1279cd82a32eSJiri Olsa break; 1280cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1281cd82a32eSJiri Olsa vp = &attr->config1; 1282cd82a32eSJiri Olsa break; 1283cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1284cd82a32eSJiri Olsa vp = &attr->config2; 1285cd82a32eSJiri Olsa break; 1286204e7c49SRob Herring case PERF_PMU_FORMAT_VALUE_CONFIG3: 1287204e7c49SRob Herring vp = &attr->config3; 1288204e7c49SRob Herring break; 1289cd82a32eSJiri Olsa default: 1290cd82a32eSJiri Olsa return -EINVAL; 1291cd82a32eSJiri Olsa } 1292cd82a32eSJiri Olsa 129316fa7e82SJiri Olsa /* 1294688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1295688d4dfcSCody P Schafer * using event parameters. 129616fa7e82SJiri Olsa */ 129799e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 129899e7138eSJiri Olsa if (term->no_value && 129999e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 130099e7138eSJiri Olsa if (err) { 13016c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1302448d732cSIan Rogers strdup("no value assigned for term"), 1303448d732cSIan Rogers NULL); 130499e7138eSJiri Olsa } 130599e7138eSJiri Olsa return -EINVAL; 130699e7138eSJiri Olsa } 130799e7138eSJiri Olsa 1308688d4dfcSCody P Schafer val = term->val.num; 130999e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1310688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1311bb963e16SNamhyung Kim if (verbose > 0) { 1312688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1313688d4dfcSCody P Schafer term->config, term->val.str); 1314e64b020bSJiri Olsa } 1315e64b020bSJiri Olsa if (err) { 13166c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1317448d732cSIan Rogers strdup("expected numeric value"), 1318448d732cSIan Rogers NULL); 1319e64b020bSJiri Olsa } 1320688d4dfcSCody P Schafer return -EINVAL; 1321688d4dfcSCody P Schafer } 1322688d4dfcSCody P Schafer 1323688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1324688d4dfcSCody P Schafer return -EINVAL; 1325688d4dfcSCody P Schafer } else 1326688d4dfcSCody P Schafer return -EINVAL; 1327688d4dfcSCody P Schafer 13280efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 13290efe6b67SAdrian Hunter if (val > max_val) { 13300efe6b67SAdrian Hunter if (err) { 1331448d732cSIan Rogers char *err_str; 1332448d732cSIan Rogers 13336c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1334448d732cSIan Rogers asprintf(&err_str, 13350efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1336448d732cSIan Rogers (unsigned long long)max_val) < 0 1337448d732cSIan Rogers ? strdup("value too big for format") 1338448d732cSIan Rogers : err_str, 1339448d732cSIan Rogers NULL); 13400efe6b67SAdrian Hunter return -EINVAL; 13410efe6b67SAdrian Hunter } 13420efe6b67SAdrian Hunter /* 13430efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 13440efe6b67SAdrian Hunter * silently truncated. 13450efe6b67SAdrian Hunter */ 13460efe6b67SAdrian Hunter } 13470efe6b67SAdrian Hunter 1348688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1349cd82a32eSJiri Olsa return 0; 1350cd82a32eSJiri Olsa } 1351cd82a32eSJiri Olsa 1352804fee5dSIan Rogers int perf_pmu__config_terms(struct perf_pmu *pmu, 1353cff7f956SJiri Olsa struct perf_event_attr *attr, 1354dc0a6202SAdrian Hunter struct list_head *head_terms, 1355e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1356cd82a32eSJiri Olsa { 13576cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1358cd82a32eSJiri Olsa 1359688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 1360804fee5dSIan Rogers if (pmu_config_term(pmu, attr, term, head_terms, zero, err)) 1361cd82a32eSJiri Olsa return -EINVAL; 1362688d4dfcSCody P Schafer } 1363cd82a32eSJiri Olsa 1364cd82a32eSJiri Olsa return 0; 1365cd82a32eSJiri Olsa } 1366cd82a32eSJiri Olsa 1367cd82a32eSJiri Olsa /* 1368cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1369cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1370cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1371cd82a32eSJiri Olsa */ 1372cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1373e64b020bSJiri Olsa struct list_head *head_terms, 1374e64b020bSJiri Olsa struct parse_events_error *err) 1375cd82a32eSJiri Olsa { 1376dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1377dc0a6202SAdrian Hunter 1378804fee5dSIan Rogers return perf_pmu__config_terms(pmu, attr, head_terms, zero, err); 1379cd82a32eSJiri Olsa } 1380cd82a32eSJiri Olsa 13815c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 13826cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1383a6146d50SZheng Yan { 1384e6ff1eedSIan Rogers struct perf_pmu_alias *alias; 1385a6146d50SZheng Yan char *name; 1386a6146d50SZheng Yan 1387a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1388a6146d50SZheng Yan return NULL; 1389a6146d50SZheng Yan 1390a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1391a6146d50SZheng Yan if (term->val.num != 1) 1392a6146d50SZheng Yan return NULL; 1393a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1394a6146d50SZheng Yan return NULL; 1395a6146d50SZheng Yan name = term->config; 1396c3245d20SIan Rogers 1397a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1398a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1399a6146d50SZheng Yan return NULL; 1400a6146d50SZheng Yan name = term->val.str; 1401a6146d50SZheng Yan } else { 1402a6146d50SZheng Yan return NULL; 1403a6146d50SZheng Yan } 1404a6146d50SZheng Yan 1405e6ff1eedSIan Rogers alias = perf_pmu__find_alias(pmu, name); 1406e6ff1eedSIan Rogers if (alias || pmu->cpu_aliases_added) 1407e6ff1eedSIan Rogers return alias; 1408e6ff1eedSIan Rogers 1409e6ff1eedSIan Rogers /* Alias doesn't exist, try to get it from the json events. */ 1410e6ff1eedSIan Rogers if (pmu->events_table && 1411e6ff1eedSIan Rogers pmu_events_table__find_event(pmu->events_table, pmu, name, 1412e6ff1eedSIan Rogers pmu_add_cpu_aliases_map_callback, 1413e6ff1eedSIan Rogers pmu) == 0) { 1414e6ff1eedSIan Rogers alias = perf_pmu__find_alias(pmu, name); 1415e6ff1eedSIan Rogers } 1416e6ff1eedSIan Rogers return alias; 1417a6146d50SZheng Yan } 1418a6146d50SZheng Yan 1419410136f5SStephane Eranian 14201d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 14219d31cb93SIan Rogers struct perf_pmu_info *info, 14229d31cb93SIan Rogers struct parse_events_error *err, 14239d31cb93SIan Rogers int column) 1424410136f5SStephane Eranian { 1425410136f5SStephane Eranian /* 1426410136f5SStephane Eranian * Only one term in event definition can 14271d9e446bSJiri Olsa * define unit, scale and snapshot, fail 14281d9e446bSJiri Olsa * if there's more than one. 1429410136f5SStephane Eranian */ 14309d31cb93SIan Rogers if (info->unit && alias->unit[0]) { 14319d31cb93SIan Rogers parse_events_error__handle(err, column, 14329d31cb93SIan Rogers strdup("Attempt to set event's unit twice"), 14339d31cb93SIan Rogers NULL); 1434410136f5SStephane Eranian return -EINVAL; 14359d31cb93SIan Rogers } 14369d31cb93SIan Rogers if (info->scale && alias->scale) { 14379d31cb93SIan Rogers parse_events_error__handle(err, column, 14389d31cb93SIan Rogers strdup("Attempt to set event's scale twice"), 14399d31cb93SIan Rogers NULL); 14409d31cb93SIan Rogers return -EINVAL; 14419d31cb93SIan Rogers } 14429d31cb93SIan Rogers if (info->snapshot && alias->snapshot) { 14439d31cb93SIan Rogers parse_events_error__handle(err, column, 14449d31cb93SIan Rogers strdup("Attempt to set event snapshot twice"), 14459d31cb93SIan Rogers NULL); 14469d31cb93SIan Rogers return -EINVAL; 14479d31cb93SIan Rogers } 1448410136f5SStephane Eranian 1449b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 14501d9e446bSJiri Olsa info->unit = alias->unit; 1451410136f5SStephane Eranian 1452410136f5SStephane Eranian if (alias->scale) 14531d9e446bSJiri Olsa info->scale = alias->scale; 14541d9e446bSJiri Olsa 14551d9e446bSJiri Olsa if (alias->snapshot) 14561d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1457410136f5SStephane Eranian 1458410136f5SStephane Eranian return 0; 1459410136f5SStephane Eranian } 1460410136f5SStephane Eranian 1461a6146d50SZheng Yan /* 1462a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1463a6146d50SZheng Yan * defined for the alias 1464a6146d50SZheng Yan */ 1465410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 14669d31cb93SIan Rogers struct perf_pmu_info *info, struct parse_events_error *err) 1467a6146d50SZheng Yan { 14686cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 14695c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1470a6146d50SZheng Yan int ret; 1471a6146d50SZheng Yan 1472044330c1SMatt Fleming info->per_pkg = false; 1473044330c1SMatt Fleming 14748a398897SStephane Eranian /* 14758a398897SStephane Eranian * Mark unit and scale as not set 14768a398897SStephane Eranian * (different from default values, see below) 14778a398897SStephane Eranian */ 147846441bdcSMatt Fleming info->unit = NULL; 147946441bdcSMatt Fleming info->scale = 0.0; 14801d9e446bSJiri Olsa info->snapshot = false; 1481410136f5SStephane Eranian 1482a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1483a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1484a6146d50SZheng Yan if (!alias) 1485a6146d50SZheng Yan continue; 1486a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 14879d31cb93SIan Rogers if (ret) { 14889d31cb93SIan Rogers parse_events_error__handle(err, term->err_term, 14899d31cb93SIan Rogers strdup("Failure to duplicate terms"), 14909d31cb93SIan Rogers NULL); 1491a6146d50SZheng Yan return ret; 14929d31cb93SIan Rogers } 1493410136f5SStephane Eranian 14949d31cb93SIan Rogers ret = check_info_data(alias, info, err, term->err_term); 1495410136f5SStephane Eranian if (ret) 1496410136f5SStephane Eranian return ret; 1497410136f5SStephane Eranian 1498044330c1SMatt Fleming if (alias->per_pkg) 1499044330c1SMatt Fleming info->per_pkg = true; 1500044330c1SMatt Fleming 1501e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 15021dc92556SIan Rogers parse_events_term__delete(term); 1503a6146d50SZheng Yan } 15048a398897SStephane Eranian 15058a398897SStephane Eranian /* 15068a398897SStephane Eranian * if no unit or scale found in aliases, then 15078a398897SStephane Eranian * set defaults as for evsel 15088a398897SStephane Eranian * unit cannot left to NULL 15098a398897SStephane Eranian */ 151046441bdcSMatt Fleming if (info->unit == NULL) 151146441bdcSMatt Fleming info->unit = ""; 15128a398897SStephane Eranian 151346441bdcSMatt Fleming if (info->scale == 0.0) 151446441bdcSMatt Fleming info->scale = 1.0; 15158a398897SStephane Eranian 1516a6146d50SZheng Yan return 0; 1517a6146d50SZheng Yan } 1518a6146d50SZheng Yan 1519c3245d20SIan Rogers struct find_event_args { 1520c3245d20SIan Rogers const char *event; 1521c3245d20SIan Rogers void *state; 1522c3245d20SIan Rogers pmu_event_callback cb; 1523c3245d20SIan Rogers }; 1524c3245d20SIan Rogers 1525c3245d20SIan Rogers static int find_event_callback(void *state, struct pmu_event_info *info) 1526838a8c5fSIan Rogers { 1527c3245d20SIan Rogers struct find_event_args *args = state; 1528838a8c5fSIan Rogers 1529c3245d20SIan Rogers if (!strcmp(args->event, info->name)) 1530c3245d20SIan Rogers return args->cb(args->state, info); 1531838a8c5fSIan Rogers 1532c3245d20SIan Rogers return 0; 1533838a8c5fSIan Rogers } 1534c3245d20SIan Rogers 1535c3245d20SIan Rogers int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb) 1536c3245d20SIan Rogers { 1537c3245d20SIan Rogers struct find_event_args args = { 1538c3245d20SIan Rogers .event = event, 1539c3245d20SIan Rogers .state = state, 1540c3245d20SIan Rogers .cb = cb, 1541c3245d20SIan Rogers }; 1542c3245d20SIan Rogers 1543c3245d20SIan Rogers return perf_pmu__for_each_event(pmu, &args, find_event_callback); 1544c3245d20SIan Rogers } 1545c3245d20SIan Rogers 1546804fee5dSIan Rogers static void perf_pmu__del_formats(struct list_head *formats) 1547d26383dcSNamhyung Kim { 1548d26383dcSNamhyung Kim struct perf_pmu_format *fmt, *tmp; 1549d26383dcSNamhyung Kim 1550d26383dcSNamhyung Kim list_for_each_entry_safe(fmt, tmp, formats, list) { 1551d26383dcSNamhyung Kim list_del(&fmt->list); 1552efe98a7aSArnaldo Carvalho de Melo zfree(&fmt->name); 1553d26383dcSNamhyung Kim free(fmt); 1554d26383dcSNamhyung Kim } 1555d26383dcSNamhyung Kim } 1556d26383dcSNamhyung Kim 155707d2b820SIan Rogers bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name) 155807d2b820SIan Rogers { 155907d2b820SIan Rogers struct perf_pmu_format *format; 156007d2b820SIan Rogers 156107d2b820SIan Rogers list_for_each_entry(format, &pmu->format, list) { 156207d2b820SIan Rogers if (!strcmp(format->name, name)) 156307d2b820SIan Rogers return true; 156407d2b820SIan Rogers } 156507d2b820SIan Rogers return false; 156607d2b820SIan Rogers } 156707d2b820SIan Rogers 1568d504fae9SJohn Garry bool is_pmu_core(const char *name) 1569d504fae9SJohn Garry { 15706fbd67b0SThomas Richter return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name); 15714bf7e81aSIan Rogers } 15724bf7e81aSIan Rogers 15736fd1e519SIan Rogers bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu) 15746fd1e519SIan Rogers { 1575e20d1f2fSIan Rogers return pmu->is_core; 15766fd1e519SIan Rogers } 15776fd1e519SIan Rogers 157852c7b4d3SIan Rogers bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu) 157952c7b4d3SIan Rogers { 1580710dffc9SIan Rogers return !pmu->is_core || perf_pmus__num_core_pmus() == 1; 158152c7b4d3SIan Rogers } 158252c7b4d3SIan Rogers 1583e6ff1eedSIan Rogers bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name) 1584abe9544eSIan Rogers { 1585e6ff1eedSIan Rogers if (perf_pmu__find_alias(pmu, name) != NULL) 1586e6ff1eedSIan Rogers return true; 1587e6ff1eedSIan Rogers if (pmu->cpu_aliases_added || !pmu->events_table) 1588e6ff1eedSIan Rogers return false; 1589e6ff1eedSIan Rogers return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0; 15904cabc3d1SAndi Kleen } 1591c3245d20SIan Rogers 1592e6ff1eedSIan Rogers size_t perf_pmu__num_events(struct perf_pmu *pmu) 1593c3245d20SIan Rogers { 1594e6ff1eedSIan Rogers size_t nr = pmu->sysfs_aliases; 1595c3245d20SIan Rogers 1596e6ff1eedSIan Rogers if (pmu->cpu_aliases_added) 1597e6ff1eedSIan Rogers nr += pmu->loaded_json_aliases; 1598e6ff1eedSIan Rogers else if (pmu->events_table) 1599e6ff1eedSIan Rogers nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases; 1600c3245d20SIan Rogers 1601c3245d20SIan Rogers return pmu->selectable ? nr + 1 : nr; 1602c3245d20SIan Rogers } 1603c3245d20SIan Rogers 1604c3245d20SIan Rogers static int sub_non_neg(int a, int b) 1605c3245d20SIan Rogers { 1606c3245d20SIan Rogers if (b > a) 1607c3245d20SIan Rogers return 0; 1608c3245d20SIan Rogers return a - b; 1609c3245d20SIan Rogers } 1610c3245d20SIan Rogers 1611c3245d20SIan Rogers static char *format_alias(char *buf, int len, const struct perf_pmu *pmu, 1612c3245d20SIan Rogers const struct perf_pmu_alias *alias) 1613c3245d20SIan Rogers { 1614c3245d20SIan Rogers struct parse_events_term *term; 1615c3245d20SIan Rogers int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1616c3245d20SIan Rogers 1617c3245d20SIan Rogers list_for_each_entry(term, &alias->terms, list) { 1618c3245d20SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1619c3245d20SIan Rogers used += snprintf(buf + used, sub_non_neg(len, used), 1620c3245d20SIan Rogers ",%s=%s", term->config, 1621c3245d20SIan Rogers term->val.str); 1622c3245d20SIan Rogers } 1623c3245d20SIan Rogers 1624c3245d20SIan Rogers if (sub_non_neg(len, used) > 0) { 1625c3245d20SIan Rogers buf[used] = '/'; 1626c3245d20SIan Rogers used++; 1627c3245d20SIan Rogers } 1628c3245d20SIan Rogers if (sub_non_neg(len, used) > 0) { 1629c3245d20SIan Rogers buf[used] = '\0'; 1630c3245d20SIan Rogers used++; 1631c3245d20SIan Rogers } else 1632c3245d20SIan Rogers buf[len - 1] = '\0'; 1633c3245d20SIan Rogers 1634c3245d20SIan Rogers return buf; 1635c3245d20SIan Rogers } 1636c3245d20SIan Rogers 1637e6ff1eedSIan Rogers int perf_pmu__for_each_event(struct perf_pmu *pmu, void *state, pmu_event_callback cb) 1638c3245d20SIan Rogers { 1639c3245d20SIan Rogers char buf[1024]; 1640c3245d20SIan Rogers struct perf_pmu_alias *event; 1641c3245d20SIan Rogers struct pmu_event_info info = { 1642c3245d20SIan Rogers .pmu = pmu, 1643c3245d20SIan Rogers }; 1644c3245d20SIan Rogers int ret = 0; 1645c3245d20SIan Rogers 1646e6ff1eedSIan Rogers pmu_add_cpu_aliases(pmu); 1647c3245d20SIan Rogers list_for_each_entry(event, &pmu->aliases, list) { 1648c3245d20SIan Rogers size_t buf_used; 1649c3245d20SIan Rogers 1650c3245d20SIan Rogers info.pmu_name = event->pmu_name ?: pmu->name; 1651c3245d20SIan Rogers info.alias = NULL; 1652c3245d20SIan Rogers if (event->desc) { 1653c3245d20SIan Rogers info.name = event->name; 1654c3245d20SIan Rogers buf_used = 0; 1655c3245d20SIan Rogers } else { 1656c3245d20SIan Rogers info.name = format_alias(buf, sizeof(buf), pmu, event); 1657c3245d20SIan Rogers if (pmu->is_core) { 1658c3245d20SIan Rogers info.alias = info.name; 1659c3245d20SIan Rogers info.name = event->name; 1660c3245d20SIan Rogers } 1661c3245d20SIan Rogers buf_used = strlen(buf) + 1; 1662c3245d20SIan Rogers } 1663c3245d20SIan Rogers info.scale_unit = NULL; 1664c3245d20SIan Rogers if (strlen(event->unit) || event->scale != 1.0) { 1665c3245d20SIan Rogers info.scale_unit = buf + buf_used; 1666c3245d20SIan Rogers buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used, 1667c3245d20SIan Rogers "%G%s", event->scale, event->unit) + 1; 1668c3245d20SIan Rogers } 1669c3245d20SIan Rogers info.desc = event->desc; 1670c3245d20SIan Rogers info.long_desc = event->long_desc; 1671c3245d20SIan Rogers info.encoding_desc = buf + buf_used; 1672c3245d20SIan Rogers buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used, 1673c3245d20SIan Rogers "%s/%s/", info.pmu_name, event->str) + 1; 1674c3245d20SIan Rogers info.topic = event->topic; 1675c3245d20SIan Rogers info.str = event->str; 1676c3245d20SIan Rogers info.deprecated = event->deprecated; 1677c3245d20SIan Rogers ret = cb(state, &info); 1678c3245d20SIan Rogers if (ret) 1679c3245d20SIan Rogers return ret; 1680c3245d20SIan Rogers } 1681c3245d20SIan Rogers if (pmu->selectable) { 1682c3245d20SIan Rogers info.name = buf; 1683c3245d20SIan Rogers snprintf(buf, sizeof(buf), "%s//", pmu->name); 1684c3245d20SIan Rogers info.alias = NULL; 1685c3245d20SIan Rogers info.scale_unit = NULL; 1686c3245d20SIan Rogers info.desc = NULL; 1687c3245d20SIan Rogers info.long_desc = NULL; 1688c3245d20SIan Rogers info.encoding_desc = NULL; 1689c3245d20SIan Rogers info.topic = NULL; 1690c3245d20SIan Rogers info.pmu_name = pmu->name; 1691c3245d20SIan Rogers info.deprecated = false; 1692c3245d20SIan Rogers ret = cb(state, &info); 1693c3245d20SIan Rogers } 1694c3245d20SIan Rogers return ret; 16954cabc3d1SAndi Kleen } 16967d4bdab5SAdrian Hunter 1697e3edd6cfSIan Rogers bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name) 1698e3edd6cfSIan Rogers { 1699e3edd6cfSIan Rogers return !strcmp(pmu->name, pmu_name) || 1700e3edd6cfSIan Rogers (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)); 1701e3edd6cfSIan Rogers } 1702e3edd6cfSIan Rogers 1703251aa040SIan Rogers bool perf_pmu__is_software(const struct perf_pmu *pmu) 1704251aa040SIan Rogers { 1705251aa040SIan Rogers if (pmu->is_core || pmu->is_uncore || pmu->auxtrace) 1706251aa040SIan Rogers return false; 1707251aa040SIan Rogers switch (pmu->type) { 1708251aa040SIan Rogers case PERF_TYPE_HARDWARE: return false; 1709251aa040SIan Rogers case PERF_TYPE_SOFTWARE: return true; 1710251aa040SIan Rogers case PERF_TYPE_TRACEPOINT: return true; 1711251aa040SIan Rogers case PERF_TYPE_HW_CACHE: return false; 1712251aa040SIan Rogers case PERF_TYPE_RAW: return false; 1713251aa040SIan Rogers case PERF_TYPE_BREAKPOINT: return true; 1714251aa040SIan Rogers default: break; 1715251aa040SIan Rogers } 1716251aa040SIan Rogers return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe"); 1717251aa040SIan Rogers } 1718251aa040SIan Rogers 1719d50a79cdSJames Clark FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 17207d4bdab5SAdrian Hunter { 17217d4bdab5SAdrian Hunter char path[PATH_MAX]; 17227d4bdab5SAdrian Hunter 1723f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) || 1724f8ad6018SJames Clark !file_available(path)) 17257d4bdab5SAdrian Hunter return NULL; 17267d4bdab5SAdrian Hunter 17277d4bdab5SAdrian Hunter return fopen(path, "r"); 17287d4bdab5SAdrian Hunter } 17297d4bdab5SAdrian Hunter 17303a69672eSNamhyung Kim FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name) 17313a69672eSNamhyung Kim { 17323a69672eSNamhyung Kim int fd; 17333a69672eSNamhyung Kim 17343a69672eSNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY); 17353a69672eSNamhyung Kim if (fd < 0) 17363a69672eSNamhyung Kim return NULL; 17373a69672eSNamhyung Kim 17383a69672eSNamhyung Kim return fdopen(fd, "r"); 17393a69672eSNamhyung Kim } 17403a69672eSNamhyung Kim 17417d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 17427d4bdab5SAdrian Hunter ...) 17437d4bdab5SAdrian Hunter { 17447d4bdab5SAdrian Hunter va_list args; 17457d4bdab5SAdrian Hunter FILE *file; 17467d4bdab5SAdrian Hunter int ret = EOF; 17477d4bdab5SAdrian Hunter 17487d4bdab5SAdrian Hunter va_start(args, fmt); 17497d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 17507d4bdab5SAdrian Hunter if (file) { 17517d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 17527d4bdab5SAdrian Hunter fclose(file); 17537d4bdab5SAdrian Hunter } 17547d4bdab5SAdrian Hunter va_end(args); 17557d4bdab5SAdrian Hunter return ret; 17567d4bdab5SAdrian Hunter } 17579fbc61f8SKan Liang 17583a69672eSNamhyung Kim int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name, 17593a69672eSNamhyung Kim const char *fmt, ...) 17603a69672eSNamhyung Kim { 17613a69672eSNamhyung Kim va_list args; 17623a69672eSNamhyung Kim FILE *file; 17633a69672eSNamhyung Kim int ret = EOF; 17643a69672eSNamhyung Kim 17653a69672eSNamhyung Kim va_start(args, fmt); 17663a69672eSNamhyung Kim file = perf_pmu__open_file_at(pmu, dirfd, name); 17673a69672eSNamhyung Kim if (file) { 17683a69672eSNamhyung Kim ret = vfscanf(file, fmt, args); 17693a69672eSNamhyung Kim fclose(file); 17703a69672eSNamhyung Kim } 17713a69672eSNamhyung Kim va_end(args); 17723a69672eSNamhyung Kim return ret; 17733a69672eSNamhyung Kim } 17743a69672eSNamhyung Kim 1775c2b6a896SGerman Gomez bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name) 1776c2b6a896SGerman Gomez { 1777c2b6a896SGerman Gomez char path[PATH_MAX]; 1778c2b6a896SGerman Gomez 1779c2b6a896SGerman Gomez if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name)) 1780c2b6a896SGerman Gomez return false; 1781c2b6a896SGerman Gomez 1782c2b6a896SGerman Gomez return file_available(path); 1783c2b6a896SGerman Gomez } 1784c2b6a896SGerman Gomez 17859fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 17869fbc61f8SKan Liang { 17879fbc61f8SKan Liang struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 17889fbc61f8SKan Liang 17899fbc61f8SKan Liang if (!caps) 17909fbc61f8SKan Liang return -ENOMEM; 17919fbc61f8SKan Liang 17929fbc61f8SKan Liang caps->name = strdup(name); 17939fbc61f8SKan Liang if (!caps->name) 17949fbc61f8SKan Liang goto free_caps; 17959fbc61f8SKan Liang caps->value = strndup(value, strlen(value) - 1); 17969fbc61f8SKan Liang if (!caps->value) 17979fbc61f8SKan Liang goto free_name; 17989fbc61f8SKan Liang list_add_tail(&caps->list, list); 17999fbc61f8SKan Liang return 0; 18009fbc61f8SKan Liang 18019fbc61f8SKan Liang free_name: 180257f14b5aSArnaldo Carvalho de Melo zfree(&caps->name); 18039fbc61f8SKan Liang free_caps: 18049fbc61f8SKan Liang free(caps); 18059fbc61f8SKan Liang 18069fbc61f8SKan Liang return -ENOMEM; 18079fbc61f8SKan Liang } 18089fbc61f8SKan Liang 1809eec11310SNamhyung Kim static void perf_pmu__del_caps(struct perf_pmu *pmu) 1810eec11310SNamhyung Kim { 1811eec11310SNamhyung Kim struct perf_pmu_caps *caps, *tmp; 1812eec11310SNamhyung Kim 1813eec11310SNamhyung Kim list_for_each_entry_safe(caps, tmp, &pmu->caps, list) { 1814eec11310SNamhyung Kim list_del(&caps->list); 1815efe98a7aSArnaldo Carvalho de Melo zfree(&caps->name); 1816efe98a7aSArnaldo Carvalho de Melo zfree(&caps->value); 1817eec11310SNamhyung Kim free(caps); 1818eec11310SNamhyung Kim } 1819eec11310SNamhyung Kim } 1820eec11310SNamhyung Kim 18219fbc61f8SKan Liang /* 18229fbc61f8SKan Liang * Reading/parsing the given pmu capabilities, which should be located at: 18239fbc61f8SKan Liang * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 18249fbc61f8SKan Liang * Return the number of capabilities 18259fbc61f8SKan Liang */ 18269fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu) 18279fbc61f8SKan Liang { 18289fbc61f8SKan Liang struct stat st; 18299fbc61f8SKan Liang char caps_path[PATH_MAX]; 18309fbc61f8SKan Liang DIR *caps_dir; 18319fbc61f8SKan Liang struct dirent *evt_ent; 1832b39094d3SNamhyung Kim int caps_fd; 18333339ec44SRavi Bangoria 18343339ec44SRavi Bangoria if (pmu->caps_initialized) 18353339ec44SRavi Bangoria return pmu->nr_caps; 18363339ec44SRavi Bangoria 18373339ec44SRavi Bangoria pmu->nr_caps = 0; 18389fbc61f8SKan Liang 1839f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps")) 18409fbc61f8SKan Liang return -1; 18419fbc61f8SKan Liang 18423339ec44SRavi Bangoria if (stat(caps_path, &st) < 0) { 18433339ec44SRavi Bangoria pmu->caps_initialized = true; 18449fbc61f8SKan Liang return 0; /* no error if caps does not exist */ 18453339ec44SRavi Bangoria } 18469fbc61f8SKan Liang 18479fbc61f8SKan Liang caps_dir = opendir(caps_path); 18489fbc61f8SKan Liang if (!caps_dir) 18499fbc61f8SKan Liang return -EINVAL; 18509fbc61f8SKan Liang 1851b39094d3SNamhyung Kim caps_fd = dirfd(caps_dir); 1852b39094d3SNamhyung Kim 18539fbc61f8SKan Liang while ((evt_ent = readdir(caps_dir)) != NULL) { 18549fbc61f8SKan Liang char *name = evt_ent->d_name; 18559fbc61f8SKan Liang char value[128]; 18569fbc61f8SKan Liang FILE *file; 1857b39094d3SNamhyung Kim int fd; 18589fbc61f8SKan Liang 18599fbc61f8SKan Liang if (!strcmp(name, ".") || !strcmp(name, "..")) 18609fbc61f8SKan Liang continue; 18619fbc61f8SKan Liang 1862b39094d3SNamhyung Kim fd = openat(caps_fd, name, O_RDONLY); 18630ea8920eSIan Rogers if (fd == -1) 18649fbc61f8SKan Liang continue; 18650ea8920eSIan Rogers file = fdopen(fd, "r"); 18660ea8920eSIan Rogers if (!file) { 18670ea8920eSIan Rogers close(fd); 18680ea8920eSIan Rogers continue; 18690ea8920eSIan Rogers } 18709fbc61f8SKan Liang 18719fbc61f8SKan Liang if (!fgets(value, sizeof(value), file) || 18729fbc61f8SKan Liang (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 18739fbc61f8SKan Liang fclose(file); 18749fbc61f8SKan Liang continue; 18759fbc61f8SKan Liang } 18769fbc61f8SKan Liang 18773339ec44SRavi Bangoria pmu->nr_caps++; 18789fbc61f8SKan Liang fclose(file); 18799fbc61f8SKan Liang } 18809fbc61f8SKan Liang 18819fbc61f8SKan Liang closedir(caps_dir); 18829fbc61f8SKan Liang 18833339ec44SRavi Bangoria pmu->caps_initialized = true; 18843339ec44SRavi Bangoria return pmu->nr_caps; 18859fbc61f8SKan Liang } 1886e4064776SJin Yao 1887b9f01032SIan Rogers static void perf_pmu__compute_config_masks(struct perf_pmu *pmu) 1888e4064776SJin Yao { 1889e4064776SJin Yao struct perf_pmu_format *format; 1890b9f01032SIan Rogers 1891b9f01032SIan Rogers if (pmu->config_masks_computed) 1892b9f01032SIan Rogers return; 1893e4064776SJin Yao 1894e4064776SJin Yao list_for_each_entry(format, &pmu->format, list) { 1895b9f01032SIan Rogers unsigned int i; 1896b9f01032SIan Rogers __u64 *mask; 1897b9f01032SIan Rogers 1898b9f01032SIan Rogers if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) 1899e4064776SJin Yao continue; 1900e4064776SJin Yao 1901b9f01032SIan Rogers pmu->config_masks_present = true; 1902b9f01032SIan Rogers mask = &pmu->config_masks[format->value]; 1903b9f01032SIan Rogers 1904e4064776SJin Yao for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1905b9f01032SIan Rogers *mask |= 1ULL << i; 1906e4064776SJin Yao } 1907b9f01032SIan Rogers pmu->config_masks_computed = true; 1908b9f01032SIan Rogers } 1909b9f01032SIan Rogers 1910b9f01032SIan Rogers void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 1911b9f01032SIan Rogers const char *name, int config_num, 1912b9f01032SIan Rogers const char *config_name) 1913b9f01032SIan Rogers { 1914b9f01032SIan Rogers __u64 bits; 1915b9f01032SIan Rogers char buf[100]; 1916b9f01032SIan Rogers 1917b9f01032SIan Rogers perf_pmu__compute_config_masks(pmu); 1918e4064776SJin Yao 1919e4064776SJin Yao /* 1920e4064776SJin Yao * Kernel doesn't export any valid format bits. 1921e4064776SJin Yao */ 1922b9f01032SIan Rogers if (!pmu->config_masks_present) 1923e4064776SJin Yao return; 1924e4064776SJin Yao 1925b9f01032SIan Rogers bits = config & ~pmu->config_masks[config_num]; 1926e4064776SJin Yao if (bits == 0) 1927e4064776SJin Yao return; 1928e4064776SJin Yao 1929e4064776SJin Yao bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1930e4064776SJin Yao 1931b9f01032SIan Rogers pr_warning("WARNING: event '%s' not valid (bits %s of %s " 1932e4064776SJin Yao "'%llx' not supported by kernel)!\n", 1933b9f01032SIan Rogers name ?: "N/A", buf, config_name, config); 1934e4064776SJin Yao } 1935c5a26ea4SJin Yao 1936c47a5599SJin Yao int perf_pmu__match(char *pattern, char *name, char *tok) 1937c47a5599SJin Yao { 193813d60ba0SKan Liang if (!name) 193913d60ba0SKan Liang return -1; 194013d60ba0SKan Liang 1941c47a5599SJin Yao if (fnmatch(pattern, name, 0)) 1942c47a5599SJin Yao return -1; 1943c47a5599SJin Yao 1944240e6fd0SIan Rogers if (tok && !perf_pmu__match_ignoring_suffix(name, tok)) 1945c47a5599SJin Yao return -1; 1946c47a5599SJin Yao 1947c47a5599SJin Yao return 0; 1948c47a5599SJin Yao } 19491d3351e6SJin Yao 1950acef233bSJing Zhang double __weak perf_pmu__cpu_slots_per_cycle(void) 1951acef233bSJing Zhang { 1952acef233bSJing Zhang return NAN; 1953acef233bSJing Zhang } 1954f8ad6018SJames Clark 1955f8ad6018SJames Clark int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size) 1956f8ad6018SJames Clark { 1957f8ad6018SJames Clark const char *sysfs = sysfs__mountpoint(); 1958f8ad6018SJames Clark 1959f8ad6018SJames Clark if (!sysfs) 1960f8ad6018SJames Clark return 0; 1961f8ad6018SJames Clark return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs); 1962f8ad6018SJames Clark } 1963f8ad6018SJames Clark 1964e293a5e8SNamhyung Kim int perf_pmu__event_source_devices_fd(void) 1965e293a5e8SNamhyung Kim { 1966e293a5e8SNamhyung Kim char path[PATH_MAX]; 1967e293a5e8SNamhyung Kim const char *sysfs = sysfs__mountpoint(); 1968e293a5e8SNamhyung Kim 1969e293a5e8SNamhyung Kim if (!sysfs) 1970e293a5e8SNamhyung Kim return -1; 1971e293a5e8SNamhyung Kim 1972e293a5e8SNamhyung Kim scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs); 1973e293a5e8SNamhyung Kim return open(path, O_DIRECTORY); 1974e293a5e8SNamhyung Kim } 1975e293a5e8SNamhyung Kim 1976f8ad6018SJames Clark /* 1977f8ad6018SJames Clark * Fill 'buf' with the path to a file or folder in 'pmu_name' in 1978f8ad6018SJames Clark * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format" 1979f8ad6018SJames Clark * then pathname will be filled with 1980f8ad6018SJames Clark * "/sys/bus/event_source/devices/cs_etm/format" 1981f8ad6018SJames Clark * 1982e1a3aad3SIan Rogers * Return 0 if the sysfs mountpoint couldn't be found, if no characters were 1983e1a3aad3SIan Rogers * written or if the buffer size is exceeded. 1984f8ad6018SJames Clark */ 1985f8ad6018SJames Clark int perf_pmu__pathname_scnprintf(char *buf, size_t size, 1986f8ad6018SJames Clark const char *pmu_name, const char *filename) 1987f8ad6018SJames Clark { 1988e1a3aad3SIan Rogers size_t len; 1989f8ad6018SJames Clark 1990e1a3aad3SIan Rogers len = perf_pmu__event_source_devices_scnprintf(buf, size); 1991e1a3aad3SIan Rogers if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size) 1992f8ad6018SJames Clark return 0; 1993e1a3aad3SIan Rogers 1994e1a3aad3SIan Rogers return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename); 1995f8ad6018SJames Clark } 1996eec11310SNamhyung Kim 1997e293a5e8SNamhyung Kim int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags) 1998e293a5e8SNamhyung Kim { 1999e293a5e8SNamhyung Kim char path[PATH_MAX]; 2000e293a5e8SNamhyung Kim 2001e293a5e8SNamhyung Kim scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename); 2002e293a5e8SNamhyung Kim return openat(dirfd, path, flags); 2003e293a5e8SNamhyung Kim } 2004e293a5e8SNamhyung Kim 20051eaf496eSIan Rogers void perf_pmu__delete(struct perf_pmu *pmu) 2006eec11310SNamhyung Kim { 2007eec11310SNamhyung Kim perf_pmu__del_formats(&pmu->format); 2008eec11310SNamhyung Kim perf_pmu__del_aliases(pmu); 2009eec11310SNamhyung Kim perf_pmu__del_caps(pmu); 2010eec11310SNamhyung Kim 2011eec11310SNamhyung Kim perf_cpu_map__put(pmu->cpus); 2012eec11310SNamhyung Kim 2013efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->default_config); 2014efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->name); 2015efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->alias_name); 2016eec11310SNamhyung Kim free(pmu); 2017eec11310SNamhyung Kim } 20189d5da30eSJames Clark 20199d5da30eSJames Clark struct perf_pmu *pmu__find_core_pmu(void) 20209d5da30eSJames Clark { 20219d5da30eSJames Clark struct perf_pmu *pmu = NULL; 20229d5da30eSJames Clark 20239d5da30eSJames Clark while ((pmu = perf_pmus__scan_core(pmu))) { 20249d5da30eSJames Clark /* 20259d5da30eSJames Clark * The cpumap should cover all CPUs. Otherwise, some CPUs may 20269d5da30eSJames Clark * not support some events or have different event IDs. 20279d5da30eSJames Clark */ 20289d5da30eSJames Clark if (RC_CHK_ACCESS(pmu->cpus)->nr != cpu__max_cpu().cpu) 20299d5da30eSJames Clark return NULL; 20309d5da30eSJames Clark 20319d5da30eSJames Clark return pmu; 20329d5da30eSJames Clark } 20339d5da30eSJames Clark return NULL; 20349d5da30eSJames Clark } 2035