xref: /freebsd/lib/libpmc/libpmc_pmu_util.c (revision a0ac5706af0ef24fb490f1f3c78b81ff977f9d63)
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>
401b000b50SMatt Macy #include <assert.h>
41959826caSMatt Macy #include <libpmcstat.h>
42959826caSMatt Macy #include "pmu-events/pmu-events.h"
43959826caSMatt Macy 
447d1c2b74SMatt Macy #if defined(__amd64__) || defined(__i386__)
45959826caSMatt Macy struct pmu_alias {
46959826caSMatt Macy 	const char *pa_alias;
47959826caSMatt Macy 	const char *pa_name;
48959826caSMatt Macy };
4981eb4dcfSMatt Macy 
5081eb4dcfSMatt Macy typedef enum {
5181eb4dcfSMatt Macy 	PMU_INVALID,
5281eb4dcfSMatt Macy 	PMU_INTEL,
5381eb4dcfSMatt Macy 	PMU_AMD,
5481eb4dcfSMatt Macy } pmu_mfr_t;
5581eb4dcfSMatt Macy 
5681eb4dcfSMatt Macy static struct pmu_alias pmu_intel_alias_table[] = {
57959826caSMatt Macy 	{"UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
58959826caSMatt Macy 	{"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
59959826caSMatt Macy 	{"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
60959826caSMatt Macy 	{"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
61959826caSMatt Macy 	{"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
62959826caSMatt Macy 	{"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
63959826caSMatt Macy 	{"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
64959826caSMatt Macy 	{"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
65959826caSMatt Macy 	{"RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
66959826caSMatt Macy 	{"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
67959826caSMatt Macy 	{"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
68959826caSMatt Macy 	{"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
69959826caSMatt Macy 	{"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
70959826caSMatt Macy 	{"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
7196cbd26aSMatt Macy 	{"cycles", "tsc-tsc"},
7286b5e013SMatt Macy 	{"unhalted-cycles", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
7396cbd26aSMatt Macy 	{"instructions", "inst-retired.any_p"},
7496cbd26aSMatt Macy 	{"branch-mispredicts", "br_misp_retired.all_branches"},
7596cbd26aSMatt Macy 	{"branches", "br_inst_retired.all_branches"},
7696cbd26aSMatt Macy 	{"interrupts", "hw_interrupts.received"},
7796cbd26aSMatt Macy 	{"ic-misses", "frontend_retired.l1i_miss"},
78959826caSMatt Macy 	{NULL, NULL},
79959826caSMatt Macy };
80959826caSMatt Macy 
8181eb4dcfSMatt Macy static struct pmu_alias pmu_amd_alias_table[] = {
8281eb4dcfSMatt Macy 	{"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"},
8381eb4dcfSMatt Macy 	{"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"},
8481eb4dcfSMatt Macy 	{NULL, NULL},
8581eb4dcfSMatt Macy };
8681eb4dcfSMatt Macy 
8781eb4dcfSMatt Macy 
8881eb4dcfSMatt Macy static pmu_mfr_t
8981eb4dcfSMatt Macy pmu_events_mfr(void)
9081eb4dcfSMatt Macy {
9181eb4dcfSMatt Macy 	char *buf;
9281eb4dcfSMatt Macy 	size_t s;
9381eb4dcfSMatt Macy 	pmu_mfr_t mfr;
9481eb4dcfSMatt Macy 
9581eb4dcfSMatt Macy 	if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
9681eb4dcfSMatt Macy 	    (void *)NULL, 0) == -1)
9781eb4dcfSMatt Macy 		return (PMU_INVALID);
9881eb4dcfSMatt Macy 	if ((buf = malloc(s + 1)) == NULL)
9981eb4dcfSMatt Macy 		return (PMU_INVALID);
10081eb4dcfSMatt Macy 	if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
10181eb4dcfSMatt Macy 		(void *)NULL, 0) == -1) {
10281eb4dcfSMatt Macy 		free(buf);
10381eb4dcfSMatt Macy 		return (PMU_INVALID);
10481eb4dcfSMatt Macy 	}
10581eb4dcfSMatt Macy 	if (strcasestr(buf, "AuthenticAMD") != NULL)
10681eb4dcfSMatt Macy 		mfr = PMU_AMD;
10781eb4dcfSMatt Macy 	else if (strcasestr(buf, "GenuineIntel") != NULL)
10881eb4dcfSMatt Macy 		mfr = PMU_INTEL;
10981eb4dcfSMatt Macy 	else
11081eb4dcfSMatt Macy 		mfr = PMU_INVALID;
11181eb4dcfSMatt Macy 	free(buf);
11281eb4dcfSMatt Macy 	return (mfr);
11381eb4dcfSMatt Macy }
11481eb4dcfSMatt Macy 
11507d80fd8SMatt Macy /*
11607d80fd8SMatt Macy  *  The Intel fixed mode counters are:
11707d80fd8SMatt Macy  *	"inst_retired.any",
11807d80fd8SMatt Macy  *	"cpu_clk_unhalted.thread",
11907d80fd8SMatt Macy  *	"cpu_clk_unhalted.thread_any",
12007d80fd8SMatt Macy  *	"cpu_clk_unhalted.ref_tsc",
12107d80fd8SMatt Macy  *
12207d80fd8SMatt Macy  */
123a191ed2dSMatt Macy 
124959826caSMatt Macy static const char *
125959826caSMatt Macy pmu_alias_get(const char *name)
126959826caSMatt Macy {
12781eb4dcfSMatt Macy 	pmu_mfr_t mfr;
128959826caSMatt Macy 	struct pmu_alias *pa;
12981eb4dcfSMatt Macy 	struct pmu_alias *pmu_alias_table;
13081eb4dcfSMatt Macy 
13181eb4dcfSMatt Macy 	if ((mfr = pmu_events_mfr()) == PMU_INVALID)
13281eb4dcfSMatt Macy 		return (name);
13381eb4dcfSMatt Macy 	if (mfr == PMU_AMD)
13481eb4dcfSMatt Macy 		pmu_alias_table = pmu_amd_alias_table;
13581eb4dcfSMatt Macy 	else if (mfr == PMU_INTEL)
13681eb4dcfSMatt Macy 		pmu_alias_table = pmu_intel_alias_table;
13781eb4dcfSMatt Macy 	else
13881eb4dcfSMatt Macy 		return (name);
139959826caSMatt Macy 
140959826caSMatt Macy 	for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
141959826caSMatt Macy 		if (strcasecmp(name, pa->pa_alias) == 0)
142959826caSMatt Macy 			return (pa->pa_name);
14381eb4dcfSMatt Macy 
144959826caSMatt Macy 	return (name);
145959826caSMatt Macy }
146959826caSMatt Macy 
147959826caSMatt Macy struct pmu_event_desc {
148959826caSMatt Macy 	uint64_t ped_period;
149959826caSMatt Macy 	uint64_t ped_offcore_rsp;
150dacc43dfSMatt Macy 	uint64_t ped_l3_thread;
151dacc43dfSMatt Macy 	uint64_t ped_l3_slice;
152959826caSMatt Macy 	uint32_t ped_event;
153959826caSMatt Macy 	uint32_t ped_frontend;
154959826caSMatt Macy 	uint32_t ped_ldlat;
155959826caSMatt Macy 	uint32_t ped_config1;
15607d80fd8SMatt Macy 	int16_t	ped_umask;
157959826caSMatt Macy 	uint8_t	ped_cmask;
158959826caSMatt Macy 	uint8_t	ped_any;
159959826caSMatt Macy 	uint8_t	ped_inv;
160959826caSMatt Macy 	uint8_t	ped_edge;
161959826caSMatt Macy 	uint8_t	ped_fc_mask;
162959826caSMatt Macy 	uint8_t	ped_ch_mask;
163959826caSMatt Macy };
164959826caSMatt Macy 
165959826caSMatt Macy static const struct pmu_events_map *
166bfb46e2bSMatt Macy pmu_events_map_get(const char *cpuid)
167959826caSMatt Macy {
168*a0ac5706SEmmanuel Vadot 	regex_t re;
169*a0ac5706SEmmanuel Vadot 	regmatch_t pmatch[1];
170*a0ac5706SEmmanuel Vadot 	size_t s, len;
171959826caSMatt Macy 	char buf[64];
172*a0ac5706SEmmanuel Vadot 	int match;
173959826caSMatt Macy 	const struct pmu_events_map *pme;
174959826caSMatt Macy 
175bfb46e2bSMatt Macy 	if (cpuid != NULL) {
176bfb46e2bSMatt Macy 		memcpy(buf, cpuid, 64);
177bfb46e2bSMatt Macy 	} else {
178959826caSMatt Macy 		if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
179959826caSMatt Macy 		    (void *)NULL, 0) == -1)
180959826caSMatt Macy 			return (NULL);
181959826caSMatt Macy 		if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
182959826caSMatt Macy 		    (void *)NULL, 0) == -1)
183959826caSMatt Macy 			return (NULL);
184bfb46e2bSMatt Macy 	}
185*a0ac5706SEmmanuel Vadot 	for (pme = pmu_events_map; pme->cpuid != NULL; pme++) {
186*a0ac5706SEmmanuel Vadot 		if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) {
187*a0ac5706SEmmanuel Vadot 			printf("regex '%s' failed to compile, ignoring\n",
188*a0ac5706SEmmanuel Vadot 			    pme->cpuid);
189*a0ac5706SEmmanuel Vadot 			continue;
190*a0ac5706SEmmanuel Vadot 		}
191*a0ac5706SEmmanuel Vadot 		match = regexec(&re, buf, 1, pmatch, 0);
192*a0ac5706SEmmanuel Vadot 		regfree(&re);
193*a0ac5706SEmmanuel Vadot 		if (match == 0) {
194*a0ac5706SEmmanuel Vadot 			len = pmatch[0].rm_eo - pmatch[0].rm_so;
195*a0ac5706SEmmanuel Vadot 			if(len == strlen(buf))
196959826caSMatt Macy 				return (pme);
197*a0ac5706SEmmanuel Vadot 		}
198*a0ac5706SEmmanuel Vadot 	}
199959826caSMatt Macy 	return (NULL);
200959826caSMatt Macy }
201959826caSMatt Macy 
202959826caSMatt Macy static const struct pmu_event *
203bfb46e2bSMatt Macy pmu_event_get(const char *cpuid, const char *event_name, int *idx)
204959826caSMatt Macy {
205959826caSMatt Macy 	const struct pmu_events_map *pme;
206959826caSMatt Macy 	const struct pmu_event *pe;
207959826caSMatt Macy 	int i;
208959826caSMatt Macy 
209bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(cpuid)) == NULL)
210959826caSMatt Macy 		return (NULL);
211959826caSMatt Macy 	for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
212959826caSMatt Macy 		if (pe->name == NULL)
213959826caSMatt Macy 			continue;
214959826caSMatt Macy 		if (strcasecmp(pe->name, event_name) == 0) {
215959826caSMatt Macy 			if (idx)
216959826caSMatt Macy 				*idx = i;
217959826caSMatt Macy 			return (pe);
218959826caSMatt Macy 		}
219959826caSMatt Macy 	}
220959826caSMatt Macy 	return (NULL);
221959826caSMatt Macy }
222959826caSMatt Macy 
223bfb46e2bSMatt Macy int
224bfb46e2bSMatt Macy pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
225bfb46e2bSMatt Macy {
226bfb46e2bSMatt Macy 	int idx;
227bfb46e2bSMatt Macy 	const char *realname;
228bfb46e2bSMatt Macy 
229bfb46e2bSMatt Macy 	realname = pmu_alias_get(event);
230bfb46e2bSMatt Macy 	if (pmu_event_get(cpuid, realname, &idx) == NULL)
231bfb46e2bSMatt Macy 		return (-1);
232bfb46e2bSMatt Macy 	return (idx);
233bfb46e2bSMatt Macy }
234bfb46e2bSMatt Macy 
235959826caSMatt Macy const char *
236b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *cpuid, int idx)
237959826caSMatt Macy {
238959826caSMatt Macy 	const struct pmu_events_map *pme;
239959826caSMatt Macy 
240b2ca2e50SMatt Macy 	if ((pme = pmu_events_map_get(cpuid)) == NULL)
241959826caSMatt Macy 		return (NULL);
2421b000b50SMatt Macy 	assert(pme->table[idx].name);
2431b000b50SMatt Macy 	return (pme->table[idx].name);
244959826caSMatt Macy }
245959826caSMatt Macy 
246959826caSMatt Macy static int
247959826caSMatt Macy pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
248959826caSMatt Macy {
249959826caSMatt Macy 	char *event;
2508bed125dSMatt Macy 	char *kvp, *key, *value, *r;
251959826caSMatt Macy 	char *debug;
252959826caSMatt Macy 
253959826caSMatt Macy 	if ((event = strdup(eventin)) == NULL)
254959826caSMatt Macy 		return (ENOMEM);
2558bed125dSMatt Macy 	r = event;
256959826caSMatt Macy 	bzero(ped, sizeof(*ped));
2570204d85aSMatt Macy 	ped->ped_period = DEFAULT_SAMPLE_COUNT;
25807d80fd8SMatt Macy 	ped->ped_umask = -1;
259959826caSMatt Macy 	while ((kvp = strsep(&event, ",")) != NULL) {
260959826caSMatt Macy 		key = strsep(&kvp, "=");
261959826caSMatt Macy 		if (key == NULL)
262959826caSMatt Macy 			abort();
263959826caSMatt Macy 		value = kvp;
264959826caSMatt Macy 		if (strcmp(key, "umask") == 0)
265959826caSMatt Macy 			ped->ped_umask = strtol(value, NULL, 16);
266959826caSMatt Macy 		else if (strcmp(key, "event") == 0)
267959826caSMatt Macy 			ped->ped_event = strtol(value, NULL, 16);
268959826caSMatt Macy 		else if (strcmp(key, "period") == 0)
269959826caSMatt Macy 			ped->ped_period = strtol(value, NULL, 10);
270959826caSMatt Macy 		else if (strcmp(key, "offcore_rsp") == 0)
271959826caSMatt Macy 			ped->ped_offcore_rsp = strtol(value, NULL, 16);
272959826caSMatt Macy 		else if (strcmp(key, "any") == 0)
273959826caSMatt Macy 			ped->ped_any = strtol(value, NULL, 10);
274959826caSMatt Macy 		else if (strcmp(key, "cmask") == 0)
275959826caSMatt Macy 			ped->ped_cmask = strtol(value, NULL, 10);
276959826caSMatt Macy 		else if (strcmp(key, "inv") == 0)
277959826caSMatt Macy 			ped->ped_inv = strtol(value, NULL, 10);
278959826caSMatt Macy 		else if (strcmp(key, "edge") == 0)
279959826caSMatt Macy 			ped->ped_edge = strtol(value, NULL, 10);
280959826caSMatt Macy 		else if (strcmp(key, "frontend") == 0)
281959826caSMatt Macy 			ped->ped_frontend = strtol(value, NULL, 16);
282959826caSMatt Macy 		else if (strcmp(key, "ldlat") == 0)
283959826caSMatt Macy 			ped->ped_ldlat = strtol(value, NULL, 16);
284959826caSMatt Macy 		else if (strcmp(key, "fc_mask") == 0)
285959826caSMatt Macy 			ped->ped_fc_mask = strtol(value, NULL, 16);
286959826caSMatt Macy 		else if (strcmp(key, "ch_mask") == 0)
287959826caSMatt Macy 			ped->ped_ch_mask = strtol(value, NULL, 16);
288959826caSMatt Macy 		else if (strcmp(key, "config1") == 0)
289959826caSMatt Macy 			ped->ped_config1 = strtol(value, NULL, 16);
290dacc43dfSMatt Macy 		else if (strcmp(key, "l3_thread_mask") == 0)
291dacc43dfSMatt Macy 			ped->ped_l3_thread = strtol(value, NULL, 16);
292dacc43dfSMatt Macy 		else if (strcmp(key, "l3_slice_mask") == 0)
293dacc43dfSMatt Macy 			ped->ped_l3_slice = strtol(value, NULL, 16);
294959826caSMatt Macy 		else {
295959826caSMatt Macy 			debug = getenv("PMUDEBUG");
296959826caSMatt Macy 			if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
297959826caSMatt Macy 				printf("unrecognized kvpair: %s:%s\n", key, value);
298959826caSMatt Macy 		}
299959826caSMatt Macy 	}
3008bed125dSMatt Macy 	free(r);
301959826caSMatt Macy 	return (0);
302959826caSMatt Macy }
303959826caSMatt Macy 
304959826caSMatt Macy uint64_t
305959826caSMatt Macy pmc_pmu_sample_rate_get(const char *event_name)
306959826caSMatt Macy {
307959826caSMatt Macy 	const struct pmu_event *pe;
308959826caSMatt Macy 	struct pmu_event_desc ped;
309959826caSMatt Macy 
310959826caSMatt Macy 	event_name = pmu_alias_get(event_name);
311bfb46e2bSMatt Macy 	if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
312959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
313bfb46e2bSMatt Macy 	if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, NULL)) == NULL)
314959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
315959826caSMatt Macy 	if (pe->event == NULL)
316959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
317959826caSMatt Macy 	if (pmu_parse_event(&ped, pe->event))
318959826caSMatt Macy 		return (DEFAULT_SAMPLE_COUNT);
319959826caSMatt Macy 	return (ped.ped_period);
320959826caSMatt Macy }
321959826caSMatt Macy 
322959826caSMatt Macy int
323959826caSMatt Macy pmc_pmu_enabled(void)
324959826caSMatt Macy {
325959826caSMatt Macy 
326bfb46e2bSMatt Macy 	return (pmu_events_map_get(NULL) != NULL);
327959826caSMatt Macy }
328959826caSMatt Macy 
329959826caSMatt Macy void
330fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name)
331959826caSMatt Macy {
332959826caSMatt Macy 	const struct pmu_events_map *pme;
333959826caSMatt Macy 	const struct pmu_event *pe;
334959826caSMatt Macy 	struct pmu_event_desc ped;
335959826caSMatt Macy 	char *debug;
336959826caSMatt Macy 	int do_debug;
337959826caSMatt Macy 
338959826caSMatt Macy 	debug = getenv("PMUDEBUG");
339959826caSMatt Macy 	do_debug = 0;
340959826caSMatt Macy 
341959826caSMatt Macy 	if (debug != NULL && strcmp(debug, "true") == 0)
342959826caSMatt Macy 		do_debug = 1;
343bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
344959826caSMatt Macy 		return;
345959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
346959826caSMatt Macy 		if (pe->name == NULL)
347959826caSMatt Macy 			continue;
348fbf962e6SMatt Macy 		if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
349fbf962e6SMatt Macy 			continue;
350959826caSMatt Macy 		printf("\t%s\n", pe->name);
351959826caSMatt Macy 		if (do_debug)
352959826caSMatt Macy 			pmu_parse_event(&ped, pe->event);
353959826caSMatt Macy 	}
354959826caSMatt Macy }
355959826caSMatt Macy 
356959826caSMatt Macy void
357959826caSMatt Macy pmc_pmu_print_counter_desc(const char *ev)
358959826caSMatt Macy {
359959826caSMatt Macy 	const struct pmu_events_map *pme;
360959826caSMatt Macy 	const struct pmu_event *pe;
361959826caSMatt Macy 
362bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
363959826caSMatt Macy 		return;
364959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
365959826caSMatt Macy 		if (pe->name == NULL)
366959826caSMatt Macy 			continue;
367959826caSMatt Macy 		if (strcasestr(pe->name, ev) != NULL &&
368959826caSMatt Macy 		    pe->desc != NULL)
369959826caSMatt Macy 			printf("%s:\t%s\n", pe->name, pe->desc);
370959826caSMatt Macy 	}
371959826caSMatt Macy }
372959826caSMatt Macy 
373959826caSMatt Macy void
374959826caSMatt Macy pmc_pmu_print_counter_desc_long(const char *ev)
375959826caSMatt Macy {
376959826caSMatt Macy 	const struct pmu_events_map *pme;
377959826caSMatt Macy 	const struct pmu_event *pe;
378959826caSMatt Macy 
379bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
380959826caSMatt Macy 		return;
381959826caSMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
382959826caSMatt Macy 		if (pe->name == NULL)
383959826caSMatt Macy 			continue;
384959826caSMatt Macy 		if (strcasestr(pe->name, ev) != NULL) {
385959826caSMatt Macy 			if (pe->long_desc != NULL)
386959826caSMatt Macy 				printf("%s:\n%s\n", pe->name, pe->long_desc);
387959826caSMatt Macy 			else if (pe->desc != NULL)
388959826caSMatt Macy 				printf("%s:\t%s\n", pe->name, pe->desc);
389959826caSMatt Macy 		}
390959826caSMatt Macy 	}
391959826caSMatt Macy }
392959826caSMatt Macy 
393fbf962e6SMatt Macy void
394fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *ev)
395fbf962e6SMatt Macy {
396fbf962e6SMatt Macy 	const struct pmu_events_map *pme;
397fbf962e6SMatt Macy 	const struct pmu_event *pe;
398fbf962e6SMatt Macy 
399bfb46e2bSMatt Macy 	if ((pme = pmu_events_map_get(NULL)) == NULL)
400fbf962e6SMatt Macy 		return;
401fbf962e6SMatt Macy 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
402fbf962e6SMatt Macy 		if (pe->name == NULL)
403fbf962e6SMatt Macy 			continue;
404fbf962e6SMatt Macy 		if (strcasestr(pe->name, ev) == NULL)
405fbf962e6SMatt Macy 			continue;
406fbf962e6SMatt Macy 		printf("name: %s\n", pe->name);
407fbf962e6SMatt Macy 		if (pe->long_desc != NULL)
408fbf962e6SMatt Macy 			printf("desc: %s\n", pe->long_desc);
409fbf962e6SMatt Macy 		else if (pe->desc != NULL)
410fbf962e6SMatt Macy 			printf("desc: %s\n", pe->desc);
411fbf962e6SMatt Macy 		if (pe->event != NULL)
412fbf962e6SMatt Macy 			printf("event: %s\n", pe->event);
413fbf962e6SMatt Macy 		if (pe->topic != NULL)
414fbf962e6SMatt Macy 			printf("topic: %s\n", pe->topic);
415fbf962e6SMatt Macy 		if (pe->pmu != NULL)
416fbf962e6SMatt Macy 			printf("pmu: %s\n", pe->pmu);
417fbf962e6SMatt Macy 		if (pe->unit != NULL)
418fbf962e6SMatt Macy 			printf("unit: %s\n", pe->unit);
419fbf962e6SMatt Macy 		if (pe->perpkg != NULL)
420fbf962e6SMatt Macy 			printf("perpkg: %s\n", pe->perpkg);
421fbf962e6SMatt Macy 		if (pe->metric_expr != NULL)
422fbf962e6SMatt Macy 			printf("metric_expr: %s\n", pe->metric_expr);
423fbf962e6SMatt Macy 		if (pe->metric_name != NULL)
424fbf962e6SMatt Macy 			printf("metric_name: %s\n", pe->metric_name);
425fbf962e6SMatt Macy 		if (pe->metric_group != NULL)
426fbf962e6SMatt Macy 			printf("metric_group: %s\n", pe->metric_group);
427fbf962e6SMatt Macy 	}
428fbf962e6SMatt Macy }
429fbf962e6SMatt Macy 
43081eb4dcfSMatt Macy static int
431dacc43dfSMatt Macy pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
43281eb4dcfSMatt Macy 	struct pmu_event_desc *ped)
433959826caSMatt Macy {
43481eb4dcfSMatt Macy 	struct pmc_md_amd_op_pmcallocate *amd;
435dacc43dfSMatt Macy 	const struct pmu_event *pe;
436dacc43dfSMatt Macy 	int idx = -1;
43781eb4dcfSMatt Macy 
43881eb4dcfSMatt Macy 	amd = &pm->pm_md.pm_amd;
43981eb4dcfSMatt Macy 	if (ped->ped_umask > 0) {
44081eb4dcfSMatt Macy 		pm->pm_caps |= PMC_CAP_QUALIFIER;
44181eb4dcfSMatt Macy 		amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask);
44281eb4dcfSMatt Macy 	}
44381eb4dcfSMatt Macy 	pm->pm_class = PMC_CLASS_K8;
444dacc43dfSMatt Macy 	pe = pmu_event_get(NULL, event_name, &idx);
44581eb4dcfSMatt Macy 
446dacc43dfSMatt Macy 	if (strcmp("l3cache", pe->topic) == 0){
447dacc43dfSMatt Macy 		amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
448dacc43dfSMatt Macy 		amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE;
449dacc43dfSMatt Macy 		amd->pm_amd_config |= AMD_PMC_TO_L3SLICE(ped->ped_l3_slice);
450dacc43dfSMatt Macy 		amd->pm_amd_config |= AMD_PMC_TO_L3CORE(ped->ped_l3_thread);
451dacc43dfSMatt Macy 	}
452dacc43dfSMatt Macy 	else if (strcmp("data fabric", pe->topic) == 0){
453dacc43dfSMatt Macy 
454dacc43dfSMatt Macy 		amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK_DF(ped->ped_event);
455dacc43dfSMatt Macy 		amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC;
456dacc43dfSMatt Macy 	}
457dacc43dfSMatt Macy 	else{
458dacc43dfSMatt Macy 		amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
459dacc43dfSMatt Macy 		amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE;
46081eb4dcfSMatt Macy 		if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
46181eb4dcfSMatt Macy 			(pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
46281eb4dcfSMatt Macy 			(PMC_CAP_USER|PMC_CAP_SYSTEM))
46381eb4dcfSMatt Macy 			amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS);
46481eb4dcfSMatt Macy 		else if (pm->pm_caps & PMC_CAP_USER)
46581eb4dcfSMatt Macy 			amd->pm_amd_config |= AMD_PMC_USR;
46681eb4dcfSMatt Macy 		else if (pm->pm_caps & PMC_CAP_SYSTEM)
46781eb4dcfSMatt Macy 			amd->pm_amd_config |= AMD_PMC_OS;
46881eb4dcfSMatt Macy 		if (ped->ped_edge)
46981eb4dcfSMatt Macy 			amd->pm_amd_config |= AMD_PMC_EDGE;
47081eb4dcfSMatt Macy 		if (ped->ped_inv)
47181eb4dcfSMatt Macy 			amd->pm_amd_config |= AMD_PMC_EDGE;
47281eb4dcfSMatt Macy 		if (pm->pm_caps & PMC_CAP_INTERRUPT)
47381eb4dcfSMatt Macy 			amd->pm_amd_config |= AMD_PMC_INT;
474dacc43dfSMatt Macy 	}
47581eb4dcfSMatt Macy 	return (0);
47681eb4dcfSMatt Macy }
47781eb4dcfSMatt Macy 
47881eb4dcfSMatt Macy static int
47981eb4dcfSMatt Macy pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
48081eb4dcfSMatt Macy 	struct pmu_event_desc *ped)
48181eb4dcfSMatt Macy {
482959826caSMatt Macy 	struct pmc_md_iap_op_pmcallocate *iap;
48381eb4dcfSMatt Macy 	int isfixed;
484959826caSMatt Macy 
485a191ed2dSMatt Macy 	isfixed = 0;
48681eb4dcfSMatt Macy 	iap = &pm->pm_md.pm_iap;
48707d80fd8SMatt Macy 	if (strcasestr(event_name, "UNC_") == event_name ||
488785dd70dSMatt Macy 	    strcasestr(event_name, "uncore") != NULL) {
489785dd70dSMatt Macy 		pm->pm_class = PMC_CLASS_UCP;
490a191ed2dSMatt Macy 		pm->pm_caps |= PMC_CAP_QUALIFIER;
49181eb4dcfSMatt Macy 	} else if ((ped->ped_umask == -1) ||
49281eb4dcfSMatt Macy 	    (ped->ped_event == 0x0 && ped->ped_umask == 0x3)) {
49307d80fd8SMatt Macy 		pm->pm_class = PMC_CLASS_IAF;
49407d80fd8SMatt Macy 	} else {
495959826caSMatt Macy 		pm->pm_class = PMC_CLASS_IAP;
49607d80fd8SMatt Macy 		pm->pm_caps |= PMC_CAP_QUALIFIER;
497785dd70dSMatt Macy 	}
49881eb4dcfSMatt Macy 	iap->pm_iap_config |= IAP_EVSEL(ped->ped_event);
49981eb4dcfSMatt Macy 	if (ped->ped_umask > 0)
50081eb4dcfSMatt Macy 		iap->pm_iap_config |= IAP_UMASK(ped->ped_umask);
50181eb4dcfSMatt Macy 	iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask);
50281eb4dcfSMatt Macy 	iap->pm_iap_rsp = ped->ped_offcore_rsp;
503959826caSMatt Macy 
50481eb4dcfSMatt Macy 	if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
50581eb4dcfSMatt Macy 		(pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
50681eb4dcfSMatt Macy 		(PMC_CAP_USER|PMC_CAP_SYSTEM))
507959826caSMatt Macy 		iap->pm_iap_config |= (IAP_USR | IAP_OS);
50881eb4dcfSMatt Macy 	else if (pm->pm_caps & PMC_CAP_USER)
50981eb4dcfSMatt Macy 		iap->pm_iap_config |= IAP_USR;
51081eb4dcfSMatt Macy 	else if (pm->pm_caps & PMC_CAP_SYSTEM)
51181eb4dcfSMatt Macy 		iap->pm_iap_config |= IAP_OS;
51281eb4dcfSMatt Macy 	if (ped->ped_edge)
513959826caSMatt Macy 		iap->pm_iap_config |= IAP_EDGE;
51481eb4dcfSMatt Macy 	if (ped->ped_any)
515959826caSMatt Macy 		iap->pm_iap_config |= IAP_ANY;
51681eb4dcfSMatt Macy 	if (ped->ped_inv)
517959826caSMatt Macy 		iap->pm_iap_config |= IAP_EDGE;
518959826caSMatt Macy 	if (pm->pm_caps & PMC_CAP_INTERRUPT)
519959826caSMatt Macy 		iap->pm_iap_config |= IAP_INT;
520959826caSMatt Macy 	return (0);
521959826caSMatt Macy }
522959826caSMatt Macy 
52381eb4dcfSMatt Macy int
52481eb4dcfSMatt Macy pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
52581eb4dcfSMatt Macy {
52681eb4dcfSMatt Macy 	const struct pmu_event *pe;
52781eb4dcfSMatt Macy 	struct pmu_event_desc ped;
52881eb4dcfSMatt Macy 	pmu_mfr_t mfr;
52981eb4dcfSMatt Macy 	int idx = -1;
53081eb4dcfSMatt Macy 
53181eb4dcfSMatt Macy 	if ((mfr = pmu_events_mfr()) == PMU_INVALID)
53281eb4dcfSMatt Macy 		return (ENOENT);
53381eb4dcfSMatt Macy 
53481eb4dcfSMatt Macy 	bzero(&pm->pm_md, sizeof(pm->pm_md));
53581eb4dcfSMatt Macy 	pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
53681eb4dcfSMatt Macy 	event_name = pmu_alias_get(event_name);
53781eb4dcfSMatt Macy 	if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
53881eb4dcfSMatt Macy 		return (ENOENT);
53981eb4dcfSMatt Macy 	if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, &idx)) == NULL)
54081eb4dcfSMatt Macy 		return (ENOENT);
54181eb4dcfSMatt Macy 	assert(idx >= 0);
54281eb4dcfSMatt Macy 	pm->pm_ev = idx;
54381eb4dcfSMatt Macy 
54481eb4dcfSMatt Macy 	if (pe->event == NULL)
54581eb4dcfSMatt Macy 		return (ENOENT);
54681eb4dcfSMatt Macy 	if (pmu_parse_event(&ped, pe->event))
54781eb4dcfSMatt Macy 		return (ENOENT);
54881eb4dcfSMatt Macy 
54981eb4dcfSMatt Macy 	if (mfr == PMU_INTEL)
55081eb4dcfSMatt Macy 		return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped));
55181eb4dcfSMatt Macy 	else
55281eb4dcfSMatt Macy 		return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped));
55381eb4dcfSMatt Macy }
55481eb4dcfSMatt Macy 
55578beed2fSMatt Macy /*
55678beed2fSMatt Macy  * Ultimately rely on AMD calling theirs the same
55778beed2fSMatt Macy  */
55878beed2fSMatt Macy static const char *stat_mode_cntrs[] = {
559ee62e0b4SMatt Macy 	"cpu_clk_unhalted.thread",
56078beed2fSMatt Macy 	"inst_retired.any",
56178beed2fSMatt Macy 	"br_inst_retired.all_branches",
56278beed2fSMatt Macy 	"br_misp_retired.all_branches",
563a191ed2dSMatt Macy 	"longest_lat_cache.reference",
564a191ed2dSMatt Macy 	"longest_lat_cache.miss",
56578beed2fSMatt Macy };
56678beed2fSMatt Macy 
56778beed2fSMatt Macy int
56878beed2fSMatt Macy pmc_pmu_stat_mode(const char ***cntrs)
56978beed2fSMatt Macy {
57078beed2fSMatt Macy 	if (pmc_pmu_enabled()) {
57178beed2fSMatt Macy 		*cntrs = stat_mode_cntrs;
57278beed2fSMatt Macy 		return (0);
57378beed2fSMatt Macy 	}
57478beed2fSMatt Macy 	return (EOPNOTSUPP);
57578beed2fSMatt Macy }
57678beed2fSMatt Macy 
577959826caSMatt Macy #else
5788b20f975SMatt Macy 
5798b20f975SMatt Macy uint64_t
5808b20f975SMatt Macy pmc_pmu_sample_rate_get(const char *event_name __unused)
5818b20f975SMatt Macy {
5828b20f975SMatt Macy 	return (DEFAULT_SAMPLE_COUNT);
5838b20f975SMatt Macy }
5848b20f975SMatt Macy 
5858b20f975SMatt Macy void
586fbf962e6SMatt Macy pmc_pmu_print_counters(const char *event_name __unused)
5878b20f975SMatt Macy {
5888b20f975SMatt Macy }
5898b20f975SMatt Macy 
5908b20f975SMatt Macy void
5918b20f975SMatt Macy pmc_pmu_print_counter_desc(const char *e __unused)
5928b20f975SMatt Macy {
5938b20f975SMatt Macy }
5948b20f975SMatt Macy 
5958b20f975SMatt Macy void
5968b20f975SMatt Macy pmc_pmu_print_counter_desc_long(const char *e __unused)
5978b20f975SMatt Macy {
5988b20f975SMatt Macy }
5998b20f975SMatt Macy 
600fbf962e6SMatt Macy void
601fbf962e6SMatt Macy pmc_pmu_print_counter_full(const char *e __unused)
602fbf962e6SMatt Macy {
603fbf962e6SMatt Macy 
604fbf962e6SMatt Macy }
605fbf962e6SMatt Macy 
6068b20f975SMatt Macy int
6078b20f975SMatt Macy pmc_pmu_enabled(void)
6088b20f975SMatt Macy {
6098b20f975SMatt Macy 	return (0);
6108b20f975SMatt Macy }
6118b20f975SMatt Macy 
6128b20f975SMatt Macy int
6138b20f975SMatt Macy pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
6148b20f975SMatt Macy {
6158b20f975SMatt Macy 	return (EOPNOTSUPP);
6168b20f975SMatt Macy }
6178b20f975SMatt Macy 
6188b20f975SMatt Macy const char *
619b2ca2e50SMatt Macy pmc_pmu_event_get_by_idx(const char *c __unused, int idx __unused)
6208b20f975SMatt Macy {
6218b20f975SMatt Macy 	return (NULL);
6228b20f975SMatt Macy }
623bfb46e2bSMatt Macy 
6248b20f975SMatt Macy int
6258b20f975SMatt Macy pmc_pmu_stat_mode(const char ***a __unused)
6268b20f975SMatt Macy {
6278b20f975SMatt Macy 	return (EOPNOTSUPP);
6288b20f975SMatt Macy }
629959826caSMatt Macy 
630bfb46e2bSMatt Macy int
631ce6fc9d7SMatt Macy pmc_pmu_idx_get_by_event(const char *c __unused, const char *e __unused)
632bfb46e2bSMatt Macy {
633bfb46e2bSMatt Macy 	return (-1);
634bfb46e2bSMatt Macy }
635bfb46e2bSMatt Macy 
636959826caSMatt Macy #endif
637