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