CPULoad() : CPU usage percentage ( Demos for XGraph )

Post your working scripts, libraries and tools
User avatar
SKAN
Posts: 207
Joined: 29 Sep 2013, 16:58

CPULoad() : CPU usage percentage ( Demos for XGraph )

22 Apr 2014, 13:05

Code: [Select all] [Download] (CPULoad.ahk)GeSHi © Codebox Plus

CPULoad() { ; By SKAN, CD:22-Apr-2014 / MD:05-May-2014. Thanks to ejor, Codeproject: http://goo.gl/epYnkO
Static PIT, PKT, PUT ; http://ahkscript.org/boards/viewtopic.php?p=17166#p17166
IfEqual, PIT,, Return 0, DllCall( "GetSystemTimes", "Int64P",PIT, "Int64P",PKT, "Int64P",PUT )

DllCall( "GetSystemTimes", "Int64P",CIT, "Int64P",CKT, "Int64P",CUT )
, IdleTime := PIT - CIT, KernelTime := PKT - CKT, UserTime := PUT - CUT
, SystemTime := KernelTime + UserTime

Return ( ( SystemTime - IdleTime ) * 100 ) // SystemTime, PIT := CIT, PKT := CKT, PUT := CUT
}


Simple test:


Plotting Real time CPU load with XGraph

The following samples are extremely light on CPU and takes several hours ( 6 - 12 ) to consume one second of CPU Time.

Sample #1 - High-Resolution plotting

Will plot CPU usage for trivial things like mouse movement.

    MKV in Windows Media Player ( Peace / Loading / Running / Paused / Resumed / Closing / Peace )
    Image

    Processor in peace.
    Image

Code: [Select all] [Expand] [Download] (CPULoadMonitor.ahk)GeSHi © Codebox Plus



Sample #2 - Grid version.

Clicking on a column reveals the percentage in status bar.
Toggling ALT key will pause/un-pause the timer. ( wasn't aware until today )

    Google Chrome ( Middle click smooth scroll, uses 2 processes in my dual core )
    Image

    Processor in peace.
    Image

Code: [Select all] [Expand] [Download] (CPULoadMonitor_Grid.ahk)GeSHi © Codebox Plus



Sample #3 - Spikes version.

Background gradient created with CreateDIB()

    Google Chrome ( Middle click smooth scroll, uses 2 processes in my dual core )
    Image

    Processor in peace.
    Image

Code: [Select all] [Expand] [Download] (CPULoadMonitor_Spikes.ahk)GeSHi © Codebox Plus

Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: CPULoad() : CPU usage as percentage

22 Apr 2014, 15:37

thanks! does this have something to do with http://www.auto-hotkey.com/boards/viewtopic.php?f=6&t=254 ?
User avatar
SKAN
Posts: 207
Joined: 29 Sep 2013, 16:58

Re: CPULoad() : CPU usage as percentage

22 Apr 2014, 18:04

I have seen that one.. a good one, but no!

I have written a high speed graphing component ( 24fps ) and needed something to test with ( which will also end up as example script ), and hence CPULoad().
I have been test running the graph for more than an hour and it is yet to use 1 second of "CPU Time" according to Task manager. *Sigh*. Right now, I am writing a test tool that can show the milliseconds used by a process.

Hopefully, I will post a GUI script ( separate topic ) for CPULoad() by tomorrow.

Thanks. :)
User avatar
SKAN
Posts: 207
Joined: 29 Sep 2013, 16:58

Re: CPULoad() : CPU usage as percentage

22 Apr 2014, 19:56

Guest10 wrote:thanks! does this have something to do with http://www.auto-hotkey.com/boards/viewtopic.php?f=6&t=254 ?

Ah, Thanks for pointing it out. I read the code. When Sean wrote it, it did not work for me Win 2000 and I had completely forgotten about it. When I googled today, only the Laszlo's old versions were returned. Wheel recreated! I am getting too old, I guess. :)
User avatar
joedf
Posts: 5500
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: CPULoad() : CPU usage as percentage

22 Apr 2014, 21:18

Nice!
User avatar
Drugwash
Posts: 191
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

30 May 2014, 00:08

Hi, SKAN! Do you by chance have any experience with the Win9x version of getting CPU load (via registry, that is)? I've been trying to use it in my MemPanel script (see repository in my sig) but I'm getting unreal results as if the value is accumulating over time and never settles back to a minimum when no activity. Could it be my system's fault or am I using the readings in a wrong way?

BTW, I got both versions in my script so it can be run (theoretically) on any OS.
User avatar
SKAN
Posts: 207
Joined: 29 Sep 2013, 16:58

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

