Module: Celluloid::Internals::CPUCounter

Defined in:
lib/celluloid/internals/cpu_counter.rb

Class Method Summary collapse

Class Method Details

.coresObject



5
6
7
# File 'lib/celluloid/internals/cpu_counter.rb', line 5

def cores
  @cores ||= count_cores
end

.count_coresObject



9
10
11
# File 'lib/celluloid/internals/cpu_counter.rb', line 9

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

.from_envObject



13
14
15
16
17
# File 'lib/celluloid/internals/cpu_counter.rb', line 13

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

.from_javaObject



30
31
32
33
# File 'lib/celluloid/internals/cpu_counter.rb', line 30

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

.from_procObject



35
36
37
38
# File 'lib/celluloid/internals/cpu_counter.rb', line 35

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

.from_result(result) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/celluloid/internals/cpu_counter.rb', line 52

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

.from_sysctlObject



47
48
49
50
# File 'lib/celluloid/internals/cpu_counter.rb', line 47

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

.from_sysdevObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/celluloid/internals/cpu_counter.rb', line 19

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

.from_win32oleObject



40
41
42
43
44
45
# File 'lib/celluloid/internals/cpu_counter.rb', line 40

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