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__)
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"},
66 static const char *fixed_mode_cntrs[] = {
68 "cpu_clk_unhalted.thread",
69 "cpu_clk_unhalted.thread_any",
70 "cpu_clk_unhalted.ref_tsc",
75 pmu_alias_get(const char *name)
79 for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
80 if (strcasecmp(name, pa->pa_alias) == 0)
85 struct pmu_event_desc {
87 uint64_t ped_offcore_rsp;
89 uint32_t ped_frontend;
101 static const struct pmu_events_map *
102 pmu_events_map_get(void)
106 const struct pmu_events_map *pme;
108 if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
109 (void *)NULL, 0) == -1)
111 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
112 (void *)NULL, 0) == -1)
114 for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
115 if (strcmp(buf, pme->cpuid) == 0)
120 static const struct pmu_event *
121 pmu_event_get(const char *event_name, int *idx)
123 const struct pmu_events_map *pme;
124 const struct pmu_event *pe;
127 if ((pme = pmu_events_map_get()) == NULL)
129 for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
130 if (pe->name == NULL)
132 if (strcasecmp(pe->name, event_name) == 0) {
142 pmc_pmu_event_get_by_idx(int idx)
144 const struct pmu_events_map *pme;
145 const struct pmu_event *pe;
148 if ((pme = pmu_events_map_get()) == NULL)
150 for (i = 0, pe = pme->table; (pe->name || pe->desc || pe->event) && i < idx; pe++, i++)
156 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
159 char *kvp, *key, *value;
162 if ((event = strdup(eventin)) == NULL)
164 bzero(ped, sizeof(*ped));
165 while ((kvp = strsep(&event, ",")) != NULL) {
166 key = strsep(&kvp, "=");
170 if (strcmp(key, "umask") == 0)
171 ped->ped_umask = strtol(value, NULL, 16);
172 else if (strcmp(key, "event") == 0)
173 ped->ped_event = strtol(value, NULL, 16);
174 else if (strcmp(key, "period") == 0)
175 ped->ped_period = strtol(value, NULL, 10);
176 else if (strcmp(key, "offcore_rsp") == 0)
177 ped->ped_offcore_rsp = strtol(value, NULL, 16);
178 else if (strcmp(key, "any") == 0)
179 ped->ped_any = strtol(value, NULL, 10);
180 else if (strcmp(key, "cmask") == 0)
181 ped->ped_cmask = strtol(value, NULL, 10);
182 else if (strcmp(key, "inv") == 0)
183 ped->ped_inv = strtol(value, NULL, 10);
184 else if (strcmp(key, "edge") == 0)
185 ped->ped_edge = strtol(value, NULL, 10);
186 else if (strcmp(key, "frontend") == 0)
187 ped->ped_frontend = strtol(value, NULL, 16);
188 else if (strcmp(key, "ldlat") == 0)
189 ped->ped_ldlat = strtol(value, NULL, 16);
190 else if (strcmp(key, "fc_mask") == 0)
191 ped->ped_fc_mask = strtol(value, NULL, 16);
192 else if (strcmp(key, "ch_mask") == 0)
193 ped->ped_ch_mask = strtol(value, NULL, 16);
194 else if (strcmp(key, "config1") == 0)
195 ped->ped_config1 = strtol(value, NULL, 16);
197 debug = getenv("PMUDEBUG");
198 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
199 printf("unrecognized kvpair: %s:%s\n", key, value);
207 pmc_pmu_sample_rate_get(const char *event_name)
209 const struct pmu_event *pe;
210 struct pmu_event_desc ped;
212 event_name = pmu_alias_get(event_name);
213 if ((pe = pmu_event_get(event_name, NULL)) == NULL)
214 return (DEFAULT_SAMPLE_COUNT);
215 if (pe->alias && (pe = pmu_event_get(pe->alias, NULL)) == NULL)
216 return (DEFAULT_SAMPLE_COUNT);
217 if (pe->event == NULL)
218 return (DEFAULT_SAMPLE_COUNT);
219 if (pmu_parse_event(&ped, pe->event))
220 return (DEFAULT_SAMPLE_COUNT);
221 return (ped.ped_period);
225 pmc_pmu_enabled(void)
228 return (pmu_events_map_get() != NULL);
232 pmc_pmu_print_counters(void)
234 const struct pmu_events_map *pme;
235 const struct pmu_event *pe;
236 struct pmu_event_desc ped;
240 debug = getenv("PMUDEBUG");
243 if (debug != NULL && strcmp(debug, "true") == 0)
245 if ((pme = pmu_events_map_get()) == NULL)
247 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
248 if (pe->name == NULL)
250 printf("\t%s\n", pe->name);
252 pmu_parse_event(&ped, pe->event);
257 pmc_pmu_print_counter_desc(const char *ev)
259 const struct pmu_events_map *pme;
260 const struct pmu_event *pe;
262 if ((pme = pmu_events_map_get()) == NULL)
264 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
265 if (pe->name == NULL)
267 if (strcasestr(pe->name, ev) != NULL &&
269 printf("%s:\t%s\n", pe->name, pe->desc);
274 pmc_pmu_print_counter_desc_long(const char *ev)
276 const struct pmu_events_map *pme;
277 const struct pmu_event *pe;
279 if ((pme = pmu_events_map_get()) == NULL)
281 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
282 if (pe->name == NULL)
284 if (strcasestr(pe->name, ev) != NULL) {
285 if (pe->long_desc != NULL)
286 printf("%s:\n%s\n", pe->name, pe->long_desc);
287 else if (pe->desc != NULL)
288 printf("%s:\t%s\n", pe->name, pe->desc);
294 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
296 const struct pmu_event *pe;
297 struct pmu_event_desc ped;
298 struct pmc_md_iap_op_pmcallocate *iap;
299 struct pmc_md_iaf_op_pmcallocate *iaf;
302 iap = &pm->pm_md.pm_iap;
303 iaf = &pm->pm_md.pm_iaf;
305 bzero(iap, sizeof(*iap));
306 event_name = pmu_alias_get(event_name);
307 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
308 if ((pe = pmu_event_get(event_name, &idx)) == NULL)
310 if (pe->alias && (pe = pmu_event_get(pe->alias, &idx)) == NULL)
312 if (pe->event == NULL)
314 if (pmu_parse_event(&ped, pe->event))
317 for (idx = 0; fixed_mode_cntrs[idx] != NULL; idx++)
318 if (strcmp(fixed_mode_cntrs[idx], event_name) == 0)
322 if (strcasestr(pe->desc, "retired") != NULL)
323 pm->pm_ev = PMC_EV_IAF_INSTR_RETIRED_ANY;
324 else if (strcasestr(pe->desc, "core") != NULL ||
325 strcasestr(pe->desc, "unhalted"))
326 pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_CORE;
327 else if (strcasestr(pe->desc, "ref") != NULL)
328 pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_REF;
329 iaf->pm_iaf_flags |= (IAF_USR | IAF_OS);
331 iaf->pm_iaf_flags |= IAF_ANY;
332 if (pm->pm_caps & PMC_CAP_INTERRUPT)
333 iaf->pm_iaf_flags |= IAF_PMI;
334 pm->pm_class = PMC_CLASS_IAF;
337 pm->pm_caps |= PMC_CAP_QUALIFIER;
338 pm->pm_class = PMC_CLASS_IAP;
340 iap->pm_iap_config |= IAP_EVSEL(ped.ped_event);
341 iap->pm_iap_config |= IAP_UMASK(ped.ped_umask);
342 iap->pm_iap_config |= IAP_CMASK(ped.ped_cmask);
343 iap->pm_iap_rsp = ped.ped_offcore_rsp;
345 iap->pm_iap_config |= (IAP_USR | IAP_OS);
347 iap->pm_iap_config |= IAP_EDGE;
349 iap->pm_iap_config |= IAP_ANY;
351 iap->pm_iap_config |= IAP_EDGE;
352 if (pm->pm_caps & PMC_CAP_INTERRUPT)
353 iap->pm_iap_config |= IAP_INT;
358 * Ultimately rely on AMD calling theirs the same
360 static const char *stat_mode_cntrs[] = {
361 "cpu_clk_unhalted.thread_any",
363 "br_inst_retired.all_branches",
364 "br_misp_retired.all_branches",
365 "longest_lat_cache.reference",
366 "longest_lat_cache.miss",
370 pmc_pmu_stat_mode(const char ***cntrs)
372 if (pmc_pmu_enabled()) {
373 *cntrs = stat_mode_cntrs;
380 uint64_t pmc_pmu_sample_rate_get(const char *event_name __unused) { return (DEFAULT_SAMPLE_COUNT); }
381 void pmc_pmu_print_counters(void) {}
382 void pmc_pmu_print_counter_desc(const char *e __unused) {}
383 void pmc_pmu_print_counter_desc_long(const char *e __unused) {}
384 int pmc_pmu_enabled(void) { return (0); }
385 int pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused) { return (EOPNOTSUPP); }
386 const char *pmc_pmu_event_get_by_idx(int idx __unused) { return (NULL); }
387 int pmc_pmu_stat_mode(const char ***a __unused) { return (EOPNOTSUPP); }