Skip to content
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

gh-90230: Python API for specialization stats #92323

Closed
wants to merge 6 commits into from
Closed
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
8 changes: 6 additions & 2 deletions Include/internal/pycore_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,10 @@ typedef struct _stats {
} PyStats;

extern PyStats _py_stats;
extern int _enable_spec_stats;

#define STAT_INC(opname, name) _py_stats.opcode_stats[opname].specialization.name++
#define STAT_DEC(opname, name) _py_stats.opcode_stats[opname].specialization.name--
#define STAT_INC(opname, name) _enable_spec_stats && _py_stats.opcode_stats[opname].specialization.name++
#define STAT_DEC(opname, name) _enable_spec_stats && _py_stats.opcode_stats[opname].specialization.name--
#define OPCODE_EXE_INC(opname) _py_stats.opcode_stats[opname].execution_count++
#define CALL_STAT_INC(name) _py_stats.call_stats.name++
#define OBJECT_STAT_INC(name) _py_stats.object_stats.name++
Expand All @@ -325,6 +326,9 @@ extern void _Py_PrintSpecializationStats(int to_file);

// Used by the _opcode extension which is built as a shared library
PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void);
PyAPI_FUNC(void) _Py_ClearSpecializationStats(void);
PyAPI_FUNC(void) _Py_EnableSpecializationStats(void);
PyAPI_FUNC(void) _Py_DisableSpecializationStats(void);

#else
#define STAT_INC(opname, name) ((void)0)
Expand Down
56 changes: 56 additions & 0 deletions Modules/_opcode.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,59 @@ _opcode_stack_effect_impl(PyObject *module, int opcode, PyObject *oparg,

/*[clinic input]

_opcode.enable_specialization_stats

Enable the specialization stats
[clinic start generated code]*/

static PyObject *
_opcode_enable_specialization_stats_impl(PyObject *module)
/*[clinic end generated code: output=02d8385bbafb7b63 input=62e09e133581bdad]*/
{
#ifdef Py_STATS
_Py_EnableSpecializationStats();
#endif
Py_RETURN_NONE;
}


/*[clinic input]

_opcode.disable_specialization_stats

Disable the specialization stats
[clinic start generated code]*/

static PyObject *
_opcode_disable_specialization_stats_impl(PyObject *module)
/*[clinic end generated code: output=14ca17e44089d7a3 input=3213ea9f684d5f3b]*/
{
#ifdef Py_STATS
_Py_DisableSpecializationStats();
#endif
Py_RETURN_NONE;
}

/*[clinic input]

_opcode.init_specialization_stats

Initialize the specialization stats
[clinic start generated code]*/

static PyObject *
_opcode_init_specialization_stats_impl(PyObject *module)
/*[clinic end generated code: output=b067640d0e5de6a5 input=94c13e3ae8629a21]*/
{
#ifdef Py_STATS
_Py_ClearSpecializationStats();
_Py_DisableSpecializationStats();
#endif
Py_RETURN_NONE;
}

/*[clinic input]

_opcode.get_specialization_stats

Return the specialization stats
Expand All @@ -96,6 +149,9 @@ static PyMethodDef
opcode_functions[] = {
_OPCODE_STACK_EFFECT_METHODDEF
_OPCODE_GET_SPECIALIZATION_STATS_METHODDEF
_OPCODE_ENABLE_SPECIALIZATION_STATS_METHODDEF
_OPCODE_DISABLE_SPECIALIZATION_STATS_METHODDEF
_OPCODE_INIT_SPECIALIZATION_STATS_METHODDEF
{NULL, NULL, 0, NULL}
};

Expand Down
56 changes: 55 additions & 1 deletion Modules/clinic/_opcode.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ uint8_t _PyOpcode_Adaptive[256] = {
Py_ssize_t _Py_QuickenedCount = 0;
#ifdef Py_STATS
PyStats _py_stats = { 0 };
/* Enable specialization stats by default */
int _enable_spec_stats = 1;

#define ADD_STAT_TO_DICT(res, field) \
do { \
Expand Down Expand Up @@ -128,6 +130,26 @@ _Py_GetSpecializationStats(void) {
}
return stats;
}

void
_Py_ClearSpecializationStats()
{
for (Py_ssize_t i = 0; i < 256; i++) {
memset(&_py_stats.opcode_stats[i].specialization, 0, sizeof(SpecializationStats));
}
}

void
_Py_EnableSpecializationStats(void)
{
_enable_spec_stats = 1;
}
void
_Py_DisableSpecializationStats(void)
{
_enable_spec_stats = 0;
}

#endif


Expand Down