-
Notifications
You must be signed in to change notification settings - Fork 383
Enable dotnet-trace and dotnet-counters collection from startup #1635
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
Merged
Merged
Changes from 29 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
cc40349
add run to counters
sywhang f9d96f7
Add trace run command
sywhang 51dca9b
use acceptasync instead of sleeping... :p
sywhang 907ada3
Counter run works now
sywhang 4eaba69
Fix incorrect cpu-usage descriptor
sywhang 7a873d0
Merge remote-tracking branch 'upstream/master'
sywhang e4e463e
Merge remote-tracking branch 'upstream/master'
sywhang c5e542d
Merge remote-tracking branch 'upstream/master' into dev/suwhang/tools…
sywhang 643cc1c
use processlauncher to handle --
sywhang 8df4435
Merge branch 'master' into dev/suwhang/tools-run
sywhang 2229e45
remove unused code
sywhang 583fcef
make dotnet-counters collect attachable at startup
sywhang 8529324
cleanup
sywhang cd96473
more refactoring
sywhang 0878632
Fix misbehaviors when stopping counters/child proc
sywhang c1b6003
dotnet-trace collect can attach at startup now
sywhang 8c01c55
Fix IOException being thrown for Stop command
sywhang c451374
Defer ResumeRuntime after we create EventPipeSession
sywhang 02451f6
Change the default behavior to not redirect stdin/stdout/stderr
sywhang f381430
terminate child proc upon exit
sywhang 154bb17
remove useless code
sywhang 70ebf8d
gracefully handle older versions that can't connect to reversed server.
sywhang 8e80aa4
terminate child proc
sywhang 7a16985
Fix build err'
sywhang 326cdd3
code review feedback
sywhang bcdae64
Code review feedback
sywhang b32f366
Handle escape characters in argument string to child process
sywhang 27a2565
fix build failure
sywhang 2500d03
fix build failure
sywhang e25dd08
Remove unused code
sywhang ae4eec3
Enable --counters option for default scenario as well
sywhang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
src/Tools/Common/ReversedServerHelpers/ReversedServerHelpers.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using Microsoft.Diagnostics.NETCore.Client; | ||
| using Microsoft.Diagnostics.Tracing.Parsers.Clr; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using System.Text.RegularExpressions; | ||
|
|
||
| namespace Microsoft.Internal.Common.Utils | ||
| { | ||
| // <summary> | ||
| // ProcessLauncher is a child-process launcher for "diagnostics tools at startup" scenarios | ||
| // It launches the target process at startup and passes its processId to the corresponding Command handler. | ||
| // </summary> | ||
| internal class ProcessLauncher | ||
| { | ||
| private Process _childProc = null; | ||
|
|
||
| internal static ProcessLauncher Launcher = new ProcessLauncher(); | ||
|
|
||
| public void PrepareChildProcess(string[] args) | ||
| { | ||
| int unparsedTokenIdx = FindUnparsedTokenIndex(args); | ||
| if (unparsedTokenIdx < 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| _childProc = new Process(); | ||
| _childProc.StartInfo.FileName = args[unparsedTokenIdx]; | ||
| string arguments = ""; | ||
| for (int i = unparsedTokenIdx+1; i < args.Length; i++) | ||
| { | ||
| if (args[i].Contains(" ")) | ||
| { | ||
| arguments += $"\"{args[i].Replace("\"", "\\\"")}\""; | ||
| } | ||
| else | ||
| { | ||
| arguments += args[i]; | ||
| } | ||
|
|
||
| if (i != args.Length) | ||
| arguments += " "; | ||
| } | ||
| _childProc.StartInfo.Arguments = arguments; | ||
| } | ||
|
|
||
| private int FindUnparsedTokenIndex(string[] args) | ||
| { | ||
| for (int i = 0; i < args.Length; i++) | ||
| { | ||
| if (args[i] == "--" && i < (args.Length - 1)) return i+1; | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| public bool HasChildProc | ||
| { | ||
| get | ||
| { | ||
| return _childProc != null; | ||
| } | ||
| } | ||
|
|
||
| public Process ChildProc | ||
| { | ||
| get | ||
| { | ||
| return _childProc; | ||
| } | ||
| } | ||
| public bool Start(string diagnosticTransportName) | ||
| { | ||
| _childProc.StartInfo.UseShellExecute = false; | ||
| _childProc.StartInfo.RedirectStandardOutput = true; | ||
| _childProc.StartInfo.RedirectStandardError = true; | ||
| _childProc.StartInfo.RedirectStandardInput = true; | ||
| _childProc.StartInfo.Environment.Add("DOTNET_DiagnosticPorts", $"{diagnosticTransportName},suspend"); | ||
| try | ||
| { | ||
| _childProc.Start(); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Console.WriteLine($"Cannot start target process: {_childProc.StartInfo.FileName} {_childProc.StartInfo.Arguments}"); | ||
| Console.WriteLine(e.ToString()); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| public void Cleanup() | ||
| { | ||
| if (_childProc != null && !_childProc.HasExited) | ||
| { | ||
| try | ||
| { | ||
| _childProc.Kill(); | ||
| } | ||
| // if process exited while we were trying to kill it, it can throw IOE | ||
| catch (InvalidOperationException) { } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // <summary> | ||
| // This class acts a helper class for building a DiagnosticsClient instance | ||
| // </summary> | ||
| internal class ReversedDiagnosticsClientBuilder | ||
| { | ||
| private static string GetTransportName(string toolName) => $"{toolName}-{Process.GetCurrentProcess().Id}-{DateTime.Now:yyyyMMdd_HHmmss}.socket"; | ||
|
|
||
| // <summary> | ||
| // Starts the child process and returns the diagnostics client once the child proc connects to the reversed diagnostics pipe. | ||
| // The callee needs to resume the diagnostics client at appropriate time. | ||
| // </summary> | ||
| public static DiagnosticsClient Build(ProcessLauncher childProcLauncher, string toolName, int timeoutInSec) | ||
| { | ||
| if (!childProcLauncher.HasChildProc) | ||
| { | ||
| throw new InvalidOperationException("Must have a valid child process to launch."); | ||
| } | ||
| // Create and start the reversed server | ||
| string diagnosticTransportName = GetTransportName(toolName); | ||
| ReversedDiagnosticsServer server = new ReversedDiagnosticsServer(diagnosticTransportName); | ||
| server.Start(); | ||
|
|
||
| // Start the child proc | ||
| if (!childProcLauncher.Start(diagnosticTransportName)) | ||
| { | ||
| throw new InvalidOperationException("Failed to start dotnet-counters."); | ||
| } | ||
|
|
||
| // Wait for attach | ||
| IpcEndpointInfo endpointInfo = server.Accept(TimeSpan.FromSeconds(timeoutInSec)); | ||
|
|
||
| // If for some reason a different process attached to us, wait until the expected process attaches. | ||
| while (endpointInfo.ProcessId != childProcLauncher.ChildProc.Id) | ||
| { | ||
| endpointInfo = server.Accept(TimeSpan.FromSeconds(timeoutInSec)); | ||
| } | ||
| return new DiagnosticsClient(endpointInfo.Endpoint); | ||
sywhang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.