Class: OsCtl::Lib::CGroup::PathReader::V2

Inherits:
Base
  • Object
show all
Defined in:
lib/libosctl/cgroup/path_reader.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#cache

Instance Method Summary collapse

Methods inherited from Base

#cache_param, #meminfo, #read_stats

Methods included from Utils::Humanize

#break_interval, #format_long_duration, #format_percent, #format_short_duration, #humanize_data, #humanize_number, #humanize_percent, #humanize_time_ns, #humanize_time_us, #parse_data

Constructor Details

#initialize(cg_root, path) ⇒ V2

Returns a new instance of V2.

Parameters:

  • cg_root (String)

    path to CGroup mountpoint

  • path (String)

    CGroup path relative to cg_root



255
256
257
258
259
# File 'lib/libosctl/cgroup/path_reader.rb', line 255

def initialize(cg_root, path)
  super()
  @cg_root = cg_root
  @path = path
end

Instance Attribute Details

#cg_rootObject (readonly, protected)

Returns the value of attribute cg_root.



427
428
429
# File 'lib/libosctl/cgroup/path_reader.rb', line 427

def cg_root
  @cg_root
end

#pathObject (readonly, protected)

Returns the value of attribute path.



427
428
429
# File 'lib/libosctl/cgroup/path_reader.rb', line 427

def path
  @path
end

Instance Method Details

#list_available_paramsObject



396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/libosctl/cgroup/path_reader.rb', line 396

def list_available_params
  params = []

  cgpath = File.join(cg_root, path)

  begin
    entries = Dir.entries(cgpath)
  rescue Errno::ENOENT
    # the /osctl cgroup does not exist when there are no containers
    return params
  end

  entries.each do |v|
    next if %w[. .. notify_on_release release_agent tasks].include?(v)
    next if v.start_with?('cgroup.')

    st = File.stat(File.join(cgpath, v))
    next if st.directory?

    # Ignore files that do not have read by user permission
    next if (st.mode & 0o400) != 0o400

    params << v
  end

  params.sort!
  params
end

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



429
430
431
432
433
# File 'lib/libosctl/cgroup/path_reader.rb', line 429

def read_cgparam(group_path, param)
  cache_param(group_path, param) do
    File.read(File.join(cg_root, group_path, param)).strip
  end
end

#read_cpu_limitObject



344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/libosctl/cgroup/path_reader.rb', line 344

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

  quota, period = read_cgparam(limit_path, 'cpu.max').strip.split
  return :none if quota == 'max'

  (quota.to_i / period.to_i) * 100
end

#read_cpu_statHash

Returns cpu usage in microseconds.

Returns:

  • (Hash)

    cpu usage in microseconds



334
335
336
337
338
339
340
341
342
# File 'lib/libosctl/cgroup/path_reader.rb', line 334

def read_cpu_stat
  params = read_cgparam(path, 'cpu.stat').strip.split("\n").to_h(&:split)

  {
    all: params['usage_usec'].to_i,
    user: params['user_usec'].to_i,
    system: params['system_usec'].to_i
  }
end

#read_memory_limitInteger

Returns:

  • (Integer)


317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
# File 'lib/libosctl/cgroup/path_reader.rb', line 317

def read_memory_limit
  unlimited = 'max'

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

  raw_v = read_cgparam(limit_path, 'memory.max')
  return :none if raw_v == unlimited

  raw_v.to_i
end

#read_params(params) ⇒ Hash

Returns:

  • (Hash)


359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/libosctl/cgroup/path_reader.rb', line 359

def read_params(params)
  ret = {}

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

  params.each do |par|
    v_raw =
      begin
        read_cgparam(read_path, par.to_s)
      rescue Errno::ENOENT
        nil
      end

    if v_raw.nil?
      v_target = nil
    else
      v_int = v_raw.to_i

      v_target =
        if v_int.to_s == v_raw
          Cli::Presentable.new(v_int, formatted: v_raw, exported: v_raw)
        else
          v_raw
        end
    end

    ret[par.to_sym] = v_target
  end

  ret
end

#read_stats_param(field, precise) ⇒ Object



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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/libosctl/cgroup/path_reader.rb', line 261

def read_stats_param(field, precise)
  case field
  when :memory
    @memory_usage ||= read_cgparam(path, 'memory.current').to_i
    Cli::Presentable.new(@memory_usage, formatted: precise ? nil : humanize_data(@memory_usage))

  when :kmemory
    nil

  when :memory_limit
    @memory_limit ||= read_memory_limit
    t = @memory_limit == :none ? nil : @memory_limit
    t && Cli::Presentable.new(t, formatted: precise ? nil : humanize_data(t))

  when :memory_pct
    @memory_limit ||= read_memory_limit
    @memory_usage ||= read_cgparam(path, 'memory.current').to_i
    effective_limit = @memory_limit == :none ? meminfo.total * 1024 : @memory_limit
    t = @memory_usage.to_f / effective_limit * 100
    Cli::Presentable.new(t, formatted: precise ? nil : humanize_percent(t))

  when :cpu_us, :cpu_user_us, :cpu_system_us
    stat = read_cpu_stat

    {
      cpu_us: Cli::Presentable.new(
        stat[:all], formatted: precise ? nil : humanize_time_us(stat[:all])
      ),
      cpu_user_us: Cli::Presentable.new(
        stat[:user], formatted: precise ? nil : humanize_time_us(stat[:user])
      ),
      cpu_system_us: Cli::Presentable.new(
        stat[:system], formatted: precise ? nil : humanize_time_us(stat[:system])
      )
    }

  when :cpu_hz, :cpu_user_hz, :cpu_system_hz
    stat = read_cpu_stat

    {
      cpu_user_hz: stat[:user] / (1_000_000 / OsProcess::TICS_PER_SECOND),
      cpu_system_hz: stat[:system] / (1_000_000 / OsProcess::TICS_PER_SECOND)
    }

  when :cpu_limit
    cpu_limit = read_cpu_limit
    t = cpu_limit == :none ? nil : cpu_limit
    t && Cli::Presentable.new(t, formatted: precise ? nil : humanize_percent(t))

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

  end
end