48
© Copyright SELA Software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com Sasha Goldshtein blog.sashag.net CTO, Sela Group @goldshtn Monitoring .NET Performance with Event Tracing for Windows https://s.sashag.net/etwsdd

Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

  • Upload
    others

  • View
    17

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

© Copyright SELA Software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com

Sasha Goldshtein blog.sashag.netCTO, Sela Group @goldshtn

Monitoring .NET Performance with Event Tracing for Windows

https://s.sashag.net/etwsdd

Page 2: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Agenda

ETW on the performance monitoring spectrumSemantic loggingCapturing and analyzing tracesAdvanced trace analysis with PerfViewMemory leak analysis with PerfViewProgrammatic ETW recording and analysis

Page 3: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Introduction to ETW

Page 4: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Today’s focus

Performance Monitoring Spectrum

Performance metrics and simulations

Development-time profiling

Performance/load tests

Production-time performance investigations

Continuous low-overhead

monitoring

Page 5: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Problems with Traditional Profilers

Invasiveness• Often requires restart or code injection

Overhead• 2x slowdowns are not unheard of

Trace size• Often not applicable for continuous monitoring for hours/days on end

Licensing costs• Production mode or remote profiling mode not always available

Page 6: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Event Tracing for WindowsHigh-performance facility for emitting 100K+ log events per second with rich payloads and stack trace supportUsed widely across Windows, .NET, drivers, services, third party components

Page 7: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

ETW ParticipantsA provider generates ETW eventsA controller starts and stops ETW collectionA consumer logs, analyzes, or processes ETW events

ProvidersProviders

ProvidersProviders

ProvidersConsumers

ProvidersControllers

Event tracing sessions

events

Log filesevents

real-time

logged events

buffers

Page 8: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Sample ETW ScenariosProfile an app in sampling mode

Perform wait-time analysis

Log disk accesses

including stacks

Log GC and JIT events

Log memory allocation statistics

(.NET/C++)

Custom application event log

Page 9: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Semantic Logging

Page 10: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Semantic LoggingUnstructured logs are a thing of the past

Require additional parsing, discipline, and tools to get through

Semantic logging associates structure with each log message

Unstructured Structured

[572 09:04:10.112/@prod1] WARN Connection to cluster head node head1 lost, attempting reconnect in 2 seconds from prod2; this may be a temporary failure

TYPE = CONN_HEAD_LOSTID = 572TIME = 09:04:10.112SERVER = prod1HEAD_NODE = head1RECONNECT_IN = 00:00:02RECONNECT_FROM = prod2

Page 11: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

ETW Events Have StructureEach ETW event has a well-defined payloadTools can use the payload format to easily consume and analyze large volumes of logging information

<Event MSec="5905.4352" PID="476" PName="explorer" TID="2352"EventName="EdgeUi_Invoke_Mouse_HitSingleEdge"TimeStamp="12/01/14 21:56:06.880587" ID="3205"Level="Informational" CPU="7" EventIndex="27983"ProviderName="Microsoft-Windows-Immersive-Shell" ...>

<PrettyPrint><Event ... EdgeUiComponent="3"/>

</PrettyPrint>...

</Event>

Page 12: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Semantic Logging Application BlockA p&p project that logs events from an ETW provider to a database, text file, Azure storage, ElasticSearch, etc.

Page 13: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

EventSourceThe EventSource class greatly simplifies ETW providers in .NET applications

class PerformanceEventsSource : EventSource{

public void StartOperation(string op) { WriteEvent(1, op); }

[Event(2, Message = "Completed {0} with result {1}")]public void StopOperation(string op, string result){

WriteEvent(2, op, result);}

}

Page 14: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Configuring SLABConsoleLog.CreateListener().EnableEvents(

OperationsEventSource.Log, EventLevel.LogAlways);

RollingFlatFileLog.CreateListener("informational.log", 1000, "yyyy-MM-dd",RollFileExistsBehavior.Increment, RollInterval.Hour).EnableEvents(OperationsEventSource.Log,

EventLevel.Informational);

var listener = new ObservableEventListener();listener.EnableEvents(OperationsEventSource.Log,

EventLevel.LogAlways);listener.LogToElasticsearch(Environment.MachineName,

"http://localhost:9200", "slab", "logs",bufferingCount: 1);

Page 15: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Structured Payload Examples

Page 16: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Trace Capturing and Analysis

Page 17: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

ETW Toolsxperf.exe: Command-line tool for ETW capturing and processingwpr.exe: Command-line and GUI for end userswpa.exe: Visual trace analysis toolPerfView.exe: Visual tool for capturing and recording ETW events from managed providers and the CLRlogman.exe, tracerpt.exe: Built-in Windows tools for trace recording and formatting

Page 18: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Production UseAll ETW tools are suitable for production useSome things to watch out for:

Choose event providers carefully to minimize the performance impact on the systemCapture to a circular log file to avoid running out of disk spaceSet triggers to stop collection (and keep all preceding events) when a critical event occurs

Page 19: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Capturing a TraceXperf

xperf -on DiagEasy...xperf -d diag.etl

WPR

Page 20: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Xperf Providers and FlagsXperf makes it easy to capture a trace from the kernel provider using a set of flags and groupsExamples:

Group Flags

Base PROC_THREAD, LOADER, DISK_IO, HARD_FAULTS, PROFILE, MEMINFO, MEMINFO_WS

FileIO PROC_THREAD, LOADER, DISK_IO, HARD_FAULTS, FILE_IO, FILE_IO_INIT

