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[] = { 63e144cd92SAlexander Motin {"UNHALTED_CORE_CYCLES", "cpu_clk_unhalted.thread"}, 64e144cd92SAlexander Motin {"UNHALTED-CORE-CYCLES", "cpu_clk_unhalted.thread"}, 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"}, 77e144cd92SAlexander Motin {"unhalted-cycles", "cpu_clk_unhalted.thread"}, 78e144cd92SAlexander Motin {"instructions", "inst_retired.any"}, 7996cbd26aSMatt Macy {"branch-mispredicts", "br_misp_retired.all_branches"}, 8096cbd26aSMatt Macy {"branches", "br_inst_retired.all_branches"}, 8196cbd26aSMatt Macy {"interrupts", "hw_interrupts.received"}, 8296cbd26aSMatt Macy {"ic-misses", "frontend_retired.l1i_miss"}, 83959826caSMatt Macy {NULL, NULL}, 84959826caSMatt Macy }; 85959826caSMatt Macy 8681eb4dcfSMatt Macy static struct pmu_alias pmu_amd_alias_table[] = { 8781eb4dcfSMatt Macy {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"}, 8881eb4dcfSMatt Macy {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"}, 89a20c1089SMitchell Horne {"LLC_MISSES", "l3_comb_clstr_state.request_miss"}, 90a20c1089SMitchell Horne {"LLC-MISSES", "l3_comb_clstr_state.request_miss"}, 91a20c1089SMitchell Horne {"LLC_REFERENCE", "l3_request_g1.caching_l3_cache_accesses"}, 92a20c1089SMitchell Horne {"LLC-REFERENCE", "l3_request_g1.caching_l3_cache_accesses"}, 93a20c1089SMitchell Horne {"BRANCH_INSTRUCTION_RETIRED", "ex_ret_brn"}, 94a20c1089SMitchell Horne {"BRANCH-INSTRUCTION-RETIRED", "ex_ret_brn"}, 95a20c1089SMitchell Horne {"BRANCH_MISSES_RETIRED", "ex_ret_brn_misp"}, 96a20c1089SMitchell Horne {"BRANCH-MISSES-RETIRED", "ex_ret_brn_misp"}, 97a20c1089SMitchell Horne {"unhalted-cycles", "ls_not_halted_cyc"}, 98a20c1089SMitchell Horne {"instructions", "ex_ret_instr",}, 99a20c1089SMitchell Horne {"branch-mispredicts", "ex_ret_brn_misp"}, 100a20c1089SMitchell Horne {"branches", "ex_ret_brn"}, 101a20c1089SMitchell Horne {"interrupts", "ls_int_taken"}, /* Not on amdzen1 */ 10281eb4dcfSMatt Macy {NULL, NULL}, 10381eb4dcfSMatt Macy }; 10481eb4dcfSMatt Macy 10581eb4dcfSMatt Macy 10681eb4dcfSMatt Macy static pmu_mfr_t 10781eb4dcfSMatt Macy pmu_events_mfr(void) 10881eb4dcfSMatt Macy { 10924e337beSRyan Moeller char buf[PMC_CPUID_LEN]; 11024e337beSRyan Moeller size_t s = sizeof(buf); 11181eb4dcfSMatt Macy pmu_mfr_t mfr; 11281eb4dcfSMatt Macy 11324e337beSRyan Moeller if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 11481eb4dcfSMatt Macy (void *)NULL, 0) == -1) 11581eb4dcfSMatt Macy return (PMU_INVALID); 11653071ed1SKonstantin Belousov if (strcasestr(buf, "AuthenticAMD") != NULL || 11753071ed1SKonstantin Belousov strcasestr(buf, "HygonGenuine") != NULL) 11881eb4dcfSMatt Macy mfr = PMU_AMD; 11981eb4dcfSMatt Macy else if (strcasestr(buf, "GenuineIntel") != NULL) 12081eb4dcfSMatt Macy mfr = PMU_INTEL; 12181eb4dcfSMatt Macy else 12281eb4dcfSMatt Macy mfr = PMU_INVALID; 12381eb4dcfSMatt Macy return (mfr); 12481eb4dcfSMatt Macy } 12581eb4dcfSMatt Macy 12607d80fd8SMatt Macy /* 12707d80fd8SMatt Macy * The Intel fixed mode counters are: 12807d80fd8SMatt Macy * "inst_retired.any", 12907d80fd8SMatt Macy * "cpu_clk_unhalted.thread", 13007d80fd8SMatt Macy * "cpu_clk_unhalted.thread_any", 13107d80fd8SMatt Macy * "cpu_clk_unhalted.ref_tsc", 13207d80fd8SMatt Macy * 13307d80fd8SMatt Macy */ 134a191ed2dSMatt Macy 135959826caSMatt Macy static const char * 136959826caSMatt Macy pmu_alias_get(const char *name) 137959826caSMatt Macy { 13881eb4dcfSMatt Macy pmu_mfr_t mfr; 139959826caSMatt Macy struct pmu_alias *pa; 14081eb4dcfSMatt Macy struct pmu_alias *pmu_alias_table; 14181eb4dcfSMatt Macy 14281eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 14381eb4dcfSMatt Macy return (name); 14481eb4dcfSMatt Macy if (mfr == PMU_AMD) 14581eb4dcfSMatt Macy pmu_alias_table = pmu_amd_alias_table; 14681eb4dcfSMatt Macy else if (mfr == PMU_INTEL) 14781eb4dcfSMatt Macy pmu_alias_table = pmu_intel_alias_table; 14881eb4dcfSMatt Macy else 14981eb4dcfSMatt Macy return (name); 150959826caSMatt Macy 151959826caSMatt Macy for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++) 152959826caSMatt Macy if (strcasecmp(name, pa->pa_alias) == 0) 153959826caSMatt Macy return (pa->pa_name); 15481eb4dcfSMatt Macy 155959826caSMatt Macy return (name); 156959826caSMatt Macy } 157b48a2770SLeandro Lupori #elif defined(__powerpc64__) 158b48a2770SLeandro Lupori 159b48a2770SLeandro Lupori static const char * 160b48a2770SLeandro Lupori pmu_alias_get(const char *name) 161b48a2770SLeandro Lupori { 162b48a2770SLeandro Lupori return (name); 163b48a2770SLeandro Lupori } 164959826caSMatt Macy 16528dd6730SMitchell Horne #elif defined(__aarch64__) 16628dd6730SMitchell Horne 16728dd6730SMitchell Horne static struct pmu_alias pmu_armv8_alias_table[] = { 168*6f50b73eSMitchell Horne {"UNHALTED_CORE_CYCLES", "CPU_CYCLES"}, 169*6f50b73eSMitchell Horne {"UNHALTED-CORE-CYCLES", "CPU_CYCLES"}, 170*6f50b73eSMitchell Horne {"LLC_MISSES", "LL_CACHE_MISS_RD"}, 171*6f50b73eSMitchell Horne {"LLC-MISSES", "LL_CACHE_MISS_RD"}, 172*6f50b73eSMitchell Horne {"LLC_REFERENCE", "LL_CACHE_RD"}, 173*6f50b73eSMitchell Horne {"LLC-REFERENCE", "LL_CACHE_RD"}, 174*6f50b73eSMitchell Horne {"BRANCH_INSTRUCTION_RETIRED", "BR_RETIRED"}, 175*6f50b73eSMitchell Horne {"BRANCH-INSTRUCTION-RETIRED", "BR_RETIRED"}, 176*6f50b73eSMitchell Horne {"BRANCH_MISSES_RETIRED", "BR_MIS_PRED_RETIRED"}, 177*6f50b73eSMitchell Horne {"BRANCH-MISSES-RETIRED", "BR_MIS_PRED_RETIRED"}, 178*6f50b73eSMitchell Horne {"unhalted-cycles", "CPU_CYCLES"}, 179*6f50b73eSMitchell Horne {"instructions", "INST_RETIRED",}, 180*6f50b73eSMitchell Horne {"branch-mispredicts", "BR_MIS_PRED_RETIRED"}, 181*6f50b73eSMitchell Horne {"branches", "BR_RETIRED"}, 182*6f50b73eSMitchell Horne {"interrupts", "EXC_IRQ"}, 18328dd6730SMitchell Horne {NULL, NULL}, 18428dd6730SMitchell Horne }; 18528dd6730SMitchell Horne 18628dd6730SMitchell Horne static const char * 18728dd6730SMitchell Horne pmu_alias_get(const char *name) 18828dd6730SMitchell Horne { 18928dd6730SMitchell Horne struct pmu_alias *pa; 19028dd6730SMitchell Horne 19128dd6730SMitchell Horne for (pa = pmu_armv8_alias_table; pa->pa_alias != NULL; pa++) 19228dd6730SMitchell Horne if (strcasecmp(name, pa->pa_alias) == 0) 19328dd6730SMitchell Horne return (pa->pa_name); 19428dd6730SMitchell Horne 19528dd6730SMitchell Horne return (name); 19628dd6730SMitchell Horne } 19728dd6730SMitchell Horne 1980024f1aaSMitchell Horne #else 1990024f1aaSMitchell Horne 2000024f1aaSMitchell Horne static const char * 2010024f1aaSMitchell Horne pmu_alias_get(const char *name) 2020024f1aaSMitchell Horne { 2030024f1aaSMitchell Horne 2040024f1aaSMitchell Horne return (name); 2050024f1aaSMitchell Horne } 2060024f1aaSMitchell Horne #endif 2070024f1aaSMitchell Horne 208959826caSMatt Macy struct pmu_event_desc { 209959826caSMatt Macy uint64_t ped_period; 210959826caSMatt Macy uint64_t ped_offcore_rsp; 211dacc43dfSMatt Macy uint64_t ped_l3_thread; 212dacc43dfSMatt Macy uint64_t ped_l3_slice; 213959826caSMatt Macy uint32_t ped_event; 214959826caSMatt Macy uint32_t ped_frontend; 215959826caSMatt Macy uint32_t ped_ldlat; 216959826caSMatt Macy uint32_t ped_config1; 21707d80fd8SMatt Macy int16_t ped_umask; 218959826caSMatt Macy uint8_t ped_cmask; 219959826caSMatt Macy uint8_t ped_any; 220959826caSMatt Macy uint8_t ped_inv; 221959826caSMatt Macy uint8_t ped_edge; 222959826caSMatt Macy uint8_t ped_fc_mask; 223959826caSMatt Macy uint8_t ped_ch_mask; 224959826caSMatt Macy }; 225959826caSMatt Macy 226959826caSMatt Macy static const struct pmu_events_map * 227bfb46e2bSMatt Macy pmu_events_map_get(const char *cpuid) 228959826caSMatt Macy { 229a0ac5706SEmmanuel Vadot regex_t re; 230a0ac5706SEmmanuel Vadot regmatch_t pmatch[1]; 23124e337beSRyan Moeller char buf[PMC_CPUID_LEN]; 23224e337beSRyan Moeller size_t s = sizeof(buf); 233a0ac5706SEmmanuel Vadot int match; 234959826caSMatt Macy const struct pmu_events_map *pme; 235959826caSMatt Macy 236bfb46e2bSMatt Macy if (cpuid != NULL) { 23724e337beSRyan Moeller strlcpy(buf, cpuid, s); 238bfb46e2bSMatt Macy } else { 239959826caSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 240959826caSMatt Macy (void *)NULL, 0) == -1) 241959826caSMatt Macy return (NULL); 242bfb46e2bSMatt Macy } 243a0ac5706SEmmanuel Vadot for (pme = pmu_events_map; pme->cpuid != NULL; pme++) { 244a0ac5706SEmmanuel Vadot if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) { 245a0ac5706SEmmanuel Vadot printf("regex '%s' failed to compile, ignoring\n", 246a0ac5706SEmmanuel Vadot pme->cpuid); 247a0ac5706SEmmanuel Vadot continue; 248a0ac5706SEmmanuel Vadot } 249a0ac5706SEmmanuel Vadot match = regexec(&re, buf, 1, pmatch, 0); 250a0ac5706SEmmanuel Vadot regfree(&re); 251a0ac5706SEmmanuel Vadot if (match == 0) { 2521791cad0SAlexander Motin if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0 2531791cad0SAlexander Motin || buf[pmatch[0].rm_eo] == '-')) 254959826caSMatt Macy return (pme); 255a0ac5706SEmmanuel Vadot } 256a0ac5706SEmmanuel Vadot } 257959826caSMatt Macy return (NULL); 258959826caSMatt Macy } 259959826caSMatt Macy 260959826caSMatt Macy static const struct pmu_event * 261bfb46e2bSMatt Macy pmu_event_get(const char *cpuid, const char *event_name, int *idx) 262959826caSMatt Macy { 263959826caSMatt Macy const struct pmu_events_map *pme; 264959826caSMatt Macy const struct pmu_event *pe; 265959826caSMatt Macy int i; 266959826caSMatt Macy 267bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 268959826caSMatt Macy return (NULL); 269959826caSMatt Macy for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) { 270959826caSMatt Macy if (pe->name == NULL) 271959826caSMatt Macy continue; 272959826caSMatt Macy if (strcasecmp(pe->name, event_name) == 0) { 273959826caSMatt Macy if (idx) 274959826caSMatt Macy *idx = i; 275959826caSMatt Macy return (pe); 276959826caSMatt Macy } 277959826caSMatt Macy } 278959826caSMatt Macy return (NULL); 279959826caSMatt Macy } 280959826caSMatt Macy 281bfb46e2bSMatt Macy int 282bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *cpuid, const char *event) 283bfb46e2bSMatt Macy { 284bfb46e2bSMatt Macy int idx; 285bfb46e2bSMatt Macy const char *realname; 286bfb46e2bSMatt Macy 287bfb46e2bSMatt Macy realname = pmu_alias_get(event); 288bfb46e2bSMatt Macy if (pmu_event_get(cpuid, realname, &idx) == NULL) 289bfb46e2bSMatt Macy return (-1); 290bfb46e2bSMatt Macy return (idx); 291bfb46e2bSMatt Macy } 292bfb46e2bSMatt Macy 293959826caSMatt Macy const char * 294b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *cpuid, int idx) 295959826caSMatt Macy { 296959826caSMatt Macy const struct pmu_events_map *pme; 297959826caSMatt Macy 298b2ca2e50SMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 299959826caSMatt Macy return (NULL); 3001b000b50SMatt Macy assert(pme->table[idx].name); 3011b000b50SMatt Macy return (pme->table[idx].name); 302959826caSMatt Macy } 303959826caSMatt Macy 304959826caSMatt Macy static int 305959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin) 306959826caSMatt Macy { 307959826caSMatt Macy char *event; 3088bed125dSMatt Macy char *kvp, *key, *value, *r; 309959826caSMatt Macy char *debug; 310959826caSMatt Macy 311959826caSMatt Macy if ((event = strdup(eventin)) == NULL) 312959826caSMatt Macy return (ENOMEM); 3138bed125dSMatt Macy r = event; 314959826caSMatt Macy bzero(ped, sizeof(*ped)); 3150204d85aSMatt Macy ped->ped_period = DEFAULT_SAMPLE_COUNT; 31607d80fd8SMatt Macy ped->ped_umask = -1; 317959826caSMatt Macy while ((kvp = strsep(&event, ",")) != NULL) { 318959826caSMatt Macy key = strsep(&kvp, "="); 319959826caSMatt Macy if (key == NULL) 320959826caSMatt Macy abort(); 321959826caSMatt Macy value = kvp; 322959826caSMatt Macy if (strcmp(key, "umask") == 0) 323959826caSMatt Macy ped->ped_umask = strtol(value, NULL, 16); 324959826caSMatt Macy else if (strcmp(key, "event") == 0) 325959826caSMatt Macy ped->ped_event = strtol(value, NULL, 16); 326959826caSMatt Macy else if (strcmp(key, "period") == 0) 327959826caSMatt Macy ped->ped_period = strtol(value, NULL, 10); 328959826caSMatt Macy else if (strcmp(key, "offcore_rsp") == 0) 329959826caSMatt Macy ped->ped_offcore_rsp = strtol(value, NULL, 16); 330959826caSMatt Macy else if (strcmp(key, "any") == 0) 331959826caSMatt Macy ped->ped_any = strtol(value, NULL, 10); 332959826caSMatt Macy else if (strcmp(key, "cmask") == 0) 333959826caSMatt Macy ped->ped_cmask = strtol(value, NULL, 10); 334959826caSMatt Macy else if (strcmp(key, "inv") == 0) 335959826caSMatt Macy ped->ped_inv = strtol(value, NULL, 10); 336959826caSMatt Macy else if (strcmp(key, "edge") == 0) 337959826caSMatt Macy ped->ped_edge = strtol(value, NULL, 10); 338959826caSMatt Macy else if (strcmp(key, "frontend") == 0) 339959826caSMatt Macy ped->ped_frontend = strtol(value, NULL, 16); 340959826caSMatt Macy else if (strcmp(key, "ldlat") == 0) 341959826caSMatt Macy ped->ped_ldlat = strtol(value, NULL, 16); 342959826caSMatt Macy else if (strcmp(key, "fc_mask") == 0) 343959826caSMatt Macy ped->ped_fc_mask = strtol(value, NULL, 16); 344959826caSMatt Macy else if (strcmp(key, "ch_mask") == 0) 345959826caSMatt Macy ped->ped_ch_mask = strtol(value, NULL, 16); 346959826caSMatt Macy else if (strcmp(key, "config1") == 0) 347959826caSMatt Macy ped->ped_config1 = strtol(value, NULL, 16); 348dacc43dfSMatt Macy else if (strcmp(key, "l3_thread_mask") == 0) 349dacc43dfSMatt Macy ped->ped_l3_thread = strtol(value, NULL, 16); 350dacc43dfSMatt Macy else if (strcmp(key, "l3_slice_mask") == 0) 351dacc43dfSMatt Macy ped->ped_l3_slice = strtol(value, NULL, 16); 352959826caSMatt Macy else { 353959826caSMatt Macy debug = getenv("PMUDEBUG"); 354959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL) 355959826caSMatt Macy printf("unrecognized kvpair: %s:%s\n", key, value); 356959826caSMatt Macy } 357959826caSMatt Macy } 3588bed125dSMatt Macy free(r); 359959826caSMatt Macy return (0); 360959826caSMatt Macy } 361959826caSMatt Macy 362959826caSMatt Macy uint64_t 363959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name) 364959826caSMatt Macy { 365959826caSMatt Macy const struct pmu_event *pe; 366959826caSMatt Macy struct pmu_event_desc ped; 367959826caSMatt Macy 368959826caSMatt Macy event_name = pmu_alias_get(event_name); 369bfb46e2bSMatt Macy if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL) 370959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 371959826caSMatt Macy if (pe->event == NULL) 372959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 373959826caSMatt Macy if (pmu_parse_event(&ped, pe->event)) 374959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 375959826caSMatt Macy return (ped.ped_period); 376959826caSMatt Macy } 377959826caSMatt Macy 378959826caSMatt Macy int 379959826caSMatt Macy pmc_pmu_enabled(void) 380959826caSMatt Macy { 381959826caSMatt Macy 382bfb46e2bSMatt Macy return (pmu_events_map_get(NULL) != NULL); 383959826caSMatt Macy } 384959826caSMatt Macy 385959826caSMatt Macy void 386fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name) 387959826caSMatt Macy { 388959826caSMatt Macy const struct pmu_events_map *pme; 389959826caSMatt Macy const struct pmu_event *pe; 390959826caSMatt Macy struct pmu_event_desc ped; 391959826caSMatt Macy char *debug; 392959826caSMatt Macy int do_debug; 393959826caSMatt Macy 394959826caSMatt Macy debug = getenv("PMUDEBUG"); 395959826caSMatt Macy do_debug = 0; 396959826caSMatt Macy 397959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0) 398959826caSMatt Macy do_debug = 1; 399bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 400959826caSMatt Macy return; 401959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 402959826caSMatt Macy if (pe->name == NULL) 403959826caSMatt Macy continue; 404fbf962e6SMatt Macy if (event_name != NULL && strcasestr(pe->name, event_name) == NULL) 405fbf962e6SMatt Macy continue; 406959826caSMatt Macy printf("\t%s\n", pe->name); 407959826caSMatt Macy if (do_debug) 408959826caSMatt Macy pmu_parse_event(&ped, pe->event); 409959826caSMatt Macy } 410959826caSMatt Macy } 411959826caSMatt Macy 412959826caSMatt Macy void 413959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev) 414959826caSMatt Macy { 415959826caSMatt Macy const struct pmu_events_map *pme; 416959826caSMatt Macy const struct pmu_event *pe; 417959826caSMatt Macy 418bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 419959826caSMatt Macy return; 420959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 421959826caSMatt Macy if (pe->name == NULL) 422959826caSMatt Macy continue; 423959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL && 424959826caSMatt Macy pe->desc != NULL) 425959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 426959826caSMatt Macy } 427959826caSMatt Macy } 428959826caSMatt Macy 429959826caSMatt Macy void 430959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev) 431959826caSMatt Macy { 432959826caSMatt Macy const struct pmu_events_map *pme; 433959826caSMatt Macy const struct pmu_event *pe; 434959826caSMatt Macy 435bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 436959826caSMatt Macy return; 437959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 438959826caSMatt Macy if (pe->name == NULL) 439959826caSMatt Macy continue; 440959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL) { 441959826caSMatt Macy if (pe->long_desc != NULL) 442959826caSMatt Macy printf("%s:\n%s\n", pe->name, pe->long_desc); 443959826caSMatt Macy else if (pe->desc != NULL) 444959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 445959826caSMatt Macy } 446959826caSMatt Macy } 447959826caSMatt Macy } 448959826caSMatt Macy 449fbf962e6SMatt Macy void 450fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev) 451fbf962e6SMatt Macy { 452fbf962e6SMatt Macy const struct pmu_events_map *pme; 453fbf962e6SMatt Macy const struct pmu_event *pe; 454fbf962e6SMatt Macy 455bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 456fbf962e6SMatt Macy return; 457fbf962e6SMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 458fbf962e6SMatt Macy if (pe->name == NULL) 459fbf962e6SMatt Macy continue; 460fbf962e6SMatt Macy if (strcasestr(pe->name, ev) == NULL) 461fbf962e6SMatt Macy continue; 462fbf962e6SMatt Macy printf("name: %s\n", pe->name); 463fbf962e6SMatt Macy if (pe->long_desc != NULL) 464fbf962e6SMatt Macy printf("desc: %s\n", pe->long_desc); 465fbf962e6SMatt Macy else if (pe->desc != NULL) 466fbf962e6SMatt Macy printf("desc: %s\n", pe->desc); 467fbf962e6SMatt Macy if (pe->event != NULL) 468fbf962e6SMatt Macy printf("event: %s\n", pe->event); 469fbf962e6SMatt Macy if (pe->topic != NULL) 470fbf962e6SMatt Macy printf("topic: %s\n", pe->topic); 471fbf962e6SMatt Macy if (pe->pmu != NULL) 472fbf962e6SMatt Macy printf("pmu: %s\n", pe->pmu); 473fbf962e6SMatt Macy if (pe->unit != NULL) 474fbf962e6SMatt Macy printf("unit: %s\n", pe->unit); 475fbf962e6SMatt Macy if (pe->perpkg != NULL) 476fbf962e6SMatt Macy printf("perpkg: %s\n", pe->perpkg); 477fbf962e6SMatt Macy if (pe->metric_expr != NULL) 478fbf962e6SMatt Macy printf("metric_expr: %s\n", pe->metric_expr); 479fbf962e6SMatt Macy if (pe->metric_name != NULL) 480fbf962e6SMatt Macy printf("metric_name: %s\n", pe->metric_name); 481fbf962e6SMatt Macy if (pe->metric_group != NULL) 482fbf962e6SMatt Macy printf("metric_group: %s\n", pe->metric_group); 483fbf962e6SMatt Macy } 484fbf962e6SMatt Macy } 485fbf962e6SMatt Macy 4860024f1aaSMitchell Horne #if defined(__amd64__) || defined(__i386__) 48781eb4dcfSMatt Macy static int 488dacc43dfSMatt Macy pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 48981eb4dcfSMatt Macy struct pmu_event_desc *ped) 490959826caSMatt Macy { 49181eb4dcfSMatt Macy struct pmc_md_amd_op_pmcallocate *amd; 492dacc43dfSMatt Macy const struct pmu_event *pe; 493dacc43dfSMatt Macy int idx = -1; 49481eb4dcfSMatt Macy 49581eb4dcfSMatt Macy amd = &pm->pm_md.pm_amd; 49681eb4dcfSMatt Macy if (ped->ped_umask > 0) { 49781eb4dcfSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 49881eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask); 49981eb4dcfSMatt Macy } 50081eb4dcfSMatt Macy pm->pm_class = PMC_CLASS_K8; 501dacc43dfSMatt Macy pe = pmu_event_get(NULL, event_name, &idx); 50281eb4dcfSMatt Macy 503dacc43dfSMatt Macy if (strcmp("l3cache", pe->topic) == 0){ 504dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 505dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE; 506dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice); 507dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread); 508dacc43dfSMatt Macy } 509dacc43dfSMatt Macy else if (strcmp("data fabric", pe->topic) == 0){ 510dacc43dfSMatt Macy 511dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event); 512dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC; 513dacc43dfSMatt Macy } 514dacc43dfSMatt Macy else{ 515dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 516dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE; 51781eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 51881eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 51981eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 52081eb4dcfSMatt Macy amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS); 52181eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 52281eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_USR; 52381eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 52481eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_OS; 52581eb4dcfSMatt Macy if (ped->ped_edge) 52681eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 52781eb4dcfSMatt Macy if (ped->ped_inv) 528037dd0a9SAlexander Motin amd->pm_amd_config |= AMD_PMC_INVERT; 52981eb4dcfSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 53081eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_INT; 531dacc43dfSMatt Macy } 53281eb4dcfSMatt Macy return (0); 53381eb4dcfSMatt Macy } 53481eb4dcfSMatt Macy 53581eb4dcfSMatt Macy static int 53681eb4dcfSMatt Macy pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 53781eb4dcfSMatt Macy struct pmu_event_desc *ped) 53881eb4dcfSMatt Macy { 539959826caSMatt Macy struct pmc_md_iap_op_pmcallocate *iap; 540959826caSMatt Macy 54181eb4dcfSMatt Macy iap = &pm->pm_md.pm_iap; 54207d80fd8SMatt Macy if (strcasestr(event_name, "UNC_") == event_name || 543785dd70dSMatt Macy strcasestr(event_name, "uncore") != NULL) { 544785dd70dSMatt Macy pm->pm_class = PMC_CLASS_UCP; 545a191ed2dSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 54673b7b181SAlexander Motin } else if (ped->ped_event == 0x0) { 54707d80fd8SMatt Macy pm->pm_class = PMC_CLASS_IAF; 54807d80fd8SMatt Macy } else { 549959826caSMatt Macy pm->pm_class = PMC_CLASS_IAP; 55007d80fd8SMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 551785dd70dSMatt Macy } 55281eb4dcfSMatt Macy iap->pm_iap_config |= IAP_EVSEL(ped->ped_event); 55381eb4dcfSMatt Macy if (ped->ped_umask > 0) 55481eb4dcfSMatt Macy iap->pm_iap_config |= IAP_UMASK(ped->ped_umask); 55581eb4dcfSMatt Macy iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask); 55681eb4dcfSMatt Macy iap->pm_iap_rsp = ped->ped_offcore_rsp; 557959826caSMatt Macy 55881eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 55981eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 56081eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 561959826caSMatt Macy iap->pm_iap_config |= (IAP_USR | IAP_OS); 56281eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 56381eb4dcfSMatt Macy iap->pm_iap_config |= IAP_USR; 56481eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 56581eb4dcfSMatt Macy iap->pm_iap_config |= IAP_OS; 56681eb4dcfSMatt Macy if (ped->ped_edge) 567959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 56881eb4dcfSMatt Macy if (ped->ped_any) 569959826caSMatt Macy iap->pm_iap_config |= IAP_ANY; 57081eb4dcfSMatt Macy if (ped->ped_inv) 571037dd0a9SAlexander Motin iap->pm_iap_config |= IAP_INV; 572959826caSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 573959826caSMatt Macy iap->pm_iap_config |= IAP_INT; 574959826caSMatt Macy return (0); 575959826caSMatt Macy } 576959826caSMatt Macy 57781eb4dcfSMatt Macy int 57881eb4dcfSMatt Macy pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 57981eb4dcfSMatt Macy { 58081eb4dcfSMatt Macy const struct pmu_event *pe; 58181eb4dcfSMatt Macy struct pmu_event_desc ped; 58281eb4dcfSMatt Macy pmu_mfr_t mfr; 58381eb4dcfSMatt Macy int idx = -1; 58481eb4dcfSMatt Macy 58581eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 58681eb4dcfSMatt Macy return (ENOENT); 58781eb4dcfSMatt Macy 58881eb4dcfSMatt Macy bzero(&pm->pm_md, sizeof(pm->pm_md)); 58981eb4dcfSMatt Macy pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 59081eb4dcfSMatt Macy event_name = pmu_alias_get(event_name); 59181eb4dcfSMatt Macy if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 59281eb4dcfSMatt Macy return (ENOENT); 59381eb4dcfSMatt Macy assert(idx >= 0); 59481eb4dcfSMatt Macy pm->pm_ev = idx; 59581eb4dcfSMatt Macy 59681eb4dcfSMatt Macy if (pe->event == NULL) 59781eb4dcfSMatt Macy return (ENOENT); 59881eb4dcfSMatt Macy if (pmu_parse_event(&ped, pe->event)) 59981eb4dcfSMatt Macy return (ENOENT); 60081eb4dcfSMatt Macy 60181eb4dcfSMatt Macy if (mfr == PMU_INTEL) 60281eb4dcfSMatt Macy return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped)); 60381eb4dcfSMatt Macy else 60481eb4dcfSMatt Macy return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped)); 60581eb4dcfSMatt Macy } 60681eb4dcfSMatt Macy 607b48a2770SLeandro Lupori #elif defined(__powerpc64__) 608b48a2770SLeandro Lupori 609b48a2770SLeandro Lupori int 610b48a2770SLeandro Lupori pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 611b48a2770SLeandro Lupori { 612b48a2770SLeandro Lupori const struct pmu_event *pe; 613b48a2770SLeandro Lupori struct pmu_event_desc ped; 614b48a2770SLeandro Lupori int idx = -1; 615b48a2770SLeandro Lupori 616b48a2770SLeandro Lupori bzero(&pm->pm_md, sizeof(pm->pm_md)); 617b48a2770SLeandro Lupori pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 618b48a2770SLeandro Lupori event_name = pmu_alias_get(event_name); 619b48a2770SLeandro Lupori 620b48a2770SLeandro Lupori if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 621b48a2770SLeandro Lupori return (ENOENT); 622b48a2770SLeandro Lupori if (pe->event == NULL) 623b48a2770SLeandro Lupori return (ENOENT); 624b48a2770SLeandro Lupori if (pmu_parse_event(&ped, pe->event)) 625b48a2770SLeandro Lupori return (ENOENT); 626b48a2770SLeandro Lupori 627b48a2770SLeandro Lupori assert(ped.ped_event >= 0); 628b48a2770SLeandro Lupori pm->pm_ev = idx; 629b48a2770SLeandro Lupori pm->pm_md.pm_event = ped.ped_event; 630b48a2770SLeandro Lupori pm->pm_class = PMC_CLASS_POWER8; 631b48a2770SLeandro Lupori return (0); 632b48a2770SLeandro Lupori } 633b48a2770SLeandro Lupori 63428dd6730SMitchell Horne #elif defined(__aarch64__) 63528dd6730SMitchell Horne 63628dd6730SMitchell Horne int 63728dd6730SMitchell Horne pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 63828dd6730SMitchell Horne { 63928dd6730SMitchell Horne const struct pmu_event *pe; 64027ea55fcSMitchell Horne struct pmu_event_desc ped; 64128dd6730SMitchell Horne int idx = -1; 64228dd6730SMitchell Horne 64328dd6730SMitchell Horne event_name = pmu_alias_get(event_name); 64428dd6730SMitchell Horne if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 64528dd6730SMitchell Horne return (ENOENT); 64628dd6730SMitchell Horne if (pe->event == NULL) 64728dd6730SMitchell Horne return (ENOENT); 64827ea55fcSMitchell Horne if (pmu_parse_event(&ped, pe->event)) 64927ea55fcSMitchell Horne return (ENOENT); 65028dd6730SMitchell Horne 65128dd6730SMitchell Horne assert(idx >= 0); 65227ea55fcSMitchell Horne pm->pm_ev = idx; 65327ea55fcSMitchell Horne pm->pm_md.pm_md_config = ped.ped_event; 65428dd6730SMitchell Horne pm->pm_md.pm_md_flags |= PM_MD_RAW_EVENT; 65528dd6730SMitchell Horne pm->pm_class = PMC_CLASS_ARMV8; 65628dd6730SMitchell Horne pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 65728dd6730SMitchell Horne 65828dd6730SMitchell Horne return (0); 65928dd6730SMitchell Horne } 66028dd6730SMitchell Horne 661959826caSMatt Macy #else 6628b20f975SMatt Macy 6638b20f975SMatt Macy int 6648b20f975SMatt Macy pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused) 6658b20f975SMatt Macy { 6668b20f975SMatt Macy return (EOPNOTSUPP); 6678b20f975SMatt Macy } 668959826caSMatt Macy #endif 669