如何在Windows上找到当前的系统缓存大小?

时间:2013-01-17 02:59:26

标签: python c windows winapi ctypes

搜索高低,但无法找到API调用来检索Windows上的(文件)系统缓存的当前大小。

GlobalMemoryStatusEx - 检索Total,Free,Used和Swap统计信息。

GetSystemFileCacheSize - 返回可能的最小值和最大值,不是很有用。

我还尝试了Windows扩展,它返回了下面无用的数字。看起来它可能在1mb到2gb之间?

>>> import win32api
>>> win32api.GetSystemFileCacheSize()
(1048576L, 2143289344L, 0L)

获取此信息的正确API调用是什么?我看到它在任务管理器中可用,所以它必须在某处?这是一个屏幕截图和我正在寻找的数字:

enter image description here

更喜欢python解决方案,但C / C ++会有很多帮助。

1 个答案:

答案 0 :(得分:2)

我终于搞清楚了:

import ctypes
psapi = ctypes.WinDLL('psapi')

class PERFORMANCE_INFORMATION(ctypes.Structure):
    ''' Struct for Windows .GetPerformanceInfo().
        http://msdn.microsoft.com/en-us/library/ms683210
    '''

    _DWORD = ctypes.c_ulong
    _SIZE_T = ctypes.c_size_t

    _fields_ = [
        ('cb', _DWORD),
        ('CommitTotal', _SIZE_T),
        ('CommitLimit', _SIZE_T),
        ('CommitPeak', _SIZE_T),
        ('PhysicalTotal', _SIZE_T),
        ('PhysicalAvailable', _SIZE_T),
        ('SystemCache', _SIZE_T),
        ('KernelTotal', _SIZE_T),
        ('KernelPaged', _SIZE_T),
        ('KernelNonpaged', _SIZE_T),
        ('PageSize', _SIZE_T),
        ('HandleCount', _DWORD),
        ('ProcessCount', _DWORD),
        ('ThreadCount', _DWORD),
    ]

    def __init__(self, getinfo=True, *args, **kwds):
        super(PERFORMANCE_INFORMATION, self).__init__(
              ctypes.sizeof(self), *args, **kwds)
        if (getinfo and not
            psapi.GetPerformanceInfo(ctypes.byref(self), 
                                     self.cb)):
            raise WinError()

    @property
    def cache_info(self):
        return self.SystemCache * self.PageSize

def get_cache_info():
    return PERFORMANCE_INFORMATION().cache_info

if __name__ == '__main__':
    print(get_cache_info())