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