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].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enable_iostat: true) ⇒ Model

Returns a new instance of Model.



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

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.



202
203
204
# File 'lib/osctl/cli/top/model.rb', line 202

def client
  @client
end

#containersObject (readonly)

Returns the value of attribute containers.



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

def containers
  @containers
end

#hostObject (readonly, protected)

Returns the value of attribute host.



202
203
204
# File 'lib/osctl/cli/top/model.rb', line 202

def host
  @host
end

#indexObject (readonly, protected)

Returns the value of attribute index.



202
203
204
# File 'lib/osctl/cli/top/model.rb', line 202

def index
  @index
end

#iostatObject (readonly, protected)

Returns the value of attribute iostat.



202
203
204
# File 'lib/osctl/cli/top/model.rb', line 202

def iostat
  @iostat
end

#modeObject

Returns the value of attribute mode.



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

def mode
  @mode
end

#monitorObject (readonly, protected)

Returns the value of attribute monitor.



202
203
204
# File 'lib/osctl/cli/top/model.rb', line 202

def monitor
  @monitor
end

#nprocObject (readonly, protected)

Returns the value of attribute nproc.



202
203
204
# File 'lib/osctl/cli/top/model.rb', line 202

def nproc
  @nproc
end

#subsystemsObject (readonly, protected)

Returns the value of attribute subsystems.



202
203
204
# File 'lib/osctl/cli/top/model.rb', line 202

def subsystems
  @subsystems
end

Instance Method Details

#add_ct(pool, id) ⇒ Object



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

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

  sync do
    containers << ct
    index << ct
  end
rescue OsCtl::Client::Error
  raise "Container #{pool}:#{id} announced, but not found"
end

#add_ct_netif(ct, name) ⇒ Object



185
186
187
188
189
190
# File 'lib/osctl/cli/top/model.rb', line 185

def add_ct_netif(ct, name)
  attrs = client.cmd_data!(:netif_show, pool: ct.pool, id: ct.id, name:)
  sync { ct.netifs << Top::Container::NetIf.new(attrs) }
rescue OsCtl::Client::Error
  raise "Unable to find netif #{name} for container #{ct.pool}:#{ct.id}"
end

#add_pool(pool) ⇒ Object



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

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

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



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

def calc_cpu_usage(part, total)
  if part < 0
    0.0
  else
    part.to_f / total * 100 * nproc
  end
end

#calc_host_cpu_usage(cpu) ⇒ Object (protected)



245
246
247
248
249
250
251
252
253
# File 'lib/osctl/cli/top/model.rb', line 245

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



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

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



177
178
179
# File 'lib/osctl/cli/top/model.rb', line 177

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

#has_ct?(pool, id) ⇒ Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/osctl/cli/top/model.rb', line 181

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

#has_pool?(pool) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#iostat_enabled?Boolean

Returns:

  • (Boolean)


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

def iostat_enabled?
  !iostat.nil?
end

#measureObject



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

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 StandardError
      @ct_lavgs = {}
    end
  end
end

#openObject (protected)



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

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



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

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

#remove_pool(pool) ⇒ Object



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

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

#setupObject



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

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



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

def stop
  iostat.stop if iostat
end

#syncObject



192
193
194
195
196
197
198
# File 'lib/osctl/cli/top/model.rb', line 192

def sync(&)
  if @mutex.owned?
    yield
  else
    @mutex.synchronize(&)
  end
end

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



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

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