1ebccf1e3SJoseph Koshy.\" Copyright (c) 2003-2005 Joseph Koshy 2ebccf1e3SJoseph Koshy.\" All rights reserved. 3ebccf1e3SJoseph Koshy.\" 4ebccf1e3SJoseph Koshy.\" Redistribution and use in source and binary forms, with or without 5ebccf1e3SJoseph Koshy.\" modification, are permitted provided that the following conditions 6ebccf1e3SJoseph Koshy.\" are met: 7ebccf1e3SJoseph Koshy.\" 1. Redistributions of source code must retain the above copyright 8ebccf1e3SJoseph Koshy.\" notice, this list of conditions and the following disclaimer. 9ebccf1e3SJoseph Koshy.\" 2. Redistributions in binary form must reproduce the above copyright 10ebccf1e3SJoseph Koshy.\" notice, this list of conditions and the following disclaimer in the 11ebccf1e3SJoseph Koshy.\" documentation and/or other materials provided with the distribution. 12ebccf1e3SJoseph Koshy.\" 13ebccf1e3SJoseph Koshy.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14ebccf1e3SJoseph Koshy.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15ebccf1e3SJoseph Koshy.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16ebccf1e3SJoseph Koshy.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17ebccf1e3SJoseph Koshy.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18ebccf1e3SJoseph Koshy.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19ebccf1e3SJoseph Koshy.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20ebccf1e3SJoseph Koshy.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21ebccf1e3SJoseph Koshy.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22ebccf1e3SJoseph Koshy.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23ebccf1e3SJoseph Koshy.\" SUCH DAMAGE. 24ebccf1e3SJoseph Koshy.\" 25ebccf1e3SJoseph Koshy.\" $FreeBSD$ 26ebccf1e3SJoseph Koshy.\" 27ebccf1e3SJoseph Koshy.Dd Apr 15, 2005 28ebccf1e3SJoseph Koshy.Dt HWPMC 4 29ebccf1e3SJoseph Koshy.Os 30ebccf1e3SJoseph Koshy.Sh NAME 31ebccf1e3SJoseph Koshy.Nm hwpmc 32ebccf1e3SJoseph Koshy.Nd Hardware performance monitoring counter support 33ebccf1e3SJoseph Koshy.Sh SYNOPSIS 34ebccf1e3SJoseph Koshy.Cd options PMC_HOOKS 35ebccf1e3SJoseph Koshy.br 36ebccf1e3SJoseph Koshy.Cd device hwpmc 37ebccf1e3SJoseph Koshy.Sh DESCRIPTION 38ebccf1e3SJoseph KoshyThe 39ebccf1e3SJoseph Koshy.Nm 40ebccf1e3SJoseph Koshydriver virtualizes the hardware performance monitoring facilities in 41ebccf1e3SJoseph Koshymodern CPUs and provides support for using these facilities from 42ebccf1e3SJoseph Koshyuser level processes. 43ebccf1e3SJoseph Koshy.Pp 44ebccf1e3SJoseph KoshyThe driver supports multi-processor systems. 45ebccf1e3SJoseph Koshy.Pp 46ebccf1e3SJoseph KoshyPMCs are allocated using the 47ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCALLOCATE 48ebccf1e3SJoseph Koshyrequest. 49ebccf1e3SJoseph KoshyA successful 50ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCALLOCATE 51ebccf1e3SJoseph Koshyrequest will return an integer handle (typically a small integer) to 52ebccf1e3SJoseph Koshythe requesting process. 53ebccf1e3SJoseph KoshySubsequent operations on the allocated PMC use this handle to denote 54ebccf1e3SJoseph Koshythe specific PMC. 55ebccf1e3SJoseph KoshyA process that has successfully allocated a PMC is termed an 56ebccf1e3SJoseph Koshy.Dq "owner process" . 57ebccf1e3SJoseph Koshy.Pp 58ebccf1e3SJoseph KoshyPMCs may be allocated to operate in process-private or in system-wide 59ebccf1e3SJoseph Koshymodes. 60ebccf1e3SJoseph Koshy.Bl -hang -width "XXXXXXXXXXXXXXX" 61ebccf1e3SJoseph Koshy.It Em Process-private 62ebccf1e3SJoseph KoshyIn process-private mode, a PMC is active only when a thread belonging 63ebccf1e3SJoseph Koshyto a process it is attached to is scheduled on a CPU. 64ebccf1e3SJoseph Koshy.It Em System-wide 65ebccf1e3SJoseph KoshyIn system-wide mode a PMC operates independently of processes and 66ebccf1e3SJoseph Koshymeasures hardware events for the system as a whole. 67ebccf1e3SJoseph Koshy.El 68ebccf1e3SJoseph Koshy.Pp 69ebccf1e3SJoseph KoshyThe 70ebccf1e3SJoseph Koshy.Nm 71ebccf1e3SJoseph Koshydriver supports the use of hardware PMCs for counting or for 72ebccf1e3SJoseph Koshysampling: 73ebccf1e3SJoseph Koshy.Bl -hang -width "XXXXXXXXX" 74ebccf1e3SJoseph Koshy.It Em Counting 75ebccf1e3SJoseph KoshyIn counting modes, the PMCs count hardware events. 76ebccf1e3SJoseph KoshyThese counts are retrievable using the 77ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCREAD 78ebccf1e3SJoseph Koshysystem call on all architectures, though some architectures like the 79ebccf1e3SJoseph Koshyx86 and amd64 offer faster methods of reading these counts. 80ebccf1e3SJoseph Koshy.It Em Sampling 81ebccf1e3SJoseph KoshyIn sampling modes, where PMCs are configured to sample the CPU 82ebccf1e3SJoseph Koshyinstruction pointer after a configurable number of hardware events 83ebccf1e3SJoseph Koshyhave been observed. 84ebccf1e3SJoseph KoshyThese instruction pointer samples are directed to a log file for 85ebccf1e3SJoseph Koshysubsequent analysis. 86ebccf1e3SJoseph Koshy.El 87ebccf1e3SJoseph Koshy.Pp 88ebccf1e3SJoseph KoshyThese modes of operation are orthogonal; a PMC may be configured to 89ebccf1e3SJoseph Koshyoperate in one of four modes: 90ebccf1e3SJoseph Koshy.Bl -tag -width indent 91ebccf1e3SJoseph Koshy.It Process-private, counting 92ebccf1e3SJoseph KoshyThese PMCs count hardware events whenever a thread in their attached process is 93ebccf1e3SJoseph Koshyscheduled on a CPU. 94ebccf1e3SJoseph KoshyThese PMCs normally count from zero, but the initial count may be 95ebccf1e3SJoseph Koshyset using the 96ebccf1e3SJoseph Koshy.Ic PMC_OP_SETCOUNT 97ebccf1e3SJoseph Koshyoperation. 98ebccf1e3SJoseph KoshyApplications can read the value of the PMC anytime using the 99ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRW 100ebccf1e3SJoseph Koshyoperation. 101ebccf1e3SJoseph Koshy.It Process-private, sampling 102ebccf1e3SJoseph KoshyThese PMCs sample the target processes instruction pointer after they 103ebccf1e3SJoseph Koshyhave seen the configured number of hardware events. 104ebccf1e3SJoseph KoshyThe PMCs only count events when a thread belonging to their attached 105ebccf1e3SJoseph Koshyprocess is active. 106ebccf1e3SJoseph KoshyThe desired frequency of sampling is set using the 107ebccf1e3SJoseph Koshy.Ic PMC_OP_SETCOUNT 108ebccf1e3SJoseph Koshyoperation prior to starting the PMC. 109ebccf1e3SJoseph KoshyLog files are configured using the 110ebccf1e3SJoseph Koshy.Ic PMC_OP_CONFIGURELOG 111ebccf1e3SJoseph Koshyoperation. 112ebccf1e3SJoseph Koshy.It System-wide, counting 113ebccf1e3SJoseph KoshyThese PMCs count hardware events seen by them independent of the 114ebccf1e3SJoseph Koshyprocesses that are executing. 115ebccf1e3SJoseph KoshyThe current count on these PMCs can be read using the 116ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRW 117ebccf1e3SJoseph Koshyrequest. 118ebccf1e3SJoseph KoshyThese PMCs normally count from zero, but the initial count may be 119ebccf1e3SJoseph Koshyset using the 120ebccf1e3SJoseph Koshy.Ic PMC_OP_SETCOUNT 121ebccf1e3SJoseph Koshyoperation. 122ebccf1e3SJoseph Koshy.It System-wide, sampling 123ebccf1e3SJoseph KoshyThese PMCs will periodically sample the instruction pointer of the CPU 124ebccf1e3SJoseph Koshythey are allocated on, and will write the sample to a log for further 125ebccf1e3SJoseph Koshyprocessing. 126ebccf1e3SJoseph KoshyThe desired frequency of sampling is set using the 127ebccf1e3SJoseph Koshy.Ic PMC_OP_SETCOUNT 128ebccf1e3SJoseph Koshyoperation prior to starting the PMC. 129ebccf1e3SJoseph KoshyLog files are configured using the 130ebccf1e3SJoseph Koshy.Ic PMC_OP_CONFIGURELOG 131ebccf1e3SJoseph Koshyoperation. 132ebccf1e3SJoseph Koshy.Pp 133ebccf1e3SJoseph KoshySystem-wide statistical sampling can only be enabled by a process with 134ebccf1e3SJoseph Koshysuper-user privileges. 135ebccf1e3SJoseph Koshy.El 136ebccf1e3SJoseph Koshy.Pp 137ebccf1e3SJoseph KoshyProcesses are allowed to allocate as many PMCs are the hardware and 138ebccf1e3SJoseph Koshycurrent operating conditions permit. 139ebccf1e3SJoseph KoshyProcesses may mix allocations of system-wide and process-private 140ebccf1e3SJoseph KoshyPMCs. 141ebccf1e3SJoseph KoshyMultiple processes are allowed to be concurrently using the facilities 142ebccf1e3SJoseph Koshyof the 143ebccf1e3SJoseph Koshy.Nm 144ebccf1e3SJoseph Koshydriver. 145ebccf1e3SJoseph Koshy.Pp 146ebccf1e3SJoseph KoshyAllocated PMCs are started using the 147ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCSTART 148ebccf1e3SJoseph Koshyoperation, and stopped using the 149ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCSTOP 150ebccf1e3SJoseph Koshyoperation. 151ebccf1e3SJoseph KoshyStopping and starting a PMC is permitted at any time the owner process 152ebccf1e3SJoseph Koshyhas a valid handle to the PMC. 153ebccf1e3SJoseph Koshy.Pp 154ebccf1e3SJoseph KoshyProcess-private PMCs need to be attached to a target process before 155ebccf1e3SJoseph Koshythey can be used. 156ebccf1e3SJoseph KoshyAttaching a process to a PMC is done using the 157ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCATTACH 158ebccf1e3SJoseph Koshyoperation. 159ebccf1e3SJoseph KoshyAn already attached PMC may be detached from its target process 160ebccf1e3SJoseph Koshyusing the converse 161ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCDETACH 162ebccf1e3SJoseph Koshyoperation. 163ebccf1e3SJoseph KoshyIssuing an 164ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCSTART 165ebccf1e3SJoseph Koshyoperation on an as yet unattached PMC will cause it to be attached 166ebccf1e3SJoseph Koshyto its owner process. 167ebccf1e3SJoseph KoshyThe following rules determine whether a given process may attach 168ebccf1e3SJoseph Koshya PMC to another target process: 169ebccf1e3SJoseph Koshy.Bl -bullet -compact 170ebccf1e3SJoseph Koshy.It 171ebccf1e3SJoseph KoshyA non-jailed process with super-user privileges is allowed to attach 172ebccf1e3SJoseph Koshyto any other process in the system. 173ebccf1e3SJoseph Koshy.It 174ebccf1e3SJoseph KoshyOther processes are only allowed to attach to targets that they would 175ebccf1e3SJoseph Koshybe able to attach to for debugging (as determined by 176ebccf1e3SJoseph Koshy.Xr p_candebug 9 ) . 177ebccf1e3SJoseph Koshy.El 178ebccf1e3SJoseph Koshy.Pp 179ebccf1e3SJoseph KoshyPMCs are released using 180ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRELEASE . 181ebccf1e3SJoseph KoshyAfter a successful 182ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRELEASE 183ebccf1e3SJoseph Koshyoperation the handle to the PMC will become invalid. 184ebccf1e3SJoseph Koshy.Ss MODIFIER FLAGS 185ebccf1e3SJoseph KoshyThe 186ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCALLOCATE 187ebccf1e3SJoseph Koshyoperation supports the following flags that modify the behavior 188ebccf1e3SJoseph Koshyof an allocated PMC: 189ebccf1e3SJoseph Koshy.Bl -tag -width indent 190ebccf1e3SJoseph Koshy.It Dv PMC_F_DESCENDANTS 191ebccf1e3SJoseph KoshyThis flag is valid only for a PMC being allocated in process-private 192ebccf1e3SJoseph Koshymode. 193ebccf1e3SJoseph KoshyIt signifies that the PMC will track hardware events for its 194ebccf1e3SJoseph Koshytarget process and the target's current and future descendants. 195ebccf1e3SJoseph Koshy.El 196ebccf1e3SJoseph Koshy.Ss SIGNALS 197ebccf1e3SJoseph KoshyThe 198ebccf1e3SJoseph Koshy.Nm 199ebccf1e3SJoseph Koshydriver may deliver signals to processes that have allocated PMCs: 200ebccf1e3SJoseph Koshy.Bl -tag -width indent 201ebccf1e3SJoseph Koshy.It Bq SIGIO 202ebccf1e3SJoseph KoshyA 203ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRW 204ebccf1e3SJoseph Koshyoperation was attempted on a process-private PMC that does not have 205ebccf1e3SJoseph Koshyattached target processes. 206ebccf1e3SJoseph Koshy.It Bq SIGBUS 207ebccf1e3SJoseph KoshyThe 208ebccf1e3SJoseph Koshy.Nm 209ebccf1e3SJoseph Koshydriver is being unloaded from the kernel. 210ebccf1e3SJoseph Koshy.El 211ebccf1e3SJoseph Koshy.Sh PROGRAMMING API 212ebccf1e3SJoseph KoshyThe recommended way for application programs to use the facilities of 213ebccf1e3SJoseph Koshythe 214ebccf1e3SJoseph Koshy.Nm 215ebccf1e3SJoseph Koshydriver is using the API provided by the library 216ebccf1e3SJoseph Koshy.Xr pmc 3 . 217ebccf1e3SJoseph Koshy.Pp 218ebccf1e3SJoseph KoshyThe 219ebccf1e3SJoseph Koshy.Nm 220ebccf1e3SJoseph Koshydriver operates using a system call number that is dynamically 221ebccf1e3SJoseph Koshyallotted to it when it is loaded into the kernel. 222ebccf1e3SJoseph Koshy.Pp 223ebccf1e3SJoseph KoshyThe 224ebccf1e3SJoseph Koshy.Nm 225ebccf1e3SJoseph Koshydriver supports the following operations: 226ebccf1e3SJoseph Koshy.Bl -tag -width indent 227ebccf1e3SJoseph Koshy.It Ic PMC_OP_CONFIGURELOG 228ebccf1e3SJoseph KoshyConfigure a log file for sampling mode PMCs. 229ebccf1e3SJoseph Koshy.It Ic PMC_OP_GETCPUINFO 230ebccf1e3SJoseph KoshyRetrieve information about the number of CPUs on the system and 231ebccf1e3SJoseph Koshythe number of hardware performance monitoring counters available per-CPU. 232ebccf1e3SJoseph Koshy.It Ic PMC_OP_GETDRIVERSTATS 233ebccf1e3SJoseph KoshyRetrieve module statistics (for analyzing the behavior of 234ebccf1e3SJoseph Koshy.Nm 235ebccf1e3SJoseph Koshyitself). 236ebccf1e3SJoseph Koshy.It Ic PMC_OP_GETMODULEVERSION 237ebccf1e3SJoseph KoshyRetrieve the version number of API. 238ebccf1e3SJoseph Koshy.It Ic PMC_OP_GETPMCINFO 239ebccf1e3SJoseph KoshyRetrieve information about the current state of the PMCs on a 240ebccf1e3SJoseph Koshygiven CPU. 241ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCADMIN 242ebccf1e3SJoseph KoshySet the administrative state (i.e., whether enabled or disabled) for 243ebccf1e3SJoseph Koshythe hardware PMCs managed by the 244ebccf1e3SJoseph Koshy.Nm 245ebccf1e3SJoseph Koshydriver. 246ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCALLOCATE 247ebccf1e3SJoseph KoshyAllocate and configure a PMC. 248ebccf1e3SJoseph KoshyOn successful allocation, a handle to the PMC (a small integer) 249ebccf1e3SJoseph Koshyis returned. 250ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCATTACH 251ebccf1e3SJoseph KoshyAttach a process mode PMC to a target process. 252ebccf1e3SJoseph KoshyThe PMC will be active whenever a thread in the target process is 253ebccf1e3SJoseph Koshyscheduled on a CPU. 254ebccf1e3SJoseph Koshy.Pp 255ebccf1e3SJoseph KoshyIf the 256ebccf1e3SJoseph Koshy.Dv PMC_F_DESCENDANTS 257ebccf1e3SJoseph Koshyflag had been specified at PMC allocation time, then the PMC is 258ebccf1e3SJoseph Koshyattached to all current and future descendants of the target process. 259ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCDETACH 260ebccf1e3SJoseph KoshyDetach a PMC from its target process. 261ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCRELEASE 262ebccf1e3SJoseph KoshyRelease a PMC. 263ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCRW 264ebccf1e3SJoseph KoshyRead and write a PMC. 265ebccf1e3SJoseph KoshyThis operation is valid only for PMCs configured in counting modes. 266ebccf1e3SJoseph Koshy.It Ic PMC_OP_SETCOUNT 267ebccf1e3SJoseph KoshySet the initial count (for counting mode PMCs) or the desired sampling 268ebccf1e3SJoseph Koshyrate (for sampling mode PMCs). 269ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCSTART 270ebccf1e3SJoseph KoshyStart a PMC. 271ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCSTOP 272ebccf1e3SJoseph KoshyStop a PMC. 273ebccf1e3SJoseph Koshy.It Ic PMC_OP_WRITELOG 274ebccf1e3SJoseph KoshyInsert a timestamped user record into the log file. 275ebccf1e3SJoseph Koshy.El 276ebccf1e3SJoseph Koshy.Ss i386 SPECIFIC API 277ebccf1e3SJoseph KoshySome i386 family CPUs support the RDPMC instruction which allows a 278ebccf1e3SJoseph Koshyuser process to read a PMC value without needing to invoke a 279ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRW 280ebccf1e3SJoseph Koshyoperation. 281ebccf1e3SJoseph KoshyOn such CPUs, the machine address associated with an allocated PMC is 282ebccf1e3SJoseph Koshyretrievable using the 283ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCX86GETMSR 284ebccf1e3SJoseph Koshysystem call. 285ebccf1e3SJoseph Koshy.Bl -tag -width indent 286ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCX86GETMSR 287ebccf1e3SJoseph KoshyRetrieve the MSR (machine specific register) number associated with 288ebccf1e3SJoseph Koshythe given PMC handle. 289ebccf1e3SJoseph Koshy.Pp 290c5153e19SJoseph KoshyThe PMC needs to be in process-private mode and allocated without the 291c5153e19SJoseph Koshy.Va PMC_F_DESCENDANTS 292c5153e19SJoseph Koshymodifier flag, and should be attached only to its owner process at the 293c5153e19SJoseph Koshytime of the call. 294ebccf1e3SJoseph Koshy.El 295ebccf1e3SJoseph Koshy.Ss amd64 SPECIFIC API 296ebccf1e3SJoseph KoshyAMD64 cpus support the RDPMC instruction which allows a 297ebccf1e3SJoseph Koshyuser process to read a PMC value without needing to invoke a 298ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRW 299ebccf1e3SJoseph Koshyoperation. 300ebccf1e3SJoseph KoshyThe machine address associated with an allocated PMC is 301ebccf1e3SJoseph Koshyretrievable using the 302ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCX86GETMSR 303ebccf1e3SJoseph Koshysystem call. 304ebccf1e3SJoseph Koshy.Bl -tag -width indent 305ebccf1e3SJoseph Koshy.It Ic PMC_OP_PMCX86GETMSR 306ebccf1e3SJoseph KoshyRetrieve the MSR (machine specific register) number associated with 307ebccf1e3SJoseph Koshythe given PMC handle. 308ebccf1e3SJoseph Koshy.Pp 309c5153e19SJoseph KoshyThe PMC needs to be in process-private mode and allocated without the 310c5153e19SJoseph Koshy.Va PMC_F_DESCENDANTS 311c5153e19SJoseph Koshymodifier flag, and should be attached only to its owner process at the 312c5153e19SJoseph Koshytime of the call. 313ebccf1e3SJoseph Koshy.El 314ebccf1e3SJoseph Koshy.Sh SYSCTL TUNABLES 315ebccf1e3SJoseph KoshyThe behavior of 316ebccf1e3SJoseph Koshy.Nm 317ebccf1e3SJoseph Koshyis influenced by the following 318ebccf1e3SJoseph Koshy.Xr sysctl 8 319ebccf1e3SJoseph Koshytunables: 320ebccf1e3SJoseph Koshy.Bl -tag -width indent 321ebccf1e3SJoseph Koshy.It Va kern.hwpmc.debugflags 322ebccf1e3SJoseph Koshy(Only available if the 323ebccf1e3SJoseph Koshy.Nm 324ebccf1e3SJoseph Koshydriver was compiled with 325ebccf1e3SJoseph Koshy.Fl DDEBUG ) . 326ebccf1e3SJoseph KoshyControl the verbosity of debug messages from the 327ebccf1e3SJoseph Koshy.Nm 328ebccf1e3SJoseph Koshydriver. 329ebccf1e3SJoseph Koshy.It Va kern.hwpmc.hashsize 330ebccf1e3SJoseph KoshyThe number of rows in the hash-tables used to keep track of owner and 331ebccf1e3SJoseph Koshytarget processes. 332ebccf1e3SJoseph Koshy.It Va kern.hwpmc.mtxpoolsize 333ebccf1e3SJoseph KoshyThe size of the spin mutex pool used by the PMC driver. 334ebccf1e3SJoseph Koshy.It Va kern.hwpmc.pcpubuffersize 335ebccf1e3SJoseph KoshyThe size of the per-cpu hash table used when performing system-wide 336ebccf1e3SJoseph Koshystatistical profiling. 337ebccf1e3SJoseph Koshy.It Va security.bsd.unprivileged_syspmcs 338ebccf1e3SJoseph KoshyIf set to non-zero, allow unprivileged processes to allocate system-wide 339ebccf1e3SJoseph KoshyPMCs. 340ebccf1e3SJoseph KoshyThe default value is 0. 341ebccf1e3SJoseph Koshy.It Va security.bsd.unprivileged_proc_debug 342ebccf1e3SJoseph KoshyIf set to 0, the 343ebccf1e3SJoseph Koshy.Nm 344ebccf1e3SJoseph Koshydriver will only allow privileged process to attach PMCs to other 345ebccf1e3SJoseph Koshyprocesses. 346ebccf1e3SJoseph Koshy.El 347ebccf1e3SJoseph Koshy.Pp 348ebccf1e3SJoseph KoshyThese variables may be set in the kernel environment using 349ebccf1e3SJoseph Koshy.Xr kenv 1 350ebccf1e3SJoseph Koshybefore 351ebccf1e3SJoseph Koshy.Nm 352ebccf1e3SJoseph Koshyis loaded. 353ebccf1e3SJoseph Koshy.Sh SECURITY CONSIDERATIONS 354ebccf1e3SJoseph KoshyPMCs may be used to monitor the actual behaviour of the system on hardware. 355ebccf1e3SJoseph KoshyIn situations where this constitutes an undesirable information leak, 356ebccf1e3SJoseph Koshythe following options are available: 357ebccf1e3SJoseph Koshy.Bl -enum 358ebccf1e3SJoseph Koshy.It 359ebccf1e3SJoseph KoshySet the 360ebccf1e3SJoseph Koshy.Xr sysctl 8 361ebccf1e3SJoseph Koshytunable 362ebccf1e3SJoseph Koshy.Va "security.bsd.unprivileged_syspmcs" 363ebccf1e3SJoseph Koshyto 0. 364ebccf1e3SJoseph Koshy.Pp 365ebccf1e3SJoseph KoshyThis ensures that unprivileged processes cannot allocate system-wide 366ebccf1e3SJoseph KoshyPMCs and thus cannot observe the hardware behavior of the system 367ebccf1e3SJoseph Koshyas a whole. 368ebccf1e3SJoseph Koshy.Pp 369ebccf1e3SJoseph KoshyThis tunable may also be set at boot time using 370ebccf1e3SJoseph Koshy.Xr loader 8 , 371ebccf1e3SJoseph Koshyor with 372ebccf1e3SJoseph Koshy.Xr kenv 1 373ebccf1e3SJoseph Koshyprior to loading the 374ebccf1e3SJoseph Koshy.Nm 375ebccf1e3SJoseph Koshydriver into the kernel. 376ebccf1e3SJoseph Koshy.It 377ebccf1e3SJoseph KoshySet the 378ebccf1e3SJoseph Koshy.Xr sysctl 8 379ebccf1e3SJoseph Koshytunable 380ebccf1e3SJoseph Koshy.Va "security.bsd.unprivileged_proc_debug" 381ebccf1e3SJoseph Koshyto 0. 382ebccf1e3SJoseph Koshy.Pp 383ebccf1e3SJoseph KoshyThis will ensure that an unprivileged process cannot attach a PMC 384ebccf1e3SJoseph Koshyto any process other than itself and thus cannot observe the hardware 385ebccf1e3SJoseph Koshybehavior of other processes with the same credentials. 386ebccf1e3SJoseph Koshy.El 387ebccf1e3SJoseph Koshy.Pp 388ebccf1e3SJoseph KoshySystem administrators should note that on IA-32 platforms 389ebccf1e3SJoseph Koshy.Fx 390ebccf1e3SJoseph Koshymakes the content of the IA-32 TSC counter available to all processes 391ebccf1e3SJoseph Koshyvia the RDTSC instruction. 392ebccf1e3SJoseph Koshy.Sh IMPLEMENTATION NOTES 393ebccf1e3SJoseph Koshy.Ss i386 TSC Handling 394ebccf1e3SJoseph KoshyHistorically, on the x86 architecture, 395ebccf1e3SJoseph Koshy.Fx 396ebccf1e3SJoseph Koshyhas permitted user processes running at a processor CPL of 3 to 397ebccf1e3SJoseph Koshyread the TSC using the RDTSC instruction. 398ebccf1e3SJoseph KoshyThe 399ebccf1e3SJoseph Koshy.Nm 400ebccf1e3SJoseph Koshydriver preserves this semantic. 401ebccf1e3SJoseph Koshy.Pp 402ebccf1e3SJoseph KoshyTSCs are treated as shared, read-only counters and hence are 403ebccf1e3SJoseph Koshyonly allowed to be allocated in system-wide counting mode. 404ebccf1e3SJoseph Koshy.Ss Intel P4/HTT Handling 405ebccf1e3SJoseph KoshyOn CPUs with HTT support, Intel P4 PMCs are capable of qualifying 406ebccf1e3SJoseph Koshyonly a subset of hardware events on a per-logical CPU basis. 407ebccf1e3SJoseph KoshyConsequently, if HTT is enabled on a system with Intel Pentium P4 408ebccf1e3SJoseph KoshyPMCs, then the 409ebccf1e3SJoseph Koshy.Nm 410ebccf1e3SJoseph Koshydriver will reject allocation requests for process-private PMCs that 411ebccf1e3SJoseph Koshyrequest counting of hardware events that cannot be counted separately 412ebccf1e3SJoseph Koshyfor each logical CPU. 413ebccf1e3SJoseph Koshy.Sh ERRORS 414ebccf1e3SJoseph KoshyAn command issued to the 415ebccf1e3SJoseph Koshy.Nm 416ebccf1e3SJoseph Koshydriver may fail with the following errors: 417ebccf1e3SJoseph Koshy.Bl -tag -width Er 418ebccf1e3SJoseph Koshy.It Bq Er EBUSY 419ebccf1e3SJoseph KoshyAn 420ebccf1e3SJoseph Koshy.Ic OP_CONFIGURELOG 421ebccf1e3SJoseph Koshyoperation was requested while an existing log was active. 422ebccf1e3SJoseph Koshy.It Bq Er EBUSY 423ebccf1e3SJoseph KoshyA 424ebccf1e3SJoseph Koshy.Ic DISABLE 425ebccf1e3SJoseph Koshyoperation was requested using the 426ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCADMIN 427ebccf1e3SJoseph Koshyrequest for a set of hardware resources currently in use for 428ebccf1e3SJoseph Koshyprocess-private PMCs. 429ebccf1e3SJoseph Koshy.It Bq Er EBUSY 430ebccf1e3SJoseph KoshyA 431ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCADMIN 432ebccf1e3SJoseph Koshyoperation was requested on an active system mode PMC. 433ebccf1e3SJoseph Koshy.It Bq Er EBUSY 434ebccf1e3SJoseph KoshyA 435ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCATTACH 436ebccf1e3SJoseph Koshyoperation was requested for a target process that already had another 437ebccf1e3SJoseph KoshyPMC using the same hardware resources attached to it. 438ebccf1e3SJoseph Koshy.It Bq Er EBUSY 439ebccf1e3SJoseph KoshyAn 440ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRW 441ebccf1e3SJoseph Koshyrequest writing a new value was issued on a PMC that was active. 442ebccf1e3SJoseph Koshy.It Bq Er EBUSY 443ebccf1e3SJoseph KoshyAn 444ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCSETCOUNT 445ebccf1e3SJoseph Koshyrequest was issued on a PMC that was active. 446ebccf1e3SJoseph Koshy.It Bq Er EEXIST 447ebccf1e3SJoseph KoshyA 448ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCATTACH 449ebccf1e3SJoseph Koshyrequest was reissued for a target process that already is the target 450ebccf1e3SJoseph Koshyof this PMC. 451ebccf1e3SJoseph Koshy.It Bq Er EFAULT 452ebccf1e3SJoseph KoshyA bad address was passed in to the driver. 453ebccf1e3SJoseph Koshy.It Bq Er EINVAL 454ebccf1e3SJoseph KoshyA process specified an invalid PMC handle. 455ebccf1e3SJoseph Koshy.It Bq Er EINVAL 456ebccf1e3SJoseph KoshyAn invalid CPU number was passed in for an 457ebccf1e3SJoseph Koshy.Ic PMC_OP_GETPMCINFO 458ebccf1e3SJoseph Koshyoperation. 459ebccf1e3SJoseph Koshy.It Bq Er EINVAL 460ebccf1e3SJoseph KoshyAn invalid CPU number was passed in for an 461ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCADMIN 462ebccf1e3SJoseph Koshyoperation. 463ebccf1e3SJoseph Koshy.It Bq Er EINVAL 464ebccf1e3SJoseph KoshyAn invalid operation request was passed in for an 465ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCADMIN 466ebccf1e3SJoseph Koshyoperation. 467ebccf1e3SJoseph Koshy.It Bq Er EINVAL 468ebccf1e3SJoseph KoshyAn invalid PMC id was passed in for an 469ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCADMIN 470ebccf1e3SJoseph Koshyoperation. 471ebccf1e3SJoseph Koshy.It Bq Er EINVAL 472ebccf1e3SJoseph KoshyA suitable PMC matching the parameters passed in to a 473ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCALLOCATE 474ebccf1e3SJoseph Koshyrequest could not be allocated. 475ebccf1e3SJoseph Koshy.It Bq Er EINVAL 476ebccf1e3SJoseph KoshyAn invalid PMC mode was requested during a 477ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCALLOCATE 478ebccf1e3SJoseph Koshyrequest. 479ebccf1e3SJoseph Koshy.It Bq Er EINVAL 480ebccf1e3SJoseph KoshyAn invalid CPU number was specified during a 481ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCALLOCATE 482ebccf1e3SJoseph Koshyrequest. 483ebccf1e3SJoseph Koshy.It Bq Er EINVAL 484ebccf1e3SJoseph KoshyA cpu other than 485ebccf1e3SJoseph Koshy.Li PMC_CPU_ANY 486ebccf1e3SJoseph Koshywas specified in a 487ebccf1e3SJoseph Koshy.Ic PMC_OP_ALLOCATE 488ebccf1e3SJoseph Koshyrequest for a process-private PMC. 489ebccf1e3SJoseph Koshy.It Bq Er EINVAL 490ebccf1e3SJoseph KoshyA cpu number of 491ebccf1e3SJoseph Koshy.Li PMC_CPU_ANY 492ebccf1e3SJoseph Koshywas specified in a 493ebccf1e3SJoseph Koshy.Ic PMC_OP_ALLOCATE 494ebccf1e3SJoseph Koshyrequest for a system-wide PMC. 495ebccf1e3SJoseph Koshy.It Bq Er EINVAL 496ebccf1e3SJoseph KoshyThe 497ebccf1e3SJoseph Koshy.Ar pm_flags 498ebccf1e3SJoseph Koshyargument to an 499ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCALLOCATE 500ebccf1e3SJoseph Koshyrequest contained unknown flags. 501ebccf1e3SJoseph Koshy.It Bq Er EINVAL 502ebccf1e3SJoseph KoshyA PMC allocated for system-wide operation was specified with a 503ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCATTACH 504ebccf1e3SJoseph Koshyrequest. 505ebccf1e3SJoseph Koshy.It Bq Er EINVAL 506ebccf1e3SJoseph KoshyThe 507ebccf1e3SJoseph Koshy.Ar pm_pid 508ebccf1e3SJoseph Koshyargument to a 509ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCATTACH 510ebccf1e3SJoseph Koshyrequest specified an illegal process id. 511ebccf1e3SJoseph Koshy.It Bq Er EINVAL 512ebccf1e3SJoseph KoshyA 513ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCDETACH 514ebccf1e3SJoseph Koshyrequest was issued for a PMC not attached to the target process. 515ebccf1e3SJoseph Koshy.It Bq Er EINVAL 516ebccf1e3SJoseph KoshyArgument 517ebccf1e3SJoseph Koshy.Ar pm_flags 518ebccf1e3SJoseph Koshyto a 519ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCRW 520ebccf1e3SJoseph Koshyrequest contained illegal flags. 521ebccf1e3SJoseph Koshy.It Bq Er EINVAL 522ebccf1e3SJoseph KoshyA 523ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCX86GETMSR 524c5153e19SJoseph Koshyoperation was requested for a PMC not in process-virtual mode, or 525c5153e19SJoseph Koshyfor a PMC that is not solely attached to its owner process, or for 526c5153e19SJoseph Koshya PMC that was allocated with flag 527c5153e19SJoseph Koshy.Va PMC_F_DESCENDANTS . 528ebccf1e3SJoseph Koshy.It Bq Er EINVAL 529ebccf1e3SJoseph Koshy(On Intel Pentium 4 CPUs with HTT support) An allocation request for 530ebccf1e3SJoseph Koshya process-private PMC was issued for an event that does not support 531ebccf1e3SJoseph Koshycounting on a per-logical CPU basis. 532ebccf1e3SJoseph Koshy.It Bq Er ENOMEM 533ebccf1e3SJoseph KoshyThe system was not able to allocate kernel memory. 534ebccf1e3SJoseph Koshy.It Bq Er ENOSYS 535ebccf1e3SJoseph Koshy(i386 architectures) A 536ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCX86GETMSR 537ebccf1e3SJoseph Koshyoperation was requested for hardware that does not support reading 538ebccf1e3SJoseph KoshyPMCs directly with the RDPMC instruction. 539ebccf1e3SJoseph Koshy.It Bq Er ENXIO 540ebccf1e3SJoseph KoshyAn 541ebccf1e3SJoseph Koshy.Ic OP_GETPMCINFO 542ebccf1e3SJoseph Koshyoperation was requested for a disabled CPU. 543ebccf1e3SJoseph Koshy.It Bq Er ENXIO 544ebccf1e3SJoseph KoshyA system-wide PMC on a disabled CPU was requested to be allocated with 545ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCALLOCATE . 546ebccf1e3SJoseph Koshy.It Bq Er ENXIO 547ebccf1e3SJoseph KoshyA 548ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCSTART 549ebccf1e3SJoseph Koshyor 550ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCSTOP 551ebccf1e3SJoseph Koshyrequest was issued for a system-wide PMC that was allocated on a 552ebccf1e3SJoseph Koshycurrently disabled CPU. 553ebccf1e3SJoseph Koshy.It Bq Er EPERM 554ebccf1e3SJoseph KoshyAn 555ebccf1e3SJoseph Koshy.Ic OP_PMCADMIN 556ebccf1e3SJoseph Koshyrequest was issued by a process without super-user 557ebccf1e3SJoseph Koshyprivilege or by a jailed super-user process. 558ebccf1e3SJoseph Koshy.It Bq Er EPERM 559ebccf1e3SJoseph KoshyAn 560ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCATTACH 561ebccf1e3SJoseph Koshyoperation was issued for a target process that the current process 562ebccf1e3SJoseph Koshydoes not have permission to attach to. 563c5153e19SJoseph Koshy.It Bq Er EPERM 564c5153e19SJoseph Koshy.Pq "i386 and amd64 architectures" 565c5153e19SJoseph KoshyAn 566c5153e19SJoseph Koshy.Ic PMC_OP_PMCATTACH 567c5153e19SJoseph Koshyoperation was issued on a PMC whose MSR has been retrieved using 568c5153e19SJoseph Koshy.Ic PMC_OP_PMCX86GETMSR . 569ebccf1e3SJoseph Koshy.It Bq Er ESRCH 570ebccf1e3SJoseph KoshyA process issued a PMC operation request without having allocated any PMCs. 571ebccf1e3SJoseph Koshy.It Bq Er ESRCH 572ebccf1e3SJoseph KoshyA 573ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCATTACH 574ebccf1e3SJoseph Koshyrequest specified a non-existent process id. 575ebccf1e3SJoseph Koshy.It Bq Er ESRCH 576ebccf1e3SJoseph KoshyThe target process for a 577ebccf1e3SJoseph Koshy.Ic PMC_OP_PMCDETACH 578ebccf1e3SJoseph Koshyoperation is not being monitored by the 579ebccf1e3SJoseph Koshy.Nm 580ebccf1e3SJoseph Koshydriver. 581ebccf1e3SJoseph Koshy.El 582ebccf1e3SJoseph Koshy.Sh BUGS 583ebccf1e3SJoseph KoshyThe kernel driver requires all CPUs in an SMP system to be symmetric 584ebccf1e3SJoseph Koshywith respect to their performance monitoring counter resources. 585ebccf1e3SJoseph Koshy.Pp 586ebccf1e3SJoseph KoshyThe driver samples the state of the kernel's logical processor support 587ebccf1e3SJoseph Koshyat the time of initialization (i.e., at module load time). 588ebccf1e3SJoseph KoshyOn CPUs supporting logical processors, the driver could misbehave if 589ebccf1e3SJoseph Koshylogical processors are subsequently enabled or disabled while the 590ebccf1e3SJoseph Koshydriver is active. 591ebccf1e3SJoseph Koshy.Sh SEE ALSO 592ebccf1e3SJoseph Koshy.Xr kenv 1 , 593ebccf1e3SJoseph Koshy.Xr pmc 3 , 594ebccf1e3SJoseph Koshy.Xr kldload 8 , 595ebccf1e3SJoseph Koshy.Xr pmccontrol 8 , 596ebccf1e3SJoseph Koshy.Xr pmcstat 8 , 597ebccf1e3SJoseph Koshy.Xr sysctl 8 , 598ebccf1e3SJoseph Koshy.Xr p_candebug 9 599