SysProf PROC_THREAD, LOADER, PROFILE

Process/thread creation/deletion

1ms sampling event for profiling

File create, delete, read, write, etc.

operations

Page 21: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

What’s In A Trace?A trace is a huge list of eventsEvents have multiple columns (payload)Useless without additional processing

Page 22: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Trace Processing with XperfI/O summary report per file

xperf -i fileio.etl-o fileio.csv -a diskio-summary

Interactive profiling report (for a specific process)

xperf -i cpu.etl-o cpu.html -symbols-a stacks -process app.exe -butterfly

Page 23: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Managed StacksTo display managed stack traces correctly, additional CLR data is requiredWPR & PerfView take care of this automaticallyIf using Xperf, see:http://msdn.microsoft.com/en-us/library/windows/desktop/hh448186.aspx

Page 24: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Trace Analysis with WPA

List of graphs

Graph display

Ungrouped columnsGrouped

columns Grouping bar

Page 25: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Stack SummariesLearn how to read stack summaries

Group by Stack columnExpand “hot path”, like in profiler

Stack resolution requires symbols (slow)

Page 26: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Lab

Collecting File I/O Information

Page 27: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

PerfViewETW collection and analysis tool tailored for .NET applications (but not only)Can be used as a sampling profilerCan be used as an allocation profilerCan be used for heap snapshot analysis

Page 28: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Collecting Data with PerfViewCLI

PerfView run app.exe

GUI

Option Meaning

/MaxCollectSec:N Stop collection after N seconds

/StartOnPerfCounter/StopOnPerfCounter

Start/stop collection based on performance

counter

/Providers=…/OnlyProviders=…

Restrict to specific set of providers

/CircularMB:NCircular logging N megabytes of newest events

Page 29: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

PerfView Collection Options

Profiling wall-clock time

Allocation profiling

File/registry accesses

CPU sampling profiling

Page 30: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Browsing Installed Providers

Page 31: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

PerfView ReportsPerfView has built-in support for CPU utilization, GC and JIT information, disk and file I/O, and a bunch of additional reports

Page 32: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

CPU StacksGrouping options Filtering options

Call stack tree

In-trace activity highlighter

Page 33: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Disk I/O Stacks

Page 34: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

.NET Allocation Stacks

Page 35: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Lab

Diagnosing Slow Performance

Page 36: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Lab

Lab 3: Diagnosing Excessive Allocs

Page 37: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Memory Analysis with PerfView

Page 38: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

.NET Memory Leak Analysis Process1. Ascertain that a memory leak is present2. Capture memory snapshots of target process

usually done by capturing dump files or using a profiler that can capture snapshots

3. Compare snapshots to understand which objects are being added and not removed

4. Determine why these objects aren’t being garbage collectedi.e., which reference paths are keeping the objects alive

Page 39: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Memory Leak Analysis with PerfViewPerfView can generate heap snapshots (smaller than a dump), analyze, and compare themCan also import dumps directly

Page 40: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Comparing SnapshotsBaseline with referencing paths

Diff between snapshots

Page 41: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Lab

Memory Leak Analysis

Page 42: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Programmatic ETW Analysis

Page 43: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Automatic ETW AnalysisThe TraceEvent library provides ETW analysis API

Understands kernel and CLR eventsSupports call stacks (incl. managed)Can start ETW sessions and/or process log files

Page 44: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Example Analysis ScenariosMonitor the system for CLR exceptions w/ stacksExceptionTraceData

Get a profiling trace and look for regressionsTraceLogSampledProfileTraceDataTraceCallStack

Page 45: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Trace Analysis Examplevar traceLog = TraceLog.OpenOrConvert("trace.etl");var process = traceLog.Processes.LastProcessWithName(...);var symbolReader = new SymbolReader(Console.Out, symPath);

foreach (var exc in process.EventsInProcess.ByEventType<ExceptionTraceData>())

{Console.WriteLine(exc.ExceptionType);Console.WriteLine(exc.ExceptionMessage);var stack = exc.CallStack();while (stack != null){

Console.WriteLine(stack.CodeAddress.Method.FullMethodName);stack = stack.Caller;

}}

Page 46: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

Trace Session Examplevar session = new TraceEventSession("ObserveGCs");session.EnableProvider(ClrTraceEventParser.ProviderGuid,

TraceEventLevel.Verbose, (ulong)ClrTraceEventParser.Keywords.GC);

// Allocation tick every 100KBvar alloc =

session.Source.Clr.Observe<GCAllocationTickTraceData>();alloc.Subscribe(ad => Console.WriteLine(ad.AllocationAmount));

var gc = session.Source.Clr.Observe<GCHeapStatsTraceData>();gc.Subscribe(cd => Console.WriteLine(cd.GenerationSize2));

session.Source.Process();

Page 47: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

SummarySemantic logging is valuable for automatically and easily analyzing large volumes of log dataETW is the present and future of performance monitoring and diagnostics in development and production environmentsPerfView is the tool of choice for .NET developers and offers advanced profiling and leak analysis capabilities

Page 48: Monitoring .NET Performance with Event Tracing for Windows · ElasticSearch, etc. EventSource The EventSource class greatly simplifies ETW providers in .NET applications class PerformanceEventsSource

© Copyright SELA Software & Education Labs Ltd. | 14-18 Baruch Hirsch St Bnei Brak, 51202 Israel | www.selagroup.com

Sasha Goldshtein blog.sashag.netCTO, Sela Group @goldshtn

Thank You!