class Concurrent::Utility::ProcessorCounter

@!visibility private

def compute_physical_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
        when /linux/
          cores = {} # unique physical ID / core ID combinations
          phy   = 0
          IO.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 if not 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
rescue
  return 1
end

def compute_processor_count

def compute_processor_count
  if Concurrent.on_jruby?
    java.lang.Runtime.getRuntime.availableProcessors
  else
    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") && (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.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", &:read).scan(/^.*processor/).size
    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
      # TODO (pitr-ch 05-Nov-2016): warn about failures
      1
    end
  end
rescue
  return 1
end

def initialize

def initialize
  @processor_count          = Delay.new { compute_processor_count }
  @physical_processor_count = Delay.new { compute_physical_processor_count }
end

def physical_processor_count

Other tags:
    See: http://linux.die.net/man/8/sysctl -
    See: http://www.unix.com/man-page/osx/1/HWPREFS/ -
    See: http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx -
    See: https://github.com/grosser/parallel/blob/4fc8b89d08c7091fe0419ca8fba1ec3ce5a8d185/lib/parallel.rb -

Returns:
  • (Integer) - number physical processor cores on the current system
def physical_processor_count
  @physical_processor_count.value
end

def processor_count

Other tags:
    See: http://msdn.microsoft.com/en-us/library/aa394373(v=vs.85).aspx -
    See: http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#availableProcessors() -
    See: https://github.com/grosser/parallel/blob/4fc8b89d08c7091fe0419ca8fba1ec3ce5a8d185/lib/parallel.rb -

Returns:
  • (Integer) - number of processors seen by the OS or Java runtime
def processor_count
  @processor_count.value
end