AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Anyone know of a way to get the the cpu usage of a process?

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
acowbear



Joined: 14 May 2006
Posts: 45

PostPosted: Thu May 03, 2007 11:44 pm    Post subject: Anyone know of a way to get the the cpu usage of a process? Reply with quote

-What I'm doing....
I'm trying to track and record the cpu usage of a process (Waveosaur.1.0.0.7000.exe) in the background while I work and then send me an msgbox if it ever goes over a certain number...

-What my problem is...
How can I get the cpu usage of that process??
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 413

PostPosted: Fri May 04, 2007 9:10 am    Post subject: Reply with quote

Give this a whirl. It takes the info from Task Manager, running it if it's not already open. I looked for a way to get cpu usage with API calls, but it looks very difficult if not impossible in AHK. (I just know someone's going to prove me wrong!)

Code:
;~ Select "Hide when Minimized" under "Options" in the Task Manager
;~ menubar if you want Task Manager in the tasktray.
;~ While this is running, you can't select any other tabs, it
;~ forces the process tab.
#Persistent
DetectHiddenWindows,On
Menu,Tray,Icon,%A_WinDir%\system32\shell32.dll,13


Tracktitle =      ;title of window to track
TrackProcess = firefox.exe   ;Waveosaur.1.0.0.7000.exe   ;process to track
;one or the other, title overrides process

TrackPID_Col = 2   ;column of pids
TrackCPU_Col = 4   ;column of cpu usage
TrackLIMIT = 75   ;percent not to exceed


If(Tracktitle)   ;if user has specified title
{   WinGet, TrackPID, PID, %Tracktitle%    ;get pid of window to track
}
Else   ;otherwise specified process
{
   Process, Exist, %TrackProcess%
   TrackPID := ErrorLevel
   Tracktitle := TrackProcess
}
title = Windows Task Manager ahk_class #32770   ;info for task manager
IfWinNotExist,%title%   ;if task manager not running
{   Run, %A_WinDir%\system32\taskmgr.exe,,Minimize,outPID
   WinWaitActive, ahk_pid %outPID%,,5
   sleep,2000   ;wait for 2 seconds to ensure that the task manager has initialized
}

SetTimer, getTrack, 500   ;checks every half second
Return

