我终于弄明白了:
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())