1c10c16deSRichard Lowe.\" Copyright (c) 2008, Sun Microsystems, Inc. All Rights Reserved. 2c10c16deSRichard Lowe.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. 3c10c16deSRichard Lowe.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License. 4c10c16deSRichard Lowe.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner] 5*53548f91SRobert Mustacchi.\" Copyright (c) 2018, Joyent, Inc. 6*53548f91SRobert Mustacchi.Dd June 28, 2018 7*53548f91SRobert Mustacchi.Dt CPC 3CPC 8*53548f91SRobert Mustacchi.Os 9*53548f91SRobert Mustacchi.Sh NAME 10*53548f91SRobert Mustacchi.Nm cpc 11*53548f91SRobert Mustacchi.Nd hardware performance counters 12*53548f91SRobert Mustacchi.Sh DESCRIPTION 13*53548f91SRobert MustacchiModern microprocessors contain 14*53548f91SRobert Mustacchi.Em hardware performance counters 15*53548f91SRobert Mustacchithat allow 16c10c16deSRichard Lowethe measurement of many different hardware events related to CPU behavior, 17c10c16deSRichard Loweincluding instruction and data cache misses as well as various internal states 18*53548f91SRobert Mustacchiof the processor. 19*53548f91SRobert MustacchiThe counters can be configured to count user events, system 20*53548f91SRobert Mustacchievents, or both. 21*53548f91SRobert MustacchiData from the performance counters can be used to analyze and 22c10c16deSRichard Lowetune the behavior of software on a particular type of processor. 23*53548f91SRobert Mustacchi.Pp 24c10c16deSRichard LoweMost processors are able to generate an interrupt on counter overflow, allowing 25c10c16deSRichard Lowethe counters to be used for various forms of profiling. 26*53548f91SRobert Mustacchi.Pp 27*53548f91SRobert MustacchiThis manual page describes a set of APIs that allow illumos applications to use 28*53548f91SRobert Mustacchithese counters. 29*53548f91SRobert MustacchiApplications can measure their own behavior, the behavior of 30c10c16deSRichard Loweother applications, or the behavior of the whole system. 31*53548f91SRobert Mustacchi.Ss Shared Counters or Private Counters 32*53548f91SRobert MustacchiThere are two principal models for using these performance counters. 33*53548f91SRobert MustacchiSome users of these statistics want to observe system-wide behavior. 34*53548f91SRobert MustacchiOther users want to view the performance counters as part of the register set 35*53548f91SRobert Mustacchiexported by each 36*53548f91SRobert Mustacchi.Sy LWP . 37*53548f91SRobert MustacchiOn a machine performing more than one activity, these two models are 38c10c16deSRichard Lowein conflict because the counters represent a critical hardware resource that 39c10c16deSRichard Lowecannot simultaneously be both shared and private. 40*53548f91SRobert Mustacchi.Ss Configuration Interfaces 41c10c16deSRichard LoweThe following configuration interfaces are provided: 42*53548f91SRobert Mustacchi.Bl -tag -width Xr 43*53548f91SRobert Mustacchi.It Xr cpc_open 3CPC 44c10c16deSRichard LoweCheck the version the application was compiled with against the version of the 45*53548f91SRobert Mustacchi.It Xr cpc_cciname 3CPC 46c10c16deSRichard LoweReturn a printable string to describe the performance counters of the 47c10c16deSRichard Loweprocessor. 48*53548f91SRobert Mustacchi.It Xr cpc_npic 3CPC 49c10c16deSRichard LoweReturn the number of performance counters on the processor. 50*53548f91SRobert Mustacchi.It cpc_cpuref 3CPC 51c10c16deSRichard LoweReturn a reference to documentation that should be consulted to understand how 52c10c16deSRichard Loweto use and interpret data from the performance counters. 53*53548f91SRobert Mustacchi.El 54*53548f91SRobert Mustacchi.Ss Performance Counter Access 55c10c16deSRichard LowePerformance counters can be present in hardware but not acccessible because 56c10c16deSRichard Loweeither some of the necessary system software components are not available or 57*53548f91SRobert Mustacchinot installed, or the counters might be in use by other processes. 58*53548f91SRobert MustacchiThe 59*53548f91SRobert Mustacchi.Xr cpc_open 3CPC 60*53548f91SRobert Mustacchifunction determines the accessibility of the counters and 61c10c16deSRichard Lowemust be invoked before any attempt to program the counters. 62*53548f91SRobert Mustacchi.Ss "Finding Events" 63c10c16deSRichard LoweEach different type of processor has its own set of events available for 64*53548f91SRobert Mustacchimeasurement. 65*53548f91SRobert MustacchiThe 66*53548f91SRobert Mustacchi.Xr cpc_walk_events_all 3CPC 67*53548f91SRobert Mustacchiand 68*53548f91SRobert Mustacchi.Xr cpc_walk_events_pic 3CPC 69*53548f91SRobert Mustacchifunctions allow an application to determine the 70*53548f91SRobert Mustacchinames of events supported by the underlying processor. 71*53548f91SRobert MustacchiA collection of generic, platform independent event names are defined by 72*53548f91SRobert Mustacchi.Xr generic_events 3CPC . 73c10c16deSRichard LoweEach generic event maps to an underlying hardware event specific to the 74*53548f91SRobert Mustacchiunderlying processor and any optional attributes. 75*53548f91SRobert MustacchiThe 76*53548f91SRobert Mustacchi.Xr cpc_walk_generic_events_all 3CPC 77*53548f91SRobert Mustacchiand 78*53548f91SRobert Mustacchi.Xr cpc_walk_generic_events_pic 3CPC 79*53548f91SRobert Mustacchifunctions allow an application to determine the generic events supported 80*53548f91SRobert Mustacchion the underlying platform. 81*53548f91SRobert Mustacchi.Ss Processor Specific Events 82*53548f91SRobert MustacchiManual pages specific to events for recent Intel processors are 83*53548f91SRobert Mustacchiavailable. 84*53548f91SRobert MustacchiThe following manual pages cover the following Intel processor models 85*53548f91SRobert Mustacchiwhich are listed in hexadecimal: 86*53548f91SRobert Mustacchi.Bl -tag -width Xr 87*53548f91SRobert Mustacchi.It Xr bdw_de_events 3CPC 88*53548f91SRobert MustacchiIntel Broadwell-DE events; covers model 56h. 89*53548f91SRobert Mustacchi.It Xr bdw_events 3CPC 90*53548f91SRobert MustacchiIntel Broadwell client events; covers models 3dh and 47h. 91*53548f91SRobert Mustacchi.It Xr bdx_events 3CPC 92*53548f91SRobert MustacchiIntel Broadwell server events; covers model 4fh. 93*53548f91SRobert Mustacchi.It Xr bnl_events 3CPC 94*53548f91SRobert MustacchiIntel Atom Bonnell events; covers models 35h, 36h, 27h, 26h, and 1ch. 95*53548f91SRobert Mustacchi.It Xr glm_events 3CPC 96*53548f91SRobert MustacchiIntel Goldmont SoC events; covers models 5fh and 5ch. 97*53548f91SRobert Mustacchi.It Xr glp_events 3CPC 98*53548f91SRobert MustacchiIntel Goldmont Plus SoC events; covers model 7ah. 99*53548f91SRobert Mustacchi.It Xr hsw_events 3CPC 100*53548f91SRobert MustacchiIntel Haswell client events; covers models 46h, 45h, and 3ch. 101*53548f91SRobert Mustacchi.It Xr hsx_events 3CPC 102*53548f91SRobert MustacchiIntel Haswell server events; covers model 3fh. 103*53548f91SRobert Mustacchi.It Xr ivb_events 3CPC 104*53548f91SRobert MustacchiIntel Ivy Bridge client events; covers model 3ah. 105*53548f91SRobert Mustacchi.It Xr ivt_events 3CPC 106*53548f91SRobert MustacchiIntel Ivy Bridge server events; covers model 3eh. 107*53548f91SRobert Mustacchi.It Xr jkt_events 3CPC 108*53548f91SRobert MustacchiIntel Sandy Bridge server events; covers model 2dh. 109*53548f91SRobert Mustacchi.It Xr nhm_ep_events 3CPC 110*53548f91SRobert MustacchiIntel Nehalem-EP events; covers models, 1ah, 1fh, and 1eh. 111*53548f91SRobert Mustacchi.It Xr nhm_ex_events 3CPC 112*53548f91SRobert MustacchiIntel Sandy Bridge server events; covers model 23h. 113*53548f91SRobert Mustacchi.It Xr skl_events 3CPC 114*53548f91SRobert MustacchiIntel Skylake client events; covers model 9eh, 8eh, 5e, and 4eh. 115*53548f91SRobert Mustacchi.It Xr skx_events 3CPC 116*53548f91SRobert MustacchiIntel Skylake server events; covers model 55h. 117*53548f91SRobert Mustacchi.It Xr slm_events 3CPC 118*53548f91SRobert MustacchiIntel Atom Silvermont events; covers models 4ch, 4dh, and 37h. 119*53548f91SRobert Mustacchi.It Xr snb_events 3CPC 120*53548f91SRobert MustacchiIntel Sandy Bridge Client events; covers model 2ah. 121*53548f91SRobert Mustacchi.It Xr wsm_ep_dp_events 3CPC 122*53548f91SRobert MustacchiIntel Westmere-EP-DP events; covers model 2ch. 123*53548f91SRobert Mustacchi.It Xr wsm_ep_sp_events 3CPC 124*53548f91SRobert MustacchiIntel Westmere-EP-SP events; covers model 25h. 125*53548f91SRobert Mustacchi.It Xr wsm_ex_events 3CPC 126*53548f91SRobert MustacchiIntel Westmere-EX events; covers model 2fh. 127*53548f91SRobert Mustacchi.El 128*53548f91SRobert Mustacchi.Ss Using Attributes 129c10c16deSRichard LoweSome processors have advanced performance counter capabilities that are 130*53548f91SRobert Mustacchiconfigured with attributes. 131*53548f91SRobert MustacchiThe 132*53548f91SRobert Mustacchi.Xr cpc_walk_attrs 3CPC 133*53548f91SRobert Mustacchifunction can be used to determine the names of attributes supported by 134*53548f91SRobert Mustacchithe underlying processor. 135*53548f91SRobert MustacchiThe documentation referenced by 136*53548f91SRobert Mustacchi.Xr cpc_cpuref 3CPC 137*53548f91SRobert Mustacchishould be consulted to understand the meaning of a processor's performance 138*53548f91SRobert Mustacchicounter attributes. 139*53548f91SRobert Mustacchi.Ss Performance Counter Context 140c10c16deSRichard LoweEach processor on the system possesses its own set of performance counter 141*53548f91SRobert Mustacchiregisters. 142*53548f91SRobert MustacchiFor a single process, it is often desirable to maintain the illusion 143c10c16deSRichard Lowethat the counters are an intrinsic part of that process (whichever processors 144c10c16deSRichard Loweit runs on), since this allows the events to be directly attributed to the 145c10c16deSRichard Loweprocess without having to make passive all other activity on the system. 146*53548f91SRobert Mustacchi.Pp 147*53548f91SRobert MustacchiTo achieve this behavior, the library associates 148*53548f91SRobert Mustacchi.Em performance counter context 149*53548f91SRobert Mustacchiwith each 150*53548f91SRobert Mustacchi.Sy LWP 151*53548f91SRobert Mustacchiin the process. 152*53548f91SRobert MustacchiThe context consists of a small amount of kernel memory to hold the counter 153*53548f91SRobert Mustacchivalues when the 154*53548f91SRobert Mustacchi.Sy BLWP 155*53548f91SRobert Mustacchiis not running, and some simple kernel functions to save and restore those counter 156*53548f91SRobert Mustacchivalues from and to the hardware registers when the 157*53548f91SRobert Mustacchi.Sy LWP 158*53548f91SRobert Mustacchiperforms a normal context switch. 159*53548f91SRobert MustacchiA process can only observe and manipulate its own copy of the 160c10c16deSRichard Loweperformance counter control and data registers. 161*53548f91SRobert Mustacchi.Ss Performance Counters \&In Other Processes 162c10c16deSRichard LoweThough applications can be modified to instrument themselves as demonstrated 163c10c16deSRichard Loweabove, it is frequently useful to be able to examine the behavior of an 164*53548f91SRobert Mustacchiexisting application without changing the source code. 165*53548f91SRobert MustacchiA separate library, 166*53548f91SRobert Mustacchi.Sy libpctx , 167*53548f91SRobert Mustacchiprovides a simple set of interfaces that use the facilities of 168*53548f91SRobert Mustacchi.Xr proc 4 169*53548f91SRobert Mustacchito control a target process, and together with functions in 170*53548f91SRobert Mustacchi.Sy libcpc , 171*53548f91SRobert Mustacchiallow 172*53548f91SRobert Mustacchi.Sy truss No -like 173*53548f91SRobert Mustacchitools to be constructed to measure the performance counters in other 174*53548f91SRobert Mustacchiapplications. 175*53548f91SRobert MustacchiAn example of one such application is 176*53548f91SRobert Mustacchi.Xr cputrack 1 . 177*53548f91SRobert Mustacchi.Pp 178*53548f91SRobert MustacchiThe functions in 179*53548f91SRobert Mustacchi.Sy libpctx 180*53548f91SRobert Mustacchiare independent of those in 181*53548f91SRobert Mustacchi.Sy libcpc . 182*53548f91SRobert MustacchiThese functions manage a process using an event-loop paradigm \(em that is, the 183c10c16deSRichard Loweexecution of certain system calls by the controlled process cause the library 184c10c16deSRichard Loweto stop the controlled process and execute callback functions in the context of 185*53548f91SRobert Mustacchithe controlling process. 186*53548f91SRobert MustacchiThese handlers can perform various operations on the target process using APIs 187*53548f91SRobert Mustacchiin 188*53548f91SRobert Mustacchi.Sy libpctx 189*53548f91SRobert Mustacchiand 190*53548f91SRobert Mustacchi.Sy libcpc 191*53548f91SRobert Mustacchithat consume 192*53548f91SRobert Mustacchi.Vt pctx_t 193*53548f91SRobert Mustacchihandles. 194*53548f91SRobert Mustacchi.Sh SEE ALSO 195*53548f91SRobert Mustacchi.Xr cputrack 1 , 196*53548f91SRobert Mustacchi.Xr cpustat 1M , 197*53548f91SRobert Mustacchi.Xr cpc_bind_curlwp 3CPC , 198*53548f91SRobert Mustacchi.Xr cpc_buf_create 3CPC , 199*53548f91SRobert Mustacchi.Xr cpc_enable 3CPC , 200*53548f91SRobert Mustacchi.Xr cpc_npic 3CPC , 201*53548f91SRobert Mustacchi.Xr cpc_open 3CPC , 202*53548f91SRobert Mustacchi.Xr cpc_set_create 3CPC , 203*53548f91SRobert Mustacchi.Xr cpc_seterrhndlr 3CPC , 204*53548f91SRobert Mustacchi.Xr generic_events 3CPC , 205*53548f91SRobert Mustacchi.Xr pctx_capture 3CPC , 206*53548f91SRobert Mustacchi.Xr pctx_set_events 3CPC , 207*53548f91SRobert Mustacchi.Xr libcpc 3LIB , 208*53548f91SRobert Mustacchi.Xr proc 4 209