Skip to content

Commit 993bd07

Browse files
authored
Merge pull request #207 from westonganger/master
Fix #149 - Utilize Etc.nprocessors in Ruby 2.2+
2 parents b4ef3d6 + 490549c commit 993bd07

2 files changed

Lines changed: 48 additions & 29 deletions

File tree

lib/parallel/processor_count.rb

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
if RUBY_VERSION.to_f >= 2.2
2+
require 'etc'
3+
end
4+
15
module Parallel
26
module ProcessorCount
37
# Number of processors seen by the OS and used for process scheduling.
@@ -16,36 +20,40 @@ module ProcessorCount
1620
#
1721
def processor_count
1822
@processor_count ||= begin
19-
os_name = RbConfig::CONFIG["target_os"]
20-
if os_name =~ /mingw|mswin/
21-
require 'win32ole'
22-
result = WIN32OLE.connect("winmgmts://").ExecQuery(
23-
"select NumberOfLogicalProcessors from Win32_Processor")
24-
result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+)
25-
elsif File.readable?("/proc/cpuinfo")
26-
IO.read("/proc/cpuinfo").scan(/^processor/).size
27-
elsif File.executable?("/usr/bin/hwprefs")
28-
IO.popen("/usr/bin/hwprefs thread_count").read.to_i
29-
elsif File.executable?("/usr/sbin/psrinfo")
30-
IO.popen("/usr/sbin/psrinfo").read.scan(/^.*on-*line/).size
31-
elsif File.executable?("/usr/sbin/ioscan")
32-
IO.popen("/usr/sbin/ioscan -kC processor") do |out|
33-
out.read.scan(/^.*processor/).size
34-
end
35-
elsif File.executable?("/usr/sbin/pmcycles")
36-
IO.popen("/usr/sbin/pmcycles -m").read.count("\n")
37-
elsif File.executable?("/usr/sbin/lsdev")
38-
IO.popen("/usr/sbin/lsdev -Cc processor -S 1").read.count("\n")
39-
elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/i
40-
IO.popen("/usr/sbin/sysconf NPROC_ONLN").read.to_i
41-
elsif File.executable?("/usr/sbin/sysctl")
42-
IO.popen("/usr/sbin/sysctl -n hw.ncpu").read.to_i
43-
elsif File.executable?("/sbin/sysctl")
44-
IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i
23+
if defined?(Etc) && Etc.respond_to?(:nprocessors)
24+
Etc.nprocessors
4525
else
46-
$stderr.puts "Unknown platform: " + RbConfig::CONFIG["target_os"]
47-
$stderr.puts "Assuming 1 processor."
48-
1
26+
os_name = RbConfig::CONFIG["target_os"]
27+
if os_name =~ /mingw|mswin/
28+
require 'win32ole'
29+
result = WIN32OLE.connect("winmgmts://").ExecQuery(
30+
"select NumberOfLogicalProcessors from Win32_Processor")
31+
result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+)
32+
elsif File.readable?("/proc/cpuinfo")
33+
IO.read("/proc/cpuinfo").scan(/^processor/).size
34+
elsif File.executable?("/usr/bin/hwprefs")
35+
IO.popen("/usr/bin/hwprefs thread_count").read.to_i
36+
elsif File.executable?("/usr/sbin/psrinfo")
37+
IO.popen("/usr/sbin/psrinfo").read.scan(/^.*on-*line/).size
38+
elsif File.executable?("/usr/sbin/ioscan")
39+
IO.popen("/usr/sbin/ioscan -kC processor") do |out|
40+
out.read.scan(/^.*processor/).size
41+
end
42+
elsif File.executable?("/usr/sbin/pmcycles")
43+
IO.popen("/usr/sbin/pmcycles -m").read.count("\n")
44+
elsif File.executable?("/usr/sbin/lsdev")
45+
IO.popen("/usr/sbin/lsdev -Cc processor -S 1").read.count("\n")
46+
elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /irix/i
47+
IO.popen("/usr/sbin/sysconf NPROC_ONLN").read.to_i
48+
elsif File.executable?("/usr/sbin/sysctl")
49+
IO.popen("/usr/sbin/sysctl -n hw.ncpu").read.to_i
50+
elsif File.executable?("/sbin/sysctl")
51+
IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i
52+
else
53+
$stderr.puts "Unknown platform: " + RbConfig::CONFIG["target_os"]
54+
$stderr.puts "Assuming 1 processor."
55+
1
56+
end
4957
end
5058
end
5159
end

spec/parallel_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@ def execute_start_and_kill(command, amount, signal='INT')
4242
(1..999).should include(Parallel.processor_count)
4343
end
4444
end
45+
46+
if RUBY_VERSION.to_f >= 2.2
47+
it 'uses Etc.nprocessors in Ruby 2.2+' do
48+
defined?(Etc).should == "constant"
49+
Etc.respond_to?(:nprocessors).should == true
50+
end
51+
else
52+
it 'doesnt use Etc.nprocessors in Ruby 2.1 and below' do
53+
defined?(Etc).should == "constant"
54+
end
55+
end
4556
end
4657

4758
describe ".physical_processor_count" do

0 commit comments

Comments
 (0)