2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003-2008 Joseph Koshy
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/module.h>
36 #include <sys/syscall.h>
50 #include "libpmcinternal.h"
52 /* Function prototypes */
53 #if defined(__amd64__) || defined(__i386__)
54 static int k8_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
55 struct pmc_op_pmcallocate *_pmc_config);
57 #if defined(__amd64__) || defined(__i386__)
58 static int tsc_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
59 struct pmc_op_pmcallocate *_pmc_config);
62 static int armv7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
63 struct pmc_op_pmcallocate *_pmc_config);
65 #if defined(__aarch64__)
66 static int arm64_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
67 struct pmc_op_pmcallocate *_pmc_config);
70 static int mips_allocate_pmc(enum pmc_event _pe, char* ctrspec,
71 struct pmc_op_pmcallocate *_pmc_config);
73 static int soft_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
74 struct pmc_op_pmcallocate *_pmc_config);
76 #if defined(__powerpc__)
77 static int powerpc_allocate_pmc(enum pmc_event _pe, char* ctrspec,
78 struct pmc_op_pmcallocate *_pmc_config);
79 #endif /* __powerpc__ */
81 #define PMC_CALL(cmd, params) \
82 syscall(pmc_syscall, PMC_OP_##cmd, (params))
85 * Event aliases provide a way for the user to ask for generic events
86 * like "cache-misses", or "instructions-retired". These aliases are
87 * mapped to the appropriate canonical event descriptions using a
90 struct pmc_event_alias {
95 static const struct pmc_event_alias *pmc_mdep_event_aliases;
98 * The pmc_event_descr structure maps symbolic names known to the user
99 * to integer codes used by the PMC KLD.
101 struct pmc_event_descr {
102 const char *pm_ev_name;
103 enum pmc_event pm_ev_code;
107 * The pmc_class_descr structure maps class name prefixes for
108 * event names to event tables and other PMC class data.
110 struct pmc_class_descr {
111 const char *pm_evc_name;
112 size_t pm_evc_name_size;
113 enum pmc_class pm_evc_class;
114 const struct pmc_event_descr *pm_evc_event_table;
115 size_t pm_evc_event_table_size;
116 int (*pm_evc_allocate_pmc)(enum pmc_event _pe,
117 char *_ctrspec, struct pmc_op_pmcallocate *_pa);
120 #define PMC_TABLE_SIZE(N) (sizeof(N)/sizeof(N[0]))
121 #define PMC_EVENT_TABLE_SIZE(N) PMC_TABLE_SIZE(N##_event_table)
124 #define __PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
127 * PMC_CLASSDEP_TABLE(NAME, CLASS)
129 * Define a table mapping event names and aliases to HWPMC event IDs.
131 #define PMC_CLASSDEP_TABLE(N, C) \
132 static const struct pmc_event_descr N##_event_table[] = \
137 PMC_CLASSDEP_TABLE(iaf, IAF);
138 PMC_CLASSDEP_TABLE(k8, K8);
139 PMC_CLASSDEP_TABLE(armv7, ARMV7);
140 PMC_CLASSDEP_TABLE(armv8, ARMV8);
141 PMC_CLASSDEP_TABLE(beri, BERI);
142 PMC_CLASSDEP_TABLE(mips24k, MIPS24K);
143 PMC_CLASSDEP_TABLE(mips74k, MIPS74K);
144 PMC_CLASSDEP_TABLE(octeon, OCTEON);
145 PMC_CLASSDEP_TABLE(ppc7450, PPC7450);
146 PMC_CLASSDEP_TABLE(ppc970, PPC970);
147 PMC_CLASSDEP_TABLE(e500, E500);
149 static struct pmc_event_descr soft_event_table[PMC_EV_DYN_COUNT];
151 #undef __PMC_EV_ALIAS
152 #define __PMC_EV_ALIAS(N,CODE) { N, PMC_EV_##CODE },
154 static const struct pmc_event_descr cortex_a8_event_table[] =
156 __PMC_EV_ALIAS_ARMV7_CORTEX_A8()
159 static const struct pmc_event_descr cortex_a9_event_table[] =
161 __PMC_EV_ALIAS_ARMV7_CORTEX_A9()
164 static const struct pmc_event_descr cortex_a53_event_table[] =
166 __PMC_EV_ALIAS_ARMV8_CORTEX_A53()
169 static const struct pmc_event_descr cortex_a57_event_table[] =
171 __PMC_EV_ALIAS_ARMV8_CORTEX_A57()
174 static const struct pmc_event_descr cortex_a76_event_table[] =
176 __PMC_EV_ALIAS_ARMV8_CORTEX_A76()
179 static const struct pmc_event_descr tsc_event_table[] =
184 #undef PMC_CLASS_TABLE_DESC
185 #define PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR) \
186 static const struct pmc_class_descr NAME##_class_table_descr = \
188 .pm_evc_name = #CLASS "-", \
189 .pm_evc_name_size = sizeof(#CLASS "-") - 1, \
190 .pm_evc_class = PMC_CLASS_##CLASS , \
191 .pm_evc_event_table = EVENTS##_event_table , \
192 .pm_evc_event_table_size = \
193 PMC_EVENT_TABLE_SIZE(EVENTS), \
194 .pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc \
197 #if defined(__i386__) || defined(__amd64__)
198 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
200 #if defined(__i386__) || defined(__amd64__)
201 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
204 PMC_CLASS_TABLE_DESC(cortex_a8, ARMV7, cortex_a8, armv7);
205 PMC_CLASS_TABLE_DESC(cortex_a9, ARMV7, cortex_a9, armv7);
207 #if defined(__aarch64__)
208 PMC_CLASS_TABLE_DESC(cortex_a53, ARMV8, cortex_a53, arm64);
209 PMC_CLASS_TABLE_DESC(cortex_a57, ARMV8, cortex_a57, arm64);
210 PMC_CLASS_TABLE_DESC(cortex_a76, ARMV8, cortex_a76, arm64);
212 #if defined(__mips__)
213 PMC_CLASS_TABLE_DESC(beri, BERI, beri, mips);
214 PMC_CLASS_TABLE_DESC(mips24k, MIPS24K, mips24k, mips);
215 PMC_CLASS_TABLE_DESC(mips74k, MIPS74K, mips74k, mips);
216 PMC_CLASS_TABLE_DESC(octeon, OCTEON, octeon, mips);
217 #endif /* __mips__ */
218 #if defined(__powerpc__)
219 PMC_CLASS_TABLE_DESC(ppc7450, PPC7450, ppc7450, powerpc);
220 PMC_CLASS_TABLE_DESC(ppc970, PPC970, ppc970, powerpc);
221 PMC_CLASS_TABLE_DESC(e500, E500, e500, powerpc);
224 static struct pmc_class_descr soft_class_table_descr =
226 .pm_evc_name = "SOFT-",
227 .pm_evc_name_size = sizeof("SOFT-") - 1,
228 .pm_evc_class = PMC_CLASS_SOFT,
229 .pm_evc_event_table = NULL,
230 .pm_evc_event_table_size = 0,
231 .pm_evc_allocate_pmc = soft_allocate_pmc
234 #undef PMC_CLASS_TABLE_DESC
236 static const struct pmc_class_descr **pmc_class_table;
237 #define PMC_CLASS_TABLE_SIZE cpu_info.pm_nclass
240 * Mapping tables, mapping enumeration values to human readable
244 static const char * pmc_capability_names[] = {
246 #define __PMC_CAP(N,V,D) #N ,
250 struct pmc_class_map {
251 enum pmc_class pm_class;
255 static const struct pmc_class_map pmc_class_names[] = {
257 #define __PMC_CLASS(S,V,D) { .pm_class = PMC_CLASS_##S, .pm_name = #S } ,
261 struct pmc_cputype_map {
262 enum pmc_cputype pm_cputype;
266 static const struct pmc_cputype_map pmc_cputype_names[] = {
268 #define __PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
272 static const char * pmc_disposition_names[] = {
274 #define __PMC_DISP(D) #D ,
278 static const char * pmc_mode_names[] = {
280 #define __PMC_MODE(M,N) #M ,
284 static const char * pmc_state_names[] = {
286 #define __PMC_STATE(S) #S ,
291 * Filled in by pmc_init().
293 static int pmc_syscall = -1;
294 static struct pmc_cpuinfo cpu_info;
295 static struct pmc_op_getdyneventinfo soft_event_info;
297 /* Event masks for events */
300 const uint64_t pm_value;
302 #define PMCMASK(N,V) { .pm_name = #N, .pm_value = (V) }
303 #define NULLMASK { .pm_name = NULL }
305 #if defined(__amd64__) || defined(__i386__)
307 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint64_t *evmask)
309 const struct pmc_masks *pm;
313 if (pmask == NULL) /* no mask keywords */
315 q = strchr(p, '='); /* skip '=' */
316 if (*++q == '\0') /* no more data */
318 c = 0; /* count of mask keywords seen */
319 while ((r = strsep(&q, "+")) != NULL) {
320 for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
323 if (pm->pm_name == NULL) /* not found */
325 *evmask |= pm->pm_value;
332 #define KWMATCH(p,kw) (strcasecmp((p), (kw)) == 0)
333 #define KWPREFIXMATCH(p,kw) (strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
334 #define EV_ALIAS(N,S) { .pm_alias = N, .pm_spec = S }
336 #if defined(__amd64__) || defined(__i386__)
342 static struct pmc_event_alias k8_aliases[] = {
343 EV_ALIAS("branches", "k8-fr-retired-taken-branches"),
344 EV_ALIAS("branch-mispredicts",
345 "k8-fr-retired-taken-branches-mispredicted"),
346 EV_ALIAS("cycles", "tsc"),
347 EV_ALIAS("dc-misses", "k8-dc-miss"),
348 EV_ALIAS("ic-misses", "k8-ic-miss"),
349 EV_ALIAS("instructions", "k8-fr-retired-x86-instructions"),
350 EV_ALIAS("interrupts", "k8-fr-taken-hardware-interrupts"),
351 EV_ALIAS("unhalted-cycles", "k8-bu-cpu-clk-unhalted"),
355 #define __K8MASK(N,V) PMCMASK(N,(1 << (V)))
361 /* fp dispatched fpu ops */
362 static const struct pmc_masks k8_mask_fdfo[] = {
363 __K8MASK(add-pipe-excluding-junk-ops, 0),
364 __K8MASK(multiply-pipe-excluding-junk-ops, 1),
365 __K8MASK(store-pipe-excluding-junk-ops, 2),
366 __K8MASK(add-pipe-junk-ops, 3),
367 __K8MASK(multiply-pipe-junk-ops, 4),
368 __K8MASK(store-pipe-junk-ops, 5),
372 /* ls segment register loads */
373 static const struct pmc_masks k8_mask_lsrl[] = {
384 /* ls locked operation */
385 static const struct pmc_masks k8_mask_llo[] = {
386 __K8MASK(locked-instructions, 0),
387 __K8MASK(cycles-in-request, 1),
388 __K8MASK(cycles-to-complete, 2),
392 /* dc refill from {l2,system} and dc copyback */
393 static const struct pmc_masks k8_mask_dc[] = {
394 __K8MASK(invalid, 0),
396 __K8MASK(exclusive, 2),
398 __K8MASK(modified, 4),
402 /* dc one bit ecc error */
403 static const struct pmc_masks k8_mask_dobee[] = {
404 __K8MASK(scrubber, 0),
405 __K8MASK(piggyback, 1),
409 /* dc dispatched prefetch instructions */
410 static const struct pmc_masks k8_mask_ddpi[] = {
417 /* dc dcache accesses by locks */
418 static const struct pmc_masks k8_mask_dabl[] = {
419 __K8MASK(accesses, 0),
424 /* bu internal l2 request */
425 static const struct pmc_masks k8_mask_bilr[] = {
426 __K8MASK(ic-fill, 0),
427 __K8MASK(dc-fill, 1),
428 __K8MASK(tlb-reload, 2),
429 __K8MASK(tag-snoop, 3),
430 __K8MASK(cancelled, 4),
434 /* bu fill request l2 miss */
435 static const struct pmc_masks k8_mask_bfrlm[] = {
436 __K8MASK(ic-fill, 0),
437 __K8MASK(dc-fill, 1),
438 __K8MASK(tlb-reload, 2),
442 /* bu fill into l2 */
443 static const struct pmc_masks k8_mask_bfil[] = {
444 __K8MASK(dirty-l2-victim, 0),
445 __K8MASK(victim-from-l2, 1),
449 /* fr retired fpu instructions */
450 static const struct pmc_masks k8_mask_frfi[] = {
452 __K8MASK(mmx-3dnow, 1),
453 __K8MASK(packed-sse-sse2, 2),
454 __K8MASK(scalar-sse-sse2, 3),
458 /* fr retired fastpath double op instructions */
459 static const struct pmc_masks k8_mask_frfdoi[] = {
460 __K8MASK(low-op-pos-0, 0),
461 __K8MASK(low-op-pos-1, 1),
462 __K8MASK(low-op-pos-2, 2),
466 /* fr fpu exceptions */
467 static const struct pmc_masks k8_mask_ffe[] = {
468 __K8MASK(x87-reclass-microfaults, 0),
469 __K8MASK(sse-retype-microfaults, 1),
470 __K8MASK(sse-reclass-microfaults, 2),
471 __K8MASK(sse-and-x87-microtraps, 3),
475 /* nb memory controller page access event */
476 static const struct pmc_masks k8_mask_nmcpae[] = {
477 __K8MASK(page-hit, 0),
478 __K8MASK(page-miss, 1),
479 __K8MASK(page-conflict, 2),
483 /* nb memory controller turnaround */
484 static const struct pmc_masks k8_mask_nmct[] = {
485 __K8MASK(dimm-turnaround, 0),
486 __K8MASK(read-to-write-turnaround, 1),
487 __K8MASK(write-to-read-turnaround, 2),
491 /* nb memory controller bypass saturation */
492 static const struct pmc_masks k8_mask_nmcbs[] = {
493 __K8MASK(memory-controller-hi-pri-bypass, 0),
494 __K8MASK(memory-controller-lo-pri-bypass, 1),
495 __K8MASK(dram-controller-interface-bypass, 2),
496 __K8MASK(dram-controller-queue-bypass, 3),
500 /* nb sized commands */
501 static const struct pmc_masks k8_mask_nsc[] = {
502 __K8MASK(nonpostwrszbyte, 0),
503 __K8MASK(nonpostwrszdword, 1),
504 __K8MASK(postwrszbyte, 2),
505 __K8MASK(postwrszdword, 3),
506 __K8MASK(rdszbyte, 4),
507 __K8MASK(rdszdword, 5),
508 __K8MASK(rdmodwr, 6),
512 /* nb probe result */
513 static const struct pmc_masks k8_mask_npr[] = {
514 __K8MASK(probe-miss, 0),
515 __K8MASK(probe-hit, 1),
516 __K8MASK(probe-hit-dirty-no-memory-cancel, 2),
517 __K8MASK(probe-hit-dirty-with-memory-cancel, 3),
521 /* nb hypertransport bus bandwidth */
522 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
523 __K8MASK(command, 0),
525 __K8MASK(buffer-release, 2),
532 #define K8_KW_COUNT "count"
533 #define K8_KW_EDGE "edge"
534 #define K8_KW_INV "inv"
535 #define K8_KW_MASK "mask"
536 #define K8_KW_OS "os"
537 #define K8_KW_USR "usr"
540 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
541 struct pmc_op_pmcallocate *pmc_config)
547 const struct pmc_masks *pm, *pmask;
549 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
550 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
555 #define __K8SETMASK(M) pmask = k8_mask_##M
557 /* setup parsing tables */
559 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
562 case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
565 case PMC_EV_K8_LS_LOCKED_OPERATION:
568 case PMC_EV_K8_DC_REFILL_FROM_L2:
569 case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
570 case PMC_EV_K8_DC_COPYBACK:
573 case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
576 case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
579 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
582 case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
585 case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
588 case PMC_EV_K8_BU_FILL_INTO_L2:
591 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
594 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
597 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
600 case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
603 case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
606 case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
609 case PMC_EV_K8_NB_SIZED_COMMANDS:
612 case PMC_EV_K8_NB_PROBE_RESULT:
615 case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
616 case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
617 case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
622 break; /* no options defined */
625 while ((p = strsep(&ctrspec, ",")) != NULL) {
626 if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
628 if (*++q == '\0') /* skip '=' */
631 count = strtol(q, &e, 0);
632 if (e == q || *e != '\0')
635 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
636 pmc_config->pm_md.pm_amd.pm_amd_config |=
637 AMD_PMC_TO_COUNTER(count);
639 } else if (KWMATCH(p, K8_KW_EDGE)) {
640 pmc_config->pm_caps |= PMC_CAP_EDGE;
641 } else if (KWMATCH(p, K8_KW_INV)) {
642 pmc_config->pm_caps |= PMC_CAP_INVERT;
643 } else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
644 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
646 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
647 } else if (KWMATCH(p, K8_KW_OS)) {
648 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
649 } else if (KWMATCH(p, K8_KW_USR)) {
650 pmc_config->pm_caps |= PMC_CAP_USER;
655 /* other post processing */
657 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
658 case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
659 case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
660 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
661 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
662 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
663 /* XXX only available in rev B and later */
665 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
666 /* XXX only available in rev C and later */
668 case PMC_EV_K8_LS_LOCKED_OPERATION:
669 /* XXX CPU Rev A,B evmask is to be zero */
670 if (evmask & (evmask - 1)) /* > 1 bit set */
673 evmask = 0x01; /* Rev C and later: #instrs */
674 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
678 if (evmask == 0 && pmask != NULL) {
679 for (pm = pmask; pm->pm_name; pm++)
680 evmask |= pm->pm_value;
681 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
685 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
686 pmc_config->pm_md.pm_amd.pm_amd_config =
687 AMD_PMC_TO_UNITMASK(evmask);
694 #if defined(__i386__) || defined(__amd64__)
696 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
697 struct pmc_op_pmcallocate *pmc_config)
699 if (pe != PMC_EV_TSC_TSC)
702 /* TSC events must be unqualified. */
703 if (ctrspec && *ctrspec != '\0')
706 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
707 pmc_config->pm_caps |= PMC_CAP_READ;
713 static struct pmc_event_alias generic_aliases[] = {
714 EV_ALIAS("instructions", "SOFT-CLOCK.HARD"),
719 soft_allocate_pmc(enum pmc_event pe, char *ctrspec,
720 struct pmc_op_pmcallocate *pmc_config)
725 if ((int)pe < PMC_EV_SOFT_FIRST || (int)pe > PMC_EV_SOFT_LAST)
728 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
733 static struct pmc_event_alias cortex_a8_aliases[] = {
734 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
735 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
736 EV_ALIAS("instructions", "INSTR_EXECUTED"),
740 static struct pmc_event_alias cortex_a9_aliases[] = {
741 EV_ALIAS("dc-misses", "L1_DCACHE_REFILL"),
742 EV_ALIAS("ic-misses", "L1_ICACHE_REFILL"),
743 EV_ALIAS("instructions", "INSTR_EXECUTED"),
748 armv7_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
749 struct pmc_op_pmcallocate *pmc_config __unused)
760 #if defined(__aarch64__)
761 static struct pmc_event_alias cortex_a53_aliases[] = {
764 static struct pmc_event_alias cortex_a57_aliases[] = {
767 static struct pmc_event_alias cortex_a76_aliases[] = {
771 arm64_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
772 struct pmc_op_pmcallocate *pmc_config __unused)
783 #if defined(__mips__)
785 static struct pmc_event_alias beri_aliases[] = {
786 EV_ALIAS("instructions", "INST"),
790 static struct pmc_event_alias mips24k_aliases[] = {
791 EV_ALIAS("instructions", "INSTR_EXECUTED"),
792 EV_ALIAS("branches", "BRANCH_COMPLETED"),
793 EV_ALIAS("branch-mispredicts", "BRANCH_MISPRED"),
797 static struct pmc_event_alias mips74k_aliases[] = {
798 EV_ALIAS("instructions", "INSTR_EXECUTED"),
799 EV_ALIAS("branches", "BRANCH_INSNS"),
800 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCH_INSNS"),
804 static struct pmc_event_alias octeon_aliases[] = {
805 EV_ALIAS("instructions", "RET"),
806 EV_ALIAS("branches", "BR"),
807 EV_ALIAS("branch-mispredicts", "BRMIS"),
811 #define MIPS_KW_OS "os"
812 #define MIPS_KW_USR "usr"
813 #define MIPS_KW_ANYTHREAD "anythread"
816 mips_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
817 struct pmc_op_pmcallocate *pmc_config __unused)
823 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
825 while ((p = strsep(&ctrspec, ",")) != NULL) {
826 if (KWMATCH(p, MIPS_KW_OS))
827 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
828 else if (KWMATCH(p, MIPS_KW_USR))
829 pmc_config->pm_caps |= PMC_CAP_USER;
830 else if (KWMATCH(p, MIPS_KW_ANYTHREAD))
831 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
839 #endif /* __mips__ */
841 #if defined(__powerpc__)
843 static struct pmc_event_alias ppc7450_aliases[] = {
844 EV_ALIAS("instructions", "INSTR_COMPLETED"),
845 EV_ALIAS("branches", "BRANCHES_COMPLETED"),
846 EV_ALIAS("branch-mispredicts", "MISPREDICTED_BRANCHES"),
850 static struct pmc_event_alias ppc970_aliases[] = {
851 EV_ALIAS("instructions", "INSTR_COMPLETED"),
852 EV_ALIAS("cycles", "CYCLES"),
856 static struct pmc_event_alias e500_aliases[] = {
857 EV_ALIAS("instructions", "INSTR_COMPLETED"),
858 EV_ALIAS("cycles", "CYCLES"),
862 #define POWERPC_KW_OS "os"
863 #define POWERPC_KW_USR "usr"
864 #define POWERPC_KW_ANYTHREAD "anythread"
867 powerpc_allocate_pmc(enum pmc_event pe, char *ctrspec __unused,
868 struct pmc_op_pmcallocate *pmc_config __unused)
874 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
876 while ((p = strsep(&ctrspec, ",")) != NULL) {
877 if (KWMATCH(p, POWERPC_KW_OS))
878 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
879 else if (KWMATCH(p, POWERPC_KW_USR))
880 pmc_config->pm_caps |= PMC_CAP_USER;
881 else if (KWMATCH(p, POWERPC_KW_ANYTHREAD))
882 pmc_config->pm_caps |= (PMC_CAP_USER | PMC_CAP_SYSTEM);
890 #endif /* __powerpc__ */
894 * Match an event name `name' with its canonical form.
896 * Matches are case insensitive and spaces, periods, underscores and
897 * hyphen characters are considered to match each other.
899 * Returns 1 for a match, 0 otherwise.
903 pmc_match_event_name(const char *name, const char *canonicalname)
906 const unsigned char *c, *n;
908 c = (const unsigned char *) canonicalname;
909 n = (const unsigned char *) name;
911 for (; (nc = *n) && (cc = *c); n++, c++) {
913 if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
914 (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
917 if (toupper(nc) == toupper(cc))
924 if (*n == '\0' && *c == '\0')
931 * Match an event name against all the event named supported by a
934 * Returns an event descriptor pointer on match or NULL otherwise.
936 static const struct pmc_event_descr *
937 pmc_match_event_class(const char *name,
938 const struct pmc_class_descr *pcd)
941 const struct pmc_event_descr *ev;
943 ev = pcd->pm_evc_event_table;
944 for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
945 if (pmc_match_event_name(name, ev->pm_ev_name))
956 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
957 uint32_t flags, int cpu, pmc_id_t *pmcid,
964 const struct pmc_event_descr *ev;
965 const struct pmc_event_alias *alias;
966 struct pmc_op_pmcallocate pmc_config;
967 const struct pmc_class_descr *pcd;
972 if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
973 mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
977 bzero(&pmc_config, sizeof(pmc_config));
978 pmc_config.pm_cpu = cpu;
979 pmc_config.pm_mode = mode;
980 pmc_config.pm_flags = flags;
981 pmc_config.pm_count = count;
982 if (PMC_IS_SAMPLING_MODE(mode))
983 pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
986 * Try to pull the raw event ID directly from the pmu-events table. If
987 * this is unsupported on the platform, or the event is not found,
988 * continue with searching the regular event tables.
990 r = spec_copy = strdup(ctrspec);
991 ctrname = strsep(&r, ",");
992 if (pmc_pmu_enabled()) {
993 if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0)
996 /* Otherwise, reset any changes */
997 pmc_config.pm_ev = 0;
998 pmc_config.pm_caps = 0;
999 pmc_config.pm_class = 0;
1004 /* replace an event alias with the canonical event specifier */
1005 if (pmc_mdep_event_aliases)
1006 for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
1007 if (!strcasecmp(ctrspec, alias->pm_alias)) {
1008 spec_copy = strdup(alias->pm_spec);
1012 if (spec_copy == NULL)
1013 spec_copy = strdup(ctrspec);
1016 ctrname = strsep(&r, ",");
1019 * If a explicit class prefix was given by the user, restrict the
1020 * search for the event to the specified PMC class.
1023 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
1024 pcd = pmc_class_table[n];
1025 if (pcd != NULL && strncasecmp(ctrname, pcd->pm_evc_name,
1026 pcd->pm_evc_name_size) == 0) {
1027 if ((ev = pmc_match_event_class(ctrname +
1028 pcd->pm_evc_name_size, pcd)) == NULL) {
1037 * Otherwise, search for this event in all compatible PMC
1040 for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
1041 pcd = pmc_class_table[n];
1043 ev = pmc_match_event_class(ctrname, pcd);
1051 pmc_config.pm_ev = ev->pm_ev_code;
1052 pmc_config.pm_class = pcd->pm_evc_class;
1054 if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
1060 if (PMC_CALL(PMCALLOCATE, &pmc_config) == 0) {
1061 *pmcid = pmc_config.pm_pmcid;
1072 pmc_attach(pmc_id_t pmc, pid_t pid)
1074 struct pmc_op_pmcattach pmc_attach_args;
1076 pmc_attach_args.pm_pmc = pmc;
1077 pmc_attach_args.pm_pid = pid;
1079 return (PMC_CALL(PMCATTACH, &pmc_attach_args));
1083 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
1088 cl = PMC_ID_TO_CLASS(pmcid);
1089 for (i = 0; i < cpu_info.pm_nclass; i++)
1090 if (cpu_info.pm_classes[i].pm_class == cl) {
1091 *caps = cpu_info.pm_classes[i].pm_caps;
1099 pmc_configure_logfile(int fd)
1101 struct pmc_op_configurelog cla;
1104 if (PMC_CALL(CONFIGURELOG, &cla) < 0)
1110 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
1112 if (pmc_syscall == -1) {
1122 pmc_detach(pmc_id_t pmc, pid_t pid)
1124 struct pmc_op_pmcattach pmc_detach_args;
1126 pmc_detach_args.pm_pmc = pmc;
1127 pmc_detach_args.pm_pid = pid;
1128 return (PMC_CALL(PMCDETACH, &pmc_detach_args));
1132 pmc_disable(int cpu, int pmc)
1134 struct pmc_op_pmcadmin ssa;
1138 ssa.pm_state = PMC_STATE_DISABLED;
1139 return (PMC_CALL(PMCADMIN, &ssa));
1143 pmc_enable(int cpu, int pmc)
1145 struct pmc_op_pmcadmin ssa;
1149 ssa.pm_state = PMC_STATE_FREE;
1150 return (PMC_CALL(PMCADMIN, &ssa));
1154 * Return a list of events known to a given PMC class. 'cl' is the
1155 * PMC class identifier, 'eventnames' is the returned list of 'const
1156 * char *' pointers pointing to the names of the events. 'nevents' is
1157 * the number of event name pointers returned.
1159 * The space for 'eventnames' is allocated using malloc(3). The caller
1160 * is responsible for freeing this space when done.
1163 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
1168 const struct pmc_event_descr *ev;
1173 ev = iaf_event_table;
1174 count = PMC_EVENT_TABLE_SIZE(iaf);
1177 ev = tsc_event_table;
1178 count = PMC_EVENT_TABLE_SIZE(tsc);
1181 ev = k8_event_table;
1182 count = PMC_EVENT_TABLE_SIZE(k8);
1184 case PMC_CLASS_ARMV7:
1185 switch (cpu_info.pm_cputype) {
1187 case PMC_CPU_ARMV7_CORTEX_A8:
1188 ev = cortex_a8_event_table;
1189 count = PMC_EVENT_TABLE_SIZE(cortex_a8);
1191 case PMC_CPU_ARMV7_CORTEX_A9:
1192 ev = cortex_a9_event_table;
1193 count = PMC_EVENT_TABLE_SIZE(cortex_a9);
1197 case PMC_CLASS_ARMV8:
1198 switch (cpu_info.pm_cputype) {
1200 case PMC_CPU_ARMV8_CORTEX_A53:
1201 ev = cortex_a53_event_table;
1202 count = PMC_EVENT_TABLE_SIZE(cortex_a53);
1204 case PMC_CPU_ARMV8_CORTEX_A57:
1205 ev = cortex_a57_event_table;
1206 count = PMC_EVENT_TABLE_SIZE(cortex_a57);
1208 case PMC_CPU_ARMV8_CORTEX_A76:
1209 ev = cortex_a76_event_table;
1210 count = PMC_EVENT_TABLE_SIZE(cortex_a76);
1214 case PMC_CLASS_BERI:
1215 ev = beri_event_table;
1216 count = PMC_EVENT_TABLE_SIZE(beri);
1218 case PMC_CLASS_MIPS24K:
1219 ev = mips24k_event_table;
1220 count = PMC_EVENT_TABLE_SIZE(mips24k);
1222 case PMC_CLASS_MIPS74K:
1223 ev = mips74k_event_table;
1224 count = PMC_EVENT_TABLE_SIZE(mips74k);
1226 case PMC_CLASS_OCTEON:
1227 ev = octeon_event_table;
1228 count = PMC_EVENT_TABLE_SIZE(octeon);
1230 case PMC_CLASS_PPC7450:
1231 ev = ppc7450_event_table;
1232 count = PMC_EVENT_TABLE_SIZE(ppc7450);
1234 case PMC_CLASS_PPC970:
1235 ev = ppc970_event_table;
1236 count = PMC_EVENT_TABLE_SIZE(ppc970);
1238 case PMC_CLASS_E500:
1239 ev = e500_event_table;
1240 count = PMC_EVENT_TABLE_SIZE(e500);
1242 case PMC_CLASS_SOFT:
1243 ev = soft_event_table;
1244 count = soft_event_info.pm_nevent;
1251 if ((names = malloc(count * sizeof(const char *))) == NULL)
1254 *eventnames = names;
1257 for (;count--; ev++, names++)
1258 *names = ev->pm_ev_name;
1264 pmc_flush_logfile(void)
1266 return (PMC_CALL(FLUSHLOG,0));
1270 pmc_close_logfile(void)
1272 return (PMC_CALL(CLOSELOG,0));
1276 pmc_get_driver_stats(struct pmc_driverstats *ds)
1278 struct pmc_op_getdriverstats gms;
1280 if (PMC_CALL(GETDRIVERSTATS, &gms) < 0)
1283 /* copy out fields in the current userland<->library interface */
1284 ds->pm_intr_ignored = gms.pm_intr_ignored;
1285 ds->pm_intr_processed = gms.pm_intr_processed;
1286 ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
1287 ds->pm_syscalls = gms.pm_syscalls;
1288 ds->pm_syscall_errors = gms.pm_syscall_errors;
1289 ds->pm_buffer_requests = gms.pm_buffer_requests;
1290 ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
1291 ds->pm_log_sweeps = gms.pm_log_sweeps;
1296 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
1298 struct pmc_op_getmsr gm;
1301 if (PMC_CALL(PMCGETMSR, &gm) < 0)
1310 int error, pmc_mod_id;
1312 uint32_t abi_version;
1313 struct module_stat pmc_modstat;
1314 struct pmc_op_getcpuinfo op_cpu_info;
1316 if (pmc_syscall != -1) /* already inited */
1319 /* retrieve the system call number from the KLD */
1320 if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
1323 pmc_modstat.version = sizeof(struct module_stat);
1324 if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
1327 pmc_syscall = pmc_modstat.data.intval;
1329 /* check the kernel module's ABI against our compiled-in version */
1330 abi_version = PMC_VERSION;
1331 if (PMC_CALL(GETMODULEVERSION, &abi_version) < 0)
1332 return (pmc_syscall = -1);
1334 /* ignore patch & minor numbers for the comparison */
1335 if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
1336 errno = EPROGMISMATCH;
1337 return (pmc_syscall = -1);
1340 bzero(&op_cpu_info, sizeof(op_cpu_info));
1341 if (PMC_CALL(GETCPUINFO, &op_cpu_info) < 0)
1342 return (pmc_syscall = -1);
1344 cpu_info.pm_cputype = op_cpu_info.pm_cputype;
1345 cpu_info.pm_ncpu = op_cpu_info.pm_ncpu;
1346 cpu_info.pm_npmc = op_cpu_info.pm_npmc;
1347 cpu_info.pm_nclass = op_cpu_info.pm_nclass;
1348 for (n = 0; n < op_cpu_info.pm_nclass; n++)
1349 memcpy(&cpu_info.pm_classes[n], &op_cpu_info.pm_classes[n],
1350 sizeof(cpu_info.pm_classes[n]));
1352 pmc_class_table = malloc(PMC_CLASS_TABLE_SIZE *
1353 sizeof(struct pmc_class_descr *));
1355 if (pmc_class_table == NULL)
1358 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++)
1359 pmc_class_table[n] = NULL;
1362 * Get soft events list.
1364 soft_event_info.pm_class = PMC_CLASS_SOFT;
1365 if (PMC_CALL(GETDYNEVENTINFO, &soft_event_info) < 0)
1366 return (pmc_syscall = -1);
1368 /* Map soft events to static list. */
1369 for (n = 0; n < soft_event_info.pm_nevent; n++) {
1370 soft_event_table[n].pm_ev_name =
1371 soft_event_info.pm_events[n].pm_ev_name;
1372 soft_event_table[n].pm_ev_code =
1373 soft_event_info.pm_events[n].pm_ev_code;
1375 soft_class_table_descr.pm_evc_event_table_size = \
1376 soft_event_info.pm_nevent;
1377 soft_class_table_descr.pm_evc_event_table = \
1381 * Fill in the class table.
1385 /* Fill soft events information. */
1386 pmc_class_table[n++] = &soft_class_table_descr;
1387 #if defined(__amd64__) || defined(__i386__)
1388 if (cpu_info.pm_cputype != PMC_CPU_GENERIC)
1389 pmc_class_table[n++] = &tsc_class_table_descr;
1392 #define PMC_MDEP_INIT(C) pmc_mdep_event_aliases = C##_aliases
1394 /* Configure the event name parser. */
1395 switch (cpu_info.pm_cputype) {
1396 #if defined(__amd64__) || defined(__i386__)
1397 case PMC_CPU_AMD_K8:
1399 pmc_class_table[n] = &k8_class_table_descr;
1402 case PMC_CPU_GENERIC:
1403 PMC_MDEP_INIT(generic);
1405 #if defined(__arm__)
1406 case PMC_CPU_ARMV7_CORTEX_A8:
1407 PMC_MDEP_INIT(cortex_a8);
1408 pmc_class_table[n] = &cortex_a8_class_table_descr;
1410 case PMC_CPU_ARMV7_CORTEX_A9:
1411 PMC_MDEP_INIT(cortex_a9);
1412 pmc_class_table[n] = &cortex_a9_class_table_descr;
1415 #if defined(__aarch64__)
1416 case PMC_CPU_ARMV8_CORTEX_A53:
1417 PMC_MDEP_INIT(cortex_a53);
1418 pmc_class_table[n] = &cortex_a53_class_table_descr;
1420 case PMC_CPU_ARMV8_CORTEX_A57:
1421 PMC_MDEP_INIT(cortex_a57);
1422 pmc_class_table[n] = &cortex_a57_class_table_descr;
1424 case PMC_CPU_ARMV8_CORTEX_A76:
1425 PMC_MDEP_INIT(cortex_a76);
1426 pmc_class_table[n] = &cortex_a76_class_table_descr;
1429 #if defined(__mips__)
1430 case PMC_CPU_MIPS_BERI:
1431 PMC_MDEP_INIT(beri);
1432 pmc_class_table[n] = &beri_class_table_descr;
1434 case PMC_CPU_MIPS_24K:
1435 PMC_MDEP_INIT(mips24k);
1436 pmc_class_table[n] = &mips24k_class_table_descr;
1438 case PMC_CPU_MIPS_74K:
1439 PMC_MDEP_INIT(mips74k);
1440 pmc_class_table[n] = &mips74k_class_table_descr;
1442 case PMC_CPU_MIPS_OCTEON:
1443 PMC_MDEP_INIT(octeon);
1444 pmc_class_table[n] = &octeon_class_table_descr;
1446 #endif /* __mips__ */
1447 #if defined(__powerpc__)
1448 case PMC_CPU_PPC_7450:
1449 PMC_MDEP_INIT(ppc7450);
1450 pmc_class_table[n] = &ppc7450_class_table_descr;
1452 case PMC_CPU_PPC_970:
1453 PMC_MDEP_INIT(ppc970);
1454 pmc_class_table[n] = &ppc970_class_table_descr;
1456 case PMC_CPU_PPC_E500:
1457 PMC_MDEP_INIT(e500);
1458 pmc_class_table[n] = &e500_class_table_descr;
1463 * Some kind of CPU this version of the library knows nothing
1464 * about. This shouldn't happen since the abi version check
1465 * should have caught this.
1467 #if defined(__amd64__) || defined(__i386__) || defined(__powerpc64__)
1471 return (pmc_syscall = -1);
1478 pmc_name_of_capability(enum pmc_caps cap)
1483 * 'cap' should have a single bit set and should be in
1486 if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
1487 cap > PMC_CAP_LAST) {
1493 return (pmc_capability_names[i - 1]);
1497 pmc_name_of_class(enum pmc_class pc)
1501 for (n = 0; n < PMC_TABLE_SIZE(pmc_class_names); n++)
1502 if (pc == pmc_class_names[n].pm_class)
1503 return (pmc_class_names[n].pm_name);
1510 pmc_name_of_cputype(enum pmc_cputype cp)
1514 for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
1515 if (cp == pmc_cputype_names[n].pm_cputype)
1516 return (pmc_cputype_names[n].pm_name);
1523 pmc_name_of_disposition(enum pmc_disp pd)
1525 if ((int) pd >= PMC_DISP_FIRST &&
1526 pd <= PMC_DISP_LAST)
1527 return (pmc_disposition_names[pd]);
1534 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
1536 const struct pmc_event_descr *ev, *evfence;
1538 ev = evfence = NULL;
1539 if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
1540 ev = k8_event_table;
1541 evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
1543 } else if (pe >= PMC_EV_ARMV7_FIRST && pe <= PMC_EV_ARMV7_LAST) {
1545 case PMC_CPU_ARMV7_CORTEX_A8:
1546 ev = cortex_a8_event_table;
1547 evfence = cortex_a8_event_table + PMC_EVENT_TABLE_SIZE(cortex_a8);
1549 case PMC_CPU_ARMV7_CORTEX_A9:
1550 ev = cortex_a9_event_table;
1551 evfence = cortex_a9_event_table + PMC_EVENT_TABLE_SIZE(cortex_a9);
1553 default: /* Unknown CPU type. */
1556 } else if (pe >= PMC_EV_ARMV8_FIRST && pe <= PMC_EV_ARMV8_LAST) {
1558 case PMC_CPU_ARMV8_CORTEX_A53:
1559 ev = cortex_a53_event_table;
1560 evfence = cortex_a53_event_table + PMC_EVENT_TABLE_SIZE(cortex_a53);
1562 case PMC_CPU_ARMV8_CORTEX_A57:
1563 ev = cortex_a57_event_table;
1564 evfence = cortex_a57_event_table + PMC_EVENT_TABLE_SIZE(cortex_a57);
1566 case PMC_CPU_ARMV8_CORTEX_A76:
1567 ev = cortex_a76_event_table;
1568 evfence = cortex_a76_event_table + PMC_EVENT_TABLE_SIZE(cortex_a76);
1570 default: /* Unknown CPU type. */
1573 } else if (pe >= PMC_EV_BERI_FIRST && pe <= PMC_EV_BERI_LAST) {
1574 ev = beri_event_table;
1575 evfence = beri_event_table + PMC_EVENT_TABLE_SIZE(beri);
1576 } else if (pe >= PMC_EV_MIPS24K_FIRST && pe <= PMC_EV_MIPS24K_LAST) {
1577 ev = mips24k_event_table;
1578 evfence = mips24k_event_table + PMC_EVENT_TABLE_SIZE(mips24k);
1579 } else if (pe >= PMC_EV_MIPS74K_FIRST && pe <= PMC_EV_MIPS74K_LAST) {
1580 ev = mips74k_event_table;
1581 evfence = mips74k_event_table + PMC_EVENT_TABLE_SIZE(mips74k);
1582 } else if (pe >= PMC_EV_OCTEON_FIRST && pe <= PMC_EV_OCTEON_LAST) {
1583 ev = octeon_event_table;
1584 evfence = octeon_event_table + PMC_EVENT_TABLE_SIZE(octeon);
1585 } else if (pe >= PMC_EV_PPC7450_FIRST && pe <= PMC_EV_PPC7450_LAST) {
1586 ev = ppc7450_event_table;
1587 evfence = ppc7450_event_table + PMC_EVENT_TABLE_SIZE(ppc7450);
1588 } else if (pe >= PMC_EV_PPC970_FIRST && pe <= PMC_EV_PPC970_LAST) {
1589 ev = ppc970_event_table;
1590 evfence = ppc970_event_table + PMC_EVENT_TABLE_SIZE(ppc970);
1591 } else if (pe >= PMC_EV_E500_FIRST && pe <= PMC_EV_E500_LAST) {
1592 ev = e500_event_table;
1593 evfence = e500_event_table + PMC_EVENT_TABLE_SIZE(e500);
1594 } else if (pe == PMC_EV_TSC_TSC) {
1595 ev = tsc_event_table;
1596 evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
1597 } else if ((int)pe >= PMC_EV_SOFT_FIRST && (int)pe <= PMC_EV_SOFT_LAST) {
1598 ev = soft_event_table;
1599 evfence = soft_event_table + soft_event_info.pm_nevent;
1602 for (; ev != evfence; ev++)
1603 if (pe == ev->pm_ev_code)
1604 return (ev->pm_ev_name);
1610 pmc_name_of_event(enum pmc_event pe)
1614 if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
1622 pmc_name_of_mode(enum pmc_mode pm)
1624 if ((int) pm >= PMC_MODE_FIRST &&
1625 pm <= PMC_MODE_LAST)
1626 return (pmc_mode_names[pm]);
1633 pmc_name_of_state(enum pmc_state ps)
1635 if ((int) ps >= PMC_STATE_FIRST &&
1636 ps <= PMC_STATE_LAST)
1637 return (pmc_state_names[ps]);
1646 if (pmc_syscall == -1) {
1651 return (cpu_info.pm_ncpu);
1657 if (pmc_syscall == -1) {
1662 if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
1667 return (cpu_info.pm_npmc);
1671 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
1674 struct pmc_op_getpmcinfo *pmci;
1676 if ((npmc = pmc_npmc(cpu)) < 0)
1679 nbytes = sizeof(struct pmc_op_getpmcinfo) +
1680 npmc * sizeof(struct pmc_info);
1682 if ((pmci = calloc(1, nbytes)) == NULL)
1687 if (PMC_CALL(GETPMCINFO, pmci) < 0) {
1692 /* kernel<->library, library<->userland interfaces are identical */
1693 *ppmci = (struct pmc_pmcinfo *) pmci;
1698 pmc_read(pmc_id_t pmc, pmc_value_t *value)
1700 struct pmc_op_pmcrw pmc_read_op;
1702 pmc_read_op.pm_pmcid = pmc;
1703 pmc_read_op.pm_flags = PMC_F_OLDVALUE;
1704 pmc_read_op.pm_value = -1;
1706 if (PMC_CALL(PMCRW, &pmc_read_op) < 0)
1709 *value = pmc_read_op.pm_value;
1714 pmc_release(pmc_id_t pmc)
1716 struct pmc_op_simple pmc_release_args;
1718 pmc_release_args.pm_pmcid = pmc;
1719 return (PMC_CALL(PMCRELEASE, &pmc_release_args));
1723 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
1725 struct pmc_op_pmcrw pmc_rw_op;
1727 pmc_rw_op.pm_pmcid = pmc;
1728 pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
1729 pmc_rw_op.pm_value = newvalue;
1731 if (PMC_CALL(PMCRW, &pmc_rw_op) < 0)
1734 *oldvaluep = pmc_rw_op.pm_value;
1739 pmc_set(pmc_id_t pmc, pmc_value_t value)
1741 struct pmc_op_pmcsetcount sc;
1744 sc.pm_count = value;
1746 if (PMC_CALL(PMCSETCOUNT, &sc) < 0)
1752 pmc_start(pmc_id_t pmc)
1754 struct pmc_op_simple pmc_start_args;
1756 pmc_start_args.pm_pmcid = pmc;
1757 return (PMC_CALL(PMCSTART, &pmc_start_args));
1761 pmc_stop(pmc_id_t pmc)
1763 struct pmc_op_simple pmc_stop_args;
1765 pmc_stop_args.pm_pmcid = pmc;
1766 return (PMC_CALL(PMCSTOP, &pmc_stop_args));
1770 pmc_width(pmc_id_t pmcid, uint32_t *width)
1775 cl = PMC_ID_TO_CLASS(pmcid);
1776 for (i = 0; i < cpu_info.pm_nclass; i++)
1777 if (cpu_info.pm_classes[i].pm_class == cl) {
1778 *width = cpu_info.pm_classes[i].pm_width;
1786 pmc_write(pmc_id_t pmc, pmc_value_t value)
1788 struct pmc_op_pmcrw pmc_write_op;
1790 pmc_write_op.pm_pmcid = pmc;
1791 pmc_write_op.pm_flags = PMC_F_NEWVALUE;
1792 pmc_write_op.pm_value = value;
1793 return (PMC_CALL(PMCRW, &pmc_write_op));
1797 pmc_writelog(uint32_t userdata)
1799 struct pmc_op_writelog wl;
1801 wl.pm_userdata = userdata;
1802 return (PMC_CALL(WRITELOG, &wl));