Class: ProcParser::CPUStat

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(stat_file = '/proc/stat') ⇒ CPUStat

Returns a new instance of CPUStat.

Raises:



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

#guestObject

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_niceObject

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

#idleObject

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

#iowaitObject

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

#irqObject

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_cpuObject

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

#niceObject

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

#softirqObject

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

#stealObject

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

#systemObject

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

#userObject

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

#idletimeObject

The next values (idletime, non_idletime and totaltime) are computed based on the following information:

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_idletimeObject



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

#totaltimeObject



72
73
74
# File 'lib/proc_parser/cpu_stat.rb', line 72

def totaltime
  return idletime + non_idletime
end