Skip to content

Commit 0371ed4

Browse files
graaffpitr-ch
authored andcommitted
Avoid forking for processor_count if possible
This re-orders the checks for the processor count so that forking is only done when needed. alpha-linux does not support the processor entry in /proc/cpuinfo so we must catch that case and fall back to using nproc which does work on alpha. This is a rework of #549 based on suggestions made in #576
1 parent 72b7d71 commit 0371ed4

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

lib/concurrent/utility/processor_counter.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def compute_processor_count
8585
result = WIN32OLE.connect("winmgmts://").ExecQuery(
8686
"select NumberOfLogicalProcessors from Win32_Processor")
8787
result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+)
88+
elsif File.readable?("/proc/cpuinfo") && (cpuinfo_count = IO.read("/proc/cpuinfo").scan(/^processor/).size) > 0
89+
cpuinfo_count
8890
elsif File.executable?("/usr/bin/nproc")
8991
IO.popen("/usr/bin/nproc --all", &:read).to_i
90-
elsif File.readable?("/proc/cpuinfo")
91-
IO.read("/proc/cpuinfo").scan(/^processor/).size
9292
elsif File.executable?("/usr/bin/hwprefs")
9393
IO.popen("/usr/bin/hwprefs thread_count", &:read).to_i
9494
elsif File.executable?("/usr/sbin/psrinfo")

0 commit comments

Comments
 (0)