Class: OsCtl::Cli::Top::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/cli/top/model.rb

Constant Summary collapse

MODES =
%i(realtime cumulative)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enable_iostat: true) ⇒ Model

Returns a new instance of Model.



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

def initialize(enable_iostat: true)
  @mutex = Mutex.new
  @monitor = Top::Monitor.new(self)
  @iostat = enable_iostat ? OsCtl::Lib::Zfs::IOStat.new : nil
  @host = Top::Host.new(iostat)
  @mode = :realtime

  @monitor.subscribe
  open
end

Instance Attribute Details

#clientObject (readonly, protected)

Returns the value of attribute client.



204
205
206
# File 'lib/osctl/cli/top/model.rb', line 204

def client
  @client
end

#containersObject (readonly)

Returns the value of attribute containers.



8
9
10
# File 'lib/osctl/cli/top/model.rb', line 8

def containers
  @containers
end

#hostObject (readonly, protected)

Returns the value of attribute host.



204
205
206
# File 'lib/osctl/cli/top/model.rb', line 204

def host
  @host
end

#indexObject (readonly, protected)

Returns the value of attribute index.



204
205
206
# File 'lib/osctl/cli/top/model.rb', line 204

def index
  @index
end

#iostatObject (readonly, protected)

Returns the value of attribute iostat.



204
205
206
# File 'lib/osctl/cli/top/model.rb', line 204

def iostat
  @iostat
end

#modeObject

Returns the value of attribute mode.



9
10
11
# File 'lib/osctl/cli/top/model.rb', line 9

def mode
  @mode
end

#monitorObject (readonly, protected)

Returns the value of attribute monitor.



204
205
206
# File 'lib/osctl/cli/top/model.rb', line 204

def monitor
  @monitor
end

#nprocObject (readonly, protected)

Returns the value of attribute nproc.



204
205
206
# File 'lib/osctl/cli/top/model.rb', line 204

def nproc
  @nproc
end

#subsystemsObject (readonly, protected)

Returns the value of attribute subsystems.



204
205
206
# File 'lib/osctl/cli/top/model.rb', line 204

def subsystems
  @subsystems
end

Instance Method Details

#add_ct(pool, id) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/osctl/cli/top/model.rb', line 152

def add_ct(pool, id)
  ct = client.cmd_data!(:ct_show, pool: pool, id: id)
  ct = Top::Container.new(ct)
  ct.netifs = client.cmd_data!(
    :netif_list,
    id: id,
    pool: pool
  ).map do |netif_attrs|
    Top::Container::NetIf.new(netif_attrs)
  end

  sync do
    containers << ct
    index << ct
  end

rescue OsCtl::Client::Error
  fail "Container #{pool}:#{id} announced, but not found"
end

#add_ct_netif(ct, name) ⇒ Object



187
188
189
190
191
192
193
# File 'lib/osctl/cli/top/model.rb', line 187

def add_ct_netif(ct, name)
  attrs = client.cmd_data!(:netif_show, pool: ct.pool, id: ct.id, name: name)
  sync { ct.netifs << Top::Container::NetIf.new(attrs) }

rescue OsCtl::Client::Error
  fail "Unable to find netif #{name} for container #{ct.pool}:#{ct.id}"
end

#add_pool(pool) ⇒ Object



142
143
144
145
# File 'lib/osctl/cli/top/model.rb', line 142

def add_pool(pool)
  host.pools << pool
  iostat.add_pool(pool) if iostat
end

#calc_cpu_usage(part, total) ⇒ Object (protected)



238
239
240
# File 'lib/osctl/cli/top/model.rb', line 238

def calc_cpu_usage(part, total)
  part.to_f / total * 100 * nproc
end

#calc_host_cpu_usage(cpu) ⇒ Object (protected)



242
243
244
245
246
247
248
249
250
# File 'lib/osctl/cli/top/model.rb', line 242

def calc_host_cpu_usage(cpu)
  ret = {}

  cpu.each_pair do |k, v|
    ret[k] = v.to_f / cpu.total * 100
  end

  ret
end

#dataObject



65
66
67
68
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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/osctl/cli/top/model.rb', line 65

