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> 6*c47a5599SJin Yao #include <linux/ctype.h> 7fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h> 8cd82a32eSJiri Olsa #include <sys/types.h> 9a43783aeSArnaldo Carvalho de Melo #include <errno.h> 10c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h> 117a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h> 12cd82a32eSJiri Olsa #include <unistd.h> 13cd82a32eSJiri Olsa #include <stdio.h> 14dc0a6202SAdrian Hunter #include <stdbool.h> 157d4bdab5SAdrian Hunter #include <stdarg.h> 16cd82a32eSJiri Olsa #include <dirent.h> 17cd0cfad7SBorislav Petkov #include <api/fs/fs.h> 18410136f5SStephane Eranian #include <locale.h> 19fbc2844eSWilliam Cohen #include <regex.h> 209c3516d1SJiri Olsa #include <perf/cpumap.h> 21*c47a5599SJin Yao #include <fnmatch.h> 225e51b0bbSArnaldo Carvalho de Melo #include "debug.h" 23e12ee9f7SAdrian Hunter #include "evsel.h" 24cd82a32eSJiri Olsa #include "pmu.h" 25cd82a32eSJiri Olsa #include "parse-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" 3044462430SJin Yao #include "pmu-hybrid.h" 31cd82a32eSJiri Olsa 32e46fc8d9SArnaldo Carvalho de Melo struct perf_pmu perf_pmu__fake; 33e46fc8d9SArnaldo Carvalho de Melo 34ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 35ab1bf653SArnaldo Carvalho de Melo char *name; 36ab1bf653SArnaldo Carvalho de Melo int value; 37ab1bf653SArnaldo Carvalho de Melo DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 38ab1bf653SArnaldo Carvalho de Melo struct list_head list; 39ab1bf653SArnaldo Carvalho de Melo }; 40ab1bf653SArnaldo Carvalho de Melo 41cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name); 42cd82a32eSJiri Olsa extern FILE *perf_pmu_in; 43cd82a32eSJiri Olsa 44cd82a32eSJiri Olsa static LIST_HEAD(pmus); 45c5a26ea4SJin Yao static bool hybrid_scanned; 46cd82a32eSJiri Olsa 47cd82a32eSJiri Olsa /* 48cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 49cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 50cd82a32eSJiri Olsa */ 51cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head) 52cd82a32eSJiri Olsa { 53cd82a32eSJiri Olsa struct dirent *evt_ent; 54cd82a32eSJiri Olsa DIR *format_dir; 55cd82a32eSJiri Olsa int ret = 0; 56cd82a32eSJiri Olsa 57cd82a32eSJiri Olsa format_dir = opendir(dir); 58cd82a32eSJiri Olsa if (!format_dir) 59cd82a32eSJiri Olsa return -EINVAL; 60cd82a32eSJiri Olsa 61cd82a32eSJiri Olsa while (!ret && (evt_ent = readdir(format_dir))) { 62cd82a32eSJiri Olsa char path[PATH_MAX]; 63cd82a32eSJiri Olsa char *name = evt_ent->d_name; 64cd82a32eSJiri Olsa FILE *file; 65cd82a32eSJiri Olsa 66cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 67cd82a32eSJiri Olsa continue; 68cd82a32eSJiri Olsa 69cd82a32eSJiri Olsa snprintf(path, PATH_MAX, "%s/%s", dir, name); 70cd82a32eSJiri Olsa 71cd82a32eSJiri Olsa ret = -EINVAL; 72cd82a32eSJiri Olsa file = fopen(path, "r"); 73cd82a32eSJiri Olsa if (!file) 74cd82a32eSJiri Olsa break; 75cd82a32eSJiri Olsa 76cd82a32eSJiri Olsa perf_pmu_in = file; 77cd82a32eSJiri Olsa ret = perf_pmu_parse(head, name); 78cd82a32eSJiri Olsa fclose(file); 79cd82a32eSJiri Olsa } 80cd82a32eSJiri Olsa 81cd82a32eSJiri Olsa closedir(format_dir); 82cd82a32eSJiri Olsa return ret; 83cd82a32eSJiri Olsa } 84cd82a32eSJiri Olsa 85cd82a32eSJiri Olsa /* 86cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 87cd82a32eSJiri Olsa * located at: 88cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 89cd82a32eSJiri Olsa */ 90b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format) 91cd82a32eSJiri Olsa { 92cd82a32eSJiri Olsa char path[PATH_MAX]; 93cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 94cd82a32eSJiri Olsa 95cd82a32eSJiri Olsa if (!sysfs) 96cd82a32eSJiri Olsa return -1; 97cd82a32eSJiri Olsa 98cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 9950a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); 100cd82a32eSJiri Olsa 101d9664582SAndi Kleen if (!file_available(path)) 102d9664582SAndi Kleen return 0; 103cd82a32eSJiri Olsa 104cff7f956SJiri Olsa if (perf_pmu__format_parse(path, format)) 105cd82a32eSJiri Olsa return -1; 106cd82a32eSJiri Olsa 107cd82a32eSJiri Olsa return 0; 108cd82a32eSJiri Olsa } 109cd82a32eSJiri Olsa 110a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval) 111d02fc6bcSAndi Kleen { 112d02fc6bcSAndi Kleen char *lc; 113d02fc6bcSAndi Kleen int ret = 0; 114d02fc6bcSAndi Kleen 115d02fc6bcSAndi Kleen /* 116d02fc6bcSAndi Kleen * save current locale 117d02fc6bcSAndi Kleen */ 118d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 119d02fc6bcSAndi Kleen 120d02fc6bcSAndi Kleen /* 121d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 122d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 123d02fc6bcSAndi Kleen * call below. 124d02fc6bcSAndi Kleen */ 125d02fc6bcSAndi Kleen lc = strdup(lc); 126d02fc6bcSAndi Kleen if (!lc) { 127d02fc6bcSAndi Kleen ret = -ENOMEM; 128d02fc6bcSAndi Kleen goto out; 129d02fc6bcSAndi Kleen } 130d02fc6bcSAndi Kleen 131d02fc6bcSAndi Kleen /* 132d02fc6bcSAndi Kleen * force to C locale to ensure kernel 133d02fc6bcSAndi Kleen * scale string is converted correctly. 134d02fc6bcSAndi Kleen * kernel uses default C locale. 135d02fc6bcSAndi Kleen */ 136d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 137d02fc6bcSAndi Kleen 138d02fc6bcSAndi Kleen *sval = strtod(scale, end); 139d02fc6bcSAndi Kleen 140d02fc6bcSAndi Kleen out: 141d02fc6bcSAndi Kleen /* restore locale */ 142d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 143d02fc6bcSAndi Kleen free(lc); 144d02fc6bcSAndi Kleen return ret; 145d02fc6bcSAndi Kleen } 146d02fc6bcSAndi Kleen 147410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) 148410136f5SStephane Eranian { 149410136f5SStephane Eranian struct stat st; 150410136f5SStephane Eranian ssize_t sret; 151410136f5SStephane Eranian char scale[128]; 152410136f5SStephane Eranian int fd, ret = -1; 153410136f5SStephane Eranian char path[PATH_MAX]; 154410136f5SStephane Eranian 15511a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name); 156410136f5SStephane Eranian 157410136f5SStephane Eranian fd = open(path, O_RDONLY); 158410136f5SStephane Eranian if (fd == -1) 159410136f5SStephane Eranian return -1; 160410136f5SStephane Eranian 161410136f5SStephane Eranian if (fstat(fd, &st) < 0) 162410136f5SStephane Eranian goto error; 163410136f5SStephane Eranian 164410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 165410136f5SStephane Eranian if (sret < 0) 166410136f5SStephane Eranian goto error; 167410136f5SStephane Eranian 1689ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 1699ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 1709ecae065SMadhavan Srinivasan else 171410136f5SStephane Eranian scale[sret] = '\0'; 1729ecae065SMadhavan Srinivasan 173a55ab7c4SJin Yao ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); 174410136f5SStephane Eranian error: 175410136f5SStephane Eranian close(fd); 176410136f5SStephane Eranian return ret; 177410136f5SStephane Eranian } 178410136f5SStephane Eranian 179410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) 180410136f5SStephane Eranian { 181410136f5SStephane Eranian char path[PATH_MAX]; 182410136f5SStephane Eranian ssize_t sret; 183410136f5SStephane Eranian int fd; 184410136f5SStephane Eranian 18511a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name); 186410136f5SStephane Eranian 187410136f5SStephane Eranian fd = open(path, O_RDONLY); 188410136f5SStephane Eranian if (fd == -1) 189410136f5SStephane Eranian return -1; 190410136f5SStephane Eranian 191410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 192410136f5SStephane Eranian if (sret < 0) 193410136f5SStephane Eranian goto error; 194410136f5SStephane Eranian 195410136f5SStephane Eranian close(fd); 196410136f5SStephane Eranian 1979ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 1989ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 1999ecae065SMadhavan Srinivasan else 200410136f5SStephane Eranian alias->unit[sret] = '\0'; 201410136f5SStephane Eranian 202410136f5SStephane Eranian return 0; 203410136f5SStephane Eranian error: 204410136f5SStephane Eranian close(fd); 205410136f5SStephane Eranian alias->unit[0] = '\0'; 206410136f5SStephane Eranian return -1; 207410136f5SStephane Eranian } 208410136f5SStephane Eranian 209044330c1SMatt Fleming static int 210044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) 211044330c1SMatt Fleming { 212044330c1SMatt Fleming char path[PATH_MAX]; 213044330c1SMatt Fleming int fd; 214044330c1SMatt Fleming 21511a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); 216044330c1SMatt Fleming 217044330c1SMatt Fleming fd = open(path, O_RDONLY); 218044330c1SMatt Fleming if (fd == -1) 219044330c1SMatt Fleming return -1; 220044330c1SMatt Fleming 221044330c1SMatt Fleming close(fd); 222044330c1SMatt Fleming 223044330c1SMatt Fleming alias->per_pkg = true; 224044330c1SMatt Fleming return 0; 225044330c1SMatt Fleming } 226044330c1SMatt Fleming 2271d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 2281d9e446bSJiri Olsa char *dir, char *name) 2291d9e446bSJiri Olsa { 2301d9e446bSJiri Olsa char path[PATH_MAX]; 2311d9e446bSJiri Olsa int fd; 2321d9e446bSJiri Olsa 23311a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); 2341d9e446bSJiri Olsa 2351d9e446bSJiri Olsa fd = open(path, O_RDONLY); 2361d9e446bSJiri Olsa if (fd == -1) 2371d9e446bSJiri Olsa return -1; 2381d9e446bSJiri Olsa 2391d9e446bSJiri Olsa alias->snapshot = true; 2401d9e446bSJiri Olsa close(fd); 2411d9e446bSJiri Olsa return 0; 2421d9e446bSJiri Olsa } 2431d9e446bSJiri Olsa 2446dde6429SThomas Richter static void perf_pmu_assign_str(char *name, const char *field, char **old_str, 2456dde6429SThomas Richter char **new_str) 2466dde6429SThomas Richter { 2476dde6429SThomas Richter if (!*old_str) 2486dde6429SThomas Richter goto set_new; 2496dde6429SThomas Richter 2506dde6429SThomas Richter if (*new_str) { /* Have new string, check with old */ 2516dde6429SThomas Richter if (strcasecmp(*old_str, *new_str)) 2526dde6429SThomas Richter pr_debug("alias %s differs in field '%s'\n", 2536dde6429SThomas Richter name, field); 2546dde6429SThomas Richter zfree(old_str); 2556dde6429SThomas Richter } else /* Nothing new --> keep old string */ 2566dde6429SThomas Richter return; 2576dde6429SThomas Richter set_new: 2586dde6429SThomas Richter *old_str = *new_str; 2596dde6429SThomas Richter *new_str = NULL; 2606dde6429SThomas Richter } 2616dde6429SThomas Richter 2626dde6429SThomas Richter static void perf_pmu_update_alias(struct perf_pmu_alias *old, 2636dde6429SThomas Richter struct perf_pmu_alias *newalias) 2646dde6429SThomas Richter { 2656dde6429SThomas Richter perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); 2666dde6429SThomas Richter perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, 2676dde6429SThomas Richter &newalias->long_desc); 2686dde6429SThomas Richter perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); 2696dde6429SThomas Richter perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr, 2706dde6429SThomas Richter &newalias->metric_expr); 2716dde6429SThomas Richter perf_pmu_assign_str(old->name, "metric_name", &old->metric_name, 2726dde6429SThomas Richter &newalias->metric_name); 2736dde6429SThomas Richter perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); 2746dde6429SThomas Richter old->scale = newalias->scale; 2756dde6429SThomas Richter old->per_pkg = newalias->per_pkg; 2766dde6429SThomas Richter old->snapshot = newalias->snapshot; 2776dde6429SThomas Richter memcpy(old->unit, newalias->unit, sizeof(old->unit)); 2786dde6429SThomas Richter } 2796dde6429SThomas Richter 2806dde6429SThomas Richter /* Delete an alias entry. */ 28122fe5a25SNamhyung Kim void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 2826dde6429SThomas Richter { 2836dde6429SThomas Richter zfree(&newalias->name); 2846dde6429SThomas Richter zfree(&newalias->desc); 2856dde6429SThomas Richter zfree(&newalias->long_desc); 2866dde6429SThomas Richter zfree(&newalias->topic); 2876dde6429SThomas Richter zfree(&newalias->str); 2886dde6429SThomas Richter zfree(&newalias->metric_expr); 2896dde6429SThomas Richter zfree(&newalias->metric_name); 29032705de7SJin Yao zfree(&newalias->pmu_name); 2916dde6429SThomas Richter parse_events_terms__purge(&newalias->terms); 2926dde6429SThomas Richter free(newalias); 2936dde6429SThomas Richter } 2946dde6429SThomas Richter 2956dde6429SThomas Richter /* Merge an alias, search in alias list. If this name is already 2966dde6429SThomas Richter * present merge both of them to combine all information. 2976dde6429SThomas Richter */ 2986dde6429SThomas Richter static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, 2996dde6429SThomas Richter struct list_head *alist) 3006dde6429SThomas Richter { 3016dde6429SThomas Richter struct perf_pmu_alias *a; 3026dde6429SThomas Richter 3036dde6429SThomas Richter list_for_each_entry(a, alist, list) { 3046dde6429SThomas Richter if (!strcasecmp(newalias->name, a->name)) { 30532705de7SJin Yao if (newalias->pmu_name && a->pmu_name && 30632705de7SJin Yao !strcasecmp(newalias->pmu_name, a->pmu_name)) { 30732705de7SJin Yao continue; 30832705de7SJin Yao } 3096dde6429SThomas Richter perf_pmu_update_alias(a, newalias); 3106dde6429SThomas Richter perf_pmu_free_alias(newalias); 3116dde6429SThomas Richter return true; 3126dde6429SThomas Richter } 3136dde6429SThomas Richter } 3146dde6429SThomas Richter return false; 3156dde6429SThomas Richter } 3166dde6429SThomas Richter 31770c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, 318eab35953SJin Yao char *desc, char *val, struct pmu_event *pe) 319a6146d50SZheng Yan { 3200c24d6fbSThomas Richter struct parse_events_term *term; 3215c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 322a6146d50SZheng Yan int ret; 323fedb2b51SAndi Kleen int num; 3240c24d6fbSThomas Richter char newval[256]; 325eab35953SJin Yao char *long_desc = NULL, *topic = NULL, *unit = NULL, *perpkg = NULL, 32632705de7SJin Yao *metric_expr = NULL, *metric_name = NULL, *deprecated = NULL, 32732705de7SJin Yao *pmu_name = NULL; 328eab35953SJin Yao 329eab35953SJin Yao if (pe) { 330eab35953SJin Yao long_desc = (char *)pe->long_desc; 331eab35953SJin Yao topic = (char *)pe->topic; 332eab35953SJin Yao unit = (char *)pe->unit; 333eab35953SJin Yao perpkg = (char *)pe->perpkg; 334eab35953SJin Yao metric_expr = (char *)pe->metric_expr; 335eab35953SJin Yao metric_name = (char *)pe->metric_name; 336eab35953SJin Yao deprecated = (char *)pe->deprecated; 33732705de7SJin Yao pmu_name = (char *)pe->pmu; 338eab35953SJin Yao } 339a6146d50SZheng Yan 340a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 341a6146d50SZheng Yan if (!alias) 342a6146d50SZheng Yan return -ENOMEM; 343a6146d50SZheng Yan 344a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 345410136f5SStephane Eranian alias->scale = 1.0; 346410136f5SStephane Eranian alias->unit[0] = '\0'; 347044330c1SMatt Fleming alias->per_pkg = false; 34884530920SStephane Eranian alias->snapshot = false; 349a7f6c8c8SJin Yao alias->deprecated = false; 350410136f5SStephane Eranian 35170c646e0SSukadev Bhattiprolu ret = parse_events_terms(&alias->terms, val); 352a6146d50SZheng Yan if (ret) { 35370c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 354a6146d50SZheng Yan free(alias); 355a6146d50SZheng Yan return ret; 356a6146d50SZheng Yan } 357a6146d50SZheng Yan 3580c24d6fbSThomas Richter /* Scan event and remove leading zeroes, spaces, newlines, some 3590c24d6fbSThomas Richter * platforms have terms specified as 3600c24d6fbSThomas Richter * event=0x0091 (read from files ../<PMU>/events/<FILE> 3610c24d6fbSThomas Richter * and terms specified as event=0x91 (read from JSON files). 3620c24d6fbSThomas Richter * 3630c24d6fbSThomas Richter * Rebuild string to make alias->str member comparable. 3640c24d6fbSThomas Richter */ 3650c24d6fbSThomas Richter memset(newval, 0, sizeof(newval)); 3660c24d6fbSThomas Richter ret = 0; 3670c24d6fbSThomas Richter list_for_each_entry(term, &alias->terms, list) { 3680c24d6fbSThomas Richter if (ret) 3690c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3700c24d6fbSThomas Richter ","); 3710c24d6fbSThomas Richter if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 3720c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3730c24d6fbSThomas Richter "%s=%#x", term->config, term->val.num); 3740c24d6fbSThomas Richter else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 3750c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3760c24d6fbSThomas Richter "%s=%s", term->config, term->val.str); 3770c24d6fbSThomas Richter } 3780c24d6fbSThomas Richter 379a6146d50SZheng Yan alias->name = strdup(name); 38070c646e0SSukadev Bhattiprolu if (dir) { 381410136f5SStephane Eranian /* 382410136f5SStephane Eranian * load unit name and scale if available 383410136f5SStephane Eranian */ 384410136f5SStephane Eranian perf_pmu__parse_unit(alias, dir, name); 385410136f5SStephane Eranian perf_pmu__parse_scale(alias, dir, name); 386044330c1SMatt Fleming perf_pmu__parse_per_pkg(alias, dir, name); 3871d9e446bSJiri Olsa perf_pmu__parse_snapshot(alias, dir, name); 38870c646e0SSukadev Bhattiprolu } 389410136f5SStephane Eranian 39000636c3bSAndi Kleen alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL; 39196284814SAndi Kleen alias->metric_name = metric_name ? strdup(metric_name): NULL; 39208e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 393c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 394c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 395dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 396fedb2b51SAndi Kleen if (unit) { 397a55ab7c4SJin Yao if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0) 398fedb2b51SAndi Kleen return -1; 399fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 400fedb2b51SAndi Kleen } 401fedb2b51SAndi Kleen alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1; 4020c24d6fbSThomas Richter alias->str = strdup(newval); 40332705de7SJin Yao alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 404f2361024SAndi Kleen 405a7f6c8c8SJin Yao if (deprecated) 406a7f6c8c8SJin Yao alias->deprecated = true; 407a7f6c8c8SJin Yao 4086dde6429SThomas Richter if (!perf_pmu_merge_alias(alias, list)) 409a6146d50SZheng Yan list_add_tail(&alias->list, list); 410410136f5SStephane Eranian 411a6146d50SZheng Yan return 0; 412a6146d50SZheng Yan } 413a6146d50SZheng Yan 41470c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) 41570c646e0SSukadev Bhattiprolu { 41670c646e0SSukadev Bhattiprolu char buf[256]; 41770c646e0SSukadev Bhattiprolu int ret; 41870c646e0SSukadev Bhattiprolu 41970c646e0SSukadev Bhattiprolu ret = fread(buf, 1, sizeof(buf), file); 42070c646e0SSukadev Bhattiprolu if (ret == 0) 42170c646e0SSukadev Bhattiprolu return -EINVAL; 42270c646e0SSukadev Bhattiprolu 42370c646e0SSukadev Bhattiprolu buf[ret] = 0; 42470c646e0SSukadev Bhattiprolu 425ea23ac73SThomas Richter /* Remove trailing newline from sysfs file */ 42613c230abSArnaldo Carvalho de Melo strim(buf); 427ea23ac73SThomas Richter 428eab35953SJin Yao return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL); 42970c646e0SSukadev Bhattiprolu } 43070c646e0SSukadev Bhattiprolu 43146441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 43246441bdcSMatt Fleming { 43346441bdcSMatt Fleming size_t len; 43446441bdcSMatt Fleming 43546441bdcSMatt Fleming len = strlen(name); 43646441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 43746441bdcSMatt Fleming return true; 43846441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 43946441bdcSMatt Fleming return true; 440044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 441044330c1SMatt Fleming return true; 4421d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 4431d9e446bSJiri Olsa return true; 44446441bdcSMatt Fleming 44546441bdcSMatt Fleming return false; 44646441bdcSMatt Fleming } 44746441bdcSMatt Fleming 448a6146d50SZheng Yan /* 449a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 450a6146d50SZheng Yan * specified in 'dir' parameter. 451a6146d50SZheng Yan */ 452a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head) 453a6146d50SZheng Yan { 454a6146d50SZheng Yan struct dirent *evt_ent; 455a6146d50SZheng Yan DIR *event_dir; 456a6146d50SZheng Yan 457a6146d50SZheng Yan event_dir = opendir(dir); 458a6146d50SZheng Yan if (!event_dir) 459a6146d50SZheng Yan return -EINVAL; 460a6146d50SZheng Yan 461940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 462a6146d50SZheng Yan char path[PATH_MAX]; 463a6146d50SZheng Yan char *name = evt_ent->d_name; 464a6146d50SZheng Yan FILE *file; 465a6146d50SZheng Yan 466a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 467a6146d50SZheng Yan continue; 468a6146d50SZheng Yan 469410136f5SStephane Eranian /* 47046441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 471410136f5SStephane Eranian */ 47246441bdcSMatt Fleming if (pmu_alias_info_file(name)) 473410136f5SStephane Eranian continue; 474410136f5SStephane Eranian 47577f18153SJiri Olsa scnprintf(path, PATH_MAX, "%s/%s", dir, name); 476a6146d50SZheng Yan 477a6146d50SZheng Yan file = fopen(path, "r"); 478940db6dcSAndi Kleen if (!file) { 479940db6dcSAndi Kleen pr_debug("Cannot open %s\n", path); 480940db6dcSAndi Kleen continue; 481940db6dcSAndi Kleen } 482410136f5SStephane Eranian 483940db6dcSAndi Kleen if (perf_pmu__new_alias(head, dir, name, file) < 0) 484940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 485a6146d50SZheng Yan fclose(file); 486a6146d50SZheng Yan } 487a6146d50SZheng Yan 488a6146d50SZheng Yan closedir(event_dir); 489940db6dcSAndi Kleen return 0; 490a6146d50SZheng Yan } 491a6146d50SZheng Yan 492a6146d50SZheng Yan /* 493a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 494a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 495a6146d50SZheng Yan */ 496b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head) 497a6146d50SZheng Yan { 498a6146d50SZheng Yan char path[PATH_MAX]; 499cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 500a6146d50SZheng Yan 501a6146d50SZheng Yan if (!sysfs) 502a6146d50SZheng Yan return -1; 503a6146d50SZheng Yan 504a6146d50SZheng Yan snprintf(path, PATH_MAX, 505a6146d50SZheng Yan "%s/bus/event_source/devices/%s/events", sysfs, name); 506a6146d50SZheng Yan 507d9664582SAndi Kleen if (!file_available(path)) 508d9664582SAndi Kleen return 0; 509a6146d50SZheng Yan 510a6146d50SZheng Yan if (pmu_aliases_parse(path, head)) 511a6146d50SZheng Yan return -1; 512a6146d50SZheng Yan 513a6146d50SZheng Yan return 0; 514a6146d50SZheng Yan } 515a6146d50SZheng Yan 5165c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 517a6146d50SZheng Yan struct list_head *terms) 518a6146d50SZheng Yan { 5197c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 520a6146d50SZheng Yan LIST_HEAD(list); 521a6146d50SZheng Yan int ret; 522a6146d50SZheng Yan 523a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 5247c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 525a6146d50SZheng Yan if (ret) { 526682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 527a6146d50SZheng Yan return ret; 528a6146d50SZheng Yan } 529c2f1ceadSAndi Kleen /* 530c2f1ceadSAndi Kleen * Weak terms don't override command line options, 531c2f1ceadSAndi Kleen * which we don't want for implicit terms in aliases. 532c2f1ceadSAndi Kleen */ 533c2f1ceadSAndi Kleen cloned->weak = true; 5347c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 535a6146d50SZheng Yan } 536a6146d50SZheng Yan list_splice(&list, terms); 537a6146d50SZheng Yan return 0; 538a6146d50SZheng Yan } 539a6146d50SZheng Yan 540cd82a32eSJiri Olsa /* 541cd82a32eSJiri Olsa * Reading/parsing the default pmu type value, which should be 542cd82a32eSJiri Olsa * located at: 543cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 544cd82a32eSJiri Olsa */ 545b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type) 546cd82a32eSJiri Olsa { 547cd82a32eSJiri Olsa char path[PATH_MAX]; 548cd82a32eSJiri Olsa FILE *file; 549cd82a32eSJiri Olsa int ret = 0; 550cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 551cd82a32eSJiri Olsa 552cd82a32eSJiri Olsa if (!sysfs) 553cd82a32eSJiri Olsa return -1; 554cd82a32eSJiri Olsa 555cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 55650a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); 557cd82a32eSJiri Olsa 558d9664582SAndi Kleen if (access(path, R_OK) < 0) 559cd82a32eSJiri Olsa return -1; 560cd82a32eSJiri Olsa 561cd82a32eSJiri Olsa file = fopen(path, "r"); 562cd82a32eSJiri Olsa if (!file) 563cd82a32eSJiri Olsa return -EINVAL; 564cd82a32eSJiri Olsa 565cd82a32eSJiri Olsa if (1 != fscanf(file, "%u", type)) 566cd82a32eSJiri Olsa ret = -1; 567cd82a32eSJiri Olsa 568cd82a32eSJiri Olsa fclose(file); 569cd82a32eSJiri Olsa return ret; 570cd82a32eSJiri Olsa } 571cd82a32eSJiri Olsa 57250a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */ 57350a9667cSRobert Richter static void pmu_read_sysfs(void) 57450a9667cSRobert Richter { 57550a9667cSRobert Richter char path[PATH_MAX]; 57650a9667cSRobert Richter DIR *dir; 57750a9667cSRobert Richter struct dirent *dent; 578cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 57950a9667cSRobert Richter 58050a9667cSRobert Richter if (!sysfs) 58150a9667cSRobert Richter return; 58250a9667cSRobert Richter 58350a9667cSRobert Richter snprintf(path, PATH_MAX, 58450a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 58550a9667cSRobert Richter 58650a9667cSRobert Richter dir = opendir(path); 58750a9667cSRobert Richter if (!dir) 58850a9667cSRobert Richter return; 58950a9667cSRobert Richter 59050a9667cSRobert Richter while ((dent = readdir(dir))) { 59150a9667cSRobert Richter if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 59250a9667cSRobert Richter continue; 59350a9667cSRobert Richter /* add to static LIST_HEAD(pmus): */ 59450a9667cSRobert Richter perf_pmu__find(dent->d_name); 59550a9667cSRobert Richter } 59650a9667cSRobert Richter 59750a9667cSRobert Richter closedir(dir); 59850a9667cSRobert Richter } 59950a9667cSRobert Richter 600f854839bSJiri Olsa static struct perf_cpu_map *__pmu_cpumask(const char *path) 60166ec1191SMark Rutland { 60266ec1191SMark Rutland FILE *file; 603f854839bSJiri Olsa struct perf_cpu_map *cpus; 60466ec1191SMark Rutland 60566ec1191SMark Rutland file = fopen(path, "r"); 60666ec1191SMark Rutland if (!file) 60766ec1191SMark Rutland return NULL; 60866ec1191SMark Rutland 6099c3516d1SJiri Olsa cpus = perf_cpu_map__read(file); 61066ec1191SMark Rutland fclose(file); 61166ec1191SMark Rutland return cpus; 61266ec1191SMark Rutland } 61366ec1191SMark Rutland 61466ec1191SMark Rutland /* 61566ec1191SMark Rutland * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 61666ec1191SMark Rutland * may have a "cpus" file. 61766ec1191SMark Rutland */ 61851d54847SJohn Garry #define SYS_TEMPLATE_ID "./bus/event_source/devices/%s/identifier" 61966ec1191SMark Rutland #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask" 62066ec1191SMark Rutland 621f854839bSJiri Olsa static struct perf_cpu_map *pmu_cpumask(const char *name) 6227ae92e74SYan, Zheng { 6237ae92e74SYan, Zheng char path[PATH_MAX]; 624f854839bSJiri Olsa struct perf_cpu_map *cpus; 625cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 6267e3fcffeSMark Rutland const char *templates[] = { 62766ec1191SMark Rutland CPUS_TEMPLATE_UNCORE, 62866ec1191SMark Rutland CPUS_TEMPLATE_CPU, 6297e3fcffeSMark Rutland NULL 6307e3fcffeSMark Rutland }; 6317e3fcffeSMark Rutland const char **template; 6327ae92e74SYan, Zheng 6337ae92e74SYan, Zheng if (!sysfs) 6347ae92e74SYan, Zheng return NULL; 6357ae92e74SYan, Zheng 6367e3fcffeSMark Rutland for (template = templates; *template; template++) { 6377e3fcffeSMark Rutland snprintf(path, PATH_MAX, *template, sysfs, name); 63866ec1191SMark Rutland cpus = __pmu_cpumask(path); 63966ec1191SMark Rutland if (cpus) 64066ec1191SMark Rutland return cpus; 6417e3fcffeSMark Rutland } 6427ae92e74SYan, Zheng 6437ae92e74SYan, Zheng return NULL; 64466ec1191SMark Rutland } 6457ae92e74SYan, Zheng 64666ec1191SMark Rutland static bool pmu_is_uncore(const char *name) 64766ec1191SMark Rutland { 64866ec1191SMark Rutland char path[PATH_MAX]; 649d9664582SAndi Kleen const char *sysfs; 6507ae92e74SYan, Zheng 65144462430SJin Yao if (perf_pmu__hybrid_mounted(name)) 65244462430SJin Yao return false; 65344462430SJin Yao 654d9664582SAndi Kleen sysfs = sysfs__mountpoint(); 65566ec1191SMark Rutland snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name); 656d9664582SAndi Kleen return file_available(path); 6577ae92e74SYan, Zheng } 6587ae92e74SYan, Zheng 65951d54847SJohn Garry static char *pmu_id(const char *name) 66051d54847SJohn Garry { 66151d54847SJohn Garry char path[PATH_MAX], *str; 66251d54847SJohn Garry size_t len; 66351d54847SJohn Garry 66451d54847SJohn Garry snprintf(path, PATH_MAX, SYS_TEMPLATE_ID, name); 66551d54847SJohn Garry 66651d54847SJohn Garry if (sysfs__read_str(path, &str, &len) < 0) 66751d54847SJohn Garry return NULL; 66851d54847SJohn Garry 66951d54847SJohn Garry str[len - 1] = 0; /* remove line feed */ 67051d54847SJohn Garry 67151d54847SJohn Garry return str; 67251d54847SJohn Garry } 67351d54847SJohn Garry 674933f82ffSSukadev Bhattiprolu /* 67514b22ae0SGanapatrao Kulkarni * PMU CORE devices have different name other than cpu in sysfs on some 676292c34c1SKan Liang * platforms. 677292c34c1SKan Liang * Looking for possible sysfs files to identify the arm core device. 67814b22ae0SGanapatrao Kulkarni */ 679292c34c1SKan Liang static int is_arm_pmu_core(const char *name) 68014b22ae0SGanapatrao Kulkarni { 68114b22ae0SGanapatrao Kulkarni char path[PATH_MAX]; 68214b22ae0SGanapatrao Kulkarni const char *sysfs = sysfs__mountpoint(); 68314b22ae0SGanapatrao Kulkarni 68414b22ae0SGanapatrao Kulkarni if (!sysfs) 68514b22ae0SGanapatrao Kulkarni return 0; 68614b22ae0SGanapatrao Kulkarni 68714b22ae0SGanapatrao Kulkarni /* Look for cpu sysfs (specific to arm) */ 68814b22ae0SGanapatrao Kulkarni scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus", 68914b22ae0SGanapatrao Kulkarni sysfs, name); 690d9664582SAndi Kleen return file_available(path); 69114b22ae0SGanapatrao Kulkarni } 69214b22ae0SGanapatrao Kulkarni 69354e32dc0SGanapatrao Kulkarni static char *perf_pmu__getcpuid(struct perf_pmu *pmu) 694d77ade9fSAndi Kleen { 695d77ade9fSAndi Kleen char *cpuid; 696d77ade9fSAndi Kleen static bool printed; 697d77ade9fSAndi Kleen 698d77ade9fSAndi Kleen cpuid = getenv("PERF_CPUID"); 699d77ade9fSAndi Kleen if (cpuid) 700d77ade9fSAndi Kleen cpuid = strdup(cpuid); 701d77ade9fSAndi Kleen if (!cpuid) 70254e32dc0SGanapatrao Kulkarni cpuid = get_cpuid_str(pmu); 703d77ade9fSAndi Kleen if (!cpuid) 704d77ade9fSAndi Kleen return NULL; 705d77ade9fSAndi Kleen 706d77ade9fSAndi Kleen if (!printed) { 707d77ade9fSAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 708d77ade9fSAndi Kleen printed = true; 709d77ade9fSAndi Kleen } 710d77ade9fSAndi Kleen return cpuid; 711d77ade9fSAndi Kleen } 712d77ade9fSAndi Kleen 71354e32dc0SGanapatrao Kulkarni struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu) 714d77ade9fSAndi Kleen { 715d77ade9fSAndi Kleen struct pmu_events_map *map; 71654e32dc0SGanapatrao Kulkarni char *cpuid = perf_pmu__getcpuid(pmu); 717d77ade9fSAndi Kleen int i; 718d77ade9fSAndi Kleen 719de3d0f12SGanapatrao Kulkarni /* on some platforms which uses cpus map, cpuid can be NULL for 720de3d0f12SGanapatrao Kulkarni * PMUs other than CORE PMUs. 721de3d0f12SGanapatrao Kulkarni */ 722de3d0f12SGanapatrao Kulkarni if (!cpuid) 723de3d0f12SGanapatrao Kulkarni return NULL; 724de3d0f12SGanapatrao Kulkarni 725d77ade9fSAndi Kleen i = 0; 726d77ade9fSAndi Kleen for (;;) { 727d77ade9fSAndi Kleen map = &pmu_events_map[i++]; 728d77ade9fSAndi Kleen if (!map->table) { 729d77ade9fSAndi Kleen map = NULL; 730d77ade9fSAndi Kleen break; 731d77ade9fSAndi Kleen } 732d77ade9fSAndi Kleen 7334cb7d3ecSThomas Richter if (!strcmp_cpuid_str(map->cpuid, cpuid)) 734d77ade9fSAndi Kleen break; 735d77ade9fSAndi Kleen } 736d77ade9fSAndi Kleen free(cpuid); 737d77ade9fSAndi Kleen return map; 738d77ade9fSAndi Kleen } 739d77ade9fSAndi Kleen 740e126bef5SJohn Garry struct pmu_events_map *__weak pmu_events_map__find(void) 741e126bef5SJohn Garry { 742e126bef5SJohn Garry return perf_pmu__find_map(NULL); 743e126bef5SJohn Garry } 744e126bef5SJohn Garry 745*c47a5599SJin Yao static bool perf_pmu__valid_suffix(char *pmu_name, char *tok) 746*c47a5599SJin Yao { 747*c47a5599SJin Yao char *p; 748*c47a5599SJin Yao 749*c47a5599SJin Yao if (strncmp(pmu_name, tok, strlen(tok))) 750*c47a5599SJin Yao return false; 751*c47a5599SJin Yao 752*c47a5599SJin Yao p = pmu_name + strlen(tok); 753*c47a5599SJin Yao if (*p == 0) 754*c47a5599SJin Yao return true; 755*c47a5599SJin Yao 756*c47a5599SJin Yao if (*p != '_') 757*c47a5599SJin Yao return false; 758*c47a5599SJin Yao 759*c47a5599SJin Yao ++p; 760*c47a5599SJin Yao if (*p == 0 || !isdigit(*p)) 761*c47a5599SJin Yao return false; 762*c47a5599SJin Yao 763*c47a5599SJin Yao return true; 764*c47a5599SJin Yao } 765*c47a5599SJin Yao 7665b9a5000SJohn Garry bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 767730670b1SJohn Garry { 768730670b1SJohn Garry char *tmp = NULL, *tok, *str; 769730670b1SJohn Garry bool res; 770730670b1SJohn Garry 771730670b1SJohn Garry str = strdup(pmu_name); 772730670b1SJohn Garry if (!str) 773730670b1SJohn Garry return false; 774730670b1SJohn Garry 775730670b1SJohn Garry /* 776730670b1SJohn Garry * uncore alias may be from different PMU with common prefix 777730670b1SJohn Garry */ 778730670b1SJohn Garry tok = strtok_r(str, ",", &tmp); 779730670b1SJohn Garry if (strncmp(pmu_name, tok, strlen(tok))) { 780730670b1SJohn Garry res = false; 781730670b1SJohn Garry goto out; 782730670b1SJohn Garry } 783730670b1SJohn Garry 784730670b1SJohn Garry /* 785730670b1SJohn Garry * Match more complex aliases where the alias name is a comma-delimited 786730670b1SJohn Garry * list of tokens, orderly contained in the matching PMU name. 787730670b1SJohn Garry * 788730670b1SJohn Garry * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 789730670b1SJohn Garry * match "socket" in "socketX_pmunameY" and then "pmuname" in 790730670b1SJohn Garry * "pmunameY". 791730670b1SJohn Garry */ 792730670b1SJohn Garry for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) { 793730670b1SJohn Garry name = strstr(name, tok); 794*c47a5599SJin Yao if (!name || !perf_pmu__valid_suffix((char *)name, tok)) { 795730670b1SJohn Garry res = false; 796730670b1SJohn Garry goto out; 797730670b1SJohn Garry } 798730670b1SJohn Garry } 799730670b1SJohn Garry 800730670b1SJohn Garry res = true; 801730670b1SJohn Garry out: 802730670b1SJohn Garry free(str); 803730670b1SJohn Garry return res; 804730670b1SJohn Garry } 805730670b1SJohn Garry 806933f82ffSSukadev Bhattiprolu /* 807933f82ffSSukadev Bhattiprolu * From the pmu_events_map, find the table of PMU events that corresponds 808933f82ffSSukadev Bhattiprolu * to the current running CPU. Then, add all PMU events from that table 809933f82ffSSukadev Bhattiprolu * as aliases. 810933f82ffSSukadev Bhattiprolu */ 811e45ad701SJohn Garry void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu, 812e45ad701SJohn Garry struct pmu_events_map *map) 813933f82ffSSukadev Bhattiprolu { 814933f82ffSSukadev Bhattiprolu int i; 81554e32dc0SGanapatrao Kulkarni const char *name = pmu->name; 816933f82ffSSukadev Bhattiprolu /* 817933f82ffSSukadev Bhattiprolu * Found a matching PMU events table. Create aliases 818933f82ffSSukadev Bhattiprolu */ 819933f82ffSSukadev Bhattiprolu i = 0; 820933f82ffSSukadev Bhattiprolu while (1) { 821599ee18fSJohn Garry const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu"; 822599ee18fSJohn Garry struct pmu_event *pe = &map->table[i++]; 823599ee18fSJohn Garry const char *pname = pe->pmu ? pe->pmu : cpu_name; 824fedb2b51SAndi Kleen 825b18f3e36SAndi Kleen if (!pe->name) { 826b18f3e36SAndi Kleen if (pe->metric_group || pe->metric_name) 827b18f3e36SAndi Kleen continue; 828933f82ffSSukadev Bhattiprolu break; 829b18f3e36SAndi Kleen } 830933f82ffSSukadev Bhattiprolu 831e94d6b7fSKan Liang if (pmu_is_uncore(name) && 832730670b1SJohn Garry pmu_uncore_alias_match(pname, name)) 833e94d6b7fSKan Liang goto new_alias; 834e94d6b7fSKan Liang 835ea1fa48cSThomas Richter if (strcmp(pname, name)) 83614b22ae0SGanapatrao Kulkarni continue; 837fedb2b51SAndi Kleen 838e94d6b7fSKan Liang new_alias: 839933f82ffSSukadev Bhattiprolu /* need type casts to override 'const' */ 840933f82ffSSukadev Bhattiprolu __perf_pmu__new_alias(head, NULL, (char *)pe->name, 841c8d6828aSSukadev Bhattiprolu (char *)pe->desc, (char *)pe->event, 842eab35953SJin Yao pe); 843933f82ffSSukadev Bhattiprolu } 844933f82ffSSukadev Bhattiprolu } 845933f82ffSSukadev Bhattiprolu 846e45ad701SJohn Garry static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) 847e45ad701SJohn Garry { 848e45ad701SJohn Garry struct pmu_events_map *map; 849e45ad701SJohn Garry 850e45ad701SJohn Garry map = perf_pmu__find_map(pmu); 851e45ad701SJohn Garry if (!map) 852e45ad701SJohn Garry return; 853e45ad701SJohn Garry 854e45ad701SJohn Garry pmu_add_cpu_aliases_map(head, pmu, map); 855e45ad701SJohn Garry } 856e45ad701SJohn Garry 8574513c719SJohn Garry void pmu_for_each_sys_event(pmu_sys_event_iter_fn fn, void *data) 8584513c719SJohn Garry { 8594513c719SJohn Garry int i = 0; 8604513c719SJohn Garry 8614513c719SJohn Garry while (1) { 8624513c719SJohn Garry struct pmu_sys_events *event_table; 8634513c719SJohn Garry int j = 0; 8644513c719SJohn Garry 8654513c719SJohn Garry event_table = &pmu_sys_event_tables[i++]; 8664513c719SJohn Garry 8674513c719SJohn Garry if (!event_table->table) 8684513c719SJohn Garry break; 8694513c719SJohn Garry 8704513c719SJohn Garry while (1) { 8714513c719SJohn Garry struct pmu_event *pe = &event_table->table[j++]; 8724513c719SJohn Garry int ret; 8734513c719SJohn Garry 8744513c719SJohn Garry if (!pe->name && !pe->metric_group && !pe->metric_name) 8754513c719SJohn Garry break; 8764513c719SJohn Garry 8774513c719SJohn Garry ret = fn(pe, data); 8784513c719SJohn Garry if (ret) 8794513c719SJohn Garry break; 8804513c719SJohn Garry } 8814513c719SJohn Garry } 8824513c719SJohn Garry } 8834513c719SJohn Garry 8844513c719SJohn Garry struct pmu_sys_event_iter_data { 8854513c719SJohn Garry struct list_head *head; 8864513c719SJohn Garry struct perf_pmu *pmu; 8874513c719SJohn Garry }; 8884513c719SJohn Garry 8894513c719SJohn Garry static int pmu_add_sys_aliases_iter_fn(struct pmu_event *pe, void *data) 8904513c719SJohn Garry { 8914513c719SJohn Garry struct pmu_sys_event_iter_data *idata = data; 8924513c719SJohn Garry struct perf_pmu *pmu = idata->pmu; 8934513c719SJohn Garry 8944513c719SJohn Garry if (!pe->name) { 8954513c719SJohn Garry if (pe->metric_group || pe->metric_name) 8964513c719SJohn Garry return 0; 8974513c719SJohn Garry return -EINVAL; 8984513c719SJohn Garry } 8994513c719SJohn Garry 9004513c719SJohn Garry if (!pe->compat || !pe->pmu) 9014513c719SJohn Garry return 0; 9024513c719SJohn Garry 9034513c719SJohn Garry if (!strcmp(pmu->id, pe->compat) && 9044513c719SJohn Garry pmu_uncore_alias_match(pe->pmu, pmu->name)) { 9054513c719SJohn Garry __perf_pmu__new_alias(idata->head, NULL, 9064513c719SJohn Garry (char *)pe->name, 9074513c719SJohn Garry (char *)pe->desc, 9084513c719SJohn Garry (char *)pe->event, 909eab35953SJin Yao pe); 9104513c719SJohn Garry } 9114513c719SJohn Garry 9124513c719SJohn Garry return 0; 9134513c719SJohn Garry } 9144513c719SJohn Garry 9154513c719SJohn Garry static void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu) 9164513c719SJohn Garry { 9174513c719SJohn Garry struct pmu_sys_event_iter_data idata = { 9184513c719SJohn Garry .head = head, 9194513c719SJohn Garry .pmu = pmu, 9204513c719SJohn Garry }; 9214513c719SJohn Garry 9224513c719SJohn Garry if (!pmu->id) 9234513c719SJohn Garry return; 9244513c719SJohn Garry 9254513c719SJohn Garry pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata); 9264513c719SJohn Garry } 9274513c719SJohn Garry 928c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 929dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 930dc0a6202SAdrian Hunter { 931dc0a6202SAdrian Hunter return NULL; 932dc0a6202SAdrian Hunter } 933dc0a6202SAdrian Hunter 93490a86bdeSJiri Olsa static int pmu_max_precise(const char *name) 93590a86bdeSJiri Olsa { 93690a86bdeSJiri Olsa char path[PATH_MAX]; 93790a86bdeSJiri Olsa int max_precise = -1; 93890a86bdeSJiri Olsa 93990a86bdeSJiri Olsa scnprintf(path, PATH_MAX, 94090a86bdeSJiri Olsa "bus/event_source/devices/%s/caps/max_precise", 94190a86bdeSJiri Olsa name); 94290a86bdeSJiri Olsa 94390a86bdeSJiri Olsa sysfs__read_int(path, &max_precise); 94490a86bdeSJiri Olsa return max_precise; 94590a86bdeSJiri Olsa } 94690a86bdeSJiri Olsa 947b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name) 948cd82a32eSJiri Olsa { 949cd82a32eSJiri Olsa struct perf_pmu *pmu; 950cd82a32eSJiri Olsa LIST_HEAD(format); 951a6146d50SZheng Yan LIST_HEAD(aliases); 952cd82a32eSJiri Olsa __u32 type; 953cd82a32eSJiri Olsa 954cd82a32eSJiri Olsa /* 955cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 956cd82a32eSJiri Olsa * type value and format definitions. Load both right 957cd82a32eSJiri Olsa * now. 958cd82a32eSJiri Olsa */ 959cd82a32eSJiri Olsa if (pmu_format(name, &format)) 960cd82a32eSJiri Olsa return NULL; 961cd82a32eSJiri Olsa 96215b22ed3SAndi Kleen /* 96315b22ed3SAndi Kleen * Check the type first to avoid unnecessary work. 96415b22ed3SAndi Kleen */ 96515b22ed3SAndi Kleen if (pmu_type(name, &type)) 96615b22ed3SAndi Kleen return NULL; 96715b22ed3SAndi Kleen 9683fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 9693fded963SJiri Olsa return NULL; 9703fded963SJiri Olsa 971cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 972cd82a32eSJiri Olsa if (!pmu) 973cd82a32eSJiri Olsa return NULL; 974cd82a32eSJiri Olsa 9757ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 97654e32dc0SGanapatrao Kulkarni pmu->name = strdup(name); 97754e32dc0SGanapatrao Kulkarni pmu->type = type; 97866ec1191SMark Rutland pmu->is_uncore = pmu_is_uncore(name); 97951d54847SJohn Garry if (pmu->is_uncore) 98051d54847SJohn Garry pmu->id = pmu_id(name); 98144462430SJin Yao pmu->is_hybrid = perf_pmu__hybrid_mounted(name); 98290a86bdeSJiri Olsa pmu->max_precise = pmu_max_precise(name); 98354e32dc0SGanapatrao Kulkarni pmu_add_cpu_aliases(&aliases, pmu); 9844513c719SJohn Garry pmu_add_sys_aliases(&aliases, pmu); 98566ec1191SMark Rutland 986cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 987a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 9889fbc61f8SKan Liang INIT_LIST_HEAD(&pmu->caps); 989cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 990a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 9919bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 992dc0a6202SAdrian Hunter 99344462430SJin Yao if (pmu->is_hybrid) 99444462430SJin Yao list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus); 99544462430SJin Yao 996dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 997dc0a6202SAdrian Hunter 998cd82a32eSJiri Olsa return pmu; 999cd82a32eSJiri Olsa } 1000cd82a32eSJiri Olsa 1001b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 1002cd82a32eSJiri Olsa { 1003cd82a32eSJiri Olsa struct perf_pmu *pmu; 1004cd82a32eSJiri Olsa 1005cd82a32eSJiri Olsa list_for_each_entry(pmu, &pmus, list) 1006cd82a32eSJiri Olsa if (!strcmp(pmu->name, name)) 1007cd82a32eSJiri Olsa return pmu; 1008cd82a32eSJiri Olsa 1009cd82a32eSJiri Olsa return NULL; 1010cd82a32eSJiri Olsa } 1011cd82a32eSJiri Olsa 10123a50dc76SStephane Eranian struct perf_pmu *perf_pmu__find_by_type(unsigned int type) 10133a50dc76SStephane Eranian { 10143a50dc76SStephane Eranian struct perf_pmu *pmu; 10153a50dc76SStephane Eranian 10163a50dc76SStephane Eranian list_for_each_entry(pmu, &pmus, list) 10173a50dc76SStephane Eranian if (pmu->type == type) 10183a50dc76SStephane Eranian return pmu; 10193a50dc76SStephane Eranian 10203a50dc76SStephane Eranian return NULL; 10213a50dc76SStephane Eranian } 10223a50dc76SStephane Eranian 102350a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 102450a9667cSRobert Richter { 102550a9667cSRobert Richter /* 102650a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 102750a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 102850a9667cSRobert Richter */ 102950a9667cSRobert Richter if (!pmu) { 103050a9667cSRobert Richter pmu_read_sysfs(); 103150a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 103250a9667cSRobert Richter } 103350a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 103450a9667cSRobert Richter return pmu; 103550a9667cSRobert Richter return NULL; 103650a9667cSRobert Richter } 103750a9667cSRobert Richter 1038e76026bdSArnaldo Carvalho de Melo struct perf_pmu *evsel__find_pmu(struct evsel *evsel) 1039e12ee9f7SAdrian Hunter { 1040e12ee9f7SAdrian Hunter struct perf_pmu *pmu = NULL; 1041e12ee9f7SAdrian Hunter 1042e12ee9f7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1043e12ee9f7SAdrian Hunter if (pmu->type == evsel->core.attr.type) 1044e12ee9f7SAdrian Hunter break; 1045e12ee9f7SAdrian Hunter } 1046e12ee9f7SAdrian Hunter 1047e12ee9f7SAdrian Hunter return pmu; 1048e12ee9f7SAdrian Hunter } 1049e12ee9f7SAdrian Hunter 105039453ed5SArnaldo Carvalho de Melo bool evsel__is_aux_event(struct evsel *evsel) 1051e12ee9f7SAdrian Hunter { 1052e76026bdSArnaldo Carvalho de Melo struct perf_pmu *pmu = evsel__find_pmu(evsel); 1053e12ee9f7SAdrian Hunter 1054e12ee9f7SAdrian Hunter return pmu && pmu->auxtrace; 1055e12ee9f7SAdrian Hunter } 1056e12ee9f7SAdrian Hunter 1057b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 1058cd82a32eSJiri Olsa { 1059cd82a32eSJiri Olsa struct perf_pmu *pmu; 1060cd82a32eSJiri Olsa 1061cd82a32eSJiri Olsa /* 1062cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 1063cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 1064cd82a32eSJiri Olsa * the pmu format definitions. 1065cd82a32eSJiri Olsa */ 1066cd82a32eSJiri Olsa pmu = pmu_find(name); 1067cd82a32eSJiri Olsa if (pmu) 1068cd82a32eSJiri Olsa return pmu; 1069cd82a32eSJiri Olsa 1070cd82a32eSJiri Olsa return pmu_lookup(name); 1071cd82a32eSJiri Olsa } 1072cd82a32eSJiri Olsa 10735c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 107409ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 1075cd82a32eSJiri Olsa { 10765c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1077cd82a32eSJiri Olsa 1078cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 1079cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 1080cd82a32eSJiri Olsa return format; 1081cd82a32eSJiri Olsa 1082cd82a32eSJiri Olsa return NULL; 1083cd82a32eSJiri Olsa } 1084cd82a32eSJiri Olsa 108509ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 108609ff6071SAdrian Hunter { 108709ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 108809ff6071SAdrian Hunter __u64 bits = 0; 108909ff6071SAdrian Hunter int fbit; 109009ff6071SAdrian Hunter 109109ff6071SAdrian Hunter if (!format) 109209ff6071SAdrian Hunter return 0; 109309ff6071SAdrian Hunter 109409ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 109509ff6071SAdrian Hunter bits |= 1ULL << fbit; 109609ff6071SAdrian Hunter 109709ff6071SAdrian Hunter return bits; 109809ff6071SAdrian Hunter } 109909ff6071SAdrian Hunter 1100a1ac7de6SAdrian Hunter int perf_pmu__format_type(struct list_head *formats, const char *name) 1101a1ac7de6SAdrian Hunter { 1102a1ac7de6SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 1103a1ac7de6SAdrian Hunter 1104a1ac7de6SAdrian Hunter if (!format) 1105a1ac7de6SAdrian Hunter return -1; 1106a1ac7de6SAdrian Hunter 1107a1ac7de6SAdrian Hunter return format->value; 1108a1ac7de6SAdrian Hunter } 1109a1ac7de6SAdrian Hunter 1110cd82a32eSJiri Olsa /* 1111dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 11124d39c89fSIngo Molnar * and unformatted value (value parameter). 1113cd82a32eSJiri Olsa */ 1114dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1115dc0a6202SAdrian Hunter bool zero) 1116cd82a32eSJiri Olsa { 1117cd82a32eSJiri Olsa unsigned long fbit, vbit; 1118cd82a32eSJiri Olsa 1119cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1120cd82a32eSJiri Olsa 1121cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 1122cd82a32eSJiri Olsa continue; 1123cd82a32eSJiri Olsa 1124dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 1125dc0a6202SAdrian Hunter *v |= (1llu << fbit); 1126dc0a6202SAdrian Hunter else if (zero) 1127dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 1128cd82a32eSJiri Olsa } 1129cd82a32eSJiri Olsa } 1130cd82a32eSJiri Olsa 11310efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 11320efe6b67SAdrian Hunter { 11331b9caa10SJiri Olsa int w; 11340efe6b67SAdrian Hunter 11351b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 11361b9caa10SJiri Olsa if (!w) 11371b9caa10SJiri Olsa return 0; 11381b9caa10SJiri Olsa if (w < 64) 11391b9caa10SJiri Olsa return (1ULL << w) - 1; 11401b9caa10SJiri Olsa return -1; 11410efe6b67SAdrian Hunter } 11420efe6b67SAdrian Hunter 1143cd82a32eSJiri Olsa /* 1144688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 1145688d4dfcSCody P Schafer * in the remaining terms. 1146688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 1147688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 1148688d4dfcSCody P Schafer * in a config string) later on in the term list. 1149688d4dfcSCody P Schafer */ 1150688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 1151688d4dfcSCody P Schafer struct list_head *head_terms, 1152688d4dfcSCody P Schafer __u64 *value) 1153688d4dfcSCody P Schafer { 1154688d4dfcSCody P Schafer struct parse_events_term *t; 1155688d4dfcSCody P Schafer 1156688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 11572a3d252dSIan Rogers if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 11582a3d252dSIan Rogers t->config && !strcmp(t->config, term->config)) { 1159688d4dfcSCody P Schafer t->used = true; 1160688d4dfcSCody P Schafer *value = t->val.num; 1161688d4dfcSCody P Schafer return 0; 1162688d4dfcSCody P Schafer } 1163688d4dfcSCody P Schafer } 1164688d4dfcSCody P Schafer 1165bb963e16SNamhyung Kim if (verbose > 0) 1166688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 1167688d4dfcSCody P Schafer 1168688d4dfcSCody P Schafer return -1; 1169688d4dfcSCody P Schafer } 1170688d4dfcSCody P Schafer 1171ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 1172e64b020bSJiri Olsa { 1173e64b020bSJiri Olsa struct perf_pmu_format *format; 117411db4e29SMasami Hiramatsu char *str = NULL; 117511db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1176e64b020bSJiri Olsa unsigned i = 0; 1177e64b020bSJiri Olsa 1178ffeb883eSHe Kuang if (!formats) 1179e64b020bSJiri Olsa return NULL; 1180e64b020bSJiri Olsa 1181e64b020bSJiri Olsa /* sysfs exported terms */ 1182ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 118311db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 118411db4e29SMasami Hiramatsu goto error; 1185e64b020bSJiri Olsa 1186ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 118711db4e29SMasami Hiramatsu error: 1188ffeb883eSHe Kuang strbuf_release(&buf); 1189e64b020bSJiri Olsa 1190e64b020bSJiri Olsa return str; 1191e64b020bSJiri Olsa } 1192e64b020bSJiri Olsa 1193688d4dfcSCody P Schafer /* 1194cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 119588aca8d9SCody P Schafer * user input data - term parameter. 1196cd82a32eSJiri Olsa */ 11974ac22b48SIan Rogers static int pmu_config_term(const char *pmu_name, 11984ac22b48SIan Rogers struct list_head *formats, 1199cd82a32eSJiri Olsa struct perf_event_attr *attr, 1200dc0a6202SAdrian Hunter struct parse_events_term *term, 1201688d4dfcSCody P Schafer struct list_head *head_terms, 1202e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1203cd82a32eSJiri Olsa { 12045c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1205cd82a32eSJiri Olsa __u64 *vp; 12060efe6b67SAdrian Hunter __u64 val, max_val; 1207cd82a32eSJiri Olsa 1208cd82a32eSJiri Olsa /* 1209688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1210688d4dfcSCody P Schafer * skip it in normal eval. 1211688d4dfcSCody P Schafer */ 1212688d4dfcSCody P Schafer if (term->used) 1213688d4dfcSCody P Schafer return 0; 1214688d4dfcSCody P Schafer 1215688d4dfcSCody P Schafer /* 1216cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1217cd82a32eSJiri Olsa * to be done for them. 1218cd82a32eSJiri Olsa */ 1219cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1220cd82a32eSJiri Olsa return 0; 1221cd82a32eSJiri Olsa 1222cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 1223688d4dfcSCody P Schafer if (!format) { 1224ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 12254ac22b48SIan Rogers char *unknown_term; 12264ac22b48SIan Rogers char *help_msg; 1227ffeb883eSHe Kuang 12284ac22b48SIan Rogers if (asprintf(&unknown_term, 12294ac22b48SIan Rogers "unknown term '%s' for pmu '%s'", 12304ac22b48SIan Rogers term->config, pmu_name) < 0) 12314ac22b48SIan Rogers unknown_term = NULL; 12324ac22b48SIan Rogers help_msg = parse_events_formats_error_string(pmu_term); 12334ac22b48SIan Rogers if (err) { 1234448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 12354ac22b48SIan Rogers unknown_term, 12364ac22b48SIan Rogers help_msg); 12374ac22b48SIan Rogers } else { 12384ac22b48SIan Rogers pr_debug("%s (%s)\n", unknown_term, help_msg); 12394ac22b48SIan Rogers free(unknown_term); 1240e64b020bSJiri Olsa } 12414ac22b48SIan Rogers free(pmu_term); 1242cd82a32eSJiri Olsa return -EINVAL; 1243688d4dfcSCody P Schafer } 1244cd82a32eSJiri Olsa 1245cd82a32eSJiri Olsa switch (format->value) { 1246cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1247cd82a32eSJiri Olsa vp = &attr->config; 1248cd82a32eSJiri Olsa break; 1249cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1250cd82a32eSJiri Olsa vp = &attr->config1; 1251cd82a32eSJiri Olsa break; 1252cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1253cd82a32eSJiri Olsa vp = &attr->config2; 1254cd82a32eSJiri Olsa break; 1255cd82a32eSJiri Olsa default: 1256cd82a32eSJiri Olsa return -EINVAL; 1257cd82a32eSJiri Olsa } 1258cd82a32eSJiri Olsa 125916fa7e82SJiri Olsa /* 1260688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1261688d4dfcSCody P Schafer * using event parameters. 126216fa7e82SJiri Olsa */ 126399e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 126499e7138eSJiri Olsa if (term->no_value && 126599e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 126699e7138eSJiri Olsa if (err) { 1267448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1268448d732cSIan Rogers strdup("no value assigned for term"), 1269448d732cSIan Rogers NULL); 127099e7138eSJiri Olsa } 127199e7138eSJiri Olsa return -EINVAL; 127299e7138eSJiri Olsa } 127399e7138eSJiri Olsa 1274688d4dfcSCody P Schafer val = term->val.num; 127599e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1276688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1277bb963e16SNamhyung Kim if (verbose > 0) { 1278688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1279688d4dfcSCody P Schafer term->config, term->val.str); 1280e64b020bSJiri Olsa } 1281e64b020bSJiri Olsa if (err) { 1282448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1283448d732cSIan Rogers strdup("expected numeric value"), 1284448d732cSIan Rogers NULL); 1285e64b020bSJiri Olsa } 1286688d4dfcSCody P Schafer return -EINVAL; 1287688d4dfcSCody P Schafer } 1288688d4dfcSCody P Schafer 1289688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1290688d4dfcSCody P Schafer return -EINVAL; 1291688d4dfcSCody P Schafer } else 1292688d4dfcSCody P Schafer return -EINVAL; 1293688d4dfcSCody P Schafer 12940efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 12950efe6b67SAdrian Hunter if (val > max_val) { 12960efe6b67SAdrian Hunter if (err) { 1297448d732cSIan Rogers char *err_str; 1298448d732cSIan Rogers 1299448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1300448d732cSIan Rogers asprintf(&err_str, 13010efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1302448d732cSIan Rogers (unsigned long long)max_val) < 0 1303448d732cSIan Rogers ? strdup("value too big for format") 1304448d732cSIan Rogers : err_str, 1305448d732cSIan Rogers NULL); 13060efe6b67SAdrian Hunter return -EINVAL; 13070efe6b67SAdrian Hunter } 13080efe6b67SAdrian Hunter /* 13090efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 13100efe6b67SAdrian Hunter * silently truncated. 13110efe6b67SAdrian Hunter */ 13120efe6b67SAdrian Hunter } 13130efe6b67SAdrian Hunter 1314688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1315cd82a32eSJiri Olsa return 0; 1316cd82a32eSJiri Olsa } 1317cd82a32eSJiri Olsa 13184ac22b48SIan Rogers int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, 1319cff7f956SJiri Olsa struct perf_event_attr *attr, 1320dc0a6202SAdrian Hunter struct list_head *head_terms, 1321e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1322cd82a32eSJiri Olsa { 13236cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1324cd82a32eSJiri Olsa 1325688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 13264ac22b48SIan Rogers if (pmu_config_term(pmu_name, formats, attr, term, head_terms, 1327e64b020bSJiri Olsa zero, err)) 1328cd82a32eSJiri Olsa return -EINVAL; 1329688d4dfcSCody P Schafer } 1330cd82a32eSJiri Olsa 1331cd82a32eSJiri Olsa return 0; 1332cd82a32eSJiri Olsa } 1333cd82a32eSJiri Olsa 1334cd82a32eSJiri Olsa /* 1335cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1336cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1337cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1338cd82a32eSJiri Olsa */ 1339cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1340e64b020bSJiri Olsa struct list_head *head_terms, 1341e64b020bSJiri Olsa struct parse_events_error *err) 1342cd82a32eSJiri Olsa { 1343dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1344dc0a6202SAdrian Hunter 1345cd82a32eSJiri Olsa attr->type = pmu->type; 13464ac22b48SIan Rogers return perf_pmu__config_terms(pmu->name, &pmu->format, attr, 13474ac22b48SIan Rogers head_terms, zero, err); 1348cd82a32eSJiri Olsa } 1349cd82a32eSJiri Olsa 13505c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 13516cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1352a6146d50SZheng Yan { 13535c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1354a6146d50SZheng Yan char *name; 1355a6146d50SZheng Yan 1356a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1357a6146d50SZheng Yan return NULL; 1358a6146d50SZheng Yan 1359a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1360a6146d50SZheng Yan if (term->val.num != 1) 1361a6146d50SZheng Yan return NULL; 1362a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1363a6146d50SZheng Yan return NULL; 1364a6146d50SZheng Yan name = term->config; 1365a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1366a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1367a6146d50SZheng Yan return NULL; 1368a6146d50SZheng Yan name = term->val.str; 1369a6146d50SZheng Yan } else { 1370a6146d50SZheng Yan return NULL; 1371a6146d50SZheng Yan } 1372a6146d50SZheng Yan 1373a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 1374a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 1375a6146d50SZheng Yan return alias; 1376a6146d50SZheng Yan } 1377a6146d50SZheng Yan return NULL; 1378a6146d50SZheng Yan } 1379a6146d50SZheng Yan 1380410136f5SStephane Eranian 13811d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 13821d9e446bSJiri Olsa struct perf_pmu_info *info) 1383410136f5SStephane Eranian { 1384410136f5SStephane Eranian /* 1385410136f5SStephane Eranian * Only one term in event definition can 13861d9e446bSJiri Olsa * define unit, scale and snapshot, fail 13871d9e446bSJiri Olsa * if there's more than one. 1388410136f5SStephane Eranian */ 1389b30a7d1fSArnaldo Carvalho de Melo if ((info->unit && alias->unit[0]) || 13901d9e446bSJiri Olsa (info->scale && alias->scale) || 13911d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 1392410136f5SStephane Eranian return -EINVAL; 1393410136f5SStephane Eranian 1394b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 13951d9e446bSJiri Olsa info->unit = alias->unit; 1396410136f5SStephane Eranian 1397410136f5SStephane Eranian if (alias->scale) 13981d9e446bSJiri Olsa info->scale = alias->scale; 13991d9e446bSJiri Olsa 14001d9e446bSJiri Olsa if (alias->snapshot) 14011d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1402410136f5SStephane Eranian 1403410136f5SStephane Eranian return 0; 1404410136f5SStephane Eranian } 1405410136f5SStephane Eranian 1406a6146d50SZheng Yan /* 1407a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1408a6146d50SZheng Yan * defined for the alias 1409a6146d50SZheng Yan */ 1410410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 141146441bdcSMatt Fleming struct perf_pmu_info *info) 1412a6146d50SZheng Yan { 14136cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 14145c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1415a6146d50SZheng Yan int ret; 1416a6146d50SZheng Yan 1417044330c1SMatt Fleming info->per_pkg = false; 1418044330c1SMatt Fleming 14198a398897SStephane Eranian /* 14208a398897SStephane Eranian * Mark unit and scale as not set 14218a398897SStephane Eranian * (different from default values, see below) 14228a398897SStephane Eranian */ 142346441bdcSMatt Fleming info->unit = NULL; 142446441bdcSMatt Fleming info->scale = 0.0; 14251d9e446bSJiri Olsa info->snapshot = false; 142637932c18SAndi Kleen info->metric_expr = NULL; 142796284814SAndi Kleen info->metric_name = NULL; 1428410136f5SStephane Eranian 1429a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1430a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1431a6146d50SZheng Yan if (!alias) 1432a6146d50SZheng Yan continue; 1433a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 1434a6146d50SZheng Yan if (ret) 1435a6146d50SZheng Yan return ret; 1436410136f5SStephane Eranian 14371d9e446bSJiri Olsa ret = check_info_data(alias, info); 1438410136f5SStephane Eranian if (ret) 1439410136f5SStephane Eranian return ret; 1440410136f5SStephane Eranian 1441044330c1SMatt Fleming if (alias->per_pkg) 1442044330c1SMatt Fleming info->per_pkg = true; 144337932c18SAndi Kleen info->metric_expr = alias->metric_expr; 144496284814SAndi Kleen info->metric_name = alias->metric_name; 1445044330c1SMatt Fleming 1446e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 14471dc92556SIan Rogers parse_events_term__delete(term); 1448a6146d50SZheng Yan } 14498a398897SStephane Eranian 14508a398897SStephane Eranian /* 14518a398897SStephane Eranian * if no unit or scale found in aliases, then 14528a398897SStephane Eranian * set defaults as for evsel 14538a398897SStephane Eranian * unit cannot left to NULL 14548a398897SStephane Eranian */ 145546441bdcSMatt Fleming if (info->unit == NULL) 145646441bdcSMatt Fleming info->unit = ""; 14578a398897SStephane Eranian 145846441bdcSMatt Fleming if (info->scale == 0.0) 145946441bdcSMatt Fleming info->scale = 1.0; 14608a398897SStephane Eranian 1461a6146d50SZheng Yan return 0; 1462a6146d50SZheng Yan } 1463a6146d50SZheng Yan 1464cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 1465cd82a32eSJiri Olsa int config, unsigned long *bits) 1466cd82a32eSJiri Olsa { 14675c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1468cd82a32eSJiri Olsa 1469cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 1470cd82a32eSJiri Olsa if (!format) 1471cd82a32eSJiri Olsa return -ENOMEM; 1472cd82a32eSJiri Olsa 1473cd82a32eSJiri Olsa format->name = strdup(name); 1474cd82a32eSJiri Olsa format->value = config; 1475cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 1476cd82a32eSJiri Olsa 1477cd82a32eSJiri Olsa list_add_tail(&format->list, list); 1478cd82a32eSJiri Olsa return 0; 1479cd82a32eSJiri Olsa } 1480cd82a32eSJiri Olsa 1481cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 1482cd82a32eSJiri Olsa { 1483cd82a32eSJiri Olsa long b; 1484cd82a32eSJiri Olsa 1485cd82a32eSJiri Olsa if (!to) 1486cd82a32eSJiri Olsa to = from; 1487cd82a32eSJiri Olsa 148815268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1489cd82a32eSJiri Olsa for (b = from; b <= to; b++) 1490cd82a32eSJiri Olsa set_bit(b, bits); 1491cd82a32eSJiri Olsa } 1492dc098b35SAndi Kleen 1493d26383dcSNamhyung Kim void perf_pmu__del_formats(struct list_head *formats) 1494d26383dcSNamhyung Kim { 1495d26383dcSNamhyung Kim struct perf_pmu_format *fmt, *tmp; 1496d26383dcSNamhyung Kim 1497d26383dcSNamhyung Kim list_for_each_entry_safe(fmt, tmp, formats, list) { 1498d26383dcSNamhyung Kim list_del(&fmt->list); 1499d26383dcSNamhyung Kim free(fmt->name); 1500d26383dcSNamhyung Kim free(fmt); 1501d26383dcSNamhyung Kim } 1502d26383dcSNamhyung Kim } 1503d26383dcSNamhyung Kim 1504aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 1505aaea3617SCody P Schafer { 1506aaea3617SCody P Schafer if (b > a) 1507aaea3617SCody P Schafer return 0; 1508aaea3617SCody P Schafer return a - b; 1509aaea3617SCody P Schafer } 1510aaea3617SCody P Schafer 1511dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 1512dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1513dc098b35SAndi Kleen { 1514aaea3617SCody P Schafer struct parse_events_term *term; 1515aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1516aaea3617SCody P Schafer 1517aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 1518aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1519aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 1520aaea3617SCody P Schafer ",%s=%s", term->config, 1521aaea3617SCody P Schafer term->val.str); 1522aaea3617SCody P Schafer } 1523aaea3617SCody P Schafer 1524aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1525aaea3617SCody P Schafer buf[used] = '/'; 1526aaea3617SCody P Schafer used++; 1527aaea3617SCody P Schafer } 1528aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1529aaea3617SCody P Schafer buf[used] = '\0'; 1530aaea3617SCody P Schafer used++; 1531aaea3617SCody P Schafer } else 1532aaea3617SCody P Schafer buf[len - 1] = '\0'; 1533aaea3617SCody P Schafer 1534dc098b35SAndi Kleen return buf; 1535dc098b35SAndi Kleen } 1536dc098b35SAndi Kleen 1537dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 1538dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1539dc098b35SAndi Kleen { 1540dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 1541dc098b35SAndi Kleen return buf; 1542dc098b35SAndi Kleen } 1543dc098b35SAndi Kleen 1544dd5f1036SAndi Kleen struct sevent { 154508e60ed1SAndi Kleen char *name; 154608e60ed1SAndi Kleen char *desc; 1547dd5f1036SAndi Kleen char *topic; 1548f2361024SAndi Kleen char *str; 1549f2361024SAndi Kleen char *pmu; 15507f372a63SAndi Kleen char *metric_expr; 155196284814SAndi Kleen char *metric_name; 1552ce0dc7d2SJohn Garry int is_cpu; 155308e60ed1SAndi Kleen }; 155408e60ed1SAndi Kleen 1555dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b) 1556dc098b35SAndi Kleen { 1557dd5f1036SAndi Kleen const struct sevent *as = a; 1558dd5f1036SAndi Kleen const struct sevent *bs = b; 155908e60ed1SAndi Kleen 156008e60ed1SAndi Kleen /* Put extra events last */ 156108e60ed1SAndi Kleen if (!!as->desc != !!bs->desc) 156208e60ed1SAndi Kleen return !!as->desc - !!bs->desc; 1563dd5f1036SAndi Kleen if (as->topic && bs->topic) { 1564dd5f1036SAndi Kleen int n = strcmp(as->topic, bs->topic); 1565dd5f1036SAndi Kleen 1566dd5f1036SAndi Kleen if (n) 1567dd5f1036SAndi Kleen return n; 1568dd5f1036SAndi Kleen } 1569ce0dc7d2SJohn Garry 1570ce0dc7d2SJohn Garry /* Order CPU core events to be first */ 1571ce0dc7d2SJohn Garry if (as->is_cpu != bs->is_cpu) 1572ce0dc7d2SJohn Garry return bs->is_cpu - as->is_cpu; 1573ce0dc7d2SJohn Garry 157408e60ed1SAndi Kleen return strcmp(as->name, bs->name); 157508e60ed1SAndi Kleen } 157608e60ed1SAndi Kleen 157708e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr) 157808e60ed1SAndi Kleen { 157908e60ed1SAndi Kleen int column = start; 158008e60ed1SAndi Kleen int n; 158108e60ed1SAndi Kleen 158208e60ed1SAndi Kleen while (*s) { 158308e60ed1SAndi Kleen int wlen = strcspn(s, " \t"); 158408e60ed1SAndi Kleen 158508e60ed1SAndi Kleen if (column + wlen >= max && column > start) { 158608e60ed1SAndi Kleen printf("\n%*s", start, ""); 158708e60ed1SAndi Kleen column = start + corr; 158808e60ed1SAndi Kleen } 158908e60ed1SAndi Kleen n = printf("%s%.*s", column > start ? " " : "", wlen, s); 159008e60ed1SAndi Kleen if (n <= 0) 159108e60ed1SAndi Kleen break; 159208e60ed1SAndi Kleen s += wlen; 159308e60ed1SAndi Kleen column += n; 159432858480SArnaldo Carvalho de Melo s = skip_spaces(s); 159508e60ed1SAndi Kleen } 1596dc098b35SAndi Kleen } 1597dc098b35SAndi Kleen 1598d504fae9SJohn Garry bool is_pmu_core(const char *name) 1599d504fae9SJohn Garry { 1600d504fae9SJohn Garry return !strcmp(name, "cpu") || is_arm_pmu_core(name); 1601d504fae9SJohn Garry } 1602d504fae9SJohn Garry 1603c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, 1604a7f6c8c8SJin Yao bool long_desc, bool details_flag, bool deprecated) 1605dc098b35SAndi Kleen { 1606dc098b35SAndi Kleen struct perf_pmu *pmu; 1607dc098b35SAndi Kleen struct perf_pmu_alias *alias; 1608dc098b35SAndi Kleen char buf[1024]; 1609dc098b35SAndi Kleen int printed = 0; 1610dc098b35SAndi Kleen int len, j; 1611dd5f1036SAndi Kleen struct sevent *aliases; 161208e60ed1SAndi Kleen int numdesc = 0; 161361eb2eb4SAndi Kleen int columns = pager_get_columns(); 1614dd5f1036SAndi Kleen char *topic = NULL; 1615dc098b35SAndi Kleen 1616dc098b35SAndi Kleen pmu = NULL; 1617dc098b35SAndi Kleen len = 0; 161842634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1619dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 1620dc098b35SAndi Kleen len++; 162142634bc7SAdrian Hunter if (pmu->selectable) 162242634bc7SAdrian Hunter len++; 162342634bc7SAdrian Hunter } 1624dd5f1036SAndi Kleen aliases = zalloc(sizeof(struct sevent) * len); 1625dc098b35SAndi Kleen if (!aliases) 16267e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1627dc098b35SAndi Kleen pmu = NULL; 1628dc098b35SAndi Kleen j = 0; 162942634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1630dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 163108e60ed1SAndi Kleen char *name = alias->desc ? alias->name : 163208e60ed1SAndi Kleen format_alias(buf, sizeof(buf), pmu, alias); 1633c1b4745bSJohn Garry bool is_cpu = is_pmu_core(pmu->name); 1634dc098b35SAndi Kleen 1635a7f6c8c8SJin Yao if (alias->deprecated && !deprecated) 1636a7f6c8c8SJin Yao continue; 1637a7f6c8c8SJin Yao 1638dc098b35SAndi Kleen if (event_glob != NULL && 163938d14f0cSAndi Kleen !(strglobmatch_nocase(name, event_glob) || 164038d14f0cSAndi Kleen (!is_cpu && strglobmatch_nocase(alias->name, 164167bdc35fSAndi Kleen event_glob)) || 164267bdc35fSAndi Kleen (alias->topic && 164367bdc35fSAndi Kleen strglobmatch_nocase(alias->topic, event_glob)))) 1644dc098b35SAndi Kleen continue; 16457e4772dcSArnaldo Carvalho de Melo 164608e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 16477e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 16487e4772dcSArnaldo Carvalho de Melo 164908e60ed1SAndi Kleen aliases[j].name = name; 165008e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 165108e60ed1SAndi Kleen aliases[j].name = format_alias_or(buf, 165208e60ed1SAndi Kleen sizeof(buf), 165308e60ed1SAndi Kleen pmu, alias); 165408e60ed1SAndi Kleen aliases[j].name = strdup(aliases[j].name); 165508e60ed1SAndi Kleen if (!aliases[j].name) 16567e4772dcSArnaldo Carvalho de Melo goto out_enomem; 165708e60ed1SAndi Kleen 1658c8d6828aSSukadev Bhattiprolu aliases[j].desc = long_desc ? alias->long_desc : 1659c8d6828aSSukadev Bhattiprolu alias->desc; 1660dd5f1036SAndi Kleen aliases[j].topic = alias->topic; 1661f2361024SAndi Kleen aliases[j].str = alias->str; 1662f2361024SAndi Kleen aliases[j].pmu = pmu->name; 16637f372a63SAndi Kleen aliases[j].metric_expr = alias->metric_expr; 166496284814SAndi Kleen aliases[j].metric_name = alias->metric_name; 1665ce0dc7d2SJohn Garry aliases[j].is_cpu = is_cpu; 1666dc098b35SAndi Kleen j++; 1667dc098b35SAndi Kleen } 1668fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1669fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 16707e4772dcSArnaldo Carvalho de Melo char *s; 16717e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 16727e4772dcSArnaldo Carvalho de Melo goto out_enomem; 167308e60ed1SAndi Kleen aliases[j].name = s; 167442634bc7SAdrian Hunter j++; 167542634bc7SAdrian Hunter } 167642634bc7SAdrian Hunter } 1677dc098b35SAndi Kleen len = j; 1678dd5f1036SAndi Kleen qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1679dc098b35SAndi Kleen for (j = 0; j < len; j++) { 168015b22ed3SAndi Kleen /* Skip duplicates */ 168115b22ed3SAndi Kleen if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) 168215b22ed3SAndi Kleen continue; 1683dc098b35SAndi Kleen if (name_only) { 168408e60ed1SAndi Kleen printf("%s ", aliases[j].name); 1685dc098b35SAndi Kleen continue; 1686dc098b35SAndi Kleen } 16871c5f01feSAndi Kleen if (aliases[j].desc && !quiet_flag) { 168808e60ed1SAndi Kleen if (numdesc++ == 0) 168908e60ed1SAndi Kleen printf("\n"); 1690dd5f1036SAndi Kleen if (aliases[j].topic && (!topic || 1691dd5f1036SAndi Kleen strcmp(topic, aliases[j].topic))) { 1692dd5f1036SAndi Kleen printf("%s%s:\n", topic ? "\n" : "", 1693dd5f1036SAndi Kleen aliases[j].topic); 1694dd5f1036SAndi Kleen topic = aliases[j].topic; 1695dd5f1036SAndi Kleen } 169608e60ed1SAndi Kleen printf(" %-50s\n", aliases[j].name); 169708e60ed1SAndi Kleen printf("%*s", 8, "["); 169808e60ed1SAndi Kleen wordwrap(aliases[j].desc, 8, columns, 0); 169908e60ed1SAndi Kleen printf("]\n"); 1700bf874fcfSAndi Kleen if (details_flag) { 17017f372a63SAndi Kleen printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); 170296284814SAndi Kleen if (aliases[j].metric_name) 170396284814SAndi Kleen printf(" MetricName: %s", aliases[j].metric_name); 17047f372a63SAndi Kleen if (aliases[j].metric_expr) 17057f372a63SAndi Kleen printf(" MetricExpr: %s", aliases[j].metric_expr); 17067f372a63SAndi Kleen putchar('\n'); 17077f372a63SAndi Kleen } 170808e60ed1SAndi Kleen } else 170908e60ed1SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j].name); 1710dc098b35SAndi Kleen printed++; 1711dc098b35SAndi Kleen } 1712dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1713dc098b35SAndi Kleen printf("\n"); 17147e4772dcSArnaldo Carvalho de Melo out_free: 17157e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 171608e60ed1SAndi Kleen zfree(&aliases[j].name); 17177e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 17187e4772dcSArnaldo Carvalho de Melo return; 17197e4772dcSArnaldo Carvalho de Melo 17207e4772dcSArnaldo Carvalho de Melo out_enomem: 17217e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 17227e4772dcSArnaldo Carvalho de Melo if (aliases) 17237e4772dcSArnaldo Carvalho de Melo goto out_free; 1724dc098b35SAndi Kleen } 17254cabc3d1SAndi Kleen 17264cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 17274cabc3d1SAndi Kleen { 17284cabc3d1SAndi Kleen struct perf_pmu *pmu; 17294cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 17304cabc3d1SAndi Kleen 17314cabc3d1SAndi Kleen pmu = NULL; 17324cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 17334cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 17344cabc3d1SAndi Kleen continue; 17354cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 17364cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 17374cabc3d1SAndi Kleen return true; 17384cabc3d1SAndi Kleen } 17394cabc3d1SAndi Kleen return false; 17404cabc3d1SAndi Kleen } 17417d4bdab5SAdrian Hunter 17427d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 17437d4bdab5SAdrian Hunter { 17447d4bdab5SAdrian Hunter char path[PATH_MAX]; 17457d4bdab5SAdrian Hunter const char *sysfs; 17467d4bdab5SAdrian Hunter 17477d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 17487d4bdab5SAdrian Hunter if (!sysfs) 17497d4bdab5SAdrian Hunter return NULL; 17507d4bdab5SAdrian Hunter 17517d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 17527d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 1753d9664582SAndi Kleen if (!file_available(path)) 17547d4bdab5SAdrian Hunter return NULL; 17557d4bdab5SAdrian Hunter return fopen(path, "r"); 17567d4bdab5SAdrian Hunter } 17577d4bdab5SAdrian Hunter 17587d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 17597d4bdab5SAdrian Hunter ...) 17607d4bdab5SAdrian Hunter { 17617d4bdab5SAdrian Hunter va_list args; 17627d4bdab5SAdrian Hunter FILE *file; 17637d4bdab5SAdrian Hunter int ret = EOF; 17647d4bdab5SAdrian Hunter 17657d4bdab5SAdrian Hunter va_start(args, fmt); 17667d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 17677d4bdab5SAdrian Hunter if (file) { 17687d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 17697d4bdab5SAdrian Hunter fclose(file); 17707d4bdab5SAdrian Hunter } 17717d4bdab5SAdrian Hunter va_end(args); 17727d4bdab5SAdrian Hunter return ret; 17737d4bdab5SAdrian Hunter } 17749fbc61f8SKan Liang 17759fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 17769fbc61f8SKan Liang { 17779fbc61f8SKan Liang struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 17789fbc61f8SKan Liang 17799fbc61f8SKan Liang if (!caps) 17809fbc61f8SKan Liang return -ENOMEM; 17819fbc61f8SKan Liang 17829fbc61f8SKan Liang caps->name = strdup(name); 17839fbc61f8SKan Liang if (!caps->name) 17849fbc61f8SKan Liang goto free_caps; 17859fbc61f8SKan Liang caps->value = strndup(value, strlen(value) - 1); 17869fbc61f8SKan Liang if (!caps->value) 17879fbc61f8SKan Liang goto free_name; 17889fbc61f8SKan Liang list_add_tail(&caps->list, list); 17899fbc61f8SKan Liang return 0; 17909fbc61f8SKan Liang 17919fbc61f8SKan Liang free_name: 17929fbc61f8SKan Liang zfree(caps->name); 17939fbc61f8SKan Liang free_caps: 17949fbc61f8SKan Liang free(caps); 17959fbc61f8SKan Liang 17969fbc61f8SKan Liang return -ENOMEM; 17979fbc61f8SKan Liang } 17989fbc61f8SKan Liang 17999fbc61f8SKan Liang /* 18009fbc61f8SKan Liang * Reading/parsing the given pmu capabilities, which should be located at: 18019fbc61f8SKan Liang * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 18029fbc61f8SKan Liang * Return the number of capabilities 18039fbc61f8SKan Liang */ 18049fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu) 18059fbc61f8SKan Liang { 18069fbc61f8SKan Liang struct stat st; 18079fbc61f8SKan Liang char caps_path[PATH_MAX]; 18089fbc61f8SKan Liang const char *sysfs = sysfs__mountpoint(); 18099fbc61f8SKan Liang DIR *caps_dir; 18109fbc61f8SKan Liang struct dirent *evt_ent; 18119fbc61f8SKan Liang int nr_caps = 0; 18129fbc61f8SKan Liang 18139fbc61f8SKan Liang if (!sysfs) 18149fbc61f8SKan Liang return -1; 18159fbc61f8SKan Liang 18169fbc61f8SKan Liang snprintf(caps_path, PATH_MAX, 18179fbc61f8SKan Liang "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name); 18189fbc61f8SKan Liang 18199fbc61f8SKan Liang if (stat(caps_path, &st) < 0) 18209fbc61f8SKan Liang return 0; /* no error if caps does not exist */ 18219fbc61f8SKan Liang 18229fbc61f8SKan Liang caps_dir = opendir(caps_path); 18239fbc61f8SKan Liang if (!caps_dir) 18249fbc61f8SKan Liang return -EINVAL; 18259fbc61f8SKan Liang 18269fbc61f8SKan Liang while ((evt_ent = readdir(caps_dir)) != NULL) { 18279fbc61f8SKan Liang char path[PATH_MAX + NAME_MAX + 1]; 18289fbc61f8SKan Liang char *name = evt_ent->d_name; 18299fbc61f8SKan Liang char value[128]; 18309fbc61f8SKan Liang FILE *file; 18319fbc61f8SKan Liang 18329fbc61f8SKan Liang if (!strcmp(name, ".") || !strcmp(name, "..")) 18339fbc61f8SKan Liang continue; 18349fbc61f8SKan Liang 18359fbc61f8SKan Liang snprintf(path, sizeof(path), "%s/%s", caps_path, name); 18369fbc61f8SKan Liang 18379fbc61f8SKan Liang file = fopen(path, "r"); 18389fbc61f8SKan Liang if (!file) 18399fbc61f8SKan Liang continue; 18409fbc61f8SKan Liang 18419fbc61f8SKan Liang if (!fgets(value, sizeof(value), file) || 18429fbc61f8SKan Liang (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 18439fbc61f8SKan Liang fclose(file); 18449fbc61f8SKan Liang continue; 18459fbc61f8SKan Liang } 18469fbc61f8SKan Liang 18479fbc61f8SKan Liang nr_caps++; 18489fbc61f8SKan Liang fclose(file); 18499fbc61f8SKan Liang } 18509fbc61f8SKan Liang 18519fbc61f8SKan Liang closedir(caps_dir); 18529fbc61f8SKan Liang 18539fbc61f8SKan Liang return nr_caps; 18549fbc61f8SKan Liang } 1855e4064776SJin Yao 1856e4064776SJin Yao void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 1857e4064776SJin Yao char *name) 1858e4064776SJin Yao { 1859e4064776SJin Yao struct perf_pmu_format *format; 1860e4064776SJin Yao __u64 masks = 0, bits; 1861e4064776SJin Yao char buf[100]; 1862e4064776SJin Yao unsigned int i; 1863e4064776SJin Yao 1864e4064776SJin Yao list_for_each_entry(format, &pmu->format, list) { 1865e4064776SJin Yao if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG) 1866e4064776SJin Yao continue; 1867e4064776SJin Yao 1868e4064776SJin Yao for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1869e4064776SJin Yao masks |= 1ULL << i; 1870e4064776SJin Yao } 1871e4064776SJin Yao 1872e4064776SJin Yao /* 1873e4064776SJin Yao * Kernel doesn't export any valid format bits. 1874e4064776SJin Yao */ 1875e4064776SJin Yao if (masks == 0) 1876e4064776SJin Yao return; 1877e4064776SJin Yao 1878e4064776SJin Yao bits = config & ~masks; 1879e4064776SJin Yao if (bits == 0) 1880e4064776SJin Yao return; 1881e4064776SJin Yao 1882e4064776SJin Yao bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1883e4064776SJin Yao 1884e4064776SJin Yao pr_warning("WARNING: event '%s' not valid (bits %s of config " 1885e4064776SJin Yao "'%llx' not supported by kernel)!\n", 1886e4064776SJin Yao name ?: "N/A", buf, config); 1887e4064776SJin Yao } 1888c5a26ea4SJin Yao 1889c5a26ea4SJin Yao bool perf_pmu__has_hybrid(void) 1890c5a26ea4SJin Yao { 1891c5a26ea4SJin Yao if (!hybrid_scanned) { 1892c5a26ea4SJin Yao hybrid_scanned = true; 1893c5a26ea4SJin Yao perf_pmu__scan(NULL); 1894c5a26ea4SJin Yao } 1895c5a26ea4SJin Yao 1896c5a26ea4SJin Yao return !list_empty(&perf_pmu__hybrid_pmus); 1897c5a26ea4SJin Yao } 1898*c47a5599SJin Yao 1899*c47a5599SJin Yao int perf_pmu__match(char *pattern, char *name, char *tok) 1900*c47a5599SJin Yao { 1901*c47a5599SJin Yao if (fnmatch(pattern, name, 0)) 1902*c47a5599SJin Yao return -1; 1903*c47a5599SJin Yao 1904*c47a5599SJin Yao if (tok && !perf_pmu__valid_suffix(name, tok)) 1905*c47a5599SJin Yao return -1; 1906*c47a5599SJin Yao 1907*c47a5599SJin Yao return 0; 1908*c47a5599SJin Yao } 1909