diff options
author | Michael Droettboom <mdboom@gmail.com> | 2024-01-25 11:10:51 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-25 11:10:51 (GMT) |
commit | ea3cd0498c443e93be441736c804258e93d21edd (patch) | |
tree | ab975dc0fe8c933fd2c197275a7d9fc6598aa04b /Python | |
parent | c63c6142f9146e1e977f4c824c56e8979e6aca87 (diff) | |
download | cpython-ea3cd0498c443e93be441736c804258e93d21edd.zip cpython-ea3cd0498c443e93be441736c804258e93d21edd.tar.gz cpython-ea3cd0498c443e93be441736c804258e93d21edd.tar.bz2 |
gh-114312: Collect stats for unlikely events (GH-114493)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/pylifecycle.c | 18 | ||||
-rw-r--r-- | Python/pystate.c | 1 | ||||
-rw-r--r-- | Python/specialize.c | 11 |
3 files changed, 30 insertions, 0 deletions
diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 0d5eec0..261622a 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -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 diff --git a/Python/pystate.c b/Python/pystate.c index 548c77b..c9b5213 100644 --- a/Python/pystate.c +++ b/Python/pystate.c @@ -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; } diff --git a/Python/specialize.c b/Python/specialize.c index 13e0440..a9efbe0 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -268,6 +268,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) { print_spec_stats(out, stats->opcode_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 |