diff --git a/bin/Makefile b/bin/Makefile index cdd96d0b84e8..83bf79dcb9f9 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -1,52 +1,53 @@ # From: @(#)Makefile 8.1 (Berkeley) 5/31/93 # $FreeBSD$ .include SUBDIR= cat \ chflags \ chio \ chmod \ cp \ date \ dd \ df \ domainname \ echo \ ed \ expr \ freebsd-version \ getfacl \ hostname \ kenv \ kill \ ln \ ls \ mkdir \ mv \ + nproc \ pax \ pkill \ ps \ pwait \ pwd \ realpath \ rm \ rmdir \ setfacl \ sh \ sleep \ stty \ sync \ test \ timeout \ uuidgen SUBDIR.${MK_SENDMAIL}+= rmail SUBDIR.${MK_TCSH}+= csh SUBDIR.${MK_TESTS}+= tests .include SUBDIR_PARALLEL= .include 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 @@ -1,228 +1,229 @@ .\" Copyright (c) 2008 Christian Brueffer .\" Copyright (c) 2008 Jeffrey Roberson .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" $FreeBSD$ .\" .Dd July 3, 2018 .Dt CPUSET 1 .Os .Sh NAME .Nm cpuset .Nd "configure processor sets" .Sh SYNOPSIS .Nm .Op Fl l Ar cpu-list .Op Fl n Ar policy:domain-list .Op Fl s Ar setid .Ar cmd ... .Nm .Op Fl l Ar cpu-list .Op Fl n Ar policy:domain-list .Op Fl s Ar setid .Fl p Ar pid .Nm .Op Fl c .Op Fl l Ar cpu-list .Op Fl n Ar policy:domain-list .Fl C .Fl p Ar pid .Nm .Op Fl c .Op Fl l Ar cpu-list .Op Fl n Ar policy:domain-list .Op Fl j Ar jail | Fl p Ar pid | Fl t Ar tid | Fl s Ar setid | Fl x Ar irq .Nm .Fl g .Op Fl cir .Op Fl d Ar domain | Fl j Ar jail | Fl p Ar pid | Fl t Ar tid | Fl s Ar setid | Fl x Ar irq .Sh DESCRIPTION The .Nm command can be used to assign processor sets to processes, run commands constrained to a given set or list of processors and memory domains, and query information about processor binding, memory binding and policy, sets, and available processors and memory domains in the system. .Pp .Nm requires a target to modify or query. The target may be specified as a command, process id, thread id, a cpuset id, an irq, a jail, or a NUMA domain. Using .Fl g the target's set id or mask may be queried. Using .Fl l or .Fl s the target's CPU mask or set id may be set. If no target is specified, .Nm operates on itself. Not all combinations of operations and targets are supported. For example, you may not set the id of an existing set or query and launch a command at the same time. .Pp There are two sets applicable to each process and one private mask per thread. Every process in the system belongs to a cpuset. By default processes are started in set 1. The mask or id may be queried using .Fl c . Each thread also has a private mask of CPUs it is allowed to run on that must be a subset of the assigned set. And finally, there is a root set, numbered 0, that is immutable. This last set is the list of all possible CPUs in the system and is queried using .Fl r . .Pp Most sets include NUMA memory domain and policy information. This can be inspected with .Fl g and set with .Fl n . This will specify which NUMA domains are visible to the process and affect where anonymous memory and file pages will be stored on first access. Files accessed first by other processes may specify conflicting policy. .Pp When running a command it may join a set specified with .Fl s otherwise a new set is created. In addition, a mask for the command may be specified using .Fl l . When used in conjunction with .Fl c the mask modifies the supplied or created set rather than the private mask for the thread. .Pp The options are as follows: .Bl -tag -width ".Fl l Ar cpu-list" .It Fl C Create a new cpuset and assign the target process to that set. .It Fl c The requested operation should reference the cpuset available via the target specifier. .It Fl d Ar domain Specifies a NUMA domain id as the target of the operation. This can only be used to query the cpus visible in each numberd domain. .It Fl g Causes .Nm to print either a list of valid CPUs or, using .Fl i , the id of the target. .It Fl i When used with the .Fl g option print the id rather than the valid mask of the target. .It Fl j Ar jail Specifies a jail id or name as the target of the operation. .It Fl l Ar cpu-list Specifies a list of CPUs to apply to a target. Specification may include numbers separated by '-' for ranges and commas separating individual numbers. A special list of .Dq all may be specified in which case the list includes all CPUs from the root set. .It Fl n Ar policy:domain-list Specifies a list of domains and allocation policy to apply to a target. Ranges may be specified as in .Fl l . Valid policies include first-touch (ft), round-robin (rr), prefer and interleave (il). First-touch allocates on the local domain when memory is available. Round-robin alternates between every possible domain page at a time. The prefer policy accepts only a single domain in the set. The parent of the set is consulted if the preferred domain is unavailable. Interleave operates like round-robin with an implementation defined stripe width. See .Xr domainset 9 for more details on policies. .It Fl p Ar pid Specifies a pid as the target of the operation. .It Fl s Ar setid Specifies a set id as the target of the operation. .It Fl r The requested operation should reference the root set available via the target specifier. .It Fl t Ar tid Specifies a thread id as the target of the operation. .It Fl x Ar irq Specifies an irq as the target of the operation. .El .Sh EXIT STATUS .Ex -std .Sh EXAMPLES Create a new group with CPUs 0-4 inclusive and run .Pa /bin/sh on it: .Dl cpuset -c -l 0-4 /bin/sh .Pp Query the mask of CPUs the .Aq sh pid is allowed to run on: .Dl cpuset -g -p .Pp Restrict .Pa /bin/sh to run on CPUs 0 and 2 while its group is still allowed to run on CPUs 0-4: .Dl cpuset -l 0,2 -p .Pp Modify the cpuset .Pa /bin/sh belongs to restricting it to CPUs 0 and 2: .Dl cpuset -l 0,2 -c -p .Pp Modify the cpuset all threads are in by default to contain only the first 4 CPUs, leaving the rest idle: .Dl cpuset -l 0-3 -s 1 .Pp Print the id of the cpuset .Pa /bin/sh is in: .Dl cpuset -g -i -p .Pp Move the .Ar pid into the specified cpuset .Ar setid so it may be managed with other pids in that set: .Dl cpuset -s -p .Pp Create a new cpuset that is restricted to CPUs 0 and 2 and move .Ar pid 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 The .Nm command first appeared in .Fx 7.1 . .Sh AUTHORS .An Jeffrey Roberson Aq Mt jeff@FreeBSD.org