1959826caSMatt Macy /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 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 */ 32959826caSMatt Macy 33959826caSMatt Macy #include <sys/types.h> 34959826caSMatt Macy #include <sys/errno.h> 3524e337beSRyan Moeller #include <sys/pmc.h> 36959826caSMatt Macy #include <sys/sysctl.h> 37959826caSMatt Macy #include <stddef.h> 38959826caSMatt Macy #include <stdlib.h> 39959826caSMatt Macy #include <limits.h> 40f3dbece8SEmmanuel Vadot #include <regex.h> 41959826caSMatt Macy #include <string.h> 42959826caSMatt Macy #include <pmc.h> 43959826caSMatt Macy #include <pmclog.h> 441b000b50SMatt Macy #include <assert.h> 45959826caSMatt Macy #include <libpmcstat.h> 46959826caSMatt Macy #include "pmu-events/pmu-events.h" 47959826caSMatt Macy 48959826caSMatt Macy struct pmu_alias { 49959826caSMatt Macy const char *pa_alias; 50959826caSMatt Macy const char *pa_name; 51959826caSMatt Macy }; 5281eb4dcfSMatt Macy 530024f1aaSMitchell Horne #if defined(__amd64__) || defined(__i386__) 5481eb4dcfSMatt Macy typedef enum { 5581eb4dcfSMatt Macy PMU_INVALID, 5681eb4dcfSMatt Macy PMU_INTEL, 5781eb4dcfSMatt Macy PMU_AMD, 5881eb4dcfSMatt Macy } pmu_mfr_t; 5981eb4dcfSMatt Macy 6081eb4dcfSMatt Macy static struct pmu_alias pmu_intel_alias_table[] = { 61e144cd92SAlexander Motin {"UNHALTED_CORE_CYCLES", "cpu_clk_unhalted.thread"}, 62e144cd92SAlexander Motin {"UNHALTED-CORE-CYCLES", "cpu_clk_unhalted.thread"}, 63959826caSMatt Macy {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"}, 64959826caSMatt Macy {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"}, 65959826caSMatt Macy {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"}, 66959826caSMatt Macy {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"}, 67959826caSMatt Macy {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"}, 68959826caSMatt Macy {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"}, 69959826caSMatt Macy {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"}, 70959826caSMatt Macy {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"}, 71959826caSMatt Macy {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"}, 72959826caSMatt Macy {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"}, 73959826caSMatt Macy {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"}, 74959826caSMatt Macy {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"}, 75e144cd92SAlexander Motin {"unhalted-cycles", "cpu_clk_unhalted.thread"}, 76e144cd92SAlexander Motin {"instructions", "inst_retired.any"}, 7796cbd26aSMatt Macy {"branch-mispredicts", "br_misp_retired.all_branches"}, 7896cbd26aSMatt Macy {"branches", "br_inst_retired.all_branches"}, 7996cbd26aSMatt Macy {"interrupts", "hw_interrupts.received"}, 8096cbd26aSMatt Macy {"ic-misses", "frontend_retired.l1i_miss"}, 81959826caSMatt Macy {NULL, NULL}, 82959826caSMatt Macy }; 83959826caSMatt Macy 8481eb4dcfSMatt Macy static struct pmu_alias pmu_amd_alias_table[] = { 8581eb4dcfSMatt Macy {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"}, 8681eb4dcfSMatt Macy {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"}, 87a20c1089SMitchell Horne {"LLC_MISSES", "l3_comb_clstr_state.request_miss"}, 88a20c1089SMitchell Horne {"LLC-MISSES", "l3_comb_clstr_state.request_miss"}, 89a20c1089SMitchell Horne {"LLC_REFERENCE", "l3_request_g1.caching_l3_cache_accesses"}, 90a20c1089SMitchell Horne {"LLC-REFERENCE", "l3_request_g1.caching_l3_cache_accesses"}, 91a20c1089SMitchell Horne {"BRANCH_INSTRUCTION_RETIRED", "ex_ret_brn"}, 92a20c1089SMitchell Horne {"BRANCH-INSTRUCTION-RETIRED", "ex_ret_brn"}, 93a20c1089SMitchell Horne {"BRANCH_MISSES_RETIRED", "ex_ret_brn_misp"}, 94a20c1089SMitchell Horne {"BRANCH-MISSES-RETIRED", "ex_ret_brn_misp"}, 95a20c1089SMitchell Horne {"unhalted-cycles", "ls_not_halted_cyc"}, 96a20c1089SMitchell Horne {"instructions", "ex_ret_instr",}, 97a20c1089SMitchell Horne {"branch-mispredicts", "ex_ret_brn_misp"}, 98a20c1089SMitchell Horne {"branches", "ex_ret_brn"}, 99a20c1089SMitchell Horne {"interrupts", "ls_int_taken"}, /* Not on amdzen1 */ 10081eb4dcfSMatt Macy {NULL, NULL}, 10181eb4dcfSMatt Macy }; 10281eb4dcfSMatt Macy 10381eb4dcfSMatt Macy 10481eb4dcfSMatt Macy static pmu_mfr_t 10581eb4dcfSMatt Macy pmu_events_mfr(void) 10681eb4dcfSMatt Macy { 10724e337beSRyan Moeller char buf[PMC_CPUID_LEN]; 10824e337beSRyan Moeller size_t s = sizeof(buf); 10981eb4dcfSMatt Macy pmu_mfr_t mfr; 11081eb4dcfSMatt Macy 11124e337beSRyan Moeller if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 11281eb4dcfSMatt Macy (void *)NULL, 0) == -1) 11381eb4dcfSMatt Macy return (PMU_INVALID); 11453071ed1SKonstantin Belousov if (strcasestr(buf, "AuthenticAMD") != NULL || 11553071ed1SKonstantin Belousov strcasestr(buf, "HygonGenuine") != NULL) 11681eb4dcfSMatt Macy mfr = PMU_AMD; 11781eb4dcfSMatt Macy else if (strcasestr(buf, "GenuineIntel") != NULL) 11881eb4dcfSMatt Macy mfr = PMU_INTEL; 11981eb4dcfSMatt Macy else 12081eb4dcfSMatt Macy mfr = PMU_INVALID; 12181eb4dcfSMatt Macy return (mfr); 12281eb4dcfSMatt Macy } 12381eb4dcfSMatt Macy 12407d80fd8SMatt Macy /* 12507d80fd8SMatt Macy * The Intel fixed mode counters are: 12607d80fd8SMatt Macy * "inst_retired.any", 12707d80fd8SMatt Macy * "cpu_clk_unhalted.thread", 12807d80fd8SMatt Macy * "cpu_clk_unhalted.thread_any", 12907d80fd8SMatt Macy * "cpu_clk_unhalted.ref_tsc", 13007d80fd8SMatt Macy * 13107d80fd8SMatt Macy */ 132a191ed2dSMatt Macy 133959826caSMatt Macy static const char * 134959826caSMatt Macy pmu_alias_get(const char *name) 135959826caSMatt Macy { 13681eb4dcfSMatt Macy pmu_mfr_t mfr; 137959826caSMatt Macy struct pmu_alias *pa; 13881eb4dcfSMatt Macy struct pmu_alias *pmu_alias_table; 13981eb4dcfSMatt Macy 14081eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 14181eb4dcfSMatt Macy return (name); 14281eb4dcfSMatt Macy if (mfr == PMU_AMD) 14381eb4dcfSMatt Macy pmu_alias_table = pmu_amd_alias_table; 14481eb4dcfSMatt Macy else if (mfr == PMU_INTEL) 14581eb4dcfSMatt Macy pmu_alias_table = pmu_intel_alias_table; 14681eb4dcfSMatt Macy else 14781eb4dcfSMatt Macy return (name); 148959826caSMatt Macy 149959826caSMatt Macy for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++) 150959826caSMatt Macy if (strcasecmp(name, pa->pa_alias) == 0) 151959826caSMatt Macy return (pa->pa_name); 15281eb4dcfSMatt Macy 153959826caSMatt Macy return (name); 154959826caSMatt Macy } 155b48a2770SLeandro Lupori #elif defined(__powerpc64__) 156b48a2770SLeandro Lupori 157b48a2770SLeandro Lupori static const char * 158b48a2770SLeandro Lupori pmu_alias_get(const char *name) 159b48a2770SLeandro Lupori { 160b48a2770SLeandro Lupori return (name); 161b48a2770SLeandro Lupori } 162959826caSMatt Macy 16328dd6730SMitchell Horne #elif defined(__aarch64__) 16428dd6730SMitchell Horne 16528dd6730SMitchell Horne static struct pmu_alias pmu_armv8_alias_table[] = { 1666f50b73eSMitchell Horne {"UNHALTED_CORE_CYCLES", "CPU_CYCLES"}, 1676f50b73eSMitchell Horne {"UNHALTED-CORE-CYCLES", "CPU_CYCLES"}, 1686f50b73eSMitchell Horne {"LLC_MISSES", "LL_CACHE_MISS_RD"}, 1696f50b73eSMitchell Horne {"LLC-MISSES", "LL_CACHE_MISS_RD"}, 1706f50b73eSMitchell Horne {"LLC_REFERENCE", "LL_CACHE_RD"}, 1716f50b73eSMitchell Horne {"LLC-REFERENCE", "LL_CACHE_RD"}, 1726f50b73eSMitchell Horne {"BRANCH_INSTRUCTION_RETIRED", "BR_RETIRED"}, 1736f50b73eSMitchell Horne {"BRANCH-INSTRUCTION-RETIRED", "BR_RETIRED"}, 1746f50b73eSMitchell Horne {"BRANCH_MISSES_RETIRED", "BR_MIS_PRED_RETIRED"}, 1756f50b73eSMitchell Horne {"BRANCH-MISSES-RETIRED", "BR_MIS_PRED_RETIRED"}, 1766f50b73eSMitchell Horne {"unhalted-cycles", "CPU_CYCLES"}, 1776f50b73eSMitchell Horne {"instructions", "INST_RETIRED",}, 1786f50b73eSMitchell Horne {"branch-mispredicts", "BR_MIS_PRED_RETIRED"}, 1796f50b73eSMitchell Horne {"branches", "BR_RETIRED"}, 1806f50b73eSMitchell Horne {"interrupts", "EXC_IRQ"}, 18128dd6730SMitchell Horne {NULL, NULL}, 18228dd6730SMitchell Horne }; 18328dd6730SMitchell Horne 18428dd6730SMitchell Horne static const char * 18528dd6730SMitchell Horne pmu_alias_get(const char *name) 18628dd6730SMitchell Horne { 18728dd6730SMitchell Horne struct pmu_alias *pa; 18828dd6730SMitchell Horne 18928dd6730SMitchell Horne for (pa = pmu_armv8_alias_table; pa->pa_alias != NULL; pa++) 19028dd6730SMitchell Horne if (strcasecmp(name, pa->pa_alias) == 0) 19128dd6730SMitchell Horne return (pa->pa_name); 19228dd6730SMitchell Horne 19328dd6730SMitchell Horne return (name); 19428dd6730SMitchell Horne } 19528dd6730SMitchell Horne 1960024f1aaSMitchell Horne #else 1970024f1aaSMitchell Horne 1980024f1aaSMitchell Horne static const char * 1990024f1aaSMitchell Horne pmu_alias_get(const char *name) 2000024f1aaSMitchell Horne { 2010024f1aaSMitchell Horne 2020024f1aaSMitchell Horne return (name); 2030024f1aaSMitchell Horne } 2040024f1aaSMitchell Horne #endif 2050024f1aaSMitchell Horne 206959826caSMatt Macy struct pmu_event_desc { 207959826caSMatt Macy uint64_t ped_period; 208959826caSMatt Macy uint64_t ped_offcore_rsp; 209dacc43dfSMatt Macy uint64_t ped_l3_thread; 210dacc43dfSMatt Macy uint64_t ped_l3_slice; 211959826caSMatt Macy uint32_t ped_event; 212959826caSMatt Macy uint32_t ped_frontend; 213959826caSMatt Macy uint32_t ped_ldlat; 214959826caSMatt Macy uint32_t ped_config1; 21507d80fd8SMatt Macy int16_t ped_umask; 216959826caSMatt Macy uint8_t ped_cmask; 217959826caSMatt Macy uint8_t ped_any; 218959826caSMatt Macy uint8_t ped_inv; 219959826caSMatt Macy uint8_t ped_edge; 220959826caSMatt Macy uint8_t ped_fc_mask; 221959826caSMatt Macy uint8_t ped_ch_mask; 222959826caSMatt Macy }; 223959826caSMatt Macy 224959826caSMatt Macy static const struct pmu_events_map * 225bfb46e2bSMatt Macy pmu_events_map_get(const char *cpuid) 226959826caSMatt Macy { 227a0ac5706SEmmanuel Vadot regex_t re; 228a0ac5706SEmmanuel Vadot regmatch_t pmatch[1]; 22924e337beSRyan Moeller char buf[PMC_CPUID_LEN]; 23024e337beSRyan Moeller size_t s = sizeof(buf); 231a0ac5706SEmmanuel Vadot int match; 232959826caSMatt Macy const struct pmu_events_map *pme; 233959826caSMatt Macy 234bfb46e2bSMatt Macy if (cpuid != NULL) { 23524e337beSRyan Moeller strlcpy(buf, cpuid, s); 236bfb46e2bSMatt Macy } else { 237959826caSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 238959826caSMatt Macy (void *)NULL, 0) == -1) 239959826caSMatt Macy return (NULL); 240bfb46e2bSMatt Macy } 241a0ac5706SEmmanuel Vadot for (pme = pmu_events_map; pme->cpuid != NULL; pme++) { 242a0ac5706SEmmanuel Vadot if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) { 243a0ac5706SEmmanuel Vadot printf("regex '%s' failed to compile, ignoring\n", 244a0ac5706SEmmanuel Vadot pme->cpuid); 245a0ac5706SEmmanuel Vadot continue; 246a0ac5706SEmmanuel Vadot } 247a0ac5706SEmmanuel Vadot match = regexec(&re, buf, 1, pmatch, 0); 248a0ac5706SEmmanuel Vadot regfree(&re); 249a0ac5706SEmmanuel Vadot if (match == 0) { 2501791cad0SAlexander Motin if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0 2511791cad0SAlexander Motin || buf[pmatch[0].rm_eo] == '-')) 252959826caSMatt Macy return (pme); 253a0ac5706SEmmanuel Vadot } 254a0ac5706SEmmanuel Vadot } 255959826caSMatt Macy return (NULL); 256959826caSMatt Macy } 257959826caSMatt Macy 258959826caSMatt Macy static const struct pmu_event * 259bfb46e2bSMatt Macy pmu_event_get(const char *cpuid, const char *event_name, int *idx) 260959826caSMatt Macy { 261959826caSMatt Macy const struct pmu_events_map *pme; 262959826caSMatt Macy const struct pmu_event *pe; 263959826caSMatt Macy int i; 264959826caSMatt Macy 265bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 266959826caSMatt Macy return (NULL); 267959826caSMatt Macy for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) { 268959826caSMatt Macy if (pe->name == NULL) 269959826caSMatt Macy continue; 270959826caSMatt Macy if (strcasecmp(pe->name, event_name) == 0) { 271959826caSMatt Macy if (idx) 272959826caSMatt Macy *idx = i; 273959826caSMatt Macy return (pe); 274959826caSMatt Macy } 275959826caSMatt Macy } 276959826caSMatt Macy return (NULL); 277959826caSMatt Macy } 278959826caSMatt Macy 279bfb46e2bSMatt Macy int 280bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *cpuid, const char *event) 281bfb46e2bSMatt Macy { 282bfb46e2bSMatt Macy int idx; 283bfb46e2bSMatt Macy const char *realname; 284bfb46e2bSMatt Macy 285bfb46e2bSMatt Macy realname = pmu_alias_get(event); 286bfb46e2bSMatt Macy if (pmu_event_get(cpuid, realname, &idx) == NULL) 287bfb46e2bSMatt Macy return (-1); 288bfb46e2bSMatt Macy return (idx); 289bfb46e2bSMatt Macy } 290bfb46e2bSMatt Macy 291959826caSMatt Macy const char * 292b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *cpuid, int idx) 293959826caSMatt Macy { 294959826caSMatt Macy const struct pmu_events_map *pme; 295959826caSMatt Macy 296b2ca2e50SMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 297959826caSMatt Macy return (NULL); 2981b000b50SMatt Macy assert(pme->table[idx].name); 2991b000b50SMatt Macy return (pme->table[idx].name); 300959826caSMatt Macy } 301959826caSMatt Macy 302959826caSMatt Macy static int 303959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin) 304959826caSMatt Macy { 305959826caSMatt Macy char *event; 3068bed125dSMatt Macy char *kvp, *key, *value, *r; 307959826caSMatt Macy char *debug; 308959826caSMatt Macy 309959826caSMatt Macy if ((event = strdup(eventin)) == NULL) 310959826caSMatt Macy return (ENOMEM); 3118bed125dSMatt Macy r = event; 312959826caSMatt Macy bzero(ped, sizeof(*ped)); 3130204d85aSMatt Macy ped->ped_period = DEFAULT_SAMPLE_COUNT; 31407d80fd8SMatt Macy ped->ped_umask = -1; 315959826caSMatt Macy while ((kvp = strsep(&event, ",")) != NULL) { 316959826caSMatt Macy key = strsep(&kvp, "="); 317959826caSMatt Macy if (key == NULL) 318959826caSMatt Macy abort(); 319959826caSMatt Macy value = kvp; 320959826caSMatt Macy if (strcmp(key, "umask") == 0) 321959826caSMatt Macy ped->ped_umask = strtol(value, NULL, 16); 322959826caSMatt Macy else if (strcmp(key, "event") == 0) 323959826caSMatt Macy ped->ped_event = strtol(value, NULL, 16); 324959826caSMatt Macy else if (strcmp(key, "period") == 0) 325959826caSMatt Macy ped->ped_period = strtol(value, NULL, 10); 326959826caSMatt Macy else if (strcmp(key, "offcore_rsp") == 0) 327959826caSMatt Macy ped->ped_offcore_rsp = strtol(value, NULL, 16); 328959826caSMatt Macy else if (strcmp(key, "any") == 0) 329959826caSMatt Macy ped->ped_any = strtol(value, NULL, 10); 330959826caSMatt Macy else if (strcmp(key, "cmask") == 0) 331959826caSMatt Macy ped->ped_cmask = strtol(value, NULL, 10); 332959826caSMatt Macy else if (strcmp(key, "inv") == 0) 333959826caSMatt Macy ped->ped_inv = strtol(value, NULL, 10); 334959826caSMatt Macy else if (strcmp(key, "edge") == 0) 335959826caSMatt Macy ped->ped_edge = strtol(value, NULL, 10); 336959826caSMatt Macy else if (strcmp(key, "frontend") == 0) 337959826caSMatt Macy ped->ped_frontend = strtol(value, NULL, 16); 338959826caSMatt Macy else if (strcmp(key, "ldlat") == 0) 339959826caSMatt Macy ped->ped_ldlat = strtol(value, NULL, 16); 340959826caSMatt Macy else if (strcmp(key, "fc_mask") == 0) 341959826caSMatt Macy ped->ped_fc_mask = strtol(value, NULL, 16); 342959826caSMatt Macy else if (strcmp(key, "ch_mask") == 0) 343959826caSMatt Macy ped->ped_ch_mask = strtol(value, NULL, 16); 344959826caSMatt Macy else if (strcmp(key, "config1") == 0) 345959826caSMatt Macy ped->ped_config1 = strtol(value, NULL, 16); 346dacc43dfSMatt Macy else if (strcmp(key, "l3_thread_mask") == 0) 347dacc43dfSMatt Macy ped->ped_l3_thread = strtol(value, NULL, 16); 348dacc43dfSMatt Macy else if (strcmp(key, "l3_slice_mask") == 0) 349dacc43dfSMatt Macy ped->ped_l3_slice = strtol(value, NULL, 16); 350959826caSMatt Macy else { 351959826caSMatt Macy debug = getenv("PMUDEBUG"); 352959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL) 353959826caSMatt Macy printf("unrecognized kvpair: %s:%s\n", key, value); 354959826caSMatt Macy } 355959826caSMatt Macy } 3568bed125dSMatt Macy free(r); 357959826caSMatt Macy return (0); 358959826caSMatt Macy } 359959826caSMatt Macy 360959826caSMatt Macy uint64_t 361959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name) 362959826caSMatt Macy { 363959826caSMatt Macy const struct pmu_event *pe; 364959826caSMatt Macy struct pmu_event_desc ped; 365959826caSMatt Macy 366959826caSMatt Macy event_name = pmu_alias_get(event_name); 367bfb46e2bSMatt Macy if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL) 368959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 369959826caSMatt Macy if (pe->event == NULL) 370959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 371959826caSMatt Macy if (pmu_parse_event(&ped, pe->event)) 372959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 373959826caSMatt Macy return (ped.ped_period); 374959826caSMatt Macy } 375959826caSMatt Macy 376959826caSMatt Macy int 377959826caSMatt Macy pmc_pmu_enabled(void) 378959826caSMatt Macy { 379959826caSMatt Macy 380bfb46e2bSMatt Macy return (pmu_events_map_get(NULL) != NULL); 381959826caSMatt Macy } 382959826caSMatt Macy 383959826caSMatt Macy void 384fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name) 385959826caSMatt Macy { 386959826caSMatt Macy const struct pmu_events_map *pme; 387959826caSMatt Macy const struct pmu_event *pe; 388959826caSMatt Macy struct pmu_event_desc ped; 389959826caSMatt Macy char *debug; 390959826caSMatt Macy int do_debug; 391959826caSMatt Macy 392959826caSMatt Macy debug = getenv("PMUDEBUG"); 393959826caSMatt Macy do_debug = 0; 394959826caSMatt Macy 395959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0) 396959826caSMatt Macy do_debug = 1; 397bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 398959826caSMatt Macy return; 399959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 400959826caSMatt Macy if (pe->name == NULL) 401959826caSMatt Macy continue; 402fbf962e6SMatt Macy if (event_name != NULL && strcasestr(pe->name, event_name) == NULL) 403fbf962e6SMatt Macy continue; 404959826caSMatt Macy printf("\t%s\n", pe->name); 405959826caSMatt Macy if (do_debug) 406959826caSMatt Macy pmu_parse_event(&ped, pe->event); 407959826caSMatt Macy } 408959826caSMatt Macy } 409959826caSMatt Macy 410959826caSMatt Macy void 411959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev) 412959826caSMatt Macy { 413959826caSMatt Macy const struct pmu_events_map *pme; 414959826caSMatt Macy const struct pmu_event *pe; 415959826caSMatt Macy 416bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 417959826caSMatt Macy return; 418959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 419959826caSMatt Macy if (pe->name == NULL) 420959826caSMatt Macy continue; 421959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL && 422959826caSMatt Macy pe->desc != NULL) 423959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 424959826caSMatt Macy } 425959826caSMatt Macy } 426959826caSMatt Macy 427959826caSMatt Macy void 428959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev) 429959826caSMatt Macy { 430959826caSMatt Macy const struct pmu_events_map *pme; 431959826caSMatt Macy const struct pmu_event *pe; 432959826caSMatt Macy 433bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 434959826caSMatt Macy return; 435959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 436959826caSMatt Macy if (pe->name == NULL) 437959826caSMatt Macy continue; 438959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL) { 439959826caSMatt Macy if (pe->long_desc != NULL) 440959826caSMatt Macy printf("%s:\n%s\n", pe->name, pe->long_desc); 441959826caSMatt Macy else if (pe->desc != NULL) 442959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 443959826caSMatt Macy } 444959826caSMatt Macy } 445959826caSMatt Macy } 446959826caSMatt Macy 447fbf962e6SMatt Macy void 448fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev) 449fbf962e6SMatt Macy { 450fbf962e6SMatt Macy const struct pmu_events_map *pme; 451fbf962e6SMatt Macy const struct pmu_event *pe; 452fbf962e6SMatt Macy 453bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 454fbf962e6SMatt Macy return; 455fbf962e6SMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 456fbf962e6SMatt Macy if (pe->name == NULL) 457fbf962e6SMatt Macy continue; 458fbf962e6SMatt Macy if (strcasestr(pe->name, ev) == NULL) 459fbf962e6SMatt Macy continue; 460fbf962e6SMatt Macy printf("name: %s\n", pe->name); 461fbf962e6SMatt Macy if (pe->long_desc != NULL) 462fbf962e6SMatt Macy printf("desc: %s\n", pe->long_desc); 463fbf962e6SMatt Macy else if (pe->desc != NULL) 464fbf962e6SMatt Macy printf("desc: %s\n", pe->desc); 465fbf962e6SMatt Macy if (pe->event != NULL) 466fbf962e6SMatt Macy printf("event: %s\n", pe->event); 467fbf962e6SMatt Macy if (pe->topic != NULL) 468fbf962e6SMatt Macy printf("topic: %s\n", pe->topic); 469fbf962e6SMatt Macy if (pe->pmu != NULL) 470fbf962e6SMatt Macy printf("pmu: %s\n", pe->pmu); 471fbf962e6SMatt Macy if (pe->unit != NULL) 472fbf962e6SMatt Macy printf("unit: %s\n", pe->unit); 473fbf962e6SMatt Macy if (pe->perpkg != NULL) 474fbf962e6SMatt Macy printf("perpkg: %s\n", pe->perpkg); 475fbf962e6SMatt Macy if (pe->metric_expr != NULL) 476fbf962e6SMatt Macy printf("metric_expr: %s\n", pe->metric_expr); 477fbf962e6SMatt Macy if (pe->metric_name != NULL) 478fbf962e6SMatt Macy printf("metric_name: %s\n", pe->metric_name); 479fbf962e6SMatt Macy if (pe->metric_group != NULL) 480fbf962e6SMatt Macy printf("metric_group: %s\n", pe->metric_group); 481fbf962e6SMatt Macy } 482fbf962e6SMatt Macy } 483fbf962e6SMatt Macy 4840024f1aaSMitchell Horne #if defined(__amd64__) || defined(__i386__) 48581eb4dcfSMatt Macy static int 486dacc43dfSMatt Macy pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 48781eb4dcfSMatt Macy struct pmu_event_desc *ped) 488959826caSMatt Macy { 48981eb4dcfSMatt Macy struct pmc_md_amd_op_pmcallocate *amd; 490dacc43dfSMatt Macy const struct pmu_event *pe; 491dacc43dfSMatt Macy int idx = -1; 49281eb4dcfSMatt Macy 49381eb4dcfSMatt Macy amd = &pm->pm_md.pm_amd; 49481eb4dcfSMatt Macy if (ped->ped_umask > 0) { 49581eb4dcfSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 49681eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask); 49781eb4dcfSMatt Macy } 49881eb4dcfSMatt Macy pm->pm_class = PMC_CLASS_K8; 499dacc43dfSMatt Macy pe = pmu_event_get(NULL, event_name, &idx); 50081eb4dcfSMatt Macy 501dacc43dfSMatt Macy if (strcmp("l3cache", pe->topic) == 0){ 502dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 503dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE; 504dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice); 505dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread); 506dacc43dfSMatt Macy } 507dacc43dfSMatt Macy else if (strcmp("data fabric", pe->topic) == 0){ 508dacc43dfSMatt Macy 509dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event); 510dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC; 511dacc43dfSMatt Macy } 512dacc43dfSMatt Macy else{ 513dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 514dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE; 51581eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 51681eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 51781eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 51881eb4dcfSMatt Macy amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS); 51981eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 52081eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_USR; 52181eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 52281eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_OS; 52381eb4dcfSMatt Macy if (ped->ped_edge) 52481eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 52581eb4dcfSMatt Macy if (ped->ped_inv) 526037dd0a9SAlexander Motin amd->pm_amd_config |= AMD_PMC_INVERT; 52781eb4dcfSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 52881eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_INT; 529dacc43dfSMatt Macy } 53081eb4dcfSMatt Macy return (0); 53181eb4dcfSMatt Macy } 53281eb4dcfSMatt Macy 53381eb4dcfSMatt Macy static int 53481eb4dcfSMatt Macy pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 53581eb4dcfSMatt Macy struct pmu_event_desc *ped) 53681eb4dcfSMatt Macy { 537959826caSMatt Macy struct pmc_md_iap_op_pmcallocate *iap; 538959826caSMatt Macy 53981eb4dcfSMatt Macy iap = &pm->pm_md.pm_iap; 54007d80fd8SMatt Macy if (strcasestr(event_name, "UNC_") == event_name || 541785dd70dSMatt Macy strcasestr(event_name, "uncore") != NULL) { 542785dd70dSMatt Macy pm->pm_class = PMC_CLASS_UCP; 543a191ed2dSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 54473b7b181SAlexander Motin } else if (ped->ped_event == 0x0) { 54507d80fd8SMatt Macy pm->pm_class = PMC_CLASS_IAF; 54607d80fd8SMatt Macy } else { 547959826caSMatt Macy pm->pm_class = PMC_CLASS_IAP; 54807d80fd8SMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 549785dd70dSMatt Macy } 55081eb4dcfSMatt Macy iap->pm_iap_config |= IAP_EVSEL(ped->ped_event); 55181eb4dcfSMatt Macy if (ped->ped_umask > 0) 55281eb4dcfSMatt Macy iap->pm_iap_config |= IAP_UMASK(ped->ped_umask); 55381eb4dcfSMatt Macy iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask); 55481eb4dcfSMatt Macy iap->pm_iap_rsp = ped->ped_offcore_rsp; 555959826caSMatt Macy 55681eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 55781eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 55881eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 559959826caSMatt Macy iap->pm_iap_config |= (IAP_USR | IAP_OS); 56081eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 56181eb4dcfSMatt Macy iap->pm_iap_config |= IAP_USR; 56281eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 56381eb4dcfSMatt Macy iap->pm_iap_config |= IAP_OS; 56481eb4dcfSMatt Macy if (ped->ped_edge) 565959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 56681eb4dcfSMatt Macy if (ped->ped_any) 567959826caSMatt Macy iap->pm_iap_config |= IAP_ANY; 56881eb4dcfSMatt Macy if (ped->ped_inv) 569037dd0a9SAlexander Motin iap->pm_iap_config |= IAP_INV; 570959826caSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 571959826caSMatt Macy iap->pm_iap_config |= IAP_INT; 572959826caSMatt Macy return (0); 573959826caSMatt Macy } 574959826caSMatt Macy 57545dcc17eSMitchell Horne static int 57645dcc17eSMitchell Horne pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm) 57781eb4dcfSMatt Macy { 57881eb4dcfSMatt Macy const struct pmu_event *pe; 57981eb4dcfSMatt Macy struct pmu_event_desc ped; 58081eb4dcfSMatt Macy pmu_mfr_t mfr; 58181eb4dcfSMatt Macy int idx = -1; 58281eb4dcfSMatt Macy 58381eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 58481eb4dcfSMatt Macy return (ENOENT); 58581eb4dcfSMatt Macy 58681eb4dcfSMatt Macy bzero(&pm->pm_md, sizeof(pm->pm_md)); 58781eb4dcfSMatt Macy pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 58881eb4dcfSMatt Macy event_name = pmu_alias_get(event_name); 58981eb4dcfSMatt Macy if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 59081eb4dcfSMatt Macy return (ENOENT); 59181eb4dcfSMatt Macy assert(idx >= 0); 59281eb4dcfSMatt Macy pm->pm_ev = idx; 59381eb4dcfSMatt Macy 59481eb4dcfSMatt Macy if (pe->event == NULL) 59581eb4dcfSMatt Macy return (ENOENT); 59681eb4dcfSMatt Macy if (pmu_parse_event(&ped, pe->event)) 59781eb4dcfSMatt Macy return (ENOENT); 59881eb4dcfSMatt Macy 59981eb4dcfSMatt Macy if (mfr == PMU_INTEL) 60081eb4dcfSMatt Macy return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped)); 60181eb4dcfSMatt Macy else 60281eb4dcfSMatt Macy return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped)); 60381eb4dcfSMatt Macy } 60481eb4dcfSMatt Macy 605b48a2770SLeandro Lupori #elif defined(__powerpc64__) 606b48a2770SLeandro Lupori 60745dcc17eSMitchell Horne static int 60845dcc17eSMitchell Horne pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm) 609b48a2770SLeandro Lupori { 610b48a2770SLeandro Lupori const struct pmu_event *pe; 611b48a2770SLeandro Lupori struct pmu_event_desc ped; 612b48a2770SLeandro Lupori int idx = -1; 613b48a2770SLeandro Lupori 614b48a2770SLeandro Lupori bzero(&pm->pm_md, sizeof(pm->pm_md)); 615b48a2770SLeandro Lupori pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 616b48a2770SLeandro Lupori event_name = pmu_alias_get(event_name); 617b48a2770SLeandro Lupori 618b48a2770SLeandro Lupori if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 619b48a2770SLeandro Lupori return (ENOENT); 620b48a2770SLeandro Lupori if (pe->event == NULL) 621b48a2770SLeandro Lupori return (ENOENT); 622b48a2770SLeandro Lupori if (pmu_parse_event(&ped, pe->event)) 623b48a2770SLeandro Lupori return (ENOENT); 624b48a2770SLeandro Lupori 625b48a2770SLeandro Lupori pm->pm_ev = idx; 626b48a2770SLeandro Lupori pm->pm_md.pm_event = ped.ped_event; 627b48a2770SLeandro Lupori pm->pm_class = PMC_CLASS_POWER8; 628b48a2770SLeandro Lupori return (0); 629b48a2770SLeandro Lupori } 630b48a2770SLeandro Lupori 63128dd6730SMitchell Horne #elif defined(__aarch64__) 63228dd6730SMitchell Horne 63345dcc17eSMitchell Horne static int 63445dcc17eSMitchell Horne pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm) 63528dd6730SMitchell Horne { 63628dd6730SMitchell Horne const struct pmu_event *pe; 63727ea55fcSMitchell Horne struct pmu_event_desc ped; 63828dd6730SMitchell Horne int idx = -1; 63928dd6730SMitchell Horne 64028dd6730SMitchell Horne event_name = pmu_alias_get(event_name); 64128dd6730SMitchell Horne if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 64228dd6730SMitchell Horne return (ENOENT); 64328dd6730SMitchell Horne if (pe->event == NULL) 64428dd6730SMitchell Horne return (ENOENT); 64527ea55fcSMitchell Horne if (pmu_parse_event(&ped, pe->event)) 64627ea55fcSMitchell Horne return (ENOENT); 64728dd6730SMitchell Horne 64828dd6730SMitchell Horne assert(idx >= 0); 64927ea55fcSMitchell Horne pm->pm_ev = idx; 65027ea55fcSMitchell Horne pm->pm_md.pm_md_config = ped.ped_event; 65128dd6730SMitchell Horne pm->pm_class = PMC_CLASS_ARMV8; 65228dd6730SMitchell Horne pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 65328dd6730SMitchell Horne 65428dd6730SMitchell Horne return (0); 65528dd6730SMitchell Horne } 65628dd6730SMitchell Horne 657959826caSMatt Macy #else 6588b20f975SMatt Macy 65945dcc17eSMitchell Horne static int 66045dcc17eSMitchell Horne pmc_pmu_pmcallocate_md(const char *e __unused, struct pmc_op_pmcallocate *p __unused) 6618b20f975SMatt Macy { 6628b20f975SMatt Macy return (EOPNOTSUPP); 6638b20f975SMatt Macy } 664959826caSMatt Macy #endif 66545dcc17eSMitchell Horne 66645dcc17eSMitchell Horne int 66745dcc17eSMitchell Horne pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 66845dcc17eSMitchell Horne { 66945dcc17eSMitchell Horne int error; 67045dcc17eSMitchell Horne 67145dcc17eSMitchell Horne error = pmc_pmu_pmcallocate_md(event_name, pm); 67245dcc17eSMitchell Horne if (error != 0) { 67345dcc17eSMitchell Horne /* Reset any changes. */ 67445dcc17eSMitchell Horne pm->pm_ev = 0; 67545dcc17eSMitchell Horne pm->pm_caps = 0; 67645dcc17eSMitchell Horne pm->pm_class = 0; 67745dcc17eSMitchell Horne 67845dcc17eSMitchell Horne return (error); 67945dcc17eSMitchell Horne } 68045dcc17eSMitchell Horne 681*c190fb35SMitchell Horne pm->pm_flags |= PMC_F_EV_PMU; 68245dcc17eSMitchell Horne return (0); 68345dcc17eSMitchell Horne } 684