Results 1 to 6 of 6

Thread: Script to retrieve each process CPU usage

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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
  •