2 * Copyright (c) 2003-2008 Joseph Koshy
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/module.h>
33 #include <sys/syscall.h>
45 #include "libpmcinternal.h"
47 /* Function prototypes */
49 static int k7_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
50 struct pmc_op_pmcallocate *_pmc_config);
52 #if defined(__amd64__) || defined(__i386__)
53 static int iaf_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
54 struct pmc_op_pmcallocate *_pmc_config);
55 static int iap_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
56 struct pmc_op_pmcallocate *_pmc_config);
57 static int k8_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
58 struct pmc_op_pmcallocate *_pmc_config);
59 static int p4_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
60 struct pmc_op_pmcallocate *_pmc_config);
63 static int p5_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
64 struct pmc_op_pmcallocate *_pmc_config);
65 static int p6_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
66 struct pmc_op_pmcallocate *_pmc_config);
68 #if defined(__amd64__) || defined(__i386__)
69 static int tsc_allocate_pmc(enum pmc_event _pe, char *_ctrspec,
70 struct pmc_op_pmcallocate *_pmc_config);
73 #define PMC_CALL(cmd, params) \
74 syscall(pmc_syscall, PMC_OP_##cmd, (params))
77 * Event aliases provide a way for the user to ask for generic events
78 * like "cache-misses", or "instructions-retired". These aliases are
79 * mapped to the appropriate canonical event descriptions using a
82 struct pmc_event_alias {
87 static const struct pmc_event_alias *pmc_mdep_event_aliases;
90 * The pmc_event_descr structure maps symbolic names known to the user
91 * to integer codes used by the PMC KLD.
93 struct pmc_event_descr {
94 const char *pm_ev_name;
95 enum pmc_event pm_ev_code;
99 * The pmc_class_descr structure maps class name prefixes for
100 * event names to event tables and other PMC class data.
102 struct pmc_class_descr {
103 const char *pm_evc_name;
104 size_t pm_evc_name_size;
105 enum pmc_class pm_evc_class;
106 const struct pmc_event_descr *pm_evc_event_table;
107 size_t pm_evc_event_table_size;
108 int (*pm_evc_allocate_pmc)(enum pmc_event _pe,
109 char *_ctrspec, struct pmc_op_pmcallocate *_pa);
112 #define PMC_TABLE_SIZE(N) (sizeof(N)/sizeof(N[0]))
113 #define PMC_EVENT_TABLE_SIZE(N) PMC_TABLE_SIZE(N##_event_table)
116 #define __PMC_EV(C,N) { #N, PMC_EV_ ## C ## _ ## N },
119 * PMC_CLASSDEP_TABLE(NAME, CLASS)
121 * Define a table mapping event names and aliases to HWPMC event IDs.
123 #define PMC_CLASSDEP_TABLE(N, C) \
124 static const struct pmc_event_descr N##_event_table[] = \
129 PMC_CLASSDEP_TABLE(iaf, IAF);
130 PMC_CLASSDEP_TABLE(k7, K7);
131 PMC_CLASSDEP_TABLE(k8, K8);
132 PMC_CLASSDEP_TABLE(p4, P4);
133 PMC_CLASSDEP_TABLE(p5, P5);
134 PMC_CLASSDEP_TABLE(p6, P6);
136 #undef __PMC_EV_ALIAS
137 #define __PMC_EV_ALIAS(N,CODE) { N, PMC_EV_##CODE },
139 static const struct pmc_event_descr atom_event_table[] =
141 __PMC_EV_ALIAS_ATOM()
144 static const struct pmc_event_descr core_event_table[] =
146 __PMC_EV_ALIAS_CORE()
150 static const struct pmc_event_descr core2_event_table[] =
152 __PMC_EV_ALIAS_CORE2()
155 static const struct pmc_event_descr corei7_event_table[] =
157 __PMC_EV_ALIAS_COREI7()
161 * PMC_MDEP_TABLE(NAME, PRIMARYCLASS, ADDITIONAL_CLASSES...)
163 * Map a CPU to the PMC classes it supports.
165 #define PMC_MDEP_TABLE(N,C,...) \
166 static const enum pmc_class N##_pmc_classes[] = { \
167 PMC_CLASS_##C, __VA_ARGS__ \
170 PMC_MDEP_TABLE(atom, IAP, PMC_CLASS_IAF, PMC_CLASS_TSC);
171 PMC_MDEP_TABLE(core, IAP, PMC_CLASS_TSC);
172 PMC_MDEP_TABLE(core2, IAP, PMC_CLASS_IAF, PMC_CLASS_TSC);
173 PMC_MDEP_TABLE(corei7, IAP, PMC_CLASS_IAF, PMC_CLASS_TSC);
174 PMC_MDEP_TABLE(k7, K7, PMC_CLASS_TSC);
175 PMC_MDEP_TABLE(k8, K8, PMC_CLASS_TSC);
176 PMC_MDEP_TABLE(p4, P4, PMC_CLASS_TSC);
177 PMC_MDEP_TABLE(p5, P5, PMC_CLASS_TSC);
178 PMC_MDEP_TABLE(p6, P6, PMC_CLASS_TSC);
180 static const struct pmc_event_descr tsc_event_table[] =
185 #undef PMC_CLASS_TABLE_DESC
186 #define PMC_CLASS_TABLE_DESC(NAME, CLASS, EVENTS, ALLOCATOR) \
187 static const struct pmc_class_descr NAME##_class_table_descr = \
189 .pm_evc_name = #CLASS "-", \
190 .pm_evc_name_size = sizeof(#CLASS "-") - 1, \
191 .pm_evc_class = PMC_CLASS_##CLASS , \
192 .pm_evc_event_table = EVENTS##_event_table , \
193 .pm_evc_event_table_size = \
194 PMC_EVENT_TABLE_SIZE(EVENTS), \
195 .pm_evc_allocate_pmc = ALLOCATOR##_allocate_pmc \
198 #if defined(__i386__) || defined(__amd64__)
199 PMC_CLASS_TABLE_DESC(iaf, IAF, iaf, iaf);
200 PMC_CLASS_TABLE_DESC(atom, IAP, atom, iap);
201 PMC_CLASS_TABLE_DESC(core, IAP, core, iap);
202 PMC_CLASS_TABLE_DESC(core2, IAP, core2, iap);
203 PMC_CLASS_TABLE_DESC(corei7, IAP, corei7, iap);
205 #if defined(__i386__)
206 PMC_CLASS_TABLE_DESC(k7, K7, k7, k7);
208 #if defined(__i386__) || defined(__amd64__)
209 PMC_CLASS_TABLE_DESC(k8, K8, k8, k8);
210 PMC_CLASS_TABLE_DESC(p4, P4, p4, p4);
212 #if defined(__i386__)
213 PMC_CLASS_TABLE_DESC(p5, P5, p5, p5);
214 PMC_CLASS_TABLE_DESC(p6, P6, p6, p6);
216 #if defined(__i386__) || defined(__amd64__)
217 PMC_CLASS_TABLE_DESC(tsc, TSC, tsc, tsc);
220 #undef PMC_CLASS_TABLE_DESC
222 static const struct pmc_class_descr **pmc_class_table;
223 #define PMC_CLASS_TABLE_SIZE cpu_info.pm_nclass
225 static const enum pmc_class *pmc_mdep_class_list;
226 static size_t pmc_mdep_class_list_size;
229 * Mapping tables, mapping enumeration values to human readable
233 static const char * pmc_capability_names[] = {
235 #define __PMC_CAP(N,V,D) #N ,
239 static const char * pmc_class_names[] = {
241 #define __PMC_CLASS(C) #C ,
245 struct pmc_cputype_map {
246 enum pmc_class pm_cputype;
250 static const struct pmc_cputype_map pmc_cputype_names[] = {
252 #define __PMC_CPU(S, V, D) { .pm_cputype = PMC_CPU_##S, .pm_name = #S } ,
256 static const char * pmc_disposition_names[] = {
258 #define __PMC_DISP(D) #D ,
262 static const char * pmc_mode_names[] = {
264 #define __PMC_MODE(M,N) #M ,
268 static const char * pmc_state_names[] = {
270 #define __PMC_STATE(S) #S ,
274 static int pmc_syscall = -1; /* filled in by pmc_init() */
276 static struct pmc_cpuinfo cpu_info; /* filled in by pmc_init() */
278 /* Event masks for events */
281 const uint32_t pm_value;
283 #define PMCMASK(N,V) { .pm_name = #N, .pm_value = (V) }
284 #define NULLMASK PMCMASK(NULL,0)
286 #if defined(__amd64__) || defined(__i386__)
288 pmc_parse_mask(const struct pmc_masks *pmask, char *p, uint32_t *evmask)
290 const struct pmc_masks *pm;
294 if (pmask == NULL) /* no mask keywords */
296 q = strchr(p, '='); /* skip '=' */
297 if (*++q == '\0') /* no more data */
299 c = 0; /* count of mask keywords seen */
300 while ((r = strsep(&q, "+")) != NULL) {
301 for (pm = pmask; pm->pm_name && strcasecmp(r, pm->pm_name);
304 if (pm->pm_name == NULL) /* not found */
306 *evmask |= pm->pm_value;
313 #define KWMATCH(p,kw) (strcasecmp((p), (kw)) == 0)
314 #define KWPREFIXMATCH(p,kw) (strncasecmp((p), (kw), sizeof((kw)) - 1) == 0)
315 #define EV_ALIAS(N,S) { .pm_alias = N, .pm_spec = S }
317 #if defined(__i386__)
320 * AMD K7 (Athlon) CPUs.
323 static struct pmc_event_alias k7_aliases[] = {
324 EV_ALIAS("branches", "k7-retired-branches"),
325 EV_ALIAS("branch-mispredicts", "k7-retired-branches-mispredicted"),
326 EV_ALIAS("cycles", "tsc"),
327 EV_ALIAS("dc-misses", "k7-dc-misses"),
328 EV_ALIAS("ic-misses", "k7-ic-misses"),
329 EV_ALIAS("instructions", "k7-retired-instructions"),
330 EV_ALIAS("interrupts", "k7-hardware-interrupts"),
334 #define K7_KW_COUNT "count"
335 #define K7_KW_EDGE "edge"
336 #define K7_KW_INV "inv"
337 #define K7_KW_OS "os"
338 #define K7_KW_UNITMASK "unitmask"
339 #define K7_KW_USR "usr"
342 k7_allocate_pmc(enum pmc_event pe, char *ctrspec,
343 struct pmc_op_pmcallocate *pmc_config)
347 uint32_t count, unitmask;
349 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
350 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
352 if (pe == PMC_EV_K7_DC_REFILLS_FROM_L2 ||
353 pe == PMC_EV_K7_DC_REFILLS_FROM_SYSTEM ||
354 pe == PMC_EV_K7_DC_WRITEBACKS) {
356 unitmask = AMD_PMC_UNITMASK_MOESI;
358 unitmask = has_unitmask = 0;
360 while ((p = strsep(&ctrspec, ",")) != NULL) {
361 if (KWPREFIXMATCH(p, K7_KW_COUNT "=")) {
363 if (*++q == '\0') /* skip '=' */
366 count = strtol(q, &e, 0);
367 if (e == q || *e != '\0')
370 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
371 pmc_config->pm_md.pm_amd.pm_amd_config |=
372 AMD_PMC_TO_COUNTER(count);
374 } else if (KWMATCH(p, K7_KW_EDGE)) {
375 pmc_config->pm_caps |= PMC_CAP_EDGE;
376 } else if (KWMATCH(p, K7_KW_INV)) {
377 pmc_config->pm_caps |= PMC_CAP_INVERT;
378 } else if (KWMATCH(p, K7_KW_OS)) {
379 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
380 } else if (KWPREFIXMATCH(p, K7_KW_UNITMASK "=")) {
381 if (has_unitmask == 0)
385 if (*++q == '\0') /* skip '=' */
388 while ((c = tolower(*q++)) != 0)
390 unitmask |= AMD_PMC_UNITMASK_M;
392 unitmask |= AMD_PMC_UNITMASK_O;
394 unitmask |= AMD_PMC_UNITMASK_E;
396 unitmask |= AMD_PMC_UNITMASK_S;
398 unitmask |= AMD_PMC_UNITMASK_I;
407 } else if (KWMATCH(p, K7_KW_USR)) {
408 pmc_config->pm_caps |= PMC_CAP_USER;
414 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
415 pmc_config->pm_md.pm_amd.pm_amd_config |=
416 AMD_PMC_TO_UNITMASK(unitmask);
425 #if defined(__amd64__) || defined(__i386__)
428 * Intel Core (Family 6, Model E) PMCs.
431 static struct pmc_event_alias core_aliases[] = {
432 EV_ALIAS("branches", "iap-br-instr-ret"),
433 EV_ALIAS("branch-mispredicts", "iap-br-mispred-ret"),
434 EV_ALIAS("cycles", "tsc-tsc"),
435 EV_ALIAS("ic-misses", "iap-icache-misses"),
436 EV_ALIAS("instructions", "iap-instr-ret"),
437 EV_ALIAS("interrupts", "iap-core-hw-int-rx"),
438 EV_ALIAS("unhalted-cycles", "iap-unhalted-core-cycles"),
443 * Intel Core2 (Family 6, Model F), Core2Extreme (Family 6, Model 17H)
444 * and Atom (Family 6, model 1CH) PMCs.
446 * We map aliases to events on the fixed-function counters if these
447 * are present. Note that not all CPUs in this family contain fixed-function
451 static struct pmc_event_alias core2_aliases[] = {
452 EV_ALIAS("branches", "iap-br-inst-retired.any"),
453 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
454 EV_ALIAS("cycles", "tsc-tsc"),
455 EV_ALIAS("ic-misses", "iap-l1i-misses"),
456 EV_ALIAS("instructions", "iaf-instr-retired.any"),
457 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
458 EV_ALIAS("unhalted-cycles", "iaf-cpu-clk-unhalted.core"),
462 static struct pmc_event_alias core2_aliases_without_iaf[] = {
463 EV_ALIAS("branches", "iap-br-inst-retired.any"),
464 EV_ALIAS("branch-mispredicts", "iap-br-inst-retired.mispred"),
465 EV_ALIAS("cycles", "tsc-tsc"),
466 EV_ALIAS("ic-misses", "iap-l1i-misses"),
467 EV_ALIAS("instructions", "iap-inst-retired.any_p"),
468 EV_ALIAS("interrupts", "iap-hw-int-rcv"),
469 EV_ALIAS("unhalted-cycles", "iap-cpu-clk-unhalted.core_p"),
473 #define atom_aliases core2_aliases
474 #define atom_aliases_without_iaf core2_aliases_without_iaf
475 #define corei7_aliases core2_aliases
476 #define corei7_aliases_without_iaf core2_aliases_without_iaf
478 #define IAF_KW_OS "os"
479 #define IAF_KW_USR "usr"
480 #define IAF_KW_ANYTHREAD "anythread"
483 * Parse an event specifier for Intel fixed function counters.
486 iaf_allocate_pmc(enum pmc_event pe, char *ctrspec,
487 struct pmc_op_pmcallocate *pmc_config)
493 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
494 pmc_config->pm_md.pm_iaf.pm_iaf_flags = 0;
496 while ((p = strsep(&ctrspec, ",")) != NULL) {
497 if (KWMATCH(p, IAF_KW_OS))
498 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
499 else if (KWMATCH(p, IAF_KW_USR))
500 pmc_config->pm_caps |= PMC_CAP_USER;
501 else if (KWMATCH(p, IAF_KW_ANYTHREAD))
502 pmc_config->pm_md.pm_iaf.pm_iaf_flags |= IAF_ANY;
511 * Core/Core2 support.
514 #define IAP_KW_AGENT "agent"
515 #define IAP_KW_ANYTHREAD "anythread"
516 #define IAP_KW_CACHESTATE "cachestate"
517 #define IAP_KW_CMASK "cmask"
518 #define IAP_KW_CORE "core"
519 #define IAP_KW_EDGE "edge"
520 #define IAP_KW_INV "inv"
521 #define IAP_KW_OS "os"
522 #define IAP_KW_PREFETCH "prefetch"
523 #define IAP_KW_SNOOPRESPONSE "snoopresponse"
524 #define IAP_KW_SNOOPTYPE "snooptype"
525 #define IAP_KW_TRANSITION "trans"
526 #define IAP_KW_USR "usr"
528 static struct pmc_masks iap_core_mask[] = {
529 PMCMASK(all, (0x3 << 14)),
530 PMCMASK(this, (0x1 << 14)),
534 static struct pmc_masks iap_agent_mask[] = {
536 PMCMASK(any, (0x1 << 13)),
540 static struct pmc_masks iap_prefetch_mask[] = {
541 PMCMASK(both, (0x3 << 12)),
542 PMCMASK(only, (0x1 << 12)),
547 static struct pmc_masks iap_cachestate_mask[] = {
548 PMCMASK(i, (1 << 8)),
549 PMCMASK(s, (1 << 9)),
550 PMCMASK(e, (1 << 10)),
551 PMCMASK(m, (1 << 11)),
555 static struct pmc_masks iap_snoopresponse_mask[] = {
556 PMCMASK(clean, (1 << 8)),
557 PMCMASK(hit, (1 << 9)),
558 PMCMASK(hitm, (1 << 11)),
562 static struct pmc_masks iap_snooptype_mask[] = {
563 PMCMASK(cmp2s, (1 << 8)),
564 PMCMASK(cmp2i, (1 << 9)),
568 static struct pmc_masks iap_transition_mask[] = {
570 PMCMASK(frequency, 0x10),
575 iap_allocate_pmc(enum pmc_event pe, char *ctrspec,
576 struct pmc_op_pmcallocate *pmc_config)
579 uint32_t cachestate, evmask;
582 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE |
584 pmc_config->pm_md.pm_iap.pm_iap_config = 0;
586 cachestate = evmask = 0;
588 /* Parse additional modifiers if present */
589 while ((p = strsep(&ctrspec, ",")) != NULL) {
592 if (KWPREFIXMATCH(p, IAP_KW_CMASK "=")) {
594 if (*++q == '\0') /* skip '=' */
596 count = strtol(q, &e, 0);
597 if (e == q || *e != '\0')
599 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
600 pmc_config->pm_md.pm_iap.pm_iap_config |=
602 } else if (KWMATCH(p, IAP_KW_EDGE)) {
603 pmc_config->pm_caps |= PMC_CAP_EDGE;
604 } else if (KWMATCH(p, IAP_KW_INV)) {
605 pmc_config->pm_caps |= PMC_CAP_INVERT;
606 } else if (KWMATCH(p, IAP_KW_OS)) {
607 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
608 } else if (KWMATCH(p, IAP_KW_USR)) {
609 pmc_config->pm_caps |= PMC_CAP_USER;
610 } else if (KWMATCH(p, IAP_KW_ANYTHREAD)) {
611 pmc_config->pm_md.pm_iap.pm_iap_config |= IAP_ANY;
612 } else if (KWPREFIXMATCH(p, IAP_KW_CORE "=")) {
613 n = pmc_parse_mask(iap_core_mask, p, &evmask);
616 } else if (KWPREFIXMATCH(p, IAP_KW_AGENT "=")) {
617 n = pmc_parse_mask(iap_agent_mask, p, &evmask);
620 } else if (KWPREFIXMATCH(p, IAP_KW_PREFETCH "=")) {
621 n = pmc_parse_mask(iap_prefetch_mask, p, &evmask);
624 } else if (KWPREFIXMATCH(p, IAP_KW_CACHESTATE "=")) {
625 n = pmc_parse_mask(iap_cachestate_mask, p, &cachestate);
626 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_CORE &&
627 KWPREFIXMATCH(p, IAP_KW_TRANSITION "=")) {
628 n = pmc_parse_mask(iap_transition_mask, p, &evmask);
631 } else if (cpu_info.pm_cputype == PMC_CPU_INTEL_ATOM ||
632 cpu_info.pm_cputype == PMC_CPU_INTEL_CORE2 ||
633 cpu_info.pm_cputype == PMC_CPU_INTEL_CORE2EXTREME ||
634 cpu_info.pm_cputype == PMC_CPU_INTEL_COREI7) {
635 if (KWPREFIXMATCH(p, IAP_KW_SNOOPRESPONSE "=")) {
636 n = pmc_parse_mask(iap_snoopresponse_mask, p,
638 } else if (KWPREFIXMATCH(p, IAP_KW_SNOOPTYPE "=")) {
639 n = pmc_parse_mask(iap_snooptype_mask, p,
646 if (n < 0) /* Parsing failed. */
650 pmc_config->pm_md.pm_iap.pm_iap_config |= evmask;
653 * If the event requires a 'cachestate' qualifier but was not
654 * specified by the user, use a sensible default.
657 case PMC_EV_IAP_EVENT_28H: /* Core, Core2, Atom */
658 case PMC_EV_IAP_EVENT_29H: /* Core, Core2, Atom */
659 case PMC_EV_IAP_EVENT_2AH: /* Core, Core2, Atom */
660 case PMC_EV_IAP_EVENT_2BH: /* Atom, Core2 */
661 case PMC_EV_IAP_EVENT_2EH: /* Core, Core2, Atom */
662 case PMC_EV_IAP_EVENT_30H: /* Core, Core2, Atom */
663 case PMC_EV_IAP_EVENT_32H: /* Core */
664 case PMC_EV_IAP_EVENT_40H: /* Core */
665 case PMC_EV_IAP_EVENT_41H: /* Core */
666 case PMC_EV_IAP_EVENT_42H: /* Core, Core2, Atom */
667 case PMC_EV_IAP_EVENT_77H: /* Core */
669 cachestate = (0xF << 8);
674 pmc_config->pm_md.pm_iap.pm_iap_config |= cachestate;
682 * These are very similar to AMD K7 PMCs, but support more kinds of
686 static struct pmc_event_alias k8_aliases[] = {
687 EV_ALIAS("branches", "k8-fr-retired-taken-branches"),
688 EV_ALIAS("branch-mispredicts",
689 "k8-fr-retired-taken-branches-mispredicted"),
690 EV_ALIAS("cycles", "tsc"),
691 EV_ALIAS("dc-misses", "k8-dc-miss"),
692 EV_ALIAS("ic-misses", "k8-ic-miss"),
693 EV_ALIAS("instructions", "k8-fr-retired-x86-instructions"),
694 EV_ALIAS("interrupts", "k8-fr-taken-hardware-interrupts"),
695 EV_ALIAS("unhalted-cycles", "k8-bu-cpu-clk-unhalted"),
699 #define __K8MASK(N,V) PMCMASK(N,(1 << (V)))
705 /* fp dispatched fpu ops */
706 static const struct pmc_masks k8_mask_fdfo[] = {
707 __K8MASK(add-pipe-excluding-junk-ops, 0),
708 __K8MASK(multiply-pipe-excluding-junk-ops, 1),
709 __K8MASK(store-pipe-excluding-junk-ops, 2),
710 __K8MASK(add-pipe-junk-ops, 3),
711 __K8MASK(multiply-pipe-junk-ops, 4),
712 __K8MASK(store-pipe-junk-ops, 5),
716 /* ls segment register loads */
717 static const struct pmc_masks k8_mask_lsrl[] = {
728 /* ls locked operation */
729 static const struct pmc_masks k8_mask_llo[] = {
730 __K8MASK(locked-instructions, 0),
731 __K8MASK(cycles-in-request, 1),
732 __K8MASK(cycles-to-complete, 2),
736 /* dc refill from {l2,system} and dc copyback */
737 static const struct pmc_masks k8_mask_dc[] = {
738 __K8MASK(invalid, 0),
740 __K8MASK(exclusive, 2),
742 __K8MASK(modified, 4),
746 /* dc one bit ecc error */
747 static const struct pmc_masks k8_mask_dobee[] = {
748 __K8MASK(scrubber, 0),
749 __K8MASK(piggyback, 1),
753 /* dc dispatched prefetch instructions */
754 static const struct pmc_masks k8_mask_ddpi[] = {
761 /* dc dcache accesses by locks */
762 static const struct pmc_masks k8_mask_dabl[] = {
763 __K8MASK(accesses, 0),
768 /* bu internal l2 request */
769 static const struct pmc_masks k8_mask_bilr[] = {
770 __K8MASK(ic-fill, 0),
771 __K8MASK(dc-fill, 1),
772 __K8MASK(tlb-reload, 2),
773 __K8MASK(tag-snoop, 3),
774 __K8MASK(cancelled, 4),
778 /* bu fill request l2 miss */
779 static const struct pmc_masks k8_mask_bfrlm[] = {
780 __K8MASK(ic-fill, 0),
781 __K8MASK(dc-fill, 1),
782 __K8MASK(tlb-reload, 2),
786 /* bu fill into l2 */
787 static const struct pmc_masks k8_mask_bfil[] = {
788 __K8MASK(dirty-l2-victim, 0),
789 __K8MASK(victim-from-l2, 1),
793 /* fr retired fpu instructions */
794 static const struct pmc_masks k8_mask_frfi[] = {
796 __K8MASK(mmx-3dnow, 1),
797 __K8MASK(packed-sse-sse2, 2),
798 __K8MASK(scalar-sse-sse2, 3),
802 /* fr retired fastpath double op instructions */
803 static const struct pmc_masks k8_mask_frfdoi[] = {
804 __K8MASK(low-op-pos-0, 0),
805 __K8MASK(low-op-pos-1, 1),
806 __K8MASK(low-op-pos-2, 2),
810 /* fr fpu exceptions */
811 static const struct pmc_masks k8_mask_ffe[] = {
812 __K8MASK(x87-reclass-microfaults, 0),
813 __K8MASK(sse-retype-microfaults, 1),
814 __K8MASK(sse-reclass-microfaults, 2),
815 __K8MASK(sse-and-x87-microtraps, 3),
819 /* nb memory controller page access event */
820 static const struct pmc_masks k8_mask_nmcpae[] = {
821 __K8MASK(page-hit, 0),
822 __K8MASK(page-miss, 1),
823 __K8MASK(page-conflict, 2),
827 /* nb memory controller turnaround */
828 static const struct pmc_masks k8_mask_nmct[] = {
829 __K8MASK(dimm-turnaround, 0),
830 __K8MASK(read-to-write-turnaround, 1),
831 __K8MASK(write-to-read-turnaround, 2),
835 /* nb memory controller bypass saturation */
836 static const struct pmc_masks k8_mask_nmcbs[] = {
837 __K8MASK(memory-controller-hi-pri-bypass, 0),
838 __K8MASK(memory-controller-lo-pri-bypass, 1),
839 __K8MASK(dram-controller-interface-bypass, 2),
840 __K8MASK(dram-controller-queue-bypass, 3),
844 /* nb sized commands */
845 static const struct pmc_masks k8_mask_nsc[] = {
846 __K8MASK(nonpostwrszbyte, 0),
847 __K8MASK(nonpostwrszdword, 1),
848 __K8MASK(postwrszbyte, 2),
849 __K8MASK(postwrszdword, 3),
850 __K8MASK(rdszbyte, 4),
851 __K8MASK(rdszdword, 5),
852 __K8MASK(rdmodwr, 6),
856 /* nb probe result */
857 static const struct pmc_masks k8_mask_npr[] = {
858 __K8MASK(probe-miss, 0),
859 __K8MASK(probe-hit, 1),
860 __K8MASK(probe-hit-dirty-no-memory-cancel, 2),
861 __K8MASK(probe-hit-dirty-with-memory-cancel, 3),
865 /* nb hypertransport bus bandwidth */
866 static const struct pmc_masks k8_mask_nhbb[] = { /* HT bus bandwidth */
867 __K8MASK(command, 0),
869 __K8MASK(buffer-release, 2),
876 #define K8_KW_COUNT "count"
877 #define K8_KW_EDGE "edge"
878 #define K8_KW_INV "inv"
879 #define K8_KW_MASK "mask"
880 #define K8_KW_OS "os"
881 #define K8_KW_USR "usr"
884 k8_allocate_pmc(enum pmc_event pe, char *ctrspec,
885 struct pmc_op_pmcallocate *pmc_config)
889 uint32_t count, evmask;
890 const struct pmc_masks *pm, *pmask;
892 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
893 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
898 #define __K8SETMASK(M) pmask = k8_mask_##M
900 /* setup parsing tables */
902 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
905 case PMC_EV_K8_LS_SEGMENT_REGISTER_LOAD:
908 case PMC_EV_K8_LS_LOCKED_OPERATION:
911 case PMC_EV_K8_DC_REFILL_FROM_L2:
912 case PMC_EV_K8_DC_REFILL_FROM_SYSTEM:
913 case PMC_EV_K8_DC_COPYBACK:
916 case PMC_EV_K8_DC_ONE_BIT_ECC_ERROR:
919 case PMC_EV_K8_DC_DISPATCHED_PREFETCH_INSTRUCTIONS:
922 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
925 case PMC_EV_K8_BU_INTERNAL_L2_REQUEST:
928 case PMC_EV_K8_BU_FILL_REQUEST_L2_MISS:
931 case PMC_EV_K8_BU_FILL_INTO_L2:
934 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
937 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
940 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
943 case PMC_EV_K8_NB_MEMORY_CONTROLLER_PAGE_ACCESS_EVENT:
946 case PMC_EV_K8_NB_MEMORY_CONTROLLER_TURNAROUND:
949 case PMC_EV_K8_NB_MEMORY_CONTROLLER_BYPASS_SATURATION:
952 case PMC_EV_K8_NB_SIZED_COMMANDS:
955 case PMC_EV_K8_NB_PROBE_RESULT:
958 case PMC_EV_K8_NB_HT_BUS0_BANDWIDTH:
959 case PMC_EV_K8_NB_HT_BUS1_BANDWIDTH:
960 case PMC_EV_K8_NB_HT_BUS2_BANDWIDTH:
965 break; /* no options defined */
968 while ((p = strsep(&ctrspec, ",")) != NULL) {
969 if (KWPREFIXMATCH(p, K8_KW_COUNT "=")) {
971 if (*++q == '\0') /* skip '=' */
974 count = strtol(q, &e, 0);
975 if (e == q || *e != '\0')
978 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
979 pmc_config->pm_md.pm_amd.pm_amd_config |=
980 AMD_PMC_TO_COUNTER(count);
982 } else if (KWMATCH(p, K8_KW_EDGE)) {
983 pmc_config->pm_caps |= PMC_CAP_EDGE;
984 } else if (KWMATCH(p, K8_KW_INV)) {
985 pmc_config->pm_caps |= PMC_CAP_INVERT;
986 } else if (KWPREFIXMATCH(p, K8_KW_MASK "=")) {
987 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
989 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
990 } else if (KWMATCH(p, K8_KW_OS)) {
991 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
992 } else if (KWMATCH(p, K8_KW_USR)) {
993 pmc_config->pm_caps |= PMC_CAP_USER;
998 /* other post processing */
1000 case PMC_EV_K8_FP_DISPATCHED_FPU_OPS:
1001 case PMC_EV_K8_FP_CYCLES_WITH_NO_FPU_OPS_RETIRED:
1002 case PMC_EV_K8_FP_DISPATCHED_FPU_FAST_FLAG_OPS:
1003 case PMC_EV_K8_FR_RETIRED_FASTPATH_DOUBLE_OP_INSTRUCTIONS:
1004 case PMC_EV_K8_FR_RETIRED_FPU_INSTRUCTIONS:
1005 case PMC_EV_K8_FR_FPU_EXCEPTIONS:
1006 /* XXX only available in rev B and later */
1008 case PMC_EV_K8_DC_DCACHE_ACCESSES_BY_LOCKS:
1009 /* XXX only available in rev C and later */
1011 case PMC_EV_K8_LS_LOCKED_OPERATION:
1012 /* XXX CPU Rev A,B evmask is to be zero */
1013 if (evmask & (evmask - 1)) /* > 1 bit set */
1016 evmask = 0x01; /* Rev C and later: #instrs */
1017 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1021 if (evmask == 0 && pmask != NULL) {
1022 for (pm = pmask; pm->pm_name; pm++)
1023 evmask |= pm->pm_value;
1024 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1028 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
1029 pmc_config->pm_md.pm_amd.pm_amd_config =
1030 AMD_PMC_TO_UNITMASK(evmask);
1037 #if defined(__amd64__) || defined(__i386__)
1043 static struct pmc_event_alias p4_aliases[] = {
1044 EV_ALIAS("branches", "p4-branch-retired,mask=mmtp+mmtm"),
1045 EV_ALIAS("branch-mispredicts", "p4-mispred-branch-retired"),
1046 EV_ALIAS("cycles", "tsc"),
1047 EV_ALIAS("instructions",
1048 "p4-instr-retired,mask=nbogusntag+nbogustag"),
1049 EV_ALIAS("unhalted-cycles", "p4-global-power-events"),
1050 EV_ALIAS(NULL, NULL)
1053 #define P4_KW_ACTIVE "active"
1054 #define P4_KW_ACTIVE_ANY "any"
1055 #define P4_KW_ACTIVE_BOTH "both"
1056 #define P4_KW_ACTIVE_NONE "none"
1057 #define P4_KW_ACTIVE_SINGLE "single"
1058 #define P4_KW_BUSREQTYPE "busreqtype"
1059 #define P4_KW_CASCADE "cascade"
1060 #define P4_KW_EDGE "edge"
1061 #define P4_KW_INV "complement"
1062 #define P4_KW_OS "os"
1063 #define P4_KW_MASK "mask"
1064 #define P4_KW_PRECISE "precise"
1065 #define P4_KW_TAG "tag"
1066 #define P4_KW_THRESHOLD "threshold"
1067 #define P4_KW_USR "usr"
1069 #define __P4MASK(N,V) PMCMASK(N, (1 << (V)))
1071 static const struct pmc_masks p4_mask_tcdm[] = { /* tc deliver mode */
1083 static const struct pmc_masks p4_mask_bfr[] = { /* bpu fetch request */
1084 __P4MASK(tcmiss, 0),
1088 static const struct pmc_masks p4_mask_ir[] = { /* itlb reference */
1091 __P4MASK(hit-uc, 2),
1095 static const struct pmc_masks p4_mask_memcan[] = { /* memory cancel */
1096 __P4MASK(st-rb-full, 2),
1097 __P4MASK(64k-conf, 3),
1101 static const struct pmc_masks p4_mask_memcomp[] = { /* memory complete */
1107 static const struct pmc_masks p4_mask_lpr[] = { /* load port replay */
1108 __P4MASK(split-ld, 1),
1112 static const struct pmc_masks p4_mask_spr[] = { /* store port replay */
1113 __P4MASK(split-st, 1),
1117 static const struct pmc_masks p4_mask_mlr[] = { /* mob load replay */
1118 __P4MASK(no-sta, 1),
1119 __P4MASK(no-std, 3),
1120 __P4MASK(partial-data, 4),
1121 __P4MASK(unalgn-addr, 5),
1125 static const struct pmc_masks p4_mask_pwt[] = { /* page walk type */
1126 __P4MASK(dtmiss, 0),
1127 __P4MASK(itmiss, 1),
1131 static const struct pmc_masks p4_mask_bcr[] = { /* bsq cache reference */
1132 __P4MASK(rd-2ndl-hits, 0),
1133 __P4MASK(rd-2ndl-hite, 1),
1134 __P4MASK(rd-2ndl-hitm, 2),
1135 __P4MASK(rd-3rdl-hits, 3),
1136 __P4MASK(rd-3rdl-hite, 4),
1137 __P4MASK(rd-3rdl-hitm, 5),
1138 __P4MASK(rd-2ndl-miss, 8),
1139 __P4MASK(rd-3rdl-miss, 9),
1140 __P4MASK(wr-2ndl-miss, 10),
1144 static const struct pmc_masks p4_mask_ia[] = { /* ioq allocation */
1145 __P4MASK(all-read, 5),
1146 __P4MASK(all-write, 6),
1147 __P4MASK(mem-uc, 7),
1148 __P4MASK(mem-wc, 8),
1149 __P4MASK(mem-wt, 9),
1150 __P4MASK(mem-wp, 10),
1151 __P4MASK(mem-wb, 11),
1153 __P4MASK(other, 14),
1154 __P4MASK(prefetch, 15),
1158 static const struct pmc_masks p4_mask_iae[] = { /* ioq active entries */
1159 __P4MASK(all-read, 5),
1160 __P4MASK(all-write, 6),
1161 __P4MASK(mem-uc, 7),
1162 __P4MASK(mem-wc, 8),
1163 __P4MASK(mem-wt, 9),
1164 __P4MASK(mem-wp, 10),
1165 __P4MASK(mem-wb, 11),
1167 __P4MASK(other, 14),
1168 __P4MASK(prefetch, 15),
1172 static const struct pmc_masks p4_mask_fda[] = { /* fsb data activity */
1173 __P4MASK(drdy-drv, 0),
1174 __P4MASK(drdy-own, 1),
1175 __P4MASK(drdy-other, 2),
1176 __P4MASK(dbsy-drv, 3),
1177 __P4MASK(dbsy-own, 4),
1178 __P4MASK(dbsy-other, 5),
1182 static const struct pmc_masks p4_mask_ba[] = { /* bsq allocation */
1183 __P4MASK(req-type0, 0),
1184 __P4MASK(req-type1, 1),
1185 __P4MASK(req-len0, 2),
1186 __P4MASK(req-len1, 3),
1187 __P4MASK(req-io-type, 5),
1188 __P4MASK(req-lock-type, 6),
1189 __P4MASK(req-cache-type, 7),
1190 __P4MASK(req-split-type, 8),
1191 __P4MASK(req-dem-type, 9),
1192 __P4MASK(req-ord-type, 10),
1193 __P4MASK(mem-type0, 11),
1194 __P4MASK(mem-type1, 12),
1195 __P4MASK(mem-type2, 13),
1199 static const struct pmc_masks p4_mask_sia[] = { /* sse input assist */
1204 static const struct pmc_masks p4_mask_psu[] = { /* packed sp uop */
1209 static const struct pmc_masks p4_mask_pdu[] = { /* packed dp uop */
1214 static const struct pmc_masks p4_mask_ssu[] = { /* scalar sp uop */
1219 static const struct pmc_masks p4_mask_sdu[] = { /* scalar dp uop */
1224 static const struct pmc_masks p4_mask_64bmu[] = { /* 64 bit mmx uop */
1229 static const struct pmc_masks p4_mask_128bmu[] = { /* 128 bit mmx uop */
1234 static const struct pmc_masks p4_mask_xfu[] = { /* X87 fp uop */
1239 static const struct pmc_masks p4_mask_xsmu[] = { /* x87 simd moves uop */
1245 static const struct pmc_masks p4_mask_gpe[] = { /* global power events */
1246 __P4MASK(running, 0),
1250 static const struct pmc_masks p4_mask_tmx[] = { /* TC ms xfer */
1255 static const struct pmc_masks p4_mask_uqw[] = { /* uop queue writes */
1256 __P4MASK(from-tc-build, 0),
1257 __P4MASK(from-tc-deliver, 1),
1258 __P4MASK(from-rom, 2),
1262 static const struct pmc_masks p4_mask_rmbt[] = {
1263 /* retired mispred branch type */
1264 __P4MASK(conditional, 1),
1266 __P4MASK(return, 3),
1267 __P4MASK(indirect, 4),
1271 static const struct pmc_masks p4_mask_rbt[] = { /* retired branch type */
1272 __P4MASK(conditional, 1),
1274 __P4MASK(retired, 3),
1275 __P4MASK(indirect, 4),
1279 static const struct pmc_masks p4_mask_rs[] = { /* resource stall */
1280 __P4MASK(sbfull, 5),
1284 static const struct pmc_masks p4_mask_wb[] = { /* WC buffer */
1285 __P4MASK(wcb-evicts, 0),
1286 __P4MASK(wcb-full-evict, 1),
1290 static const struct pmc_masks p4_mask_fee[] = { /* front end event */
1291 __P4MASK(nbogus, 0),
1296 static const struct pmc_masks p4_mask_ee[] = { /* execution event */
1297 __P4MASK(nbogus0, 0),
1298 __P4MASK(nbogus1, 1),
1299 __P4MASK(nbogus2, 2),
1300 __P4MASK(nbogus3, 3),
1301 __P4MASK(bogus0, 4),
1302 __P4MASK(bogus1, 5),
1303 __P4MASK(bogus2, 6),
1304 __P4MASK(bogus3, 7),
1308 static const struct pmc_masks p4_mask_re[] = { /* replay event */
1309 __P4MASK(nbogus, 0),
1314 static const struct pmc_masks p4_mask_insret[] = { /* instr retired */
1315 __P4MASK(nbogusntag, 0),
1316 __P4MASK(nbogustag, 1),
1317 __P4MASK(bogusntag, 2),
1318 __P4MASK(bogustag, 3),
1322 static const struct pmc_masks p4_mask_ur[] = { /* uops retired */
1323 __P4MASK(nbogus, 0),
1328 static const struct pmc_masks p4_mask_ut[] = { /* uop type */
1329 __P4MASK(tagloads, 1),
1330 __P4MASK(tagstores, 2),
1334 static const struct pmc_masks p4_mask_br[] = { /* branch retired */
1342 static const struct pmc_masks p4_mask_mbr[] = { /* mispred branch retired */
1343 __P4MASK(nbogus, 0),
1347 static const struct pmc_masks p4_mask_xa[] = { /* x87 assist */
1356 static const struct pmc_masks p4_mask_machclr[] = { /* machine clear */
1358 __P4MASK(moclear, 2),
1359 __P4MASK(smclear, 3),
1363 /* P4 event parser */
1365 p4_allocate_pmc(enum pmc_event pe, char *ctrspec,
1366 struct pmc_op_pmcallocate *pmc_config)
1370 int count, has_tag, has_busreqtype, n;
1371 uint32_t evmask, cccractivemask;
1372 const struct pmc_masks *pm, *pmask;
1374 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1375 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig =
1376 pmc_config->pm_md.pm_p4.pm_p4_escrconfig = 0;
1380 cccractivemask = 0x3;
1381 has_tag = has_busreqtype = 0;
1383 #define __P4SETMASK(M) do { \
1384 pmask = p4_mask_##M; \
1388 case PMC_EV_P4_TC_DELIVER_MODE:
1391 case PMC_EV_P4_BPU_FETCH_REQUEST:
1394 case PMC_EV_P4_ITLB_REFERENCE:
1397 case PMC_EV_P4_MEMORY_CANCEL:
1398 __P4SETMASK(memcan);
1400 case PMC_EV_P4_MEMORY_COMPLETE:
1401 __P4SETMASK(memcomp);
1403 case PMC_EV_P4_LOAD_PORT_REPLAY:
1406 case PMC_EV_P4_STORE_PORT_REPLAY:
1409 case PMC_EV_P4_MOB_LOAD_REPLAY:
1412 case PMC_EV_P4_PAGE_WALK_TYPE:
1415 case PMC_EV_P4_BSQ_CACHE_REFERENCE:
1418 case PMC_EV_P4_IOQ_ALLOCATION:
1422 case PMC_EV_P4_IOQ_ACTIVE_ENTRIES:
1426 case PMC_EV_P4_FSB_DATA_ACTIVITY:
1429 case PMC_EV_P4_BSQ_ALLOCATION:
1432 case PMC_EV_P4_SSE_INPUT_ASSIST:
1435 case PMC_EV_P4_PACKED_SP_UOP:
1438 case PMC_EV_P4_PACKED_DP_UOP:
1441 case PMC_EV_P4_SCALAR_SP_UOP:
1444 case PMC_EV_P4_SCALAR_DP_UOP:
1447 case PMC_EV_P4_64BIT_MMX_UOP:
1450 case PMC_EV_P4_128BIT_MMX_UOP:
1451 __P4SETMASK(128bmu);
1453 case PMC_EV_P4_X87_FP_UOP:
1456 case PMC_EV_P4_X87_SIMD_MOVES_UOP:
1459 case PMC_EV_P4_GLOBAL_POWER_EVENTS:
1462 case PMC_EV_P4_TC_MS_XFER:
1465 case PMC_EV_P4_UOP_QUEUE_WRITES:
1468 case PMC_EV_P4_RETIRED_MISPRED_BRANCH_TYPE:
1471 case PMC_EV_P4_RETIRED_BRANCH_TYPE:
1474 case PMC_EV_P4_RESOURCE_STALL:
1477 case PMC_EV_P4_WC_BUFFER:
1480 case PMC_EV_P4_BSQ_ACTIVE_ENTRIES:
1481 case PMC_EV_P4_B2B_CYCLES:
1483 case PMC_EV_P4_SNOOP:
1484 case PMC_EV_P4_RESPONSE:
1486 case PMC_EV_P4_FRONT_END_EVENT:
1489 case PMC_EV_P4_EXECUTION_EVENT:
1492 case PMC_EV_P4_REPLAY_EVENT:
1495 case PMC_EV_P4_INSTR_RETIRED:
1496 __P4SETMASK(insret);
1498 case PMC_EV_P4_UOPS_RETIRED:
1501 case PMC_EV_P4_UOP_TYPE:
1504 case PMC_EV_P4_BRANCH_RETIRED:
1507 case PMC_EV_P4_MISPRED_BRANCH_RETIRED:
1510 case PMC_EV_P4_X87_ASSIST:
1513 case PMC_EV_P4_MACHINE_CLEAR:
1514 __P4SETMASK(machclr);
1520 /* process additional flags */
1521 while ((p = strsep(&ctrspec, ",")) != NULL) {
1522 if (KWPREFIXMATCH(p, P4_KW_ACTIVE)) {
1524 if (*++q == '\0') /* skip '=' */
1527 if (strcasecmp(q, P4_KW_ACTIVE_NONE) == 0)
1528 cccractivemask = 0x0;
1529 else if (strcasecmp(q, P4_KW_ACTIVE_SINGLE) == 0)
1530 cccractivemask = 0x1;
1531 else if (strcasecmp(q, P4_KW_ACTIVE_BOTH) == 0)
1532 cccractivemask = 0x2;
1533 else if (strcasecmp(q, P4_KW_ACTIVE_ANY) == 0)
1534 cccractivemask = 0x3;
1538 } else if (KWPREFIXMATCH(p, P4_KW_BUSREQTYPE)) {
1539 if (has_busreqtype == 0)
1543 if (*++q == '\0') /* skip '=' */
1546 count = strtol(q, &e, 0);
1547 if (e == q || *e != '\0')
1549 evmask = (evmask & ~0x1F) | (count & 0x1F);
1550 } else if (KWMATCH(p, P4_KW_CASCADE))
1551 pmc_config->pm_caps |= PMC_CAP_CASCADE;
1552 else if (KWMATCH(p, P4_KW_EDGE))
1553 pmc_config->pm_caps |= PMC_CAP_EDGE;
1554 else if (KWMATCH(p, P4_KW_INV))
1555 pmc_config->pm_caps |= PMC_CAP_INVERT;
1556 else if (KWPREFIXMATCH(p, P4_KW_MASK "=")) {
1557 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
1559 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1560 } else if (KWMATCH(p, P4_KW_OS))
1561 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
1562 else if (KWMATCH(p, P4_KW_PRECISE))
1563 pmc_config->pm_caps |= PMC_CAP_PRECISE;
1564 else if (KWPREFIXMATCH(p, P4_KW_TAG "=")) {
1569 if (*++q == '\0') /* skip '=' */
1572 count = strtol(q, &e, 0);
1573 if (e == q || *e != '\0')
1576 pmc_config->pm_caps |= PMC_CAP_TAGGING;
1577 pmc_config->pm_md.pm_p4.pm_p4_escrconfig |=
1578 P4_ESCR_TO_TAG_VALUE(count);
1579 } else if (KWPREFIXMATCH(p, P4_KW_THRESHOLD "=")) {
1581 if (*++q == '\0') /* skip '=' */
1584 count = strtol(q, &e, 0);
1585 if (e == q || *e != '\0')
1588 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
1589 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig &=
1590 ~P4_CCCR_THRESHOLD_MASK;
1591 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig |=
1592 P4_CCCR_TO_THRESHOLD(count);
1593 } else if (KWMATCH(p, P4_KW_USR))
1594 pmc_config->pm_caps |= PMC_CAP_USER;
1599 /* other post processing */
1600 if (pe == PMC_EV_P4_IOQ_ALLOCATION ||
1601 pe == PMC_EV_P4_FSB_DATA_ACTIVITY ||
1602 pe == PMC_EV_P4_BSQ_ALLOCATION)
1603 pmc_config->pm_caps |= PMC_CAP_EDGE;
1605 /* fill in thread activity mask */
1606 pmc_config->pm_md.pm_p4.pm_p4_cccrconfig |=
1607 P4_CCCR_TO_ACTIVE_THREAD(cccractivemask);
1610 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1613 case PMC_EV_P4_FSB_DATA_ACTIVITY:
1614 if ((evmask & 0x06) == 0x06 ||
1615 (evmask & 0x18) == 0x18)
1616 return (-1); /* can't have own+other bits together */
1617 if (evmask == 0) /* default:drdy-{drv,own}+dbsy{drv,own} */
1620 case PMC_EV_P4_MACHINE_CLEAR:
1621 /* only one bit is allowed to be set */
1622 if ((evmask & (evmask - 1)) != 0)
1625 evmask = 0x1; /* 'CLEAR' */
1626 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1630 if (evmask == 0 && pmask) {
1631 for (pm = pmask; pm->pm_name; pm++)
1632 evmask |= pm->pm_value;
1633 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1637 pmc_config->pm_md.pm_p4.pm_p4_escrconfig =
1638 P4_ESCR_TO_EVENT_MASK(evmask);
1645 #if defined(__i386__)
1648 * Pentium style PMCs
1651 static struct pmc_event_alias p5_aliases[] = {
1652 EV_ALIAS("branches", "p5-taken-branches"),
1653 EV_ALIAS("cycles", "tsc"),
1654 EV_ALIAS("dc-misses", "p5-data-read-miss-or-write-miss"),
1655 EV_ALIAS("ic-misses", "p5-code-cache-miss"),
1656 EV_ALIAS("instructions", "p5-instructions-executed"),
1657 EV_ALIAS("interrupts", "p5-hardware-interrupts"),
1658 EV_ALIAS("unhalted-cycles",
1659 "p5-number-of-cycles-not-in-halt-state"),
1660 EV_ALIAS(NULL, NULL)
1664 p5_allocate_pmc(enum pmc_event pe, char *ctrspec,
1665 struct pmc_op_pmcallocate *pmc_config)
1667 return (-1 || pe || ctrspec || pmc_config); /* shut up gcc */
1671 * Pentium Pro style PMCs. These PMCs are found in Pentium II, Pentium III,
1672 * and Pentium M CPUs.
1675 static struct pmc_event_alias p6_aliases[] = {
1676 EV_ALIAS("branches", "p6-br-inst-retired"),
1677 EV_ALIAS("branch-mispredicts", "p6-br-miss-pred-retired"),
1678 EV_ALIAS("cycles", "tsc"),
1679 EV_ALIAS("dc-misses", "p6-dcu-lines-in"),
1680 EV_ALIAS("ic-misses", "p6-ifu-fetch-miss"),
1681 EV_ALIAS("instructions", "p6-inst-retired"),
1682 EV_ALIAS("interrupts", "p6-hw-int-rx"),
1683 EV_ALIAS("unhalted-cycles", "p6-cpu-clk-unhalted"),
1684 EV_ALIAS(NULL, NULL)
1687 #define P6_KW_CMASK "cmask"
1688 #define P6_KW_EDGE "edge"
1689 #define P6_KW_INV "inv"
1690 #define P6_KW_OS "os"
1691 #define P6_KW_UMASK "umask"
1692 #define P6_KW_USR "usr"
1694 static struct pmc_masks p6_mask_mesi[] = {
1702 static struct pmc_masks p6_mask_mesihw[] = {
1707 PMCMASK(nonhw, 0x00),
1709 PMCMASK(both, 0x30),
1713 static struct pmc_masks p6_mask_hw[] = {
1714 PMCMASK(nonhw, 0x00),
1716 PMCMASK(both, 0x30),
1720 static struct pmc_masks p6_mask_any[] = {
1721 PMCMASK(self, 0x00),
1726 static struct pmc_masks p6_mask_ekp[] = {
1734 static struct pmc_masks p6_mask_pps[] = {
1735 PMCMASK(packed-and-scalar, 0x00),
1736 PMCMASK(scalar, 0x01),
1740 static struct pmc_masks p6_mask_mite[] = {
1741 PMCMASK(packed-multiply, 0x01),
1742 PMCMASK(packed-shift, 0x02),
1743 PMCMASK(pack, 0x04),
1744 PMCMASK(unpack, 0x08),
1745 PMCMASK(packed-logical, 0x10),
1746 PMCMASK(packed-arithmetic, 0x20),
1750 static struct pmc_masks p6_mask_fmt[] = {
1751 PMCMASK(mmxtofp, 0x00),
1752 PMCMASK(fptommx, 0x01),
1756 static struct pmc_masks p6_mask_sr[] = {
1764 static struct pmc_masks p6_mask_eet[] = {
1766 PMCMASK(freq, 0x02),
1770 static struct pmc_masks p6_mask_efur[] = {
1772 PMCMASK(loadop, 0x01),
1773 PMCMASK(stdsta, 0x02),
1777 static struct pmc_masks p6_mask_essir[] = {
1778 PMCMASK(sse-packed-single, 0x00),
1779 PMCMASK(sse-packed-single-scalar-single, 0x01),
1780 PMCMASK(sse2-packed-double, 0x02),
1781 PMCMASK(sse2-scalar-double, 0x03),
1785 static struct pmc_masks p6_mask_esscir[] = {
1786 PMCMASK(sse-packed-single, 0x00),
1787 PMCMASK(sse-scalar-single, 0x01),
1788 PMCMASK(sse2-packed-double, 0x02),
1789 PMCMASK(sse2-scalar-double, 0x03),
1793 /* P6 event parser */
1795 p6_allocate_pmc(enum pmc_event pe, char *ctrspec,
1796 struct pmc_op_pmcallocate *pmc_config)
1801 const struct pmc_masks *pm, *pmask;
1803 pmc_config->pm_caps |= (PMC_CAP_READ | PMC_CAP_WRITE);
1804 pmc_config->pm_md.pm_ppro.pm_ppro_config = 0;
1808 #define P6MASKSET(M) pmask = p6_mask_ ## M
1811 case PMC_EV_P6_L2_IFETCH: P6MASKSET(mesi); break;
1812 case PMC_EV_P6_L2_LD: P6MASKSET(mesi); break;
1813 case PMC_EV_P6_L2_ST: P6MASKSET(mesi); break;
1814 case PMC_EV_P6_L2_RQSTS: P6MASKSET(mesi); break;
1815 case PMC_EV_P6_BUS_DRDY_CLOCKS:
1816 case PMC_EV_P6_BUS_LOCK_CLOCKS:
1817 case PMC_EV_P6_BUS_TRAN_BRD:
1818 case PMC_EV_P6_BUS_TRAN_RFO:
1819 case PMC_EV_P6_BUS_TRANS_WB:
1820 case PMC_EV_P6_BUS_TRAN_IFETCH:
1821 case PMC_EV_P6_BUS_TRAN_INVAL:
1822 case PMC_EV_P6_BUS_TRAN_PWR:
1823 case PMC_EV_P6_BUS_TRANS_P:
1824 case PMC_EV_P6_BUS_TRANS_IO:
1825 case PMC_EV_P6_BUS_TRAN_DEF:
1826 case PMC_EV_P6_BUS_TRAN_BURST:
1827 case PMC_EV_P6_BUS_TRAN_ANY:
1828 case PMC_EV_P6_BUS_TRAN_MEM:
1829 P6MASKSET(any); break;
1830 case PMC_EV_P6_EMON_KNI_PREF_DISPATCHED:
1831 case PMC_EV_P6_EMON_KNI_PREF_MISS:
1832 P6MASKSET(ekp); break;
1833 case PMC_EV_P6_EMON_KNI_INST_RETIRED:
1834 case PMC_EV_P6_EMON_KNI_COMP_INST_RET:
1835 P6MASKSET(pps); break;
1836 case PMC_EV_P6_MMX_INSTR_TYPE_EXEC:
1837 P6MASKSET(mite); break;
1838 case PMC_EV_P6_FP_MMX_TRANS:
1839 P6MASKSET(fmt); break;
1840 case PMC_EV_P6_SEG_RENAME_STALLS:
1841 case PMC_EV_P6_SEG_REG_RENAMES:
1842 P6MASKSET(sr); break;
1843 case PMC_EV_P6_EMON_EST_TRANS:
1844 P6MASKSET(eet); break;
1845 case PMC_EV_P6_EMON_FUSED_UOPS_RET:
1846 P6MASKSET(efur); break;
1847 case PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED:
1848 P6MASKSET(essir); break;
1849 case PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED:
1850 P6MASKSET(esscir); break;
1856 /* Pentium M PMCs have a few events with different semantics */
1857 if (cpu_info.pm_cputype == PMC_CPU_INTEL_PM) {
1858 if (pe == PMC_EV_P6_L2_LD ||
1859 pe == PMC_EV_P6_L2_LINES_IN ||
1860 pe == PMC_EV_P6_L2_LINES_OUT)
1862 else if (pe == PMC_EV_P6_L2_M_LINES_OUTM)
1866 /* Parse additional modifiers if present */
1867 while ((p = strsep(&ctrspec, ",")) != NULL) {
1868 if (KWPREFIXMATCH(p, P6_KW_CMASK "=")) {
1870 if (*++q == '\0') /* skip '=' */
1872 count = strtol(q, &e, 0);
1873 if (e == q || *e != '\0')
1875 pmc_config->pm_caps |= PMC_CAP_THRESHOLD;
1876 pmc_config->pm_md.pm_ppro.pm_ppro_config |=
1877 P6_EVSEL_TO_CMASK(count);
1878 } else if (KWMATCH(p, P6_KW_EDGE)) {
1879 pmc_config->pm_caps |= PMC_CAP_EDGE;
1880 } else if (KWMATCH(p, P6_KW_INV)) {
1881 pmc_config->pm_caps |= PMC_CAP_INVERT;
1882 } else if (KWMATCH(p, P6_KW_OS)) {
1883 pmc_config->pm_caps |= PMC_CAP_SYSTEM;
1884 } else if (KWPREFIXMATCH(p, P6_KW_UMASK "=")) {
1886 if ((n = pmc_parse_mask(pmask, p, &evmask)) < 0)
1888 if ((pe == PMC_EV_P6_BUS_DRDY_CLOCKS ||
1889 pe == PMC_EV_P6_BUS_LOCK_CLOCKS ||
1890 pe == PMC_EV_P6_BUS_TRAN_BRD ||
1891 pe == PMC_EV_P6_BUS_TRAN_RFO ||
1892 pe == PMC_EV_P6_BUS_TRAN_IFETCH ||
1893 pe == PMC_EV_P6_BUS_TRAN_INVAL ||
1894 pe == PMC_EV_P6_BUS_TRAN_PWR ||
1895 pe == PMC_EV_P6_BUS_TRAN_DEF ||
1896 pe == PMC_EV_P6_BUS_TRAN_BURST ||
1897 pe == PMC_EV_P6_BUS_TRAN_ANY ||
1898 pe == PMC_EV_P6_BUS_TRAN_MEM ||
1899 pe == PMC_EV_P6_BUS_TRANS_IO ||
1900 pe == PMC_EV_P6_BUS_TRANS_P ||
1901 pe == PMC_EV_P6_BUS_TRANS_WB ||
1902 pe == PMC_EV_P6_EMON_EST_TRANS ||
1903 pe == PMC_EV_P6_EMON_FUSED_UOPS_RET ||
1904 pe == PMC_EV_P6_EMON_KNI_COMP_INST_RET ||
1905 pe == PMC_EV_P6_EMON_KNI_INST_RETIRED ||
1906 pe == PMC_EV_P6_EMON_KNI_PREF_DISPATCHED ||
1907 pe == PMC_EV_P6_EMON_KNI_PREF_MISS ||
1908 pe == PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED ||
1909 pe == PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED ||
1910 pe == PMC_EV_P6_FP_MMX_TRANS)
1911 && (n > 1)) /* Only one mask keyword is allowed. */
1913 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1914 } else if (KWMATCH(p, P6_KW_USR)) {
1915 pmc_config->pm_caps |= PMC_CAP_USER;
1920 /* post processing */
1924 * The following events default to an evmask of 0
1927 /* default => 'self' */
1928 case PMC_EV_P6_BUS_DRDY_CLOCKS:
1929 case PMC_EV_P6_BUS_LOCK_CLOCKS:
1930 case PMC_EV_P6_BUS_TRAN_BRD:
1931 case PMC_EV_P6_BUS_TRAN_RFO:
1932 case PMC_EV_P6_BUS_TRANS_WB:
1933 case PMC_EV_P6_BUS_TRAN_IFETCH:
1934 case PMC_EV_P6_BUS_TRAN_INVAL:
1935 case PMC_EV_P6_BUS_TRAN_PWR:
1936 case PMC_EV_P6_BUS_TRANS_P:
1937 case PMC_EV_P6_BUS_TRANS_IO:
1938 case PMC_EV_P6_BUS_TRAN_DEF:
1939 case PMC_EV_P6_BUS_TRAN_BURST:
1940 case PMC_EV_P6_BUS_TRAN_ANY:
1941 case PMC_EV_P6_BUS_TRAN_MEM:
1943 /* default => 'nta' */
1944 case PMC_EV_P6_EMON_KNI_PREF_DISPATCHED:
1945 case PMC_EV_P6_EMON_KNI_PREF_MISS:
1947 /* default => 'packed and scalar' */
1948 case PMC_EV_P6_EMON_KNI_INST_RETIRED:
1949 case PMC_EV_P6_EMON_KNI_COMP_INST_RET:
1951 /* default => 'mmx to fp transitions' */
1952 case PMC_EV_P6_FP_MMX_TRANS:
1954 /* default => 'SSE Packed Single' */
1955 case PMC_EV_P6_EMON_SSE_SSE2_INST_RETIRED:
1956 case PMC_EV_P6_EMON_SSE_SSE2_COMP_INST_RETIRED:
1958 /* default => 'all fused micro-ops' */
1959 case PMC_EV_P6_EMON_FUSED_UOPS_RET:
1961 /* default => 'all transitions' */
1962 case PMC_EV_P6_EMON_EST_TRANS:
1965 case PMC_EV_P6_MMX_UOPS_EXEC:
1966 evmask = 0x0F; /* only value allowed */
1971 * For all other events, set the default event mask
1972 * to a logical OR of all the allowed event mask bits.
1974 if (evmask == 0 && pmask) {
1975 for (pm = pmask; pm->pm_name; pm++)
1976 evmask |= pm->pm_value;
1977 pmc_config->pm_caps |= PMC_CAP_QUALIFIER;
1983 if (pmc_config->pm_caps & PMC_CAP_QUALIFIER)
1984 pmc_config->pm_md.pm_ppro.pm_ppro_config |=
1985 P6_EVSEL_TO_UMASK(evmask);
1992 #if defined(__i386__) || defined(__amd64__)
1994 tsc_allocate_pmc(enum pmc_event pe, char *ctrspec,
1995 struct pmc_op_pmcallocate *pmc_config)
1997 if (pe != PMC_EV_TSC_TSC)
2000 /* TSC events must be unqualified. */
2001 if (ctrspec && *ctrspec != '\0')
2004 pmc_config->pm_md.pm_amd.pm_amd_config = 0;
2005 pmc_config->pm_caps |= PMC_CAP_READ;
2012 * Match an event name `name' with its canonical form.
2014 * Matches are case insensitive and spaces, periods, underscores and
2015 * hyphen characters are considered to match each other.
2017 * Returns 1 for a match, 0 otherwise.
2021 pmc_match_event_name(const char *name, const char *canonicalname)
2024 const unsigned char *c, *n;
2026 c = (const unsigned char *) canonicalname;
2027 n = (const unsigned char *) name;
2029 for (; (nc = *n) && (cc = *c); n++, c++) {
2031 if ((nc == ' ' || nc == '_' || nc == '-' || nc == '.') &&
2032 (cc == ' ' || cc == '_' || cc == '-' || cc == '.'))
2035 if (toupper(nc) == toupper(cc))
2042 if (*n == '\0' && *c == '\0')
2049 * Match an event name against all the event named supported by a
2052 * Returns an event descriptor pointer on match or NULL otherwise.
2054 static const struct pmc_event_descr *
2055 pmc_match_event_class(const char *name,
2056 const struct pmc_class_descr *pcd)
2059 const struct pmc_event_descr *ev;
2061 ev = pcd->pm_evc_event_table;
2062 for (n = 0; n < pcd->pm_evc_event_table_size; n++, ev++)
2063 if (pmc_match_event_name(name, ev->pm_ev_name))
2070 pmc_mdep_is_compatible_class(enum pmc_class pc)
2074 for (n = 0; n < pmc_mdep_class_list_size; n++)
2075 if (pmc_mdep_class_list[n] == pc)
2085 pmc_allocate(const char *ctrspec, enum pmc_mode mode,
2086 uint32_t flags, int cpu, pmc_id_t *pmcid)
2090 char *r, *spec_copy;
2091 const char *ctrname;
2092 const struct pmc_event_descr *ev;
2093 const struct pmc_event_alias *alias;
2094 struct pmc_op_pmcallocate pmc_config;
2095 const struct pmc_class_descr *pcd;
2100 if (mode != PMC_MODE_SS && mode != PMC_MODE_TS &&
2101 mode != PMC_MODE_SC && mode != PMC_MODE_TC) {
2106 /* replace an event alias with the canonical event specifier */
2107 if (pmc_mdep_event_aliases)
2108 for (alias = pmc_mdep_event_aliases; alias->pm_alias; alias++)
2109 if (!strcasecmp(ctrspec, alias->pm_alias)) {
2110 spec_copy = strdup(alias->pm_spec);
2114 if (spec_copy == NULL)
2115 spec_copy = strdup(ctrspec);
2118 ctrname = strsep(&r, ",");
2121 * If a explicit class prefix was given by the user, restrict the
2122 * search for the event to the specified PMC class.
2125 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) {
2126 pcd = pmc_class_table[n];
2127 if (pmc_mdep_is_compatible_class(pcd->pm_evc_class) &&
2128 strncasecmp(ctrname, pcd->pm_evc_name,
2129 pcd->pm_evc_name_size) == 0) {
2130 if ((ev = pmc_match_event_class(ctrname +
2131 pcd->pm_evc_name_size, pcd)) == NULL) {
2140 * Otherwise, search for this event in all compatible PMC
2143 for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) {
2144 pcd = pmc_class_table[n];
2145 if (pmc_mdep_is_compatible_class(pcd->pm_evc_class))
2146 ev = pmc_match_event_class(ctrname, pcd);
2154 bzero(&pmc_config, sizeof(pmc_config));
2155 pmc_config.pm_ev = ev->pm_ev_code;
2156 pmc_config.pm_class = pcd->pm_evc_class;
2157 pmc_config.pm_cpu = cpu;
2158 pmc_config.pm_mode = mode;
2159 pmc_config.pm_flags = flags;
2161 if (PMC_IS_SAMPLING_MODE(mode))
2162 pmc_config.pm_caps |= PMC_CAP_INTERRUPT;
2164 if (pcd->pm_evc_allocate_pmc(ev->pm_ev_code, r, &pmc_config) < 0) {
2169 if (PMC_CALL(PMCALLOCATE, &pmc_config) < 0)
2172 *pmcid = pmc_config.pm_pmcid;
2184 pmc_attach(pmc_id_t pmc, pid_t pid)
2186 struct pmc_op_pmcattach pmc_attach_args;
2188 pmc_attach_args.pm_pmc = pmc;
2189 pmc_attach_args.pm_pid = pid;
2191 return (PMC_CALL(PMCATTACH, &pmc_attach_args));
2195 pmc_capabilities(pmc_id_t pmcid, uint32_t *caps)
2200 cl = PMC_ID_TO_CLASS(pmcid);
2201 for (i = 0; i < cpu_info.pm_nclass; i++)
2202 if (cpu_info.pm_classes[i].pm_class == cl) {
2203 *caps = cpu_info.pm_classes[i].pm_caps;
2211 pmc_configure_logfile(int fd)
2213 struct pmc_op_configurelog cla;
2216 if (PMC_CALL(CONFIGURELOG, &cla) < 0)
2222 pmc_cpuinfo(const struct pmc_cpuinfo **pci)
2224 if (pmc_syscall == -1) {
2234 pmc_detach(pmc_id_t pmc, pid_t pid)
2236 struct pmc_op_pmcattach pmc_detach_args;
2238 pmc_detach_args.pm_pmc = pmc;
2239 pmc_detach_args.pm_pid = pid;
2240 return (PMC_CALL(PMCDETACH, &pmc_detach_args));
2244 pmc_disable(int cpu, int pmc)
2246 struct pmc_op_pmcadmin ssa;
2250 ssa.pm_state = PMC_STATE_DISABLED;
2251 return (PMC_CALL(PMCADMIN, &ssa));
2255 pmc_enable(int cpu, int pmc)
2257 struct pmc_op_pmcadmin ssa;
2261 ssa.pm_state = PMC_STATE_FREE;
2262 return (PMC_CALL(PMCADMIN, &ssa));
2266 * Return a list of events known to a given PMC class. 'cl' is the
2267 * PMC class identifier, 'eventnames' is the returned list of 'const
2268 * char *' pointers pointing to the names of the events. 'nevents' is
2269 * the number of event name pointers returned.
2271 * The space for 'eventnames' is allocated using malloc(3). The caller
2272 * is responsible for freeing this space when done.
2275 pmc_event_names_of_class(enum pmc_class cl, const char ***eventnames,
2280 const struct pmc_event_descr *ev;
2285 ev = iaf_event_table;
2286 count = PMC_EVENT_TABLE_SIZE(iaf);
2290 * Return the most appropriate set of event name
2291 * spellings for the current CPU.
2293 switch (cpu_info.pm_cputype) {
2295 case PMC_CPU_INTEL_ATOM:
2296 ev = atom_event_table;
2297 count = PMC_EVENT_TABLE_SIZE(atom);
2299 case PMC_CPU_INTEL_CORE:
2300 ev = core_event_table;
2301 count = PMC_EVENT_TABLE_SIZE(core);
2303 case PMC_CPU_INTEL_CORE2:
2304 case PMC_CPU_INTEL_CORE2EXTREME:
2305 ev = core2_event_table;
2306 count = PMC_EVENT_TABLE_SIZE(core2);
2308 case PMC_CPU_INTEL_COREI7:
2309 ev = corei7_event_table;
2310 count = PMC_EVENT_TABLE_SIZE(corei7);
2315 ev = tsc_event_table;
2316 count = PMC_EVENT_TABLE_SIZE(tsc);
2319 ev = k7_event_table;
2320 count = PMC_EVENT_TABLE_SIZE(k7);
2323 ev = k8_event_table;
2324 count = PMC_EVENT_TABLE_SIZE(k8);
2327 ev = p4_event_table;
2328 count = PMC_EVENT_TABLE_SIZE(p4);
2331 ev = p5_event_table;
2332 count = PMC_EVENT_TABLE_SIZE(p5);
2335 ev = p6_event_table;
2336 count = PMC_EVENT_TABLE_SIZE(p6);
2343 if ((names = malloc(count * sizeof(const char *))) == NULL)
2346 *eventnames = names;
2349 for (;count--; ev++, names++)
2350 *names = ev->pm_ev_name;
2355 pmc_flush_logfile(void)
2357 return (PMC_CALL(FLUSHLOG,0));
2361 pmc_get_driver_stats(struct pmc_driverstats *ds)
2363 struct pmc_op_getdriverstats gms;
2365 if (PMC_CALL(GETDRIVERSTATS, &gms) < 0)
2368 /* copy out fields in the current userland<->library interface */
2369 ds->pm_intr_ignored = gms.pm_intr_ignored;
2370 ds->pm_intr_processed = gms.pm_intr_processed;
2371 ds->pm_intr_bufferfull = gms.pm_intr_bufferfull;
2372 ds->pm_syscalls = gms.pm_syscalls;
2373 ds->pm_syscall_errors = gms.pm_syscall_errors;
2374 ds->pm_buffer_requests = gms.pm_buffer_requests;
2375 ds->pm_buffer_requests_failed = gms.pm_buffer_requests_failed;
2376 ds->pm_log_sweeps = gms.pm_log_sweeps;
2381 pmc_get_msr(pmc_id_t pmc, uint32_t *msr)
2383 struct pmc_op_getmsr gm;
2386 if (PMC_CALL(PMCGETMSR, &gm) < 0)
2395 int error, pmc_mod_id;
2397 uint32_t abi_version;
2398 struct module_stat pmc_modstat;
2399 struct pmc_op_getcpuinfo op_cpu_info;
2400 #if defined(__amd64__) || defined(__i386__)
2401 int cpu_has_iaf_counters;
2405 if (pmc_syscall != -1) /* already inited */
2408 /* retrieve the system call number from the KLD */
2409 if ((pmc_mod_id = modfind(PMC_MODULE_NAME)) < 0)
2412 pmc_modstat.version = sizeof(struct module_stat);
2413 if ((error = modstat(pmc_mod_id, &pmc_modstat)) < 0)
2416 pmc_syscall = pmc_modstat.data.intval;
2418 /* check the kernel module's ABI against our compiled-in version */
2419 abi_version = PMC_VERSION;
2420 if (PMC_CALL(GETMODULEVERSION, &abi_version) < 0)
2421 return (pmc_syscall = -1);
2423 /* ignore patch & minor numbers for the comparision */
2424 if ((abi_version & 0xFF000000) != (PMC_VERSION & 0xFF000000)) {
2425 errno = EPROGMISMATCH;
2426 return (pmc_syscall = -1);
2429 if (PMC_CALL(GETCPUINFO, &op_cpu_info) < 0)
2430 return (pmc_syscall = -1);
2432 cpu_info.pm_cputype = op_cpu_info.pm_cputype;
2433 cpu_info.pm_ncpu = op_cpu_info.pm_ncpu;
2434 cpu_info.pm_npmc = op_cpu_info.pm_npmc;
2435 cpu_info.pm_nclass = op_cpu_info.pm_nclass;
2436 for (n = 0; n < cpu_info.pm_nclass; n++)
2437 cpu_info.pm_classes[n] = op_cpu_info.pm_classes[n];
2439 pmc_class_table = malloc(PMC_CLASS_TABLE_SIZE *
2440 sizeof(struct pmc_class_descr *));
2442 if (pmc_class_table == NULL)
2445 for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++)
2446 pmc_class_table[n] = NULL;
2449 * Fill in the class table.
2452 #if defined(__amd64__) || defined(__i386__)
2453 pmc_class_table[n++] = &tsc_class_table_descr;
2456 * Check if this CPU has fixed function counters.
2458 cpu_has_iaf_counters = 0;
2459 for (t = 0; t < cpu_info.pm_nclass; t++)
2460 if (cpu_info.pm_classes[t].pm_class == PMC_CLASS_IAF)
2461 cpu_has_iaf_counters = 1;
2464 #define PMC_MDEP_INIT(C) do { \
2465 pmc_mdep_event_aliases = C##_aliases; \
2466 pmc_mdep_class_list = C##_pmc_classes; \
2467 pmc_mdep_class_list_size = \
2468 PMC_TABLE_SIZE(C##_pmc_classes); \
2471 #define PMC_MDEP_INIT_INTEL_V2(C) do { \
2473 if (cpu_has_iaf_counters) \
2474 pmc_class_table[n++] = &iaf_class_table_descr; \
2476 pmc_mdep_event_aliases = \
2477 C##_aliases_without_iaf; \
2478 pmc_class_table[n] = &C##_class_table_descr; \
2481 /* Configure the event name parser. */
2482 switch (cpu_info.pm_cputype) {
2483 #if defined(__i386__)
2484 case PMC_CPU_AMD_K7:
2486 pmc_class_table[n] = &k7_class_table_descr;
2488 case PMC_CPU_INTEL_P5:
2490 pmc_class_table[n] = &p5_class_table_descr;
2492 case PMC_CPU_INTEL_P6: /* P6 ... Pentium M CPUs have */
2493 case PMC_CPU_INTEL_PII: /* similar PMCs. */
2494 case PMC_CPU_INTEL_PIII:
2495 case PMC_CPU_INTEL_PM:
2497 pmc_class_table[n] = &p6_class_table_descr;
2500 #if defined(__amd64__) || defined(__i386__)
2501 case PMC_CPU_AMD_K8:
2503 pmc_class_table[n] = &k8_class_table_descr;
2505 case PMC_CPU_INTEL_ATOM:
2506 PMC_MDEP_INIT_INTEL_V2(atom);
2508 case PMC_CPU_INTEL_CORE:
2509 PMC_MDEP_INIT(core);
2511 case PMC_CPU_INTEL_CORE2:
2512 case PMC_CPU_INTEL_CORE2EXTREME:
2513 PMC_MDEP_INIT_INTEL_V2(core2);
2515 case PMC_CPU_INTEL_COREI7:
2516 PMC_MDEP_INIT_INTEL_V2(corei7);
2518 case PMC_CPU_INTEL_PIV:
2520 pmc_class_table[n] = &p4_class_table_descr;
2527 * Some kind of CPU this version of the library knows nothing
2528 * about. This shouldn't happen since the abi version check
2529 * should have caught this.
2532 return (pmc_syscall = -1);
2539 pmc_name_of_capability(enum pmc_caps cap)
2544 * 'cap' should have a single bit set and should be in
2547 if ((cap & (cap - 1)) || cap < PMC_CAP_FIRST ||
2548 cap > PMC_CAP_LAST) {
2554 return (pmc_capability_names[i - 1]);
2558 pmc_name_of_class(enum pmc_class pc)
2560 if ((int) pc >= PMC_CLASS_FIRST &&
2561 pc <= PMC_CLASS_LAST)
2562 return (pmc_class_names[pc]);
2569 pmc_name_of_cputype(enum pmc_cputype cp)
2573 for (n = 0; n < PMC_TABLE_SIZE(pmc_cputype_names); n++)
2574 if (cp == pmc_cputype_names[n].pm_cputype)
2575 return (pmc_cputype_names[n].pm_name);
2582 pmc_name_of_disposition(enum pmc_disp pd)
2584 if ((int) pd >= PMC_DISP_FIRST &&
2585 pd <= PMC_DISP_LAST)
2586 return (pmc_disposition_names[pd]);
2593 _pmc_name_of_event(enum pmc_event pe, enum pmc_cputype cpu)
2595 const struct pmc_event_descr *ev, *evfence;
2597 ev = evfence = NULL;
2598 if (pe >= PMC_EV_IAF_FIRST && pe <= PMC_EV_IAF_LAST) {
2599 ev = iaf_event_table;
2600 evfence = iaf_event_table + PMC_EVENT_TABLE_SIZE(iaf);
2601 } else if (pe >= PMC_EV_IAP_FIRST && pe <= PMC_EV_IAP_LAST) {
2603 case PMC_CPU_INTEL_ATOM:
2604 ev = atom_event_table;
2605 evfence = atom_event_table + PMC_EVENT_TABLE_SIZE(atom);
2607 case PMC_CPU_INTEL_CORE:
2608 ev = core_event_table;
2609 evfence = core_event_table + PMC_EVENT_TABLE_SIZE(core);
2611 case PMC_CPU_INTEL_CORE2:
2612 case PMC_CPU_INTEL_CORE2EXTREME:
2613 ev = core2_event_table;
2614 evfence = core2_event_table + PMC_EVENT_TABLE_SIZE(core2);
2616 case PMC_CPU_INTEL_COREI7:
2617 ev = corei7_event_table;
2618 evfence = corei7_event_table + PMC_EVENT_TABLE_SIZE(corei7);
2620 default: /* Unknown CPU type. */
2623 } if (pe >= PMC_EV_K7_FIRST && pe <= PMC_EV_K7_LAST) {
2624 ev = k7_event_table;
2625 evfence = k7_event_table + PMC_EVENT_TABLE_SIZE(k7);
2626 } else if (pe >= PMC_EV_K8_FIRST && pe <= PMC_EV_K8_LAST) {
2627 ev = k8_event_table;
2628 evfence = k8_event_table + PMC_EVENT_TABLE_SIZE(k8);
2629 } else if (pe >= PMC_EV_P4_FIRST && pe <= PMC_EV_P4_LAST) {
2630 ev = p4_event_table;
2631 evfence = p4_event_table + PMC_EVENT_TABLE_SIZE(p4);
2632 } else if (pe >= PMC_EV_P5_FIRST && pe <= PMC_EV_P5_LAST) {
2633 ev = p5_event_table;
2634 evfence = p5_event_table + PMC_EVENT_TABLE_SIZE(p5);
2635 } else if (pe >= PMC_EV_P6_FIRST && pe <= PMC_EV_P6_LAST) {
2636 ev = p6_event_table;
2637 evfence = p6_event_table + PMC_EVENT_TABLE_SIZE(p6);
2638 } else if (pe == PMC_EV_TSC_TSC) {
2639 ev = tsc_event_table;
2640 evfence = tsc_event_table + PMC_EVENT_TABLE_SIZE(tsc);
2643 for (; ev != evfence; ev++)
2644 if (pe == ev->pm_ev_code)
2645 return (ev->pm_ev_name);
2651 pmc_name_of_event(enum pmc_event pe)
2655 if ((n = _pmc_name_of_event(pe, cpu_info.pm_cputype)) != NULL)
2663 pmc_name_of_mode(enum pmc_mode pm)
2665 if ((int) pm >= PMC_MODE_FIRST &&
2666 pm <= PMC_MODE_LAST)
2667 return (pmc_mode_names[pm]);
2674 pmc_name_of_state(enum pmc_state ps)
2676 if ((int) ps >= PMC_STATE_FIRST &&
2677 ps <= PMC_STATE_LAST)
2678 return (pmc_state_names[ps]);
2687 if (pmc_syscall == -1) {
2692 return (cpu_info.pm_ncpu);
2698 if (pmc_syscall == -1) {
2703 if (cpu < 0 || cpu >= (int) cpu_info.pm_ncpu) {
2708 return (cpu_info.pm_npmc);
2712 pmc_pmcinfo(int cpu, struct pmc_pmcinfo **ppmci)
2715 struct pmc_op_getpmcinfo *pmci;
2717 if ((npmc = pmc_npmc(cpu)) < 0)
2720 nbytes = sizeof(struct pmc_op_getpmcinfo) +
2721 npmc * sizeof(struct pmc_info);
2723 if ((pmci = calloc(1, nbytes)) == NULL)
2728 if (PMC_CALL(GETPMCINFO, pmci) < 0) {
2733 /* kernel<->library, library<->userland interfaces are identical */
2734 *ppmci = (struct pmc_pmcinfo *) pmci;
2739 pmc_read(pmc_id_t pmc, pmc_value_t *value)
2741 struct pmc_op_pmcrw pmc_read_op;
2743 pmc_read_op.pm_pmcid = pmc;
2744 pmc_read_op.pm_flags = PMC_F_OLDVALUE;
2745 pmc_read_op.pm_value = -1;
2747 if (PMC_CALL(PMCRW, &pmc_read_op) < 0)
2750 *value = pmc_read_op.pm_value;
2755 pmc_release(pmc_id_t pmc)
2757 struct pmc_op_simple pmc_release_args;
2759 pmc_release_args.pm_pmcid = pmc;
2760 return (PMC_CALL(PMCRELEASE, &pmc_release_args));
2764 pmc_rw(pmc_id_t pmc, pmc_value_t newvalue, pmc_value_t *oldvaluep)
2766 struct pmc_op_pmcrw pmc_rw_op;
2768 pmc_rw_op.pm_pmcid = pmc;
2769 pmc_rw_op.pm_flags = PMC_F_NEWVALUE | PMC_F_OLDVALUE;
2770 pmc_rw_op.pm_value = newvalue;
2772 if (PMC_CALL(PMCRW, &pmc_rw_op) < 0)
2775 *oldvaluep = pmc_rw_op.pm_value;
2780 pmc_set(pmc_id_t pmc, pmc_value_t value)
2782 struct pmc_op_pmcsetcount sc;
2785 sc.pm_count = value;
2787 if (PMC_CALL(PMCSETCOUNT, &sc) < 0)
2793 pmc_start(pmc_id_t pmc)
2795 struct pmc_op_simple pmc_start_args;
2797 pmc_start_args.pm_pmcid = pmc;
2798 return (PMC_CALL(PMCSTART, &pmc_start_args));
2802 pmc_stop(pmc_id_t pmc)
2804 struct pmc_op_simple pmc_stop_args;
2806 pmc_stop_args.pm_pmcid = pmc;
2807 return (PMC_CALL(PMCSTOP, &pmc_stop_args));
2811 pmc_width(pmc_id_t pmcid, uint32_t *width)
2816 cl = PMC_ID_TO_CLASS(pmcid);
2817 for (i = 0; i < cpu_info.pm_nclass; i++)
2818 if (cpu_info.pm_classes[i].pm_class == cl) {
2819 *width = cpu_info.pm_classes[i].pm_width;
2827 pmc_write(pmc_id_t pmc, pmc_value_t value)
2829 struct pmc_op_pmcrw pmc_write_op;
2831 pmc_write_op.pm_pmcid = pmc;
2832 pmc_write_op.pm_flags = PMC_F_NEWVALUE;
2833 pmc_write_op.pm_value = value;
2834 return (PMC_CALL(PMCRW, &pmc_write_op));
2838 pmc_writelog(uint32_t userdata)
2840 struct pmc_op_writelog wl;
2842 wl.pm_userdata = userdata;
2843 return (PMC_CALL(WRITELOG, &wl));