Skip to content

Commit

Permalink
Widen 32-bit numbers before multiplying
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <widdis@gmail.com>
  • Loading branch information
dbwiddis committed Oct 20, 2022
1 parent ce76cfe commit f2b7e19
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 32 deletions.
38 changes: 9 additions & 29 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,43 +610,23 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) {
*/
static PyObject *
psutil_virtual_mem(PyObject *self, PyObject *args) {
size_t totalPhys, availPhys, totalPageFile, usedPageFile, pageSize;
unsigned long long totalPhys, availPhys, totalSys, availSys, pageSize;
PERFORMANCE_INFORMATION perfInfo;
MEMORYSTATUSEX memInfo;
memInfo.dwLength = sizeof(MEMORYSTATUSEX);

if (! GlobalMemoryStatusEx(&memInfo)) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
if (! GetPerformanceInfo(&perfInfo, sizeof(PERFORMANCE_INFORMATION))) {
PyErr_SetFromWindowsErr(0);
return NULL;
}
// values are size_t, widen to long long
pageSize = perfInfo.PageSize;
totalPhys = perfInfo.PhysicalTotal * pageSize;
availPhys = perfInfo.PhysicalAvailable * pageSize;
totalPageFile = perfInfo.CommitLimit * pageSize;
usedPageFile = perfInfo.CommitTotal * pageSize;
// PERFORMANCE_INFORMATION values are defined as SIZE_T which on 64bits
// is an (unsigned long long) and on 32bits is an (unsigned int).
#if defined(_WIN64)
return Py_BuildValue("(LLLLLL)",
(unsigned long long) totalPhys,
(unsigned long long) availPhys,
(unsigned long long) totalPageFile,
(unsigned long long) (totalPageFile - usedPageFile),
memInfo.ullTotalVirtual,
memInfo.ullAvailVirtual);
#else
return Py_BuildValue("(IIIILL)",
(unsigned int) totalPhys,
(unsigned int) availPhys,
(unsigned int) totalPageFile,
(unsigned int) (totalPageFile - usedPageFile),
memInfo.ullTotalVirtual,
memInfo.ullAvailVirtual);
#endif
totalSys = perfInfo.CommitLimit * pageSize;
availSys = totalSys - perfInfo.CommitTotal * pageSize;
return Py_BuildValue("(LLLL)",
totalPhys,
availPhys,
totalSys,
availSys);
}


Expand Down
5 changes: 2 additions & 3 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ def getpagesize():
def virtual_memory():
"""System virtual memory as a namedtuple."""
mem = cext.virtual_mem()
totphys, availphys, totpagef, availpagef, totvirt, freevirt = mem
totphys, availphys, totsys, availsys = mem
#
total = totphys
avail = availphys
Expand All @@ -248,8 +248,7 @@ def swap_memory():
total_system = mem[2]
free_system = mem[3]

# Despite the name PageFile refers to total system memory here
# thus physical memory values need to be substracted to get swap values
# physical memory values need to be substracted to get swap values
total = total_system - total_phys
free = min(total, free_system - free_phys)
used = total - free
Expand Down

0 comments on commit f2b7e19

Please sign in to comment.