SlideShare une entreprise Scribd logo
1  sur  45
Debugging And
Profiling
.NET Core Apps
on Linux
Sasha Goldshtein
@goldshtn
The Plan
• This is a talk on debugging and profiling .NET Core apps on Linux—
yes, it’s pretty crazy that we got that far!
• You’ll learn:
To profile CPU activity in .NET Core apps
To visualize stack traces (e.g. of CPU samples) using flame graphs
To use Linux tracing tools with .NET Core processes
To capture .NET Core runtime events using LTTng
To generate and analyze core dumps of .NET Core apps
🎓 Disclaimer
• A lot of this stuff has changed, and is changing
• The tools described here mostly work with .NET Core 2.x, but your
mileage may vary
• Some of this relies on scripts I hacked together, and will hopefully* be
officially supported in the future
*I’ve been saying this for almost two years now
Tools And Operating Systems Supported
Linux Windows macOS
CPU sampling perf, BCC ETW Instruments, dtrace
Dynamic tracing perf, SystemTap, BCC ⛔️ dtrace
Static tracing LTTng ETW ⛔️
Dump generation core_pattern, gcore Procdump, WER kern.corefile, gcore
Dump analysis lldb Visual Studio, WinDbg lldb
This talk
⚠️ Mind The Overhead
• Any observation can change the state of the system, but some
observations are worse than others
• Diagnostic tools have overhead
• Check the docs
• Try on a test system first
• Measure degradation introduced by the tool
OVERHEAD
This traces various kernel page cache functions and maintains in-kernel counts, which are asynchronously copied
to user-space. While the rate of operations can be very high (>1G/sec) we can have up to 34% overhead, this is still
a relatively efficient way to trace these events, and so the overhead is expected to be small for normal
workloads. Measure in a test environment.
—man cachestat (from BCC)
Sampling vs. Tracing
• Sampling works by getting a snapshot or a call stack every N
occurrences of an interesting event
• For most events, implemented in the PMU using overflow counters and
interrupts
• Tracing works by getting a message or a call stack at every occurrence
of an interesting event
CPU timepid 121 pid 121 pid 408 pid 188
system timepid 121 pid 408
CPU sample
disk write
.NET Core on Linux Tracing Architecture
kernel
user
.NET Core app
CoreCLR
OS libraries
CPU
EventSource events
CLR events (LTTng)
Probes, USDT
Tracepoints (scheduler, block I/O, networking)
“Software events” (core migrations, page faults)
PMU events (clock cycles, branches, LLC misses)
The Official Story: perfcollect and PerfView
1. Download perfcollect
2. Install prerequisites: ./perfcollect install
3. Run collection: ./perfcollect collect mytrace
4. Copy the mytrace.zip file to a Windows machine 😳
5. Download PerfView 😁
6. Open the trace in PerfView 😱
perf
• perf is a Linux multi-tool for performance investigations
• Capable of both tracing and sampling
• Developed in the kernel tree, must match running kernel’s version
• Debian-based: apt install linux-tools-common
• RedHat-based: yum install perf
perf_events Architecture
UserKernel
tcp_msgsend
Page fault
LLC miss
sched_switch
perf_events
libc:malloc
mmap buffer
mmap buffer
Real-time
analysis
perf script…perf.data file
Five Things That Will Happen To You If You
Don’t Have Symbolic Debug Information
The stuff we care about
Getting Debug Information
Type Debug information source
SyS_write Kernel /proc/kallsyms
__write Native Debuginfo package
System.IO.SyncText… Managed (AOT) Crossgen or COMPlus_ZapDisable=1
System.Console.WriteLine Managed (AOT) Crossgen or COMPlus_ZapDisable=1
MyApp.Program.Foo Managed (JIT) /tmp/perf-$PID.map
MyApp.Program.Main Managed (JIT) /tmp/perf-$PID.map
ExecuteAssembly Native (CLR) dotnet symbols or source build
CorExeMain Native (CLR) dotnet symbols or source build
__libc_start_main Native Debuginfo package
🔥 Flame Graphs
• A visualization method (adjacency graph), very
useful for stack traces, invented by Brendan
Gregg
• http://www.brendangregg.com/flamegraphs.html
• Turns thousands of stack trace pages into a
single interactive graph
• Example scenarios:
• Identify CPU hotspots on the system/application
• Show stacks that perform heavy disk accesses
• Find threads that block for a long time and the stack
where they do it
Reading a Flame Graph
• Each rectangle is a function
• Y-axis: caller-callee
• X-axis: sorted stacks (not time)
• Wider frames are more common
• Supports zoom, find
• Filter with grep 😎
$ COMPlus_PerfMapEnabled=1 ./myapp
# perf record -g -p $(pidof Buggy) -F 97
# perf report
# perf script | stackcollapse-perf.pl | FlameGraph.pl > flame.svg
The Old Way And The New Way: BPF
kernel
u{,ret}probe:
malloc
perf_events perf.data
user
perf | awk | …
report
bytes # distribution
0 - 1 … |@@@@ |
1 – 2 … |@ |
2 - 4 … |@@@@@@@@ |
kernel
u{,ret}probe:
malloc
BPF program BPF map
user
control program
report
bytes # distribution
0 - 1 … |@@@@ |
1 – 2 … |@ |
2 - 4 … |@@@@@@@@ |
user
libc:malloc
user
libc:malloc
The BCC BPF Front-End
• https://github.com/iovisor/bcc
• BPF Compiler Collection (BCC) is a
BPF frontend library and a massive
collection of performance tools
• Contributors from Facebook, PLUMgrid,
Netflix, Sela
• Helps build BPF-based tools in high-
level languages
• Python, Lua, C++
• Up-and-coming competitor: bpftrace
kernel
user
BCC tool BCC tool …
BCC compiler frontend
Clang + LLVM
BCC loader library
BPF runtime
event
sources
Managed runtimes
Syscall interface
Block I/O Ethernet
Scheduler Mem
Device drivers
Filesystem TCP/IP
CPU
Applications
System libraries
profile
llcstat
hardirqs
softirqs
ttysnoop
runqlat
cpudist
offcputime
offwaketime
cpuunclaimed
memleak
oomkill
slabratetoptcptop
tcplife
tcpconnect
tcpaccept
biotop
biolatency
biosnoop
bitesize
filetop
filelife
fileslower
vfscount
vfsstat
cachestat
cachetop
mountsnoop
*fsslower
*fsdist
dcstat
dcsnoop
mdflush
execsnoop
opensnoop
killsnoop
statsnoop
syncsnoop
setuidsnoop
mysqld_qslower
bashreadline
dbslower
dbstat
mysqlsniff
memleak
sslsniff
gethostlatency
deadlock_detector
ustat
ugc
uthreads
ucalls
uflow
argdist
trace
funccount
funclatency
stackcount
# syscount -c -p $(pidof Buggy)
# opensnoop -p $(pidof Buggy)
# funccount -p $(pidof Buggy) …/libcoreclr.so:*GarbageCollect*
# stackcount -p $(pidof Buggy) …/libcoreclr.so:*GarbageCollect*
LTTng Architecture
UserKernel
SyS_write
SyS_read LLTng
kernel
module
myapp:myprobe
buffer lttng --live
babeltraceCTF file
buffer
$ COMPlus_EnableEventLog=1 ./myapp
# lttng create my-trace
# lttng enable-event --userspace --tracepoint DotNetRuntime:*
# lttng start
Plotting data from GCStats events,
original work by Aleksei Vereshchagin
at DotNext Saint Petersburg
🚮 Core Dumps
• A core dump is a memory snapshot of a running process
• Can be generated on crash or on demand
dump file
Generating Core Dumps
• /proc/sys/kernel/core_pattern configures the core file name or
application to process the crash
• ulimit -c controls maximum core file size (often 0 by default)
• gcore (part of gdb) can create a core dump on demand
• CoreCLR ships with a createdump utility
• There’s also Procdump for Linux 🙀😱
Analyzing .NET Core Dumps
$ lldb /usr/bin/dotnet -c core.1788
(lldb) bt
The stuff we care about
libsosplugin.so
(lldb) plugin load …/libsosplugin.so
(lldb) setclrpath …
DumpObj
DumpArray
DumpHeap
GCRoot
PrintException
Threads
ClrStack
EEHeap
DumpDomain
DumpAssembly
Checklist: Preparing Your Environment
export COMPlus_PerfMapEnabled=1
AOT perf map with crossgen, or COMPlus_ZapDisable=1
Debuginfo package for libcoreclr, libc
Install perf/BCC tools
export COMPlus_EnableEventLog=1
ulimit -c unlimited (or managed by system)
Install lldb-3.x
Summary
• We have learned:
To profile CPU activity in .NET Core apps
To visualize stack traces (e.g. of CPU samples) using flame graphs
To use Linux tracing tools with .NET Core processes
To capture .NET Core runtime events using LTTng
To generate and analyze core dumps of .NET Core apps
References
• perf and flame graphs
• https://perf.wiki.kernel.org/index.php/Main_Page
• http://www.brendangregg.com/perf.html
• https://github.com/brendangregg/perf-tools
• .NET Core diagnostics docs
• https://github.com/dotnet/coreclr/blob/master/Do
cumentation/project-docs/linux-performance-
tracing.md
• https://github.com/dotnet/coreclr/blob/master/Do
cumentation/building/debugging-instructions.md
• My blog posts
• http://blogs.microsoft.co.il/sasha/2017/02/26/anal
yzing-a-net-core-core-dump-on-linux/
• http://blogs.microsoft.co.il/sasha/2017/02/27/profi
ling-a-net-core-application-on-linux/
• http://blogs.microsoft.co.il/sasha/2017/03/30/traci
ng-runtime-events-in-net-core-on-linux/
• BCC tutorials
• https://github.com/iovisor/bcc/blob/master/docs/t
utorial.md
• https://github.com/iovisor/bcc/blob/master/docs/t
utorial_bcc_python_developer.md
• https://github.com/iovisor/bcc/blob/master/docs/r
eference_guide.md
Thank You!
@goldshtn

