brintos

brintos / linux-shallow public Read only

0
0
Text · 7.2 KiB · 5b479f5 Raw
217 lines · plain
1perf-script-perl(1)2===================3 4NAME5----6perf-script-perl - Process trace data with a Perl script7 8SYNOPSIS9--------10[verse]11'perf script' [-s [Perl]:script[.pl] ]12 13DESCRIPTION14-----------15 16This perf script option is used to process perf script data using perf's17built-in Perl interpreter.  It reads and processes the input file and18displays the results of the trace analysis implemented in the given19Perl script, if any.20 21STARTER SCRIPTS22---------------23 24You can avoid reading the rest of this document by running 'perf script25-g perl' in the same directory as an existing perf.data trace file.26That will generate a starter script containing a handler for each of27the event types in the trace file; it simply prints every available28field for each event in the trace file.29 30You can also look at the existing scripts in31~/libexec/perf-core/scripts/perl for typical examples showing how to32do basic things like aggregate event data, print results, etc.  Also,33the check-perf-script.pl script, while not interesting for its results,34attempts to exercise all of the main scripting features.35 36EVENT HANDLERS37--------------38 39When perf script is invoked using a trace script, a user-defined40'handler function' is called for each event in the trace.  If there's41no handler function defined for a given event type, the event is42ignored (or passed to a 'trace_unhandled' function, see below) and the43next event is processed.44 45Most of the event's field values are passed as arguments to the46handler function; some of the less common ones aren't - those are47available as calls back into the perf executable (see below).48 49As an example, the following perf record command can be used to record50all sched_wakeup events in the system:51 52 # perf record -a -e sched:sched_wakeup53 54Traces meant to be processed using a script should be recorded with55the above option: -a to enable system-wide collection.56 57The format file for the sched_wakeup event defines the following fields58(see /sys/kernel/tracing/events/sched/sched_wakeup/format):59 60----61 format:62        field:unsigned short common_type;63        field:unsigned char common_flags;64        field:unsigned char common_preempt_count;65        field:int common_pid;66 67        field:char comm[TASK_COMM_LEN];68        field:pid_t pid;69        field:int prio;70        field:int success;71        field:int target_cpu;72----73 74The handler function for this event would be defined as:75 76----77sub sched::sched_wakeup78{79   my ($event_name, $context, $common_cpu, $common_secs,80       $common_nsecs, $common_pid, $common_comm,81       $comm, $pid, $prio, $success, $target_cpu) = @_;82}83----84 85The handler function takes the form subsystem::event_name.86 87The $common_* arguments in the handler's argument list are the set of88arguments passed to all event handlers; some of the fields correspond89to the common_* fields in the format file, but some are synthesized,90and some of the common_* fields aren't common enough to to be passed91to every event as arguments but are available as library functions.92 93Here's a brief description of each of the invariant event args:94 95 $event_name 	  	    the name of the event as text96 $context		    an opaque 'cookie' used in calls back into perf97 $common_cpu		    the cpu the event occurred on98 $common_secs		    the secs portion of the event timestamp99 $common_nsecs		    the nsecs portion of the event timestamp100 $common_pid		    the pid of the current task101 $common_comm		    the name of the current process102 103All of the remaining fields in the event's format file have104counterparts as handler function arguments of the same name, as can be105seen in the example above.106 107The above provides the basics needed to directly access every field of108every event in a trace, which covers 90% of what you need to know to109write a useful trace script.  The sections below cover the rest.110 111SCRIPT LAYOUT112-------------113 114Every perf script Perl script should start by setting up a Perl module115search path and 'use'ing a few support modules (see module116descriptions below):117 118----119 use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";120 use lib "./Perf-Trace-Util/lib";121 use Perf::Trace::Core;122 use Perf::Trace::Context;123 use Perf::Trace::Util;124----125 126The rest of the script can contain handler functions and support127functions in any order.128 129Aside from the event handler functions discussed above, every script130can implement a set of optional functions:131 132*trace_begin*, if defined, is called before any event is processed and133gives scripts a chance to do setup tasks:134 135----136 sub trace_begin137 {138 }139----140 141*trace_end*, if defined, is called after all events have been142 processed and gives scripts a chance to do end-of-script tasks, such143 as display results:144 145----146sub trace_end147{148}149----150 151*trace_unhandled*, if defined, is called after for any event that152 doesn't have a handler explicitly defined for it.  The standard set153 of common arguments are passed into it:154 155----156sub trace_unhandled157{158    my ($event_name, $context, $common_cpu, $common_secs,159        $common_nsecs, $common_pid, $common_comm) = @_;160}161----162 163The remaining sections provide descriptions of each of the available164built-in perf script Perl modules and their associated functions.165 166AVAILABLE MODULES AND FUNCTIONS167-------------------------------168 169The following sections describe the functions and variables available170via the various Perf::Trace::* Perl modules.  To use the functions and171variables from the given module, add the corresponding 'use172Perf::Trace::XXX' line to your perf script script.173 174Perf::Trace::Core Module175~~~~~~~~~~~~~~~~~~~~~~~~176 177These functions provide some essential functions to user scripts.178 179The *flag_str* and *symbol_str* functions provide human-readable180strings for flag and symbolic fields.  These correspond to the strings181and values parsed from the 'print fmt' fields of the event format182files:183 184  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_name185  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_name186 187Perf::Trace::Context Module188~~~~~~~~~~~~~~~~~~~~~~~~~~~189 190Some of the 'common' fields in the event format file aren't all that191common, but need to be made accessible to user scripts nonetheless.192 193Perf::Trace::Context defines a set of functions that can be used to194access this data in the context of the current event.  Each of these195functions expects a $context variable, which is the same as the196$context variable passed into every event handler as the second197argument.198 199 common_pc($context) - returns common_preempt count for the current event200 common_flags($context) - returns common_flags for the current event201 common_lock_depth($context) - returns common_lock_depth for the current event202 203Perf::Trace::Util Module204~~~~~~~~~~~~~~~~~~~~~~~~205 206Various utility functions for use with perf script:207 208  nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair209  nsecs_secs($nsecs) - returns whole secs portion given nsecs210  nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs211  nsecs_str($nsecs) - returns printable string in the form secs.nsecs212  avg($total, $n) - returns average given a sum and a total number of values213 214SEE ALSO215--------216linkperf:perf-script[1]217