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 {"cycles", "tsc-tsc"},
78 {"unhalted-cycles", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
79 {"instructions", "inst_retired.any_p"},
80 {"branch-mispredicts", "br_misp_retired.all_branches"},
81 {"branches", "br_inst_retired.all_branches"},
82 {"interrupts", "hw_interrupts.received"},
83 {"ic-misses", "frontend_retired.l1i_miss"},
87 static struct pmu_alias pmu_amd_alias_table[] = {
88 {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"},
89 {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"},
97 char buf[PMC_CPUID_LEN];
98 size_t s = sizeof(buf);
101 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
102 (void *)NULL, 0) == -1)
103 return (PMU_INVALID);
104 if (strcasestr(buf, "AuthenticAMD") != NULL ||
105 strcasestr(buf, "HygonGenuine") != NULL)
107 else if (strcasestr(buf, "GenuineIntel") != NULL)
115 * The Intel fixed mode counters are:
116 * "inst_retired.any",
117 * "cpu_clk_unhalted.thread",
118 * "cpu_clk_unhalted.thread_any",
119 * "cpu_clk_unhalted.ref_tsc",
124 pmu_alias_get(const char *name)
127 struct pmu_alias *pa;
128 struct pmu_alias *pmu_alias_table;
130 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
133 pmu_alias_table = pmu_amd_alias_table;
134 else if (mfr == PMU_INTEL)
135 pmu_alias_table = pmu_intel_alias_table;
139 for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
140 if (strcasecmp(name, pa->pa_alias) == 0)
141 return (pa->pa_name);
146 #elif defined(__aarch64__)
148 static struct pmu_alias pmu_armv8_alias_table[] = {
153 pmu_alias_get(const char *name)
155 struct pmu_alias *pa;
157 for (pa = pmu_armv8_alias_table; pa->pa_alias != NULL; pa++)
158 if (strcasecmp(name, pa->pa_alias) == 0)
159 return (pa->pa_name);
167 pmu_alias_get(const char *name)
174 struct pmu_event_desc {
176 uint64_t ped_offcore_rsp;
177 uint64_t ped_l3_thread;
178 uint64_t ped_l3_slice;
180 uint32_t ped_frontend;
182 uint32_t ped_config1;
192 static const struct pmu_events_map *
193 pmu_events_map_get(const char *cpuid)
196 regmatch_t pmatch[1];
197 char buf[PMC_CPUID_LEN];
198 size_t s = sizeof(buf);
200 const struct pmu_events_map *pme;
203 strlcpy(buf, cpuid, s);
205 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
206 (void *)NULL, 0) == -1)
209 for (pme = pmu_events_map; pme->cpuid != NULL; pme++) {
210 if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) {
211 printf("regex '%s' failed to compile, ignoring\n",
215 match = regexec(&re, buf, 1, pmatch, 0);
218 if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0
219 || buf[pmatch[0].rm_eo] == '-'))
226 static const struct pmu_event *
227 pmu_event_get(const char *cpuid, const char *event_name, int *idx)
229 const struct pmu_events_map *pme;
230 const struct pmu_event *pe;
233 if ((pme = pmu_events_map_get(cpuid)) == NULL)
235 for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
236 if (pe->name == NULL)
238 if (strcasecmp(pe->name, event_name) == 0) {
248 pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
251 const char *realname;
253 realname = pmu_alias_get(event);
254 if (pmu_event_get(cpuid, realname, &idx) == NULL)
260 pmc_pmu_event_get_by_idx(const char *cpuid, int idx)
262 const struct pmu_events_map *pme;
264 if ((pme = pmu_events_map_get(cpuid)) == NULL)
266 assert(pme->table[idx].name);
267 return (pme->table[idx].name);
271 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
274 char *kvp, *key, *value, *r;
277 if ((event = strdup(eventin)) == NULL)
280 bzero(ped, sizeof(*ped));
281 ped->ped_period = DEFAULT_SAMPLE_COUNT;
283 while ((kvp = strsep(&event, ",")) != NULL) {
284 key = strsep(&kvp, "=");
288 if (strcmp(key, "umask") == 0)
289 ped->ped_umask = strtol(value, NULL, 16);
290 else if (strcmp(key, "event") == 0)
291 ped->ped_event = strtol(value, NULL, 16);
292 else if (strcmp(key, "period") == 0)
293 ped->ped_period = strtol(value, NULL, 10);
294 else if (strcmp(key, "offcore_rsp") == 0)
295 ped->ped_offcore_rsp = strtol(value, NULL, 16);
296 else if (strcmp(key, "any") == 0)
297 ped->ped_any = strtol(value, NULL, 10);
298 else if (strcmp(key, "cmask") == 0)
299 ped->ped_cmask = strtol(value, NULL, 10);
300 else if (strcmp(key, "inv") == 0)
301 ped->ped_inv = strtol(value, NULL, 10);
302 else if (strcmp(key, "edge") == 0)
303 ped->ped_edge = strtol(value, NULL, 10);
304 else if (strcmp(key, "frontend") == 0)
305 ped->ped_frontend = strtol(value, NULL, 16);
306 else if (strcmp(key, "ldlat") == 0)
307 ped->ped_ldlat = strtol(value, NULL, 16);
308 else if (strcmp(key, "fc_mask") == 0)
309 ped->ped_fc_mask = strtol(value, NULL, 16);
310 else if (strcmp(key, "ch_mask") == 0)
311 ped->ped_ch_mask = strtol(value, NULL, 16);
312 else if (strcmp(key, "config1") == 0)
313 ped->ped_config1 = strtol(value, NULL, 16);
314 else if (strcmp(key, "l3_thread_mask") == 0)
315 ped->ped_l3_thread = strtol(value, NULL, 16);
316 else if (strcmp(key, "l3_slice_mask") == 0)
317 ped->ped_l3_slice = strtol(value, NULL, 16);
319 debug = getenv("PMUDEBUG");
320 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
321 printf("unrecognized kvpair: %s:%s\n", key, value);
329 pmc_pmu_sample_rate_get(const char *event_name)
331 const struct pmu_event *pe;
332 struct pmu_event_desc ped;
334 event_name = pmu_alias_get(event_name);
335 if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
336 return (DEFAULT_SAMPLE_COUNT);
337 if (pe->event == NULL)
338 return (DEFAULT_SAMPLE_COUNT);
339 if (pmu_parse_event(&ped, pe->event))
340 return (DEFAULT_SAMPLE_COUNT);
341 return (ped.ped_period);
345 pmc_pmu_enabled(void)
348 return (pmu_events_map_get(NULL) != NULL);
352 pmc_pmu_print_counters(const char *event_name)
354 const struct pmu_events_map *pme;
355 const struct pmu_event *pe;
356 struct pmu_event_desc ped;
360 debug = getenv("PMUDEBUG");
363 if (debug != NULL && strcmp(debug, "true") == 0)
365 if ((pme = pmu_events_map_get(NULL)) == NULL)
367 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
368 if (pe->name == NULL)
370 if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
372 printf("\t%s\n", pe->name);
374 pmu_parse_event(&ped, pe->event);
379 pmc_pmu_print_counter_desc(const char *ev)
381 const struct pmu_events_map *pme;
382 const struct pmu_event *pe;
384 if ((pme = pmu_events_map_get(NULL)) == NULL)
386 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
387 if (pe->name == NULL)
389 if (strcasestr(pe->name, ev) != NULL &&
391 printf("%s:\t%s\n", pe->name, pe->desc);
396 pmc_pmu_print_counter_desc_long(const char *ev)
398 const struct pmu_events_map *pme;
399 const struct pmu_event *pe;
401 if ((pme = pmu_events_map_get(NULL)) == NULL)
403 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
404 if (pe->name == NULL)
406 if (strcasestr(pe->name, ev) != NULL) {
407 if (pe->long_desc != NULL)
408 printf("%s:\n%s\n", pe->name, pe->long_desc);
409 else if (pe->desc != NULL)
410 printf("%s:\t%s\n", pe->name, pe->desc);
416 pmc_pmu_print_counter_full(const char *ev)
418 const struct pmu_events_map *pme;
419 const struct pmu_event *pe;
421 if ((pme = pmu_events_map_get(NULL)) == NULL)
423 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
424 if (pe->name == NULL)
426 if (strcasestr(pe->name, ev) == NULL)
428 printf("name: %s\n", pe->name);
429 if (pe->long_desc != NULL)
430 printf("desc: %s\n", pe->long_desc);
431 else if (pe->desc != NULL)
432 printf("desc: %s\n", pe->desc);
433 if (pe->event != NULL)
434 printf("event: %s\n", pe->event);
435 if (pe->topic != NULL)
436 printf("topic: %s\n", pe->topic);
438 printf("pmu: %s\n", pe->pmu);
439 if (pe->unit != NULL)
440 printf("unit: %s\n", pe->unit);
441 if (pe->perpkg != NULL)
442 printf("perpkg: %s\n", pe->perpkg);
443 if (pe->metric_expr != NULL)
444 printf("metric_expr: %s\n", pe->metric_expr);
445 if (pe->metric_name != NULL)
446 printf("metric_name: %s\n", pe->metric_name);
447 if (pe->metric_group != NULL)
448 printf("metric_group: %s\n", pe->metric_group);
452 #if defined(__amd64__) || defined(__i386__)
454 pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
455 struct pmu_event_desc *ped)
457 struct pmc_md_amd_op_pmcallocate *amd;
458 const struct pmu_event *pe;
461 amd = &pm->pm_md.pm_amd;
462 if (ped->ped_umask > 0) {
463 pm->pm_caps |= PMC_CAP_QUALIFIER;
464 amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask);
466 pm->pm_class = PMC_CLASS_K8;
467 pe = pmu_event_get(NULL, event_name, &idx);
469 if (strcmp("l3cache", pe->topic) == 0){
470 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
471 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE;
472 amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice);
473 amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread);
475 else if (strcmp("data fabric", pe->topic) == 0){
477 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event);
478 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC;
481 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
482 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE;
483 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
484 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
485 (PMC_CAP_USER|PMC_CAP_SYSTEM))
486 amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS);
487 else if (pm->pm_caps & PMC_CAP_USER)
488 amd->pm_amd_config |= AMD_PMC_USR;
489 else if (pm->pm_caps & PMC_CAP_SYSTEM)
490 amd->pm_amd_config |= AMD_PMC_OS;
492 amd->pm_amd_config |= AMD_PMC_EDGE;
494 amd->pm_amd_config |= AMD_PMC_EDGE;
495 if (pm->pm_caps & PMC_CAP_INTERRUPT)
496 amd->pm_amd_config |= AMD_PMC_INT;
502 pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
503 struct pmu_event_desc *ped)
505 struct pmc_md_iap_op_pmcallocate *iap;
507 iap = &pm->pm_md.pm_iap;
508 if (strcasestr(event_name, "UNC_") == event_name ||
509 strcasestr(event_name, "uncore") != NULL) {
510 pm->pm_class = PMC_CLASS_UCP;
511 pm->pm_caps |= PMC_CAP_QUALIFIER;
512 } else if ((ped->ped_umask == -1) ||
513 (ped->ped_event == 0x0 && ped->ped_umask == 0x3)) {
514 pm->pm_class = PMC_CLASS_IAF;
516 pm->pm_class = PMC_CLASS_IAP;
517 pm->pm_caps |= PMC_CAP_QUALIFIER;
519 iap->pm_iap_config |= IAP_EVSEL(ped->ped_event);
520 if (ped->ped_umask > 0)
521 iap->pm_iap_config |= IAP_UMASK(ped->ped_umask);
522 iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask);
523 iap->pm_iap_rsp = ped->ped_offcore_rsp;
525 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
526 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
527 (PMC_CAP_USER|PMC_CAP_SYSTEM))
528 iap->pm_iap_config |= (IAP_USR | IAP_OS);
529 else if (pm->pm_caps & PMC_CAP_USER)
530 iap->pm_iap_config |= IAP_USR;
531 else if (pm->pm_caps & PMC_CAP_SYSTEM)
532 iap->pm_iap_config |= IAP_OS;
534 iap->pm_iap_config |= IAP_EDGE;
536 iap->pm_iap_config |= IAP_ANY;
538 iap->pm_iap_config |= IAP_EDGE;
539 if (pm->pm_caps & PMC_CAP_INTERRUPT)
540 iap->pm_iap_config |= IAP_INT;
545 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
547 const struct pmu_event *pe;
548 struct pmu_event_desc ped;
552 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
555 bzero(&pm->pm_md, sizeof(pm->pm_md));
556 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
557 event_name = pmu_alias_get(event_name);
558 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
563 if (pe->event == NULL)
565 if (pmu_parse_event(&ped, pe->event))
568 if (mfr == PMU_INTEL)
569 return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped));
571 return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped));
574 #elif defined(__aarch64__)
577 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
579 const struct pmu_event *pe;
582 event_name = pmu_alias_get(event_name);
583 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
585 if (pe->event == NULL)
589 pm->pm_md.pm_md_flags |= PM_MD_RAW_EVENT;
590 pm->pm_class = PMC_CLASS_ARMV8;
591 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
599 pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)