summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2022-10-21 13:52:10 -0700
committerGiampaolo Rodola <g.rodola@gmail.com>2022-10-21 13:52:10 -0700
commit13ec54ad259b2320c62779d4752a8b4af261b390 (patch)
tree63c7d14ef8b6fa6d5af4b2a24fe7cebee9a99d46
parent1cd46a0076b52a3d2ccf8e0b077fe0adae38549b (diff)
downloadpsutil-win-swap-usage-via-pagefiles.tar.gz
invoke C fun + write test for percent usagewin-swap-usage-via-pagefiles
-rw-r--r--psutil/_psutil_windows.c1
-rw-r--r--psutil/_pswindows.py19
-rw-r--r--psutil/arch/windows/disk.h1
-rwxr-xr-xpsutil/tests/test_windows.py5
4 files changed, 14 insertions, 12 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 6e51c449..6b6d3103 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -1596,6 +1596,7 @@ PsutilMethods[] = {
{"cpu_times", psutil_cpu_times, METH_VARARGS},
{"disk_io_counters", psutil_disk_io_counters, METH_VARARGS},
{"disk_partitions", psutil_disk_partitions, METH_VARARGS},
+ {"disk_swaps", psutil_disk_swaps, METH_VARARGS},
{"disk_usage", psutil_disk_usage, METH_VARARGS},
{"getloadavg", (PyCFunction)psutil_get_loadavg, METH_VARARGS},
{"getpagesize", psutil_getpagesize, METH_VARARGS},
diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py
index b546f15d..80cd3211 100644
--- a/psutil/_pswindows.py
+++ b/psutil/_pswindows.py
@@ -241,18 +241,13 @@ def virtual_memory():
def swap_memory():
"""Swap system memory as a (total, used, free, sin, sout) tuple."""
- mem = cext.virtual_mem()
-
- total_phys = mem[0]
- free_phys = mem[1]
- total_system = mem[2]
- free_system = mem[3]
-
- # system memory (commit total/limit) is the sum of physical and swap
- # thus 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
+ total = used = 0
+ pagefiles = cext.disk_swaps()
+ for pf in pagefiles:
+ path, total_size, total_in_use, peak_usage = pf
+ total += total_size
+ used += total_in_use
+ free = total - used
percent = usage_percent(used, total, round_=1)
return _common.sswap(total, used, free, percent, 0, 0)
diff --git a/psutil/arch/windows/disk.h b/psutil/arch/windows/disk.h
index 28bed22b..e0a7f895 100644
--- a/psutil/arch/windows/disk.h
+++ b/psutil/arch/windows/disk.h
@@ -8,5 +8,6 @@
PyObject *psutil_disk_io_counters(PyObject *self, PyObject *args);
PyObject *psutil_disk_partitions(PyObject *self, PyObject *args);
+PyObject *psutil_disk_swaps(PyObject *self, PyObject *args);
PyObject *psutil_disk_usage(PyObject *self, PyObject *args);
PyObject *psutil_QueryDosDevice(PyObject *self, PyObject *args);
diff --git a/psutil/tests/test_windows.py b/psutil/tests/test_windows.py
index cb2642d1..478e9010 100755
--- a/psutil/tests/test_windows.py
+++ b/psutil/tests/test_windows.py
@@ -162,6 +162,11 @@ class TestSystemAPIs(WindowsTestCase):
int(w.AvailableBytes), psutil.virtual_memory().free,
delta=TOLERANCE_SYS_MEM)
+ def test_percent_swapmem(self):
+ w = wmi.WMI().Win32_PerfRawData_PerfOS_PagingFile()[0]
+ percent = int(w.PercentUsage) * 100 / int(w.PercentUsage_Base)
+ self.assertEqual(int(percent), int(psutil.swap_memory().percent))
+
# @unittest.skipIf(wmi is None, "wmi module is not installed")
# def test__UPTIME(self):
# # _UPTIME constant is not public but it is used internally