Module: OsCtld::CGroup
- Includes:
- OsCtl::Lib::Utils::Log
- Defined in:
- lib/osctld/cgroup.rb,
lib/osctld/cgroup/param.rb
Defined Under Namespace
Classes: ContainerParams, Param, Params
Constant Summary collapse
- FS =
'/sys/fs/cgroup'.freeze
- ROOT_GROUP =
'osctl'.freeze
- DELEGATE_FILES =
File.read('/sys/kernel/cgroup/delegate').strip.split
- MUTEX =
Mutex.new
Class Method Summary collapse
-
.abs_cgroup_path(subsys, *path) ⇒ String
Return an absolute path to a cgroup.
-
.abs_cgroup_path_exist?(subsys, *path) ⇒ Boolean
Check if cgroup exists.
-
.attach_to(type, path, pid: nil, debug: false) ⇒ Object
Attach process to a cgroup.
-
.attach_to_all(path, pid: nil) ⇒ Object
Attach process to a cgroup in all subsystems.
- .available_controllers(cgroup) ⇒ Array<String>
-
.create(cgroup, delegate:, type:, base:) ⇒ Boolean
Create cgroup.
-
.delegate_available_controllers(cgroup) ⇒ Object
Enable all available controllers on cgroup.
-
.exist?(abs_path) ⇒ Boolean
Check if cgroup at path exists.
-
.freeze_tree(path) ⇒ Object
Freeze cgroup at path.
-
.get_cgroup_pids(subsys, path) ⇒ Array<Integer>
Get a list of processes in a cgroup.
-
.get_process_cgroups(pid) ⇒ Hash<String, String>
Get process cgroups, v1-only.
-
.inherit_param(base, cgroup, param) ⇒ Object
Inherit cgroup parameter from the parent cgroup.
- .init ⇒ Object
-
.init_cgroup(type, base, cgroup) ⇒ Object
Initialize cgroup after it was created.
-
.mkpath(type, path, chown: nil, attach: false, leaf: true, pid: nil, debug: false) ⇒ Boolean
Create CGroup a path, optionally chowning the last CGroup or attaching the current process into it.
-
.mkpath_all(path, chown: nil, attach: false, leaf: true, pid: nil, debug: false) ⇒ Object
Create cgroup path in all subsystems, see selfself.mkpath.
-
.real_subsystem(subsys) ⇒ Object
Convert a single subsystem name to the mountpoint name, because some CGroup subsystems are mounted in a shared mountpoint.
-
.reset_subtree_control(cgroup) ⇒ Object
Remove controller delegation from a cgroup.
-
.rmpath(subsystem, path) ⇒ Object
Remove cgroup path.
-
.rmpath_all(path) ⇒ Object
Remove cgroup path in all subsystems.
- .set_param(path, value) ⇒ Boolean
- .subsystem_paths ⇒ Hash<Symbol, String>
-
.subsystems ⇒ Array<String>
Returns a list of mounted CGroup subsystems on the system.
-
.subtree_control(cgroup) ⇒ Array<String>
Return controllers from ‘cgroup.subtree_control`.
- .sync(&block) ⇒ Object
-
.thaw_tree(path) ⇒ Object
Thaw all frozen cgroups under path.
- .v1? ⇒ Boolean
- .v2? ⇒ Boolean
-
.version ⇒ 1, 2
Cgroup hierarchy version.
Class Method Details
.abs_cgroup_path(subsys, *path) ⇒ String
Return an absolute path to a cgroup
71 72 73 74 75 76 77 |
# File 'lib/osctld/cgroup.rb', line 71 def self.abs_cgroup_path(subsys, *path) if v1? File.join(FS, real_subsystem(subsys), *path) else File.join(FS, *path) end end |
.abs_cgroup_path_exist?(subsys, *path) ⇒ Boolean
Check if cgroup exists
83 84 85 |
# File 'lib/osctld/cgroup.rb', line 83 def self.abs_cgroup_path_exist?(subsys, *path) exist?(abs_cgroup_path(subsys, *path)) end |
.attach_to(type, path, pid: nil, debug: false) ⇒ Object
Attach process to a cgroup
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/osctld/cgroup.rb', line 192 def self.attach_to(type, path, pid: nil, debug: false) cgroup = File.join(abs_cgroup_path(type), *path) attached = false attach_pid = pid || Process.pid ['cgroup.procs', 'tasks'].each do |tasks| begin File.open(File.join(cgroup, tasks), 'w') do |f| f.puts(attach_pid) end rescue Errno::ENOENT next end if debug log(:debug, :cgroup, "Attached PID #{attach_pid} to cgroup #{cgroup}") end attached = true break end unless attached raise "unable to attach pid #{attach_pid} to cgroup #{cgroup.inspect}" end nil end |
.attach_to_all(path, pid: nil) ⇒ Object
Attach process to a cgroup in all subsystems
225 226 227 228 229 |
# File 'lib/osctld/cgroup.rb', line 225 def self.attach_to_all(path, pid: nil) subsystems.each do |subsys| attach_to(subsys, path, pid:) end end |
.available_controllers(cgroup) ⇒ Array<String>
282 283 284 |
# File 'lib/osctld/cgroup.rb', line 282 def self.available_controllers(cgroup) File.read(File.join(cgroup, 'cgroup.controllers')).strip.split end |
.create(cgroup, delegate:, type:, base:) ⇒ Boolean
Create cgroup
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/osctld/cgroup.rb', line 166 def self.create(cgroup, delegate:, type:, base:) created = false sync do begin Dir.mkdir(cgroup) created = true rescue Errno::EEXIST created = false end if created && v2? && delegate CGroup.delegate_available_controllers(cgroup) end init_cgroup(type, base, cgroup) if created end created end |
.delegate_available_controllers(cgroup) ⇒ Object
Enable all available controllers on cgroup
272 273 274 275 276 277 278 |
# File 'lib/osctld/cgroup.rb', line 272 def self.delegate_available_controllers(cgroup) cmd = available_controllers(cgroup).map do |controller| "+#{controller}" end.join(' ') File.write(File.join(cgroup, 'cgroup.subtree_control'), cmd) end |
.exist?(abs_path) ⇒ Boolean
Check if cgroup at path exists
90 91 92 |
# File 'lib/osctld/cgroup.rb', line 90 def self.exist?(abs_path) Dir.exist?(abs_path) && File.exist?(File.join(abs_path, 'cgroup.procs')) end |
.freeze_tree(path) ⇒ Object
Freeze cgroup at path
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 |
# File 'lib/osctld/cgroup.rb', line 428 def self.freeze_tree(path) abs_path = abs_cgroup_path('freezer', path) if v1? state = File.join(abs_path, 'freezer.state') begin File.write(state, 'FROZEN') rescue SystemCallError => e log(:warn, "Unable to freeze #{abs_path}: #{e.} (#{e.class})") end else state = File.join(abs_path, 'cgroup.freeze') begin File.write(state, '1') rescue SystemCallError => e log(:warn, "Unable to freeze #{abs_path}: #{e.} (#{e.class})") end end end |
.get_cgroup_pids(subsys, path) ⇒ Array<Integer>
Get a list of processes in a cgroup
413 414 415 416 417 418 419 420 421 422 423 424 |
# File 'lib/osctld/cgroup.rb', line 413 def self.get_cgroup_pids(subsys, path) pids = [] File.open(File.join(abs_cgroup_path(subsys, path), 'cgroup.procs')) do |f| f.each_line do |line| pid = line.strip.to_i pids << pid if pid > 0 end end pids end |
.get_process_cgroups(pid) ⇒ Hash<String, String>
Get process cgroups, v1-only
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 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/osctld/cgroup.rb', line 363 def self.get_process_cgroups(pid) raise 'CGroup.get_process_cgroups is for cgroup v1 only' unless CGroup.v1? ret = {} File.open(File.join('/proc', pid.to_s, 'cgroup')) do |f| f.each_line do |line| s = line.strip # Hierarchy ID colon = s.index(':') next if colon.nil? s = s[(colon + 1)..] # Controllers colon = s.index(':') next if colon.nil? subsystems = if colon == 0 'unified' else s[0..(colon - 1)].split(',').map do |subsys| # Remove name= from named controllers if (eq = subsys.index('=')) subsys[(eq + 1)..] else subsys end end.join(',') end s = s[(colon + 1)..] # Path next if s.nil? path = s ret[subsystems] = path end end ret end |
.inherit_param(base, cgroup, param) ⇒ Object
Inherit cgroup parameter from the parent cgroup
The parameter is considered to be set if it isn’t empty. If the parent cgroup does not have the parameter set, it is inherited from its own parent and so on, all the way to the root cgroup defined by ‘base`. All parents will inherit the parameter as well.
260 261 262 263 264 265 266 267 268 |
# File 'lib/osctld/cgroup.rb', line 260 def self.inherit_param(base, cgroup, param) v = File.read(File.join(cgroup, param)).strip return v unless v.empty? raise "parameter #{param} not set in root cgroup #{base}" if base == cgroup v = inherit_param(base, File.dirname(cgroup), param) set_param(File.join(cgroup, param), [v]) v end |
.init ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/osctld/cgroup.rb', line 15 def self.init begin @version = File.read(RunState::CGROUP_VERSION).strip.to_i rescue Errno::ENOENT @version = 1 end @version = 1 unless [1, 2].include?(@version) @subsystems = if @version == 1 Dir.entries(FS) - ['.', '..'] else [''] end end |
.init_cgroup(type, base, cgroup) ⇒ Object
Initialize cgroup after it was created.
This is used to ensure that ‘cpuset` cgroups have parameters `cpuset.cpus` and `cpuset.mems` set.
239 240 241 242 243 244 245 246 247 248 |
# File 'lib/osctld/cgroup.rb', line 239 def self.init_cgroup(type, base, cgroup) case type when 'cpuset' if v1? inherit_param(base, cgroup, 'cpuset.cpus') inherit_param(base, cgroup, 'cpuset.mems') set_param(File.join(cgroup, 'cgroup.clone_children'), ['1']) end end end |
.mkpath(type, path, chown: nil, attach: false, leaf: true, pid: nil, debug: false) ⇒ Boolean
Create CGroup a path, optionally chowning the last CGroup or attaching the current process into it.
For example, ‘path` `[’osctl’, ‘subgroup’, ‘subsubgroup’]‘ will create `osctl/subgroup/subsubgroup` in the chosen subsystem. If `chown` or `attach` is set, it has an effect on the last group, i.e. `subsubgroup`,
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/osctld/cgroup.rb', line 110 def self.mkpath(type, path, chown: nil, attach: false, leaf: true, pid: nil, debug: false) base = abs_cgroup_path(type) tmp = [] created = false path.each_with_index do |name, i| tmp << name cgroup = File.join(base, *tmp) created = create( cgroup, delegate: i + 1 < path.length || (!leaf && !attach), type:, base: ) end if chown cgroup = File.join(base, *path) File.chown(chown, chown, cgroup) File.chown(chown, chown, File.join(cgroup, 'cgroup.procs')) if v2? || type == 'unified' DELEGATE_FILES.each do |f| File.chown(chown, chown, File.join(cgroup, f)) rescue Errno::ENOENT # ignore end end end attach_to(type, path, pid:, debug:) if attach created end |
.mkpath_all(path, chown: nil, attach: false, leaf: true, pid: nil, debug: false) ⇒ Object
Create cgroup path in all subsystems, see selfself.mkpath
154 155 156 157 158 |
# File 'lib/osctld/cgroup.rb', line 154 def self.mkpath_all(path, chown: nil, attach: false, leaf: true, pid: nil, debug: false) subsystems.each do |subsys| mkpath(subsys, path, chown:, attach:, pid:, debug:) end end |
.real_subsystem(subsys) ⇒ Object
Convert a single subsystem name to the mountpoint name, because some CGroup subsystems are mounted in a shared mountpoint.
47 48 49 50 51 52 |
# File 'lib/osctld/cgroup.rb', line 47 def self.real_subsystem(subsys) return 'cpu,cpuacct' if %w[cpu cpuacct].include?(subsys) return 'net_cls,net_prio' if %w[net_cls net_prio].include?(subsys) subsys end |
.reset_subtree_control(cgroup) ⇒ Object
Remove controller delegation from a cgroup
288 289 290 291 292 293 294 295 296 |
# File 'lib/osctld/cgroup.rb', line 288 def self.reset_subtree_control(cgroup) changes = subtree_control(cgroup).map do |controller| "-#{controller}" end return if changes.empty? File.write(File.join(cgroup, 'cgroup.subtree_control'), changes.join(' ')) end |
.rmpath(subsystem, path) ⇒ Object
Remove cgroup path
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/osctld/cgroup.rb', line 332 def self.rmpath(subsystem, path) abs_path = abs_cgroup_path(subsystem, path) # Remove subdirectories recursively Dir.entries(abs_path).each do |dir| next if ['.', '..'].include?(dir) next unless Dir.exist?(File.join(abs_path, dir)) rmpath(subsystem, File.join(path, dir)) end # Remove directory Dir.rmdir(abs_path) if CGroup.v2? # Remove pinned links for the cgroup Devices::V2::BpfProgramCache.prune_cgroup_links(abs_path) end rescue Errno::ENOENT # pass end |
.rmpath_all(path) ⇒ Object
Remove cgroup path in all subsystems
356 357 358 |
# File 'lib/osctld/cgroup.rb', line 356 def self.rmpath_all(path) subsystems.each { |subsys| rmpath(subsys, path) } end |
.set_param(path, value) ⇒ Boolean
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/osctld/cgroup.rb', line 306 def self.set_param(path, value) raise CGroupFileNotFound.new(path, value) unless File.exist?(path) ret = true value.each do |v| log(:info, :cgroup, "Set #{path}=#{v}") begin File.write(path, v.to_s) rescue StandardError => e log( :warn, :cgroup, "Unable to set #{path}=#{v}: #{e.}" ) ret = false end end ret end |
.subsystem_paths ⇒ Hash<Symbol, String>
61 62 63 64 65 |
# File 'lib/osctld/cgroup.rb', line 61 def self.subsystem_paths %i[cpu cpuacct memory pids].to_h do |subsys| [subsys, abs_cgroup_path(real_subsystem(subsys.to_s))] end end |
.subsystems ⇒ Array<String>
Returns a list of mounted CGroup subsystems on the system
56 57 58 |
# File 'lib/osctld/cgroup.rb', line 56 def self.subsystems @subsystems end |
.subtree_control(cgroup) ⇒ Array<String>
Return controllers from ‘cgroup.subtree_control`
301 302 303 |
# File 'lib/osctld/cgroup.rb', line 301 def self.subtree_control(cgroup) File.read(File.join(cgroup, 'cgroup.subtree_control')).strip.split end |
.sync(&block) ⇒ Object
496 497 498 499 500 501 502 |
# File 'lib/osctld/cgroup.rb', line 496 def self.sync(&block) if MUTEX.owned? block.call else MUTEX.synchronize(&block) end end |
.thaw_tree(path) ⇒ Object
Thaw all frozen cgroups under path
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/osctld/cgroup.rb', line 453 def self.thaw_tree(path) abs_path = abs_cgroup_path('freezer', path) begin entries = Dir.entries(abs_path) rescue Errno::ENOENT log(:warn, "Unable to thaw #{abs_path}: directory not found") return end entries.each do |dir| next if ['.', '..'].include?(dir) next unless Dir.exist?(File.join(abs_path, dir)) thaw_tree(File.join(path, dir)) end if v1? state = File.join(abs_path, 'freezer.state') begin if %w[FREEZING FROZEN].include?(File.read(state).strip) log(:info, "Thawing #{abs_path}") File.write(state, 'THAWED') end rescue SystemCallError => e log(:warn, "Unable to thaw #{abs_path}: #{e.} (#{e.class})") end else state = File.join(abs_path, 'cgroup.freeze') begin if File.read(state).strip == '1' log(:info, "Thawing #{abs_path}") File.write(state, '0') end rescue SystemCallError => e log(:warn, "Unable to thaw #{abs_path}: #{e.} (#{e.class})") end end end |
.v1? ⇒ Boolean
37 38 39 |
# File 'lib/osctld/cgroup.rb', line 37 def self.v1? @version == 1 end |
.v2? ⇒ Boolean
41 42 43 |
# File 'lib/osctld/cgroup.rb', line 41 def self.v2? @version == 2 end |
.version ⇒ 1, 2
Returns cgroup hierarchy version.
33 34 35 |
# File 'lib/osctld/cgroup.rb', line 33 def self.version @version end |