AutoHotkey Community

It is currently May 25th, 2012, 6:40 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: May 4th, 2007, 12:44 am 
Offline

Joined: May 14th, 2006, 8:31 pm
Posts: 45
-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??


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 4th, 2007, 10:10 am 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
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

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 10th, 2007, 2:11 pm 
Offline

Joined: May 14th, 2006, 8:31 pm
Posts: 45
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)"


Report this post
Top
 Profile  
Reply with quote  
PostPosted: May 10th, 2007, 2:27 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
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/viewtop ... 695#119695

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 11th, 2007, 4:23 am 
Offline

Joined: December 1st, 2006, 9:27 am
Posts: 460
Code:
I just know someone's going to prove me wrong!

See? I was right! :D

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 11th, 2007, 8:03 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Micahs wrote:
See? I was right! :D


But no feedback from acowbear :roll: :(


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2007, 1:26 am 
Offline

Joined: May 14th, 2006, 8:31 pm
Posts: 45
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!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 26th, 2007, 6:56 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
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.

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 29th, 2007, 2:20 am 
Offline

Joined: May 14th, 2006, 8:31 pm
Posts: 45
Thanks. I looked and it was set to notify me...but it was using an obolete email address!! Updated it to my current email.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 1st, 2007, 9:52 am 
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?


Report this post
Top
  
Reply with quote  
PostPosted: February 15th, 2008, 9:02 pm 
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
}


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Cerberus, KenC, kwfine and 70 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group