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
29 .Nd library for accessing hardware performance monitoring counters
37 provides a programming interface that allows applications to use
38 hardware performance counters to gather performance data about
39 specific processes or for the system as a whole.
40 The library is implemented using the lower-level facilities offered by
45 Performance monitoring counters (PMCs) are represented by the library
46 using a software abstraction.
49 PMCs can have two scopes:
53 These PMCs measure events in a whole-system manner, i.e., independent
54 of the currently executing thread.
55 System scope PMCs are allocated on specific CPUs and do not
57 Non-privileged process are allowed to allocate system scope PMCs if the
60 .Va security.bsd.unprivileged_syspmcs
64 These PMCs only measure hardware events when the processes they are
65 attached to are executing on a CPU.
66 In an SMP system, process scope PMCs migrate between CPUs along with
67 their target processes.
70 Orthogonal to PMC scope, PMCs may be allocated in one of two
74 Counting PMCs measure events according to their scope
76 The application needs to explicitly read these counters
77 to retrieve their value.
79 Sampling PMCs cause the CPU to be periodically interrupted
80 and information about its state of execution to be collected.
81 Sampling PMCs are used to profile specific processes and kernel
82 threads or to profile the system as a whole.
85 The scope and operational mode for a software PMC are specified at
87 An application is allowed to allocate multiple PMCs subject
88 to availability of hardware resources.
90 The library uses human-readable strings to name the event being
92 The syntax used for specifying a hardware event along with additional
93 event specific qualifiers (if any) is described in detail in section
94 .Sx "EVENT SPECIFIERS"
97 PMCs are associated with the process that allocated them and
98 will be automatically reclaimed by the system when the process exits.
99 Additionally, process-scope PMCs have to be attached to one or more
100 target processes before they can perform measurements.
101 A process-scope PMC may be attached to those target processes
102 that its owner process would otherwise be permitted to debug.
103 An owner process may attach PMCs to itself allowing
104 it to measure its own behavior.
105 Additionally, on some machine architectures, such self-attached PMCs
106 may be read cheaply using specialized instructions supported by the
109 Certain kinds of PMCs require that a log file be configured before
114 System scope sampling PMCs.
116 Process scope sampling PMCs.
118 Process scope counting PMCs that have been configured to report PMC
119 readings on process context switches or process exits.
122 Up to one log file may be configured per owner process.
123 Events logged to a log file may be subsequently analyzed using the
127 The CPUs known to the PMC library are named by the
128 .Vt "enum pmc_cputype"
130 Supported CPUs include:
132 .Bl -tag -width "Li PMC_CPU_ARMV7_CORTEX_A15" -compact
133 .It Li PMC_CPU_AMD_K8
136 .It Li PMC_CPU_ARMV7_CORTEX_A5
140 .It Li PMC_CPU_ARMV7_CORTEX_A7
144 .It Li PMC_CPU_ARMV7_CORTEX_A8
148 .It Li PMC_CPU_ARMV7_CORTEX_A9
152 .It Li PMC_CPU_ARMV7_CORTEX_A15
155 .It Li PMC_CPU_ARMV7_CORTEX_A17
159 .It Li PMC_CPU_ARMV8_CORTEX_A53
163 .It Li PMC_CPU_ARMV8_CORTEX_A57
167 .It Li PMC_CPU_ARMV8_CORTEX_A76
173 .It Li PMC_CPU_INTEL_ATOM
176 CPUs and other CPUs conforming to version 3 of the
178 performance measurement architecture.
179 .It Li PMC_CPU_INTEL_CORE
184 CPUs, and other CPUs conforming to version 1 of the
186 performance measurement architecture.
187 .It Li PMC_CPU_INTEL_CORE2
193 CPUs, and other CPUs conforming to version 2 of the
195 performance measurement architecture.
196 .It Li PMC_CPU_PPC_7450
199 .It Li PMC_CPU_PPC_970
203 .It Li PMC_CPU_PPC_E500
206 .It Li PMC_CPU_PPC_POWER8
213 PMCs supported by this library are named by the
216 Supported PMC classes include:
218 .Bl -tag -width "Li PMC_CLASS_POWER8" -compact
220 Fixed function hardware counters presents in CPUs conforming to the
222 performance measurement architecture version 2 and later.
224 Programmable hardware counters present in CPUs conforming to the
226 performance measurement architecture version 1 and later.
228 Programmable hardware counters present in
232 The timestamp counter on i386 and amd64 architecture CPUs.
233 .It Li PMC_CLASS_ARMV7
235 .It Li PMC_CLASS_ARMV8
237 .It Li PMC_CLASS_PPC970
241 .It Li PMC_CLASS_POWER8
245 .It Li PMC_CLASS_SOFT
249 Capabilities of performance monitoring hardware are denoted using
253 Supported capabilities include:
255 .Bl -tag -width "Li PMC_CAP_INTERRUPT" -compact
256 .It Li PMC_CAP_CASCADE
257 The ability to cascade counters.
258 .It Li PMC_CAP_DOMWIDE
259 Separate counters tied to each NUMA domain.
261 The ability to count negated to asserted transitions of the hardware
262 conditions being probed for.
263 .It Li PMC_CAP_INTERRUPT
264 The ability to interrupt the CPU.
265 .It Li PMC_CAP_INVERT
266 The ability to invert the sense of the hardware conditions being
268 .It Li PMC_CAP_PRECISE
269 The ability to perform precise sampling.
270 .It Li PMC_CAP_QUALIFIER
271 The hardware allows monitored to be further qualified in some
272 system dependent way.
274 The ability to read from performance counters.
275 .It Li PMC_CAP_SYSTEM
276 The ability to restrict counting of hardware events to when the CPU is
277 running privileged code.
278 .It Li PMC_CAP_SYSWIDE
279 A single counter aggregating events for the whole system.
280 .It Li PMC_CAP_THRESHOLD
281 The ability to ignore simultaneous hardware events below a
282 programmable threshold.
284 The ability to restrict counting of hardware events to those when the
285 CPU is running unprivileged code.
287 The ability to write to performance counters.
289 .Ss CPU Naming Conventions
290 CPUs are named using small integers from zero up to, but
291 excluding, the value returned by function
293 On platforms supporting sparsely numbered CPUs not all the numbers in
294 this range will denote valid CPUs.
295 Operations on non-existent CPUs will return an error.
296 .Ss Functional Grouping of the API
297 This section contains a brief overview of the available functionality
299 Each function listed here is described further in its own manual page.
302 .Bl -tag -width 6n -compact
303 .It Fn pmc_disable , Fn pmc_enable
304 Administratively disable (enable) specific performance monitoring
306 Counters that are disabled will not be available to applications to
309 .It "Convenience Functions"
310 .Bl -tag -width 6n -compact
311 .It Fn pmc_event_names_of_class
312 Returns a list of event names supported by a given PMC type.
313 .It Fn pmc_name_of_capability
316 flag to a human-readable string.
317 .It Fn pmc_name_of_class
320 constant to a human-readable string.
321 .It Fn pmc_name_of_cputype
322 Return a human-readable name for a CPU type.
323 .It Fn pmc_name_of_disposition
324 Return a human-readable string describing a PMC's disposition.
325 .It Fn pmc_name_of_event
326 Convert a numeric event code to a human-readable string.
327 .It Fn pmc_name_of_mode
330 constant to a human-readable name.
331 .It Fn pmc_name_of_state
332 Return a human-readable string describing a PMC's current state.
334 .It "Library Initialization"
335 .Bl -tag -width 6n -compact
337 Initialize the library.
338 This function must be called before any other library function.
340 .It "Log File Handling"
341 .Bl -tag -width 6n -compact
342 .It Fn pmc_configure_logfile
343 Configure a log file for
345 to write logged events to.
346 .It Fn pmc_flush_logfile
347 Flush all pending log data in
350 .It Fn pmc_close_logfile
351 Flush all pending log data and close
355 Append arbitrary user data to the current log file.
358 .Bl -tag -width 6n -compact
359 .It Fn pmc_allocate , Fn pmc_release
360 Allocate (free) a PMC.
361 .It Fn pmc_attach , Fn pmc_detach
362 Attach (detach) a process scope PMC to a target.
363 .It Fn pmc_read , Fn pmc_write , Fn pmc_rw
364 Read (write) a value from (to) a PMC.
365 .It Fn pmc_start , Fn pmc_stop
366 Start (stop) a software PMC.
368 Set the reload value for a sampling PMC.
371 .Bl -tag -width 6n -compact
372 .It Fn pmc_capabilities
373 Retrieve the capabilities for a given PMC.
375 Retrieve information about the CPUs and PMC hardware present in the
377 .It Fn pmc_get_driver_stats
378 Retrieve statistics maintained by
381 Determine the greatest possible CPU number on the system.
383 Return the number of hardware PMCs present in a given CPU.
385 Return information about the state of a given CPU's PMCs.
387 Determine the width of a hardware counter in bits.
389 .It "x86 Architecture Specific API"
390 .Bl -tag -width 6n -compact
392 Returns the processor model specific register number
395 Applications may then use the x86
397 instruction to directly read the contents of the PMC.
400 .Ss Signal Handling Requirements
401 Applications using PMCs are required to handle the following signals:
402 .Bl -tag -width ".Dv SIGBUS"
406 module is unloaded using
408 processes that have PMCs allocated to them will be sent a
414 driver will send a PMC owning process a
419 any process-mode PMC allocated by it loses all its
422 the driver encounters an error when writing log data to a
424 This error may be retrieved by a subsequent call to
425 .Fn pmc_flush_logfile .
428 .Ss Typical Program Flow
431 An application would first invoke function
433 to allow the library to initialize itself.
435 Signal handling would then be set up.
437 Next the application would allocate the PMCs it desires using function
440 Initial values for PMCs may be set using function
443 If a log file is necessary for the PMCs to work, it would
444 be configured using function
445 .Fn pmc_configure_logfile .
447 Process scope PMCs would then be attached to their target processes
451 The PMCs would then be started using function
454 Once started, the values of counting PMCs may be read using function
456 For PMCs that write events to the log file, this logged data would be
457 read and parsed using the
461 PMCs are stopped using function
463 and process scope PMCs are detached from their targets using
467 Before the process exits, it may release its PMCs using function
469 Any configured log file may be closed using function
470 .Fn pmc_configure_logfile .
473 Event specifiers are strings comprising of an event name, followed by
474 optional parameters modifying the semantics of the hardware event
476 Event names are PMC architecture dependent, but the PMC library defines
477 machine independent aliases for commonly used events.
479 Event specifiers spellings are case-insensitive and space characters,
480 periods, underscores and hyphens are considered equivalent to each other.
481 Thus the event specifiers
482 .Qq "Example Event" ,
483 .Qq "example-event" ,
487 .Ss PMC Architecture Dependent Events
488 PMC architecture dependent event specifiers are described in the
489 following manual pages:
490 .Bl -column " PMC_CLASS_TSC " "MANUAL PAGE "
491 .It Em "PMC Class" Ta Em "Manual Page"
492 .It Li PMC_CLASS_IAF Ta Xr pmc.iaf 3
493 .It Li PMC_CLASS_IAP Ta Xr pmc.atom 3 , Xr pmc.core 3 , Xr pmc.core2 3
494 .It Li PMC_CLASS_K8 Ta Xr pmc.amd 3
495 .It Li PMC_CLASS_TSC Ta Xr pmc.tsc 3
497 .Ss Event Name Aliases
498 Event name aliases are PMC-independent names for commonly used events.
499 The following aliases are known to this version of the
502 .Bl -tag -width indent
504 Measure the number of branches retired.
505 .It Li branch-mispredicts
506 Measure the number of retired branches that were mispredicted.
508 Measure processor cycles.
509 This event is implemented using the processor's Time Stamp Counter
512 Measure the number of data cache misses.
514 Measure the number of instruction cache misses.
516 Measure the number of instructions retired.
518 Measure the number of interrupts seen.
519 .It Li unhalted-cycles
520 Measure the number of cycles the processor is not in a halted
524 The interface between the
528 driver is intended to be private to the implementation and may
530 In order to ease forward compatibility with future versions of the
532 driver, applications are urged to dynamically link with the
535 Doing otherwise is unsupported.
542 .Xr pmc.haswelluc 3 ,
543 .Xr pmc.haswellxeon 3 ,
545 .Xr pmc.ivybridge 3 ,
546 .Xr pmc.ivybridgexeon 3 ,
547 .Xr pmc.sandybridge 3 ,
548 .Xr pmc.sandybridgeuc 3 ,
549 .Xr pmc.sandybridgexeon 3 ,
553 .Xr pmc.westmereuc 3 ,
556 .Xr pmc_capabilities 3 ,
557 .Xr pmc_configure_logfile 3 ,
559 .Xr pmc_event_names_of_class 3 ,
560 .Xr pmc_get_driver_stats 3 ,
563 .Xr pmc_name_of_capability 3 ,
574 library first appeared in
579 library was written by
580 .An Joseph Koshy Aq Mt jkoshy@FreeBSD.org .