1.\" Copyright (c) 2003-2008 Joseph Koshy. All rights reserved. 2.\" 3.\" Redistribution and use in source and binary forms, with or without 4.\" modification, are permitted provided that the following conditions 5.\" are met: 6.\" 1. Redistributions of source code must retain the above copyright 7.\" notice, this list of conditions and the following disclaimer. 8.\" 2. Redistributions in binary form must reproduce the above copyright 9.\" notice, this list of conditions and the following disclaimer in the 10.\" documentation and/or other materials provided with the distribution. 11.\" 12.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 13.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 16.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 19.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 22.\" SUCH DAMAGE. 23.\" 24.Dd June 23, 2023 25.Dt PMC 3 26.Os 27.Sh NAME 28.Nm pmc 29.Nd library for accessing hardware performance monitoring counters 30.Sh LIBRARY 31.Lb libpmc 32.Sh SYNOPSIS 33.In pmc.h 34.Sh DESCRIPTION 35The 36.Lb libpmc 37provides a programming interface that allows applications to use 38hardware performance counters to gather performance data about 39specific processes or for the system as a whole. 40The library is implemented using the lower-level facilities offered by 41the 42.Xr hwpmc 4 43driver. 44.Ss Key Concepts 45Performance monitoring counters (PMCs) are represented by the library 46using a software abstraction. 47These 48.Dq abstract 49PMCs can have two scopes: 50.Bl -bullet 51.It 52System scope. 53These PMCs measure events in a whole-system manner, i.e., independent 54of the currently executing thread. 55System scope PMCs are allocated on specific CPUs and do not 56migrate between CPUs. 57Non-privileged process are allowed to allocate system scope PMCs if the 58.Xr hwpmc 4 59sysctl tunable: 60.Va security.bsd.unprivileged_syspmcs 61is non-zero. 62.It 63Process scope. 64These PMCs only measure hardware events when the processes they are 65attached to are executing on a CPU. 66In an SMP system, process scope PMCs migrate between CPUs along with 67their target processes. 68.El 69.Pp 70Orthogonal to PMC scope, PMCs may be allocated in one of two 71operational modes: 72.Bl -bullet 73.It 74Counting PMCs measure events according to their scope 75(system or process). 76The application needs to explicitly read these counters 77to retrieve their value. 78.It 79Sampling PMCs cause the CPU to be periodically interrupted 80and information about its state of execution to be collected. 81Sampling PMCs are used to profile specific processes and kernel 82threads or to profile the system as a whole. 83.El 84.Pp 85The scope and operational mode for a software PMC are specified at 86PMC allocation time. 87An application is allowed to allocate multiple PMCs subject 88to availability of hardware resources. 89.Pp 90The library uses human-readable strings to name the event being 91measured by hardware. 92The syntax used for specifying a hardware event along with additional 93event specific qualifiers (if any) is described in detail in section 94.Sx "EVENT SPECIFIERS" 95below. 96.Pp 97PMCs are associated with the process that allocated them and 98will be automatically reclaimed by the system when the process exits. 99Additionally, process-scope PMCs have to be attached to one or more 100target processes before they can perform measurements. 101A process-scope PMC may be attached to those target processes 102that its owner process would otherwise be permitted to debug. 103An owner process may attach PMCs to itself allowing 104it to measure its own behavior. 105Additionally, on some machine architectures, such self-attached PMCs 106may be read cheaply using specialized instructions supported by the 107processor. 108.Pp 109Certain kinds of PMCs require that a log file be configured before 110they may be started. 111These include: 112.Bl -bullet 113.It 114System scope sampling PMCs. 115.It 116Process scope sampling PMCs. 117.It 118Process scope counting PMCs that have been configured to report PMC 119readings on process context switches or process exits. 120.El 121.Pp 122Up to one log file may be configured per owner process. 123Events logged to a log file may be subsequently analyzed using the 124.Xr pmclog 3 125family of functions. 126.Ss Supported CPUs 127The CPUs known to the PMC library are named by the 128.Vt "enum pmc_cputype" 129enumeration. 130Supported CPUs include: 131.Pp 132.Bl -tag -width "Li PMC_CPU_ARMV7_CORTEX_A15" -compact 133.It Li PMC_CPU_AMD_K8 134.Tn "AMD Athlon64" 135CPUs. 136.It Li PMC_CPU_ARMV7_CORTEX_A5 137.Tn ARMv7 138.Tn Cortex A5 139CPUs. 140.It Li PMC_CPU_ARMV7_CORTEX_A7 141.Tn ARMv7 142.Tn Cortex A7 143CPUs. 144.It Li PMC_CPU_ARMV7_CORTEX_A8 145.Tn ARMv7 146.Tn Cortex A8 147CPUs. 148.It Li PMC_CPU_ARMV7_CORTEX_A9 149.Tn ARMv7 150.Tn Cortex A9 151CPUs. 152.It Li PMC_CPU_ARMV7_CORTEX_A15 153.Tn ARMv7 Cortex A15 154CPUs. 155.It Li PMC_CPU_ARMV7_CORTEX_A17 156.Tn ARMv7 157.Tn Cortex A17 158CPUs. 159.It Li PMC_CPU_ARMV8_CORTEX_A53 160ARMv8 161.Tn Cortex A53 162CPUs. 163.It Li PMC_CPU_ARMV8_CORTEX_A57 164ARMv8 165.Tn Cortex A57 166CPUs. 167.It Li PMC_CPU_ARMV8_CORTEX_A76 168ARMv8 169.Tn Cortex A76 170CPUs. 171.It Li GENERIC 172Generic 173.It Li PMC_CPU_INTEL_ATOM 174.Tn Intel 175.Tn Atom 176CPUs and other CPUs conforming to version 3 of the 177.Tn Intel 178performance measurement architecture. 179.It Li PMC_CPU_INTEL_CORE 180.Tn Intel 181.Tn Core Solo 182and 183.Tn Core Duo 184CPUs, and other CPUs conforming to version 1 of the 185.Tn Intel 186performance measurement architecture. 187.It Li PMC_CPU_INTEL_CORE2 188.Tn Intel 189.Tn "Core2 Solo" , 190.Tn "Core2 Duo" 191and 192.Tn "Core2 Extreme" 193CPUs, and other CPUs conforming to version 2 of the 194.Tn Intel 195performance measurement architecture. 196.It Li PMC_CPU_PPC_7450 197.Tn PowerPC 198MPC7450 CPUs. 199.It Li PMC_CPU_PPC_970 200.Tn IBM 201.Tn PowerPC 202970 CPUs. 203.It Li PMC_CPU_PPC_E500 204.Tn PowerPC 205e500 Core CPUs. 206.It Li PMC_CPU_PPC_POWER8 207.Tn IBM 208.Tn POWER8 and 209.Tn POWER9 210CPUs. 211.El 212.Ss Supported PMCs 213PMCs supported by this library are named by the 214.Vt enum pmc_class 215enumeration. 216Supported PMC classes include: 217.Pp 218.Bl -tag -width "Li PMC_CLASS_POWER8" -compact 219.It Li PMC_CLASS_IAF 220Fixed function hardware counters presents in CPUs conforming to the 221.Tn Intel 222performance measurement architecture version 2 and later. 223.It Li PMC_CLASS_IAP 224Programmable hardware counters present in CPUs conforming to the 225.Tn Intel 226performance measurement architecture version 1 and later. 227.It Li PMC_CLASS_IBS 228.Tn AMD 229Instruction Based Sampling (IBS) counters present in 230.Tn AMD 231Family 10h and above. 232.It Li PMC_CLASS_K8 233Programmable hardware counters present in 234.Tn "AMD Athlon64" 235CPUs. 236.It Li PMC_CLASS_PERF 237MPERF and APERF counters present in 238.Tn AMD 239and 240.Tn Intel 241CPUs. 242.It Li PMC_CLASS_RAPL 243RAPL energy counters present in 244.Tn AMD 245and 246.Tn Intel 247CPUs. 248.It Li PMC_CLASS_TSC 249The timestamp counter on i386 and amd64 architecture CPUs. 250.It Li PMC_CLASS_ARMV7 251.Tn ARMv7 252.It Li PMC_CLASS_ARMV8 253.Tn ARMv8 254.It Li PMC_CLASS_PPC970 255.Tn IBM 256.Tn PowerPC 257970 class. 258.It Li PMC_CLASS_POWER8 259.Tn IBM 260.Tn POWER8 261class. 262.It Li PMC_CLASS_SOFT 263Software events. 264.El 265.Ss PMC Capabilities 266Capabilities of performance monitoring hardware are denoted using 267the 268.Vt "enum pmc_caps" 269enumeration. 270Supported capabilities include: 271.Pp 272.Bl -tag -width "Li PMC_CAP_INTERRUPT" -compact 273.It Li PMC_CAP_CASCADE 274The ability to cascade counters. 275.It Li PMC_CAP_DOMWIDE 276Separate counters tied to each NUMA domain. 277.It Li PMC_CAP_EDGE 278The ability to count negated to asserted transitions of the hardware 279conditions being probed for. 280.It Li PMC_CAP_INTERRUPT 281The ability to interrupt the CPU. 282.It Li PMC_CAP_INVERT 283The ability to invert the sense of the hardware conditions being 284measured. 285.It Li PMC_CAP_PRECISE 286The ability to perform precise sampling. 287.It Li PMC_CAP_QUALIFIER 288The hardware allows monitored to be further qualified in some 289system dependent way. 290.It Li PMC_CAP_READ 291The ability to read from performance counters. 292.It Li PMC_CAP_SYSTEM 293The ability to restrict counting of hardware events to when the CPU is 294running privileged code. 295.It Li PMC_CAP_SYSWIDE 296A single counter aggregating events for the whole system. 297.It Li PMC_CAP_THRESHOLD 298The ability to ignore simultaneous hardware events below a 299programmable threshold. 300.It Li PMC_CAP_USER 301The ability to restrict counting of hardware events to those when the 302CPU is running unprivileged code. 303.It Li PMC_CAP_WRITE 304The ability to write to performance counters. 305.El 306.Ss CPU Naming Conventions 307CPUs are named using small integers from zero up to, but 308excluding, the value returned by function 309.Fn pmc_ncpu . 310On platforms supporting sparsely numbered CPUs not all the numbers in 311this range will denote valid CPUs. 312Operations on non-existent CPUs will return an error. 313.Ss Functional Grouping of the API 314This section contains a brief overview of the available functionality 315in the PMC library. 316Each function listed here is described further in its own manual page. 317.Bl -tag -width 2n 318.It Administration 319.Bl -tag -width 6n -compact 320.It Fn pmc_disable , Fn pmc_enable 321Administratively disable (enable) specific performance monitoring 322counter hardware. 323Counters that are disabled will not be available to applications to 324use. 325.El 326.It "Convenience Functions" 327.Bl -tag -width 6n -compact 328.It Fn pmc_event_names_of_class 329Returns a list of event names supported by a given PMC type. 330.It Fn pmc_name_of_capability 331Convert a 332.Dv PMC_CAP_* 333flag to a human-readable string. 334.It Fn pmc_name_of_class 335Convert a 336.Dv PMC_CLASS_* 337constant to a human-readable string. 338.It Fn pmc_name_of_cputype 339Return a human-readable name for a CPU type. 340.It Fn pmc_name_of_disposition 341Return a human-readable string describing a PMC's disposition. 342.It Fn pmc_name_of_event 343Convert a numeric event code to a human-readable string. 344.It Fn pmc_name_of_mode 345Convert a 346.Dv PMC_MODE_* 347constant to a human-readable name. 348.It Fn pmc_name_of_state 349Return a human-readable string describing a PMC's current state. 350.El 351.It "Library Initialization" 352.Bl -tag -width 6n -compact 353.It Fn pmc_init 354Initialize the library. 355This function must be called before any other library function. 356.El 357.It "Log File Handling" 358.Bl -tag -width 6n -compact 359.It Fn pmc_configure_logfile 360Configure a log file for 361.Xr hwpmc 4 362to write logged events to. 363.It Fn pmc_flush_logfile 364Flush all pending log data in 365.Xr hwpmc 4 Ns Ap s 366buffers. 367.It Fn pmc_close_logfile 368Flush all pending log data and close 369.Xr hwpmc 4 Ns Ap s 370side of the stream. 371.It Fn pmc_writelog 372Append arbitrary user data to the current log file. 373.El 374.It "PMC Management" 375.Bl -tag -width 6n -compact 376.It Fn pmc_allocate , Fn pmc_release 377Allocate (free) a PMC. 378.It Fn pmc_attach , Fn pmc_detach 379Attach (detach) a process scope PMC to a target. 380.It Fn pmc_read , Fn pmc_write , Fn pmc_rw 381Read (write) a value from (to) a PMC. 382.It Fn pmc_start , Fn pmc_stop 383Start (stop) a software PMC. 384.It Fn pmc_set 385Set the reload value for a sampling PMC. 386.El 387.It "Queries" 388.Bl -tag -width 6n -compact 389.It Fn pmc_capabilities 390Retrieve the capabilities for a given PMC. 391.It Fn pmc_cpuinfo 392Retrieve information about the CPUs and PMC hardware present in the 393system. 394.It Fn pmc_get_driver_stats 395Retrieve statistics maintained by 396.Xr hwpmc 4 . 397.It Fn pmc_ncpu 398Determine the greatest possible CPU number on the system. 399.It Fn pmc_npmc 400Return the number of hardware PMCs present in a given CPU. 401.It Fn pmc_pmcinfo 402Return information about the state of a given CPU's PMCs. 403.It Fn pmc_width 404Determine the width of a hardware counter in bits. 405.El 406.It "x86 Architecture Specific API" 407.Bl -tag -width 6n -compact 408.It Fn pmc_get_msr 409Returns the processor model specific register number 410associated with 411.Fa pmc . 412Applications may then use the x86 413.Ic RDPMC 414instruction to directly read the contents of the PMC. 415.El 416.El 417.Ss Signal Handling Requirements 418Applications using PMCs are required to handle the following signals: 419.Bl -tag -width ".Dv SIGBUS" 420.It Dv SIGBUS 421When the 422.Xr hwpmc 4 423module is unloaded using 424.Xr kldunload 8 , 425processes that have PMCs allocated to them will be sent a 426.Dv SIGBUS 427signal. 428.It Dv SIGIO 429The 430.Xr hwpmc 4 431driver will send a PMC owning process a 432.Dv SIGIO 433signal if: 434.Bl -bullet 435.It 436any process-mode PMC allocated by it loses all its 437target processes. 438.It 439the driver encounters an error when writing log data to a 440configured log file. 441This error may be retrieved by a subsequent call to 442.Fn pmc_flush_logfile . 443.El 444.El 445.Ss Typical Program Flow 446.Bl -enum 447.It 448An application would first invoke function 449.Fn pmc_init 450to allow the library to initialize itself. 451.It 452Signal handling would then be set up. 453.It 454Next the application would allocate the PMCs it desires using function 455.Fn pmc_allocate . 456.It 457Initial values for PMCs may be set using function 458.Fn pmc_set . 459.It 460If a log file is necessary for the PMCs to work, it would 461be configured using function 462.Fn pmc_configure_logfile . 463.It 464Process scope PMCs would then be attached to their target processes 465using function 466.Fn pmc_attach . 467.It 468The PMCs would then be started using function 469.Fn pmc_start . 470.It 471Once started, the values of counting PMCs may be read using function 472.Fn pmc_read . 473For PMCs that write events to the log file, this logged data would be 474read and parsed using the 475.Xr pmclog 3 476family of functions. 477.It 478PMCs are stopped using function 479.Fn pmc_stop , 480and process scope PMCs are detached from their targets using 481function 482.Fn pmc_detach . 483.It 484Before the process exits, it may release its PMCs using function 485.Fn pmc_release . 486Any configured log file may be closed using function 487.Fn pmc_configure_logfile . 488.El 489.Sh EVENT SPECIFIERS 490Event specifiers are strings comprising of an event name, followed by 491optional parameters modifying the semantics of the hardware event 492being probed. 493Event names are PMC architecture dependent, but the PMC library defines 494machine independent aliases for commonly used events. 495.Pp 496Event specifiers spellings are case-insensitive and space characters, 497periods, underscores and hyphens are considered equivalent to each other. 498Thus the event specifiers 499.Qq "Example Event" , 500.Qq "example-event" , 501and 502.Qq "EXAMPLE_EVENT" 503are equivalent. 504.Ss PMC Architecture Dependent Events 505PMC architecture dependent event specifiers are described in the 506following manual pages: 507.Bl -column " PMC_CLASS_TSC " "MANUAL PAGE " 508.It Em "PMC Class" Ta Em "Manual Page" 509.It Li PMC_CLASS_IAF Ta Xr pmc.iaf 3 510.It Li PMC_CLASS_IAP Ta Xr pmc.atom 3 , Xr pmc.core 3 , Xr pmc.core2 3 511.It Li PMC_CLASS_IBS Ta Xr pmc.ibs 3 512.It Li PMC_CLASS_K8 Ta Xr pmc.amd 3 513.It Li PMC_CLASS_RAPL Ta Xr pmc.rapl 3 514.It Li PMC_CLASS_PERF Ta Xr pmc.perf 3 515.It Li PMC_CLASS_TSC Ta Xr pmc.tsc 3 516.El 517.Ss Event Name Aliases 518Event name aliases are PMC-independent names for commonly used events. 519The following aliases are known to this version of the 520.Nm pmc 521library: 522.Bl -tag -width indent 523.It Li branches 524Measure the number of branches retired. 525.It Li branch-mispredicts 526Measure the number of retired branches that were mispredicted. 527.It Li cycles 528Measure processor cycles. 529This event is implemented using the processor's Time Stamp Counter 530register. 531.It Li dc-misses 532Measure the number of data cache misses. 533.It Li ic-misses 534Measure the number of instruction cache misses. 535.It Li instructions 536Measure the number of instructions retired. 537.It Li interrupts 538Measure the number of interrupts seen. 539.It Li unhalted-cycles 540Measure the number of cycles the processor is not in a halted 541or sleep state. 542.El 543.Sh COMPATIBILITY 544The interface between the 545.Nm pmc 546library and the 547.Xr hwpmc 4 548driver is intended to be private to the implementation and may 549change. 550In order to ease forward compatibility with future versions of the 551.Xr hwpmc 4 552driver, applications are urged to dynamically link with the 553.Nm pmc 554library. 555Doing otherwise is unsupported. 556.Sh SEE ALSO 557.Xr pmc.amd 3 , 558.Xr pmc.atom 3 , 559.Xr pmc.core 3 , 560.Xr pmc.core2 3 , 561.Xr pmc.haswell 3 , 562.Xr pmc.haswelluc 3 , 563.Xr pmc.haswellxeon 3 , 564.Xr pmc.iaf 3 , 565.Xr pmc.ibs 3 , 566.Xr pmc.ivybridge 3 , 567.Xr pmc.ivybridgexeon 3 , 568.Xr pmc.perf 3 , 569.Xr pmc.rapl 3 , 570.Xr pmc.sandybridge 3 , 571.Xr pmc.sandybridgeuc 3 , 572.Xr pmc.sandybridgexeon 3 , 573.Xr pmc.soft 3 , 574.Xr pmc.tsc 3 , 575.Xr pmc.westmere 3 , 576.Xr pmc.westmereuc 3 , 577.Xr pmc_allocate 3 , 578.Xr pmc_attach 3 , 579.Xr pmc_capabilities 3 , 580.Xr pmc_configure_logfile 3 , 581.Xr pmc_disable 3 , 582.Xr pmc_event_names_of_class 3 , 583.Xr pmc_get_driver_stats 3 , 584.Xr pmc_get_msr 3 , 585.Xr pmc_init 3 , 586.Xr pmc_name_of_capability 3 , 587.Xr pmc_read 3 , 588.Xr pmc_set 3 , 589.Xr pmc_start 3 , 590.Xr pmclog 3 , 591.Xr hwpmc 4 , 592.Xr pmccontrol 8 , 593.Xr pmcstat 8 594.Sh HISTORY 595The 596.Nm pmc 597library first appeared in 598.Fx 6.0 . 599.Sh AUTHORS 600The 601.Lb libpmc 602library was written by 603.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org . 604