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, *r;
162 if ((event = strdup(eventin)) == NULL)
165 bzero(ped, sizeof(*ped));
166 while ((kvp = strsep(&event, ",")) != NULL) {
167 key = strsep(&kvp, "=");
171 if (strcmp(key, "umask") == 0)
172 ped->ped_umask = strtol(value, NULL, 16);
173 else if (strcmp(key, "event") == 0)
174 ped->ped_event = strtol(value, NULL, 16);
175 else if (strcmp(key, "period") == 0)
176 ped->ped_period = strtol(value, NULL, 10);
177 else if (strcmp(key, "offcore_rsp") == 0)
178 ped->ped_offcore_rsp = strtol(value, NULL, 16);
179 else if (strcmp(key, "any") == 0)
180 ped->ped_any = strtol(value, NULL, 10);
181 else if (strcmp(key, "cmask") == 0)
182 ped->ped_cmask = strtol(value, NULL, 10);
183 else if (strcmp(key, "inv") == 0)
184 ped->ped_inv = strtol(value, NULL, 10);
185 else if (strcmp(key, "edge") == 0)
186 ped->ped_edge = strtol(value, NULL, 10);
187 else if (strcmp(key, "frontend") == 0)
188 ped->ped_frontend = strtol(value, NULL, 16);
189 else if (strcmp(key, "ldlat") == 0)
190 ped->ped_ldlat = strtol(value, NULL, 16);
191 else if (strcmp(key, "fc_mask") == 0)
192 ped->ped_fc_mask = strtol(value, NULL, 16);
193 else if (strcmp(key, "ch_mask") == 0)
194 ped->ped_ch_mask = strtol(value, NULL, 16);
195 else if (strcmp(key, "config1") == 0)
196 ped->ped_config1 = strtol(value, NULL, 16);
198 debug = getenv("PMUDEBUG");
199 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
200 printf("unrecognized kvpair: %s:%s\n", key, value);
208 pmc_pmu_sample_rate_get(const char *event_name)
210 const struct pmu_event *pe;
211 struct pmu_event_desc ped;
213 event_name = pmu_alias_get(event_name);
214 if ((pe = pmu_event_get(event_name, NULL)) == NULL)
215 return (DEFAULT_SAMPLE_COUNT);
216 if (pe->alias && (pe = pmu_event_get(pe->alias, NULL)) == NULL)
217 return (DEFAULT_SAMPLE_COUNT);
218 if (pe->event == NULL)
219 return (DEFAULT_SAMPLE_COUNT);
220 if (pmu_parse_event(&ped, pe->event))
221 return (DEFAULT_SAMPLE_COUNT);
222 return (ped.ped_period);
226 pmc_pmu_enabled(void)
229 return (pmu_events_map_get() != NULL);
233 pmc_pmu_print_counters(void)
235 const struct pmu_events_map *pme;
236 const struct pmu_event *pe;
237 struct pmu_event_desc ped;
241 debug = getenv("PMUDEBUG");
244 if (debug != NULL && strcmp(debug, "true") == 0)
246 if ((pme = pmu_events_map_get()) == NULL)
248 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
249 if (pe->name == NULL)
251 printf("\t%s\n", pe->name);
253 pmu_parse_event(&ped, pe->event);
258 pmc_pmu_print_counter_desc(const char *ev)
260 const struct pmu_events_map *pme;
261 const struct pmu_event *pe;
263 if ((pme = pmu_events_map_get()) == NULL)
265 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
266 if (pe->name == NULL)
268 if (strcasestr(pe->name, ev) != NULL &&
270 printf("%s:\t%s\n", pe->name, pe->desc);
275 pmc_pmu_print_counter_desc_long(const char *ev)
277 const struct pmu_events_map *pme;
278 const struct pmu_event *pe;
280 if ((pme = pmu_events_map_get()) == NULL)
282 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
283 if (pe->name == NULL)
285 if (strcasestr(pe->name, ev) != NULL) {
286 if (pe->long_desc != NULL)
287 printf("%s:\n%s\n", pe->name, pe->long_desc);
288 else if (pe->desc != NULL)
289 printf("%s:\t%s\n", pe->name, pe->desc);
295 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
297 const struct pmu_event *pe;
298 struct pmu_event_desc ped;
299 struct pmc_md_iap_op_pmcallocate *iap;
300 struct pmc_md_iaf_op_pmcallocate *iaf;
303 iap = &pm->pm_md.pm_iap;
304 iaf = &pm->pm_md.pm_iaf;
306 bzero(iap, sizeof(*iap));
307 event_name = pmu_alias_get(event_name);
308 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
309 if ((pe = pmu_event_get(event_name, &idx)) == NULL)
311 if (pe->alias && (pe = pmu_event_get(pe->alias, &idx)) == NULL)
313 if (pe->event == NULL)
315 if (pmu_parse_event(&ped, pe->event))
318 for (idx = 0; fixed_mode_cntrs[idx] != NULL; idx++)
319 if (strcmp(fixed_mode_cntrs[idx], event_name) == 0)
323 if (strcasestr(pe->desc, "retired") != NULL)
324 pm->pm_ev = PMC_EV_IAF_INSTR_RETIRED_ANY;
325 else if (strcasestr(pe->desc, "core") != NULL ||
326 strcasestr(pe->desc, "unhalted"))
327 pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_CORE;
328 else if (strcasestr(pe->desc, "ref") != NULL)
329 pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_REF;
330 iaf->pm_iaf_flags |= (IAF_USR | IAF_OS);
332 iaf->pm_iaf_flags |= IAF_ANY;
333 if (pm->pm_caps & PMC_CAP_INTERRUPT)
334 iaf->pm_iaf_flags |= IAF_PMI;
335 pm->pm_class = PMC_CLASS_IAF;
338 pm->pm_caps |= PMC_CAP_QUALIFIER;
339 pm->pm_class = PMC_CLASS_IAP;
341 iap->pm_iap_config |= IAP_EVSEL(ped.ped_event);
342 iap->pm_iap_config |= IAP_UMASK(ped.ped_umask);
343 iap->pm_iap_config |= IAP_CMASK(ped.ped_cmask);
344 iap->pm_iap_rsp = ped.ped_offcore_rsp;
346 iap->pm_iap_config |= (IAP_USR | IAP_OS);
348 iap->pm_iap_config |= IAP_EDGE;
350 iap->pm_iap_config |= IAP_ANY;
352 iap->pm_iap_config |= IAP_EDGE;
353 if (pm->pm_caps & PMC_CAP_INTERRUPT)
354 iap->pm_iap_config |= IAP_INT;
359 * Ultimately rely on AMD calling theirs the same
361 static const char *stat_mode_cntrs[] = {
362 "cpu_clk_unhalted.thread_any",
364 "br_inst_retired.all_branches",
365 "br_misp_retired.all_branches",
366 "longest_lat_cache.reference",
367 "longest_lat_cache.miss",
371 pmc_pmu_stat_mode(const char ***cntrs)
373 if (pmc_pmu_enabled()) {
374 *cntrs = stat_mode_cntrs;
381 uint64_t pmc_pmu_sample_rate_get(const char *event_name __unused) { return (DEFAULT_SAMPLE_COUNT); }
382 void pmc_pmu_print_counters(void) {}
383 void pmc_pmu_print_counter_desc(const char *e __unused) {}
384 void pmc_pmu_print_counter_desc_long(const char *e __unused) {}
385 int pmc_pmu_enabled(void) { return (0); }
386 int pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused) { return (EOPNOTSUPP); }
387 const char *pmc_pmu_event_get_by_idx(int idx __unused) { return (NULL); }
388 int pmc_pmu_stat_mode(const char ***a __unused) { return (EOPNOTSUPP); }