Get general CPU usage?

Just starting out? Need help? Post your questions and find answers here.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Get general CPU usage?

Post by ricardo »

Hi,

I want to know the general CPU usage, no matter if its dual core, etc. Just the general CPU usage.

Can anybody point me in the right direction?

Thanks in advance

*I know how to do it with RunProgram("cmd.exe","/C wmic cpu get loadpercentage",""), Im looking a more direct way
ARGENTINA WORLD CHAMPION
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Get general CPU usage?

Post by ostapas »

Code snippet by luis, works both on x32 and x64

Code: Select all

    Prototype GetSystemTimes(*lpIdleTime, *lpKernelTime, *lpUserTime )

    Procedure.s CPULoad()
     
      Dll = OpenLibrary(#PB_Any,"kernel32.dll")
     
      Global GetSystemTimes.GetSystemTimes = GetFunction(Dll, "GetSystemTimes")
     
      Static FirstRun = #True
      Static Dim StartTimes(1), Dim EndTimes(1)
      Static CPU_Kernel.FILETIME, CPU_User.FILETIME, CPU_Idle.FILETIME
     
      GetSystemTimes(@CPU_Idle, @CPU_Kernel, @CPU_User)
      EndTimes(0) = (CPU_Kernel\dwLowDateTime + CPU_User\dwLowDateTime)
      EndTimes(1) = CPU_Idle\dwLowDateTime
     
      If FirstRun
        StartTimes(1) = EndTimes(1)
        StartTimes(0) = EndTimes(0)
        FirstRun = #False
        CloseLibrary(Dll)
        ProcedureReturn ""
      EndIf
     
      Idle_Time = EndTimes(1) - StartTimes(1)
     
      If Idle_Time > 0
        Total_Time = EndTimes(0) - StartTimes(0)
        CPU_Percent.f = ((Total_Time - Idle_Time) / Total_Time) * 100   
        Text.s = Left(StrF(CPU_Percent), FindString(StrF(CPU_Percent), ".", 1) + 1) + " %"   
      EndIf
      StartTimes(1) = EndTimes(1)
      StartTimes(0) = EndTimes(0)   
      CloseLibrary(Dll)
     
      ProcedureReturn Text
     
    EndProcedure

    ; usage example

    CPULoad() ; init

    Repeat
        Delay(1000)
        Debug CPULoad()   
    ForEver
Last edited by ostapas on Mon Aug 24, 2015 7:18 pm, edited 1 time in total.
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Get general CPU usage?

Post by ricardo »

@Ostapas

its not working for me or maqybe i am missing something.

---------------------------------------------


Using the RunProgram("cmd.exe","/C wmic cpu get loadpercentage","") make the CPU load increases, so the number i get its not the CPU load that was before running it.
ARGENTINA WORLD CHAMPION
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Get general CPU usage?

Post by infratec »

Hi,

use:

Code: Select all

Debug CPULoad()
Delay(100)
Debug CPULoad()
Delay(100)
Debug CPULoad()
The first call is only a dummy call to initialize the start values.

Bernd
ricardo
Addict
Addict
Posts: 2438
Joined: Fri Apr 25, 2003 7:06 pm
Location: Argentina

Re: Get general CPU usage?

Post by ricardo »

infratec wrote:Hi,
The first call is only a dummy call to initialize the start values.
Bernd
Yes, i notice that, byt was not using delay, my bad.

Thanks :)
ARGENTINA WORLD CHAMPION
User avatar
falsam
Enthusiast
Enthusiast
Posts: 632
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: Get general CPU usage?

Post by falsam »

Thanks for sharing ostapas.

➽ Windows 11 64-bit - PB 6.21 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect 🤪
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: Get general CPU usage?

Post by Xanos »

I would replace OpenLibrary with

Code: Select all

      Static DLL
      If Not IsLibrary(DLL)
        Dll = OpenLibrary(#PB_Any,"kernel32.dll")
      EndIf
and remove the CloseLibrary calls
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
User avatar
Shield
Addict
Addict
Posts: 1021
Joined: Fri Jan 21, 2011 8:25 am
Location: 'stralia!
Contact:

Re: Get general CPU usage?

Post by Shield »

Nah, just do this:

Code: Select all


EnableExplicit

Import "kernel32.lib"
	GetSystemTimes(*lpIdleTime, *lpKernelTime, *lpUserTime)
EndImport

Procedure.s CPULoad()	
	Static FirstRun = #True
	Static Dim StartTimes(1), Dim EndTimes(1)
	Static CPU_Kernel.FILETIME, CPU_User.FILETIME, CPU_Idle.FILETIME
	
	Protected Idle_Time.i
	Protected Total_Time.i
	Protected CPU_Percent.f
	Protected Text.s
	
	GetSystemTimes(@CPU_Idle, @CPU_Kernel, @CPU_User)
	EndTimes(0) = (CPU_Kernel\dwLowDateTime + CPU_User\dwLowDateTime)
	EndTimes(1) = CPU_Idle\dwLowDateTime
	
	If FirstRun
		StartTimes(1) = EndTimes(1)
		StartTimes(0) = EndTimes(0)
		FirstRun = #False
		ProcedureReturn ""
	EndIf
	
	Idle_Time = EndTimes(1) - StartTimes(1)
	
	If Idle_Time > 0
		Total_Time = EndTimes(0) - StartTimes(0)
		CPU_Percent.f = ((Total_Time - Idle_Time) / Total_Time) * 100   
		Text.s = Left(StrF(CPU_Percent), FindString(StrF(CPU_Percent), ".", 1) + 1) + " %"   
	EndIf
	StartTimes(1) = EndTimes(1)
	StartTimes(0) = EndTimes(0)   
	
	ProcedureReturn Text	
EndProcedure

; usage example

CPULoad() ; init

Repeat
	Delay(1000)
	Debug CPULoad()   
ForEver
Image
Blog: Why Does It Suck? (http://whydoesitsuck.com/)
"You can disagree with me as much as you want, but during this talk, by definition, anybody who disagrees is stupid and ugly."
- Linus Torvalds
User avatar
Xanos
User
User
Posts: 45
Joined: Sat Feb 28, 2015 1:20 pm

Re: Get general CPU usage?

Post by Xanos »

I just wanted to stick with the original code and point out that closing and opening the library all the time might not be the best way.
However, importing the lib file directly sure is a good way to go :)
Programming on Windows 10 Pro / Windows 7 Enterprise / Linux Mint 19 / Manjaro Linux with PB 5, Python 3 and Go
Post Reply