From 2ddfbbb8817c60aa1bbdc7252dc245b3b2786e9c Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Fri, 26 Apr 2019 00:50:47 -0700 Subject: [PATCH] Add command handler to dotnet-counters (#200) --- src/Tools/dotnet-counters/ConsoleWriter.cs | 5 +-- src/Tools/dotnet-counters/CounterMonitor.cs | 37 +++++++++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Tools/dotnet-counters/ConsoleWriter.cs b/src/Tools/dotnet-counters/ConsoleWriter.cs index 4f2a50e87..2963d2f84 100644 --- a/src/Tools/dotnet-counters/ConsoleWriter.cs +++ b/src/Tools/dotnet-counters/ConsoleWriter.cs @@ -33,8 +33,9 @@ namespace Microsoft.Diagnostics.Tools.Counters Console.Clear(); origRow = Console.CursorTop; origCol = Console.CursorLeft; + Console.WriteLine("Press p to pause, r to resume, q to quit."); - maxRow = origRow; + maxRow = origRow+1; maxCol = origCol; } @@ -47,7 +48,7 @@ namespace Microsoft.Diagnostics.Tools.Counters { (int left, int row) = displayPosition[name]; Console.SetCursorPosition(left, row); - Console.Write(new String(' ', 16)); + Console.Write(new String(' ', 8)); Console.SetCursorPosition(left, row); Console.Write(payload.GetValue()); diff --git a/src/Tools/dotnet-counters/CounterMonitor.cs b/src/Tools/dotnet-counters/CounterMonitor.cs index 3c5c00407..d0c88ecae 100644 --- a/src/Tools/dotnet-counters/CounterMonitor.cs +++ b/src/Tools/dotnet-counters/CounterMonitor.cs @@ -25,14 +25,24 @@ namespace Microsoft.Diagnostics.Tools.Counters private ConsoleWriter writer; private CounterFilter filter; private ulong _sessionId; + private bool pauseCmdSet; + public CounterMonitor() { writer = new ConsoleWriter(); filter = new CounterFilter(); + pauseCmdSet = false; } private void Dynamic_All(TraceEvent obj) { + // If we are paused, ignore the event. + // There's a potential race here between the two tasks but not a huge deal if we miss by one event. + if (pauseCmdSet) + { + return; + } + if (obj.EventName.Equals("EventCounters")) { IDictionary payloadVal = (IDictionary)(obj.PayloadValue(0)); @@ -154,10 +164,31 @@ namespace Microsoft.Diagnostics.Tools.Counters source.Process(); }); + Task commandTask = new Task(() => + { + while(true) + { + while (!Console.KeyAvailable) { } + ConsoleKey cmd = Console.ReadKey(true).Key; + if (cmd == ConsoleKey.Q) + { + break; + } + else if (cmd == ConsoleKey.P) + { + pauseCmdSet = true; + } + else if (cmd == ConsoleKey.R) + { + pauseCmdSet = false; + } + } + }); + monitorTask.Start(); - - await monitorTask; - + commandTask.Start(); + await commandTask; + try { EventPipeClient.StopTracing(_processId, _sessionId); -- 2.34.1