diff options
author | Daniel Widdis <widdis@gmail.com> | 2022-10-20 16:11:21 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-21 01:11:21 +0200 |
commit | cd8827d065d63114060aae69f36b447006c5a70f (patch) | |
tree | 643103d217b252fa0282acf1a03c119746fe1a30 /psutil/_psutil_windows.c | |
parent | 614e91158ced7a65aabbe7244d52de1cebfc494b (diff) | |
download | psutil-cd8827d065d63114060aae69f36b447006c5a70f.tar.gz |
Use system-level values for Windows virtual memory (#2077)
* Use system-level values for Windows virtual memory
Submitting this as a draft PR as I believe it will address #2074. I do not have either a C or Python development environment to test these changes, so they are provided in the hopes someone else can test and confirm they fix the issue.
There's probably some room to simplify the code with temporary variables to reduce redundancy.
For background, I have implemented precisely this logic in my own (Java-based) project [here](https://github.com/oshi/oshi/blob/3bb9eafbe2062edad4108b7b12501b1f9be27361/oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsVirtualMemory.java#L110-L118) and [here](https://github.com/oshi/oshi/blob/3bb9eafbe2062edad4108b7b12501b1f9be27361/oshi-core/src/main/java/oshi/hardware/platform/windows/WindowsGlobalMemory.java#L253-L263) following a detailed investigation in https://github.com/oshi/oshi/issues/1182
Signed-off-by: Daniel Widdis <widdis@gmail.com>
* Formatting, credits, change log
Signed-off-by: Daniel Widdis <widdis@gmail.com>
Signed-off-by: Daniel Widdis <widdis@gmail.com>
Diffstat (limited to 'psutil/_psutil_windows.c')
-rw-r--r-- | psutil/_psutil_windows.c | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 83da3a26..6e51c449 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -610,20 +610,25 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) { */ static PyObject * psutil_virtual_mem(PyObject *self, PyObject *args) { - MEMORYSTATUSEX memInfo; - memInfo.dwLength = sizeof(MEMORYSTATUSEX); + unsigned long long totalPhys, availPhys, totalSys, availSys, pageSize; + PERFORMANCE_INFORMATION perfInfo; - if (! GlobalMemoryStatusEx(&memInfo)) { + if (! GetPerformanceInfo(&perfInfo, sizeof(PERFORMANCE_INFORMATION))) { PyErr_SetFromWindowsErr(0); return NULL; } - return Py_BuildValue("(LLLLLL)", - memInfo.ullTotalPhys, // total - memInfo.ullAvailPhys, // avail - memInfo.ullTotalPageFile, // total page file - memInfo.ullAvailPageFile, // avail page file - memInfo.ullTotalVirtual, // total virtual - memInfo.ullAvailVirtual); // avail virtual + // values are size_t, widen (if needed) to long long + pageSize = perfInfo.PageSize; + totalPhys = perfInfo.PhysicalTotal * pageSize; + availPhys = perfInfo.PhysicalAvailable * pageSize; + totalSys = perfInfo.CommitLimit * pageSize; + availSys = totalSys - perfInfo.CommitTotal * pageSize; + return Py_BuildValue( + "(LLLL)", + totalPhys, + availPhys, + totalSys, + availSys); } |