Skip to content

ReJITID is always zero in ILToNativeMapDCStart event #70187

Closed

Description

@pharring

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.

  1. Capture a trace with PerfView of a reasonably complex .NET 6 application (something that exercises tiered compilation)
  2. 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

  1. ghost added
    untriaged
    on Jun 2, 2022
  2. tommcdon commented on Jun 3, 2022

    @tommcdon
    Member
  3. davmason commented on Jun 3, 2022

    @davmason
    Contributor

    Looking at the code

    if ((dwEventOptions & ETW::EnumerationLog::EnumerationStructs::JitMethodILToNativeMap) != 0)
    {
    FireEtwMethodILToNativeMap(
    ullMethodIdentifier,
    ilCodeId,
    0, // Extent: This event is only sent for JITted (not NGENd) methods, and
    // currently there is only one extent (hot) for JITted methods.
    cMap,
    rguiILOffset,
    rguiNativeOffset,
    GetClrInstanceId());
    }
    // Rundown provider
    //
    // These macros already check for the JittedMethodILToNativeMapRundownKeyword
    // before choosing to fire the event--we further check our options to see if we
    // should fire the Start and / or End flavor of the event (since the keyword alone
    // is insufficient to distinguish these).
    //
    // (for an explanation of the parameters see the FireEtwMethodILToNativeMap call above)
    if ((dwEventOptions & ETW::EnumerationLog::EnumerationStructs::MethodDCStartILToNativeMap) != 0)
    FireEtwMethodDCStartILToNativeMap(ullMethodIdentifier, 0, 0, cMap, rguiILOffset, rguiNativeOffset, GetClrInstanceId());
    if ((dwEventOptions & ETW::EnumerationLog::EnumerationStructs::MethodDCEndILToNativeMap) != 0)
    FireEtwMethodDCEndILToNativeMap(ullMethodIdentifier, 0, 0, cMap, rguiILOffset, rguiNativeOffset, GetClrInstanceId());

    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.

  4. pharring commented on Jun 3, 2022

    @pharring
    ContributorAuthor

    @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.

  5. davmason commented on Jun 3, 2022

    @davmason
    Contributor

    @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

  6. kouvel commented on Jun 3, 2022

    @kouvel
    Contributor

    I don't know why it was hardcoded to 0, it seems to have been like that from the beginning in the history

  7. added this to the 7.0.0 milestone on Jun 6, 2022
  8. ghost removed
    untriaged
    on Jun 6, 2022
  9. jakobbotsch commented on Jun 23, 2022

    @jakobbotsch
    Member

    Another weird thing I've noticed is that we seem to have different definitions of "ReJITID" used in different ETW events

    • In MethodDCStart (fired by ETW::MethodLog::SendMethodEvent), the "ReJITID" we report is actually a NativeCodeVersionId retrieved through NativeCodeVersion::GetVersionId()
    • In ILToNativeMap (fired by ETW::MethodLog::SendMethodILToNativeMapEvent) the "ReJITID" is an actual (typedeffed) ReJITID that we retrieve through NativeCodeVersion::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.

  10. jakobbotsch commented on Jun 23, 2022

    @jakobbotsch
    Member

    Ah, looks like the commit by @kouvel linked above does seem to discuss this. I wonder what the reasoning was not to change ILToNativeMap as well?

  11. noahfalk commented on Jun 23, 2022

    @noahfalk
    Member

    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!

  12. ghost added
    in-pr
    on Jun 30, 2022
  13. ghost removed
    in-pr
    on Jul 7, 2022
  14. davmason commented on Jul 7, 2022

    @davmason
    Contributor

    @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

  15. ghost locked as resolved and limited conversation to collaborators on Aug 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions