Skip to content

bpo-40630: add tracemalloc.reset_peak #20102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Doc/library/tracemalloc.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,26 @@ Functions
:mod:`tracemalloc` module as a tuple: ``(current: int, peak: int)``.


.. function:: reset_peak()

Set the peak size of memory blocks traced by the :mod:`tracemalloc` module
to the current size.

The peak memory usage of a specific section of code can be determined as
follows::

tracemalloc.reset_peak()

# some code

current, peak = tracemalloc.get_traced_memory()

``peak`` will then be the peak size of traced memory blocks after the
:func:`reset_peak` call, even if there was a higher peak before that.

See also :func:`get_traced_memory`.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, you need to add ".. versionadded:: 3.10".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


.. function:: get_tracemalloc_memory()

Get the memory usage in bytes of the :mod:`tracemalloc` module used to store
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_tracemalloc.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,35 @@ def test_clear_traces(self):
traceback2 = tracemalloc.get_object_traceback(obj)
self.assertIsNone(traceback2)

def test_reset_peak(self):
max_error = 2048
def valid(size):
return range(size, size + max_error)

obj_size = 1024 * 1024

tracemalloc.clear_traces()

obj, obj_traceback = allocate_bytes(obj_size)
# add to the peak
allocate_bytes(obj_size)

size, peak_size = tracemalloc.get_traced_memory()
self.assertIn(peak_size, valid(2 * obj_size))

tracemalloc.reset_peak()

size2, peak_size2 = tracemalloc.get_traced_memory()
self.assertIn(size2, valid(size))
# the peak should include 'obj', but not the other allocation
self.assertIn(peak_size2, valid(size2))

# the peak should continue to be updated
allocate_bytes(obj_size)
size3, peak_size3 = tracemalloc.get_traced_memory()
self.assertIn(size3, valid(obj_size))
self.assertIn(peak_size3, valid(2 * obj_size))

def test_is_tracing(self):
tracemalloc.stop()
self.assertFalse(tracemalloc.is_tracing())
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1860,6 +1860,7 @@ Alex Willmer
David Wilson
Geoff Wilson
Greg V. Wilson
Huon Wilson
J Derek Wilson
Paul Winkler
Jody Winston
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added :func:`tracemalloc.reset_peak` to set the peak size of traced memory
blocks to the current size, to measure the peak of specific pieces of code.
23 changes: 23 additions & 0 deletions Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,28 @@ _tracemalloc_get_traced_memory_impl(PyObject *module)
return Py_BuildValue("nn", size, peak_size);
}

/*[clinic input]
_tracemalloc.reset_peak

Set the peak size of memory blocks traced by tracemalloc to the current size.

[clinic start generated code]*/

static PyObject *
_tracemalloc_reset_peak_impl(PyObject *module)
/*[clinic end generated code: output=140c2870f691dbb2 input=5a7e962ebebf9836]*/
{
if (!_Py_tracemalloc_config.tracing) {
Py_RETURN_NONE;
}

TABLES_LOCK();
tracemalloc_peak_traced_memory = tracemalloc_traced_memory;
TABLES_UNLOCK();

Py_RETURN_NONE;
}


static PyMethodDef module_methods[] = {
_TRACEMALLOC_IS_TRACING_METHODDEF
Expand All @@ -1654,6 +1676,7 @@ static PyMethodDef module_methods[] = {
_TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF
_TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF
_TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF
_TRACEMALLOC_RESET_PEAK_METHODDEF
/* sentinel */
{NULL, NULL}
};
Expand Down
20 changes: 19 additions & 1 deletion Modules/clinic/_tracemalloc.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.