bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

nproc, physmem: Use sysctl() as a fallback on GNU/kFreeBSD


From: Bruno Haible
Subject: nproc, physmem: Use sysctl() as a fallback on GNU/kFreeBSD
Date: Mon, 14 Aug 2023 01:11:30 +0200

There was no need to disable the use of sysctl() on GNU/kFreeBSD on 2020-02-02,
since the glibc change re <sys/sysctl.h> was only regarding Linux [1].

Fortunately that change was not a regression, since sysconf() apparently
produces the same values as sysctl(); nevertheless, it's useful to have a
fallback (for whatever reason sysconf() may fail).

[1] 
https://sourceware.org/git/?p=glibc.git;a=commitdiff;h=076f09afbac1aa57756faa7a8feadb7936a724e4


2023-08-13  Bruno Haible  <bruno@clisp.org>

        nproc, physmem: Use sysctl() as a fallback on GNU/kFreeBSD.
        * lib/nproc.c: Do include <sys/sysctl.h> on GNU/kFreeBSD.
        (num_processors_ignoring_omp): Call sysctl on GNU/kFreeBSD.
        * lib/physmem.c: Do include <sys/sysctl.h> on GNU/kFreeBSD.
        (physmem_total, physmem_available): Call sysctl on GNU/kFreeBSD.

diff --git a/lib/nproc.c b/lib/nproc.c
index 2740c458c1..e3de1873a9 100644
--- a/lib/nproc.c
+++ b/lib/nproc.c
@@ -46,7 +46,7 @@
 # include <sys/param.h>
 #endif
 
-#if HAVE_SYS_SYSCTL_H && ! defined __GLIBC__
+#if HAVE_SYS_SYSCTL_H && !(defined __GLIBC__ && defined __linux__)
 # include <sys/sysctl.h>
 #endif
 
@@ -306,7 +306,7 @@ num_processors_ignoring_omp (enum nproc_query query)
   /* Finally, as fallback, use the APIs that don't distinguish between
      NPROC_CURRENT and NPROC_ALL.  */
 
-#if HAVE_SYSCTL && ! defined __GLIBC__ && defined HW_NCPU
+#if HAVE_SYSCTL && !(defined __GLIBC__ && defined __linux__) && defined HW_NCPU
   { /* This works on macOS, FreeBSD, NetBSD, OpenBSD.
        macOS 10.14 does not allow mib to be const.  */
     int nprocs;
diff --git a/lib/physmem.c b/lib/physmem.c
index f450587141..398f99b727 100644
--- a/lib/physmem.c
+++ b/lib/physmem.c
@@ -50,7 +50,7 @@
 # include <sys/param.h>
 #endif
 
-#if HAVE_SYS_SYSCTL_H && ! defined __GLIBC__
+#if HAVE_SYS_SYSCTL_H && !(defined __GLIBC__ && defined __linux__)
 # include <sys/sysctl.h>
 #endif
 
@@ -96,7 +96,7 @@ double
 physmem_total (void)
 {
 #if defined _SC_PHYS_PAGES && defined _SC_PAGESIZE
-  { /* This works on linux-gnu, solaris2 and cygwin.  */
+  { /* This works on linux-gnu, kfreebsd-gnu, solaris2, and cygwin.  */
     double pages = sysconf (_SC_PHYS_PAGES);
     double pagesize = sysconf (_SC_PAGESIZE);
     if (0 <= pages && 0 <= pagesize)
@@ -153,8 +153,8 @@ physmem_total (void)
   }
 #endif
 
-#if HAVE_SYSCTL && ! defined __GLIBC__ && defined HW_PHYSMEM
-  { /* This works on *bsd and darwin.  */
+#if HAVE_SYSCTL && !(defined __GLIBC__ && defined __linux__) && defined 
HW_PHYSMEM
+  { /* This works on *bsd, kfreebsd-gnu, and darwin.  */
     unsigned int physmem;
     size_t len = sizeof physmem;
     static int mib[2] = { CTL_HW, HW_PHYSMEM };
@@ -208,7 +208,7 @@ double
 physmem_available (void)
 {
 #if defined _SC_AVPHYS_PAGES && defined _SC_PAGESIZE
-  { /* This works on linux-gnu, solaris2 and cygwin.  */
+  { /* This works on linux-gnu, kfreebsd-gnu, solaris2, and cygwin.  */
     double pages = sysconf (_SC_AVPHYS_PAGES);
     double pagesize = sysconf (_SC_PAGESIZE);
     if (0 <= pages && 0 <= pagesize)
@@ -267,8 +267,8 @@ physmem_available (void)
   }
 #endif
 
-#if HAVE_SYSCTL && ! defined __GLIBC__ && defined HW_USERMEM
-  { /* This works on *bsd and darwin.  */
+#if HAVE_SYSCTL && !(defined __GLIBC__ && defined __linux__) && defined 
HW_USERMEM
+  { /* This works on *bsd, kfreebsd-gnu, and darwin.  */
     unsigned int usermem;
     size_t len = sizeof usermem;
     static int mib[2] = { CTL_HW, HW_USERMEM };






reply via email to

[Prev in Thread] Current Thread [Next in Thread]