Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1697201

[PATCH 31/40] perf script python: Allocate memory only if handler exists

From Arnaldo Carvalho de Melo <acme@kernel.org>
Newsgroups linux.kernel
Subject [PATCH 31/40] perf script python: Allocate memory only if handler exists
Date 2017-07-26 16:30 +0200
Message-ID <u7vu4-1s1-73@gated-at.bofh.it> (permalink)
References <u7vkm-1ob-7@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


From: Arun Kalyanasundaram <arunkaly@google.com>

Avoid allocating memory if hook handler is not available. This saves
unused memory allocation and simplifies error path.

Let handler in python_process_tracepoint point to either tracepoint
specific or trace_unhandled hook. Use dict to check if handler points to
trace_unhandled.

Remove the exit label in python_process_general_event and return when no
handler is available.

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-2-arunkaly@google.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 .../util/scripting-engines/trace-event-python.c    | 38 +++++++++++++---------
 1 file changed, 22 insertions(+), 16 deletions(-)

diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c
index 57b7a00e6f16..8a8f4829d3e2 100644
--- a/tools/perf/util/scripting-engines/trace-event-python.c
+++ b/tools/perf/util/scripting-engines/trace-event-python.c
@@ -407,10 +407,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
 	void *data = sample->raw_data;
 	unsigned long long nsecs = sample->time;
 	const char *comm = thread__comm_str(al->thread);
-
-	t = PyTuple_New(MAX_FIELDS);
-	if (!t)
-		Py_FatalError("couldn't create Python tuple");
+	const char *default_handler_name = "trace_unhandled";
 
 	if (!event) {
 		snprintf(handler_name, sizeof(handler_name),
@@ -427,10 +424,19 @@ static void python_process_tracepoint(struct perf_sample *sample,
 
 	handler = get_handler(handler_name);
 	if (!handler) {
+		handler = get_handler(default_handler_name);
+		if (!handler)
+			return;
 		dict = PyDict_New();
 		if (!dict)
 			Py_FatalError("couldn't create Python dict");
 	}
+
+	t = PyTuple_New(MAX_FIELDS);
+	if (!t)
+		Py_FatalError("couldn't create Python tuple");
+
+
 	s = nsecs / NSEC_PER_SEC;
 	ns = nsecs - s * NSEC_PER_SEC;
 
@@ -445,7 +451,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
 	/* ip unwinding */
 	callchain = python_process_callchain(sample, evsel, al);
 
-	if (handler) {
+	if (!dict) {
 		PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
 		PyTuple_SetItem(t, n++, PyInt_FromLong(s));
 		PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
@@ -484,23 +490,23 @@ static void python_process_tracepoint(struct perf_sample *sample,
 		} else { /* FIELD_IS_NUMERIC */
 			obj = get_field_numeric_entry(event, field, data);
 		}
-		if (handler)
+		if (!dict)
 			PyTuple_SetItem(t, n++, obj);
 		else
 			pydict_set_item_string_decref(dict, field->name, obj);
 
 	}
 
-	if (!handler)
+	if (dict)
 		PyTuple_SetItem(t, n++, dict);
 
 	if (_PyTuple_Resize(&t, n) == -1)
 		Py_FatalError("error resizing Python tuple");
 
-	if (handler) {
+	if (!dict) {
 		call_object(handler, t, handler_name);
 	} else {
-		try_call_object("trace_unhandled", t);
+		call_object(handler, t, default_handler_name);
 		Py_DECREF(dict);
 	}
 
@@ -799,6 +805,12 @@ static void python_process_general_event(struct perf_sample *sample,
 	static char handler_name[64];
 	unsigned n = 0;
 
+	snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
+
+	handler = get_handler(handler_name);
+	if (!handler)
+		return;
+
 	/*
 	 * Use the MAX_FIELDS to make the function expandable, though
 	 * currently there is only one item for the tuple.
@@ -815,12 +827,6 @@ static void python_process_general_event(struct perf_sample *sample,
 	if (!dict_sample)
 		Py_FatalError("couldn't create Python dictionary");
 
-	snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
-
-	handler = get_handler(handler_name);
-	if (!handler)
-		goto exit;
-
 	pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
 	pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
 			(const char *)&evsel->attr, sizeof(evsel->attr)));
@@ -861,7 +867,7 @@ static void python_process_general_event(struct perf_sample *sample,
 		Py_FatalError("error resizing Python tuple");
 
 	call_object(handler, t, handler_name);
-exit:
+
 	Py_DECREF(dict);
 	Py_DECREF(t);
 }
-- 
2.9.4

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[GIT PULL 00/40] perf/core improvements and fixes Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:20 +0200
  [PATCH 15/40] tools include: Adopt strstarts() from the kernel Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:20 +0200
  [PATCH 30/40] perf script: Remove some bogus error handling Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 39/40] perf annotate stdio: Fix column header when using --show-total-period Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 33/40] perf script python: Add sample_read to dict Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 22/40] perf annotate stdio: Fix --show-total-period Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 40/40] perf tools: Add tools/include/uapi/asm-generic/fcntl.h to the MANIFEST Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 38/40] perf jevents: Make build fail on JSON parse error Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 36/40] perf report: Make --branch-history work without callgraphs(-g) option in perf record Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 26/40] perf annotate: Process tracing data in pipe mode Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 29/40] perf top: Support lookup of symbols in other mount namespaces. Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 32/40] perf script python: Refactor creation of perf sample dict Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 23/40] perf report: Fix kernel symbol adjustment for s390x Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 28/40] perf evsel: Add verbose output for sys_perf_event_open fallback Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 35/40] perf script python: Generate hooks with additional argument Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 21/40] perf annotate: Do not overwrite sample->period Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 27/40] perf jvmti: Fix linker error when libelf config is disabled Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 31/40] perf script python: Allocate memory only if handler exists Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 34/40] perf script python: Add perf_sample dict to tracepoint handlers Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 25/40] perf tools: Add EXCLUDE_EXTLIBS and EXTRA_PERFLIBS to makefile Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 24/40] perf cgroup: Fix refcount usage Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 37/40] perf report: Tag branch type/flag on "to" and tag cycles on "from" Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:30 +0200
  [PATCH 03/40] perf trace: Add missing ' = ' in the default formatting of syscall returns Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:40 +0200
  [PATCH 05/40] perf trace: Allow allocating sc->arg_fmt even without the syscall tracepoint Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:40 +0200
  [PATCH 01/40] perf intel-pt: Set no_aux_samples for the tracking event Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:40 +0200
  [PATCH 04/40] perf trace beauty mmap: Ignore 'fd' and 'offset' args for MAP_ANONYMOUS Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:40 +0200
  [PATCH 17/40] perf annotate: Introduce struct sym_hist_entry Arnaldo Carvalho de Melo <acme@kernel.org> - 2017-07-26 16:40 +0200
  Re: [GIT PULL 00/40] perf/core improvements and fixes Ingo Molnar <mingo@kernel.org> - 2017-07-26 19:20 +0200

csiph-web