PDA

View Full Version : Script to retrieve each process CPU usage



Petr Schreiber
15-04-2011, 21:20
Hi,

I just recently ran into situation I needed to retrieve list of processes running in memory + their CPU usage and save it to file. The following script did the job:


Uses "WMI", "OS", "Console", "File"

PrintL "Retrieving information, please wait..."

' -- The following code returns query to string buffer
' -- We request only Name and PercentProcessorTime fields, but more is possible, see:
' -- http://msdn.microsoft.com/en-us/library/aa394277%28v=vs.85%29.aspx
String sBuffer = WMI_GetData(OS_GetComputerName, "", "", "", "Win32_PerfFormattedData_PerfProc_Process", "", "Name,PercentProcessorTime")
String sBufferOut

' -- Number of processes found equals to number of Element "tags"
Long nProcesses = Tally(sBuffer, "{Element")
Long i
String sName, sUsage, sLine

' -- File to which we retrieve info
String sFile = APP_SourcePath+"ProcessList.txt"

For i = 1 To nProcesses

' -- Grab$ is great for retrieving fields determined by different "tags"
sName = Grab$(sBuffer, "Name=", $CRLF, i)
sUsage = Grab$(sBuffer, "PercentProcessorTime=", $CRLF, i)

' -- We leave out items Idle and _Total, but you can change this to your needs
If sName = "Idle" Or sName = "_Total" Then Iterate For

sLine = LSet$(sName, 30 Using " ") + sUsage+"%"
sBufferOut += sLine + $CRLF
PrintL sLine

Next

FILE_Save(sFile, sBufferOut)

PrintL
PrintL "File saved as " + sFile
PrintL "Press any key to quit..."
WaitKey
It can be done via Win32 as well, but the code gets quite long, while using WMI it was just few lines of code.

The version I used was 3 lines long, but the version above is more readable :)
Original code was:


Uses "WMI", "OS", "File"

FILE_Save(APP_SourcePath+"ProcessDump.txt", WMI_GetData(OS_GetComputerName, "", "", "", "Win32_PerfFormattedData_PerfProc_Process", "", "Name,PercentProcessorTime"))
and resulted in such a log:


Computer:{PETR-TOSH}@Action:{Win32_PerfFormattedData_PerfProc_Process}
{Element:1|82}
Name=Idle
PercentProcessorTime=100
{Element:2|82}
Name=System
PercentProcessorTime=0
{Element:3|82}
Name=smss
PercentProcessorTime=0
{Element:4|82}
Name=csrss
PercentProcessorTime=0
...
So you can see why I picked the GRAB$ in tuned version to make it more readable.


Petr

matthew
16-04-2011, 02:55
Hey Petr nice script, is there a reason why System Idle Process doesn't show up?

zak
16-04-2011, 07:47
thanks Petr
it does not show System Idle Process in my windows xp,
it shows also wmiprvse 100% while it is not available in the processes List when we ctrl-del-alt, i replaced it by another wmiprvse.exe from backup and it is the same. some reports says that wmiprvse.exe can be virus infected .
www.neuber.com/taskmanager/process/wmiprvse.exe.html (http://www.neuber.com/taskmanager/process/wmiprvse.exe.html)

Petr Schreiber
16-04-2011, 09:25
Hey Petr nice script, is there a reason why System Idle Process doesn't show up?

Hi Matthew,

yes, the reason is right in the code:


If sName = "Idle" Or sName = "_Total" Then Iterate For
If you want it listed, just change the condition to:


If sName = "_Total" Then Iterate For
I did not include it, as I do not consider the Idle information a process in standard way.

Zak, wmiprvse seems to be the engine responsible for the wmi queries, so it disappears once program is gone. On my PC I usually get 0% for it. The typical "hog" seems Firefox 4 for me now - during post editing it is usually from 18 to 24%.


Petr

matthew
16-04-2011, 11:41
Sorry Petr I probably would have noticed that line in the script but it was late at night when I was posting.

I really should have been sleeping, lol. :D

Petr Schreiber
16-04-2011, 11:52
No problem,

I added more comments to the code, so it should be better understandable.


Petr