Class: Apolo::Domains::CpuSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/apolo/domains/cpu_socket.rb

Overview

CpuSocket

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

Instance Method Summary collapse

Constructor Details

#initializeCpuSocket

Returns a new instance of CpuSocket.



7
8
9
10
11
# File 'lib/apolo/domains/cpu_socket.rb', line 7

def initialize
  @init_stats = statistics_of_process
  sleep 1
  @end_stats = statistics_of_process
end

Instance Method Details

#cpu_socketInteger

Get number of cpus for each socket

Returns:

  • (Integer)

    the number of cpus for sockets



16
17
18
# File 'lib/apolo/domains/cpu_socket.rb', line 16

def cpu_socket
  cpus / (sockets + 1)
end

#cpu_usageArray

Get usage for each cpu

Returns:

  • (Array)

    the percentage of usage for each cpu



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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

#cpusInteger

Get number of cpus

Returns:

  • (Integer)

    the number of cpus on system



83
84
85
# File 'lib/apolo/domains/cpu_socket.rb', line 83

def cpus
  File.readlines('/proc/cpuinfo').grep(/^processor/).count
end

#proc_total(cores, stats) ⇒ Integer

Get total of process

Returns:

  • (Integer)

    the total of process



37
38
39
40
41
42
43
44
45
46
# 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

#socketsInteger

Get number of sockets

Returns:

  • (Integer)

    the number of sockets on system



76
77
78
# 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) ⇒ Integer

Get usage for cpus

Returns:

  • (Integer)

    the usage for cpus



23
24
25
26
27
28
29
30
31
32
# 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