summaryrefslogtreecommitdiff
path: root/Python/specialize.c
diff options
context:
space:
mode:
authorMark Shannon <mark@hotpy.org>2021-08-04 11:39:52 +0100
committerGitHub <noreply@github.com>2021-08-04 11:39:52 +0100
commitc83919bd635f4433f1c6ae8504996a9fe3c215e5 (patch)
tree820040d9928408060179d684adc4775f98df76d0 /Python/specialize.c
parentac811f9b5a68ce8756911ef2c8be83b46696018f (diff)
downloadcpython-git-c83919bd635f4433f1c6ae8504996a9fe3c215e5.tar.gz
Add option to write specialization stats to files and script to summarize. (GH-27575)
* Add option to write stats to random file in a directory. * Add script to summarize stats.
Diffstat (limited to 'Python/specialize.c')
-rw-r--r--Python/specialize.c43
1 files changed, 31 insertions, 12 deletions
diff --git a/Python/specialize.c b/Python/specialize.c
index 680ffb428e..1ca49d244a 100644
--- a/Python/specialize.c
+++ b/Python/specialize.c
@@ -117,10 +117,10 @@ _Py_GetSpecializationStats(void) {
#endif
-#define PRINT_STAT(name, field) fprintf(stderr, " %s." #field " : %" PRIu64 "\n", name, stats->field);
+#define PRINT_STAT(name, field) fprintf(out, " %s." #field " : %" PRIu64 "\n", name, stats->field);
static void
-print_stats(SpecializationStats *stats, const char *name)
+print_stats(FILE *out, SpecializationStats *stats, const char *name)
{
PRINT_STAT(name, specialization_success);
PRINT_STAT(name, specialization_failure);
@@ -133,18 +133,18 @@ print_stats(SpecializationStats *stats, const char *name)
if (stats->miss_types == NULL) {
return;
}
- fprintf(stderr, " %s.fails:\n", name);
+ fprintf(out, " %s.fails:\n", name);
PyObject *key, *count;
Py_ssize_t pos = 0;
while (PyDict_Next(stats->miss_types, &pos, &key, &count)) {
PyObject *type = PyTuple_GetItem(key, 0);
PyObject *name = PyTuple_GetItem(key, 1);
PyObject *kind = PyTuple_GetItem(key, 2);
- fprintf(stderr, " %s.", ((PyTypeObject *)type)->tp_name);
- PyObject_Print(name, stderr, Py_PRINT_RAW);
- fprintf(stderr, " (");
- PyObject_Print(kind, stderr, Py_PRINT_RAW);
- fprintf(stderr, "): %ld\n", PyLong_AsLong(count));
+ fprintf(out, " %s.", ((PyTypeObject *)type)->tp_name);
+ PyObject_Print(name, out, Py_PRINT_RAW);
+ fprintf(out, " (");
+ PyObject_Print(kind, out, Py_PRINT_RAW);
+ fprintf(out, "): %ld\n", PyLong_AsLong(count));
}
#endif
}
@@ -153,10 +153,29 @@ print_stats(SpecializationStats *stats, const char *name)
void
_Py_PrintSpecializationStats(void)
{
- printf("Specialization stats:\n");
- print_stats(&_specialization_stats[LOAD_ATTR], "load_attr");
- print_stats(&_specialization_stats[LOAD_GLOBAL], "load_global");
- print_stats(&_specialization_stats[BINARY_SUBSCR], "binary_subscr");
+ FILE *out = stderr;
+#if SPECIALIZATION_STATS_TO_FILE
+ /* Write to a file instead of stderr. */
+# ifdef MS_WINDOWS
+ const char *dirname = "c:\\temp\\py_stats\\";
+# else
+ const char *dirname = "/tmp/py_stats/";
+# endif
+ char buf[48];
+ sprintf(buf, "%s%u_%u.txt", dirname, (unsigned)clock(), (unsigned)rand());
+ FILE *fout = fopen(buf, "w");
+ if (fout) {
+ out = fout;
+ }
+#else
+ fprintf(out, "Specialization stats:\n");
+#endif
+ print_stats(out, &_specialization_stats[LOAD_ATTR], "load_attr");
+ print_stats(out, &_specialization_stats[LOAD_GLOBAL], "load_global");
+ print_stats(out, &_specialization_stats[BINARY_SUBSCR], "binary_subscr");
+ if (out != stderr) {
+ fclose(out);
+ }
}
#if SPECIALIZATION_STATS_DETAILED