7 Commits

Author SHA1 Message Date
Aleksa Sarai
033906b9f0 unix: add (*CPUSet).Fill helper to enable all CPUs
Some programs (such as container runtimes) want to reset their CPU
affinity if they are spawned by processes with a particular CPU
affinity. Container runtimes didn't really have to deal with this issue
until Linux 6.2 when the cpuset cgroup was changed to no longer
auto-reset CPU affinity in this case.

A naive approach to resetting your CPU affinity would be to get the
number of CPUs by looking at "/proc/stat" or "/sys/devices/system/cpu"
(note that runtime.NumCPU() actually returns the CPU affinity of the
process at startup time, which isn't useful for this purpose) and then
asking for all of those CPUs.

However, sched_setaffinity(2) will silently ignore any CPU bits set in
the provided CPUSet if they do not exist or are not enabled in the
cpuset cgroup of the process. This means that you can reset your CPU
affinity by just setting every CPU bit in CPUSet and passing it to
sched_setaffinity(2).

Unfortunately, setting every CPU bit in CPUSet with (*CPUSet).Set() is
very inefficient. If it were possible to just memset(0xFF) the CPUSet
array, users would be able to reset their CPU affinity even more
cheaply. However, Go doesn't have a memset primitive that can be used in
that way.

Obvious solutions like setting the array elements of CPUSet to (^0) do
not work because CPUSet is an array of a private newtype and so the
compiler complains if you try to use a constant like (^0) without a
cast (and we cannot use a cast because the type is private):

    cannot use ^0 (untyped int constant -1) as
    "golang.org/x/sys/unix".cpuMask value in assignment (overflows)

The only real alternative is to do something quite hacky like:

    cpuset := unix.CPUSet{}
    for i := range cpuset {
        cpuset[i]-- // underflow to 0xFF..FF
    }

... which is the solution we use in runc.

It would be much nicer to have a helper that does this memset for us in
a less hacky way, since resetting CPU affinity seems like a fairly
common operation.

Ref: Linux kernel commit da019032819a ("sched: Enforce user requested affinity")
Fixes golang/go#75186

Change-Id: I211ddeafd54ce35079a67493123d28e1bb76966a
GitHub-Last-Rev: 71871db0f3
GitHub-Pull-Request: golang/sys#259
Reviewed-on: https://go-review.googlesource.com/c/sys/+/698015
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Kirill Kolyshkin <kolyshkin@gmail.com>
Reviewed-by: Junyang Shao <shaojunyang@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
2025-09-26 10:17:15 -07:00
Aleksa Sarai
9bd37534d8 unix: switch (*CPUSet).Zero to clear builtin
clear was added to Go 1.21 and is better than the manual loop approach.

Change-Id: I851203714446e21b6329e2bcf308f2571d339e36
GitHub-Last-Rev: 71dc7f073e
GitHub-Pull-Request: golang/sys#262
Reviewed-on: https://go-review.googlesource.com/c/sys/+/698495
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Auto-Submit: Keith Randall <khr@golang.org>
2025-08-25 10:48:03 -07:00
Tobias Klauser
5fe476d890 unix: use bits.OnesCount64 instead of local copy
The local copy of bits.OnesCount64 was added for compatibility with Go
1.8 and earlier in CL 86477. Go 1.8 is no longer supported and go.mod
requires Go 1.12, so drop the local copy and use bits.OnesCount64.

Change-Id: Ieb77f0cef5f8f206b74ca737449efdcfe1949d44
Reviewed-on: https://go-review.googlesource.com/c/sys/+/192357
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
2019-08-29 20:48:30 +00:00
Matt Layher
1393eb0183 unix: fix several staticcheck issues
Resolves a number of issues, except for the clen function being
unused on Linux, as that would require a larger refactor which
probably isn't strictly necessary.

$ staticcheck .
affinity_linux.go:94:8: const m3 is unused (U1000)
affinity_linux.go:95:8: const m4 is unused (U1000)
sendfile_test.go:41:2: the goroutine calls T.Fatal, which must be called in the same goroutine as the test (SA2002)
sendfile_test.go:47:6: this value of err is never used (SA4006)
syscall_linux_test.go:519:21: should use time.Since instead of time.Now().Sub (S1012)
syscall_linux_test.go:530:21: should use time.Since instead of time.Now().Sub (S1012)
syscall_test.go:66:2: this value of ts is never used (SA4006)
syscall_test.go:67:2: this value of ts is never used (SA4006)
syscall_unix.go:88:6: func clen is unused (U1000)

Change-Id: I2611b4559339cea758b5da27ea1f36fb8cc2df3f
Reviewed-on: https://go-review.googlesource.com/c/sys/+/188037
Run-TryBot: Matt Layher <mdlayher@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2019-07-30 18:39:49 +00:00
Tobias Klauser
8f27ce8a60 unix: fix cpuset size argument in sched_affinity syscall
Fixes golang/go#23639

Change-Id: I7793dc50de35791a7c8af1966bb2ee4a36a2ac1c
Reviewed-on: https://go-review.googlesource.com/91375
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-02-01 15:31:26 +00:00
Brad Fitzpatrick
dd9ec17814 unix: fix build on Go 1.8
Don't use math/bits yet. Go 1.8 doesn't have it, and Go 1.8 is still
supported for a few weeks.

Fixes golang/go#23353

Change-Id: Ie69163f6f6dca95d4652441849e0152d2e346cd0
Reviewed-on: https://go-review.googlesource.com/86477
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-01-06 05:20:50 +00:00
Tobias Klauser
12d9d5b281 unix: add SchedGetaffinity and SchedSetaffinity on Linux
SchedGetaffinity and SchedSetaffinity are used to get and set a thread's
CPU affinity mask.

See http://man7.org/linux/man-pages/man2/sched_setaffinity.2.html for
details.

Also add a manual implementation of CpuSet_t and the corresponding
accessor functions (mimicking the CPU_* macros from sched.h).

Fixes golang/go#11243

Change-Id: Ia5abc0053cd06810b3b09ab65c27434f5323c1ad
Reviewed-on: https://go-review.googlesource.com/85915
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-01-05 20:31:47 +00:00