bpo-44725 : expose specialization stats in python (GH-27192)

This commit is contained in:
Irit Katriel 2021-07-29 17:26:53 +01:00 committed by GitHub
parent 6741794dd4
commit ddd1c418c0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 158 additions and 1 deletions

View file

@ -40,6 +40,83 @@ Py_ssize_t _Py_QuickenedCount = 0;
#if SPECIALIZATION_STATS
SpecializationStats _specialization_stats[256] = { 0 };
#define ADD_STAT_TO_DICT(res, field) \
do { \
PyObject *val = PyLong_FromUnsignedLongLong(stats->field); \
if (val == NULL) { \
Py_DECREF(res); \
return NULL; \
} \
if (PyDict_SetItemString(res, #field, val) == -1) { \
Py_DECREF(res); \
Py_DECREF(val); \
return NULL; \
} \
Py_DECREF(val); \
} while(0);
static PyObject*
stats_to_dict(SpecializationStats *stats)
{
PyObject *res = PyDict_New();
if (res == NULL) {
return NULL;
}
ADD_STAT_TO_DICT(res, specialization_success);
ADD_STAT_TO_DICT(res, specialization_failure);
ADD_STAT_TO_DICT(res, hit);
ADD_STAT_TO_DICT(res, deferred);
ADD_STAT_TO_DICT(res, miss);
ADD_STAT_TO_DICT(res, deopt);
ADD_STAT_TO_DICT(res, unquickened);
#if SPECIALIZATION_STATS_DETAILED
if (stats->miss_types != NULL) {
if (PyDict_SetItemString(res, "fails", stats->miss_types) == -1) {
Py_DECREF(res);
return NULL;
}
}
#endif
return res;
}
#undef ADD_STAT_TO_DICT
static int
add_stat_dict(
PyObject *res,
int opcode,
const char *name) {
SpecializationStats *stats = &_specialization_stats[opcode];
PyObject *d = stats_to_dict(stats);
if (d == NULL) {
return -1;
}
int err = PyDict_SetItemString(res, name, d);
Py_DECREF(d);
return err;
}
#if SPECIALIZATION_STATS
PyObject*
_Py_GetSpecializationStats(void) {
PyObject *stats = PyDict_New();
if (stats == NULL) {
return NULL;
}
int err = 0;
err += add_stat_dict(stats, LOAD_ATTR, "load_attr");
err += add_stat_dict(stats, LOAD_GLOBAL, "load_global");
err += add_stat_dict(stats, BINARY_SUBSCR, "binary_subscr");
if (err < 0) {
Py_DECREF(stats);
return NULL;
}
return stats;
}
#endif
#define PRINT_STAT(name, field) fprintf(stderr, " %s." #field " : %" PRIu64 "\n", name, stats->field);
static void
@ -71,6 +148,7 @@ print_stats(SpecializationStats *stats, const char *name)
}
#endif
}
#undef PRINT_STAT
void
_Py_PrintSpecializationStats(void)