From patchwork Fri Aug 6 19:17:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Cristian_Rodr=C3=ADguez?= X-Patchwork-Id: 1514457 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GhFcr3Gf0z9sPf for ; Sat, 7 Aug 2021 05:18:20 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8BAD938618FC for ; Fri, 6 Aug 2021 19:18:17 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by sourceware.org (Postfix) with ESMTPS id BB9BF38618FC for ; Fri, 6 Aug 2021 19:18:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BB9BF38618FC Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=opensuse.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-f174.google.com with SMTP id u16so8392110ple.2 for ; Fri, 06 Aug 2021 12:18:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fGoY9q+v2VGUOFDzQ09cMHiBC6SisUMp0qSMZVeR24I=; b=GBo3smrnbubdX1oPhB9se47b6pLu5oOmjMtf+wYAKn8NFKrGl8NTDsoxmmPwkZJhCo rI/dF2s0AwzFqNOp5zyTArZKq/l7FxxDaLztdxq8LR4ul9MKk1U4juP6v9hk68aWr88I dwLnzAAduaF1v8n8Y57S8Cdk+CWuMhJHW9ACkcxQ070gB1Vo380h3/t/Y4Lka9raUr8e leTLw+GnUxYdZu/QD3ZmMi56sIj6Lbj6p+3l/Pwyzx+dJt2TCuDF44RO8uq5Sy4bga7Z dlMs4myYZeiL1N1EGLv33WDT9+J7fTPGhibBfcON2QmanFIyFN51j69c4JTT+g5sUaM6 R+DQ== X-Gm-Message-State: AOAM533SCmCInwDPqNhyI/xkmrcB6LmNmE1JRm2U5CjYOI3bUQsz4ZRx 0xzhb+sMfWeS3vJd1CFV3GVu/8bl3CETug== X-Google-Smtp-Source: ABdhPJw5WzSBLwWKZz7ugLsyk09mIA1H7HTLN6MM37oPEr/YCj5vfO492adNCQxcNm/Y89RbiS7+1Q== X-Received: by 2002:a17:90a:d686:: with SMTP id x6mr13509759pju.8.1628277487445; Fri, 06 Aug 2021 12:18:07 -0700 (PDT) Received: from tumbleweedvm.. (181-162-17-236.baf.movistar.cl. [181.162.17.236]) by smtp.gmail.com with ESMTPSA id u17sm11304078pfh.184.2021.08.06.12.18.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 06 Aug 2021 12:18:07 -0700 (PDT) From: =?utf-8?q?Cristian_Rodr=C3=ADguez?= To: libc-alpha@sourceware.org Subject: [PATCH 1/2] Linux: implement getloadavg(3) using sysinfo(2) Date: Fri, 6 Aug 2021 15:17:48 -0400 Message-Id: <20210806191749.4620-1-crrodriguez@opensuse.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 X-Spam-Status: No, score=-10.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Signed-off-by: Cristian Rodríguez Reviewed-by: Adhemerval Zanella --- 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..3ea7fd02b7 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, (int)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; }