Module: OsCtl::Cli::CGroupParams

Included in:
Container, Group, Top::Measurement, Tree
Defined in:
lib/osctl/cli/cgroup_params.rb

Constant Summary collapse

CGPARAM_FIELDS =
%i(
  subsystem
  parameter
  value
  group
  abs_path
)
CGPARAM_FILTERS =
%i(
  subsystem
)
CGPARAM_DEFAULT_FIELDS =
%i(
  parameter
  value
)
CGPARAM_STATS =
%i(
  memory
  memory_pct
  kmemory
  cpu_time
  cpu_user_time
  cpu_sys_time
  cpu_stat
  nproc
)

Instance Method Summary collapse

Instance Method Details

#cg_add_stats(client, data, path, params, precise) ⇒ Hash, Array

Add runtime stats from CGroup parameters to `data`

Parameters:

  • client (OsCtl::Client)
  • data (Hash, Array)

    hash/array to which the stats are added

  • path (String)

    path of the chosen group

  • params (Array)

    selected stat parameters

  • precise (Boolean)

    humanize parameter values?

Returns:

  • (Hash, Array)

    data extended with stats



335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/osctl/cli/cgroup_params.rb', line 335

def cg_add_stats(client, data, path, params, precise)
  subsystems = client.cmd_data!(:group_cgsubsystems)
  fields = CGPARAM_STATS & params

  if data.is_a?(::Hash)
    data.update(cg_read_stats(subsystems, path, fields, precise))
    data

  elsif data.is_a?(::Array)
    data.map do |v|
      v.update(cg_read_stats(subsystems, path.call(v), fields, precise))
    end
  end
end

#cg_read_stats(subsystems, path, params, precise) ⇒ Hash

Read select CGroup parameters

Parameters:

  • subsystems (Hash)

    subsystem => absolute path

  • path (String)

    path of chosen group, relative to the subsystem

  • params (Array)

    parameters to read

  • precise (Boolean)

    humanize parameter values?

Returns:

  • (Hash)

    parameter => value



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/osctl/cli/cgroup_params.rb', line 211

def cg_read_stats(subsystems, path, params, precise)
  ret = {}

  params.each do |field|
    begin
      v = case field
      when :memory
        t = read_memory_usage(subsystems[:memory], path)
        OsCtl::Lib::Cli::Presentable.new(t, formatted: precise ? nil : humanize_data(t))

      when :kmemory
        t = read_cgparam(
          subsystems[:memory],
          path,
          'memory.kmem.usage_in_bytes'
        ).to_i
        OsCtl::Lib::Cli::Presentable.new(t, formatted: precise ? nil : humanize_data(t))

      when :memory_pct
        limit = read_memory_limit(subsystems[:memory], path)
        usage = read_memory_usage(subsystems[:memory], path)
        t = usage.to_f / limit * 100
        OsCtl::Lib::Cli::Presentable.new(t, formatted: precise ? nil : humanize_percent(t))

      when :cpu_time
        t = read_cgparam(
          subsystems[:cpuacct],
          path,
          'cpuacct.usage'
        ).to_i
        OsCtl::Lib::Cli::Presentable.new(t, formatted: precise ? nil : humanize_time_ns(t))

      when :cpu_user_time
        t = read_cgparam(
          subsystems[:cpuacct],
          path,
          'cpuacct.usage_user'
        ).to_i
        OsCtl::Lib::Cli::Presentable.new(t, formatted: precise ? nil : humanize_time_ns(t))

      when :cpu_sys_time
        t = read_cgparam(
          subsystems[:cpuacct],
          path,
          'cpuacct.usage_sys'
        ).to_i
        OsCtl::Lib::Cli::Presentable.new(t, formatted: precise ? nil : humanize_time_ns(t))

      when :cpu_stat
        Hash[
          read_cgparam(
            subsystems[:cpuacct],
            path,
            'cpuacct.stat'
          ).split("\n").map do |line|
            type, hz = line.split(' ')
            [:"cpu_#{type}_hz", hz.to_i]
          end
        ]

      when :nproc
        read_cgparam(
          subsystems[:pids],
          path,
          'pids.current'
        ).to_i

      else
        nil
      end

      next if v.nil?

      if v.is_a?(Hash)
        ret.update(v)
      else
        ret[field] = v
      end

    rescue Errno::ENOENT
      ret[field] = nil
    end
  end

  ret
