Finding SC_PAGE_SIZE using Python in Windows

3.4k Views Asked by At

I'm working in this mixed environment where I'm on a Windows machine using Simics and Cygwin to run some code in a unix like environment. I've been coding in C but I'm interested in trying to do my solution in Python. In the unix environment to find the SC_PAGE_SIZE you can simply do:

#Python-2.7, unix environment
page_size = os.sysconf("SC_PAGE_SIZE")

If you're coding in c you can do:

#C, unix environment
size_t page_size = (size_t) sysconf (_SC_PAGESIZE);

However when using python in Windows os.sysconf doesn't exist and I've been unable to find a replacement. What can I use in python to find the PAGE_SIZE of the environment.

A side note, I know you may wonder why I'm using the setup as it is and it's not my choice. It's an homework assignment from work. The question I'm asking is for my own benefit it's not for the homework.

4

There are 4 best solutions below

0
On BEST ANSWER

Try:

import mmap

print mmap.PAGESIZE
2
On

I am not a system expert so I don't what correspond to SC_PAGE_SIZE on windows. Hovever, you can use WMI to query the system performance.

Here is an example that should give a lot of things. May you find what you are looking for:

import win32com.client

import unicodedata
def _(text):
    if type(text) is unicode:
        return unicodedata.normalize('NFKD', text).encode('ascii','ignore')
    return text

def to_kb(x):
    if x:
        return int(x)/1024
    return x

strComputer = "."
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(strComputer,"root\cimv2")
colItems = objSWbemServices.ExecQuery("Select * from Win32_Process")


for objItem in colItems:
    print "------------------------------------------"
    print "Command Line: ", _(objItem.CommandLine)
    print "Process Id: ", objItem.ProcessId

    print "Handle: ", objItem.Handle
    print "Handle Count: ", objItem.HandleCount
    print "Maximum Working Set Size: ", to_kb(objItem.MaximumWorkingSetSize)
    print "Minimum Working Set Size: ", to_kb(objItem.MinimumWorkingSetSize)
    print "Page Faults: ", objItem.PageFaults
    print "PageFile Usage: ", objItem.PageFileUsage
    print "Peak PageFile Usage: ", objItem.PeakPageFileUsage
    print "Peak Virtual Size: ", objItem.PeakVirtualSize
    print "Peak Working Set Size: ", objItem.PeakWorkingSetSize
    print "Private Page Count: ", objItem.PrivatePageCount
    print "Quota NonPaged Pool Usage: ", objItem.QuotaNonPagedPoolUsage
    print "Quota Paged Pool Usage: ", objItem.QuotaPagedPoolUsage
    print "Quota Peak NonPaged Pool Usage: ", objItem.QuotaPeakNonPagedPoolUsage
    print "Quota Peak Paged Pool Usage: ", objItem.QuotaPeakPagedPoolUsage
    print "Virtual Size: ", objItem.VirtualSize
    print "Working Set Size: ", to_kb(objItem.WorkingSetSize)
    print "Write Operation Count: ", objItem.WriteOperationCount
    print "Write Transfer Count: ", objItem.WriteTransferCount
0
On

The only equivalent I could find was in C but if I compile the code and then execute it from python I can get the result I was looking for. Unfortunately as of right now there doesn't seem to be a python command that works in Windows that is as simple as the unix version but this at least gives me a result.

int main(void) {
        SYSTEM_INFO si;
        GetSystemInfo(&si);

        printf("%u", si.dwPageSize);

        return 0;
}
0
On

It is possible to do it with ctypes module:

from ctypes import Structure, byref, windll
from ctypes.wintypes import WORD, DWORD, LPVOID


class SYSTEM_INFO(Structure):
    _fields_ = [
        ("wProcessorArchitecture", WORD),
        ("wReserved", WORD),
        ("dwPageSize", DWORD),
        ("lpMinimumApplicationAddress", LPVOID),
        ("lpMaximumApplicationAddress", LPVOID),
        ("dwActiveProcessorMask", DWORD),
        ("dwNumberOfProcessors", DWORD),
        ("dwProcessorType", DWORD),
        ("dwAllocationGranularity", DWORD),
        ("wProcessorLevel", WORD),
        ("wProcessorRevision", WORD),
    ]


si = SYSTEM_INFO()
windll.kernel32.GetSystemInfo(byref(si))
print(si.dwPageSize)