private string BuildProviderString() { string providerString; if (_counterList.Count == 0) { CounterProvider defaultProvider = null; _console.Out.WriteLine($"--counters is unspecified. Monitoring System.Runtime counters by default."); // Enable the default profile if nothing is specified if (!KnownData.TryGetProvider("System.Runtime", out defaultProvider)) { _console.Error.WriteLine("No providers or profiles were specified and there is no default profile available."); return(""); } providerString = defaultProvider.ToProviderString(_interval); filter.AddFilter("System.Runtime"); } else { CounterProvider provider = null; StringBuilder sb = new StringBuilder(""); for (var i = 0; i < _counterList.Count; i++) { string counterSpecifier = _counterList[i]; string[] tokens = counterSpecifier.Split('['); string providerName = tokens[0]; if (!KnownData.TryGetProvider(providerName, out provider)) { sb.Append(CounterProvider.SerializeUnknownProviderName(providerName, _interval)); } else { sb.Append(provider.ToProviderString(_interval)); } if (i != _counterList.Count - 1) { sb.Append(","); } if (tokens.Length == 1) { filter.AddFilter(providerName); // This means no counter filter was specified. } else { string counterNames = tokens[1]; string[] enabledCounters = counterNames.Substring(0, counterNames.Length - 1).Split(','); filter.AddFilter(providerName, enabledCounters); } } providerString = sb.ToString(); } return(providerString); }
private async Task <int> StartMonitor() { if (_processId == 0) { _console.Error.WriteLine("ProcessId is required."); return(1); } String providerString; if (_counterList.Count == 0) { CounterProvider defaultProvider = null; _console.Out.WriteLine($"counter_list is unspecified. Monitoring all counters by default."); // Enable the default profile if nothing is specified if (!KnownData.TryGetProvider("System.Runtime", out defaultProvider)) { _console.Error.WriteLine("No providers or profiles were specified and there is no default profile available."); return(1); } providerString = defaultProvider.ToProviderString(_interval); filter.AddFilter("System.Runtime"); } else { CounterProvider provider = null; StringBuilder sb = new StringBuilder(""); for (var i = 0; i < _counterList.Count; i++) { string counterSpecifier = _counterList[i]; string[] tokens = counterSpecifier.Split('['); string providerName = tokens[0]; if (!KnownData.TryGetProvider(providerName, out provider)) { sb.Append(CounterProvider.SerializeUnknownProviderName(providerName, _interval)); } else { sb.Append(provider.ToProviderString(_interval)); } if (i != _counterList.Count - 1) { sb.Append(","); } if (tokens.Length == 1) { filter.AddFilter(providerName); // This means no counter filter was specified. } else { string counterNames = tokens[1]; string[] enabledCounters = counterNames.Substring(0, counterNames.Length - 1).Split(','); filter.AddFilter(providerName, enabledCounters); } } providerString = sb.ToString(); } ManualResetEvent shouldExit = new ManualResetEvent(false); var terminated = false; writer.InitializeDisplay(); Task monitorTask = new Task(() => { try { var configuration = new SessionConfiguration( circularBufferSizeMB: 1000, format: EventPipeSerializationFormat.NetTrace, providers: Trace.Extensions.ToProviders(providerString)); var binaryReader = EventPipeClient.CollectTracing(_processId, configuration, out _sessionId); EventPipeEventSource source = new EventPipeEventSource(binaryReader); source.Dynamic.All += Dynamic_All; source.Process(); } catch (Exception ex) { Debug.WriteLine($"[ERROR] {ex.ToString()}"); } finally { terminated = true; // This indicates that the runtime is done. We shouldn't try to talk to it anymore. shouldExit.Set(); } }); monitorTask.Start(); Console.CancelKeyPress += (sender, args) => { args.Cancel = true; shouldExit.Set(); }; while (!shouldExit.WaitOne(250)) { while (true) { if (shouldExit.WaitOne(250)) { StopMonitor(); return(0); } if (Console.KeyAvailable) { break; } } 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; } } if (!terminated) { StopMonitor(); } return(await Task.FromResult(0)); }
private async Task <int> StartMonitor() { if (_processId == 0) { _console.Error.WriteLine("ProcessId is required."); return(1); } String providerString; if (_counterList.Count == 0) { CounterProvider defaultProvider = null; _console.Out.WriteLine($"counter_list is unspecified. Monitoring all counters by default."); // Enable the default profile if nothing is specified if (!KnownData.TryGetProvider("System.Runtime", out defaultProvider)) { _console.Error.WriteLine("No providers or profiles were specified and there is no default profile available."); return(1); } providerString = defaultProvider.ToProviderString(_interval); filter.AddFilter("System.Runtime"); } else { CounterProvider provider = null; StringBuilder sb = new StringBuilder(""); for (var i = 0; i < _counterList.Count; i++) { string counterSpecifier = _counterList[i]; string[] tokens = counterSpecifier.Split('['); string providerName = tokens[0]; if (!KnownData.TryGetProvider(providerName, out provider)) { sb.Append(CounterProvider.SerializeUnknownProviderName(providerName, _interval)); } else { sb.Append(provider.ToProviderString(_interval)); } if (i != _counterList.Count - 1) { sb.Append(","); } if (tokens.Length == 1) { filter.AddFilter(providerName); // This means no counter filter was specified. } else { string counterNames = tokens[1]; string[] enabledCounters = counterNames.Substring(0, counterNames.Length - 1).Split(','); filter.AddFilter(providerName, enabledCounters); } } providerString = sb.ToString(); } ManualResetEvent shouldExit = new ManualResetEvent(false); _ct.Register(() => shouldExit.Set()); var terminated = false; writer.InitializeDisplay(); Task monitorTask = new Task(() => { try { RequestTracingV2(providerString); } catch (EventPipeUnknownCommandException) { // If unknown command exception is thrown, it's likely the app being monitored is running an older version of runtime that doesn't support CollectTracingV2. Try again with V1. RequestTracingV1(providerString); } catch (Exception ex) { Debug.WriteLine($"[ERROR] {ex.ToString()}"); } finally { terminated = true; // This indicates that the runtime is done. We shouldn't try to talk to it anymore. shouldExit.Set(); } }); monitorTask.Start(); while (!shouldExit.WaitOne(250)) { while (true) { if (shouldExit.WaitOne(250)) { StopMonitor(); return(0); } if (Console.KeyAvailable) { break; } } 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; } } if (!terminated) { StopMonitor(); } return(await Task.FromResult(0)); }
private async Task <int> StartMonitor() { if (_processId == 0) { _console.Error.WriteLine("ProcessId is required."); return(1); } if (_interval == 0) { _console.Error.WriteLine("refreshInterval is required."); return(1); } String providerString; if (_counterList.Count == 0) { CounterProvider defaultProvider = null; _console.Out.WriteLine($"counter_list is unspecified. Monitoring all counters by default."); // Enable the default profile if nothing is specified if (!KnownData.TryGetProvider("System.Runtime", out defaultProvider)) { _console.Error.WriteLine("No providers or profiles were specified and there is no default profile available."); return(1); } providerString = defaultProvider.ToProviderString(_interval); filter.AddFilter("System.Runtime"); } else { CounterProvider provider = null; StringBuilder sb = new StringBuilder(""); for (var i = 0; i < _counterList.Count; i++) { string counterSpecifier = _counterList[i]; string[] tokens = counterSpecifier.Split('['); string providerName = tokens[0]; if (!KnownData.TryGetProvider(providerName, out provider)) { sb.Append(CounterProvider.SerializeUnknownProviderName(providerName, _interval)); } else { sb.Append(provider.ToProviderString(_interval)); } if (i != _counterList.Count - 1) { sb.Append(","); } if (tokens.Length == 1) { filter.AddFilter(providerName); // This means no counter filter was specified. } else { string counterNames = tokens[1]; string[] enabledCounters = counterNames.Substring(0, counterNames.Length - 1).Split(','); filter.AddFilter(providerName, enabledCounters); } } providerString = sb.ToString(); } var terminated = false; Task monitorTask = new Task(() => { var configuration = new SessionConfiguration( circularBufferSizeMB: 1000, outputPath: "", providers: Trace.Extensions.ToProviders(providerString)); var binaryReader = EventPipeClient.CollectTracing(_processId, configuration, out _sessionId); EventPipeEventSource source = new EventPipeEventSource(binaryReader); writer.InitializeDisplay(); source.Dynamic.All += Dynamic_All; source.Process(); terminated = true; // This indicates that the runtime is done. We shoudn't try to talk to it anymore. }); 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(); commandTask.Start(); await commandTask; if (!terminated) { try { EventPipeClient.StopTracing(_processId, _sessionId); } catch (System.IO.EndOfStreamException) {} // If the app we're monitoring exits abrubtly, this may throw in which case we just swallow the exception and exit gracefully. } return(0); }
public static bool TryGetProvider(string providerName, out CounterProvider provider) => _knownProviders.TryGetValue(providerName, out provider);