public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: tip-bot for Arun Kalyanasundaram <tipbot@zytor.com>
To: linux-tip-commits@vger.kernel.org
Cc: sj38.park@gmail.com, daniel@iogearbox.net, mingo@kernel.org,
	davidcc@google.com, acme@redhat.com, peterz@infradead.org,
	jolsa@kernel.org, linux-kernel@vger.kernel.org,
	davem@davemloft.net, alexander.shishkin@linux.intel.com,
	arunkaly@google.com, eranian@google.com, hpa@zytor.com,
	tglx@linutronix.de
Subject: [tip:perf/core] perf script python: Add perf_sample dict to tracepoint handlers
Date: Wed, 26 Jul 2017 10:25:29 -0700	[thread overview]
Message-ID: <tip-f38d281663b011d1d8a1b0119bb8357706d134a8@git.kernel.org> (raw)
In-Reply-To: <20170721220422.63962-5-arunkaly@google.com>

Commit-ID:  f38d281663b011d1d8a1b0119bb8357706d134a8
Gitweb:     http://git.kernel.org/tip/f38d281663b011d1d8a1b0119bb8357706d134a8
Author:     Arun Kalyanasundaram <arunkaly@google.com>
AuthorDate: Fri, 21 Jul 2017 15:04:21 -0700
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Tue, 25 Jul 2017 22:43:20 -0300

perf script python: Add perf_sample dict to tracepoint handlers

The process_event python hook receives a dict with all perf_sample
entries, but the tracepoint specific and trace_unhandled hooks predate
the introduction of this dict, and do not receive it.

Add the aforementioned dict as an additional argument to the affected
handlers. To keep backwards compatibility (and avoid unnecessary work),
do not pass the dict if the number of arguments signals that handler
version predates this change.

Signed-off-by: Arun Kalyanasundaram <arunkaly@google.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: David Carrillo-Cisneros <davidcc@google.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Seongjae Park <sj38.park@gmail.com>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/20170721220422.63962-5-arunkaly@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 .../util/scripting-engines/trace-event-python.c    | 41 +++++++++++++++++++++-
 1 file changed, 40 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 55a4578..938b39f 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -116,6 +116,34 @@ static PyObject *get_handler(const char *handler_name)
 	return handler;
 }
 
+static int get_argument_count(PyObject *handler)
+{
+	int arg_count = 0;
+
+	/*
+	 * The attribute for the code object is func_code in Python 2,
+	 * whereas it is __code__ in Python 3.0+.
+	 */
+	PyObject *code_obj = PyObject_GetAttrString(handler,
+		"func_code");
+	if (PyErr_Occurred()) {
+		PyErr_Clear();
+		code_obj = PyObject_GetAttrString(handler,
+			"__code__");
+	}
+	PyErr_Clear();
+	if (code_obj) {
+		PyObject *arg_count_obj = PyObject_GetAttrString(code_obj,
+			"co_argcount");
+		if (arg_count_obj) {
+			arg_count = (int) PyInt_AsLong(arg_count_obj);
+			Py_DECREF(arg_count_obj);
+		}
+		Py_DECREF(code_obj);
+	}
+	return arg_count;
+}
+
 static void call_object(PyObject *handler, PyObject *args, const char *die_msg)
 {
 	PyObject *retval;
@@ -499,7 +527,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
 {
 	struct event_format *event = evsel->tp_format;
 	PyObject *handler, *context, *t, *obj = NULL, *callchain;
-	PyObject *dict = NULL;
+	PyObject *dict = NULL, *all_entries_dict = NULL;
 	static char handler_name[256];
 	struct format_field *field;
 	unsigned long s, ns;
@@ -552,6 +580,8 @@ static void python_process_tracepoint(struct perf_sample *sample,
 
 	/* ip unwinding */
 	callchain = python_process_callchain(sample, evsel, al);
+	/* Need an additional reference for the perf_sample dict */
+	Py_INCREF(callchain);
 
 	if (!dict) {
 		PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
@@ -602,6 +632,14 @@ static void python_process_tracepoint(struct perf_sample *sample,
 	if (dict)
 		PyTuple_SetItem(t, n++, dict);
 
+	if (get_argument_count(handler) == (int) n + 1) {
+		all_entries_dict = get_perf_sample_dict(sample, evsel, al,
+			callchain);
+		PyTuple_SetItem(t, n++,	all_entries_dict);
+	} else {
+		Py_DECREF(callchain);
+	}
+
 	if (_PyTuple_Resize(&t, n) == -1)
 		Py_FatalError("error resizing Python tuple");
 
@@ -612,6 +650,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
 		Py_DECREF(dict);
 	}
 
+	Py_XDECREF(all_entries_dict);
 	Py_DECREF(t);
 }
 

  reply	other threads:[~2017-07-26 17:30 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-21 22:04 [PATCH v2 0/5] perf script python: Provide sample dict to all handlers Arun Kalyanasundaram
2017-07-21 22:04 ` [PATCH v2 1/5] perf script python: Allocate memory only if handler exists Arun Kalyanasundaram
2017-07-26 17:24   ` [tip:perf/core] " tip-bot for Arun Kalyanasundaram
2017-07-21 22:04 ` [PATCH v2 2/5] perf script python: Refactor creation of perf sample dict Arun Kalyanasundaram
2017-07-26 17:24   ` [tip:perf/core] " tip-bot for Arun Kalyanasundaram
2017-07-21 22:04 ` [PATCH v2 3/5] perf script python: Add sample_read to dict Arun Kalyanasundaram
2017-07-26 17:25   ` [tip:perf/core] " tip-bot for Arun Kalyanasundaram
2017-07-21 22:04 ` [PATCH v2 4/5] perf script python: Add perf_sample dict to tracepoint handlers Arun Kalyanasundaram
2017-07-26 17:25   ` tip-bot for Arun Kalyanasundaram [this message]
2017-07-21 22:04 ` [PATCH v2 5/5] perf script python: Generate hooks with additional argument Arun Kalyanasundaram
2017-07-26 17:25   ` [tip:perf/core] " tip-bot for Arun Kalyanasundaram
2017-07-24 13:28 ` [PATCH v2 0/5] perf script python: Provide sample dict to all handlers Jiri Olsa

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=tip-f38d281663b011d1d8a1b0119bb8357706d134a8@git.kernel.org \
    --to=tipbot@zytor.com \
    --cc=acme@redhat.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=arunkaly@google.com \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=davidcc@google.com \
    --cc=eranian@google.com \
    --cc=hpa@zytor.com \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    --cc=sj38.park@gmail.com \
    --cc=tglx@linutronix.de \
    /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