Skip to content

Commit 4f7d8a8

Browse files
committed
revert to refs_created/_destroyed, other tweaks
1 parent 0bb3261 commit 4f7d8a8

File tree

6 files changed

+77
-76
lines changed

6 files changed

+77
-76
lines changed

Doc/library/tracemalloc.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,12 @@ Functions
331331
:mod:`tracemalloc` module as a tuple: ``(current: int, peak: int)``.
332332

333333

334-
.. function:: get_traced_allocs()
334+
.. function:: get_traced_refs()
335335

336-
Get the current unit counts of allocated and deallocated memory blocks.
337-
:mod:`tracemalloc` module as a tuple: ``(allocs: int, deallocs: int)``.
336+
Get the current count of created and destroyed traced references.
337+
:mod:`tracemalloc` module as a tuple: ``(created: int, destroyed: int)``.
338+
339+
.. versionadded:: next
338340

339341

340342
.. function:: reset_peak()

Include/internal/pycore_tracemalloc.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ struct _tracemalloc_runtime_state {
9494
/* domain (unsigned int) => traces (_Py_hashtable_t).
9595
Protected by TABLES_LOCK(). */
9696
_Py_hashtable_t *domains;
97-
/* Number of allocations.
97+
/* Number of references created.
9898
Protected by TABLES_LOCK(). */
99-
Py_ssize_t allocations;
100-
/* Number of deallocations.
99+
Py_ssize_t refs_created;
100+
/* Number of references destroyed.
101101
Protected by TABLES_LOCK() and sometimes modified atomically. */
102-
Py_ssize_t deallocations;
102+
Py_ssize_t refs_destroyed;
103103

104104
struct tracemalloc_traceback empty_traceback;
105105

@@ -162,7 +162,7 @@ extern size_t _PyTraceMalloc_GetMemory(void);
162162
extern PyObject* _PyTraceMalloc_GetTracedMemory(void);
163163

164164
/* Get the current number of references created and destroyed as a 2-tuple */
165-
extern PyObject* _PyTraceMalloc_GetTracedAllocs(void);
165+
extern PyObject* _PyTraceMalloc_GetTracedRefs(void);
166166

167167
/* Set the peak size of traced memory blocks to the current size */
168168
extern void _PyTraceMalloc_ResetPeak(void);

Lib/test/test_tracemalloc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,15 +1156,15 @@ def g():
11561156
try:
11571157
tracemalloc.clear_traces()
11581158
f()
1159-
refs = tracemalloc.get_traced_allocs()
1159+
refs = tracemalloc.get_traced_refs()
11601160
if refs == (1, 0):
11611161
warnings.warn("ceval Py_DECREF doesn't emit PyRefTracer_DESTROY in this build")
11621162
else:
11631163
self.assertEqual(refs, (1, 1))
11641164

11651165
tracemalloc.clear_traces()
11661166
g()
1167-
refs = tracemalloc.get_traced_allocs()
1167+
refs = tracemalloc.get_traced_refs()
11681168
if refs == (3, 2):
11691169
warnings.warn("ceval Py_DECREF doesn't emit PyRefTracer_DESTROY in this build")
11701170
else:

Modules/_tracemalloc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,18 @@ _tracemalloc_get_traced_memory_impl(PyObject *module)
169169

170170

171171
/*[clinic input]
172-
_tracemalloc.get_traced_allocs
172+
_tracemalloc.get_traced_refs
173173
174174
Get the current count of created and destroyed refs.
175175
176176
Returns a tuple: (created: int, destroyed: int).
177177
[clinic start generated code]*/
178178

179179
static PyObject *
180-
_tracemalloc_get_traced_allocs_impl(PyObject *module)
181-
/*[clinic end generated code: output=cb84cbd781fab85f input=336f38d37a5830ef]*/
180+
_tracemalloc_get_traced_refs_impl(PyObject *module)
181+
/*[clinic end generated code: output=81d36fdeb3ffc362 input=d0652f2592733b0e]*/
182182
{
183-
return _PyTraceMalloc_GetTracedAllocs();
183+
return _PyTraceMalloc_GetTracedRefs();
184184
}
185185

186186

@@ -212,7 +212,7 @@ static PyMethodDef module_methods[] = {
212212
_TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF
213213
_TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF
214214
_TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF
215-
_TRACEMALLOC_GET_TRACED_ALLOCS_METHODDEF
215+
_TRACEMALLOC_GET_TRACED_REFS_METHODDEF
216216
_TRACEMALLOC_RESET_PEAK_METHODDEF
217217
/* sentinel */
218218
{NULL, NULL}

Modules/clinic/_tracemalloc.c.h

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/tracemalloc.c

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ typedef struct {
7676
#define tracemalloc_tracebacks _PyRuntime.tracemalloc.tracebacks
7777
#define tracemalloc_traces _PyRuntime.tracemalloc.traces
7878
#define tracemalloc_domains _PyRuntime.tracemalloc.domains
79-
#define tracemalloc_allocations _PyRuntime.tracemalloc.allocations
80-
#define tracemalloc_deallocations _PyRuntime.tracemalloc.deallocations
79+
#define tracemalloc_refs_created _PyRuntime.tracemalloc.refs_created
80+
#define tracemalloc_refs_destroyed _PyRuntime.tracemalloc.refs_destroyed
8181

8282

8383
#ifdef TRACE_DEBUG
@@ -1255,48 +1255,47 @@ _PyTraceMalloc_Fini(void)
12551255
12561256
Do nothing if tracemalloc is not tracing memory allocations
12571257
or if the object memory block is not already traced. */
1258-
static int
1259-
_PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event,
1260-
void* Py_UNUSED(ignore))
1261-
{
1262-
if (event != PyRefTracer_CREATE) {
1263-
/* we don't want bother here with the lock for performance reasons */
1264-
if (_Py_atomic_load_int_relaxed(&tracemalloc_config.tracing)) {
1265-
_Py_atomic_add_ssize(&tracemalloc_deallocations, 1);
1266-
}
1267-
return 0;
1268-
}
1269-
if (get_reentrant()) {
1270-
return 0;
1271-
}
1272-
1273-
_Py_AssertHoldsTstate();
1274-
TABLES_LOCK();
1275-
1276-
if (!tracemalloc_config.tracing) {
1277-
goto done;
1278-
}
1279-
1280-
tracemalloc_allocations += 1;
1281-
1282-
PyTypeObject *type = Py_TYPE(op);
1283-
const size_t presize = _PyType_PreHeaderSize(type);
1284-
uintptr_t ptr = (uintptr_t)((char *)op - presize);
1285-
1286-
trace_t *trace = _Py_hashtable_get(tracemalloc_traces, TO_PTR(ptr));
1287-
if (trace != NULL) {
1288-
/* update the traceback of the memory block */
1289-
traceback_t *traceback = traceback_new();
1290-
if (traceback != NULL) {
1291-
trace->traceback = traceback;
1292-
}
1293-
}
1294-
/* else: cannot track the object, its memory block size is unknown */
1295-
1296-
done:
1297-
TABLES_UNLOCK();
1298-
return 0;
1299-
}
1258+
static int
1259+
_PyTraceMalloc_TraceRef(PyObject *op, PyRefTracerEvent event,
1260+
void* Py_UNUSED(ignore))
1261+
{
1262+
if (get_reentrant()) {
1263+
return 0;
1264+
}
1265+
1266+
_Py_AssertHoldsTstate();
1267+
TABLES_LOCK();
1268+
1269+
if (!tracemalloc_config.tracing) {
1270+
goto done;
1271+
}
1272+
1273+
if (event == PyRefTracer_CREATE) {
1274+
tracemalloc_refs_created += 1;
1275+
}
1276+
else {
1277+
tracemalloc_refs_destroyed += 1;
1278+
goto done;
1279+
}
1280+
1281+
PyTypeObject *type = Py_TYPE(op);
1282+
const size_t presize = _PyType_PreHeaderSize(type);
1283+
uintptr_t ptr = (uintptr_t)((char *)op - presize);
1284+
1285+
trace_t *trace = _Py_hashtable_get(tracemalloc_traces, TO_PTR(ptr));
1286+
if (trace != NULL) {
1287+
/* update the traceback of the memory block */
1288+
traceback_t *traceback = traceback_new();
1289+
if (traceback != NULL) {
1290+
trace->traceback = traceback;
1291+
}
1292+
}
1293+
/* else: cannot track the object, its memory block size is unknown */
1294+
1295+
done:
1296+
TABLES_UNLOCK();
1297+
return 0;
1298+
}
13001299

13011300

13021301
PyObject*
@@ -1334,8 +1333,8 @@ _PyTraceMalloc_ClearTraces(void)
13341333
TABLES_LOCK();
13351334
if (tracemalloc_config.tracing) {
13361335
tracemalloc_clear_traces_unlocked();
1337-
tracemalloc_allocations = 0;
1338-
tracemalloc_deallocations = 0;
1336+
tracemalloc_refs_created = 0;
1337+
tracemalloc_refs_destroyed = 0;
13391338
}
13401339
TABLES_UNLOCK();
13411340
}
@@ -1476,21 +1475,21 @@ _PyTraceMalloc_GetTracedMemory(void)
14761475

14771476

14781477
PyObject *
1479-
_PyTraceMalloc_GetTracedAllocs(void)
1478+
_PyTraceMalloc_GetTracedRefs(void)
14801479
{
14811480
TABLES_LOCK();
1482-
Py_ssize_t allocations, deallocations;
1481+
Py_ssize_t created, destroyed;
14831482
if (tracemalloc_config.tracing) {
1484-
allocations = tracemalloc_allocations;
1485-
deallocations = tracemalloc_deallocations;
1483+
created = tracemalloc_refs_created;
1484+
destroyed = tracemalloc_refs_destroyed;
14861485
}
14871486
else {
1488-
allocations = 0;
1489-
deallocations = 0;
1487+
created = 0;
1488+
destroyed = 0;
14901489
}
14911490
TABLES_UNLOCK();
14921491

1493-
return Py_BuildValue("nn", allocations, deallocations);
1492+
return Py_BuildValue("nn", created, destroyed);
14941493
}
14951494

14961495
void

0 commit comments

Comments
 (0)