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" 312879ff36SJing Zhang #include <regex.h> 32cd82a32eSJiri Olsa 33c091ee90SIan Rogers struct perf_pmu perf_pmu__fake = { 34c091ee90SIan Rogers .name = "fake", 35c091ee90SIan Rogers }; 36e46fc8d9SArnaldo Carvalho de Melo 37c3245d20SIan Rogers #define UNIT_MAX_LEN 31 /* max length for event unit name */ 38c3245d20SIan Rogers 39c3245d20SIan Rogers /** 40c3245d20SIan Rogers * struct perf_pmu_alias - An event either read from sysfs or builtin in 41c3245d20SIan Rogers * pmu-events.c, created by parsing the pmu-events json files. 42c3245d20SIan Rogers */ 43c3245d20SIan Rogers struct perf_pmu_alias { 44c3245d20SIan Rogers /** @name: Name of the event like "mem-loads". */ 45c3245d20SIan Rogers char *name; 46c3245d20SIan Rogers /** @desc: Optional short description of the event. */ 47c3245d20SIan Rogers char *desc; 48c3245d20SIan Rogers /** @long_desc: Optional long description. */ 49c3245d20SIan Rogers char *long_desc; 50c3245d20SIan Rogers /** 51c3245d20SIan Rogers * @topic: Optional topic such as cache or pipeline, particularly for 52c3245d20SIan Rogers * json events. 53c3245d20SIan Rogers */ 54c3245d20SIan Rogers char *topic; 55c3245d20SIan Rogers /** @terms: Owned list of the original parsed parameters. */ 560d3f0e6fSIan Rogers struct parse_events_terms terms; 57c3245d20SIan Rogers /** @list: List element of struct perf_pmu aliases. */ 58c3245d20SIan Rogers struct list_head list; 597b723dbbSIan Rogers /** 607b723dbbSIan Rogers * @pmu_name: The name copied from the json struct pmu_event. This can 617b723dbbSIan Rogers * differ from the PMU name as it won't have suffixes. 627b723dbbSIan Rogers */ 637b723dbbSIan Rogers char *pmu_name; 64c3245d20SIan Rogers /** @unit: Units for the event, such as bytes or cache lines. */ 65c3245d20SIan Rogers char unit[UNIT_MAX_LEN+1]; 66c3245d20SIan Rogers /** @scale: Value to scale read counter values by. */ 67c3245d20SIan Rogers double scale; 68c3245d20SIan Rogers /** 69c3245d20SIan Rogers * @per_pkg: Does the file 70c3245d20SIan Rogers * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.per-pkg or 71c3245d20SIan Rogers * equivalent json value exist and have the value 1. 72c3245d20SIan Rogers */ 73c3245d20SIan Rogers bool per_pkg; 74c3245d20SIan Rogers /** 75c3245d20SIan Rogers * @snapshot: Does the file 76c3245d20SIan Rogers * <sysfs>/bus/event_source/devices/<pmu_name>/events/<name>.snapshot 77c3245d20SIan Rogers * exist and have the value 1. 78c3245d20SIan Rogers */ 79c3245d20SIan Rogers bool snapshot; 80c3245d20SIan Rogers /** 81c3245d20SIan Rogers * @deprecated: Is the event hidden and so not shown in perf list by 82c3245d20SIan Rogers * default. 83c3245d20SIan Rogers */ 84c3245d20SIan Rogers bool deprecated; 857b723dbbSIan Rogers /** @from_sysfs: Was the alias from sysfs or a json event? */ 867b723dbbSIan Rogers bool from_sysfs; 877b723dbbSIan Rogers /** @info_loaded: Have the scale, unit and other values been read from disk? */ 887b723dbbSIan Rogers bool info_loaded; 89c3245d20SIan Rogers }; 90c3245d20SIan Rogers 91fe13d43dSIan Rogers /** 92fe13d43dSIan Rogers * struct perf_pmu_format - Values from a format file read from 93fe13d43dSIan Rogers * <sysfs>/devices/cpu/format/ held in struct perf_pmu. 94fe13d43dSIan Rogers * 95fe13d43dSIan Rogers * For example, the contents of <sysfs>/devices/cpu/format/event may be 96fe13d43dSIan Rogers * "config:0-7" and will be represented here as name="event", 97fe13d43dSIan Rogers * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. 98fe13d43dSIan Rogers */ 99ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 10050402641SIan Rogers /** @list: Element on list within struct perf_pmu. */ 10150402641SIan Rogers struct list_head list; 10250402641SIan Rogers /** @bits: Which config bits are set by this format value. */ 10350402641SIan Rogers DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 104fe13d43dSIan Rogers /** @name: The modifier/file name. */ 105ab1bf653SArnaldo Carvalho de Melo char *name; 106fe13d43dSIan Rogers /** 107fe13d43dSIan Rogers * @value : Which config value the format relates to. Supported values 108fe13d43dSIan Rogers * are from PERF_PMU_FORMAT_VALUE_CONFIG to 109fe13d43dSIan Rogers * PERF_PMU_FORMAT_VALUE_CONFIG_END. 110fe13d43dSIan Rogers */ 11150402641SIan Rogers u16 value; 11250402641SIan Rogers /** @loaded: Has the contents been loaded/parsed. */ 11350402641SIan Rogers bool loaded; 114ab1bf653SArnaldo Carvalho de Melo }; 115ab1bf653SArnaldo Carvalho de Melo 1168d4b6d37SIan Rogers static int pmu_aliases_parse(struct perf_pmu *pmu); 1178d4b6d37SIan Rogers 11850402641SIan Rogers static struct perf_pmu_format *perf_pmu__new_format(struct list_head *list, char *name) 11950402641SIan Rogers { 12050402641SIan Rogers struct perf_pmu_format *format; 12150402641SIan Rogers 12250402641SIan Rogers format = zalloc(sizeof(*format)); 12350402641SIan Rogers if (!format) 12450402641SIan Rogers return NULL; 12550402641SIan Rogers 12650402641SIan Rogers format->name = strdup(name); 12750402641SIan Rogers if (!format->name) { 12850402641SIan Rogers free(format); 12950402641SIan Rogers return NULL; 13050402641SIan Rogers } 13150402641SIan Rogers list_add_tail(&format->list, list); 13250402641SIan Rogers return format; 13350402641SIan Rogers } 13450402641SIan Rogers 13550402641SIan Rogers /* Called at the end of parsing a format. */ 13650402641SIan Rogers void perf_pmu_format__set_value(void *vformat, int config, unsigned long *bits) 13750402641SIan Rogers { 13850402641SIan Rogers struct perf_pmu_format *format = vformat; 13950402641SIan Rogers 14050402641SIan Rogers format->value = config; 14150402641SIan Rogers memcpy(format->bits, bits, sizeof(format->bits)); 14250402641SIan Rogers } 14350402641SIan Rogers 14450402641SIan Rogers static void __perf_pmu_format__load(struct perf_pmu_format *format, FILE *file) 14550402641SIan Rogers { 14650402641SIan Rogers void *scanner; 14750402641SIan Rogers int ret; 14850402641SIan Rogers 14950402641SIan Rogers ret = perf_pmu_lex_init(&scanner); 15050402641SIan Rogers if (ret) 15150402641SIan Rogers return; 15250402641SIan Rogers 15350402641SIan Rogers perf_pmu_set_in(file, scanner); 15450402641SIan Rogers ret = perf_pmu_parse(format, scanner); 15550402641SIan Rogers perf_pmu_lex_destroy(scanner); 15650402641SIan Rogers format->loaded = true; 15750402641SIan Rogers } 15850402641SIan Rogers 15963883cb0SIan Rogers static void perf_pmu_format__load(const struct perf_pmu *pmu, struct perf_pmu_format *format) 16050402641SIan Rogers { 16150402641SIan Rogers char path[PATH_MAX]; 16250402641SIan Rogers FILE *file = NULL; 16350402641SIan Rogers 16450402641SIan Rogers if (format->loaded) 16550402641SIan Rogers return; 16650402641SIan Rogers 16750402641SIan Rogers if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, "format")) 16850402641SIan Rogers return; 16950402641SIan Rogers 17050402641SIan Rogers assert(strlen(path) + strlen(format->name) + 2 < sizeof(path)); 17150402641SIan Rogers strcat(path, "/"); 17250402641SIan Rogers strcat(path, format->name); 17350402641SIan Rogers 17450402641SIan Rogers file = fopen(path, "r"); 17550402641SIan Rogers if (!file) 17650402641SIan Rogers return; 17750402641SIan Rogers __perf_pmu_format__load(format, file); 17850402641SIan Rogers fclose(file); 17950402641SIan Rogers } 18050402641SIan Rogers 181cd82a32eSJiri Olsa /* 182cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 183cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 184cd82a32eSJiri Olsa */ 18550402641SIan Rogers int perf_pmu__format_parse(struct perf_pmu *pmu, int dirfd, bool eager_load) 186cd82a32eSJiri Olsa { 187cd82a32eSJiri Olsa struct dirent *evt_ent; 188cd82a32eSJiri Olsa DIR *format_dir; 189cd82a32eSJiri Olsa int ret = 0; 190cd82a32eSJiri Olsa 191e293a5e8SNamhyung Kim format_dir = fdopendir(dirfd); 192cd82a32eSJiri Olsa if (!format_dir) 193cd82a32eSJiri Olsa return -EINVAL; 194cd82a32eSJiri Olsa 19550402641SIan Rogers while ((evt_ent = readdir(format_dir)) != NULL) { 19650402641SIan Rogers struct perf_pmu_format *format; 197cd82a32eSJiri Olsa char *name = evt_ent->d_name; 198cd82a32eSJiri Olsa 199cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 200cd82a32eSJiri Olsa continue; 201cd82a32eSJiri Olsa 20250402641SIan Rogers format = perf_pmu__new_format(&pmu->format, name); 20350402641SIan Rogers if (!format) { 20450402641SIan Rogers ret = -ENOMEM; 205cd82a32eSJiri Olsa break; 20650402641SIan Rogers } 207cd82a32eSJiri Olsa 20850402641SIan Rogers if (eager_load) { 20950402641SIan Rogers FILE *file; 21050402641SIan Rogers int fd = openat(dirfd, name, O_RDONLY); 21150402641SIan Rogers 21250402641SIan Rogers if (fd < 0) { 21350402641SIan Rogers ret = -errno; 21450402641SIan Rogers break; 21550402641SIan Rogers } 2163d88aec0SIan Rogers file = fdopen(fd, "r"); 2173d88aec0SIan Rogers if (!file) { 2183d88aec0SIan Rogers close(fd); 2193d88aec0SIan Rogers break; 2203d88aec0SIan Rogers } 22150402641SIan Rogers __perf_pmu_format__load(format, file); 2223d88aec0SIan Rogers fclose(file); 2233d88aec0SIan Rogers } 224cd82a32eSJiri Olsa } 225cd82a32eSJiri Olsa 226cd82a32eSJiri Olsa closedir(format_dir); 227cd82a32eSJiri Olsa return ret; 228cd82a32eSJiri Olsa } 229cd82a32eSJiri Olsa 230cd82a32eSJiri Olsa /* 231cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 232cd82a32eSJiri Olsa * located at: 233cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 234cd82a32eSJiri Olsa */ 235838a8c5fSIan Rogers static int pmu_format(struct perf_pmu *pmu, int dirfd, const char *name) 236cd82a32eSJiri Olsa { 237e293a5e8SNamhyung Kim int fd; 238cd82a32eSJiri Olsa 239e293a5e8SNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY); 240e293a5e8SNamhyung Kim if (fd < 0) 241d9664582SAndi Kleen return 0; 242cd82a32eSJiri Olsa 243e293a5e8SNamhyung Kim /* it'll close the fd */ 24450402641SIan Rogers if (perf_pmu__format_parse(pmu, fd, /*eager_load=*/false)) 245cd82a32eSJiri Olsa return -1; 246cd82a32eSJiri Olsa 247cd82a32eSJiri Olsa return 0; 248cd82a32eSJiri Olsa } 249cd82a32eSJiri Olsa 250a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval) 251d02fc6bcSAndi Kleen { 252d02fc6bcSAndi Kleen char *lc; 253d02fc6bcSAndi Kleen int ret = 0; 254d02fc6bcSAndi Kleen 255d02fc6bcSAndi Kleen /* 256d02fc6bcSAndi Kleen * save current locale 257d02fc6bcSAndi Kleen */ 258d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 259d02fc6bcSAndi Kleen 260d02fc6bcSAndi Kleen /* 261d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 262d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 263d02fc6bcSAndi Kleen * call below. 264d02fc6bcSAndi Kleen */ 265d02fc6bcSAndi Kleen lc = strdup(lc); 266d02fc6bcSAndi Kleen if (!lc) { 267d02fc6bcSAndi Kleen ret = -ENOMEM; 268d02fc6bcSAndi Kleen goto out; 269d02fc6bcSAndi Kleen } 270d02fc6bcSAndi Kleen 271d02fc6bcSAndi Kleen /* 272d02fc6bcSAndi Kleen * force to C locale to ensure kernel 273d02fc6bcSAndi Kleen * scale string is converted correctly. 274d02fc6bcSAndi Kleen * kernel uses default C locale. 275d02fc6bcSAndi Kleen */ 276d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 277d02fc6bcSAndi Kleen 278d02fc6bcSAndi Kleen *sval = strtod(scale, end); 279d02fc6bcSAndi Kleen 280d02fc6bcSAndi Kleen out: 281d02fc6bcSAndi Kleen /* restore locale */ 282d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 283d02fc6bcSAndi Kleen free(lc); 284d02fc6bcSAndi Kleen return ret; 285d02fc6bcSAndi Kleen } 286d02fc6bcSAndi Kleen 2877b723dbbSIan Rogers static int perf_pmu__parse_scale(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 288410136f5SStephane Eranian { 289410136f5SStephane Eranian struct stat st; 290410136f5SStephane Eranian ssize_t sret; 2917b723dbbSIan Rogers size_t len; 292410136f5SStephane Eranian char scale[128]; 293410136f5SStephane Eranian int fd, ret = -1; 294410136f5SStephane Eranian char path[PATH_MAX]; 295410136f5SStephane Eranian 2967b723dbbSIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 2977b723dbbSIan Rogers if (!len) 2987b723dbbSIan Rogers return 0; 2997b723dbbSIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/%s.scale", pmu->name, alias->name); 300410136f5SStephane Eranian 3017b723dbbSIan Rogers fd = open(path, O_RDONLY); 302410136f5SStephane Eranian if (fd == -1) 303410136f5SStephane Eranian return -1; 304410136f5SStephane Eranian 305410136f5SStephane Eranian if (fstat(fd, &st) < 0) 306410136f5SStephane Eranian goto error; 307410136f5SStephane Eranian 308410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 309410136f5SStephane Eranian if (sret < 0) 310410136f5SStephane Eranian goto error; 311410136f5SStephane Eranian 3129ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 3139ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 3149ecae065SMadhavan Srinivasan else 315410136f5SStephane Eranian scale[sret] = '\0'; 3169ecae065SMadhavan Srinivasan 317a55ab7c4SJin Yao ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); 318410136f5SStephane Eranian error: 319410136f5SStephane Eranian close(fd); 320410136f5SStephane Eranian return ret; 321410136f5SStephane Eranian } 322410136f5SStephane Eranian 3237b723dbbSIan Rogers static int perf_pmu__parse_unit(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 324410136f5SStephane Eranian { 325410136f5SStephane Eranian char path[PATH_MAX]; 3267b723dbbSIan Rogers size_t len; 327410136f5SStephane Eranian ssize_t sret; 328410136f5SStephane Eranian int fd; 329410136f5SStephane Eranian 330410136f5SStephane Eranian 3317b723dbbSIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 3327b723dbbSIan Rogers if (!len) 3337b723dbbSIan Rogers return 0; 3347b723dbbSIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/%s.unit", pmu->name, alias->name); 3357b723dbbSIan Rogers 3367b723dbbSIan Rogers fd = open(path, O_RDONLY); 337410136f5SStephane Eranian if (fd == -1) 338410136f5SStephane Eranian return -1; 339410136f5SStephane Eranian 340410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 341410136f5SStephane Eranian if (sret < 0) 342410136f5SStephane Eranian goto error; 343410136f5SStephane Eranian 344410136f5SStephane Eranian close(fd); 345410136f5SStephane Eranian 3469ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 3479ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 3489ecae065SMadhavan Srinivasan else 349410136f5SStephane Eranian alias->unit[sret] = '\0'; 350410136f5SStephane Eranian 351410136f5SStephane Eranian return 0; 352410136f5SStephane Eranian error: 353410136f5SStephane Eranian close(fd); 354410136f5SStephane Eranian alias->unit[0] = '\0'; 355410136f5SStephane Eranian return -1; 356410136f5SStephane Eranian } 357410136f5SStephane Eranian 358044330c1SMatt Fleming static int 3597b723dbbSIan Rogers perf_pmu__parse_per_pkg(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 360044330c1SMatt Fleming { 361044330c1SMatt Fleming char path[PATH_MAX]; 3627b723dbbSIan Rogers size_t len; 363044330c1SMatt Fleming int fd; 364044330c1SMatt Fleming 3657b723dbbSIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 3667b723dbbSIan Rogers if (!len) 3677b723dbbSIan Rogers return 0; 3687b723dbbSIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/%s.per-pkg", pmu->name, alias->name); 369044330c1SMatt Fleming 3707b723dbbSIan Rogers fd = open(path, O_RDONLY); 371044330c1SMatt Fleming if (fd == -1) 372044330c1SMatt Fleming return -1; 373044330c1SMatt Fleming 374044330c1SMatt Fleming close(fd); 375044330c1SMatt Fleming 376044330c1SMatt Fleming alias->per_pkg = true; 377044330c1SMatt Fleming return 0; 378044330c1SMatt Fleming } 379044330c1SMatt Fleming 3807b723dbbSIan Rogers static int perf_pmu__parse_snapshot(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 3811d9e446bSJiri Olsa { 3821d9e446bSJiri Olsa char path[PATH_MAX]; 3837b723dbbSIan Rogers size_t len; 3841d9e446bSJiri Olsa int fd; 3851d9e446bSJiri Olsa 3867b723dbbSIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 3877b723dbbSIan Rogers if (!len) 3887b723dbbSIan Rogers return 0; 3897b723dbbSIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/%s.snapshot", pmu->name, alias->name); 3901d9e446bSJiri Olsa 3917b723dbbSIan Rogers fd = open(path, O_RDONLY); 3921d9e446bSJiri Olsa if (fd == -1) 3931d9e446bSJiri Olsa return -1; 3941d9e446bSJiri Olsa 3951d9e446bSJiri Olsa alias->snapshot = true; 3961d9e446bSJiri Olsa close(fd); 3971d9e446bSJiri Olsa return 0; 3981d9e446bSJiri Olsa } 3991d9e446bSJiri Olsa 4006dde6429SThomas Richter /* Delete an alias entry. */ 401c3245d20SIan Rogers static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 4026dde6429SThomas Richter { 4036dde6429SThomas Richter zfree(&newalias->name); 4046dde6429SThomas Richter zfree(&newalias->desc); 4056dde6429SThomas Richter zfree(&newalias->long_desc); 4066dde6429SThomas Richter zfree(&newalias->topic); 40732705de7SJin Yao zfree(&newalias->pmu_name); 4080d3f0e6fSIan Rogers parse_events_terms__exit(&newalias->terms); 4096dde6429SThomas Richter free(newalias); 4106dde6429SThomas Richter } 4116dde6429SThomas Richter 412eec11310SNamhyung Kim static void perf_pmu__del_aliases(struct perf_pmu *pmu) 413eec11310SNamhyung Kim { 414eec11310SNamhyung Kim struct perf_pmu_alias *alias, *tmp; 415eec11310SNamhyung Kim 416eec11310SNamhyung Kim list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) { 417eec11310SNamhyung Kim list_del(&alias->list); 418eec11310SNamhyung Kim perf_pmu_free_alias(alias); 419eec11310SNamhyung Kim } 420eec11310SNamhyung Kim } 421eec11310SNamhyung Kim 4228d4b6d37SIan Rogers static struct perf_pmu_alias *perf_pmu__find_alias(struct perf_pmu *pmu, 4238d4b6d37SIan Rogers const char *name, 4248d4b6d37SIan Rogers bool load) 4256dde6429SThomas Richter { 426f63a536fSIan Rogers struct perf_pmu_alias *alias; 4276dde6429SThomas Richter 4288d4b6d37SIan Rogers if (load && !pmu->sysfs_aliases_loaded) 4298d4b6d37SIan Rogers pmu_aliases_parse(pmu); 4308d4b6d37SIan Rogers 431f63a536fSIan Rogers list_for_each_entry(alias, &pmu->aliases, list) { 432f63a536fSIan Rogers if (!strcasecmp(alias->name, name)) 433f63a536fSIan Rogers return alias; 43432705de7SJin Yao } 435f63a536fSIan Rogers return NULL; 436f63a536fSIan Rogers } 437f63a536fSIan Rogers 438f63a536fSIan Rogers static bool assign_str(const char *name, const char *field, char **old_str, 439f63a536fSIan Rogers const char *new_str) 440f63a536fSIan Rogers { 441f63a536fSIan Rogers if (!*old_str && new_str) { 442f63a536fSIan Rogers *old_str = strdup(new_str); 4436dde6429SThomas Richter return true; 4446dde6429SThomas Richter } 445f63a536fSIan Rogers 446f63a536fSIan Rogers if (!new_str || !strcasecmp(*old_str, new_str)) 447f63a536fSIan Rogers return false; /* Nothing to update. */ 448f63a536fSIan Rogers 449f63a536fSIan Rogers pr_debug("alias %s differs in field '%s' ('%s' != '%s')\n", 450f63a536fSIan Rogers name, field, *old_str, new_str); 451f63a536fSIan Rogers zfree(old_str); 452f63a536fSIan Rogers *old_str = strdup(new_str); 453f63a536fSIan Rogers return true; 4546dde6429SThomas Richter } 455f63a536fSIan Rogers 4567b723dbbSIan Rogers static void read_alias_info(struct perf_pmu *pmu, struct perf_pmu_alias *alias) 4577b723dbbSIan Rogers { 4587b723dbbSIan Rogers if (!alias->from_sysfs || alias->info_loaded) 4597b723dbbSIan Rogers return; 4607b723dbbSIan Rogers 4617b723dbbSIan Rogers /* 4627b723dbbSIan Rogers * load unit name and scale if available 4637b723dbbSIan Rogers */ 4647b723dbbSIan Rogers perf_pmu__parse_unit(pmu, alias); 4657b723dbbSIan Rogers perf_pmu__parse_scale(pmu, alias); 4667b723dbbSIan Rogers perf_pmu__parse_per_pkg(pmu, alias); 4677b723dbbSIan Rogers perf_pmu__parse_snapshot(pmu, alias); 4687b723dbbSIan Rogers } 4697b723dbbSIan Rogers 4707b723dbbSIan Rogers struct update_alias_data { 4717b723dbbSIan Rogers struct perf_pmu *pmu; 4727b723dbbSIan Rogers struct perf_pmu_alias *alias; 4737b723dbbSIan Rogers }; 4747b723dbbSIan Rogers 475f63a536fSIan Rogers static int update_alias(const struct pmu_event *pe, 476f63a536fSIan Rogers const struct pmu_events_table *table __maybe_unused, 477f63a536fSIan Rogers void *vdata) 478f63a536fSIan Rogers { 4797b723dbbSIan Rogers struct update_alias_data *data = vdata; 480f63a536fSIan Rogers int ret = 0; 481f63a536fSIan Rogers 4827b723dbbSIan Rogers read_alias_info(data->pmu, data->alias); 4837b723dbbSIan Rogers assign_str(pe->name, "desc", &data->alias->desc, pe->desc); 4847b723dbbSIan Rogers assign_str(pe->name, "long_desc", &data->alias->long_desc, pe->long_desc); 4857b723dbbSIan Rogers assign_str(pe->name, "topic", &data->alias->topic, pe->topic); 4867b723dbbSIan Rogers data->alias->per_pkg = pe->perpkg; 48730f0b435SIan Rogers if (pe->event) { 4880d3f0e6fSIan Rogers parse_events_terms__exit(&data->alias->terms); 4897b723dbbSIan Rogers ret = parse_events_terms(&data->alias->terms, pe->event, /*input=*/NULL); 490f63a536fSIan Rogers } 491f63a536fSIan Rogers if (!ret && pe->unit) { 492f63a536fSIan Rogers char *unit; 493f63a536fSIan Rogers 4947b723dbbSIan Rogers ret = perf_pmu__convert_scale(pe->unit, &unit, &data->alias->scale); 495f63a536fSIan Rogers if (!ret) 4967b723dbbSIan Rogers snprintf(data->alias->unit, sizeof(data->alias->unit), "%s", unit); 497f63a536fSIan Rogers } 498f63a536fSIan Rogers return ret; 4996dde6429SThomas Richter } 5006dde6429SThomas Richter 5017b723dbbSIan Rogers static int perf_pmu__new_alias(struct perf_pmu *pmu, const char *name, 502edb217ffSIan Rogers const char *desc, const char *val, FILE *val_fd, 503edb217ffSIan Rogers const struct pmu_event *pe) 504a6146d50SZheng Yan { 5055c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 506a6146d50SZheng Yan int ret; 507330f40a0SIan Rogers const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL; 508bd680861SIan Rogers bool deprecated = false, perpkg = false; 509eab35953SJin Yao 5108d4b6d37SIan Rogers if (perf_pmu__find_alias(pmu, name, /*load=*/ false)) { 511f63a536fSIan Rogers /* Alias was already created/loaded. */ 512f63a536fSIan Rogers return 0; 513f63a536fSIan Rogers } 514f63a536fSIan Rogers 515eab35953SJin Yao if (pe) { 516330f40a0SIan Rogers long_desc = pe->long_desc; 517330f40a0SIan Rogers topic = pe->topic; 518330f40a0SIan Rogers unit = pe->unit; 519bd680861SIan Rogers perpkg = pe->perpkg; 5209ed8b7dcSIan Rogers deprecated = pe->deprecated; 521330f40a0SIan Rogers pmu_name = pe->pmu; 522eab35953SJin Yao } 523a6146d50SZheng Yan 524eaaebb01SIan Rogers alias = zalloc(sizeof(*alias)); 525a6146d50SZheng Yan if (!alias) 526a6146d50SZheng Yan return -ENOMEM; 527a6146d50SZheng Yan 5280d3f0e6fSIan Rogers parse_events_terms__init(&alias->terms); 529410136f5SStephane Eranian alias->scale = 1.0; 530410136f5SStephane Eranian alias->unit[0] = '\0'; 531bd680861SIan Rogers alias->per_pkg = perpkg; 53284530920SStephane Eranian alias->snapshot = false; 5339ed8b7dcSIan Rogers alias->deprecated = deprecated; 534410136f5SStephane Eranian 535edb217ffSIan Rogers ret = parse_events_terms(&alias->terms, val, val_fd); 536a6146d50SZheng Yan if (ret) { 53770c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 538a6146d50SZheng Yan free(alias); 539a6146d50SZheng Yan return ret; 540a6146d50SZheng Yan } 541a6146d50SZheng Yan 542a6146d50SZheng Yan alias->name = strdup(name); 54308e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 544c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 545c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 546dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 547f63a536fSIan Rogers alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 548fedb2b51SAndi Kleen if (unit) { 549f63a536fSIan Rogers if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) { 550f63a536fSIan Rogers perf_pmu_free_alias(alias); 551fedb2b51SAndi Kleen return -1; 552f63a536fSIan Rogers } 553fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 554fedb2b51SAndi Kleen } 555f63a536fSIan Rogers if (!pe) { 556f63a536fSIan Rogers /* Update an event from sysfs with json data. */ 5577b723dbbSIan Rogers struct update_alias_data data = { 5587b723dbbSIan Rogers .pmu = pmu, 5597b723dbbSIan Rogers .alias = alias, 5607b723dbbSIan Rogers }; 5617b723dbbSIan Rogers 5627b723dbbSIan Rogers alias->from_sysfs = true; 5637c52f10cSIan Rogers if (pmu->events_table) { 564e6ff1eedSIan Rogers if (pmu_events_table__find_event(pmu->events_table, pmu, name, 5657b723dbbSIan Rogers update_alias, &data) == 0) 566e6ff1eedSIan Rogers pmu->loaded_json_aliases++; 5677c52f10cSIan Rogers } 568f63a536fSIan Rogers } 569f63a536fSIan Rogers 570e6ff1eedSIan Rogers if (!pe) 571e6ff1eedSIan Rogers pmu->sysfs_aliases++; 572e6ff1eedSIan Rogers else 573e6ff1eedSIan Rogers pmu->loaded_json_aliases++; 574f26d22f1SIan Rogers list_add_tail(&alias->list, &pmu->aliases); 575a6146d50SZheng Yan return 0; 576a6146d50SZheng Yan } 577a6146d50SZheng Yan 5783a42f4c7SIan Rogers static inline bool pmu_alias_info_file(const char *name) 57946441bdcSMatt Fleming { 58046441bdcSMatt Fleming size_t len; 58146441bdcSMatt Fleming 58246441bdcSMatt Fleming len = strlen(name); 58346441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 58446441bdcSMatt Fleming return true; 58546441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 58646441bdcSMatt Fleming return true; 587044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 588044330c1SMatt Fleming return true; 5891d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 5901d9e446bSJiri Olsa return true; 59146441bdcSMatt Fleming 59246441bdcSMatt Fleming return false; 59346441bdcSMatt Fleming } 59446441bdcSMatt Fleming 595a6146d50SZheng Yan /* 5968d4b6d37SIan Rogers * Reading the pmu event aliases definition, which should be located at: 5978d4b6d37SIan Rogers * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 598a6146d50SZheng Yan */ 5998d4b6d37SIan Rogers static int pmu_aliases_parse(struct perf_pmu *pmu) 600a6146d50SZheng Yan { 6018d4b6d37SIan Rogers char path[PATH_MAX]; 602a6146d50SZheng Yan struct dirent *evt_ent; 603a6146d50SZheng Yan DIR *event_dir; 6048d4b6d37SIan Rogers size_t len; 6058d4b6d37SIan Rogers int fd, dir_fd; 606a6146d50SZheng Yan 6078d4b6d37SIan Rogers len = perf_pmu__event_source_devices_scnprintf(path, sizeof(path)); 6088d4b6d37SIan Rogers if (!len) 6098d4b6d37SIan Rogers return 0; 6108d4b6d37SIan Rogers scnprintf(path + len, sizeof(path) - len, "%s/events", pmu->name); 6118d4b6d37SIan Rogers 6128d4b6d37SIan Rogers dir_fd = open(path, O_DIRECTORY); 6138d4b6d37SIan Rogers if (dir_fd == -1) { 6148d4b6d37SIan Rogers pmu->sysfs_aliases_loaded = true; 6158d4b6d37SIan Rogers return 0; 6168d4b6d37SIan Rogers } 6178d4b6d37SIan Rogers 6188d4b6d37SIan Rogers event_dir = fdopendir(dir_fd); 6198d4b6d37SIan Rogers if (!event_dir){ 6208d4b6d37SIan Rogers close (dir_fd); 621a6146d50SZheng Yan return -EINVAL; 6228d4b6d37SIan Rogers } 623a6146d50SZheng Yan 624940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 625a6146d50SZheng Yan char *name = evt_ent->d_name; 626a6146d50SZheng Yan FILE *file; 627a6146d50SZheng Yan 628a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 629a6146d50SZheng Yan continue; 630a6146d50SZheng Yan 631410136f5SStephane Eranian /* 63246441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 633410136f5SStephane Eranian */ 63446441bdcSMatt Fleming if (pmu_alias_info_file(name)) 635410136f5SStephane Eranian continue; 636410136f5SStephane Eranian 6378d4b6d37SIan Rogers fd = openat(dir_fd, name, O_RDONLY); 6380ea8920eSIan Rogers if (fd == -1) { 6390ea8920eSIan Rogers pr_debug("Cannot open %s\n", name); 6400ea8920eSIan Rogers continue; 6410ea8920eSIan Rogers } 642e293a5e8SNamhyung Kim file = fdopen(fd, "r"); 643940db6dcSAndi Kleen if (!file) { 6440ea8920eSIan Rogers close(fd); 645940db6dcSAndi Kleen continue; 646940db6dcSAndi Kleen } 647410136f5SStephane Eranian 6487b723dbbSIan Rogers if (perf_pmu__new_alias(pmu, name, /*desc=*/ NULL, 649edb217ffSIan Rogers /*val=*/ NULL, file, /*pe=*/ NULL) < 0) 650940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 651a6146d50SZheng Yan fclose(file); 652a6146d50SZheng Yan } 653a6146d50SZheng Yan 654a6146d50SZheng Yan closedir(event_dir); 6558d4b6d37SIan Rogers close (dir_fd); 6568d4b6d37SIan Rogers pmu->sysfs_aliases_loaded = true; 657a6146d50SZheng Yan return 0; 658a6146d50SZheng Yan } 659a6146d50SZheng Yan 6600d3f0e6fSIan Rogers static int pmu_alias_terms(struct perf_pmu_alias *alias, struct list_head *terms) 661a6146d50SZheng Yan { 6627c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 6630d3f0e6fSIan Rogers struct parse_events_terms clone_terms; 664a6146d50SZheng Yan 6650d3f0e6fSIan Rogers parse_events_terms__init(&clone_terms); 6660d3f0e6fSIan Rogers list_for_each_entry(term, &alias->terms.terms, list) { 6670d3f0e6fSIan Rogers int ret = parse_events_term__clone(&cloned, term); 6680d3f0e6fSIan Rogers 669a6146d50SZheng Yan if (ret) { 6700d3f0e6fSIan Rogers parse_events_terms__exit(&clone_terms); 671a6146d50SZheng Yan return ret; 672a6146d50SZheng Yan } 673c2f1ceadSAndi Kleen /* 674c2f1ceadSAndi Kleen * Weak terms don't override command line options, 675c2f1ceadSAndi Kleen * which we don't want for implicit terms in aliases. 676c2f1ceadSAndi Kleen */ 677c2f1ceadSAndi Kleen cloned->weak = true; 6780d3f0e6fSIan Rogers list_add_tail(&cloned->list, &clone_terms.terms); 679a6146d50SZheng Yan } 6800d3f0e6fSIan Rogers list_splice_init(&clone_terms.terms, terms); 6810d3f0e6fSIan Rogers parse_events_terms__exit(&clone_terms); 682a6146d50SZheng Yan return 0; 683a6146d50SZheng Yan } 684a6146d50SZheng Yan 68566ec1191SMark Rutland /* 68666ec1191SMark Rutland * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 68766ec1191SMark Rutland * may have a "cpus" file. 68866ec1191SMark Rutland */ 689d06593aaSIan Rogers static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name, bool is_core) 6907ae92e74SYan, Zheng { 691f854839bSJiri Olsa struct perf_cpu_map *cpus; 6927e3fcffeSMark Rutland const char *templates[] = { 693d50a79cdSJames Clark "cpumask", 694d50a79cdSJames Clark "cpus", 6957e3fcffeSMark Rutland NULL 6967e3fcffeSMark Rutland }; 6977e3fcffeSMark Rutland const char **template; 698d50a79cdSJames Clark char pmu_name[PATH_MAX]; 699d50a79cdSJames Clark struct perf_pmu pmu = {.name = pmu_name}; 700d50a79cdSJames Clark FILE *file; 7017ae92e74SYan, Zheng 702d50a79cdSJames Clark strlcpy(pmu_name, name, sizeof(pmu_name)); 7037e3fcffeSMark Rutland for (template = templates; *template; template++) { 7043a69672eSNamhyung Kim file = perf_pmu__open_file_at(&pmu, dirfd, *template); 705d50a79cdSJames Clark if (!file) 706d50a79cdSJames Clark continue; 707d50a79cdSJames Clark cpus = perf_cpu_map__read(file); 7083a69672eSNamhyung Kim fclose(file); 70966ec1191SMark Rutland if (cpus) 71066ec1191SMark Rutland return cpus; 7117e3fcffeSMark Rutland } 7127ae92e74SYan, Zheng 713d06593aaSIan Rogers /* Nothing found, for core PMUs assume this means all CPUs. */ 714d06593aaSIan Rogers return is_core ? perf_cpu_map__get(cpu_map__online()) : NULL; 71566ec1191SMark Rutland } 7167ae92e74SYan, Zheng 7173a69672eSNamhyung Kim static bool pmu_is_uncore(int dirfd, const char *name) 71866ec1191SMark Rutland { 7193a69672eSNamhyung Kim int fd; 7207ae92e74SYan, Zheng 7213a69672eSNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH); 7223a69672eSNamhyung Kim if (fd < 0) 7233a69672eSNamhyung Kim return false; 7243a69672eSNamhyung Kim 7253a69672eSNamhyung Kim close(fd); 7263a69672eSNamhyung Kim return true; 7277ae92e74SYan, Zheng } 7287ae92e74SYan, Zheng 72951d54847SJohn Garry static char *pmu_id(const char *name) 73051d54847SJohn Garry { 73151d54847SJohn Garry char path[PATH_MAX], *str; 73251d54847SJohn Garry size_t len; 73351d54847SJohn Garry 7345f2c8efaSJames Clark perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier"); 73551d54847SJohn Garry 7365f2c8efaSJames Clark if (filename__read_str(path, &str, &len) < 0) 73751d54847SJohn Garry return NULL; 73851d54847SJohn Garry 73951d54847SJohn Garry str[len - 1] = 0; /* remove line feed */ 74051d54847SJohn Garry 74151d54847SJohn Garry return str; 74251d54847SJohn Garry } 74351d54847SJohn Garry 7444bf7e81aSIan Rogers /** 7454bf7e81aSIan Rogers * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in 7464bf7e81aSIan Rogers * sysfs on some platforms like ARM or Intel hybrid. Looking for 7474bf7e81aSIan Rogers * possible the cpus file in sysfs files to identify whether this is a 7484bf7e81aSIan Rogers * core device. 7494bf7e81aSIan Rogers * @name: The PMU name such as "cpu_atom". 75014b22ae0SGanapatrao Kulkarni */ 7514bf7e81aSIan Rogers static int is_sysfs_pmu_core(const char *name) 75214b22ae0SGanapatrao Kulkarni { 75314b22ae0SGanapatrao Kulkarni char path[PATH_MAX]; 75414b22ae0SGanapatrao Kulkarni 755f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus")) 75614b22ae0SGanapatrao Kulkarni return 0; 757d9664582SAndi Kleen return file_available(path); 75814b22ae0SGanapatrao Kulkarni } 75914b22ae0SGanapatrao Kulkarni 76029be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu) 761d77ade9fSAndi Kleen { 762d77ade9fSAndi Kleen char *cpuid; 763d77ade9fSAndi Kleen static bool printed; 764d77ade9fSAndi Kleen 765d77ade9fSAndi Kleen cpuid = getenv("PERF_CPUID"); 766d77ade9fSAndi Kleen if (cpuid) 767d77ade9fSAndi Kleen cpuid = strdup(cpuid); 768d77ade9fSAndi Kleen if (!cpuid) 76954e32dc0SGanapatrao Kulkarni cpuid = get_cpuid_str(pmu); 770d77ade9fSAndi Kleen if (!cpuid) 771d77ade9fSAndi Kleen return NULL; 772d77ade9fSAndi Kleen 773d77ade9fSAndi Kleen if (!printed) { 774d77ade9fSAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 775d77ade9fSAndi Kleen printed = true; 776d77ade9fSAndi Kleen } 777d77ade9fSAndi Kleen return cpuid; 778d77ade9fSAndi Kleen } 779d77ade9fSAndi Kleen 780f8ea2c15SIan Rogers __weak const struct pmu_metrics_table *pmu_metrics_table__find(void) 781f8ea2c15SIan Rogers { 782f8ea2c15SIan Rogers return perf_pmu__find_metrics_table(NULL); 783f8ea2c15SIan Rogers } 784f8ea2c15SIan Rogers 785240e6fd0SIan Rogers /** 786240e6fd0SIan Rogers * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any 787240e6fd0SIan Rogers * trailing suffix? The Suffix must be in form 788240e6fd0SIan Rogers * tok_{digits}, or tok{digits}. 789240e6fd0SIan Rogers * @pmu_name: The pmu_name with possible suffix. 790240e6fd0SIan Rogers * @tok: The possible match to pmu_name without suffix. 791c07d5c92SJohn Garry */ 792240e6fd0SIan Rogers static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok) 793c47a5599SJin Yao { 794c07d5c92SJohn Garry const char *p; 795c47a5599SJin Yao 796c47a5599SJin Yao if (strncmp(pmu_name, tok, strlen(tok))) 797c47a5599SJin Yao return false; 798c47a5599SJin Yao 799c47a5599SJin Yao p = pmu_name + strlen(tok); 800c47a5599SJin Yao if (*p == 0) 801c47a5599SJin Yao return true; 802c47a5599SJin Yao 803c07d5c92SJohn Garry if (*p == '_') 804c47a5599SJin Yao ++p; 805c07d5c92SJohn Garry 806c07d5c92SJohn Garry /* Ensure we end in a number */ 807c07d5c92SJohn Garry while (1) { 808c07d5c92SJohn Garry if (!isdigit(*p)) 809c47a5599SJin Yao return false; 810c07d5c92SJohn Garry if (*(++p) == 0) 811c07d5c92SJohn Garry break; 812c07d5c92SJohn Garry } 813c47a5599SJin Yao 814c47a5599SJin Yao return true; 815c47a5599SJin Yao } 816c47a5599SJin Yao 817240e6fd0SIan Rogers /** 818240e6fd0SIan Rogers * pmu_uncore_alias_match - does name match the PMU name? 819240e6fd0SIan Rogers * @pmu_name: the json struct pmu_event name. This may lack a suffix (which 820240e6fd0SIan Rogers * matches) or be of the form "socket,pmuname" which will match 821240e6fd0SIan Rogers * "socketX_pmunameY". 822240e6fd0SIan Rogers * @name: a real full PMU name as from sysfs. 823240e6fd0SIan Rogers */ 824240e6fd0SIan Rogers static bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 825730670b1SJohn Garry { 826730670b1SJohn Garry char *tmp = NULL, *tok, *str; 827730670b1SJohn Garry bool res; 828730670b1SJohn Garry 829240e6fd0SIan Rogers if (strchr(pmu_name, ',') == NULL) 830240e6fd0SIan Rogers return perf_pmu__match_ignoring_suffix(name, pmu_name); 831240e6fd0SIan Rogers 832730670b1SJohn Garry str = strdup(pmu_name); 833730670b1SJohn Garry if (!str) 834730670b1SJohn Garry return false; 835730670b1SJohn Garry 836730670b1SJohn Garry /* 837730670b1SJohn Garry * uncore alias may be from different PMU with common prefix 838730670b1SJohn Garry */ 839730670b1SJohn Garry tok = strtok_r(str, ",", &tmp); 840730670b1SJohn Garry if (strncmp(pmu_name, tok, strlen(tok))) { 841730670b1SJohn Garry res = false; 842730670b1SJohn Garry goto out; 843730670b1SJohn Garry } 844730670b1SJohn Garry 845730670b1SJohn Garry /* 846730670b1SJohn Garry * Match more complex aliases where the alias name is a comma-delimited 847730670b1SJohn Garry * list of tokens, orderly contained in the matching PMU name. 848730670b1SJohn Garry * 849730670b1SJohn Garry * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 850730670b1SJohn Garry * match "socket" in "socketX_pmunameY" and then "pmuname" in 851730670b1SJohn Garry * "pmunameY". 852730670b1SJohn Garry */ 853c07d5c92SJohn Garry while (1) { 854c07d5c92SJohn Garry char *next_tok = strtok_r(NULL, ",", &tmp); 855c07d5c92SJohn Garry 856730670b1SJohn Garry name = strstr(name, tok); 857c07d5c92SJohn Garry if (!name || 858240e6fd0SIan Rogers (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) { 859730670b1SJohn Garry res = false; 860730670b1SJohn Garry goto out; 861730670b1SJohn Garry } 862c07d5c92SJohn Garry if (!next_tok) 863c07d5c92SJohn Garry break; 864c07d5c92SJohn Garry tok = next_tok; 865c07d5c92SJohn Garry name += strlen(tok); 866730670b1SJohn Garry } 867730670b1SJohn Garry 868730670b1SJohn Garry res = true; 869730670b1SJohn Garry out: 870730670b1SJohn Garry free(str); 871730670b1SJohn Garry return res; 872730670b1SJohn Garry } 873730670b1SJohn Garry 8742879ff36SJing Zhang bool pmu_uncore_identifier_match(const char *compat, const char *id) 8752879ff36SJing Zhang { 8762879ff36SJing Zhang regex_t re; 8772879ff36SJing Zhang regmatch_t pmatch[1]; 8782879ff36SJing Zhang int match; 8792879ff36SJing Zhang 8802879ff36SJing Zhang if (regcomp(&re, compat, REG_EXTENDED) != 0) { 8812879ff36SJing Zhang /* Warn unable to generate match particular string. */ 8822879ff36SJing Zhang pr_info("Invalid regular expression %s\n", compat); 8832879ff36SJing Zhang return false; 8842879ff36SJing Zhang } 8852879ff36SJing Zhang 8862879ff36SJing Zhang match = !regexec(&re, id, 1, pmatch, 0); 8872879ff36SJing Zhang if (match) { 8882879ff36SJing Zhang /* Ensure a full match. */ 8892879ff36SJing Zhang match = pmatch[0].rm_so == 0 && (size_t)pmatch[0].rm_eo == strlen(id); 8902879ff36SJing Zhang } 8912879ff36SJing Zhang regfree(&re); 8922879ff36SJing Zhang 8932879ff36SJing Zhang return match; 8942879ff36SJing Zhang } 8952879ff36SJing Zhang 896660842e4SIan Rogers static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, 8971ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 898660842e4SIan Rogers void *vdata) 899660842e4SIan Rogers { 900f26d22f1SIan Rogers struct perf_pmu *pmu = vdata; 901660842e4SIan Rogers 9027b723dbbSIan Rogers perf_pmu__new_alias(pmu, pe->name, pe->desc, pe->event, /*val_fd=*/ NULL, pe); 903660842e4SIan Rogers return 0; 904660842e4SIan Rogers } 905660842e4SIan Rogers 906933f82ffSSukadev Bhattiprolu /* 907d685819bSIan Rogers * From the pmu_events_table, find the events that correspond to the given 908d685819bSIan Rogers * PMU and add them to the list 'head'. 909933f82ffSSukadev Bhattiprolu */ 910838a8c5fSIan Rogers void pmu_add_cpu_aliases_table(struct perf_pmu *pmu, const struct pmu_events_table *table) 911933f82ffSSukadev Bhattiprolu { 912f26d22f1SIan Rogers pmu_events_table__for_each_event(table, pmu, pmu_add_cpu_aliases_map_callback, pmu); 913933f82ffSSukadev Bhattiprolu } 914933f82ffSSukadev Bhattiprolu 915838a8c5fSIan Rogers static void pmu_add_cpu_aliases(struct perf_pmu *pmu) 916e45ad701SJohn Garry { 9177c52f10cSIan Rogers if (!pmu->events_table) 918e45ad701SJohn Garry return; 919e45ad701SJohn Garry 920e6ff1eedSIan Rogers if (pmu->cpu_aliases_added) 921e6ff1eedSIan Rogers return; 922e6ff1eedSIan Rogers 9237c52f10cSIan Rogers pmu_add_cpu_aliases_table(pmu, pmu->events_table); 924e6ff1eedSIan Rogers pmu->cpu_aliases_added = true; 925e45ad701SJohn Garry } 926e45ad701SJohn Garry 92729be2fe0SIan Rogers static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, 9281ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 929f26d22f1SIan Rogers void *vdata) 9304513c719SJohn Garry { 931f26d22f1SIan Rogers struct perf_pmu *pmu = vdata; 9324513c719SJohn Garry 9334513c719SJohn Garry if (!pe->compat || !pe->pmu) 9344513c719SJohn Garry return 0; 9354513c719SJohn Garry 9362879ff36SJing Zhang if (pmu_uncore_alias_match(pe->pmu, pmu->name) && 9372879ff36SJing Zhang pmu_uncore_identifier_match(pe->compat, pmu->id)) { 9387b723dbbSIan Rogers perf_pmu__new_alias(pmu, 939edb217ffSIan Rogers pe->name, 940edb217ffSIan Rogers pe->desc, 941edb217ffSIan Rogers pe->event, 942edb217ffSIan Rogers /*val_fd=*/ NULL, 943eab35953SJin Yao pe); 9444513c719SJohn Garry } 9454513c719SJohn Garry 9464513c719SJohn Garry return 0; 9474513c719SJohn Garry } 9484513c719SJohn Garry 949838a8c5fSIan Rogers void pmu_add_sys_aliases(struct perf_pmu *pmu) 9504513c719SJohn Garry { 9514513c719SJohn Garry if (!pmu->id) 9524513c719SJohn Garry return; 9534513c719SJohn Garry 954f26d22f1SIan Rogers pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, pmu); 9554513c719SJohn Garry } 9564513c719SJohn Garry 957b1f05622SIan Rogers static char *pmu_find_alias_name(struct perf_pmu *pmu, int dirfd) 95813d60ba0SKan Liang { 959b1f05622SIan Rogers FILE *file = perf_pmu__open_file_at(pmu, dirfd, "alias"); 960b1f05622SIan Rogers char *line = NULL; 961b1f05622SIan Rogers size_t line_len = 0; 962b1f05622SIan Rogers ssize_t ret; 96313d60ba0SKan Liang 964b1f05622SIan Rogers if (!file) 96513d60ba0SKan Liang return NULL; 966b1f05622SIan Rogers 967b1f05622SIan Rogers ret = getline(&line, &line_len, file); 968b1f05622SIan Rogers if (ret < 0) { 969b1f05622SIan Rogers fclose(file); 970b1f05622SIan Rogers return NULL; 971b1f05622SIan Rogers } 972b1f05622SIan Rogers /* Remove trailing newline. */ 973b1f05622SIan Rogers if (ret > 0 && line[ret - 1] == '\n') 974b1f05622SIan Rogers line[--ret] = '\0'; 975b1f05622SIan Rogers 976b1f05622SIan Rogers fclose(file); 977b1f05622SIan Rogers return line; 97813d60ba0SKan Liang } 97913d60ba0SKan Liang 9803a69672eSNamhyung Kim static int pmu_max_precise(int dirfd, struct perf_pmu *pmu) 98190a86bdeSJiri Olsa { 98290a86bdeSJiri Olsa int max_precise = -1; 98390a86bdeSJiri Olsa 9843a69672eSNamhyung Kim perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise); 98590a86bdeSJiri Olsa return max_precise; 98690a86bdeSJiri Olsa } 98790a86bdeSJiri Olsa 988aa613601SIan Rogers void __weak 989aa613601SIan Rogers perf_pmu__arch_init(struct perf_pmu *pmu __maybe_unused) 990aa613601SIan Rogers { 991aa613601SIan Rogers } 992aa613601SIan Rogers 993b1f05622SIan Rogers struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *name) 994cd82a32eSJiri Olsa { 995cd82a32eSJiri Olsa struct perf_pmu *pmu; 996cd82a32eSJiri Olsa __u32 type; 99749afa7f6SJin Yao 998cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 999cd82a32eSJiri Olsa if (!pmu) 1000cd82a32eSJiri Olsa return NULL; 1001cd82a32eSJiri Olsa 1002f63a536fSIan Rogers pmu->name = strdup(name); 1003f63a536fSIan Rogers if (!pmu->name) 1004f63a536fSIan Rogers goto err; 100588ed9184SIan Rogers 100688ed9184SIan Rogers /* 100788ed9184SIan Rogers * Read type early to fail fast if a lookup name isn't a PMU. Ensure 100888ed9184SIan Rogers * that type value is successfully assigned (return 1). 100988ed9184SIan Rogers */ 101088ed9184SIan Rogers if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1) 101188ed9184SIan Rogers goto err; 101288ed9184SIan Rogers 101388ed9184SIan Rogers INIT_LIST_HEAD(&pmu->format); 101488ed9184SIan Rogers INIT_LIST_HEAD(&pmu->aliases); 101588ed9184SIan Rogers INIT_LIST_HEAD(&pmu->caps); 101688ed9184SIan Rogers 1017838a8c5fSIan Rogers /* 1018838a8c5fSIan Rogers * The pmu data we store & need consists of the pmu 1019838a8c5fSIan Rogers * type value and format definitions. Load both right 1020838a8c5fSIan Rogers * now. 1021838a8c5fSIan Rogers */ 1022838a8c5fSIan Rogers if (pmu_format(pmu, dirfd, name)) { 1023838a8c5fSIan Rogers free(pmu); 1024838a8c5fSIan Rogers return NULL; 1025838a8c5fSIan Rogers } 1026d06593aaSIan Rogers pmu->is_core = is_pmu_core(name); 1027d06593aaSIan Rogers pmu->cpus = pmu_cpumask(dirfd, name, pmu->is_core); 102813d60ba0SKan Liang 102954e32dc0SGanapatrao Kulkarni pmu->type = type; 10303a69672eSNamhyung Kim pmu->is_uncore = pmu_is_uncore(dirfd, name); 103151d54847SJohn Garry if (pmu->is_uncore) 103251d54847SJohn Garry pmu->id = pmu_id(name); 10333a69672eSNamhyung Kim pmu->max_precise = pmu_max_precise(dirfd, pmu); 1034b1f05622SIan Rogers pmu->alias_name = pmu_find_alias_name(pmu, dirfd); 10357c52f10cSIan Rogers pmu->events_table = perf_pmu__find_events_table(pmu); 1036838a8c5fSIan Rogers pmu_add_sys_aliases(pmu); 10371eaf496eSIan Rogers list_add_tail(&pmu->list, pmus); 1038dc0a6202SAdrian Hunter 1039aa613601SIan Rogers perf_pmu__arch_init(pmu); 1040dc0a6202SAdrian Hunter 1041cd82a32eSJiri Olsa return pmu; 104213d60ba0SKan Liang err: 1043efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->name); 104413d60ba0SKan Liang free(pmu); 104513d60ba0SKan Liang return NULL; 1046cd82a32eSJiri Olsa } 1047cd82a32eSJiri Olsa 1048628eaa4eSIan Rogers /* Creates the PMU when sysfs scanning fails. */ 1049628eaa4eSIan Rogers struct perf_pmu *perf_pmu__create_placeholder_core_pmu(struct list_head *core_pmus) 1050628eaa4eSIan Rogers { 1051628eaa4eSIan Rogers struct perf_pmu *pmu = zalloc(sizeof(*pmu)); 1052628eaa4eSIan Rogers 1053628eaa4eSIan Rogers if (!pmu) 1054628eaa4eSIan Rogers return NULL; 1055628eaa4eSIan Rogers 1056628eaa4eSIan Rogers pmu->name = strdup("cpu"); 1057628eaa4eSIan Rogers if (!pmu->name) { 1058628eaa4eSIan Rogers free(pmu); 1059628eaa4eSIan Rogers return NULL; 1060628eaa4eSIan Rogers } 1061628eaa4eSIan Rogers 1062628eaa4eSIan Rogers pmu->is_core = true; 1063628eaa4eSIan Rogers pmu->type = PERF_TYPE_RAW; 1064628eaa4eSIan Rogers pmu->cpus = cpu_map__online(); 1065628eaa4eSIan Rogers 1066628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->format); 1067628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->aliases); 1068628eaa4eSIan Rogers INIT_LIST_HEAD(&pmu->caps); 1069628eaa4eSIan Rogers list_add_tail(&pmu->list, core_pmus); 1070628eaa4eSIan Rogers return pmu; 1071628eaa4eSIan Rogers } 1072628eaa4eSIan Rogers 1073e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) 1074e552b7beSRob Herring { 1075e552b7beSRob Herring struct perf_pmu_format *format; 1076e552b7beSRob Herring 107768c25043SIan Rogers if (pmu->formats_checked) 107868c25043SIan Rogers return; 107968c25043SIan Rogers 108068c25043SIan Rogers pmu->formats_checked = true; 108168c25043SIan Rogers 1082e552b7beSRob Herring /* fake pmu doesn't have format list */ 1083e552b7beSRob Herring if (pmu == &perf_pmu__fake) 1084e552b7beSRob Herring return; 1085e552b7beSRob Herring 108650402641SIan Rogers list_for_each_entry(format, &pmu->format, list) { 108750402641SIan Rogers perf_pmu_format__load(pmu, format); 1088e552b7beSRob Herring if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) { 1089e552b7beSRob Herring pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" 1090e552b7beSRob Herring "which is not supported by this version of perf!\n", 1091e552b7beSRob Herring pmu->name, format->name, format->value); 1092e552b7beSRob Herring return; 1093e552b7beSRob Herring } 1094e552b7beSRob Herring } 109550402641SIan Rogers } 1096e552b7beSRob Herring 1097c6d616feSIan Rogers bool evsel__is_aux_event(const struct evsel *evsel) 1098e12ee9f7SAdrian Hunter { 1099e76026bdSArnaldo Carvalho de Melo struct perf_pmu *pmu = evsel__find_pmu(evsel); 1100e12ee9f7SAdrian Hunter 1101e12ee9f7SAdrian Hunter return pmu && pmu->auxtrace; 1102e12ee9f7SAdrian Hunter } 1103e12ee9f7SAdrian Hunter 11046593f019SJames Clark /* 11056593f019SJames Clark * Set @config_name to @val as long as the user hasn't already set or cleared it 11066593f019SJames Clark * by passing a config term on the command line. 11076593f019SJames Clark * 11086593f019SJames Clark * @val is the value to put into the bits specified by @config_name rather than 11096593f019SJames Clark * the bit pattern. It is shifted into position by this function, so to set 11106593f019SJames Clark * something to true, pass 1 for val rather than a pre shifted value. 11116593f019SJames Clark */ 11126593f019SJames Clark #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask)) 11136593f019SJames Clark void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel, 11146593f019SJames Clark const char *config_name, u64 val) 11156593f019SJames Clark { 11166593f019SJames Clark u64 user_bits = 0, bits; 11176593f019SJames Clark struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG); 11186593f019SJames Clark 11196593f019SJames Clark if (term) 11206593f019SJames Clark user_bits = term->val.cfg_chg; 11216593f019SJames Clark 1122da6a5afdSIan Rogers bits = perf_pmu__format_bits(pmu, config_name); 11236593f019SJames Clark 11246593f019SJames Clark /* Do nothing if the user changed the value */ 11256593f019SJames Clark if (bits & user_bits) 11266593f019SJames Clark return; 11276593f019SJames Clark 11286593f019SJames Clark /* Otherwise replace it */ 11296593f019SJames Clark evsel->core.attr.config &= ~bits; 11306593f019SJames Clark evsel->core.attr.config |= field_prep(bits, val); 11316593f019SJames Clark } 11326593f019SJames Clark 11335c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 113463883cb0SIan Rogers pmu_find_format(const struct list_head *formats, const char *name) 1135cd82a32eSJiri Olsa { 11365c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1137cd82a32eSJiri Olsa 1138cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 1139cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 1140cd82a32eSJiri Olsa return format; 1141cd82a32eSJiri Olsa 1142cd82a32eSJiri Olsa return NULL; 1143cd82a32eSJiri Olsa } 1144cd82a32eSJiri Olsa 1145da6a5afdSIan Rogers __u64 perf_pmu__format_bits(struct perf_pmu *pmu, const char *name) 114609ff6071SAdrian Hunter { 1147da6a5afdSIan Rogers struct perf_pmu_format *format = pmu_find_format(&pmu->format, name); 114809ff6071SAdrian Hunter __u64 bits = 0; 114909ff6071SAdrian Hunter int fbit; 115009ff6071SAdrian Hunter 115109ff6071SAdrian Hunter if (!format) 115209ff6071SAdrian Hunter return 0; 115309ff6071SAdrian Hunter 115409ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 115509ff6071SAdrian Hunter bits |= 1ULL << fbit; 115609ff6071SAdrian Hunter 115709ff6071SAdrian Hunter return bits; 115809ff6071SAdrian Hunter } 115909ff6071SAdrian Hunter 11607eb54733SIan Rogers int perf_pmu__format_type(struct perf_pmu *pmu, const char *name) 1161a1ac7de6SAdrian Hunter { 11627eb54733SIan Rogers struct perf_pmu_format *format = pmu_find_format(&pmu->format, name); 1163a1ac7de6SAdrian Hunter 1164a1ac7de6SAdrian Hunter if (!format) 1165a1ac7de6SAdrian Hunter return -1; 1166a1ac7de6SAdrian Hunter 116750402641SIan Rogers perf_pmu_format__load(pmu, format); 1168a1ac7de6SAdrian Hunter return format->value; 1169a1ac7de6SAdrian Hunter } 1170a1ac7de6SAdrian Hunter 1171cd82a32eSJiri Olsa /* 1172dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 11734d39c89fSIngo Molnar * and unformatted value (value parameter). 1174cd82a32eSJiri Olsa */ 1175dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1176dc0a6202SAdrian Hunter bool zero) 1177cd82a32eSJiri Olsa { 1178cd82a32eSJiri Olsa unsigned long fbit, vbit; 1179cd82a32eSJiri Olsa 1180cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1181cd82a32eSJiri Olsa 1182cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 1183cd82a32eSJiri Olsa continue; 1184cd82a32eSJiri Olsa 1185dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 1186dc0a6202SAdrian Hunter *v |= (1llu << fbit); 1187dc0a6202SAdrian Hunter else if (zero) 1188dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 1189cd82a32eSJiri Olsa } 1190cd82a32eSJiri Olsa } 1191cd82a32eSJiri Olsa 11920efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 11930efe6b67SAdrian Hunter { 11941b9caa10SJiri Olsa int w; 11950efe6b67SAdrian Hunter 11961b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 11971b9caa10SJiri Olsa if (!w) 11981b9caa10SJiri Olsa return 0; 11991b9caa10SJiri Olsa if (w < 64) 12001b9caa10SJiri Olsa return (1ULL << w) - 1; 12011b9caa10SJiri Olsa return -1; 12020efe6b67SAdrian Hunter } 12030efe6b67SAdrian Hunter 1204cd82a32eSJiri Olsa /* 1205688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 1206688d4dfcSCody P Schafer * in the remaining terms. 1207688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 1208688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 1209688d4dfcSCody P Schafer * in a config string) later on in the term list. 1210688d4dfcSCody P Schafer */ 1211688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 12120d3f0e6fSIan Rogers struct parse_events_terms *head_terms, 1213688d4dfcSCody P Schafer __u64 *value) 1214688d4dfcSCody P Schafer { 1215688d4dfcSCody P Schafer struct parse_events_term *t; 1216688d4dfcSCody P Schafer 12170d3f0e6fSIan Rogers list_for_each_entry(t, &head_terms->terms, list) { 12182a3d252dSIan Rogers if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 12192a3d252dSIan Rogers t->config && !strcmp(t->config, term->config)) { 1220688d4dfcSCody P Schafer t->used = true; 1221688d4dfcSCody P Schafer *value = t->val.num; 1222688d4dfcSCody P Schafer return 0; 1223688d4dfcSCody P Schafer } 1224688d4dfcSCody P Schafer } 1225688d4dfcSCody P Schafer 1226bb963e16SNamhyung Kim if (verbose > 0) 1227688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 1228688d4dfcSCody P Schafer 1229688d4dfcSCody P Schafer return -1; 1230688d4dfcSCody P Schafer } 1231688d4dfcSCody P Schafer 123263883cb0SIan Rogers static char *pmu_formats_string(const struct list_head *formats) 1233e64b020bSJiri Olsa { 1234e64b020bSJiri Olsa struct perf_pmu_format *format; 123511db4e29SMasami Hiramatsu char *str = NULL; 123611db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1237f1417ceaSXin Gao unsigned int i = 0; 1238e64b020bSJiri Olsa 1239ffeb883eSHe Kuang if (!formats) 1240e64b020bSJiri Olsa return NULL; 1241e64b020bSJiri Olsa 1242e64b020bSJiri Olsa /* sysfs exported terms */ 1243ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 124411db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 124511db4e29SMasami Hiramatsu goto error; 1246e64b020bSJiri Olsa 1247ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 124811db4e29SMasami Hiramatsu error: 1249ffeb883eSHe Kuang strbuf_release(&buf); 1250e64b020bSJiri Olsa 1251e64b020bSJiri Olsa return str; 1252e64b020bSJiri Olsa } 1253e64b020bSJiri Olsa 1254688d4dfcSCody P Schafer /* 1255cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 125688aca8d9SCody P Schafer * user input data - term parameter. 1257cd82a32eSJiri Olsa */ 125863883cb0SIan Rogers static int pmu_config_term(const struct perf_pmu *pmu, 1259cd82a32eSJiri Olsa struct perf_event_attr *attr, 1260dc0a6202SAdrian Hunter struct parse_events_term *term, 12610d3f0e6fSIan Rogers struct parse_events_terms *head_terms, 1262e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1263cd82a32eSJiri Olsa { 12645c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1265cd82a32eSJiri Olsa __u64 *vp; 12660efe6b67SAdrian Hunter __u64 val, max_val; 1267cd82a32eSJiri Olsa 1268cd82a32eSJiri Olsa /* 1269688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1270688d4dfcSCody P Schafer * skip it in normal eval. 1271688d4dfcSCody P Schafer */ 1272688d4dfcSCody P Schafer if (term->used) 1273688d4dfcSCody P Schafer return 0; 1274688d4dfcSCody P Schafer 1275688d4dfcSCody P Schafer /* 1276cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1277cd82a32eSJiri Olsa * to be done for them. 1278cd82a32eSJiri Olsa */ 1279cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1280cd82a32eSJiri Olsa return 0; 1281cd82a32eSJiri Olsa 1282804fee5dSIan Rogers format = pmu_find_format(&pmu->format, term->config); 1283688d4dfcSCody P Schafer if (!format) { 1284804fee5dSIan Rogers char *pmu_term = pmu_formats_string(&pmu->format); 12854ac22b48SIan Rogers char *unknown_term; 12864ac22b48SIan Rogers char *help_msg; 1287ffeb883eSHe Kuang 12884ac22b48SIan Rogers if (asprintf(&unknown_term, 12894ac22b48SIan Rogers "unknown term '%s' for pmu '%s'", 1290804fee5dSIan Rogers term->config, pmu->name) < 0) 12914ac22b48SIan Rogers unknown_term = NULL; 12924ac22b48SIan Rogers help_msg = parse_events_formats_error_string(pmu_term); 12934ac22b48SIan Rogers if (err) { 12946c191289SIan Rogers parse_events_error__handle(err, term->err_term, 12954ac22b48SIan Rogers unknown_term, 12964ac22b48SIan Rogers help_msg); 12974ac22b48SIan Rogers } else { 12984ac22b48SIan Rogers pr_debug("%s (%s)\n", unknown_term, help_msg); 12994ac22b48SIan Rogers free(unknown_term); 1300e64b020bSJiri Olsa } 13014ac22b48SIan Rogers free(pmu_term); 1302cd82a32eSJiri Olsa return -EINVAL; 1303688d4dfcSCody P Schafer } 130450402641SIan Rogers perf_pmu_format__load(pmu, format); 1305cd82a32eSJiri Olsa switch (format->value) { 1306cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1307cd82a32eSJiri Olsa vp = &attr->config; 1308cd82a32eSJiri Olsa break; 1309cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1310cd82a32eSJiri Olsa vp = &attr->config1; 1311cd82a32eSJiri Olsa break; 1312cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1313cd82a32eSJiri Olsa vp = &attr->config2; 1314cd82a32eSJiri Olsa break; 1315204e7c49SRob Herring case PERF_PMU_FORMAT_VALUE_CONFIG3: 1316204e7c49SRob Herring vp = &attr->config3; 1317204e7c49SRob Herring break; 1318cd82a32eSJiri Olsa default: 1319cd82a32eSJiri Olsa return -EINVAL; 1320cd82a32eSJiri Olsa } 1321cd82a32eSJiri Olsa 132216fa7e82SJiri Olsa /* 1323688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1324688d4dfcSCody P Schafer * using event parameters. 132516fa7e82SJiri Olsa */ 132699e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 132799e7138eSJiri Olsa if (term->no_value && 132899e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 132999e7138eSJiri Olsa if (err) { 13306c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1331448d732cSIan Rogers strdup("no value assigned for term"), 1332448d732cSIan Rogers NULL); 133399e7138eSJiri Olsa } 133499e7138eSJiri Olsa return -EINVAL; 133599e7138eSJiri Olsa } 133699e7138eSJiri Olsa 1337688d4dfcSCody P Schafer val = term->val.num; 133899e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1339688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1340bb963e16SNamhyung Kim if (verbose > 0) { 1341688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1342688d4dfcSCody P Schafer term->config, term->val.str); 1343e64b020bSJiri Olsa } 1344e64b020bSJiri Olsa if (err) { 13456c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1346448d732cSIan Rogers strdup("expected numeric value"), 1347448d732cSIan Rogers NULL); 1348e64b020bSJiri Olsa } 1349688d4dfcSCody P Schafer return -EINVAL; 1350688d4dfcSCody P Schafer } 1351688d4dfcSCody P Schafer 1352688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1353688d4dfcSCody P Schafer return -EINVAL; 1354688d4dfcSCody P Schafer } else 1355688d4dfcSCody P Schafer return -EINVAL; 1356688d4dfcSCody P Schafer 13570efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 13580efe6b67SAdrian Hunter if (val > max_val) { 13590efe6b67SAdrian Hunter if (err) { 1360448d732cSIan Rogers char *err_str; 1361448d732cSIan Rogers 13626c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1363448d732cSIan Rogers asprintf(&err_str, 13640efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1365448d732cSIan Rogers (unsigned long long)max_val) < 0 1366448d732cSIan Rogers ? strdup("value too big for format") 1367448d732cSIan Rogers : err_str, 1368448d732cSIan Rogers NULL); 13690efe6b67SAdrian Hunter return -EINVAL; 13700efe6b67SAdrian Hunter } 13710efe6b67SAdrian Hunter /* 13720efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 13730efe6b67SAdrian Hunter * silently truncated. 13740efe6b67SAdrian Hunter */ 13750efe6b67SAdrian Hunter } 13760efe6b67SAdrian Hunter 1377688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1378cd82a32eSJiri Olsa return 0; 1379cd82a32eSJiri Olsa } 1380cd82a32eSJiri Olsa 138163883cb0SIan Rogers int perf_pmu__config_terms(const struct perf_pmu *pmu, 1382cff7f956SJiri Olsa struct perf_event_attr *attr, 13830d3f0e6fSIan Rogers struct parse_events_terms *terms, 1384e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1385cd82a32eSJiri Olsa { 13866cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1387cd82a32eSJiri Olsa 13880d3f0e6fSIan Rogers list_for_each_entry(term, &terms->terms, list) { 13890d3f0e6fSIan Rogers if (pmu_config_term(pmu, attr, term, terms, zero, err)) 1390cd82a32eSJiri Olsa return -EINVAL; 1391688d4dfcSCody P Schafer } 1392cd82a32eSJiri Olsa 1393cd82a32eSJiri Olsa return 0; 1394cd82a32eSJiri Olsa } 1395cd82a32eSJiri Olsa 1396cd82a32eSJiri Olsa /* 1397cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1398cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1399cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1400cd82a32eSJiri Olsa */ 1401cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 14020d3f0e6fSIan Rogers struct parse_events_terms *head_terms, 1403e64b020bSJiri Olsa struct parse_events_error *err) 1404cd82a32eSJiri Olsa { 1405*0197da7aSIan Rogers bool zero = !!pmu->perf_event_attr_init_default; 1406dc0a6202SAdrian Hunter 1407804fee5dSIan Rogers return perf_pmu__config_terms(pmu, attr, head_terms, zero, err); 1408cd82a32eSJiri Olsa } 1409cd82a32eSJiri Olsa 14105c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 14116cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1412a6146d50SZheng Yan { 1413e6ff1eedSIan Rogers struct perf_pmu_alias *alias; 1414970ef02eSIan Rogers const char *name; 1415a6146d50SZheng Yan 1416a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1417a6146d50SZheng Yan return NULL; 1418a6146d50SZheng Yan 1419a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 142064199ae4SIan Rogers if (!term->no_value) 1421a6146d50SZheng Yan return NULL; 1422a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1423a6146d50SZheng Yan return NULL; 1424a6146d50SZheng Yan name = term->config; 1425c3245d20SIan Rogers 1426a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1427a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1428a6146d50SZheng Yan return NULL; 1429a6146d50SZheng Yan name = term->val.str; 1430a6146d50SZheng Yan } else { 1431a6146d50SZheng Yan return NULL; 1432a6146d50SZheng Yan } 1433a6146d50SZheng Yan 14348d4b6d37SIan Rogers alias = perf_pmu__find_alias(pmu, name, /*load=*/ true); 1435e6ff1eedSIan Rogers if (alias || pmu->cpu_aliases_added) 1436e6ff1eedSIan Rogers return alias; 1437e6ff1eedSIan Rogers 1438e6ff1eedSIan Rogers /* Alias doesn't exist, try to get it from the json events. */ 1439e6ff1eedSIan Rogers if (pmu->events_table && 1440e6ff1eedSIan Rogers pmu_events_table__find_event(pmu->events_table, pmu, name, 1441e6ff1eedSIan Rogers pmu_add_cpu_aliases_map_callback, 1442e6ff1eedSIan Rogers pmu) == 0) { 14438d4b6d37SIan Rogers alias = perf_pmu__find_alias(pmu, name, /*load=*/ false); 1444e6ff1eedSIan Rogers } 1445e6ff1eedSIan Rogers return alias; 1446a6146d50SZheng Yan } 1447a6146d50SZheng Yan 1448410136f5SStephane Eranian 14497b723dbbSIan Rogers static int check_info_data(struct perf_pmu *pmu, 14507b723dbbSIan Rogers struct perf_pmu_alias *alias, 14519d31cb93SIan Rogers struct perf_pmu_info *info, 14529d31cb93SIan Rogers struct parse_events_error *err, 14539d31cb93SIan Rogers int column) 1454410136f5SStephane Eranian { 14557b723dbbSIan Rogers read_alias_info(pmu, alias); 1456410136f5SStephane Eranian /* 1457410136f5SStephane Eranian * Only one term in event definition can 14581d9e446bSJiri Olsa * define unit, scale and snapshot, fail 14591d9e446bSJiri Olsa * if there's more than one. 1460410136f5SStephane Eranian */ 14619d31cb93SIan Rogers if (info->unit && alias->unit[0]) { 14629d31cb93SIan Rogers parse_events_error__handle(err, column, 14639d31cb93SIan Rogers strdup("Attempt to set event's unit twice"), 14649d31cb93SIan Rogers NULL); 1465410136f5SStephane Eranian return -EINVAL; 14669d31cb93SIan Rogers } 14679d31cb93SIan Rogers if (info->scale && alias->scale) { 14689d31cb93SIan Rogers parse_events_error__handle(err, column, 14699d31cb93SIan Rogers strdup("Attempt to set event's scale twice"), 14709d31cb93SIan Rogers NULL); 14719d31cb93SIan Rogers return -EINVAL; 14729d31cb93SIan Rogers } 14739d31cb93SIan Rogers if (info->snapshot && alias->snapshot) { 14749d31cb93SIan Rogers parse_events_error__handle(err, column, 14759d31cb93SIan Rogers strdup("Attempt to set event snapshot twice"), 14769d31cb93SIan Rogers NULL); 14779d31cb93SIan Rogers return -EINVAL; 14789d31cb93SIan Rogers } 1479410136f5SStephane Eranian 1480b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 14811d9e446bSJiri Olsa info->unit = alias->unit; 1482410136f5SStephane Eranian 1483410136f5SStephane Eranian if (alias->scale) 14841d9e446bSJiri Olsa info->scale = alias->scale; 14851d9e446bSJiri Olsa 14861d9e446bSJiri Olsa if (alias->snapshot) 14871d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1488410136f5SStephane Eranian 1489410136f5SStephane Eranian return 0; 1490410136f5SStephane Eranian } 1491410136f5SStephane Eranian 1492a6146d50SZheng Yan /* 1493a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1494a6146d50SZheng Yan * defined for the alias 1495a6146d50SZheng Yan */ 14960d3f0e6fSIan Rogers int perf_pmu__check_alias(struct perf_pmu *pmu, struct parse_events_terms *head_terms, 14979d31cb93SIan Rogers struct perf_pmu_info *info, struct parse_events_error *err) 1498a6146d50SZheng Yan { 14996cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 15005c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1501a6146d50SZheng Yan int ret; 1502a6146d50SZheng Yan 1503044330c1SMatt Fleming info->per_pkg = false; 1504044330c1SMatt Fleming 15058a398897SStephane Eranian /* 15068a398897SStephane Eranian * Mark unit and scale as not set 15078a398897SStephane Eranian * (different from default values, see below) 15088a398897SStephane Eranian */ 150946441bdcSMatt Fleming info->unit = NULL; 151046441bdcSMatt Fleming info->scale = 0.0; 15111d9e446bSJiri Olsa info->snapshot = false; 1512410136f5SStephane Eranian 15130d3f0e6fSIan Rogers list_for_each_entry_safe(term, h, &head_terms->terms, list) { 1514a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1515a6146d50SZheng Yan if (!alias) 1516a6146d50SZheng Yan continue; 1517a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 15189d31cb93SIan Rogers if (ret) { 15199d31cb93SIan Rogers parse_events_error__handle(err, term->err_term, 15209d31cb93SIan Rogers strdup("Failure to duplicate terms"), 15219d31cb93SIan Rogers NULL); 1522a6146d50SZheng Yan return ret; 15239d31cb93SIan Rogers } 1524410136f5SStephane Eranian 15257b723dbbSIan Rogers ret = check_info_data(pmu, alias, info, err, term->err_term); 1526410136f5SStephane Eranian if (ret) 1527410136f5SStephane Eranian return ret; 1528410136f5SStephane Eranian 1529044330c1SMatt Fleming if (alias->per_pkg) 1530044330c1SMatt Fleming info->per_pkg = true; 1531044330c1SMatt Fleming 1532e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 15331dc92556SIan Rogers parse_events_term__delete(term); 1534a6146d50SZheng Yan } 15358a398897SStephane Eranian 15368a398897SStephane Eranian /* 15378a398897SStephane Eranian * if no unit or scale found in aliases, then 15388a398897SStephane Eranian * set defaults as for evsel 15398a398897SStephane Eranian * unit cannot left to NULL 15408a398897SStephane Eranian */ 154146441bdcSMatt Fleming if (info->unit == NULL) 154246441bdcSMatt Fleming info->unit = ""; 15438a398897SStephane Eranian 154446441bdcSMatt Fleming if (info->scale == 0.0) 154546441bdcSMatt Fleming info->scale = 1.0; 15468a398897SStephane Eranian 1547a6146d50SZheng Yan return 0; 1548a6146d50SZheng Yan } 1549a6146d50SZheng Yan 1550c3245d20SIan Rogers struct find_event_args { 1551c3245d20SIan Rogers const char *event; 1552c3245d20SIan Rogers void *state; 1553c3245d20SIan Rogers pmu_event_callback cb; 1554c3245d20SIan Rogers }; 1555c3245d20SIan Rogers 1556c3245d20SIan Rogers static int find_event_callback(void *state, struct pmu_event_info *info) 1557838a8c5fSIan Rogers { 1558c3245d20SIan Rogers struct find_event_args *args = state; 1559838a8c5fSIan Rogers 1560c3245d20SIan Rogers if (!strcmp(args->event, info->name)) 1561c3245d20SIan Rogers return args->cb(args->state, info); 1562838a8c5fSIan Rogers 1563c3245d20SIan Rogers return 0; 1564838a8c5fSIan Rogers } 1565c3245d20SIan Rogers 1566c3245d20SIan Rogers int perf_pmu__find_event(struct perf_pmu *pmu, const char *event, void *state, pmu_event_callback cb) 1567c3245d20SIan Rogers { 1568c3245d20SIan Rogers struct find_event_args args = { 1569c3245d20SIan Rogers .event = event, 1570c3245d20SIan Rogers .state = state, 1571c3245d20SIan Rogers .cb = cb, 1572c3245d20SIan Rogers }; 1573c3245d20SIan Rogers 1574cd4e1efbSIan Rogers /* Sub-optimal, but function is only used by tests. */ 1575cd4e1efbSIan Rogers return perf_pmu__for_each_event(pmu, /*skip_duplicate_pmus=*/ false, 1576cd4e1efbSIan Rogers &args, find_event_callback); 1577c3245d20SIan Rogers } 1578c3245d20SIan Rogers 1579804fee5dSIan Rogers static void perf_pmu__del_formats(struct list_head *formats) 1580d26383dcSNamhyung Kim { 1581d26383dcSNamhyung Kim struct perf_pmu_format *fmt, *tmp; 1582d26383dcSNamhyung Kim 1583d26383dcSNamhyung Kim list_for_each_entry_safe(fmt, tmp, formats, list) { 1584d26383dcSNamhyung Kim list_del(&fmt->list); 1585efe98a7aSArnaldo Carvalho de Melo zfree(&fmt->name); 1586d26383dcSNamhyung Kim free(fmt); 1587d26383dcSNamhyung Kim } 1588d26383dcSNamhyung Kim } 1589d26383dcSNamhyung Kim 159007d2b820SIan Rogers bool perf_pmu__has_format(const struct perf_pmu *pmu, const char *name) 159107d2b820SIan Rogers { 159207d2b820SIan Rogers struct perf_pmu_format *format; 159307d2b820SIan Rogers 159407d2b820SIan Rogers list_for_each_entry(format, &pmu->format, list) { 159507d2b820SIan Rogers if (!strcmp(format->name, name)) 159607d2b820SIan Rogers return true; 159707d2b820SIan Rogers } 159807d2b820SIan Rogers return false; 159907d2b820SIan Rogers } 160007d2b820SIan Rogers 1601d504fae9SJohn Garry bool is_pmu_core(const char *name) 1602d504fae9SJohn Garry { 16036fbd67b0SThomas Richter return !strcmp(name, "cpu") || !strcmp(name, "cpum_cf") || is_sysfs_pmu_core(name); 16044bf7e81aSIan Rogers } 16054bf7e81aSIan Rogers 16066fd1e519SIan Rogers bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu) 16076fd1e519SIan Rogers { 1608e20d1f2fSIan Rogers return pmu->is_core; 16096fd1e519SIan Rogers } 16106fd1e519SIan Rogers 161152c7b4d3SIan Rogers bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu) 161252c7b4d3SIan Rogers { 1613710dffc9SIan Rogers return !pmu->is_core || perf_pmus__num_core_pmus() == 1; 161452c7b4d3SIan Rogers } 161552c7b4d3SIan Rogers 1616e6ff1eedSIan Rogers bool perf_pmu__have_event(struct perf_pmu *pmu, const char *name) 1617abe9544eSIan Rogers { 16188d4b6d37SIan Rogers if (perf_pmu__find_alias(pmu, name, /*load=*/ true) != NULL) 1619e6ff1eedSIan Rogers return true; 1620e6ff1eedSIan Rogers if (pmu->cpu_aliases_added || !pmu->events_table) 1621e6ff1eedSIan Rogers return false; 1622e6ff1eedSIan Rogers return pmu_events_table__find_event(pmu->events_table, pmu, name, NULL, NULL) == 0; 16234cabc3d1SAndi Kleen } 1624c3245d20SIan Rogers 1625e6ff1eedSIan Rogers size_t perf_pmu__num_events(struct perf_pmu *pmu) 1626c3245d20SIan Rogers { 16278d4b6d37SIan Rogers size_t nr; 16288d4b6d37SIan Rogers 16298d4b6d37SIan Rogers if (!pmu->sysfs_aliases_loaded) 16308d4b6d37SIan Rogers pmu_aliases_parse(pmu); 16318d4b6d37SIan Rogers 16328d4b6d37SIan Rogers nr = pmu->sysfs_aliases; 1633c3245d20SIan Rogers 1634e6ff1eedSIan Rogers if (pmu->cpu_aliases_added) 1635e6ff1eedSIan Rogers nr += pmu->loaded_json_aliases; 1636e6ff1eedSIan Rogers else if (pmu->events_table) 1637e6ff1eedSIan Rogers nr += pmu_events_table__num_events(pmu->events_table, pmu) - pmu->loaded_json_aliases; 1638c3245d20SIan Rogers 1639c3245d20SIan Rogers return pmu->selectable ? nr + 1 : nr; 1640c3245d20SIan Rogers } 1641c3245d20SIan Rogers 1642c3245d20SIan Rogers static int sub_non_neg(int a, int b) 1643c3245d20SIan Rogers { 1644c3245d20SIan Rogers if (b > a) 1645c3245d20SIan Rogers return 0; 1646c3245d20SIan Rogers return a - b; 1647c3245d20SIan Rogers } 1648c3245d20SIan Rogers 1649c3245d20SIan Rogers static char *format_alias(char *buf, int len, const struct perf_pmu *pmu, 1650cd4e1efbSIan Rogers const struct perf_pmu_alias *alias, bool skip_duplicate_pmus) 1651c3245d20SIan Rogers { 1652c3245d20SIan Rogers struct parse_events_term *term; 1653cd4e1efbSIan Rogers int pmu_name_len = skip_duplicate_pmus 1654cd4e1efbSIan Rogers ? pmu_name_len_no_suffix(pmu->name, /*num=*/NULL) 1655cd4e1efbSIan Rogers : (int)strlen(pmu->name); 1656cd4e1efbSIan Rogers int used = snprintf(buf, len, "%.*s/%s", pmu_name_len, pmu->name, alias->name); 1657c3245d20SIan Rogers 16580d3f0e6fSIan Rogers list_for_each_entry(term, &alias->terms.terms, list) { 1659c3245d20SIan Rogers if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1660c3245d20SIan Rogers used += snprintf(buf + used, sub_non_neg(len, used), 1661c3245d20SIan Rogers ",%s=%s", term->config, 1662c3245d20SIan Rogers term->val.str); 1663c3245d20SIan Rogers } 1664c3245d20SIan Rogers 1665c3245d20SIan Rogers if (sub_non_neg(len, used) > 0) { 1666c3245d20SIan Rogers buf[used] = '/'; 1667c3245d20SIan Rogers used++; 1668c3245d20SIan Rogers } 1669c3245d20SIan Rogers if (sub_non_neg(len, used) > 0) { 1670c3245d20SIan Rogers buf[used] = '\0'; 1671c3245d20SIan Rogers used++; 1672c3245d20SIan Rogers } else 1673c3245d20SIan Rogers buf[len - 1] = '\0'; 1674c3245d20SIan Rogers 1675c3245d20SIan Rogers return buf; 1676c3245d20SIan Rogers } 1677c3245d20SIan Rogers 1678cd4e1efbSIan Rogers int perf_pmu__for_each_event(struct perf_pmu *pmu, bool skip_duplicate_pmus, 1679cd4e1efbSIan Rogers void *state, pmu_event_callback cb) 1680c3245d20SIan Rogers { 1681c3245d20SIan Rogers char buf[1024]; 1682c3245d20SIan Rogers struct perf_pmu_alias *event; 1683c3245d20SIan Rogers struct pmu_event_info info = { 1684c3245d20SIan Rogers .pmu = pmu, 1685c3245d20SIan Rogers }; 1686c3245d20SIan Rogers int ret = 0; 168730f0b435SIan Rogers struct strbuf sb; 1688c3245d20SIan Rogers 168930f0b435SIan Rogers strbuf_init(&sb, /*hint=*/ 0); 1690e6ff1eedSIan Rogers pmu_add_cpu_aliases(pmu); 1691c3245d20SIan Rogers list_for_each_entry(event, &pmu->aliases, list) { 1692c3245d20SIan Rogers size_t buf_used; 1693c3245d20SIan Rogers 1694c3245d20SIan Rogers info.pmu_name = event->pmu_name ?: pmu->name; 1695c3245d20SIan Rogers info.alias = NULL; 1696c3245d20SIan Rogers if (event->desc) { 1697c3245d20SIan Rogers info.name = event->name; 1698c3245d20SIan Rogers buf_used = 0; 1699c3245d20SIan Rogers } else { 1700cd4e1efbSIan Rogers info.name = format_alias(buf, sizeof(buf), pmu, event, 1701cd4e1efbSIan Rogers skip_duplicate_pmus); 1702c3245d20SIan Rogers if (pmu->is_core) { 1703c3245d20SIan Rogers info.alias = info.name; 1704c3245d20SIan Rogers info.name = event->name; 1705c3245d20SIan Rogers } 1706c3245d20SIan Rogers buf_used = strlen(buf) + 1; 1707c3245d20SIan Rogers } 1708c3245d20SIan Rogers info.scale_unit = NULL; 1709c3245d20SIan Rogers if (strlen(event->unit) || event->scale != 1.0) { 1710c3245d20SIan Rogers info.scale_unit = buf + buf_used; 1711c3245d20SIan Rogers buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used, 1712c3245d20SIan Rogers "%G%s", event->scale, event->unit) + 1; 1713c3245d20SIan Rogers } 1714c3245d20SIan Rogers info.desc = event->desc; 1715c3245d20SIan Rogers info.long_desc = event->long_desc; 1716c3245d20SIan Rogers info.encoding_desc = buf + buf_used; 17170d3f0e6fSIan Rogers parse_events_terms__to_strbuf(&event->terms, &sb); 1718c3245d20SIan Rogers buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used, 171930f0b435SIan Rogers "%s/%s/", info.pmu_name, sb.buf) + 1; 1720c3245d20SIan Rogers info.topic = event->topic; 172130f0b435SIan Rogers info.str = sb.buf; 1722c3245d20SIan Rogers info.deprecated = event->deprecated; 1723c3245d20SIan Rogers ret = cb(state, &info); 1724c3245d20SIan Rogers if (ret) 172530f0b435SIan Rogers goto out; 172630f0b435SIan Rogers strbuf_setlen(&sb, /*len=*/ 0); 1727c3245d20SIan Rogers } 1728c3245d20SIan Rogers if (pmu->selectable) { 1729c3245d20SIan Rogers info.name = buf; 1730c3245d20SIan Rogers snprintf(buf, sizeof(buf), "%s//", pmu->name); 1731c3245d20SIan Rogers info.alias = NULL; 1732c3245d20SIan Rogers info.scale_unit = NULL; 1733c3245d20SIan Rogers info.desc = NULL; 1734c3245d20SIan Rogers info.long_desc = NULL; 1735c3245d20SIan Rogers info.encoding_desc = NULL; 1736c3245d20SIan Rogers info.topic = NULL; 1737c3245d20SIan Rogers info.pmu_name = pmu->name; 1738c3245d20SIan Rogers info.deprecated = false; 1739c3245d20SIan Rogers ret = cb(state, &info); 1740c3245d20SIan Rogers } 174130f0b435SIan Rogers out: 174230f0b435SIan Rogers strbuf_release(&sb); 1743c3245d20SIan Rogers return ret; 17444cabc3d1SAndi Kleen } 17457d4bdab5SAdrian Hunter 1746e3edd6cfSIan Rogers bool pmu__name_match(const struct perf_pmu *pmu, const char *pmu_name) 1747e3edd6cfSIan Rogers { 1748e3edd6cfSIan Rogers return !strcmp(pmu->name, pmu_name) || 1749d2045f87SIan Rogers (pmu->is_uncore && pmu_uncore_alias_match(pmu_name, pmu->name)) || 1750d2045f87SIan Rogers /* 1751d2045f87SIan Rogers * jevents and tests use default_core as a marker for any core 1752d2045f87SIan Rogers * PMU as the PMU name varies across architectures. 1753d2045f87SIan Rogers */ 1754d2045f87SIan Rogers (pmu->is_core && !strcmp(pmu_name, "default_core")); 1755e3edd6cfSIan Rogers } 1756e3edd6cfSIan Rogers 1757251aa040SIan Rogers bool perf_pmu__is_software(const struct perf_pmu *pmu) 1758251aa040SIan Rogers { 1759251aa040SIan Rogers if (pmu->is_core || pmu->is_uncore || pmu->auxtrace) 1760251aa040SIan Rogers return false; 1761251aa040SIan Rogers switch (pmu->type) { 1762251aa040SIan Rogers case PERF_TYPE_HARDWARE: return false; 1763251aa040SIan Rogers case PERF_TYPE_SOFTWARE: return true; 1764251aa040SIan Rogers case PERF_TYPE_TRACEPOINT: return true; 1765251aa040SIan Rogers case PERF_TYPE_HW_CACHE: return false; 1766251aa040SIan Rogers case PERF_TYPE_RAW: return false; 1767251aa040SIan Rogers case PERF_TYPE_BREAKPOINT: return true; 1768251aa040SIan Rogers default: break; 1769251aa040SIan Rogers } 1770251aa040SIan Rogers return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe"); 1771251aa040SIan Rogers } 1772251aa040SIan Rogers 17733a42f4c7SIan Rogers FILE *perf_pmu__open_file(const struct perf_pmu *pmu, const char *name) 17747d4bdab5SAdrian Hunter { 17757d4bdab5SAdrian Hunter char path[PATH_MAX]; 17767d4bdab5SAdrian Hunter 1777f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) || 1778f8ad6018SJames Clark !file_available(path)) 17797d4bdab5SAdrian Hunter return NULL; 17807d4bdab5SAdrian Hunter 17817d4bdab5SAdrian Hunter return fopen(path, "r"); 17827d4bdab5SAdrian Hunter } 17837d4bdab5SAdrian Hunter 17843a42f4c7SIan Rogers FILE *perf_pmu__open_file_at(const struct perf_pmu *pmu, int dirfd, const char *name) 17853a69672eSNamhyung Kim { 17863a69672eSNamhyung Kim int fd; 17873a69672eSNamhyung Kim 17883a69672eSNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY); 17893a69672eSNamhyung Kim if (fd < 0) 17903a69672eSNamhyung Kim return NULL; 17913a69672eSNamhyung Kim 17923a69672eSNamhyung Kim return fdopen(fd, "r"); 17933a69672eSNamhyung Kim } 17943a69672eSNamhyung Kim 17953a42f4c7SIan Rogers int perf_pmu__scan_file(const struct perf_pmu *pmu, const char *name, const char *fmt, 17967d4bdab5SAdrian Hunter ...) 17977d4bdab5SAdrian Hunter { 17987d4bdab5SAdrian Hunter va_list args; 17997d4bdab5SAdrian Hunter FILE *file; 18007d4bdab5SAdrian Hunter int ret = EOF; 18017d4bdab5SAdrian Hunter 18027d4bdab5SAdrian Hunter va_start(args, fmt); 18037d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 18047d4bdab5SAdrian Hunter if (file) { 18057d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 18067d4bdab5SAdrian Hunter fclose(file); 18077d4bdab5SAdrian Hunter } 18087d4bdab5SAdrian Hunter va_end(args); 18097d4bdab5SAdrian Hunter return ret; 18107d4bdab5SAdrian Hunter } 18119fbc61f8SKan Liang 18123a42f4c7SIan Rogers int perf_pmu__scan_file_at(const struct perf_pmu *pmu, int dirfd, const char *name, 18133a69672eSNamhyung Kim const char *fmt, ...) 18143a69672eSNamhyung Kim { 18153a69672eSNamhyung Kim va_list args; 18163a69672eSNamhyung Kim FILE *file; 18173a69672eSNamhyung Kim int ret = EOF; 18183a69672eSNamhyung Kim 18193a69672eSNamhyung Kim va_start(args, fmt); 18203a69672eSNamhyung Kim file = perf_pmu__open_file_at(pmu, dirfd, name); 18213a69672eSNamhyung Kim if (file) { 18223a69672eSNamhyung Kim ret = vfscanf(file, fmt, args); 18233a69672eSNamhyung Kim fclose(file); 18243a69672eSNamhyung Kim } 18253a69672eSNamhyung Kim va_end(args); 18263a69672eSNamhyung Kim return ret; 18273a69672eSNamhyung Kim } 18283a69672eSNamhyung Kim 18293a42f4c7SIan Rogers bool perf_pmu__file_exists(const struct perf_pmu *pmu, const char *name) 1830c2b6a896SGerman Gomez { 1831c2b6a896SGerman Gomez char path[PATH_MAX]; 1832c2b6a896SGerman Gomez 1833c2b6a896SGerman Gomez if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name)) 1834c2b6a896SGerman Gomez return false; 1835c2b6a896SGerman Gomez 1836c2b6a896SGerman Gomez return file_available(path); 1837c2b6a896SGerman Gomez } 1838c2b6a896SGerman Gomez 18399fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 18409fbc61f8SKan Liang { 18419fbc61f8SKan Liang struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 18429fbc61f8SKan Liang 18439fbc61f8SKan Liang if (!caps) 18449fbc61f8SKan Liang return -ENOMEM; 18459fbc61f8SKan Liang 18469fbc61f8SKan Liang caps->name = strdup(name); 18479fbc61f8SKan Liang if (!caps->name) 18489fbc61f8SKan Liang goto free_caps; 18499fbc61f8SKan Liang caps->value = strndup(value, strlen(value) - 1); 18509fbc61f8SKan Liang if (!caps->value) 18519fbc61f8SKan Liang goto free_name; 18529fbc61f8SKan Liang list_add_tail(&caps->list, list); 18539fbc61f8SKan Liang return 0; 18549fbc61f8SKan Liang 18559fbc61f8SKan Liang free_name: 185657f14b5aSArnaldo Carvalho de Melo zfree(&caps->name); 18579fbc61f8SKan Liang free_caps: 18589fbc61f8SKan Liang free(caps); 18599fbc61f8SKan Liang 18609fbc61f8SKan Liang return -ENOMEM; 18619fbc61f8SKan Liang } 18629fbc61f8SKan Liang 1863eec11310SNamhyung Kim static void perf_pmu__del_caps(struct perf_pmu *pmu) 1864eec11310SNamhyung Kim { 1865eec11310SNamhyung Kim struct perf_pmu_caps *caps, *tmp; 1866eec11310SNamhyung Kim 1867eec11310SNamhyung Kim list_for_each_entry_safe(caps, tmp, &pmu->caps, list) { 1868eec11310SNamhyung Kim list_del(&caps->list); 1869efe98a7aSArnaldo Carvalho de Melo zfree(&caps->name); 1870efe98a7aSArnaldo Carvalho de Melo zfree(&caps->value); 1871eec11310SNamhyung Kim free(caps); 1872eec11310SNamhyung Kim } 1873eec11310SNamhyung Kim } 1874eec11310SNamhyung Kim 18759fbc61f8SKan Liang /* 18769fbc61f8SKan Liang * Reading/parsing the given pmu capabilities, which should be located at: 18779fbc61f8SKan Liang * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 18789fbc61f8SKan Liang * Return the number of capabilities 18799fbc61f8SKan Liang */ 18809fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu) 18819fbc61f8SKan Liang { 18829fbc61f8SKan Liang struct stat st; 18839fbc61f8SKan Liang char caps_path[PATH_MAX]; 18849fbc61f8SKan Liang DIR *caps_dir; 18859fbc61f8SKan Liang struct dirent *evt_ent; 1886b39094d3SNamhyung Kim int caps_fd; 18873339ec44SRavi Bangoria 18883339ec44SRavi Bangoria if (pmu->caps_initialized) 18893339ec44SRavi Bangoria return pmu->nr_caps; 18903339ec44SRavi Bangoria 18913339ec44SRavi Bangoria pmu->nr_caps = 0; 18929fbc61f8SKan Liang 1893f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps")) 18949fbc61f8SKan Liang return -1; 18959fbc61f8SKan Liang 18963339ec44SRavi Bangoria if (stat(caps_path, &st) < 0) { 18973339ec44SRavi Bangoria pmu->caps_initialized = true; 18989fbc61f8SKan Liang return 0; /* no error if caps does not exist */ 18993339ec44SRavi Bangoria } 19009fbc61f8SKan Liang 19019fbc61f8SKan Liang caps_dir = opendir(caps_path); 19029fbc61f8SKan Liang if (!caps_dir) 19039fbc61f8SKan Liang return -EINVAL; 19049fbc61f8SKan Liang 1905b39094d3SNamhyung Kim caps_fd = dirfd(caps_dir); 1906b39094d3SNamhyung Kim 19079fbc61f8SKan Liang while ((evt_ent = readdir(caps_dir)) != NULL) { 19089fbc61f8SKan Liang char *name = evt_ent->d_name; 19099fbc61f8SKan Liang char value[128]; 19109fbc61f8SKan Liang FILE *file; 1911b39094d3SNamhyung Kim int fd; 19129fbc61f8SKan Liang 19139fbc61f8SKan Liang if (!strcmp(name, ".") || !strcmp(name, "..")) 19149fbc61f8SKan Liang continue; 19159fbc61f8SKan Liang 1916b39094d3SNamhyung Kim fd = openat(caps_fd, name, O_RDONLY); 19170ea8920eSIan Rogers if (fd == -1) 19189fbc61f8SKan Liang continue; 19190ea8920eSIan Rogers file = fdopen(fd, "r"); 19200ea8920eSIan Rogers if (!file) { 19210ea8920eSIan Rogers close(fd); 19220ea8920eSIan Rogers continue; 19230ea8920eSIan Rogers } 19249fbc61f8SKan Liang 19259fbc61f8SKan Liang if (!fgets(value, sizeof(value), file) || 19269fbc61f8SKan Liang (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 19279fbc61f8SKan Liang fclose(file); 19289fbc61f8SKan Liang continue; 19299fbc61f8SKan Liang } 19309fbc61f8SKan Liang 19313339ec44SRavi Bangoria pmu->nr_caps++; 19329fbc61f8SKan Liang fclose(file); 19339fbc61f8SKan Liang } 19349fbc61f8SKan Liang 19359fbc61f8SKan Liang closedir(caps_dir); 19369fbc61f8SKan Liang 19373339ec44SRavi Bangoria pmu->caps_initialized = true; 19383339ec44SRavi Bangoria return pmu->nr_caps; 19399fbc61f8SKan Liang } 1940e4064776SJin Yao 1941b9f01032SIan Rogers static void perf_pmu__compute_config_masks(struct perf_pmu *pmu) 1942e4064776SJin Yao { 1943e4064776SJin Yao struct perf_pmu_format *format; 1944b9f01032SIan Rogers 1945b9f01032SIan Rogers if (pmu->config_masks_computed) 1946b9f01032SIan Rogers return; 1947e4064776SJin Yao 1948e4064776SJin Yao list_for_each_entry(format, &pmu->format, list) { 1949b9f01032SIan Rogers unsigned int i; 1950b9f01032SIan Rogers __u64 *mask; 1951b9f01032SIan Rogers 1952b9f01032SIan Rogers if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) 1953e4064776SJin Yao continue; 1954e4064776SJin Yao 1955b9f01032SIan Rogers pmu->config_masks_present = true; 1956b9f01032SIan Rogers mask = &pmu->config_masks[format->value]; 1957b9f01032SIan Rogers 1958e4064776SJin Yao for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1959b9f01032SIan Rogers *mask |= 1ULL << i; 1960e4064776SJin Yao } 1961b9f01032SIan Rogers pmu->config_masks_computed = true; 1962b9f01032SIan Rogers } 1963b9f01032SIan Rogers 1964b9f01032SIan Rogers void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 1965b9f01032SIan Rogers const char *name, int config_num, 1966b9f01032SIan Rogers const char *config_name) 1967b9f01032SIan Rogers { 1968b9f01032SIan Rogers __u64 bits; 1969b9f01032SIan Rogers char buf[100]; 1970b9f01032SIan Rogers 1971b9f01032SIan Rogers perf_pmu__compute_config_masks(pmu); 1972e4064776SJin Yao 1973e4064776SJin Yao /* 1974e4064776SJin Yao * Kernel doesn't export any valid format bits. 1975e4064776SJin Yao */ 1976b9f01032SIan Rogers if (!pmu->config_masks_present) 1977e4064776SJin Yao return; 1978e4064776SJin Yao 1979b9f01032SIan Rogers bits = config & ~pmu->config_masks[config_num]; 1980e4064776SJin Yao if (bits == 0) 1981e4064776SJin Yao return; 1982e4064776SJin Yao 1983e4064776SJin Yao bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1984e4064776SJin Yao 1985b9f01032SIan Rogers pr_warning("WARNING: event '%s' not valid (bits %s of %s " 1986e4064776SJin Yao "'%llx' not supported by kernel)!\n", 1987b9f01032SIan Rogers name ?: "N/A", buf, config_name, config); 1988e4064776SJin Yao } 1989c5a26ea4SJin Yao 1990c091ee90SIan Rogers int perf_pmu__match(const char *pattern, const char *name, const char *tok) 1991c47a5599SJin Yao { 199213d60ba0SKan Liang if (!name) 199313d60ba0SKan Liang return -1; 199413d60ba0SKan Liang 1995c47a5599SJin Yao if (fnmatch(pattern, name, 0)) 1996c47a5599SJin Yao return -1; 1997c47a5599SJin Yao 1998240e6fd0SIan Rogers if (tok && !perf_pmu__match_ignoring_suffix(name, tok)) 1999c47a5599SJin Yao return -1; 2000c47a5599SJin Yao 2001c47a5599SJin Yao return 0; 2002c47a5599SJin Yao } 20031d3351e6SJin Yao 2004acef233bSJing Zhang double __weak perf_pmu__cpu_slots_per_cycle(void) 2005acef233bSJing Zhang { 2006acef233bSJing Zhang return NAN; 2007acef233bSJing Zhang } 2008f8ad6018SJames Clark 2009f8ad6018SJames Clark int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size) 2010f8ad6018SJames Clark { 2011f8ad6018SJames Clark const char *sysfs = sysfs__mountpoint(); 2012f8ad6018SJames Clark 2013f8ad6018SJames Clark if (!sysfs) 2014f8ad6018SJames Clark return 0; 2015f8ad6018SJames Clark return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs); 2016f8ad6018SJames Clark } 2017f8ad6018SJames Clark 2018e293a5e8SNamhyung Kim int perf_pmu__event_source_devices_fd(void) 2019e293a5e8SNamhyung Kim { 2020e293a5e8SNamhyung Kim char path[PATH_MAX]; 2021e293a5e8SNamhyung Kim const char *sysfs = sysfs__mountpoint(); 2022e293a5e8SNamhyung Kim 2023e293a5e8SNamhyung Kim if (!sysfs) 2024e293a5e8SNamhyung Kim return -1; 2025e293a5e8SNamhyung Kim 2026e293a5e8SNamhyung Kim scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs); 2027e293a5e8SNamhyung Kim return open(path, O_DIRECTORY); 2028e293a5e8SNamhyung Kim } 2029e293a5e8SNamhyung Kim 2030f8ad6018SJames Clark /* 2031f8ad6018SJames Clark * Fill 'buf' with the path to a file or folder in 'pmu_name' in 2032f8ad6018SJames Clark * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format" 2033f8ad6018SJames Clark * then pathname will be filled with 2034f8ad6018SJames Clark * "/sys/bus/event_source/devices/cs_etm/format" 2035f8ad6018SJames Clark * 2036e1a3aad3SIan Rogers * Return 0 if the sysfs mountpoint couldn't be found, if no characters were 2037e1a3aad3SIan Rogers * written or if the buffer size is exceeded. 2038f8ad6018SJames Clark */ 2039f8ad6018SJames Clark int perf_pmu__pathname_scnprintf(char *buf, size_t size, 2040f8ad6018SJames Clark const char *pmu_name, const char *filename) 2041f8ad6018SJames Clark { 2042e1a3aad3SIan Rogers size_t len; 2043f8ad6018SJames Clark 2044e1a3aad3SIan Rogers len = perf_pmu__event_source_devices_scnprintf(buf, size); 2045e1a3aad3SIan Rogers if (!len || (len + strlen(pmu_name) + strlen(filename) + 1) >= size) 2046f8ad6018SJames Clark return 0; 2047e1a3aad3SIan Rogers 2048e1a3aad3SIan Rogers return scnprintf(buf + len, size - len, "%s/%s", pmu_name, filename); 2049f8ad6018SJames Clark } 2050eec11310SNamhyung Kim 2051e293a5e8SNamhyung Kim int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags) 2052e293a5e8SNamhyung Kim { 2053e293a5e8SNamhyung Kim char path[PATH_MAX]; 2054e293a5e8SNamhyung Kim 2055e293a5e8SNamhyung Kim scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename); 2056e293a5e8SNamhyung Kim return openat(dirfd, path, flags); 2057e293a5e8SNamhyung Kim } 2058e293a5e8SNamhyung Kim 20591eaf496eSIan Rogers void perf_pmu__delete(struct perf_pmu *pmu) 2060eec11310SNamhyung Kim { 2061eec11310SNamhyung Kim perf_pmu__del_formats(&pmu->format); 2062eec11310SNamhyung Kim perf_pmu__del_aliases(pmu); 2063eec11310SNamhyung Kim perf_pmu__del_caps(pmu); 2064eec11310SNamhyung Kim 2065eec11310SNamhyung Kim perf_cpu_map__put(pmu->cpus); 2066eec11310SNamhyung Kim 2067efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->name); 2068efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->alias_name); 2069b7823045SIan Rogers zfree(&pmu->id); 2070eec11310SNamhyung Kim free(pmu); 2071eec11310SNamhyung Kim } 2072