]> git.lizzy.rs Git - rust.git/commit
Use cgroup quotas for calculating `available_parallelism`
authorThe 8472 <git@infinite-source.de>
Sun, 9 Jan 2022 18:39:02 +0000 (19:39 +0100)
committerThe 8472 <git@infinite-source.de>
Wed, 2 Mar 2022 23:43:45 +0000 (00:43 +0100)
commitbac5523ea0025988361a092c1f0e7f4eb90f6ad7
tree825750b995d9f2c9b1c2bab49094f7196acb5673
parent8769f4ef2fe1efddd1f072485f97f568e7328f79
Use cgroup quotas for calculating `available_parallelism`

Manually tested via

```
// spawn a new cgroup scope for the current user
$ sudo systemd-run -p CPUQuota="300%" --uid=$(id -u) -tdS

// quota.rs
#![feature(available_parallelism)]
fn main() {
    println!("{:?}", std::thread::available_parallelism()); // prints Ok(3)
}
```

Caveats

* cgroup v1 is ignored
* funky mountpoints (containing spaces, newlines or control chars) for cgroupfs will not be handled correctly since that would require unescaping /proc/self/mountinfo
  The escaping behavior of procfs seems to be undocumented. systemd and docker default to `/sys/fs/cgroup` so it should be fine for most systems.
* quota will be ignored when `sched_getaffinity` doesn't work
* assumes procfs is mounted under `/proc` and cgroupfs mounted and readable somewhere in the directory tree
library/std/src/sys/unix/thread.rs