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

#initializeModel

Returns a new instance of Model.



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

def initialize
  @mutex = Mutex.new
  @monitor = Top::Monitor.new(self)
  @host = Top::Host.new
  @mode = :realtime

  @monitor.subscribe
  open
end

Instance Attribute Details

#clientObject (readonly, protected)

Returns the value of attribute client.



166
167
168
# File 'lib/osctl/cli/top/model.rb', line 166

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.



166
167
168
# File 'lib/osctl/cli/top/model.rb', line 166

def host
  @host
end

#indexObject (readonly, protected)

Returns the value of attribute index.



166
167
168
# File 'lib/osctl/cli/top/model.rb', line 166

def index
  @index
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.



166
167
168
# File 'lib/osctl/cli/top/model.rb', line 166

def monitor
  @monitor
end

#nprocObject (readonly, protected)

Returns the value of attribute nproc.



166
167
168
# File 'lib/osctl/cli/top/model.rb', line 166

def nproc
  @nproc
end

#subsystemsObject (readonly, protected)

Returns the value of attribute subsystems.



166
167
168
# File 'lib/osctl/cli/top/model.rb', line 166

def subsystems
  @subsystems
end

Instance Method Details

#add_ct(pool, id) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/osctl/cli/top/model.rb', line 114

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



149
150
151
152
153
154
155
# File 'lib/osctl/cli/top/model.rb', line 149

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



106
107
108
# File 'lib/osctl/cli/top/model.rb', line 106

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

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



200
201
202
# File 'lib/osctl/cli/top/model.rb', line 200

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

#calc_host_cpu_usage(cpu) ⇒ Object (protected)



204
205
206
207
208
209
210
211
212
# File 'lib/osctl/cli/top/model.rb', line 204

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
# File 'lib/osctl/cli/top/model.rb', line 44

def data
  return [] unless host.setup?

  mem = Top::MemInfo.new
  host_result = host.result(mode, mem)
  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_data = {pool: ct.pool, id: ct.id}.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}.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),
    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_cached * 1024,
      swap_free: mem.swap_free * 1024,
    },
    zfs: host_zfs,
    containers: cts,
  }
end

#find_ct(pool, id) ⇒ Object



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

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

#has_ct?(pool, id) ⇒ Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/osctl/cli/top/model.rb', line 145

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

#has_pool?(pool) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/osctl/cli/top/model.rb', line 102

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

#measureObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/osctl/cli/top/model.rb', line 28

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
  end
end

#openObject (protected)



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/osctl/cli/top/model.rb', line 168

def open
  @client = OsCtl::Client.new
  @client.open
  @subsystems = client.cmd_data!(:group_cgsubsystems)
  @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



134
135
136
137
138
139
# File 'lib/osctl/cli/top/model.rb', line 134

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

#remove_pool(pool) ⇒ Object



110
111
112
# File 'lib/osctl/cli/top/model.rb', line 110

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

#setupObject



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

def setup
  client.cmd_data!(:pool_list).each { |v| host.pools << v[:name] }
  monitor.start
  @nproc = `nproc`.strip.to_i
  measure
end

#syncObject



157
158
159
160
161
162
163
# File 'lib/osctl/cli/top/model.rb', line 157

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

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



186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/osctl/cli/top/model.rb', line 186

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