在 Windows 中使用 Python 查找 SC_PAGE_SIZE

发布于 2024-12-26 05:54:24 字数 581 浏览 2 评论 0原文

我在这个混合环境中工作,在 Windows 计算机上使用 Simics 和 Cygwin 在类似 UNIX 的环境中运行一些代码。我一直在用 C 进行编码,但我有兴趣尝试用 Python 来完成我的解决方案。在unix环境中,要找到SC_PAGE_SIZE,您可以简单地执行以下操作:

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

如果您在c中编码,则可以执行以下操作:

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

但是,在Windows中使用python时,os.sysconf不会存在,但我一直无法找到替代品。我可以在 python 中使用什么来查找环境的 PAGE_SIZE

附注,我知道您可能想知道为什么我要使用现有的设置,而这不是我的选择。这是工作中的家庭作业。我问这个问题是为了我自己的利益,而不是为了作业。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

待"谢繁草 2025-01-02 05:54:25

尝试:

import mmap

print mmap.PAGESIZE

Try:

import mmap

print mmap.PAGESIZE
吖咩 2025-01-02 05:54:25

我不是系统专家,所以我不知道 Windows 上的 SC_PAGE_SIZE 对应什么。不过,您可以使用WMI来查询系统性能。

这是一个应该给出很多东西的例子。愿您找到您正在寻找的东西:

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

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
呆橘 2025-01-02 05:54:25

我能找到的唯一等效的语言是 C 语言,但如果我编译代码然后从 python 执行它,我可以获得我正在寻找的结果。不幸的是,到目前为止,似乎没有一个可以在 Windows 中工作的 python 命令像 unix 版本一样简单,但这至少给了我一个结果。

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

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

        return 0;
}

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;
}
别靠近我心 2025-01-02 05:54:25

可以使用 ctypes 模块来做到这一点:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文