From 000ec1f5e0992d9fe2fa1ca11348c2a78a8d9582 Mon Sep 17 00:00:00 2001 From: Daniel Widdis Date: Fri, 21 Oct 2022 11:07:38 -0700 Subject: [PATCH] Add tests Signed-off-by: Daniel Widdis --- CREDITS | 2 +- HISTORY.rst | 2 ++ psutil/_pswindows.py | 2 +- psutil/tests/test_windows.py | 25 +++++++++++++++++++++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/CREDITS b/CREDITS index 1990231593..9aa95ff74c 100644 --- a/CREDITS +++ b/CREDITS @@ -804,7 +804,7 @@ I: 2150 N: Daniel Widdis W: https://github.com/dbwiddis -I: 2077 +I: 2077, 2160 N: Amir Rossert W: https://github.com/arossert diff --git a/HISTORY.rst b/HISTORY.rst index 2b1257bd4b..b30a0c3738 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -19,6 +19,8 @@ XXXX-XX-XX ``SPEED_UNKNOWN`` definition. (patch by Amir Rossert) - 2010_, [macOS]: on MacOS, arm64 ``IFM_1000_TX`` and ``IFM_1000_T`` are the same value, causing a build failure. (patch by Lawrence D'Anna) +- 2160_, [Windows]: Handle mismatch between committed vs. used physical memory. + (patch by Daniel Widdis) 5.9.3 ===== diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py index fcd2c32eb2..9661882717 100644 --- a/psutil/_pswindows.py +++ b/psutil/_pswindows.py @@ -256,7 +256,7 @@ def swap_memory(): # pages are accessed, so free_phys is an overestimate of the portion # free_system contributed to by physical memory, and in some edge cases # can exceed free system memory. - free = max(0, free_system - free_phys) + free = max(0, min(total, free_system - free_phys)) used = total - free percent = usage_percent(used, total, round_=1) return _common.sswap(total, used, free, percent, 0, 0) diff --git a/psutil/tests/test_windows.py b/psutil/tests/test_windows.py index 3bf458079f..776579073f 100755 --- a/psutil/tests/test_windows.py +++ b/psutil/tests/test_windows.py @@ -123,6 +123,31 @@ def test_total_phymem(self): self.assertEqual(int(w.TotalPhysicalMemory), psutil.virtual_memory().total) + def test_total_swapmem(self): + if (psutil.swap_memory().total > 0): + w = wmi.WMI().Win32_PerfRawData_PerfOS_Memory()[0] + self.assertEqual(int(w.CommitLimit) + - psutil.virtual_memory().total, + psutil.swap_memory().total) + else: + self.assertEqual(0, psutil.swap_memory().total) + self.assertEqual(0, psutil.swap_memory().free) + self.assertEqual(0, psutil.swap_memory().used) + + def test_percent_swapmem(self): + if (psutil.swap_memory().total > 0): + w = wmi.WMI().Win32_PerfRawData_PerfOS_PagingFile()[0] + # calculate swap usage to integer percent + percentSwap = int(w.PercentUsage) * 100 / int(w.PercentUsage_Base) + # exact percent may change but should be reasonable + # assert within +/- 10% and between 0 and 100 + self.assertGreaterEqual(psutil.swap_memory().percent, 0) + self.assertGreaterEqual(psutil.swap_memory().percent, + percentSwap - 10) + self.assertLessEqual(psutil.swap_memory().percent, + percentSwap + 10) + self.assertLessEqual(psutil.swap_memory().percent, 100) + # @unittest.skipIf(wmi is None, "wmi module is not installed") # def test__UPTIME(self): # # _UPTIME constant is not public but it is used internally