Jump to content
red0fireus

Get CPU % with decimal

Recommended Posts

red0fireus

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.

Share this post


Link to post
Share on other sites
TheDcoder

I think you are looking for StringFormat :)

StringFormat

 


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
red0fireus

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

Share this post


Link to post
Share on other sites
TheDcoder
; Try this

Local $iPercentage = 0

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

ConsoleWrite($sPercentage & @CRLF)

 


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
red0fireus
Posted (edited)

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

Share this post


Link to post
Share on other sites
jchd

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)

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

snip

Edited by Earthshine
  • Confused 1

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
RTFC

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

Share this post


Link to post
Share on other sites
Earthshine

wowsers that lots of code... lol


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
RTFC
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

  • Like 1

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

what doesnt have .net installed. I'm only using handful of dll

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
TheDcoder

@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 :)

  • Like 1

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

If I have hurt or offended you in anyway, Please accept my apologies, I never (regardless of the situation) mean to do that to anybody!!!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites
Earthshine

you can use c# with autoit though with CLR3.au3 just in case you guys determine some kind of bug, but it looks like it's covered. Thanks.


My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
RTFC
Posted (edited)

@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
  • Thanks 1

Share this post


Link to post
Share on other sites
Melba23

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


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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
Earthshine
Posted (edited)

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

 

Share this post


Link to post
Share on other sites
JLogan3o13
27 minutes ago, Earthshine said:

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

Then stay out of the topic...


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
RTFC

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

Share this post


Link to post
Share on other sites
Earthshine
Posted (edited)

yes indeed. sorry all. @RTFC link led me to what he was talking about, which does, in fact give the OP what he wants.

CPU_ProcessorUsage.au3

 

Sorry again.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Share this post


Link to post
Share on other sites
red0fireus

Thank you so much for the help!

I really appreciate it

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

  • Similar Content

    • argumentum
      By argumentum
      The back story:
      I've got a Dell XPS w/ i7-8700K. The fastest, by single core, I could get, by well known PC maker.
      The problem is that the fan can get so loud, like, REALLY LOUD, I can not use the CPU at its max. clock speed.
      I could leave it at 90% all the time and not use this but I want to have the full 4.x Ghz and no parked cores, at all times, if I can.
      But as room temperature and CPU load changes, a set throttle, may still make fan noise.
      The solution:
      To avoid the fan from going "airplane turbine mode", the utility gets the temp. from "Core Temp" ( you can google it )
      It has a"plug-in" called "Core Temp Remote Server". The utility gets the values via TCP.
      When it "feels" it's gonna get hot, drops the CPU throttle to a selected value, lets say 99% ( where is quieter ) and back up to 100% when it "feels" is ok to go back.
      Now temperature can creep up to higher than expected if load is sustained or room temperature changes. So there is an "anti creep up" feature, to temporarily set the throttle even lower, 5% at a time, until the known quiet temperature is achieved.
      If don't know how to find the temperature you should use, check out these videos. They will tell you how.
      https://www.youtube.com/watch?v=p3B5WCJZTuw&ab_channel=SergeantPope-KomadaComputerRepair
      https://www.youtube.com/watch?v=VuP6I0mOb1s&ab_channel=Techquickie
      https://www.google.com/search?q=find+max+cpu+temperature
      The end result:
      Any thermal problem, is a hardware problem. No way around that, other than attending to the CPU cooling and case ventilation. Software can not fix that.
      But without this utility, the PC would slow down the CPU anyway, to keep it from melting.
      This software preemptively slow down the CPU, keeping the CPU related fan speeds from going to maximum RPM. Hence having a slower, but a quieter box.
    • francoiste
      By francoiste
      in AutoIt we have:
      _WinAPI_IsProcessorFeaturePresent($iFeature) this internally just calls what is provided by microsoft: https://msdn.microsoft.com/en-us/library/windows/desktop/ms724482(v=vs.85).aspx
      unfortunately this is completely outdated as we can only check for features like MMX and up to a maximum of SSE2 or SSE3 - which have been introduced back in 2001 / 2004.
      but nowadays there is stuff like AVX, AVX2, AVX-512 ...
      for example see: https://en.wikipedia.org/wiki/Template:Multimedia_extensions
      well, for CPP we have the FeatureDetector (by Mysticial): https://github.com/Mysticial/FeatureDetector
      it calls __cpuid / __cpuidex from <intrin.h>.
      for additional details see: https://msdn.microsoft.com/en-us/library/hskdteyh.aspx
       
      has anyone tried to determine such SIMD features and related CPU capabilites with AutoIt?
    • VIP
      By VIP
      The simple widget shows the percentage of cpu, ram used and the CPU temperature (WMI).
      - Every 30s check, sync and update time for computer.
      - Drag adn Drop any files on GUI for reset Attribute (-RAHS) and set Full Access to Everyone !
      - Righ Click on x and click Un/Install app to boot with Windows!
      CPU_Widget_TimeSync.exe CRC32: 1956339E MD5: BFC2B596BD1EDA35D6B02B97F8FB57AE SHA-1: 796A663739EA5AC9890D9FA324A98E987F35E7C8
      Function code from:
      CPU: 
      RAM:  MemGetStats ( )
      Memory:   _WinAPI_EmptyWorkingSet ()
       
    • Wicked_Caty
      By Wicked_Caty
      What am I supposed to do, if a program is able for a long time?
      My program is idle for 20 minutes, before doing a very tiny task, and then starting all over again.
      I tries using Sleep(), but that literally melts my CPU and pushes them up for 20 to 30%. Permanently checking the time costs too much CPU too.
      Any other way of doing this? Preferably without taking more than 1% CPU. It also needn't be exactly 20 minutes, everything between 15 and 25 is fine too
      Thanks!
    • Wicked_Caty
      By Wicked_Caty
      I've written a small surveillance tool for one of my programs. It shows the time that has already passed, the estimated time until the end, the average time of the steps, and the progress in %.
      It works pretty well, but I want to add some stats. That'd be the usages of the CPU, RAM, network and hard-drive of all threads in %. Basically what you see in the taskmanager, but without the list of every single process.
      I'm aware that there's no in-built function for that, and that I'd need an additional library. Are there any for that? Simply functions that return the stats mentioned above. Thanks!
      Edit:   I run an ASUS R752M with an Intel BayTrail M3540 on Windows 10 64-bits.
×