-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathCPULoad.ahk
75 lines (60 loc) · 2.05 KB
/
CPULoad.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
;Get CPU load
;by shimanov
;www.autohotkey.com/forum/viewtopic.php?t=5064
;https://autohotkey.com/board/topic/7127-monitor-when-cpu-becomes-idle/
/* EXAMPLE
#Persistent
SetTimer, CheckCPULoad, 1000
return
CheckCPULoad:
SetFormat, float, 0.0
ToolTip, % "CPULoad = " GetCPULoad() "%"
return
*/
GetCPULoad_Short() {
Static IdleTime, Tick
SetBatchLines, -1
OldIdleTime = %IdleTime%
OldTick = %Tick%
DllCall("kernel32.dll\GetSystemTimes", "uint",&IdleTicks, "uint",0, "uint",0)
IdleTime := *(&IdleTicks)
Loop 7
IdleTime += *( &IdleTicks + A_Index ) << ( 8 * A_Index )
Tick := A_TickCount
Return 100 - 0.01*(IdleTime - OldIdleTime)/(Tick - OldTick)
}
GetCPULoad() {
Static LastStartTickCount, LastLowTickCounts, LastHighTickCounts
SetBatchLines, -1
;get tickcounts between this and the last call of the subroutine
StartTickCount := A_TickCount
ElapsedTickCounts := StartTickCount - LastStartTickCount
;assume CPU load is zero
CPULOAD = 0
;get information from system idle time (i.e., the OS idle process)
VarSetCapacity( Ticks, 4+4 )
DllCall( "kernel32.dll\GetSystemTimes", "uint", &Ticks, "uint", 0, "uint", 0 )
LowTickCounts := ReadInteger( &Ticks, 0, 4 )
HighTickCounts := ReadInteger( &Ticks, 4, 4 )
;calculate CPU load after second call
If LastStartTickCount
{
Idle_ticks := ( HighTickCounts - LastHighTickCounts ) << 32
Idle_ticks := Idle_ticks + LowTickCounts - LastLowTickCounts
CPULOAD := 100 - Idle_ticks / ElapsedTickCounts / 100
}
;remember current values for next call
LastStartTickCount := StartTickCount
LastLowTickCounts := LowTickCounts
LastHighTickCounts := HighTickCounts
Return, CPULOAD
}
ReadInteger( Address, Offset, Size ) {
old_FormatInteger := A_FormatInteger
SetFormat, Integer, hex
value = 0
Loop, %Size%
value := value + ( *( ( Address + Offset ) + ( A_Index - 1 ) ) << ( 8 * ( A_Index - 1 ) ) )
SetFormat, Integer, %old_FormatInteger%
Return, value
}