162306a36Sopenharmony_ciperf-script-perl(1) 262306a36Sopenharmony_ci=================== 362306a36Sopenharmony_ci 462306a36Sopenharmony_ciNAME 562306a36Sopenharmony_ci---- 662306a36Sopenharmony_ciperf-script-perl - Process trace data with a Perl script 762306a36Sopenharmony_ci 862306a36Sopenharmony_ciSYNOPSIS 962306a36Sopenharmony_ci-------- 1062306a36Sopenharmony_ci[verse] 1162306a36Sopenharmony_ci'perf script' [-s [Perl]:script[.pl] ] 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ciDESCRIPTION 1462306a36Sopenharmony_ci----------- 1562306a36Sopenharmony_ci 1662306a36Sopenharmony_ciThis perf script option is used to process perf script data using perf's 1762306a36Sopenharmony_cibuilt-in Perl interpreter. It reads and processes the input file and 1862306a36Sopenharmony_cidisplays the results of the trace analysis implemented in the given 1962306a36Sopenharmony_ciPerl script, if any. 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_ciSTARTER SCRIPTS 2262306a36Sopenharmony_ci--------------- 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ciYou can avoid reading the rest of this document by running 'perf script 2562306a36Sopenharmony_ci-g perl' in the same directory as an existing perf.data trace file. 2662306a36Sopenharmony_ciThat will generate a starter script containing a handler for each of 2762306a36Sopenharmony_cithe event types in the trace file; it simply prints every available 2862306a36Sopenharmony_cifield for each event in the trace file. 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ciYou can also look at the existing scripts in 3162306a36Sopenharmony_ci~/libexec/perf-core/scripts/perl for typical examples showing how to 3262306a36Sopenharmony_cido basic things like aggregate event data, print results, etc. Also, 3362306a36Sopenharmony_cithe check-perf-script.pl script, while not interesting for its results, 3462306a36Sopenharmony_ciattempts to exercise all of the main scripting features. 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_ciEVENT HANDLERS 3762306a36Sopenharmony_ci-------------- 3862306a36Sopenharmony_ci 3962306a36Sopenharmony_ciWhen perf script is invoked using a trace script, a user-defined 4062306a36Sopenharmony_ci'handler function' is called for each event in the trace. If there's 4162306a36Sopenharmony_cino handler function defined for a given event type, the event is 4262306a36Sopenharmony_ciignored (or passed to a 'trace_unhandled' function, see below) and the 4362306a36Sopenharmony_cinext event is processed. 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ciMost of the event's field values are passed as arguments to the 4662306a36Sopenharmony_cihandler function; some of the less common ones aren't - those are 4762306a36Sopenharmony_ciavailable as calls back into the perf executable (see below). 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_ciAs an example, the following perf record command can be used to record 5062306a36Sopenharmony_ciall sched_wakeup events in the system: 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci # perf record -a -e sched:sched_wakeup 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ciTraces meant to be processed using a script should be recorded with 5562306a36Sopenharmony_cithe above option: -a to enable system-wide collection. 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_ciThe format file for the sched_wakeup event defines the following fields 5862306a36Sopenharmony_ci(see /sys/kernel/tracing/events/sched/sched_wakeup/format): 5962306a36Sopenharmony_ci 6062306a36Sopenharmony_ci---- 6162306a36Sopenharmony_ci format: 6262306a36Sopenharmony_ci field:unsigned short common_type; 6362306a36Sopenharmony_ci field:unsigned char common_flags; 6462306a36Sopenharmony_ci field:unsigned char common_preempt_count; 6562306a36Sopenharmony_ci field:int common_pid; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_ci field:char comm[TASK_COMM_LEN]; 6862306a36Sopenharmony_ci field:pid_t pid; 6962306a36Sopenharmony_ci field:int prio; 7062306a36Sopenharmony_ci field:int success; 7162306a36Sopenharmony_ci field:int target_cpu; 7262306a36Sopenharmony_ci---- 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_ciThe handler function for this event would be defined as: 7562306a36Sopenharmony_ci 7662306a36Sopenharmony_ci---- 7762306a36Sopenharmony_cisub sched::sched_wakeup 7862306a36Sopenharmony_ci{ 7962306a36Sopenharmony_ci my ($event_name, $context, $common_cpu, $common_secs, 8062306a36Sopenharmony_ci $common_nsecs, $common_pid, $common_comm, 8162306a36Sopenharmony_ci $comm, $pid, $prio, $success, $target_cpu) = @_; 8262306a36Sopenharmony_ci} 8362306a36Sopenharmony_ci---- 8462306a36Sopenharmony_ci 8562306a36Sopenharmony_ciThe handler function takes the form subsystem::event_name. 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ciThe $common_* arguments in the handler's argument list are the set of 8862306a36Sopenharmony_ciarguments passed to all event handlers; some of the fields correspond 8962306a36Sopenharmony_cito the common_* fields in the format file, but some are synthesized, 9062306a36Sopenharmony_ciand some of the common_* fields aren't common enough to to be passed 9162306a36Sopenharmony_cito every event as arguments but are available as library functions. 9262306a36Sopenharmony_ci 9362306a36Sopenharmony_ciHere's a brief description of each of the invariant event args: 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci $event_name the name of the event as text 9662306a36Sopenharmony_ci $context an opaque 'cookie' used in calls back into perf 9762306a36Sopenharmony_ci $common_cpu the cpu the event occurred on 9862306a36Sopenharmony_ci $common_secs the secs portion of the event timestamp 9962306a36Sopenharmony_ci $common_nsecs the nsecs portion of the event timestamp 10062306a36Sopenharmony_ci $common_pid the pid of the current task 10162306a36Sopenharmony_ci $common_comm the name of the current process 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ciAll of the remaining fields in the event's format file have 10462306a36Sopenharmony_cicounterparts as handler function arguments of the same name, as can be 10562306a36Sopenharmony_ciseen in the example above. 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ciThe above provides the basics needed to directly access every field of 10862306a36Sopenharmony_cievery event in a trace, which covers 90% of what you need to know to 10962306a36Sopenharmony_ciwrite a useful trace script. The sections below cover the rest. 11062306a36Sopenharmony_ci 11162306a36Sopenharmony_ciSCRIPT LAYOUT 11262306a36Sopenharmony_ci------------- 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_ciEvery perf script Perl script should start by setting up a Perl module 11562306a36Sopenharmony_cisearch path and 'use'ing a few support modules (see module 11662306a36Sopenharmony_cidescriptions below): 11762306a36Sopenharmony_ci 11862306a36Sopenharmony_ci---- 11962306a36Sopenharmony_ci use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib"; 12062306a36Sopenharmony_ci use lib "./Perf-Trace-Util/lib"; 12162306a36Sopenharmony_ci use Perf::Trace::Core; 12262306a36Sopenharmony_ci use Perf::Trace::Context; 12362306a36Sopenharmony_ci use Perf::Trace::Util; 12462306a36Sopenharmony_ci---- 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_ciThe rest of the script can contain handler functions and support 12762306a36Sopenharmony_cifunctions in any order. 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ciAside from the event handler functions discussed above, every script 13062306a36Sopenharmony_cican implement a set of optional functions: 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci*trace_begin*, if defined, is called before any event is processed and 13362306a36Sopenharmony_cigives scripts a chance to do setup tasks: 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ci---- 13662306a36Sopenharmony_ci sub trace_begin 13762306a36Sopenharmony_ci { 13862306a36Sopenharmony_ci } 13962306a36Sopenharmony_ci---- 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci*trace_end*, if defined, is called after all events have been 14262306a36Sopenharmony_ci processed and gives scripts a chance to do end-of-script tasks, such 14362306a36Sopenharmony_ci as display results: 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ci---- 14662306a36Sopenharmony_cisub trace_end 14762306a36Sopenharmony_ci{ 14862306a36Sopenharmony_ci} 14962306a36Sopenharmony_ci---- 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci*trace_unhandled*, if defined, is called after for any event that 15262306a36Sopenharmony_ci doesn't have a handler explicitly defined for it. The standard set 15362306a36Sopenharmony_ci of common arguments are passed into it: 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci---- 15662306a36Sopenharmony_cisub trace_unhandled 15762306a36Sopenharmony_ci{ 15862306a36Sopenharmony_ci my ($event_name, $context, $common_cpu, $common_secs, 15962306a36Sopenharmony_ci $common_nsecs, $common_pid, $common_comm) = @_; 16062306a36Sopenharmony_ci} 16162306a36Sopenharmony_ci---- 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ciThe remaining sections provide descriptions of each of the available 16462306a36Sopenharmony_cibuilt-in perf script Perl modules and their associated functions. 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ciAVAILABLE MODULES AND FUNCTIONS 16762306a36Sopenharmony_ci------------------------------- 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ciThe following sections describe the functions and variables available 17062306a36Sopenharmony_civia the various Perf::Trace::* Perl modules. To use the functions and 17162306a36Sopenharmony_civariables from the given module, add the corresponding 'use 17262306a36Sopenharmony_ciPerf::Trace::XXX' line to your perf script script. 17362306a36Sopenharmony_ci 17462306a36Sopenharmony_ciPerf::Trace::Core Module 17562306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~ 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ciThese functions provide some essential functions to user scripts. 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_ciThe *flag_str* and *symbol_str* functions provide human-readable 18062306a36Sopenharmony_cistrings for flag and symbolic fields. These correspond to the strings 18162306a36Sopenharmony_ciand values parsed from the 'print fmt' fields of the event format 18262306a36Sopenharmony_cifiles: 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_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 18562306a36Sopenharmony_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 18662306a36Sopenharmony_ci 18762306a36Sopenharmony_ciPerf::Trace::Context Module 18862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~ 18962306a36Sopenharmony_ci 19062306a36Sopenharmony_ciSome of the 'common' fields in the event format file aren't all that 19162306a36Sopenharmony_cicommon, but need to be made accessible to user scripts nonetheless. 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ciPerf::Trace::Context defines a set of functions that can be used to 19462306a36Sopenharmony_ciaccess this data in the context of the current event. Each of these 19562306a36Sopenharmony_cifunctions expects a $context variable, which is the same as the 19662306a36Sopenharmony_ci$context variable passed into every event handler as the second 19762306a36Sopenharmony_ciargument. 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci common_pc($context) - returns common_preempt count for the current event 20062306a36Sopenharmony_ci common_flags($context) - returns common_flags for the current event 20162306a36Sopenharmony_ci common_lock_depth($context) - returns common_lock_depth for the current event 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ciPerf::Trace::Util Module 20462306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~ 20562306a36Sopenharmony_ci 20662306a36Sopenharmony_ciVarious utility functions for use with perf script: 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair 20962306a36Sopenharmony_ci nsecs_secs($nsecs) - returns whole secs portion given nsecs 21062306a36Sopenharmony_ci nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs 21162306a36Sopenharmony_ci nsecs_str($nsecs) - returns printable string in the form secs.nsecs 21262306a36Sopenharmony_ci avg($total, $n) - returns average given a sum and a total number of values 21362306a36Sopenharmony_ci 21462306a36Sopenharmony_ciSEE ALSO 21562306a36Sopenharmony_ci-------- 21662306a36Sopenharmony_cilinkperf:perf-script[1] 217