diff --git a/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs b/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs index 96dd4f9..89a6bfe 100644 --- a/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs +++ b/src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs @@ -32,7 +32,6 @@ internal unsafe class MacOSUltraSampler : UltraSampler private UnsafeDictionary _threadIdToCompressedFrameIndex = new(MaximumThreadCountForCompressedFrames); private UnsafeHashSet _activeThreadIds = new(MaximumThreadCountForCompressedFrames); private UnsafeHashSet _currentThreadIds = new(MaximumThreadCountForCompressedFrames); - private UnsafeList _tempThreadIds = new(MaximumThreadCountForCompressedFrames); // Modules private const int DefaultImageCount = 1024; @@ -48,6 +47,9 @@ internal unsafe class MacOSUltraSampler : UltraSampler public MacOSUltraSampler() { + // Make sure to use the instance to trigger the constructor of the EventSource so that it is registered in the runtime! + _samplerEventSource = UltraSamplerSource.Log; + _frames = GC.AllocateArray(4096, true); _allCompressedFrames = GC.AllocateArray(MaximumCompressedFrameTotalCount * MaximumThreadCountForCompressedFrames, true); @@ -61,10 +63,7 @@ public MacOSUltraSampler() _callbackDyldAdded = new MacOSLibSystem.dyld_register_callback(CallbackDyldAdded); _callbackDyldRemoved = new MacOSLibSystem.dyld_register_callback(CallbackDyldRemoved); - - // Make sure to use the instance to trigger the constructor of the EventSource so that it is registered in the runtime! - _samplerEventSource = UltraSamplerSource.Log; - + // Register dyld callbacks _initializingModules = true; MacOSLibSystem._dyld_register_func_for_add_image(Marshal.GetFunctionPointerForDelegate(_callbackDyldAdded)); @@ -141,6 +140,7 @@ private unsafe void RunImpl() } else { + ClearThreadStates(); _samplerResumeThreadEvent.WaitOne(); sendManifest = true; } @@ -152,6 +152,19 @@ private unsafe void RunImpl() } } + + private void ClearThreadStates() + { + // Reset the state for threads + _currentThreadIds.Clear(); + _activeThreadIds.Clear(); + foreach (var compressedIndex in _threadIdToCompressedFrameIndex.Values) + { + _freeCompressedFramesIndices.Add(compressedIndex); + } + _threadIdToCompressedFrameIndex.Clear(); + } + private static void SendManifest() { // Make sure to always send the manifest before resuming the capture thread