Skip to content

Commit

Permalink
Add async to filestream
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 8, 2024
1 parent 9617857 commit 9b6898d
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/Ultra.Core/UltraProfilerEventPipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ private UltraSamplerProfilerState(UltraProfilerOptions options, DiagnosticsClien
_token = token;
_ultraDiagnosticsClient = ultraDiagnosticsClient;
_ultraNetTraceFilePath = Path.Combine(Environment.CurrentDirectory, $"{baseName}_sampler_{pid}.nettrace");
_ultraNetTraceFileStream = new FileStream(_ultraNetTraceFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
_ultraNetTraceFileStream = new FileStream(_ultraNetTraceFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 65536, FileOptions.Asynchronous);
if (mainDiagnosticsClient != null)
{
_mainDiagnosticsClient = mainDiagnosticsClient;
_mainNetTraceFilePath = Path.Combine(Environment.CurrentDirectory, $"{baseName}_main_{pid}.nettrace");
_mainNetTraceFileStream = new FileStream(_mainNetTraceFilePath, FileMode.Create, FileAccess.Write, FileShare.Read);
_mainNetTraceFileStream = new FileStream(_mainNetTraceFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, 65536, FileOptions.Asynchronous);
}
}

Expand Down Expand Up @@ -191,18 +191,13 @@ public static async Task<UltraSamplerProfilerState> Connect(string baseName, int
var diagnosticClientMain = new DiagnosticsClient(pid);
var diagnosticClientUltra = await DiagnosticsClientConnector.FromDiagnosticPort(ultraDiagnosticPortSocket, token);

if (diagnosticClientUltra is null)
{

}

var timeoutSource = new CancellationTokenSource();
var linkedCancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(token, timeoutSource.Token);

try
{
timeoutSource.CancelAfter(1000);
await diagnosticClientUltra.Instance.WaitForConnectionAsync(linkedCancellationTokenSource.Token).ConfigureAwait(false);
await diagnosticClientUltra!.Instance.WaitForConnectionAsync(linkedCancellationTokenSource.Token).ConfigureAwait(false);
await diagnosticClientMain.WaitForConnectionAsync(linkedCancellationTokenSource.Token).ConfigureAwait(false);
}
catch (OperationCanceledException) when (timeoutSource.IsCancellationRequested)
Expand Down Expand Up @@ -259,23 +254,24 @@ public async Task StartProfiling()

public async Task Stop()
{
if (_ultraSession is not null)
if (_ultraEventStreamCopyTask is not null)
{
await _ultraSession.StopAsync(_token).ConfigureAwait(false);
await _ultraEventStreamCopyTask.ConfigureAwait(false);
}
if (_mainSession is not null)

if (_mainEventStreamCopyTask is not null)
{
await _mainSession.StopAsync(_token).ConfigureAwait(false);
await _mainEventStreamCopyTask.ConfigureAwait(false);
}

if (_ultraEventStreamCopyTask is not null)
if (_ultraSession is not null)
{
await _ultraEventStreamCopyTask.ConfigureAwait(false);
await _ultraSession.StopAsync(_token).ConfigureAwait(false);
}

if (_mainEventStreamCopyTask is not null)
if (_mainSession is not null)
{
await _mainEventStreamCopyTask.ConfigureAwait(false);
await _mainSession.StopAsync(_token).ConfigureAwait(false);
}
}

Expand Down

0 comments on commit 9b6898d

Please sign in to comment.