Class: Process::Metrics::Host::Memory::Linux::CgroupV2

Inherits:
Object
  • Object
show all
Defined in:
lib/process/metrics/host/memory/linux/cgroup_v2.rb

Overview

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

Constant Summary collapse

DEFAULT_CGROUP_ROOT =
"/sys/fs/cgroup"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cgroup_root: nil) ⇒ CgroupV2

Initialize the cgroup v2 memory reader.



28
29
30
# File 'lib/process/metrics/host/memory/linux/cgroup_v2.rb', line 28

def initialize(cgroup_root: nil)
	@cgroup_root = (cgroup_root || DEFAULT_CGROUP_ROOT).to_s.chomp("/")
end

Class Method Details

.supported?(cgroup_root = DEFAULT_CGROUP_ROOT) ⇒ Boolean

Whether cgroup v2 memory metrics are available.

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
# File 'lib/process/metrics/host/memory/linux/cgroup_v2.rb', line 16

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

Instance Method Details

#captureObject

Capture host memory from the cgroup v2 filesystem.



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/process/metrics/host/memory/linux/cgroup_v2.rb', line 34

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