def data
  return {} unless host.setup?

  mem = OsCtl::Lib::MemInfo.new
  lavg = OsCtl::Lib::LoadAvg.new

  host_result = host.result(mode, mem, lavg)
  host_cpu = host.cpu_result
  host_zfs = host.zfs_result
  sum_ct_cpu_hz = 0
  cts = []

  sync do
    containers.each do |ct|
      next if !ct.running? || !ct.setup?

      ct_result = ct.result(mode)
      update_host_result(host_result, ct_result)

      ct_lavg = @ct_lavgs[ct.ident]

      ct_data = {
        pool: ct.pool,
        id: ct.id,
        cpu_package_inuse: ct.cpu_package_inuse,
        init_pid: ct.init_pid,
        loadavg: ct_lavg ? ct_lavg.averages : [0.0, 0.0, 0.0],
      }.merge(ct_result)

      if mode == :realtime
        ct_cpu_hz = ct_result[:cpu_user_hz] + ct_result[:cpu_system_hz]
        sum_ct_cpu_hz += ct_cpu_hz

        ct_data.update(
          cpu_usage: calc_cpu_usage(ct_cpu_hz, host_cpu.total),
        )
      end

      cts << ct_data
    end
  end

  host_data = {
    id: host.id,
    loadavg: lavg.to_a,
  }.merge(host_result)

  if mode == :realtime
    host_data.update(
      cpu_usage: calc_cpu_usage(host_cpu.total_used - sum_ct_cpu_hz, host_cpu.total),
    )
  end

  cts << host_data

  {
    cpu: calc_host_cpu_usage(host_cpu),
    loadavg: lavg.to_a,
    memory: {
      total: mem.total * 1024,
      used: mem.used * 1024,
      free: mem.free * 1024,
      buffers: mem.buffers * 1024,
      cached: mem.cached * 1024,
      swap_total: mem.swap_total * 1024,
      swap_used: mem.swap_used * 1024,
      swap_free: mem.swap_free * 1024,
    },
    zfs: host_zfs,
    containers: cts,
  }
end

#find_ct(pool, id) ⇒ Object



179
180
181
# File 'lib/osctl/cli/top/model.rb', line 179

def find_ct(pool, id)
  sync { index["#{pool}:#{id}"] }
end

#has_ct?(pool, id) ⇒ Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/osctl/cli/top/model.rb', line 183

def has_ct?(pool, id)
  !find_ct(pool, id).nil?
end

#has_pool?(pool) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/osctl/cli/top/model.rb', line 138

def has_pool?(pool)
  host.pools.include?(pool)
end

#iostat_enabled?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/osctl/cli/top/model.rb', line 22

def iostat_enabled?
  !iostat.nil?
end

#measureObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/osctl/cli/top/model.rb', line 43

def measure
  host.measure(subsystems)

  sync do
    containers.each do |ct|
      next unless ct.running?

      begin
        ct.measure(host, subsystems)
      rescue Top::Measurement::Error
        ct.state = :error
      end
    end

    begin
      @ct_lavgs = OsCtl::Lib::LoadAvgReader.read_for(containers)
    rescue
      @ct_lavgs = {}
    end
  end
end

#openObject (protected)



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/osctl/cli/top/model.rb', line 206

def open
  @client = OsCtl::Client.new
  @client.open
  @subsystems = client.cmd_data!(:group_cgsubsystems) if OsCtl::Lib::CGroup.v1?
  @index = OsCtl::Lib::Index.new { |ct| "#{ct.pool}:#{ct.id}" }
  @containers = client.cmd_data!(:ct_list).map do |ct_attrs|
    ct = Top::Container.new(ct_attrs)
    index << ct
    ct
  end

  client.cmd_data!(:netif_list).each do |netif_attrs|
    ct = index["#{netif_attrs[:pool]}:#{netif_attrs[:ctid]}"]
    next if ct.nil?
    ct.netifs << Top::Container::NetIf.new(netif_attrs)
  end
end

#remove_ct(ct) ⇒ Object



172
173
174
175
176
177
# File 'lib/osctl/cli/top/model.rb', line 172

def remove_ct(ct)
  sync do
    containers.delete(ct)
    index.delete(ct)
  end
end

#remove_pool(pool) ⇒ Object



147
148
149
150
# File 'lib/osctl/cli/top/model.rb', line 147

def remove_pool(pool)
  host.pools.delete(pool)
  iostat.remove_pool(pool) if iostat
end

#setupObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/osctl/cli/top/model.rb', line 26

def setup
  client.cmd_data!(:pool_list).each { |v| host.pools << v[:name] }
  monitor.start

  if iostat
    iostat.pools = host.pools
    iostat.start
  end

  @nproc = `nproc`.strip.to_i
  measure
end

#stopObject



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

def stop
  iostat.stop if iostat
end

#syncObject



195
196
197
198
199
200
201
# File 'lib/osctl/cli/top/model.rb', line 195

def sync
  if @mutex.owned?
    yield
  else
    @mutex.synchronize { yield }
  end
end

#update_host_result(host_result, ct_result) ⇒ Object (protected)



224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/osctl/cli/top/model.rb', line 224

def update_host_result(host_result, ct_result)
  ct_result.each do |k, v|
    if v.is_a?(Hash)
      host_result[k] = update_host_result(host_result[k], v)

    else
      host_result[k] -= v
      host_result[k] = 0 if host_result[k] < 0
    end
  end

  host_result
end