xref: /freebsd/lib/libpmc/libpmc_pmu_util.c (revision fbf962e6bb51d2d51ab3275bd1e3ea428a919434)
1959826caSMatt Macy /*-
2959826caSMatt Macy  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3959826caSMatt Macy  *
4959826caSMatt Macy  * Copyright (c) 2018, Matthew Macy
5959826caSMatt Macy  *
6959826caSMatt Macy  * Redistribution and use in source and binary forms, with or without
7959826caSMatt Macy  * modification, are permitted provided that the following conditions
8959826caSMatt Macy  * are met:
9959826caSMatt Macy  * 1. Redistributions of source code must retain the above copyright
10959826caSMatt Macy  *    notice, this list of conditions and the following disclaimer.
11959826caSMatt Macy  * 2. Redistributions in binary form must reproduce the above copyright
12959826caSMatt Macy  *    notice, this list of conditions and the following disclaimer in the
13959826caSMatt Macy  *    documentation and/or other materials provided with the distribution.
14959826caSMatt Macy  *
15959826caSMatt Macy  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16959826caSMatt Macy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17959826caSMatt Macy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18959826caSMatt Macy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19959826caSMatt Macy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20959826caSMatt Macy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21959826caSMatt Macy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22959826caSMatt Macy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23959826caSMatt Macy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24959826caSMatt Macy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25959826caSMatt Macy  * SUCH DAMAGE.
26959826caSMatt Macy  *
27959826caSMatt Macy  * $FreeBSD$
28959826caSMatt Macy  *
29959826caSMatt Macy  */
30959826caSMatt Macy 
31959826caSMatt Macy #include <sys/types.h>
32959826caSMatt Macy #include <sys/errno.h>
33959826caSMatt Macy #include <sys/sysctl.h>
34959826caSMatt Macy #include <stddef.h>
35959826caSMatt Macy #include <stdlib.h>
36959826caSMatt Macy #include <limits.h>
37959826caSMatt Macy #include <string.h>
38959826caSMatt Macy #include <pmc.h>
39959826caSMatt Macy #include <pmclog.h>
40959826caSMatt Macy #include <libpmcstat.h>
41959826caSMatt Macy #include "pmu-events/pmu-events.h"
42959826caSMatt Macy 
437d1c2b74SMatt Macy #if defined(__amd64__) || defined(__i386__)
44959826caSMatt Macy struct pmu_alias {
45959826caSMatt Macy 	const char *pa_alias;
46959826caSMatt Macy 	const char *pa_name;
47959826caSMatt Macy };
48959826caSMatt Macy static struct pmu_alias pmu_alias_table[] = {
49959826caSMatt Macy 	{"UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
50959826caSMatt Macy 	{"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
51959826caSMatt Macy 	{"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
52959826caSMatt Macy 	{"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
53959826caSMatt Macy 	{"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
54959826caSMatt Macy 	{"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
55959826caSMatt Macy 	{"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
56959826caSMatt Macy 	{"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
57959826caSMatt Macy 	{"RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
58959826caSMatt Macy 	{"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
59959826caSMatt Macy 	{"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
60959826caSMatt Macy 	{"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
61959826caSMatt Macy 	{"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
62959826caSMatt Macy 	{"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
6396cbd26aSMatt Macy 	{"cycles", "tsc-tsc"},
6496cbd26aSMatt Macy 	{"instructions", "inst-retired.any_p"},
6596cbd26aSMatt Macy 	{"branch-mispredicts", "br_misp_retired.all_branches" },
6696cbd26aSMatt Macy 	{"branches", "br_inst_retired.all_branches" },
6796cbd26aSMatt Macy 	{"interrupts", "hw_interrupts.received"},
6896cbd26aSMatt Macy 	{"ic-misses", "frontend_retired.l1i_miss"},
69959826caSMatt Macy 	{NULL, NULL},
70959826caSMatt Macy };
71959826caSMatt Macy 
72a191ed2dSMatt Macy static const char *fixed_mode_cntrs[] = {
73a191ed2dSMatt Macy 	"inst_retired.any",
74a191ed2dSMatt Macy 	"cpu_clk_unhalted.thread",
75a191ed2dSMatt Macy 	"cpu_clk_unhalted.thread_any",
76a191ed2dSMatt Macy 	"cpu_clk_unhalted.ref_tsc",
77a191ed2dSMatt Macy 	NULL
78a191ed2dSMatt Macy };
79a191ed2dSMatt Macy 
80959826caSMatt Macy static const char *
81959826caSMatt Macy pmu_alias_get(const char *name)
82959826caSMatt Macy {
83959826caSMatt Macy 	struct pmu_alias *pa;
84959826caSMatt Macy 
85959826caSMatt Macy 	for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
86959826caSMatt Macy 		if (strcasecmp(name, pa->pa_alias) == 0)
87959826caSMatt Macy 			return (pa->pa_name);
88959826caSMatt Macy 	return (name);
89959826caSMatt Macy }
90959826caSMatt Macy 
91959826caSMatt Macy struct pmu_event_desc {
92959826caSMatt Macy 	uint64_t ped_period;
93959826caSMatt Macy 	uint64_t ped_offcore_rsp;
94959826caSMatt Macy 	uint32_t ped_event;
95959826caSMatt Macy 	uint32_t ped_frontend;
96959826caSMatt Macy 	uint32_t ped_ldlat;
97959826caSMatt Macy 	uint32_t ped_config1;
98959826caSMatt Macy 	uint8_t	ped_umask;
99959826caSMatt Macy 	uint8_t	ped_cmask;
100959826caSMatt Macy 	uint8_t	ped_any;
101959826caSMatt Macy 	uint8_t	ped_inv;
102959826caSMatt Macy 	uint8_t	ped_edge;
103959826caSMatt Macy 	uint8_t	ped_fc_mask;
104959826caSMatt Macy 	uint8_t	ped_ch_mask;
105959826caSMatt Macy };
106959826caSMatt Macy 
107959826caSMatt Macy static const struct pmu_events_map *
108959826caSMatt Macy pmu_events_map_get(void)
109959826caSMatt Macy {
110959826caSMatt Macy 	size_t s;
111959826caSMatt Macy 	char buf[64];
112959826caSMatt Macy 	const struct pmu_events_map *pme;
113959826caSMatt Macy 
114959826caSMatt Macy 	if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
115959826caSMatt Macy 	    (void *)NULL, 0) == -1)
116959826caSMatt Macy 		return (NULL);
117959826caSMatt Macy 	if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
118959826caSMatt Macy 	    (void *)NULL, 0) == -1)
119959826caSMatt Macy 		return (NULL);
120959826caSMatt Macy 	for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
121959826caSMatt Macy 		if (strcmp(buf, pme->cpuid) == 0)
122959826caSMatt Macy 			return (pme);
123959826caSMatt Macy 	return (NULL);
124959826caSMatt Macy }
125959826caSMatt Macy 
126959826caSMatt Macy static const struct pmu_event *
127959826caSMatt Macy pmu_event_get(const char *event_name, int *idx)
128959826caSMatt Macy {
129959826caSMatt Macy 	const struct pmu_events_map *pme;
130959826caSMatt Macy 	const struct pmu_event *pe;
131959826caSMatt Macy 	int i;
132959826caSMatt Macy 
133959826caSMatt Macy 	if ((pme = pmu_events_map_get()) == NULL)
134959826caSMatt Macy 		return (NULL);
135959826caSMatt Macy 	for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
136959826caSMatt Macy 		if (pe->name == NULL)
137959826caSMatt Macy 			continue;
138959826caSMatt Macy 		if (strcasecmp(pe->name, event_name) == 0) {
139959826caSMatt Macy 			if (idx)
140959826caSMatt Macy 				*idx = i;
141959826caSMatt Macy 			return (pe);
142959826caSMatt Macy 		}
143959826caSMatt Macy 	}
144959826caSMatt Macy 	return (NULL);
145959826caSMatt Macy }
146959826caSMatt Macy 
147959826caSMatt Macy const char *
14878beed2fSMatt Macy pmc_pmu_event_get_by_idx(int idx)
149959826caSMatt Macy {
150959826caSMatt Macy 	const struct pmu_events_map *pme;
151959826caSMatt Macy 	const struct pmu_event *pe;
152959826caSMatt Macy 	int i;
153959826caSMatt Macy 
154959826caSMatt Macy 	if ((pme = pmu_events_map_get()) == NULL)
155959826caSMatt Macy 		return (NULL);
1568b20f975SMatt Macy 	for (i = 0, pe = pme->table; (pe->name || pe->desc || pe->event) && i < idx; pe++, i++);
157959826caSMatt Macy 	return (pe->name);
158959826caSMatt Macy }
159959826caSMatt Macy 
160959826caSMatt Macy static int
161959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
162959826caSMatt Macy {
163959826caSMatt Macy 	char *event;
1648bed125dSMatt Macy 	char *kvp, *key, *value, *r;
165959826caSMatt Macy 	char *debug;
166959826caSMatt Macy 
167959826caSMatt Macy 	if ((event = strdup(eventin)) == NULL)
168959826caSMatt Macy 		return (ENOMEM);
1698bed125dSMatt Macy 	r = event;
170959826caSMatt Macy 	bzero(ped, sizeof(*ped));
171959826caSMatt Macy 	while ((kvp = strsep(&event, ",")) != NULL) {
172959826caSMatt Macy 		key = strsep(&kvp, "=");
173959826caSMatt Macy 		if (key == NULL)
174959826caSMatt Macy 			abort();
175959826caSMatt Macy 		value = kvp;
176959826caSMatt Macy 		if (strcmp(key, "umask") == 0)
177959826caSMatt Macy 			ped->ped_umask = strtol(value, NULL, 16);
178959826caSMatt Macy 		else if (strcmp(key, "event") == 0)
179959826caSMatt Macy 			ped->ped_event = strtol(value, NULL, 16);
180959826caSMatt Macy 		else if (strcmp(key, "period") == 0)
181959826caSMatt Macy 			ped->ped_period = strtol(value, NULL, 10);
182959826caSMatt Macy 		else if (strcmp(key, "offcore_rsp") == 0)
183959826caSMatt Macy 			ped->ped_offcore_rsp = strtol(value, NULL, 16);
184959826caSMatt Macy 		else if (strcmp(key, "any") == 0)
185959826caSMatt Macy 			ped->ped_any = strtol(value, NULL, 10);
186959826caSMatt Macy 		else if (strcmp(key, "cmask") == 0)
187959826caSMatt Macy 			ped->ped_cmask = strtol(value, NULL, 10);
188959826caSMatt Macy 		else if (strcmp(key, "inv") == 0)
189959826caSMatt Macy 			ped->ped_inv = strtol(value, NULL, 10);
190959826caSMatt Macy 		else if (strcmp(key, "edge") == 0)
191959826caSMatt Macy 			ped->ped_edge = strtol(value, NULL, 10);
192959826caSMatt Macy 		else if (strcmp(key, "frontend") == 0)
193959826caSMatt Macy 			ped->ped_frontend = strtol(value, NULL, 16);
194959826caSMatt Macy 		else if (strcmp(key, "ldlat") == 0)
195959826caSMatt Macy 			ped->ped_ldlat = strtol(value, NULL, 16);
196959826caSMatt Macy 		else if (strcmp(key, "fc_mask") == 0)
197959826caSMatt Macy 			ped->ped_fc_mask = strtol(value, NULL, 16);
198959826caSMatt Macy 		else if (strcmp(key, "ch_mask") == 0)
199959826caSMatt Macy 			ped->ped_ch_mask = strtol(value, NULL, 16);
200959826caSMatt Macy 		else if (strcmp(key, "config1") == 0)
201959826caSMatt Macy 			ped->ped_config1 = strtol(value, NULL, 16);
202959826caSMatt Macy 		else {
203959826caSMatt Macy 			debug = getenv("PMUDEBUG");
204959826caSMatt Macy 			if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
205959826caSMatt Macy 				printf("unrecognized kvpair: %s:%s\n", key, value);
206959826caSMatt Macy 		}
207959826caSMatt Macy 	}
2088bed125dSMatt Macy 	free(r);
209959826caSMatt Macy 	return (0);
210959826caSMatt Macy }
211959826caSMatt Macy 
212959826caSMatt Macy uint64_t
213959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name)
214959826caSMatt Macy {
215959826caSMatt Macy 	const struct pmu_event *pe;
216959826caSMatt Macy 	struct pmu_event_desc ped;
217959826caSMatt Macy 
218959826caSMatt Macy 	event_name = pmu_alias_get(event_name);
219959826caSMatt Macy 	if ((pe = pmu_event_get(event_name, NULL)) == NULL)
220959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
221959826caSMatt Macy 	if (pe->alias && (pe = pmu_event_get(pe->alias, NULL)) == NULL)
222959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
223959826caSMatt Macy 	if (pe->event == NULL)
224959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
225959826caSMatt Macy 	if (pmu_parse_event(&ped, pe->event))
226959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
227959826caSMatt Macy 	return (ped.ped_period);
228959826caSMatt Macy }
229959826caSMatt Macy 
230959826caSMatt Macy int
231959826caSMatt Macy pmc_pmu_enabled(void)
232959826caSMatt Macy {
233959826caSMatt Macy 
234959826caSMatt Macy 	return (pmu_events_map_get() != NULL);
235959826caSMatt Macy }
236959826caSMatt Macy 
237959826caSMatt Macy void
238*fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name)
239959826caSMatt Macy {
240959826caSMatt Macy 	const struct pmu_events_map *pme;
241959826caSMatt Macy 	const struct pmu_event *pe;
242959826caSMatt Macy 	struct pmu_event_desc ped;
243959826caSMatt Macy 	char *debug;
244959826caSMatt Macy 	int do_debug;
245959826caSMatt Macy 
246959826caSMatt Macy 	debug = getenv("PMUDEBUG");
247959826caSMatt Macy 	do_debug = 0;
248959826caSMatt Macy 
249959826caSMatt Macy 	if (debug != NULL && strcmp(debug, "true") == 0)
250959826caSMatt Macy 		do_debug = 1;
251959826caSMatt Macy 	if ((pme = pmu_events_map_get()) == NULL)
252959826caSMatt Macy 		return;
253959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
254959826caSMatt Macy 		if (pe->name == NULL)
255959826caSMatt Macy 			continue;
256*fbf962e6SMatt Macy 		if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
257*fbf962e6SMatt Macy 			continue;
258959826caSMatt Macy 		printf("\t%s\n", pe->name);
259959826caSMatt Macy 		if (do_debug)
260959826caSMatt Macy 			pmu_parse_event(&ped, pe->event);
261959826caSMatt Macy 	}
262959826caSMatt Macy }
263959826caSMatt Macy 
264959826caSMatt Macy void
265959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev)
266959826caSMatt Macy {
267959826caSMatt Macy 	const struct pmu_events_map *pme;
268959826caSMatt Macy 	const struct pmu_event *pe;
269959826caSMatt Macy 
270959826caSMatt Macy 	if ((pme = pmu_events_map_get()) == NULL)
271959826caSMatt Macy 		return;
272959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
273959826caSMatt Macy 		if (pe->name == NULL)
274959826caSMatt Macy 			continue;
275959826caSMatt Macy 		if (strcasestr(pe->name, ev) != NULL &&
276959826caSMatt Macy 		    pe->desc != NULL)
277959826caSMatt Macy 			printf("%s:\t%s\n", pe->name, pe->desc);
278959826caSMatt Macy 	}
279959826caSMatt Macy }
280959826caSMatt Macy 
281959826caSMatt Macy void
282959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev)
283959826caSMatt Macy {
284959826caSMatt Macy 	const struct pmu_events_map *pme;
285959826caSMatt Macy 	const struct pmu_event *pe;
286959826caSMatt Macy 
287959826caSMatt Macy 	if ((pme = pmu_events_map_get()) == NULL)
288959826caSMatt Macy 		return;
289959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
290959826caSMatt Macy 		if (pe->name == NULL)
291959826caSMatt Macy 			continue;
292959826caSMatt Macy 		if (strcasestr(pe->name, ev) != NULL) {
293959826caSMatt Macy 			if (pe->long_desc != NULL)
294959826caSMatt Macy 				printf("%s:\n%s\n", pe->name, pe->long_desc);
295959826caSMatt Macy 			else if (pe->desc != NULL)
296959826caSMatt Macy 				printf("%s:\t%s\n", pe->name, pe->desc);
297959826caSMatt Macy 		}
298959826caSMatt Macy 	}
299959826caSMatt Macy }
300959826caSMatt Macy 
301*fbf962e6SMatt Macy void
302*fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev)
303*fbf962e6SMatt Macy {
304*fbf962e6SMatt Macy 	const struct pmu_events_map *pme;
305*fbf962e6SMatt Macy 	const struct pmu_event *pe;
306*fbf962e6SMatt Macy 
307*fbf962e6SMatt Macy 	if ((pme = pmu_events_map_get()) == NULL)
308*fbf962e6SMatt Macy 		return;
309*fbf962e6SMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
310*fbf962e6SMatt Macy 		if (pe->name == NULL)
311*fbf962e6SMatt Macy 			continue;
312*fbf962e6SMatt Macy 		if (strcasestr(pe->name, ev) == NULL)
313*fbf962e6SMatt Macy 			continue;
314*fbf962e6SMatt Macy 		printf("name: %s\n", pe->name);
315*fbf962e6SMatt Macy 		if (pe->long_desc != NULL)
316*fbf962e6SMatt Macy 			printf("desc: %s\n", pe->long_desc);
317*fbf962e6SMatt Macy 		else if (pe->desc != NULL)
318*fbf962e6SMatt Macy 			printf("desc: %s\n", pe->desc);
319*fbf962e6SMatt Macy 		if (pe->event != NULL)
320*fbf962e6SMatt Macy 			printf("event: %s\n", pe->event);
321*fbf962e6SMatt Macy 		if (pe->topic != NULL)
322*fbf962e6SMatt Macy 			printf("topic: %s\n", pe->topic);
323*fbf962e6SMatt Macy 		if (pe->pmu != NULL)
324*fbf962e6SMatt Macy 			printf("pmu: %s\n", pe->pmu);
325*fbf962e6SMatt Macy 		if (pe->unit != NULL)
326*fbf962e6SMatt Macy 			printf("unit: %s\n", pe->unit);
327*fbf962e6SMatt Macy 		if (pe->perpkg != NULL)
328*fbf962e6SMatt Macy 			printf("perpkg: %s\n", pe->perpkg);
329*fbf962e6SMatt Macy 		if (pe->metric_expr != NULL)
330*fbf962e6SMatt Macy 			printf("metric_expr: %s\n", pe->metric_expr);
331*fbf962e6SMatt Macy 		if (pe->metric_name != NULL)
332*fbf962e6SMatt Macy 			printf("metric_name: %s\n", pe->metric_name);
333*fbf962e6SMatt Macy 		if (pe->metric_group != NULL)
334*fbf962e6SMatt Macy 			printf("metric_group: %s\n", pe->metric_group);
335*fbf962e6SMatt Macy 	}
336*fbf962e6SMatt Macy }
337*fbf962e6SMatt Macy 
338959826caSMatt Macy int
339959826caSMatt Macy pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
340959826caSMatt Macy {
341959826caSMatt Macy 	const struct pmu_event *pe;
342959826caSMatt Macy 	struct pmu_event_desc ped;
343959826caSMatt Macy 	struct pmc_md_iap_op_pmcallocate *iap;
344a191ed2dSMatt Macy 	struct pmc_md_iaf_op_pmcallocate *iaf;
345a191ed2dSMatt Macy 	int idx, isfixed;
346959826caSMatt Macy 
347959826caSMatt Macy 	iap = &pm->pm_md.pm_iap;
348a191ed2dSMatt Macy 	isfixed = 0;
349959826caSMatt Macy 	bzero(iap, sizeof(*iap));
350959826caSMatt Macy 	event_name = pmu_alias_get(event_name);
351a191ed2dSMatt Macy 	pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
352959826caSMatt Macy 	if ((pe = pmu_event_get(event_name, &idx)) == NULL)
353959826caSMatt Macy 		return (ENOENT);
354959826caSMatt Macy 	if (pe->alias && (pe = pmu_event_get(pe->alias, &idx)) == NULL)
355959826caSMatt Macy 		return (ENOENT);
356959826caSMatt Macy 	if (pe->event == NULL)
357959826caSMatt Macy 		return (ENOENT);
358959826caSMatt Macy 	if (pmu_parse_event(&ped, pe->event))
359959826caSMatt Macy 		return (ENOENT);
360959826caSMatt Macy 
361a191ed2dSMatt Macy 	for (idx = 0; fixed_mode_cntrs[idx] != NULL; idx++)
362c53c63f6SMatt Macy 		if (strcmp(fixed_mode_cntrs[idx], event_name) == 0)
363a191ed2dSMatt Macy 			isfixed = 1;
364a191ed2dSMatt Macy 	if (isfixed) {
365785dd70dSMatt Macy 		iaf = &pm->pm_md.pm_iaf;
366785dd70dSMatt Macy 		pm->pm_class = PMC_CLASS_IAF;
367a191ed2dSMatt Macy 		if (strcasestr(pe->desc, "retired") != NULL)
368a191ed2dSMatt Macy 			pm->pm_ev = PMC_EV_IAF_INSTR_RETIRED_ANY;
369a191ed2dSMatt Macy 		else if (strcasestr(pe->desc, "core") != NULL ||
370a191ed2dSMatt Macy 		    strcasestr(pe->desc, "unhalted"))
371a191ed2dSMatt Macy 			pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_CORE;
372a191ed2dSMatt Macy 		else if (strcasestr(pe->desc, "ref") != NULL)
373a191ed2dSMatt Macy 			pm->pm_ev = PMC_EV_IAF_CPU_CLK_UNHALTED_REF;
374a191ed2dSMatt Macy 		iaf->pm_iaf_flags |= (IAF_USR | IAF_OS);
375a191ed2dSMatt Macy 		if (ped.ped_any)
376a191ed2dSMatt Macy 			iaf->pm_iaf_flags |= IAF_ANY;
377a191ed2dSMatt Macy 		if (pm->pm_caps & PMC_CAP_INTERRUPT)
378a191ed2dSMatt Macy 			iaf->pm_iaf_flags |= IAF_PMI;
379a191ed2dSMatt Macy 		return (0);
380785dd70dSMatt Macy 	} else if (strcasestr(event_name, "UNC_") == event_name ||
381785dd70dSMatt Macy 			   strcasestr(event_name, "uncore") != NULL) {
382785dd70dSMatt Macy 		pm->pm_class = PMC_CLASS_UCP;
383785dd70dSMatt Macy 	} else {
384a191ed2dSMatt Macy 		pm->pm_caps |= PMC_CAP_QUALIFIER;
385959826caSMatt Macy 		pm->pm_class = PMC_CLASS_IAP;
386785dd70dSMatt Macy 	}
387959826caSMatt Macy 	pm->pm_ev = idx;
388959826caSMatt Macy 	iap->pm_iap_config |= IAP_EVSEL(ped.ped_event);
389959826caSMatt Macy 	iap->pm_iap_config |= IAP_UMASK(ped.ped_umask);
390959826caSMatt Macy 	iap->pm_iap_config |= IAP_CMASK(ped.ped_cmask);
391959826caSMatt Macy 	iap->pm_iap_rsp = ped.ped_offcore_rsp;
392959826caSMatt Macy 
393959826caSMatt Macy 	iap->pm_iap_config |= (IAP_USR | IAP_OS);
394959826caSMatt Macy 	if (ped.ped_edge)
395959826caSMatt Macy 		iap->pm_iap_config |= IAP_EDGE;
396959826caSMatt Macy 	if (ped.ped_any)
397959826caSMatt Macy 		iap->pm_iap_config |= IAP_ANY;
398959826caSMatt Macy 	if (ped.ped_inv)
399959826caSMatt Macy 		iap->pm_iap_config |= IAP_EDGE;
400959826caSMatt Macy 	if (pm->pm_caps & PMC_CAP_INTERRUPT)
401959826caSMatt Macy 		iap->pm_iap_config |= IAP_INT;
402959826caSMatt Macy 	return (0);
403959826caSMatt Macy }
404959826caSMatt Macy 
40578beed2fSMatt Macy /*
40678beed2fSMatt Macy  * Ultimately rely on AMD calling theirs the same
40778beed2fSMatt Macy  */
40878beed2fSMatt Macy static const char *stat_mode_cntrs[] = {
409a191ed2dSMatt Macy 	"cpu_clk_unhalted.thread_any",
41078beed2fSMatt Macy 	"inst_retired.any",
41178beed2fSMatt Macy 	"br_inst_retired.all_branches",
41278beed2fSMatt Macy 	"br_misp_retired.all_branches",
413a191ed2dSMatt Macy 	"longest_lat_cache.reference",
414a191ed2dSMatt Macy 	"longest_lat_cache.miss",
41578beed2fSMatt Macy };
41678beed2fSMatt Macy 
41778beed2fSMatt Macy int
41878beed2fSMatt Macy pmc_pmu_stat_mode(const char ***cntrs)
41978beed2fSMatt Macy {
42078beed2fSMatt Macy 	if (pmc_pmu_enabled()) {
42178beed2fSMatt Macy 		*cntrs = stat_mode_cntrs;
42278beed2fSMatt Macy 		return (0);
42378beed2fSMatt Macy 	}
42478beed2fSMatt Macy 	return (EOPNOTSUPP);
42578beed2fSMatt Macy }
42678beed2fSMatt Macy 
427959826caSMatt Macy #else
4288b20f975SMatt Macy 
4298b20f975SMatt Macy uint64_t
4308b20f975SMatt Macy pmc_pmu_sample_rate_get(const char *event_name __unused)
4318b20f975SMatt Macy {
4328b20f975SMatt Macy 	return (DEFAULT_SAMPLE_COUNT);
4338b20f975SMatt Macy }
4348b20f975SMatt Macy 
4358b20f975SMatt Macy void
436*fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name __unused)
4378b20f975SMatt Macy {
4388b20f975SMatt Macy }
4398b20f975SMatt Macy 
4408b20f975SMatt Macy void
4418b20f975SMatt Macy pmc_pmu_print_counter_desc(const char *e __unused)
4428b20f975SMatt Macy {
4438b20f975SMatt Macy }
4448b20f975SMatt Macy 
4458b20f975SMatt Macy void
4468b20f975SMatt Macy pmc_pmu_print_counter_desc_long(const char *e __unused)
4478b20f975SMatt Macy {
4488b20f975SMatt Macy }
4498b20f975SMatt Macy 
450*fbf962e6SMatt Macy void
451*fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *e __unused)
452*fbf962e6SMatt Macy {
453*fbf962e6SMatt Macy 
454*fbf962e6SMatt Macy }
455*fbf962e6SMatt Macy 
4568b20f975SMatt Macy int
4578b20f975SMatt Macy pmc_pmu_enabled(void)
4588b20f975SMatt Macy {
4598b20f975SMatt Macy 	return (0);
4608b20f975SMatt Macy }
4618b20f975SMatt Macy 
4628b20f975SMatt Macy int
4638b20f975SMatt Macy pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
4648b20f975SMatt Macy {
4658b20f975SMatt Macy 	return (EOPNOTSUPP);
4668b20f975SMatt Macy }
4678b20f975SMatt Macy 
4688b20f975SMatt Macy const char *
4698b20f975SMatt Macy pmc_pmu_event_get_by_idx(int idx __unused)
4708b20f975SMatt Macy {
4718b20f975SMatt Macy 	return (NULL);
4728b20f975SMatt Macy }
4738b20f975SMatt Macy int
4748b20f975SMatt Macy pmc_pmu_stat_mode(const char ***a __unused)
4758b20f975SMatt Macy {
4768b20f975SMatt Macy 	return (EOPNOTSUPP);
4778b20f975SMatt Macy }
478959826caSMatt Macy 
479959826caSMatt Macy #endif
480