8000 GH-93841: Turn stats on and off, clear and dump them at runtime. by markshannon · Pull Request #93843 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-93841: Turn stats on and off, clear and dump them at runtime. #93843

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

Merged
merged 8 commits into from
Jun 21, 2022
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
Fixups and add dump function.
  • Loading branch information
markshannon committed Jun 15, 2022
commit d6ce8e62f15065a8166468dc26c8545a41a4db15
28 changes: 27 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.

7 changes: 5 additions & 2 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,15 @@ print_stats(FILE *out, PyStats *stats) {
void
_Py_StatsClear(void)
{
_Py_stat_struct = { 0 };
_py_stats_struct = (PyStats) { 0 };
}

void
_Py_PrintSpecializationStats(int to_file)
{
if (_py_stats == NULL) {
return;
}
FILE *out = stderr;
if (to_file) {
/* Write to a file instead of stderr. */
Expand Down Expand Up @@ -249,7 +252,7 @@ _Py_PrintSpecializationStats(int to_file)
else {
fprintf(out, "Specialization stats:\n");
}
print_stats(out, &_py_stats);
print_stats(out, _py_stats);
if (out != stderr) {
fclose(out);
}
Expand Down
24 changes: 14 additions & 10 deletions Python/sysmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1919,10 +1919,6 @@ Turns on stats gathering (stats gathering is on by default)
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;
Expand All @@ -1937,10 +1933,6 @@ Turns off stats gathering (stats gathering is on by default)
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
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
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;
Expand All @@ -1955,11 +1947,22 @@ Clears stats
static PyObject *
sys__stats_clear_impl(PyObject *module)
/*[clinic end generated code: output=fb65a2525ee50604 input=0bd23b30a48f67ab]*/
{
_Py_StatsClear();
Py_RETURN_NONE;
}

/*[clinic input]
sys._stats_dump

Dump stats to file, and clear current stats.
[clinic start generated code]*/

static PyObject *
sys__stats_clear(PyObject *module)
/*[clinic end generated code]*/
sys__stats_dump_impl(PyObject *module)
/*[clinic end generated code: output=79f796fb2b4ddf05 input=b2e51dae2fe969b1]*/
{
_Py_PrintSpecializationStats(1);
_Py_StatsClear();
Py_RETURN_NONE;
}
Expand Down Expand Up @@ -2039,6 +2042,7 @@ static PyMethodDef sys_methods[] = {
SYS__STATS_ON_METHODDEF
SYS__STATS_OFF_METHODDEF
SYS__STATS_CLEAR_METHODDEF
SYS__STATS_DUMP_METHODDEF
#endif
{NULL, NULL} // sentinel
};
Expand Down
0