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: Implementation of PEP 669: Low Impact Monitoring for CPython #103083
base: main
Are you sure you want to change the base?
Conversation
|
I'd like to invite @nedbat to review this PR, in particular, to see if it breaks or improves anything related to coverage.py. Ned, I know that you're busy, but your early feedback is really important here! |
|
Thanks for the ping. I don't know that I'll be able to review the code, but I ran the coverage.py test suite with it, and everything passed, so "first do no harm" checks out. We should talk about what else I can do here. I don't know what effort would be required to try to take advantage of the new changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I'm not going to finish the review today, here are some UX nits. (I trust Erlend to catch the C code nits. :-)
I did try to play around with it using the PEP as documentation, and I found a few things (some probably just in need of PEP tweaks).
- All in all I really like the experience of tracing using this new API!
- The PEP is pretty terse: This will need user documentation as part of the library reference; maybe even a tutorial. (Not in this PR of course, but let's please keep the issue open or create a separate doc issue.)
- It took me a while to figure out that
event_setis a "set" constructed by OR-ing various events together (a common C idiom of course but not apparent until you see their integer values, and I intentionally didn't look at the code for this). set_local_events:tool_idandcodeargs are swapped compared to the PEP.dis.dis(code, adaptive=True)shows theRESERVEDandCACHEopcodes following any INSTRUMENTED opcodes (even thoughshow_cachesdefaults toFalse).- I found it a little inconvenient that
instruction_offsetis measured in instructions, not in bytes (I was usingdisto see what I was getting). There was a lot ofinstr*2in my code. - The naming of the CALL and RETURN/RAISE events is a bit confusing -- we have
CALL(universal),C_RETURN/PY_RETURN(per language), but thenC_RAISEandRAISE-- why isn't the latterPY_RAISE, just for consistency? - I see that the PEP prescribes that INSTRUCTION events are reported before LINE events on the same instruction. I found this confusing. Is it too late to change this around? Since a line can contain many instructions, it would appear more useful to see the LINE event first, so I can e.g. just do
print("LINE", lineno)in the LINE callback and print all instructions that are logically part of the line following that point. - How do I retrieve additional arguments if there are more than 1?
| } | ||
|
|
||
|
|
||
| PyTypeObject _PyLegacyEventHandler_Type = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be added to
Line 1982 in 47753ec
| static PyTypeObject* static_types[] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
Shared static objects need to be stateless (and ideally immortal and immutable once we have immortal objects).
There can be some very minimal state provided that initializing it is idempotent and thread-safe, but that should be the responsibility not of the class, but the interpreter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/python/cpython/blob/main/Objects/typeobject.c#L7012, it seems that _PyStaticType_InitBuiltin, called from _PyTypes_InitTypes in object.c for every type in that array, finishes initializing types (not ideal, but that's the current convention).
Thanks
I've updated the issue to mention documentation
I didn't really give this much thought. As soon as you set
Sure, we can change the offsets to be bytecode offsets. It would be more consistent with everything else.
The If
I don't think there is a natural or correct order of events here. There is no guarantee that the compiler will put the line event on the instruction you expect. For example: def f():
a = 1
pass
return 2Could (and probably should) be compiled as:
You don't. The first argument is only provided to support the strange behavior of Would it be useful to do so? |
|
I should say that I'm not surprised this works on coverage, thanks to @nedbat's tireless efforts to make sure every corner case of tracing was tested for PEP 626. Thanks to Ned, we have a pretty solid set of tests for line tracing. The tests for |
|
@fabioz Just an FYI that this is about to land. Are you interested in trying this out? |
I don't want to hide the INSTRUMENTED opcodes (or even show the original opcodes), but I'd like to hide the RESERVED/CACHED opcodes unless show_caches=True is also passed. But I just realized that dis doesn't have the information needed to even show the underlying instruction -- I guess it'd have to be passed both the adaptive and the original bytecode arrays. Given this, I don't care enough to even file that issue. :-)
Okay, I think I follow this now. It's definitely worth calling out these subtleties in the user docs -- it's much easier to remember if you know the underlying model.
I don't understand why in that example the line number for the STORE_FAST shouldn't be the same as that for the LOAD_CONST. Maybe you copied/pasted the wrong function? The key point seems to be your assertion "There is no guarantee that the compiler will put the line event on the instruction you expect." Maybe not in obscure corner cases, like situations where there is code emitted that doesn't naturally belong to a given line, like the I suspect that the reason has more to do with how it's implemented -- I'm still trying to understand how INSTRUMENTED_INSTRUCTION ends up calling INSTRUMENTED_LINE (if it even does so). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's some more. Still to do: instrumentation.c (not sure if I'll get to it, don't wait for me).
| @@ -2973,69 +2612,6 @@ PyUnstable_Eval_RequestCodeExtraIndex(freefunc free) | |||
| return new_index; | |||
| } | |||
|
|
|||
| static void | |||
| dtrace_function_entry(_PyInterpreterFrame *frame) | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like DTRACE is now dead? There are a few relics in pyconfig.h, pydtrace.h, and in ceval_macros.h.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have DTRACE tests and there are a few open issues about it not working as expected.
Rather than get it wrong, I think it is better to not support it for now.
I think @ambv wants to fix up DTRACE support, so let's leave the pydtrace.h and the macros for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it worth opening an issue to determine the fate of DTRACE support? @ambv
| "\n"); | ||
|
|
||
| #define MONITORING_USE_TOOL_ID_METHODDEF \ | ||
| {"use_tool_id", _PyCFunction_CAST(monitoring_use_tool_id), METH_FASTCALL, monitoring_use_tool_id__doc__}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just discovered this. Shouldn't the fundamental functions (set_events etc.) check that the tool ID is registered? That doesn't really prevent stomping on someone else intentionally, but at least it makes people properly register their tool id.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spec is that registering IDs is a politeness, not a requirement.
The value of having IDs is that well behaved tools don't accidentally trample on each other.
Forcing tools to register might give an illusion of ownership.
There is no way to check that the tool that registered the ID is the same one as is setting events, nor is there anyway to stop a tool taking another's ID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But checking that the ID is registered helps everyone stay polite. These IDs are like file descriptors. I like to require registration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Come to think of it, maybe it could work like open() and return the next available ID.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Come to think of it, maybe it could work like open() and return the next available ID.)
There are recommended IDs for tools to use. The idea being that you might want a debugger and coverage tool, but you almost certainly don't want two debuggers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point, but this is a change to the PEP, with all the rigmarole that involves.
The code change should be simple enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The predefined IDs could be like pre-opens FDs, e.g. 0, 1, 2 for std(in,out,err). I’ll take it to the SC as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 on the approach of asking for the next available id.
| } | ||
|
|
||
|
|
||
| PyTypeObject _PyLegacyEventHandler_Type = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See https://github.com/python/cpython/blob/main/Objects/typeobject.c#L7012, it seems that _PyStaticType_InitBuiltin, called from _PyTypes_InitTypes in object.c for every type in that array, finishes initializing types (not ideal, but that's the current convention).
No copy and paste error. That is a correct translation of the function. And that's the point: it is correct and does not conform to your intuition of the sequencing of line events and instructions.
|
So assuming the compiler is allowed to translate it that way, I still think the instrumentation should report the lineno before the first instruction considered to be part of that line. Then the LINE callback can assign the line number to some variable inside the debugger and the INSTRUCTION callback can safely assume that that line number is the best estimate of the line to which the instruction belongs (according to the compiler). |
That seems awfully complicated. You'll need to maintain a stack of line numbers to handle calls, handle every call/return call/raise, send/yield, etc pair. |
|
@gvanrossum
to:
is feasible. It's a bit of work, but not too much. FTR: any other order is infeasible. All other events must come after |
|
@gvanrossum thanks for the ping :) @markshannon I'm testing this version (still using the tracing) with the debugger (debugpy/pydev.debugger) and I'm facing a bug (note: I confirmed it works in the main branch and fails just after this PR is applied). Consider the code below: Now, if you use the current debugger version (you can use the latest vscode with the python extension or eclipse/pydev to reproduce) and place a breakpoint at the line: As far as I could see from the debugger side, this is because the Note that this doesn't reproduce in a really simple tracer as the one below (in that case it prints the lines correctly as expected). I can only reproduce it when running in the actual debugger and it actually stops in a breakpoint (presumably because when the debugger is stopped at a breakpoint, lots of things happen such as getting local variables, etc). p.s.: To see the file received in the debugger during the tracing, there's a print that can be uncommented at the top of the following method: |
I would be happy to do that. Given that this is the first user feedback on a complex PEP this shouldn’t run into opposition.
I have no problem with that. The order LINE, INSTRUCTION, everything else makes the most sense to me. |


This implements PEP 669.
There are a couple of things missing, but no harm in early review.