/
index.md
211 lines (163 loc) · 10.7 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
---
title: Windows Performance Counter Types
date: 2013-11-20
topics:
- windows
- dotnet
aliases:
- /2013/11/20/windows-performance-counter-types/
- /2013/11/windows-performance-counter-types/
---
There are several types of performance counters in Windows. However, I had a hard time of understanding all these types just from their documentation. So I decided to compile some examples for each counter type.
I also wrote some C# code to demonstrate how to use performance counters. You'll find it at the end of this article.
<!--more-->
**Notes:**
* You can watch performance counters with the **Performance Monitor** (`perfmon`) that comes with Windows. However, it can't display negative values so some counters can't be monitored properly.
* I use the .NET names for performance counter types in this article.
* Categorization of the counter types follows the [official documentation](http://msdn.microsoft.com/EN-US/library/4bcx21aa.aspx).
## Terminology
Performance counter
: A counter to monitor performance of a running system (e.g. CPU usage). Is basically just an `int`.
Sampling, Measurement
: Read the value of a performance counter (at a certain point in time)
Sampling rate
: How often a performance counter is sampled (e.g. every second)
Time frame
: Time between two samplings (usually one second)
Monotonicity
: Whether you can increase and/or decrease the value of a performance counter. (Some counters require you to always increase - but not decrease - their values.)
## Composite Counters
Before we begin, a word about (what I call) "composite counters".
As the name suggests, these counters are composited of two counters - a parent counter and a base counter. Parent counters can be monitored, base counters cannot.
To register (new/custom) performance counters in Windows, you need to pass a list of performance counter creation data to [PerformanceCounterCategory.Create()](http://msdn.microsoft.com/EN-US/library/440b32x1%28v=VS.110,d=hv.2%29.aspx). In this list the base counter data must come immediately after the parent counter data.
The following table lists which base counter type can be used with which parent counter type:
| Base counter type | Parent (composite) counter types
| ------------------ | --------------------------------
| `AverageBase` | `AverageTimer32`, `AverageCount64`
| `CounterMultiBase` | `CounterMultiTimer`, `CounterMultiTimerInverse`, `CounterMultiTimer100Ns`, `CounterMultiTimer100NsInverse`
| `RawBase` | `RawFraction`
| `SampleBase` | `SampleFraction`
## Instantaneous Counters
**Displays:** the most recent measurement
| |
| ------------ | -----
| Types | `NumberOfItems32`, `NumberOfItems64`, `NumberOfItemsHEX32`, `NumberOfItemsHEX64`
| Description | The simplest counter; just displays the raw number without any calculations; the `HEX` variants indicate that the values should be displayed as hexadecimal (instead of decimal)
| Example | Overall database operations
| Displays as | raw count
| Composite | no
| Monotonicity | increasing, decreasing, remain static
| |
| ------------ | -----
| Types | `RawFraction`
| Description | Used to display fractions (instead of integers like with `NumberOfItems`); usually interpreted as percentage
| Example | Percentage of maximum allowed concurrent database connections; `RawFraction`: current connection count; `RawBase`: maximum connection count
| Displays as | `(RawFraction / RawBase) * 100`
| Composite | yes (base: `RawBase`)
| Monotonicity | increasing, decreasing, remain static
## Average Counters
**Displays:** average of values during time frame
| |
| ------------ | -----
| Types | `AverageCount64`
| Description | Used to describe how many items were processed per operation in average
| Example | Database operations per transaction; for each operation, you'd increment `AverageCount64` and for each completed transaction you'd increment `AverageBase`
| Displays as | average items per operation (during the last time frame)
| Composite | yes (`AverageBase`)
| Monotonicity | increasing, ~~decreasing~~, remain static
```note
The difference to `RawFraction` is that `RawFraction` would be displayed as the average since the creation/last reset of the counter, while `AverageCount64` would be displayed as average during the last time frame (usually a second).
```
| |
| ------------ | -----
| Types | `AverageTimer32`
| Description | Used to describe how long an operation takes in average
| Example | Average time each database transaction takes; for each transaction, you'd measure the execution time of the transaction with `Stopwatch` and increment `AverageTimer32` by `Stopwatch.ElapsedTicks`; and for each completed transaction you'd increment `AverageBase` by one
| Displays as | average time per operation (during the last time frame)
| Composite | yes (`AverageBase`)
| Monotonicity | increasing, ~~decreasing~~, remain static
| |
| ------------ | -----
| Types | `CountPerTimeInterval32`, `CountPerTimeInterval64`
| Description | Usually used to describe the average count of items in a queue; if you have 0 items at the beginning, and 10 items after 5 seconds, then the rate would be displayed as 2.
| Example | Monitoring a worker queue that contains jobs to run in the background
| Displays as | average count change per time frame
| Composite | no
| Monotonicity | increasing, decreasing, remain static
```note
I found the information about `CountPerTimeInterval` somewhere on the internet. However, in my test I couldn't get it working properly. The values of this counter would display as extremely small in Performance Monitor; something like `10.5E-06` - although it should be something like `10.5`. Also Performance Monitor doesn't seem capable of displaying negative values. They are necessary however when the queue handles requests faster than new requests get placed into it.
```
| |
| ------------ | -----
| Types | `SampleCounter`
| Description | Shows the average number of operations completed in one second (according to MSDN). Seems to be identical to `RateOfCountsPerSecond` (see below).
| Example | Web requests served per second; for each served request, you would call `Increment()` on the counter
| Displays as | average operation per second (during the last time frame)
| Composite | no
| Monotonicity | increasing, ~~decreasing~~, remain static
## Rate Counters
**Displays:** rate of item count growth
| |
| ------------ | -----
| Types | `RateOfCountsPerSecond32`, `RateOfCountsPerSecond64`
| Description | Used to display how many items were - in average - processed per second during the last time frame. Seems to be identical to `SampleCounter`.
| Example | Web requests served per second; for each served request, you would call `Increment()` on the counter
| Displays as | average item per second (during the last time frame)
| Composite | no
| Monotonicity | increasing, ~~decreasing~~, remain static
## Percentage (of Time) Counters
**Displays:** calculated values as a percentage
| |
| ------------ | -----
| Types | `CounterTimer`, `CounterTimerInverse`, `Timer100Ns`, `Timer100NsInverse`
| Description | Used to display the percent of time that a component is active (`CounterTimer`) or inactive (`CounterTimerInverse`).
| Example | % of time an operation is running; you'd measure the time the operation takes to execute and then increase the counter's value by this amount
| Displays as | % of time a component was active during the last second (or other time frame)
| Composite | no
| Monotonicity | increasing, ~~decreasing~~, remain static
**Notes:**
* `CounterTimerInverse` is like `CounterTimer` but measures how long the component is inactive. So, you'd measure the time from when the component became inactive to the next time it becomes active and increase the counter's value by this amount.
* For `CounterTimer` and `CounterTimerInverse`, use `Stopwatch.ElapsedTicks` as counter value. For `Timer100Ns` and `Timer100NsInverse`, use `DateTime.Ticks` instead. (Note that these ticks have different lengths, as stated [here](http://msdn.microsoft.com/EN-US/library/2d0zc00w.aspx).)
| |
| ------------ | -----
| Types | `CounterMultiTimer`, `CounterMultiTimerInverse`, `CounterMultiTimer100Ns`, `CounterMultiTimer100NsInverse`
| Description | Work exactly as their non-multi counterparts (e.g. `CounterTimer` for `CounterMultiTimer`), except that the result is divided by `CounterMultiBase`
| Example | % of time an operation is running, if multiple calls to the operation can run in parallel; you'd measure the time the operation takes to execute and then increase the counter's value by this amount; `CounterMultiBase` would be set to the number of parallel operations
| Displays as | % of time a component was active during the last second (or other time frame)
| Composite | yes (`CounterMultiBase`)
| Monotonicity | increasing, ~~decreasing~~, remain static
| |
| ------------ | -----
| Types | `SampleFraction`
| Description | Average ratio during the last time frame in percent
| Example | Average ratio (%) of successful operations during the last time frame; you would increment `SampleBase` for every operation, and `SampleFraction` for each successful one.
| Displays as | average ratio in %
| Composite | yes (`SampleBase`)
| Monotonicity | increasing, ~~decreasing~~, remain static
## Difference Counters
**Displays:** difference between value at the beginning and the end of the time frame
| |
| ------------ | -----
| Types | `CounterDelta32`, `CounterDelta64`
| Description | Displays the difference between the raw value at the beginning and the end of the measured time frame
| Example | Anything you need to display as difference
| Displays as | difference
| Composite | no
| Monotonicity | increasing, decreasing, remain static
```note
Performance Monitor can't display negative values.
```
| |
| ------------ | -----
| Types | `ElapsedTime`
| Description | Time in seconds since ~~the counter was created~~ sometime in the past. Note that you can't set the value of this counter manually; it automatically increases.
| Example | System up time
| Displays as | seconds
| Composite | no
| Monotonicity | ~~increasing~~, ~~decreasing~~, ~~remain static~~
```note
I have no clue what start time is chosen for the counter. It's certainly not the time the counter was created. In my example, where I created the counter just a couple of seconds ago, it already had a value of about 27 hours (was even longer than my system up time).
```
## Example Code
Here's an example project that demonstrates the various performance counters:
[](PerformanceCounters.zip) (15 KB)