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: 721765 Delivered-To: patch@linaro.org Received: by 2002:adf:f64d:0:b0:31d:da82:a3b4 with SMTP id x13csp1635555wrp; Tue, 12 Sep 2023 06:15:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHGbQCS99hmMdqZHS9fVeOYJN7y62mzOfzvxwdpJqAhtgtmxH+CD6M0jc2LsP8etqz+B14Y X-Received: by 2002:a2e:9609:0:b0:2bc:fd50:573a with SMTP id v9-20020a2e9609000000b002bcfd50573amr11070717ljh.6.1694524522799; Tue, 12 Sep 2023 06:15:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694524522; cv=none; d=google.com; s=arc-20160816; b=epr1sixsGJQF4AatbslAfDCW7FWXGv1bW3i+kePPVI1xO+N43tSpUofmp3mIvPsf+0 361ry0PORVQxXJfX8aJjKKNKSMemFrlhc43TG0bUwW5yNKUeARh4IbySt+x3CdgNqpqs f7LLDq5AuENUuXPHw0l8fQdVL5+lajmX1ux2J4wp55m0Wl1/CstLTlVBSK7dEqalKLkP ZCd69mH6UiT8yKvZGthIB9bf9YA3PCVu68T3izh3W9NMlhqk6OoGonbhO2epqF/Pazh0 wg1c/KdbPzzeFHwwdG7JWGIE9B0kZ5jgqtkjC/OWWFPMpRPYSejLIis1k54HbXWKEXW6 /fFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dmarc-filter:delivered-to; bh=JRNuMxTqUR11wciqdAOxI18q500y2nT8X9tj4jcDTuw=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=jPfmH1xAI7Q1UsbnqLD5naU4WX0/11/VTJrbFeMrnBb6krckIplN7YCj5EyKkm/wE5 wAT70+WSZwjBiOjp0kfc9OtvKmsTsymISxxeGLBUGNJTF5PwPEqOaSuCTugg2dTVuCpC ZCZ+REp+B/yGQoGWDlyB5oCjWPyYGpkiTkggzAyaidPRsJlpqGJLUVXLZhsBj3NC5vKy Yx3uToJiuORuc4pr7t+lW/LSsUtM/8xwv7iFoURfIPZ9JxCzndJhB+VTnANEOq7Hy2pS zUn/hGoVrICuRBVGt1uK7BdujP2NQd9jbsdF7jF+RFOWS5P1OXAGR7s+Y/2+y/Yrh/WQ /gAg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fhuZUhvP; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id q24-20020a170906361800b009a1960b4ad9si8571869ejb.515.2023.09.12.06.15.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:15:22 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=fhuZUhvP; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CCAD3385701E for ; Tue, 12 Sep 2023 13:15:20 +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-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+patch=linaro.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 From patchwork Tue Sep 12 13:15:06 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: 721766 Delivered-To: patch@linaro.org Received: by 2002:adf:f64d:0:b0:31d:da82:a3b4 with SMTP id x13csp1635577wrp; Tue, 12 Sep 2023 06:15:24 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFud8+AeEKMXRCQ+5RfLWSrbB+lg/qCrnxUuc8Omy2vJ1szHcF2gukiSKWevFbg8Ayotv0z X-Received: by 2002:a2e:9b97:0:b0:2bc:ddf2:4fcb with SMTP id z23-20020a2e9b97000000b002bcddf24fcbmr10115794lji.1.1694524524717; Tue, 12 Sep 2023 06:15:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694524524; cv=none; d=google.com; s=arc-20160816; b=BhxW+qwD5SmUrHN0ikBISpoi54s9e4i5bkN/7TKvTx0NiTvVITQsydKVgKyCas2ala e7LEjr9jbUa6DydZq2hVtgufRqWipZ/joh7if+8F3/grkbLHpJKAQu9bo41Z0U6HbfDv uWcv1N2SpYACc/ZDra4ILMIY/+VxzuyY5xp5bDMISpeRrdXRqKuKrcGfcmPdkTg7aVkY xukl4TS7lTyLpTI886KNPs/kI8Vpci7JOVVnbjph8DXnk6SO68tqmX+mVkVwcMYlnv3k ImbIRGSuQV8tmQpA4ngEe9fDFnvty/KZpRsCKzfpsvs/0E7LCKlc9I/wKE6HghXwQgXf 4mhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature:dmarc-filter:delivered-to; bh=byUh0fkTXwGO90iS7vgYnvL9snEsS5I2Z7uquGyrEWo=; fh=dHLBnA+MhGtNtN2B2JMAELi4oD+gmgMg7DL8H0jYbkI=; b=x+VmcXanYtHYRz0fYzvBK0z3EMz9DDjZYBT+vv6ruro7Zzb3ulbm4Eq+WzUtIYGC9O Evjx3Ihi7ffibPC3e/Lfnv7cBBd6Zcw+dfc9vHF0lhtm2dF1lrvMgqtb01q/n4U6jGr1 SzPbS0+8ZYhEvTXhryzffjOgh67XNJHWql+SZH7urY0OzfbYKww2uQrfRjbhBSPI9Yt+ N+nQO4TjiFYxI3gxRMEMZ6EsQWp9j7mrypk7zQ43rxaLc1bcyIcT4MDuatHbitybDBu5 UnjrCvaCZFHvXCSDfRK+AnvB+SSPzJiN5j0N4rbE6RQGLHI+PSvjRMH0fR0IAvN5GtOZ GLtw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WPfCWqjM; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id pk24-20020a170906d7b800b0099bcb54fae7si8886247ejb.512.2023.09.12.06.15.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:15:24 -0700 (PDT) Received-SPF: pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=WPfCWqjM; spf=pass (google.com: domain of libc-alpha-bounces+patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="libc-alpha-bounces+patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9D1D238560A4 for ; Tue, 12 Sep 2023 13:15:23 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-ot1-x331.google.com (mail-ot1-x331.google.com [IPv6:2607:f8b0:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 05B52385842A for ; Tue, 12 Sep 2023 13:15:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 05B52385842A 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-ot1-x331.google.com with SMTP id 46e09a7af769-6c09f1f9df2so4197518a34.2 for ; Tue, 12 Sep 2023 06:15:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1694524513; x=1695129313; 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=byUh0fkTXwGO90iS7vgYnvL9snEsS5I2Z7uquGyrEWo=; b=WPfCWqjMrTedO9gAdJ5UW3Leg3Iqr5yuRWci2u1nFk6CPGk1F8QpiBvcTqvxVKFbXW Iot+sdMFlL/CiGHxeceV4VNyxsiwSOHFNxzlrBZYj5T5zIe9hesXGvrmvVdkIwQYUnxt 4vglXQaj3enKiQj1y4Gk2WTPZ7ZfTVMmNGguLpdaGzZchI5jxgo56Bge/+Pfx5I/wn7r LyIdACLUIX1x1A30CeGbmn+TzC1kf7S7oEkGkWqsZsWzCFF6e23e2X4cN3wBXaMg8zhw nM0eEf0Enfg8tDSjvwcCmCX2UrgHUs7+7cMChWt7+uB8aCbIC5W8XHw6OPOilo5Q2K0Y wU5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694524513; x=1695129313; 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=byUh0fkTXwGO90iS7vgYnvL9snEsS5I2Z7uquGyrEWo=; b=MW+XGSTayGqLVjBzojla5oEqMYE3lwU3+dzqndAUcRYKkV5D9PkuMkeAznALmYCJtQ D+5B4fPff72YE/qK3Soi8B58mwwnL0WxZb0LRD21oANpVrdaYrXKSDVSptAcYLSE4jOm R/IzzCnb/45CghPtCbAAbcQYsqmyeP6Eb+M0s0bQbK3+SQJk0M+ZUXbUJnSdBInxI331 69MWT9yDJnR+az+9nEoHjsL0psWu46uhdt1MGkHAcVQ/M78Ybf/j0DrFdbrFLaV9VfwC AVmC0pWH1eLqG55iSg9DA1/sIhF7jQfICRcaQZ0ppMnjsmcRYQ5UJYpM5f/+FoA6OdYz H5qA== X-Gm-Message-State: AOJu0YziamEk5TCci2OPqz2avqGVRCnqLsDNbQtJHm+xRDQTYU4cQmM+ bwJ4SnHiOO1AlsweEpoMfpOrRnWp6GLhYVfIjSWJvA== X-Received: by 2002:a05:6830:1493:b0:6b5:8a98:f593 with SMTP id s19-20020a056830149300b006b58a98f593mr13139541otq.8.1694524513460; Tue, 12 Sep 2023 06:15:13 -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.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 06:15:12 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Subject: [PATCH 2/2] linux: Use procutils_read_file on malloc-hugepages Date: Tue, 12 Sep 2023 10:15:06 -0300 Message-Id: <20230912131506.2571612-3-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.3 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+patch=linaro.org@sourceware.org Checked on x86_64-linux-gnu. --- sysdeps/aarch64/Makefile | 5 +- sysdeps/unix/sysv/linux/Makefile | 6 ++ sysdeps/unix/sysv/linux/malloc-hugepages.c | 68 ++++++++++------------ 3 files changed, 40 insertions(+), 39 deletions(-) diff --git a/sysdeps/aarch64/Makefile b/sysdeps/aarch64/Makefile index 6a9559e5f5..6c80ee8479 100644 --- a/sysdeps/aarch64/Makefile +++ b/sysdeps/aarch64/Makefile @@ -67,5 +67,8 @@ sysdep_routines += __mtag_tag_zero_region \ endif ifeq ($(subdir),malloc) -sysdep_malloc_debug_routines = __mtag_tag_zero_region __mtag_tag_region +sysdep_malloc_debug_routines += \ + __mtag_tag_zero_region \ + __mtag_tag_region \ + # sysdep_malloc_debug_routines endif diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 063719bae6..d4ab43c8f2 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -637,6 +637,12 @@ CFLAGS-mq_send.c += -fexceptions CFLAGS-mq_receive.c += -fexceptions endif +ifeq ($(subdir),malloc) +sysdep_malloc_debug_routines += \ + procutils \ + # sysdep_malloc_debug_routines +endif + ifeq ($(subdir),nscd) sysdep-CFLAGS += -DHAVE_EPOLL -DHAVE_INOTIFY -DHAVE_NETLINK CFLAGS-gai.c += -DNEED_NETLINK diff --git a/sysdeps/unix/sysv/linux/malloc-hugepages.c b/sysdeps/unix/sysv/linux/malloc-hugepages.c index 2f316474c1..cec034e07f 100644 --- a/sysdeps/unix/sysv/linux/malloc-hugepages.c +++ b/sysdeps/unix/sysv/linux/malloc-hugepages.c @@ -16,10 +16,12 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, see . */ -#include #include +#include #include +#include #include +#include #include unsigned long int @@ -78,51 +80,41 @@ __malloc_thp_mode (void) return malloc_thp_mode_not_supported; } -static size_t -malloc_default_hugepage_size (void) +static int +proc_meminfo_parse (const char *s, void *arg) { - int fd = __open64_nocancel ("/proc/meminfo", O_RDONLY); - if (fd == -1) + enum { HUGEPAGESIZELEN = sizeof ("Hugepagesize:") - 1 }; + if (strncmp (s, "Hugepagesize:", HUGEPAGESIZELEN) != 0) return 0; - size_t hpsize = 0; + /* The default huge page size is in the form: + Hugepagesize: NUMBER kB */ + s += HUGEPAGESIZELEN; - char buf[512]; - off64_t off = 0; - while (1) + size_t hpsize = 0; + for (int i = 0; (s[i] >= '0' && s[i] <= '9') || s[i] == ' '; i++) { - ssize_t r = __pread64_nocancel (fd, buf, sizeof (buf) - 1, off); - if (r < 0) - break; - buf[r] = '\0'; - - /* If the tag is not found, read the last line again. */ - const char *s = strstr (buf, "Hugepagesize:"); - if (s == NULL) - { - char *nl = strrchr (buf, '\n'); - if (nl == NULL) - break; - off += (nl + 1) - buf; - continue; - } - - /* The default huge page size is in the form: - Hugepagesize: NUMBER kB */ - s += sizeof ("Hugepagesize: ") - 1; - for (int i = 0; (s[i] >= '0' && s[i] <= '9') || s[i] == ' '; i++) - { - if (s[i] == ' ') - continue; - hpsize *= 10; - hpsize += s[i] - '0'; - } - hpsize *= 1024; - break; + if (s[i] == ' ') + continue; + + if (INT_MULTIPLY_WRAPV (hpsize, 10, &hpsize) + || INT_ADD_WRAPV (hpsize, s[i] - '0', &hpsize)) + return -1; } + if (INT_MULTIPLY_WRAPV (hpsize, 1024, &hpsize)) + return -1; - __close_nocancel (fd); + *(size_t*) arg = hpsize; + return 1; +} +static size_t +malloc_default_hugepage_size (void) +{ + size_t hpsize = 0; + /* If procfs is not accessible of if huge page field is not present, + the 'hpsize' argument is not changed. */ + __procutils_read_file ("/proc/meminfo", proc_meminfo_parse, &hpsize); return hpsize; }