3

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.

Community
  • 1
  • 1
Gringo Suave
  • 29,931
  • 6
  • 88
  • 75
  • 1
    You can try to get programmatically 4 performance counters `Cached` = `\Memory\Modified Page List Bytes` + `\Memory\Standby Cache Reserve Bytes` + `\Memory\Standby Cache Normal Priority Bytes` + `\Memory\Standby Cache Core Bytes` – Serg Jan 17 '13 at 07:51
  • Thanks for your help. I wasn't able to find an api call after many hours of searching. Looking up these perf counters and why they didn't match the task manager led me to a mailing list where a post pointed to the call below. ;) – Gringo Suave Jan 18 '13 at 10:15

1 Answers1

2

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())
Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Gringo Suave
  • 29,931
  • 6
  • 88
  • 75