end

#do_cgparam_apply(cmd, cmd_opts) ⇒ Object



109
110
111
# File 'lib/osctl/cli/cgroup_params.rb', line 109

def do_cgparam_apply(cmd, cmd_opts)
  osctld_fmt(cmd, cmd_opts)
end

#do_cgparam_list(cmd, cmd_opts) ⇒ Object



33
34
35
36
37
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
# File 'lib/osctl/cli/cgroup_params.rb', line 33

def do_cgparam_list(cmd, cmd_opts)
  if opts[:list]
    puts CGPARAM_FIELDS.join("\n")
    return
  end

  fmt_opts = {layout: :columns}

  cmd_opts[:parameters] = args[1..-1] if args.count > 1
  cmd_opts[:subsystem] = opts[:subsystem].split(',') if opts[:subsystem]
  cmd_opts[:all] = true if opts[:all]
  fmt_opts[:header] = false if opts['hide-header']

  if opts[:output]
    cols = opts[:output].split(',').map(&:to_sym)

  elsif opts[:all]
    cols = %i(group) + CGPARAM_DEFAULT_FIELDS

  else
    cols = CGPARAM_DEFAULT_FIELDS
  end

  if i = cols.index(:value)
    cols[i] = {
      name: :value,
      label: 'VALUE',
      align: 'right',
      display: Proc.new do |values|
        values.map do |v|
          if gopts[:parsable] \
             || gopts[:json] \
             || (!v.is_a?(Integer) && /^\d+$/ !~ v)
            next v
          end

          humanize_data(v.to_i)
        end.join('; ')
      end
    }
  end

  osctld_fmt(
    cmd,
    cmd_opts,
    cols,
    fmt_opts
  )
end

#do_cgparam_replace(cmd, cmd_opts) ⇒ Object



113
114
115
116
117
# File 'lib/osctl/cli/cgroup_params.rb', line 113

def do_cgparam_replace(cmd, cmd_opts)
  osctld_fmt(cmd, cmd_opts.merge(
    parameters: JSON.parse(STDIN.read)['parameters'],
  ))
end

#do_cgparam_set(cmd, cmd_opts, params = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/osctl/cli/cgroup_params.rb', line 83

def do_cgparam_set(cmd, cmd_opts, params = nil)
  params ||= [{
    subsystem: parse_subsystem(args[1]),
    parameter: args[1],
    value: args[2..-1].map { |v| parse_data(v) },
  }]

  cmd_opts.update({
    parameters: params,
    append: opts[:append],
  })

  osctld_fmt(cmd, cmd_opts)
end

#do_cgparam_unset(cmd, cmd_opts, params = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/osctl/cli/cgroup_params.rb', line 98

def do_cgparam_unset(cmd, cmd_opts, params = nil)
  params ||= [{
    subsystem: parse_subsystem(args[1]),
    parameter: args[1],
  }]

  cmd_opts.update(parameters: params)

  osctld_fmt(cmd, cmd_opts)
end

#do_set_cpu_limit(cmd, cmd_opts) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/osctl/cli/cgroup_params.rb', line 119

def do_set_cpu_limit(cmd, cmd_opts)
  quota = args[1].to_f / 100 * opts[:period]

  do_cgparam_set(
    cmd,
    cmd_opts,
    [
      {
        subsystem: 'cpu',
        parameter: 'cpu.cfs_period_us',
        value: [opts[:period]],
      },
      {
        subsystem: 'cpu',
        parameter: 'cpu.cfs_quota_us',
        value: [quota.round],
      },
    ]
  )
end

#do_set_memory(set_cmd, unset_cmd, cmd_opts) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/osctl/cli/cgroup_params.rb', line 157

