How to Save and Reload Custom Performance Counter Sets in Windows Performance Monitor for Developers


1 views

As developers working with Windows systems, we often need to monitor specific performance metrics during debugging or performance tuning. The default Windows Performance Monitor (perfmon) requires manually adding counters each session - a tedious process when dealing with complex monitoring setups. Here's how to automate this workflow.

Windows provides a simple way to save your counter configuration as an HTML file:

# PowerShell command to export counters
Get-Counter -ListSet * | Out-File "C:\CounterSets.txt"

# Alternative method using perfmon GUI:
1. Open Performance Monitor (perfmon.msc)
2. Set up your desired counters
3. Right-click the counter list → "Save Settings As..."
4. Choose .html format

For more advanced scenarios, we can create Data Collector Sets that persist between sessions:

logman create counter MyAppPerfCounters -o "C:\perflogs\MyApp.blg" -f bin -v mmddhhmm -c "\Processor(*)\% Processor Time" "\Memory\Available MBytes" "\PhysicalDisk(*)\Disk Bytes/sec"

logman start MyAppPerfCounters
# Later...
logman stop MyAppPerfCounters

For integration with custom tools, here's a C# snippet to save and load counter sets:

using System.Diagnostics;

// Saving counters
void SaveCounterSet(string path, PerformanceCounter[] counters)
{
    using (StreamWriter sw = new StreamWriter(path))
    {
        foreach (var counter in counters)
        {
            sw.WriteLine($"{counter.CategoryName}\\{counter.CounterName}\\{counter.InstanceName}");
        }
    }
}

// Loading counters
PerformanceCounter[] LoadCounterSet(string path)
{
    var lines = File.ReadAllLines(path);
    return lines.Select(line => {
        var parts = line.Split('\\');
        return new PerformanceCounter(parts[0], parts[1], parts[2] == "" ? null : parts[2]);
    }).ToArray();
}

For production environments, create permanent Data Collector Sets that automatically start with the system:

# Create a system-wide collector set
logman create counter SystemPerf -s MyServer -f csv -o "\\logs\serverperf" -cf "C:\counters.txt" -si 00:01:00

# Install as a scheduled task
schtasks /create /tn "PerfMonitor" /tr "logman start SystemPerf" /sc onstart /ru SYSTEM

For developers needing more advanced features, consider these alternatives:

  • PAL (Performance Analysis of Logs) Tool
  • PerfView from Microsoft
  • Windows Performance Recorder (WPR)

If you're like most system administrators or performance analysts, you've probably wasted precious time manually adding the same performance counters every time you launch Windows Performance Monitor (perfmon). This becomes particularly frustrating when working with complex monitoring setups that might include 20+ counters across multiple categories.

Windows Performance Monitor actually supports saving counter configurations as Data Collector Sets (DCS) in XML format. Here's how to create and reuse them:

# PowerShell script to export Performance Monitor counters
$counterSet = New-Object -TypeName System.Diagnostics.PerformanceCounterCategory
$counterPath = "C:\PerfLogs\MyCounterSet.xml"
$counters = @(
    "\Processor(_Total)\% Processor Time",
    "\Memory\Available MBytes",
    "\PhysicalDisk(_Total)\Disk Read Bytes/sec",
    "\Network Interface(*)\Bytes Total/sec"
)

# Create the Data Collector Set
logman create counter MyCounterSet -o $counterPath -c $counters -f xml

For more complex setups, you might want to include:

# Extended counter set with sampling interval
logman create counter AdvancedMonitoring -o "C:\PerfLogs\Advanced.xml" 
    -c "\Process(*)\% Processor Time" "\LogicalDisk(*)\% Free Space" 
    -si 00:00:05 -v mmddhhmm -max 1000

To import a previously saved configuration:

# Import counter set from XML
logman import -name "ReloadedCounters" -xml "C:\PerfLogs\MyCounterSet.xml"

# Alternative method using Performance Monitor GUI:
# 1. Open perfmon
# 2. Right-click "Performance Monitor" → "New" → "Data Collector Set"
# 3. Choose "Create from template" and browse to your XML file

For enterprise environments, you might need to deploy standardized counter sets across multiple machines:

# PowerShell script to deploy counter sets to multiple servers
$servers = @("Server01", "Server02", "Server03")
$counterXML = "\\fileshare\IT\StandardCounters.xml"

foreach ($server in $servers) {
    Invoke-Command -ComputerName $server -ScriptBlock {
        param($xmlPath)
        logman import -name "StandardMonitoring" -xml $xmlPath
    } -ArgumentList $counterXML
}

When working with saved counter sets, watch out for:

  • Missing counters on different Windows versions
  • Permission issues when saving/loading XML files
  • Path differences between 32-bit and 64-bit systems
  • Counter name localization in non-English systems

For quick access, create shortcuts with command-line parameters:

# Shortcut target for direct loading
%SystemRoot%\System32\perfmon.exe /sys /counters "C:\Configs\WebServerCounters.xml"

Remember that saved counter sets are machine-specific for certain counters (like instance names), so test them when moving between systems with different hardware configurations.