Jump to content

simplier cpu usage script


kiboost
 Share

Recommended Posts

Riiight. Maybe you should actually read the book now that you're aware of it. There's no 'free interpretation' of unions in there.

By the way, why add AutoItObject to this? kiboost didn't want to use my UDF because it was 'huge'. That'd be an understatement for AutoIt object.

Link to comment
Share on other sites

i believe i've followed the wmi examples but i still return a couple of problems

1) it seems i am in hundreds when i should be in the tens

2) but changing the 100 to a * 10 only highlights the greater issue in that it seems a fairly static number and unrelated to cpu usage.

while 1

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "localhost"

$Output=""
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")


$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)


If IsObj($colItems) then
   For $objItem In $colItems

If $objitem.name = "_Total" Then

      $Proc1 = $objItem.PercentProcessorTime
       $Stamp1 = $objItem.Timestamp_Sys100NS

   EndIf
       next
EndIf



 sleep (2000)

 $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PerfRawData_PerfProc_Process", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)


If IsObj($colItems) then
   For $objItem In $colItems

If $objitem.name = "_Total" Then

      $Proc2 = $objItem.PercentProcessorTime
       $Stamp2 = $objItem.Timestamp_Sys100NS

   EndIf
       next
EndIf

$Procd = $Proc2 - $Proc1
$Stampd = $Stamp2 - $Stamp1


$final = ($procd/$stampd) * 100

tooltip ("CPU USAGE:  " & $final, 0, 0)

sleep (1000)

wend

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Riiight. Maybe you should actually read the book now that you're aware of it. There's no 'free interpretation' of unions in there.

By the way, why add AutoItObject to this? kiboost didn't want to use my UDF because it was 'huge'. That'd be an understatement for AutoIt object.

Don't riiight me please. I thought we passed that level of interaction.

When was that book published? When Microsoft released first 64bit system?

Find "64bit systems", or even better -windows 7- chapter in that book and read about definitions of that structure(s).

AutoItObject is there to show that earth is not flat. I wasn't aiming kiboost.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

kiboot, if you just want to check the *total* combined CPU usage once using my Performance Counters UDF, below would be the easiest way..

Oh, and you shouldn't really get an error here (unless it has to do with the short sleep time), but anyway the code below takes the possibility of an error into account:

#include "_PDH_PerformanceCounters.au3"
_PDH_Init()
Local $hPDHQuery,$hCPUCounter,$iCPUUsage
$hPDHQuery=_PDH_GetNewQueryHandle()
$hCPUCounter=_PDH_AddCounter($hPDHQuery,":238\6\(_Total)")
_PDH_CollectQueryData($hPDHQuery) ; collect the query information once (1st collection may fail, so we'll discard it)
Sleep(50)
While 1
    $iCPUUsage=_PDH_UpdateCounter($hPDHQuery,$hCPUCounter)  ; collect query info and then grab counter value
    If Not @error Then ExitLoop
    Sleep(50)
WEnd
_PDH_UnInit($hPDHQuery)
ConsoleWrite("CPU Counter = "&$iCPUUsage&@CRLF)

If the size bothers you, you can actually strip the 7 functions (6 above +1 for string translation) from the UDF. Or just use a strip command for Obfuscator when compiling the code.

For me the above gives very accurate results. You can certainly try your luck with the other code however.

*edit: (don't bother with the WMI version above if you want something that works quick. Besides, the data it is pulling info from is the same place the Performance Counters pulls info from, only WMI is much slower)

Edited by Ascend4nt
Link to comment
Share on other sites

Don't riiight me please. I thought we passed that level of interaction.

When was that book published? When Microsoft released first 64bit system?

Find "64bit systems", or even better -windows 7- chapter in that book and read about definitions of that structure(s).

If the structures are still giving the same data, and only the type sizes have changed, why would you question it. I've worked fairly extensively with 'undocumented' functions on systems from Win 2000->Win 7 x64. Microsoft doesn't document the structures properly on MSDN and creates what you may interpret as 'unions', but really they are just hiding what is already defined by other sources.

Link to comment
Share on other sites

Link to comment
Share on other sites

Nice finds. ;) Downloading from the 2nd link as I type!

Link to comment
Share on other sites

kiboot, if you just want to check the *total* combined CPU usage once using my Performance Counters UDF, below would be the easiest way..

#include "_PDH_PerformanceCounters.au3"
_PDH_Init()
Local $hPDHQuery,$hCPUCounter,$iCPUUsage
$hPDHQuery=_PDH_GetNewQueryHandle()
$hCPUCounter=_PDH_AddCounter($hPDHQuery,":238\6\(_Total)")
_PDH_CollectQueryData($hPDHQuery) ; collect the query information once (1st collection may fail, so we'll discard it)
Sleep(50)
While 1
    $iCPUUsage=_PDH_UpdateCounter($hPDHQuery,$hCPUCounter)  ; collect query info and then grab counter value
    If Not @error Then ExitLoop
    Sleep(50)
WEnd
_PDH_UnInit($hPDHQuery)
ConsoleWrite("CPU Counter = "&$iCPUUsage&@CRLF)

Many thanks Ascend4nt, works like a charm !! Will adapt this one in my script. It's surprising how many functions and line code it take to just check a value, but I guess we should thanks microsoft here for that !

Thanks,

Kib

Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

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...