In this post http://itknowledgeexchange.techtarget.com/powershell/wmi-and-performance-counters-i/ we looked at using WMI to retrieve performance counter information and looked at the
classes. We used the formatted data because the raw data needed some calculations performing. The draw back to the formatted data is that it doesn’t contain the timestamp. if we want the exact timestamp we need to work with the raw data.
If you run this
Get-WmiObject -Class Win32_PerfRawData_PerfOS_Processor -Filter "Name=’_Total’"
you will see similar results to the formatted data except that the numbers don’t seem to make sense – for instance I got
PercentProcessorTime : 88950912194
Man that processor is busy!
OK we need to perform some calculations on this data. We start by looking at the class documentation. For each counter it will give something like this
- Data type: uint64
- Access type: Read-only
- Qualifiers: DisplayName ( "% Processor Time") , PerfDefault, CounterType ( 558957824) , DefaultScale ( 0) , PerfDetail ( 100)
Percentage of time that the processor is executing a non-idle thread. This property was designed as a primary indicator of processor activity. It is calculated by measuring the time that the processor spends executing the thread of the idle process in each sample interval and subtracting that value from 100%. Each processor has an idle thread which consumes cycles when no other threads are ready to run. It can be viewed as the percentage of the sample interval spent doing useful work. This property displays the average percentage of busy time observed during the sample interval. It is calculated by monitoring the time the service was inactive, and then subtracing that value from 100%.
The important point is the counter type. The list of counter types can be found here
Our counter type translates as PERF_100NSEC_TIMER_INV
OK that means a lot. Now what do we do with it. If we look up the type we get this
This counter type shows the average percentage of active time observed during the sample interval. This is an inverse counter. Counters of this type calculate active time by measuring the time that the service was inactive and then subtracting the percentage of active time from 100 percent.
and a formula of (1- ((N1 - N0) / (D1 - D0))) x 100
This means we need two measurements
$count1 = Get-WmiObject -Class Win32_PerfRawData_PerfOS_Processor -Filter "Name=’_Total’"
Start-Sleep -Seconds 10
$count2 = Get-WmiObject -Class Win32_PerfRawData_PerfOS_Processor -Filter "Name=’_Total’"
$ppt = (($count2.PercentProcessorTme – $count1.PercentProcessorTme) / ($count2.TimeStamp_sys100NS – $count1.TimeStamp_sys100NS)) * 100
"Percent processor time is $ppt"
This doesn’t give sensible answers so need to go back and dig a bit deeper. I’m beginning to think that using the raw counters is too hard