Skip to content

Commit

Permalink
Prepare sampler for TraceEvent parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 14, 2024
1 parent facb393 commit 154cdbe
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 83 deletions.
2 changes: 1 addition & 1 deletion src/Ultra.Core/DiagnosticPortSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public async Task StartProfiling(CancellationToken token)
_nettraceFileStream = new FileStream(_nettraceFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 65536, FileOptions.Asynchronous);

long keywords = -1;
var providerName = UltraSamplerParser.Name;
var providerName = UltraSamplerConstants.Name;
var level = EventLevel.Verbose;

if (!_sampler)
Expand Down
2 changes: 1 addition & 1 deletion src/Ultra.Core/Ultra.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\Ultra.Sampler\UltraSamplerParser.cs" Link="UltraSamplerParser.cs" />
<Compile Include="..\Ultra.Sampler\UltraSamplerConstants.cs" Link="UltraSamplerConstants.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private void NotifyPendingNativeModuleEvents()
for(; _nextModuleEventIndexToLog < events.Length; _nextModuleEventIndexToLog++)
{
var evt = events[_nextModuleEventIndexToLog];
UltraSamplerSource.Log.OnNativeModuleEvent((int)evt.Kind, evt.LoadAddress, evt.Size, evt.Path, evt.TimestampUtc);
UltraSamplerSource.Log.OnNativeModuleEvent((int)evt.Kind, evt.LoadAddress, evt.Size, evt.TimestampUtc, evt.Path?.Length ?? 0, evt.Path);
}
}
}
Expand Down Expand Up @@ -293,7 +293,7 @@ private static unsafe void Sample(MacOS.MacOSLibSystem.mach_port_t rootTask, ulo

//Console.WriteLine($"sp: 0x{armThreadState.__sp:X8}, fp: 0x{armThreadState.__fp:X8}, lr: 0x{armThreadState.__lr:X8}");
int frameCount = WalkNativeCallStack(armThreadState.__sp, armThreadState.__fp, armThreadState.__lr, pFrames);
nativeCallstack(threadInfo.thread_id, (ulong)pFrames, frameCount);
nativeCallstack(threadInfo.thread_id, frameCount, (ulong)pFrames);
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion src/Ultra.Sampler/MacOS/NativeCallstackDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

namespace Ultra.Sampler.MacOS;

internal unsafe delegate void NativeCallstackDelegate(ulong threadId, ulong pFrames, int frameCount);
internal unsafe delegate void NativeCallstackDelegate(ulong threadId, int frameCount, ulong pFrames);
19 changes: 19 additions & 0 deletions src/Ultra.Sampler/UltraSamplerConstants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Alexandre Mutel. All rights reserved.
// Licensed under the BSD-Clause 2 license.
// See license.txt file in the project root for full license information.

namespace Ultra.Sampler;

internal static class UltraSamplerConstants
{
public const string Name = "Ultra-Sampler";

public const string IdAsString = "04E4DCBF-494F-4A77-B55E-F5C041A92F56";

public static readonly Guid Id = new(IdAsString);

public const int NativeCallStackEvent = 1;

public const int NativeModuleEvent = 2;

}
30 changes: 0 additions & 30 deletions src/Ultra.Sampler/UltraSamplerParser.cs

This file was deleted.

83 changes: 35 additions & 48 deletions src/Ultra.Sampler/UltraSamplerSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;

namespace Ultra.Sampler;

[EventSource(Name = UltraSamplerParser.Name, Guid = UltraSamplerParser.IdAsString)]
[EventSource(Name = UltraSamplerConstants.Name, Guid = UltraSamplerConstants.IdAsString)]
internal sealed class UltraSamplerSource : EventSource
{
public static readonly UltraSamplerSource Log = new();
Expand All @@ -16,39 +17,47 @@ private UltraSamplerSource()
{
}

[Event(UltraSamplerParser.NativeCallStackEvent, Level = EventLevel.Informational)]
[Event(UltraSamplerConstants.NativeCallStackEvent, Level = EventLevel.Informational)]
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public unsafe void OnNativeCallstack(ulong threadId, ulong pFrames, int count)
public unsafe void OnNativeCallstack(ulong threadId, int frameCount, ulong frames) // frames is last to allow perfview to visualize previous fixed size arguments and also, it is an ulong otherwise the EventSource will silently fail to register!
{
EventData3 evt = default;
evt.Data1.DataPointer = (nint)(void*)&threadId;
evt.Data1.Size = sizeof(ulong);
evt.Data2.DataPointer = (nint)pFrames;
evt.Data2.Size = count * sizeof(ulong);
evt.Data3.DataPointer = (int) &count;
evt.Data3.Size = sizeof(int);
WriteEventCore(UltraSamplerParser.NativeCallStackEvent, 3, &evt.Data1);
var evt = stackalloc EventData[3];
evt[0].DataPointer = (nint)(void*)&threadId;
evt[0].Size = sizeof(ulong);
evt[1].DataPointer = (int) &frameCount;
evt[1].Size = sizeof(int);
evt[2].DataPointer = (nint)frames;
evt[2].Size = frameCount * sizeof(ulong);
WriteEventCore(UltraSamplerConstants.NativeCallStackEvent, 3, evt);
}

[Event(UltraSamplerParser.NativeModuleEvent, Level = EventLevel.Informational)]
[Event(UltraSamplerConstants.NativeModuleEvent, Level = EventLevel.Informational)]
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public unsafe void OnNativeModuleEvent(int nativeModuleEventKind, ulong loadAddress, ulong size, byte[]? modulePathUtf8, DateTime timestampUtc)
[SkipLocalsInit]
public unsafe void OnNativeModuleEvent(int nativeModuleEventKind, ulong loadAddress, ulong size, DateTime timestampUtc, int modulePathUtf8Length, byte[]? modulePathUtf8) // byte[] is last to allow perfview to visualize previous fixed size arguments
{
EventData5 evt = default;
evt.Data1.DataPointer = (nint)(void*)&nativeModuleEventKind;
evt.Data1.Size = sizeof(int);
evt.Data2.DataPointer = (nint)(void*)&loadAddress;
evt.Data2.Size = sizeof(ulong);
evt.Data3.DataPointer = (nint)(void*)&size;
evt.Data3.Size = sizeof(ulong);
var evt = stackalloc EventData[6];
evt[0].DataPointer = (nint)(void*)&nativeModuleEventKind;
evt[0].Size = sizeof(int);
evt[1].DataPointer = (nint)(void*)&loadAddress;
evt[1].Size = sizeof(ulong);
evt[2].DataPointer = (nint)(void*)&size;
evt[2].Size = sizeof(ulong);
var utcFileTime = timestampUtc.ToFileTimeUtc();
evt[3].DataPointer = (nint)(void*)&utcFileTime;
evt[3].Size = sizeof(long);
fixed (byte* evtPathPtr = modulePathUtf8)
{
evt.Data4.DataPointer = (nint)evtPathPtr;
evt.Data4.Size = modulePathUtf8?.Length ?? 0;
var utcFileTime = timestampUtc.ToFileTimeUtc();
evt.Data5.DataPointer = (nint)(void*)&utcFileTime;
evt.Data5.Size = sizeof(long);
WriteEventCore(UltraSamplerParser.NativeModuleEvent, 5, &evt.Data1);
evt[4].DataPointer = (nint)(void*)&modulePathUtf8Length;
evt[4].Size = sizeof(int);

if (modulePathUtf8Length > 0)
{
evt[5].DataPointer = (nint)evtPathPtr;
evt[5].Size = modulePathUtf8Length;
}

WriteEventCore(UltraSamplerConstants.NativeModuleEvent, modulePathUtf8Length > 0 ? 5 : 4, evt);
}
}

Expand All @@ -67,26 +76,4 @@ protected override void OnEventCommand(EventCommandEventArgs command)
Thread.Sleep(100);
}
}

private struct EventData3
{
public EventData Data1;

public EventData Data2;

public EventData Data3;
}

private struct EventData5
{
public EventData Data1;

public EventData Data2;

public EventData Data3;

public EventData Data4;

public EventData Data5;
}
}

0 comments on commit 154cdbe

Please sign in to comment.