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