编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)
Performance Counters(性能計(jì)數(shù)器)
性能計(jì)數(shù)器是監(jiān)視應(yīng)用程序和系統(tǒng)性能的最簡(jiǎn)單的方法之一。它有幾十個(gè)類別數(shù)百個(gè)計(jì)數(shù)器在,包括一些.net特有的計(jì)數(shù)器。要訪問這些可以通過系統(tǒng)自帶的 性能監(jiān)控程序(perfmon.exe)來實(shí)現(xiàn)。
圖1-2。是PerfMon的主要窗口,它顯示一個(gè)小的時(shí)間段內(nèi)處理器計(jì)數(shù)器。垂直線表示當(dāng)前實(shí)例,默認(rèn)情況下100秒鐘后圖形將換行。
圖1-3。這是很多類別里的其中一個(gè)計(jì)數(shù)器,還顯示了適用這個(gè)計(jì)數(shù)器的應(yīng)用實(shí)例。
每個(gè)計(jì)數(shù)器都有一個(gè)類別和一個(gè)名稱。 很多計(jì)數(shù)器還可以選擇對(duì)應(yīng)的進(jìn)程。 例如,對(duì)于Process類別中的%Processor Time計(jì)數(shù)器,實(shí)例可以是各種進(jìn)程。 一些計(jì)數(shù)器還具有元實(shí)例,例如_Total或 ,它實(shí)際上是所有實(shí)例上的匯總值。
后面的很多章節(jié)將詳細(xì)介紹相關(guān)主題對(duì)應(yīng)的計(jì)數(shù)器。幾乎每個(gè)Windows子系統(tǒng)都有對(duì)應(yīng)的性能計(jì)數(shù)器,這些計(jì)數(shù)器通常適用于每個(gè)程序。
 但是,在繼續(xù)之前,您應(yīng)該熟悉一些基本的操作系統(tǒng)相關(guān)的計(jì)數(shù)器:
? Physical Memory—The actual physical memory chips in a computer. Only the operating system manages physical memory directly.
? Virtual Memory—A logical organization of memory in a given process. Virtual memory size can be larger than physical memory. For example, 32-bit programs have a 4 GB address space, even if the computer itself only has 2 GB of RAM. Windows allows the program to access only 2 GB of that by default, but all 4 GB is possible if the executable is large-address aware. (On 32-bit versions of Windows, large-address aware programs are limited to 3 GB.) As of Windows 8.1 and Server 2012, 64-bit processes have a 128 TB process space, far larger than the 4 TB physical memory limit. Some of the virtual memory may be in RAM while other parts are stored on disk in a paging file. Contiguous blocks of virtual memory may not be contiguous in physical memory. All memory addresses in a process are for the virtual memory.
? Reserved Memory—A region of virtual memory address space that has been reserved for the process and thus will not be allocated to a future requester. Reserved memory cannot be used for memory allocation requests because there is nothing backing it—it is just a description of a range of memory addresses.
 ??Committed Memory—A region of memory that has a physical backing store. This can be RAM or disk.
 ??Page—An organizational unit of memory. Blocks of memory are allocated in a page, which is usually a few KB in size.
? Paging—The process of transferring pages between regions of virtual memory. The page can move to or from another process (soft paging) or the disk (hard paging). Soft paging can be accomplished very quickly by mapping the existing memory into the current process’s virtual address space. Hard paging involves a relatively slow transfer of data to or from disk. Your program must avoid this at all costs to maintain good performance.
? Page In—Transfer a page from another location to the current process.
? Page Out—Transfer a page from the current process to another location, such as disk.
? Context Switch—The process of saving and restoring the state of a thread or process. Because there are usually more running threads than available processors, there are often many context switches per second.
? Kernel Mode—A mode that allows the OS to modify low-level aspects of the hardware’s state, such as modifying certain registers or enabling/disabling interrupts. Transitioning to Kernel Mode requires an operating system call, and can be quite expensive.
? User Mode—An unprivileged mode of executing instructions. There is no ability to modify low-level aspects of the system.
以上的我翻譯過一次,感覺都不通常,所以還是不翻了,懂的看前面單詞就知道什么意思,不懂的百度一下也就知道了。
以下是我將在整本書中使用的一些計(jì)數(shù)器,特別是在第2章討論垃圾回收時(shí)。 當(dāng)然如果你想了解相關(guān)主題的更多信息,請(qǐng)查看專門介紹操作系統(tǒng)的書,如Windows Internals。 (見附錄C中的參考書目)
 以下的計(jì)數(shù)器內(nèi)容涵蓋了進(jìn)程常用的計(jì)數(shù)器,它可以詳細(xì)記錄每個(gè)進(jìn)程的數(shù)據(jù):
? % Privileged Time—Amount of time spent in executing privileged (kernel mode) code.
? % Processor Time—Percentage of a single processor the application is using. If your application is using two logical processor cores at 100% each, then this counter will read 200.
? % User Time—Amount of time spent in executing unprivileged (user mode) code.
? IO Data Bytes/sec—How much I/O your process is doing.
? Page Faults/sec—Total number of page faults in your process. A page fault occurs when a page of memory is missing from the current working set. It is important to realize that this number includes both soft and hard page faults. Soft page faults are innocuous and can be caused by the page being in memory, but outside the current process (such as for shared DLLs). Hard page faults are more serious, indicating data that is on disk but not currently in memory. Unfortunately, you cannot track hard page faults per process with performance counters, but you can see it for the entire system with the Memory\Page Reads/sec counter. You can do some correlation with a process’s total page faults plus the system’s overall page reads (hard faults). You can definitively track a process’s hard faults with ETW tracing with the Windows Kernel/Memory/Hard Fault event.
? Pool Nonpaged Bytes—Typically operating system and driver allocated memory for data structures that cannot be paged out such as operating system objects like threads and mutexes, but also custom data structures.
? Pool Paged Bytes—Also for operating system data structures, but these are allowed to be paged out.
 Private Bytes—Committed virtual memory private to the specific process (not shared with any other processes).
