perf-trace-perl.txt 7.3 KB

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