xref: /freebsd/lib/libpmc/libpmc_pmu_util.c (revision bfb46e2ba8bda92b9f94fabea65ae23d489ef787)
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 
7207d80fd8SMatt Macy /*
7307d80fd8SMatt Macy  *  The Intel fixed mode counters are:
7407d80fd8SMatt Macy  *	"inst_retired.any",
7507d80fd8SMatt Macy  *	"cpu_clk_unhalted.thread",
7607d80fd8SMatt Macy  *	"cpu_clk_unhalted.thread_any",
7707d80fd8SMatt Macy  *	"cpu_clk_unhalted.ref_tsc",
7807d80fd8SMatt Macy  *
7907d80fd8SMatt Macy  */
80a191ed2dSMatt Macy 
81959826caSMatt Macy static const char *
82959826caSMatt Macy pmu_alias_get(const char *name)
83959826caSMatt Macy {
84959826caSMatt Macy 	struct pmu_alias *pa;
85959826caSMatt Macy 
86959826caSMatt Macy 	for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
87959826caSMatt Macy 		if (strcasecmp(name, pa->pa_alias) == 0)
88959826caSMatt Macy 			return (pa->pa_name);
89959826caSMatt Macy 	return (name);
90959826caSMatt Macy }
91959826caSMatt Macy 
92959826caSMatt Macy struct pmu_event_desc {
93959826caSMatt Macy 	uint64_t ped_period;
94959826caSMatt Macy 	uint64_t ped_offcore_rsp;
95959826caSMatt Macy 	uint32_t ped_event;
96959826caSMatt Macy 	uint32_t ped_frontend;
97959826caSMatt Macy 	uint32_t ped_ldlat;
98959826caSMatt Macy 	uint32_t ped_config1;
9907d80fd8SMatt Macy 	int16_t	ped_umask;
100959826caSMatt Macy 	uint8_t	ped_cmask;
101959826caSMatt Macy 	uint8_t	ped_any;
102959826caSMatt Macy 	uint8_t	ped_inv;
103959826caSMatt Macy 	uint8_t	ped_edge;
104959826caSMatt Macy 	uint8_t	ped_fc_mask;
105959826caSMatt Macy 	uint8_t	ped_ch_mask;
106959826caSMatt Macy };
107959826caSMatt Macy 
108959826caSMatt Macy static const struct pmu_events_map *
109*bfb46e2bSMatt Macy pmu_events_map_get(const char *cpuid)
110959826caSMatt Macy {
111959826caSMatt Macy 	size_t s;
112959826caSMatt Macy 	char buf[64];
113959826caSMatt Macy 	const struct pmu_events_map *pme;
114959826caSMatt Macy 
115*bfb46e2bSMatt Macy 	if (cpuid != NULL) {
116*bfb46e2bSMatt Macy 		memcpy(buf, cpuid, 64);
117*bfb46e2bSMatt Macy 	} else {
118959826caSMatt Macy 		if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
119959826caSMatt Macy 		    (void *)NULL, 0) == -1)
120959826caSMatt Macy 			return (NULL);
121959826caSMatt Macy 		if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
122959826caSMatt Macy 		    (void *)NULL, 0) == -1)
123959826caSMatt Macy 			return (NULL);
124*bfb46e2bSMatt Macy 	}
125959826caSMatt Macy 	for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
126959826caSMatt Macy 		if (strcmp(buf, pme->cpuid) == 0)
127959826caSMatt Macy 			return (pme);
128959826caSMatt Macy 	return (NULL);
129959826caSMatt Macy }
130959826caSMatt Macy 
131959826caSMatt Macy static const struct pmu_event *
132*bfb46e2bSMatt Macy pmu_event_get(const char *cpuid, const char *event_name, int *idx)
133959826caSMatt Macy {
134959826caSMatt Macy 	const struct pmu_events_map *pme;
135959826caSMatt Macy 	const struct pmu_event *pe;
136959826caSMatt Macy 	int i;
137959826caSMatt Macy 
138*bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(cpuid)) == NULL)
139959826caSMatt Macy 		return (NULL);
140959826caSMatt Macy 	for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
141959826caSMatt Macy 		if (pe->name == NULL)
142959826caSMatt Macy 			continue;
143959826caSMatt Macy 		if (strcasecmp(pe->name, event_name) == 0) {
144959826caSMatt Macy 			if (idx)
145959826caSMatt Macy 				*idx = i;
146959826caSMatt Macy 			return (pe);
147959826caSMatt Macy 		}
148959826caSMatt Macy 	}
149959826caSMatt Macy 	return (NULL);
150959826caSMatt Macy }
151959826caSMatt Macy 
152*bfb46e2bSMatt Macy int
153*bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
154*bfb46e2bSMatt Macy {
155*bfb46e2bSMatt Macy 	int idx;
156*bfb46e2bSMatt Macy 	const char *realname;
157*bfb46e2bSMatt Macy 
158*bfb46e2bSMatt Macy 	realname = pmu_alias_get(event);
159*bfb46e2bSMatt Macy 	if (pmu_event_get(cpuid, realname, &idx) == NULL)
160*bfb46e2bSMatt Macy 		return (-1);
161*bfb46e2bSMatt Macy 	return (idx);
162*bfb46e2bSMatt Macy }
163*bfb46e2bSMatt Macy 
164959826caSMatt Macy const char *
16578beed2fSMatt Macy pmc_pmu_event_get_by_idx(int idx)
166959826caSMatt Macy {
167959826caSMatt Macy 	const struct pmu_events_map *pme;
168959826caSMatt Macy 	const struct pmu_event *pe;
169959826caSMatt Macy 	int i;
170959826caSMatt Macy 
171*bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
172959826caSMatt Macy 		return (NULL);
1738b20f975SMatt Macy 	for (i = 0, pe = pme->table; (pe->name || pe->desc || pe->event) && i < idx; pe++, i++);
174959826caSMatt Macy 	return (pe->name);
175959826caSMatt Macy }
176959826caSMatt Macy 
177959826caSMatt Macy static int
178959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
179959826caSMatt Macy {
180959826caSMatt Macy 	char *event;
1818bed125dSMatt Macy 	char *kvp, *key, *value, *r;
182959826caSMatt Macy 	char *debug;
183959826caSMatt Macy 
184959826caSMatt Macy 	if ((event = strdup(eventin)) == NULL)
185959826caSMatt Macy 		return (ENOMEM);
1868bed125dSMatt Macy 	r = event;
187959826caSMatt Macy 	bzero(ped, sizeof(*ped));
18807d80fd8SMatt Macy 	ped->ped_umask = -1;
189959826caSMatt Macy 	while ((kvp = strsep(&event, ",")) != NULL) {
190959826caSMatt Macy 		key = strsep(&kvp, "=");
191959826caSMatt Macy 		if (key == NULL)
192959826caSMatt Macy 			abort();
193959826caSMatt Macy 		value = kvp;
194959826caSMatt Macy 		if (strcmp(key, "umask") == 0)
195959826caSMatt Macy 			ped->ped_umask = strtol(value, NULL, 16);
196959826caSMatt Macy 		else if (strcmp(key, "event") == 0)
197959826caSMatt Macy 			ped->ped_event = strtol(value, NULL, 16);
198959826caSMatt Macy 		else if (strcmp(key, "period") == 0)
199959826caSMatt Macy 			ped->ped_period = strtol(value, NULL, 10);
200959826caSMatt Macy 		else if (strcmp(key, "offcore_rsp") == 0)
201959826caSMatt Macy 			ped->ped_offcore_rsp = strtol(value, NULL, 16);
202959826caSMatt Macy 		else if (strcmp(key, "any") == 0)
203959826caSMatt Macy 			ped->ped_any = strtol(value, NULL, 10);
204959826caSMatt Macy 		else if (strcmp(key, "cmask") == 0)
205959826caSMatt Macy 			ped->ped_cmask = strtol(value, NULL, 10);
206959826caSMatt Macy 		else if (strcmp(key, "inv") == 0)
207959826caSMatt Macy 			ped->ped_inv = strtol(value, NULL, 10);
208959826caSMatt Macy 		else if (strcmp(key, "edge") == 0)
209959826caSMatt Macy 			ped->ped_edge = strtol(value, NULL, 10);
210959826caSMatt Macy 		else if (strcmp(key, "frontend") == 0)
211959826caSMatt Macy 			ped->ped_frontend = strtol(value, NULL, 16);
212959826caSMatt Macy 		else if (strcmp(key, "ldlat") == 0)
213959826caSMatt Macy 			ped->ped_ldlat = strtol(value, NULL, 16);
214959826caSMatt Macy 		else if (strcmp(key, "fc_mask") == 0)
215959826caSMatt Macy 			ped->ped_fc_mask = strtol(value, NULL, 16);
216959826caSMatt Macy 		else if (strcmp(key, "ch_mask") == 0)
217959826caSMatt Macy 			ped->ped_ch_mask = strtol(value, NULL, 16);
218959826caSMatt Macy 		else if (strcmp(key, "config1") == 0)
219959826caSMatt Macy 			ped->ped_config1 = strtol(value, NULL, 16);
220959826caSMatt Macy 		else {
221959826caSMatt Macy 			debug = getenv("PMUDEBUG");
222959826caSMatt Macy 			if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
223959826caSMatt Macy 				printf("unrecognized kvpair: %s:%s\n", key, value);
224959826caSMatt Macy 		}
225959826caSMatt Macy 	}
2268bed125dSMatt Macy 	free(r);
227959826caSMatt Macy 	return (0);
228959826caSMatt Macy }
229959826caSMatt Macy 
230959826caSMatt Macy uint64_t
231959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name)
232959826caSMatt Macy {
233959826caSMatt Macy 	const struct pmu_event *pe;
234959826caSMatt Macy 	struct pmu_event_desc ped;
235959826caSMatt Macy 
236959826caSMatt Macy 	event_name = pmu_alias_get(event_name);
237*bfb46e2bSMatt Macy 	if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
238959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
239*bfb46e2bSMatt Macy 	if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, NULL)) == NULL)
240959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
241959826caSMatt Macy 	if (pe->event == NULL)
242959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
243959826caSMatt Macy 	if (pmu_parse_event(&ped, pe->event))
244959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
245959826caSMatt Macy 	return (ped.ped_period);
246959826caSMatt Macy }
247959826caSMatt Macy 
248959826caSMatt Macy int
249959826caSMatt Macy pmc_pmu_enabled(void)
250959826caSMatt Macy {
251959826caSMatt Macy 
252*bfb46e2bSMatt Macy 	return (pmu_events_map_get(NULL) != NULL);
253959826caSMatt Macy }
254959826caSMatt Macy 
255959826caSMatt Macy void
256fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name)
257959826caSMatt Macy {
258959826caSMatt Macy 	const struct pmu_events_map *pme;
259959826caSMatt Macy 	const struct pmu_event *pe;
260959826caSMatt Macy 	struct pmu_event_desc ped;
261959826caSMatt Macy 	char *debug;
262959826caSMatt Macy 	int do_debug;
263959826caSMatt Macy 
264959826caSMatt Macy 	debug = getenv("PMUDEBUG");
265959826caSMatt Macy 	do_debug = 0;
266959826caSMatt Macy 
267959826caSMatt Macy 	if (debug != NULL && strcmp(debug, "true") == 0)
268959826caSMatt Macy 		do_debug = 1;
269*bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
270959826caSMatt Macy 		return;
271959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
272959826caSMatt Macy 		if (pe->name == NULL)
273959826caSMatt Macy 			continue;
274fbf962e6SMatt Macy 		if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
275fbf962e6SMatt Macy 			continue;
276959826caSMatt Macy 		printf("\t%s\n", pe->name);
277959826caSMatt Macy 		if (do_debug)
278959826caSMatt Macy 			pmu_parse_event(&ped, pe->event);
279959826caSMatt Macy 	}
280959826caSMatt Macy }
281959826caSMatt Macy 
282959826caSMatt Macy void
283959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev)
284959826caSMatt Macy {
285959826caSMatt Macy 	const struct pmu_events_map *pme;
286959826caSMatt Macy 	const struct pmu_event *pe;
287959826caSMatt Macy 
288*bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
289959826caSMatt Macy 		return;
290959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
291959826caSMatt Macy 		if (pe->name == NULL)
292959826caSMatt Macy 			continue;
293959826caSMatt Macy 		if (strcasestr(pe->name, ev) != NULL &&
294959826caSMatt Macy 		    pe->desc != NULL)
295959826caSMatt Macy 			printf("%s:\t%s\n", pe->name, pe->desc);
296959826caSMatt Macy 	}
297959826caSMatt Macy }
298959826caSMatt Macy 
299959826caSMatt Macy void
300959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev)
301959826caSMatt Macy {
302959826caSMatt Macy 	const struct pmu_events_map *pme;
303959826caSMatt Macy 	const struct pmu_event *pe;
304959826caSMatt Macy 
305*bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
306959826caSMatt Macy 		return;
307959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
308959826caSMatt Macy 		if (pe->name == NULL)
309959826caSMatt Macy 			continue;
310959826caSMatt Macy 		if (strcasestr(pe->name, ev) != NULL) {
311959826caSMatt Macy 			if (pe->long_desc != NULL)
312959826caSMatt Macy 				printf("%s:\n%s\n", pe->name, pe->long_desc);
313959826caSMatt Macy 			else if (pe->desc != NULL)
314959826caSMatt Macy 				printf("%s:\t%s\n", pe->name, pe->desc);
315959826caSMatt Macy 		}
316959826caSMatt Macy 	}
317959826caSMatt Macy }
318959826caSMatt Macy 
319fbf962e6SMatt Macy void
320fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev)
321fbf962e6SMatt Macy {
322fbf962e6SMatt Macy 	const struct pmu_events_map *pme;
323fbf962e6SMatt Macy 	const struct pmu_event *pe;
324fbf962e6SMatt Macy 
325*bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
326fbf962e6SMatt Macy 		return;
327fbf962e6SMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
328fbf962e6SMatt Macy 		if (pe->name == NULL)
329fbf962e6SMatt Macy 			continue;
330fbf962e6SMatt Macy 		if (strcasestr(pe->name, ev) == NULL)
331fbf962e6SMatt Macy 			continue;
332fbf962e6SMatt Macy 		printf("name: %s\n", pe->name);
333fbf962e6SMatt Macy 		if (pe->long_desc != NULL)
334fbf962e6SMatt Macy 			printf("desc: %s\n", pe->long_desc);
335fbf962e6SMatt Macy 		else if (pe->desc != NULL)
336fbf962e6SMatt Macy 			printf("desc: %s\n", pe->desc);
337fbf962e6SMatt Macy 		if (pe->event != NULL)
338fbf962e6SMatt Macy 			printf("event: %s\n", pe->event);
339fbf962e6SMatt Macy 		if (pe->topic != NULL)
340fbf962e6SMatt Macy 			printf("topic: %s\n", pe->topic);
341fbf962e6SMatt Macy 		if (pe->pmu != NULL)
342fbf962e6SMatt Macy 			printf("pmu: %s\n", pe->pmu);
343fbf962e6SMatt Macy 		if (pe->unit != NULL)
344fbf962e6SMatt Macy 			printf("unit: %s\n", pe->unit);
345fbf962e6SMatt Macy 		if (pe->perpkg != NULL)
346fbf962e6SMatt Macy 			printf("perpkg: %s\n", pe->perpkg);
347fbf962e6SMatt Macy 		if (pe->metric_expr != NULL)
348fbf962e6SMatt Macy 			printf("metric_expr: %s\n", pe->metric_expr);
349fbf962e6SMatt Macy 		if (pe->metric_name != NULL)
350fbf962e6SMatt Macy 			printf("metric_name: %s\n", pe->metric_name);
351fbf962e6SMatt Macy 		if (pe->metric_group != NULL)
352fbf962e6SMatt Macy 			printf("metric_group: %s\n", pe->metric_group);
353fbf962e6SMatt Macy 	}
354fbf962e6SMatt Macy }
355fbf962e6SMatt Macy 
356959826caSMatt Macy int
357959826caSMatt Macy pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
358959826caSMatt Macy {
359959826caSMatt Macy 	const struct pmu_event *pe;
360959826caSMatt Macy 	struct pmu_event_desc ped;
361959826caSMatt Macy 	struct pmc_md_iap_op_pmcallocate *iap;
362a191ed2dSMatt Macy 	int idx, isfixed;
363959826caSMatt Macy 
364959826caSMatt Macy 	iap = &pm->pm_md.pm_iap;
365a191ed2dSMatt Macy 	isfixed = 0;
366959826caSMatt Macy 	bzero(iap, sizeof(*iap));
367959826caSMatt Macy 	event_name = pmu_alias_get(event_name);
368a191ed2dSMatt Macy 	pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
369*bfb46e2bSMatt Macy 	if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
370959826caSMatt Macy 		return (ENOENT);
371*bfb46e2bSMatt Macy 	if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, &idx)) == NULL)
372959826caSMatt Macy 		return (ENOENT);
373959826caSMatt Macy 	if (pe->event == NULL)
374959826caSMatt Macy 		return (ENOENT);
375959826caSMatt Macy 	if (pmu_parse_event(&ped, pe->event))
376959826caSMatt Macy 		return (ENOENT);
377959826caSMatt Macy 
37807d80fd8SMatt Macy 
37907d80fd8SMatt Macy 	if (strcasestr(event_name, "UNC_") == event_name ||
380785dd70dSMatt Macy 	    strcasestr(event_name, "uncore") != NULL) {
381785dd70dSMatt Macy 		pm->pm_class = PMC_CLASS_UCP;
382a191ed2dSMatt Macy 		pm->pm_caps |= PMC_CAP_QUALIFIER;
38307d80fd8SMatt Macy 	} else if ((ped.ped_umask == -1) ||
38407d80fd8SMatt Macy 	    (ped.ped_event == 0x0 && ped.ped_umask == 0x3)) {
38507d80fd8SMatt Macy 		pm->pm_class = PMC_CLASS_IAF;
38607d80fd8SMatt Macy 	} else {
387959826caSMatt Macy 		pm->pm_class = PMC_CLASS_IAP;
38807d80fd8SMatt Macy 		pm->pm_caps |= PMC_CAP_QUALIFIER;
389785dd70dSMatt Macy 	}
390959826caSMatt Macy 	pm->pm_ev = idx;
391959826caSMatt Macy 	iap->pm_iap_config |= IAP_EVSEL(ped.ped_event);
39207d80fd8SMatt Macy 	if (ped.ped_umask > 0)
393959826caSMatt Macy 		iap->pm_iap_config |= IAP_UMASK(ped.ped_umask);
394959826caSMatt Macy 	iap->pm_iap_config |= IAP_CMASK(ped.ped_cmask);
395959826caSMatt Macy 	iap->pm_iap_rsp = ped.ped_offcore_rsp;
396959826caSMatt Macy 
397959826caSMatt Macy 	iap->pm_iap_config |= (IAP_USR | IAP_OS);
398959826caSMatt Macy 	if (ped.ped_edge)
399959826caSMatt Macy 		iap->pm_iap_config |= IAP_EDGE;
400959826caSMatt Macy 	if (ped.ped_any)
401959826caSMatt Macy 		iap->pm_iap_config |= IAP_ANY;
402959826caSMatt Macy 	if (ped.ped_inv)
403959826caSMatt Macy 		iap->pm_iap_config |= IAP_EDGE;
404959826caSMatt Macy 	if (pm->pm_caps & PMC_CAP_INTERRUPT)
405959826caSMatt Macy 		iap->pm_iap_config |= IAP_INT;
406959826caSMatt Macy 	return (0);
407959826caSMatt Macy }
408959826caSMatt Macy 
40978beed2fSMatt Macy /*
41078beed2fSMatt Macy  * Ultimately rely on AMD calling theirs the same
41178beed2fSMatt Macy  */
41278beed2fSMatt Macy static const char *stat_mode_cntrs[] = {
413a191ed2dSMatt Macy 	"cpu_clk_unhalted.thread_any",
41478beed2fSMatt Macy 	"inst_retired.any",
41578beed2fSMatt Macy 	"br_inst_retired.all_branches",
41678beed2fSMatt Macy 	"br_misp_retired.all_branches",
417a191ed2dSMatt Macy 	"longest_lat_cache.reference",
418a191ed2dSMatt Macy 	"longest_lat_cache.miss",
41978beed2fSMatt Macy };
42078beed2fSMatt Macy 
42178beed2fSMatt Macy int
42278beed2fSMatt Macy pmc_pmu_stat_mode(const char ***cntrs)
42378beed2fSMatt Macy {
42478beed2fSMatt Macy 	if (pmc_pmu_enabled()) {
42578beed2fSMatt Macy 		*cntrs = stat_mode_cntrs;
42678beed2fSMatt Macy 		return (0);
42778beed2fSMatt Macy 	}
42878beed2fSMatt Macy 	return (EOPNOTSUPP);
42978beed2fSMatt Macy }
43078beed2fSMatt Macy 
431959826caSMatt Macy #else
4328b20f975SMatt Macy 
4338b20f975SMatt Macy uint64_t
4348b20f975SMatt Macy pmc_pmu_sample_rate_get(const char *event_name __unused)
4358b20f975SMatt Macy {
4368b20f975SMatt Macy 	return (DEFAULT_SAMPLE_COUNT);
4378b20f975SMatt Macy }
4388b20f975SMatt Macy 
4398b20f975SMatt Macy void
440fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name __unused)
4418b20f975SMatt Macy {
4428b20f975SMatt Macy }
4438b20f975SMatt Macy 
4448b20f975SMatt Macy void
4458b20f975SMatt Macy pmc_pmu_print_counter_desc(const char *e __unused)
4468b20f975SMatt Macy {
4478b20f975SMatt Macy }
4488b20f975SMatt Macy 
4498b20f975SMatt Macy void
4508b20f975SMatt Macy pmc_pmu_print_counter_desc_long(const char *e __unused)
4518b20f975SMatt Macy {
4528b20f975SMatt Macy }
4538b20f975SMatt Macy 
454fbf962e6SMatt Macy void
455fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *e __unused)
456fbf962e6SMatt Macy {
457fbf962e6SMatt Macy 
458fbf962e6SMatt Macy }
459fbf962e6SMatt Macy 
4608b20f975SMatt Macy int
4618b20f975SMatt Macy pmc_pmu_enabled(void)
4628b20f975SMatt Macy {
4638b20f975SMatt Macy 	return (0);
4648b20f975SMatt Macy }
4658b20f975SMatt Macy 
4668b20f975SMatt Macy int
4678b20f975SMatt Macy pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
4688b20f975SMatt Macy {
4698b20f975SMatt Macy 	return (EOPNOTSUPP);
4708b20f975SMatt Macy }
4718b20f975SMatt Macy 
4728b20f975SMatt Macy const char *
4738b20f975SMatt Macy pmc_pmu_event_get_by_idx(int idx __unused)
4748b20f975SMatt Macy {
4758b20f975SMatt Macy 	return (NULL);
4768b20f975SMatt Macy }
477*bfb46e2bSMatt Macy 
4788b20f975SMatt Macy int
4798b20f975SMatt Macy pmc_pmu_stat_mode(const char ***a __unused)
4808b20f975SMatt Macy {
4818b20f975SMatt Macy 	return (EOPNOTSUPP);
4828b20f975SMatt Macy }
483959826caSMatt Macy 
484*bfb46e2bSMatt Macy int
485*bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *e __unused)
486*bfb46e2bSMatt Macy {
487*bfb46e2bSMatt Macy 	return (-1);
488*bfb46e2bSMatt Macy }
489*bfb46e2bSMatt Macy 
490959826caSMatt Macy #endif
491