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