Skip to content

gh-126076: Account for relocated objects in tracemalloc #126077

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 8 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 9 additions & 5 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ PyAPI_FUNC(void) _Py_NO_RETURN _Py_FatalRefcountErrorFunc(
#define _Py_FatalRefcountError(message) \
_Py_FatalRefcountErrorFunc(__func__, (message))

#define _PyReftracerTrack(obj, operation) \
do { \
struct _reftracer_runtime_state *tracer = &_PyRuntime.ref_tracer; \
if (tracer->tracer_func != NULL) { \
void *data = tracer->tracer_data; \
tracer->tracer_func((obj), (operation), data); \
} \
} while(0)

#ifdef Py_REF_DEBUG
/* The symbol is only exposed in the API for the sake of extensions
Expand Down Expand Up @@ -208,11 +216,7 @@ _Py_DECREF_SPECIALIZED(PyObject *op, const destructor destruct)
#ifdef Py_TRACE_REFS
_Py_ForgetReference(op);
#endif
struct _reftracer_runtime_state *tracer = &_PyRuntime.ref_tracer;
if (tracer->tracer_func != NULL) {
void* data = tracer->tracer_data;
tracer->tracer_func(op, PyRefTracer_DESTROY, data);
}
_PyReftracerTrack(op, PyRefTracer_DESTROY);
destruct(op);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Relocated objects such as ``tuple``, ``bytes`` and ``str`` objects are
properly tracked by :mod:`tracemalloc` and its associated hooks. Patch by
Pablo Galindo.
1 change: 1 addition & 0 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3195,6 +3195,7 @@ _PyBytes_Resize(PyObject **pv, Py_ssize_t newsize)
#ifdef Py_TRACE_REFS
_Py_ForgetReference(v);
#endif
_PyReftracerTrack(v, PyRefTracer_DESTROY);
*pv = (PyObject *)
PyObject_Realloc(v, PyBytesObject_SIZE + newsize);
if (*pv == NULL) {
Expand Down
17 changes: 2 additions & 15 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2457,11 +2457,7 @@ new_reference(PyObject *op)
#ifdef Py_TRACE_REFS
_Py_AddToAllObjects(op);
#endif
struct _reftracer_runtime_state *tracer = &_PyRuntime.ref_tracer;
if (tracer->tracer_func != NULL) {
void* data = tracer->tracer_data;
tracer->tracer_func(op, PyRefTracer_CREATE, data);
}
_PyReftracerTrack(op, PyRefTracer_CREATE);
}

void
Expand Down Expand Up @@ -2525,10 +2521,6 @@ _Py_ResurrectReference(PyObject *op)
#ifdef Py_TRACE_REFS
_Py_AddToAllObjects(op);
#endif
if (_PyRuntime.ref_tracer.tracer_func != NULL) {
void* data = _PyRuntime.ref_tracer.tracer_data;
_PyRuntime.ref_tracer.tracer_func(op, PyRefTracer_CREATE, data);
}
}


Expand Down Expand Up @@ -2918,15 +2910,10 @@ _Py_Dealloc(PyObject *op)
Py_INCREF(type);
#endif

struct _reftracer_runtime_state *tracer = &_PyRuntime.ref_tracer;
if (tracer->tracer_func != NULL) {
void* data = tracer->tracer_data;
tracer->tracer_func(op, PyRefTracer_DESTROY, data);
}

#ifdef Py_TRACE_REFS
_Py_ForgetReference(op);
#endif
_PyReftracerTrack(op, PyRefTracer_DESTROY);
(*dealloc)(op);

#ifdef Py_DEBUG
Expand Down
1 change: 1 addition & 0 deletions Objects/tupleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ _PyTuple_Resize(PyObject **pv, Py_ssize_t newsize)
for (i = newsize; i < oldsize; i++) {
Py_CLEAR(v->ob_item[i]);
}
_PyReftracerTrack((PyObject *)v, PyRefTracer_DESTROY);
sv = PyObject_GC_Resize(PyTupleObject, v, newsize);
if (sv == NULL) {
*pv = NULL;
Expand Down
1 change: 1 addition & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,7 @@ resize_compact(PyObject *unicode, Py_ssize_t length)
#ifdef Py_TRACE_REFS
_Py_ForgetReference(unicode);
#endif
_PyReftracerTrack(unicode, PyRefTracer_DESTROY);

new_unicode = (PyObject *)PyObject_Realloc(unicode, new_size);
if (new_unicode == NULL) {
Expand Down
6 changes: 1 addition & 5 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,7 @@
} \
_Py_DECREF_STAT_INC(); \
if (--op->ob_refcnt == 0) { \
struct _reftracer_runtime_state *tracer = &_PyRuntime.ref_tracer; \
if (tracer->tracer_func != NULL) { \
void* data = tracer->tracer_data; \
tracer->tracer_func(op, PyRefTracer_DESTROY, data); \
} \
_PyReftracerTrack(op, PyRefTracer_DESTROY); \
destructor d = (destructor)(dealloc); \
d(op); \
} \
Expand Down
Loading