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

Class Method Details

.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.

Parameters:

  • base (String)

    absolute path to the root cgroup

  • cgroup (String)

    absolute path of the created cgroup

  • param (String)

    parameter name



113
114
115
116
117
118
119
120
121
# File 'lib/osctld/cgroup.rb', line 113

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.

Parameters:

  • type (String)

    cgroup subsystem

  • base (String)

    absolute path to the root cgroup

  • cgroup (String)

    absolute path of the created cgroup



95
96
97
98
99
100
101
# 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')
  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`,

Parameters:

  • type (String)

    subsystem

  • path (Array<String>)

    paths to create

  • chown (Integer)

    chown the last group to `chown`:`chown`

  • attach (Boolean)

    attach the current process to the last group

Returns:

  • (Boolean)

    `true` if the last component was created, `false` if it already existed



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

Parameters:

  • subsystem (String)
  • path (String)

    path to remove, relative to the subsystem



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/osctld/cgroup.rb', line 145

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

Parameters:

  • path (String)

    path to remove, relative to subsystem



165
166
167
# File 'lib/osctld/cgroup.rb', line 165

def self.rmpath_all(path)
  subsystems.each { |subsys| rmpath(subsys, path) }
end

.set_param(path, value) ⇒ Object

Raises:



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/osctld/cgroup.rb', line 123

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.message}"
      )
    end
  end
end

.subsystemsArray<String>

Returns a list of mounted CGroup subsystems on the system

Returns:

  • (Array<String>)


19
20
21
# File 'lib/osctld/cgroup.rb', line 19

def self.subsystems
  Dir.entries(FS) - ['.', '..']
end