Skip to content

Commit 179eeb3

Browse files
Arun Kalyanasundaram0day robot
authored andcommitted
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 <arunkaly@google.com>
1 parent 510457e commit 179eeb3

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

tools/perf/util/scripting-engines/trace-event-python.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
407407
void *data = sample->raw_data;
408408
unsigned long long nsecs = sample->time;
409409
const char *comm = thread__comm_str(al->thread);
410-
411-
t = PyTuple_New(MAX_FIELDS);
412-
if (!t)
413-
Py_FatalError("couldn't create Python tuple");
410+
const char *default_handler_name = "trace_unhandled";
414411

415412
if (!event) {
416413
snprintf(handler_name, sizeof(handler_name),
@@ -427,10 +424,19 @@ static void python_process_tracepoint(struct perf_sample *sample,
427424

428425
handler = get_handler(handler_name);
429426
if (!handler) {
427+
handler = get_handler(default_handler_name);
428+
if (!handler)
429+
return;
430430
dict = PyDict_New();
431431
if (!dict)
432432
Py_FatalError("couldn't create Python dict");
433433
}
434+
435+
t = PyTuple_New(MAX_FIELDS);
436+
if (!t)
437+
Py_FatalError("couldn't create Python tuple");
438+
439+
434440
s = nsecs / NSEC_PER_SEC;
435441
ns = nsecs - s * NSEC_PER_SEC;
436442

@@ -445,7 +451,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
445451
/* ip unwinding */
446452
callchain = python_process_callchain(sample, evsel, al);
447453

448-
if (handler) {
454+
if (!dict) {
449455
PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
450456
PyTuple_SetItem(t, n++, PyInt_FromLong(s));
451457
PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
@@ -484,23 +490,23 @@ static void python_process_tracepoint(struct perf_sample *sample,
484490
} else { /* FIELD_IS_NUMERIC */
485491
obj = get_field_numeric_entry(event, field, data);
486492
}
487-
if (handler)
493+
if (!dict)
488494
PyTuple_SetItem(t, n++, obj);
489495
else
490496
pydict_set_item_string_decref(dict, field->name, obj);
491497

492498
}
493499

494-
if (!handler)
500+
if (dict)
495501
PyTuple_SetItem(t, n++, dict);
496502

497503
if (_PyTuple_Resize(&t, n) == -1)
498504
Py_FatalError("error resizing Python tuple");
499505

500-
if (handler) {
506+
if (!dict) {
501507
call_object(handler, t, handler_name);
502508
} else {
503-
try_call_object("trace_unhandled", t);
509+
call_object(handler, t, default_handler_name);
504510
Py_DECREF(dict);
505511
}
506512

@@ -799,6 +805,12 @@ static void python_process_general_event(struct perf_sample *sample,
799805
static char handler_name[64];
800806
unsigned n = 0;
801807

808+
snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
809+
810+
handler = get_handler(handler_name);
811+
if (!handler)
812+
return;
813+
802814
/*
803815
* Use the MAX_FIELDS to make the function expandable, though
804816
* currently there is only one item for the tuple.
@@ -815,12 +827,6 @@ static void python_process_general_event(struct perf_sample *sample,
815827
if (!dict_sample)
816828
Py_FatalError("couldn't create Python dictionary");
817829

818-
snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
819-
820-
handler = get_handler(handler_name);
821-
if (!handler)
822-
goto exit;
823-
824830
pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
825831
pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
826832
(const char *)&evsel->attr, sizeof(evsel->attr)));
@@ -861,7 +867,7 @@ static void python_process_general_event(struct perf_sample *sample,
861867
Py_FatalError("error resizing Python tuple");
862868

863869
call_object(handler, t, handler_name);
864-
exit:
870+
865871
Py_DECREF(dict);
866872
Py_DECREF(t);
867873
}

0 commit comments

Comments
 (0)