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 713*0ec43c08SIan Rogers const struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu) 714d77ade9fSAndi Kleen { 715*0ec43c08SIan Rogers const 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 740*0ec43c08SIan Rogers const 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, 827*0ec43c08SIan Rogers const 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 { 862*0ec43c08SIan Rogers const 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 94813d60ba0SKan Liang char * __weak 94913d60ba0SKan Liang pmu_find_real_name(const char *name) 95013d60ba0SKan Liang { 95113d60ba0SKan Liang return (char *)name; 95213d60ba0SKan Liang } 95313d60ba0SKan Liang 95413d60ba0SKan Liang char * __weak 95513d60ba0SKan Liang pmu_find_alias_name(const char *name __maybe_unused) 95613d60ba0SKan Liang { 95713d60ba0SKan Liang return NULL; 95813d60ba0SKan Liang } 95913d60ba0SKan Liang 96090a86bdeSJiri Olsa static int pmu_max_precise(const char *name) 96190a86bdeSJiri Olsa { 96290a86bdeSJiri Olsa char path[PATH_MAX]; 96390a86bdeSJiri Olsa int max_precise = -1; 96490a86bdeSJiri Olsa 96590a86bdeSJiri Olsa scnprintf(path, PATH_MAX, 96690a86bdeSJiri Olsa "bus/event_source/devices/%s/caps/max_precise", 96790a86bdeSJiri Olsa name); 96890a86bdeSJiri Olsa 96990a86bdeSJiri Olsa sysfs__read_int(path, &max_precise); 97090a86bdeSJiri Olsa return max_precise; 97190a86bdeSJiri Olsa } 97290a86bdeSJiri Olsa 97313d60ba0SKan Liang static struct perf_pmu *pmu_lookup(const char *lookup_name) 974cd82a32eSJiri Olsa { 975cd82a32eSJiri Olsa struct perf_pmu *pmu; 976cd82a32eSJiri Olsa LIST_HEAD(format); 977a6146d50SZheng Yan LIST_HEAD(aliases); 978cd82a32eSJiri Olsa __u32 type; 97913d60ba0SKan Liang char *name = pmu_find_real_name(lookup_name); 98049afa7f6SJin Yao bool is_hybrid = perf_pmu__hybrid_mounted(name); 98113d60ba0SKan Liang char *alias_name; 98249afa7f6SJin Yao 98349afa7f6SJin Yao /* 98449afa7f6SJin Yao * Check pmu name for hybrid and the pmu may be invalid in sysfs 98549afa7f6SJin Yao */ 98649afa7f6SJin Yao if (!strncmp(name, "cpu_", 4) && !is_hybrid) 98749afa7f6SJin Yao return NULL; 988cd82a32eSJiri Olsa 989cd82a32eSJiri Olsa /* 990cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 991cd82a32eSJiri Olsa * type value and format definitions. Load both right 992cd82a32eSJiri Olsa * now. 993cd82a32eSJiri Olsa */ 994cd82a32eSJiri Olsa if (pmu_format(name, &format)) 995cd82a32eSJiri Olsa return NULL; 996cd82a32eSJiri Olsa 99715b22ed3SAndi Kleen /* 99815b22ed3SAndi Kleen * Check the type first to avoid unnecessary work. 99915b22ed3SAndi Kleen */ 100015b22ed3SAndi Kleen if (pmu_type(name, &type)) 100115b22ed3SAndi Kleen return NULL; 100215b22ed3SAndi Kleen 10033fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 10043fded963SJiri Olsa return NULL; 10053fded963SJiri Olsa 1006cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 1007cd82a32eSJiri Olsa if (!pmu) 1008cd82a32eSJiri Olsa return NULL; 1009cd82a32eSJiri Olsa 10107ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 101154e32dc0SGanapatrao Kulkarni pmu->name = strdup(name); 101213d60ba0SKan Liang if (!pmu->name) 101313d60ba0SKan Liang goto err; 101413d60ba0SKan Liang 101513d60ba0SKan Liang alias_name = pmu_find_alias_name(name); 101613d60ba0SKan Liang if (alias_name) { 101713d60ba0SKan Liang pmu->alias_name = strdup(alias_name); 101813d60ba0SKan Liang if (!pmu->alias_name) 101913d60ba0SKan Liang goto err; 102013d60ba0SKan Liang } 102113d60ba0SKan Liang 102254e32dc0SGanapatrao Kulkarni pmu->type = type; 102366ec1191SMark Rutland pmu->is_uncore = pmu_is_uncore(name); 102451d54847SJohn Garry if (pmu->is_uncore) 102551d54847SJohn Garry pmu->id = pmu_id(name); 102649afa7f6SJin Yao pmu->is_hybrid = is_hybrid; 102790a86bdeSJiri Olsa pmu->max_precise = pmu_max_precise(name); 102854e32dc0SGanapatrao Kulkarni pmu_add_cpu_aliases(&aliases, pmu); 10294513c719SJohn Garry pmu_add_sys_aliases(&aliases, pmu); 103066ec1191SMark Rutland 1031cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 1032a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 10339fbc61f8SKan Liang INIT_LIST_HEAD(&pmu->caps); 1034cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 1035a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 10369bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 1037dc0a6202SAdrian Hunter 103844462430SJin Yao if (pmu->is_hybrid) 103944462430SJin Yao list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus); 104044462430SJin Yao 1041dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 1042dc0a6202SAdrian Hunter 1043cd82a32eSJiri Olsa return pmu; 104413d60ba0SKan Liang err: 104513d60ba0SKan Liang if (pmu->name) 104613d60ba0SKan Liang free(pmu->name); 104713d60ba0SKan Liang free(pmu); 104813d60ba0SKan Liang return NULL; 1049cd82a32eSJiri Olsa } 1050cd82a32eSJiri Olsa 1051b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 1052cd82a32eSJiri Olsa { 1053cd82a32eSJiri Olsa struct perf_pmu *pmu; 1054cd82a32eSJiri Olsa 105513d60ba0SKan Liang list_for_each_entry(pmu, &pmus, list) { 105613d60ba0SKan Liang if (!strcmp(pmu->name, name) || 105713d60ba0SKan Liang (pmu->alias_name && !strcmp(pmu->alias_name, name))) 1058cd82a32eSJiri Olsa return pmu; 105913d60ba0SKan Liang } 1060cd82a32eSJiri Olsa 1061cd82a32eSJiri Olsa return NULL; 1062cd82a32eSJiri Olsa } 1063cd82a32eSJiri Olsa 10643a50dc76SStephane Eranian struct perf_pmu *perf_pmu__find_by_type(unsigned int type) 10653a50dc76SStephane Eranian { 10663a50dc76SStephane Eranian struct perf_pmu *pmu; 10673a50dc76SStephane Eranian 10683a50dc76SStephane Eranian list_for_each_entry(pmu, &pmus, list) 10693a50dc76SStephane Eranian if (pmu->type == type) 10703a50dc76SStephane Eranian return pmu; 10713a50dc76SStephane Eranian 10723a50dc76SStephane Eranian return NULL; 10733a50dc76SStephane Eranian } 10743a50dc76SStephane Eranian 107550a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 107650a9667cSRobert Richter { 107750a9667cSRobert Richter /* 107850a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 107950a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 108050a9667cSRobert Richter */ 108150a9667cSRobert Richter if (!pmu) { 108250a9667cSRobert Richter pmu_read_sysfs(); 108350a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 108450a9667cSRobert Richter } 108550a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 108650a9667cSRobert Richter return pmu; 108750a9667cSRobert Richter return NULL; 108850a9667cSRobert Richter } 108950a9667cSRobert Richter 1090e76026bdSArnaldo Carvalho de Melo struct perf_pmu *evsel__find_pmu(struct evsel *evsel) 1091e12ee9f7SAdrian Hunter { 1092e12ee9f7SAdrian Hunter struct perf_pmu *pmu = NULL; 1093e12ee9f7SAdrian Hunter 1094e12ee9f7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1095e12ee9f7SAdrian Hunter if (pmu->type == evsel->core.attr.type) 1096e12ee9f7SAdrian Hunter break; 1097e12ee9f7SAdrian Hunter } 1098e12ee9f7SAdrian Hunter 1099e12ee9f7SAdrian Hunter return pmu; 1100e12ee9f7SAdrian Hunter } 1101e12ee9f7SAdrian Hunter 110239453ed5SArnaldo Carvalho de Melo bool evsel__is_aux_event(struct evsel *evsel) 1103e12ee9f7SAdrian Hunter { 1104e76026bdSArnaldo Carvalho de Melo struct perf_pmu *pmu = evsel__find_pmu(evsel); 1105e12ee9f7SAdrian Hunter 1106e12ee9f7SAdrian Hunter return pmu && pmu->auxtrace; 1107e12ee9f7SAdrian Hunter } 1108e12ee9f7SAdrian Hunter 1109b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 1110cd82a32eSJiri Olsa { 1111cd82a32eSJiri Olsa struct perf_pmu *pmu; 1112cd82a32eSJiri Olsa 1113cd82a32eSJiri Olsa /* 1114cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 1115cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 1116cd82a32eSJiri Olsa * the pmu format definitions. 1117cd82a32eSJiri Olsa */ 1118cd82a32eSJiri Olsa pmu = pmu_find(name); 1119cd82a32eSJiri Olsa if (pmu) 1120cd82a32eSJiri Olsa return pmu; 1121cd82a32eSJiri Olsa 1122cd82a32eSJiri Olsa return pmu_lookup(name); 1123cd82a32eSJiri Olsa } 1124cd82a32eSJiri Olsa 11255c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 112609ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 1127cd82a32eSJiri Olsa { 11285c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1129cd82a32eSJiri Olsa 1130cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 1131cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 1132cd82a32eSJiri Olsa return format; 1133cd82a32eSJiri Olsa 1134cd82a32eSJiri Olsa return NULL; 1135cd82a32eSJiri Olsa } 1136cd82a32eSJiri Olsa 113709ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 113809ff6071SAdrian Hunter { 113909ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 114009ff6071SAdrian Hunter __u64 bits = 0; 114109ff6071SAdrian Hunter int fbit; 114209ff6071SAdrian Hunter 114309ff6071SAdrian Hunter if (!format) 114409ff6071SAdrian Hunter return 0; 114509ff6071SAdrian Hunter 114609ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 114709ff6071SAdrian Hunter bits |= 1ULL << fbit; 114809ff6071SAdrian Hunter 114909ff6071SAdrian Hunter return bits; 115009ff6071SAdrian Hunter } 115109ff6071SAdrian Hunter 1152a1ac7de6SAdrian Hunter int perf_pmu__format_type(struct list_head *formats, const char *name) 1153a1ac7de6SAdrian Hunter { 1154a1ac7de6SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 1155a1ac7de6SAdrian Hunter 1156a1ac7de6SAdrian Hunter if (!format) 1157a1ac7de6SAdrian Hunter return -1; 1158a1ac7de6SAdrian Hunter 1159a1ac7de6SAdrian Hunter return format->value; 1160a1ac7de6SAdrian Hunter } 1161a1ac7de6SAdrian Hunter 1162cd82a32eSJiri Olsa /* 1163dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 11644d39c89fSIngo Molnar * and unformatted value (value parameter). 1165cd82a32eSJiri Olsa */ 1166dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1167dc0a6202SAdrian Hunter bool zero) 1168cd82a32eSJiri Olsa { 1169cd82a32eSJiri Olsa unsigned long fbit, vbit; 1170cd82a32eSJiri Olsa 1171cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1172cd82a32eSJiri Olsa 1173cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 1174cd82a32eSJiri Olsa continue; 1175cd82a32eSJiri Olsa 1176dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 1177dc0a6202SAdrian Hunter *v |= (1llu << fbit); 1178dc0a6202SAdrian Hunter else if (zero) 1179dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 1180cd82a32eSJiri Olsa } 1181cd82a32eSJiri Olsa } 1182cd82a32eSJiri Olsa 11830efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 11840efe6b67SAdrian Hunter { 11851b9caa10SJiri Olsa int w; 11860efe6b67SAdrian Hunter 11871b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 11881b9caa10SJiri Olsa if (!w) 11891b9caa10SJiri Olsa return 0; 11901b9caa10SJiri Olsa if (w < 64) 11911b9caa10SJiri Olsa return (1ULL << w) - 1; 11921b9caa10SJiri Olsa return -1; 11930efe6b67SAdrian Hunter } 11940efe6b67SAdrian Hunter 1195cd82a32eSJiri Olsa /* 1196688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 1197688d4dfcSCody P Schafer * in the remaining terms. 1198688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 1199688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 1200688d4dfcSCody P Schafer * in a config string) later on in the term list. 1201688d4dfcSCody P Schafer */ 1202688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 1203688d4dfcSCody P Schafer struct list_head *head_terms, 1204688d4dfcSCody P Schafer __u64 *value) 1205688d4dfcSCody P Schafer { 1206688d4dfcSCody P Schafer struct parse_events_term *t; 1207688d4dfcSCody P Schafer 1208688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 12092a3d252dSIan Rogers if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 12102a3d252dSIan Rogers t->config && !strcmp(t->config, term->config)) { 1211688d4dfcSCody P Schafer t->used = true; 1212688d4dfcSCody P Schafer *value = t->val.num; 1213688d4dfcSCody P Schafer return 0; 1214688d4dfcSCody P Schafer } 1215688d4dfcSCody P Schafer } 1216688d4dfcSCody P Schafer 1217bb963e16SNamhyung Kim if (verbose > 0) 1218688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 1219688d4dfcSCody P Schafer 1220688d4dfcSCody P Schafer return -1; 1221688d4dfcSCody P Schafer } 1222688d4dfcSCody P Schafer 1223ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 1224e64b020bSJiri Olsa { 1225e64b020bSJiri Olsa struct perf_pmu_format *format; 122611db4e29SMasami Hiramatsu char *str = NULL; 122711db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1228e64b020bSJiri Olsa unsigned i = 0; 1229e64b020bSJiri Olsa 1230ffeb883eSHe Kuang if (!formats) 1231e64b020bSJiri Olsa return NULL; 1232e64b020bSJiri Olsa 1233e64b020bSJiri Olsa /* sysfs exported terms */ 1234ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 123511db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 123611db4e29SMasami Hiramatsu goto error; 1237e64b020bSJiri Olsa 1238ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 123911db4e29SMasami Hiramatsu error: 1240ffeb883eSHe Kuang strbuf_release(&buf); 1241e64b020bSJiri Olsa 1242e64b020bSJiri Olsa return str; 1243e64b020bSJiri Olsa } 1244e64b020bSJiri Olsa 1245688d4dfcSCody P Schafer /* 1246cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 124788aca8d9SCody P Schafer * user input data - term parameter. 1248cd82a32eSJiri Olsa */ 12494ac22b48SIan Rogers static int pmu_config_term(const char *pmu_name, 12504ac22b48SIan Rogers struct list_head *formats, 1251cd82a32eSJiri Olsa struct perf_event_attr *attr, 1252dc0a6202SAdrian Hunter struct parse_events_term *term, 1253688d4dfcSCody P Schafer struct list_head *head_terms, 1254e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1255cd82a32eSJiri Olsa { 12565c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1257cd82a32eSJiri Olsa __u64 *vp; 12580efe6b67SAdrian Hunter __u64 val, max_val; 1259cd82a32eSJiri Olsa 1260cd82a32eSJiri Olsa /* 1261688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1262688d4dfcSCody P Schafer * skip it in normal eval. 1263688d4dfcSCody P Schafer */ 1264688d4dfcSCody P Schafer if (term->used) 1265688d4dfcSCody P Schafer return 0; 1266688d4dfcSCody P Schafer 1267688d4dfcSCody P Schafer /* 1268cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1269cd82a32eSJiri Olsa * to be done for them. 1270cd82a32eSJiri Olsa */ 1271cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1272cd82a32eSJiri Olsa return 0; 1273cd82a32eSJiri Olsa 1274cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 1275688d4dfcSCody P Schafer if (!format) { 1276ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 12774ac22b48SIan Rogers char *unknown_term; 12784ac22b48SIan Rogers char *help_msg; 1279ffeb883eSHe Kuang 12804ac22b48SIan Rogers if (asprintf(&unknown_term, 12814ac22b48SIan Rogers "unknown term '%s' for pmu '%s'", 12824ac22b48SIan Rogers term->config, pmu_name) < 0) 12834ac22b48SIan Rogers unknown_term = NULL; 12844ac22b48SIan Rogers help_msg = parse_events_formats_error_string(pmu_term); 12854ac22b48SIan Rogers if (err) { 1286448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 12874ac22b48SIan Rogers unknown_term, 12884ac22b48SIan Rogers help_msg); 12894ac22b48SIan Rogers } else { 12904ac22b48SIan Rogers pr_debug("%s (%s)\n", unknown_term, help_msg); 12914ac22b48SIan Rogers free(unknown_term); 1292e64b020bSJiri Olsa } 12934ac22b48SIan Rogers free(pmu_term); 1294cd82a32eSJiri Olsa return -EINVAL; 1295688d4dfcSCody P Schafer } 1296cd82a32eSJiri Olsa 1297cd82a32eSJiri Olsa switch (format->value) { 1298cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1299cd82a32eSJiri Olsa vp = &attr->config; 1300cd82a32eSJiri Olsa break; 1301cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1302cd82a32eSJiri Olsa vp = &attr->config1; 1303cd82a32eSJiri Olsa break; 1304cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1305cd82a32eSJiri Olsa vp = &attr->config2; 1306cd82a32eSJiri Olsa break; 1307cd82a32eSJiri Olsa default: 1308cd82a32eSJiri Olsa return -EINVAL; 1309cd82a32eSJiri Olsa } 1310cd82a32eSJiri Olsa 131116fa7e82SJiri Olsa /* 1312688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1313688d4dfcSCody P Schafer * using event parameters. 131416fa7e82SJiri Olsa */ 131599e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 131699e7138eSJiri Olsa if (term->no_value && 131799e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 131899e7138eSJiri Olsa if (err) { 1319448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1320448d732cSIan Rogers strdup("no value assigned for term"), 1321448d732cSIan Rogers NULL); 132299e7138eSJiri Olsa } 132399e7138eSJiri Olsa return -EINVAL; 132499e7138eSJiri Olsa } 132599e7138eSJiri Olsa 1326688d4dfcSCody P Schafer val = term->val.num; 132799e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1328688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1329bb963e16SNamhyung Kim if (verbose > 0) { 1330688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1331688d4dfcSCody P Schafer term->config, term->val.str); 1332e64b020bSJiri Olsa } 1333e64b020bSJiri Olsa if (err) { 1334448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1335448d732cSIan Rogers strdup("expected numeric value"), 1336448d732cSIan Rogers NULL); 1337e64b020bSJiri Olsa } 1338688d4dfcSCody P Schafer return -EINVAL; 1339688d4dfcSCody P Schafer } 1340688d4dfcSCody P Schafer 1341688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1342688d4dfcSCody P Schafer return -EINVAL; 1343688d4dfcSCody P Schafer } else 1344688d4dfcSCody P Schafer return -EINVAL; 1345688d4dfcSCody P Schafer 13460efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 13470efe6b67SAdrian Hunter if (val > max_val) { 13480efe6b67SAdrian Hunter if (err) { 1349448d732cSIan Rogers char *err_str; 1350448d732cSIan Rogers 1351448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1352448d732cSIan Rogers asprintf(&err_str, 13530efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1354448d732cSIan Rogers (unsigned long long)max_val) < 0 1355448d732cSIan Rogers ? strdup("value too big for format") 1356448d732cSIan Rogers : err_str, 1357448d732cSIan Rogers NULL); 13580efe6b67SAdrian Hunter return -EINVAL; 13590efe6b67SAdrian Hunter } 13600efe6b67SAdrian Hunter /* 13610efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 13620efe6b67SAdrian Hunter * silently truncated. 13630efe6b67SAdrian Hunter */ 13640efe6b67SAdrian Hunter } 13650efe6b67SAdrian Hunter 1366688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1367cd82a32eSJiri Olsa return 0; 1368cd82a32eSJiri Olsa } 1369cd82a32eSJiri Olsa 13704ac22b48SIan Rogers int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, 1371cff7f956SJiri Olsa struct perf_event_attr *attr, 1372dc0a6202SAdrian Hunter struct list_head *head_terms, 1373e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1374cd82a32eSJiri Olsa { 13756cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1376cd82a32eSJiri Olsa 1377688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 13784ac22b48SIan Rogers if (pmu_config_term(pmu_name, formats, attr, term, head_terms, 1379e64b020bSJiri Olsa zero, err)) 1380cd82a32eSJiri Olsa return -EINVAL; 1381688d4dfcSCody P Schafer } 1382cd82a32eSJiri Olsa 1383cd82a32eSJiri Olsa return 0; 1384cd82a32eSJiri Olsa } 1385cd82a32eSJiri Olsa 1386cd82a32eSJiri Olsa /* 1387cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1388cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1389cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1390cd82a32eSJiri Olsa */ 1391cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1392e64b020bSJiri Olsa struct list_head *head_terms, 1393e64b020bSJiri Olsa struct parse_events_error *err) 1394cd82a32eSJiri Olsa { 1395dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1396dc0a6202SAdrian Hunter 1397cd82a32eSJiri Olsa attr->type = pmu->type; 13984ac22b48SIan Rogers return perf_pmu__config_terms(pmu->name, &pmu->format, attr, 13994ac22b48SIan Rogers head_terms, zero, err); 1400cd82a32eSJiri Olsa } 1401cd82a32eSJiri Olsa 14025c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 14036cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1404a6146d50SZheng Yan { 14055c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1406a6146d50SZheng Yan char *name; 1407a6146d50SZheng Yan 1408a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1409a6146d50SZheng Yan return NULL; 1410a6146d50SZheng Yan 1411a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1412a6146d50SZheng Yan if (term->val.num != 1) 1413a6146d50SZheng Yan return NULL; 1414a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1415a6146d50SZheng Yan return NULL; 1416a6146d50SZheng Yan name = term->config; 1417a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1418a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1419a6146d50SZheng Yan return NULL; 1420a6146d50SZheng Yan name = term->val.str; 1421a6146d50SZheng Yan } else { 1422a6146d50SZheng Yan return NULL; 1423a6146d50SZheng Yan } 1424a6146d50SZheng Yan 1425a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 1426a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 1427a6146d50SZheng Yan return alias; 1428a6146d50SZheng Yan } 1429a6146d50SZheng Yan return NULL; 1430a6146d50SZheng Yan } 1431a6146d50SZheng Yan 1432410136f5SStephane Eranian 14331d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 14341d9e446bSJiri Olsa struct perf_pmu_info *info) 1435410136f5SStephane Eranian { 1436410136f5SStephane Eranian /* 1437410136f5SStephane Eranian * Only one term in event definition can 14381d9e446bSJiri Olsa * define unit, scale and snapshot, fail 14391d9e446bSJiri Olsa * if there's more than one. 1440410136f5SStephane Eranian */ 1441b30a7d1fSArnaldo Carvalho de Melo if ((info->unit && alias->unit[0]) || 14421d9e446bSJiri Olsa (info->scale && alias->scale) || 14431d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 1444410136f5SStephane Eranian return -EINVAL; 1445410136f5SStephane Eranian 1446b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 14471d9e446bSJiri Olsa info->unit = alias->unit; 1448410136f5SStephane Eranian 1449410136f5SStephane Eranian if (alias->scale) 14501d9e446bSJiri Olsa info->scale = alias->scale; 14511d9e446bSJiri Olsa 14521d9e446bSJiri Olsa if (alias->snapshot) 14531d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1454410136f5SStephane Eranian 1455410136f5SStephane Eranian return 0; 1456410136f5SStephane Eranian } 1457410136f5SStephane Eranian 1458a6146d50SZheng Yan /* 1459a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1460a6146d50SZheng Yan * defined for the alias 1461a6146d50SZheng Yan */ 1462410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 146346441bdcSMatt Fleming struct perf_pmu_info *info) 1464a6146d50SZheng Yan { 14656cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 14665c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1467a6146d50SZheng Yan int ret; 1468a6146d50SZheng Yan 1469044330c1SMatt Fleming info->per_pkg = false; 1470044330c1SMatt Fleming 14718a398897SStephane Eranian /* 14728a398897SStephane Eranian * Mark unit and scale as not set 14738a398897SStephane Eranian * (different from default values, see below) 14748a398897SStephane Eranian */ 147546441bdcSMatt Fleming info->unit = NULL; 147646441bdcSMatt Fleming info->scale = 0.0; 14771d9e446bSJiri Olsa info->snapshot = false; 147837932c18SAndi Kleen info->metric_expr = NULL; 147996284814SAndi Kleen info->metric_name = NULL; 1480410136f5SStephane Eranian 1481a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1482a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1483a6146d50SZheng Yan if (!alias) 1484a6146d50SZheng Yan continue; 1485a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 1486a6146d50SZheng Yan if (ret) 1487a6146d50SZheng Yan return ret; 1488410136f5SStephane Eranian 14891d9e446bSJiri Olsa ret = check_info_data(alias, info); 1490410136f5SStephane Eranian if (ret) 1491410136f5SStephane Eranian return ret; 1492410136f5SStephane Eranian 1493044330c1SMatt Fleming if (alias->per_pkg) 1494044330c1SMatt Fleming info->per_pkg = true; 149537932c18SAndi Kleen info->metric_expr = alias->metric_expr; 149696284814SAndi Kleen info->metric_name = alias->metric_name; 1497044330c1SMatt Fleming 1498e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 14991dc92556SIan Rogers parse_events_term__delete(term); 1500a6146d50SZheng Yan } 15018a398897SStephane Eranian 15028a398897SStephane Eranian /* 15038a398897SStephane Eranian * if no unit or scale found in aliases, then 15048a398897SStephane Eranian * set defaults as for evsel 15058a398897SStephane Eranian * unit cannot left to NULL 15068a398897SStephane Eranian */ 150746441bdcSMatt Fleming if (info->unit == NULL) 150846441bdcSMatt Fleming info->unit = ""; 15098a398897SStephane Eranian 151046441bdcSMatt Fleming if (info->scale == 0.0) 151146441bdcSMatt Fleming info->scale = 1.0; 15128a398897SStephane Eranian 1513a6146d50SZheng Yan return 0; 1514a6146d50SZheng Yan } 1515a6146d50SZheng Yan 1516cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 1517cd82a32eSJiri Olsa int config, unsigned long *bits) 1518cd82a32eSJiri Olsa { 15195c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1520cd82a32eSJiri Olsa 1521cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 1522cd82a32eSJiri Olsa if (!format) 1523cd82a32eSJiri Olsa return -ENOMEM; 1524cd82a32eSJiri Olsa 1525cd82a32eSJiri Olsa format->name = strdup(name); 1526cd82a32eSJiri Olsa format->value = config; 1527cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 1528cd82a32eSJiri Olsa 1529cd82a32eSJiri Olsa list_add_tail(&format->list, list); 1530cd82a32eSJiri Olsa return 0; 1531cd82a32eSJiri Olsa } 1532cd82a32eSJiri Olsa 1533cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 1534cd82a32eSJiri Olsa { 1535cd82a32eSJiri Olsa long b; 1536cd82a32eSJiri Olsa 1537cd82a32eSJiri Olsa if (!to) 1538cd82a32eSJiri Olsa to = from; 1539cd82a32eSJiri Olsa 154015268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1541cd82a32eSJiri Olsa for (b = from; b <= to; b++) 1542cd82a32eSJiri Olsa set_bit(b, bits); 1543cd82a32eSJiri Olsa } 1544dc098b35SAndi Kleen 1545d26383dcSNamhyung Kim void perf_pmu__del_formats(struct list_head *formats) 1546d26383dcSNamhyung Kim { 1547d26383dcSNamhyung Kim struct perf_pmu_format *fmt, *tmp; 1548d26383dcSNamhyung Kim 1549d26383dcSNamhyung Kim list_for_each_entry_safe(fmt, tmp, formats, list) { 1550d26383dcSNamhyung Kim list_del(&fmt->list); 1551d26383dcSNamhyung Kim free(fmt->name); 1552d26383dcSNamhyung Kim free(fmt); 1553d26383dcSNamhyung Kim } 1554d26383dcSNamhyung Kim } 1555d26383dcSNamhyung Kim 1556aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 1557aaea3617SCody P Schafer { 1558aaea3617SCody P Schafer if (b > a) 1559aaea3617SCody P Schafer return 0; 1560aaea3617SCody P Schafer return a - b; 1561aaea3617SCody P Schafer } 1562aaea3617SCody P Schafer 1563dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 1564dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1565dc098b35SAndi Kleen { 1566aaea3617SCody P Schafer struct parse_events_term *term; 1567aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1568aaea3617SCody P Schafer 1569aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 1570aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1571aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 1572aaea3617SCody P Schafer ",%s=%s", term->config, 1573aaea3617SCody P Schafer term->val.str); 1574aaea3617SCody P Schafer } 1575aaea3617SCody P Schafer 1576aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1577aaea3617SCody P Schafer buf[used] = '/'; 1578aaea3617SCody P Schafer used++; 1579aaea3617SCody P Schafer } 1580aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1581aaea3617SCody P Schafer buf[used] = '\0'; 1582aaea3617SCody P Schafer used++; 1583aaea3617SCody P Schafer } else 1584aaea3617SCody P Schafer buf[len - 1] = '\0'; 1585aaea3617SCody P Schafer 1586dc098b35SAndi Kleen return buf; 1587dc098b35SAndi Kleen } 1588dc098b35SAndi Kleen 1589dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 1590dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1591dc098b35SAndi Kleen { 1592dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 1593dc098b35SAndi Kleen return buf; 1594dc098b35SAndi Kleen } 1595dc098b35SAndi Kleen 1596dd5f1036SAndi Kleen struct sevent { 159708e60ed1SAndi Kleen char *name; 159808e60ed1SAndi Kleen char *desc; 1599dd5f1036SAndi Kleen char *topic; 1600f2361024SAndi Kleen char *str; 1601f2361024SAndi Kleen char *pmu; 16027f372a63SAndi Kleen char *metric_expr; 160396284814SAndi Kleen char *metric_name; 1604ce0dc7d2SJohn Garry int is_cpu; 160508e60ed1SAndi Kleen }; 160608e60ed1SAndi Kleen 1607dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b) 1608dc098b35SAndi Kleen { 1609dd5f1036SAndi Kleen const struct sevent *as = a; 1610dd5f1036SAndi Kleen const struct sevent *bs = b; 161108e60ed1SAndi Kleen 161208e60ed1SAndi Kleen /* Put extra events last */ 161308e60ed1SAndi Kleen if (!!as->desc != !!bs->desc) 161408e60ed1SAndi Kleen return !!as->desc - !!bs->desc; 1615dd5f1036SAndi Kleen if (as->topic && bs->topic) { 1616dd5f1036SAndi Kleen int n = strcmp(as->topic, bs->topic); 1617dd5f1036SAndi Kleen 1618dd5f1036SAndi Kleen if (n) 1619dd5f1036SAndi Kleen return n; 1620dd5f1036SAndi Kleen } 1621ce0dc7d2SJohn Garry 1622ce0dc7d2SJohn Garry /* Order CPU core events to be first */ 1623ce0dc7d2SJohn Garry if (as->is_cpu != bs->is_cpu) 1624ce0dc7d2SJohn Garry return bs->is_cpu - as->is_cpu; 1625ce0dc7d2SJohn Garry 162608e60ed1SAndi Kleen return strcmp(as->name, bs->name); 162708e60ed1SAndi Kleen } 162808e60ed1SAndi Kleen 162908e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr) 163008e60ed1SAndi Kleen { 163108e60ed1SAndi Kleen int column = start; 163208e60ed1SAndi Kleen int n; 163308e60ed1SAndi Kleen 163408e60ed1SAndi Kleen while (*s) { 163508e60ed1SAndi Kleen int wlen = strcspn(s, " \t"); 163608e60ed1SAndi Kleen 163708e60ed1SAndi Kleen if (column + wlen >= max && column > start) { 163808e60ed1SAndi Kleen printf("\n%*s", start, ""); 163908e60ed1SAndi Kleen column = start + corr; 164008e60ed1SAndi Kleen } 164108e60ed1SAndi Kleen n = printf("%s%.*s", column > start ? " " : "", wlen, s); 164208e60ed1SAndi Kleen if (n <= 0) 164308e60ed1SAndi Kleen break; 164408e60ed1SAndi Kleen s += wlen; 164508e60ed1SAndi Kleen column += n; 164632858480SArnaldo Carvalho de Melo s = skip_spaces(s); 164708e60ed1SAndi Kleen } 1648dc098b35SAndi Kleen } 1649dc098b35SAndi Kleen 1650d504fae9SJohn Garry bool is_pmu_core(const char *name) 1651d504fae9SJohn Garry { 1652d504fae9SJohn Garry return !strcmp(name, "cpu") || is_arm_pmu_core(name); 1653d504fae9SJohn Garry } 1654d504fae9SJohn Garry 1655c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, 1656a7f6c8c8SJin Yao bool long_desc, bool details_flag, bool deprecated) 1657dc098b35SAndi Kleen { 1658dc098b35SAndi Kleen struct perf_pmu *pmu; 1659dc098b35SAndi Kleen struct perf_pmu_alias *alias; 1660dc098b35SAndi Kleen char buf[1024]; 1661dc098b35SAndi Kleen int printed = 0; 1662dc098b35SAndi Kleen int len, j; 1663dd5f1036SAndi Kleen struct sevent *aliases; 166408e60ed1SAndi Kleen int numdesc = 0; 166561eb2eb4SAndi Kleen int columns = pager_get_columns(); 1666dd5f1036SAndi Kleen char *topic = NULL; 1667dc098b35SAndi Kleen 1668dc098b35SAndi Kleen pmu = NULL; 1669dc098b35SAndi Kleen len = 0; 167042634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1671dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 1672dc098b35SAndi Kleen len++; 167342634bc7SAdrian Hunter if (pmu->selectable) 167442634bc7SAdrian Hunter len++; 167542634bc7SAdrian Hunter } 1676dd5f1036SAndi Kleen aliases = zalloc(sizeof(struct sevent) * len); 1677dc098b35SAndi Kleen if (!aliases) 16787e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1679dc098b35SAndi Kleen pmu = NULL; 1680dc098b35SAndi Kleen j = 0; 168142634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1682dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 168308e60ed1SAndi Kleen char *name = alias->desc ? alias->name : 168408e60ed1SAndi Kleen format_alias(buf, sizeof(buf), pmu, alias); 1685c1b4745bSJohn Garry bool is_cpu = is_pmu_core(pmu->name); 1686dc098b35SAndi Kleen 1687a7f6c8c8SJin Yao if (alias->deprecated && !deprecated) 1688a7f6c8c8SJin Yao continue; 1689a7f6c8c8SJin Yao 1690dc098b35SAndi Kleen if (event_glob != NULL && 169138d14f0cSAndi Kleen !(strglobmatch_nocase(name, event_glob) || 169238d14f0cSAndi Kleen (!is_cpu && strglobmatch_nocase(alias->name, 169367bdc35fSAndi Kleen event_glob)) || 169467bdc35fSAndi Kleen (alias->topic && 169567bdc35fSAndi Kleen strglobmatch_nocase(alias->topic, event_glob)))) 1696dc098b35SAndi Kleen continue; 16977e4772dcSArnaldo Carvalho de Melo 169808e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 16997e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 17007e4772dcSArnaldo Carvalho de Melo 170108e60ed1SAndi Kleen aliases[j].name = name; 170208e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 170308e60ed1SAndi Kleen aliases[j].name = format_alias_or(buf, 170408e60ed1SAndi Kleen sizeof(buf), 170508e60ed1SAndi Kleen pmu, alias); 170608e60ed1SAndi Kleen aliases[j].name = strdup(aliases[j].name); 170708e60ed1SAndi Kleen if (!aliases[j].name) 17087e4772dcSArnaldo Carvalho de Melo goto out_enomem; 170908e60ed1SAndi Kleen 1710c8d6828aSSukadev Bhattiprolu aliases[j].desc = long_desc ? alias->long_desc : 1711c8d6828aSSukadev Bhattiprolu alias->desc; 1712dd5f1036SAndi Kleen aliases[j].topic = alias->topic; 1713f2361024SAndi Kleen aliases[j].str = alias->str; 1714f2361024SAndi Kleen aliases[j].pmu = pmu->name; 17157f372a63SAndi Kleen aliases[j].metric_expr = alias->metric_expr; 171696284814SAndi Kleen aliases[j].metric_name = alias->metric_name; 1717ce0dc7d2SJohn Garry aliases[j].is_cpu = is_cpu; 1718dc098b35SAndi Kleen j++; 1719dc098b35SAndi Kleen } 1720fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1721fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 17227e4772dcSArnaldo Carvalho de Melo char *s; 17237e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 17247e4772dcSArnaldo Carvalho de Melo goto out_enomem; 172508e60ed1SAndi Kleen aliases[j].name = s; 172642634bc7SAdrian Hunter j++; 172742634bc7SAdrian Hunter } 172842634bc7SAdrian Hunter } 1729dc098b35SAndi Kleen len = j; 1730dd5f1036SAndi Kleen qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1731dc098b35SAndi Kleen for (j = 0; j < len; j++) { 173215b22ed3SAndi Kleen /* Skip duplicates */ 173315b22ed3SAndi Kleen if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) 173415b22ed3SAndi Kleen continue; 1735dc098b35SAndi Kleen if (name_only) { 173608e60ed1SAndi Kleen printf("%s ", aliases[j].name); 1737dc098b35SAndi Kleen continue; 1738dc098b35SAndi Kleen } 17391c5f01feSAndi Kleen if (aliases[j].desc && !quiet_flag) { 174008e60ed1SAndi Kleen if (numdesc++ == 0) 174108e60ed1SAndi Kleen printf("\n"); 1742dd5f1036SAndi Kleen if (aliases[j].topic && (!topic || 1743dd5f1036SAndi Kleen strcmp(topic, aliases[j].topic))) { 1744dd5f1036SAndi Kleen printf("%s%s:\n", topic ? "\n" : "", 1745dd5f1036SAndi Kleen aliases[j].topic); 1746dd5f1036SAndi Kleen topic = aliases[j].topic; 1747dd5f1036SAndi Kleen } 174808e60ed1SAndi Kleen printf(" %-50s\n", aliases[j].name); 174908e60ed1SAndi Kleen printf("%*s", 8, "["); 175008e60ed1SAndi Kleen wordwrap(aliases[j].desc, 8, columns, 0); 175108e60ed1SAndi Kleen printf("]\n"); 1752bf874fcfSAndi Kleen if (details_flag) { 17537f372a63SAndi Kleen printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); 175496284814SAndi Kleen if (aliases[j].metric_name) 175596284814SAndi Kleen printf(" MetricName: %s", aliases[j].metric_name); 17567f372a63SAndi Kleen if (aliases[j].metric_expr) 17577f372a63SAndi Kleen printf(" MetricExpr: %s", aliases[j].metric_expr); 17587f372a63SAndi Kleen putchar('\n'); 17597f372a63SAndi Kleen } 176008e60ed1SAndi Kleen } else 176108e60ed1SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j].name); 1762dc098b35SAndi Kleen printed++; 1763dc098b35SAndi Kleen } 1764dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1765dc098b35SAndi Kleen printf("\n"); 17667e4772dcSArnaldo Carvalho de Melo out_free: 17677e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 176808e60ed1SAndi Kleen zfree(&aliases[j].name); 17697e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 17707e4772dcSArnaldo Carvalho de Melo return; 17717e4772dcSArnaldo Carvalho de Melo 17727e4772dcSArnaldo Carvalho de Melo out_enomem: 17737e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 17747e4772dcSArnaldo Carvalho de Melo if (aliases) 17757e4772dcSArnaldo Carvalho de Melo goto out_free; 1776dc098b35SAndi Kleen } 17774cabc3d1SAndi Kleen 17784cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 17794cabc3d1SAndi Kleen { 17804cabc3d1SAndi Kleen struct perf_pmu *pmu; 17814cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 17824cabc3d1SAndi Kleen 17834cabc3d1SAndi Kleen pmu = NULL; 17844cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 17854cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 17864cabc3d1SAndi Kleen continue; 17874cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 17884cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 17894cabc3d1SAndi Kleen return true; 17904cabc3d1SAndi Kleen } 17914cabc3d1SAndi Kleen return false; 17924cabc3d1SAndi Kleen } 17937d4bdab5SAdrian Hunter 17947d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 17957d4bdab5SAdrian Hunter { 17967d4bdab5SAdrian Hunter char path[PATH_MAX]; 17977d4bdab5SAdrian Hunter const char *sysfs; 17987d4bdab5SAdrian Hunter 17997d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 18007d4bdab5SAdrian Hunter if (!sysfs) 18017d4bdab5SAdrian Hunter return NULL; 18027d4bdab5SAdrian Hunter 18037d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 18047d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 1805d9664582SAndi Kleen if (!file_available(path)) 18067d4bdab5SAdrian Hunter return NULL; 18077d4bdab5SAdrian Hunter return fopen(path, "r"); 18087d4bdab5SAdrian Hunter } 18097d4bdab5SAdrian Hunter 18107d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 18117d4bdab5SAdrian Hunter ...) 18127d4bdab5SAdrian Hunter { 18137d4bdab5SAdrian Hunter va_list args; 18147d4bdab5SAdrian Hunter FILE *file; 18157d4bdab5SAdrian Hunter int ret = EOF; 18167d4bdab5SAdrian Hunter 18177d4bdab5SAdrian Hunter va_start(args, fmt); 18187d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 18197d4bdab5SAdrian Hunter if (file) { 18207d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 18217d4bdab5SAdrian Hunter fclose(file); 18227d4bdab5SAdrian Hunter } 18237d4bdab5SAdrian Hunter va_end(args); 18247d4bdab5SAdrian Hunter return ret; 18257d4bdab5SAdrian Hunter } 18269fbc61f8SKan Liang 18279fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 18289fbc61f8SKan Liang { 18299fbc61f8SKan Liang struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 18309fbc61f8SKan Liang 18319fbc61f8SKan Liang if (!caps) 18329fbc61f8SKan Liang return -ENOMEM; 18339fbc61f8SKan Liang 18349fbc61f8SKan Liang caps->name = strdup(name); 18359fbc61f8SKan Liang if (!caps->name) 18369fbc61f8SKan Liang goto free_caps; 18379fbc61f8SKan Liang caps->value = strndup(value, strlen(value) - 1); 18389fbc61f8SKan Liang if (!caps->value) 18399fbc61f8SKan Liang goto free_name; 18409fbc61f8SKan Liang list_add_tail(&caps->list, list); 18419fbc61f8SKan Liang return 0; 18429fbc61f8SKan Liang 18439fbc61f8SKan Liang free_name: 18449fbc61f8SKan Liang zfree(caps->name); 18459fbc61f8SKan Liang free_caps: 18469fbc61f8SKan Liang free(caps); 18479fbc61f8SKan Liang 18489fbc61f8SKan Liang return -ENOMEM; 18499fbc61f8SKan Liang } 18509fbc61f8SKan Liang 18519fbc61f8SKan Liang /* 18529fbc61f8SKan Liang * Reading/parsing the given pmu capabilities, which should be located at: 18539fbc61f8SKan Liang * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 18549fbc61f8SKan Liang * Return the number of capabilities 18559fbc61f8SKan Liang */ 18569fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu) 18579fbc61f8SKan Liang { 18589fbc61f8SKan Liang struct stat st; 18599fbc61f8SKan Liang char caps_path[PATH_MAX]; 18609fbc61f8SKan Liang const char *sysfs = sysfs__mountpoint(); 18619fbc61f8SKan Liang DIR *caps_dir; 18629fbc61f8SKan Liang struct dirent *evt_ent; 18639fbc61f8SKan Liang int nr_caps = 0; 18649fbc61f8SKan Liang 18659fbc61f8SKan Liang if (!sysfs) 18669fbc61f8SKan Liang return -1; 18679fbc61f8SKan Liang 18689fbc61f8SKan Liang snprintf(caps_path, PATH_MAX, 18699fbc61f8SKan Liang "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name); 18709fbc61f8SKan Liang 18719fbc61f8SKan Liang if (stat(caps_path, &st) < 0) 18729fbc61f8SKan Liang return 0; /* no error if caps does not exist */ 18739fbc61f8SKan Liang 18749fbc61f8SKan Liang caps_dir = opendir(caps_path); 18759fbc61f8SKan Liang if (!caps_dir) 18769fbc61f8SKan Liang return -EINVAL; 18779fbc61f8SKan Liang 18789fbc61f8SKan Liang while ((evt_ent = readdir(caps_dir)) != NULL) { 18799fbc61f8SKan Liang char path[PATH_MAX + NAME_MAX + 1]; 18809fbc61f8SKan Liang char *name = evt_ent->d_name; 18819fbc61f8SKan Liang char value[128]; 18829fbc61f8SKan Liang FILE *file; 18839fbc61f8SKan Liang 18849fbc61f8SKan Liang if (!strcmp(name, ".") || !strcmp(name, "..")) 18859fbc61f8SKan Liang continue; 18869fbc61f8SKan Liang 18879fbc61f8SKan Liang snprintf(path, sizeof(path), "%s/%s", caps_path, name); 18889fbc61f8SKan Liang 18899fbc61f8SKan Liang file = fopen(path, "r"); 18909fbc61f8SKan Liang if (!file) 18919fbc61f8SKan Liang continue; 18929fbc61f8SKan Liang 18939fbc61f8SKan Liang if (!fgets(value, sizeof(value), file) || 18949fbc61f8SKan Liang (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 18959fbc61f8SKan Liang fclose(file); 18969fbc61f8SKan Liang continue; 18979fbc61f8SKan Liang } 18989fbc61f8SKan Liang 18999fbc61f8SKan Liang nr_caps++; 19009fbc61f8SKan Liang fclose(file); 19019fbc61f8SKan Liang } 19029fbc61f8SKan Liang 19039fbc61f8SKan Liang closedir(caps_dir); 19049fbc61f8SKan Liang 19059fbc61f8SKan Liang return nr_caps; 19069fbc61f8SKan Liang } 1907e4064776SJin Yao 1908e4064776SJin Yao void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 1909e4064776SJin Yao char *name) 1910e4064776SJin Yao { 1911e4064776SJin Yao struct perf_pmu_format *format; 1912e4064776SJin Yao __u64 masks = 0, bits; 1913e4064776SJin Yao char buf[100]; 1914e4064776SJin Yao unsigned int i; 1915e4064776SJin Yao 1916e4064776SJin Yao list_for_each_entry(format, &pmu->format, list) { 1917e4064776SJin Yao if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG) 1918e4064776SJin Yao continue; 1919e4064776SJin Yao 1920e4064776SJin Yao for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1921e4064776SJin Yao masks |= 1ULL << i; 1922e4064776SJin Yao } 1923e4064776SJin Yao 1924e4064776SJin Yao /* 1925e4064776SJin Yao * Kernel doesn't export any valid format bits. 1926e4064776SJin Yao */ 1927e4064776SJin Yao if (masks == 0) 1928e4064776SJin Yao return; 1929e4064776SJin Yao 1930e4064776SJin Yao bits = config & ~masks; 1931e4064776SJin Yao if (bits == 0) 1932e4064776SJin Yao return; 1933e4064776SJin Yao 1934e4064776SJin Yao bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1935e4064776SJin Yao 1936e4064776SJin Yao pr_warning("WARNING: event '%s' not valid (bits %s of config " 1937e4064776SJin Yao "'%llx' not supported by kernel)!\n", 1938e4064776SJin Yao name ?: "N/A", buf, config); 1939e4064776SJin Yao } 1940c5a26ea4SJin Yao 1941c5a26ea4SJin Yao bool perf_pmu__has_hybrid(void) 1942c5a26ea4SJin Yao { 1943c5a26ea4SJin Yao if (!hybrid_scanned) { 1944c5a26ea4SJin Yao hybrid_scanned = true; 1945c5a26ea4SJin Yao perf_pmu__scan(NULL); 1946c5a26ea4SJin Yao } 1947c5a26ea4SJin Yao 1948c5a26ea4SJin Yao return !list_empty(&perf_pmu__hybrid_pmus); 1949c5a26ea4SJin Yao } 1950c47a5599SJin Yao 1951c47a5599SJin Yao int perf_pmu__match(char *pattern, char *name, char *tok) 1952c47a5599SJin Yao { 195313d60ba0SKan Liang if (!name) 195413d60ba0SKan Liang return -1; 195513d60ba0SKan Liang 1956c47a5599SJin Yao if (fnmatch(pattern, name, 0)) 1957c47a5599SJin Yao return -1; 1958c47a5599SJin Yao 1959c47a5599SJin Yao if (tok && !perf_pmu__valid_suffix(name, tok)) 1960c47a5599SJin Yao return -1; 1961c47a5599SJin Yao 1962c47a5599SJin Yao return 0; 1963c47a5599SJin Yao } 19641d3351e6SJin Yao 19651d3351e6SJin Yao int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus, 19661d3351e6SJin Yao struct perf_cpu_map **mcpus_ptr, 19671d3351e6SJin Yao struct perf_cpu_map **ucpus_ptr) 19681d3351e6SJin Yao { 19691d3351e6SJin Yao struct perf_cpu_map *pmu_cpus = pmu->cpus; 19701d3351e6SJin Yao struct perf_cpu_map *matched_cpus, *unmatched_cpus; 19711d3351e6SJin Yao int matched_nr = 0, unmatched_nr = 0; 19721d3351e6SJin Yao 19731d3351e6SJin Yao matched_cpus = perf_cpu_map__default_new(); 19741d3351e6SJin Yao if (!matched_cpus) 19751d3351e6SJin Yao return -1; 19761d3351e6SJin Yao 19771d3351e6SJin Yao unmatched_cpus = perf_cpu_map__default_new(); 19781d3351e6SJin Yao if (!unmatched_cpus) { 19791d3351e6SJin Yao perf_cpu_map__put(matched_cpus); 19801d3351e6SJin Yao return -1; 19811d3351e6SJin Yao } 19821d3351e6SJin Yao 19831d3351e6SJin Yao for (int i = 0; i < cpus->nr; i++) { 19841d3351e6SJin Yao int cpu; 19851d3351e6SJin Yao 19861d3351e6SJin Yao cpu = perf_cpu_map__idx(pmu_cpus, cpus->map[i]); 19871d3351e6SJin Yao if (cpu == -1) 19881d3351e6SJin Yao unmatched_cpus->map[unmatched_nr++] = cpus->map[i]; 19891d3351e6SJin Yao else 19901d3351e6SJin Yao matched_cpus->map[matched_nr++] = cpus->map[i]; 19911d3351e6SJin Yao } 19921d3351e6SJin Yao 19931d3351e6SJin Yao unmatched_cpus->nr = unmatched_nr; 19941d3351e6SJin Yao matched_cpus->nr = matched_nr; 19951d3351e6SJin Yao *mcpus_ptr = matched_cpus; 19961d3351e6SJin Yao *ucpus_ptr = unmatched_cpus; 19971d3351e6SJin Yao return 0; 19981d3351e6SJin Yao } 1999