class Apolo::Domains::CpuSocket

CpuSocket

Get information about percentage of usage for each cpu on all sockets

Public Class Methods

new() click to toggle source
# File lib/apolo/domains/cpu_socket.rb, line 7
def initialize
  @init_stats = statistics_of_process
  sleep 1
  @end_stats = statistics_of_process
end

Public Instance Methods

cpu_socket() click to toggle source

Get number of cpus for each socket

@return [Integer] the number of cpus for sockets

# File lib/apolo/domains/cpu_socket.rb, line 16
def cpu_socket
  cpus / (sockets + 1)
end
cpu_usage() click to toggle source

Get usage for each cpu

@return [Array] the percentage of usage for each cpu

# File lib/apolo/domains/cpu_socket.rb, line 51
def cpu_usage
  cpu_usage = []
  (0..sockets).each do |socket|
    cores = (socket * cpu_socket..socket * cpu_socket + (cpu_socket - 1))

    init_usage = usage_sum(cores, @init_stats)
    end_usage = usage_sum(cores, @end_stats)

    proc_usage = end_usage - init_usage

    init_total = proc_total(cores, @init_stats)
    end_total = proc_total(cores, @end_stats)

    proctotal = (end_total - init_total)

    usage = (proc_usage.to_f / proctotal.to_f)

    cpu_usage[socket] = (100 * usage).to_f
  end
  cpu_usage
end
cpus() click to toggle source

Get number of cpus

@return [Integer] the number of cpus on system

# File lib/apolo/domains/cpu_socket.rb, line 83
def cpus
  File.readlines('/proc/cpuinfo').grep(/^processor/).count
end
proc_total(cores, stats) click to toggle source

Get total of process

@return [Integer] the total of process

# File lib/apolo/domains/cpu_socket.rb, line 37
def proc_total(cores, stats)
  proc_total = 0
  (1..4).each do |i|
    cores.each do |core|
      line = stats[core + 1].split(' ')
      proc_total += line[i].to_i
    end
  end
  proc_total
end
sockets() click to toggle source

Get number of sockets

@return [Integer] the number of sockets on system

# File lib/apolo/domains/cpu_socket.rb, line 76
def sockets
  File.readlines('/proc/cpuinfo').grep(/^physical id/).last.split(' ')[3].to_i
end
usage_sum(cores, stats) click to toggle source

Get usage for cpus

@return [Integer] the usage for cpus

# File lib/apolo/domains/cpu_socket.rb, line 23
def usage_sum(cores, stats)
  usage_sum = 0

  cores.each do |core|
    line = stats[core + 1].split(' ')
    usage_sum = line[1].to_i + line[2].to_i + line[3].to_i
  end

  usage_sum
end

Private Instance Methods

statistics_of_process() click to toggle source

Get statistics of process

@return [Array] the statistics of process

# File lib/apolo/domains/cpu_socket.rb, line 92
def statistics_of_process
    File.readlines('/proc/stat')
end