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

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

Defined Under Namespace

Classes: InvalidSample

Constant Summary collapse

MODES =
%i[realtime cumulative].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enable_iostat: true, enable_netif: true, enable_zfs: true) ⇒ Model

Returns a new instance of Model.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/osctl/cli/top/model.rb', line 13

def initialize(enable_iostat: true, enable_netif: true, enable_zfs: true)
  @mutex = Mutex.new
  @generation = 0
  @sequence = 0
  @epoch = SecureRandom.uuid
  @baseline_started = nil
  @snapshots = {}
  @collectors = { netif: enable_netif, zfs: enable_zfs, iostat: enable_zfs && enable_iostat }.freeze
  @monitor = Top::Monitor.new(self)
  @iostat = iostat_enabled? ? OsCtl::Lib::Zfs::IOStat.new : nil
  @host = Top::Host.new(iostat, enable_netif:, enable_zfs:)
  @mode = :realtime
  @monitor.subscribe
  open
end

Instance Attribute Details

#client ⇒ Object (readonly, protected)

Returns the value of attribute client.



229
230
231
# File 'lib/osctl/cli/top/model.rb', line 229

def client
  @client
end

#collectors ⇒ Object (readonly)

Returns the value of attribute collectors.



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

def collectors
  @collectors
end

#containers ⇒ Object (readonly)

Returns the value of attribute containers.



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

def containers
  @containers
end

#host ⇒ Object (readonly, protected)

Returns the value of attribute host.



229
230
231
# File 'lib/osctl/cli/top/model.rb', line 229

def host
  @host
end

#index ⇒ Object (readonly, protected)

Returns the value of attribute index.



229
230
231
# File 'lib/osctl/cli/top/model.rb', line 229

def index
  @index
end

#iostat ⇒ Object (readonly, protected)

Returns the value of attribute iostat.



229
230
231
# File 'lib/osctl/cli/top/model.rb', line 229

def iostat
  @iostat
end

#mode ⇒ Object

Returns the value of attribute mode.



11
12
13
# File 'lib/osctl/cli/top/model.rb', line 11

def mode
  @mode
end

#monitor ⇒ Object (readonly, protected)

Returns the value of attribute monitor.



229
230
231
# File 'lib/osctl/cli/top/model.rb', line 229

def monitor
  @monitor
end

#nproc ⇒ Object (readonly, protected)

Returns the value of attribute nproc.



229
230
231
# File 'lib/osctl/cli/top/model.rb', line 229

def nproc
  @nproc
end

#subsystems ⇒ Object (readonly, protected)

Returns the value of attribute subsystems.



229
230
231
# File 'lib/osctl/cli/top/model.rb', line 229

def subsystems
  @subsystems
end

Instance Method Details

#add_ct(pool, id) ⇒ Object



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

def add_ct(pool, id)
  # Monitor lookups use their own connection, never the measuring client's
  # stream or the model publication mutex.
  with_client do |lookup|
    attrs = lookup.cmd_data!(:ct_show, pool:, id:, read_hostname: false)
    ct = new_container(attrs)
    ct.netifs = read_netifs(lookup, pool:, id:) if netif_enabled?
    sync do
      unless index[ct.ident]
        containers << ct
        index << ct
        changed!
      end
    end
  end
rescue OsCtl::Client::Error
  raise "Container #{pool}:#{id} announced, but not found"
end

#add_ct_netif(ct, name) ⇒ Object



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

def add_ct_netif(ct, name)
  return unless netif_enabled?

  attrs = with_client { |lookup| lookup.cmd_data!(:netif_show, pool: ct.pool, id: ct.id, name:) }
  sync do
    current = index[ct.ident]
    current.netifs << Top::Container::NetIf.new(attrs) if current && !current.has_netif?(name)
    changed!
  end
rescue OsCtl::Client::Error
  raise "Unable to find netif #{name} for container #{ct.pool}:#{ct.id}"
end

#add_pool(pool) ⇒ Object



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

def add_pool(pool)
  return unless zfs_enabled?

  sync do
    host.pools << pool unless host.pools.include?(pool)
    changed!
  end
  iostat.add_pool(pool) if iostat
end

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



432
433
434
435
436
437
438
# File 'lib/osctl/cli/top/model.rb', line 432

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)



440
441
442
443
444
445
446
447
448
# File 'lib/osctl/cli/top/model.rb', line 440

def calc_host_cpu_usage(cpu)
  ret = {}

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

  ret
end

#changed! ⇒ Object



219
220
221
# File 'lib/osctl/cli/top/model.rb', line 219

def changed!
  @generation += 1
end

#copy_data(value) ⇒ Object (protected)



408
409
410
411
412
413
414
# File 'lib/osctl/cli/top/model.rb', line 408

