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

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

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

22 Apr 2014, 13:05

Code: Select all

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:

Code: Select all

Loop {
  Tooltip % CPULoad()
  Sleep 250
}
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

#NoEnv
#SingleInstance, Force
ListLines, Off
Process, Priority,, High
OnExit, GuiClose

Gui +AlwaysOnTop +ToolWindow 
Gui, Margin, 0, 0
Gui, Add, Text, w610 h111 hwndhGraph  
pGraph := XGraph( hGraph, 0x000044, 1, "5,5,5,5", 0x8080FF )
Gui, Show,, CPU Load Monitor - High Resolution ( One minute window )

SetTimer, XGraph_Plot, 100

F1::Msgbox, 0x1040, XGraph, % XGraph_Info( pGraph ) ; // end of auto-execute section //

XGraph_Plot:
  CPUL := CPULoad()
  XGraph_Plot( pGraph, 100 - CPUL, CPUL )
Return  

GuiClose:
  pGraph := XGraph_Detach( pGraph )
  OnExit
  ExitApp

; -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - 

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 
} 

; -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
  
#Include XGraph.ahk
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

#NoEnv
#SingleInstance, Force
ListLines, Off
OnExit, GuiClose

OnMessage( 0xF, "WM_PAINT")

Gui +AlwaysOnTop
Gui, Margin, 10, 10
Gui, Font, s8, Verdana
Loop, % 11 + ( Y := 15 ) - 15 ; Loop 11 times 
 Gui, Add, Text, xm y%y% w22 h10 0x200 Right, % 125 - (Y += 10)

ColumnW := 10

hBM := XGraph_MakeGrid(  ColumnW, 10, 40, 12, 0x008000, 0, GraphW, GraphH )
Gui, Add, Text, % "xm+25 ym w" ( GraphW + 2 ) " h" ( GraphH + 2 ) " 0x1000" ; SS_SUNKEN := 0x1000
Gui, Add, Text, xp+1 yp+1 w%GraphW% h%GraphH% hwndhGraph gXGraph_GetVal 0xE, pGraph
pGraph := XGraph( hGraph, hBM, ColumnW, "1,10,0,10", 0x00FF00, 1, True )

Gui, Add, StatusBar 
SB_SetParts( 100, 150 )
Gui, Show,, CPU Load Monitor ( One minute window )

SetTimer, XGraph_Plot, 1500
GoTo, XGraph_Plot

F1::MsgbOx, 0x1040, XGraph, % XGraph_Info( pGraph )                   ; // end of auto-execute section //
F2::MsgbOx, 0x1040, XGraph - Array, % XGraph_Info( pGraph, "0.2" )

XGraph_GetVal:
 Value := XGraph_GetVal( pGraph ) 
 If ( Col := ErrorLevel )
   SB_SetText( "`tColumn : " Col, 1 ), SB_SetText( "`tValue : " Value, 2 ) 
Return

XGraph_Paint:
 Sleep -1
 XGraph_Plot( pGraph )
Return

XGraph_Plot:
  CPUL := CPULoad()
  XGraph_Plot( pGraph, 100 - CPUL, CPUL )
Return

GuiClose:
 OnExit
 ExitApp

; -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

WM_PAINT() {
 IfEqual, A_GuiControl, pGraph, SetTimer, XGraph_Paint, -1
}

; -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - 

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 
} 

; -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

#Include xGraph.ahk
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

#SingleInstance, Force
#Include xGraph.ahk
SetWorkingDir %A_ScriptDir%  
OnExit, GuiClose
OnMessage( 0xF, "WM_PAINT")

Gui +AlwaysOnTop
Gui, Margin, 10, 10

Gui, Font, s8 c4D7186, Verdana
Loop, % 21 + ( Y := 15 ) - 15 ; Loop 21 times 
 Gui, Add, Text, xm y%y% w25 h10 0x200 Right, % Abs( 125 - ( Y += 10 ) ) "-"

Gui, Add, Text, xm+25 ym w483 h223 0x1000 ; SS_SUNKEN := 0x1000
Gui, Add, Text, xp+1 yp+1 w481 h221 hwndhGraph, pGraph

