1959826caSMatt Macy /*- 2959826caSMatt Macy * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3959826caSMatt Macy * 4959826caSMatt Macy * Copyright (c) 2018, Matthew Macy 528dd6730SMitchell Horne * Copyright (c) 2021, The FreeBSD Foundation 628dd6730SMitchell Horne * 728dd6730SMitchell Horne * Portions of this software were developed by Mitchell Horne 828dd6730SMitchell Horne * under sponsorship from the FreeBSD Foundation. 9959826caSMatt Macy * 10959826caSMatt Macy * Redistribution and use in source and binary forms, with or without 11959826caSMatt Macy * modification, are permitted provided that the following conditions 12959826caSMatt Macy * are met: 13959826caSMatt Macy * 1. Redistributions of source code must retain the above copyright 14959826caSMatt Macy * notice, this list of conditions and the following disclaimer. 15959826caSMatt Macy * 2. Redistributions in binary form must reproduce the above copyright 16959826caSMatt Macy * notice, this list of conditions and the following disclaimer in the 17959826caSMatt Macy * documentation and/or other materials provided with the distribution. 18959826caSMatt Macy * 19959826caSMatt Macy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20959826caSMatt Macy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21959826caSMatt Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22959826caSMatt Macy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23959826caSMatt Macy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24959826caSMatt Macy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25959826caSMatt Macy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26959826caSMatt Macy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27959826caSMatt Macy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28959826caSMatt Macy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29959826caSMatt Macy * SUCH DAMAGE. 30959826caSMatt Macy * 31959826caSMatt Macy * $FreeBSD$ 32959826caSMatt Macy * 33959826caSMatt Macy */ 34959826caSMatt Macy 35959826caSMatt Macy #include <sys/types.h> 36959826caSMatt Macy #include <sys/errno.h> 3724e337beSRyan Moeller #include <sys/pmc.h> 38959826caSMatt Macy #include <sys/sysctl.h> 39959826caSMatt Macy #include <stddef.h> 40959826caSMatt Macy #include <stdlib.h> 41959826caSMatt Macy #include <limits.h> 42f3dbece8SEmmanuel Vadot #include <regex.h> 43959826caSMatt Macy #include <string.h> 44959826caSMatt Macy #include <pmc.h> 45959826caSMatt Macy #include <pmclog.h> 461b000b50SMatt Macy #include <assert.h> 47959826caSMatt Macy #include <libpmcstat.h> 48959826caSMatt Macy #include "pmu-events/pmu-events.h" 49959826caSMatt Macy 50959826caSMatt Macy struct pmu_alias { 51959826caSMatt Macy const char *pa_alias; 52959826caSMatt Macy const char *pa_name; 53959826caSMatt Macy }; 5481eb4dcfSMatt Macy 550024f1aaSMitchell Horne #if defined(__amd64__) || defined(__i386__) 5681eb4dcfSMatt Macy typedef enum { 5781eb4dcfSMatt Macy PMU_INVALID, 5881eb4dcfSMatt Macy PMU_INTEL, 5981eb4dcfSMatt Macy PMU_AMD, 6081eb4dcfSMatt Macy } pmu_mfr_t; 6181eb4dcfSMatt Macy 6281eb4dcfSMatt Macy static struct pmu_alias pmu_intel_alias_table[] = { 63959826caSMatt Macy {"UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 64959826caSMatt Macy {"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 65959826caSMatt Macy {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"}, 66959826caSMatt Macy {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"}, 67959826caSMatt Macy {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"}, 68959826caSMatt Macy {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"}, 69959826caSMatt Macy {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"}, 70959826caSMatt Macy {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"}, 71959826caSMatt Macy {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"}, 72959826caSMatt Macy {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"}, 73959826caSMatt Macy {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"}, 74959826caSMatt Macy {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"}, 75959826caSMatt Macy {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"}, 76959826caSMatt Macy {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"}, 7796cbd26aSMatt Macy {"cycles", "tsc-tsc"}, 7886b5e013SMatt Macy {"unhalted-cycles", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 79bc1a6a9dSMitchell Horne {"instructions", "inst_retired.any_p"}, 8096cbd26aSMatt Macy {"branch-mispredicts", "br_misp_retired.all_branches"}, 8196cbd26aSMatt Macy {"branches", "br_inst_retired.all_branches"}, 8296cbd26aSMatt Macy {"interrupts", "hw_interrupts.received"}, 8396cbd26aSMatt Macy {"ic-misses", "frontend_retired.l1i_miss"}, 84959826caSMatt Macy {NULL, NULL}, 85959826caSMatt Macy }; 86959826caSMatt Macy 8781eb4dcfSMatt Macy static struct pmu_alias pmu_amd_alias_table[] = { 8881eb4dcfSMatt Macy {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"}, 8981eb4dcfSMatt Macy {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"}, 9081eb4dcfSMatt Macy {NULL, NULL}, 9181eb4dcfSMatt Macy }; 9281eb4dcfSMatt Macy 9381eb4dcfSMatt Macy 9481eb4dcfSMatt Macy static pmu_mfr_t 9581eb4dcfSMatt Macy pmu_events_mfr(void) 9681eb4dcfSMatt Macy { 9724e337beSRyan Moeller char buf[PMC_CPUID_LEN]; 9824e337beSRyan Moeller size_t s = sizeof(buf); 9981eb4dcfSMatt Macy pmu_mfr_t mfr; 10081eb4dcfSMatt Macy 10124e337beSRyan Moeller if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 10281eb4dcfSMatt Macy (void *)NULL, 0) == -1) 10381eb4dcfSMatt Macy return (PMU_INVALID); 10453071ed1SKonstantin Belousov if (strcasestr(buf, "AuthenticAMD") != NULL || 10553071ed1SKonstantin Belousov strcasestr(buf, "HygonGenuine") != NULL) 10681eb4dcfSMatt Macy mfr = PMU_AMD; 10781eb4dcfSMatt Macy else if (strcasestr(buf, "GenuineIntel") != NULL) 10881eb4dcfSMatt Macy mfr = PMU_INTEL; 10981eb4dcfSMatt Macy else 11081eb4dcfSMatt Macy mfr = PMU_INVALID; 11181eb4dcfSMatt Macy return (mfr); 11281eb4dcfSMatt Macy } 11381eb4dcfSMatt Macy 11407d80fd8SMatt Macy /* 11507d80fd8SMatt Macy * The Intel fixed mode counters are: 11607d80fd8SMatt Macy * "inst_retired.any", 11707d80fd8SMatt Macy * "cpu_clk_unhalted.thread", 11807d80fd8SMatt Macy * "cpu_clk_unhalted.thread_any", 11907d80fd8SMatt Macy * "cpu_clk_unhalted.ref_tsc", 12007d80fd8SMatt Macy * 12107d80fd8SMatt Macy */ 122a191ed2dSMatt Macy 123959826caSMatt Macy static const char * 124959826caSMatt Macy pmu_alias_get(const char *name) 125959826caSMatt Macy { 12681eb4dcfSMatt Macy pmu_mfr_t mfr; 127959826caSMatt Macy struct pmu_alias *pa; 12881eb4dcfSMatt Macy struct pmu_alias *pmu_alias_table; 12981eb4dcfSMatt Macy 13081eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 13181eb4dcfSMatt Macy return (name); 13281eb4dcfSMatt Macy if (mfr == PMU_AMD) 13381eb4dcfSMatt Macy pmu_alias_table = pmu_amd_alias_table; 13481eb4dcfSMatt Macy else if (mfr == PMU_INTEL) 13581eb4dcfSMatt Macy pmu_alias_table = pmu_intel_alias_table; 13681eb4dcfSMatt Macy else 13781eb4dcfSMatt Macy return (name); 138959826caSMatt Macy 139959826caSMatt Macy for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++) 140959826caSMatt Macy if (strcasecmp(name, pa->pa_alias) == 0) 141959826caSMatt Macy return (pa->pa_name); 14281eb4dcfSMatt Macy 143959826caSMatt Macy return (name); 144959826caSMatt Macy } 145959826caSMatt Macy 14628dd6730SMitchell Horne #elif defined(__aarch64__) 14728dd6730SMitchell Horne 14828dd6730SMitchell Horne static struct pmu_alias pmu_armv8_alias_table[] = { 14928dd6730SMitchell Horne {NULL, NULL}, 15028dd6730SMitchell Horne }; 15128dd6730SMitchell Horne 15228dd6730SMitchell Horne static const char * 15328dd6730SMitchell Horne pmu_alias_get(const char *name) 15428dd6730SMitchell Horne { 15528dd6730SMitchell Horne struct pmu_alias *pa; 15628dd6730SMitchell Horne 15728dd6730SMitchell Horne for (pa = pmu_armv8_alias_table; pa->pa_alias != NULL; pa++) 15828dd6730SMitchell Horne if (strcasecmp(name, pa->pa_alias) == 0) 15928dd6730SMitchell Horne return (pa->pa_name); 16028dd6730SMitchell Horne 16128dd6730SMitchell Horne return (name); 16228dd6730SMitchell Horne } 16328dd6730SMitchell Horne 1640024f1aaSMitchell Horne #else 1650024f1aaSMitchell Horne 1660024f1aaSMitchell Horne static const char * 1670024f1aaSMitchell Horne pmu_alias_get(const char *name) 1680024f1aaSMitchell Horne { 1690024f1aaSMitchell Horne 1700024f1aaSMitchell Horne return (name); 1710024f1aaSMitchell Horne } 1720024f1aaSMitchell Horne #endif 1730024f1aaSMitchell Horne 174959826caSMatt Macy struct pmu_event_desc { 175959826caSMatt Macy uint64_t ped_period; 176959826caSMatt Macy uint64_t ped_offcore_rsp; 177dacc43dfSMatt Macy uint64_t ped_l3_thread; 178dacc43dfSMatt Macy uint64_t ped_l3_slice; 179959826caSMatt Macy uint32_t ped_event; 180959826caSMatt Macy uint32_t ped_frontend; 181959826caSMatt Macy uint32_t ped_ldlat; 182959826caSMatt Macy uint32_t ped_config1; 18307d80fd8SMatt Macy int16_t ped_umask; 184959826caSMatt Macy uint8_t ped_cmask; 185959826caSMatt Macy uint8_t ped_any; 186959826caSMatt Macy uint8_t ped_inv; 187959826caSMatt Macy uint8_t ped_edge; 188959826caSMatt Macy uint8_t ped_fc_mask; 189959826caSMatt Macy uint8_t ped_ch_mask; 190959826caSMatt Macy }; 191959826caSMatt Macy 192959826caSMatt Macy static const struct pmu_events_map * 193bfb46e2bSMatt Macy pmu_events_map_get(const char *cpuid) 194959826caSMatt Macy { 195a0ac5706SEmmanuel Vadot regex_t re; 196a0ac5706SEmmanuel Vadot regmatch_t pmatch[1]; 19724e337beSRyan Moeller char buf[PMC_CPUID_LEN]; 19824e337beSRyan Moeller size_t s = sizeof(buf); 199a0ac5706SEmmanuel Vadot int match; 200959826caSMatt Macy const struct pmu_events_map *pme; 201959826caSMatt Macy 202bfb46e2bSMatt Macy if (cpuid != NULL) { 20324e337beSRyan Moeller strlcpy(buf, cpuid, s); 204bfb46e2bSMatt Macy } else { 205959826caSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 206959826caSMatt Macy (void *)NULL, 0) == -1) 207959826caSMatt Macy return (NULL); 208bfb46e2bSMatt Macy } 209a0ac5706SEmmanuel Vadot for (pme = pmu_events_map; pme->cpuid != NULL; pme++) { 210a0ac5706SEmmanuel Vadot if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) { 211a0ac5706SEmmanuel Vadot printf("regex '%s' failed to compile, ignoring\n", 212a0ac5706SEmmanuel Vadot pme->cpuid); 213a0ac5706SEmmanuel Vadot continue; 214a0ac5706SEmmanuel Vadot } 215a0ac5706SEmmanuel Vadot match = regexec(&re, buf, 1, pmatch, 0); 216a0ac5706SEmmanuel Vadot regfree(&re); 217a0ac5706SEmmanuel Vadot if (match == 0) { 2181791cad0SAlexander Motin if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0 2191791cad0SAlexander Motin || buf[pmatch[0].rm_eo] == '-')) 220959826caSMatt Macy return (pme); 221a0ac5706SEmmanuel Vadot } 222a0ac5706SEmmanuel Vadot } 223959826caSMatt Macy return (NULL); 224959826caSMatt Macy } 225959826caSMatt Macy 226959826caSMatt Macy static const struct pmu_event * 227bfb46e2bSMatt Macy pmu_event_get(const char *cpuid, const char *event_name, int *idx) 228959826caSMatt Macy { 229959826caSMatt Macy const struct pmu_events_map *pme; 230959826caSMatt Macy const struct pmu_event *pe; 231959826caSMatt Macy int i; 232959826caSMatt Macy 233bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 234959826caSMatt Macy return (NULL); 235959826caSMatt Macy for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) { 236959826caSMatt Macy if (pe->name == NULL) 237959826caSMatt Macy continue; 238959826caSMatt Macy if (strcasecmp(pe->name, event_name) == 0) { 239959826caSMatt Macy if (idx) 240959826caSMatt Macy *idx = i; 241959826caSMatt Macy return (pe); 242959826caSMatt Macy } 243959826caSMatt Macy } 244959826caSMatt Macy return (NULL); 245959826caSMatt Macy } 246959826caSMatt Macy 247bfb46e2bSMatt Macy int 248bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *cpuid, const char *event) 249bfb46e2bSMatt Macy { 250bfb46e2bSMatt Macy int idx; 251bfb46e2bSMatt Macy const char *realname; 252bfb46e2bSMatt Macy 253bfb46e2bSMatt Macy realname = pmu_alias_get(event); 254bfb46e2bSMatt Macy if (pmu_event_get(cpuid, realname, &idx) == NULL) 255bfb46e2bSMatt Macy return (-1); 256bfb46e2bSMatt Macy return (idx); 257bfb46e2bSMatt Macy } 258bfb46e2bSMatt Macy 259959826caSMatt Macy const char * 260b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *cpuid, int idx) 261959826caSMatt Macy { 262959826caSMatt Macy const struct pmu_events_map *pme; 263959826caSMatt Macy 264b2ca2e50SMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 265959826caSMatt Macy return (NULL); 2661b000b50SMatt Macy assert(pme->table[idx].name); 2671b000b50SMatt Macy return (pme->table[idx].name); 268959826caSMatt Macy } 269959826caSMatt Macy 270959826caSMatt Macy static int 271959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin) 272959826caSMatt Macy { 273959826caSMatt Macy char *event; 2748bed125dSMatt Macy char *kvp, *key, *value, *r; 275959826caSMatt Macy char *debug; 276959826caSMatt Macy 277959826caSMatt Macy if ((event = strdup(eventin)) == NULL) 278959826caSMatt Macy return (ENOMEM); 2798bed125dSMatt Macy r = event; 280959826caSMatt Macy bzero(ped, sizeof(*ped)); 2810204d85aSMatt Macy ped->ped_period = DEFAULT_SAMPLE_COUNT; 28207d80fd8SMatt Macy ped->ped_umask = -1; 283959826caSMatt Macy while ((kvp = strsep(&event, ",")) != NULL) { 284959826caSMatt Macy key = strsep(&kvp, "="); 285959826caSMatt Macy if (key == NULL) 286959826caSMatt Macy abort(); 287959826caSMatt Macy value = kvp; 288959826caSMatt Macy if (strcmp(key, "umask") == 0) 289959826caSMatt Macy ped->ped_umask = strtol(value, NULL, 16); 290959826caSMatt Macy else if (strcmp(key, "event") == 0) 291959826caSMatt Macy ped->ped_event = strtol(value, NULL, 16); 292959826caSMatt Macy else if (strcmp(key, "period") == 0) 293959826caSMatt Macy ped->ped_period = strtol(value, NULL, 10); 294959826caSMatt Macy else if (strcmp(key, "offcore_rsp") == 0) 295959826caSMatt Macy ped->ped_offcore_rsp = strtol(value, NULL, 16); 296959826caSMatt Macy else if (strcmp(key, "any") == 0) 297959826caSMatt Macy ped->ped_any = strtol(value, NULL, 10); 298959826caSMatt Macy else if (strcmp(key, "cmask") == 0) 299959826caSMatt Macy ped->ped_cmask = strtol(value, NULL, 10); 300959826caSMatt Macy else if (strcmp(key, "inv") == 0) 301959826caSMatt Macy ped->ped_inv = strtol(value, NULL, 10); 302959826caSMatt Macy else if (strcmp(key, "edge") == 0) 303959826caSMatt Macy ped->ped_edge = strtol(value, NULL, 10); 304959826caSMatt Macy else if (strcmp(key, "frontend") == 0) 305959826caSMatt Macy ped->ped_frontend = strtol(value, NULL, 16); 306959826caSMatt Macy else if (strcmp(key, "ldlat") == 0) 307959826caSMatt Macy ped->ped_ldlat = strtol(value, NULL, 16); 308959826caSMatt Macy else if (strcmp(key, "fc_mask") == 0) 309959826caSMatt Macy ped->ped_fc_mask = strtol(value, NULL, 16); 310959826caSMatt Macy else if (strcmp(key, "ch_mask") == 0) 311959826caSMatt Macy ped->ped_ch_mask = strtol(value, NULL, 16); 312959826caSMatt Macy else if (strcmp(key, "config1") == 0) 313959826caSMatt Macy ped->ped_config1 = strtol(value, NULL, 16); 314dacc43dfSMatt Macy else if (strcmp(key, "l3_thread_mask") == 0) 315dacc43dfSMatt Macy ped->ped_l3_thread = strtol(value, NULL, 16); 316dacc43dfSMatt Macy else if (strcmp(key, "l3_slice_mask") == 0) 317dacc43dfSMatt Macy ped->ped_l3_slice = strtol(value, NULL, 16); 318959826caSMatt Macy else { 319959826caSMatt Macy debug = getenv("PMUDEBUG"); 320959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL) 321959826caSMatt Macy printf("unrecognized kvpair: %s:%s\n", key, value); 322959826caSMatt Macy } 323959826caSMatt Macy } 3248bed125dSMatt Macy free(r); 325959826caSMatt Macy return (0); 326959826caSMatt Macy } 327959826caSMatt Macy 328959826caSMatt Macy uint64_t 329959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name) 330959826caSMatt Macy { 331959826caSMatt Macy const struct pmu_event *pe; 332959826caSMatt Macy struct pmu_event_desc ped; 333959826caSMatt Macy 334959826caSMatt Macy event_name = pmu_alias_get(event_name); 335bfb46e2bSMatt Macy if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL) 336959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 337959826caSMatt Macy if (pe->event == NULL) 338959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 339959826caSMatt Macy if (pmu_parse_event(&ped, pe->event)) 340959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 341959826caSMatt Macy return (ped.ped_period); 342959826caSMatt Macy } 343959826caSMatt Macy 344959826caSMatt Macy int 345959826caSMatt Macy pmc_pmu_enabled(void) 346959826caSMatt Macy { 347959826caSMatt Macy 348bfb46e2bSMatt Macy return (pmu_events_map_get(NULL) != NULL); 349959826caSMatt Macy } 350959826caSMatt Macy 351959826caSMatt Macy void 352fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name) 353959826caSMatt Macy { 354959826caSMatt Macy const struct pmu_events_map *pme; 355959826caSMatt Macy const struct pmu_event *pe; 356959826caSMatt Macy struct pmu_event_desc ped; 357959826caSMatt Macy char *debug; 358959826caSMatt Macy int do_debug; 359959826caSMatt Macy 360959826caSMatt Macy debug = getenv("PMUDEBUG"); 361959826caSMatt Macy do_debug = 0; 362959826caSMatt Macy 363959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0) 364959826caSMatt Macy do_debug = 1; 365bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 366959826caSMatt Macy return; 367959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 368959826caSMatt Macy if (pe->name == NULL) 369959826caSMatt Macy continue; 370fbf962e6SMatt Macy if (event_name != NULL && strcasestr(pe->name, event_name) == NULL) 371fbf962e6SMatt Macy continue; 372959826caSMatt Macy printf("\t%s\n", pe->name); 373959826caSMatt Macy if (do_debug) 374959826caSMatt Macy pmu_parse_event(&ped, pe->event); 375959826caSMatt Macy } 376959826caSMatt Macy } 377959826caSMatt Macy 378959826caSMatt Macy void 379959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev) 380959826caSMatt Macy { 381959826caSMatt Macy const struct pmu_events_map *pme; 382959826caSMatt Macy const struct pmu_event *pe; 383959826caSMatt Macy 384bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 385959826caSMatt Macy return; 386959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 387959826caSMatt Macy if (pe->name == NULL) 388959826caSMatt Macy continue; 389959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL && 390959826caSMatt Macy pe->desc != NULL) 391959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 392959826caSMatt Macy } 393959826caSMatt Macy } 394959826caSMatt Macy 395959826caSMatt Macy void 396959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev) 397959826caSMatt Macy { 398959826caSMatt Macy const struct pmu_events_map *pme; 399959826caSMatt Macy const struct pmu_event *pe; 400959826caSMatt Macy 401bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 402959826caSMatt Macy return; 403959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 404959826caSMatt Macy if (pe->name == NULL) 405959826caSMatt Macy continue; 406959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL) { 407959826caSMatt Macy if (pe->long_desc != NULL) 408959826caSMatt Macy printf("%s:\n%s\n", pe->name, pe->long_desc); 409959826caSMatt Macy else if (pe->desc != NULL) 410959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 411959826caSMatt Macy } 412959826caSMatt Macy } 413959826caSMatt Macy } 414959826caSMatt Macy 415fbf962e6SMatt Macy void 416fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev) 417fbf962e6SMatt Macy { 418fbf962e6SMatt Macy const struct pmu_events_map *pme; 419fbf962e6SMatt Macy const struct pmu_event *pe; 420fbf962e6SMatt Macy 421bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 422fbf962e6SMatt Macy return; 423fbf962e6SMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 424fbf962e6SMatt Macy if (pe->name == NULL) 425fbf962e6SMatt Macy continue; 426fbf962e6SMatt Macy if (strcasestr(pe->name, ev) == NULL) 427fbf962e6SMatt Macy continue; 428fbf962e6SMatt Macy printf("name: %s\n", pe->name); 429fbf962e6SMatt Macy if (pe->long_desc != NULL) 430fbf962e6SMatt Macy printf("desc: %s\n", pe->long_desc); 431fbf962e6SMatt Macy else if (pe->desc != NULL) 432fbf962e6SMatt Macy printf("desc: %s\n", pe->desc); 433fbf962e6SMatt Macy if (pe->event != NULL) 434fbf962e6SMatt Macy printf("event: %s\n", pe->event); 435fbf962e6SMatt Macy if (pe->topic != NULL) 436fbf962e6SMatt Macy printf("topic: %s\n", pe->topic); 437fbf962e6SMatt Macy if (pe->pmu != NULL) 438fbf962e6SMatt Macy printf("pmu: %s\n", pe->pmu); 439fbf962e6SMatt Macy if (pe->unit != NULL) 440fbf962e6SMatt Macy printf("unit: %s\n", pe->unit); 441fbf962e6SMatt Macy if (pe->perpkg != NULL) 442fbf962e6SMatt Macy printf("perpkg: %s\n", pe->perpkg); 443fbf962e6SMatt Macy if (pe->metric_expr != NULL) 444fbf962e6SMatt Macy printf("metric_expr: %s\n", pe->metric_expr); 445fbf962e6SMatt Macy if (pe->metric_name != NULL) 446fbf962e6SMatt Macy printf("metric_name: %s\n", pe->metric_name); 447fbf962e6SMatt Macy if (pe->metric_group != NULL) 448fbf962e6SMatt Macy printf("metric_group: %s\n", pe->metric_group); 449fbf962e6SMatt Macy } 450fbf962e6SMatt Macy } 451fbf962e6SMatt Macy 4520024f1aaSMitchell Horne #if defined(__amd64__) || defined(__i386__) 45381eb4dcfSMatt Macy static int 454dacc43dfSMatt Macy pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 45581eb4dcfSMatt Macy struct pmu_event_desc *ped) 456959826caSMatt Macy { 45781eb4dcfSMatt Macy struct pmc_md_amd_op_pmcallocate *amd; 458dacc43dfSMatt Macy const struct pmu_event *pe; 459dacc43dfSMatt Macy int idx = -1; 46081eb4dcfSMatt Macy 46181eb4dcfSMatt Macy amd = &pm->pm_md.pm_amd; 46281eb4dcfSMatt Macy if (ped->ped_umask > 0) { 46381eb4dcfSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 46481eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask); 46581eb4dcfSMatt Macy } 46681eb4dcfSMatt Macy pm->pm_class = PMC_CLASS_K8; 467dacc43dfSMatt Macy pe = pmu_event_get(NULL, event_name, &idx); 46881eb4dcfSMatt Macy 469dacc43dfSMatt Macy if (strcmp("l3cache", pe->topic) == 0){ 470dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 471dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE; 472dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice); 473dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread); 474dacc43dfSMatt Macy } 475dacc43dfSMatt Macy else if (strcmp("data fabric", pe->topic) == 0){ 476dacc43dfSMatt Macy 477dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event); 478dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC; 479dacc43dfSMatt Macy } 480dacc43dfSMatt Macy else{ 481dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 482dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE; 48381eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 48481eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 48581eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 48681eb4dcfSMatt Macy amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS); 48781eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 48881eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_USR; 48981eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 49081eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_OS; 49181eb4dcfSMatt Macy if (ped->ped_edge) 49281eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 49381eb4dcfSMatt Macy if (ped->ped_inv) 49481eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 49581eb4dcfSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 49681eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_INT; 497dacc43dfSMatt Macy } 49881eb4dcfSMatt Macy return (0); 49981eb4dcfSMatt Macy } 50081eb4dcfSMatt Macy 50181eb4dcfSMatt Macy static int 50281eb4dcfSMatt Macy pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 50381eb4dcfSMatt Macy struct pmu_event_desc *ped) 50481eb4dcfSMatt Macy { 505959826caSMatt Macy struct pmc_md_iap_op_pmcallocate *iap; 506959826caSMatt Macy 50781eb4dcfSMatt Macy iap = &pm->pm_md.pm_iap; 50807d80fd8SMatt Macy if (strcasestr(event_name, "UNC_") == event_name || 509785dd70dSMatt Macy strcasestr(event_name, "uncore") != NULL) { 510785dd70dSMatt Macy pm->pm_class = PMC_CLASS_UCP; 511a191ed2dSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 51281eb4dcfSMatt Macy } else if ((ped->ped_umask == -1) || 51381eb4dcfSMatt Macy (ped->ped_event == 0x0 && ped->ped_umask == 0x3)) { 51407d80fd8SMatt Macy pm->pm_class = PMC_CLASS_IAF; 51507d80fd8SMatt Macy } else { 516959826caSMatt Macy pm->pm_class = PMC_CLASS_IAP; 51707d80fd8SMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 518785dd70dSMatt Macy } 51981eb4dcfSMatt Macy iap->pm_iap_config |= IAP_EVSEL(ped->ped_event); 52081eb4dcfSMatt Macy if (ped->ped_umask > 0) 52181eb4dcfSMatt Macy iap->pm_iap_config |= IAP_UMASK(ped->ped_umask); 52281eb4dcfSMatt Macy iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask); 52381eb4dcfSMatt Macy iap->pm_iap_rsp = ped->ped_offcore_rsp; 524959826caSMatt Macy 52581eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 52681eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 52781eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 528959826caSMatt Macy iap->pm_iap_config |= (IAP_USR | IAP_OS); 52981eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 53081eb4dcfSMatt Macy iap->pm_iap_config |= IAP_USR; 53181eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 53281eb4dcfSMatt Macy iap->pm_iap_config |= IAP_OS; 53381eb4dcfSMatt Macy if (ped->ped_edge) 534959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 53581eb4dcfSMatt Macy if (ped->ped_any) 536959826caSMatt Macy iap->pm_iap_config |= IAP_ANY; 53781eb4dcfSMatt Macy if (ped->ped_inv) 538959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 539959826caSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 540959826caSMatt Macy iap->pm_iap_config |= IAP_INT; 541959826caSMatt Macy return (0); 542959826caSMatt Macy } 543959826caSMatt Macy 54481eb4dcfSMatt Macy int 54581eb4dcfSMatt Macy pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 54681eb4dcfSMatt Macy { 54781eb4dcfSMatt Macy const struct pmu_event *pe; 54881eb4dcfSMatt Macy struct pmu_event_desc ped; 54981eb4dcfSMatt Macy pmu_mfr_t mfr; 55081eb4dcfSMatt Macy int idx = -1; 55181eb4dcfSMatt Macy 55281eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 55381eb4dcfSMatt Macy return (ENOENT); 55481eb4dcfSMatt Macy 55581eb4dcfSMatt Macy bzero(&pm->pm_md, sizeof(pm->pm_md)); 55681eb4dcfSMatt Macy pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 55781eb4dcfSMatt Macy event_name = pmu_alias_get(event_name); 55881eb4dcfSMatt Macy if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 55981eb4dcfSMatt Macy return (ENOENT); 56081eb4dcfSMatt Macy assert(idx >= 0); 56181eb4dcfSMatt Macy pm->pm_ev = idx; 56281eb4dcfSMatt Macy 56381eb4dcfSMatt Macy if (pe->event == NULL) 56481eb4dcfSMatt Macy return (ENOENT); 56581eb4dcfSMatt Macy if (pmu_parse_event(&ped, pe->event)) 56681eb4dcfSMatt Macy return (ENOENT); 56781eb4dcfSMatt Macy 56881eb4dcfSMatt Macy if (mfr == PMU_INTEL) 56981eb4dcfSMatt Macy return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped)); 57081eb4dcfSMatt Macy else 57181eb4dcfSMatt Macy return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped)); 57281eb4dcfSMatt Macy } 57381eb4dcfSMatt Macy 57428dd6730SMitchell Horne #elif defined(__aarch64__) 57528dd6730SMitchell Horne 57628dd6730SMitchell Horne int 57728dd6730SMitchell Horne pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 57828dd6730SMitchell Horne { 57928dd6730SMitchell Horne const struct pmu_event *pe; 580*27ea55fcSMitchell Horne struct pmu_event_desc ped; 58128dd6730SMitchell Horne int idx = -1; 58228dd6730SMitchell Horne 58328dd6730SMitchell Horne event_name = pmu_alias_get(event_name); 58428dd6730SMitchell Horne if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 58528dd6730SMitchell Horne return (ENOENT); 58628dd6730SMitchell Horne if (pe->event == NULL) 58728dd6730SMitchell Horne return (ENOENT); 588*27ea55fcSMitchell Horne if (pmu_parse_event(&ped, pe->event)) 589*27ea55fcSMitchell Horne return (ENOENT); 59028dd6730SMitchell Horne 59128dd6730SMitchell Horne assert(idx >= 0); 592*27ea55fcSMitchell Horne pm->pm_ev = idx; 593*27ea55fcSMitchell Horne pm->pm_md.pm_md_config = ped.ped_event; 59428dd6730SMitchell Horne pm->pm_md.pm_md_flags |= PM_MD_RAW_EVENT; 59528dd6730SMitchell Horne pm->pm_class = PMC_CLASS_ARMV8; 59628dd6730SMitchell Horne pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 59728dd6730SMitchell Horne 59828dd6730SMitchell Horne return (0); 59928dd6730SMitchell Horne } 60028dd6730SMitchell Horne 601959826caSMatt Macy #else 6028b20f975SMatt Macy 6038b20f975SMatt Macy int 6048b20f975SMatt Macy pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused) 6058b20f975SMatt Macy { 6068b20f975SMatt Macy return (EOPNOTSUPP); 6078b20f975SMatt Macy } 608959826caSMatt Macy #endif 609