Skip to content

Commit

Permalink
Append IL stub code gen when it is available.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronRobinsonMSFT committed Sep 29, 2020
1 parent 1452970 commit d283b71
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
15 changes: 12 additions & 3 deletions DllImportGenerator/TestAssets/Benchmarking/CodeGenEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.IO;

using Iced.Intel;

using Decoder = Iced.Intel.Decoder;
Expand Down Expand Up @@ -63,6 +62,7 @@ private void OnILStubGenerated(EventWrittenEventArgs eventData)
ulong methodId = 0;
string fqClassName = string.Empty;
string methodName = string.Empty;
string ilCode = string.Empty;
uint metadataToken = 0;

// See https://docs.microsoft.com/dotnet/framework/performance/interop-etw-events
Expand All @@ -87,14 +87,19 @@ private void OnILStubGenerated(EventWrittenEventArgs eventData)
{
metadataToken = (uint)payload;
}
else if (payloadName == "StubMethodILCode")
{
ilCode = (string)payload;
}
}

Debug.Assert(methodId != 0);
pinvokeInstances.Add(methodId, new PInvokeInstance()
{
MetadataToken = metadataToken,
FullyQualifiedClassName = fqClassName,
MethodName = methodName
MethodName = methodName,
ILCode = ilCode,
});;
}

Expand All @@ -110,6 +115,7 @@ private void OnVerboseMethodLoad(EventWrittenEventArgs eventData)
string namespaceName = "?";
string methodName = "?";
string methodSignature = "?";
string ilCode = string.Empty;
uint flags = 0;
uint metadataToken = 0;

Expand Down Expand Up @@ -160,6 +166,7 @@ private void OnVerboseMethodLoad(EventWrittenEventArgs eventData)
metadataToken = pin.MetadataToken;
namespaceName = pin.FullyQualifiedClassName;
methodName = pin.MethodName;
ilCode = pin.ILCode;
}

string flagString = "";
Expand Down Expand Up @@ -241,7 +248,8 @@ private void OnVerboseMethodLoad(EventWrittenEventArgs eventData)
FullyQualifiedClassName = namespaceName,
MethodName = methodName,
GeneratedCode = generatedCode.ToString(),
CodeSize = size
GeneratedCodeSize = size,
ILCode = ilCode,
});
}

Expand All @@ -250,6 +258,7 @@ private record PInvokeInstance
public uint MetadataToken { get; init; }
public string FullyQualifiedClassName { get; init; }
public string MethodName { get; init; }
public string ILCode { get; init; }
}

private unsafe class MemoryCodeReader : CodeReader
Expand Down
8 changes: 5 additions & 3 deletions DllImportGenerator/TestAssets/Benchmarking/CodeGenMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public record MethodCodeGen
public string FullyQualifiedClassName { get; init; }
public string MethodName { get; init; }
public string GeneratedCode { get; init; }
public uint CodeSize { get; init; }
public uint GeneratedCodeSize { get; init; }
public string ILCode { get; init; }
}

/// <summary>
Expand Down Expand Up @@ -136,14 +137,15 @@ public string GenerateHtml(string pattern = "", int waitForMonitorInMs = 2_000)

if (mcgs.Value.Count == 1)
{
yield return new(mcgs.Key, mcgs.Value.Last().GeneratedCode);
var mcg = mcgs.Value.Last();
yield return new(mcgs.Key, mcg.GeneratedCode + Environment.NewLine + mcg.ILCode);
}
else
{
int i = 1;
foreach (var mcg in mcgs.Value)
{
yield return new(mcgs.Key + $" ({i})", mcg.GeneratedCode);
yield return new(mcgs.Key + $" ({i})", mcg.GeneratedCode + Environment.NewLine + mcg.ILCode);
++i;
}
}
Expand Down

0 comments on commit d283b71

Please sign in to comment.