topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Friday March 29, 2024, 4:29 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: use AHK to determine CPU usgae of any given process?  (Read 23015 times)

AEN007

  • Participant
  • Joined in 2011
  • *
  • Posts: 74
  • Imagination is more important than knowledge
    • View Profile
    • Donate to Member
use AHK to determine CPU usgae of any given process?
« on: February 22, 2011, 08:35 AM »
22Feb2011
Greetings.
I've just discovered dc.com.
I also do lots of AHK scripting ... love AHK...
I have LOTS of AHK scripts - a few are memory-resident/permanent.
I also do some CPU usage monitoring and (attempted) control.
One of my permanent scripts uses the following
Code: Autohotkey [Select]
  1. SBWErr := 0
  2. If WinExist("Windows Task ahk_class #32770") and (SlWS2w = 0)
  3.  {
  4.   If (RuWTMin = 1)
  5.    {
  6.     RuWTMin := 0
  7.     RunWait, aWTMrepo.exe, %IU%
  8.    }
  9.   SlWS2w := SlWS2/1000
  10.   ErrorLevel := 0
  11.   StatusBarWait, 100, %SlWS2w%, 2, Windows Task ahk_class #32770
  12.   If (ErrorLevel = 0)
  13.    If (ExEx = 1)
  14.     SBWErr := 1
  15.  }
  16. else If (SlWS2w = 0)
  17.  SLEEP, %SlWS2%
which checks if CPU usage at the given moment is 100%.
I have three XP laptops - one auf Deutsch,
so I have to accomodate differing window titles sometimes.

Anyway, I have no idea how to determine the CPU usage of any given process
but would REALLY like to know ...

AHK/NP++ users might want to check out the following»
NP++AHK

Any helpful replies/insights appreciated.
Thank you.
Regards,
AEN
Æ

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #1 on: February 22, 2011, 08:50 AM »
Anyway, I have no idea how to determine the CPU usage of any given process
but would REALLY like to know ...

http://www.autohotke...orum/topic18913.html

AEN007

  • Participant
  • Joined in 2011
  • *
  • Posts: 74
  • Imagination is more important than knowledge
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #2 on: November 07, 2011, 10:26 AM »
7November2011

Greetings.
Many thanks for the reply (and everything else ...).
I looked at that thread/link you posted above earlier this year but
have not yet been able to script what I want.
I would like one of my Persistent ahk scripts to determine
the CPU usage of the active window, so the script could
then do varoius things depending on the CPU usage of the active window.
Determining the active window is obviously no problem.
The CPUtimes script works as written, but
how could I modify it to do what I want?

Any helpful replies/insights appreciated ...

Maybe it would be interest to you to hear that
I have noticed that the CPU usage Trout reports for Trout.exe
does not agree with the CPU usage XP WTM reports for Trout.exe ..

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #3 on: November 07, 2011, 11:37 AM »
You can use SetTimer to constantly poll which window is active, get its CPU usage, and then act upon that value.  Alternately, you could use a ShellHook to watch which window is active.  There are pros and cons to both methods.

I have noticed that the CPU usage Trout reports for Trout.exe
does not agree with the CPU usage XP WTM reports for Trout.exe ..

Correct.  Trout reports what the BASS library is using.

AEN007

  • Participant
  • Joined in 2011
  • *
  • Posts: 74
  • Imagination is more important than knowledge
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #4 on: November 08, 2011, 02:17 AM »
8November2011

Greetings.
Thanks for the reply ...

I want to determine the active window CPU usage
when, for example, the IdleTimePhysical is > 5min,
so SetTimer is not an option ...

In any case, the CPUtimes script reports the CPU usage to a TrayTip - not a variable.
I'm hoping to see a script example of how to have the CPU usage
of the active window reported to a variable ...

Any helpful replies/insights appreciated ...

skwire

  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,286
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #5 on: November 08, 2011, 02:34 AM »
In any case, the CPUtimes script reports the CPU usage to a TrayTip - not a variable.
I'm hoping to see a script example of how to have the CPU usage
of the active window reported to a variable ...

