xref: /openbmc/linux/tools/perf/Documentation/perf-script-perl.txt (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1133dc4c3SIngo Molnarperf-script-perl(1)
2ea66536aSTakashi Iwai===================
3133dc4c3SIngo Molnar
4133dc4c3SIngo MolnarNAME
5133dc4c3SIngo Molnar----
6133dc4c3SIngo Molnarperf-script-perl - Process trace data with a Perl script
7133dc4c3SIngo Molnar
8133dc4c3SIngo MolnarSYNOPSIS
9133dc4c3SIngo Molnar--------
10133dc4c3SIngo Molnar[verse]
11133dc4c3SIngo Molnar'perf script' [-s [Perl]:script[.pl] ]
12133dc4c3SIngo Molnar
13133dc4c3SIngo MolnarDESCRIPTION
14133dc4c3SIngo Molnar-----------
15133dc4c3SIngo Molnar
16133dc4c3SIngo MolnarThis perf script option is used to process perf script data using perf's
17133dc4c3SIngo Molnarbuilt-in Perl interpreter.  It reads and processes the input file and
18133dc4c3SIngo Molnardisplays the results of the trace analysis implemented in the given
19133dc4c3SIngo MolnarPerl script, if any.
20133dc4c3SIngo Molnar
21133dc4c3SIngo MolnarSTARTER SCRIPTS
22133dc4c3SIngo Molnar---------------
23133dc4c3SIngo Molnar
24133dc4c3SIngo MolnarYou can avoid reading the rest of this document by running 'perf script
25133dc4c3SIngo Molnar-g perl' in the same directory as an existing perf.data trace file.
26133dc4c3SIngo MolnarThat will generate a starter script containing a handler for each of
27133dc4c3SIngo Molnarthe event types in the trace file; it simply prints every available
28133dc4c3SIngo Molnarfield for each event in the trace file.
29133dc4c3SIngo Molnar
30133dc4c3SIngo MolnarYou can also look at the existing scripts in
31133dc4c3SIngo Molnar~/libexec/perf-core/scripts/perl for typical examples showing how to
32133dc4c3SIngo Molnardo basic things like aggregate event data, print results, etc.  Also,
33133dc4c3SIngo Molnarthe check-perf-script.pl script, while not interesting for its results,
34133dc4c3SIngo Molnarattempts to exercise all of the main scripting features.
35133dc4c3SIngo Molnar
36133dc4c3SIngo MolnarEVENT HANDLERS
37133dc4c3SIngo Molnar--------------
38133dc4c3SIngo Molnar
39133dc4c3SIngo MolnarWhen perf script is invoked using a trace script, a user-defined
40133dc4c3SIngo Molnar'handler function' is called for each event in the trace.  If there's
41133dc4c3SIngo Molnarno handler function defined for a given event type, the event is
4234d4453dSSeongJae Parkignored (or passed to a 'trace_unhandled' function, see below) and the
43133dc4c3SIngo Molnarnext event is processed.
44133dc4c3SIngo Molnar
45133dc4c3SIngo MolnarMost of the event's field values are passed as arguments to the
46133dc4c3SIngo Molnarhandler function; some of the less common ones aren't - those are
47133dc4c3SIngo Molnaravailable as calls back into the perf executable (see below).
48133dc4c3SIngo Molnar
49133dc4c3SIngo MolnarAs an example, the following perf record command can be used to record
50133dc4c3SIngo Molnarall sched_wakeup events in the system:
51133dc4c3SIngo Molnar
52133dc4c3SIngo Molnar # perf record -a -e sched:sched_wakeup
53133dc4c3SIngo Molnar
54133dc4c3SIngo MolnarTraces meant to be processed using a script should be recorded with
55133dc4c3SIngo Molnarthe above option: -a to enable system-wide collection.
56133dc4c3SIngo Molnar
574da6552cSLike XuThe format file for the sched_wakeup event defines the following fields
58*1df49ef9SRoss Zwisler(see /sys/kernel/tracing/events/sched/sched_wakeup/format):
59133dc4c3SIngo Molnar
60133dc4c3SIngo Molnar----
61133dc4c3SIngo Molnar format:
62133dc4c3SIngo Molnar        field:unsigned short common_type;
63133dc4c3SIngo Molnar        field:unsigned char common_flags;
64133dc4c3SIngo Molnar        field:unsigned char common_preempt_count;
65133dc4c3SIngo Molnar        field:int common_pid;
66133dc4c3SIngo Molnar
67133dc4c3SIngo Molnar        field:char comm[TASK_COMM_LEN];
68133dc4c3SIngo Molnar        field:pid_t pid;
69133dc4c3SIngo Molnar        field:int prio;
70133dc4c3SIngo Molnar        field:int success;
71133dc4c3SIngo Molnar        field:int target_cpu;
72133dc4c3SIngo Molnar----
73133dc4c3SIngo Molnar
74133dc4c3SIngo MolnarThe handler function for this event would be defined as:
75133dc4c3SIngo Molnar
76133dc4c3SIngo Molnar----
77133dc4c3SIngo Molnarsub sched::sched_wakeup
78133dc4c3SIngo Molnar{
79133dc4c3SIngo Molnar   my ($event_name, $context, $common_cpu, $common_secs,
80133dc4c3SIngo Molnar       $common_nsecs, $common_pid, $common_comm,
81133dc4c3SIngo Molnar       $comm, $pid, $prio, $success, $target_cpu) = @_;
82133dc4c3SIngo Molnar}
83133dc4c3SIngo Molnar----
84133dc4c3SIngo Molnar
85133dc4c3SIngo MolnarThe handler function takes the form subsystem::event_name.
86133dc4c3SIngo Molnar
87133dc4c3SIngo MolnarThe $common_* arguments in the handler's argument list are the set of
88133dc4c3SIngo Molnararguments passed to all event handlers; some of the fields correspond
89133dc4c3SIngo Molnarto the common_* fields in the format file, but some are synthesized,
90133dc4c3SIngo Molnarand some of the common_* fields aren't common enough to to be passed
91133dc4c3SIngo Molnarto every event as arguments but are available as library functions.
92133dc4c3SIngo Molnar
93133dc4c3SIngo MolnarHere's a brief description of each of the invariant event args:
94133dc4c3SIngo Molnar
95133dc4c3SIngo Molnar $event_name 	  	    the name of the event as text
96133dc4c3SIngo Molnar $context		    an opaque 'cookie' used in calls back into perf
97133dc4c3SIngo Molnar $common_cpu		    the cpu the event occurred on
98133dc4c3SIngo Molnar $common_secs		    the secs portion of the event timestamp
99133dc4c3SIngo Molnar $common_nsecs		    the nsecs portion of the event timestamp
100133dc4c3SIngo Molnar $common_pid		    the pid of the current task
101133dc4c3SIngo Molnar $common_comm		    the name of the current process
102133dc4c3SIngo Molnar
103133dc4c3SIngo MolnarAll of the remaining fields in the event's format file have
104133dc4c3SIngo Molnarcounterparts as handler function arguments of the same name, as can be
105133dc4c3SIngo Molnarseen in the example above.
106133dc4c3SIngo Molnar
107133dc4c3SIngo MolnarThe above provides the basics needed to directly access every field of
108133dc4c3SIngo Molnarevery event in a trace, which covers 90% of what you need to know to
109133dc4c3SIngo Molnarwrite a useful trace script.  The sections below cover the rest.
110133dc4c3SIngo Molnar
111133dc4c3SIngo MolnarSCRIPT LAYOUT
112133dc4c3SIngo Molnar-------------
113133dc4c3SIngo Molnar
114133dc4c3SIngo MolnarEvery perf script Perl script should start by setting up a Perl module
115133dc4c3SIngo Molnarsearch path and 'use'ing a few support modules (see module
116133dc4c3SIngo Molnardescriptions below):
117133dc4c3SIngo Molnar
118133dc4c3SIngo Molnar----
119e8d0f400SDavid Ahern use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
120e8d0f400SDavid Ahern use lib "./Perf-Trace-Util/lib";
121133dc4c3SIngo Molnar use Perf::Trace::Core;
122133dc4c3SIngo Molnar use Perf::Trace::Context;
123133dc4c3SIngo Molnar use Perf::Trace::Util;
124133dc4c3SIngo Molnar----
125133dc4c3SIngo Molnar
126133dc4c3SIngo MolnarThe rest of the script can contain handler functions and support
127133dc4c3SIngo Molnarfunctions in any order.
128133dc4c3SIngo Molnar
129133dc4c3SIngo MolnarAside from the event handler functions discussed above, every script
130133dc4c3SIngo Molnarcan implement a set of optional functions:
131133dc4c3SIngo Molnar
132133dc4c3SIngo Molnar*trace_begin*, if defined, is called before any event is processed and
133133dc4c3SIngo Molnargives scripts a chance to do setup tasks:
134133dc4c3SIngo Molnar
135133dc4c3SIngo Molnar----
136133dc4c3SIngo Molnar sub trace_begin
137133dc4c3SIngo Molnar {
138133dc4c3SIngo Molnar }
139133dc4c3SIngo Molnar----
140133dc4c3SIngo Molnar
141133dc4c3SIngo Molnar*trace_end*, if defined, is called after all events have been
142133dc4c3SIngo Molnar processed and gives scripts a chance to do end-of-script tasks, such
143133dc4c3SIngo Molnar as display results:
144133dc4c3SIngo Molnar
145133dc4c3SIngo Molnar----
146133dc4c3SIngo Molnarsub trace_end
147133dc4c3SIngo Molnar{
148133dc4c3SIngo Molnar}
149133dc4c3SIngo Molnar----
150133dc4c3SIngo Molnar
151133dc4c3SIngo Molnar*trace_unhandled*, if defined, is called after for any event that
152133dc4c3SIngo Molnar doesn't have a handler explicitly defined for it.  The standard set
153133dc4c3SIngo Molnar of common arguments are passed into it:
154133dc4c3SIngo Molnar
155133dc4c3SIngo Molnar----
156133dc4c3SIngo Molnarsub trace_unhandled
157133dc4c3SIngo Molnar{
158133dc4c3SIngo Molnar    my ($event_name, $context, $common_cpu, $common_secs,
159133dc4c3SIngo Molnar        $common_nsecs, $common_pid, $common_comm) = @_;
160133dc4c3SIngo Molnar}
161133dc4c3SIngo Molnar----
162133dc4c3SIngo Molnar
163133dc4c3SIngo MolnarThe remaining sections provide descriptions of each of the available
164133dc4c3SIngo Molnarbuilt-in perf script Perl modules and their associated functions.
165133dc4c3SIngo Molnar
166133dc4c3SIngo MolnarAVAILABLE MODULES AND FUNCTIONS
167133dc4c3SIngo Molnar-------------------------------
168133dc4c3SIngo Molnar
169133dc4c3SIngo MolnarThe following sections describe the functions and variables available
170133dc4c3SIngo Molnarvia the various Perf::Trace::* Perl modules.  To use the functions and
171133dc4c3SIngo Molnarvariables from the given module, add the corresponding 'use
172133dc4c3SIngo MolnarPerf::Trace::XXX' line to your perf script script.
173133dc4c3SIngo Molnar
174133dc4c3SIngo MolnarPerf::Trace::Core Module
175133dc4c3SIngo Molnar~~~~~~~~~~~~~~~~~~~~~~~~
176133dc4c3SIngo Molnar
177133dc4c3SIngo MolnarThese functions provide some essential functions to user scripts.
178133dc4c3SIngo Molnar
179133dc4c3SIngo MolnarThe *flag_str* and *symbol_str* functions provide human-readable
180133dc4c3SIngo Molnarstrings for flag and symbolic fields.  These correspond to the strings
181133dc4c3SIngo Molnarand values parsed from the 'print fmt' fields of the event format
182133dc4c3SIngo Molnarfiles:
183133dc4c3SIngo Molnar
18496355f2cSMasanari Iida  flag_str($event_name, $field_name, $field_value) - returns the string representation corresponding to $field_value for the flag field $field_name of event $event_name
18596355f2cSMasanari Iida  symbol_str($event_name, $field_name, $field_value) - returns the string representation corresponding to $field_value for the symbolic field $field_name of event $event_name
186133dc4c3SIngo Molnar
187133dc4c3SIngo MolnarPerf::Trace::Context Module
188133dc4c3SIngo Molnar~~~~~~~~~~~~~~~~~~~~~~~~~~~
189133dc4c3SIngo Molnar
190133dc4c3SIngo MolnarSome of the 'common' fields in the event format file aren't all that
191133dc4c3SIngo Molnarcommon, but need to be made accessible to user scripts nonetheless.
192133dc4c3SIngo Molnar
193133dc4c3SIngo MolnarPerf::Trace::Context defines a set of functions that can be used to
194133dc4c3SIngo Molnaraccess this data in the context of the current event.  Each of these
195133dc4c3SIngo Molnarfunctions expects a $context variable, which is the same as the
196133dc4c3SIngo Molnar$context variable passed into every event handler as the second
197133dc4c3SIngo Molnarargument.
198133dc4c3SIngo Molnar
199133dc4c3SIngo Molnar common_pc($context) - returns common_preempt count for the current event
200133dc4c3SIngo Molnar common_flags($context) - returns common_flags for the current event
201133dc4c3SIngo Molnar common_lock_depth($context) - returns common_lock_depth for the current event
202133dc4c3SIngo Molnar
203133dc4c3SIngo MolnarPerf::Trace::Util Module
204133dc4c3SIngo Molnar~~~~~~~~~~~~~~~~~~~~~~~~
205133dc4c3SIngo Molnar
206133dc4c3SIngo MolnarVarious utility functions for use with perf script:
207133dc4c3SIngo Molnar
208133dc4c3SIngo Molnar  nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair
209133dc4c3SIngo Molnar  nsecs_secs($nsecs) - returns whole secs portion given nsecs
210133dc4c3SIngo Molnar  nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs
211133dc4c3SIngo Molnar  nsecs_str($nsecs) - returns printable string in the form secs.nsecs
212133dc4c3SIngo Molnar  avg($total, $n) - returns average given a sum and a total number of values
213133dc4c3SIngo Molnar
214133dc4c3SIngo MolnarSEE ALSO
215133dc4c3SIngo Molnar--------
216133dc4c3SIngo Molnarlinkperf:perf-script[1]
217