16455febbSJoseph Koshy.\" Copyright (c) 2003-2008 Joseph Koshy 25fc97cc3SMitchell Horne.\" Copyright (c) 2007,2023 The FreeBSD Foundation 3ebccf1e3SJoseph Koshy.\" 4a07d350cSJoseph Koshy.\" Portions of this software were developed by A. Joseph Koshy under 5a07d350cSJoseph Koshy.\" sponsorship from the FreeBSD Foundation and Google, Inc. 6a07d350cSJoseph Koshy.\" 75fc97cc3SMitchell Horne.\" Portions of this documentation were written by Mitchell Horne 85fc97cc3SMitchell Horne.\" under sponsorship from the FreeBSD Foundation. 95fc97cc3SMitchell Horne.\" 10ebccf1e3SJoseph Koshy.\" Redistribution and use in source and binary forms, with or without 11ebccf1e3SJoseph Koshy.\" modification, are permitted provided that the following conditions 12ebccf1e3SJoseph Koshy.\" are met: 13ebccf1e3SJoseph Koshy.\" 1. Redistributions of source code must retain the above copyright 14ebccf1e3SJoseph Koshy.\" notice, this list of conditions and the following disclaimer. 15ebccf1e3SJoseph Koshy.\" 2. Redistributions in binary form must reproduce the above copyright 16ebccf1e3SJoseph Koshy.\" notice, this list of conditions and the following disclaimer in the 17ebccf1e3SJoseph Koshy.\" documentation and/or other materials provided with the distribution. 18ebccf1e3SJoseph Koshy.\" 19ebccf1e3SJoseph Koshy.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20ebccf1e3SJoseph Koshy.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21ebccf1e3SJoseph Koshy.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22ebccf1e3SJoseph Koshy.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23ebccf1e3SJoseph Koshy.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24ebccf1e3SJoseph Koshy.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25ebccf1e3SJoseph Koshy.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26ebccf1e3SJoseph Koshy.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27ebccf1e3SJoseph Koshy.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28ebccf1e3SJoseph Koshy.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29ebccf1e3SJoseph Koshy.\" SUCH DAMAGE. 30ebccf1e3SJoseph Koshy.\" 31ebccf1e3SJoseph Koshy.\" $FreeBSD$ 32ebccf1e3SJoseph Koshy.\" 33666c54d1SGraham Perrin.Dd July 8, 2023 34ebccf1e3SJoseph Koshy.Dt HWPMC 4 35ebccf1e3SJoseph Koshy.Os 36ebccf1e3SJoseph Koshy.Sh NAME 37ebccf1e3SJoseph Koshy.Nm hwpmc 38d9a5f346SJoseph Koshy.Nd "Hardware Performance Monitoring Counter support" 39ebccf1e3SJoseph Koshy.Sh SYNOPSIS 400a1a4814SMitchell HorneThe following option must be present in the kernel configuration file: 410a1a4814SMitchell Horne.Bd -ragged -offset indent 42d9a5f346SJoseph Koshy.Cd "options HWPMC_HOOKS" 430a1a4814SMitchell Horne.Ed 4479247772SJoseph Koshy.Pp 4579247772SJoseph KoshyAdditionally, for i386 systems: 460a1a4814SMitchell Horne.Bd -ragged -offset indent 4778ad5421SRuslan Ermilov.Cd "device apic" 480a1a4814SMitchell Horne.Ed 490a1a4814SMitchell Horne.Pp 50666c54d1SGraham PerrinTo load the driver as a module at boot time: 510a1a4814SMitchell Horne.Bd -literal -offset indent 52666c54d1SGraham Perrinsysrc kld_list+=hwpmc 530a1a4814SMitchell Horne.Ed 540a1a4814SMitchell Horne.Pp 55666c54d1SGraham PerrinAlternatively, to compile the driver into the kernel: 560a1a4814SMitchell Horne.Bd -ragged -offset indent 570a1a4814SMitchell Horne.Cd "device hwpmc" 580a1a4814SMitchell Horne.Ed 595fc97cc3SMitchell Horne.Pp 605fc97cc3SMitchell HorneTo enable debugging features 615fc97cc3SMitchell Horne.Po see 625fc97cc3SMitchell Horne.Sx DEBUGGING 635fc97cc3SMitchell Horne.Pc : 645fc97cc3SMitchell Horne.Bd -ragged -offset indent 655fc97cc3SMitchell Horne.Cd "options KTR" 665fc97cc3SMitchell Horne.Cd "options KTR_COMPILE=(KTR_SUBSYS)" 675fc97cc3SMitchell Horne.Cd "options KTR_MASK=(KTR_SUBSYS)" 685fc97cc3SMitchell Horne.Cd "options HWPMC_DEBUG" 695fc97cc3SMitchell Horne.Ed 70ebccf1e3SJoseph Koshy.Sh DESCRIPTION 71ebccf1e3SJoseph KoshyThe 72ebccf1e3SJoseph Koshy.Nm 73ebccf1e3SJoseph Koshydriver virtualizes the hardware performance monitoring facilities in 74ebccf1e3SJoseph Koshymodern CPUs and provides support for using these facilities from 75ebccf1e3SJoseph Koshyuser level processes. 76ebccf1e3SJoseph Koshy.Pp 77ebccf1e3SJoseph KoshyThe driver supports multi-processor systems. 78ebccf1e3SJoseph Koshy.Pp 79ebccf1e3SJoseph KoshyPMCs are allocated using the 80d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 81ebccf1e3SJoseph Koshyrequest. 82ebccf1e3SJoseph KoshyA successful 83d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 841a94fde8SJoseph Koshyrequest will return a handle to the requesting process. 85ebccf1e3SJoseph KoshySubsequent operations on the allocated PMC use this handle to denote 86ebccf1e3SJoseph Koshythe specific PMC. 87ebccf1e3SJoseph KoshyA process that has successfully allocated a PMC is termed an 88ebccf1e3SJoseph Koshy.Dq "owner process" . 89ebccf1e3SJoseph Koshy.Pp 901a94fde8SJoseph KoshyPMCs may be allocated with process or system scope. 911a94fde8SJoseph Koshy.Bl -tag -width ".Em Process-scope" 921a94fde8SJoseph Koshy.It Em "Process-scope" 931a94fde8SJoseph KoshyThe PMC is active only when a thread belonging 94ebccf1e3SJoseph Koshyto a process it is attached to is scheduled on a CPU. 951a94fde8SJoseph Koshy.It Em "System-scope" 961a94fde8SJoseph KoshyThe PMC operates independently of processes and 97ebccf1e3SJoseph Koshymeasures hardware events for the system as a whole. 98ebccf1e3SJoseph Koshy.El 99ebccf1e3SJoseph Koshy.Pp 1001a94fde8SJoseph KoshyPMCs may be allocated for counting or for sampling: 101d9a5f346SJoseph Koshy.Bl -tag -width ".Em Counting" 1026d6a103aSJoseph Koshy.It Em Counting 103ebccf1e3SJoseph KoshyIn counting modes, the PMCs count hardware events. 104ebccf1e3SJoseph KoshyThese counts are retrievable using the 105d9a5f346SJoseph Koshy.Dv PMC_OP_PMCREAD 1064f00efe0SJoseph Koshysystem call on all architectures. 1074f00efe0SJoseph KoshySome architectures offer faster methods of reading these counts. 108ebccf1e3SJoseph Koshy.It Em Sampling 1094f00efe0SJoseph KoshyIn sampling modes, the PMCs are configured to sample the CPU 110a07d350cSJoseph Koshyinstruction pointer (and optionally to capture the call chain leading 111a07d350cSJoseph Koshyup to the sampled instruction pointer) after a configurable number of 112a07d350cSJoseph Koshyhardware events have been observed. 113a07d350cSJoseph KoshyInstruction pointer samples and call chain records are usually 114a07d350cSJoseph Koshydirected to a log file for subsequent analysis. 115ebccf1e3SJoseph Koshy.El 116ebccf1e3SJoseph Koshy.Pp 1171a94fde8SJoseph KoshyScope and operational mode are orthogonal; a PMC may thus be 1181a94fde8SJoseph Koshyconfigured to operate in one of the following four modes: 119ebccf1e3SJoseph Koshy.Bl -tag -width indent 1201a94fde8SJoseph Koshy.It Process-scope, counting 121ebccf1e3SJoseph KoshyThese PMCs count hardware events whenever a thread in their attached process is 122ebccf1e3SJoseph Koshyscheduled on a CPU. 123ebccf1e3SJoseph KoshyThese PMCs normally count from zero, but the initial count may be 124ebccf1e3SJoseph Koshyset using the 125d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT 126ebccf1e3SJoseph Koshyoperation. 127ebccf1e3SJoseph KoshyApplications can read the value of the PMC anytime using the 128d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 129ebccf1e3SJoseph Koshyoperation. 1301a94fde8SJoseph Koshy.It Process-scope, sampling 131ebccf1e3SJoseph KoshyThese PMCs sample the target processes instruction pointer after they 132ebccf1e3SJoseph Koshyhave seen the configured number of hardware events. 133ebccf1e3SJoseph KoshyThe PMCs only count events when a thread belonging to their attached 134ebccf1e3SJoseph Koshyprocess is active. 135ebccf1e3SJoseph KoshyThe desired frequency of sampling is set using the 136d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT 137ebccf1e3SJoseph Koshyoperation prior to starting the PMC. 138ebccf1e3SJoseph KoshyLog files are configured using the 139d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 140ebccf1e3SJoseph Koshyoperation. 1411a94fde8SJoseph Koshy.It System-scope, counting 142ebccf1e3SJoseph KoshyThese PMCs count hardware events seen by them independent of the 143ebccf1e3SJoseph Koshyprocesses that are executing. 144ebccf1e3SJoseph KoshyThe current count on these PMCs can be read using the 145d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 146ebccf1e3SJoseph Koshyrequest. 147ebccf1e3SJoseph KoshyThese PMCs normally count from zero, but the initial count may be 148ebccf1e3SJoseph Koshyset using the 149d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT 150ebccf1e3SJoseph Koshyoperation. 1511a94fde8SJoseph Koshy.It System-scope, sampling 152ebccf1e3SJoseph KoshyThese PMCs will periodically sample the instruction pointer of the CPU 153ebccf1e3SJoseph Koshythey are allocated on, and will write the sample to a log for further 154ebccf1e3SJoseph Koshyprocessing. 155ebccf1e3SJoseph KoshyThe desired frequency of sampling is set using the 156d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT 157ebccf1e3SJoseph Koshyoperation prior to starting the PMC. 158ebccf1e3SJoseph KoshyLog files are configured using the 159d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 160ebccf1e3SJoseph Koshyoperation. 161ebccf1e3SJoseph Koshy.Pp 162ebccf1e3SJoseph KoshySystem-wide statistical sampling can only be enabled by a process with 163ebccf1e3SJoseph Koshysuper-user privileges. 164ebccf1e3SJoseph Koshy.El 165ebccf1e3SJoseph Koshy.Pp 1664f00efe0SJoseph KoshyProcesses are allowed to allocate as many PMCs as the hardware and 167ebccf1e3SJoseph Koshycurrent operating conditions permit. 168ebccf1e3SJoseph KoshyProcesses may mix allocations of system-wide and process-private 169ebccf1e3SJoseph KoshyPMCs. 17044caa872SJoseph KoshyMultiple processes may be using PMCs simultaneously. 171ebccf1e3SJoseph Koshy.Pp 172ebccf1e3SJoseph KoshyAllocated PMCs are started using the 173d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART 174ebccf1e3SJoseph Koshyoperation, and stopped using the 175d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTOP 176ebccf1e3SJoseph Koshyoperation. 177ebccf1e3SJoseph KoshyStopping and starting a PMC is permitted at any time the owner process 178ebccf1e3SJoseph Koshyhas a valid handle to the PMC. 179ebccf1e3SJoseph Koshy.Pp 180ebccf1e3SJoseph KoshyProcess-private PMCs need to be attached to a target process before 181ebccf1e3SJoseph Koshythey can be used. 182ebccf1e3SJoseph KoshyAttaching a process to a PMC is done using the 183d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 184ebccf1e3SJoseph Koshyoperation. 185ebccf1e3SJoseph KoshyAn already attached PMC may be detached from its target process 186ebccf1e3SJoseph Koshyusing the converse 187d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH 188ebccf1e3SJoseph Koshyoperation. 189d9a5f346SJoseph KoshyIssuing a 190d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART 191ebccf1e3SJoseph Koshyoperation on an as yet unattached PMC will cause it to be attached 192ebccf1e3SJoseph Koshyto its owner process. 193ebccf1e3SJoseph KoshyThe following rules determine whether a given process may attach 194ebccf1e3SJoseph Koshya PMC to another target process: 195ebccf1e3SJoseph Koshy.Bl -bullet -compact 196ebccf1e3SJoseph Koshy.It 197ebccf1e3SJoseph KoshyA non-jailed process with super-user privileges is allowed to attach 198ebccf1e3SJoseph Koshyto any other process in the system. 199ebccf1e3SJoseph Koshy.It 200ebccf1e3SJoseph KoshyOther processes are only allowed to attach to targets that they would 201ebccf1e3SJoseph Koshybe able to attach to for debugging (as determined by 202ebccf1e3SJoseph Koshy.Xr p_candebug 9 ) . 203ebccf1e3SJoseph Koshy.El 204ebccf1e3SJoseph Koshy.Pp 205ebccf1e3SJoseph KoshyPMCs are released using 206d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRELEASE . 207ebccf1e3SJoseph KoshyAfter a successful 208d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRELEASE 209ebccf1e3SJoseph Koshyoperation the handle to the PMC will become invalid. 210d9a5f346SJoseph Koshy.Ss Modifier Flags 211ebccf1e3SJoseph KoshyThe 212d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 213ebccf1e3SJoseph Koshyoperation supports the following flags that modify the behavior 214ebccf1e3SJoseph Koshyof an allocated PMC: 215d9a5f346SJoseph Koshy.Bl -tag -width indent 216a07d350cSJoseph Koshy.It Dv PMC_F_CALLCHAIN 217a07d350cSJoseph KoshyThis modifier informs sampling PMCs to record a callchain when 218a07d350cSJoseph Koshycapturing a sample. 219a07d350cSJoseph KoshyThe maximum depth to which call chains are recorded is specified 220a07d350cSJoseph Koshyby the 221a07d350cSJoseph Koshy.Va "kern.hwpmc.callchaindepth" 222a07d350cSJoseph Koshykernel tunable. 223ebccf1e3SJoseph Koshy.It Dv PMC_F_DESCENDANTS 224f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private 225ebccf1e3SJoseph Koshymode. 226ebccf1e3SJoseph KoshyIt signifies that the PMC will track hardware events for its 227ebccf1e3SJoseph Koshytarget process and the target's current and future descendants. 228f263522aSJoseph Koshy.It Dv PMC_F_LOG_PROCCSW 229f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private 230f263522aSJoseph Koshymode. 2313bc6c674SJoseph KoshyWhen this modifier is present, at every context switch, 232f263522aSJoseph Koshy.Nm 2333bc6c674SJoseph Koshywill log a record containing the number of hardware events 2343bc6c674SJoseph Koshyseen by the target process when it was scheduled on the CPU. 235f263522aSJoseph Koshy.It Dv PMC_F_LOG_PROCEXIT 236f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private 237f263522aSJoseph Koshymode. 238f263522aSJoseph KoshyWith this modifier present, 239f263522aSJoseph Koshy.Nm 240f263522aSJoseph Koshywill maintain per-process counts for each target process attached to 241f263522aSJoseph Koshya PMC. 242d9a5f346SJoseph KoshyAt process exit time, a record containing the target process' PID and 243f263522aSJoseph Koshythe accumulated per-process count for that process will be written to the 244f263522aSJoseph Koshyconfigured log file. 245ebccf1e3SJoseph Koshy.El 246d9a5f346SJoseph Koshy.Pp 247f263522aSJoseph KoshyModifiers 248f263522aSJoseph Koshy.Dv PMC_F_LOG_PROCEXIT 249f263522aSJoseph Koshyand 250f263522aSJoseph Koshy.Dv PMC_F_LOG_PROCCSW 251f263522aSJoseph Koshymay be used in combination with modifier 252f263522aSJoseph Koshy.Dv PMC_F_DESCENDANTS 2534f00efe0SJoseph Koshyto track the behavior of complex pipelines of processes. 2543bc6c674SJoseph KoshyPMCs with modifiers 2553bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCEXIT 2563bc6c674SJoseph Koshyand 2573bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCCSW 2583bc6c674SJoseph Koshycannot be started until their owner process has configured a log file. 259d9a5f346SJoseph Koshy.Ss Signals 260ebccf1e3SJoseph KoshyThe 261ebccf1e3SJoseph Koshy.Nm 262ebccf1e3SJoseph Koshydriver may deliver signals to processes that have allocated PMCs: 263d9a5f346SJoseph Koshy.Bl -tag -width ".Dv SIGBUS" 264d9a5f346SJoseph Koshy.It Dv SIGIO 265ebccf1e3SJoseph KoshyA 266d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 267ebccf1e3SJoseph Koshyoperation was attempted on a process-private PMC that does not have 268ebccf1e3SJoseph Koshyattached target processes. 269d9a5f346SJoseph Koshy.It Dv SIGBUS 270ebccf1e3SJoseph KoshyThe 271ebccf1e3SJoseph Koshy.Nm 272ebccf1e3SJoseph Koshydriver is being unloaded from the kernel. 273ebccf1e3SJoseph Koshy.El 2741a94fde8SJoseph Koshy.Ss PMC ROW DISPOSITIONS 2751a94fde8SJoseph KoshyA PMC row is defined as the set of PMC resources at the same hardware 2761a94fde8SJoseph Koshyaddress in the CPUs in a system. 2771a94fde8SJoseph KoshySince process scope PMCs need to move between CPUs following their 2781a94fde8SJoseph Koshytarget threads, allocation of a process scope PMC reserves all PMCs in 2791a94fde8SJoseph Koshya PMC row for use only with process scope PMCs. 2801a94fde8SJoseph KoshyAccordingly a PMC row will be in one of the following dispositions: 2811a94fde8SJoseph Koshy.Bl -tag -width ".Dv PMC_DISP_STANDALONE" -compact 2821a94fde8SJoseph Koshy.It Dv PMC_DISP_FREE 2831a94fde8SJoseph KoshyHardware counters in this row are free and may be use to satisfy 2841a94fde8SJoseph Koshyeither of system scope or process scope allocation requests. 2851a94fde8SJoseph Koshy.It Dv PMC_DISP_THREAD 2861a94fde8SJoseph KoshyHardware counters in this row are in use by process scope PMCs 2871a94fde8SJoseph Koshyand are only available for process scope allocation requests. 2881a94fde8SJoseph Koshy.It Dv PMC_DISP_STANDALONE 2891a94fde8SJoseph KoshySome hardware counters in this row have been administratively 2901a94fde8SJoseph Koshydisabled or are in use by system scope PMCs. 2911a94fde8SJoseph KoshyNon-disabled hardware counters in such a row may be used 2921a94fde8SJoseph Koshyfor satisfying system scope allocation requests. 2931a94fde8SJoseph KoshyNo process scope PMCs will use hardware counters in this row. 2941a94fde8SJoseph Koshy.El 2953977781bSMitchell Horne.Sh COMPATIBILITY 2963977781bSMitchell HorneThe API and ABI documented in this manual page may change in the future. 2973977781bSMitchell HorneThis interface is intended to be consumed by the 298d9a5f346SJoseph Koshy.Xr pmc 3 2993977781bSMitchell Hornelibrary; other consumers are unsupported. 3003977781bSMitchell HorneApplications targeting PMCs should use the 3013977781bSMitchell Horne.Xr pmc 3 3023977781bSMitchell Hornelibrary API. 3033977781bSMitchell Horne.Sh PROGRAMMING API 304ebccf1e3SJoseph KoshyThe 305ebccf1e3SJoseph Koshy.Nm 306ebccf1e3SJoseph Koshydriver operates using a system call number that is dynamically 307ebccf1e3SJoseph Koshyallotted to it when it is loaded into the kernel. 308ebccf1e3SJoseph Koshy.Pp 309ebccf1e3SJoseph KoshyThe 310ebccf1e3SJoseph Koshy.Nm 311ebccf1e3SJoseph Koshydriver supports the following operations: 312ebccf1e3SJoseph Koshy.Bl -tag -width indent 313d9a5f346SJoseph Koshy.It Dv PMC_OP_CONFIGURELOG 314a5c506d2SJoseph KoshyConfigure a log file for PMCs that require a log file. 315a5c506d2SJoseph KoshyThe 316a5c506d2SJoseph Koshy.Nm 317a5c506d2SJoseph Koshydriver will write log data to this file asynchronously. 318a5c506d2SJoseph KoshyIf it encounters an error, logging will be stopped and the error code 319a5c506d2SJoseph Koshyencountered will be saved for subsequent retrieval by a 320a5c506d2SJoseph Koshy.Dv PMC_OP_FLUSHLOG 321a5c506d2SJoseph Koshyrequest. 322d9a5f346SJoseph Koshy.It Dv PMC_OP_FLUSHLOG 323f263522aSJoseph KoshyTransfer buffered log data inside 324f263522aSJoseph Koshy.Nm 325f263522aSJoseph Koshyto a configured output file. 326f263522aSJoseph KoshyThis operation returns to the caller after the write operation 327f263522aSJoseph Koshyhas returned. 328a5c506d2SJoseph KoshyThe returned error code reflects any pending error state inside 329a5c506d2SJoseph Koshy.Nm . 330d9a5f346SJoseph Koshy.It Dv PMC_OP_GETCPUINFO 3316455febbSJoseph KoshyRetrieve information about the highest possible CPU number for the system, 3326455febbSJoseph Koshyand the number of hardware performance monitoring counters available per CPU. 333d9a5f346SJoseph Koshy.It Dv PMC_OP_GETDRIVERSTATS 334ebccf1e3SJoseph KoshyRetrieve module statistics (for analyzing the behavior of 335ebccf1e3SJoseph Koshy.Nm 336ebccf1e3SJoseph Koshyitself). 337d9a5f346SJoseph Koshy.It Dv PMC_OP_GETMODULEVERSION 338ebccf1e3SJoseph KoshyRetrieve the version number of API. 339d9a5f346SJoseph Koshy.It Dv PMC_OP_GETPMCINFO 340ebccf1e3SJoseph KoshyRetrieve information about the current state of the PMCs on a 341ebccf1e3SJoseph Koshygiven CPU. 342d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCADMIN 343ebccf1e3SJoseph KoshySet the administrative state (i.e., whether enabled or disabled) for 344ebccf1e3SJoseph Koshythe hardware PMCs managed by the 345ebccf1e3SJoseph Koshy.Nm 346ebccf1e3SJoseph Koshydriver. 3471a94fde8SJoseph KoshyThe invoking process needs to possess the 3481a94fde8SJoseph Koshy.Dv PRIV_PMC_MANAGE 3491a94fde8SJoseph Koshyprivilege. 350d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCALLOCATE 351ebccf1e3SJoseph KoshyAllocate and configure a PMC. 352e805d32bSJoseph KoshyOn successful allocation, a handle to the PMC (a 32 bit value) 353ebccf1e3SJoseph Koshyis returned. 354d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCATTACH 355ebccf1e3SJoseph KoshyAttach a process mode PMC to a target process. 356ebccf1e3SJoseph KoshyThe PMC will be active whenever a thread in the target process is 357ebccf1e3SJoseph Koshyscheduled on a CPU. 358ebccf1e3SJoseph Koshy.Pp 359ebccf1e3SJoseph KoshyIf the 360ebccf1e3SJoseph Koshy.Dv PMC_F_DESCENDANTS 361ebccf1e3SJoseph Koshyflag had been specified at PMC allocation time, then the PMC is 362ebccf1e3SJoseph Koshyattached to all current and future descendants of the target process. 363d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCDETACH 364ebccf1e3SJoseph KoshyDetach a PMC from its target process. 365d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCRELEASE 366ebccf1e3SJoseph KoshyRelease a PMC. 367d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCRW 368ebccf1e3SJoseph KoshyRead and write a PMC. 369ebccf1e3SJoseph KoshyThis operation is valid only for PMCs configured in counting modes. 370d9a5f346SJoseph Koshy.It Dv PMC_OP_SETCOUNT 371ebccf1e3SJoseph KoshySet the initial count (for counting mode PMCs) or the desired sampling 372ebccf1e3SJoseph Koshyrate (for sampling mode PMCs). 373d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCSTART 374ebccf1e3SJoseph KoshyStart a PMC. 375d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCSTOP 376ebccf1e3SJoseph KoshyStop a PMC. 377d9a5f346SJoseph Koshy.It Dv PMC_OP_WRITELOG 378ebccf1e3SJoseph KoshyInsert a timestamped user record into the log file. 379ebccf1e3SJoseph Koshy.El 380d9a5f346SJoseph Koshy.Ss i386 Specific API 381ebccf1e3SJoseph KoshySome i386 family CPUs support the RDPMC instruction which allows a 382ebccf1e3SJoseph Koshyuser process to read a PMC value without needing to invoke a 383d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 384ebccf1e3SJoseph Koshyoperation. 385ebccf1e3SJoseph KoshyOn such CPUs, the machine address associated with an allocated PMC is 386ebccf1e3SJoseph Koshyretrievable using the 387d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR 388ebccf1e3SJoseph Koshysystem call. 389ebccf1e3SJoseph Koshy.Bl -tag -width indent 390d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCX86GETMSR 391ebccf1e3SJoseph KoshyRetrieve the MSR (machine specific register) number associated with 392ebccf1e3SJoseph Koshythe given PMC handle. 393ebccf1e3SJoseph Koshy.Pp 394c5153e19SJoseph KoshyThe PMC needs to be in process-private mode and allocated without the 395d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS 396c5153e19SJoseph Koshymodifier flag, and should be attached only to its owner process at the 397c5153e19SJoseph Koshytime of the call. 398ebccf1e3SJoseph Koshy.El 399d9a5f346SJoseph Koshy.Ss amd64 Specific API 400d9a5f346SJoseph KoshyAMD64 CPUs support the RDPMC instruction which allows a 401ebccf1e3SJoseph Koshyuser process to read a PMC value without needing to invoke a 402d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 403ebccf1e3SJoseph Koshyoperation. 404ebccf1e3SJoseph KoshyThe machine address associated with an allocated PMC is 405ebccf1e3SJoseph Koshyretrievable using the 406d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR 407ebccf1e3SJoseph Koshysystem call. 408ebccf1e3SJoseph Koshy.Bl -tag -width indent 409d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCX86GETMSR 410ebccf1e3SJoseph KoshyRetrieve the MSR (machine specific register) number associated with 411ebccf1e3SJoseph Koshythe given PMC handle. 412ebccf1e3SJoseph Koshy.Pp 413c5153e19SJoseph KoshyThe PMC needs to be in process-private mode and allocated without the 414d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS 415c5153e19SJoseph Koshymodifier flag, and should be attached only to its owner process at the 416c5153e19SJoseph Koshytime of the call. 417ebccf1e3SJoseph Koshy.El 41844cb3ab0SHiroki Sato.Sh SYSCTL VARIABLES AND LOADER TUNABLES 419ebccf1e3SJoseph KoshyThe behavior of 420ebccf1e3SJoseph Koshy.Nm 421ebccf1e3SJoseph Koshyis influenced by the following 422ebccf1e3SJoseph Koshy.Xr sysctl 8 423f263522aSJoseph Koshyand 424f263522aSJoseph Koshy.Xr loader 8 425ebccf1e3SJoseph Koshytunables: 426ebccf1e3SJoseph Koshy.Bl -tag -width indent 427a07d350cSJoseph Koshy.It Va kern.hwpmc.callchaindepth Pq integer, read-only 428a07d350cSJoseph KoshyThe maximum number of call chain records to capture per sample. 429a07d350cSJoseph KoshyThe default is 8. 430f263522aSJoseph Koshy.It Va kern.hwpmc.debugflags Pq string, read-write 431ebccf1e3SJoseph Koshy(Only available if the 432ebccf1e3SJoseph Koshy.Nm 433ebccf1e3SJoseph Koshydriver was compiled with 434d9a5f346SJoseph Koshy.Fl DDEBUG . ) 435ebccf1e3SJoseph KoshyControl the verbosity of debug messages from the 436ebccf1e3SJoseph Koshy.Nm 437ebccf1e3SJoseph Koshydriver. 438f263522aSJoseph Koshy.It Va kern.hwpmc.hashsize Pq integer, read-only 439d9a5f346SJoseph KoshyThe number of rows in the hash tables used to keep track of owner and 440ebccf1e3SJoseph Koshytarget processes. 441f263522aSJoseph KoshyThe default is 16. 442f263522aSJoseph Koshy.It Va kern.hwpmc.logbuffersize Pq integer, read-only 443f263522aSJoseph KoshyThe size in kilobytes of each log buffer used by 444d9a5f346SJoseph Koshy.Nm Ns 's 445f263522aSJoseph Koshylogging function. 44624bae989SJoseph KoshyThe default buffer size is 4KB. 447ca341f3cSJohn Baldwin.It Va kern.hwpmc.mincount Pq integer, read-write 448ca341f3cSJohn BaldwinThe minimum sampling rate for sampling mode PMCs. 449ca341f3cSJohn BaldwinThe default count is 1000 events. 450f263522aSJoseph Koshy.It Va kern.hwpmc.mtxpoolsize Pq integer, read-only 451ebccf1e3SJoseph KoshyThe size of the spin mutex pool used by the PMC driver. 452f263522aSJoseph KoshyThe default is 32. 4530939f965SPiotr Pawel Stefaniak.It Va kern.hwpmc.nbuffers_pcpu Pq integer, read-only 454f263522aSJoseph KoshyThe number of log buffers used by 455f263522aSJoseph Koshy.Nm 456f263522aSJoseph Koshyfor logging. 457ac6a9a8eSJim HarrisThe default is 64. 458f263522aSJoseph Koshy.It Va kern.hwpmc.nsamples Pq integer, read-only 459d9a5f346SJoseph KoshyThe number of entries in the per-CPU ring buffer used during sampling. 460ac6a9a8eSJim HarrisThe default is 512. 461f263522aSJoseph Koshy.It Va security.bsd.unprivileged_syspmcs Pq boolean, read-write 462ebccf1e3SJoseph KoshyIf set to non-zero, allow unprivileged processes to allocate system-wide 463ebccf1e3SJoseph KoshyPMCs. 464ebccf1e3SJoseph KoshyThe default value is 0. 465f263522aSJoseph Koshy.It Va security.bsd.unprivileged_proc_debug Pq boolean, read-write 466ebccf1e3SJoseph KoshyIf set to 0, the 467ebccf1e3SJoseph Koshy.Nm 468f263522aSJoseph Koshydriver will only allow privileged processes to attach PMCs to other 469ebccf1e3SJoseph Koshyprocesses. 470ebccf1e3SJoseph Koshy.El 471ebccf1e3SJoseph Koshy.Pp 472ebccf1e3SJoseph KoshyThese variables may be set in the kernel environment using 473ebccf1e3SJoseph Koshy.Xr kenv 1 474ebccf1e3SJoseph Koshybefore 475ebccf1e3SJoseph Koshy.Nm 476ebccf1e3SJoseph Koshyis loaded. 477ebccf1e3SJoseph Koshy.Sh IMPLEMENTATION NOTES 478f263522aSJoseph Koshy.Ss SMP Symmetry 479f263522aSJoseph KoshyThe kernel driver requires all physical CPUs in an SMP system to have 480f263522aSJoseph Koshyidentical performance monitoring counter hardware. 4816455febbSJoseph Koshy.Ss Sparse CPU Numbering 4826455febbSJoseph KoshyOn platforms that sparsely number CPUs and which support hot-plugging 4836455febbSJoseph Koshyof CPUs, requests that specify non-existent or disabled CPUs will fail 4846455febbSJoseph Koshywith an error. 4856455febbSJoseph KoshyApplications allocating system-scope PMCs need to be aware of 4866455febbSJoseph Koshythe possibility of such transient failures. 48779247772SJoseph Koshy.Ss x86 TSC Handling 488ebccf1e3SJoseph KoshyHistorically, on the x86 architecture, 489ebccf1e3SJoseph Koshy.Fx 490ebccf1e3SJoseph Koshyhas permitted user processes running at a processor CPL of 3 to 491ebccf1e3SJoseph Koshyread the TSC using the RDTSC instruction. 492ebccf1e3SJoseph KoshyThe 493ebccf1e3SJoseph Koshy.Nm 4944f00efe0SJoseph Koshydriver preserves this behavior. 495ebccf1e3SJoseph Koshy.Ss Intel P4/HTT Handling 496ebccf1e3SJoseph KoshyOn CPUs with HTT support, Intel P4 PMCs are capable of qualifying 497ebccf1e3SJoseph Koshyonly a subset of hardware events on a per-logical CPU basis. 498ebccf1e3SJoseph KoshyConsequently, if HTT is enabled on a system with Intel Pentium P4 499ebccf1e3SJoseph KoshyPMCs, then the 500ebccf1e3SJoseph Koshy.Nm 501ebccf1e3SJoseph Koshydriver will reject allocation requests for process-private PMCs that 502ebccf1e3SJoseph Koshyrequest counting of hardware events that cannot be counted separately 503ebccf1e3SJoseph Koshyfor each logical CPU. 504f263522aSJoseph Koshy.Sh DIAGNOSTICS 505f263522aSJoseph Koshy.Bl -diag 506953adc17SJoseph Koshy.It "hwpmc: [class/npmc/capabilities]..." 507953adc17SJoseph KoshyAnnounce the presence of 508953adc17SJoseph Koshy.Va npmc 509953adc17SJoseph KoshyPMCs of class 510953adc17SJoseph Koshy.Va class , 511953adc17SJoseph Koshywith capabilities described by bit string 512953adc17SJoseph Koshy.Va capabilities . 513953adc17SJoseph Koshy.It "hwpmc: kernel version (0x%x) does not match module version (0x%x)." 514953adc17SJoseph KoshyThe module loading process failed because a version mismatch was detected 515953adc17SJoseph Koshybetween the currently executing kernel and the module being loaded. 5163419317cSJoseph Koshy.It "hwpmc: this kernel has not been compiled with 'options HWPMC_HOOKS'." 5173419317cSJoseph KoshyThe module loading process failed because the currently executing kernel 5183419317cSJoseph Koshywas not configured with the required configuration option 51978ad5421SRuslan Ermilov.Dv HWPMC_HOOKS . 520d9a5f346SJoseph Koshy.It "hwpmc: tunable hashsize=%d must be greater than zero." 521f263522aSJoseph KoshyA negative value was supplied for tunable 522f263522aSJoseph Koshy.Va kern.hwpmc.hashsize . 523d9a5f346SJoseph Koshy.It "hwpmc: tunable logbuffersize=%d must be greater than zero." 524f263522aSJoseph KoshyA negative value was supplied for tunable 525f263522aSJoseph Koshy.Va kern.hwpmc.logbuffersize . 526d9a5f346SJoseph Koshy.It "hwpmc: tunable nlogbuffers=%d must be greater than zero." 527f263522aSJoseph KoshyA negative value was supplied for tunable 528f263522aSJoseph Koshy.Va kern.hwpmc.nlogbuffers . 529d9a5f346SJoseph Koshy.It "hwpmc: tunable nsamples=%d out of range." 530f263522aSJoseph KoshyThe value for tunable 531f263522aSJoseph Koshy.Va kern.hwpmc.nsamples 532f263522aSJoseph Koshywas negative or greater than 65535. 533f263522aSJoseph Koshy.El 5345fc97cc3SMitchell Horne.Sh DEBUGGING 5355fc97cc3SMitchell HorneThe 5365fc97cc3SMitchell Horne.Nm 5375fc97cc3SMitchell Hornemodule can be configured to record trace entries using the 5385fc97cc3SMitchell Horne.Xr ktr 4 5395fc97cc3SMitchell Horneinterface. 5405fc97cc3SMitchell HorneThis is useful for debugging the driver's functionality, primarily during 5415fc97cc3SMitchell Hornedevelopment. 5425fc97cc3SMitchell HorneThis debugging functionality is not enabled by default, and requires 5435fc97cc3SMitchell Hornerecompiling the kernel and 5445fc97cc3SMitchell Horne.Nm 5455fc97cc3SMitchell Hornemodule after adding the following to the kernel config: 5465fc97cc3SMitchell Horne.Bd -literal -offset indent 5475fc97cc3SMitchell Horne.Cd options KTR 5485fc97cc3SMitchell Horne.Cd options KTR_COMPILE=(KTR_SUBSYS) 5495fc97cc3SMitchell Horne.Cd options KTR_MASK=(KTR_SUBSYS) 5505fc97cc3SMitchell Horne.Cd options HWPMC_DEBUG 5515fc97cc3SMitchell Horne.Ed 5525fc97cc3SMitchell Horne.Pp 5535fc97cc3SMitchell HorneThis alone is not enough to enable tracing; one must also configure the 5545fc97cc3SMitchell Horne.Va kern.hwpmc.debugflags 5555fc97cc3SMitchell Horne.Xr sysctl 8 5565fc97cc3SMitchell Hornevariable, which provides fine-grained control over which types of events are 5575fc97cc3SMitchell Hornelogged to the trace buffer. 5585fc97cc3SMitchell Horne.Pp 5595fc97cc3SMitchell Horne.Nm 5605fc97cc3SMitchell Hornetrace events are grouped by 'major' and 'minor' flag types. 561*89240e0aSMitchell HorneThe major flag names are as follows: 5625fc97cc3SMitchell Horne.Pp 563*89240e0aSMitchell Horne.Bl -tag -width "sampling" -compact -offset indent 564*89240e0aSMitchell Horne.It cpu 5655fc97cc3SMitchell HorneCPU events 566*89240e0aSMitchell Horne.It csw 5675fc97cc3SMitchell HorneContext switch events 568*89240e0aSMitchell Horne.It logging 5695fc97cc3SMitchell HorneLogging events 570*89240e0aSMitchell Horne.It md 5715fc97cc3SMitchell HorneMachine-dependent/class-dependent events 572*89240e0aSMitchell Horne.It module 5735fc97cc3SMitchell HorneMiscellaneous events 574*89240e0aSMitchell Horne.It owner 5755fc97cc3SMitchell HornePMC owner events 576*89240e0aSMitchell Horne.It pmc 5775fc97cc3SMitchell HornePMC management events 578*89240e0aSMitchell Horne.It process 5795fc97cc3SMitchell HorneProcess events 580*89240e0aSMitchell Horne.It sampling 5815fc97cc3SMitchell HorneSampling events 5825fc97cc3SMitchell Horne.El 5835fc97cc3SMitchell Horne.Pp 5845fc97cc3SMitchell HorneThe minor flags for each major flag group can vary. 585*89240e0aSMitchell HorneThe individual minor flag names are: 586*89240e0aSMitchell Horne.Bd -ragged -offset indent 587*89240e0aSMitchell Horneallocaterow, 588*89240e0aSMitchell Horneallocate, 589*89240e0aSMitchell Horneattach, 590*89240e0aSMitchell Hornebind, 591*89240e0aSMitchell Horneconfig, 592*89240e0aSMitchell Horneexec, 593*89240e0aSMitchell Horneexit, 594*89240e0aSMitchell Hornefind, 595*89240e0aSMitchell Horneflush, 596*89240e0aSMitchell Hornefork, 597*89240e0aSMitchell Hornegetbuf, 598*89240e0aSMitchell Hornehook, 599*89240e0aSMitchell Horneinit, 600*89240e0aSMitchell Horneintr, 601*89240e0aSMitchell Hornelinktarget, 602*89240e0aSMitchell Hornemayberemove, 603*89240e0aSMitchell Horneops, 604*89240e0aSMitchell Horneread, 605*89240e0aSMitchell Horneregister, 606*89240e0aSMitchell Hornerelease, 607*89240e0aSMitchell Horneremove, 608*89240e0aSMitchell Hornesample, 609*89240e0aSMitchell Hornescheduleio, 610*89240e0aSMitchell Horneselect, 611*89240e0aSMitchell Hornesignal, 612*89240e0aSMitchell Horneswi, 613*89240e0aSMitchell Horneswo, 614*89240e0aSMitchell Hornestart, 615*89240e0aSMitchell Hornestop, 616*89240e0aSMitchell Hornesyscall, 617*89240e0aSMitchell Horneunlinktarget, 618*89240e0aSMitchell Hornewrite 619*89240e0aSMitchell Horne.Ed 6205fc97cc3SMitchell Horne.Pp 6215fc97cc3SMitchell HorneThe 6225fc97cc3SMitchell Horne.Va kern.hwpmc.debugflags 6235fc97cc3SMitchell Hornevariable is a string with a custom format. 6245fc97cc3SMitchell HorneThe string should contain a space-separated list of event specifiers. 6255fc97cc3SMitchell HorneEach event specifier consists of the major flag name, followed by an equal sign 6265fc97cc3SMitchell Horne(=), followed by a comma-separated list of minor event types. 627*89240e0aSMitchell HorneTo track all events for a major group, an asterisk (*) can be given instead of 628*89240e0aSMitchell Horneminor event names. 6295fc97cc3SMitchell Horne.Pp 6305fc97cc3SMitchell HorneFor example, to trace all allocation and release events, set 6315fc97cc3SMitchell Horne.Va debugflags 6325fc97cc3SMitchell Horneas follows: 6335fc97cc3SMitchell Horne.Bd -literal -offset indent 634*89240e0aSMitchell Hornekern.hwpmc.debugflags="pmc=allocate,release md=allocate,release" 6355fc97cc3SMitchell Horne.Ed 6365fc97cc3SMitchell Horne.Pp 637*89240e0aSMitchell HorneTo trace all events in the process and context switch major flag groups: 6385fc97cc3SMitchell Horne.Bd -literal -offset indent 639*89240e0aSMitchell Hornekern.hwpmc.debugflags="process=* csw=*" 6405fc97cc3SMitchell Horne.Ed 6415fc97cc3SMitchell Horne.Pp 6425fc97cc3SMitchell HorneTo disable all trace events, set the variable to an empty string. 6435fc97cc3SMitchell Horne.Bd -literal -offset indent 6445fc97cc3SMitchell Hornekern.hwpmc.debugflags="" 6455fc97cc3SMitchell Horne.Ed 6465fc97cc3SMitchell Horne.Pp 6475fc97cc3SMitchell HorneTrace events are recorded by 6485fc97cc3SMitchell Horne.Xr ktr 4 , 6495fc97cc3SMitchell Horneand can be inspected at run-time using the 6505fc97cc3SMitchell Horne.Xr ktrdump 8 6515fc97cc3SMitchell Horneutility, or at the 6525fc97cc3SMitchell Horne.Xr ddb 4 6535fc97cc3SMitchell Horneprompt after a panic with the 'show ktr' command. 654ebccf1e3SJoseph Koshy.Sh ERRORS 655d9a5f346SJoseph KoshyA command issued to the 656ebccf1e3SJoseph Koshy.Nm 657ebccf1e3SJoseph Koshydriver may fail with the following errors: 658ebccf1e3SJoseph Koshy.Bl -tag -width Er 659a5c506d2SJoseph Koshy.It Bq Er EAGAIN 660a5c506d2SJoseph KoshyHelper process creation failed for a 661a5c506d2SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 662a5c506d2SJoseph Koshyrequest due to a temporary resource shortage in the kernel. 663ebccf1e3SJoseph Koshy.It Bq Er EBUSY 664d9a5f346SJoseph KoshyA 665d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 666ebccf1e3SJoseph Koshyoperation was requested while an existing log was active. 667ebccf1e3SJoseph Koshy.It Bq Er EBUSY 668d9a5f346SJoseph KoshyA DISABLE operation was requested using the 669d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 670ebccf1e3SJoseph Koshyrequest for a set of hardware resources currently in use for 671ebccf1e3SJoseph Koshyprocess-private PMCs. 672ebccf1e3SJoseph Koshy.It Bq Er EBUSY 673ebccf1e3SJoseph KoshyA 674d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 675ebccf1e3SJoseph Koshyoperation was requested on an active system mode PMC. 676ebccf1e3SJoseph Koshy.It Bq Er EBUSY 677ebccf1e3SJoseph KoshyA 678d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 679ebccf1e3SJoseph Koshyoperation was requested for a target process that already had another 680ebccf1e3SJoseph KoshyPMC using the same hardware resources attached to it. 681ebccf1e3SJoseph Koshy.It Bq Er EBUSY 682d9a5f346SJoseph KoshyA 683d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 684ebccf1e3SJoseph Koshyrequest writing a new value was issued on a PMC that was active. 685953adc17SJoseph Koshy.It Bq Er EBUSY 686953adc17SJoseph KoshyA 687953adc17SJoseph Koshy.Dv PMC_OP_PMCSETCOUNT 688953adc17SJoseph Koshyrequest was issued on a PMC that was active. 6893bc6c674SJoseph Koshy.It Bq Er EDOOFUS 6903bc6c674SJoseph KoshyA 691d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART 6923bc6c674SJoseph Koshyoperation was requested without a log file being configured for a 6933bc6c674SJoseph KoshyPMC allocated with 6943bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCCSW 6953bc6c674SJoseph Koshyand 6963bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCEXIT 6973bc6c674SJoseph Koshymodifiers. 6981e999888SJoseph Koshy.It Bq Er EDOOFUS 6991e999888SJoseph KoshyA 7001e999888SJoseph Koshy.Dv PMC_OP_PMCSTART 7011e999888SJoseph Koshyoperation was requested on a system-wide sampling PMC without a log 7021e999888SJoseph Koshyfile being configured. 703ebccf1e3SJoseph Koshy.It Bq Er EEXIST 704ebccf1e3SJoseph KoshyA 705d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 706ebccf1e3SJoseph Koshyrequest was reissued for a target process that already is the target 707ebccf1e3SJoseph Koshyof this PMC. 708ebccf1e3SJoseph Koshy.It Bq Er EFAULT 709ebccf1e3SJoseph KoshyA bad address was passed in to the driver. 710ebccf1e3SJoseph Koshy.It Bq Er EINVAL 711e805d32bSJoseph KoshyAn invalid PMC handle was specified. 712ebccf1e3SJoseph Koshy.It Bq Er EINVAL 713d9a5f346SJoseph KoshyAn invalid CPU number was passed in for a 714d9a5f346SJoseph Koshy.Dv PMC_OP_GETPMCINFO 715ebccf1e3SJoseph Koshyoperation. 716ebccf1e3SJoseph Koshy.It Bq Er EINVAL 7178e63e787SJessica ClarkeThe 7188e63e787SJessica Clarke.Ar pm_flags 7198e63e787SJessica Clarkeargument to a 7208e63e787SJessica Clarke.Dv PMC_OP_CONFIGURELOG 7218e63e787SJessica Clarkerequest contained unknown flags. 7228e63e787SJessica Clarke.It Bq Er EINVAL 723a5c506d2SJoseph KoshyA 724a5c506d2SJoseph Koshy.Dv PMC_OP_CONFIGURELOG 725a5c506d2SJoseph Koshyrequest to de-configure a log file was issued without a log file 726a5c506d2SJoseph Koshybeing configured. 727a5c506d2SJoseph Koshy.It Bq Er EINVAL 728a5c506d2SJoseph KoshyA 729a5c506d2SJoseph Koshy.Dv PMC_OP_FLUSHLOG 730a5c506d2SJoseph Koshyrequest was issued without a log file being configured. 731a5c506d2SJoseph Koshy.It Bq Er EINVAL 732d9a5f346SJoseph KoshyAn invalid CPU number was passed in for a 733d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 734ebccf1e3SJoseph Koshyoperation. 735ebccf1e3SJoseph Koshy.It Bq Er EINVAL 736d9a5f346SJoseph KoshyAn invalid operation request was passed in for a 737d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 738ebccf1e3SJoseph Koshyoperation. 739ebccf1e3SJoseph Koshy.It Bq Er EINVAL 740d9a5f346SJoseph KoshyAn invalid PMC ID was passed in for a 741d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 742ebccf1e3SJoseph Koshyoperation. 743ebccf1e3SJoseph Koshy.It Bq Er EINVAL 744ebccf1e3SJoseph KoshyA suitable PMC matching the parameters passed in to a 745d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 746ebccf1e3SJoseph Koshyrequest could not be allocated. 747ebccf1e3SJoseph Koshy.It Bq Er EINVAL 748ebccf1e3SJoseph KoshyAn invalid PMC mode was requested during a 749d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 750ebccf1e3SJoseph Koshyrequest. 751ebccf1e3SJoseph Koshy.It Bq Er EINVAL 752ebccf1e3SJoseph KoshyAn invalid CPU number was specified during a 753d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 754ebccf1e3SJoseph Koshyrequest. 755ebccf1e3SJoseph Koshy.It Bq Er EINVAL 756d9a5f346SJoseph KoshyA CPU other than 757d9a5f346SJoseph Koshy.Dv PMC_CPU_ANY 758ebccf1e3SJoseph Koshywas specified in a 759a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 760ebccf1e3SJoseph Koshyrequest for a process-private PMC. 761ebccf1e3SJoseph Koshy.It Bq Er EINVAL 762d9a5f346SJoseph KoshyA CPU number of 763d9a5f346SJoseph Koshy.Dv PMC_CPU_ANY 764ebccf1e3SJoseph Koshywas specified in a 765a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 766ebccf1e3SJoseph Koshyrequest for a system-wide PMC. 767ebccf1e3SJoseph Koshy.It Bq Er EINVAL 768ebccf1e3SJoseph KoshyThe 769ebccf1e3SJoseph Koshy.Ar pm_flags 770ebccf1e3SJoseph Koshyargument to an 771d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 772ebccf1e3SJoseph Koshyrequest contained unknown flags. 773ebccf1e3SJoseph Koshy.It Bq Er EINVAL 774a5c506d2SJoseph Koshy(On Intel Pentium 4 CPUs with HTT support) 775a5c506d2SJoseph KoshyA 776a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 777a5c506d2SJoseph Koshyrequest for a process-private PMC was issued for an event that does 778a5c506d2SJoseph Koshynot support counting on a per-logical CPU basis. 779a5c506d2SJoseph Koshy.It Bq Er EINVAL 780ebccf1e3SJoseph KoshyA PMC allocated for system-wide operation was specified with a 781d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 782a5c506d2SJoseph Koshyor 783a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH 784ebccf1e3SJoseph Koshyrequest. 785ebccf1e3SJoseph Koshy.It Bq Er EINVAL 786ebccf1e3SJoseph KoshyThe 787ebccf1e3SJoseph Koshy.Ar pm_pid 788ebccf1e3SJoseph Koshyargument to a 789d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 790a5c506d2SJoseph Koshyor 791a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH 792d9a5f346SJoseph Koshyrequest specified an illegal process ID. 793ebccf1e3SJoseph Koshy.It Bq Er EINVAL 794ebccf1e3SJoseph KoshyA 795d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH 796ebccf1e3SJoseph Koshyrequest was issued for a PMC not attached to the target process. 797ebccf1e3SJoseph Koshy.It Bq Er EINVAL 798ebccf1e3SJoseph KoshyArgument 799ebccf1e3SJoseph Koshy.Ar pm_flags 800ebccf1e3SJoseph Koshyto a 801d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW 802ebccf1e3SJoseph Koshyrequest contained illegal flags. 803ebccf1e3SJoseph Koshy.It Bq Er EINVAL 804ebccf1e3SJoseph KoshyA 805d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR 806c5153e19SJoseph Koshyoperation was requested for a PMC not in process-virtual mode, or 807c5153e19SJoseph Koshyfor a PMC that is not solely attached to its owner process, or for 808c5153e19SJoseph Koshya PMC that was allocated with flag 809d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS . 810ebccf1e3SJoseph Koshy.It Bq Er EINVAL 811a5c506d2SJoseph KoshyA 812a5c506d2SJoseph Koshy.Dv PMC_OP_WRITELOG 813a5c506d2SJoseph Koshyrequest was issued for an owner process without a log file 814a5c506d2SJoseph Koshyconfigured. 815ebccf1e3SJoseph Koshy.It Bq Er ENOMEM 816ebccf1e3SJoseph KoshyThe system was not able to allocate kernel memory. 817ebccf1e3SJoseph Koshy.It Bq Er ENOSYS 818a5c506d2SJoseph Koshy(On i386 and amd64 architectures) 819d9a5f346SJoseph KoshyA 820d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR 821ebccf1e3SJoseph Koshyoperation was requested for hardware that does not support reading 822ebccf1e3SJoseph KoshyPMCs directly with the RDPMC instruction. 823ebccf1e3SJoseph Koshy.It Bq Er ENXIO 824d9a5f346SJoseph KoshyA 825d9a5f346SJoseph Koshy.Dv PMC_OP_GETPMCINFO 8266455febbSJoseph Koshyoperation was requested for an absent or disabled CPU. 827ebccf1e3SJoseph Koshy.It Bq Er ENXIO 8286455febbSJoseph KoshyA 8296455febbSJoseph Koshy.Dv PMC_OP_PMCALLOCATE 8306455febbSJoseph Koshyoperation specified allocation of a system-wide PMC on an absent or 8316455febbSJoseph Koshydisabled CPU. 832ebccf1e3SJoseph Koshy.It Bq Er ENXIO 833ebccf1e3SJoseph KoshyA 834d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART 835ebccf1e3SJoseph Koshyor 836d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTOP 8376455febbSJoseph Koshyrequest was issued for a system-wide PMC that was allocated on a CPU 8386455febbSJoseph Koshythat is currently absent or disabled. 839953adc17SJoseph Koshy.It Bq Er EOPNOTSUPP 840953adc17SJoseph KoshyA 841953adc17SJoseph Koshy.Dv PMC_OP_PMCALLOCATE 842953adc17SJoseph Koshyrequest was issued for PMC capabilities not supported 843953adc17SJoseph Koshyby the specified PMC class. 844fe5ca00aSJoseph Koshy.It Bq Er EOPNOTSUPP 845fe5ca00aSJoseph Koshy(i386 architectures) 846fe5ca00aSJoseph KoshyA sampling mode PMC was requested on a CPU lacking an APIC. 847ebccf1e3SJoseph Koshy.It Bq Er EPERM 848d9a5f346SJoseph KoshyA 849d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN 850ebccf1e3SJoseph Koshyrequest was issued by a process without super-user 851ebccf1e3SJoseph Koshyprivilege or by a jailed super-user process. 852ebccf1e3SJoseph Koshy.It Bq Er EPERM 853d9a5f346SJoseph KoshyA 854d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 855ebccf1e3SJoseph Koshyoperation was issued for a target process that the current process 856ebccf1e3SJoseph Koshydoes not have permission to attach to. 857c5153e19SJoseph Koshy.It Bq Er EPERM 858d9a5f346SJoseph Koshy(i386 and amd64 architectures) 859d9a5f346SJoseph KoshyA 860d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 861c5153e19SJoseph Koshyoperation was issued on a PMC whose MSR has been retrieved using 862d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR . 863ebccf1e3SJoseph Koshy.It Bq Er ESRCH 864f263522aSJoseph KoshyA process issued a PMC operation request without having allocated any 865f263522aSJoseph KoshyPMCs. 866f263522aSJoseph Koshy.It Bq Er ESRCH 867f263522aSJoseph KoshyA process issued a PMC operation request after the PMC was detached 868f263522aSJoseph Koshyfrom all of its target processes. 869ebccf1e3SJoseph Koshy.It Bq Er ESRCH 870ebccf1e3SJoseph KoshyA 871d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH 872a5c506d2SJoseph Koshyor 873a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH 874d9a5f346SJoseph Koshyrequest specified a non-existent process ID. 875ebccf1e3SJoseph Koshy.It Bq Er ESRCH 876ebccf1e3SJoseph KoshyThe target process for a 877d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH 878a5c506d2SJoseph Koshyoperation is not being monitored by 879a5c506d2SJoseph Koshy.Nm . 880ebccf1e3SJoseph Koshy.El 881ebccf1e3SJoseph Koshy.Sh SEE ALSO 882ebccf1e3SJoseph Koshy.Xr kenv 1 , 883ebccf1e3SJoseph Koshy.Xr pmc 3 , 8846d6a103aSJoseph Koshy.Xr pmclog 3 , 8855fc97cc3SMitchell Horne.Xr ddb 4 , 8865fc97cc3SMitchell Horne.Xr ktr 4 , 887ebccf1e3SJoseph Koshy.Xr kldload 8 , 8885fc97cc3SMitchell Horne.Xr ktrdump 8 , 889ebccf1e3SJoseph Koshy.Xr pmccontrol 8 , 890ebccf1e3SJoseph Koshy.Xr pmcstat 8 , 891ebccf1e3SJoseph Koshy.Xr sysctl 8 , 892a5c506d2SJoseph Koshy.Xr kproc_create 9 , 893ebccf1e3SJoseph Koshy.Xr p_candebug 9 8946d6a103aSJoseph Koshy.Sh HISTORY 8956d6a103aSJoseph KoshyThe 8966d6a103aSJoseph Koshy.Nm 8976d6a103aSJoseph Koshydriver first appeared in 8986d6a103aSJoseph Koshy.Fx 6.0 . 899e805d32bSJoseph Koshy.Sh AUTHORS 900e805d32bSJoseph KoshyThe 901e805d32bSJoseph Koshy.Nm 902e805d32bSJoseph Koshydriver was written by 9036c899950SBaptiste Daroussin.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org . 904d9a5f346SJoseph Koshy.Sh BUGS 905d9a5f346SJoseph KoshyThe driver samples the state of the kernel's logical processor support 906d9a5f346SJoseph Koshyat the time of initialization (i.e., at module load time). 907d9a5f346SJoseph KoshyOn CPUs supporting logical processors, the driver could misbehave if 908d9a5f346SJoseph Koshylogical processors are subsequently enabled or disabled while the 909d9a5f346SJoseph Koshydriver is active. 910953adc17SJoseph Koshy.Pp 91179247772SJoseph KoshyOn the i386 architecture, the driver requires that the local APIC on the 912953adc17SJoseph KoshyCPU be enabled for sampling mode to be supported. 913953adc17SJoseph KoshyMany single-processor motherboards keep the APIC disabled in BIOS; on 914953adc17SJoseph Koshysuch systems 915953adc17SJoseph Koshy.Nm 916953adc17SJoseph Koshywill not support sampling PMCs. 9170afc94c1SUlrich Spörlein.Sh SECURITY CONSIDERATIONS 9180afc94c1SUlrich SpörleinPMCs may be used to monitor the actual behavior of the system on hardware. 9190afc94c1SUlrich SpörleinIn situations where this constitutes an undesirable information leak, 9200afc94c1SUlrich Spörleinthe following options are available: 9210afc94c1SUlrich Spörlein.Bl -enum 9220afc94c1SUlrich Spörlein.It 9230afc94c1SUlrich SpörleinSet the 9240afc94c1SUlrich Spörlein.Xr sysctl 8 9250afc94c1SUlrich Spörleintunable 9260afc94c1SUlrich Spörlein.Va security.bsd.unprivileged_syspmcs 9270afc94c1SUlrich Spörleinto 0. 9280afc94c1SUlrich SpörleinThis ensures that unprivileged processes cannot allocate system-wide 9290afc94c1SUlrich SpörleinPMCs and thus cannot observe the hardware behavior of the system 9300afc94c1SUlrich Spörleinas a whole. 9310afc94c1SUlrich SpörleinThis tunable may also be set at boot time using 9320afc94c1SUlrich Spörlein.Xr loader 8 , 9330afc94c1SUlrich Spörleinor with 9340afc94c1SUlrich Spörlein.Xr kenv 1 9350afc94c1SUlrich Spörleinprior to loading the 9360afc94c1SUlrich Spörlein.Nm 9370afc94c1SUlrich Spörleindriver into the kernel. 9380afc94c1SUlrich Spörlein.It 9390afc94c1SUlrich SpörleinSet the 9400afc94c1SUlrich Spörlein.Xr sysctl 8 9410afc94c1SUlrich Spörleintunable 9420afc94c1SUlrich Spörlein.Va security.bsd.unprivileged_proc_debug 9430afc94c1SUlrich Spörleinto 0. 9440afc94c1SUlrich SpörleinThis will ensure that an unprivileged process cannot attach a PMC 9450afc94c1SUlrich Spörleinto any process other than itself and thus cannot observe the hardware 9460afc94c1SUlrich Spörleinbehavior of other processes with the same credentials. 9470afc94c1SUlrich Spörlein.El 9480afc94c1SUlrich Spörlein.Pp 9490afc94c1SUlrich SpörleinSystem administrators should note that on IA-32 platforms 9500afc94c1SUlrich Spörlein.Fx 9510afc94c1SUlrich Spörleinmakes the content of the IA-32 TSC counter available to all processes 9520afc94c1SUlrich Spörleinvia the RDTSC instruction. 953