class ProcParser::CPUStat
Attributes
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
Public Class Methods
# File lib/proc_parser/cpu_stat.rb, line 24 def initialize(stat_file = '/proc/stat') raise NoProcData, "This system doesn't have /proc/stat data." if !File.exist?(stat_file) File.open(stat_file, 'r') do |file| firstline = file.readline.strip.squeeze(' ').split(' ') raise NoProcData, 'Unsupported format of /proc/stat' if firstline[0] != 'cpu' @@attributes.each do |attribute, index| instance_variable_set("@#{attribute}", firstline[index].to_i) end @nb_cpu = 0 file.each do |line| splitted_line = line.strip.squeeze(' ').split(' ') break if !splitted_line[0].start_with? 'cpu' @nb_cpu += 1 end end end
Public Instance Methods
The next values (idletime, non_idletime
and totaltime) are computed based on the following information:
-
`htop` source code: github.com/hishamhm/htop/blob/e0209da88faf3b390d71ff174065abd407abfdfd/ProcessList.c#L947
-
man 5 proc
Computing the percentage usage is done with the following algorithm: totald = Total - PrevTotal idled = Idle - PrevIdle CPU_Percentage = (totald - idled)/totald
With the Prev* variables fetched earlier than the current values.
# File lib/proc_parser/cpu_stat.rb, line 58 def idletime return @idle + @iowait end
# File lib/proc_parser/cpu_stat.rb, line 62 def non_idletime # Guest time is already accounted in user time user = @user - @guest nice = @nice - @guest_nice systemalltime = @system + @irq + @softirq virtalltime = @guest + @guest_nice return user + nice + systemalltime + @steal + virtalltime end
# File lib/proc_parser/cpu_stat.rb, line 72 def totaltime return idletime + non_idletime end