[翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

时间:2024-01-06 22:07:32

<<返回目录

Performance Counters(性能计数器)

性能计数器是监视应用程序和系统性能的最简单的方法之一。它有几十个类别数百个计数器在,包括一些.net特有的计数器。要访问这些可以通过系统自带的 性能监控程序(perfmon.exe)来实现。

[翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

图1-2。是PerfMon的主要窗口,它显示一个小的时间段内处理器计数器。垂直线表示当前实例,默认情况下100秒钟后图形将换行。

[翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

图1-3。这是很多类别里的其中一个计数器,还显示了适用这个计数器的应用实例。

每个计数器都有一个类别和一个名称。 很多计数器还可以选择对应的进程。 例如,对于Process类别中的%Processor Time计数器,实例可以是各种进程。 一些计数器还具有元实例,例如_Total或,它实际上是所有实例上的汇总值。

后面的很多章节将详细介绍相关主题对应的计数器。几乎每个Windows子系统都有对应的性能计数器,这些计数器通常适用于每个程序。
但是,在继续之前,您应该熟悉一些基本的操作系统相关的计数器:

• 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.

以上的我翻译过一次,感觉都不通常,所以还是不翻了,懂的看前面单词就知道什么意思,不懂的百度一下也就知道了。

以下是我将在整本书中使用的一些计数器,特别是在第2章讨论垃圾回收时。 当然如果你想了解相关主题的更多信息,请查看专门介绍操作系统的书,如Windows Internals。 (见附录C中的参考书目)
以下的计数器内容涵盖了进程常用的计数器,它可以详细记录每个进程的数据:

• % 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.

根据应用还有一些有用的分类。 您可以使用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

还是那句话,我翻译过一遍发现太丑陋了就不放出来了,特别是当我翻译完下面的文字的时候。

令人惊讶的是,在互联网里没有找到关于性能计数器的详细介绍,但幸运的是,PrefMon自己有完善的记录,在PrefMon工具里的“添加计数器”对话框中,可以选中底部的“显示描述”框以突出显示计数器的详细信息。

你说你不是坑爹吗,你既然都有描述说明了,干嘛不直接开放出来,非得点勾选一个选项才出来。

PrefMon还能够设置时间定时启动性能计数器,并存储在日志*以后查看,甚至在计数器超过阈值时执行自定义操作。它不仅限于性能计数器数据的收集,还可以收集系统配置数据和ETW事件数据。

让我们启动性能监视器,设置一次数据收集器

  1. 展开“数据收集器集”树
  2. 右键单击“用户定义”
  3. 选择“新建”
  4. 选择“数据收集器集”

[翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

5.给它一个名称,选中“手动创建(高级)”,然后单击下一步

[翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

6.检查“创建数据日志”下的性能计数器框,然后单击下一步

[翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

7.单击添加以选择要包括的计数器

[翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

8.单击下一步设置要存储日志的路径,然后单击下一步直到选择结束

完成后,您可以打开收集器的属性,并设置收集计划。也可以通过右键单击作业节点并选择开始来手动运行。 这将创建一个报表(应该是文件吧),可以通过在主树视图中的“报告--用户定义”下节点来查看。

[翻译]编写高性能 .NET 代码 第一章:工具介绍 -- Performance Counters(性能计数器)

图1-7。 已保存的报告文件。 使用工具栏按钮将视图更改为收集时的计数器数据的图形。

要创建警报,请执行相同的过程,在向导中选择“性能计数器警报”选项。
使用此处描述的功能可能需要执行性能计数器所需的一切,但如果要采取控制或创建自己的计数器,请参阅第7章(性能计数器)了解详细信息。 您应该将性能计数器分析视为应用程序的所有性能工作的基准。