예제 #1
0
        public static int List(IConsole console, string runtimeVersion)
        {
            if (!s_SupportedRuntimeVersions.Contains(runtimeVersion))
            {
                Console.WriteLine($"{runtimeVersion} is not a supported version string or a supported runtime version.");
                Console.WriteLine("Supported version strings: 3.0, 3.1, 5.0");
                return(0);
            }
            var profiles      = KnownData.GetAllProviders(runtimeVersion);
            var maxNameLength = profiles.Max(p => p.Name.Length);

            Console.WriteLine($"Showing well-known counters for .NET (Core) version {runtimeVersion} only. Specific processes may support additional counters.");
            foreach (var profile in profiles)
            {
                var counters             = profile.GetAllCounters();
                var maxCounterNameLength = counters.Max(c => c.Name.Length);
                Console.WriteLine($"{profile.Name.PadRight(maxNameLength)}");
                foreach (var counter in profile.Counters.Values)
                {
                    Console.WriteLine($"    {counter.Name.PadRight(maxCounterNameLength)} \t\t {counter.Description}");
                }
                Console.WriteLine("");
            }
            return(1);
        }
        public void TestASPNETProvider()
        {
            KnownData.TryGetProvider("Microsoft.AspNetCore.Hosting", out CounterProvider aspnetProvider);

            Assert.Equal("Microsoft.AspNetCore.Hosting", aspnetProvider.Name);
            Assert.Equal("0x0", aspnetProvider.Keywords);
            Assert.Equal("4", aspnetProvider.Level);
            Assert.Equal("Microsoft.AspNetCore.Hosting:0x0:4:EventCounterIntervalSec=5", aspnetProvider.ToProviderString(5));
        }
        public void TestRuntimeProvider()
        {
            KnownData.TryGetProvider("System.Runtime", out CounterProvider runtimeProvider);

            Assert.Equal("System.Runtime", runtimeProvider.Name);
            Assert.Equal("0xffffffff", runtimeProvider.Keywords);
            Assert.Equal("5", runtimeProvider.Level);
            Assert.Equal("System.Runtime:0xffffffff:5:EventCounterIntervalSec=1", runtimeProvider.ToProviderString(1));
        }
예제 #4
0
        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);
        }
예제 #5
0
        public static int List(IConsole console)
        {
            var profiles      = KnownData.GetAllProviders();
            var maxNameLength = profiles.Max(p => p.Name.Length);

            Console.WriteLine("Showing well-known counters only. Specific processes may support additional counters.\n");
            foreach (var profile in profiles)
            {
                var counters             = profile.GetAllCounters();
                var maxCounterNameLength = counters.Max(c => c.Name.Length);
                Console.WriteLine($"{profile.Name.PadRight(maxNameLength)}");
                foreach (var counter in profile.Counters.Values)
                {
                    Console.WriteLine($"    {counter.Name.PadRight(maxCounterNameLength)} \t\t {counter.Description}");
                }
                Console.WriteLine("");
            }
            return(1);
        }
예제 #6
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);
            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));
        }
        public void UnknownProvider()
        {
            KnownData.TryGetProvider("SomeRandomProvider", out CounterProvider randomProvider);

            Assert.Null(randomProvider);
        }
예제 #8
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));
        }
예제 #9
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);
        }
예제 #10
0
 public ObservedProvider(string name)
 {
     Name = name;
     KnownData.TryGetProvider(name, out KnownProvider);
 }