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_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(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.



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

def dataset
  @dataset
end

#group_pathObject (readonly, protected)

Returns the value of attribute group_path.



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

def group_path
  @group_path
end

#hostObject (readonly, protected)

Returns the value of attribute host.



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

def host
  @host
end

#netifsObject (readonly, protected)

Returns the value of attribute netifs.



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

def netifs
  @netifs
end

#subsystemsObject (readonly, protected)

Returns the value of attribute subsystems.



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

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)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/osctl/cli/top/measurement.rb', line 95

def add_zfs_io_stats
  if dataset.nil?
    data[:zfsio] = read_zfs_host_io_stats
  else
    ds = host.objsets[dataset]

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

#diff_from(other, mode) ⇒ Object



39
40
41
# File 'lib/osctl/cli/top/measurement.rb', line 39

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)



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

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



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

def measure
  @time = Time.now

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

  add_zfs_io_stats

  data.update(netif_stats)

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

#netif_statsObject (protected)



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

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)



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

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

#read_zfs_host_io_statsObject (protected)



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/osctl/cli/top/measurement.rb', line 122

def read_zfs_host_io_stats
  ret = {
    ios: {w: 0, r: 0},
    bytes: {w: 0, r: 0},
  }

  host.pools.each do |pool|
    st = OsCtl::Lib::Zfs::PoolIOStat.new(pool)
    ret[:ios][:w] += st.writes
    ret[:ios][:r] += st.reads
    ret[:bytes][:w] += st.nwritten
    ret[:bytes][:r] += st.nread
  end

  ret
end