diff options
| author | Adrian Hunter <adrian.hunter@intel.com> | 2014-07-31 02:01:01 -0400 |
|---|---|---|
| committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2014-08-13 18:22:01 -0400 |
| commit | a5563edfa1bd25d052d81f5ad7fe74ba71c3d44e (patch) | |
| tree | dc2928a8d8de33be19aa129865e591b0198e2205 /tools/perf/util/scripting-engines/trace-event-python.c | |
| parent | 98526ee7229be8537373aebe037b74cac112d84b (diff) | |
perf script python: Add helpers for calling Python objects
The Python script API repeatedly uses the same lines of code to get and
call objects. Make that into helper functions instead.
A side-effect is that some reference counting bugs disappear because the
new call_object() function always decrements the reference count of
'retval'.
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1406786474-9306-19-git-send-email-adrian.hunter@intel.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.c | 114 |
1 files changed, 47 insertions, 67 deletions
diff --git a/tools/perf/util/scripting-engines/trace-event-python.c b/tools/perf/util/scripting-engines/trace-event-python.c index cbce2545da45..26e5f14239ed 100644 --- a/tools/perf/util/scripting-engines/trace-event-python.c +++ b/tools/perf/util/scripting-engines/trace-event-python.c | |||
| @@ -73,6 +73,35 @@ static void pydict_set_item_string_decref(PyObject *dict, const char *key, PyObj | |||
| 73 | Py_DECREF(val); | 73 | Py_DECREF(val); |
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | static PyObject *get_handler(const char *handler_name) | ||
| 77 | { | ||
| 78 | PyObject *handler; | ||
| 79 | |||
| 80 | handler = PyDict_GetItemString(main_dict, handler_name); | ||
| 81 | if (handler && !PyCallable_Check(handler)) | ||
| 82 | return NULL; | ||
| 83 | return handler; | ||
| 84 | } | ||
| 85 | |||
| 86 | static void call_object(PyObject *handler, PyObject *args, const char *die_msg) | ||
| 87 | { | ||
| 88 | PyObject *retval; | ||
| 89 | |||
| 90 | retval = PyObject_CallObject(handler, args); | ||
| 91 | if (retval == NULL) | ||
| 92 | handler_call_die(die_msg); | ||
| 93 | Py_DECREF(retval); | ||
| 94 | } | ||
| 95 | |||
| 96 | static void try_call_object(const char *handler_name, PyObject *args) | ||
| 97 | { | ||
| 98 | PyObject *handler; | ||
| 99 | |||
| 100 | handler = get_handler(handler_name); | ||
| 101 | if (handler) | ||
| 102 | call_object(handler, args, handler_name); | ||
| 103 | } | ||
| 104 | |||
| 76 | static void define_value(enum print_arg_type field_type, | 105 | static void define_value(enum print_arg_type field_type, |
| 77 | const char *ev_name, | 106 | const char *ev_name, |
| 78 | const char *field_name, | 107 | const char *field_name, |
| @@ -80,7 +109,7 @@ static void define_value(enum print_arg_type field_type, | |||
| 80 | const char *field_str) | 109 | const char *field_str) |
| 81 | { | 110 | { |
| 82 | const char *handler_name = "define_flag_value"; | 111 | const char *handler_name = "define_flag_value"; |
| 83 | PyObject *handler, *t, *retval; | 112 | PyObject *t; |
| 84 | unsigned long long value; | 113 | unsigned long long value; |
| 85 | unsigned n = 0; | 114 | unsigned n = 0; |
| 86 | 115 | ||
| @@ -98,13 +127,7 @@ static void define_value(enum print_arg_type field_type, | |||
| 98 | PyTuple_SetItem(t, n++, PyInt_FromLong(value)); | 127 | PyTuple_SetItem(t, n++, PyInt_FromLong(value)); |
| 99 | PyTuple_SetItem(t, n++, PyString_FromString(field_str)); | 128 | PyTuple_SetItem(t, n++, PyString_FromString(field_str)); |
| 100 | 129 | ||
| 101 | handler = PyDict_GetItemString(main_dict, handler_name); | 130 | try_call_object(handler_name, t); |
| 102 | if (handler && PyCallable_Check(handler)) { | ||
| 103 | retval = PyObject_CallObject(handler, t); | ||
| 104 | if (retval == NULL) | ||
| 105 | handler_call_die(handler_name); | ||
| 106 | Py_DECREF(retval); | ||
| 107 | } | ||
| 108 | 131 | ||
| 109 | Py_DECREF(t); | 132 | Py_DECREF(t); |
| 110 | } | 133 | } |
| @@ -127,7 +150,7 @@ static void define_field(enum print_arg_type field_type, | |||
| 127 | const char *delim) | 150 | const char *delim) |
| 128 | { | 151 | { |
| 129 | const char *handler_name = "define_flag_field"; | 152 | const char *handler_name = "define_flag_field"; |
| 130 | PyObject *handler, *t, *retval; | 153 | PyObject *t; |
| 131 | unsigned n = 0; | 154 | unsigned n = 0; |
| 132 | 155 | ||
| 133 | if (field_type == PRINT_SYMBOL) | 156 | if (field_type == PRINT_SYMBOL) |
| @@ -145,13 +168,7 @@ static void define_field(enum print_arg_type field_type, | |||
| 145 | if (field_type == PRINT_FLAGS) | 168 | if (field_type == PRINT_FLAGS) |
| 146 | PyTuple_SetItem(t, n++, PyString_FromString(delim)); | 169 | PyTuple_SetItem(t, n++, PyString_FromString(delim)); |
| 147 | 170 | ||
| 148 | handler = PyDict_GetItemString(main_dict, handler_name); | 171 | try_call_object(handler_name, t); |
| 149 | if (handler && PyCallable_Check(handler)) { | ||
| 150 | retval = PyObject_CallObject(handler, t); | ||
| 151 | if (retval == NULL) | ||
| 152 | handler_call_die(handler_name); | ||
| 153 | Py_DECREF(retval); | ||
| 154 | } | ||
| 155 | 172 | ||
| 156 | Py_DECREF(t); | 173 | Py_DECREF(t); |
| 157 | } | 174 | } |
| @@ -362,7 +379,7 @@ static void python_process_tracepoint(struct perf_sample *sample, | |||
| 362 | struct thread *thread, | 379 | struct thread *thread, |
| 363 | struct addr_location *al) | 380 | struct addr_location *al) |
| 364 | { | 381 | { |
| 365 | PyObject *handler, *retval, *context, *t, *obj, *callchain; | 382 | PyObject *handler, *context, *t, *obj, *callchain; |
| 366 | PyObject *dict = NULL; | 383 | PyObject *dict = NULL; |
| 367 | static char handler_name[256]; | 384 | static char handler_name[256]; |
| 368 | struct format_field *field; | 385 | struct format_field *field; |
| @@ -387,9 +404,7 @@ static void python_process_tracepoint(struct perf_sample *sample, | |||
| 387 | 404 | ||
| 388 | sprintf(handler_name, "%s__%s", event->system, event->name); | 405 | sprintf(handler_name, "%s__%s", event->system, event->name); |
| 389 | 406 | ||
| 390 | handler = PyDict_GetItemString(main_dict, handler_name); | 407 | handler = get_handler(handler_name); |
| 391 | if (handler && !PyCallable_Check(handler)) | ||
| 392 | handler = NULL; | ||
| 393 | if (!handler) { | 408 | if (!handler) { |
| 394 | dict = PyDict_New(); | 409 | dict = PyDict_New(); |
| 395 | if (!dict) | 410 | if (!dict) |
| @@ -450,19 +465,9 @@ static void python_process_tracepoint(struct perf_sample *sample, | |||
| 450 | Py_FatalError("error resizing Python tuple"); | 465 | Py_FatalError("error resizing Python tuple"); |
| 451 | 466 | ||
| 452 | if (handler) { | 467 | if (handler) { |
| 453 | retval = PyObject_CallObject(handler, t); | 468 | call_object(handler, t, handler_name); |
| 454 | if (retval == NULL) | ||
| 455 | handler_call_die(handler_name); | ||
| 456 | Py_DECREF(retval); | ||
| 457 | } else { | 469 | } else { |
| 458 | handler = PyDict_GetItemString(main_dict, "trace_unhandled"); | 470 | try_call_object("trace_unhandled", t); |
| 459 | if (handler && PyCallable_Check(handler)) { | ||
| 460 | |||
| 461 | retval = PyObject_CallObject(handler, t); | ||
| 462 | if (retval == NULL) | ||
| 463 | handler_call_die("trace_unhandled"); | ||
| 464 | Py_DECREF(retval); | ||
| 465 | } | ||
| 466 | Py_DECREF(dict); | 471 | Py_DECREF(dict); |
| 467 | } | 472 | } |
| 468 | 473 | ||
| @@ -474,7 +479,7 @@ static void python_process_general_event(struct perf_sample *sample, | |||
| 474 | struct thread *thread, | 479 | struct thread *thread, |
| 475 | struct addr_location *al) | 480 | struct addr_location *al) |
| 476 | { | 481 | { |
| 477 | PyObject *handler, *retval, *t, *dict, *callchain, *dict_sample; | 482 | PyObject *handler, *t, *dict, *callchain, *dict_sample; |
| 478 | static char handler_name[64]; | 483 | static char handler_name[64]; |
| 479 | unsigned n = 0; | 484 | unsigned n = 0; |
| 480 | 485 | ||
| @@ -496,8 +501,8 @@ static void python_process_general_event(struct perf_sample *sample, | |||
| 496 | 501 | ||
| 497 | snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); | 502 | snprintf(handler_name, sizeof(handler_name), "%s", "process_event"); |
| 498 | 503 | ||
| 499 | handler = PyDict_GetItemString(main_dict, handler_name); | 504 | handler = get_handler(handler_name); |
| 500 | if (!handler || !PyCallable_Check(handler)) | 505 | if (!handler) |
| 501 | goto exit; | 506 | goto exit; |
| 502 | 507 | ||
| 503 | pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); | 508 | pydict_set_item_string_decref(dict, "ev_name", PyString_FromString(perf_evsel__name(evsel))); |
| @@ -539,10 +544,7 @@ static void python_process_general_event(struct perf_sample *sample, | |||
| 539 | if (_PyTuple_Resize(&t, n) == -1) | 544 | if (_PyTuple_Resize(&t, n) == -1) |
| 540 | Py_FatalError("error resizing Python tuple"); | 545 | Py_FatalError("error resizing Python tuple"); |
| 541 | 546 | ||
| 542 | retval = PyObject_CallObject(handler, t); | 547 | call_object(handler, t, handler_name); |
| 543 | if (retval == NULL) | ||
| 544 | handler_call_die(handler_name); | ||
| 545 | Py_DECREF(retval); | ||
| 546 | exit: | 548 | exit: |
| 547 | Py_DECREF(dict); | 549 | Py_DECREF(dict); |
| 548 | Py_DECREF(t); | 550 | Py_DECREF(t); |
| @@ -566,36 +568,24 @@ static void python_process_event(union perf_event *event __maybe_unused, | |||
| 566 | 568 | ||
| 567 | static int run_start_sub(void) | 569 | static int run_start_sub(void) |
| 568 | { | 570 | { |
| 569 | PyObject *handler, *retval; | ||
| 570 | int err = 0; | ||
| 571 | |||
| 572 | main_module = PyImport_AddModule("__main__"); | 571 | main_module = PyImport_AddModule("__main__"); |
| 573 | if (main_module == NULL) | 572 | if (main_module == NULL) |
| 574 | return -1; | 573 | return -1; |
| 575 | Py_INCREF(main_module); | 574 | Py_INCREF(main_module); |
| 576 | 575 | ||
| 577 | main_dict = PyModule_GetDict(main_module); | 576 | main_dict = PyModule_GetDict(main_module); |
| 578 | if (main_dict == NULL) { | 577 | if (main_dict == NULL) |
| 579 | err = -1; | ||
| 580 | goto error; | 578 | goto error; |
| 581 | } | ||
| 582 | Py_INCREF(main_dict); | 579 | Py_INCREF(main_dict); |
| 583 | 580 | ||
| 584 | handler = PyDict_GetItemString(main_dict, "trace_begin"); | 581 | try_call_object("trace_begin", NULL); |
| 585 | if (handler == NULL || !PyCallable_Check(handler)) | ||
| 586 | goto out; | ||
| 587 | 582 | ||
| 588 | retval = PyObject_CallObject(handler, NULL); | 583 | return 0; |
| 589 | if (retval == NULL) | ||
| 590 | handler_call_die("trace_begin"); | ||
| 591 | 584 | ||
| 592 | Py_DECREF(retval); | ||
| 593 | return err; | ||
| 594 | error: | 585 | error: |
| 595 | Py_XDECREF(main_dict); | 586 | Py_XDECREF(main_dict); |
| 596 | Py_XDECREF(main_module); | 587 | Py_XDECREF(main_module); |
| 597 | out: | 588 | return -1; |
| 598 | return err; | ||
| 599 | } | 589 | } |
| 600 | 590 | ||
| 601 | /* | 591 | /* |
| @@ -654,23 +644,13 @@ error: | |||
| 654 | */ | 644 | */ |
| 655 | static int python_stop_script(void) | 645 | static int python_stop_script(void) |
| 656 | { | 646 | { |
| 657 | PyObject *handler, *retval; | 647 | try_call_object("trace_end", NULL); |
| 658 | int err = 0; | ||
| 659 | 648 | ||
| 660 | handler = PyDict_GetItemString(main_dict, "trace_end"); | ||
| 661 | if (handler == NULL || !PyCallable_Check(handler)) | ||
| 662 | goto out; | ||
| 663 | |||
| 664 | retval = PyObject_CallObject(handler, NULL); | ||
| 665 | if (retval == NULL) | ||
| 666 | handler_call_die("trace_end"); | ||
| 667 | Py_DECREF(retval); | ||
| 668 | out: | ||
| 669 | Py_XDECREF(main_dict); | 649 | Py_XDECREF(main_dict); |
| 670 | Py_XDECREF(main_module); | 650 | Py_XDECREF(main_module); |
| 671 | Py_Finalize(); | 651 | Py_Finalize(); |
| 672 | 652 | ||
| 673 | return err; | 653 | return 0; |
| 674 | } | 654 | } |
| 675 | 655 | ||
| 676 | static int python_generate_script(struct pevent *pevent, const char *outfile) | 656 | static int python_generate_script(struct pevent *pevent, const char *outfile) |
