Skip to content

Commit

Permalink
pythongh-112040: Support parallel pystats collection
Browse files Browse the repository at this point in the history
  • Loading branch information
mdboom committed Apr 24, 2024
1 parent 345e1e0 commit 53c6bba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
7 changes: 4 additions & 3 deletions Doc/using/configure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,10 @@ General Options
* :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/`` (Unix) or ``C:\temp\py_stats\`` (Windows). If that
directory does not exist, results will be printed on stderr.
The statistics will be dumped to a file in the directory specifed by the
environment variable ``PYTHON_PYSTATS_DIR`` (or ``/tmp/py_stats/`` (Unix) or
``C:\temp\py_stats\`` (Windows) if not specified). If that directory does not
exist, results will be printed on stderr.

Use ``Tools/scripts/summarize_stats.py`` to read the stats.

Expand Down
11 changes: 7 additions & 4 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,12 +366,15 @@ _Py_PrintSpecializationStats(int to_file)

FILE *out = stderr;
if (to_file) {
const char *dirname = getenv("PYTHON_PYSTATS_DIR");
/* Write to a file instead of stderr. */
if (dirname == NULL) {
# ifdef MS_WINDOWS
const char *dirname = "c:\\temp\\py_stats\\";
dirname = "c:\\temp\\py_stats\\";
# else
const char *dirname = "/tmp/py_stats/";
dirname = "/tmp/py_stats/";
# endif
}
/* Use random 160 bit number as file name,
* to avoid both accidental collisions and
* symlink attacks. */
Expand All @@ -383,8 +386,8 @@ _Py_PrintSpecializationStats(int to_file)
hex_name[2*i+1] = Py_hexdigits[(rand[i]>>4)&15];
}
hex_name[40] = '\0';
char buf[64];
assert(strlen(dirname) + 40 + strlen(".txt") < 64);
char buf[128];
assert(strlen(dirname) + 40 + strlen(".txt") < 128);
sprintf(buf, "%s%s.txt", dirname, hex_name);
FILE *fout = fopen(buf, "w");
if (fout) {
Expand Down

0 comments on commit 53c6bba

Please sign in to comment.