Skip to content

Commit 836b17c

Browse files
authored
Add more stats for freelist use and allocations. (GH-92211)
1 parent e8d7661 commit 836b17c

File tree

9 files changed

+43
-0
lines changed

9 files changed

+43
-0
lines changed

Diff for: Include/internal/pycore_code.h

+8
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,12 @@ typedef struct _call_stats {
292292

293293
typedef struct _object_stats {
294294
uint64_t allocations;
295+
uint64_t allocations512;
296+
uint64_t allocations4k;
297+
uint64_t allocations_big;
295298
uint64_t frees;
299+
uint64_t to_freelist;
300+
uint64_t from_freelist;
296301
uint64_t new_values;
297302
uint64_t dict_materialized_on_request;
298303
uint64_t dict_materialized_new_key;
@@ -313,6 +318,8 @@ extern PyStats _py_stats;
313318
#define OPCODE_EXE_INC(opname) _py_stats.opcode_stats[opname].execution_count++
314319
#define CALL_STAT_INC(name) _py_stats.call_stats.name++
315320
#define OBJECT_STAT_INC(name) _py_stats.object_stats.name++
321+
#define OBJECT_STAT_INC_COND(name, cond) \
322+
do { if (cond) _py_stats.object_stats.name++; } while (0)
316323

317324
extern void _Py_PrintSpecializationStats(int to_file);
318325

@@ -325,6 +332,7 @@ PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
325332
#define OPCODE_EXE_INC(opname) ((void)0)
326333
#define CALL_STAT_INC(name) ((void)0)
327334
#define OBJECT_STAT_INC(name) ((void)0)
335+
#define OBJECT_STAT_INC_COND(name, cond) ((void)0)
328336
#endif // !Py_STATS
329337

330338
// Cache values are only valid in memory, so use native endianness.

Diff for: Objects/dictobject.c

+5
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,7 @@ new_keys_object(uint8_t log2_size, bool unicode)
624624
#endif
625625
if (log2_size == PyDict_LOG_MINSIZE && unicode && state->keys_numfree > 0) {
626626
dk = state->keys_free_list[--state->keys_numfree];
627+
OBJECT_STAT_INC(from_freelist);
627628
}
628629
else
629630
#endif
@@ -681,6 +682,7 @@ free_keys_object(PyDictKeysObject *keys)
681682
&& state->keys_numfree < PyDict_MAXFREELIST
682683
&& DK_IS_UNICODE(keys)) {
683684
state->keys_free_list[state->keys_numfree++] = keys;
685+
OBJECT_STAT_INC(to_freelist);
684686
return;
685687
}
686688
#endif
@@ -726,6 +728,7 @@ new_dict(PyDictKeysObject *keys, PyDictValues *values, Py_ssize_t used, int free
726728
mp = state->free_list[--state->numfree];
727729
assert (mp != NULL);
728730
assert (Py_IS_TYPE(mp, &PyDict_Type));
731+
OBJECT_STAT_INC(from_freelist);
729732
_Py_NewReference((PyObject *)mp);
730733
}
731734
else
@@ -1544,6 +1547,7 @@ dictresize(PyDictObject *mp, uint8_t log2_newsize, int unicode)
15441547
state->keys_numfree < PyDict_MAXFREELIST)
15451548
{
15461549
state->keys_free_list[state->keys_numfree++] = oldkeys;
1550+
OBJECT_STAT_INC(to_freelist);
15471551
}
15481552
else
15491553
#endif
@@ -2381,6 +2385,7 @@ dict_dealloc(PyDictObject *mp)
23812385
#endif
23822386
if (state->numfree < PyDict_MAXFREELIST && Py_IS_TYPE(mp, &PyDict_Type)) {
23832387
state->free_list[state->numfree++] = mp;
2388+
OBJECT_STAT_INC(to_freelist);
23842389
}
23852390
else
23862391
#endif

