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*5fc40de0SRobert Mustacchi.\" Copyright (c) 2019, Joyent, Inc. 6*5fc40de0SRobert Mustacchi.Dd January 8, 2019 753548f91SRobert Mustacchi.Dt CPC 3CPC 853548f91SRobert Mustacchi.Os 953548f91SRobert Mustacchi.Sh NAME 1053548f91SRobert Mustacchi.Nm cpc 1153548f91SRobert Mustacchi.Nd hardware performance counters 1253548f91SRobert Mustacchi.Sh DESCRIPTION 1353548f91SRobert MustacchiModern microprocessors contain 1453548f91SRobert Mustacchi.Em hardware performance counters 1553548f91SRobert 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 1853548f91SRobert Mustacchiof the processor. 1953548f91SRobert MustacchiThe counters can be configured to count user events, system 2053548f91SRobert Mustacchievents, or both. 2153548f91SRobert MustacchiData from the performance counters can be used to analyze and 22c10c16deSRichard Lowetune the behavior of software on a particular type of processor. 2353548f91SRobert 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. 2653548f91SRobert Mustacchi.Pp 2753548f91SRobert MustacchiThis manual page describes a set of APIs that allow illumos applications to use 2853548f91SRobert Mustacchithese counters. 2953548f91SRobert MustacchiApplications can measure their own behavior, the behavior of 30c10c16deSRichard Loweother applications, or the behavior of the whole system. 3153548f91SRobert Mustacchi.Ss Shared Counters or Private Counters 3253548f91SRobert MustacchiThere are two principal models for using these performance counters. 3353548f91SRobert MustacchiSome users of these statistics want to observe system-wide behavior. 3453548f91SRobert MustacchiOther users want to view the performance counters as part of the register set 3553548f91SRobert Mustacchiexported by each 3653548f91SRobert Mustacchi.Sy LWP . 3753548f91SRobert 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. 4053548f91SRobert Mustacchi.Ss Configuration Interfaces 41c10c16deSRichard LoweThe following configuration interfaces are provided: 4253548f91SRobert Mustacchi.Bl -tag -width Xr 4353548f91SRobert Mustacchi.It Xr cpc_open 3CPC 44c10c16deSRichard LoweCheck the version the application was compiled with against the version of the 4553548f91SRobert Mustacchi.It Xr cpc_cciname 3CPC 46c10c16deSRichard LoweReturn a printable string to describe the performance counters of the 47c10c16deSRichard Loweprocessor. 4853548f91SRobert Mustacchi.It Xr cpc_npic 3CPC 49c10c16deSRichard LoweReturn the number of performance counters on the processor. 5053548f91SRobert 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. 5353548f91SRobert Mustacchi.El 5453548f91SRobert 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 5753548f91SRobert Mustacchinot installed, or the counters might be in use by other processes. 5853548f91SRobert MustacchiThe 5953548f91SRobert Mustacchi.Xr cpc_open 3CPC 6053548f91SRobert Mustacchifunction determines the accessibility of the counters and 61c10c16deSRichard Lowemust be invoked before any attempt to program the counters. 6253548f91SRobert Mustacchi.Ss "Finding Events" 63c10c16deSRichard LoweEach different type of processor has its own set of events available for 6453548f91SRobert Mustacchimeasurement. 6553548f91SRobert MustacchiThe 6653548f91SRobert Mustacchi.Xr cpc_walk_events_all 3CPC 6753548f91SRobert Mustacchiand 6853548f91SRobert Mustacchi.Xr cpc_walk_events_pic 3CPC 6953548f91SRobert Mustacchifunctions allow an application to determine the 7053548f91SRobert Mustacchinames of events supported by the underlying processor. 7153548f91SRobert MustacchiA collection of generic, platform independent event names are defined by 7253548f91SRobert Mustacchi.Xr generic_events 3CPC . 73c10c16deSRichard LoweEach generic event maps to an underlying hardware event specific to the 7453548f91SRobert Mustacchiunderlying processor and any optional attributes. 7553548f91SRobert MustacchiThe 7653548f91SRobert Mustacchi.Xr cpc_walk_generic_events_all 3CPC 7753548f91SRobert Mustacchiand 7853548f91SRobert Mustacchi.Xr cpc_walk_generic_events_pic 3CPC 7953548f91SRobert Mustacchifunctions allow an application to determine the generic events supported 8053548f91SRobert Mustacchion the underlying platform. 8153548f91SRobert Mustacchi.Ss Processor Specific Events 8253548f91SRobert MustacchiManual pages specific to events for recent Intel processors are 8353548f91SRobert Mustacchiavailable. 8453548f91SRobert MustacchiThe following manual pages cover the following Intel processor models 8553548f91SRobert Mustacchiwhich are listed in hexadecimal: 8653548f91SRobert Mustacchi.Bl -tag -width Xr 8753548f91SRobert Mustacchi.It Xr bdw_de_events 3CPC 8853548f91SRobert MustacchiIntel Broadwell-DE events; covers model 56h. 8953548f91SRobert Mustacchi.It Xr bdw_events 3CPC 9053548f91SRobert MustacchiIntel Broadwell client events; covers models 3dh and 47h. 9153548f91SRobert Mustacchi.It Xr bdx_events 3CPC 9253548f91SRobert MustacchiIntel Broadwell server events; covers model 4fh. 9353548f91SRobert Mustacchi.It Xr bnl_events 3CPC 9453548f91SRobert MustacchiIntel Atom Bonnell events; covers models 35h, 36h, 27h, 26h, and 1ch. 95*5fc40de0SRobert Mustacchi.It Xr clx_events 3CPC 96*5fc40de0SRobert MustacchiIntel Cascade Lake server events; covers model 55h, steppings 5-fh. 9753548f91SRobert Mustacchi.It Xr glm_events 3CPC 9853548f91SRobert MustacchiIntel Goldmont SoC events; covers models 5fh and 5ch. 9953548f91SRobert Mustacchi.It Xr glp_events 3CPC 10053548f91SRobert MustacchiIntel Goldmont Plus SoC events; covers model 7ah. 10153548f91SRobert Mustacchi.It Xr hsw_events 3CPC 10253548f91SRobert MustacchiIntel Haswell client events; covers models 46h, 45h, and 3ch. 10353548f91SRobert Mustacchi.It Xr hsx_events 3CPC 10453548f91SRobert MustacchiIntel Haswell server events; covers model 3fh. 10553548f91SRobert Mustacchi.It Xr ivb_events 3CPC 10653548f91SRobert MustacchiIntel Ivy Bridge client events; covers model 3ah. 10753548f91SRobert Mustacchi.It Xr ivt_events 3CPC 10853548f91SRobert MustacchiIntel Ivy Bridge server events; covers model 3eh. 10953548f91SRobert Mustacchi.It Xr jkt_events 3CPC 11053548f91SRobert MustacchiIntel Sandy Bridge server events; covers model 2dh. 11153548f91SRobert Mustacchi.It Xr nhm_ep_events 3CPC 11253548f91SRobert MustacchiIntel Nehalem-EP events; covers models, 1ah, 1fh, and 1eh. 11353548f91SRobert Mustacchi.It Xr nhm_ex_events 3CPC 11453548f91SRobert MustacchiIntel Sandy Bridge server events; covers model 23h. 11553548f91SRobert Mustacchi.It Xr skl_events 3CPC 11653548f91SRobert MustacchiIntel Skylake client events; covers model 9eh, 8eh, 5e, and 4eh. 11753548f91SRobert Mustacchi.It Xr skx_events 3CPC 118*5fc40de0SRobert MustacchiIntel Skylake server events; covers model 55h, steppings 0-4h. 11953548f91SRobert Mustacchi.It Xr slm_events 3CPC 12053548f91SRobert MustacchiIntel Atom Silvermont events; covers models 4ch, 4dh, and 37h. 12153548f91SRobert Mustacchi.It Xr snb_events 3CPC 12253548f91SRobert MustacchiIntel Sandy Bridge Client events; covers model 2ah. 12353548f91SRobert Mustacchi.It Xr wsm_ep_dp_events 3CPC 12453548f91SRobert MustacchiIntel Westmere-EP-DP events; covers model 2ch. 12553548f91SRobert Mustacchi.It Xr wsm_ep_sp_events 3CPC 12653548f91SRobert MustacchiIntel Westmere-EP-SP events; covers model 25h. 12753548f91SRobert Mustacchi.It Xr wsm_ex_events 3CPC 12853548f91SRobert MustacchiIntel Westmere-EX events; covers model 2fh. 12953548f91SRobert Mustacchi.El 13053548f91SRobert Mustacchi.Ss Using Attributes 131c10c16deSRichard LoweSome processors have advanced performance counter capabilities that are 13253548f91SRobert Mustacchiconfigured with attributes. 13353548f91SRobert MustacchiThe 13453548f91SRobert Mustacchi.Xr cpc_walk_attrs 3CPC 13553548f91SRobert Mustacchifunction can be used to determine the names of attributes supported by 13653548f91SRobert Mustacchithe underlying processor. 13753548f91SRobert MustacchiThe documentation referenced by 13853548f91SRobert Mustacchi.Xr cpc_cpuref 3CPC 13953548f91SRobert Mustacchishould be consulted to understand the meaning of a processor's performance 14053548f91SRobert Mustacchicounter attributes. 14153548f91SRobert Mustacchi.Ss Performance Counter Context 142c10c16deSRichard LoweEach processor on the system possesses its own set of performance counter 14353548f91SRobert Mustacchiregisters. 14453548f91SRobert MustacchiFor a single process, it is often desirable to maintain the illusion 145c10c16deSRichard Lowethat the counters are an intrinsic part of that process (whichever processors 146c10c16deSRichard Loweit runs on), since this allows the events to be directly attributed to the 147c10c16deSRichard Loweprocess without having to make passive all other activity on the system. 14853548f91SRobert Mustacchi.Pp 14953548f91SRobert MustacchiTo achieve this behavior, the library associates 15053548f91SRobert Mustacchi.Em performance counter context 15153548f91SRobert Mustacchiwith each 15253548f91SRobert Mustacchi.Sy LWP 15353548f91SRobert Mustacchiin the process. 15453548f91SRobert MustacchiThe context consists of a small amount of kernel memory to hold the counter 15553548f91SRobert Mustacchivalues when the 15653548f91SRobert Mustacchi.Sy BLWP 15753548f91SRobert Mustacchiis not running, and some simple kernel functions to save and restore those counter 15853548f91SRobert Mustacchivalues from and to the hardware registers when the 15953548f91SRobert Mustacchi.Sy LWP 16053548f91SRobert Mustacchiperforms a normal context switch. 16153548f91SRobert MustacchiA process can only observe and manipulate its own copy of the 162c10c16deSRichard Loweperformance counter control and data registers. 16353548f91SRobert Mustacchi.Ss Performance Counters \&In Other Processes 164c10c16deSRichard LoweThough applications can be modified to instrument themselves as demonstrated 165c10c16deSRichard Loweabove, it is frequently useful to be able to examine the behavior of an 16653548f91SRobert Mustacchiexisting application without changing the source code. 16753548f91SRobert MustacchiA separate library, 16853548f91SRobert Mustacchi.Sy libpctx , 16953548f91SRobert Mustacchiprovides a simple set of interfaces that use the facilities of 17053548f91SRobert Mustacchi.Xr proc 4 17153548f91SRobert Mustacchito control a target process, and together with functions in 17253548f91SRobert Mustacchi.Sy libcpc , 17353548f91SRobert Mustacchiallow 17453548f91SRobert Mustacchi.Sy truss No -like 17553548f91SRobert Mustacchitools to be constructed to measure the performance counters in other 17653548f91SRobert Mustacchiapplications. 17753548f91SRobert MustacchiAn example of one such application is 17853548f91SRobert Mustacchi.Xr cputrack 1 . 17953548f91SRobert Mustacchi.Pp 18053548f91SRobert MustacchiThe functions in 18153548f91SRobert Mustacchi.Sy libpctx 18253548f91SRobert Mustacchiare independent of those in 18353548f91SRobert Mustacchi.Sy libcpc . 18453548f91SRobert MustacchiThese functions manage a process using an event-loop paradigm \(em that is, the 185c10c16deSRichard Loweexecution of certain system calls by the controlled process cause the library 186c10c16deSRichard Loweto stop the controlled process and execute callback functions in the context of 18753548f91SRobert Mustacchithe controlling process. 18853548f91SRobert MustacchiThese handlers can perform various operations on the target process using APIs 18953548f91SRobert Mustacchiin 19053548f91SRobert Mustacchi.Sy libpctx 19153548f91SRobert Mustacchiand 19253548f91SRobert Mustacchi.Sy libcpc 19353548f91SRobert Mustacchithat consume 19453548f91SRobert Mustacchi.Vt pctx_t 19553548f91SRobert Mustacchihandles. 19653548f91SRobert Mustacchi.Sh SEE ALSO 19753548f91SRobert Mustacchi.Xr cputrack 1 , 19853548f91SRobert Mustacchi.Xr cpustat 1M , 19953548f91SRobert Mustacchi.Xr cpc_bind_curlwp 3CPC , 20053548f91SRobert Mustacchi.Xr cpc_buf_create 3CPC , 20153548f91SRobert Mustacchi.Xr cpc_enable 3CPC , 20253548f91SRobert Mustacchi.Xr cpc_npic 3CPC , 20353548f91SRobert Mustacchi.Xr cpc_open 3CPC , 20453548f91SRobert Mustacchi.Xr cpc_set_create 3CPC , 20553548f91SRobert Mustacchi.Xr cpc_seterrhndlr 3CPC , 20653548f91SRobert Mustacchi.Xr generic_events 3CPC , 20753548f91SRobert Mustacchi.Xr pctx_capture 3CPC , 20853548f91SRobert Mustacchi.Xr pctx_set_events 3CPC , 20953548f91SRobert Mustacchi.Xr libcpc 3LIB , 21053548f91SRobert Mustacchi.Xr proc 4 211