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



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

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_blkio_stats(subsystems, path, params) ⇒ Object

Read and accumulate BlkIO stats

Parameters:

  • subsystems (Hash)

    subsystem => absolute path

  • path (String)

    path of chosen group, relative to the subsystem

  • params (Array)

    parameters to read: `bytes`, `iops`



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/osctl/cli/cgroup_params.rb', line 293

def cg_blkio_stats(subsystems, path, params)
  file = {
    bytes: 'blkio.throttle.io_service_bytes',
    iops: 'blkio.throttle.io_serviced',
  }
  ret = {}

  (%i(bytes iops) & params).each do |param|
    r = w = 0

    read_cgparam(subsystems[:blkio], path, file[param]).split("\n").each do |line|
      if /^\d+:\d+ Read (\d+)$/ =~ line
        r += $1.to_i

      elsif /^\d+:\d+ Write (\d+)$/ =~ line
        w += $1.to_i
      end
    end

    ret[param] = {r: r, w: w}
  end

  ret
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



208
209
210
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
# File 'lib/osctl/cli/cgroup_params.rb', line 208

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)
        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
        Cli::Presentable.new(t, formatted: precise ? nil : humanize_data(t))

      when :cpu_time
        t = read_cgparam(
          subsystems[:cpuacct],
          path,
          'cpuacct.usage'
        ).to_i
        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
        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
        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



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

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

#do_cgparam_list(cmd, cmd_opts) ⇒ Object



30
31
32
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
# File 'lib/osctl/cli/cgroup_params.rb', line 30

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



110
111
112
113
114
# File 'lib/osctl/cli/cgroup_params.rb', line 110

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osctl/cli/cgroup_params.rb', line 80

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



95
96
97
98
99
100
101
102
103
104
# File 'lib/osctl/cli/cgroup_params.rb', line 95

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



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

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



154
155
156
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
# File 'lib/osctl/cli/cgroup_params.rb', line 154

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



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

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



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

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)



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

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)



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

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)



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

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

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



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

def read_cgparam(subsys_path, group_path, param)
  File.read(File.join(subsys_path, group_path, param)).strip
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)


321
322
323
324
325
# File 'lib/osctl/cli/cgroup_params.rb', line 321

def read_memory_usage(memory, path)
  st = parse_memory_stat(memory, path)
  to_sum = %i(rss shmem active_anon inactive_anon)
  to_sum.inject(0) { |sum, v| st[:"total_#{v}"] + sum }
end