16455febbSJoseph Koshy.\" Copyright (c) 2003-2008 Joseph Koshy 2a07d350cSJoseph Koshy.\" Copyright (c) 2007 The FreeBSD Foundation 3ebccf1e3SJoseph Koshy.\" All rights reserved. 4ebccf1e3SJoseph Koshy.\" 5a07d350cSJoseph Koshy.\" Portions of this software were developed by A. Joseph Koshy under 6a07d350cSJoseph Koshy.\" sponsorship from the FreeBSD Foundation and Google, Inc. 7a07d350cSJoseph Koshy.\" 8ebccf1e3SJoseph Koshy.\" Redistribution and use in source and binary forms, with or without 9ebccf1e3SJoseph Koshy.\" modification, are permitted provided that the following conditions 10ebccf1e3SJoseph Koshy.\" are met: 11ebccf1e3SJoseph Koshy.\" 1. Redistributions of source code must retain the above copyright 12ebccf1e3SJoseph Koshy.\" notice, this list of conditions and the following disclaimer. 13ebccf1e3SJoseph Koshy.\" 2. Redistributions in binary form must reproduce the above copyright 14ebccf1e3SJoseph Koshy.\" notice, this list of conditions and the following disclaimer in the 15ebccf1e3SJoseph Koshy.\" documentation and/or other materials provided with the distribution. 16ebccf1e3SJoseph Koshy.\" 17ebccf1e3SJoseph Koshy.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18ebccf1e3SJoseph Koshy.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19ebccf1e3SJoseph Koshy.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20ebccf1e3SJoseph Koshy.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21ebccf1e3SJoseph Koshy.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22ebccf1e3SJoseph Koshy.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23ebccf1e3SJoseph Koshy.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24ebccf1e3SJoseph Koshy.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25ebccf1e3SJoseph Koshy.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26ebccf1e3SJoseph Koshy.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27ebccf1e3SJoseph Koshy.\" SUCH DAMAGE. 28ebccf1e3SJoseph Koshy.\" 29ebccf1e3SJoseph Koshy.\" $FreeBSD$ 30ebccf1e3SJoseph Koshy.\" 315aa1e55bSGordon Bergling.Dd April 3, 2021 32ebccf1e3SJoseph Koshy.Dt HWPMC 4 33ebccf1e3SJoseph Koshy.Os 34ebccf1e3SJoseph Koshy.Sh NAME 35ebccf1e3SJoseph Koshy.Nm hwpmc 36d9a5f346SJoseph Koshy.Nd "Hardware Performance Monitoring Counter support" 37ebccf1e3SJoseph Koshy.Sh SYNOPSIS 38d9a5f346SJoseph Koshy.Cd "options HWPMC_HOOKS" 39d9a5f346SJoseph Koshy.Cd "device hwpmc" 4079247772SJoseph Koshy.Pp 4179247772SJoseph KoshyAdditionally, for i386 systems: 4278ad5421SRuslan Ermilov.Cd "device apic" 43ebccf1e3SJoseph Koshy.Sh DESCRIPTION 44ebccf1e3SJoseph KoshyThe 45ebccf1e3SJoseph Koshy.Nm 46ebccf1e3SJoseph Koshydriver virtualizes the hardware performance monitoring facilities in 47ebccf1e3SJoseph Koshymodern CPUs and provides support for using these facilities from 48ebccf1e3SJoseph Koshyuser level processes. 49ebccf1e3SJoseph Koshy.Pp 50ebccf1e3SJoseph KoshyThe driver supports multi-processor systems. 51ebccf1e3SJoseph Koshy.Pp 52ebccf1e3SJoseph KoshyPMCs are allocated using the 53d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 54ebccf1e3SJoseph Koshyrequest. 55ebccf1e3SJoseph KoshyA successful 56d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 571a94fde8SJoseph Koshyrequest will return a handle to the requesting process. 58ebccf1e3SJoseph KoshySubsequent operations on the allocated PMC use this handle to denote 59ebccf1e3SJoseph Koshythe specific PMC. 60ebccf1e3SJoseph KoshyA process that has successfully allocated a PMC is termed an 61ebccf1e3SJoseph Koshy.Dq "owner process" . 62ebccf1e3SJoseph Koshy.Pp 631a94fde8SJoseph KoshyPMCs may be allocated with process or system scope. 641a94fde8SJoseph Koshy.Bl -tag -width ".Em Process-scope" 651a94fde8SJoseph Koshy.It Em "Process-scope" 661a94fde8SJoseph KoshyThe PMC is active only when a thread belonging 67ebccf1e3SJoseph Koshyto a process it is attached to is scheduled on a CPU. 681a94fde8SJoseph Koshy.It Em "System-scope" 691a94fde8SJoseph KoshyThe PMC operates independently of processes and 70ebccf1e3SJoseph Koshymeasures hardware events for the system as a whole. 71ebccf1e3SJoseph Koshy.El 72ebccf1e3SJoseph Koshy.Pp 731a94fde8SJoseph KoshyPMCs may be allocated for counting or for sampling: 74d9a5f346SJoseph Koshy.Bl -tag -width ".Em Counting" 756d6a103aSJoseph Koshy.It Em Counting 76ebccf1e3SJoseph KoshyIn counting modes, the PMCs count hardware events. 77ebccf1e3SJoseph KoshyThese counts are retrievable using the 78d9a5f346SJoseph Koshy.Dv PMC_OP_PMCREAD 794f00efe0SJoseph Koshysystem call on all architectures. 804f00efe0SJoseph KoshySome architectures offer faster methods of reading these counts. 81ebccf1e3SJoseph Koshy.It Em Sampling 824f00efe0SJoseph KoshyIn sampling modes, the PMCs are configured to sample the CPU 83a07d350cSJoseph Koshyinstruction pointer (and optionally to capture the call chain leading 84a07d350cSJoseph Koshyup to the sampled instruction pointer) after a configurable number of 85a07d350cSJoseph Koshyhardware events have been observed. 86a07d350cSJoseph KoshyInstruction pointer samples and call chain records are usually 87a07d350cSJoseph Koshydirected to a log file for subsequent analysis. 88ebccf1e3SJoseph Koshy.El 89ebccf1e3SJoseph Koshy.Pp 901a94fde8SJoseph KoshyScope and operational mode are orthogonal; a PMC may thus be 911a94fde8SJoseph Koshyconfigured to operate in one of the following four modes: 92ebccf1e3SJoseph Koshy.Bl -tag -width indent 931a94fde8SJoseph Koshy.It Process-scope, counting 94ebccf1e3SJoseph KoshyThese PMCs count hardware events whenever a thread in their attached process is 95ebccf1e3SJoseph Koshyscheduled on a CPU. 96ebccf1e3SJoseph KoshyThese PMCs normally count from zero, but the initial count may be 97ebccf1e3SJoseph Koshyset using the 98d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT 99ebccf1e3SJoseph Koshyoperation. 100ebccf1e3SJoseph KoshyApplications can read the value of the PMC anytime using the 101d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 102ebccf1e3SJoseph Koshyoperation. 1031a94fde8SJoseph Koshy.It Process-scope, sampling 104ebccf1e3SJoseph KoshyThese PMCs sample the target processes instruction pointer after they 105ebccf1e3SJoseph Koshyhave seen the configured number of hardware events. 106ebccf1e3SJoseph KoshyThe PMCs only count events when a thread belonging to their attached 107ebccf1e3SJoseph Koshyprocess is active. 108ebccf1e3SJoseph KoshyThe desired frequency of sampling is set using the 109d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT 110ebccf1e3SJoseph Koshyoperation prior to starting the PMC. 111ebccf1e3SJoseph KoshyLog files are configured using the 112d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 113ebccf1e3SJoseph Koshyoperation. 1141a94fde8SJoseph Koshy.It System-scope, counting 115ebccf1e3SJoseph KoshyThese PMCs count hardware events seen by them independent of the 116ebccf1e3SJoseph Koshyprocesses that are executing. 117ebccf1e3SJoseph KoshyThe current count on these PMCs can be read using the 118d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 119ebccf1e3SJoseph Koshyrequest. 120ebccf1e3SJoseph KoshyThese PMCs normally count from zero, but the initial count may be 121ebccf1e3SJoseph Koshyset using the 122d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT 123ebccf1e3SJoseph Koshyoperation. 1241a94fde8SJoseph Koshy.It System-scope, sampling 125ebccf1e3SJoseph KoshyThese PMCs will periodically sample the instruction pointer of the CPU 126ebccf1e3SJoseph Koshythey are allocated on, and will write the sample to a log for further 127ebccf1e3SJoseph Koshyprocessing. 128ebccf1e3SJoseph KoshyThe desired frequency of sampling is set using the 129d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT 130ebccf1e3SJoseph Koshyoperation prior to starting the PMC. 131ebccf1e3SJoseph KoshyLog files are configured using the 132d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 133ebccf1e3SJoseph Koshyoperation. 134ebccf1e3SJoseph Koshy.Pp 135ebccf1e3SJoseph KoshySystem-wide statistical sampling can only be enabled by a process with 136ebccf1e3SJoseph Koshysuper-user privileges. 137ebccf1e3SJoseph Koshy.El 138ebccf1e3SJoseph Koshy.Pp 1394f00efe0SJoseph KoshyProcesses are allowed to allocate as many PMCs as the hardware and 140ebccf1e3SJoseph Koshycurrent operating conditions permit. 141ebccf1e3SJoseph KoshyProcesses may mix allocations of system-wide and process-private 142ebccf1e3SJoseph KoshyPMCs. 14344caa872SJoseph KoshyMultiple processes may be using PMCs simultaneously. 144ebccf1e3SJoseph Koshy.Pp 145ebccf1e3SJoseph KoshyAllocated PMCs are started using the 146d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART 147ebccf1e3SJoseph Koshyoperation, and stopped using the 148d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTOP 149ebccf1e3SJoseph Koshyoperation. 150ebccf1e3SJoseph KoshyStopping and starting a PMC is permitted at any time the owner process 151ebccf1e3SJoseph Koshyhas a valid handle to the PMC. 152ebccf1e3SJoseph Koshy.Pp 153ebccf1e3SJoseph KoshyProcess-private PMCs need to be attached to a target process before 154ebccf1e3SJoseph Koshythey can be used. 155ebccf1e3SJoseph KoshyAttaching a process to a PMC is done using the 156d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 157ebccf1e3SJoseph Koshyoperation. 158ebccf1e3SJoseph KoshyAn already attached PMC may be detached from its target process 159ebccf1e3SJoseph Koshyusing the converse 160d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH 161ebccf1e3SJoseph Koshyoperation. 162d9a5f346SJoseph KoshyIssuing a 163d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART 164ebccf1e3SJoseph Koshyoperation on an as yet unattached PMC will cause it to be attached 165ebccf1e3SJoseph Koshyto its owner process. 166ebccf1e3SJoseph KoshyThe following rules determine whether a given process may attach 167ebccf1e3SJoseph Koshya PMC to another target process: 168ebccf1e3SJoseph Koshy.Bl -bullet -compact 169ebccf1e3SJoseph Koshy.It 170ebccf1e3SJoseph KoshyA non-jailed process with super-user privileges is allowed to attach 171ebccf1e3SJoseph Koshyto any other process in the system. 172ebccf1e3SJoseph Koshy.It 173ebccf1e3SJoseph KoshyOther processes are only allowed to attach to targets that they would 174ebccf1e3SJoseph Koshybe able to attach to for debugging (as determined by 175ebccf1e3SJoseph Koshy.Xr p_candebug 9 ) . 176ebccf1e3SJoseph Koshy.El 177ebccf1e3SJoseph Koshy.Pp 178ebccf1e3SJoseph KoshyPMCs are released using 179d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRELEASE . 180ebccf1e3SJoseph KoshyAfter a successful 181d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRELEASE 182ebccf1e3SJoseph Koshyoperation the handle to the PMC will become invalid. 183d9a5f346SJoseph Koshy.Ss Modifier Flags 184ebccf1e3SJoseph KoshyThe 185d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 186ebccf1e3SJoseph Koshyoperation supports the following flags that modify the behavior 187ebccf1e3SJoseph Koshyof an allocated PMC: 188d9a5f346SJoseph Koshy.Bl -tag -width indent 189a07d350cSJoseph Koshy.It Dv PMC_F_CALLCHAIN 190a07d350cSJoseph KoshyThis modifier informs sampling PMCs to record a callchain when 191a07d350cSJoseph Koshycapturing a sample. 192a07d350cSJoseph KoshyThe maximum depth to which call chains are recorded is specified 193a07d350cSJoseph Koshyby the 194a07d350cSJoseph Koshy.Va "kern.hwpmc.callchaindepth" 195a07d350cSJoseph Koshykernel tunable. 196ebccf1e3SJoseph Koshy.It Dv PMC_F_DESCENDANTS 197f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private 198ebccf1e3SJoseph Koshymode. 199ebccf1e3SJoseph KoshyIt signifies that the PMC will track hardware events for its 200ebccf1e3SJoseph Koshytarget process and the target's current and future descendants. 201f263522aSJoseph Koshy.It Dv PMC_F_LOG_PROCCSW 202f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private 203f263522aSJoseph Koshymode. 2043bc6c674SJoseph KoshyWhen this modifier is present, at every context switch, 205f263522aSJoseph Koshy.Nm 2063bc6c674SJoseph Koshywill log a record containing the number of hardware events 2073bc6c674SJoseph Koshyseen by the target process when it was scheduled on the CPU. 208f263522aSJoseph Koshy.It Dv PMC_F_LOG_PROCEXIT 209f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private 210f263522aSJoseph Koshymode. 211f263522aSJoseph KoshyWith this modifier present, 212f263522aSJoseph Koshy.Nm 213f263522aSJoseph Koshywill maintain per-process counts for each target process attached to 214f263522aSJoseph Koshya PMC. 215d9a5f346SJoseph KoshyAt process exit time, a record containing the target process' PID and 216f263522aSJoseph Koshythe accumulated per-process count for that process will be written to the 217f263522aSJoseph Koshyconfigured log file. 218ebccf1e3SJoseph Koshy.El 219d9a5f346SJoseph Koshy.Pp 220f263522aSJoseph KoshyModifiers 221f263522aSJoseph Koshy.Dv PMC_F_LOG_PROCEXIT 222f263522aSJoseph Koshyand 223f263522aSJoseph Koshy.Dv PMC_F_LOG_PROCCSW 224f263522aSJoseph Koshymay be used in combination with modifier 225f263522aSJoseph Koshy.Dv PMC_F_DESCENDANTS 2264f00efe0SJoseph Koshyto track the behavior of complex pipelines of processes. 2273bc6c674SJoseph KoshyPMCs with modifiers 2283bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCEXIT 2293bc6c674SJoseph Koshyand 2303bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCCSW 2313bc6c674SJoseph Koshycannot be started until their owner process has configured a log file. 232d9a5f346SJoseph Koshy.Ss Signals 233ebccf1e3SJoseph KoshyThe 234ebccf1e3SJoseph Koshy.Nm 235ebccf1e3SJoseph Koshydriver may deliver signals to processes that have allocated PMCs: 236d9a5f346SJoseph Koshy.Bl -tag -width ".Dv SIGBUS" 237d9a5f346SJoseph Koshy.It Dv SIGIO 238ebccf1e3SJoseph KoshyA 239d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 240ebccf1e3SJoseph Koshyoperation was attempted on a process-private PMC that does not have 241ebccf1e3SJoseph Koshyattached target processes. 242d9a5f346SJoseph Koshy.It Dv SIGBUS 243ebccf1e3SJoseph KoshyThe 244ebccf1e3SJoseph Koshy.Nm 245ebccf1e3SJoseph Koshydriver is being unloaded from the kernel. 246ebccf1e3SJoseph Koshy.El 2471a94fde8SJoseph Koshy.Ss PMC ROW DISPOSITIONS 2481a94fde8SJoseph KoshyA PMC row is defined as the set of PMC resources at the same hardware 2491a94fde8SJoseph Koshyaddress in the CPUs in a system. 2501a94fde8SJoseph KoshySince process scope PMCs need to move between CPUs following their 2511a94fde8SJoseph Koshytarget threads, allocation of a process scope PMC reserves all PMCs in 2521a94fde8SJoseph Koshya PMC row for use only with process scope PMCs. 2531a94fde8SJoseph KoshyAccordingly a PMC row will be in one of the following dispositions: 2541a94fde8SJoseph Koshy.Bl -tag -width ".Dv PMC_DISP_STANDALONE" -compact 2551a94fde8SJoseph Koshy.It Dv PMC_DISP_FREE 2561a94fde8SJoseph KoshyHardware counters in this row are free and may be use to satisfy 2571a94fde8SJoseph Koshyeither of system scope or process scope allocation requests. 2581a94fde8SJoseph Koshy.It Dv PMC_DISP_THREAD 2591a94fde8SJoseph KoshyHardware counters in this row are in use by process scope PMCs 2601a94fde8SJoseph Koshyand are only available for process scope allocation requests. 2611a94fde8SJoseph Koshy.It Dv PMC_DISP_STANDALONE 2621a94fde8SJoseph KoshySome hardware counters in this row have been administratively 2631a94fde8SJoseph Koshydisabled or are in use by system scope PMCs. 2641a94fde8SJoseph KoshyNon-disabled hardware counters in such a row may be used 2651a94fde8SJoseph Koshyfor satisfying system scope allocation requests. 2661a94fde8SJoseph KoshyNo process scope PMCs will use hardware counters in this row. 2671a94fde8SJoseph Koshy.El 268ebccf1e3SJoseph Koshy.Sh PROGRAMMING API 269ebccf1e3SJoseph KoshyThe recommended way for application programs to use the facilities of 270ebccf1e3SJoseph Koshythe 271ebccf1e3SJoseph Koshy.Nm 272d9a5f346SJoseph Koshydriver is using the API provided by the 273d9a5f346SJoseph Koshy.Xr pmc 3 274d9a5f346SJoseph Koshylibrary. 275ebccf1e3SJoseph Koshy.Pp 276ebccf1e3SJoseph KoshyThe 277ebccf1e3SJoseph Koshy.Nm 278ebccf1e3SJoseph Koshydriver operates using a system call number that is dynamically 279ebccf1e3SJoseph Koshyallotted to it when it is loaded into the kernel. 280ebccf1e3SJoseph Koshy.Pp 281ebccf1e3SJoseph KoshyThe 282ebccf1e3SJoseph Koshy.Nm 283ebccf1e3SJoseph Koshydriver supports the following operations: 284ebccf1e3SJoseph Koshy.Bl -tag -width indent 285d9a5f346SJoseph Koshy.It Dv PMC_OP_CONFIGURELOG 286a5c506d2SJoseph KoshyConfigure a log file for PMCs that require a log file. 287a5c506d2SJoseph KoshyThe 288a5c506d2SJoseph Koshy.Nm 289a5c506d2SJoseph Koshydriver will write log data to this file asynchronously. 290a5c506d2SJoseph KoshyIf it encounters an error, logging will be stopped and the error code 291a5c506d2SJoseph Koshyencountered will be saved for subsequent retrieval by a 292a5c506d2SJoseph Koshy.Dv PMC_OP_FLUSHLOG 293a5c506d2SJoseph Koshyrequest. 294d9a5f346SJoseph Koshy.It Dv PMC_OP_FLUSHLOG 295f263522aSJoseph KoshyTransfer buffered log data inside 296f263522aSJoseph Koshy.Nm 297f263522aSJoseph Koshyto a configured output file. 298f263522aSJoseph KoshyThis operation returns to the caller after the write operation 299f263522aSJoseph Koshyhas returned. 300a5c506d2SJoseph KoshyThe returned error code reflects any pending error state inside 301a5c506d2SJoseph Koshy.Nm . 302d9a5f346SJoseph Koshy.It Dv PMC_OP_GETCPUINFO 3036455febbSJoseph KoshyRetrieve information about the highest possible CPU number for the system, 3046455febbSJoseph Koshyand the number of hardware performance monitoring counters available per CPU. 305d9a5f346SJoseph Koshy.It Dv PMC_OP_GETDRIVERSTATS 306ebccf1e3SJoseph KoshyRetrieve module statistics (for analyzing the behavior of 307ebccf1e3SJoseph Koshy.Nm 308ebccf1e3SJoseph Koshyitself). 309d9a5f346SJoseph Koshy.It Dv PMC_OP_GETMODULEVERSION 310ebccf1e3SJoseph KoshyRetrieve the version number of API. 311d9a5f346SJoseph Koshy.It Dv PMC_OP_GETPMCINFO 312ebccf1e3SJoseph KoshyRetrieve information about the current state of the PMCs on a 313ebccf1e3SJoseph Koshygiven CPU. 314d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCADMIN 315ebccf1e3SJoseph KoshySet the administrative state (i.e., whether enabled or disabled) for 316ebccf1e3SJoseph Koshythe hardware PMCs managed by the 317ebccf1e3SJoseph Koshy.Nm 318ebccf1e3SJoseph Koshydriver. 3191a94fde8SJoseph KoshyThe invoking process needs to possess the 3201a94fde8SJoseph Koshy.Dv PRIV_PMC_MANAGE 3211a94fde8SJoseph Koshyprivilege. 322d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCALLOCATE 323ebccf1e3SJoseph KoshyAllocate and configure a PMC. 324e805d32bSJoseph KoshyOn successful allocation, a handle to the PMC (a 32 bit value) 325ebccf1e3SJoseph Koshyis returned. 326d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCATTACH 327ebccf1e3SJoseph KoshyAttach a process mode PMC to a target process. 328ebccf1e3SJoseph KoshyThe PMC will be active whenever a thread in the target process is 329ebccf1e3SJoseph Koshyscheduled on a CPU. 330ebccf1e3SJoseph Koshy.Pp 331ebccf1e3SJoseph KoshyIf the 332ebccf1e3SJoseph Koshy.Dv PMC_F_DESCENDANTS 333ebccf1e3SJoseph Koshyflag had been specified at PMC allocation time, then the PMC is 334ebccf1e3SJoseph Koshyattached to all current and future descendants of the target process. 335d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCDETACH 336ebccf1e3SJoseph KoshyDetach a PMC from its target process. 337d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCRELEASE 338ebccf1e3SJoseph KoshyRelease a PMC. 339d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCRW 340ebccf1e3SJoseph KoshyRead and write a PMC. 341ebccf1e3SJoseph KoshyThis operation is valid only for PMCs configured in counting modes. 342d9a5f346SJoseph Koshy.It Dv PMC_OP_SETCOUNT 343ebccf1e3SJoseph KoshySet the initial count (for counting mode PMCs) or the desired sampling 344ebccf1e3SJoseph Koshyrate (for sampling mode PMCs). 345d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCSTART 346ebccf1e3SJoseph KoshyStart a PMC. 347d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCSTOP 348ebccf1e3SJoseph KoshyStop a PMC. 349d9a5f346SJoseph Koshy.It Dv PMC_OP_WRITELOG 350ebccf1e3SJoseph KoshyInsert a timestamped user record into the log file. 351ebccf1e3SJoseph Koshy.El 352d9a5f346SJoseph Koshy.Ss i386 Specific API 353ebccf1e3SJoseph KoshySome i386 family CPUs support the RDPMC instruction which allows a 354ebccf1e3SJoseph Koshyuser process to read a PMC value without needing to invoke a 355d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 356ebccf1e3SJoseph Koshyoperation. 357ebccf1e3SJoseph KoshyOn such CPUs, the machine address associated with an allocated PMC is 358ebccf1e3SJoseph Koshyretrievable using the 359d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR 360ebccf1e3SJoseph Koshysystem call. 361ebccf1e3SJoseph Koshy.Bl -tag -width indent 362d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCX86GETMSR 363ebccf1e3SJoseph KoshyRetrieve the MSR (machine specific register) number associated with 364ebccf1e3SJoseph Koshythe given PMC handle. 365ebccf1e3SJoseph Koshy.Pp 366c5153e19SJoseph KoshyThe PMC needs to be in process-private mode and allocated without the 367d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS 368c5153e19SJoseph Koshymodifier flag, and should be attached only to its owner process at the 369c5153e19SJoseph Koshytime of the call. 370ebccf1e3SJoseph Koshy.El 371d9a5f346SJoseph Koshy.Ss amd64 Specific API 372d9a5f346SJoseph KoshyAMD64 CPUs support the RDPMC instruction which allows a 373ebccf1e3SJoseph Koshyuser process to read a PMC value without needing to invoke a 374d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 375ebccf1e3SJoseph Koshyoperation. 376ebccf1e3SJoseph KoshyThe machine address associated with an allocated PMC is 377ebccf1e3SJoseph Koshyretrievable using the 378d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR 379ebccf1e3SJoseph Koshysystem call. 380ebccf1e3SJoseph Koshy.Bl -tag -width indent 381d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCX86GETMSR 382ebccf1e3SJoseph KoshyRetrieve the MSR (machine specific register) number associated with 383ebccf1e3SJoseph Koshythe given PMC handle. 384ebccf1e3SJoseph Koshy.Pp 385c5153e19SJoseph KoshyThe PMC needs to be in process-private mode and allocated without the 386d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS 387c5153e19SJoseph Koshymodifier flag, and should be attached only to its owner process at the 388c5153e19SJoseph Koshytime of the call. 389ebccf1e3SJoseph Koshy.El 39044cb3ab0SHiroki Sato.Sh SYSCTL VARIABLES AND LOADER TUNABLES 391ebccf1e3SJoseph KoshyThe behavior of 392ebccf1e3SJoseph Koshy.Nm 393ebccf1e3SJoseph Koshyis influenced by the following 394ebccf1e3SJoseph Koshy.Xr sysctl 8 395f263522aSJoseph Koshyand 396f263522aSJoseph Koshy.Xr loader 8 397ebccf1e3SJoseph Koshytunables: 398ebccf1e3SJoseph Koshy.Bl -tag -width indent 399a07d350cSJoseph Koshy.It Va kern.hwpmc.callchaindepth Pq integer, read-only 400a07d350cSJoseph KoshyThe maximum number of call chain records to capture per sample. 401a07d350cSJoseph KoshyThe default is 8. 402f263522aSJoseph Koshy.It Va kern.hwpmc.debugflags Pq string, read-write 403ebccf1e3SJoseph Koshy(Only available if the 404ebccf1e3SJoseph Koshy.Nm 405ebccf1e3SJoseph Koshydriver was compiled with 406d9a5f346SJoseph Koshy.Fl DDEBUG . ) 407ebccf1e3SJoseph KoshyControl the verbosity of debug messages from the 408ebccf1e3SJoseph Koshy.Nm 409ebccf1e3SJoseph Koshydriver. 410f263522aSJoseph Koshy.It Va kern.hwpmc.hashsize Pq integer, read-only 411d9a5f346SJoseph KoshyThe number of rows in the hash tables used to keep track of owner and 412ebccf1e3SJoseph Koshytarget processes. 413f263522aSJoseph KoshyThe default is 16. 414f263522aSJoseph Koshy.It Va kern.hwpmc.logbuffersize Pq integer, read-only 415f263522aSJoseph KoshyThe size in kilobytes of each log buffer used by 416d9a5f346SJoseph Koshy.Nm Ns 's 417f263522aSJoseph Koshylogging function. 41824bae989SJoseph KoshyThe default buffer size is 4KB. 419*ca341f3cSJohn Baldwin.It Va kern.hwpmc.mincount Pq integer, read-write 420*ca341f3cSJohn BaldwinThe minimum sampling rate for sampling mode PMCs. 421*ca341f3cSJohn BaldwinThe default count is 1000 events. 422f263522aSJoseph Koshy.It Va kern.hwpmc.mtxpoolsize Pq integer, read-only 423ebccf1e3SJoseph KoshyThe size of the spin mutex pool used by the PMC driver. 424f263522aSJoseph KoshyThe default is 32. 4250939f965SPiotr Pawel Stefaniak.It Va kern.hwpmc.nbuffers_pcpu Pq integer, read-only 426f263522aSJoseph KoshyThe number of log buffers used by 427f263522aSJoseph Koshy.Nm 428f263522aSJoseph Koshyfor logging. 429ac6a9a8eSJim HarrisThe default is 64. 430f263522aSJoseph Koshy.It Va kern.hwpmc.nsamples Pq integer, read-only 431d9a5f346SJoseph KoshyThe number of entries in the per-CPU ring buffer used during sampling. 432ac6a9a8eSJim HarrisThe default is 512. 433f263522aSJoseph Koshy.It Va security.bsd.unprivileged_syspmcs Pq boolean, read-write 434ebccf1e3SJoseph KoshyIf set to non-zero, allow unprivileged processes to allocate system-wide 435ebccf1e3SJoseph KoshyPMCs. 436ebccf1e3SJoseph KoshyThe default value is 0. 437f263522aSJoseph Koshy.It Va security.bsd.unprivileged_proc_debug Pq boolean, read-write 438ebccf1e3SJoseph KoshyIf set to 0, the 439ebccf1e3SJoseph Koshy.Nm 440f263522aSJoseph Koshydriver will only allow privileged processes to attach PMCs to other 441ebccf1e3SJoseph Koshyprocesses. 442ebccf1e3SJoseph Koshy.El 443ebccf1e3SJoseph Koshy.Pp 444ebccf1e3SJoseph KoshyThese variables may be set in the kernel environment using 445ebccf1e3SJoseph Koshy.Xr kenv 1 446ebccf1e3SJoseph Koshybefore 447ebccf1e3SJoseph Koshy.Nm 448ebccf1e3SJoseph Koshyis loaded. 449ebccf1e3SJoseph Koshy.Sh IMPLEMENTATION NOTES 450f263522aSJoseph Koshy.Ss SMP Symmetry 451f263522aSJoseph KoshyThe kernel driver requires all physical CPUs in an SMP system to have 452f263522aSJoseph Koshyidentical performance monitoring counter hardware. 4536455febbSJoseph Koshy.Ss Sparse CPU Numbering 4546455febbSJoseph KoshyOn platforms that sparsely number CPUs and which support hot-plugging 4556455febbSJoseph Koshyof CPUs, requests that specify non-existent or disabled CPUs will fail 4566455febbSJoseph Koshywith an error. 4576455febbSJoseph KoshyApplications allocating system-scope PMCs need to be aware of 4586455febbSJoseph Koshythe possibility of such transient failures. 45979247772SJoseph Koshy.Ss x86 TSC Handling 460ebccf1e3SJoseph KoshyHistorically, on the x86 architecture, 461ebccf1e3SJoseph Koshy.Fx 462ebccf1e3SJoseph Koshyhas permitted user processes running at a processor CPL of 3 to 463ebccf1e3SJoseph Koshyread the TSC using the RDTSC instruction. 464ebccf1e3SJoseph KoshyThe 465ebccf1e3SJoseph Koshy.Nm 4664f00efe0SJoseph Koshydriver preserves this behavior. 467ebccf1e3SJoseph Koshy.Ss Intel P4/HTT Handling 468ebccf1e3SJoseph KoshyOn CPUs with HTT support, Intel P4 PMCs are capable of qualifying 469ebccf1e3SJoseph Koshyonly a subset of hardware events on a per-logical CPU basis. 470ebccf1e3SJoseph KoshyConsequently, if HTT is enabled on a system with Intel Pentium P4 471ebccf1e3SJoseph KoshyPMCs, then the 472ebccf1e3SJoseph Koshy.Nm 473ebccf1e3SJoseph Koshydriver will reject allocation requests for process-private PMCs that 474ebccf1e3SJoseph Koshyrequest counting of hardware events that cannot be counted separately 475ebccf1e3SJoseph Koshyfor each logical CPU. 476f263522aSJoseph Koshy.Ss Intel Pentium-Pro Handling 477d48fe4c5SJoseph KoshyWriting a value to the PMC MSRs found in Intel Pentium-Pro style PMCs 478f263522aSJoseph Koshy(found in 479f263522aSJoseph Koshy.Tn "Intel Pentium Pro" , 480f263522aSJoseph Koshy.Tn "Pentium II" , 481f263522aSJoseph Koshy.Tn "Pentium III" , 482f263522aSJoseph Koshy.Tn "Pentium M" 483f263522aSJoseph Koshyand 484f263522aSJoseph Koshy.Tn "Celeron" 485f263522aSJoseph Koshyprocessors) will replicate bit 31 of the 486f263522aSJoseph Koshyvalue being written into the upper 8 bits of the MSR, 487f263522aSJoseph Koshybringing down the usable width of these PMCs to 31 bits. 488f263522aSJoseph KoshyFor process-virtual PMCs, the 489f263522aSJoseph Koshy.Nm 490f263522aSJoseph Koshydriver implements a workaround in software and makes the corrected 64 491f263522aSJoseph Koshybit count available via the 492d9a5f346SJoseph Koshy.Dv PMC_OP_RW 493f263522aSJoseph Koshyoperation. 494f263522aSJoseph KoshyProcesses that intend to use RDPMC instructions directly or 495f263522aSJoseph Koshythat intend to write values larger than 2^31 into these PMCs with 496d9a5f346SJoseph Koshy.Dv PMC_OP_RW 497f263522aSJoseph Koshyneed to be aware of this hardware limitation. 498f263522aSJoseph Koshy.Sh DIAGNOSTICS 499f263522aSJoseph Koshy.Bl -diag 500953adc17SJoseph Koshy.It "hwpmc: [class/npmc/capabilities]..." 501953adc17SJoseph KoshyAnnounce the presence of 502953adc17SJoseph Koshy.Va npmc 503953adc17SJoseph KoshyPMCs of class 504953adc17SJoseph Koshy.Va class , 505953adc17SJoseph Koshywith capabilities described by bit string 506953adc17SJoseph Koshy.Va capabilities . 507953adc17SJoseph Koshy.It "hwpmc: kernel version (0x%x) does not match module version (0x%x)." 508953adc17SJoseph KoshyThe module loading process failed because a version mismatch was detected 509953adc17SJoseph Koshybetween the currently executing kernel and the module being loaded. 5103419317cSJoseph Koshy.It "hwpmc: this kernel has not been compiled with 'options HWPMC_HOOKS'." 5113419317cSJoseph KoshyThe module loading process failed because the currently executing kernel 5123419317cSJoseph Koshywas not configured with the required configuration option 51378ad5421SRuslan Ermilov.Dv HWPMC_HOOKS . 514d9a5f346SJoseph Koshy.It "hwpmc: tunable hashsize=%d must be greater than zero." 515f263522aSJoseph KoshyA negative value was supplied for tunable 516f263522aSJoseph Koshy.Va kern.hwpmc.hashsize . 517d9a5f346SJoseph Koshy.It "hwpmc: tunable logbuffersize=%d must be greater than zero." 518f263522aSJoseph KoshyA negative value was supplied for tunable 519f263522aSJoseph Koshy.Va kern.hwpmc.logbuffersize . 520d9a5f346SJoseph Koshy.It "hwpmc: tunable nlogbuffers=%d must be greater than zero." 521f263522aSJoseph KoshyA negative value was supplied for tunable 522f263522aSJoseph Koshy.Va kern.hwpmc.nlogbuffers . 523d9a5f346SJoseph Koshy.It "hwpmc: tunable nsamples=%d out of range." 524f263522aSJoseph KoshyThe value for tunable 525f263522aSJoseph Koshy.Va kern.hwpmc.nsamples 526f263522aSJoseph Koshywas negative or greater than 65535. 527f263522aSJoseph Koshy.El 5286d6a103aSJoseph Koshy.Sh COMPATIBILITY 5296d6a103aSJoseph KoshyThe 5306d6a103aSJoseph Koshy.Nm 5316d6a103aSJoseph Koshydriver is 5326d6a103aSJoseph Koshy.Ud 5336d6a103aSJoseph KoshyThe API and ABI documented in this manual page may change in 5346d6a103aSJoseph Koshythe future. 5356d6a103aSJoseph KoshyThe recommended method of accessing this driver is using the 5366d6a103aSJoseph Koshy.Xr pmc 3 5376d6a103aSJoseph KoshyAPI. 538ebccf1e3SJoseph Koshy.Sh ERRORS 539d9a5f346SJoseph KoshyA command issued to the 540ebccf1e3SJoseph Koshy.Nm 541ebccf1e3SJoseph Koshydriver may fail with the following errors: 542ebccf1e3SJoseph Koshy.Bl -tag -width Er 543a5c506d2SJoseph Koshy.It Bq Er EAGAIN 544a5c506d2SJoseph KoshyHelper process creation failed for a 545a5c506d2SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 546a5c506d2SJoseph Koshyrequest due to a temporary resource shortage in the kernel. 547ebccf1e3SJoseph Koshy.It Bq Er EBUSY 548d9a5f346SJoseph KoshyA 549d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 550ebccf1e3SJoseph Koshyoperation was requested while an existing log was active. 551ebccf1e3SJoseph Koshy.It Bq Er EBUSY 552d9a5f346SJoseph KoshyA DISABLE operation was requested using the 553d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 554ebccf1e3SJoseph Koshyrequest for a set of hardware resources currently in use for 555ebccf1e3SJoseph Koshyprocess-private PMCs. 556ebccf1e3SJoseph Koshy.It Bq Er EBUSY 557ebccf1e3SJoseph KoshyA 558d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 559ebccf1e3SJoseph Koshyoperation was requested on an active system mode PMC. 560ebccf1e3SJoseph Koshy.It Bq Er EBUSY 561ebccf1e3SJoseph KoshyA 562d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 563ebccf1e3SJoseph Koshyoperation was requested for a target process that already had another 564ebccf1e3SJoseph KoshyPMC using the same hardware resources attached to it. 565ebccf1e3SJoseph Koshy.It Bq Er EBUSY 566d9a5f346SJoseph KoshyA 567d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 568ebccf1e3SJoseph Koshyrequest writing a new value was issued on a PMC that was active. 569953adc17SJoseph Koshy.It Bq Er EBUSY 570953adc17SJoseph KoshyA 571953adc17SJoseph Koshy.Dv PMC_OP_PMCSETCOUNT 572953adc17SJoseph Koshyrequest was issued on a PMC that was active. 5733bc6c674SJoseph Koshy.It Bq Er EDOOFUS 5743bc6c674SJoseph KoshyA 575d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART 5763bc6c674SJoseph Koshyoperation was requested without a log file being configured for a 5773bc6c674SJoseph KoshyPMC allocated with 5783bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCCSW 5793bc6c674SJoseph Koshyand 5803bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCEXIT 5813bc6c674SJoseph Koshymodifiers. 5821e999888SJoseph Koshy.It Bq Er EDOOFUS 5831e999888SJoseph KoshyA 5841e999888SJoseph Koshy.Dv PMC_OP_PMCSTART 5851e999888SJoseph Koshyoperation was requested on a system-wide sampling PMC without a log 5861e999888SJoseph Koshyfile being configured. 587ebccf1e3SJoseph Koshy.It Bq Er EEXIST 588ebccf1e3SJoseph KoshyA 589d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 590ebccf1e3SJoseph Koshyrequest was reissued for a target process that already is the target 591ebccf1e3SJoseph Koshyof this PMC. 592ebccf1e3SJoseph Koshy.It Bq Er EFAULT 593ebccf1e3SJoseph KoshyA bad address was passed in to the driver. 594ebccf1e3SJoseph Koshy.It Bq Er EINVAL 595e805d32bSJoseph KoshyAn invalid PMC handle was specified. 596ebccf1e3SJoseph Koshy.It Bq Er EINVAL 597d9a5f346SJoseph KoshyAn invalid CPU number was passed in for a 598d9a5f346SJoseph Koshy.Dv PMC_OP_GETPMCINFO 599ebccf1e3SJoseph Koshyoperation. 600ebccf1e3SJoseph Koshy.It Bq Er EINVAL 601a5c506d2SJoseph KoshyA 602a5c506d2SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 603a5c506d2SJoseph Koshyrequest to de-configure a log file was issued without a log file 604a5c506d2SJoseph Koshybeing configured. 605a5c506d2SJoseph Koshy.It Bq Er EINVAL 606a5c506d2SJoseph KoshyA 607a5c506d2SJoseph Koshy.Dv PMC_OP_FLUSHLOG 608a5c506d2SJoseph Koshyrequest was issued without a log file being configured. 609a5c506d2SJoseph Koshy.It Bq Er EINVAL 610d9a5f346SJoseph KoshyAn invalid CPU number was passed in for a 611d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 612ebccf1e3SJoseph Koshyoperation. 613ebccf1e3SJoseph Koshy.It Bq Er EINVAL 614d9a5f346SJoseph KoshyAn invalid operation request was passed in for a 615d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 616ebccf1e3SJoseph Koshyoperation. 617ebccf1e3SJoseph Koshy.It Bq Er EINVAL 618d9a5f346SJoseph KoshyAn invalid PMC ID was passed in for a 619d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 620ebccf1e3SJoseph Koshyoperation. 621ebccf1e3SJoseph Koshy.It Bq Er EINVAL 622ebccf1e3SJoseph KoshyA suitable PMC matching the parameters passed in to a 623d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 624ebccf1e3SJoseph Koshyrequest could not be allocated. 625ebccf1e3SJoseph Koshy.It Bq Er EINVAL 626ebccf1e3SJoseph KoshyAn invalid PMC mode was requested during a 627d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 628ebccf1e3SJoseph Koshyrequest. 629ebccf1e3SJoseph Koshy.It Bq Er EINVAL 630ebccf1e3SJoseph KoshyAn invalid CPU number was specified during a 631d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 632ebccf1e3SJoseph Koshyrequest. 633ebccf1e3SJoseph Koshy.It Bq Er EINVAL 634d9a5f346SJoseph KoshyA CPU other than 635d9a5f346SJoseph Koshy.Dv PMC_CPU_ANY 636ebccf1e3SJoseph Koshywas specified in a 637a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 638ebccf1e3SJoseph Koshyrequest for a process-private PMC. 639ebccf1e3SJoseph Koshy.It Bq Er EINVAL 640d9a5f346SJoseph KoshyA CPU number of 641d9a5f346SJoseph Koshy.Dv PMC_CPU_ANY 642ebccf1e3SJoseph Koshywas specified in a 643a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 644ebccf1e3SJoseph Koshyrequest for a system-wide PMC. 645ebccf1e3SJoseph Koshy.It Bq Er EINVAL 646ebccf1e3SJoseph KoshyThe 647ebccf1e3SJoseph Koshy.Ar pm_flags 648ebccf1e3SJoseph Koshyargument to an 649d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 650ebccf1e3SJoseph Koshyrequest contained unknown flags. 651ebccf1e3SJoseph Koshy.It Bq Er EINVAL 652a5c506d2SJoseph Koshy(On Intel Pentium 4 CPUs with HTT support) 653a5c506d2SJoseph KoshyA 654a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 655a5c506d2SJoseph Koshyrequest for a process-private PMC was issued for an event that does 656a5c506d2SJoseph Koshynot support counting on a per-logical CPU basis. 657a5c506d2SJoseph Koshy.It Bq Er EINVAL 658ebccf1e3SJoseph KoshyA PMC allocated for system-wide operation was specified with a 659d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 660a5c506d2SJoseph Koshyor 661a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH 662ebccf1e3SJoseph Koshyrequest. 663ebccf1e3SJoseph Koshy.It Bq Er EINVAL 664ebccf1e3SJoseph KoshyThe 665ebccf1e3SJoseph Koshy.Ar pm_pid 666ebccf1e3SJoseph Koshyargument to a 667d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 668a5c506d2SJoseph Koshyor 669a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH 670d9a5f346SJoseph Koshyrequest specified an illegal process ID. 671ebccf1e3SJoseph Koshy.It Bq Er EINVAL 672ebccf1e3SJoseph KoshyA 673d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH 674ebccf1e3SJoseph Koshyrequest was issued for a PMC not attached to the target process. 675ebccf1e3SJoseph Koshy.It Bq Er EINVAL 676ebccf1e3SJoseph KoshyArgument 677ebccf1e3SJoseph Koshy.Ar pm_flags 678ebccf1e3SJoseph Koshyto a 679d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 680ebccf1e3SJoseph Koshyrequest contained illegal flags. 681ebccf1e3SJoseph Koshy.It Bq Er EINVAL 682ebccf1e3SJoseph KoshyA 683d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR 684c5153e19SJoseph Koshyoperation was requested for a PMC not in process-virtual mode, or 685c5153e19SJoseph Koshyfor a PMC that is not solely attached to its owner process, or for 686c5153e19SJoseph Koshya PMC that was allocated with flag 687d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS . 688ebccf1e3SJoseph Koshy.It Bq Er EINVAL 689a5c506d2SJoseph KoshyA 690a5c506d2SJoseph Koshy.Dv PMC_OP_WRITELOG 691a5c506d2SJoseph Koshyrequest was issued for an owner process without a log file 692a5c506d2SJoseph Koshyconfigured. 693ebccf1e3SJoseph Koshy.It Bq Er ENOMEM 694ebccf1e3SJoseph KoshyThe system was not able to allocate kernel memory. 695ebccf1e3SJoseph Koshy.It Bq Er ENOSYS 696a5c506d2SJoseph Koshy(On i386 and amd64 architectures) 697d9a5f346SJoseph KoshyA 698d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR 699ebccf1e3SJoseph Koshyoperation was requested for hardware that does not support reading 700ebccf1e3SJoseph KoshyPMCs directly with the RDPMC instruction. 701ebccf1e3SJoseph Koshy.It Bq Er ENXIO 702d9a5f346SJoseph KoshyA 703d9a5f346SJoseph Koshy.Dv PMC_OP_GETPMCINFO 7046455febbSJoseph Koshyoperation was requested for an absent or disabled CPU. 705ebccf1e3SJoseph Koshy.It Bq Er ENXIO 7066455febbSJoseph KoshyA 7076455febbSJoseph Koshy.Dv PMC_OP_PMCALLOCATE 7086455febbSJoseph Koshyoperation specified allocation of a system-wide PMC on an absent or 7096455febbSJoseph Koshydisabled CPU. 710ebccf1e3SJoseph Koshy.It Bq Er ENXIO 711ebccf1e3SJoseph KoshyA 712d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART 713ebccf1e3SJoseph Koshyor 714d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTOP 7156455febbSJoseph Koshyrequest was issued for a system-wide PMC that was allocated on a CPU 7166455febbSJoseph Koshythat is currently absent or disabled. 717953adc17SJoseph Koshy.It Bq Er EOPNOTSUPP 718953adc17SJoseph KoshyA 719953adc17SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 720953adc17SJoseph Koshyrequest was issued for PMC capabilities not supported 721953adc17SJoseph Koshyby the specified PMC class. 722fe5ca00aSJoseph Koshy.It Bq Er EOPNOTSUPP 723fe5ca00aSJoseph Koshy(i386 architectures) 724fe5ca00aSJoseph KoshyA sampling mode PMC was requested on a CPU lacking an APIC. 725ebccf1e3SJoseph Koshy.It Bq Er EPERM 726d9a5f346SJoseph KoshyA 727d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 728ebccf1e3SJoseph Koshyrequest was issued by a process without super-user 729ebccf1e3SJoseph Koshyprivilege or by a jailed super-user process. 730ebccf1e3SJoseph Koshy.It Bq Er EPERM 731d9a5f346SJoseph KoshyA 732d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 733ebccf1e3SJoseph Koshyoperation was issued for a target process that the current process 734ebccf1e3SJoseph Koshydoes not have permission to attach to. 735c5153e19SJoseph Koshy.It Bq Er EPERM 736d9a5f346SJoseph Koshy(i386 and amd64 architectures) 737d9a5f346SJoseph KoshyA 738d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 739c5153e19SJoseph Koshyoperation was issued on a PMC whose MSR has been retrieved using 740d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR . 741ebccf1e3SJoseph Koshy.It Bq Er ESRCH 742f263522aSJoseph KoshyA process issued a PMC operation request without having allocated any 743f263522aSJoseph KoshyPMCs. 744f263522aSJoseph Koshy.It Bq Er ESRCH 745f263522aSJoseph KoshyA process issued a PMC operation request after the PMC was detached 746f263522aSJoseph Koshyfrom all of its target processes. 747ebccf1e3SJoseph Koshy.It Bq Er ESRCH 748ebccf1e3SJoseph KoshyA 749d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 750a5c506d2SJoseph Koshyor 751a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH 752d9a5f346SJoseph Koshyrequest specified a non-existent process ID. 753ebccf1e3SJoseph Koshy.It Bq Er ESRCH 754ebccf1e3SJoseph KoshyThe target process for a 755d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH 756a5c506d2SJoseph Koshyoperation is not being monitored by 757a5c506d2SJoseph Koshy.Nm . 758ebccf1e3SJoseph Koshy.El 759ebccf1e3SJoseph Koshy.Sh SEE ALSO 760ebccf1e3SJoseph Koshy.Xr kenv 1 , 761ebccf1e3SJoseph Koshy.Xr pmc 3 , 7626d6a103aSJoseph Koshy.Xr pmclog 3 , 763ebccf1e3SJoseph Koshy.Xr kldload 8 , 764ebccf1e3SJoseph Koshy.Xr pmccontrol 8 , 765ebccf1e3SJoseph Koshy.Xr pmcstat 8 , 766ebccf1e3SJoseph Koshy.Xr sysctl 8 , 767a5c506d2SJoseph Koshy.Xr kproc_create 9 , 768ebccf1e3SJoseph Koshy.Xr p_candebug 9 7696d6a103aSJoseph Koshy.Sh HISTORY 7706d6a103aSJoseph KoshyThe 7716d6a103aSJoseph Koshy.Nm 7726d6a103aSJoseph Koshydriver first appeared in 7736d6a103aSJoseph Koshy.Fx 6.0 . 774e805d32bSJoseph Koshy.Sh AUTHORS 775e805d32bSJoseph KoshyThe 776e805d32bSJoseph Koshy.Nm 777e805d32bSJoseph Koshydriver was written by 7786c899950SBaptiste Daroussin.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org . 779d9a5f346SJoseph Koshy.Sh BUGS 780d9a5f346SJoseph KoshyThe driver samples the state of the kernel's logical processor support 781d9a5f346SJoseph Koshyat the time of initialization (i.e., at module load time). 782d9a5f346SJoseph KoshyOn CPUs supporting logical processors, the driver could misbehave if 783d9a5f346SJoseph Koshylogical processors are subsequently enabled or disabled while the 784d9a5f346SJoseph Koshydriver is active. 785953adc17SJoseph Koshy.Pp 78679247772SJoseph KoshyOn the i386 architecture, the driver requires that the local APIC on the 787953adc17SJoseph KoshyCPU be enabled for sampling mode to be supported. 788953adc17SJoseph KoshyMany single-processor motherboards keep the APIC disabled in BIOS; on 789953adc17SJoseph Koshysuch systems 790953adc17SJoseph Koshy.Nm 791953adc17SJoseph Koshywill not support sampling PMCs. 7920afc94c1SUlrich Spörlein.Sh SECURITY CONSIDERATIONS 7930afc94c1SUlrich SpörleinPMCs may be used to monitor the actual behavior of the system on hardware. 7940afc94c1SUlrich SpörleinIn situations where this constitutes an undesirable information leak, 7950afc94c1SUlrich Spörleinthe following options are available: 7960afc94c1SUlrich Spörlein.Bl -enum 7970afc94c1SUlrich Spörlein.It 7980afc94c1SUlrich SpörleinSet the 7990afc94c1SUlrich Spörlein.Xr sysctl 8 8000afc94c1SUlrich Spörleintunable 8010afc94c1SUlrich Spörlein.Va security.bsd.unprivileged_syspmcs 8020afc94c1SUlrich Spörleinto 0. 8030afc94c1SUlrich SpörleinThis ensures that unprivileged processes cannot allocate system-wide 8040afc94c1SUlrich SpörleinPMCs and thus cannot observe the hardware behavior of the system 8050afc94c1SUlrich Spörleinas a whole. 8060afc94c1SUlrich SpörleinThis tunable may also be set at boot time using 8070afc94c1SUlrich Spörlein.Xr loader 8 , 8080afc94c1SUlrich Spörleinor with 8090afc94c1SUlrich Spörlein.Xr kenv 1 8100afc94c1SUlrich Spörleinprior to loading the 8110afc94c1SUlrich Spörlein.Nm 8120afc94c1SUlrich Spörleindriver into the kernel. 8130afc94c1SUlrich Spörlein.It 8140afc94c1SUlrich SpörleinSet the 8150afc94c1SUlrich Spörlein.Xr sysctl 8 8160afc94c1SUlrich Spörleintunable 8170afc94c1SUlrich Spörlein.Va security.bsd.unprivileged_proc_debug 8180afc94c1SUlrich Spörleinto 0. 8190afc94c1SUlrich SpörleinThis will ensure that an unprivileged process cannot attach a PMC 8200afc94c1SUlrich Spörleinto any process other than itself and thus cannot observe the hardware 8210afc94c1SUlrich Spörleinbehavior of other processes with the same credentials. 8220afc94c1SUlrich Spörlein.El 8230afc94c1SUlrich Spörlein.Pp 8240afc94c1SUlrich SpörleinSystem administrators should note that on IA-32 platforms 8250afc94c1SUlrich Spörlein.Fx 8260afc94c1SUlrich Spörleinmakes the content of the IA-32 TSC counter available to all processes 8270afc94c1SUlrich Spörleinvia the RDTSC instruction. 828