Skip to content

Commit

Permalink
perf/scripts: Add Python scripting engine
Browse files Browse the repository at this point in the history
Add base support for Python scripting to perf trace.

Signed-off-by: Tom Zanussi <tzanussi@gmail.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Keiichi KII <k-keiichi@bx.jp.nec.com>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
LKML-Reference: <1264580883-15324-6-git-send-email-tzanussi@gmail.com>
Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com>
  • Loading branch information
Tom Zanussi authored and Frederic Weisbecker committed Feb 25, 2010
1 parent 266fe2f commit 7e4b21b
Show file tree
Hide file tree
Showing 8 changed files with 864 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tools/perf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,19 @@ else
LIB_OBJS += scripts/perl/Perf-Trace-Util/Context.o
endif

ifndef NO_LIBPYTHON
PYTHON_EMBED_LDOPTS = `python-config --ldflags 2>/dev/null`
PYTHON_EMBED_CCOPTS = `python-config --cflags 2>/dev/null`
endif

ifneq ($(shell sh -c "(echo '\#include <Python.h>'; echo 'int main(void) { Py_Initialize(); return 0; }') | $(CC) -x c - $(PYTHON_EMBED_CCOPTS) -o /dev/null $(PYTHON_EMBED_LDOPTS) > /dev/null 2>&1 && echo y"), y)
BASIC_CFLAGS += -DNO_LIBPYTHON
else
ALL_LDFLAGS += $(PYTHON_EMBED_LDOPTS)
LIB_OBJS += util/scripting-engines/trace-event-python.o
LIB_OBJS += scripts/python/Perf-Trace-Util/Context.o
endif

ifdef NO_DEMANGLE
BASIC_CFLAGS += -DNO_DEMANGLE
else
Expand Down Expand Up @@ -899,6 +912,12 @@ util/scripting-engines/trace-event-perl.o: util/scripting-engines/trace-event-pe
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 $<

util/scripting-engines/trace-event-python.o: util/scripting-engines/trace-event-python.c PERF-CFLAGS
$(QUIET_CC)$(CC) -o util/scripting-engines/trace-event-python.o -c $(ALL_CFLAGS) $(PYTHON_EMBED_CCOPTS) -Wno-redundant-decls -Wno-strict-prototypes -Wno-unused-parameter -Wno-shadow $<

scripts/python/Perf-Trace-Util/Context.o: scripts/python/Perf-Trace-Util/Context.c PERF-CFLAGS
$(QUIET_CC)$(CC) -o scripts/python/Perf-Trace-Util/Context.o -c $(ALL_CFLAGS) $(PYTHON_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)

Expand Down Expand Up @@ -1012,6 +1031,8 @@ install: all
$(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) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/Perf-Trace-Util/lib/Perf/Trace'
$(INSTALL) scripts/python/Perf-Trace-Util/lib/Perf/Trace/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/Perf-Trace-Util/lib/Perf/Trace'

ifdef BUILT_INS
$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)'
Expand Down
1 change: 1 addition & 0 deletions tools/perf/builtin-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static void setup_scripting(void)
perf_set_argv_exec_path(perf_exec_path());

setup_perl_scripting();
setup_python_scripting();

scripting_ops = &default_scripting_ops;
}
Expand Down
88 changes: 88 additions & 0 deletions tools/perf/scripts/python/Perf-Trace-Util/Context.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Context.c. Python interfaces for perf trace.
*
* Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
*
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* 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
*
*/

#include <Python.h>
#include "../../../perf.h"
#include "../../../util/trace-event.h"

PyMODINIT_FUNC initperf_trace_context(void);

static PyObject *perf_trace_context_common_pc(PyObject *self, PyObject *args)
{
static struct scripting_context *scripting_context;
PyObject *context;
int retval;

if (!PyArg_ParseTuple(args, "O", &context))
return NULL;

scripting_context = PyCObject_AsVoidPtr(context);
retval = common_pc(scripting_context);

return Py_BuildValue("i", retval);
}

static PyObject *perf_trace_context_common_flags(PyObject *self,
PyObject *args)
{
static struct scripting_context *scripting_context;
PyObject *context;
int retval;

if (!PyArg_ParseTuple(args, "O", &context))
return NULL;

scripting_context = PyCObject_AsVoidPtr(context);
retval = common_flags(scripting_context);

return Py_BuildValue("i", retval);
}

