Class: MoolCpu
- Inherits:
-
Object
- Object
- MoolCpu
- 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
-
#cores ⇒ Object
readonly
Returns the value of attribute cores.
-
#cpu_name ⇒ Object
readonly
Returns the value of attribute cpu_name.
-
#gnice ⇒ Object
readonly
Returns the value of attribute gnice.
-
#guest ⇒ Object
readonly
Returns the value of attribute guest.
-
#idle ⇒ Object
readonly
Returns the value of attribute idle.
-
#iowait ⇒ Object
readonly
Returns the value of attribute iowait.
-
#irq ⇒ Object
readonly
Returns the value of attribute irq.
-
#model_name ⇒ Object
readonly
Returns the value of attribute model_name.
-
#nice ⇒ Object
readonly
Returns the value of attribute nice.
-
#soft ⇒ Object
readonly
Returns the value of attribute soft.
-
#steal ⇒ Object
readonly
Returns the value of attribute steal.
-
#sys ⇒ Object
readonly
Returns the value of attribute sys.
-
#total ⇒ Object
readonly
Returns the value of attribute total.
-
#usr ⇒ Object
readonly
Returns the value of attribute usr.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(process_number, opt = {}) ⇒ MoolCpu
constructor
[“all”, “1”, “2”].
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
#cores ⇒ Object (readonly)
Returns the value of attribute cores.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def cores @cores end |
#cpu_name ⇒ Object (readonly)
Returns the value of attribute cpu_name.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def cpu_name @cpu_name end |
#gnice ⇒ Object (readonly)
Returns the value of attribute gnice.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def gnice @gnice end |
#guest ⇒ Object (readonly)
Returns the value of attribute guest.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def guest @guest end |
#idle ⇒ Object (readonly)
Returns the value of attribute idle.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def idle @idle end |
#iowait ⇒ Object (readonly)
Returns the value of attribute iowait.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def iowait @iowait end |
#irq ⇒ Object (readonly)
Returns the value of attribute irq.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def irq @irq end |
#model_name ⇒ Object (readonly)
Returns the value of attribute model_name.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def model_name @model_name end |
#nice ⇒ Object (readonly)
Returns the value of attribute nice.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def nice @nice end |
#soft ⇒ Object (readonly)
Returns the value of attribute soft.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def soft @soft end |
#steal ⇒ Object (readonly)
Returns the value of attribute steal.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def steal @steal end |
#sys ⇒ Object (readonly)
Returns the value of attribute sys.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def sys @sys end |
#total ⇒ Object (readonly)
Returns the value of attribute total.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def total @total end |
#usr ⇒ Object (readonly)
Returns the value of attribute usr.
5 6 7 |
# File 'lib/mool/cpu.rb', line 5 def usr @usr end |
Class Method Details
.all ⇒ Object
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_info ⇒ Object
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 |
.processors ⇒ Object
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 |