/* 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.
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:
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))
}
# 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);
return elem;
-# endif /* __linux__ || __ANDROID__ || __CYGWIN__ */
+# endif /* __CYGWIN__ */
# if !defined (LDAV_DONE) && defined (__NetBSD__) /* NetBSD < 0.9 */
# define LDAV_DONE