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_raw_cgroup_params, #cg_add_stats, #cg_init_subsystems, #cg_list_raw_cgroup_params, #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_subsystem

Constructor Details

#initialize(host, subsystems, group_path, dataset, netifs) ⇒ Measurement

Returns a new instance of Measurement.



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

def initialize(host, subsystems, group_path, dataset, netifs)
  @host = host
  @subsystems = subsystems
  @group_path = group_path
  @dataset = dataset
  @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

#datasetObject (readonly, protected)

Returns the value of attribute dataset.



43
44
45
# File 'lib/osctl/cli/top/measurement.rb', line 43

def dataset
  @dataset
end

#group_pathObject (readonly, protected)

Returns the value of attribute group_path.



43
44
45
# File 'lib/osctl/cli/top/measurement.rb', line 43

def group_path
  @group_path
end

#hostObject (readonly, protected)

Returns the value of attribute host.



43
44
45
# File 'lib/osctl/cli/top/measurement.rb', line 43

def host
  @host
end

#netifsObject (readonly, protected)

Returns the value of attribute netifs.



43
44
45
# File 'lib/osctl/cli/top/measurement.rb', line 43

def netifs
  @netifs
end

#subsystemsObject (readonly, protected)

Returns the value of attribute subsystems.



43
44
45
# File 'lib/osctl/cli/top/measurement.rb', line 43

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

#add_zfs_io_statsObject (protected)



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/osctl/cli/top/measurement.rb', line 97

def add_zfs_io_stats
  if dataset.nil?
    st = host.objsets.aggregate_stats

    data[:zfsio] = {
      ios: {
        w: st.write_ios,
        r: st.read_ios
      },
      bytes: {
        w: st.write_bytes,
        r: st.read_bytes
      }
    }
  else
    ds = host.objsets[dataset]

    if ds
      st = ds.aggregate_stats
      data[:zfsio] = {
        ios: {
          w: st.write_ios,
          r: st.read_ios
        },
        bytes: {
          w: st.write_bytes,
          r: st.read_bytes
        }
      }
    else
      data[:zfsio] = {
        ios: { w: 0, r: 0 },
        bytes: { w: 0, r: 0 }
      }
    end
  end
end

#diff_from(other, mode) ⇒ Object



37
38
39
# File 'lib/osctl/cli/top/measurement.rb', line 37

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)



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

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
      ret[k] = if mode == :realtime
                 ((mine[k] - v) / delta.to_f).round
               else
                 mine[k] - v
               end

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

  ret
end

#measureObject



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

def measure
  @time = Time.now
  cg_reader = OsCtl::Lib::CGroup::PathReader.new(subsystems, group_path)

  data.update(cg_reader.read_stats(
                %i[cpu_us cpu_hz memory nproc],
                true
              ))

  add_zfs_io_stats

  data.update(netif_stats)
rescue SystemCallError => e
  raise Error, e.message
end

#netif_statsObject (protected)



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/osctl/cli/top/measurement.rb', line 69

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

  if netifs == :all
    host.netif_stats.get_stats_for_all.each_value do |st|
      ret[:tx][:bytes] += st[:tx][:bytes]
      ret[:tx][:packets] += st[:tx][:packets]
      ret[:rx][:bytes] += st[:rx][:bytes]
      ret[:rx][:packets] += st[:rx][:packets]
    end

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

      st = host.netif_stats.get_stats_for(netif.veth)

      # rx/tx are reversed within the container
      ret[:tx][:bytes] += st[:rx][:bytes]
      ret[:tx][:packets] += st[:rx][:packets]
      ret[:rx][:bytes] += st[:tx][:bytes]
      ret[:rx][:packets] += st[:tx][:packets]
    end
  end

  ret
end