git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] builtin/gc: correct physical memory detection for OpenBSD / NetBSD
@ 2025-06-01  8:24 Brad Smith
  0 siblings, 0 replies; 3+ messages in thread
From: Brad Smith @ 2025-06-01  8:24 UTC (permalink / raw)
  To: git

OpenBSD / NetBSD use HW_PHYSMEM64 to detect the amount of physical
memory in a system. HW_PHYSMEM will not provide the correct amount
on a system with >=4GB of memory.

Signed-off-by: Brad Smith <brad@comstyle.com>
---
 builtin/gc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/builtin/gc.c b/builtin/gc.c
index e690453d4f..eacb445085 100644
--- a/builtin/gc.c
+++ b/builtin/gc.c
@@ -431,7 +431,7 @@ static uint64_t total_ram(void)
 			total *= (uint64_t)si.mem_unit;
 		return total;
 	}
-#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM))
+#elif defined(HAVE_BSD_SYSCTL) && (defined(HW_MEMSIZE) || defined(HW_PHYSMEM) || defined(HW_PHYSMEM64))
 	int64_t physical_memory;
 	int mib[2];
 	size_t length;
@@ -439,6 +439,8 @@ static uint64_t total_ram(void)
 	mib[0] = CTL_HW;
 # if defined(HW_MEMSIZE)
 	mib[1] = HW_MEMSIZE;
+# elif defined(HW_PHYSMEM64)
+	mib[1] = HW_PHYSMEM64;
 # else
 	mib[1] = HW_PHYSMEM;
 # endif
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread
* Re: [PATCH] builtin/gc: correct physical memory detection for OpenBSD / NetBSD
@ 2025-06-02  1:34 Collin Funk
  2025-06-02  2:00 ` Junio C Hamano
  0 siblings, 1 reply; 3+ messages in thread
From: Collin Funk @ 2025-06-02  1:34 UTC (permalink / raw)
  To: Brad Smith, git

Hi Brad,

You wrote:

> OpenBSD / NetBSD use HW_PHYSMEM64 to detect the amount of physical
> memory in a system. HW_PHYSMEM will not provide the correct amount
> on a system with >=4GB of memory.
>
> Signed-off-by: Brad Smith <brad@comstyle.com>
> ---
> builtin/gc.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)

I confirm this patch is correct.

Reviewed-by: Collin Funk <collin.funk1@gmail.com>

I also used the following test program:

------------------------------------------------------------------------
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/sysctl.h>
#define ARRAY_SIZE(array) (sizeof array / sizeof *array)
int
main (void)
{
  {
    unsigned int physmem;
    size_t len = sizeof physmem;
    static int mib[2] = { CTL_HW, HW_PHYSMEM };
    if (!(sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0
          && len == sizeof (physmem)))
      abort ();
    printf ("HW_PHYSMEM: %jd\n", (intmax_t) physmem);
  }
  {
    int64_t physmem;
    size_t len = sizeof physmem;
    static int mib[2] = { CTL_HW, HW_PHYSMEM64 };
    if (!(sysctl (mib, ARRAY_SIZE (mib), &physmem, &len, NULL, 0) == 0
          && len == sizeof (physmem)))
      abort ();
    printf ("HW_PHYSMEM64: %jd\n", (intmax_t) physmem);
  }
  return 0;
}
------------------------------------------------------------------------

On NetBSD 10.0:

    $ ./a.out
    HW_PHYSMEM: 4294967295
    HW_PHYSMEM64: 17153662976

OpenBSD 7.6:

    $ ./a.out
    HW_PHYSMEM: 4286128128
    HW_PHYSMEM64: 17171030016

Thanks for the fix.

Collin

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-02  2:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-06-01  8:24 [PATCH] builtin/gc: correct physical memory detection for OpenBSD / NetBSD Brad Smith
  -- strict thread matches above, loose matches on Subject: below --
2025-06-02  1:34 Collin Funk
2025-06-02  2:00 ` Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).