Skip to content

Commit 154cdbe

Browse files
committed
Prepare sampler for TraceEvent parsing
1 parent facb393 commit 154cdbe

7 files changed

+59
-83
lines changed

src/Ultra.Core/DiagnosticPortSession.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public async Task StartProfiling(CancellationToken token)
112112
_nettraceFileStream = new FileStream(_nettraceFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 65536, FileOptions.Asynchronous);
113113

114114
long keywords = -1;
115-
var providerName = UltraSamplerParser.Name;
115+
var providerName = UltraSamplerConstants.Name;
116116
var level = EventLevel.Verbose;
117117

118118
if (!_sampler)

src/Ultra.Core/Ultra.Core.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<Compile Include="..\Ultra.Sampler\UltraSamplerParser.cs" Link="UltraSamplerParser.cs" />
30+
<Compile Include="..\Ultra.Sampler\UltraSamplerConstants.cs" Link="UltraSamplerConstants.cs" />
3131
</ItemGroup>
3232

3333
<ItemGroup>

src/Ultra.Sampler/MacOS/MacOSUltraSampler.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private void NotifyPendingNativeModuleEvents()
170170
for(; _nextModuleEventIndexToLog < events.Length; _nextModuleEventIndexToLog++)
171171
{
172172
var evt = events[_nextModuleEventIndexToLog];
173-
UltraSamplerSource.Log.OnNativeModuleEvent((int)evt.Kind, evt.LoadAddress, evt.Size, evt.Path, evt.TimestampUtc);
173+
UltraSamplerSource.Log.OnNativeModuleEvent((int)evt.Kind, evt.LoadAddress, evt.Size, evt.TimestampUtc, evt.Path?.Length ?? 0, evt.Path);
174174
}
175175
}
176176
}
@@ -293,7 +293,7 @@ private static unsafe void Sample(MacOS.MacOSLibSystem.mach_port_t rootTask, ulo
293293

294294
//Console.WriteLine($"sp: 0x{armThreadState.__sp:X8}, fp: 0x{armThreadState.__fp:X8}, lr: 0x{armThreadState.__lr:X8}");
295295
int frameCount = WalkNativeCallStack(armThreadState.__sp, armThreadState.__fp, armThreadState.__lr, pFrames);
296-
nativeCallstack(threadInfo.thread_id, (ulong)pFrames, frameCount);
296+
nativeCallstack(threadInfo.thread_id, frameCount, (ulong)pFrames);
297297
}
298298
finally
299299
{

src/Ultra.Sampler/MacOS/NativeCallstackDelegate.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
namespace Ultra.Sampler.MacOS;
66

7-
internal unsafe delegate void NativeCallstackDelegate(ulong threadId, ulong pFrames, int frameCount);
7+
internal unsafe delegate void NativeCallstackDelegate(ulong threadId, int frameCount, ulong pFrames);
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Alexandre Mutel. All rights reserved.
2+
// Licensed under the BSD-Clause 2 license.
3+
// See license.txt file in the project root for full license information.
4+
5+
namespace Ultra.Sampler;
6+
7+
internal static class UltraSamplerConstants
8+
{
9+
public const string Name = "Ultra-Sampler";
10+
11+
public const string IdAsString = "04E4DCBF-494F-4A77-B55E-F5C041A92F56";
12+
13+
public static readonly Guid Id = new(IdAsString);
14+
15+
public const int NativeCallStackEvent = 1;
16+
17+
public const int NativeModuleEvent = 2;
18+
19+
}

src/Ultra.Sampler/UltraSamplerParser.cs

-30
This file was deleted.

src/Ultra.Sampler/UltraSamplerSource.cs

+35-48
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
using System.Diagnostics.CodeAnalysis;
66
using System.Diagnostics.Tracing;
7+
using System.Runtime.CompilerServices;
78

89
namespace Ultra.Sampler;
910

10-
[EventSource(Name = UltraSamplerParser.Name, Guid = UltraSamplerParser.IdAsString)]
11+
[EventSource(Name = UltraSamplerConstants.Name, Guid = UltraSamplerConstants.IdAsString)]
1112
internal sealed class UltraSamplerSource : EventSource
1213
{
1314
public static readonly UltraSamplerSource Log = new();
@@ -16,39 +17,47 @@ private UltraSamplerSource()
1617
{
1718
}
1819

19-
[Event(UltraSamplerParser.NativeCallStackEvent, Level = EventLevel.Informational)]
20+
[Event(UltraSamplerConstants.NativeCallStackEvent, Level = EventLevel.Informational)]
2021
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
21-
public unsafe void OnNativeCallstack(ulong threadId, ulong pFrames, int count)
22+
public unsafe void OnNativeCallstack(ulong threadId, int frameCount, ulong 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!
2223
{
23-
EventData3 evt = default;
24-
evt.Data1.DataPointer = (nint)(void*)&threadId;
25-
evt.Data1.Size = sizeof(ulong);
26-
evt.Data2.DataPointer = (nint)pFrames;
27-
evt.Data2.Size = count * sizeof(ulong);
28-
evt.Data3.DataPointer = (int) &count;
29-
evt.Data3.Size = sizeof(int);
30-
WriteEventCore(UltraSamplerParser.NativeCallStackEvent, 3, &evt.Data1);
24+
var evt = stackalloc EventData[3];
25+
evt[0].DataPointer = (nint)(void*)&threadId;
26+
evt[0].Size = sizeof(ulong);
27+
evt[1].DataPointer = (int) &frameCount;
28+
evt[1].Size = sizeof(int);
29+
evt[2].DataPointer = (nint)frames;
30+
evt[2].Size = frameCount * sizeof(ulong);
31+
WriteEventCore(UltraSamplerConstants.NativeCallStackEvent, 3, evt);
3132
}
3233

33-
[Event(UltraSamplerParser.NativeModuleEvent, Level = EventLevel.Informational)]
34+
[Event(UltraSamplerConstants.NativeModuleEvent, Level = EventLevel.Informational)]
3435
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
35-
public unsafe void OnNativeModuleEvent(int nativeModuleEventKind, ulong loadAddress, ulong size, byte[]? modulePathUtf8, DateTime timestampUtc)
36+
[SkipLocalsInit]
37+
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
3638
{
37-
EventData5 evt = default;
38-
evt.Data1.DataPointer = (nint)(void*)&nativeModuleEventKind;
39-
evt.Data1.Size = sizeof(int);
40-
evt.Data2.DataPointer = (nint)(void*)&loadAddress;
41-
evt.Data2.Size = sizeof(ulong);
42-
evt.Data3.DataPointer = (nint)(void*)&size;
43-
evt.Data3.Size = sizeof(ulong);
39+
var evt = stackalloc EventData[6];
40+
evt[0].DataPointer = (nint)(void*)&nativeModuleEventKind;
41+
evt[0].Size = sizeof(int);
42+
evt[1].DataPointer = (nint)(void*)&loadAddress;
43+
evt[1].Size = sizeof(ulong);
44+
evt[2].DataPointer = (nint)(void*)&size;
45+
evt[2].Size = sizeof(ulong);
46+
var utcFileTime = timestampUtc.ToFileTimeUtc();
47+
evt[3].DataPointer = (nint)(void*)&utcFileTime;
48+
evt[3].Size = sizeof(long);
4449
fixed (byte* evtPathPtr = modulePathUtf8)
4550
{
46-
evt.Data4.DataPointer = (nint)evtPathPtr;
47-
evt.Data4.Size = modulePathUtf8?.Length ?? 0;
48-
var utcFileTime = timestampUtc.ToFileTimeUtc();
49-
evt.Data5.DataPointer = (nint)(void*)&utcFileTime;
50-
evt.Data5.Size = sizeof(long);
51-
WriteEventCore(UltraSamplerParser.NativeModuleEvent, 5, &evt.Data1);
51+
evt[4].DataPointer = (nint)(void*)&modulePathUtf8Length;
52+
evt[4].Size = sizeof(int);
53+
54+
if (modulePathUtf8Length > 0)
55+
{
56+
evt[5].DataPointer = (nint)evtPathPtr;
57+
evt[5].Size = modulePathUtf8Length;
58+
}
59+
60+
WriteEventCore(UltraSamplerConstants.NativeModuleEvent, modulePathUtf8Length > 0 ? 5 : 4, evt);
5261
}
5362
}
5463

@@ -67,26 +76,4 @@ protected override void OnEventCommand(EventCommandEventArgs command)
6776
Thread.Sleep(100);
6877
}
6978
}
70-
71-
private struct EventData3
72-
{
73-
public EventData Data1;
74-
75-
public EventData Data2;
76-
77-
public EventData Data3;
78-
}
79-
80-
private struct EventData5
81-
{
82-
public EventData Data1;
83-
84-
public EventData Data2;
85-
86-
public EventData Data3;
87-
88-
public EventData Data4;
89-
90-
public EventData Data5;
91-
}
9279
}

0 commit comments

Comments
 (0)