Skip to content

Commit 9617857

Browse files
committed
Fix various issues with EventPipe
1 parent d31a701 commit 9617857

6 files changed

+39
-14
lines changed

src/Ultra.Core/Ultra.Core.csproj

+11
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,15 @@
4141
<ProjectReference Include="..\Ultra.ProcessHook\Ultra.ProcessHook.csproj" />
4242
<ProjectReference Include="..\Ultra.Sampler\Ultra.Sampler.csproj" />
4343
</ItemGroup>
44+
45+
<ItemGroup>
46+
<Content Include="..\Ultra.Sampler\libUltraSampler.dylib">
47+
<Link>libUltraSampler.dylib</Link>
48+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
49+
</Content>
50+
<Content Include="..\Ultra.Sampler\libUltraSamplerIndirect.dyld">
51+
<Link>libUltraSamplerIndirect.dyld</Link>
52+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
53+
</Content>
54+
</ItemGroup>
4455
</Project>

src/Ultra.Core/UltraProfiler.cs

+13-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Runtime.InteropServices;
88
using System.Text.Json;
99
using ByteSizeLib;
10-
using Microsoft.Diagnostics.Tracing.Session;
1110

1211
namespace Ultra.Core;
1312

@@ -87,14 +86,21 @@ public void Dispose()
8786
}
8887

8988
private protected abstract void DisposeImpl();
90-
89+
9190
/// <summary>
9291
/// Determines whether the current process is running with elevated privileges.
9392
/// </summary>
9493
/// <returns>True if the current process is running with elevated privileges; otherwise, false.</returns>
9594
public static bool IsElevated()
9695
{
97-
var isElevated = TraceEventSession.IsElevated();
96+
if (OperatingSystem.IsMacOS()) return true;
97+
98+
return IsElevatedWindows();
99+
}
100+
101+
private static bool IsElevatedWindows()
102+
{
103+
var isElevated = Microsoft.Diagnostics.Tracing.Session.TraceEventSession.IsElevated();
98104
return isElevated.HasValue && isElevated.Value;
99105
}
100106

@@ -155,7 +161,7 @@ public async Task<string> Run(UltraProfilerOptions ultraProfilerOptions)
155161
}
156162
else if (ultraProfilerOptions.ProgramPath != null)
157163
{
158-
if (!ultraProfilerOptions.ProgramPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
164+
if (OperatingSystem.IsWindows() && !ultraProfilerOptions.ProgramPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
159165
{
160166
throw new ArgumentException($"Executable path {ultraProfilerOptions.ProgramPath} must end with .exe");
161167
}
@@ -300,11 +306,11 @@ public async Task<string> Run(UltraProfilerOptions ultraProfilerOptions)
300306
}
301307

302308
await runner.OnFinalCleanup();
303-
309+
304310
return jsonFinalFile;
305311
}
306312

307-
313+
308314
private protected abstract ProfilerRunner CreateRunner(UltraProfilerOptions ultraProfilerOptions, List<Process> processList, string baseName, Process? singleProcess);
309315

310316
/// <summary>
@@ -373,7 +379,7 @@ private async Task EnableProfiling(ProfilerRunner runner, UltraProfilerOptions u
373379
// Reset the clock to account for the duration of the profiler
374380
ProfilerClock.Restart();
375381
}
376-
382+
377383
private protected async Task WaitForStaleFile(string file, UltraProfilerOptions options)
378384
{
379385
var clock = Stopwatch.StartNew();

src/Ultra.Core/UltraProfilerEventPipe.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace Ultra.Core;
2121
internal sealed class UltraProfilerEventPipe : UltraProfiler
2222
{
2323
private static string PathToNativeUltraSampler => Path.Combine(AppContext.BaseDirectory, "libUltraSamplerIndirect.dyld");
24-
24+
2525
/// <summary>
2626
/// Initializes a new instance of the <see cref="UltraProfiler"/> class.
2727
/// </summary>
@@ -117,6 +117,7 @@ private static void SetupUltraSampler(ProcessStartInfo startInfo)
117117
value = ultraSamplerPath;
118118
}
119119

120+
Console.WriteLine($"DYLD_INSERT_LIBRARIES={value}");
120121
startInfo.Environment[key] = value;
121122
}
122123

@@ -228,13 +229,13 @@ public long TotalFileLength()
228229

229230
return totalLength;
230231
}
231-
232+
232233
public async Task StartProfiling()
233234
{
234235
var ultraEventProvider = new EventPipeProvider(UltraSamplerParser.Name, EventLevel.Verbose);
235236
_ultraSession = await _ultraDiagnosticsClient.StartEventPipeSessionAsync([ultraEventProvider], true, 256, _token).ConfigureAwait(false);
236237
_ultraEventStreamCopyTask = _ultraSession.EventStream.CopyToAsync(_ultraNetTraceFileStream, _token);
237-
238+
238239
if (_mainDiagnosticsClient is not null)
239240
{
240241
var jitEvents = ClrTraceEventParser.Keywords.JITSymbols |
@@ -290,5 +291,5 @@ public void Dispose()
290291
}
291292
}
292293
}
293-
294+
294295
}

src/Ultra.Sampler/Ultra.Sampler.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@
2121
<PackageReference Include="XenoAtom.Collections" />
2222
</ItemGroup>
2323

24+
<ItemGroup>
25+
<None Remove="libUltraSampler.dylib" />
26+
27+
<None Remove="libUltraSamplerIndirect.dyld" />
28+
</ItemGroup>
29+
2430
</Project>

src/Ultra.Sampler/build_native.sh

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
dotnet publish -c Release -r osx-arm64
33
LIB_NAME=libUltraSampler.dylib
44
LIB_OUTPUT_PATH=./bin/Release/net8.0/osx-arm64/publish/$LIB_NAME
5-
install_name_tool -id $LIB_NAME $LIB_OUTPUT_PATH
5+
#install_name_tool -id @loader/$LIB_NAME $LIB_OUTPUT_PATH
6+
install_name_tool -id @loader_path/$LIB_NAME $LIB_OUTPUT_PATH
67
cp $LIB_OUTPUT_PATH ./$LIB_NAME
7-
clang -shared -O2 -o libUltraSamplerIndirect.dyld ultra_sampler_indirect.cpp ./$LIB_NAME
8+
clang -dynamiclib -O2 -o libUltraSamplerIndirect.dyld ultra_sampler_indirect.cpp -L . -l UltraSampler
9+
#install_name_tool -change $LIB_NAME @loader/$LIB_NAME libUltraSamplerIndirect.dyld

src/Ultra.Sampler/ultra_sampler_indirect.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
extern "C" {
1010
void ultra_sampler_start();
1111

12-
1312
void set_temporary_tmpdir(const char* sub_dir_name, void (*func)()) {
1413
// Get the current TMPDIR
1514
const char* original_tmpdir = getenv("TMPDIR");

0 commit comments

Comments
 (0)