Class: Rake::CpuCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/rake/cpu_counter.rb

Overview

Based on a script at:

http://stackoverflow.com/questions/891537/ruby-detect-number-of-cpus-installed

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.countObject

:nodoc: all



11
12
13
# File 'lib/rake/cpu_counter.rb', line 11

def self.count
  new.count_with_default
end

Instance Method Details

#countObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rake/cpu_counter.rb', line 21

def count
  if defined?(Java::Java)
    count_via_java_runtime
  else
    case RbConfig::CONFIG['host_os']
    when /darwin9/
      count_via_hwprefs_cpu_count
    when /darwin/
      count_via_hwprefs_thread_count || count_via_sysctl
    when /linux/
      count_via_cpuinfo
    when /bsd/
      count_via_sysctl
    when /mswin|mingw/
      count_via_win32
    else
      # Try everything
      count_via_win32 ||
        count_via_sysctl ||
        count_via_hwprefs_thread_count ||
        count_via_hwprefs_cpu_count
    end
  end
end

#count_via_cpuinfoObject



61
62
63
64
65
# File 'lib/rake/cpu_counter.rb', line 61

def count_via_cpuinfo
  open('/proc/cpuinfo') { |f| f.readlines }.grep(/processor/).size
rescue StandardError
  nil
end

#count_via_hwprefs_cpu_countObject



71
72
73
# File 'lib/rake/cpu_counter.rb', line 71

def count_via_hwprefs_cpu_count
  run 'hwprefs', 'cpu_count'
end

#count_via_hwprefs_thread_countObject



67
68
69
# File 'lib/rake/cpu_counter.rb', line 67

def count_via_hwprefs_thread_count
  run 'hwprefs', 'thread_count'
end

#count_via_java_runtimeObject



46
47
48
49
50
# File 'lib/rake/cpu_counter.rb', line 46

def count_via_java_runtime
  Java::Java.lang.Runtime.getRuntime.availableProcessors
rescue StandardError
  nil
end

#count_via_sysctlObject



75
76
77
# File 'lib/rake/cpu_counter.rb', line 75

def count_via_sysctl
  run 'sysctl', '-n', 'hw.ncpu'
end

#count_via_win32Object



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

def count_via_win32
  require 'win32ole'
  wmi = WIN32OLE.connect("winmgmts://")
  cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # TODO count hyper-threaded in this
  cpu.to_enum.first.NumberOfCores
rescue StandardError, LoadError
  nil
end

#count_with_default(default = 4) ⇒ Object



15
16
17
18
19
# File 'lib/rake/cpu_counter.rb', line 15

def count_with_default(default=4)
  count || default
rescue StandardError
  default
end

#in_path_command(command) ⇒ Object



103
104
105
106
107
# File 'lib/rake/cpu_counter.rb', line 103

def in_path_command(command)
  Open3.popen3 'which', command do |_, out,|
    out.eof? ? nil : command
  end
end

#look_for_command(dir, command) ⇒ Object



98
99
100
101
# File 'lib/rake/cpu_counter.rb', line 98

def look_for_command(dir, command)
  path = File.join(dir, command)
  File.exist?(path) ? path : nil
end

#resolve_command(command) ⇒ Object



92
93
94
95
96
# File 'lib/rake/cpu_counter.rb', line 92

def resolve_command(command)
  look_for_command("/usr/sbin", command) ||
    look_for_command("/sbin", command) ||
    in_path_command(command)
end

#run(command, *args) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rake/cpu_counter.rb', line 79

def run(command, *args)
  cmd = resolve_command(command)
  if cmd
    Open3.popen3 cmd, *args do |inn, out, err,|
      inn.close
      err.read
      out.read.to_i
    end
  else
    nil
  end
end