2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2018, Matthew Macy
5 * Copyright (c) 2021, The FreeBSD Foundation
7 * Portions of this software were developed by Mitchell Horne
8 * under sponsorship from the FreeBSD Foundation.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/types.h>
34 #include <sys/errno.h>
36 #include <sys/sysctl.h>
45 #include <libpmcstat.h>
46 #include "pmu-events/pmu-events.h"
53 #if defined(__amd64__) || defined(__i386__)
60 static struct pmu_alias pmu_intel_alias_table[] = {
61 {"UNHALTED_CORE_CYCLES", "cpu_clk_unhalted.thread"},
62 {"UNHALTED-CORE-CYCLES", "cpu_clk_unhalted.thread"},
63 {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
64 {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
65 {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
66 {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
67 {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
68 {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
69 {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
70 {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
71 {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
72 {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
73 {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
74 {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
75 {"unhalted-cycles", "cpu_clk_unhalted.thread"},
76 {"instructions", "inst_retired.any"},
77 {"branch-mispredicts", "br_misp_retired.all_branches"},
78 {"branches", "br_inst_retired.all_branches"},
79 {"interrupts", "hw_interrupts.received"},
80 {"ic-misses", "frontend_retired.l1i_miss"},
84 static struct pmu_alias pmu_amd_alias_table[] = {
85 {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"},
86 {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"},
87 {"LLC_MISSES", "l3_comb_clstr_state.request_miss"},
88 {"LLC-MISSES", "l3_comb_clstr_state.request_miss"},
89 {"LLC_REFERENCE", "l3_request_g1.caching_l3_cache_accesses"},
90 {"LLC-REFERENCE", "l3_request_g1.caching_l3_cache_accesses"},
91 {"BRANCH_INSTRUCTION_RETIRED", "ex_ret_brn"},
92 {"BRANCH-INSTRUCTION-RETIRED", "ex_ret_brn"},
93 {"BRANCH_MISSES_RETIRED", "ex_ret_brn_misp"},
94 {"BRANCH-MISSES-RETIRED", "ex_ret_brn_misp"},
95 {"unhalted-cycles", "ls_not_halted_cyc"},
96 {"instructions", "ex_ret_instr",},
97 {"branch-mispredicts", "ex_ret_brn_misp"},
98 {"branches", "ex_ret_brn"},
99 {"interrupts", "ls_int_taken"}, /* Not on amdzen1 */
107 char buf[PMC_CPUID_LEN];
108 size_t s = sizeof(buf);
111 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
112 (void *)NULL, 0) == -1)
113 return (PMU_INVALID);
114 if (strcasestr(buf, "AuthenticAMD") != NULL ||
115 strcasestr(buf, "HygonGenuine") != NULL)
117 else if (strcasestr(buf, "GenuineIntel") != NULL)
125 * The Intel fixed mode counters are:
126 * "inst_retired.any",
127 * "cpu_clk_unhalted.thread",
128 * "cpu_clk_unhalted.thread_any",
129 * "cpu_clk_unhalted.ref_tsc",
134 pmu_alias_get(const char *name)
137 struct pmu_alias *pa;
138 struct pmu_alias *pmu_alias_table;
140 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
143 pmu_alias_table = pmu_amd_alias_table;
144 else if (mfr == PMU_INTEL)
145 pmu_alias_table = pmu_intel_alias_table;
149 for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
150 if (strcasecmp(name, pa->pa_alias) == 0)
151 return (pa->pa_name);
155 #elif defined(__powerpc64__)
158 pmu_alias_get(const char *name)
163 #elif defined(__aarch64__)
165 static struct pmu_alias pmu_armv8_alias_table[] = {
166 {"UNHALTED_CORE_CYCLES", "CPU_CYCLES"},
167 {"UNHALTED-CORE-CYCLES", "CPU_CYCLES"},
168 {"LLC_MISSES", "LL_CACHE_MISS_RD"},
169 {"LLC-MISSES", "LL_CACHE_MISS_RD"},
170 {"LLC_REFERENCE", "LL_CACHE_RD"},
171 {"LLC-REFERENCE", "LL_CACHE_RD"},
172 {"BRANCH_INSTRUCTION_RETIRED", "BR_RETIRED"},
173 {"BRANCH-INSTRUCTION-RETIRED", "BR_RETIRED"},
174 {"BRANCH_MISSES_RETIRED", "BR_MIS_PRED_RETIRED"},
175 {"BRANCH-MISSES-RETIRED", "BR_MIS_PRED_RETIRED"},
176 {"unhalted-cycles", "CPU_CYCLES"},
177 {"instructions", "INST_RETIRED",},
178 {"branch-mispredicts", "BR_MIS_PRED_RETIRED"},
179 {"branches", "BR_RETIRED"},
180 {"interrupts", "EXC_IRQ"},
185 pmu_alias_get(const char *name)
187 struct pmu_alias *pa;
189 for (pa = pmu_armv8_alias_table; pa->pa_alias != NULL; pa++)
190 if (strcasecmp(name, pa->pa_alias) == 0)
191 return (pa->pa_name);
199 pmu_alias_get(const char *name)
206 struct pmu_event_desc {
208 uint64_t ped_offcore_rsp;
209 uint64_t ped_l3_thread;
210 uint64_t ped_l3_slice;
212 uint32_t ped_frontend;
214 uint32_t ped_config1;
224 static const struct pmu_events_map *
225 pmu_events_map_get(const char *cpuid)
228 regmatch_t pmatch[1];
229 char buf[PMC_CPUID_LEN];
230 size_t s = sizeof(buf);
232 const struct pmu_events_map *pme;
235 strlcpy(buf, cpuid, s);
237 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
238 (void *)NULL, 0) == -1)
241 for (pme = pmu_events_map; pme->cpuid != NULL; pme++) {
242 if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) {
243 printf("regex '%s' failed to compile, ignoring\n",
247 match = regexec(&re, buf, 1, pmatch, 0);
250 if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0
251 || buf[pmatch[0].rm_eo] == '-'))
258 static const struct pmu_event *
259 pmu_event_get(const char *cpuid, const char *event_name, int *idx)
261 const struct pmu_events_map *pme;
262 const struct pmu_event *pe;
265 if ((pme = pmu_events_map_get(cpuid)) == NULL)
267 for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
268 if (pe->name == NULL)
270 if (strcasecmp(pe->name, event_name) == 0) {
280 pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
283 const char *realname;
285 realname = pmu_alias_get(event);
286 if (pmu_event_get(cpuid, realname, &idx) == NULL)
292 pmc_pmu_event_get_by_idx(const char *cpuid, int idx)
294 const struct pmu_events_map *pme;
296 if ((pme = pmu_events_map_get(cpuid)) == NULL)
298 assert(pme->table[idx].name);
299 return (pme->table[idx].name);
303 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
306 char *kvp, *key, *value, *r;
309 if ((event = strdup(eventin)) == NULL)
312 bzero(ped, sizeof(*ped));
313 ped->ped_period = DEFAULT_SAMPLE_COUNT;
315 while ((kvp = strsep(&event, ",")) != NULL) {
316 key = strsep(&kvp, "=");
320 if (strcmp(key, "umask") == 0)
321 ped->ped_umask = strtol(value, NULL, 16);
322 else if (strcmp(key, "event") == 0)
323 ped->ped_event = strtol(value, NULL, 16);
324 else if (strcmp(key, "period") == 0)
325 ped->ped_period = strtol(value, NULL, 10);
326 else if (strcmp(key, "offcore_rsp") == 0)
327 ped->ped_offcore_rsp = strtol(value, NULL, 16);
328 else if (strcmp(key, "any") == 0)
329 ped->ped_any = strtol(value, NULL, 10);
330 else if (strcmp(key, "cmask") == 0)
331 ped->ped_cmask = strtol(value, NULL, 10);
332 else if (strcmp(key, "inv") == 0)
333 ped->ped_inv = strtol(value, NULL, 10);
334 else if (strcmp(key, "edge") == 0)
335 ped->ped_edge = strtol(value, NULL, 10);
336 else if (strcmp(key, "frontend") == 0)
337 ped->ped_frontend = strtol(value, NULL, 16);
338 else if (strcmp(key, "ldlat") == 0)
339 ped->ped_ldlat = strtol(value, NULL, 16);
340 else if (strcmp(key, "fc_mask") == 0)
341 ped->ped_fc_mask = strtol(value, NULL, 16);
342 else if (strcmp(key, "ch_mask") == 0)
343 ped->ped_ch_mask = strtol(value, NULL, 16);
344 else if (strcmp(key, "config1") == 0)
345 ped->ped_config1 = strtol(value, NULL, 16);
346 else if (strcmp(key, "l3_thread_mask") == 0)
347 ped->ped_l3_thread = strtol(value, NULL, 16);
348 else if (strcmp(key, "l3_slice_mask") == 0)
349 ped->ped_l3_slice = strtol(value, NULL, 16);
351 debug = getenv("PMUDEBUG");
352 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
353 printf("unrecognized kvpair: %s:%s\n", key, value);
361 pmc_pmu_sample_rate_get(const char *event_name)
363 const struct pmu_event *pe;
364 struct pmu_event_desc ped;
366 event_name = pmu_alias_get(event_name);
367 if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
368 return (DEFAULT_SAMPLE_COUNT);
369 if (pe->event == NULL)
370 return (DEFAULT_SAMPLE_COUNT);
371 if (pmu_parse_event(&ped, pe->event))
372 return (DEFAULT_SAMPLE_COUNT);
373 return (ped.ped_period);
377 pmc_pmu_enabled(void)
380 return (pmu_events_map_get(NULL) != NULL);
384 pmc_pmu_print_counters(const char *event_name)
386 const struct pmu_events_map *pme;
387 const struct pmu_event *pe;
388 struct pmu_event_desc ped;
392 debug = getenv("PMUDEBUG");
395 if (debug != NULL && strcmp(debug, "true") == 0)
397 if ((pme = pmu_events_map_get(NULL)) == NULL)
399 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
400 if (pe->name == NULL)
402 if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
404 printf("\t%s\n", pe->name);
406 pmu_parse_event(&ped, pe->event);
411 pmc_pmu_print_counter_desc(const char *ev)
413 const struct pmu_events_map *pme;
414 const struct pmu_event *pe;
416 if ((pme = pmu_events_map_get(NULL)) == NULL)
418 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
419 if (pe->name == NULL)
421 if (strcasestr(pe->name, ev) != NULL &&
423 printf("%s:\t%s\n", pe->name, pe->desc);
428 pmc_pmu_print_counter_desc_long(const char *ev)
430 const struct pmu_events_map *pme;
431 const struct pmu_event *pe;
433 if ((pme = pmu_events_map_get(NULL)) == NULL)
435 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
436 if (pe->name == NULL)
438 if (strcasestr(pe->name, ev) != NULL) {
439 if (pe->long_desc != NULL)
440 printf("%s:\n%s\n", pe->name, pe->long_desc);
441 else if (pe->desc != NULL)
442 printf("%s:\t%s\n", pe->name, pe->desc);
448 pmc_pmu_print_counter_full(const char *ev)
450 const struct pmu_events_map *pme;
451 const struct pmu_event *pe;
453 if ((pme = pmu_events_map_get(NULL)) == NULL)
455 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
456 if (pe->name == NULL)
458 if (strcasestr(pe->name, ev) == NULL)
460 printf("name: %s\n", pe->name);
461 if (pe->long_desc != NULL)
462 printf("desc: %s\n", pe->long_desc);
463 else if (pe->desc != NULL)
464 printf("desc: %s\n", pe->desc);
465 if (pe->event != NULL)
466 printf("event: %s\n", pe->event);
467 if (pe->topic != NULL)
468 printf("topic: %s\n", pe->topic);
470 printf("pmu: %s\n", pe->pmu);
471 if (pe->unit != NULL)
472 printf("unit: %s\n", pe->unit);
473 if (pe->perpkg != NULL)
474 printf("perpkg: %s\n", pe->perpkg);
475 if (pe->metric_expr != NULL)
476 printf("metric_expr: %s\n", pe->metric_expr);
477 if (pe->metric_name != NULL)
478 printf("metric_name: %s\n", pe->metric_name);
479 if (pe->metric_group != NULL)
480 printf("metric_group: %s\n", pe->metric_group);
484 #if defined(__amd64__) || defined(__i386__)
486 pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
487 struct pmu_event_desc *ped)
489 struct pmc_md_amd_op_pmcallocate *amd;
490 const struct pmu_event *pe;
493 amd = &pm->pm_md.pm_amd;
494 if (ped->ped_umask > 0) {
495 pm->pm_caps |= PMC_CAP_QUALIFIER;
496 amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask);
498 pm->pm_class = PMC_CLASS_K8;
499 pe = pmu_event_get(NULL, event_name, &idx);
501 if (strcmp("l3cache", pe->topic) == 0){
502 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
503 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE;
504 amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice);
505 amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread);
507 else if (strcmp("data fabric", pe->topic) == 0){
509 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event);
510 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC;
513 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
514 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE;
515 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
516 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
517 (PMC_CAP_USER|PMC_CAP_SYSTEM))
518 amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS);
519 else if (pm->pm_caps & PMC_CAP_USER)
520 amd->pm_amd_config |= AMD_PMC_USR;
521 else if (pm->pm_caps & PMC_CAP_SYSTEM)
522 amd->pm_amd_config |= AMD_PMC_OS;
524 amd->pm_amd_config |= AMD_PMC_EDGE;
526 amd->pm_amd_config |= AMD_PMC_INVERT;
527 if (pm->pm_caps & PMC_CAP_INTERRUPT)
528 amd->pm_amd_config |= AMD_PMC_INT;
534 pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
535 struct pmu_event_desc *ped)
537 struct pmc_md_iap_op_pmcallocate *iap;
539 iap = &pm->pm_md.pm_iap;
540 if (strcasestr(event_name, "UNC_") == event_name ||
541 strcasestr(event_name, "uncore") != NULL) {
542 pm->pm_class = PMC_CLASS_UCP;
543 pm->pm_caps |= PMC_CAP_QUALIFIER;
544 } else if (ped->ped_event == 0x0) {
545 pm->pm_class = PMC_CLASS_IAF;
547 pm->pm_class = PMC_CLASS_IAP;
548 pm->pm_caps |= PMC_CAP_QUALIFIER;
550 iap->pm_iap_config |= IAP_EVSEL(ped->ped_event);
551 if (ped->ped_umask > 0)
552 iap->pm_iap_config |= IAP_UMASK(ped->ped_umask);
553 iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask);
554 iap->pm_iap_rsp = ped->ped_offcore_rsp;
556 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
557 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
558 (PMC_CAP_USER|PMC_CAP_SYSTEM))
559 iap->pm_iap_config |= (IAP_USR | IAP_OS);
560 else if (pm->pm_caps & PMC_CAP_USER)
561 iap->pm_iap_config |= IAP_USR;
562 else if (pm->pm_caps & PMC_CAP_SYSTEM)
563 iap->pm_iap_config |= IAP_OS;
565 iap->pm_iap_config |= IAP_EDGE;
567 iap->pm_iap_config |= IAP_ANY;
569 iap->pm_iap_config |= IAP_INV;
570 if (pm->pm_caps & PMC_CAP_INTERRUPT)
571 iap->pm_iap_config |= IAP_INT;
576 pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm)
578 const struct pmu_event *pe;
579 struct pmu_event_desc ped;
583 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
586 bzero(&pm->pm_md, sizeof(pm->pm_md));
587 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
588 event_name = pmu_alias_get(event_name);
589 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
594 if (pe->event == NULL)
596 if (pmu_parse_event(&ped, pe->event))
599 if (mfr == PMU_INTEL)
600 return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped));
602 return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped));
605 #elif defined(__powerpc64__)
608 pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm)
610 const struct pmu_event *pe;
611 struct pmu_event_desc ped;
614 bzero(&pm->pm_md, sizeof(pm->pm_md));
615 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
616 event_name = pmu_alias_get(event_name);
618 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
620 if (pe->event == NULL)
622 if (pmu_parse_event(&ped, pe->event))
625 assert(ped.ped_event >= 0);
627 pm->pm_md.pm_event = ped.ped_event;
628 pm->pm_class = PMC_CLASS_POWER8;
632 #elif defined(__aarch64__)
635 pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm)
637 const struct pmu_event *pe;
638 struct pmu_event_desc ped;
641 event_name = pmu_alias_get(event_name);
642 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
644 if (pe->event == NULL)
646 if (pmu_parse_event(&ped, pe->event))
651 pm->pm_md.pm_md_config = ped.ped_event;
652 pm->pm_class = PMC_CLASS_ARMV8;
653 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
661 pmc_pmu_pmcallocate_md(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
668 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
672 error = pmc_pmu_pmcallocate_md(event_name, pm);
674 /* Reset any changes. */
682 pm->pm_flags |= PMC_F_EV_PMU;