getTrack:
   ControlGet,tabNum, Tab,,SysTabControl321, %title%
   If(tabNum != 2)   ;has to be processes tab, 2
   {   ;need tab two, but it's 0 based, so select 1
      SendMessage, 0x130C, 1,, SysTabControl321, %title%  ; 0x130C is TCM_SETCURSEL
      ControlSend, systabcontrol321, {left}, %title%   ;workaround to make tab selection work
      ControlSend, systabcontrol321, {right}, %title%
   }

   ControlGet, listPID, List, col%TrackPID_Col%, SysListView321, %title%   ;get pid column from listview
   ControlGet, listCPU, List, col%TrackCPU_Col%, SysListView321, %title%   ;get cpu column from listview
   StringSplit,PID, listPID, `n   ;%A_Tab%   ;split into array
   StringSplit,CPU, listCPU, `n   ;%A_Tab%   ;split into array
   Loop
   {   If(PID%A_Index% = TrackPID)
      {   CPU := CPU%A_Index%   ;get current CPU usage
         Break   ;if found it, Break
      }
      IfGreater,A_Index,200,Break   ;probably nobody has more than 200 processes running
   }
   ;tooltip,%CPU%   ;for testing
   IfGreater,CPU,%TrackLIMIT%,msgbox,262208,CPUtracker v1.0,%Tracktitle% has exceeded %TrackLIMIT%`%.
Return

_________________
Back to top
View user's profile Send private message
acowbear



Joined: 14 May 2006
Posts: 45

PostPosted: Thu May 10, 2007 1:11 pm    Post subject: Reply with quote

Hey, thanks! It didn't work for me when I tried it, but after I made a few small changes it worked fine. The titles and cpu columns must be in a different place on my computer because I needed to change
TrackPID_Col = 2 ;column of pids
TrackCPU_Col = 4 ;column of cpu usage
to...
TrackPID_Col = 1 ;column of pids
TrackCPU_Col = 3 ;column of cpu usage
and also I changed
"If(PID%A_Index% = TrackPID)"
to...
"If(PID%A_Index% = TrackProcess)"
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Thu May 10, 2007 1:27 pm    Post subject: Re: Anyone know of a way to get the the cpu usage of a proce Reply with quote

acowbear wrote:
I'm trying to track and record the cpu usage of a process (Waveosaur.1.0.0.7000.exe) in the background while I work and then send me an msgbox if it ever goes over a certain number...


Code:
#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   
}


GetProcessTimes() by Sean : http://www.autohotkey.com/forum/viewtopic.php?p=119695#119695

Smile
Back to top
View user's profile Send private message
Micahs



Joined: 01 Dec 2006
Posts: 413

PostPosted: Fri May 11, 2007 3:23 am    Post subject: Reply with quote

Code:
I just know someone's going to prove me wrong!

See? I was right! Very Happy
_________________
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Fri May 11, 2007 7:03 am    Post subject: Reply with quote

Micahs wrote:
See? I was right! Very Happy


But no feedback from acowbear Rolling Eyes Sad
Back to top
View user's profile Send private message
acowbear



Joined: 14 May 2006
Posts: 45

PostPosted: Sat May 26, 2007 12:26 am    Post subject: Reply with quote

AAAHHHHHHHHHHHHHHHH!!!!!!!
So Awsome I think I just might explode!!!!!!!

Sorry fot the late reply. I just now noticed that I got more responses on this string. Great script! Thanks!
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Sat May 26, 2007 5:56 am    Post subject: Reply with quote

acowbear wrote:
I just now noticed that I got more responses on this string


You may visit your Profile, and set the
Always notify me of replies: to Yes.
That way you will receive an email alert for the topics that you have posted in.

Smile
Back to top
View user's profile Send private message
acowbear



Joined: 14 May 2006
Posts: 45

PostPosted: Tue May 29, 2007 1:20 am    Post subject: Reply with quote

Thanks. I looked and it was set to notify me...but it was using an obolete email address!! Updated it to my current email.
Back to top
View user's profile Send private message
Maxanna
Guest





PostPosted: Sat Dec 01, 2007 8:52 am    Post subject: Reply with quote

looked at the script, almost what I'm looking for.

I'm trying to make a script where if a certain process (we'll call it abc.exe for example) uses ANY CPU at all, it changes the color of the script icon in the tray (using an icon file), and if the process is not using any CPU at all, the icon is another color. Any way to do this?
Back to top
Sajith
Guest





PostPosted: Fri Feb 15, 2008 8:02 pm    Post subject: CPU usage for multiple instances of a process Reply with quote

I know this is quite an old post to respond to...
I ran into a situation where i had to record the CPU usage (over a long time duration) of a process which has multiple instances runnin at the same time.
I have no idea of kernel32.dll and the funtions in it. I have just merged 2 scripts I found on the Forum (1 of which is the SKAN's script above). The second script i used is
http://www.autohotkey.com/forum/topic28464.html&highlight=process+list
The merged script gives me a log file, with Process load of each instance followed by the total CPU usage, every 250 mSec.
I just want to ensure that after all the changes i made, the script still generates accurate results? And I am also sure the script can be optimised further.
Any Ideas???? Can someone atleast confirm the correctness???

usage (on the command prompt): <filename.ahk> processname logfile.txt

Code:

#Persistent
#SingleInstance, Force
CoordMode, ToolTip, Screen

IfNotEqual, 0, 2
{
    Msgbox, Invalid Arguments`nUsage: RecordCPUusage.exe <Process Name> <Filename.txt>
    ExitApp
}
Process, Exist, %1%
PID := errorLevel
If PID = 0
{
    Msgbox, Process does not exist.  Please check the process name.
    ExitApp
}
OUTPUTFILE = %2%

