Processor % usage

In this post - http://msmvps.com/blogs/richardsiddaway/archive/2011/08/07/top-processes-by-resource.aspx – I looked at wrapping the Get-Process cmdlet in a function so that we could always get the top N sorted by a number of criteria.

I was asked if this showed % processor use. The answer is no. The CPU property show total CPU time used by a process since the machine started but as processes come and go it doesn’t show the whole story.

If you want to view the %processor usage of a process we can use Get-Counter.

PS> get-counter -counter "\Process(System)\% Processor Time"

Timestamp                 CounterSamples
---------                 --------------
11/08/2011 20:24:44       \\rslaptop01\process(system)\% processor time :
                          0

but we have lots of processes running so we need to find them all

Get-Counter -ListSet Process

shows the process performance object. If we expand the PathsWithInstances property

Get-Counter -ListSet Process | select -ExpandProperty PathsWithInstances

we get a bit more than we need – so we add a filter

Get-Counter -ListSet Process |
select -ExpandProperty PathsWithInstances |
where {$_ -like "*% Processor Time"}

which gives us all of the process % usage counters.

We might think to use them like this

Get-Counter -ListSet Process |             
select -ExpandProperty PathsWithInstances |             
where {$_ -like "*% Processor Time"} |            
foreach {            
Get-Counter -Counter $_             
            
}


But it accesses one counter at a time and there will be a few seconds difference on the time stamp of the first and last process. We could live with that but it would be better to get all the counters at the same time



PS> get-counter -counter "\Process(System)\% Processor Time", "\Process(Idle)\% Processor Time"



Timestamp                 CounterSamples
---------                 --------------
11/08/2011 20:31:08       \\rslaptop01\process(system)\% processor time :
                          1.53847140039448



                          \\rslaptop01\process(idle)\% processor time :
                          186.155039447732



It would be good if we could sort these in terms of usage



get-counter -counter "\Process(System)\% Processor Time", `

"\Process(Idle)\% Processor Time" |


select -ExpandProperty  countersamples |


select Instancename, Cookedvalue |


sort Cookedvalue -Descending | ft -a



InstanceName      CookedValue
------------      -----------
idle         196.924339250493
system                      0



All we have to do is create a list of all of the processes



function get-cpuuser {            
            
$procnames = Get-Counter -ListSet Process |             
select -ExpandProperty PathsWithInstances |             
where {$_ -like "*% Processor Time"}             
            
Get-Counter -Counter $procnames |            
select -ExpandProperty  Countersamples |             
select Instancename, Cookedvalue |             
sort Cookedvalue -Descending            
}


I get values of more than 100 sometimes – this is because I have multiple cores in the system

Leave a Reply