Skip to content

Commit 98d0f16

Browse files
y-yagieregon
authored andcommitted
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 cbee215 commit 98d0f16

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Current
22

3+
* (#1060) Fix bug with return value of `Concurrent.available_processor_count` when `cpu.cfs_quota_us` is -1.
4+
35
## Release v1.3.3 (9 June 2024)
46

57
* (#1053) Improve the speed of `Concurrent.physical_processor_count` on Windows.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
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)