Class: MoolCpu

Inherits:
Object
  • Object
show all
Defined in:
lib/mool/cpu.rb

Constant Summary collapse

PATH_PROC_CPUINFO =
"/proc/cpuinfo"
PROCESSORS =
File.read(PATH_PROC_CPUINFO).scan(/processor\t*: (\d+)/).flatten + ["all"]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(process_number, opt = {}) ⇒ MoolCpu

“all”, “1”, “2”


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/mool/cpu.rb', line 8

def initialize(process_number, opt={})
  raise "Cpu name incorrect!. Posible values: #{MoolCpu::PROCESSORS.join(",")}" unless MoolCpu::PROCESSORS.include?(process_number.to_s)
  result = opt.empty? ? MoolCpu.cpu_info[process_number.to_s] : opt
  @cpu_name = "cpu_#{process_number.to_s}"
  @model_name = result["model_name"]
  @cores      = result["cpu_cores"].to_i
  @usr        = result["%usr"].to_f
  @nice       = result["%nice"].to_f
  @sys        = result["%sys"].to_f # This is kernel %
  @iowait     = result["%iowait"].to_f
  @irq        = result["%irq"].to_f
  @soft       = result["%soft"].to_f
  @steal      = result["%steal"].to_f
  @guest      = result["%guest"].to_f
  @gnice      = result["%gnice"].to_f
  @idle       = result["%idle"].to_f
  @total      = @usr + @nice + @sys + @iowait + @irq + @soft + @steal + @guest
end

Instance Attribute Details

#coresObject (readonly)

Returns the value of attribute cores.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def cores
  @cores
end

#cpu_nameObject (readonly)

Returns the value of attribute cpu_name.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def cpu_name
  @cpu_name
end

#gniceObject (readonly)

Returns the value of attribute gnice.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def gnice
  @gnice
end

#guestObject (readonly)

Returns the value of attribute guest.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def guest
  @guest
end

#idleObject (readonly)

Returns the value of attribute idle.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def idle
  @idle
end

#iowaitObject (readonly)

Returns the value of attribute iowait.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def iowait
  @iowait
end

#irqObject (readonly)

Returns the value of attribute irq.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def irq
  @irq
end

#model_nameObject (readonly)

Returns the value of attribute model_name.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def model_name
  @model_name
end

#niceObject (readonly)

Returns the value of attribute nice.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def nice
  @nice
end

#softObject (readonly)

Returns the value of attribute soft.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def soft
  @soft
end

#stealObject (readonly)

Returns the value of attribute steal.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def steal
  @steal
end

#sysObject (readonly)

Returns the value of attribute sys.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def sys
  @sys
end

#totalObject (readonly)

Returns the value of attribute total.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def total
  @total
end

#usrObject (readonly)

Returns the value of attribute usr.



5
6
7
# File 'lib/mool/cpu.rb', line 5

def usr
  @usr
end

Class Method Details

.allObject



53
54
55
# File 'lib/mool/cpu.rb', line 53

def self.all
  MoolCpu.cpu_info.map{ |key, value| MoolCpu.new(key, value) }
end

.cpu_infoObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mool/cpu.rb', line 27

def self.cpu_info
  cpu_info = {}

  mpstat = File.read("|mpstat -P ALL 1 1").split("\n\n")[2].split("\n").map{|i| i.gsub(/^\S+:/, '').strip.split(/\s+/) }
  mpstat_vars = mpstat.shift
  mpstat_vars.shift
  mpstat.each do |data|
    res = {}
    core_name = data.shift
    data.each_with_index { |d, i| res.merge!(mpstat_vars[i] => d) }
    cpu_info.merge!(core_name => res)
  end

  File.read(PATH_PROC_CPUINFO).gsub(/([^\n])\n([^\n])/, '\1 \2').scan(/processor\t*: (\d+).*model name\t*: (.*) stepping.*cpu cores\t*: (\d+)/).each do |v|
    cpu_info[v[0]]["model_name"] = v[1]
    cpu_info[v[0]]["cpu_cores"] = v[2]
  end

  cpu_info
end

.processorsObject



48
49
50
# File 'lib/mool/cpu.rb', line 48

def self.processors
  File.read(PATH_PROC_CPUINFO).scan(/processor\t*: (\d+)/).flatten + ["all"]
end