Process::MetricsSourceProcessMetricsHostMemoryLinuxCgroupV2

class CgroupV2

Captures host memory limits and usage from a cgroup v2 filesystem.

Definitions

def self.supported?(cgroup_root = DEFAULT_CGROUP_ROOT)

Whether cgroup v2 memory metrics are available.

Signature

parameter cgroup_root String | Nil

The root of the cgroup filesystem.

returns Boolean

Whether the required cgroup v2 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.current") && File.exist?("#{root}/memory.max")
		return true
	end
	
	return false
end

def initialize(cgroup_root: nil)

Initialize the cgroup v2 memory reader.

Signature

parameter cgroup_root String | 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 v2 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
	used = 0 if used.nil? || 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