perf-script-perl.txt 7.2 KB
Newer Older
1
perf-script-perl(1)
2 3 4 5
==================

NAME
----
6
perf-script-perl - Process trace data with a Perl script
7 8 9 10

SYNOPSIS
--------
[verse]
11
'perf script' [-s [Perl]:script[.pl] ]
12 13 14 15

DESCRIPTION
-----------

16
This perf script option is used to process perf script data using perf's
17 18 19 20 21 22 23
built-in Perl interpreter.  It reads and processes the input file and
displays the results of the trace analysis implemented in the given
Perl script, if any.

STARTER SCRIPTS
---------------

24
You can avoid reading the rest of this document by running 'perf script
25 26 27 28 29 30 31 32
-g perl' in the same directory as an existing perf.data trace file.
That will generate a starter script containing a handler for each of
the event types in the trace file; it simply prints every available
field for each event in the trace file.

You can also look at the existing scripts in
~/libexec/perf-core/scripts/perl for typical examples showing how to
do basic things like aggregate event data, print results, etc.  Also,
33
the check-perf-script.pl script, while not interesting for its results,
34 35 36 37 38
attempts to exercise all of the main scripting features.

EVENT HANDLERS
--------------

39
When perf script is invoked using a trace script, a user-defined
40 41 42 43 44 45 46 47 48 49 50 51
'handler function' is called for each event in the trace.  If there's
no handler function defined for a given event type, the event is
ignored (or passed to a 'trace_handled' function, see below) and the
next event is processed.

Most of the event's field values are passed as arguments to the
handler function; some of the less common ones aren't - those are
available as calls back into the perf executable (see below).

As an example, the following perf record command can be used to record
all sched_wakeup events in the system:

52
 # perf record -a -e sched:sched_wakeup
53 54

Traces meant to be processed using a script should be recorded with
55
the above option: -a to enable system-wide collection.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

The format file for the sched_wakep event defines the following fields
(see /sys/kernel/debug/tracing/events/sched/sched_wakeup/format):

----
 format:
        field:unsigned short common_type;
        field:unsigned char common_flags;
        field:unsigned char common_preempt_count;
        field:int common_pid;

        field:char comm[TASK_COMM_LEN];
        field:pid_t pid;
        field:int prio;
        field:int success;
        field:int target_cpu;
----

The handler function for this event would be defined as:

----
sub sched::sched_wakeup
{
   my ($event_name, $context, $common_cpu, $common_secs,
       $common_nsecs, $common_pid, $common_comm,
       $comm, $pid, $prio, $success, $target_cpu) = @_;
}
----

The handler function takes the form subsystem::event_name.

The $common_* arguments in the handler's argument list are the set of
arguments passed to all event handlers; some of the fields correspond
to the common_* fields in the format file, but some are synthesized,
and some of the common_* fields aren't common enough to to be passed
to every event as arguments but are available as library functions.

Here's a brief description of each of the invariant event args:

 $event_name 	  	    the name of the event as text
 $context		    an opaque 'cookie' used in calls back into perf
 $common_cpu		    the cpu the event occurred on
 $common_secs		    the secs portion of the event timestamp
 $common_nsecs		    the nsecs portion of the event timestamp
 $common_pid		    the pid of the current task
 $common_comm		    the name of the current process

All of the remaining fields in the event's format file have
counterparts as handler function arguments of the same name, as can be
seen in the example above.

The above provides the basics needed to directly access every field of
every event in a trace, which covers 90% of what you need to know to
write a useful trace script.  The sections below cover the rest.

SCRIPT LAYOUT
-------------

114
Every perf script Perl script should start by setting up a Perl module
115 116 117 118
search path and 'use'ing a few support modules (see module
descriptions below):

----
119 120
 use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/perf-script-Util/lib";
 use lib "./perf-script-Util/lib";
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
 use Perf::Trace::Core;
 use Perf::Trace::Context;
 use Perf::Trace::Util;
----

The rest of the script can contain handler functions and support
functions in any order.

Aside from the event handler functions discussed above, every script
can implement a set of optional functions:

*trace_begin*, if defined, is called before any event is processed and
gives scripts a chance to do setup tasks:

----
 sub trace_begin
 {
 }
----

*trace_end*, if defined, is called after all events have been
 processed and gives scripts a chance to do end-of-script tasks, such
 as display results:

----
sub trace_end
{
}
----

*trace_unhandled*, if defined, is called after for any event that
 doesn't have a handler explicitly defined for it.  The standard set
 of common arguments are passed into it:

----
sub trace_unhandled
{
    my ($event_name, $context, $common_cpu, $common_secs,
        $common_nsecs, $common_pid, $common_comm) = @_;
}
----

The remaining sections provide descriptions of each of the available
164
built-in perf script Perl modules and their associated functions.
165 166 167 168 169 170 171

AVAILABLE MODULES AND FUNCTIONS
-------------------------------

The following sections describe the functions and variables available
via the various Perf::Trace::* Perl modules.  To use the functions and
variables from the given module, add the corresponding 'use
172
Perf::Trace::XXX' line to your perf script script.
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

Perf::Trace::Core Module
~~~~~~~~~~~~~~~~~~~~~~~~

These functions provide some essential functions to user scripts.

The *flag_str* and *symbol_str* functions provide human-readable
strings for flag and symbolic fields.  These correspond to the strings
and values parsed from the 'print fmt' fields of the event format
files:

  flag_str($event_name, $field_name, $field_value) - returns the string represention corresponding to $field_value for the flag field $field_name of event $event_name
  symbol_str($event_name, $field_name, $field_value) - returns the string represention corresponding to $field_value for the symbolic field $field_name of event $event_name

Perf::Trace::Context Module
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Some of the 'common' fields in the event format file aren't all that
common, but need to be made accessible to user scripts nonetheless.

Perf::Trace::Context defines a set of functions that can be used to
access this data in the context of the current event.  Each of these
functions expects a $context variable, which is the same as the
$context variable passed into every event handler as the second
argument.

 common_pc($context) - returns common_preempt count for the current event
 common_flags($context) - returns common_flags for the current event
 common_lock_depth($context) - returns common_lock_depth for the current event

Perf::Trace::Util Module
~~~~~~~~~~~~~~~~~~~~~~~~

206
Various utility functions for use with perf script:
207 208 209 210 211 212 213 214 215

  nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair
  nsecs_secs($nsecs) - returns whole secs portion given nsecs
  nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs
  nsecs_str($nsecs) - returns printable string in the form secs.nsecs
  avg($total, $n) - returns average given a sum and a total number of values

SEE ALSO
--------
216
linkperf:perf-script[1]