Created
November 18, 2020 19:20
-
-
Save shirhatti/84a7ab5d984a6feea836f18f7eb92b0f to your computer and use it in GitHub Desktop.
EventCounters via ETW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Licensed to the .NET Foundation under one or more agreements. | |
// The .NET Foundation licenses this file to you under the MIT license. | |
// See the LICENSE file in the project root for more information. | |
using System; | |
using System.Collections.Generic; | |
namespace Microsoft.Diagnostics.Tools.Counters | |
{ | |
public interface ICounterPayload | |
{ | |
string GetName(); | |
string GetValue(); | |
string GetDisplay(); | |
} | |
class CounterPayload : ICounterPayload | |
{ | |
public string m_Name; | |
public string m_Value; | |
public string m_DisplayName; | |
public CounterPayload(IDictionary<string, object> payloadFields) | |
{ | |
m_Name = payloadFields["Name"].ToString(); | |
m_Value = payloadFields["Mean"].ToString(); | |
m_DisplayName = payloadFields["DisplayName"].ToString(); | |
} | |
public string GetName() => m_Name; | |
public string GetValue() => m_Value; | |
public string GetDisplay() => m_DisplayName; | |
} | |
class IncrementingCounterPayload : ICounterPayload | |
{ | |
public string m_Name; | |
public string m_Value; | |
public string m_DisplayName; | |
public string m_DisplayRateTimeScale; | |
public IncrementingCounterPayload(IDictionary<string, object> payloadFields) | |
{ | |
m_Name = payloadFields["Name"].ToString(); | |
m_Value = payloadFields["Increment"].ToString(); | |
m_DisplayName = payloadFields["DisplayName"].ToString(); | |
m_DisplayRateTimeScale = TimeSpan.Parse(payloadFields["DisplayRateTimeScale"].ToString()).ToString("%s' sec'"); | |
} | |
public string GetName() => m_Name; | |
public string GetValue() => m_Value; | |
public string GetDisplay() => $"{m_DisplayName} / {m_DisplayRateTimeScale}"; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net5.0</TargetFramework> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="2.0.62" /> | |
</ItemGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using Microsoft.Diagnostics.Tools.Counters; | |
using Microsoft.Diagnostics.Tracing; | |
using Microsoft.Diagnostics.Tracing.Session; | |
var pids = new List<int> { 2412 }; | |
using var session = new TraceEventSession("metrics"); | |
Console.CancelKeyPress += | |
(object s, ConsoleCancelEventArgs args) => session.Stop(); | |
var options = new TraceEventProviderOptions(); | |
options.ProcessIDFilter = pids; | |
options.AddArgument("EventCounterIntervalSec", "1"); | |
session.EnableProvider("System.Runtime", TraceEventLevel.Always, ulong.MaxValue, options); | |
session.Source.Dynamic.AddCallbackForProviderEvent("System.Runtime", "EventCounters", (obj) => | |
{ | |
IDictionary<string, object> payloadFields = (IDictionary<string, object>)(obj.PayloadValue(0)); | |
ICounterPayload payload; | |
if (payloadFields.ContainsKey("CounterType")) | |
{ | |
payload = payloadFields["CounterType"].Equals("Sum") ? (ICounterPayload)new IncrementingCounterPayload(payloadFields) : (ICounterPayload)new CounterPayload(payloadFields); | |
} | |
else | |
{ | |
payload = payloadFields.Count == 6 ? (ICounterPayload)new IncrementingCounterPayload(payloadFields) : (ICounterPayload)new CounterPayload(payloadFields); | |
} | |
Console.WriteLine($"{payload.GetDisplay()}\t{payload.GetValue()}"); | |
}); | |
session.Source.Process(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment