Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 05 Feb 2023 00:07:34 +0100
From:      Steffen Nurpmeso <steffen@sdaoden.eu>
To:        Mateusz Guzik <mjguzik@gmail.com>
Cc:        Mateusz Guzik <mjg@freebsd.org>, src-committers@freebsd.org, dev-commits-src-all@freebsd.org, dev-commits-src-main@freebsd.org
Subject:   Re: git: d9d5f2c042a5 - main - cpuset: add --count
Message-ID:  <20230204230734.U91zY%steffen@sdaoden.eu>
In-Reply-To: <CAGudoHHZiVLQgnYhQdDSKNaKSirCoMyT2Fh2VMpqz33akids6Q@mail.gmail.com>
References:  <202302041751.314HpRrn042970@gitrepo.freebsd.org> <20230204203529.CusFt%steffen@sdaoden.eu> <CAGudoHHZiVLQgnYhQdDSKNaKSirCoMyT2Fh2VMpqz33akids6Q@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Mateusz Guzik wrote in
 <CAGudoHHZiVLQgnYhQdDSKNaKSirCoMyT2Fh2VMpqz33akids6Q@mail.gmail.com>:
 |On 2/4/23, Steffen Nurpmeso <steffen@sdaoden.eu> wrote:
 |> Mateusz Guzik wrote in
 |>  <202302041751.314HpRrn042970@gitrepo.freebsd.org>:
 |>|The branch main has been updated by mjg:
 |>|
 |>|URL: https://cgit.FreeBSD.org/src/commit/?id=3Dd9d5f2c042a51a9f0dd69eb1=
fc3\
 |>|49efd81ffa483
 |>|
 |>|commit d9d5f2c042a51a9f0dd69eb1fc349efd81ffa483
 |>|Author:     Mateusz Guzik <mjg@FreeBSD.org>
 |>|AuthorDate: 2023-02-04 17:47:41 +0000
 |>|Commit:     Mateusz Guzik <mjg@FreeBSD.org>
 |>|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.
 |>  ...
 |>|    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.
 |>
 |> This is great (in respect to a #freebsd IRC from 2021-07-02)!
 |> Are there plans to address
 |>
 |>   19:26 < RhodiumToad> and in fact sysconf(_SC_NPROCESSORS_ONLN) is
 |> implemented as  sysctl hw.ncpu
 |>
 |> or
 |>
 |>   19:25 < RhodiumToad> on fbsd 13 on a 4-cpu box, cpuset -l 0-1 getconf
 |> _NPROCESSORS_ONLN   returns 4 rather than 2
 |>
 |
 |So I just checked on Linux and get:
 |$ taskset --cpu-list 0 getconf _NPROCESSORS_ONLN
 |4

At least it does not count totally disabled SMT CPUs (here), but
true, also cgroup'ized restricted ones show up "falsely" for 6.1.
(This could be a "bug" in the C library though, as this checks
/sys/devices/system/cpu/online (nproc(1) even fails in boxes
without /sys/), /proc/stat, and only then (iirc now) uses
sched_getaffinity.  It documents get_nprocs(3) as "number of
processors currently available in the system".

POSIX standardized these[1] as _SC_NPROCESSORS_CONF
+ _SC_NPROCESSORS_ONLN for sysconf(3) as well as NPROCESSORS_CONF
and NPROCESSORS_ONLN for getconf(1), saying

  ...
    The maximum number of execution units | _SC_NPROCESSORS_ONLN
    currently available to run threads=E2=80=A0 |
  ....
    =E2=80=A0 The nature of an execution unit and the precise conditions
    under which an execution unit is considered to be available,
    or can be made available, or how many threads it can execute
    in parallel, are implementation-defined.

Which is not the same.  Implementation-defined though.
But i mean if it is easy to give correct results for an execution
context (i would).

  [1] https://austingroupbugs.net/view.php?id=3D339

 |iow the current behavior is probably the expected one.

 |> or even add a shallow usr.bin/nproc(1) that does only this
 |> operation, as in Linux?  (Or ln(1) it and check out argv[0] to do
 |> so?)  Having a "portable" thing would be great.
 |
 |I have no opinion on this one, looks a trivial addition, but someone
 |else will have to ack it (most notably whether to add it to cpuset as
 |you suggested above or create new utility altogether).
 |
 |That said, happy to take a look at a patch which adds a *new*
 |nproc utility.

	#!/bin/sh -
	if [ $# -ne 0 ]; then
		echo >&2 'Synopsis: nproc'
		exit 1
	fi
	exec cpuset --count

	.Dd February 4, 2023
	.Dt NPROC 1
	.Os
	.Sh NAME
	.Nm nproc
	.Nd print the number of available processing units
	.Sh SYNOPSIS
	.Nm
	.Sh DESCRIPTION
	.Nm
	prints the number of processing units available to the current process,
	which may be less than the number of online processors.
	.Sh "SEE ALSO"
	.Xr cpuset 1
	.Sh HISTORY
	Appeared in
	.Fx 14 .

Ciao,

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20230204230734.U91zY%steffen>