Skip to content
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
Allow stats to be turned on and off, and be cleared.
  • Loading branch information
markshannon committed Jun 15, 2022
commit daa5487a4dee22ed372c238b42c12fd279e36e1f
1 change: 1 addition & 0 deletions Include/pystats.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ typedef struct _stats {
PyAPI_DATA(PyStats) _py_stats_struct;
PyAPI_DATA(PyStats *) _py_stats;

extern void _Py_StatsClear(void);
extern void _Py_PrintSpecializationStats(int to_file);

#ifdef _PY_INTERPRETER
Expand Down
80 changes: 79 additions & 1 deletion Python/clinic/sysmodule.c.h

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

6 changes: 6 additions & 0 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ print_stats(FILE *out, PyStats *stats) {
print_object_stats(out, &stats->object_stats);
}

void
_Py_StatsClear(void)
{
_Py_stat_struct = { 0 };
}

void
_Py_PrintSpecializationStats(int to_file)
{
Expand Down
62 changes: 62 additions & 0 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1909,6 +1909,63 @@ sys_is_finalizing_impl(PyObject *module)
return PyBool_FromLong(_Py_IsFinalizing());
}

#ifdef Py_STATS
/*[clinic input]
sys._stats_on

Turns on stats gathering (stats gathering is on by default)
[clinic start generated code]*/

static PyObject *
sys__stats_on_impl(PyObject *module)
/*[clinic end generated code: output=aca53eafcbb4d9fe input=f4bef5763c4387b8]*/

static PyObject *
sys__stats_on(PyObject *module)
/*[clinic end generated code]*/
{
_py_stats = &_py_stats_struct;
Py_RETURN_NONE;
}

/*[clinic input]
sys._stats_off

Turns off stats gathering (stats gathering is on by default)
[clinic start generated code]*/

static PyObject *
sys__stats_off_impl(PyObject *module)
/*[clinic end generated code: output=1534c1ee63812214 input=ec6e593e39b12b4a]*/

static PyObject *
sys__stats_off(PyObject *module)
/*[clinic end generated code]*/
{
_py_stats = NULL;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use a hidden, dummy brother of _py_stats_struct here instead of NULL? It'll allow to omit if (_py_stats) checks.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did consider it, but the stats object is about 600k and is likely to get larger. So I think the extra NULL checks are less inefficient than wasting the memory.

Py_RETURN_NONE;
}

/*[clinic input]
sys._stats_clear

Clears stats
[clinic start generated code]*/

static PyObject *
sys__stats_clear_impl(PyObject *module)
/*[clinic end generated code: output=fb65a2525ee50604 input=0bd23b30a48f67ab]*/

static PyObject *
sys__stats_clear(PyObject *module)
/*[clinic end generated code]*/
{
_Py_StatsClear();
Py_RETURN_NONE;
}

#endif

#ifdef ANDROID_API_LEVEL
/*[clinic input]
sys.getandroidapilevel
Expand Down Expand Up @@ -1978,6 +2035,11 @@ static PyMethodDef sys_methods[] = {
SYS_GET_ASYNCGEN_HOOKS_METHODDEF
SYS_GETANDROIDAPILEVEL_METHODDEF
SYS_UNRAISABLEHOOK_METHODDEF
#ifdef Py_STATS
SYS__STATS_ON_METHODDEF
SYS__STATS_OFF_METHODDEF
SYS__STATS_CLEAR_METHODDEF
#endif
{NULL, NULL} // sentinel
};

Expand Down