Class: OsCtl::Cli::Top::Measurement

Inherits:
Object
  • Object
show all
Includes:
CGroupParams
Defined in:
lib/osctl/cli/top/measurement.rb

Defined Under Namespace

Classes: Error

Constant Summary

Constants included from CGroupParams

CGroupParams::CGPARAM_DEFAULT_FIELDS, CGroupParams::CGPARAM_FIELDS, CGroupParams::CGPARAM_FILTERS, CGroupParams::CGPARAM_STATS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CGroupParams

#cg_add_stats, #cg_blkio_stats, #cg_read_stats, #do_cgparam_apply, #do_cgparam_list, #do_cgparam_replace, #do_cgparam_set, #do_cgparam_unset, #do_set_cpu_limit, #do_set_memory, #do_unset_cpu_limit, #do_unset_memory, #parse_cgparams, #parse_memory_stat, #parse_subsystem, #read_cgparam, #read_memory_usage

Constructor Details

#initialize(subsystems, group_path, netifs) ⇒ Measurement

Returns a new instance of Measurement



12
13
14
15
16
17
# File 'lib/osctl/cli/top/measurement.rb', line 12

def initialize(subsystems, group_path, netifs)
  @subsystems = subsystems
  @group_path = group_path
  @netifs = netifs
  @data = {}
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data



10
11
12
# File 'lib/osctl/cli/top/measurement.rb', line 10

def data
  @data
end

#group_pathObject (readonly, protected)

Returns the value of attribute group_path



46
47
48
# File 'lib/osctl/cli/top/measurement.rb', line 46

def group_path
  @group_path
end

#netifsObject (readonly, protected)

Returns the value of attribute netifs



46
47
48
# File 'lib/osctl/cli/top/measurement.rb', line 46

def netifs
  @netifs
end

#subsystemsObject (readonly, protected)

Returns the value of attribute subsystems



46
47
48
# File 'lib/osctl/cli/top/measurement.rb', line 46

def subsystems
  @subsystems
end

#timeObject (readonly)

Returns the value of attribute time



10
11
12
# File 'lib/osctl/cli/top/measurement.rb', line 10

def time
  @time
end

Instance Method Details

#diff_from(other, mode) ⇒ Object



41
42
43
# File 'lib/osctl/cli/top/measurement.rb', line 41

def diff_from(other, mode)
  do_diff_from(other.data, data, mode, time - other.time)
end

#do_diff_from(other, mine, mode, delta) ⇒ Object (protected)



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/osctl/cli/top/measurement.rb', line 48

def do_diff_from(other, mine, mode, delta)
  ret = {}

  other.each do |k, v|
    if v.is_a?(Hash)
      ret[k] = do_diff_from(v, mine[k], mode, delta)

    elsif %i(memory nproc).include?(k)
      ret[k] = mine[k]

    else
      if mode == :realtime
        ret[k] = ((mine[k] - v) / delta.to_f).round
      else
        ret[k] = mine[k] - v
      end

      ret[k] = 0 if ret[k] < 0
    end
  end

  ret
end

#measureObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/osctl/cli/top/measurement.rb', line 19

def measure
  @time = Time.now

  data.update(cg_read_stats(
    subsystems,
    group_path,
    %i(cpu_time cpu_stat memory nproc),
    true
  ))

  data[:blkio] = cg_blkio_stats(
    subsystems,
    group_path,
    %i(bytes iops)
  )

  data.update(netif_stats)

rescue SystemCallError => e
  raise Error, e.message
end

#netif_statsObject (protected)



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/osctl/cli/top/measurement.rb', line 72

def netif_stats
  ret = {tx: {bytes: 0, packets: 0}, rx: {bytes: 0, packets: 0}}

  netifs.each do |netif|
    next unless netif.veth

    %i(bytes packets).each do |type|
      # rx/tx are reversed within the container
      {rx: :tx, tx: :rx}.each do |host_dir, ct_dir|
        ret[ct_dir][type] = read_netif_stats(netif, host_dir, type)
      end
    end
  end

  ret
end

#read_netif_stats(netif, dir, type) ⇒ Object (protected)



89
90
91
92
93
94
95
# File 'lib/osctl/cli/top/measurement.rb', line 89

def read_netif_stats(netif, dir, type)
  ret = File.read("/sys/class/net/#{netif.veth}/statistics/#{dir}_#{type}")
  ret.strip.to_i

rescue Errno::ENOENT
  0
end