Skip to content

Commit

Permalink
Fix module resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 18, 2024
1 parent c5983d3 commit 409245e
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/Ultra.Core/Parser/UltraEventPipeProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ private void SamplerParserOnEventNativeModule(UltraNativeModuleTraceEvent evt)
_nativeModuleAddressRanges[index] = new(evt.LoadAddress, evt.LoadAddress + evt.Size, index);
}

_sortedNativeModuleAddressRanges.Clear();
_sortedNativeModuleAddressRanges.AddRange(_nativeModuleAddressRanges);

// Always keep the list sorted because we resolve the address to the module while parsing the native callstacks
CollectionsMarshal.AsSpan(_nativeModuleAddressRanges).SortByRef(new AddressRangeComparer());
CollectionsMarshal.AsSpan(_sortedNativeModuleAddressRanges).SortByRef(new AddressRangeComparer());
}
}

Expand All @@ -141,7 +144,7 @@ private void PrintCallStack(UltraNativeCallstackTraceEvent callstackTraceEvent)
{
var frame = span[i];
var addressRangeIndex = FindAddressRange(sortedNativeModuleAddressRanges, frame);
if (addressRangeIndex != -1)
if (addressRangeIndex >= 0)
{
var addressRange = sortedNativeModuleAddressRanges[addressRangeIndex];
var module = _nativeModules[addressRange.Index];
Expand All @@ -154,15 +157,11 @@ private void PrintCallStack(UltraNativeCallstackTraceEvent callstackTraceEvent)
}
}

private static int FindAddressRange(Span<AddressRange> modules, ulong address)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int FindAddressRange(Span<AddressRange> ranges, ulong address)
{
if (modules.Length == 0) return -1;

var comparer = new ModuleAddressComparer(address);
int index = modules.BinarySearch(comparer);
if (index < 0) return -1;

return modules[index].Contains(address) ? index : -1;
return ranges.BinarySearch(comparer);
}

public void Run()
Expand All @@ -174,13 +173,13 @@ public void Run()
_samplerEventSource.Process();
}

private record struct AddressRange(ulong BeginAddress, ulong EndAddress, int Index)
private readonly record struct AddressRange(ulong BeginAddress, ulong EndAddress, int Index)
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Contains(ulong address) => address >= BeginAddress && address < EndAddress;
}

private record struct NativeModule(string ModulePath, Guid Uuid);
private readonly record struct NativeModule(string ModulePath, Guid Uuid);

private readonly record struct ModuleAddressComparer(ulong Address) : IComparable<AddressRange>
{
Expand Down

0 comments on commit 409245e

Please sign in to comment.