www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19513] New: Use sched_getaffinity(2) to get the number of CPU


          Issue ID: 19513
           Summary: Use sched_getaffinity(2) to get the number of CPU
                    cores if available
           Product: D
           Version: D2
          Hardware: All
                OS: Linux
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: phobos
          Assignee: nobody puremagic.com
          Reporter: kubo39 gmail.com

Currently std.parallelism.totalCPUs is implemented by
However, on GNU/Linux, usable number of processoes may be restricted if a
process runs in containers.
In case it's better to use sched_getaffinity(2).

How to implement:

1. std.parallelism.totalCPUs uses sched_getaffinity(2) internally.
2. Add new API (and considering function name).
  - std.parallelism.sched_getaffinity(pid, mask)? (Like Python)
  - std.parallelism.nprocessors? (Like Ruby)
  - or berrer name

Libraries exposing sched_getaffinity:

- Python >= 3.3 provides os.get_affinity(pid, mask) function.
- Ruby >= 2.2 provides Etc.nprocessors function.
- GNU coreutils provides nproc(1) command.
- Rust's num_cpus crate provides num_cpus::get() function.


- https://github.com/golang/go/issues/3921
- https://docs.python.org/3/library/os.html#os.sched_getaffinity
- https://bugs.ruby-lang.org/issues/10267
- http://man7.org/linux/man-pages/man1/nproc.1.html

Dec 25 2018