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> 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> 21c47a5599SJin 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 745c07d5c92SJohn Garry /* 746c07d5c92SJohn Garry * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name 747c07d5c92SJohn Garry * to be valid. 748c07d5c92SJohn Garry */ 749c07d5c92SJohn Garry static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok) 750c47a5599SJin Yao { 751c07d5c92SJohn Garry const char *p; 752c47a5599SJin Yao 753c47a5599SJin Yao if (strncmp(pmu_name, tok, strlen(tok))) 754c47a5599SJin Yao return false; 755c47a5599SJin Yao 756c47a5599SJin Yao p = pmu_name + strlen(tok); 757c47a5599SJin Yao if (*p == 0) 758c47a5599SJin Yao return true; 759c47a5599SJin Yao 760c07d5c92SJohn Garry if (*p == '_') 761c47a5599SJin Yao ++p; 762c07d5c92SJohn Garry 763c07d5c92SJohn Garry /* Ensure we end in a number */ 764c07d5c92SJohn Garry while (1) { 765c07d5c92SJohn Garry if (!isdigit(*p)) 766c47a5599SJin Yao return false; 767c07d5c92SJohn Garry if (*(++p) == 0) 768c07d5c92SJohn Garry break; 769c07d5c92SJohn Garry } 770c47a5599SJin Yao 771c47a5599SJin Yao return true; 772c47a5599SJin Yao } 773c47a5599SJin Yao 7745b9a5000SJohn Garry bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 775730670b1SJohn Garry { 776730670b1SJohn Garry char *tmp = NULL, *tok, *str; 777730670b1SJohn Garry bool res; 778730670b1SJohn Garry 779730670b1SJohn Garry str = strdup(pmu_name); 780730670b1SJohn Garry if (!str) 781730670b1SJohn Garry return false; 782730670b1SJohn Garry 783730670b1SJohn Garry /* 784730670b1SJohn Garry * uncore alias may be from different PMU with common prefix 785730670b1SJohn Garry */ 786730670b1SJohn Garry tok = strtok_r(str, ",", &tmp); 787730670b1SJohn Garry if (strncmp(pmu_name, tok, strlen(tok))) { 788730670b1SJohn Garry res = false; 789730670b1SJohn Garry goto out; 790730670b1SJohn Garry } 791730670b1SJohn Garry 792730670b1SJohn Garry /* 793730670b1SJohn Garry * Match more complex aliases where the alias name is a comma-delimited 794730670b1SJohn Garry * list of tokens, orderly contained in the matching PMU name. 795730670b1SJohn Garry * 796730670b1SJohn Garry * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 797730670b1SJohn Garry * match "socket" in "socketX_pmunameY" and then "pmuname" in 798730670b1SJohn Garry * "pmunameY". 799730670b1SJohn Garry */ 800c07d5c92SJohn Garry while (1) { 801c07d5c92SJohn Garry char *next_tok = strtok_r(NULL, ",", &tmp); 802c07d5c92SJohn Garry 803730670b1SJohn Garry name = strstr(name, tok); 804c07d5c92SJohn Garry if (!name || 805c07d5c92SJohn Garry (!next_tok && !perf_pmu__valid_suffix(name, tok))) { 806730670b1SJohn Garry res = false; 807730670b1SJohn Garry goto out; 808730670b1SJohn Garry } 809c07d5c92SJohn Garry if (!next_tok) 810c07d5c92SJohn Garry break; 811c07d5c92SJohn Garry tok = next_tok; 812c07d5c92SJohn Garry name += strlen(tok); 813730670b1SJohn Garry } 814730670b1SJohn Garry 815730670b1SJohn Garry res = true; 816730670b1SJohn Garry out: 817730670b1SJohn Garry free(str); 818730670b1SJohn Garry return res; 819730670b1SJohn Garry } 820730670b1SJohn Garry 821933f82ffSSukadev Bhattiprolu /* 822933f82ffSSukadev Bhattiprolu * From the pmu_events_map, find the table of PMU events that corresponds 823933f82ffSSukadev Bhattiprolu * to the current running CPU. Then, add all PMU events from that table 824933f82ffSSukadev Bhattiprolu * as aliases. 825933f82ffSSukadev Bhattiprolu */ 826e45ad701SJohn Garry void pmu_add_cpu_aliases_map(struct list_head *head, struct perf_pmu *pmu, 827e45ad701SJohn Garry struct pmu_events_map *map) 828933f82ffSSukadev Bhattiprolu { 829933f82ffSSukadev Bhattiprolu int i; 83054e32dc0SGanapatrao Kulkarni const char *name = pmu->name; 831933f82ffSSukadev Bhattiprolu /* 832933f82ffSSukadev Bhattiprolu * Found a matching PMU events table. Create aliases 833933f82ffSSukadev Bhattiprolu */ 834933f82ffSSukadev Bhattiprolu i = 0; 835933f82ffSSukadev Bhattiprolu while (1) { 836599ee18fSJohn Garry const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu"; 837599ee18fSJohn Garry struct pmu_event *pe = &map->table[i++]; 838599ee18fSJohn Garry const char *pname = pe->pmu ? pe->pmu : cpu_name; 839fedb2b51SAndi Kleen 840b18f3e36SAndi Kleen if (!pe->name) { 841b18f3e36SAndi Kleen if (pe->metric_group || pe->metric_name) 842b18f3e36SAndi Kleen continue; 843933f82ffSSukadev Bhattiprolu break; 844b18f3e36SAndi Kleen } 845933f82ffSSukadev Bhattiprolu 8465806099aSJohn Garry if (pmu->is_uncore && pmu_uncore_alias_match(pname, name)) 847e94d6b7fSKan Liang goto new_alias; 848e94d6b7fSKan Liang 849ea1fa48cSThomas Richter if (strcmp(pname, name)) 85014b22ae0SGanapatrao Kulkarni continue; 851fedb2b51SAndi Kleen 852e94d6b7fSKan Liang new_alias: 853933f82ffSSukadev Bhattiprolu /* need type casts to override 'const' */ 854933f82ffSSukadev Bhattiprolu __perf_pmu__new_alias(head, NULL, (char *)pe->name, 855c8d6828aSSukadev Bhattiprolu (char *)pe->desc, (char *)pe->event, 856eab35953SJin Yao pe); 857933f82ffSSukadev Bhattiprolu } 858933f82ffSSukadev Bhattiprolu } 859933f82ffSSukadev Bhattiprolu 860e45ad701SJohn Garry static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) 861e45ad701SJohn Garry { 862e45ad701SJohn Garry struct pmu_events_map *map; 863e45ad701SJohn Garry 864e45ad701SJohn Garry map = perf_pmu__find_map(pmu); 865e45ad701SJohn Garry if (!map) 866e45ad701SJohn Garry return; 867e45ad701SJohn Garry 868e45ad701SJohn Garry pmu_add_cpu_aliases_map(head, pmu, map); 869e45ad701SJohn Garry } 870e45ad701SJohn Garry 8714513c719SJohn Garry void pmu_for_each_sys_event(pmu_sys_event_iter_fn fn, void *data) 8724513c719SJohn Garry { 8734513c719SJohn Garry int i = 0; 8744513c719SJohn Garry 8754513c719SJohn Garry while (1) { 8764513c719SJohn Garry struct pmu_sys_events *event_table; 8774513c719SJohn Garry int j = 0; 8784513c719SJohn Garry 8794513c719SJohn Garry event_table = &pmu_sys_event_tables[i++]; 8804513c719SJohn Garry 8814513c719SJohn Garry if (!event_table->table) 8824513c719SJohn Garry break; 8834513c719SJohn Garry 8844513c719SJohn Garry while (1) { 8854513c719SJohn Garry struct pmu_event *pe = &event_table->table[j++]; 8864513c719SJohn Garry int ret; 8874513c719SJohn Garry 8884513c719SJohn Garry if (!pe->name && !pe->metric_group && !pe->metric_name) 8894513c719SJohn Garry break; 8904513c719SJohn Garry 8914513c719SJohn Garry ret = fn(pe, data); 8924513c719SJohn Garry if (ret) 8934513c719SJohn Garry break; 8944513c719SJohn Garry } 8954513c719SJohn Garry } 8964513c719SJohn Garry } 8974513c719SJohn Garry 8984513c719SJohn Garry struct pmu_sys_event_iter_data { 8994513c719SJohn Garry struct list_head *head; 9004513c719SJohn Garry struct perf_pmu *pmu; 9014513c719SJohn Garry }; 9024513c719SJohn Garry 9034513c719SJohn Garry static int pmu_add_sys_aliases_iter_fn(struct pmu_event *pe, void *data) 9044513c719SJohn Garry { 9054513c719SJohn Garry struct pmu_sys_event_iter_data *idata = data; 9064513c719SJohn Garry struct perf_pmu *pmu = idata->pmu; 9074513c719SJohn Garry 9084513c719SJohn Garry if (!pe->name) { 9094513c719SJohn Garry if (pe->metric_group || pe->metric_name) 9104513c719SJohn Garry return 0; 9114513c719SJohn Garry return -EINVAL; 9124513c719SJohn Garry } 9134513c719SJohn Garry 9144513c719SJohn Garry if (!pe->compat || !pe->pmu) 9154513c719SJohn Garry return 0; 9164513c719SJohn Garry 9174513c719SJohn Garry if (!strcmp(pmu->id, pe->compat) && 9184513c719SJohn Garry pmu_uncore_alias_match(pe->pmu, pmu->name)) { 9194513c719SJohn Garry __perf_pmu__new_alias(idata->head, NULL, 9204513c719SJohn Garry (char *)pe->name, 9214513c719SJohn Garry (char *)pe->desc, 9224513c719SJohn Garry (char *)pe->event, 923eab35953SJin Yao pe); 9244513c719SJohn Garry } 9254513c719SJohn Garry 9264513c719SJohn Garry return 0; 9274513c719SJohn Garry } 9284513c719SJohn Garry 929e199f47fSJohn Garry void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu) 9304513c719SJohn Garry { 9314513c719SJohn Garry struct pmu_sys_event_iter_data idata = { 9324513c719SJohn Garry .head = head, 9334513c719SJohn Garry .pmu = pmu, 9344513c719SJohn Garry }; 9354513c719SJohn Garry 9364513c719SJohn Garry if (!pmu->id) 9374513c719SJohn Garry return; 9384513c719SJohn Garry 9394513c719SJohn Garry pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata); 9404513c719SJohn Garry } 9414513c719SJohn Garry 942c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 943dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 944dc0a6202SAdrian Hunter { 945dc0a6202SAdrian Hunter return NULL; 946dc0a6202SAdrian Hunter } 947dc0a6202SAdrian Hunter 94890a86bdeSJiri Olsa static int pmu_max_precise(const char *name) 94990a86bdeSJiri Olsa { 95090a86bdeSJiri Olsa char path[PATH_MAX]; 95190a86bdeSJiri Olsa int max_precise = -1; 95290a86bdeSJiri Olsa 95390a86bdeSJiri Olsa scnprintf(path, PATH_MAX, 95490a86bdeSJiri Olsa "bus/event_source/devices/%s/caps/max_precise", 95590a86bdeSJiri Olsa name); 95690a86bdeSJiri Olsa 95790a86bdeSJiri Olsa sysfs__read_int(path, &max_precise); 95890a86bdeSJiri Olsa return max_precise; 95990a86bdeSJiri Olsa } 96090a86bdeSJiri Olsa 961b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name) 962cd82a32eSJiri Olsa { 963cd82a32eSJiri Olsa struct perf_pmu *pmu; 964cd82a32eSJiri Olsa LIST_HEAD(format); 965a6146d50SZheng Yan LIST_HEAD(aliases); 966cd82a32eSJiri Olsa __u32 type; 96749afa7f6SJin Yao bool is_hybrid = perf_pmu__hybrid_mounted(name); 96849afa7f6SJin Yao 96949afa7f6SJin Yao /* 97049afa7f6SJin Yao * Check pmu name for hybrid and the pmu may be invalid in sysfs 97149afa7f6SJin Yao */ 97249afa7f6SJin Yao if (!strncmp(name, "cpu_", 4) && !is_hybrid) 97349afa7f6SJin Yao return NULL; 974cd82a32eSJiri Olsa 975cd82a32eSJiri Olsa /* 976cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 977cd82a32eSJiri Olsa * type value and format definitions. Load both right 978cd82a32eSJiri Olsa * now. 979cd82a32eSJiri Olsa */ 980cd82a32eSJiri Olsa if (pmu_format(name, &format)) 981cd82a32eSJiri Olsa return NULL; 982cd82a32eSJiri Olsa 98315b22ed3SAndi Kleen /* 98415b22ed3SAndi Kleen * Check the type first to avoid unnecessary work. 98515b22ed3SAndi Kleen */ 98615b22ed3SAndi Kleen if (pmu_type(name, &type)) 98715b22ed3SAndi Kleen return NULL; 98815b22ed3SAndi Kleen 9893fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 9903fded963SJiri Olsa return NULL; 9913fded963SJiri Olsa 992cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 993cd82a32eSJiri Olsa if (!pmu) 994cd82a32eSJiri Olsa return NULL; 995cd82a32eSJiri Olsa 9967ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 99754e32dc0SGanapatrao Kulkarni pmu->name = strdup(name); 99854e32dc0SGanapatrao Kulkarni pmu->type = type; 99966ec1191SMark Rutland pmu->is_uncore = pmu_is_uncore(name); 100051d54847SJohn Garry if (pmu->is_uncore) 100151d54847SJohn Garry pmu->id = pmu_id(name); 100249afa7f6SJin Yao pmu->is_hybrid = is_hybrid; 100390a86bdeSJiri Olsa pmu->max_precise = pmu_max_precise(name); 100454e32dc0SGanapatrao Kulkarni pmu_add_cpu_aliases(&aliases, pmu); 10054513c719SJohn Garry pmu_add_sys_aliases(&aliases, pmu); 100666ec1191SMark Rutland 1007cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 1008a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 10099fbc61f8SKan Liang INIT_LIST_HEAD(&pmu->caps); 1010cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 1011a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 10129bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 1013dc0a6202SAdrian Hunter 101444462430SJin Yao if (pmu->is_hybrid) 101544462430SJin Yao list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus); 101644462430SJin Yao 1017dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 1018dc0a6202SAdrian Hunter 1019cd82a32eSJiri Olsa return pmu; 1020cd82a32eSJiri Olsa } 1021cd82a32eSJiri Olsa 1022b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 1023cd82a32eSJiri Olsa { 1024cd82a32eSJiri Olsa struct perf_pmu *pmu; 1025cd82a32eSJiri Olsa 1026cd82a32eSJiri Olsa list_for_each_entry(pmu, &pmus, list) 1027cd82a32eSJiri Olsa if (!strcmp(pmu->name, name)) 1028cd82a32eSJiri Olsa return pmu; 1029cd82a32eSJiri Olsa 1030cd82a32eSJiri Olsa return NULL; 1031cd82a32eSJiri Olsa } 1032cd82a32eSJiri Olsa 10333a50dc76SStephane Eranian struct perf_pmu *perf_pmu__find_by_type(unsigned int type) 10343a50dc76SStephane Eranian { 10353a50dc76SStephane Eranian struct perf_pmu *pmu; 10363a50dc76SStephane Eranian 10373a50dc76SStephane Eranian list_for_each_entry(pmu, &pmus, list) 10383a50dc76SStephane Eranian if (pmu->type == type) 10393a50dc76SStephane Eranian return pmu; 10403a50dc76SStephane Eranian 10413a50dc76SStephane Eranian return NULL; 10423a50dc76SStephane Eranian } 10433a50dc76SStephane Eranian 104450a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 104550a9667cSRobert Richter { 104650a9667cSRobert Richter /* 104750a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 104850a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 104950a9667cSRobert Richter */ 105050a9667cSRobert Richter if (!pmu) { 105150a9667cSRobert Richter pmu_read_sysfs(); 105250a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 105350a9667cSRobert Richter } 105450a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 105550a9667cSRobert Richter return pmu; 105650a9667cSRobert Richter return NULL; 105750a9667cSRobert Richter } 105850a9667cSRobert Richter 1059e76026bdSArnaldo Carvalho de Melo struct perf_pmu *evsel__find_pmu(struct evsel *evsel) 1060e12ee9f7SAdrian Hunter { 1061e12ee9f7SAdrian Hunter struct perf_pmu *pmu = NULL; 1062e12ee9f7SAdrian Hunter 1063e12ee9f7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1064e12ee9f7SAdrian Hunter if (pmu->type == evsel->core.attr.type) 1065e12ee9f7SAdrian Hunter break; 1066e12ee9f7SAdrian Hunter } 1067e12ee9f7SAdrian Hunter 1068e12ee9f7SAdrian Hunter return pmu; 1069e12ee9f7SAdrian Hunter } 1070e12ee9f7SAdrian Hunter 107139453ed5SArnaldo Carvalho de Melo bool evsel__is_aux_event(struct evsel *evsel) 1072e12ee9f7SAdrian Hunter { 1073e76026bdSArnaldo Carvalho de Melo struct perf_pmu *pmu = evsel__find_pmu(evsel); 1074e12ee9f7SAdrian Hunter 1075e12ee9f7SAdrian Hunter return pmu && pmu->auxtrace; 1076e12ee9f7SAdrian Hunter } 1077e12ee9f7SAdrian Hunter 1078b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 1079cd82a32eSJiri Olsa { 1080cd82a32eSJiri Olsa struct perf_pmu *pmu; 1081cd82a32eSJiri Olsa 1082cd82a32eSJiri Olsa /* 1083cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 1084cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 1085cd82a32eSJiri Olsa * the pmu format definitions. 1086cd82a32eSJiri Olsa */ 1087cd82a32eSJiri Olsa pmu = pmu_find(name); 1088cd82a32eSJiri Olsa if (pmu) 1089cd82a32eSJiri Olsa return pmu; 1090cd82a32eSJiri Olsa 1091cd82a32eSJiri Olsa return pmu_lookup(name); 1092cd82a32eSJiri Olsa } 1093cd82a32eSJiri Olsa 10945c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 109509ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 1096cd82a32eSJiri Olsa { 10975c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1098cd82a32eSJiri Olsa 1099cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 1100cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 1101cd82a32eSJiri Olsa return format; 1102cd82a32eSJiri Olsa 1103cd82a32eSJiri Olsa return NULL; 1104cd82a32eSJiri Olsa } 1105cd82a32eSJiri Olsa 110609ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 110709ff6071SAdrian Hunter { 110809ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 110909ff6071SAdrian Hunter __u64 bits = 0; 111009ff6071SAdrian Hunter int fbit; 111109ff6071SAdrian Hunter 111209ff6071SAdrian Hunter if (!format) 111309ff6071SAdrian Hunter return 0; 111409ff6071SAdrian Hunter 111509ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 111609ff6071SAdrian Hunter bits |= 1ULL << fbit; 111709ff6071SAdrian Hunter 111809ff6071SAdrian Hunter return bits; 111909ff6071SAdrian Hunter } 112009ff6071SAdrian Hunter 1121a1ac7de6SAdrian Hunter int perf_pmu__format_type(struct list_head *formats, const char *name) 1122a1ac7de6SAdrian Hunter { 1123a1ac7de6SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 1124a1ac7de6SAdrian Hunter 1125a1ac7de6SAdrian Hunter if (!format) 1126a1ac7de6SAdrian Hunter return -1; 1127a1ac7de6SAdrian Hunter 1128a1ac7de6SAdrian Hunter return format->value; 1129a1ac7de6SAdrian Hunter } 1130a1ac7de6SAdrian Hunter 1131cd82a32eSJiri Olsa /* 1132dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 11334d39c89fSIngo Molnar * and unformatted value (value parameter). 1134cd82a32eSJiri Olsa */ 1135dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1136dc0a6202SAdrian Hunter bool zero) 1137cd82a32eSJiri Olsa { 1138cd82a32eSJiri Olsa unsigned long fbit, vbit; 1139cd82a32eSJiri Olsa 1140cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1141cd82a32eSJiri Olsa 1142cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 1143cd82a32eSJiri Olsa continue; 1144cd82a32eSJiri Olsa 1145dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 1146dc0a6202SAdrian Hunter *v |= (1llu << fbit); 1147dc0a6202SAdrian Hunter else if (zero) 1148dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 1149cd82a32eSJiri Olsa } 1150cd82a32eSJiri Olsa } 1151cd82a32eSJiri Olsa 11520efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 11530efe6b67SAdrian Hunter { 11541b9caa10SJiri Olsa int w; 11550efe6b67SAdrian Hunter 11561b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 11571b9caa10SJiri Olsa if (!w) 11581b9caa10SJiri Olsa return 0; 11591b9caa10SJiri Olsa if (w < 64) 11601b9caa10SJiri Olsa return (1ULL << w) - 1; 11611b9caa10SJiri Olsa return -1; 11620efe6b67SAdrian Hunter } 11630efe6b67SAdrian Hunter 1164cd82a32eSJiri Olsa /* 1165688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 1166688d4dfcSCody P Schafer * in the remaining terms. 1167688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 1168688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 1169688d4dfcSCody P Schafer * in a config string) later on in the term list. 1170688d4dfcSCody P Schafer */ 1171688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 1172688d4dfcSCody P Schafer struct list_head *head_terms, 1173688d4dfcSCody P Schafer __u64 *value) 1174688d4dfcSCody P Schafer { 1175688d4dfcSCody P Schafer struct parse_events_term *t; 1176688d4dfcSCody P Schafer 1177688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 11782a3d252dSIan Rogers if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 11792a3d252dSIan Rogers t->config && !strcmp(t->config, term->config)) { 1180688d4dfcSCody P Schafer t->used = true; 1181688d4dfcSCody P Schafer *value = t->val.num; 1182688d4dfcSCody P Schafer return 0; 1183688d4dfcSCody P Schafer } 1184688d4dfcSCody P Schafer } 1185688d4dfcSCody P Schafer 1186bb963e16SNamhyung Kim if (verbose > 0) 1187688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 1188688d4dfcSCody P Schafer 1189688d4dfcSCody P Schafer return -1; 1190688d4dfcSCody P Schafer } 1191688d4dfcSCody P Schafer 1192ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 1193e64b020bSJiri Olsa { 1194e64b020bSJiri Olsa struct perf_pmu_format *format; 119511db4e29SMasami Hiramatsu char *str = NULL; 119611db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1197e64b020bSJiri Olsa unsigned i = 0; 1198e64b020bSJiri Olsa 1199ffeb883eSHe Kuang if (!formats) 1200e64b020bSJiri Olsa return NULL; 1201e64b020bSJiri Olsa 1202e64b020bSJiri Olsa /* sysfs exported terms */ 1203ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 120411db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 120511db4e29SMasami Hiramatsu goto error; 1206e64b020bSJiri Olsa 1207ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 120811db4e29SMasami Hiramatsu error: 1209ffeb883eSHe Kuang strbuf_release(&buf); 1210e64b020bSJiri Olsa 1211e64b020bSJiri Olsa return str; 1212e64b020bSJiri Olsa } 1213e64b020bSJiri Olsa 1214688d4dfcSCody P Schafer /* 1215cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 121688aca8d9SCody P Schafer * user input data - term parameter. 1217cd82a32eSJiri Olsa */ 12184ac22b48SIan Rogers static int pmu_config_term(const char *pmu_name, 12194ac22b48SIan Rogers struct list_head *formats, 1220cd82a32eSJiri Olsa struct perf_event_attr *attr, 1221dc0a6202SAdrian Hunter struct parse_events_term *term, 1222688d4dfcSCody P Schafer struct list_head *head_terms, 1223e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1224cd82a32eSJiri Olsa { 12255c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1226cd82a32eSJiri Olsa __u64 *vp; 12270efe6b67SAdrian Hunter __u64 val, max_val; 1228cd82a32eSJiri Olsa 1229cd82a32eSJiri Olsa /* 1230688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1231688d4dfcSCody P Schafer * skip it in normal eval. 1232688d4dfcSCody P Schafer */ 1233688d4dfcSCody P Schafer if (term->used) 1234688d4dfcSCody P Schafer return 0; 1235688d4dfcSCody P Schafer 1236688d4dfcSCody P Schafer /* 1237cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1238cd82a32eSJiri Olsa * to be done for them. 1239cd82a32eSJiri Olsa */ 1240cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1241cd82a32eSJiri Olsa return 0; 1242cd82a32eSJiri Olsa 1243cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 1244688d4dfcSCody P Schafer if (!format) { 1245ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 12464ac22b48SIan Rogers char *unknown_term; 12474ac22b48SIan Rogers char *help_msg; 1248ffeb883eSHe Kuang 12494ac22b48SIan Rogers if (asprintf(&unknown_term, 12504ac22b48SIan Rogers "unknown term '%s' for pmu '%s'", 12514ac22b48SIan Rogers term->config, pmu_name) < 0) 12524ac22b48SIan Rogers unknown_term = NULL; 12534ac22b48SIan Rogers help_msg = parse_events_formats_error_string(pmu_term); 12544ac22b48SIan Rogers if (err) { 1255448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 12564ac22b48SIan Rogers unknown_term, 12574ac22b48SIan Rogers help_msg); 12584ac22b48SIan Rogers } else { 12594ac22b48SIan Rogers pr_debug("%s (%s)\n", unknown_term, help_msg); 12604ac22b48SIan Rogers free(unknown_term); 1261e64b020bSJiri Olsa } 12624ac22b48SIan Rogers free(pmu_term); 1263cd82a32eSJiri Olsa return -EINVAL; 1264688d4dfcSCody P Schafer } 1265cd82a32eSJiri Olsa 1266cd82a32eSJiri Olsa switch (format->value) { 1267cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1268cd82a32eSJiri Olsa vp = &attr->config; 1269cd82a32eSJiri Olsa break; 1270cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1271cd82a32eSJiri Olsa vp = &attr->config1; 1272cd82a32eSJiri Olsa break; 1273cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1274cd82a32eSJiri Olsa vp = &attr->config2; 1275cd82a32eSJiri Olsa break; 1276cd82a32eSJiri Olsa default: 1277cd82a32eSJiri Olsa return -EINVAL; 1278cd82a32eSJiri Olsa } 1279cd82a32eSJiri Olsa 128016fa7e82SJiri Olsa /* 1281688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1282688d4dfcSCody P Schafer * using event parameters. 128316fa7e82SJiri Olsa */ 128499e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 128599e7138eSJiri Olsa if (term->no_value && 128699e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 128799e7138eSJiri Olsa if (err) { 1288448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1289448d732cSIan Rogers strdup("no value assigned for term"), 1290448d732cSIan Rogers NULL); 129199e7138eSJiri Olsa } 129299e7138eSJiri Olsa return -EINVAL; 129399e7138eSJiri Olsa } 129499e7138eSJiri Olsa 1295688d4dfcSCody P Schafer val = term->val.num; 129699e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1297688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1298bb963e16SNamhyung Kim if (verbose > 0) { 1299688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1300688d4dfcSCody P Schafer term->config, term->val.str); 1301e64b020bSJiri Olsa } 1302e64b020bSJiri Olsa if (err) { 1303448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1304448d732cSIan Rogers strdup("expected numeric value"), 1305448d732cSIan Rogers NULL); 1306e64b020bSJiri Olsa } 1307688d4dfcSCody P Schafer return -EINVAL; 1308688d4dfcSCody P Schafer } 1309688d4dfcSCody P Schafer 1310688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1311688d4dfcSCody P Schafer return -EINVAL; 1312688d4dfcSCody P Schafer } else 1313688d4dfcSCody P Schafer return -EINVAL; 1314688d4dfcSCody P Schafer 13150efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 13160efe6b67SAdrian Hunter if (val > max_val) { 13170efe6b67SAdrian Hunter if (err) { 1318448d732cSIan Rogers char *err_str; 1319448d732cSIan Rogers 1320448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1321448d732cSIan Rogers asprintf(&err_str, 13220efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1323448d732cSIan Rogers (unsigned long long)max_val) < 0 1324448d732cSIan Rogers ? strdup("value too big for format") 1325448d732cSIan Rogers : err_str, 1326448d732cSIan Rogers NULL); 13270efe6b67SAdrian Hunter return -EINVAL; 13280efe6b67SAdrian Hunter } 13290efe6b67SAdrian Hunter /* 13300efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 13310efe6b67SAdrian Hunter * silently truncated. 13320efe6b67SAdrian Hunter */ 13330efe6b67SAdrian Hunter } 13340efe6b67SAdrian Hunter 1335688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1336cd82a32eSJiri Olsa return 0; 1337cd82a32eSJiri Olsa } 1338cd82a32eSJiri Olsa 13394ac22b48SIan Rogers int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, 1340cff7f956SJiri Olsa struct perf_event_attr *attr, 1341dc0a6202SAdrian Hunter struct list_head *head_terms, 1342e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1343cd82a32eSJiri Olsa { 13446cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1345cd82a32eSJiri Olsa 1346688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 13474ac22b48SIan Rogers if (pmu_config_term(pmu_name, formats, attr, term, head_terms, 1348e64b020bSJiri Olsa zero, err)) 1349cd82a32eSJiri Olsa return -EINVAL; 1350688d4dfcSCody P Schafer } 1351cd82a32eSJiri Olsa 1352cd82a32eSJiri Olsa return 0; 1353cd82a32eSJiri Olsa } 1354cd82a32eSJiri Olsa 1355cd82a32eSJiri Olsa /* 1356cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1357cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1358cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1359cd82a32eSJiri Olsa */ 1360cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1361e64b020bSJiri Olsa struct list_head *head_terms, 1362e64b020bSJiri Olsa struct parse_events_error *err) 1363cd82a32eSJiri Olsa { 1364dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1365dc0a6202SAdrian Hunter 1366cd82a32eSJiri Olsa attr->type = pmu->type; 13674ac22b48SIan Rogers return perf_pmu__config_terms(pmu->name, &pmu->format, attr, 13684ac22b48SIan Rogers head_terms, zero, err); 1369cd82a32eSJiri Olsa } 1370cd82a32eSJiri Olsa 13715c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 13726cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1373a6146d50SZheng Yan { 13745c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1375a6146d50SZheng Yan char *name; 1376a6146d50SZheng Yan 1377a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1378a6146d50SZheng Yan return NULL; 1379a6146d50SZheng Yan 1380a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1381a6146d50SZheng Yan if (term->val.num != 1) 1382a6146d50SZheng Yan return NULL; 1383a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1384a6146d50SZheng Yan return NULL; 1385a6146d50SZheng Yan name = term->config; 1386a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1387a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1388a6146d50SZheng Yan return NULL; 1389a6146d50SZheng Yan name = term->val.str; 1390a6146d50SZheng Yan } else { 1391a6146d50SZheng Yan return NULL; 1392a6146d50SZheng Yan } 1393a6146d50SZheng Yan 1394a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 1395a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 1396a6146d50SZheng Yan return alias; 1397a6146d50SZheng Yan } 1398a6146d50SZheng Yan return NULL; 1399a6146d50SZheng Yan } 1400a6146d50SZheng Yan 1401410136f5SStephane Eranian 14021d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 14031d9e446bSJiri Olsa struct perf_pmu_info *info) 1404410136f5SStephane Eranian { 1405410136f5SStephane Eranian /* 1406410136f5SStephane Eranian * Only one term in event definition can 14071d9e446bSJiri Olsa * define unit, scale and snapshot, fail 14081d9e446bSJiri Olsa * if there's more than one. 1409410136f5SStephane Eranian */ 1410b30a7d1fSArnaldo Carvalho de Melo if ((info->unit && alias->unit[0]) || 14111d9e446bSJiri Olsa (info->scale && alias->scale) || 14121d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 1413410136f5SStephane Eranian return -EINVAL; 1414410136f5SStephane Eranian 1415b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 14161d9e446bSJiri Olsa info->unit = alias->unit; 1417410136f5SStephane Eranian 1418410136f5SStephane Eranian if (alias->scale) 14191d9e446bSJiri Olsa info->scale = alias->scale; 14201d9e446bSJiri Olsa 14211d9e446bSJiri Olsa if (alias->snapshot) 14221d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1423410136f5SStephane Eranian 1424410136f5SStephane Eranian return 0; 1425410136f5SStephane Eranian } 1426410136f5SStephane Eranian 1427a6146d50SZheng Yan /* 1428a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1429a6146d50SZheng Yan * defined for the alias 1430a6146d50SZheng Yan */ 1431410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 143246441bdcSMatt Fleming struct perf_pmu_info *info) 1433a6146d50SZheng Yan { 14346cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 14355c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1436a6146d50SZheng Yan int ret; 1437a6146d50SZheng Yan 1438044330c1SMatt Fleming info->per_pkg = false; 1439044330c1SMatt Fleming 14408a398897SStephane Eranian /* 14418a398897SStephane Eranian * Mark unit and scale as not set 14428a398897SStephane Eranian * (different from default values, see below) 14438a398897SStephane Eranian */ 144446441bdcSMatt Fleming info->unit = NULL; 144546441bdcSMatt Fleming info->scale = 0.0; 14461d9e446bSJiri Olsa info->snapshot = false; 144737932c18SAndi Kleen info->metric_expr = NULL; 144896284814SAndi Kleen info->metric_name = NULL; 1449410136f5SStephane Eranian 1450a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1451a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1452a6146d50SZheng Yan if (!alias) 1453a6146d50SZheng Yan continue; 1454a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 1455a6146d50SZheng Yan if (ret) 1456a6146d50SZheng Yan return ret; 1457410136f5SStephane Eranian 14581d9e446bSJiri Olsa ret = check_info_data(alias, info); 1459410136f5SStephane Eranian if (ret) 1460410136f5SStephane Eranian return ret; 1461410136f5SStephane Eranian 1462044330c1SMatt Fleming if (alias->per_pkg) 1463044330c1SMatt Fleming info->per_pkg = true; 146437932c18SAndi Kleen info->metric_expr = alias->metric_expr; 146596284814SAndi Kleen info->metric_name = alias->metric_name; 1466044330c1SMatt Fleming 1467e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 14681dc92556SIan Rogers parse_events_term__delete(term); 1469a6146d50SZheng Yan } 14708a398897SStephane Eranian 14718a398897SStephane Eranian /* 14728a398897SStephane Eranian * if no unit or scale found in aliases, then 14738a398897SStephane Eranian * set defaults as for evsel 14748a398897SStephane Eranian * unit cannot left to NULL 14758a398897SStephane Eranian */ 147646441bdcSMatt Fleming if (info->unit == NULL) 147746441bdcSMatt Fleming info->unit = ""; 14788a398897SStephane Eranian 147946441bdcSMatt Fleming if (info->scale == 0.0) 148046441bdcSMatt Fleming info->scale = 1.0; 14818a398897SStephane Eranian 1482a6146d50SZheng Yan return 0; 1483a6146d50SZheng Yan } 1484a6146d50SZheng Yan 1485cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 1486cd82a32eSJiri Olsa int config, unsigned long *bits) 1487cd82a32eSJiri Olsa { 14885c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1489cd82a32eSJiri Olsa 1490cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 1491cd82a32eSJiri Olsa if (!format) 1492cd82a32eSJiri Olsa return -ENOMEM; 1493cd82a32eSJiri Olsa 1494cd82a32eSJiri Olsa format->name = strdup(name); 1495cd82a32eSJiri Olsa format->value = config; 1496cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 1497cd82a32eSJiri Olsa 1498cd82a32eSJiri Olsa list_add_tail(&format->list, list); 1499cd82a32eSJiri Olsa return 0; 1500cd82a32eSJiri Olsa } 1501cd82a32eSJiri Olsa 1502cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 1503cd82a32eSJiri Olsa { 1504cd82a32eSJiri Olsa long b; 1505cd82a32eSJiri Olsa 1506cd82a32eSJiri Olsa if (!to) 1507cd82a32eSJiri Olsa to = from; 1508cd82a32eSJiri Olsa 150915268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1510cd82a32eSJiri Olsa for (b = from; b <= to; b++) 1511cd82a32eSJiri Olsa set_bit(b, bits); 1512cd82a32eSJiri Olsa } 1513dc098b35SAndi Kleen 1514d26383dcSNamhyung Kim void perf_pmu__del_formats(struct list_head *formats) 1515d26383dcSNamhyung Kim { 1516d26383dcSNamhyung Kim struct perf_pmu_format *fmt, *tmp; 1517d26383dcSNamhyung Kim 1518d26383dcSNamhyung Kim list_for_each_entry_safe(fmt, tmp, formats, list) { 1519d26383dcSNamhyung Kim list_del(&fmt->list); 1520d26383dcSNamhyung Kim free(fmt->name); 1521d26383dcSNamhyung Kim free(fmt); 1522d26383dcSNamhyung Kim } 1523d26383dcSNamhyung Kim } 1524d26383dcSNamhyung Kim 1525aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 1526aaea3617SCody P Schafer { 1527aaea3617SCody P Schafer if (b > a) 1528aaea3617SCody P Schafer return 0; 1529aaea3617SCody P Schafer return a - b; 1530aaea3617SCody P Schafer } 1531aaea3617SCody P Schafer 1532dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 1533dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1534dc098b35SAndi Kleen { 1535aaea3617SCody P Schafer struct parse_events_term *term; 1536aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1537aaea3617SCody P Schafer 1538aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 1539aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1540aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 1541aaea3617SCody P Schafer ",%s=%s", term->config, 1542aaea3617SCody P Schafer term->val.str); 1543aaea3617SCody P Schafer } 1544aaea3617SCody P Schafer 1545aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1546aaea3617SCody P Schafer buf[used] = '/'; 1547aaea3617SCody P Schafer used++; 1548aaea3617SCody P Schafer } 1549aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1550aaea3617SCody P Schafer buf[used] = '\0'; 1551aaea3617SCody P Schafer used++; 1552aaea3617SCody P Schafer } else 1553aaea3617SCody P Schafer buf[len - 1] = '\0'; 1554aaea3617SCody P Schafer 1555dc098b35SAndi Kleen return buf; 1556dc098b35SAndi Kleen } 1557dc098b35SAndi Kleen 1558dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 1559dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1560dc098b35SAndi Kleen { 1561dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 1562dc098b35SAndi Kleen return buf; 1563dc098b35SAndi Kleen } 1564dc098b35SAndi Kleen 1565dd5f1036SAndi Kleen struct sevent { 156608e60ed1SAndi Kleen char *name; 156708e60ed1SAndi Kleen char *desc; 1568dd5f1036SAndi Kleen char *topic; 1569f2361024SAndi Kleen char *str; 1570f2361024SAndi Kleen char *pmu; 15717f372a63SAndi Kleen char *metric_expr; 157296284814SAndi Kleen char *metric_name; 1573ce0dc7d2SJohn Garry int is_cpu; 157408e60ed1SAndi Kleen }; 157508e60ed1SAndi Kleen 1576dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b) 1577dc098b35SAndi Kleen { 1578dd5f1036SAndi Kleen const struct sevent *as = a; 1579dd5f1036SAndi Kleen const struct sevent *bs = b; 158008e60ed1SAndi Kleen 158108e60ed1SAndi Kleen /* Put extra events last */ 158208e60ed1SAndi Kleen if (!!as->desc != !!bs->desc) 158308e60ed1SAndi Kleen return !!as->desc - !!bs->desc; 1584dd5f1036SAndi Kleen if (as->topic && bs->topic) { 1585dd5f1036SAndi Kleen int n = strcmp(as->topic, bs->topic); 1586dd5f1036SAndi Kleen 1587dd5f1036SAndi Kleen if (n) 1588dd5f1036SAndi Kleen return n; 1589dd5f1036SAndi Kleen } 1590ce0dc7d2SJohn Garry 1591ce0dc7d2SJohn Garry /* Order CPU core events to be first */ 1592ce0dc7d2SJohn Garry if (as->is_cpu != bs->is_cpu) 1593ce0dc7d2SJohn Garry return bs->is_cpu - as->is_cpu; 1594ce0dc7d2SJohn Garry 159508e60ed1SAndi Kleen return strcmp(as->name, bs->name); 159608e60ed1SAndi Kleen } 159708e60ed1SAndi Kleen 159808e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr) 159908e60ed1SAndi Kleen { 160008e60ed1SAndi Kleen int column = start; 160108e60ed1SAndi Kleen int n; 160208e60ed1SAndi Kleen 160308e60ed1SAndi Kleen while (*s) { 160408e60ed1SAndi Kleen int wlen = strcspn(s, " \t"); 160508e60ed1SAndi Kleen 160608e60ed1SAndi Kleen if (column + wlen >= max && column > start) { 160708e60ed1SAndi Kleen printf("\n%*s", start, ""); 160808e60ed1SAndi Kleen column = start + corr; 160908e60ed1SAndi Kleen } 161008e60ed1SAndi Kleen n = printf("%s%.*s", column > start ? " " : "", wlen, s); 161108e60ed1SAndi Kleen if (n <= 0) 161208e60ed1SAndi Kleen break; 161308e60ed1SAndi Kleen s += wlen; 161408e60ed1SAndi Kleen column += n; 161532858480SArnaldo Carvalho de Melo s = skip_spaces(s); 161608e60ed1SAndi Kleen } 1617dc098b35SAndi Kleen } 1618dc098b35SAndi Kleen 1619d504fae9SJohn Garry bool is_pmu_core(const char *name) 1620d504fae9SJohn Garry { 1621d504fae9SJohn Garry return !strcmp(name, "cpu") || is_arm_pmu_core(name); 1622d504fae9SJohn Garry } 1623d504fae9SJohn Garry 1624c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, 1625a7f6c8c8SJin Yao bool long_desc, bool details_flag, bool deprecated) 1626dc098b35SAndi Kleen { 1627dc098b35SAndi Kleen struct perf_pmu *pmu; 1628dc098b35SAndi Kleen struct perf_pmu_alias *alias; 1629dc098b35SAndi Kleen char buf[1024]; 1630dc098b35SAndi Kleen int printed = 0; 1631dc098b35SAndi Kleen int len, j; 1632dd5f1036SAndi Kleen struct sevent *aliases; 163308e60ed1SAndi Kleen int numdesc = 0; 163461eb2eb4SAndi Kleen int columns = pager_get_columns(); 1635dd5f1036SAndi Kleen char *topic = NULL; 1636dc098b35SAndi Kleen 1637dc098b35SAndi Kleen pmu = NULL; 1638dc098b35SAndi Kleen len = 0; 163942634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1640dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 1641dc098b35SAndi Kleen len++; 164242634bc7SAdrian Hunter if (pmu->selectable) 164342634bc7SAdrian Hunter len++; 164442634bc7SAdrian Hunter } 1645dd5f1036SAndi Kleen aliases = zalloc(sizeof(struct sevent) * len); 1646dc098b35SAndi Kleen if (!aliases) 16477e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1648dc098b35SAndi Kleen pmu = NULL; 1649dc098b35SAndi Kleen j = 0; 165042634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1651dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 165208e60ed1SAndi Kleen char *name = alias->desc ? alias->name : 165308e60ed1SAndi Kleen format_alias(buf, sizeof(buf), pmu, alias); 1654c1b4745bSJohn Garry bool is_cpu = is_pmu_core(pmu->name); 1655dc098b35SAndi Kleen 1656a7f6c8c8SJin Yao if (alias->deprecated && !deprecated) 1657a7f6c8c8SJin Yao continue; 1658a7f6c8c8SJin Yao 1659dc098b35SAndi Kleen if (event_glob != NULL && 166038d14f0cSAndi Kleen !(strglobmatch_nocase(name, event_glob) || 166138d14f0cSAndi Kleen (!is_cpu && strglobmatch_nocase(alias->name, 166267bdc35fSAndi Kleen event_glob)) || 166367bdc35fSAndi Kleen (alias->topic && 166467bdc35fSAndi Kleen strglobmatch_nocase(alias->topic, event_glob)))) 1665dc098b35SAndi Kleen continue; 16667e4772dcSArnaldo Carvalho de Melo 166708e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 16687e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 16697e4772dcSArnaldo Carvalho de Melo 167008e60ed1SAndi Kleen aliases[j].name = name; 167108e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 167208e60ed1SAndi Kleen aliases[j].name = format_alias_or(buf, 167308e60ed1SAndi Kleen sizeof(buf), 167408e60ed1SAndi Kleen pmu, alias); 167508e60ed1SAndi Kleen aliases[j].name = strdup(aliases[j].name); 167608e60ed1SAndi Kleen if (!aliases[j].name) 16777e4772dcSArnaldo Carvalho de Melo goto out_enomem; 167808e60ed1SAndi Kleen 1679c8d6828aSSukadev Bhattiprolu aliases[j].desc = long_desc ? alias->long_desc : 1680c8d6828aSSukadev Bhattiprolu alias->desc; 1681dd5f1036SAndi Kleen aliases[j].topic = alias->topic; 1682f2361024SAndi Kleen aliases[j].str = alias->str; 1683f2361024SAndi Kleen aliases[j].pmu = pmu->name; 16847f372a63SAndi Kleen aliases[j].metric_expr = alias->metric_expr; 168596284814SAndi Kleen aliases[j].metric_name = alias->metric_name; 1686ce0dc7d2SJohn Garry aliases[j].is_cpu = is_cpu; 1687dc098b35SAndi Kleen j++; 1688dc098b35SAndi Kleen } 1689fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1690fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 16917e4772dcSArnaldo Carvalho de Melo char *s; 16927e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 16937e4772dcSArnaldo Carvalho de Melo goto out_enomem; 169408e60ed1SAndi Kleen aliases[j].name = s; 169542634bc7SAdrian Hunter j++; 169642634bc7SAdrian Hunter } 169742634bc7SAdrian Hunter } 1698dc098b35SAndi Kleen len = j; 1699dd5f1036SAndi Kleen qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1700dc098b35SAndi Kleen for (j = 0; j < len; j++) { 170115b22ed3SAndi Kleen /* Skip duplicates */ 170215b22ed3SAndi Kleen if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) 170315b22ed3SAndi Kleen continue; 1704dc098b35SAndi Kleen if (name_only) { 170508e60ed1SAndi Kleen printf("%s ", aliases[j].name); 1706dc098b35SAndi Kleen continue; 1707dc098b35SAndi Kleen } 17081c5f01feSAndi Kleen if (aliases[j].desc && !quiet_flag) { 170908e60ed1SAndi Kleen if (numdesc++ == 0) 171008e60ed1SAndi Kleen printf("\n"); 1711dd5f1036SAndi Kleen if (aliases[j].topic && (!topic || 1712dd5f1036SAndi Kleen strcmp(topic, aliases[j].topic))) { 1713dd5f1036SAndi Kleen printf("%s%s:\n", topic ? "\n" : "", 1714dd5f1036SAndi Kleen aliases[j].topic); 1715dd5f1036SAndi Kleen topic = aliases[j].topic; 1716dd5f1036SAndi Kleen } 171708e60ed1SAndi Kleen printf(" %-50s\n", aliases[j].name); 171808e60ed1SAndi Kleen printf("%*s", 8, "["); 171908e60ed1SAndi Kleen wordwrap(aliases[j].desc, 8, columns, 0); 172008e60ed1SAndi Kleen printf("]\n"); 1721bf874fcfSAndi Kleen if (details_flag) { 17227f372a63SAndi Kleen printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); 172396284814SAndi Kleen if (aliases[j].metric_name) 172496284814SAndi Kleen printf(" MetricName: %s", aliases[j].metric_name); 17257f372a63SAndi Kleen if (aliases[j].metric_expr) 17267f372a63SAndi Kleen printf(" MetricExpr: %s", aliases[j].metric_expr); 17277f372a63SAndi Kleen putchar('\n'); 17287f372a63SAndi Kleen } 172908e60ed1SAndi Kleen } else 173008e60ed1SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j].name); 1731dc098b35SAndi Kleen printed++; 1732dc098b35SAndi Kleen } 1733dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1734dc098b35SAndi Kleen printf("\n"); 17357e4772dcSArnaldo Carvalho de Melo out_free: 17367e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 173708e60ed1SAndi Kleen zfree(&aliases[j].name); 17387e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 17397e4772dcSArnaldo Carvalho de Melo return; 17407e4772dcSArnaldo Carvalho de Melo 17417e4772dcSArnaldo Carvalho de Melo out_enomem: 17427e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 17437e4772dcSArnaldo Carvalho de Melo if (aliases) 17447e4772dcSArnaldo Carvalho de Melo goto out_free; 1745dc098b35SAndi Kleen } 17464cabc3d1SAndi Kleen 17474cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 17484cabc3d1SAndi Kleen { 17494cabc3d1SAndi Kleen struct perf_pmu *pmu; 17504cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 17514cabc3d1SAndi Kleen 17524cabc3d1SAndi Kleen pmu = NULL; 17534cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 17544cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 17554cabc3d1SAndi Kleen continue; 17564cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 17574cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 17584cabc3d1SAndi Kleen return true; 17594cabc3d1SAndi Kleen } 17604cabc3d1SAndi Kleen return false; 17614cabc3d1SAndi Kleen } 17627d4bdab5SAdrian Hunter 17637d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 17647d4bdab5SAdrian Hunter { 17657d4bdab5SAdrian Hunter char path[PATH_MAX]; 17667d4bdab5SAdrian Hunter const char *sysfs; 17677d4bdab5SAdrian Hunter 17687d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 17697d4bdab5SAdrian Hunter if (!sysfs) 17707d4bdab5SAdrian Hunter return NULL; 17717d4bdab5SAdrian Hunter 17727d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 17737d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 1774d9664582SAndi Kleen if (!file_available(path)) 17757d4bdab5SAdrian Hunter return NULL; 17767d4bdab5SAdrian Hunter return fopen(path, "r"); 17777d4bdab5SAdrian Hunter } 17787d4bdab5SAdrian Hunter 17797d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 17807d4bdab5SAdrian Hunter ...) 17817d4bdab5SAdrian Hunter { 17827d4bdab5SAdrian Hunter va_list args; 17837d4bdab5SAdrian Hunter FILE *file; 17847d4bdab5SAdrian Hunter int ret = EOF; 17857d4bdab5SAdrian Hunter 17867d4bdab5SAdrian Hunter va_start(args, fmt); 17877d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 17887d4bdab5SAdrian Hunter if (file) { 17897d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 17907d4bdab5SAdrian Hunter fclose(file); 17917d4bdab5SAdrian Hunter } 17927d4bdab5SAdrian Hunter va_end(args); 17937d4bdab5SAdrian Hunter return ret; 17947d4bdab5SAdrian Hunter } 17959fbc61f8SKan Liang 17969fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 17979fbc61f8SKan Liang { 17989fbc61f8SKan Liang struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 17999fbc61f8SKan Liang 18009fbc61f8SKan Liang if (!caps) 18019fbc61f8SKan Liang return -ENOMEM; 18029fbc61f8SKan Liang 18039fbc61f8SKan Liang caps->name = strdup(name); 18049fbc61f8SKan Liang if (!caps->name) 18059fbc61f8SKan Liang goto free_caps; 18069fbc61f8SKan Liang caps->value = strndup(value, strlen(value) - 1); 18079fbc61f8SKan Liang if (!caps->value) 18089fbc61f8SKan Liang goto free_name; 18099fbc61f8SKan Liang list_add_tail(&caps->list, list); 18109fbc61f8SKan Liang return 0; 18119fbc61f8SKan Liang 18129fbc61f8SKan Liang free_name: 18139fbc61f8SKan Liang zfree(caps->name); 18149fbc61f8SKan Liang free_caps: 18159fbc61f8SKan Liang free(caps); 18169fbc61f8SKan Liang 18179fbc61f8SKan Liang return -ENOMEM; 18189fbc61f8SKan Liang } 18199fbc61f8SKan Liang 18209fbc61f8SKan Liang /* 18219fbc61f8SKan Liang * Reading/parsing the given pmu capabilities, which should be located at: 18229fbc61f8SKan Liang * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 18239fbc61f8SKan Liang * Return the number of capabilities 18249fbc61f8SKan Liang */ 18259fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu) 18269fbc61f8SKan Liang { 18279fbc61f8SKan Liang struct stat st; 18289fbc61f8SKan Liang char caps_path[PATH_MAX]; 18299fbc61f8SKan Liang const char *sysfs = sysfs__mountpoint(); 18309fbc61f8SKan Liang DIR *caps_dir; 18319fbc61f8SKan Liang struct dirent *evt_ent; 18329fbc61f8SKan Liang int nr_caps = 0; 18339fbc61f8SKan Liang 18349fbc61f8SKan Liang if (!sysfs) 18359fbc61f8SKan Liang return -1; 18369fbc61f8SKan Liang 18379fbc61f8SKan Liang snprintf(caps_path, PATH_MAX, 18389fbc61f8SKan Liang "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name); 18399fbc61f8SKan Liang 18409fbc61f8SKan Liang if (stat(caps_path, &st) < 0) 18419fbc61f8SKan Liang return 0; /* no error if caps does not exist */ 18429fbc61f8SKan Liang 18439fbc61f8SKan Liang caps_dir = opendir(caps_path); 18449fbc61f8SKan Liang if (!caps_dir) 18459fbc61f8SKan Liang return -EINVAL; 18469fbc61f8SKan Liang 18479fbc61f8SKan Liang while ((evt_ent = readdir(caps_dir)) != NULL) { 18489fbc61f8SKan Liang char path[PATH_MAX + NAME_MAX + 1]; 18499fbc61f8SKan Liang char *name = evt_ent->d_name; 18509fbc61f8SKan Liang char value[128]; 18519fbc61f8SKan Liang FILE *file; 18529fbc61f8SKan Liang 18539fbc61f8SKan Liang if (!strcmp(name, ".") || !strcmp(name, "..")) 18549fbc61f8SKan Liang continue; 18559fbc61f8SKan Liang 18569fbc61f8SKan Liang snprintf(path, sizeof(path), "%s/%s", caps_path, name); 18579fbc61f8SKan Liang 18589fbc61f8SKan Liang file = fopen(path, "r"); 18599fbc61f8SKan Liang if (!file) 18609fbc61f8SKan Liang continue; 18619fbc61f8SKan Liang 18629fbc61f8SKan Liang if (!fgets(value, sizeof(value), file) || 18639fbc61f8SKan Liang (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 18649fbc61f8SKan Liang fclose(file); 18659fbc61f8SKan Liang continue; 18669fbc61f8SKan Liang } 18679fbc61f8SKan Liang 18689fbc61f8SKan Liang nr_caps++; 18699fbc61f8SKan Liang fclose(file); 18709fbc61f8SKan Liang } 18719fbc61f8SKan Liang 18729fbc61f8SKan Liang closedir(caps_dir); 18739fbc61f8SKan Liang 18749fbc61f8SKan Liang return nr_caps; 18759fbc61f8SKan Liang } 1876e4064776SJin Yao 1877e4064776SJin Yao void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 1878e4064776SJin Yao char *name) 1879e4064776SJin Yao { 1880e4064776SJin Yao struct perf_pmu_format *format; 1881e4064776SJin Yao __u64 masks = 0, bits; 1882e4064776SJin Yao char buf[100]; 1883e4064776SJin Yao unsigned int i; 1884e4064776SJin Yao 1885e4064776SJin Yao list_for_each_entry(format, &pmu->format, list) { 1886e4064776SJin Yao if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG) 1887e4064776SJin Yao continue; 1888e4064776SJin Yao 1889e4064776SJin Yao for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1890e4064776SJin Yao masks |= 1ULL << i; 1891e4064776SJin Yao } 1892e4064776SJin Yao 1893e4064776SJin Yao /* 1894e4064776SJin Yao * Kernel doesn't export any valid format bits. 1895e4064776SJin Yao */ 1896e4064776SJin Yao if (masks == 0) 1897e4064776SJin Yao return; 1898e4064776SJin Yao 1899e4064776SJin Yao bits = config & ~masks; 1900e4064776SJin Yao if (bits == 0) 1901e4064776SJin Yao return; 1902e4064776SJin Yao 1903e4064776SJin Yao bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1904e4064776SJin Yao 1905e4064776SJin Yao pr_warning("WARNING: event '%s' not valid (bits %s of config " 1906e4064776SJin Yao "'%llx' not supported by kernel)!\n", 1907e4064776SJin Yao name ?: "N/A", buf, config); 1908e4064776SJin Yao } 1909c5a26ea4SJin Yao 1910c5a26ea4SJin Yao bool perf_pmu__has_hybrid(void) 1911c5a26ea4SJin Yao { 1912c5a26ea4SJin Yao if (!hybrid_scanned) { 1913c5a26ea4SJin Yao hybrid_scanned = true; 1914c5a26ea4SJin Yao perf_pmu__scan(NULL); 1915c5a26ea4SJin Yao } 1916c5a26ea4SJin Yao 1917c5a26ea4SJin Yao return !list_empty(&perf_pmu__hybrid_pmus); 1918c5a26ea4SJin Yao } 1919c47a5599SJin Yao 1920c47a5599SJin Yao int perf_pmu__match(char *pattern, char *name, char *tok) 1921c47a5599SJin Yao { 1922c47a5599SJin Yao if (fnmatch(pattern, name, 0)) 1923c47a5599SJin Yao return -1; 1924c47a5599SJin Yao 1925c47a5599SJin Yao if (tok && !perf_pmu__valid_suffix(name, tok)) 1926c47a5599SJin Yao return -1; 1927c47a5599SJin Yao 1928c47a5599SJin Yao return 0; 1929c47a5599SJin Yao } 1930*1d3351e6SJin Yao 1931*1d3351e6SJin Yao int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus, 1932*1d3351e6SJin Yao struct perf_cpu_map **mcpus_ptr, 1933*1d3351e6SJin Yao struct perf_cpu_map **ucpus_ptr) 1934*1d3351e6SJin Yao { 1935*1d3351e6SJin Yao struct perf_cpu_map *pmu_cpus = pmu->cpus; 1936*1d3351e6SJin Yao struct perf_cpu_map *matched_cpus, *unmatched_cpus; 1937*1d3351e6SJin Yao int matched_nr = 0, unmatched_nr = 0; 1938*1d3351e6SJin Yao 1939*1d3351e6SJin Yao matched_cpus = perf_cpu_map__default_new(); 1940*1d3351e6SJin Yao if (!matched_cpus) 1941*1d3351e6SJin Yao return -1; 1942*1d3351e6SJin Yao 1943*1d3351e6SJin Yao unmatched_cpus = perf_cpu_map__default_new(); 1944*1d3351e6SJin Yao if (!unmatched_cpus) { 1945*1d3351e6SJin Yao perf_cpu_map__put(matched_cpus); 1946*1d3351e6SJin Yao return -1; 1947*1d3351e6SJin Yao } 1948*1d3351e6SJin Yao 1949*1d3351e6SJin Yao for (int i = 0; i < cpus->nr; i++) { 1950*1d3351e6SJin Yao int cpu; 1951*1d3351e6SJin Yao 1952*1d3351e6SJin Yao cpu = perf_cpu_map__idx(pmu_cpus, cpus->map[i]); 1953*1d3351e6SJin Yao if (cpu == -1) 1954*1d3351e6SJin Yao unmatched_cpus->map[unmatched_nr++] = cpus->map[i]; 1955*1d3351e6SJin Yao else 1956*1d3351e6SJin Yao matched_cpus->map[matched_nr++] = cpus->map[i]; 1957*1d3351e6SJin Yao } 1958*1d3351e6SJin Yao 1959*1d3351e6SJin Yao unmatched_cpus->nr = unmatched_nr; 1960*1d3351e6SJin Yao matched_cpus->nr = matched_nr; 1961*1d3351e6SJin Yao *mcpus_ptr = matched_cpus; 1962*1d3351e6SJin Yao *ucpus_ptr = unmatched_cpus; 1963*1d3351e6SJin Yao return 0; 1964*1d3351e6SJin Yao } 1965