From 904a7f0c0b7761b59c552aeb58b97195840e3b4a Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 31 Aug 2025 19:27:43 +0200 Subject: [PATCH 1/4] [explorer/cpu_{cores,sockets}] Assume cores instead of sockets by default and fix for empty /proc/cpuinfo Assume that multiple processors are cores instead of sockets. For most architectures sockets are not reported using "physical id" in /proc/cpuinfo, making this explorer report all cores as separate sockets. This does not make much sense. For most systems assuming one socket and N cores is more correct. On NetBSD /proc/cpuinfo is only implemented for a few architectures. For all the others /proc/cpuinfo is an empty file (which makes grep -c exit 1). On NetBSD also currently no architecture reports "physical id", so socket detection using /proc/cpuinfo will never work. It is thus disabled on NetBSD until somebody finds another way. --- explorer/cpu_cores | 5 +++-- explorer/cpu_sockets | 9 ++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/explorer/cpu_cores b/explorer/cpu_cores index 10a2e5749..7d8ab3fda 100755 --- a/explorer/cpu_cores +++ b/explorer/cpu_cores @@ -45,8 +45,9 @@ in (*) if test -r /proc/cpuinfo then - cores=$(grep -c -e '^processor[[:blank:]]*:' /proc/cpuinfo) - test $((cores)) -ge 1 && echo $((cores)) || echo 1 + cores=$(grep -c -e '^processor[[:blank:]]*:' /proc/cpuinfo || :) + test $((cores)) -gt 0 || cores=1 + printf '%u\n' "${cores}" fi ;; esac diff --git a/explorer/cpu_sockets b/explorer/cpu_sockets index 8dccf1901..39f98b265 100755 --- a/explorer/cpu_sockets +++ b/explorer/cpu_sockets @@ -28,15 +28,14 @@ in (macosx) system_profiler SPHardwareDataType | grep -F 'Number of Processors' | awk -F': ' '{print $2}' ;; + (netbsd) + ;; (*) if test -r /proc/cpuinfo then sockets=$(grep -e '^physical id[[:blank:]]*:' /proc/cpuinfo | sort -u | wc -l) - if test $((sockets)) -lt 1 - then - sockets=$(grep -c -e '^processor[[:blank:]]*:' /proc/cpuinfo) - fi - echo $((sockets)) + test $((sockets)) -gt 0 || sockets=1 + printf '%u\n' "${sockets}" fi ;; esac From 221a8a0902fa1c754733cdc8bc72569de7c3ebdc Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 31 Aug 2025 19:31:47 +0200 Subject: [PATCH 2/4] [explorer/cpu_cores] Update detection for NetBSD, fallback for old OpenBSD NetBSD also has hw.ncpuonline like OpenBSD (in fact, OpenBSD imported the code for it from NetBSD). Let's merge the code for NetBSD and OpenBSD instead of FreeBSD. On OpenBSD sysctl(8) never returns a non-zero exit status, so `grep .' is used to detect success and fall back to hw.ncpu. https://marc.info/?l=openbsd-cvs&m=153135863131342&w=2 --- explorer/cpu_cores | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/explorer/cpu_cores b/explorer/cpu_cores index 7d8ab3fda..d83dfcb8f 100755 --- a/explorer/cpu_cores +++ b/explorer/cpu_cores @@ -35,11 +35,12 @@ in (macosx) sysctl -n hw.physicalcpu ;; - (openbsd) - sysctl -n hw.ncpuonline + (netbsd|openbsd) + # OpenBSD sysctl(8) always exits 0 (thus `grep .' to check for success) + /sbin/sysctl -n hw.ncpuonline 2>/dev/null | grep . \ + || /sbin/sysctl -n hw.ncpu ;; - (freebsd|netbsd) - PATH=$(getconf PATH) + (freebsd) sysctl -n hw.ncpu ;; (*) From e7a868aac5d4ee9765d409c589cdab12af7425e6 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 31 Aug 2025 21:15:08 +0200 Subject: [PATCH 3/4] [explorer/memory] Use hw.physmem64 on NetBSD OpenBSD exports HW_PHYSMEM64 as hw.physmem. NetBSD exports HW_PHYSMEM as hw.physmem and HW_PHYSMEM64 as hw.physmem64 (since NetBSD 2.0). --- explorer/memory | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/explorer/memory b/explorer/memory index 9162e3747..2c5bee0c8 100755 --- a/explorer/memory +++ b/explorer/memory @@ -62,9 +62,17 @@ in (FreeBSD) sysctl -n hw.realmem | bytes2kib ;; - (NetBSD|OpenBSD) - # NOTE: This reports "usable" memory, not physically installed memory. - command -p sysctl -n hw.physmem | bytes2kib + (NetBSD) + # NOTE: This reports "usable" memory, not physically installed memory + # (on some architectures). + # + # hw.physmem64 is available on NetBSD >= 2.0 + /sbin/sysctl -n hw.physmem64 | bytes2kib + ;; + (OpenBSD) + # NOTE: This reports "usable" memory, not physically installed memory + # (on some architectures). + sysctl -n hw.physmem | bytes2kib ;; (SunOS) # Make sure that awk from xpg4 is used for the scripts to work From 57ab6e7d12d87bea666b6b9c61a62700b6e3099e Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Sun, 31 Aug 2025 21:39:49 +0200 Subject: [PATCH 4/4] [explorer/memory] Fall back to meminfo if memory blocks detection fails In the unlikely (or impossible?) case that there are no online memory blocks, fall back to /proc/meminfo. --- explorer/memory | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/explorer/memory b/explorer/memory index 2c5bee0c8..7338f6d7f 100755 --- a/explorer/memory +++ b/explorer/memory @@ -89,10 +89,12 @@ in then # Use memory blocks if the architecture (e.g. x86, PPC64, s390) # supports them (they denote physical memory) - num_mem_blocks=$(cat /sys/devices/system/memory/memory[0-9]*/state | grep -cxF online) - mem_block_size=$(cat /sys/devices/system/memory/block_size_bytes) - - echo $((num_mem_blocks * 0x${mem_block_size})) | bytes2kib && exit + if num_mem_blocks=$(cat /sys/devices/system/memory/memory[0-9]*/state 2>/dev/null | grep -cxF online) + then + mem_block_size=$(cat /sys/devices/system/memory/block_size_bytes) + echo $((num_mem_blocks * 0x${mem_block_size})) | bytes2kib + exit 0 + fi fi if test -r /proc/meminfo then