Add command handler to dotnet-counters (#200)
authorSung Yoon Whang <suwhang@microsoft.com>
Fri, 26 Apr 2019 07:50:47 +0000 (00:50 -0700)
committerGitHub <noreply@github.com>
Fri, 26 Apr 2019 07:50:47 +0000 (00:50 -0700)
src/Tools/dotnet-counters/ConsoleWriter.cs
src/Tools/dotnet-counters/CounterMonitor.cs

index 4f2a50e87c4dd7b142c18a7e796e9cd525d7e389..2963d2f8426808120d043cdf609710dd7ba29dc4 100644 (file)
@@ -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());  
index 3c5c004071b807a7c1879c019f4246ba04442692..d0c88ecae7a3352574a21bb8d29f012d556651da 100644 (file)
@@ -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<string, object> payloadVal = (IDictionary<string, object>)(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);