-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use cgroups aware processor count by default #348
Conversation
lib/parallel.rb
Outdated
def processor_count | ||
require 'etc' | ||
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Etc.nprocessors) | ||
require 'concurrent-ruby' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about this: no new dependency or version conflicts with other concurrent-ruby usage
... and maybe a readme entry for "if you want x add concurrent-ruby"
begin
require 'concurrent-ruby'
Concurrent.available_processor_count.to_i
rescue LoadError
require 'etc'
Etc.nprocessors
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe also use .floor
instead of .to_i
to make this more explicit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review.
how about this: no new dependency or version conflicts with other concurrent-ruby
This makes sense to me. I've updated my fix to use this approach.
f7bfe1b
to
2e13dfd
Compare
In containerized environments, a number of CPU cores isn't the same as the available CPUs. In this case, we need to consider cgroups. `concurrent-ruby` now has the method to get that since v1.3.1. I think it's better to use this for setting more container environment friendly default value. Ref: ruby-concurrency/concurrent-ruby#1038 I keep `Parallel.processor_count` as is to keep supporting setting processor count via env(`PARALLEL_PROCESSOR_COUNT`).
2e13dfd
to
39f5630
Compare
def processor_count | ||
require 'etc' | ||
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Etc.nprocessors) | ||
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || available_processor_count) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice side-effect that if somehow etc is broken you can set env var now :)
def available_processor_count | ||
require 'concurrent-ruby' | ||
Concurrent.available_processor_count.floor | ||
rescue LoadError, NoMethodError |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NoMethodError is a nice touch 👍
1.26.0 |
this change causes "negative array size" errors because
according to docs it looks like a bug in ruby-concurrency/concurrent-ruby#1038 |
Ah great 😞
I'll revert in a bit
…On Thu, Aug 8, 2024, 3:39 PM Pavel Rosický ***@***.***> wrote:
this change causes "negative array size" errors because
> Concurrent.available_processor_count.floor
=> -1
> Concurrent.physical_processor_count
=> 6
> Concurrent.processor_count
=> 12
> Etc.nprocessors
=> 12
> Concurrent.cpu_quota.floor
=> -1
according to docs
cpu.cfs_quota_us = -1: Unlimited CPU time.
cpu.cfs_quota_us = 0: No CPU time (tasks are blocked).
cpu.cfs_quota_us > 0: Limited CPU time as specified.
it looks like a bug in ruby-concurrency/concurrent-ruby#1038
<ruby-concurrency/concurrent-ruby#1038>
—
Reply to this email directly, view it on GitHub
<#348 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAACYZ7XFZY7ZRDIYDF5QRDZQNYH7AVCNFSM6AAAAABMFRSALOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENZVHA2TSMJSGY>
.
You are receiving this because you modified the open/close state.Message
ID: ***@***.***>
|
You can fall back to using Etc.nprocessors if Concurrent.available_processor_count returns an invalid value, such as -1 (negative) or 0. |
…cpu.cfs_quota_us` is -1 I tried to use `Concurrent.available_processor_count` in `parallel` gem, but we got some feedback `Concurrent.available_processor_count` returned a negative value. grosser/parallel#348 (comment) grosser/parallel#349 (comment) According to the https://docs.kernel.org/scheduler/sched-bwc.html#management, The default value of `cpu.cfs_quota_us` is -1. In that case, cgroup does not adhere to any CPU time restrictions. This PR adds the case of `cpu.cfs_quota_us` is -1 to `#cpu_quota` to return processor count from `Concurrent.available_processor_count` in that case.
…cpu.cfs_quota_us` is -1 I tried to use `Concurrent.available_processor_count` in `parallel` gem, but we got some feedback `Concurrent.available_processor_count` returned a negative value. grosser/parallel#348 (comment) grosser/parallel#349 (comment) According to the https://docs.kernel.org/scheduler/sched-bwc.html#management, The default value of `cpu.cfs_quota_us` is -1. In that case, cgroup does not adhere to any CPU time restrictions. This PR adds the case of `cpu.cfs_quota_us` is -1 to `#cpu_quota` to return processor count from `Concurrent.available_processor_count` in that case.
…cpu.cfs_quota_us` is -1 I tried to use `Concurrent.available_processor_count` in `parallel` gem, but we got some feedback `Concurrent.available_processor_count` returned a negative value. grosser/parallel#348 (comment) grosser/parallel#349 (comment) According to the https://docs.kernel.org/scheduler/sched-bwc.html#management, The default value of `cpu.cfs_quota_us` is -1. In that case, cgroup does not adhere to any CPU time restrictions. This PR adds the case of `cpu.cfs_quota_us` is -1 to `#cpu_quota` to return processor count from `Concurrent.available_processor_count` in that case.
…cpu.cfs_quota_us` is -1 I tried to use `Concurrent.available_processor_count` in `parallel` gem, but we got some feedback `Concurrent.available_processor_count` returned a negative value. grosser/parallel#348 (comment) grosser/parallel#349 (comment) According to the https://docs.kernel.org/scheduler/sched-bwc.html#management, The default value of `cpu.cfs_quota_us` is -1. In that case, cgroup does not adhere to any CPU time restrictions. This PR adds the case of `cpu.cfs_quota_us` is -1 to `#cpu_quota` to return processor count from `Concurrent.available_processor_count` in that case.
|
…cessor_count"" This reverts commit c316cc5.
re-released as v1.26.2 🤞 |
In containerized environments, a number of CPU cores isn't the same as the available CPUs. In this case, we need to consider cgroups.
concurrent-ruby
now has the method to get that since v1.3.1. I think it's better to use this for setting more container environment friendly default value. Ref: ruby-concurrency/concurrent-ruby#1038I keep
Parallel.processor_count
as is to keep supporting setting processor count via env(PARALLEL_PROCESSOR_COUNT
).