Feature #21797
openMake Etc.nprocessors cgroup-aware on Linux
Description
Currently, Etc.nprocessors ignores cgroup CPU quotas. This causes issues in containers where CPU limits differ from the host CPU count.
I have written a gem for this purpose (https://github.com/moznion/maxprocs-ruby), but it would be preferable if the Ruby core implementation respected cgroup configuration.
Additionally, concurrent-ruby provides similar functionality, but if the language itself offered this capability, language users would not need to implement it individually.
And some parts of the Ruby language handle the number of processors using hard-coded values (e.g., https://github.com/ruby/ruby/blob/8efaf5e6b6a25e0d237f3d71b75865661ae98268/thread_pthread.c#L1737-L1738), so this could also be useful for Ruby language development.
Extending Etc.nprocessors to respect cgroups is one option, but that would be a breaking change, so adding a new API (e.g., Etc.cpu_quota or something?) might be a better approach.
Prior Art¶
Updated by moznion (Taiki Kawakami) about 3 hours ago
- Description updated (diff)
Updated by hsbt (Hiroshi SHIBATA) about 3 hours ago
RubyGems 4.0.x support -j option for building C extension gem. But It causes in container environment like Circle CI.
https://github.com/ruby/rubygems/issues/9170
If cgroup provides the correct number of CPU for Cicle CI or others, I'm positive to support this.
Updated by moznion (Taiki Kawakami) about 3 hours ago
- Description updated (diff)
Updated by osyoyu (Daisuke Aritomo) about 2 hours ago
It'd be nice if RUBY_MAX_CPU would be autoconfigured based on this, just like Go 1.25 GOMAXPROCS.
Its default value is currently fixed to 8, but not many cloud containers have 8 cores worth of processors.
https://github.com/ruby/ruby/blob/8efaf5e6b6a25e0d237f3d71b75865661ae98268/thread_pthread.c#L1737-L1738