Skip to content
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

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ Tips
- [Isolation] Do not reuse previous worker processes: `isolation: true`
- [Stop all processes with an alternate interrupt signal] `'INT'` (from `ctrl+c`) is caught by default. Catch `'TERM'` (from `kill`) with `interrupt_signal: 'TERM'`
- [Process count via ENV] `PARALLEL_PROCESSOR_COUNT=16` will use `16` instead of the number of processors detected. This is used to reconfigure a tool using `parallel` without inserting custom logic.
- [Process count] `parallel` uses a number of processors seen by the OS for process count by default. If you want to use a value considering CPU quota, please add `concurrent-ruby` to your `Gemfile`.

TODO
====
Expand Down
14 changes: 11 additions & 3 deletions lib/parallel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,10 @@ def physical_processor_count
end
end

# Number of processors seen by the OS, used for process scheduling
# Number of processors seen by the OS or value considering CPU quota if the process is inside a cgroup,
# used for process scheduling
def processor_count
require 'etc'
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Etc.nprocessors)
@processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || available_processor_count)
Copy link
Owner

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 :)

end

def worker_number
Expand Down Expand Up @@ -695,5 +695,13 @@ def instrument_start(item, index, options)
return unless (on_start = options[:start])
options[:mutex].synchronize { on_start.call(item, index) }
end

def available_processor_count
require 'concurrent-ruby'
Concurrent.available_processor_count.floor
rescue LoadError, NoMethodError
Copy link
Owner

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 👍

require 'etc'
Etc.nprocessors
end
end
end
5 changes: 0 additions & 5 deletions spec/parallel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ def without_ractor_warning(out)
(1..999).should include(Parallel.processor_count)
end
end

it 'uses Etc.nprocessors in Ruby 2.2+' do
defined?(Etc).should == "constant"
Etc.respond_to?(:nprocessors).should == true
end
end

describe ".physical_processor_count" do
Expand Down