1959826caSMatt Macy /*- 2959826caSMatt Macy * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3959826caSMatt Macy * 4959826caSMatt Macy * Copyright (c) 2018, Matthew Macy 5959826caSMatt Macy * 6959826caSMatt Macy * Redistribution and use in source and binary forms, with or without 7959826caSMatt Macy * modification, are permitted provided that the following conditions 8959826caSMatt Macy * are met: 9959826caSMatt Macy * 1. Redistributions of source code must retain the above copyright 10959826caSMatt Macy * notice, this list of conditions and the following disclaimer. 11959826caSMatt Macy * 2. Redistributions in binary form must reproduce the above copyright 12959826caSMatt Macy * notice, this list of conditions and the following disclaimer in the 13959826caSMatt Macy * documentation and/or other materials provided with the distribution. 14959826caSMatt Macy * 15959826caSMatt Macy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16959826caSMatt Macy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17959826caSMatt Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18959826caSMatt Macy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19959826caSMatt Macy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20959826caSMatt Macy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21959826caSMatt Macy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22959826caSMatt Macy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23959826caSMatt Macy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24959826caSMatt Macy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25959826caSMatt Macy * SUCH DAMAGE. 26959826caSMatt Macy * 27959826caSMatt Macy * $FreeBSD$ 28959826caSMatt Macy * 29959826caSMatt Macy */ 30959826caSMatt Macy 31959826caSMatt Macy #include <sys/types.h> 32959826caSMatt Macy #include <sys/errno.h> 33959826caSMatt Macy #include <sys/sysctl.h> 34959826caSMatt Macy #include <stddef.h> 35959826caSMatt Macy #include <stdlib.h> 36959826caSMatt Macy #include <limits.h> 37*f3dbece8SEmmanuel Vadot #include <regex.h> 38959826caSMatt Macy #include <string.h> 39959826caSMatt Macy #include <pmc.h> 40959826caSMatt Macy #include <pmclog.h> 411b000b50SMatt Macy #include <assert.h> 42959826caSMatt Macy #include <libpmcstat.h> 43959826caSMatt Macy #include "pmu-events/pmu-events.h" 44959826caSMatt Macy 457d1c2b74SMatt Macy #if defined(__amd64__) || defined(__i386__) 46959826caSMatt Macy struct pmu_alias { 47959826caSMatt Macy const char *pa_alias; 48959826caSMatt Macy const char *pa_name; 49959826caSMatt Macy }; 5081eb4dcfSMatt Macy 5181eb4dcfSMatt Macy typedef enum { 5281eb4dcfSMatt Macy PMU_INVALID, 5381eb4dcfSMatt Macy PMU_INTEL, 5481eb4dcfSMatt Macy PMU_AMD, 5581eb4dcfSMatt Macy } pmu_mfr_t; 5681eb4dcfSMatt Macy 5781eb4dcfSMatt Macy static struct pmu_alias pmu_intel_alias_table[] = { 58959826caSMatt Macy {"UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 59959826caSMatt Macy {"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 60959826caSMatt Macy {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"}, 61959826caSMatt Macy {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"}, 62959826caSMatt Macy {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"}, 63959826caSMatt Macy {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"}, 64959826caSMatt Macy {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"}, 65959826caSMatt Macy {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"}, 66959826caSMatt Macy {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"}, 67959826caSMatt Macy {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"}, 68959826caSMatt Macy {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"}, 69959826caSMatt Macy {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"}, 70959826caSMatt Macy {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"}, 71959826caSMatt Macy {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"}, 7296cbd26aSMatt Macy {"cycles", "tsc-tsc"}, 7386b5e013SMatt Macy {"unhalted-cycles", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 7496cbd26aSMatt Macy {"instructions", "inst-retired.any_p"}, 7596cbd26aSMatt Macy {"branch-mispredicts", "br_misp_retired.all_branches"}, 7696cbd26aSMatt Macy {"branches", "br_inst_retired.all_branches"}, 7796cbd26aSMatt Macy {"interrupts", "hw_interrupts.received"}, 7896cbd26aSMatt Macy {"ic-misses", "frontend_retired.l1i_miss"}, 79959826caSMatt Macy {NULL, NULL}, 80959826caSMatt Macy }; 81959826caSMatt Macy 8281eb4dcfSMatt Macy static struct pmu_alias pmu_amd_alias_table[] = { 8381eb4dcfSMatt Macy {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"}, 8481eb4dcfSMatt Macy {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"}, 8581eb4dcfSMatt Macy {NULL, NULL}, 8681eb4dcfSMatt Macy }; 8781eb4dcfSMatt Macy 8881eb4dcfSMatt Macy 8981eb4dcfSMatt Macy static pmu_mfr_t 9081eb4dcfSMatt Macy pmu_events_mfr(void) 9181eb4dcfSMatt Macy { 9281eb4dcfSMatt Macy char *buf; 9381eb4dcfSMatt Macy size_t s; 9481eb4dcfSMatt Macy pmu_mfr_t mfr; 9581eb4dcfSMatt Macy 9681eb4dcfSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s, 9781eb4dcfSMatt Macy (void *)NULL, 0) == -1) 9881eb4dcfSMatt Macy return (PMU_INVALID); 9981eb4dcfSMatt Macy if ((buf = malloc(s + 1)) == NULL) 10081eb4dcfSMatt Macy return (PMU_INVALID); 10181eb4dcfSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 10281eb4dcfSMatt Macy (void *)NULL, 0) == -1) { 10381eb4dcfSMatt Macy free(buf); 10481eb4dcfSMatt Macy return (PMU_INVALID); 10581eb4dcfSMatt Macy } 10681eb4dcfSMatt Macy if (strcasestr(buf, "AuthenticAMD") != NULL) 10781eb4dcfSMatt Macy mfr = PMU_AMD; 10881eb4dcfSMatt Macy else if (strcasestr(buf, "GenuineIntel") != NULL) 10981eb4dcfSMatt Macy mfr = PMU_INTEL; 11081eb4dcfSMatt Macy else 11181eb4dcfSMatt Macy mfr = PMU_INVALID; 11281eb4dcfSMatt Macy free(buf); 11381eb4dcfSMatt Macy return (mfr); 11481eb4dcfSMatt Macy } 11581eb4dcfSMatt Macy 11607d80fd8SMatt Macy /* 11707d80fd8SMatt Macy * The Intel fixed mode counters are: 11807d80fd8SMatt Macy * "inst_retired.any", 11907d80fd8SMatt Macy * "cpu_clk_unhalted.thread", 12007d80fd8SMatt Macy * "cpu_clk_unhalted.thread_any", 12107d80fd8SMatt Macy * "cpu_clk_unhalted.ref_tsc", 12207d80fd8SMatt Macy * 12307d80fd8SMatt Macy */ 124a191ed2dSMatt Macy 125959826caSMatt Macy static const char * 126959826caSMatt Macy pmu_alias_get(const char *name) 127959826caSMatt Macy { 12881eb4dcfSMatt Macy pmu_mfr_t mfr; 129959826caSMatt Macy struct pmu_alias *pa; 13081eb4dcfSMatt Macy struct pmu_alias *pmu_alias_table; 13181eb4dcfSMatt Macy 13281eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 13381eb4dcfSMatt Macy return (name); 13481eb4dcfSMatt Macy if (mfr == PMU_AMD) 13581eb4dcfSMatt Macy pmu_alias_table = pmu_amd_alias_table; 13681eb4dcfSMatt Macy else if (mfr == PMU_INTEL) 13781eb4dcfSMatt Macy pmu_alias_table = pmu_intel_alias_table; 13881eb4dcfSMatt Macy else 13981eb4dcfSMatt Macy return (name); 140959826caSMatt Macy 141959826caSMatt Macy for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++) 142959826caSMatt Macy if (strcasecmp(name, pa->pa_alias) == 0) 143959826caSMatt Macy return (pa->pa_name); 14481eb4dcfSMatt Macy 145959826caSMatt Macy return (name); 146959826caSMatt Macy } 147959826caSMatt Macy 148959826caSMatt Macy struct pmu_event_desc { 149959826caSMatt Macy uint64_t ped_period; 150959826caSMatt Macy uint64_t ped_offcore_rsp; 151dacc43dfSMatt Macy uint64_t ped_l3_thread; 152dacc43dfSMatt Macy uint64_t ped_l3_slice; 153959826caSMatt Macy uint32_t ped_event; 154959826caSMatt Macy uint32_t ped_frontend; 155959826caSMatt Macy uint32_t ped_ldlat; 156959826caSMatt Macy uint32_t ped_config1; 15707d80fd8SMatt Macy int16_t ped_umask; 158959826caSMatt Macy uint8_t ped_cmask; 159959826caSMatt Macy uint8_t ped_any; 160959826caSMatt Macy uint8_t ped_inv; 161959826caSMatt Macy uint8_t ped_edge; 162959826caSMatt Macy uint8_t ped_fc_mask; 163959826caSMatt Macy uint8_t ped_ch_mask; 164959826caSMatt Macy }; 165959826caSMatt Macy 166959826caSMatt Macy static const struct pmu_events_map * 167bfb46e2bSMatt Macy pmu_events_map_get(const char *cpuid) 168959826caSMatt Macy { 169a0ac5706SEmmanuel Vadot regex_t re; 170a0ac5706SEmmanuel Vadot regmatch_t pmatch[1]; 171a0ac5706SEmmanuel Vadot size_t s, len; 172959826caSMatt Macy char buf[64]; 173a0ac5706SEmmanuel Vadot int match; 174959826caSMatt Macy const struct pmu_events_map *pme; 175959826caSMatt Macy 176bfb46e2bSMatt Macy if (cpuid != NULL) { 177bfb46e2bSMatt Macy memcpy(buf, cpuid, 64); 178bfb46e2bSMatt Macy } else { 179959826caSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s, 180959826caSMatt Macy (void *)NULL, 0) == -1) 181959826caSMatt Macy return (NULL); 182959826caSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 183959826caSMatt Macy (void *)NULL, 0) == -1) 184959826caSMatt Macy return (NULL); 185bfb46e2bSMatt Macy } 186a0ac5706SEmmanuel Vadot for (pme = pmu_events_map; pme->cpuid != NULL; pme++) { 187a0ac5706SEmmanuel Vadot if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) { 188a0ac5706SEmmanuel Vadot printf("regex '%s' failed to compile, ignoring\n", 189a0ac5706SEmmanuel Vadot pme->cpuid); 190a0ac5706SEmmanuel Vadot continue; 191a0ac5706SEmmanuel Vadot } 192a0ac5706SEmmanuel Vadot match = regexec(&re, buf, 1, pmatch, 0); 193a0ac5706SEmmanuel Vadot regfree(&re); 194a0ac5706SEmmanuel Vadot if (match == 0) { 195a0ac5706SEmmanuel Vadot len = pmatch[0].rm_eo - pmatch[0].rm_so; 196a0ac5706SEmmanuel Vadot if(len == strlen(buf)) 197959826caSMatt Macy return (pme); 198a0ac5706SEmmanuel Vadot } 199a0ac5706SEmmanuel Vadot } 200959826caSMatt Macy return (NULL); 201959826caSMatt Macy } 202959826caSMatt Macy 203959826caSMatt Macy static const struct pmu_event * 204bfb46e2bSMatt Macy pmu_event_get(const char *cpuid, const char *event_name, int *idx) 205959826caSMatt Macy { 206959826caSMatt Macy const struct pmu_events_map *pme; 207959826caSMatt Macy const struct pmu_event *pe; 208959826caSMatt Macy int i; 209959826caSMatt Macy 210bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 211959826caSMatt Macy return (NULL); 212959826caSMatt Macy for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) { 213959826caSMatt Macy if (pe->name == NULL) 214959826caSMatt Macy continue; 215959826caSMatt Macy if (strcasecmp(pe->name, event_name) == 0) { 216959826caSMatt Macy if (idx) 217959826caSMatt Macy *idx = i; 218959826caSMatt Macy return (pe); 219959826caSMatt Macy } 220959826caSMatt Macy } 221959826caSMatt Macy return (NULL); 222959826caSMatt Macy } 223959826caSMatt Macy 224bfb46e2bSMatt Macy int 225bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *cpuid, const char *event) 226bfb46e2bSMatt Macy { 227bfb46e2bSMatt Macy int idx; 228bfb46e2bSMatt Macy const char *realname; 229bfb46e2bSMatt Macy 230bfb46e2bSMatt Macy realname = pmu_alias_get(event); 231bfb46e2bSMatt Macy if (pmu_event_get(cpuid, realname, &idx) == NULL) 232bfb46e2bSMatt Macy return (-1); 233bfb46e2bSMatt Macy return (idx); 234bfb46e2bSMatt Macy } 235bfb46e2bSMatt Macy 236959826caSMatt Macy const char * 237b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *cpuid, int idx) 238959826caSMatt Macy { 239959826caSMatt Macy const struct pmu_events_map *pme; 240959826caSMatt Macy 241b2ca2e50SMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 242959826caSMatt Macy return (NULL); 2431b000b50SMatt Macy assert(pme->table[idx].name); 2441b000b50SMatt Macy return (pme->table[idx].name); 245959826caSMatt Macy } 246959826caSMatt Macy 247959826caSMatt Macy static int 248959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin) 249959826caSMatt Macy { 250959826caSMatt Macy char *event; 2518bed125dSMatt Macy char *kvp, *key, *value, *r; 252959826caSMatt Macy char *debug; 253959826caSMatt Macy 254959826caSMatt Macy if ((event = strdup(eventin)) == NULL) 255959826caSMatt Macy return (ENOMEM); 2568bed125dSMatt Macy r = event; 257959826caSMatt Macy bzero(ped, sizeof(*ped)); 2580204d85aSMatt Macy ped->ped_period = DEFAULT_SAMPLE_COUNT; 25907d80fd8SMatt Macy ped->ped_umask = -1; 260959826caSMatt Macy while ((kvp = strsep(&event, ",")) != NULL) { 261959826caSMatt Macy key = strsep(&kvp, "="); 262959826caSMatt Macy if (key == NULL) 263959826caSMatt Macy abort(); 264959826caSMatt Macy value = kvp; 265959826caSMatt Macy if (strcmp(key, "umask") == 0) 266959826caSMatt Macy ped->ped_umask = strtol(value, NULL, 16); 267959826caSMatt Macy else if (strcmp(key, "event") == 0) 268959826caSMatt Macy ped->ped_event = strtol(value, NULL, 16); 269959826caSMatt Macy else if (strcmp(key, "period") == 0) 270959826caSMatt Macy ped->ped_period = strtol(value, NULL, 10); 271959826caSMatt Macy else if (strcmp(key, "offcore_rsp") == 0) 272959826caSMatt Macy ped->ped_offcore_rsp = strtol(value, NULL, 16); 273959826caSMatt Macy else if (strcmp(key, "any") == 0) 274959826caSMatt Macy ped->ped_any = strtol(value, NULL, 10); 275959826caSMatt Macy else if (strcmp(key, "cmask") == 0) 276959826caSMatt Macy ped->ped_cmask = strtol(value, NULL, 10); 277959826caSMatt Macy else if (strcmp(key, "inv") == 0) 278959826caSMatt Macy ped->ped_inv = strtol(value, NULL, 10); 279959826caSMatt Macy else if (strcmp(key, "edge") == 0) 280959826caSMatt Macy ped->ped_edge = strtol(value, NULL, 10); 281959826caSMatt Macy else if (strcmp(key, "frontend") == 0) 282959826caSMatt Macy ped->ped_frontend = strtol(value, NULL, 16); 283959826caSMatt Macy else if (strcmp(key, "ldlat") == 0) 284959826caSMatt Macy ped->ped_ldlat = strtol(value, NULL, 16); 285959826caSMatt Macy else if (strcmp(key, "fc_mask") == 0) 286959826caSMatt Macy ped->ped_fc_mask = strtol(value, NULL, 16); 287959826caSMatt Macy else if (strcmp(key, "ch_mask") == 0) 288959826caSMatt Macy ped->ped_ch_mask = strtol(value, NULL, 16); 289959826caSMatt Macy else if (strcmp(key, "config1") == 0) 290959826caSMatt Macy ped->ped_config1 = strtol(value, NULL, 16); 291dacc43dfSMatt Macy else if (strcmp(key, "l3_thread_mask") == 0) 292dacc43dfSMatt Macy ped->ped_l3_thread = strtol(value, NULL, 16); 293dacc43dfSMatt Macy else if (strcmp(key, "l3_slice_mask") == 0) 294dacc43dfSMatt Macy ped->ped_l3_slice = strtol(value, NULL, 16); 295959826caSMatt Macy else { 296959826caSMatt Macy debug = getenv("PMUDEBUG"); 297959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL) 298959826caSMatt Macy printf("unrecognized kvpair: %s:%s\n", key, value); 299959826caSMatt Macy } 300959826caSMatt Macy } 3018bed125dSMatt Macy free(r); 302959826caSMatt Macy return (0); 303959826caSMatt Macy } 304959826caSMatt Macy 305959826caSMatt Macy uint64_t 306959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name) 307959826caSMatt Macy { 308959826caSMatt Macy const struct pmu_event *pe; 309959826caSMatt Macy struct pmu_event_desc ped; 310959826caSMatt Macy 311959826caSMatt Macy event_name = pmu_alias_get(event_name); 312bfb46e2bSMatt Macy if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL) 313959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 314bfb46e2bSMatt Macy if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, NULL)) == NULL) 315959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 316959826caSMatt Macy if (pe->event == NULL) 317959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 318959826caSMatt Macy if (pmu_parse_event(&ped, pe->event)) 319959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 320959826caSMatt Macy return (ped.ped_period); 321959826caSMatt Macy } 322959826caSMatt Macy 323959826caSMatt Macy int 324959826caSMatt Macy pmc_pmu_enabled(void) 325959826caSMatt Macy { 326959826caSMatt Macy 327bfb46e2bSMatt Macy return (pmu_events_map_get(NULL) != NULL); 328959826caSMatt Macy } 329959826caSMatt Macy 330959826caSMatt Macy void 331fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name) 332959826caSMatt Macy { 333959826caSMatt Macy const struct pmu_events_map *pme; 334959826caSMatt Macy const struct pmu_event *pe; 335959826caSMatt Macy struct pmu_event_desc ped; 336959826caSMatt Macy char *debug; 337959826caSMatt Macy int do_debug; 338959826caSMatt Macy 339959826caSMatt Macy debug = getenv("PMUDEBUG"); 340959826caSMatt Macy do_debug = 0; 341959826caSMatt Macy 342959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0) 343959826caSMatt Macy do_debug = 1; 344bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 345959826caSMatt Macy return; 346959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 347959826caSMatt Macy if (pe->name == NULL) 348959826caSMatt Macy continue; 349fbf962e6SMatt Macy if (event_name != NULL && strcasestr(pe->name, event_name) == NULL) 350fbf962e6SMatt Macy continue; 351959826caSMatt Macy printf("\t%s\n", pe->name); 352959826caSMatt Macy if (do_debug) 353959826caSMatt Macy pmu_parse_event(&ped, pe->event); 354959826caSMatt Macy } 355959826caSMatt Macy } 356959826caSMatt Macy 357959826caSMatt Macy void 358959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev) 359959826caSMatt Macy { 360959826caSMatt Macy const struct pmu_events_map *pme; 361959826caSMatt Macy const struct pmu_event *pe; 362959826caSMatt Macy 363bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 364959826caSMatt Macy return; 365959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 366959826caSMatt Macy if (pe->name == NULL) 367959826caSMatt Macy continue; 368959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL && 369959826caSMatt Macy pe->desc != NULL) 370959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 371959826caSMatt Macy } 372959826caSMatt Macy } 373959826caSMatt Macy 374959826caSMatt Macy void 375959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev) 376959826caSMatt Macy { 377959826caSMatt Macy const struct pmu_events_map *pme; 378959826caSMatt Macy const struct pmu_event *pe; 379959826caSMatt Macy 380bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 381959826caSMatt Macy return; 382959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 383959826caSMatt Macy if (pe->name == NULL) 384959826caSMatt Macy continue; 385959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL) { 386959826caSMatt Macy if (pe->long_desc != NULL) 387959826caSMatt Macy printf("%s:\n%s\n", pe->name, pe->long_desc); 388959826caSMatt Macy else if (pe->desc != NULL) 389959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 390959826caSMatt Macy } 391959826caSMatt Macy } 392959826caSMatt Macy } 393959826caSMatt Macy 394fbf962e6SMatt Macy void 395fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev) 396fbf962e6SMatt Macy { 397fbf962e6SMatt Macy const struct pmu_events_map *pme; 398fbf962e6SMatt Macy const struct pmu_event *pe; 399fbf962e6SMatt Macy 400bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 401fbf962e6SMatt Macy return; 402fbf962e6SMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 403fbf962e6SMatt Macy if (pe->name == NULL) 404fbf962e6SMatt Macy continue; 405fbf962e6SMatt Macy if (strcasestr(pe->name, ev) == NULL) 406fbf962e6SMatt Macy continue; 407fbf962e6SMatt Macy printf("name: %s\n", pe->name); 408fbf962e6SMatt Macy if (pe->long_desc != NULL) 409fbf962e6SMatt Macy printf("desc: %s\n", pe->long_desc); 410fbf962e6SMatt Macy else if (pe->desc != NULL) 411fbf962e6SMatt Macy printf("desc: %s\n", pe->desc); 412fbf962e6SMatt Macy if (pe->event != NULL) 413fbf962e6SMatt Macy printf("event: %s\n", pe->event); 414fbf962e6SMatt Macy if (pe->topic != NULL) 415fbf962e6SMatt Macy printf("topic: %s\n", pe->topic); 416fbf962e6SMatt Macy if (pe->pmu != NULL) 417fbf962e6SMatt Macy printf("pmu: %s\n", pe->pmu); 418fbf962e6SMatt Macy if (pe->unit != NULL) 419fbf962e6SMatt Macy printf("unit: %s\n", pe->unit); 420fbf962e6SMatt Macy if (pe->perpkg != NULL) 421fbf962e6SMatt Macy printf("perpkg: %s\n", pe->perpkg); 422fbf962e6SMatt Macy if (pe->metric_expr != NULL) 423fbf962e6SMatt Macy printf("metric_expr: %s\n", pe->metric_expr); 424fbf962e6SMatt Macy if (pe->metric_name != NULL) 425fbf962e6SMatt Macy printf("metric_name: %s\n", pe->metric_name); 426fbf962e6SMatt Macy if (pe->metric_group != NULL) 427fbf962e6SMatt Macy printf("metric_group: %s\n", pe->metric_group); 428fbf962e6SMatt Macy } 429fbf962e6SMatt Macy } 430fbf962e6SMatt Macy 43181eb4dcfSMatt Macy static int 432dacc43dfSMatt Macy pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 43381eb4dcfSMatt Macy struct pmu_event_desc *ped) 434959826caSMatt Macy { 43581eb4dcfSMatt Macy struct pmc_md_amd_op_pmcallocate *amd; 436dacc43dfSMatt Macy const struct pmu_event *pe; 437dacc43dfSMatt Macy int idx = -1; 43881eb4dcfSMatt Macy 43981eb4dcfSMatt Macy amd = &pm->pm_md.pm_amd; 44081eb4dcfSMatt Macy if (ped->ped_umask > 0) { 44181eb4dcfSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 44281eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask); 44381eb4dcfSMatt Macy } 44481eb4dcfSMatt Macy pm->pm_class = PMC_CLASS_K8; 445dacc43dfSMatt Macy pe = pmu_event_get(NULL, event_name, &idx); 44681eb4dcfSMatt Macy 447dacc43dfSMatt Macy if (strcmp("l3cache", pe->topic) == 0){ 448dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 449dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE; 450dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice); 451dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread); 452dacc43dfSMatt Macy } 453dacc43dfSMatt Macy else if (strcmp("data fabric", pe->topic) == 0){ 454dacc43dfSMatt Macy 455dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event); 456dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC; 457dacc43dfSMatt Macy } 458dacc43dfSMatt Macy else{ 459dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 460dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE; 46181eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 46281eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 46381eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 46481eb4dcfSMatt Macy amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS); 46581eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 46681eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_USR; 46781eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 46881eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_OS; 46981eb4dcfSMatt Macy if (ped->ped_edge) 47081eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 47181eb4dcfSMatt Macy if (ped->ped_inv) 47281eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 47381eb4dcfSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 47481eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_INT; 475dacc43dfSMatt Macy } 47681eb4dcfSMatt Macy return (0); 47781eb4dcfSMatt Macy } 47881eb4dcfSMatt Macy 47981eb4dcfSMatt Macy static int 48081eb4dcfSMatt Macy pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 48181eb4dcfSMatt Macy struct pmu_event_desc *ped) 48281eb4dcfSMatt Macy { 483959826caSMatt Macy struct pmc_md_iap_op_pmcallocate *iap; 48481eb4dcfSMatt Macy int isfixed; 485959826caSMatt Macy 486a191ed2dSMatt Macy isfixed = 0; 48781eb4dcfSMatt Macy iap = &pm->pm_md.pm_iap; 48807d80fd8SMatt Macy if (strcasestr(event_name, "UNC_") == event_name || 489785dd70dSMatt Macy strcasestr(event_name, "uncore") != NULL) { 490785dd70dSMatt Macy pm->pm_class = PMC_CLASS_UCP; 491a191ed2dSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 49281eb4dcfSMatt Macy } else if ((ped->ped_umask == -1) || 49381eb4dcfSMatt Macy (ped->ped_event == 0x0 && ped->ped_umask == 0x3)) { 49407d80fd8SMatt Macy pm->pm_class = PMC_CLASS_IAF; 49507d80fd8SMatt Macy } else { 496959826caSMatt Macy pm->pm_class = PMC_CLASS_IAP; 49707d80fd8SMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 498785dd70dSMatt Macy } 49981eb4dcfSMatt Macy iap->pm_iap_config |= IAP_EVSEL(ped->ped_event); 50081eb4dcfSMatt Macy if (ped->ped_umask > 0) 50181eb4dcfSMatt Macy iap->pm_iap_config |= IAP_UMASK(ped->ped_umask); 50281eb4dcfSMatt Macy iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask); 50381eb4dcfSMatt Macy iap->pm_iap_rsp = ped->ped_offcore_rsp; 504959826caSMatt Macy 50581eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 50681eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 50781eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 508959826caSMatt Macy iap->pm_iap_config |= (IAP_USR | IAP_OS); 50981eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 51081eb4dcfSMatt Macy iap->pm_iap_config |= IAP_USR; 51181eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 51281eb4dcfSMatt Macy iap->pm_iap_config |= IAP_OS; 51381eb4dcfSMatt Macy if (ped->ped_edge) 514959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 51581eb4dcfSMatt Macy if (ped->ped_any) 516959826caSMatt Macy iap->pm_iap_config |= IAP_ANY; 51781eb4dcfSMatt Macy if (ped->ped_inv) 518959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 519959826caSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 520959826caSMatt Macy iap->pm_iap_config |= IAP_INT; 521959826caSMatt Macy return (0); 522959826caSMatt Macy } 523959826caSMatt Macy 52481eb4dcfSMatt Macy int 52581eb4dcfSMatt Macy pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 52681eb4dcfSMatt Macy { 52781eb4dcfSMatt Macy const struct pmu_event *pe; 52881eb4dcfSMatt Macy struct pmu_event_desc ped; 52981eb4dcfSMatt Macy pmu_mfr_t mfr; 53081eb4dcfSMatt Macy int idx = -1; 53181eb4dcfSMatt Macy 53281eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 53381eb4dcfSMatt Macy return (ENOENT); 53481eb4dcfSMatt Macy 53581eb4dcfSMatt Macy bzero(&pm->pm_md, sizeof(pm->pm_md)); 53681eb4dcfSMatt Macy pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 53781eb4dcfSMatt Macy event_name = pmu_alias_get(event_name); 53881eb4dcfSMatt Macy if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 53981eb4dcfSMatt Macy return (ENOENT); 54081eb4dcfSMatt Macy if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, &idx)) == NULL) 54181eb4dcfSMatt Macy return (ENOENT); 54281eb4dcfSMatt Macy assert(idx >= 0); 54381eb4dcfSMatt Macy pm->pm_ev = idx; 54481eb4dcfSMatt Macy 54581eb4dcfSMatt Macy if (pe->event == NULL) 54681eb4dcfSMatt Macy return (ENOENT); 54781eb4dcfSMatt Macy if (pmu_parse_event(&ped, pe->event)) 54881eb4dcfSMatt Macy return (ENOENT); 54981eb4dcfSMatt Macy 55081eb4dcfSMatt Macy if (mfr == PMU_INTEL) 55181eb4dcfSMatt Macy return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped)); 55281eb4dcfSMatt Macy else 55381eb4dcfSMatt Macy return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped)); 55481eb4dcfSMatt Macy } 55581eb4dcfSMatt Macy 55678beed2fSMatt Macy /* 55778beed2fSMatt Macy * Ultimately rely on AMD calling theirs the same 55878beed2fSMatt Macy */ 55978beed2fSMatt Macy static const char *stat_mode_cntrs[] = { 560ee62e0b4SMatt Macy "cpu_clk_unhalted.thread", 56178beed2fSMatt Macy "inst_retired.any", 56278beed2fSMatt Macy "br_inst_retired.all_branches", 56378beed2fSMatt Macy "br_misp_retired.all_branches", 564a191ed2dSMatt Macy "longest_lat_cache.reference", 565a191ed2dSMatt Macy "longest_lat_cache.miss", 56678beed2fSMatt Macy }; 56778beed2fSMatt Macy 56878beed2fSMatt Macy int 56978beed2fSMatt Macy pmc_pmu_stat_mode(const char ***cntrs) 57078beed2fSMatt Macy { 57178beed2fSMatt Macy if (pmc_pmu_enabled()) { 57278beed2fSMatt Macy *cntrs = stat_mode_cntrs; 57378beed2fSMatt Macy return (0); 57478beed2fSMatt Macy } 57578beed2fSMatt Macy return (EOPNOTSUPP); 57678beed2fSMatt Macy } 57778beed2fSMatt Macy 578959826caSMatt Macy #else 5798b20f975SMatt Macy 5808b20f975SMatt Macy uint64_t 5818b20f975SMatt Macy pmc_pmu_sample_rate_get(const char *event_name __unused) 5828b20f975SMatt Macy { 5838b20f975SMatt Macy return (DEFAULT_SAMPLE_COUNT); 5848b20f975SMatt Macy } 5858b20f975SMatt Macy 5868b20f975SMatt Macy void 587fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name __unused) 5888b20f975SMatt Macy { 5898b20f975SMatt Macy } 5908b20f975SMatt Macy 5918b20f975SMatt Macy void 5928b20f975SMatt Macy pmc_pmu_print_counter_desc(const char *e __unused) 5938b20f975SMatt Macy { 5948b20f975SMatt Macy } 5958b20f975SMatt Macy 5968b20f975SMatt Macy void 5978b20f975SMatt Macy pmc_pmu_print_counter_desc_long(const char *e __unused) 5988b20f975SMatt Macy { 5998b20f975SMatt Macy } 6008b20f975SMatt Macy 601fbf962e6SMatt Macy void 602fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *e __unused) 603fbf962e6SMatt Macy { 604fbf962e6SMatt Macy 605fbf962e6SMatt Macy } 606fbf962e6SMatt Macy 6078b20f975SMatt Macy int 6088b20f975SMatt Macy pmc_pmu_enabled(void) 6098b20f975SMatt Macy { 6108b20f975SMatt Macy return (0); 6118b20f975SMatt Macy } 6128b20f975SMatt Macy 6138b20f975SMatt Macy int 6148b20f975SMatt Macy pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused) 6158b20f975SMatt Macy { 6168b20f975SMatt Macy return (EOPNOTSUPP); 6178b20f975SMatt Macy } 6188b20f975SMatt Macy 6198b20f975SMatt Macy const char * 620b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *c __unused, int idx __unused) 6218b20f975SMatt Macy { 6228b20f975SMatt Macy return (NULL); 6238b20f975SMatt Macy } 624bfb46e2bSMatt Macy 6258b20f975SMatt Macy int 6268b20f975SMatt Macy pmc_pmu_stat_mode(const char ***a __unused) 6278b20f975SMatt Macy { 6288b20f975SMatt Macy return (EOPNOTSUPP); 6298b20f975SMatt Macy } 630959826caSMatt Macy 631bfb46e2bSMatt Macy int 632ce6fc9d7SMatt Macy pmc_pmu_idx_get_by_event(const char *c __unused, const char *e __unused) 633bfb46e2bSMatt Macy { 634bfb46e2bSMatt Macy return (-1); 635bfb46e2bSMatt Macy } 636bfb46e2bSMatt Macy 637959826caSMatt Macy #endif 638