Jump to content

Get CPU % with decimal


Recommended Posts

Hello, I'm very stumped with finding the decimal place for the CPU %.

I found some example code online

#include <Array.au3>

$aProcess_Info = GetCPU("RzSynapse")

_ArrayDisplay($aProcess_Info)

Func GetCPU($sProcess = "")

    Local $aAllProcess_Info[1000][3] = [[0]], $aOneProcess_Info[3]

    $oWinMgmts = ObjGet("winmgmts:\\.\root\cimv2")
    $aCPU_Usage = $oWinMgmts.ExecQuery("SELECT Name, IDProcess, PercentProcessorTime FROM Win32_PerfFormattedData_PerfProc_Process")

    For $vCPU_Info In $aCPU_Usage
        If $sProcess Then
            If $vCPU_Info.Name = $sProcess Then
                $aOneProcess_Info[0] = $sProcess
                $aOneProcess_Info[1] = $vCPU_Info.IDProcess
                $aOneProcess_Info[2] = $vCPU_Info.PercentProcessorTime & " %"
                Return $aOneProcess_Info
            EndIf
        Else
            $aAllProcess_Info[0][0] += 1
            $aAllProcess_Info[$aAllProcess_Info[0][0]][0] = $vCPU_Info.Name
            $aAllProcess_Info[$aAllProcess_Info[0][0]][1] = $vCPU_Info.IDProcess
            $aAllProcess_Info[$aAllProcess_Info[0][0]][2] = $vCPU_Info.PercentProcessorTime & " %.1f"
        EndIf
    Next

    ReDim $aAllProcess_Info[$aAllProcess_Info[0][0] +1][3]
    Return $aAllProcess_Info
EndFunc   ;==>GetCPU

Currently the code shows the CPU % as 0 % and I want to show 0.0 % I found this online for strings "%.1f" but i'm not sure how to incorporate it.

Link to comment
Share on other sites

I do see that but I've been looking through other examples and I can't find any CPU percentages with a decimal. The "PercentProcessorTime" is what returns the number back and I don't think it will work with StringFormat. I could be wrong though

PercentProcessorTime
Link to comment
Share on other sites

; Try this

Local $iPercentage = 0

Local $sPercentage = StringFormat('%.1f', $iPercentage)

ConsoleWrite($sPercentage & @CRLF)

 

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

Okay, I got the decimal after the CPU usage to show up but the program is not reading the decimals from the task manager. Its at Example: 11.0 %,  5.0 %, 22.0%. It never reads 5.4% or 12.8%

#include <Array.au3>

$aProcess_Info = GetCPU("Explorer")

_ArrayDisplay($aProcess_Info)

Func GetCPU($sProcess = "")

    Local $aAllProcess_Info[1000][3] = [[0]], $aOneProcess_Info[3]

    $oWinMgmts = ObjGet("winmgmts:\\.\root\cimv2")
    $aCPU_Usage = $oWinMgmts.ExecQuery("SELECT Name, IDProcess, PercentProcessorTime FROM Win32_PerfFormattedData_PerfProc_Process")

    For $vCPU_Info In $aCPU_Usage
        If $sProcess Then
            If $vCPU_Info.Name = $sProcess Then
                $aOneProcess_Info[0] = $sProcess
                $aOneProcess_Info[1] = $vCPU_Info.IDProcess
                $aOneProcess_Info[2] = StringFormat('%.1f', $vCPU_Info.PercentProcessorTime) & " %"
                Return $aOneProcess_Info
            EndIf
        Else
            $aAllProcess_Info[0][0] += 1
            $aAllProcess_Info[$aAllProcess_Info[0][0]][0] = $vCPU_Info.Name
            $aAllProcess_Info[$aAllProcess_Info[0][0]][1] = $vCPU_Info.IDProcess
            $aAllProcess_Info[$aAllProcess_Info[0][0]][2] = StringFormat('%.1f', $vCPU_Info.PercentProcessorTime) & " %"
        EndIf
    Next

    ReDim $aAllProcess_Info[$aAllProcess_Info[0][0] +1][3]
    Return $aAllProcess_Info
EndFunc   ;==>GetCPU

svPlbA4.png

 

Sorry for my crappy explanation.

Edited by red0fireus
Link to comment
Share on other sites

Add this debug line:

ConsoleWrite($vCPU_Info.PercentProcessorTime & @TAB & VarGetType($vCPU_Info.PercentProcessorTime) & @LF)

and see that this property returns a string datatype containing an integer.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

It really isn't that difficult to find a pure AutoIt Example script that produces this (google: site:autoitscript.com cpu load, near bottom of first page).

Link to comment
Share on other sites

1 hour ago, Earthshine said:

lots of code

Oh really? kernel32.dll + ntdll.dll + CPU_ProcessorUsage.au3 = 687 Kb + 1909 Kb + 14 Kb = 2,610 Kb

.NET = 4.5 GB

Link to comment
Share on other sites

@Earthshine Well we are obviously looking for something which we can easily use in AutoIt code, and it is simpler to add more AutoIt code rather than learning C# and writing your program in it :)

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

@Earthshine: There's actually a C#/.Net AutoIt subforum for your suggestion; I think it's neither apposite nor helpful if your first post in someone's request for Help with AutoIt in the main AutoIt GH & S forum is producing a C# snippet, especially when responding to New Members and obvious beginners, and even more so when native AutoIt solutions are readily available. Thanks.

Edited by RTFC
Link to comment
Share on other sites

  • Moderators

Earthshine,

I entirely agree with RTFC's comments above - your constant suggestions of using other languages or offering of non-AutoIt code in response to questions is becoming annoying. Please remember that this is an AutoIt forum and we should try and focus on that language when we can. Thanks for your future cooperation in this.

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

 

Link to comment
Share on other sites

ok. but i have not any examples that give anything but integer percentages. using the suggested google search as mentioned above. going through samples from search pages. thanks.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

  • Moderators
27 minutes ago, Earthshine said:

ok. but i have not any examples that give anything but integer percentages

Then stay out of the topic...

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Ascendant's Example script I linked to earlier in this thread actually reports CPU usage with decimal. And if you look for the Round() function in that script, you can add more decimals (by editing its second parameter).

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

×
×
  • Create New...