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'
Class Method Summary collapse
-
.freeze_tree(path) ⇒ Object
Freeze cgroup at path.
-
.inherit_param(base, cgroup, param) ⇒ Object
Inherit cgroup parameter from the parent cgroup.
-
.init_cgroup(type, base, cgroup) ⇒ Object
Initialize cgroup after it was created.
-
.mkpath(type, path, chown: nil, attach: false) ⇒ Boolean
Create CGroup a path, optionally chowning the last CGroup or attaching the current process into it.
-
.real_subsystem(subsys) ⇒ Object
Convert a single subsystem name to the mountpoint name, because some CGroup subsystems are mounted in a shared mountpoint.
-
.rmpath(subsystem, path) ⇒ Object
Remove cgroup path.
-
.rmpath_all(path) ⇒ Object
Remove cgroup path in all subsystems.
- .set_param(path, value) ⇒ Object
-
.subsystems ⇒ Array<String>
Returns a list of mounted CGroup subsystems on the system.
-
.thaw_tree(path) ⇒ Object
Thaw all frozen cgroups under path.
Class Method Details
.freeze_tree(path) ⇒ Object
Freeze cgroup at path
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/osctld/cgroup.rb', line 172 def self.freeze_tree(path) abs_path = File.join(FS, real_subsystem('freezer'), path) state = File.join(abs_path, 'freezer.state') begin File.open(state, 'w') { |f| f.write('FROZEN') } rescue SystemCallError => e log(:warn, "Unable to freeze #{abs_path}: #{e.} (#{e.class})") end 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.
114 115 116 117 118 119 120 121 122 |
# File 'lib/osctld/cgroup.rb', line 114 def self.inherit_param(base, cgroup, param) v = File.read(File.join(cgroup, param)).strip return v unless v.empty? fail "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_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.
95 96 97 98 99 100 101 102 |
# File 'lib/osctld/cgroup.rb', line 95 def self.init_cgroup(type, base, cgroup) case type when 'cpuset' inherit_param(base, cgroup, 'cpuset.cpus') inherit_param(base, cgroup, 'cpuset.mems') set_param(File.join(cgroup, 'cgroup.clone_children'), ['1']) end end |
.mkpath(type, path, chown: nil, attach: 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`,
36 37 38 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 |
# File 'lib/osctld/cgroup.rb', line 36 def self.mkpath(type, path, chown: nil, attach: false) base = File.join(FS, type) tmp = [] created = false path.each do |name| tmp << name cgroup = File.join(base, *tmp) next if Dir.exist?(cgroup) # Prevent an error if multiple processes attempt to create this cgroup # at a time begin Dir.mkdir(cgroup) created = true rescue Errno::EEXIST created = false next end init_cgroup(type, base, cgroup) end cgroup = File.join(base, *path) if chown File.chown(chown, chown, cgroup) if type == 'unified' %w(cgroup.procs cgroup.threads cgroup.subtree_control).each do |f| File.chown(chown, chown, File.join(cgroup, f)) end end end if attach ['tasks', 'cgroup.procs'].each do |tasks| tasks_path = File.join(cgroup, tasks) next unless File.exist?(tasks_path) File.open(tasks_path, 'a') do |f| f.write("#{Process.pid}\n") end end end created end |
.real_subsystem(subsys) ⇒ Object
Convert a single subsystem name to the mountpoint name, because some CGroup subsystems are mounted in a shared mountpoint.
11 12 13 14 15 |
# File 'lib/osctld/cgroup.rb', line 11 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 |
.rmpath(subsystem, path) ⇒ Object
Remove cgroup path
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/osctld/cgroup.rb', line 146 def self.rmpath(subsystem, path) abs_path = File.join(FS, subsystem, path) # Remove subdirectories recursively Dir.entries(abs_path).each do |dir| next if dir == '.' || dir == '..' next unless Dir.exist?(File.join(abs_path, dir)) rmpath(subsystem, File.join(path, dir)) end # Remove directory Dir.rmdir(abs_path) rescue Errno::ENOENT # pass end |
.rmpath_all(path) ⇒ Object
Remove cgroup path in all subsystems
166 167 168 |
# File 'lib/osctld/cgroup.rb', line 166 def self.rmpath_all(path) subsystems.each { |subsys| rmpath(subsys, path) } end |
.set_param(path, value) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/osctld/cgroup.rb', line 124 def self.set_param(path, value) raise CGroupFileNotFound.new(path, value) unless File.exist?(path) value.each do |v| log(:info, :cgroup, "Set #{path}=#{v}") begin File.write(path, v.to_s) rescue => e log( :warn, :cgroup, "Unable to set #{path}=#{v}: #{e.}" ) end end end |
.subsystems ⇒ Array<String>
Returns a list of mounted CGroup subsystems on the system
19 20 21 |
# File 'lib/osctld/cgroup.rb', line 19 def self.subsystems Dir.entries(FS) - ['.', '..'] end |
.thaw_tree(path) ⇒ Object
Thaw all frozen cgroups under path
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/osctld/cgroup.rb', line 185 def self.thaw_tree(path) abs_path = File.join(FS, real_subsystem('freezer'), path) Dir.entries(abs_path).each do |dir| next if dir == '.' || dir == '..' next unless Dir.exist?(File.join(abs_path, dir)) thaw_tree(File.join(path, dir)) end state = File.join(abs_path, 'freezer.state') begin if File.read(state).strip == 'FROZEN' log(:info, "Thawing #{abs_path}") File.open(state, 'w') { |f| f.write('THAWED') } end rescue SystemCallError => e log(:warn, "Unable to thaw #{abs_path}: #{e.} (#{e.class})") end end |