From nobody Thu Feb 23 17:52:49 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PN0wx5jwtz3sHpR; Thu, 23 Feb 2023 17:52:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PN0wx55v0z3ppZ; Thu, 23 Feb 2023 17:52:49 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1677174769; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=btnWCqVeKi2fS3ioZLSrh8ApJERk9D3Y1Tcu7OV3hBU=; b=CUAsj3SSti4F0oMcQ/J/rP35Y7hnVfFnQvgimssqcQpJx4heGrtI44Y95ix66FElfyF7sg p1OMBll8UIDoBkXn9IVIrr6UacpRSyH9yexi5uTTmMDANzjlECzNfmq9uQeuXT/Vae213Q LgISaYgOPKs0QpaFdi5KvLSrZC95JHLShnzIZJovsbBJZ0RyQwmZl+8+ko3+HDhGsq+Ez9 OeQX3laSsSlJAJmva5N63ySOf/CkOeMKU7RfSXlxKfkDkFsTJR9q2Z0ETD0bQB9Jr9z11K 584eG4SNbgoY0nZcYFxxg4N08mf/ao15dZhcm89U4IcJG0GpFiUO3yIInsK9Qw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1677174769; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=btnWCqVeKi2fS3ioZLSrh8ApJERk9D3Y1Tcu7OV3hBU=; b=khQ6sI/Gah0uZ6IFMhMisFSEnnPdFHMcwLSjvMcJ3mAOG+80sn+zIgmSnKrJMLT5LR197y eO8de1/t9pu+vwX1ZTf/tATaZtPypgYxxfZt/oCuYGOFHKtl2FSuaPF3h59VQuoxVOsuAi j/UJuVVrDVhS3E4qTq70ZbAzDX7duLhqDRhWqRixt9D+5glNB4SpCbst56NApamf9khgrp pFmi53WBSxhXggeAKWUv0ec7/BppELscxwHhD7xbBs7fGu02fGmW/c6qXXhpJr7HYer2ey ycLQMzYc4S7/Ax2V0oX/pbv+YcIU4VOFoY3j41eHJdK4zXv8+tzpCVzOL+GFfQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1677174769; a=rsa-sha256; cv=none; b=TcVA2fNcaZYayWIOdlAHdgvaJ3G9x/NkSt+zMOULpv7x97aODmMMw7B8hzajz9g/Z1q7wa dBesRKzzsP9AkJAcndB+Lr/yThIqPRhkF/lKoKqjxwIJLQ240Rlormrt0Fn/29qqb1lBMT Y3Jrz/lDBfRHtfy1iMgvaqDSEbGqk8wgdkjZDkmM9UdSZv1FPO+vKofbOcdfasnaproUPZ vyKdwnwZnZfb74IdeJy92H0EWEXcdnQcW8h6yyDKzF+S54aldLsSbHBukKHKSxf05w2ZXU n1Y/pbP9sw4askNk74v6mgxmiV5Z6smE2saIEWDdaXQSgvMFwl9eiwvYmT5OAQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PN0wx487zzrSp; Thu, 23 Feb 2023 17:52:49 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 31NHqnBA075811; Thu, 23 Feb 2023 17:52:49 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 31NHqnPd075810; Thu, 23 Feb 2023 17:52:49 GMT (envelope-from git) Date: Thu, 23 Feb 2023 17:52:49 GMT Message-Id: <202302231752.31NHqnPd075810@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Mateusz Guzik Subject: git: eb9e9f1c2533 - releng/13.2 - Add nproc(1) List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mjg X-Git-Repository: src X-Git-Refname: refs/heads/releng/13.2 X-Git-Reftype: branch X-Git-Commit: eb9e9f1c2533a8b01446e14a46f45eb7b0cd71e2 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch releng/13.2 has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=eb9e9f1c2533a8b01446e14a46f45eb7b0cd71e2 commit eb9e9f1c2533a8b01446e14a46f45eb7b0cd71e2 Author: Mateusz Guzik AuthorDate: 2023-02-04 23:33:48 +0000 Commit: Mateusz Guzik CommitDate: 2023-02-23 17:51:07 +0000 Add nproc(1) This program prints the number of CPU threads it can run on, while respecting cpusets (or not, depending on switches). It aims to be compatible with nproc as found in GNU coreutils. Approved by: re (cperciva) Reviewed by: des Reviewed by: pstef Differential Revision: https://reviews.freebsd.org/D38386 (cherry picked from commit 48bfd3597654490cdc43bf0f591a539d3a28b590) (cherry picked from commit 31f9db72a35bb20ad01d792eaaa1d6048252ab38) --- bin/Makefile | 1 + bin/nproc/Makefile | 4 ++ bin/nproc/nproc.1 | 54 ++++++++++++++++++++ bin/nproc/nproc.c | 132 ++++++++++++++++++++++++++++++++++++++++++++++++ usr.bin/cpuset/cpuset.1 | 1 + 5 files changed, 192 insertions(+) diff --git a/bin/Makefile b/bin/Makefile index cdd96d0b84e8..83bf79dcb9f9 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -24,6 +24,7 @@ SUBDIR= cat \ ls \ mkdir \ mv \ + nproc \ pax \ pkill \ ps \ diff --git a/bin/nproc/Makefile b/bin/nproc/Makefile new file mode 100644 index 000000000000..2a57083b9d40 --- /dev/null +++ b/bin/nproc/Makefile @@ -0,0 +1,4 @@ +PACKAGE=runtime +PROG= nproc + +.include diff --git a/bin/nproc/nproc.1 b/bin/nproc/nproc.1 new file mode 100644 index 000000000000..ae252fe0f50c --- /dev/null +++ b/bin/nproc/nproc.1 @@ -0,0 +1,54 @@ +.\"- +.\" * Copyright (c) 2023 Piotr Paweł Stefaniak +.\" +.\" * SPDX-License-Identifier: BSD-2-Clause +.\" +.Dd February 5, 2023 +.Dt NPROC 1 +.Os +.Sh NAME +.Nm nproc +.Nd print the number of processors +.Sh SYNOPSIS +.Nm +.Op Fl -all +.Op Fl -ignore Ns = Ns Ar count +.Nm Fl -help +.Nm Fl -version +.Sh DESCRIPTION +The +.Nm +utility is used to print the number of processors limited to the +.Xr cpuset 2 +of the current process, unless the +.Fl -all +flag is specified. +.Pp +The available flags are: +.Bl -tag -width Ds +.It Fl -all +Count all processors currently online. +.It Fl -ignore Ns = Ns Ar count +The result is decreased by +.Ar count , +but never below 1. +.It Fl -version +Print the current program version and exit. Don't use this option. +.It Fl -help +Print usage information and exit. +.El +.Sh COMPATIBILITY +This program is intended to be compatible with nproc as found in GNU coreutils. +.Sh SEE ALSO +.Xr cpuset 1 +.Sh HISTORY +The +.Nm +utility first appeared in +.Fx 14.0 . +.Sh AUTHORS +.An -nosplit +.An Mateusz Guzik Aq Mt mjg@FreeBSD.org +wrote the program and +.An Piotr Paweł Stefaniak Aq Mt pstef@FreeBSD.org +wrote this page. diff --git a/bin/nproc/nproc.c b/bin/nproc/nproc.c new file mode 100644 index 000000000000..9037c74dbfff --- /dev/null +++ b/bin/nproc/nproc.c @@ -0,0 +1,132 @@ +/*- + * Copyright (c) 2023 Mateusz Guzik + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +/* + * This program is intended to be compatible with nproc as found in GNU + * coreutils. + * + * In order to maintain that, do not add any features here if they are not + * present in said program. If you are looking for anything more advanced you + * probably should patch cpuset(1) instead. + */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define OPT_ALL (CHAR_MAX + 1) +#define OPT_IGNORE (CHAR_MAX + 2) +#define OPT_VERSION (CHAR_MAX + 3) +#define OPT_HELP (CHAR_MAX + 4) + +static struct option long_opts[] = { + { "all", no_argument, NULL, OPT_ALL }, + { "ignore", required_argument, NULL, OPT_IGNORE }, + { "version", no_argument, NULL, OPT_VERSION }, + { "help", no_argument, NULL, OPT_HELP }, + { NULL, 0, NULL, 0 } +}; + +static void +help(void) +{ + fprintf(stderr, + "usage: nproc [--all] [--ignore=count]\n"); + fprintf(stderr, + " nproc --help\n"); + fprintf(stderr, + " nproc --version\n"); +} + +static void +usage(void) +{ + help(); + exit(EX_USAGE); +} + +/* + * GNU variant ships with the --version switch. + * + * While we don't have anything to put there, print something which is + * whitespace-compatible with the original. Version number was taken + * from coreutils this code is in sync with. + */ +static void +version(void) +{ + printf("nproc (neither_GNU nor_coreutils) 8.32\n"); + exit(EXIT_SUCCESS); +} + +int +main(int argc, char *argv[]) +{ + const char *errstr; + cpuset_t mask; + int ch, cpus, ignore; + bool all_flag; + + ignore = 0; + all_flag = false; + + while ((ch = getopt_long(argc, argv, "", long_opts, NULL)) != -1) { + switch (ch) { + case OPT_ALL: + all_flag = true; + break; + case OPT_IGNORE: + ignore = strtonum(optarg, 0, INT_MAX, &errstr); + if (errstr) + errx(1, "bad ignore count: %s", errstr); + break; + case OPT_VERSION: + version(); + __builtin_unreachable(); + case OPT_HELP: + help(); + exit(EXIT_SUCCESS); + default: + usage(); + } + } + + argc -= optind; + argv += optind; + + if (argc != 0) + usage(); + + if (all_flag) { + cpus = sysconf(_SC_NPROCESSORS_ONLN); + if (cpus == -1) + err(1, "sysconf"); + } else { + CPU_ZERO(&mask); + if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, + sizeof(mask), &mask) != 0) + err(1, "cpuset_getaffinity"); + cpus = CPU_COUNT(&mask); + } + + if (ignore >= cpus) + cpus = 1; + else + cpus -= ignore; + + printf("%u\n", cpus); + + exit(EXIT_SUCCESS); +} diff --git a/usr.bin/cpuset/cpuset.1 b/usr.bin/cpuset/cpuset.1 index 935164394b31..1d0180c98991 100644 --- a/usr.bin/cpuset/cpuset.1 +++ b/usr.bin/cpuset/cpuset.1 @@ -217,6 +217,7 @@ Create a new cpuset that is restricted to CPUs 0 and 2 and move into the new set: .Dl cpuset -C -c -l 0,2 -p .Sh SEE ALSO +.Xr nproc 1 , .Xr cpuset 2 , .Xr rctl 8 .Sh HISTORY