Ce code permet d'obtenir et de modifier le mask d'affinité d'un processus ou d'un thread
Pour Info:
Le mask d'affinité défini sur quel CPU (processeur ou core) un thread ou un processus doit s'executer.
Fonctionnement des masques d'affinité
Première chose, les <> CPUs sont nommés en commencant a 0
Le masque pour un CPU est: 2 ^ CpuIndex
Donc par exemple pour le CPU 0 (premier CPU) on aura le masque
Masque = (2 ^ 0) = 1
Pour definir plusieurs CPU on additionne simplement le mask des <> CPU
Exemple pour le CPU 0 et CPU 1
Masque = (2 ^ 0) + (2 ^ 1) = 3
Remarque vous ne pouvez pas affecter un thread sur le CPU 0 si le processus pere est sur le CPU 1 uniquement! dans ce cas il faut d'abord affecté le processus au CPU 0 (logique me direz vous...)
J'ai mis un exemple concret dans le zip...
++
Source / Exemple :
Option Explicit
Private Declare Function NtQueryInformationProcess Lib "Ntdll.dll" (ByVal hProcess As Long, ByVal ProcessInformationClass As Long, ByVal ProcessInformation As Long, ByVal ProcessInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function NtSetInformationProcess Lib "Ntdll.dll" (ByVal hProcess As Long, ByVal ProcessInformationClass As Long, ByVal ProcessInformation As Long, ByVal ProcessInformationLength As Long) As Long
Private Declare Function NtQueryInformationThread Lib "Ntdll.dll" (ByVal hThread As Long, ByVal ThreadInformationClass As Long, ByVal ThreadInformation As Long, ByVal ThreadInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function NtSetInformationThread Lib "Ntdll.dll" (ByVal hThread As Long, ByVal ThreadInformationClass As Long, ByVal ThreadInformation As Long, ByVal ThreadInformationLength As Long) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function OpenThread Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwThreadId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Sub GetSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
Private Const PROCESS_SET_INFORMATION = &H200
Private Const PROCESS_QUERY_INFORMATION = &H400
Private Const THREAD_SET_INFORMATION = &H20
Private Const THREAD_QUERY_INFORMATION = &H40
Private Type CLIENT_ID
UniqueProcess As Long
UniqueThread As Long
End Type
Private Type PROCESS_BASIC_INFORMATION
ExitStatus As Long
PEBBaseAddress As Long
AffinityMask As Long
BasePriority As Long
UniqueProcessId As Long
ParentProcessId As Long
End Type
Private Type THREAD_BASIC_INFORMATION
ExitStatus As Long
TebBaseAddress As Long
ClientId As CLIENT_ID
AffinityMask As Long
Priority As Long
BasePriority As Long
End Type
Private Type SYSTEM_INFO
dwOemID As Long
dwPageSize As Long
lpMinimumApplicationAddress As Long
lpMaximumApplicationAddress As Long
dwActiveProcessorMask As Long
dwNumberOrfProcessors As Long
dwProcessorType As Long
dwAllocationGranularity As Long
wProcessorLevel As Integer
wProcessorRevision As Integer
End Type
Public Function GetCpuCount() As Long
Dim S_I As SYSTEM_INFO
GetSystemInfo S_I
GetCpuCount = S_I.dwNumberOrfProcessors
End Function
Public Function GetAllCoreMask() As Long
Dim S_I As SYSTEM_INFO
GetSystemInfo S_I
GetAllCoreMask = S_I.dwActiveProcessorMask
End Function
Public Function GetThreadMask(ByVal ThreadId As Long) As Long
Dim hThread As Long
Dim TBI As THREAD_BASIC_INFORMATION
hThread = OpenThread(THREAD_QUERY_INFORMATION, 0, ThreadId)
If hThread Then
NtQueryInformationThread hThread, 0&, VarPtr(TBI), Len(TBI), ByVal 0&
GetThreadMask = TBI.AffinityMask
CloseHandle hThread
End If
End Function
Public Function SetThreadMask(ByVal ThreadId As Long, ByVal Mask As Long) As Long
Dim hThread As Long
hThread = OpenThread(THREAD_SET_INFORMATION, 0, ThreadId)
If hThread Then
SetThreadMask = NtSetInformationThread(hThread, 4&, VarPtr(Mask), Len(Mask))
CloseHandle hThread
End If
End Function
Public Function GetProcessMask(ByVal ProcessId As Long) As Long
Dim hProcess As Long
Dim Pbi As PROCESS_BASIC_INFORMATION
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, ProcessId)
If hProcess Then
NtQueryInformationProcess hProcess, 0&, VarPtr(Pbi), Len(Pbi), ByVal 0&
GetProcessMask = Pbi.AffinityMask
CloseHandle hProcess
End If
End Function
Public Function SetProcessMask(ByVal ProcessId As Long, ByVal Mask As Long) As Long
Dim hProcess As Long
hProcess = OpenProcess(PROCESS_SET_INFORMATION, 0, ProcessId)
If hProcess Then
SetProcessMask = NtSetInformationProcess(hProcess, 21&, VarPtr(Mask), Len(Mask))
CloseHandle hProcess
End If
End Function
Public Function GetMaskFromCpuList(ByRef CpuList As String, Optional ByRef Separator As String = ",") As Long
Dim s() As String
Dim i As Integer
Dim l As Long
If CpuList = "" Then Exit Function
s = Split(CpuList, Separator)
For i = 0 To UBound(s)
l = l + (2 ^ Int(s(i)))
Next i
GetMaskFromCpuList = l
End Function
Public Function GetCpuListFromMask(ByVal Mask As Long, Optional ByRef Separator As String = ",") As String
Dim i As Byte
Dim Value As Long
Dim sMask As String
For i = 0 To GetCpuCount - 1
Value = (2 ^ i)
If Mask < Value Then Exit For
If Mask And Value Then
sMask = sMask & i & Separator
End If
Next i
If sMask <> "" Then GetCpuListFromMask = Left(sMask, Len(sMask) - 1)
End Function
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.