2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2018, Matthew Macy
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
32 #include <sys/errno.h>
33 #include <sys/sysctl.h>
41 #include <libpmcstat.h>
42 #include "pmu-events/pmu-events.h"
44 #if defined(__amd64__) || defined(__i386__)
49 static struct pmu_alias pmu_alias_table[] = {
50 {"UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
51 {"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
52 {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
53 {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
54 {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
55 {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
56 {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
57 {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
58 {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
59 {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
60 {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
61 {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
62 {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
63 {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
64 {"cycles", "tsc-tsc"},
65 {"instructions", "inst-retired.any_p"},
66 {"branch-mispredicts", "br_misp_retired.all_branches"},
67 {"branches", "br_inst_retired.all_branches"},
68 {"interrupts", "hw_interrupts.received"},
69 {"ic-misses", "frontend_retired.l1i_miss"},
74 * The Intel fixed mode counters are:
76 * "cpu_clk_unhalted.thread",
77 * "cpu_clk_unhalted.thread_any",
78 * "cpu_clk_unhalted.ref_tsc",
83 pmu_alias_get(const char *name)
87 for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
88 if (strcasecmp(name, pa->pa_alias) == 0)
93 struct pmu_event_desc {
95 uint64_t ped_offcore_rsp;
97 uint32_t ped_frontend;
109 static const struct pmu_events_map *
110 pmu_events_map_get(const char *cpuid)
114 const struct pmu_events_map *pme;
117 memcpy(buf, cpuid, 64);
119 if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
120 (void *)NULL, 0) == -1)
122 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
123 (void *)NULL, 0) == -1)
126 for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
127 if (strcmp(buf, pme->cpuid) == 0)
132 static const struct pmu_event *
133 pmu_event_get(const char *cpuid, const char *event_name, int *idx)
135 const struct pmu_events_map *pme;
136 const struct pmu_event *pe;
139 if ((pme = pmu_events_map_get(cpuid)) == NULL)
141 for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
142 if (pe->name == NULL)
144 if (strcasecmp(pe->name, event_name) == 0) {
154 pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
157 const char *realname;
159 realname = pmu_alias_get(event);
160 if (pmu_event_get(cpuid, realname, &idx) == NULL)
166 pmc_pmu_event_get_by_idx(const char *cpuid, int idx)
168 const struct pmu_events_map *pme;
170 if ((pme = pmu_events_map_get(cpuid)) == NULL)
172 assert(pme->table[idx].name);
173 return (pme->table[idx].name);
177 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
180 char *kvp, *key, *value, *r;
183 if ((event = strdup(eventin)) == NULL)
186 bzero(ped, sizeof(*ped));
188 while ((kvp = strsep(&event, ",")) != NULL) {
189 key = strsep(&kvp, "=");
193 if (strcmp(key, "umask") == 0)
194 ped->ped_umask = strtol(value, NULL, 16);
195 else if (strcmp(key, "event") == 0)
196 ped->ped_event = strtol(value, NULL, 16);
197 else if (strcmp(key, "period") == 0)
198 ped->ped_period = strtol(value, NULL, 10);
199 else if (strcmp(key, "offcore_rsp") == 0)
200 ped->ped_offcore_rsp = strtol(value, NULL, 16);
201 else if (strcmp(key, "any") == 0)
202 ped->ped_any = strtol(value, NULL, 10);
203 else if (strcmp(key, "cmask") == 0)
204 ped->ped_cmask = strtol(value, NULL, 10);
205 else if (strcmp(key, "inv") == 0)
206 ped->ped_inv = strtol(value, NULL, 10);
207 else if (strcmp(key, "edge") == 0)
208 ped->ped_edge = strtol(value, NULL, 10);
209 else if (strcmp(key, "frontend") == 0)
210 ped->ped_frontend = strtol(value, NULL, 16);
211 else if (strcmp(key, "ldlat") == 0)
212 ped->ped_ldlat = strtol(value, NULL, 16);
213 else if (strcmp(key, "fc_mask") == 0)
214 ped->ped_fc_mask = strtol(value, NULL, 16);
215 else if (strcmp(key, "ch_mask") == 0)
216 ped->ped_ch_mask = strtol(value, NULL, 16);
217 else if (strcmp(key, "config1") == 0)
218 ped->ped_config1 = strtol(value, NULL, 16);
220 debug = getenv("PMUDEBUG");
221 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
222 printf("unrecognized kvpair: %s:%s\n", key, value);
230 pmc_pmu_sample_rate_get(const char *event_name)
232 const struct pmu_event *pe;
233 struct pmu_event_desc ped;
235 event_name = pmu_alias_get(event_name);
236 if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
237 return (DEFAULT_SAMPLE_COUNT);
238 if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, NULL)) == NULL)
239 return (DEFAULT_SAMPLE_COUNT);
240 if (pe->event == NULL)
241 return (DEFAULT_SAMPLE_COUNT);
242 if (pmu_parse_event(&ped, pe->event))
243 return (DEFAULT_SAMPLE_COUNT);
244 return (ped.ped_period);
248 pmc_pmu_enabled(void)
251 return (pmu_events_map_get(NULL) != NULL);
255 pmc_pmu_print_counters(const char *event_name)
257 const struct pmu_events_map *pme;
258 const struct pmu_event *pe;
259 struct pmu_event_desc ped;
263 debug = getenv("PMUDEBUG");
266 if (debug != NULL && strcmp(debug, "true") == 0)
268 if ((pme = pmu_events_map_get(NULL)) == NULL)
270 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
271 if (pe->name == NULL)
273 if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
275 printf("\t%s\n", pe->name);
277 pmu_parse_event(&ped, pe->event);
282 pmc_pmu_print_counter_desc(const char *ev)
284 const struct pmu_events_map *pme;
285 const struct pmu_event *pe;
287 if ((pme = pmu_events_map_get(NULL)) == NULL)
289 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
290 if (pe->name == NULL)
292 if (strcasestr(pe->name, ev) != NULL &&
294 printf("%s:\t%s\n", pe->name, pe->desc);
299 pmc_pmu_print_counter_desc_long(const char *ev)
301 const struct pmu_events_map *pme;
302 const struct pmu_event *pe;
304 if ((pme = pmu_events_map_get(NULL)) == NULL)
306 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
307 if (pe->name == NULL)
309 if (strcasestr(pe->name, ev) != NULL) {
310 if (pe->long_desc != NULL)
311 printf("%s:\n%s\n", pe->name, pe->long_desc);
312 else if (pe->desc != NULL)
313 printf("%s:\t%s\n", pe->name, pe->desc);
319 pmc_pmu_print_counter_full(const char *ev)
321 const struct pmu_events_map *pme;
322 const struct pmu_event *pe;
324 if ((pme = pmu_events_map_get(NULL)) == NULL)
326 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
327 if (pe->name == NULL)
329 if (strcasestr(pe->name, ev) == NULL)
331 printf("name: %s\n", pe->name);
332 if (pe->long_desc != NULL)
333 printf("desc: %s\n", pe->long_desc);
334 else if (pe->desc != NULL)
335 printf("desc: %s\n", pe->desc);
336 if (pe->event != NULL)
337 printf("event: %s\n", pe->event);
338 if (pe->topic != NULL)
339 printf("topic: %s\n", pe->topic);
341 printf("pmu: %s\n", pe->pmu);
342 if (pe->unit != NULL)
343 printf("unit: %s\n", pe->unit);
344 if (pe->perpkg != NULL)
345 printf("perpkg: %s\n", pe->perpkg);
346 if (pe->metric_expr != NULL)
347 printf("metric_expr: %s\n", pe->metric_expr);
348 if (pe->metric_name != NULL)
349 printf("metric_name: %s\n", pe->metric_name);
350 if (pe->metric_group != NULL)
351 printf("metric_group: %s\n", pe->metric_group);
356 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
358 const struct pmu_event *pe;
359 struct pmu_event_desc ped;
360 struct pmc_md_iap_op_pmcallocate *iap;
363 iap = &pm->pm_md.pm_iap;
365 bzero(iap, sizeof(*iap));
366 event_name = pmu_alias_get(event_name);
367 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
368 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
370 if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, &idx)) == NULL)
372 if (pe->event == NULL)
374 if (pmu_parse_event(&ped, pe->event))
378 if (strcasestr(event_name, "UNC_") == event_name ||
379 strcasestr(event_name, "uncore") != NULL) {
380 pm->pm_class = PMC_CLASS_UCP;
381 pm->pm_caps |= PMC_CAP_QUALIFIER;
382 } else if ((ped.ped_umask == -1) ||
383 (ped.ped_event == 0x0 && ped.ped_umask == 0x3)) {
384 pm->pm_class = PMC_CLASS_IAF;
386 pm->pm_class = PMC_CLASS_IAP;
387 pm->pm_caps |= PMC_CAP_QUALIFIER;
390 iap->pm_iap_config |= IAP_EVSEL(ped.ped_event);
391 if (ped.ped_umask > 0)
392 iap->pm_iap_config |= IAP_UMASK(ped.ped_umask);
393 iap->pm_iap_config |= IAP_CMASK(ped.ped_cmask);
394 iap->pm_iap_rsp = ped.ped_offcore_rsp;
396 iap->pm_iap_config |= (IAP_USR | IAP_OS);
398 iap->pm_iap_config |= IAP_EDGE;
400 iap->pm_iap_config |= IAP_ANY;
402 iap->pm_iap_config |= IAP_EDGE;
403 if (pm->pm_caps & PMC_CAP_INTERRUPT)
404 iap->pm_iap_config |= IAP_INT;
409 * Ultimately rely on AMD calling theirs the same
411 static const char *stat_mode_cntrs[] = {
412 "cpu_clk_unhalted.thread",
414 "br_inst_retired.all_branches",
415 "br_misp_retired.all_branches",
416 "longest_lat_cache.reference",
417 "longest_lat_cache.miss",
421 pmc_pmu_stat_mode(const char ***cntrs)
423 if (pmc_pmu_enabled()) {
424 *cntrs = stat_mode_cntrs;
433 pmc_pmu_sample_rate_get(const char *event_name __unused)
435 return (DEFAULT_SAMPLE_COUNT);
439 pmc_pmu_print_counters(const char *event_name __unused)
444 pmc_pmu_print_counter_desc(const char *e __unused)
449 pmc_pmu_print_counter_desc_long(const char *e __unused)
454 pmc_pmu_print_counter_full(const char *e __unused)
460 pmc_pmu_enabled(void)
466 pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
472 pmc_pmu_event_get_by_idx(const char *c __unused, int idx __unused)
478 pmc_pmu_stat_mode(const char ***a __unused)
484 pmc_pmu_idx_get_by_event(const char *c __unused, const char *e __unused)