Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

CPU LoadTimes


  • Please log in to reply
30 replies to this topic
Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007
I saw this question frequently in the Ask section, and I also produced one solution myself using WMI COM.
As I saw the question again, I became curious. Really there have been no other answers about this basic question?
So, I finally searched the forum, and on the contrary found many useful solutions.
IMO, however, they appear rather complicated than really is. So, I wrote it again myself. I wish it could be of help to some users.

DOWNLOAD CPUTimes.ahk.

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Well, there is Laszlo's version long time ago...
Posted Image

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Well, there is Laszlo's version long time ago...

I already said that I saw it.
BTW, 64bit integer wasn't supported at that time?

majkinetor
  • Moderators
  • 4512 posts
  • Last active: May 20 2019 07:41 AM
  • Joined: 24 May 2006
Thx for the functions. They are indeed much more usable then those that were already made, plus we got nice func for single process.
Posted Image

Grumpy
  • Guests
  • Last active:
  • Joined: --

found many useful solutions.

Looks like that...

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005
Very nice! Thanks Sean.
GetSystemTimes() will not work with Win2K ? :(

:)

Laszlo
  • Moderators
  • 4713 posts
  • Last active: Mar 31 2012 03:17 AM
  • Joined: 14 Feb 2005
GetProcessTimes is nice! Thanks, Sean! I added it to my script collection.

I use now a simple hotkey (Win-P), which pops up the tooltip with the processor load of the *active* window and the system load. Subsequent hotkey presses toggle this tooltip on/off.

I never updated the system load function I posted long-long ago, but below is a modernized version. It gives the same result as your GetSystemTimes(), so chose which one you like.
#Persistent
CoordMode ToolTip
SetFormat Float, 6.2

#p::
   If (pid) {
      pid =
      SetTimer CPUTimes, OFF
      DllCall("CloseHandle", Uint,hProc)
      ToolTip
   } Else {
      WinGet pid, PID, A
      hProc := DllCall("OpenProcess", Uint,0x400, Int,0, Uint,pid)
      SetTimer CPUTimes, 1000
   }
Return

CPUTimes:
   ToolTip % GetSystemTimes() . "%`n" . GetProcessTimes(hProc) . "% ", 0, 0
Return


GetProcessTimes(hProc) {  ; Individual CPU Load of the process with pid
   Static oldKrnlTime, oldUserTime, newKrnlTime, newUserTime

   oldKrnlTime := newKrnlTime, oldUserTime := newUserTime
   DllCall("GetProcessTimes", Uint,hProc, Int64P,CreationTime, Int64P,ExitTime, Int64P,newKrnlTime, Int64P,newUserTime)
   Return (newKrnlTime-oldKrnlTime + newUserTime-oldUserTime)*1.e-5
}

GetSystemTimes() {   ; Total CPU Load
   Static IdleTime, IdleTime0, Tick, Tick0

   IdleTime0 := IdleTime, Tick0 := Tick
   DllCall("GetSystemTimes", UInt64P,IdleTime, Int,0, Int,0)
   Return 100 - .01*(IdleTime0 - IdleTime)/(Tick0 - Tick:=A_TickCount)
}


Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

GetSystemTimes() will not work with Win2K ? :(

Oh, sorry. GetSystemTimes is a pretty recent function.
According to the documentation, it requires at least XP SP1.

The best solution in those OS < XPSP1 might be to retrieve the performance data through HKEY_PERFORMANCE_DATA registry, assuming WMI doesn't provide one either. However, AHK seems to not support this key, which is understandable as this key is not really a registry key. Anyway, it can be done by invoking RegQueryValueEx API so this shouldn't be a problem.

PS. I've never actually tried to obtain the performance data through this method, so I don't know what data should be retrieved yet, either.

PSPS. I forgot mentioning about another useful function, NtQuerySystemInformation.
However, I'm rather reluctant about using it as it's an undocumented function (:seems that it]s now got some documentations).

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

I never updated the system load function I posted long-long ago, but below is a modernized version.

Thanks for the update. I hope other authors also update their scripts, so as to have various alternatives.

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

GetSystemTimes() will not work with Win2K ? :(

Oh, sorry. GetSystemTimes is a pretty recent function.
According to the documentation, it requires at least XP SP1.

I added NtQuerySystemInformation() function, which can be used instead of GetSystemTimes().
I hope it works for you.

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

I added NtQuerySystemInformation() function, which can be used instead of GetSystemTimes().
I hope it works for you.


Thanks Sean, but NtQuerySystemInformation() does not seem to work in Win2K SP4 :( :roll:

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

but NtQuerySystemInformation() does not seem to work in Win2K SP4 :( :roll:

Looks like SystemProcessorPerformanceInformation is also a new feature.
Then, I'm afraid we have to rely a bit on the undocumented property.

I updated the script adding GetIdleTimes(). You may try it instead of GetSystemTimes().

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

I updated the script adding GetIdleTimes().


Yes! That is the One :D
That works fine for me in Win2K SP4 as well as WinXP SP2
Thank you very much Sean !!!

SKAN
  • Administrators
  • 9115 posts
  • Last active:
  • Joined: 26 Dec 2005

I updated the script adding GetIdleTimes()


Can you update it again to use NumGet() ... Please! :)

Sean
  • Members
  • 2462 posts
  • Last active: Feb 07 2012 04:00 AM
  • Joined: 12 Feb 2007

Can you update it again to use NumGet()

Done!