From ac0549fd00569399677821257b2df58c6d851766 Mon Sep 17 00:00:00 2001 From: penguin-wwy <940375606@qq.com> Date: Wed, 27 Apr 2022 23:42:08 +0800 Subject: [PATCH 1/3] Add py_stats enable flag --- Include/internal/pycore_code.h | 14 +++++++------ Modules/_opcode.c | 38 ++++++++++++++++++++++++++++++++++ Modules/clinic/_opcode.c.h | 38 +++++++++++++++++++++++++++++++++- Python/specialize.c | 7 +++++++ 4 files changed, 90 insertions(+), 7 deletions(-) diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h index 3059db465e7d25..c17787a3239fe1 100644 --- a/Include/internal/pycore_code.h +++ b/Include/internal/pycore_code.h @@ -307,16 +307,18 @@ typedef struct _stats { } PyStats; extern PyStats _py_stats; +PyAPI_DATA(int) _enable_py_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 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++ +#define STAT_INC(opname, name) _enable_py_stats && _py_stats.opcode_stats[opname].specialization.name++ +#define STAT_DEC(opname, name) _enable_py_stats && _py_stats.opcode_stats[opname].specialization.name-- +#define OPCODE_EXE_INC(opname) _enable_py_stats && _py_stats.opcode_stats[opname].execution_count++ +#define CALL_STAT_INC(name) _enable_py_stats && _py_stats.call_stats.name++ +#define OBJECT_STAT_INC(name) _enable_py_stats && _py_stats.object_stats.name++ extern void _Py_PrintSpecializationStats(int to_file); -extern PyObject* _Py_GetSpecializationStats(void); +PyAPI_FUNC(PyObject*) _Py_GetSpecializationStats(void); +PyAPI_FUNC(void) _Py_ClearSpecializationStats(void); #else #define STAT_INC(opname, name) ((void)0) diff --git a/Modules/_opcode.c b/Modules/_opcode.c index 4812716c672718..b6816c3b63015a 100644 --- a/Modules/_opcode.c +++ b/Modules/_opcode.c @@ -76,6 +76,42 @@ _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 + _enable_py_stats = 1; + _Py_ClearSpecializationStats(); +#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 + _enable_py_stats = 0; +#endif + Py_RETURN_NONE; +} + +/*[clinic input] + _opcode.get_specialization_stats Return the specialization stats @@ -96,6 +132,8 @@ static PyMethodDef opcode_functions[] = { _OPCODE_STACK_EFFECT_METHODDEF _OPCODE_GET_SPECIALIZATION_STATS_METHODDEF + _OPCODE_ENABLE_SPECIALIZATION_STATS_METHODDEF + _OPCODE_DISABLE_SPECIALIZATION_STATS_METHODDEF {NULL, NULL, 0, NULL} }; diff --git a/Modules/clinic/_opcode.c.h b/Modules/clinic/_opcode.c.h index 6ef303bb05e7c6..33d2c1c1ceb37d 100644 --- a/Modules/clinic/_opcode.c.h +++ b/Modules/clinic/_opcode.c.h @@ -57,6 +57,42 @@ _opcode_stack_effect(PyObject *module, PyObject *const *args, Py_ssize_t nargs, return return_value; } +PyDoc_STRVAR(_opcode_enable_specialization_stats__doc__, +"enable_specialization_stats($module, /)\n" +"--\n" +"\n" +"Enable the specialization stats"); + +#define _OPCODE_ENABLE_SPECIALIZATION_STATS_METHODDEF \ + {"enable_specialization_stats", (PyCFunction)_opcode_enable_specialization_stats, METH_NOARGS, _opcode_enable_specialization_stats__doc__}, + +static PyObject * +_opcode_enable_specialization_stats_impl(PyObject *module); + +static PyObject * +_opcode_enable_specialization_stats(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _opcode_enable_specialization_stats_impl(module); +} + +PyDoc_STRVAR(_opcode_disable_specialization_stats__doc__, +"disable_specialization_stats($module, /)\n" +"--\n" +"\n" +"Disable the specialization stats"); + +#define _OPCODE_DISABLE_SPECIALIZATION_STATS_METHODDEF \ + {"disable_specialization_stats", (PyCFunction)_opcode_disable_specialization_stats, METH_NOARGS, _opcode_disable_specialization_stats__doc__}, + +static PyObject * +_opcode_disable_specialization_stats_impl(PyObject *module); + +static PyObject * +_opcode_disable_specialization_stats(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _opcode_disable_specialization_stats_impl(module); +} + PyDoc_STRVAR(_opcode_get_specialization_stats__doc__, "get_specialization_stats($module, /)\n" "--\n" @@ -74,4 +110,4 @@ _opcode_get_specialization_stats(PyObject *module, PyObject *Py_UNUSED(ignored)) { return _opcode_get_specialization_stats_impl(module); } -/*[clinic end generated code: output=1699b4b1488b49c1 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=359c0440090ed376 input=a9049054013a1b77]*/ diff --git a/Python/specialize.c b/Python/specialize.c index 9449ac117979da..550becb23d90b8 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -34,6 +34,7 @@ uint8_t _PyOpcode_Adaptive[256] = { Py_ssize_t _Py_QuickenedCount = 0; #ifdef Py_STATS PyStats _py_stats = { 0 }; +int _enable_py_stats = 1; #define ADD_STAT_TO_DICT(res, field) \ do { \ @@ -128,6 +129,12 @@ _Py_GetSpecializationStats(void) { } return stats; } + +void +_Py_ClearSpecializationStats() +{ + memset(&_py_stats, 0, sizeof(_py_stats)); +} #endif From b09a612ad53750ed6048a2e12679d774b16cb522 Mon Sep 17 00:00:00 2001 From: penguin-wwy <940375606@qq.com> Date: Thu, 28 Apr 2022 10:44:07 +0800 Subject: [PATCH 2/3] Add specialization stats init --- Modules/_opcode.c | 20 +++++++++++++++++++- Modules/clinic/_opcode.c.h | 20 +++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Modules/_opcode.c b/Modules/_opcode.c index b6816c3b63015a..fd91b99d578015 100644 --- a/Modules/_opcode.c +++ b/Modules/_opcode.c @@ -87,7 +87,6 @@ _opcode_enable_specialization_stats_impl(PyObject *module) { #ifdef Py_STATS _enable_py_stats = 1; - _Py_ClearSpecializationStats(); #endif Py_RETURN_NONE; } @@ -112,6 +111,24 @@ _opcode_disable_specialization_stats_impl(PyObject *module) /*[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=b3a1ac38507b3ced]*/ +{ +#ifdef Py_STATS + _Py_ClearSpecializationStats(); + _enable_py_stats = 0; +#endif + Py_RETURN_NONE; +} + +/*[clinic input] + _opcode.get_specialization_stats Return the specialization stats @@ -134,6 +151,7 @@ opcode_functions[] = { _OPCODE_GET_SPECIALIZATION_STATS_METHODDEF _OPCODE_ENABLE_SPECIALIZATION_STATS_METHODDEF _OPCODE_DISABLE_SPECIALIZATION_STATS_METHODDEF + _OPCODE_INIT_SPECIALIZATION_STATS_METHODDEF {NULL, NULL, 0, NULL} }; diff --git a/Modules/clinic/_opcode.c.h b/Modules/clinic/_opcode.c.h index 33d2c1c1ceb37d..119b3287d82a88 100644 --- a/Modules/clinic/_opcode.c.h +++ b/Modules/clinic/_opcode.c.h @@ -93,6 +93,24 @@ _opcode_disable_specialization_stats(PyObject *module, PyObject *Py_UNUSED(ignor return _opcode_disable_specialization_stats_impl(module); } +PyDoc_STRVAR(_opcode_init_specialization_stats__doc__, +"init_specialization_stats($module, /)\n" +"--\n" +"\n" +"Initialize the specialization stats"); + +#define _OPCODE_INIT_SPECIALIZATION_STATS_METHODDEF \ + {"init_specialization_stats", (PyCFunction)_opcode_init_specialization_stats, METH_NOARGS, _opcode_init_specialization_stats__doc__}, + +static PyObject * +_opcode_init_specialization_stats_impl(PyObject *module); + +static PyObject * +_opcode_init_specialization_stats(PyObject *module, PyObject *Py_UNUSED(ignored)) +{ + return _opcode_init_specialization_stats_impl(module); +} + PyDoc_STRVAR(_opcode_get_specialization_stats__doc__, "get_specialization_stats($module, /)\n" "--\n" @@ -110,4 +128,4 @@ _opcode_get_specialization_stats(PyObject *module, PyObject *Py_UNUSED(ignored)) { return _opcode_get_specialization_stats_impl(module); } -/*[clinic end generated code: output=359c0440090ed376 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=118d7f69f26dc46e input=a9049054013a1b77]*/ From 2d0bad4e9644f9c073ef674a3e04f11350eac6df Mon Sep 17 00:00:00 2001 From: penguin-wwy <940375606@qq.com> Date: Thu, 5 May 2022 11:45:28 +0800 Subject: [PATCH 3/3] Clear specialization stats properly --- Include/internal/pycore_code.h | 2 +- Python/specialize.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Include/internal/pycore_code.h b/Include/internal/pycore_code.h index 4480edac3121eb..b379322be0450e 100644 --- a/Include/internal/pycore_code.h +++ b/Include/internal/pycore_code.h @@ -316,7 +316,7 @@ extern int _enable_spec_stats; #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) _enable_spec_stats && _py_stats.opcode_stats[opname].execution_count++ +#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++ #define OBJECT_STAT_INC_COND(name, cond) \ diff --git a/Python/specialize.c b/Python/specialize.c index 0649b6c3dd2bca..9e3ca2f10d8e00 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -134,7 +134,9 @@ _Py_GetSpecializationStats(void) { void _Py_ClearSpecializationStats() { - memset(&_py_stats.opcode_stats, 0, sizeof(OpcodeStats) * 256); + for (Py_ssize_t i = 0; i < 256; i++) { + memset(&_py_stats.opcode_stats[i].specialization, 0, sizeof(SpecializationStats)); + } } void