def copy_data(value)
  case value
  when Hash then value.transform_values { |v| copy_data(v) }
  when Array then value.map { |v| copy_data(v) }
  else value
  end
end

#data ⇒ Object

Presentation may copy/sort a snapshot, but cannot acquire or restamp it.



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

def data
  sync { copy_data(@snapshots.fetch(mode, {})) }
end

#find_ct(pool, id) ⇒ Object



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

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

#has_ct?(pool, id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#has_pool?(pool) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_pool?(pool)
  sync { host.pools.include?(pool) }
end

#init_start_time(pid) ⇒ Object (protected)



319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/osctl/cli/top/model.rb', line 319

def init_start_time(pid)
  stat = File.read("/proc/#{pid}/stat")
  closing = stat.rindex(')')
  raise InvalidSample, 'invalid_run' unless closing && stat.split.first == pid.to_s

  fields = stat[closing + 1..].split
  ticks = Integer(fields.fetch(19), 10)
  raise InvalidSample, 'invalid_run' unless ticks > 0 && !%w[Z X x].include?(fields[0])

  ticks
rescue ArgumentError, IndexError
  raise InvalidSample, 'invalid_run'
end

#invalid_snapshot(started, completed, reason) ⇒ Object (protected)



399
400
401
402
403
404
405
406
# File 'lib/osctl/cli/top/model.rb', line 399

def invalid_snapshot(started, completed, reason)
  (started, completed, reason, nil).merge(
    cpu: nil, memory: nil, loadavg: nil, zfs: nil,
    containers: [{ pool: nil, id: '[host]', init_pid: nil, init_start_time_ticks: nil,
                   cpu_usage: nil, memory: nil, memory_limit: nil, cpu_limit: nil,
                   loadavg: nil, tx: nil, rx: nil, zfsio: nil, valid: false, invalid_reason: reason }]
  )
end

#iostat_enabled? ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/osctl/cli/top/model.rb', line 29

def iostat_enabled?
  collectors[:iostat]
end

#measure ⇒ Object



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

def measure
  started = monotonic_ns
  generation = sync do
    @sequence += 1
    @generation
  end
  begin
    raise InvalidSample, 'invalid_cpu_count' unless nproc && nproc > 0

    attrs = read_inventory
    new_cts = prepare_new_containers(attrs)
    measured_host, measured_cts, baseline = sync do
      raise InvalidSample, 'inventory_changed' unless generation == @generation

      reconcile_inventory(attrs, new_cts)
      [host.snapshot, containers.map(&:snapshot), @baseline_started]
    end
    running = measured_cts.select(&:running?)
    runs = running.to_h { |ct| [ct.ident, init_start_time(ct.init_pid)] }
    running.each do |ct|
      if ct.init_start_time_ticks && ct.init_start_time_ticks != runs[ct.ident]
        ct.reset_measurements
      end
      ct.init_start_time_ticks = runs[ct.ident]
    end

    # Acquisition is outside the publication lock. These are the ordinary
    # Host/Container/Measurement objects, copied with their existing history.
    measured_host.measure(subsystems)
    running.each do |ct|
      ct.measure(measured_host, subsystems)
      raise InvalidSample, ct.current_measurement.core_error if ct.current_measurement.core_error
    end
    mem = OsCtl::Lib::MemInfo.new
    lavg = OsCtl::Lib::LoadAvg.new
    validate_host_inputs(mem, lavg)
    loads = running.empty? ? {} : OsCtl::Lib::LoadAvgReader.read_for(running)
    running.each do |ct|
      entry = loads[ct.ident]
      raise InvalidSample, 'missing_container_load' unless entry && valid_load?(entry.averages)
    end
    unless running_identities(attrs) == running_identities(read_inventory)
      raise InvalidSample, 'inventory_changed'
    end

    running.each do |ct|
      raise InvalidSample, 'run_changed' unless runs[ct.ident] == init_start_time(ct.init_pid)
    end

    reason = measured_host.core_delta_error || running.filter_map(&:core_delta_error).first
    if reason || baseline.nil? || baseline >= started
      reason ||= baseline.nil? ? 'initial_baseline' : 'invalid_window'
      measured_host.reset_measurements(keep_current: true)
      measured_cts.each { |ct| ct.reset_measurements(keep_current: true) }
      baseline = nil
    end
    snapshots = MODES.to_h do |projection_mode|
      [projection_mode, project(measured_host, running, mem, lavg, loads, projection_mode, reason)]
    end
    completed = monotonic_ns
     = (started, completed, reason, baseline)
    snapshots.each_value { |snapshot| snapshot.update() }
    sync do
      raise InvalidSample, 'inventory_changed' unless generation == @generation

      @host = measured_host
      @containers = measured_cts
      rebuild_index
      @baseline_started = started
      @snapshots = snapshots
    end
  rescue StandardError => e
    reason = e.is_a?(InvalidSample) ? e.message : 'required_read_failed'
    completed = monotonic_ns
    sync do
      host.reset_measurements
      containers.each(&:reset_measurements)
      @baseline_started = nil
      @snapshots = MODES.to_h { |m| [m, invalid_snapshot(started, completed, reason)] }
    end
  end
