PMC(3)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

🌸 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 tunable security.bsd.unprivileged_syspmcs is 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 on i386 and amd64 architecture 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 a PMC_CAP_* flag to a human-readable string.
  • pmc_name_of_class() β€” Convert a PMC_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 a PMC_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 for hwpmc(4) to write logged events to.
  • pmc_flush_logfile() β€” Flush all pending log data in hwpmc(4)'s buffers.
  • pmc_close_logfile() β€” Flush all pending log data and close hwpmc(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 by hwpmc(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 with pmc, bestie. Once you've got it, your app can use the x86 RDPMC instruction 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 the hwpmc(4) module gets unloaded using kldunload(8), every process that has PMCs allocated to it gets sent a SIGBUS. Like, surprise, your counters are gone πŸ’€
  • SIGIO β€” the hwpmc(4) driver sends a SIGIO to 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:

  1. First, your app calls pmc_init() so the library can initialize itself. Gotta start somewhere πŸ’–
  2. Then you'd set up your signal handling. (See above, bestie β€” you literally have to.)
  3. Next, your app allocates the PMCs it wants using pmc_allocate().
  4. You can set initial values for those PMCs using pmc_set().
  5. If a log file is necessary for the PMCs to work, configure it using pmc_configure_logfile().
  6. Process scope PMCs get attached to their target processes using pmc_attach().
  7. Then you start the PMCs using pmc_start().
  8. 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 the pmclog(3) family of functions.
  9. PMCs get stopped using pmc_stop(), and process scope PMCs get detached from their targets using pmc_detach().
  10. Before the process exits, it can release its PMCs using pmc_release(). And any configured log file can be closed using pmc_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:


⭐ 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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms