]> Savannah Git Hosting - gnulib.git/commitdiff
getloadavg: Improve Linux and Android support.
authorBruno Haible <bruno@clisp.org>
Fri, 21 Mar 2025 15:25:59 +0000 (16:25 +0100)
committerBruno Haible <bruno@clisp.org>
Tue, 1 Apr 2025 15:01:46 +0000 (17:01 +0200)
* lib/getloadavg.c [__linux__, __ANDROID__]: Include <sys/param.h> and
<sys/sysinfo.h>.
(getloadavg) [__linux__, __ANDROID__]: Use sysinfo() instead of reading
/proc/loadavg.
(LINUX_LDAV_FILE): Remove macro.

ChangeLog
lib/getloadavg.c

index 51e381ad0cc1bb46ee6406f546f2c29562545bee..4c4851518272ac49a4080919cc20084cfd4612d0 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2025-03-21  Bruno Haible  <bruno@clisp.org>
+
+       getloadavg: Improve Linux and Android support.
+       * lib/getloadavg.c [__linux__, __ANDROID__]: Include <sys/param.h> and
+       <sys/sysinfo.h>.
+       (getloadavg) [__linux__, __ANDROID__]: Use sysinfo() instead of reading
+       /proc/loadavg.
+       (LINUX_LDAV_FILE): Remove macro.
+
 2025-03-21  Bruno Haible  <bruno@clisp.org>
 
        setlocale-null: Fix autoconf warning.
index c940e4c7ee01274b07b0f111260f6fbb41e0ca5d..628fecf21cc53283588c28479aa3ad1172c39dfb 100644 (file)
@@ -1,6 +1,6 @@
 /* Get the system load averages.
 
-   Copyright (C) 1985-1989, 1991-1995, 1997, 1999-2000, 2003-2024 Free Software
+   Copyright (C) 1985-1989, 1991-1995, 1997, 1999-2000, 2003-2025 Free Software
    Foundation, Inc.
 
    NOTE: The canonical source of this file is maintained with gnulib.
@@ -47,8 +47,6 @@
    N_NAME_POINTER               The nlist n_name element is a pointer,
                                 not an array.
    HAVE_STRUCT_NLIST_N_UN_N_NAME 'n_un.n_name' is member of 'struct nlist'.
-   LINUX_LDAV_FILE              [__linux__, __ANDROID__, __CYGWIN__]: File
-                                containing load averages.
 
    Specific system predefines this file uses, aside from setting
    default values if not emacs:
@@ -65,8 +63,7 @@
    UMAX4_3
    VMS
    _WIN32                       Native Windows (possibly also defined on Cygwin)
-   __linux__, __ANDROID__       Linux: assumes /proc file system mounted.
-                                Support from Michael K. Johnson.
+   __linux__, __ANDROID__       Linux: assumes sysinfo() call.
    __CYGWIN__                   Cygwin emulates linux /proc/loadavg.
    __NetBSD__                   NetBSD: assumes /kern file system mounted.
 
 #  include <sys/dg_sys_info.h>
 # endif
 
+# if defined __linux__ || defined __ANDROID__
+#  include <sys/param.h>
+#  include <sys/sysinfo.h>
+# endif
+
 # if (defined __linux__ || defined __ANDROID__ \
       || defined __CYGWIN__ || defined SUNOS_5 \
       || (defined LOAD_AVE_TYPE && ! defined __VMS))
@@ -498,20 +500,32 @@ getloadavg (double loadavg[], int nelem)
   }
 # endif
 
-# if !defined (LDAV_DONE) && (defined __linux__ || defined __ANDROID__ || defined __CYGWIN__)
+# if !defined (LDAV_DONE) && (defined __linux__ || defined __ANDROID__)
                                       /* Linux without glibc, Android, Cygwin */
 #  define LDAV_DONE
 #  undef LOAD_AVE_TYPE
 
-#  ifndef LINUX_LDAV_FILE
-#   define LINUX_LDAV_FILE "/proc/loadavg"
-#  endif
+  {
+    struct sysinfo info;
+    if (sysinfo (&info) < 0)
+      return -1;
+    loadavg[0] = info.loads[0] / (double)(1U << SI_LOAD_SHIFT);
+    loadavg[1] = info.loads[1] / (double)(1U << SI_LOAD_SHIFT);
+    loadavg[2] = info.loads[2] / (double)(1U << SI_LOAD_SHIFT);
+    elem = 3;
+  }
+# endif /* __linux__ || __ANDROID__ */
+
+# if !defined (LDAV_DONE) && defined __CYGWIN__
+                                      /* Cygwin */
+#  define LDAV_DONE
+#  undef LOAD_AVE_TYPE
 
   char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
   char const *ptr = ldavgbuf;
   int fd, count, saved_errno;
 
-  fd = open (LINUX_LDAV_FILE, O_RDONLY | O_CLOEXEC);
+  fd = open ("/proc/loadavg", O_RDONLY | O_CLOEXEC);
   if (fd == -1)
     return -1;
   count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
@@ -554,7 +568,7 @@ getloadavg (double loadavg[], int nelem)
 
   return elem;
 
-# endif /* __linux__ || __ANDROID__ || __CYGWIN__ */
+# endif /* __CYGWIN__ */
 
 # if !defined (LDAV_DONE) && defined (__NetBSD__)          /* NetBSD < 0.9 */
 #  define LDAV_DONE