xref: /OK3568_Linux_fs/kernel/tools/perf/Documentation/perf-script-perl.txt (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyunperf-script-perl(1)
2*4882a593Smuzhiyun===================
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunNAME
5*4882a593Smuzhiyun----
6*4882a593Smuzhiyunperf-script-perl - Process trace data with a Perl script
7*4882a593Smuzhiyun
8*4882a593SmuzhiyunSYNOPSIS
9*4882a593Smuzhiyun--------
10*4882a593Smuzhiyun[verse]
11*4882a593Smuzhiyun'perf script' [-s [Perl]:script[.pl] ]
12*4882a593Smuzhiyun
13*4882a593SmuzhiyunDESCRIPTION
14*4882a593Smuzhiyun-----------
15*4882a593Smuzhiyun
16*4882a593SmuzhiyunThis perf script option is used to process perf script data using perf's
17*4882a593Smuzhiyunbuilt-in Perl interpreter.  It reads and processes the input file and
18*4882a593Smuzhiyundisplays the results of the trace analysis implemented in the given
19*4882a593SmuzhiyunPerl script, if any.
20*4882a593Smuzhiyun
21*4882a593SmuzhiyunSTARTER SCRIPTS
22*4882a593Smuzhiyun---------------
23*4882a593Smuzhiyun
24*4882a593SmuzhiyunYou can avoid reading the rest of this document by running 'perf script
25*4882a593Smuzhiyun-g perl' in the same directory as an existing perf.data trace file.
26*4882a593SmuzhiyunThat will generate a starter script containing a handler for each of
27*4882a593Smuzhiyunthe event types in the trace file; it simply prints every available
28*4882a593Smuzhiyunfield for each event in the trace file.
29*4882a593Smuzhiyun
30*4882a593SmuzhiyunYou can also look at the existing scripts in
31*4882a593Smuzhiyun~/libexec/perf-core/scripts/perl for typical examples showing how to
32*4882a593Smuzhiyundo basic things like aggregate event data, print results, etc.  Also,
33*4882a593Smuzhiyunthe check-perf-script.pl script, while not interesting for its results,
34*4882a593Smuzhiyunattempts to exercise all of the main scripting features.
35*4882a593Smuzhiyun
36*4882a593SmuzhiyunEVENT HANDLERS
37*4882a593Smuzhiyun--------------
38*4882a593Smuzhiyun
39*4882a593SmuzhiyunWhen perf script is invoked using a trace script, a user-defined
40*4882a593Smuzhiyun'handler function' is called for each event in the trace.  If there's
41*4882a593Smuzhiyunno handler function defined for a given event type, the event is
42*4882a593Smuzhiyunignored (or passed to a 'trace_unhandled' function, see below) and the
43*4882a593Smuzhiyunnext event is processed.
44*4882a593Smuzhiyun
45*4882a593SmuzhiyunMost of the event's field values are passed as arguments to the
46*4882a593Smuzhiyunhandler function; some of the less common ones aren't - those are
47*4882a593Smuzhiyunavailable as calls back into the perf executable (see below).
48*4882a593Smuzhiyun
49*4882a593SmuzhiyunAs an example, the following perf record command can be used to record
50*4882a593Smuzhiyunall sched_wakeup events in the system:
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun # perf record -a -e sched:sched_wakeup
53*4882a593Smuzhiyun
54*4882a593SmuzhiyunTraces meant to be processed using a script should be recorded with
55*4882a593Smuzhiyunthe above option: -a to enable system-wide collection.
56*4882a593Smuzhiyun
57*4882a593SmuzhiyunThe format file for the sched_wakep event defines the following fields
58*4882a593Smuzhiyun(see /sys/kernel/debug/tracing/events/sched/sched_wakeup/format):
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun----
61*4882a593Smuzhiyun format:
62*4882a593Smuzhiyun        field:unsigned short common_type;
63*4882a593Smuzhiyun        field:unsigned char common_flags;
64*4882a593Smuzhiyun        field:unsigned char common_preempt_count;
65*4882a593Smuzhiyun        field:int common_pid;
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun        field:char comm[TASK_COMM_LEN];
68*4882a593Smuzhiyun        field:pid_t pid;
69*4882a593Smuzhiyun        field:int prio;
70*4882a593Smuzhiyun        field:int success;
71*4882a593Smuzhiyun        field:int target_cpu;
72*4882a593Smuzhiyun----
73*4882a593Smuzhiyun
74*4882a593SmuzhiyunThe handler function for this event would be defined as:
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun----
77*4882a593Smuzhiyunsub sched::sched_wakeup
78*4882a593Smuzhiyun{
79*4882a593Smuzhiyun   my ($event_name, $context, $common_cpu, $common_secs,
80*4882a593Smuzhiyun       $common_nsecs, $common_pid, $common_comm,
81*4882a593Smuzhiyun       $comm, $pid, $prio, $success, $target_cpu) = @_;
82*4882a593Smuzhiyun}
83*4882a593Smuzhiyun----
84*4882a593Smuzhiyun
85*4882a593SmuzhiyunThe handler function takes the form subsystem::event_name.
86*4882a593Smuzhiyun
87*4882a593SmuzhiyunThe $common_* arguments in the handler's argument list are the set of
88*4882a593Smuzhiyunarguments passed to all event handlers; some of the fields correspond
89*4882a593Smuzhiyunto the common_* fields in the format file, but some are synthesized,
90*4882a593Smuzhiyunand some of the common_* fields aren't common enough to to be passed
91*4882a593Smuzhiyunto every event as arguments but are available as library functions.
92*4882a593Smuzhiyun
93*4882a593SmuzhiyunHere's a brief description of each of the invariant event args:
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun $event_name 	  	    the name of the event as text
96*4882a593Smuzhiyun $context		    an opaque 'cookie' used in calls back into perf
97*4882a593Smuzhiyun $common_cpu		    the cpu the event occurred on
98*4882a593Smuzhiyun $common_secs		    the secs portion of the event timestamp
99*4882a593Smuzhiyun $common_nsecs		    the nsecs portion of the event timestamp
100*4882a593Smuzhiyun $common_pid		    the pid of the current task
101*4882a593Smuzhiyun $common_comm		    the name of the current process
102*4882a593Smuzhiyun
103*4882a593SmuzhiyunAll of the remaining fields in the event's format file have
104*4882a593Smuzhiyuncounterparts as handler function arguments of the same name, as can be
105*4882a593Smuzhiyunseen in the example above.
106*4882a593Smuzhiyun
107*4882a593SmuzhiyunThe above provides the basics needed to directly access every field of
108*4882a593Smuzhiyunevery event in a trace, which covers 90% of what you need to know to
109*4882a593Smuzhiyunwrite a useful trace script.  The sections below cover the rest.
110*4882a593Smuzhiyun
111*4882a593SmuzhiyunSCRIPT LAYOUT
112*4882a593Smuzhiyun-------------
113*4882a593Smuzhiyun
114*4882a593SmuzhiyunEvery perf script Perl script should start by setting up a Perl module
115*4882a593Smuzhiyunsearch path and 'use'ing a few support modules (see module
116*4882a593Smuzhiyundescriptions below):
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun----
119*4882a593Smuzhiyun use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
120*4882a593Smuzhiyun use lib "./Perf-Trace-Util/lib";
121*4882a593Smuzhiyun use Perf::Trace::Core;
122*4882a593Smuzhiyun use Perf::Trace::Context;
123*4882a593Smuzhiyun use Perf::Trace::Util;
124*4882a593Smuzhiyun----
125*4882a593Smuzhiyun
126*4882a593SmuzhiyunThe rest of the script can contain handler functions and support
127*4882a593Smuzhiyunfunctions in any order.
128*4882a593Smuzhiyun
129*4882a593SmuzhiyunAside from the event handler functions discussed above, every script
130*4882a593Smuzhiyuncan implement a set of optional functions:
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun*trace_begin*, if defined, is called before any event is processed and
133*4882a593Smuzhiyungives scripts a chance to do setup tasks:
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun----
136*4882a593Smuzhiyun sub trace_begin
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun----
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun*trace_end*, if defined, is called after all events have been
142*4882a593Smuzhiyun processed and gives scripts a chance to do end-of-script tasks, such
143*4882a593Smuzhiyun as display results:
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun----
146*4882a593Smuzhiyunsub trace_end
147*4882a593Smuzhiyun{
148*4882a593Smuzhiyun}
149*4882a593Smuzhiyun----
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun*trace_unhandled*, if defined, is called after for any event that
152*4882a593Smuzhiyun doesn't have a handler explicitly defined for it.  The standard set
153*4882a593Smuzhiyun of common arguments are passed into it:
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun----
156*4882a593Smuzhiyunsub trace_unhandled
157*4882a593Smuzhiyun{
158*4882a593Smuzhiyun    my ($event_name, $context, $common_cpu, $common_secs,
159*4882a593Smuzhiyun        $common_nsecs, $common_pid, $common_comm) = @_;
160*4882a593Smuzhiyun}
161*4882a593Smuzhiyun----
162*4882a593Smuzhiyun
163*4882a593SmuzhiyunThe remaining sections provide descriptions of each of the available
164*4882a593Smuzhiyunbuilt-in perf script Perl modules and their associated functions.
165*4882a593Smuzhiyun
166*4882a593SmuzhiyunAVAILABLE MODULES AND FUNCTIONS
167*4882a593Smuzhiyun-------------------------------
168*4882a593Smuzhiyun
169*4882a593SmuzhiyunThe following sections describe the functions and variables available
170*4882a593Smuzhiyunvia the various Perf::Trace::* Perl modules.  To use the functions and
171*4882a593Smuzhiyunvariables from the given module, add the corresponding 'use
172*4882a593SmuzhiyunPerf::Trace::XXX' line to your perf script script.
173*4882a593Smuzhiyun
174*4882a593SmuzhiyunPerf::Trace::Core Module
175*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunThese functions provide some essential functions to user scripts.
178*4882a593Smuzhiyun
179*4882a593SmuzhiyunThe *flag_str* and *symbol_str* functions provide human-readable
180*4882a593Smuzhiyunstrings for flag and symbolic fields.  These correspond to the strings
181*4882a593Smuzhiyunand values parsed from the 'print fmt' fields of the event format
182*4882a593Smuzhiyunfiles:
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun  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
185*4882a593Smuzhiyun  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
186*4882a593Smuzhiyun
187*4882a593SmuzhiyunPerf::Trace::Context Module
188*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~
189*4882a593Smuzhiyun
190*4882a593SmuzhiyunSome of the 'common' fields in the event format file aren't all that
191*4882a593Smuzhiyuncommon, but need to be made accessible to user scripts nonetheless.
192*4882a593Smuzhiyun
193*4882a593SmuzhiyunPerf::Trace::Context defines a set of functions that can be used to
194*4882a593Smuzhiyunaccess this data in the context of the current event.  Each of these
195*4882a593Smuzhiyunfunctions expects a $context variable, which is the same as the
196*4882a593Smuzhiyun$context variable passed into every event handler as the second
197*4882a593Smuzhiyunargument.
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun common_pc($context) - returns common_preempt count for the current event
200*4882a593Smuzhiyun common_flags($context) - returns common_flags for the current event
201*4882a593Smuzhiyun common_lock_depth($context) - returns common_lock_depth for the current event
202*4882a593Smuzhiyun
203*4882a593SmuzhiyunPerf::Trace::Util Module
204*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~
205*4882a593Smuzhiyun
206*4882a593SmuzhiyunVarious utility functions for use with perf script:
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun  nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair
209*4882a593Smuzhiyun  nsecs_secs($nsecs) - returns whole secs portion given nsecs
210*4882a593Smuzhiyun  nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs
211*4882a593Smuzhiyun  nsecs_str($nsecs) - returns printable string in the form secs.nsecs
212*4882a593Smuzhiyun  avg($total, $n) - returns average given a sum and a total number of values
213*4882a593Smuzhiyun
214*4882a593SmuzhiyunSEE ALSO
215*4882a593Smuzhiyun--------
216*4882a593Smuzhiyunlinkperf:perf-script[1]
217