From 5467e9f6e111ce52b69362ceaf9ee5b1a6efad70 Mon Sep 17 00:00:00 2001 From: Brian Robbins Date: Fri, 8 Jan 2021 13:52:01 -0800 Subject: [PATCH] Remove PerfTrace support. --- src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs | 35 ++++---- .../Tracing/PerfTrace.cs | 34 -------- .../Tracing/PerfTraceEvent.cs | 27 ------- .../Tracing/PerfTraceOutput.cs | 80 ------------------- .../Tracing/PerfTraceThreadContext.cs | 76 ------------------ src/Cli/dotnet/Program.cs | 53 +++++------- 6 files changed, 34 insertions(+), 271 deletions(-) delete mode 100644 src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTrace.cs delete mode 100644 src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceEvent.cs delete mode 100644 src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceOutput.cs delete mode 100644 src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceThreadContext.cs diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs index 9ee240c67d29..f1c9f844f158 100644 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs +++ b/src/Cli/Microsoft.DotNet.Cli.Utils/Command.cs @@ -52,28 +52,25 @@ public CommandResult Execute(Action processStarted) Reporter.Verbose.WriteLine($"> {FormatProcessInfo(_process.StartInfo)}".White()); #endif - using (PerfTrace.Current.CaptureTiming($"{Path.GetFileNameWithoutExtension(_process.StartInfo.FileName)} {_process.StartInfo.Arguments}")) + using (var reaper = new ProcessReaper(_process)) { - using (var reaper = new ProcessReaper(_process)) + _process.Start(); + if (processStarted != null) { - _process.Start(); - if (processStarted != null) - { - processStarted(_process); - } - reaper.NotifyProcessStarted(); - - Reporter.Verbose.WriteLine(string.Format( - LocalizableStrings.ProcessId, - _process.Id)); - - var taskOut = _stdOut?.BeginRead(_process.StandardOutput); - var taskErr = _stdErr?.BeginRead(_process.StandardError); - _process.WaitForExit(); - - taskOut?.Wait(); - taskErr?.Wait(); + processStarted(_process); } + reaper.NotifyProcessStarted(); + + Reporter.Verbose.WriteLine(string.Format( + LocalizableStrings.ProcessId, + _process.Id)); + + var taskOut = _stdOut?.BeginRead(_process.StandardOutput); + var taskErr = _stdErr?.BeginRead(_process.StandardError); + _process.WaitForExit(); + + taskOut?.Wait(); + taskErr?.Wait(); } var exitCode = _process.ExitCode; diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTrace.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTrace.cs deleted file mode 100644 index 696c00195b37..000000000000 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTrace.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Threading; - -namespace Microsoft.DotNet.Cli.Utils -{ - public static class PerfTrace - { - private static ConcurrentBag _threads = new ConcurrentBag(); - - [ThreadStatic] - private static PerfTraceThreadContext _current; - - public static bool Enabled { get; set; } - - public static PerfTraceThreadContext Current => _current ?? (_current = InitializeCurrent()); - - private static PerfTraceThreadContext InitializeCurrent() - { - var context = new PerfTraceThreadContext(Thread.CurrentThread.ManagedThreadId); - _threads.Add(context); - return context; - } - - public static IEnumerable GetEvents() - { - return _threads; - } - } -} diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceEvent.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceEvent.cs deleted file mode 100644 index 9bf2e36ee51e..000000000000 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceEvent.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Microsoft.DotNet.Cli.Utils -{ - public class PerfTraceEvent - { - public string Type { get; } - public string Instance { get; } - public DateTime StartUtc { get; } - public TimeSpan Duration { get; } - public IList Children { get; } - - public PerfTraceEvent(string type, string instance, IEnumerable children, DateTime startUtc, TimeSpan duration) - { - Type = type; - Instance = instance; - StartUtc = startUtc; - Duration = duration; - Children = children.OrderBy(e => e.StartUtc).ToList(); - } - } -} diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceOutput.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceOutput.cs deleted file mode 100644 index 2fbfbeea49f3..000000000000 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceOutput.cs +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Generic; -using System.Text; - -namespace Microsoft.DotNet.Cli.Utils -{ - public class PerfTraceOutput - { - private static TimeSpan _minDuration = TimeSpan.FromSeconds(0.001); - - public static void Print(Reporter reporter, IEnumerable contexts) - { - foreach (var threadContext in contexts) - { - Print(reporter, new[] { threadContext.Root }, threadContext.Root, null); - } - } - - private static void Print(Reporter reporter, IEnumerable events, PerfTraceEvent root, PerfTraceEvent parent, int padding = 0) - { - foreach (var e in events) - { - if (e.Duration < _minDuration) - { - continue; - } - reporter.Write(new string(' ', padding)); - reporter.WriteLine(FormatEvent(e, root, parent)); - Print(reporter, e.Children, root, e, padding + 2); - } - } - - private static string FormatEvent(PerfTraceEvent e, PerfTraceEvent root, PerfTraceEvent parent) - { - var builder = new StringBuilder(); - FormatEventTimeStat(builder, e, root, parent); - builder.Append($" {e.Type.Bold()} {e.Instance}"); - return builder.ToString(); - } - - private static void FormatEventTimeStat(StringBuilder builder, PerfTraceEvent e, PerfTraceEvent root, PerfTraceEvent parent) - { - builder.Append("["); - if (root != e) - { - AppendTime(builder, e.Duration.TotalSeconds / root.Duration.TotalSeconds, 0.2); - } - AppendTime(builder, e.Duration.TotalSeconds / parent?.Duration.TotalSeconds, 0.5); - builder.Append($"{e.Duration.ToString("ss\\.fff\\s").Blue()}]"); - } - - private static void AppendTime(StringBuilder builder, double? percent, double threshold) - { - if (percent != null) - { - var formattedPercent = $"{percent*100:00\\.00%}"; - if (percent > threshold) - { - builder.Append(formattedPercent.Red()); - } - else if (percent > threshold / 2) - { - builder.Append(formattedPercent.Yellow()); - } - else if (percent > threshold / 5) - { - builder.Append(formattedPercent.Green()); - } - else - { - builder.Append(formattedPercent); - } - builder.Append(" "); - } - } - } -} diff --git a/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceThreadContext.cs b/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceThreadContext.cs deleted file mode 100644 index 2be2af011e3f..000000000000 --- a/src/Cli/Microsoft.DotNet.Cli.Utils/Tracing/PerfTraceThreadContext.cs +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright (c) .NET Foundation and contributors. All rights reserved. -// Licensed under the MIT license. See LICENSE file in the project root for full license information. - -using System; -using System.Collections.Concurrent; -using System.Diagnostics; -using System.IO; -using System.Runtime.CompilerServices; -using System.Threading; - -namespace Microsoft.DotNet.Cli.Utils -{ - public class PerfTraceThreadContext - { - private readonly int _threadId; - - private TimerDisposable _activeEvent; - - public PerfTraceEvent Root => _activeEvent.CreateEvent(); - - public PerfTraceThreadContext(int threadId) - { - _activeEvent = new TimerDisposable(this, "Thread", $"{threadId.ToString()}"); - _threadId = threadId; - } - - public IDisposable CaptureTiming(string instance = "", [CallerMemberName] string memberName = "", [CallerFilePath] string filePath = "") - { - if(!PerfTrace.Enabled) - { - return null; - } - - var newTimer = new TimerDisposable(this, $"{Path.GetFileNameWithoutExtension(filePath)}:{memberName}", instance); - var previousTimer = Interlocked.Exchange(ref _activeEvent, newTimer); - newTimer.Parent = previousTimer; - return newTimer; - } - - private void RecordTiming(PerfTraceEvent newEvent, TimerDisposable parent) - { - Interlocked.Exchange(ref _activeEvent, parent); - _activeEvent.Children.Add(newEvent); - } - - private class TimerDisposable : IDisposable - { - private readonly PerfTraceThreadContext _context; - private string _eventType; - private string _instance; - private DateTime _startUtc; - private Stopwatch _stopwatch = Stopwatch.StartNew(); - - public TimerDisposable Parent { get; set; } - - public ConcurrentBag Children { get; set; } = new ConcurrentBag(); - - public TimerDisposable(PerfTraceThreadContext context, string eventType, string instance) - { - _context = context; - _eventType = eventType; - _instance = instance; - _startUtc = DateTime.UtcNow; - } - - public void Dispose() - { - _stopwatch.Stop(); - - _context.RecordTiming(CreateEvent(), Parent); - } - - public PerfTraceEvent CreateEvent() => new PerfTraceEvent(_eventType, _instance, Children, _startUtc, _stopwatch.Elapsed); - } - } -} diff --git a/src/Cli/dotnet/Program.cs b/src/Cli/dotnet/Program.cs index 478c10c824b2..2bc078ce9bbd 100644 --- a/src/Cli/dotnet/Program.cs +++ b/src/Cli/dotnet/Program.cs @@ -51,19 +51,11 @@ public static int Main(string[] args) PerformanceLogEventSource.Log.LogStartUpInformation(startupInfo); PerformanceLogEventSource.Log.CLIStart(); - if (Env.GetEnvironmentVariableAsBool("DOTNET_CLI_CAPTURE_TIMING", false)) - { - PerfTrace.Enabled = true; - } - InitializeProcess(); try { - using (PerfTrace.Current.CaptureTiming()) - { - return ProcessArgs(args); - } + return ProcessArgs(args); } catch (HelpException e) { @@ -94,12 +86,6 @@ public static int Main(string[] args) } finally { - if (PerfTrace.Enabled) - { - Reporter.Output.WriteLine("Performance Summary:"); - PerfTraceOutput.Print(Reporter.Output, PerfTrace.GetEvents()); - } - PerformanceLogEventSource.Log.CLIStop(); } } @@ -280,27 +266,24 @@ private static void ConfigureDotNetForFirstTimeUse( DotnetFirstRunConfiguration dotnetFirstRunConfiguration, IEnvironmentProvider environmentProvider) { - using (PerfTrace.Current.CaptureTiming()) + var environmentPath = EnvironmentPathFactory.CreateEnvironmentPath(isDotnetBeingInvokedFromNativeInstaller, environmentProvider); + var commandFactory = new DotNetCommandFactory(alwaysRunOutOfProc: true); + var aspnetCertificateGenerator = new AspNetCoreCertificateGenerator(); + var dotnetConfigurer = new DotnetFirstTimeUseConfigurer( + firstTimeUseNoticeSentinel, + aspNetCertificateSentinel, + aspnetCertificateGenerator, + toolPathSentinel, + dotnetFirstRunConfiguration, + Reporter.Output, + CliFolderPathCalculator.CliFallbackFolderPath, + environmentPath); + + dotnetConfigurer.Configure(); + + if (isDotnetBeingInvokedFromNativeInstaller && OperatingSystem.IsWindows()) { - var environmentPath = EnvironmentPathFactory.CreateEnvironmentPath(isDotnetBeingInvokedFromNativeInstaller, environmentProvider); - var commandFactory = new DotNetCommandFactory(alwaysRunOutOfProc: true); - var aspnetCertificateGenerator = new AspNetCoreCertificateGenerator(); - var dotnetConfigurer = new DotnetFirstTimeUseConfigurer( - firstTimeUseNoticeSentinel, - aspNetCertificateSentinel, - aspnetCertificateGenerator, - toolPathSentinel, - dotnetFirstRunConfiguration, - Reporter.Output, - CliFolderPathCalculator.CliFallbackFolderPath, - environmentPath); - - dotnetConfigurer.Configure(); - - if (isDotnetBeingInvokedFromNativeInstaller && OperatingSystem.IsWindows()) - { - DotDefaultPathCorrector.Correct(); - } + DotDefaultPathCorrector.Correct(); } }