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 34*fe13d43dSIan Rogers /** 35*fe13d43dSIan Rogers * struct perf_pmu_format - Values from a format file read from 36*fe13d43dSIan Rogers * <sysfs>/devices/cpu/format/ held in struct perf_pmu. 37*fe13d43dSIan Rogers * 38*fe13d43dSIan Rogers * For example, the contents of <sysfs>/devices/cpu/format/event may be 39*fe13d43dSIan Rogers * "config:0-7" and will be represented here as name="event", 40*fe13d43dSIan Rogers * value=PERF_PMU_FORMAT_VALUE_CONFIG and bits 0 to 7 will be set. 41*fe13d43dSIan Rogers */ 42ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 43*fe13d43dSIan Rogers /** @name: The modifier/file name. */ 44ab1bf653SArnaldo Carvalho de Melo char *name; 45*fe13d43dSIan Rogers /** 46*fe13d43dSIan Rogers * @value : Which config value the format relates to. Supported values 47*fe13d43dSIan Rogers * are from PERF_PMU_FORMAT_VALUE_CONFIG to 48*fe13d43dSIan Rogers * PERF_PMU_FORMAT_VALUE_CONFIG_END. 49*fe13d43dSIan Rogers */ 50ab1bf653SArnaldo Carvalho de Melo int value; 51*fe13d43dSIan Rogers /** @bits: Which config bits are set by this format value. */ 52ab1bf653SArnaldo Carvalho de Melo DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 53*fe13d43dSIan Rogers /** @list: Element on list within struct perf_pmu. */ 54ab1bf653SArnaldo Carvalho de Melo struct list_head list; 55ab1bf653SArnaldo Carvalho de Melo }; 56ab1bf653SArnaldo Carvalho de Melo 57cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name); 58cd82a32eSJiri Olsa extern FILE *perf_pmu_in; 59cd82a32eSJiri Olsa 60cd82a32eSJiri Olsa static LIST_HEAD(pmus); 61c5a26ea4SJin Yao static bool hybrid_scanned; 62cd82a32eSJiri Olsa 63cd82a32eSJiri Olsa /* 64cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 65cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 66cd82a32eSJiri Olsa */ 67cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head) 68cd82a32eSJiri Olsa { 69cd82a32eSJiri Olsa struct dirent *evt_ent; 70cd82a32eSJiri Olsa DIR *format_dir; 71cd82a32eSJiri Olsa int ret = 0; 72cd82a32eSJiri Olsa 73cd82a32eSJiri Olsa format_dir = opendir(dir); 74cd82a32eSJiri Olsa if (!format_dir) 75cd82a32eSJiri Olsa return -EINVAL; 76cd82a32eSJiri Olsa 77cd82a32eSJiri Olsa while (!ret && (evt_ent = readdir(format_dir))) { 78cd82a32eSJiri Olsa char path[PATH_MAX]; 79cd82a32eSJiri Olsa char *name = evt_ent->d_name; 80cd82a32eSJiri Olsa FILE *file; 81cd82a32eSJiri Olsa 82cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 83cd82a32eSJiri Olsa continue; 84cd82a32eSJiri Olsa 85cd82a32eSJiri Olsa snprintf(path, PATH_MAX, "%s/%s", dir, name); 86cd82a32eSJiri Olsa 87cd82a32eSJiri Olsa ret = -EINVAL; 88cd82a32eSJiri Olsa file = fopen(path, "r"); 89cd82a32eSJiri Olsa if (!file) 90cd82a32eSJiri Olsa break; 91cd82a32eSJiri Olsa 92cd82a32eSJiri Olsa perf_pmu_in = file; 93cd82a32eSJiri Olsa ret = perf_pmu_parse(head, name); 94cd82a32eSJiri Olsa fclose(file); 95cd82a32eSJiri Olsa } 96cd82a32eSJiri Olsa 97cd82a32eSJiri Olsa closedir(format_dir); 98cd82a32eSJiri Olsa return ret; 99cd82a32eSJiri Olsa } 100cd82a32eSJiri Olsa 101cd82a32eSJiri Olsa /* 102cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 103cd82a32eSJiri Olsa * located at: 104cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 105cd82a32eSJiri Olsa */ 106b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format) 107cd82a32eSJiri Olsa { 108cd82a32eSJiri Olsa char path[PATH_MAX]; 109cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 110cd82a32eSJiri Olsa 111cd82a32eSJiri Olsa if (!sysfs) 112cd82a32eSJiri Olsa return -1; 113cd82a32eSJiri Olsa 114cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 11550a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); 116cd82a32eSJiri Olsa 117d9664582SAndi Kleen if (!file_available(path)) 118d9664582SAndi Kleen return 0; 119cd82a32eSJiri Olsa 120cff7f956SJiri Olsa if (perf_pmu__format_parse(path, format)) 121cd82a32eSJiri Olsa return -1; 122cd82a32eSJiri Olsa 123cd82a32eSJiri Olsa return 0; 124cd82a32eSJiri Olsa } 125cd82a32eSJiri Olsa 126a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval) 127d02fc6bcSAndi Kleen { 128d02fc6bcSAndi Kleen char *lc; 129d02fc6bcSAndi Kleen int ret = 0; 130d02fc6bcSAndi Kleen 131d02fc6bcSAndi Kleen /* 132d02fc6bcSAndi Kleen * save current locale 133d02fc6bcSAndi Kleen */ 134d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 135d02fc6bcSAndi Kleen 136d02fc6bcSAndi Kleen /* 137d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 138d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 139d02fc6bcSAndi Kleen * call below. 140d02fc6bcSAndi Kleen */ 141d02fc6bcSAndi Kleen lc = strdup(lc); 142d02fc6bcSAndi Kleen if (!lc) { 143d02fc6bcSAndi Kleen ret = -ENOMEM; 144d02fc6bcSAndi Kleen goto out; 145d02fc6bcSAndi Kleen } 146d02fc6bcSAndi Kleen 147d02fc6bcSAndi Kleen /* 148d02fc6bcSAndi Kleen * force to C locale to ensure kernel 149d02fc6bcSAndi Kleen * scale string is converted correctly. 150d02fc6bcSAndi Kleen * kernel uses default C locale. 151d02fc6bcSAndi Kleen */ 152d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 153d02fc6bcSAndi Kleen 154d02fc6bcSAndi Kleen *sval = strtod(scale, end); 155d02fc6bcSAndi Kleen 156d02fc6bcSAndi Kleen out: 157d02fc6bcSAndi Kleen /* restore locale */ 158d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 159d02fc6bcSAndi Kleen free(lc); 160d02fc6bcSAndi Kleen return ret; 161d02fc6bcSAndi Kleen } 162d02fc6bcSAndi Kleen 163410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) 164410136f5SStephane Eranian { 165410136f5SStephane Eranian struct stat st; 166410136f5SStephane Eranian ssize_t sret; 167410136f5SStephane Eranian char scale[128]; 168410136f5SStephane Eranian int fd, ret = -1; 169410136f5SStephane Eranian char path[PATH_MAX]; 170410136f5SStephane Eranian 17111a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name); 172410136f5SStephane Eranian 173410136f5SStephane Eranian fd = open(path, O_RDONLY); 174410136f5SStephane Eranian if (fd == -1) 175410136f5SStephane Eranian return -1; 176410136f5SStephane Eranian 177410136f5SStephane Eranian if (fstat(fd, &st) < 0) 178410136f5SStephane Eranian goto error; 179410136f5SStephane Eranian 180410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 181410136f5SStephane Eranian if (sret < 0) 182410136f5SStephane Eranian goto error; 183410136f5SStephane Eranian 1849ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 1859ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 1869ecae065SMadhavan Srinivasan else 187410136f5SStephane Eranian scale[sret] = '\0'; 1889ecae065SMadhavan Srinivasan 189a55ab7c4SJin Yao ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); 190410136f5SStephane Eranian error: 191410136f5SStephane Eranian close(fd); 192410136f5SStephane Eranian return ret; 193410136f5SStephane Eranian } 194410136f5SStephane Eranian 195410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) 196410136f5SStephane Eranian { 197410136f5SStephane Eranian char path[PATH_MAX]; 198410136f5SStephane Eranian ssize_t sret; 199410136f5SStephane Eranian int fd; 200410136f5SStephane Eranian 20111a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name); 202410136f5SStephane Eranian 203410136f5SStephane Eranian fd = open(path, O_RDONLY); 204410136f5SStephane Eranian if (fd == -1) 205410136f5SStephane Eranian return -1; 206410136f5SStephane Eranian 207410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 208410136f5SStephane Eranian if (sret < 0) 209410136f5SStephane Eranian goto error; 210410136f5SStephane Eranian 211410136f5SStephane Eranian close(fd); 212410136f5SStephane Eranian 2139ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 2149ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 2159ecae065SMadhavan Srinivasan else 216410136f5SStephane Eranian alias->unit[sret] = '\0'; 217410136f5SStephane Eranian 218410136f5SStephane Eranian return 0; 219410136f5SStephane Eranian error: 220410136f5SStephane Eranian close(fd); 221410136f5SStephane Eranian alias->unit[0] = '\0'; 222410136f5SStephane Eranian return -1; 223410136f5SStephane Eranian } 224410136f5SStephane Eranian 225044330c1SMatt Fleming static int 226044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) 227044330c1SMatt Fleming { 228044330c1SMatt Fleming char path[PATH_MAX]; 229044330c1SMatt Fleming int fd; 230044330c1SMatt Fleming 23111a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); 232044330c1SMatt Fleming 233044330c1SMatt Fleming fd = open(path, O_RDONLY); 234044330c1SMatt Fleming if (fd == -1) 235044330c1SMatt Fleming return -1; 236044330c1SMatt Fleming 237044330c1SMatt Fleming close(fd); 238044330c1SMatt Fleming 239044330c1SMatt Fleming alias->per_pkg = true; 240044330c1SMatt Fleming return 0; 241044330c1SMatt Fleming } 242044330c1SMatt Fleming 2431d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 2441d9e446bSJiri Olsa char *dir, char *name) 2451d9e446bSJiri Olsa { 2461d9e446bSJiri Olsa char path[PATH_MAX]; 2471d9e446bSJiri Olsa int fd; 2481d9e446bSJiri Olsa 24911a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); 2501d9e446bSJiri Olsa 2511d9e446bSJiri Olsa fd = open(path, O_RDONLY); 2521d9e446bSJiri Olsa if (fd == -1) 2531d9e446bSJiri Olsa return -1; 2541d9e446bSJiri Olsa 2551d9e446bSJiri Olsa alias->snapshot = true; 2561d9e446bSJiri Olsa close(fd); 2571d9e446bSJiri Olsa return 0; 2581d9e446bSJiri Olsa } 2591d9e446bSJiri Olsa 2606dde6429SThomas Richter static void perf_pmu_assign_str(char *name, const char *field, char **old_str, 2616dde6429SThomas Richter char **new_str) 2626dde6429SThomas Richter { 2636dde6429SThomas Richter if (!*old_str) 2646dde6429SThomas Richter goto set_new; 2656dde6429SThomas Richter 2666dde6429SThomas Richter if (*new_str) { /* Have new string, check with old */ 2676dde6429SThomas Richter if (strcasecmp(*old_str, *new_str)) 2686dde6429SThomas Richter pr_debug("alias %s differs in field '%s'\n", 2696dde6429SThomas Richter name, field); 2706dde6429SThomas Richter zfree(old_str); 2716dde6429SThomas Richter } else /* Nothing new --> keep old string */ 2726dde6429SThomas Richter return; 2736dde6429SThomas Richter set_new: 2746dde6429SThomas Richter *old_str = *new_str; 2756dde6429SThomas Richter *new_str = NULL; 2766dde6429SThomas Richter } 2776dde6429SThomas Richter 2786dde6429SThomas Richter static void perf_pmu_update_alias(struct perf_pmu_alias *old, 2796dde6429SThomas Richter struct perf_pmu_alias *newalias) 2806dde6429SThomas Richter { 2816dde6429SThomas Richter perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); 2826dde6429SThomas Richter perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, 2836dde6429SThomas Richter &newalias->long_desc); 2846dde6429SThomas Richter perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); 2856dde6429SThomas Richter perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr, 2866dde6429SThomas Richter &newalias->metric_expr); 2876dde6429SThomas Richter perf_pmu_assign_str(old->name, "metric_name", &old->metric_name, 2886dde6429SThomas Richter &newalias->metric_name); 2896dde6429SThomas Richter perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); 2906dde6429SThomas Richter old->scale = newalias->scale; 2916dde6429SThomas Richter old->per_pkg = newalias->per_pkg; 2926dde6429SThomas Richter old->snapshot = newalias->snapshot; 2936dde6429SThomas Richter memcpy(old->unit, newalias->unit, sizeof(old->unit)); 2946dde6429SThomas Richter } 2956dde6429SThomas Richter 2966dde6429SThomas Richter /* Delete an alias entry. */ 29722fe5a25SNamhyung Kim void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 2986dde6429SThomas Richter { 2996dde6429SThomas Richter zfree(&newalias->name); 3006dde6429SThomas Richter zfree(&newalias->desc); 3016dde6429SThomas Richter zfree(&newalias->long_desc); 3026dde6429SThomas Richter zfree(&newalias->topic); 3036dde6429SThomas Richter zfree(&newalias->str); 3046dde6429SThomas Richter zfree(&newalias->metric_expr); 3056dde6429SThomas Richter zfree(&newalias->metric_name); 30632705de7SJin Yao zfree(&newalias->pmu_name); 3076dde6429SThomas Richter parse_events_terms__purge(&newalias->terms); 3086dde6429SThomas Richter free(newalias); 3096dde6429SThomas Richter } 3106dde6429SThomas Richter 3116dde6429SThomas Richter /* Merge an alias, search in alias list. If this name is already 3126dde6429SThomas Richter * present merge both of them to combine all information. 3136dde6429SThomas Richter */ 3146dde6429SThomas Richter static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, 3156dde6429SThomas Richter struct list_head *alist) 3166dde6429SThomas Richter { 3176dde6429SThomas Richter struct perf_pmu_alias *a; 3186dde6429SThomas Richter 3196dde6429SThomas Richter list_for_each_entry(a, alist, list) { 3206dde6429SThomas Richter if (!strcasecmp(newalias->name, a->name)) { 32132705de7SJin Yao if (newalias->pmu_name && a->pmu_name && 32232705de7SJin Yao !strcasecmp(newalias->pmu_name, a->pmu_name)) { 32332705de7SJin Yao continue; 32432705de7SJin Yao } 3256dde6429SThomas Richter perf_pmu_update_alias(a, newalias); 3266dde6429SThomas Richter perf_pmu_free_alias(newalias); 3276dde6429SThomas Richter return true; 3286dde6429SThomas Richter } 3296dde6429SThomas Richter } 3306dde6429SThomas Richter return false; 3316dde6429SThomas Richter } 3326dde6429SThomas Richter 33370c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, 33447f572aaSIan Rogers char *desc, char *val, const struct pmu_event *pe) 335a6146d50SZheng Yan { 3360c24d6fbSThomas Richter struct parse_events_term *term; 3375c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 338a6146d50SZheng Yan int ret; 339fedb2b51SAndi Kleen int num; 3400c24d6fbSThomas Richter char newval[256]; 341eab35953SJin Yao char *long_desc = NULL, *topic = NULL, *unit = NULL, *perpkg = NULL, 34232705de7SJin Yao *metric_expr = NULL, *metric_name = NULL, *deprecated = NULL, 34332705de7SJin Yao *pmu_name = NULL; 344eab35953SJin Yao 345eab35953SJin Yao if (pe) { 346eab35953SJin Yao long_desc = (char *)pe->long_desc; 347eab35953SJin Yao topic = (char *)pe->topic; 348eab35953SJin Yao unit = (char *)pe->unit; 349eab35953SJin Yao perpkg = (char *)pe->perpkg; 350eab35953SJin Yao metric_expr = (char *)pe->metric_expr; 351eab35953SJin Yao metric_name = (char *)pe->metric_name; 352eab35953SJin Yao deprecated = (char *)pe->deprecated; 35332705de7SJin Yao pmu_name = (char *)pe->pmu; 354eab35953SJin Yao } 355a6146d50SZheng Yan 356a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 357a6146d50SZheng Yan if (!alias) 358a6146d50SZheng Yan return -ENOMEM; 359a6146d50SZheng Yan 360a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 361410136f5SStephane Eranian alias->scale = 1.0; 362410136f5SStephane Eranian alias->unit[0] = '\0'; 363044330c1SMatt Fleming alias->per_pkg = false; 36484530920SStephane Eranian alias->snapshot = false; 365a7f6c8c8SJin Yao alias->deprecated = false; 366410136f5SStephane Eranian 36770c646e0SSukadev Bhattiprolu ret = parse_events_terms(&alias->terms, val); 368a6146d50SZheng Yan if (ret) { 36970c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 370a6146d50SZheng Yan free(alias); 371a6146d50SZheng Yan return ret; 372a6146d50SZheng Yan } 373a6146d50SZheng Yan 3740c24d6fbSThomas Richter /* Scan event and remove leading zeroes, spaces, newlines, some 3750c24d6fbSThomas Richter * platforms have terms specified as 3760c24d6fbSThomas Richter * event=0x0091 (read from files ../<PMU>/events/<FILE> 3770c24d6fbSThomas Richter * and terms specified as event=0x91 (read from JSON files). 3780c24d6fbSThomas Richter * 3790c24d6fbSThomas Richter * Rebuild string to make alias->str member comparable. 3800c24d6fbSThomas Richter */ 3810c24d6fbSThomas Richter memset(newval, 0, sizeof(newval)); 3820c24d6fbSThomas Richter ret = 0; 3830c24d6fbSThomas Richter list_for_each_entry(term, &alias->terms, list) { 3840c24d6fbSThomas Richter if (ret) 3850c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3860c24d6fbSThomas Richter ","); 3870c24d6fbSThomas Richter if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 3880c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3890c24d6fbSThomas Richter "%s=%#x", term->config, term->val.num); 3900c24d6fbSThomas Richter else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 3910c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3920c24d6fbSThomas Richter "%s=%s", term->config, term->val.str); 3930c24d6fbSThomas Richter } 3940c24d6fbSThomas Richter 395a6146d50SZheng Yan alias->name = strdup(name); 39670c646e0SSukadev Bhattiprolu if (dir) { 397410136f5SStephane Eranian /* 398410136f5SStephane Eranian * load unit name and scale if available 399410136f5SStephane Eranian */ 400410136f5SStephane Eranian perf_pmu__parse_unit(alias, dir, name); 401410136f5SStephane Eranian perf_pmu__parse_scale(alias, dir, name); 402044330c1SMatt Fleming perf_pmu__parse_per_pkg(alias, dir, name); 4031d9e446bSJiri Olsa perf_pmu__parse_snapshot(alias, dir, name); 40470c646e0SSukadev Bhattiprolu } 405410136f5SStephane Eranian 40600636c3bSAndi Kleen alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL; 40796284814SAndi Kleen alias->metric_name = metric_name ? strdup(metric_name): NULL; 40808e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 409c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 410c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 411dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 412fedb2b51SAndi Kleen if (unit) { 413a55ab7c4SJin Yao if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0) 414fedb2b51SAndi Kleen return -1; 415fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 416fedb2b51SAndi Kleen } 417fedb2b51SAndi Kleen alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1; 4180c24d6fbSThomas Richter alias->str = strdup(newval); 41932705de7SJin Yao alias->pmu_name = pmu_name ? strdup(pmu_name) : NULL; 420f2361024SAndi Kleen 421a7f6c8c8SJin Yao if (deprecated) 422a7f6c8c8SJin Yao alias->deprecated = true; 423a7f6c8c8SJin Yao 4246dde6429SThomas Richter if (!perf_pmu_merge_alias(alias, list)) 425a6146d50SZheng Yan list_add_tail(&alias->list, list); 426410136f5SStephane Eranian 427a6146d50SZheng Yan return 0; 428a6146d50SZheng Yan } 429a6146d50SZheng Yan 43070c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) 43170c646e0SSukadev Bhattiprolu { 43270c646e0SSukadev Bhattiprolu char buf[256]; 43370c646e0SSukadev Bhattiprolu int ret; 43470c646e0SSukadev Bhattiprolu 43570c646e0SSukadev Bhattiprolu ret = fread(buf, 1, sizeof(buf), file); 43670c646e0SSukadev Bhattiprolu if (ret == 0) 43770c646e0SSukadev Bhattiprolu return -EINVAL; 43870c646e0SSukadev Bhattiprolu 43970c646e0SSukadev Bhattiprolu buf[ret] = 0; 44070c646e0SSukadev Bhattiprolu 441ea23ac73SThomas Richter /* Remove trailing newline from sysfs file */ 44213c230abSArnaldo Carvalho de Melo strim(buf); 443ea23ac73SThomas Richter 444eab35953SJin Yao return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL); 44570c646e0SSukadev Bhattiprolu } 44670c646e0SSukadev Bhattiprolu 44746441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 44846441bdcSMatt Fleming { 44946441bdcSMatt Fleming size_t len; 45046441bdcSMatt Fleming 45146441bdcSMatt Fleming len = strlen(name); 45246441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 45346441bdcSMatt Fleming return true; 45446441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 45546441bdcSMatt Fleming return true; 456044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 457044330c1SMatt Fleming return true; 4581d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 4591d9e446bSJiri Olsa return true; 46046441bdcSMatt Fleming 46146441bdcSMatt Fleming return false; 46246441bdcSMatt Fleming } 46346441bdcSMatt Fleming 464a6146d50SZheng Yan /* 465a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 466a6146d50SZheng Yan * specified in 'dir' parameter. 467a6146d50SZheng Yan */ 468a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head) 469a6146d50SZheng Yan { 470a6146d50SZheng Yan struct dirent *evt_ent; 471a6146d50SZheng Yan DIR *event_dir; 472a6146d50SZheng Yan 473a6146d50SZheng Yan event_dir = opendir(dir); 474a6146d50SZheng Yan if (!event_dir) 475a6146d50SZheng Yan return -EINVAL; 476a6146d50SZheng Yan 477940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 478a6146d50SZheng Yan char path[PATH_MAX]; 479a6146d50SZheng Yan char *name = evt_ent->d_name; 480a6146d50SZheng Yan FILE *file; 481a6146d50SZheng Yan 482a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 483a6146d50SZheng Yan continue; 484a6146d50SZheng Yan 485410136f5SStephane Eranian /* 48646441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 487410136f5SStephane Eranian */ 48846441bdcSMatt Fleming if (pmu_alias_info_file(name)) 489410136f5SStephane Eranian continue; 490410136f5SStephane Eranian 49177f18153SJiri Olsa scnprintf(path, PATH_MAX, "%s/%s", dir, name); 492a6146d50SZheng Yan 493a6146d50SZheng Yan file = fopen(path, "r"); 494940db6dcSAndi Kleen if (!file) { 495940db6dcSAndi Kleen pr_debug("Cannot open %s\n", path); 496940db6dcSAndi Kleen continue; 497940db6dcSAndi Kleen } 498410136f5SStephane Eranian 499940db6dcSAndi Kleen if (perf_pmu__new_alias(head, dir, name, file) < 0) 500940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 501a6146d50SZheng Yan fclose(file); 502a6146d50SZheng Yan } 503a6146d50SZheng Yan 504a6146d50SZheng Yan closedir(event_dir); 505940db6dcSAndi Kleen return 0; 506a6146d50SZheng Yan } 507a6146d50SZheng Yan 508a6146d50SZheng Yan /* 509a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 510a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 511a6146d50SZheng Yan */ 512b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head) 513a6146d50SZheng Yan { 514a6146d50SZheng Yan char path[PATH_MAX]; 515cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 516a6146d50SZheng Yan 517a6146d50SZheng Yan if (!sysfs) 518a6146d50SZheng Yan return -1; 519a6146d50SZheng Yan 520a6146d50SZheng Yan snprintf(path, PATH_MAX, 521a6146d50SZheng Yan "%s/bus/event_source/devices/%s/events", sysfs, name); 522a6146d50SZheng Yan 523d9664582SAndi Kleen if (!file_available(path)) 524d9664582SAndi Kleen return 0; 525a6146d50SZheng Yan 526a6146d50SZheng Yan if (pmu_aliases_parse(path, head)) 527a6146d50SZheng Yan return -1; 528a6146d50SZheng Yan 529a6146d50SZheng Yan return 0; 530a6146d50SZheng Yan } 531a6146d50SZheng Yan 5325c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 533a6146d50SZheng Yan struct list_head *terms) 534a6146d50SZheng Yan { 5357c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 536a6146d50SZheng Yan LIST_HEAD(list); 537a6146d50SZheng Yan int ret; 538a6146d50SZheng Yan 539a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 5407c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 541a6146d50SZheng Yan if (ret) { 542682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 543a6146d50SZheng Yan return ret; 544a6146d50SZheng Yan } 545c2f1ceadSAndi Kleen /* 546c2f1ceadSAndi Kleen * Weak terms don't override command line options, 547c2f1ceadSAndi Kleen * which we don't want for implicit terms in aliases. 548c2f1ceadSAndi Kleen */ 549c2f1ceadSAndi Kleen cloned->weak = true; 5507c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 551a6146d50SZheng Yan } 552a6146d50SZheng Yan list_splice(&list, terms); 553a6146d50SZheng Yan return 0; 554a6146d50SZheng Yan } 555a6146d50SZheng Yan 556cd82a32eSJiri Olsa /* 557cd82a32eSJiri Olsa * Reading/parsing the default pmu type value, which should be 558cd82a32eSJiri Olsa * located at: 559cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 560cd82a32eSJiri Olsa */ 561b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type) 562cd82a32eSJiri Olsa { 563cd82a32eSJiri Olsa char path[PATH_MAX]; 564cd82a32eSJiri Olsa FILE *file; 565cd82a32eSJiri Olsa int ret = 0; 566cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 567cd82a32eSJiri Olsa 568cd82a32eSJiri Olsa if (!sysfs) 569cd82a32eSJiri Olsa return -1; 570cd82a32eSJiri Olsa 571cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 57250a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); 573cd82a32eSJiri Olsa 574d9664582SAndi Kleen if (access(path, R_OK) < 0) 575cd82a32eSJiri Olsa return -1; 576cd82a32eSJiri Olsa 577cd82a32eSJiri Olsa file = fopen(path, "r"); 578cd82a32eSJiri Olsa if (!file) 579cd82a32eSJiri Olsa return -EINVAL; 580cd82a32eSJiri Olsa 581cd82a32eSJiri Olsa if (1 != fscanf(file, "%u", type)) 582cd82a32eSJiri Olsa ret = -1; 583cd82a32eSJiri Olsa 584cd82a32eSJiri Olsa fclose(file); 585cd82a32eSJiri Olsa return ret; 586cd82a32eSJiri Olsa } 587cd82a32eSJiri Olsa 58850a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */ 58950a9667cSRobert Richter static void pmu_read_sysfs(void) 59050a9667cSRobert Richter { 59150a9667cSRobert Richter char path[PATH_MAX]; 59250a9667cSRobert Richter DIR *dir; 59350a9667cSRobert Richter struct dirent *dent; 594cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 59550a9667cSRobert Richter 59650a9667cSRobert Richter if (!sysfs) 59750a9667cSRobert Richter return; 59850a9667cSRobert Richter 59950a9667cSRobert Richter snprintf(path, PATH_MAX, 60050a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 60150a9667cSRobert Richter 60250a9667cSRobert Richter dir = opendir(path); 60350a9667cSRobert Richter if (!dir) 60450a9667cSRobert Richter return; 60550a9667cSRobert Richter 60650a9667cSRobert Richter while ((dent = readdir(dir))) { 60750a9667cSRobert Richter if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 60850a9667cSRobert Richter continue; 60950a9667cSRobert Richter /* add to static LIST_HEAD(pmus): */ 61050a9667cSRobert Richter perf_pmu__find(dent->d_name); 61150a9667cSRobert Richter } 61250a9667cSRobert Richter 61350a9667cSRobert Richter closedir(dir); 61450a9667cSRobert Richter } 61550a9667cSRobert Richter 616f854839bSJiri Olsa static struct perf_cpu_map *__pmu_cpumask(const char *path) 61766ec1191SMark Rutland { 61866ec1191SMark Rutland FILE *file; 619f854839bSJiri Olsa struct perf_cpu_map *cpus; 62066ec1191SMark Rutland 62166ec1191SMark Rutland file = fopen(path, "r"); 62266ec1191SMark Rutland if (!file) 62366ec1191SMark Rutland return NULL; 62466ec1191SMark Rutland 6259c3516d1SJiri Olsa cpus = perf_cpu_map__read(file); 62666ec1191SMark Rutland fclose(file); 62766ec1191SMark Rutland return cpus; 62866ec1191SMark Rutland } 62966ec1191SMark Rutland 63066ec1191SMark Rutland /* 63166ec1191SMark Rutland * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 63266ec1191SMark Rutland * may have a "cpus" file. 63366ec1191SMark Rutland */ 63451d54847SJohn Garry #define SYS_TEMPLATE_ID "./bus/event_source/devices/%s/identifier" 63566ec1191SMark Rutland #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask" 63666ec1191SMark Rutland 637f854839bSJiri Olsa static struct perf_cpu_map *pmu_cpumask(const char *name) 6387ae92e74SYan, Zheng { 6397ae92e74SYan, Zheng char path[PATH_MAX]; 640f854839bSJiri Olsa struct perf_cpu_map *cpus; 641cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 6427e3fcffeSMark Rutland const char *templates[] = { 64366ec1191SMark Rutland CPUS_TEMPLATE_UNCORE, 64466ec1191SMark Rutland CPUS_TEMPLATE_CPU, 6457e3fcffeSMark Rutland NULL 6467e3fcffeSMark Rutland }; 6477e3fcffeSMark Rutland const char **template; 6487ae92e74SYan, Zheng 6497ae92e74SYan, Zheng if (!sysfs) 6507ae92e74SYan, Zheng return NULL; 6517ae92e74SYan, Zheng 6527e3fcffeSMark Rutland for (template = templates; *template; template++) { 6537e3fcffeSMark Rutland snprintf(path, PATH_MAX, *template, sysfs, name); 65466ec1191SMark Rutland cpus = __pmu_cpumask(path); 65566ec1191SMark Rutland if (cpus) 65666ec1191SMark Rutland return cpus; 6577e3fcffeSMark Rutland } 6587ae92e74SYan, Zheng 6597ae92e74SYan, Zheng return NULL; 66066ec1191SMark Rutland } 6617ae92e74SYan, Zheng 66266ec1191SMark Rutland static bool pmu_is_uncore(const char *name) 66366ec1191SMark Rutland { 66466ec1191SMark Rutland char path[PATH_MAX]; 665d9664582SAndi Kleen const char *sysfs; 6667ae92e74SYan, Zheng 66744462430SJin Yao if (perf_pmu__hybrid_mounted(name)) 66844462430SJin Yao return false; 66944462430SJin Yao 670d9664582SAndi Kleen sysfs = sysfs__mountpoint(); 67166ec1191SMark Rutland snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name); 672d9664582SAndi Kleen return file_available(path); 6737ae92e74SYan, Zheng } 6747ae92e74SYan, Zheng 67551d54847SJohn Garry static char *pmu_id(const char *name) 67651d54847SJohn Garry { 67751d54847SJohn Garry char path[PATH_MAX], *str; 67851d54847SJohn Garry size_t len; 67951d54847SJohn Garry 68051d54847SJohn Garry snprintf(path, PATH_MAX, SYS_TEMPLATE_ID, name); 68151d54847SJohn Garry 68251d54847SJohn Garry if (sysfs__read_str(path, &str, &len) < 0) 68351d54847SJohn Garry return NULL; 68451d54847SJohn Garry 68551d54847SJohn Garry str[len - 1] = 0; /* remove line feed */ 68651d54847SJohn Garry 68751d54847SJohn Garry return str; 68851d54847SJohn Garry } 68951d54847SJohn Garry 690933f82ffSSukadev Bhattiprolu /* 69114b22ae0SGanapatrao Kulkarni * PMU CORE devices have different name other than cpu in sysfs on some 692292c34c1SKan Liang * platforms. 693292c34c1SKan Liang * Looking for possible sysfs files to identify the arm core device. 69414b22ae0SGanapatrao Kulkarni */ 695292c34c1SKan Liang static int is_arm_pmu_core(const char *name) 69614b22ae0SGanapatrao Kulkarni { 69714b22ae0SGanapatrao Kulkarni char path[PATH_MAX]; 69814b22ae0SGanapatrao Kulkarni const char *sysfs = sysfs__mountpoint(); 69914b22ae0SGanapatrao Kulkarni 70014b22ae0SGanapatrao Kulkarni if (!sysfs) 70114b22ae0SGanapatrao Kulkarni return 0; 70214b22ae0SGanapatrao Kulkarni 70314b22ae0SGanapatrao Kulkarni /* Look for cpu sysfs (specific to arm) */ 70414b22ae0SGanapatrao Kulkarni scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus", 70514b22ae0SGanapatrao Kulkarni sysfs, name); 706d9664582SAndi Kleen return file_available(path); 70714b22ae0SGanapatrao Kulkarni } 70814b22ae0SGanapatrao Kulkarni 70929be2fe0SIan Rogers char *perf_pmu__getcpuid(struct perf_pmu *pmu) 710d77ade9fSAndi Kleen { 711d77ade9fSAndi Kleen char *cpuid; 712d77ade9fSAndi Kleen static bool printed; 713d77ade9fSAndi Kleen 714d77ade9fSAndi Kleen cpuid = getenv("PERF_CPUID"); 715d77ade9fSAndi Kleen if (cpuid) 716d77ade9fSAndi Kleen cpuid = strdup(cpuid); 717d77ade9fSAndi Kleen if (!cpuid) 71854e32dc0SGanapatrao Kulkarni cpuid = get_cpuid_str(pmu); 719d77ade9fSAndi Kleen if (!cpuid) 720d77ade9fSAndi Kleen return NULL; 721d77ade9fSAndi Kleen 722d77ade9fSAndi Kleen if (!printed) { 723d77ade9fSAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 724d77ade9fSAndi Kleen printed = true; 725d77ade9fSAndi Kleen } 726d77ade9fSAndi Kleen return cpuid; 727d77ade9fSAndi Kleen } 728d77ade9fSAndi Kleen 7291ba3752aSIan Rogers __weak const struct pmu_events_table *pmu_events_table__find(void) 730e126bef5SJohn Garry { 731eeac7730SIan Rogers return perf_pmu__find_table(NULL); 732e126bef5SJohn Garry } 733e126bef5SJohn Garry 734c07d5c92SJohn Garry /* 735c07d5c92SJohn Garry * Suffix must be in form tok_{digits}, or tok{digits}, or same as pmu_name 736c07d5c92SJohn Garry * to be valid. 737c07d5c92SJohn Garry */ 738c07d5c92SJohn Garry static bool perf_pmu__valid_suffix(const char *pmu_name, char *tok) 739c47a5599SJin Yao { 740c07d5c92SJohn Garry const char *p; 741c47a5599SJin Yao 742c47a5599SJin Yao if (strncmp(pmu_name, tok, strlen(tok))) 743c47a5599SJin Yao return false; 744c47a5599SJin Yao 745c47a5599SJin Yao p = pmu_name + strlen(tok); 746c47a5599SJin Yao if (*p == 0) 747c47a5599SJin Yao return true; 748c47a5599SJin Yao 749c07d5c92SJohn Garry if (*p == '_') 750c47a5599SJin Yao ++p; 751c07d5c92SJohn Garry 752c07d5c92SJohn Garry /* Ensure we end in a number */ 753c07d5c92SJohn Garry while (1) { 754c07d5c92SJohn Garry if (!isdigit(*p)) 755c47a5599SJin Yao return false; 756c07d5c92SJohn Garry if (*(++p) == 0) 757c07d5c92SJohn Garry break; 758c07d5c92SJohn Garry } 759c47a5599SJin Yao 760c47a5599SJin Yao return true; 761c47a5599SJin Yao } 762c47a5599SJin Yao 7635b9a5000SJohn Garry bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 764730670b1SJohn Garry { 765730670b1SJohn Garry char *tmp = NULL, *tok, *str; 766730670b1SJohn Garry bool res; 767730670b1SJohn Garry 768730670b1SJohn Garry str = strdup(pmu_name); 769730670b1SJohn Garry if (!str) 770730670b1SJohn Garry return false; 771730670b1SJohn Garry 772730670b1SJohn Garry /* 773730670b1SJohn Garry * uncore alias may be from different PMU with common prefix 774730670b1SJohn Garry */ 775730670b1SJohn Garry tok = strtok_r(str, ",", &tmp); 776730670b1SJohn Garry if (strncmp(pmu_name, tok, strlen(tok))) { 777730670b1SJohn Garry res = false; 778730670b1SJohn Garry goto out; 779730670b1SJohn Garry } 780730670b1SJohn Garry 781730670b1SJohn Garry /* 782730670b1SJohn Garry * Match more complex aliases where the alias name is a comma-delimited 783730670b1SJohn Garry * list of tokens, orderly contained in the matching PMU name. 784730670b1SJohn Garry * 785730670b1SJohn Garry * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 786730670b1SJohn Garry * match "socket" in "socketX_pmunameY" and then "pmuname" in 787730670b1SJohn Garry * "pmunameY". 788730670b1SJohn Garry */ 789c07d5c92SJohn Garry while (1) { 790c07d5c92SJohn Garry char *next_tok = strtok_r(NULL, ",", &tmp); 791c07d5c92SJohn Garry 792730670b1SJohn Garry name = strstr(name, tok); 793c07d5c92SJohn Garry if (!name || 794c07d5c92SJohn Garry (!next_tok && !perf_pmu__valid_suffix(name, tok))) { 795730670b1SJohn Garry res = false; 796730670b1SJohn Garry goto out; 797730670b1SJohn Garry } 798c07d5c92SJohn Garry if (!next_tok) 799c07d5c92SJohn Garry break; 800c07d5c92SJohn Garry tok = next_tok; 801c07d5c92SJohn Garry name += strlen(tok); 802730670b1SJohn Garry } 803730670b1SJohn Garry 804730670b1SJohn Garry res = true; 805730670b1SJohn Garry out: 806730670b1SJohn Garry free(str); 807730670b1SJohn Garry return res; 808730670b1SJohn Garry } 809730670b1SJohn Garry 810660842e4SIan Rogers struct pmu_add_cpu_aliases_map_data { 811660842e4SIan Rogers struct list_head *head; 812660842e4SIan Rogers const char *name; 813660842e4SIan Rogers const char *cpu_name; 814660842e4SIan Rogers struct perf_pmu *pmu; 815660842e4SIan Rogers }; 816660842e4SIan Rogers 817660842e4SIan Rogers static int pmu_add_cpu_aliases_map_callback(const struct pmu_event *pe, 8181ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 819660842e4SIan Rogers void *vdata) 820660842e4SIan Rogers { 821660842e4SIan Rogers struct pmu_add_cpu_aliases_map_data *data = vdata; 822660842e4SIan Rogers const char *pname = pe->pmu ? pe->pmu : data->cpu_name; 823660842e4SIan Rogers 824660842e4SIan Rogers if (!pe->name) 825660842e4SIan Rogers return 0; 826660842e4SIan Rogers 827660842e4SIan Rogers if (data->pmu->is_uncore && pmu_uncore_alias_match(pname, data->name)) 828660842e4SIan Rogers goto new_alias; 829660842e4SIan Rogers 830660842e4SIan Rogers if (strcmp(pname, data->name)) 831660842e4SIan Rogers return 0; 832660842e4SIan Rogers 833660842e4SIan Rogers new_alias: 834660842e4SIan Rogers /* need type casts to override 'const' */ 835660842e4SIan Rogers __perf_pmu__new_alias(data->head, NULL, (char *)pe->name, (char *)pe->desc, 836660842e4SIan Rogers (char *)pe->event, pe); 837660842e4SIan Rogers return 0; 838660842e4SIan Rogers } 839660842e4SIan Rogers 840933f82ffSSukadev Bhattiprolu /* 841933f82ffSSukadev Bhattiprolu * From the pmu_events_map, find the table of PMU events that corresponds 842933f82ffSSukadev Bhattiprolu * to the current running CPU. Then, add all PMU events from that table 843933f82ffSSukadev Bhattiprolu * as aliases. 844933f82ffSSukadev Bhattiprolu */ 845eeac7730SIan Rogers void pmu_add_cpu_aliases_table(struct list_head *head, struct perf_pmu *pmu, 8461ba3752aSIan Rogers const struct pmu_events_table *table) 847933f82ffSSukadev Bhattiprolu { 848660842e4SIan Rogers struct pmu_add_cpu_aliases_map_data data = { 849660842e4SIan Rogers .head = head, 850660842e4SIan Rogers .name = pmu->name, 851660842e4SIan Rogers .cpu_name = is_arm_pmu_core(pmu->name) ? pmu->name : "cpu", 852660842e4SIan Rogers .pmu = pmu, 853660842e4SIan Rogers }; 854fedb2b51SAndi Kleen 855660842e4SIan Rogers pmu_events_table_for_each_event(table, pmu_add_cpu_aliases_map_callback, &data); 856933f82ffSSukadev Bhattiprolu } 857933f82ffSSukadev Bhattiprolu 858e45ad701SJohn Garry static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) 859e45ad701SJohn Garry { 8601ba3752aSIan Rogers const struct pmu_events_table *table; 861e45ad701SJohn Garry 862eeac7730SIan Rogers table = perf_pmu__find_table(pmu); 863eeac7730SIan Rogers if (!table) 864e45ad701SJohn Garry return; 865e45ad701SJohn Garry 866eeac7730SIan Rogers pmu_add_cpu_aliases_table(head, pmu, table); 867e45ad701SJohn Garry } 868e45ad701SJohn Garry 8694513c719SJohn Garry struct pmu_sys_event_iter_data { 8704513c719SJohn Garry struct list_head *head; 8714513c719SJohn Garry struct perf_pmu *pmu; 8724513c719SJohn Garry }; 8734513c719SJohn Garry 87429be2fe0SIan Rogers static int pmu_add_sys_aliases_iter_fn(const struct pmu_event *pe, 8751ba3752aSIan Rogers const struct pmu_events_table *table __maybe_unused, 87629be2fe0SIan Rogers void *data) 8774513c719SJohn Garry { 8784513c719SJohn Garry struct pmu_sys_event_iter_data *idata = data; 8794513c719SJohn Garry struct perf_pmu *pmu = idata->pmu; 8804513c719SJohn Garry 8814513c719SJohn Garry if (!pe->name) { 8824513c719SJohn Garry if (pe->metric_group || pe->metric_name) 8834513c719SJohn Garry return 0; 8844513c719SJohn Garry return -EINVAL; 8854513c719SJohn Garry } 8864513c719SJohn Garry 8874513c719SJohn Garry if (!pe->compat || !pe->pmu) 8884513c719SJohn Garry return 0; 8894513c719SJohn Garry 8904513c719SJohn Garry if (!strcmp(pmu->id, pe->compat) && 8914513c719SJohn Garry pmu_uncore_alias_match(pe->pmu, pmu->name)) { 8924513c719SJohn Garry __perf_pmu__new_alias(idata->head, NULL, 8934513c719SJohn Garry (char *)pe->name, 8944513c719SJohn Garry (char *)pe->desc, 8954513c719SJohn Garry (char *)pe->event, 896eab35953SJin Yao pe); 8974513c719SJohn Garry } 8984513c719SJohn Garry 8994513c719SJohn Garry return 0; 9004513c719SJohn Garry } 9014513c719SJohn Garry 902e199f47fSJohn Garry void pmu_add_sys_aliases(struct list_head *head, struct perf_pmu *pmu) 9034513c719SJohn Garry { 9044513c719SJohn Garry struct pmu_sys_event_iter_data idata = { 9054513c719SJohn Garry .head = head, 9064513c719SJohn Garry .pmu = pmu, 9074513c719SJohn Garry }; 9084513c719SJohn Garry 9094513c719SJohn Garry if (!pmu->id) 9104513c719SJohn Garry return; 9114513c719SJohn Garry 9124513c719SJohn Garry pmu_for_each_sys_event(pmu_add_sys_aliases_iter_fn, &idata); 9134513c719SJohn Garry } 9144513c719SJohn Garry 915c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 916dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 917dc0a6202SAdrian Hunter { 918dc0a6202SAdrian Hunter return NULL; 919dc0a6202SAdrian Hunter } 920dc0a6202SAdrian Hunter 92113d60ba0SKan Liang char * __weak 92213d60ba0SKan Liang pmu_find_real_name(const char *name) 92313d60ba0SKan Liang { 92413d60ba0SKan Liang return (char *)name; 92513d60ba0SKan Liang } 92613d60ba0SKan Liang 92713d60ba0SKan Liang char * __weak 92813d60ba0SKan Liang pmu_find_alias_name(const char *name __maybe_unused) 92913d60ba0SKan Liang { 93013d60ba0SKan Liang return NULL; 93113d60ba0SKan Liang } 93213d60ba0SKan Liang 93390a86bdeSJiri Olsa static int pmu_max_precise(const char *name) 93490a86bdeSJiri Olsa { 93590a86bdeSJiri Olsa char path[PATH_MAX]; 93690a86bdeSJiri Olsa int max_precise = -1; 93790a86bdeSJiri Olsa 93890a86bdeSJiri Olsa scnprintf(path, PATH_MAX, 93990a86bdeSJiri Olsa "bus/event_source/devices/%s/caps/max_precise", 94090a86bdeSJiri Olsa name); 94190a86bdeSJiri Olsa 94290a86bdeSJiri Olsa sysfs__read_int(path, &max_precise); 94390a86bdeSJiri Olsa return max_precise; 94490a86bdeSJiri Olsa } 94590a86bdeSJiri Olsa 94613d60ba0SKan Liang static struct perf_pmu *pmu_lookup(const char *lookup_name) 947cd82a32eSJiri Olsa { 948cd82a32eSJiri Olsa struct perf_pmu *pmu; 949cd82a32eSJiri Olsa LIST_HEAD(format); 950a6146d50SZheng Yan LIST_HEAD(aliases); 951cd82a32eSJiri Olsa __u32 type; 95213d60ba0SKan Liang char *name = pmu_find_real_name(lookup_name); 95349afa7f6SJin Yao bool is_hybrid = perf_pmu__hybrid_mounted(name); 95413d60ba0SKan Liang char *alias_name; 95549afa7f6SJin Yao 95649afa7f6SJin Yao /* 95749afa7f6SJin Yao * Check pmu name for hybrid and the pmu may be invalid in sysfs 95849afa7f6SJin Yao */ 95949afa7f6SJin Yao if (!strncmp(name, "cpu_", 4) && !is_hybrid) 96049afa7f6SJin Yao return NULL; 961cd82a32eSJiri Olsa 962cd82a32eSJiri Olsa /* 963cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 964cd82a32eSJiri Olsa * type value and format definitions. Load both right 965cd82a32eSJiri Olsa * now. 966cd82a32eSJiri Olsa */ 967cd82a32eSJiri Olsa if (pmu_format(name, &format)) 968cd82a32eSJiri Olsa return NULL; 969cd82a32eSJiri Olsa 97015b22ed3SAndi Kleen /* 97115b22ed3SAndi Kleen * Check the type first to avoid unnecessary work. 97215b22ed3SAndi Kleen */ 97315b22ed3SAndi Kleen if (pmu_type(name, &type)) 97415b22ed3SAndi Kleen return NULL; 97515b22ed3SAndi Kleen 9763fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 9773fded963SJiri Olsa return NULL; 9783fded963SJiri Olsa 979cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 980cd82a32eSJiri Olsa if (!pmu) 981cd82a32eSJiri Olsa return NULL; 982cd82a32eSJiri Olsa 9837ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 98454e32dc0SGanapatrao Kulkarni pmu->name = strdup(name); 98513d60ba0SKan Liang if (!pmu->name) 98613d60ba0SKan Liang goto err; 98713d60ba0SKan Liang 98813d60ba0SKan Liang alias_name = pmu_find_alias_name(name); 98913d60ba0SKan Liang if (alias_name) { 99013d60ba0SKan Liang pmu->alias_name = strdup(alias_name); 99113d60ba0SKan Liang if (!pmu->alias_name) 99213d60ba0SKan Liang goto err; 99313d60ba0SKan Liang } 99413d60ba0SKan Liang 99554e32dc0SGanapatrao Kulkarni pmu->type = type; 99666ec1191SMark Rutland pmu->is_uncore = pmu_is_uncore(name); 99751d54847SJohn Garry if (pmu->is_uncore) 99851d54847SJohn Garry pmu->id = pmu_id(name); 99990a86bdeSJiri Olsa pmu->max_precise = pmu_max_precise(name); 100054e32dc0SGanapatrao Kulkarni pmu_add_cpu_aliases(&aliases, pmu); 10014513c719SJohn Garry pmu_add_sys_aliases(&aliases, pmu); 100266ec1191SMark Rutland 1003cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 1004a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 10059fbc61f8SKan Liang INIT_LIST_HEAD(&pmu->caps); 1006cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 1007a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 10089bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 1009dc0a6202SAdrian Hunter 1010e5f4afbeSIan Rogers if (is_hybrid) 101144462430SJin Yao list_add_tail(&pmu->hybrid_list, &perf_pmu__hybrid_pmus); 101244462430SJin Yao 1013dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 1014dc0a6202SAdrian Hunter 1015cd82a32eSJiri Olsa return pmu; 101613d60ba0SKan Liang err: 101713d60ba0SKan Liang if (pmu->name) 101813d60ba0SKan Liang free(pmu->name); 101913d60ba0SKan Liang free(pmu); 102013d60ba0SKan Liang return NULL; 1021cd82a32eSJiri Olsa } 1022cd82a32eSJiri Olsa 1023e552b7beSRob Herring void perf_pmu__warn_invalid_formats(struct perf_pmu *pmu) 1024e552b7beSRob Herring { 1025e552b7beSRob Herring struct perf_pmu_format *format; 1026e552b7beSRob Herring 1027e552b7beSRob Herring /* fake pmu doesn't have format list */ 1028e552b7beSRob Herring if (pmu == &perf_pmu__fake) 1029e552b7beSRob Herring return; 1030e552b7beSRob Herring 1031e552b7beSRob Herring list_for_each_entry(format, &pmu->format, list) 1032e552b7beSRob Herring if (format->value >= PERF_PMU_FORMAT_VALUE_CONFIG_END) { 1033e552b7beSRob Herring pr_warning("WARNING: '%s' format '%s' requires 'perf_event_attr::config%d'" 1034e552b7beSRob Herring "which is not supported by this version of perf!\n", 1035e552b7beSRob Herring pmu->name, format->name, format->value); 1036e552b7beSRob Herring return; 1037e552b7beSRob Herring } 1038e552b7beSRob Herring } 1039e552b7beSRob Herring 1040b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 1041cd82a32eSJiri Olsa { 1042cd82a32eSJiri Olsa struct perf_pmu *pmu; 1043cd82a32eSJiri Olsa 104413d60ba0SKan Liang list_for_each_entry(pmu, &pmus, list) { 104513d60ba0SKan Liang if (!strcmp(pmu->name, name) || 104613d60ba0SKan Liang (pmu->alias_name && !strcmp(pmu->alias_name, name))) 1047cd82a32eSJiri Olsa return pmu; 104813d60ba0SKan Liang } 1049cd82a32eSJiri Olsa 1050cd82a32eSJiri Olsa return NULL; 1051cd82a32eSJiri Olsa } 1052cd82a32eSJiri Olsa 10533a50dc76SStephane Eranian struct perf_pmu *perf_pmu__find_by_type(unsigned int type) 10543a50dc76SStephane Eranian { 10553a50dc76SStephane Eranian struct perf_pmu *pmu; 10563a50dc76SStephane Eranian 10573a50dc76SStephane Eranian list_for_each_entry(pmu, &pmus, list) 10583a50dc76SStephane Eranian if (pmu->type == type) 10593a50dc76SStephane Eranian return pmu; 10603a50dc76SStephane Eranian 10613a50dc76SStephane Eranian return NULL; 10623a50dc76SStephane Eranian } 10633a50dc76SStephane Eranian 106450a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 106550a9667cSRobert Richter { 106650a9667cSRobert Richter /* 106750a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 106850a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 106950a9667cSRobert Richter */ 107050a9667cSRobert Richter if (!pmu) { 107150a9667cSRobert Richter pmu_read_sysfs(); 107250a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 107350a9667cSRobert Richter } 107450a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 107550a9667cSRobert Richter return pmu; 107650a9667cSRobert Richter return NULL; 107750a9667cSRobert Richter } 107850a9667cSRobert Richter 1079e76026bdSArnaldo Carvalho de Melo struct perf_pmu *evsel__find_pmu(struct evsel *evsel) 1080e12ee9f7SAdrian Hunter { 1081e12ee9f7SAdrian Hunter struct perf_pmu *pmu = NULL; 1082e12ee9f7SAdrian Hunter 1083f7400262SNamhyung Kim if (evsel->pmu) 1084f7400262SNamhyung Kim return evsel->pmu; 1085f7400262SNamhyung Kim 1086e12ee9f7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1087e12ee9f7SAdrian Hunter if (pmu->type == evsel->core.attr.type) 1088e12ee9f7SAdrian Hunter break; 1089e12ee9f7SAdrian Hunter } 1090e12ee9f7SAdrian Hunter 1091f7400262SNamhyung Kim evsel->pmu = pmu; 1092e12ee9f7SAdrian Hunter return pmu; 1093e12ee9f7SAdrian Hunter } 1094e12ee9f7SAdrian Hunter 109539453ed5SArnaldo Carvalho de Melo bool evsel__is_aux_event(struct evsel *evsel) 1096e12ee9f7SAdrian Hunter { 1097e76026bdSArnaldo Carvalho de Melo struct perf_pmu *pmu = evsel__find_pmu(evsel); 1098e12ee9f7SAdrian Hunter 1099e12ee9f7SAdrian Hunter return pmu && pmu->auxtrace; 1100e12ee9f7SAdrian Hunter } 1101e12ee9f7SAdrian Hunter 1102b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 1103cd82a32eSJiri Olsa { 1104cd82a32eSJiri Olsa struct perf_pmu *pmu; 1105cd82a32eSJiri Olsa 1106cd82a32eSJiri Olsa /* 1107cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 1108cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 1109cd82a32eSJiri Olsa * the pmu format definitions. 1110cd82a32eSJiri Olsa */ 1111cd82a32eSJiri Olsa pmu = pmu_find(name); 1112cd82a32eSJiri Olsa if (pmu) 1113cd82a32eSJiri Olsa return pmu; 1114cd82a32eSJiri Olsa 1115cd82a32eSJiri Olsa return pmu_lookup(name); 1116cd82a32eSJiri Olsa } 1117cd82a32eSJiri Olsa 11185c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 111909ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 1120cd82a32eSJiri Olsa { 11215c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1122cd82a32eSJiri Olsa 1123cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 1124cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 1125cd82a32eSJiri Olsa return format; 1126cd82a32eSJiri Olsa 1127cd82a32eSJiri Olsa return NULL; 1128cd82a32eSJiri Olsa } 1129cd82a32eSJiri Olsa 113009ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 113109ff6071SAdrian Hunter { 113209ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 113309ff6071SAdrian Hunter __u64 bits = 0; 113409ff6071SAdrian Hunter int fbit; 113509ff6071SAdrian Hunter 113609ff6071SAdrian Hunter if (!format) 113709ff6071SAdrian Hunter return 0; 113809ff6071SAdrian Hunter 113909ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 114009ff6071SAdrian Hunter bits |= 1ULL << fbit; 114109ff6071SAdrian Hunter 114209ff6071SAdrian Hunter return bits; 114309ff6071SAdrian Hunter } 114409ff6071SAdrian Hunter 1145a1ac7de6SAdrian Hunter int perf_pmu__format_type(struct list_head *formats, const char *name) 1146a1ac7de6SAdrian Hunter { 1147a1ac7de6SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 1148a1ac7de6SAdrian Hunter 1149a1ac7de6SAdrian Hunter if (!format) 1150a1ac7de6SAdrian Hunter return -1; 1151a1ac7de6SAdrian Hunter 1152a1ac7de6SAdrian Hunter return format->value; 1153a1ac7de6SAdrian Hunter } 1154a1ac7de6SAdrian Hunter 1155cd82a32eSJiri Olsa /* 1156dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 11574d39c89fSIngo Molnar * and unformatted value (value parameter). 1158cd82a32eSJiri Olsa */ 1159dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 1160dc0a6202SAdrian Hunter bool zero) 1161cd82a32eSJiri Olsa { 1162cd82a32eSJiri Olsa unsigned long fbit, vbit; 1163cd82a32eSJiri Olsa 1164cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 1165cd82a32eSJiri Olsa 1166cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 1167cd82a32eSJiri Olsa continue; 1168cd82a32eSJiri Olsa 1169dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 1170dc0a6202SAdrian Hunter *v |= (1llu << fbit); 1171dc0a6202SAdrian Hunter else if (zero) 1172dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 1173cd82a32eSJiri Olsa } 1174cd82a32eSJiri Olsa } 1175cd82a32eSJiri Olsa 11760efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 11770efe6b67SAdrian Hunter { 11781b9caa10SJiri Olsa int w; 11790efe6b67SAdrian Hunter 11801b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 11811b9caa10SJiri Olsa if (!w) 11821b9caa10SJiri Olsa return 0; 11831b9caa10SJiri Olsa if (w < 64) 11841b9caa10SJiri Olsa return (1ULL << w) - 1; 11851b9caa10SJiri Olsa return -1; 11860efe6b67SAdrian Hunter } 11870efe6b67SAdrian Hunter 1188cd82a32eSJiri Olsa /* 1189688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 1190688d4dfcSCody P Schafer * in the remaining terms. 1191688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 1192688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 1193688d4dfcSCody P Schafer * in a config string) later on in the term list. 1194688d4dfcSCody P Schafer */ 1195688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 1196688d4dfcSCody P Schafer struct list_head *head_terms, 1197688d4dfcSCody P Schafer __u64 *value) 1198688d4dfcSCody P Schafer { 1199688d4dfcSCody P Schafer struct parse_events_term *t; 1200688d4dfcSCody P Schafer 1201688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 12022a3d252dSIan Rogers if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM && 12032a3d252dSIan Rogers t->config && !strcmp(t->config, term->config)) { 1204688d4dfcSCody P Schafer t->used = true; 1205688d4dfcSCody P Schafer *value = t->val.num; 1206688d4dfcSCody P Schafer return 0; 1207688d4dfcSCody P Schafer } 1208688d4dfcSCody P Schafer } 1209688d4dfcSCody P Schafer 1210bb963e16SNamhyung Kim if (verbose > 0) 1211688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 1212688d4dfcSCody P Schafer 1213688d4dfcSCody P Schafer return -1; 1214688d4dfcSCody P Schafer } 1215688d4dfcSCody P Schafer 1216ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 1217e64b020bSJiri Olsa { 1218e64b020bSJiri Olsa struct perf_pmu_format *format; 121911db4e29SMasami Hiramatsu char *str = NULL; 122011db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1221f1417ceaSXin Gao unsigned int i = 0; 1222e64b020bSJiri Olsa 1223ffeb883eSHe Kuang if (!formats) 1224e64b020bSJiri Olsa return NULL; 1225e64b020bSJiri Olsa 1226e64b020bSJiri Olsa /* sysfs exported terms */ 1227ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 122811db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 122911db4e29SMasami Hiramatsu goto error; 1230e64b020bSJiri Olsa 1231ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 123211db4e29SMasami Hiramatsu error: 1233ffeb883eSHe Kuang strbuf_release(&buf); 1234e64b020bSJiri Olsa 1235e64b020bSJiri Olsa return str; 1236e64b020bSJiri Olsa } 1237e64b020bSJiri Olsa 1238688d4dfcSCody P Schafer /* 1239cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 124088aca8d9SCody P Schafer * user input data - term parameter. 1241cd82a32eSJiri Olsa */ 12424ac22b48SIan Rogers static int pmu_config_term(const char *pmu_name, 12434ac22b48SIan Rogers struct list_head *formats, 1244cd82a32eSJiri Olsa struct perf_event_attr *attr, 1245dc0a6202SAdrian Hunter struct parse_events_term *term, 1246688d4dfcSCody P Schafer struct list_head *head_terms, 1247e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1248cd82a32eSJiri Olsa { 12495c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1250cd82a32eSJiri Olsa __u64 *vp; 12510efe6b67SAdrian Hunter __u64 val, max_val; 1252cd82a32eSJiri Olsa 1253cd82a32eSJiri Olsa /* 1254688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1255688d4dfcSCody P Schafer * skip it in normal eval. 1256688d4dfcSCody P Schafer */ 1257688d4dfcSCody P Schafer if (term->used) 1258688d4dfcSCody P Schafer return 0; 1259688d4dfcSCody P Schafer 1260688d4dfcSCody P Schafer /* 1261cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1262cd82a32eSJiri Olsa * to be done for them. 1263cd82a32eSJiri Olsa */ 1264cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1265cd82a32eSJiri Olsa return 0; 1266cd82a32eSJiri Olsa 1267cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 1268688d4dfcSCody P Schafer if (!format) { 1269ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 12704ac22b48SIan Rogers char *unknown_term; 12714ac22b48SIan Rogers char *help_msg; 1272ffeb883eSHe Kuang 12734ac22b48SIan Rogers if (asprintf(&unknown_term, 12744ac22b48SIan Rogers "unknown term '%s' for pmu '%s'", 12754ac22b48SIan Rogers term->config, pmu_name) < 0) 12764ac22b48SIan Rogers unknown_term = NULL; 12774ac22b48SIan Rogers help_msg = parse_events_formats_error_string(pmu_term); 12784ac22b48SIan Rogers if (err) { 12796c191289SIan Rogers parse_events_error__handle(err, term->err_term, 12804ac22b48SIan Rogers unknown_term, 12814ac22b48SIan Rogers help_msg); 12824ac22b48SIan Rogers } else { 12834ac22b48SIan Rogers pr_debug("%s (%s)\n", unknown_term, help_msg); 12844ac22b48SIan Rogers free(unknown_term); 1285e64b020bSJiri Olsa } 12864ac22b48SIan Rogers free(pmu_term); 1287cd82a32eSJiri Olsa return -EINVAL; 1288688d4dfcSCody P Schafer } 1289cd82a32eSJiri Olsa 1290cd82a32eSJiri Olsa switch (format->value) { 1291cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1292cd82a32eSJiri Olsa vp = &attr->config; 1293cd82a32eSJiri Olsa break; 1294cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1295cd82a32eSJiri Olsa vp = &attr->config1; 1296cd82a32eSJiri Olsa break; 1297cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1298cd82a32eSJiri Olsa vp = &attr->config2; 1299cd82a32eSJiri Olsa break; 1300cd82a32eSJiri Olsa default: 1301cd82a32eSJiri Olsa return -EINVAL; 1302cd82a32eSJiri Olsa } 1303cd82a32eSJiri Olsa 130416fa7e82SJiri Olsa /* 1305688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1306688d4dfcSCody P Schafer * using event parameters. 130716fa7e82SJiri Olsa */ 130899e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 130999e7138eSJiri Olsa if (term->no_value && 131099e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 131199e7138eSJiri Olsa if (err) { 13126c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1313448d732cSIan Rogers strdup("no value assigned for term"), 1314448d732cSIan Rogers NULL); 131599e7138eSJiri Olsa } 131699e7138eSJiri Olsa return -EINVAL; 131799e7138eSJiri Olsa } 131899e7138eSJiri Olsa 1319688d4dfcSCody P Schafer val = term->val.num; 132099e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1321688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1322bb963e16SNamhyung Kim if (verbose > 0) { 1323688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1324688d4dfcSCody P Schafer term->config, term->val.str); 1325e64b020bSJiri Olsa } 1326e64b020bSJiri Olsa if (err) { 13276c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1328448d732cSIan Rogers strdup("expected numeric value"), 1329448d732cSIan Rogers NULL); 1330e64b020bSJiri Olsa } 1331688d4dfcSCody P Schafer return -EINVAL; 1332688d4dfcSCody P Schafer } 1333688d4dfcSCody P Schafer 1334688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1335688d4dfcSCody P Schafer return -EINVAL; 1336688d4dfcSCody P Schafer } else 1337688d4dfcSCody P Schafer return -EINVAL; 1338688d4dfcSCody P Schafer 13390efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 13400efe6b67SAdrian Hunter if (val > max_val) { 13410efe6b67SAdrian Hunter if (err) { 1342448d732cSIan Rogers char *err_str; 1343448d732cSIan Rogers 13446c191289SIan Rogers parse_events_error__handle(err, term->err_val, 1345448d732cSIan Rogers asprintf(&err_str, 13460efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1347448d732cSIan Rogers (unsigned long long)max_val) < 0 1348448d732cSIan Rogers ? strdup("value too big for format") 1349448d732cSIan Rogers : err_str, 1350448d732cSIan Rogers NULL); 13510efe6b67SAdrian Hunter return -EINVAL; 13520efe6b67SAdrian Hunter } 13530efe6b67SAdrian Hunter /* 13540efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 13550efe6b67SAdrian Hunter * silently truncated. 13560efe6b67SAdrian Hunter */ 13570efe6b67SAdrian Hunter } 13580efe6b67SAdrian Hunter 1359688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1360cd82a32eSJiri Olsa return 0; 1361cd82a32eSJiri Olsa } 1362cd82a32eSJiri Olsa 13634ac22b48SIan Rogers int perf_pmu__config_terms(const char *pmu_name, struct list_head *formats, 1364cff7f956SJiri Olsa struct perf_event_attr *attr, 1365dc0a6202SAdrian Hunter struct list_head *head_terms, 1366e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1367cd82a32eSJiri Olsa { 13686cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1369cd82a32eSJiri Olsa 1370688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 13714ac22b48SIan Rogers if (pmu_config_term(pmu_name, formats, attr, term, head_terms, 1372e64b020bSJiri Olsa zero, err)) 1373cd82a32eSJiri Olsa return -EINVAL; 1374688d4dfcSCody P Schafer } 1375cd82a32eSJiri Olsa 1376cd82a32eSJiri Olsa return 0; 1377cd82a32eSJiri Olsa } 1378cd82a32eSJiri Olsa 1379cd82a32eSJiri Olsa /* 1380cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1381cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1382cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1383cd82a32eSJiri Olsa */ 1384cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1385e64b020bSJiri Olsa struct list_head *head_terms, 1386e64b020bSJiri Olsa struct parse_events_error *err) 1387cd82a32eSJiri Olsa { 1388dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1389dc0a6202SAdrian Hunter 1390cd82a32eSJiri Olsa attr->type = pmu->type; 13914ac22b48SIan Rogers return perf_pmu__config_terms(pmu->name, &pmu->format, attr, 13924ac22b48SIan Rogers head_terms, zero, err); 1393cd82a32eSJiri Olsa } 1394cd82a32eSJiri Olsa 13955c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 13966cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1397a6146d50SZheng Yan { 13985c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1399a6146d50SZheng Yan char *name; 1400a6146d50SZheng Yan 1401a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1402a6146d50SZheng Yan return NULL; 1403a6146d50SZheng Yan 1404a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1405a6146d50SZheng Yan if (term->val.num != 1) 1406a6146d50SZheng Yan return NULL; 1407a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1408a6146d50SZheng Yan return NULL; 1409a6146d50SZheng Yan name = term->config; 1410a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1411a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1412a6146d50SZheng Yan return NULL; 1413a6146d50SZheng Yan name = term->val.str; 1414a6146d50SZheng Yan } else { 1415a6146d50SZheng Yan return NULL; 1416a6146d50SZheng Yan } 1417a6146d50SZheng Yan 1418a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 1419a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 1420a6146d50SZheng Yan return alias; 1421a6146d50SZheng Yan } 1422a6146d50SZheng Yan return NULL; 1423a6146d50SZheng Yan } 1424a6146d50SZheng Yan 1425410136f5SStephane Eranian 14261d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 14271d9e446bSJiri Olsa struct perf_pmu_info *info) 1428410136f5SStephane Eranian { 1429410136f5SStephane Eranian /* 1430410136f5SStephane Eranian * Only one term in event definition can 14311d9e446bSJiri Olsa * define unit, scale and snapshot, fail 14321d9e446bSJiri Olsa * if there's more than one. 1433410136f5SStephane Eranian */ 1434b30a7d1fSArnaldo Carvalho de Melo if ((info->unit && alias->unit[0]) || 14351d9e446bSJiri Olsa (info->scale && alias->scale) || 14361d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 1437410136f5SStephane Eranian return -EINVAL; 1438410136f5SStephane Eranian 1439b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 14401d9e446bSJiri Olsa info->unit = alias->unit; 1441410136f5SStephane Eranian 1442410136f5SStephane Eranian if (alias->scale) 14431d9e446bSJiri Olsa info->scale = alias->scale; 14441d9e446bSJiri Olsa 14451d9e446bSJiri Olsa if (alias->snapshot) 14461d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1447410136f5SStephane Eranian 1448410136f5SStephane Eranian return 0; 1449410136f5SStephane Eranian } 1450410136f5SStephane Eranian 1451a6146d50SZheng Yan /* 1452a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1453a6146d50SZheng Yan * defined for the alias 1454a6146d50SZheng Yan */ 1455410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 145646441bdcSMatt Fleming struct perf_pmu_info *info) 1457a6146d50SZheng Yan { 14586cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 14595c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1460a6146d50SZheng Yan int ret; 1461a6146d50SZheng Yan 1462044330c1SMatt Fleming info->per_pkg = false; 1463044330c1SMatt Fleming 14648a398897SStephane Eranian /* 14658a398897SStephane Eranian * Mark unit and scale as not set 14668a398897SStephane Eranian * (different from default values, see below) 14678a398897SStephane Eranian */ 146846441bdcSMatt Fleming info->unit = NULL; 146946441bdcSMatt Fleming info->scale = 0.0; 14701d9e446bSJiri Olsa info->snapshot = false; 147137932c18SAndi Kleen info->metric_expr = NULL; 147296284814SAndi Kleen info->metric_name = NULL; 1473410136f5SStephane Eranian 1474a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1475a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1476a6146d50SZheng Yan if (!alias) 1477a6146d50SZheng Yan continue; 1478a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 1479a6146d50SZheng Yan if (ret) 1480a6146d50SZheng Yan return ret; 1481410136f5SStephane Eranian 14821d9e446bSJiri Olsa ret = check_info_data(alias, info); 1483410136f5SStephane Eranian if (ret) 1484410136f5SStephane Eranian return ret; 1485410136f5SStephane Eranian 1486044330c1SMatt Fleming if (alias->per_pkg) 1487044330c1SMatt Fleming info->per_pkg = true; 148837932c18SAndi Kleen info->metric_expr = alias->metric_expr; 148996284814SAndi Kleen info->metric_name = alias->metric_name; 1490044330c1SMatt Fleming 1491e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 14921dc92556SIan Rogers parse_events_term__delete(term); 1493a6146d50SZheng Yan } 14948a398897SStephane Eranian 14958a398897SStephane Eranian /* 14968a398897SStephane Eranian * if no unit or scale found in aliases, then 14978a398897SStephane Eranian * set defaults as for evsel 14988a398897SStephane Eranian * unit cannot left to NULL 14998a398897SStephane Eranian */ 150046441bdcSMatt Fleming if (info->unit == NULL) 150146441bdcSMatt Fleming info->unit = ""; 15028a398897SStephane Eranian 150346441bdcSMatt Fleming if (info->scale == 0.0) 150446441bdcSMatt Fleming info->scale = 1.0; 15058a398897SStephane Eranian 1506a6146d50SZheng Yan return 0; 1507a6146d50SZheng Yan } 1508a6146d50SZheng Yan 1509cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 1510cd82a32eSJiri Olsa int config, unsigned long *bits) 1511cd82a32eSJiri Olsa { 15125c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1513cd82a32eSJiri Olsa 1514cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 1515cd82a32eSJiri Olsa if (!format) 1516cd82a32eSJiri Olsa return -ENOMEM; 1517cd82a32eSJiri Olsa 1518cd82a32eSJiri Olsa format->name = strdup(name); 1519cd82a32eSJiri Olsa format->value = config; 1520cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 1521cd82a32eSJiri Olsa 1522cd82a32eSJiri Olsa list_add_tail(&format->list, list); 1523cd82a32eSJiri Olsa return 0; 1524cd82a32eSJiri Olsa } 1525cd82a32eSJiri Olsa 1526cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 1527cd82a32eSJiri Olsa { 1528cd82a32eSJiri Olsa long b; 1529cd82a32eSJiri Olsa 1530cd82a32eSJiri Olsa if (!to) 1531cd82a32eSJiri Olsa to = from; 1532cd82a32eSJiri Olsa 153315268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1534cd82a32eSJiri Olsa for (b = from; b <= to; b++) 1535cd82a32eSJiri Olsa set_bit(b, bits); 1536cd82a32eSJiri Olsa } 1537dc098b35SAndi Kleen 1538d26383dcSNamhyung Kim void perf_pmu__del_formats(struct list_head *formats) 1539d26383dcSNamhyung Kim { 1540d26383dcSNamhyung Kim struct perf_pmu_format *fmt, *tmp; 1541d26383dcSNamhyung Kim 1542d26383dcSNamhyung Kim list_for_each_entry_safe(fmt, tmp, formats, list) { 1543d26383dcSNamhyung Kim list_del(&fmt->list); 1544d26383dcSNamhyung Kim free(fmt->name); 1545d26383dcSNamhyung Kim free(fmt); 1546d26383dcSNamhyung Kim } 1547d26383dcSNamhyung Kim } 1548d26383dcSNamhyung Kim 1549aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 1550aaea3617SCody P Schafer { 1551aaea3617SCody P Schafer if (b > a) 1552aaea3617SCody P Schafer return 0; 1553aaea3617SCody P Schafer return a - b; 1554aaea3617SCody P Schafer } 1555aaea3617SCody P Schafer 1556dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 1557dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1558dc098b35SAndi Kleen { 1559aaea3617SCody P Schafer struct parse_events_term *term; 1560aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1561aaea3617SCody P Schafer 1562aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 1563aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1564aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 1565aaea3617SCody P Schafer ",%s=%s", term->config, 1566aaea3617SCody P Schafer term->val.str); 1567aaea3617SCody P Schafer } 1568aaea3617SCody P Schafer 1569aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1570aaea3617SCody P Schafer buf[used] = '/'; 1571aaea3617SCody P Schafer used++; 1572aaea3617SCody P Schafer } 1573aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1574aaea3617SCody P Schafer buf[used] = '\0'; 1575aaea3617SCody P Schafer used++; 1576aaea3617SCody P Schafer } else 1577aaea3617SCody P Schafer buf[len - 1] = '\0'; 1578aaea3617SCody P Schafer 1579dc098b35SAndi Kleen return buf; 1580dc098b35SAndi Kleen } 1581dc098b35SAndi Kleen 1582dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 1583dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1584dc098b35SAndi Kleen { 1585dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 1586dc098b35SAndi Kleen return buf; 1587dc098b35SAndi Kleen } 1588dc098b35SAndi Kleen 1589dd5f1036SAndi Kleen struct sevent { 159008e60ed1SAndi Kleen char *name; 159108e60ed1SAndi Kleen char *desc; 1592dd5f1036SAndi Kleen char *topic; 1593f2361024SAndi Kleen char *str; 1594f2361024SAndi Kleen char *pmu; 15957f372a63SAndi Kleen char *metric_expr; 159696284814SAndi Kleen char *metric_name; 1597ce0dc7d2SJohn Garry int is_cpu; 159808e60ed1SAndi Kleen }; 159908e60ed1SAndi Kleen 1600dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b) 1601dc098b35SAndi Kleen { 1602dd5f1036SAndi Kleen const struct sevent *as = a; 1603dd5f1036SAndi Kleen const struct sevent *bs = b; 16040e0ae874SJin Yao int ret; 160508e60ed1SAndi Kleen 160608e60ed1SAndi Kleen /* Put extra events last */ 160708e60ed1SAndi Kleen if (!!as->desc != !!bs->desc) 160808e60ed1SAndi Kleen return !!as->desc - !!bs->desc; 1609dd5f1036SAndi Kleen if (as->topic && bs->topic) { 1610dd5f1036SAndi Kleen int n = strcmp(as->topic, bs->topic); 1611dd5f1036SAndi Kleen 1612dd5f1036SAndi Kleen if (n) 1613dd5f1036SAndi Kleen return n; 1614dd5f1036SAndi Kleen } 1615ce0dc7d2SJohn Garry 1616ce0dc7d2SJohn Garry /* Order CPU core events to be first */ 1617ce0dc7d2SJohn Garry if (as->is_cpu != bs->is_cpu) 1618ce0dc7d2SJohn Garry return bs->is_cpu - as->is_cpu; 1619ce0dc7d2SJohn Garry 16200e0ae874SJin Yao ret = strcmp(as->name, bs->name); 16210e0ae874SJin Yao if (!ret) { 16220e0ae874SJin Yao if (as->pmu && bs->pmu) 16230e0ae874SJin Yao return strcmp(as->pmu, bs->pmu); 16240e0ae874SJin Yao } 16250e0ae874SJin Yao 16260e0ae874SJin Yao return ret; 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 1655e0257a01SJohn Garry static bool pmu_alias_is_duplicate(struct sevent *alias_a, 1656e0257a01SJohn Garry struct sevent *alias_b) 1657e0257a01SJohn Garry { 1658e0257a01SJohn Garry /* Different names -> never duplicates */ 1659e0257a01SJohn Garry if (strcmp(alias_a->name, alias_b->name)) 1660e0257a01SJohn Garry return false; 1661e0257a01SJohn Garry 1662e0257a01SJohn Garry /* Don't remove duplicates for hybrid PMUs */ 1663e0257a01SJohn Garry if (perf_pmu__is_hybrid(alias_a->pmu) && 1664e0257a01SJohn Garry perf_pmu__is_hybrid(alias_b->pmu)) 1665e0257a01SJohn Garry return false; 1666e0257a01SJohn Garry 1667e0257a01SJohn Garry return true; 1668e0257a01SJohn Garry } 1669e0257a01SJohn Garry 1670c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, 16710e0ae874SJin Yao bool long_desc, bool details_flag, bool deprecated, 16720e0ae874SJin Yao const char *pmu_name) 1673dc098b35SAndi Kleen { 1674dc098b35SAndi Kleen struct perf_pmu *pmu; 1675dc098b35SAndi Kleen struct perf_pmu_alias *alias; 1676dc098b35SAndi Kleen char buf[1024]; 1677dc098b35SAndi Kleen int printed = 0; 1678dc098b35SAndi Kleen int len, j; 1679dd5f1036SAndi Kleen struct sevent *aliases; 168008e60ed1SAndi Kleen int numdesc = 0; 168161eb2eb4SAndi Kleen int columns = pager_get_columns(); 1682dd5f1036SAndi Kleen char *topic = NULL; 1683dc098b35SAndi Kleen 1684dc098b35SAndi Kleen pmu = NULL; 1685dc098b35SAndi Kleen len = 0; 168642634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1687dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 1688dc098b35SAndi Kleen len++; 168942634bc7SAdrian Hunter if (pmu->selectable) 169042634bc7SAdrian Hunter len++; 169142634bc7SAdrian Hunter } 1692dd5f1036SAndi Kleen aliases = zalloc(sizeof(struct sevent) * len); 1693dc098b35SAndi Kleen if (!aliases) 16947e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1695dc098b35SAndi Kleen pmu = NULL; 1696dc098b35SAndi Kleen j = 0; 169742634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 16980e0ae874SJin Yao if (pmu_name && perf_pmu__is_hybrid(pmu->name) && 16990e0ae874SJin Yao strcmp(pmu_name, pmu->name)) { 17000e0ae874SJin Yao continue; 17010e0ae874SJin Yao } 17020e0ae874SJin Yao 1703dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 170408e60ed1SAndi Kleen char *name = alias->desc ? alias->name : 170508e60ed1SAndi Kleen format_alias(buf, sizeof(buf), pmu, alias); 17060e0ae874SJin Yao bool is_cpu = is_pmu_core(pmu->name) || 17070e0ae874SJin Yao perf_pmu__is_hybrid(pmu->name); 1708dc098b35SAndi Kleen 1709a7f6c8c8SJin Yao if (alias->deprecated && !deprecated) 1710a7f6c8c8SJin Yao continue; 1711a7f6c8c8SJin Yao 1712dc098b35SAndi Kleen if (event_glob != NULL && 171338d14f0cSAndi Kleen !(strglobmatch_nocase(name, event_glob) || 171438d14f0cSAndi Kleen (!is_cpu && strglobmatch_nocase(alias->name, 171567bdc35fSAndi Kleen event_glob)) || 171667bdc35fSAndi Kleen (alias->topic && 171767bdc35fSAndi Kleen strglobmatch_nocase(alias->topic, event_glob)))) 1718dc098b35SAndi Kleen continue; 17197e4772dcSArnaldo Carvalho de Melo 172008e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 17217e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 17227e4772dcSArnaldo Carvalho de Melo 172308e60ed1SAndi Kleen aliases[j].name = name; 172408e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 172508e60ed1SAndi Kleen aliases[j].name = format_alias_or(buf, 172608e60ed1SAndi Kleen sizeof(buf), 172708e60ed1SAndi Kleen pmu, alias); 172808e60ed1SAndi Kleen aliases[j].name = strdup(aliases[j].name); 172908e60ed1SAndi Kleen if (!aliases[j].name) 17307e4772dcSArnaldo Carvalho de Melo goto out_enomem; 173108e60ed1SAndi Kleen 1732c8d6828aSSukadev Bhattiprolu aliases[j].desc = long_desc ? alias->long_desc : 1733c8d6828aSSukadev Bhattiprolu alias->desc; 1734dd5f1036SAndi Kleen aliases[j].topic = alias->topic; 1735f2361024SAndi Kleen aliases[j].str = alias->str; 1736f2361024SAndi Kleen aliases[j].pmu = pmu->name; 17377f372a63SAndi Kleen aliases[j].metric_expr = alias->metric_expr; 173896284814SAndi Kleen aliases[j].metric_name = alias->metric_name; 1739ce0dc7d2SJohn Garry aliases[j].is_cpu = is_cpu; 1740dc098b35SAndi Kleen j++; 1741dc098b35SAndi Kleen } 1742fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1743fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 17447e4772dcSArnaldo Carvalho de Melo char *s; 17457e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 17467e4772dcSArnaldo Carvalho de Melo goto out_enomem; 174708e60ed1SAndi Kleen aliases[j].name = s; 174842634bc7SAdrian Hunter j++; 174942634bc7SAdrian Hunter } 175042634bc7SAdrian Hunter } 1751dc098b35SAndi Kleen len = j; 1752dd5f1036SAndi Kleen qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1753dc098b35SAndi Kleen for (j = 0; j < len; j++) { 175415b22ed3SAndi Kleen /* Skip duplicates */ 1755e0257a01SJohn Garry if (j > 0 && pmu_alias_is_duplicate(&aliases[j], &aliases[j - 1])) 175615b22ed3SAndi Kleen continue; 17570e0ae874SJin Yao 1758dc098b35SAndi Kleen if (name_only) { 175908e60ed1SAndi Kleen printf("%s ", aliases[j].name); 1760dc098b35SAndi Kleen continue; 1761dc098b35SAndi Kleen } 17621c5f01feSAndi Kleen if (aliases[j].desc && !quiet_flag) { 176308e60ed1SAndi Kleen if (numdesc++ == 0) 176408e60ed1SAndi Kleen printf("\n"); 1765dd5f1036SAndi Kleen if (aliases[j].topic && (!topic || 1766dd5f1036SAndi Kleen strcmp(topic, aliases[j].topic))) { 1767dd5f1036SAndi Kleen printf("%s%s:\n", topic ? "\n" : "", 1768dd5f1036SAndi Kleen aliases[j].topic); 1769dd5f1036SAndi Kleen topic = aliases[j].topic; 1770dd5f1036SAndi Kleen } 177108e60ed1SAndi Kleen printf(" %-50s\n", aliases[j].name); 177208e60ed1SAndi Kleen printf("%*s", 8, "["); 177308e60ed1SAndi Kleen wordwrap(aliases[j].desc, 8, columns, 0); 177408e60ed1SAndi Kleen printf("]\n"); 1775bf874fcfSAndi Kleen if (details_flag) { 17767f372a63SAndi Kleen printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); 177796284814SAndi Kleen if (aliases[j].metric_name) 177896284814SAndi Kleen printf(" MetricName: %s", aliases[j].metric_name); 17797f372a63SAndi Kleen if (aliases[j].metric_expr) 17807f372a63SAndi Kleen printf(" MetricExpr: %s", aliases[j].metric_expr); 17817f372a63SAndi Kleen putchar('\n'); 17827f372a63SAndi Kleen } 178308e60ed1SAndi Kleen } else 178408e60ed1SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j].name); 1785dc098b35SAndi Kleen printed++; 1786dc098b35SAndi Kleen } 1787dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1788dc098b35SAndi Kleen printf("\n"); 17897e4772dcSArnaldo Carvalho de Melo out_free: 17907e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 179108e60ed1SAndi Kleen zfree(&aliases[j].name); 17927e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 17937e4772dcSArnaldo Carvalho de Melo return; 17947e4772dcSArnaldo Carvalho de Melo 17957e4772dcSArnaldo Carvalho de Melo out_enomem: 17967e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 17977e4772dcSArnaldo Carvalho de Melo if (aliases) 17987e4772dcSArnaldo Carvalho de Melo goto out_free; 1799dc098b35SAndi Kleen } 18004cabc3d1SAndi Kleen 18014cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 18024cabc3d1SAndi Kleen { 18034cabc3d1SAndi Kleen struct perf_pmu *pmu; 18044cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 18054cabc3d1SAndi Kleen 18064cabc3d1SAndi Kleen pmu = NULL; 18074cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 18084cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 18094cabc3d1SAndi Kleen continue; 18104cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 18114cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 18124cabc3d1SAndi Kleen return true; 18134cabc3d1SAndi Kleen } 18144cabc3d1SAndi Kleen return false; 18154cabc3d1SAndi Kleen } 18167d4bdab5SAdrian Hunter 18177d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 18187d4bdab5SAdrian Hunter { 18197d4bdab5SAdrian Hunter char path[PATH_MAX]; 18207d4bdab5SAdrian Hunter const char *sysfs; 18217d4bdab5SAdrian Hunter 18227d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 18237d4bdab5SAdrian Hunter if (!sysfs) 18247d4bdab5SAdrian Hunter return NULL; 18257d4bdab5SAdrian Hunter 18267d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 18277d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 1828d9664582SAndi Kleen if (!file_available(path)) 18297d4bdab5SAdrian Hunter return NULL; 18307d4bdab5SAdrian Hunter return fopen(path, "r"); 18317d4bdab5SAdrian Hunter } 18327d4bdab5SAdrian Hunter 18337d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 18347d4bdab5SAdrian Hunter ...) 18357d4bdab5SAdrian Hunter { 18367d4bdab5SAdrian Hunter va_list args; 18377d4bdab5SAdrian Hunter FILE *file; 18387d4bdab5SAdrian Hunter int ret = EOF; 18397d4bdab5SAdrian Hunter 18407d4bdab5SAdrian Hunter va_start(args, fmt); 18417d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 18427d4bdab5SAdrian Hunter if (file) { 18437d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 18447d4bdab5SAdrian Hunter fclose(file); 18457d4bdab5SAdrian Hunter } 18467d4bdab5SAdrian Hunter va_end(args); 18477d4bdab5SAdrian Hunter return ret; 18487d4bdab5SAdrian Hunter } 18499fbc61f8SKan Liang 18509fbc61f8SKan Liang static int perf_pmu__new_caps(struct list_head *list, char *name, char *value) 18519fbc61f8SKan Liang { 18529fbc61f8SKan Liang struct perf_pmu_caps *caps = zalloc(sizeof(*caps)); 18539fbc61f8SKan Liang 18549fbc61f8SKan Liang if (!caps) 18559fbc61f8SKan Liang return -ENOMEM; 18569fbc61f8SKan Liang 18579fbc61f8SKan Liang caps->name = strdup(name); 18589fbc61f8SKan Liang if (!caps->name) 18599fbc61f8SKan Liang goto free_caps; 18609fbc61f8SKan Liang caps->value = strndup(value, strlen(value) - 1); 18619fbc61f8SKan Liang if (!caps->value) 18629fbc61f8SKan Liang goto free_name; 18639fbc61f8SKan Liang list_add_tail(&caps->list, list); 18649fbc61f8SKan Liang return 0; 18659fbc61f8SKan Liang 18669fbc61f8SKan Liang free_name: 18679fbc61f8SKan Liang zfree(caps->name); 18689fbc61f8SKan Liang free_caps: 18699fbc61f8SKan Liang free(caps); 18709fbc61f8SKan Liang 18719fbc61f8SKan Liang return -ENOMEM; 18729fbc61f8SKan Liang } 18739fbc61f8SKan Liang 18749fbc61f8SKan Liang /* 18759fbc61f8SKan Liang * Reading/parsing the given pmu capabilities, which should be located at: 18769fbc61f8SKan Liang * /sys/bus/event_source/devices/<dev>/caps as sysfs group attributes. 18779fbc61f8SKan Liang * Return the number of capabilities 18789fbc61f8SKan Liang */ 18799fbc61f8SKan Liang int perf_pmu__caps_parse(struct perf_pmu *pmu) 18809fbc61f8SKan Liang { 18819fbc61f8SKan Liang struct stat st; 18829fbc61f8SKan Liang char caps_path[PATH_MAX]; 18839fbc61f8SKan Liang const char *sysfs = sysfs__mountpoint(); 18849fbc61f8SKan Liang DIR *caps_dir; 18859fbc61f8SKan Liang struct dirent *evt_ent; 18863339ec44SRavi Bangoria 18873339ec44SRavi Bangoria if (pmu->caps_initialized) 18883339ec44SRavi Bangoria return pmu->nr_caps; 18893339ec44SRavi Bangoria 18903339ec44SRavi Bangoria pmu->nr_caps = 0; 18919fbc61f8SKan Liang 18929fbc61f8SKan Liang if (!sysfs) 18939fbc61f8SKan Liang return -1; 18949fbc61f8SKan Liang 18959fbc61f8SKan Liang snprintf(caps_path, PATH_MAX, 18969fbc61f8SKan Liang "%s" EVENT_SOURCE_DEVICE_PATH "%s/caps", sysfs, pmu->name); 18979fbc61f8SKan Liang 18983339ec44SRavi Bangoria if (stat(caps_path, &st) < 0) { 18993339ec44SRavi Bangoria pmu->caps_initialized = true; 19009fbc61f8SKan Liang return 0; /* no error if caps does not exist */ 19013339ec44SRavi Bangoria } 19029fbc61f8SKan Liang 19039fbc61f8SKan Liang caps_dir = opendir(caps_path); 19049fbc61f8SKan Liang if (!caps_dir) 19059fbc61f8SKan Liang return -EINVAL; 19069fbc61f8SKan Liang 19079fbc61f8SKan Liang while ((evt_ent = readdir(caps_dir)) != NULL) { 19089fbc61f8SKan Liang char path[PATH_MAX + NAME_MAX + 1]; 19099fbc61f8SKan Liang char *name = evt_ent->d_name; 19109fbc61f8SKan Liang char value[128]; 19119fbc61f8SKan Liang FILE *file; 19129fbc61f8SKan Liang 19139fbc61f8SKan Liang if (!strcmp(name, ".") || !strcmp(name, "..")) 19149fbc61f8SKan Liang continue; 19159fbc61f8SKan Liang 19169fbc61f8SKan Liang snprintf(path, sizeof(path), "%s/%s", caps_path, name); 19179fbc61f8SKan Liang 19189fbc61f8SKan Liang file = fopen(path, "r"); 19199fbc61f8SKan Liang if (!file) 19209fbc61f8SKan Liang continue; 19219fbc61f8SKan Liang 19229fbc61f8SKan Liang if (!fgets(value, sizeof(value), file) || 19239fbc61f8SKan Liang (perf_pmu__new_caps(&pmu->caps, name, value) < 0)) { 19249fbc61f8SKan Liang fclose(file); 19259fbc61f8SKan Liang continue; 19269fbc61f8SKan Liang } 19279fbc61f8SKan Liang 19283339ec44SRavi Bangoria pmu->nr_caps++; 19299fbc61f8SKan Liang fclose(file); 19309fbc61f8SKan Liang } 19319fbc61f8SKan Liang 19329fbc61f8SKan Liang closedir(caps_dir); 19339fbc61f8SKan Liang 19343339ec44SRavi Bangoria pmu->caps_initialized = true; 19353339ec44SRavi Bangoria return pmu->nr_caps; 19369fbc61f8SKan Liang } 1937e4064776SJin Yao 1938e4064776SJin Yao void perf_pmu__warn_invalid_config(struct perf_pmu *pmu, __u64 config, 19398e8bbfb3SIan Rogers const char *name) 1940e4064776SJin Yao { 1941e4064776SJin Yao struct perf_pmu_format *format; 1942e4064776SJin Yao __u64 masks = 0, bits; 1943e4064776SJin Yao char buf[100]; 1944e4064776SJin Yao unsigned int i; 1945e4064776SJin Yao 1946e4064776SJin Yao list_for_each_entry(format, &pmu->format, list) { 1947e4064776SJin Yao if (format->value != PERF_PMU_FORMAT_VALUE_CONFIG) 1948e4064776SJin Yao continue; 1949e4064776SJin Yao 1950e4064776SJin Yao for_each_set_bit(i, format->bits, PERF_PMU_FORMAT_BITS) 1951e4064776SJin Yao masks |= 1ULL << i; 1952e4064776SJin Yao } 1953e4064776SJin Yao 1954e4064776SJin Yao /* 1955e4064776SJin Yao * Kernel doesn't export any valid format bits. 1956e4064776SJin Yao */ 1957e4064776SJin Yao if (masks == 0) 1958e4064776SJin Yao return; 1959e4064776SJin Yao 1960e4064776SJin Yao bits = config & ~masks; 1961e4064776SJin Yao if (bits == 0) 1962e4064776SJin Yao return; 1963e4064776SJin Yao 1964e4064776SJin Yao bitmap_scnprintf((unsigned long *)&bits, sizeof(bits) * 8, buf, sizeof(buf)); 1965e4064776SJin Yao 1966e4064776SJin Yao pr_warning("WARNING: event '%s' not valid (bits %s of config " 1967e4064776SJin Yao "'%llx' not supported by kernel)!\n", 1968e4064776SJin Yao name ?: "N/A", buf, config); 1969e4064776SJin Yao } 1970c5a26ea4SJin Yao 1971c5a26ea4SJin Yao bool perf_pmu__has_hybrid(void) 1972c5a26ea4SJin Yao { 1973c5a26ea4SJin Yao if (!hybrid_scanned) { 1974c5a26ea4SJin Yao hybrid_scanned = true; 1975c5a26ea4SJin Yao perf_pmu__scan(NULL); 1976c5a26ea4SJin Yao } 1977c5a26ea4SJin Yao 1978c5a26ea4SJin Yao return !list_empty(&perf_pmu__hybrid_pmus); 1979c5a26ea4SJin Yao } 1980c47a5599SJin Yao 1981c47a5599SJin Yao int perf_pmu__match(char *pattern, char *name, char *tok) 1982c47a5599SJin Yao { 198313d60ba0SKan Liang if (!name) 198413d60ba0SKan Liang return -1; 198513d60ba0SKan Liang 1986c47a5599SJin Yao if (fnmatch(pattern, name, 0)) 1987c47a5599SJin Yao return -1; 1988c47a5599SJin Yao 1989c47a5599SJin Yao if (tok && !perf_pmu__valid_suffix(name, tok)) 1990c47a5599SJin Yao return -1; 1991c47a5599SJin Yao 1992c47a5599SJin Yao return 0; 1993c47a5599SJin Yao } 19941d3351e6SJin Yao 19951d3351e6SJin Yao int perf_pmu__cpus_match(struct perf_pmu *pmu, struct perf_cpu_map *cpus, 19961d3351e6SJin Yao struct perf_cpu_map **mcpus_ptr, 19971d3351e6SJin Yao struct perf_cpu_map **ucpus_ptr) 19981d3351e6SJin Yao { 19991d3351e6SJin Yao struct perf_cpu_map *pmu_cpus = pmu->cpus; 20001d3351e6SJin Yao struct perf_cpu_map *matched_cpus, *unmatched_cpus; 20016a12a63eSIan Rogers struct perf_cpu cpu; 20026a12a63eSIan Rogers int i, matched_nr = 0, unmatched_nr = 0; 20031d3351e6SJin Yao 20041d3351e6SJin Yao matched_cpus = perf_cpu_map__default_new(); 20051d3351e6SJin Yao if (!matched_cpus) 20061d3351e6SJin Yao return -1; 20071d3351e6SJin Yao 20081d3351e6SJin Yao unmatched_cpus = perf_cpu_map__default_new(); 20091d3351e6SJin Yao if (!unmatched_cpus) { 20101d3351e6SJin Yao perf_cpu_map__put(matched_cpus); 20111d3351e6SJin Yao return -1; 20121d3351e6SJin Yao } 20131d3351e6SJin Yao 20146a12a63eSIan Rogers perf_cpu_map__for_each_cpu(cpu, i, cpus) { 20156a12a63eSIan Rogers if (!perf_cpu_map__has(pmu_cpus, cpu)) 20166a12a63eSIan Rogers unmatched_cpus->map[unmatched_nr++] = cpu; 20171d3351e6SJin Yao else 20186a12a63eSIan Rogers matched_cpus->map[matched_nr++] = cpu; 20191d3351e6SJin Yao } 20201d3351e6SJin Yao 20211d3351e6SJin Yao unmatched_cpus->nr = unmatched_nr; 20221d3351e6SJin Yao matched_cpus->nr = matched_nr; 20231d3351e6SJin Yao *mcpus_ptr = matched_cpus; 20241d3351e6SJin Yao *ucpus_ptr = unmatched_cpus; 20251d3351e6SJin Yao return 0; 20261d3351e6SJin Yao } 2027