d = `n  ; string separator
s := 4096  ; size of buffers and arrays (4 KB)

Process, Exist  ; sets ErrorLevel to the PID of this running script
; Get the handle of this script with PROCESS_QUERY_INFORMATION (0x0400)
h := DllCall("OpenProcess", "UInt", 0x0400, "Int", false, "UInt", ErrorLevel)
; Open an adjustable access token with this process (TOKEN_ADJUST_PRIVILEGES = 32)
DllCall("Advapi32.dll\OpenProcessToken", "UInt", h, "UInt", 32, "UIntP", t)
VarSetCapacity(ti, 16, 0)  ; structure of privileges
InsertInteger(1, ti, 0, 4)  ; one entry in the privileges array...
; Retrieves the locally unique identifier of the debug privilege:
DllCall("Advapi32.dll\LookupPrivilegeValueA", "UInt", 0, "Str", "SeDebugPrivilege", "UIntP", luid)
InsertInteger(luid, ti, 4, 8)
InsertInteger(2, ti, 12, 4)  ; enable this privilege: SE_PRIVILEGE_ENABLED = 2
; Update the privileges of this process with the new access token:
DllCall("Advapi32.dll\AdjustTokenPrivileges", "UInt", t, "Int", false, "UInt", &ti, "UInt", 0, "UInt", 0, "UInt", 0)
DllCall("CloseHandle", "UInt", h)  ; close this process handle to save memory

hModule := DllCall("LoadLibrary", "Str", "Psapi.dll")  ; increase performance by preloading the libaray
s := VarSetCapacity(a, s)  ; an array that receives the list of process identifiers:
DllCall("Psapi.dll\EnumProcesses", "UInt", &a, "UInt", s, "UIntP", r)
PROCESSIDCOUNT = 0
Loop, % r // 4  ; parse array for identifiers as DWORDs (32 bits):
{
   id := ExtractInteger(a, A_Index * 4)
   ; Open process with: PROCESS_VM_READ (0x0010) | PROCESS_QUERY_INFORMATION (0x0400)
   h := DllCall("OpenProcess", "UInt", 0x0010 | 0x0400, "Int", false, "UInt", id)
   VarSetCapacity(m, s)  ; an array that receives the list of module handles:
   DllCall("Psapi.dll\EnumProcessModules", "UInt", h, "UInt", &m, "UInt", s, "UIntP", r)
   VarSetCapacity(n, s, 0)  ; a buffer that receives the base name of the module:
   e := DllCall("Psapi.dll\GetModuleBaseNameA", "UInt", h, "UInt", m, "Str", n, "Chr", s)
   DllCall("CloseHandle", "UInt", h)  ; close process handle to save memory
   If n  ; if image is not null add to list:
   {
       ifequal, n,%1%
       {
            PROCESSIDCOUNT += 1
            PROCESSIDLIST%PROCESSIDCOUNT% = %id%
       }
   }
}
DllCall("FreeLibrary", "UInt", hModule)  ; unload the library to free memory

FileAppend, %PROCESSIDCOUNT% process(s) found by the name %1%.`n, %OUTPUTFILE%


ExtractInteger(ByRef pSource, pOffset = 0, pIsSigned = false, pSize = 4)  ; See DllCall for details.
{
    Loop %pSize%  ; Build the integer by adding up its bytes.
        result += *(&pSource + pOffset + A_Index-1) << 8*(A_Index-1)
    if (!pIsSigned OR pSize > 4 OR result < 0x80000000)
        return result  ; Signed vs. unsigned doesn't matter in these cases.
    return -(0xFFFFFFFF - result + 1)
}

InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4)
{
    Loop %pSize%  ; Copy each byte in the integer into the structure as raw binary data.
        DllCall("RtlFillMemory", "UInt", &pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF)
}


SetTimer, ShowToolTip
Return

ShowToolTip:
   FileAppend, %A_DD% %A_MMM% %A_YYYY% %A_Hour%:%A_Min%:%A_Sec%.%A_MSec%, %OUTPUTFILE%
   Loop %PROCESSIDCOUNT%
   {
       element := PROCESSIDLIST%A_Index%
       PrLoad := Round( GetProcessTimes(element, A_Index),2)
       FileAppend, `tProcess load (ID: %element%) = %PrLoad%`%, %OUTPUTFILE%
   }

   TotCPULoad := GetSystemTimes()
   FileAppend, `tTotal CPU Load = %TotCPULoad%`%`n, %OUTPUTFILE%
   Return

GetProcessTimes( PID=0, IndexValue=1 )   
{
   global
   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)
   PROCESSCPUUSAGE :=  (newKrnlTime - oldKrnlTime%IndexValue% + newUserTime - oldUserTime%IndexValue%)/10000000 * 100
   oldKrnlTime%IndexValue% := newKrnlTime
   oldUserTime%IndexValue% := newUserTime
   Return PROCESSCPUUSAGE



GetSystemTimes()    ; Total CPU Load
{
   Static oldIdleTime, oldKrnlTime, oldUserTime
   Static newIdleTime, newKrnlTime, newUserTime

   oldIdleTime := newIdleTime
   oldKrnlTime := newKrnlTime
   oldUserTime := newUserTime

   DllCall("GetSystemTimes", "int64P", newIdleTime, "int64P", newKrnlTime, "int64P", newUserTime)
   Return (1 - (newIdleTime-oldIdleTime)/(newKrnlTime-oldKrnlTime + newUserTime-oldUserTime)) * 100
}
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group