Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runtime resume option to dotnet-trace and dotnet-counters. #2343

Merged
merged 3 commits into from
Jun 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Tools/dotnet-counters/CounterMonitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private void StopMonitor()
_renderer.Stop();
}

public async Task<int> Monitor(CancellationToken ct, List<string> counter_list, string counters, IConsole console, int processId, int refreshInterval, string name, string diagnosticPort)
public async Task<int> Monitor(CancellationToken ct, List<string> counter_list, string counters, IConsole console, int processId, int refreshInterval, string name, string diagnosticPort, bool resumeRuntime)
{
if (!ProcessLauncher.Launcher.HasChildProc && !CommandUtils.ValidateArgumentsForAttach(processId, name, diagnosticPort, out _processId))
{
Expand All @@ -115,7 +115,7 @@ public async Task<int> Monitor(CancellationToken ct, List<string> counter_list,
_interval = refreshInterval;
_renderer = new ConsoleWriter();
_diagnosticsClient = holder.Client;
shouldResumeRuntime = ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort);
shouldResumeRuntime = ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort) || resumeRuntime;
int ret = await Start();
ProcessLauncher.Launcher.Cleanup();
return ret;
Expand All @@ -135,7 +135,7 @@ public async Task<int> Monitor(CancellationToken ct, List<string> counter_list,
}


public async Task<int> Collect(CancellationToken ct, List<string> counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string name, string diagnosticPort)
public async Task<int> Collect(CancellationToken ct, List<string> counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string name, string diagnosticPort, bool resumeRuntime)
{
if (!ProcessLauncher.Launcher.HasChildProc && !CommandUtils.ValidateArgumentsForAttach(processId, name, diagnosticPort, out _processId))
{
Expand Down Expand Up @@ -190,7 +190,7 @@ public async Task<int> Collect(CancellationToken ct, List<string> counter_list,
_console.Error.WriteLine($"The output format {format} is not a valid output format.");
return 0;
}
shouldResumeRuntime = ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort);
shouldResumeRuntime = ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort) || resumeRuntime;
int ret = await Start();
return ret;
}
Expand Down
16 changes: 12 additions & 4 deletions src/Tools/dotnet-counters/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public enum CountersExportFormat { csv, json };

internal class Program
{
delegate Task<int> CollectDelegate(CancellationToken ct, List<string> counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string processName, string port);
delegate Task<int> MonitorDelegate(CancellationToken ct, List<string> counter_list, string counters, IConsole console, int processId, int refreshInterval, string processName, string port);
delegate Task<int> CollectDelegate(CancellationToken ct, List<string> counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string processName, string port, bool resumeRuntime);
delegate Task<int> MonitorDelegate(CancellationToken ct, List<string> counter_list, string counters, IConsole console, int processId, int refreshInterval, string processName, string port, bool resumeRuntime);

private static Command MonitorCommand() =>
new Command(
Expand All @@ -33,7 +33,7 @@ private static Command MonitorCommand() =>
// Handler
HandlerDescriptor.FromDelegate((MonitorDelegate)new CounterMonitor().Monitor).GetCommandHandler(),
// Arguments and Options
CounterList(), CounterOption(), ProcessIdOption(), RefreshIntervalOption(), NameOption(), DiagnosticPortOption(),
CounterList(), CounterOption(), ProcessIdOption(), RefreshIntervalOption(), NameOption(), DiagnosticPortOption(), ResumeRuntimeOption()
};

private static Command CollectCommand() =>
Expand All @@ -44,7 +44,7 @@ private static Command CollectCommand() =>
// Handler
HandlerDescriptor.FromDelegate((CollectDelegate)new CounterMonitor().Collect).GetCommandHandler(),
// Arguments and Options
CounterList(), CounterOption(), ProcessIdOption(), RefreshIntervalOption(), ExportFormatOption(), ExportFileNameOption(), NameOption(), DiagnosticPortOption()
CounterList(), CounterOption(), ProcessIdOption(), RefreshIntervalOption(), ExportFormatOption(), ExportFileNameOption(), NameOption(), DiagnosticPortOption(), ResumeRuntimeOption()
};

