From 9617857f1cfb2d4b65e66ba3a35db99659cf975a Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Sun, 8 Dec 2024 16:41:30 +0100 Subject: [PATCH] Fix various issues with EventPipe --- src/Ultra.Core/Ultra.Core.csproj | 11 +++++++++++ src/Ultra.Core/UltraProfiler.cs | 20 +++++++++++++------- src/Ultra.Core/UltraProfilerEventPipe.cs | 9 +++++---- src/Ultra.Sampler/Ultra.Sampler.csproj | 6 ++++++ src/Ultra.Sampler/build_native.sh | 6 ++++-- src/Ultra.Sampler/ultra_sampler_indirect.cpp | 1 - 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/Ultra.Core/Ultra.Core.csproj b/src/Ultra.Core/Ultra.Core.csproj index 74b54fa..5dc3532 100644 --- a/src/Ultra.Core/Ultra.Core.csproj +++ b/src/Ultra.Core/Ultra.Core.csproj @@ -41,4 +41,15 @@ + + + + libUltraSampler.dylib + PreserveNewest + + + libUltraSamplerIndirect.dyld + PreserveNewest + + diff --git a/src/Ultra.Core/UltraProfiler.cs b/src/Ultra.Core/UltraProfiler.cs index 8618f02..a6d4d1c 100644 --- a/src/Ultra.Core/UltraProfiler.cs +++ b/src/Ultra.Core/UltraProfiler.cs @@ -7,7 +7,6 @@ using System.Runtime.InteropServices; using System.Text.Json; using ByteSizeLib; -using Microsoft.Diagnostics.Tracing.Session; namespace Ultra.Core; @@ -87,14 +86,21 @@ public void Dispose() } private protected abstract void DisposeImpl(); - + /// /// Determines whether the current process is running with elevated privileges. /// /// True if the current process is running with elevated privileges; otherwise, false. public static bool IsElevated() { - var isElevated = TraceEventSession.IsElevated(); + if (OperatingSystem.IsMacOS()) return true; + + return IsElevatedWindows(); + } + + private static bool IsElevatedWindows() + { + var isElevated = Microsoft.Diagnostics.Tracing.Session.TraceEventSession.IsElevated(); return isElevated.HasValue && isElevated.Value; } @@ -155,7 +161,7 @@ public async Task Run(UltraProfilerOptions ultraProfilerOptions) } else if (ultraProfilerOptions.ProgramPath != null) { - if (!ultraProfilerOptions.ProgramPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) + if (OperatingSystem.IsWindows() && !ultraProfilerOptions.ProgramPath.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException($"Executable path {ultraProfilerOptions.ProgramPath} must end with .exe"); } @@ -300,11 +306,11 @@ public async Task Run(UltraProfilerOptions ultraProfilerOptions) } await runner.OnFinalCleanup(); - + return jsonFinalFile; } - + private protected abstract ProfilerRunner CreateRunner(UltraProfilerOptions ultraProfilerOptions, List processList, string baseName, Process? singleProcess); /// @@ -373,7 +379,7 @@ private async Task EnableProfiling(ProfilerRunner runner, UltraProfilerOptions u // Reset the clock to account for the duration of the profiler ProfilerClock.Restart(); } - + private protected async Task WaitForStaleFile(string file, UltraProfilerOptions options) { var clock = Stopwatch.StartNew(); diff --git a/src/Ultra.Core/UltraProfilerEventPipe.cs b/src/Ultra.Core/UltraProfilerEventPipe.cs index 7c8f2bc..d603015 100644 --- a/src/Ultra.Core/UltraProfilerEventPipe.cs +++ b/src/Ultra.Core/UltraProfilerEventPipe.cs @@ -21,7 +21,7 @@ namespace Ultra.Core; internal sealed class UltraProfilerEventPipe : UltraProfiler { private static string PathToNativeUltraSampler => Path.Combine(AppContext.BaseDirectory, "libUltraSamplerIndirect.dyld"); - + /// /// Initializes a new instance of the class. /// @@ -117,6 +117,7 @@ private static void SetupUltraSampler(ProcessStartInfo startInfo) value = ultraSamplerPath; } + Console.WriteLine($"DYLD_INSERT_LIBRARIES={value}"); startInfo.Environment[key] = value; } @@ -228,13 +229,13 @@ public long TotalFileLength() return totalLength; } - + public async Task StartProfiling() { var ultraEventProvider = new EventPipeProvider(UltraSamplerParser.Name, EventLevel.Verbose); _ultraSession = await _ultraDiagnosticsClient.StartEventPipeSessionAsync([ultraEventProvider], true, 256, _token).ConfigureAwait(false); _ultraEventStreamCopyTask = _ultraSession.EventStream.CopyToAsync(_ultraNetTraceFileStream, _token); - + if (_mainDiagnosticsClient is not null) { var jitEvents = ClrTraceEventParser.Keywords.JITSymbols | @@ -290,5 +291,5 @@ public void Dispose() } } } - + } \ No newline at end of file diff --git a/src/Ultra.Sampler/Ultra.Sampler.csproj b/src/Ultra.Sampler/Ultra.Sampler.csproj index 18a3280..e1a4509 100644 --- a/src/Ultra.Sampler/Ultra.Sampler.csproj +++ b/src/Ultra.Sampler/Ultra.Sampler.csproj @@ -21,4 +21,10 @@ + + + + + + diff --git a/src/Ultra.Sampler/build_native.sh b/src/Ultra.Sampler/build_native.sh index ccde0be..b79bf2b 100755 --- a/src/Ultra.Sampler/build_native.sh +++ b/src/Ultra.Sampler/build_native.sh @@ -2,6 +2,8 @@ dotnet publish -c Release -r osx-arm64 LIB_NAME=libUltraSampler.dylib LIB_OUTPUT_PATH=./bin/Release/net8.0/osx-arm64/publish/$LIB_NAME -install_name_tool -id $LIB_NAME $LIB_OUTPUT_PATH +#install_name_tool -id @loader/$LIB_NAME $LIB_OUTPUT_PATH +install_name_tool -id @loader_path/$LIB_NAME $LIB_OUTPUT_PATH cp $LIB_OUTPUT_PATH ./$LIB_NAME -clang -shared -O2 -o libUltraSamplerIndirect.dyld ultra_sampler_indirect.cpp ./$LIB_NAME +clang -dynamiclib -O2 -o libUltraSamplerIndirect.dyld ultra_sampler_indirect.cpp -L . -l UltraSampler +#install_name_tool -change $LIB_NAME @loader/$LIB_NAME libUltraSamplerIndirect.dyld diff --git a/src/Ultra.Sampler/ultra_sampler_indirect.cpp b/src/Ultra.Sampler/ultra_sampler_indirect.cpp index 8f12523..8c6905d 100644 --- a/src/Ultra.Sampler/ultra_sampler_indirect.cpp +++ b/src/Ultra.Sampler/ultra_sampler_indirect.cpp @@ -9,7 +9,6 @@ extern "C" { void ultra_sampler_start(); - void set_temporary_tmpdir(const char* sub_dir_name, void (*func)()) { // Get the current TMPDIR const char* original_tmpdir = getenv("TMPDIR");