Skip to content

Commit 7761135

Browse files
committed
keep local vs other separation
1 parent 958595a commit 7761135

File tree

5 files changed

+48
-50
lines changed

5 files changed

+48
-50
lines changed

Doc/library/sys.monitoring.rst

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,9 @@ events, use the expression ``PY_RETURN | PY_START``.
179179
Local events
180180
''''''''''''
181181

182-
Local events are associated with execution of a particular code object and
183-
can all be disabled via :data:`DISABLE`. There are two kinds, which differ
184-
in how :data:`DISABLE` takes effect.
185-
186-
**Instrumented local events** require bytecode instrumentation and fire at
187-
clearly defined instruction locations within a function. Returning
188-
:data:`DISABLE` from a callback disables the event at that specific
189-
code location only, leaving all other locations unaffected:
182+
Local events are associated with normal execution of the program and happen
183+
at clearly defined locations. All local events can be disabled
184+
per location. The local events are:
190185

191186
* :monitoring-event:`PY_START`
192187
* :monitoring-event:`PY_RESUME`
@@ -200,24 +195,6 @@ code location only, leaving all other locations unaffected:
200195
* :monitoring-event:`BRANCH_RIGHT`
201196
* :monitoring-event:`STOP_ITERATION`
202197

203-
**Non-instrumented local events** do not require bytecode instrumentation and
204-
are not tied to a specific instruction. They can fire at any point within
205-
a function where the triggering condition occurs. Returning :data:`DISABLE`
206-
from a callback disables the event for the entire code object (for the
207-
current tool):
208-
209-
* :monitoring-event:`PY_UNWIND`
210-
* :monitoring-event:`EXCEPTION_HANDLED`
211-
* :monitoring-event:`RAISE`
212-
* :monitoring-event:`PY_THROW`
213-
* :monitoring-event:`RERAISE`
214-
215-
.. versionchanged:: 3.15
216-
:monitoring-event:`PY_UNWIND`, :monitoring-event:`EXCEPTION_HANDLED`,
217-
:monitoring-event:`RAISE`, :monitoring-event:`PY_THROW`, and
218-
:monitoring-event:`RERAISE` are now local events and can be disabled
219-
via :data:`DISABLE`.
220-
221198
Deprecated event
222199
''''''''''''''''
223200

@@ -245,6 +222,28 @@ are controlled by the :monitoring-event:`CALL` event.
245222
seen if the corresponding :monitoring-event:`CALL` event is being monitored.
246223

247224

225+
.. _monitoring-event-global:
226+
227+
Other events
228+
''''''''''''
229+
230+
Other events are not necessarily tied to a specific location in the
231+
program and cannot be individually disabled per location.
232+
233+
The other events that can be monitored are:
234+
235+
* :monitoring-event:`PY_THROW`
236+
* :monitoring-event:`PY_UNWIND`
237+
* :monitoring-event:`RAISE`
238+
* :monitoring-event:`EXCEPTION_HANDLED`
239+
* :monitoring-event:`RERAISE`
240+
241+
.. versionchanged:: 3.15
242+
Other events can now be turned on and disabled on a per code object
243+
basis. Returning :data:`DISABLE` from a callback disables the event
244+
for the entire code object (for the current tool).
245+
246+
248247
The STOP_ITERATION event
249248
''''''''''''''''''''''''
250249

@@ -315,23 +314,21 @@ Disabling events
315314
.. data:: DISABLE
316315

317316
A special value that can be returned from a callback function to disable
318-
the event.
319-
320-
All :ref:`local events <monitoring-event-local>` can be disabled by returning
321-
:data:`sys.monitoring.DISABLE` from a callback function. This does not change
322-
which events are set globally.
317+
events for the current code location.
323318

324-
For :ref:`instrumented local events <monitoring-event-local>`, :data:`DISABLE`
325-
disables the event at the specific code location where it fired only, leaving
326-
all other locations for the same event unaffected.
319+
:ref:`Local events <monitoring-event-local>` can be disabled for a specific code
320+
location by returning :data:`sys.monitoring.DISABLE` from a callback function.
321+
This does not change which events are set, or any other code locations for the
322+
same event.
327323

328-
For :ref:`non-instrumented local events <monitoring-event-local>`,
329-
:data:`DISABLE` disables the event for the entire code object (for the current
324+
:ref:`Other events <monitoring-event-global>` can be disabled on a per code
325+
object basis by returning :data:`sys.monitoring.DISABLE` from a callback
326+
function. This disables the event for the entire code object (for the current
330327
tool).
331328

332-
Disabling events is very important for high performance monitoring. For
333-
example, a program can be run under a debugger with no overhead if the
334-
debugger disables all monitoring except for a few breakpoints.
329+
Disabling events for specific locations is very important for high performance
330+
monitoring. For example, a program can be run under a debugger with no overhead
331+
if the debugger disables all monitoring except for a few breakpoints.
335332

336333
.. function:: restart_events() -> None
337334

Include/cpython/monitoring.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ extern "C" {
88
// There is currently no limited API for monitoring
99

1010

11-
/* Local events (tracked per code object).
12-
* Events 0-10 require bytecode instrumentation; 11-15 do not. */
11+
/* Local events.
12+
* These require bytecode instrumentation */
1313

1414
#define PY_MONITORING_EVENT_PY_START 0
1515
#define PY_MONITORING_EVENT_PY_RESUME 1
@@ -26,6 +26,9 @@ extern "C" {
2626
#define PY_MONITORING_IS_INSTRUMENTED_EVENT(ev) \
2727
((ev) <= PY_MONITORING_EVENT_STOP_ITERATION)
2828

29+
/* Other events, mainly exceptions.
30+
* These can now be turned on and disabled on a per code object basis. */
31+
2932
#define PY_MONITORING_EVENT_PY_UNWIND 11
3033
#define PY_MONITORING_EVENT_EXCEPTION_HANDLED 12
3134
#define PY_MONITORING_EVENT_RAISE 13

Include/internal/pycore_instruments.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ PyAPI_DATA(PyObject) _PyInstrumentation_DISABLE;
7474
/* Total tool ids available */
7575
#define PY_MONITORING_TOOL_IDS 8
7676
/* Count of all "real" monitoring events (not derived from other events).
77-
* All ungrouped events are now local events. */
77+
* "Other" events can now be turned on/disabled per code object. */
7878
#define _PY_MONITORING_UNGROUPED_EVENTS 16
7979
/* Count of all monitoring events */
8080
#define _PY_MONITORING_EVENTS 19
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
Make all the low-impact monitoring API events local on a per-code object
2-
basis. The sentinel ``DISABLE`` value can be returned to disable them for
3-
the whole code object. The behavior of events that were already local (via
4-
bytecode instrumentation) hsa not been changed.
1+
Global :mod:`sys.monitoring` events can now be turned on and disabled on a
2+
per code object basis. Returning ``DISABLE`` from a callback disables the
3+
event for the entire code object (for the current tool).

Python/instrumentation.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ get_tools_for_instruction(PyCodeObject *code, PyInterpreterState *interp, int i,
11141114
}
11151115
}
11161116
else {
1117-
/* Non-instrumented local events are not tied to specific instructions;
1117+
/* Other (non-instrumented) events are not tied to specific instructions;
11181118
* use the code-object-level active_monitors bitmap instead. */
11191119
tools = code->_co_monitoring->active_monitors.tools[event];
11201120
}
@@ -1143,7 +1143,7 @@ static const char *const event_names [] = {
11431143
[PY_MONITORING_EVENT_STOP_ITERATION] = "STOP_ITERATION",
11441144
};
11451145

1146-
/* Disable a local-but-not-instrumented event (e.g. PY_UNWIND) for a single
1146+
/* Disable an "other" (non-instrumented) event (e.g. PY_UNWIND) for a single
11471147
* tool on this code object. Must be called with the world stopped or the
11481148
* code lock held. */
11491149
static void
@@ -1212,8 +1212,7 @@ call_instrumentation_vector(
12121212
_PyEval_StartTheWorld(interp);
12131213
}
12141214
else if (_PY_MONITORING_IS_UNGROUPED_EVENT(event)) {
1215-
/* Non-instrumented local event: disable for this code object
1216-
* entirely. */
1215+
/* Other (non-instrumented) event: disable for this code object. */
12171216
_PyEval_StopTheWorld(interp);
12181217
remove_local_tool(code, interp, event, tool);
12191218
_PyEval_StartTheWorld(interp);

0 commit comments

Comments
 (0)