mirror of
https://github.com/python/cpython.git
synced 2025-07-19 09:15:34 +00:00
gh-114312: Collect stats for unlikely events (GH-114493)
This commit is contained in:
parent
c63c6142f9
commit
ea3cd0498c
11 changed files with 199 additions and 1 deletions
|
@ -605,6 +605,12 @@ init_interp_create_gil(PyThreadState *tstate, int gil)
|
|||
_PyEval_InitGIL(tstate, own_gil);
|
||||
}
|
||||
|
||||
static int
|
||||
builtins_dict_watcher(PyDict_WatchEvent event, PyObject *dict, PyObject *key, PyObject *new_value)
|
||||
{
|
||||
RARE_EVENT_INC(builtin_dict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static PyStatus
|
||||
pycore_create_interpreter(_PyRuntimeState *runtime,
|
||||
|
@ -1266,6 +1272,14 @@ init_interp_main(PyThreadState *tstate)
|
|||
}
|
||||
}
|
||||
|
||||
if ((interp->rare_events.builtins_dict_watcher_id = PyDict_AddWatcher(&builtins_dict_watcher)) == -1) {
|
||||
return _PyStatus_ERR("failed to add builtin dict watcher");
|
||||
}
|
||||
|
||||
if (PyDict_Watch(interp->rare_events.builtins_dict_watcher_id, interp->builtins) != 0) {
|
||||
return _PyStatus_ERR("failed to set builtin dict watcher");
|
||||
}
|
||||
|
||||
assert(!_PyErr_Occurred(tstate));
|
||||
|
||||
return _PyStatus_OK();
|
||||
|
@ -1592,6 +1606,10 @@ static void
|
|||
finalize_modules(PyThreadState *tstate)
|
||||
{
|
||||
PyInterpreterState *interp = tstate->interp;
|
||||
|
||||
// Stop collecting stats on __builtin__ modifications during teardown
|
||||
PyDict_Unwatch(interp->rare_events.builtins_dict_watcher_id, interp->builtins);
|
||||
|
||||
PyObject *modules = _PyImport_GetModules(interp);
|
||||
if (modules == NULL) {
|
||||
// Already done
|
||||
|
|
|
@ -2616,6 +2616,7 @@ _PyInterpreterState_SetEvalFrameFunc(PyInterpreterState *interp,
|
|||
if (eval_frame != NULL) {
|
||||
_Py_Executors_InvalidateAll(interp);
|
||||
}
|
||||
RARE_EVENT_INC(set_eval_frame_func);
|
||||
interp->eval_frame = eval_frame;
|
||||
}
|
||||
|
||||
|
|
|
@ -267,6 +267,16 @@ print_optimization_stats(FILE *out, OptimizationStats *stats)
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
print_rare_event_stats(FILE *out, RareEventStats *stats)
|
||||
{
|
||||
fprintf(out, "Rare event (set_class): %" PRIu64 "\n", stats->set_class);
|
||||
fprintf(out, "Rare event (set_bases): %" PRIu64 "\n", stats->set_bases);
|
||||
fprintf(out, "Rare event (set_eval_frame_func): %" PRIu64 "\n", stats->set_eval_frame_func);
|
||||
fprintf(out, "Rare event (builtin_dict): %" PRIu64 "\n", stats->builtin_dict);
|
||||
fprintf(out, "Rare event (func_modification): %" PRIu64 "\n", stats->func_modification);
|
||||
}
|
||||
|
||||
static void
|
||||
print_stats(FILE *out, PyStats *stats)
|
||||
{
|
||||
|
@ -275,6 +285,7 @@ print_stats(FILE *out, PyStats *stats)
|
|||
print_object_stats(out, &stats->object_stats);
|
||||
print_gc_stats(out, stats->gc_stats);
|
||||
print_optimization_stats(out, &stats->optimization_stats);
|
||||
print_rare_event_stats(out, &stats->rare_event_stats);
|
||||
}
|
||||
|
||||
void
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue