Context.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Context.c. Python interfaces for perf script.
  3. *
  4. * Copyright (C) 2010 Tom Zanussi <tzanussi@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #include <Python.h>
  22. #include "../../../perf.h"
  23. #include "../../../util/trace-event.h"
  24. #if PY_MAJOR_VERSION < 3
  25. #define _PyCapsule_GetPointer(arg1, arg2) \
  26. PyCObject_AsVoidPtr(arg1)
  27. PyMODINIT_FUNC initperf_trace_context(void);
  28. #else
  29. #define _PyCapsule_GetPointer(arg1, arg2) \
  30. PyCapsule_GetPointer((arg1), (arg2))
  31. PyMODINIT_FUNC PyInit_perf_trace_context(void);
  32. #endif
  33. static PyObject *perf_trace_context_common_pc(PyObject *obj, PyObject *args)
  34. {
  35. static struct scripting_context *scripting_context;
  36. PyObject *context;
  37. int retval;
  38. if (!PyArg_ParseTuple(args, "O", &context))
  39. return NULL;
  40. scripting_context = _PyCapsule_GetPointer(context, NULL);
  41. retval = common_pc(scripting_context);
  42. return Py_BuildValue("i", retval);
  43. }
  44. static PyObject *perf_trace_context_common_flags(PyObject *obj,
  45. PyObject *args)
  46. {
  47. static struct scripting_context *scripting_context;
  48. PyObject *context;
  49. int retval;
  50. if (!PyArg_ParseTuple(args, "O", &context))
  51. return NULL;
  52. scripting_context = _PyCapsule_GetPointer(context, NULL);
  53. retval = common_flags(scripting_context);
  54. return Py_BuildValue("i", retval);
  55. }
  56. static PyObject *perf_trace_context_common_lock_depth(PyObject *obj,
  57. PyObject *args)
  58. {
  59. static struct scripting_context *scripting_context;
  60. PyObject *context;
  61. int retval;
  62. if (!PyArg_ParseTuple(args, "O", &context))
  63. return NULL;
  64. scripting_context = _PyCapsule_GetPointer(context, NULL);
  65. retval = common_lock_depth(scripting_context);
  66. return Py_BuildValue("i", retval);
  67. }
  68. static PyMethodDef ContextMethods[] = {
  69. { "common_pc", perf_trace_context_common_pc, METH_VARARGS,
  70. "Get the common preempt count event field value."},
  71. { "common_flags", perf_trace_context_common_flags, METH_VARARGS,
  72. "Get the common flags event field value."},
  73. { "common_lock_depth", perf_trace_context_common_lock_depth,
  74. METH_VARARGS, "Get the common lock depth event field value."},
  75. { NULL, NULL, 0, NULL}
  76. };
  77. #if PY_MAJOR_VERSION < 3
  78. PyMODINIT_FUNC initperf_trace_context(void)
  79. {
  80. (void) Py_InitModule("perf_trace_context", ContextMethods);
  81. }
  82. #else
  83. PyMODINIT_FUNC PyInit_perf_trace_context(void)
  84. {
  85. static struct PyModuleDef moduledef = {
  86. PyModuleDef_HEAD_INIT,
  87. "perf_trace_context", /* m_name */
  88. "", /* m_doc */
  89. -1, /* m_size */
  90. ContextMethods, /* m_methods */
  91. NULL, /* m_reload */
  92. NULL, /* m_traverse */
  93. NULL, /* m_clear */
  94. NULL, /* m_free */
  95. };
  96. return PyModule_Create(&moduledef);
  97. }
  98. #endif