Skip to content

Commit ddf62f6

Browse files
committed
Fix the return value of Concurrent.available_processor_count when 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.
1 parent 6f7c91a commit ddf62f6

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/concurrent-ruby/concurrent/utility/processor_counter.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def compute_physical_processor_count
8383
# Bail out if both commands returned something unexpected
8484
processor_count
8585
else
86-
# powershell: "\nNumberOfCores\n-------------\n 4\n\n\n"
86+
# powershell: "\nNumberOfCores\n-------------\n 4\n\n\n"
8787
# wmic: "NumberOfCores \n\n4 \n\n\n\n"
8888
result.scan(/\d+/).map(&:to_i).reduce(:+)
8989
end
@@ -112,7 +112,9 @@ def compute_cpu_quota
112112
elsif File.exist?("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us")
113113
# cgroups v1: https://kernel.googlesource.com/pub/scm/linux/kernel/git/glommer/memcg/+/cpu_stat/Documentation/cgroups/cpu.txt
114114
max = File.read("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us").to_i
115-
return nil if max == 0
115+
# If the cpu.cfs_quota_us is -1, cgroup does not adhere to any CPU time restrictions
116+
# https://docs.kernel.org/scheduler/sched-bwc.html#management
117+
return nil if max == 0 || max == -1
116118
period = File.read("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us").to_f
117119
max / period
118120
end

spec/concurrent/utility/processor_count_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ module Concurrent
5656
expect(counter.cpu_quota).to be_nil
5757
end
5858

59+
it 'returns nil if cgroups v1 and cpu.cfs_quota_us is -1' do
60+
expect(RbConfig::CONFIG).to receive(:[]).with("target_os").and_return("linux")
61+
expect(File).to receive(:exist?).with("/sys/fs/cgroup/cpu.max").and_return(false)
62+
expect(File).to receive(:exist?).with("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us").and_return(true)
63+
64+
expect(File).to receive(:read).with("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us").and_return("-1\n")
65+
expect(counter.cpu_quota).to be_nil
66+
end
67+
5968
it 'returns a float if cgroups v1 sets a limit' do
6069
expect(RbConfig::CONFIG).to receive(:[]).with("target_os").and_return("linux")
6170
expect(File).to receive(:exist?).with("/sys/fs/cgroup/cpu.max").and_return(false)

0 commit comments

Comments
 (0)