From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751776AbdGZR3Z (ORCPT ); Wed, 26 Jul 2017 13:29:25 -0400 Received: from terminus.zytor.com ([65.50.211.136]:58739 "EHLO terminus.zytor.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751755AbdGZR3W (ORCPT ); Wed, 26 Jul 2017 13:29:22 -0400 Date: Wed, 26 Jul 2017 10:24:27 -0700 From: tip-bot for Arun Kalyanasundaram Message-ID: Cc: davidcc@google.com, mingo@kernel.org, sj38.park@gmail.com, arunkaly@google.com, peterz@infradead.org, eranian@google.com, hpa@zytor.com, linux-kernel@vger.kernel.org, davem@davemloft.net, alexander.shishkin@linux.intel.com, tglx@linutronix.de, acme@redhat.com, jolsa@kernel.org, daniel@iogearbox.net Reply-To: alexander.shishkin@linux.intel.com, davem@davemloft.net, jolsa@kernel.org, daniel@iogearbox.net, tglx@linutronix.de, acme@redhat.com, eranian@google.com, peterz@infradead.org, arunkaly@google.com, linux-kernel@vger.kernel.org, hpa@zytor.com, sj38.park@gmail.com, davidcc@google.com, mingo@kernel.org In-Reply-To: <20170721220422.63962-2-arunkaly@google.com> References: <20170721220422.63962-2-arunkaly@google.com> To: linux-tip-commits@vger.kernel.org Subject: [tip:perf/core] perf script python: Allocate memory only if handler exists Git-Commit-ID: e9f9a9ca8588e58dc0800b44adc41d32f6fc813a X-Mailer: tip-git-log-daemon Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8 Content-Disposition: inline Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit-ID: e9f9a9ca8588e58dc0800b44adc41d32f6fc813a Gitweb: http://git.kernel.org/tip/e9f9a9ca8588e58dc0800b44adc41d32f6fc813a Author: Arun Kalyanasundaram AuthorDate: Fri, 21 Jul 2017 15:04:18 -0700 Committer: Arnaldo Carvalho de Melo CommitDate: Tue, 25 Jul 2017 22:43:18 -0300 perf script python: Allocate memory only if handler exists 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 Acked-by: Jiri Olsa Cc: Alexander Shishkin Cc: Daniel Borkmann Cc: David Carrillo-Cisneros Cc: David S. Miller Cc: Peter Zijlstra Cc: Seongjae Park Cc: Stephane Eranian Link: http://lkml.kernel.org/r/20170721220422.63962-2-arunkaly@google.com Signed-off-by: Arnaldo Carvalho de Melo --- .../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 57b7a00..8a8f482 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); }