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

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



224
225
226
227
228
# File 'lib/libosctl/cgroup/path_reader.rb', line 224

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.



375
376
377
# File 'lib/libosctl/cgroup/path_reader.rb', line 375

def cg_root
  @cg_root
end

#pathObject (readonly, protected)

Returns the value of attribute path.



375
376
377
# File 'lib/libosctl/cgroup/path_reader.rb', line 375

def path
  @path
end

Instance Method Details

#list_available_paramsObject



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
# File 'lib/libosctl/cgroup/path_reader.rb', line 345

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 & 0400) != 0400

    params << v
  end

  params.sort!
  params
end

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



377
378
379
380
381
# File 'lib/libosctl/cgroup/path_reader.rb', line 377

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_statHash

Returns cpu usage in microseconds.

Returns:

  • (Hash)

    cpu usage in microseconds



297
298
299
300
301
302
303
304
305
# File 'lib/libosctl/cgroup/path_reader.rb', line 297

def read_cpu_stat
  params = Hash[read_cgparam(path, 'cpu.stat').strip.split("\n").map(&: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)


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/libosctl/cgroup/path_reader.rb', line 277

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

  if raw_v == unlimited
    meminfo.total * 1024
  else
    raw_v.to_i
  end
end

#read_params(params) ⇒ Hash

Returns:

  • (Hash)


308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/libosctl/cgroup/path_reader.rb', line 308

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



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
# File 'lib/libosctl/cgroup/path_reader.rb', line 230

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

  when :kmemory
    nil

  when :memory_pct
    limit = read_memory_limit
    usage = read_cgparam(path, 'memory.current').to_i
    t = usage.to_f / 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 :nproc
    read_cgparam(path, 'pids.current').to_i

  else
    nil
  end
end