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