From nobody Sat Feb 04 17:51:27 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 4P8Kp726Sxz3ngPq; Sat, 4 Feb 2023 17:51:27 +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 4P8Kp71XjSz4Q9F; Sat, 4 Feb 2023 17:51:27 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675533087; 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=TzdwE589rlRRKeAYAlmM9zXaIFRWEzclfVa/5tLD2C4=; b=i6d+SKYE5bC47NV/Hlc4xQqD206lFs8mBW6i1b9a8QvvzpNAcPwb9uXfgnhzjWE3r19TUM IV9hEnxMpegVIy5UuJnIbbLMVuDFOmtwRmsM/brxeItazvTvfsLA8fEXzmHl7+SfsVoj40 QrS9tRK5AuCVyW9fXkifMC46C4aDp17Oms1P1eRN/8/LSM67/c19W547mMKkLw6bHceUro U2mzNsdFon7LQSQhF+gdmnblVXR/A0iVIv2Ju9PrpWx1gwmZT01+ygJ+GVu5s3XxPAbLJf CSqTfYYHnLHjnRfMKfiYSv6kCe10i64Z3L0A/zqmzbjorXv2pt7hNhqjOX4H7g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1675533087; 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=TzdwE589rlRRKeAYAlmM9zXaIFRWEzclfVa/5tLD2C4=; b=esG83TOIS6ctmWLezhHVRnKDGDBBzRDEIdM/fmrb3U3EXlVAARrlo4OKTgp1nHgRE0e5pq wy0rDlFisUoNX7pmCegpbjozW+JY7DHTmnX9S2jK5TzvCGasnAuIu7/mUZkmY4hF9Nk6DW xjdqKuEtksx4M6ZyDqeVYvEJ52NUkoAZluNXPght4XLWszSo/01HOpRN+ChFwMt8laSHFm Q+HImcTJ8AaBYOPE0RCj5nr4pMcpBH04OUueStOngwI3Po7cb50RPGvmCM6hvkfbbrwwte 0w7VrmVcJ9BGtyHNIcRFkxsdA7tcHteRxpcuaO1bGrSN9XK6EvA1Y7SPbxDK2A== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1675533087; a=rsa-sha256; cv=none; b=uTbU3Jxj6bKGY6thJqqCzo5d7OLZVlIiWtTHOilFprHUdYFphYf2hwoFQCqHjukw8Iqy0T ++n1++7T9nI+mOPCZwO8gMUSgXs6tm+E9LSmjQaXwCF6IY+R58N+BiRJPzx5GOchqo+Rgp nBj1v+FzjoKobbzVkCWQ4XNAZnJshBapfofyLI9OYu/893cFy4Q6QznecdVVlDNt2Ty6it cyuWH9ILtGRaNFPmqYnlROGbCHlqULOfNVhKxbMRCfq+fIa7ijlEpx0GkjYFeZNX+r2lTB nD/TJjJxFlVMk752qbASfOi0zPWPl/ueDBGAXMQr9fDPg9gNmIkLaVhKnlHCkQ== 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 4P8Kp70cNvzNs7; Sat, 4 Feb 2023 17:51:27 +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 314HpRs6042971; Sat, 4 Feb 2023 17:51:27 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 314HpRrn042970; Sat, 4 Feb 2023 17:51:27 GMT (envelope-from git) Date: Sat, 4 Feb 2023 17:51:27 GMT Message-Id: <202302041751.314HpRrn042970@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Guzik Subject: git: d9d5f2c042a5 - main - cpuset: add --count 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/main X-Git-Reftype: branch X-Git-Commit: d9d5f2c042a51a9f0dd69eb1fc349efd81ffa483 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=d9d5f2c042a51a9f0dd69eb1fc349efd81ffa483 commit d9d5f2c042a51a9f0dd69eb1fc349efd81ffa483 Author: Mateusz Guzik AuthorDate: 2023-02-04 17:47:41 +0000 Commit: Mateusz Guzik CommitDate: 2023-02-04 17:50:41 +0000 cpuset: add --count Can be used to count the number of hardware threads in the cpu set. For example: $ cpuset -g -p $$ pid 2440 mask: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 pid 2440 domain policy: first-touch mask: 0, 1 $ cpuset -g --count -p $$ 40 The intent is to replace calls to sysctl hw.ncpu and kern.smp.cpus which can be found in the tree, which are not adequate given existence of cpusets. Right now only -g -p combination is supported to reduce complexity. As anything else errors out, this can be expanded later as needed. Differential Revision: https://reviews.freebsd.org/D36351 --- usr.bin/cpuset/cpuset.1 | 16 +++++++++++++++- usr.bin/cpuset/cpuset.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/usr.bin/cpuset/cpuset.1 b/usr.bin/cpuset/cpuset.1 index 935164394b31..1a8902e1c235 100644 --- a/usr.bin/cpuset/cpuset.1 +++ b/usr.bin/cpuset/cpuset.1 @@ -25,7 +25,7 @@ .\" .\" $FreeBSD$ .\" -.Dd July 3, 2018 +.Dd August 25, 2022 .Dt CPUSET 1 .Os .Sh NAME @@ -57,6 +57,10 @@ .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 +.Nm +.Fl g +.Fl -count +.Fl p Ar pid .Sh DESCRIPTION The .Nm @@ -172,6 +176,16 @@ 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 +.Pp +The long options are as follows: +.Bl -tag -width ".Fl -count" +.It Fl -count +Count the number of hardware threads included in the set. Requires +.Fl g +and +.Fl p +flags. +.El .Sh EXIT STATUS .Ex -std .Sh EXAMPLES diff --git a/usr.bin/cpuset/cpuset.c b/usr.bin/cpuset/cpuset.c index 79c6c2b6ca79..528cbc39bbc7 100644 --- a/usr.bin/cpuset/cpuset.c +++ b/usr.bin/cpuset/cpuset.c @@ -44,6 +44,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -52,6 +53,9 @@ __FBSDID("$FreeBSD$"); #include #include +/* + * Short opts. + */ static int Cflag; static int cflag; static int dflag; @@ -65,12 +69,24 @@ static int rflag; static int sflag; static int tflag; static int xflag; + +/* + * Long-only opts. + */ +static int count_flag; + static id_t id; static cpulevel_t level; static cpuwhich_t which; +#define OPT_THREADCOUNT (CHAR_MAX + 1) static void usage(void); +static struct option long_opts[] = { + { "count", no_argument, NULL, OPT_THREADCOUNT }, + { NULL, 0, NULL, 0 } +}; + struct numa_policy { const char *name; int policy; @@ -283,6 +299,18 @@ printsetid(void) levelnames[level], setid); } +static void +printcpucount(void) +{ + cpuset_t mask; + CPU_ZERO(&mask); + + if (cpuset_getaffinity(CPU_LEVEL_ROOT, CPU_WHICH_PID, id, + sizeof(mask), &mask) != 0) + err(EXIT_FAILURE, "getaffinity"); + printf("%d\n", CPU_COUNT(&mask)); +} + int main(int argc, char *argv[]) { @@ -300,7 +328,8 @@ main(int argc, char *argv[]) level = CPU_LEVEL_WHICH; which = CPU_WHICH_PID; id = pid = tid = setid = -1; - while ((ch = getopt(argc, argv, "Ccd:gij:l:n:p:rs:t:x:")) != -1) { + while ((ch = getopt_long(argc, argv, + "Ccd:gij:l:n:p:rs:t:x:", long_opts, NULL)) != -1) { switch (ch) { case 'C': Cflag = 1; @@ -359,12 +388,30 @@ main(int argc, char *argv[]) which = CPU_WHICH_IRQ; id = atoi(optarg); break; + case OPT_THREADCOUNT: + count_flag = 1; + break; default: usage(); } } argc -= optind; argv += optind; + + /* + * count requires g and p flags and is incompatible with + * everything else for simplicity. + */ + if (count_flag) { + if (!gflag || !pflag) + usage(); + if (Cflag || cflag || dflag || iflag || jflag || lflag || + nflag || rflag || sflag || tflag || xflag) + usage(); + printcpucount(); + exit(EXIT_SUCCESS); + } + if (gflag) { if (argc || Cflag || lflag || nflag) usage(); @@ -471,5 +518,7 @@ usage(void) fprintf(stderr, " cpuset -g [-cir]\n" " [-d domain | -j jailid | -p pid | -t tid | -s setid | -x irq]\n"); + fprintf(stderr, + " cpuset -g --count -p pid\n"); exit(1); }