aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/scripting-engines/trace-event-python.c
diff options
context:
space:
mode:
authorArun Kalyanasundaram <arunkaly@google.com>2017-07-21 18:04:18 -0400
committerArnaldo Carvalho de Melo <acme@redhat.com>2017-07-25 21:43:18 -0400
commite9f9a9ca8588e58dc0800b44adc41d32f6fc813a (patch)
treea03cb69cd02eace0efc142d5c19e97e468ca47c1 /tools/perf/util/scripting-engines/trace-event-python.c
parent2ec5cab604b2bbc5abe7138b537199762bea59ce (diff)
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> 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>
Diffstat (limited to 'tools/perf/util/scripting-engines/trace-event-python.c')
-rw-r--r--tools/perf/util/scripting-engines/trace-event-python.c38
1 files 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,
407 void *data = sample->raw_data; 407 void *data = sample->raw_data;
408 unsigned long long nsecs = sample->time; 408 unsigned long long nsecs = sample->time;
409 const char *comm = thread__comm_str(al->thread); 409 const char *comm = thread__comm_str(al->thread);
410 410 const char *default_handler_name = "trace_unhandled";
411 t = PyTuple_New(MAX_FIELDS);
412 if (!t)
413 Py_FatalError("couldn't create Python tuple");
414 411
415 if (!event) { 412 if (!event) {
416 snprintf(handler_name, sizeof(handler_name), 413 snprintf(handler_name, sizeof(handler_name),
@@ -427,10 +424,19 @@ static void python_process_tracepoint(struct perf_sample *sample,
427 424
428 handler = get_handler(handler_name); 425 handler = get_handler(handler_name);
429 if (!handler) { 426 if (!handler) {
427 handler = get_handler(default_handler_name);
428 if (!handler)
429 return;
430 dict = PyDict_New(); 430 dict = PyDict_New();
431 if (!dict) 431 if (!dict)
432 Py_FatalError("couldn't create Python dict"); 432 Py_FatalError("couldn't create Python dict");
433 } 433 }
434
435 t = PyTuple_New(MAX_FIELDS);
436 if (!t)
437 Py_FatalError("couldn't create Python tuple");
438
439
434 s = nsecs / NSEC_PER_SEC; 440 s = nsecs / NSEC_PER_SEC;
435 ns = nsecs - s * NSEC_PER_SEC; 441 ns = nsecs - s * NSEC_PER_SEC;
436 442
@@ -445,7 +451,7 @@ static void python_process_tracepoint(struct perf_sample *sample,
445 /* ip unwinding */ 451 /* ip unwinding */
446 callchain = python_process_callchain(sample, evsel, al); 452 callchain = python_process_callchain(sample, evsel, al);
447 453
448 if (handler) { 454 if (!dict) {
449 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu)); 455 PyTuple_SetItem(t, n++, PyInt_FromLong(cpu));
450 PyTuple_SetItem(t, n++, PyInt_FromLong(s)); 456 PyTuple_SetItem(t, n++, PyInt_FromLong(s));
451 PyTuple_SetItem(t, n++, PyInt_FromLong(ns)); 457 PyTuple_SetItem(t, n++, PyInt_FromLong(ns));
@@ -484,23 +490,23 @@ static void python_process_tracepoint(struct perf_sample *sample,
484 } else { /* FIELD_IS_NUMERIC */ 490 } else { /* FIELD_IS_NUMERIC */
485 obj = get_field_numeric_entry(event, field, data); 491 obj = get_field_numeric_entry(event, field, data);
486 } 492 }
487 if (handler) 493 if (!dict)
488 PyTuple_SetItem(t, n++, obj); 494 PyTuple_SetItem(t, n++, obj);
489 else 495 else
490 pydict_set_item_string_decref(dict, field->name, obj); 496 pydict_set_item_string_decref(dict, field->name, obj);
491 497
492 } 498 }
493 499
494 if (!handler) 500 if (dict)
495 PyTuple_SetItem(t, n++, dict); 501 PyTuple_SetItem(t, n++, dict);
496 502
497 if (_PyTuple_Resize(&t, n) == -1) 503 if (_PyTuple_Resize(&t, n) == -1)
498 Py_FatalError("error resizing Python tuple"); 504 Py_FatalError("error resizing Python tuple");
499 505
500 if (handler) { 506 if (!dict) {
501 call_object(handler, t, handler_name); 507 call_object(handler, t, handler_name);
502 } else { 508 } else {
503 try_call_object("trace_unhandled", t); 509 call_object(handler, t, default_handler_name);
504 Py_DECREF(dict); 510 Py_DECREF(dict);
505 } 511 }
506 512
@@ -799,6 +805,12 @@ static void python_process_general_event(struct perf_sample *sample,
799 static char handler_name[64]; 805 static char handler_name[64];
800 unsigned n = 0; 806 unsigned n = 0;
801 807
808 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
809
810 handler = get_handler(handler_name);
811 if (!handler)
812 return;
813
802 /* 814 /*
803 * Use the MAX_FIELDS to make the function expandable, though 815 * Use the MAX_FIELDS to make the function expandable, though
804 * currently there is only one item for the tuple. 816 * currently there is only one item for the tuple.
@@ -815,12 +827,6 @@ static void python_process_general_event(struct perf_sample *sample,
815 if (!dict_sample) 827 if (!dict_sample)
816 Py_FatalError("couldn't create Python dictionary"); 828 Py_FatalError("couldn't create Python dictionary");
817 829
818 snprintf(handler_name, sizeof(handler_name), "%s", "process_event");
819
820 handler = get_handler(handler_name);
821 if (!handler)
822 goto exit;
823
824 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); 830 pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel)));
825 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize( 831 pydict_set_item_string_decref(dict, "attr", PyString_FromStringAndSize(
826 (const char *)&evsel->attr, sizeof(evsel->attr))); 832 (const char *)&evsel->attr, sizeof(evsel->attr)));
@@ -861,7 +867,7 @@ static void python_process_general_event(struct perf_sample *sample,
861 Py_FatalError("error resizing Python tuple"); 867 Py_FatalError("error resizing Python tuple");
862 868
863 call_object(handler, t, handler_name); 869 call_object(handler, t, handler_name);
864exit: 870
865 Py_DECREF(dict); 871 Py_DECREF(dict);
866 Py_DECREF(t); 872 Py_DECREF(t);
867} 873}