diff options
Diffstat (limited to 'tools/perf/Documentation/perf-script-perl.txt')
| -rw-r--r-- | tools/perf/Documentation/perf-script-perl.txt | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/tools/perf/Documentation/perf-script-perl.txt b/tools/perf/Documentation/perf-script-perl.txt new file mode 100644 index 000000000000..5bb41e55a3ac --- /dev/null +++ b/tools/perf/Documentation/perf-script-perl.txt | |||
| @@ -0,0 +1,217 @@ | |||
| 1 | perf-script-perl(1) | ||
| 2 | ================== | ||
| 3 | |||
| 4 | NAME | ||
| 5 | ---- | ||
| 6 | perf-script-perl - Process trace data with a Perl script | ||
| 7 | |||
| 8 | SYNOPSIS | ||
| 9 | -------- | ||
| 10 | [verse] | ||
| 11 | 'perf script' [-s [Perl]:script[.pl] ] | ||
| 12 | |||
| 13 | DESCRIPTION | ||
| 14 | ----------- | ||
| 15 | |||
| 16 | This perf script option is used to process perf script data using perf's | ||
| 17 | built-in Perl interpreter. It reads and processes the input file and | ||
| 18 | displays the results of the trace analysis implemented in the given | ||
| 19 | Perl script, if any. | ||
| 20 | |||
| 21 | STARTER SCRIPTS | ||
| 22 | --------------- | ||
| 23 | |||
| 24 | You can avoid reading the rest of this document by running 'perf script | ||
| 25 | -g perl' in the same directory as an existing perf.data trace file. | ||
| 26 | That will generate a starter script containing a handler for each of | ||
| 27 | the event types in the trace file; it simply prints every available | ||
| 28 | field for each event in the trace file. | ||
| 29 | |||
| 30 | You can also look at the existing scripts in | ||
| 31 | ~/libexec/perf-core/scripts/perl for typical examples showing how to | ||
| 32 | 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 | attempts to exercise all of the main scripting features. | ||
| 35 | |||
| 36 | EVENT HANDLERS | ||
| 37 | -------------- | ||
| 38 | |||
| 39 | When perf script is invoked using a trace script, a user-defined | ||
| 40 | 'handler function' is called for each event in the trace. If there's | ||
| 41 | no handler function defined for a given event type, the event is | ||
| 42 | ignored (or passed to a 'trace_handled' function, see below) and the | ||
| 43 | next event is processed. | ||
| 44 | |||
| 45 | Most of the event's field values are passed as arguments to the | ||
| 46 | handler function; some of the less common ones aren't - those are | ||
| 47 | available as calls back into the perf executable (see below). | ||
| 48 | |||
| 49 | As an example, the following perf record command can be used to record | ||
| 50 | all sched_wakeup events in the system: | ||
| 51 | |||
| 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 | The format file for the sched_wakep event defines the following fields | ||
| 58 | (see /sys/kernel/debug/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 | field:int common_lock_depth; | ||
| 67 | |||
| 68 | field:char comm[TASK_COMM_LEN]; | ||
| 69 | field:pid_t pid; | ||
| 70 | field:int prio; | ||
| 71 | field:int success; | ||
| 72 | field:int target_cpu; | ||
| 73 | ---- | ||
| 74 | |||
| 75 | The handler function for this event would be defined as: | ||
| 76 | |||
| 77 | ---- | ||
| 78 | sub sched::sched_wakeup | ||
| 79 | { | ||
| 80 | my ($event_name, $context, $common_cpu, $common_secs, | ||
| 81 | $common_nsecs, $common_pid, $common_comm, | ||
| 82 | $comm, $pid, $prio, $success, $target_cpu) = @_; | ||
| 83 | } | ||
| 84 | ---- | ||
| 85 | |||
| 86 | The handler function takes the form subsystem::event_name. | ||
| 87 | |||
| 88 | The $common_* arguments in the handler's argument list are the set of | ||
| 89 | arguments passed to all event handlers; some of the fields correspond | ||
| 90 | to the common_* fields in the format file, but some are synthesized, | ||
| 91 | and some of the common_* fields aren't common enough to to be passed | ||
| 92 | to every event as arguments but are available as library functions. | ||
| 93 | |||
| 94 | Here's a brief description of each of the invariant event args: | ||
| 95 | |||
| 96 | $event_name the name of the event as text | ||
| 97 | $context an opaque 'cookie' used in calls back into perf | ||
| 98 | $common_cpu the cpu the event occurred on | ||
| 99 | $common_secs the secs portion of the event timestamp | ||
| 100 | $common_nsecs the nsecs portion of the event timestamp | ||
| 101 | $common_pid the pid of the current task | ||
| 102 | $common_comm the name of the current process | ||
| 103 | |||
| 104 | All of the remaining fields in the event's format file have | ||
| 105 | counterparts as handler function arguments of the same name, as can be | ||
| 106 | seen in the example above. | ||
| 107 | |||
| 108 | The above provides the basics needed to directly access every field of | ||
| 109 | every event in a trace, which covers 90% of what you need to know to | ||
| 110 | write a useful trace script. The sections below cover the rest. | ||
| 111 | |||
| 112 | SCRIPT LAYOUT | ||
| 113 | ------------- | ||
| 114 | |||
| 115 | Every perf script Perl script should start by setting up a Perl module | ||
| 116 | search path and 'use'ing a few support modules (see module | ||
| 117 | descriptions below): | ||
| 118 | |||
| 119 | ---- | ||
| 120 | use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/perf-script-Util/lib"; | ||
| 121 | use lib "./perf-script-Util/lib"; | ||
| 122 | use Perf::Trace::Core; | ||
| 123 | use Perf::Trace::Context; | ||
| 124 | use Perf::Trace::Util; | ||
| 125 | ---- | ||
| 126 | |||
| 127 | The rest of the script can contain handler functions and support | ||
| 128 | functions in any order. | ||
| 129 | |||
| 130 | Aside from the event handler functions discussed above, every script | ||
| 131 | can implement a set of optional functions: | ||
| 132 | |||
| 133 | *trace_begin*, if defined, is called before any event is processed and | ||
| 134 | gives scripts a chance to do setup tasks: | ||
| 135 | |||
| 136 | ---- | ||
| 137 | sub trace_begin | ||
| 138 | { | ||
| 139 | } | ||
| 140 | ---- | ||
| 141 | |||
| 142 | *trace_end*, if defined, is called after all events have been | ||
| 143 | processed and gives scripts a chance to do end-of-script tasks, such | ||
| 144 | as display results: | ||
| 145 | |||
| 146 | ---- | ||
| 147 | sub trace_end | ||
| 148 | { | ||
| 149 | } | ||
| 150 | ---- | ||
| 151 | |||
| 152 | *trace_unhandled*, if defined, is called after for any event that | ||
| 153 | doesn't have a handler explicitly defined for it. The standard set | ||
| 154 | of common arguments are passed into it: | ||
| 155 | |||
| 156 | ---- | ||
| 157 | sub trace_unhandled | ||
| 158 | { | ||
| 159 | my ($event_name, $context, $common_cpu, $common_secs, | ||
| 160 | $common_nsecs, $common_pid, $common_comm) = @_; | ||
| 161 | } | ||
| 162 | ---- | ||
| 163 | |||
| 164 | The remaining sections provide descriptions of each of the available | ||
| 165 | built-in perf script Perl modules and their associated functions. | ||
| 166 | |||
| 167 | AVAILABLE MODULES AND FUNCTIONS | ||
| 168 | ------------------------------- | ||
| 169 | |||
| 170 | The following sections describe the functions and variables available | ||
| 171 | via the various Perf::Trace::* Perl modules. To use the functions and | ||
| 172 | variables from the given module, add the corresponding 'use | ||
| 173 | Perf::Trace::XXX' line to your perf script script. | ||
| 174 | |||
| 175 | Perf::Trace::Core Module | ||
| 176 | ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 177 | |||
| 178 | These functions provide some essential functions to user scripts. | ||
| 179 | |||
| 180 | The *flag_str* and *symbol_str* functions provide human-readable | ||
| 181 | strings for flag and symbolic fields. These correspond to the strings | ||
| 182 | and values parsed from the 'print fmt' fields of the event format | ||
| 183 | files: | ||
| 184 | |||
| 185 | 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 | ||
| 186 | 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 | ||
| 187 | |||
| 188 | Perf::Trace::Context Module | ||
| 189 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 190 | |||
| 191 | Some of the 'common' fields in the event format file aren't all that | ||
| 192 | common, but need to be made accessible to user scripts nonetheless. | ||
| 193 | |||
| 194 | Perf::Trace::Context defines a set of functions that can be used to | ||
| 195 | access this data in the context of the current event. Each of these | ||
| 196 | functions expects a $context variable, which is the same as the | ||
| 197 | $context variable passed into every event handler as the second | ||
| 198 | argument. | ||
| 199 | |||
| 200 | common_pc($context) - returns common_preempt count for the current event | ||
| 201 | common_flags($context) - returns common_flags for the current event | ||
| 202 | common_lock_depth($context) - returns common_lock_depth for the current event | ||
| 203 | |||
| 204 | Perf::Trace::Util Module | ||
| 205 | ~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 206 | |||
| 207 | Various utility functions for use with perf script: | ||
| 208 | |||
| 209 | nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair | ||
| 210 | nsecs_secs($nsecs) - returns whole secs portion given nsecs | ||
| 211 | nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs | ||
| 212 | nsecs_str($nsecs) - returns printable string in the form secs.nsecs | ||
| 213 | avg($total, $n) - returns average given a sum and a total number of values | ||
| 214 | |||
| 215 | SEE ALSO | ||
| 216 | -------- | ||
| 217 | linkperf:perf-script[1] | ||
