|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
| 3 | +require "etc" |
| 4 | +require "rbconfig" |
| 5 | +require "io/wait" unless IO.method_defined?(:wait_readable) |
| 6 | + |
3 | 7 | module Bootsnap
|
4 | 8 | class CLI
|
5 | 9 | class WorkerPool
|
6 | 10 | class << self
|
7 | 11 | def create(size:, jobs:)
|
| 12 | + size ||= default_size |
8 | 13 | if size > 0 && Process.respond_to?(:fork)
|
9 | 14 | new(size: size, jobs: jobs)
|
10 | 15 | else
|
11 | 16 | Inline.new(jobs: jobs)
|
12 | 17 | end
|
13 | 18 | end
|
| 19 | + |
| 20 | + def default_size |
| 21 | + nprocessors = Etc.nprocessors |
| 22 | + size = [nprocessors, cpu_quota || nprocessors].min |
| 23 | + case size |
| 24 | + when 0, 1 |
| 25 | + 0 |
| 26 | + else |
| 27 | + if fork_defunct? |
| 28 | + $stderr.puts "warning: faulty fork(2) detected, probably in cross platform docker builds. " \ |
| 29 | + "Disabling parallel compilation." |
| 30 | + 0 |
| 31 | + else |
| 32 | + size |
| 33 | + end |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + def cpu_quota |
| 38 | + if RbConfig::CONFIG["target_os"].include?("linux") |
| 39 | + if File.exist?("/sys/fs/cgroup/cpu.max") |
| 40 | + # cgroups v2: https://docs.kernel.org/admin-guide/cgroup-v2.html#cpu-interface-files |
| 41 | + cpu_max = File.read("/sys/fs/cgroup/cpu.max") |
| 42 | + return nil if cpu_max.start_with?("max ") # no limit |
| 43 | + |
| 44 | + max, period = cpu_max.split.map(&:to_f) |
| 45 | + max / period |
| 46 | + elsif File.exist?("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us") |
| 47 | + # cgroups v1: https://kernel.googlesource.com/pub/scm/linux/kernel/git/glommer/memcg/+/cpu_stat/Documentation/cgroups/cpu.txt |
| 48 | + max = File.read("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_quota_us").to_i |
| 49 | + # If the cpu.cfs_quota_us is -1, cgroup does not adhere to any CPU time restrictions |
| 50 | + # https://docs.kernel.org/scheduler/sched-bwc.html#management |
| 51 | + return nil if max <= 0 |
| 52 | + |
| 53 | + period = File.read("/sys/fs/cgroup/cpu,cpuacct/cpu.cfs_period_us").to_f |
| 54 | + max / period |
| 55 | + end |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + def fork_defunct? |
| 60 | + return true unless ::Process.respond_to?(:fork) |
| 61 | + |
| 62 | + # Ref: https://github.com/Shopify/bootsnap/issues/495 |
| 63 | + # The second forked process will hang on some QEMU environments |
| 64 | + r, w = IO.pipe |
| 65 | + pids = 2.times.map do |
| 66 | + ::Process.fork do |
| 67 | + exit!(true) |
| 68 | + end |
| 69 | + end |
| 70 | + w.close |
| 71 | + r.wait_readable(1) # Wait at most 1s |
| 72 | + |
| 73 | + defunct = false |
| 74 | + |
| 75 | + pids.each do |pid| |
| 76 | + _pid, status = ::Process.wait2(pid, ::Process::WNOHANG) |
| 77 | + if status.nil? # Didn't exit in 1s |
| 78 | + defunct = true |
| 79 | + Process.kill(:KILL, pid) |
| 80 | + ::Process.wait2(pid) |
| 81 | + end |
| 82 | + end |
| 83 | + |
| 84 | + defunct |
| 85 | + end |
14 | 86 | end
|
15 | 87 |
|
16 | 88 | class Inline
|
|
0 commit comments