Jump to content

CPU Meter


Polyphem
 Share

Recommended Posts

Good Evening Geeks :D, Gents :lmao: and Ladys :P,

I searched the forum about acquiring CPU usage and stumbled over WMI. So, the script below does exactly what I want it to... but... its performance is really bad. As I read, and guess, this might be inherent to the WMI service. Can anybody point me to another source / another method for the LoadPercentage info? What's important is, that I want the info per Core, found some methods (Statusbar of Taskmanager) to get the overall usage, but not per core. Read something about a Statistic in the Registry, but that entry does not seem to exist in XP (my current OS).

; ============================================================================
; Program Name: CPU Meter
; Version: V0.1
; AutoIt Version : v3.2.2.0
; Author : Polyphem
; Description: 
;
; Usage Monitor for a Multi-Core CPUs, using the WMI Service to retrieve the actual workload / utilization (similar to the Windows TaskManager)
; Should work on any system, just showing the actual  processors / cores (hyperthreading?) present
;
; Currently it supports 4 processors... now guess which processor I have :)....
;
; ============================================================================

; ============================================================================
; Declare includes, opts and local/global variables
; ============================================================================

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

Local $CenterWidth = (@DesktopWidth - 200) / 2 
Local $CenterHeight = (@DesktopHeight - 300) / 2

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

; ============================================================================
; GUI Definition
; ============================================================================

$gui = GUICreate("CPU Meter",200,300,$CenterWidth, $CenterHeight,-1,$WS_EX_TOPMOST)
GUISetOnEvent(-3, "_Exit_Event", $gui)
GUISetIcon (@SystemDir & "\shell32.dll",12)

GUICtrlCreateLabel ("CPU1",5,3,40,20,$SS_CENTER)
$progressbar_core1 = GUICtrlCreateProgress (10,20,30,250,$PBS_VERTICAL)
$label_core1 = GUICtrlCreateLabel ("",10,275,30,20,$SS_CENTER)

GUICtrlCreateLabel ("CPU2",55,3,40,20,$SS_CENTER)
$progressbar_core2 = GUICtrlCreateProgress (60,20,30,250,$PBS_VERTICAL)
$label_core2 = GUICtrlCreateLabel ("",60,275,30,20,$SS_CENTER)

GUICtrlCreateLabel ("CPU3",105,3,40,20,$SS_CENTER)
$progressbar_core3 = GUICtrlCreateProgress (110,20,30,250,$PBS_VERTICAL)
$label_core3 = GUICtrlCreateLabel ("",110,275,30,20,$SS_CENTER)

GUICtrlCreateLabel ("CPU4",155,3,40,20,$SS_CENTER)
$progressbar_core4 = GUICtrlCreateProgress (160,20,30,250,$PBS_VERTICAL)
$label_core4 = GUICtrlCreateLabel ("",160,275,30,20,$SS_CENTER)

GUISetState ()

; ============================================================================
; Main Loop
; ============================================================================

While 1
    $colItems = $objWMIService.ExecQuery("SELECT LoadPercentage FROM Win32_Processor", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($colItems) Then
        $i = 0
        For $objItem In $colItems
            if $i = 0 Then
                GUICtrlSetData ($progressbar_core1,$objItem.LoadPercentage)
                GUICtrlSetData ($label_core1,$objItem.LoadPercentage & "%")
            ElseIf $i = 1 Then
                GUICtrlSetData ($progressbar_core2,$objItem.LoadPercentage)
                GUICtrlSetData ($label_core2,$objItem.LoadPercentage & "%")
            ElseIf $i = 2 Then
                GUICtrlSetData ($progressbar_core3,$objItem.LoadPercentage)
                GUICtrlSetData ($label_core3,$objItem.LoadPercentage & "%")
            ElseIf $i = 3 Then
                GUICtrlSetData ($progressbar_core4,$objItem.LoadPercentage)
                GUICtrlSetData ($label_core4,$objItem.LoadPercentage & "%")
            EndIf
            $i += 1
        Next
    Else
        MsgBox(0, "WMI Output", "No WMI Objects Found for class: " & "Win32_Processor")
    EndIf
WEnd

; ============================================================================
; Functions
; ============================================================================

Func _Exit_Event()
    Exit
EndFunc

I would be happy, if someone can point me to a method offering a better performance.

Regards

Polyphem

P.S.: The AUTOIT Tag seems to be a bit lazy, parsing the smily, hu :whistle: ?

Edit: Code cleanup.

Edited by Polyphem
This post will be edited again by Polyphem: Tomorrow, 11:55 AM
Link to comment
Share on other sites

Link to comment
Share on other sites

  • 8 months later...

If you want better performance you should put a Sleep(10) in your while loop. The WMI query probably takes quite some time to process, so you use TimerInit() and TimerDiff() to benchmark how long it takes the query to return.

Based on the result of the benchmark you should make the sleep at or above that.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...