UnsafeNativeMethods.PostMessage-Transition to unmanaged code

I'm trying to profile a section of the code that ANTS says %5 of CPU time.

It's reported as

UnsafeNativeMethods.PostMessage > Transition to unmanaged code

In the call graph, all the callers using a pattern like this:
if (SomethingHappenedHandler != null)
			{
				SomethingHappenedHandler(something);
			}

However mostly SomethingHappenedHandler is null.


Unfortunately profiler doesn't tell me what line is about "Transition to unmanaged code" so I'm guess this is the line as all 3 callers got something like this. Another code that might be related is this:
Interlocked.Increment(ref _DCount);

Most of these events later on handled in the GUI and then GUI uses InvokeRequired and BeginInvokes something.

How can I understand which code leads to "Transition to unmanaged code"? And what does it actually mean?

Another similar one exactly same but this time original method is "SetNativeMethods.GetWindowThreadProcessId" again it's linked to "Transition to unmanaged code" and I can't find which calls actually calls it as there is no line indicators.


Thanks,

Comments

  • OK, I figured that "Transition to unmanaged code" is referring to marshalling in UI thread due to invokes.

    Still it's not possible to spot this from the source code as profiler doesn't mark it up or show any time allocation on that line, so it's a bit of guess work.
  • Brian DonahueBrian Donahue Posts: 6,590 Bronze 1
    Hi,

    You're only going to see "transition to managed code" on the tree view as it's probably going to be difficult to wedge it into the source code view. However, the managed method name invoking the unmanaged code should be directly below the "transition" entry in the tree view. For instance:
    Microsoft.Samples.Debugging.CorDebug.CorDebuggerVersion.GetDebuggerVersionFromPid(int pid)
    -> Transition to unmanaged code...
         ->Microsoft.Samples.Debugging.CorDebug.NativeMethods.GetVersionFromProcess(ProcessSafeHandle hProcess, StringBuilder versionString, int bufferSize, out int dwLength)
    
    Where GetVersionFromProcess is a pInvoke defined as...
    [DllImport("mscoree.dll", CharSet=CharSet.Unicode, PreserveSig=true)]
            public static extern void GetVersionFromProcess(ProcessSafeHandle hProcess, StringBuilder versionString,
                                                            Int32 bufferSize, out Int32 dwLength);
    

    I think the "time with children" on the "transition" is not very useful - it seems to include the run-time of the unmanaged methods, too. The "time %" seems more sane (in my case <0.001).

    I'm told that if computing resources are low this may affect the reported time in transition. So you may want to turn down the detail level (for instance do not profile with line-level timings on).
Sign In or Register to comment.