1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2018, Matthew Macy
5 * Copyright (c) 2021, The FreeBSD Foundation
6 *
7 * Portions of this software were developed by Mitchell Horne
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33 #include <sys/types.h>
34 #include <sys/errno.h>
35 #include <sys/pmc.h>
36 #include <sys/sysctl.h>
37 #include <stddef.h>
38 #include <stdlib.h>
39 #include <limits.h>
40 #include <regex.h>
41 #include <string.h>
42 #include <pmc.h>
43 #include <pmclog.h>
44 #include <assert.h>
45 #include <libpmcstat.h>
46 #include "pmu-events/pmu-events.h"
47
48 struct pmu_alias {
49 const char *pa_alias;
50 const char *pa_name;
51 };
52
53 #if defined(__amd64__) || defined(__i386__)
54 typedef enum {
55 PMU_INVALID,
56 PMU_INTEL,
57 PMU_AMD,
58 } pmu_mfr_t;
59
60 static struct pmu_alias pmu_intel_alias_table[] = {
61 {"UNHALTED_CORE_CYCLES", "cpu_clk_unhalted.thread"},
62 {"UNHALTED-CORE-CYCLES", "cpu_clk_unhalted.thread"},
63 {"LLC_MISSES", "LONGEST_LAT_CACHE.MISS"},
64 {"LLC-MISSES", "LONGEST_LAT_CACHE.MISS"},
65 {"LLC_REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
66 {"LLC-REFERENCE", "LONGEST_LAT_CACHE.REFERENCE"},
67 {"LLC_MISS_RHITM", "mem_load_l3_miss_retired.remote_hitm"},
68 {"LLC-MISS-RHITM", "mem_load_l3_miss_retired.remote_hitm"},
69 {"RESOURCE_STALL", "RESOURCE_STALLS.ANY"},
70 {"RESOURCE_STALLS_ANY", "RESOURCE_STALLS.ANY"},
71 {"BRANCH_INSTRUCTION_RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
72 {"BRANCH-INSTRUCTION-RETIRED", "BR_INST_RETIRED.ALL_BRANCHES"},
73 {"BRANCH_MISSES_RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
74 {"BRANCH-MISSES-RETIRED", "BR_MISP_RETIRED.ALL_BRANCHES"},
75 {"unhalted-cycles", "cpu_clk_unhalted.thread"},
76 {"instructions", "inst_retired.any"},
77 {"branch-mispredicts", "br_misp_retired.all_branches"},
78 {"branches", "br_inst_retired.all_branches"},
79 {"interrupts", "hw_interrupts.received"},
80 {"ic-misses", "frontend_retired.l1i_miss"},
81 {NULL, NULL},
82 };
83
84 static struct pmu_alias pmu_amd_alias_table[] = {
85 {"UNHALTED_CORE_CYCLES", "ls_not_halted_cyc"},
86 {"UNHALTED-CORE-CYCLES", "ls_not_halted_cyc"},
87 {"LLC_MISSES", "l3_comb_clstr_state.request_miss"},
88 {"LLC-MISSES", "l3_comb_clstr_state.request_miss"},
89 {"LLC_REFERENCE", "l3_request_g1.caching_l3_cache_accesses"},
90 {"LLC-REFERENCE", "l3_request_g1.caching_l3_cache_accesses"},
91 {"BRANCH_INSTRUCTION_RETIRED", "ex_ret_brn"},
92 {"BRANCH-INSTRUCTION-RETIRED", "ex_ret_brn"},
93 {"BRANCH_MISSES_RETIRED", "ex_ret_brn_misp"},
94 {"BRANCH-MISSES-RETIRED", "ex_ret_brn_misp"},
95 {"unhalted-cycles", "ls_not_halted_cyc"},
96 {"instructions", "ex_ret_instr",},
97 {"branch-mispredicts", "ex_ret_brn_misp"},
98 {"branches", "ex_ret_brn"},
99 {"interrupts", "ls_int_taken"}, /* Not on amdzen1 */
100 {NULL, NULL},
101 };
102
103
104 static pmu_mfr_t
pmu_events_mfr(void)105 pmu_events_mfr(void)
106 {
107 char buf[PMC_CPUID_LEN];
108 size_t s = sizeof(buf);
109 pmu_mfr_t mfr;
110
111 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
112 (void *)NULL, 0) == -1)
113 return (PMU_INVALID);
114 if (strcasestr(buf, "AuthenticAMD") != NULL ||
115 strcasestr(buf, "HygonGenuine") != NULL)
116 mfr = PMU_AMD;
117 else if (strcasestr(buf, "GenuineIntel") != NULL)
118 mfr = PMU_INTEL;
119 else
120 mfr = PMU_INVALID;
121 return (mfr);
122 }
123
124 static int
pmu_events_x86_family(void)125 pmu_events_x86_family(void)
126 {
127 char buf[PMC_CPUID_LEN];
128 size_t s = sizeof(buf);
129 char *cpuid, *family;
130
131 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
132 (void *)NULL, 0) == -1)
133 return (-1);
134 cpuid = &buf[0];
135
136 strsep(&cpuid, "-");
137 family = strsep(&cpuid, "-");
138
139 return (strtol(family, NULL, 10));
140 }
141
142 /*
143 * The Intel fixed mode counters are:
144 * "inst_retired.any",
145 * "cpu_clk_unhalted.thread",
146 * "cpu_clk_unhalted.thread_any",
147 * "cpu_clk_unhalted.ref_tsc",
148 *
149 */
150
151 static const char *
pmu_alias_get(const char * name)152 pmu_alias_get(const char *name)
153 {
154 pmu_mfr_t mfr;
155 struct pmu_alias *pa;
156 struct pmu_alias *pmu_alias_table;
157
158 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
159 return (name);
160 if (mfr == PMU_AMD)
161 pmu_alias_table = pmu_amd_alias_table;
162 else if (mfr == PMU_INTEL)
163 pmu_alias_table = pmu_intel_alias_table;
164 else
165 return (name);
166
167 for (pa = pmu_alias_table; pa->pa_alias != NULL; pa++)
168 if (strcasecmp(name, pa->pa_alias) == 0)
169 return (pa->pa_name);
170
171 return (name);
172 }
173 #elif defined(__powerpc64__)
174
175 static const char *
pmu_alias_get(const char * name)176 pmu_alias_get(const char *name)
177 {
178 return (name);
179 }
180
181 #elif defined(__aarch64__)
182
183 static struct pmu_alias pmu_armv8_alias_table[] = {
184 {"UNHALTED_CORE_CYCLES", "CPU_CYCLES"},
185 {"UNHALTED-CORE-CYCLES", "CPU_CYCLES"},
186 {"LLC_MISSES", "LL_CACHE_MISS_RD"},
187 {"LLC-MISSES", "LL_CACHE_MISS_RD"},
188 {"LLC_REFERENCE", "LL_CACHE_RD"},
189 {"LLC-REFERENCE", "LL_CACHE_RD"},
190 {"BRANCH_INSTRUCTION_RETIRED", "BR_RETIRED"},
191 {"BRANCH-INSTRUCTION-RETIRED", "BR_RETIRED"},
192 {"BRANCH_MISSES_RETIRED", "BR_MIS_PRED_RETIRED"},
193 {"BRANCH-MISSES-RETIRED", "BR_MIS_PRED_RETIRED"},
194 {"unhalted-cycles", "CPU_CYCLES"},
195 {"instructions", "INST_RETIRED",},
196 {"branch-mispredicts", "BR_MIS_PRED_RETIRED"},
197 {"branches", "BR_RETIRED"},
198 {"interrupts", "EXC_IRQ"},
199 {NULL, NULL},
200 };
201
202 static const char *
pmu_alias_get(const char * name)203 pmu_alias_get(const char *name)
204 {
205 struct pmu_alias *pa;
206
207 for (pa = pmu_armv8_alias_table; pa->pa_alias != NULL; pa++)
208 if (strcasecmp(name, pa->pa_alias) == 0)
209 return (pa->pa_name);
210
211 return (name);
212 }
213
214 #else
215
216 static const char *
pmu_alias_get(const char * name)217 pmu_alias_get(const char *name)
218 {
219
220 return (name);
221 }
222 #endif
223
224 struct pmu_event_desc {
225 uint64_t ped_period;
226 uint64_t ped_offcore_rsp;
227 uint64_t ped_l3_thread;
228 uint64_t ped_l3_slice;
229 uint32_t ped_sourceid;
230 uint32_t ped_coreid;
231 uint32_t ped_allsources;
232 uint32_t ped_allcores;
233 uint32_t ped_event;
234 uint32_t ped_frontend;
235 uint32_t ped_ldlat;
236 uint32_t ped_config1;
237 int16_t ped_umask;
238 uint8_t ped_cmask;
239 uint8_t ped_any;
240 uint8_t ped_inv;
241 uint8_t ped_edge;
242 uint8_t ped_fc_mask;
243 uint8_t ped_ch_mask;
244 uint8_t ped_pebs;
245 };
246
247 static const struct pmu_events_map *
pmu_events_map_get(const char * cpuid)248 pmu_events_map_get(const char *cpuid)
249 {
250 regex_t re;
251 regmatch_t pmatch[1];
252 char buf[PMC_CPUID_LEN];
253 size_t s = sizeof(buf);
254 int match;
255 const struct pmu_events_map *pme;
256
257 if (cpuid != NULL) {
258 strlcpy(buf, cpuid, s);
259 } else {
260 if (sysctlbyname("kern.hwpmc.cpuid", buf, &s,
261 (void *)NULL, 0) == -1)
262 return (NULL);
263 }
264 for (pme = pmu_events_map; pme->cpuid != NULL; pme++) {
265 if (regcomp(&re, pme->cpuid, REG_EXTENDED) != 0) {
266 printf("regex '%s' failed to compile, ignoring\n",
267 pme->cpuid);
268 continue;
269 }
270 match = regexec(&re, buf, 1, pmatch, 0);
271 regfree(&re);
272 if (match == 0) {
273 if (pmatch[0].rm_so == 0 && (buf[pmatch[0].rm_eo] == 0
274 || buf[pmatch[0].rm_eo] == '-'))
275 return (pme);
276 }
277 }
278 return (NULL);
279 }
280
281 static const struct pmu_event *
pmu_event_get(const char * cpuid,const char * event_name,int * idx)282 pmu_event_get(const char *cpuid, const char *event_name, int *idx)
283 {
284 const struct pmu_events_map *pme;
285 const struct pmu_event *pe;
286 int i;
287
288 if ((pme = pmu_events_map_get(cpuid)) == NULL)
289 return (NULL);
290 for (i = 0, pe = pme->table; pe->name || pe->desc || pe->event; pe++, i++) {
291 if (pe->name == NULL)
292 continue;
293 if (strcasecmp(pe->name, event_name) == 0) {
294 if (idx)
295 *idx = i;
296 return (pe);
297 }
298 }
299 return (NULL);
300 }
301
302 int
pmc_pmu_idx_get_by_event(const char * cpuid,const char * event)303 pmc_pmu_idx_get_by_event(const char *cpuid, const char *event)
304 {
305 int idx;
306 const char *realname;
307
308 realname = pmu_alias_get(event);
309 if (pmu_event_get(cpuid, realname, &idx) == NULL)
310 return (-1);
311 return (idx);
312 }
313
314 const char *
pmc_pmu_event_get_by_idx(const char * cpuid,int idx)315 pmc_pmu_event_get_by_idx(const char *cpuid, int idx)
316 {
317 const struct pmu_events_map *pme;
318
319 if ((pme = pmu_events_map_get(cpuid)) == NULL)
320 return (NULL);
321 assert(pme->table[idx].name);
322 return (pme->table[idx].name);
323 }
324
325 static int
pmu_parse_event(struct pmu_event_desc * ped,const char * eventin)326 pmu_parse_event(struct pmu_event_desc *ped, const char *eventin)
327 {
328 char *event;
329 char *kvp, *key, *value, *r;
330 char *debug;
331
332 if ((event = strdup(eventin)) == NULL)
333 return (ENOMEM);
334 r = event;
335 bzero(ped, sizeof(*ped));
336 ped->ped_period = DEFAULT_SAMPLE_COUNT;
337 ped->ped_umask = -1;
338 while ((kvp = strsep(&event, ",")) != NULL) {
339 key = strsep(&kvp, "=");
340 if (key == NULL)
341 abort();
342 value = kvp;
343 if (strcmp(key, "umask") == 0)
344 ped->ped_umask = strtol(value, NULL, 16);
345 else if (strcmp(key, "event") == 0)
346 ped->ped_event = strtol(value, NULL, 16);
347 else if (strcmp(key, "period") == 0)
348 ped->ped_period = strtol(value, NULL, 10);
349 else if (strcmp(key, "offcore_rsp") == 0)
350 ped->ped_offcore_rsp = strtol(value, NULL, 16);
351 else if (strcmp(key, "any") == 0)
352 ped->ped_any = strtol(value, NULL, 10);
353 else if (strcmp(key, "cmask") == 0)
354 ped->ped_cmask = strtol(value, NULL, 10);
355 else if (strcmp(key, "inv") == 0)
356 ped->ped_inv = strtol(value, NULL, 10);
357 else if (strcmp(key, "edge") == 0)
358 ped->ped_edge = strtol(value, NULL, 10);
359 else if (strcmp(key, "frontend") == 0)
360 ped->ped_frontend = strtol(value, NULL, 16);
361 else if (strcmp(key, "ldlat") == 0)
362 ped->ped_ldlat = strtol(value, NULL, 16);
363 else if (strcmp(key, "fc_mask") == 0)
364 ped->ped_fc_mask = strtol(value, NULL, 16);
365 else if (strcmp(key, "ch_mask") == 0)
366 ped->ped_ch_mask = strtol(value, NULL, 16);
367 else if (strcmp(key, "config1") == 0)
368 ped->ped_config1 = strtol(value, NULL, 16);
369 else if (strcmp(key, "l3_thread_mask") == 0)
370 ped->ped_l3_thread = strtol(value, NULL, 16);
371 else if (strcmp(key, "l3_slice_mask") == 0)
372 ped->ped_l3_slice = strtol(value, NULL, 16);
373 else if (strcmp(key, "sourceid") == 0)
374 ped->ped_sourceid = strtol(value, NULL, 16);
375 else if (strcmp(key, "coreid") == 0)
376 ped->ped_coreid = strtol(value, NULL, 16);
377 else if (strcmp(key, "allcores") == 0)
378 ped->ped_allcores = strtol(value, NULL, 10);
379 else if (strcmp(key, "allsources") == 0)
380 ped->ped_allsources = strtol(value, NULL, 10);
381 else if (strcmp(key, "pebs") == 0)
382 ped->ped_pebs = strtol(value, NULL, 10);
383 else {
384 debug = getenv("PMUDEBUG");
385 if (debug != NULL && strcmp(debug, "true") == 0 && value != NULL)
386 printf("unrecognized kvpair: %s:%s\n", key, value);
387 }
388 }
389 free(r);
390 return (0);
391 }
392
393 uint64_t
pmc_pmu_sample_rate_get(const char * event_name)394 pmc_pmu_sample_rate_get(const char *event_name)
395 {
396 const struct pmu_event *pe;
397 struct pmu_event_desc ped;
398
399 event_name = pmu_alias_get(event_name);
400 if ((pe = pmu_event_get(NULL, event_name, NULL)) == NULL)
401 return (DEFAULT_SAMPLE_COUNT);
402 if (pe->event == NULL)
403 return (DEFAULT_SAMPLE_COUNT);
404 if (pmu_parse_event(&ped, pe->event))
405 return (DEFAULT_SAMPLE_COUNT);
406 return (ped.ped_period);
407 }
408
409 int
pmc_pmu_enabled(void)410 pmc_pmu_enabled(void)
411 {
412
413 return (pmu_events_map_get(NULL) != NULL);
414 }
415
416 void
pmc_pmu_print_counters(const char * event_name)417 pmc_pmu_print_counters(const char *event_name)
418 {
419 const struct pmu_events_map *pme;
420 const struct pmu_event *pe;
421 struct pmu_event_desc ped;
422 char *debug;
423 int do_debug;
424
425 debug = getenv("PMUDEBUG");
426 do_debug = 0;
427
428 if (debug != NULL && strcmp(debug, "true") == 0)
429 do_debug = 1;
430 if ((pme = pmu_events_map_get(NULL)) == NULL)
431 return;
432 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
433 if (pe->name == NULL)
434 continue;
435 if (event_name != NULL && strcasestr(pe->name, event_name) == NULL)
436 continue;
437 printf("\t%s\n", pe->name);
438 if (do_debug)
439 pmu_parse_event(&ped, pe->event);
440 }
441 }
442
443 void
pmc_pmu_print_counter_desc(const char * ev)444 pmc_pmu_print_counter_desc(const char *ev)
445 {
446 const struct pmu_events_map *pme;
447 const struct pmu_event *pe;
448
449 if ((pme = pmu_events_map_get(NULL)) == NULL)
450 return;
451 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
452 if (pe->name == NULL)
453 continue;
454 if (strcasestr(pe->name, ev) != NULL &&
455 pe->desc != NULL)
456 printf("%s:\t%s\n", pe->name, pe->desc);
457 }
458 }
459
460 void
pmc_pmu_print_counter_desc_long(const char * ev)461 pmc_pmu_print_counter_desc_long(const char *ev)
462 {
463 const struct pmu_events_map *pme;
464 const struct pmu_event *pe;
465
466 if ((pme = pmu_events_map_get(NULL)) == NULL)
467 return;
468 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
469 if (pe->name == NULL)
470 continue;
471 if (strcasestr(pe->name, ev) != NULL) {
472 if (pe->long_desc != NULL)
473 printf("%s:\n%s\n", pe->name, pe->long_desc);
474 else if (pe->desc != NULL)
475 printf("%s:\t%s\n", pe->name, pe->desc);
476 }
477 }
478 }
479
480 void
pmc_pmu_print_counter_full(const char * ev)481 pmc_pmu_print_counter_full(const char *ev)
482 {
483 const struct pmu_events_map *pme;
484 const struct pmu_event *pe;
485
486 if ((pme = pmu_events_map_get(NULL)) == NULL)
487 return;
488 for (pe = pme->table; pe->name || pe->desc || pe->event; pe++) {
489 if (pe->name == NULL)
490 continue;
491 if (strcasestr(pe->name, ev) == NULL)
492 continue;
493 if (pe != pme->table)
494 printf("\n");
495 printf("name: %s\n", pe->name);
496 if (pe->long_desc != NULL)
497 printf("desc: %s\n", pe->long_desc);
498 else if (pe->desc != NULL)
499 printf("desc: %s\n", pe->desc);
500 if (pe->event != NULL)
501 printf("event: %s\n", pe->event);
502 if (pe->topic != NULL)
503 printf("topic: %s\n", pe->topic);
504 if (pe->pmu != NULL)
505 printf("pmu: %s\n", pe->pmu);
506 if (pe->unit != NULL)
507 printf("unit: %s\n", pe->unit);
508 if (pe->perpkg != NULL)
509 printf("perpkg: %s\n", pe->perpkg);
510 if (pe->metric_expr != NULL)
511 printf("metric_expr: %s\n", pe->metric_expr);
512 if (pe->metric_name != NULL)
513 printf("metric_name: %s\n", pe->metric_name);
514 if (pe->metric_group != NULL)
515 printf("metric_group: %s\n", pe->metric_group);
516 }
517 }
518
519 #if defined(__amd64__) || defined(__i386__)
520 static int
pmc_pmu_amd_pmcallocate(const char * event_name,struct pmc_op_pmcallocate * pm,struct pmu_event_desc * ped)521 pmc_pmu_amd_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
522 struct pmu_event_desc *ped)
523 {
524 int cpu_family;
525 struct pmc_md_amd_op_pmcallocate *amd;
526 const struct pmu_event *pe;
527 int idx = -1;
528
529 cpu_family = pmu_events_x86_family();
530
531 amd = &pm->pm_md.pm_amd;
532 if (ped->ped_umask > 0) {
533 pm->pm_caps |= PMC_CAP_QUALIFIER;
534 }
535 pm->pm_class = PMC_CLASS_K8;
536 pe = pmu_event_get(NULL, event_name, &idx);
537
538 if (pe->pmu == NULL) {
539 amd->pm_amd_config |= AMD_PMC_TO_EVENTMASK(ped->ped_event);
540 amd->pm_amd_config |= AMD_PMC_TO_UNITMASK(ped->ped_umask);
541 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_CORE;
542 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
543 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
544 (PMC_CAP_USER|PMC_CAP_SYSTEM))
545 amd->pm_amd_config |= (AMD_PMC_USR | AMD_PMC_OS);
546 else if (pm->pm_caps & PMC_CAP_USER)
547 amd->pm_amd_config |= AMD_PMC_USR;
548 else if (pm->pm_caps & PMC_CAP_SYSTEM)
549 amd->pm_amd_config |= AMD_PMC_OS;
550 if (ped->ped_edge)
551 amd->pm_amd_config |= AMD_PMC_EDGE;
552 if (ped->ped_inv)
553 amd->pm_amd_config |= AMD_PMC_INVERT;
554 if (pm->pm_caps & PMC_CAP_INTERRUPT)
555 amd->pm_amd_config |= AMD_PMC_INT;
556 if (pm->pm_caps & PMC_CAP_PRECISE)
557 amd->pm_amd_config |= AMD_PMC_PRECISERETIRE;
558 } else if (strcmp("amd_l3", pe->pmu) == 0) {
559 amd->pm_amd_config |= AMD_PMC_L3_TO_EVENTMASK(ped->ped_event);
560 amd->pm_amd_config |= AMD_PMC_L3_TO_UNITMASK(ped->ped_umask);
561 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_L3_CACHE;
562 if (cpu_family <= 0x17) {
563 amd->pm_amd_config |=
564 AMD_PMC_L31_TO_SLICE(ped->ped_l3_slice);
565 amd->pm_amd_config |=
566 AMD_PMC_L31_TO_CORE(ped->ped_l3_thread);
567 } else {
568 amd->pm_amd_config |=
569 AMD_PMC_L32_TO_THREAD(ped->ped_l3_thread);
570 amd->pm_amd_config |=
571 AMD_PMC_L32_TO_SOURCEID(ped->ped_sourceid);
572 amd->pm_amd_config |=
573 AMD_PMC_L32_TO_COREID(ped->ped_coreid);
574 if (ped->ped_allcores)
575 amd->pm_amd_config |= AMD_PMC_L32_ALLCORES;
576 if (ped->ped_allsources)
577 amd->pm_amd_config |= AMD_PMC_L32_ALLSOURCES;
578 }
579 } else if (strcmp("amd_df", pe->pmu) == 0) {
580 amd->pm_amd_sub_class = PMC_AMD_SUB_CLASS_DATA_FABRIC;
581 if (cpu_family <= 19) {
582 amd->pm_amd_config |=
583 AMD_PMC_DF1_TO_EVENTMASK(ped->ped_event);
584 amd->pm_amd_config |=
585 AMD_PMC_DF1_TO_UNITMASK(ped->ped_umask);
586 } else {
587 amd->pm_amd_config |=
588 AMD_PMC_DF2_TO_EVENTMASK(ped->ped_event);
589 amd->pm_amd_config |=
590 AMD_PMC_DF2_TO_UNITMASK(ped->ped_umask);
591 }
592 } else {
593 printf("PMC pmu '%s' is not supported!\n", pe->pmu);
594 return (EOPNOTSUPP);
595 }
596
597 return (0);
598 }
599
600 static int
pmc_pmu_intel_pmcallocate(const char * event_name,struct pmc_op_pmcallocate * pm,struct pmu_event_desc * ped)601 pmc_pmu_intel_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm,
602 struct pmu_event_desc *ped)
603 {
604 struct pmc_md_iap_op_pmcallocate *iap;
605
606 iap = &pm->pm_md.pm_iap;
607 if (strcasestr(event_name, "UNC_") == event_name ||
608 strcasestr(event_name, "uncore") != NULL) {
609 pm->pm_class = PMC_CLASS_UCP;
610 pm->pm_caps |= PMC_CAP_QUALIFIER;
611 /* XXX Check and block unsupported uncore counters */
612 } else if (ped->ped_event == 0x0) {
613 pm->pm_class = PMC_CLASS_IAF;
614 } else {
615 pm->pm_class = PMC_CLASS_IAP;
616 pm->pm_caps |= PMC_CAP_QUALIFIER;
617 }
618 iap->pm_iap_config |= IAP_EVSEL(ped->ped_event);
619 if (ped->ped_umask > 0)
620 iap->pm_iap_config |= IAP_UMASK(ped->ped_umask);
621 iap->pm_iap_config |= IAP_CMASK(ped->ped_cmask);
622 iap->pm_iap_rsp = ped->ped_offcore_rsp;
623
624 if ((pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) == 0 ||
625 (pm->pm_caps & (PMC_CAP_USER|PMC_CAP_SYSTEM)) ==
626 (PMC_CAP_USER|PMC_CAP_SYSTEM))
627 iap->pm_iap_config |= (IAP_USR | IAP_OS);
628 else if (pm->pm_caps & PMC_CAP_USER)
629 iap->pm_iap_config |= IAP_USR;
630 else if (pm->pm_caps & PMC_CAP_SYSTEM)
631 iap->pm_iap_config |= IAP_OS;
632 if (ped->ped_edge)
633 iap->pm_iap_config |= IAP_EDGE;
634 if (ped->ped_any)
635 iap->pm_iap_config |= IAP_ANY;
636 if (ped->ped_inv)
637 iap->pm_iap_config |= IAP_INV;
638 if (pm->pm_caps & PMC_CAP_INTERRUPT)
639 iap->pm_iap_config |= IAP_INT;
640
641 /* XXX Implement alone flag in the kernel module */
642
643 /*
644 * PEBS counters are not implemented on FreeBSD yet. Counters labeled
645 * with PEBS = 2 in the JSON definitions require PEBS. It seems that
646 * some of them return bogus counts if you attempt to use them.
647 */
648 if (ped->ped_pebs == 2) {
649 printf("PEBS only counters are not supported!\n");
650 return (EOPNOTSUPP);
651 }
652
653 return (0);
654 }
655
656 static int
pmc_pmu_pmcallocate_md(const char * event_name,struct pmc_op_pmcallocate * pm)657 pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm)
658 {
659 const struct pmu_event *pe;
660 struct pmu_event_desc ped;
661 pmu_mfr_t mfr;
662 int idx = -1;
663
664 if ((mfr = pmu_events_mfr()) == PMU_INVALID)
665 return (ENOENT);
666
667 bzero(&pm->pm_md, sizeof(pm->pm_md));
668 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
669 event_name = pmu_alias_get(event_name);
670 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
671 return (ENOENT);
672 assert(idx >= 0);
673 pm->pm_ev = idx;
674
675 if (pe->event == NULL)
676 return (ENOENT);
677 if (pmu_parse_event(&ped, pe->event))
678 return (ENOENT);
679
680 if (mfr == PMU_INTEL)
681 return (pmc_pmu_intel_pmcallocate(event_name, pm, &ped));
682 else
683 return (pmc_pmu_amd_pmcallocate(event_name, pm, &ped));
684 }
685
686 #elif defined(__powerpc64__)
687
688 static int
pmc_pmu_pmcallocate_md(const char * event_name,struct pmc_op_pmcallocate * pm)689 pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm)
690 {
691 const struct pmu_event *pe;
692 struct pmu_event_desc ped;
693 int idx = -1;
694
695 bzero(&pm->pm_md, sizeof(pm->pm_md));
696 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
697 event_name = pmu_alias_get(event_name);
698
699 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
700 return (ENOENT);
701 if (pe->event == NULL)
702 return (ENOENT);
703 if (pmu_parse_event(&ped, pe->event))
704 return (ENOENT);
705
706 pm->pm_ev = idx;
707 pm->pm_md.pm_event = ped.ped_event;
708 pm->pm_class = PMC_CLASS_POWER8;
709 return (0);
710 }
711
712 #elif defined(__aarch64__)
713
714 static int
pmc_pmu_pmcallocate_md(const char * event_name,struct pmc_op_pmcallocate * pm)715 pmc_pmu_pmcallocate_md(const char *event_name, struct pmc_op_pmcallocate *pm)
716 {
717 const struct pmu_event *pe;
718 struct pmu_event_desc ped;
719 int idx = -1;
720
721 event_name = pmu_alias_get(event_name);
722 if ((pe = pmu_event_get(NULL, event_name, &idx)) == NULL)
723 return (ENOENT);
724 if (pe->event == NULL)
725 return (ENOENT);
726 if (pmu_parse_event(&ped, pe->event))
727 return (ENOENT);
728
729 assert(idx >= 0);
730 pm->pm_ev = idx;
731 pm->pm_md.pm_md_config = ped.ped_event;
732 pm->pm_class = PMC_CLASS_ARMV8;
733 pm->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
734
735 return (0);
736 }
737
738 #else
739
740 static int
pmc_pmu_pmcallocate_md(const char * e __unused,struct pmc_op_pmcallocate * p __unused)741 pmc_pmu_pmcallocate_md(const char *e __unused, struct pmc_op_pmcallocate *p __unused)
742 {
743 return (EOPNOTSUPP);
744 }
745 #endif
746
747 int
pmc_pmu_pmcallocate(const char * event_name,struct pmc_op_pmcallocate * pm)748 pmc_pmu_pmcallocate(const char *event_name, struct pmc_op_pmcallocate *pm)
749 {
750 int error;
751
752 error = pmc_pmu_pmcallocate_md(event_name, pm);
753 if (error != 0) {
754 /* Reset any changes. */
755 pm->pm_ev = 0;
756 pm->pm_caps = 0;
757 pm->pm_class = 0;
758
759 return (error);
760 }
761
762 pm->pm_flags |= PMC_F_EV_PMU;
763 return (0);
764 }
765