Class: CzSystemInfo::CPU
- Inherits:
-
Object
- Object
- CzSystemInfo::CPU
- Defined in:
- lib/cz_system_info/cpu.rb
Overview
The CPU class encapsulates information about physical CPUs on your system.
Class Method Summary collapse
-
.architecture ⇒ Object
Return the architecture of the CPU.
-
.cpu_stats ⇒ Object
Returns a hash of arrays that contains an array of the following information (as of 2.6.33), respectively:.
-
.freq ⇒ Object
Returns an integer indicating the speed of the CPU.
-
.load_avg ⇒ Object
Returns a 3 element Array corresponding to the 1, 5 and 15 minute load average for the system.
-
.model ⇒ Object
Returns a string indicating the CPU model.
-
.num_cpu ⇒ Object
Return the total number of logical CPU on the system.
-
.processors ⇒ Object
In block form, yields a CPUStruct for each CPU on the system.
- .respond_to_missing?(method, _private_methods = false) ⇒ Boolean
Class Method Details
.architecture ⇒ Object
Return the architecture of the CPU.
81 82 83 84 85 86 87 88 |
# File 'lib/cz_system_info/cpu.rb', line 81 def self.architecture case CPU_ARRAY.first['cpu_family'] when '3' 'x86' when '6' 'x86_64' end end |
.cpu_stats ⇒ Object
Returns a hash of arrays that contains an array of the following information (as of 2.6.33), respectively:
-
user: time spent in user mode.
-
nice: time spent in user mode with low priority.
-
system: time spent in system mode.
-
idle: time spent in the idle task.
-
iowait: time waiting for IO to complete.
-
irq: time servicing interrupts.
-
softirq: time servicing softirqs.
-
steal: time spent in other operating systems when running in a virtualized environment.
-
guest: time spent running a virtual CPU for guest operating systems.
-
guest_nice: time spent running a niced guest, i.e a virtual CPU for guest operating systems.
Note that older kernels may not necessarily include some of these fields.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/cz_system_info/cpu.rb', line 144 def self.cpu_stats cpu_stat_file = '/proc/stat' hash = {} # Hash needed for multi-cpu systems lines = File.readlines(cpu_stat_file) lines.each_with_index do |line, i| array = line.split break unless array[0] =~ /cpu/ # 'cpu' entries always on top # Some machines list a 'cpu' and a 'cpu0'. In this case only # return values for the numbered cpu entry. if lines[i].split[0] == 'cpu' && lines[i + 1].split[0] =~ /cpu\d/ next end vals = array[1..-1].map{ |e| e.to_i / 100 } # 100 jiffies/sec. hash[array[0]] = vals end hash end |
.freq ⇒ Object
Returns an integer indicating the speed of the CPU.
98 99 100 |
# File 'lib/cz_system_info/cpu.rb', line 98 def self.freq CPU_ARRAY.first['cpu_mhz'].to_f.round end |
.load_avg ⇒ Object
Returns a 3 element Array corresponding to the 1, 5 and 15 minute load average for the system.
123 124 125 126 |
# File 'lib/cz_system_info/cpu.rb', line 123 def self.load_avg load_avg_file = '/proc/loadavg' File.readlines(load_avg_file).first.split[0..2].map(&:to_f) end |
.model ⇒ Object
Returns a string indicating the CPU model.
92 93 94 |
# File 'lib/cz_system_info/cpu.rb', line 92 def self.model CPU_ARRAY.first['model_name'] end |
.num_cpu ⇒ Object
Return the total number of logical CPU on the system.
75 76 77 |
# File 'lib/cz_system_info/cpu.rb', line 75 def self.num_cpu CPU_ARRAY.size end |
.processors ⇒ Object
In block form, yields a CPUStruct for each CPU on the system. In non-block form, returns an Array of CPUStruct’s.
The exact members of the struct vary on Linux systems.
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/cz_system_info/cpu.rb', line 59 def self.processors array = [] CPU_ARRAY.each do |hash| struct = CPUStruct.new struct.members.each{ |m| struct.send("#{m}=", hash[m.to_s]) } if block_given? yield struct else array << struct end end array unless block_given? end |
.respond_to_missing?(method, _private_methods = false) ⇒ Boolean
114 115 116 |
# File 'lib/cz_system_info/cpu.rb', line 114 def self.respond_to_missing?(method, _private_methods = false) CPU_ARRAY.first.keys.include?(method.to_s) end |