找回密码
 加入
搜索
查看: 5611|回复: 13

[AU3基础] 怎么样获得某个进程CPU使用率

  [复制链接]
发表于 2011-7-26 21:09:24 | 显示全部楼层 |阅读模式
$wbemServices = ObjGet("winmgmts:\\.\root\cimv2")
$wbemObjectSet= $wbemServices.ExecQuery("Select * from Win32_Processor")
For $wbemObject In $wbemObjectSet
    $dd=$wbemObject.LoadPercentage
Next
MsgBox(0,"cpu使用率:",$dd)


这个是获得全部的使用
 楼主| 发表于 2011-7-26 21:20:31 | 显示全部楼层
我要单个进的CPU使用率
发表于 2011-7-26 21:30:49 | 显示全部楼层
感觉好久没有来了,帮顶....
发表于 2011-7-26 22:23:42 | 显示全部楼层
;coded by UEZ
#include <WindowsConstants.au3>
#include <array.au3>

Opt('MustDeclareVars', 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")

Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword")
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $FileTime = DllStructCreate("dword;dword")
Global $SystemTime = DllStructCreate("ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort")
Global $IdleTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret, $hour, $minutes, $seconds, $milliseconds
Global $ProcHandle, $Process_CPU_Usage
Global $PID, $CPUTime, $mem, $prg
Global $logical_cpus = CPU()
Global $hProc
Global $GUI, $info

Global $hFile, $au3, $autoit3

Local $list = ProcessList("QQ.exe")
If $list[0][0]=1 Then $PID=$list[1][1]

If $PID = 0 Then
    ConsoleWrite(@CRLF & "ERROR! Process " & $prg & " not found! Aborting..." & @CRLF)
    Exit
EndIf

Global $iMemo, $hDebugGUI
$GUI = GUICreate("进程的CPU占用率  UEZ - Press ESC to quit! ", 740, 25, -1, -1, BitOR($WS_CAPTION, $WS_POPUP, $WS_BORDER, $WS_CLIPSIBLINGS))
$iMemo = GUICtrlCreateEdit("", -1, -1, 820)
GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
GUICtrlSetBkColor($iMemo, 0xFFFFFF)
GUISetState()

$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]

;~ ConsoleWrite(@CRLF & @CRLF & "Process Information v0.30 Beta by UEZ" & @CRLF)
;~ ConsoleWrite("=====================================" & @CRLF & @CRLF)
;~ ConsoleWrite("(Press ESC to exit)" & @CRLF)

While True
    $CPUTime = _GetProcTime($ProcHandle)
    $mem = _ProcessGetMem($ProcHandle) / 1024
    $hour = Int($CPUTime / 10000000 / 3600)

    $minutes = Int($CPUTime / 10000000 / 60)
    If $minutes < 10 Then $minutes = "0" & $minutes

    $seconds = Round((Int($CPUTime / 10000000) / 60 - Int($CPUTime / 10000000 / 60)) * 60, 0)
    If $seconds < 10 Then $seconds = "0" & $seconds

    $milliseconds = Round((Int($CPUTime / 10000) / 1000 - Int(Int($CPUTime / 10000) / 1000)) * 1000, 0)
    If $milliseconds < 10 Then $milliseconds = "00" & $milliseconds
    If $milliseconds > 9 And $milliseconds < 100 Then $milliseconds = "0" & $milliseconds

    ;$info = @CR & $prg & " | PID: " & $PID & " | CPU time: " & $hour & ":" & $minutes & ":" & $seconds & "." & $milliseconds & " (h:m:s.ms) | Mem usage: " & $mem & " KB | CPU usage: " & $Process_CPU_Usage & " % " & @CR
    $info="CPU usage: " & $Process_CPU_Usage & " % " & @CR
        MemoWrite($info)

;~  ConsoleWrite($prg & " | PID: " & $PID & " | CPU time: " & $hour & ":" & $minutes & ":" & $seconds & "." & $milliseconds & " (h:m:s.ms) | Mem usage: " & $mem & " KB | CPU usage: " & $Process_CPU_Usage & " % " & @CR)
    Sleep(1000)
    If Not ProcessExists($PID) Then _Exit()
WEnd


Func MemoWrite($sMessage = "")
    GUICtrlSetData($iMemo, $sMessage)
EndFunc   ;==>MemoWrite

Func _GetProcTime($ProcessHandle)
    $ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
    If $ret[0] = 0 Then
        ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
        SetError(1, 0, $ret[0])
    EndIf
    $sKernelTime = DllStructGetData($KernelTime, 1)
    $sUserTime = DllStructGetData($UserTime, 1)
    $Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
    If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
    $eKernelTime = $sKernelTime
    $eUserTime = $sUserTime
    Return $sUserTime + $sKernelTime
EndFunc   ;==>_GetProcTime

