From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1791) id 963D33858431; Fri, 8 Oct 2021 12:53:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 963D33858431 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Adhemerval Zanella To: glibc-cvs@sourceware.org Subject: [glibc] Linux: implement getloadavg(3) using sysinfo(2) X-Act-Checkin: glibc X-Git-Author: =?utf-8?q?Cristian_Rodr=C3=ADguez?= X-Git-Refname: refs/heads/master X-Git-Oldrev: f3c6c190388bb445568cfbf190a0942fc3c28553 X-Git-Newrev: b5c8a3aa82f66f49b731ca5204104cee48bccfa5 Message-Id: <20211008125325.963D33858431@sourceware.org> Date: Fri, 8 Oct 2021 12:53:25 +0000 (GMT) X-BeenThere: glibc-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Glibc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Oct 2021 12:53:25 -0000 https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=b5c8a3aa82f66f49b731ca5204104cee48bccfa5 commit b5c8a3aa82f66f49b731ca5204104cee48bccfa5 Author: Cristian Rodríguez Date: Fri Aug 6 15:17:48 2021 -0400 Linux: implement getloadavg(3) using sysinfo(2) Signed-off-by: Cristian Rodríguez Reviewed-by: Adhemerval Zanella Diff: --- sysdeps/unix/sysv/linux/getloadavg.c | 50 ++++++++++-------------------------- 1 file changed, 14 insertions(+), 36 deletions(-) diff --git a/sysdeps/unix/sysv/linux/getloadavg.c b/sysdeps/unix/sysv/linux/getloadavg.c index e50cc396e7..6f3e5b93a8 100644 --- a/sysdeps/unix/sysv/linux/getloadavg.c +++ b/sysdeps/unix/sysv/linux/getloadavg.c @@ -1,4 +1,4 @@ -/* Get system load averages. Linux (/proc/loadavg) version. +/* Get system load averages. Linux version. Copyright (C) 1999-2021 Free Software Foundation, Inc. This file is part of the GNU C Library. @@ -16,53 +16,31 @@ License along with the GNU C Library; if not, see . */ -#include -#include -#include -#include -#include -#include +#include +#include +#include /* Put the 1 minute, 5 minute and 15 minute load averages into the first NELEM elements of LOADAVG. Return the number written (never more than 3, but may be less than NELEM), or -1 if an error occurred. */ +#define CLAMP(v, lo, hi) MIN (MAX (v, lo), hi) + +#define SYSINFO_LOADS_SCALE (1 << SI_LOAD_SHIFT) + int getloadavg (double loadavg[], int nelem) { - int fd; + struct sysinfo info; - fd = __open_nocancel ("/proc/loadavg", O_RDONLY); - if (fd < 0) + if (__sysinfo (&info) != 0) return -1; - else - { - char buf[65], *p; - ssize_t nread; - int i; - nread = __read_nocancel (fd, buf, sizeof buf - 1); - __close_nocancel_nostatus (fd); - if (nread <= 0) - return -1; - buf[nread - 1] = '\0'; + nelem = CLAMP (nelem, 0, array_length (info.loads)); - if (nelem > 3) - nelem = 3; - p = buf; - for (i = 0; i < nelem; ++i) - { - char *endp; - loadavg[i] = __strtod_l (p, &endp, _nl_C_locobj_ptr); - if (endp == p) - /* This should not happen. The format of /proc/loadavg - must have changed. Don't return with what we have, - signal an error. */ - return -1; - p = endp; - } + for (int i = 0; i < nelem; i++) + loadavg[i] = (double) info.loads[i] / SYSINFO_LOADS_SCALE; - return i; - } + return nelem; }