Skip to content

Commit 4383eb3

Browse files
committed
Cleanup some code in the sampler
1 parent b387cee commit 4383eb3

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs

+18-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ internal unsafe class MacOSUltraSampler : UltraSampler
3232
private UnsafeDictionary<ulong, int> _threadIdToCompressedFrameIndex = new(MaximumThreadCountForCompressedFrames);
3333
private UnsafeHashSet<ulong> _activeThreadIds = new(MaximumThreadCountForCompressedFrames);
3434
private UnsafeHashSet<ulong> _currentThreadIds = new(MaximumThreadCountForCompressedFrames);
35-
private UnsafeList<ulong> _tempThreadIds = new(MaximumThreadCountForCompressedFrames);
3635

3736
// Modules
3837
private const int DefaultImageCount = 1024;
@@ -48,6 +47,9 @@ internal unsafe class MacOSUltraSampler : UltraSampler
4847

4948
public MacOSUltraSampler()
5049
{
50+
// Make sure to use the instance to trigger the constructor of the EventSource so that it is registered in the runtime!
51+
_samplerEventSource = UltraSamplerSource.Log;
52+
5153
_frames = GC.AllocateArray<ulong>(4096, true);
5254
_allCompressedFrames = GC.AllocateArray<ulong>(MaximumCompressedFrameTotalCount * MaximumThreadCountForCompressedFrames, true);
5355

@@ -61,10 +63,7 @@ public MacOSUltraSampler()
6163

6264
_callbackDyldAdded = new MacOSLibSystem.dyld_register_callback(CallbackDyldAdded);
6365
_callbackDyldRemoved = new MacOSLibSystem.dyld_register_callback(CallbackDyldRemoved);
64-
65-
// Make sure to use the instance to trigger the constructor of the EventSource so that it is registered in the runtime!
66-
_samplerEventSource = UltraSamplerSource.Log;
67-
66+
6867
// Register dyld callbacks
6968
_initializingModules = true;
7069
MacOSLibSystem._dyld_register_func_for_add_image(Marshal.GetFunctionPointerForDelegate(_callbackDyldAdded));
@@ -141,6 +140,7 @@ private unsafe void RunImpl()
141140
}
142141
else
143142
{
143+
ClearThreadStates();
144144
_samplerResumeThreadEvent.WaitOne();
145145
sendManifest = true;
146146
}
@@ -152,6 +152,19 @@ private unsafe void RunImpl()
152152
}
153153
}
154154

155+
156+
private void ClearThreadStates()
157+
{
158+
// Reset the state for threads
159+
_currentThreadIds.Clear();
160+
_activeThreadIds.Clear();
161+
foreach (var compressedIndex in _threadIdToCompressedFrameIndex.Values)
162+
{
163+
_freeCompressedFramesIndices.Add(compressedIndex);
164+
}
165+
_threadIdToCompressedFrameIndex.Clear();
166+
}
167+
155168
private static void SendManifest()
156169
{
157170
// Make sure to always send the manifest before resuming the capture thread

0 commit comments

Comments
 (0)