From: Adrian Hunter <adrian.hunter@intel.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>,
linux-kernel@vger.kernel.org, David Ahern <dsahern@gmail.com>,
Frederic Weisbecker <fweisbec@gmail.com>,
Jiri Olsa <jolsa@redhat.com>, Namhyung Kim <namhyung@gmail.com>,
Paul Mackerras <paulus@samba.org>,
Stephane Eranian <eranian@google.com>
Subject: [PATCH 13/24] perf tools: Extend Python script interface to export data in a database-friendly way
Date: Fri, 15 Aug 2014 22:08:48 +0300 [thread overview]
Message-ID: <1408129739-17368-14-git-send-email-adrian.hunter@intel.com> (raw)
In-Reply-To: <1408129739-17368-1-git-send-email-adrian.hunter@intel.com>
Use the new db_export facility to export data in a
database-friendly way.
A Python script selects the db_export mode by setting
a global variable 'perf_db_export_mode' to True. The
script then optionally implements functions to receive
table rows. The functions are:
evsel_table
machine_table
thread_table
comm_table
dso_table
symbol_table
sample_table
An example script is provided in a subsequent patch.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
.../util/scripting-engines/trace-event-python.c | 281 ++++++++++++++++++++-
1 file changed, 279 insertions(+), 2 deletions(-)
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 56ba07c..400c7d6 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -24,6 +24,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <stdbool.h>
#include <errno.h>
#include "../../perf.h"
@@ -32,6 +33,9 @@
#include "../util.h"
#include "../event.h"
#include "../thread.h"
+#include "../comm.h"
+#include "../machine.h"
+#include "../db-export.h"
#include "../trace-event.h"
#include "../machine.h"
@@ -52,6 +56,21 @@ static int zero_flag_atom;
static PyObject *main_module, *main_dict;
+struct tables {
+ struct db_export dbe;
+ PyObject *evsel_handler;
+ PyObject *machine_handler;
+ PyObject *thread_handler;
+ PyObject *comm_handler;
+ PyObject *comm_thread_handler;
+ PyObject *dso_handler;
+ PyObject *symbol_handler;
+ PyObject *sample_handler;
+ bool db_export_mode;
+};
+
+static struct tables tables_global;
+
static void handler_call_die(const char *handler_name) NORETURN;
static void handler_call_die(const char *handler_name)
{
@@ -474,6 +493,210 @@ static void python_process_tracepoint(struct perf_sample *sample,
Py_DECREF(t);
}
+static PyObject *tuple_new(unsigned int sz)
+{
+ PyObject *t;
+
+ t = PyTuple_New(sz);
+ if (!t)
+ Py_FatalError("couldn't create Python tuple");
+ return t;
+}
+
+static int tuple_set_u64(PyObject *t, unsigned int pos, u64 val)
+{
+#if BITS_PER_LONG == 64
+ return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
+#endif
+#if BITS_PER_LONG == 32
+ return PyTuple_SetItem(t, pos, PyLong_FromLongLong(val));
+#endif
+}
+
+static int tuple_set_s32(PyObject *t, unsigned int pos, s32 val)
+{
+ return PyTuple_SetItem(t, pos, PyInt_FromLong(val));
+}
+
+static int tuple_set_string(PyObject *t, unsigned int pos, const char *s)
+{
+ return PyTuple_SetItem(t, pos, PyString_FromString(s));
+}
+
+static int python_export_evsel(struct db_export *dbe, struct perf_evsel *evsel)
+{
+ struct tables *tables = container_of(dbe, struct tables, dbe);
+ PyObject *t;
+
+ t = tuple_new(2);
+
+ tuple_set_u64(t, 0, evsel->db_id);
+ tuple_set_string(t, 1, perf_evsel__name(evsel));
+
+ call_object(tables->evsel_handler, t, "evsel_table");
+
+ Py_DECREF(t);
+
+ return 0;
+}
+
+static int python_export_machine(struct db_export *dbe,
+ struct machine *machine)
+{
+ struct tables *tables = container_of(dbe, struct tables, dbe);
+ PyObject *t;
+
+ t = tuple_new(3);
+
+ tuple_set_u64(t, 0, machine->db_id);
+ tuple_set_s32(t, 1, machine->pid);
+ tuple_set_string(t, 2, machine->root_dir ? machine->root_dir : "");
+
+ call_object(tables->machine_handler, t, "machine_table");
+
+ Py_DECREF(t);
+
+ return 0;
+}
+
+static int python_export_thread(struct db_export *dbe, struct thread *thread,
+ u64 main_thread_db_id, struct machine *machine)
+{
+ struct tables *tables = container_of(dbe, struct tables, dbe);
+ PyObject *t;
+
+ t = tuple_new(5);
+
+ tuple_set_u64(t, 0, thread->db_id);
+ tuple_set_u64(t, 1, machine->db_id);
+ tuple_set_u64(t, 2, main_thread_db_id);
+ tuple_set_s32(t, 3, thread->pid_);
+ tuple_set_s32(t, 4, thread->tid);
+
+ call_object(tables->thread_handler, t, "thread_table");
+
+ Py_DECREF(t);
+
+ return 0;
+}
+
+static int python_export_comm(struct db_export *dbe, struct comm *comm)
+{
+ struct tables *tables = container_of(dbe, struct tables, dbe);
+ PyObject *t;
+
+ t = tuple_new(2);
+
+ tuple_set_u64(t, 0, comm->db_id);
+ tuple_set_string(t, 1, comm__str(comm));
+
+ call_object(tables->comm_handler, t, "comm_table");
+
+ Py_DECREF(t);
+
+ return 0;
+}
+
+static int python_export_comm_thread(struct db_export *dbe, u64 db_id,
+ struct comm *comm, struct thread *thread)
+{
+ struct tables *tables = container_of(dbe, struct tables, dbe);
+ PyObject *t;
+
+ t = tuple_new(3);
+
+ tuple_set_u64(t, 0, db_id);
+ tuple_set_u64(t, 1, comm->db_id);
+ tuple_set_u64(t, 2, thread->db_id);
+
+ call_object(tables->comm_thread_handler, t, "comm_thread_table");
+
+ Py_DECREF(t);
+
+ return 0;
+}
+
+static int python_export_dso(struct db_export *dbe, struct dso *dso,
+ struct machine *machine)
+{
+ struct tables *tables = container_of(dbe, struct tables, dbe);
+ char sbuild_id[BUILD_ID_SIZE * 2 + 1];
+ PyObject *t;
+
+ build_id__sprintf(dso->build_id, sizeof(dso->build_id), sbuild_id);
+
+ t = tuple_new(5);
+
+ tuple_set_u64(t, 0, dso->db_id);
+ tuple_set_u64(t, 1, machine->db_id);
+ tuple_set_string(t, 2, dso->short_name);
+ tuple_set_string(t, 3, dso->long_name);
+ tuple_set_string(t, 4, sbuild_id);
+
+ call_object(tables->dso_handler, t, "dso_table");
+
+ Py_DECREF(t);
+
+ return 0;
+}
+
+static int python_export_symbol(struct db_export *dbe, struct symbol *sym,
+ struct dso *dso)
+{
+ struct tables *tables = container_of(dbe, struct tables, dbe);
+ PyObject *t;
+
+ t = tuple_new(6);
+
+ tuple_set_u64(t, 0, sym->db_id);
+ tuple_set_u64(t, 1, dso->db_id);
+ tuple_set_u64(t, 2, sym->start);
+ tuple_set_u64(t, 3, sym->end);
+ tuple_set_s32(t, 4, sym->binding);
+ tuple_set_string(t, 5, sym->name);
+
+ call_object(tables->symbol_handler, t, "symbol_table");
+
+ Py_DECREF(t);
+
+ return 0;
+}
+
+static int python_export_sample(struct db_export *dbe,
+ struct export_sample *es)
+{
+ struct tables *tables = container_of(dbe, struct tables, dbe);
+ PyObject *t;
+
+ t = tuple_new(19);
+
+ tuple_set_u64(t, 0, es->db_id);
+ tuple_set_u64(t, 1, es->evsel->db_id);
+ tuple_set_u64(t, 2, es->al->machine->db_id);
+ tuple_set_u64(t, 3, es->thread->db_id);
+ tuple_set_u64(t, 4, es->comm_db_id);
+ tuple_set_u64(t, 5, es->dso_db_id);
+ tuple_set_u64(t, 6, es->sym_db_id);
+ tuple_set_u64(t, 7, es->offset);
+ tuple_set_u64(t, 8, es->sample->ip);
+ tuple_set_u64(t, 9, es->sample->time);
+ tuple_set_s32(t, 10, es->sample->cpu);
+ tuple_set_u64(t, 11, es->addr_dso_db_id);
+ tuple_set_u64(t, 12, es->addr_sym_db_id);
+ tuple_set_u64(t, 13, es->addr_offset);
+ tuple_set_u64(t, 14, es->sample->addr);
+ tuple_set_u64(t, 15, es->sample->period);
+ tuple_set_u64(t, 16, es->sample->weight);
+ tuple_set_u64(t, 17, es->sample->transaction);
+ tuple_set_u64(t, 18, es->sample->data_src);
+
+ call_object(tables->sample_handler, t, "sample_table");
+
+ Py_DECREF(t);
+
+ return 0;
+}
+
static void python_process_general_event(struct perf_sample *sample,
struct perf_evsel *evsel,
struct thread *thread,
@@ -550,19 +773,25 @@ exit:
Py_DECREF(t);
}
-static void python_process_event(union perf_event *event __maybe_unused,
+static void python_process_event(union perf_event *event,
struct perf_sample *sample,
struct perf_evsel *evsel,
struct thread *thread,
struct addr_location *al)
{
+ struct tables *tables = &tables_global;
+
switch (evsel->attr.type) {
case PERF_TYPE_TRACEPOINT:
python_process_tracepoint(sample, evsel, thread, al);
break;
/* Reserve for future process_hw/sw/raw APIs */
default:
- python_process_general_event(sample, evsel, thread, al);
+ if (tables->db_export_mode)
+ db_export__sample(&tables->dbe, event, sample, evsel,
+ thread, al);
+ else
+ python_process_general_event(sample, evsel, thread, al);
}
}
@@ -588,11 +817,53 @@ error:
return -1;
}
+#define SET_TABLE_HANDLER_(name, handler_name, table_name) do { \
+ tables->handler_name = get_handler(#table_name); \
+ if (tables->handler_name) \
+ tables->dbe.export_ ## name = python_export_ ## name; \
+} while (0)
+
+#define SET_TABLE_HANDLER(name) \
+ SET_TABLE_HANDLER_(name, name ## _handler, name ## _table)
+
+static void set_table_handlers(struct tables *tables)
+{
+ const char *perf_db_export_mode = "perf_db_export_mode";
+ PyObject *db_export_mode;
+ int ret;
+
+ memset(tables, 0, sizeof(struct tables));
+ if (db_export__init(&tables->dbe))
+ Py_FatalError("failed to initialize export");
+
+ db_export_mode = PyDict_GetItemString(main_dict, perf_db_export_mode);
+ if (!db_export_mode)
+ return;
+
+ ret = PyObject_IsTrue(db_export_mode);
+ if (ret == -1)
+ handler_call_die(perf_db_export_mode);
+ if (!ret)
+ return;
+
+ tables->db_export_mode = true;
+
+ SET_TABLE_HANDLER(evsel);
+ SET_TABLE_HANDLER(machine);
+ SET_TABLE_HANDLER(thread);
+ SET_TABLE_HANDLER(comm);
+ SET_TABLE_HANDLER(comm_thread);
+ SET_TABLE_HANDLER(dso);
+ SET_TABLE_HANDLER(symbol);
+ SET_TABLE_HANDLER(sample);
+}
+
/*
* Start trace script
*/
static int python_start_script(const char *script, int argc, const char **argv)
{
+ struct tables *tables = &tables_global;
const char **command_line;
char buf[PATH_MAX];
int i, err = 0;
@@ -631,6 +902,8 @@ static int python_start_script(const char *script, int argc, const char **argv)
free(command_line);
+ set_table_handlers(tables);
+
return err;
error:
Py_Finalize();
@@ -649,8 +922,12 @@ static int python_flush_script(void)
*/
static int python_stop_script(void)
{
+ struct tables *tables = &tables_global;
+
try_call_object("trace_end", NULL);
+ db_export__exit(&tables->dbe);
+
Py_XDECREF(main_dict);
Py_XDECREF(main_module);
Py_Finalize();
--
1.8.3.2
next prev parent reply other threads:[~2014-08-15 19:14 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-08-15 19:08 [PATCH 00/24] perf tools: Still more preparation for Intel PT Adrian Hunter
2014-08-15 19:08 ` [PATCH 01/24] perf tools: Add a test for tracking with sched_switch Adrian Hunter
2014-08-20 19:48 ` Arnaldo Carvalho de Melo
2014-08-29 13:52 ` Adrian Hunter
2014-08-29 15:06 ` Arnaldo Carvalho de Melo
2014-08-29 15:18 ` Jiri Olsa
2014-08-29 18:38 ` Adrian Hunter
2014-08-21 16:58 ` Arnaldo Carvalho de Melo
2014-08-24 14:58 ` [tip:perf/core] perf tests: " tip-bot for Adrian Hunter
2014-08-15 19:08 ` [PATCH 02/24] perf scripting: Add 'flush' callback to scripting API Adrian Hunter
2014-08-24 14:58 ` [tip:perf/core] " tip-bot for Adrian Hunter
2014-08-15 19:08 ` [PATCH 03/24] perf tools: Rename machine__get_kernel_start_addr() Adrian Hunter
2014-08-24 14:58 ` [tip:perf/core] perf machine: Rename machine__get_kernel_start_addr() method tip-bot for Adrian Hunter
2014-08-15 19:08 ` [PATCH 04/24] perf tools: Add machine__kernel_ip() Adrian Hunter
2014-08-24 14:58 ` [tip:perf/core] " tip-bot for Adrian Hunter
2014-08-15 19:08 ` [PATCH 05/24] perf tools: Let a user specify a PMU event without any config terms Adrian Hunter
2014-09-19 5:20 ` [tip:perf/core] " tip-bot for Adrian Hunter
2014-08-15 19:08 ` [PATCH 06/24] perf tools: Let default config be defined for a PMU Adrian Hunter
2014-08-15 19:08 ` [PATCH 07/24] perf tools: Add perf_pmu__scan_file() Adrian Hunter
2014-08-15 19:08 ` [PATCH 08/24] perf tools: Add id index Adrian Hunter
2014-08-15 19:08 ` [PATCH 09/24] perf pmu: Let pmu's with no events show up on perf list Adrian Hunter
2014-08-15 19:08 ` [PATCH 10/24] perf session: Add perf_session__deliver_synth_event() Adrian Hunter
2014-08-15 19:08 ` [PATCH 11/24] perf tools: Add a thread stack for synthesizing call chains Adrian Hunter
2014-08-15 19:08 ` [PATCH 12/24] perf tools: Add facility to export data in database-friendly way Adrian Hunter
2014-08-15 19:08 ` Adrian Hunter [this message]
2014-08-15 19:08 ` [PATCH 14/24] perf tools: Add Python script to export to postgresql Adrian Hunter
2014-08-15 19:08 ` [PATCH 15/24] perf tools: Add branch type to db export Adrian Hunter
2014-08-15 19:08 ` [PATCH 16/24] perf tools: Add branch_type and in_tx to Python export Adrian Hunter
2014-08-15 19:08 ` [PATCH 17/24] perf tools: Enhance the thread stack to output call/return data Adrian Hunter
2014-08-15 19:08 ` [PATCH 18/24] perf tools: Add call information to the database export API Adrian Hunter
2014-08-15 19:08 ` [PATCH 19/24] perf tools: Add call information to Python export Adrian Hunter
2014-08-15 19:08 ` [PATCH 20/24] perf tools: Defer export of comms that were not 'set' Adrian Hunter
2014-08-15 19:08 ` [PATCH 21/24] perf tools: Add perf-with-kcore script Adrian Hunter
2014-08-15 19:08 ` [PATCH 22/24] perf tools: Build programs to copy 32-bit compatibility VDSOs Adrian Hunter
2014-09-15 15:47 ` Arnaldo Carvalho de Melo
2014-09-15 15:50 ` Arnaldo Carvalho de Melo
2014-09-16 6:59 ` Adrian Hunter
2014-09-17 14:52 ` Arnaldo Carvalho de Melo
2014-08-15 19:08 ` [PATCH 23/24] perf tools: Add support for " Adrian Hunter
2014-08-15 19:08 ` [PATCH 24/24] perf tools: Add feature checks to .gitignore Adrian Hunter
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1408129739-17368-14-git-send-email-adrian.hunter@intel.com \
--to=adrian.hunter@intel.com \
--cc=acme@kernel.org \
--cc=dsahern@gmail.com \
--cc=eranian@google.com \
--cc=fweisbec@gmail.com \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=namhyung@gmail.com \
--cc=paulus@samba.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).