From 72b7d7126fc2dbd3738dcdbe60aaa1bc33f3609b Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Fri, 23 Sep 2016 06:59:44 +0930 Subject: [PATCH 1/2] Use block form of IO.popen to prevent zombies --- lib/concurrent/utility/processor_counter.rb | 22 ++++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/concurrent/utility/processor_counter.rb b/lib/concurrent/utility/processor_counter.rb index e82c0a6ad..fa6616ad1 100644 --- a/lib/concurrent/utility/processor_counter.rb +++ b/lib/concurrent/utility/processor_counter.rb @@ -86,27 +86,25 @@ def compute_processor_count "select NumberOfLogicalProcessors from Win32_Processor") result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+) elsif File.executable?("/usr/bin/nproc") - IO.popen("/usr/bin/nproc --all").read.to_i + IO.popen("/usr/bin/nproc --all", &:read).to_i elsif File.readable?("/proc/cpuinfo") IO.read("/proc/cpuinfo").scan(/^processor/).size elsif File.executable?("/usr/bin/hwprefs") - IO.popen("/usr/bin/hwprefs thread_count").read.to_i + IO.popen("/usr/bin/hwprefs thread_count", &:read).to_i elsif File.executable?("/usr/sbin/psrinfo") - IO.popen("/usr/sbin/psrinfo").read.scan(/^.*on-*line/).size + IO.popen("/usr/sbin/psrinfo", &:read).scan(/^.*on-*line/).size elsif File.executable?("/usr/sbin/ioscan") - IO.popen("/usr/sbin/ioscan -kC processor") do |out| - out.read.scan(/^.*processor/).size - end + IO.popen("/usr/sbin/ioscan -kC processor", &:read).scan(/^.*processor/).size elsif File.executable?("/usr/sbin/pmcycles") - IO.popen("/usr/sbin/pmcycles -m").read.count("\n") + IO.popen("/usr/sbin/pmcycles -m", &:read).count("\n") elsif File.executable?("/usr/sbin/lsdev") - IO.popen("/usr/sbin/lsdev -Cc processor -S 1").read.count("\n") + IO.popen("/usr/sbin/lsdev -Cc processor -S 1", &:read).count("\n") elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/i - IO.popen("/usr/sbin/sysconf NPROC_ONLN").read.to_i + IO.popen("/usr/sbin/sysconf NPROC_ONLN", &:read).to_i elsif File.executable?("/usr/sbin/sysctl") - IO.popen("/usr/sbin/sysctl -n hw.ncpu").read.to_i + IO.popen("/usr/sbin/sysctl -n hw.ncpu", &:read).to_i elsif File.executable?("/sbin/sysctl") - IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i + IO.popen("/sbin/sysctl -n hw.ncpu", &:read).to_i else 1 end @@ -118,7 +116,7 @@ def compute_processor_count def compute_physical_processor_count ppc = case RbConfig::CONFIG["target_os"] when /darwin1/ - IO.popen("/usr/sbin/sysctl -n hw.physicalcpu").read.to_i + IO.popen("/usr/sbin/sysctl -n hw.physicalcpu", &:read).to_i when /linux/ cores = {} # unique physical ID / core ID combinations phy = 0 From 0371ed4ad5c910fdcf569c8e85d7de96abf5903e Mon Sep 17 00:00:00 2001 From: Hans de Graaff Date: Mon, 7 Nov 2016 20:50:43 +0100 Subject: [PATCH 2/2] 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 --- lib/concurrent/utility/processor_counter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/concurrent/utility/processor_counter.rb b/lib/concurrent/utility/processor_counter.rb index fa6616ad1..aa310ea30 100644 --- a/lib/concurrent/utility/processor_counter.rb +++ b/lib/concurrent/utility/processor_counter.rb @@ -85,10 +85,10 @@ def compute_processor_count result = WIN32OLE.connect("winmgmts://").ExecQuery( "select NumberOfLogicalProcessors from Win32_Processor") result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+) + elsif File.readable?("/proc/cpuinfo") && (cpuinfo_count = IO.read("/proc/cpuinfo").scan(/^processor/).size) > 0 + cpuinfo_count elsif File.executable?("/usr/bin/nproc") IO.popen("/usr/bin/nproc --all", &:read).to_i - elsif File.readable?("/proc/cpuinfo") - IO.read("/proc/cpuinfo").scan(/^processor/).size elsif File.executable?("/usr/bin/hwprefs") IO.popen("/usr/bin/hwprefs thread_count", &:read).to_i elsif File.executable?("/usr/sbin/psrinfo")