_tracemalloc.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include "Python.h"
  2. #include "clinic/_tracemalloc.c.h"
  3. /*[clinic input]
  4. module _tracemalloc
  5. [clinic start generated code]*/
  6. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=708a98302fc46e5f]*/
  7. /*[clinic input]
  8. _tracemalloc.is_tracing
  9. Return True if the tracemalloc module is tracing Python memory allocations.
  10. [clinic start generated code]*/
  11. static PyObject *
  12. _tracemalloc_is_tracing_impl(PyObject *module)
  13. /*[clinic end generated code: output=2d763b42601cd3ef input=af104b0a00192f63]*/
  14. {
  15. return PyBool_FromLong(_PyTraceMalloc_IsTracing());
  16. }
  17. /*[clinic input]
  18. _tracemalloc.clear_traces
  19. Clear traces of memory blocks allocated by Python.
  20. [clinic start generated code]*/
  21. static PyObject *
  22. _tracemalloc_clear_traces_impl(PyObject *module)
  23. /*[clinic end generated code: output=a86080ee41b84197 input=0dab5b6c785183a5]*/
  24. {
  25. _PyTraceMalloc_ClearTraces();
  26. Py_RETURN_NONE;
  27. }
  28. /*[clinic input]
  29. _tracemalloc._get_traces
  30. Get traces of all memory blocks allocated by Python.
  31. Return a list of (size: int, traceback: tuple) tuples.
  32. traceback is a tuple of (filename: str, lineno: int) tuples.
  33. Return an empty list if the tracemalloc module is disabled.
  34. [clinic start generated code]*/
  35. static PyObject *
  36. _tracemalloc__get_traces_impl(PyObject *module)
  37. /*[clinic end generated code: output=e9929876ced4b5cc input=6c7d2230b24255aa]*/
  38. {
  39. return _PyTraceMalloc_GetTraces();
  40. }
  41. /*[clinic input]
  42. _tracemalloc._get_object_traceback
  43. obj: object
  44. /
  45. Get the traceback where the Python object obj was allocated.
  46. Return a tuple of (filename: str, lineno: int) tuples.
  47. Return None if the tracemalloc module is disabled or did not
  48. trace the allocation of the object.
  49. [clinic start generated code]*/
  50. static PyObject *
  51. _tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
  52. /*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
  53. {
  54. return _PyTraceMalloc_GetObjectTraceback(obj);
  55. }
  56. /*[clinic input]
  57. _tracemalloc.start
  58. nframe: int = 1
  59. /
  60. Start tracing Python memory allocations.
  61. Also set the maximum number of frames stored in the traceback of a
  62. trace to nframe.
  63. [clinic start generated code]*/
  64. static PyObject *
  65. _tracemalloc_start_impl(PyObject *module, int nframe)
  66. /*[clinic end generated code: output=caae05c23c159d3c input=40d849b5b29d1933]*/
  67. {
  68. if (_PyTraceMalloc_Start(nframe) < 0) {
  69. return NULL;
  70. }
  71. Py_RETURN_NONE;
  72. }
  73. /*[clinic input]
  74. _tracemalloc.stop
  75. Stop tracing Python memory allocations.
  76. Also clear traces of memory blocks allocated by Python.
  77. [clinic start generated code]*/
  78. static PyObject *
  79. _tracemalloc_stop_impl(PyObject *module)
  80. /*[clinic end generated code: output=c3c42ae03e3955cd input=7478f075e51dae18]*/
  81. {
  82. _PyTraceMalloc_Stop();
  83. Py_RETURN_NONE;
  84. }
  85. /*[clinic input]
  86. _tracemalloc.get_traceback_limit
  87. Get the maximum number of frames stored in the traceback of a trace.
  88. By default, a trace of an allocated memory block only stores
  89. the most recent frame: the limit is 1.
  90. [clinic start generated code]*/
  91. static PyObject *
  92. _tracemalloc_get_traceback_limit_impl(PyObject *module)
  93. /*[clinic end generated code: output=d556d9306ba95567 input=da3cd977fc68ae3b]*/
  94. {
  95. return PyLong_FromLong(_PyTraceMalloc_GetTracebackLimit());
  96. }
  97. /*[clinic input]
  98. _tracemalloc.get_tracemalloc_memory
  99. Get the memory usage in bytes of the tracemalloc module.
  100. This memory is used internally to trace memory allocations.
  101. [clinic start generated code]*/
  102. static PyObject *
  103. _tracemalloc_get_tracemalloc_memory_impl(PyObject *module)
  104. /*[clinic end generated code: output=e3f14e280a55f5aa input=5d919c0f4d5132ad]*/
  105. {
  106. return PyLong_FromSize_t(_PyTraceMalloc_GetMemory());
  107. }
  108. /*[clinic input]
  109. _tracemalloc.get_traced_memory
  110. Get the current size and peak size of memory blocks traced by tracemalloc.
  111. Returns a tuple: (current: int, peak: int).
  112. [clinic start generated code]*/
  113. static PyObject *
  114. _tracemalloc_get_traced_memory_impl(PyObject *module)
  115. /*[clinic end generated code: output=5b167189adb9e782 input=61ddb5478400ff66]*/
  116. {
  117. return _PyTraceMalloc_GetTracedMemory();
  118. }
  119. /*[clinic input]
  120. _tracemalloc.reset_peak
  121. Set the peak size of memory blocks traced by tracemalloc to the current size.
  122. Do nothing if the tracemalloc module is not tracing memory allocations.
  123. [clinic start generated code]*/
  124. static PyObject *
  125. _tracemalloc_reset_peak_impl(PyObject *module)
  126. /*[clinic end generated code: output=140c2870f691dbb2 input=18afd0635066e9ce]*/
  127. {
  128. _PyTraceMalloc_ResetPeak();
  129. Py_RETURN_NONE;
  130. }
  131. static PyMethodDef module_methods[] = {
  132. _TRACEMALLOC_IS_TRACING_METHODDEF
  133. _TRACEMALLOC_CLEAR_TRACES_METHODDEF
  134. _TRACEMALLOC__GET_TRACES_METHODDEF
  135. _TRACEMALLOC__GET_OBJECT_TRACEBACK_METHODDEF
  136. _TRACEMALLOC_START_METHODDEF
  137. _TRACEMALLOC_STOP_METHODDEF
  138. _TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF
  139. _TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF
  140. _TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF
  141. _TRACEMALLOC_RESET_PEAK_METHODDEF
  142. /* sentinel */
  143. {NULL, NULL}
  144. };
  145. PyDoc_STRVAR(module_doc,
  146. "Debug module to trace memory blocks allocated by Python.");
  147. static struct PyModuleDef module_def = {
  148. PyModuleDef_HEAD_INIT,
  149. "_tracemalloc",
  150. module_doc,
  151. 0, /* non-negative size to be able to unload the module */
  152. module_methods,
  153. NULL,
  154. };
  155. PyMODINIT_FUNC
  156. PyInit__tracemalloc(void)
  157. {
  158. PyObject *m;
  159. m = PyModule_Create(&module_def);
  160. if (m == NULL)
  161. return NULL;
  162. if (_PyTraceMalloc_Init() < 0) {
  163. Py_DECREF(m);
  164. return NULL;
  165. }
  166. return m;
  167. }