Func _ProcessGetMem($ProcessHandle) ;get physical memory of the process -> http://msdn.microsoft.com/en-us/library/ms683219%28VS.85%29.aspx
    Local $structPROCESS_MEMORY_COUNTERS, $structPROCESS_MEMORY_COUNTERS_EX, $nSize, $aRet
    If @OSVersion <> "WIN_7" Then
        $structPROCESS_MEMORY_COUNTERS = DllStructCreate("dword cb; dword PageFaultCount; dword_ptr PeakWorkingSetSize; dword_ptr WorkingSetSize; " & _
                "dword_ptr QuotaPeakPagedPoolUsage; dword_ptr QuotaPagedPoolUsage; dword_ptr QuotaPeakNonPagedPoolUsage; " & _
                "dword_ptr QuotaNonPagePoolUsage; dword_ptr PagefileUsage; dword_ptr PeakPagefileUsage") ;http://msdn.microsoft.com/en-us/library/ms684877%28VS.85%29.aspx
        $nSize = DllStructGetSize($structPROCESS_MEMORY_COUNTERS)
        $aRet = DllCall("psapi.dll", "int", "GetProcessMemoryInfo", "hwnd", $ProcessHandle, "ptr", DllStructGetPtr($structPROCESS_MEMORY_COUNTERS), "dword", $nSize) ;call GetProcessMemoryInfo
        If $aRet[0] = 0 Then
            ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessMemoryInfo call" & @LF)
            SetError(1, 0, $aRet[0])
        EndIf
        Return DllStructGetData($structPROCESS_MEMORY_COUNTERS, "WorkingSetSize")
    Else
        $structPROCESS_MEMORY_COUNTERS_EX = DllStructCreate("dword cb; dword PageFaultCount; dword_ptr PeakWorkingSetSize; dword_ptr WorkingSetSize; " & _
                "dword_ptr QuotaPeakPagedPoolUsage; dword_ptr QuotaPagedPoolUsage; dword_ptr QuotaPeakNonPagedPoolUsage; " & _
                "dword_ptr QuotaNonPagePoolUsage; dword_ptr PagefileUsage; dword_ptr PeakPagefileUsage; " & _
                "dword_ptr PrivateUsage") ;http://msdn.microsoft.com/en-us/library/ms684877%28VS.85%29.aspx
        $nSize = DllStructGetSize($structPROCESS_MEMORY_COUNTERS_EX)
        $aRet = DllCall("Kernel32.dll", "int", "K32GetProcessMemoryInfo", "hwnd", $ProcessHandle, "ptr", DllStructGetPtr($structPROCESS_MEMORY_COUNTERS_EX), "dword", $nSize) ;call GetProcessMemoryInfo
        If $aRet[0] = 0 Then
            ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessMemoryInfo call" & @LF)
            SetError(1, 0, $aRet[0])
        EndIf
