On 04/18/2014 10:16 AM, Natanael Copa wrote:
> * lib/physmem.c (physmem_total): Some systems like musl libc does not
> (yet) support _SC_PHYS_PAGES. Use the linux syscall sysinfo as fallback
> if _SC_PHYS_PAGES or _SC_PAGESIZE fails.
> 
> Signed-off-by: Natanael Copa <nc...@alpinelinux.org>
> ---
> Changes since v1:
>  - prefer use _SC_PHYS_PAGES * _SC_PAGESIZE if available and use
>    sysinfo as fallback.
>  - check for and use mem_unit, member of struct sysinfo.
> 
>  lib/physmem.c | 13 ++++++++++++-
>  m4/physmem.m4 |  3 ++-
>  2 files changed, 14 insertions(+), 2 deletions(-)
> 
> diff --git a/lib/physmem.c b/lib/physmem.c
> index 7a67fb3..53afdd9 100644
> --- a/lib/physmem.c
> +++ b/lib/physmem.c
> @@ -32,8 +32,11 @@
>  # include <sys/sysmp.h>
>  #endif
>  
> -#if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
> +#if HAVE_SYS_SYSINFO_H
>  # include <sys/sysinfo.h>
> +#endif
> +
> +#if HAVE_MACHINE_HAL_SYSINFO_H
>  # include <machine/hal_sysinfo.h>
>  #endif
>  
> @@ -90,6 +93,14 @@ physmem_total (void)
>    }
>  #endif
>  
> +#if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
> +  { /* This works on linux */
> +    struct sysinfo si;
> +    if (sysinfo(&si) == 0)
> +      return (double) si.totalram * (double) si.mem_unit;
> +  }
> +#endif
> +
>  #if HAVE_PSTAT_GETSTATIC
>    { /* This works on hpux11.  */
>      struct pst_static pss;
> diff --git a/m4/physmem.m4 b/m4/physmem.m4
> index ff3d268..a1179eb 100644
> --- a/m4/physmem.m4
> +++ b/m4/physmem.m4
> @@ -40,6 +40,7 @@ AC_DEFUN([gl_PHYSMEM],
>       #endif
>      ])
>  
> -  AC_CHECK_FUNCS([pstat_getstatic pstat_getdynamic sysmp getsysinfo sysctl 
> table])
> +  AC_CHECK_FUNCS([pstat_getstatic pstat_getdynamic sysmp getsysinfo sysctl 
> table sysinfo])
> +  AC_CHECK_MEMBERS([struct sysinfo.mem_unit],,, [[#include <sys/sysinfo.h>]])
>    AC_REQUIRE([gl_SYS__SYSTEM_CONFIGURATION])
>  ])
> 

looks good,
though you might as well also adjust physmem_available() similarly.
I've done that in the attached and will push later.

thanks,
Pádraig.
>From 8def08e0879d55250931ca9ba38bed1735355608 Mon Sep 17 00:00:00 2001
From: Natanael Copa <nc...@alpinelinux.org>
Date: Fri, 18 Apr 2014 09:16:27 +0000
Subject: [PATCH] physmem: use sysinfo if _SC_PHYS_PAGES unavailable

* lib/physmem.c (physmem_total): Some systems like musl libc do not
(yet) support _SC_PHYS_PAGES.  Use the linux syscall sysinfo as fallback
if _SC_PHYS_PAGES or _SC_PAGESIZE fails.
(physmem_available): Likewise for _SC_AVPHYS_PAGES.

Signed-off-by: Natanael Copa <nc...@alpinelinux.org>
---
 ChangeLog     |    8 ++++++++
 lib/physmem.c |   21 ++++++++++++++++++++-
 m4/physmem.m4 |    3 ++-
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index fb25112..36e0672 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2014-04-18  Natanael Copa  <nc...@alpinelinux.org>
+
+	physmem: use sysinfo on linux-gnu if _SC_PHYS_PAGES unavailable
+	* lib/physmem.c (physmem_total): Some systems like musl libc don't yet
+	support _SC_PHYS_PAGES.  Use the linux syscall sysinfo as fallback
+	if _SC_PHYS_PAGES or _SC_PAGESIZE fails.
+	(physmem_available): Likewise for _SC_AVPHYS_PAGES.
+
 2014-04-17  Paul Eggert  <egg...@cs.ucla.edu>
 
 	regex: do not depend on malloc-gnu
diff --git a/lib/physmem.c b/lib/physmem.c
index 7a67fb3..d0989aa 100644
--- a/lib/physmem.c
+++ b/lib/physmem.c
@@ -32,8 +32,11 @@
 # include <sys/sysmp.h>
 #endif
 
-#if HAVE_SYS_SYSINFO_H && HAVE_MACHINE_HAL_SYSINFO_H
+#if HAVE_SYS_SYSINFO_H
 # include <sys/sysinfo.h>
+#endif
+
+#if HAVE_MACHINE_HAL_SYSINFO_H
 # include <machine/hal_sysinfo.h>
 #endif
 
@@ -90,6 +93,14 @@ physmem_total (void)
   }
 #endif
 
+#if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
+  { /* This works on linux.  */
+    struct sysinfo si;
+    if (sysinfo(&si) == 0)
+      return (double) si.totalram * si.mem_unit;
+  }
+#endif
+
 #if HAVE_PSTAT_GETSTATIC
   { /* This works on hpux11.  */
     struct pst_static pss;
@@ -194,6 +205,14 @@ physmem_available (void)
   }
 #endif
 
+#if HAVE_SYSINFO && HAVE_STRUCT_SYSINFO_MEM_UNIT
+  { /* This works on linux.  */
+    struct sysinfo si;
+    if (sysinfo(&si) == 0)
+      return ((double) si.freeram + si.bufferram) * si.mem_unit;
+  }
+#endif
+
 #if HAVE_PSTAT_GETSTATIC && HAVE_PSTAT_GETDYNAMIC
   { /* This works on hpux11.  */
     struct pst_static pss;
diff --git a/m4/physmem.m4 b/m4/physmem.m4
index ff3d268..a1179eb 100644
--- a/m4/physmem.m4
+++ b/m4/physmem.m4
@@ -40,6 +40,7 @@ AC_DEFUN([gl_PHYSMEM],
      #endif
     ])
 
-  AC_CHECK_FUNCS([pstat_getstatic pstat_getdynamic sysmp getsysinfo sysctl table])
+  AC_CHECK_FUNCS([pstat_getstatic pstat_getdynamic sysmp getsysinfo sysctl table sysinfo])
+  AC_CHECK_MEMBERS([struct sysinfo.mem_unit],,, [[#include <sys/sysinfo.h>]])
   AC_REQUIRE([gl_SYS__SYSTEM_CONFIGURATION])
 ])
-- 
1.7.7.6

Reply via email to