8000 gh-93502: Add new C-API functions to trace object creation and destruction by pablogsal · Pull Request #115945 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-93502: Add new C-API functions to trace object creation and destruction #115945

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 2, 2024
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
Prev Previous commit
Next Next commit
Address feedback
  • Loading branch information
pablogsal committed May 1, 2024
commit 9b3ddce47e94d36dfe28fbacae02773f7354dd33
2 changes: 1 addition & 1 deletion Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ extern int _PyDict_CheckConsistency(PyObject *mp, int check_content);
when a memory block is reused from a free list.

Internal function called by _Py_NewReference(). */
extern int _PyTraceMalloc_NewReference(PyObject *op, PyRefTracerEvent event, void*);
extern int _PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event, void*);

// Fast inlined version of PyType_HasFeature()
static inline int
Expand Down
3 changes: 2 additions & 1 deletion Include/internal/pycore_runtime_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ extern PyTypeObject _PyExc_MemoryError;
}, \
.faulthandler = _faulthandler_runtime_state_INIT, \
.tracemalloc = _tracemalloc_runtime_state_INIT, \
.reftracer = { \
.ref_tracer = { \
.tracer_func = NULL, \
.tracer_data = NULL, \
}, \
.stoptheworld = { \
.is_global = 1, \
Expand Down
6 changes: 4 additions & 2 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3308,12 +3308,14 @@ test_reftracer(PyObject *ob, PyObject *Py_UNUSED(ignored))
struct simpletracer_data tracer_data = {0};
void* the_data = &tracer_data;
// Install a simple tracer function
PyRefTracer_SetTracer(_simpletracer, the_data);
if (PyRefTracer_SetTracer(_simpletracer, the_data) != 0) {
return NULL;
}

// Check that the tracer was correctly installed
void* data;
if (PyRefTracer_GetTracer(&data) != _simpletracer || data != the_data) {
PyErr_SetString(PyExc_ValueError, "The reftracer not correctly installed");
PyErr_SetString(PyExc_AssertionError, "The reftracer not correctly installed");
PyRefTracer_SetTracer(NULL, NULL);
return NULL;
}
Expand Down
26 changes: 13 additions & 13 deletions Objects/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -2381,9 +2381,9 @@ new_reference(PyObject *op)
#ifdef Py_TRACE_REFS
_Py_AddToAllObjects(op);
#endif
if (_PyRuntime.reftracer.tracer_func != NULL) {
void* data = _PyRuntime.reftracer.data;
_PyRuntime.reftracer.tracer_func(op, PyRefTracer_CREATE, data);
if (_PyRuntime.ref_tracer.tracer_func != NULL) {
void* data = _PyRuntime.ref_tracer.tracer_data;
_PyRuntime.ref_tracer.tracer_func(op, PyRefTracer_CREATE, data);
}
}

Expand All @@ -2408,9 +2408,9 @@ _Py_ResurrectReference(PyObject *op)
#ifdef Py_TRACE_REFS
_Py_AddToAllObjects(op);
#endif
if (_PyRuntime.reftracer.tracer_func != NULL) {
void* data = _PyRuntime.reftracer.data;
_PyRuntime.reftracer.tracer_func(op, PyRefTracer_CREATE, data);
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 @@ -2885,9 +2885,9 @@ _Py_Dealloc(PyObject *op)
Py_INCREF(type);
#endif

if (_PyRuntime.reftracer.tracer_func != NULL) {
void* data = _PyRuntime.reftracer.data;
_PyRuntime.reftracer.tracer_func(op, PyRefTracer_DESTROY, data);
if (_PyRuntime.ref_tracer.tracer_func != NULL) {
void* data = _PyRuntime.ref_tracer.tracer_data;
_PyRuntime.ref_tracer.tracer_func(op, PyRefTracer_DESTROY, data);
}

#ifdef Py_TRACE_REFS
Expand Down Expand Up @@ -2980,16 +2980,16 @@ _Py_SetRefcnt(PyObject *ob, Py_ssize_t refcnt)

int PyRefTracer_SetTracer(PyRefTracer tracer, void *data) {
assert(PyGILState_Check());
_PyRuntime.reftracer.tracer_func = tracer;
_PyRuntime.reftracer.data = data;
_PyRuntime.ref_tracer.tracer_func = tracer;
_PyRuntime.ref_tracer.tracer_data = data;
return 0;
}

PyRefTracer PyRefTracer_GetTracer(void** data) {
assert(PyGILState_Check());
if (data != NULL) {
*data = _PyRuntime.reftracer.data;
*data = _PyRuntime.ref_tracer.tracer_data;
}
return _PyRuntime.reftracer.tracer_func;
return _PyRuntime.ref_tracer.tracer_func;
}

5 changes: 2 additions & 3 deletions Python/tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ _PyTraceMalloc_Start(int max_nframe)
return -1;
}

_PyRuntime.reftracer.tracer_func = _PyTraceMalloc_NewReference;
PyRefTracer_SetTracer(_PyTraceMalloc_TraceRef, NULL);

if (tracemalloc_config.tracing) {
/* hook already installed: do nothing */
Expand Down Expand Up @@ -1354,8 +1354,7 @@ _PyTraceMalloc_Fini(void)
Do nothing if tracemalloc is not tracing memory allocations
or if the object memory block is not already traced. */
int
_PyTraceMalloc_NewReference(PyObject *op, PyRefTracerEvent event, void* Py_UNUSED(ignore))

_PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event, void* Py_UNUSED(ignore))
{
if (event != PyRefTracer_CREATE) {
return 0;
Expand Down
0