static PyObject *perf_trace_context_common_lock_depth(PyObject *self,
PyObject *args)
{
static struct scripting_context *scripting_context;
PyObject *context;
int retval;

if (!PyArg_ParseTuple(args, "O", &context))
return NULL;

scripting_context = PyCObject_AsVoidPtr(context);
retval = common_lock_depth(scripting_context);

return Py_BuildValue("i", retval);
}

static PyMethodDef ContextMethods[] = {
{ "common_pc", perf_trace_context_common_pc, METH_VARARGS,
"Get the common preempt count event field value."},
{ "common_flags", perf_trace_context_common_flags, METH_VARARGS,
"Get the common flags event field value."},
{ "common_lock_depth", perf_trace_context_common_lock_depth,
METH_VARARGS, "Get the common lock depth event field value."},
{ NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initperf_trace_context(void)
{
(void) Py_InitModule("perf_trace_context", ContextMethods);
}
91 changes: 91 additions & 0 deletions tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Core.py - Python extension for perf trace, core functions
#
# Copyright (C) 2010 by Tom Zanussi <tzanussi@gmail.com>
#
# This software may be distributed under the terms of the GNU General
# Public License ("GPL") version 2 as published by the Free Software
# Foundation.

from collections import defaultdict

def autodict():
return defaultdict(autodict)

flag_fields = autodict()
symbolic_fields = autodict()

def define_flag_field(event_name, field_name, delim):
flag_fields[event_name][field_name]['delim'] = delim

def define_flag_value(event_name, field_name, value, field_str):
flag_fields[event_name][field_name]['values'][value] = field_str

def define_symbolic_field(event_name, field_name):
# nothing to do, really
pass

def define_symbolic_value(event_name, field_name, value, field_str):
symbolic_fields[event_name][field_name]['values'][value] = field_str

def flag_str(event_name, field_name, value):
string = ""

if flag_fields[event_name][field_name]:
print_delim = 0
keys = flag_fields[event_name][field_name]['values'].keys()
keys.sort()
for idx in keys:
if not value and not idx:
string += flag_fields[event_name][field_name]['values'][idx]
break
if idx and (value & idx) == idx:
if print_delim and flag_fields[event_name][field_name]['delim']:
string += " " + flag_fields[event_name][field_name]['delim'] + " "
string += flag_fields[event_name][field_name]['values'][idx]
print_delim = 1
value &= ~idx

return string

def symbol_str(event_name, field_name, value):
string = ""

if symbolic_fields[event_name][field_name]:
keys = symbolic_fields[event_name][field_name]['values'].keys()
keys.sort()
for idx in keys:
if not value and not idx:
string = symbolic_fields[event_name][field_name]['values'][idx]
break
if (value == idx):
string = symbolic_fields[event_name][field_name]['values'][idx]
break

return string

trace_flags = { 0x00: "NONE", \
0x01: "IRQS_OFF", \
0x02: "IRQS_NOSUPPORT", \
0x04: "NEED_RESCHED", \
0x08: "HARDIRQ", \
0x10: "SOFTIRQ" }

def trace_flag_str(value):
string = ""
print_delim = 0

keys = trace_flags.keys()

for idx in keys:
if not value and not idx:
string += "NONE"
break

if idx and (value & idx) == idx:
if print_delim:
string += " | ";
string += trace_flags[idx]
print_delim = 1
value &= ~idx

return string
25 changes: 25 additions & 0 deletions tools/perf/scripts/python/Perf-Trace-Util/lib/Perf/Trace/Util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Util.py - Python extension for perf trace, miscellaneous utility code
#
# Copyright (C) 2010 by Tom Zanussi <tzanussi@gmail.com>
#
# This software may be distributed under the terms of the GNU General
# Public License ("GPL") version 2 as published by the Free Software
# Foundation.

NSECS_PER_SEC = 1000000000

def avg(total, n):
return total / n

def nsecs(secs, nsecs):
return secs * NSECS_PER_SEC + nsecs

def nsecs_secs(nsecs):
return nsecs / NSECS_PER_SEC

def nsecs_nsecs(nsecs):
return nsecs % NSECS_PER_SEC

def nsecs_str(nsecs):
str = "%5u.%09u" % (nsecs_secs(nsecs), nsecs_nsecs(nsecs)),
return str
Loading

0 comments on commit 7e4b21b

Please sign in to comment.