18c2ecf20Sopenharmony_ciperf-script-perl(1)
28c2ecf20Sopenharmony_ci===================
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ciNAME
58c2ecf20Sopenharmony_ci----
68c2ecf20Sopenharmony_ciperf-script-perl - Process trace data with a Perl script
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciSYNOPSIS
98c2ecf20Sopenharmony_ci--------
108c2ecf20Sopenharmony_ci[verse]
118c2ecf20Sopenharmony_ci'perf script' [-s [Perl]:script[.pl] ]
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ciDESCRIPTION
148c2ecf20Sopenharmony_ci-----------
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciThis perf script option is used to process perf script data using perf's
178c2ecf20Sopenharmony_cibuilt-in Perl interpreter.  It reads and processes the input file and
188c2ecf20Sopenharmony_cidisplays the results of the trace analysis implemented in the given
198c2ecf20Sopenharmony_ciPerl script, if any.
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ciSTARTER SCRIPTS
228c2ecf20Sopenharmony_ci---------------
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciYou can avoid reading the rest of this document by running 'perf script
258c2ecf20Sopenharmony_ci-g perl' in the same directory as an existing perf.data trace file.
268c2ecf20Sopenharmony_ciThat will generate a starter script containing a handler for each of
278c2ecf20Sopenharmony_cithe event types in the trace file; it simply prints every available
288c2ecf20Sopenharmony_cifield for each event in the trace file.
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ciYou can also look at the existing scripts in
318c2ecf20Sopenharmony_ci~/libexec/perf-core/scripts/perl for typical examples showing how to
328c2ecf20Sopenharmony_cido basic things like aggregate event data, print results, etc.  Also,
338c2ecf20Sopenharmony_cithe check-perf-script.pl script, while not interesting for its results,
348c2ecf20Sopenharmony_ciattempts to exercise all of the main scripting features.
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ciEVENT HANDLERS
378c2ecf20Sopenharmony_ci--------------
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ciWhen perf script is invoked using a trace script, a user-defined
408c2ecf20Sopenharmony_ci'handler function' is called for each event in the trace.  If there's
418c2ecf20Sopenharmony_cino handler function defined for a given event type, the event is
428c2ecf20Sopenharmony_ciignored (or passed to a 'trace_unhandled' function, see below) and the
438c2ecf20Sopenharmony_cinext event is processed.
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ciMost of the event's field values are passed as arguments to the
468c2ecf20Sopenharmony_cihandler function; some of the less common ones aren't - those are
478c2ecf20Sopenharmony_ciavailable as calls back into the perf executable (see below).
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciAs an example, the following perf record command can be used to record
508c2ecf20Sopenharmony_ciall sched_wakeup events in the system:
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci # perf record -a -e sched:sched_wakeup
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciTraces meant to be processed using a script should be recorded with
558c2ecf20Sopenharmony_cithe above option: -a to enable system-wide collection.
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ciThe format file for the sched_wakep event defines the following fields
588c2ecf20Sopenharmony_ci(see /sys/kernel/debug/tracing/events/sched/sched_wakeup/format):
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci----
618c2ecf20Sopenharmony_ci format:
628c2ecf20Sopenharmony_ci        field:unsigned short common_type;
638c2ecf20Sopenharmony_ci        field:unsigned char common_flags;
648c2ecf20Sopenharmony_ci        field:unsigned char common_preempt_count;
658c2ecf20Sopenharmony_ci        field:int common_pid;
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci        field:char comm[TASK_COMM_LEN];
688c2ecf20Sopenharmony_ci        field:pid_t pid;
698c2ecf20Sopenharmony_ci        field:int prio;
708c2ecf20Sopenharmony_ci        field:int success;
718c2ecf20Sopenharmony_ci        field:int target_cpu;
728c2ecf20Sopenharmony_ci----
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ciThe handler function for this event would be defined as:
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci----
778c2ecf20Sopenharmony_cisub sched::sched_wakeup
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci   my ($event_name, $context, $common_cpu, $common_secs,
808c2ecf20Sopenharmony_ci       $common_nsecs, $common_pid, $common_comm,
818c2ecf20Sopenharmony_ci       $comm, $pid, $prio, $success, $target_cpu) = @_;
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci----
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ciThe handler function takes the form subsystem::event_name.
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ciThe $common_* arguments in the handler's argument list are the set of
888c2ecf20Sopenharmony_ciarguments passed to all event handlers; some of the fields correspond
898c2ecf20Sopenharmony_cito the common_* fields in the format file, but some are synthesized,
908c2ecf20Sopenharmony_ciand some of the common_* fields aren't common enough to to be passed
918c2ecf20Sopenharmony_cito every event as arguments but are available as library functions.
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ciHere's a brief description of each of the invariant event args:
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci $event_name 	  	    the name of the event as text
968c2ecf20Sopenharmony_ci $context		    an opaque 'cookie' used in calls back into perf
978c2ecf20Sopenharmony_ci $common_cpu		    the cpu the event occurred on
988c2ecf20Sopenharmony_ci $common_secs		    the secs portion of the event timestamp
998c2ecf20Sopenharmony_ci $common_nsecs		    the nsecs portion of the event timestamp
1008c2ecf20Sopenharmony_ci $common_pid		    the pid of the current task
1018c2ecf20Sopenharmony_ci $common_comm		    the name of the current process
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ciAll of the remaining fields in the event's format file have
1048c2ecf20Sopenharmony_cicounterparts as handler function arguments of the same name, as can be
1058c2ecf20Sopenharmony_ciseen in the example above.
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ciThe above provides the basics needed to directly access every field of
1088c2ecf20Sopenharmony_cievery event in a trace, which covers 90% of what you need to know to
1098c2ecf20Sopenharmony_ciwrite a useful trace script.  The sections below cover the rest.
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ciSCRIPT LAYOUT
1128c2ecf20Sopenharmony_ci-------------
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciEvery perf script Perl script should start by setting up a Perl module
1158c2ecf20Sopenharmony_cisearch path and 'use'ing a few support modules (see module
1168c2ecf20Sopenharmony_cidescriptions below):
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci----
1198c2ecf20Sopenharmony_ci use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
1208c2ecf20Sopenharmony_ci use lib "./Perf-Trace-Util/lib";
1218c2ecf20Sopenharmony_ci use Perf::Trace::Core;
1228c2ecf20Sopenharmony_ci use Perf::Trace::Context;
1238c2ecf20Sopenharmony_ci use Perf::Trace::Util;
1248c2ecf20Sopenharmony_ci----
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ciThe rest of the script can contain handler functions and support
1278c2ecf20Sopenharmony_cifunctions in any order.
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciAside from the event handler functions discussed above, every script
1308c2ecf20Sopenharmony_cican implement a set of optional functions:
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci*trace_begin*, if defined, is called before any event is processed and
1338c2ecf20Sopenharmony_cigives scripts a chance to do setup tasks:
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci----
1368c2ecf20Sopenharmony_ci sub trace_begin
1378c2ecf20Sopenharmony_ci {
1388c2ecf20Sopenharmony_ci }
1398c2ecf20Sopenharmony_ci----
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci*trace_end*, if defined, is called after all events have been
1428c2ecf20Sopenharmony_ci processed and gives scripts a chance to do end-of-script tasks, such
1438c2ecf20Sopenharmony_ci as display results:
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci----
1468c2ecf20Sopenharmony_cisub trace_end
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci----
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci*trace_unhandled*, if defined, is called after for any event that
1528c2ecf20Sopenharmony_ci doesn't have a handler explicitly defined for it.  The standard set
1538c2ecf20Sopenharmony_ci of common arguments are passed into it:
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci----
1568c2ecf20Sopenharmony_cisub trace_unhandled
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci    my ($event_name, $context, $common_cpu, $common_secs,
1598c2ecf20Sopenharmony_ci        $common_nsecs, $common_pid, $common_comm) = @_;
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci----
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ciThe remaining sections provide descriptions of each of the available
1648c2ecf20Sopenharmony_cibuilt-in perf script Perl modules and their associated functions.
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ciAVAILABLE MODULES AND FUNCTIONS
1678c2ecf20Sopenharmony_ci-------------------------------
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ciThe following sections describe the functions and variables available
1708c2ecf20Sopenharmony_civia the various Perf::Trace::* Perl modules.  To use the functions and
1718c2ecf20Sopenharmony_civariables from the given module, add the corresponding 'use
1728c2ecf20Sopenharmony_ciPerf::Trace::XXX' line to your perf script script.
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ciPerf::Trace::Core Module
1758c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciThese functions provide some essential functions to user scripts.
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ciThe *flag_str* and *symbol_str* functions provide human-readable
1808c2ecf20Sopenharmony_cistrings for flag and symbolic fields.  These correspond to the strings
1818c2ecf20Sopenharmony_ciand values parsed from the 'print fmt' fields of the event format
1828c2ecf20Sopenharmony_cifiles:
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci  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
1858c2ecf20Sopenharmony_ci  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
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ciPerf::Trace::Context Module
1888c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ciSome of the 'common' fields in the event format file aren't all that
1918c2ecf20Sopenharmony_cicommon, but need to be made accessible to user scripts nonetheless.
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ciPerf::Trace::Context defines a set of functions that can be used to
1948c2ecf20Sopenharmony_ciaccess this data in the context of the current event.  Each of these
1958c2ecf20Sopenharmony_cifunctions expects a $context variable, which is the same as the
1968c2ecf20Sopenharmony_ci$context variable passed into every event handler as the second
1978c2ecf20Sopenharmony_ciargument.
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci common_pc($context) - returns common_preempt count for the current event
2008c2ecf20Sopenharmony_ci common_flags($context) - returns common_flags for the current event
2018c2ecf20Sopenharmony_ci common_lock_depth($context) - returns common_lock_depth for the current event
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ciPerf::Trace::Util Module
2048c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciVarious utility functions for use with perf script:
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci  nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair
2098c2ecf20Sopenharmony_ci  nsecs_secs($nsecs) - returns whole secs portion given nsecs
2108c2ecf20Sopenharmony_ci  nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs
2118c2ecf20Sopenharmony_ci  nsecs_str($nsecs) - returns printable string in the form secs.nsecs
2128c2ecf20Sopenharmony_ci  avg($total, $n) - returns average given a sum and a total number of values
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ciSEE ALSO
2158c2ecf20Sopenharmony_ci--------
2168c2ecf20Sopenharmony_cilinkperf:perf-script[1]
217