Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 2 | /* |
Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 3 | * Context.c. Python interfaces for perf script. |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com> |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <Python.h> |
| 9 | #include "../../../perf.h" |
| 10 | #include "../../../util/trace-event.h" |
| 11 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 12 | #if PY_MAJOR_VERSION < 3 |
| 13 | #define _PyCapsule_GetPointer(arg1, arg2) \ |
| 14 | PyCObject_AsVoidPtr(arg1) |
| 15 | |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 16 | PyMODINIT_FUNC initperf_trace_context(void); |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 17 | #else |
| 18 | #define _PyCapsule_GetPointer(arg1, arg2) \ |
| 19 | PyCapsule_GetPointer((arg1), (arg2)) |
| 20 | |
| 21 | PyMODINIT_FUNC PyInit_perf_trace_context(void); |
| 22 | #endif |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 23 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 24 | static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args) |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 25 | { |
| 26 | static struct scripting_context *scripting_context; |
| 27 | PyObject *context; |
| 28 | int retval; |
| 29 | |
| 30 | if (!PyArg_ParseTuple(args, "O", &context)) |
| 31 | return NULL; |
| 32 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 33 | scripting_context = _PyCapsule_GetPointer(context, NULL); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 34 | retval = common_pc(scripting_context); |
| 35 | |
| 36 | return Py_BuildValue("i", retval); |
| 37 | } |
| 38 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 39 | static PyObject *perf_trace_context_common_flags(PyObject *obj, |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 40 | PyObject *args) |
| 41 | { |
| 42 | static struct scripting_context *scripting_context; |
| 43 | PyObject *context; |
| 44 | int retval; |
| 45 | |
| 46 | if (!PyArg_ParseTuple(args, "O", &context)) |
| 47 | return NULL; |
| 48 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 49 | scripting_context = _PyCapsule_GetPointer(context, NULL); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 50 | retval = common_flags(scripting_context); |
| 51 | |
| 52 | return Py_BuildValue("i", retval); |
| 53 | } |
| 54 | |
Arnaldo Carvalho de Melo | 316c713 | 2013-11-05 15:32:36 -0300 | [diff] [blame] | 55 | static PyObject *perf_trace_context_common_lock_depth(PyObject *obj, |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 56 | PyObject *args) |
| 57 | { |
| 58 | static struct scripting_context *scripting_context; |
| 59 | PyObject *context; |
| 60 | int retval; |
| 61 | |
| 62 | if (!PyArg_ParseTuple(args, "O", &context)) |
| 63 | return NULL; |
| 64 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 65 | scripting_context = _PyCapsule_GetPointer(context, NULL); |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 66 | retval = common_lock_depth(scripting_context); |
| 67 | |
| 68 | return Py_BuildValue("i", retval); |
| 69 | } |
| 70 | |
| 71 | static PyMethodDef ContextMethods[] = { |
| 72 | { "common_pc", perf_trace_context_common_pc, METH_VARARGS, |
| 73 | "Get the common preempt count event field value."}, |
| 74 | { "common_flags", perf_trace_context_common_flags, METH_VARARGS, |
| 75 | "Get the common flags event field value."}, |
| 76 | { "common_lock_depth", perf_trace_context_common_lock_depth, |
| 77 | METH_VARARGS, "Get the common lock depth event field value."}, |
| 78 | { NULL, NULL, 0, NULL} |
| 79 | }; |
| 80 | |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 81 | #if PY_MAJOR_VERSION < 3 |
Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 82 | PyMODINIT_FUNC initperf_trace_context(void) |
| 83 | { |
| 84 | (void) Py_InitModule("perf_trace_context", ContextMethods); |
| 85 | } |
Jaroslav Škarvada | 66dfdff | 2018-01-19 21:56:41 +0100 | [diff] [blame] | 86 | #else |
| 87 | PyMODINIT_FUNC PyInit_perf_trace_context(void) |
| 88 | { |
| 89 | static struct PyModuleDef moduledef = { |
| 90 | PyModuleDef_HEAD_INIT, |
| 91 | "perf_trace_context", /* m_name */ |
| 92 | "", /* m_doc */ |
| 93 | -1, /* m_size */ |
| 94 | ContextMethods, /* m_methods */ |
| 95 | NULL, /* m_reload */ |
| 96 | NULL, /* m_traverse */ |
| 97 | NULL, /* m_clear */ |
| 98 | NULL, /* m_free */ |
| 99 | }; |
| 100 | return PyModule_Create(&moduledef); |
| 101 | } |
| 102 | #endif |