Skip to content

Commit

Permalink
Fix ProviderGuid for UltraSamplerSource
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 15, 2024
1 parent f2d50cc commit 865d5ea
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
5 changes: 2 additions & 3 deletions src/Ultra.Core/Parser/UltraSamplerParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ namespace Ultra.Core;

public sealed class UltraSamplerParser : TraceEventParser
{
public static Guid ProviderGuid => UltraSamplerConstants.ProviderGuid;
public static readonly Guid ProviderGuid = UltraSamplerConstants.ProviderGuid;


public static string ProviderName => UltraSamplerConstants.ProviderName;
public const string ProviderName = UltraSamplerConstants.ProviderName;

private static volatile TraceEvent[]? _templates;

Expand Down
6 changes: 3 additions & 3 deletions src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private unsafe void RunImpl()
NotifyPendingNativeModuleEvents();

// Sample the callstacks
Sample(rootTask, currentThreadId, _frames, UltraSamplerSource.Log.OnNativeCallstack);
Sample(rootTask, currentThreadId, _frames, UltraSamplerSource.Log.NativeCallstack);

// Sleep for 1ms
Thread.Sleep(1);
Expand Down 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.TimestampUtc, evt.Path?.Length ?? 0, evt.Path);
UltraSamplerSource.Log.NativeModuleEvent((int)evt.Kind, evt.LoadAddress, evt.Size, evt.TimestampUtc, 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, frameCount, (byte*)pFrames);
nativeCallstack(threadInfo.thread_id, frameCount * sizeof(ulong), (byte*)pFrames);
}
finally
{
Expand Down
8 changes: 6 additions & 2 deletions src/Ultra.Sampler/UltraSamplerConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ public static class UltraSamplerConstants
{
public const string ProviderName = "Ultra-Sampler";

public const string IdAsString = "{04E4DCBF-494F-4A77-B55E-F5C041A92F56}";
public const string ProviderGuidString = "d0b3f179-3b34-50ad-4c32-dd48a98dc5bf"; // This Guid is generated by the EventSource - Got it via EventSource.GetGuid(typeof(UltraSamplerSource))

public static readonly Guid ProviderGuid = new(IdAsString);
public static readonly Guid ProviderGuid = new(ProviderGuidString);

public const int NativeCallStackEventId = 1;

public const int NativeModuleEventId = 2;

public const int TaskNativeCallStackEventId = 1;

public const int TaskNativeModuleEventId = 2;
}
22 changes: 11 additions & 11 deletions src/Ultra.Sampler/UltraSamplerSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

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

namespace Ultra.Sampler;

[EventSource(Name = UltraSamplerConstants.ProviderName, Guid = UltraSamplerConstants.IdAsString)]
[EventSource(Name = UltraSamplerConstants.ProviderName)] // Cannot set the ProviderGuid that is not used https://github.com/dotnet/diagnostics/issues/389
internal sealed class UltraSamplerSource : EventSource
{
public static readonly UltraSamplerSource Log = new();
Expand All @@ -18,23 +17,23 @@ private UltraSamplerSource()
{
}

[Event(UltraSamplerConstants.NativeCallStackEventId, Level = EventLevel.Informational)]
[Event(UltraSamplerConstants.NativeCallStackEventId, Level = EventLevel.Informational, Task = (EventTask)UltraSamplerConstants.TaskNativeCallStackEventId)]
[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, int frameCount, byte* 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!
public unsafe void NativeCallstack(ulong threadId, int framesSize, byte* 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!
{
var evt = stackalloc EventData[3];
evt[0].DataPointer = (nint)(void*)&threadId;
evt[0].Size = sizeof(ulong);
evt[1].DataPointer = (nint)(void*)&frameCount;
evt[1].DataPointer = (nint)(void*)&framesSize;
evt[1].Size = sizeof(int);
evt[2].DataPointer = (nint)(void*)&frames;
evt[2].Size = frameCount * sizeof(ulong);
evt[2].Size = framesSize;
WriteEventCore(UltraSamplerConstants.NativeCallStackEventId, 3, evt);
}

[Event(UltraSamplerConstants.NativeModuleEventId, Level = EventLevel.Informational)]
[Event(UltraSamplerConstants.NativeModuleEventId, Level = EventLevel.Informational, Task = (EventTask)UltraSamplerConstants.TaskNativeModuleEventId)]
[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, DateTime timestampUtc, int modulePathUtf8Length, byte[]? modulePathUtf8) // byte[] is last to allow perfview to visualize previous fixed size arguments
public unsafe void NativeModuleEvent(int nativeModuleEventKind, ulong loadAddress, ulong size, DateTime timestampUtc, byte[]? modulePathUtf8) // byte[] is last to allow perfview to visualize previous fixed size arguments
{
var evt = stackalloc EventData[6];
evt[0].DataPointer = (nint)(void*)&nativeModuleEventKind;
Expand All @@ -48,13 +47,14 @@ private UltraSamplerSource()
evt[3].Size = sizeof(long);
fixed (byte* evtPathPtr = modulePathUtf8)
{
evt[4].DataPointer = (nint)(void*)&modulePathUtf8Length;
var modulePathUtf8Size = modulePathUtf8?.Length ?? 0;
evt[4].DataPointer = (nint)(void*)&modulePathUtf8Size;
evt[4].Size = sizeof(int);

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

0 comments on commit 865d5ea

Please sign in to comment.