xref: /freebsd/usr.sbin/pmcstat/pmcstat.8 (revision 2ed3236082a4473c1da8f72c1ebc071a7b54321f)
1.\" Copyright (c) 2003-2008 Joseph Koshy
2.\" Copyright (c) 2007 The FreeBSD Foundation
3.\" All rights reserved.
4.\"
5.\" Redistribution and use in source and binary forms, with or without
6.\" modification, are permitted provided that the following conditions
7.\" are met:
8.\" 1. Redistributions of source code must retain the above copyright
9.\"    notice, this list of conditions and the following disclaimer.
10.\" 2. Redistributions in binary form must reproduce the above copyright
11.\"    notice, this list of conditions and the following disclaimer in the
12.\"    documentation and/or other materials provided with the distribution.
13.\"
14.\" This software is provided by Joseph Koshy ``as is'' and
15.\" any express or implied warranties, including, but not limited to, the
16.\" implied warranties of merchantability and fitness for a particular purpose
17.\" are disclaimed.  in no event shall Joseph Koshy be liable
18.\" for any direct, indirect, incidental, special, exemplary, or consequential
19.\" damages (including, but not limited to, procurement of substitute goods
20.\" or services; loss of use, data, or profits; or business interruption)
21.\" however caused and on any theory of liability, whether in contract, strict
22.\" liability, or tort (including negligence or otherwise) arising in any way
23.\" out of the use of this software, even if advised of the possibility of
24.\" such damage.
25.\"
26.\" $FreeBSD$
27.\"
28.Dd August 17, 2020
29.Dt PMCSTAT 8
30.Os
31.Sh NAME
32.Nm pmcstat
33.Nd "performance measurement with performance monitoring hardware"
34.Sh SYNOPSIS
35.Nm
36.Op Fl A
37.Op Fl C
38.Op Fl D Ar pathname
39.Op Fl E
40.Op Fl F Ar pathname
41.Op Fl G Ar pathname
42.Op Fl I
43.Op Fl L
44.Op Fl M Ar mapfilename
45.Op Fl N
46.Op Fl O Ar logfilename
47.Op Fl P Ar event-spec
48.Op Fl R Ar logfilename
49.Op Fl S Ar event-spec
50.Op Fl T
51.Op Fl U
52.Op Fl W
53.Op Fl a Ar pathname
54.Op Fl c Ar cpu-spec
55.Op Fl d
56.Op Fl e
57.Op Fl f Ar pluginopt
58.Op Fl g
59.Op Fl i Ar lwp
60.Op Fl k Ar kerneldir
61.Op Fl l Ar secs
62.Op Fl m Ar pathname
63.Op Fl n Ar rate
64.Op Fl o Ar outputfile
65.Op Fl p Ar event-spec
66.Op Fl q
67.Op Fl r Ar fsroot
68.Op Fl s Ar event-spec
69.Op Fl t Ar process-spec
70.Op Fl u Ar event-spec
71.Op Fl v
72.Op Fl w Ar secs
73.Op Fl z Ar graphdepth
74.Op Ar command Op Ar args
75.Sh DESCRIPTION
76The
77.Nm
78utility measures system performance using the facilities provided by
79.Xr hwpmc 4 .
80.Pp
81The
82.Nm
83utility can measure both hardware events seen by the system as a
84whole, and those seen when a specified set of processes are executing
85on the system's CPUs.
86If a specific set of processes is being targeted (for example,
87if the
88.Fl t Ar process-spec
89option is specified, or if a command line is specified using
90.Ar command ) ,
91then measurement occurs till
92.Ar command
93exits, or till all target processes specified by the
94.Fl t Ar process-spec
95options exit, or till the
96.Nm
97utility is interrupted by the user.
98If a specific set of processes is not targeted for measurement, then
99.Nm
100will perform system-wide measurements till interrupted by the
101user.
102.Pp
103A given invocation of
104.Nm
105can mix allocations of system-mode and process-mode PMCs, of both
106counting and sampling flavors.
107The values of all counting PMCs are printed in human readable form
108at regular intervals by
109.Nm .
110The output of sampling PMCs may be configured to go to a log file for
111subsequent offline analysis, or, at the expense of greater
112overhead, may be configured to be printed in text form on the fly.
113.Pp
114Hardware events to measure are specified to
115.Nm
116using event specifier strings
117.Ar event-spec .
118The syntax of these event specifiers is machine dependent and is
119documented in
120.Xr pmc 3 .
121.Pp
122A process-mode PMC may be configured to be inheritable by the target
123process' current and future children.
124.Sh OPTIONS
125The following options are available:
126.Bl -tag -width indent
127.It Fl A
128Skip symbol lookup and display address instead.
129.It Fl C
130Toggle between showing cumulative or incremental counts for
131subsequent counting mode PMCs specified on the command line.
132The default is to show incremental counts.
133.It Fl D Ar pathname
134Create files with per-program samples in the directory named
135by
136.Ar pathname .
137The default is to create these files in the current directory.
138.It Fl E
139Toggle showing per-process counts at the time a tracked process
140exits for subsequent process-mode PMCs specified on the command line.
141This option is useful for mapping the performance characteristics of a
142complex pipeline of processes when used in conjunction with the
143.Fl d
144option.
145The default is to not to enable per-process tracking.
146.It Fl F Ar pathname
147Print calltree (Kcachegrind) information to file
148.Ar pathname .
149If argument
150.Ar pathname
151is a
152.Dq Li -
153this information is sent to the output file specified by the
154.Fl o
155option.
156.It Fl G Ar pathname
157Print callchain information to file
158.Ar pathname .
159If argument
160.Ar pathname
161is a
162.Dq Li -
163this information is sent to the output file specified by the
164.Fl o
165option.
166.It Fl I
167Show the offset of the instruction pointer into the symbol.
168.It Fl L
169List all event names.
170.It Fl M Ar mapfilename
171Write the mapping between executable objects encountered in the event
172log and the abbreviated pathnames used for
173.Xr gprof 1
174profiles to file
175.Ar mapfilename .
176If this option is not specified, mapping information is not written.
177Argument
178.Ar mapfilename
179may be a
180.Dq Li -
181in which case this mapping information is sent to the output
182file configured by the
183.Fl o
184option.
185.It Fl N
186Toggle capturing callchain information for subsequent sampling PMCs.
187The default is for sampling PMCs to capture callchain information.
188.It Fl O Ar logfilename
189Send logging output to file
190.Ar logfilename .
191If
192.Ar logfilename
193is of the form
194.Ar hostname Ns : Ns Ar port ,
195where
196.Ar hostname
197does not start with a
198.Ql \&.
199or a
200.Ql / ,
201then
202.Nm
203will open a network socket to host
204.Ar hostname
205on port
206.Ar port .
207.Pp
208If the
209.Fl O
210option is not specified and one of the logging options is requested,
211then
212.Nm
213will print a textual form of the logged events to the configured
214output file.
215.It Fl P Ar event-spec
216Allocate a process mode sampling PMC measuring hardware events
217specified in
218.Ar event-spec .
219.It Fl R Ar logfilename
220Perform offline analysis using sampling data in file
221.Ar logfilename .
222.It Fl S Ar event-spec
223Allocate a system mode sampling PMC measuring hardware events
224specified in
225.Ar event-spec .
226.It Fl T
227Use a
228.Xr top 1 Ns -like
229mode for sampling PMCs. The following hotkeys
230can be used:
231.Pp
232.Bl -tag -compact -width "Ctrl+a" -offset 4n
233.It Ic A
234Toggle symbol resolution
235.Sm off
236.It Ic Ctrl + a
237.Sm on
238Switch to accumulative mode
239.Sm off
240.It Ic Ctrl + d
241.Sm on
242Switch to delta mode
243.It Ic f
244Represent the
245.Dq f
246cost under
247threshold as a dot (calltree only)
248.It Ic I
249Toggle showing offsets into symbols
250.It Ic m
251Merge PMCs
252.It Ic n
253Change view
254.It Ic p
255Show next PMC
256.It Ic q
257Quit
258.It Ic Space
259Pause
260.El
261.It Fl U
262Toggle capturing user-space call traces while in kernel mode.
263The default is for sampling PMCs to capture user-space callchain information
264while in user-space mode, and kernel callchain information while in kernel mode.
265.It Fl W
266Toggle logging the incremental counts seen by the threads of a
267tracked process each time they are scheduled on a CPU.
268This is an experimental feature intended to help analyse the
269dynamic behaviour of processes in the system.
270It may incur substantial overhead if enabled.
271The default is for this feature to be disabled.
272.It Fl a Ar pathname
273Perform a symbol and file:line lookup for each address in each
274callgraph and save the output to
275.Ar pathname .
276Unlike
277.Fl m
278that only resolves the first symbol in the graph, this resolves
279every node in the callgraph, or prints out addresses if no
280lookup information is available.
281This option requires the
282.Fl R
283option to read in samples that were previously collected and
284saved with the
285.Fl O
286option.
287.It Fl c Ar cpu-spec
288Set the cpus for subsequent system mode PMCs specified on the
289command line to
290.Ar cpu-spec .
291Argument
292.Ar cpu-spec
293is a comma separated list of CPU numbers, or the literal
294.Sq *
295denoting all available CPUs.
296The default is to allocate system mode PMCs on all available
297CPUs.
298.It Fl d
299Toggle between process mode PMCs measuring events for the target
300process' current and future children or only measuring events for
301the target process.
302The default is to measure events for the target process alone.
303(it has to be passed in the command line prior to
304.Fl p ,
305.Fl s ,
306.Fl P ,
307or
308.Fl S ) .
309.It Fl e
310Specify that the gprof profile files will use a wide history counter.
311These files are produced in a format compatible with
312.Xr gprof 1 .
313However, other tools that cannot fully parse a BSD-style
314gmon header might be unable to correctly parse these files.
315.It Fl f Ar pluginopt
316Pass option string to the active plugin.
317.br
318threshold=<float> do not display cost under specified value (Top).
319.br
320skiplink=0|1 replace node with cost under threshold by a dot (Top).
321.It Fl g
322Produce profiles in a format compatible with
323.Xr gprof 1 .
324A separate profile file is generated for each executable object
325encountered.
326Profile files are placed in sub-directories named by their PMC
327event name.
328.It Fl i Ar lwp
329Filter on thread ID
330.Ar lwp ,
331which you can get from
332.Xr ps 1
333.Fl o
334.Li lwp .
335.It Fl k Ar kerneldir
336Set the pathname of the kernel directory to argument
337.Ar kerneldir .
338This directory specifies where
339.Nm
340should look for the kernel and its modules.
341The default is to use the path of the running kernel obtained from the
342.Va kern.bootfile
343sysctl.
344Modules will also be searched for in /boot/modules if not found in
345.Ar kerneldir .
346.It Fl l Ar secs
347Set system-wide performance measurement duration for
348.Ar secs
349seconds.
350The argument
351.Ar secs
352may be a fractional value.
353.It Fl m Ar pathname
354Print the sampled PCs with the name, the start and ending addresses
355of the function within they live.
356The
357.Ar pathname
358argument is mandatory and indicates where the information will be stored.
359If argument
360.Ar pathname
361is a
362.Dq Li -
363this information is sent to the output file specified by the
364.Fl o
365option.
366This option requires the
367.Fl R
368option to read in samples that were previously collected and
369saved with the
370.Fl O
371option.
372.It Fl n Ar rate
373Set the default sampling rate for subsequent sampling mode
374PMCs specified on the command line.
375The default is to configure PMCs to sample the CPU's instruction
376pointer every 65536 events.
377.It Fl o Ar outputfile
378Send counter readings and textual representations of logged data
379to file
380.Ar outputfile .
381The default is to send output to
382.Pa stderr
383when collecting live data and to
384.Pa stdout
385when processing a pre-existing logfile.
386.It Fl p Ar event-spec
387Allocate a process mode counting PMC measuring hardware events
388specified in
389.Ar event-spec .
390.It Fl q
391Decrease verbosity.
392.It Fl r Ar fsroot
393Set the top of the filesystem hierarchy under which executables
394are located to argument
395.Ar fsroot .
396The default is
397.Pa / .
398.It Fl s Ar event-spec
399Allocate a system mode counting PMC measuring hardware events
400specified in
401.Ar event-spec .
402.It Fl t Ar process-spec
403Attach process mode PMCs to the processes named by argument
404.Ar process-spec .
405Argument
406.Ar process-spec
407may be a non-negative integer denoting a specific process id, or a
408regular expression for selecting processes based on their command names.
409.It Fl u Ar event-spec
410Provide short description of event.
411.It Fl v
412Increase verbosity.
413.It Fl w Ar secs
414Print the values of all counting mode PMCs or sampling mode PMCs
415for top mode every
416.Ar secs
417seconds.
418The argument
419.Ar secs
420may be a fractional value.
421The default interval is 5 seconds.
422.It Fl z Ar graphdepth
423When printing system-wide callgraphs, limit callgraphs to the depth
424specified by argument
425.Ar graphdepth .
426.El
427.Pp
428If
429.Ar command
430is specified, it is executed using
431.Xr execvp 3 .
432.Sh EXAMPLES
433To perform system-wide statistical sampling on an AMD Athlon CPU with
434samples taken every 32768 instruction retirals and data being sampled
435to file
436.Pa sample.stat ,
437use:
438.Dl "pmcstat -O sample.stat -n 32768 -S k7-retired-instructions"
439.Pp
440To execute
441.Nm firefox
442and measure the number of data cache misses suffered
443by it and its children every 12 seconds on an AMD Athlon, use:
444.Dl "pmcstat -d -w 12 -p k7-dc-misses firefox"
445.Pp
446To measure instructions retired for all processes named
447.Dq emacs
448use:
449.Dl "pmcstat -t '^emacs$' -p instructions"
450.Pp
451To measure instructions retired for processes named
452.Dq emacs
453for a period of 10 seconds use:
454.Dl "pmcstat -t '^emacs$' -p instructions sleep 10"
455.Pp
456To count instruction tlb-misses on CPUs 0 and 2 on a Intel
457Pentium Pro/Pentium III SMP system use:
458.Dl "pmcstat -c 0,2 -s p6-itlb-miss"
459.Pp
460To collect profiling information for a specific process with pid 1234
461based on instruction cache misses seen by it use:
462.Dl "pmcstat -P ic-misses -t 1234 -O /tmp/sample.out"
463.Pp
464To perform system-wide sampling on all configured processors
465based on processor instructions retired use:
466.Dl "pmcstat -S instructions -O /tmp/sample.out"
467If callgraph capture is not desired use:
468.Dl "pmcstat -N -S instructions -O /tmp/sample.out"
469.Pp
470To send the generated event log to a remote machine use:
471.Dl "pmcstat -S instructions -O remotehost:port"
472On the remote machine, the sample log can be collected using
473.Xr nc 1 :
474.Dl "nc -l remotehost port > /tmp/sample.out"
475.Pp
476To generate
477.Xr gprof 1
478compatible profiles from a sample file use:
479.Dl "pmcstat -R /tmp/sample.out -g"
480.Pp
481To print a system-wide profile with callgraphs to file
482.Pa "foo.graph"
483use:
484.Dl "pmcstat -R /tmp/sample.out -G foo.graph"
485.Sh DIAGNOSTICS
486If option
487.Fl v
488is specified,
489.Nm
490may issue the following diagnostic messages:
491.Bl -diag
492.It "#callchain/dubious-frames"
493The number of callchain records that had an
494.Dq impossible
495value for a return address.
496.It "#exec handling errors"
497The number of
498.Xr exec 2
499events in the log file that named executables that could not be
500analyzed.
501.It "#exec/elf"
502The number of
503.Xr exec 2
504events that named ELF executables.
505.It "#exec/unknown"
506The number of
507.Xr exec 2
508events that named executables with unrecognized formats.
509.It "#samples/total"
510The total number of samples in the log file.
511.It "#samples/unclaimed"
512The number of samples that could not be correlated to a known
513executable object (i.e., to an executable, shared library, the
514kernel or the runtime loader).
515.It "#samples/unknown-object"
516The number of samples that were associated with an executable
517with an unrecognized object format.
518.El
519.Pp
520.Ex -std
521.Sh COMPATIBILITY
522Due to the limitations of the
523.Pa gmon.out
524file format,
525.Xr gprof 1
526compatible profiles generated by the
527.Fl g
528option do not contain information about calls that cross executable
529boundaries.
530The generated
531.Pa gmon.out
532files are also only meaningful for native executables.
533.Sh SEE ALSO
534.Xr gprof 1 ,
535.Xr nc 1 ,
536.Xr execvp 3 ,
537.Xr pmc 3 ,
538.Xr pmclog 3 ,
539.Xr hwpmc 4 ,
540.Xr pmccontrol 8 ,
541.Xr sysctl 8
542.Sh HISTORY
543The
544.Nm
545utility first appeared in
546.Fx 6.0 .
547It is
548.Ud
549.Sh AUTHORS
550.An Joseph Koshy Aq Mt jkoshy@FreeBSD.org
551.Sh BUGS
552The
553.Nm
554utility cannot yet analyse
555.Xr hwpmc 4
556logs generated by non-native architectures.
557