gh-108753: Enhance pystats (#108754)

Statistics gathering is now off by default. Use the "-X pystats"
command line option or set the new PYTHONSTATS environment variable
to 1 to turn statistics gathering on at Python startup.

Statistics are no longer dumped at exit if statistics gathering was
off or statistics have been cleared.

Changes:

* Add PYTHONSTATS environment variable.
* sys._stats_dump() now returns False if statistics are not dumped
  because they are all equal to zero.
* Add PyConfig._pystats member.
* Add tests on sys functions and on setting PyConfig._pystats to 1.
* Add Include/cpython/pystats.h and Include/internal/pycore_pystats.h
  header files.
* Rename '_py_stats' variable to '_Py_stats'.
* Exclude Include/cpython/pystats.h from the Py_LIMITED_API.
* Move pystats.h include from object.h to Python.h.
* Add _Py_StatsOn() and _Py_StatsOff() functions. Remove
  '_py_stats_struct' variable from the API: make it static in
  specialize.c.
* Document API in Include/pystats.h and Include/cpython/pystats.h.
* Complete pystats documentation in Doc/using/configure.rst.
* Don't write "all zeros" stats: if _stats_off() and _stats_clear()
  or _stats_dump() were called.
* _PyEval_Fini() now always call _Py_PrintSpecializationStats() which
  does nothing if stats are all zeros.

Co-authored-by: Michael Droettboom <mdboom@gmail.com>
This commit is contained in:
Victor Stinner 2023-09-06 17:54:59 +02:00 committed by GitHub
parent 8ff1142578
commit a0773b89df
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 403 additions and 184 deletions

View file

@ -192,14 +192,69 @@ General Options
.. cmdoption:: --enable-pystats
Turn on internal statistics gathering.
Turn on internal Python performance statistics gathering.
By default, statistics gathering is off. Use ``python3 -X pystats`` command
or set ``PYTHONSTATS=1`` environment variable to turn on statistics
gathering at Python startup.
At Python exit, dump statistics if statistics gathering was on and not
cleared.
Effects:
* Add :option:`-X pystats <-X>` command line option.
* Add :envvar:`!PYTHONSTATS` environment variable.
* Define the ``Py_STATS`` macro.
* Add functions to the :mod:`sys` module:
* :func:`!sys._stats_on`: Turns on statistics gathering.
* :func:`!sys._stats_off`: Turns off statistics gathering.
* :func:`!sys._stats_clear`: Clears the statistics.
* :func:`!sys._stats_dump`: Dump statistics to file, and clears the statistics.
The statistics will be dumped to a arbitrary (probably unique) file in
``/tmp/py_stats/``, or ``C:\temp\py_stats\`` on Windows. If that directory
does not exist, results will be printed on stdout.
``/tmp/py_stats/`` (Unix) or ``C:\temp\py_stats\`` (Windows). If that
directory does not exist, results will be printed on stderr.
Use ``Tools/scripts/summarize_stats.py`` to read the stats.
Statistics:
* Opcode:
* Specialization: success, failure, hit, deferred, miss, deopt, failures;
* Execution count;
* Pair count.
* Call:
* Inlined Python calls;
* PyEval calls;
* Frames pushed;
* Frame object created;
* Eval calls: vector, generator, legacy, function VECTORCALL, build class,
slot, function "ex", API, method.
* Object:
* incref and decref;
* interpreter incref and decref;
* allocations: all, 512 bytes, 4 kiB, big;
* free;
* to/from free lists;
* dictionary materialized/dematerialized;
* type cache;
* optimization attemps;
* optimization traces created/executed;
* uops executed.
* Garbage collector:
* Garbage collections;
* Objects visited;
* Objects collected.
.. versionadded:: 3.11
.. cmdoption:: --disable-gil