8000 gh-102304: Move _Py_RefTotal to _PyRuntimeState by ericsnowcurrently · Pull Request #102543 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-102304: Move _Py_RefTotal to _PyRuntimeState #102543

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
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
Preserve the reftotal past runtime finalization.
  • Loading branch information
ericsnowcurrently committed Mar 17, 2023
commit 7dc494d2b56ee6808dcfa712c24f21331a5d02b0
1 change: 1 addition & 0 deletions Include/internal/pycore_object.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ static inline void _PyObject_GC_UNTRACK(
#endif

#ifdef Py_REF_DEBUG
extern void _Py_SetFinalRefTotal(_PyRuntimeState *);
extern void _PyDebug_PrintTotalRefs(void);
#endif

Expand Down
44 changes: 29 additions & 15 deletions Objects/object.c
BA8A
Original file line number Diff line number Diff line change
Expand Up @@ -66,43 +66,56 @@ get_legacy_reftotal(void)

#ifdef Py_REF_DEBUG

# define REFTOTAL (_PyRuntime.object_state.reftotal)
# define REFTOTAL ((runtime)->object_state.reftotal)

static inline void
reftotal_increment(void)
reftotal_increment(_PyRuntimeState *runtime)
{
REFTOTAL++;
}

static inline void
reftotal_decrement(void)
reftotal_decrement(_PyRuntimeState *runtime)
{
REFTOTAL--;
}

static inline void
reftotal_add(Py_ssize_t n)
reftotal_add(_PyRuntimeState *runtime, Py_ssize_t n)
{
REFTOTAL += n;
}

/* We preserve the number of refs leaked during runtime finalization,
so they can be reported if the runtime is initialized again. */
// XXX We don't lose any information by dropping this,
// so we should consider doing so.
static Py_ssize_t last_final_reftotal = 0;

void
_Py_SetFinalRefTotal(_PyRuntimeState *runtime)
{
last_final_reftotal += REFTOTAL;
}

static inline Py_ssize_t
get_global_reftotal(void)
get_global_reftotal(_PyRuntimeState *runtime)
{
Py_ssize_t last = _PyRuntime.object_state.last_legacy_reftotal;
Py_ssize_t legacy = get_legacy_reftotal();
_PyRuntime.object_state.last_legacy_reftotal = legacy;
REFTOTAL += legacy - last;
return REFTOTAL;
return REFTOTAL + last_final_reftotal;
}

#undef REFTOTAL

void
_PyDebug_PrintTotalRefs(void) {
_PyRuntimeState *runtime = &_PyRuntime;
fprintf(stderr,
"[%zd refs, %zd blocks]\n",
get_global_reftotal(), _Py_GetAllocatedBlocks());
get_global_reftotal(runtime), _Py_GetAllocatedBlocks());
/* It may be helpful to also print the "legacy" reftotal separately. */
}
#endif /* Py_REF_DEBUG */
Expand Down Expand Up @@ -162,39 +175,40 @@ _Py_NegativeRefcount(const char *filename, int lineno, PyObject *op)
void
_Py_IncRefTotal_DO_NOT_USE_THIS(void)
{
reftotal_increment();
reftotal_increment(&_PyRuntime);
}

/* This is used strictly by Py_DECREF(). */
void
_Py_DecRefTotal_DO_NOT_USE_THIS(void)
{
reftotal_decrement();
reftotal_decrement(&_PyRuntime);
}

void
_Py_IncRefTotal(void)
{
reftotal_increment();
reftotal_increment(&_PyRuntime);
}

void
_Py_DecRefTotal(void)
{
reftotal_decrement();
reftotal_decrement(&_PyRuntime);
}

void
_Py_AddRefTotal(Py_ssize_t n)
{
reftotal_add(n);
reftotal_add(&_PyRuntime, n);
}

/* This includes the legacy total. */
/* This includes the legacy total
and any carried over from the last runtime init/fini cycle. */
Py_ssize_t
_Py_GetGlobalRefTotal(void)
{
return get_global_reftotal();
return get_global_reftotal(&_PyRuntime);
}

Py_ssize_t
Expand Down Expand Up @@ -2106,7 +2120,7 @@ void
_Py_NewReference(PyObject *op)
{
#ifdef Py_REF_DEBUG
reftotal_increment();
reftotal_increment(&_PyRuntime);
#endif
new_reference(op);
}
Expand Down
1 change: 1 addition & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,7 @@ Py_FinalizeEx(void)
if (show_ref_count) {
_PyDebug_PrintTotalRefs();
}
_Py_SetFinalRefTotal(runtime);
#endif

#ifdef Py_TRACE_REFS
Expand Down
0