Windows Performance Data Helper - PDH API

Costas

Administrator
Staff member
User can use the Performance Monitor included in Windows, to explore performance counters want to monitor. In cmd run, perfmon.

R2icTYG.png

You can use the following powershell to list the available performance counters :
Code:
Get-Counter -ListSet * | Select-Object -ExpandProperty CounterSetName
OR
https://ss64.com/nt/logman.html

Windows Performance Data Helper (PDH) API allows developers to programmatically access performance metrics and performance counters in a Windows system. The PDH API provides a set of functions and data structures that enable applications to query, collect, and manage performance data.

In nutshell :
1) PdhOpenQuery - create a query object
2) PdhAddCounter or PdhAddEnglishCounter - are used to add a performance counter to a query object
//PdhAddEnglishCounter - It allows you to specify the counter path in English, which can be more user-friendly
At this point, adding the counter to the query object does not immediately initiate data collection. Instead, it sets up the query object with the counters you want to monitor and configures what data to collect. The actual data collection typically happens when you call PdhCollectQueryData
3) PdhCollectQueryData - collect performance data from the performance counters added to a query object using functions like PdhAddCounter or
4) PdhGetFormattedCounterValue - process data [HERE GET THE DATA]
5) PdhRemoveCounter - close performance counter (otherwise leaving orphaned handles in the system)
6) PdhCloseQuery - close query object (otherwise leaving orphaned handles in the system)

Bash:
###Processor:
\Processor(_Total)\% Processor Time: Total CPU usage.
\Processor(_Total)\% User Time: User-mode CPU usage for all cores.
\Processor(_Total)\% Privileged Time: Privileged mode CPU usage for all cores.
\Processor(_Total)\Interrupts/sec: Interrupts per second.

###Memory:
\Memory\Available MBytes: Available physical memory in megabytes.
\Memory\Committed Bytes: Total committed memory in bytes.
\Memory\Page Reads/sec: Page reads from disk per second.
\Memory\Page Writes/sec: Page writes to disk per second.

###Network Interface:
\Network Interface(*)\Bytes Total/sec: Total bytes transmitted and received across all network interfaces.
\Network Interface(*)\Bytes Received/sec: Bytes received on all network interfaces.
\Network Interface(*)\Bytes Sent/sec: Bytes sent on all network interfaces.
\Network Interface(Local Area Connection)\Bytes Received/sec: Bytes received on a specific network interface.

###Logical Disk:
\LogicalDisk(_Total)\% Free Space: Percentage of free space on all logical disks.
\LogicalDisk(_Total)\Free Megabytes: Free space in megabytes on all logical disks.
\LogicalDisk(C:)\% Free Space: Percentage of free space on a specific logical disk.
\LogicalDisk(D:)\Disk Writes/sec: Disk writes per second on a specific logical disk.

###System:
\System\System Up Time: System uptime in seconds.
\System\Context Switches/sec: Context switches per second.
\System\Processor Queue Length: Number of threads in the processor queue.

###Process:
\Process(_Total)\% Processor Time: Total CPU usage of all processes.
\Process(notepad)\% Processor Time: CPU usage of a specific process (e.g., "notepad").
\Process(_Total)\Working Set: Working set memory usage of all processes.
\Process(explorer)\Private Bytes: Private bytes usage of a specific process.

###.NET CLR Data (for .NET applications):
\.NET CLR Memory(*)\# Bytes in all Heaps: Total bytes in all .NET memory heaps.
\.NET CLR Memory(v4.0.30319)\% Time in GC: Percentage of time spent in garbage collection.
\.NET CLR LocksAndThreads(v4.0.30319)\Contention Rate / sec: Contention rate for .NET locks.

###PhysicalDisk:
\PhysicalDisk(_Total)\% Idle Time: Percentage of time the physical disk is idle.
\PhysicalDisk(0 C:)\Avg. Disk Bytes/Read: Average bytes per read operation on a specific disk.

###Cache:
\Cache\Cache Read Hits %: Percentage of cache read hits.
\Cache\Cache Bytes: Current size of the cache in bytes.

###Network:
\Network\Connections Established: Number of network connections established.
\Network\Datagrams Received Errors: Number of datagrams received with errors.

###Server:
\Server\Bytes Received/sec: Bytes received by the server per second.
\Server\Pool Nonpaged Failures: Nonpaged pool failures on the server.

###SQLServer:
\SQLServer:SQL Statistics\Batch Requests/sec: SQL Server batch requests per second.
\SQLServer:Locks(_Total)\Lock Wait Time (ms): Total lock wait time in milliseconds.

###ASP.NET:
\ASP.NET\Requests Queued: Number of requests queued.
\ASP.NET\Request Execution Time: Average request execution time.

###IIS:
\Web Service(_Total)\Total Get Requests: Total GET requests to the web service.
\Web Service(Default Web Site)\Total Bytes Sent: Total bytes sent by the Default Web Site.

the actual performance counters available on your system may vary depending on the hardware, software, and services installed.

2023 - sample CSharp
2009 - greets to adamserrata



System.Diagnostics OOB
C#:
//src - https://stackoverflow.com/a/3545253 | https://stackoverflow.com/a/278088
Process p = /*get the desired process here*/;
PerformanceCounter ramCounter = new PerformanceCounter("Process", "Working Set", p.ProcessName);
PerformanceCounter cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
while (true)
{
    Thread.Sleep(500);
    double ram = ramCounter.NextValue();
    double cpu = cpuCounter.NextValue();
    Console.WriteLine("RAM: "+(ram/1024/1024)+" MB; CPU: "+(cpu)+" %");
}

github sample
 
Top