summaryrefslogtreecommitdiff
path: root/psutil/_psbsd.py
diff options
context:
space:
mode:
Diffstat (limited to 'psutil/_psbsd.py')
-rw-r--r--psutil/_psbsd.py41
1 files changed, 23 insertions, 18 deletions
diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py
index c1cc1c03..9dd8420d 100644
--- a/psutil/_psbsd.py
+++ b/psutil/_psbsd.py
@@ -176,11 +176,10 @@ else:
# =====================================================================
-if NETBSD:
- def virtual_memory():
- """System virtual memory as a namedtuple."""
- mem = cext.virtual_mem()
- total, free, active, inactive, wired, cached, avail = mem
+def virtual_memory():
+ mem = cext.virtual_mem()
+ if NETBSD:
+ total, free, active, inactive, wired, cached = mem
# On NetBSD buffers and shared mem is determined via /proc.
# The C ext set them to 0.
with open('/proc/meminfo', 'rb') as f:
@@ -189,22 +188,28 @@ if NETBSD:
buffers = int(line.split()[1]) * 1024
elif line.startswith(b'MemShared:'):
shared = int(line.split()[1]) * 1024
- elif line.startswith(b'Cached:'):
- cached = int(line.split()[1]) * 1024
- used = active + wired + cached
- percent = usage_percent((total - avail), total, round_=1)
- return svmem(total, avail, percent, used, free,
- active, inactive, buffers, cached, shared, wired)
-else:
- def virtual_memory():
- """System virtual memory as a namedtuple."""
- mem = cext.virtual_mem()
+ # Before avail was calculated as (inactive + cached + free),
+ # same as zabbix, but it turned out it could exceed total (see
+ # #2233), so zabbix seems to be wrong. Htop calculates it
+ # differently, and the used value seem more realistic, so let's
+ # match htop.
+ # https://github.com/htop-dev/htop/blob/e7f447b/netbsd/NetBSDProcessList.c#L162 # noqa
+ # https://github.com/zabbix/zabbix/blob/af5e0f8/src/libs/zbxsysinfo/netbsd/memory.c#L135 # noqa
+ used = active + wired
+ avail = total - used
+ else:
total, free, active, inactive, wired, cached, buffers, shared = mem
+ # matches freebsd-memory CLI:
+ # * https://people.freebsd.org/~rse/dist/freebsd-memory
+ # * https://www.cyberciti.biz/files/scripts/freebsd-memory.pl.txt
+ # matches zabbix:
+ # * https://github.com/zabbix/zabbix/blob/af5e0f8/src/libs/zbxsysinfo/freebsd/memory.c#L143 # noqa
avail = inactive + cached + free
used = active + wired + cached
- percent = usage_percent((total - avail), total, round_=1)
- return svmem(total, avail, percent, used, free,
- active, inactive, buffers, cached, shared, wired)
+
+ percent = usage_percent((total - avail), total, round_=1)
+ return svmem(total, avail, percent, used, free,
+ active, inactive, buffers, cached, shared, wired)
def swap_memory():