I've built a small Windows Service which exposes perfmon counters to track sockets in
TIME_WAIT state. It can be downloaded from the links later in this post.
Back in 2011 I was helping a client look for issues in their systems caused by having too many sockets in a
state (see here
for why this can be a problem). This was affecting their connectivity. Rather surprisingly there seemed to be no way to track the number of sockets in
using perfmon as there didn't seem to be a counter exposed. A counter would have been useful so that we could track the
; connections over time along with all of the other metrics that we track for their system. Anyway, I put together a quick and dirty tool for the client, this worked like a special version of
which totalled up the number of sockets in each state by polling the system using
At the time I suggested that it wouldn't take much to build a small service which did this polling and exposed the results as a perfmon counter so that we COULD track this metric in the usual way. Well, I've finally got around to doing that (thanks to the encouragement of another client who had a similar issue). The result is TCPStatsPerfCounters which is a service that you can install and which provides counters for all of the states in the TCP table for both IPv4 and IPv6;
CLOSE_WAIT, etc. There's an x86 and and x64 build available and the services come as a single exe which automatically deploy and install the required counter dlls when you run them with /install.
- Download the x86 version of TCPStatsPerfCounters from here.
- Download the x64 version of TCPStatsPerfCounters from here.
Note that the x64 version will install an x86 and an x64 counter dll for maximum interoperability. The x86 version only installs an x86 counter dll and so will not integrate with perfmon on an x64 box. You should only use the x86 version on x86 machines.
- Unzip. You will end up with either
TCPStatsPerfCounters64.exe. All of the rest of the instructions apply equally to either version of the program.
TCPStatsPerfCounters.exe with the
/? command line switch to see help.
TCPStatsPerfCounters.exe with the
/install command line switch to install the service. You may be prompted to elevate your credentials if you are not an admin.
- To run as a service, open the services applet and locate the "JetByte TCP Stats Perfmon Counters" service, start it and, optionally set its start-up type to automatic.
- To run
TCPStatsPerfCounters.exe as an exe rather than as a service you still need to have installed it as a service but you can run from the command line with the
/run command line switch.
- You can get
TCPStatsPerfCounters.exe to produce a log file - this may be useful if you want to track socket states outside of perfmon. Either
/run with the additional
/createLog command line switch. By default the log is created in the same directory as the exe, you can change this with the addition of the
/logPath command line switch.
- By default the program polls the system every second, you can change this by using the
/poll command line switch (again supply this either with
/run. The polling interval is specified in seconds.
There are counters for all of the socket states for both IPv4 and IPv6. Sockets transition through some of the states very quickly and so you may not see many of the states. There are also maximum counters for
TIME_WAIT which show the highest value seen. In addition there are counters for the
TcpTimedWaitDelay registry keys. I found it useful to have these values clearly visible for situations where machines happened to be configured in an unusual way and nobody had thought to check the registry. These values are set once on program start up and will be zero if the registry key is not set and the operating system's default value is in operation.