|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
| 3 | +require 'etc' |
3 | 4 | require 'pathname' |
4 | 5 | require 'overcommit/os' |
5 | 6 | require 'overcommit/subprocess' |
@@ -150,6 +151,9 @@ def parent_command |
150 | 151 | else |
151 | 152 | `ps -ocommand= -p #{Process.ppid}`.chomp |
152 | 153 | end |
| 154 | + rescue Errno::EPERM, Errno::ENOENT |
| 155 | + # Process information may not be available, such as inside sandboxed environments |
| 156 | + nil |
153 | 157 | end |
154 | 158 |
|
155 | 159 | # Execute a command in a subprocess, capturing exit status and output from |
@@ -212,40 +216,8 @@ def execute_in_background(args) |
212 | 216 | end |
213 | 217 |
|
214 | 218 | # Return the number of processors used by the OS for process scheduling. |
215 | | - # |
216 | | - # @see https://github.com/grosser/parallel/blob/v1.6.1/lib/parallel/processor_count.rb#L17-L51 |
217 | | - def processor_count # rubocop:disable all |
218 | | - @processor_count ||= |
219 | | - begin |
220 | | - if Overcommit::OS.windows? |
221 | | - require 'win32ole' |
222 | | - result = WIN32OLE.connect('winmgmts://').ExecQuery( |
223 | | - 'select NumberOfLogicalProcessors from Win32_Processor' |
224 | | - ) |
225 | | - result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+) |
226 | | - elsif File.readable?('/proc/cpuinfo') |
227 | | - IO.read('/proc/cpuinfo').scan(/^processor/).size |
228 | | - elsif File.executable?('/usr/bin/hwprefs') |
229 | | - IO.popen('/usr/bin/hwprefs thread_count').read.to_i |
230 | | - elsif File.executable?('/usr/sbin/psrinfo') |
231 | | - IO.popen('/usr/sbin/psrinfo').read.scan(/^.*on-*line/).size |
232 | | - elsif File.executable?('/usr/sbin/ioscan') |
233 | | - IO.popen('/usr/sbin/ioscan -kC processor') do |out| |
234 | | - out.read.scan(/^.*processor/).size |
235 | | - end |
236 | | - elsif File.executable?('/usr/sbin/pmcycles') |
237 | | - IO.popen('/usr/sbin/pmcycles -m').read.count("\n") |
238 | | - elsif File.executable?('/usr/sbin/lsdev') |
239 | | - IO.popen('/usr/sbin/lsdev -Cc processor -S 1').read.count("\n") |
240 | | - elsif File.executable?('/usr/sbin/sysctl') |
241 | | - IO.popen('/usr/sbin/sysctl -n hw.ncpu').read.to_i |
242 | | - elsif File.executable?('/sbin/sysctl') |
243 | | - IO.popen('/sbin/sysctl -n hw.ncpu').read.to_i |
244 | | - else |
245 | | - # Unknown platform; assume 1 processor |
246 | | - 1 |
247 | | - end |
248 | | - end |
| 219 | + def processor_count |
| 220 | + @processor_count ||= Etc.nprocessors |
249 | 221 | end |
250 | 222 |
|
251 | 223 | # Calls a block of code with a modified set of environment variables, |
|
0 commit comments