? Virtual Bytes—Allocated memory in the process’s address space, some of which may be backed by the page file, shared with other processes, and memory private to the process.
? Working Set—The amount of virtual memory currently resident in physical memory (usually RAM).
? Working Set-Private—The amount of private bytes currently resident in physical memory.
? Thread Count—The number of threads in the process. This may or may not be equal to the number of .NET threads. See Chapter 4 (Asynchronous Programming) for a discussion of .NET thread-related counters.
根據(jù)應(yīng)用還有一些有用的分類。 您可以使用PerfMon來探索在這些特別的分類。
? IPv4/IPv6—Internet Protocol-related counters for datagrams and fragments.
? Memory—System-wide memory counters such as overall paging, available bytes, committed bytes, and much more.
? Objects—Data about kernel-owned objects such as events, mutexes, processes, threads, semaphores, and sections.
? Processor—Counters for each logical processor in the system.
? System—Context switches, alignment fixes, file operations, process count, threads, and more.
? TCPv4/TCPv6—Data for TCP connections and segment transfers
還是那句話,我翻譯過一遍發(fā)現(xiàn)太丑陋了就不放出來了,特別是當(dāng)我翻譯完下面的文字的時(shí)候。
令人驚訝的是,在互聯(lián)網(wǎng)里沒有找到關(guān)于性能計(jì)數(shù)器的詳細(xì)介紹,但幸運(yùn)的是,PrefMon自己有完善的記錄,在PrefMon工具里的“添加計(jì)數(shù)器”對(duì)話框中,可以選中底部的“顯示描述”框以突出顯示計(jì)數(shù)器的詳細(xì)信息。
你說你不是坑爹嗎,你既然都有描述說明了,干嘛不直接開放出來,非得點(diǎn)勾選一個(gè)選項(xiàng)才出來。
PrefMon還能夠設(shè)置時(shí)間定時(shí)啟動(dòng)性能計(jì)數(shù)器,并存儲(chǔ)在日志中供以后查看,甚至在計(jì)數(shù)器超過閾值時(shí)執(zhí)行自定義操作。它不僅限于性能計(jì)數(shù)器數(shù)據(jù)的收集,還可以收集系統(tǒng)配置數(shù)據(jù)和ETW事件數(shù)據(jù)。
讓我們啟動(dòng)性能監(jiān)視器,設(shè)置一次數(shù)據(jù)收集器
展開“數(shù)據(jù)收集器集”樹
右鍵單擊“用戶定義”
選擇“新建”
選擇“數(shù)據(jù)收集器集”
5.給它一個(gè)名稱,選中“手動(dòng)創(chuàng)建(高級(jí))”,然后單擊下一步
6.檢查“創(chuàng)建數(shù)據(jù)日志”下的性能計(jì)數(shù)器框,然后單擊下一步
7.單擊添加以選擇要包括的計(jì)數(shù)器
8.單擊下一步設(shè)置要存儲(chǔ)日志的路徑,然后單擊下一步直到選擇結(jié)束
完成后,您可以打開收集器的屬性,并設(shè)置收集計(jì)劃。也可以通過右鍵單擊作業(yè)節(jié)點(diǎn)并選擇開始來手動(dòng)運(yùn)行。 這將創(chuàng)建一個(gè)報(bào)表(應(yīng)該是文件吧),可以通過在主樹視圖中的“報(bào)告--用戶定義”下節(jié)點(diǎn)來查看。
圖1-7。 已保存的報(bào)告文件。 使用工具欄按鈕將視圖更改為收集時(shí)的計(jì)數(shù)器數(shù)據(jù)的圖形。
要?jiǎng)?chuàng)建警報(bào),請(qǐng)執(zhí)行相同的過程,在向?qū)е羞x擇“性能計(jì)數(shù)器警報(bào)”選項(xiàng)。
 使用此處描述的功能可能需要執(zhí)行性能計(jì)數(shù)器所需的一切,但如果要采取控制或創(chuàng)建自己的計(jì)數(shù)器,請(qǐng)參閱第7章(性能計(jì)數(shù)器)了解詳細(xì)信息。 您應(yīng)該將性能計(jì)數(shù)器分析視為應(yīng)用程序的所有性能工作的基準(zhǔn)。
相關(guān)文章:
- [翻譯]編寫高性能 .NET 代碼 第一章:性能測(cè)試與工具 -- 選擇什么來衡量 
- [翻譯]編寫高性能 .NET 代碼 第一章:性能測(cè)試與工具 -- 平均值 vs 百分比 
- [翻譯]編寫高性能 .NET 代碼 第一章:工具介紹 -- Visual Studio 
原文地址:http://www.cnblogs.com/yahle/p/6531297.html
.NET社區(qū)新聞,深度好文,微信中搜索dotNET跨平臺(tái)或掃描二維碼關(guān)注
總結(jié)
以上是生活随笔為你收集整理的编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: ASP.NET 开发者 开始学习ASP.
- 下一篇: EF通用数据层封装类(支持读写分离,一主
