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>
40 #include <libpmcstat.h>
41 #include "pmu-events/pmu-events.h"
43 #if defined(__amd64__) || defined(__i386__)
48 static struct pmu_alias pmu_alias_table[] = {
49 {"UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
50 {"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
51 {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
52 {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
53 {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
54 {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
55 {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
56 {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
57 {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
58 {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
59 {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
60 {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
61 {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
62 {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
67 static struct pmc_event_alias core2_aliases_without_iaf[] = {
68 EV_ALIAS("branches", "iap-br-inst-retired.any"),
69 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
70 EV_ALIAS("cycles", "tsc-tsc"),
71 EV_ALIAS("ic-misses", "iap-l1i-misses"),
72 EV_ALIAS("instructions", "iap-inst-retired.any_p"),
73 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
74 EV_ALIAS("unhalted-cycles", "iap-cpu-clk-unhalted.core_p"),
78 static const char *fixed_mode_cntrs[] = {
80 "cpu_clk_unhalted.thread",
81 "cpu_clk_unhalted.thread_any",
82 "cpu_clk_unhalted.ref_tsc",
87 pmu_alias_get(const char *name)
91 for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
92 if (strcasecmp(name, pa->pa_alias) == 0)
97 struct pmu_event_desc {
99 uint64_t ped_offcore_rsp;
101 uint32_t ped_frontend;
103 uint32_t ped_config1;
113 static const struct pmu_events_map *
114 pmu_events_map_get(void)
118 const struct pmu_events_map *pme;
120 if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
121 (void *)NULL, 0) == -1)
123 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
124 (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 *event_name, int *idx)
135 const struct pmu_events_map *pme;
136 const struct pmu_event *pe;
139 if ((pme = pmu_events_map_get()) == 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_event_get_by_idx(int idx)
156 const struct pmu_events_map *pme;
157 const struct pmu_event *pe;
160 if ((pme = pmu_events_map_get()) == NULL)
162 for (i = 0, pe = pme->table; (pe->name || pe->desc || pe->event) && i < idx; pe++, i++);
167 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
170 char *kvp, *key, *value, *r;
173 if ((event = strdup(eventin)) == NULL)
176 bzero(ped, sizeof(*ped));
177 while ((kvp = strsep(&event, ",")) != NULL) {
178 key = strsep(&kvp, "=");
182 if (strcmp(key, "umask") == 0)
183 ped->ped_umask = strtol(value, NULL, 16);
184 else if (strcmp(key, "event") == 0)
185 ped->ped_event = strtol(value, NULL, 16);
186 else if (strcmp(key, "period") == 0)
187 ped->ped_period = strtol(value, NULL, 10);
188 else if (strcmp(key, "offcore_rsp") == 0)
189 ped->ped_offcore_rsp = strtol(value, NULL, 16);
190 else if (strcmp(key, "any") == 0)
191 ped->ped_any = strtol(value, NULL, 10);
192 else if (strcmp(key, "cmask") == 0)
193 ped->ped_cmask = strtol(value, NULL, 10);
194 else if (strcmp(key, "inv") == 0)
195 ped->ped_inv = strtol(value, NULL, 10);
196 else if (strcmp(key, "edge") == 0)
197 ped->ped_edge = strtol(value, NULL, 10);
198 else if (strcmp(key, "frontend") == 0)
199 ped->ped_frontend = strtol(value, NULL, 16);
200 else if (strcmp(key, "ldlat") == 0)
201 ped->ped_ldlat = strtol(value, NULL, 16);
202 else if (strcmp(key, "fc_mask") == 0)
203 ped->ped_fc_mask = strtol(value, NULL, 16);
204 else if (strcmp(key, "ch_mask") == 0)
205 ped->ped_ch_mask = strtol(value, NULL, 16);
206 else if (strcmp(key, "config1") == 0)
207 ped->ped_config1 = strtol(value, NULL, 16);
209 debug = getenv("PMUDEBUG");
210 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
211 printf("unrecognized kvpair: %s:%s\n", key, value);
219 pmc_pmu_sample_rate_get(const char *event_name)
221 const struct pmu_event *pe;
222 struct pmu_event_desc ped;
224 event_name = pmu_alias_get(event_name);
225 if ((pe = pmu_event_get(event_name, NULL)) == NULL)
226 return (DEFAULT_SAMPLE_COUNT);
227 if (pe->alias && (pe = pmu_event_get(pe->alias, NULL)) == NULL)
228 return (DEFAULT_SAMPLE_COUNT);
229 if (pe->event == NULL)
230 return (DEFAULT_SAMPLE_COUNT);
231 if (pmu_parse_event(&ped, pe->event))
232 return (DEFAULT_SAMPLE_COUNT);
233 return (ped.ped_period);
237 pmc_pmu_enabled(void)
240 return (pmu_events_map_get() != NULL);
244 pmc_pmu_print_counters(void)
246 const struct pmu_events_map *pme;
247 const struct pmu_event *pe;
248 struct pmu_event_desc ped;
252 debug = getenv("PMUDEBUG");
255 if (debug != NULL && strcmp(debug, "true") == 0)
257 if ((pme = pmu_events_map_get()) == NULL)
259 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
260 if (pe->name == NULL)
262 printf("\t%s\n", pe->name);
264 pmu_parse_event(&ped, pe->event);
269 pmc_pmu_print_counter_desc(const char *ev)
271 const struct pmu_events_map *pme;
272 const struct pmu_event *pe;
274 if ((pme = pmu_events_map_get()) == NULL)
276 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
277 if (pe->name == NULL)
279 if (strcasestr(pe->name, ev) != NULL &&
281 printf("%s:\t%s\n", pe->name, pe->desc);
286 pmc_pmu_print_counter_desc_long(const char *ev)
288 const struct pmu_events_map *pme;
289 const struct pmu_event *pe;
291 if ((pme = pmu_events_map_get()) == NULL)
293 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
294 if (pe->name == NULL)
296 if (strcasestr(pe->name, ev) != NULL) {
297 if (pe->long_desc != NULL)
298 printf("%s:\n%s\n", pe->name, pe->long_desc);
299 else if (pe->desc != NULL)
300 printf("%s:\t%s\n", pe->name, pe->desc);
306 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
308 const struct pmu_event *pe;
309 struct pmu_event_desc ped;
310 struct pmc_md_iap_op_pmcallocate *iap;
311 struct pmc_md_iaf_op_pmcallocate *iaf;
314 iap = &pm->pm_md.pm_iap;
316 bzero(iap, sizeof(*iap));
317 event_name = pmu_alias_get(event_name);
318 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
319 if ((pe = pmu_event_get(event_name, &idx)) == NULL)
321 if (pe->alias && (pe = pmu_event_get(pe->alias, &idx)) == NULL)
323 if (pe->event == NULL)
325 if (pmu_parse_event(&ped, pe->event))
328 for (idx = 0; fixed_mode_cntrs[idx] != NULL; idx++)
329 if (strcmp(fixed_mode_cntrs[idx], event_name) == 0)
332 iaf = &pm->pm_md.pm_iaf;
333 pm->pm_class = PMC_CLASS_IAF;
334 if (strcasestr(pe->desc, "retired") != NULL)
335 pm->pm_ev = PMC_EV_IAF_INSTR_RETIRED_ANY;
336 else if (strcasestr(pe->desc, "core") != NULL ||
337 strcasestr(pe->desc, "unhalted"))
338 pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_CORE;
339 else if (strcasestr(pe->desc, "ref") != NULL)
340 pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_REF;
341 iaf->pm_iaf_flags |= (IAF_USR | IAF_OS);
343 iaf->pm_iaf_flags |= IAF_ANY;
344 if (pm->pm_caps & PMC_CAP_INTERRUPT)
345 iaf->pm_iaf_flags |= IAF_PMI;
347 } else if (strcasestr(event_name, "UNC_") == event_name ||
348 strcasestr(event_name, "uncore") != NULL) {
349 pm->pm_class = PMC_CLASS_UCP;
351 pm->pm_caps |= PMC_CAP_QUALIFIER;
352 pm->pm_class = PMC_CLASS_IAP;
355 iap->pm_iap_config |= IAP_EVSEL(ped.ped_event);
356 iap->pm_iap_config |= IAP_UMASK(ped.ped_umask);
357 iap->pm_iap_config |= IAP_CMASK(ped.ped_cmask);
358 iap->pm_iap_rsp = ped.ped_offcore_rsp;
360 iap->pm_iap_config |= (IAP_USR | IAP_OS);
362 iap->pm_iap_config |= IAP_EDGE;
364 iap->pm_iap_config |= IAP_ANY;
366 iap->pm_iap_config |= IAP_EDGE;
367 if (pm->pm_caps & PMC_CAP_INTERRUPT)
368 iap->pm_iap_config |= IAP_INT;
373 * Ultimately rely on AMD calling theirs the same
375 static const char *stat_mode_cntrs[] = {
376 "cpu_clk_unhalted.thread_any",
378 "br_inst_retired.all_branches",
379 "br_misp_retired.all_branches",
380 "longest_lat_cache.reference",
381 "longest_lat_cache.miss",
385 pmc_pmu_stat_mode(const char ***cntrs)
387 if (pmc_pmu_enabled()) {
388 *cntrs = stat_mode_cntrs;
397 pmc_pmu_sample_rate_get(const char *event_name __unused)
399 return (DEFAULT_SAMPLE_COUNT);
403 pmc_pmu_print_counters(void)
408 pmc_pmu_print_counter_desc(const char *e __unused)
413 pmc_pmu_print_counter_desc_long(const char *e __unused)
418 pmc_pmu_enabled(void)
424 pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
430 pmc_pmu_event_get_by_idx(int idx __unused)
435 pmc_pmu_stat_mode(const char ***a __unused)