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> 6fa0d9846SArnaldo Carvalho de Melo #include <subcmd/pager.h> 7cd82a32eSJiri Olsa #include <sys/types.h> 8a43783aeSArnaldo Carvalho de Melo #include <errno.h> 9c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h> 107a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h> 11cd82a32eSJiri Olsa #include <unistd.h> 12cd82a32eSJiri Olsa #include <stdio.h> 13dc0a6202SAdrian Hunter #include <stdbool.h> 147d4bdab5SAdrian Hunter #include <stdarg.h> 15cd82a32eSJiri Olsa #include <dirent.h> 16cd0cfad7SBorislav Petkov #include <api/fs/fs.h> 17410136f5SStephane Eranian #include <locale.h> 18fbc2844eSWilliam Cohen #include <regex.h> 199c3516d1SJiri Olsa #include <perf/cpumap.h> 205e51b0bbSArnaldo Carvalho de Melo #include "debug.h" 21cd82a32eSJiri Olsa #include "pmu.h" 22cd82a32eSJiri Olsa #include "parse-events.h" 23933f82ffSSukadev Bhattiprolu #include "header.h" 24933f82ffSSukadev Bhattiprolu #include "pmu-events/pmu-events.h" 25a067558eSArnaldo Carvalho de Melo #include "string2.h" 26fa0d9846SArnaldo Carvalho de Melo #include "strbuf.h" 27cd82a32eSJiri Olsa 28ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 29ab1bf653SArnaldo Carvalho de Melo char *name; 30ab1bf653SArnaldo Carvalho de Melo int value; 31ab1bf653SArnaldo Carvalho de Melo DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 32ab1bf653SArnaldo Carvalho de Melo struct list_head list; 33ab1bf653SArnaldo Carvalho de Melo }; 34ab1bf653SArnaldo Carvalho de Melo 35cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name); 36cd82a32eSJiri Olsa extern FILE *perf_pmu_in; 37cd82a32eSJiri Olsa 38cd82a32eSJiri Olsa static LIST_HEAD(pmus); 39cd82a32eSJiri Olsa 40cd82a32eSJiri Olsa /* 41cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 42cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 43cd82a32eSJiri Olsa */ 44cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head) 45cd82a32eSJiri Olsa { 46cd82a32eSJiri Olsa struct dirent *evt_ent; 47cd82a32eSJiri Olsa DIR *format_dir; 48cd82a32eSJiri Olsa int ret = 0; 49cd82a32eSJiri Olsa 50cd82a32eSJiri Olsa format_dir = opendir(dir); 51cd82a32eSJiri Olsa if (!format_dir) 52cd82a32eSJiri Olsa return -EINVAL; 53cd82a32eSJiri Olsa 54cd82a32eSJiri Olsa while (!ret && (evt_ent = readdir(format_dir))) { 55cd82a32eSJiri Olsa char path[PATH_MAX]; 56cd82a32eSJiri Olsa char *name = evt_ent->d_name; 57cd82a32eSJiri Olsa FILE *file; 58cd82a32eSJiri Olsa 59cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 60cd82a32eSJiri Olsa continue; 61cd82a32eSJiri Olsa 62cd82a32eSJiri Olsa snprintf(path, PATH_MAX, "%s/%s", dir, name); 63cd82a32eSJiri Olsa 64cd82a32eSJiri Olsa ret = -EINVAL; 65cd82a32eSJiri Olsa file = fopen(path, "r"); 66cd82a32eSJiri Olsa if (!file) 67cd82a32eSJiri Olsa break; 68cd82a32eSJiri Olsa 69cd82a32eSJiri Olsa perf_pmu_in = file; 70cd82a32eSJiri Olsa ret = perf_pmu_parse(head, name); 71cd82a32eSJiri Olsa fclose(file); 72cd82a32eSJiri Olsa } 73cd82a32eSJiri Olsa 74cd82a32eSJiri Olsa closedir(format_dir); 75cd82a32eSJiri Olsa return ret; 76cd82a32eSJiri Olsa } 77cd82a32eSJiri Olsa 78cd82a32eSJiri Olsa /* 79cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 80cd82a32eSJiri Olsa * located at: 81cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 82cd82a32eSJiri Olsa */ 83b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format) 84cd82a32eSJiri Olsa { 85cd82a32eSJiri Olsa struct stat st; 86cd82a32eSJiri Olsa char path[PATH_MAX]; 87cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 88cd82a32eSJiri Olsa 89cd82a32eSJiri Olsa if (!sysfs) 90cd82a32eSJiri Olsa return -1; 91cd82a32eSJiri Olsa 92cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 9350a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); 94cd82a32eSJiri Olsa 95cd82a32eSJiri Olsa if (stat(path, &st) < 0) 969bc8f9feSRobert Richter return 0; /* no error if format does not exist */ 97cd82a32eSJiri Olsa 98cff7f956SJiri Olsa if (perf_pmu__format_parse(path, format)) 99cd82a32eSJiri Olsa return -1; 100cd82a32eSJiri Olsa 101cd82a32eSJiri Olsa return 0; 102cd82a32eSJiri Olsa } 103cd82a32eSJiri Olsa 104a55ab7c4SJin Yao int perf_pmu__convert_scale(const char *scale, char **end, double *sval) 105d02fc6bcSAndi Kleen { 106d02fc6bcSAndi Kleen char *lc; 107d02fc6bcSAndi Kleen int ret = 0; 108d02fc6bcSAndi Kleen 109d02fc6bcSAndi Kleen /* 110d02fc6bcSAndi Kleen * save current locale 111d02fc6bcSAndi Kleen */ 112d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 113d02fc6bcSAndi Kleen 114d02fc6bcSAndi Kleen /* 115d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 116d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 117d02fc6bcSAndi Kleen * call below. 118d02fc6bcSAndi Kleen */ 119d02fc6bcSAndi Kleen lc = strdup(lc); 120d02fc6bcSAndi Kleen if (!lc) { 121d02fc6bcSAndi Kleen ret = -ENOMEM; 122d02fc6bcSAndi Kleen goto out; 123d02fc6bcSAndi Kleen } 124d02fc6bcSAndi Kleen 125d02fc6bcSAndi Kleen /* 126d02fc6bcSAndi Kleen * force to C locale to ensure kernel 127d02fc6bcSAndi Kleen * scale string is converted correctly. 128d02fc6bcSAndi Kleen * kernel uses default C locale. 129d02fc6bcSAndi Kleen */ 130d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 131d02fc6bcSAndi Kleen 132d02fc6bcSAndi Kleen *sval = strtod(scale, end); 133d02fc6bcSAndi Kleen 134d02fc6bcSAndi Kleen out: 135d02fc6bcSAndi Kleen /* restore locale */ 136d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 137d02fc6bcSAndi Kleen free(lc); 138d02fc6bcSAndi Kleen return ret; 139d02fc6bcSAndi Kleen } 140d02fc6bcSAndi Kleen 141410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) 142410136f5SStephane Eranian { 143410136f5SStephane Eranian struct stat st; 144410136f5SStephane Eranian ssize_t sret; 145410136f5SStephane Eranian char scale[128]; 146410136f5SStephane Eranian int fd, ret = -1; 147410136f5SStephane Eranian char path[PATH_MAX]; 148410136f5SStephane Eranian 14911a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.scale", dir, name); 150410136f5SStephane Eranian 151410136f5SStephane Eranian fd = open(path, O_RDONLY); 152410136f5SStephane Eranian if (fd == -1) 153410136f5SStephane Eranian return -1; 154410136f5SStephane Eranian 155410136f5SStephane Eranian if (fstat(fd, &st) < 0) 156410136f5SStephane Eranian goto error; 157410136f5SStephane Eranian 158410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 159410136f5SStephane Eranian if (sret < 0) 160410136f5SStephane Eranian goto error; 161410136f5SStephane Eranian 1629ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 1639ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 1649ecae065SMadhavan Srinivasan else 165410136f5SStephane Eranian scale[sret] = '\0'; 1669ecae065SMadhavan Srinivasan 167a55ab7c4SJin Yao ret = perf_pmu__convert_scale(scale, NULL, &alias->scale); 168410136f5SStephane Eranian error: 169410136f5SStephane Eranian close(fd); 170410136f5SStephane Eranian return ret; 171410136f5SStephane Eranian } 172410136f5SStephane Eranian 173410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) 174410136f5SStephane Eranian { 175410136f5SStephane Eranian char path[PATH_MAX]; 176410136f5SStephane Eranian ssize_t sret; 177410136f5SStephane Eranian int fd; 178410136f5SStephane Eranian 17911a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.unit", dir, name); 180410136f5SStephane Eranian 181410136f5SStephane Eranian fd = open(path, O_RDONLY); 182410136f5SStephane Eranian if (fd == -1) 183410136f5SStephane Eranian return -1; 184410136f5SStephane Eranian 185410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 186410136f5SStephane Eranian if (sret < 0) 187410136f5SStephane Eranian goto error; 188410136f5SStephane Eranian 189410136f5SStephane Eranian close(fd); 190410136f5SStephane Eranian 1919ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 1929ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 1939ecae065SMadhavan Srinivasan else 194410136f5SStephane Eranian alias->unit[sret] = '\0'; 195410136f5SStephane Eranian 196410136f5SStephane Eranian return 0; 197410136f5SStephane Eranian error: 198410136f5SStephane Eranian close(fd); 199410136f5SStephane Eranian alias->unit[0] = '\0'; 200410136f5SStephane Eranian return -1; 201410136f5SStephane Eranian } 202410136f5SStephane Eranian 203044330c1SMatt Fleming static int 204044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) 205044330c1SMatt Fleming { 206044330c1SMatt Fleming char path[PATH_MAX]; 207044330c1SMatt Fleming int fd; 208044330c1SMatt Fleming 20911a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); 210044330c1SMatt Fleming 211044330c1SMatt Fleming fd = open(path, O_RDONLY); 212044330c1SMatt Fleming if (fd == -1) 213044330c1SMatt Fleming return -1; 214044330c1SMatt Fleming 215044330c1SMatt Fleming close(fd); 216044330c1SMatt Fleming 217044330c1SMatt Fleming alias->per_pkg = true; 218044330c1SMatt Fleming return 0; 219044330c1SMatt Fleming } 220044330c1SMatt Fleming 2211d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 2221d9e446bSJiri Olsa char *dir, char *name) 2231d9e446bSJiri Olsa { 2241d9e446bSJiri Olsa char path[PATH_MAX]; 2251d9e446bSJiri Olsa int fd; 2261d9e446bSJiri Olsa 22711a64a05SBen Hutchings scnprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); 2281d9e446bSJiri Olsa 2291d9e446bSJiri Olsa fd = open(path, O_RDONLY); 2301d9e446bSJiri Olsa if (fd == -1) 2311d9e446bSJiri Olsa return -1; 2321d9e446bSJiri Olsa 2331d9e446bSJiri Olsa alias->snapshot = true; 2341d9e446bSJiri Olsa close(fd); 2351d9e446bSJiri Olsa return 0; 2361d9e446bSJiri Olsa } 2371d9e446bSJiri Olsa 2386dde6429SThomas Richter static void perf_pmu_assign_str(char *name, const char *field, char **old_str, 2396dde6429SThomas Richter char **new_str) 2406dde6429SThomas Richter { 2416dde6429SThomas Richter if (!*old_str) 2426dde6429SThomas Richter goto set_new; 2436dde6429SThomas Richter 2446dde6429SThomas Richter if (*new_str) { /* Have new string, check with old */ 2456dde6429SThomas Richter if (strcasecmp(*old_str, *new_str)) 2466dde6429SThomas Richter pr_debug("alias %s differs in field '%s'\n", 2476dde6429SThomas Richter name, field); 2486dde6429SThomas Richter zfree(old_str); 2496dde6429SThomas Richter } else /* Nothing new --> keep old string */ 2506dde6429SThomas Richter return; 2516dde6429SThomas Richter set_new: 2526dde6429SThomas Richter *old_str = *new_str; 2536dde6429SThomas Richter *new_str = NULL; 2546dde6429SThomas Richter } 2556dde6429SThomas Richter 2566dde6429SThomas Richter static void perf_pmu_update_alias(struct perf_pmu_alias *old, 2576dde6429SThomas Richter struct perf_pmu_alias *newalias) 2586dde6429SThomas Richter { 2596dde6429SThomas Richter perf_pmu_assign_str(old->name, "desc", &old->desc, &newalias->desc); 2606dde6429SThomas Richter perf_pmu_assign_str(old->name, "long_desc", &old->long_desc, 2616dde6429SThomas Richter &newalias->long_desc); 2626dde6429SThomas Richter perf_pmu_assign_str(old->name, "topic", &old->topic, &newalias->topic); 2636dde6429SThomas Richter perf_pmu_assign_str(old->name, "metric_expr", &old->metric_expr, 2646dde6429SThomas Richter &newalias->metric_expr); 2656dde6429SThomas Richter perf_pmu_assign_str(old->name, "metric_name", &old->metric_name, 2666dde6429SThomas Richter &newalias->metric_name); 2676dde6429SThomas Richter perf_pmu_assign_str(old->name, "value", &old->str, &newalias->str); 2686dde6429SThomas Richter old->scale = newalias->scale; 2696dde6429SThomas Richter old->per_pkg = newalias->per_pkg; 2706dde6429SThomas Richter old->snapshot = newalias->snapshot; 2716dde6429SThomas Richter memcpy(old->unit, newalias->unit, sizeof(old->unit)); 2726dde6429SThomas Richter } 2736dde6429SThomas Richter 2746dde6429SThomas Richter /* Delete an alias entry. */ 2756dde6429SThomas Richter static void perf_pmu_free_alias(struct perf_pmu_alias *newalias) 2766dde6429SThomas Richter { 2776dde6429SThomas Richter zfree(&newalias->name); 2786dde6429SThomas Richter zfree(&newalias->desc); 2796dde6429SThomas Richter zfree(&newalias->long_desc); 2806dde6429SThomas Richter zfree(&newalias->topic); 2816dde6429SThomas Richter zfree(&newalias->str); 2826dde6429SThomas Richter zfree(&newalias->metric_expr); 2836dde6429SThomas Richter zfree(&newalias->metric_name); 2846dde6429SThomas Richter parse_events_terms__purge(&newalias->terms); 2856dde6429SThomas Richter free(newalias); 2866dde6429SThomas Richter } 2876dde6429SThomas Richter 2886dde6429SThomas Richter /* Merge an alias, search in alias list. If this name is already 2896dde6429SThomas Richter * present merge both of them to combine all information. 2906dde6429SThomas Richter */ 2916dde6429SThomas Richter static bool perf_pmu_merge_alias(struct perf_pmu_alias *newalias, 2926dde6429SThomas Richter struct list_head *alist) 2936dde6429SThomas Richter { 2946dde6429SThomas Richter struct perf_pmu_alias *a; 2956dde6429SThomas Richter 2966dde6429SThomas Richter list_for_each_entry(a, alist, list) { 2976dde6429SThomas Richter if (!strcasecmp(newalias->name, a->name)) { 2986dde6429SThomas Richter perf_pmu_update_alias(a, newalias); 2996dde6429SThomas Richter perf_pmu_free_alias(newalias); 3006dde6429SThomas Richter return true; 3016dde6429SThomas Richter } 3026dde6429SThomas Richter } 3036dde6429SThomas Richter return false; 3046dde6429SThomas Richter } 3056dde6429SThomas Richter 30670c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, 307fedb2b51SAndi Kleen char *desc, char *val, 308fedb2b51SAndi Kleen char *long_desc, char *topic, 30900636c3bSAndi Kleen char *unit, char *perpkg, 31096284814SAndi Kleen char *metric_expr, 311a7f6c8c8SJin Yao char *metric_name, 312a7f6c8c8SJin Yao char *deprecated) 313a6146d50SZheng Yan { 3140c24d6fbSThomas Richter struct parse_events_term *term; 3155c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 316a6146d50SZheng Yan int ret; 317fedb2b51SAndi Kleen int num; 3180c24d6fbSThomas Richter char newval[256]; 319a6146d50SZheng Yan 320a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 321a6146d50SZheng Yan if (!alias) 322a6146d50SZheng Yan return -ENOMEM; 323a6146d50SZheng Yan 324a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 325410136f5SStephane Eranian alias->scale = 1.0; 326410136f5SStephane Eranian alias->unit[0] = '\0'; 327044330c1SMatt Fleming alias->per_pkg = false; 32884530920SStephane Eranian alias->snapshot = false; 329a7f6c8c8SJin Yao alias->deprecated = false; 330410136f5SStephane Eranian 33170c646e0SSukadev Bhattiprolu ret = parse_events_terms(&alias->terms, val); 332a6146d50SZheng Yan if (ret) { 33370c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 334a6146d50SZheng Yan free(alias); 335a6146d50SZheng Yan return ret; 336a6146d50SZheng Yan } 337a6146d50SZheng Yan 3380c24d6fbSThomas Richter /* Scan event and remove leading zeroes, spaces, newlines, some 3390c24d6fbSThomas Richter * platforms have terms specified as 3400c24d6fbSThomas Richter * event=0x0091 (read from files ../<PMU>/events/<FILE> 3410c24d6fbSThomas Richter * and terms specified as event=0x91 (read from JSON files). 3420c24d6fbSThomas Richter * 3430c24d6fbSThomas Richter * Rebuild string to make alias->str member comparable. 3440c24d6fbSThomas Richter */ 3450c24d6fbSThomas Richter memset(newval, 0, sizeof(newval)); 3460c24d6fbSThomas Richter ret = 0; 3470c24d6fbSThomas Richter list_for_each_entry(term, &alias->terms, list) { 3480c24d6fbSThomas Richter if (ret) 3490c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3500c24d6fbSThomas Richter ","); 3510c24d6fbSThomas Richter if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 3520c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3530c24d6fbSThomas Richter "%s=%#x", term->config, term->val.num); 3540c24d6fbSThomas Richter else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 3550c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 3560c24d6fbSThomas Richter "%s=%s", term->config, term->val.str); 3570c24d6fbSThomas Richter } 3580c24d6fbSThomas Richter 359a6146d50SZheng Yan alias->name = strdup(name); 36070c646e0SSukadev Bhattiprolu if (dir) { 361410136f5SStephane Eranian /* 362410136f5SStephane Eranian * load unit name and scale if available 363410136f5SStephane Eranian */ 364410136f5SStephane Eranian perf_pmu__parse_unit(alias, dir, name); 365410136f5SStephane Eranian perf_pmu__parse_scale(alias, dir, name); 366044330c1SMatt Fleming perf_pmu__parse_per_pkg(alias, dir, name); 3671d9e446bSJiri Olsa perf_pmu__parse_snapshot(alias, dir, name); 36870c646e0SSukadev Bhattiprolu } 369410136f5SStephane Eranian 37000636c3bSAndi Kleen alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL; 37196284814SAndi Kleen alias->metric_name = metric_name ? strdup(metric_name): NULL; 37208e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 373c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 374c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 375dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 376fedb2b51SAndi Kleen if (unit) { 377a55ab7c4SJin Yao if (perf_pmu__convert_scale(unit, &unit, &alias->scale) < 0) 378fedb2b51SAndi Kleen return -1; 379fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 380fedb2b51SAndi Kleen } 381fedb2b51SAndi Kleen alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1; 3820c24d6fbSThomas Richter alias->str = strdup(newval); 383f2361024SAndi Kleen 384a7f6c8c8SJin Yao if (deprecated) 385a7f6c8c8SJin Yao alias->deprecated = true; 386a7f6c8c8SJin Yao 3876dde6429SThomas Richter if (!perf_pmu_merge_alias(alias, list)) 388a6146d50SZheng Yan list_add_tail(&alias->list, list); 389410136f5SStephane Eranian 390a6146d50SZheng Yan return 0; 391a6146d50SZheng Yan } 392a6146d50SZheng Yan 39370c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) 39470c646e0SSukadev Bhattiprolu { 39570c646e0SSukadev Bhattiprolu char buf[256]; 39670c646e0SSukadev Bhattiprolu int ret; 39770c646e0SSukadev Bhattiprolu 39870c646e0SSukadev Bhattiprolu ret = fread(buf, 1, sizeof(buf), file); 39970c646e0SSukadev Bhattiprolu if (ret == 0) 40070c646e0SSukadev Bhattiprolu return -EINVAL; 40170c646e0SSukadev Bhattiprolu 40270c646e0SSukadev Bhattiprolu buf[ret] = 0; 40370c646e0SSukadev Bhattiprolu 404ea23ac73SThomas Richter /* Remove trailing newline from sysfs file */ 40513c230abSArnaldo Carvalho de Melo strim(buf); 406ea23ac73SThomas Richter 407fedb2b51SAndi Kleen return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL, 408a7f6c8c8SJin Yao NULL, NULL, NULL, NULL); 40970c646e0SSukadev Bhattiprolu } 41070c646e0SSukadev Bhattiprolu 41146441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 41246441bdcSMatt Fleming { 41346441bdcSMatt Fleming size_t len; 41446441bdcSMatt Fleming 41546441bdcSMatt Fleming len = strlen(name); 41646441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 41746441bdcSMatt Fleming return true; 41846441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 41946441bdcSMatt Fleming return true; 420044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 421044330c1SMatt Fleming return true; 4221d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 4231d9e446bSJiri Olsa return true; 42446441bdcSMatt Fleming 42546441bdcSMatt Fleming return false; 42646441bdcSMatt Fleming } 42746441bdcSMatt Fleming 428a6146d50SZheng Yan /* 429a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 430a6146d50SZheng Yan * specified in 'dir' parameter. 431a6146d50SZheng Yan */ 432a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head) 433a6146d50SZheng Yan { 434a6146d50SZheng Yan struct dirent *evt_ent; 435a6146d50SZheng Yan DIR *event_dir; 436a6146d50SZheng Yan 437a6146d50SZheng Yan event_dir = opendir(dir); 438a6146d50SZheng Yan if (!event_dir) 439a6146d50SZheng Yan return -EINVAL; 440a6146d50SZheng Yan 441940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 442a6146d50SZheng Yan char path[PATH_MAX]; 443a6146d50SZheng Yan char *name = evt_ent->d_name; 444a6146d50SZheng Yan FILE *file; 445a6146d50SZheng Yan 446a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 447a6146d50SZheng Yan continue; 448a6146d50SZheng Yan 449410136f5SStephane Eranian /* 45046441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 451410136f5SStephane Eranian */ 45246441bdcSMatt Fleming if (pmu_alias_info_file(name)) 453410136f5SStephane Eranian continue; 454410136f5SStephane Eranian 45577f18153SJiri Olsa scnprintf(path, PATH_MAX, "%s/%s", dir, name); 456a6146d50SZheng Yan 457a6146d50SZheng Yan file = fopen(path, "r"); 458940db6dcSAndi Kleen if (!file) { 459940db6dcSAndi Kleen pr_debug("Cannot open %s\n", path); 460940db6dcSAndi Kleen continue; 461940db6dcSAndi Kleen } 462410136f5SStephane Eranian 463940db6dcSAndi Kleen if (perf_pmu__new_alias(head, dir, name, file) < 0) 464940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 465a6146d50SZheng Yan fclose(file); 466a6146d50SZheng Yan } 467a6146d50SZheng Yan 468a6146d50SZheng Yan closedir(event_dir); 469940db6dcSAndi Kleen return 0; 470a6146d50SZheng Yan } 471a6146d50SZheng Yan 472a6146d50SZheng Yan /* 473a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 474a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 475a6146d50SZheng Yan */ 476b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head) 477a6146d50SZheng Yan { 478a6146d50SZheng Yan struct stat st; 479a6146d50SZheng Yan char path[PATH_MAX]; 480cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 481a6146d50SZheng Yan 482a6146d50SZheng Yan if (!sysfs) 483a6146d50SZheng Yan return -1; 484a6146d50SZheng Yan 485a6146d50SZheng Yan snprintf(path, PATH_MAX, 486a6146d50SZheng Yan "%s/bus/event_source/devices/%s/events", sysfs, name); 487a6146d50SZheng Yan 488a6146d50SZheng Yan if (stat(path, &st) < 0) 4893fded963SJiri Olsa return 0; /* no error if 'events' does not exist */ 490a6146d50SZheng Yan 491a6146d50SZheng Yan if (pmu_aliases_parse(path, head)) 492a6146d50SZheng Yan return -1; 493a6146d50SZheng Yan 494a6146d50SZheng Yan return 0; 495a6146d50SZheng Yan } 496a6146d50SZheng Yan 4975c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 498a6146d50SZheng Yan struct list_head *terms) 499a6146d50SZheng Yan { 5007c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 501a6146d50SZheng Yan LIST_HEAD(list); 502a6146d50SZheng Yan int ret; 503a6146d50SZheng Yan 504a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 5057c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 506a6146d50SZheng Yan if (ret) { 507682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 508a6146d50SZheng Yan return ret; 509a6146d50SZheng Yan } 510c2f1ceadSAndi Kleen /* 511c2f1ceadSAndi Kleen * Weak terms don't override command line options, 512c2f1ceadSAndi Kleen * which we don't want for implicit terms in aliases. 513c2f1ceadSAndi Kleen */ 514c2f1ceadSAndi Kleen cloned->weak = true; 5157c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 516a6146d50SZheng Yan } 517a6146d50SZheng Yan list_splice(&list, terms); 518a6146d50SZheng Yan return 0; 519a6146d50SZheng Yan } 520a6146d50SZheng Yan 521cd82a32eSJiri Olsa /* 522cd82a32eSJiri Olsa * Reading/parsing the default pmu type value, which should be 523cd82a32eSJiri Olsa * located at: 524cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 525cd82a32eSJiri Olsa */ 526b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type) 527cd82a32eSJiri Olsa { 528cd82a32eSJiri Olsa struct stat st; 529cd82a32eSJiri Olsa char path[PATH_MAX]; 530cd82a32eSJiri Olsa FILE *file; 531cd82a32eSJiri Olsa int ret = 0; 532cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 533cd82a32eSJiri Olsa 534cd82a32eSJiri Olsa if (!sysfs) 535cd82a32eSJiri Olsa return -1; 536cd82a32eSJiri Olsa 537cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 53850a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); 539cd82a32eSJiri Olsa 540cd82a32eSJiri Olsa if (stat(path, &st) < 0) 541cd82a32eSJiri Olsa return -1; 542cd82a32eSJiri Olsa 543cd82a32eSJiri Olsa file = fopen(path, "r"); 544cd82a32eSJiri Olsa if (!file) 545cd82a32eSJiri Olsa return -EINVAL; 546cd82a32eSJiri Olsa 547cd82a32eSJiri Olsa if (1 != fscanf(file, "%u", type)) 548cd82a32eSJiri Olsa ret = -1; 549cd82a32eSJiri Olsa 550cd82a32eSJiri Olsa fclose(file); 551cd82a32eSJiri Olsa return ret; 552cd82a32eSJiri Olsa } 553cd82a32eSJiri Olsa 55450a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */ 55550a9667cSRobert Richter static void pmu_read_sysfs(void) 55650a9667cSRobert Richter { 55750a9667cSRobert Richter char path[PATH_MAX]; 55850a9667cSRobert Richter DIR *dir; 55950a9667cSRobert Richter struct dirent *dent; 560cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 56150a9667cSRobert Richter 56250a9667cSRobert Richter if (!sysfs) 56350a9667cSRobert Richter return; 56450a9667cSRobert Richter 56550a9667cSRobert Richter snprintf(path, PATH_MAX, 56650a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 56750a9667cSRobert Richter 56850a9667cSRobert Richter dir = opendir(path); 56950a9667cSRobert Richter if (!dir) 57050a9667cSRobert Richter return; 57150a9667cSRobert Richter 57250a9667cSRobert Richter while ((dent = readdir(dir))) { 57350a9667cSRobert Richter if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 57450a9667cSRobert Richter continue; 57550a9667cSRobert Richter /* add to static LIST_HEAD(pmus): */ 57650a9667cSRobert Richter perf_pmu__find(dent->d_name); 57750a9667cSRobert Richter } 57850a9667cSRobert Richter 57950a9667cSRobert Richter closedir(dir); 58050a9667cSRobert Richter } 58150a9667cSRobert Richter 582f854839bSJiri Olsa static struct perf_cpu_map *__pmu_cpumask(const char *path) 58366ec1191SMark Rutland { 58466ec1191SMark Rutland FILE *file; 585f854839bSJiri Olsa struct perf_cpu_map *cpus; 58666ec1191SMark Rutland 58766ec1191SMark Rutland file = fopen(path, "r"); 58866ec1191SMark Rutland if (!file) 58966ec1191SMark Rutland return NULL; 59066ec1191SMark Rutland 5919c3516d1SJiri Olsa cpus = perf_cpu_map__read(file); 59266ec1191SMark Rutland fclose(file); 59366ec1191SMark Rutland return cpus; 59466ec1191SMark Rutland } 59566ec1191SMark Rutland 59666ec1191SMark Rutland /* 59766ec1191SMark Rutland * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 59866ec1191SMark Rutland * may have a "cpus" file. 59966ec1191SMark Rutland */ 60066ec1191SMark Rutland #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask" 60166ec1191SMark Rutland #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus" 60266ec1191SMark Rutland 603f854839bSJiri Olsa static struct perf_cpu_map *pmu_cpumask(const char *name) 6047ae92e74SYan, Zheng { 6057ae92e74SYan, Zheng char path[PATH_MAX]; 606f854839bSJiri Olsa struct perf_cpu_map *cpus; 607cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 6087e3fcffeSMark Rutland const char *templates[] = { 60966ec1191SMark Rutland CPUS_TEMPLATE_UNCORE, 61066ec1191SMark Rutland CPUS_TEMPLATE_CPU, 6117e3fcffeSMark Rutland NULL 6127e3fcffeSMark Rutland }; 6137e3fcffeSMark Rutland const char **template; 6147ae92e74SYan, Zheng 6157ae92e74SYan, Zheng if (!sysfs) 6167ae92e74SYan, Zheng return NULL; 6177ae92e74SYan, Zheng 6187e3fcffeSMark Rutland for (template = templates; *template; template++) { 6197e3fcffeSMark Rutland snprintf(path, PATH_MAX, *template, sysfs, name); 62066ec1191SMark Rutland cpus = __pmu_cpumask(path); 62166ec1191SMark Rutland if (cpus) 62266ec1191SMark Rutland return cpus; 6237e3fcffeSMark Rutland } 6247ae92e74SYan, Zheng 6257ae92e74SYan, Zheng return NULL; 62666ec1191SMark Rutland } 6277ae92e74SYan, Zheng 62866ec1191SMark Rutland static bool pmu_is_uncore(const char *name) 62966ec1191SMark Rutland { 63066ec1191SMark Rutland char path[PATH_MAX]; 631f854839bSJiri Olsa struct perf_cpu_map *cpus; 63266ec1191SMark Rutland const char *sysfs = sysfs__mountpoint(); 6337ae92e74SYan, Zheng 63466ec1191SMark Rutland snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name); 63566ec1191SMark Rutland cpus = __pmu_cpumask(path); 63638f01d8dSJiri Olsa perf_cpu_map__put(cpus); 63766ec1191SMark Rutland 63866ec1191SMark Rutland return !!cpus; 6397ae92e74SYan, Zheng } 6407ae92e74SYan, Zheng 641933f82ffSSukadev Bhattiprolu /* 64214b22ae0SGanapatrao Kulkarni * PMU CORE devices have different name other than cpu in sysfs on some 643292c34c1SKan Liang * platforms. 644292c34c1SKan Liang * Looking for possible sysfs files to identify the arm core device. 64514b22ae0SGanapatrao Kulkarni */ 646292c34c1SKan Liang static int is_arm_pmu_core(const char *name) 64714b22ae0SGanapatrao Kulkarni { 64814b22ae0SGanapatrao Kulkarni struct stat st; 64914b22ae0SGanapatrao Kulkarni char path[PATH_MAX]; 65014b22ae0SGanapatrao Kulkarni const char *sysfs = sysfs__mountpoint(); 65114b22ae0SGanapatrao Kulkarni 65214b22ae0SGanapatrao Kulkarni if (!sysfs) 65314b22ae0SGanapatrao Kulkarni return 0; 65414b22ae0SGanapatrao Kulkarni 65514b22ae0SGanapatrao Kulkarni /* Look for cpu sysfs (specific to arm) */ 65614b22ae0SGanapatrao Kulkarni scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus", 65714b22ae0SGanapatrao Kulkarni sysfs, name); 65814b22ae0SGanapatrao Kulkarni if (stat(path, &st) == 0) 65914b22ae0SGanapatrao Kulkarni return 1; 66014b22ae0SGanapatrao Kulkarni 66114b22ae0SGanapatrao Kulkarni return 0; 66214b22ae0SGanapatrao Kulkarni } 66314b22ae0SGanapatrao Kulkarni 66454e32dc0SGanapatrao Kulkarni static char *perf_pmu__getcpuid(struct perf_pmu *pmu) 665d77ade9fSAndi Kleen { 666d77ade9fSAndi Kleen char *cpuid; 667d77ade9fSAndi Kleen static bool printed; 668d77ade9fSAndi Kleen 669d77ade9fSAndi Kleen cpuid = getenv("PERF_CPUID"); 670d77ade9fSAndi Kleen if (cpuid) 671d77ade9fSAndi Kleen cpuid = strdup(cpuid); 672d77ade9fSAndi Kleen if (!cpuid) 67354e32dc0SGanapatrao Kulkarni cpuid = get_cpuid_str(pmu); 674d77ade9fSAndi Kleen if (!cpuid) 675d77ade9fSAndi Kleen return NULL; 676d77ade9fSAndi Kleen 677d77ade9fSAndi Kleen if (!printed) { 678d77ade9fSAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 679d77ade9fSAndi Kleen printed = true; 680d77ade9fSAndi Kleen } 681d77ade9fSAndi Kleen return cpuid; 682d77ade9fSAndi Kleen } 683d77ade9fSAndi Kleen 68454e32dc0SGanapatrao Kulkarni struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu) 685d77ade9fSAndi Kleen { 686d77ade9fSAndi Kleen struct pmu_events_map *map; 68754e32dc0SGanapatrao Kulkarni char *cpuid = perf_pmu__getcpuid(pmu); 688d77ade9fSAndi Kleen int i; 689d77ade9fSAndi Kleen 690de3d0f12SGanapatrao Kulkarni /* on some platforms which uses cpus map, cpuid can be NULL for 691de3d0f12SGanapatrao Kulkarni * PMUs other than CORE PMUs. 692de3d0f12SGanapatrao Kulkarni */ 693de3d0f12SGanapatrao Kulkarni if (!cpuid) 694de3d0f12SGanapatrao Kulkarni return NULL; 695de3d0f12SGanapatrao Kulkarni 696d77ade9fSAndi Kleen i = 0; 697d77ade9fSAndi Kleen for (;;) { 698d77ade9fSAndi Kleen map = &pmu_events_map[i++]; 699d77ade9fSAndi Kleen if (!map->table) { 700d77ade9fSAndi Kleen map = NULL; 701d77ade9fSAndi Kleen break; 702d77ade9fSAndi Kleen } 703d77ade9fSAndi Kleen 7044cb7d3ecSThomas Richter if (!strcmp_cpuid_str(map->cpuid, cpuid)) 705d77ade9fSAndi Kleen break; 706d77ade9fSAndi Kleen } 707d77ade9fSAndi Kleen free(cpuid); 708d77ade9fSAndi Kleen return map; 709d77ade9fSAndi Kleen } 710d77ade9fSAndi Kleen 711730670b1SJohn Garry static bool pmu_uncore_alias_match(const char *pmu_name, const char *name) 712730670b1SJohn Garry { 713730670b1SJohn Garry char *tmp = NULL, *tok, *str; 714730670b1SJohn Garry bool res; 715730670b1SJohn Garry 716730670b1SJohn Garry str = strdup(pmu_name); 717730670b1SJohn Garry if (!str) 718730670b1SJohn Garry return false; 719730670b1SJohn Garry 720730670b1SJohn Garry /* 721730670b1SJohn Garry * uncore alias may be from different PMU with common prefix 722730670b1SJohn Garry */ 723730670b1SJohn Garry tok = strtok_r(str, ",", &tmp); 724730670b1SJohn Garry if (strncmp(pmu_name, tok, strlen(tok))) { 725730670b1SJohn Garry res = false; 726730670b1SJohn Garry goto out; 727730670b1SJohn Garry } 728730670b1SJohn Garry 729730670b1SJohn Garry /* 730730670b1SJohn Garry * Match more complex aliases where the alias name is a comma-delimited 731730670b1SJohn Garry * list of tokens, orderly contained in the matching PMU name. 732730670b1SJohn Garry * 733730670b1SJohn Garry * Example: For alias "socket,pmuname" and PMU "socketX_pmunameY", we 734730670b1SJohn Garry * match "socket" in "socketX_pmunameY" and then "pmuname" in 735730670b1SJohn Garry * "pmunameY". 736730670b1SJohn Garry */ 737730670b1SJohn Garry for (; tok; name += strlen(tok), tok = strtok_r(NULL, ",", &tmp)) { 738730670b1SJohn Garry name = strstr(name, tok); 739730670b1SJohn Garry if (!name) { 740730670b1SJohn Garry res = false; 741730670b1SJohn Garry goto out; 742730670b1SJohn Garry } 743730670b1SJohn Garry } 744730670b1SJohn Garry 745730670b1SJohn Garry res = true; 746730670b1SJohn Garry out: 747730670b1SJohn Garry free(str); 748730670b1SJohn Garry return res; 749730670b1SJohn Garry } 750730670b1SJohn Garry 751933f82ffSSukadev Bhattiprolu /* 752933f82ffSSukadev Bhattiprolu * From the pmu_events_map, find the table of PMU events that corresponds 753933f82ffSSukadev Bhattiprolu * to the current running CPU. Then, add all PMU events from that table 754933f82ffSSukadev Bhattiprolu * as aliases. 755933f82ffSSukadev Bhattiprolu */ 75654e32dc0SGanapatrao Kulkarni static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) 757933f82ffSSukadev Bhattiprolu { 758933f82ffSSukadev Bhattiprolu int i; 759933f82ffSSukadev Bhattiprolu struct pmu_events_map *map; 76054e32dc0SGanapatrao Kulkarni const char *name = pmu->name; 761933f82ffSSukadev Bhattiprolu 76254e32dc0SGanapatrao Kulkarni map = perf_pmu__find_map(pmu); 763d77ade9fSAndi Kleen if (!map) 764933f82ffSSukadev Bhattiprolu return; 765933f82ffSSukadev Bhattiprolu 766933f82ffSSukadev Bhattiprolu /* 767933f82ffSSukadev Bhattiprolu * Found a matching PMU events table. Create aliases 768933f82ffSSukadev Bhattiprolu */ 769933f82ffSSukadev Bhattiprolu i = 0; 770933f82ffSSukadev Bhattiprolu while (1) { 771599ee18fSJohn Garry const char *cpu_name = is_arm_pmu_core(name) ? name : "cpu"; 772599ee18fSJohn Garry struct pmu_event *pe = &map->table[i++]; 773599ee18fSJohn Garry const char *pname = pe->pmu ? pe->pmu : cpu_name; 774fedb2b51SAndi Kleen 775b18f3e36SAndi Kleen if (!pe->name) { 776b18f3e36SAndi Kleen if (pe->metric_group || pe->metric_name) 777b18f3e36SAndi Kleen continue; 778933f82ffSSukadev Bhattiprolu break; 779b18f3e36SAndi Kleen } 780933f82ffSSukadev Bhattiprolu 781e94d6b7fSKan Liang if (pmu_is_uncore(name) && 782730670b1SJohn Garry pmu_uncore_alias_match(pname, name)) 783e94d6b7fSKan Liang goto new_alias; 784e94d6b7fSKan Liang 785ea1fa48cSThomas Richter if (strcmp(pname, name)) 78614b22ae0SGanapatrao Kulkarni continue; 787fedb2b51SAndi Kleen 788e94d6b7fSKan Liang new_alias: 789933f82ffSSukadev Bhattiprolu /* need type casts to override 'const' */ 790933f82ffSSukadev Bhattiprolu __perf_pmu__new_alias(head, NULL, (char *)pe->name, 791c8d6828aSSukadev Bhattiprolu (char *)pe->desc, (char *)pe->event, 792fedb2b51SAndi Kleen (char *)pe->long_desc, (char *)pe->topic, 79300636c3bSAndi Kleen (char *)pe->unit, (char *)pe->perpkg, 79496284814SAndi Kleen (char *)pe->metric_expr, 795a7f6c8c8SJin Yao (char *)pe->metric_name, 796a7f6c8c8SJin Yao (char *)pe->deprecated); 797933f82ffSSukadev Bhattiprolu } 798933f82ffSSukadev Bhattiprolu } 799933f82ffSSukadev Bhattiprolu 800c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 801dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 802dc0a6202SAdrian Hunter { 803dc0a6202SAdrian Hunter return NULL; 804dc0a6202SAdrian Hunter } 805dc0a6202SAdrian Hunter 80690a86bdeSJiri Olsa static int pmu_max_precise(const char *name) 80790a86bdeSJiri Olsa { 80890a86bdeSJiri Olsa char path[PATH_MAX]; 80990a86bdeSJiri Olsa int max_precise = -1; 81090a86bdeSJiri Olsa 81190a86bdeSJiri Olsa scnprintf(path, PATH_MAX, 81290a86bdeSJiri Olsa "bus/event_source/devices/%s/caps/max_precise", 81390a86bdeSJiri Olsa name); 81490a86bdeSJiri Olsa 81590a86bdeSJiri Olsa sysfs__read_int(path, &max_precise); 81690a86bdeSJiri Olsa return max_precise; 81790a86bdeSJiri Olsa } 81890a86bdeSJiri Olsa 819b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name) 820cd82a32eSJiri Olsa { 821cd82a32eSJiri Olsa struct perf_pmu *pmu; 822cd82a32eSJiri Olsa LIST_HEAD(format); 823a6146d50SZheng Yan LIST_HEAD(aliases); 824cd82a32eSJiri Olsa __u32 type; 825cd82a32eSJiri Olsa 826cd82a32eSJiri Olsa /* 827cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 828cd82a32eSJiri Olsa * type value and format definitions. Load both right 829cd82a32eSJiri Olsa * now. 830cd82a32eSJiri Olsa */ 831cd82a32eSJiri Olsa if (pmu_format(name, &format)) 832cd82a32eSJiri Olsa return NULL; 833cd82a32eSJiri Olsa 83415b22ed3SAndi Kleen /* 83515b22ed3SAndi Kleen * Check the type first to avoid unnecessary work. 83615b22ed3SAndi Kleen */ 83715b22ed3SAndi Kleen if (pmu_type(name, &type)) 83815b22ed3SAndi Kleen return NULL; 83915b22ed3SAndi Kleen 8403fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 8413fded963SJiri Olsa return NULL; 8423fded963SJiri Olsa 843cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 844cd82a32eSJiri Olsa if (!pmu) 845cd82a32eSJiri Olsa return NULL; 846cd82a32eSJiri Olsa 8477ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 84854e32dc0SGanapatrao Kulkarni pmu->name = strdup(name); 84954e32dc0SGanapatrao Kulkarni pmu->type = type; 85066ec1191SMark Rutland pmu->is_uncore = pmu_is_uncore(name); 85190a86bdeSJiri Olsa pmu->max_precise = pmu_max_precise(name); 85254e32dc0SGanapatrao Kulkarni pmu_add_cpu_aliases(&aliases, pmu); 85366ec1191SMark Rutland 854cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 855a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 856cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 857a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 8589bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 859dc0a6202SAdrian Hunter 860dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 861dc0a6202SAdrian Hunter 862cd82a32eSJiri Olsa return pmu; 863cd82a32eSJiri Olsa } 864cd82a32eSJiri Olsa 865b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 866cd82a32eSJiri Olsa { 867cd82a32eSJiri Olsa struct perf_pmu *pmu; 868cd82a32eSJiri Olsa 869cd82a32eSJiri Olsa list_for_each_entry(pmu, &pmus, list) 870cd82a32eSJiri Olsa if (!strcmp(pmu->name, name)) 871cd82a32eSJiri Olsa return pmu; 872cd82a32eSJiri Olsa 873cd82a32eSJiri Olsa return NULL; 874cd82a32eSJiri Olsa } 875cd82a32eSJiri Olsa 87650a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 87750a9667cSRobert Richter { 87850a9667cSRobert Richter /* 87950a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 88050a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 88150a9667cSRobert Richter */ 88250a9667cSRobert Richter if (!pmu) { 88350a9667cSRobert Richter pmu_read_sysfs(); 88450a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 88550a9667cSRobert Richter } 88650a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 88750a9667cSRobert Richter return pmu; 88850a9667cSRobert Richter return NULL; 88950a9667cSRobert Richter } 89050a9667cSRobert Richter 891b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 892cd82a32eSJiri Olsa { 893cd82a32eSJiri Olsa struct perf_pmu *pmu; 894cd82a32eSJiri Olsa 895cd82a32eSJiri Olsa /* 896cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 897cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 898cd82a32eSJiri Olsa * the pmu format definitions. 899cd82a32eSJiri Olsa */ 900cd82a32eSJiri Olsa pmu = pmu_find(name); 901cd82a32eSJiri Olsa if (pmu) 902cd82a32eSJiri Olsa return pmu; 903cd82a32eSJiri Olsa 904cd82a32eSJiri Olsa return pmu_lookup(name); 905cd82a32eSJiri Olsa } 906cd82a32eSJiri Olsa 9075c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 90809ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 909cd82a32eSJiri Olsa { 9105c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 911cd82a32eSJiri Olsa 912cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 913cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 914cd82a32eSJiri Olsa return format; 915cd82a32eSJiri Olsa 916cd82a32eSJiri Olsa return NULL; 917cd82a32eSJiri Olsa } 918cd82a32eSJiri Olsa 91909ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 92009ff6071SAdrian Hunter { 92109ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 92209ff6071SAdrian Hunter __u64 bits = 0; 92309ff6071SAdrian Hunter int fbit; 92409ff6071SAdrian Hunter 92509ff6071SAdrian Hunter if (!format) 92609ff6071SAdrian Hunter return 0; 92709ff6071SAdrian Hunter 92809ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 92909ff6071SAdrian Hunter bits |= 1ULL << fbit; 93009ff6071SAdrian Hunter 93109ff6071SAdrian Hunter return bits; 93209ff6071SAdrian Hunter } 93309ff6071SAdrian Hunter 934cd82a32eSJiri Olsa /* 935dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 936cd82a32eSJiri Olsa * and unformated value (value parameter). 937cd82a32eSJiri Olsa */ 938dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 939dc0a6202SAdrian Hunter bool zero) 940cd82a32eSJiri Olsa { 941cd82a32eSJiri Olsa unsigned long fbit, vbit; 942cd82a32eSJiri Olsa 943cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 944cd82a32eSJiri Olsa 945cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 946cd82a32eSJiri Olsa continue; 947cd82a32eSJiri Olsa 948dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 949dc0a6202SAdrian Hunter *v |= (1llu << fbit); 950dc0a6202SAdrian Hunter else if (zero) 951dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 952cd82a32eSJiri Olsa } 953cd82a32eSJiri Olsa } 954cd82a32eSJiri Olsa 9550efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 9560efe6b67SAdrian Hunter { 9571b9caa10SJiri Olsa int w; 9580efe6b67SAdrian Hunter 9591b9caa10SJiri Olsa w = bitmap_weight(format, PERF_PMU_FORMAT_BITS); 9601b9caa10SJiri Olsa if (!w) 9611b9caa10SJiri Olsa return 0; 9621b9caa10SJiri Olsa if (w < 64) 9631b9caa10SJiri Olsa return (1ULL << w) - 1; 9641b9caa10SJiri Olsa return -1; 9650efe6b67SAdrian Hunter } 9660efe6b67SAdrian Hunter 967cd82a32eSJiri Olsa /* 968688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 969688d4dfcSCody P Schafer * in the remaining terms. 970688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 971688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 972688d4dfcSCody P Schafer * in a config string) later on in the term list. 973688d4dfcSCody P Schafer */ 974688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 975688d4dfcSCody P Schafer struct list_head *head_terms, 976688d4dfcSCody P Schafer __u64 *value) 977688d4dfcSCody P Schafer { 978688d4dfcSCody P Schafer struct parse_events_term *t; 979688d4dfcSCody P Schafer 980688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 981688d4dfcSCody P Schafer if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 982688d4dfcSCody P Schafer if (!strcmp(t->config, term->config)) { 983688d4dfcSCody P Schafer t->used = true; 984688d4dfcSCody P Schafer *value = t->val.num; 985688d4dfcSCody P Schafer return 0; 986688d4dfcSCody P Schafer } 987688d4dfcSCody P Schafer } 988688d4dfcSCody P Schafer } 989688d4dfcSCody P Schafer 990bb963e16SNamhyung Kim if (verbose > 0) 991688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 992688d4dfcSCody P Schafer 993688d4dfcSCody P Schafer return -1; 994688d4dfcSCody P Schafer } 995688d4dfcSCody P Schafer 996ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 997e64b020bSJiri Olsa { 998e64b020bSJiri Olsa struct perf_pmu_format *format; 99911db4e29SMasami Hiramatsu char *str = NULL; 100011db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 1001e64b020bSJiri Olsa unsigned i = 0; 1002e64b020bSJiri Olsa 1003ffeb883eSHe Kuang if (!formats) 1004e64b020bSJiri Olsa return NULL; 1005e64b020bSJiri Olsa 1006e64b020bSJiri Olsa /* sysfs exported terms */ 1007ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 100811db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 100911db4e29SMasami Hiramatsu goto error; 1010e64b020bSJiri Olsa 1011ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 101211db4e29SMasami Hiramatsu error: 1013ffeb883eSHe Kuang strbuf_release(&buf); 1014e64b020bSJiri Olsa 1015e64b020bSJiri Olsa return str; 1016e64b020bSJiri Olsa } 1017e64b020bSJiri Olsa 1018688d4dfcSCody P Schafer /* 1019cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 102088aca8d9SCody P Schafer * user input data - term parameter. 1021cd82a32eSJiri Olsa */ 1022cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats, 1023cd82a32eSJiri Olsa struct perf_event_attr *attr, 1024dc0a6202SAdrian Hunter struct parse_events_term *term, 1025688d4dfcSCody P Schafer struct list_head *head_terms, 1026e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1027cd82a32eSJiri Olsa { 10285c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1029cd82a32eSJiri Olsa __u64 *vp; 10300efe6b67SAdrian Hunter __u64 val, max_val; 1031cd82a32eSJiri Olsa 1032cd82a32eSJiri Olsa /* 1033688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 1034688d4dfcSCody P Schafer * skip it in normal eval. 1035688d4dfcSCody P Schafer */ 1036688d4dfcSCody P Schafer if (term->used) 1037688d4dfcSCody P Schafer return 0; 1038688d4dfcSCody P Schafer 1039688d4dfcSCody P Schafer /* 1040cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 1041cd82a32eSJiri Olsa * to be done for them. 1042cd82a32eSJiri Olsa */ 1043cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 1044cd82a32eSJiri Olsa return 0; 1045cd82a32eSJiri Olsa 1046cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 1047688d4dfcSCody P Schafer if (!format) { 1048bb963e16SNamhyung Kim if (verbose > 0) 1049688d4dfcSCody P Schafer printf("Invalid event/parameter '%s'\n", term->config); 1050e64b020bSJiri Olsa if (err) { 1051ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 1052ffeb883eSHe Kuang 1053448d732cSIan Rogers parse_events__handle_error(err, term->err_term, 1054448d732cSIan Rogers strdup("unknown term"), 1055448d732cSIan Rogers parse_events_formats_error_string(pmu_term)); 1056ffeb883eSHe Kuang free(pmu_term); 1057e64b020bSJiri Olsa } 1058cd82a32eSJiri Olsa return -EINVAL; 1059688d4dfcSCody P Schafer } 1060cd82a32eSJiri Olsa 1061cd82a32eSJiri Olsa switch (format->value) { 1062cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 1063cd82a32eSJiri Olsa vp = &attr->config; 1064cd82a32eSJiri Olsa break; 1065cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 1066cd82a32eSJiri Olsa vp = &attr->config1; 1067cd82a32eSJiri Olsa break; 1068cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 1069cd82a32eSJiri Olsa vp = &attr->config2; 1070cd82a32eSJiri Olsa break; 1071cd82a32eSJiri Olsa default: 1072cd82a32eSJiri Olsa return -EINVAL; 1073cd82a32eSJiri Olsa } 1074cd82a32eSJiri Olsa 107516fa7e82SJiri Olsa /* 1076688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 1077688d4dfcSCody P Schafer * using event parameters. 107816fa7e82SJiri Olsa */ 107999e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 108099e7138eSJiri Olsa if (term->no_value && 108199e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 108299e7138eSJiri Olsa if (err) { 1083448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1084448d732cSIan Rogers strdup("no value assigned for term"), 1085448d732cSIan Rogers NULL); 108699e7138eSJiri Olsa } 108799e7138eSJiri Olsa return -EINVAL; 108899e7138eSJiri Olsa } 108999e7138eSJiri Olsa 1090688d4dfcSCody P Schafer val = term->val.num; 109199e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1092688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1093bb963e16SNamhyung Kim if (verbose > 0) { 1094688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1095688d4dfcSCody P Schafer term->config, term->val.str); 1096e64b020bSJiri Olsa } 1097e64b020bSJiri Olsa if (err) { 1098448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1099448d732cSIan Rogers strdup("expected numeric value"), 1100448d732cSIan Rogers NULL); 1101e64b020bSJiri Olsa } 1102688d4dfcSCody P Schafer return -EINVAL; 1103688d4dfcSCody P Schafer } 1104688d4dfcSCody P Schafer 1105688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1106688d4dfcSCody P Schafer return -EINVAL; 1107688d4dfcSCody P Schafer } else 1108688d4dfcSCody P Schafer return -EINVAL; 1109688d4dfcSCody P Schafer 11100efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 11110efe6b67SAdrian Hunter if (val > max_val) { 11120efe6b67SAdrian Hunter if (err) { 1113448d732cSIan Rogers char *err_str; 1114448d732cSIan Rogers 1115448d732cSIan Rogers parse_events__handle_error(err, term->err_val, 1116448d732cSIan Rogers asprintf(&err_str, 11170efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 1118448d732cSIan Rogers (unsigned long long)max_val) < 0 1119448d732cSIan Rogers ? strdup("value too big for format") 1120448d732cSIan Rogers : err_str, 1121448d732cSIan Rogers NULL); 11220efe6b67SAdrian Hunter return -EINVAL; 11230efe6b67SAdrian Hunter } 11240efe6b67SAdrian Hunter /* 11250efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 11260efe6b67SAdrian Hunter * silently truncated. 11270efe6b67SAdrian Hunter */ 11280efe6b67SAdrian Hunter } 11290efe6b67SAdrian Hunter 1130688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1131cd82a32eSJiri Olsa return 0; 1132cd82a32eSJiri Olsa } 1133cd82a32eSJiri Olsa 1134cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats, 1135cff7f956SJiri Olsa struct perf_event_attr *attr, 1136dc0a6202SAdrian Hunter struct list_head *head_terms, 1137e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1138cd82a32eSJiri Olsa { 11396cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1140cd82a32eSJiri Olsa 1141688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 1142e64b020bSJiri Olsa if (pmu_config_term(formats, attr, term, head_terms, 1143e64b020bSJiri Olsa zero, err)) 1144cd82a32eSJiri Olsa return -EINVAL; 1145688d4dfcSCody P Schafer } 1146cd82a32eSJiri Olsa 1147cd82a32eSJiri Olsa return 0; 1148cd82a32eSJiri Olsa } 1149cd82a32eSJiri Olsa 1150cd82a32eSJiri Olsa /* 1151cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1152cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1153cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1154cd82a32eSJiri Olsa */ 1155cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1156e64b020bSJiri Olsa struct list_head *head_terms, 1157e64b020bSJiri Olsa struct parse_events_error *err) 1158cd82a32eSJiri Olsa { 1159dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1160dc0a6202SAdrian Hunter 1161cd82a32eSJiri Olsa attr->type = pmu->type; 1162e64b020bSJiri Olsa return perf_pmu__config_terms(&pmu->format, attr, head_terms, 1163e64b020bSJiri Olsa zero, err); 1164cd82a32eSJiri Olsa } 1165cd82a32eSJiri Olsa 11665c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 11676cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1168a6146d50SZheng Yan { 11695c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1170a6146d50SZheng Yan char *name; 1171a6146d50SZheng Yan 1172a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1173a6146d50SZheng Yan return NULL; 1174a6146d50SZheng Yan 1175a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1176a6146d50SZheng Yan if (term->val.num != 1) 1177a6146d50SZheng Yan return NULL; 1178a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1179a6146d50SZheng Yan return NULL; 1180a6146d50SZheng Yan name = term->config; 1181a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1182a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1183a6146d50SZheng Yan return NULL; 1184a6146d50SZheng Yan name = term->val.str; 1185a6146d50SZheng Yan } else { 1186a6146d50SZheng Yan return NULL; 1187a6146d50SZheng Yan } 1188a6146d50SZheng Yan 1189a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 1190a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 1191a6146d50SZheng Yan return alias; 1192a6146d50SZheng Yan } 1193a6146d50SZheng Yan return NULL; 1194a6146d50SZheng Yan } 1195a6146d50SZheng Yan 1196410136f5SStephane Eranian 11971d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 11981d9e446bSJiri Olsa struct perf_pmu_info *info) 1199410136f5SStephane Eranian { 1200410136f5SStephane Eranian /* 1201410136f5SStephane Eranian * Only one term in event definition can 12021d9e446bSJiri Olsa * define unit, scale and snapshot, fail 12031d9e446bSJiri Olsa * if there's more than one. 1204410136f5SStephane Eranian */ 1205b30a7d1fSArnaldo Carvalho de Melo if ((info->unit && alias->unit[0]) || 12061d9e446bSJiri Olsa (info->scale && alias->scale) || 12071d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 1208410136f5SStephane Eranian return -EINVAL; 1209410136f5SStephane Eranian 1210b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 12111d9e446bSJiri Olsa info->unit = alias->unit; 1212410136f5SStephane Eranian 1213410136f5SStephane Eranian if (alias->scale) 12141d9e446bSJiri Olsa info->scale = alias->scale; 12151d9e446bSJiri Olsa 12161d9e446bSJiri Olsa if (alias->snapshot) 12171d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1218410136f5SStephane Eranian 1219410136f5SStephane Eranian return 0; 1220410136f5SStephane Eranian } 1221410136f5SStephane Eranian 1222a6146d50SZheng Yan /* 1223a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1224a6146d50SZheng Yan * defined for the alias 1225a6146d50SZheng Yan */ 1226410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 122746441bdcSMatt Fleming struct perf_pmu_info *info) 1228a6146d50SZheng Yan { 12296cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 12305c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1231a6146d50SZheng Yan int ret; 1232a6146d50SZheng Yan 1233044330c1SMatt Fleming info->per_pkg = false; 1234044330c1SMatt Fleming 12358a398897SStephane Eranian /* 12368a398897SStephane Eranian * Mark unit and scale as not set 12378a398897SStephane Eranian * (different from default values, see below) 12388a398897SStephane Eranian */ 123946441bdcSMatt Fleming info->unit = NULL; 124046441bdcSMatt Fleming info->scale = 0.0; 12411d9e446bSJiri Olsa info->snapshot = false; 124237932c18SAndi Kleen info->metric_expr = NULL; 124396284814SAndi Kleen info->metric_name = NULL; 1244410136f5SStephane Eranian 1245a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1246a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1247a6146d50SZheng Yan if (!alias) 1248a6146d50SZheng Yan continue; 1249a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 1250a6146d50SZheng Yan if (ret) 1251a6146d50SZheng Yan return ret; 1252410136f5SStephane Eranian 12531d9e446bSJiri Olsa ret = check_info_data(alias, info); 1254410136f5SStephane Eranian if (ret) 1255410136f5SStephane Eranian return ret; 1256410136f5SStephane Eranian 1257044330c1SMatt Fleming if (alias->per_pkg) 1258044330c1SMatt Fleming info->per_pkg = true; 125937932c18SAndi Kleen info->metric_expr = alias->metric_expr; 126096284814SAndi Kleen info->metric_name = alias->metric_name; 1261044330c1SMatt Fleming 1262e56fbc9dSArnaldo Carvalho de Melo list_del_init(&term->list); 1263a6146d50SZheng Yan free(term); 1264a6146d50SZheng Yan } 12658a398897SStephane Eranian 12668a398897SStephane Eranian /* 12678a398897SStephane Eranian * if no unit or scale foundin aliases, then 12688a398897SStephane Eranian * set defaults as for evsel 12698a398897SStephane Eranian * unit cannot left to NULL 12708a398897SStephane Eranian */ 127146441bdcSMatt Fleming if (info->unit == NULL) 127246441bdcSMatt Fleming info->unit = ""; 12738a398897SStephane Eranian 127446441bdcSMatt Fleming if (info->scale == 0.0) 127546441bdcSMatt Fleming info->scale = 1.0; 12768a398897SStephane Eranian 1277a6146d50SZheng Yan return 0; 1278a6146d50SZheng Yan } 1279a6146d50SZheng Yan 1280cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 1281cd82a32eSJiri Olsa int config, unsigned long *bits) 1282cd82a32eSJiri Olsa { 12835c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1284cd82a32eSJiri Olsa 1285cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 1286cd82a32eSJiri Olsa if (!format) 1287cd82a32eSJiri Olsa return -ENOMEM; 1288cd82a32eSJiri Olsa 1289cd82a32eSJiri Olsa format->name = strdup(name); 1290cd82a32eSJiri Olsa format->value = config; 1291cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 1292cd82a32eSJiri Olsa 1293cd82a32eSJiri Olsa list_add_tail(&format->list, list); 1294cd82a32eSJiri Olsa return 0; 1295cd82a32eSJiri Olsa } 1296cd82a32eSJiri Olsa 1297cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 1298cd82a32eSJiri Olsa { 1299cd82a32eSJiri Olsa long b; 1300cd82a32eSJiri Olsa 1301cd82a32eSJiri Olsa if (!to) 1302cd82a32eSJiri Olsa to = from; 1303cd82a32eSJiri Olsa 130415268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1305cd82a32eSJiri Olsa for (b = from; b <= to; b++) 1306cd82a32eSJiri Olsa set_bit(b, bits); 1307cd82a32eSJiri Olsa } 1308dc098b35SAndi Kleen 1309aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 1310aaea3617SCody P Schafer { 1311aaea3617SCody P Schafer if (b > a) 1312aaea3617SCody P Schafer return 0; 1313aaea3617SCody P Schafer return a - b; 1314aaea3617SCody P Schafer } 1315aaea3617SCody P Schafer 1316dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 1317dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1318dc098b35SAndi Kleen { 1319aaea3617SCody P Schafer struct parse_events_term *term; 1320aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1321aaea3617SCody P Schafer 1322aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 1323aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1324aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 1325aaea3617SCody P Schafer ",%s=%s", term->config, 1326aaea3617SCody P Schafer term->val.str); 1327aaea3617SCody P Schafer } 1328aaea3617SCody P Schafer 1329aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1330aaea3617SCody P Schafer buf[used] = '/'; 1331aaea3617SCody P Schafer used++; 1332aaea3617SCody P Schafer } 1333aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1334aaea3617SCody P Schafer buf[used] = '\0'; 1335aaea3617SCody P Schafer used++; 1336aaea3617SCody P Schafer } else 1337aaea3617SCody P Schafer buf[len - 1] = '\0'; 1338aaea3617SCody P Schafer 1339dc098b35SAndi Kleen return buf; 1340dc098b35SAndi Kleen } 1341dc098b35SAndi Kleen 1342dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 1343dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1344dc098b35SAndi Kleen { 1345dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 1346dc098b35SAndi Kleen return buf; 1347dc098b35SAndi Kleen } 1348dc098b35SAndi Kleen 1349dd5f1036SAndi Kleen struct sevent { 135008e60ed1SAndi Kleen char *name; 135108e60ed1SAndi Kleen char *desc; 1352dd5f1036SAndi Kleen char *topic; 1353f2361024SAndi Kleen char *str; 1354f2361024SAndi Kleen char *pmu; 13557f372a63SAndi Kleen char *metric_expr; 135696284814SAndi Kleen char *metric_name; 135708e60ed1SAndi Kleen }; 135808e60ed1SAndi Kleen 1359dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b) 1360dc098b35SAndi Kleen { 1361dd5f1036SAndi Kleen const struct sevent *as = a; 1362dd5f1036SAndi Kleen const struct sevent *bs = b; 136308e60ed1SAndi Kleen 136408e60ed1SAndi Kleen /* Put extra events last */ 136508e60ed1SAndi Kleen if (!!as->desc != !!bs->desc) 136608e60ed1SAndi Kleen return !!as->desc - !!bs->desc; 1367dd5f1036SAndi Kleen if (as->topic && bs->topic) { 1368dd5f1036SAndi Kleen int n = strcmp(as->topic, bs->topic); 1369dd5f1036SAndi Kleen 1370dd5f1036SAndi Kleen if (n) 1371dd5f1036SAndi Kleen return n; 1372dd5f1036SAndi Kleen } 137308e60ed1SAndi Kleen return strcmp(as->name, bs->name); 137408e60ed1SAndi Kleen } 137508e60ed1SAndi Kleen 137608e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr) 137708e60ed1SAndi Kleen { 137808e60ed1SAndi Kleen int column = start; 137908e60ed1SAndi Kleen int n; 138008e60ed1SAndi Kleen 138108e60ed1SAndi Kleen while (*s) { 138208e60ed1SAndi Kleen int wlen = strcspn(s, " \t"); 138308e60ed1SAndi Kleen 138408e60ed1SAndi Kleen if (column + wlen >= max && column > start) { 138508e60ed1SAndi Kleen printf("\n%*s", start, ""); 138608e60ed1SAndi Kleen column = start + corr; 138708e60ed1SAndi Kleen } 138808e60ed1SAndi Kleen n = printf("%s%.*s", column > start ? " " : "", wlen, s); 138908e60ed1SAndi Kleen if (n <= 0) 139008e60ed1SAndi Kleen break; 139108e60ed1SAndi Kleen s += wlen; 139208e60ed1SAndi Kleen column += n; 139332858480SArnaldo Carvalho de Melo s = skip_spaces(s); 139408e60ed1SAndi Kleen } 1395dc098b35SAndi Kleen } 1396dc098b35SAndi Kleen 1397c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, 1398a7f6c8c8SJin Yao bool long_desc, bool details_flag, bool deprecated) 1399dc098b35SAndi Kleen { 1400dc098b35SAndi Kleen struct perf_pmu *pmu; 1401dc098b35SAndi Kleen struct perf_pmu_alias *alias; 1402dc098b35SAndi Kleen char buf[1024]; 1403dc098b35SAndi Kleen int printed = 0; 1404dc098b35SAndi Kleen int len, j; 1405dd5f1036SAndi Kleen struct sevent *aliases; 140608e60ed1SAndi Kleen int numdesc = 0; 140761eb2eb4SAndi Kleen int columns = pager_get_columns(); 1408dd5f1036SAndi Kleen char *topic = NULL; 1409dc098b35SAndi Kleen 1410dc098b35SAndi Kleen pmu = NULL; 1411dc098b35SAndi Kleen len = 0; 141242634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1413dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 1414dc098b35SAndi Kleen len++; 141542634bc7SAdrian Hunter if (pmu->selectable) 141642634bc7SAdrian Hunter len++; 141742634bc7SAdrian Hunter } 1418dd5f1036SAndi Kleen aliases = zalloc(sizeof(struct sevent) * len); 1419dc098b35SAndi Kleen if (!aliases) 14207e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1421dc098b35SAndi Kleen pmu = NULL; 1422dc098b35SAndi Kleen j = 0; 142342634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1424dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 142508e60ed1SAndi Kleen char *name = alias->desc ? alias->name : 142608e60ed1SAndi Kleen format_alias(buf, sizeof(buf), pmu, alias); 1427dc098b35SAndi Kleen bool is_cpu = !strcmp(pmu->name, "cpu"); 1428dc098b35SAndi Kleen 1429a7f6c8c8SJin Yao if (alias->deprecated && !deprecated) 1430a7f6c8c8SJin Yao continue; 1431a7f6c8c8SJin Yao 1432dc098b35SAndi Kleen if (event_glob != NULL && 143338d14f0cSAndi Kleen !(strglobmatch_nocase(name, event_glob) || 143438d14f0cSAndi Kleen (!is_cpu && strglobmatch_nocase(alias->name, 143567bdc35fSAndi Kleen event_glob)) || 143667bdc35fSAndi Kleen (alias->topic && 143767bdc35fSAndi Kleen strglobmatch_nocase(alias->topic, event_glob)))) 1438dc098b35SAndi Kleen continue; 14397e4772dcSArnaldo Carvalho de Melo 144008e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 14417e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 14427e4772dcSArnaldo Carvalho de Melo 144308e60ed1SAndi Kleen aliases[j].name = name; 144408e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 144508e60ed1SAndi Kleen aliases[j].name = format_alias_or(buf, 144608e60ed1SAndi Kleen sizeof(buf), 144708e60ed1SAndi Kleen pmu, alias); 144808e60ed1SAndi Kleen aliases[j].name = strdup(aliases[j].name); 144908e60ed1SAndi Kleen if (!aliases[j].name) 14507e4772dcSArnaldo Carvalho de Melo goto out_enomem; 145108e60ed1SAndi Kleen 1452c8d6828aSSukadev Bhattiprolu aliases[j].desc = long_desc ? alias->long_desc : 1453c8d6828aSSukadev Bhattiprolu alias->desc; 1454dd5f1036SAndi Kleen aliases[j].topic = alias->topic; 1455f2361024SAndi Kleen aliases[j].str = alias->str; 1456f2361024SAndi Kleen aliases[j].pmu = pmu->name; 14577f372a63SAndi Kleen aliases[j].metric_expr = alias->metric_expr; 145896284814SAndi Kleen aliases[j].metric_name = alias->metric_name; 1459dc098b35SAndi Kleen j++; 1460dc098b35SAndi Kleen } 1461fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1462fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 14637e4772dcSArnaldo Carvalho de Melo char *s; 14647e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 14657e4772dcSArnaldo Carvalho de Melo goto out_enomem; 146608e60ed1SAndi Kleen aliases[j].name = s; 146742634bc7SAdrian Hunter j++; 146842634bc7SAdrian Hunter } 146942634bc7SAdrian Hunter } 1470dc098b35SAndi Kleen len = j; 1471dd5f1036SAndi Kleen qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1472dc098b35SAndi Kleen for (j = 0; j < len; j++) { 147315b22ed3SAndi Kleen /* Skip duplicates */ 147415b22ed3SAndi Kleen if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) 147515b22ed3SAndi Kleen continue; 1476dc098b35SAndi Kleen if (name_only) { 147708e60ed1SAndi Kleen printf("%s ", aliases[j].name); 1478dc098b35SAndi Kleen continue; 1479dc098b35SAndi Kleen } 14801c5f01feSAndi Kleen if (aliases[j].desc && !quiet_flag) { 148108e60ed1SAndi Kleen if (numdesc++ == 0) 148208e60ed1SAndi Kleen printf("\n"); 1483dd5f1036SAndi Kleen if (aliases[j].topic && (!topic || 1484dd5f1036SAndi Kleen strcmp(topic, aliases[j].topic))) { 1485dd5f1036SAndi Kleen printf("%s%s:\n", topic ? "\n" : "", 1486dd5f1036SAndi Kleen aliases[j].topic); 1487dd5f1036SAndi Kleen topic = aliases[j].topic; 1488dd5f1036SAndi Kleen } 148908e60ed1SAndi Kleen printf(" %-50s\n", aliases[j].name); 149008e60ed1SAndi Kleen printf("%*s", 8, "["); 149108e60ed1SAndi Kleen wordwrap(aliases[j].desc, 8, columns, 0); 149208e60ed1SAndi Kleen printf("]\n"); 1493bf874fcfSAndi Kleen if (details_flag) { 14947f372a63SAndi Kleen printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); 149596284814SAndi Kleen if (aliases[j].metric_name) 149696284814SAndi Kleen printf(" MetricName: %s", aliases[j].metric_name); 14977f372a63SAndi Kleen if (aliases[j].metric_expr) 14987f372a63SAndi Kleen printf(" MetricExpr: %s", aliases[j].metric_expr); 14997f372a63SAndi Kleen putchar('\n'); 15007f372a63SAndi Kleen } 150108e60ed1SAndi Kleen } else 150208e60ed1SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j].name); 1503dc098b35SAndi Kleen printed++; 1504dc098b35SAndi Kleen } 1505dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1506dc098b35SAndi Kleen printf("\n"); 15077e4772dcSArnaldo Carvalho de Melo out_free: 15087e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 150908e60ed1SAndi Kleen zfree(&aliases[j].name); 15107e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 15117e4772dcSArnaldo Carvalho de Melo return; 15127e4772dcSArnaldo Carvalho de Melo 15137e4772dcSArnaldo Carvalho de Melo out_enomem: 15147e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 15157e4772dcSArnaldo Carvalho de Melo if (aliases) 15167e4772dcSArnaldo Carvalho de Melo goto out_free; 1517dc098b35SAndi Kleen } 15184cabc3d1SAndi Kleen 15194cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 15204cabc3d1SAndi Kleen { 15214cabc3d1SAndi Kleen struct perf_pmu *pmu; 15224cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 15234cabc3d1SAndi Kleen 15244cabc3d1SAndi Kleen pmu = NULL; 15254cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 15264cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 15274cabc3d1SAndi Kleen continue; 15284cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 15294cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 15304cabc3d1SAndi Kleen return true; 15314cabc3d1SAndi Kleen } 15324cabc3d1SAndi Kleen return false; 15334cabc3d1SAndi Kleen } 15347d4bdab5SAdrian Hunter 15357d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 15367d4bdab5SAdrian Hunter { 15377d4bdab5SAdrian Hunter struct stat st; 15387d4bdab5SAdrian Hunter char path[PATH_MAX]; 15397d4bdab5SAdrian Hunter const char *sysfs; 15407d4bdab5SAdrian Hunter 15417d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 15427d4bdab5SAdrian Hunter if (!sysfs) 15437d4bdab5SAdrian Hunter return NULL; 15447d4bdab5SAdrian Hunter 15457d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 15467d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 15477d4bdab5SAdrian Hunter 15487d4bdab5SAdrian Hunter if (stat(path, &st) < 0) 15497d4bdab5SAdrian Hunter return NULL; 15507d4bdab5SAdrian Hunter 15517d4bdab5SAdrian Hunter return fopen(path, "r"); 15527d4bdab5SAdrian Hunter } 15537d4bdab5SAdrian Hunter 15547d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 15557d4bdab5SAdrian Hunter ...) 15567d4bdab5SAdrian Hunter { 15577d4bdab5SAdrian Hunter va_list args; 15587d4bdab5SAdrian Hunter FILE *file; 15597d4bdab5SAdrian Hunter int ret = EOF; 15607d4bdab5SAdrian Hunter 15617d4bdab5SAdrian Hunter va_start(args, fmt); 15627d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 15637d4bdab5SAdrian Hunter if (file) { 15647d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 15657d4bdab5SAdrian Hunter fclose(file); 15667d4bdab5SAdrian Hunter } 15677d4bdab5SAdrian Hunter va_end(args); 15687d4bdab5SAdrian Hunter return ret; 15697d4bdab5SAdrian Hunter } 1570