From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 5A07015817D for ; Fri, 21 Jun 2024 13:14:20 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 3612FE2A4F; Fri, 21 Jun 2024 13:14:19 +0000 (UTC) Received: from smtp.gentoo.org (woodpecker.gentoo.org [140.211.166.183]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id A9AA7E2A52 for ; Fri, 21 Jun 2024 13:14:18 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id B55ED33BF28 for ; Fri, 21 Jun 2024 13:14:17 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id E1BE41D52 for ; Fri, 21 Jun 2024 13:14:14 +0000 (UTC) From: "Sam James" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" Message-ID: <1718176002.fa942450e3b289057881a60fd98a9d4b35d99604.sam@gentoo> Subject: [gentoo-commits] proj/gentoo-functions:master commit in: / X-VCS-Repository: proj/gentoo-functions X-VCS-Files: functions.sh test-functions X-VCS-Directories: / X-VCS-Committer: sam X-VCS-Committer-Name: Sam James X-VCS-Revision: fa942450e3b289057881a60fd98a9d4b35d99604 X-VCS-Branch: master Date: Fri, 21 Jun 2024 13:14:14 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply X-Archives-Salt: 62e53e18-5618-4391-be69-63a93c5cd1a4 X-Archives-Hash: 745f8bbc8af184be9672a7be2bdac70d commit: fa942450e3b289057881a60fd98a9d4b35d99604 Author: Kerin Millar plushkava net> AuthorDate: Fri Jun 7 13:00:01 2024 +0000 Commit: Sam James gentoo org> CommitDate: Wed Jun 12 07:06:42 2024 +0000 URL: https://gitweb.gentoo.org/proj/gentoo-functions.git/commit/?id=fa942450 Add the get_nprocs() function It stands a good chance of printing a useful value, even in the case that nproc(1) from coreutils is unavailable. Signed-off-by: Kerin Millar plushkava.net> functions.sh | 20 ++++++++++++++++++++ test-functions | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/functions.sh b/functions.sh index 504ee9e..60a4b2e 100644 --- a/functions.sh +++ b/functions.sh @@ -460,6 +460,26 @@ newest() _select_by_mtime -r "$@" } +# +# Tries to determine the number of available processors. Falls back to trying to +# determine the number of online processors in a way that is somewhat portable. +# +get_nprocs() +{ + if nproc 2>/dev/null; then + # The nproc(1) utility is provided by GNU coreutils. It has the + # advantage of acknowledging the effect of sched_setaffinity(2). + true + elif getconf _NPROCESSORS_ONLN 2>/dev/null; then + # This is a non-standard extension. Nevertheless, it works for + # glibc, musl-utils, macOS, FreeBSD, NetBSD and OpenBSD. + true + else + warn "get_nprocs: failed to determine the number of processors" + false + fi +} + # # Considers one or more pathnames and prints the one having the oldest # modification time. If at least one parameter is provided, all parameters shall diff --git a/test-functions b/test-functions index 813d524..c734141 100755 --- a/test-functions +++ b/test-functions @@ -544,6 +544,17 @@ test_whenceforth() { iterate_tests 4 "$@" } +test_get_nprocs() { + set -- eq 0 + + callback() { + shift + test_description="get_nprocs" + nproc=$(get_nprocs) && is_int "${nproc}" && test "${nproc}" -gt 0 + } + + iterate_tests 2 "$@" +} iterate_tests() { slice_width=$1 @@ -610,6 +621,7 @@ test_newest || rc=1 test_trim || rc=1 test_hr || rc=1 test_whenceforth || rc=1 +test_get_nprocs || rc=1 cleanup_tmpdir