diff --git a/src/Tools/dotnet-counters/CounterMonitor.cs b/src/Tools/dotnet-counters/CounterMonitor.cs index 185259014c..c8eee8df70 100644 --- a/src/Tools/dotnet-counters/CounterMonitor.cs +++ b/src/Tools/dotnet-counters/CounterMonitor.cs @@ -91,7 +91,7 @@ private void StopMonitor() _renderer.Stop(); } - public async Task Monitor(CancellationToken ct, List counter_list, string counters, IConsole console, int processId, int refreshInterval, string name, string diagnosticPort) + public async Task Monitor(CancellationToken ct, List 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)) { @@ -115,7 +115,7 @@ public async Task Monitor(CancellationToken ct, List 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; @@ -135,7 +135,7 @@ public async Task Monitor(CancellationToken ct, List counter_list, } - public async Task Collect(CancellationToken ct, List counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string name, string diagnosticPort) + public async Task Collect(CancellationToken ct, List 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)) { @@ -190,7 +190,7 @@ public async Task Collect(CancellationToken ct, List 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; } diff --git a/src/Tools/dotnet-counters/Program.cs b/src/Tools/dotnet-counters/Program.cs index 5da067fcd9..ff040f3572 100644 --- a/src/Tools/dotnet-counters/Program.cs +++ b/src/Tools/dotnet-counters/Program.cs @@ -22,8 +22,8 @@ public enum CountersExportFormat { csv, json }; internal class Program { - delegate Task CollectDelegate(CancellationToken ct, List counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string processName, string port); - delegate Task MonitorDelegate(CancellationToken ct, List counter_list, string counters, IConsole console, int processId, int refreshInterval, string processName, string port); + delegate Task CollectDelegate(CancellationToken ct, List counter_list, string counters, IConsole console, int processId, int refreshInterval, CountersExportFormat format, string output, string processName, string port, bool resumeRuntime); + delegate Task MonitorDelegate(CancellationToken ct, List counter_list, string counters, IConsole console, int processId, int refreshInterval, string processName, string port, bool resumeRuntime); private static Command MonitorCommand() => new Command( @@ -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() => @@ -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() => @@ -127,6 +127,14 @@ private static Option DiagnosticPortOption() => Argument = new Argument(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(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) diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs index 3764b1da9f..65856947ae 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs @@ -21,7 +21,7 @@ namespace Microsoft.Diagnostics.Tools.Trace { internal static class CollectCommandHandler { - delegate Task 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 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); /// /// Collects a diagnostic trace from a currently running process or launch a child process and trace it. @@ -41,8 +41,9 @@ internal static class CollectCommandHandler /// The verbosity level of CLR events /// Path to the diagnostic port to be created. /// Should IO from a child process be hidden. + /// Resume runtime once session has been initialized. /// - private static async Task 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 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; @@ -150,7 +151,7 @@ private static async Task 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()); @@ -162,7 +163,7 @@ private static async Task 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); } @@ -399,7 +400,8 @@ public static Command CollectCommand() => CLREventLevelOption(), CommonOptions.NameOption(), DiagnosticPortOption(), - ShowChildIOOption() + ShowChildIOOption(), + ResumeRuntimeOption() }; private static uint DefaultCircularBufferSizeInMB() => 256; @@ -482,5 +484,13 @@ private static Option ShowChildIOOption() => { Argument = new Argument(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(name: "resumeRuntime", getDefaultValue: () => true) + }; } }