hBM := CreateDIB( "E9F5F8|E9F5F8|FFFFFF|FFFFFF|E9F5F8|E9F5F8", 2, 3, 481, 221 )
pGraph := XGraph( hGraph, hBM, 5, "1,10,0,10", 0xAF9977, 1 ) 
Gui, Show,, CPU Load Monitor ( bi-directional spikes )
SetTimer, XGraph_Plot, 200
GoTo, XGraph_Plot

F1::MsgBox, 0x1040, XGraph, % XGraph_Info( pGraph ) ;    // end of auto-execute section //

XGraph_Plot:
  CPUL := CPULoad()
  XGraph_Plot( pGraph, 100 + CPUL, "", False )
  XGraph_Plot( pGraph, 100 - CPUL, "", True  )
Return

GuiClose:
 OnExit
 ExitApp

; -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - 

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 
} 

; -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

CreateDIB( PixelData, W, H, ResizeW := 0, ResizeH := 0, Gradient := 1  ) {      
; http://ahkscript.org/boards/viewtopic.php?t=3203                  SKAN, CD: 01-Apr-2014 MD: 05-May-2014
Static LR_Flag1 := 0x2008 ; LR_CREATEDIBSECTION := 0x2000 | LR_COPYDELETEORG := 8
    ,  LR_Flag2 := 0x200C ; LR_CREATEDIBSECTION := 0x2000 | LR_COPYDELETEORG := 8 | LR_COPYRETURNORG := 4 
    ,  LR_Flag3 := 0x0008 ; LR_COPYDELETEORG := 8

  WB := Ceil( ( W * 3 ) / 2 ) * 2,  VarSetCapacity( BMBITS, WB * H + 1, 0 ),  P := &BMBITS
  Loop, Parse, PixelData, |
    P := Numput( "0x" A_LoopField, P+0, 0, "UInt" ) - ( W & 1 and Mod( A_Index * 3, W * 3 ) = 0 ? 0 : 1 )

  hBM := DllCall( "CreateBitmap", "Int",W, "Int",H, "UInt",1, "UInt",24, "Ptr",0, "Ptr" )    
  hBM := DllCall( "CopyImage", "Ptr",hBM, "UInt",0, "Int",0, "Int",0, "UInt",LR_Flag1, "Ptr" ) 
  DllCall( "SetBitmapBits", "Ptr",hBM, "UInt",WB * H, "Ptr",&BMBITS )

  If not ( Gradient + 0 )
     hBM := DllCall( "CopyImage", "Ptr",hBM, "UInt",0, "Int",0, "Int",0, "UInt",LR_Flag3, "Ptr" )  
Return DllCall( "CopyImage", "Ptr",hBM, "Int",0, "Int",ResizeW, "Int",ResizeH, "Int",LR_Flag2, "UPtr" )
}    

; -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -
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/viewt ... ?f=6&t=254 ?
User avatar
SKAN
Posts: 333
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: 333
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/viewt ... ?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: 6682
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!
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 3.00 GHz, 16GB DDR4 3200 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
User avatar
Drugwash
Posts: 640
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

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.
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.
User avatar
SKAN
Posts: 333
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: 640
Joined: 29 May 2014, 21:07
Location: Ploieşti, Romania

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
I've deleted my CloudMe account because of GDPR - the now legal base for privacy invasion and data theft.
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: 6682
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

Code: Select all

setbatchlines,-1
#include CPULoad.ahk
Exitapp, % CPULoad()
Image Image Image Image Image
Windows 10 x64 Professional, Intel i5-8500 @ 3.00 GHz, 16GB DDR4 3200 MHz, NVIDIA GTX 1060 6GB | [About Me] | [ASPDM - StdLib Distribution]
[Populate the AHK MiniCity!] | [Qonsole - Quake-like console emulator] | [LibCon - Autohotkey Console Library] | [About the AHK Foundation]
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: 2341
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

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

08 Jan 2015, 08:15

yes...
use this function for Memory Usage

Code: Select all

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.30.01 x64 Unicode | [WIN] 10 Pro (Version 1803) x64 | [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

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: [Shambles], Drugwash and 27 guests