Repository navigation
Description
Description
I've been looking at how we implement "Goto Source (Def)" in PerfView for JIT-compiled frames when using ETW (i.e. not EventPipe)
The mapping from stack frame to source goes via a number of steps. One important piece is mapping from { module, RVA } to { Assembly, Namespace, Type, Method, IL offset }
That's achieved by tracking three tables emitted by the CLR's rundown provider: Loader/ModuleDCStart, Method/DCStartVerbose and Method/ILToNativeMapDCStart.
The first two gets us the Assembly, Namespace, Type, Method part. The final table gives us a way to find the IL offset (which, along with the PDB, allows us to identify the line number in source).
If the method in question has been re-JIT-ted -- perhaps due to tiered compilation -- we get two entries in the DCStartVerbose and ILToNativeMapDCStart tables, as you'd expect. In the DCStartVerbose table, the ReJITID can be used to distinguish them. Typically, I see 0 for "QuickJitted" and 1 for "OptimizedTier1". However, in the ILToNativeMapDCStart table the ReJITID always appears to be 0. That makes it difficult to correlate reliably (you have to rely on heuristics).
It looks like a bug that the ReJITID is always 0 in the ILToNativeMapDCStart event.
Reproduction Steps
I can share a .diagsession, if necessary.
- Capture a trace with PerfView of a reasonably complex .NET 6 application (something that exercises tiered compilation)
- Open the trace, view the CPU stacks and try to "Goto Source" for some of the JITted methods. Assuming it works at all, you're likely to end up at the wrong line number. The difference may be very subtle.
More directly (which is how I found it), take a look at the raw events in the trace using PerfView's Events view. Filter to "Method/DCStartVerbose" and "Method/ILToNativeMapDCStart" events. Find an interesting method in the DCStartVerbose table -- one that has been JITted twice for tiered compilation (has a ReJITID of 1). Then search for that MethodID in the ILToNativeMap table. You'll find that it has two entries, each with ReJITID of 0. They likely have completely different IL offsets (view the raw event data) even though they claim to be the same.
Expected behavior
ReJITID is set correctly in the ILToNativeMapDCStart event
Actual behavior
ReJITID is always zero
Regression?
I suspect it's been that way forever -- it's just that tiered compilation only recently became widely available.
Known Workarounds
None
Configuration
.NET 6, Windows, x64
Other information
No response
Activity
tommcdon commentedon Jun 3, 2022
@davmason @noahfalk
davmason commentedon Jun 3, 2022
Looking at the code
runtime/src/coreclr/vm/eventtrace.cpp
Lines 6891 to 6915 in 45589f2
We correctly set the ID for the non-rundown event but it's hardcoded to 0 for the rundown events. I haven't thought about it too much but at the surface level it seems like a one line type of fix.
pharring commentedon Jun 3, 2022
@davmason thanks for looking at this.
FWIW, the link to code led me to this commit: 3f7a996 which appears to be replacing ReJITID with NativeCodeID. I know almost nothing about the implementation and history here, so please take this with a grain of salt. I just mention it because it seemed relevant. If I've misunderstood the purpose of the ReJITID field, or if PerfView/TraceEvent needs to be modified to understand NativeCodeID, then please let me know. At the end of the day, I just want "Goto Source" to work reliably and accurately every time.
davmason commentedon Jun 3, 2022
@kouvel is there any compat reason to hard code the rundown ILToNativeMap events to 0? I can't think of any reason so this seems like an easy fix for 7
kouvel commentedon Jun 3, 2022
I don't know why it was hardcoded to 0, it seems to have been like that from the beginning in the history
jakobbotsch commentedon Jun 23, 2022
Another weird thing I've noticed is that we seem to have different definitions of "ReJITID" used in different ETW events
MethodDCStart(fired byETW::MethodLog::SendMethodEvent), the "ReJITID" we report is actually aNativeCodeVersionIdretrieved throughNativeCodeVersion::GetVersionId()ILToNativeMap(fired byETW::MethodLog::SendMethodILToNativeMapEvent) the "ReJITID" is an actual (typedeffed)ReJITIDthat we retrieve throughNativeCodeVersion::GetILCodeVersionId()for the non-rundown version, and which is 0 for the rundown version.Correct me if I'm wrong, but while both of these are called "ReJITID" in the events, they are actually two separate IDs and cannot be used for crossreferencing. One difference I believe is that of the two, only the former is incremented when compiling new tiered versions.
It seems like we should figure out how to reconcile these so that crossreferencing is easier/possible.
jakobbotsch commentedon Jun 23, 2022
Ah, looks like the commit by @kouvel linked above does seem to discuss this. I wonder what the reasoning was not to change
ILToNativeMapas well?noahfalk commentedon Jun 23, 2022
I can offer a little background on why the ids and naming is confusing. In the history of developing CLR the ICorProfiler ReJIT feature came first and tiered compilation came later. ICorProfiler ReJIT allows the profiler to change the IL which has the consequence of changing the jitted code as well and for a period of years that was the only way to do it (ignoring ENC). This meant that IL version == native version == rejitId. When tiered compilation entered the scene that invariant broke because we could change the native version of the code without changing the IL version. At this point we had the term rejitID already exposed in various places, in some cases refering to the concept of IL version and in others refering to the concept of native code version. When tools read our ETW events via TraceEvent, the name of the parameter is exposed to them in the public API (example) so we chose not rename it even though it may be getting used for different purposes.
My best understanding at the moment is that rejitId, when it appears in MethodDCStart, refers to native code version and when it appears in ILToNativeMap it refers to IL version (of course given that it is always zero may mean it is broken regardless). If we wanted to change its meaning in ILToNativeMap we should confirm that this doesn't cause problems for PerfView or VS profiler source mapping. Another alternative would be to add a new parameter to event named 'nativeCodeVersion'. Regardless of what else we do it would probably be helpful to add some comments clarifying what is going on. Hope that helps!
davmason commentedon Jul 7, 2022
@pharring this is fixed in main now, the sooner you can verify it fixes your scenario the better since we are getting close to shutting down for 7.0