Diff for: Objects/floatobject.c

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ PyFloat_FromDouble(double fval)
141141
#endif
142142
state->free_list = (PyFloatObject *) Py_TYPE(op);
143143
state->numfree--;
144+
OBJECT_STAT_INC(from_freelist);
144145
}
145146
else
146147
#endif
@@ -256,6 +257,7 @@ _PyFloat_ExactDealloc(PyObject *obj)
256257
state->numfree++;
257258
Py_SET_TYPE(op, (PyTypeObject *)state->free_list);
258259
state->free_list = op;
260+
OBJECT_STAT_INC(to_freelist);
259261
#else
260262
PyObject_Free(op);
261263
#endif

Diff for: Objects/genobject.c

+2
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,7 @@ async_gen_wrapped_val_dealloc(_PyAsyncGenWrappedValue *o)
19421942
if (state->value_numfree < _PyAsyncGen_MAXFREELIST) {
19431943
assert(_PyAsyncGenWrappedValue_CheckExact(o));
19441944
state->value_freelist[state->value_numfree++] = o;
1945+
OBJECT_STAT_INC(to_freelist);
19451946
}
19461947
else
19471948
#endif
@@ -2018,6 +2019,7 @@ _PyAsyncGenValueWrapperNew(PyObject *val)
20182019
if (state->value_numfree) {
20192020
state->value_numfree--;
20202021
o = state->value_freelist[state->value_numfree];
2022+
OBJECT_STAT_INC(from_freelist);
20212023
assert(_PyAsyncGenWrappedValue_CheckExact(o));
20222024
_Py_NewReference((PyObject*)o);
20232025
}

Diff for: Objects/listobject.c

+2
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ PyList_New(Py_ssize_t size)
158158
if (PyList_MAXFREELIST && state->numfree) {
159159
state->numfree--;
160160
op = state->free_list[state->numfree];
161+
OBJECT_STAT_INC(from_freelist);
161162
_Py_NewReference((PyObject *)op);
162163
}
163164
else
@@ -353,6 +354,7 @@ list_dealloc(PyListObject *op)
353354
#endif
354355
if (state->numfree < PyList_MAXFREELIST && PyList_CheckExact(op)) {
355356
state->free_list[state->numfree++] = op;
357+
OBJECT_STAT_INC(to_freelist);
356358
}
357359
else
358360
#endif

Diff for: Objects/obmalloc.c

+15
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,10 @@ PyMem_Malloc(size_t size)
616616
/* see PyMem_RawMalloc() */
617617
if (size > (size_t)PY_SSIZE_T_MAX)
618618
return NULL;
619+
OBJECT_STAT_INC_COND(allocations512, size < 512);
620+
OBJECT_STAT_INC_COND(allocations4k, size >= 512 && size < 4094);
621+
OBJECT_STAT_INC_COND(allocations_big, size >= 4094);
622+
OBJECT_STAT_INC(allocations);
619623
return _PyMem.malloc(_PyMem.ctx, size);
620624
}
621625

@@ -625,6 +629,10 @@ PyMem_Calloc(size_t nelem, size_t elsize)
625629
/* see PyMem_RawMalloc() */
626630
if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
627631
return NULL;
632+
OBJECT_STAT_INC_COND(allocations512, elsize < 512);
633+
OBJECT_STAT_INC_COND(allocations4k, elsize >= 512 && elsize < 4094);
634+
OBJECT_STAT_INC_COND(allocations_big, elsize >= 4094);
635+
OBJECT_STAT_INC(allocations);
628636
return _PyMem.calloc(_PyMem.ctx, nelem, elsize);
629637
}
630638

@@ -640,6 +648,7 @@ PyMem_Realloc(void *ptr, size_t new_size)
640648
void
641649
PyMem_Free(void *ptr)
642650
{
651+
OBJECT_STAT_INC(frees);
643652
_PyMem.free(_PyMem.ctx, ptr);
644653
}
645654

