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