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" 223d88aec0SIan Rogers #include "pmu-bison.h" 233d88aec0SIan Rogers #include "pmu-flex.h" 24cd82a32eSJiri Olsa #include "parse-events.h" 25e5c6109fSIan Rogers #include "print-events.h" 26933f82ffSSukadev Bhattiprolu #include "header.h" 27a067558eSArnaldo Carvalho de Melo #include "string2.h" 28fa0d9846SArnaldo Carvalho de Melo #include "strbuf.h" 29d9664582SAndi Kleen #include "fncache.h" 306593f019SJames Clark #include "util/evsel_config.h" 31cd82a32eSJiri Olsa 32e46fc8d9SArnaldo Carvalho de Melo struct perf_pmu perf_pmu__fake; 33e46fc8d9SArnaldo Carvalho de Melo 34fe13d43dSIan Rogers /** 35fe13d43dSIan Rogers * struct perf_pmu_format - Values from a format file read from 36fe13d43dSIan Rogers * <sysfs>/devices/cpu/format/ held in struct perf_pmu. 37fe13d43dSIan Rogers * 38fe13d43dSIan Rogers * For example, the contents of <sysfs>/devices/cpu/format/event may be 39fe13d43dSIan Rogers * "config:0-7" and will be represented here as name="event", 40fe13d43dSIan Rogers * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. 41fe13d43dSIan Rogers */ 42ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 43fe13d43dSIan Rogers /** @name: The modifier/file name. */ 44ab1bf653SArnaldo Carvalho de Melo char *name; 45fe13d43dSIan Rogers /** 46fe13d43dSIan Rogers * @value : Which config value the format relates to. Supported values 47fe13d43dSIan Rogers * are from PERF_PMU_FORMAT_VALUE_CONFIG to 48fe13d43dSIan Rogers * PERF_PMU_FORMAT_VALUE_CONFIG_END. 49fe13d43dSIan Rogers */ 50ab1bf653SArnaldo Carvalho de Melo int value; 51fe13d43dSIan Rogers /** @bits: Which config bits are set by this format value. */ 52ab1bf653SArnaldo Carvalho de Melo DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 53fe13d43dSIan Rogers /** @list: Element on list within struct perf_pmu. */ 54ab1bf653SArnaldo Carvalho de Melo struct list_head list; 55ab1bf653SArnaldo Carvalho de Melo }; 56ab1bf653SArnaldo Carvalho de Melo 57cd82a32eSJiri Olsa /* 58cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 59cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 60cd82a32eSJiri Olsa */ 61e293a5e8SNamhyung Kim int perf_pmu__format_parse(int dirfd, struct list_head *head) 62cd82a32eSJiri Olsa { 63cd82a32eSJiri Olsa struct dirent *evt_ent; 64cd82a32eSJiri Olsa DIR *format_dir; 65cd82a32eSJiri Olsa int ret = 0; 66cd82a32eSJiri Olsa 67e293a5e8SNamhyung Kim format_dir = fdopendir(dirfd); 68cd82a32eSJiri Olsa if (!format_dir) 69cd82a32eSJiri Olsa return -EINVAL; 70cd82a32eSJiri Olsa 71cd82a32eSJiri Olsa while (!ret && (evt_ent = readdir(format_dir))) { 72cd82a32eSJiri Olsa char *name = evt_ent->d_name; 73e293a5e8SNamhyung Kim int fd; 743d88aec0SIan Rogers void *scanner; 753d88aec0SIan Rogers FILE *file; 76cd82a32eSJiri Olsa 77cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 78cd82a32eSJiri Olsa continue; 79cd82a32eSJiri Olsa 80cd82a32eSJiri Olsa 81cd82a32eSJiri Olsa ret = -EINVAL; 82e293a5e8SNamhyung Kim fd = openat(dirfd, name, O_RDONLY); 83e293a5e8SNamhyung Kim if (fd < 0) 84cd82a32eSJiri Olsa break; 85cd82a32eSJiri Olsa 863d88aec0SIan Rogers file = fdopen(fd, "r"); 873d88aec0SIan Rogers if (!file) { 883d88aec0SIan Rogers close(fd); 893d88aec0SIan Rogers break; 903d88aec0SIan Rogers } 913d88aec0SIan Rogers 923d88aec0SIan Rogers ret = perf_pmu_lex_init(&scanner); 933d88aec0SIan Rogers if (ret) { 943d88aec0SIan Rogers fclose(file); 953d88aec0SIan Rogers break; 963d88aec0SIan Rogers } 973d88aec0SIan Rogers 983d88aec0SIan Rogers perf_pmu_set_in(file, scanner); 993d88aec0SIan Rogers ret = perf_pmu_parse(head, name, scanner); 1003d88aec0SIan Rogers perf_pmu_lex_destroy(scanner); 1013d88aec0SIan Rogers fclose(file); 102cd82a32eSJiri Olsa } 103cd82a32eSJiri Olsa 104cd82a32eSJiri Olsa closedir(format_dir); 105cd82a32eSJiri Olsa return ret; 106cd82a32eSJiri Olsa } 107cd82a32eSJiri Olsa 108cd82a32eSJiri Olsa /* 109cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 110cd82a32eSJiri Olsa * located at: 111cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 112cd82a32eSJiri Olsa */ 113e293a5e8SNamhyung Kim static int pmu_format(int dirfd, const char *name, struct list_head *format) 114cd82a32eSJiri Olsa { 115e293a5e8SNamhyung Kim int fd; 116cd82a32eSJiri Olsa 117e293a5e8SNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "format", O_DIRECTORY); 118e293a5e8SNamhyung Kim if (fd < 0) 119d9664582SAndi Kleen return 0; 120cd82a32eSJiri Olsa 121e293a5e8SNamhyung Kim /* it'll close the fd */ 122e293a5e8SNamhyung Kim if (perf_pmu__format_parse(fd, format)) 123cd82a32eSJiri Olsa return -1; 124cd82a32eSJiri Olsa 125cd82a32eSJiri Olsa return 0; 126cd82a32eSJiri Olsa } 127cd82a32eSJiri Olsa 128a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval) 129d02fc6bcSAndi Kleen { 130d02fc6bcSAndi Kleen char *lc; 131d02fc6bcSAndi Kleen int ret = 0; 132d02fc6bcSAndi Kleen 133d02fc6bcSAndi Kleen /* 134d02fc6bcSAndi Kleen * save current locale 135d02fc6bcSAndi Kleen */ 136d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 137d02fc6bcSAndi Kleen 138d02fc6bcSAndi Kleen /* 139d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 140d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 141d02fc6bcSAndi Kleen * call below. 142d02fc6bcSAndi Kleen */ 143d02fc6bcSAndi Kleen lc = strdup(lc); 144d02fc6bcSAndi Kleen if (!lc) { 145d02fc6bcSAndi Kleen ret = -ENOMEM; 146d02fc6bcSAndi Kleen goto out; 147d02fc6bcSAndi Kleen } 148d02fc6bcSAndi Kleen 149d02fc6bcSAndi Kleen /* 150d02fc6bcSAndi Kleen * force to C locale to ensure kernel 151d02fc6bcSAndi Kleen * scale string is converted correctly. 152d02fc6bcSAndi Kleen * kernel uses default C locale. 153d02fc6bcSAndi Kleen */ 154d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 155d02fc6bcSAndi Kleen 156d02fc6bcSAndi Kleen *sval = strtod(scale, end); 157d02fc6bcSAndi Kleen 158d02fc6bcSAndi Kleen out: 159d02fc6bcSAndi Kleen /* restore locale */ 160d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 161d02fc6bcSAndi Kleen free(lc); 162d02fc6bcSAndi Kleen return ret; 163d02fc6bcSAndi Kleen } 164d02fc6bcSAndi Kleen 165e293a5e8SNamhyung Kim static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, int dirfd, char *name) 166410136f5SStephane Eranian { 167410136f5SStephane Eranian struct stat st; 168410136f5SStephane Eranian ssize_t sret; 169410136f5SStephane Eranian char scale[128]; 170410136f5SStephane Eranian int fd, ret = -1; 171410136f5SStephane Eranian char path[PATH_MAX]; 172410136f5SStephane Eranian 173e293a5e8SNamhyung Kim scnprintf(path, PATH_MAX, "%s.scale", name); 174410136f5SStephane Eranian 175e293a5e8SNamhyung Kim fd = openat(dirfd, path, O_RDONLY); 176410136f5SStephane Eranian if (fd == -1) 177410136f5SStephane Eranian return -1; 178410136f5SStephane Eranian 179410136f5SStephane Eranian if (fstat(fd, &st) < 0) 180410136f5SStephane Eranian goto error; 181410136f5SStephane Eranian 182410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 183410136f5SStephane Eranian if (sret < 0) 184410136f5SStephane Eranian goto error; 185410136f5SStephane Eranian 1869ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 1879ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 1889ecae065SMadhavan Srinivasan else 189410136f5SStephane Eranian scale[sret] = '\0'; 1909ecae065SMadhavan Srinivasan 191a55ab7c4SJin Yao ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); 192410136f5SStephane Eranian error: 193410136f5SStephane Eranian close(fd); 194410136f5SStephane Eranian return ret; 195410136f5SStephane Eranian } 196410136f5SStephane Eranian 197e293a5e8SNamhyung Kim static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, int dirfd, char *name) 198410136f5SStephane Eranian { 199410136f5SStephane Eranian char path[PATH_MAX]; 200410136f5SStephane Eranian ssize_t sret; 201410136f5SStephane Eranian int fd; 202410136f5SStephane Eranian 203e293a5e8SNamhyung Kim scnprintf(path, PATH_MAX, "%s.unit", name); 204410136f5SStephane Eranian 205e293a5e8SNamhyung Kim fd = openat(dirfd, path, O_RDONLY); 206410136f5SStephane Eranian if (fd == -1) 207410136f5SStephane Eranian return -1; 208410136f5SStephane Eranian 209410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 210410136f5SStephane Eranian if (sret < 0) 211410136f5SStephane Eranian goto error; 212410136f5SStephane Eranian 213410136f5SStephane Eranian close(fd); 214410136f5SStephane Eranian 2159ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 2169ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 2179ecae065SMadhavan Srinivasan else 218410136f5SStephane Eranian alias->unit[sret] = '\0'; 219410136f5SStephane Eranian 220410136f5SStephane Eranian return 0; 221410136f5SStephane Eranian error: 222410136f5SStephane Eranian close(fd); 223410136f5SStephane Eranian alias->unit[0] = '\0'; 224410136f5SStephane Eranian return -1; 225410136f5SStephane Eranian } 226410136f5SStephane Eranian 227044330c1SMatt Fleming static int 228e293a5e8SNamhyung Kim perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, int dirfd, char *name) 229044330c1SMatt Fleming { 230044330c1SMatt Fleming char path[PATH_MAX]; 231044330c1SMatt Fleming int fd; 232044330c1SMatt Fleming 233e293a5e8SNamhyung Kim scnprintf(path, PATH_MAX, "%s.per-pkg", name); 234044330c1SMatt Fleming 235e293a5e8SNamhyung Kim fd = openat(dirfd, path, O_RDONLY); 236044330c1SMatt Fleming if (fd == -1) 237044330c1SMatt Fleming return -1; 238044330c1SMatt Fleming 239044330c1SMatt Fleming close(fd); 240044330c1SMatt Fleming 241044330c1SMatt Fleming alias->per_pkg = true; 242044330c1SMatt Fleming return 0; 243044330c1SMatt Fleming } 244044330c1SMatt Fleming 2451d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 246e293a5e8SNamhyung Kim int dirfd, char *name) 2471d9e446bSJiri Olsa { 2481d9e446bSJiri Olsa char path[PATH_MAX]; 2491d9e446bSJiri Olsa int fd; 2501d9e446bSJiri Olsa 251e293a5e8SNamhyung Kim scnprintf(path, PATH_MAX, "%s.snapshot", name); 2521d9e446bSJiri Olsa 253e293a5e8SNamhyung Kim fd = openat(dirfd, path, O_RDONLY); 2541d9e446bSJiri Olsa if (fd == -1) 2551d9e446bSJiri Olsa return -1; 2561d9e446bSJiri Olsa 2571d9e446bSJiri Olsa alias->snapshot = true; 2581d9e446bSJiri Olsa close(fd); 2591d9e446bSJiri Olsa return 0; 2601d9e446bSJiri Olsa } 2611d9e446bSJiri Olsa 2626dde6429SThomas Richter static void perf_pmu_assign_str(char *name, const char *field, char **old_str, 2636dde6429SThomas Richter char **new_str) 2646dde6429SThomas Richter { 2656dde6429SThomas Richter if (!*old_str) 2666dde6429SThomas Richter goto set_new; 2676dde6429SThomas Richter 2686dde6429SThomas Richter if (*new_str) { /* Have new string, check with old */ 2696dde6429SThomas Richter if (strcasecmp(*old_str, *new_str)) 2706dde6429SThomas Richter pr_debug("alias %s differs in field '%s'\n", 2716dde6429SThomas Richter name, field); 2726dde6429SThomas Richter zfree(old_str); 2736dde6429SThomas Richter } else /* Nothing new --> keep old string */ 2746dde6429SThomas Richter return; 2756dde6429SThomas Richter set_new: 2766dde6429SThomas Richter *old_str = *new_str; 2776dde6429SThomas Richter *new_str = NULL; 2786dde6429SThomas Richter } 2796dde6429SThomas Richter 2806dde6429SThomas Richter static void perf_pmu_update_alias(struct perf_pmu_alias *old, 2816dde6429SThomas Richter struct perf_pmu_alias *newalias) 2826dde6429SThomas Richter { 2836dde6429SThomas Richter perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); 2846dde6429SThomas Richter perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, 2856dde6429SThomas Richter &newalias->long_desc); 2866dde6429SThomas Richter perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); 2876dde6429SThomas Richter perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); 2886dde6429SThomas Richter old->scale = newalias->scale; 2896dde6429SThomas Richter old->per_pkg = newalias->per_pkg; 2906dde6429SThomas Richter old->snapshot = newalias->snapshot; 2916dde6429SThomas Richter memcpy(old->unit, newalias->unit, sizeof(old->unit)); 2926dde6429SThomas Richter } 2936dde6429SThomas Richter 2946dde6429SThomas Richter /* Delete an alias entry. */ 29522fe5a25SNamhyung Kim void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 2966dde6429SThomas Richter { 2976dde6429SThomas Richter zfree(&newalias->name); 2986dde6429SThomas Richter zfree(&newalias->desc); 2996dde6429SThomas Richter zfree(&newalias->long_desc); 3006dde6429SThomas Richter zfree(&newalias->topic); 3016dde6429SThomas Richter zfree(&newalias->str); 30232705de7SJin Yao zfree(&newalias->pmu_name); 3036dde6429SThomas Richter parse_events_terms__purge(&newalias->terms); 3046dde6429SThomas Richter free(newalias); 3056dde6429SThomas Richter } 3066dde6429SThomas Richter 307eec11310SNamhyung Kim static void perf_pmu__del_aliases(struct perf_pmu *pmu) 308eec11310SNamhyung Kim { 309eec11310SNamhyung Kim struct perf_pmu_alias *alias, *tmp; 310eec11310SNamhyung Kim 311eec11310SNamhyung Kim list_for_each_entry_safe(alias, tmp, &pmu->aliases, list) { 312eec11310SNamhyung Kim list_del(&alias->list); 313eec11310SNamhyung Kim perf_pmu_free_alias(alias); 314eec11310SNamhyung Kim } 315eec11310SNamhyung Kim } 316eec11310SNamhyung Kim 3176dde6429SThomas Richter /* Merge an alias, search in alias list. If this name is already 3186dde6429SThomas Richter * present merge both of them to combine all information. 3196dde6429SThomas Richter */ 3206dde6429SThomas Richter static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, 3216dde6429SThomas Richter struct list_head *alist) 3226dde6429SThomas Richter { 3236dde6429SThomas Richter struct perf_pmu_alias *a; 3246dde6429SThomas Richter 3256dde6429SThomas Richter list_for_each_entry(a, alist, list) { 3266dde6429SThomas Richter if (!strcasecmp(newalias->name, a->name)) { 32732705de7SJin Yao if (newalias->pmu_name && a->pmu_name && 32832705de7SJin Yao !strcasecmp(newalias->pmu_name, a->pmu_name)) { 32932705de7SJin Yao continue; 33032705de7SJin Yao } 3316dde6429SThomas Richter perf_pmu_update_alias(a, newalias); 3326dde6429SThomas Richter perf_pmu_free_alias(newalias); 3336dde6429SThomas Richter return true; 3346dde6429SThomas Richter } 3356dde6429SThomas Richter } 3366dde6429SThomas Richter return false; 3376dde6429SThomas Richter } 3386dde6429SThomas Richter 339e293a5e8SNamhyung Kim static int __perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, 34047f572aaSIan Rogers char *desc, char *val, const struct pmu_event *pe) 341a6146d50SZheng Yan { 3420c24d6fbSThomas Richter struct parse_events_term *term; 3435c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 344a6146d50SZheng Yan int ret; 3450c24d6fbSThomas Richter char newval[256]; 346330f40a0SIan Rogers const char *long_desc = NULL, *topic = NULL, *unit = NULL, *pmu_name = NULL; 347bd680861SIan Rogers bool deprecated = false, perpkg = false; 348eab35953SJin Yao 349eab35953SJin Yao if (pe) { 350330f40a0SIan Rogers long_desc = pe->long_desc; 351330f40a0SIan Rogers topic = pe->topic; 352330f40a0SIan Rogers unit = pe->unit; 353bd680861SIan Rogers perpkg = pe->perpkg; 3549ed8b7dcSIan Rogers deprecated = pe->deprecated; 355330f40a0SIan Rogers pmu_name = pe->pmu; 356eab35953SJin Yao } 357a6146d50SZheng Yan 358a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 359a6146d50SZheng Yan if (!alias) 360a6146d50SZheng Yan return -ENOMEM; 361a6146d50SZheng Yan 362a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 363410136f5SStephane Eranian alias->scale = 1.0; 364410136f5SStephane Eranian alias->unit[0] = '\0'; 365bd680861SIan Rogers alias->per_pkg = perpkg; 36684530920SStephane Eranian alias->snapshot = false; 3679ed8b7dcSIan Rogers alias->deprecated = deprecated; 368410136f5SStephane Eranian 36970c646e0SSukadev Bhattiprolu ret = parse_events_terms(&alias->terms, val); 370a6146d50SZheng Yan if (ret) { 37170c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 372a6146d50SZheng Yan free(alias); 373a6146d50SZheng Yan return ret; 374a6146d50SZheng Yan } 375a6146d50SZheng Yan 3760c24d6fbSThomas Richter /* Scan event and remove leading zeroes, spaces, newlines, some 3770c24d6fbSThomas Richter * platforms have terms specified as 3780c24d6fbSThomas Richter * event=0x0091 (read from files ../<PMU>/events/<FILE> 3790c24d6fbSThomas Richter * and terms specified as event=0x91 (read from JSON files). 3800c24d6fbSThomas Richter * 3810c24d6fbSThomas Richter * Rebuild string to make alias->str member comparable. 3820c24d6fbSThomas Richter */ 3830c24d6fbSThomas Richter memset(newval, 0, sizeof(newval)); 3840c24d6fbSThomas Richter ret = 0; 3850c24d6fbSThomas Richter list_for_each_entry(term, &alias->terms, list) { 3860c24d6fbSThomas Richter if (ret) 3870c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3880c24d6fbSThomas Richter ","); 3890c24d6fbSThomas Richter if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 3900c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3910c24d6fbSThomas Richter "%s=%#x", term->config, term->val.num); 3920c24d6fbSThomas Richter else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 3930c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3940c24d6fbSThomas Richter "%s=%s", term->config, term->val.str); 3950c24d6fbSThomas Richter } 3960c24d6fbSThomas Richter 397a6146d50SZheng Yan alias->name = strdup(name); 398e293a5e8SNamhyung Kim if (dirfd >= 0) { 399410136f5SStephane Eranian /* 400410136f5SStephane Eranian * load unit name and scale if available 401410136f5SStephane Eranian */ 402e293a5e8SNamhyung Kim perf_pmu__parse_unit(alias, dirfd, name); 403e293a5e8SNamhyung Kim perf_pmu__parse_scale(alias, dirfd, name); 404e293a5e8SNamhyung Kim perf_pmu__parse_per_pkg(alias, dirfd, name); 405e293a5e8SNamhyung Kim perf_pmu__parse_snapshot(alias, dirfd, name); 40670c646e0SSukadev Bhattiprolu } 407410136f5SStephane Eranian 40808e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 409c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 410c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 411dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 412fedb2b51SAndi Kleen if (unit) { 413330f40a0SIan Rogers if (perf_pmu__convert_scale(unit, (char **)&unit, &alias->scale) < 0) 414fedb2b51SAndi Kleen return -1; 415fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 416fedb2b51SAndi Kleen } 4170c24d6fbSThomas Richter alias->str = strdup(newval); 41832705de7SJin Yao alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 419f2361024SAndi Kleen 4206dde6429SThomas Richter if (!perf_pmu_merge_alias(alias, list)) 421a6146d50SZheng Yan list_add_tail(&alias->list, list); 422410136f5SStephane Eranian 423a6146d50SZheng Yan return 0; 424a6146d50SZheng Yan } 425a6146d50SZheng Yan 426e293a5e8SNamhyung Kim static int perf_pmu__new_alias(struct list_head *list, int dirfd, char *name, FILE *file) 42770c646e0SSukadev Bhattiprolu { 42870c646e0SSukadev Bhattiprolu char buf[256]; 42970c646e0SSukadev Bhattiprolu int ret; 43070c646e0SSukadev Bhattiprolu 43170c646e0SSukadev Bhattiprolu ret = fread(buf, 1, sizeof(buf), file); 43270c646e0SSukadev Bhattiprolu if (ret == 0) 43370c646e0SSukadev Bhattiprolu return -EINVAL; 43470c646e0SSukadev Bhattiprolu 43570c646e0SSukadev Bhattiprolu buf[ret] = 0; 43670c646e0SSukadev Bhattiprolu 437ea23ac73SThomas Richter /* Remove trailing newline from sysfs file */ 43813c230abSArnaldo Carvalho de Melo strim(buf); 439ea23ac73SThomas Richter 440e293a5e8SNamhyung Kim return __perf_pmu__new_alias(list, dirfd, name, NULL, buf, NULL); 44170c646e0SSukadev Bhattiprolu } 44270c646e0SSukadev Bhattiprolu 44346441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 44446441bdcSMatt Fleming { 44546441bdcSMatt Fleming size_t len; 44646441bdcSMatt Fleming 44746441bdcSMatt Fleming len = strlen(name); 44846441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 44946441bdcSMatt Fleming return true; 45046441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 45146441bdcSMatt Fleming return true; 452044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 453044330c1SMatt Fleming return true; 4541d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 4551d9e446bSJiri Olsa return true; 45646441bdcSMatt Fleming 45746441bdcSMatt Fleming return false; 45846441bdcSMatt Fleming } 45946441bdcSMatt Fleming 460a6146d50SZheng Yan /* 461a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 462a6146d50SZheng Yan * specified in 'dir' parameter. 463a6146d50SZheng Yan */ 464e293a5e8SNamhyung Kim static int pmu_aliases_parse(int dirfd, struct list_head *head) 465a6146d50SZheng Yan { 466a6146d50SZheng Yan struct dirent *evt_ent; 467a6146d50SZheng Yan DIR *event_dir; 468e293a5e8SNamhyung Kim int fd; 469a6146d50SZheng Yan 470e293a5e8SNamhyung Kim event_dir = fdopendir(dirfd); 471a6146d50SZheng Yan if (!event_dir) 472a6146d50SZheng Yan return -EINVAL; 473a6146d50SZheng Yan 474940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 475a6146d50SZheng Yan char *name = evt_ent->d_name; 476a6146d50SZheng Yan FILE *file; 477a6146d50SZheng Yan 478a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 479a6146d50SZheng Yan continue; 480a6146d50SZheng Yan 481410136f5SStephane Eranian /* 48246441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 483410136f5SStephane Eranian */ 48446441bdcSMatt Fleming if (pmu_alias_info_file(name)) 485410136f5SStephane Eranian continue; 486410136f5SStephane Eranian 487e293a5e8SNamhyung Kim fd = openat(dirfd, name, O_RDONLY); 4880ea8920eSIan Rogers if (fd == -1) { 4890ea8920eSIan Rogers pr_debug("Cannot open %s\n", name); 4900ea8920eSIan Rogers continue; 4910ea8920eSIan Rogers } 492e293a5e8SNamhyung Kim file = fdopen(fd, "r"); 493940db6dcSAndi Kleen if (!file) { 4940ea8920eSIan Rogers close(fd); 495940db6dcSAndi Kleen continue; 496940db6dcSAndi Kleen } 497410136f5SStephane Eranian 498e293a5e8SNamhyung Kim if (perf_pmu__new_alias(head, dirfd, name, file) < 0) 499940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 500a6146d50SZheng Yan fclose(file); 501a6146d50SZheng Yan } 502a6146d50SZheng Yan 503a6146d50SZheng Yan closedir(event_dir); 504940db6dcSAndi Kleen return 0; 505a6146d50SZheng Yan } 506a6146d50SZheng Yan 507a6146d50SZheng Yan /* 508a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 509a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 510a6146d50SZheng Yan */ 511e293a5e8SNamhyung Kim static int pmu_aliases(int dirfd, const char *name, struct list_head *head) 512a6146d50SZheng Yan { 513e293a5e8SNamhyung Kim int fd; 514a6146d50SZheng Yan 515e293a5e8SNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "events", O_DIRECTORY); 516e293a5e8SNamhyung Kim if (fd < 0) 517d9664582SAndi Kleen return 0; 518a6146d50SZheng Yan 519e293a5e8SNamhyung Kim /* it'll close the fd */ 520e293a5e8SNamhyung Kim if (pmu_aliases_parse(fd, head)) 521a6146d50SZheng Yan return -1; 522a6146d50SZheng Yan 523a6146d50SZheng Yan return 0; 524a6146d50SZheng Yan } 525a6146d50SZheng Yan 5265c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 527a6146d50SZheng Yan struct list_head *terms) 528a6146d50SZheng Yan { 5297c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 530a6146d50SZheng Yan LIST_HEAD(list); 531a6146d50SZheng Yan int ret; 532a6146d50SZheng Yan 533a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 5347c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 535a6146d50SZheng Yan if (ret) { 536682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 537a6146d50SZheng Yan return ret; 538a6146d50SZheng Yan } 539c2f1ceadSAndi Kleen /* 540c2f1ceadSAndi Kleen * Weak terms don't override command line options, 541c2f1ceadSAndi Kleen * which we don't want for implicit terms in aliases. 542c2f1ceadSAndi Kleen */ 543c2f1ceadSAndi Kleen cloned->weak = true; 5447c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 545a6146d50SZheng Yan } 546a6146d50SZheng Yan list_splice(&list, terms); 547a6146d50SZheng Yan return 0; 548a6146d50SZheng Yan } 549a6146d50SZheng Yan 55066ec1191SMark Rutland /* 55166ec1191SMark Rutland * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 55266ec1191SMark Rutland * may have a "cpus" file. 55366ec1191SMark Rutland */ 5543a69672eSNamhyung Kim static struct perf_cpu_map *pmu_cpumask(int dirfd, const char *name) 5557ae92e74SYan, Zheng { 556f854839bSJiri Olsa struct perf_cpu_map *cpus; 5577e3fcffeSMark Rutland const char *templates[] = { 558d50a79cdSJames Clark "cpumask", 559d50a79cdSJames Clark "cpus", 5607e3fcffeSMark Rutland NULL 5617e3fcffeSMark Rutland }; 5627e3fcffeSMark Rutland const char **template; 563d50a79cdSJames Clark char pmu_name[PATH_MAX]; 564d50a79cdSJames Clark struct perf_pmu pmu = {.name = pmu_name}; 565d50a79cdSJames Clark FILE *file; 5667ae92e74SYan, Zheng 567d50a79cdSJames Clark strlcpy(pmu_name, name, sizeof(pmu_name)); 5687e3fcffeSMark Rutland for (template = templates; *template; template++) { 5693a69672eSNamhyung Kim file = perf_pmu__open_file_at(&pmu, dirfd, *template); 570d50a79cdSJames Clark if (!file) 571d50a79cdSJames Clark continue; 572d50a79cdSJames Clark cpus = perf_cpu_map__read(file); 5733a69672eSNamhyung Kim fclose(file); 57466ec1191SMark Rutland if (cpus) 57566ec1191SMark Rutland return cpus; 5767e3fcffeSMark Rutland } 5777ae92e74SYan, Zheng 578a0c41caeSIan Rogers return !strcmp(name, "cpu") ? perf_cpu_map__get(cpu_map__online()) : NULL; 57966ec1191SMark Rutland } 5807ae92e74SYan, Zheng 5813a69672eSNamhyung Kim static bool pmu_is_uncore(int dirfd, const char *name) 58266ec1191SMark Rutland { 5833a69672eSNamhyung Kim int fd; 5847ae92e74SYan, Zheng 5853a69672eSNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, name, "cpumask", O_PATH); 5863a69672eSNamhyung Kim if (fd < 0) 5873a69672eSNamhyung Kim return false; 5883a69672eSNamhyung Kim 5893a69672eSNamhyung Kim close(fd); 5903a69672eSNamhyung Kim return true; 5917ae92e74SYan, Zheng } 5927ae92e74SYan, Zheng 59351d54847SJohn Garry static char *pmu_id(const char *name) 59451d54847SJohn Garry { 59551d54847SJohn Garry char path[PATH_MAX], *str; 59651d54847SJohn Garry size_t len; 59751d54847SJohn Garry 5985f2c8efaSJames Clark perf_pmu__pathname_scnprintf(path, sizeof(path), name, "identifier"); 59951d54847SJohn Garry 6005f2c8efaSJames Clark if (filename__read_str(path, &str, &len) < 0) 60151d54847SJohn Garry return NULL; 60251d54847SJohn Garry 60351d54847SJohn Garry str[len - 1] = 0; /* remove line feed */ 60451d54847SJohn Garry 60551d54847SJohn Garry return str; 60651d54847SJohn Garry } 60751d54847SJohn Garry 6084bf7e81aSIan Rogers /** 6094bf7e81aSIan Rogers * is_sysfs_pmu_core() - PMU CORE devices have different name other than cpu in 6104bf7e81aSIan Rogers * sysfs on some platforms like ARM or Intel hybrid. Looking for 6114bf7e81aSIan Rogers * possible the cpus file in sysfs files to identify whether this is a 6124bf7e81aSIan Rogers * core device. 6134bf7e81aSIan Rogers * @name: The PMU name such as "cpu_atom". 61414b22ae0SGanapatrao Kulkarni */ 6154bf7e81aSIan Rogers static int is_sysfs_pmu_core(const char *name) 61614b22ae0SGanapatrao Kulkarni { 61714b22ae0SGanapatrao Kulkarni char path[PATH_MAX]; 61814b22ae0SGanapatrao Kulkarni 619f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(path, sizeof(path), name, "cpus")) 62014b22ae0SGanapatrao Kulkarni return 0; 621d9664582SAndi Kleen return file_available(path); 62214b22ae0SGanapatrao Kulkarni } 62314b22ae0SGanapatrao Kulkarni 62429be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu) 625d77ade9fSAndi Kleen { 626d77ade9fSAndi Kleen char *cpuid; 627d77ade9fSAndi Kleen static bool printed; 628d77ade9fSAndi Kleen 629d77ade9fSAndi Kleen cpuid = getenv("PERF_CPUID"); 630d77ade9fSAndi Kleen if (cpuid) 631d77ade9fSAndi Kleen cpuid = strdup(cpuid); 632d77ade9fSAndi Kleen if (!cpuid) 63354e32dc0SGanapatrao Kulkarni cpuid = get_cpuid_str(pmu); 634d77ade9fSAndi Kleen if (!cpuid) 635d77ade9fSAndi Kleen return NULL; 636d77ade9fSAndi Kleen 637d77ade9fSAndi Kleen if (!printed) { 638d77ade9fSAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 639d77ade9fSAndi Kleen printed = true; 640d77ade9fSAndi Kleen } 641d77ade9fSAndi Kleen return cpuid; 642d77ade9fSAndi Kleen } 643d77ade9fSAndi Kleen 6441ba3752aSIan Rogers __weak const struct pmu_events_table *pmu_events_table__find(void) 645e126bef5SJohn Garry { 64696d2a746SIan Rogers return perf_pmu__find_events_table(NULL); 647e126bef5SJohn Garry } 648e126bef5SJohn Garry 649f8ea2c15SIan Rogers __weak const struct pmu_metrics_table *pmu_metrics_table__find(void) 650f8ea2c15SIan Rogers { 651f8ea2c15SIan Rogers return perf_pmu__find_metrics_table(NULL); 652f8ea2c15SIan Rogers } 653f8ea2c15SIan Rogers 654240e6fd0SIan Rogers /** 655240e6fd0SIan Rogers * perf_pmu__match_ignoring_suffix - Does the pmu_name match tok ignoring any 656240e6fd0SIan Rogers * trailing suffix? The Suffix must be in form 657240e6fd0SIan Rogers * tok_{digits}, or tok{digits}. 658240e6fd0SIan Rogers * @pmu_name: The pmu_name with possible suffix. 659240e6fd0SIan Rogers * @tok: The possible match to pmu_name without suffix. 660c07d5c92SJohn Garry */ 661240e6fd0SIan Rogers static bool perf_pmu__match_ignoring_suffix(const char *pmu_name, const char *tok) 662c47a5599SJin Yao { 663c07d5c92SJohn Garry const char *p; 664c47a5599SJin Yao 665c47a5599SJin Yao if (strncmp(pmu_name, tok, strlen(tok))) 666c47a5599SJin Yao return false; 667c47a5599SJin Yao 668c47a5599SJin Yao p = pmu_name + strlen(tok); 669c47a5599SJin Yao if (*p == 0) 670c47a5599SJin Yao return true; 671c47a5599SJin Yao 672c07d5c92SJohn Garry if (*p == '_') 673c47a5599SJin Yao ++p; 674c07d5c92SJohn Garry 675c07d5c92SJohn Garry /* Ensure we end in a number */ 676c07d5c92SJohn Garry while (1) { 677c07d5c92SJohn Garry if (!isdigit(*p)) 678c47a5599SJin Yao return false; 679c07d5c92SJohn Garry if (*(++p) == 0) 680c07d5c92SJohn Garry break; 681c07d5c92SJohn Garry } 682c47a5599SJin Yao 683c47a5599SJin Yao return true; 684c47a5599SJin Yao } 685c47a5599SJin Yao 686240e6fd0SIan Rogers /** 687240e6fd0SIan Rogers * pmu_uncore_alias_match - does name match the PMU name? 688240e6fd0SIan Rogers * @pmu_name: the json struct pmu_event name. This may lack a suffix (which 689240e6fd0SIan Rogers * matches) or be of the form "socket,pmuname" which will match 690240e6fd0SIan Rogers * "socketX_pmunameY". 691240e6fd0SIan Rogers * @name: a real full PMU name as from sysfs. 692240e6fd0SIan Rogers */ 693240e6fd0SIan Rogers static bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 694730670b1SJohn Garry { 695730670b1SJohn Garry char *tmp = NULL, *tok, *str; 696730670b1SJohn Garry bool res; 697730670b1SJohn Garry 698240e6fd0SIan Rogers if (strchr(pmu_name, ',') == NULL) 699240e6fd0SIan Rogers return perf_pmu__match_ignoring_suffix(name, pmu_name); 700240e6fd0SIan Rogers 701730670b1SJohn Garry str = strdup(pmu_name); 702730670b1SJohn Garry if (!str) 703730670b1SJohn Garry return false; 704730670b1SJohn Garry 705730670b1SJohn Garry /* 706730670b1SJohn Garry * uncore alias may be from different PMU with common prefix 707730670b1SJohn Garry */ 708730670b1SJohn Garry tok = strtok_r(str, ",", &tmp); 709730670b1SJohn Garry if (strncmp(pmu_name, tok, strlen(tok))) { 710730670b1SJohn Garry res = false; 711730670b1SJohn Garry goto out; 712730670b1SJohn Garry } 713730670b1SJohn Garry 714730670b1SJohn Garry /* 715730670b1SJohn Garry * Match more complex aliases where the alias name is a comma-delimited 716730670b1SJohn Garry * list of tokens, orderly contained in the matching PMU name. 717730670b1SJohn Garry * 718730670b1SJohn Garry * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 719730670b1SJohn Garry * match "socket" in "socketX_pmunameY" and then "pmuname" in 720730670b1SJohn Garry * "pmunameY". 721730670b1SJohn Garry */ 722c07d5c92SJohn Garry while (1) { 723c07d5c92SJohn Garry char *next_tok = strtok_r(NULL, ",", &tmp); 724c07d5c92SJohn Garry 725730670b1SJohn Garry name = strstr(name, tok); 726c07d5c92SJohn Garry if (!name || 727240e6fd0SIan Rogers (!next_tok && !perf_pmu__match_ignoring_suffix(name, tok))) { 728730670b1SJohn Garry res = false; 729730670b1SJohn Garry goto out; 730730670b1SJohn Garry } 731c07d5c92SJohn Garry if (!next_tok) 732c07d5c92SJohn Garry break; 733c07d5c92SJohn Garry tok = next_tok; 734c07d5c92SJohn Garry name += strlen(tok); 735730670b1SJohn Garry } 736730670b1SJohn Garry 737730670b1SJohn Garry res = true; 738730670b1SJohn Garry out: 739730670b1SJohn Garry free(str); 740730670b1SJohn Garry return res; 741730670b1SJohn Garry } 742730670b1SJohn Garry 743660842e4SIan Rogers struct pmu_add_cpu_aliases_map_data { 744660842e4SIan Rogers struct list_head *head; 745660842e4SIan Rogers const char *name; 746660842e4SIan Rogers const char *cpu_name; 747660842e4SIan Rogers struct perf_pmu *pmu; 748660842e4SIan Rogers }; 749660842e4SIan Rogers 750660842e4SIan Rogers static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, 7511ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 752660842e4SIan Rogers void *vdata) 753660842e4SIan Rogers { 754660842e4SIan Rogers struct pmu_add_cpu_aliases_map_data *data = vdata; 755660842e4SIan Rogers const char *pname = pe->pmu ? pe->pmu : data->cpu_name; 756660842e4SIan Rogers 757660842e4SIan Rogers if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name)) 758660842e4SIan Rogers goto new_alias; 759660842e4SIan Rogers 760660842e4SIan Rogers if (strcmp(pname, data->name)) 761660842e4SIan Rogers return 0; 762660842e4SIan Rogers 763660842e4SIan Rogers new_alias: 764660842e4SIan Rogers /* need type casts to override 'const' */ 765e293a5e8SNamhyung Kim __perf_pmu__new_alias(data->head, -1, (char *)pe->name, (char *)pe->desc, 766660842e4SIan Rogers (char *)pe->event, pe); 767660842e4SIan Rogers return 0; 768660842e4SIan Rogers } 769660842e4SIan Rogers 770933f82ffSSukadev Bhattiprolu /* 771933f82ffSSukadev Bhattiprolu * From the pmu_events_map, find the table of PMU events that corresponds 772933f82ffSSukadev Bhattiprolu * to the current running CPU. Then, add all PMU events from that table 773933f82ffSSukadev Bhattiprolu * as aliases. 774933f82ffSSukadev Bhattiprolu */ 775eeac7730SIan Rogers void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu, 7761ba3752aSIan Rogers const struct pmu_events_table *table) 777933f82ffSSukadev Bhattiprolu { 778660842e4SIan Rogers struct pmu_add_cpu_aliases_map_data data = { 779660842e4SIan Rogers .head = head, 780660842e4SIan Rogers .name = pmu->name, 7814bf7e81aSIan Rogers .cpu_name = is_sysfs_pmu_core(pmu->name) ? pmu->name : "cpu", 782660842e4SIan Rogers .pmu = pmu, 783660842e4SIan Rogers }; 784fedb2b51SAndi Kleen 785660842e4SIan Rogers pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data); 786933f82ffSSukadev Bhattiprolu } 787933f82ffSSukadev Bhattiprolu 788e45ad701SJohn Garry static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) 789e45ad701SJohn Garry { 7901ba3752aSIan Rogers const struct pmu_events_table *table; 791e45ad701SJohn Garry 79296d2a746SIan Rogers table = perf_pmu__find_events_table(pmu); 793eeac7730SIan Rogers if (!table) 794e45ad701SJohn Garry return; 795e45ad701SJohn Garry 796eeac7730SIan Rogers pmu_add_cpu_aliases_table(head, pmu, table); 797e45ad701SJohn Garry } 798e45ad701SJohn Garry 7994513c719SJohn Garry struct pmu_sys_event_iter_data { 8004513c719SJohn Garry struct list_head *head; 8014513c719SJohn Garry struct perf_pmu *pmu; 8024513c719SJohn Garry }; 8034513c719SJohn Garry 80429be2fe0SIan Rogers static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, 8051ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 80629be2fe0SIan Rogers void *data) 8074513c719SJohn Garry { 8084513c719SJohn Garry struct pmu_sys_event_iter_data *idata = data; 8094513c719SJohn Garry struct perf_pmu *pmu = idata->pmu; 8104513c719SJohn Garry 8114513c719SJohn Garry if (!pe->compat || !pe->pmu) 8124513c719SJohn Garry return 0; 8134513c719SJohn Garry 8144513c719SJohn Garry if (!strcmp(pmu->id, pe->compat) && 8154513c719SJohn Garry pmu_uncore_alias_match(pe->pmu, pmu->name)) { 816e293a5e8SNamhyung Kim __perf_pmu__new_alias(idata->head, -1, 8174513c719SJohn Garry (char *)pe->name, 8184513c719SJohn Garry (char *)pe->desc, 8194513c719SJohn Garry (char *)pe->event, 820eab35953SJin Yao pe); 8214513c719SJohn Garry } 8224513c719SJohn Garry 8234513c719SJohn Garry return 0; 8244513c719SJohn Garry } 8254513c719SJohn Garry 826e199f47fSJohn Garry void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu) 8274513c719SJohn Garry { 8284513c719SJohn Garry struct pmu_sys_event_iter_data idata = { 8294513c719SJohn Garry .head = head, 8304513c719SJohn Garry .pmu = pmu, 8314513c719SJohn Garry }; 8324513c719SJohn Garry 8334513c719SJohn Garry if (!pmu->id) 8344513c719SJohn Garry return; 8354513c719SJohn Garry 8364513c719SJohn Garry pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata); 8374513c719SJohn Garry } 8384513c719SJohn Garry 839c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 840dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 841dc0a6202SAdrian Hunter { 842dc0a6202SAdrian Hunter return NULL; 843dc0a6202SAdrian Hunter } 844dc0a6202SAdrian Hunter 84513d60ba0SKan Liang char * __weak 84613d60ba0SKan Liang pmu_find_real_name(const char *name) 84713d60ba0SKan Liang { 84813d60ba0SKan Liang return (char *)name; 84913d60ba0SKan Liang } 85013d60ba0SKan Liang 85113d60ba0SKan Liang char * __weak 85213d60ba0SKan Liang pmu_find_alias_name(const char *name __maybe_unused) 85313d60ba0SKan Liang { 85413d60ba0SKan Liang return NULL; 85513d60ba0SKan Liang } 85613d60ba0SKan Liang 8573a69672eSNamhyung Kim static int pmu_max_precise(int dirfd, struct perf_pmu *pmu) 85890a86bdeSJiri Olsa { 85990a86bdeSJiri Olsa int max_precise = -1; 86090a86bdeSJiri Olsa 8613a69672eSNamhyung Kim perf_pmu__scan_file_at(pmu, dirfd, "caps/max_precise", "%d", &max_precise); 86290a86bdeSJiri Olsa return max_precise; 86390a86bdeSJiri Olsa } 86490a86bdeSJiri Olsa 8651eaf496eSIan Rogers struct perf_pmu *perf_pmu__lookup(struct list_head *pmus, int dirfd, const char *lookup_name) 866cd82a32eSJiri Olsa { 867cd82a32eSJiri Olsa struct perf_pmu *pmu; 868cd82a32eSJiri Olsa LIST_HEAD(format); 869a6146d50SZheng Yan LIST_HEAD(aliases); 870cd82a32eSJiri Olsa __u32 type; 87113d60ba0SKan Liang char *name = pmu_find_real_name(lookup_name); 87213d60ba0SKan Liang char *alias_name; 87349afa7f6SJin Yao 87449afa7f6SJin Yao /* 875cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 876cd82a32eSJiri Olsa * type value and format definitions. Load both right 877cd82a32eSJiri Olsa * now. 878cd82a32eSJiri Olsa */ 879e293a5e8SNamhyung Kim if (pmu_format(dirfd, name, &format)) 880cd82a32eSJiri Olsa return NULL; 881cd82a32eSJiri Olsa 88215b22ed3SAndi Kleen /* 883f8ad6018SJames Clark * Check the aliases first to avoid unnecessary work. 88415b22ed3SAndi Kleen */ 885e293a5e8SNamhyung Kim if (pmu_aliases(dirfd, name, &aliases)) 8863fded963SJiri Olsa return NULL; 8873fded963SJiri Olsa 888cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 889cd82a32eSJiri Olsa if (!pmu) 890cd82a32eSJiri Olsa return NULL; 891cd82a32eSJiri Olsa 8923a69672eSNamhyung Kim pmu->cpus = pmu_cpumask(dirfd, name); 89354e32dc0SGanapatrao Kulkarni pmu->name = strdup(name); 89413d60ba0SKan Liang if (!pmu->name) 89513d60ba0SKan Liang goto err; 89613d60ba0SKan Liang 897f8ad6018SJames Clark /* Read type, and ensure that type value is successfully assigned (return 1) */ 8983a69672eSNamhyung Kim if (perf_pmu__scan_file_at(pmu, dirfd, "type", "%u", &type) != 1) 899f8ad6018SJames Clark goto err; 900f8ad6018SJames Clark 90113d60ba0SKan Liang alias_name = pmu_find_alias_name(name); 90213d60ba0SKan Liang if (alias_name) { 90313d60ba0SKan Liang pmu->alias_name = strdup(alias_name); 90413d60ba0SKan Liang if (!pmu->alias_name) 90513d60ba0SKan Liang goto err; 90613d60ba0SKan Liang } 90713d60ba0SKan Liang 90854e32dc0SGanapatrao Kulkarni pmu->type = type; 909e20d1f2fSIan Rogers pmu->is_core = is_pmu_core(name); 9103a69672eSNamhyung Kim pmu->is_uncore = pmu_is_uncore(dirfd, name); 91151d54847SJohn Garry if (pmu->is_uncore) 91251d54847SJohn Garry pmu->id = pmu_id(name); 9133a69672eSNamhyung Kim pmu->max_precise = pmu_max_precise(dirfd, pmu); 91454e32dc0SGanapatrao Kulkarni pmu_add_cpu_aliases(&aliases, pmu); 9154513c719SJohn Garry pmu_add_sys_aliases(&aliases, pmu); 91666ec1191SMark Rutland 917cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 918a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 9199fbc61f8SKan Liang INIT_LIST_HEAD(&pmu->caps); 920cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 921a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 9221eaf496eSIan Rogers list_add_tail(&pmu->list, pmus); 923dc0a6202SAdrian Hunter 924dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 925dc0a6202SAdrian Hunter 926cd82a32eSJiri Olsa return pmu; 92713d60ba0SKan Liang err: 928efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->name); 92913d60ba0SKan Liang free(pmu); 93013d60ba0SKan Liang return NULL; 931cd82a32eSJiri Olsa } 932cd82a32eSJiri Olsa 933e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) 934e552b7beSRob Herring { 935e552b7beSRob Herring struct perf_pmu_format *format; 936e552b7beSRob Herring 937e552b7beSRob Herring /* fake pmu doesn't have format list */ 938e552b7beSRob Herring if (pmu == &perf_pmu__fake) 939e552b7beSRob Herring return; 940e552b7beSRob Herring 941e552b7beSRob Herring list_for_each_entry(format, &pmu->format, list) 942e552b7beSRob Herring if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) { 943e552b7beSRob Herring pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" 944e552b7beSRob Herring "which is not supported by this version of perf!\n", 945e552b7beSRob Herring pmu->name, format->name, format->value); 946e552b7beSRob Herring return; 947e552b7beSRob Herring } 948e552b7beSRob Herring } 949e552b7beSRob Herring 950c6d616feSIan Rogers bool evsel__is_aux_event(const struct evsel *evsel) 951e12ee9f7SAdrian Hunter { 952e76026bdSArnaldo Carvalho de Melo struct perf_pmu *pmu = evsel__find_pmu(evsel); 953e12ee9f7SAdrian Hunter 954e12ee9f7SAdrian Hunter return pmu && pmu->auxtrace; 955e12ee9f7SAdrian Hunter } 956e12ee9f7SAdrian Hunter 9576593f019SJames Clark /* 9586593f019SJames Clark * Set @config_name to @val as long as the user hasn't already set or cleared it 9596593f019SJames Clark * by passing a config term on the command line. 9606593f019SJames Clark * 9616593f019SJames Clark * @val is the value to put into the bits specified by @config_name rather than 9626593f019SJames Clark * the bit pattern. It is shifted into position by this function, so to set 9636593f019SJames Clark * something to true, pass 1 for val rather than a pre shifted value. 9646593f019SJames Clark */ 9656593f019SJames Clark #define field_prep(_mask, _val) (((_val) << (ffsll(_mask) - 1)) & (_mask)) 9666593f019SJames Clark void evsel__set_config_if_unset(struct perf_pmu *pmu, struct evsel *evsel, 9676593f019SJames Clark const char *config_name, u64 val) 9686593f019SJames Clark { 9696593f019SJames Clark u64 user_bits = 0, bits; 9706593f019SJames Clark struct evsel_config_term *term = evsel__get_config_term(evsel, CFG_CHG); 9716593f019SJames Clark 9726593f019SJames Clark if (term) 9736593f019SJames Clark user_bits = term->val.cfg_chg; 9746593f019SJames Clark 9756593f019SJames Clark bits = perf_pmu__format_bits(&pmu->format, config_name); 9766593f019SJames Clark 9776593f019SJames Clark /* Do nothing if the user changed the value */ 9786593f019SJames Clark if (bits & user_bits) 9796593f019SJames Clark return; 9806593f019SJames Clark 9816593f019SJames Clark /* Otherwise replace it */ 9826593f019SJames Clark evsel->core.attr.config &= ~bits; 9836593f019SJames Clark evsel->core.attr.config |= field_prep(bits, val); 9846593f019SJames Clark } 9856593f019SJames Clark 9865c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 98709ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 988cd82a32eSJiri Olsa { 9895c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 990cd82a32eSJiri Olsa 991cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 992cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 993cd82a32eSJiri Olsa return format; 994cd82a32eSJiri Olsa 995cd82a32eSJiri Olsa return NULL; 996cd82a32eSJiri Olsa } 997cd82a32eSJiri Olsa 99809ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 99909ff6071SAdrian Hunter { 100009ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 100109ff6071SAdrian Hunter __u64 bits = 0; 100209ff6071SAdrian Hunter int fbit; 100309ff6071SAdrian Hunter 100409ff6071SAdrian Hunter if (!format) 100509ff6071SAdrian Hunter return 0; 100609ff6071SAdrian Hunter 100709ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 100809ff6071SAdrian Hunter bits |= 1ULL << fbit; 100909ff6071SAdrian Hunter 101009ff6071SAdrian Hunter return bits; 101109ff6071SAdrian Hunter } 101209ff6071SAdrian Hunter 1013a1ac7de6SAdrian Hunter int perf_pmu__format_type(struct list_head *formats, const char *name) 1014a1ac7de6SAdrian Hunter { 1015a1ac7de6SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 1016a1ac7de6SAdrian Hunter 1017a1ac7de6SAdrian Hunter if (!format) 1018a1ac7de6SAdrian Hunter return -1; 1019a1ac7de6SAdrian Hunter 1020a1ac7de6SAdrian Hunter return format->value; 1021a1ac7de6SAdrian Hunter } 1022a1ac7de6SAdrian Hunter 1023cd82a32eSJiri Olsa /* 1024dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 10254d39c89fSIngo Molnar * and unformatted value (value parameter). 1026cd82a32eSJiri Olsa */ 1027dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1028dc0a6202SAdrian Hunter bool zero) 1029cd82a32eSJiri Olsa { 1030cd82a32eSJiri Olsa unsigned long fbit, vbit; 1031cd82a32eSJiri Olsa 1032cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1033cd82a32eSJiri Olsa 1034cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 1035cd82a32eSJiri Olsa continue; 1036cd82a32eSJiri Olsa 1037dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 1038dc0a6202SAdrian Hunter *v |= (1llu << fbit); 1039dc0a6202SAdrian Hunter else if (zero) 1040dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 1041cd82a32eSJiri Olsa } 1042cd82a32eSJiri Olsa } 1043cd82a32eSJiri Olsa 10440efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 10450efe6b67SAdrian Hunter { 10461b9caa10SJiri Olsa int w; 10470efe6b67SAdrian Hunter 10481b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 10491b9caa10SJiri Olsa if (!w) 10501b9caa10SJiri Olsa return 0; 10511b9caa10SJiri Olsa if (w < 64) 10521b9caa10SJiri Olsa return (1ULL << w) - 1; 10531b9caa10SJiri Olsa return -1; 10540efe6b67SAdrian Hunter } 10550efe6b67SAdrian Hunter 1056cd82a32eSJiri Olsa /* 1057688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 1058688d4dfcSCody P Schafer * in the remaining terms. 1059688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 1060688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 1061688d4dfcSCody P Schafer * in a config string) later on in the term list. 1062688d4dfcSCody P Schafer */ 1063688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 1064688d4dfcSCody P Schafer struct list_head *head_terms, 1065688d4dfcSCody P Schafer __u64 *value) 1066688d4dfcSCody P Schafer { 1067688d4dfcSCody P Schafer struct parse_events_term *t; 1068688d4dfcSCody P Schafer 1069688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 10702a3d252dSIan Rogers if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 10712a3d252dSIan Rogers t->config && !strcmp(t->config, term->config)) { 1072688d4dfcSCody P Schafer t->used = true; 1073688d4dfcSCody P Schafer *value = t->val.num; 1074688d4dfcSCody P Schafer return 0; 1075688d4dfcSCody P Schafer } 1076688d4dfcSCody P Schafer } 1077688d4dfcSCody P Schafer 1078bb963e16SNamhyung Kim if (verbose > 0) 1079688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 1080688d4dfcSCody P Schafer 1081688d4dfcSCody P Schafer return -1; 1082688d4dfcSCody P Schafer } 1083688d4dfcSCody P Schafer 1084ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 1085e64b020bSJiri Olsa { 1086e64b020bSJiri Olsa struct perf_pmu_format *format; 108711db4e29SMasami Hiramatsu char *str = NULL; 108811db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1089f1417ceaSXin Gao unsigned int i = 0; 1090e64b020bSJiri Olsa 1091ffeb883eSHe Kuang if (!formats) 1092e64b020bSJiri Olsa return NULL; 1093e64b020bSJiri Olsa 1094e64b020bSJiri Olsa /* sysfs exported terms */ 1095ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 109611db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 109711db4e29SMasami Hiramatsu goto error; 1098e64b020bSJiri Olsa 1099ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 110011db4e29SMasami Hiramatsu error: 1101ffeb883eSHe Kuang strbuf_release(&buf); 1102e64b020bSJiri Olsa 1103e64b020bSJiri Olsa return str; 1104e64b020bSJiri Olsa } 1105e64b020bSJiri Olsa 1106688d4dfcSCody P Schafer /* 1107cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 110888aca8d9SCody P Schafer * user input data - term parameter. 1109cd82a32eSJiri Olsa */ 11104ac22b48SIan Rogers static int pmu_config_term(const char *pmu_name, 11114ac22b48SIan Rogers struct list_head *formats, 1112cd82a32eSJiri Olsa struct perf_event_attr *attr, 1113dc0a6202SAdrian Hunter struct parse_events_term *term, 1114688d4dfcSCody P Schafer struct list_head *head_terms, 1115e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1116cd82a32eSJiri Olsa { 11175c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1118cd82a32eSJiri Olsa __u64 *vp; 11190efe6b67SAdrian Hunter __u64 val, max_val; 1120cd82a32eSJiri Olsa 1121cd82a32eSJiri Olsa /* 1122688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1123688d4dfcSCody P Schafer * skip it in normal eval. 1124688d4dfcSCody P Schafer */ 1125688d4dfcSCody P Schafer if (term->used) 1126688d4dfcSCody P Schafer return 0; 1127688d4dfcSCody P Schafer 1128688d4dfcSCody P Schafer /* 1129cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1130cd82a32eSJiri Olsa * to be done for them. 1131cd82a32eSJiri Olsa */ 1132cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1133cd82a32eSJiri Olsa return 0; 1134cd82a32eSJiri Olsa 1135cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 1136688d4dfcSCody P Schafer if (!format) { 1137ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 11384ac22b48SIan Rogers char *unknown_term; 11394ac22b48SIan Rogers char *help_msg; 1140ffeb883eSHe Kuang 11414ac22b48SIan Rogers if (asprintf(&unknown_term, 11424ac22b48SIan Rogers "unknown term '%s' for pmu '%s'", 11434ac22b48SIan Rogers term->config, pmu_name) < 0) 11444ac22b48SIan Rogers unknown_term = NULL; 11454ac22b48SIan Rogers help_msg = parse_events_formats_error_string(pmu_term); 11464ac22b48SIan Rogers if (err) { 11476c191289SIan Rogers parse_events_error__handle(err, term->err_term, 11484ac22b48SIan Rogers unknown_term, 11494ac22b48SIan Rogers help_msg); 11504ac22b48SIan Rogers } else { 11514ac22b48SIan Rogers pr_debug("%s (%s)\n", unknown_term, help_msg); 11524ac22b48SIan Rogers free(unknown_term); 1153e64b020bSJiri Olsa } 11544ac22b48SIan Rogers free(pmu_term); 1155cd82a32eSJiri Olsa return -EINVAL; 1156688d4dfcSCody P Schafer } 1157cd82a32eSJiri Olsa 1158cd82a32eSJiri Olsa switch (format->value) { 1159cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1160cd82a32eSJiri Olsa vp = &attr->config; 1161cd82a32eSJiri Olsa break; 1162cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1163cd82a32eSJiri Olsa vp = &attr->config1; 1164cd82a32eSJiri Olsa break; 1165cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1166cd82a32eSJiri Olsa vp = &attr->config2; 1167cd82a32eSJiri Olsa break; 1168204e7c49SRob Herring case PERF_PMU_FORMAT_VALUE_CONFIG3: 1169204e7c49SRob Herring vp = &attr->config3; 1170204e7c49SRob Herring break; 1171cd82a32eSJiri Olsa default: 1172cd82a32eSJiri Olsa return -EINVAL; 1173cd82a32eSJiri Olsa } 1174cd82a32eSJiri Olsa 117516fa7e82SJiri Olsa /* 1176688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1177688d4dfcSCody P Schafer * using event parameters. 117816fa7e82SJiri Olsa */ 117999e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 118099e7138eSJiri Olsa if (term->no_value && 118199e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 118299e7138eSJiri Olsa if (err) { 11836c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1184448d732cSIan Rogers strdup("no value assigned for term"), 1185448d732cSIan Rogers NULL); 118699e7138eSJiri Olsa } 118799e7138eSJiri Olsa return -EINVAL; 118899e7138eSJiri Olsa } 118999e7138eSJiri Olsa 1190688d4dfcSCody P Schafer val = term->val.num; 119199e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1192688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1193bb963e16SNamhyung Kim if (verbose > 0) { 1194688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1195688d4dfcSCody P Schafer term->config, term->val.str); 1196e64b020bSJiri Olsa } 1197e64b020bSJiri Olsa if (err) { 11986c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1199448d732cSIan Rogers strdup("expected numeric value"), 1200448d732cSIan Rogers NULL); 1201e64b020bSJiri Olsa } 1202688d4dfcSCody P Schafer return -EINVAL; 1203688d4dfcSCody P Schafer } 1204688d4dfcSCody P Schafer 1205688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1206688d4dfcSCody P Schafer return -EINVAL; 1207688d4dfcSCody P Schafer } else 1208688d4dfcSCody P Schafer return -EINVAL; 1209688d4dfcSCody P Schafer 12100efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 12110efe6b67SAdrian Hunter if (val > max_val) { 12120efe6b67SAdrian Hunter if (err) { 1213448d732cSIan Rogers char *err_str; 1214448d732cSIan Rogers 12156c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1216448d732cSIan Rogers asprintf(&err_str, 12170efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1218448d732cSIan Rogers (unsigned long long)max_val) < 0 1219448d732cSIan Rogers ? strdup("value too big for format") 1220448d732cSIan Rogers : err_str, 1221448d732cSIan Rogers NULL); 12220efe6b67SAdrian Hunter return -EINVAL; 12230efe6b67SAdrian Hunter } 12240efe6b67SAdrian Hunter /* 12250efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 12260efe6b67SAdrian Hunter * silently truncated. 12270efe6b67SAdrian Hunter */ 12280efe6b67SAdrian Hunter } 12290efe6b67SAdrian Hunter 1230688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1231cd82a32eSJiri Olsa return 0; 1232cd82a32eSJiri Olsa } 1233cd82a32eSJiri Olsa 12344ac22b48SIan Rogers int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, 1235cff7f956SJiri Olsa struct perf_event_attr *attr, 1236dc0a6202SAdrian Hunter struct list_head *head_terms, 1237e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1238cd82a32eSJiri Olsa { 12396cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1240cd82a32eSJiri Olsa 1241688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 12424ac22b48SIan Rogers if (pmu_config_term(pmu_name, formats, attr, term, head_terms, 1243e64b020bSJiri Olsa zero, err)) 1244cd82a32eSJiri Olsa return -EINVAL; 1245688d4dfcSCody P Schafer } 1246cd82a32eSJiri Olsa 1247cd82a32eSJiri Olsa return 0; 1248cd82a32eSJiri Olsa } 1249cd82a32eSJiri Olsa 1250cd82a32eSJiri Olsa /* 1251cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1252cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1253cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1254cd82a32eSJiri Olsa */ 1255cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1256e64b020bSJiri Olsa struct list_head *head_terms, 1257e64b020bSJiri Olsa struct parse_events_error *err) 1258cd82a32eSJiri Olsa { 1259dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1260dc0a6202SAdrian Hunter 12614ac22b48SIan Rogers return perf_pmu__config_terms(pmu->name, &pmu->format, attr, 12624ac22b48SIan Rogers head_terms, zero, err); 1263cd82a32eSJiri Olsa } 1264cd82a32eSJiri Olsa 12655c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 12666cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1267a6146d50SZheng Yan { 12685c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1269a6146d50SZheng Yan char *name; 1270a6146d50SZheng Yan 1271a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1272a6146d50SZheng Yan return NULL; 1273a6146d50SZheng Yan 1274a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1275a6146d50SZheng Yan if (term->val.num != 1) 1276a6146d50SZheng Yan return NULL; 1277a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1278a6146d50SZheng Yan return NULL; 1279a6146d50SZheng Yan name = term->config; 1280a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1281a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1282a6146d50SZheng Yan return NULL; 1283a6146d50SZheng Yan name = term->val.str; 1284a6146d50SZheng Yan } else { 1285a6146d50SZheng Yan return NULL; 1286a6146d50SZheng Yan } 1287a6146d50SZheng Yan 1288a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 1289a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 1290a6146d50SZheng Yan return alias; 1291a6146d50SZheng Yan } 1292a6146d50SZheng Yan return NULL; 1293a6146d50SZheng Yan } 1294a6146d50SZheng Yan 1295410136f5SStephane Eranian 12961d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 12971d9e446bSJiri Olsa struct perf_pmu_info *info) 1298410136f5SStephane Eranian { 1299410136f5SStephane Eranian /* 1300410136f5SStephane Eranian * Only one term in event definition can 13011d9e446bSJiri Olsa * define unit, scale and snapshot, fail 13021d9e446bSJiri Olsa * if there's more than one. 1303410136f5SStephane Eranian */ 1304b30a7d1fSArnaldo Carvalho de Melo if ((info->unit && alias->unit[0]) || 13051d9e446bSJiri Olsa (info->scale && alias->scale) || 13061d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 1307410136f5SStephane Eranian return -EINVAL; 1308410136f5SStephane Eranian 1309b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 13101d9e446bSJiri Olsa info->unit = alias->unit; 1311410136f5SStephane Eranian 1312410136f5SStephane Eranian if (alias->scale) 13131d9e446bSJiri Olsa info->scale = alias->scale; 13141d9e446bSJiri Olsa 13151d9e446bSJiri Olsa if (alias->snapshot) 13161d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1317410136f5SStephane Eranian 1318410136f5SStephane Eranian return 0; 1319410136f5SStephane Eranian } 1320410136f5SStephane Eranian 1321a6146d50SZheng Yan /* 1322a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1323a6146d50SZheng Yan * defined for the alias 1324a6146d50SZheng Yan */ 1325410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 132646441bdcSMatt Fleming struct perf_pmu_info *info) 1327a6146d50SZheng Yan { 13286cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 13295c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1330a6146d50SZheng Yan int ret; 1331a6146d50SZheng Yan 1332044330c1SMatt Fleming info->per_pkg = false; 1333044330c1SMatt Fleming 13348a398897SStephane Eranian /* 13358a398897SStephane Eranian * Mark unit and scale as not set 13368a398897SStephane Eranian * (different from default values, see below) 13378a398897SStephane Eranian */ 133846441bdcSMatt Fleming info->unit = NULL; 133946441bdcSMatt Fleming info->scale = 0.0; 13401d9e446bSJiri Olsa info->snapshot = false; 1341410136f5SStephane Eranian 1342a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1343a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1344a6146d50SZheng Yan if (!alias) 1345a6146d50SZheng Yan continue; 1346a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 1347a6146d50SZheng Yan if (ret) 1348a6146d50SZheng Yan return ret; 1349410136f5SStephane Eranian 13501d9e446bSJiri Olsa ret = check_info_data(alias, info); 1351410136f5SStephane Eranian if (ret) 1352410136f5SStephane Eranian return ret; 1353410136f5SStephane Eranian 1354044330c1SMatt Fleming if (alias->per_pkg) 1355044330c1SMatt Fleming info->per_pkg = true; 1356044330c1SMatt Fleming 1357e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 13581dc92556SIan Rogers parse_events_term__delete(term); 1359a6146d50SZheng Yan } 13608a398897SStephane Eranian 13618a398897SStephane Eranian /* 13628a398897SStephane Eranian * if no unit or scale found in aliases, then 13638a398897SStephane Eranian * set defaults as for evsel 13648a398897SStephane Eranian * unit cannot left to NULL 13658a398897SStephane Eranian */ 136646441bdcSMatt Fleming if (info->unit == NULL) 136746441bdcSMatt Fleming info->unit = ""; 13688a398897SStephane Eranian 136946441bdcSMatt Fleming if (info->scale == 0.0) 137046441bdcSMatt Fleming info->scale = 1.0; 13718a398897SStephane Eranian 1372a6146d50SZheng Yan return 0; 1373a6146d50SZheng Yan } 1374a6146d50SZheng Yan 1375cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 1376cd82a32eSJiri Olsa int config, unsigned long *bits) 1377cd82a32eSJiri Olsa { 13785c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1379cd82a32eSJiri Olsa 1380cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 1381cd82a32eSJiri Olsa if (!format) 1382cd82a32eSJiri Olsa return -ENOMEM; 1383cd82a32eSJiri Olsa 1384cd82a32eSJiri Olsa format->name = strdup(name); 1385cd82a32eSJiri Olsa format->value = config; 1386cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 1387cd82a32eSJiri Olsa 1388cd82a32eSJiri Olsa list_add_tail(&format->list, list); 1389cd82a32eSJiri Olsa return 0; 1390cd82a32eSJiri Olsa } 1391cd82a32eSJiri Olsa 1392cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 1393cd82a32eSJiri Olsa { 1394cd82a32eSJiri Olsa long b; 1395cd82a32eSJiri Olsa 1396cd82a32eSJiri Olsa if (!to) 1397cd82a32eSJiri Olsa to = from; 1398cd82a32eSJiri Olsa 139915268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1400cd82a32eSJiri Olsa for (b = from; b <= to; b++) 140149bd97c2SSean Christopherson __set_bit(b, bits); 1402cd82a32eSJiri Olsa } 1403dc098b35SAndi Kleen 1404d26383dcSNamhyung Kim void perf_pmu__del_formats(struct list_head *formats) 1405d26383dcSNamhyung Kim { 1406d26383dcSNamhyung Kim struct perf_pmu_format *fmt, *tmp; 1407d26383dcSNamhyung Kim 1408d26383dcSNamhyung Kim list_for_each_entry_safe(fmt, tmp, formats, list) { 1409d26383dcSNamhyung Kim list_del(&fmt->list); 1410efe98a7aSArnaldo Carvalho de Melo zfree(&fmt->name); 1411d26383dcSNamhyung Kim free(fmt); 1412d26383dcSNamhyung Kim } 1413d26383dcSNamhyung Kim } 1414d26383dcSNamhyung Kim 1415d504fae9SJohn Garry bool is_pmu_core(const char *name) 1416d504fae9SJohn Garry { 14174bf7e81aSIan Rogers return !strcmp(name, "cpu") || is_sysfs_pmu_core(name); 14184bf7e81aSIan Rogers } 14194bf7e81aSIan Rogers 14206fd1e519SIan Rogers bool perf_pmu__supports_legacy_cache(const struct perf_pmu *pmu) 14216fd1e519SIan Rogers { 1422e20d1f2fSIan Rogers return pmu->is_core; 14236fd1e519SIan Rogers } 14246fd1e519SIan Rogers 142552c7b4d3SIan Rogers bool perf_pmu__auto_merge_stats(const struct perf_pmu *pmu) 142652c7b4d3SIan Rogers { 1427e2342142SIan Rogers return pmu->is_core && perf_pmus__num_core_pmus() == 1; 142852c7b4d3SIan Rogers } 142952c7b4d3SIan Rogers 14301eaf496eSIan Rogers bool perf_pmu__have_event(const struct perf_pmu *pmu, const char *name) 1431abe9544eSIan Rogers { 14324cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 14334cabc3d1SAndi Kleen 14341eaf496eSIan Rogers list_for_each_entry(alias, &pmu->aliases, list) { 14354cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 14364cabc3d1SAndi Kleen return true; 14374cabc3d1SAndi Kleen } 14384cabc3d1SAndi Kleen return false; 14394cabc3d1SAndi Kleen } 14407d4bdab5SAdrian Hunter 1441*251aa040SIan Rogers bool perf_pmu__is_software(const struct perf_pmu *pmu) 1442*251aa040SIan Rogers { 1443*251aa040SIan Rogers if (pmu->is_core || pmu->is_uncore || pmu->auxtrace) 1444*251aa040SIan Rogers return false; 1445*251aa040SIan Rogers switch (pmu->type) { 1446*251aa040SIan Rogers case PERF_TYPE_HARDWARE: return false; 1447*251aa040SIan Rogers case PERF_TYPE_SOFTWARE: return true; 1448*251aa040SIan Rogers case PERF_TYPE_TRACEPOINT: return true; 1449*251aa040SIan Rogers case PERF_TYPE_HW_CACHE: return false; 1450*251aa040SIan Rogers case PERF_TYPE_RAW: return false; 1451*251aa040SIan Rogers case PERF_TYPE_BREAKPOINT: return true; 1452*251aa040SIan Rogers default: break; 1453*251aa040SIan Rogers } 1454*251aa040SIan Rogers return !strcmp(pmu->name, "kprobe") || !strcmp(pmu->name, "uprobe"); 1455*251aa040SIan Rogers } 1456*251aa040SIan Rogers 1457d50a79cdSJames Clark FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 14587d4bdab5SAdrian Hunter { 14597d4bdab5SAdrian Hunter char path[PATH_MAX]; 14607d4bdab5SAdrian Hunter 1461f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name) || 1462f8ad6018SJames Clark !file_available(path)) 14637d4bdab5SAdrian Hunter return NULL; 14647d4bdab5SAdrian Hunter 14657d4bdab5SAdrian Hunter return fopen(path, "r"); 14667d4bdab5SAdrian Hunter } 14677d4bdab5SAdrian Hunter 14683a69672eSNamhyung Kim FILE *perf_pmu__open_file_at(struct perf_pmu *pmu, int dirfd, const char *name) 14693a69672eSNamhyung Kim { 14703a69672eSNamhyung Kim int fd; 14713a69672eSNamhyung Kim 14723a69672eSNamhyung Kim fd = perf_pmu__pathname_fd(dirfd, pmu->name, name, O_RDONLY); 14733a69672eSNamhyung Kim if (fd < 0) 14743a69672eSNamhyung Kim return NULL; 14753a69672eSNamhyung Kim 14763a69672eSNamhyung Kim return fdopen(fd, "r"); 14773a69672eSNamhyung Kim } 14783a69672eSNamhyung Kim 14797d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 14807d4bdab5SAdrian Hunter ...) 14817d4bdab5SAdrian Hunter { 14827d4bdab5SAdrian Hunter va_list args; 14837d4bdab5SAdrian Hunter FILE *file; 14847d4bdab5SAdrian Hunter int ret = EOF; 14857d4bdab5SAdrian Hunter 14867d4bdab5SAdrian Hunter va_start(args, fmt); 14877d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 14887d4bdab5SAdrian Hunter if (file) { 14897d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 14907d4bdab5SAdrian Hunter fclose(file); 14917d4bdab5SAdrian Hunter } 14927d4bdab5SAdrian Hunter va_end(args); 14937d4bdab5SAdrian Hunter return ret; 14947d4bdab5SAdrian Hunter } 14959fbc61f8SKan Liang 14963a69672eSNamhyung Kim int perf_pmu__scan_file_at(struct perf_pmu *pmu, int dirfd, const char *name, 14973a69672eSNamhyung Kim const char *fmt, ...) 14983a69672eSNamhyung Kim { 14993a69672eSNamhyung Kim va_list args; 15003a69672eSNamhyung Kim FILE *file; 15013a69672eSNamhyung Kim int ret = EOF; 15023a69672eSNamhyung Kim 15033a69672eSNamhyung Kim va_start(args, fmt); 15043a69672eSNamhyung Kim file = perf_pmu__open_file_at(pmu, dirfd, name); 15053a69672eSNamhyung Kim if (file) { 15063a69672eSNamhyung Kim ret = vfscanf(file, fmt, args); 15073a69672eSNamhyung Kim fclose(file); 15083a69672eSNamhyung Kim } 15093a69672eSNamhyung Kim va_end(args); 15103a69672eSNamhyung Kim return ret; 15113a69672eSNamhyung Kim } 15123a69672eSNamhyung Kim 1513c2b6a896SGerman Gomez bool perf_pmu__file_exists(struct perf_pmu *pmu, const char *name) 1514c2b6a896SGerman Gomez { 1515c2b6a896SGerman Gomez char path[PATH_MAX]; 1516c2b6a896SGerman Gomez 1517c2b6a896SGerman Gomez if (!perf_pmu__pathname_scnprintf(path, sizeof(path), pmu->name, name)) 1518c2b6a896SGerman Gomez return false; 1519c2b6a896SGerman Gomez 1520c2b6a896SGerman Gomez return file_available(path); 1521c2b6a896SGerman Gomez } 1522c2b6a896SGerman Gomez 15239fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 15249fbc61f8SKan Liang { 15259fbc61f8SKan Liang struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 15269fbc61f8SKan Liang 15279fbc61f8SKan Liang if (!caps) 15289fbc61f8SKan Liang return -ENOMEM; 15299fbc61f8SKan Liang 15309fbc61f8SKan Liang caps->name = strdup(name); 15319fbc61f8SKan Liang if (!caps->name) 15329fbc61f8SKan Liang goto free_caps; 15339fbc61f8SKan Liang caps->value = strndup(value, strlen(value) - 1); 15349fbc61f8SKan Liang if (!caps->value) 15359fbc61f8SKan Liang goto free_name; 15369fbc61f8SKan Liang list_add_tail(&caps->list, list); 15379fbc61f8SKan Liang return 0; 15389fbc61f8SKan Liang 15399fbc61f8SKan Liang free_name: 154057f14b5aSArnaldo Carvalho de Melo zfree(&caps->name); 15419fbc61f8SKan Liang free_caps: 15429fbc61f8SKan Liang free(caps); 15439fbc61f8SKan Liang 15449fbc61f8SKan Liang return -ENOMEM; 15459fbc61f8SKan Liang } 15469fbc61f8SKan Liang 1547eec11310SNamhyung Kim static void perf_pmu__del_caps(struct perf_pmu *pmu) 1548eec11310SNamhyung Kim { 1549eec11310SNamhyung Kim struct perf_pmu_caps *caps, *tmp; 1550eec11310SNamhyung Kim 1551eec11310SNamhyung Kim list_for_each_entry_safe(caps, tmp, &pmu->caps, list) { 1552eec11310SNamhyung Kim list_del(&caps->list); 1553efe98a7aSArnaldo Carvalho de Melo zfree(&caps->name); 1554efe98a7aSArnaldo Carvalho de Melo zfree(&caps->value); 1555eec11310SNamhyung Kim free(caps); 1556eec11310SNamhyung Kim } 1557eec11310SNamhyung Kim } 1558eec11310SNamhyung Kim 15599fbc61f8SKan Liang /* 15609fbc61f8SKan Liang * Reading/parsing the given pmu capabilities, which should be located at: 15619fbc61f8SKan Liang * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 15629fbc61f8SKan Liang * Return the number of capabilities 15639fbc61f8SKan Liang */ 15649fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu) 15659fbc61f8SKan Liang { 15669fbc61f8SKan Liang struct stat st; 15679fbc61f8SKan Liang char caps_path[PATH_MAX]; 15689fbc61f8SKan Liang DIR *caps_dir; 15699fbc61f8SKan Liang struct dirent *evt_ent; 1570b39094d3SNamhyung Kim int caps_fd; 15713339ec44SRavi Bangoria 15723339ec44SRavi Bangoria if (pmu->caps_initialized) 15733339ec44SRavi Bangoria return pmu->nr_caps; 15743339ec44SRavi Bangoria 15753339ec44SRavi Bangoria pmu->nr_caps = 0; 15769fbc61f8SKan Liang 1577f8ad6018SJames Clark if (!perf_pmu__pathname_scnprintf(caps_path, sizeof(caps_path), pmu->name, "caps")) 15789fbc61f8SKan Liang return -1; 15799fbc61f8SKan Liang 15803339ec44SRavi Bangoria if (stat(caps_path, &st) < 0) { 15813339ec44SRavi Bangoria pmu->caps_initialized = true; 15829fbc61f8SKan Liang return 0; /* no error if caps does not exist */ 15833339ec44SRavi Bangoria } 15849fbc61f8SKan Liang 15859fbc61f8SKan Liang caps_dir = opendir(caps_path); 15869fbc61f8SKan Liang if (!caps_dir) 15879fbc61f8SKan Liang return -EINVAL; 15889fbc61f8SKan Liang 1589b39094d3SNamhyung Kim caps_fd = dirfd(caps_dir); 1590b39094d3SNamhyung Kim 15919fbc61f8SKan Liang while ((evt_ent = readdir(caps_dir)) != NULL) { 15929fbc61f8SKan Liang char *name = evt_ent->d_name; 15939fbc61f8SKan Liang char value[128]; 15949fbc61f8SKan Liang FILE *file; 1595b39094d3SNamhyung Kim int fd; 15969fbc61f8SKan Liang 15979fbc61f8SKan Liang if (!strcmp(name, ".") || !strcmp(name, "..")) 15989fbc61f8SKan Liang continue; 15999fbc61f8SKan Liang 1600b39094d3SNamhyung Kim fd = openat(caps_fd, name, O_RDONLY); 16010ea8920eSIan Rogers if (fd == -1) 16029fbc61f8SKan Liang continue; 16030ea8920eSIan Rogers file = fdopen(fd, "r"); 16040ea8920eSIan Rogers if (!file) { 16050ea8920eSIan Rogers close(fd); 16060ea8920eSIan Rogers continue; 16070ea8920eSIan Rogers } 16089fbc61f8SKan Liang 16099fbc61f8SKan Liang if (!fgets(value, sizeof(value), file) || 16109fbc61f8SKan Liang (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 16119fbc61f8SKan Liang fclose(file); 16129fbc61f8SKan Liang continue; 16139fbc61f8SKan Liang } 16149fbc61f8SKan Liang 16153339ec44SRavi Bangoria pmu->nr_caps++; 16169fbc61f8SKan Liang fclose(file); 16179fbc61f8SKan Liang } 16189fbc61f8SKan Liang 16199fbc61f8SKan Liang closedir(caps_dir); 16209fbc61f8SKan Liang 16213339ec44SRavi Bangoria pmu->caps_initialized = true; 16223339ec44SRavi Bangoria return pmu->nr_caps; 16239fbc61f8SKan Liang } 1624e4064776SJin Yao 1625e4064776SJin Yao void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 16268e8bbfb3SIan Rogers const char *name) 1627e4064776SJin Yao { 1628e4064776SJin Yao struct perf_pmu_format *format; 1629e4064776SJin Yao __u64 masks = 0, bits; 1630e4064776SJin Yao char buf[100]; 1631e4064776SJin Yao unsigned int i; 1632e4064776SJin Yao 1633e4064776SJin Yao list_for_each_entry(format, &pmu->format, list) { 1634e4064776SJin Yao if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG) 1635e4064776SJin Yao continue; 1636e4064776SJin Yao 1637e4064776SJin Yao for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1638e4064776SJin Yao masks |= 1ULL << i; 1639e4064776SJin Yao } 1640e4064776SJin Yao 1641e4064776SJin Yao /* 1642e4064776SJin Yao * Kernel doesn't export any valid format bits. 1643e4064776SJin Yao */ 1644e4064776SJin Yao if (masks == 0) 1645e4064776SJin Yao return; 1646e4064776SJin Yao 1647e4064776SJin Yao bits = config & ~masks; 1648e4064776SJin Yao if (bits == 0) 1649e4064776SJin Yao return; 1650e4064776SJin Yao 1651e4064776SJin Yao bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1652e4064776SJin Yao 1653e4064776SJin Yao pr_warning("WARNING: event '%s' not valid (bits %s of config " 1654e4064776SJin Yao "'%llx' not supported by kernel)!\n", 1655e4064776SJin Yao name ?: "N/A", buf, config); 1656e4064776SJin Yao } 1657c5a26ea4SJin Yao 1658c47a5599SJin Yao int perf_pmu__match(char *pattern, char *name, char *tok) 1659c47a5599SJin Yao { 166013d60ba0SKan Liang if (!name) 166113d60ba0SKan Liang return -1; 166213d60ba0SKan Liang 1663c47a5599SJin Yao if (fnmatch(pattern, name, 0)) 1664c47a5599SJin Yao return -1; 1665c47a5599SJin Yao 1666240e6fd0SIan Rogers if (tok && !perf_pmu__match_ignoring_suffix(name, tok)) 1667c47a5599SJin Yao return -1; 1668c47a5599SJin Yao 1669c47a5599SJin Yao return 0; 1670c47a5599SJin Yao } 16711d3351e6SJin Yao 1672acef233bSJing Zhang double __weak perf_pmu__cpu_slots_per_cycle(void) 1673acef233bSJing Zhang { 1674acef233bSJing Zhang return NAN; 1675acef233bSJing Zhang } 1676f8ad6018SJames Clark 1677f8ad6018SJames Clark int perf_pmu__event_source_devices_scnprintf(char *pathname, size_t size) 1678f8ad6018SJames Clark { 1679f8ad6018SJames Clark const char *sysfs = sysfs__mountpoint(); 1680f8ad6018SJames Clark 1681f8ad6018SJames Clark if (!sysfs) 1682f8ad6018SJames Clark return 0; 1683f8ad6018SJames Clark return scnprintf(pathname, size, "%s/bus/event_source/devices/", sysfs); 1684f8ad6018SJames Clark } 1685f8ad6018SJames Clark 1686e293a5e8SNamhyung Kim int perf_pmu__event_source_devices_fd(void) 1687e293a5e8SNamhyung Kim { 1688e293a5e8SNamhyung Kim char path[PATH_MAX]; 1689e293a5e8SNamhyung Kim const char *sysfs = sysfs__mountpoint(); 1690e293a5e8SNamhyung Kim 1691e293a5e8SNamhyung Kim if (!sysfs) 1692e293a5e8SNamhyung Kim return -1; 1693e293a5e8SNamhyung Kim 1694e293a5e8SNamhyung Kim scnprintf(path, sizeof(path), "%s/bus/event_source/devices/", sysfs); 1695e293a5e8SNamhyung Kim return open(path, O_DIRECTORY); 1696e293a5e8SNamhyung Kim } 1697e293a5e8SNamhyung Kim 1698f8ad6018SJames Clark /* 1699f8ad6018SJames Clark * Fill 'buf' with the path to a file or folder in 'pmu_name' in 1700f8ad6018SJames Clark * sysfs. For example if pmu_name = "cs_etm" and 'filename' = "format" 1701f8ad6018SJames Clark * then pathname will be filled with 1702f8ad6018SJames Clark * "/sys/bus/event_source/devices/cs_etm/format" 1703f8ad6018SJames Clark * 1704f8ad6018SJames Clark * Return 0 if the sysfs mountpoint couldn't be found or if no 1705f8ad6018SJames Clark * characters were written. 1706f8ad6018SJames Clark */ 1707f8ad6018SJames Clark int perf_pmu__pathname_scnprintf(char *buf, size_t size, 1708f8ad6018SJames Clark const char *pmu_name, const char *filename) 1709f8ad6018SJames Clark { 1710f8ad6018SJames Clark char base_path[PATH_MAX]; 1711f8ad6018SJames Clark 1712f8ad6018SJames Clark if (!perf_pmu__event_source_devices_scnprintf(base_path, sizeof(base_path))) 1713f8ad6018SJames Clark return 0; 1714f8ad6018SJames Clark return scnprintf(buf, size, "%s%s/%s", base_path, pmu_name, filename); 1715f8ad6018SJames Clark } 1716eec11310SNamhyung Kim 1717e293a5e8SNamhyung Kim int perf_pmu__pathname_fd(int dirfd, const char *pmu_name, const char *filename, int flags) 1718e293a5e8SNamhyung Kim { 1719e293a5e8SNamhyung Kim char path[PATH_MAX]; 1720e293a5e8SNamhyung Kim 1721e293a5e8SNamhyung Kim scnprintf(path, sizeof(path), "%s/%s", pmu_name, filename); 1722e293a5e8SNamhyung Kim return openat(dirfd, path, flags); 1723e293a5e8SNamhyung Kim } 1724e293a5e8SNamhyung Kim 17251eaf496eSIan Rogers void perf_pmu__delete(struct perf_pmu *pmu) 1726eec11310SNamhyung Kim { 1727eec11310SNamhyung Kim perf_pmu__del_formats(&pmu->format); 1728eec11310SNamhyung Kim perf_pmu__del_aliases(pmu); 1729eec11310SNamhyung Kim perf_pmu__del_caps(pmu); 1730eec11310SNamhyung Kim 1731eec11310SNamhyung Kim perf_cpu_map__put(pmu->cpus); 1732eec11310SNamhyung Kim 1733efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->default_config); 1734efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->name); 1735efe98a7aSArnaldo Carvalho de Melo zfree(&pmu->alias_name); 1736eec11310SNamhyung Kim free(pmu); 1737eec11310SNamhyung Kim } 1738