class CgroupV1
Captures host memory limits and usage from a cgroup v1 filesystem.
Definitions
def self.supported?(cgroup_root = DEFAULT_CGROUP_ROOT)
Whether cgroup v1 memory metrics are available.
Signature
-
parameter
cgroup_rootString | Nil The root of the cgroup filesystem.
-
returns
Boolean Whether the required cgroup v1 files exist.
Implementation
def self.supported?(cgroup_root = DEFAULT_CGROUP_ROOT)
root = (cgroup_root || DEFAULT_CGROUP_ROOT).to_s.chomp("/")
if File.exist?("#{root}/memory/memory.limit_in_bytes") && File.exist?("#{root}/memory/memory.usage_in_bytes")
return true
end
return false
end
def initialize(cgroup_root: nil)
Initialize the cgroup v1 memory reader.
Signature
-
parameter
cgroup_rootString | Nil The root of the cgroup filesystem.
Implementation
def initialize(cgroup_root: nil)
@cgroup_root = (cgroup_root || DEFAULT_CGROUP_ROOT).to_s.chomp("/")
end
def capture
Capture host memory from the cgroup v1 filesystem.
Signature
-
returns
Host::Memory | Nil The captured host memory, if the cgroup has a finite limit.
Implementation
def capture
total = read_total
return nil unless total && total.positive?
used = read_used
return nil unless used
used = 0 if used.negative?
used = [used, total].min
swap_total, swap_used = read_swap
reclaimable = read_reclaimable
return Host::Memory.new(total, used, swap_total, swap_used, reclaimable)
end