HWPMC(4) | Device Drivers Manual | HWPMC(4) |
hwpmc
— Hardware
Performance Monitoring Counter support
options HWPMC_HOOKS
device hwpmc
Additionally, for i386 systems:
device apic
The hwpmc
driver virtualizes the hardware
performance monitoring facilities in modern CPUs and provides support for
using these facilities from user level processes.
The driver supports multi-processor systems.
PMCs are allocated using the
PMC_OP_PMCALLOCATE
request. A successful
PMC_OP_PMCALLOCATE
request will return a handle to
the requesting process. Subsequent operations on the allocated PMC use this
handle to denote the specific PMC. A process that has successfully allocated
a PMC is termed an “owner process”.
PMCs may be allocated with process or system scope.
PMCs may be allocated for counting or for sampling:
PMC_OP_PMCREAD
system call
on all architectures. Some architectures offer faster methods of reading
these counts.Scope and operational mode are orthogonal; a PMC may thus be configured to operate in one of the following four modes:
PMC_OP_SETCOUNT
operation. Applications can read
the value of the PMC anytime using the
PMC_OP_PMCRW
operation.PMC_OP_SETCOUNT
operation prior to starting the
PMC. Log files are configured using the
PMC_OP_CONFIGURELOG
operation.PMC_OP_PMCRW
request. These PMCs normally count
from zero, but the initial count may be set using the
PMC_OP_SETCOUNT
operation.PMC_OP_SETCOUNT
operation prior to starting the
PMC. Log files are configured using the
PMC_OP_CONFIGURELOG
operation.
System-wide statistical sampling can only be enabled by a process with super-user privileges.
Processes are allowed to allocate as many PMCs as the hardware and current operating conditions permit. Processes may mix allocations of system-wide and process-private PMCs. Multiple processes may be using PMCs simultaneously.
Allocated PMCs are started using the
PMC_OP_PMCSTART
operation, and stopped using the
PMC_OP_PMCSTOP
operation. Stopping and starting a
PMC is permitted at any time the owner process has a valid handle to the
PMC.
Process-private PMCs need to be attached to a target process
before they can be used. Attaching a process to a PMC is done using the
PMC_OP_PMCATTACH
operation. An already attached PMC
may be detached from its target process using the converse
PMC_OP_PMCDETACH
operation. Issuing a
PMC_OP_PMCSTART
operation on an as yet unattached
PMC will cause it to be attached to its owner process. The following rules
determine whether a given process may attach a PMC to another target
process:
PMCs are released using PMC_OP_PMCRELEASE
.
After a successful PMC_OP_PMCRELEASE
operation the
handle to the PMC will become invalid.
The PMC_OP_PMCALLOCATE
operation supports
the following flags that modify the behavior of an allocated PMC:
PMC_F_CALLCHAIN
PMC_F_DESCENDANTS
PMC_F_KGMON
PMC_F_LOG_PROCCSW
hwpmc
will log a record containing the number of
hardware events seen by the target process when it was scheduled on the
CPU.PMC_F_LOG_PROCEXIT
hwpmc
will
maintain per-process counts for each target process attached to a PMC. At
process exit time, a record containing the target process' PID and the
accumulated per-process count for that process will be written to the
configured log file.Modifiers PMC_F_LOG_PROCEXIT
and
PMC_F_LOG_PROCCSW
may be used in combination with
modifier PMC_F_DESCENDANTS
to track the behavior of
complex pipelines of processes. PMCs with modifiers
PMC_F_LOG_PROCEXIT
and
PMC_F_LOG_PROCCSW
cannot be started until their
owner process has configured a log file.
The hwpmc
driver may deliver signals to
processes that have allocated PMCs:
A PMC row is defined as the set of PMC resources at the same hardware address in the CPUs in a system. Since process scope PMCs need to move between CPUs following their target threads, allocation of a process scope PMC reserves all PMCs in a PMC row for use only with process scope PMCs. Accordingly a PMC row will be in one of the following dispositions:
PMC_DISP_FREE
PMC_DISP_THREAD
PMC_DISP_STANDALONE
The recommended way for application programs to use the facilities
of the hwpmc
driver is using the API provided by the
pmc(3) library.
The hwpmc
driver operates using a system
call number that is dynamically allotted to it when it is loaded into the
kernel.
The hwpmc
driver supports the following
operations:
PMC_OP_CONFIGURELOG
hwpmc
driver will write log data to this file
asynchronously. If it encounters an error, logging will be stopped and the
error code encountered will be saved for subsequent retrieval by a
PMC_OP_FLUSHLOG
request.PMC_OP_FLUSHLOG
hwpmc
to a
configured output file. This operation returns to the caller after the
write operation has returned. The returned error code reflects any pending
error state inside hwpmc
.PMC_OP_GETCPUINFO
PMC_OP_GETDRIVERSTATS
hwpmc
itself).PMC_OP_GETMODULEVERSION
PMC_OP_GETPMCINFO
PMC_OP_PMCADMIN
hwpmc
driver. The
invoking process needs to possess the
PRIV_PMC_MANAGE
privilege.PMC_OP_PMCALLOCATE
PMC_OP_PMCATTACH
If the PMC_F_DESCENDANTS
flag had been
specified at PMC allocation time, then the PMC is attached to all
current and future descendants of the target process.
PMC_OP_PMCDETACH
PMC_OP_PMCRELEASE
PMC_OP_PMCRW
PMC_OP_SETCOUNT
PMC_OP_PMCSTART
PMC_OP_PMCSTOP
PMC_OP_WRITELOG
Some i386 family CPUs support the RDPMC instruction which allows a
user process to read a PMC value without needing to invoke a
PMC_OP_PMCRW
operation. On such CPUs, the machine
address associated with an allocated PMC is retrievable using the
PMC_OP_PMCX86GETMSR
system call.
PMC_OP_PMCX86GETMSR
The PMC needs to be in process-private mode and allocated
without the PMC_F_DESCENDANTS
modifier flag, and
should be attached only to its owner process at the time of the
call.
AMD64 CPUs support the RDPMC instruction which allows a user
process to read a PMC value without needing to invoke a
PMC_OP_PMCRW
operation. The machine address
associated with an allocated PMC is retrievable using the
PMC_OP_PMCX86GETMSR
system call.
PMC_OP_PMCX86GETMSR
The PMC needs to be in process-private mode and allocated
without the PMC_F_DESCENDANTS
modifier flag, and
should be attached only to its owner process at the time of the
call.
The behavior of hwpmc
is influenced by the
following sysctl(8) and loader(8)
tunables:
hwpmc
driver was compiled
with -DDEBUG
.) Control the verbosity of debug
messages from the hwpmc
driver.hwpmc
's logging function. The default buffer size
is 4KB.hwpmc
for
logging. The default is 64.hwpmc
driver will only allow
privileged processes to attach PMCs to other processes.These variables may be set in the kernel environment using
kenv(1) before hwpmc
is
loaded.
The kernel driver requires all physical CPUs in an SMP system to have identical performance monitoring counter hardware.
On platforms that sparsely number CPUs and which support hot-plugging of CPUs, requests that specify non-existent or disabled CPUs will fail with an error. Applications allocating system-scope PMCs need to be aware of the possibility of such transient failures.
Historically, on the x86 architecture,
FreeBSD has permitted user processes running at a
processor CPL of 3 to read the TSC using the RDTSC instruction. The
hwpmc
driver preserves this behavior.
On CPUs with HTT support, Intel P4 PMCs are capable of qualifying
only a subset of hardware events on a per-logical CPU basis. Consequently,
if HTT is enabled on a system with Intel Pentium P4 PMCs, then the
hwpmc
driver will reject allocation requests for
process-private PMCs that request counting of hardware events that cannot be
counted separately for each logical CPU.
Writing a value to the PMC MSRs found in Intel Pentium-Pro style
PMCs (found in Intel Pentium Pro, Pentium II, Pentium III, Pentium M and
Celeron processors) will replicate bit 31 of the value being written into
the upper 8 bits of the MSR, bringing down the usable width of these PMCs to
31 bits. For process-virtual PMCs, the hwpmc
driver
implements a workaround in software and makes the corrected 64 bit count
available via the PMC_OP_RW
operation. Processes
that intend to use RDPMC instructions directly or that intend to write
values larger than 2^31 into these PMCs with
PMC_OP_RW
need to be aware of this hardware
limitation.
HWPMC_HOOKS
.The hwpmc
driver is currently under
development. The API and ABI documented in this manual page may change in
the future. The recommended method of accessing this driver is using the
pmc(3) API.
A command issued to the hwpmc
driver may
fail with the following errors:
EAGAIN
]PMC_OP_CONFIGURELOG
request due to a temporary
resource shortage in the kernel.EBUSY
]PMC_OP_CONFIGURELOG
operation was requested
while an existing log was active.EBUSY
]PMC_OP_PMCADMIN
request for a set of hardware
resources currently in use for process-private PMCs.EBUSY
]PMC_OP_PMCADMIN
operation was requested on an
active system mode PMC.EBUSY
]PMC_OP_PMCATTACH
operation was requested for a
target process that already had another PMC using the same hardware
resources attached to it.EBUSY
]PMC_OP_PMCRW
request writing a new value was
issued on a PMC that was active.EBUSY
]PMC_OP_PMCSETCOUNT
request was issued on a PMC
that was active.EDOOFUS
]PMC_OP_PMCSTART
operation was requested without
a log file being configured for a PMC allocated with
PMC_F_LOG_PROCCSW
and
PMC_F_LOG_PROCEXIT
modifiers.EDOOFUS
]PMC_OP_PMCSTART
operation was requested on a
system-wide sampling PMC without a log file being configured.EEXIST
]PMC_OP_PMCATTACH
request was reissued for a
target process that already is the target of this PMC.EFAULT
]EINVAL
]EINVAL
]PMC_OP_GETPMCINFO
operation.EINVAL
]PMC_OP_CONFIGURELOG
request to de-configure a
log file was issued without a log file being configured.EINVAL
]PMC_OP_FLUSHLOG
request was issued without a log
file being configured.EINVAL
]PMC_OP_PMCADMIN
operation.EINVAL
]PMC_OP_PMCADMIN
operation.EINVAL
]PMC_OP_PMCADMIN
operation.EINVAL
]PMC_OP_PMCALLOCATE
request could not be
allocated.EINVAL
]PMC_OP_PMCALLOCATE
request.EINVAL
]PMC_OP_PMCALLOCATE
request.EINVAL
]PMC_CPU_ANY
was specified in a
PMC_OP_PMCALLOCATE
request for a process-private
PMC.EINVAL
]PMC_CPU_ANY
was specified in a
PMC_OP_PMCALLOCATE
request for a system-wide
PMC.EINVAL
]PMC_OP_PMCALLOCATE
request contained unknown
flags.EINVAL
]PMC_OP_PMCALLOCATE
request for a process-private
PMC was issued for an event that does not support counting on a
per-logical CPU basis.EINVAL
]PMC_OP_PMCATTACH
or
PMC_OP_PMCDETACH
request.EINVAL
]PMC_OP_PMCATTACH
or
PMC_OP_PMCDETACH
request specified an illegal
process ID.EINVAL
]PMC_OP_PMCDETACH
request was issued for a PMC
not attached to the target process.EINVAL
]PMC_OP_PMCRW
request contained illegal flags.EINVAL
]PMC_OP_PMCX86GETMSR
operation was requested for
a PMC not in process-virtual mode, or for a PMC that is not solely
attached to its owner process, or for a PMC that was allocated with flag
PMC_F_DESCENDANTS
.EINVAL
]PMC_OP_WRITELOG
request was issued for an owner
process without a log file configured.ENOMEM
]ENOSYS
]PMC_OP_PMCX86GETMSR
operation was requested for
hardware that does not support reading PMCs directly with the RDPMC
instruction.ENXIO
]PMC_OP_GETPMCINFO
operation was requested for an
absent or disabled CPU.ENXIO
]PMC_OP_PMCALLOCATE
operation specified
allocation of a system-wide PMC on an absent or disabled CPU.ENXIO
]PMC_OP_PMCSTART
or
PMC_OP_PMCSTOP
request was issued for a
system-wide PMC that was allocated on a CPU that is currently absent or
disabled.EOPNOTSUPP
]PMC_OP_PMCALLOCATE
request was issued for PMC
capabilities not supported by the specified PMC class.EOPNOTSUPP
]EPERM
]PMC_OP_PMCADMIN
request was issued by a process
without super-user privilege or by a jailed super-user process.EPERM
]PMC_OP_PMCATTACH
operation was issued for a
target process that the current process does not have permission to attach
to.EPERM
]PMC_OP_PMCATTACH
operation was issued on a PMC whose MSR has been retrieved using
PMC_OP_PMCX86GETMSR
.ESRCH
]ESRCH
]ESRCH
]PMC_OP_PMCATTACH
or
PMC_OP_PMCDETACH
request specified a non-existent
process ID.ESRCH
]PMC_OP_PMCDETACH
operation is not being monitored by hwpmc
.kenv(1), pmc(3), pmclog(3), kgmon(8), kldload(8), pmccontrol(8), pmcstat(8), sysctl(8), kproc_create(9), p_candebug(9)
The hwpmc
driver first appeared in
FreeBSD 6.0.
The hwpmc
driver was written by
Joseph Koshy
<jkoshy@FreeBSD.org>.
The driver samples the state of the kernel's logical processor support at the time of initialization (i.e., at module load time). On CPUs supporting logical processors, the driver could misbehave if logical processors are subsequently enabled or disabled while the driver is active.
On the i386 architecture, the driver requires that the local APIC
on the CPU be enabled for sampling mode to be supported. Many
single-processor motherboards keep the APIC disabled in BIOS; on such
systems hwpmc
will not support sampling PMCs.
PMCs may be used to monitor the actual behavior of the system on hardware. In situations where this constitutes an undesirable information leak, the following options are available:
hwpmc
driver into the kernel.System administrators should note that on IA-32 platforms FreeBSD makes the content of the IA-32 TSC counter available to all processes via the RDTSC instruction.
November 2, 2012 | Debian |