Sha256: d487a167c5d48aad0da40c50797aecb886dcf8458bfa84461f85f2e38fdfdb92

Contents?: true

Size: 1.57 KB

Versions: 3

Compression:

Stored size: 1.57 KB

Contents

module Celluloid
  module Internals
    module CPUCounter
      class << self
        def cores
          @cores ||= count_cores
        end

        private unless $CELLULOID_TEST

        def count_cores
          from_result(from_env || from_sysdev || from_java || from_proc || from_win32ole || from_sysctl) || 1
        end

        def from_env
          result = ENV["NUMBER_OF_PROCESSORS"]
          result if result && !result.empty?
        rescue
        end

        def from_sysdev
          ::IO.read("/sys/devices/system/cpu/present").split("-").last.to_i + 1
        rescue Errno::ENOENT
          begin
            result = Dir["/sys/devices/system/cpu/cpu*"].count { |n| n =~ /cpu\d+/ }
            result unless result.zero?
          rescue
          end
        rescue
        end

        def from_java
          Java::Java.lang.Runtime.getRuntime.availableProcessors if defined? Java::Java
        rescue
        end

        def from_proc
          File.read('/proc/cpuinfo').scan(/^processor\s*:/).size if File.exist?('/proc/cpuinfo')
        rescue
        end

        def from_win32ole
          require 'win32ole'
          WIN32OLE.connect("winmgmts://").ExecQuery("select * from Win32_ComputerSystem").NumberOfProcessors
        rescue LoadError
        rescue
        end

        def from_sysctl
          Integer `sysctl -n hw.ncpu 2>/dev/null`
        rescue
        end

        def from_result(result)
          if result
            i = Integer(result.to_s[/\d+/], 10)
            return i if i > 0
          end
        rescue
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
celluloid-essentials-0.20.2.1 lib/celluloid/internals/cpu_counter.rb
celluloid-essentials-0.20.2 lib/celluloid/internals/cpu_counter.rb
celluloid-essentials-0.20.1.1 lib/celluloid/internals/cpu_counter.rb