Commit e9f9a9ca authored by Arun Kalyanasundaram's avatar Arun Kalyanasundaram Committed by Arnaldo Carvalho de Melo

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: default avatarArun Kalyanasundaram <arunkaly@google.com>
Acked-by: default avatarJiri 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.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent 2ec5cab6
...@@ -407,10 +407,7 @@ static void python_process_tracepoint(struct perf_sample *sample, ...@@ -407,10 +407,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
void *data = sample->raw_data; void *data = sample->raw_data;
unsigned long long nsecs = sample->time; unsigned long long nsecs = sample->time;
const char *comm = thread__comm_str(al->thread); const char *comm = thread__comm_str(al->thread);
const char *default_handler_name = "trace_unhandled";
t = PyTuple_New(MAX_FIELDS);
if (!t)
Py_FatalError("couldn't create Python tuple");
if (!event) { if (!event) {
snprintf(handler_name, sizeof(handler_name), snprintf(handler_name, sizeof(handler_name),
...@@ -427,10 +424,19 @@ static void python_process_tracepoint(struct perf_sample *sample, ...@@ -427,10 +424,19 @@ static void python_process_tracepoint(struct perf_sample *sample,
handler = get_handler(handler_name); handler = get_handler(handler_name);
if (!handler) { if (!handler) {
handler = get_handler(default_handler_name);
if (!handler)
return;
dict = PyDict_New(); dict = PyDict_New();
if (!dict) if (!dict)
Py_FatalError("couldn't create Python 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; s = nsecs / NSEC_PER_SEC;
ns = nsecs - s * NSEC_PER_SEC; ns = nsecs - s * NSEC_PER_SEC;
...@@ -445,7 +451,7 @@ static void python_process_tracepoint(struct perf_sample *sample, ...@@ -445,7 +451,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
/* ip unwinding */ /* ip unwinding */
callchain = python_process_callchain(sample, evsel, al); callchain = python_process_callchain(sample, evsel, al);
if (handler) { if (!dict) {
PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
PyTuple_SetItem(t, n++, PyInt_FromLong(s)); PyTuple_SetItem(t, n++, PyInt_FromLong(s));
PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
...@@ -484,23 +490,23 @@ static void python_process_tracepoint(struct perf_sample *sample, ...@@ -484,23 +490,23 @@ static void python_process_tracepoint(struct perf_sample *sample,
} else { /* FIELD_IS_NUMERIC */ } else { /* FIELD_IS_NUMERIC */
obj = get_field_numeric_entry(event, field, data); obj = get_field_numeric_entry(event, field, data);
} }
if (handler) if (!dict)
PyTuple_SetItem(t, n++, obj); PyTuple_SetItem(t, n++, obj);
else else
pydict_set_item_string_decref(dict, field->name, obj); pydict_set_item_string_decref(dict, field->name, obj);
} }
if (!handler) if (dict)
PyTuple_SetItem(t, n++, dict); PyTuple_SetItem(t, n++, dict);
if (_PyTuple_Resize(&t, n) == -1) if (_PyTuple_Resize(&t, n) == -1)
Py_FatalError("error resizing Python tuple"); Py_FatalError("error resizing Python tuple");
if (handler) { if (!dict) {
call_object(handler, t, handler_name); call_object(handler, t, handler_name);
} else { } else {
try_call_object("trace_unhandled", t); call_object(handler, t, default_handler_name);
Py_DECREF(dict); Py_DECREF(dict);
} }
...@@ -799,6 +805,12 @@ static void python_process_general_event(struct perf_sample *sample, ...@@ -799,6 +805,12 @@ static void python_process_general_event(struct perf_sample *sample,
static char handler_name[64]; static char handler_name[64];
unsigned n = 0; 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 * Use the MAX_FIELDS to make the function expandable, though
* currently there is only one item for the tuple. * currently there is only one item for the tuple.
...@@ -815,12 +827,6 @@ static void python_process_general_event(struct perf_sample *sample, ...@@ -815,12 +827,6 @@ static void python_process_general_event(struct perf_sample *sample,
if (!dict_sample) if (!dict_sample)
Py_FatalError("couldn't create Python dictionary"); 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, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize( pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
(const char *)&evsel->attr, sizeof(evsel->attr))); (const char *)&evsel->attr, sizeof(evsel->attr)));
...@@ -861,7 +867,7 @@ static void python_process_general_event(struct perf_sample *sample, ...@@ -861,7 +867,7 @@ static void python_process_general_event(struct perf_sample *sample,
Py_FatalError("error resizing Python tuple"); Py_FatalError("error resizing Python tuple");
call_object(handler, t, handler_name); call_object(handler, t, handler_name);
exit:
Py_DECREF(dict); Py_DECREF(dict);
Py_DECREF(t); Py_DECREF(t);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment