添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
潇洒的弓箭  ·  Measure process CPU ...·  1 月前    · 
沉着的水桶  ·  [Windows 10] ...·  1 月前    · 
年轻有为的茴香  ·  Coverity ...·  1 月前    · 
慷慨大方的柚子  ·  2019 Satoshi Gogo ...·  2 月前    · 
鬼畜的柚子  ·  Always get error ...·  3 月前    · 
I was searching on the forum for a script to measure the cpu usage of a process, I have found this one:

Code: Select all

#Persistent
CoordMode, ToolTip, Screen
Process, Exist, Waveosaur.1.0.0.7000.exe
PID := errorLevel
IfEqual, PID, 0, ExitApp
SetTimer, ShowToolTip
Return
ShowToolTip:
 PrLoad := Round( GetProcessTimes(PID),2)
 ToolTip %PrLoad%`%, 10,10
Return
GetProcessTimes( PID=0 )    { 
   Static oldKrnlTime, oldUserTime 
   Static newKrnlTime, newUserTime 
   oldKrnlTime := newKrnlTime 
   oldUserTime := newUserTime 
   hProc := DllCall("OpenProcess", "Uint", 0x400, "int", 0, "Uint", pid) 
   DllCall("GetProcessTimes", "Uint", hProc, "int64P", CreationTime, "int64P"
           , ExitTime, "int64P", newKrnlTime, "int64P", newUserTime) 
   DllCall("CloseHandle", "Uint", hProc) 
Return (newKrnlTime-oldKrnlTime + newUserTime-oldUserTime)/10000000 * 100   
But the value returned is not correctly. Does someone have or know any?

Code: Select all

#SingleInstance Force
proc = procexp64.exe ; Change to whatever process you want to check
SplashTextOn, 300, 100, Working, `nPlease wait....
Process, Exist, %proc%
pid := ErrorLevel, load := [], n := 0
Loop {
 If (load[Mod(++n, 4) + 1] := getProcessTimes(pid)) = -2 {
  SplashTextOff
  MsgBox, 48, Error, Process was not found. Aborting.`n`n%proc%
  ExitApp
 If (n = 4)
  SplashTextOff
 If !Mod(n, 4)
  ToolTip, % Format("{:0.2f}%", (load.1 + load.2 + load.3 + load.4) / 4)
 Sleep, 350
Return
getProcessTimes(PID) {
 ; RHCP https://autohotkey.com/board/topic/113942-solved-get-cpu-usage-in/
 ; Return values
 ; -1 on first run 
 ; -2 if process doesn't exist or you don't have access to it
 ; Process cpu usage as percent of total CPU
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=73498
    static aPIDs := [], hasSetDebug
    ; If called too frequently, will get mostly 0%, so it's better to just return the previous usage 
    if aPIDs.HasKey(PID) && A_TickCount - aPIDs[PID, "tickPrior"] < 250
        return aPIDs[PID, "usagePrior"] 
   	; Open a handle with PROCESS_QUERY_LIMITED_INFORMATION access
    if !hProc := DllCall("OpenProcess", "UInt", 0x1000, "Int", 0, "Ptr", pid, "Ptr")
        return -2, aPIDs.HasKey(PID) ? aPIDs.Remove(PID, "") : "" ; Process doesn't exist anymore or don't have access to it.
    DllCall("GetProcessTimes", "Ptr", hProc, "Int64*", lpCreationTime, "Int64*", lpExitTime, "Int64*", lpKernelTimeProcess, "Int64*", lpUserTimeProcess)
    DllCall("CloseHandle", "Ptr", hProc)
    DllCall("GetSystemTimes", "Int64*", lpIdleTimeSystem, "Int64*", lpKernelTimeSystem, "Int64*", lpUserTimeSystem)
    if aPIDs.HasKey(PID) ; check if previously run
        ; find the total system run time delta between the two calls
        systemKernelDelta := lpKernelTimeSystem - aPIDs[PID, "lpKernelTimeSystem"] ;lpKernelTimeSystemOld
        systemUserDelta := lpUserTimeSystem - aPIDs[PID, "lpUserTimeSystem"] ; lpUserTimeSystemOld
        ; get the total process run time delta between the two calls 
        procKernalDelta := lpKernelTimeProcess - aPIDs[PID, "lpKernelTimeProcess"] ; lpKernelTimeProcessOld
        procUserDelta := lpUserTimeProcess - aPIDs[PID, "lpUserTimeProcess"] ;lpUserTimeProcessOld
        ; sum the kernal + user time
        totalSystem :=  systemKernelDelta + systemUserDelta
        totalProcess := procKernalDelta + procUserDelta
        ; The result is simply the process delta run time as a percent of system delta run time
        result := 100 * totalProcess / totalSystem
    else result := -1
    aPIDs[PID, "lpKernelTimeSystem"] := lpKernelTimeSystem
    aPIDs[PID, "lpKernelTimeSystem"] := lpKernelTimeSystem
    aPIDs[PID, "lpUserTimeSystem"] := lpUserTimeSystem
    aPIDs[PID, "lpKernelTimeProcess"] := lpKernelTimeProcess
    aPIDs[PID, "lpUserTimeProcess"] := lpUserTimeProcess
    aPIDs[PID, "tickPrior"] := A_TickCount
    return aPIDs[PID, "usagePrior"] := result 
getProcessTimes(Process) {
	 ; RHCP https://autohotkey.com/board/topic/113942-solved-get-cpu-usage-in/
	 ; Return values
	 ; -1 on first run 
	 ; -2 if process doesn't exist or you don't have access to it
	 ; Process cpu usage as percent of total CPU
	 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=73498&p=317460&hilit=cpu+process+load#p317460
	Process, Exist, %Process%
	PID := ErrorLevel
	If (PID == 0)
		Return 0
    static aPIDs := [], hasSetDebug
    ; If called too frequently, will get mostly 0%, so it's better to just return the previous usage 
    if aPIDs.HasKey(PID) && A_TickCount - aPIDs[PID, "tickPrior"] < 250
        return aPIDs[PID, "usagePrior"] 
   	; Open a handle with PROCESS_QUERY_LIMITED_INFORMATION access
    if !hProc := DllCall("OpenProcess", "UInt", 0x1000, "Int", 0, "Ptr", pid, "Ptr")
        return -2, aPIDs.HasKey(PID) ? aPIDs.Remove(PID, "") : "" ; Process doesn't exist anymore or don't have access to it.
    DllCall("GetProcessTimes", "Ptr", hProc, "Int64*", lpCreationTime, "Int64*", lpExitTime, "Int64*", lpKernelTimeProcess, "Int64*", lpUserTimeProcess)
    DllCall("CloseHandle", "Ptr", hProc)
    DllCall("GetSystemTimes", "Int64*", lpIdleTimeSystem, "Int64*", lpKernelTimeSystem, "Int64*", lpUserTimeSystem)
    if aPIDs.HasKey(PID) ; check if previously run
        ; find the total system run time delta between the two calls
        systemKernelDelta := lpKernelTimeSystem - aPIDs[PID, "lpKernelTimeSystem"] ;lpKernelTimeSystemOld
        systemUserDelta := lpUserTimeSystem - aPIDs[PID, "lpUserTimeSystem"] ; lpUserTimeSystemOld
        ; get the total process run time delta between the two calls 
        procKernalDelta := lpKernelTimeProcess - aPIDs[PID, "lpKernelTimeProcess"] ; lpKernelTimeProcessOld
        procUserDelta := lpUserTimeProcess - aPIDs[PID, "lpUserTimeProcess"] ;lpUserTimeProcessOld
        ; sum the kernal + user time
        totalSystem :=  systemKernelDelta + systemUserDelta
        totalProcess := procKernalDelta + procUserDelta
        ; The result is simply the process delta run time as a percent of system delta run time
        result := 100 * totalProcess / totalSystem
    else result := -1
    aPIDs[PID, "lpKernelTimeSystem"] := lpKernelTimeSystem
    aPIDs[PID, "lpKernelTimeSystem"] := lpKernelTimeSystem
    aPIDs[PID, "lpUserTimeSystem"] := lpUserTimeSystem
    aPIDs[PID, "lpKernelTimeProcess"] := lpKernelTimeProcess
    aPIDs[PID, "lpUserTimeProcess"] := lpUserTimeProcess
    aPIDs[PID, "tickPrior"] := A_TickCount
    return aPIDs[PID, "usagePrior"] := result