Commit 23ba90e3 authored by Ingo Molnar's avatar Ingo Molnar
Browse files

Merge branch 'perf/scripting' into perf/core

Merge reason: it's ready for v2.6.33.
Signed-off-by: default avatarIngo Molnar <>
parents e859cf86 8ea339ad
perf-trace-perl - Process trace data with a Perl script
'perf trace' [-s [lang]:script[.ext] ]
This perf trace option is used to process perf trace data using perf's
built-in Perl interpreter. It reads and processes the input file and
displays the results of the trace analysis implemented in the given
Perl script, if any.
You can avoid reading the rest of this document by running 'perf trace
-g perl' in the same directory as an existing trace file.
That will generate a starter script containing a handler for each of
the event types in the trace file; it simply prints every available
field for each event in the trace file.
You can also look at the existing scripts in
~/libexec/perf-core/scripts/perl for typical examples showing how to
do basic things like aggregate event data, print results, etc. Also,
the script, while not interesting for its results,
attempts to exercise all of the main scripting features.
When perf trace 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_handled' 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 -c 1 -f -a -M -R -e sched:sched_wakeup
Traces meant to be processed using a script should be recorded with
the above options: -c 1 says to sample every event, -a to enable
system-wide collection, -M to multiplex the output, and -R to collect
raw samples.
The format file for the sched_wakep event defines the following fields
(see /sys/kernel/debug/tracing/events/sched/sched_wakeup/format):
field:unsigned short common_type;
field:unsigned char common_flags;
field:unsigned char common_preempt_count;
field:int common_pid;
field:int common_lock_depth;
field:char comm[TASK_COMM_LEN];
field:pid_t pid;
field:int prio;
field:int success;
field:int target_cpu;
The handler function for this event would be defined as:
sub sched::sched_wakeup
my ($event_name, $context, $common_cpu, $common_secs,
$common_nsecs, $common_pid, $common_comm,
$comm, $pid, $prio, $success, $target_cpu) = @_;
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.
Every perf trace Perl script should start by setting up a Perl module
search path and 'use'ing a few support modules (see module
descriptions below):
use lib "$ENV{'PERF_EXEC_PATH'}/scripts/perl/Perf-Trace-Util/lib";
use lib "./Perf-Trace-Util/lib";
use Perf::Trace::Core;
use Perf::Trace::Context;
use Perf::Trace::Util;
The rest of the script can contain handler functions and support
functions in any order.
Aside from the event handler functions discussed above, every script
can implement a set of optional functions:
*trace_begin*, if defined, is called before any event is processed and
gives scripts a chance to do setup tasks:
sub trace_begin
*trace_end*, if defined, is called after all events have been
processed and gives scripts a chance to do end-of-script tasks, such
as display results:
sub trace_end
*trace_unhandled*, if defined, is called after for any event that
doesn't have a handler explicitly defined for it. The standard set
of common arguments are passed into it:
sub trace_unhandled
my ($event_name, $context, $common_cpu, $common_secs,
$common_nsecs, $common_pid, $common_comm) = @_;
The remaining sections provide descriptions of each of the available
built-in perf trace Perl modules and their associated functions.
The following sections describe the functions and variables available
via the various Perf::Trace::* Perl modules. To use the functions and
variables from the given module, add the corresponding 'use
Perf::Trace::XXX' line to your perf trace script.
Perf::Trace::Core Module
These functions provide some essential functions to user scripts.
The *flag_str* and *symbol_str* functions provide human-readable
strings for flag and symbolic fields. These correspond to the strings
and values parsed from the 'print fmt' fields of the event format
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
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
Perf::Trace::Context Module
Some of the 'common' fields in the event format file aren't all that
common, but need to be made accessible to user scripts nonetheless.
Perf::Trace::Context defines a set of functions that can be used to
access this data in the context of the current event. Each of these
functions expects a $context variable, which is the same as the
$context variable passed into every event handler as the second
common_pc($context) - returns common_preempt count for the current event
common_flags($context) - returns common_flags for the current event
common_lock_depth($context) - returns common_lock_depth for the current event
Perf::Trace::Util Module
Various utility functions for use with perf trace:
nsecs($secs, $nsecs) - returns total nsecs given secs/nsecs pair
nsecs_secs($nsecs) - returns whole secs portion given nsecs
nsecs_nsecs($nsecs) - returns nsecs remainder given nsecs
nsecs_str($nsecs) - returns printable string in the form secs.nsecs
avg($total, $n) - returns average given a sum and a total number of values
......@@ -20,6 +20,15 @@ OPTIONS
Display verbose dump of the trace data.
Process trace data with the given script ([lang]:script[.ext]).
Generate perf-trace.[ext] starter script for given language,
using current
linkperf:perf-record[1], linkperf:perf-trace-perl[1]
......@@ -409,6 +409,7 @@ LIB_OBJS += util/thread.o
LIB_OBJS += util/trace-event-parse.o
LIB_OBJS += util/trace-event-read.o
LIB_OBJS += util/trace-event-info.o
LIB_OBJS += util/trace-event-perl.o
LIB_OBJS += util/svghelper.o
LIB_OBJS += util/sort.o
LIB_OBJS += util/hist.o
......@@ -491,6 +492,16 @@ else
LIB_OBJS += util/probe-finder.o
PERL_EMBED_LDOPTS = `perl -MExtUtils::Embed -e ldopts 2>/dev/null`
PERL_EMBED_CCOPTS = `perl -MExtUtils::Embed -e ccopts 2>/dev/null`
ifneq ($(shell sh -c "(echo '\#include <EXTERN.h>'; echo '\#include <perl.h>'; echo 'int main(void) { perl_alloc(); return 0; }') | $(CC) -x c - $(PERL_EMBED_CCOPTS) -o /dev/null $(PERL_EMBED_LDOPTS) > /dev/null 2>&1 && echo y"), y)
LIB_OBJS += scripts/perl/Perf-Trace-Util/Context.o
......@@ -862,6 +873,12 @@ util/hweight.o: ../../lib/hweight.c PERF-CFLAGS
util/find_next_bit.o: ../../lib/find_next_bit.c PERF-CFLAGS
$(QUIET_CC)$(CC) -o util/find_next_bit.o -c $(ALL_CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
util/trace-event-perl.o: util/trace-event-perl.c PERF-CFLAGS
$(QUIET_CC)$(CC) -o util/trace-event-perl.o -c $(ALL_CFLAGS) $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow $<
scripts/perl/Perf-Trace-Util/Context.o: scripts/perl/Perf-Trace-Util/Context.c PERF-CFLAGS
$(QUIET_CC)$(CC) -o scripts/perl/Perf-Trace-Util/Context.o -c $(ALL_CFLAGS) $(PERL_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-nested-externs $<
perf-%$X: %.o $(PERFLIBS)
$(QUIET_LINK)$(CC) $(ALL_CFLAGS) -o $@ $(ALL_LDFLAGS) $(filter %.o,$^) $(LIBS)
......@@ -969,6 +986,13 @@ export perfexec_instdir
install: all
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(bindir_SQ)'
$(INSTALL) perf$X '$(DESTDIR_SQ)$(bindir_SQ)'
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util/lib/Perf/Trace'
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/bin'
$(INSTALL) scripts/perl/Perf-Trace-Util/lib/Perf/Trace/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util/lib/Perf/Trace'
$(INSTALL) scripts/perl/*.pl -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl'
$(INSTALL) scripts/perl/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/bin'
$(INSTALL) scripts/perl/Perf-Trace-Util/Makefile.PL -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util'
$(INSTALL) scripts/perl/Perf-Trace-Util/README -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/perl/Perf-Trace-Util'
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)'
$(INSTALL) $(BUILT_INS) '$(DESTDIR_SQ)$(perfexec_instdir_SQ)'
......@@ -1054,7 +1078,7 @@ distclean: clean
# $(RM) configure
$(RM) *.o */*.o $(LIB_FILE)
$(RM) *.o */*.o */*/*.o */*/*/*.o $(LIB_FILE)
$(RM) *.spec *.pyc *.pyo */*.pyc */*.pyo common-cmds.h TAGS tags cscope*
......@@ -5,6 +5,50 @@
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"
#include "util/exec_cmd.h"
#include "util/trace-event.h"
static char const *script_name;
static char const *generate_script_lang;
static int default_start_script(const char *script __attribute((unused)))
return 0;
static int default_stop_script(void)
return 0;
static int default_generate_script(const char *outfile __attribute ((unused)))
return 0;
static struct scripting_ops default_scripting_ops = {
.start_script = default_start_script,
.stop_script = default_stop_script,
.process_event = print_event,
.generate_script = default_generate_script,
static struct scripting_ops *scripting_ops;
static void setup_scripting(void)
/* make sure PERF_EXEC_PATH is set for scripts */
scripting_ops = &default_scripting_ops;
static int cleanup_scripting(void)
return scripting_ops->stop_script();
#include "util/parse-options.h"
......@@ -13,11 +57,12 @@
#include "util/trace-event.h"
#include "util/data_map.h"
#include "util/exec_cmd.h"
static char const *input_name = "";
static char const *input_name = "";
static struct perf_header *header;
static u64 sample_type;
static struct perf_header *header;
static u64 sample_type;
static int process_sample_event(event_t *event)
......@@ -69,7 +114,8 @@ static int process_sample_event(event_t *event)
* field, although it should be the same than this perf
* event pid
print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
scripting_ops->process_event(cpu, raw->data, raw->size,
timestamp, thread->comm);
} += period;
......@@ -105,6 +151,154 @@ static int __cmd_trace(void)
0, 0, &event__cwdlen, &event__cwd);
struct script_spec {
struct list_head node;
struct scripting_ops *ops;
char spec[0];
static struct script_spec *script_spec__new(const char *spec,
struct scripting_ops *ops)
struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
if (s != NULL) {
strcpy(s->spec, spec);
s->ops = ops;
return s;
static void script_spec__delete(struct script_spec *s)
static void script_spec__add(struct script_spec *s)
list_add_tail(&s->node, &script_specs);
static struct script_spec *script_spec__find(const char *spec)
struct script_spec *s;
list_for_each_entry(s, &script_specs, node)
if (strcasecmp(s->spec, spec) == 0)
return s;
return NULL;
static struct script_spec *script_spec__findnew(const char *spec,
struct scripting_ops *ops)
struct script_spec *s = script_spec__find(spec);
if (s)
return s;
s = script_spec__new(spec, ops);
if (!s)
goto out_delete_spec;
return s;
return NULL;
int script_spec_register(const char *spec, struct scripting_ops *ops)
struct script_spec *s;
s = script_spec__find(spec);
if (s)
return -1;
s = script_spec__findnew(spec, ops);
if (!s)
return -1;
return 0;
static struct scripting_ops *script_spec__lookup(const char *spec)
struct script_spec *s = script_spec__find(spec);
if (!s)
return NULL;
return s->ops;
static void list_available_languages(void)
struct script_spec *s;
fprintf(stderr, "\n");
fprintf(stderr, "Scripting language extensions (used in "
"perf trace -s [spec:]script.[spec]):\n\n");
list_for_each_entry(s, &script_specs, node)
fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
fprintf(stderr, "\n");
static int parse_scriptname(const struct option *opt __used,
const char *str, int unset __used)
char spec[PATH_MAX];
const char *script, *ext;
int len;
if (strcmp(str, "list") == 0) {
return 0;
script = strchr(str, ':');
if (script) {
len = script - str;
if (len >= PATH_MAX) {
fprintf(stderr, "invalid language specifier");
return -1;
strncpy(spec, str, len);
spec[len] = '\0';
scripting_ops = script_spec__lookup(spec);
if (!scripting_ops) {
fprintf(stderr, "invalid language specifier");
return -1;
} else {
script = str;
ext = strchr(script, '.');
if (!ext) {
fprintf(stderr, "invalid script extension");
return -1;
scripting_ops = script_spec__lookup(++ext);
if (!scripting_ops) {
fprintf(stderr, "invalid script extension");
return -1;
script_name = strdup(script);
return 0;
static const char * const annotate_usage[] = {
"perf trace [<options>] <command>",
......@@ -117,13 +311,23 @@ static const struct option options[] = {
"be more verbose (show symbol address, etc)"),
OPT_BOOLEAN('l', "latency", &latency_format,
"show latency attributes (irqs/preemption disabled, etc)"),
OPT_CALLBACK('s', "script", NULL, "name",
"script file name (lang:script name, script name, or *)",
OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
"generate perf-trace.xx script in specified language"),
int cmd_trace(int argc, const char **argv, const char *prefix __used)
int err;
argc = parse_options(argc, argv, options, annotate_usage, 0);
if (argc) {
......@@ -136,5 +340,50 @@ int cmd_trace(int argc, const char **argv, const char *prefix __used)
return __cmd_trace();
if (generate_script_lang) {
struct stat perf_stat;
int input = open(input_name, O_RDONLY);
if (input < 0) {
perror("failed to open file");
err = fstat(input, &perf_stat);
if (err < 0) {
perror("failed to stat file");
if (!perf_stat.st_size) {
fprintf(stderr, "zero-sized file, nothing to do!\n");
scripting_ops = script_spec__lookup(generate_script_lang);
if (!scripting_ops) {
fprintf(stderr, "invalid language specifier");
return -1;
header = perf_header__new();
if (header == NULL)
return -1;
perf_header__read(header, input);
err = scripting_ops->generate_script("perf-trace");
goto out;
if (script_name) {
err = scripting_ops->start_script(script_name);
if (err)
goto out;
err = __cmd_trace();
return err;
* This file was generated automatically by ExtUtils::ParseXS version 2.18_02 from the
* contents of Context.xs. Do not edit this file, edit Context.xs instead.
#line 1 "Context.xs"
* Context.xs. XS interfaces for perf trace.
* Copyright (C) 2009 Tom Zanussi <>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA