summaryrefslogtreecommitdiff
path: root/psutil
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2023-04-15 13:39:02 +0200
committerGitHub <noreply@github.com>2023-04-15 13:39:02 +0200
commita0b096c88421548593ecebe93bbe369385087f3b (patch)
tree6a9706d587ea2ff06d1cb7f4c26b712d4ccdd706 /psutil
parent9322179812d3177719c7bab23301646bbf862974 (diff)
downloadpsutil-a0b096c88421548593ecebe93bbe369385087f3b.tar.gz
[NetBSD] virtual_memory() metrics are completely wrong (#2235), fixes #2234
Match values shown by **htop**. This is before. In here: * available mem is almost the same as total (unrealistic) * used is higher than total; there's also a failing test: ``` MEMORY ------ Total : 972.7M Available : 959.1M Percent : 1.4 Used : 1.1G Free : 173.6M Active : 434.3M Inactive : 258.4M Buffers : 509.4M Cached : 692.9M Shared : 0.0B Wired : 280.0K ``` Now: ``` MEMORY ------ Total : 972.7M Available : 538.1M Percent : 44.7 Used : 434.5M Free : 173.6M Active : 434.2M Inactive : 258.4M Buffers : 509.4M Cached : 692.9M Shared : 0.0B Wired : 280.0K ```
Diffstat (limited to 'psutil')
-rw-r--r--psutil/_psbsd.py41
-rw-r--r--psutil/arch/netbsd/mem.c24
2 files changed, 35 insertions, 30 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():
diff --git a/psutil/arch/netbsd/mem.c b/psutil/arch/netbsd/mem.c
index 26ab8e69..456479ba 100644
--- a/psutil/arch/netbsd/mem.c
+++ b/psutil/arch/netbsd/mem.c
@@ -31,8 +31,7 @@ psutil_virtual_mem(PyObject *self, PyObject *args) {
size_t size;
struct uvmexp_sysctl uv;
int mib[] = {CTL_VM, VM_UVMEXP2};
- long pagesize = psutil_getpagesize();
- unsigned long long available;
+ long long cached;
size = sizeof(uv);
if (sysctl(mib, 2, &uv, &size, NULL, 0) < 0) {
@@ -40,17 +39,18 @@ psutil_virtual_mem(PyObject *self, PyObject *args) {
return NULL;
}
- // follow zabbix
- available = uv.inactive + uv.execpages + uv.filepages + uv.free;
+ // Note: zabbix does not include anonpages, but that doesn't match the
+ // "Cached" value in /proc/meminfo.
+ // https://github.com/zabbix/zabbix/blob/af5e0f8/src/libs/zbxsysinfo/netbsd/memory.c#L182
+ cached = (uv.filepages + uv.execpages + uv.anonpages) << uv.pageshift;
return Py_BuildValue(
- "KKKKKKK",
- (unsigned long long) uv.npages << uv.pageshift, // total
- (unsigned long long) uv.free << uv.pageshift, // free
- (unsigned long long) uv.active << uv.pageshift, // active
- (unsigned long long) uv.inactive << uv.pageshift, // inactive
- (unsigned long long) uv.wired << uv.pageshift, // wired
- (unsigned long long) (uv.filepages + uv.execpages) * pagesize, // cached
- available << uv.pageshift // available
+ "LLLLLL",
+ (long long) uv.npages << uv.pageshift, // total
+ (long long) uv.free << uv.pageshift, // free
+ (long long) uv.active << uv.pageshift, // active
+ (long long) uv.inactive << uv.pageshift, // inactive
+ (long long) uv.wired << uv.pageshift, // wired
+ cached // cached
);
}