31 May 2014, 00:39

Hello Drugwash :D, Welcome.

Drugwash wrote:Hi, SKAN! Do you by chance have any experience with the Win9x version of getting CPU load (via registry, that is)?


Unfortunately, no.

my MemPanel script


I am on a slow connection right now. I will try it in a couple of days after I return to city.

:)
User avatar
Drugwash
Posts: 191
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania
Contact:

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

31 May 2014, 07:21

Thank you for the welcome and for taking the time to reply. :) Take your time, there's no rush with my script.
BTW, my connection is most of the time capped at 14kB/s just as in the good ol' dial-up days 15-20 years ago so I hear you. But we're unstoppable anyway. :D
shadowd

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

16 Dec 2014, 06:23

Hi,
is it possible to have cpuload with a exitcode with cpu integer usage value to use in a windows .bat batch file?
Thank so much
User avatar
joedf
Posts: 5500
Joined: 29 Sep 2013, 17:08
Facebook: J0EDF
Google: +joedf
GitHub: joedf
Location: Canada, Quebec
Contact:

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

16 Dec 2014, 08:32

shadowd

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

17 Dec 2014, 09:45

thank you, I tested yout script but in batch windows after i execute script "echo %errorlevel%" return me a 0 value and echo %exitcode% return me "exitcode" (word not number).
I would that script executed from cmd.exe window and after execution give me a numeric value that I can save in a variabile with cmd.exe command set /a var= cpuload
FrostByte
Posts: 6
Joined: 17 Feb 2014, 17:01
Facebook: frostbyte62@yahoo.com

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

29 Dec 2014, 19:21

SKAN, I just wanted to say your scripts are amazing. I've been looking for a way to graph data with AHK for a while (and knew it had to do something with GDI+ but didn't know what to do). Same thing with getting CPU usage...

Again; SKAN, thank you. :)
kalik
Posts: 5
Joined: 16 Dec 2014, 07:24

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

08 Jan 2015, 06:53

Hey, thanks for the script, I have been using it for a bit now. Would it be possible to get a memory usage graph as well?
User avatar
jNizM
Posts: 2158
Joined: 30 Sep 2013, 01:33
GitHub: jNizM

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

08 Jan 2015, 08:15

yes...
use this function for Memory Usage

Code: [Select all] [Download] GeSHi © Codebox Plus

MemoryLoad()
{
static MEMORYSTATUSEX, init := NumPut(VarSetCapacity(MEMORYSTATUSEX, 64, 0), MEMORYSTATUSEX, "uint")
if !(DllCall("GlobalMemoryStatusEx", "ptr", &MEMORYSTATUSEX))
throw Exception("Call to GlobalMemoryStatusEx failed: " A_LastError, -1)
return NumGet(MEMORYSTATUSEX, 4, "UInt")
}
[AHK] 1.1.25.02 x64 Unicode
[WIN] 10 Pro (Version 1607 | Build 14393.1198) x64
My GitHub Profile | Donations are appreciated if I could help you
kalik
Posts: 5
Joined: 16 Dec 2014, 07:24

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

09 Jan 2015, 06:44

Thank you, good sir.

jNizM wrote:yes...
use this function for Memory Usage

Code: [Select all] [Download] GeSHi © Codebox Plus

MemoryLoad()
{
static MEMORYSTATUSEX, init := VarSetCapacity(MEMORYSTATUSEX, 64, 0) && NumPut(64, MEMORYSTATUSEX, "UInt")
if !(DllCall("Kernel32.dll\GlobalMemoryStatusEx", "Ptr", &MEMORYSTATUSEX))
return DllCall("kernel32.dll\GetLastError")
return NumGet(MEMORYSTATUSEX, 4, "UInt")
}
serg
Posts: 35
Joined: 21 Mar 2015, 05:33

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

05 Apr 2015, 08:20

First, big thanks to SKAN for this awesome function!

Is it possible to get process that uses the most CPU, using this code?
I tried it myself - getting all processes and then getting CPU for each of them, but nothing worked out - my programming skills are very limited.

Maybe someone already have code for this? Or can show direction on how it can be done?

Thanks in advance!
loadingx86
Posts: 7
Joined: 16 Jun 2014, 13:42

Re: CPULoad() : CPU usage percentage ( Demos for XGraph )

26 Apr 2017, 23:05

I know the thread is old, but has anyone used XGraph to track CPU/mem usage of a single process with PID as an input ?

Return to “Scripts and Functions”

Who is online

Users browsing this forum: Bon, Flipeador and 11 guests