1cd82a32eSJiri Olsa #include <linux/list.h> 2c5de47f2SSukadev Bhattiprolu #include <linux/compiler.h> 3cd82a32eSJiri Olsa #include <sys/types.h> 4cd82a32eSJiri Olsa #include <unistd.h> 5cd82a32eSJiri Olsa #include <stdio.h> 6dc0a6202SAdrian Hunter #include <stdbool.h> 77d4bdab5SAdrian Hunter #include <stdarg.h> 8cd82a32eSJiri Olsa #include <dirent.h> 9cd0cfad7SBorislav Petkov #include <api/fs/fs.h> 10410136f5SStephane Eranian #include <locale.h> 11cd82a32eSJiri Olsa #include "util.h" 12cd82a32eSJiri Olsa #include "pmu.h" 13cd82a32eSJiri Olsa #include "parse-events.h" 147ae92e74SYan, Zheng #include "cpumap.h" 15933f82ffSSukadev Bhattiprolu #include "header.h" 16933f82ffSSukadev Bhattiprolu #include "pmu-events/pmu-events.h" 1761eb2eb4SAndi Kleen #include "cache.h" 18cd82a32eSJiri Olsa 19ab1bf653SArnaldo Carvalho de Melo struct perf_pmu_format { 20ab1bf653SArnaldo Carvalho de Melo char *name; 21ab1bf653SArnaldo Carvalho de Melo int value; 22ab1bf653SArnaldo Carvalho de Melo DECLARE_BITMAP(bits, PERF_PMU_FORMAT_BITS); 23ab1bf653SArnaldo Carvalho de Melo struct list_head list; 24ab1bf653SArnaldo Carvalho de Melo }; 25ab1bf653SArnaldo Carvalho de Melo 2650a9667cSRobert Richter #define EVENT_SOURCE_DEVICE_PATH "/bus/event_source/devices/" 2750a9667cSRobert Richter 28cd82a32eSJiri Olsa int perf_pmu_parse(struct list_head *list, char *name); 29cd82a32eSJiri Olsa extern FILE *perf_pmu_in; 30cd82a32eSJiri Olsa 31cd82a32eSJiri Olsa static LIST_HEAD(pmus); 32cd82a32eSJiri Olsa 33cd82a32eSJiri Olsa /* 34cd82a32eSJiri Olsa * Parse & process all the sysfs attributes located under 35cd82a32eSJiri Olsa * the directory specified in 'dir' parameter. 36cd82a32eSJiri Olsa */ 37cff7f956SJiri Olsa int perf_pmu__format_parse(char *dir, struct list_head *head) 38cd82a32eSJiri Olsa { 39cd82a32eSJiri Olsa struct dirent *evt_ent; 40cd82a32eSJiri Olsa DIR *format_dir; 41cd82a32eSJiri Olsa int ret = 0; 42cd82a32eSJiri Olsa 43cd82a32eSJiri Olsa format_dir = opendir(dir); 44cd82a32eSJiri Olsa if (!format_dir) 45cd82a32eSJiri Olsa return -EINVAL; 46cd82a32eSJiri Olsa 47cd82a32eSJiri Olsa while (!ret && (evt_ent = readdir(format_dir))) { 48cd82a32eSJiri Olsa char path[PATH_MAX]; 49cd82a32eSJiri Olsa char *name = evt_ent->d_name; 50cd82a32eSJiri Olsa FILE *file; 51cd82a32eSJiri Olsa 52cd82a32eSJiri Olsa if (!strcmp(name, ".") || !strcmp(name, "..")) 53cd82a32eSJiri Olsa continue; 54cd82a32eSJiri Olsa 55cd82a32eSJiri Olsa snprintf(path, PATH_MAX, "%s/%s", dir, name); 56cd82a32eSJiri Olsa 57cd82a32eSJiri Olsa ret = -EINVAL; 58cd82a32eSJiri Olsa file = fopen(path, "r"); 59cd82a32eSJiri Olsa if (!file) 60cd82a32eSJiri Olsa break; 61cd82a32eSJiri Olsa 62cd82a32eSJiri Olsa perf_pmu_in = file; 63cd82a32eSJiri Olsa ret = perf_pmu_parse(head, name); 64cd82a32eSJiri Olsa fclose(file); 65cd82a32eSJiri Olsa } 66cd82a32eSJiri Olsa 67cd82a32eSJiri Olsa closedir(format_dir); 68cd82a32eSJiri Olsa return ret; 69cd82a32eSJiri Olsa } 70cd82a32eSJiri Olsa 71cd82a32eSJiri Olsa /* 72cd82a32eSJiri Olsa * Reading/parsing the default pmu format definition, which should be 73cd82a32eSJiri Olsa * located at: 74cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/format as sysfs group attributes. 75cd82a32eSJiri Olsa */ 76b6b96fb4SAdrian Hunter static int pmu_format(const char *name, struct list_head *format) 77cd82a32eSJiri Olsa { 78cd82a32eSJiri Olsa struct stat st; 79cd82a32eSJiri Olsa char path[PATH_MAX]; 80cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 81cd82a32eSJiri Olsa 82cd82a32eSJiri Olsa if (!sysfs) 83cd82a32eSJiri Olsa return -1; 84cd82a32eSJiri Olsa 85cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 8650a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/format", sysfs, name); 87cd82a32eSJiri Olsa 88cd82a32eSJiri Olsa if (stat(path, &st) < 0) 899bc8f9feSRobert Richter return 0; /* no error if format does not exist */ 90cd82a32eSJiri Olsa 91cff7f956SJiri Olsa if (perf_pmu__format_parse(path, format)) 92cd82a32eSJiri Olsa return -1; 93cd82a32eSJiri Olsa 94cd82a32eSJiri Olsa return 0; 95cd82a32eSJiri Olsa } 96cd82a32eSJiri Olsa 97d02fc6bcSAndi Kleen static int convert_scale(const char *scale, char **end, double *sval) 98d02fc6bcSAndi Kleen { 99d02fc6bcSAndi Kleen char *lc; 100d02fc6bcSAndi Kleen int ret = 0; 101d02fc6bcSAndi Kleen 102d02fc6bcSAndi Kleen /* 103d02fc6bcSAndi Kleen * save current locale 104d02fc6bcSAndi Kleen */ 105d02fc6bcSAndi Kleen lc = setlocale(LC_NUMERIC, NULL); 106d02fc6bcSAndi Kleen 107d02fc6bcSAndi Kleen /* 108d02fc6bcSAndi Kleen * The lc string may be allocated in static storage, 109d02fc6bcSAndi Kleen * so get a dynamic copy to make it survive setlocale 110d02fc6bcSAndi Kleen * call below. 111d02fc6bcSAndi Kleen */ 112d02fc6bcSAndi Kleen lc = strdup(lc); 113d02fc6bcSAndi Kleen if (!lc) { 114d02fc6bcSAndi Kleen ret = -ENOMEM; 115d02fc6bcSAndi Kleen goto out; 116d02fc6bcSAndi Kleen } 117d02fc6bcSAndi Kleen 118d02fc6bcSAndi Kleen /* 119d02fc6bcSAndi Kleen * force to C locale to ensure kernel 120d02fc6bcSAndi Kleen * scale string is converted correctly. 121d02fc6bcSAndi Kleen * kernel uses default C locale. 122d02fc6bcSAndi Kleen */ 123d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, "C"); 124d02fc6bcSAndi Kleen 125d02fc6bcSAndi Kleen *sval = strtod(scale, end); 126d02fc6bcSAndi Kleen 127d02fc6bcSAndi Kleen out: 128d02fc6bcSAndi Kleen /* restore locale */ 129d02fc6bcSAndi Kleen setlocale(LC_NUMERIC, lc); 130d02fc6bcSAndi Kleen free(lc); 131d02fc6bcSAndi Kleen return ret; 132d02fc6bcSAndi Kleen } 133d02fc6bcSAndi Kleen 134410136f5SStephane Eranian static int perf_pmu__parse_scale(struct perf_pmu_alias *alias, char *dir, char *name) 135410136f5SStephane Eranian { 136410136f5SStephane Eranian struct stat st; 137410136f5SStephane Eranian ssize_t sret; 138410136f5SStephane Eranian char scale[128]; 139410136f5SStephane Eranian int fd, ret = -1; 140410136f5SStephane Eranian char path[PATH_MAX]; 141410136f5SStephane Eranian 142410136f5SStephane Eranian snprintf(path, PATH_MAX, "%s/%s.scale", dir, name); 143410136f5SStephane Eranian 144410136f5SStephane Eranian fd = open(path, O_RDONLY); 145410136f5SStephane Eranian if (fd == -1) 146410136f5SStephane Eranian return -1; 147410136f5SStephane Eranian 148410136f5SStephane Eranian if (fstat(fd, &st) < 0) 149410136f5SStephane Eranian goto error; 150410136f5SStephane Eranian 151410136f5SStephane Eranian sret = read(fd, scale, sizeof(scale)-1); 152410136f5SStephane Eranian if (sret < 0) 153410136f5SStephane Eranian goto error; 154410136f5SStephane Eranian 1559ecae065SMadhavan Srinivasan if (scale[sret - 1] == '\n') 1569ecae065SMadhavan Srinivasan scale[sret - 1] = '\0'; 1579ecae065SMadhavan Srinivasan else 158410136f5SStephane Eranian scale[sret] = '\0'; 1599ecae065SMadhavan Srinivasan 160d02fc6bcSAndi Kleen ret = convert_scale(scale, NULL, &alias->scale); 161410136f5SStephane Eranian error: 162410136f5SStephane Eranian close(fd); 163410136f5SStephane Eranian return ret; 164410136f5SStephane Eranian } 165410136f5SStephane Eranian 166410136f5SStephane Eranian static int perf_pmu__parse_unit(struct perf_pmu_alias *alias, char *dir, char *name) 167410136f5SStephane Eranian { 168410136f5SStephane Eranian char path[PATH_MAX]; 169410136f5SStephane Eranian ssize_t sret; 170410136f5SStephane Eranian int fd; 171410136f5SStephane Eranian 172410136f5SStephane Eranian snprintf(path, PATH_MAX, "%s/%s.unit", dir, name); 173410136f5SStephane Eranian 174410136f5SStephane Eranian fd = open(path, O_RDONLY); 175410136f5SStephane Eranian if (fd == -1) 176410136f5SStephane Eranian return -1; 177410136f5SStephane Eranian 178410136f5SStephane Eranian sret = read(fd, alias->unit, UNIT_MAX_LEN); 179410136f5SStephane Eranian if (sret < 0) 180410136f5SStephane Eranian goto error; 181410136f5SStephane Eranian 182410136f5SStephane Eranian close(fd); 183410136f5SStephane Eranian 1849ecae065SMadhavan Srinivasan if (alias->unit[sret - 1] == '\n') 1859ecae065SMadhavan Srinivasan alias->unit[sret - 1] = '\0'; 1869ecae065SMadhavan Srinivasan else 187410136f5SStephane Eranian alias->unit[sret] = '\0'; 188410136f5SStephane Eranian 189410136f5SStephane Eranian return 0; 190410136f5SStephane Eranian error: 191410136f5SStephane Eranian close(fd); 192410136f5SStephane Eranian alias->unit[0] = '\0'; 193410136f5SStephane Eranian return -1; 194410136f5SStephane Eranian } 195410136f5SStephane Eranian 196044330c1SMatt Fleming static int 197044330c1SMatt Fleming perf_pmu__parse_per_pkg(struct perf_pmu_alias *alias, char *dir, char *name) 198044330c1SMatt Fleming { 199044330c1SMatt Fleming char path[PATH_MAX]; 200044330c1SMatt Fleming int fd; 201044330c1SMatt Fleming 202044330c1SMatt Fleming snprintf(path, PATH_MAX, "%s/%s.per-pkg", dir, name); 203044330c1SMatt Fleming 204044330c1SMatt Fleming fd = open(path, O_RDONLY); 205044330c1SMatt Fleming if (fd == -1) 206044330c1SMatt Fleming return -1; 207044330c1SMatt Fleming 208044330c1SMatt Fleming close(fd); 209044330c1SMatt Fleming 210044330c1SMatt Fleming alias->per_pkg = true; 211044330c1SMatt Fleming return 0; 212044330c1SMatt Fleming } 213044330c1SMatt Fleming 2141d9e446bSJiri Olsa static int perf_pmu__parse_snapshot(struct perf_pmu_alias *alias, 2151d9e446bSJiri Olsa char *dir, char *name) 2161d9e446bSJiri Olsa { 2171d9e446bSJiri Olsa char path[PATH_MAX]; 2181d9e446bSJiri Olsa int fd; 2191d9e446bSJiri Olsa 2201d9e446bSJiri Olsa snprintf(path, PATH_MAX, "%s/%s.snapshot", dir, name); 2211d9e446bSJiri Olsa 2221d9e446bSJiri Olsa fd = open(path, O_RDONLY); 2231d9e446bSJiri Olsa if (fd == -1) 2241d9e446bSJiri Olsa return -1; 2251d9e446bSJiri Olsa 2261d9e446bSJiri Olsa alias->snapshot = true; 2271d9e446bSJiri Olsa close(fd); 2281d9e446bSJiri Olsa return 0; 2291d9e446bSJiri Olsa } 2301d9e446bSJiri Olsa 23170c646e0SSukadev Bhattiprolu static int __perf_pmu__new_alias(struct list_head *list, char *dir, char *name, 232fedb2b51SAndi Kleen char *desc, char *val, 233fedb2b51SAndi Kleen char *long_desc, char *topic, 23400636c3bSAndi Kleen char *unit, char *perpkg, 23596284814SAndi Kleen char *metric_expr, 23696284814SAndi Kleen char *metric_name) 237a6146d50SZheng Yan { 2385c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 239a6146d50SZheng Yan int ret; 240fedb2b51SAndi Kleen int num; 241a6146d50SZheng Yan 242a6146d50SZheng Yan alias = malloc(sizeof(*alias)); 243a6146d50SZheng Yan if (!alias) 244a6146d50SZheng Yan return -ENOMEM; 245a6146d50SZheng Yan 246a6146d50SZheng Yan INIT_LIST_HEAD(&alias->terms); 247410136f5SStephane Eranian alias->scale = 1.0; 248410136f5SStephane Eranian alias->unit[0] = '\0'; 249044330c1SMatt Fleming alias->per_pkg = false; 25084530920SStephane Eranian alias->snapshot = false; 251410136f5SStephane Eranian 25270c646e0SSukadev Bhattiprolu ret = parse_events_terms(&alias->terms, val); 253a6146d50SZheng Yan if (ret) { 25470c646e0SSukadev Bhattiprolu pr_err("Cannot parse alias %s: %d\n", val, ret); 255a6146d50SZheng Yan free(alias); 256a6146d50SZheng Yan return ret; 257a6146d50SZheng Yan } 258a6146d50SZheng Yan 259a6146d50SZheng Yan alias->name = strdup(name); 26070c646e0SSukadev Bhattiprolu if (dir) { 261410136f5SStephane Eranian /* 262410136f5SStephane Eranian * load unit name and scale if available 263410136f5SStephane Eranian */ 264410136f5SStephane Eranian perf_pmu__parse_unit(alias, dir, name); 265410136f5SStephane Eranian perf_pmu__parse_scale(alias, dir, name); 266044330c1SMatt Fleming perf_pmu__parse_per_pkg(alias, dir, name); 2671d9e446bSJiri Olsa perf_pmu__parse_snapshot(alias, dir, name); 26870c646e0SSukadev Bhattiprolu } 269410136f5SStephane Eranian 27000636c3bSAndi Kleen alias->metric_expr = metric_expr ? strdup(metric_expr) : NULL; 27196284814SAndi Kleen alias->metric_name = metric_name ? strdup(metric_name): NULL; 27208e60ed1SAndi Kleen alias->desc = desc ? strdup(desc) : NULL; 273c8d6828aSSukadev Bhattiprolu alias->long_desc = long_desc ? strdup(long_desc) : 274c8d6828aSSukadev Bhattiprolu desc ? strdup(desc) : NULL; 275dd5f1036SAndi Kleen alias->topic = topic ? strdup(topic) : NULL; 276fedb2b51SAndi Kleen if (unit) { 277fedb2b51SAndi Kleen if (convert_scale(unit, &unit, &alias->scale) < 0) 278fedb2b51SAndi Kleen return -1; 279fedb2b51SAndi Kleen snprintf(alias->unit, sizeof(alias->unit), "%s", unit); 280fedb2b51SAndi Kleen } 281fedb2b51SAndi Kleen alias->per_pkg = perpkg && sscanf(perpkg, "%d", &num) == 1 && num == 1; 282f2361024SAndi Kleen alias->str = strdup(val); 283f2361024SAndi Kleen 284a6146d50SZheng Yan list_add_tail(&alias->list, list); 285410136f5SStephane Eranian 286a6146d50SZheng Yan return 0; 287a6146d50SZheng Yan } 288a6146d50SZheng Yan 28970c646e0SSukadev Bhattiprolu static int perf_pmu__new_alias(struct list_head *list, char *dir, char *name, FILE *file) 29070c646e0SSukadev Bhattiprolu { 29170c646e0SSukadev Bhattiprolu char buf[256]; 29270c646e0SSukadev Bhattiprolu int ret; 29370c646e0SSukadev Bhattiprolu 29470c646e0SSukadev Bhattiprolu ret = fread(buf, 1, sizeof(buf), file); 29570c646e0SSukadev Bhattiprolu if (ret == 0) 29670c646e0SSukadev Bhattiprolu return -EINVAL; 29770c646e0SSukadev Bhattiprolu 29870c646e0SSukadev Bhattiprolu buf[ret] = 0; 29970c646e0SSukadev Bhattiprolu 300fedb2b51SAndi Kleen return __perf_pmu__new_alias(list, dir, name, NULL, buf, NULL, NULL, NULL, 30196284814SAndi Kleen NULL, NULL, NULL); 30270c646e0SSukadev Bhattiprolu } 30370c646e0SSukadev Bhattiprolu 30446441bdcSMatt Fleming static inline bool pmu_alias_info_file(char *name) 30546441bdcSMatt Fleming { 30646441bdcSMatt Fleming size_t len; 30746441bdcSMatt Fleming 30846441bdcSMatt Fleming len = strlen(name); 30946441bdcSMatt Fleming if (len > 5 && !strcmp(name + len - 5, ".unit")) 31046441bdcSMatt Fleming return true; 31146441bdcSMatt Fleming if (len > 6 && !strcmp(name + len - 6, ".scale")) 31246441bdcSMatt Fleming return true; 313044330c1SMatt Fleming if (len > 8 && !strcmp(name + len - 8, ".per-pkg")) 314044330c1SMatt Fleming return true; 3151d9e446bSJiri Olsa if (len > 9 && !strcmp(name + len - 9, ".snapshot")) 3161d9e446bSJiri Olsa return true; 31746441bdcSMatt Fleming 31846441bdcSMatt Fleming return false; 31946441bdcSMatt Fleming } 32046441bdcSMatt Fleming 321a6146d50SZheng Yan /* 322a6146d50SZheng Yan * Process all the sysfs attributes located under the directory 323a6146d50SZheng Yan * specified in 'dir' parameter. 324a6146d50SZheng Yan */ 325a6146d50SZheng Yan static int pmu_aliases_parse(char *dir, struct list_head *head) 326a6146d50SZheng Yan { 327a6146d50SZheng Yan struct dirent *evt_ent; 328a6146d50SZheng Yan DIR *event_dir; 329a6146d50SZheng Yan 330a6146d50SZheng Yan event_dir = opendir(dir); 331a6146d50SZheng Yan if (!event_dir) 332a6146d50SZheng Yan return -EINVAL; 333a6146d50SZheng Yan 334940db6dcSAndi Kleen while ((evt_ent = readdir(event_dir))) { 335a6146d50SZheng Yan char path[PATH_MAX]; 336a6146d50SZheng Yan char *name = evt_ent->d_name; 337a6146d50SZheng Yan FILE *file; 338a6146d50SZheng Yan 339a6146d50SZheng Yan if (!strcmp(name, ".") || !strcmp(name, "..")) 340a6146d50SZheng Yan continue; 341a6146d50SZheng Yan 342410136f5SStephane Eranian /* 34346441bdcSMatt Fleming * skip info files parsed in perf_pmu__new_alias() 344410136f5SStephane Eranian */ 34546441bdcSMatt Fleming if (pmu_alias_info_file(name)) 346410136f5SStephane Eranian continue; 347410136f5SStephane Eranian 348a6146d50SZheng Yan snprintf(path, PATH_MAX, "%s/%s", dir, name); 349a6146d50SZheng Yan 350a6146d50SZheng Yan file = fopen(path, "r"); 351940db6dcSAndi Kleen if (!file) { 352940db6dcSAndi Kleen pr_debug("Cannot open %s\n", path); 353940db6dcSAndi Kleen continue; 354940db6dcSAndi Kleen } 355410136f5SStephane Eranian 356940db6dcSAndi Kleen if (perf_pmu__new_alias(head, dir, name, file) < 0) 357940db6dcSAndi Kleen pr_debug("Cannot set up %s\n", name); 358a6146d50SZheng Yan fclose(file); 359a6146d50SZheng Yan } 360a6146d50SZheng Yan 361a6146d50SZheng Yan closedir(event_dir); 362940db6dcSAndi Kleen return 0; 363a6146d50SZheng Yan } 364a6146d50SZheng Yan 365a6146d50SZheng Yan /* 366a6146d50SZheng Yan * Reading the pmu event aliases definition, which should be located at: 367a6146d50SZheng Yan * /sys/bus/event_source/devices/<dev>/events as sysfs group attributes. 368a6146d50SZheng Yan */ 369b6b96fb4SAdrian Hunter static int pmu_aliases(const char *name, struct list_head *head) 370a6146d50SZheng Yan { 371a6146d50SZheng Yan struct stat st; 372a6146d50SZheng Yan char path[PATH_MAX]; 373cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 374a6146d50SZheng Yan 375a6146d50SZheng Yan if (!sysfs) 376a6146d50SZheng Yan return -1; 377a6146d50SZheng Yan 378a6146d50SZheng Yan snprintf(path, PATH_MAX, 379a6146d50SZheng Yan "%s/bus/event_source/devices/%s/events", sysfs, name); 380a6146d50SZheng Yan 381a6146d50SZheng Yan if (stat(path, &st) < 0) 3823fded963SJiri Olsa return 0; /* no error if 'events' does not exist */ 383a6146d50SZheng Yan 384a6146d50SZheng Yan if (pmu_aliases_parse(path, head)) 385a6146d50SZheng Yan return -1; 386a6146d50SZheng Yan 387a6146d50SZheng Yan return 0; 388a6146d50SZheng Yan } 389a6146d50SZheng Yan 3905c6ccc37SArnaldo Carvalho de Melo static int pmu_alias_terms(struct perf_pmu_alias *alias, 391a6146d50SZheng Yan struct list_head *terms) 392a6146d50SZheng Yan { 3937c2f8164SJiri Olsa struct parse_events_term *term, *cloned; 394a6146d50SZheng Yan LIST_HEAD(list); 395a6146d50SZheng Yan int ret; 396a6146d50SZheng Yan 397a6146d50SZheng Yan list_for_each_entry(term, &alias->terms, list) { 3987c2f8164SJiri Olsa ret = parse_events_term__clone(&cloned, term); 399a6146d50SZheng Yan if (ret) { 400682dc24cSArnaldo Carvalho de Melo parse_events_terms__purge(&list); 401a6146d50SZheng Yan return ret; 402a6146d50SZheng Yan } 4037c2f8164SJiri Olsa list_add_tail(&cloned->list, &list); 404a6146d50SZheng Yan } 405a6146d50SZheng Yan list_splice(&list, terms); 406a6146d50SZheng Yan return 0; 407a6146d50SZheng Yan } 408a6146d50SZheng Yan 409cd82a32eSJiri Olsa /* 410cd82a32eSJiri Olsa * Reading/parsing the default pmu type value, which should be 411cd82a32eSJiri Olsa * located at: 412cd82a32eSJiri Olsa * /sys/bus/event_source/devices/<dev>/type as sysfs attribute. 413cd82a32eSJiri Olsa */ 414b6b96fb4SAdrian Hunter static int pmu_type(const char *name, __u32 *type) 415cd82a32eSJiri Olsa { 416cd82a32eSJiri Olsa struct stat st; 417cd82a32eSJiri Olsa char path[PATH_MAX]; 418cd82a32eSJiri Olsa FILE *file; 419cd82a32eSJiri Olsa int ret = 0; 420cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 421cd82a32eSJiri Olsa 422cd82a32eSJiri Olsa if (!sysfs) 423cd82a32eSJiri Olsa return -1; 424cd82a32eSJiri Olsa 425cd82a32eSJiri Olsa snprintf(path, PATH_MAX, 42650a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH "%s/type", sysfs, name); 427cd82a32eSJiri Olsa 428cd82a32eSJiri Olsa if (stat(path, &st) < 0) 429cd82a32eSJiri Olsa return -1; 430cd82a32eSJiri Olsa 431cd82a32eSJiri Olsa file = fopen(path, "r"); 432cd82a32eSJiri Olsa if (!file) 433cd82a32eSJiri Olsa return -EINVAL; 434cd82a32eSJiri Olsa 435cd82a32eSJiri Olsa if (1 != fscanf(file, "%u", type)) 436cd82a32eSJiri Olsa ret = -1; 437cd82a32eSJiri Olsa 438cd82a32eSJiri Olsa fclose(file); 439cd82a32eSJiri Olsa return ret; 440cd82a32eSJiri Olsa } 441cd82a32eSJiri Olsa 44250a9667cSRobert Richter /* Add all pmus in sysfs to pmu list: */ 44350a9667cSRobert Richter static void pmu_read_sysfs(void) 44450a9667cSRobert Richter { 44550a9667cSRobert Richter char path[PATH_MAX]; 44650a9667cSRobert Richter DIR *dir; 44750a9667cSRobert Richter struct dirent *dent; 448cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 44950a9667cSRobert Richter 45050a9667cSRobert Richter if (!sysfs) 45150a9667cSRobert Richter return; 45250a9667cSRobert Richter 45350a9667cSRobert Richter snprintf(path, PATH_MAX, 45450a9667cSRobert Richter "%s" EVENT_SOURCE_DEVICE_PATH, sysfs); 45550a9667cSRobert Richter 45650a9667cSRobert Richter dir = opendir(path); 45750a9667cSRobert Richter if (!dir) 45850a9667cSRobert Richter return; 45950a9667cSRobert Richter 46050a9667cSRobert Richter while ((dent = readdir(dir))) { 46150a9667cSRobert Richter if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) 46250a9667cSRobert Richter continue; 46350a9667cSRobert Richter /* add to static LIST_HEAD(pmus): */ 46450a9667cSRobert Richter perf_pmu__find(dent->d_name); 46550a9667cSRobert Richter } 46650a9667cSRobert Richter 46750a9667cSRobert Richter closedir(dir); 46850a9667cSRobert Richter } 46950a9667cSRobert Richter 470b6b96fb4SAdrian Hunter static struct cpu_map *pmu_cpumask(const char *name) 4717ae92e74SYan, Zheng { 4727ae92e74SYan, Zheng struct stat st; 4737ae92e74SYan, Zheng char path[PATH_MAX]; 4747ae92e74SYan, Zheng FILE *file; 4757ae92e74SYan, Zheng struct cpu_map *cpus; 476cf38fadaSArnaldo Carvalho de Melo const char *sysfs = sysfs__mountpoint(); 4777e3fcffeSMark Rutland const char *templates[] = { 4787e3fcffeSMark Rutland "%s/bus/event_source/devices/%s/cpumask", 4797e3fcffeSMark Rutland "%s/bus/event_source/devices/%s/cpus", 4807e3fcffeSMark Rutland NULL 4817e3fcffeSMark Rutland }; 4827e3fcffeSMark Rutland const char **template; 4837ae92e74SYan, Zheng 4847ae92e74SYan, Zheng if (!sysfs) 4857ae92e74SYan, Zheng return NULL; 4867ae92e74SYan, Zheng 4877e3fcffeSMark Rutland for (template = templates; *template; template++) { 4887e3fcffeSMark Rutland snprintf(path, PATH_MAX, *template, sysfs, name); 4897e3fcffeSMark Rutland if (stat(path, &st) == 0) 4907e3fcffeSMark Rutland break; 4917e3fcffeSMark Rutland } 4927ae92e74SYan, Zheng 4937e3fcffeSMark Rutland if (!*template) 4947ae92e74SYan, Zheng return NULL; 4957ae92e74SYan, Zheng 4967ae92e74SYan, Zheng file = fopen(path, "r"); 4977ae92e74SYan, Zheng if (!file) 4987ae92e74SYan, Zheng return NULL; 4997ae92e74SYan, Zheng 5007ae92e74SYan, Zheng cpus = cpu_map__read(file); 5017ae92e74SYan, Zheng fclose(file); 5027ae92e74SYan, Zheng return cpus; 5037ae92e74SYan, Zheng } 5047ae92e74SYan, Zheng 505933f82ffSSukadev Bhattiprolu /* 506933f82ffSSukadev Bhattiprolu * Return the CPU id as a raw string. 507933f82ffSSukadev Bhattiprolu * 508933f82ffSSukadev Bhattiprolu * Each architecture should provide a more precise id string that 509933f82ffSSukadev Bhattiprolu * can be use to match the architecture's "mapfile". 510933f82ffSSukadev Bhattiprolu */ 511933f82ffSSukadev Bhattiprolu char * __weak get_cpuid_str(void) 512933f82ffSSukadev Bhattiprolu { 513933f82ffSSukadev Bhattiprolu return NULL; 514933f82ffSSukadev Bhattiprolu } 515933f82ffSSukadev Bhattiprolu 516933f82ffSSukadev Bhattiprolu /* 517933f82ffSSukadev Bhattiprolu * From the pmu_events_map, find the table of PMU events that corresponds 518933f82ffSSukadev Bhattiprolu * to the current running CPU. Then, add all PMU events from that table 519933f82ffSSukadev Bhattiprolu * as aliases. 520933f82ffSSukadev Bhattiprolu */ 521fedb2b51SAndi Kleen static void pmu_add_cpu_aliases(struct list_head *head, const char *name) 522933f82ffSSukadev Bhattiprolu { 523933f82ffSSukadev Bhattiprolu int i; 524933f82ffSSukadev Bhattiprolu struct pmu_events_map *map; 525933f82ffSSukadev Bhattiprolu struct pmu_event *pe; 526933f82ffSSukadev Bhattiprolu char *cpuid; 527fb967063SAndi Kleen static bool printed; 528933f82ffSSukadev Bhattiprolu 529fc06e2a5SAndi Kleen cpuid = getenv("PERF_CPUID"); 530fc06e2a5SAndi Kleen if (cpuid) 531fc06e2a5SAndi Kleen cpuid = strdup(cpuid); 532fc06e2a5SAndi Kleen if (!cpuid) 533933f82ffSSukadev Bhattiprolu cpuid = get_cpuid_str(); 534933f82ffSSukadev Bhattiprolu if (!cpuid) 535933f82ffSSukadev Bhattiprolu return; 536933f82ffSSukadev Bhattiprolu 537fb967063SAndi Kleen if (!printed) { 538fc06e2a5SAndi Kleen pr_debug("Using CPUID %s\n", cpuid); 539fb967063SAndi Kleen printed = true; 540fb967063SAndi Kleen } 541fc06e2a5SAndi Kleen 542933f82ffSSukadev Bhattiprolu i = 0; 543933f82ffSSukadev Bhattiprolu while (1) { 544933f82ffSSukadev Bhattiprolu map = &pmu_events_map[i++]; 545933f82ffSSukadev Bhattiprolu if (!map->table) 546933f82ffSSukadev Bhattiprolu goto out; 547933f82ffSSukadev Bhattiprolu 548933f82ffSSukadev Bhattiprolu if (!strcmp(map->cpuid, cpuid)) 549933f82ffSSukadev Bhattiprolu break; 550933f82ffSSukadev Bhattiprolu } 551933f82ffSSukadev Bhattiprolu 552933f82ffSSukadev Bhattiprolu /* 553933f82ffSSukadev Bhattiprolu * Found a matching PMU events table. Create aliases 554933f82ffSSukadev Bhattiprolu */ 555933f82ffSSukadev Bhattiprolu i = 0; 556933f82ffSSukadev Bhattiprolu while (1) { 557fedb2b51SAndi Kleen const char *pname; 558fedb2b51SAndi Kleen 559933f82ffSSukadev Bhattiprolu pe = &map->table[i++]; 560933f82ffSSukadev Bhattiprolu if (!pe->name) 561933f82ffSSukadev Bhattiprolu break; 562933f82ffSSukadev Bhattiprolu 563fedb2b51SAndi Kleen pname = pe->pmu ? pe->pmu : "cpu"; 564fedb2b51SAndi Kleen if (strncmp(pname, name, strlen(pname))) 565fedb2b51SAndi Kleen continue; 566fedb2b51SAndi Kleen 567933f82ffSSukadev Bhattiprolu /* need type casts to override 'const' */ 568933f82ffSSukadev Bhattiprolu __perf_pmu__new_alias(head, NULL, (char *)pe->name, 569c8d6828aSSukadev Bhattiprolu (char *)pe->desc, (char *)pe->event, 570fedb2b51SAndi Kleen (char *)pe->long_desc, (char *)pe->topic, 57100636c3bSAndi Kleen (char *)pe->unit, (char *)pe->perpkg, 57296284814SAndi Kleen (char *)pe->metric_expr, 57396284814SAndi Kleen (char *)pe->metric_name); 574933f82ffSSukadev Bhattiprolu } 575933f82ffSSukadev Bhattiprolu 576933f82ffSSukadev Bhattiprolu out: 577933f82ffSSukadev Bhattiprolu free(cpuid); 578933f82ffSSukadev Bhattiprolu } 579933f82ffSSukadev Bhattiprolu 580c5de47f2SSukadev Bhattiprolu struct perf_event_attr * __weak 581dc0a6202SAdrian Hunter perf_pmu__get_default_config(struct perf_pmu *pmu __maybe_unused) 582dc0a6202SAdrian Hunter { 583dc0a6202SAdrian Hunter return NULL; 584dc0a6202SAdrian Hunter } 585dc0a6202SAdrian Hunter 586b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_lookup(const char *name) 587cd82a32eSJiri Olsa { 588cd82a32eSJiri Olsa struct perf_pmu *pmu; 589cd82a32eSJiri Olsa LIST_HEAD(format); 590a6146d50SZheng Yan LIST_HEAD(aliases); 591cd82a32eSJiri Olsa __u32 type; 592cd82a32eSJiri Olsa 593cd82a32eSJiri Olsa /* 594cd82a32eSJiri Olsa * The pmu data we store & need consists of the pmu 595cd82a32eSJiri Olsa * type value and format definitions. Load both right 596cd82a32eSJiri Olsa * now. 597cd82a32eSJiri Olsa */ 598cd82a32eSJiri Olsa if (pmu_format(name, &format)) 599cd82a32eSJiri Olsa return NULL; 600cd82a32eSJiri Olsa 60115b22ed3SAndi Kleen /* 60215b22ed3SAndi Kleen * Check the type first to avoid unnecessary work. 60315b22ed3SAndi Kleen */ 60415b22ed3SAndi Kleen if (pmu_type(name, &type)) 60515b22ed3SAndi Kleen return NULL; 60615b22ed3SAndi Kleen 6073fded963SJiri Olsa if (pmu_aliases(name, &aliases)) 6083fded963SJiri Olsa return NULL; 6093fded963SJiri Olsa 610fedb2b51SAndi Kleen pmu_add_cpu_aliases(&aliases, name); 611cd82a32eSJiri Olsa pmu = zalloc(sizeof(*pmu)); 612cd82a32eSJiri Olsa if (!pmu) 613cd82a32eSJiri Olsa return NULL; 614cd82a32eSJiri Olsa 6157ae92e74SYan, Zheng pmu->cpus = pmu_cpumask(name); 6167ae92e74SYan, Zheng 617cd82a32eSJiri Olsa INIT_LIST_HEAD(&pmu->format); 618a6146d50SZheng Yan INIT_LIST_HEAD(&pmu->aliases); 619cd82a32eSJiri Olsa list_splice(&format, &pmu->format); 620a6146d50SZheng Yan list_splice(&aliases, &pmu->aliases); 621cd82a32eSJiri Olsa pmu->name = strdup(name); 622cd82a32eSJiri Olsa pmu->type = type; 6239bc8f9feSRobert Richter list_add_tail(&pmu->list, &pmus); 624dc0a6202SAdrian Hunter 625dc0a6202SAdrian Hunter pmu->default_config = perf_pmu__get_default_config(pmu); 626dc0a6202SAdrian Hunter 627cd82a32eSJiri Olsa return pmu; 628cd82a32eSJiri Olsa } 629cd82a32eSJiri Olsa 630b6b96fb4SAdrian Hunter static struct perf_pmu *pmu_find(const char *name) 631cd82a32eSJiri Olsa { 632cd82a32eSJiri Olsa struct perf_pmu *pmu; 633cd82a32eSJiri Olsa 634cd82a32eSJiri Olsa list_for_each_entry(pmu, &pmus, list) 635cd82a32eSJiri Olsa if (!strcmp(pmu->name, name)) 636cd82a32eSJiri Olsa return pmu; 637cd82a32eSJiri Olsa 638cd82a32eSJiri Olsa return NULL; 639cd82a32eSJiri Olsa } 640cd82a32eSJiri Olsa 64150a9667cSRobert Richter struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu) 64250a9667cSRobert Richter { 64350a9667cSRobert Richter /* 64450a9667cSRobert Richter * pmu iterator: If pmu is NULL, we start at the begin, 64550a9667cSRobert Richter * otherwise return the next pmu. Returns NULL on end. 64650a9667cSRobert Richter */ 64750a9667cSRobert Richter if (!pmu) { 64850a9667cSRobert Richter pmu_read_sysfs(); 64950a9667cSRobert Richter pmu = list_prepare_entry(pmu, &pmus, list); 65050a9667cSRobert Richter } 65150a9667cSRobert Richter list_for_each_entry_continue(pmu, &pmus, list) 65250a9667cSRobert Richter return pmu; 65350a9667cSRobert Richter return NULL; 65450a9667cSRobert Richter } 65550a9667cSRobert Richter 656b6b96fb4SAdrian Hunter struct perf_pmu *perf_pmu__find(const char *name) 657cd82a32eSJiri Olsa { 658cd82a32eSJiri Olsa struct perf_pmu *pmu; 659cd82a32eSJiri Olsa 660cd82a32eSJiri Olsa /* 661cd82a32eSJiri Olsa * Once PMU is loaded it stays in the list, 662cd82a32eSJiri Olsa * so we keep us from multiple reading/parsing 663cd82a32eSJiri Olsa * the pmu format definitions. 664cd82a32eSJiri Olsa */ 665cd82a32eSJiri Olsa pmu = pmu_find(name); 666cd82a32eSJiri Olsa if (pmu) 667cd82a32eSJiri Olsa return pmu; 668cd82a32eSJiri Olsa 669cd82a32eSJiri Olsa return pmu_lookup(name); 670cd82a32eSJiri Olsa } 671cd82a32eSJiri Olsa 6725c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_format * 67309ff6071SAdrian Hunter pmu_find_format(struct list_head *formats, const char *name) 674cd82a32eSJiri Olsa { 6755c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 676cd82a32eSJiri Olsa 677cd82a32eSJiri Olsa list_for_each_entry(format, formats, list) 678cd82a32eSJiri Olsa if (!strcmp(format->name, name)) 679cd82a32eSJiri Olsa return format; 680cd82a32eSJiri Olsa 681cd82a32eSJiri Olsa return NULL; 682cd82a32eSJiri Olsa } 683cd82a32eSJiri Olsa 68409ff6071SAdrian Hunter __u64 perf_pmu__format_bits(struct list_head *formats, const char *name) 68509ff6071SAdrian Hunter { 68609ff6071SAdrian Hunter struct perf_pmu_format *format = pmu_find_format(formats, name); 68709ff6071SAdrian Hunter __u64 bits = 0; 68809ff6071SAdrian Hunter int fbit; 68909ff6071SAdrian Hunter 69009ff6071SAdrian Hunter if (!format) 69109ff6071SAdrian Hunter return 0; 69209ff6071SAdrian Hunter 69309ff6071SAdrian Hunter for_each_set_bit(fbit, format->bits, PERF_PMU_FORMAT_BITS) 69409ff6071SAdrian Hunter bits |= 1ULL << fbit; 69509ff6071SAdrian Hunter 69609ff6071SAdrian Hunter return bits; 69709ff6071SAdrian Hunter } 69809ff6071SAdrian Hunter 699cd82a32eSJiri Olsa /* 700dc0a6202SAdrian Hunter * Sets value based on the format definition (format parameter) 701cd82a32eSJiri Olsa * and unformated value (value parameter). 702cd82a32eSJiri Olsa */ 703dc0a6202SAdrian Hunter static void pmu_format_value(unsigned long *format, __u64 value, __u64 *v, 704dc0a6202SAdrian Hunter bool zero) 705cd82a32eSJiri Olsa { 706cd82a32eSJiri Olsa unsigned long fbit, vbit; 707cd82a32eSJiri Olsa 708cd82a32eSJiri Olsa for (fbit = 0, vbit = 0; fbit < PERF_PMU_FORMAT_BITS; fbit++) { 709cd82a32eSJiri Olsa 710cd82a32eSJiri Olsa if (!test_bit(fbit, format)) 711cd82a32eSJiri Olsa continue; 712cd82a32eSJiri Olsa 713dc0a6202SAdrian Hunter if (value & (1llu << vbit++)) 714dc0a6202SAdrian Hunter *v |= (1llu << fbit); 715dc0a6202SAdrian Hunter else if (zero) 716dc0a6202SAdrian Hunter *v &= ~(1llu << fbit); 717cd82a32eSJiri Olsa } 718cd82a32eSJiri Olsa } 719cd82a32eSJiri Olsa 7200efe6b67SAdrian Hunter static __u64 pmu_format_max_value(const unsigned long *format) 7210efe6b67SAdrian Hunter { 722ac0e2cd5SKan Liang __u64 w = 0; 723ac0e2cd5SKan Liang int fbit; 7240efe6b67SAdrian Hunter 725ac0e2cd5SKan Liang for_each_set_bit(fbit, format, PERF_PMU_FORMAT_BITS) 726ac0e2cd5SKan Liang w |= (1ULL << fbit); 727ac0e2cd5SKan Liang 728ac0e2cd5SKan Liang return w; 7290efe6b67SAdrian Hunter } 7300efe6b67SAdrian Hunter 731cd82a32eSJiri Olsa /* 732688d4dfcSCody P Schafer * Term is a string term, and might be a param-term. Try to look up it's value 733688d4dfcSCody P Schafer * in the remaining terms. 734688d4dfcSCody P Schafer * - We have a term like "base-or-format-term=param-term", 735688d4dfcSCody P Schafer * - We need to find the value supplied for "param-term" (with param-term named 736688d4dfcSCody P Schafer * in a config string) later on in the term list. 737688d4dfcSCody P Schafer */ 738688d4dfcSCody P Schafer static int pmu_resolve_param_term(struct parse_events_term *term, 739688d4dfcSCody P Schafer struct list_head *head_terms, 740688d4dfcSCody P Schafer __u64 *value) 741688d4dfcSCody P Schafer { 742688d4dfcSCody P Schafer struct parse_events_term *t; 743688d4dfcSCody P Schafer 744688d4dfcSCody P Schafer list_for_each_entry(t, head_terms, list) { 745688d4dfcSCody P Schafer if (t->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 746688d4dfcSCody P Schafer if (!strcmp(t->config, term->config)) { 747688d4dfcSCody P Schafer t->used = true; 748688d4dfcSCody P Schafer *value = t->val.num; 749688d4dfcSCody P Schafer return 0; 750688d4dfcSCody P Schafer } 751688d4dfcSCody P Schafer } 752688d4dfcSCody P Schafer } 753688d4dfcSCody P Schafer 754bb963e16SNamhyung Kim if (verbose > 0) 755688d4dfcSCody P Schafer printf("Required parameter '%s' not specified\n", term->config); 756688d4dfcSCody P Schafer 757688d4dfcSCody P Schafer return -1; 758688d4dfcSCody P Schafer } 759688d4dfcSCody P Schafer 760ffeb883eSHe Kuang static char *pmu_formats_string(struct list_head *formats) 761e64b020bSJiri Olsa { 762e64b020bSJiri Olsa struct perf_pmu_format *format; 76311db4e29SMasami Hiramatsu char *str = NULL; 76411db4e29SMasami Hiramatsu struct strbuf buf = STRBUF_INIT; 765e64b020bSJiri Olsa unsigned i = 0; 766e64b020bSJiri Olsa 767ffeb883eSHe Kuang if (!formats) 768e64b020bSJiri Olsa return NULL; 769e64b020bSJiri Olsa 770e64b020bSJiri Olsa /* sysfs exported terms */ 771ffeb883eSHe Kuang list_for_each_entry(format, formats, list) 77211db4e29SMasami Hiramatsu if (strbuf_addf(&buf, i++ ? ",%s" : "%s", format->name) < 0) 77311db4e29SMasami Hiramatsu goto error; 774e64b020bSJiri Olsa 775ffeb883eSHe Kuang str = strbuf_detach(&buf, NULL); 77611db4e29SMasami Hiramatsu error: 777ffeb883eSHe Kuang strbuf_release(&buf); 778e64b020bSJiri Olsa 779e64b020bSJiri Olsa return str; 780e64b020bSJiri Olsa } 781e64b020bSJiri Olsa 782688d4dfcSCody P Schafer /* 783cd82a32eSJiri Olsa * Setup one of config[12] attr members based on the 78488aca8d9SCody P Schafer * user input data - term parameter. 785cd82a32eSJiri Olsa */ 786cd82a32eSJiri Olsa static int pmu_config_term(struct list_head *formats, 787cd82a32eSJiri Olsa struct perf_event_attr *attr, 788dc0a6202SAdrian Hunter struct parse_events_term *term, 789688d4dfcSCody P Schafer struct list_head *head_terms, 790e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 791cd82a32eSJiri Olsa { 7925c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 793cd82a32eSJiri Olsa __u64 *vp; 7940efe6b67SAdrian Hunter __u64 val, max_val; 795cd82a32eSJiri Olsa 796cd82a32eSJiri Olsa /* 797688d4dfcSCody P Schafer * If this is a parameter we've already used for parameterized-eval, 798688d4dfcSCody P Schafer * skip it in normal eval. 799688d4dfcSCody P Schafer */ 800688d4dfcSCody P Schafer if (term->used) 801688d4dfcSCody P Schafer return 0; 802688d4dfcSCody P Schafer 803688d4dfcSCody P Schafer /* 804cd82a32eSJiri Olsa * Hardcoded terms should be already in, so nothing 805cd82a32eSJiri Olsa * to be done for them. 806cd82a32eSJiri Olsa */ 807cd82a32eSJiri Olsa if (parse_events__is_hardcoded_term(term)) 808cd82a32eSJiri Olsa return 0; 809cd82a32eSJiri Olsa 810cd82a32eSJiri Olsa format = pmu_find_format(formats, term->config); 811688d4dfcSCody P Schafer if (!format) { 812bb963e16SNamhyung Kim if (verbose > 0) 813688d4dfcSCody P Schafer printf("Invalid event/parameter '%s'\n", term->config); 814e64b020bSJiri Olsa if (err) { 815ffeb883eSHe Kuang char *pmu_term = pmu_formats_string(formats); 816ffeb883eSHe Kuang 817e64b020bSJiri Olsa err->idx = term->err_term; 818e64b020bSJiri Olsa err->str = strdup("unknown term"); 819ffeb883eSHe Kuang err->help = parse_events_formats_error_string(pmu_term); 820ffeb883eSHe Kuang free(pmu_term); 821e64b020bSJiri Olsa } 822cd82a32eSJiri Olsa return -EINVAL; 823688d4dfcSCody P Schafer } 824cd82a32eSJiri Olsa 825cd82a32eSJiri Olsa switch (format->value) { 826cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG: 827cd82a32eSJiri Olsa vp = &attr->config; 828cd82a32eSJiri Olsa break; 829cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG1: 830cd82a32eSJiri Olsa vp = &attr->config1; 831cd82a32eSJiri Olsa break; 832cd82a32eSJiri Olsa case PERF_PMU_FORMAT_VALUE_CONFIG2: 833cd82a32eSJiri Olsa vp = &attr->config2; 834cd82a32eSJiri Olsa break; 835cd82a32eSJiri Olsa default: 836cd82a32eSJiri Olsa return -EINVAL; 837cd82a32eSJiri Olsa } 838cd82a32eSJiri Olsa 83916fa7e82SJiri Olsa /* 840688d4dfcSCody P Schafer * Either directly use a numeric term, or try to translate string terms 841688d4dfcSCody P Schafer * using event parameters. 84216fa7e82SJiri Olsa */ 84399e7138eSJiri Olsa if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 84499e7138eSJiri Olsa if (term->no_value && 84599e7138eSJiri Olsa bitmap_weight(format->bits, PERF_PMU_FORMAT_BITS) > 1) { 84699e7138eSJiri Olsa if (err) { 84799e7138eSJiri Olsa err->idx = term->err_val; 84899e7138eSJiri Olsa err->str = strdup("no value assigned for term"); 84999e7138eSJiri Olsa } 85099e7138eSJiri Olsa return -EINVAL; 85199e7138eSJiri Olsa } 85299e7138eSJiri Olsa 853688d4dfcSCody P Schafer val = term->val.num; 85499e7138eSJiri Olsa } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 855688d4dfcSCody P Schafer if (strcmp(term->val.str, "?")) { 856bb963e16SNamhyung Kim if (verbose > 0) { 857688d4dfcSCody P Schafer pr_info("Invalid sysfs entry %s=%s\n", 858688d4dfcSCody P Schafer term->config, term->val.str); 859e64b020bSJiri Olsa } 860e64b020bSJiri Olsa if (err) { 861e64b020bSJiri Olsa err->idx = term->err_val; 862e64b020bSJiri Olsa err->str = strdup("expected numeric value"); 863e64b020bSJiri Olsa } 864688d4dfcSCody P Schafer return -EINVAL; 865688d4dfcSCody P Schafer } 866688d4dfcSCody P Schafer 867688d4dfcSCody P Schafer if (pmu_resolve_param_term(term, head_terms, &val)) 868688d4dfcSCody P Schafer return -EINVAL; 869688d4dfcSCody P Schafer } else 870688d4dfcSCody P Schafer return -EINVAL; 871688d4dfcSCody P Schafer 8720efe6b67SAdrian Hunter max_val = pmu_format_max_value(format->bits); 8730efe6b67SAdrian Hunter if (val > max_val) { 8740efe6b67SAdrian Hunter if (err) { 8750efe6b67SAdrian Hunter err->idx = term->err_val; 8760efe6b67SAdrian Hunter if (asprintf(&err->str, 8770efe6b67SAdrian Hunter "value too big for format, maximum is %llu", 8780efe6b67SAdrian Hunter (unsigned long long)max_val) < 0) 8790efe6b67SAdrian Hunter err->str = strdup("value too big for format"); 8800efe6b67SAdrian Hunter return -EINVAL; 8810efe6b67SAdrian Hunter } 8820efe6b67SAdrian Hunter /* 8830efe6b67SAdrian Hunter * Assume we don't care if !err, in which case the value will be 8840efe6b67SAdrian Hunter * silently truncated. 8850efe6b67SAdrian Hunter */ 8860efe6b67SAdrian Hunter } 8870efe6b67SAdrian Hunter 888688d4dfcSCody P Schafer pmu_format_value(format->bits, val, vp, zero); 889cd82a32eSJiri Olsa return 0; 890cd82a32eSJiri Olsa } 891cd82a32eSJiri Olsa 892cff7f956SJiri Olsa int perf_pmu__config_terms(struct list_head *formats, 893cff7f956SJiri Olsa struct perf_event_attr *attr, 894dc0a6202SAdrian Hunter struct list_head *head_terms, 895e64b020bSJiri Olsa bool zero, struct parse_events_error *err) 896cd82a32eSJiri Olsa { 8976cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term; 898cd82a32eSJiri Olsa 899688d4dfcSCody P Schafer list_for_each_entry(term, head_terms, list) { 900e64b020bSJiri Olsa if (pmu_config_term(formats, attr, term, head_terms, 901e64b020bSJiri Olsa zero, err)) 902cd82a32eSJiri Olsa return -EINVAL; 903688d4dfcSCody P Schafer } 904cd82a32eSJiri Olsa 905cd82a32eSJiri Olsa return 0; 906cd82a32eSJiri Olsa } 907cd82a32eSJiri Olsa 908cd82a32eSJiri Olsa /* 909cd82a32eSJiri Olsa * Configures event's 'attr' parameter based on the: 910cd82a32eSJiri Olsa * 1) users input - specified in terms parameter 911cd82a32eSJiri Olsa * 2) pmu format definitions - specified by pmu parameter 912cd82a32eSJiri Olsa */ 913cd82a32eSJiri Olsa int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr, 914e64b020bSJiri Olsa struct list_head *head_terms, 915e64b020bSJiri Olsa struct parse_events_error *err) 916cd82a32eSJiri Olsa { 917dc0a6202SAdrian Hunter bool zero = !!pmu->default_config; 918dc0a6202SAdrian Hunter 919cd82a32eSJiri Olsa attr->type = pmu->type; 920e64b020bSJiri Olsa return perf_pmu__config_terms(&pmu->format, attr, head_terms, 921e64b020bSJiri Olsa zero, err); 922cd82a32eSJiri Olsa } 923cd82a32eSJiri Olsa 9245c6ccc37SArnaldo Carvalho de Melo static struct perf_pmu_alias *pmu_find_alias(struct perf_pmu *pmu, 9256cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term) 926a6146d50SZheng Yan { 9275c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 928a6146d50SZheng Yan char *name; 929a6146d50SZheng Yan 930a6146d50SZheng Yan if (parse_events__is_hardcoded_term(term)) 931a6146d50SZheng Yan return NULL; 932a6146d50SZheng Yan 933a6146d50SZheng Yan if (term->type_val == PARSE_EVENTS__TERM_TYPE_NUM) { 934a6146d50SZheng Yan if (term->val.num != 1) 935a6146d50SZheng Yan return NULL; 936a6146d50SZheng Yan if (pmu_find_format(&pmu->format, term->config)) 937a6146d50SZheng Yan return NULL; 938a6146d50SZheng Yan name = term->config; 939a6146d50SZheng Yan } else if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) { 940a6146d50SZheng Yan if (strcasecmp(term->config, "event")) 941a6146d50SZheng Yan return NULL; 942a6146d50SZheng Yan name = term->val.str; 943a6146d50SZheng Yan } else { 944a6146d50SZheng Yan return NULL; 945a6146d50SZheng Yan } 946a6146d50SZheng Yan 947a6146d50SZheng Yan list_for_each_entry(alias, &pmu->aliases, list) { 948a6146d50SZheng Yan if (!strcasecmp(alias->name, name)) 949a6146d50SZheng Yan return alias; 950a6146d50SZheng Yan } 951a6146d50SZheng Yan return NULL; 952a6146d50SZheng Yan } 953a6146d50SZheng Yan 954410136f5SStephane Eranian 9551d9e446bSJiri Olsa static int check_info_data(struct perf_pmu_alias *alias, 9561d9e446bSJiri Olsa struct perf_pmu_info *info) 957410136f5SStephane Eranian { 958410136f5SStephane Eranian /* 959410136f5SStephane Eranian * Only one term in event definition can 9601d9e446bSJiri Olsa * define unit, scale and snapshot, fail 9611d9e446bSJiri Olsa * if there's more than one. 962410136f5SStephane Eranian */ 963b30a7d1fSArnaldo Carvalho de Melo if ((info->unit && alias->unit[0]) || 9641d9e446bSJiri Olsa (info->scale && alias->scale) || 9651d9e446bSJiri Olsa (info->snapshot && alias->snapshot)) 966410136f5SStephane Eranian return -EINVAL; 967410136f5SStephane Eranian 968b30a7d1fSArnaldo Carvalho de Melo if (alias->unit[0]) 9691d9e446bSJiri Olsa info->unit = alias->unit; 970410136f5SStephane Eranian 971410136f5SStephane Eranian if (alias->scale) 9721d9e446bSJiri Olsa info->scale = alias->scale; 9731d9e446bSJiri Olsa 9741d9e446bSJiri Olsa if (alias->snapshot) 9751d9e446bSJiri Olsa info->snapshot = alias->snapshot; 976410136f5SStephane Eranian 977410136f5SStephane Eranian return 0; 978410136f5SStephane Eranian } 979410136f5SStephane Eranian 980a6146d50SZheng Yan /* 981a6146d50SZheng Yan * Find alias in the terms list and replace it with the terms 982a6146d50SZheng Yan * defined for the alias 983a6146d50SZheng Yan */ 984410136f5SStephane Eranian int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms, 98546441bdcSMatt Fleming struct perf_pmu_info *info) 986a6146d50SZheng Yan { 9876cee6cd3SArnaldo Carvalho de Melo struct parse_events_term *term, *h; 9885c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_alias *alias; 989a6146d50SZheng Yan int ret; 990a6146d50SZheng Yan 991044330c1SMatt Fleming info->per_pkg = false; 992044330c1SMatt Fleming 9938a398897SStephane Eranian /* 9948a398897SStephane Eranian * Mark unit and scale as not set 9958a398897SStephane Eranian * (different from default values, see below) 9968a398897SStephane Eranian */ 99746441bdcSMatt Fleming info->unit = NULL; 99846441bdcSMatt Fleming info->scale = 0.0; 9991d9e446bSJiri Olsa info->snapshot = false; 100037932c18SAndi Kleen info->metric_expr = NULL; 100196284814SAndi Kleen info->metric_name = NULL; 1002410136f5SStephane Eranian 1003a6146d50SZheng Yan list_for_each_entry_safe(term, h, head_terms, list) { 1004a6146d50SZheng Yan alias = pmu_find_alias(pmu, term); 1005a6146d50SZheng Yan if (!alias) 1006a6146d50SZheng Yan continue; 1007a6146d50SZheng Yan ret = pmu_alias_terms(alias, &term->list); 1008a6146d50SZheng Yan if (ret) 1009a6146d50SZheng Yan return ret; 1010410136f5SStephane Eranian 10111d9e446bSJiri Olsa ret = check_info_data(alias, info); 1012410136f5SStephane Eranian if (ret) 1013410136f5SStephane Eranian return ret; 1014410136f5SStephane Eranian 1015044330c1SMatt Fleming if (alias->per_pkg) 1016044330c1SMatt Fleming info->per_pkg = true; 101737932c18SAndi Kleen info->metric_expr = alias->metric_expr; 101896284814SAndi Kleen info->metric_name = alias->metric_name; 1019044330c1SMatt Fleming 1020a6146d50SZheng Yan list_del(&term->list); 1021a6146d50SZheng Yan free(term); 1022a6146d50SZheng Yan } 10238a398897SStephane Eranian 10248a398897SStephane Eranian /* 10258a398897SStephane Eranian * if no unit or scale foundin aliases, then 10268a398897SStephane Eranian * set defaults as for evsel 10278a398897SStephane Eranian * unit cannot left to NULL 10288a398897SStephane Eranian */ 102946441bdcSMatt Fleming if (info->unit == NULL) 103046441bdcSMatt Fleming info->unit = ""; 10318a398897SStephane Eranian 103246441bdcSMatt Fleming if (info->scale == 0.0) 103346441bdcSMatt Fleming info->scale = 1.0; 10348a398897SStephane Eranian 1035a6146d50SZheng Yan return 0; 1036a6146d50SZheng Yan } 1037a6146d50SZheng Yan 1038cd82a32eSJiri Olsa int perf_pmu__new_format(struct list_head *list, char *name, 1039cd82a32eSJiri Olsa int config, unsigned long *bits) 1040cd82a32eSJiri Olsa { 10415c6ccc37SArnaldo Carvalho de Melo struct perf_pmu_format *format; 1042cd82a32eSJiri Olsa 1043cd82a32eSJiri Olsa format = zalloc(sizeof(*format)); 1044cd82a32eSJiri Olsa if (!format) 1045cd82a32eSJiri Olsa return -ENOMEM; 1046cd82a32eSJiri Olsa 1047cd82a32eSJiri Olsa format->name = strdup(name); 1048cd82a32eSJiri Olsa format->value = config; 1049cd82a32eSJiri Olsa memcpy(format->bits, bits, sizeof(format->bits)); 1050cd82a32eSJiri Olsa 1051cd82a32eSJiri Olsa list_add_tail(&format->list, list); 1052cd82a32eSJiri Olsa return 0; 1053cd82a32eSJiri Olsa } 1054cd82a32eSJiri Olsa 1055cd82a32eSJiri Olsa void perf_pmu__set_format(unsigned long *bits, long from, long to) 1056cd82a32eSJiri Olsa { 1057cd82a32eSJiri Olsa long b; 1058cd82a32eSJiri Olsa 1059cd82a32eSJiri Olsa if (!to) 1060cd82a32eSJiri Olsa to = from; 1061cd82a32eSJiri Olsa 106215268138SSukadev Bhattiprolu memset(bits, 0, BITS_TO_BYTES(PERF_PMU_FORMAT_BITS)); 1063cd82a32eSJiri Olsa for (b = from; b <= to; b++) 1064cd82a32eSJiri Olsa set_bit(b, bits); 1065cd82a32eSJiri Olsa } 1066dc098b35SAndi Kleen 1067aaea3617SCody P Schafer static int sub_non_neg(int a, int b) 1068aaea3617SCody P Schafer { 1069aaea3617SCody P Schafer if (b > a) 1070aaea3617SCody P Schafer return 0; 1071aaea3617SCody P Schafer return a - b; 1072aaea3617SCody P Schafer } 1073aaea3617SCody P Schafer 1074dc098b35SAndi Kleen static char *format_alias(char *buf, int len, struct perf_pmu *pmu, 1075dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1076dc098b35SAndi Kleen { 1077aaea3617SCody P Schafer struct parse_events_term *term; 1078aaea3617SCody P Schafer int used = snprintf(buf, len, "%s/%s", pmu->name, alias->name); 1079aaea3617SCody P Schafer 1080aaea3617SCody P Schafer list_for_each_entry(term, &alias->terms, list) { 1081aaea3617SCody P Schafer if (term->type_val == PARSE_EVENTS__TERM_TYPE_STR) 1082aaea3617SCody P Schafer used += snprintf(buf + used, sub_non_neg(len, used), 1083aaea3617SCody P Schafer ",%s=%s", term->config, 1084aaea3617SCody P Schafer term->val.str); 1085aaea3617SCody P Schafer } 1086aaea3617SCody P Schafer 1087aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1088aaea3617SCody P Schafer buf[used] = '/'; 1089aaea3617SCody P Schafer used++; 1090aaea3617SCody P Schafer } 1091aaea3617SCody P Schafer if (sub_non_neg(len, used) > 0) { 1092aaea3617SCody P Schafer buf[used] = '\0'; 1093aaea3617SCody P Schafer used++; 1094aaea3617SCody P Schafer } else 1095aaea3617SCody P Schafer buf[len - 1] = '\0'; 1096aaea3617SCody P Schafer 1097dc098b35SAndi Kleen return buf; 1098dc098b35SAndi Kleen } 1099dc098b35SAndi Kleen 1100dc098b35SAndi Kleen static char *format_alias_or(char *buf, int len, struct perf_pmu *pmu, 1101dc098b35SAndi Kleen struct perf_pmu_alias *alias) 1102dc098b35SAndi Kleen { 1103dc098b35SAndi Kleen snprintf(buf, len, "%s OR %s/%s/", alias->name, pmu->name, alias->name); 1104dc098b35SAndi Kleen return buf; 1105dc098b35SAndi Kleen } 1106dc098b35SAndi Kleen 1107dd5f1036SAndi Kleen struct sevent { 110808e60ed1SAndi Kleen char *name; 110908e60ed1SAndi Kleen char *desc; 1110dd5f1036SAndi Kleen char *topic; 1111f2361024SAndi Kleen char *str; 1112f2361024SAndi Kleen char *pmu; 11137f372a63SAndi Kleen char *metric_expr; 111496284814SAndi Kleen char *metric_name; 111508e60ed1SAndi Kleen }; 111608e60ed1SAndi Kleen 1117dd5f1036SAndi Kleen static int cmp_sevent(const void *a, const void *b) 1118dc098b35SAndi Kleen { 1119dd5f1036SAndi Kleen const struct sevent *as = a; 1120dd5f1036SAndi Kleen const struct sevent *bs = b; 112108e60ed1SAndi Kleen 112208e60ed1SAndi Kleen /* Put extra events last */ 112308e60ed1SAndi Kleen if (!!as->desc != !!bs->desc) 112408e60ed1SAndi Kleen return !!as->desc - !!bs->desc; 1125dd5f1036SAndi Kleen if (as->topic && bs->topic) { 1126dd5f1036SAndi Kleen int n = strcmp(as->topic, bs->topic); 1127dd5f1036SAndi Kleen 1128dd5f1036SAndi Kleen if (n) 1129dd5f1036SAndi Kleen return n; 1130dd5f1036SAndi Kleen } 113108e60ed1SAndi Kleen return strcmp(as->name, bs->name); 113208e60ed1SAndi Kleen } 113308e60ed1SAndi Kleen 113408e60ed1SAndi Kleen static void wordwrap(char *s, int start, int max, int corr) 113508e60ed1SAndi Kleen { 113608e60ed1SAndi Kleen int column = start; 113708e60ed1SAndi Kleen int n; 113808e60ed1SAndi Kleen 113908e60ed1SAndi Kleen while (*s) { 114008e60ed1SAndi Kleen int wlen = strcspn(s, " \t"); 114108e60ed1SAndi Kleen 114208e60ed1SAndi Kleen if (column + wlen >= max && column > start) { 114308e60ed1SAndi Kleen printf("\n%*s", start, ""); 114408e60ed1SAndi Kleen column = start + corr; 114508e60ed1SAndi Kleen } 114608e60ed1SAndi Kleen n = printf("%s%.*s", column > start ? " " : "", wlen, s); 114708e60ed1SAndi Kleen if (n <= 0) 114808e60ed1SAndi Kleen break; 114908e60ed1SAndi Kleen s += wlen; 115008e60ed1SAndi Kleen column += n; 115108e60ed1SAndi Kleen while (isspace(*s)) 115208e60ed1SAndi Kleen s++; 115308e60ed1SAndi Kleen } 1154dc098b35SAndi Kleen } 1155dc098b35SAndi Kleen 1156c8d6828aSSukadev Bhattiprolu void print_pmu_events(const char *event_glob, bool name_only, bool quiet_flag, 1157c8d6828aSSukadev Bhattiprolu bool long_desc) 1158dc098b35SAndi Kleen { 1159dc098b35SAndi Kleen struct perf_pmu *pmu; 1160dc098b35SAndi Kleen struct perf_pmu_alias *alias; 1161dc098b35SAndi Kleen char buf[1024]; 1162dc098b35SAndi Kleen int printed = 0; 1163dc098b35SAndi Kleen int len, j; 1164dd5f1036SAndi Kleen struct sevent *aliases; 116508e60ed1SAndi Kleen int numdesc = 0; 116661eb2eb4SAndi Kleen int columns = pager_get_columns(); 1167dd5f1036SAndi Kleen char *topic = NULL; 1168dc098b35SAndi Kleen 1169dc098b35SAndi Kleen pmu = NULL; 1170dc098b35SAndi Kleen len = 0; 117142634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1172dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 1173dc098b35SAndi Kleen len++; 117442634bc7SAdrian Hunter if (pmu->selectable) 117542634bc7SAdrian Hunter len++; 117642634bc7SAdrian Hunter } 1177dd5f1036SAndi Kleen aliases = zalloc(sizeof(struct sevent) * len); 1178dc098b35SAndi Kleen if (!aliases) 11797e4772dcSArnaldo Carvalho de Melo goto out_enomem; 1180dc098b35SAndi Kleen pmu = NULL; 1181dc098b35SAndi Kleen j = 0; 118242634bc7SAdrian Hunter while ((pmu = perf_pmu__scan(pmu)) != NULL) { 1183dc098b35SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) { 118408e60ed1SAndi Kleen char *name = alias->desc ? alias->name : 118508e60ed1SAndi Kleen format_alias(buf, sizeof(buf), pmu, alias); 1186dc098b35SAndi Kleen bool is_cpu = !strcmp(pmu->name, "cpu"); 1187dc098b35SAndi Kleen 1188dc098b35SAndi Kleen if (event_glob != NULL && 118938d14f0cSAndi Kleen !(strglobmatch_nocase(name, event_glob) || 119038d14f0cSAndi Kleen (!is_cpu && strglobmatch_nocase(alias->name, 119167bdc35fSAndi Kleen event_glob)) || 119267bdc35fSAndi Kleen (alias->topic && 119367bdc35fSAndi Kleen strglobmatch_nocase(alias->topic, event_glob)))) 1194dc098b35SAndi Kleen continue; 11957e4772dcSArnaldo Carvalho de Melo 119608e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 11977e4772dcSArnaldo Carvalho de Melo name = format_alias_or(buf, sizeof(buf), pmu, alias); 11987e4772dcSArnaldo Carvalho de Melo 119908e60ed1SAndi Kleen aliases[j].name = name; 120008e60ed1SAndi Kleen if (is_cpu && !name_only && !alias->desc) 120108e60ed1SAndi Kleen aliases[j].name = format_alias_or(buf, 120208e60ed1SAndi Kleen sizeof(buf), 120308e60ed1SAndi Kleen pmu, alias); 120408e60ed1SAndi Kleen aliases[j].name = strdup(aliases[j].name); 120508e60ed1SAndi Kleen if (!aliases[j].name) 12067e4772dcSArnaldo Carvalho de Melo goto out_enomem; 120708e60ed1SAndi Kleen 1208c8d6828aSSukadev Bhattiprolu aliases[j].desc = long_desc ? alias->long_desc : 1209c8d6828aSSukadev Bhattiprolu alias->desc; 1210dd5f1036SAndi Kleen aliases[j].topic = alias->topic; 1211f2361024SAndi Kleen aliases[j].str = alias->str; 1212f2361024SAndi Kleen aliases[j].pmu = pmu->name; 12137f372a63SAndi Kleen aliases[j].metric_expr = alias->metric_expr; 121496284814SAndi Kleen aliases[j].metric_name = alias->metric_name; 1215dc098b35SAndi Kleen j++; 1216dc098b35SAndi Kleen } 1217fa52ceabSArnaldo Carvalho de Melo if (pmu->selectable && 1218fa52ceabSArnaldo Carvalho de Melo (event_glob == NULL || strglobmatch(pmu->name, event_glob))) { 12197e4772dcSArnaldo Carvalho de Melo char *s; 12207e4772dcSArnaldo Carvalho de Melo if (asprintf(&s, "%s//", pmu->name) < 0) 12217e4772dcSArnaldo Carvalho de Melo goto out_enomem; 122208e60ed1SAndi Kleen aliases[j].name = s; 122342634bc7SAdrian Hunter j++; 122442634bc7SAdrian Hunter } 122542634bc7SAdrian Hunter } 1226dc098b35SAndi Kleen len = j; 1227dd5f1036SAndi Kleen qsort(aliases, len, sizeof(struct sevent), cmp_sevent); 1228dc098b35SAndi Kleen for (j = 0; j < len; j++) { 122915b22ed3SAndi Kleen /* Skip duplicates */ 123015b22ed3SAndi Kleen if (j > 0 && !strcmp(aliases[j].name, aliases[j - 1].name)) 123115b22ed3SAndi Kleen continue; 1232dc098b35SAndi Kleen if (name_only) { 123308e60ed1SAndi Kleen printf("%s ", aliases[j].name); 1234dc098b35SAndi Kleen continue; 1235dc098b35SAndi Kleen } 12361c5f01feSAndi Kleen if (aliases[j].desc && !quiet_flag) { 123708e60ed1SAndi Kleen if (numdesc++ == 0) 123808e60ed1SAndi Kleen printf("\n"); 1239dd5f1036SAndi Kleen if (aliases[j].topic && (!topic || 1240dd5f1036SAndi Kleen strcmp(topic, aliases[j].topic))) { 1241dd5f1036SAndi Kleen printf("%s%s:\n", topic ? "\n" : "", 1242dd5f1036SAndi Kleen aliases[j].topic); 1243dd5f1036SAndi Kleen topic = aliases[j].topic; 1244dd5f1036SAndi Kleen } 124508e60ed1SAndi Kleen printf(" %-50s\n", aliases[j].name); 124608e60ed1SAndi Kleen printf("%*s", 8, "["); 124708e60ed1SAndi Kleen wordwrap(aliases[j].desc, 8, columns, 0); 124808e60ed1SAndi Kleen printf("]\n"); 12497f372a63SAndi Kleen if (verbose > 0) { 12507f372a63SAndi Kleen printf("%*s%s/%s/ ", 8, "", aliases[j].pmu, aliases[j].str); 125196284814SAndi Kleen if (aliases[j].metric_name) 125296284814SAndi Kleen printf(" MetricName: %s", aliases[j].metric_name); 12537f372a63SAndi Kleen if (aliases[j].metric_expr) 12547f372a63SAndi Kleen printf(" MetricExpr: %s", aliases[j].metric_expr); 12557f372a63SAndi Kleen putchar('\n'); 12567f372a63SAndi Kleen } 125708e60ed1SAndi Kleen } else 125808e60ed1SAndi Kleen printf(" %-50s [Kernel PMU event]\n", aliases[j].name); 1259dc098b35SAndi Kleen printed++; 1260dc098b35SAndi Kleen } 1261dfc431cbSArnaldo Carvalho de Melo if (printed && pager_in_use()) 1262dc098b35SAndi Kleen printf("\n"); 12637e4772dcSArnaldo Carvalho de Melo out_free: 12647e4772dcSArnaldo Carvalho de Melo for (j = 0; j < len; j++) 126508e60ed1SAndi Kleen zfree(&aliases[j].name); 12667e4772dcSArnaldo Carvalho de Melo zfree(&aliases); 12677e4772dcSArnaldo Carvalho de Melo return; 12687e4772dcSArnaldo Carvalho de Melo 12697e4772dcSArnaldo Carvalho de Melo out_enomem: 12707e4772dcSArnaldo Carvalho de Melo printf("FATAL: not enough memory to print PMU events\n"); 12717e4772dcSArnaldo Carvalho de Melo if (aliases) 12727e4772dcSArnaldo Carvalho de Melo goto out_free; 1273dc098b35SAndi Kleen } 12744cabc3d1SAndi Kleen 12754cabc3d1SAndi Kleen bool pmu_have_event(const char *pname, const char *name) 12764cabc3d1SAndi Kleen { 12774cabc3d1SAndi Kleen struct perf_pmu *pmu; 12784cabc3d1SAndi Kleen struct perf_pmu_alias *alias; 12794cabc3d1SAndi Kleen 12804cabc3d1SAndi Kleen pmu = NULL; 12814cabc3d1SAndi Kleen while ((pmu = perf_pmu__scan(pmu)) != NULL) { 12824cabc3d1SAndi Kleen if (strcmp(pname, pmu->name)) 12834cabc3d1SAndi Kleen continue; 12844cabc3d1SAndi Kleen list_for_each_entry(alias, &pmu->aliases, list) 12854cabc3d1SAndi Kleen if (!strcmp(alias->name, name)) 12864cabc3d1SAndi Kleen return true; 12874cabc3d1SAndi Kleen } 12884cabc3d1SAndi Kleen return false; 12894cabc3d1SAndi Kleen } 12907d4bdab5SAdrian Hunter 12917d4bdab5SAdrian Hunter static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name) 12927d4bdab5SAdrian Hunter { 12937d4bdab5SAdrian Hunter struct stat st; 12947d4bdab5SAdrian Hunter char path[PATH_MAX]; 12957d4bdab5SAdrian Hunter const char *sysfs; 12967d4bdab5SAdrian Hunter 12977d4bdab5SAdrian Hunter sysfs = sysfs__mountpoint(); 12987d4bdab5SAdrian Hunter if (!sysfs) 12997d4bdab5SAdrian Hunter return NULL; 13007d4bdab5SAdrian Hunter 13017d4bdab5SAdrian Hunter snprintf(path, PATH_MAX, 13027d4bdab5SAdrian Hunter "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name); 13037d4bdab5SAdrian Hunter 13047d4bdab5SAdrian Hunter if (stat(path, &st) < 0) 13057d4bdab5SAdrian Hunter return NULL; 13067d4bdab5SAdrian Hunter 13077d4bdab5SAdrian Hunter return fopen(path, "r"); 13087d4bdab5SAdrian Hunter } 13097d4bdab5SAdrian Hunter 13107d4bdab5SAdrian Hunter int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt, 13117d4bdab5SAdrian Hunter ...) 13127d4bdab5SAdrian Hunter { 13137d4bdab5SAdrian Hunter va_list args; 13147d4bdab5SAdrian Hunter FILE *file; 13157d4bdab5SAdrian Hunter int ret = EOF; 13167d4bdab5SAdrian Hunter 13177d4bdab5SAdrian Hunter va_start(args, fmt); 13187d4bdab5SAdrian Hunter file = perf_pmu__open_file(pmu, name); 13197d4bdab5SAdrian Hunter if (file) { 13207d4bdab5SAdrian Hunter ret = vfscanf(file, fmt, args); 13217d4bdab5SAdrian Hunter fclose(file); 13227d4bdab5SAdrian Hunter } 13237d4bdab5SAdrian Hunter va_end(args); 13247d4bdab5SAdrian Hunter return ret; 13257d4bdab5SAdrian Hunter } 1326