Skip to content

Commit

Permalink
Merge pull request #207 from westonganger/master
Browse files Browse the repository at this point in the history
Fix #149 - Utilize Etc.nprocessors in Ruby 2.2+
  • Loading branch information
grosser authored Jul 24, 2017
2 parents b4ef3d6 + 490549c commit 993bd07
Showing 2 changed files with 48 additions and 29 deletions.
66 changes: 37 additions & 29 deletions lib/parallel/processor_count.rb
Original file line number Diff line number Diff line change
@@ -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.respond_to?(:nprocessors)
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
11 changes: 11 additions & 0 deletions spec/parallel_spec.rb
Original file line number Diff line number Diff line change
@@ -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 == "constant"
end
end
end

describe ".physical_processor_count" do

0 comments on commit 993bd07

Please sign in to comment.