示例#1
0
        private static void PrintRuntimeGCEvents(string file)
        {
            var source = new EventPipeEventSource(file);

            // https://devblogs.microsoft.com/dotnet/glad-part-2/
            source.NeedLoadedDotNetRuntimes();
            source.AddCallbackOnProcessStart(proc =>
            {
                proc.AddCallbackOnDotNetRuntimeLoad(runtime =>
                {
                    runtime.GCEnd += RuntimeOnGCEnd;
                });
            });

            //source.Clr.All += (TraceEvent obj) => { Console.WriteLine(obj.EventName); };
            try
            {
                source.Process();
            }
            // NOTE: This exception does not currently exist. It is something that needs to be added to TraceEvent.
            catch (Exception e)
            {
                Console.WriteLine("Error encountered while processing events");
                Console.WriteLine(e.ToString());
            }
        }
示例#2
0
        public void Start()
        {
            Task.Run(() => {
                try {
                    var client       = new DiagnosticsClient(_pid);
                    _session         = client.StartEventPipeSession(_providers, false);
                    using var source = new EventPipeEventSource(_session.EventStream);

                    source.NeedLoadedDotNetRuntimes();
                    source.AddCallbackOnProcessStart(proc => {
                        if (proc.ProcessID != _pid)
                        {
                            return;
                        }

                        proc.AddCallbackOnDotNetRuntimeLoad(runtime => {
                            runtime.GCEnd += (process, gc) => {
                                var key = $"gen-{gc.Generation}-gc-collection-count";
                                _collectedStats.TryGetValue(key, out var collected);
                                _collectedStats[key] = ++collected;
                            };
                        });
                    });

                    source.Dynamic.All += obj => {
                        if (obj.EventName.Equals("EventCounters"))
                        {
                            var payload = (IDictionary <string, object>)obj.PayloadValue(0);
                            var pairs   = (IDictionary <string, object>)(payload["Payload"]);

                            var name = string.Intern(pairs["Name"].ToString());

                            var counterType = pairs["CounterType"];
                            if (counterType.Equals("Sum"))
                            {
                                _collectedStats[name] = double.Parse(pairs["Increment"].ToString());
                            }

                            if (counterType.Equals("Mean"))
                            {
                                _collectedStats[name] = double.Parse(pairs["Mean"].ToString());
                            }
                        }
                    };

                    source.Process();
                } catch (ObjectDisposedException) {
                    // ignore exception on shutdown
                } catch (Exception exception) {
                    Log.Warning(exception, "Error encountered while processing events");
                }
            });
        }
        static void PrintRuntimeGCEvents(int processId)
        {
            var providers = new List <EventPipeProvider>()
            {
                new EventPipeProvider("Microsoft-Windows-DotNETRuntime",
                                      EventLevel.Informational, (long)ClrTraceEventParser.Keywords.GC)
            };

            var client = new DiagnosticsClient(processId);

            using (var session = client.StartEventPipeSession(providers, false))
            {
                var source = new EventPipeEventSource(session.EventStream);

                // https://devblogs.microsoft.com/dotnet/glad-part-2/
                source.NeedLoadedDotNetRuntimes();
                source.AddCallbackOnProcessStart(proc =>
                {
                    proc.AddCallbackOnDotNetRuntimeLoad(runtime =>
                    {
                        runtime.GCEnd += RuntimeOnGCEnd;
                    });
                });

                source.Clr.All += (TraceEvent obj) => { Console.WriteLine(obj.EventName); };
                try
                {
                    source.Process();
                }
                // NOTE: This exception does not currently exist. It is something that needs to be added to TraceEvent.
                catch (Exception e)
                {
                    Console.WriteLine("Error encountered while processing events");
                    Console.WriteLine(e.ToString());
                }
            }
        }