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_P_ANY"},
64 {"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
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_P_ANY"},
78 {"instructions", "inst_retired.any_p"},
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[] = {
165 pmu_alias_get(const char *name)
167 struct pmu_alias *pa;
169 for (pa = pmu_armv8_alias_table; pa->pa_alias != NULL; pa++)
170 if (strcasecmp(name, pa->pa_alias) == 0)
171 return (pa->pa_name);
179 pmu_alias_get(const char *name)
186 struct pmu_event_desc {
188 uint64_t ped_offcore_rsp;
189 uint64_t ped_l3_thread;
190 uint64_t ped_l3_slice;
192 uint32_t ped_frontend;
194 uint32_t ped_config1;
204 static const struct pmu_events_map *
205 pmu_events_map_get(const char *cpuid)
208 regmatch_t pmatch[1];
209 char buf[PMC_CPUID_LEN];
210 size_t s = sizeof(buf);
212 const struct pmu_events_map *pme;
215 strlcpy(buf, cpuid, s);
217 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
218 (void *)NULL, 0) == -1)
221 for (pme = pmu_events_map; pme->cpuid != NULL; pme++) {
222 if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) {
223 printf("regex '%s' failed to compile, ignoring\n",
227 match = regexec(&re, buf, 1, pmatch, 0);
230 if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0
231 || buf[pmatch[0].rm_eo] == '-'))
238 static const struct pmu_event *
239 pmu_event_get(const char *cpuid, const char *event_name, int *idx)
241 const struct pmu_events_map *pme;
242 const struct pmu_event *pe;
245 if ((pme = pmu_events_map_get(cpuid)) == NULL)
247 for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
248 if (pe->name == NULL)
250 if (strcasecmp(pe->name, event_name) == 0) {
260 pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
263 const char *realname;
265 realname = pmu_alias_get(event);
266 if (pmu_event_get(cpuid, realname, &idx) == NULL)
272 pmc_pmu_event_get_by_idx(const char *cpuid, int idx)
274 const struct pmu_events_map *pme;
276 if ((pme = pmu_events_map_get(cpuid)) == NULL)
278 assert(pme->table[idx].name);
279 return (pme->table[idx].name);
283 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
286 char *kvp, *key, *value, *r;
289 if ((event = strdup(eventin)) == NULL)
292 bzero(ped, sizeof(*ped));
293 ped->ped_period = DEFAULT_SAMPLE_COUNT;
295 while ((kvp = strsep(&event, ",")) != NULL) {
296 key = strsep(&kvp, "=");
300 if (strcmp(key, "umask") == 0)
301 ped->ped_umask = strtol(value, NULL, 16);
302 else if (strcmp(key, "event") == 0)
303 ped->ped_event = strtol(value, NULL, 16);
304 else if (strcmp(key, "period") == 0)
305 ped->ped_period = strtol(value, NULL, 10);
306 else if (strcmp(key, "offcore_rsp") == 0)
307 ped->ped_offcore_rsp = strtol(value, NULL, 16);
308 else if (strcmp(key, "any") == 0)
309 ped->ped_any = strtol(value, NULL, 10);
310 else if (strcmp(key, "cmask") == 0)
311 ped->ped_cmask = strtol(value, NULL, 10);
312 else if (strcmp(key, "inv") == 0)
313 ped->ped_inv = strtol(value, NULL, 10);
314 else if (strcmp(key, "edge") == 0)
315 ped->ped_edge = strtol(value, NULL, 10);
316 else if (strcmp(key, "frontend") == 0)
317 ped->ped_frontend = strtol(value, NULL, 16);
318 else if (strcmp(key, "ldlat") == 0)
319 ped->ped_ldlat = strtol(value, NULL, 16);
320 else if (strcmp(key, "fc_mask") == 0)
321 ped->ped_fc_mask = strtol(value, NULL, 16);
322 else if (strcmp(key, "ch_mask") == 0)
323 ped->ped_ch_mask = strtol(value, NULL, 16);
324 else if (strcmp(key, "config1") == 0)
325 ped->ped_config1 = strtol(value, NULL, 16);
326 else if (strcmp(key, "l3_thread_mask") == 0)
327 ped->ped_l3_thread = strtol(value, NULL, 16);
328 else if (strcmp(key, "l3_slice_mask") == 0)
329 ped->ped_l3_slice = strtol(value, NULL, 16);
331 debug = getenv("PMUDEBUG");
332 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
333 printf("unrecognized kvpair: %s:%s\n", key, value);
341 pmc_pmu_sample_rate_get(const char *event_name)
343 const struct pmu_event *pe;
344 struct pmu_event_desc ped;
346 event_name = pmu_alias_get(event_name);
347 if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
348 return (DEFAULT_SAMPLE_COUNT);
349 if (pe->event == NULL)
350 return (DEFAULT_SAMPLE_COUNT);
351 if (pmu_parse_event(&ped, pe->event))
352 return (DEFAULT_SAMPLE_COUNT);
353 return (ped.ped_period);
357 pmc_pmu_enabled(void)
360 return (pmu_events_map_get(NULL) != NULL);
364 pmc_pmu_print_counters(const char *event_name)
366 const struct pmu_events_map *pme;
367 const struct pmu_event *pe;
368 struct pmu_event_desc ped;
372 debug = getenv("PMUDEBUG");
375 if (debug != NULL && strcmp(debug, "true") == 0)
377 if ((pme = pmu_events_map_get(NULL)) == NULL)
379 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
380 if (pe->name == NULL)
382 if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
384 printf("\t%s\n", pe->name);
386 pmu_parse_event(&ped, pe->event);
391 pmc_pmu_print_counter_desc(const char *ev)
393 const struct pmu_events_map *pme;
394 const struct pmu_event *pe;
396 if ((pme = pmu_events_map_get(NULL)) == NULL)
398 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
399 if (pe->name == NULL)
401 if (strcasestr(pe->name, ev) != NULL &&
403 printf("%s:\t%s\n", pe->name, pe->desc);
408 pmc_pmu_print_counter_desc_long(const char *ev)
410 const struct pmu_events_map *pme;
411 const struct pmu_event *pe;
413 if ((pme = pmu_events_map_get(NULL)) == NULL)
415 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
416 if (pe->name == NULL)
418 if (strcasestr(pe->name, ev) != NULL) {
419 if (pe->long_desc != NULL)
420 printf("%s:\n%s\n", pe->name, pe->long_desc);
421 else if (pe->desc != NULL)
422 printf("%s:\t%s\n", pe->name, pe->desc);
428 pmc_pmu_print_counter_full(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)
440 printf("name: %s\n", pe->name);
441 if (pe->long_desc != NULL)
442 printf("desc: %s\n", pe->long_desc);
443 else if (pe->desc != NULL)
444 printf("desc: %s\n", pe->desc);
445 if (pe->event != NULL)
446 printf("event: %s\n", pe->event);
447 if (pe->topic != NULL)
448 printf("topic: %s\n", pe->topic);
450 printf("pmu: %s\n", pe->pmu);
451 if (pe->unit != NULL)
452 printf("unit: %s\n", pe->unit);
453 if (pe->perpkg != NULL)
454 printf("perpkg: %s\n", pe->perpkg);
455 if (pe->metric_expr != NULL)
456 printf("metric_expr: %s\n", pe->metric_expr);
457 if (pe->metric_name != NULL)
458 printf("metric_name: %s\n", pe->metric_name);
459 if (pe->metric_group != NULL)
460 printf("metric_group: %s\n", pe->metric_group);
464 #if defined(__amd64__) || defined(__i386__)
466 pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
467 struct pmu_event_desc *ped)
469 struct pmc_md_amd_op_pmcallocate *amd;
470 const struct pmu_event *pe;
473 amd = &pm->pm_md.pm_amd;
474 if (ped->ped_umask > 0) {
475 pm->pm_caps |= PMC_CAP_QUALIFIER;
476 amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask);
478 pm->pm_class = PMC_CLASS_K8;
479 pe = pmu_event_get(NULL, event_name, &idx);
481 if (strcmp("l3cache", pe->topic) == 0){
482 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
483 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE;
484 amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice);
485 amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread);
487 else if (strcmp("data fabric", pe->topic) == 0){
489 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event);
490 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC;
493 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
494 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE;
495 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
496 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
497 (PMC_CAP_USER|PMC_CAP_SYSTEM))
498 amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS);
499 else if (pm->pm_caps & PMC_CAP_USER)
500 amd->pm_amd_config |= AMD_PMC_USR;
501 else if (pm->pm_caps & PMC_CAP_SYSTEM)
502 amd->pm_amd_config |= AMD_PMC_OS;
504 amd->pm_amd_config |= AMD_PMC_EDGE;
506 amd->pm_amd_config |= AMD_PMC_EDGE;
507 if (pm->pm_caps & PMC_CAP_INTERRUPT)
508 amd->pm_amd_config |= AMD_PMC_INT;
514 pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
515 struct pmu_event_desc *ped)
517 struct pmc_md_iap_op_pmcallocate *iap;
519 iap = &pm->pm_md.pm_iap;
520 if (strcasestr(event_name, "UNC_") == event_name ||
521 strcasestr(event_name, "uncore") != NULL) {
522 pm->pm_class = PMC_CLASS_UCP;
523 pm->pm_caps |= PMC_CAP_QUALIFIER;
524 } else if (ped->ped_event == 0x0) {
525 pm->pm_class = PMC_CLASS_IAF;
527 pm->pm_class = PMC_CLASS_IAP;
528 pm->pm_caps |= PMC_CAP_QUALIFIER;
530 iap->pm_iap_config |= IAP_EVSEL(ped->ped_event);
531 if (ped->ped_umask > 0)
532 iap->pm_iap_config |= IAP_UMASK(ped->ped_umask);
533 iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask);
534 iap->pm_iap_rsp = ped->ped_offcore_rsp;
536 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
537 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
538 (PMC_CAP_USER|PMC_CAP_SYSTEM))
539 iap->pm_iap_config |= (IAP_USR | IAP_OS);
540 else if (pm->pm_caps & PMC_CAP_USER)
541 iap->pm_iap_config |= IAP_USR;
542 else if (pm->pm_caps & PMC_CAP_SYSTEM)
543 iap->pm_iap_config |= IAP_OS;
545 iap->pm_iap_config |= IAP_EDGE;
547 iap->pm_iap_config |= IAP_ANY;
549 iap->pm_iap_config |= IAP_EDGE;
550 if (pm->pm_caps & PMC_CAP_INTERRUPT)
551 iap->pm_iap_config |= IAP_INT;
556 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
558 const struct pmu_event *pe;
559 struct pmu_event_desc ped;
563 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
566 bzero(&pm->pm_md, sizeof(pm->pm_md));
567 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
568 event_name = pmu_alias_get(event_name);
569 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
574 if (pe->event == NULL)
576 if (pmu_parse_event(&ped, pe->event))
579 if (mfr == PMU_INTEL)
580 return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped));
582 return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped));
585 #elif defined(__aarch64__)
588 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
590 const struct pmu_event *pe;
591 struct pmu_event_desc ped;
594 event_name = pmu_alias_get(event_name);
595 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
597 if (pe->event == NULL)
599 if (pmu_parse_event(&ped, pe->event))
604 pm->pm_md.pm_md_config = ped.ped_event;
605 pm->pm_md.pm_md_flags |= PM_MD_RAW_EVENT;
606 pm->pm_class = PMC_CLASS_ARMV8;
607 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
615 pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)