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, enable_netif: true, enable_zfs: true) ⇒ Measurement

Returns a new instance of Measurement.



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

def initialize(host, subsystems, group_path, dataset, netifs, enable_netif: true, enable_zfs: true)
  @host = host
  @subsystems = subsystems
  @group_path = group_path
  @dataset = dataset
  @netifs = netifs
  @data = {}
  @enable_netif = enable_netif
  @enable_zfs = enable_zfs
end

Instance Attribute Details

#core_error ⇒ Object (readonly)

Returns the value of attribute core_error.



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

def core_error
  @core_error
end

#data ⇒ Object (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#dataset ⇒ Object (readonly, protected)

Returns the value of attribute dataset.



73
74
75
# File 'lib/osctl/cli/top/measurement.rb', line 73

def dataset
  @dataset
end

#group_path ⇒ Object (readonly, protected)

Returns the value of attribute group_path.



73
74
75
# File 'lib/osctl/cli/top/measurement.rb', line 73

def group_path
  @group_path
end

#host ⇒ Object (readonly, protected)

Returns the value of attribute host.



73
74
75
# File 'lib/osctl/cli/top/measurement.rb', line 73

def host
  @host
end

#netifs ⇒ Object (readonly, protected)

Returns the value of attribute netifs.



73
74
75
# File 'lib/osctl/cli/top/measurement.rb', line 73

def netifs
  @netifs
end

#subsystems ⇒ Object (readonly, protected)

Returns the value of attribute subsystems.



73
74
75
# File 'lib/osctl/cli/top/measurement.rb', line 73

def subsystems
  @subsystems
end

#time ⇒ Object (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_stats ⇒ Object (protected)



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/osctl/cli/top/measurement.rb', line 156

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

#delta_error(other) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/osctl/cli/top/measurement.rb', line 61

def delta_error(other)
  return 'invalid_window' unless time > other.time

  %i[cpu_us cpu_user_us cpu_system_us cpu_hz cpu_user_hz cpu_system_hz].each do |key|
    next if data[key].nil? || other.data[key].nil?
    return 'counter_reset' if raw(data[key]) < raw(other.data[key])
  end
  nil
end

#diff_from(other, mode) ⇒ Object



57
58
59
# File 'lib/osctl/cli/top/measurement.rb', line 57

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)



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
# File 'lib/osctl/cli/top/measurement.rb', line 100

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

  (other.keys | mine.keys).each do |k|
    v = other[k]
    if %i[cpu_limit memory memory_limit nproc].include?(k)
      ret[k] = mine[k]

    elsif v.nil? || mine[k].nil?
      ret[k] = nil

    elsif v.is_a?(Hash)
      ret[k] = do_diff_from(v, mine[k], mode, delta)

    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

#measure ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/osctl/cli/top/measurement.rb', line 23

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 cpu_limit memory memory_limit nproc],
                true
              ))

  validate_core
  data[:cpu_limit] = nil unless data.has_key?(:cpu_limit)
  data[:memory_limit] = nil unless data.has_key?(:memory_limit)

  data[:zfsio] = nil
  if @enable_zfs
    begin
      add_zfs_io_stats
    rescue StandardError
      data[:zfsio] = nil
    end
  end

  data[:tx] = data[:rx] = nil
  if @enable_netif
    begin
      data.update(netif_stats)
    rescue StandardError
      data[:tx] = data[:rx] = nil
    end
  end
rescue SystemCallError => e
  raise Error, e.message
end

#netif_stats ⇒ Object (protected)



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/osctl/cli/top/measurement.rb', line 128

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

#nonnegative?(value) ⇒ Boolean (protected)

Returns:

  • (Boolean)


79
80
81
82
# File 'lib/osctl/cli/top/measurement.rb', line 79

def nonnegative?(value)
  value = raw(value)
  value.is_a?(Numeric) && value.finite? && value >= 0
end

#raw(value) ⇒ Object (protected)



75
76
77
# File 'lib/osctl/cli/top/measurement.rb', line 75

def raw(value)
  value.is_a?(OsCtl::Lib::Cli::Presentable) ? value.raw : value
end

#validate_core ⇒ Object (protected)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/osctl/cli/top/measurement.rb', line 84

def validate_core
  @core_error = nil
  unless %i[cpu_user_hz cpu_system_hz memory].all? { |k| nonnegative?(data[k]) }
    @core_error = 'missing_cgroup_value'
  end
  %i[cpu_limit memory_limit].each do |k|
    next unless data.has_key?(k)

    if data[k].nil?
      @core_error = 'missing_cgroup_value'
    elsif !nonnegative?(data[k]) || raw(data[k]) <= 0
      @core_error = 'invalid_limit'
    end
  end
end