From 86cf4d1c4109c109fae1ce4949f7f5dddc0c9404 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Mon, 24 Jul 2017 11:35:33 -0700 Subject: [PATCH 1/3] fix #149 --- lib/parallel/processor_count.rb | 66 ++++++++++++++++++--------------- spec/parallel_spec.rb | 11 ++++++ 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/lib/parallel/processor_count.rb b/lib/parallel/processor_count.rb index 3c5a6f19..c307c0d2 100644 --- a/lib/parallel/processor_count.rb +++ b/lib/parallel/processor_count.rb @@ -1,3 +1,7 @@ +if RUBY_VERSION.to_f >= 2.2 + require 'etc' +end + module Parallel module ProcessorCount # Number of processors seen by the OS and used for process scheduling. @@ -16,36 +20,40 @@ module ProcessorCount # def processor_count @processor_count ||= begin - os_name = RbConfig::CONFIG["target_os"] - if os_name =~ /mingw|mswin/ - require 'win32ole' - result = WIN32OLE.connect("winmgmts://").ExecQuery( - "select NumberOfLogicalProcessors from Win32_Processor") - result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+) - 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") - 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 - elsif File.executable?("/usr/sbin/pmcycles") - 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") - elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/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 - elsif File.executable?("/sbin/sysctl") - IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i + if defined?(Etc) + Etc.nprocessors else - $stderr.puts "Unknown platform: " + RbConfig::CONFIG["target_os"] - $stderr.puts "Assuming 1 processor." - 1 + os_name = RbConfig::CONFIG["target_os"] + if os_name =~ /mingw|mswin/ + require 'win32ole' + result = WIN32OLE.connect("winmgmts://").ExecQuery( + "select NumberOfLogicalProcessors from Win32_Processor") + result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+) + 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") + 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 + elsif File.executable?("/usr/sbin/pmcycles") + 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") + elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/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 + elsif File.executable?("/sbin/sysctl") + IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i + else + $stderr.puts "Unknown platform: " + RbConfig::CONFIG["target_os"] + $stderr.puts "Assuming 1 processor." + 1 + end end end end diff --git a/spec/parallel_spec.rb b/spec/parallel_spec.rb index 277d4b4d..d11e6736 100644 --- a/spec/parallel_spec.rb +++ b/spec/parallel_spec.rb @@ -42,6 +42,17 @@ def execute_start_and_kill(command, amount, signal='INT') (1..999).should include(Parallel.processor_count) end end + + if RUBY_VERSION.to_f >= 2.2 + it 'uses Etc.nprocessors in Ruby 2.2+' do + defined?(Etc).should == "constant" + Etc.respond_to?(:nprocessors).should == true + end + else + it 'doesnt use Etc.nprocessors in Ruby 2.1 and below' do + defined?(Etc).should == nil + end + end end describe ".physical_processor_count" do From ae044e58dfcceeb89344757e4777c6b36cfb3493 Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Mon, 24 Jul 2017 11:40:06 -0700 Subject: [PATCH 2/3] fix #149 --- lib/parallel/processor_count.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parallel/processor_count.rb b/lib/parallel/processor_count.rb index c307c0d2..3ac19935 100644 --- a/lib/parallel/processor_count.rb +++ b/lib/parallel/processor_count.rb @@ -20,7 +20,7 @@ module ProcessorCount # def processor_count @processor_count ||= begin - if defined?(Etc) + if defined?(Etc) && Etc.respond_to?(:nprocessors) Etc.nprocessors else os_name = RbConfig::CONFIG["target_os"] From 490549cccf0cbf4a18dad206a66a9a96cb7b82bc Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Mon, 24 Jul 2017 13:22:41 -0700 Subject: [PATCH 3/3] fix #149 --- spec/parallel_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/parallel_spec.rb b/spec/parallel_spec.rb index d11e6736..021e4481 100644 --- a/spec/parallel_spec.rb +++ b/spec/parallel_spec.rb @@ -50,7 +50,7 @@ def execute_start_and_kill(command, amount, signal='INT') end else it 'doesnt use Etc.nprocessors in Ruby 2.1 and below' do - defined?(Etc).should == nil + defined?(Etc).should == "constant" end end end