Issue #18874: Remove tracemalloc.set_traceback_limit()
tracemalloc.start() now has an option nframe parameter
This commit is contained in:
parent
ed3b0bca3e
commit
3728d6ced0
|
@ -21,8 +21,7 @@ start tracing Python memory allocations.
|
||||||
By default, a trace of an allocated memory block only stores the most recent
|
By default, a trace of an allocated memory block only stores the most recent
|
||||||
frame (1 frame). To store 25 frames at startup: set the
|
frame (1 frame). To store 25 frames at startup: set the
|
||||||
:envvar:`PYTHONTRACEMALLOC` environment variable to ``25``, or use the
|
:envvar:`PYTHONTRACEMALLOC` environment variable to ``25``, or use the
|
||||||
:option:`-X` ``tracemalloc=25`` command line option. The
|
:option:`-X` ``tracemalloc=25`` command line option.
|
||||||
:func:`set_traceback_limit` function can be used at runtime to set the limit.
|
|
||||||
|
|
||||||
.. versionadded:: 3.4
|
.. versionadded:: 3.4
|
||||||
|
|
||||||
|
@ -120,8 +119,8 @@ Code to display the traceback of the biggest memory block::
|
||||||
import linecache
|
import linecache
|
||||||
import tracemalloc
|
import tracemalloc
|
||||||
|
|
||||||
tracemalloc.set_traceback_limit(25)
|
# Store 25 frames
|
||||||
tracemalloc.start()
|
tracemalloc.start(25)
|
||||||
|
|
||||||
# ... run your application ...
|
# ... run your application ...
|
||||||
|
|
||||||
|
@ -267,10 +266,10 @@ Functions
|
||||||
|
|
||||||
Get the maximum number of frames stored in the traceback of a trace.
|
Get the maximum number of frames stored in the traceback of a trace.
|
||||||
|
|
||||||
By default, a trace of a memory block only stores the most recent
|
The :mod:`tracemalloc` module must be tracing memory allocations to
|
||||||
frame: the limit is ``1``.
|
get the limit, otherwise an exception is raised.
|
||||||
|
|
||||||
Use the :func:`set_traceback_limit` function to change the limit.
|
The limit is set by the :func:`start` function.
|
||||||
|
|
||||||
|
|
||||||
.. function:: get_traced_memory()
|
.. function:: get_traced_memory()
|
||||||
|
@ -294,10 +293,12 @@ Functions
|
||||||
See also :func:`start` and :func:`stop` functions.
|
See also :func:`start` and :func:`stop` functions.
|
||||||
|
|
||||||
|
|
||||||
.. function:: set_traceback_limit(nframe: int)
|
.. function:: start(nframe: int=1)
|
||||||
|
|
||||||
Set the maximum number of frames stored in the traceback of a trace.
|
Start tracing Python memory allocations: install hooks on Python memory
|
||||||
*nframe* must be greater or equal to ``1``.
|
allocators. Collected tracebacks of traces will be limited to *nframe*
|
||||||
|
frames. By default, a trace of a memory block only stores the most recent
|
||||||
|
frame: the limit is ``1``. *nframe* must be greater or equal to ``1``.
|
||||||
|
|
||||||
Storing more than ``1`` frame is only useful to compute statistics grouped
|
Storing more than ``1`` frame is only useful to compute statistics grouped
|
||||||
by ``'traceback'`` or to compute cumulative statistics: see the
|
by ``'traceback'`` or to compute cumulative statistics: see the
|
||||||
|
@ -309,17 +310,10 @@ Functions
|
||||||
|
|
||||||
The :envvar:`PYTHONTRACEMALLOC` environment variable
|
The :envvar:`PYTHONTRACEMALLOC` environment variable
|
||||||
(``PYTHONTRACEMALLOC=NFRAME``) and the :option:`-X` ``tracemalloc=NFRAME``
|
(``PYTHONTRACEMALLOC=NFRAME``) and the :option:`-X` ``tracemalloc=NFRAME``
|
||||||
command line option can be used to set the limit at startup.
|
command line option can be used to start tracing at startup.
|
||||||
|
|
||||||
Use the :func:`get_traceback_limit` function to get the current limit.
|
See also :func:`stop`, :func:`is_tracing` and :func:`get_traceback_limit`
|
||||||
|
functions.
|
||||||
|
|
||||||
.. function:: start()
|
|
||||||
|
|
||||||
Start tracing Python memory allocations: install hooks on Python memory
|
|
||||||
allocators.
|
|
||||||
|
|
||||||
See also :func:`stop` and :func:`is_tracing` functions.
|
|
||||||
|
|
||||||
|
|
||||||
.. function:: stop()
|
.. function:: stop()
|
||||||
|
@ -342,7 +336,7 @@ Functions
|
||||||
:mod:`tracemalloc` module started to trace memory allocations.
|
:mod:`tracemalloc` module started to trace memory allocations.
|
||||||
|
|
||||||
Tracebacks of traces are limited to :func:`get_traceback_limit` frames. Use
|
Tracebacks of traces are limited to :func:`get_traceback_limit` frames. Use
|
||||||
:func:`set_traceback_limit` to store more frames.
|
the *nframe* parameter of the :func:`start` function to store more frames.
|
||||||
|
|
||||||
The :mod:`tracemalloc` module must be tracing memory allocations to take a
|
The :mod:`tracemalloc` module must be tracing memory allocations to take a
|
||||||
snapshot, see the the :func:`start` function.
|
snapshot, see the the :func:`start` function.
|
||||||
|
|
|
@ -381,10 +381,11 @@ Miscellaneous options
|
||||||
* ``-X faulthandler`` to enable :mod:`faulthandler`;
|
* ``-X faulthandler`` to enable :mod:`faulthandler`;
|
||||||
* ``-X showrefcount`` to enable the output of the total reference count
|
* ``-X showrefcount`` to enable the output of the total reference count
|
||||||
and memory blocks (only works on debug builds);
|
and memory blocks (only works on debug builds);
|
||||||
* ``-X tracemalloc`` to enable :mod:`tracemalloc`.
|
* ``-X tracemalloc`` to start tracing Python memory allocations using the
|
||||||
* ``-X tracemalloc=NFRAME`` to enable :mod:`tracemalloc`, *NFRAME* is the
|
:mod:`tracemalloc` module. By default, only the most recent frame is
|
||||||
maximum number of frames stored in a trace: see the
|
stored in a traceback of a trace. Use ``-X tracemalloc=NFRAME`` to start
|
||||||
:func:`tracemalloc.set_traceback_limit` function.
|
tracing with a traceback limit of *NFRAME* frames. See the
|
||||||
|
:func:`tracemalloc.start` for more information.
|
||||||
|
|
||||||
It also allows to pass arbitrary values and retrieve them through the
|
It also allows to pass arbitrary values and retrieve them through the
|
||||||
:data:`sys._xoptions` dictionary.
|
:data:`sys._xoptions` dictionary.
|
||||||
|
@ -600,10 +601,11 @@ conflict.
|
||||||
|
|
||||||
.. envvar:: PYTHONTRACEMALLOC
|
.. envvar:: PYTHONTRACEMALLOC
|
||||||
|
|
||||||
If this environment variable is set to a non-empty string, all memory
|
If this environment variable is set to a non-empty string, start tracing
|
||||||
allocations made by Python are traced by the :mod:`tracemalloc` module.
|
Python memory allocations using the :mod:`tracemalloc` module. The value of
|
||||||
The value of the variable is the maximum number of frames stored in a trace:
|
the variable is the maximum number of frames stored in a traceback of a
|
||||||
see the :func:`tracemalloc.set_traceback_limit` function.
|
trace. For example, ``PYTHONTRACEMALLOC=1`` stores only the most recent
|
||||||
|
frame. See the :func:`tracemalloc.start` for more information.
|
||||||
|
|
||||||
.. versionadded:: 3.4
|
.. versionadded:: 3.4
|
||||||
|
|
||||||
|
|
|
@ -83,8 +83,7 @@ class TestTracemallocEnabled(unittest.TestCase):
|
||||||
if tracemalloc.is_tracing():
|
if tracemalloc.is_tracing():
|
||||||
self.skipTest("tracemalloc must be stopped before the test")
|
self.skipTest("tracemalloc must be stopped before the test")
|
||||||
|
|
||||||
tracemalloc.set_traceback_limit(1)
|
tracemalloc.start(1)
|
||||||
tracemalloc.start()
|
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
tracemalloc.stop()
|
tracemalloc.stop()
|
||||||
|
@ -109,20 +108,18 @@ class TestTracemallocEnabled(unittest.TestCase):
|
||||||
def test_set_traceback_limit(self):
|
def test_set_traceback_limit(self):
|
||||||
obj_size = 10
|
obj_size = 10
|
||||||
|
|
||||||
nframe = tracemalloc.get_traceback_limit()
|
tracemalloc.stop()
|
||||||
self.addCleanup(tracemalloc.set_traceback_limit, nframe)
|
self.assertRaises(ValueError, tracemalloc.start, -1)
|
||||||
|
|
||||||
self.assertRaises(ValueError, tracemalloc.set_traceback_limit, -1)
|
tracemalloc.stop()
|
||||||
|
tracemalloc.start(10)
|
||||||
tracemalloc.clear_traces()
|
|
||||||
tracemalloc.set_traceback_limit(10)
|
|
||||||
obj2, obj2_traceback = allocate_bytes(obj_size)
|
obj2, obj2_traceback = allocate_bytes(obj_size)
|
||||||
traceback = tracemalloc.get_object_traceback(obj2)
|
traceback = tracemalloc.get_object_traceback(obj2)
|
||||||
self.assertEqual(len(traceback), 10)
|
self.assertEqual(len(traceback), 10)
|
||||||
self.assertEqual(traceback, obj2_traceback)
|
self.assertEqual(traceback, obj2_traceback)
|
||||||
|
|
||||||
tracemalloc.clear_traces()
|
tracemalloc.stop()
|
||||||
tracemalloc.set_traceback_limit(1)
|
tracemalloc.start(1)
|
||||||
obj, obj_traceback = allocate_bytes(obj_size)
|
obj, obj_traceback = allocate_bytes(obj_size)
|
||||||
traceback = tracemalloc.get_object_traceback(obj)
|
traceback = tracemalloc.get_object_traceback(obj)
|
||||||
self.assertEqual(len(traceback), 1)
|
self.assertEqual(len(traceback), 1)
|
||||||
|
@ -163,8 +160,8 @@ class TestTracemallocEnabled(unittest.TestCase):
|
||||||
return allocate_bytes3(size)
|
return allocate_bytes3(size)
|
||||||
|
|
||||||
# Ensure that two identical tracebacks are not duplicated
|
# Ensure that two identical tracebacks are not duplicated
|
||||||
tracemalloc.clear_traces()
|
tracemalloc.stop()
|
||||||
tracemalloc.set_traceback_limit(4)
|
tracemalloc.start(4)
|
||||||
obj_size = 123
|
obj_size = 123
|
||||||
obj1, obj1_traceback = allocate_bytes4(obj_size)
|
obj1, obj1_traceback = allocate_bytes4(obj_size)
|
||||||
obj2, obj2_traceback = allocate_bytes4(obj_size)
|
obj2, obj2_traceback = allocate_bytes4(obj_size)
|
||||||
|
|
|
@ -1151,13 +1151,27 @@ done:
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(tracemalloc_start_doc,
|
PyDoc_STRVAR(tracemalloc_start_doc,
|
||||||
"start()\n"
|
"start(nframe: int=1)\n"
|
||||||
"\n"
|
"\n"
|
||||||
"Start tracing Python memory allocations.");
|
"Start tracing Python memory allocations. Set also the maximum number \n"
|
||||||
|
"of frames stored in the traceback of a trace to nframe.");
|
||||||
|
|
||||||
static PyObject*
|
static PyObject*
|
||||||
py_tracemalloc_start(PyObject *self)
|
py_tracemalloc_start(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
|
Py_ssize_t nframe = 1;
|
||||||
|
|
||||||
|
if (!PyArg_ParseTuple(args, "|n:start", &nframe))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if (nframe < 1 || nframe > MAX_NFRAME) {
|
||||||
|
PyErr_Format(PyExc_ValueError,
|
||||||
|
"the number of frames must be in range [1; %i]",
|
||||||
|
MAX_NFRAME);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
tracemalloc_config.max_nframe = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
|
||||||
|
|
||||||
if (tracemalloc_start() < 0)
|
if (tracemalloc_start() < 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
@ -1192,31 +1206,6 @@ py_tracemalloc_get_traceback_limit(PyObject *self)
|
||||||
return PyLong_FromLong(tracemalloc_config.max_nframe);
|
return PyLong_FromLong(tracemalloc_config.max_nframe);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(tracemalloc_set_traceback_limit_doc,
|
|
||||||
"set_traceback_limit(nframe: int)\n"
|
|
||||||
"\n"
|
|
||||||
"Set the maximum number of frames stored in the traceback of a trace.");
|
|
||||||
|
|
||||||
static PyObject*
|
|
||||||
tracemalloc_set_traceback_limit(PyObject *self, PyObject *args)
|
|
||||||
{
|
|
||||||
Py_ssize_t nframe;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "n:set_traceback_limit",
|
|
||||||
&nframe))
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if (nframe < 1 || nframe > MAX_NFRAME) {
|
|
||||||
PyErr_Format(PyExc_ValueError,
|
|
||||||
"the number of frames must be in range [1; %i]",
|
|
||||||
MAX_NFRAME);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
tracemalloc_config.max_nframe = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
|
|
||||||
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc,
|
PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc,
|
||||||
"get_tracemalloc_memory() -> int\n"
|
"get_tracemalloc_memory() -> int\n"
|
||||||
"\n"
|
"\n"
|
||||||
|
@ -1275,13 +1264,11 @@ static PyMethodDef module_methods[] = {
|
||||||
{"_get_object_traceback", (PyCFunction)py_tracemalloc_get_object_traceback,
|
{"_get_object_traceback", (PyCFunction)py_tracemalloc_get_object_traceback,
|
||||||
METH_O, tracemalloc_get_object_traceback_doc},
|
METH_O, tracemalloc_get_object_traceback_doc},
|
||||||
{"start", (PyCFunction)py_tracemalloc_start,
|
{"start", (PyCFunction)py_tracemalloc_start,
|
||||||
METH_NOARGS, tracemalloc_start_doc},
|
METH_VARARGS, tracemalloc_start_doc},
|
||||||
{"stop", (PyCFunction)py_tracemalloc_stop,
|
{"stop", (PyCFunction)py_tracemalloc_stop,
|
||||||
METH_NOARGS, tracemalloc_stop_doc},
|
METH_NOARGS, tracemalloc_stop_doc},
|
||||||
{"get_traceback_limit", (PyCFunction)py_tracemalloc_get_traceback_limit,
|
{"get_traceback_limit", (PyCFunction)py_tracemalloc_get_traceback_limit,
|
||||||
METH_NOARGS, tracemalloc_get_traceback_limit_doc},
|
METH_NOARGS, tracemalloc_get_traceback_limit_doc},
|
||||||
{"set_traceback_limit", (PyCFunction)tracemalloc_set_traceback_limit,
|
|
||||||
METH_VARARGS, tracemalloc_set_traceback_limit_doc},
|
|
||||||
{"get_tracemalloc_memory", (PyCFunction)tracemalloc_get_tracemalloc_memory,
|
{"get_tracemalloc_memory", (PyCFunction)tracemalloc_get_tracemalloc_memory,
|
||||||
METH_NOARGS, tracemalloc_get_tracemalloc_memory_doc},
|
METH_NOARGS, tracemalloc_get_tracemalloc_memory_doc},
|
||||||
{"get_traced_memory", (PyCFunction)tracemalloc_get_traced_memory,
|
{"get_traced_memory", (PyCFunction)tracemalloc_get_traced_memory,
|
||||||
|
|
Loading…
Reference in New Issue