;~  ConsoleWrite("WorkingSetSize: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "WorkingSetSize") / 1024, 0) & @CRLF & _
;~  "PrivateUsage: " & Round(DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PrivateUsage") / 1024, 0) & @CRLF & @CRLF)
        Return DllStructGetData($structPROCESS_MEMORY_COUNTERS_EX, "PrivateUsage")
    EndIf
EndFunc   ;==>_ProcessGetMem

Func CPU() ;get logical CPU(s)
    Local $sLPSystemInfo = DllStructCreate( "ushort dwOemId;" & _
                                                                            "short wProcessorArchitecture;" & _
                                                                            "dword dwPageSize;" & _
                                                                            "ptr lpMinimumApplicationAddress;" & _
                                                                            "ptr lpMaximumApplicationAddress;" & _
                                                                            "long_ptr dwActiveProcessorMask;" & _
                                                                            "dword dwNumberOfProcessors;" & _
                                                                            "dword dwProcessorType;" & _
                                                                            "dword dwAllocationGranularity;" & _
                                                                            "short wProcessorLevel;" & _
                                                                            "short wProcessorRevision")
    Local $aResult = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr",DllStructGetPtr($sLPSystemInfo))
    If @error Or Not IsArray($aResult) Then Return SetError(1, 0, 0)
    Return SetError(0, 0, DllStructGetData($sLPSystemInfo, "dwNumberOfProcessors"))
EndFunc   ;==>CPU

Func Remove_Last_Comma($str)
    If StringRight($str, 2) = ", " Then $str = StringMid($str, 1, StringLen($str) - 2)
    Return $str
EndFunc   ;==>Remove_Last_Comma

Func _Exit()
    DllCall("kernel32.dll", "int", "CloseHandle", "int", $ProcHandle)
    DllCall("psapi.dll", "int", "CloseHandle", "hwnd", $ProcHandle)
    ProcessClose($PID)
    Exit
EndFunc   ;==>_Exit

点评

这个完美解决指定进程CPU获取问题!谢谢!  发表于 2020-10-5 18:16
发表于 2011-7-27 03:43:26 | 显示全部楼层
回复 4# 3mile
不知道这样好些,还是pdh好些
 楼主| 发表于 2011-7-27 08:37:14 | 显示全部楼层
回复 4# 3mile

这个是获取单个进程的吗?  谢谢
 楼主| 发表于 2011-7-27 08:41:52 | 显示全部楼层
回复 4# 3mile

    哥哥你好强大啊,要向你学习
发表于 2011-7-27 10:54:40 | 显示全部楼层
3M哥就是强。。。
 楼主| 发表于 2011-7-27 21:45:58 | 显示全部楼层
改成这样子,还是有点不懂
#include <array.au3>
#include <file.au3>
Opt('MustDeclareVars', 1)
Opt("TrayIconHide", 1)
HotKeySet("{ESC}", "_Exit")
        Dim $log ="cpu.log"
Global Const $Process_All_Access = 0x1F0FFF
Global $CreateTime = DllStructCreate("dword;dword")
Global $ExitTime = DllStructCreate("dword;dword")
Global $KernelTime = DllStructCreate("dword;dword")
Global $UserTime = DllStructCreate("dword;dword")
Global $sUserTime, $sKernelTime, $eUserTime, $eKernelTime
Global $ret
Global $ProcHandle, $Process_CPU_Usage
Global $PID, $CPUTime
Global $logical_cpus = CPU()
Global $info
_FileWriteLog(@ScriptDir & $log,"$logical_cpus"& $logical_cpus )

Local $list = ProcessList("MyPopo.exe")
If $list[0][0]=1 Then $PID=$list[1][1]
$ProcHandle = DllCall("kernel32.dll", "hwnd", "OpenProcess", "dword", $Process_All_Access, "int", False, "dword", $PID)
$ProcHandle = $ProcHandle[0]
While True
    $CPUTime = _GetProcTime($ProcHandle)
    $info="CPU usage: " & $Process_CPU_Usage & " % " & @CRLF
        _FileWriteLog(@ScriptDir & $log,$info )
    Sleep(1000)
    If Not ProcessExists($PID) Then _Exit()
WEnd
Func _GetProcTime($ProcessHandle)
    $ret = DllCall("kernel32.dll", "int", "GetProcessTimes", "int", $ProcessHandle, "ptr", DllStructGetPtr($CreateTime), "ptr", DllStructGetPtr($ExitTime), "ptr", DllStructGetPtr($KernelTime), "ptr", DllStructGetPtr($UserTime))
    If $ret[0] = 0 Then
        ConsoleWrite("(" & @ScriptLineNumber & ") : = Error in GetProcessTimes call" & @CRLF)
        SetError(1, 0, $ret[0])
    EndIf
    $sKernelTime = DllStructGetData($KernelTime, 1)
    $sUserTime = DllStructGetData($UserTime, 1)
    $Process_CPU_Usage = Floor(($sKernelTime - $eKernelTime + $sUserTime - $eUserTime) / 100000 / $logical_cpus)
    If $Process_CPU_Usage > 100 Then $Process_CPU_Usage = "100"
    $eKernelTime = $sKernelTime
    $eUserTime = $sUserTime
    Return $sUserTime + $sKernelTime
EndFunc   
Func CPU()
    Local $sLPSystemInfo = DllStructCreate( "ushort dwOemId;" & "short wProcessorArchitecture;" & "dword dwPageSize;" & _
                                                                            "ptr lpMinimumApplicationAddress;" & _
                                                                            "ptr lpMaximumApplicationAddress;" & _
                                                                            "long_ptr dwActiveProcessorMask;" & _
                                                                            "dword dwNumberOfProcessors;" & _
                                                                            "dword dwProcessorType;" & _
                                                                            "dword dwAllocationGranularity;" & _
                                                                            "short wProcessorLevel;" & _
                                                                            "short wProcessorRevision")
    Local $aResult = DllCall("Kernel32.dll", "none", "GetSystemInfo", "ptr",DllStructGetPtr($sLPSystemInfo))
    If @error Or Not IsArray($aResult) Then Return SetError(1, 0, 0)
    Return SetError(0, 0, DllStructGetData($sLPSystemInfo, "dwNumberOfProcessors"))
EndFunc   
Func _Exit()
    Exit
EndFunc
 楼主| 发表于 2011-7-27 21:46:53 | 显示全部楼层
Func _GetProcTime($ProcessHandle)
这个不太懂
 楼主| 发表于 2011-7-27 21:46:53 | 显示全部楼层
Func _GetProcTime($ProcessHandle)
这个不太懂
 楼主| 发表于 2011-7-27 21:50:05 | 显示全部楼层
回复 4# 3mile
有点不太理解,好多看不懂是什么意思,自己好菜啊
发表于 2020-10-5 18:17:17 | 显示全部楼层
3mile 发表于 2011-7-26 22:23
[au3];coded by UEZ
#include
#include

这个完美解决指定进程CPU获取问题!谢谢!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-6-2 00:04 , Processed in 0.082694 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表