Module: OsCtl::Lib::CGroup

Defined in:
lib/libosctl/cgroup.rb

Defined Under Namespace

Classes: PathReader

Constant Summary collapse

DEFAULT_FS =
'/sys/fs/cgroup'.freeze
RUNSTATE_FS =
'/run/osctl/cgroup'.freeze
FS =

Legacy public-hierarchy path. Control-plane callers should use .fs.

DEFAULT_FS
RUNSTATE_VERSION =
'/run/osctl/cgroup.version'.freeze
INIT_MUTEX =
Mutex.new

Class Method Summary collapse

Class Method Details

.configuration ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/libosctl/cgroup.rb', line 27

def self.configuration
  cached = @configuration
  return cached if cached

  INIT_MUTEX.synchronize do
    @configuration ||= detect_configuration.freeze
  end
end

.detect_configuration ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/libosctl/cgroup.rb', line 47

def self.detect_configuration
  expected_version = runstate_version

  if expected_version
    selected = [RUNSTATE_FS, DEFAULT_FS].find do |path|
      usable_fs?(path, expected_version)
    end
    return [expected_version, selected] if selected
  end

  # A stale or missing runstate version must never pair one hierarchy's
  # version with another hierarchy's path. Fall back to the public mount
  # and derive both values from that same layout.
  [filesystem_version(DEFAULT_FS) || 1, DEFAULT_FS]
end

.detect_fs(version) ⇒ Object



63
64
65
66
67
# File 'lib/libosctl/cgroup.rb', line 63

def self.detect_fs(version)
  [RUNSTATE_FS, DEFAULT_FS].find do |path|
    usable_fs?(path, version)
  end || DEFAULT_FS
end

.filesystem_version(path) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/libosctl/cgroup.rb', line 76

def self.filesystem_version(path)
  return unless Dir.exist?(path)
  return 2 if File.exist?(File.join(path, 'cgroup.procs'))
  return 1 if Dir.children(path).any?

  nil
rescue SystemCallError
  nil
end

.fs ⇒ Object



23
24
25
# File 'lib/libosctl/cgroup.rb', line 23

def self.fs
  configuration.fetch(1)
end

.fs_version ⇒ Object



43
44
45
# File 'lib/libosctl/cgroup.rb', line 43

def self.fs_version
  filesystem_version(fs) || 1
end

.runstate_version ⇒ Object



36
37
38
39
40
41
# File 'lib/libosctl/cgroup.rb', line 36

def self.runstate_version
  version = Integer(File.read(RUNSTATE_VERSION).strip)
  version if [1, 2].include?(version)
rescue Errno::ENOENT, Errno::EACCES, ArgumentError
  nil
end

.usable_fs?(path, version) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
# File 'lib/libosctl/cgroup.rb', line 69

def self.usable_fs?(path, version)
  detected = filesystem_version(path)
  version.nil? ? !detected.nil? : detected == version
rescue SystemCallError
  false
end

.v1? ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/libosctl/cgroup.rb', line 15

def self.v1?
  version == 1
end

.v2? ⇒ Boolean

Returns:

  • (Boolean)


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

def self.v2?
  version == 2
end

.version ⇒ 1, 2

Returns cgroup hierarchy version.

Returns:

  • (1, 2) —

    cgroup hierarchy version



11
12
13
# File 'lib/libosctl/cgroup.rb', line 11

def self.version
  configuration.fetch(0)
end