17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*23961e2bSvb70745 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #ifndef _LIBCPC_IMPL_H 287c478bd9Sstevel@tonic-gate #define _LIBCPC_IMPL_H 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include <libcpc.h> 337c478bd9Sstevel@tonic-gate #include <inttypes.h> 347c478bd9Sstevel@tonic-gate #include <thread.h> 357c478bd9Sstevel@tonic-gate #include <synch.h> 367c478bd9Sstevel@tonic-gate #include <sys/types.h> 377c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #ifdef __cplusplus 407c478bd9Sstevel@tonic-gate extern "C" { 417c478bd9Sstevel@tonic-gate #endif 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate #define CPC_VER_1 1 447c478bd9Sstevel@tonic-gate #define CPC1_BUFSIZE (2 * sizeof (uint64_t)) 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate struct _cpc_attr { 477c478bd9Sstevel@tonic-gate char ca_name[CPC_MAX_ATTR_LEN]; 487c478bd9Sstevel@tonic-gate uint64_t ca_val; 497c478bd9Sstevel@tonic-gate }; 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate typedef struct __cpc_request cpc_request_t; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate struct __cpc_request { 547c478bd9Sstevel@tonic-gate char cr_event[CPC_MAX_EVENT_LEN]; 557c478bd9Sstevel@tonic-gate uint64_t cr_preset; /* Initial value */ 567c478bd9Sstevel@tonic-gate uint16_t cr_index; /* Index of request in data */ 577c478bd9Sstevel@tonic-gate uint_t cr_flags; 587c478bd9Sstevel@tonic-gate uint_t cr_nattrs; /* # CPU-specific attrs */ 597c478bd9Sstevel@tonic-gate kcpc_attr_t *cr_attr; 607c478bd9Sstevel@tonic-gate cpc_request_t *cr_next; /* next request in set */ 617c478bd9Sstevel@tonic-gate }; 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate struct __cpc_buf { 647c478bd9Sstevel@tonic-gate uint64_t *cb_data; /* Pointer to data store */ 657c478bd9Sstevel@tonic-gate hrtime_t cb_hrtime; /* hrtime at last sample */ 667c478bd9Sstevel@tonic-gate uint64_t cb_tick; /* virtualized tsc/tick */ 677c478bd9Sstevel@tonic-gate size_t cb_size; /* Size of data store, bytes */ 687c478bd9Sstevel@tonic-gate cpc_buf_t *cb_next; /* List of all bufs */ 697c478bd9Sstevel@tonic-gate }; 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * Possible cpc_set_t states: 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate typedef enum { 757c478bd9Sstevel@tonic-gate CS_UNBOUND, /* Set is not currently bound */ 767c478bd9Sstevel@tonic-gate CS_BOUND_CURLWP, /* Set has been bound to curlwp */ 777c478bd9Sstevel@tonic-gate CS_BOUND_PCTX, /* Set has been bound via libpctx */ 787c478bd9Sstevel@tonic-gate CS_BOUND_CPU /* Set has been bound to a CPU */ 797c478bd9Sstevel@tonic-gate } __cpc_state_t; 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate struct __cpc_set { 827c478bd9Sstevel@tonic-gate cpc_request_t *cs_request; /* linked list of requests */ 837c478bd9Sstevel@tonic-gate __cpc_state_t cs_state; /* State of this set */ 847c478bd9Sstevel@tonic-gate int cs_nreqs; /* Number of requests in set */ 857c478bd9Sstevel@tonic-gate int cs_fd; /* file descriptor of cpc dev */ 867c478bd9Sstevel@tonic-gate processorid_t cs_obind; /* previous proc binding */ 877c478bd9Sstevel@tonic-gate pctx_t *cs_pctx; /* pctx of process bound to */ 887c478bd9Sstevel@tonic-gate id_t cs_id; /* lwp ID of pctx binding */ 897c478bd9Sstevel@tonic-gate thread_t cs_thr; /* thread ID which bound set */ 907c478bd9Sstevel@tonic-gate cpc_set_t *cs_next; /* Linked list of all sets */ 917c478bd9Sstevel@tonic-gate }; 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate struct __cpc { 947c478bd9Sstevel@tonic-gate cpc_set_t *cpc_sets; /* List of existing sets */ 957c478bd9Sstevel@tonic-gate cpc_buf_t *cpc_bufs; /* List of existing bufs */ 967c478bd9Sstevel@tonic-gate cpc_errhndlr_t *cpc_errfn; /* Handles library errors */ 977c478bd9Sstevel@tonic-gate mutex_t cpc_lock; /* Protect various ops */ 987c478bd9Sstevel@tonic-gate char *cpc_attrlist; /* List of supported attrs */ 997c478bd9Sstevel@tonic-gate char **cpc_evlist; /* List of events per pic */ 1007c478bd9Sstevel@tonic-gate char cpc_cpuref[CPC_MAX_CPUREF]; 1017c478bd9Sstevel@tonic-gate char cpc_cciname[CPC_MAX_IMPL_NAME]; 1027c478bd9Sstevel@tonic-gate uint_t cpc_caps; 1037c478bd9Sstevel@tonic-gate uint_t cpc_npic; 1047c478bd9Sstevel@tonic-gate }; 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* 1077c478bd9Sstevel@tonic-gate * cpc_t handle for CPCv1 clients. 1087c478bd9Sstevel@tonic-gate */ 1097c478bd9Sstevel@tonic-gate extern cpc_t *__cpc; 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 1127c478bd9Sstevel@tonic-gate extern void __cpc_error(const char *fn, const char *fmt, ...); 1137c478bd9Sstevel@tonic-gate 1147c478bd9Sstevel@tonic-gate extern const char *__cpc_reg_to_name(int cpuver, int regno, uint8_t bits); 1157c478bd9Sstevel@tonic-gate extern int __cpc_name_to_reg(int cpuver, int regno, 1167c478bd9Sstevel@tonic-gate const char *name, uint8_t *bits); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate extern uint_t __cpc_workver; 1197c478bd9Sstevel@tonic-gate extern int __cpc_v1_cpuver; 1207c478bd9Sstevel@tonic-gate #ifdef __sparc 1217c478bd9Sstevel@tonic-gate extern uint64_t __cpc_v1_pcr; 1227c478bd9Sstevel@tonic-gate #else 1237c478bd9Sstevel@tonic-gate extern uint32_t __cpc_v1_pes[2]; 1247c478bd9Sstevel@tonic-gate #endif /* __sparc */ 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate extern char *__cpc_pack_set(cpc_set_t *set, uint_t flags, size_t *buflen); 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate typedef struct __cpc_strhash cpc_strhash_t; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate struct __cpc_strhash { 1317c478bd9Sstevel@tonic-gate char *str; 1327c478bd9Sstevel@tonic-gate struct __cpc_strhash *cur; 1337c478bd9Sstevel@tonic-gate struct __cpc_strhash *next; 1347c478bd9Sstevel@tonic-gate }; 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate extern cpc_strhash_t *__cpc_strhash_alloc(void); 1377c478bd9Sstevel@tonic-gate extern void __cpc_strhash_free(cpc_strhash_t *hash); 1387c478bd9Sstevel@tonic-gate extern int __cpc_strhash_add(cpc_strhash_t *hash, char *key); 1397c478bd9Sstevel@tonic-gate extern char *__cpc_strhash_next(cpc_strhash_t *hash); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Implementation-private system call used by libcpc 1437c478bd9Sstevel@tonic-gate */ 1447c478bd9Sstevel@tonic-gate struct __cpc; 1457c478bd9Sstevel@tonic-gate extern int __pctx_cpc(pctx_t *pctx, struct __cpc *cpc, int cmd, id_t lwpid, 1467c478bd9Sstevel@tonic-gate void *data1, void *data2, void *data3, int bufsize); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate #define CPUDRV "/devices/pseudo/cpc@0" 1497c478bd9Sstevel@tonic-gate #define CPUDRV_SHARED CPUDRV":shared" 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate #if defined(__sparc) || defined(__i386) 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * These two are only used for backwards compatibility to the Obsolete CPCv1. 1547c478bd9Sstevel@tonic-gate */ 1557c478bd9Sstevel@tonic-gate extern int __cpc_init(void); 1567c478bd9Sstevel@tonic-gate extern cpc_set_t *__cpc_eventtoset(cpc_t *cpc, cpc_event_t *event, int flags); 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* 1597c478bd9Sstevel@tonic-gate * ce_cpuver values 1607c478bd9Sstevel@tonic-gate */ 1617c478bd9Sstevel@tonic-gate #define CPC_ULTRA1 1000 1627c478bd9Sstevel@tonic-gate #define CPC_ULTRA2 1001 /* same as ultra1 for these purposes */ 1637c478bd9Sstevel@tonic-gate #define CPC_ULTRA3 1002 1647c478bd9Sstevel@tonic-gate #define CPC_ULTRA3_PLUS 1003 1657c478bd9Sstevel@tonic-gate #define CPC_ULTRA3_I 1004 166*23961e2bSvb70745 #define CPC_ULTRA4_PLUS 1005 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate #define CPC_PENTIUM 2000 1697c478bd9Sstevel@tonic-gate #define CPC_PENTIUM_MMX 2001 1707c478bd9Sstevel@tonic-gate #define CPC_PENTIUM_PRO 2002 1717c478bd9Sstevel@tonic-gate #define CPC_PENTIUM_PRO_MMX 2003 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate #define CPC_SPARC64_III 3000 1747c478bd9Sstevel@tonic-gate #define CPC_SPARC64_V 3002 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate #endif /* __sparc || __i386 */ 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64) 1797c478bd9Sstevel@tonic-gate /* 1807c478bd9Sstevel@tonic-gate * This is common between i386 and amd64, because amd64 implements %tick. 1817c478bd9Sstevel@tonic-gate * Currently only used by the cpc tools to print the label atop the CPU ticks 1827c478bd9Sstevel@tonic-gate * column on amd64. 1837c478bd9Sstevel@tonic-gate */ 1847c478bd9Sstevel@tonic-gate #define CPC_TICKREG_NAME "tsc" 1857c478bd9Sstevel@tonic-gate #endif /* __i386 || __amd64 */ 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate #if defined(__sparc) 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate /* 190*23961e2bSvb70745 * UltraSPARC I, II, III and IV processors 1917c478bd9Sstevel@tonic-gate * 1927c478bd9Sstevel@tonic-gate * The performance counters on these processors allow up to two 32-bit 1937c478bd9Sstevel@tonic-gate * performance events to be captured simultaneously from a selection 1947c478bd9Sstevel@tonic-gate * of metrics. The metrics are selected by writing to the performance 1957c478bd9Sstevel@tonic-gate * control register, and subsequent values collected by reading from the 1967c478bd9Sstevel@tonic-gate * performance instrumentation counter registers. Both registers are 1977c478bd9Sstevel@tonic-gate * priviliged by default, and implemented as ASRs. 1987c478bd9Sstevel@tonic-gate */ 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate struct _cpc_event { 2017c478bd9Sstevel@tonic-gate int ce_cpuver; 2027c478bd9Sstevel@tonic-gate hrtime_t ce_hrt; /* gethrtime() */ 2037c478bd9Sstevel@tonic-gate uint64_t ce_tick; /* virtualized %tick */ 2047c478bd9Sstevel@tonic-gate uint64_t ce_pic[2]; /* virtualized %pic */ 2057c478bd9Sstevel@tonic-gate uint64_t ce_pcr; /* %pcr */ 2067c478bd9Sstevel@tonic-gate }; 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate #define CPC_TICKREG(ev) ((ev)->ce_tick) 2097c478bd9Sstevel@tonic-gate #define CPC_TICKREG_NAME "%tick" 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate /* 2127c478bd9Sstevel@tonic-gate * "Well known" bitfields in the UltraSPARC %pcr register 2137c478bd9Sstevel@tonic-gate * The interfaces in libcpc should make these #defines uninteresting. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate #define CPC_ULTRA_PCR_USR 2 2167c478bd9Sstevel@tonic-gate #define CPC_ULTRA_PCR_SYS 1 2177c478bd9Sstevel@tonic-gate #define CPC_ULTRA_PCR_PRIVPIC 0 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate #define CPC_ULTRA_PCR_PIC0_SHIFT 4 2207c478bd9Sstevel@tonic-gate #define CPC_ULTRA2_PCR_PIC0_MASK UINT64_C(0xf) 2217c478bd9Sstevel@tonic-gate #define CPC_ULTRA3_PCR_PIC0_MASK UINT64_C(0x3f) 2227c478bd9Sstevel@tonic-gate #define CPC_ULTRA_PCR_PIC1_SHIFT 11 2237c478bd9Sstevel@tonic-gate #define CPC_ULTRA2_PCR_PIC1_MASK UINT64_C(0xf) 2247c478bd9Sstevel@tonic-gate #define CPC_ULTRA3_PCR_PIC1_MASK UINT64_C(0x3f) 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate #elif defined(__i386) 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* 2297c478bd9Sstevel@tonic-gate * Pentium I, II and III processors 2307c478bd9Sstevel@tonic-gate * 2317c478bd9Sstevel@tonic-gate * These CPUs allow pairs of events to captured. 2327c478bd9Sstevel@tonic-gate * The hardware counters count up to 40-bits of significance, but 2337c478bd9Sstevel@tonic-gate * only allow 32 (signed) bits to be programmed into them. 2347c478bd9Sstevel@tonic-gate * Pentium I and Pentium II processors are programmed differently, but 2357c478bd9Sstevel@tonic-gate * the resulting counters and timestamps can be handled portably. 2367c478bd9Sstevel@tonic-gate */ 2377c478bd9Sstevel@tonic-gate 2387c478bd9Sstevel@tonic-gate struct _cpc_event { 2397c478bd9Sstevel@tonic-gate int ce_cpuver; 2407c478bd9Sstevel@tonic-gate hrtime_t ce_hrt; /* gethrtime() */ 2417c478bd9Sstevel@tonic-gate uint64_t ce_tsc; /* virtualized rdtsc value */ 2427c478bd9Sstevel@tonic-gate uint64_t ce_pic[2]; /* virtualized PerfCtr[01] */ 2437c478bd9Sstevel@tonic-gate uint32_t ce_pes[2]; /* Pentium II */ 2447c478bd9Sstevel@tonic-gate #define ce_cesr ce_pes[0] /* Pentium I */ 2457c478bd9Sstevel@tonic-gate }; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate #define CPC_TICKREG(ev) ((ev)->ce_tsc) 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate /* 2507c478bd9Sstevel@tonic-gate * "Well known" bit fields in the Pentium CES register 2517c478bd9Sstevel@tonic-gate * The interfaces in libcpc should make these #defines uninteresting. 2527c478bd9Sstevel@tonic-gate */ 2537c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_ES0_SHIFT 0 2547c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_ES0_MASK 0x3f 2557c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_ES1_SHIFT 16 2567c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_ES1_MASK 0x3f 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_OS0 6 2597c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_USR0 7 2607c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_CLK0 8 2617c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_PC0 9 2627c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_OS1 (CPC_P5_CESR_OS0 + 16) 2637c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_USR1 (CPC_P5_CESR_USR0 + 16) 2647c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_CLK1 (CPC_P5_CESR_CLK0 + 16) 2657c478bd9Sstevel@tonic-gate #define CPC_P5_CESR_PC1 (CPC_P5_CESR_PC0 + 16) 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate /* 2687c478bd9Sstevel@tonic-gate * "Well known" bit fields in the Pentium Pro PerfEvtSel registers 2697c478bd9Sstevel@tonic-gate * The interfaces in libcpc should make these #defines uninteresting. 2707c478bd9Sstevel@tonic-gate */ 2717c478bd9Sstevel@tonic-gate #define CPC_P6_PES_INV 23 2727c478bd9Sstevel@tonic-gate #define CPC_P6_PES_EN 22 2737c478bd9Sstevel@tonic-gate #define CPC_P6_PES_INT 20 2747c478bd9Sstevel@tonic-gate #define CPC_P6_PES_PC 19 2757c478bd9Sstevel@tonic-gate #define CPC_P6_PES_E 18 2767c478bd9Sstevel@tonic-gate #define CPC_P6_PES_OS 17 2777c478bd9Sstevel@tonic-gate #define CPC_P6_PES_USR 16 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate #define CPC_P6_PES_UMASK_SHIFT 8 2807c478bd9Sstevel@tonic-gate #define CPC_P6_PES_UMASK_MASK (0xffu) 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate #define CPC_P6_PES_CMASK_SHIFT 24 2837c478bd9Sstevel@tonic-gate #define CPC_P6_PES_CMASK_MASK (0xffu) 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate #define CPC_P6_PES_PIC0_MASK (0xffu) 2867c478bd9Sstevel@tonic-gate #define CPC_P6_PES_PIC1_MASK (0xffu) 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate #endif /* __i386 */ 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate #ifdef __cplusplus 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate #endif 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate #endif /* _LIBCPC_IMPL_H */ 295