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



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

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



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

def host
  @host
end

#indexObject (readonly, protected)

Returns the value of attribute index



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

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



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

def monitor
  @monitor
end

#nprocObject (readonly, protected)

Returns the value of attribute nproc



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

def nproc
  @nproc
end

#subsystemsObject (readonly, protected)

Returns the value of attribute subsystems



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

def subsystems
  @subsystems
end

Instance Method Details

#add_ct(pool, id) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/osctl/cli/top/model.rb', line 96

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



131
132
133
134
135
136
137
# File 'lib/osctl/cli/top/model.rb', line 131

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

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



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

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

#calc_host_cpu_usage(cpu) ⇒ Object (protected)



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

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



38
39
40
41
42
43
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
# File 'lib/osctl/cli/top/model.rb', line 38

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



123
124
125
# File 'lib/osctl/cli/top/model.rb', line 123

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

#has_ct?(pool, id) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/osctl/cli/top/model.rb', line 127

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

#measureObject



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

def measure
  host.measure(subsystems)

  sync do
    containers.each do |ct|
      next unless ct.running?
      ct.measure(subsystems)
    end
  end
end

#openObject (protected)



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

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



116
117
118
119
120
121
# File 'lib/osctl/cli/top/model.rb', line 116

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

#setupObject



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

def setup
  monitor.start
  @nproc = `nproc`.strip.to_i
  measure
end

#syncObject



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

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

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



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

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