Page 1 of 1

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

Posted: 22 Apr 2014, 13:05
by SKAN

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" )
}    

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

Re: CPULoad() : CPU usage as percentage

Posted: 22 Apr 2014, 15:37
by Guest10
thanks! does this have something to do with http://www.auto-hotkey.com/boards/viewt ... ?f=6&t=254 ?

Re: CPULoad() : CPU usage as percentage

Posted: 22 Apr 2014, 18:04
by SKAN
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. :)

Re: CPULoad() : CPU usage as percentage

Posted: 22 Apr 2014, 19:56
by SKAN
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. :)

Re: CPULoad() : CPU usage as percentage

Posted: 22 Apr 2014, 21:18
by joedf
Nice!

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

Posted: 05 May 2014, 19:28
by SKAN
Topic updated.

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

Posted: 30 May 2014, 00:08
by Drugwash
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.

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

Posted: 31 May 2014, 00:39
by SKAN
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.

:)

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

Posted: 31 May 2014, 07:21
by Drugwash
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

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

Posted: 16 Dec 2014, 06:23
by shadowd
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

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

Posted: 16 Dec 2014, 08:32
by joedf

Code: Select all

setbatchlines,-1
#include CPULoad.ahk
Exitapp, % CPULoad()

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

Posted: 17 Dec 2014, 09:45
by shadowd
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

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

Posted: 29 Dec 2014, 19:21
by FrostByte
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. :)

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

Posted: 08 Jan 2015, 06:53
by kalik
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?

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

Posted: 08 Jan 2015, 08:15
by jNizM
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")
}

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

Posted: 09 Jan 2015, 06:44
by kalik
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")
}

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

Posted: 05 Apr 2015, 08:20
by serg
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!

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

Posted: 26 Apr 2017, 23:05
by loadingx86
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 ?