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