內容預告:html
- Windows內置工具(性能計數器)
- 事件跟蹤器(WPT,PerfMoniter,PerfView,自定義ETW)
- 時間分析
- 內存分配分析
- 內存使用量分析
- 其餘分析
Event Tracing for Windows(ETW)能夠查看不少內核和CLR的性能數據,以下表所示,有幾個工具都是基於ETW開發的,後面會詳細介紹:數據庫
Kernel | PROC_THREAD | Creation and destruction of processes and threads | |
Kernel | LOADER | Load and unload of images (DLLs, drivers, EXEs) | |
Kernel | SYSCALL | System calls | |
Kernel | DISK_IO | Disk I/O reads and writes (including head location) | |
Kernel | HARD_FAULTS | Page faults that resulted in disk I/O (not satisfied from memory) | |
Kernel | PROFILE | Sampling event—a stack trace of all processors collected every 1ms | |
CLR | GCKeyword | Garbage collection statistics and information | Collection started, collection ended, finalizers run, ~100KB of memory have been allocated |
CLR | ContentionKeyword | Threads contend for a managed lock | Contention starts (a thread begins waiting), contention ends |
CLR | JITTracingKeyword | Just in time compiler (JIT) information | Method inlining succeeded, method inlining failed |
CLR | ExceptionKeyword | Exceptions that are thrown |
Windows Performance Toolkit (WPT)是ETW的工具集,捕獲ETW事件到日誌文件。能夠在http://msdn.microsoft.com/en-us/performance/cc752957.aspx 下載。windows
使用步驟以下:緩存
- 在環境變量裏將_NT_SYMBOL_PATH的值設置到微軟的公開符號服務器和本地符號緩存,如C:\Temp\Symbols*http://msdl.microsoft.com/download/symbols
- 在環境變量裏_NT_SYMCACHE_PATH的值設置到一個自定義目錄。
- 打開管理員權限的cmd窗口定位到安裝文件夾(如C:\Program Files\Windows Kits\8.0\Windows Performance Toolkit)。
- 運行xperf -on Base開始跟蹤內核數據
- 隨便運行一些程序
- 中止跟蹤,輸出日誌。xperf -d KernelTrace.etl
- 打開圖形分析器 xperfview KernelTrace.etl
WPT的運用場景是:性能優化
- 捕獲硬盤IO操做
- 提供全部CPU的活動
- 顯示IO,內存,CPU等的疊加圖
- 顯示調用棧
最新的Windows SDK8.0包含一些新工具,叫作Windows Performance Recorder (wpr.exe) 和Windows Performance Analyzer (wpa.exe), 是爲了替代XPerf 和XPerfView的。wpr -start和xperf -on相同,wpr -stop和xperf -d 同樣。WPA分析UI和XPerfView的功能同樣,更多信息能夠參考 http://msdn.microsoft.com/en-us/library/hh162962.aspx.服務器
XPerfView能夠看GC事件的數據:session
PerfMonitor是一個開源的控制檯工具 http://bcl.codeplex.com/releases/view/49601.併發
PerfMonitor的優勢是能夠更細節地分析CLR和JIT。框架
使用方法以下:ide
C:\PerfMonitor > perfmonitor runAnalyze JackCompiler.exe Starting kernel tracing. Output file: PerfMonitorOutput.kernel.etl Starting user model tracing. Output file: PerfMonitorOutput.etl Starting at 4/7/2012 12:33:40 PM Current Directory C:\PerfMonitor Executing: JackCompiler.exe { } Stopping at 4/7/2012 12:33:42 PM = 1.724 sec Stopping tracing for sessions 'NT Kernel Logger' and 'PerfMonitorSession'. Analyzing data in C:\PerfMonitor\PerfMonitorOutput.etlx GC Time HTML Report in C:\PerfMonitor\PerfMonitorOutput.GCTime.html JIT Time HTML Report in C:\PerfMonitor\PerfMonitorOutput.jitTime.html Filtering to process JackCompiler (1372). Started at 1372.000 msec. Filtering to Time region [0.000, 1391.346] msec CPU Time HTML report in C:\PerfMonitor\PerfMonitorOutput.cpuTime.html Filtering to process JackCompiler (1372). Started at 1372.000 msec. Perf Analysis HTML report in C:\PerfMonitor\PerfMonitorOutput.analyze.html PerfMonitor processing time: 7.172 secs.
上面的統計結果包括:
- CPU統計,CPU利用率。
- GC統計,GC時間,最大GC堆是4.5MB,內存分配速率最高是1496.1MB/秒,平均GC暫停時間是0.1MS。
- JIT編譯統計,159個函數在運行時被JIT編譯,共30493個機器字節。
下圖代表CPU作的最多的工做是這3個函數:System.String.Concat, JackCompiler.Tokenizer.Advance, 和System.Linq.Enumerable.Contains
84.2%的CPU時間花在了:JackCompiler.Parser.Parse, 調用了ParseClass, ParseSubDecls, ParseSubDecl, ParseSubBody
下圖是一些GC事件的細節統計:
PerfView:一個免費工具,能夠分析堆的使用。在http://www.microsoft.com/download/en/details.aspx?id=28567 下載。
上面的報告包括了:
- 原始的ETW事件列表。
- CPU在棧上使用時間的分組。
- 鏡象加載,硬盤IO,GC使用棧的狀況。
- GC統計。
PerfView還能夠生成堆的快照。
自定義 ETW Providers:也能夠本身開發基於ETW的性能數據統計工具。.NET4.5以前輸出ETW數據至關困難,.NET4.5要容易不少了,繼承System.Diagnostics.Tracing.EventSource類而後調用 WriteEvent函數就能夠輸出了。
public class CustomEventSource : EventSource { public class Keywords { public const EventKeywords Loop = (EventKeywords)1; public const EventKeywords Method = (EventKeywords)2; } [Event(1, Level = EventLevel.Verbose, Keywords = Keywords.Loop, Message = "Loop {0} iteration {1}")] public void LoopIteration(string loopTitle, int iteration) { WriteEvent(1, loopTitle, iteration); } [Event(2, Level = EventLevel.Informational, Keywords = Keywords.Loop, Message = "Loop {0} done")] public void LoopDone(string loopTitle) { WriteEvent(2, loopTitle); } [Event(3, Level = EventLevel.Informational, Keywords = Keywords.Method, Message = "Method {0} done")] public void MethodDone([CallerMemberName] string methodName = null) { WriteEvent(3, methodName); } } class Program { static void Main(string[] args) { CustomEventSource log = new CustomEventSource(); for (int i = 0; i < 10; ++i) { Thread.Sleep(50); log.LoopIteration("MainLoop", i); } log.LoopDone("MainLoop"); Thread.Sleep(100); log.MethodDone(); } }
PerfMonitor工具能夠自動從程序得到ETW的事件數據:
C:\PerfMonitor > perfmonitor monitorDump Ch02.exe Starting kernel tracing. Output file: PerfMonitorOutput.kernel.etl Starting user model tracing. Output file: PerfMonitorOutput.etl Found Provider CustomEventSource Guid ff6a40d2-5116-5555-675b-4468e821162e Enabling provider ff6a40d2-5116-5555-675b-4468e821162e level: Verbose keywords: 0xffffffffffffffff Starting at 4/7/2012 1:44:00 PM Current Directory C:\PerfMonitor Executing: Ch02.exe { } Stopping at 4/7/2012 1:44:01 PM = 0.693 sec Stopping tracing for sessions 'NT Kernel Logger' and 'PerfMonitorSession'. Converting C:\PerfMonitor\PerfMonitorOutput.etlx to an XML file. Output in C:\PerfMonitor\PerfMonitorOutput.dump.xml PerfMonitor processing time: 1.886 secs.
其實還有一個東西叫作Windows Management Instrumentation (WMI).這裏沒有提到,它能夠獲取到系統狀態,BIOS固件,等數據。文檔見 http://msdn.microsoft.com/en-us/library/windows/desktop/aa394582.aspx
.NET 性能測試工具 -- 事件跟蹤器(ETW)
Posted on 2012-12-09 22:03 淡如水wp 閱讀(3620) 評論(0) 編輯 收藏內容預告:
- Windows內置工具(性能計數器)
- 事件跟蹤器(WPT,PerfMoniter,PerfView,自定義ETW)
- 時間分析
- 內存分配分析
- 內存使用量分析
- 其餘分析
Event Tracing for Windows(ETW)能夠查看不少內核和CLR的性能數據,以下表所示,有幾個工具都是基於ETW開發的,後面會詳細介紹:
Kernel | PROC_THREAD | Creation and destruction of processes and threads | |
Kernel | LOADER | Load and unload of images (DLLs, drivers, EXEs) | |
Kernel | SYSCALL | System calls | |
Kernel | DISK_IO | Disk I/O reads and writes (including head location) | |
Kernel | HARD_FAULTS | Page faults that resulted in disk I/O (not satisfied from memory) | |
Kernel | PROFILE | Sampling event—a stack trace of all processors collected every 1ms | |
CLR | GCKeyword | Garbage collection statistics and information | Collection started, collection ended, finalizers run, ~100KB of memory have been allocated |
CLR | ContentionKeyword | Threads contend for a managed lock | Contention starts (a thread begins waiting), contention ends |
CLR | JITTracingKeyword | Just in time compiler (JIT) information | Method inlining succeeded, method inlining failed |
CLR | ExceptionKeyword | Exceptions that are thrown |
Windows Performance Toolkit (WPT)是ETW的工具集,捕獲ETW事件到日誌文件。能夠在http://msdn.microsoft.com/en-us/performance/cc752957.aspx 下載。
使用步驟以下:
- 在環境變量裏將_NT_SYMBOL_PATH的值設置到微軟的公開符號服務器和本地符號緩存,如C:\Temp\Symbols*http://msdl.microsoft.com/download/symbols
- 在環境變量裏_NT_SYMCACHE_PATH的值設置到一個自定義目錄。
- 打開管理員權限的cmd窗口定位到安裝文件夾(如C:\Program Files\Windows Kits\8.0\Windows Performance Toolkit)。
- 運行xperf -on Base開始跟蹤內核數據
- 隨便運行一些程序
- 中止跟蹤,輸出日誌。xperf -d KernelTrace.etl
- 打開圖形分析器 xperfview KernelTrace.etl
WPT的運用場景是:
- 捕獲硬盤IO操做
- 提供全部CPU的活動
- 顯示IO,內存,CPU等的疊加圖
- 顯示調用棧
最新的Windows SDK8.0包含一些新工具,叫作Windows Performance Recorder (wpr.exe) 和Windows Performance Analyzer (wpa.exe), 是爲了替代XPerf 和XPerfView的。wpr -start和xperf -on相同,wpr -stop和xperf -d 同樣。WPA分析UI和XPerfView的功能同樣,更多信息能夠參考 http://msdn.microsoft.com/en-us/library/hh162962.aspx.
XPerfView能夠看GC事件的數據:
PerfMonitor是一個開源的控制檯工具 http://bcl.codeplex.com/releases/view/49601.
PerfMonitor的優勢是能夠更細節地分析CLR和JIT。
使用方法以下:
C:\PerfMonitor > perfmonitor runAnalyze JackCompiler.exe Starting kernel tracing. Output file: PerfMonitorOutput.kernel.etl Starting user model tracing. Output file: PerfMonitorOutput.etl Starting at 4/7/2012 12:33:40 PM Current Directory C:\PerfMonitor Executing: JackCompiler.exe { } Stopping at 4/7/2012 12:33:42 PM = 1.724 sec Stopping tracing for sessions 'NT Kernel Logger' and 'PerfMonitorSession'. Analyzing data in C:\PerfMonitor\PerfMonitorOutput.etlx GC Time HTML Report in C:\PerfMonitor\PerfMonitorOutput.GCTime.html JIT Time HTML Report in C:\PerfMonitor\PerfMonitorOutput.jitTime.html Filtering to process JackCompiler (1372). Started at 1372.000 msec. Filtering to Time region [0.000, 1391.346] msec CPU Time HTML report in C:\PerfMonitor\PerfMonitorOutput.cpuTime.html Filtering to process JackCompiler (1372). Started at 1372.000 msec. Perf Analysis HTML report in C:\PerfMonitor\PerfMonitorOutput.analyze.html PerfMonitor processing time: 7.172 secs.
上面的統計結果包括:
- CPU統計,CPU利用率。
- GC統計,GC時間,最大GC堆是4.5MB,內存分配速率最高是1496.1MB/秒,平均GC暫停時間是0.1MS。
- JIT編譯統計,159個函數在運行時被JIT編譯,共30493個機器字節。
下圖代表CPU作的最多的工做是這3個函數:System.String.Concat, JackCompiler.Tokenizer.Advance, 和System.Linq.Enumerable.Contains
84.2%的CPU時間花在了:JackCompiler.Parser.Parse, 調用了ParseClass, ParseSubDecls, ParseSubDecl, ParseSubBody
下圖是一些GC事件的細節統計:
PerfView:一個免費工具,能夠分析堆的使用。在http://www.microsoft.com/download/en/details.aspx?id=28567 下載。
上面的報告包括了:
- 原始的ETW事件列表。
- CPU在棧上使用時間的分組。
- 鏡象加載,硬盤IO,GC使用棧的狀況。
- GC統計。
PerfView還能夠生成堆的快照。
自定義 ETW Providers:也能夠本身開發基於ETW的性能數據統計工具。.NET4.5以前輸出ETW數據至關困難,.NET4.5要容易不少了,繼承System.Diagnostics.Tracing.EventSource類而後調用 WriteEvent函數就能夠輸出了。
public class CustomEventSource : EventSource { public class Keywords { public const EventKeywords Loop = (EventKeywords)1; public const EventKeywords Method = (EventKeywords)2; } [Event(1, Level = EventLevel.Verbose, Keywords = Keywords.Loop, Message = "Loop {0} iteration {1}")] public void LoopIteration(string loopTitle, int iteration) { WriteEvent(1, loopTitle, iteration); } [Event(2, Level = EventLevel.Informational, Keywords = Keywords.Loop, Message = "Loop {0} done")] public void LoopDone(string loopTitle) { WriteEvent(2, loopTitle); } [Event(3, Level = EventLevel.Informational, Keywords = Keywords.Method, Message = "Method {0} done")] public void MethodDone([CallerMemberName] string methodName = null) { WriteEvent(3, methodName); } } class Program { static void Main(string[] args) { CustomEventSource log = new CustomEventSource(); for (int i = 0; i < 10; ++i) { Thread.Sleep(50); log.LoopIteration("MainLoop", i); } log.LoopDone("MainLoop"); Thread.Sleep(100); log.MethodDone(); } }
PerfMonitor工具能夠自動從程序得到ETW的事件數據:
C:\PerfMonitor > perfmonitor monitorDump Ch02.exe Starting kernel tracing. Output file: PerfMonitorOutput.kernel.etl Starting user model tracing. Output file: PerfMonitorOutput.etl Found Provider CustomEventSource Guid ff6a40d2-5116-5555-675b-4468e821162e Enabling provider ff6a40d2-5116-5555-675b-4468e821162e level: Verbose keywords: 0xffffffffffffffff Starting at 4/7/2012 1:44:00 PM Current Directory C:\PerfMonitor Executing: Ch02.exe { } Stopping at 4/7/2012 1:44:01 PM = 0.693 sec Stopping tracing for sessions 'NT Kernel Logger' and 'PerfMonitorSession'. Converting C:\PerfMonitor\PerfMonitorOutput.etlx to an XML file. Output in C:\PerfMonitor\PerfMonitorOutput.dump.xml PerfMonitor processing time: 1.886 secs.
其實還有一個東西叫作Windows Management Instrumentation (WMI).這裏沒有提到,它能夠獲取到系統狀態,BIOS固件,等數據。文檔見 http://msdn.microsoft.com/en-us/library/windows/desktop/aa394582.aspx
關於託管調試和性能優化的工具
2014-11-13 22:34 by 周信達, 699 閱讀, 0 評論, 收藏, 編輯
最近一個禮拜,一直在看一些調試和性能相關的東西,由於公司的產品主要瓶頸就在這些地方,因而以爲頗有必要去了解一下這方面的東西。插個小話題,話說今天.NET官方公佈將整個.NET框架開源,這實在是一個重磅消息,幾家歡喜幾家愁,各路歡笑各路口水。無論如何,本身做爲一個陪伴着.NET多年的開發者,仍是對它有感情的(好吧有點矯情),不過話說回來,我不知道微軟原來閉源是有何種商業目的和戰略計劃,可是,微軟的.NET在揹負着不叫好的口水中,在商業上的挫敗中,即使如此都是一如既往地投入大量精力在.NET的建設上,包括從半開源一直走到今天的開源,無論怎樣,這個態度仍是能夠的,雖然它來得有點太晚了。何況,.NET程序是到了該改變思惟的時候了,額話說的有點多,上正題吧
調試工具篇
- Windows調試工具集(Debugging Tools for Windows)
- 工具集中包含Windbg、NTSD、CDB
- http://msdn.microsoft.com/en-us/windows/hardware/hh852365
- 單獨下載地 Debuggers (x86) Debuggers (x64)
- Debugging Tools for Windows MSDN Help
- 功能:主要針對非託管程序的調試
- http://www.cnblogs.com/awpatp/archive/2010/05/30/1747736.html
- Windows調試器擴展(Debugger Extension)
- SOS(.NET版本自帶,能夠集成到VS中使用)
- SOSEX for .NET
- PssCor4
- CLR Profiler(主要對內存分配狀況進行監視和分析)
- PowerDbg
- MDA(Manage Debugger Assistant)
- MDbg.exe(.NET Framework 命令行調試程序)
- .NET Memory Profiler
性能工具篇
- 性能計數器(Performance Counter)
- 系統自帶
- CLR Profiler(主要對內存分配狀況進行監視和分析)
- Visual Studio自帶的性能分析器
- JetBrains dotTrace
- ANTS Performance Profiler
- .NET Memory Profiler
- Event Trace for Windows(ETW)
- Windows Performance Toolkit(WPT)
- PerfMonitor
- PerfView
- Visual Studio Sampling Profiler
- Visual Studio Allocation Profiler
- ANTS Memory Profiler
- SciTech .NET Memory Profiler
- Concurrency Visualizer
- Concurrency Profiler
- I/O Profilers
- BCL PerfMonitor
數據庫工具篇
- data access profilers
- database profilers
- SQL Server Profiler
- RedGate ANTS Performance Profiler
- Visual Studio 「Tier Interactions」 profiling feature
- LINQ to SQL Profiler
- Entity Framework Profiler
- NHibernate Profiler
附:
引用
《Pro .NET Performance Optimize Your C# Application》
《Advanced .NET Debugging》
《Windows高級調試》
結語
調試和性能是混爲一體的,一般咱們都要深刻到程序的內部,尤爲是當工程規模較大的時候,每每咱們經過簡單的Code Review或者簡易的Debug難以定位問題,此時咱們就要藉助於一些工具了,所謂工欲善其事必先利其器。一般咱們診斷時基本上關注與類型使用、異常處理、垃圾回收、線程處理、併發處理等等。其實這裏面列出的大部分工具我本人都沒有使用過,在實踐中使用過一部分在平時學習的時候也研究過一小部分,列出來主要是做個記錄,方便之後能夠翻出來再使用和評測