xref: /freebsd/share/man/man4/hwpmc.4 (revision 5fc97cc32553c4073c7dafd67a2d489b8af8563e)
16455febbSJoseph Koshy.\" Copyright (c) 2003-2008 Joseph Koshy
2*5fc97cc3SMitchell 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.\"
7*5fc97cc3SMitchell Horne.\" Portions of this documentation were written by Mitchell Horne
8*5fc97cc3SMitchell Horne.\" under sponsorship from the FreeBSD Foundation.
9*5fc97cc3SMitchell 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.\"
333977781bSMitchell Horne.Dd June 16, 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
500a1a4814SMitchell HorneTo load the driver as a module at boot time, place the
510a1a4814SMitchell Hornefollowing line in
520a1a4814SMitchell Horne.Xr rc.conf 5 :
530a1a4814SMitchell Horne.Bd -literal -offset indent
540a1a4814SMitchell Hornekld_list="${kld_list} hwpmc"
550a1a4814SMitchell Horne.Ed
560a1a4814SMitchell Horne.Pp
570a1a4814SMitchell HorneAlternatively, to compile this driver into the kernel:
580a1a4814SMitchell Horne.Bd -ragged -offset indent
590a1a4814SMitchell Horne.Cd "device hwpmc"
600a1a4814SMitchell Horne.Ed
61*5fc97cc3SMitchell Horne.Pp
62*5fc97cc3SMitchell HorneTo enable debugging features
63*5fc97cc3SMitchell Horne.Po see
64*5fc97cc3SMitchell Horne.Sx DEBUGGING
65*5fc97cc3SMitchell Horne.Pc :
66*5fc97cc3SMitchell Horne.Bd -ragged -offset indent
67*5fc97cc3SMitchell Horne.Cd "options KTR"
68*5fc97cc3SMitchell Horne.Cd "options KTR_COMPILE=(KTR_SUBSYS)"
69*5fc97cc3SMitchell Horne.Cd "options KTR_MASK=(KTR_SUBSYS)"
70*5fc97cc3SMitchell Horne.Cd "options HWPMC_DEBUG"
71*5fc97cc3SMitchell Horne.Ed
72ebccf1e3SJoseph Koshy.Sh DESCRIPTION
73ebccf1e3SJoseph KoshyThe
74ebccf1e3SJoseph Koshy.Nm
75ebccf1e3SJoseph Koshydriver virtualizes the hardware performance monitoring facilities in
76ebccf1e3SJoseph Koshymodern CPUs and provides support for using these facilities from
77ebccf1e3SJoseph Koshyuser level processes.
78ebccf1e3SJoseph Koshy.Pp
79ebccf1e3SJoseph KoshyThe driver supports multi-processor systems.
80ebccf1e3SJoseph Koshy.Pp
81ebccf1e3SJoseph KoshyPMCs are allocated using the
82d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
83ebccf1e3SJoseph Koshyrequest.
84ebccf1e3SJoseph KoshyA successful
85d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
861a94fde8SJoseph Koshyrequest will return a handle to the requesting process.
87ebccf1e3SJoseph KoshySubsequent operations on the allocated PMC use this handle to denote
88ebccf1e3SJoseph Koshythe specific PMC.
89ebccf1e3SJoseph KoshyA process that has successfully allocated a PMC is termed an
90ebccf1e3SJoseph Koshy.Dq "owner process" .
91ebccf1e3SJoseph Koshy.Pp
921a94fde8SJoseph KoshyPMCs may be allocated with process or system scope.
931a94fde8SJoseph Koshy.Bl -tag -width ".Em Process-scope"
941a94fde8SJoseph Koshy.It Em "Process-scope"
951a94fde8SJoseph KoshyThe PMC is active only when a thread belonging
96ebccf1e3SJoseph Koshyto a process it is attached to is scheduled on a CPU.
971a94fde8SJoseph Koshy.It Em "System-scope"
981a94fde8SJoseph KoshyThe PMC operates independently of processes and
99ebccf1e3SJoseph Koshymeasures hardware events for the system as a whole.
100ebccf1e3SJoseph Koshy.El
101ebccf1e3SJoseph Koshy.Pp
1021a94fde8SJoseph KoshyPMCs may be allocated for counting or for sampling:
103d9a5f346SJoseph Koshy.Bl -tag -width ".Em Counting"
1046d6a103aSJoseph Koshy.It Em Counting
105ebccf1e3SJoseph KoshyIn counting modes, the PMCs count hardware events.
106ebccf1e3SJoseph KoshyThese counts are retrievable using the
107d9a5f346SJoseph Koshy.Dv PMC_OP_PMCREAD
1084f00efe0SJoseph Koshysystem call on all architectures.
1094f00efe0SJoseph KoshySome architectures offer faster methods of reading these counts.
110ebccf1e3SJoseph Koshy.It Em Sampling
1114f00efe0SJoseph KoshyIn sampling modes, the PMCs are configured to sample the CPU
112a07d350cSJoseph Koshyinstruction pointer (and optionally to capture the call chain leading
113a07d350cSJoseph Koshyup to the sampled instruction pointer) after a configurable number of
114a07d350cSJoseph Koshyhardware events have been observed.
115a07d350cSJoseph KoshyInstruction pointer samples and call chain records are usually
116a07d350cSJoseph Koshydirected to a log file for subsequent analysis.
117ebccf1e3SJoseph Koshy.El
118ebccf1e3SJoseph Koshy.Pp
1191a94fde8SJoseph KoshyScope and operational mode are orthogonal; a PMC may thus be
1201a94fde8SJoseph Koshyconfigured to operate in one of the following four modes:
121ebccf1e3SJoseph Koshy.Bl -tag -width indent
1221a94fde8SJoseph Koshy.It Process-scope, counting
123ebccf1e3SJoseph KoshyThese PMCs count hardware events whenever a thread in their attached process is
124ebccf1e3SJoseph Koshyscheduled on a CPU.
125ebccf1e3SJoseph KoshyThese PMCs normally count from zero, but the initial count may be
126ebccf1e3SJoseph Koshyset using the
127d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT
128ebccf1e3SJoseph Koshyoperation.
129ebccf1e3SJoseph KoshyApplications can read the value of the PMC anytime using the
130d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW
131ebccf1e3SJoseph Koshyoperation.
1321a94fde8SJoseph Koshy.It Process-scope, sampling
133ebccf1e3SJoseph KoshyThese PMCs sample the target processes instruction pointer after they
134ebccf1e3SJoseph Koshyhave seen the configured number of hardware events.
135ebccf1e3SJoseph KoshyThe PMCs only count events when a thread belonging to their attached
136ebccf1e3SJoseph Koshyprocess is active.
137ebccf1e3SJoseph KoshyThe desired frequency of sampling is set using the
138d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT
139ebccf1e3SJoseph Koshyoperation prior to starting the PMC.
140ebccf1e3SJoseph KoshyLog files are configured using the
141d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG
142ebccf1e3SJoseph Koshyoperation.
1431a94fde8SJoseph Koshy.It System-scope, counting
144ebccf1e3SJoseph KoshyThese PMCs count hardware events seen by them independent of the
145ebccf1e3SJoseph Koshyprocesses that are executing.
146ebccf1e3SJoseph KoshyThe current count on these PMCs can be read using the
147d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW
148ebccf1e3SJoseph Koshyrequest.
149ebccf1e3SJoseph KoshyThese PMCs normally count from zero, but the initial count may be
150ebccf1e3SJoseph Koshyset using the
151d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT
152ebccf1e3SJoseph Koshyoperation.
1531a94fde8SJoseph Koshy.It System-scope, sampling
154ebccf1e3SJoseph KoshyThese PMCs will periodically sample the instruction pointer of the CPU
155ebccf1e3SJoseph Koshythey are allocated on, and will write the sample to a log for further
156ebccf1e3SJoseph Koshyprocessing.
157ebccf1e3SJoseph KoshyThe desired frequency of sampling is set using the
158d9a5f346SJoseph Koshy.Dv PMC_OP_SETCOUNT
159ebccf1e3SJoseph Koshyoperation prior to starting the PMC.
160ebccf1e3SJoseph KoshyLog files are configured using the
161d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG
162ebccf1e3SJoseph Koshyoperation.
163ebccf1e3SJoseph Koshy.Pp
164ebccf1e3SJoseph KoshySystem-wide statistical sampling can only be enabled by a process with
165ebccf1e3SJoseph Koshysuper-user privileges.
166ebccf1e3SJoseph Koshy.El
167ebccf1e3SJoseph Koshy.Pp
1684f00efe0SJoseph KoshyProcesses are allowed to allocate as many PMCs as the hardware and
169ebccf1e3SJoseph Koshycurrent operating conditions permit.
170ebccf1e3SJoseph KoshyProcesses may mix allocations of system-wide and process-private
171ebccf1e3SJoseph KoshyPMCs.
17244caa872SJoseph KoshyMultiple processes may be using PMCs simultaneously.
173ebccf1e3SJoseph Koshy.Pp
174ebccf1e3SJoseph KoshyAllocated PMCs are started using the
175d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART
176ebccf1e3SJoseph Koshyoperation, and stopped using the
177d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTOP
178ebccf1e3SJoseph Koshyoperation.
179ebccf1e3SJoseph KoshyStopping and starting a PMC is permitted at any time the owner process
180ebccf1e3SJoseph Koshyhas a valid handle to the PMC.
181ebccf1e3SJoseph Koshy.Pp
182ebccf1e3SJoseph KoshyProcess-private PMCs need to be attached to a target process before
183ebccf1e3SJoseph Koshythey can be used.
184ebccf1e3SJoseph KoshyAttaching a process to a PMC is done using the
185d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH
186ebccf1e3SJoseph Koshyoperation.
187ebccf1e3SJoseph KoshyAn already attached PMC may be detached from its target process
188ebccf1e3SJoseph Koshyusing the converse
189d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH
190ebccf1e3SJoseph Koshyoperation.
191d9a5f346SJoseph KoshyIssuing a
192d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART
193ebccf1e3SJoseph Koshyoperation on an as yet unattached PMC will cause it to be attached
194ebccf1e3SJoseph Koshyto its owner process.
195ebccf1e3SJoseph KoshyThe following rules determine whether a given process may attach
196ebccf1e3SJoseph Koshya PMC to another target process:
197ebccf1e3SJoseph Koshy.Bl -bullet -compact
198ebccf1e3SJoseph Koshy.It
199ebccf1e3SJoseph KoshyA non-jailed process with super-user privileges is allowed to attach
200ebccf1e3SJoseph Koshyto any other process in the system.
201ebccf1e3SJoseph Koshy.It
202ebccf1e3SJoseph KoshyOther processes are only allowed to attach to targets that they would
203ebccf1e3SJoseph Koshybe able to attach to for debugging (as determined by
204ebccf1e3SJoseph Koshy.Xr p_candebug 9 ) .
205ebccf1e3SJoseph Koshy.El
206ebccf1e3SJoseph Koshy.Pp
207ebccf1e3SJoseph KoshyPMCs are released using
208d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRELEASE .
209ebccf1e3SJoseph KoshyAfter a successful
210d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRELEASE
211ebccf1e3SJoseph Koshyoperation the handle to the PMC will become invalid.
212d9a5f346SJoseph Koshy.Ss Modifier Flags
213ebccf1e3SJoseph KoshyThe
214d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
215ebccf1e3SJoseph Koshyoperation supports the following flags that modify the behavior
216ebccf1e3SJoseph Koshyof an allocated PMC:
217d9a5f346SJoseph Koshy.Bl -tag -width indent
218a07d350cSJoseph Koshy.It Dv PMC_F_CALLCHAIN
219a07d350cSJoseph KoshyThis modifier informs sampling PMCs to record a callchain when
220a07d350cSJoseph Koshycapturing a sample.
221a07d350cSJoseph KoshyThe maximum depth to which call chains are recorded is specified
222a07d350cSJoseph Koshyby the
223a07d350cSJoseph Koshy.Va "kern.hwpmc.callchaindepth"
224a07d350cSJoseph Koshykernel tunable.
225ebccf1e3SJoseph Koshy.It Dv PMC_F_DESCENDANTS
226f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private
227ebccf1e3SJoseph Koshymode.
228ebccf1e3SJoseph KoshyIt signifies that the PMC will track hardware events for its
229ebccf1e3SJoseph Koshytarget process and the target's current and future descendants.
230f263522aSJoseph Koshy.It Dv PMC_F_LOG_PROCCSW
231f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private
232f263522aSJoseph Koshymode.
2333bc6c674SJoseph KoshyWhen this modifier is present, at every context switch,
234f263522aSJoseph Koshy.Nm
2353bc6c674SJoseph Koshywill log a record containing the number of hardware events
2363bc6c674SJoseph Koshyseen by the target process when it was scheduled on the CPU.
237f263522aSJoseph Koshy.It Dv PMC_F_LOG_PROCEXIT
238f263522aSJoseph KoshyThis modifier is valid only for a PMC being allocated in process-private
239f263522aSJoseph Koshymode.
240f263522aSJoseph KoshyWith this modifier present,
241f263522aSJoseph Koshy.Nm
242f263522aSJoseph Koshywill maintain per-process counts for each target process attached to
243f263522aSJoseph Koshya PMC.
244d9a5f346SJoseph KoshyAt process exit time, a record containing the target process' PID and
245f263522aSJoseph Koshythe accumulated per-process count for that process will be written to the
246f263522aSJoseph Koshyconfigured log file.
247ebccf1e3SJoseph Koshy.El
248d9a5f346SJoseph Koshy.Pp
249f263522aSJoseph KoshyModifiers
250f263522aSJoseph Koshy.Dv PMC_F_LOG_PROCEXIT
251f263522aSJoseph Koshyand
252f263522aSJoseph Koshy.Dv PMC_F_LOG_PROCCSW
253f263522aSJoseph Koshymay be used in combination with modifier
254f263522aSJoseph Koshy.Dv PMC_F_DESCENDANTS
2554f00efe0SJoseph Koshyto track the behavior of complex pipelines of processes.
2563bc6c674SJoseph KoshyPMCs with modifiers
2573bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCEXIT
2583bc6c674SJoseph Koshyand
2593bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCCSW
2603bc6c674SJoseph Koshycannot be started until their owner process has configured a log file.
261d9a5f346SJoseph Koshy.Ss Signals
262ebccf1e3SJoseph KoshyThe
263ebccf1e3SJoseph Koshy.Nm
264ebccf1e3SJoseph Koshydriver may deliver signals to processes that have allocated PMCs:
265d9a5f346SJoseph Koshy.Bl -tag -width ".Dv SIGBUS"
266d9a5f346SJoseph Koshy.It Dv SIGIO
267ebccf1e3SJoseph KoshyA
268d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW
269ebccf1e3SJoseph Koshyoperation was attempted on a process-private PMC that does not have
270ebccf1e3SJoseph Koshyattached target processes.
271d9a5f346SJoseph Koshy.It Dv SIGBUS
272ebccf1e3SJoseph KoshyThe
273ebccf1e3SJoseph Koshy.Nm
274ebccf1e3SJoseph Koshydriver is being unloaded from the kernel.
275ebccf1e3SJoseph Koshy.El
2761a94fde8SJoseph Koshy.Ss PMC ROW DISPOSITIONS
2771a94fde8SJoseph KoshyA PMC row is defined as the set of PMC resources at the same hardware
2781a94fde8SJoseph Koshyaddress in the CPUs in a system.
2791a94fde8SJoseph KoshySince process scope PMCs need to move between CPUs following their
2801a94fde8SJoseph Koshytarget threads, allocation of a process scope PMC reserves all PMCs in
2811a94fde8SJoseph Koshya PMC row for use only with process scope PMCs.
2821a94fde8SJoseph KoshyAccordingly a PMC row will be in one of the following dispositions:
2831a94fde8SJoseph Koshy.Bl -tag -width ".Dv PMC_DISP_STANDALONE" -compact
2841a94fde8SJoseph Koshy.It Dv PMC_DISP_FREE
2851a94fde8SJoseph KoshyHardware counters in this row are free and may be use to satisfy
2861a94fde8SJoseph Koshyeither of system scope or process scope allocation requests.
2871a94fde8SJoseph Koshy.It Dv PMC_DISP_THREAD
2881a94fde8SJoseph KoshyHardware counters in this row are in use by process scope PMCs
2891a94fde8SJoseph Koshyand are only available for process scope allocation requests.
2901a94fde8SJoseph Koshy.It Dv PMC_DISP_STANDALONE
2911a94fde8SJoseph KoshySome hardware counters in this row have been administratively
2921a94fde8SJoseph Koshydisabled or are in use by system scope PMCs.
2931a94fde8SJoseph KoshyNon-disabled hardware counters in such a row may be used
2941a94fde8SJoseph Koshyfor satisfying system scope allocation requests.
2951a94fde8SJoseph KoshyNo process scope PMCs will use hardware counters in this row.
2961a94fde8SJoseph Koshy.El
2973977781bSMitchell Horne.Sh COMPATIBILITY
2983977781bSMitchell HorneThe API and ABI documented in this manual page may change in the future.
2993977781bSMitchell HorneThis interface is intended to be consumed by the
300d9a5f346SJoseph Koshy.Xr pmc 3
3013977781bSMitchell Hornelibrary; other consumers are unsupported.
3023977781bSMitchell HorneApplications targeting PMCs should use the
3033977781bSMitchell Horne.Xr pmc 3
3043977781bSMitchell Hornelibrary API.
3053977781bSMitchell Horne.Sh PROGRAMMING API
306ebccf1e3SJoseph KoshyThe
307ebccf1e3SJoseph Koshy.Nm
308ebccf1e3SJoseph Koshydriver operates using a system call number that is dynamically
309ebccf1e3SJoseph Koshyallotted to it when it is loaded into the kernel.
310ebccf1e3SJoseph Koshy.Pp
311ebccf1e3SJoseph KoshyThe
312ebccf1e3SJoseph Koshy.Nm
313ebccf1e3SJoseph Koshydriver supports the following operations:
314ebccf1e3SJoseph Koshy.Bl -tag -width indent
315d9a5f346SJoseph Koshy.It Dv PMC_OP_CONFIGURELOG
316a5c506d2SJoseph KoshyConfigure a log file for PMCs that require a log file.
317a5c506d2SJoseph KoshyThe
318a5c506d2SJoseph Koshy.Nm
319a5c506d2SJoseph Koshydriver will write log data to this file asynchronously.
320a5c506d2SJoseph KoshyIf it encounters an error, logging will be stopped and the error code
321a5c506d2SJoseph Koshyencountered will be saved for subsequent retrieval by a
322a5c506d2SJoseph Koshy.Dv PMC_OP_FLUSHLOG
323a5c506d2SJoseph Koshyrequest.
324d9a5f346SJoseph Koshy.It Dv PMC_OP_FLUSHLOG
325f263522aSJoseph KoshyTransfer buffered log data inside
326f263522aSJoseph Koshy.Nm
327f263522aSJoseph Koshyto a configured output file.
328f263522aSJoseph KoshyThis operation returns to the caller after the write operation
329f263522aSJoseph Koshyhas returned.
330a5c506d2SJoseph KoshyThe returned error code reflects any pending error state inside
331a5c506d2SJoseph Koshy.Nm .
332d9a5f346SJoseph Koshy.It Dv PMC_OP_GETCPUINFO
3336455febbSJoseph KoshyRetrieve information about the highest possible CPU number for the system,
3346455febbSJoseph Koshyand the number of hardware performance monitoring counters available per CPU.
335d9a5f346SJoseph Koshy.It Dv PMC_OP_GETDRIVERSTATS
336ebccf1e3SJoseph KoshyRetrieve module statistics (for analyzing the behavior of
337ebccf1e3SJoseph Koshy.Nm
338ebccf1e3SJoseph Koshyitself).
339d9a5f346SJoseph Koshy.It Dv PMC_OP_GETMODULEVERSION
340ebccf1e3SJoseph KoshyRetrieve the version number of API.
341d9a5f346SJoseph Koshy.It Dv PMC_OP_GETPMCINFO
342ebccf1e3SJoseph KoshyRetrieve information about the current state of the PMCs on a
343ebccf1e3SJoseph Koshygiven CPU.
344d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCADMIN
345ebccf1e3SJoseph KoshySet the administrative state (i.e., whether enabled or disabled) for
346ebccf1e3SJoseph Koshythe hardware PMCs managed by the
347ebccf1e3SJoseph Koshy.Nm
348ebccf1e3SJoseph Koshydriver.
3491a94fde8SJoseph KoshyThe invoking process needs to possess the
3501a94fde8SJoseph Koshy.Dv PRIV_PMC_MANAGE
3511a94fde8SJoseph Koshyprivilege.
352d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCALLOCATE
353ebccf1e3SJoseph KoshyAllocate and configure a PMC.
354e805d32bSJoseph KoshyOn successful allocation, a handle to the PMC (a 32 bit value)
355ebccf1e3SJoseph Koshyis returned.
356d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCATTACH
357ebccf1e3SJoseph KoshyAttach a process mode PMC to a target process.
358ebccf1e3SJoseph KoshyThe PMC will be active whenever a thread in the target process is
359ebccf1e3SJoseph Koshyscheduled on a CPU.
360ebccf1e3SJoseph Koshy.Pp
361ebccf1e3SJoseph KoshyIf the
362ebccf1e3SJoseph Koshy.Dv PMC_F_DESCENDANTS
363ebccf1e3SJoseph Koshyflag had been specified at PMC allocation time, then the PMC is
364ebccf1e3SJoseph Koshyattached to all current and future descendants of the target process.
365d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCDETACH
366ebccf1e3SJoseph KoshyDetach a PMC from its target process.
367d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCRELEASE
368ebccf1e3SJoseph KoshyRelease a PMC.
369d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCRW
370ebccf1e3SJoseph KoshyRead and write a PMC.
371ebccf1e3SJoseph KoshyThis operation is valid only for PMCs configured in counting modes.
372d9a5f346SJoseph Koshy.It Dv PMC_OP_SETCOUNT
373ebccf1e3SJoseph KoshySet the initial count (for counting mode PMCs) or the desired sampling
374ebccf1e3SJoseph Koshyrate (for sampling mode PMCs).
375d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCSTART
376ebccf1e3SJoseph KoshyStart a PMC.
377d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCSTOP
378ebccf1e3SJoseph KoshyStop a PMC.
379d9a5f346SJoseph Koshy.It Dv PMC_OP_WRITELOG
380ebccf1e3SJoseph KoshyInsert a timestamped user record into the log file.
381ebccf1e3SJoseph Koshy.El
382d9a5f346SJoseph Koshy.Ss i386 Specific API
383ebccf1e3SJoseph KoshySome i386 family CPUs support the RDPMC instruction which allows a
384ebccf1e3SJoseph Koshyuser process to read a PMC value without needing to invoke a
385d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW
386ebccf1e3SJoseph Koshyoperation.
387ebccf1e3SJoseph KoshyOn such CPUs, the machine address associated with an allocated PMC is
388ebccf1e3SJoseph Koshyretrievable using the
389d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR
390ebccf1e3SJoseph Koshysystem call.
391ebccf1e3SJoseph Koshy.Bl -tag -width indent
392d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCX86GETMSR
393ebccf1e3SJoseph KoshyRetrieve the MSR (machine specific register) number associated with
394ebccf1e3SJoseph Koshythe given PMC handle.
395ebccf1e3SJoseph Koshy.Pp
396c5153e19SJoseph KoshyThe PMC needs to be in process-private mode and allocated without the
397d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS
398c5153e19SJoseph Koshymodifier flag, and should be attached only to its owner process at the
399c5153e19SJoseph Koshytime of the call.
400ebccf1e3SJoseph Koshy.El
401d9a5f346SJoseph Koshy.Ss amd64 Specific API
402d9a5f346SJoseph KoshyAMD64 CPUs support the RDPMC instruction which allows a
403ebccf1e3SJoseph Koshyuser process to read a PMC value without needing to invoke a
404d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW
405ebccf1e3SJoseph Koshyoperation.
406ebccf1e3SJoseph KoshyThe machine address associated with an allocated PMC is
407ebccf1e3SJoseph Koshyretrievable using the
408d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR
409ebccf1e3SJoseph Koshysystem call.
410ebccf1e3SJoseph Koshy.Bl -tag -width indent
411d9a5f346SJoseph Koshy.It Dv PMC_OP_PMCX86GETMSR
412ebccf1e3SJoseph KoshyRetrieve the MSR (machine specific register) number associated with
413ebccf1e3SJoseph Koshythe given PMC handle.
414ebccf1e3SJoseph Koshy.Pp
415c5153e19SJoseph KoshyThe PMC needs to be in process-private mode and allocated without the
416d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS
417c5153e19SJoseph Koshymodifier flag, and should be attached only to its owner process at the
418c5153e19SJoseph Koshytime of the call.
419ebccf1e3SJoseph Koshy.El
42044cb3ab0SHiroki Sato.Sh SYSCTL VARIABLES AND LOADER TUNABLES
421ebccf1e3SJoseph KoshyThe behavior of
422ebccf1e3SJoseph Koshy.Nm
423ebccf1e3SJoseph Koshyis influenced by the following
424ebccf1e3SJoseph Koshy.Xr sysctl 8
425f263522aSJoseph Koshyand
426f263522aSJoseph Koshy.Xr loader 8
427ebccf1e3SJoseph Koshytunables:
428ebccf1e3SJoseph Koshy.Bl -tag -width indent
429a07d350cSJoseph Koshy.It Va kern.hwpmc.callchaindepth Pq integer, read-only
430a07d350cSJoseph KoshyThe maximum number of call chain records to capture per sample.
431a07d350cSJoseph KoshyThe default is 8.
432f263522aSJoseph Koshy.It Va kern.hwpmc.debugflags Pq string, read-write
433ebccf1e3SJoseph Koshy(Only available if the
434ebccf1e3SJoseph Koshy.Nm
435ebccf1e3SJoseph Koshydriver was compiled with
436d9a5f346SJoseph Koshy.Fl DDEBUG . )
437ebccf1e3SJoseph KoshyControl the verbosity of debug messages from the
438ebccf1e3SJoseph Koshy.Nm
439ebccf1e3SJoseph Koshydriver.
440f263522aSJoseph Koshy.It Va kern.hwpmc.hashsize Pq integer, read-only
441d9a5f346SJoseph KoshyThe number of rows in the hash tables used to keep track of owner and
442ebccf1e3SJoseph Koshytarget processes.
443f263522aSJoseph KoshyThe default is 16.
444f263522aSJoseph Koshy.It Va kern.hwpmc.logbuffersize Pq integer, read-only
445f263522aSJoseph KoshyThe size in kilobytes of each log buffer used by
446d9a5f346SJoseph Koshy.Nm Ns 's
447f263522aSJoseph Koshylogging function.
44824bae989SJoseph KoshyThe default buffer size is 4KB.
449ca341f3cSJohn Baldwin.It Va kern.hwpmc.mincount Pq integer, read-write
450ca341f3cSJohn BaldwinThe minimum sampling rate for sampling mode PMCs.
451ca341f3cSJohn BaldwinThe default count is 1000 events.
452f263522aSJoseph Koshy.It Va kern.hwpmc.mtxpoolsize Pq integer, read-only
453ebccf1e3SJoseph KoshyThe size of the spin mutex pool used by the PMC driver.
454f263522aSJoseph KoshyThe default is 32.
4550939f965SPiotr Pawel Stefaniak.It Va kern.hwpmc.nbuffers_pcpu Pq integer, read-only
456f263522aSJoseph KoshyThe number of log buffers used by
457f263522aSJoseph Koshy.Nm
458f263522aSJoseph Koshyfor logging.
459ac6a9a8eSJim HarrisThe default is 64.
460f263522aSJoseph Koshy.It Va kern.hwpmc.nsamples Pq integer, read-only
461d9a5f346SJoseph KoshyThe number of entries in the per-CPU ring buffer used during sampling.
462ac6a9a8eSJim HarrisThe default is 512.
463f263522aSJoseph Koshy.It Va security.bsd.unprivileged_syspmcs Pq boolean, read-write
464ebccf1e3SJoseph KoshyIf set to non-zero, allow unprivileged processes to allocate system-wide
465ebccf1e3SJoseph KoshyPMCs.
466ebccf1e3SJoseph KoshyThe default value is 0.
467f263522aSJoseph Koshy.It Va security.bsd.unprivileged_proc_debug Pq boolean, read-write
468ebccf1e3SJoseph KoshyIf set to 0, the
469ebccf1e3SJoseph Koshy.Nm
470f263522aSJoseph Koshydriver will only allow privileged processes to attach PMCs to other
471ebccf1e3SJoseph Koshyprocesses.
472ebccf1e3SJoseph Koshy.El
473ebccf1e3SJoseph Koshy.Pp
474ebccf1e3SJoseph KoshyThese variables may be set in the kernel environment using
475ebccf1e3SJoseph Koshy.Xr kenv 1
476ebccf1e3SJoseph Koshybefore
477ebccf1e3SJoseph Koshy.Nm
478ebccf1e3SJoseph Koshyis loaded.
479ebccf1e3SJoseph Koshy.Sh IMPLEMENTATION NOTES
480f263522aSJoseph Koshy.Ss SMP Symmetry
481f263522aSJoseph KoshyThe kernel driver requires all physical CPUs in an SMP system to have
482f263522aSJoseph Koshyidentical performance monitoring counter hardware.
4836455febbSJoseph Koshy.Ss Sparse CPU Numbering
4846455febbSJoseph KoshyOn platforms that sparsely number CPUs and which support hot-plugging
4856455febbSJoseph Koshyof CPUs, requests that specify non-existent or disabled CPUs will fail
4866455febbSJoseph Koshywith an error.
4876455febbSJoseph KoshyApplications allocating system-scope PMCs need to be aware of
4886455febbSJoseph Koshythe possibility of such transient failures.
48979247772SJoseph Koshy.Ss x86 TSC Handling
490ebccf1e3SJoseph KoshyHistorically, on the x86 architecture,
491ebccf1e3SJoseph Koshy.Fx
492ebccf1e3SJoseph Koshyhas permitted user processes running at a processor CPL of 3 to
493ebccf1e3SJoseph Koshyread the TSC using the RDTSC instruction.
494ebccf1e3SJoseph KoshyThe
495ebccf1e3SJoseph Koshy.Nm
4964f00efe0SJoseph Koshydriver preserves this behavior.
497ebccf1e3SJoseph Koshy.Ss Intel P4/HTT Handling
498ebccf1e3SJoseph KoshyOn CPUs with HTT support, Intel P4 PMCs are capable of qualifying
499ebccf1e3SJoseph Koshyonly a subset of hardware events on a per-logical CPU basis.
500ebccf1e3SJoseph KoshyConsequently, if HTT is enabled on a system with Intel Pentium P4
501ebccf1e3SJoseph KoshyPMCs, then the
502ebccf1e3SJoseph Koshy.Nm
503ebccf1e3SJoseph Koshydriver will reject allocation requests for process-private PMCs that
504ebccf1e3SJoseph Koshyrequest counting of hardware events that cannot be counted separately
505ebccf1e3SJoseph Koshyfor each logical CPU.
506f263522aSJoseph Koshy.Sh DIAGNOSTICS
507f263522aSJoseph Koshy.Bl -diag
508953adc17SJoseph Koshy.It "hwpmc: [class/npmc/capabilities]..."
509953adc17SJoseph KoshyAnnounce the presence of
510953adc17SJoseph Koshy.Va npmc
511953adc17SJoseph KoshyPMCs of class
512953adc17SJoseph Koshy.Va class ,
513953adc17SJoseph Koshywith capabilities described by bit string
514953adc17SJoseph Koshy.Va capabilities .
515953adc17SJoseph Koshy.It "hwpmc: kernel version (0x%x) does not match module version (0x%x)."
516953adc17SJoseph KoshyThe module loading process failed because a version mismatch was detected
517953adc17SJoseph Koshybetween the currently executing kernel and the module being loaded.
5183419317cSJoseph Koshy.It "hwpmc: this kernel has not been compiled with 'options HWPMC_HOOKS'."
5193419317cSJoseph KoshyThe module loading process failed because the currently executing kernel
5203419317cSJoseph Koshywas not configured with the required configuration option
52178ad5421SRuslan Ermilov.Dv HWPMC_HOOKS .
522d9a5f346SJoseph Koshy.It "hwpmc: tunable hashsize=%d must be greater than zero."
523f263522aSJoseph KoshyA negative value was supplied for tunable
524f263522aSJoseph Koshy.Va kern.hwpmc.hashsize .
525d9a5f346SJoseph Koshy.It "hwpmc: tunable logbuffersize=%d must be greater than zero."
526f263522aSJoseph KoshyA negative value was supplied for tunable
527f263522aSJoseph Koshy.Va kern.hwpmc.logbuffersize .
528d9a5f346SJoseph Koshy.It "hwpmc: tunable nlogbuffers=%d must be greater than zero."
529f263522aSJoseph KoshyA negative value was supplied for tunable
530f263522aSJoseph Koshy.Va kern.hwpmc.nlogbuffers .
531d9a5f346SJoseph Koshy.It "hwpmc: tunable nsamples=%d out of range."
532f263522aSJoseph KoshyThe value for tunable
533f263522aSJoseph Koshy.Va kern.hwpmc.nsamples
534f263522aSJoseph Koshywas negative or greater than 65535.
535f263522aSJoseph Koshy.El
536*5fc97cc3SMitchell Horne.Sh DEBUGGING
537*5fc97cc3SMitchell HorneThe
538*5fc97cc3SMitchell Horne.Nm
539*5fc97cc3SMitchell Hornemodule can be configured to record trace entries using the
540*5fc97cc3SMitchell Horne.Xr ktr 4
541*5fc97cc3SMitchell Horneinterface.
542*5fc97cc3SMitchell HorneThis is useful for debugging the driver's functionality, primarily during
543*5fc97cc3SMitchell Hornedevelopment.
544*5fc97cc3SMitchell HorneThis debugging functionality is not enabled by default, and requires
545*5fc97cc3SMitchell Hornerecompiling the kernel and
546*5fc97cc3SMitchell Horne.Nm
547*5fc97cc3SMitchell Hornemodule after adding the following to the kernel config:
548*5fc97cc3SMitchell Horne.Bd -literal -offset indent
549*5fc97cc3SMitchell Horne.Cd options KTR
550*5fc97cc3SMitchell Horne.Cd options KTR_COMPILE=(KTR_SUBSYS)
551*5fc97cc3SMitchell Horne.Cd options KTR_MASK=(KTR_SUBSYS)
552*5fc97cc3SMitchell Horne.Cd options HWPMC_DEBUG
553*5fc97cc3SMitchell Horne.Ed
554*5fc97cc3SMitchell Horne.Pp
555*5fc97cc3SMitchell HorneThis alone is not enough to enable tracing; one must also configure the
556*5fc97cc3SMitchell Horne.Va kern.hwpmc.debugflags
557*5fc97cc3SMitchell Horne.Xr sysctl 8
558*5fc97cc3SMitchell Hornevariable, which provides fine-grained control over which types of events are
559*5fc97cc3SMitchell Hornelogged to the trace buffer.
560*5fc97cc3SMitchell Horne.Pp
561*5fc97cc3SMitchell Horne.Nm
562*5fc97cc3SMitchell Hornetrace events are grouped by 'major' and 'minor' flag types.
563*5fc97cc3SMitchell HorneThe major flag groups are as follows:
564*5fc97cc3SMitchell Horne.Pp
565*5fc97cc3SMitchell Horne.Bl -tag -compact -offset indent
566*5fc97cc3SMitchell Horne.It CPU
567*5fc97cc3SMitchell HorneCPU events
568*5fc97cc3SMitchell Horne.It CSW
569*5fc97cc3SMitchell HorneContext switch events
570*5fc97cc3SMitchell Horne.It LOG
571*5fc97cc3SMitchell HorneLogging events
572*5fc97cc3SMitchell Horne.It MDP
573*5fc97cc3SMitchell HorneMachine-dependent/class-dependent events
574*5fc97cc3SMitchell Horne.It MOD
575*5fc97cc3SMitchell HorneMiscellaneous events
576*5fc97cc3SMitchell Horne.It OWN
577*5fc97cc3SMitchell HornePMC owner events
578*5fc97cc3SMitchell Horne.It PMC
579*5fc97cc3SMitchell HornePMC management events
580*5fc97cc3SMitchell Horne.It PRC
581*5fc97cc3SMitchell HorneProcess events
582*5fc97cc3SMitchell Horne.It SAM
583*5fc97cc3SMitchell HorneSampling events
584*5fc97cc3SMitchell Horne.El
585*5fc97cc3SMitchell Horne.Pp
586*5fc97cc3SMitchell HorneThe minor flags for each major flag group can vary.
587*5fc97cc3SMitchell HorneFor the full list of flag groups, consult
588*5fc97cc3SMitchell Horne.In sys/pmc.h .
589*5fc97cc3SMitchell Horne.Pp
590*5fc97cc3SMitchell HorneThe
591*5fc97cc3SMitchell Horne.Va kern.hwpmc.debugflags
592*5fc97cc3SMitchell Hornevariable is a string with a custom format.
593*5fc97cc3SMitchell HorneThe string should contain a space-separated list of event specifiers.
594*5fc97cc3SMitchell HorneEach event specifier consists of the major flag name, followed by an equal sign
595*5fc97cc3SMitchell Horne(=), followed by a comma-separated list of minor event types.
596*5fc97cc3SMitchell HorneTo track all events for a major group, an asterisk (*) can be given instead.
597*5fc97cc3SMitchell Horne.Pp
598*5fc97cc3SMitchell HorneFor example, to trace all allocation and release events, set
599*5fc97cc3SMitchell Horne.Va debugflags
600*5fc97cc3SMitchell Horneas follows:
601*5fc97cc3SMitchell Horne.Bd -literal -offset indent
602*5fc97cc3SMitchell Hornekern.hwpmc.debugflags="pmc=all,rel mdp=all,rel"
603*5fc97cc3SMitchell Horne.Ed
604*5fc97cc3SMitchell Horne.Pp
605*5fc97cc3SMitchell HorneTo trace all process events, as well as context switches:
606*5fc97cc3SMitchell Horne.Bd -literal -offset indent
607*5fc97cc3SMitchell Hornekern.hwpmc.debugflags="prc=* csw=*"
608*5fc97cc3SMitchell Horne.Ed
609*5fc97cc3SMitchell Horne.Pp
610*5fc97cc3SMitchell HorneTo disable all trace events, set the variable to an empty string.
611*5fc97cc3SMitchell Horne.Bd -literal -offset indent
612*5fc97cc3SMitchell Hornekern.hwpmc.debugflags=""
613*5fc97cc3SMitchell Horne.Ed
614*5fc97cc3SMitchell Horne.Pp
615*5fc97cc3SMitchell HorneTrace events are recorded by
616*5fc97cc3SMitchell Horne.Xr ktr 4 ,
617*5fc97cc3SMitchell Horneand can be inspected at run-time using the
618*5fc97cc3SMitchell Horne.Xr ktrdump 8
619*5fc97cc3SMitchell Horneutility, or at the
620*5fc97cc3SMitchell Horne.Xr ddb 4
621*5fc97cc3SMitchell Horneprompt after a panic with the 'show ktr' command.
622ebccf1e3SJoseph Koshy.Sh ERRORS
623d9a5f346SJoseph KoshyA command issued to the
624ebccf1e3SJoseph Koshy.Nm
625ebccf1e3SJoseph Koshydriver may fail with the following errors:
626ebccf1e3SJoseph Koshy.Bl -tag -width Er
627a5c506d2SJoseph Koshy.It Bq Er EAGAIN
628a5c506d2SJoseph KoshyHelper process creation failed for a
629a5c506d2SJoseph Koshy.Dv PMC_OP_CONFIGURELOG
630a5c506d2SJoseph Koshyrequest due to a temporary resource shortage in the kernel.
631ebccf1e3SJoseph Koshy.It Bq Er EBUSY
632d9a5f346SJoseph KoshyA
633d9a5f346SJoseph Koshy.Dv PMC_OP_CONFIGURELOG
634ebccf1e3SJoseph Koshyoperation was requested while an existing log was active.
635ebccf1e3SJoseph Koshy.It Bq Er EBUSY
636d9a5f346SJoseph KoshyA DISABLE operation was requested using the
637d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN
638ebccf1e3SJoseph Koshyrequest for a set of hardware resources currently in use for
639ebccf1e3SJoseph Koshyprocess-private PMCs.
640ebccf1e3SJoseph Koshy.It Bq Er EBUSY
641ebccf1e3SJoseph KoshyA
642d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN
643ebccf1e3SJoseph Koshyoperation was requested on an active system mode PMC.
644ebccf1e3SJoseph Koshy.It Bq Er EBUSY
645ebccf1e3SJoseph KoshyA
646d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH
647ebccf1e3SJoseph Koshyoperation was requested for a target process that already had another
648ebccf1e3SJoseph KoshyPMC using the same hardware resources attached to it.
649ebccf1e3SJoseph Koshy.It Bq Er EBUSY
650d9a5f346SJoseph KoshyA
651d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW
652ebccf1e3SJoseph Koshyrequest writing a new value was issued on a PMC that was active.
653953adc17SJoseph Koshy.It Bq Er EBUSY
654953adc17SJoseph KoshyA
655953adc17SJoseph Koshy.Dv PMC_OP_PMCSETCOUNT
656953adc17SJoseph Koshyrequest was issued on a PMC that was active.
6573bc6c674SJoseph Koshy.It Bq Er EDOOFUS
6583bc6c674SJoseph KoshyA
659d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART
6603bc6c674SJoseph Koshyoperation was requested without a log file being configured for a
6613bc6c674SJoseph KoshyPMC allocated with
6623bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCCSW
6633bc6c674SJoseph Koshyand
6643bc6c674SJoseph Koshy.Dv PMC_F_LOG_PROCEXIT
6653bc6c674SJoseph Koshymodifiers.
6661e999888SJoseph Koshy.It Bq Er EDOOFUS
6671e999888SJoseph KoshyA
6681e999888SJoseph Koshy.Dv PMC_OP_PMCSTART
6691e999888SJoseph Koshyoperation was requested on a system-wide sampling PMC without a log
6701e999888SJoseph Koshyfile being configured.
671ebccf1e3SJoseph Koshy.It Bq Er EEXIST
672ebccf1e3SJoseph KoshyA
673d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH
674ebccf1e3SJoseph Koshyrequest was reissued for a target process that already is the target
675ebccf1e3SJoseph Koshyof this PMC.
676ebccf1e3SJoseph Koshy.It Bq Er EFAULT
677ebccf1e3SJoseph KoshyA bad address was passed in to the driver.
678ebccf1e3SJoseph Koshy.It Bq Er EINVAL
679e805d32bSJoseph KoshyAn invalid PMC handle was specified.
680ebccf1e3SJoseph Koshy.It Bq Er EINVAL
681d9a5f346SJoseph KoshyAn invalid CPU number was passed in for a
682d9a5f346SJoseph Koshy.Dv PMC_OP_GETPMCINFO
683ebccf1e3SJoseph Koshyoperation.
684ebccf1e3SJoseph Koshy.It Bq Er EINVAL
6858e63e787SJessica ClarkeThe
6868e63e787SJessica Clarke.Ar pm_flags
6878e63e787SJessica Clarkeargument to a
6888e63e787SJessica Clarke.Dv PMC_OP_CONFIGURELOG
6898e63e787SJessica Clarkerequest contained unknown flags.
6908e63e787SJessica Clarke.It Bq Er EINVAL
691a5c506d2SJoseph KoshyA
692a5c506d2SJoseph Koshy.Dv PMC_OP_CONFIGURELOG
693a5c506d2SJoseph Koshyrequest to de-configure a log file was issued without a log file
694a5c506d2SJoseph Koshybeing configured.
695a5c506d2SJoseph Koshy.It Bq Er EINVAL
696a5c506d2SJoseph KoshyA
697a5c506d2SJoseph Koshy.Dv PMC_OP_FLUSHLOG
698a5c506d2SJoseph Koshyrequest was issued without a log file being configured.
699a5c506d2SJoseph Koshy.It Bq Er EINVAL
700d9a5f346SJoseph KoshyAn invalid CPU number was passed in for a
701d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN
702ebccf1e3SJoseph Koshyoperation.
703ebccf1e3SJoseph Koshy.It Bq Er EINVAL
704d9a5f346SJoseph KoshyAn invalid operation request was passed in for a
705d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN
706ebccf1e3SJoseph Koshyoperation.
707ebccf1e3SJoseph Koshy.It Bq Er EINVAL
708d9a5f346SJoseph KoshyAn invalid PMC ID was passed in for a
709d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN
710ebccf1e3SJoseph Koshyoperation.
711ebccf1e3SJoseph Koshy.It Bq Er EINVAL
712ebccf1e3SJoseph KoshyA suitable PMC matching the parameters passed in to a
713d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
714ebccf1e3SJoseph Koshyrequest could not be allocated.
715ebccf1e3SJoseph Koshy.It Bq Er EINVAL
716ebccf1e3SJoseph KoshyAn invalid PMC mode was requested during a
717d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
718ebccf1e3SJoseph Koshyrequest.
719ebccf1e3SJoseph Koshy.It Bq Er EINVAL
720ebccf1e3SJoseph KoshyAn invalid CPU number was specified during a
721d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
722ebccf1e3SJoseph Koshyrequest.
723ebccf1e3SJoseph Koshy.It Bq Er EINVAL
724d9a5f346SJoseph KoshyA CPU other than
725d9a5f346SJoseph Koshy.Dv PMC_CPU_ANY
726ebccf1e3SJoseph Koshywas specified in a
727a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
728ebccf1e3SJoseph Koshyrequest for a process-private PMC.
729ebccf1e3SJoseph Koshy.It Bq Er EINVAL
730d9a5f346SJoseph KoshyA CPU number of
731d9a5f346SJoseph Koshy.Dv PMC_CPU_ANY
732ebccf1e3SJoseph Koshywas specified in a
733a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
734ebccf1e3SJoseph Koshyrequest for a system-wide PMC.
735ebccf1e3SJoseph Koshy.It Bq Er EINVAL
736ebccf1e3SJoseph KoshyThe
737ebccf1e3SJoseph Koshy.Ar pm_flags
738ebccf1e3SJoseph Koshyargument to an
739d9a5f346SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
740ebccf1e3SJoseph Koshyrequest contained unknown flags.
741ebccf1e3SJoseph Koshy.It Bq Er EINVAL
742a5c506d2SJoseph Koshy(On Intel Pentium 4 CPUs with HTT support)
743a5c506d2SJoseph KoshyA
744a5c506d2SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
745a5c506d2SJoseph Koshyrequest for a process-private PMC was issued for an event that does
746a5c506d2SJoseph Koshynot support counting on a per-logical CPU basis.
747a5c506d2SJoseph Koshy.It Bq Er EINVAL
748ebccf1e3SJoseph KoshyA PMC allocated for system-wide operation was specified with a
749d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH
750a5c506d2SJoseph Koshyor
751a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH
752ebccf1e3SJoseph Koshyrequest.
753ebccf1e3SJoseph Koshy.It Bq Er EINVAL
754ebccf1e3SJoseph KoshyThe
755ebccf1e3SJoseph Koshy.Ar pm_pid
756ebccf1e3SJoseph Koshyargument to a
757d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH
758a5c506d2SJoseph Koshyor
759a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH
760d9a5f346SJoseph Koshyrequest specified an illegal process ID.
761ebccf1e3SJoseph Koshy.It Bq Er EINVAL
762ebccf1e3SJoseph KoshyA
763d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH
764ebccf1e3SJoseph Koshyrequest was issued for a PMC not attached to the target process.
765ebccf1e3SJoseph Koshy.It Bq Er EINVAL
766ebccf1e3SJoseph KoshyArgument
767ebccf1e3SJoseph Koshy.Ar pm_flags
768ebccf1e3SJoseph Koshyto a
769d9a5f346SJoseph Koshy.Dv PMC_OP_PMCRW
770ebccf1e3SJoseph Koshyrequest contained illegal flags.
771ebccf1e3SJoseph Koshy.It Bq Er EINVAL
772ebccf1e3SJoseph KoshyA
773d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR
774c5153e19SJoseph Koshyoperation was requested for a PMC not in process-virtual mode, or
775c5153e19SJoseph Koshyfor a PMC that is not solely attached to its owner process, or for
776c5153e19SJoseph Koshya PMC that was allocated with flag
777d9a5f346SJoseph Koshy.Dv PMC_F_DESCENDANTS .
778ebccf1e3SJoseph Koshy.It Bq Er EINVAL
779a5c506d2SJoseph KoshyA
780a5c506d2SJoseph Koshy.Dv PMC_OP_WRITELOG
781a5c506d2SJoseph Koshyrequest was issued for an owner process without a log file
782a5c506d2SJoseph Koshyconfigured.
783ebccf1e3SJoseph Koshy.It Bq Er ENOMEM
784ebccf1e3SJoseph KoshyThe system was not able to allocate kernel memory.
785ebccf1e3SJoseph Koshy.It Bq Er ENOSYS
786a5c506d2SJoseph Koshy(On i386 and amd64 architectures)
787d9a5f346SJoseph KoshyA
788d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR
789ebccf1e3SJoseph Koshyoperation was requested for hardware that does not support reading
790ebccf1e3SJoseph KoshyPMCs directly with the RDPMC instruction.
791ebccf1e3SJoseph Koshy.It Bq Er ENXIO
792d9a5f346SJoseph KoshyA
793d9a5f346SJoseph Koshy.Dv PMC_OP_GETPMCINFO
7946455febbSJoseph Koshyoperation was requested for an absent or disabled CPU.
795ebccf1e3SJoseph Koshy.It Bq Er ENXIO
7966455febbSJoseph KoshyA
7976455febbSJoseph Koshy.Dv PMC_OP_PMCALLOCATE
7986455febbSJoseph Koshyoperation specified allocation of a system-wide PMC on an absent or
7996455febbSJoseph Koshydisabled CPU.
800ebccf1e3SJoseph Koshy.It Bq Er ENXIO
801ebccf1e3SJoseph KoshyA
802d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTART
803ebccf1e3SJoseph Koshyor
804d9a5f346SJoseph Koshy.Dv PMC_OP_PMCSTOP
8056455febbSJoseph Koshyrequest was issued for a system-wide PMC that was allocated on a CPU
8066455febbSJoseph Koshythat is currently absent or disabled.
807953adc17SJoseph Koshy.It Bq Er EOPNOTSUPP
808953adc17SJoseph KoshyA
809953adc17SJoseph Koshy.Dv PMC_OP_PMCALLOCATE
810953adc17SJoseph Koshyrequest was issued for PMC capabilities not supported
811953adc17SJoseph Koshyby the specified PMC class.
812fe5ca00aSJoseph Koshy.It Bq Er EOPNOTSUPP
813fe5ca00aSJoseph Koshy(i386 architectures)
814fe5ca00aSJoseph KoshyA sampling mode PMC was requested on a CPU lacking an APIC.
815ebccf1e3SJoseph Koshy.It Bq Er EPERM
816d9a5f346SJoseph KoshyA
817d9a5f346SJoseph Koshy.Dv PMC_OP_PMCADMIN
818ebccf1e3SJoseph Koshyrequest was issued by a process without super-user
819ebccf1e3SJoseph Koshyprivilege or by a jailed super-user process.
820ebccf1e3SJoseph Koshy.It Bq Er EPERM
821d9a5f346SJoseph KoshyA
822d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH
823ebccf1e3SJoseph Koshyoperation was issued for a target process that the current process
824ebccf1e3SJoseph Koshydoes not have permission to attach to.
825c5153e19SJoseph Koshy.It Bq Er EPERM
826d9a5f346SJoseph Koshy(i386 and amd64 architectures)
827d9a5f346SJoseph KoshyA
828d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH
829c5153e19SJoseph Koshyoperation was issued on a PMC whose MSR has been retrieved using
830d9a5f346SJoseph Koshy.Dv PMC_OP_PMCX86GETMSR .
831ebccf1e3SJoseph Koshy.It Bq Er ESRCH
832f263522aSJoseph KoshyA process issued a PMC operation request without having allocated any
833f263522aSJoseph KoshyPMCs.
834f263522aSJoseph Koshy.It Bq Er ESRCH
835f263522aSJoseph KoshyA process issued a PMC operation request after the PMC was detached
836f263522aSJoseph Koshyfrom all of its target processes.
837ebccf1e3SJoseph Koshy.It Bq Er ESRCH
838ebccf1e3SJoseph KoshyA
839d9a5f346SJoseph Koshy.Dv PMC_OP_PMCATTACH
840a5c506d2SJoseph Koshyor
841a5c506d2SJoseph Koshy.Dv PMC_OP_PMCDETACH
842d9a5f346SJoseph Koshyrequest specified a non-existent process ID.
843ebccf1e3SJoseph Koshy.It Bq Er ESRCH
844ebccf1e3SJoseph KoshyThe target process for a
845d9a5f346SJoseph Koshy.Dv PMC_OP_PMCDETACH
846a5c506d2SJoseph Koshyoperation is not being monitored by
847a5c506d2SJoseph Koshy.Nm .
848ebccf1e3SJoseph Koshy.El
849ebccf1e3SJoseph Koshy.Sh SEE ALSO
850ebccf1e3SJoseph Koshy.Xr kenv 1 ,
851ebccf1e3SJoseph Koshy.Xr pmc 3 ,
8526d6a103aSJoseph Koshy.Xr pmclog 3 ,
853*5fc97cc3SMitchell Horne.Xr ddb 4 ,
854*5fc97cc3SMitchell Horne.Xr ktr 4 ,
855ebccf1e3SJoseph Koshy.Xr kldload 8 ,
856*5fc97cc3SMitchell Horne.Xr ktrdump 8 ,
857ebccf1e3SJoseph Koshy.Xr pmccontrol 8 ,
858ebccf1e3SJoseph Koshy.Xr pmcstat 8 ,
859ebccf1e3SJoseph Koshy.Xr sysctl 8 ,
860a5c506d2SJoseph Koshy.Xr kproc_create 9 ,
861ebccf1e3SJoseph Koshy.Xr p_candebug 9
8626d6a103aSJoseph Koshy.Sh HISTORY
8636d6a103aSJoseph KoshyThe
8646d6a103aSJoseph Koshy.Nm
8656d6a103aSJoseph Koshydriver first appeared in
8666d6a103aSJoseph Koshy.Fx 6.0 .
867e805d32bSJoseph Koshy.Sh AUTHORS
868e805d32bSJoseph KoshyThe
869e805d32bSJoseph Koshy.Nm
870e805d32bSJoseph Koshydriver was written by
8716c899950SBaptiste Daroussin.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org .
872d9a5f346SJoseph Koshy.Sh BUGS
873d9a5f346SJoseph KoshyThe driver samples the state of the kernel's logical processor support
874d9a5f346SJoseph Koshyat the time of initialization (i.e., at module load time).
875d9a5f346SJoseph KoshyOn CPUs supporting logical processors, the driver could misbehave if
876d9a5f346SJoseph Koshylogical processors are subsequently enabled or disabled while the
877d9a5f346SJoseph Koshydriver is active.
878953adc17SJoseph Koshy.Pp
87979247772SJoseph KoshyOn the i386 architecture, the driver requires that the local APIC on the
880953adc17SJoseph KoshyCPU be enabled for sampling mode to be supported.
881953adc17SJoseph KoshyMany single-processor motherboards keep the APIC disabled in BIOS; on
882953adc17SJoseph Koshysuch systems
883953adc17SJoseph Koshy.Nm
884953adc17SJoseph Koshywill not support sampling PMCs.
8850afc94c1SUlrich Spörlein.Sh SECURITY CONSIDERATIONS
8860afc94c1SUlrich SpörleinPMCs may be used to monitor the actual behavior of the system on hardware.
8870afc94c1SUlrich SpörleinIn situations where this constitutes an undesirable information leak,
8880afc94c1SUlrich Spörleinthe following options are available:
8890afc94c1SUlrich Spörlein.Bl -enum
8900afc94c1SUlrich Spörlein.It
8910afc94c1SUlrich SpörleinSet the
8920afc94c1SUlrich Spörlein.Xr sysctl 8
8930afc94c1SUlrich Spörleintunable
8940afc94c1SUlrich Spörlein.Va security.bsd.unprivileged_syspmcs
8950afc94c1SUlrich Spörleinto 0.
8960afc94c1SUlrich SpörleinThis ensures that unprivileged processes cannot allocate system-wide
8970afc94c1SUlrich SpörleinPMCs and thus cannot observe the hardware behavior of the system
8980afc94c1SUlrich Spörleinas a whole.
8990afc94c1SUlrich SpörleinThis tunable may also be set at boot time using
9000afc94c1SUlrich Spörlein.Xr loader 8 ,
9010afc94c1SUlrich Spörleinor with
9020afc94c1SUlrich Spörlein.Xr kenv 1
9030afc94c1SUlrich Spörleinprior to loading the
9040afc94c1SUlrich Spörlein.Nm
9050afc94c1SUlrich Spörleindriver into the kernel.
9060afc94c1SUlrich Spörlein.It
9070afc94c1SUlrich SpörleinSet the
9080afc94c1SUlrich Spörlein.Xr sysctl 8
9090afc94c1SUlrich Spörleintunable
9100afc94c1SUlrich Spörlein.Va security.bsd.unprivileged_proc_debug
9110afc94c1SUlrich Spörleinto 0.
9120afc94c1SUlrich SpörleinThis will ensure that an unprivileged process cannot attach a PMC
9130afc94c1SUlrich Spörleinto any process other than itself and thus cannot observe the hardware
9140afc94c1SUlrich Spörleinbehavior of other processes with the same credentials.
9150afc94c1SUlrich Spörlein.El
9160afc94c1SUlrich Spörlein.Pp
9170afc94c1SUlrich SpörleinSystem administrators should note that on IA-32 platforms
9180afc94c1SUlrich Spörlein.Fx
9190afc94c1SUlrich Spörleinmakes the content of the IA-32 TSC counter available to all processes
9200afc94c1SUlrich Spörleinvia the RDTSC instruction.
921