Results 1 to 6 of 6

Thread: Script to retrieve each process CPU usage

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736

    Script to retrieve each process CPU usage

    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
    Last edited by Petr Schreiber; 16-04-2011 at 11:52.
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2
    Member matthew's Avatar
    Join Date
    Mar 2007
    Location
    England
    Age
    47
    Posts
    489
    Rep Power
    66
    Hey Petr nice script, is there a reason why System Idle Process doesn't show up?

  3. #3
    Senior Member zak's Avatar
    Join Date
    Dec 2008
    Posts
    637
    Rep Power
    83
    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
    Last edited by zak; 16-04-2011 at 07:57.

  4. #4
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    Quote Originally Posted by matthew View Post
    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
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  5. #5
    Member matthew's Avatar
    Join Date
    Mar 2007
    Location
    England
    Age
    47
    Posts
    489
    Rep Power
    66
    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.

  6. #6
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,153
    Rep Power
    736
    No problem,

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


    Petr
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

Similar Threads

  1. MDI script example with some API usage
    By ErosOlmi in forum Preview testing
    Replies: 11
    Last Post: 02-11-2008, 09:46
  2. thinAir process remains open
    By ErosOlmi in forum thinBundle bugs report
    Replies: 2
    Last Post: 21-02-2007, 15:32

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •