PMC(3)
πΈ NAME πΈ
Okay so bestie, you're looking at pmc(3) β the library for accessing hardware performance monitoring counters. That's the whole name, no cap. It's the manual page for the pmc library, and we're gonna make hardware counters make sense. π
π LIBRARY π
Performance Counters Library (libpmc, -lpmc) β that's the glow-up. Link it with -lpmc and you're in.
β¨ SYNOPSIS β¨
#include <pmc.h>
π DESCRIPTION π
The Performance Counters Library (libpmc, -lpmc) gives you a programming interface that lets applications use hardware performance counters to gather performance data about specific processes or for the system as a whole. The library is implemented using the lower-level facilities offered by the hwpmc(4) driver. Slay. π₯
π§ Key Concepts π§
Performance monitoring counters (PMCs) are represented by the library using a software abstraction. These βabstractβ PMCs can have two scopes:
- System scope. These PMCs measure events in a whole-system manner, i.e., independent of the currently executing thread. System scope PMCs are allocated on specific CPUs and do not migrate between CPUs. Non-privileged processes are allowed to allocate system scope PMCs if the
hwpmc(4)sysctl tunablesecurity.bsd.unprivileged_syspmcsis non-zero. π - Process scope. These PMCs only measure hardware events when the processes they are attached to are executing on a CPU. In an SMP system, process scope PMCs migrate between CPUs along with their target processes. π¦
Orthogonal to PMC scope, PMCs may be allocated in one of two operational modes:
- Counting PMCs measure events according to their scope (system or process). The application needs to explicitly read these counters to retrieve their value. π
- Sampling PMCs cause the CPU to be periodically interrupted and information about its state of execution to be collected. Sampling PMCs are used to profile specific processes and kernel threads or to profile the system as a whole. π₯
The scope and operational mode for a software PMC are specified at PMC allocation time. An application is allowed to allocate multiple PMCs subject to availability of hardware resources. No gatekeeping beyond that, bestie.
The library uses human-readable strings to name the event being measured by hardware. The syntax used for specifying a hardware event along with additional event specific qualifiers (if any) is described in detail in section EVENT SPECIFIERS below. Yes, we're getting there.
PMCs are associated with the process that allocated them and will be automatically reclaimed by the system when the process exits. Additionally, process-scope PMCs have to be attached to one or more target processes before they can perform measurements. A process-scope PMC may be attached to those target processes that its owner process would otherwise be permitted to debug. An owner process may attach PMCs to itself allowing it to measure its own behavior. Additionally, on some machine architectures, such self-attached PMCs may be read cheaply using specialized instructions supported by the processor. Iconic. π
Certain kinds of PMCs require that a log file be configured before they may be started. These include:
- System scope sampling PMCs.
- Process scope sampling PMCs.
- Process scope counting PMCs that have been configured to report PMC readings on process context switches or process exits.
Up to one log file may be configured per owner process. Events logged to a log file may be subsequently analyzed using the pmclog(3) family of functions. π«Ά
π Supported CPUs π
The CPUs known to the PMC library are named by the enum pmc_cputype enumeration. Supported CPUs include:
PMC_CPU_AMD_K8β AMD Athlon64 CPUs.PMC_CPU_ARMV7_CORTEX_A5β ARMv7 Cortex A5 CPUs.PMC_CPU_ARMV7_CORTEX_A7β ARMv7 Cortex A7 CPUs.PMC_CPU_ARMV7_CORTEX_A8β ARMv7 Cortex A8 CPUs.PMC_CPU_ARMV7_CORTEX_A9β ARMv7 Cortex A9 CPUs.PMC_CPU_ARMV7_CORTEX_A15β ARMv7 Cortex A15 CPUs.PMC_CPU_ARMV7_CORTEX_A17β ARMv7 Cortex A17 CPUs.PMC_CPU_ARMV8_CORTEX_A53β ARMv8 Cortex A53 CPUs.PMC_CPU_ARMV8_CORTEX_A57β ARMv8 Cortex A57 CPUs.PMC_CPU_ARMV8_CORTEX_A76β ARMv8 Cortex A76 CPUs.GENERICβ Generic.PMC_CPU_INTEL_ATOMβ Intel Atom CPUs and other CPUs conforming to version 3 of the Intel performance measurement architecture.PMC_CPU_INTEL_COREβ Intel Core Solo and Core Duo CPUs, and other CPUs conforming to version 1 of the Intel performance measurement architecture.PMC_CPU_INTEL_CORE2β Intel Core2 Solo, Core2 Duo and Core2 Extreme CPUs, and other CPUs conforming to version 2 of the Intel performance measurement architecture.PMC_CPU_PPC_7450β PowerPC MPC7450 CPUs.PMC_CPU_PPC_970β IBM PowerPC 970 CPUs.PMC_CPU_PPC_E500β PowerPC e500 Core CPUs.PMC_CPU_PPC_POWER8β IBM POWER8 and POWER9 CPUs.
π¦ Supported PMCs π¦
PMCs supported by this library are named by the enum pmc_class enumeration. Supported PMC classes include:
PMC_CLASS_IAFβ Fixed function hardware counters present in CPUs conforming to the Intel performance measurement architecture version 2 and later.PMC_CLASS_IAPβ Programmable hardware counters present in CPUs conforming to the Intel performance measurement architecture version 1 and later.PMC_CLASS_K8β Programmable hardware counters present in AMD Athlon64 CPUs.PMC_CLASS_TSCβ The timestamp counter oni386andamd64architecture CPUs.PMC_CLASS_ARMV7β ARMv7.PMC_CLASS_ARMV8β ARMv8.PMC_CLASS_PPC970β IBM PowerPC 970 class.PMC_CLASS_POWER8β IBM POWER8 class.PMC_CLASS_SOFTβ Software events.
π PMC Capabilities π
Capabilities of performance monitoring hardware are denoted using the enum pmc_caps enumeration. Supported capabilities include:
PMC_CAP_CASCADEβ The ability to cascade counters.PMC_CAP_DOMWIDEβ Separate counters tied to each NUMA domain.PMC_CAP_EDGEβ The ability to count negated to asserted transitions of the hardware conditions being probed for.PMC_CAP_INTERRUPTβ The ability to interrupt the CPU.PMC_CAP_INVERTβ The ability to invert the sense of the hardware conditions being measured.PMC_CAP_PRECISEβ The ability to perform precise sampling.PMC_CAP_QUALIFIERβ The hardware allows monitored to be further qualified in some system dependent way.PMC_CAP_READβ The ability to read from performance counters.PMC_CAP_SYSTEMβ The ability to restrict counting of hardware events to when the CPU is running privileged code.PMC_CAP_SYSWIDEβ A single counter aggregating events for the whole system.PMC_CAP_THRESHOLDβ The ability to ignore simultaneous hardware events below a programmable threshold.PMC_CAP_USERβ The ability to restrict counting of hardware events to those when the CPU is running unprivileged code.PMC_CAP_WRITEβ The ability to write to performance counters.
π CPU Naming Conventions π
CPUs are named using small integers from zero up to, but excluding, the value returned by function pmc_ncpu(). On platforms supporting sparsely numbered CPUs not all the numbers in this range will denote valid CPUs. Operations on non-existent CPUs will return an error. Don't come for me, that's just how it is. π
β Functional Grouping of the API β
This section contains a brief overview of the available functionality in the PMC library. Each function listed here is described further in its own manual page.
π Administration
pmc_disable(),pmc_enable()β Administratively disable (enable) specific performance monitoring counter hardware. Counters that are disabled will not be available to applications to use. π«
π Convenience Functions
pmc_event_names_of_class()β Returns a list of event names supported by a given PMC type.pmc_name_of_capability()β Convert aPMC_CAP_*flag to a human-readable string.pmc_name_of_class()β Convert aPMC_CLASS_*constant to a human-readable string.pmc_name_of_cputype()β Return a human-readable name for a CPU type.pmc_name_of_disposition()β Return a human-readable string describing a PMC's disposition.pmc_name_of_event()β Convert a numeric event code to a human-readable string.pmc_name_of_mode()β Convert aPMC_MODE_*constant to a human-readable name.pmc_name_of_state()β Return a human-readable string describing a PMC's current state.
π§ Library Initialization
pmc_init()β Initialize the library. This function must be called before any other library function. Like, before anything else. Periodt.
πΈ Log File Handling
pmc_configure_logfile()β Configure a log file forhwpmc(4)to write logged events to.pmc_flush_logfile()β Flush all pending log data inhwpmc(4)'s buffers.pmc_close_logfile()β Flush all pending log data and closehwpmc(4)'s side of the stream.pmc_writelog()β Append arbitrary user data to the current log file.
π₯ PMC Management
pmc_allocate(),pmc_release()β Allocate (free) a PMC.pmc_attach(),pmc_detach()β Attach (detach) a process scope PMC to a target.pmc_read(),pmc_write(),pmc_rw()β Read (write) a value from (to) a PMC.pmc_start(),pmc_stop()β Start (stop) a software PMC.pmc_set()β Set the reload value for a sampling PMC.
β¨ Queries
pmc_capabilities()β Retrieve the capabilities for a given PMC.pmc_cpuinfo()β Retrieve information about the CPUs and PMC hardware present in the system.pmc_get_driver_stats()β Retrieve statistics maintained byhwpmc(4).pmc_ncpu()β Determine the greatest possible CPU number on the system.pmc_npmc()β Return the number of hardware PMCs present in a given CPU.pmc_pmcinfo()β Return information about the state of a given CPU's PMCs.pmc_width()β Determine the width of a hardware counter in bits.
β¨ x86 Architecture Specific API β¨
pmc_get_msr()β returns the processor model specific register number that's associated withpmc, bestie. Once you've got it, your app can use the x86RDPMCinstruction to read the contents of that PMC directly. Slay, iykyk.
π¨ Signal Handling Requirements π¨
Okay so this part is important β applications using PMCs are required to handle these signals, no cap:
SIGBUSβ when thehwpmc(4)module gets unloaded usingkldunload(8), every process that has PMCs allocated to it gets sent aSIGBUS. Like, surprise, your counters are gone πSIGIOβ thehwpmc(4)driver sends aSIGIOto the process that owns a PMC whenever:- any process-mode PMC it allocated loses all of its target processes, or
- the driver hits an error while writing log data to a configured log file. You can grab that error with a later call to
pmc_flush_logfile().
π Typical Program Flow π
Here's the whole vibe of how your program should go, step by step:
- First, your app calls
pmc_init()so the library can initialize itself. Gotta start somewhere π - Then you'd set up your signal handling. (See above, bestie β you literally have to.)
- Next, your app allocates the PMCs it wants using
pmc_allocate(). - You can set initial values for those PMCs using
pmc_set(). - If a log file is necessary for the PMCs to work, configure it using
pmc_configure_logfile(). - Process scope PMCs get attached to their target processes using
pmc_attach(). - Then you start the PMCs using
pmc_start(). - Once they're started, you can read the values of counting PMCs using
pmc_read(). For PMCs that write events to the log file, you read and parse that logged data using thepmclog(3)family of functions. - PMCs get stopped using
pmc_stop(), and process scope PMCs get detached from their targets usingpmc_detach(). - Before the process exits, it can release its PMCs using
pmc_release(). And any configured log file can be closed usingpmc_configure_logfile().
Periodt. That's the whole arc π
πΈ EVENT SPECIFIERS πΈ
Okay so event specifiers are these strings made up of an event name, plus optional parameters that tweak the semantics of the hardware event you're probing. Event names depend on your PMC architecture β but the PMC library defines machine independent aliases for the commonly used events, which is honestly such a serve π
Spelling of event specifiers is case-insensitive, and here's the cute part: space characters, periods, underscores and hyphens are all considered equivalent to each other. So "Example Event", example-event, and EXAMPLE_EVENT are all literally the same thing. No notes, iconic π¦
π§ PMC Architecture Dependent Events π§
PMC architecture dependent event specifiers are described in these manual pages β go read them, bestie:
PMC_CLASS_IAFβ seepmc.iaf(3)PMC_CLASS_IAPβ seepmc.atom(3),pmc.core(3),pmc.core2(3)PMC_CLASS_K8β seepmc.amd(3)PMC_CLASS_TSCβ seepmc.tsc(3)
β Event Name Aliases β
Event name aliases are PMC-independent names for commonly used events. This version of the pmc library knows about the following aliases:
branchesβ measure the number of branches retired.branch-mispredictsβ measure the number of retired branches that were mispredicted. πcyclesβ measure processor cycles. This event is implemented using the processor's Time Stamp Counter register.dc-missesβ measure the number of data cache misses.ic-missesβ measure the number of instruction cache misses.instructionsβ measure the number of instructions retired.interruptsβ measure the number of interrupts seen.unhalted-cyclesβ measure the number of cycles the processor is not in a halted or sleep state.
π«Ά COMPATIBILITY π«Ά
Real talk: the interface between the pmc library and the hwpmc(4) driver is intended to be private to the implementation and may change. So to make forward compatibility with future versions of the hwpmc(4) driver easy, applications are urged to dynamically link with the pmc library. Doing anything else is unsupported β like, genuinely, don't π
π SEE ALSO π
pmc.amd(3), pmc.atom(3), pmc.core(3), pmc.core2(3), pmc.haswell(3), pmc.haswelluc(3), pmc.haswellxeon(3), pmc.iaf(3), pmc.ivybridge(3), pmc.ivybridgexeon(3), pmc.sandybridge(3), pmc.sandybridgeuc(3), pmc.sandybridgexeon(3), pmc.soft(3), pmc.tsc(3), pmc.westmere(3), pmc.westmereuc(3), pmc_allocate(3), pmc_attach(3), pmc_capabilities(3), pmc_configure_logfile(3), pmc_disable(3), pmc_event_names_of_class(3), pmc_get_driver_stats(3), pmc_get_msr(3), pmc_init(3), pmc_name_of_capability(3), pmc_read(3), pmc_set(3), pmc_start(3), pmclog(3), hwpmc(4), pmccontrol(8), pmcstat(8)
π HISTORY π
The pmc library first appeared in FreeBSD 6.0. Vintage, honestly π
π AUTHORS π
The Performance Counters Library (libpmc, -lpmc) library was written by Joseph Koshy <jkoshy@FreeBSD.org>. King behavior π«Ά
And that's the whole page, bestie! This lil' icon comes courtesy of the Debian crew, dated June 23, 2023, filed under PMC(3) β go forth and count those cycles β¨π
π
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2003-2008 Joseph Koshy. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms