Gringo Suave
Gringo Suave

Reputation: 31880

How to find the current System Cache Size on Windows?

Searched high and low but haven't been able to find an API call to retrieve the current size of the (File) System Cache on Windows.

GlobalMemoryStatusEx - retrieves the Total, Free, Used, and Swap stats.

GetSystemFileCacheSize - returns the Minimum and Maximum it could be, not very helpful.

I also tried the Windows extensions, which returned the unhelpful numbers below. Looks like it could be anywhere from 1mb to 2gb?

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

What is the correct API call to get this information? I see it available in the task manager, so it must be in there somewhere? Here's a screen shot and the numbers I'm looking for:

enter image description here

Would prefer a python solution, but C/C++ would help a lot.

Upvotes: 3

Views: 2590

Answers (1)

Gringo Suave
Gringo Suave

Reputation: 31880

I did finally figure it out:

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())

Upvotes: 2

Related Questions