cpython: 66db0c66a6ee (original) (raw)
Mercurial > cpython
changeset 87402:66db0c66a6ee
Issue #18874: Remove tracemalloc.set_traceback_limit() tracemalloc.start() now has an option nframe parameter [#18874]
Victor Stinner victor.stinner@gmail.com | |
---|---|
date | Sat, 23 Nov 2013 12:37:20 +0100 |
parents | 6e2089dbc5ad |
children | 2e3bee6e682b |
files | Doc/library/tracemalloc.rst Doc/using/cmdline.rst Lib/test/test_tracemalloc.py Modules/_tracemalloc.c |
diffstat | 4 files changed, 52 insertions(+), 72 deletions(-)[+] [-] Doc/library/tracemalloc.rst 36 Doc/using/cmdline.rst 18 Lib/test/test_tracemalloc.py 21 Modules/_tracemalloc.c 49 |
line wrap: on
line diff
--- a/Doc/library/tracemalloc.rst
+++ b/Doc/library/tracemalloc.rst
@@ -21,8 +21,7 @@ start tracing Python memory allocations.
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
:envvar:PYTHONTRACEMALLOC
environment variable to 25
, or use the
-:option:-X
tracemalloc=25
command line option. The
-:func:set_traceback_limit
function can be used at runtime to set the limit.
+:option:-X
tracemalloc=25
command line option.
.. versionadded:: 3.4
@@ -120,8 +119,8 @@ Code to display the traceback of the big
import linecache
import tracemalloc
# ... run your application ... @@ -267,10 +266,10 @@ Functions Get the maximum number of frames stored in the traceback of a trace.
- The :mod:
tracemalloc
module must be tracing memory allocations to - get the limit, otherwise an exception is raised.
.. function:: get_traced_memory()
@@ -294,10 +293,12 @@ 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.
- nframe must be greater or equal to
1
.
- Start tracing Python memory allocations: install hooks on Python memory
- 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 to1
. Storing more than1
frame is only useful to compute statistics grouped by'traceback'
or to compute cumulative statistics: see the @@ -309,17 +310,10 @@ Functions The :envvar:PYTHONTRACEMALLOC
environment variable (PYTHONTRACEMALLOC=NFRAME
) and the :option:-X
tracemalloc=NFRAME
- command line option can be used to set the limit at startup. -
- Use the :func:
get_traceback_limit
function to get the current limit. -
- Start tracing Python memory allocations: install hooks on Python memory
- allocators. -
- See also :func:
stop
and :func:is_tracing
functions.
.. function:: stop()
@@ -342,7 +336,7 @@ Functions
:mod:tracemalloc
module started to trace memory allocations.
Tracebacks of traces are limited to :func:get_traceback_limit
frames. Use
- the nframe parameter of the :func:
start
function to store more frames. The :mod:tracemalloc
module must be tracing memory allocations to take a snapshot, see the the :func:start
function.
--- a/Doc/using/cmdline.rst
+++ b/Doc/using/cmdline.rst
@@ -381,10 +381,11 @@ Miscellaneous options
* -X faulthandler
to enable :mod:faulthandler
;
* -X showrefcount
to enable the output of the total reference count
and memory blocks (only works on debug builds);
maximum number of frames stored in a trace: see the[](#l2.9)
:func:`tracemalloc.set_traceback_limit` function.[](#l2.10)
:mod:`tracemalloc` module. By default, only the most recent frame is[](#l2.12)
stored in a traceback of a trace. Use ``-X tracemalloc=NFRAME`` to start[](#l2.13)
tracing with a traceback limit of *NFRAME* frames. See the[](#l2.14)
:func:`tracemalloc.start` for more information.[](#l2.15)
It also allows to pass arbitrary values and retrieve them through the
:data:sys._xoptions
dictionary.
@@ -600,10 +601,11 @@ conflict.
.. envvar:: PYTHONTRACEMALLOC
- If this environment variable is set to a non-empty string, all memory
- allocations made by Python are traced by the :mod:
tracemalloc
module. - The value of the variable is the maximum number of frames stored in a trace:
- see the :func:
tracemalloc.set_traceback_limit
function.
- If this environment variable is set to a non-empty string, start tracing
- Python memory allocations using the :mod:
tracemalloc
module. The value of - the variable is the maximum number of frames stored in a traceback of a
- trace. For example,
PYTHONTRACEMALLOC=1
stores only the most recent - frame. See the :func:
tracemalloc.start
for more information. .. versionadded:: 3.4
--- a/Lib/test/test_tracemalloc.py +++ b/Lib/test/test_tracemalloc.py @@ -83,8 +83,7 @@ class TestTracemallocEnabled(unittest.Te if tracemalloc.is_tracing(): self.skipTest("tracemalloc must be stopped before the test")
tracemalloc.set_traceback_limit(1)[](#l3.7)
tracemalloc.start()[](#l3.8)
tracemalloc.start(1)[](#l3.9)
def tearDown(self): tracemalloc.stop() @@ -109,20 +108,18 @@ class TestTracemallocEnabled(unittest.Te def test_set_traceback_limit(self): obj_size = 10
nframe = tracemalloc.get_traceback_limit()[](#l3.17)
self.addCleanup(tracemalloc.set_traceback_limit, nframe)[](#l3.18)
tracemalloc.stop()[](#l3.19)
self.assertRaises(ValueError, tracemalloc.start, -1)[](#l3.20)
self.assertRaises(ValueError, tracemalloc.set_traceback_limit, -1)[](#l3.22)
tracemalloc.clear_traces()[](#l3.24)
tracemalloc.set_traceback_limit(10)[](#l3.25)
tracemalloc.stop()[](#l3.26)
tracemalloc.start(10)[](#l3.27) obj2, obj2_traceback = allocate_bytes(obj_size)[](#l3.28) traceback = tracemalloc.get_object_traceback(obj2)[](#l3.29) self.assertEqual(len(traceback), 10)[](#l3.30) self.assertEqual(traceback, obj2_traceback)[](#l3.31)
tracemalloc.clear_traces()[](#l3.33)
tracemalloc.set_traceback_limit(1)[](#l3.34)
tracemalloc.stop()[](#l3.35)
tracemalloc.start(1)[](#l3.36) obj, obj_traceback = allocate_bytes(obj_size)[](#l3.37) traceback = tracemalloc.get_object_traceback(obj)[](#l3.38) self.assertEqual(len(traceback), 1)[](#l3.39)
@@ -163,8 +160,8 @@ class TestTracemallocEnabled(unittest.Te return allocate_bytes3(size) # Ensure that two identical tracebacks are not duplicated
tracemalloc.clear_traces()[](#l3.44)
tracemalloc.set_traceback_limit(4)[](#l3.45)
tracemalloc.stop()[](#l3.46)
tracemalloc.start(4)[](#l3.47) obj_size = 123[](#l3.48) obj1, obj1_traceback = allocate_bytes4(obj_size)[](#l3.49) obj2, obj2_traceback = allocate_bytes4(obj_size)[](#l3.50)
--- a/Modules/_tracemalloc.c +++ b/Modules/_tracemalloc.c @@ -1151,13 +1151,27 @@ done: } PyDoc_STRVAR(tracemalloc_start_doc,
- "Start tracing Python memory allocations. Set also the maximum number \n"
- "of frames stored in the traceback of a trace to nframe.");
static PyObject* -py_tracemalloc_start(PyObject *self) +py_tracemalloc_start(PyObject *self, PyObject *args) {
- if (nframe < 1 || nframe > MAX_NFRAME) {
PyErr_Format(PyExc_ValueError,[](#l4.24)
"the number of frames must be in range [1; %i]",[](#l4.25)
MAX_NFRAME);[](#l4.26)
return NULL;[](#l4.27)
- }
- tracemalloc_config.max_nframe = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
+ if (tracemalloc_start() < 0) return NULL; @@ -1192,31 +1206,6 @@ py_tracemalloc_get_traceback_limit(PyObj 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) -{
- if (nframe < 1 || nframe > MAX_NFRAME) {
PyErr_Format(PyExc_ValueError,[](#l4.53)
"the number of frames must be in range [1; %i]",[](#l4.54)
MAX_NFRAME);[](#l4.55)
return NULL;[](#l4.56)
- }
- tracemalloc_config.max_nframe = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
-} - PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc, "get_tracemalloc_memory() -> int\n" "\n" @@ -1275,13 +1264,11 @@ static PyMethodDef module_methods[] = { {"_get_object_traceback", (PyCFunction)py_tracemalloc_get_object_traceback, METH_O, tracemalloc_get_object_traceback_doc}, {"start", (PyCFunction)py_tracemalloc_start,
METH_NOARGS, tracemalloc_start_doc},[](#l4.70)
{"stop", (PyCFunction)py_tracemalloc_stop, METH_NOARGS, tracemalloc_stop_doc}, {"get_traceback_limit", (PyCFunction)py_tracemalloc_get_traceback_limit, METH_NOARGS, tracemalloc_get_traceback_limit_doc},METH_VARARGS, tracemalloc_start_doc},[](#l4.71)
- {"set_traceback_limit", (PyCFunction)tracemalloc_set_traceback_limit,
{"get_tracemalloc_memory", (PyCFunction)tracemalloc_get_tracemalloc_memory, METH_NOARGS, tracemalloc_get_tracemalloc_memory_doc}, {"get_traced_memory", (PyCFunction)tracemalloc_get_traced_memory,METH_VARARGS, tracemalloc_set_traceback_limit_doc},[](#l4.77)