Class: ProcParser::CPUStat
- Inherits:
-
Object
- Object
- ProcParser::CPUStat
- Defined in:
- lib/proc_parser/cpu_stat.rb
Constant Summary collapse
- @@attributes =
{ user: 1, nice: 2, system: 3, idle: 4, iowait: 5, irq: 6, softirq: 7, steal: 8, guest: 9, guest_nice: 10, }
Instance Attribute Summary collapse
-
#guest ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#guest_nice ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#idle ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#iowait ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#irq ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#nb_cpu ⇒ Object
Returns the value of attribute nb_cpu.
-
#nice ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#softirq ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#steal ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#system ⇒ Object
This class read CPU usage information from the /proc/stat file.
-
#user ⇒ Object
This class read CPU usage information from the /proc/stat file.
Instance Method Summary collapse
-
#idletime ⇒ Object
The next values (idletime, non_idletime and totaltime) are computed based on the following information:.
-
#initialize(stat_file = '/proc/stat') ⇒ CPUStat
constructor
A new instance of CPUStat.
- #non_idletime ⇒ Object
- #totaltime ⇒ Object
Constructor Details
#initialize(stat_file = '/proc/stat') ⇒ CPUStat
Returns a new instance of CPUStat.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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 |
Instance Attribute Details
#guest ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def guest @guest end |
#guest_nice ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def guest_nice @guest_nice end |
#idle ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def idle @idle end |
#iowait ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def iowait @iowait end |
#irq ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def irq @irq end |
#nb_cpu ⇒ Object
Returns the value of attribute nb_cpu.
22 23 24 |
# File 'lib/proc_parser/cpu_stat.rb', line 22 def nb_cpu @nb_cpu end |
#nice ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def nice @nice end |
#softirq ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def softirq @softirq end |
#steal ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def steal @steal end |
#system ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def system @system end |
#user ⇒ Object
This class read CPU usage information from the /proc/stat file. The behavior for Kernel version prior to 2.5.41 is undefined.
8 9 10 |
# File 'lib/proc_parser/cpu_stat.rb', line 8 def user @user end |
Instance Method Details
#idletime ⇒ Object
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.
58 59 60 |
# File 'lib/proc_parser/cpu_stat.rb', line 58 def idletime return @idle + @iowait end |
#non_idletime ⇒ Object
62 63 64 65 66 67 68 69 70 |
# 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 |
#totaltime ⇒ Object
72 73 74 |
# File 'lib/proc_parser/cpu_stat.rb', line 72 def totaltime return idletime + non_idletime end |