Ce code est un début de projet pour l'évolution de l'un de mes progs, il a pour but de vous donner le pourcentage CPU utilisé par le système pour chaque processeur ou coeur.
Pourriez-vous me dire si ce code fonctionne sur votre ordi, en m'indiquant votre version de Windows
MERCI D'AVANCE !!!
Structure SYSTEM_BASIC_INFORMATION
reserved.l
TimerResolution.l
PageSize.l
NumberOfPhysicalPages.l
LowestPhysicalPageNumber.l
HighestPhysicalPageNumber.l
AllocationGranularity.l
MinimumUserModeAddress.l
MaximumUserModeAddress.l
ActiveProcessorsAffinityMask.l
NumberOfProcessors.l
EndStructure
Structure SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION
IdleTime.q
KernelTime.q
UserTime.q
Reserved1.q[3]
EndStructure
#SystemBasicInformation = 0
#SystemProcessorPerformanceInformation = 8
#SysProcPerfInfoLength = 48
#PROCESS_QUERY_INFORMATION = $400
#HUNDRED_NANOSEC = 10000000
DeclareDLL.l GetNumberOfProcessor()
Global Frequency.q, TimeStart.q
Global NumberOfProcessors = GetNumberOfProcessor()
Global Dim CpuInfo.SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION(NumberOfProcessors)
Global Dim KernelTimesStart.d(NumberOfProcessors+1)
Global Dim UserTimesStart.d(NumberOfProcessors+1)
Global Dim IdleTimesStart.d(NumberOfProcessors+1)
Global Dim KernelTimesStop.d(NumberOfProcessors+1)
Global Dim UserTimesStop.d(NumberOfProcessors+1)
Global Dim IdleTimesStop.d(NumberOfProcessors+1)
Global Dim KernelTimesDelta.d(NumberOfProcessors+1)
Global Dim UserTimesDelta.d(NumberOfProcessors+1)
Global Dim IdleTimesDelta.d(NumberOfProcessors+1)
Global Dim CpuUsages.d(NumberOfProcessors+1)
ProcedureDLL.l GetNumberOfProcessor() ; Retourne le nombre de processeur
Protected resultat, Lib, SBI.SYSTEM_BASIC_INFORMATION
Lib = OpenLibrary ( #PB_Any , "ntdll.dll" )
If Lib
If CallFunction (Lib, "NtQuerySystemInformation" , #SystemBasicInformation , @SBI, SizeOf (SYSTEM_BASIC_INFORMATION), 0) = 0
resultat = SBI\NumberOfProcessors
EndIf
CloseLibrary (Lib)
EndIf
ProcedureReturn resultat
EndProcedure
Procedure.d Div10k(Value.q)
ProcedureReturn Value/10000
EndProcedure
Procedure GetProcessorsTimes(start = #True )
Protected Lib
Lib = OpenLibrary ( #PB_Any , "ntdll.dll" )
If Lib
If CallFunction (Lib, "NtQuerySystemInformation" , #SystemProcessorPerformanceInformation , @CpuInfo(), #SysProcPerfInfoLength * NumberOfProcessors, 0) = 0
If start = #True
KernelTimesStart(NumberOfProcessors) = 0
IdleTimesStart(NumberOfProcessors) = 0
UserTimesStart(NumberOfProcessors) = 0
For i = 0 To NumberOfProcessors - 1
KernelTimesStart(i) = Div10k(CpuInfo(i)\KernelTime)
IdleTimesStart(i) = Div10k(CpuInfo(i)\IdleTime)
UserTimesStart(i) = Div10k(CpuInfo(i)\UserTime)
KernelTimesStart(NumberOfProcessors) = KernelTimesStart(NumberOfProcessors) + Div10k(CpuInfo(i)\KernelTime)
IdleTimesStart(NumberOfProcessors) = IdleTimesStart(NumberOfProcessors) + Div10k(CpuInfo(i)\IdleTime)
UserTimesStart(NumberOfProcessors) = UserTimesStart(NumberOfProcessors) + Div10k(CpuInfo(i)\UserTime)
Next
Else
KernelTimesStop(NumberOfProcessors) = 0
IdleTimesStop(NumberOfProcessors) = 0
UserTimesStop(NumberOfProcessors) = 0
For i = 0 To NumberOfProcessors - 1
KernelTimesStop(i) = Div10k(CpuInfo(i)\KernelTime)
IdleTimesStop(i) = Div10k(CpuInfo(i)\IdleTime)
UserTimesStop(i) = Div10k(CpuInfo(i)\UserTime)
KernelTimesStop(NumberOfProcessors) = KernelTimesStop(NumberOfProcessors) + Div10k(CpuInfo(i)\KernelTime)
IdleTimesStop(NumberOfProcessors) = IdleTimesStop(NumberOfProcessors) +Div10k( CpuInfo(i)\IdleTime)
UserTimesStop(NumberOfProcessors) = UserTimesStop(NumberOfProcessors) + Div10k(CpuInfo(i)\UserTime)
Next
EndIf
EndIf
CloseLibrary (Lib)
EndIf
EndProcedure
Procedure refresh()
Protected CpuTime.d, CurTime.q, DiffTime.d
QueryPerformanceCounter_ (@CurTime)
GetProcessorsTimes( #False )
CpuUsages(NumberOfProcessors) = 0
For i = 0 To NumberOfProcessors - 1
KernelTimesDelta(i) = KernelTimesStop(i) - KernelTimesStart(i)
UserTimesDelta(i) = UserTimesStop(i) - UserTimesStart(i)
IdleTimesDelta(i) = IdleTimesStop(i) - IdleTimesStart(i)
CpuTime = (KernelTimesDelta(i) + UserTimesDelta(i) + IdleTimesDelta(i))
DiffTime = (CurTime - TimeStart) / Frequency
If DiffTime > 0 : CpuTime = (((CpuTime) / (DiffTime)) / NumberOfProcessors) * 10000 : EndIf
CpuUsages(i) = (100 * (1 - CpuTime / #HUNDRED_NANOSEC ))
If NumberOfProcessors = 1 : CpuUsages(i) = 100 + CpuUsages(i) : EndIf
If CpuUsages(i) < 0 : CpuUsages(i) = 0 : EndIf
If CpuUsages(i) > 100 : CpuUsages(i) = 100 : EndIf
CpuUsages(i) = CpuUsages(i) * NumberOfProcessors
CpuUsages(NumberOfProcessors) = CpuUsages(NumberOfProcessors) + CpuUsages(i)
KernelTimesStart(i) = KernelTimesStop(i)
UserTimesStart(i) = UserTimesStop(i)
IdleTimesStart(i) = IdleTimesStop(i)
SetGadgetItemText (1, i, StrD (CpuUsages(i), 3), 1)
SetGadgetItemText (1, i, StrD (KernelTimesStart(i), 3), 2)
SetGadgetItemText (1, i, StrD (UserTimesStart(i), 3), 3)
SetGadgetItemText (1, i, StrD (IdleTimesStart(i), 3), 4)
Next
KernelTimesStart(NumberOfProcessors) = KernelTimesStop(NumberOfProcessors)
UserTimesStart(NumberOfProcessors) = UserTimesStop(NumberOfProcessors)
IdleTimesStart(NumberOfProcessors) = IdleTimesStop(NumberOfProcessors)
CpuUsages(NumberOfProcessors) = CpuUsages(NumberOfProcessors) / NumberOfProcessors
SetGadgetItemText (1, NumberOfProcessors, StrD (CpuUsages(NumberOfProcessors), 3), 1)
SetGadgetItemText (1, NumberOfProcessors, StrD (KernelTimesStart(NumberOfProcessors), 3), 2)
SetGadgetItemText (1, NumberOfProcessors, StrD (UserTimesStart(NumberOfProcessors), 3), 3)
SetGadgetItemText (1, NumberOfProcessors, StrD (IdleTimesStart(NumberOfProcessors), 3), 4)
TimeStart = CurTime
EndProcedure
QueryPerformanceFrequency_ (@Frequency)
QueryPerformanceCounter_ (@TimeStart)
GetProcessorsTimes()
Win = OpenWindow ( #PB_Any , 0, 0, 535, 140, "CPU Usage Multi-Processors" , #PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_MinimizeGadget )
If IsWindow (Win) And CreateGadgetList ( WindowID (Win))
ListIconGadget (1, 10, 10, 515, 120, "CPU" , 75)
AddGadgetColumn (1, 1, "% Usage" , 60)
AddGadgetColumn (1, 2, "Kernel Time" , 120)
AddGadgetColumn (1, 3, "User Time" , 120)
AddGadgetColumn (1, 4, "Idle Time" , 120)
For i=0 To NumberOfProcessors-1
AddGadgetItem (1, -1, "CPU " + Str (i))
Next
AddGadgetItem (1, 2, "CPU TOTAL" )
SetTimer_ ( WindowID (Win), 1, 1000, @refresh())
Repeat
Select WaitWindowEvent ()
Case #PB_Event_Gadget
Select EventGadget ()
;Case
EndSelect
Case #PB_Event_CloseWindow
Quit = 1
EndSelect
Until Quit = 1
KillTimer_ ( WindowID (Win), 1)
EndIf