Contenu connexe

Plus de NETFest

.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixtureNETFest
 
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
.NET Fest 2019. Анатолий Колесник. Love, Death & F# TestsNETFest
 
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос....NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...NETFest
 
.NET Fest 2019. Roberto Freato. Azure App Service deep dive
.NET Fest 2019. Roberto Freato. Azure App Service deep dive.NET Fest 2019. Roberto Freato. Azure App Service deep dive
.NET Fest 2019. Roberto Freato. Azure App Service deep diveNETFest
 
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
.NET Fest 2019. Леонид Молотиевский. DotNet Core in productionNETFest
 
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com....NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com...NETFest
 
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real....NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...NETFest
 
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystemNETFest
 
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ....NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...NETFest
 
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali....NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...NETFest
 
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NETNETFest
 
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur....NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...NETFest
 
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith...
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith....NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith...
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith...NETFest
 
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPINETFest
 
.NET Fest 2019. Kevin Dockx. OpenID Connect In Depth
.NET Fest 2019. Kevin Dockx. OpenID Connect In Depth.NET Fest 2019. Kevin Dockx. OpenID Connect In Depth
.NET Fest 2019. Kevin Dockx. OpenID Connect In DepthNETFest
 
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре...
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре....NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре...
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре...NETFest
 
