xref: /freebsd/lib/libpmc/libpmc_pmu_util.c (revision 78b9f0095b4af3aca6c931b2c7b009ddb8a05125)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018, Matthew Macy
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD$
28  *
29  */
30 
31 #include <sys/types.h>
32 #include <sys/errno.h>
33 #include <sys/sysctl.h>
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <limits.h>
37 #include <string.h>
38 #include <pmc.h>
39 #include <pmclog.h>
40 #include <assert.h>
41 #include <libpmcstat.h>
42 #include "pmu-events/pmu-events.h"
43 
44 #if defined(__amd64__) || defined(__i386__)
45 struct pmu_alias {
46 	const char *pa_alias;
47 	const char *pa_name;
48 };
49 
50 typedef enum {
51 	PMU_INVALID,
52 	PMU_INTEL,
53 	PMU_AMD,
54 } pmu_mfr_t;
55 
56 static struct pmu_alias pmu_intel_alias_table[] = {
57 	{"UNHALTED_CORE_CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
58 	{"UNHALTED-CORE-CYCLES", "CPU_CLK_UNHALTED.THREAD_P_ANY"},
59 	{"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
60 	{"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
61 	{"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
62 	{"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
63 	{"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
64 	{"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
65 	{"RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
66 	{"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
67 	{"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
68 	{"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
69 	{"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
70 	{"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
71 	{"cycles", "tsc-tsc"},
72 	{"instructions", "inst-retired.any_p"},
73 	{"branch-mispredicts", "br_misp_retired.all_branches"},
74 	{"branches", "br_inst_retired.all_branches"},
75 	{"interrupts", "hw_interrupts.received"},
76 	{"ic-misses", "frontend_retired.l1i_miss"},
77 	{NULL, NULL},
78 };
79 
80 static struct pmu_alias pmu_amd_alias_table[] = {
81 	{"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"},
82 	{"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"},
83 	{NULL, NULL},
84 };
85 
86 
87 static pmu_mfr_t
88 pmu_events_mfr(void)
89 {
90 	char *buf;
91 	size_t s;
92 	pmu_mfr_t mfr;
93 
94 	if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
95 	    (void *)NULL, 0) == -1)
96 		return (PMU_INVALID);
97 	if ((buf = malloc(s + 1)) == NULL)
98 		return (PMU_INVALID);
99 	if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
100 		(void *)NULL, 0) == -1) {
101 		free(buf);
102 		return (PMU_INVALID);
103 	}
104 	if (strcasestr(buf, "AuthenticAMD") != NULL)
105 		mfr = PMU_AMD;
106 	else if (strcasestr(buf, "GenuineIntel") != NULL)
107 		mfr = PMU_INTEL;
108 	else
109 		mfr = PMU_INVALID;
110 	free(buf);
111 	return (mfr);
112 }
113 
114 /*
115  *  The Intel fixed mode counters are:
116  *	"inst_retired.any",
117  *	"cpu_clk_unhalted.thread",
118  *	"cpu_clk_unhalted.thread_any",
119  *	"cpu_clk_unhalted.ref_tsc",
120  *
121  */
122 
123 static const char *
124 pmu_alias_get(const char *name)
125 {
126 	pmu_mfr_t mfr;
127 	struct pmu_alias *pa;
128 	struct pmu_alias *pmu_alias_table;
129 
130 	if ((mfr = pmu_events_mfr()) == PMU_INVALID)
131 		return (name);
132 	if (mfr == PMU_AMD)
133 		pmu_alias_table = pmu_amd_alias_table;
134 	else if (mfr == PMU_INTEL)
135 		pmu_alias_table = pmu_intel_alias_table;
136 	else
137 		return (name);
138 
139 	for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
140 		if (strcasecmp(name, pa->pa_alias) == 0)
141 			return (pa->pa_name);
142 
143 	return (name);
144 }
145 
146 struct pmu_event_desc {
147 	uint64_t ped_period;
148 	uint64_t ped_offcore_rsp;
149 	uint32_t ped_event;
150 	uint32_t ped_frontend;
151 	uint32_t ped_ldlat;
152 	uint32_t ped_config1;
153 	int16_t	ped_umask;
154 	uint8_t	ped_cmask;
155 	uint8_t	ped_any;
156 	uint8_t	ped_inv;
157 	uint8_t	ped_edge;
158 	uint8_t	ped_fc_mask;
159 	uint8_t	ped_ch_mask;
160 };
161 
162 static const struct pmu_events_map *
163 pmu_events_map_get(const char *cpuid)
164 {
165 	size_t s;
166 	char buf[64];
167 	const struct pmu_events_map *pme;
168 
169 	if (cpuid != NULL) {
170 		memcpy(buf, cpuid, 64);
171 	} else {
172 		if (sysctlbyname("kern.hwpmc.cpuid", (void *)NULL, &s,
173 		    (void *)NULL, 0) == -1)
174 			return (NULL);
175 		if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
176 		    (void *)NULL, 0) == -1)
177 			return (NULL);
178 	}
179 	for (pme = pmu_events_map; pme->cpuid != NULL; pme++)
180 		if (strcmp(buf, pme->cpuid) == 0)
181 			return (pme);
182 	return (NULL);
183 }
184 
185 static const struct pmu_event *
186 pmu_event_get(const char *cpuid, const char *event_name, int *idx)
187 {
188 	const struct pmu_events_map *pme;
189 	const struct pmu_event *pe;
190 	int i;
191 
192 	if ((pme = pmu_events_map_get(cpuid)) == NULL)
193 		return (NULL);
194 	for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
195 		if (pe->name == NULL)
196 			continue;
197 		if (strcasecmp(pe->name, event_name) == 0) {
198 			if (idx)
199 				*idx = i;
200 			return (pe);
201 		}
202 	}
203 	return (NULL);
204 }
205 
206 int
207 pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
208 {
209 	int idx;
210 	const char *realname;
211 
212 	realname = pmu_alias_get(event);
213 	if (pmu_event_get(cpuid, realname, &idx) == NULL)
214 		return (-1);
215 	return (idx);
216 }
217 
218 const char *
219 pmc_pmu_event_get_by_idx(const char *cpuid, int idx)
220 {
221 	const struct pmu_events_map *pme;
222 
223 	if ((pme = pmu_events_map_get(cpuid)) == NULL)
224 		return (NULL);
225 	assert(pme->table[idx].name);
226 	return (pme->table[idx].name);
227 }
228 
229 static int
230 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
231 {
232 	char *event;
233 	char *kvp, *key, *value, *r;
234 	char *debug;
235 
236 	if ((event = strdup(eventin)) == NULL)
237 		return (ENOMEM);
238 	r = event;
239 	bzero(ped, sizeof(*ped));
240 	ped->ped_umask = -1;
241 	while ((kvp = strsep(&event, ",")) != NULL) {
242 		key = strsep(&kvp, "=");
243 		if (key == NULL)
244 			abort();
245 		value = kvp;
246 		if (strcmp(key, "umask") == 0)
247 			ped->ped_umask = strtol(value, NULL, 16);
248 		else if (strcmp(key, "event") == 0)
249 			ped->ped_event = strtol(value, NULL, 16);
250 		else if (strcmp(key, "period") == 0)
251 			ped->ped_period = strtol(value, NULL, 10);
252 		else if (strcmp(key, "offcore_rsp") == 0)
253 			ped->ped_offcore_rsp = strtol(value, NULL, 16);
254 		else if (strcmp(key, "any") == 0)
255 			ped->ped_any = strtol(value, NULL, 10);
256 		else if (strcmp(key, "cmask") == 0)
257 			ped->ped_cmask = strtol(value, NULL, 10);
258 		else if (strcmp(key, "inv") == 0)
259 			ped->ped_inv = strtol(value, NULL, 10);
260 		else if (strcmp(key, "edge") == 0)
261 			ped->ped_edge = strtol(value, NULL, 10);
262 		else if (strcmp(key, "frontend") == 0)
263 			ped->ped_frontend = strtol(value, NULL, 16);
264 		else if (strcmp(key, "ldlat") == 0)
265 			ped->ped_ldlat = strtol(value, NULL, 16);
266 		else if (strcmp(key, "fc_mask") == 0)
267 			ped->ped_fc_mask = strtol(value, NULL, 16);
268 		else if (strcmp(key, "ch_mask") == 0)
269 			ped->ped_ch_mask = strtol(value, NULL, 16);
270 		else if (strcmp(key, "config1") == 0)
271 			ped->ped_config1 = strtol(value, NULL, 16);
272 		else {
273 			debug = getenv("PMUDEBUG");
274 			if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
275 				printf("unrecognized kvpair: %s:%s\n", key, value);
276 		}
277 	}
278 	free(r);
279 	return (0);
280 }
281 
282 uint64_t
283 pmc_pmu_sample_rate_get(const char *event_name)
284 {
285 	const struct pmu_event *pe;
286 	struct pmu_event_desc ped;
287 
288 	event_name = pmu_alias_get(event_name);
289 	if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
290 		return (DEFAULT_SAMPLE_COUNT);
291 	if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, NULL)) == NULL)
292 		return (DEFAULT_SAMPLE_COUNT);
293 	if (pe->event == NULL)
294 		return (DEFAULT_SAMPLE_COUNT);
295 	if (pmu_parse_event(&ped, pe->event))
296 		return (DEFAULT_SAMPLE_COUNT);
297 	return (ped.ped_period);
298 }
299 
300 int
301 pmc_pmu_enabled(void)
302 {
303 
304 	return (pmu_events_map_get(NULL) != NULL);
305 }
306 
307 void
308 pmc_pmu_print_counters(const char *event_name)
309 {
310 	const struct pmu_events_map *pme;
311 	const struct pmu_event *pe;
312 	struct pmu_event_desc ped;
313 	char *debug;
314 	int do_debug;
315 
316 	debug = getenv("PMUDEBUG");
317 	do_debug = 0;
318 
319 	if (debug != NULL && strcmp(debug, "true") == 0)
320 		do_debug = 1;
321 	if ((pme = pmu_events_map_get(NULL)) == NULL)
322 		return;
323 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
324 		if (pe->name == NULL)
325 			continue;
326 		if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
327 			continue;
328 		printf("\t%s\n", pe->name);
329 		if (do_debug)
330 			pmu_parse_event(&ped, pe->event);
331 	}
332 }
333 
334 void
335 pmc_pmu_print_counter_desc(const char *ev)
336 {
337 	const struct pmu_events_map *pme;
338 	const struct pmu_event *pe;
339 
340 	if ((pme = pmu_events_map_get(NULL)) == NULL)
341 		return;
342 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
343 		if (pe->name == NULL)
344 			continue;
345 		if (strcasestr(pe->name, ev) != NULL &&
346 		    pe->desc != NULL)
347 			printf("%s:\t%s\n", pe->name, pe->desc);
348 	}
349 }
350 
351 void
352 pmc_pmu_print_counter_desc_long(const char *ev)
353 {
354 	const struct pmu_events_map *pme;
355 	const struct pmu_event *pe;
356 
357 	if ((pme = pmu_events_map_get(NULL)) == NULL)
358 		return;
359 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
360 		if (pe->name == NULL)
361 			continue;
362 		if (strcasestr(pe->name, ev) != NULL) {
363 			if (pe->long_desc != NULL)
364 				printf("%s:\n%s\n", pe->name, pe->long_desc);
365 			else if (pe->desc != NULL)
366 				printf("%s:\t%s\n", pe->name, pe->desc);
367 		}
368 	}
369 }
370 
371 void
372 pmc_pmu_print_counter_full(const char *ev)
373 {
374 	const struct pmu_events_map *pme;
375 	const struct pmu_event *pe;
376 
377 	if ((pme = pmu_events_map_get(NULL)) == NULL)
378 		return;
379 	for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
380 		if (pe->name == NULL)
381 			continue;
382 		if (strcasestr(pe->name, ev) == NULL)
383 			continue;
384 		printf("name: %s\n", pe->name);
385 		if (pe->long_desc != NULL)
386 			printf("desc: %s\n", pe->long_desc);
387 		else if (pe->desc != NULL)
388 			printf("desc: %s\n", pe->desc);
389 		if (pe->event != NULL)
390 			printf("event: %s\n", pe->event);
391 		if (pe->topic != NULL)
392 			printf("topic: %s\n", pe->topic);
393 		if (pe->pmu != NULL)
394 			printf("pmu: %s\n", pe->pmu);
395 		if (pe->unit != NULL)
396 			printf("unit: %s\n", pe->unit);
397 		if (pe->perpkg != NULL)
398 			printf("perpkg: %s\n", pe->perpkg);
399 		if (pe->metric_expr != NULL)
400 			printf("metric_expr: %s\n", pe->metric_expr);
401 		if (pe->metric_name != NULL)
402 			printf("metric_name: %s\n", pe->metric_name);
403 		if (pe->metric_group != NULL)
404 			printf("metric_group: %s\n", pe->metric_group);
405 	}
406 }
407 
408 static int
409 pmc_pmu_amd_pmcallocate(const char *event_name __unused, struct pmc_op_pmcallocate *pm,
410 	struct pmu_event_desc *ped)
411 {
412 	struct pmc_md_amd_op_pmcallocate *amd;
413 
414 	amd = &pm->pm_md.pm_amd;
415 	amd->pm_amd_config = AMD_PMC_TO_EVENTMASK(ped->ped_event);
416 	if (ped->ped_umask > 0) {
417 		pm->pm_caps |= PMC_CAP_QUALIFIER;
418 		amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask);
419 	}
420 	pm->pm_class = PMC_CLASS_K8;
421 
422 	if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
423 		(pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
424 		(PMC_CAP_USER|PMC_CAP_SYSTEM))
425 		amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS);
426 	else if (pm->pm_caps & PMC_CAP_USER)
427 		amd->pm_amd_config |= AMD_PMC_USR;
428 	else if (pm->pm_caps & PMC_CAP_SYSTEM)
429 		amd->pm_amd_config |= AMD_PMC_OS;
430 	if (ped->ped_edge)
431 		amd->pm_amd_config |= AMD_PMC_EDGE;
432 	if (ped->ped_inv)
433 		amd->pm_amd_config |= AMD_PMC_EDGE;
434 	if (pm->pm_caps & PMC_CAP_INTERRUPT)
435 		amd->pm_amd_config |= AMD_PMC_INT;
436 	return (0);
437 }
438 
439 static int
440 pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
441 	struct pmu_event_desc *ped)
442 {
443 	struct pmc_md_iap_op_pmcallocate *iap;
444 	int isfixed;
445 
446 	isfixed = 0;
447 	iap = &pm->pm_md.pm_iap;
448 	if (strcasestr(event_name, "UNC_") == event_name ||
449 	    strcasestr(event_name, "uncore") != NULL) {
450 		pm->pm_class = PMC_CLASS_UCP;
451 		pm->pm_caps |= PMC_CAP_QUALIFIER;
452 	} else if ((ped->ped_umask == -1) ||
453 	    (ped->ped_event == 0x0 && ped->ped_umask == 0x3)) {
454 		pm->pm_class = PMC_CLASS_IAF;
455 	} else {
456 		pm->pm_class = PMC_CLASS_IAP;
457 		pm->pm_caps |= PMC_CAP_QUALIFIER;
458 	}
459 	iap->pm_iap_config |= IAP_EVSEL(ped->ped_event);
460 	if (ped->ped_umask > 0)
461 		iap->pm_iap_config |= IAP_UMASK(ped->ped_umask);
462 	iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask);
463 	iap->pm_iap_rsp = ped->ped_offcore_rsp;
464 
465 	if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
466 		(pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
467 		(PMC_CAP_USER|PMC_CAP_SYSTEM))
468 		iap->pm_iap_config |= (IAP_USR | IAP_OS);
469 	else if (pm->pm_caps & PMC_CAP_USER)
470 		iap->pm_iap_config |= IAP_USR;
471 	else if (pm->pm_caps & PMC_CAP_SYSTEM)
472 		iap->pm_iap_config |= IAP_OS;
473 	if (ped->ped_edge)
474 		iap->pm_iap_config |= IAP_EDGE;
475 	if (ped->ped_any)
476 		iap->pm_iap_config |= IAP_ANY;
477 	if (ped->ped_inv)
478 		iap->pm_iap_config |= IAP_EDGE;
479 	if (pm->pm_caps & PMC_CAP_INTERRUPT)
480 		iap->pm_iap_config |= IAP_INT;
481 	return (0);
482 }
483 
484 int
485 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
486 {
487 	const struct pmu_event *pe;
488 	struct pmu_event_desc ped;
489 	pmu_mfr_t mfr;
490 	int idx = -1;
491 
492 	if ((mfr = pmu_events_mfr()) == PMU_INVALID)
493 		return (ENOENT);
494 
495 	bzero(&pm->pm_md, sizeof(pm->pm_md));
496 	pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
497 	event_name = pmu_alias_get(event_name);
498 	if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
499 		return (ENOENT);
500 	if (pe->alias && (pe = pmu_event_get(NULL, pe->alias, &idx)) == NULL)
501 		return (ENOENT);
502 	assert(idx >= 0);
503 	pm->pm_ev = idx;
504 
505 	if (pe->event == NULL)
506 		return (ENOENT);
507 	if (pmu_parse_event(&ped, pe->event))
508 		return (ENOENT);
509 
510 	if (mfr == PMU_INTEL)
511 		return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped));
512 	else
513 		return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped));
514 }
515 
516 /*
517  * Ultimately rely on AMD calling theirs the same
518  */
519 static const char *stat_mode_cntrs[] = {
520 	"cpu_clk_unhalted.thread",
521 	"inst_retired.any",
522 	"br_inst_retired.all_branches",
523 	"br_misp_retired.all_branches",
524 	"longest_lat_cache.reference",
525 	"longest_lat_cache.miss",
526 };
527 
528 int
529 pmc_pmu_stat_mode(const char ***cntrs)
530 {
531 	if (pmc_pmu_enabled()) {
532 		*cntrs = stat_mode_cntrs;
533 		return (0);
534 	}
535 	return (EOPNOTSUPP);
536 }
537 
538 #else
539 
540 uint64_t
541 pmc_pmu_sample_rate_get(const char *event_name __unused)
542 {
543 	return (DEFAULT_SAMPLE_COUNT);
544 }
545 
546 void
547 pmc_pmu_print_counters(const char *event_name __unused)
548 {
549 }
550 
551 void
552 pmc_pmu_print_counter_desc(const char *e __unused)
553 {
554 }
555 
556 void
557 pmc_pmu_print_counter_desc_long(const char *e __unused)
558 {
559 }
560 
561 void
562 pmc_pmu_print_counter_full(const char *e __unused)
563 {
564 
565 }
566 
567 int
568 pmc_pmu_enabled(void)
569 {
570 	return (0);
571 }
572 
573 int
574 pmc_pmu_pmcallocate(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
575 {
576 	return (EOPNOTSUPP);
577 }
578 
579 const char *
580 pmc_pmu_event_get_by_idx(const char *c __unused, int idx __unused)
581 {
582 	return (NULL);
583 }
584 
585 int
586 pmc_pmu_stat_mode(const char ***a __unused)
587 {
588 	return (EOPNOTSUPP);
589 }
590 
591 int
592 pmc_pmu_idx_get_by_event(const char *c __unused, const char *e __unused)
593 {
594 	return (-1);
595 }
596 
597 #endif
598