Path: blob/master/tools/perf/Documentation/perf-script-perl.txt
10821 views
perf-script-perl(1)1==================23NAME4----5perf-script-perl - Process trace data with a Perl script67SYNOPSIS8--------9[verse]10'perf script' [-s [Perl]:script[.pl] ]1112DESCRIPTION13-----------1415This perf script option is used to process perf script data using perf's16built-in Perl interpreter. It reads and processes the input file and17displays the results of the trace analysis implemented in the given18Perl script, if any.1920STARTER SCRIPTS21---------------2223You can avoid reading the rest of this document by running 'perf script24-g perl' in the same directory as an existing perf.data trace file.25That will generate a starter script containing a handler for each of26the event types in the trace file; it simply prints every available27field for each event in the trace file.2829You can also look at the existing scripts in30~/libexec/perf-core/scripts/perl for typical examples showing how to31do basic things like aggregate event data, print results, etc. Also,32the check-perf-script.pl script, while not interesting for its results,33attempts to exercise all of the main scripting features.3435EVENT HANDLERS36--------------3738When perf script is invoked using a trace script, a user-defined39'handler function' is called for each event in the trace. If there's40no handler function defined for a given event type, the event is41ignored (or passed to a 'trace_handled' function, see below) and the42next event is processed.4344Most of the event's field values are passed as arguments to the45handler function; some of the less common ones aren't - those are46available as calls back into the perf executable (see below).4748As an example, the following perf record command can be used to record49all sched_wakeup events in the system:5051# perf record -a -e sched:sched_wakeup5253Traces meant to be processed using a script should be recorded with54the above option: -a to enable system-wide collection.5556The format file for the sched_wakep event defines the following fields57(see /sys/kernel/debug/tracing/events/sched/sched_wakeup/format):5859----60format:61field:unsigned short common_type;62field:unsigned char common_flags;63field:unsigned char common_preempt_count;64field:int common_pid;6566field:char comm[TASK_COMM_LEN];67field:pid_t pid;68field:int prio;69field:int success;70field:int target_cpu;71----7273The handler function for this event would be defined as:7475----76sub sched::sched_wakeup77{78my ($event_name, $context, $common_cpu, $common_secs,79$common_nsecs, $common_pid, $common_comm,80$comm, $pid, $prio, $success, $target_cpu) = @_;81}82----8384The handler function takes the form subsystem::event_name.8586The $common_* arguments in the handler's argument list are the set of87arguments passed to all event handlers; some of the fields correspond88to the common_* fields in the format file, but some are synthesized,89and some of the common_* fields aren't common enough to to be passed90to every event as arguments but are available as library functions.9192Here's a brief description of each of the invariant event args:9394$event_name the name of the event as text95$context an opaque 'cookie' used in calls back into perf96$common_cpu the cpu the event occurred on97$common_secs the secs portion of the event timestamp98$common_nsecs the nsecs portion of the event timestamp99$common_pid the pid of the current task100$common_comm the name of the current process101102All of the remaining fields in the event's format file have103counterparts as handler function arguments of the same name, as can be104seen in the example above.105106The above provides the basics needed to directly access every field of107every event in a trace, which covers 90% of what you need to know to108write a useful trace script. The sections below cover the rest.109110SCRIPT LAYOUT111-------------112113Every perf script Perl script should start by setting up a Perl module114search path and 'use'ing a few support modules (see module115descriptions below):116117----118use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/perf-script-Util/lib";119use lib "./perf-script-Util/lib";120use Perf::Trace::Core;121use Perf::Trace::Context;122use Perf::Trace::Util;123----124125The rest of the script can contain handler functions and support126functions in any order.127128Aside from the event handler functions discussed above, every script129can implement a set of optional functions:130131*trace_begin*, if defined, is called before any event is processed and132gives scripts a chance to do setup tasks:133134----135sub trace_begin136{137}138----139140*trace_end*, if defined, is called after all events have been141processed and gives scripts a chance to do end-of-script tasks, such142as display results:143144----145sub trace_end146{147}148----149150*trace_unhandled*, if defined, is called after for any event that151doesn't have a handler explicitly defined for it. The standard set152of common arguments are passed into it:153154----155sub trace_unhandled156{157my ($event_name, $context, $common_cpu, $common_secs,158$common_nsecs, $common_pid, $common_comm) = @_;159}160----161162The remaining sections provide descriptions of each of the available163built-in perf script Perl modules and their associated functions.164165AVAILABLE MODULES AND FUNCTIONS166-------------------------------167168The following sections describe the functions and variables available169via the various Perf::Trace::* Perl modules. To use the functions and170variables from the given module, add the corresponding 'use171Perf::Trace::XXX' line to your perf script script.172173Perf::Trace::Core Module174~~~~~~~~~~~~~~~~~~~~~~~~175176These functions provide some essential functions to user scripts.177178The *flag_str* and *symbol_str* functions provide human-readable179strings for flag and symbolic fields. These correspond to the strings180and values parsed from the 'print fmt' fields of the event format181files:182183flag_str($event_name, $field_name, $field_value) - returns the string represention corresponding to $field_value for the flag field $field_name of event $event_name184symbol_str($event_name, $field_name, $field_value) - returns the string represention corresponding to $field_value for the symbolic field $field_name of event $event_name185186Perf::Trace::Context Module187~~~~~~~~~~~~~~~~~~~~~~~~~~~188189Some of the 'common' fields in the event format file aren't all that190common, but need to be made accessible to user scripts nonetheless.191192Perf::Trace::Context defines a set of functions that can be used to193access this data in the context of the current event. Each of these194functions expects a $context variable, which is the same as the195$context variable passed into every event handler as the second196argument.197198common_pc($context) - returns common_preempt count for the current event199common_flags($context) - returns common_flags for the current event200common_lock_depth($context) - returns common_lock_depth for the current event201202Perf::Trace::Util Module203~~~~~~~~~~~~~~~~~~~~~~~~204205Various utility functions for use with perf script:206207nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair208nsecs_secs($nsecs) - returns whole secs portion given nsecs209nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs210nsecs_str($nsecs) - returns printable string in the form secs.nsecs211avg($total, $n) - returns average given a sum and a total number of values212213SEE ALSO214--------215linkperf:perf-script[1]216217218