The GetProcessTimes(), GetSystemTimes() and GetIdleTimes() functions in that script return the data you want.  It's up to you to dump that data into a variable:

Code: Autohotkey [Select]
  1. myVar := GetSystemTimes()
  2. ; myVar will now contain the total CPU load at that moment.
  3. ; Now do whatever you need to do with the variable and data.

« Last Edit: December 02, 2011, 10:12 AM by skwire »

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #6 on: November 08, 2011, 03:49 AM »
I am interested in this subject, because I fairly often wish to check on the relative CPU utilisation of running processes, for the purposes of comparison - i.e., which of the CPU cycle gobblers are doing what.
I do not usually want to know the CPU utilisation of just a single process in isolation.
And sometimes I want more information than just the basic CPU utilisation - e.g., I may want to know more about process I/O performance - in this case I will usually use Sysinternals' Process Explorer.

However, if it's just CPU utilisation that I am after, then the quickest most useful display that I can currently get is by:
Step 1. pressing Shift+Ctrl+Esc - which brings up the Windows Task Manager window.
Step 2. selecting the Processes tab.
Step 3. clicking on the CPU column header, to get the active processes (those that are consuming CPU) so that they are stacked in ascending order of size at the bottom of the display.
Step 4. pressing End to get the select cursor to the highest CPU number (that's the one at the bottom of the table, and is normaly the System Idle Process.

What that gives is a comparative and dynamically changing view of the active processes (many/most processes could be idle, depending on what is currently being processed).
When I have finished viewing these processes in the Windows Task Manager window, I usually:
Step 5. press Esc - which closes the window.

Once you have gone through the above steps, the window settings you selected for Windows Task Manager are retained, so you only need to go through steps 1., 4., 5., subsequently.

Based on the hypothesis that automating a repetitive process can usually save a lot of time, the most useful thing I could think of here for my purposes would be to automate these steps using (say) AHK, so that 1 hotkey action does it all, including (say) a 10-second pause to give me time to view the Windows Task Manager window before it is closed.

If anyone could give me some pointers as to how to do this (using AHK), then that could be very helpful, and it would help me to learn more about using AHK as well - which I am interested in doing.

I think automation like this is potentially highly useful, and can sometimes have unexpected spin-offs.
For example, the potential time-savings of automating the above steps could be quite significant for me over a year of computer use, and may even leave the way open to (say) automate the manual comparison/monitoring of process CPU utilisation, so that I would get an alert of a potential system CPU performance issue - rather than keep having to look and see if there is one as at present.

Similarly, I have already saved myself an inestimable amount of time and trouble by the simple act of recoding my CapsLock key (using Remapkey) to behave like the RightShift key - which also means that I can easily use key combinations such as, for example, LeftShift+RightShift+N, to bring up Notepad (or any other application I choose for "N"), without much risk of running into actual/potential conflict with any existing/preset Hotkey allocations from the system or other applications. I mean, who in their right mind is otherwise likely to be using combinations such as LeftShift+RightShift+N? Maybe an exception could be pianists, I suppose.

Stoic Joker

  • Honorary Member
  • Joined in 2008
  • **
  • Posts: 6,646
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #7 on: November 08, 2011, 06:53 AM »
However, if it's just CPU utilisation that I am after, then the quickest most useful display that I can currently get is by:
Step 1. pressing Shift+Ctrl+Esc - which brings up the Windows Task Manager window.
Step 2. selecting the Processes tab.
Step 3. clicking on the CPU column header, to get the active processes (those that are consuming CPU) so that they are stacked in ascending order of size at the bottom of the display.
Step 4. pressing End to get the select cursor to the highest CPU number (that's the one at the bottom of the table, and is normaly the System Idle Process.

Interesting, I do much the same, with one exception. I sort the CPU utilization column so that the highest usage is at the top (which would save you a key stroke) because that is where it opens to by default.

IainB

  • Supporting Member
  • Joined in 2008
  • **
  • Posts: 7,540
  • @Slartibartfarst
    • View Profile
    • Read more about this member.
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #8 on: November 08, 2011, 08:38 AM »
Interesting, I do much the same, with one exception. I sort the CPU utilization column so that the highest usage is at the top (which would save you a key stroke) because that is where it opens to by default.
Ahh! Good point, thanks.
I shall do that (omit Step 4) from now on, as it is more efficient that way.
The only reason I had that step 4 in the first place is simply that I prefer to see the numbers displayed that way. I don't think it alters their value to me if they are in reverse order.

anandcoral

  • Honorary Member
  • Joined in 2009
  • **
  • Posts: 777
    • View Profile
    • Free Portable Apps
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #9 on: November 08, 2011, 10:26 AM »
I will suggest Process Tamer https://www.donation...proctamer/index.html, freeware by our own Mouser.

Not only you can see the top CPU using process (it hides the idle ones by default), it also 'tames' them by lowering their priority if any one goes beyond the limit. All work is done auto. After I started using it, I forgot when was the last time my machine freeze.

Give it a spin.

Regards,

Anand

Shades

  • Member
  • Joined in 2006
  • **
  • Posts: 2,922
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #10 on: November 08, 2011, 11:21 AM »
You can also take a look at 'Prism HUD'

This tool only shows progress bars (in opaque) how much CPU a process uses as well as the total load. When there is no load the bar disappears. It does the same with Network load and disk usage. Free and portable.

AEN007

  • Participant
  • Joined in 2011
  • *
  • Posts: 74
  • Imagination is more important than knowledge
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #11 on: November 08, 2011, 02:01 PM »
8November2011

Greetings.

I have a maybe a dozen WTM-related AHK scripts.

Here is a modified/enhanced version of what I already posted»
Code: Autohotkey [Select]
  1. scrX := 1
  2.     Perc := "%"
  3.     StatusBarGetText, CPU, 2, Windows Task ahk_class #32770
  4.     SLEEP, %Slb%
  5.     StringGetPos, pos, CPU, %Perc%
  6.     If (pos = 14) or ((pos = 19) and (CN = "T"))
  7.      {
  8.       CPU100 := 1
  9.       If (SS = "C98") or (SS = "M")
  10.        Process, Priority, %scrPID%, AboveNormal
  11.       CPUlim := CPUlim+1
  12.      }
  13.     else
  14.      {
  15.       If (CN <> "T")
  16.        StringMid, CPUU, CPU, 13, 2, L
  17.       else StringMid, CPUU, CPU, 18, 2, L
  18.       If (CPUU > 90)
  19.        {
  20.         CPU100 := 1
  21.         CPUlim := CPUlim+1
  22.         CuDa := A_YYYY A_MM A_DD
  23.         CuTi := A_Hour A_Min A_Sec
  24.         FileAppend, CPUU ¦ %CuDa%@%CuTi% ¦ %CPUU% ¦ SS:%SS% ¦ CPUlim:%CPUlim% `n, %PFa%\ahk\%CN%%SNt2%.txt
  25.        }
  26.       else CPU100 := 0
  27.      }
which can be used to test for CPU usage (CPUU) at 100 or 90 or any %;

I have but do not often use the SysIntern ProX.
I (like IainB & StoicJo) started watching the XP WTM
(I have always sorted like StoicJo posted) a long time ago -
mostly because I hate trying to work on an unresponsive computer.

The PC is largely unresponsive to the user & processes in general when CPUU is 100%.
(I have never found the trick of lowering the priority of the resource hog to be effective.
 Lowering the priority allows other exes to grap CPUU but does not lower CPUU from 100%,

If there is interest I could post some other WTM-related scripts.
These other scripts mostly (re-)position the XP WTM.
I always have the XP WTM open & in the lower right corner.
One of my AHK scripts short-circuits/supercharges my XP "AppsKey" (AK).
For example, when the XP WTM is active & I hit the AK,
the script(s) will reposition the the XP WTM various ways ...»
WTMin
WTMin is for when I want the XP WTM to take up the least screen real estate ...
WTMreg
WTMreg is for when the XP WTM doesn't need to be minimized/maximized ...
All I need to do is Alt-Tab to the WTM & then Ctrl-(Shift)-Tab between the Task/Processes/Summary ...
WTMax
WTMmax is for when I need to maximize the XP WTM  ...

In any case, the CPUtimes script reports the CPU usage to a TrayTip - not a variable.
I'm hoping to see a script example of how to have the CPU usage
of the active window reported to a variable ...
The GetProcessTimes(), GetSystemTimes() and GetIdleTimes() functions in that script return the data you want.  It's up to you dump that data into a variable:
Code: Autohotkey [Select]
  1. myVar := GetSystemTimes()
  2. ; myVar will now contain the total CPU load at that moment.
  3. ; Now do whatever you need to do with the variable and data.
Greetings & thanks for the reply ...
I have already been trying to do what you have posted,
but it has not been working for me.
I can get from the XP WTM what GetSystemTimes provides.
I want to put GetProcessTimes into a variable at any given moment.
I have used Sean's TrayIcons() & TaskButtons() in many scripts, like ...
Code: Autohotkey [Select]
  1. If (WxX = 0)
  2.  {
  3.   TBEE := TaskButtons("explorer.exe")
  4.   StringSplit, TBV, TBEE,|
  5.   idn :=  RegExReplace(TBV2, "idn: ")
  6.   StringTrimRight, idnT, idn, 1
  7.   idnNWC := idnT
  8.   HideButton(idnNWC, False)
  9.   SLEEP, %Slb%
  10.   HideButton(idnNWC, True)
  11.  }
Those TrayIcons() & TaskButtons script/functions reside in the AHK\Lib "PATH".
I should be able to access/use CPUTimes.ahk the same way, ja?

I have already tried the following»
Code: Autohotkey [Select]
  1. WinGet, AWpid, PID, A
  2. GPTpid := AWpid
  3. MsgBox, 0, GPTpid, %GPTpid%
  4. GST := GetSystemTimes()
  5. MsgBox, 0, GST, %GST%
  6. GPT := GetProcessTimes(GPTpid)
  7. MsgBox, 0, GPT, %GPT%
GST & GPT are always blank ...
Any helpful replies/insights appreciated ...

AEN007

  • Participant
  • Joined in 2011
  • *
  • Posts: 74
  • Imagination is more important than knowledge
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #12 on: December 02, 2011, 07:16 AM »
2December2011
Still hoping for a solution.
Any helpful replies/insights appreciated ...

AEN007

  • Participant
  • Joined in 2011
  • *
  • Posts: 74
  • Imagination is more important than knowledge
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #13 on: June 17, 2012, 01:59 PM »
2December2011
Still hoping for a solution.
Any helpful replies/insights appreciated ...

AEN007

  • Participant
  • Joined in 2011
  • *
  • Posts: 74
  • Imagination is more important than knowledge
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #14 on: June 25, 2012, 03:30 PM »
25June2012

Greetings.

Well, today I FINALLY worked out how to use GetProcessTimes (GPT) in an AHK script.
Many thanks to Skwire for the reply with the link to the AHK forum thread ...

However, from what I can tell, GPT does not retrieve CPU usage values
that are in any way close to those reported by Windows Task Manager,
and half the time or more often GPT retrieves a CPU usage value of zero ...
There is probably no need to elaborate ...

I searched the web with "autohotkey process cpu usage" and
found an AHK forum thread with some code that uses»
ControlGet, var, List, , SysListView321, Windows Task ahk_class #32770.
I came up with some quicker/simpler code that will allow my AHK scripts
to determine the (WTM) CPU usage of any running process ...
so unless there is some discussion (of value) to follow,
I guess I am finished with this thread ...

AEN007

  • Participant
  • Joined in 2011
  • *
  • Posts: 74
  • Imagination is more important than knowledge
    • View Profile
    • Donate to Member
Re: use AHK to determine CPU usgae of any given process?
« Reply #15 on: September 18, 2012, 11:33 AM »
18September2012

I have tweaked my script to the following»
Code: Autohotkey [Select]
  1. StatusBarGetText, CPU, 2, Windows Task ahk_class #32770
  2. StringSplit, StrSpl, CPU, %A_Space%, `%
  3. If (StrSpl3 > whatever)
  4.  Process, Priority, whatever.exe, High
  5. else Process, Priority, whatever.exe, AboveNormal
  6. AHk2pid := ErrorLevel