Skip to content

Fix the return value of Concurrent.available_processor_count when cpu.cfs_quota_us is -1 #1060

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

Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Current

* (#1060) Fix bug with return value of `Concurrent.available_processor_count` when `cpu.cfs_quota_us` is -1.

## Release v1.3.3 (9 June 2024)

* (#1053) Improve the speed of `Concurrent.physical_processor_count` on Windows.
Expand Down
4 changes: 3 additions & 1 deletion lib/concurrent-ruby/concurrent/utility/processor_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def compute_cpu_quota
elsif File.exist?("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us")
# cgroups v1: https://kernel.googlesource.com/pub/scm/linux/kernel/git/glommer/memcg/+/cpu_stat/Documentation/cgroups/cpu.txt
max = File.read("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us").to_i
return nil if max == 0
# If the cpu.cfs_quota_us is -1, cgroup does not adhere to any CPU time restrictions
# https://docs.kernel.org/scheduler/sched-bwc.html#management
return nil if max <= 0
period = File.read("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us").to_f
max / period
end
Expand Down
9 changes: 9 additions & 0 deletions spec/concurrent/utility/processor_count_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ module Concurrent
expect(counter.cpu_quota).to be_nil
end

it 'returns nil if cgroups v1 and cpu.cfs_quota_us is -1' do
expect(RbConfig::CONFIG).to receive(:[]).with("target_os").and_return("linux")
expect(File).to receive(:exist?).with("/sys/fs/cgroup/cpu.max").and_return(false)
expect(File).to receive(:exist?).with("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us").and_return(true)

expect(File).to receive(:read).with("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us").and_return("-1\n")
expect(counter.cpu_quota).to be_nil
end

it 'returns a float if cgroups v1 sets a limit' do
expect(RbConfig::CONFIG).to receive(:[]).with("target_os").and_return("linux")
expect(File).to receive(:exist?).with("/sys/fs/cgroup/cpu.max").and_return(false)
Expand Down