Collin Funk <[email protected]> writes:

> Bruno Haible <[email protected]> writes:
>
>>> I have attached a patch that works on FreeBSD.
>>
>> Thanks! In my 32 GiB VM it reports 31.5 GB; good.
>>
>> May I ask for a few cosmetic changes:
>
> All of those requests sound good to me. I pushed the attached v2 patch
> which addresses them.

I also pushed the attached patch for NetBSD, since it feels fairly
straightforward. It only requires a single sysctl like the previously
used sysctl.

Here is it compared to the output of 'top' on a NetBSD 10.1 VM:

    $ ./gltests/test-physmem 
    Total memory:      12884369408 B =  12287 MiB
    Available memory:  12070146048 B =  11511 MiB
    $ top | grep '^Memory:'
    Memory: 196M Act, 21M Wired, 15M Exec, 168M File, 11G Free

Collin

>From 6f4b99b1e29bbd9fe6bc94e360008a407717c953 Mon Sep 17 00:00:00 2001
Message-ID: <6f4b99b1e29bbd9fe6bc94e360008a407717c953.1784172141.git.collin.fu...@gmail.com>
From: Collin Funk <[email protected]>
Date: Wed, 15 Jul 2026 20:13:02 -0700
Subject: [PATCH] physmem: allow physmem_claimable to return more than 4 GiB on
 NetBSD.

Problem reported by Bruno Haible in:
<https://lists.gnu.org/r/bug-gnulib/2026-07/msg00046.html>.

* m4/physmem.m4 (gl_PHYSMEM): Check for the presence of
uvm/uvm_extern.h.
* lib/physmem.c [HAVE_UVM_UVM_EXTERN_H]: Include uvm/uvm_extern.h.
(physmem_claimable) [HAVE_SYSCTL && VM_UVMEXP2]: Calculate the free
memory using a NetBSD-specific sysctl.
---
 ChangeLog     |  9 +++++++++
 lib/physmem.c | 20 ++++++++++++++++++++
 m4/physmem.m4 |  5 +++--
 3 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 8da6e48ef9..0dbdaf1b1b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2026-07-15  Collin Funk  <[email protected]>
 
+	physmem: allow physmem_claimable to return more than 4 GiB on NetBSD.
+	Problem reported by Bruno Haible in:
+	<https://lists.gnu.org/r/bug-gnulib/2026-07/msg00046.html>.
+	* m4/physmem.m4 (gl_PHYSMEM): Check for the presence of
+	uvm/uvm_extern.h.
+	* lib/physmem.c [HAVE_UVM_UVM_EXTERN_H]: Include uvm/uvm_extern.h.
+	(physmem_claimable) [HAVE_SYSCTL && VM_UVMEXP2]: Calculate the free
+	memory using a NetBSD-specific sysctl.
+
 	physmem: Use countof.
 	* modules/physmem (Depends-on): Add stdcountof-h.
 	* lib/physmem.c: Include stdcountof.h.
diff --git a/lib/physmem.c b/lib/physmem.c
index 41fb13ab5d..ccfb596e42 100644
--- a/lib/physmem.c
+++ b/lib/physmem.c
@@ -51,6 +51,11 @@
 # include <sys/systemcfg.h>
 #endif
 
+#if HAVE_UVM_UVM_EXTERN_H
+/* NetBSD.  */
+# include <uvm/uvm_extern.h>
+#endif
+
 #include "full-read.h"
 
 #ifdef _WIN32
@@ -297,6 +302,21 @@ physmem_claimable (double aggressivity)
   }
 #endif
 
+#if HAVE_SYSCTL && defined VM_UVMEXP2
+  { /* This works on NetBSD.  */
+    struct uvmexp_sysctl uvmexp;
+    size_t len = sizeof uvmexp;
+    static int mib[2] = { CTL_VM, VM_UVMEXP2 };
+
+    if (sysctl (mib, countof (mib), &uvmexp, &len, NULL, 0) == 0
+        && len == sizeof uvmexp)
+      {
+        double pagesize = getpagesize ();
+        return (uvmexp.free + uvmexp.inactive) * pagesize;
+      }
+  }
+#endif
+
 #if HAVE_SYSCTL && !(defined __GLIBC__ && defined __linux__) && defined HW_USERMEM
   { /* This works on *bsd, kfreebsd-gnu, and darwin.  */
     unsigned int usermem;
diff --git a/m4/physmem.m4 b/m4/physmem.m4
index 824ab451c5..fd3a8f67c7 100644
--- a/m4/physmem.m4
+++ b/m4/physmem.m4
@@ -1,5 +1,5 @@
 # physmem.m4
-# serial 14
+# serial 15
 dnl Copyright (C) 2002-2003, 2005-2006, 2008-2026 Free Software Foundation,
 dnl Inc.
 dnl This file is free software; the Free Software Foundation
@@ -32,7 +32,8 @@ AC_DEFUN([gl_PHYSMEM]
 [
   # Prerequisites of lib/physmem.c.
   AC_CHECK_HEADERS([sys/pstat.h sys/sysinfo.h \
-    machine/hal_sysinfo.h sys/table.h sys/param.h sys/systemcfg.h],,,
+    machine/hal_sysinfo.h sys/table.h sys/param.h sys/systemcfg.h
+    uvm/uvm_extern.h],,,
     [AC_INCLUDES_DEFAULT])
   dnl <sys/sysctl.h> requires <sys/param.h> on OpenBSD 4.0.
   AC_CHECK_HEADERS([sys/sysctl.h],,,
-- 
2.55.0

Reply via email to