Sha256: f93d92c344ff5398ce31e09a679e86aa9c8b14773790f671df7d3d7db95c3006

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 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
        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

1 entries across 1 versions & 1 rubygems

Version Path
celluloid-essentials-0.20.1 lib/celluloid/internals/cpu_counter.rb