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> 37f3dbece8SEmmanuel 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 } 10653071ed1SKonstantin Belousov if (strcasestr(buf, "AuthenticAMD") != NULL || 10753071ed1SKonstantin Belousov strcasestr(buf, "HygonGenuine") != NULL) 10881eb4dcfSMatt Macy mfr = PMU_AMD; 10981eb4dcfSMatt Macy else if (strcasestr(buf, "GenuineIntel") != NULL) 11081eb4dcfSMatt Macy mfr = PMU_INTEL; 11181eb4dcfSMatt Macy else 11281eb4dcfSMatt Macy mfr = PMU_INVALID; 11381eb4dcfSMatt Macy free(buf); 11481eb4dcfSMatt Macy return (mfr); 11581eb4dcfSMatt Macy } 11681eb4dcfSMatt Macy 11707d80fd8SMatt Macy /* 11807d80fd8SMatt Macy * The Intel fixed mode counters are: 11907d80fd8SMatt Macy * "inst_retired.any", 12007d80fd8SMatt Macy * "cpu_clk_unhalted.thread", 12107d80fd8SMatt Macy * "cpu_clk_unhalted.thread_any", 12207d80fd8SMatt Macy * "cpu_clk_unhalted.ref_tsc", 12307d80fd8SMatt Macy * 12407d80fd8SMatt Macy */ 125a191ed2dSMatt Macy 126959826caSMatt Macy static const char * 127959826caSMatt Macy pmu_alias_get(const char *name) 128959826caSMatt Macy { 12981eb4dcfSMatt Macy pmu_mfr_t mfr; 130959826caSMatt Macy struct pmu_alias *pa; 13181eb4dcfSMatt Macy struct pmu_alias *pmu_alias_table; 13281eb4dcfSMatt Macy 13381eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 13481eb4dcfSMatt Macy return (name); 13581eb4dcfSMatt Macy if (mfr == PMU_AMD) 13681eb4dcfSMatt Macy pmu_alias_table = pmu_amd_alias_table; 13781eb4dcfSMatt Macy else if (mfr == PMU_INTEL) 13881eb4dcfSMatt Macy pmu_alias_table = pmu_intel_alias_table; 13981eb4dcfSMatt Macy else 14081eb4dcfSMatt Macy return (name); 141959826caSMatt Macy 142959826caSMatt Macy for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++) 143959826caSMatt Macy if (strcasecmp(name, pa->pa_alias) == 0) 144959826caSMatt Macy return (pa->pa_name); 14581eb4dcfSMatt Macy 146959826caSMatt Macy return (name); 147959826caSMatt Macy } 148959826caSMatt Macy 149959826caSMatt Macy struct pmu_event_desc { 150959826caSMatt Macy uint64_t ped_period; 151959826caSMatt Macy uint64_t ped_offcore_rsp; 152dacc43dfSMatt Macy uint64_t ped_l3_thread; 153dacc43dfSMatt Macy uint64_t ped_l3_slice; 154959826caSMatt Macy uint32_t ped_event; 155959826caSMatt Macy uint32_t ped_frontend; 156959826caSMatt Macy uint32_t ped_ldlat; 157959826caSMatt Macy uint32_t ped_config1; 15807d80fd8SMatt Macy int16_t ped_umask; 159959826caSMatt Macy uint8_t ped_cmask; 160959826caSMatt Macy uint8_t ped_any; 161959826caSMatt Macy uint8_t ped_inv; 162959826caSMatt Macy uint8_t ped_edge; 163959826caSMatt Macy uint8_t ped_fc_mask; 164959826caSMatt Macy uint8_t ped_ch_mask; 165959826caSMatt Macy }; 166959826caSMatt Macy 167959826caSMatt Macy static const struct pmu_events_map * 168bfb46e2bSMatt Macy pmu_events_map_get(const char *cpuid) 169959826caSMatt Macy { 170a0ac5706SEmmanuel Vadot regex_t re; 171a0ac5706SEmmanuel Vadot regmatch_t pmatch[1]; 172*1791cad0SAlexander Motin size_t s; 173959826caSMatt Macy char buf[64]; 174a0ac5706SEmmanuel Vadot int match; 175959826caSMatt Macy const struct pmu_events_map *pme; 176959826caSMatt Macy 177bfb46e2bSMatt Macy if (cpuid != NULL) { 178bfb46e2bSMatt Macy memcpy(buf, cpuid, 64); 179bfb46e2bSMatt Macy } else { 180959826caSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s, 181959826caSMatt Macy (void *)NULL, 0) == -1) 182959826caSMatt Macy return (NULL); 183959826caSMatt Macy if (sysctlbyname("kern.hwpmc.cpuid", buf, &s, 184959826caSMatt Macy (void *)NULL, 0) == -1) 185959826caSMatt Macy return (NULL); 186bfb46e2bSMatt Macy } 187a0ac5706SEmmanuel Vadot for (pme = pmu_events_map; pme->cpuid != NULL; pme++) { 188a0ac5706SEmmanuel Vadot if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) { 189a0ac5706SEmmanuel Vadot printf("regex '%s' failed to compile, ignoring\n", 190a0ac5706SEmmanuel Vadot pme->cpuid); 191a0ac5706SEmmanuel Vadot continue; 192a0ac5706SEmmanuel Vadot } 193a0ac5706SEmmanuel Vadot match = regexec(&re, buf, 1, pmatch, 0); 194a0ac5706SEmmanuel Vadot regfree(&re); 195a0ac5706SEmmanuel Vadot if (match == 0) { 196*1791cad0SAlexander Motin if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0 197*1791cad0SAlexander Motin || buf[pmatch[0].rm_eo] == '-')) 198959826caSMatt Macy return (pme); 199a0ac5706SEmmanuel Vadot } 200a0ac5706SEmmanuel Vadot } 201959826caSMatt Macy return (NULL); 202959826caSMatt Macy } 203959826caSMatt Macy 204959826caSMatt Macy static const struct pmu_event * 205bfb46e2bSMatt Macy pmu_event_get(const char *cpuid, const char *event_name, int *idx) 206959826caSMatt Macy { 207959826caSMatt Macy const struct pmu_events_map *pme; 208959826caSMatt Macy const struct pmu_event *pe; 209959826caSMatt Macy int i; 210959826caSMatt Macy 211bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 212959826caSMatt Macy return (NULL); 213959826caSMatt Macy for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) { 214959826caSMatt Macy if (pe->name == NULL) 215959826caSMatt Macy continue; 216959826caSMatt Macy if (strcasecmp(pe->name, event_name) == 0) { 217959826caSMatt Macy if (idx) 218959826caSMatt Macy *idx = i; 219959826caSMatt Macy return (pe); 220959826caSMatt Macy } 221959826caSMatt Macy } 222959826caSMatt Macy return (NULL); 223959826caSMatt Macy } 224959826caSMatt Macy 225bfb46e2bSMatt Macy int 226bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *cpuid, const char *event) 227bfb46e2bSMatt Macy { 228bfb46e2bSMatt Macy int idx; 229bfb46e2bSMatt Macy const char *realname; 230bfb46e2bSMatt Macy 231bfb46e2bSMatt Macy realname = pmu_alias_get(event); 232bfb46e2bSMatt Macy if (pmu_event_get(cpuid, realname, &idx) == NULL) 233bfb46e2bSMatt Macy return (-1); 234bfb46e2bSMatt Macy return (idx); 235bfb46e2bSMatt Macy } 236bfb46e2bSMatt Macy 237959826caSMatt Macy const char * 238b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *cpuid, int idx) 239959826caSMatt Macy { 240959826caSMatt Macy const struct pmu_events_map *pme; 241959826caSMatt Macy 242b2ca2e50SMatt Macy if ((pme = pmu_events_map_get(cpuid)) == NULL) 243959826caSMatt Macy return (NULL); 2441b000b50SMatt Macy assert(pme->table[idx].name); 2451b000b50SMatt Macy return (pme->table[idx].name); 246959826caSMatt Macy } 247959826caSMatt Macy 248959826caSMatt Macy static int 249959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin) 250959826caSMatt Macy { 251959826caSMatt Macy char *event; 2528bed125dSMatt Macy char *kvp, *key, *value, *r; 253959826caSMatt Macy char *debug; 254959826caSMatt Macy 255959826caSMatt Macy if ((event = strdup(eventin)) == NULL) 256959826caSMatt Macy return (ENOMEM); 2578bed125dSMatt Macy r = event; 258959826caSMatt Macy bzero(ped, sizeof(*ped)); 2590204d85aSMatt Macy ped->ped_period = DEFAULT_SAMPLE_COUNT; 26007d80fd8SMatt Macy ped->ped_umask = -1; 261959826caSMatt Macy while ((kvp = strsep(&event, ",")) != NULL) { 262959826caSMatt Macy key = strsep(&kvp, "="); 263959826caSMatt Macy if (key == NULL) 264959826caSMatt Macy abort(); 265959826caSMatt Macy value = kvp; 266959826caSMatt Macy if (strcmp(key, "umask") == 0) 267959826caSMatt Macy ped->ped_umask = strtol(value, NULL, 16); 268959826caSMatt Macy else if (strcmp(key, "event") == 0) 269959826caSMatt Macy ped->ped_event = strtol(value, NULL, 16); 270959826caSMatt Macy else if (strcmp(key, "period") == 0) 271959826caSMatt Macy ped->ped_period = strtol(value, NULL, 10); 272959826caSMatt Macy else if (strcmp(key, "offcore_rsp") == 0) 273959826caSMatt Macy ped->ped_offcore_rsp = strtol(value, NULL, 16); 274959826caSMatt Macy else if (strcmp(key, "any") == 0) 275959826caSMatt Macy ped->ped_any = strtol(value, NULL, 10); 276959826caSMatt Macy else if (strcmp(key, "cmask") == 0) 277959826caSMatt Macy ped->ped_cmask = strtol(value, NULL, 10); 278959826caSMatt Macy else if (strcmp(key, "inv") == 0) 279959826caSMatt Macy ped->ped_inv = strtol(value, NULL, 10); 280959826caSMatt Macy else if (strcmp(key, "edge") == 0) 281959826caSMatt Macy ped->ped_edge = strtol(value, NULL, 10); 282959826caSMatt Macy else if (strcmp(key, "frontend") == 0) 283959826caSMatt Macy ped->ped_frontend = strtol(value, NULL, 16); 284959826caSMatt Macy else if (strcmp(key, "ldlat") == 0) 285959826caSMatt Macy ped->ped_ldlat = strtol(value, NULL, 16); 286959826caSMatt Macy else if (strcmp(key, "fc_mask") == 0) 287959826caSMatt Macy ped->ped_fc_mask = strtol(value, NULL, 16); 288959826caSMatt Macy else if (strcmp(key, "ch_mask") == 0) 289959826caSMatt Macy ped->ped_ch_mask = strtol(value, NULL, 16); 290959826caSMatt Macy else if (strcmp(key, "config1") == 0) 291959826caSMatt Macy ped->ped_config1 = strtol(value, NULL, 16); 292dacc43dfSMatt Macy else if (strcmp(key, "l3_thread_mask") == 0) 293dacc43dfSMatt Macy ped->ped_l3_thread = strtol(value, NULL, 16); 294dacc43dfSMatt Macy else if (strcmp(key, "l3_slice_mask") == 0) 295dacc43dfSMatt Macy ped->ped_l3_slice = strtol(value, NULL, 16); 296959826caSMatt Macy else { 297959826caSMatt Macy debug = getenv("PMUDEBUG"); 298959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL) 299959826caSMatt Macy printf("unrecognized kvpair: %s:%s\n", key, value); 300959826caSMatt Macy } 301959826caSMatt Macy } 3028bed125dSMatt Macy free(r); 303959826caSMatt Macy return (0); 304959826caSMatt Macy } 305959826caSMatt Macy 306959826caSMatt Macy uint64_t 307959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name) 308959826caSMatt Macy { 309959826caSMatt Macy const struct pmu_event *pe; 310959826caSMatt Macy struct pmu_event_desc ped; 311959826caSMatt Macy 312959826caSMatt Macy event_name = pmu_alias_get(event_name); 313bfb46e2bSMatt Macy if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL) 314959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 315bfb46e2bSMatt Macy if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, NULL)) == NULL) 316959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 317959826caSMatt Macy if (pe->event == NULL) 318959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 319959826caSMatt Macy if (pmu_parse_event(&ped, pe->event)) 320959826caSMatt Macy return (DEFAULT_SAMPLE_COUNT); 321959826caSMatt Macy return (ped.ped_period); 322959826caSMatt Macy } 323959826caSMatt Macy 324959826caSMatt Macy int 325959826caSMatt Macy pmc_pmu_enabled(void) 326959826caSMatt Macy { 327959826caSMatt Macy 328bfb46e2bSMatt Macy return (pmu_events_map_get(NULL) != NULL); 329959826caSMatt Macy } 330959826caSMatt Macy 331959826caSMatt Macy void 332fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name) 333959826caSMatt Macy { 334959826caSMatt Macy const struct pmu_events_map *pme; 335959826caSMatt Macy const struct pmu_event *pe; 336959826caSMatt Macy struct pmu_event_desc ped; 337959826caSMatt Macy char *debug; 338959826caSMatt Macy int do_debug; 339959826caSMatt Macy 340959826caSMatt Macy debug = getenv("PMUDEBUG"); 341959826caSMatt Macy do_debug = 0; 342959826caSMatt Macy 343959826caSMatt Macy if (debug != NULL && strcmp(debug, "true") == 0) 344959826caSMatt Macy do_debug = 1; 345bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 346959826caSMatt Macy return; 347959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 348959826caSMatt Macy if (pe->name == NULL) 349959826caSMatt Macy continue; 350fbf962e6SMatt Macy if (event_name != NULL && strcasestr(pe->name, event_name) == NULL) 351fbf962e6SMatt Macy continue; 352959826caSMatt Macy printf("\t%s\n", pe->name); 353959826caSMatt Macy if (do_debug) 354959826caSMatt Macy pmu_parse_event(&ped, pe->event); 355959826caSMatt Macy } 356959826caSMatt Macy } 357959826caSMatt Macy 358959826caSMatt Macy void 359959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev) 360959826caSMatt Macy { 361959826caSMatt Macy const struct pmu_events_map *pme; 362959826caSMatt Macy const struct pmu_event *pe; 363959826caSMatt Macy 364bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 365959826caSMatt Macy return; 366959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 367959826caSMatt Macy if (pe->name == NULL) 368959826caSMatt Macy continue; 369959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL && 370959826caSMatt Macy pe->desc != NULL) 371959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 372959826caSMatt Macy } 373959826caSMatt Macy } 374959826caSMatt Macy 375959826caSMatt Macy void 376959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev) 377959826caSMatt Macy { 378959826caSMatt Macy const struct pmu_events_map *pme; 379959826caSMatt Macy const struct pmu_event *pe; 380959826caSMatt Macy 381bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 382959826caSMatt Macy return; 383959826caSMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 384959826caSMatt Macy if (pe->name == NULL) 385959826caSMatt Macy continue; 386959826caSMatt Macy if (strcasestr(pe->name, ev) != NULL) { 387959826caSMatt Macy if (pe->long_desc != NULL) 388959826caSMatt Macy printf("%s:\n%s\n", pe->name, pe->long_desc); 389959826caSMatt Macy else if (pe->desc != NULL) 390959826caSMatt Macy printf("%s:\t%s\n", pe->name, pe->desc); 391959826caSMatt Macy } 392959826caSMatt Macy } 393959826caSMatt Macy } 394959826caSMatt Macy 395fbf962e6SMatt Macy void 396fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev) 397fbf962e6SMatt Macy { 398fbf962e6SMatt Macy const struct pmu_events_map *pme; 399fbf962e6SMatt Macy const struct pmu_event *pe; 400fbf962e6SMatt Macy 401bfb46e2bSMatt Macy if ((pme = pmu_events_map_get(NULL)) == NULL) 402fbf962e6SMatt Macy return; 403fbf962e6SMatt Macy for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) { 404fbf962e6SMatt Macy if (pe->name == NULL) 405fbf962e6SMatt Macy continue; 406fbf962e6SMatt Macy if (strcasestr(pe->name, ev) == NULL) 407fbf962e6SMatt Macy continue; 408fbf962e6SMatt Macy printf("name: %s\n", pe->name); 409fbf962e6SMatt Macy if (pe->long_desc != NULL) 410fbf962e6SMatt Macy printf("desc: %s\n", pe->long_desc); 411fbf962e6SMatt Macy else if (pe->desc != NULL) 412fbf962e6SMatt Macy printf("desc: %s\n", pe->desc); 413fbf962e6SMatt Macy if (pe->event != NULL) 414fbf962e6SMatt Macy printf("event: %s\n", pe->event); 415fbf962e6SMatt Macy if (pe->topic != NULL) 416fbf962e6SMatt Macy printf("topic: %s\n", pe->topic); 417fbf962e6SMatt Macy if (pe->pmu != NULL) 418fbf962e6SMatt Macy printf("pmu: %s\n", pe->pmu); 419fbf962e6SMatt Macy if (pe->unit != NULL) 420fbf962e6SMatt Macy printf("unit: %s\n", pe->unit); 421fbf962e6SMatt Macy if (pe->perpkg != NULL) 422fbf962e6SMatt Macy printf("perpkg: %s\n", pe->perpkg); 423fbf962e6SMatt Macy if (pe->metric_expr != NULL) 424fbf962e6SMatt Macy printf("metric_expr: %s\n", pe->metric_expr); 425fbf962e6SMatt Macy if (pe->metric_name != NULL) 426fbf962e6SMatt Macy printf("metric_name: %s\n", pe->metric_name); 427fbf962e6SMatt Macy if (pe->metric_group != NULL) 428fbf962e6SMatt Macy printf("metric_group: %s\n", pe->metric_group); 429fbf962e6SMatt Macy } 430fbf962e6SMatt Macy } 431fbf962e6SMatt Macy 43281eb4dcfSMatt Macy static int 433dacc43dfSMatt Macy pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 43481eb4dcfSMatt Macy struct pmu_event_desc *ped) 435959826caSMatt Macy { 43681eb4dcfSMatt Macy struct pmc_md_amd_op_pmcallocate *amd; 437dacc43dfSMatt Macy const struct pmu_event *pe; 438dacc43dfSMatt Macy int idx = -1; 43981eb4dcfSMatt Macy 44081eb4dcfSMatt Macy amd = &pm->pm_md.pm_amd; 44181eb4dcfSMatt Macy if (ped->ped_umask > 0) { 44281eb4dcfSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 44381eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask); 44481eb4dcfSMatt Macy } 44581eb4dcfSMatt Macy pm->pm_class = PMC_CLASS_K8; 446dacc43dfSMatt Macy pe = pmu_event_get(NULL, event_name, &idx); 44781eb4dcfSMatt Macy 448dacc43dfSMatt Macy if (strcmp("l3cache", pe->topic) == 0){ 449dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 450dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE; 451dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice); 452dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread); 453dacc43dfSMatt Macy } 454dacc43dfSMatt Macy else if (strcmp("data fabric", pe->topic) == 0){ 455dacc43dfSMatt Macy 456dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event); 457dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC; 458dacc43dfSMatt Macy } 459dacc43dfSMatt Macy else{ 460dacc43dfSMatt Macy amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event); 461dacc43dfSMatt Macy amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE; 46281eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 46381eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 46481eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 46581eb4dcfSMatt Macy amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS); 46681eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 46781eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_USR; 46881eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 46981eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_OS; 47081eb4dcfSMatt Macy if (ped->ped_edge) 47181eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 47281eb4dcfSMatt Macy if (ped->ped_inv) 47381eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_EDGE; 47481eb4dcfSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 47581eb4dcfSMatt Macy amd->pm_amd_config |= AMD_PMC_INT; 476dacc43dfSMatt Macy } 47781eb4dcfSMatt Macy return (0); 47881eb4dcfSMatt Macy } 47981eb4dcfSMatt Macy 48081eb4dcfSMatt Macy static int 48181eb4dcfSMatt Macy pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm, 48281eb4dcfSMatt Macy struct pmu_event_desc *ped) 48381eb4dcfSMatt Macy { 484959826caSMatt Macy struct pmc_md_iap_op_pmcallocate *iap; 48581eb4dcfSMatt Macy int isfixed; 486959826caSMatt Macy 487a191ed2dSMatt Macy isfixed = 0; 48881eb4dcfSMatt Macy iap = &pm->pm_md.pm_iap; 48907d80fd8SMatt Macy if (strcasestr(event_name, "UNC_") == event_name || 490785dd70dSMatt Macy strcasestr(event_name, "uncore") != NULL) { 491785dd70dSMatt Macy pm->pm_class = PMC_CLASS_UCP; 492a191ed2dSMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 49381eb4dcfSMatt Macy } else if ((ped->ped_umask == -1) || 49481eb4dcfSMatt Macy (ped->ped_event == 0x0 && ped->ped_umask == 0x3)) { 49507d80fd8SMatt Macy pm->pm_class = PMC_CLASS_IAF; 49607d80fd8SMatt Macy } else { 497959826caSMatt Macy pm->pm_class = PMC_CLASS_IAP; 49807d80fd8SMatt Macy pm->pm_caps |= PMC_CAP_QUALIFIER; 499785dd70dSMatt Macy } 50081eb4dcfSMatt Macy iap->pm_iap_config |= IAP_EVSEL(ped->ped_event); 50181eb4dcfSMatt Macy if (ped->ped_umask > 0) 50281eb4dcfSMatt Macy iap->pm_iap_config |= IAP_UMASK(ped->ped_umask); 50381eb4dcfSMatt Macy iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask); 50481eb4dcfSMatt Macy iap->pm_iap_rsp = ped->ped_offcore_rsp; 505959826caSMatt Macy 50681eb4dcfSMatt Macy if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 || 50781eb4dcfSMatt Macy (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 50881eb4dcfSMatt Macy (PMC_CAP_USER|PMC_CAP_SYSTEM)) 509959826caSMatt Macy iap->pm_iap_config |= (IAP_USR | IAP_OS); 51081eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_USER) 51181eb4dcfSMatt Macy iap->pm_iap_config |= IAP_USR; 51281eb4dcfSMatt Macy else if (pm->pm_caps & PMC_CAP_SYSTEM) 51381eb4dcfSMatt Macy iap->pm_iap_config |= IAP_OS; 51481eb4dcfSMatt Macy if (ped->ped_edge) 515959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 51681eb4dcfSMatt Macy if (ped->ped_any) 517959826caSMatt Macy iap->pm_iap_config |= IAP_ANY; 51881eb4dcfSMatt Macy if (ped->ped_inv) 519959826caSMatt Macy iap->pm_iap_config |= IAP_EDGE; 520959826caSMatt Macy if (pm->pm_caps & PMC_CAP_INTERRUPT) 521959826caSMatt Macy iap->pm_iap_config |= IAP_INT; 522959826caSMatt Macy return (0); 523959826caSMatt Macy } 524959826caSMatt Macy 52581eb4dcfSMatt Macy int 52681eb4dcfSMatt Macy pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm) 52781eb4dcfSMatt Macy { 52881eb4dcfSMatt Macy const struct pmu_event *pe; 52981eb4dcfSMatt Macy struct pmu_event_desc ped; 53081eb4dcfSMatt Macy pmu_mfr_t mfr; 53181eb4dcfSMatt Macy int idx = -1; 53281eb4dcfSMatt Macy 53381eb4dcfSMatt Macy if ((mfr = pmu_events_mfr()) == PMU_INVALID) 53481eb4dcfSMatt Macy return (ENOENT); 53581eb4dcfSMatt Macy 53681eb4dcfSMatt Macy bzero(&pm->pm_md, sizeof(pm->pm_md)); 53781eb4dcfSMatt Macy pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE); 53881eb4dcfSMatt Macy event_name = pmu_alias_get(event_name); 53981eb4dcfSMatt Macy if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL) 54081eb4dcfSMatt Macy return (ENOENT); 54181eb4dcfSMatt Macy if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, &idx)) == NULL) 54281eb4dcfSMatt Macy return (ENOENT); 54381eb4dcfSMatt Macy assert(idx >= 0); 54481eb4dcfSMatt Macy pm->pm_ev = idx; 54581eb4dcfSMatt Macy 54681eb4dcfSMatt Macy if (pe->event == NULL) 54781eb4dcfSMatt Macy return (ENOENT); 54881eb4dcfSMatt Macy if (pmu_parse_event(&ped, pe->event)) 54981eb4dcfSMatt Macy return (ENOENT); 55081eb4dcfSMatt Macy 55181eb4dcfSMatt Macy if (mfr == PMU_INTEL) 55281eb4dcfSMatt Macy return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped)); 55381eb4dcfSMatt Macy else 55481eb4dcfSMatt Macy return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped)); 55581eb4dcfSMatt Macy } 55681eb4dcfSMatt Macy 55778beed2fSMatt Macy /* 55878beed2fSMatt Macy * Ultimately rely on AMD calling theirs the same 55978beed2fSMatt Macy */ 56078beed2fSMatt Macy static const char *stat_mode_cntrs[] = { 561ee62e0b4SMatt Macy "cpu_clk_unhalted.thread", 56278beed2fSMatt Macy "inst_retired.any", 56378beed2fSMatt Macy "br_inst_retired.all_branches", 56478beed2fSMatt Macy "br_misp_retired.all_branches", 565a191ed2dSMatt Macy "longest_lat_cache.reference", 566a191ed2dSMatt Macy "longest_lat_cache.miss", 56778beed2fSMatt Macy }; 56878beed2fSMatt Macy 56978beed2fSMatt Macy int 57078beed2fSMatt Macy pmc_pmu_stat_mode(const char ***cntrs) 57178beed2fSMatt Macy { 57278beed2fSMatt Macy if (pmc_pmu_enabled()) { 57378beed2fSMatt Macy *cntrs = stat_mode_cntrs; 57478beed2fSMatt Macy return (0); 57578beed2fSMatt Macy } 57678beed2fSMatt Macy return (EOPNOTSUPP); 57778beed2fSMatt Macy } 57878beed2fSMatt Macy 579959826caSMatt Macy #else 5808b20f975SMatt Macy 5818b20f975SMatt Macy uint64_t 5828b20f975SMatt Macy pmc_pmu_sample_rate_get(const char *event_name __unused) 5838b20f975SMatt Macy { 5848b20f975SMatt Macy return (DEFAULT_SAMPLE_COUNT); 5858b20f975SMatt Macy } 5868b20f975SMatt Macy 5878b20f975SMatt Macy void 588fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name __unused) 5898b20f975SMatt Macy { 5908b20f975SMatt Macy } 5918b20f975SMatt Macy 5928b20f975SMatt Macy void 5938b20f975SMatt Macy pmc_pmu_print_counter_desc(const char *e __unused) 5948b20f975SMatt Macy { 5958b20f975SMatt Macy } 5968b20f975SMatt Macy 5978b20f975SMatt Macy void 5988b20f975SMatt Macy pmc_pmu_print_counter_desc_long(const char *e __unused) 5998b20f975SMatt Macy { 6008b20f975SMatt Macy } 6018b20f975SMatt Macy 602fbf962e6SMatt Macy void 603fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *e __unused) 604fbf962e6SMatt Macy { 605fbf962e6SMatt Macy 606fbf962e6SMatt Macy } 607fbf962e6SMatt Macy 6088b20f975SMatt Macy int 6098b20f975SMatt Macy pmc_pmu_enabled(void) 6108b20f975SMatt Macy { 6118b20f975SMatt Macy return (0); 6128b20f975SMatt Macy } 6138b20f975SMatt Macy 6148b20f975SMatt Macy int 6158b20f975SMatt Macy pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused) 6168b20f975SMatt Macy { 6178b20f975SMatt Macy return (EOPNOTSUPP); 6188b20f975SMatt Macy } 6198b20f975SMatt Macy 6208b20f975SMatt Macy const char * 621b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *c __unused, int idx __unused) 6228b20f975SMatt Macy { 6238b20f975SMatt Macy return (NULL); 6248b20f975SMatt Macy } 625bfb46e2bSMatt Macy 6268b20f975SMatt Macy int 6278b20f975SMatt Macy pmc_pmu_stat_mode(const char ***a __unused) 6288b20f975SMatt Macy { 6298b20f975SMatt Macy return (EOPNOTSUPP); 6308b20f975SMatt Macy } 631959826caSMatt Macy 632bfb46e2bSMatt Macy int 633ce6fc9d7SMatt Macy pmc_pmu_idx_get_by_event(const char *c __unused, const char *e __unused) 634bfb46e2bSMatt Macy { 635bfb46e2bSMatt Macy return (-1); 636bfb46e2bSMatt Macy } 637bfb46e2bSMatt Macy 638959826caSMatt Macy #endif 639