1 .\" Copyright (c) 2003-2008 Joseph Koshy. All rights reserved.
3 .\" Redistribution and use in source and binary forms, with or without
4 .\" modification, are permitted provided that the following conditions
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.
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
31 .Nd library for accessing hardware performance monitoring counters
39 provides a programming interface that allows applications to use
40 hardware performance counters to gather performance data about
41 specific processes or for the system as a whole.
42 The library is implemented using the lower-level facilities offered by
47 Performance monitoring counters (PMCs) are represented by the library
48 using a software abstraction.
51 PMCs can have two scopes:
55 These PMCs measure events in a whole-system manner, i.e., independent
56 of the currently executing thread.
57 System scope PMCs are allocated on specific CPUs and do not
59 Non-privileged process are allowed to allocate system scope PMCs if the
62 .Va security.bsd.unprivileged_syspmcs
66 These PMCs only measure hardware events when the processes they are
67 attached to are executing on a CPU.
68 In an SMP system, process scope PMCs migrate between CPUs along with
69 their target processes.
72 Orthogonal to PMC scope, PMCs may be allocated in one of two
76 Counting PMCs measure events according to their scope
78 The application needs to explicitly read these counters
79 to retrieve their value.
81 Sampling PMCs cause the CPU to be periodically interrupted
82 and information about its state of execution to be collected.
83 Sampling PMCs are used to profile specific processes and kernel
84 threads or to profile the system as a whole.
87 The scope and operational mode for a software PMC are specified at
89 An application is allowed to allocate multiple PMCs subject
90 to availability of hardware resources.
92 The library uses human-readable strings to name the event being
94 The syntax used for specifying a hardware event along with additional
95 event specific qualifiers (if any) is described in detail in section
96 .Sx "EVENT SPECIFIERS"
99 PMCs are associated with the process that allocated them and
100 will be automatically reclaimed by the system when the process exits.
101 Additionally, process-scope PMCs have to be attached to one or more
102 target processes before they can perform measurements.
103 A process-scope PMC may be attached to those target processes
104 that its owner process would otherwise be permitted to debug.
105 An owner process may attach PMCs to itself allowing
106 it to measure its own behavior.
107 Additionally, on some machine architectures, such self-attached PMCs
108 may be read cheaply using specialized instructions supported by the
111 Certain kinds of PMCs require that a log file be configured before
116 System scope sampling PMCs.
118 Process scope sampling PMCs.
120 Process scope counting PMCs that have been configured to report PMC
121 readings on process context switches or process exits.
124 Up to one log file may be configured per owner process.
125 Events logged to a log file may be subsequently analyzed using the
129 The CPUs known to the PMC library are named by the
130 .Vt "enum pmc_cputype"
132 Supported CPUs include:
134 .Bl -tag -width "Li PMC_CPU_ARMV7_CORTEX_A15" -compact
135 .It Li PMC_CPU_AMD_K7
138 .It Li PMC_CPU_AMD_K8
141 .It Li PMC_CPU_ARMV7_CORTEX_A5
145 .It Li PMC_CPU_ARMV7_CORTEX_A7
149 .It Li PMC_CPU_ARMV7_CORTEX_A8
153 .It Li PMC_CPU_ARMV7_CORTEX_A9
157 .It Li PMC_CPU_ARMV7_CORTEX_A15
160 .It Li PMC_CPU_ARMV7_CORTEX_A17
164 .It Li PMC_CPU_ARMV8_CORTEX_A53
168 .It Li PMC_CPU_ARMV8_CORTEX_A57
172 .It Li PMC_CPU_ARMV8_CORTEX_A76
178 .It Li PMC_CPU_INTEL_ATOM
181 CPUs and other CPUs conforming to version 3 of the
183 performance measurement architecture.
184 .It Li PMC_CPU_INTEL_CORE
189 CPUs, and other CPUs conforming to version 1 of the
191 performance measurement architecture.
192 .It Li PMC_CPU_INTEL_CORE2
198 CPUs, and other CPUs conforming to version 2 of the
200 performance measurement architecture.
201 .It Li PMC_CPU_PPC_7450
204 .It Li PMC_CPU_PPC_970
208 .It Li PMC_CPU_PPC_E500
211 .It Li PMC_CPU_PPC_POWER8
218 PMCs supported by this library are named by the
221 Supported PMC classes include:
223 .Bl -tag -width "Li PMC_CLASS_POWER8" -compact
225 Fixed function hardware counters presents in CPUs conforming to the
227 performance measurement architecture version 2 and later.
229 Programmable hardware counters present in CPUs conforming to the
231 performance measurement architecture version 1 and later.
233 Programmable hardware counters present in
237 Programmable hardware counters present in
241 The timestamp counter on i386 and amd64 architecture CPUs.
242 .It Li PMC_CLASS_ARMV7
244 .It Li PMC_CLASS_ARMV8
246 .It Li PMC_CLASS_PPC970
250 .It Li PMC_CLASS_POWER8
254 .It Li PMC_CLASS_SOFT
258 Capabilities of performance monitoring hardware are denoted using
262 Supported capabilities include:
264 .Bl -tag -width "Li PMC_CAP_INTERRUPT" -compact
265 .It Li PMC_CAP_CASCADE
266 The ability to cascade counters.
267 .It Li PMC_CAP_DOMWIDE
268 Separate counters tied to each NUMA domain.
270 The ability to count negated to asserted transitions of the hardware
271 conditions being probed for.
272 .It Li PMC_CAP_INTERRUPT
273 The ability to interrupt the CPU.
274 .It Li PMC_CAP_INVERT
275 The ability to invert the sense of the hardware conditions being
277 .It Li PMC_CAP_PRECISE
278 The ability to perform precise sampling.
279 .It Li PMC_CAP_QUALIFIER
280 The hardware allows monitored to be further qualified in some
281 system dependent way.
283 The ability to read from performance counters.
284 .It Li PMC_CAP_SYSTEM
285 The ability to restrict counting of hardware events to when the CPU is
286 running privileged code.
287 .It Li PMC_CAP_SYSWIDE
288 A single counter aggregating events for the whole system.
289 .It Li PMC_CAP_THRESHOLD
290 The ability to ignore simultaneous hardware events below a
291 programmable threshold.
293 The ability to restrict counting of hardware events to those when the
294 CPU is running unprivileged code.
296 The ability to write to performance counters.
298 .Ss CPU Naming Conventions
299 CPUs are named using small integers from zero up to, but
300 excluding, the value returned by function
302 On platforms supporting sparsely numbered CPUs not all the numbers in
303 this range will denote valid CPUs.
304 Operations on non-existent CPUs will return an error.
305 .Ss Functional Grouping of the API
306 This section contains a brief overview of the available functionality
308 Each function listed here is described further in its own manual page.
311 .Bl -tag -width 6n -compact
312 .It Fn pmc_disable , Fn pmc_enable
313 Administratively disable (enable) specific performance monitoring
315 Counters that are disabled will not be available to applications to
318 .It "Convenience Functions"
319 .Bl -tag -width 6n -compact
320 .It Fn pmc_event_names_of_class
321 Returns a list of event names supported by a given PMC type.
322 .It Fn pmc_name_of_capability
325 flag to a human-readable string.
326 .It Fn pmc_name_of_class
329 constant to a human-readable string.
330 .It Fn pmc_name_of_cputype
331 Return a human-readable name for a CPU type.
332 .It Fn pmc_name_of_disposition
333 Return a human-readable string describing a PMC's disposition.
334 .It Fn pmc_name_of_event
335 Convert a numeric event code to a human-readable string.
336 .It Fn pmc_name_of_mode
339 constant to a human-readable name.
340 .It Fn pmc_name_of_state
341 Return a human-readable string describing a PMC's current state.
343 .It "Library Initialization"
344 .Bl -tag -width 6n -compact
346 Initialize the library.
347 This function must be called before any other library function.
349 .It "Log File Handling"
350 .Bl -tag -width 6n -compact
351 .It Fn pmc_configure_logfile
352 Configure a log file for
354 to write logged events to.
355 .It Fn pmc_flush_logfile
356 Flush all pending log data in
359 .It Fn pmc_close_logfile
360 Flush all pending log data and close
364 Append arbitrary user data to the current log file.
367 .Bl -tag -width 6n -compact
368 .It Fn pmc_allocate , Fn pmc_release
369 Allocate (free) a PMC.
370 .It Fn pmc_attach , Fn pmc_detach
371 Attach (detach) a process scope PMC to a target.
372 .It Fn pmc_read , Fn pmc_write , Fn pmc_rw
373 Read (write) a value from (to) a PMC.
374 .It Fn pmc_start , Fn pmc_stop
375 Start (stop) a software PMC.
377 Set the reload value for a sampling PMC.
380 .Bl -tag -width 6n -compact
381 .It Fn pmc_capabilities
382 Retrieve the capabilities for a given PMC.
384 Retrieve information about the CPUs and PMC hardware present in the
386 .It Fn pmc_get_driver_stats
387 Retrieve statistics maintained by
390 Determine the greatest possible CPU number on the system.
392 Return the number of hardware PMCs present in a given CPU.
394 Return information about the state of a given CPU's PMCs.
396 Determine the width of a hardware counter in bits.
398 .It "x86 Architecture Specific API"
399 .Bl -tag -width 6n -compact
401 Returns the processor model specific register number
404 Applications may then use the x86
406 instruction to directly read the contents of the PMC.
409 .Ss Signal Handling Requirements
410 Applications using PMCs are required to handle the following signals:
411 .Bl -tag -width ".Dv SIGBUS"
415 module is unloaded using
417 processes that have PMCs allocated to them will be sent a
423 driver will send a PMC owning process a
428 any process-mode PMC allocated by it loses all its
431 the driver encounters an error when writing log data to a
433 This error may be retrieved by a subsequent call to
434 .Fn pmc_flush_logfile .
437 .Ss Typical Program Flow
440 An application would first invoke function
442 to allow the library to initialize itself.
444 Signal handling would then be set up.
446 Next the application would allocate the PMCs it desires using function
449 Initial values for PMCs may be set using function
452 If a log file is necessary for the PMCs to work, it would
453 be configured using function
454 .Fn pmc_configure_logfile .
456 Process scope PMCs would then be attached to their target processes
460 The PMCs would then be started using function
463 Once started, the values of counting PMCs may be read using function
465 For PMCs that write events to the log file, this logged data would be
466 read and parsed using the
470 PMCs are stopped using function
472 and process scope PMCs are detached from their targets using
476 Before the process exits, it may release its PMCs using function
478 Any configured log file may be closed using function
479 .Fn pmc_configure_logfile .
482 Event specifiers are strings comprising of an event name, followed by
483 optional parameters modifying the semantics of the hardware event
485 Event names are PMC architecture dependent, but the PMC library defines
486 machine independent aliases for commonly used events.
488 Event specifiers spellings are case-insensitive and space characters,
489 periods, underscores and hyphens are considered equivalent to each other.
490 Thus the event specifiers
491 .Qq "Example Event" ,
492 .Qq "example-event" ,
496 .Ss PMC Architecture Dependent Events
497 PMC architecture dependent event specifiers are described in the
498 following manual pages:
499 .Bl -column " PMC_CLASS_TSC " "MANUAL PAGE "
500 .It Em "PMC Class" Ta Em "Manual Page"
501 .It Li PMC_CLASS_IAF Ta Xr pmc.iaf 3
502 .It Li PMC_CLASS_IAP Ta Xr pmc.atom 3 , Xr pmc.core 3 , Xr pmc.core2 3
503 .It Li PMC_CLASS_K7 Ta Xr pmc.k7 3
504 .It Li PMC_CLASS_K8 Ta Xr pmc.k8 3
505 .It Li PMC_CLASS_TSC Ta Xr pmc.tsc 3
507 .Ss Event Name Aliases
508 Event name aliases are PMC-independent names for commonly used events.
509 The following aliases are known to this version of the
512 .Bl -tag -width indent
514 Measure the number of branches retired.
515 .It Li branch-mispredicts
516 Measure the number of retired branches that were mispredicted.
518 Measure processor cycles.
519 This event is implemented using the processor's Time Stamp Counter
522 Measure the number of data cache misses.
524 Measure the number of instruction cache misses.
526 Measure the number of instructions retired.
528 Measure the number of interrupts seen.
529 .It Li unhalted-cycles
530 Measure the number of cycles the processor is not in a halted
534 The interface between the
538 driver is intended to be private to the implementation and may
540 In order to ease forward compatibility with future versions of the
542 driver, applications are urged to dynamically link with the
555 .Xr pmc.haswelluc 3 ,
556 .Xr pmc.haswellxeon 3 ,
558 .Xr pmc.ivybridge 3 ,
559 .Xr pmc.ivybridgexeon 3 ,
562 .Xr pmc.sandybridge 3 ,
563 .Xr pmc.sandybridgeuc 3 ,
564 .Xr pmc.sandybridgexeon 3 ,
568 .Xr pmc.westmereuc 3 ,
571 .Xr pmc_capabilities 3 ,
572 .Xr pmc_configure_logfile 3 ,
574 .Xr pmc_event_names_of_class 3 ,
575 .Xr pmc_get_driver_stats 3 ,
578 .Xr pmc_name_of_capability 3 ,
589 library first appeared in
594 library was written by
595 .An Joseph Koshy Aq Mt jkoshy@FreeBSD.org .