Skip to content

Commit

Permalink
Fix various issues with EventPipe
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 8, 2024
1 parent d31a701 commit 9617857
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/Ultra.Core/Ultra.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,15 @@
<ProjectReference Include="..\Ultra.ProcessHook\Ultra.ProcessHook.csproj" />
<ProjectReference Include="..\Ultra.Sampler\Ultra.Sampler.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="..\Ultra.Sampler\libUltraSampler.dylib">
<Link>libUltraSampler.dylib</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\Ultra.Sampler\libUltraSamplerIndirect.dyld">
<Link>libUltraSamplerIndirect.dyld</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
</Project>
20 changes: 13 additions & 7 deletions src/Ultra.Core/UltraProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Runtime.InteropServices;
using System.Text.Json;
using ByteSizeLib;
using Microsoft.Diagnostics.Tracing.Session;

namespace Ultra.Core;

Expand Down Expand Up @@ -87,14 +86,21 @@ public void Dispose()
}

private protected abstract void DisposeImpl();

/// <summary>
/// Determines whether the current process is running with elevated privileges.
/// </summary>
/// <returns>True if the current process is running with elevated privileges; otherwise, false.</returns>
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;
}

Expand Down Expand Up @@ -155,7 +161,7 @@ public async Task<string> 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");
}
Expand Down Expand Up @@ -300,11 +306,11 @@ public async Task<string> Run(UltraProfilerOptions ultraProfilerOptions)
}

await runner.OnFinalCleanup();

return jsonFinalFile;
}


private protected abstract ProfilerRunner CreateRunner(UltraProfilerOptions ultraProfilerOptions, List<Process> processList, string baseName, Process? singleProcess);

/// <summary>
Expand Down Expand Up @@ -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();
Expand Down
9 changes: 5 additions & 4 deletions src/Ultra.Core/UltraProfilerEventPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Ultra.Core;
internal sealed class UltraProfilerEventPipe : UltraProfiler
{
private static string PathToNativeUltraSampler => Path.Combine(AppContext.BaseDirectory, "libUltraSamplerIndirect.dyld");

/// <summary>
/// Initializes a new instance of the <see cref="UltraProfiler"/> class.
/// </summary>
Expand Down Expand Up @@ -117,6 +117,7 @@ private static void SetupUltraSampler(ProcessStartInfo startInfo)
value = ultraSamplerPath;
}

Console.WriteLine($"DYLD_INSERT_LIBRARIES={value}");
startInfo.Environment[key] = value;
}

Expand Down Expand Up @@ -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 |
Expand Down Expand Up @@ -290,5 +291,5 @@ public void Dispose()
}
}
}

}
6 changes: 6 additions & 0 deletions src/Ultra.Sampler/Ultra.Sampler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@
<PackageReference Include="XenoAtom.Collections" />
</ItemGroup>

<ItemGroup>
<None Remove="libUltraSampler.dylib" />

<None Remove="libUltraSamplerIndirect.dyld" />
</ItemGroup>

</Project>
6 changes: 4 additions & 2 deletions src/Ultra.Sampler/build_native.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion src/Ultra.Sampler/ultra_sampler_indirect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 9617857

Please sign in to comment.