Class: Mool::Cpu

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#attrs

Constructor Details

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

“all”, “1”, “2”


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/mool/cpu.rb', line 20

def initialize(process_number, opt={})
  result = Mool::Cpu.processors

  unless result.include?(process_number.to_s)
    raise "Cpu name incorrect!. Posible values: #{result.join(', ')}"
  end

  result = opt.empty? ? Mool::Cpu.cpuinfo[process_number.to_s] : opt
  @cpu_name = "cpu_#{process_number}"
  @id         = process_number
  @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].sum
end

Instance Attribute Details

#coresObject

Returns the value of attribute cores.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def cores
  @cores
end

#cpu_nameObject

Returns the value of attribute cpu_name.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def cpu_name
  @cpu_name
end

#gniceObject

Returns the value of attribute gnice.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def gnice
  @gnice
end

#guestObject

Returns the value of attribute guest.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def guest
  @guest
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def id
  @id
end

#idleObject

Returns the value of attribute idle.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def idle
  @idle
end

#iowaitObject

Returns the value of attribute iowait.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def iowait
  @iowait
end

#irqObject

Returns the value of attribute irq.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def irq
  @irq
end

#model_nameObject

Returns the value of attribute model_name.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def model_name
  @model_name
end

#niceObject

Returns the value of attribute nice.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def nice
  @nice
end

#softObject

Returns the value of attribute soft.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def soft
  @soft
end

#stealObject

Returns the value of attribute steal.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def steal
  @steal
end

#sysObject

Returns the value of attribute sys.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def sys
  @sys
end

#totalObject

Returns the value of attribute total.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def total
  @total
end

#usrObject

Returns the value of attribute usr.



3
4
5
# File 'lib/mool/cpu.rb', line 3

def usr
  @usr
end

Class Method Details

.allObject



82
83
84
# File 'lib/mool/cpu.rb', line 82

def self.all
  Mool::Cpu.cpuinfo.map { |key, value| Mool::Cpu.new(key, value) }
end

.cpuinfoObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/mool/cpu.rb', line 52

def self.cpuinfo
  cpu_info = {}

  mpstat = Mool::Command.mpstat.split("\n\n")[2].split("\n").map do |i|
    i.gsub(/^\S+:/, '').strip.split(/\s+/)
  end

  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

  Mool::Command.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



78
79
80
# File 'lib/mool/cpu.rb', line 78

def self.processors
  Mool::Command.cpuinfo.scan(/processor\t*: (\d+)/).flatten + ['all']
end