Skip to content

Commit

Permalink
Cleanup some code in the sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 16, 2024
1 parent b387cee commit 4383eb3
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ internal unsafe class MacOSUltraSampler : UltraSampler
private UnsafeDictionary<ulong, int> _threadIdToCompressedFrameIndex = new(MaximumThreadCountForCompressedFrames);
private UnsafeHashSet<ulong> _activeThreadIds = new(MaximumThreadCountForCompressedFrames);
private UnsafeHashSet<ulong> _currentThreadIds = new(MaximumThreadCountForCompressedFrames);
private UnsafeList<ulong> _tempThreadIds = new(MaximumThreadCountForCompressedFrames);

// Modules
private const int DefaultImageCount = 1024;
Expand All @@ -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<ulong>(4096, true);
_allCompressedFrames = GC.AllocateArray<ulong>(MaximumCompressedFrameTotalCount * MaximumThreadCountForCompressedFrames, true);

Expand All @@ -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));
Expand Down Expand Up @@ -141,6 +140,7 @@ private unsafe void RunImpl()
}
else
{
ClearThreadStates();
_samplerResumeThreadEvent.WaitOne();
sendManifest = true;
}
Expand All @@ -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
Expand Down

0 comments on commit 4383eb3

Please sign in to comment.