Class: PosixPsutil::CPU
- Inherits:
-
Object
- Object
- PosixPsutil::CPU
- Defined in:
- lib/posixpsutil/linux/system.rb,
lib/posixpsutil/posix/system.rb
Class Method Summary collapse
- .calculate_cpu_percent_field(start, last) ⇒ Object
-
.cpu_count(logical = true) ⇒ Object
Return the number of physical/logical CPUs in the system.
-
.cpu_percent(interval = 0.0, percpu = false) ⇒ Object
measure cpu usage percent during an interval WARNING: set a small interval will cause incorrect result.
-
.cpu_times(precpu = false) ⇒ Object
Return OpenStruct representing the CPU times for all CPU available in the system.
- .cpu_times_percent(interval = 0.0, percpu = false) ⇒ Object
Class Method Details
.calculate_cpu_percent_field(start, last) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/posixpsutil/linux/system.rb', line 164 def self.calculate_cpu_percent_field(start, last) start_sum = 0 start.marshal_dump.each_value {|value| start_sum += value} last_sum = 0 last.marshal_dump.each_value {|value| last_sum += value} ret = OpenStruct.new [:user, :nice, :system, :idle, :iowait, :irq, :softirq, :steal, :guest, :guest_nice].each do |field| start_field = start[field] last_field = last[field] # be aware of float precision issue last_field = start_field if last_field < start_field field_delta = last_field - start_field all_delta = last_sum - start_sum # if the interval is too small if all_delta == 0 percent = 0 else percent = field_delta * 100 / all_delta end ret[field] = percent.round(2) end ret end |
.cpu_count(logical = true) ⇒ Object
Return the number of physical/logical CPUs in the system.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/posixpsutil/linux/system.rb', line 82 def self.cpu_count(logical=true) count = 0 if !logical unless @physical_cpu_count IO.readlines('/proc/cpuinfo').each do |line| count += 1 if line.start_with?('physical id') end @physical_cpu_count = count end return @physical_cpu_count end unless @logical_cpu_count Dir.entries('/sys/devices/system/cpu').each do |entry| count += 1 if entry.start_with?('cpu') end count -= 2 # except cpuidle and cpufreq @logical_cpu_count = count end @logical_cpu_count end |
.cpu_percent(interval = 0.0, percpu = false) ⇒ Object
measure cpu usage percent during an interval WARNING: set a small interval will cause incorrect result
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/posixpsutil/linux/system.rb', line 29 def self.cpu_percent(interval=0.0, percpu=false) if interval > 0.0 total_start = self.cpu_times(percpu) sleep interval else if percpu total_start = @last_per_cpu_times else total_start = @last_cpu_times end end if percpu @last_per_cpu_times = self.cpu_times(true) ret = [] total_start.each_index do |i| ret.push(calculate_cpu_percent(total_start[i], @last_per_cpu_times[i])) end ret else @last_cpu_times = self.cpu_times() calculate_cpu_percent(total_start, @last_cpu_times) end end |
.cpu_times(precpu = false) ⇒ Object
Return OpenStruct representing the CPU times for all CPU available in the system. If precpu is true, return an Array of that OpenStructs, one per CPU.
For the format of OpenStruct, see ‘get_cpu_fields`.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/posixpsutil/linux/system.rb', line 14 def self.cpu_times(precpu=false) proc_stat = File.new('/proc/stat') cpu = proc_stat.readline() return get_cpu_fields(cpu) unless precpu cpus = [] loop do cpu = proc_stat.readline() break unless cpu.start_with?('cpu') cpus.push(get_cpu_fields(cpu)) end cpus end |
.cpu_times_percent(interval = 0.0, percpu = false) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/posixpsutil/linux/system.rb', line 54 def self.cpu_times_percent(interval=0.0, percpu=false) if interval > 0.0 total_start = self.cpu_times(percpu) sleep interval else if percpu total_start = @last_per_cpu_times_fields else total_start = @last_cpu_times_fields end end if percpu @last_per_cpu_times_fields = self.cpu_times(true) ret = [] total_start.each_index do |i| ret.push(calculate_cpu_percent_field(total_start[i], @last_per_cpu_times_fields[i])) end ret else @last_cpu_times = self.cpu_times() calculate_cpu_percent_field(total_start, @last_cpu_times) end end |