diff --git a/lib/parallel.rb b/lib/parallel.rb index c7f2bf6..b22ef29 100644 --- a/lib/parallel.rb +++ b/lib/parallel.rb @@ -1,11 +1,8 @@ # frozen_string_literal: true require 'rbconfig' require 'parallel/version' -require 'parallel/processor_count' module Parallel - extend ProcessorCount - Stop = Object.new.freeze class DeadWorker < StandardError @@ -307,6 +304,45 @@ def flat_map(*args, &block) map(*args, &block).flatten(1) end + # Number of physical processor cores on the current system. + def physical_processor_count + @physical_processor_count ||= begin + ppc = + case RbConfig::CONFIG["target_os"] + when /darwin[12]/ + IO.popen("/usr/sbin/sysctl -n hw.physicalcpu").read.to_i + when /linux/ + cores = {} # unique physical ID / core ID combinations + phy = 0 + File.read("/proc/cpuinfo").scan(/^physical id.*|^core id.*/) do |ln| + if ln.start_with?("physical") + phy = ln[/\d+/] + elsif ln.start_with?("core") + cid = "#{phy}:#{ln[/\d+/]}" + cores[cid] = true unless cores[cid] + end + end + cores.count + when /mswin|mingw/ + require 'win32ole' + result_set = WIN32OLE.connect("winmgmts://").ExecQuery( + "select NumberOfCores from Win32_Processor" + ) + result_set.to_enum.collect(&:NumberOfCores).reduce(:+) + else + processor_count + end + # fall back to logical count if physical info is invalid + ppc > 0 ? ppc : processor_count + end + end + + # Number of processors seen by the OS, used for process scheduling + def processor_count + require 'etc' + @processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Etc.nprocessors) + end + def worker_number Thread.current[:parallel_worker_number] end diff --git a/lib/parallel/processor_count.rb b/lib/parallel/processor_count.rb deleted file mode 100644 index 3b5ea61..0000000 --- a/lib/parallel/processor_count.rb +++ /dev/null @@ -1,44 +0,0 @@ -# frozen_string_literal: true -module Parallel - # TODO: inline this method into parallel.rb and kill physical_processor_count in next major release - module ProcessorCount - # Number of processors seen by the OS, used for process scheduling - def processor_count - require 'etc' - @processor_count ||= Integer(ENV['PARALLEL_PROCESSOR_COUNT'] || Etc.nprocessors) - end - - # Number of physical processor cores on the current system. - def physical_processor_count - @physical_processor_count ||= begin - ppc = - case RbConfig::CONFIG["target_os"] - when /darwin[12]/ - IO.popen("/usr/sbin/sysctl -n hw.physicalcpu").read.to_i - when /linux/ - cores = {} # unique physical ID / core ID combinations - phy = 0 - File.read("/proc/cpuinfo").scan(/^physical id.*|^core id.*/) do |ln| - if ln.start_with?("physical") - phy = ln[/\d+/] - elsif ln.start_with?("core") - cid = "#{phy}:#{ln[/\d+/]}" - cores[cid] = true unless cores[cid] - end - end - cores.count - when /mswin|mingw/ - require 'win32ole' - result_set = WIN32OLE.connect("winmgmts://").ExecQuery( - "select NumberOfCores from Win32_Processor" - ) - result_set.to_enum.collect(&:NumberOfCores).reduce(:+) - else - processor_count - end - # fall back to logical count if physical info is invalid - ppc > 0 ? ppc : processor_count - end - end - end -end