Class: OsCtl::Lib::CGroup::PathReader::V1
- Defined in:
- lib/libosctl/cgroup/path_reader.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
protected
Returns the value of attribute path.
-
#subsystems ⇒ Object
readonly
protected
Returns the value of attribute subsystems.
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(subsystems, path) ⇒ V1
constructor
A new instance of V1.
- #list_available_params ⇒ Object
- #parse_subsystem(param) ⇒ Object protected
- #read_cgparam(subsys_name, group_path, param) ⇒ Object protected
- #read_cpu_limit ⇒ Object
- #read_memory_limit ⇒ Integer
- #read_memory_usage ⇒ Integer
- #read_params(params) ⇒ Object
- #read_stats_param(field, precise) ⇒ Object
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(subsystems, path) ⇒ V1
Returns a new instance of V1.
40 41 42 43 44 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 40 def initialize(subsystems, path) super() @subsystems = subsystems @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly, protected)
Returns the value of attribute path.
239 240 241 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 239 def path @path end |
#subsystems ⇒ Object (readonly, protected)
Returns the value of attribute subsystems.
239 240 241 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 239 def subsystems @subsystems end |
Instance Method Details
#list_available_params ⇒ Object
205 206 207 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 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 205 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)
247 248 249 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 247 def parse_subsystem(param) param.split('.').first end |
#read_cgparam(subsys_name, group_path, param) ⇒ Object (protected)
241 242 243 244 245 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 241 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_cpu_limit ⇒ Object
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 153 def read_cpu_limit limit_path = if path.end_with?('/user-owned') path.split('/')[0..-2].join('/') else path end quota = read_cgparam(:cpu, limit_path, 'cpu.cfs_quota_us') period = read_cgparam(:cpu, limit_path, 'cpu.cfs_period_us') return :none if quota == '-1' (quota.to_i / period.to_i) * 100 end |
#read_memory_limit ⇒ Integer
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 134 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 :none end |
#read_memory_usage ⇒ Integer
129 130 131 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 129 def read_memory_usage read_cgparam(:memory, path, 'memory.memsw.usage_in_bytes').to_i end |
#read_params(params) ⇒ Object
168 169 170 171 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 203 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 168 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
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/libosctl/cgroup/path_reader.rb', line 46 def read_stats_param(field, precise) case field when :memory @memory_usage ||= read_memory_usage Cli::Presentable.new(@memory_usage, formatted: precise ? nil : humanize_data(@memory_usage)) 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 @memory_limit ||= read_memory_limit @memory_usage ||= read_memory_usage 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 :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 :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 :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( :pids, path, 'pids.current' ).to_i end end |