private static Option NameOption() =>
Expand Down Expand Up @@ -127,6 +127,14 @@ private static Option DiagnosticPortOption() =>
Argument = new Argument<string>(name: "diagnosticPort", getDefaultValue: () => "")
};

private static Option ResumeRuntimeOption() =>
new Option(
alias: "--resume-runtime",
description: @"Resume runtime once session has been initialized, defaults to true. Disable resume of runtime using --resume-runtime:false")
{
Argument = new Argument<bool>(name: "resumeRuntime", getDefaultValue: () => true)
};

private static readonly string[] s_SupportedRuntimeVersions = new[] { "3.0", "3.1", "5.0" };

public static int List(IConsole console, string runtimeVersion)
Expand Down
20 changes: 15 additions & 5 deletions src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Microsoft.Diagnostics.Tools.Trace
{
internal static class CollectCommandHandler
{
delegate Task<int> CollectDelegate(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string port, bool showchildio);
delegate Task<int> CollectDelegate(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string port, bool showchildio, bool resumeRuntime);

/// <summary>
/// Collects a diagnostic trace from a currently running process or launch a child process and trace it.
Expand All @@ -41,8 +41,9 @@ internal static class CollectCommandHandler
/// <param name="clreventlevel">The verbosity level of CLR events</param>
/// <param name="port">Path to the diagnostic port to be created.</param>
/// <param name="showchildio">Should IO from a child process be hidden.</param>
/// <param name="resumeRuntime">Resume runtime once session has been initialized.</param>
/// <returns></returns>
private static async Task<int> Collect(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string diagnosticPort, bool showchildio)
private static async Task<int> Collect(CancellationToken ct, IConsole console, int processId, FileInfo output, uint buffersize, string providers, string profile, TraceFileFormat format, TimeSpan duration, string clrevents, string clreventlevel, string name, string diagnosticPort, bool showchildio, bool resumeRuntime)
{
int ret = 0;
bool collectionStopped = false;
Expand Down Expand Up @@ -150,7 +151,7 @@ private static async Task<int> Collect(CancellationToken ct, IConsole console, i
DiagnosticsClient diagnosticsClient;
Process process;
DiagnosticsClientBuilder builder = new DiagnosticsClientBuilder("dotnet-trace", 10);
bool shouldResumeRuntime = ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort);
bool shouldResumeRuntime = ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort) || resumeRuntime;
var shouldExit = new ManualResetEvent(false);
ct.Register(() => shouldExit.Set());

Expand All @@ -162,7 +163,7 @@ private static async Task<int> Collect(CancellationToken ct, IConsole console, i
return await Task.FromResult(ret);
}
diagnosticsClient = holder.Client;
if (shouldResumeRuntime)
if (ProcessLauncher.Launcher.HasChildProc || !string.IsNullOrEmpty(diagnosticPort))
{
process = Process.GetProcessById(holder.EndpointInfo.ProcessId);
}
Expand Down Expand Up @@ -399,7 +400,8 @@ public static Command CollectCommand() =>
CLREventLevelOption(),
CommonOptions.NameOption(),
DiagnosticPortOption(),
ShowChildIOOption()
ShowChildIOOption(),
ResumeRuntimeOption()
};

private static uint DefaultCircularBufferSizeInMB() => 256;
Expand Down Expand Up @@ -482,5 +484,13 @@ private static Option ShowChildIOOption() =>
{
Argument = new Argument<bool>(name: "show-child-io", getDefaultValue: () => false)
};

private static Option ResumeRuntimeOption() =>
new Option(
alias: "--resume-runtime",
description: @"Resume runtime once session has been initialized, defaults to true. Disable resume of runtime using --resume-runtime:false")
{
Argument = new Argument<bool>(name: "resumeRuntime", getDefaultValue: () => true)
};
}
}