0

I am trying to invoke the GetLogicalProcessorInformation process in Python but I'm not sure how to set up the struct. I have done it with GlobalMemoryStatusEx with the following code

from ctypes import Structure, c_int32, c_uint64, sizeof, byref, windll

class MemoryStatusEx(Structure):
_fields_ = [
    ("length", c_int32),
    ("memory_load", c_int32),
    ("total_physical", c_uint64),
    ("available_physical", c_uint64),
    ("total_page_file", c_uint64),
    ("available_page_file", c_uint64),
    ("total_virtual", c_uint64),
    ("available_virtual", c_uint64),
    ("available_extended_virtual", c_uint64)
]

def __init__(self):
    self.length = sizeof(self)
    assert windll.kernel32.GlobalMemoryStatusEx(byref(self))

As I said, I'm not sure how to do it in Python as it makes use of UNION and LPVOID. Any help is appreciated.

1 Answer 1

1

Tested example Python 3.8 64-bit on Windows 10:

from ctypes import *
from ctypes import wintypes as w

ULONG_PTR = c_size_t  # c_ulong on Win32, c_ulonglong on Win64.
ULONGLONG = c_ulonglong

ERROR_INSUFFICIENT_BUFFER = 122

class CACHE_DESCRIPTOR(Structure):
    _fields_ = (('Level',w.BYTE),
                ('Associativity',w.BYTE),
                ('LineSize',w.WORD),
                ('Size',w.DWORD),
                ('Type',c_int))

class ProcessorCore(Structure):
    _fields_ = ('Flags', w.BYTE),

class NumaNode(Structure):
    _fields_ = ('NodeNumber', w.DWORD),

class DUMMYUNIONNAME(Union):
    _fields_ = (('ProcessorCore',ProcessorCore),
                ('NumaNode',NumaNode),
                ('Cache',CACHE_DESCRIPTOR),
                ('Reserved',ULONGLONG * 2))

class SYSTEM_LOGICAL_PROCESSOR_INFORMATION(Structure):
    _anonymous_ = 'DUMMYUNIONNAME',
    _fields_ = (('ProcessorMask', ULONG_PTR),
                ('Relationship', c_int),
                ('DUMMYUNIONNAME',DUMMYUNIONNAME))

dll = WinDLL('kernel32',use_last_error=True)
dll.GetLogicalProcessorInformation.argtypes = POINTER(SYSTEM_LOGICAL_PROCESSOR_INFORMATION),w.LPDWORD
dll.GetLogicalProcessorInformation.restype = w.BOOL

# wrapper for easier use
def GetLogicalProcessorInformation():
    bytelength = w.DWORD()
    structlength = sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION)
    # Call with null buffer to get required buffer size
    result = dll.GetLogicalProcessorInformation(None,byref(bytelength))
    if (err := get_last_error()) != ERROR_INSUFFICIENT_BUFFER:
        raise WinError(err)
    no_of_structures = bytelength.value // structlength
    arr = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION * no_of_structures)()
    result = dll.GetLogicalProcessorInformation(arr,byref(bytelength))
    if not result:
        raise WinError(get_last_error())
    return arr

infos = GetLogicalProcessorInformation()
print(f'nodes = {len(infos)}')
for info in infos:
    print(f'{info.ProcessorMask:8b} {info.Relationship} L{info.Cache.Level} {info.Cache.Size}')
nodes = 19
      11 0 L1 0
      11 2 L1 32768
      11 2 L1 32768
      11 2 L2 262144
    1100 0 L1 0
    1100 2 L1 32768
    1100 2 L1 32768
    1100 2 L2 262144
  110000 0 L1 0
  110000 2 L1 32768
  110000 2 L1 32768
  110000 2 L2 262144
11111111 3 L0 0
11000000 0 L1 0
11000000 2 L1 32768
11000000 2 L1 32768
11000000 2 L2 262144
11111111 2 L3 8388608
11111111 1 L0 0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.