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 Joseph Koshy ``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 Joseph Koshy 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 one 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.
123 Upto one log file may be configured per owner process.
124 Events logged to a log file may be subsequently analyzed using the
128 The CPUs known to the PMC library are named by the
129 .Vt "enum pmc_cputype"
131 Supported CPUs include:
132 .Bl -tag -width "Li PMC_CPU_INTEL_CORE2" -compact
133 .It Li PMC_CPU_AMD_K7
136 .It Li PMC_CPU_AMD_K8
139 .It Li PMC_CPU_INTEL_ATOM
142 CPUs and other CPUs conforming to version 3 of the
144 performance measurement architecture.
145 .It Li PMC_CPU_INTEL_CORE
150 CPUs, and other CPUs conforming to version 1 of the
152 performance measurement architecture.
153 .It Li PMC_CPU_INTEL_CORE2
159 CPUs, and other CPUs conforming to version 2 of the
161 performance measurement architecture.
162 .It Li PMC_CPU_INTEL_P5
166 .It Li PMC_CPU_INTEL_P6
170 .It Li PMC_CPU_INTEL_PII
171 .Tn "Intel Pentium II"
173 .It Li PMC_CPU_INTEL_PIII
174 .Tn "Intel Pentium III"
176 .It Li PMC_CPU_INTEL_PIV
177 .Tn "Intel Pentium 4"
179 .It Li PMC_CPU_INTEL_PM
180 .Tn "Intel Pentium M"
184 PMC supported by this library are named by the
187 Supported PMC kinds include:
188 .Bl -tag -width "Li PMC_CLASS_IAF" -compact
190 Fixed function hardwre counters presents in CPUs conforming to the
192 performance measurement architecture version 2 and later.
194 Programmable hardware counters present in CPUs conforming to the
196 performance measurement architecture version 1 and later.
198 Programmable hardware counters present in
202 Programmable hardware counters present in
206 Programmable hardware counters present in
207 .Tn "Intel Pentium 4"
210 Programmable hardware counters present in
215 Programmable hardware counters present in
225 The timestamp counter on i386 and amd64 architecture CPUs.
229 Capabilities of performance monitoring hardware are denoted using
233 Supported capabilities include:
234 .Bl -tag -width "Li PMC_CAP_INTERRUPT" -compact
235 .It Li PMC_CAP_CASCADE
236 The ability to cascade counters.
238 The ability to count negated to asserted transitions of the hardware
239 conditions being probed for.
240 .It Li PMC_CAP_INTERRUPT
241 The ability to interrupt the CPU.
242 .It Li PMC_CAP_INVERT
243 The ability to invert the sense of the hardware conditions being
245 .It Li PMC_CAP_PRECISE
246 The ability to perform precise sampling.
247 .It Li PMC_CAP_QUALIFIER
248 The hardware allows monitored to be further qualified in some
249 system dependent way.
251 The ability to read from performance counters.
252 .It Li PMC_CAP_SYSTEM
253 The ability to restrict counting of hardware events to when the CPU is
254 running privileged code.
255 .It Li PMC_CAP_THRESHOLD
256 The ability to ignore simultaneous hardware events below a
257 programmable threshold.
259 The ability to restrict counting of hardware events to those when the
260 CPU is running unprivileged code.
262 The ability to write to performance counters.
264 .Ss CPU Naming Conventions
265 CPUs are named using small integers from zero uptil, but
266 excluding, the value returned by function
268 On platforms supporting sparsely numbered CPUs not all the numbers in
269 this range will denote valid CPUs.
270 Operations on non-existent CPUs will return an error.
271 .Ss Functional Grouping of the API
272 This section contains a brief overview of the available functionality
274 Each function listed here is described further in its own manual page.
275 .Bl -tag -width indent
278 .It Fn pmc_disable , Fn pmc_enable
279 Administratively disable (enable) specific performance monitoring
281 Counters that are disabled will not be available to applications to
284 .It "Convenience Functions"
286 .It Fn pmc_event_names_of_class
287 Returns a list of event names supported by a given PMC type.
288 .It Fn pmc_name_of_capability
291 flag to a human-readable string.
292 .It Fn pmc_name_of_class
295 constant to a human-readable string.
296 .It Fn pmc_name_of_cputype
297 Return a human-readable name for a CPU type.
298 .It Fn pmc_name_of_disposition
299 Return a human-readable string describing a PMC's disposition.
300 .It Fn pmc_name_of_event
301 Convert a numeric event code to a human-readable string.
302 .It Fn pmc_name_of_mode
305 constant to a human-readable name.
306 .It Fn pmc_name_of_state
307 Return a human-readable string describing a PMC's current state.
309 .It "Library Initialization"
312 Initialize the library.
313 This function must be called before any other library function.
315 .It "Log File Handling"
317 .It Fn pmc_configure_logfile
318 Configure a log file for
320 to write logged events to.
321 .It Fn pmc_flush_logfile
322 Flush all pending log data in
326 Append arbitrary user data to the current log file.
330 .It Fn pmc_allocate , Fn pmc_release
331 Allocate (free) a PMC.
332 .It Fn pmc_attach , Fn pmc_detach
333 Attach (detach) a process scope PMC to a target.
334 .It Fn pmc_read , Fn pmc_write , Fn pmc_rw
335 Read (write) a value from (to) a PMC.
336 .It Fn pmc_start , Fn pmc_stop
337 Start (stop) a software PMC.
339 Set the reload value for a sampling PMC.
343 .It Fn pmc_capabilities
344 Retrieve the capabilities for a given PMC.
346 Retrieve information about the CPUs and PMC hardware present in the
348 .It Fn pmc_get_driver_stats
349 Retrieve statistics maintained by
352 Determine the greatest possible CPU number on the system.
354 Return the number of hardware PMCs present in a given CPU.
356 Return information about the state of a given CPU's PMCs.
358 Determine the width of a hardware counter in bits.
360 .It "x86 Architecture Specific API"
363 Returns the processor model specific register number
366 Applications may then use the x86
368 instruction to directly read the contents of the PMC.
371 .Ss Signal Handling Requirements
372 Applications using PMCs are required to handle the following signals:
373 .Bl -tag -width ".Dv SIGBUS"
377 module is unloaded using
379 processes that have PMCs allocated to them will be sent a
385 driver will send a PMC owning process a
390 If any process-mode PMC allocated by it loses all its
393 If the driver encounters an error when writing log data to a
395 This error may be retrieved by a subsequent call to
396 .Fn pmc_flush_logfile .
399 .Ss Typical Program Flow
402 An application would first invoke function
404 to allow the library to initialize itself.
406 Signal handling would then be set up.
408 Next the application would allocate the PMCs it desires using function
411 Initial values for PMCs may be set using function
414 If a log file is necessary for the PMCs to work, it would
415 be configured using function
416 .Fn pmc_configure_logfile .
418 Process scope PMCs would then be attached to their target processes
422 The PMCs would then be started using function
425 Once started, the values of counting PMCs may be read using function
427 For PMCs that write events to the log file, this logged data would be
428 read and parsed using the
432 PMCs are stopped using function
434 and process scope PMCs are detached from their targets using
438 Before the process exits, its may release its PMCs using function
440 Any configured log file may be closed using function
441 .Fn pmc_configure_logfile .
444 Event specifiers are strings comprising of an event name, followed by
445 optional parameters modifying the semantics of the hardware event
447 Event names are PMC architecture dependent, but the PMC library defines
448 machine independent aliases for commonly used events.
450 Event specifiers spellings are case-insensitive and space characters,
451 periods, underscores and hyphens are considered equivalent to each other.
452 Thus the event specifiers
453 .Qq "Example Event" ,
454 .Qq "example-event" ,
458 .Ss PMC Architecture Dependent Events
459 PMC architecture dependent event specifiers are described in the
460 following manual pages:
461 .Bl -column " PMC_CLASS_TSC " "MANUAL PAGE "
462 .It Em "PMC Class" Ta Em "Manual Page"
463 .It Li PMC_CLASS_IAF Ta Xr pmc.iaf 3
464 .It Li PMC_CLASS_IAP Ta Xr pmc.atom 3 , Xr pmc.core 3 , Xr pmc.core2 3
465 .It Li PMC_CLASS_K7 Ta Xr pmc.k7 3
466 .It Li PMC_CLASS_K8 Ta Xr pmc.k8 3
467 .It Li PMC_CLASS_P4 Ta Xr pmc.p4 3
468 .It Li PMC_CLASS_P5 Ta Xr pmc.p5 3
469 .It Li PMC_CLASS_P6 Ta Xr pmc.p6 3
470 .It Li PMC_CLASS_TSC Ta Xr pmc.tsc 3
472 .Ss Event Name Aliases
473 Event name aliases are PMC-independent names for commonly used events.
474 The following aliases are known to this version of the
477 .Bl -tag -width indent
479 Measure the number of branches retired.
480 .It Li branch-mispredicts
481 Measure the number of retired branches that were mispredicted.
483 Measure processor cycles.
484 This event is implemented using the processor's Time Stamp Counter
487 Measure the number of data cache misses.
489 Measure the number of instruction cache misses.
491 Measure the number of instructions retired.
493 Measure the number of interrupts seen.
494 .It Li unhalted-cycles
495 Measure the number of cycles the processor is not in a halted
499 The interface between the
503 driver is intended to be private to the implementation and may
505 In order to ease forward compatibility with future versions of the
507 driver, applications are urged to dynamically link with the
533 library first appeared in
538 library was written by
540 .Aq jkoshy@FreeBSD.org .