From 865d5eaad4ef49a83eced3e6d2069cdbca825e97 Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Sun, 15 Dec 2024 11:12:58 +0100 Subject: [PATCH] Fix ProviderGuid for UltraSamplerSource --- src/Ultra.Core/Parser/UltraSamplerParser.cs | 5 ++--- src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs | 6 +++--- src/Ultra.Sampler/UltraSamplerConstants.cs | 8 +++++-- src/Ultra.Sampler/UltraSamplerSource.cs | 22 ++++++++++---------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/Ultra.Core/Parser/UltraSamplerParser.cs b/src/Ultra.Core/Parser/UltraSamplerParser.cs index 23a2ec3..db8779a 100644 --- a/src/Ultra.Core/Parser/UltraSamplerParser.cs +++ b/src/Ultra.Core/Parser/UltraSamplerParser.cs @@ -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; diff --git a/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs b/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs index 3718f0a..f0eb8ed 100644 --- a/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs +++ b/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs @@ -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); @@ -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); } } } @@ -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 { diff --git a/src/Ultra.Sampler/UltraSamplerConstants.cs b/src/Ultra.Sampler/UltraSamplerConstants.cs index aa81109..7d5b4e7 100644 --- a/src/Ultra.Sampler/UltraSamplerConstants.cs +++ b/src/Ultra.Sampler/UltraSamplerConstants.cs @@ -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; } \ No newline at end of file diff --git a/src/Ultra.Sampler/UltraSamplerSource.cs b/src/Ultra.Sampler/UltraSamplerSource.cs index e02a18b..18d91b4 100644 --- a/src/Ultra.Sampler/UltraSamplerSource.cs +++ b/src/Ultra.Sampler/UltraSamplerSource.cs @@ -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(); @@ -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 = "")] - 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 = "")] - 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; @@ -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 {