Class: OsCtl::Lib::CGroup::PathReader::V1

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(subsystems, path) ⇒ V1

Returns a new instance of V1.



33
34
35
36
37
# File 'lib/libosctl/cgroup/path_reader.rb', line 33

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

Instance Attribute Details

#pathObject (readonly, protected)

Returns the value of attribute path.



206
207
208
# File 'lib/libosctl/cgroup/path_reader.rb', line 206

def path
  @path
end

#subsystemsObject (readonly, protected)

Returns the value of attribute subsystems.



206
207
208
# File 'lib/libosctl/cgroup/path_reader.rb', line 206

def subsystems
  @subsystems
end

Instance Method Details

#list_available_paramsObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/libosctl/cgroup/path_reader.rb', line 172

def list_available_params
  params = []

  subsystems.each_value do |subsys_path|
    cgpath = File.join(subsys_path, 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
  end

  params.uniq!
  params.sort!
  params
end

#parse_subsystem(param) ⇒ Object (protected)



214
215
216
# File 'lib/libosctl/cgroup/path_reader.rb', line 214

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

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



208
209
210
211
212
# File 'lib/libosctl/cgroup/path_reader.rb', line 208

def read_cgparam(subsys_name, group_path, param)
  cache_param(group_path, param) do
    File.read(File.join(subsystems[subsys_name], group_path, param)).strip
  end
end

#read_memory_limitInteger

Returns:

  • (Integer)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/libosctl/cgroup/path_reader.rb', line 116

def read_memory_limit
  unlimited = 9_223_372_036_854_771_712

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

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

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

  meminfo.total * 1024
end

#read_memory_usageInteger

Returns:

  • (Integer)


111
112
113
# File 'lib/libosctl/cgroup/path_reader.rb', line 111

def read_memory_usage
  read_cgparam(:memory, path, 'memory.memsw.usage_in_bytes').to_i
end

#read_params(params) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/libosctl/cgroup/path_reader.rb', line 135

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(parse_subsystem(par.to_s).to_sym, 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



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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/libosctl/cgroup/path_reader.rb', line 39

def read_stats_param(field, precise)
  case field
  when :memory
    t = read_memory_usage
    Cli::Presentable.new(t, formatted: precise ? nil : humanize_data(t))

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

  when :memory_pct
    limit = read_memory_limit
    usage = read_memory_usage
    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
    all = read_cgparam(
      :cpuacct,
      path,
      'cpuacct.usage'
    ).to_i / 1_000

    user = read_cgparam(
      :cpuacct,
      path,
      'cpuacct.usage_user'
    ).to_i / 1_000

    sys = read_cgparam(
      :cpuacct,
      path,
      'cpuacct.usage_sys'
    ).to_i / 1_000

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

  when :cpu_hz, :cpu_user_hz, :cpu_system_hz
    read_cgparam(
      :cpuacct,
      path,
      'cpuacct.stat'
    ).split("\n").to_h do |line|
      type, hz = line.split
      [:"cpu_#{type}_hz", hz.to_i]
    end

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

  end
end