.NET Fest 2019. Irina Scurtu. Forget about HTTP
.NET Fest 2019. Irina Scurtu. Forget about HTTP.NET Fest 2019. Irina Scurtu. Forget about HTTP
.NET Fest 2019. Irina Scurtu. Forget about HTTPNETFest
 
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance FuckupsNETFest
 
.NET Fest 2019. Андрей Винда. Создание REST API с поддержкой высокой нагрузки
.NET Fest 2019. Андрей Винда. Создание REST API с поддержкой высокой нагрузки.NET Fest 2019. Андрей Винда. Создание REST API с поддержкой высокой нагрузки
.NET Fest 2019. Андрей Винда. Создание REST API с поддержкой высокой нагрузкиNETFest
 
.NET Fest 2019. Arnon Axelrod. Test automation for developers
.NET Fest 2019. Arnon Axelrod. Test automation for developers.NET Fest 2019. Arnon Axelrod. Test automation for developers
.NET Fest 2019. Arnon Axelrod. Test automation for developersNETFest
 

Plus de NETFest (20)

.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
.NET Fest 2019. Андрей Литвинов. Async lifetime tests with xUnit and AutoFixture
 
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
.NET Fest 2019. Анатолий Колесник. Love, Death & F# Tests
 
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос....NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
.NET Fest 2019. Алексей Голуб. Монадные парсер-комбинаторы в C# (простой спос...
 
