1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 2cd82a32eSJiri Olsa #include <linux/list.h> 3c5de47f2SSukadev Bhattiprolu #include <linux/compiler.h> 4cd82a32eSJiri Olsa #include <sys/types.h> 5a43783aeSArnaldo Carvalho de Melo #include <errno.h> 6c23c2a0fSArnaldo Carvalho de Melo #include <fcntl.h> 77a8ef4c4SArnaldo Carvalho de Melo #include <sys/stat.h> 8cd82a32eSJiri Olsa #include <unistd.h> 9cd82a32eSJiri Olsa #include <stdio.h> 10dc0a6202SAdrian Hunter #include <stdbool.h> 117d4bdab5SAdrian Hunter #include <stdarg.h> 12cd82a32eSJiri Olsa #include <dirent.h> 13cd0cfad7SBorislav Petkov #include <api/fs/fs.h> 14410136f5SStephane Eranian #include <locale.h> 15fbc2844eSWilliam Cohen #include <regex.h> 16cd82a32eSJiri Olsa #include "util.h" 17cd82a32eSJiri Olsa #include "pmu.h" 18cd82a32eSJiri Olsa #include "parse-events.h" 197ae92e74SYan, Zheng #include "cpumap.h" 20933f82ffSSukadev Bhattiprolu #include "header.h" 21933f82ffSSukadev Bhattiprolu #include "pmu-events/pmu-events.h" 2261eb2eb4SAndi Kleen #include "cache.h" 23a067558eSArnaldo Carvalho de Melo #include "string2.h" 24cd82a32eSJiri Olsa 25ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 26ab1bf653SArnaldo Carvalho de Melo char *name; 27ab1bf653SArnaldo Carvalho de Melo int value; 28ab1bf653SArnaldo Carvalho de Melo DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 29ab1bf653SArnaldo Carvalho de Melo struct list_head list; 30ab1bf653SArnaldo Carvalho de Melo }; 31ab1bf653SArnaldo Carvalho de Melo 3250a9667cSRobert Richter #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/" 3350a9667cSRobert Richter 34cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name); 35cd82a32eSJiri Olsa extern FILE *perf_pmu_in; 36cd82a32eSJiri Olsa 37cd82a32eSJiri Olsa static LIST_HEAD(pmus); 38cd82a32eSJiri Olsa 39cd82a32eSJiri Olsa /* 40cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 41cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 42cd82a32eSJiri Olsa */ 43cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head) 44cd82a32eSJiri Olsa { 45cd82a32eSJiri Olsa struct dirent *evt_ent; 46cd82a32eSJiri Olsa DIR *format_dir; 47cd82a32eSJiri Olsa int ret = 0; 48cd82a32eSJiri Olsa 49cd82a32eSJiri Olsa format_dir = opendir(dir); 50cd82a32eSJiri Olsa if (!format_dir) 51cd82a32eSJiri Olsa return -EINVAL; 52cd82a32eSJiri Olsa 53cd82a32eSJiri Olsa while (!ret && (evt_ent = readdir(format_dir))) { 54cd82a32eSJiri Olsa char path[PATH_MAX]; 55cd82a32eSJiri Olsa char *name = evt_ent->d_name; 56cd82a32eSJiri Olsa FILE *file; 57cd82a32eSJiri Olsa 58cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 59cd82a32eSJiri Olsa continue; 60cd82a32eSJiri Olsa 61cd82a32eSJiri Olsa snprintf(path, PATH_MAX, "%s/%s", dir, name); 62cd82a32eSJiri Olsa 63cd82a32eSJiri Olsa ret = -EINVAL; 64cd82a32eSJiri Olsa file = fopen(path, "r"); 65cd82a32eSJiri Olsa if (!file) 66cd82a32eSJiri Olsa break; 67cd82a32eSJiri Olsa 68cd82a32eSJiri Olsa perf_pmu_in = file; 69cd82a32eSJiri Olsa ret = perf_pmu_parse(head, name); 70cd82a32eSJiri Olsa fclose(file); 71cd82a32eSJiri Olsa } 72cd82a32eSJiri Olsa 73cd82a32eSJiri Olsa closedir(format_dir); 74cd82a32eSJiri Olsa return ret; 75cd82a32eSJiri Olsa } 76cd82a32eSJiri Olsa 77cd82a32eSJiri Olsa /* 78cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 79cd82a32eSJiri Olsa * located at: 80cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 81cd82a32eSJiri Olsa */ 82b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format) 83cd82a32eSJiri Olsa { 84cd82a32eSJiri Olsa struct stat st; 85cd82a32eSJiri Olsa char path[PATH_MAX]; 86cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 87cd82a32eSJiri Olsa 88cd82a32eSJiri Olsa if (!sysfs) 89cd82a32eSJiri Olsa return -1; 90cd82a32eSJiri Olsa 91cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 9250a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); 93cd82a32eSJiri Olsa 94cd82a32eSJiri Olsa if (stat(path, &st) < 0) 959bc8f9feSRobert Richter return 0; /* no error if format does not exist */ 96cd82a32eSJiri Olsa 97cff7f956SJiri Olsa if (perf_pmu__format_parse(path, format)) 98cd82a32eSJiri Olsa return -1; 99cd82a32eSJiri Olsa 100cd82a32eSJiri Olsa return 0; 101cd82a32eSJiri Olsa } 102cd82a32eSJiri Olsa 103d02fc6bcSAndi Kleen static int convert_scale(const char *scale, char **end, double *sval) 104d02fc6bcSAndi Kleen { 105d02fc6bcSAndi Kleen char *lc; 106d02fc6bcSAndi Kleen int ret = 0; 107d02fc6bcSAndi Kleen 108d02fc6bcSAndi Kleen /* 109d02fc6bcSAndi Kleen * save current locale 110d02fc6bcSAndi Kleen */ 111d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 112d02fc6bcSAndi Kleen 113d02fc6bcSAndi Kleen /* 114d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 115d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 116d02fc6bcSAndi Kleen * call below. 117d02fc6bcSAndi Kleen */ 118d02fc6bcSAndi Kleen lc = strdup(lc); 119d02fc6bcSAndi Kleen if (!lc) { 120d02fc6bcSAndi Kleen ret = -ENOMEM; 121d02fc6bcSAndi Kleen goto out; 122d02fc6bcSAndi Kleen } 123d02fc6bcSAndi Kleen 124d02fc6bcSAndi Kleen /* 125d02fc6bcSAndi Kleen * force to C locale to ensure kernel 126d02fc6bcSAndi Kleen * scale string is converted correctly. 127d02fc6bcSAndi Kleen * kernel uses default C locale. 128d02fc6bcSAndi Kleen */ 129d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 130d02fc6bcSAndi Kleen 131d02fc6bcSAndi Kleen *sval = strtod(scale, end); 132d02fc6bcSAndi Kleen 133d02fc6bcSAndi Kleen out: 134d02fc6bcSAndi Kleen /* restore locale */ 135d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 136d02fc6bcSAndi Kleen free(lc); 137d02fc6bcSAndi Kleen return ret; 138d02fc6bcSAndi Kleen } 139d02fc6bcSAndi Kleen 140410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) 141410136f5SStephane Eranian { 142410136f5SStephane Eranian struct stat st; 143410136f5SStephane Eranian ssize_t sret; 144410136f5SStephane Eranian char scale[128]; 145410136f5SStephane Eranian int fd, ret = -1; 146410136f5SStephane Eranian char path[PATH_MAX]; 147410136f5SStephane Eranian 148410136f5SStephane Eranian snprintf(path, PATH_MAX, "%s/%s.scale", dir, name); 149410136f5SStephane Eranian 150410136f5SStephane Eranian fd = open(path, O_RDONLY); 151410136f5SStephane Eranian if (fd == -1) 152410136f5SStephane Eranian return -1; 153410136f5SStephane Eranian 154410136f5SStephane Eranian if (fstat(fd, &st) < 0) 155410136f5SStephane Eranian goto error; 156410136f5SStephane Eranian 157410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 158410136f5SStephane Eranian if (sret < 0) 159410136f5SStephane Eranian goto error; 160410136f5SStephane Eranian 1619ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 1629ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 1639ecae065SMadhavan Srinivasan else 164410136f5SStephane Eranian scale[sret] = '\0'; 1659ecae065SMadhavan Srinivasan 166d02fc6bcSAndi Kleen ret = convert_scale(scale, NULL, &alias->scale); 167410136f5SStephane Eranian error: 168410136f5SStephane Eranian close(fd); 169410136f5SStephane Eranian return ret; 170410136f5SStephane Eranian } 171410136f5SStephane Eranian 172410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) 173410136f5SStephane Eranian { 174410136f5SStephane Eranian char path[PATH_MAX]; 175410136f5SStephane Eranian ssize_t sret; 176410136f5SStephane Eranian int fd; 177410136f5SStephane Eranian 178410136f5SStephane Eranian snprintf(path, PATH_MAX, "%s/%s.unit", dir, name); 179410136f5SStephane Eranian 180410136f5SStephane Eranian fd = open(path, O_RDONLY); 181410136f5SStephane Eranian if (fd == -1) 182410136f5SStephane Eranian return -1; 183410136f5SStephane Eranian 184410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 185410136f5SStephane Eranian if (sret < 0) 186410136f5SStephane Eranian goto error; 187410136f5SStephane Eranian 188410136f5SStephane Eranian close(fd); 189410136f5SStephane Eranian 1909ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 1919ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 1929ecae065SMadhavan Srinivasan else 193410136f5SStephane Eranian alias->unit[sret] = '\0'; 194410136f5SStephane Eranian 195410136f5SStephane Eranian return 0; 196410136f5SStephane Eranian error: 197410136f5SStephane Eranian close(fd); 198410136f5SStephane Eranian alias->unit[0] = '\0'; 199410136f5SStephane Eranian return -1; 200410136f5SStephane Eranian } 201410136f5SStephane Eranian 202044330c1SMatt Fleming static int 203044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) 204044330c1SMatt Fleming { 205044330c1SMatt Fleming char path[PATH_MAX]; 206044330c1SMatt Fleming int fd; 207044330c1SMatt Fleming 208044330c1SMatt Fleming snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); 209044330c1SMatt Fleming 210044330c1SMatt Fleming fd = open(path, O_RDONLY); 211044330c1SMatt Fleming if (fd == -1) 212044330c1SMatt Fleming return -1; 213044330c1SMatt Fleming 214044330c1SMatt Fleming close(fd); 215044330c1SMatt Fleming 216044330c1SMatt Fleming alias->per_pkg = true; 217044330c1SMatt Fleming return 0; 218044330c1SMatt Fleming } 219044330c1SMatt Fleming 2201d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 2211d9e446bSJiri Olsa char *dir, char *name) 2221d9e446bSJiri Olsa { 2231d9e446bSJiri Olsa char path[PATH_MAX]; 2241d9e446bSJiri Olsa int fd; 2251d9e446bSJiri Olsa 2261d9e446bSJiri Olsa snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); 2271d9e446bSJiri Olsa 2281d9e446bSJiri Olsa fd = open(path, O_RDONLY); 2291d9e446bSJiri Olsa if (fd == -1) 2301d9e446bSJiri Olsa return -1; 2311d9e446bSJiri Olsa 2321d9e446bSJiri Olsa alias->snapshot = true; 2331d9e446bSJiri Olsa close(fd); 2341d9e446bSJiri Olsa return 0; 2351d9e446bSJiri Olsa } 2361d9e446bSJiri Olsa 23770c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, 238fedb2b51SAndi Kleen char *desc, char *val, 239fedb2b51SAndi Kleen char *long_desc, char *topic, 24000636c3bSAndi Kleen char *unit, char *perpkg, 24196284814SAndi Kleen char *metric_expr, 24296284814SAndi Kleen char *metric_name) 243a6146d50SZheng Yan { 244*0c24d6fbSThomas Richter struct parse_events_term *term; 2455c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 246a6146d50SZheng Yan int ret; 247fedb2b51SAndi Kleen int num; 248*0c24d6fbSThomas Richter char newval[256]; 249a6146d50SZheng Yan 250a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 251a6146d50SZheng Yan if (!alias) 252a6146d50SZheng Yan return -ENOMEM; 253a6146d50SZheng Yan 254a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 255410136f5SStephane Eranian alias->scale = 1.0; 256410136f5SStephane Eranian alias->unit[0] = '\0'; 257044330c1SMatt Fleming alias->per_pkg = false; 25884530920SStephane Eranian alias->snapshot = false; 259410136f5SStephane Eranian 26070c646e0SSukadev Bhattiprolu ret = parse_events_terms(&alias->terms, val); 261a6146d50SZheng Yan if (ret) { 26270c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 263a6146d50SZheng Yan free(alias); 264a6146d50SZheng Yan return ret; 265a6146d50SZheng Yan } 266a6146d50SZheng Yan 267*0c24d6fbSThomas Richter /* Scan event and remove leading zeroes, spaces, newlines, some 268*0c24d6fbSThomas Richter * platforms have terms specified as 269*0c24d6fbSThomas Richter * event=0x0091 (read from files ../<PMU>/events/<FILE> 270*0c24d6fbSThomas Richter * and terms specified as event=0x91 (read from JSON files). 271*0c24d6fbSThomas Richter * 272*0c24d6fbSThomas Richter * Rebuild string to make alias->str member comparable. 273*0c24d6fbSThomas Richter */ 274*0c24d6fbSThomas Richter memset(newval, 0, sizeof(newval)); 275*0c24d6fbSThomas Richter ret = 0; 276*0c24d6fbSThomas Richter list_for_each_entry(term, &alias->terms, list) { 277*0c24d6fbSThomas Richter if (ret) 278*0c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 279*0c24d6fbSThomas Richter ","); 280*0c24d6fbSThomas Richter if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) 281*0c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 282*0c24d6fbSThomas Richter "%s=%#x", term->config, term->val.num); 283*0c24d6fbSThomas Richter else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 284*0c24d6fbSThomas Richter ret += scnprintf(newval + ret, sizeof(newval) - ret, 285*0c24d6fbSThomas Richter "%s=%s", term->config, term->val.str); 286*0c24d6fbSThomas Richter } 287*0c24d6fbSThomas Richter 288a6146d50SZheng Yan alias->name = strdup(name); 28970c646e0SSukadev Bhattiprolu if (dir) { 290410136f5SStephane Eranian /* 291410136f5SStephane Eranian * load unit name and scale if available 292410136f5SStephane Eranian */ 293410136f5SStephane Eranian perf_pmu__parse_unit(alias, dir, name); 294410136f5SStephane Eranian perf_pmu__parse_scale(alias, dir, name); 295044330c1SMatt Fleming perf_pmu__parse_per_pkg(alias, dir, name); 2961d9e446bSJiri Olsa perf_pmu__parse_snapshot(alias, dir, name); 29770c646e0SSukadev Bhattiprolu } 298410136f5SStephane Eranian 29900636c3bSAndi Kleen alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL; 30096284814SAndi Kleen alias->metric_name = metric_name ? strdup(metric_name): NULL; 30108e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 302c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 303c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 304dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 305fedb2b51SAndi Kleen if (unit) { 306fedb2b51SAndi Kleen if (convert_scale(unit, &unit, &alias->scale) < 0) 307fedb2b51SAndi Kleen return -1; 308fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 309fedb2b51SAndi Kleen } 310fedb2b51SAndi Kleen alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1; 311*0c24d6fbSThomas Richter alias->str = strdup(newval); 312f2361024SAndi Kleen 313a6146d50SZheng Yan list_add_tail(&alias->list, list); 314410136f5SStephane Eranian 315a6146d50SZheng Yan return 0; 316a6146d50SZheng Yan } 317a6146d50SZheng Yan 31870c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) 31970c646e0SSukadev Bhattiprolu { 32070c646e0SSukadev Bhattiprolu char buf[256]; 32170c646e0SSukadev Bhattiprolu int ret; 32270c646e0SSukadev Bhattiprolu 32370c646e0SSukadev Bhattiprolu ret = fread(buf, 1, sizeof(buf), file); 32470c646e0SSukadev Bhattiprolu if (ret == 0) 32570c646e0SSukadev Bhattiprolu return -EINVAL; 32670c646e0SSukadev Bhattiprolu 32770c646e0SSukadev Bhattiprolu buf[ret] = 0; 32870c646e0SSukadev Bhattiprolu 329ea23ac73SThomas Richter /* Remove trailing newline from sysfs file */ 330ea23ac73SThomas Richter rtrim(buf); 331ea23ac73SThomas Richter 332fedb2b51SAndi Kleen return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL, 33396284814SAndi Kleen NULL, NULL, NULL); 33470c646e0SSukadev Bhattiprolu } 33570c646e0SSukadev Bhattiprolu 33646441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 33746441bdcSMatt Fleming { 33846441bdcSMatt Fleming size_t len; 33946441bdcSMatt Fleming 34046441bdcSMatt Fleming len = strlen(name); 34146441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 34246441bdcSMatt Fleming return true; 34346441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 34446441bdcSMatt Fleming return true; 345044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 346044330c1SMatt Fleming return true; 3471d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 3481d9e446bSJiri Olsa return true; 34946441bdcSMatt Fleming 35046441bdcSMatt Fleming return false; 35146441bdcSMatt Fleming } 35246441bdcSMatt Fleming 353a6146d50SZheng Yan /* 354a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 355a6146d50SZheng Yan * specified in 'dir' parameter. 356a6146d50SZheng Yan */ 357a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head) 358a6146d50SZheng Yan { 359a6146d50SZheng Yan struct dirent *evt_ent; 360a6146d50SZheng Yan DIR *event_dir; 361a6146d50SZheng Yan 362a6146d50SZheng Yan event_dir = opendir(dir); 363a6146d50SZheng Yan if (!event_dir) 364a6146d50SZheng Yan return -EINVAL; 365a6146d50SZheng Yan 366940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 367a6146d50SZheng Yan char path[PATH_MAX]; 368a6146d50SZheng Yan char *name = evt_ent->d_name; 369a6146d50SZheng Yan FILE *file; 370a6146d50SZheng Yan 371a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 372a6146d50SZheng Yan continue; 373a6146d50SZheng Yan 374410136f5SStephane Eranian /* 37546441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 376410136f5SStephane Eranian */ 37746441bdcSMatt Fleming if (pmu_alias_info_file(name)) 378410136f5SStephane Eranian continue; 379410136f5SStephane Eranian 38077f18153SJiri Olsa scnprintf(path, PATH_MAX, "%s/%s", dir, name); 381a6146d50SZheng Yan 382a6146d50SZheng Yan file = fopen(path, "r"); 383940db6dcSAndi Kleen if (!file) { 384940db6dcSAndi Kleen pr_debug("Cannot open %s\n", path); 385940db6dcSAndi Kleen continue; 386940db6dcSAndi Kleen } 387410136f5SStephane Eranian 388940db6dcSAndi Kleen if (perf_pmu__new_alias(head, dir, name, file) < 0) 389940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 390a6146d50SZheng Yan fclose(file); 391a6146d50SZheng Yan } 392a6146d50SZheng Yan 393a6146d50SZheng Yan closedir(event_dir); 394940db6dcSAndi Kleen return 0; 395a6146d50SZheng Yan } 396a6146d50SZheng Yan 397a6146d50SZheng Yan /* 398a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 399a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 400a6146d50SZheng Yan */ 401b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head) 402a6146d50SZheng Yan { 403a6146d50SZheng Yan struct stat st; 404a6146d50SZheng Yan char path[PATH_MAX]; 405cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 406a6146d50SZheng Yan 407a6146d50SZheng Yan if (!sysfs) 408a6146d50SZheng Yan return -1; 409a6146d50SZheng Yan 410a6146d50SZheng Yan snprintf(path, PATH_MAX, 411a6146d50SZheng Yan "%s/bus/event_source/devices/%s/events", sysfs, name); 412a6146d50SZheng Yan 413a6146d50SZheng Yan if (stat(path, &st) < 0) 4143fded963SJiri Olsa return 0; /* no error if 'events' does not exist */ 415a6146d50SZheng Yan 416a6146d50SZheng Yan if (pmu_aliases_parse(path, head)) 417a6146d50SZheng Yan return -1; 418a6146d50SZheng Yan 419a6146d50SZheng Yan return 0; 420a6146d50SZheng Yan } 421a6146d50SZheng Yan 4225c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 423a6146d50SZheng Yan struct list_head *terms) 424a6146d50SZheng Yan { 4257c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 426a6146d50SZheng Yan LIST_HEAD(list); 427a6146d50SZheng Yan int ret; 428a6146d50SZheng Yan 429a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 4307c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 431a6146d50SZheng Yan if (ret) { 432682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 433a6146d50SZheng Yan return ret; 434a6146d50SZheng Yan } 435c2f1ceadSAndi Kleen /* 436c2f1ceadSAndi Kleen * Weak terms don't override command line options, 437c2f1ceadSAndi Kleen * which we don't want for implicit terms in aliases. 438c2f1ceadSAndi Kleen */ 439c2f1ceadSAndi Kleen cloned->weak = true; 4407c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 441a6146d50SZheng Yan } 442a6146d50SZheng Yan list_splice(&list, terms); 443a6146d50SZheng Yan return 0; 444a6146d50SZheng Yan } 445a6146d50SZheng Yan 446cd82a32eSJiri Olsa /* 447cd82a32eSJiri Olsa * Reading/parsing the default pmu type value, which should be 448cd82a32eSJiri Olsa * located at: 449cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 450cd82a32eSJiri Olsa */ 451b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type) 452cd82a32eSJiri Olsa { 453cd82a32eSJiri Olsa struct stat st; 454cd82a32eSJiri Olsa char path[PATH_MAX]; 455cd82a32eSJiri Olsa FILE *file; 456cd82a32eSJiri Olsa int ret = 0; 457cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 458cd82a32eSJiri Olsa 459cd82a32eSJiri Olsa if (!sysfs) 460cd82a32eSJiri Olsa return -1; 461cd82a32eSJiri Olsa 462cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 46350a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); 464cd82a32eSJiri Olsa 465cd82a32eSJiri Olsa if (stat(path, &st) < 0) 466cd82a32eSJiri Olsa return -1; 467cd82a32eSJiri Olsa 468cd82a32eSJiri Olsa file = fopen(path, "r"); 469cd82a32eSJiri Olsa if (!file) 470cd82a32eSJiri Olsa return -EINVAL; 471cd82a32eSJiri Olsa 472cd82a32eSJiri Olsa if (1 != fscanf(file, "%u", type)) 473cd82a32eSJiri Olsa ret = -1; 474cd82a32eSJiri Olsa 475cd82a32eSJiri Olsa fclose(file); 476cd82a32eSJiri Olsa return ret; 477cd82a32eSJiri Olsa } 478cd82a32eSJiri Olsa 47950a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */ 48050a9667cSRobert Richter static void pmu_read_sysfs(void) 48150a9667cSRobert Richter { 48250a9667cSRobert Richter char path[PATH_MAX]; 48350a9667cSRobert Richter DIR *dir; 48450a9667cSRobert Richter struct dirent *dent; 485cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 48650a9667cSRobert Richter 48750a9667cSRobert Richter if (!sysfs) 48850a9667cSRobert Richter return; 48950a9667cSRobert Richter 49050a9667cSRobert Richter snprintf(path, PATH_MAX, 49150a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 49250a9667cSRobert Richter 49350a9667cSRobert Richter dir = opendir(path); 49450a9667cSRobert Richter if (!dir) 49550a9667cSRobert Richter return; 49650a9667cSRobert Richter 49750a9667cSRobert Richter while ((dent = readdir(dir))) { 49850a9667cSRobert Richter if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 49950a9667cSRobert Richter continue; 50050a9667cSRobert Richter /* add to static LIST_HEAD(pmus): */ 50150a9667cSRobert Richter perf_pmu__find(dent->d_name); 50250a9667cSRobert Richter } 50350a9667cSRobert Richter 50450a9667cSRobert Richter closedir(dir); 50550a9667cSRobert Richter } 50650a9667cSRobert Richter 50766ec1191SMark Rutland static struct cpu_map *__pmu_cpumask(const char *path) 50866ec1191SMark Rutland { 50966ec1191SMark Rutland FILE *file; 51066ec1191SMark Rutland struct cpu_map *cpus; 51166ec1191SMark Rutland 51266ec1191SMark Rutland file = fopen(path, "r"); 51366ec1191SMark Rutland if (!file) 51466ec1191SMark Rutland return NULL; 51566ec1191SMark Rutland 51666ec1191SMark Rutland cpus = cpu_map__read(file); 51766ec1191SMark Rutland fclose(file); 51866ec1191SMark Rutland return cpus; 51966ec1191SMark Rutland } 52066ec1191SMark Rutland 52166ec1191SMark Rutland /* 52266ec1191SMark Rutland * Uncore PMUs have a "cpumask" file under sysfs. CPU PMUs (e.g. on arm/arm64) 52366ec1191SMark Rutland * may have a "cpus" file. 52466ec1191SMark Rutland */ 52566ec1191SMark Rutland #define CPUS_TEMPLATE_UNCORE "%s/bus/event_source/devices/%s/cpumask" 52666ec1191SMark Rutland #define CPUS_TEMPLATE_CPU "%s/bus/event_source/devices/%s/cpus" 52766ec1191SMark Rutland 528b6b96fb4SAdrian Hunter static struct cpu_map *pmu_cpumask(const char *name) 5297ae92e74SYan, Zheng { 5307ae92e74SYan, Zheng char path[PATH_MAX]; 5317ae92e74SYan, Zheng struct cpu_map *cpus; 532cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 5337e3fcffeSMark Rutland const char *templates[] = { 53466ec1191SMark Rutland CPUS_TEMPLATE_UNCORE, 53566ec1191SMark Rutland CPUS_TEMPLATE_CPU, 5367e3fcffeSMark Rutland NULL 5377e3fcffeSMark Rutland }; 5387e3fcffeSMark Rutland const char **template; 5397ae92e74SYan, Zheng 5407ae92e74SYan, Zheng if (!sysfs) 5417ae92e74SYan, Zheng return NULL; 5427ae92e74SYan, Zheng 5437e3fcffeSMark Rutland for (template = templates; *template; template++) { 5447e3fcffeSMark Rutland snprintf(path, PATH_MAX, *template, sysfs, name); 54566ec1191SMark Rutland cpus = __pmu_cpumask(path); 54666ec1191SMark Rutland if (cpus) 54766ec1191SMark Rutland return cpus; 5487e3fcffeSMark Rutland } 5497ae92e74SYan, Zheng 5507ae92e74SYan, Zheng return NULL; 55166ec1191SMark Rutland } 5527ae92e74SYan, Zheng 55366ec1191SMark Rutland static bool pmu_is_uncore(const char *name) 55466ec1191SMark Rutland { 55566ec1191SMark Rutland char path[PATH_MAX]; 55666ec1191SMark Rutland struct cpu_map *cpus; 55766ec1191SMark Rutland const char *sysfs = sysfs__mountpoint(); 5587ae92e74SYan, Zheng 55966ec1191SMark Rutland snprintf(path, PATH_MAX, CPUS_TEMPLATE_UNCORE, sysfs, name); 56066ec1191SMark Rutland cpus = __pmu_cpumask(path); 56166ec1191SMark Rutland cpu_map__put(cpus); 56266ec1191SMark Rutland 56366ec1191SMark Rutland return !!cpus; 5647ae92e74SYan, Zheng } 5657ae92e74SYan, Zheng 566933f82ffSSukadev Bhattiprolu /* 56714b22ae0SGanapatrao Kulkarni * PMU CORE devices have different name other than cpu in sysfs on some 568292c34c1SKan Liang * platforms. 569292c34c1SKan Liang * Looking for possible sysfs files to identify the arm core device. 57014b22ae0SGanapatrao Kulkarni */ 571292c34c1SKan Liang static int is_arm_pmu_core(const char *name) 57214b22ae0SGanapatrao Kulkarni { 57314b22ae0SGanapatrao Kulkarni struct stat st; 57414b22ae0SGanapatrao Kulkarni char path[PATH_MAX]; 57514b22ae0SGanapatrao Kulkarni const char *sysfs = sysfs__mountpoint(); 57614b22ae0SGanapatrao Kulkarni 57714b22ae0SGanapatrao Kulkarni if (!sysfs) 57814b22ae0SGanapatrao Kulkarni return 0; 57914b22ae0SGanapatrao Kulkarni 58014b22ae0SGanapatrao Kulkarni /* Look for cpu sysfs (specific to arm) */ 58114b22ae0SGanapatrao Kulkarni scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s/cpus", 58214b22ae0SGanapatrao Kulkarni sysfs, name); 58314b22ae0SGanapatrao Kulkarni if (stat(path, &st) == 0) 58414b22ae0SGanapatrao Kulkarni return 1; 58514b22ae0SGanapatrao Kulkarni 586038586c3SThomas Richter /* Look for cpu sysfs (specific to s390) */ 587038586c3SThomas Richter scnprintf(path, PATH_MAX, "%s/bus/event_source/devices/%s", 588038586c3SThomas Richter sysfs, name); 589038586c3SThomas Richter if (stat(path, &st) == 0 && !strncmp(name, "cpum_", 5)) 590038586c3SThomas Richter return 1; 591038586c3SThomas Richter 59214b22ae0SGanapatrao Kulkarni return 0; 59314b22ae0SGanapatrao Kulkarni } 59414b22ae0SGanapatrao Kulkarni 59514b22ae0SGanapatrao Kulkarni /* 596933f82ffSSukadev Bhattiprolu * Return the CPU id as a raw string. 597933f82ffSSukadev Bhattiprolu * 598933f82ffSSukadev Bhattiprolu * Each architecture should provide a more precise id string that 599933f82ffSSukadev Bhattiprolu * can be use to match the architecture's "mapfile". 600933f82ffSSukadev Bhattiprolu */ 60154e32dc0SGanapatrao Kulkarni char * __weak get_cpuid_str(struct perf_pmu *pmu __maybe_unused) 602933f82ffSSukadev Bhattiprolu { 603933f82ffSSukadev Bhattiprolu return NULL; 604933f82ffSSukadev Bhattiprolu } 605933f82ffSSukadev Bhattiprolu 6064cb7d3ecSThomas Richter /* Return zero when the cpuid from the mapfile.csv matches the 6074cb7d3ecSThomas Richter * cpuid string generated on this platform. 6084cb7d3ecSThomas Richter * Otherwise return non-zero. 6094cb7d3ecSThomas Richter */ 610ce04abfbSThomas Richter int strcmp_cpuid_str(const char *mapcpuid, const char *cpuid) 6114cb7d3ecSThomas Richter { 6124cb7d3ecSThomas Richter regex_t re; 6134cb7d3ecSThomas Richter regmatch_t pmatch[1]; 6144cb7d3ecSThomas Richter int match; 6154cb7d3ecSThomas Richter 6164cb7d3ecSThomas Richter if (regcomp(&re, mapcpuid, REG_EXTENDED) != 0) { 6174cb7d3ecSThomas Richter /* Warn unable to generate match particular string. */ 6184cb7d3ecSThomas Richter pr_info("Invalid regular expression %s\n", mapcpuid); 6194cb7d3ecSThomas Richter return 1; 6204cb7d3ecSThomas Richter } 6214cb7d3ecSThomas Richter 6224cb7d3ecSThomas Richter match = !regexec(&re, cpuid, 1, pmatch, 0); 6234cb7d3ecSThomas Richter regfree(&re); 6244cb7d3ecSThomas Richter if (match) { 6254cb7d3ecSThomas Richter size_t match_len = (pmatch[0].rm_eo - pmatch[0].rm_so); 6264cb7d3ecSThomas Richter 6274cb7d3ecSThomas Richter /* Verify the entire string matched. */ 6284cb7d3ecSThomas Richter if (match_len == strlen(cpuid)) 6294cb7d3ecSThomas Richter return 0; 6304cb7d3ecSThomas Richter } 6314cb7d3ecSThomas Richter return 1; 6324cb7d3ecSThomas Richter } 6334cb7d3ecSThomas Richter 63454e32dc0SGanapatrao Kulkarni static char *perf_pmu__getcpuid(struct perf_pmu *pmu) 635d77ade9fSAndi Kleen { 636d77ade9fSAndi Kleen char *cpuid; 637d77ade9fSAndi Kleen static bool printed; 638d77ade9fSAndi Kleen 639d77ade9fSAndi Kleen cpuid = getenv("PERF_CPUID"); 640d77ade9fSAndi Kleen if (cpuid) 641d77ade9fSAndi Kleen cpuid = strdup(cpuid); 642d77ade9fSAndi Kleen if (!cpuid) 64354e32dc0SGanapatrao Kulkarni cpuid = get_cpuid_str(pmu); 644d77ade9fSAndi Kleen if (!cpuid) 645d77ade9fSAndi Kleen return NULL; 646d77ade9fSAndi Kleen 647d77ade9fSAndi Kleen if (!printed) { 648d77ade9fSAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 649d77ade9fSAndi Kleen printed = true; 650d77ade9fSAndi Kleen } 651d77ade9fSAndi Kleen return cpuid; 652d77ade9fSAndi Kleen } 653d77ade9fSAndi Kleen 65454e32dc0SGanapatrao Kulkarni struct pmu_events_map *perf_pmu__find_map(struct perf_pmu *pmu) 655d77ade9fSAndi Kleen { 656d77ade9fSAndi Kleen struct pmu_events_map *map; 65754e32dc0SGanapatrao Kulkarni char *cpuid = perf_pmu__getcpuid(pmu); 658d77ade9fSAndi Kleen int i; 659d77ade9fSAndi Kleen 660de3d0f12SGanapatrao Kulkarni /* on some platforms which uses cpus map, cpuid can be NULL for 661de3d0f12SGanapatrao Kulkarni * PMUs other than CORE PMUs. 662de3d0f12SGanapatrao Kulkarni */ 663de3d0f12SGanapatrao Kulkarni if (!cpuid) 664de3d0f12SGanapatrao Kulkarni return NULL; 665de3d0f12SGanapatrao Kulkarni 666d77ade9fSAndi Kleen i = 0; 667d77ade9fSAndi Kleen for (;;) { 668d77ade9fSAndi Kleen map = &pmu_events_map[i++]; 669d77ade9fSAndi Kleen if (!map->table) { 670d77ade9fSAndi Kleen map = NULL; 671d77ade9fSAndi Kleen break; 672d77ade9fSAndi Kleen } 673d77ade9fSAndi Kleen 6744cb7d3ecSThomas Richter if (!strcmp_cpuid_str(map->cpuid, cpuid)) 675d77ade9fSAndi Kleen break; 676d77ade9fSAndi Kleen } 677d77ade9fSAndi Kleen free(cpuid); 678d77ade9fSAndi Kleen return map; 679d77ade9fSAndi Kleen } 680d77ade9fSAndi Kleen 681933f82ffSSukadev Bhattiprolu /* 682933f82ffSSukadev Bhattiprolu * From the pmu_events_map, find the table of PMU events that corresponds 683933f82ffSSukadev Bhattiprolu * to the current running CPU. Then, add all PMU events from that table 684933f82ffSSukadev Bhattiprolu * as aliases. 685933f82ffSSukadev Bhattiprolu */ 68654e32dc0SGanapatrao Kulkarni static void pmu_add_cpu_aliases(struct list_head *head, struct perf_pmu *pmu) 687933f82ffSSukadev Bhattiprolu { 688933f82ffSSukadev Bhattiprolu int i; 689933f82ffSSukadev Bhattiprolu struct pmu_events_map *map; 690933f82ffSSukadev Bhattiprolu struct pmu_event *pe; 69154e32dc0SGanapatrao Kulkarni const char *name = pmu->name; 692292c34c1SKan Liang const char *pname; 693933f82ffSSukadev Bhattiprolu 69454e32dc0SGanapatrao Kulkarni map = perf_pmu__find_map(pmu); 695d77ade9fSAndi Kleen if (!map) 696933f82ffSSukadev Bhattiprolu return; 697933f82ffSSukadev Bhattiprolu 698933f82ffSSukadev Bhattiprolu /* 699933f82ffSSukadev Bhattiprolu * Found a matching PMU events table. Create aliases 700933f82ffSSukadev Bhattiprolu */ 701933f82ffSSukadev Bhattiprolu i = 0; 702933f82ffSSukadev Bhattiprolu while (1) { 703fedb2b51SAndi Kleen 704933f82ffSSukadev Bhattiprolu pe = &map->table[i++]; 705b18f3e36SAndi Kleen if (!pe->name) { 706b18f3e36SAndi Kleen if (pe->metric_group || pe->metric_name) 707b18f3e36SAndi Kleen continue; 708933f82ffSSukadev Bhattiprolu break; 709b18f3e36SAndi Kleen } 710933f82ffSSukadev Bhattiprolu 711292c34c1SKan Liang if (!is_arm_pmu_core(name)) { 712292c34c1SKan Liang pname = pe->pmu ? pe->pmu : "cpu"; 713292c34c1SKan Liang if (strncmp(pname, name, strlen(pname))) 71414b22ae0SGanapatrao Kulkarni continue; 71514b22ae0SGanapatrao Kulkarni } 716fedb2b51SAndi Kleen 717933f82ffSSukadev Bhattiprolu /* need type casts to override 'const' */ 718933f82ffSSukadev Bhattiprolu __perf_pmu__new_alias(head, NULL, (char *)pe->name, 719c8d6828aSSukadev Bhattiprolu (char *)pe->desc, (char *)pe->event, 720fedb2b51SAndi Kleen (char *)pe->long_desc, (char *)pe->topic, 72100636c3bSAndi Kleen (char *)pe->unit, (char *)pe->perpkg, 72296284814SAndi Kleen (char *)pe->metric_expr, 72396284814SAndi Kleen (char *)pe->metric_name); 724933f82ffSSukadev Bhattiprolu } 725933f82ffSSukadev Bhattiprolu } 726933f82ffSSukadev Bhattiprolu 727c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 728dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 729dc0a6202SAdrian Hunter { 730dc0a6202SAdrian Hunter return NULL; 731dc0a6202SAdrian Hunter } 732dc0a6202SAdrian Hunter 733b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name) 734cd82a32eSJiri Olsa { 735cd82a32eSJiri Olsa struct perf_pmu *pmu; 736cd82a32eSJiri Olsa LIST_HEAD(format); 737a6146d50SZheng Yan LIST_HEAD(aliases); 738cd82a32eSJiri Olsa __u32 type; 739cd82a32eSJiri Olsa 740cd82a32eSJiri Olsa /* 741cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 742cd82a32eSJiri Olsa * type value and format definitions. Load both right 743cd82a32eSJiri Olsa * now. 744cd82a32eSJiri Olsa */ 745cd82a32eSJiri Olsa if (pmu_format(name, &format)) 746cd82a32eSJiri Olsa return NULL; 747cd82a32eSJiri Olsa 74815b22ed3SAndi Kleen /* 74915b22ed3SAndi Kleen * Check the type first to avoid unnecessary work. 75015b22ed3SAndi Kleen */ 75115b22ed3SAndi Kleen if (pmu_type(name, &type)) 75215b22ed3SAndi Kleen return NULL; 75315b22ed3SAndi Kleen 7543fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 7553fded963SJiri Olsa return NULL; 7563fded963SJiri Olsa 757cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 758cd82a32eSJiri Olsa if (!pmu) 759cd82a32eSJiri Olsa return NULL; 760cd82a32eSJiri Olsa 7617ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 76254e32dc0SGanapatrao Kulkarni pmu->name = strdup(name); 76354e32dc0SGanapatrao Kulkarni pmu->type = type; 76466ec1191SMark Rutland pmu->is_uncore = pmu_is_uncore(name); 76554e32dc0SGanapatrao Kulkarni pmu_add_cpu_aliases(&aliases, pmu); 76666ec1191SMark Rutland 767cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 768a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 769cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 770a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 7719bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 772dc0a6202SAdrian Hunter 773dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 774dc0a6202SAdrian Hunter 775cd82a32eSJiri Olsa return pmu; 776cd82a32eSJiri Olsa } 777cd82a32eSJiri Olsa 778b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 779cd82a32eSJiri Olsa { 780cd82a32eSJiri Olsa struct perf_pmu *pmu; 781cd82a32eSJiri Olsa 782cd82a32eSJiri Olsa list_for_each_entry(pmu, &pmus, list) 783cd82a32eSJiri Olsa if (!strcmp(pmu->name, name)) 784cd82a32eSJiri Olsa return pmu; 785cd82a32eSJiri Olsa 786cd82a32eSJiri Olsa return NULL; 787cd82a32eSJiri Olsa } 788cd82a32eSJiri Olsa 78950a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 79050a9667cSRobert Richter { 79150a9667cSRobert Richter /* 79250a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 79350a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 79450a9667cSRobert Richter */ 79550a9667cSRobert Richter if (!pmu) { 79650a9667cSRobert Richter pmu_read_sysfs(); 79750a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 79850a9667cSRobert Richter } 79950a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 80050a9667cSRobert Richter return pmu; 80150a9667cSRobert Richter return NULL; 80250a9667cSRobert Richter } 80350a9667cSRobert Richter 804b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 805cd82a32eSJiri Olsa { 806cd82a32eSJiri Olsa struct perf_pmu *pmu; 807cd82a32eSJiri Olsa 808cd82a32eSJiri Olsa /* 809cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 810cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 811cd82a32eSJiri Olsa * the pmu format definitions. 812cd82a32eSJiri Olsa */ 813cd82a32eSJiri Olsa pmu = pmu_find(name); 814cd82a32eSJiri Olsa if (pmu) 815cd82a32eSJiri Olsa return pmu; 816cd82a32eSJiri Olsa 817cd82a32eSJiri Olsa return pmu_lookup(name); 818cd82a32eSJiri Olsa } 819cd82a32eSJiri Olsa 8205c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 82109ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 822cd82a32eSJiri Olsa { 8235c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 824cd82a32eSJiri Olsa 825cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 826cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 827cd82a32eSJiri Olsa return format; 828cd82a32eSJiri Olsa 829cd82a32eSJiri Olsa return NULL; 830cd82a32eSJiri Olsa } 831cd82a32eSJiri Olsa 83209ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 83309ff6071SAdrian Hunter { 83409ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 83509ff6071SAdrian Hunter __u64 bits = 0; 83609ff6071SAdrian Hunter int fbit; 83709ff6071SAdrian Hunter 83809ff6071SAdrian Hunter if (!format) 83909ff6071SAdrian Hunter return 0; 84009ff6071SAdrian Hunter 84109ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 84209ff6071SAdrian Hunter bits |= 1ULL << fbit; 84309ff6071SAdrian Hunter 84409ff6071SAdrian Hunter return bits; 84509ff6071SAdrian Hunter } 84609ff6071SAdrian Hunter 847cd82a32eSJiri Olsa /* 848dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 849cd82a32eSJiri Olsa * and unformated value (value parameter). 850cd82a32eSJiri Olsa */ 851dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 852dc0a6202SAdrian Hunter bool zero) 853cd82a32eSJiri Olsa { 854cd82a32eSJiri Olsa unsigned long fbit, vbit; 855cd82a32eSJiri Olsa 856cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 857cd82a32eSJiri Olsa 858cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 859cd82a32eSJiri Olsa continue; 860cd82a32eSJiri Olsa 861dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 862dc0a6202SAdrian Hunter *v |= (1llu << fbit); 863dc0a6202SAdrian Hunter else if (zero) 864dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 865cd82a32eSJiri Olsa } 866cd82a32eSJiri Olsa } 867cd82a32eSJiri Olsa 8680efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 8690efe6b67SAdrian Hunter { 870ac0e2cd5SKan Liang __u64 w = 0; 871ac0e2cd5SKan Liang int fbit; 8720efe6b67SAdrian Hunter 873ac0e2cd5SKan Liang for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS) 874ac0e2cd5SKan Liang w |= (1ULL << fbit); 875ac0e2cd5SKan Liang 876ac0e2cd5SKan Liang return w; 8770efe6b67SAdrian Hunter } 8780efe6b67SAdrian Hunter 879cd82a32eSJiri Olsa /* 880688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 881688d4dfcSCody P Schafer * in the remaining terms. 882688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 883688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 884688d4dfcSCody P Schafer * in a config string) later on in the term list. 885688d4dfcSCody P Schafer */ 886688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 887688d4dfcSCody P Schafer struct list_head *head_terms, 888688d4dfcSCody P Schafer __u64 *value) 889688d4dfcSCody P Schafer { 890688d4dfcSCody P Schafer struct parse_events_term *t; 891688d4dfcSCody P Schafer 892688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 893688d4dfcSCody P Schafer if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 894688d4dfcSCody P Schafer if (!strcmp(t->config, term->config)) { 895688d4dfcSCody P Schafer t->used = true; 896688d4dfcSCody P Schafer *value = t->val.num; 897688d4dfcSCody P Schafer return 0; 898688d4dfcSCody P Schafer } 899688d4dfcSCody P Schafer } 900688d4dfcSCody P Schafer } 901688d4dfcSCody P Schafer 902bb963e16SNamhyung Kim if (verbose > 0) 903688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 904688d4dfcSCody P Schafer 905688d4dfcSCody P Schafer return -1; 906688d4dfcSCody P Schafer } 907688d4dfcSCody P Schafer 908ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 909e64b020bSJiri Olsa { 910e64b020bSJiri Olsa struct perf_pmu_format *format; 91111db4e29SMasami Hiramatsu char *str = NULL; 91211db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 913e64b020bSJiri Olsa unsigned i = 0; 914e64b020bSJiri Olsa 915ffeb883eSHe Kuang if (!formats) 916e64b020bSJiri Olsa return NULL; 917e64b020bSJiri Olsa 918e64b020bSJiri Olsa /* sysfs exported terms */ 919ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 92011db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 92111db4e29SMasami Hiramatsu goto error; 922e64b020bSJiri Olsa 923ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 92411db4e29SMasami Hiramatsu error: 925ffeb883eSHe Kuang strbuf_release(&buf); 926e64b020bSJiri Olsa 927e64b020bSJiri Olsa return str; 928e64b020bSJiri Olsa } 929e64b020bSJiri Olsa 930688d4dfcSCody P Schafer /* 931cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 93288aca8d9SCody P Schafer * user input data - term parameter. 933cd82a32eSJiri Olsa */ 934cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats, 935cd82a32eSJiri Olsa struct perf_event_attr *attr, 936dc0a6202SAdrian Hunter struct parse_events_term *term, 937688d4dfcSCody P Schafer struct list_head *head_terms, 938e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 939cd82a32eSJiri Olsa { 9405c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 941cd82a32eSJiri Olsa __u64 *vp; 9420efe6b67SAdrian Hunter __u64 val, max_val; 943cd82a32eSJiri Olsa 944cd82a32eSJiri Olsa /* 945688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 946688d4dfcSCody P Schafer * skip it in normal eval. 947688d4dfcSCody P Schafer */ 948688d4dfcSCody P Schafer if (term->used) 949688d4dfcSCody P Schafer return 0; 950688d4dfcSCody P Schafer 951688d4dfcSCody P Schafer /* 952cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 953cd82a32eSJiri Olsa * to be done for them. 954cd82a32eSJiri Olsa */ 955cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 956cd82a32eSJiri Olsa return 0; 957cd82a32eSJiri Olsa 958cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 959688d4dfcSCody P Schafer if (!format) { 960bb963e16SNamhyung Kim if (verbose > 0) 961688d4dfcSCody P Schafer printf("Invalid event/parameter '%s'\n", term->config); 962e64b020bSJiri Olsa if (err) { 963ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 964ffeb883eSHe Kuang 965e64b020bSJiri Olsa err->idx = term->err_term; 966e64b020bSJiri Olsa err->str = strdup("unknown term"); 967ffeb883eSHe Kuang err->help = parse_events_formats_error_string(pmu_term); 968ffeb883eSHe Kuang free(pmu_term); 969e64b020bSJiri Olsa } 970cd82a32eSJiri Olsa return -EINVAL; 971688d4dfcSCody P Schafer } 972cd82a32eSJiri Olsa 973cd82a32eSJiri Olsa switch (format->value) { 974cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 975cd82a32eSJiri Olsa vp = &attr->config; 976cd82a32eSJiri Olsa break; 977cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 978cd82a32eSJiri Olsa vp = &attr->config1; 979cd82a32eSJiri Olsa break; 980cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 981cd82a32eSJiri Olsa vp = &attr->config2; 982cd82a32eSJiri Olsa break; 983cd82a32eSJiri Olsa default: 984cd82a32eSJiri Olsa return -EINVAL; 985cd82a32eSJiri Olsa } 986cd82a32eSJiri Olsa 98716fa7e82SJiri Olsa /* 988688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 989688d4dfcSCody P Schafer * using event parameters. 99016fa7e82SJiri Olsa */ 99199e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 99299e7138eSJiri Olsa if (term->no_value && 99399e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 99499e7138eSJiri Olsa if (err) { 99599e7138eSJiri Olsa err->idx = term->err_val; 99699e7138eSJiri Olsa err->str = strdup("no value assigned for term"); 99799e7138eSJiri Olsa } 99899e7138eSJiri Olsa return -EINVAL; 99999e7138eSJiri Olsa } 100099e7138eSJiri Olsa 1001688d4dfcSCody P Schafer val = term->val.num; 100299e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1003688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 1004bb963e16SNamhyung Kim if (verbose > 0) { 1005688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 1006688d4dfcSCody P Schafer term->config, term->val.str); 1007e64b020bSJiri Olsa } 1008e64b020bSJiri Olsa if (err) { 1009e64b020bSJiri Olsa err->idx = term->err_val; 1010e64b020bSJiri Olsa err->str = strdup("expected numeric value"); 1011e64b020bSJiri Olsa } 1012688d4dfcSCody P Schafer return -EINVAL; 1013688d4dfcSCody P Schafer } 1014688d4dfcSCody P Schafer 1015688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 1016688d4dfcSCody P Schafer return -EINVAL; 1017688d4dfcSCody P Schafer } else 1018688d4dfcSCody P Schafer return -EINVAL; 1019688d4dfcSCody P Schafer 10200efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 10210efe6b67SAdrian Hunter if (val > max_val) { 10220efe6b67SAdrian Hunter if (err) { 10230efe6b67SAdrian Hunter err->idx = term->err_val; 10240efe6b67SAdrian Hunter if (asprintf(&err->str, 10250efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 10260efe6b67SAdrian Hunter (unsigned long long)max_val) < 0) 10270efe6b67SAdrian Hunter err->str = strdup("value too big for format"); 10280efe6b67SAdrian Hunter return -EINVAL; 10290efe6b67SAdrian Hunter } 10300efe6b67SAdrian Hunter /* 10310efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 10320efe6b67SAdrian Hunter * silently truncated. 10330efe6b67SAdrian Hunter */ 10340efe6b67SAdrian Hunter } 10350efe6b67SAdrian Hunter 1036688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 1037cd82a32eSJiri Olsa return 0; 1038cd82a32eSJiri Olsa } 1039cd82a32eSJiri Olsa 1040cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats, 1041cff7f956SJiri Olsa struct perf_event_attr *attr, 1042dc0a6202SAdrian Hunter struct list_head *head_terms, 1043e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 1044cd82a32eSJiri Olsa { 10456cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 1046cd82a32eSJiri Olsa 1047688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 1048e64b020bSJiri Olsa if (pmu_config_term(formats, attr, term, head_terms, 1049e64b020bSJiri Olsa zero, err)) 1050cd82a32eSJiri Olsa return -EINVAL; 1051688d4dfcSCody P Schafer } 1052cd82a32eSJiri Olsa 1053cd82a32eSJiri Olsa return 0; 1054cd82a32eSJiri Olsa } 1055cd82a32eSJiri Olsa 1056cd82a32eSJiri Olsa /* 1057cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 1058cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 1059cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 1060cd82a32eSJiri Olsa */ 1061cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 1062e64b020bSJiri Olsa struct list_head *head_terms, 1063e64b020bSJiri Olsa struct parse_events_error *err) 1064cd82a32eSJiri Olsa { 1065dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 1066dc0a6202SAdrian Hunter 1067cd82a32eSJiri Olsa attr->type = pmu->type; 1068e64b020bSJiri Olsa return perf_pmu__config_terms(&pmu->format, attr, head_terms, 1069e64b020bSJiri Olsa zero, err); 1070cd82a32eSJiri Olsa } 1071cd82a32eSJiri Olsa 10725c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 10736cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 1074a6146d50SZheng Yan { 10755c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1076a6146d50SZheng Yan char *name; 1077a6146d50SZheng Yan 1078a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 1079a6146d50SZheng Yan return NULL; 1080a6146d50SZheng Yan 1081a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 1082a6146d50SZheng Yan if (term->val.num != 1) 1083a6146d50SZheng Yan return NULL; 1084a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 1085a6146d50SZheng Yan return NULL; 1086a6146d50SZheng Yan name = term->config; 1087a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 1088a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 1089a6146d50SZheng Yan return NULL; 1090a6146d50SZheng Yan name = term->val.str; 1091a6146d50SZheng Yan } else { 1092a6146d50SZheng Yan return NULL; 1093a6146d50SZheng Yan } 1094a6146d50SZheng Yan 1095a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 1096a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 1097a6146d50SZheng Yan return alias; 1098a6146d50SZheng Yan } 1099a6146d50SZheng Yan return NULL; 1100a6146d50SZheng Yan } 1101a6146d50SZheng Yan 1102410136f5SStephane Eranian 11031d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 11041d9e446bSJiri Olsa struct perf_pmu_info *info) 1105410136f5SStephane Eranian { 1106410136f5SStephane Eranian /* 1107410136f5SStephane Eranian * Only one term in event definition can 11081d9e446bSJiri Olsa * define unit, scale and snapshot, fail 11091d9e446bSJiri Olsa * if there's more than one. 1110410136f5SStephane Eranian */ 1111b30a7d1fSArnaldo Carvalho de Melo if ((info->unit && alias->unit[0]) || 11121d9e446bSJiri Olsa (info->scale && alias->scale) || 11131d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 1114410136f5SStephane Eranian return -EINVAL; 1115410136f5SStephane Eranian 1116b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 11171d9e446bSJiri Olsa info->unit = alias->unit; 1118410136f5SStephane Eranian 1119410136f5SStephane Eranian if (alias->scale) 11201d9e446bSJiri Olsa info->scale = alias->scale; 11211d9e446bSJiri Olsa 11221d9e446bSJiri Olsa if (alias->snapshot) 11231d9e446bSJiri Olsa info->snapshot = alias->snapshot; 1124410136f5SStephane Eranian 1125410136f5SStephane Eranian return 0; 1126410136f5SStephane Eranian } 1127410136f5SStephane Eranian 1128a6146d50SZheng Yan /* 1129a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 1130a6146d50SZheng Yan * defined for the alias 1131a6146d50SZheng Yan */ 1132410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 113346441bdcSMatt Fleming struct perf_pmu_info *info) 1134a6146d50SZheng Yan { 11356cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 11365c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 1137a6146d50SZheng Yan int ret; 1138a6146d50SZheng Yan 1139044330c1SMatt Fleming info->per_pkg = false; 1140044330c1SMatt Fleming 11418a398897SStephane Eranian /* 11428a398897SStephane Eranian * Mark unit and scale as not set 11438a398897SStephane Eranian * (different from default values, see below) 11448a398897SStephane Eranian */ 114546441bdcSMatt Fleming info->unit = NULL; 114646441bdcSMatt Fleming info->scale = 0.0; 11471d9e446bSJiri Olsa info->snapshot = false; 114837932c18SAndi Kleen info->metric_expr = NULL; 114996284814SAndi Kleen info->metric_name = NULL; 1150410136f5SStephane Eranian 1151a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1152a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1153a6146d50SZheng Yan if (!alias) 1154a6146d50SZheng Yan continue; 1155a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 1156a6146d50SZheng Yan if (ret) 1157a6146d50SZheng Yan return ret; 1158410136f5SStephane Eranian 11591d9e446bSJiri Olsa ret = check_info_data(alias, info); 1160410136f5SStephane Eranian if (ret) 1161410136f5SStephane Eranian return ret; 1162410136f5SStephane Eranian 1163044330c1SMatt Fleming if (alias->per_pkg) 1164044330c1SMatt Fleming info->per_pkg = true; 116537932c18SAndi Kleen info->metric_expr = alias->metric_expr; 116696284814SAndi Kleen info->metric_name = alias->metric_name; 1167044330c1SMatt Fleming 1168a6146d50SZheng Yan list_del(&term->list); 1169a6146d50SZheng Yan free(term); 1170a6146d50SZheng Yan } 11718a398897SStephane Eranian 11728a398897SStephane Eranian /* 11738a398897SStephane Eranian * if no unit or scale foundin aliases, then 11748a398897SStephane Eranian * set defaults as for evsel 11758a398897SStephane Eranian * unit cannot left to NULL 11768a398897SStephane Eranian */ 117746441bdcSMatt Fleming if (info->unit == NULL) 117846441bdcSMatt Fleming info->unit = ""; 11798a398897SStephane Eranian 118046441bdcSMatt Fleming if (info->scale == 0.0) 118146441bdcSMatt Fleming info->scale = 1.0; 11828a398897SStephane Eranian 1183a6146d50SZheng Yan return 0; 1184a6146d50SZheng Yan } 1185a6146d50SZheng Yan 1186cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 1187cd82a32eSJiri Olsa int config, unsigned long *bits) 1188cd82a32eSJiri Olsa { 11895c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1190cd82a32eSJiri Olsa 1191cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 1192cd82a32eSJiri Olsa if (!format) 1193cd82a32eSJiri Olsa return -ENOMEM; 1194cd82a32eSJiri Olsa 1195cd82a32eSJiri Olsa format->name = strdup(name); 1196cd82a32eSJiri Olsa format->value = config; 1197cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 1198cd82a32eSJiri Olsa 1199cd82a32eSJiri Olsa list_add_tail(&format->list, list); 1200cd82a32eSJiri Olsa return 0; 1201cd82a32eSJiri Olsa } 1202cd82a32eSJiri Olsa 1203cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 1204cd82a32eSJiri Olsa { 1205cd82a32eSJiri Olsa long b; 1206cd82a32eSJiri Olsa 1207cd82a32eSJiri Olsa if (!to) 1208cd82a32eSJiri Olsa to = from; 1209cd82a32eSJiri Olsa 121015268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1211cd82a32eSJiri Olsa for (b = from; b <= to; b++) 1212cd82a32eSJiri Olsa set_bit(b, bits); 1213cd82a32eSJiri Olsa } 1214dc098b35SAndi Kleen 1215aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 1216aaea3617SCody P Schafer { 1217aaea3617SCody P Schafer if (b > a) 1218aaea3617SCody P Schafer return 0; 1219aaea3617SCody P Schafer return a - b; 1220aaea3617SCody P Schafer } 1221aaea3617SCody P Schafer 1222dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 1223dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1224dc098b35SAndi Kleen { 1225aaea3617SCody P Schafer struct parse_events_term *term; 1226aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1227aaea3617SCody P Schafer 1228aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 1229aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1230aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 1231aaea3617SCody P Schafer ",%s=%s", term->config, 1232aaea3617SCody P Schafer term->val.str); 1233aaea3617SCody P Schafer } 1234aaea3617SCody P Schafer 1235aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1236aaea3617SCody P Schafer buf[used] = '/'; 1237aaea3617SCody P Schafer used++; 1238aaea3617SCody P Schafer } 1239aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1240aaea3617SCody P Schafer buf[used] = '\0'; 1241aaea3617SCody P Schafer used++; 1242aaea3617SCody P Schafer } else 1243aaea3617SCody P Schafer buf[len - 1] = '\0'; 1244aaea3617SCody P Schafer 1245dc098b35SAndi Kleen return buf; 1246dc098b35SAndi Kleen } 1247dc098b35SAndi Kleen 1248dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 1249dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1250dc098b35SAndi Kleen { 1251dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 1252dc098b35SAndi Kleen return buf; 1253dc098b35SAndi Kleen } 1254dc098b35SAndi Kleen 1255dd5f1036SAndi Kleen struct sevent { 125608e60ed1SAndi Kleen char *name; 125708e60ed1SAndi Kleen char *desc; 1258dd5f1036SAndi Kleen char *topic; 1259f2361024SAndi Kleen char *str; 1260f2361024SAndi Kleen char *pmu; 12617f372a63SAndi Kleen char *metric_expr; 126296284814SAndi Kleen char *metric_name; 126308e60ed1SAndi Kleen }; 126408e60ed1SAndi Kleen 1265dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b) 1266dc098b35SAndi Kleen { 1267dd5f1036SAndi Kleen const struct sevent *as = a; 1268dd5f1036SAndi Kleen const struct sevent *bs = b; 126908e60ed1SAndi Kleen 127008e60ed1SAndi Kleen /* Put extra events last */ 127108e60ed1SAndi Kleen if (!!as->desc != !!bs->desc) 127208e60ed1SAndi Kleen return !!as->desc - !!bs->desc; 1273dd5f1036SAndi Kleen if (as->topic && bs->topic) { 1274dd5f1036SAndi Kleen int n = strcmp(as->topic, bs->topic); 1275dd5f1036SAndi Kleen 1276dd5f1036SAndi Kleen if (n) 1277dd5f1036SAndi Kleen return n; 1278dd5f1036SAndi Kleen } 127908e60ed1SAndi Kleen return strcmp(as->name, bs->name); 128008e60ed1SAndi Kleen } 128108e60ed1SAndi Kleen 128208e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr) 128308e60ed1SAndi Kleen { 128408e60ed1SAndi Kleen int column = start; 128508e60ed1SAndi Kleen int n; 128608e60ed1SAndi Kleen 128708e60ed1SAndi Kleen while (*s) { 128808e60ed1SAndi Kleen int wlen = strcspn(s, " \t"); 128908e60ed1SAndi Kleen 129008e60ed1SAndi Kleen if (column + wlen >= max && column > start) { 129108e60ed1SAndi Kleen printf("\n%*s", start, ""); 129208e60ed1SAndi Kleen column = start + corr; 129308e60ed1SAndi Kleen } 129408e60ed1SAndi Kleen n = printf("%s%.*s", column > start ? " " : "", wlen, s); 129508e60ed1SAndi Kleen if (n <= 0) 129608e60ed1SAndi Kleen break; 129708e60ed1SAndi Kleen s += wlen; 129808e60ed1SAndi Kleen column += n; 1299aa4beb10STaeung Song s = ltrim(s); 130008e60ed1SAndi Kleen } 1301dc098b35SAndi Kleen } 1302dc098b35SAndi Kleen 1303c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, 1304bf874fcfSAndi Kleen bool long_desc, bool details_flag) 1305dc098b35SAndi Kleen { 1306dc098b35SAndi Kleen struct perf_pmu *pmu; 1307dc098b35SAndi Kleen struct perf_pmu_alias *alias; 1308dc098b35SAndi Kleen char buf[1024]; 1309dc098b35SAndi Kleen int printed = 0; 1310dc098b35SAndi Kleen int len, j; 1311dd5f1036SAndi Kleen struct sevent *aliases; 131208e60ed1SAndi Kleen int numdesc = 0; 131361eb2eb4SAndi Kleen int columns = pager_get_columns(); 1314dd5f1036SAndi Kleen char *topic = NULL; 1315dc098b35SAndi Kleen 1316dc098b35SAndi Kleen pmu = NULL; 1317dc098b35SAndi Kleen len = 0; 131842634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1319dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 1320dc098b35SAndi Kleen len++; 132142634bc7SAdrian Hunter if (pmu->selectable) 132242634bc7SAdrian Hunter len++; 132342634bc7SAdrian Hunter } 1324dd5f1036SAndi Kleen aliases = zalloc(sizeof(struct sevent) * len); 1325dc098b35SAndi Kleen if (!aliases) 13267e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1327dc098b35SAndi Kleen pmu = NULL; 1328dc098b35SAndi Kleen j = 0; 132942634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1330dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 133108e60ed1SAndi Kleen char *name = alias->desc ? alias->name : 133208e60ed1SAndi Kleen format_alias(buf, sizeof(buf), pmu, alias); 1333dc098b35SAndi Kleen bool is_cpu = !strcmp(pmu->name, "cpu"); 1334dc098b35SAndi Kleen 1335dc098b35SAndi Kleen if (event_glob != NULL && 133638d14f0cSAndi Kleen !(strglobmatch_nocase(name, event_glob) || 133738d14f0cSAndi Kleen (!is_cpu && strglobmatch_nocase(alias->name, 133867bdc35fSAndi Kleen event_glob)) || 133967bdc35fSAndi Kleen (alias->topic && 134067bdc35fSAndi Kleen strglobmatch_nocase(alias->topic, event_glob)))) 1341dc098b35SAndi Kleen continue; 13427e4772dcSArnaldo Carvalho de Melo 134308e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 13447e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 13457e4772dcSArnaldo Carvalho de Melo 134608e60ed1SAndi Kleen aliases[j].name = name; 134708e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 134808e60ed1SAndi Kleen aliases[j].name = format_alias_or(buf, 134908e60ed1SAndi Kleen sizeof(buf), 135008e60ed1SAndi Kleen pmu, alias); 135108e60ed1SAndi Kleen aliases[j].name = strdup(aliases[j].name); 135208e60ed1SAndi Kleen if (!aliases[j].name) 13537e4772dcSArnaldo Carvalho de Melo goto out_enomem; 135408e60ed1SAndi Kleen 1355c8d6828aSSukadev Bhattiprolu aliases[j].desc = long_desc ? alias->long_desc : 1356c8d6828aSSukadev Bhattiprolu alias->desc; 1357dd5f1036SAndi Kleen aliases[j].topic = alias->topic; 1358f2361024SAndi Kleen aliases[j].str = alias->str; 1359f2361024SAndi Kleen aliases[j].pmu = pmu->name; 13607f372a63SAndi Kleen aliases[j].metric_expr = alias->metric_expr; 136196284814SAndi Kleen aliases[j].metric_name = alias->metric_name; 1362dc098b35SAndi Kleen j++; 1363dc098b35SAndi Kleen } 1364fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1365fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 13667e4772dcSArnaldo Carvalho de Melo char *s; 13677e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 13687e4772dcSArnaldo Carvalho de Melo goto out_enomem; 136908e60ed1SAndi Kleen aliases[j].name = s; 137042634bc7SAdrian Hunter j++; 137142634bc7SAdrian Hunter } 137242634bc7SAdrian Hunter } 1373dc098b35SAndi Kleen len = j; 1374dd5f1036SAndi Kleen qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1375dc098b35SAndi Kleen for (j = 0; j < len; j++) { 137615b22ed3SAndi Kleen /* Skip duplicates */ 137715b22ed3SAndi Kleen if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) 137815b22ed3SAndi Kleen continue; 1379dc098b35SAndi Kleen if (name_only) { 138008e60ed1SAndi Kleen printf("%s ", aliases[j].name); 1381dc098b35SAndi Kleen continue; 1382dc098b35SAndi Kleen } 13831c5f01feSAndi Kleen if (aliases[j].desc && !quiet_flag) { 138408e60ed1SAndi Kleen if (numdesc++ == 0) 138508e60ed1SAndi Kleen printf("\n"); 1386dd5f1036SAndi Kleen if (aliases[j].topic && (!topic || 1387dd5f1036SAndi Kleen strcmp(topic, aliases[j].topic))) { 1388dd5f1036SAndi Kleen printf("%s%s:\n", topic ? "\n" : "", 1389dd5f1036SAndi Kleen aliases[j].topic); 1390dd5f1036SAndi Kleen topic = aliases[j].topic; 1391dd5f1036SAndi Kleen } 139208e60ed1SAndi Kleen printf(" %-50s\n", aliases[j].name); 139308e60ed1SAndi Kleen printf("%*s", 8, "["); 139408e60ed1SAndi Kleen wordwrap(aliases[j].desc, 8, columns, 0); 139508e60ed1SAndi Kleen printf("]\n"); 1396bf874fcfSAndi Kleen if (details_flag) { 13977f372a63SAndi Kleen printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); 139896284814SAndi Kleen if (aliases[j].metric_name) 139996284814SAndi Kleen printf(" MetricName: %s", aliases[j].metric_name); 14007f372a63SAndi Kleen if (aliases[j].metric_expr) 14017f372a63SAndi Kleen printf(" MetricExpr: %s", aliases[j].metric_expr); 14027f372a63SAndi Kleen putchar('\n'); 14037f372a63SAndi Kleen } 140408e60ed1SAndi Kleen } else 140508e60ed1SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j].name); 1406dc098b35SAndi Kleen printed++; 1407dc098b35SAndi Kleen } 1408dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1409dc098b35SAndi Kleen printf("\n"); 14107e4772dcSArnaldo Carvalho de Melo out_free: 14117e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 141208e60ed1SAndi Kleen zfree(&aliases[j].name); 14137e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 14147e4772dcSArnaldo Carvalho de Melo return; 14157e4772dcSArnaldo Carvalho de Melo 14167e4772dcSArnaldo Carvalho de Melo out_enomem: 14177e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 14187e4772dcSArnaldo Carvalho de Melo if (aliases) 14197e4772dcSArnaldo Carvalho de Melo goto out_free; 1420dc098b35SAndi Kleen } 14214cabc3d1SAndi Kleen 14224cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 14234cabc3d1SAndi Kleen { 14244cabc3d1SAndi Kleen struct perf_pmu *pmu; 14254cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 14264cabc3d1SAndi Kleen 14274cabc3d1SAndi Kleen pmu = NULL; 14284cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 14294cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 14304cabc3d1SAndi Kleen continue; 14314cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 14324cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 14334cabc3d1SAndi Kleen return true; 14344cabc3d1SAndi Kleen } 14354cabc3d1SAndi Kleen return false; 14364cabc3d1SAndi Kleen } 14377d4bdab5SAdrian Hunter 14387d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 14397d4bdab5SAdrian Hunter { 14407d4bdab5SAdrian Hunter struct stat st; 14417d4bdab5SAdrian Hunter char path[PATH_MAX]; 14427d4bdab5SAdrian Hunter const char *sysfs; 14437d4bdab5SAdrian Hunter 14447d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 14457d4bdab5SAdrian Hunter if (!sysfs) 14467d4bdab5SAdrian Hunter return NULL; 14477d4bdab5SAdrian Hunter 14487d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 14497d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 14507d4bdab5SAdrian Hunter 14517d4bdab5SAdrian Hunter if (stat(path, &st) < 0) 14527d4bdab5SAdrian Hunter return NULL; 14537d4bdab5SAdrian Hunter 14547d4bdab5SAdrian Hunter return fopen(path, "r"); 14557d4bdab5SAdrian Hunter } 14567d4bdab5SAdrian Hunter 14577d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 14587d4bdab5SAdrian Hunter ...) 14597d4bdab5SAdrian Hunter { 14607d4bdab5SAdrian Hunter va_list args; 14617d4bdab5SAdrian Hunter FILE *file; 14627d4bdab5SAdrian Hunter int ret = EOF; 14637d4bdab5SAdrian Hunter 14647d4bdab5SAdrian Hunter va_start(args, fmt); 14657d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 14667d4bdab5SAdrian Hunter if (file) { 14677d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 14687d4bdab5SAdrian Hunter fclose(file); 14697d4bdab5SAdrian Hunter } 14707d4bdab5SAdrian Hunter va_end(args); 14717d4bdab5SAdrian Hunter return ret; 14727d4bdab5SAdrian Hunter } 1473