Jump to content
Sign in to follow this  
Sh4d3bl4st

Read CPU usage of all processes

Recommended Posts

Sh4d3bl4st

Hey Guys,

I'm working on a programm to get the CPU usage of all running processes.

Or at least the top 5 ones.

I searched in various Forums and Google for a while, but I didn't get a clue how to do this.

I already got a formula how to calculate this, but It doesn't work with the script I wrote.

Maybe you could have a look for yourself and help me ^^

Please be patient with my programming style :mellow:

Func _ProcessListProperties()
    Local $sUserName, $sMsg, $load[1], $time[1], $CPU[1], $sUserDomain, $avProcs, $procs, $objWMI, $colItems, $values[1]

    $procs = ProcessList()
    ReDim $values[$procs[0][0]][2]
    ReDim $time[$procs[0][0]]
    ReDim $load[$procs[0][0]]
    ReDim $CPU[$procs[0][0]][2]

    $objWMI = ObjGet("winmgmts:\\" & @ComputerName & "\root\CIMV2")
    If IsObj($objWMI) Then
        For $i = 2 To ($procs[0][0])
            $colItems = $objWMI.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE IDProcess = '" & $procs[$i][1] & "'")
            For $objItem In $colItems
                $CPU[($i - 2)][0] = $procs[$i][0]
                $load[($i - 2)] = $objItem.PercentProcessorTime
                $time[($i - 2)] = $objItem.TimeStamp_Sys100NS
            Next
        Next
        For $i = 2 To ($procs[0][0])
            $colItems = $objWMI.ExecQuery("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process WHERE IDProcess = '" & $procs[$i][1] & "'")
            For $objItem In $colItems
                $values[($i - 2)][0] = $objItem.PercentProcessorTime - $load[($i - 2)]
                $values[($i - 2)][1] = $objItem.TimeStamp_Sys100NS - $time[($i - 2)]
            Next
        Next
    EndIf

    For $i = 0 To _ArrayMaxIndex($values)
        $CPU[$i][1] = $values[$i][0] / $values[$i][1]
    Next

    For $i = 0 To _ArrayMaxIndex($values)
        $CPU[$i][1] = Round($CPU[$i][1] * 100, 0)
    Next

    _ArraySort($CPU, 1, -1, -1, 1)

    Return $CPU

EndFunc   ;==>_ProcessListProperties

Share this post


Link to post
Share on other sites
Melba23

Sh4d3bl4st,

Welcome to the AutoIt forum. :mellow:

I have used this UDF successfully to read CPU usage by process.

Did you try using the forum Search facility - I recommend it, even if it is a bit quirky at times. :P

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
Sh4d3bl4st

Thx for the fast answer.

As you can see I found pretty much the same code. But the one I found didn't work like this one ^^

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×