.NET Fest 2019. Roberto Freato. Azure App Service deep dive
.NET Fest 2019. Roberto Freato. Azure App Service deep dive.NET Fest 2019. Roberto Freato. Azure App Service deep dive
.NET Fest 2019. Roberto Freato. Azure App Service deep dive
 
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
.NET Fest 2019. Леонид Молотиевский. DotNet Core in production
 
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com....NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
.NET Fest 2019. Александр Демчук. How to measure relationships within the Com...
 
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real....NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
.NET Fest 2019. Anna Melashkina та Philipp Bauknecht. Dragons in a Mixed Real...
 
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
.NET Fest 2019. Alex Thissen. Architecting .NET solutions in a Docker ecosystem
 
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ....NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
.NET Fest 2019. Stas Lebedenko. Practical serverless use cases in Azure with ...
 
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali....NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
.NET Fest 2019. Сергей Медведев. How serverless makes Integration TDD a reali...
 
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
.NET Fest 2019. Сергей Корж. Natural Language Processing in .NET
 
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur....NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...
.NET Fest 2019. Eran Stiller. Create Your Own Serverless PKI with .NET & Azur...
 
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith...
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith....NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith...
.NET Fest 2019. Eran Stiller. 6 Lessons I Learned on My Journey from Monolith...
 
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI
.NET Fest 2019. Kevin Dockx. Uncovering Swagger/OpenAPI
 
.NET Fest 2019. Kevin Dockx. OpenID Connect In Depth
.NET Fest 2019. Kevin Dockx. OpenID Connect In Depth.NET Fest 2019. Kevin Dockx. OpenID Connect In Depth
.NET Fest 2019. Kevin Dockx. OpenID Connect In Depth
 
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре...
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре....NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре...
.NET Fest 2019. Андрей Антиликаторов. Проектирование и разработка Big Data ре...
 
.NET Fest 2019. Irina Scurtu. Forget about HTTP
.NET Fest 2019. Irina Scurtu. Forget about HTTP.NET Fest 2019. Irina Scurtu. Forget about HTTP
.NET Fest 2019. Irina Scurtu. Forget about HTTP
 
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups
 
.NET Fest 2019. Андрей Винда. Создание REST API с поддержкой высокой нагрузки
.NET Fest 2019. Андрей Винда. Создание REST API с поддержкой высокой нагрузки.NET Fest 2019. Андрей Винда. Создание REST API с поддержкой высокой нагрузки
.NET Fest 2019. Андрей Винда. Создание REST API с поддержкой высокой нагрузки
 
.NET Fest 2019. Arnon Axelrod. Test automation for developers
.NET Fest 2019. Arnon Axelrod. Test automation for developers.NET Fest 2019. Arnon Axelrod. Test automation for developers
.NET Fest 2019. Arnon Axelrod. Test automation for developers
 

Dernier

ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxDhatriParmar
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxkarenfajardo43
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataBabyAnnMotar
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1GloryAnnCastre1
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptxmary850239
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 

Dernier (20)

ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptxMan or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
Man or Manufactured_ Redefining Humanity Through Biopunk Narratives.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptxGrade Three -ELLNA-REVIEWER-ENGLISH.pptx
Grade Three -ELLNA-REVIEWER-ENGLISH.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Measures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped dataMeasures of Position DECILES for ungrouped data
Measures of Position DECILES for ungrouped data
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1Reading and Writing Skills 11 quarter 4 melc 1
Reading and Writing Skills 11 quarter 4 melc 1
 
Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"Mattingly "AI & Prompt Design: Large Language Models"
Mattingly "AI & Prompt Design: Large Language Models"
 
4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx4.11.24 Mass Incarceration and the New Jim Crow.pptx
4.11.24 Mass Incarceration and the New Jim Crow.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 

