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> 33*24e337beSRyan Moeller #include <sys/pmc.h> 34959826caSMatt Macy #include <sys/sysctl.h> 35959826caSMatt Macy #include <stddef.h> 36959826caSMatt Macy #include <stdlib.h> 37959826caSMatt Macy #include <limits.h> 38f3dbece8SEmmanuel Vadot #include <regex.h> 39959826caSMatt Macy #include <string.h> 40959826caSMatt Macy #include <pmc.h> 41959826caSMatt Macy #include <pmclog.h> 421b000b50SMatt Macy #include <assert.h> 43959826caSMatt Macy #include <libpmcstat.h> 44959826caSMatt Macy #include "pmu-events/pmu-events.h" 45959826caSMatt Macy 467d1c2b74SMatt Macy #if defined(__amd64__) || defined(__i386__) 47959826caSMatt Macy struct pmu_alias { 48959826caSMatt Macy const char *pa_alias; 49959826caSMatt Macy const char *pa_name; 50959826caSMatt Macy }; 5181eb4dcfSMatt Macy 5281eb4dcfSMatt Macy typedef enum { 5381eb4dcfSMatt Macy PMU_INVALID, 5481eb4dcfSMatt Macy PMU_INTEL, 5581eb4dcfSMatt Macy PMU_AMD, 5681eb4dcfSMatt Macy } pmu_mfr_t; 5781eb4dcfSMatt Macy 5881eb4dcfSMatt Macy static struct pmu_alias pmu_intel_alias_table[] = { 59959826caSMatt Macy {"UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 60959826caSMatt Macy {"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 61959826caSMatt Macy {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"}, 62959826caSMatt Macy {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"}, 63959826caSMatt Macy {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"}, 64959826caSMatt Macy {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"}, 65959826caSMatt Macy {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"}, 66959826caSMatt Macy {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"}, 67959826caSMatt Macy {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"}, 68959826caSMatt Macy {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"}, 69959826caSMatt Macy {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"}, 70959826caSMatt Macy {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"}, 71959826caSMatt Macy {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"}, 72959826caSMatt Macy {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"}, 7396cbd26aSMatt Macy {"cycles", "tsc-tsc"}, 7486b5e013SMatt Macy {"unhalted-cycles", "CPU_CLK_UNHALTED.THREAD_P_ANY"}, 7596cbd26aSMatt Macy {"instructions", "inst-retired.any_p"}, 7696cbd26aSMatt Macy {"branch-mispredicts", "br_misp_retired.all_branches"}, 7796cbd26aSMatt Macy {"branches", "br_inst_retired.all_branches"}, 7896cbd26aSMatt Macy {"interrupts", "hw_interrupts.received"}, 7996cbd26aSMatt Macy {"ic-misses", "frontend_retired.l1i_miss"}, 80959826caSMatt Macy {NULL, NULL}, 81959826caSMatt Macy }; 82959826caSMatt Macy 8381eb4dcfSMatt Macy static struct pmu_alias pmu_amd_alias_table[] = { 8481eb4dcfSMatt Macy {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"}, 8581eb4dcfSMatt Macy {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"}, 8681eb4dcfSMatt Macy {NULL, NULL}, 8781eb4dcfSMatt Macy }; 8881eb4dcfSMatt Macy 8981eb4dcfSMatt Macy 9081eb4dcfSMatt Macy static pmu_mfr_t 9181eb4dcfSMatt Macy pmu_events_mfr(void) 9281eb4dcfSMatt Macy { 93*24e337beSRyan Moeller char buf[PMC_CPUID_LEN]; 94*24e337beSRyan Moeller size_t s = sizeof(buf); 9581eb4dcfSMatt Macy pmu_mfr_t mfr; 9681eb4dcfSMatt Macy 97*24e337beSRyan Moeller if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 9881eb4dcfSMatt Macy (void *)NULL, 0) == -1) 9981eb4dcfSMatt Macy return (PMU_INVALID); 10053071ed1SKonstantin Belousov if (strcasestr(buf, "AuthenticAMD") != NULL || 10153071ed1SKonstantin Belousov strcasestr(buf, "HygonGenuine") != NULL) 10281eb4dcfSMatt Macy mfr = PMU_AMD; 10381eb4dcfSMatt Macy else if (strcasestr(buf, "GenuineIntel") != NULL) 10481eb4dcfSMatt Macy mfr = PMU_INTEL; 10581eb4dcfSMatt Macy else 10681eb4dcfSMatt Macy mfr = PMU_INVALID; 10781eb4dcfSMatt Macy return (mfr); 10881eb4dcfSMatt Macy } 10981eb4dcfSMatt Macy 11007d80fd8SMatt Macy /* 11107d80fd8SMatt Macy * The Intel fixed mode counters are: 11207d80fd8SMatt Macy * "inst_retired.any", 11307d80fd8SMatt Macy * "cpu_clk_unhalted.thread", 11407d80fd8SMatt Macy * "cpu_clk_unhalted.thread_any", 11507d80fd8SMatt Macy * "cpu_clk_unhalted.ref_tsc", 11607d80fd8SMatt Macy * 11707d80fd8SMatt Macy */ 118a191ed2dSMatt Macy 119959826caSMatt Macy static const char * 120959826caSMatt Macy pmu_alias_get(const char *name) 121959826caSMatt Macy { 12281eb4dcfSMatt Macy pmu_mfr_t mfr; 123959826caSMatt Macy struct pmu_alias *pa; 12481eb4dcfSMatt Macy struct pmu_alias *pmu_alias_table; 12581eb4dcfSMatt Macy 12681eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 12781eb4dcfSMatt Macy return (name); 12881eb4dcfSMatt Macy if (mfr == PMU_AMD) 12981eb4dcfSMatt Macy pmu_alias_table = pmu_amd_alias_table; 13081eb4dcfSMatt Macy else if (mfr == PMU_INTEL) 13181eb4dcfSMatt Macy pmu_alias_table = pmu_intel_alias_table; 13281eb4dcfSMatt Macy else 13381eb4dcfSMatt Macy return (name); 134959826caSMatt Macy 135959826caSMatt Macy for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++) 136959826caSMatt Macy if (strcasecmp(name, pa->pa_alias) == 0) 137959826caSMatt Macy return (pa->pa_name); 13881eb4dcfSMatt Macy 139959826caSMatt Macy return (name); 140959826caSMatt Macy } 141959826caSMatt Macy 142959826caSMatt Macy struct pmu_event_desc { 143959826caSMatt Macy uint64_t ped_period; 144959826caSMatt Macy uint64_t ped_offcore_rsp; 145dacc43dfSMatt Macy uint64_t ped_l3_thread; 146dacc43dfSMatt Macy uint64_t ped_l3_slice; 147959826caSMatt Macy uint32_t ped_event; 148959826caSMatt Macy uint32_t ped_frontend; 149959826caSMatt Macy uint32_t ped_ldlat; 150959826caSMatt Macy uint32_t ped_config1; 15107d80fd8SMatt Macy int16_t ped_umask; 152959826caSMatt Macy uint8_t ped_cmask; 153959826caSMatt Macy uint8_t ped_any; 154959826caSMatt Macy uint8_t ped_inv; 155959826caSMatt Macy uint8_t ped_edge; 156959826caSMatt Macy uint8_t ped_fc_mask; 157959826caSMatt Macy uint8_t ped_ch_mask; 158959826caSMatt Macy }; 159959826caSMatt Macy 160959826caSMatt Macy static const struct pmu_events_map * 161bfb46e2bSMatt Macy pmu_events_map_get(const char *cpuid) 162959826caSMatt Macy { 163a0ac5706SEmmanuel Vadot regex_t re; 164a0ac5706SEmmanuel Vadot regmatch_t pmatch[1]; 165*24e337beSRyan Moeller char buf[PMC_CPUID_LEN]; 166*24e337beSRyan Moeller size_t s = sizeof(buf); 167a0ac5706SEmmanuel Vadot int match; 168959826caSMatt Macy const struct pmu_events_map *pme; 169959826caSMatt Macy 170bfb46e2bSMatt Macy if (cpuid != NULL) { 171*24e337beSRyan Moeller strlcpy(buf, cpuid, s); 172bfb46e2bSMatt Macy } else { 173959826caSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 174959826caSMatt Macy (void *)NULL, 0) == -1) 175959826caSMatt Macy return (NULL); 176bfb46e2bSMatt Macy } 177a0ac5706SEmmanuel Vadot for (pme = pmu_events_map; pme->cpuid != NULL; pme++) { 178a0ac5706SEmmanuel Vadot if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) { 179a0ac5706SEmmanuel Vadot printf("regex '%s' failed to compile, ignoring\n", 180a0ac5706SEmmanuel Vadot pme->cpuid); 181a0ac5706SEmmanuel Vadot continue; 182a0ac5706SEmmanuel Vadot } 183a0ac5706SEmmanuel Vadot match = regexec(&re, buf, 1, pmatch, 0); 184a0ac5706SEmmanuel Vadot regfree(&re); 185a0ac5706SEmmanuel Vadot if (match == 0) { 1861791cad0SAlexander Motin if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0 1871791cad0SAlexander Motin || buf[pmatch[0].rm_eo] == '-')) 188959826caSMatt Macy return (pme); 189a0ac5706SEmmanuel Vadot } 190a0ac5706SEmmanuel Vadot } 191959826caSMatt Macy return (NULL); 192959826caSMatt Macy } 193959826caSMatt Macy 194959826caSMatt Macy static const struct pmu_event * 195bfb46e2bSMatt Macy pmu_event_get(const char *cpuid, const char *event_name, int *idx) 196959826caSMatt Macy { 197959826caSMatt Macy const struct pmu_events_map *pme; 198959826caSMatt Macy const struct pmu_event *pe; 199959826caSMatt Macy int i; 200959826caSMatt Macy 201bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 202959826caSMatt Macy return (NULL); 203959826caSMatt Macy for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) { 204959826caSMatt Macy if (pe->name == NULL) 205959826caSMatt Macy continue; 206959826caSMatt Macy if (strcasecmp(pe->name, event_name) == 0) { 207959826caSMatt Macy if (idx) 208959826caSMatt Macy *idx = i; 209959826caSMatt Macy return (pe); 210959826caSMatt Macy } 211959826caSMatt Macy } 212959826caSMatt Macy return (NULL); 213959826caSMatt Macy } 214959826caSMatt Macy 215bfb46e2bSMatt Macy int 216bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *cpuid, const char *event) 217bfb46e2bSMatt Macy { 218bfb46e2bSMatt Macy int idx; 219bfb46e2bSMatt Macy const char *realname; 220bfb46e2bSMatt Macy 221bfb46e2bSMatt Macy realname = pmu_alias_get(event); 222bfb46e2bSMatt Macy if (pmu_event_get(cpuid, realname, &idx) == NULL) 223bfb46e2bSMatt Macy return (-1); 224bfb46e2bSMatt Macy return (idx); 225bfb46e2bSMatt Macy } 226bfb46e2bSMatt Macy 227959826caSMatt Macy const char * 228b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *cpuid, int idx) 229959826caSMatt Macy { 230959826caSMatt Macy const struct pmu_events_map *pme; 231959826caSMatt Macy 232b2ca2e50SMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 233959826caSMatt Macy return (NULL); 2341b000b50SMatt Macy assert(pme->table[idx].name); 2351b000b50SMatt Macy return (pme->table[idx].name); 236959826caSMatt Macy } 237959826caSMatt Macy 238959826caSMatt Macy static int 239959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin) 240959826caSMatt Macy { 241959826caSMatt Macy char *event; 2428bed125dSMatt Macy char *kvp, *key, *value, *r; 243959826caSMatt Macy char *debug; 244959826caSMatt Macy 245959826caSMatt Macy if ((event = strdup(eventin)) == NULL) 246959826caSMatt Macy return (ENOMEM); 2478bed125dSMatt Macy r = event; 248959826caSMatt Macy bzero(ped, sizeof(*ped)); 2490204d85aSMatt Macy ped->ped_period = DEFAULT_SAMPLE_COUNT; 25007d80fd8SMatt Macy ped->ped_umask = -1; 251959826caSMatt Macy while ((kvp = strsep(&event, ",")) != NULL) { 252959826caSMatt Macy key = strsep(&kvp, "="); 253959826caSMatt Macy if (key == NULL) 254959826caSMatt Macy abort(); 255959826caSMatt Macy value = kvp; 256959826caSMatt Macy if (strcmp(key, "umask") == 0) 257959826caSMatt Macy ped->ped_umask = strtol(value, NULL, 16); 258959826caSMatt Macy else if (strcmp(key, "event") == 0) 259959826caSMatt Macy ped->ped_event = strtol(value, NULL, 16); 260959826caSMatt Macy else if (strcmp(key, "period") == 0) 261959826caSMatt Macy ped->ped_period = strtol(value, NULL, 10); 262959826caSMatt Macy else if (strcmp(key, "offcore_rsp") == 0) 263959826caSMatt Macy ped->ped_offcore_rsp = strtol(value, NULL, 16); 264959826caSMatt Macy else if (strcmp(key, "any") == 0) 265959826caSMatt Macy ped->ped_any = strtol(value, NULL, 10); 266959826caSMatt Macy else if (strcmp(key, "cmask") == 0) 267959826caSMatt Macy ped->ped_cmask = strtol(value, NULL, 10); 268959826caSMatt Macy else if (strcmp(key, "inv") == 0) 269959826caSMatt Macy ped->ped_inv = strtol(value, NULL, 10); 270959826caSMatt Macy else if (strcmp(key, "edge") == 0) 271959826caSMatt Macy ped->ped_edge = strtol(value, NULL, 10); 272959826caSMatt Macy else if (strcmp(key, "frontend") == 0) 273959826caSMatt Macy ped->ped_frontend = strtol(value, NULL, 16); 274959826caSMatt Macy else if (strcmp(key, "ldlat") == 0) 275959826caSMatt Macy ped->ped_ldlat = strtol(value, NULL, 16); 276959826caSMatt Macy else if (strcmp(key, "fc_mask") == 0) 277959826caSMatt Macy ped->ped_fc_mask = strtol(value, NULL, 16); 278959826caSMatt Macy else if (strcmp(key, "ch_mask") == 0) 279959826caSMatt Macy ped->ped_ch_mask = strtol(value, NULL, 16); 280959826caSMatt Macy else if (strcmp(key, "config1") == 0) 281959826caSMatt Macy ped->ped_config1 = strtol(value, NULL, 16); 282dacc43dfSMatt Macy else if (strcmp(key, "l3_thread_mask") == 0) 283dacc43dfSMatt Macy ped->ped_l3_thread = strtol(value, NULL, 16); 284dacc43dfSMatt Macy else if (strcmp(key, "l3_slice_mask") == 0) 285dacc43dfSMatt Macy ped->ped_l3_slice = strtol(value, NULL, 16); 286959826caSMatt Macy else { 287959826caSMatt Macy debug = getenv("PMUDEBUG"); 288959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL) 289959826caSMatt Macy printf("unrecognized kvpair: %s:%s\n", key, value); 290959826caSMatt Macy } 291959826caSMatt Macy } 2928bed125dSMatt Macy free(r); 293959826caSMatt Macy return (0); 294959826caSMatt Macy } 295959826caSMatt Macy 296959826caSMatt Macy uint64_t 297959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name) 298959826caSMatt Macy { 299959826caSMatt Macy const struct pmu_event *pe; 300959826caSMatt Macy struct pmu_event_desc ped; 301959826caSMatt Macy 302959826caSMatt Macy event_name = pmu_alias_get(event_name); 303bfb46e2bSMatt Macy if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL) 304959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 305bfb46e2bSMatt Macy if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, NULL)) == NULL) 306959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 307959826caSMatt Macy if (pe->event == NULL) 308959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 309959826caSMatt Macy if (pmu_parse_event(&ped, pe->event)) 310959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 311959826caSMatt Macy return (ped.ped_period); 312959826caSMatt Macy } 313959826caSMatt Macy 314959826caSMatt Macy int 315959826caSMatt Macy pmc_pmu_enabled(void) 316959826caSMatt Macy { 317959826caSMatt Macy 318bfb46e2bSMatt Macy return (pmu_events_map_get(NULL) != NULL); 319959826caSMatt Macy } 320959826caSMatt Macy 321959826caSMatt Macy void 322fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name) 323959826caSMatt Macy { 324959826caSMatt Macy const struct pmu_events_map *pme; 325959826caSMatt Macy const struct pmu_event *pe; 326959826caSMatt Macy struct pmu_event_desc ped; 327959826caSMatt Macy char *debug; 328959826caSMatt Macy int do_debug; 329959826caSMatt Macy 330959826caSMatt Macy debug = getenv("PMUDEBUG"); 331959826caSMatt Macy do_debug = 0; 332959826caSMatt Macy 333959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0) 334959826caSMatt Macy do_debug = 1; 335bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 336959826caSMatt Macy return; 337959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 338959826caSMatt Macy if (pe->name == NULL) 339959826caSMatt Macy continue; 340fbf962e6SMatt Macy if (event_name != NULL && strcasestr(pe->name, event_name) == NULL) 341fbf962e6SMatt Macy continue; 342959826caSMatt Macy printf("\t%s\n", pe->name); 343959826caSMatt Macy if (do_debug) 344959826caSMatt Macy pmu_parse_event(&ped, pe->event); 345959826caSMatt Macy } 346959826caSMatt Macy } 347959826caSMatt Macy 348959826caSMatt Macy void 349959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev) 350959826caSMatt Macy { 351959826caSMatt Macy const struct pmu_events_map *pme; 352959826caSMatt Macy const struct pmu_event *pe; 353959826caSMatt Macy 354bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 355959826caSMatt Macy return; 356959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 357959826caSMatt Macy if (pe->name == NULL) 358959826caSMatt Macy continue; 359959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL && 360959826caSMatt Macy pe->desc != NULL) 361959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 362959826caSMatt Macy } 363959826caSMatt Macy } 364959826caSMatt Macy 365959826caSMatt Macy void 366959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev) 367959826caSMatt Macy { 368959826caSMatt Macy const struct pmu_events_map *pme; 369959826caSMatt Macy const struct pmu_event *pe; 370959826caSMatt Macy 371bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 372959826caSMatt Macy return; 373959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 374959826caSMatt Macy if (pe->name == NULL) 375959826caSMatt Macy continue; 376959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL) { 377959826caSMatt Macy if (pe->long_desc != NULL) 378959826caSMatt Macy printf("%s:\n%s\n", pe->name, pe->long_desc); 379959826caSMatt Macy else if (pe->desc != NULL) 380959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 381959826caSMatt Macy } 382959826caSMatt Macy } 383959826caSMatt Macy } 384959826caSMatt Macy 385fbf962e6SMatt Macy void 386fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev) 387fbf962e6SMatt Macy { 388fbf962e6SMatt Macy const struct pmu_events_map *pme; 389fbf962e6SMatt Macy const struct pmu_event *pe; 390fbf962e6SMatt Macy 391bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 392fbf962e6SMatt Macy return; 393fbf962e6SMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 394fbf962e6SMatt Macy if (pe->name == NULL) 395fbf962e6SMatt Macy continue; 396fbf962e6SMatt Macy if (strcasestr(pe->name, ev) == NULL) 397fbf962e6SMatt Macy continue; 398fbf962e6SMatt Macy printf("name: %s\n", pe->name); 399fbf962e6SMatt Macy if (pe->long_desc != NULL) 400fbf962e6SMatt Macy printf("desc: %s\n", pe->long_desc); 401fbf962e6SMatt Macy else if (pe->desc != NULL) 402fbf962e6SMatt Macy printf("desc: %s\n", pe->desc); 403fbf962e6SMatt Macy if (pe->event != NULL) 404fbf962e6SMatt Macy printf("event: %s\n", pe->event); 405fbf962e6SMatt Macy if (pe->topic != NULL) 406fbf962e6SMatt Macy printf("topic: %s\n", pe->topic); 407fbf962e6SMatt Macy if (pe->pmu != NULL) 408fbf962e6SMatt Macy printf("pmu: %s\n", pe->pmu); 409fbf962e6SMatt Macy if (pe->unit != NULL) 410fbf962e6SMatt Macy printf("unit: %s\n", pe->unit); 411fbf962e6SMatt Macy if (pe->perpkg != NULL) 412fbf962e6SMatt Macy printf("perpkg: %s\n", pe->perpkg); 413fbf962e6SMatt Macy if (pe->metric_expr != NULL) 414fbf962e6SMatt Macy printf("metric_expr: %s\n", pe->metric_expr); 415fbf962e6SMatt Macy if (pe->metric_name != NULL) 416fbf962e6SMatt Macy printf("metric_name: %s\n", pe->metric_name); 417fbf962e6SMatt Macy if (pe->metric_group != NULL) 418fbf962e6SMatt Macy printf("metric_group: %s\n", pe->metric_group); 419fbf962e6SMatt Macy } 420fbf962e6SMatt Macy } 421fbf962e6SMatt Macy 42281eb4dcfSMatt Macy static int 423dacc43dfSMatt Macy pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 42481eb4dcfSMatt Macy struct pmu_event_desc *ped) 425959826caSMatt Macy { 42681eb4dcfSMatt Macy struct pmc_md_amd_op_pmcallocate *amd; 427dacc43dfSMatt Macy const struct pmu_event *pe; 428dacc43dfSMatt Macy int idx = -1; 42981eb4dcfSMatt Macy 43081eb4dcfSMatt Macy amd = &pm->pm_md.pm_amd; 43181eb4dcfSMatt Macy if (ped->ped_umask > 0) { 43281eb4dcfSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 43381eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask); 43481eb4dcfSMatt Macy } 43581eb4dcfSMatt Macy pm->pm_class = PMC_CLASS_K8; 436dacc43dfSMatt Macy pe = pmu_event_get(NULL, event_name, &idx); 43781eb4dcfSMatt Macy 438dacc43dfSMatt Macy if (strcmp("l3cache", pe->topic) == 0){ 439dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 440dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE; 441dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice); 442dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread); 443dacc43dfSMatt Macy } 444dacc43dfSMatt Macy else if (strcmp("data fabric", pe->topic) == 0){ 445dacc43dfSMatt Macy 446dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event); 447dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC; 448dacc43dfSMatt Macy } 449dacc43dfSMatt Macy else{ 450dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 451dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE; 45281eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 45381eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 45481eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 45581eb4dcfSMatt Macy amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS); 45681eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 45781eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_USR; 45881eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 45981eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_OS; 46081eb4dcfSMatt Macy if (ped->ped_edge) 46181eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 46281eb4dcfSMatt Macy if (ped->ped_inv) 46381eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 46481eb4dcfSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 46581eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_INT; 466dacc43dfSMatt Macy } 46781eb4dcfSMatt Macy return (0); 46881eb4dcfSMatt Macy } 46981eb4dcfSMatt Macy 47081eb4dcfSMatt Macy static int 47181eb4dcfSMatt Macy pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 47281eb4dcfSMatt Macy struct pmu_event_desc *ped) 47381eb4dcfSMatt Macy { 474959826caSMatt Macy struct pmc_md_iap_op_pmcallocate *iap; 47581eb4dcfSMatt Macy int isfixed; 476959826caSMatt Macy 477a191ed2dSMatt Macy isfixed = 0; 47881eb4dcfSMatt Macy iap = &pm->pm_md.pm_iap; 47907d80fd8SMatt Macy if (strcasestr(event_name, "UNC_") == event_name || 480785dd70dSMatt Macy strcasestr(event_name, "uncore") != NULL) { 481785dd70dSMatt Macy pm->pm_class = PMC_CLASS_UCP; 482a191ed2dSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 48381eb4dcfSMatt Macy } else if ((ped->ped_umask == -1) || 48481eb4dcfSMatt Macy (ped->ped_event == 0x0 && ped->ped_umask == 0x3)) { 48507d80fd8SMatt Macy pm->pm_class = PMC_CLASS_IAF; 48607d80fd8SMatt Macy } else { 487959826caSMatt Macy pm->pm_class = PMC_CLASS_IAP; 48807d80fd8SMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 489785dd70dSMatt Macy } 49081eb4dcfSMatt Macy iap->pm_iap_config |= IAP_EVSEL(ped->ped_event); 49181eb4dcfSMatt Macy if (ped->ped_umask > 0) 49281eb4dcfSMatt Macy iap->pm_iap_config |= IAP_UMASK(ped->ped_umask); 49381eb4dcfSMatt Macy iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask); 49481eb4dcfSMatt Macy iap->pm_iap_rsp = ped->ped_offcore_rsp; 495959826caSMatt Macy 49681eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 49781eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 49881eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 499959826caSMatt Macy iap->pm_iap_config |= (IAP_USR | IAP_OS); 50081eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 50181eb4dcfSMatt Macy iap->pm_iap_config |= IAP_USR; 50281eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 50381eb4dcfSMatt Macy iap->pm_iap_config |= IAP_OS; 50481eb4dcfSMatt Macy if (ped->ped_edge) 505959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 50681eb4dcfSMatt Macy if (ped->ped_any) 507959826caSMatt Macy iap->pm_iap_config |= IAP_ANY; 50881eb4dcfSMatt Macy if (ped->ped_inv) 509959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 510959826caSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 511959826caSMatt Macy iap->pm_iap_config |= IAP_INT; 512959826caSMatt Macy return (0); 513959826caSMatt Macy } 514959826caSMatt Macy 51581eb4dcfSMatt Macy int 51681eb4dcfSMatt Macy pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 51781eb4dcfSMatt Macy { 51881eb4dcfSMatt Macy const struct pmu_event *pe; 51981eb4dcfSMatt Macy struct pmu_event_desc ped; 52081eb4dcfSMatt Macy pmu_mfr_t mfr; 52181eb4dcfSMatt Macy int idx = -1; 52281eb4dcfSMatt Macy 52381eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 52481eb4dcfSMatt Macy return (ENOENT); 52581eb4dcfSMatt Macy 52681eb4dcfSMatt Macy bzero(&pm->pm_md, sizeof(pm->pm_md)); 52781eb4dcfSMatt Macy pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 52881eb4dcfSMatt Macy event_name = pmu_alias_get(event_name); 52981eb4dcfSMatt Macy if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 53081eb4dcfSMatt Macy return (ENOENT); 53181eb4dcfSMatt Macy if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, &idx)) == NULL) 53281eb4dcfSMatt Macy return (ENOENT); 53381eb4dcfSMatt Macy assert(idx >= 0); 53481eb4dcfSMatt Macy pm->pm_ev = idx; 53581eb4dcfSMatt Macy 53681eb4dcfSMatt Macy if (pe->event == NULL) 53781eb4dcfSMatt Macy return (ENOENT); 53881eb4dcfSMatt Macy if (pmu_parse_event(&ped, pe->event)) 53981eb4dcfSMatt Macy return (ENOENT); 54081eb4dcfSMatt Macy 54181eb4dcfSMatt Macy if (mfr == PMU_INTEL) 54281eb4dcfSMatt Macy return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped)); 54381eb4dcfSMatt Macy else 54481eb4dcfSMatt Macy return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped)); 54581eb4dcfSMatt Macy } 54681eb4dcfSMatt Macy 54778beed2fSMatt Macy /* 54878beed2fSMatt Macy * Ultimately rely on AMD calling theirs the same 54978beed2fSMatt Macy */ 55078beed2fSMatt Macy static const char *stat_mode_cntrs[] = { 551ee62e0b4SMatt Macy "cpu_clk_unhalted.thread", 55278beed2fSMatt Macy "inst_retired.any", 55378beed2fSMatt Macy "br_inst_retired.all_branches", 55478beed2fSMatt Macy "br_misp_retired.all_branches", 555a191ed2dSMatt Macy "longest_lat_cache.reference", 556a191ed2dSMatt Macy "longest_lat_cache.miss", 55778beed2fSMatt Macy }; 55878beed2fSMatt Macy 55978beed2fSMatt Macy int 56078beed2fSMatt Macy pmc_pmu_stat_mode(const char ***cntrs) 56178beed2fSMatt Macy { 56278beed2fSMatt Macy if (pmc_pmu_enabled()) { 56378beed2fSMatt Macy *cntrs = stat_mode_cntrs; 56478beed2fSMatt Macy return (0); 56578beed2fSMatt Macy } 56678beed2fSMatt Macy return (EOPNOTSUPP); 56778beed2fSMatt Macy } 56878beed2fSMatt Macy 569959826caSMatt Macy #else 5708b20f975SMatt Macy 5718b20f975SMatt Macy uint64_t 5728b20f975SMatt Macy pmc_pmu_sample_rate_get(const char *event_name __unused) 5738b20f975SMatt Macy { 5748b20f975SMatt Macy return (DEFAULT_SAMPLE_COUNT); 5758b20f975SMatt Macy } 5768b20f975SMatt Macy 5778b20f975SMatt Macy void 578fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name __unused) 5798b20f975SMatt Macy { 5808b20f975SMatt Macy } 5818b20f975SMatt Macy 5828b20f975SMatt Macy void 5838b20f975SMatt Macy pmc_pmu_print_counter_desc(const char *e __unused) 5848b20f975SMatt Macy { 5858b20f975SMatt Macy } 5868b20f975SMatt Macy 5878b20f975SMatt Macy void 5888b20f975SMatt Macy pmc_pmu_print_counter_desc_long(const char *e __unused) 5898b20f975SMatt Macy { 5908b20f975SMatt Macy } 5918b20f975SMatt Macy 592fbf962e6SMatt Macy void 593fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *e __unused) 594fbf962e6SMatt Macy { 595fbf962e6SMatt Macy 596fbf962e6SMatt Macy } 597fbf962e6SMatt Macy 5988b20f975SMatt Macy int 5998b20f975SMatt Macy pmc_pmu_enabled(void) 6008b20f975SMatt Macy { 6018b20f975SMatt Macy return (0); 6028b20f975SMatt Macy } 6038b20f975SMatt Macy 6048b20f975SMatt Macy int 6058b20f975SMatt Macy pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused) 6068b20f975SMatt Macy { 6078b20f975SMatt Macy return (EOPNOTSUPP); 6088b20f975SMatt Macy } 6098b20f975SMatt Macy 6108b20f975SMatt Macy const char * 611b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *c __unused, int idx __unused) 6128b20f975SMatt Macy { 6138b20f975SMatt Macy return (NULL); 6148b20f975SMatt Macy } 615bfb46e2bSMatt Macy 6168b20f975SMatt Macy int 6178b20f975SMatt Macy pmc_pmu_stat_mode(const char ***a __unused) 6188b20f975SMatt Macy { 6198b20f975SMatt Macy return (EOPNOTSUPP); 6208b20f975SMatt Macy } 621959826caSMatt Macy 622bfb46e2bSMatt Macy int 623ce6fc9d7SMatt Macy pmc_pmu_idx_get_by_event(const char *c __unused, const char *e __unused) 624bfb46e2bSMatt Macy { 625bfb46e2bSMatt Macy return (-1); 626bfb46e2bSMatt Macy } 627bfb46e2bSMatt Macy 628959826caSMatt Macy #endif 629