-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-130695: add count create/destroy refs to tracemalloc #130696
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
Changes from 3 commits
3db607f
cfdaaee
0bb3261
4f7d8a8
7432709
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,12 @@ struct _tracemalloc_runtime_state { | |
/* domain (unsigned int) => traces (_Py_hashtable_t). | ||
Protected by TABLES_LOCK(). */ | ||
_Py_hashtable_t *domains; | ||
/* Number of allocations. | ||
Protected by TABLES_LOCK(). */ | ||
Py_ssize_t allocations; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to rename it to "ref_created" and "ref_destroyed". There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I started with that but as per colesbury above: """The terminology here is a bit confusing, but I don't think we should be calling these "references" or "refs". It's tracking object allocation and deallocation, not references or reference counts.""". Technically its tracking the first reference on an object (not all references), so excluding some unallocated "static" objects, its essentially allocations, but its called |
||
/* Number of deallocations. | ||
Protected by TABLES_LOCK() and sometimes modified atomically. */ | ||
Py_ssize_t deallocations; | ||
|
||
struct tracemalloc_traceback empty_traceback; | ||
|
||
|
@@ -155,6 +161,9 @@ extern size_t _PyTraceMalloc_GetMemory(void); | |
/* Get the current size and peak size of traced memory blocks as a 2-tuple */ | ||
extern PyObject* _PyTraceMalloc_GetTracedMemory(void); | ||
|
||
/* Get the current number of references created and destroyed as a 2-tuple */ | ||
extern PyObject* _PyTraceMalloc_GetTracedAllocs(void); | ||
|
||
/* Set the peak size of traced memory blocks to the current size */ | ||
extern void _PyTraceMalloc_ResetPeak(void); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import textwrap | ||
import tracemalloc | ||
import unittest | ||
import warnings | ||
from unittest.mock import patch | ||
from test.support.script_helper import (assert_python_ok, assert_python_failure, | ||
interpreter_requires_environment) | ||
|
@@ -1141,6 +1142,37 @@ def __del__(self, untrack=_testcapi.tracemalloc_untrack): | |
""") | ||
assert_python_ok("-c", code) | ||
|
||
def test_trace_refs(self): | ||
def f(): | ||
l = [] | ||
del l | ||
|
||
def g(): | ||
l = [], [] | ||
del l | ||
|
||
tracemalloc.start() | ||
|
||
try: | ||
tracemalloc.clear_traces() | ||
f() | ||
refs = tracemalloc.get_traced_allocs() | ||
if refs == (1, 0): | ||
warnings.warn("ceval Py_DECREF doesn't emit PyRefTracer_DESTROY in this build") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect a test failure, not a warning. In which case we would fall into this case? And why ignoring this failure? |
||
else: | ||
self.assertEqual(refs, (1, 1)) | ||
|
||
tracemalloc.clear_traces() | ||
g() | ||
refs = tracemalloc.get_traced_allocs() | ||
if refs == (3, 2): | ||
warnings.warn("ceval Py_DECREF doesn't emit PyRefTracer_DESTROY in this build") | ||
else: | ||
self.assertEqual(refs, (3, 3)) | ||
|
||
finally: | ||
tracemalloc.stop() | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add tracking of number of deallocations to :mod:`tracemalloc`. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.