@@ -696,6 +705,9 @@ PyObject_Malloc(size_t size)
696705
/* see PyMem_RawMalloc() */
697706
if (size > (size_t)PY_SSIZE_T_MAX)
698707
return NULL;
708+
OBJECT_STAT_INC_COND(allocations512, size < 512);
709+
OBJECT_STAT_INC_COND(allocations4k, size >= 512 && size < 4094);
710+
OBJECT_STAT_INC_COND(allocations_big, size >= 4094);
699711
OBJECT_STAT_INC(allocations);
700712
return _PyObject.malloc(_PyObject.ctx, size);
701713
}
@@ -706,6 +718,9 @@ PyObject_Calloc(size_t nelem, size_t elsize)
706718
/* see PyMem_RawMalloc() */
707719
if (elsize != 0 && nelem > (size_t)PY_SSIZE_T_MAX / elsize)
708720
return NULL;
721+
OBJECT_STAT_INC_COND(allocations512, elsize < 512);
722+
OBJECT_STAT_INC_COND(allocations4k, elsize >= 512 && elsize < 4094);
723+
OBJECT_STAT_INC_COND(allocations_big, elsize >= 4094);
709724
OBJECT_STAT_INC(allocations);
710725
return _PyObject.calloc(_PyObject.ctx, nelem, elsize);
711726
}

Diff for: Objects/tupleobject.c

+2
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,7 @@ maybe_freelist_pop(Py_ssize_t size)
11951195
#endif
11961196
_Py_NewReference((PyObject *)op);
11971197
/* END inlined _PyObject_InitVar() */
1198+
OBJECT_STAT_INC(from_freelist);
11981199
return op;
11991200
}
12001201
}
@@ -1224,6 +1225,7 @@ maybe_freelist_push(PyTupleObject *op)
12241225
op->ob_item[0] = (PyObject *) STATE.free_list[index];
12251226
STATE.free_list[index] = op;
12261227
STATE.numfree[index]++;
1228+
OBJECT_STAT_INC(to_freelist);
12271229
return 1;
12281230
}
12291231
#endif

Diff for: Python/context.c

+2
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ _context_alloc(void)
351351
state->numfree--;
352352
ctx = state->freelist;
353353
state->freelist = (PyContext *)ctx->ctx_weakreflist;
354+
OBJECT_STAT_INC(from_freelist);
354355
ctx->ctx_weakreflist = NULL;
355356
_Py_NewReference((PyObject *)ctx);
356357
}
@@ -482,6 +483,7 @@ context_tp_dealloc(PyContext *self)
482483
state->numfree++;
483484
self->ctx_weakreflist = (PyObject *)state->freelist;
484485
state->freelist = self;
486+
OBJECT_STAT_INC(to_freelist);
485487
}
486488
else
487489
#endif

Diff for: Python/specialize.c

+5
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,12 @@ print_call_stats(FILE *out, CallStats *stats)
183183
static void
184184
print_object_stats(FILE *out, ObjectStats *stats)
185185
{
186+
fprintf(out, "Object allocations from freelist: %" PRIu64 "\n", stats->from_freelist);
187+
fprintf(out, "Object frees to freelist: %" PRIu64 "\n", stats->to_freelist);
186188
fprintf(out, "Object allocations: %" PRIu64 "\n", stats->allocations);
189+
fprintf(out, "Object allocations to 512 bytes: %" PRIu64 "\n", stats->allocations512);
190+
fprintf(out, "Object allocations to 4 kbytes: %" PRIu64 "\n", stats->allocations4k);
191+
fprintf(out, "Object allocations over 4 kbytes: %" PRIu64 "\n", stats->allocations_big);
187192
fprintf(out, "Object frees: %" PRIu64 "\n", stats->frees);
188193
fprintf(out, "Object new values: %" PRIu64 "\n", stats->new_values);
189194
fprintf(out, "Object materialize dict (on request): %" PRIu64 "\n", stats->dict_materialized_on_request);

0 commit comments

Comments
 (0)