end

#monotonic_ns ⇒ Object (protected)



231
232
233
# File 'lib/osctl/cli/top/model.rb', line 231

def monotonic_ns
  Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
end

#netif_enabled? ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/osctl/cli/top/model.rb', line 33

def netif_enabled?
  collectors[:netif]
end

#new_container(attrs) ⇒ Object (protected)



259
260
261
# File 'lib/osctl/cli/top/model.rb', line 259

def new_container(attrs)
  Top::Container.new(attrs, enable_netif: netif_enabled?, enable_zfs: zfs_enabled?)
end

#open ⇒ Object (protected)



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/osctl/cli/top/model.rb', line 235

def open
  @client = OsCtl::Client.new
  client.open
  @subsystems = client.cmd_data!(:group_cgsubsystems) if OsCtl::Lib::CGroup.v1?
  @containers = read_inventory.map { |attrs| new_container(attrs) }
  rebuild_index
  return unless netif_enabled?

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

#prepare_new_containers(attrs) ⇒ Object (protected)



295
296
297
298
299
300
301
302
# File 'lib/osctl/cli/top/model.rb', line 295

def prepare_new_containers(attrs)
  known = sync { containers.map(&:ident) }
  attrs.reject { |ct| known.include?("#{ct[:pool]}:#{ct[:id]}") }.to_h do |ct_attrs|
    ct = new_container(ct_attrs)
    ct.netifs = read_netifs(client, pool: ct.pool, id: ct.id) if netif_enabled?
    [ct.ident, ct]
  end
end

#project(measured_host, running, mem, lavg, loads, projection_mode, reason) ⇒ Object (protected)



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/osctl/cli/top/model.rb', line 344

def project(measured_host, running, mem, lavg, loads, projection_mode, reason)
  host_result = reason ? result_for(measured_host, projection_mode, reason) : measured_host.result(projection_mode, mem, lavg)
  host_result[:memory] = mem.used * 1024
  host_result[:nproc] = lavg.total
  host_cpu = reason.nil? ? measured_host.cpu_result : nil
  sum_ct_cpu_hz = 0
  cts = running.map do |ct|
    result = result_for(ct, projection_mode, reason)
    %i[tx rx zfsio].each { |k| host_result[k] = nil if result[k].nil? }
    update_host_result(host_result, result)
    row = {
      pool: ct.pool, id: ct.id, cpu_package_inuse: ct.cpu_package_inuse,
      init_pid: ct.init_pid, init_start_time_ticks: ct.init_start_time_ticks,
      valid: reason.nil?, invalid_reason: reason, loadavg: loads.fetch(ct.ident).averages
    }.merge(result)
    if projection_mode == :realtime
      hz = reason.nil? ? result[:cpu_user_hz] + result[:cpu_system_hz] : nil
      sum_ct_cpu_hz += hz if hz
      row[:cpu_usage] = hz && calc_cpu_usage(hz, host_cpu.total)
    end
    row
  end
  host_row = {
    pool: nil, id: measured_host.id, init_pid: nil, init_start_time_ticks: nil,
    valid: reason.nil?, invalid_reason: reason, loadavg: lavg.to_a
  }.merge(host_result)
  if projection_mode == :realtime
    host_row[:cpu_usage] = host_cpu && calc_cpu_usage(host_cpu.total_used - sum_ct_cpu_hz, host_cpu.total)
  end
  cts << host_row
  {
    cpu: host_cpu && calc_host_cpu_usage(host_cpu), loadavg: lavg.to_a,
    memory: %i[total used free buffers cached swap_total swap_used swap_free].to_h { |k| [k, mem.public_send(k) * 1024] },
    zfs: measured_host.zfs_result, containers: cts
  }
end

#read_inventory ⇒ Object (protected)

Raises:



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/osctl/cli/top/model.rb', line 267

def read_inventory
  rows = client.cmd_data!(:ct_list, read_hostname: false)
  raise InvalidSample, 'invalid_inventory' unless rows.is_a?(Array)

  keys = []
  rows.each do |ct|
    unless ct.is_a?(Hash) && %i[pool id state].all? { |k| ct[k].is_a?(String) && !ct[k].empty? }
      raise InvalidSample, 'invalid_inventory'
    end

    key = [ct[:pool], ct[:id]]
    raise InvalidSample, 'invalid_inventory' if keys.include?(key)

    keys << key
    next unless ct[:state] == 'running'
    unless ct[:group_path].is_a?(String) && !ct[:group_path].empty? && ct[:init_pid].is_a?(Integer) && ct[:init_pid] > 0
      raise InvalidSample, 'invalid_inventory'
    end
  end
  rows