.NET Fest 2018. Sasha Goldshtein. Profiling and Debugging .NET Core Apps on Linux

  • 1. Debugging And Profiling .NET Core Apps on Linux Sasha Goldshtein @goldshtn
  • 2. The Plan • This is a talk on debugging and profiling .NET Core apps on Linux— yes, it’s pretty crazy that we got that far! • You’ll learn: To profile CPU activity in .NET Core apps To visualize stack traces (e.g. of CPU samples) using flame graphs To use Linux tracing tools with .NET Core processes To capture .NET Core runtime events using LTTng To generate and analyze core dumps of .NET Core apps
  • 3. 🎓 Disclaimer • A lot of this stuff has changed, and is changing • The tools described here mostly work with .NET Core 2.x, but your mileage may vary • Some of this relies on scripts I hacked together, and will hopefully* be officially supported in the future *I’ve been saying this for almost two years now
  • 4. Tools And Operating Systems Supported Linux Windows macOS CPU sampling perf, BCC ETW Instruments, dtrace Dynamic tracing perf, SystemTap, BCC ⛔️ dtrace Static tracing LTTng ETW ⛔️ Dump generation core_pattern, gcore Procdump, WER kern.corefile, gcore Dump analysis lldb Visual Studio, WinDbg lldb This talk
  • 5. ⚠️ Mind The Overhead • Any observation can change the state of the system, but some observations are worse than others • Diagnostic tools have overhead • Check the docs • Try on a test system first • Measure degradation introduced by the tool OVERHEAD This traces various kernel page cache functions and maintains in-kernel counts, which are asynchronously copied to user-space. While the rate of operations can be very high (>1G/sec) we can have up to 34% overhead, this is still a relatively efficient way to trace these events, and so the overhead is expected to be small for normal workloads. Measure in a test environment. —man cachestat (from BCC)
  • 6. Sampling vs. Tracing • Sampling works by getting a snapshot or a call stack every N occurrences of an interesting event • For most events, implemented in the PMU using overflow counters and interrupts • Tracing works by getting a message or a call stack at every occurrence of an interesting event CPU timepid 121 pid 121 pid 408 pid 188 system timepid 121 pid 408 CPU sample disk write
  • 7. .NET Core on Linux Tracing Architecture kernel user .NET Core app CoreCLR OS libraries CPU EventSource events CLR events (LTTng) Probes, USDT Tracepoints (scheduler, block I/O, networking) “Software events” (core migrations, page faults) PMU events (clock cycles, branches, LLC misses)
  • 8. The Official Story: perfcollect and PerfView 1. Download perfcollect 2. Install prerequisites: ./perfcollect install 3. Run collection: ./perfcollect collect mytrace 4. Copy the mytrace.zip file to a Windows machine 😳 5. Download PerfView 😁 6. Open the trace in PerfView 😱
  • 9. perf • perf is a Linux multi-tool for performance investigations • Capable of both tracing and sampling • Developed in the kernel tree, must match running kernel’s version • Debian-based: apt install linux-tools-common • RedHat-based: yum install perf
  • 10. perf_events Architecture UserKernel tcp_msgsend Page fault LLC miss sched_switch perf_events libc:malloc mmap buffer mmap buffer Real-time analysis perf script…perf.data file
  • 11. Five Things That Will Happen To You If You Don’t Have Symbolic Debug Information The stuff we care about
  • 12. Getting Debug Information Type Debug information source SyS_write Kernel /proc/kallsyms __write Native Debuginfo package System.IO.SyncText… Managed (AOT) Crossgen or COMPlus_ZapDisable=1 System.Console.WriteLine Managed (AOT) Crossgen or COMPlus_ZapDisable=1 MyApp.Program.Foo Managed (JIT) /tmp/perf-$PID.map MyApp.Program.Main Managed (JIT) /tmp/perf-$PID.map ExecuteAssembly Native (CLR) dotnet symbols or source build CorExeMain Native (CLR) dotnet symbols or source build __libc_start_main Native Debuginfo package
  • 13. 🔥 Flame Graphs • A visualization method (adjacency graph), very useful for stack traces, invented by Brendan Gregg • http://www.brendangregg.com/flamegraphs.html • Turns thousands of stack trace pages into a single interactive graph • Example scenarios: • Identify CPU hotspots on the system/application • Show stacks that perform heavy disk accesses • Find threads that block for a long time and the stack where they do it
  • 14. Reading a Flame Graph • Each rectangle is a function • Y-axis: caller-callee • X-axis: sorted stacks (not time) • Wider frames are more common • Supports zoom, find • Filter with grep 😎
  • 15. $ COMPlus_PerfMapEnabled=1 ./myapp # perf record -g -p $(pidof Buggy) -F 97
  • 17. # perf script | stackcollapse-perf.pl | FlameGraph.pl > flame.svg
  • 18.
  • 19. The Old Way And The New Way: BPF kernel u{,ret}probe: malloc perf_events perf.data user perf | awk | … report bytes # distribution 0 - 1 … |@@@@ | 1 – 2 … |@ | 2 - 4 … |@@@@@@@@ | kernel u{,ret}probe: malloc BPF program BPF map user control program report bytes # distribution 0 - 1 … |@@@@ | 1 – 2 … |@ | 2 - 4 … |@@@@@@@@ | user libc:malloc user libc:malloc
  • 20. The BCC BPF Front-End • https://github.com/iovisor/bcc • BPF Compiler Collection (BCC) is a BPF frontend library and a massive collection of performance tools • Contributors from Facebook, PLUMgrid, Netflix, Sela • Helps build BPF-based tools in high- level languages • Python, Lua, C++ • Up-and-coming competitor: bpftrace kernel user BCC tool BCC tool … BCC compiler frontend Clang + LLVM BCC loader library BPF runtime event sources
  • 21. Managed runtimes Syscall interface Block I/O Ethernet Scheduler Mem Device drivers Filesystem TCP/IP CPU Applications System libraries profile llcstat hardirqs softirqs ttysnoop runqlat cpudist offcputime offwaketime cpuunclaimed memleak oomkill slabratetoptcptop tcplife tcpconnect tcpaccept biotop biolatency biosnoop bitesize filetop filelife fileslower vfscount vfsstat cachestat cachetop mountsnoop *fsslower *fsdist dcstat dcsnoop mdflush execsnoop opensnoop killsnoop statsnoop syncsnoop setuidsnoop mysqld_qslower bashreadline dbslower dbstat mysqlsniff memleak sslsniff gethostlatency deadlock_detector ustat ugc uthreads ucalls uflow argdist trace funccount funclatency stackcount
  • 22. # syscount -c -p $(pidof Buggy) # opensnoop -p $(pidof Buggy)
  • 23. # funccount -p $(pidof Buggy) …/libcoreclr.so:*GarbageCollect*
  • 24. # stackcount -p $(pidof Buggy) …/libcoreclr.so:*GarbageCollect*
  • 26. $ COMPlus_EnableEventLog=1 ./myapp # lttng create my-trace # lttng enable-event --userspace --tracepoint DotNetRuntime:* # lttng start
  • 27.
  • 28.
  • 29. Plotting data from GCStats events, original work by Aleksei Vereshchagin at DotNext Saint Petersburg
  • 30. 🚮 Core Dumps • A core dump is a memory snapshot of a running process • Can be generated on crash or on demand dump file
  • 31. Generating Core Dumps • /proc/sys/kernel/core_pattern configures the core file name or application to process the crash • ulimit -c controls maximum core file size (often 0 by default) • gcore (part of gdb) can create a core dump on demand • CoreCLR ships with a createdump utility • There’s also Procdump for Linux 🙀😱
  • 32. Analyzing .NET Core Dumps $ lldb /usr/bin/dotnet -c core.1788 (lldb) bt The stuff we care about
  • 33. libsosplugin.so (lldb) plugin load …/libsosplugin.so (lldb) setclrpath … DumpObj DumpArray DumpHeap GCRoot PrintException Threads ClrStack EEHeap DumpDomain DumpAssembly
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42. Checklist: Preparing Your Environment export COMPlus_PerfMapEnabled=1 AOT perf map with crossgen, or COMPlus_ZapDisable=1 Debuginfo package for libcoreclr, libc Install perf/BCC tools export COMPlus_EnableEventLog=1 ulimit -c unlimited (or managed by system) Install lldb-3.x
  • 43. Summary • We have learned: To profile CPU activity in .NET Core apps To visualize stack traces (e.g. of CPU samples) using flame graphs To use Linux tracing tools with .NET Core processes To capture .NET Core runtime events using LTTng To generate and analyze core dumps of .NET Core apps
  • 44. References • perf and flame graphs • https://perf.wiki.kernel.org/index.php/Main_Page • http://www.brendangregg.com/perf.html • https://github.com/brendangregg/perf-tools • .NET Core diagnostics docs • https://github.com/dotnet/coreclr/blob/master/Do cumentation/project-docs/linux-performance- tracing.md • https://github.com/dotnet/coreclr/blob/master/Do cumentation/building/debugging-instructions.md • My blog posts • http://blogs.microsoft.co.il/sasha/2017/02/26/anal yzing-a-net-core-core-dump-on-linux/ • http://blogs.microsoft.co.il/sasha/2017/02/27/profi ling-a-net-core-application-on-linux/ • http://blogs.microsoft.co.il/sasha/2017/03/30/traci ng-runtime-events-in-net-core-on-linux/ • BCC tutorials • https://github.com/iovisor/bcc/blob/master/docs/t utorial.md • https://github.com/iovisor/bcc/blob/master/docs/t utorial_bcc_python_developer.md • https://github.com/iovisor/bcc/blob/master/docs/r eference_guide.md