2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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
35 #include <sys/types.h>
36 #include <sys/errno.h>
38 #include <sys/sysctl.h>
47 #include <libpmcstat.h>
48 #include "pmu-events/pmu-events.h"
55 #if defined(__amd64__) || defined(__i386__)
62 static struct pmu_alias pmu_intel_alias_table[] = {
63 {"UNHALTED_CORE_CYCLES", "cpu_clk_unhalted.thread"},
64 {"UNHALTED-CORE-CYCLES", "cpu_clk_unhalted.thread"},
65 {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
66 {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
67 {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
68 {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
69 {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
70 {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
71 {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
72 {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
73 {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
74 {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
75 {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
76 {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
77 {"unhalted-cycles", "cpu_clk_unhalted.thread"},
78 {"instructions", "inst_retired.any"},
79 {"branch-mispredicts", "br_misp_retired.all_branches"},
80 {"branches", "br_inst_retired.all_branches"},
81 {"interrupts", "hw_interrupts.received"},
82 {"ic-misses", "frontend_retired.l1i_miss"},
86 static struct pmu_alias pmu_amd_alias_table[] = {
87 {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"},
88 {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"},
89 {"LLC_MISSES", "l3_comb_clstr_state.request_miss"},
90 {"LLC-MISSES", "l3_comb_clstr_state.request_miss"},
91 {"LLC_REFERENCE", "l3_request_g1.caching_l3_cache_accesses"},
92 {"LLC-REFERENCE", "l3_request_g1.caching_l3_cache_accesses"},
93 {"BRANCH_INSTRUCTION_RETIRED", "ex_ret_brn"},
94 {"BRANCH-INSTRUCTION-RETIRED", "ex_ret_brn"},
95 {"BRANCH_MISSES_RETIRED", "ex_ret_brn_misp"},
96 {"BRANCH-MISSES-RETIRED", "ex_ret_brn_misp"},
97 {"unhalted-cycles", "ls_not_halted_cyc"},
98 {"instructions", "ex_ret_instr",},
99 {"branch-mispredicts", "ex_ret_brn_misp"},
100 {"branches", "ex_ret_brn"},
101 {"interrupts", "ls_int_taken"}, /* Not on amdzen1 */
109 char buf[PMC_CPUID_LEN];
110 size_t s = sizeof(buf);
113 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
114 (void *)NULL, 0) == -1)
115 return (PMU_INVALID);
116 if (strcasestr(buf, "AuthenticAMD") != NULL ||
117 strcasestr(buf, "HygonGenuine") != NULL)
119 else if (strcasestr(buf, "GenuineIntel") != NULL)
127 * The Intel fixed mode counters are:
128 * "inst_retired.any",
129 * "cpu_clk_unhalted.thread",
130 * "cpu_clk_unhalted.thread_any",
131 * "cpu_clk_unhalted.ref_tsc",
136 pmu_alias_get(const char *name)
139 struct pmu_alias *pa;
140 struct pmu_alias *pmu_alias_table;
142 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
145 pmu_alias_table = pmu_amd_alias_table;
146 else if (mfr == PMU_INTEL)
147 pmu_alias_table = pmu_intel_alias_table;
151 for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
152 if (strcasecmp(name, pa->pa_alias) == 0)
153 return (pa->pa_name);
158 #elif defined(__aarch64__)
160 static struct pmu_alias pmu_armv8_alias_table[] = {
161 {"UNHALTED_CORE_CYCLES", "CPU_CYCLES"},
162 {"UNHALTED-CORE-CYCLES", "CPU_CYCLES"},
163 {"LLC_MISSES", "LL_CACHE_MISS_RD"},
164 {"LLC-MISSES", "LL_CACHE_MISS_RD"},
165 {"LLC_REFERENCE", "LL_CACHE_RD"},
166 {"LLC-REFERENCE", "LL_CACHE_RD"},
167 {"BRANCH_INSTRUCTION_RETIRED", "BR_RETIRED"},
168 {"BRANCH-INSTRUCTION-RETIRED", "BR_RETIRED"},
169 {"BRANCH_MISSES_RETIRED", "BR_MIS_PRED_RETIRED"},
170 {"BRANCH-MISSES-RETIRED", "BR_MIS_PRED_RETIRED"},
171 {"unhalted-cycles", "CPU_CYCLES"},
172 {"instructions", "INST_RETIRED",},
173 {"branch-mispredicts", "BR_MIS_PRED_RETIRED"},
174 {"branches", "BR_RETIRED"},
175 {"interrupts", "EXC_IRQ"},
180 pmu_alias_get(const char *name)
182 struct pmu_alias *pa;
184 for (pa = pmu_armv8_alias_table; pa->pa_alias != NULL; pa++)
185 if (strcasecmp(name, pa->pa_alias) == 0)
186 return (pa->pa_name);
194 pmu_alias_get(const char *name)
201 struct pmu_event_desc {
203 uint64_t ped_offcore_rsp;
204 uint64_t ped_l3_thread;
205 uint64_t ped_l3_slice;
207 uint32_t ped_frontend;
209 uint32_t ped_config1;
219 static const struct pmu_events_map *
220 pmu_events_map_get(const char *cpuid)
223 regmatch_t pmatch[1];
224 char buf[PMC_CPUID_LEN];
225 size_t s = sizeof(buf);
227 const struct pmu_events_map *pme;
230 strlcpy(buf, cpuid, s);
232 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
233 (void *)NULL, 0) == -1)
236 for (pme = pmu_events_map; pme->cpuid != NULL; pme++) {
237 if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) {
238 printf("regex '%s' failed to compile, ignoring\n",
242 match = regexec(&re, buf, 1, pmatch, 0);
245 if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0
246 || buf[pmatch[0].rm_eo] == '-'))
253 static const struct pmu_event *
254 pmu_event_get(const char *cpuid, const char *event_name, int *idx)
256 const struct pmu_events_map *pme;
257 const struct pmu_event *pe;
260 if ((pme = pmu_events_map_get(cpuid)) == NULL)
262 for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
263 if (pe->name == NULL)
265 if (strcasecmp(pe->name, event_name) == 0) {
275 pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
278 const char *realname;
280 realname = pmu_alias_get(event);
281 if (pmu_event_get(cpuid, realname, &idx) == NULL)
287 pmc_pmu_event_get_by_idx(const char *cpuid, int idx)
289 const struct pmu_events_map *pme;
291 if ((pme = pmu_events_map_get(cpuid)) == NULL)
293 assert(pme->table[idx].name);
294 return (pme->table[idx].name);
298 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
301 char *kvp, *key, *value, *r;
304 if ((event = strdup(eventin)) == NULL)
307 bzero(ped, sizeof(*ped));
308 ped->ped_period = DEFAULT_SAMPLE_COUNT;
310 while ((kvp = strsep(&event, ",")) != NULL) {
311 key = strsep(&kvp, "=");
315 if (strcmp(key, "umask") == 0)
316 ped->ped_umask = strtol(value, NULL, 16);
317 else if (strcmp(key, "event") == 0)
318 ped->ped_event = strtol(value, NULL, 16);
319 else if (strcmp(key, "period") == 0)
320 ped->ped_period = strtol(value, NULL, 10);
321 else if (strcmp(key, "offcore_rsp") == 0)
322 ped->ped_offcore_rsp = strtol(value, NULL, 16);
323 else if (strcmp(key, "any") == 0)
324 ped->ped_any = strtol(value, NULL, 10);
325 else if (strcmp(key, "cmask") == 0)
326 ped->ped_cmask = strtol(value, NULL, 10);
327 else if (strcmp(key, "inv") == 0)
328 ped->ped_inv = strtol(value, NULL, 10);
329 else if (strcmp(key, "edge") == 0)
330 ped->ped_edge = strtol(value, NULL, 10);
331 else if (strcmp(key, "frontend") == 0)
332 ped->ped_frontend = strtol(value, NULL, 16);
333 else if (strcmp(key, "ldlat") == 0)
334 ped->ped_ldlat = strtol(value, NULL, 16);
335 else if (strcmp(key, "fc_mask") == 0)
336 ped->ped_fc_mask = strtol(value, NULL, 16);
337 else if (strcmp(key, "ch_mask") == 0)
338 ped->ped_ch_mask = strtol(value, NULL, 16);
339 else if (strcmp(key, "config1") == 0)
340 ped->ped_config1 = strtol(value, NULL, 16);
341 else if (strcmp(key, "l3_thread_mask") == 0)
342 ped->ped_l3_thread = strtol(value, NULL, 16);
343 else if (strcmp(key, "l3_slice_mask") == 0)
344 ped->ped_l3_slice = strtol(value, NULL, 16);
346 debug = getenv("PMUDEBUG");
347 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
348 printf("unrecognized kvpair: %s:%s\n", key, value);
356 pmc_pmu_sample_rate_get(const char *event_name)
358 const struct pmu_event *pe;
359 struct pmu_event_desc ped;
361 event_name = pmu_alias_get(event_name);
362 if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
363 return (DEFAULT_SAMPLE_COUNT);
364 if (pe->event == NULL)
365 return (DEFAULT_SAMPLE_COUNT);
366 if (pmu_parse_event(&ped, pe->event))
367 return (DEFAULT_SAMPLE_COUNT);
368 return (ped.ped_period);
372 pmc_pmu_enabled(void)
375 return (pmu_events_map_get(NULL) != NULL);
379 pmc_pmu_print_counters(const char *event_name)
381 const struct pmu_events_map *pme;
382 const struct pmu_event *pe;
383 struct pmu_event_desc ped;
387 debug = getenv("PMUDEBUG");
390 if (debug != NULL && strcmp(debug, "true") == 0)
392 if ((pme = pmu_events_map_get(NULL)) == NULL)
394 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
395 if (pe->name == NULL)
397 if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
399 printf("\t%s\n", pe->name);
401 pmu_parse_event(&ped, pe->event);
406 pmc_pmu_print_counter_desc(const char *ev)
408 const struct pmu_events_map *pme;
409 const struct pmu_event *pe;
411 if ((pme = pmu_events_map_get(NULL)) == NULL)
413 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
414 if (pe->name == NULL)
416 if (strcasestr(pe->name, ev) != NULL &&
418 printf("%s:\t%s\n", pe->name, pe->desc);
423 pmc_pmu_print_counter_desc_long(const char *ev)
425 const struct pmu_events_map *pme;
426 const struct pmu_event *pe;
428 if ((pme = pmu_events_map_get(NULL)) == NULL)
430 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
431 if (pe->name == NULL)
433 if (strcasestr(pe->name, ev) != NULL) {
434 if (pe->long_desc != NULL)
435 printf("%s:\n%s\n", pe->name, pe->long_desc);
436 else if (pe->desc != NULL)
437 printf("%s:\t%s\n", pe->name, pe->desc);
443 pmc_pmu_print_counter_full(const char *ev)
445 const struct pmu_events_map *pme;
446 const struct pmu_event *pe;
448 if ((pme = pmu_events_map_get(NULL)) == NULL)
450 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
451 if (pe->name == NULL)
453 if (strcasestr(pe->name, ev) == NULL)
455 printf("name: %s\n", pe->name);
456 if (pe->long_desc != NULL)
457 printf("desc: %s\n", pe->long_desc);
458 else if (pe->desc != NULL)
459 printf("desc: %s\n", pe->desc);
460 if (pe->event != NULL)
461 printf("event: %s\n", pe->event);
462 if (pe->topic != NULL)
463 printf("topic: %s\n", pe->topic);
465 printf("pmu: %s\n", pe->pmu);
466 if (pe->unit != NULL)
467 printf("unit: %s\n", pe->unit);
468 if (pe->perpkg != NULL)
469 printf("perpkg: %s\n", pe->perpkg);
470 if (pe->metric_expr != NULL)
471 printf("metric_expr: %s\n", pe->metric_expr);
472 if (pe->metric_name != NULL)
473 printf("metric_name: %s\n", pe->metric_name);
474 if (pe->metric_group != NULL)
475 printf("metric_group: %s\n", pe->metric_group);
479 #if defined(__amd64__) || defined(__i386__)
481 pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
482 struct pmu_event_desc *ped)
484 struct pmc_md_amd_op_pmcallocate *amd;
485 const struct pmu_event *pe;
488 amd = &pm->pm_md.pm_amd;
489 if (ped->ped_umask > 0) {
490 pm->pm_caps |= PMC_CAP_QUALIFIER;
491 amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask);
493 pm->pm_class = PMC_CLASS_K8;
494 pe = pmu_event_get(NULL, event_name, &idx);
496 if (strcmp("l3cache", pe->topic) == 0){
497 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
498 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE;
499 amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice);
500 amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread);
502 else if (strcmp("data fabric", pe->topic) == 0){
504 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event);
505 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC;
508 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
509 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE;
510 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
511 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
512 (PMC_CAP_USER|PMC_CAP_SYSTEM))
513 amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS);
514 else if (pm->pm_caps & PMC_CAP_USER)
515 amd->pm_amd_config |= AMD_PMC_USR;
516 else if (pm->pm_caps & PMC_CAP_SYSTEM)
517 amd->pm_amd_config |= AMD_PMC_OS;
519 amd->pm_amd_config |= AMD_PMC_EDGE;
521 amd->pm_amd_config |= AMD_PMC_INVERT;
522 if (pm->pm_caps & PMC_CAP_INTERRUPT)
523 amd->pm_amd_config |= AMD_PMC_INT;
529 pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
530 struct pmu_event_desc *ped)
532 struct pmc_md_iap_op_pmcallocate *iap;
534 iap = &pm->pm_md.pm_iap;
535 if (strcasestr(event_name, "UNC_") == event_name ||
536 strcasestr(event_name, "uncore") != NULL) {
537 pm->pm_class = PMC_CLASS_UCP;
538 pm->pm_caps |= PMC_CAP_QUALIFIER;
539 } else if (ped->ped_event == 0x0) {
540 pm->pm_class = PMC_CLASS_IAF;
542 pm->pm_class = PMC_CLASS_IAP;
543 pm->pm_caps |= PMC_CAP_QUALIFIER;
545 iap->pm_iap_config |= IAP_EVSEL(ped->ped_event);
546 if (ped->ped_umask > 0)
547 iap->pm_iap_config |= IAP_UMASK(ped->ped_umask);
548 iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask);
549 iap->pm_iap_rsp = ped->ped_offcore_rsp;
551 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
552 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
553 (PMC_CAP_USER|PMC_CAP_SYSTEM))
554 iap->pm_iap_config |= (IAP_USR | IAP_OS);
555 else if (pm->pm_caps & PMC_CAP_USER)
556 iap->pm_iap_config |= IAP_USR;
557 else if (pm->pm_caps & PMC_CAP_SYSTEM)
558 iap->pm_iap_config |= IAP_OS;
560 iap->pm_iap_config |= IAP_EDGE;
562 iap->pm_iap_config |= IAP_ANY;
564 iap->pm_iap_config |= IAP_INV;
565 if (pm->pm_caps & PMC_CAP_INTERRUPT)
566 iap->pm_iap_config |= IAP_INT;
571 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
573 const struct pmu_event *pe;
574 struct pmu_event_desc ped;
578 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
581 bzero(&pm->pm_md, sizeof(pm->pm_md));
582 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
583 event_name = pmu_alias_get(event_name);
584 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
589 if (pe->event == NULL)
591 if (pmu_parse_event(&ped, pe->event))
594 if (mfr == PMU_INTEL)
595 return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped));
597 return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped));
600 #elif defined(__aarch64__)
603 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
605 const struct pmu_event *pe;
606 struct pmu_event_desc ped;
609 event_name = pmu_alias_get(event_name);
610 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
612 if (pe->event == NULL)
614 if (pmu_parse_event(&ped, pe->event))
619 pm->pm_md.pm_md_config = ped.ped_event;
620 pm->pm_md.pm_md_flags |= PM_MD_RAW_EVENT;
621 pm->pm_class = PMC_CLASS_ARMV8;
622 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
630 pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)