end

#read_netifs(lookup, **opts) ⇒ Object (protected)



263
264
265
# File 'lib/osctl/cli/top/model.rb', line 263

def read_netifs(lookup, **opts)
  lookup.cmd_data!(:netif_list, **opts).map { |attrs| Top::Container::NetIf.new(attrs) }
end

#rebuild_index ⇒ Object (protected)



314
315
316
317
# File 'lib/osctl/cli/top/model.rb', line 314

def rebuild_index
  @index = OsCtl::Lib::Index.new(&:ident)
  containers.each { |ct| index << ct }
end

#reconcile_inventory(attrs, prepared) ⇒ Object (protected)



304
305
306
307
308
309
310
311
312
# File 'lib/osctl/cli/top/model.rb', line 304

def reconcile_inventory(attrs, prepared)
  @containers = attrs.map do |ct_attrs|
    ident = "#{ct_attrs[:pool]}:#{ct_attrs[:id]}"
    ct = index[ident] || prepared.fetch(ident)
    ct.update(ct_attrs)
    ct
  end
  rebuild_index
end

#remove_ct(ct) ⇒ Object



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

def remove_ct(ct)
  sync do
    current = index[ct.ident]
    if current
      containers.delete(current)
      index.delete(current)
      changed!
    end
  end
end

#remove_pool(pool) ⇒ Object



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

def remove_pool(pool)
  return unless zfs_enabled?

  sync do
    host.pools.delete(pool)
    changed!
  end
  iostat.remove_pool(pool) if iostat
end

#result_for(ct, projection_mode, reason) ⇒ Object (protected)



381
382
383
384
385
386
387
# File 'lib/osctl/cli/top/model.rb', line 381

def result_for(ct, projection_mode, reason)
  return ct.result(projection_mode) unless reason

  ct.current_measurement.data.to_h do |k, v|
    [k, %i[memory memory_limit cpu_limit nproc].include?(k) ? v : nil]
  end
end

#running_identities(rows) ⇒ Object (protected)



289
290
291
292
293
# File 'lib/osctl/cli/top/model.rb', line 289

def running_identities(rows)
  rows.select { |ct| ct[:state] == 'running' }.map do |ct|
    ct.values_at(:pool, :id, :group_path, :init_pid)
  end.sort
end

#sample_metadata(started, completed, reason, baseline) ⇒ Object (protected)



389
390
391
392
393
394
395
396
397
# File 'lib/osctl/cli/top/model.rb', line 389

def (started, completed, reason, baseline)
  {
    top_schema: 1, epoch: @epoch, sequence: @sequence, collectors:,
    sample_started_monotonic_ns: started, sample_completed_monotonic_ns: completed,
    measurement_window_started_monotonic_ns: baseline,
    measurement_window_completed_monotonic_ns: completed,
    complete: reason.nil?, invalid_reason: reason
  }
end

#setup ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/osctl/cli/top/model.rb', line 41

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

#stop ⇒ Object



52
53
54
# File 'lib/osctl/cli/top/model.rb', line 52

def stop
  iostat.stop if iostat
end

#sync ⇒ Object



223
224
225
# File 'lib/osctl/cli/top/model.rb', line 223

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

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



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/osctl/cli/top/model.rb', line 416

def update_host_result(host_result, ct_result)
  ct_result.each do |k, v|
    next if host_result[k].nil? || v.nil?

    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

#valid_load?(values) ⇒ Boolean (protected)

Returns:

  • (Boolean)


333
334
335
# File 'lib/osctl/cli/top/model.rb', line 333

def valid_load?(values)
  values.is_a?(Array) && values.size == 3 && values.all? { |v| v.is_a?(Numeric) && v.finite? && v >= 0 }
end

#validate_host_inputs(mem, lavg) ⇒ Object (protected)

Raises:



337
338
339
340
341
342
# File 'lib/osctl/cli/top/model.rb', line 337

def validate_host_inputs(mem, lavg)
  values = %i[total used free buffers cached swap_total swap_used swap_free].map { |k| mem.public_send(k) }
  return if values.all? { |v| v.is_a?(Numeric) && v.finite? && v >= 0 } && valid_load?(lavg.to_a)

  raise InvalidSample, 'invalid_host_value'
end

#with_client ⇒ Object (protected)



249
250
251
252
253
254
255
256
257
# File 'lib/osctl/cli/top/model.rb', line 249

def with_client
  lookup = OsCtl::Client.new
  begin
    lookup.open
    yield lookup
  ensure
    lookup.close
  end
end

#zfs_enabled? ⇒ Boolean

Returns:

  • (Boolean)


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

def zfs_enabled?
  collectors[:zfs]
end