Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-103082: Turn on branch events for FOR_ITER instructions. #103507

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions Lib/test/test_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,77 @@ def func3():
('line', 'func3', 6)])


def line_from_offset(code, offset):
for start, end, line in code.co_lines():
if start <= offset < end:
return line - code.co_firstlineno
return -1

class JumpRecorder:

event_type = E.JUMP
name = "jump"

def __init__(self, events):
self.events = events

def __call__(self, code, from_, to):
from_line = line_from_offset(code, from_)
to_line = line_from_offset(code, to)
self.events.append((self.name, code.co_name, from_line, to_line))


class BranchRecorder(JumpRecorder):

event_type = E.BRANCH
name = "branch"


JUMP_AND_BRANCH_RECORDERS = JumpRecorder, BranchRecorder
JUMP_BRANCH_AND_LINE_RECORDERS = JumpRecorder, BranchRecorder, LineRecorder

class TestBranchAndJumpEvents(CheckEvents):
maxDiff = None

def test_loop(self):

def func():
x = 1
for a in range(2):
if a:
x = 4
else:
x = 6

self.check_events(func, recorders = JUMP_AND_BRANCH_RECORDERS, expected = [
('branch', 'func', 2, 2),
('branch', 'func', 3, 6),
('jump', 'func', 6, 2),
('branch', 'func', 2, 2),
('branch', 'func', 3, 4),
('jump', 'func', 4, 2),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this the jump over the else?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this is the jump back to the start of the loop.

('branch', 'func', 2, 2)])


self.check_events(func, recorders = JUMP_BRANCH_AND_LINE_RECORDERS, expected = [
('line', 'check_events', 10),
('line', 'func', 1),
('line', 'func', 2),
('branch', 'func', 2, 2),
('line', 'func', 3),
('branch', 'func', 3, 6),
('line', 'func', 6),
('jump', 'func', 6, 2),
('branch', 'func', 2, 2),
('line', 'func', 3),
('branch', 'func', 3, 4),
('line', 'func', 4),
('jump', 'func', 4, 2),
('branch', 'func', 2, 2),
('line', 'func', 2),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't the line event suppose to come before the branch on that line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line event becomes before any other events for that instruction, but the jumps and branches are for other instructions.

The jump 4 -> 2 is the jump back to the FOR_ITER. The FOR_ITER instruction isn't the first instruction on line 2. The branch 2 -> 2 is the exit from the FOR_ITER.
The final "line 2" event is from the implicit return None which has line number 2.
It is marked as a line event because the previous instruction has a different line number.

TBH, line events aren't that useful for this sort of tracing, their use case is for debuggers.

('line', 'check_events', 11)])


class TestSetGetEvents(MonitoringTestBase, unittest.TestCase):

def test_global(self):
Expand Down
2 changes: 2 additions & 0 deletions Python/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ static const uint8_t INSTRUMENTED_OPCODES[256] = {
[INSTRUMENTED_END_FOR] = INSTRUMENTED_END_FOR,
[END_SEND] = INSTRUMENTED_END_SEND,
[INSTRUMENTED_END_SEND] = INSTRUMENTED_END_SEND,
[FOR_ITER] = INSTRUMENTED_FOR_ITER,
[INSTRUMENTED_FOR_ITER] = INSTRUMENTED_FOR_ITER,

[INSTRUMENTED_LINE] = INSTRUMENTED_LINE,
[INSTRUMENTED_INSTRUCTION] = INSTRUMENTED_INSTRUCTION,
Expand Down