def do_set_memory(set_cmd, unset_cmd, cmd_opts)
  mem = parse_data(args[1])
  swap = parse_data(args[2]) if args[2]

  limits = [
    {
      subsystem: 'memory',
      parameter: 'memory.limit_in_bytes',
      value: [mem],
    },
  ]

  if swap
    limits << {
      subsystem: 'memory',
      parameter: 'memory.memsw.limit_in_bytes',
      value: [mem+swap],
    }

  else
    # When no swap limit is to be set, we have to remove existing swap
    # limits
    do_cgparam_unset(unset_cmd, cmd_opts, [{
      subsystem: 'memory',
      parameter: 'memory.memsw.limit_in_bytes',
    }])
  end

  do_cgparam_set(set_cmd, cmd_opts, limits)
end

#do_unset_cpu_limit(unset_cmd, cmd_opts) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/osctl/cli/cgroup_params.rb', line 140

def do_unset_cpu_limit(unset_cmd, cmd_opts)
  do_cgparam_unset(
    unset_cmd,
    cmd_opts,
    [
      {
        subsystem: 'cpu',
        parameter: 'cpu.cfs_period_us',
      },
      {
        subsystem: 'cpu',
        parameter: 'cpu.cfs_quota_us',
      },
    ]
  )
end

#do_unset_memory(unset_cmd, cmd_opts) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/osctl/cli/cgroup_params.rb', line 188

def do_unset_memory(unset_cmd, cmd_opts)
  do_cgparam_unset(
    unset_cmd,
    cmd_opts,
    [
      {
        subsystem: 'memory',
        parameter: 'memory.memsw.limit_in_bytes',
      },
      {
        subsystem: 'memory',
        parameter: 'memory.limit_in_bytes',
      },
    ]
  )
end

#parse_cgparamsObject (protected)



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# File 'lib/osctl/cli/cgroup_params.rb', line 360

def parse_cgparams
  opts[:cgparam].map do |v|
    parts = v.split('=')

    unless parts.count == 2
      fail "invalid cgparam '#{v}': expected <parameter>=<value>"
    end

    k, v = parts

    {
      subsystem: parse_subsystem(k),
      parameter: k,
      value: parse_data(v),
    }
  end
end

#parse_memory_stat(memory, path) ⇒ Object (protected)



351
352
353
354
355
356
357
358
# File 'lib/osctl/cli/cgroup_params.rb', line 351

def parse_memory_stat(memory, path)
  Hash[
    read_cgparam(memory, path, 'memory.stat').split("\n").map do |line|
      param, value = line.split
      [param.to_sym, value.to_i]
    end
  ]
end

#parse_subsystem(param) ⇒ Object (protected)



378
379
380
# File 'lib/osctl/cli/cgroup_params.rb', line 378

def parse_subsystem(param)
  param.split('.').first
end

#read_cgparam(subsys_path, group_path, param) ⇒ Object (protected)



382
383
384
# File 'lib/osctl/cli/cgroup_params.rb', line 382

def read_cgparam(subsys_path, group_path, param)
  File.read(File.join(subsys_path, group_path, param)).strip
end

#read_memory_limit(memory, path) ⇒ Integer

Parameters:

  • memory (String)

    absolute path to memory subsystem

  • path (String)

    path of chosen group, relative to the subsystem

Returns:

  • (Integer)


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/osctl/cli/cgroup_params.rb', line 310

def read_memory_limit(memory, path)
  unlimited = 9223372036854771712

  if path.end_with?('/user-owned')
    path = path.split('/')[0..-2].join('/')
  end

  v = read_cgparam(memory, path, 'memory.memsw.limit_in_bytes').to_i
  return v if v != unlimited

  v = read_cgparam(memory, path, 'memory.limit_in_bytes').to_i
  return v if v != unlimited

  # TODO: this could be optimised to read meminfo just once for all containers
  mi = OsCtl::Cli::MemInfo.new
  mi.total * 1024
end

#read_memory_usage(memory, path) ⇒ Integer

Parameters:

  • memory (String)

    absolute path to memory subsystem

  • path (String)

    path of chosen group, relative to the subsystem

Returns:

  • (Integer)


301
302
303
304
305
# File 'lib/osctl/cli/cgroup_params.rb', line 301

def read_memory_usage(memory, path)
  st = parse_memory_stat(memory, path)
  usage = read_cgparam(memory, path, 'memory.memsw.usage_in_bytes').to_i
  usage - st[:total_cache]
end