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