gpt4 book ai didi

windows - 获取 CPU 使用率(%)

转载 作者:可可西里 更新时间:2023-11-01 10:04:28 25 4
gpt4 key购买 nike

我想使用 AutoIt 将 CPU 使用率(以 % 为单位)传递给一个变量。有没有像MemGetStats()这样的功能为此?

最佳答案

WMI 很慢。 The following solution返回速度更快,并包含每个处理器内核的信息:

$aUsage = _GetCPUUsage()
For $i = 1 To $aUsage[0]
ConsoleWrite('CPU #' & $i & ' - ' & $aUsage[$i] & '%' & @CRLF)
Next

;#####################################################################
;# Function: _GetCPUUsage()
;# Gets the utilization of the CPU, compatible with multicore
;# Return: Array
;# Array[0] Count of CPU, error if negative
;# Array[n] Utilization of CPU #n in percent
;# Error: -1 Error at 1st Dll-Call
;# -2 Error at 2nd Dll-Call
;# -3 Error at 3rd Dll-Call
;# Author: Bitboy (AutoIt.de)
;#####################################################################
Func _GetCPUUsage()
Local Const $SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION = 8
Local Const $SYSTEM_TIME_INFO = 3
Local Const $tagS_SPPI = "int64 IdleTime;int64 KernelTime;int64 UserTime;int64 DpcTime;int64 InterruptTime;long InterruptCount"

Local $CpuNum, $IdleOldArr[1],$IdleNewArr[1], $tmpStruct
Local $timediff = 0, $starttime = 0
Local $S_SYSTEM_TIME_INFORMATION, $S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
Local $RetArr[1]

Local $S_SYSTEM_INFO = DllStructCreate("ushort dwOemId;short wProcessorArchitecture;dword dwPageSize;ptr lpMinimumApplicationAddress;" & _
"ptr lpMaximumApplicationAddress;long_ptr dwActiveProcessorMask;dword dwNumberOfProcessors;dword dwProcessorType;dword dwAllocationGranularity;" & _
"short wProcessorLevel;short wProcessorRevision")

$err = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr",DllStructGetPtr($S_SYSTEM_INFO))

If @error Or Not IsArray($err) Then
Return $RetArr[0] = -1
Else
$CpuNum = DllStructGetData($S_SYSTEM_INFO, "dwNumberOfProcessors")
ReDim $RetArr[$CpuNum+1]
$RetArr[0] = $CpuNum
EndIf
$S_SYSTEM_INFO = 0

While 1
$S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION = DllStructCreate($tagS_SPPI)
$StructSize = DllStructGetSize($S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)
$S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION = DllStructCreate("byte puffer[" & $StructSize * $CpuNum & "]")
$pointer = DllStructGetPtr($S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION)

$err = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", _
"int", $SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION, _
"ptr", DllStructGetPtr($S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), _
"int", DllStructGetSize($S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION), _
"int", 0)

If $err[0] Then
Return $RetArr[0] = -2
EndIf

Local $S_SYSTEM_TIME_INFORMATION = DllStructCreate("int64;int64;int64;uint;int")
$err = DllCall("ntdll.dll", "int", "NtQuerySystemInformation", _
"int", $SYSTEM_TIME_INFO, _
"ptr", DllStructGetPtr($S_SYSTEM_TIME_INFORMATION), _
"int", DllStructGetSize($S_SYSTEM_TIME_INFORMATION), _
"int", 0)

If $err[0] Then
Return $RetArr[0] = -3
EndIf

If $starttime = 0 Then
ReDim $IdleOldArr[$CpuNum]
For $i = 0 to $CpuNum -1
$tmpStruct = DllStructCreate($tagS_SPPI, $Pointer + $i*$StructSize)
$IdleOldArr[$i] = DllStructGetData($tmpStruct,"IdleTime")
Next
$starttime = DllStructGetData($S_SYSTEM_TIME_INFORMATION, 2)
Sleep(100)
Else
ReDim $IdleNewArr[$CpuNum]
For $i = 0 to $CpuNum -1
$tmpStruct = DllStructCreate($tagS_SPPI, $Pointer + $i*$StructSize)
$IdleNewArr[$i] = DllStructGetData($tmpStruct,"IdleTime")
Next

$timediff = DllStructGetData($S_SYSTEM_TIME_INFORMATION, 2) - $starttime

For $i=0 to $CpuNum -1
$RetArr[$i+1] = Round(100-(($IdleNewArr[$i] - $IdleOldArr[$i]) * 100 / $timediff))
Next

Return $RetArr
EndIf

$S_SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION = 0
$S_SYSTEM_TIME_INFORMATION = 0
$tmpStruct = 0
WEnd
EndFunc

关于windows - 获取 CPU 使用率(%),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37320547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com