When perf script is invoked using a trace script, a user-defined
handler function is called for each event in the trace. If
there's no handler function defined for a given event type, the
event is ignored (or passed to a trace_unhandled function, see
below) and the next event is processed.
Most of the event's field values are passed as arguments to the
handler function; some of the less common ones aren't - those are
available as calls back into the perf executable (see below).
As an example, the following perf record command can be used to
record all sched_wakeup events in the system:
# perf record -a -e sched:sched_wakeup
Traces meant to be processed using a script should be recorded
with the above option: -a to enable system-wide collection.
The format file for the sched_wakep event defines the following
fields (see
/sys/kernel/debug/tracing/events/sched/sched_wakeup/format):
.ft C
format:
field:unsigned short common_type;
field:unsigned char common_flags;
field:unsigned char common_preempt_count;
field:int common_pid;
field:char comm[TASK_COMM_LEN];
field:pid_t pid;
field:int prio;
field:int success;
field:int target_cpu;
.ft
The handler function for this event would be defined as:
.ft C
def sched__sched_wakeup(event_name, context, common_cpu, common_secs,
common_nsecs, common_pid, common_comm,
comm, pid, prio, success, target_cpu):
pass
.ft
The handler function takes the form subsystem__event_name.
The common_* arguments in the handler's argument list are the set
of arguments passed to all event handlers; some of the fields
correspond to the common_* fields in the format file, but some
are synthesized, and some of the common_* fields aren't common
enough to to be passed to every event as arguments but are
available as library functions.
Here's a brief description of each of the invariant event args:
event_name the name of the event as text
context an opaque 'cookie' used in calls back into perf
common_cpu the cpu the event occurred on
common_secs the secs portion of the event timestamp
common_nsecs the nsecs portion of the event timestamp
common_pid the pid of the current task
common_comm the name of the current process
All of the remaining fields in the event's format file have
counterparts as handler function arguments of the same name, as
can be seen in the example above.
The above provides the basics needed to directly access every
field of every event in a trace, which covers 90% of what you
need to know to write a useful trace script. The sections below
cover the rest.