File tree Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Expand file tree Collapse file tree 3 files changed +37
-2
lines changed Original file line number Diff line number Diff line change 1
1
# Unreleased
2
2
3
+ * Improve CLI to detect cgroup CPU limits and avoid spawning too many worker processes.
4
+
3
5
# 1.18.4
4
6
5
7
* Allow using bootsnap without bundler. See #488 .
Original file line number Diff line number Diff line change 4
4
require "bootsnap/cli/worker_pool"
5
5
require "optparse"
6
6
require "fileutils"
7
- require "etc"
8
7
9
8
module Bootsnap
10
9
class CLI
@@ -29,7 +28,7 @@ def initialize(argv)
29
28
self . compile_gemfile = false
30
29
self . exclude = nil
31
30
self . verbose = false
32
- self . jobs = Etc . nprocessors
31
+ self . jobs = nil
33
32
self . iseq = true
34
33
self . yaml = true
35
34
self . json = true
Original file line number Diff line number Diff line change 1
1
# frozen_string_literal: true
2
2
3
+ require "etc"
4
+ require "rbconfig"
5
+
3
6
module Bootsnap
4
7
class CLI
5
8
class WorkerPool
6
9
class << self
7
10
def create ( size :, jobs :)
11
+ size ||= default_size
8
12
if size > 0 && Process . respond_to? ( :fork )
9
13
new ( size : size , jobs : jobs )
10
14
else
11
15
Inline . new ( jobs : jobs )
12
16
end
13
17
end
18
+
19
+ def default_size
20
+ size = [ Etc . nprocessors , cpu_quota || 0 ] . min
21
+ case size
22
+ when 0 , 1
23
+ 0
24
+ else
25
+ size
26
+ end
27
+ end
28
+
29
+ def cpu_quota
30
+ if RbConfig ::CONFIG [ "target_os" ] . include? ( "linux" )
31
+ if File . exist? ( "/sys/fs/cgroup/cpu.max" )
32
+ # cgroups v2: https://docs.kernel.org/admin-guide/cgroup-v2.html#cpu-interface-files
33
+ cpu_max = File . read ( "/sys/fs/cgroup/cpu.max" )
34
+ return nil if cpu_max . start_with? ( "max " ) # no limit
35
+ max , period = cpu_max . split . map ( &:to_f )
36
+ max / period
37
+ elsif File . exist? ( "/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us" )
38
+ # cgroups v1: https://kernel.googlesource.com/pub/scm/linux/kernel/git/glommer/memcg/+/cpu_stat/Documentation/cgroups/cpu.txt
39
+ max = File . read ( "/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us" ) . to_i
40
+ # If the cpu.cfs_quota_us is -1, cgroup does not adhere to any CPU time restrictions
41
+ # https://docs.kernel.org/scheduler/sched-bwc.html#management
42
+ return nil if max <= 0
43
+ period = File . read ( "/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us" ) . to_f
44
+ max / period
45
+ end
46
+ end
47
+ end
14
48
end
15
49
16
50
class Inline
You can’t perform that action at this time.
0 commit comments