From patchwork Tue Sep 12 13:15:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 75740 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 26E1F385DC13 for ; Tue, 12 Sep 2023 13:15:28 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc30.google.com (mail-oo1-xc30.google.com [IPv6:2607:f8b0:4864:20::c30]) by sourceware.org (Postfix) with ESMTPS id 266483858C1F for ; Tue, 12 Sep 2023 13:15:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 266483858C1F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org Received: by mail-oo1-xc30.google.com with SMTP id 006d021491bc7-57124ee486dso3528443eaf.3 for ; Tue, 12 Sep 2023 06:15:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694524512; x=1695129312; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=JRNuMxTqUR11wciqdAOxI18q500y2nT8X9tj4jcDTuw=; b=fhuZUhvPgTQOGCtVHIymzWlC6YWI4Ptemx1l8X8+6bEBpGh61S0xbUugXBUhovtjKd aS4pnGxlpK1hzB4Kncgo6AzIMyzbavKseyg+qpxOIhKhv3UcfXOhD3uNkcbygGXgyIDh p+G4FVvRMAGqYjK7VhIO//tD2SQLydIb+fAwthBjLQ0BZppjhHvbvuo4BnKVnfdSwYtR 1qpr6OPGSNgqrI0L2Nil0jdd0M4q7uFXlrGIL8UGXbuHJgVt1KADq6f4SaBfb9EB8SqS k4TgEbPqjhB6B3KVUPgOz2U0lPxsT7GdAxXT6CH+jlkQwGoe4GJ1hgR1Vf9R+i5HX6HP PR0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694524512; x=1695129312; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JRNuMxTqUR11wciqdAOxI18q500y2nT8X9tj4jcDTuw=; b=hOxD8/Witifo50CFKNqPtYDhOgurxYolFyYvkbhjVx0+H2wX1utL19RuB1llNLBDWv l2bwtDGcQOJoX3BqrdVJAOiNjVEUG16JtiaNnwt9Jro4YLeSNnLmunNzQRBLiwchvfWO AEs69hlcUN/S6gaMMFYSZpmQSnXw+lCebzF7RnDmdDP6Llf/VDys4K+lRvh4RTZe34hW QUP1T/3kPb03jAyRtsK/gSVsGGd4BKDhP+h4awP82IRT3RVVoYhd5gWB7bKvUFkEwM79 svI6aa9u+t5OlrrV4/qc8M2xwjE4B16Wvy8QnuSO2NUGETF2ebMkQX+hzjlG8H+HjKl0 E+KQ== X-Gm-Message-State: AOJu0YxDo4T8cclb8D2W4Xbka86e22nFqfLEuro/ocNXxGnoVRyIRqV1 aVzRIDgxEt4RR0VHG/R6ZM3tWFDs+pPlZkvqWMr9iQ== X-Google-Smtp-Source: AGHT+IFkrTxEe+jiP4T5lzsqHhgymEUb4QBR0RSxR+aFdFkr2BK3MH3hGUoswZ8zGRx5MjVvO2fdFQ== X-Received: by 2002:a4a:9c07:0:b0:56c:cd0c:1d67 with SMTP id y7-20020a4a9c07000000b0056ccd0c1d67mr11957304ooj.7.1694524511755; Tue, 12 Sep 2023 06:15:11 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c0:91cb:e4b5:40b4:e1ca:7dfe]) by smtp.gmail.com with ESMTPSA id x38-20020a4a97e9000000b00569c5eadf15sm4336813ooi.26.2023.09.12.06.15.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:15:11 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 1/2] linux: Use procutils_read_file on getsysstats Date: Tue, 12 Sep 2023 10:15:05 -0300 Message-Id: <20230912131506.2571612-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230912131506.2571612-1-adhemerval.zanella@linaro.org> References: <20230912131506.2571612-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Checked on x86_64-linux-gnu. --- sysdeps/unix/sysv/linux/getsysstats.c | 177 +++++++------------------ sysdeps/unix/sysv/linux/pidfd_getpid.c | 2 +- sysdeps/unix/sysv/linux/procutils.c | 4 +- sysdeps/unix/sysv/linux/procutils.h | 4 +- 4 files changed, 55 insertions(+), 132 deletions(-) diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c index b0b6c154ac..1adf74cfc6 100644 --- a/sysdeps/unix/sysv/linux/getsysstats.c +++ b/sysdeps/unix/sysv/linux/getsysstats.c @@ -16,18 +16,11 @@ License along with the GNU C Library; if not, see . */ -#include -#include #include -#include -#include -#include -#include -#include -#include -#include +#include #include #include +#include int __get_nprocs_sched (void) @@ -53,140 +46,66 @@ __get_nprocs_sched (void) return 0; } -static char * -next_line (int fd, char *const buffer, char **cp, char **re, - char *const buffer_end) +static int +parse_proc_stat (const char *l, void *arg) { - char *res = *cp; - char *nl = memchr (*cp, '\n', *re - *cp); - if (nl == NULL) - { - if (*cp != buffer) - { - if (*re == buffer_end) - { - memmove (buffer, *cp, *re - *cp); - *re = buffer + (*re - *cp); - *cp = buffer; - - ssize_t n = __read_nocancel (fd, *re, buffer_end - *re); - if (n < 0) - return NULL; - - *re += n; - - nl = memchr (*cp, '\n', *re - *cp); - while (nl == NULL && *re == buffer_end) - { - /* Truncate too long lines. */ - *re = buffer + 3 * (buffer_end - buffer) / 4; - n = __read_nocancel (fd, *re, buffer_end - *re); - if (n < 0) - return NULL; - - nl = memchr (*re, '\n', n); - **re = '\n'; - *re += n; - } - } - else - nl = memchr (*cp, '\n', *re - *cp); + /* The current format of /proc/stat has all the cpu* entries at the front. + We assume here that stays this way. */ + if (strncmp (l, "cpu", 3) != 0) + return -1; - res = *cp; - } + if (isdigit (l[3])) + *(int *) arg += 1; - if (nl == NULL) - nl = *re - 1; - } - - *cp = nl + 1; - assert (*cp <= *re); - - return res == *re ? NULL : res; + return 1; } static int get_nproc_stat (void) { - enum { buffer_size = 1024 }; - char buffer[buffer_size]; - char *buffer_end = buffer + buffer_size; - char *cp = buffer_end; - char *re = buffer_end; int result = 0; - - const int flags = O_RDONLY | O_CLOEXEC; - int fd = __open_nocancel ("/proc/stat", flags); - if (fd != -1) - { - char *l; - while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL) - /* The current format of /proc/stat has all the cpu* entries - at the front. We assume here that stays this way. */ - if (strncmp (l, "cpu", 3) != 0) - break; - else if (isdigit (l[3])) - ++result; - - __close_nocancel_nostatus (fd); - } - + /* If procfs can not be accessed, 'result' won't be updated. */ + __procutils_read_file ("/proc/stat", parse_proc_stat, &result); return result; } static int -read_sysfs_file (const char *fname) +parse_sysfs_file (const char *l, void *arg) { - enum { buffer_size = 1024 }; - char buffer[buffer_size]; - char *buffer_end = buffer + buffer_size; - char *cp = buffer_end; - char *re = buffer_end; + int *result = arg; - const int flags = O_RDONLY | O_CLOEXEC; - /* This file contains comma-separated ranges. */ - int fd = __open_nocancel (fname, flags); - char *l; - int result = 0; - if (fd != -1) + do { - l = next_line (fd, buffer, &cp, &re, buffer_end); - if (l != NULL) - do - { - char *endp; - unsigned long int n = strtoul (l, &endp, 10); - if (l == endp) - { - result = 0; - break; - } - - unsigned long int m = n; - if (*endp == '-') - { - l = endp + 1; - m = strtoul (l, &endp, 10); - if (l == endp) - { - result = 0; - break; - } - } - - if (m >= n) - result += m - n + 1; - - l = endp; - if (l < re && *l == ',') - ++l; - } - while (l < re && *l != '\n'); - - __close_nocancel_nostatus (fd); + char *endp; + unsigned long int n = strtoul (l, &endp, 10); + if (l == endp) + { + *result = 0; + return -1; + } + + unsigned long int m = n; + if (*endp == '-') + { + l = endp + 1; + m = strtoul (l, &endp, 10); + if (l == endp) + { + *result = 0; + return -1; + } + } + + if (m >= n) + *result += m - n + 1; + + l = endp; + if (*l != '\0' && *l == ',') + ++l; } + while (*l != '\0' && *l != '\n'); - return result; + return 1; } static int @@ -213,7 +132,9 @@ get_nprocs_fallback (void) int __get_nprocs (void) { - int result = read_sysfs_file ("/sys/devices/system/cpu/online"); + int result = 0; + __procutils_read_file ("/sys/devices/system/cpu/online", parse_sysfs_file, + &result); if (result != 0) return result; @@ -228,7 +149,9 @@ weak_alias (__get_nprocs, get_nprocs) int __get_nprocs_conf (void) { - int result = read_sysfs_file ("/sys/devices/system/cpu/possible"); + int result = 0; + __procutils_read_file ("/sys/devices/system/cpu/possible", parse_sysfs_file, + &result); if (result != 0) return result; diff --git a/sysdeps/unix/sysv/linux/pidfd_getpid.c b/sysdeps/unix/sysv/linux/pidfd_getpid.c index d03936e97f..f857b1236b 100644 --- a/sysdeps/unix/sysv/linux/pidfd_getpid.c +++ b/sysdeps/unix/sysv/linux/pidfd_getpid.c @@ -105,7 +105,7 @@ pidfd_getpid (int fd) *_fitoa_word (fd, p, 10, 0) = '\0'; struct parse_fdinfo_t fdinfo = { .found = false, .pid = -1 }; - if (!procutils_read_file (fdinfoname, parse_fdinfo, &fdinfo)) + if (!__procutils_read_file (fdinfoname, parse_fdinfo, &fdinfo)) /* The fdinfo contains an invalid 'Pid:' value. */ return INLINE_SYSCALL_ERROR_RETURN_VALUE (EBADF); diff --git a/sysdeps/unix/sysv/linux/procutils.c b/sysdeps/unix/sysv/linux/procutils.c index 30fc3a063a..1a2dbe941c 100644 --- a/sysdeps/unix/sysv/linux/procutils.c +++ b/sysdeps/unix/sysv/linux/procutils.c @@ -71,8 +71,8 @@ next_line (char **r, int fd, char *const buffer, char **cp, char **re, } bool -procutils_read_file (const char *filename, procutils_closure_t closure, - void *arg) +__procutils_read_file (const char *filename, procutils_closure_t closure, + void *arg) { enum { buffer_size = PROCUTILS_MAX_LINE_LEN }; char buffer[buffer_size]; diff --git a/sysdeps/unix/sysv/linux/procutils.h b/sysdeps/unix/sysv/linux/procutils.h index b3c2f6fb55..89ca4fcec4 100644 --- a/sysdeps/unix/sysv/linux/procutils.h +++ b/sysdeps/unix/sysv/linux/procutils.h @@ -37,7 +37,7 @@ typedef int (*procutils_closure_t) (const char *line, void *arg); It returns true in case the file is fully read or false if CLOSURE returns a value diferent than 0. */ -bool procutils_read_file (const char *filename, procutils_closure_t closure, - void *arg) attribute_hidden; +bool __procutils_read_file (const char *filename, procutils_closure_t closure, + void *arg) attribute_hidden; #endif